]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
mark arm* configs as sources
[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 5976eb33535f..7dba4acf10ce 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -320,6 +320,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 66bffb24a348..9dc7cad5fa1e 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3355,6 +3355,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 084f9b8a0ba3..a705173cf452 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -645,6 +645,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
57         goto done;
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 34a3faa4886d..68457c0e1e6c 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 @@ -63,6 +63,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
108  
109         if (filp->f_op->check_flags)
110                 error = filp->f_op->check_flags(arg);
111 +       if (!error && filp->f_op->setfl)
112 +               error = filp->f_op->setfl(filp, arg);
113         if (error)
114                 return error;
115  
116 diff --git a/fs/namespace.c b/fs/namespace.c
117 index e6a7e769d25d..e0aa17f8c802 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 9ad5e3520fae..f5aed17b2ce2 100644
163 --- a/include/linux/fs.h
164 +++ b/include/linux/fs.h
165 @@ -1348,6 +1348,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 @@ -1986,6 +1987,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 @@ -2483,6 +2485,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 @@ -2663,6 +2666,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 b6829b970093..af63df9ba503 100644
199 --- a/include/linux/lockdep.h
200 +++ b/include/linux/lockdep.h
201 @@ -244,6 +244,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 f06b91ca6482..dedf7da3dd4a 100644
247 --- a/kernel/locking/lockdep.c
248 +++ b/kernel/locking/lockdep.c
249 @@ -217,7 +217,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 @@ -238,6 +238,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 8dfa36a99c74..548deeffe7d7 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 2d04e3470d4c..f57a3bed280a 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 @@ -1908,7 +1911,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 cf3d0d673f6b..6b3c1bdf9cde 100644
341 --- a/include/linux/mm.h
342 +++ b/include/linux/mm.h
343 @@ -1905,6 +1905,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 c29ab4c0cd5c..51753b926421 100644
389 --- a/include/linux/mm_types.h
390 +++ b/include/linux/mm_types.h
391 @@ -367,6 +367,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 @@ -463,6 +466,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 9d44f2d46c69..089b21708b9a 100644
413 --- a/kernel/fork.c
414 +++ b/kernel/fork.c
415 @@ -662,7 +662,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 6f9ffa968a1a..f1e338fa3d10 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 ffdfbc8b0e3c..2932f7020bfe 100644
435 --- a/mm/filemap.c
436 +++ b/mm/filemap.c
437 @@ -3431,7 +3431,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 61e6135c54ef..9a3b117076a8 100644
448 --- a/mm/mmap.c
449 +++ b/mm/mmap.c
450 @@ -189,7 +189,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 @@ -958,7 +958,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 @@ -1889,7 +1889,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 @@ -2751,7 +2751,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 @@ -2770,7 +2770,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 @@ -2943,6 +2943,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 @@ -2998,10 +3001,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 @@ -3275,7 +3302,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 9d7afc2d959e..917851d5b7c6 100644
551 --- a/mm/nommu.c
552 +++ b/mm/nommu.c
553 @@ -524,7 +524,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 @@ -656,7 +656,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 @@ -1176,7 +1176,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 @@ -1253,10 +1253,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 0989fb8472a1..040741a7fc1a 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 68457c0e1e6c..f6affcbcea4f 100644
722 --- a/fs/fcntl.c
723 +++ b/fs/fcntl.c
724 @@ -85,6 +85,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 5424e3a8df5f..d2fe19f88894 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 e0aa17f8c802..a8c5bed09874 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 1de6631a3925..3008eb37a18d 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 1d57fbde2feb..50d04ab40175 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 e0777eefd846..5e4413802f73 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 e8dd03e4561e..fd922e39d42d 100644
838 --- a/fs/xattr.c
839 +++ b/fs/xattr.c
840 @@ -386,6 +386,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 dedf7da3dd4a..2c74c70f4816 100644
850 --- a/kernel/locking/lockdep.c
851 +++ b/kernel/locking/lockdep.c
852 @@ -238,6 +238,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 dff75bcde151..30b814618535 100644
862 --- a/kernel/task_work.c
863 +++ b/kernel/task_work.c
864 @@ -180,3 +180,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 188b8f782220..6847a174a55f 100644
871 --- a/security/security.c
872 +++ b/security/security.c
873 @@ -1165,6 +1165,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 @@ -1181,6 +1182,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 @@ -1189,6 +1191,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 @@ -1209,6 +1212,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 @@ -1216,6 +1220,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 @@ -1223,6 +1228,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 @@ -1323,6 +1329,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 @@ -1520,6 +1527,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-08-02 12:12:45.158975302 +0200
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-08-02 12:12:45.158975302 +0200
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-08-02 12:12:45.158975302 +0200
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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.162308621 +0200
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 referred 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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.162308621 +0200
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-08-02 12:12:45.158975302 +0200
2433 @@ -0,0 +1,397 @@
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 +Enya Quetzalli Gomez Rodriguez made a donation (2022/5).
2814 +
2815 +Thank you very much.
2816 +Donations are always, including future donations, very important and
2817 +helpful for me to keep on developing aufs.
2818 +
2819 +
2820 +7.
2821 +----------------------------------------
2822 +If you are an experienced user, no explanation is needed. Aufs is
2823 +just a linux filesystem.
2824 +
2825 +
2826 +Enjoy!
2827 +
2828 +# Local variables: ;
2829 +# mode: text;
2830 +# End: ;
2831 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2832 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2833 +++ linux/fs/aufs/aufs.h        2022-08-02 12:12:45.162308621 +0200
2834 @@ -0,0 +1,62 @@
2835 +/* SPDX-License-Identifier: GPL-2.0 */
2836 +/*
2837 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2838 + *
2839 + * This program, aufs is free software; you can redistribute it and/or modify
2840 + * it under the terms of the GNU General Public License as published by
2841 + * the Free Software Foundation; either version 2 of the License, or
2842 + * (at your option) any later version.
2843 + *
2844 + * This program is distributed in the hope that it will be useful,
2845 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2846 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2847 + * GNU General Public License for more details.
2848 + *
2849 + * You should have received a copy of the GNU General Public License
2850 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2851 + */
2852 +
2853 +/*
2854 + * all header files
2855 + */
2856 +
2857 +#ifndef __AUFS_H__
2858 +#define __AUFS_H__
2859 +
2860 +#ifdef __KERNEL__
2861 +
2862 +#define AuStub(type, name, body, ...) \
2863 +       static inline type name(__VA_ARGS__) { body; }
2864 +
2865 +#define AuStubVoid(name, ...) \
2866 +       AuStub(void, name, , __VA_ARGS__)
2867 +#define AuStubInt0(name, ...) \
2868 +       AuStub(int, name, return 0, __VA_ARGS__)
2869 +
2870 +#include "debug.h"
2871 +
2872 +#include "branch.h"
2873 +#include "cpup.h"
2874 +#include "dcsub.h"
2875 +#include "dbgaufs.h"
2876 +#include "dentry.h"
2877 +#include "dir.h"
2878 +#include "dirren.h"
2879 +#include "dynop.h"
2880 +#include "file.h"
2881 +#include "fstype.h"
2882 +#include "hbl.h"
2883 +#include "inode.h"
2884 +#include "lcnt.h"
2885 +#include "loop.h"
2886 +#include "module.h"
2887 +#include "opts.h"
2888 +#include "rwsem.h"
2889 +#include "super.h"
2890 +#include "sysaufs.h"
2891 +#include "vfsub.h"
2892 +#include "whout.h"
2893 +#include "wkq.h"
2894 +
2895 +#endif /* __KERNEL__ */
2896 +#endif /* __AUFS_H__ */
2897 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2898 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2899 +++ linux/fs/aufs/branch.c      2022-08-02 12:12:45.162308621 +0200
2900 @@ -0,0 +1,1427 @@
2901 +// SPDX-License-Identifier: GPL-2.0
2902 +/*
2903 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2904 + *
2905 + * This program, aufs is free software; you can redistribute it and/or modify
2906 + * it under the terms of the GNU General Public License as published by
2907 + * the Free Software Foundation; either version 2 of the License, or
2908 + * (at your option) any later version.
2909 + *
2910 + * This program is distributed in the hope that it will be useful,
2911 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2912 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2913 + * GNU General Public License for more details.
2914 + *
2915 + * You should have received a copy of the GNU General Public License
2916 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2917 + */
2918 +
2919 +/*
2920 + * branch management
2921 + */
2922 +
2923 +#include <linux/compat.h>
2924 +#include <linux/statfs.h>
2925 +#include "aufs.h"
2926 +
2927 +/*
2928 + * free a single branch
2929 + */
2930 +static void au_br_do_free(struct au_branch *br)
2931 +{
2932 +       int i;
2933 +       struct au_wbr *wbr;
2934 +       struct au_dykey **key;
2935 +
2936 +       au_hnotify_fin_br(br);
2937 +       /* always, regardless the mount option */
2938 +       au_dr_hino_free(&br->br_dirren);
2939 +       au_xino_put(br);
2940 +
2941 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2942 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2943 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2944 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2945 +
2946 +       wbr = br->br_wbr;
2947 +       if (wbr) {
2948 +               for (i = 0; i < AuBrWh_Last; i++)
2949 +                       dput(wbr->wbr_wh[i]);
2950 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2951 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2952 +       }
2953 +
2954 +       if (br->br_fhsm) {
2955 +               au_br_fhsm_fin(br->br_fhsm);
2956 +               au_kfree_try_rcu(br->br_fhsm);
2957 +       }
2958 +
2959 +       key = br->br_dykey;
2960 +       for (i = 0; i < AuBrDynOp; i++, key++)
2961 +               if (*key)
2962 +                       au_dy_put(*key);
2963 +               else
2964 +                       break;
2965 +
2966 +       /* recursive lock, s_umount of branch's */
2967 +       /* synchronize_rcu(); */ /* why? */
2968 +       lockdep_off();
2969 +       path_put(&br->br_path);
2970 +       lockdep_on();
2971 +       au_kfree_rcu(wbr);
2972 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2973 +       au_lcnt_wait_for_fin(&br->br_count);
2974 +       /* I don't know why, but percpu_refcount requires this */
2975 +       /* synchronize_rcu(); */
2976 +       au_kfree_rcu(br);
2977 +}
2978 +
2979 +/*
2980 + * frees all branches
2981 + */
2982 +void au_br_free(struct au_sbinfo *sbinfo)
2983 +{
2984 +       aufs_bindex_t bmax;
2985 +       struct au_branch **br;
2986 +
2987 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2988 +
2989 +       bmax = sbinfo->si_bbot + 1;
2990 +       br = sbinfo->si_branch;
2991 +       while (bmax--)
2992 +               au_br_do_free(*br++);
2993 +}
2994 +
2995 +/*
2996 + * find the index of a branch which is specified by @br_id.
2997 + */
2998 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2999 +{
3000 +       aufs_bindex_t bindex, bbot;
3001 +
3002 +       bbot = au_sbbot(sb);
3003 +       for (bindex = 0; bindex <= bbot; bindex++)
3004 +               if (au_sbr_id(sb, bindex) == br_id)
3005 +                       return bindex;
3006 +       return -1;
3007 +}
3008 +
3009 +/* ---------------------------------------------------------------------- */
3010 +
3011 +/*
3012 + * add a branch
3013 + */
3014 +
3015 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3016 +                       struct dentry *h_root)
3017 +{
3018 +       if (unlikely(h_adding == h_root
3019 +                    || au_test_loopback_overlap(sb, h_adding)))
3020 +               return 1;
3021 +       if (h_adding->d_sb != h_root->d_sb)
3022 +               return 0;
3023 +       return au_test_subdir(h_adding, h_root)
3024 +               || au_test_subdir(h_root, h_adding);
3025 +}
3026 +
3027 +/*
3028 + * returns a newly allocated branch. @new_nbranch is a number of branches
3029 + * after adding a branch.
3030 + */
3031 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3032 +                                    int perm)
3033 +{
3034 +       struct au_branch *add_branch;
3035 +       struct dentry *root;
3036 +       struct inode *inode;
3037 +       int err;
3038 +
3039 +       err = -ENOMEM;
3040 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3041 +       if (unlikely(!add_branch))
3042 +               goto out;
3043 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3044 +       if (unlikely(!add_branch->br_xino))
3045 +               goto out_br;
3046 +       err = au_hnotify_init_br(add_branch, perm);
3047 +       if (unlikely(err))
3048 +               goto out_xino;
3049 +
3050 +       if (au_br_writable(perm)) {
3051 +               /* may be freed separately at changing the branch permission */
3052 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3053 +                                            GFP_NOFS);
3054 +               if (unlikely(!add_branch->br_wbr))
3055 +                       goto out_hnotify;
3056 +       }
3057 +
3058 +       if (au_br_fhsm(perm)) {
3059 +               err = au_fhsm_br_alloc(add_branch);
3060 +               if (unlikely(err))
3061 +                       goto out_wbr;
3062 +       }
3063 +
3064 +       root = sb->s_root;
3065 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3066 +       if (!err)
3067 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3068 +       if (!err) {
3069 +               inode = d_inode(root);
3070 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3071 +                                       /*may_shrink*/0);
3072 +       }
3073 +       if (!err)
3074 +               return add_branch; /* success */
3075 +
3076 +out_wbr:
3077 +       au_kfree_rcu(add_branch->br_wbr);
3078 +out_hnotify:
3079 +       au_hnotify_fin_br(add_branch);
3080 +out_xino:
3081 +       au_xino_put(add_branch);
3082 +out_br:
3083 +       au_kfree_rcu(add_branch);
3084 +out:
3085 +       return ERR_PTR(err);
3086 +}
3087 +
3088 +/*
3089 + * test if the branch permission is legal or not.
3090 + */
3091 +static int test_br(struct inode *inode, int brperm, char *path)
3092 +{
3093 +       int err;
3094 +
3095 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3096 +       if (!err)
3097 +               goto out;
3098 +
3099 +       err = -EINVAL;
3100 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3101 +
3102 +out:
3103 +       return err;
3104 +}
3105 +
3106 +/*
3107 + * returns:
3108 + * 0: success, the caller will add it
3109 + * plus: success, it is already unified, the caller should ignore it
3110 + * minus: error
3111 + */
3112 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3113 +{
3114 +       int err;
3115 +       aufs_bindex_t bbot, bindex;
3116 +       struct dentry *root, *h_dentry;
3117 +       struct inode *inode, *h_inode;
3118 +
3119 +       root = sb->s_root;
3120 +       bbot = au_sbbot(sb);
3121 +       if (unlikely(bbot >= 0
3122 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3123 +               err = 1;
3124 +               if (!remount) {
3125 +                       err = -EINVAL;
3126 +                       pr_err("%s duplicated\n", add->pathname);
3127 +               }
3128 +               goto out;
3129 +       }
3130 +
3131 +       err = -ENOSPC; /* -E2BIG; */
3132 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3133 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3134 +               pr_err("number of branches exceeded %s\n", add->pathname);
3135 +               goto out;
3136 +       }
3137 +
3138 +       err = -EDOM;
3139 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3140 +               pr_err("bad index %d\n", add->bindex);
3141 +               goto out;
3142 +       }
3143 +
3144 +       inode = d_inode(add->path.dentry);
3145 +       err = -ENOENT;
3146 +       if (unlikely(!inode->i_nlink)) {
3147 +               pr_err("no existence %s\n", add->pathname);
3148 +               goto out;
3149 +       }
3150 +
3151 +       err = -EINVAL;
3152 +       if (unlikely(inode->i_sb == sb)) {
3153 +               pr_err("%s must be outside\n", add->pathname);
3154 +               goto out;
3155 +       }
3156 +
3157 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3158 +               pr_err("unsupported filesystem, %s (%s)\n",
3159 +                      add->pathname, au_sbtype(inode->i_sb));
3160 +               goto out;
3161 +       }
3162 +
3163 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3164 +               pr_err("already stacked, %s (%s)\n",
3165 +                      add->pathname, au_sbtype(inode->i_sb));
3166 +               goto out;
3167 +       }
3168 +
3169 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3170 +       if (unlikely(err))
3171 +               goto out;
3172 +
3173 +       if (bbot < 0)
3174 +               return 0; /* success */
3175 +
3176 +       err = -EINVAL;
3177 +       for (bindex = 0; bindex <= bbot; bindex++)
3178 +               if (unlikely(test_overlap(sb, add->path.dentry,
3179 +                                         au_h_dptr(root, bindex)))) {
3180 +                       pr_err("%s is overlapped\n", add->pathname);
3181 +                       goto out;
3182 +               }
3183 +
3184 +       err = 0;
3185 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3186 +               h_dentry = au_h_dptr(root, 0);
3187 +               h_inode = d_inode(h_dentry);
3188 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3189 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3190 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3191 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3192 +                               add->pathname,
3193 +                               i_uid_read(inode), i_gid_read(inode),
3194 +                               (inode->i_mode & S_IALLUGO),
3195 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3196 +                               (h_inode->i_mode & S_IALLUGO));
3197 +       }
3198 +
3199 +out:
3200 +       return err;
3201 +}
3202 +
3203 +/*
3204 + * initialize or clean the whiteouts for an adding branch
3205 + */
3206 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3207 +                        int new_perm)
3208 +{
3209 +       int err, old_perm;
3210 +       aufs_bindex_t bindex;
3211 +       struct inode *h_inode;
3212 +       struct au_wbr *wbr;
3213 +       struct au_hinode *hdir;
3214 +       struct dentry *h_dentry;
3215 +
3216 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3217 +       if (unlikely(err))
3218 +               goto out;
3219 +
3220 +       wbr = br->br_wbr;
3221 +       old_perm = br->br_perm;
3222 +       br->br_perm = new_perm;
3223 +       hdir = NULL;
3224 +       h_inode = NULL;
3225 +       bindex = au_br_index(sb, br->br_id);
3226 +       if (0 <= bindex) {
3227 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3228 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3229 +       } else {
3230 +               h_dentry = au_br_dentry(br);
3231 +               h_inode = d_inode(h_dentry);
3232 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3233 +       }
3234 +       if (!wbr)
3235 +               err = au_wh_init(br, sb);
3236 +       else {
3237 +               wbr_wh_write_lock(wbr);
3238 +               err = au_wh_init(br, sb);
3239 +               wbr_wh_write_unlock(wbr);
3240 +       }
3241 +       if (hdir)
3242 +               au_hn_inode_unlock(hdir);
3243 +       else
3244 +               inode_unlock(h_inode);
3245 +       vfsub_mnt_drop_write(au_br_mnt(br));
3246 +       br->br_perm = old_perm;
3247 +
3248 +       if (!err && wbr && !au_br_writable(new_perm)) {
3249 +               au_kfree_rcu(wbr);
3250 +               br->br_wbr = NULL;
3251 +       }
3252 +
3253 +out:
3254 +       return err;
3255 +}
3256 +
3257 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3258 +                      int perm)
3259 +{
3260 +       int err;
3261 +       struct kstatfs kst;
3262 +       struct au_wbr *wbr;
3263 +
3264 +       wbr = br->br_wbr;
3265 +       au_rw_init(&wbr->wbr_wh_rwsem);
3266 +       atomic_set(&wbr->wbr_wh_running, 0);
3267 +
3268 +       /*
3269 +        * a limit for rmdir/rename a dir
3270 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3271 +        */
3272 +       err = vfs_statfs(&br->br_path, &kst);
3273 +       if (unlikely(err))
3274 +               goto out;
3275 +       err = -EINVAL;
3276 +       if (kst.f_namelen >= NAME_MAX)
3277 +               err = au_br_init_wh(sb, br, perm);
3278 +       else
3279 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3280 +                      au_br_dentry(br),
3281 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3282 +
3283 +out:
3284 +       return err;
3285 +}
3286 +
3287 +/* initialize a new branch */
3288 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3289 +                     struct au_opt_add *add)
3290 +{
3291 +       int err;
3292 +       struct au_branch *brbase;
3293 +       struct file *xf;
3294 +       struct inode *h_inode;
3295 +
3296 +       err = 0;
3297 +       br->br_perm = add->perm;
3298 +       br->br_path = add->path; /* set first, path_get() later */
3299 +       spin_lock_init(&br->br_dykey_lock);
3300 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3301 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3302 +       br->br_id = au_new_br_id(sb);
3303 +       AuDebugOn(br->br_id < 0);
3304 +
3305 +       /* always, regardless the given option */
3306 +       err = au_dr_br_init(sb, br, &add->path);
3307 +       if (unlikely(err))
3308 +               goto out_err;
3309 +
3310 +       if (au_br_writable(add->perm)) {
3311 +               err = au_wbr_init(br, sb, add->perm);
3312 +               if (unlikely(err))
3313 +                       goto out_err;
3314 +       }
3315 +
3316 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3317 +               brbase = au_sbr(sb, 0);
3318 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3319 +               AuDebugOn(!xf);
3320 +               h_inode = d_inode(add->path.dentry);
3321 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3322 +               if (unlikely(err)) {
3323 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3324 +                       goto out_err;
3325 +               }
3326 +       }
3327 +
3328 +       sysaufs_br_init(br);
3329 +       path_get(&br->br_path);
3330 +       goto out; /* success */
3331 +
3332 +out_err:
3333 +       memset(&br->br_path, 0, sizeof(br->br_path));
3334 +out:
3335 +       return err;
3336 +}
3337 +
3338 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3339 +                            struct au_branch *br, aufs_bindex_t bbot,
3340 +                            aufs_bindex_t amount)
3341 +{
3342 +       struct au_branch **brp;
3343 +
3344 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3345 +
3346 +       brp = sbinfo->si_branch + bindex;
3347 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3348 +       *brp = br;
3349 +       sbinfo->si_bbot++;
3350 +       if (unlikely(bbot < 0))
3351 +               sbinfo->si_bbot = 0;
3352 +}
3353 +
3354 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3355 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3356 +{
3357 +       struct au_hdentry *hdp;
3358 +
3359 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3360 +
3361 +       hdp = au_hdentry(dinfo, bindex);
3362 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3363 +       au_h_dentry_init(hdp);
3364 +       dinfo->di_bbot++;
3365 +       if (unlikely(bbot < 0))
3366 +               dinfo->di_btop = 0;
3367 +}
3368 +
3369 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3370 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3371 +{
3372 +       struct au_hinode *hip;
3373 +
3374 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3375 +
3376 +       hip = au_hinode(iinfo, bindex);
3377 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3378 +       au_hinode_init(hip);
3379 +       iinfo->ii_bbot++;
3380 +       if (unlikely(bbot < 0))
3381 +               iinfo->ii_btop = 0;
3382 +}
3383 +
3384 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3385 +                        aufs_bindex_t bindex)
3386 +{
3387 +       struct dentry *root, *h_dentry;
3388 +       struct inode *root_inode, *h_inode;
3389 +       aufs_bindex_t bbot, amount;
3390 +
3391 +       root = sb->s_root;
3392 +       root_inode = d_inode(root);
3393 +       bbot = au_sbbot(sb);
3394 +       amount = bbot + 1 - bindex;
3395 +       h_dentry = au_br_dentry(br);
3396 +       au_sbilist_lock();
3397 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3398 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3399 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3400 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3401 +       h_inode = d_inode(h_dentry);
3402 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3403 +       au_sbilist_unlock();
3404 +}
3405 +
3406 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3407 +{
3408 +       int err;
3409 +       aufs_bindex_t bbot, add_bindex;
3410 +       struct dentry *root, *h_dentry;
3411 +       struct inode *root_inode;
3412 +       struct au_branch *add_branch;
3413 +
3414 +       root = sb->s_root;
3415 +       root_inode = d_inode(root);
3416 +       IMustLock(root_inode);
3417 +       IiMustWriteLock(root_inode);
3418 +       err = test_add(sb, add, remount);
3419 +       if (unlikely(err < 0))
3420 +               goto out;
3421 +       if (err) {
3422 +               err = 0;
3423 +               goto out; /* success */
3424 +       }
3425 +
3426 +       bbot = au_sbbot(sb);
3427 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3428 +       err = PTR_ERR(add_branch);
3429 +       if (IS_ERR(add_branch))
3430 +               goto out;
3431 +
3432 +       err = au_br_init(add_branch, sb, add);
3433 +       if (unlikely(err)) {
3434 +               au_br_do_free(add_branch);
3435 +               goto out;
3436 +       }
3437 +
3438 +       add_bindex = add->bindex;
3439 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3440 +       au_br_do_add(sb, add_branch, add_bindex);
3441 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3442 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3443 +
3444 +       h_dentry = add->path.dentry;
3445 +       if (!add_bindex) {
3446 +               au_cpup_attr_all(root_inode, /*force*/1);
3447 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3448 +       } else
3449 +               au_add_nlink(root_inode, d_inode(h_dentry));
3450 +
3451 +out:
3452 +       return err;
3453 +}
3454 +
3455 +/* ---------------------------------------------------------------------- */
3456 +
3457 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3458 +                                      unsigned long long max __maybe_unused,
3459 +                                      void *arg)
3460 +{
3461 +       unsigned long long n;
3462 +       struct file **p, *f;
3463 +       struct hlist_bl_head *files;
3464 +       struct hlist_bl_node *pos;
3465 +       struct au_finfo *finfo;
3466 +
3467 +       n = 0;
3468 +       p = a;
3469 +       files = &au_sbi(sb)->si_files;
3470 +       hlist_bl_lock(files);
3471 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3472 +               f = finfo->fi_file;
3473 +               if (file_count(f)
3474 +                   && !special_file(file_inode(f)->i_mode)) {
3475 +                       get_file(f);
3476 +                       *p++ = f;
3477 +                       n++;
3478 +                       AuDebugOn(n > max);
3479 +               }
3480 +       }
3481 +       hlist_bl_unlock(files);
3482 +
3483 +       return n;
3484 +}
3485 +
3486 +static struct file **au_farray_alloc(struct super_block *sb,
3487 +                                    unsigned long long *max)
3488 +{
3489 +       struct au_sbinfo *sbi;
3490 +
3491 +       sbi = au_sbi(sb);
3492 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3493 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3494 +}
3495 +
3496 +static void au_farray_free(struct file **a, unsigned long long max)
3497 +{
3498 +       unsigned long long ull;
3499 +
3500 +       for (ull = 0; ull < max; ull++)
3501 +               if (a[ull])
3502 +                       fput(a[ull]);
3503 +       kvfree(a);
3504 +}
3505 +
3506 +/* ---------------------------------------------------------------------- */
3507 +
3508 +/*
3509 + * delete a branch
3510 + */
3511 +
3512 +/* to show the line number, do not make it inlined function */
3513 +#define AuVerbose(do_info, fmt, ...) do { \
3514 +       if (do_info) \
3515 +               pr_info(fmt, ##__VA_ARGS__); \
3516 +} while (0)
3517 +
3518 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3519 +                        aufs_bindex_t bbot)
3520 +{
3521 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3522 +}
3523 +
3524 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3525 +                        aufs_bindex_t bbot)
3526 +{
3527 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3528 +}
3529 +
3530 +/*
3531 + * test if the branch is deletable or not.
3532 + */
3533 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3534 +                           unsigned int sigen, const unsigned int verbose)
3535 +{
3536 +       int err, i, j, ndentry;
3537 +       aufs_bindex_t btop, bbot;
3538 +       struct au_dcsub_pages dpages;
3539 +       struct au_dpage *dpage;
3540 +       struct dentry *d;
3541 +
3542 +       err = au_dpages_init(&dpages, GFP_NOFS);
3543 +       if (unlikely(err))
3544 +               goto out;
3545 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3546 +       if (unlikely(err))
3547 +               goto out_dpages;
3548 +
3549 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3550 +               dpage = dpages.dpages + i;
3551 +               ndentry = dpage->ndentry;
3552 +               for (j = 0; !err && j < ndentry; j++) {
3553 +                       d = dpage->dentries[j];
3554 +                       AuDebugOn(au_dcount(d) <= 0);
3555 +                       if (!au_digen_test(d, sigen)) {
3556 +                               di_read_lock_child(d, AuLock_IR);
3557 +                               if (unlikely(au_dbrange_test(d))) {
3558 +                                       di_read_unlock(d, AuLock_IR);
3559 +                                       continue;
3560 +                               }
3561 +                       } else {
3562 +                               di_write_lock_child(d);
3563 +                               if (unlikely(au_dbrange_test(d))) {
3564 +                                       di_write_unlock(d);
3565 +                                       continue;
3566 +                               }
3567 +                               err = au_reval_dpath(d, sigen);
3568 +                               if (!err)
3569 +                                       di_downgrade_lock(d, AuLock_IR);
3570 +                               else {
3571 +                                       di_write_unlock(d);
3572 +                                       break;
3573 +                               }
3574 +                       }
3575 +
3576 +                       /* AuDbgDentry(d); */
3577 +                       btop = au_dbtop(d);
3578 +                       bbot = au_dbbot(d);
3579 +                       if (btop <= bindex
3580 +                           && bindex <= bbot
3581 +                           && au_h_dptr(d, bindex)
3582 +                           && au_test_dbusy(d, btop, bbot)) {
3583 +                               err = -EBUSY;
3584 +                               AuVerbose(verbose, "busy %pd\n", d);
3585 +                               AuDbgDentry(d);
3586 +                       }
3587 +                       di_read_unlock(d, AuLock_IR);
3588 +               }
3589 +       }
3590 +
3591 +out_dpages:
3592 +       au_dpages_free(&dpages);
3593 +out:
3594 +       return err;
3595 +}
3596 +
3597 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3598 +                          unsigned int sigen, const unsigned int verbose)
3599 +{
3600 +       int err;
3601 +       unsigned long long max, ull;
3602 +       struct inode *i, **array;
3603 +       aufs_bindex_t btop, bbot;
3604 +
3605 +       array = au_iarray_alloc(sb, &max);
3606 +       err = PTR_ERR(array);
3607 +       if (IS_ERR(array))
3608 +               goto out;
3609 +
3610 +       err = 0;
3611 +       AuDbg("b%d\n", bindex);
3612 +       for (ull = 0; !err && ull < max; ull++) {
3613 +               i = array[ull];
3614 +               if (unlikely(!i))
3615 +                       break;
3616 +               if (i->i_ino == AUFS_ROOT_INO)
3617 +                       continue;
3618 +
3619 +               /* AuDbgInode(i); */
3620 +               if (au_iigen(i, NULL) == sigen)
3621 +                       ii_read_lock_child(i);
3622 +               else {
3623 +                       ii_write_lock_child(i);
3624 +                       err = au_refresh_hinode_self(i);
3625 +                       au_iigen_dec(i);
3626 +                       if (!err)
3627 +                               ii_downgrade_lock(i);
3628 +                       else {
3629 +                               ii_write_unlock(i);
3630 +                               break;
3631 +                       }
3632 +               }
3633 +
3634 +               btop = au_ibtop(i);
3635 +               bbot = au_ibbot(i);
3636 +               if (btop <= bindex
3637 +                   && bindex <= bbot
3638 +                   && au_h_iptr(i, bindex)
3639 +                   && au_test_ibusy(i, btop, bbot)) {
3640 +                       err = -EBUSY;
3641 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3642 +                       AuDbgInode(i);
3643 +               }
3644 +               ii_read_unlock(i);
3645 +       }
3646 +       au_iarray_free(array, max);
3647 +
3648 +out:
3649 +       return err;
3650 +}
3651 +
3652 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3653 +                             const unsigned int verbose)
3654 +{
3655 +       int err;
3656 +       unsigned int sigen;
3657 +
3658 +       sigen = au_sigen(root->d_sb);
3659 +       DiMustNoWaiters(root);
3660 +       IiMustNoWaiters(d_inode(root));
3661 +       di_write_unlock(root);
3662 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3663 +       if (!err)
3664 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3665 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3666 +
3667 +       return err;
3668 +}
3669 +
3670 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3671 +                        struct file **to_free, int *idx)
3672 +{
3673 +       int err;
3674 +       unsigned char matched, root;
3675 +       aufs_bindex_t bindex, bbot;
3676 +       struct au_fidir *fidir;
3677 +       struct au_hfile *hfile;
3678 +
3679 +       err = 0;
3680 +       root = IS_ROOT(file->f_path.dentry);
3681 +       if (root) {
3682 +               get_file(file);
3683 +               to_free[*idx] = file;
3684 +               (*idx)++;
3685 +               goto out;
3686 +       }
3687 +
3688 +       matched = 0;
3689 +       fidir = au_fi(file)->fi_hdir;
3690 +       AuDebugOn(!fidir);
3691 +       bbot = au_fbbot_dir(file);
3692 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3693 +               hfile = fidir->fd_hfile + bindex;
3694 +               if (!hfile->hf_file)
3695 +                       continue;
3696 +
3697 +               if (hfile->hf_br->br_id == br_id) {
3698 +                       matched = 1;
3699 +                       break;
3700 +               }
3701 +       }
3702 +       if (matched)
3703 +               err = -EBUSY;
3704 +
3705 +out:
3706 +       return err;
3707 +}
3708 +
3709 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3710 +                         struct file **to_free, int opened)
3711 +{
3712 +       int err, idx;
3713 +       unsigned long long ull, max;
3714 +       aufs_bindex_t btop;
3715 +       struct file *file, **array;
3716 +       struct dentry *root;
3717 +       struct au_hfile *hfile;
3718 +
3719 +       array = au_farray_alloc(sb, &max);
3720 +       err = PTR_ERR(array);
3721 +       if (IS_ERR(array))
3722 +               goto out;
3723 +
3724 +       err = 0;
3725 +       idx = 0;
3726 +       root = sb->s_root;
3727 +       di_write_unlock(root);
3728 +       for (ull = 0; ull < max; ull++) {
3729 +               file = array[ull];
3730 +               if (unlikely(!file))
3731 +                       break;
3732 +
3733 +               /* AuDbg("%pD\n", file); */
3734 +               fi_read_lock(file);
3735 +               btop = au_fbtop(file);
3736 +               if (!d_is_dir(file->f_path.dentry)) {
3737 +                       hfile = &au_fi(file)->fi_htop;
3738 +                       if (hfile->hf_br->br_id == br_id)
3739 +                               err = -EBUSY;
3740 +               } else
3741 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3742 +               fi_read_unlock(file);
3743 +               if (unlikely(err))
3744 +                       break;
3745 +       }
3746 +       di_write_lock_child(root);
3747 +       au_farray_free(array, max);
3748 +       AuDebugOn(idx > opened);
3749 +
3750 +out:
3751 +       return err;
3752 +}
3753 +
3754 +static void br_del_file(struct file **to_free, unsigned long long opened,
3755 +                       aufs_bindex_t br_id)
3756 +{
3757 +       unsigned long long ull;
3758 +       aufs_bindex_t bindex, btop, bbot, bfound;
3759 +       struct file *file;
3760 +       struct au_fidir *fidir;
3761 +       struct au_hfile *hfile;
3762 +
3763 +       for (ull = 0; ull < opened; ull++) {
3764 +               file = to_free[ull];
3765 +               if (unlikely(!file))
3766 +                       break;
3767 +
3768 +               /* AuDbg("%pD\n", file); */
3769 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3770 +               bfound = -1;
3771 +               fidir = au_fi(file)->fi_hdir;
3772 +               AuDebugOn(!fidir);
3773 +               fi_write_lock(file);
3774 +               btop = au_fbtop(file);
3775 +               bbot = au_fbbot_dir(file);
3776 +               for (bindex = btop; bindex <= bbot; bindex++) {
3777 +                       hfile = fidir->fd_hfile + bindex;
3778 +                       if (!hfile->hf_file)
3779 +                               continue;
3780 +
3781 +                       if (hfile->hf_br->br_id == br_id) {
3782 +                               bfound = bindex;
3783 +                               break;
3784 +                       }
3785 +               }
3786 +               AuDebugOn(bfound < 0);
3787 +               au_set_h_fptr(file, bfound, NULL);
3788 +               if (bfound == btop) {
3789 +                       for (btop++; btop <= bbot; btop++)
3790 +                               if (au_hf_dir(file, btop)) {
3791 +                                       au_set_fbtop(file, btop);
3792 +                                       break;
3793 +                               }
3794 +               }
3795 +               fi_write_unlock(file);
3796 +       }
3797 +}
3798 +
3799 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3800 +                            const aufs_bindex_t bindex,
3801 +                            const aufs_bindex_t bbot)
3802 +{
3803 +       struct au_branch **brp, **p;
3804 +
3805 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3806 +
3807 +       brp = sbinfo->si_branch + bindex;
3808 +       if (bindex < bbot)
3809 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3810 +       sbinfo->si_branch[0 + bbot] = NULL;
3811 +       sbinfo->si_bbot--;
3812 +
3813 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3814 +                       /*may_shrink*/1);
3815 +       if (p)
3816 +               sbinfo->si_branch = p;
3817 +       /* harmless error */
3818 +}
3819 +
3820 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3821 +                            const aufs_bindex_t bbot)
3822 +{
3823 +       struct au_hdentry *hdp, *p;
3824 +
3825 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3826 +
3827 +       hdp = au_hdentry(dinfo, bindex);
3828 +       if (bindex < bbot)
3829 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3830 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3831 +       dinfo->di_bbot--;
3832 +
3833 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3834 +                       /*may_shrink*/1);
3835 +       if (p)
3836 +               dinfo->di_hdentry = p;
3837 +       /* harmless error */
3838 +}
3839 +
3840 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3841 +                            const aufs_bindex_t bbot)
3842 +{
3843 +       struct au_hinode *hip, *p;
3844 +
3845 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3846 +
3847 +       hip = au_hinode(iinfo, bindex);
3848 +       if (bindex < bbot)
3849 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3850 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3851 +       iinfo->ii_bbot--;
3852 +
3853 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3854 +                       /*may_shrink*/1);
3855 +       if (p)
3856 +               iinfo->ii_hinode = p;
3857 +       /* harmless error */
3858 +}
3859 +
3860 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3861 +                        struct au_branch *br)
3862 +{
3863 +       aufs_bindex_t bbot;
3864 +       struct au_sbinfo *sbinfo;
3865 +       struct dentry *root, *h_root;
3866 +       struct inode *inode, *h_inode;
3867 +       struct au_hinode *hinode;
3868 +
3869 +       SiMustWriteLock(sb);
3870 +
3871 +       root = sb->s_root;
3872 +       inode = d_inode(root);
3873 +       sbinfo = au_sbi(sb);
3874 +       bbot = sbinfo->si_bbot;
3875 +
3876 +       h_root = au_h_dptr(root, bindex);
3877 +       hinode = au_hi(inode, bindex);
3878 +       h_inode = au_igrab(hinode->hi_inode);
3879 +       au_hiput(hinode);
3880 +
3881 +       au_sbilist_lock();
3882 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3883 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3884 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3885 +       au_sbilist_unlock();
3886 +
3887 +       /* ignore an error */
3888 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3889 +
3890 +       dput(h_root);
3891 +       iput(h_inode);
3892 +       au_br_do_free(br);
3893 +}
3894 +
3895 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3896 +                                  unsigned long long max, void *arg)
3897 +{
3898 +       return max;
3899 +}
3900 +
3901 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3902 +{
3903 +       int err, rerr, i;
3904 +       unsigned long long opened;
3905 +       unsigned int mnt_flags;
3906 +       aufs_bindex_t bindex, bbot, br_id;
3907 +       unsigned char do_wh, verbose;
3908 +       struct au_branch *br;
3909 +       struct au_wbr *wbr;
3910 +       struct dentry *root;
3911 +       struct file **to_free;
3912 +
3913 +       err = 0;
3914 +       opened = 0;
3915 +       to_free = NULL;
3916 +       root = sb->s_root;
3917 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3918 +       if (bindex < 0) {
3919 +               if (remount)
3920 +                       goto out; /* success */
3921 +               err = -ENOENT;
3922 +               pr_err("%s no such branch\n", del->pathname);
3923 +               goto out;
3924 +       }
3925 +       AuDbg("bindex b%d\n", bindex);
3926 +
3927 +       err = -EBUSY;
3928 +       mnt_flags = au_mntflags(sb);
3929 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3930 +       bbot = au_sbbot(sb);
3931 +       if (unlikely(!bbot)) {
3932 +               AuVerbose(verbose, "no more branches left\n");
3933 +               goto out;
3934 +       }
3935 +
3936 +       br = au_sbr(sb, bindex);
3937 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3938 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3939 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3940 +               goto out;
3941 +       }
3942 +
3943 +       br_id = br->br_id;
3944 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3945 +       if (unlikely(opened)) {
3946 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3947 +               err = PTR_ERR(to_free);
3948 +               if (IS_ERR(to_free))
3949 +                       goto out;
3950 +
3951 +               err = test_file_busy(sb, br_id, to_free, opened);
3952 +               if (unlikely(err)) {
3953 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3954 +                       goto out;
3955 +               }
3956 +       }
3957 +
3958 +       wbr = br->br_wbr;
3959 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3960 +       if (do_wh) {
3961 +               /* instead of WbrWhMustWriteLock(wbr) */
3962 +               SiMustWriteLock(sb);
3963 +               for (i = 0; i < AuBrWh_Last; i++) {
3964 +                       dput(wbr->wbr_wh[i]);
3965 +                       wbr->wbr_wh[i] = NULL;
3966 +               }
3967 +       }
3968 +
3969 +       err = test_children_busy(root, bindex, verbose);
3970 +       if (unlikely(err)) {
3971 +               if (do_wh)
3972 +                       goto out_wh;
3973 +               goto out;
3974 +       }
3975 +
3976 +       err = 0;
3977 +       if (to_free) {
3978 +               /*
3979 +                * now we confirmed the branch is deletable.
3980 +                * let's free the remaining opened dirs on the branch.
3981 +                */
3982 +               di_write_unlock(root);
3983 +               br_del_file(to_free, opened, br_id);
3984 +               di_write_lock_child(root);
3985 +       }
3986 +
3987 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3988 +       dbgaufs_xino_del(br);           /* remove one */
3989 +       au_br_do_del(sb, bindex, br);
3990 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3991 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
3992 +
3993 +       if (!bindex) {
3994 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3995 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3996 +       } else
3997 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3998 +       if (au_opt_test(mnt_flags, PLINK))
3999 +               au_plink_half_refresh(sb, br_id);
4000 +
4001 +       goto out; /* success */
4002 +
4003 +out_wh:
4004 +       /* revert */
4005 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4006 +       if (rerr)
4007 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4008 +                       del->pathname, rerr);
4009 +out:
4010 +       if (to_free)
4011 +               au_farray_free(to_free, opened);
4012 +       return err;
4013 +}
4014 +
4015 +/* ---------------------------------------------------------------------- */
4016 +
4017 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4018 +{
4019 +       int err;
4020 +       aufs_bindex_t btop, bbot;
4021 +       struct aufs_ibusy ibusy;
4022 +       struct inode *inode, *h_inode;
4023 +
4024 +       err = -EPERM;
4025 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4026 +               goto out;
4027 +
4028 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4029 +       if (!err)
4030 +               /* VERIFY_WRITE */
4031 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4032 +       if (unlikely(err)) {
4033 +               err = -EFAULT;
4034 +               AuTraceErr(err);
4035 +               goto out;
4036 +       }
4037 +
4038 +       err = -EINVAL;
4039 +       si_read_lock(sb, AuLock_FLUSH);
4040 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4041 +               goto out_unlock;
4042 +
4043 +       err = 0;
4044 +       ibusy.h_ino = 0; /* invalid */
4045 +       inode = ilookup(sb, ibusy.ino);
4046 +       if (!inode
4047 +           || inode->i_ino == AUFS_ROOT_INO
4048 +           || au_is_bad_inode(inode))
4049 +               goto out_unlock;
4050 +
4051 +       ii_read_lock_child(inode);
4052 +       btop = au_ibtop(inode);
4053 +       bbot = au_ibbot(inode);
4054 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4055 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4056 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4057 +                       ibusy.h_ino = h_inode->i_ino;
4058 +       }
4059 +       ii_read_unlock(inode);
4060 +       iput(inode);
4061 +
4062 +out_unlock:
4063 +       si_read_unlock(sb);
4064 +       if (!err) {
4065 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4066 +               if (unlikely(err)) {
4067 +                       err = -EFAULT;
4068 +                       AuTraceErr(err);
4069 +               }
4070 +       }
4071 +out:
4072 +       return err;
4073 +}
4074 +
4075 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4076 +{
4077 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4078 +}
4079 +
4080 +#ifdef CONFIG_COMPAT
4081 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4082 +{
4083 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4084 +}
4085 +#endif
4086 +
4087 +/* ---------------------------------------------------------------------- */
4088 +
4089 +/*
4090 + * change a branch permission
4091 + */
4092 +
4093 +static void au_warn_ima(void)
4094 +{
4095 +#ifdef CONFIG_IMA
4096 +       /* since it doesn't support mark_files_ro() */
4097 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4098 +#endif
4099 +}
4100 +
4101 +static int do_need_sigen_inc(int a, int b)
4102 +{
4103 +       return au_br_whable(a) && !au_br_whable(b);
4104 +}
4105 +
4106 +static int need_sigen_inc(int old, int new)
4107 +{
4108 +       return do_need_sigen_inc(old, new)
4109 +               || do_need_sigen_inc(new, old);
4110 +}
4111 +
4112 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4113 +{
4114 +       int err, do_warn;
4115 +       unsigned int mnt_flags;
4116 +       unsigned long long ull, max;
4117 +       aufs_bindex_t br_id;
4118 +       unsigned char verbose, writer;
4119 +       struct file *file, *hf, **array;
4120 +       struct au_hfile *hfile;
4121 +       struct inode *h_inode;
4122 +
4123 +       mnt_flags = au_mntflags(sb);
4124 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4125 +
4126 +       array = au_farray_alloc(sb, &max);
4127 +       err = PTR_ERR(array);
4128 +       if (IS_ERR(array))
4129 +               goto out;
4130 +
4131 +       do_warn = 0;
4132 +       br_id = au_sbr_id(sb, bindex);
4133 +       for (ull = 0; ull < max; ull++) {
4134 +               file = array[ull];
4135 +               if (unlikely(!file))
4136 +                       break;
4137 +
4138 +               /* AuDbg("%pD\n", file); */
4139 +               fi_read_lock(file);
4140 +               if (unlikely(au_test_mmapped(file))) {
4141 +                       err = -EBUSY;
4142 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4143 +                       AuDbgFile(file);
4144 +                       FiMustNoWaiters(file);
4145 +                       fi_read_unlock(file);
4146 +                       goto out_array;
4147 +               }
4148 +
4149 +               hfile = &au_fi(file)->fi_htop;
4150 +               hf = hfile->hf_file;
4151 +               if (!d_is_reg(file->f_path.dentry)
4152 +                   || !(file->f_mode & FMODE_WRITE)
4153 +                   || hfile->hf_br->br_id != br_id
4154 +                   || !(hf->f_mode & FMODE_WRITE))
4155 +                       array[ull] = NULL;
4156 +               else {
4157 +                       do_warn = 1;
4158 +                       get_file(file);
4159 +               }
4160 +
4161 +               FiMustNoWaiters(file);
4162 +               fi_read_unlock(file);
4163 +               fput(file);
4164 +       }
4165 +
4166 +       err = 0;
4167 +       if (do_warn)
4168 +               au_warn_ima();
4169 +
4170 +       for (ull = 0; ull < max; ull++) {
4171 +               file = array[ull];
4172 +               if (!file)
4173 +                       continue;
4174 +
4175 +               /* todo: already flushed? */
4176 +               /*
4177 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4178 +                * approach which resets f_mode and calls mnt_drop_write() and
4179 +                * file_release_write() for each file, because the branch
4180 +                * attribute in aufs world is totally different from the native
4181 +                * fs rw/ro mode.
4182 +                */
4183 +               /* fi_read_lock(file); */
4184 +               hfile = &au_fi(file)->fi_htop;
4185 +               hf = hfile->hf_file;
4186 +               /* fi_read_unlock(file); */
4187 +               spin_lock(&hf->f_lock);
4188 +               writer = !!(hf->f_mode & FMODE_WRITER);
4189 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4190 +               spin_unlock(&hf->f_lock);
4191 +               if (writer) {
4192 +                       h_inode = file_inode(hf);
4193 +                       if (hf->f_mode & FMODE_READ)
4194 +                               i_readcount_inc(h_inode);
4195 +                       put_write_access(h_inode);
4196 +                       __mnt_drop_write(hf->f_path.mnt);
4197 +               }
4198 +       }
4199 +
4200 +out_array:
4201 +       au_farray_free(array, max);
4202 +out:
4203 +       AuTraceErr(err);
4204 +       return err;
4205 +}
4206 +
4207 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4208 +             int *do_refresh)
4209 +{
4210 +       int err, rerr;
4211 +       aufs_bindex_t bindex;
4212 +       struct dentry *root;
4213 +       struct au_branch *br;
4214 +       struct au_br_fhsm *bf;
4215 +
4216 +       root = sb->s_root;
4217 +       bindex = au_find_dbindex(root, mod->h_root);
4218 +       if (bindex < 0) {
4219 +               if (remount)
4220 +                       return 0; /* success */
4221 +               err = -ENOENT;
4222 +               pr_err("%s no such branch\n", mod->path);
4223 +               goto out;
4224 +       }
4225 +       AuDbg("bindex b%d\n", bindex);
4226 +
4227 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4228 +       if (unlikely(err))
4229 +               goto out;
4230 +
4231 +       br = au_sbr(sb, bindex);
4232 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4233 +       if (br->br_perm == mod->perm)
4234 +               return 0; /* success */
4235 +
4236 +       /* pre-allocate for non-fhsm --> fhsm */
4237 +       bf = NULL;
4238 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4239 +               err = au_fhsm_br_alloc(br);
4240 +               if (unlikely(err))
4241 +                       goto out;
4242 +               bf = br->br_fhsm;
4243 +               br->br_fhsm = NULL;
4244 +       }
4245 +
4246 +       if (au_br_writable(br->br_perm)) {
4247 +               /* remove whiteout base */
4248 +               err = au_br_init_wh(sb, br, mod->perm);
4249 +               if (unlikely(err))
4250 +                       goto out_bf;
4251 +
4252 +               if (!au_br_writable(mod->perm)) {
4253 +                       /* rw --> ro, file might be mmapped */
4254 +                       DiMustNoWaiters(root);
4255 +                       IiMustNoWaiters(d_inode(root));
4256 +                       di_write_unlock(root);
4257 +                       err = au_br_mod_files_ro(sb, bindex);
4258 +                       /* aufs_write_lock() calls ..._child() */
4259 +                       di_write_lock_child(root);
4260 +
4261 +                       if (unlikely(err)) {
4262 +                               rerr = -ENOMEM;
4263 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4264 +                                                    GFP_NOFS);
4265 +                               if (br->br_wbr)
4266 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4267 +                               if (unlikely(rerr)) {
4268 +                                       AuIOErr("nested error %d (%d)\n",
4269 +                                               rerr, err);
4270 +                                       br->br_perm = mod->perm;
4271 +                               }
4272 +                       }
4273 +               }
4274 +       } else if (au_br_writable(mod->perm)) {
4275 +               /* ro --> rw */
4276 +               err = -ENOMEM;
4277 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4278 +               if (br->br_wbr) {
4279 +                       err = au_wbr_init(br, sb, mod->perm);
4280 +                       if (unlikely(err)) {
4281 +                               au_kfree_rcu(br->br_wbr);
4282 +                               br->br_wbr = NULL;
4283 +                       }
4284 +               }
4285 +       }
4286 +       if (unlikely(err))
4287 +               goto out_bf;
4288 +
4289 +       if (au_br_fhsm(br->br_perm)) {
4290 +               if (!au_br_fhsm(mod->perm)) {
4291 +                       /* fhsm --> non-fhsm */
4292 +                       au_br_fhsm_fin(br->br_fhsm);
4293 +                       au_kfree_rcu(br->br_fhsm);
4294 +                       br->br_fhsm = NULL;
4295 +               }
4296 +       } else if (au_br_fhsm(mod->perm))
4297 +               /* non-fhsm --> fhsm */
4298 +               br->br_fhsm = bf;
4299 +
4300 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4301 +       br->br_perm = mod->perm;
4302 +       goto out; /* success */
4303 +
4304 +out_bf:
4305 +       au_kfree_try_rcu(bf);
4306 +out:
4307 +       AuTraceErr(err);
4308 +       return err;
4309 +}
4310 +
4311 +/* ---------------------------------------------------------------------- */
4312 +
4313 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4314 +{
4315 +       int err;
4316 +       struct kstatfs kstfs;
4317 +
4318 +       err = vfs_statfs(&br->br_path, &kstfs);
4319 +       if (!err) {
4320 +               stfs->f_blocks = kstfs.f_blocks;
4321 +               stfs->f_bavail = kstfs.f_bavail;
4322 +               stfs->f_files = kstfs.f_files;
4323 +               stfs->f_ffree = kstfs.f_ffree;
4324 +       }
4325 +
4326 +       return err;
4327 +}
4328 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4329 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4330 +++ linux/fs/aufs/branch.h      2022-08-02 12:12:45.162308621 +0200
4331 @@ -0,0 +1,375 @@
4332 +/* SPDX-License-Identifier: GPL-2.0 */
4333 +/*
4334 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4335 + *
4336 + * This program, aufs is free software; you can redistribute it and/or modify
4337 + * it under the terms of the GNU General Public License as published by
4338 + * the Free Software Foundation; either version 2 of the License, or
4339 + * (at your option) any later version.
4340 + *
4341 + * This program is distributed in the hope that it will be useful,
4342 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4343 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4344 + * GNU General Public License for more details.
4345 + *
4346 + * You should have received a copy of the GNU General Public License
4347 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4348 + */
4349 +
4350 +/*
4351 + * branch filesystems and xino for them
4352 + */
4353 +
4354 +#ifndef __AUFS_BRANCH_H__
4355 +#define __AUFS_BRANCH_H__
4356 +
4357 +#ifdef __KERNEL__
4358 +
4359 +#include <linux/mount.h>
4360 +#include "dirren.h"
4361 +#include "dynop.h"
4362 +#include "lcnt.h"
4363 +#include "rwsem.h"
4364 +#include "super.h"
4365 +
4366 +/* ---------------------------------------------------------------------- */
4367 +
4368 +/* a xino file */
4369 +struct au_xino {
4370 +       struct file             **xi_file;
4371 +       unsigned int            xi_nfile;
4372 +
4373 +       struct {
4374 +               spinlock_t              spin;
4375 +               ino_t                   *array;
4376 +               int                     total;
4377 +               /* reserved for future use */
4378 +               /* unsigned long        *bitmap; */
4379 +               wait_queue_head_t       wqh;
4380 +       } xi_nondir;
4381 +
4382 +       struct mutex            xi_mtx; /* protects xi_file array */
4383 +       struct hlist_bl_head    xi_writing;
4384 +
4385 +       atomic_t                xi_truncating;
4386 +
4387 +       struct kref             xi_kref;
4388 +};
4389 +
4390 +/* File-based Hierarchical Storage Management */
4391 +struct au_br_fhsm {
4392 +#ifdef CONFIG_AUFS_FHSM
4393 +       struct mutex            bf_lock;
4394 +       unsigned long           bf_jiffy;
4395 +       struct aufs_stfs        bf_stfs;
4396 +       int                     bf_readable;
4397 +#endif
4398 +};
4399 +
4400 +/* members for writable branch only */
4401 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4402 +struct au_wbr {
4403 +       struct au_rwsem         wbr_wh_rwsem;
4404 +       struct dentry           *wbr_wh[AuBrWh_Last];
4405 +       atomic_t                wbr_wh_running;
4406 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4407 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4408 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4409 +
4410 +       /* mfs mode */
4411 +       unsigned long long      wbr_bytes;
4412 +};
4413 +
4414 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4415 +#define AuBrDynOp (AuDyLast * 4)
4416 +
4417 +#ifdef CONFIG_AUFS_HFSNOTIFY
4418 +/* support for asynchronous destruction */
4419 +struct au_br_hfsnotify {
4420 +       struct fsnotify_group   *hfsn_group;
4421 +};
4422 +#endif
4423 +
4424 +/* sysfs entries */
4425 +struct au_brsysfs {
4426 +       char                    name[16];
4427 +       struct attribute        attr;
4428 +};
4429 +
4430 +enum {
4431 +       AuBrSysfs_BR,
4432 +       AuBrSysfs_BRID,
4433 +       AuBrSysfs_Last
4434 +};
4435 +
4436 +/* protected by superblock rwsem */
4437 +struct au_branch {
4438 +       struct au_xino          *br_xino;
4439 +
4440 +       aufs_bindex_t           br_id;
4441 +
4442 +       int                     br_perm;
4443 +       struct path             br_path;
4444 +       spinlock_t              br_dykey_lock;
4445 +       struct au_dykey         *br_dykey[AuBrDynOp];
4446 +       au_lcnt_t               br_nfiles;      /* opened files */
4447 +       au_lcnt_t               br_count;       /* in-use for other */
4448 +
4449 +       struct au_wbr           *br_wbr;
4450 +       struct au_br_fhsm       *br_fhsm;
4451 +
4452 +#ifdef CONFIG_AUFS_HFSNOTIFY
4453 +       struct au_br_hfsnotify  *br_hfsn;
4454 +#endif
4455 +
4456 +#ifdef CONFIG_SYSFS
4457 +       /* entries under sysfs per mount-point */
4458 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4459 +#endif
4460 +
4461 +#ifdef CONFIG_DEBUG_FS
4462 +       struct dentry            *br_dbgaufs; /* xino */
4463 +#endif
4464 +
4465 +       struct au_dr_br         br_dirren;
4466 +};
4467 +
4468 +/* ---------------------------------------------------------------------- */
4469 +
4470 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4471 +{
4472 +       return br->br_path.mnt;
4473 +}
4474 +
4475 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4476 +{
4477 +       return br->br_path.dentry;
4478 +}
4479 +
4480 +static inline struct user_namespace *au_br_userns(struct au_branch *br)
4481 +{
4482 +       return mnt_user_ns(br->br_path.mnt);
4483 +}
4484 +
4485 +static inline struct super_block *au_br_sb(struct au_branch *br)
4486 +{
4487 +       return au_br_mnt(br)->mnt_sb;
4488 +}
4489 +
4490 +static inline int au_br_rdonly(struct au_branch *br)
4491 +{
4492 +       return (sb_rdonly(au_br_sb(br))
4493 +               || !au_br_writable(br->br_perm))
4494 +               ? -EROFS : 0;
4495 +}
4496 +
4497 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4498 +{
4499 +#ifdef CONFIG_AUFS_HNOTIFY
4500 +       return !(brperm & AuBrPerm_RR);
4501 +#else
4502 +       return 0;
4503 +#endif
4504 +}
4505 +
4506 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4507 +{
4508 +       int err, exec_flag;
4509 +
4510 +       err = 0;
4511 +       exec_flag = oflag & __FMODE_EXEC;
4512 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4513 +               err = -EACCES;
4514 +
4515 +       return err;
4516 +}
4517 +
4518 +static inline void au_xino_get(struct au_branch *br)
4519 +{
4520 +       struct au_xino *xi;
4521 +
4522 +       xi = br->br_xino;
4523 +       if (xi)
4524 +               kref_get(&xi->xi_kref);
4525 +}
4526 +
4527 +static inline int au_xino_count(struct au_branch *br)
4528 +{
4529 +       int v;
4530 +       struct au_xino *xi;
4531 +
4532 +       v = 0;
4533 +       xi = br->br_xino;
4534 +       if (xi)
4535 +               v = kref_read(&xi->xi_kref);
4536 +
4537 +       return v;
4538 +}
4539 +
4540 +/* ---------------------------------------------------------------------- */
4541 +
4542 +/* branch.c */
4543 +struct au_sbinfo;
4544 +void au_br_free(struct au_sbinfo *sinfo);
4545 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4546 +struct au_opt_add;
4547 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4548 +struct au_opt_del;
4549 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4550 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4551 +#ifdef CONFIG_COMPAT
4552 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4553 +#endif
4554 +struct au_opt_mod;
4555 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4556 +             int *do_refresh);
4557 +struct aufs_stfs;
4558 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4559 +
4560 +/* xino.c */
4561 +static const loff_t au_loff_max = LLONG_MAX;
4562 +
4563 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4564 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4565 +                           int wbrtop);
4566 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4567 +                            struct file *copy_src);
4568 +struct au_xi_new {
4569 +       struct au_xino *xi;     /* switch between xino and xigen */
4570 +       int idx;
4571 +       struct path *base;
4572 +       struct file *copy_src;
4573 +};
4574 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4575 +
4576 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4577 +                ino_t *ino);
4578 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4579 +                 ino_t ino);
4580 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4581 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4582 +
4583 +int au_xib_trunc(struct super_block *sb);
4584 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4585 +
4586 +struct au_xino *au_xino_alloc(unsigned int nfile);
4587 +int au_xino_put(struct au_branch *br);
4588 +struct file *au_xino_file1(struct au_xino *xi);
4589 +
4590 +struct au_opt_xino;
4591 +void au_xino_clr(struct super_block *sb);
4592 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4593 +struct file *au_xino_def(struct super_block *sb);
4594 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4595 +                   struct path *base);
4596 +
4597 +ino_t au_xino_new_ino(struct super_block *sb);
4598 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4599 +
4600 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4601 +                      ino_t h_ino, int idx);
4602 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4603 +                     int *idx);
4604 +
4605 +int au_xino_path(struct seq_file *seq, struct file *file);
4606 +
4607 +/* ---------------------------------------------------------------------- */
4608 +
4609 +/* @idx is signed to accept -1 meaning the first file */
4610 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4611 +{
4612 +       struct file *file;
4613 +
4614 +       file = NULL;
4615 +       if (!xi)
4616 +               goto out;
4617 +
4618 +       if (idx >= 0) {
4619 +               if (idx < xi->xi_nfile)
4620 +                       file = xi->xi_file[idx];
4621 +       } else
4622 +               file = au_xino_file1(xi);
4623 +
4624 +out:
4625 +       return file;
4626 +}
4627 +
4628 +/* ---------------------------------------------------------------------- */
4629 +
4630 +/* Superblock to branch */
4631 +static inline
4632 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4633 +{
4634 +       return au_sbr(sb, bindex)->br_id;
4635 +}
4636 +
4637 +static inline
4638 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4639 +{
4640 +       return au_br_mnt(au_sbr(sb, bindex));
4641 +}
4642 +
4643 +static inline
4644 +struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4645 +{
4646 +       return au_br_userns(au_sbr(sb, bindex));
4647 +}
4648 +
4649 +static inline
4650 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4651 +{
4652 +       return au_br_sb(au_sbr(sb, bindex));
4653 +}
4654 +
4655 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4656 +{
4657 +       return au_sbr(sb, bindex)->br_perm;
4658 +}
4659 +
4660 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4661 +{
4662 +       return au_br_whable(au_sbr_perm(sb, bindex));
4663 +}
4664 +
4665 +/* ---------------------------------------------------------------------- */
4666 +
4667 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4668 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4669 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4670 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4671 +/*
4672 +#define wbr_wh_read_trylock_nested(wbr) \
4673 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4674 +#define wbr_wh_write_trylock_nested(wbr) \
4675 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4676 +*/
4677 +
4678 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4679 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4680 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4681 +
4682 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4683 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4684 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4685 +
4686 +/* ---------------------------------------------------------------------- */
4687 +
4688 +#ifdef CONFIG_AUFS_FHSM
4689 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4690 +{
4691 +       mutex_init(&brfhsm->bf_lock);
4692 +       brfhsm->bf_jiffy = 0;
4693 +       brfhsm->bf_readable = 0;
4694 +}
4695 +
4696 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4697 +{
4698 +       mutex_destroy(&brfhsm->bf_lock);
4699 +}
4700 +#else
4701 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4702 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4703 +#endif
4704 +
4705 +#endif /* __KERNEL__ */
4706 +#endif /* __AUFS_BRANCH_H__ */
4707 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4708 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4709 +++ linux/fs/aufs/conf.mk       2022-08-02 12:12:45.162308621 +0200
4710 @@ -0,0 +1,40 @@
4711 +# SPDX-License-Identifier: GPL-2.0
4712 +
4713 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4714 +
4715 +define AuConf
4716 +ifdef ${1}
4717 +AuConfStr += ${1}=${${1}}
4718 +endif
4719 +endef
4720 +
4721 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4722 +       SBILIST \
4723 +       HNOTIFY HFSNOTIFY \
4724 +       EXPORT INO_T_64 \
4725 +       XATTR \
4726 +       FHSM \
4727 +       RDU \
4728 +       DIRREN \
4729 +       SHWH \
4730 +       BR_RAMFS \
4731 +       BR_FUSE POLL \
4732 +       BR_HFSPLUS \
4733 +       BDEV_LOOP \
4734 +       DEBUG MAGIC_SYSRQ
4735 +$(foreach i, ${AuConfAll}, \
4736 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4737 +
4738 +AuConfName = ${obj}/conf.str
4739 +${AuConfName}.tmp: FORCE
4740 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4741 +${AuConfName}: ${AuConfName}.tmp
4742 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4743 +       echo '  GEN    ' $@; \
4744 +       cp -p $< $@; \
4745 +       }
4746 +FORCE:
4747 +clean-files += ${AuConfName} ${AuConfName}.tmp
4748 +${obj}/sysfs.o: ${AuConfName}
4749 +
4750 +-include ${srctree}/${src}/conf_priv.mk
4751 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4752 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4753 +++ linux/fs/aufs/cpup.c        2022-08-02 12:12:45.162308621 +0200
4754 @@ -0,0 +1,1459 @@
4755 +// SPDX-License-Identifier: GPL-2.0
4756 +/*
4757 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4758 + *
4759 + * This program, aufs is free software; you can redistribute it and/or modify
4760 + * it under the terms of the GNU General Public License as published by
4761 + * the Free Software Foundation; either version 2 of the License, or
4762 + * (at your option) any later version.
4763 + *
4764 + * This program is distributed in the hope that it will be useful,
4765 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4766 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4767 + * GNU General Public License for more details.
4768 + *
4769 + * You should have received a copy of the GNU General Public License
4770 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4771 + */
4772 +
4773 +/*
4774 + * copy-up functions, see wbr_policy.c for copy-down
4775 + */
4776 +
4777 +#include <linux/fs_stack.h>
4778 +#include <linux/mm.h>
4779 +#include <linux/task_work.h>
4780 +#include "aufs.h"
4781 +
4782 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4783 +{
4784 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4785 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4786 +
4787 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4788 +
4789 +       dst->i_flags |= iflags & ~mask;
4790 +       if (au_test_fs_notime(dst->i_sb))
4791 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4792 +}
4793 +
4794 +void au_cpup_attr_timesizes(struct inode *inode)
4795 +{
4796 +       struct inode *h_inode;
4797 +
4798 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4799 +       fsstack_copy_attr_times(inode, h_inode);
4800 +       fsstack_copy_inode_size(inode, h_inode);
4801 +}
4802 +
4803 +void au_cpup_attr_nlink(struct inode *inode, int force)
4804 +{
4805 +       struct inode *h_inode;
4806 +       struct super_block *sb;
4807 +       aufs_bindex_t bindex, bbot;
4808 +
4809 +       sb = inode->i_sb;
4810 +       bindex = au_ibtop(inode);
4811 +       h_inode = au_h_iptr(inode, bindex);
4812 +       if (!force
4813 +           && !S_ISDIR(h_inode->i_mode)
4814 +           && au_opt_test(au_mntflags(sb), PLINK)
4815 +           && au_plink_test(inode))
4816 +               return;
4817 +
4818 +       /*
4819 +        * 0 can happen in revalidating.
4820 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4821 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4822 +        * case.
4823 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4824 +        *       the incorrect link count.
4825 +        */
4826 +       set_nlink(inode, h_inode->i_nlink);
4827 +
4828 +       /*
4829 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4830 +        * it may includes whplink directory.
4831 +        */
4832 +       if (S_ISDIR(h_inode->i_mode)) {
4833 +               bbot = au_ibbot(inode);
4834 +               for (bindex++; bindex <= bbot; bindex++) {
4835 +                       h_inode = au_h_iptr(inode, bindex);
4836 +                       if (h_inode)
4837 +                               au_add_nlink(inode, h_inode);
4838 +               }
4839 +       }
4840 +}
4841 +
4842 +void au_cpup_attr_changeable(struct inode *inode)
4843 +{
4844 +       struct inode *h_inode;
4845 +
4846 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4847 +       inode->i_mode = h_inode->i_mode;
4848 +       inode->i_uid = h_inode->i_uid;
4849 +       inode->i_gid = h_inode->i_gid;
4850 +       au_cpup_attr_timesizes(inode);
4851 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4852 +}
4853 +
4854 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4855 +{
4856 +       struct au_iinfo *iinfo = au_ii(inode);
4857 +
4858 +       IiMustWriteLock(inode);
4859 +
4860 +       iinfo->ii_higen = h_inode->i_generation;
4861 +       iinfo->ii_hsb1 = h_inode->i_sb;
4862 +}
4863 +
4864 +void au_cpup_attr_all(struct inode *inode, int force)
4865 +{
4866 +       struct inode *h_inode;
4867 +
4868 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4869 +       au_cpup_attr_changeable(inode);
4870 +       if (inode->i_nlink > 0)
4871 +               au_cpup_attr_nlink(inode, force);
4872 +       inode->i_rdev = h_inode->i_rdev;
4873 +       inode->i_blkbits = h_inode->i_blkbits;
4874 +       au_cpup_igen(inode, h_inode);
4875 +}
4876 +
4877 +/* ---------------------------------------------------------------------- */
4878 +
4879 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4880 +
4881 +/* keep the timestamps of the parent dir when cpup */
4882 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4883 +                   struct path *h_path)
4884 +{
4885 +       struct inode *h_inode;
4886 +
4887 +       dt->dt_dentry = dentry;
4888 +       dt->dt_h_path = *h_path;
4889 +       h_inode = d_inode(h_path->dentry);
4890 +       dt->dt_atime = h_inode->i_atime;
4891 +       dt->dt_mtime = h_inode->i_mtime;
4892 +       /* smp_mb(); */
4893 +}
4894 +
4895 +void au_dtime_revert(struct au_dtime *dt)
4896 +{
4897 +       struct iattr attr;
4898 +       int err;
4899 +
4900 +       attr.ia_atime = dt->dt_atime;
4901 +       attr.ia_mtime = dt->dt_mtime;
4902 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4903 +               | ATTR_ATIME | ATTR_ATIME_SET;
4904 +
4905 +       /* no delegation since this is a directory */
4906 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4907 +       if (unlikely(err))
4908 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4909 +}
4910 +
4911 +/* ---------------------------------------------------------------------- */
4912 +
4913 +/* internal use only */
4914 +struct au_cpup_reg_attr {
4915 +       int             valid;
4916 +       struct kstat    st;
4917 +       unsigned int    iflags; /* inode->i_flags */
4918 +};
4919 +
4920 +static noinline_for_stack
4921 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4922 +              struct au_cpup_reg_attr *h_src_attr)
4923 +{
4924 +       int err, sbits, icex;
4925 +       unsigned int mnt_flags;
4926 +       unsigned char verbose;
4927 +       struct iattr ia;
4928 +       struct path h_path;
4929 +       struct inode *h_isrc, *h_idst;
4930 +       struct kstat *h_st;
4931 +       struct au_branch *br;
4932 +
4933 +       br = au_sbr(dst->d_sb, bindex);
4934 +       h_path.mnt = au_br_mnt(br);
4935 +       h_path.dentry = au_h_dptr(dst, bindex);
4936 +       h_idst = d_inode(h_path.dentry);
4937 +       h_isrc = d_inode(h_src->dentry);
4938 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4939 +               | ATTR_ATIME | ATTR_MTIME
4940 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4941 +       if (h_src_attr && h_src_attr->valid) {
4942 +               h_st = &h_src_attr->st;
4943 +               ia.ia_uid = h_st->uid;
4944 +               ia.ia_gid = h_st->gid;
4945 +               ia.ia_atime = h_st->atime;
4946 +               ia.ia_mtime = h_st->mtime;
4947 +               if (h_idst->i_mode != h_st->mode
4948 +                   && !S_ISLNK(h_idst->i_mode)) {
4949 +                       ia.ia_valid |= ATTR_MODE;
4950 +                       ia.ia_mode = h_st->mode;
4951 +               }
4952 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4953 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4954 +       } else {
4955 +               ia.ia_uid = h_isrc->i_uid;
4956 +               ia.ia_gid = h_isrc->i_gid;
4957 +               ia.ia_atime = h_isrc->i_atime;
4958 +               ia.ia_mtime = h_isrc->i_mtime;
4959 +               if (h_idst->i_mode != h_isrc->i_mode
4960 +                   && !S_ISLNK(h_idst->i_mode)) {
4961 +                       ia.ia_valid |= ATTR_MODE;
4962 +                       ia.ia_mode = h_isrc->i_mode;
4963 +               }
4964 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4965 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4966 +       }
4967 +       /* no delegation since it is just created */
4968 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4969 +
4970 +       /* is this nfs only? */
4971 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4972 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4973 +               ia.ia_mode = h_isrc->i_mode;
4974 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4975 +       }
4976 +
4977 +       icex = br->br_perm & AuBrAttr_ICEX;
4978 +       if (!err) {
4979 +               mnt_flags = au_mntflags(dst->d_sb);
4980 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4981 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4982 +       }
4983 +
4984 +       return err;
4985 +}
4986 +
4987 +/* ---------------------------------------------------------------------- */
4988 +
4989 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4990 +                          char *buf, unsigned long blksize)
4991 +{
4992 +       int err;
4993 +       size_t sz, rbytes, wbytes;
4994 +       unsigned char all_zero;
4995 +       char *p, *zp;
4996 +       struct inode *h_inode;
4997 +       /* reduce stack usage */
4998 +       struct iattr *ia;
4999 +
5000 +       zp = page_address(ZERO_PAGE(0));
5001 +       if (unlikely(!zp))
5002 +               return -ENOMEM; /* possible? */
5003 +
5004 +       err = 0;
5005 +       all_zero = 0;
5006 +       while (len) {
5007 +               AuDbg("len %lld\n", len);
5008 +               sz = blksize;
5009 +               if (len < blksize)
5010 +                       sz = len;
5011 +
5012 +               rbytes = 0;
5013 +               /* todo: signal_pending? */
5014 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5015 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5016 +                       err = rbytes;
5017 +               }
5018 +               if (unlikely(err < 0))
5019 +                       break;
5020 +
5021 +               all_zero = 0;
5022 +               if (len >= rbytes && rbytes == blksize)
5023 +                       all_zero = !memcmp(buf, zp, rbytes);
5024 +               if (!all_zero) {
5025 +                       wbytes = rbytes;
5026 +                       p = buf;
5027 +                       while (wbytes) {
5028 +                               size_t b;
5029 +
5030 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5031 +                               err = b;
5032 +                               /* todo: signal_pending? */
5033 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5034 +                                       continue;
5035 +                               if (unlikely(err < 0))
5036 +                                       break;
5037 +                               wbytes -= b;
5038 +                               p += b;
5039 +                       }
5040 +                       if (unlikely(err < 0))
5041 +                               break;
5042 +               } else {
5043 +                       loff_t res;
5044 +
5045 +                       AuLabel(hole);
5046 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5047 +                       err = res;
5048 +                       if (unlikely(res < 0))
5049 +                               break;
5050 +               }
5051 +               len -= rbytes;
5052 +               err = 0;
5053 +       }
5054 +
5055 +       /* the last block may be a hole */
5056 +       if (!err && all_zero) {
5057 +               AuLabel(last hole);
5058 +
5059 +               err = 1;
5060 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5061 +                       /* nfs requires this step to make last hole */
5062 +                       /* is this only nfs? */
5063 +                       do {
5064 +                               /* todo: signal_pending? */
5065 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5066 +                       } while (err == -EAGAIN || err == -EINTR);
5067 +                       if (err == 1)
5068 +                               dst->f_pos--;
5069 +               }
5070 +
5071 +               if (err == 1) {
5072 +                       ia = (void *)buf;
5073 +                       ia->ia_size = dst->f_pos;
5074 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5075 +                       ia->ia_file = dst;
5076 +                       h_inode = file_inode(dst);
5077 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5078 +                       /* no delegation since it is just created */
5079 +                       err = vfsub_notify_change(&dst->f_path, ia,
5080 +                                                 /*delegated*/NULL);
5081 +                       inode_unlock(h_inode);
5082 +               }
5083 +       }
5084 +
5085 +       return err;
5086 +}
5087 +
5088 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5089 +{
5090 +       int err;
5091 +       unsigned long blksize;
5092 +       unsigned char do_kfree;
5093 +       char *buf;
5094 +       struct super_block *h_sb;
5095 +
5096 +       err = -ENOMEM;
5097 +       h_sb = file_inode(dst)->i_sb;
5098 +       blksize = h_sb->s_blocksize;
5099 +       if (!blksize || PAGE_SIZE < blksize)
5100 +               blksize = PAGE_SIZE;
5101 +       AuDbg("blksize %lu\n", blksize);
5102 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5103 +       if (do_kfree)
5104 +               buf = kmalloc(blksize, GFP_NOFS);
5105 +       else
5106 +               buf = (void *)__get_free_page(GFP_NOFS);
5107 +       if (unlikely(!buf))
5108 +               goto out;
5109 +
5110 +       if (len > (1 << 22))
5111 +               AuDbg("copying a large file %lld\n", (long long)len);
5112 +
5113 +       src->f_pos = 0;
5114 +       dst->f_pos = 0;
5115 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5116 +       if (do_kfree) {
5117 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5118 +               au_kfree_do_rcu(buf);
5119 +       } else
5120 +               free_page((unsigned long)buf);
5121 +
5122 +out:
5123 +       return err;
5124 +}
5125 +
5126 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5127 +{
5128 +       int err;
5129 +       struct super_block *h_src_sb;
5130 +       struct inode *h_src_inode;
5131 +
5132 +       h_src_inode = file_inode(src);
5133 +       h_src_sb = h_src_inode->i_sb;
5134 +
5135 +       /* XFS acquires inode_lock */
5136 +       if (!au_test_xfs(h_src_sb))
5137 +               err = au_copy_file(dst, src, len);
5138 +       else {
5139 +               inode_unlock_shared(h_src_inode);
5140 +               err = au_copy_file(dst, src, len);
5141 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5142 +       }
5143 +
5144 +       return err;
5145 +}
5146 +
5147 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5148 +{
5149 +       int err;
5150 +       loff_t lo;
5151 +       struct super_block *h_src_sb;
5152 +       struct inode *h_src_inode;
5153 +
5154 +       h_src_inode = file_inode(src);
5155 +       h_src_sb = h_src_inode->i_sb;
5156 +       if (h_src_sb != file_inode(dst)->i_sb
5157 +           || !dst->f_op->remap_file_range) {
5158 +               err = au_do_copy(dst, src, len);
5159 +               goto out;
5160 +       }
5161 +
5162 +       if (!au_test_nfs(h_src_sb)) {
5163 +               inode_unlock_shared(h_src_inode);
5164 +               lo = vfsub_clone_file_range(src, dst, len);
5165 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5166 +       } else
5167 +               lo = vfsub_clone_file_range(src, dst, len);
5168 +       if (lo == len) {
5169 +               err = 0;
5170 +               goto out; /* success */
5171 +       } else if (lo >= 0)
5172 +               /* todo: possible? */
5173 +               /* paritially succeeded */
5174 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5175 +       else if (lo != -EOPNOTSUPP) {
5176 +               /* older XFS has a condition in cloning */
5177 +               err = lo;
5178 +               goto out;
5179 +       }
5180 +
5181 +       /* the backend fs on NFS may not support cloning */
5182 +       err = au_do_copy(dst, src, len);
5183 +
5184 +out:
5185 +       AuTraceErr(err);
5186 +       return err;
5187 +}
5188 +
5189 +/*
5190 + * to support a sparse file which is opened with O_APPEND,
5191 + * we need to close the file.
5192 + */
5193 +static int au_cp_regular(struct au_cp_generic *cpg)
5194 +{
5195 +       int err, i;
5196 +       enum { SRC, DST };
5197 +       struct {
5198 +               aufs_bindex_t bindex;
5199 +               unsigned int flags;
5200 +               struct dentry *dentry;
5201 +               int force_wr;
5202 +               struct file *file;
5203 +       } *f, file[] = {
5204 +               {
5205 +                       .bindex = cpg->bsrc,
5206 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5207 +               },
5208 +               {
5209 +                       .bindex = cpg->bdst,
5210 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5211 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5212 +               }
5213 +       };
5214 +       struct au_branch *br;
5215 +       struct super_block *sb, *h_src_sb;
5216 +       struct inode *h_src_inode;
5217 +       struct task_struct *tsk = current;
5218 +
5219 +       /* bsrc branch can be ro/rw. */
5220 +       sb = cpg->dentry->d_sb;
5221 +       f = file;
5222 +       for (i = 0; i < 2; i++, f++) {
5223 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5224 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5225 +                                   /*file*/NULL, f->force_wr);
5226 +               if (IS_ERR(f->file)) {
5227 +                       err = PTR_ERR(f->file);
5228 +                       if (i == SRC)
5229 +                               goto out;
5230 +                       else
5231 +                               goto out_src;
5232 +               }
5233 +       }
5234 +
5235 +       /* try stopping to update while we copyup */
5236 +       h_src_inode = d_inode(file[SRC].dentry);
5237 +       h_src_sb = h_src_inode->i_sb;
5238 +       if (!au_test_nfs(h_src_sb))
5239 +               IMustLock(h_src_inode);
5240 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5241 +
5242 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5243 +       if (tsk->flags & PF_KTHREAD)
5244 +               __fput_sync(file[DST].file);
5245 +       else {
5246 +               /* it happened actually */
5247 +               fput(file[DST].file);
5248 +               /*
5249 +                * too bad.
5250 +                * we have to call both since we don't know which place the file
5251 +                * was added to.
5252 +                */
5253 +               task_work_run();
5254 +               flush_delayed_fput();
5255 +       }
5256 +       br = au_sbr(sb, file[DST].bindex);
5257 +       au_lcnt_dec(&br->br_nfiles);
5258 +
5259 +out_src:
5260 +       fput(file[SRC].file);
5261 +       br = au_sbr(sb, file[SRC].bindex);
5262 +       au_lcnt_dec(&br->br_nfiles);
5263 +out:
5264 +       return err;
5265 +}
5266 +
5267 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5268 +                             struct au_cpup_reg_attr *h_src_attr)
5269 +{
5270 +       int err, rerr;
5271 +       loff_t l;
5272 +       struct path h_path;
5273 +       struct inode *h_src_inode, *h_dst_inode;
5274 +
5275 +       err = 0;
5276 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5277 +       l = i_size_read(h_src_inode);
5278 +       if (cpg->len == -1 || l < cpg->len)
5279 +               cpg->len = l;
5280 +       if (cpg->len) {
5281 +               /* try stopping to update while we are referencing */
5282 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5283 +               au_pin_hdir_unlock(cpg->pin);
5284 +
5285 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5286 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5287 +               h_src_attr->iflags = h_src_inode->i_flags;
5288 +               if (!au_test_nfs(h_src_inode->i_sb))
5289 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5290 +               else {
5291 +                       inode_unlock_shared(h_src_inode);
5292 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5293 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5294 +               }
5295 +               if (unlikely(err)) {
5296 +                       inode_unlock_shared(h_src_inode);
5297 +                       goto out;
5298 +               }
5299 +               h_src_attr->valid = 1;
5300 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5301 +                       err = au_cp_regular(cpg);
5302 +                       inode_unlock_shared(h_src_inode);
5303 +               } else {
5304 +                       inode_unlock_shared(h_src_inode);
5305 +                       err = au_cp_regular(cpg);
5306 +               }
5307 +               rerr = au_pin_hdir_relock(cpg->pin);
5308 +               if (!err && rerr)
5309 +                       err = rerr;
5310 +       }
5311 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5312 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5313 +               h_dst_inode = d_inode(h_path.dentry);
5314 +               spin_lock(&h_dst_inode->i_lock);
5315 +               h_dst_inode->i_state |= I_LINKABLE;
5316 +               spin_unlock(&h_dst_inode->i_lock);
5317 +       }
5318 +
5319 +out:
5320 +       return err;
5321 +}
5322 +
5323 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5324 +                             struct inode *h_dir)
5325 +{
5326 +       int err;
5327 +       DEFINE_DELAYED_CALL(done);
5328 +       const char *sym;
5329 +
5330 +       sym = vfs_get_link(h_src, &done);
5331 +       err = PTR_ERR(sym);
5332 +       if (IS_ERR(sym))
5333 +               goto out;
5334 +
5335 +       err = vfsub_symlink(h_dir, h_path, sym);
5336 +
5337 +out:
5338 +       do_delayed_call(&done);
5339 +       return err;
5340 +}
5341 +
5342 +/*
5343 + * regardless 'acl' option, reset all ACL.
5344 + * All ACL will be copied up later from the original entry on the lower branch.
5345 + */
5346 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5347 +{
5348 +       int err;
5349 +       struct dentry *h_dentry;
5350 +       struct inode *h_inode;
5351 +       struct user_namespace *h_userns;
5352 +
5353 +       h_userns = mnt_user_ns(h_path->mnt);
5354 +       h_dentry = h_path->dentry;
5355 +       h_inode = d_inode(h_dentry);
5356 +       /* forget_all_cached_acls(h_inode)); */
5357 +       err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5358 +       AuTraceErr(err);
5359 +       if (err == -EOPNOTSUPP)
5360 +               err = 0;
5361 +       if (!err)
5362 +               err = vfsub_acl_chmod(h_userns, h_inode, mode);
5363 +
5364 +       AuTraceErr(err);
5365 +       return err;
5366 +}
5367 +
5368 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5369 +                         struct inode *h_dir, struct path *h_path)
5370 +{
5371 +       int err;
5372 +       struct inode *dir, *inode;
5373 +       struct user_namespace *h_userns;
5374 +
5375 +       h_userns = mnt_user_ns(h_path->mnt);
5376 +       err = vfsub_removexattr(h_userns, h_path->dentry,
5377 +                               XATTR_NAME_POSIX_ACL_DEFAULT);
5378 +       AuTraceErr(err);
5379 +       if (err == -EOPNOTSUPP)
5380 +               err = 0;
5381 +       if (unlikely(err))
5382 +               goto out;
5383 +
5384 +       /*
5385 +        * strange behaviour from the users view,
5386 +        * particularly setattr case
5387 +        */
5388 +       dir = d_inode(dst_parent);
5389 +       if (au_ibtop(dir) == cpg->bdst)
5390 +               au_cpup_attr_nlink(dir, /*force*/1);
5391 +       inode = d_inode(cpg->dentry);
5392 +       au_cpup_attr_nlink(inode, /*force*/1);
5393 +
5394 +out:
5395 +       return err;
5396 +}
5397 +
5398 +static noinline_for_stack
5399 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5400 +              struct au_cpup_reg_attr *h_src_attr)
5401 +{
5402 +       int err;
5403 +       umode_t mode;
5404 +       unsigned int mnt_flags;
5405 +       unsigned char isdir, isreg, force;
5406 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5407 +       struct au_dtime dt;
5408 +       struct path h_path;
5409 +       struct dentry *h_src, *h_dst, *h_parent;
5410 +       struct inode *h_inode, *h_dir;
5411 +       struct super_block *sb;
5412 +
5413 +       /* bsrc branch can be ro/rw. */
5414 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5415 +       h_inode = d_inode(h_src);
5416 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5417 +
5418 +       /* try stopping to be referenced while we are creating */
5419 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5420 +       if (au_ftest_cpup(cpg->flags, RENAME))
5421 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5422 +                                 AUFS_WH_PFX_LEN));
5423 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5424 +       h_dir = d_inode(h_parent);
5425 +       IMustLock(h_dir);
5426 +       AuDebugOn(h_parent != h_dst->d_parent);
5427 +
5428 +       sb = cpg->dentry->d_sb;
5429 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5430 +       if (do_dt) {
5431 +               h_path.dentry = h_parent;
5432 +               au_dtime_store(&dt, dst_parent, &h_path);
5433 +       }
5434 +       h_path.dentry = h_dst;
5435 +
5436 +       isreg = 0;
5437 +       isdir = 0;
5438 +       mode = h_inode->i_mode;
5439 +       switch (mode & S_IFMT) {
5440 +       case S_IFREG:
5441 +               isreg = 1;
5442 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5443 +               if (!err)
5444 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5445 +               break;
5446 +       case S_IFDIR:
5447 +               isdir = 1;
5448 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5449 +               if (!err)
5450 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5451 +               break;
5452 +       case S_IFLNK:
5453 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5454 +               break;
5455 +       case S_IFCHR:
5456 +       case S_IFBLK:
5457 +               AuDebugOn(!capable(CAP_MKNOD));
5458 +               fallthrough;
5459 +       case S_IFIFO:
5460 +       case S_IFSOCK:
5461 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5462 +               break;
5463 +       default:
5464 +               AuIOErr("Unknown inode type 0%o\n", mode);
5465 +               err = -EIO;
5466 +       }
5467 +       if (!err)
5468 +               err = au_reset_acl(h_dir, &h_path, mode);
5469 +
5470 +       mnt_flags = au_mntflags(sb);
5471 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5472 +           && !isdir
5473 +           && au_opt_test(mnt_flags, XINO)
5474 +           && (h_inode->i_nlink == 1
5475 +               || (h_inode->i_state & I_LINKABLE))
5476 +           /* todo: unnecessary? */
5477 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5478 +           && cpg->bdst < cpg->bsrc
5479 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5480 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5481 +               /* ignore this error */
5482 +
5483 +       if (!err) {
5484 +               force = 0;
5485 +               if (isreg) {
5486 +                       force = !!cpg->len;
5487 +                       if (cpg->len == -1)
5488 +                               force = !!i_size_read(h_inode);
5489 +               }
5490 +               au_fhsm_wrote(sb, cpg->bdst, force);
5491 +       }
5492 +
5493 +       if (do_dt)
5494 +               au_dtime_revert(&dt);
5495 +       return err;
5496 +}
5497 +
5498 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5499 +{
5500 +       int err;
5501 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5502 +       struct path h_ppath;
5503 +       struct inode *h_dir;
5504 +       aufs_bindex_t bdst;
5505 +
5506 +       dentry = cpg->dentry;
5507 +       bdst = cpg->bdst;
5508 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5509 +       h_dentry = au_h_dptr(dentry, bdst);
5510 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5511 +               dget(h_dentry);
5512 +               au_set_h_dptr(dentry, bdst, NULL);
5513 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5514 +               if (!err)
5515 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5516 +               au_set_h_dptr(dentry, bdst, h_dentry);
5517 +       } else {
5518 +               err = 0;
5519 +               parent = dget_parent(dentry);
5520 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5521 +               dput(parent);
5522 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5523 +               if (IS_ERR(h_path->dentry))
5524 +                       err = PTR_ERR(h_path->dentry);
5525 +       }
5526 +       if (unlikely(err))
5527 +               goto out;
5528 +
5529 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5530 +       h_dir = d_inode(h_parent);
5531 +       IMustLock(h_dir);
5532 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5533 +       /* no delegation since it is just created */
5534 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5535 +                          /*flags*/0);
5536 +       dput(h_path->dentry);
5537 +
5538 +out:
5539 +       return err;
5540 +}
5541 +
5542 +/*
5543 + * copyup the @dentry from @bsrc to @bdst.
5544 + * the caller must set the both of lower dentries.
5545 + * @len is for truncating when it is -1 copyup the entire file.
5546 + * in link/rename cases, @dst_parent may be different from the real one.
5547 + * basic->bsrc can be larger than basic->bdst.
5548 + * aufs doesn't touch the credential so
5549 + * security_inode_copy_up{,_xattr}() are unnecessary.
5550 + */
5551 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5552 +{
5553 +       int err, rerr;
5554 +       aufs_bindex_t old_ibtop;
5555 +       unsigned char isdir, plink;
5556 +       struct dentry *h_src, *h_dst, *h_parent;
5557 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5558 +       struct super_block *sb;
5559 +       struct au_branch *br;
5560 +       struct path h_src_path;
5561 +       /* to reduce stack size */
5562 +       struct {
5563 +               struct au_dtime dt;
5564 +               struct path h_path;
5565 +               struct au_cpup_reg_attr h_src_attr;
5566 +       } *a;
5567 +
5568 +       err = -ENOMEM;
5569 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5570 +       if (unlikely(!a))
5571 +               goto out;
5572 +       a->h_src_attr.valid = 0;
5573 +
5574 +       sb = cpg->dentry->d_sb;
5575 +       br = au_sbr(sb, cpg->bdst);
5576 +       a->h_path.mnt = au_br_mnt(br);
5577 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5578 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5579 +       h_dir = d_inode(h_parent);
5580 +       IMustLock(h_dir);
5581 +
5582 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5583 +       inode = d_inode(cpg->dentry);
5584 +
5585 +       if (!dst_parent)
5586 +               dst_parent = dget_parent(cpg->dentry);
5587 +       else
5588 +               dget(dst_parent);
5589 +
5590 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5591 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5592 +       if (dst_inode) {
5593 +               if (unlikely(!plink)) {
5594 +                       err = -EIO;
5595 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5596 +                               "but plink is disabled\n",
5597 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5598 +                       goto out_parent;
5599 +               }
5600 +
5601 +               if (dst_inode->i_nlink) {
5602 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5603 +
5604 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5605 +                       err = PTR_ERR(h_src);
5606 +                       if (IS_ERR(h_src))
5607 +                               goto out_parent;
5608 +                       if (unlikely(d_is_negative(h_src))) {
5609 +                               err = -EIO;
5610 +                               AuIOErr("i%lu exists on b%d "
5611 +                                       "but not pseudo-linked\n",
5612 +                                       inode->i_ino, cpg->bdst);
5613 +                               dput(h_src);
5614 +                               goto out_parent;
5615 +                       }
5616 +
5617 +                       if (do_dt) {
5618 +                               a->h_path.dentry = h_parent;
5619 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5620 +                       }
5621 +
5622 +                       a->h_path.dentry = h_dst;
5623 +                       delegated = NULL;
5624 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5625 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5626 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5627 +                       if (do_dt)
5628 +                               au_dtime_revert(&a->dt);
5629 +                       if (unlikely(err == -EWOULDBLOCK)) {
5630 +                               pr_warn("cannot retry for NFSv4 delegation"
5631 +                                       " for an internal link\n");
5632 +                               iput(delegated);
5633 +                       }
5634 +                       dput(h_src);
5635 +                       goto out_parent;
5636 +               } else
5637 +                       /* todo: cpup_wh_file? */
5638 +                       /* udba work */
5639 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5640 +       }
5641 +
5642 +       isdir = S_ISDIR(inode->i_mode);
5643 +       old_ibtop = au_ibtop(inode);
5644 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5645 +       if (unlikely(err))
5646 +               goto out_rev;
5647 +       dst_inode = d_inode(h_dst);
5648 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5649 +       /* todo: necessary? */
5650 +       /* au_pin_hdir_unlock(cpg->pin); */
5651 +
5652 +       h_src_path.dentry = h_src;
5653 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5654 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5655 +       if (unlikely(err)) {
5656 +               /* todo: necessary? */
5657 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5658 +               inode_unlock(dst_inode);
5659 +               goto out_rev;
5660 +       }
5661 +
5662 +       if (cpg->bdst < old_ibtop) {
5663 +               if (S_ISREG(inode->i_mode)) {
5664 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5665 +                       if (unlikely(err)) {
5666 +                               /* ignore an error */
5667 +                               /* au_pin_hdir_relock(cpg->pin); */
5668 +                               inode_unlock(dst_inode);
5669 +                               goto out_rev;
5670 +                       }
5671 +               }
5672 +               au_set_ibtop(inode, cpg->bdst);
5673 +       } else
5674 +               au_set_ibbot(inode, cpg->bdst);
5675 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5676 +                     au_hi_flags(inode, isdir));
5677 +
5678 +       /* todo: necessary? */
5679 +       /* err = au_pin_hdir_relock(cpg->pin); */
5680 +       inode_unlock(dst_inode);
5681 +       if (unlikely(err))
5682 +               goto out_rev;
5683 +
5684 +       src_inode = d_inode(h_src);
5685 +       if (!isdir
5686 +           && (src_inode->i_nlink > 1
5687 +               || src_inode->i_state & I_LINKABLE)
5688 +           && plink)
5689 +               au_plink_append(inode, cpg->bdst, h_dst);
5690 +
5691 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5692 +               a->h_path.dentry = h_dst;
5693 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5694 +       }
5695 +       if (!err)
5696 +               goto out_parent; /* success */
5697 +
5698 +       /* revert */
5699 +out_rev:
5700 +       a->h_path.dentry = h_parent;
5701 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5702 +       a->h_path.dentry = h_dst;
5703 +       rerr = 0;
5704 +       if (d_is_positive(h_dst)) {
5705 +               if (!isdir) {
5706 +                       /* no delegation since it is just created */
5707 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5708 +                                           /*delegated*/NULL, /*force*/0);
5709 +               } else
5710 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5711 +       }
5712 +       au_dtime_revert(&a->dt);
5713 +       if (rerr) {
5714 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5715 +               err = -EIO;
5716 +       }
5717 +out_parent:
5718 +       dput(dst_parent);
5719 +       au_kfree_rcu(a);
5720 +out:
5721 +       return err;
5722 +}
5723 +
5724 +#if 0 /* reserved */
5725 +struct au_cpup_single_args {
5726 +       int *errp;
5727 +       struct au_cp_generic *cpg;
5728 +       struct dentry *dst_parent;
5729 +};
5730 +
5731 +static void au_call_cpup_single(void *args)
5732 +{
5733 +       struct au_cpup_single_args *a = args;
5734 +
5735 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5736 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5737 +       au_pin_hdir_release(a->cpg->pin);
5738 +}
5739 +#endif
5740 +
5741 +/*
5742 + * prevent SIGXFSZ in copy-up.
5743 + * testing CAP_MKNOD is for generic fs,
5744 + * but CAP_FSETID is for xfs only, currently.
5745 + */
5746 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5747 +{
5748 +       int do_sio;
5749 +       struct super_block *sb;
5750 +       struct inode *h_dir;
5751 +
5752 +       do_sio = 0;
5753 +       sb = au_pinned_parent(pin)->d_sb;
5754 +       if (!au_wkq_test()
5755 +           && (!au_sbi(sb)->si_plink_maint_pid
5756 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5757 +               switch (mode & S_IFMT) {
5758 +               case S_IFREG:
5759 +                       /* no condition about RLIMIT_FSIZE and the file size */
5760 +                       do_sio = 1;
5761 +                       break;
5762 +               case S_IFCHR:
5763 +               case S_IFBLK:
5764 +                       do_sio = !capable(CAP_MKNOD);
5765 +                       break;
5766 +               }
5767 +               if (!do_sio)
5768 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5769 +                                 && !capable(CAP_FSETID));
5770 +               /* this workaround may be removed in the future */
5771 +               if (!do_sio) {
5772 +                       h_dir = au_pinned_h_dir(pin);
5773 +                       do_sio = h_dir->i_mode & S_ISVTX;
5774 +               }
5775 +       }
5776 +
5777 +       return do_sio;
5778 +}
5779 +
5780 +#if 0 /* reserved */
5781 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5782 +{
5783 +       int err, wkq_err;
5784 +       struct dentry *h_dentry;
5785 +
5786 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5787 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5788 +               err = au_cpup_single(cpg, dst_parent);
5789 +       else {
5790 +               struct au_cpup_single_args args = {
5791 +                       .errp           = &err,
5792 +                       .cpg            = cpg,
5793 +                       .dst_parent     = dst_parent
5794 +               };
5795 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5796 +               if (unlikely(wkq_err))
5797 +                       err = wkq_err;
5798 +       }
5799 +
5800 +       return err;
5801 +}
5802 +#endif
5803 +
5804 +/*
5805 + * copyup the @dentry from the first active lower branch to @bdst,
5806 + * using au_cpup_single().
5807 + */
5808 +static int au_cpup_simple(struct au_cp_generic *cpg)
5809 +{
5810 +       int err;
5811 +       unsigned int flags_orig;
5812 +       struct dentry *dentry;
5813 +
5814 +       AuDebugOn(cpg->bsrc < 0);
5815 +
5816 +       dentry = cpg->dentry;
5817 +       DiMustWriteLock(dentry);
5818 +
5819 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5820 +       if (!err) {
5821 +               flags_orig = cpg->flags;
5822 +               au_fset_cpup(cpg->flags, RENAME);
5823 +               err = au_cpup_single(cpg, NULL);
5824 +               cpg->flags = flags_orig;
5825 +               if (!err)
5826 +                       return 0; /* success */
5827 +
5828 +               /* revert */
5829 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5830 +               au_set_dbtop(dentry, cpg->bsrc);
5831 +       }
5832 +
5833 +       return err;
5834 +}
5835 +
5836 +struct au_cpup_simple_args {
5837 +       int *errp;
5838 +       struct au_cp_generic *cpg;
5839 +};
5840 +
5841 +static void au_call_cpup_simple(void *args)
5842 +{
5843 +       struct au_cpup_simple_args *a = args;
5844 +
5845 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5846 +       *a->errp = au_cpup_simple(a->cpg);
5847 +       au_pin_hdir_release(a->cpg->pin);
5848 +}
5849 +
5850 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5851 +{
5852 +       int err, wkq_err;
5853 +       struct dentry *dentry, *parent;
5854 +       struct file *h_file;
5855 +       struct inode *h_dir;
5856 +       struct user_namespace *h_userns;
5857 +
5858 +       dentry = cpg->dentry;
5859 +       h_file = NULL;
5860 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5861 +               AuDebugOn(cpg->bsrc < 0);
5862 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5863 +               err = PTR_ERR(h_file);
5864 +               if (IS_ERR(h_file))
5865 +                       goto out;
5866 +       }
5867 +
5868 +       parent = dget_parent(dentry);
5869 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5870 +       h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5871 +       if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5872 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5873 +               err = au_cpup_simple(cpg);
5874 +       else {
5875 +               struct au_cpup_simple_args args = {
5876 +                       .errp           = &err,
5877 +                       .cpg            = cpg
5878 +               };
5879 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5880 +               if (unlikely(wkq_err))
5881 +                       err = wkq_err;
5882 +       }
5883 +
5884 +       dput(parent);
5885 +       if (h_file)
5886 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5887 +
5888 +out:
5889 +       return err;
5890 +}
5891 +
5892 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5893 +{
5894 +       aufs_bindex_t bsrc, bbot;
5895 +       struct dentry *dentry, *h_dentry;
5896 +
5897 +       if (cpg->bsrc < 0) {
5898 +               dentry = cpg->dentry;
5899 +               bbot = au_dbbot(dentry);
5900 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5901 +                       h_dentry = au_h_dptr(dentry, bsrc);
5902 +                       if (h_dentry) {
5903 +                               AuDebugOn(d_is_negative(h_dentry));
5904 +                               break;
5905 +                       }
5906 +               }
5907 +               AuDebugOn(bsrc > bbot);
5908 +               cpg->bsrc = bsrc;
5909 +       }
5910 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5911 +       return au_do_sio_cpup_simple(cpg);
5912 +}
5913 +
5914 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5915 +{
5916 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5917 +       return au_do_sio_cpup_simple(cpg);
5918 +}
5919 +
5920 +/* ---------------------------------------------------------------------- */
5921 +
5922 +/*
5923 + * copyup the deleted file for writing.
5924 + */
5925 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5926 +                        struct file *file)
5927 +{
5928 +       int err;
5929 +       unsigned int flags_orig;
5930 +       aufs_bindex_t bsrc_orig;
5931 +       struct au_dinfo *dinfo;
5932 +       struct {
5933 +               struct au_hdentry *hd;
5934 +               struct dentry *h_dentry;
5935 +       } hdst, hsrc;
5936 +
5937 +       dinfo = au_di(cpg->dentry);
5938 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5939 +
5940 +       bsrc_orig = cpg->bsrc;
5941 +       cpg->bsrc = dinfo->di_btop;
5942 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5943 +       hdst.h_dentry = hdst.hd->hd_dentry;
5944 +       hdst.hd->hd_dentry = wh_dentry;
5945 +       dinfo->di_btop = cpg->bdst;
5946 +
5947 +       hsrc.h_dentry = NULL;
5948 +       if (file) {
5949 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5950 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5951 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5952 +       }
5953 +       flags_orig = cpg->flags;
5954 +       cpg->flags = !AuCpup_DTIME;
5955 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5956 +       cpg->flags = flags_orig;
5957 +       if (file) {
5958 +               if (!err)
5959 +                       err = au_reopen_nondir(file);
5960 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5961 +       }
5962 +       hdst.hd->hd_dentry = hdst.h_dentry;
5963 +       dinfo->di_btop = cpg->bsrc;
5964 +       cpg->bsrc = bsrc_orig;
5965 +
5966 +       return err;
5967 +}
5968 +
5969 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5970 +{
5971 +       int err;
5972 +       aufs_bindex_t bdst;
5973 +       struct au_dtime dt;
5974 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5975 +       struct au_branch *br;
5976 +       struct path h_path;
5977 +
5978 +       dentry = cpg->dentry;
5979 +       bdst = cpg->bdst;
5980 +       br = au_sbr(dentry->d_sb, bdst);
5981 +       parent = dget_parent(dentry);
5982 +       h_parent = au_h_dptr(parent, bdst);
5983 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5984 +       err = PTR_ERR(wh_dentry);
5985 +       if (IS_ERR(wh_dentry))
5986 +               goto out;
5987 +
5988 +       h_path.dentry = h_parent;
5989 +       h_path.mnt = au_br_mnt(br);
5990 +       au_dtime_store(&dt, parent, &h_path);
5991 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5992 +       if (unlikely(err))
5993 +               goto out_wh;
5994 +
5995 +       dget(wh_dentry);
5996 +       h_path.dentry = wh_dentry;
5997 +       if (!d_is_dir(wh_dentry)) {
5998 +               /* no delegation since it is just created */
5999 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6000 +                                  /*delegated*/NULL, /*force*/0);
6001 +       } else
6002 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6003 +       if (unlikely(err)) {
6004 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6005 +                       wh_dentry, err);
6006 +               err = -EIO;
6007 +       }
6008 +       au_dtime_revert(&dt);
6009 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6010 +
6011 +out_wh:
6012 +       dput(wh_dentry);
6013 +out:
6014 +       dput(parent);
6015 +       return err;
6016 +}
6017 +
6018 +struct au_cpup_wh_args {
6019 +       int *errp;
6020 +       struct au_cp_generic *cpg;
6021 +       struct file *file;
6022 +};
6023 +
6024 +static void au_call_cpup_wh(void *args)
6025 +{
6026 +       struct au_cpup_wh_args *a = args;
6027 +
6028 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6029 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6030 +       au_pin_hdir_release(a->cpg->pin);
6031 +}
6032 +
6033 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6034 +{
6035 +       int err, wkq_err;
6036 +       aufs_bindex_t bdst;
6037 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6038 +       struct inode *dir, *h_dir, *h_tmpdir;
6039 +       struct au_wbr *wbr;
6040 +       struct au_pin wh_pin, *pin_orig;
6041 +       struct user_namespace *h_userns;
6042 +
6043 +       dentry = cpg->dentry;
6044 +       bdst = cpg->bdst;
6045 +       parent = dget_parent(dentry);
6046 +       dir = d_inode(parent);
6047 +       h_orph = NULL;
6048 +       h_parent = NULL;
6049 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6050 +       h_tmpdir = h_dir;
6051 +       pin_orig = NULL;
6052 +       if (!h_dir->i_nlink) {
6053 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6054 +               h_orph = wbr->wbr_orph;
6055 +
6056 +               h_parent = dget(au_h_dptr(parent, bdst));
6057 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6058 +               h_tmpdir = d_inode(h_orph);
6059 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6060 +
6061 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6062 +               /* todo: au_h_open_pre()? */
6063 +
6064 +               pin_orig = cpg->pin;
6065 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6066 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6067 +               cpg->pin = &wh_pin;
6068 +       }
6069 +
6070 +       h_userns = au_sbr_userns(dentry->d_sb, bdst);
6071 +       if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
6072 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6073 +               err = au_cpup_wh(cpg, file);
6074 +       else {
6075 +               struct au_cpup_wh_args args = {
6076 +                       .errp   = &err,
6077 +                       .cpg    = cpg,
6078 +                       .file   = file
6079 +               };
6080 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6081 +               if (unlikely(wkq_err))
6082 +                       err = wkq_err;
6083 +       }
6084 +
6085 +       if (h_orph) {
6086 +               inode_unlock(h_tmpdir);
6087 +               /* todo: au_h_open_post()? */
6088 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6089 +               au_set_h_dptr(parent, bdst, h_parent);
6090 +               AuDebugOn(!pin_orig);
6091 +               cpg->pin = pin_orig;
6092 +       }
6093 +       iput(h_dir);
6094 +       dput(parent);
6095 +
6096 +       return err;
6097 +}
6098 +
6099 +/* ---------------------------------------------------------------------- */
6100 +
6101 +/*
6102 + * generic routine for both of copy-up and copy-down.
6103 + */
6104 +/* cf. revalidate function in file.c */
6105 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6106 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6107 +                        struct au_pin *pin,
6108 +                        struct dentry *h_parent, void *arg),
6109 +              void *arg)
6110 +{
6111 +       int err;
6112 +       struct au_pin pin;
6113 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6114 +
6115 +       err = 0;
6116 +       parent = dget_parent(dentry);
6117 +       if (IS_ROOT(parent))
6118 +               goto out;
6119 +
6120 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6121 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6122 +
6123 +       /* do not use au_dpage */
6124 +       real_parent = parent;
6125 +       while (1) {
6126 +               dput(parent);
6127 +               parent = dget_parent(dentry);
6128 +               h_parent = au_h_dptr(parent, bdst);
6129 +               if (h_parent)
6130 +                       goto out; /* success */
6131 +
6132 +               /* find top dir which is necessary to cpup */
6133 +               do {
6134 +                       d = parent;
6135 +                       dput(parent);
6136 +                       parent = dget_parent(d);
6137 +                       di_read_lock_parent3(parent, !AuLock_IR);
6138 +                       h_parent = au_h_dptr(parent, bdst);
6139 +                       di_read_unlock(parent, !AuLock_IR);
6140 +               } while (!h_parent);
6141 +
6142 +               if (d != real_parent)
6143 +                       di_write_lock_child3(d);
6144 +
6145 +               /* somebody else might create while we were sleeping */
6146 +               h_dentry = au_h_dptr(d, bdst);
6147 +               if (!h_dentry || d_is_negative(h_dentry)) {
6148 +                       if (h_dentry)
6149 +                               au_update_dbtop(d);
6150 +
6151 +                       au_pin_set_dentry(&pin, d);
6152 +                       err = au_do_pin(&pin);
6153 +                       if (!err) {
6154 +                               err = cp(d, bdst, &pin, h_parent, arg);
6155 +                               au_unpin(&pin);
6156 +                       }
6157 +               }
6158 +
6159 +               if (d != real_parent)
6160 +                       di_write_unlock(d);
6161 +               if (unlikely(err))
6162 +                       break;
6163 +       }
6164 +
6165 +out:
6166 +       dput(parent);
6167 +       return err;
6168 +}
6169 +
6170 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6171 +                      struct au_pin *pin,
6172 +                      struct dentry *h_parent __maybe_unused,
6173 +                      void *arg __maybe_unused)
6174 +{
6175 +       struct au_cp_generic cpg = {
6176 +               .dentry = dentry,
6177 +               .bdst   = bdst,
6178 +               .bsrc   = -1,
6179 +               .len    = 0,
6180 +               .pin    = pin,
6181 +               .flags  = AuCpup_DTIME
6182 +       };
6183 +       return au_sio_cpup_simple(&cpg);
6184 +}
6185 +
6186 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6187 +{
6188 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6189 +}
6190 +
6191 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6192 +{
6193 +       int err;
6194 +       struct dentry *parent;
6195 +       struct inode *dir;
6196 +
6197 +       parent = dget_parent(dentry);
6198 +       dir = d_inode(parent);
6199 +       err = 0;
6200 +       if (au_h_iptr(dir, bdst))
6201 +               goto out;
6202 +
6203 +       di_read_unlock(parent, AuLock_IR);
6204 +       di_write_lock_parent(parent);
6205 +       /* someone else might change our inode while we were sleeping */
6206 +       if (!au_h_iptr(dir, bdst))
6207 +               err = au_cpup_dirs(dentry, bdst);
6208 +       di_downgrade_lock(parent, AuLock_IR);
6209 +
6210 +out:
6211 +       dput(parent);
6212 +       return err;
6213 +}
6214 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6215 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6216 +++ linux/fs/aufs/cpup.h        2022-08-02 12:12:45.162308621 +0200
6217 @@ -0,0 +1,100 @@
6218 +/* SPDX-License-Identifier: GPL-2.0 */
6219 +/*
6220 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6221 + *
6222 + * This program, aufs is free software; you can redistribute it and/or modify
6223 + * it under the terms of the GNU General Public License as published by
6224 + * the Free Software Foundation; either version 2 of the License, or
6225 + * (at your option) any later version.
6226 + *
6227 + * This program is distributed in the hope that it will be useful,
6228 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6229 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6230 + * GNU General Public License for more details.
6231 + *
6232 + * You should have received a copy of the GNU General Public License
6233 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6234 + */
6235 +
6236 +/*
6237 + * copy-up/down functions
6238 + */
6239 +
6240 +#ifndef __AUFS_CPUP_H__
6241 +#define __AUFS_CPUP_H__
6242 +
6243 +#ifdef __KERNEL__
6244 +
6245 +#include <linux/path.h>
6246 +
6247 +struct inode;
6248 +struct file;
6249 +struct au_pin;
6250 +
6251 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6252 +void au_cpup_attr_timesizes(struct inode *inode);
6253 +void au_cpup_attr_nlink(struct inode *inode, int force);
6254 +void au_cpup_attr_changeable(struct inode *inode);
6255 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6256 +void au_cpup_attr_all(struct inode *inode, int force);
6257 +
6258 +/* ---------------------------------------------------------------------- */
6259 +
6260 +struct au_cp_generic {
6261 +       struct dentry   *dentry;
6262 +       aufs_bindex_t   bdst, bsrc;
6263 +       loff_t          len;
6264 +       struct au_pin   *pin;
6265 +       unsigned int    flags;
6266 +};
6267 +
6268 +/* cpup flags */
6269 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6270 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6271 +                                                  for link(2) */
6272 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6273 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6274 +                                                  cpup */
6275 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6276 +                                                  existing entry */
6277 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6278 +                                                  the branch is marked as RO */
6279 +
6280 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6281 +#undef AuCpup_HOPEN
6282 +#define AuCpup_HOPEN           0
6283 +#endif
6284 +
6285 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6286 +#define au_fset_cpup(flags, name) \
6287 +       do { (flags) |= AuCpup_##name; } while (0)
6288 +#define au_fclr_cpup(flags, name) \
6289 +       do { (flags) &= ~AuCpup_##name; } while (0)
6290 +
6291 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6292 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6293 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6294 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6295 +
6296 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6297 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6298 +                        struct au_pin *pin,
6299 +                        struct dentry *h_parent, void *arg),
6300 +              void *arg);
6301 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6302 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6303 +
6304 +/* ---------------------------------------------------------------------- */
6305 +
6306 +/* keep timestamps when copyup */
6307 +struct au_dtime {
6308 +       struct dentry *dt_dentry;
6309 +       struct path dt_h_path;
6310 +       struct timespec64 dt_atime, dt_mtime;
6311 +};
6312 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6313 +                   struct path *h_path);
6314 +void au_dtime_revert(struct au_dtime *dt);
6315 +
6316 +#endif /* __KERNEL__ */
6317 +#endif /* __AUFS_CPUP_H__ */
6318 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6319 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6320 +++ linux/fs/aufs/dbgaufs.c     2022-08-02 12:12:45.162308621 +0200
6321 @@ -0,0 +1,526 @@
6322 +// SPDX-License-Identifier: GPL-2.0
6323 +/*
6324 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6325 + *
6326 + * This program, aufs is free software; you can redistribute it and/or modify
6327 + * it under the terms of the GNU General Public License as published by
6328 + * the Free Software Foundation; either version 2 of the License, or
6329 + * (at your option) any later version.
6330 + *
6331 + * This program is distributed in the hope that it will be useful,
6332 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6333 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6334 + * GNU General Public License for more details.
6335 + *
6336 + * You should have received a copy of the GNU General Public License
6337 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6338 + */
6339 +
6340 +/*
6341 + * debugfs interface
6342 + */
6343 +
6344 +#include <linux/debugfs.h>
6345 +#include "aufs.h"
6346 +
6347 +#ifndef CONFIG_SYSFS
6348 +#error DEBUG_FS depends upon SYSFS
6349 +#endif
6350 +
6351 +static struct dentry *dbgaufs;
6352 +static const mode_t dbgaufs_mode = 0444;
6353 +
6354 +/* 20 is max digits length of ulong 64 */
6355 +struct dbgaufs_arg {
6356 +       int n;
6357 +       char a[20 * 4];
6358 +};
6359 +
6360 +/*
6361 + * common function for all XINO files
6362 + */
6363 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6364 +                             struct file *file)
6365 +{
6366 +       void *p;
6367 +
6368 +       p = file->private_data;
6369 +       if (p) {
6370 +               /* this is struct dbgaufs_arg */
6371 +               AuDebugOn(!au_kfree_sz_test(p));
6372 +               au_kfree_do_rcu(p);
6373 +       }
6374 +       return 0;
6375 +}
6376 +
6377 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6378 +                          int cnt)
6379 +{
6380 +       int err;
6381 +       struct kstat st;
6382 +       struct dbgaufs_arg *p;
6383 +
6384 +       err = -ENOMEM;
6385 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6386 +       if (unlikely(!p))
6387 +               goto out;
6388 +
6389 +       err = 0;
6390 +       p->n = 0;
6391 +       file->private_data = p;
6392 +       if (!xf)
6393 +               goto out;
6394 +
6395 +       err = vfsub_getattr(&xf->f_path, &st);
6396 +       if (!err) {
6397 +               if (do_fcnt)
6398 +                       p->n = snprintf
6399 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6400 +                                cnt, st.blocks, st.blksize,
6401 +                                (long long)st.size);
6402 +               else
6403 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6404 +                                       st.blocks, st.blksize,
6405 +                                       (long long)st.size);
6406 +               AuDebugOn(p->n >= sizeof(p->a));
6407 +       } else {
6408 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6409 +               err = 0;
6410 +       }
6411 +
6412 +out:
6413 +       return err;
6414 +}
6415 +
6416 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6417 +                              size_t count, loff_t *ppos)
6418 +{
6419 +       struct dbgaufs_arg *p;
6420 +
6421 +       p = file->private_data;
6422 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6423 +}
6424 +
6425 +/* ---------------------------------------------------------------------- */
6426 +
6427 +struct dbgaufs_plink_arg {
6428 +       int n;
6429 +       char a[];
6430 +};
6431 +
6432 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6433 +                                struct file *file)
6434 +{
6435 +       free_page((unsigned long)file->private_data);
6436 +       return 0;
6437 +}
6438 +
6439 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6440 +{
6441 +       int err, i, limit;
6442 +       unsigned long n, sum;
6443 +       struct dbgaufs_plink_arg *p;
6444 +       struct au_sbinfo *sbinfo;
6445 +       struct super_block *sb;
6446 +       struct hlist_bl_head *hbl;
6447 +
6448 +       err = -ENOMEM;
6449 +       p = (void *)get_zeroed_page(GFP_NOFS);
6450 +       if (unlikely(!p))
6451 +               goto out;
6452 +
6453 +       err = -EFBIG;
6454 +       sbinfo = inode->i_private;
6455 +       sb = sbinfo->si_sb;
6456 +       si_noflush_read_lock(sb);
6457 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6458 +               limit = PAGE_SIZE - sizeof(p->n);
6459 +
6460 +               /* the number of buckets */
6461 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6462 +               p->n += n;
6463 +               limit -= n;
6464 +
6465 +               sum = 0;
6466 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6467 +                    i++, hbl++) {
6468 +                       n = au_hbl_count(hbl);
6469 +                       sum += n;
6470 +
6471 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6472 +                       p->n += n;
6473 +                       limit -= n;
6474 +                       if (unlikely(limit <= 0))
6475 +                               goto out_free;
6476 +               }
6477 +               p->a[p->n - 1] = '\n';
6478 +
6479 +               /* the sum of plinks */
6480 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6481 +               p->n += n;
6482 +               limit -= n;
6483 +               if (unlikely(limit <= 0))
6484 +                       goto out_free;
6485 +       } else {
6486 +#define str "1\n0\n0\n"
6487 +               p->n = sizeof(str) - 1;
6488 +               strcpy(p->a, str);
6489 +#undef str
6490 +       }
6491 +       si_read_unlock(sb);
6492 +
6493 +       err = 0;
6494 +       file->private_data = p;
6495 +       goto out; /* success */
6496 +
6497 +out_free:
6498 +       free_page((unsigned long)p);
6499 +out:
6500 +       return err;
6501 +}
6502 +
6503 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6504 +                                 size_t count, loff_t *ppos)
6505 +{
6506 +       struct dbgaufs_plink_arg *p;
6507 +
6508 +       p = file->private_data;
6509 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6510 +}
6511 +
6512 +static const struct file_operations dbgaufs_plink_fop = {
6513 +       .owner          = THIS_MODULE,
6514 +       .open           = dbgaufs_plink_open,
6515 +       .release        = dbgaufs_plink_release,
6516 +       .read           = dbgaufs_plink_read
6517 +};
6518 +
6519 +/* ---------------------------------------------------------------------- */
6520 +
6521 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6522 +{
6523 +       int err;
6524 +       struct au_sbinfo *sbinfo;
6525 +       struct super_block *sb;
6526 +
6527 +       sbinfo = inode->i_private;
6528 +       sb = sbinfo->si_sb;
6529 +       si_noflush_read_lock(sb);
6530 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6531 +       si_read_unlock(sb);
6532 +       return err;
6533 +}
6534 +
6535 +static const struct file_operations dbgaufs_xib_fop = {
6536 +       .owner          = THIS_MODULE,
6537 +       .open           = dbgaufs_xib_open,
6538 +       .release        = dbgaufs_xi_release,
6539 +       .read           = dbgaufs_xi_read
6540 +};
6541 +
6542 +/* ---------------------------------------------------------------------- */
6543 +
6544 +#define DbgaufsXi_PREFIX "xi"
6545 +
6546 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6547 +{
6548 +       int err, idx;
6549 +       long l;
6550 +       aufs_bindex_t bindex;
6551 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6552 +       struct au_sbinfo *sbinfo;
6553 +       struct super_block *sb;
6554 +       struct au_xino *xi;
6555 +       struct file *xf;
6556 +       struct qstr *name;
6557 +       struct au_branch *br;
6558 +
6559 +       err = -ENOENT;
6560 +       name = &file->f_path.dentry->d_name;
6561 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6562 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6563 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6564 +               goto out;
6565 +
6566 +       AuDebugOn(name->len >= sizeof(a));
6567 +       memcpy(a, name->name, name->len);
6568 +       a[name->len] = '\0';
6569 +       p = strchr(a, '-');
6570 +       if (p)
6571 +               *p = '\0';
6572 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6573 +       if (unlikely(err))
6574 +               goto out;
6575 +       bindex = l;
6576 +       idx = 0;
6577 +       if (p) {
6578 +               err = kstrtol(p + 1, 10, &l);
6579 +               if (unlikely(err))
6580 +                       goto out;
6581 +               idx = l;
6582 +       }
6583 +
6584 +       err = -ENOENT;
6585 +       sbinfo = inode->i_private;
6586 +       sb = sbinfo->si_sb;
6587 +       si_noflush_read_lock(sb);
6588 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6589 +               goto out_si;
6590 +       br = au_sbr(sb, bindex);
6591 +       xi = br->br_xino;
6592 +       if (unlikely(idx >= xi->xi_nfile))
6593 +               goto out_si;
6594 +       xf = au_xino_file(xi, idx);
6595 +       if (xf)
6596 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6597 +                                     au_xino_count(br));
6598 +
6599 +out_si:
6600 +       si_read_unlock(sb);
6601 +out:
6602 +       AuTraceErr(err);
6603 +       return err;
6604 +}
6605 +
6606 +static const struct file_operations dbgaufs_xino_fop = {
6607 +       .owner          = THIS_MODULE,
6608 +       .open           = dbgaufs_xino_open,
6609 +       .release        = dbgaufs_xi_release,
6610 +       .read           = dbgaufs_xi_read
6611 +};
6612 +
6613 +void dbgaufs_xino_del(struct au_branch *br)
6614 +{
6615 +       struct dentry *dbgaufs;
6616 +
6617 +       dbgaufs = br->br_dbgaufs;
6618 +       if (!dbgaufs)
6619 +               return;
6620 +
6621 +       br->br_dbgaufs = NULL;
6622 +       /* debugfs acquires the parent i_mutex */
6623 +       lockdep_off();
6624 +       debugfs_remove(dbgaufs);
6625 +       lockdep_on();
6626 +}
6627 +
6628 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6629 +{
6630 +       aufs_bindex_t bbot;
6631 +       struct au_branch *br;
6632 +
6633 +       if (!au_sbi(sb)->si_dbgaufs)
6634 +               return;
6635 +
6636 +       bbot = au_sbbot(sb);
6637 +       for (; bindex <= bbot; bindex++) {
6638 +               br = au_sbr(sb, bindex);
6639 +               dbgaufs_xino_del(br);
6640 +       }
6641 +}
6642 +
6643 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6644 +                             unsigned int idx, struct dentry *parent,
6645 +                             struct au_sbinfo *sbinfo)
6646 +{
6647 +       struct au_branch *br;
6648 +       struct dentry *d;
6649 +       /* "xi" bindex(5) "-" idx(2) NULL */
6650 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6651 +
6652 +       if (!idx)
6653 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6654 +       else
6655 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6656 +                        bindex, idx);
6657 +       br = au_sbr(sb, bindex);
6658 +       if (br->br_dbgaufs) {
6659 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6660 +
6661 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6662 +                       /* debugfs acquires the parent i_mutex */
6663 +                       lockdep_off();
6664 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6665 +                                          name);
6666 +                       lockdep_on();
6667 +                       if (unlikely(!d))
6668 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6669 +                                       parent, name);
6670 +               }
6671 +       } else {
6672 +               lockdep_off();
6673 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6674 +                                                    sbinfo, &dbgaufs_xino_fop);
6675 +               lockdep_on();
6676 +               if (unlikely(!br->br_dbgaufs))
6677 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6678 +                               parent, name);
6679 +       }
6680 +}
6681 +
6682 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6683 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6684 +{
6685 +       struct au_branch *br;
6686 +       struct au_xino *xi;
6687 +       unsigned int u;
6688 +
6689 +       br = au_sbr(sb, bindex);
6690 +       xi = br->br_xino;
6691 +       for (u = 0; u < xi->xi_nfile; u++)
6692 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6693 +}
6694 +
6695 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6696 +{
6697 +       struct au_sbinfo *sbinfo;
6698 +       struct dentry *parent;
6699 +       aufs_bindex_t bbot;
6700 +
6701 +       if (!au_opt_test(au_mntflags(sb), XINO))
6702 +               return;
6703 +
6704 +       sbinfo = au_sbi(sb);
6705 +       parent = sbinfo->si_dbgaufs;
6706 +       if (!parent)
6707 +               return;
6708 +
6709 +       bbot = au_sbbot(sb);
6710 +       if (topdown)
6711 +               for (; bindex <= bbot; bindex++)
6712 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6713 +       else
6714 +               for (; bbot >= bindex; bbot--)
6715 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6716 +}
6717 +
6718 +/* ---------------------------------------------------------------------- */
6719 +
6720 +#ifdef CONFIG_AUFS_EXPORT
6721 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6722 +{
6723 +       int err;
6724 +       struct au_sbinfo *sbinfo;
6725 +       struct super_block *sb;
6726 +
6727 +       sbinfo = inode->i_private;
6728 +       sb = sbinfo->si_sb;
6729 +       si_noflush_read_lock(sb);
6730 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6731 +       si_read_unlock(sb);
6732 +       return err;
6733 +}
6734 +
6735 +static const struct file_operations dbgaufs_xigen_fop = {
6736 +       .owner          = THIS_MODULE,
6737 +       .open           = dbgaufs_xigen_open,
6738 +       .release        = dbgaufs_xi_release,
6739 +       .read           = dbgaufs_xi_read
6740 +};
6741 +
6742 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6743 +{
6744 +       int err;
6745 +
6746 +       /*
6747 +        * This function is a dynamic '__init' function actually,
6748 +        * so the tiny check for si_rwsem is unnecessary.
6749 +        */
6750 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6751 +
6752 +       err = -EIO;
6753 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6754 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6755 +                &dbgaufs_xigen_fop);
6756 +       if (sbinfo->si_dbgaufs_xigen)
6757 +               err = 0;
6758 +
6759 +       return err;
6760 +}
6761 +#else
6762 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6763 +{
6764 +       return 0;
6765 +}
6766 +#endif /* CONFIG_AUFS_EXPORT */
6767 +
6768 +/* ---------------------------------------------------------------------- */
6769 +
6770 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6771 +{
6772 +       /*
6773 +        * This function is a dynamic '__fin' function actually,
6774 +        * so the tiny check for si_rwsem is unnecessary.
6775 +        */
6776 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6777 +
6778 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6779 +       sbinfo->si_dbgaufs = NULL;
6780 +}
6781 +
6782 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6783 +{
6784 +       int err;
6785 +       char name[SysaufsSiNameLen];
6786 +
6787 +       /*
6788 +        * This function is a dynamic '__init' function actually,
6789 +        * so the tiny check for si_rwsem is unnecessary.
6790 +        */
6791 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6792 +
6793 +       err = -ENOENT;
6794 +       if (!dbgaufs) {
6795 +               AuErr1("/debug/aufs is uninitialized\n");
6796 +               goto out;
6797 +       }
6798 +
6799 +       err = -EIO;
6800 +       sysaufs_name(sbinfo, name);
6801 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6802 +       if (unlikely(!sbinfo->si_dbgaufs))
6803 +               goto out;
6804 +
6805 +       /* regardless plink/noplink option */
6806 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6807 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6808 +                &dbgaufs_plink_fop);
6809 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6810 +               goto out_dir;
6811 +
6812 +       /* regardless xino/noxino option */
6813 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6814 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6815 +                &dbgaufs_xib_fop);
6816 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6817 +               goto out_dir;
6818 +
6819 +       err = dbgaufs_xigen_init(sbinfo);
6820 +       if (!err)
6821 +               goto out; /* success */
6822 +
6823 +out_dir:
6824 +       dbgaufs_si_fin(sbinfo);
6825 +out:
6826 +       if (unlikely(err))
6827 +               pr_err("debugfs/aufs failed\n");
6828 +       return err;
6829 +}
6830 +
6831 +/* ---------------------------------------------------------------------- */
6832 +
6833 +void dbgaufs_fin(void)
6834 +{
6835 +       debugfs_remove(dbgaufs);
6836 +}
6837 +
6838 +int __init dbgaufs_init(void)
6839 +{
6840 +       int err;
6841 +
6842 +       err = -EIO;
6843 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6844 +       if (dbgaufs)
6845 +               err = 0;
6846 +       return err;
6847 +}
6848 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6849 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6850 +++ linux/fs/aufs/dbgaufs.h     2022-08-02 12:12:45.162308621 +0200
6851 @@ -0,0 +1,53 @@
6852 +/* SPDX-License-Identifier: GPL-2.0 */
6853 +/*
6854 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6855 + *
6856 + * This program, aufs is free software; you can redistribute it and/or modify
6857 + * it under the terms of the GNU General Public License as published by
6858 + * the Free Software Foundation; either version 2 of the License, or
6859 + * (at your option) any later version.
6860 + *
6861 + * This program is distributed in the hope that it will be useful,
6862 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6863 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6864 + * GNU General Public License for more details.
6865 + *
6866 + * You should have received a copy of the GNU General Public License
6867 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6868 + */
6869 +
6870 +/*
6871 + * debugfs interface
6872 + */
6873 +
6874 +#ifndef __DBGAUFS_H__
6875 +#define __DBGAUFS_H__
6876 +
6877 +#ifdef __KERNEL__
6878 +
6879 +struct super_block;
6880 +struct au_sbinfo;
6881 +struct au_branch;
6882 +
6883 +#ifdef CONFIG_DEBUG_FS
6884 +/* dbgaufs.c */
6885 +void dbgaufs_xino_del(struct au_branch *br);
6886 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6887 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6888 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6889 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6890 +void dbgaufs_fin(void);
6891 +int __init dbgaufs_init(void);
6892 +#else
6893 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6894 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6895 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6896 +          int topdown)
6897 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6898 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6899 +AuStubVoid(dbgaufs_fin, void)
6900 +AuStubInt0(__init dbgaufs_init, void)
6901 +#endif /* CONFIG_DEBUG_FS */
6902 +
6903 +#endif /* __KERNEL__ */
6904 +#endif /* __DBGAUFS_H__ */
6905 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6906 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6907 +++ linux/fs/aufs/dcsub.c       2022-08-02 12:12:45.162308621 +0200
6908 @@ -0,0 +1,225 @@
6909 +// SPDX-License-Identifier: GPL-2.0
6910 +/*
6911 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6912 + *
6913 + * This program, aufs is free software; you can redistribute it and/or modify
6914 + * it under the terms of the GNU General Public License as published by
6915 + * the Free Software Foundation; either version 2 of the License, or
6916 + * (at your option) any later version.
6917 + *
6918 + * This program is distributed in the hope that it will be useful,
6919 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6920 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6921 + * GNU General Public License for more details.
6922 + *
6923 + * You should have received a copy of the GNU General Public License
6924 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6925 + */
6926 +
6927 +/*
6928 + * sub-routines for dentry cache
6929 + */
6930 +
6931 +#include "aufs.h"
6932 +
6933 +static void au_dpage_free(struct au_dpage *dpage)
6934 +{
6935 +       int i;
6936 +       struct dentry **p;
6937 +
6938 +       p = dpage->dentries;
6939 +       for (i = 0; i < dpage->ndentry; i++)
6940 +               dput(*p++);
6941 +       free_page((unsigned long)dpage->dentries);
6942 +}
6943 +
6944 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6945 +{
6946 +       int err;
6947 +       void *p;
6948 +
6949 +       err = -ENOMEM;
6950 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6951 +       if (unlikely(!dpages->dpages))
6952 +               goto out;
6953 +
6954 +       p = (void *)__get_free_page(gfp);
6955 +       if (unlikely(!p))
6956 +               goto out_dpages;
6957 +
6958 +       dpages->dpages[0].ndentry = 0;
6959 +       dpages->dpages[0].dentries = p;
6960 +       dpages->ndpage = 1;
6961 +       return 0; /* success */
6962 +
6963 +out_dpages:
6964 +       au_kfree_try_rcu(dpages->dpages);
6965 +out:
6966 +       return err;
6967 +}
6968 +
6969 +void au_dpages_free(struct au_dcsub_pages *dpages)
6970 +{
6971 +       int i;
6972 +       struct au_dpage *p;
6973 +
6974 +       p = dpages->dpages;
6975 +       for (i = 0; i < dpages->ndpage; i++)
6976 +               au_dpage_free(p++);
6977 +       au_kfree_try_rcu(dpages->dpages);
6978 +}
6979 +
6980 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6981 +                           struct dentry *dentry, gfp_t gfp)
6982 +{
6983 +       int err, sz;
6984 +       struct au_dpage *dpage;
6985 +       void *p;
6986 +
6987 +       dpage = dpages->dpages + dpages->ndpage - 1;
6988 +       sz = PAGE_SIZE / sizeof(dentry);
6989 +       if (unlikely(dpage->ndentry >= sz)) {
6990 +               AuLabel(new dpage);
6991 +               err = -ENOMEM;
6992 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6993 +               p = au_kzrealloc(dpages->dpages, sz,
6994 +                                sz + sizeof(*dpages->dpages), gfp,
6995 +                                /*may_shrink*/0);
6996 +               if (unlikely(!p))
6997 +                       goto out;
6998 +
6999 +               dpages->dpages = p;
7000 +               dpage = dpages->dpages + dpages->ndpage;
7001 +               p = (void *)__get_free_page(gfp);
7002 +               if (unlikely(!p))
7003 +                       goto out;
7004 +
7005 +               dpage->ndentry = 0;
7006 +               dpage->dentries = p;
7007 +               dpages->ndpage++;
7008 +       }
7009 +
7010 +       AuDebugOn(au_dcount(dentry) <= 0);
7011 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7012 +       return 0; /* success */
7013 +
7014 +out:
7015 +       return err;
7016 +}
7017 +
7018 +/* todo: BAD approach */
7019 +/* copied from linux/fs/dcache.c */
7020 +enum d_walk_ret {
7021 +       D_WALK_CONTINUE,
7022 +       D_WALK_QUIT,
7023 +       D_WALK_NORETRY,
7024 +       D_WALK_SKIP,
7025 +};
7026 +
7027 +extern void d_walk(struct dentry *parent, void *data,
7028 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7029 +
7030 +struct ac_dpages_arg {
7031 +       int err;
7032 +       struct au_dcsub_pages *dpages;
7033 +       struct super_block *sb;
7034 +       au_dpages_test test;
7035 +       void *arg;
7036 +};
7037 +
7038 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7039 +{
7040 +       enum d_walk_ret ret;
7041 +       struct ac_dpages_arg *arg = _arg;
7042 +
7043 +       ret = D_WALK_CONTINUE;
7044 +       if (dentry->d_sb == arg->sb
7045 +           && !IS_ROOT(dentry)
7046 +           && au_dcount(dentry) > 0
7047 +           && au_di(dentry)
7048 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7049 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7050 +               if (unlikely(arg->err))
7051 +                       ret = D_WALK_QUIT;
7052 +       }
7053 +
7054 +       return ret;
7055 +}
7056 +
7057 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7058 +                  au_dpages_test test, void *arg)
7059 +{
7060 +       struct ac_dpages_arg args = {
7061 +               .err    = 0,
7062 +               .dpages = dpages,
7063 +               .sb     = root->d_sb,
7064 +               .test   = test,
7065 +               .arg    = arg
7066 +       };
7067 +
7068 +       d_walk(root, &args, au_call_dpages_append);
7069 +
7070 +       return args.err;
7071 +}
7072 +
7073 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7074 +                      int do_include, au_dpages_test test, void *arg)
7075 +{
7076 +       int err;
7077 +
7078 +       err = 0;
7079 +       write_seqlock(&rename_lock);
7080 +       spin_lock(&dentry->d_lock);
7081 +       if (do_include
7082 +           && au_dcount(dentry) > 0
7083 +           && (!test || test(dentry, arg)))
7084 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7085 +       spin_unlock(&dentry->d_lock);
7086 +       if (unlikely(err))
7087 +               goto out;
7088 +
7089 +       /*
7090 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7091 +        * mount
7092 +        */
7093 +       while (!IS_ROOT(dentry)) {
7094 +               dentry = dentry->d_parent; /* rename_lock is locked */
7095 +               spin_lock(&dentry->d_lock);
7096 +               if (au_dcount(dentry) > 0
7097 +                   && (!test || test(dentry, arg)))
7098 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7099 +               spin_unlock(&dentry->d_lock);
7100 +               if (unlikely(err))
7101 +                       break;
7102 +       }
7103 +
7104 +out:
7105 +       write_sequnlock(&rename_lock);
7106 +       return err;
7107 +}
7108 +
7109 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7110 +{
7111 +       return au_di(dentry) && dentry->d_sb == arg;
7112 +}
7113 +
7114 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7115 +                           struct dentry *dentry, int do_include)
7116 +{
7117 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7118 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7119 +}
7120 +
7121 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7122 +{
7123 +       struct path path[2] = {
7124 +               {
7125 +                       .dentry = d1
7126 +               },
7127 +               {
7128 +                       .dentry = d2
7129 +               }
7130 +       };
7131 +
7132 +       return path_is_under(path + 0, path + 1);
7133 +}
7134 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7135 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7136 +++ linux/fs/aufs/dcsub.h       2022-08-02 12:12:45.162308621 +0200
7137 @@ -0,0 +1,137 @@
7138 +/* SPDX-License-Identifier: GPL-2.0 */
7139 +/*
7140 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7141 + *
7142 + * This program, aufs is free software; you can redistribute it and/or modify
7143 + * it under the terms of the GNU General Public License as published by
7144 + * the Free Software Foundation; either version 2 of the License, or
7145 + * (at your option) any later version.
7146 + *
7147 + * This program is distributed in the hope that it will be useful,
7148 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7149 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7150 + * GNU General Public License for more details.
7151 + *
7152 + * You should have received a copy of the GNU General Public License
7153 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7154 + */
7155 +
7156 +/*
7157 + * sub-routines for dentry cache
7158 + */
7159 +
7160 +#ifndef __AUFS_DCSUB_H__
7161 +#define __AUFS_DCSUB_H__
7162 +
7163 +#ifdef __KERNEL__
7164 +
7165 +#include <linux/dcache.h>
7166 +#include <linux/fs.h>
7167 +
7168 +struct au_dpage {
7169 +       int ndentry;
7170 +       struct dentry **dentries;
7171 +};
7172 +
7173 +struct au_dcsub_pages {
7174 +       int ndpage;
7175 +       struct au_dpage *dpages;
7176 +};
7177 +
7178 +/* ---------------------------------------------------------------------- */
7179 +
7180 +/* dcsub.c */
7181 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7182 +void au_dpages_free(struct au_dcsub_pages *dpages);
7183 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7184 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7185 +                  au_dpages_test test, void *arg);
7186 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7187 +                      int do_include, au_dpages_test test, void *arg);
7188 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7189 +                           struct dentry *dentry, int do_include);
7190 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7191 +
7192 +/* ---------------------------------------------------------------------- */
7193 +
7194 +/*
7195 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7196 + * include/linux/dcache.h. Try them (in the future).
7197 + */
7198 +
7199 +static inline int au_d_hashed_positive(struct dentry *d)
7200 +{
7201 +       int err;
7202 +       struct inode *inode = d_inode(d);
7203 +
7204 +       err = 0;
7205 +       if (unlikely(d_unhashed(d)
7206 +                    || d_is_negative(d)
7207 +                    || !inode->i_nlink))
7208 +               err = -ENOENT;
7209 +       return err;
7210 +}
7211 +
7212 +static inline int au_d_linkable(struct dentry *d)
7213 +{
7214 +       int err;
7215 +       struct inode *inode = d_inode(d);
7216 +
7217 +       err = au_d_hashed_positive(d);
7218 +       if (err
7219 +           && d_is_positive(d)
7220 +           && (inode->i_state & I_LINKABLE))
7221 +               err = 0;
7222 +       return err;
7223 +}
7224 +
7225 +static inline int au_d_alive(struct dentry *d)
7226 +{
7227 +       int err;
7228 +       struct inode *inode;
7229 +
7230 +       err = 0;
7231 +       if (!IS_ROOT(d))
7232 +               err = au_d_hashed_positive(d);
7233 +       else {
7234 +               inode = d_inode(d);
7235 +               if (unlikely(d_unlinked(d)
7236 +                            || d_is_negative(d)
7237 +                            || !inode->i_nlink))
7238 +                       err = -ENOENT;
7239 +       }
7240 +       return err;
7241 +}
7242 +
7243 +static inline int au_alive_dir(struct dentry *d)
7244 +{
7245 +       int err;
7246 +
7247 +       err = au_d_alive(d);
7248 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7249 +               err = -ENOENT;
7250 +       return err;
7251 +}
7252 +
7253 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7254 +{
7255 +       return a->len == b->len
7256 +               && !memcmp(a->name, b->name, a->len);
7257 +}
7258 +
7259 +/*
7260 + * by the commit
7261 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7262 + *                     taking d_lock
7263 + * the type of d_lockref.count became int, but the inlined function d_count()
7264 + * still returns unsigned int.
7265 + * I don't know why. Maybe it is for every d_count() users?
7266 + * Anyway au_dcount() lives on.
7267 + */
7268 +static inline int au_dcount(struct dentry *d)
7269 +{
7270 +       return (int)d_count(d);
7271 +}
7272 +
7273 +#endif /* __KERNEL__ */
7274 +#endif /* __AUFS_DCSUB_H__ */
7275 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7276 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7277 +++ linux/fs/aufs/debug.c       2022-08-02 12:12:45.162308621 +0200
7278 @@ -0,0 +1,444 @@
7279 +// SPDX-License-Identifier: GPL-2.0
7280 +/*
7281 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7282 + *
7283 + * This program, aufs is free software; you can redistribute it and/or modify
7284 + * it under the terms of the GNU General Public License as published by
7285 + * the Free Software Foundation; either version 2 of the License, or
7286 + * (at your option) any later version.
7287 + *
7288 + * This program is distributed in the hope that it will be useful,
7289 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7290 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7291 + * GNU General Public License for more details.
7292 + *
7293 + * You should have received a copy of the GNU General Public License
7294 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7295 + */
7296 +
7297 +/*
7298 + * debug print functions
7299 + */
7300 +
7301 +#include <linux/iversion.h>
7302 +#include "aufs.h"
7303 +
7304 +/* Returns 0, or -errno.  arg is in kp->arg. */
7305 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7306 +{
7307 +       int err, n;
7308 +
7309 +       err = kstrtoint(val, 0, &n);
7310 +       if (!err) {
7311 +               if (n > 0)
7312 +                       au_debug_on();
7313 +               else
7314 +                       au_debug_off();
7315 +       }
7316 +       return err;
7317 +}
7318 +
7319 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7320 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7321 +{
7322 +       atomic_t *a;
7323 +
7324 +       a = kp->arg;
7325 +       return sprintf(buffer, "%d", atomic_read(a));
7326 +}
7327 +
7328 +static const struct kernel_param_ops param_ops_atomic_t = {
7329 +       .set = param_atomic_t_set,
7330 +       .get = param_atomic_t_get
7331 +       /* void (*free)(void *arg) */
7332 +};
7333 +
7334 +atomic_t aufs_debug = ATOMIC_INIT(0);
7335 +MODULE_PARM_DESC(debug, "debug print");
7336 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7337 +
7338 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7339 +char *au_plevel = KERN_DEBUG;
7340 +#define dpri(fmt, ...) do {                                    \
7341 +       if ((au_plevel                                          \
7342 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7343 +           || au_debug_test())                                 \
7344 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7345 +} while (0)
7346 +
7347 +/* ---------------------------------------------------------------------- */
7348 +
7349 +void au_dpri_whlist(struct au_nhash *whlist)
7350 +{
7351 +       unsigned long ul, n;
7352 +       struct hlist_head *head;
7353 +       struct au_vdir_wh *pos;
7354 +
7355 +       n = whlist->nh_num;
7356 +       head = whlist->nh_head;
7357 +       for (ul = 0; ul < n; ul++) {
7358 +               hlist_for_each_entry(pos, head, wh_hash)
7359 +                       dpri("b%d, %.*s, %d\n",
7360 +                            pos->wh_bindex,
7361 +                            pos->wh_str.len, pos->wh_str.name,
7362 +                            pos->wh_str.len);
7363 +               head++;
7364 +       }
7365 +}
7366 +
7367 +void au_dpri_vdir(struct au_vdir *vdir)
7368 +{
7369 +       unsigned long ul;
7370 +       union au_vdir_deblk_p p;
7371 +       unsigned char *o;
7372 +
7373 +       if (!vdir || IS_ERR(vdir)) {
7374 +               dpri("err %ld\n", PTR_ERR(vdir));
7375 +               return;
7376 +       }
7377 +
7378 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7379 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7380 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7381 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7382 +               p.deblk = vdir->vd_deblk[ul];
7383 +               o = p.deblk;
7384 +               dpri("[%lu]: %p\n", ul, o);
7385 +       }
7386 +}
7387 +
7388 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7389 +                       struct dentry *wh)
7390 +{
7391 +       char *n = NULL;
7392 +       int l = 0;
7393 +
7394 +       if (!inode || IS_ERR(inode)) {
7395 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7396 +               return -1;
7397 +       }
7398 +
7399 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7400 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7401 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7402 +       if (wh) {
7403 +               n = (void *)wh->d_name.name;
7404 +               l = wh->d_name.len;
7405 +       }
7406 +
7407 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7408 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7409 +            bindex, inode,
7410 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7411 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7412 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7413 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7414 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7415 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7416 +            inode->i_generation,
7417 +            l ? ", wh " : "", l, n);
7418 +       return 0;
7419 +}
7420 +
7421 +void au_dpri_inode(struct inode *inode)
7422 +{
7423 +       struct au_iinfo *iinfo;
7424 +       struct au_hinode *hi;
7425 +       aufs_bindex_t bindex;
7426 +       int err, hn;
7427 +
7428 +       err = do_pri_inode(-1, inode, -1, NULL);
7429 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7430 +               return;
7431 +
7432 +       iinfo = au_ii(inode);
7433 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7434 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7435 +       if (iinfo->ii_btop < 0)
7436 +               return;
7437 +       hn = 0;
7438 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7439 +               hi = au_hinode(iinfo, bindex);
7440 +               hn = !!au_hn(hi);
7441 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7442 +       }
7443 +}
7444 +
7445 +void au_dpri_dalias(struct inode *inode)
7446 +{
7447 +       struct dentry *d;
7448 +
7449 +       spin_lock(&inode->i_lock);
7450 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7451 +               au_dpri_dentry(d);
7452 +       spin_unlock(&inode->i_lock);
7453 +}
7454 +
7455 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7456 +{
7457 +       struct dentry *wh = NULL;
7458 +       int hn;
7459 +       struct inode *inode;
7460 +       struct au_iinfo *iinfo;
7461 +       struct au_hinode *hi;
7462 +
7463 +       if (!dentry || IS_ERR(dentry)) {
7464 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7465 +               return -1;
7466 +       }
7467 +       /* do not call dget_parent() here */
7468 +       /* note: access d_xxx without d_lock */
7469 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7470 +            bindex, dentry, dentry,
7471 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7472 +            au_dcount(dentry), dentry->d_flags,
7473 +            d_unhashed(dentry) ? "un" : "");
7474 +       hn = -1;
7475 +       inode = NULL;
7476 +       if (d_is_positive(dentry))
7477 +               inode = d_inode(dentry);
7478 +       if (inode
7479 +           && au_test_aufs(dentry->d_sb)
7480 +           && bindex >= 0
7481 +           && !au_is_bad_inode(inode)) {
7482 +               iinfo = au_ii(inode);
7483 +               hi = au_hinode(iinfo, bindex);
7484 +               hn = !!au_hn(hi);
7485 +               wh = hi->hi_whdentry;
7486 +       }
7487 +       do_pri_inode(bindex, inode, hn, wh);
7488 +       return 0;
7489 +}
7490 +
7491 +void au_dpri_dentry(struct dentry *dentry)
7492 +{
7493 +       struct au_dinfo *dinfo;
7494 +       aufs_bindex_t bindex;
7495 +       int err;
7496 +
7497 +       err = do_pri_dentry(-1, dentry);
7498 +       if (err || !au_test_aufs(dentry->d_sb))
7499 +               return;
7500 +
7501 +       dinfo = au_di(dentry);
7502 +       if (!dinfo)
7503 +               return;
7504 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7505 +            dinfo->di_btop, dinfo->di_bbot,
7506 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7507 +            dinfo->di_tmpfile);
7508 +       if (dinfo->di_btop < 0)
7509 +               return;
7510 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7511 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7512 +}
7513 +
7514 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7515 +{
7516 +       char a[32];
7517 +
7518 +       if (!file || IS_ERR(file)) {
7519 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7520 +               return -1;
7521 +       }
7522 +       a[0] = 0;
7523 +       if (bindex < 0
7524 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7525 +           && au_test_aufs(file->f_path.dentry->d_sb)
7526 +           && au_fi(file))
7527 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7528 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7529 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7530 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7531 +            file->f_version, file->f_pos, a);
7532 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7533 +               do_pri_dentry(bindex, file->f_path.dentry);
7534 +       return 0;
7535 +}
7536 +
7537 +void au_dpri_file(struct file *file)
7538 +{
7539 +       struct au_finfo *finfo;
7540 +       struct au_fidir *fidir;
7541 +       struct au_hfile *hfile;
7542 +       aufs_bindex_t bindex;
7543 +       int err;
7544 +
7545 +       err = do_pri_file(-1, file);
7546 +       if (err
7547 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7548 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7549 +               return;
7550 +
7551 +       finfo = au_fi(file);
7552 +       if (!finfo)
7553 +               return;
7554 +       if (finfo->fi_btop < 0)
7555 +               return;
7556 +       fidir = finfo->fi_hdir;
7557 +       if (!fidir)
7558 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7559 +       else
7560 +               for (bindex = finfo->fi_btop;
7561 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7562 +                    bindex++) {
7563 +                       hfile = fidir->fd_hfile + bindex;
7564 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7565 +               }
7566 +}
7567 +
7568 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7569 +{
7570 +       struct vfsmount *mnt;
7571 +       struct super_block *sb;
7572 +
7573 +       if (!br || IS_ERR(br))
7574 +               goto out;
7575 +       mnt = au_br_mnt(br);
7576 +       if (!mnt || IS_ERR(mnt))
7577 +               goto out;
7578 +       sb = mnt->mnt_sb;
7579 +       if (!sb || IS_ERR(sb))
7580 +               goto out;
7581 +
7582 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7583 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7584 +            "xino %d\n",
7585 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7586 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7587 +            sb->s_flags, sb->s_count,
7588 +            atomic_read(&sb->s_active),
7589 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7590 +       return 0;
7591 +
7592 +out:
7593 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7594 +       return -1;
7595 +}
7596 +
7597 +void au_dpri_sb(struct super_block *sb)
7598 +{
7599 +       struct au_sbinfo *sbinfo;
7600 +       aufs_bindex_t bindex;
7601 +       int err;
7602 +       /* to reduce stack size */
7603 +       struct {
7604 +               struct vfsmount mnt;
7605 +               struct au_branch fake;
7606 +       } *a;
7607 +
7608 +       /* this function can be called from magic sysrq */
7609 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7610 +       if (unlikely(!a)) {
7611 +               dpri("no memory\n");
7612 +               return;
7613 +       }
7614 +
7615 +       a->mnt.mnt_sb = sb;
7616 +       a->fake.br_path.mnt = &a->mnt;
7617 +       err = do_pri_br(-1, &a->fake);
7618 +       au_kfree_rcu(a);
7619 +       dpri("dev 0x%x\n", sb->s_dev);
7620 +       if (err || !au_test_aufs(sb))
7621 +               return;
7622 +
7623 +       sbinfo = au_sbi(sb);
7624 +       if (!sbinfo)
7625 +               return;
7626 +       dpri("nw %d, gen %u, kobj %d\n",
7627 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7628 +            kref_read(&sbinfo->si_kobj.kref));
7629 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7630 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7631 +}
7632 +
7633 +/* ---------------------------------------------------------------------- */
7634 +
7635 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7636 +{
7637 +       struct inode *h_inode, *inode = d_inode(dentry);
7638 +       struct dentry *h_dentry;
7639 +       aufs_bindex_t bindex, bbot, bi;
7640 +
7641 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7642 +               return;
7643 +
7644 +       bbot = au_dbbot(dentry);
7645 +       bi = au_ibbot(inode);
7646 +       if (bi < bbot)
7647 +               bbot = bi;
7648 +       bindex = au_dbtop(dentry);
7649 +       bi = au_ibtop(inode);
7650 +       if (bi > bindex)
7651 +               bindex = bi;
7652 +
7653 +       for (; bindex <= bbot; bindex++) {
7654 +               h_dentry = au_h_dptr(dentry, bindex);
7655 +               if (!h_dentry)
7656 +                       continue;
7657 +               h_inode = au_h_iptr(inode, bindex);
7658 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7659 +                       au_debug_on();
7660 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7661 +                       AuDbgDentry(dentry);
7662 +                       AuDbgInode(inode);
7663 +                       au_debug_off();
7664 +                       if (au_test_fuse(h_inode->i_sb))
7665 +                               WARN_ON_ONCE(1);
7666 +                       else
7667 +                               BUG();
7668 +               }
7669 +       }
7670 +}
7671 +
7672 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7673 +{
7674 +       int err, i, j;
7675 +       struct au_dcsub_pages dpages;
7676 +       struct au_dpage *dpage;
7677 +       struct dentry **dentries;
7678 +
7679 +       err = au_dpages_init(&dpages, GFP_NOFS);
7680 +       AuDebugOn(err);
7681 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7682 +       AuDebugOn(err);
7683 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7684 +               dpage = dpages.dpages + i;
7685 +               dentries = dpage->dentries;
7686 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7687 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7688 +       }
7689 +       au_dpages_free(&dpages);
7690 +}
7691 +
7692 +void au_dbg_verify_kthread(void)
7693 +{
7694 +       if (au_wkq_test()) {
7695 +               au_dbg_blocked();
7696 +               /*
7697 +                * It may be recursive, but udba=notify between two aufs mounts,
7698 +                * where a single ro branch is shared, is not a problem.
7699 +                */
7700 +               /* WARN_ON(1); */
7701 +       }
7702 +}
7703 +
7704 +/* ---------------------------------------------------------------------- */
7705 +
7706 +int __init au_debug_init(void)
7707 +{
7708 +       aufs_bindex_t bindex;
7709 +       struct au_vdir_destr destr;
7710 +
7711 +       bindex = -1;
7712 +       AuDebugOn(bindex >= 0);
7713 +
7714 +       destr.len = -1;
7715 +       AuDebugOn(destr.len < NAME_MAX);
7716 +
7717 +#ifdef CONFIG_4KSTACKS
7718 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7719 +#endif
7720 +
7721 +       return 0;
7722 +}
7723 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7724 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7725 +++ linux/fs/aufs/debug.h       2022-08-02 12:12:45.162308621 +0200
7726 @@ -0,0 +1,226 @@
7727 +/* SPDX-License-Identifier: GPL-2.0 */
7728 +/*
7729 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7730 + *
7731 + * This program, aufs is free software; you can redistribute it and/or modify
7732 + * it under the terms of the GNU General Public License as published by
7733 + * the Free Software Foundation; either version 2 of the License, or
7734 + * (at your option) any later version.
7735 + *
7736 + * This program is distributed in the hope that it will be useful,
7737 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7738 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7739 + * GNU General Public License for more details.
7740 + *
7741 + * You should have received a copy of the GNU General Public License
7742 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7743 + */
7744 +
7745 +/*
7746 + * debug print functions
7747 + */
7748 +
7749 +#ifndef __AUFS_DEBUG_H__
7750 +#define __AUFS_DEBUG_H__
7751 +
7752 +#ifdef __KERNEL__
7753 +
7754 +#include <linux/atomic.h>
7755 +#include <linux/module.h>
7756 +#include <linux/kallsyms.h>
7757 +#include <linux/sysrq.h>
7758 +
7759 +#ifdef CONFIG_AUFS_DEBUG
7760 +#define AuDebugOn(a)           BUG_ON(a)
7761 +
7762 +/* module parameter */
7763 +extern atomic_t aufs_debug;
7764 +static inline void au_debug_on(void)
7765 +{
7766 +       atomic_inc(&aufs_debug);
7767 +}
7768 +static inline void au_debug_off(void)
7769 +{
7770 +       atomic_dec_if_positive(&aufs_debug);
7771 +}
7772 +
7773 +static inline int au_debug_test(void)
7774 +{
7775 +       return atomic_read(&aufs_debug) > 0;
7776 +}
7777 +#else
7778 +#define AuDebugOn(a)           do {} while (0)
7779 +AuStubVoid(au_debug_on, void)
7780 +AuStubVoid(au_debug_off, void)
7781 +AuStubInt0(au_debug_test, void)
7782 +#endif /* CONFIG_AUFS_DEBUG */
7783 +
7784 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7785 +
7786 +/* ---------------------------------------------------------------------- */
7787 +
7788 +/* debug print */
7789 +
7790 +#define AuDbg(fmt, ...) do { \
7791 +       if (au_debug_test()) \
7792 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7793 +} while (0)
7794 +#define AuLabel(l)             AuDbg(#l "\n")
7795 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7796 +#define AuWarn1(fmt, ...) do { \
7797 +       static unsigned char _c; \
7798 +       if (!_c++) \
7799 +               pr_warn(fmt, ##__VA_ARGS__); \
7800 +} while (0)
7801 +
7802 +#define AuErr1(fmt, ...) do { \
7803 +       static unsigned char _c; \
7804 +       if (!_c++) \
7805 +               pr_err(fmt, ##__VA_ARGS__); \
7806 +} while (0)
7807 +
7808 +#define AuIOErr1(fmt, ...) do { \
7809 +       static unsigned char _c; \
7810 +       if (!_c++) \
7811 +               AuIOErr(fmt, ##__VA_ARGS__); \
7812 +} while (0)
7813 +
7814 +#define AuUnsupportMsg "This operation is not supported." \
7815 +                       " Please report this application to aufs-users ML."
7816 +#define AuUnsupport(fmt, ...) do { \
7817 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7818 +       dump_stack(); \
7819 +} while (0)
7820 +
7821 +#define AuTraceErr(e) do { \
7822 +       if (unlikely((e) < 0)) \
7823 +               AuDbg("err %d\n", (int)(e)); \
7824 +} while (0)
7825 +
7826 +#define AuTraceErrPtr(p) do { \
7827 +       if (IS_ERR(p)) \
7828 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7829 +} while (0)
7830 +
7831 +/* dirty macros for debug print, use with "%.*s" and caution */
7832 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7833 +
7834 +/* ---------------------------------------------------------------------- */
7835 +
7836 +struct dentry;
7837 +#ifdef CONFIG_AUFS_DEBUG
7838 +extern struct mutex au_dbg_mtx;
7839 +extern char *au_plevel;
7840 +struct au_nhash;
7841 +void au_dpri_whlist(struct au_nhash *whlist);
7842 +struct au_vdir;
7843 +void au_dpri_vdir(struct au_vdir *vdir);
7844 +struct inode;
7845 +void au_dpri_inode(struct inode *inode);
7846 +void au_dpri_dalias(struct inode *inode);
7847 +void au_dpri_dentry(struct dentry *dentry);
7848 +struct file;
7849 +void au_dpri_file(struct file *filp);
7850 +struct super_block;
7851 +void au_dpri_sb(struct super_block *sb);
7852 +
7853 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7854 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7855 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7856 +void au_dbg_verify_kthread(void);
7857 +
7858 +int __init au_debug_init(void);
7859 +
7860 +#define AuDbgWhlist(w) do { \
7861 +       mutex_lock(&au_dbg_mtx); \
7862 +       AuDbg(#w "\n"); \
7863 +       au_dpri_whlist(w); \
7864 +       mutex_unlock(&au_dbg_mtx); \
7865 +} while (0)
7866 +
7867 +#define AuDbgVdir(v) do { \
7868 +       mutex_lock(&au_dbg_mtx); \
7869 +       AuDbg(#v "\n"); \
7870 +       au_dpri_vdir(v); \
7871 +       mutex_unlock(&au_dbg_mtx); \
7872 +} while (0)
7873 +
7874 +#define AuDbgInode(i) do { \
7875 +       mutex_lock(&au_dbg_mtx); \
7876 +       AuDbg(#i "\n"); \
7877 +       au_dpri_inode(i); \
7878 +       mutex_unlock(&au_dbg_mtx); \
7879 +} while (0)
7880 +
7881 +#define AuDbgDAlias(i) do { \
7882 +       mutex_lock(&au_dbg_mtx); \
7883 +       AuDbg(#i "\n"); \
7884 +       au_dpri_dalias(i); \
7885 +       mutex_unlock(&au_dbg_mtx); \
7886 +} while (0)
7887 +
7888 +#define AuDbgDentry(d) do { \
7889 +       mutex_lock(&au_dbg_mtx); \
7890 +       AuDbg(#d "\n"); \
7891 +       au_dpri_dentry(d); \
7892 +       mutex_unlock(&au_dbg_mtx); \
7893 +} while (0)
7894 +
7895 +#define AuDbgFile(f) do { \
7896 +       mutex_lock(&au_dbg_mtx); \
7897 +       AuDbg(#f "\n"); \
7898 +       au_dpri_file(f); \
7899 +       mutex_unlock(&au_dbg_mtx); \
7900 +} while (0)
7901 +
7902 +#define AuDbgSb(sb) do { \
7903 +       mutex_lock(&au_dbg_mtx); \
7904 +       AuDbg(#sb "\n"); \
7905 +       au_dpri_sb(sb); \
7906 +       mutex_unlock(&au_dbg_mtx); \
7907 +} while (0)
7908 +
7909 +#define AuDbgSym(addr) do {                            \
7910 +       char sym[KSYM_SYMBOL_LEN];                      \
7911 +       sprint_symbol(sym, (unsigned long)addr);        \
7912 +       AuDbg("%s\n", sym);                             \
7913 +} while (0)
7914 +#else
7915 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7916 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7917 +AuStubVoid(au_dbg_verify_kthread, void)
7918 +AuStubInt0(__init au_debug_init, void)
7919 +
7920 +#define AuDbgWhlist(w)         do {} while (0)
7921 +#define AuDbgVdir(v)           do {} while (0)
7922 +#define AuDbgInode(i)          do {} while (0)
7923 +#define AuDbgDAlias(i)         do {} while (0)
7924 +#define AuDbgDentry(d)         do {} while (0)
7925 +#define AuDbgFile(f)           do {} while (0)
7926 +#define AuDbgSb(sb)            do {} while (0)
7927 +#define AuDbgSym(addr)         do {} while (0)
7928 +#endif /* CONFIG_AUFS_DEBUG */
7929 +
7930 +/* ---------------------------------------------------------------------- */
7931 +
7932 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7933 +int __init au_sysrq_init(void);
7934 +void au_sysrq_fin(void);
7935 +
7936 +#ifdef CONFIG_HW_CONSOLE
7937 +#define au_dbg_blocked() do { \
7938 +       WARN_ON(1); \
7939 +       handle_sysrq('w'); \
7940 +} while (0)
7941 +#else
7942 +AuStubVoid(au_dbg_blocked, void)
7943 +#endif
7944 +
7945 +#else
7946 +AuStubInt0(__init au_sysrq_init, void)
7947 +AuStubVoid(au_sysrq_fin, void)
7948 +AuStubVoid(au_dbg_blocked, void)
7949 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7950 +
7951 +#endif /* __KERNEL__ */
7952 +#endif /* __AUFS_DEBUG_H__ */
7953 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7954 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7955 +++ linux/fs/aufs/dentry.c      2022-08-02 12:12:45.162308621 +0200
7956 @@ -0,0 +1,1168 @@
7957 +// SPDX-License-Identifier: GPL-2.0
7958 +/*
7959 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7960 + *
7961 + * This program, aufs is free software; you can redistribute it and/or modify
7962 + * it under the terms of the GNU General Public License as published by
7963 + * the Free Software Foundation; either version 2 of the License, or
7964 + * (at your option) any later version.
7965 + *
7966 + * This program is distributed in the hope that it will be useful,
7967 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7968 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7969 + * GNU General Public License for more details.
7970 + *
7971 + * You should have received a copy of the GNU General Public License
7972 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7973 + */
7974 +
7975 +/*
7976 + * lookup and dentry operations
7977 + */
7978 +
7979 +#include <linux/iversion.h>
7980 +#include "aufs.h"
7981 +
7982 +/*
7983 + * returns positive/negative dentry, NULL or an error.
7984 + * NULL means whiteout-ed or not-found.
7985 + */
7986 +static struct dentry*
7987 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7988 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7989 +{
7990 +       struct dentry *h_dentry;
7991 +       struct inode *h_inode;
7992 +       struct au_branch *br;
7993 +       struct user_namespace *h_userns;
7994 +       struct path h_path;
7995 +       int wh_found, opq;
7996 +       unsigned char wh_able;
7997 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7998 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7999 +                                                         IGNORE_PERM);
8000 +
8001 +       wh_found = 0;
8002 +       br = au_sbr(dentry->d_sb, bindex);
8003 +       h_path.dentry = h_parent;
8004 +       h_path.mnt = au_br_mnt(br);
8005 +       h_userns = au_br_userns(br);
8006 +       wh_able = !!au_br_whable(br->br_perm);
8007 +       if (wh_able)
8008 +               wh_found = au_wh_test(h_userns, &h_path, &args->whname,
8009 +                                     ignore_perm);
8010 +       h_dentry = ERR_PTR(wh_found);
8011 +       if (!wh_found)
8012 +               goto real_lookup;
8013 +       if (unlikely(wh_found < 0))
8014 +               goto out;
8015 +
8016 +       /* We found a whiteout */
8017 +       /* au_set_dbbot(dentry, bindex); */
8018 +       au_set_dbwh(dentry, bindex);
8019 +       if (!allow_neg)
8020 +               return NULL; /* success */
8021 +
8022 +real_lookup:
8023 +       if (!ignore_perm)
8024 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8025 +       else
8026 +               h_dentry = au_sio_lkup_one(h_userns, args->name, &h_path);
8027 +       if (IS_ERR(h_dentry)) {
8028 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8029 +                   && !allow_neg)
8030 +                       h_dentry = NULL;
8031 +               goto out;
8032 +       }
8033 +
8034 +       h_inode = d_inode(h_dentry);
8035 +       if (d_is_negative(h_dentry)) {
8036 +               if (!allow_neg)
8037 +                       goto out_neg;
8038 +       } else if (wh_found
8039 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8040 +               goto out_neg;
8041 +       else if (au_ftest_lkup(args->flags, DIRREN)
8042 +                /* && h_inode */
8043 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8044 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8045 +                     (unsigned long long)h_inode->i_ino);
8046 +               goto out_neg;
8047 +       }
8048 +
8049 +       if (au_dbbot(dentry) <= bindex)
8050 +               au_set_dbbot(dentry, bindex);
8051 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8052 +               au_set_dbtop(dentry, bindex);
8053 +       au_set_h_dptr(dentry, bindex, h_dentry);
8054 +
8055 +       if (!d_is_dir(h_dentry)
8056 +           || !wh_able
8057 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8058 +               goto out; /* success */
8059 +
8060 +       h_path.dentry = h_dentry;
8061 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8062 +       opq = au_diropq_test(h_userns, &h_path);
8063 +       inode_unlock_shared(h_inode);
8064 +       if (opq > 0)
8065 +               au_set_dbdiropq(dentry, bindex);
8066 +       else if (unlikely(opq < 0)) {
8067 +               au_set_h_dptr(dentry, bindex, NULL);
8068 +               h_dentry = ERR_PTR(opq);
8069 +       }
8070 +       goto out;
8071 +
8072 +out_neg:
8073 +       dput(h_dentry);
8074 +       h_dentry = NULL;
8075 +out:
8076 +       return h_dentry;
8077 +}
8078 +
8079 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8080 +{
8081 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8082 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8083 +               return -EPERM;
8084 +       return 0;
8085 +}
8086 +
8087 +/*
8088 + * returns the number of lower positive dentries,
8089 + * otherwise an error.
8090 + * can be called at unlinking with @type is zero.
8091 + */
8092 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8093 +                  unsigned int flags)
8094 +{
8095 +       int npositive, err;
8096 +       aufs_bindex_t bindex, btail, bdiropq;
8097 +       unsigned char isdir, dirperm1, dirren;
8098 +       struct au_do_lookup_args args = {
8099 +               .flags          = flags,
8100 +               .name           = &dentry->d_name
8101 +       };
8102 +       struct dentry *parent;
8103 +       struct super_block *sb;
8104 +
8105 +       sb = dentry->d_sb;
8106 +       err = au_test_shwh(sb, args.name);
8107 +       if (unlikely(err))
8108 +               goto out;
8109 +
8110 +       err = au_wh_name_alloc(&args.whname, args.name);
8111 +       if (unlikely(err))
8112 +               goto out;
8113 +
8114 +       isdir = !!d_is_dir(dentry);
8115 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8116 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8117 +       if (dirren)
8118 +               au_fset_lkup(args.flags, DIRREN);
8119 +
8120 +       npositive = 0;
8121 +       parent = dget_parent(dentry);
8122 +       btail = au_dbtaildir(parent);
8123 +       for (bindex = btop; bindex <= btail; bindex++) {
8124 +               struct dentry *h_parent, *h_dentry;
8125 +               struct inode *h_inode, *h_dir;
8126 +               struct au_branch *br;
8127 +
8128 +               h_dentry = au_h_dptr(dentry, bindex);
8129 +               if (h_dentry) {
8130 +                       if (d_is_positive(h_dentry))
8131 +                               npositive++;
8132 +                       break;
8133 +               }
8134 +               h_parent = au_h_dptr(parent, bindex);
8135 +               if (!h_parent || !d_is_dir(h_parent))
8136 +                       continue;
8137 +
8138 +               if (dirren) {
8139 +                       /* if the inum matches, then use the prepared name */
8140 +                       err = au_dr_lkup_name(&args, bindex);
8141 +                       if (unlikely(err))
8142 +                               goto out_parent;
8143 +               }
8144 +
8145 +               h_dir = d_inode(h_parent);
8146 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8147 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8148 +               inode_unlock_shared(h_dir);
8149 +               err = PTR_ERR(h_dentry);
8150 +               if (IS_ERR(h_dentry))
8151 +                       goto out_parent;
8152 +               if (h_dentry)
8153 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8154 +               if (dirperm1)
8155 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8156 +
8157 +               if (au_dbwh(dentry) == bindex)
8158 +                       break;
8159 +               if (!h_dentry)
8160 +                       continue;
8161 +               if (d_is_negative(h_dentry))
8162 +                       continue;
8163 +               h_inode = d_inode(h_dentry);
8164 +               npositive++;
8165 +               if (!args.type)
8166 +                       args.type = h_inode->i_mode & S_IFMT;
8167 +               if (args.type != S_IFDIR)
8168 +                       break;
8169 +               else if (isdir) {
8170 +                       /* the type of lower may be different */
8171 +                       bdiropq = au_dbdiropq(dentry);
8172 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8173 +                               break;
8174 +               }
8175 +               br = au_sbr(sb, bindex);
8176 +               if (dirren
8177 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8178 +                                          /*add_ent*/NULL)) {
8179 +                       /* prepare next name to lookup */
8180 +                       err = au_dr_lkup(&args, dentry, bindex);
8181 +                       if (unlikely(err))
8182 +                               goto out_parent;
8183 +               }
8184 +       }
8185 +
8186 +       if (npositive) {
8187 +               AuLabel(positive);
8188 +               au_update_dbtop(dentry);
8189 +       }
8190 +       err = npositive;
8191 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8192 +                    && au_dbtop(dentry) < 0)) {
8193 +               err = -EIO;
8194 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8195 +                       dentry, err);
8196 +       }
8197 +
8198 +out_parent:
8199 +       dput(parent);
8200 +       au_kfree_try_rcu(args.whname.name);
8201 +       if (dirren)
8202 +               au_dr_lkup_fin(&args);
8203 +out:
8204 +       return err;
8205 +}
8206 +
8207 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
8208 +                              struct path *ppath)
8209 +{
8210 +       struct dentry *dentry;
8211 +       int wkq_err;
8212 +
8213 +       if (!au_test_h_perm_sio(userns, d_inode(ppath->dentry), MAY_EXEC))
8214 +               dentry = vfsub_lkup_one(name, ppath);
8215 +       else {
8216 +               struct vfsub_lkup_one_args args = {
8217 +                       .errp   = &dentry,
8218 +                       .name   = name,
8219 +                       .ppath  = ppath
8220 +               };
8221 +
8222 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8223 +               if (unlikely(wkq_err))
8224 +                       dentry = ERR_PTR(wkq_err);
8225 +       }
8226 +
8227 +       return dentry;
8228 +}
8229 +
8230 +/*
8231 + * lookup @dentry on @bindex which should be negative.
8232 + */
8233 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8234 +{
8235 +       int err;
8236 +       struct dentry *parent, *h_dentry;
8237 +       struct au_branch *br;
8238 +       struct user_namespace *h_userns;
8239 +       struct path h_ppath;
8240 +
8241 +       parent = dget_parent(dentry);
8242 +       br = au_sbr(dentry->d_sb, bindex);
8243 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8244 +       h_ppath.mnt = au_br_mnt(br);
8245 +       h_userns = au_br_userns(br);
8246 +       if (wh)
8247 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8248 +       else
8249 +               h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
8250 +       err = PTR_ERR(h_dentry);
8251 +       if (IS_ERR(h_dentry))
8252 +               goto out;
8253 +       if (unlikely(d_is_positive(h_dentry))) {
8254 +               err = -EIO;
8255 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8256 +               dput(h_dentry);
8257 +               goto out;
8258 +       }
8259 +
8260 +       err = 0;
8261 +       if (bindex < au_dbtop(dentry))
8262 +               au_set_dbtop(dentry, bindex);
8263 +       if (au_dbbot(dentry) < bindex)
8264 +               au_set_dbbot(dentry, bindex);
8265 +       au_set_h_dptr(dentry, bindex, h_dentry);
8266 +
8267 +out:
8268 +       dput(parent);
8269 +       return err;
8270 +}
8271 +
8272 +/* ---------------------------------------------------------------------- */
8273 +
8274 +/* subset of struct inode */
8275 +struct au_iattr {
8276 +       unsigned long           i_ino;
8277 +       /* unsigned int         i_nlink; */
8278 +       kuid_t                  i_uid;
8279 +       kgid_t                  i_gid;
8280 +       u64                     i_version;
8281 +/*
8282 +       loff_t                  i_size;
8283 +       blkcnt_t                i_blocks;
8284 +*/
8285 +       umode_t                 i_mode;
8286 +};
8287 +
8288 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8289 +{
8290 +       ia->i_ino = h_inode->i_ino;
8291 +       /* ia->i_nlink = h_inode->i_nlink; */
8292 +       ia->i_uid = h_inode->i_uid;
8293 +       ia->i_gid = h_inode->i_gid;
8294 +       ia->i_version = inode_query_iversion(h_inode);
8295 +/*
8296 +       ia->i_size = h_inode->i_size;
8297 +       ia->i_blocks = h_inode->i_blocks;
8298 +*/
8299 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8300 +}
8301 +
8302 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8303 +{
8304 +       return ia->i_ino != h_inode->i_ino
8305 +               /* || ia->i_nlink != h_inode->i_nlink */
8306 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8307 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8308 +               || !inode_eq_iversion(h_inode, ia->i_version)
8309 +/*
8310 +               || ia->i_size != h_inode->i_size
8311 +               || ia->i_blocks != h_inode->i_blocks
8312 +*/
8313 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8314 +}
8315 +
8316 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8317 +                             struct au_branch *br)
8318 +{
8319 +       int err;
8320 +       struct au_iattr ia;
8321 +       struct inode *h_inode;
8322 +       struct dentry *h_d;
8323 +       struct super_block *h_sb;
8324 +       struct path h_ppath;
8325 +
8326 +       err = 0;
8327 +       memset(&ia, -1, sizeof(ia));
8328 +       h_sb = h_dentry->d_sb;
8329 +       h_inode = NULL;
8330 +       if (d_is_positive(h_dentry)) {
8331 +               h_inode = d_inode(h_dentry);
8332 +               au_iattr_save(&ia, h_inode);
8333 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8334 +               /* nfs d_revalidate may return 0 for negative dentry */
8335 +               /* fuse d_revalidate always return 0 for negative dentry */
8336 +               goto out;
8337 +
8338 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8339 +       h_ppath.dentry = h_parent;
8340 +       h_ppath.mnt = au_br_mnt(br);
8341 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8342 +       err = PTR_ERR(h_d);
8343 +       if (IS_ERR(h_d))
8344 +               goto out;
8345 +
8346 +       err = 0;
8347 +       if (unlikely(h_d != h_dentry
8348 +                    || d_inode(h_d) != h_inode
8349 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8350 +               err = au_busy_or_stale();
8351 +       dput(h_d);
8352 +
8353 +out:
8354 +       AuTraceErr(err);
8355 +       return err;
8356 +}
8357 +
8358 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8359 +               struct dentry *h_parent, struct au_branch *br)
8360 +{
8361 +       int err;
8362 +
8363 +       err = 0;
8364 +       if (udba == AuOpt_UDBA_REVAL
8365 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8366 +               IMustLock(h_dir);
8367 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8368 +       } else if (udba != AuOpt_UDBA_NONE)
8369 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8370 +
8371 +       return err;
8372 +}
8373 +
8374 +/* ---------------------------------------------------------------------- */
8375 +
8376 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8377 +{
8378 +       int err;
8379 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8380 +       struct au_hdentry tmp, *p, *q;
8381 +       struct au_dinfo *dinfo;
8382 +       struct super_block *sb;
8383 +
8384 +       DiMustWriteLock(dentry);
8385 +
8386 +       sb = dentry->d_sb;
8387 +       dinfo = au_di(dentry);
8388 +       bbot = dinfo->di_bbot;
8389 +       bwh = dinfo->di_bwh;
8390 +       bdiropq = dinfo->di_bdiropq;
8391 +       bindex = dinfo->di_btop;
8392 +       p = au_hdentry(dinfo, bindex);
8393 +       for (; bindex <= bbot; bindex++, p++) {
8394 +               if (!p->hd_dentry)
8395 +                       continue;
8396 +
8397 +               new_bindex = au_br_index(sb, p->hd_id);
8398 +               if (new_bindex == bindex)
8399 +                       continue;
8400 +
8401 +               if (dinfo->di_bwh == bindex)
8402 +                       bwh = new_bindex;
8403 +               if (dinfo->di_bdiropq == bindex)
8404 +                       bdiropq = new_bindex;
8405 +               if (new_bindex < 0) {
8406 +                       au_hdput(p);
8407 +                       p->hd_dentry = NULL;
8408 +                       continue;
8409 +               }
8410 +
8411 +               /* swap two lower dentries, and loop again */
8412 +               q = au_hdentry(dinfo, new_bindex);
8413 +               tmp = *q;
8414 +               *q = *p;
8415 +               *p = tmp;
8416 +               if (tmp.hd_dentry) {
8417 +                       bindex--;
8418 +                       p--;
8419 +               }
8420 +       }
8421 +
8422 +       dinfo->di_bwh = -1;
8423 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8424 +               dinfo->di_bwh = bwh;
8425 +
8426 +       dinfo->di_bdiropq = -1;
8427 +       if (bdiropq >= 0
8428 +           && bdiropq <= au_sbbot(sb)
8429 +           && au_sbr_whable(sb, bdiropq))
8430 +               dinfo->di_bdiropq = bdiropq;
8431 +
8432 +       err = -EIO;
8433 +       dinfo->di_btop = -1;
8434 +       dinfo->di_bbot = -1;
8435 +       bbot = au_dbbot(parent);
8436 +       bindex = 0;
8437 +       p = au_hdentry(dinfo, bindex);
8438 +       for (; bindex <= bbot; bindex++, p++)
8439 +               if (p->hd_dentry) {
8440 +                       dinfo->di_btop = bindex;
8441 +                       break;
8442 +               }
8443 +
8444 +       if (dinfo->di_btop >= 0) {
8445 +               bindex = bbot;
8446 +               p = au_hdentry(dinfo, bindex);
8447 +               for (; bindex >= 0; bindex--, p--)
8448 +                       if (p->hd_dentry) {
8449 +                               dinfo->di_bbot = bindex;
8450 +                               err = 0;
8451 +                               break;
8452 +                       }
8453 +       }
8454 +
8455 +       return err;
8456 +}
8457 +
8458 +static void au_do_hide(struct dentry *dentry)
8459 +{
8460 +       struct inode *inode;
8461 +
8462 +       if (d_really_is_positive(dentry)) {
8463 +               inode = d_inode(dentry);
8464 +               if (!d_is_dir(dentry)) {
8465 +                       if (inode->i_nlink && !d_unhashed(dentry))
8466 +                               drop_nlink(inode);
8467 +               } else {
8468 +                       clear_nlink(inode);
8469 +                       /* stop next lookup */
8470 +                       inode->i_flags |= S_DEAD;
8471 +               }
8472 +               smp_mb(); /* necessary? */
8473 +       }
8474 +       d_drop(dentry);
8475 +}
8476 +
8477 +static int au_hide_children(struct dentry *parent)
8478 +{
8479 +       int err, i, j, ndentry;
8480 +       struct au_dcsub_pages dpages;
8481 +       struct au_dpage *dpage;
8482 +       struct dentry *dentry;
8483 +
8484 +       err = au_dpages_init(&dpages, GFP_NOFS);
8485 +       if (unlikely(err))
8486 +               goto out;
8487 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8488 +       if (unlikely(err))
8489 +               goto out_dpages;
8490 +
8491 +       /* in reverse order */
8492 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8493 +               dpage = dpages.dpages + i;
8494 +               ndentry = dpage->ndentry;
8495 +               for (j = ndentry - 1; j >= 0; j--) {
8496 +                       dentry = dpage->dentries[j];
8497 +                       if (dentry != parent)
8498 +                               au_do_hide(dentry);
8499 +               }
8500 +       }
8501 +
8502 +out_dpages:
8503 +       au_dpages_free(&dpages);
8504 +out:
8505 +       return err;
8506 +}
8507 +
8508 +static void au_hide(struct dentry *dentry)
8509 +{
8510 +       int err;
8511 +
8512 +       AuDbgDentry(dentry);
8513 +       if (d_is_dir(dentry)) {
8514 +               /* shrink_dcache_parent(dentry); */
8515 +               err = au_hide_children(dentry);
8516 +               if (unlikely(err))
8517 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8518 +                               dentry, err);
8519 +       }
8520 +       au_do_hide(dentry);
8521 +}
8522 +
8523 +/*
8524 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8525 + *
8526 + * a dirty branch is added
8527 + * - on the top of layers
8528 + * - in the middle of layers
8529 + * - to the bottom of layers
8530 + *
8531 + * on the added branch there exists
8532 + * - a whiteout
8533 + * - a diropq
8534 + * - a same named entry
8535 + *   + exist
8536 + *     * negative --> positive
8537 + *     * positive --> positive
8538 + *      - type is unchanged
8539 + *      - type is changed
8540 + *   + doesn't exist
8541 + *     * negative --> negative
8542 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8543 + * - none
8544 + */
8545 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8546 +                              struct au_dinfo *tmp)
8547 +{
8548 +       int err;
8549 +       aufs_bindex_t bindex, bbot;
8550 +       struct {
8551 +               struct dentry *dentry;
8552 +               struct inode *inode;
8553 +               mode_t mode;
8554 +       } orig_h, tmp_h = {
8555 +               .dentry = NULL
8556 +       };
8557 +       struct au_hdentry *hd;
8558 +       struct inode *inode, *h_inode;
8559 +       struct dentry *h_dentry;
8560 +
8561 +       err = 0;
8562 +       AuDebugOn(dinfo->di_btop < 0);
8563 +       orig_h.mode = 0;
8564 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8565 +       orig_h.inode = NULL;
8566 +       if (d_is_positive(orig_h.dentry)) {
8567 +               orig_h.inode = d_inode(orig_h.dentry);
8568 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8569 +       }
8570 +       if (tmp->di_btop >= 0) {
8571 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8572 +               if (d_is_positive(tmp_h.dentry)) {
8573 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8574 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8575 +               }
8576 +       }
8577 +
8578 +       inode = NULL;
8579 +       if (d_really_is_positive(dentry))
8580 +               inode = d_inode(dentry);
8581 +       if (!orig_h.inode) {
8582 +               AuDbg("negative originally\n");
8583 +               if (inode) {
8584 +                       au_hide(dentry);
8585 +                       goto out;
8586 +               }
8587 +               AuDebugOn(inode);
8588 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8589 +               AuDebugOn(dinfo->di_bdiropq != -1);
8590 +
8591 +               if (!tmp_h.inode) {
8592 +                       AuDbg("negative --> negative\n");
8593 +                       /* should have only one negative lower */
8594 +                       if (tmp->di_btop >= 0
8595 +                           && tmp->di_btop < dinfo->di_btop) {
8596 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8597 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8598 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8599 +                               au_di_cp(dinfo, tmp);
8600 +                               hd = au_hdentry(tmp, tmp->di_btop);
8601 +                               au_set_h_dptr(dentry, tmp->di_btop,
8602 +                                             dget(hd->hd_dentry));
8603 +                       }
8604 +                       au_dbg_verify_dinode(dentry);
8605 +               } else {
8606 +                       AuDbg("negative --> positive\n");
8607 +                       /*
8608 +                        * similar to the behaviour of creating with bypassing
8609 +                        * aufs.
8610 +                        * unhash it in order to force an error in the
8611 +                        * succeeding create operation.
8612 +                        * we should not set S_DEAD here.
8613 +                        */
8614 +                       d_drop(dentry);
8615 +                       /* au_di_swap(tmp, dinfo); */
8616 +                       au_dbg_verify_dinode(dentry);
8617 +               }
8618 +       } else {
8619 +               AuDbg("positive originally\n");
8620 +               /* inode may be NULL */
8621 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8622 +               if (!tmp_h.inode) {
8623 +                       AuDbg("positive --> negative\n");
8624 +                       /* or bypassing aufs */
8625 +                       au_hide(dentry);
8626 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8627 +                               dinfo->di_bwh = tmp->di_bwh;
8628 +                       if (inode)
8629 +                               err = au_refresh_hinode_self(inode);
8630 +                       au_dbg_verify_dinode(dentry);
8631 +               } else if (orig_h.mode == tmp_h.mode) {
8632 +                       AuDbg("positive --> positive, same type\n");
8633 +                       if (!S_ISDIR(orig_h.mode)
8634 +                           && dinfo->di_btop > tmp->di_btop) {
8635 +                               /*
8636 +                                * similar to the behaviour of removing and
8637 +                                * creating.
8638 +                                */
8639 +                               au_hide(dentry);
8640 +                               if (inode)
8641 +                                       err = au_refresh_hinode_self(inode);
8642 +                               au_dbg_verify_dinode(dentry);
8643 +                       } else {
8644 +                               /* fill empty slots */
8645 +                               if (dinfo->di_btop > tmp->di_btop)
8646 +                                       dinfo->di_btop = tmp->di_btop;
8647 +                               if (dinfo->di_bbot < tmp->di_bbot)
8648 +                                       dinfo->di_bbot = tmp->di_bbot;
8649 +                               dinfo->di_bwh = tmp->di_bwh;
8650 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8651 +                               bbot = dinfo->di_bbot;
8652 +                               bindex = tmp->di_btop;
8653 +                               hd = au_hdentry(tmp, bindex);
8654 +                               for (; bindex <= bbot; bindex++, hd++) {
8655 +                                       if (au_h_dptr(dentry, bindex))
8656 +                                               continue;
8657 +                                       h_dentry = hd->hd_dentry;
8658 +                                       if (!h_dentry)
8659 +                                               continue;
8660 +                                       AuDebugOn(d_is_negative(h_dentry));
8661 +                                       h_inode = d_inode(h_dentry);
8662 +                                       AuDebugOn(orig_h.mode
8663 +                                                 != (h_inode->i_mode
8664 +                                                     & S_IFMT));
8665 +                                       au_set_h_dptr(dentry, bindex,
8666 +                                                     dget(h_dentry));
8667 +                               }
8668 +                               if (inode)
8669 +                                       err = au_refresh_hinode(inode, dentry);
8670 +                               au_dbg_verify_dinode(dentry);
8671 +                       }
8672 +               } else {
8673 +                       AuDbg("positive --> positive, different type\n");
8674 +                       /* similar to the behaviour of removing and creating */
8675 +                       au_hide(dentry);
8676 +                       if (inode)
8677 +                               err = au_refresh_hinode_self(inode);
8678 +                       au_dbg_verify_dinode(dentry);
8679 +               }
8680 +       }
8681 +
8682 +out:
8683 +       return err;
8684 +}
8685 +
8686 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8687 +{
8688 +       const struct dentry_operations *dop
8689 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8690 +       static const unsigned int mask
8691 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8692 +
8693 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8694 +
8695 +       if (dentry->d_op == dop)
8696 +               return;
8697 +
8698 +       AuDbg("%pd\n", dentry);
8699 +       spin_lock(&dentry->d_lock);
8700 +       if (dop == &aufs_dop)
8701 +               dentry->d_flags |= mask;
8702 +       else
8703 +               dentry->d_flags &= ~mask;
8704 +       dentry->d_op = dop;
8705 +       spin_unlock(&dentry->d_lock);
8706 +}
8707 +
8708 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8709 +{
8710 +       int err, ebrange, nbr;
8711 +       unsigned int sigen;
8712 +       struct au_dinfo *dinfo, *tmp;
8713 +       struct super_block *sb;
8714 +       struct inode *inode;
8715 +
8716 +       DiMustWriteLock(dentry);
8717 +       AuDebugOn(IS_ROOT(dentry));
8718 +       AuDebugOn(d_really_is_negative(parent));
8719 +
8720 +       sb = dentry->d_sb;
8721 +       sigen = au_sigen(sb);
8722 +       err = au_digen_test(parent, sigen);
8723 +       if (unlikely(err))
8724 +               goto out;
8725 +
8726 +       nbr = au_sbbot(sb) + 1;
8727 +       dinfo = au_di(dentry);
8728 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8729 +       if (unlikely(err))
8730 +               goto out;
8731 +       ebrange = au_dbrange_test(dentry);
8732 +       if (!ebrange)
8733 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8734 +
8735 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8736 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8737 +               if (d_really_is_positive(dentry)) {
8738 +                       inode = d_inode(dentry);
8739 +                       err = au_refresh_hinode_self(inode);
8740 +               }
8741 +               au_dbg_verify_dinode(dentry);
8742 +               if (!err)
8743 +                       goto out_dgen; /* success */
8744 +               goto out;
8745 +       }
8746 +
8747 +       /* temporary dinfo */
8748 +       AuDbgDentry(dentry);
8749 +       err = -ENOMEM;
8750 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8751 +       if (unlikely(!tmp))
8752 +               goto out;
8753 +       au_di_swap(tmp, dinfo);
8754 +       /* returns the number of positive dentries */
8755 +       /*
8756 +        * if current working dir is removed, it returns an error.
8757 +        * but the dentry is legal.
8758 +        */
8759 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8760 +       AuDbgDentry(dentry);
8761 +       au_di_swap(tmp, dinfo);
8762 +       if (err == -ENOENT)
8763 +               err = 0;
8764 +       if (err >= 0) {
8765 +               /* compare/refresh by dinfo */
8766 +               AuDbgDentry(dentry);
8767 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8768 +               au_dbg_verify_dinode(dentry);
8769 +               AuTraceErr(err);
8770 +       }
8771 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8772 +       au_rw_write_unlock(&tmp->di_rwsem);
8773 +       au_di_free(tmp);
8774 +       if (unlikely(err))
8775 +               goto out;
8776 +
8777 +out_dgen:
8778 +       au_update_digen(dentry);
8779 +out:
8780 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8781 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8782 +               AuDbgDentry(dentry);
8783 +       }
8784 +       AuTraceErr(err);
8785 +       return err;
8786 +}
8787 +
8788 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8789 +                          struct dentry *dentry, aufs_bindex_t bindex)
8790 +{
8791 +       int err, valid;
8792 +
8793 +       err = 0;
8794 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8795 +               goto out;
8796 +
8797 +       AuDbg("b%d\n", bindex);
8798 +       /*
8799 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8800 +        * due to whiteout and branch permission.
8801 +        */
8802 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8803 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8804 +       /* it may return tri-state */
8805 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8806 +
8807 +       if (unlikely(valid < 0))
8808 +               err = valid;
8809 +       else if (!valid)
8810 +               err = -EINVAL;
8811 +
8812 +out:
8813 +       AuTraceErr(err);
8814 +       return err;
8815 +}
8816 +
8817 +/* todo: remove this */
8818 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8819 +                         unsigned int flags, int do_udba, int dirren)
8820 +{
8821 +       int err;
8822 +       umode_t mode, h_mode;
8823 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8824 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8825 +       struct inode *h_inode, *h_cached_inode;
8826 +       struct dentry *h_dentry;
8827 +       struct qstr *name, *h_name;
8828 +
8829 +       err = 0;
8830 +       plus = 0;
8831 +       mode = 0;
8832 +       ibs = -1;
8833 +       ibe = -1;
8834 +       unhashed = !!d_unhashed(dentry);
8835 +       is_root = !!IS_ROOT(dentry);
8836 +       name = &dentry->d_name;
8837 +       tmpfile = au_di(dentry)->di_tmpfile;
8838 +
8839 +       /*
8840 +        * Theoretically, REVAL test should be unnecessary in case of
8841 +        * {FS,I}NOTIFY.
8842 +        * But {fs,i}notify doesn't fire some necessary events,
8843 +        *      IN_ATTRIB for atime/nlink/pageio
8844 +        * Let's do REVAL test too.
8845 +        */
8846 +       if (do_udba && inode) {
8847 +               mode = (inode->i_mode & S_IFMT);
8848 +               plus = (inode->i_nlink > 0);
8849 +               ibs = au_ibtop(inode);
8850 +               ibe = au_ibbot(inode);
8851 +       }
8852 +
8853 +       btop = au_dbtop(dentry);
8854 +       btail = btop;
8855 +       if (inode && S_ISDIR(inode->i_mode))
8856 +               btail = au_dbtaildir(dentry);
8857 +       for (bindex = btop; bindex <= btail; bindex++) {
8858 +               h_dentry = au_h_dptr(dentry, bindex);
8859 +               if (!h_dentry)
8860 +                       continue;
8861 +
8862 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8863 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8864 +               spin_lock(&h_dentry->d_lock);
8865 +               h_name = &h_dentry->d_name;
8866 +               if (unlikely(do_udba
8867 +                            && !is_root
8868 +                            && ((!h_nfs
8869 +                                 && (unhashed != !!d_unhashed(h_dentry)
8870 +                                     || (!tmpfile && !dirren
8871 +                                         && !au_qstreq(name, h_name))
8872 +                                         ))
8873 +                                || (h_nfs
8874 +                                    && !(flags & LOOKUP_OPEN)
8875 +                                    && (h_dentry->d_flags
8876 +                                        & DCACHE_NFSFS_RENAMED)))
8877 +                           )) {
8878 +                       int h_unhashed;
8879 +
8880 +                       h_unhashed = d_unhashed(h_dentry);
8881 +                       spin_unlock(&h_dentry->d_lock);
8882 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8883 +                             unhashed, h_unhashed, dentry, h_dentry);
8884 +                       goto err;
8885 +               }
8886 +               spin_unlock(&h_dentry->d_lock);
8887 +
8888 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8889 +               if (unlikely(err))
8890 +                       /* do not goto err, to keep the errno */
8891 +                       break;
8892 +
8893 +               /* todo: plink too? */
8894 +               if (!do_udba)
8895 +                       continue;
8896 +
8897 +               /* UDBA tests */
8898 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8899 +                       goto err;
8900 +
8901 +               h_inode = NULL;
8902 +               if (d_is_positive(h_dentry))
8903 +                       h_inode = d_inode(h_dentry);
8904 +               h_plus = plus;
8905 +               h_mode = mode;
8906 +               h_cached_inode = h_inode;
8907 +               if (h_inode) {
8908 +                       h_mode = (h_inode->i_mode & S_IFMT);
8909 +                       h_plus = (h_inode->i_nlink > 0);
8910 +               }
8911 +               if (inode && ibs <= bindex && bindex <= ibe)
8912 +                       h_cached_inode = au_h_iptr(inode, bindex);
8913 +
8914 +               if (!h_nfs) {
8915 +                       if (unlikely(plus != h_plus && !tmpfile))
8916 +                               goto err;
8917 +               } else {
8918 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8919 +                                    && !is_root
8920 +                                    && !IS_ROOT(h_dentry)
8921 +                                    && unhashed != d_unhashed(h_dentry)))
8922 +                               goto err;
8923 +               }
8924 +               if (unlikely(mode != h_mode
8925 +                            || h_cached_inode != h_inode))
8926 +                       goto err;
8927 +               continue;
8928 +
8929 +err:
8930 +               err = -EINVAL;
8931 +               break;
8932 +       }
8933 +
8934 +       AuTraceErr(err);
8935 +       return err;
8936 +}
8937 +
8938 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8939 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8940 +{
8941 +       int err;
8942 +       struct dentry *parent;
8943 +
8944 +       if (!au_digen_test(dentry, sigen))
8945 +               return 0;
8946 +
8947 +       parent = dget_parent(dentry);
8948 +       di_read_lock_parent(parent, AuLock_IR);
8949 +       AuDebugOn(au_digen_test(parent, sigen));
8950 +       au_dbg_verify_gen(parent, sigen);
8951 +       err = au_refresh_dentry(dentry, parent);
8952 +       di_read_unlock(parent, AuLock_IR);
8953 +       dput(parent);
8954 +       AuTraceErr(err);
8955 +       return err;
8956 +}
8957 +
8958 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8959 +{
8960 +       int err;
8961 +       struct dentry *d, *parent;
8962 +
8963 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8964 +               return simple_reval_dpath(dentry, sigen);
8965 +
8966 +       /* slow loop, keep it simple and stupid */
8967 +       /* cf: au_cpup_dirs() */
8968 +       err = 0;
8969 +       parent = NULL;
8970 +       while (au_digen_test(dentry, sigen)) {
8971 +               d = dentry;
8972 +               while (1) {
8973 +                       dput(parent);
8974 +                       parent = dget_parent(d);
8975 +                       if (!au_digen_test(parent, sigen))
8976 +                               break;
8977 +                       d = parent;
8978 +               }
8979 +
8980 +               if (d != dentry)
8981 +                       di_write_lock_child2(d);
8982 +
8983 +               /* someone might update our dentry while we were sleeping */
8984 +               if (au_digen_test(d, sigen)) {
8985 +                       /*
8986 +                        * todo: consolidate with simple_reval_dpath(),
8987 +                        * do_refresh() and au_reval_for_attr().
8988 +                        */
8989 +                       di_read_lock_parent(parent, AuLock_IR);
8990 +                       err = au_refresh_dentry(d, parent);
8991 +                       di_read_unlock(parent, AuLock_IR);
8992 +               }
8993 +
8994 +               if (d != dentry)
8995 +                       di_write_unlock(d);
8996 +               dput(parent);
8997 +               if (unlikely(err))
8998 +                       break;
8999 +       }
9000 +
9001 +       return err;
9002 +}
9003 +
9004 +/*
9005 + * if valid returns 1, otherwise 0.
9006 + */
9007 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9008 +{
9009 +       int valid, err;
9010 +       unsigned int sigen;
9011 +       unsigned char do_udba, dirren;
9012 +       struct super_block *sb;
9013 +       struct inode *inode;
9014 +
9015 +       /* todo: support rcu-walk? */
9016 +       if (flags & LOOKUP_RCU)
9017 +               return -ECHILD;
9018 +
9019 +       valid = 0;
9020 +       if (unlikely(!au_di(dentry)))
9021 +               goto out;
9022 +
9023 +       valid = 1;
9024 +       sb = dentry->d_sb;
9025 +       /*
9026 +        * todo: very ugly
9027 +        * i_mutex of parent dir may be held,
9028 +        * but we should not return 'invalid' due to busy.
9029 +        */
9030 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9031 +       if (unlikely(err)) {
9032 +               valid = err;
9033 +               AuTraceErr(err);
9034 +               goto out;
9035 +       }
9036 +       inode = NULL;
9037 +       if (d_really_is_positive(dentry))
9038 +               inode = d_inode(dentry);
9039 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9040 +               err = -EINVAL;
9041 +               AuTraceErr(err);
9042 +               goto out_dgrade;
9043 +       }
9044 +       if (unlikely(au_dbrange_test(dentry))) {
9045 +               err = -EINVAL;
9046 +               AuTraceErr(err);
9047 +               goto out_dgrade;
9048 +       }
9049 +
9050 +       sigen = au_sigen(sb);
9051 +       if (au_digen_test(dentry, sigen)) {
9052 +               AuDebugOn(IS_ROOT(dentry));
9053 +               err = au_reval_dpath(dentry, sigen);
9054 +               if (unlikely(err)) {
9055 +                       AuTraceErr(err);
9056 +                       goto out_dgrade;
9057 +               }
9058 +       }
9059 +       di_downgrade_lock(dentry, AuLock_IR);
9060 +
9061 +       err = -EINVAL;
9062 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9063 +           && inode
9064 +           && !(inode->i_state && I_LINKABLE)
9065 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9066 +               AuTraceErr(err);
9067 +               goto out_inval;
9068 +       }
9069 +
9070 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9071 +       if (do_udba && inode) {
9072 +               aufs_bindex_t btop = au_ibtop(inode);
9073 +               struct inode *h_inode;
9074 +
9075 +               if (btop >= 0) {
9076 +                       h_inode = au_h_iptr(inode, btop);
9077 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9078 +                               AuTraceErr(err);
9079 +                               goto out_inval;
9080 +                       }
9081 +               }
9082 +       }
9083 +
9084 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9085 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9086 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9087 +               err = -EIO;
9088 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9089 +                     dentry, err);
9090 +       }
9091 +       goto out_inval;
9092 +
9093 +out_dgrade:
9094 +       di_downgrade_lock(dentry, AuLock_IR);
9095 +out_inval:
9096 +       aufs_read_unlock(dentry, AuLock_IR);
9097 +       AuTraceErr(err);
9098 +       valid = !err;
9099 +out:
9100 +       if (!valid) {
9101 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9102 +               d_drop(dentry);
9103 +       }
9104 +       return valid;
9105 +}
9106 +
9107 +static void aufs_d_release(struct dentry *dentry)
9108 +{
9109 +       if (au_di(dentry)) {
9110 +               au_di_fin(dentry);
9111 +               au_hn_di_reinit(dentry);
9112 +       }
9113 +}
9114 +
9115 +const struct dentry_operations aufs_dop = {
9116 +       .d_revalidate           = aufs_d_revalidate,
9117 +       .d_weak_revalidate      = aufs_d_revalidate,
9118 +       .d_release              = aufs_d_release
9119 +};
9120 +
9121 +/* aufs_dop without d_revalidate */
9122 +const struct dentry_operations aufs_dop_noreval = {
9123 +       .d_release              = aufs_d_release
9124 +};
9125 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9126 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9127 +++ linux/fs/aufs/dentry.h      2022-08-02 12:12:45.162308621 +0200
9128 @@ -0,0 +1,269 @@
9129 +/* SPDX-License-Identifier: GPL-2.0 */
9130 +/*
9131 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9132 + *
9133 + * This program, aufs is free software; you can redistribute it and/or modify
9134 + * it under the terms of the GNU General Public License as published by
9135 + * the Free Software Foundation; either version 2 of the License, or
9136 + * (at your option) any later version.
9137 + *
9138 + * This program is distributed in the hope that it will be useful,
9139 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9140 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9141 + * GNU General Public License for more details.
9142 + *
9143 + * You should have received a copy of the GNU General Public License
9144 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9145 + */
9146 +
9147 +/*
9148 + * lookup and dentry operations
9149 + */
9150 +
9151 +#ifndef __AUFS_DENTRY_H__
9152 +#define __AUFS_DENTRY_H__
9153 +
9154 +#ifdef __KERNEL__
9155 +
9156 +#include <linux/dcache.h>
9157 +#include "dirren.h"
9158 +#include "rwsem.h"
9159 +
9160 +struct au_hdentry {
9161 +       struct dentry           *hd_dentry;
9162 +       aufs_bindex_t           hd_id;
9163 +};
9164 +
9165 +struct au_dinfo {
9166 +       atomic_t                di_generation;
9167 +
9168 +       struct au_rwsem         di_rwsem;
9169 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9170 +       unsigned char           di_tmpfile; /* to allow the different name */
9171 +       struct au_hdentry       *di_hdentry;
9172 +       struct rcu_head         rcu;
9173 +} ____cacheline_aligned_in_smp;
9174 +
9175 +/* ---------------------------------------------------------------------- */
9176 +
9177 +/* flags for au_lkup_dentry() */
9178 +#define AuLkup_ALLOW_NEG       1
9179 +#define AuLkup_IGNORE_PERM     (1 << 1)
9180 +#define AuLkup_DIRREN          (1 << 2)
9181 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9182 +#define au_fset_lkup(flags, name) \
9183 +       do { (flags) |= AuLkup_##name; } while (0)
9184 +#define au_fclr_lkup(flags, name) \
9185 +       do { (flags) &= ~AuLkup_##name; } while (0)
9186 +
9187 +#ifndef CONFIG_AUFS_DIRREN
9188 +#undef AuLkup_DIRREN
9189 +#define AuLkup_DIRREN 0
9190 +#endif
9191 +
9192 +struct au_do_lookup_args {
9193 +       unsigned int            flags;
9194 +       mode_t                  type;
9195 +       struct qstr             whname, *name;
9196 +       struct au_dr_lookup     dirren;
9197 +};
9198 +
9199 +/* ---------------------------------------------------------------------- */
9200 +
9201 +/* dentry.c */
9202 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9203 +struct au_branch;
9204 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
9205 +                              struct path *ppath);
9206 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9207 +               struct dentry *h_parent, struct au_branch *br);
9208 +
9209 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9210 +                  unsigned int flags);
9211 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9212 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9213 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9214 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9215 +
9216 +/* dinfo.c */
9217 +void au_di_init_once(void *_di);
9218 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9219 +void au_di_free(struct au_dinfo *dinfo);
9220 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9221 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9222 +int au_di_init(struct dentry *dentry);
9223 +void au_di_fin(struct dentry *dentry);
9224 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9225 +
9226 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9227 +void di_read_unlock(struct dentry *d, int flags);
9228 +void di_downgrade_lock(struct dentry *d, int flags);
9229 +void di_write_lock(struct dentry *d, unsigned int lsc);
9230 +void di_write_unlock(struct dentry *d);
9231 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9232 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9233 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9234 +
9235 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9236 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9237 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9238 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9239 +
9240 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9241 +                  struct dentry *h_dentry);
9242 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9243 +int au_dbrange_test(struct dentry *dentry);
9244 +void au_update_digen(struct dentry *dentry);
9245 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9246 +void au_update_dbtop(struct dentry *dentry);
9247 +void au_update_dbbot(struct dentry *dentry);
9248 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9249 +
9250 +/* ---------------------------------------------------------------------- */
9251 +
9252 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9253 +{
9254 +       return dentry->d_fsdata;
9255 +}
9256 +
9257 +/* ---------------------------------------------------------------------- */
9258 +
9259 +/* lock subclass for dinfo */
9260 +enum {
9261 +       AuLsc_DI_CHILD,         /* child first */
9262 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9263 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9264 +       AuLsc_DI_PARENT,
9265 +       AuLsc_DI_PARENT2,
9266 +       AuLsc_DI_PARENT3,
9267 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9268 +};
9269 +
9270 +/*
9271 + * di_read_lock_child, di_write_lock_child,
9272 + * di_read_lock_child2, di_write_lock_child2,
9273 + * di_read_lock_child3, di_write_lock_child3,
9274 + * di_read_lock_parent, di_write_lock_parent,
9275 + * di_read_lock_parent2, di_write_lock_parent2,
9276 + * di_read_lock_parent3, di_write_lock_parent3,
9277 + */
9278 +#define AuReadLockFunc(name, lsc) \
9279 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9280 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9281 +
9282 +#define AuWriteLockFunc(name, lsc) \
9283 +static inline void di_write_lock_##name(struct dentry *d) \
9284 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9285 +
9286 +#define AuRWLockFuncs(name, lsc) \
9287 +       AuReadLockFunc(name, lsc) \
9288 +       AuWriteLockFunc(name, lsc)
9289 +
9290 +AuRWLockFuncs(child, CHILD);
9291 +AuRWLockFuncs(child2, CHILD2);
9292 +AuRWLockFuncs(child3, CHILD3);
9293 +AuRWLockFuncs(parent, PARENT);
9294 +AuRWLockFuncs(parent2, PARENT2);
9295 +AuRWLockFuncs(parent3, PARENT3);
9296 +
9297 +#undef AuReadLockFunc
9298 +#undef AuWriteLockFunc
9299 +#undef AuRWLockFuncs
9300 +
9301 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9302 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9303 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9304 +
9305 +/* ---------------------------------------------------------------------- */
9306 +
9307 +/* todo: memory barrier? */
9308 +static inline unsigned int au_digen(struct dentry *d)
9309 +{
9310 +       return atomic_read(&au_di(d)->di_generation);
9311 +}
9312 +
9313 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9314 +{
9315 +       hdentry->hd_dentry = NULL;
9316 +}
9317 +
9318 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9319 +                                           aufs_bindex_t bindex)
9320 +{
9321 +       return di->di_hdentry + bindex;
9322 +}
9323 +
9324 +static inline void au_hdput(struct au_hdentry *hd)
9325 +{
9326 +       if (hd)
9327 +               dput(hd->hd_dentry);
9328 +}
9329 +
9330 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9331 +{
9332 +       DiMustAnyLock(dentry);
9333 +       return au_di(dentry)->di_btop;
9334 +}
9335 +
9336 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9337 +{
9338 +       DiMustAnyLock(dentry);
9339 +       return au_di(dentry)->di_bbot;
9340 +}
9341 +
9342 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9343 +{
9344 +       DiMustAnyLock(dentry);
9345 +       return au_di(dentry)->di_bwh;
9346 +}
9347 +
9348 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9349 +{
9350 +       DiMustAnyLock(dentry);
9351 +       return au_di(dentry)->di_bdiropq;
9352 +}
9353 +
9354 +/* todo: hard/soft set? */
9355 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9356 +{
9357 +       DiMustWriteLock(dentry);
9358 +       au_di(dentry)->di_btop = bindex;
9359 +}
9360 +
9361 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9362 +{
9363 +       DiMustWriteLock(dentry);
9364 +       au_di(dentry)->di_bbot = bindex;
9365 +}
9366 +
9367 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9368 +{
9369 +       DiMustWriteLock(dentry);
9370 +       /* dbwh can be outside of btop - bbot range */
9371 +       au_di(dentry)->di_bwh = bindex;
9372 +}
9373 +
9374 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9375 +{
9376 +       DiMustWriteLock(dentry);
9377 +       au_di(dentry)->di_bdiropq = bindex;
9378 +}
9379 +
9380 +/* ---------------------------------------------------------------------- */
9381 +
9382 +#ifdef CONFIG_AUFS_HNOTIFY
9383 +static inline void au_digen_dec(struct dentry *d)
9384 +{
9385 +       atomic_dec(&au_di(d)->di_generation);
9386 +}
9387 +
9388 +static inline void au_hn_di_reinit(struct dentry *dentry)
9389 +{
9390 +       dentry->d_fsdata = NULL;
9391 +}
9392 +#else
9393 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9394 +#endif /* CONFIG_AUFS_HNOTIFY */
9395 +
9396 +#endif /* __KERNEL__ */
9397 +#endif /* __AUFS_DENTRY_H__ */
9398 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9399 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9400 +++ linux/fs/aufs/dinfo.c       2022-08-02 12:12:45.162308621 +0200
9401 @@ -0,0 +1,554 @@
9402 +// SPDX-License-Identifier: GPL-2.0
9403 +/*
9404 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9405 + *
9406 + * This program, aufs is free software; you can redistribute it and/or modify
9407 + * it under the terms of the GNU General Public License as published by
9408 + * the Free Software Foundation; either version 2 of the License, or
9409 + * (at your option) any later version.
9410 + *
9411 + * This program is distributed in the hope that it will be useful,
9412 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9413 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9414 + * GNU General Public License for more details.
9415 + *
9416 + * You should have received a copy of the GNU General Public License
9417 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9418 + */
9419 +
9420 +/*
9421 + * dentry private data
9422 + */
9423 +
9424 +#include "aufs.h"
9425 +
9426 +void au_di_init_once(void *_dinfo)
9427 +{
9428 +       struct au_dinfo *dinfo = _dinfo;
9429 +
9430 +       au_rw_init(&dinfo->di_rwsem);
9431 +}
9432 +
9433 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9434 +{
9435 +       struct au_dinfo *dinfo;
9436 +       int nbr, i;
9437 +
9438 +       dinfo = au_cache_alloc_dinfo();
9439 +       if (unlikely(!dinfo))
9440 +               goto out;
9441 +
9442 +       nbr = au_sbbot(sb) + 1;
9443 +       if (nbr <= 0)
9444 +               nbr = 1;
9445 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9446 +       if (dinfo->di_hdentry) {
9447 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9448 +               dinfo->di_btop = -1;
9449 +               dinfo->di_bbot = -1;
9450 +               dinfo->di_bwh = -1;
9451 +               dinfo->di_bdiropq = -1;
9452 +               dinfo->di_tmpfile = 0;
9453 +               for (i = 0; i < nbr; i++)
9454 +                       dinfo->di_hdentry[i].hd_id = -1;
9455 +               goto out;
9456 +       }
9457 +
9458 +       au_cache_free_dinfo(dinfo);
9459 +       dinfo = NULL;
9460 +
9461 +out:
9462 +       return dinfo;
9463 +}
9464 +
9465 +void au_di_free(struct au_dinfo *dinfo)
9466 +{
9467 +       struct au_hdentry *p;
9468 +       aufs_bindex_t bbot, bindex;
9469 +
9470 +       /* dentry may not be revalidated */
9471 +       bindex = dinfo->di_btop;
9472 +       if (bindex >= 0) {
9473 +               bbot = dinfo->di_bbot;
9474 +               p = au_hdentry(dinfo, bindex);
9475 +               while (bindex++ <= bbot)
9476 +                       au_hdput(p++);
9477 +       }
9478 +       au_kfree_try_rcu(dinfo->di_hdentry);
9479 +       au_cache_free_dinfo(dinfo);
9480 +}
9481 +
9482 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9483 +{
9484 +       struct au_hdentry *p;
9485 +       aufs_bindex_t bi;
9486 +
9487 +       AuRwMustWriteLock(&a->di_rwsem);
9488 +       AuRwMustWriteLock(&b->di_rwsem);
9489 +
9490 +#define DiSwap(v, name)                                \
9491 +       do {                                    \
9492 +               v = a->di_##name;               \
9493 +               a->di_##name = b->di_##name;    \
9494 +               b->di_##name = v;               \
9495 +       } while (0)
9496 +
9497 +       DiSwap(p, hdentry);
9498 +       DiSwap(bi, btop);
9499 +       DiSwap(bi, bbot);
9500 +       DiSwap(bi, bwh);
9501 +       DiSwap(bi, bdiropq);
9502 +       /* smp_mb(); */
9503 +
9504 +#undef DiSwap
9505 +}
9506 +
9507 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9508 +{
9509 +       AuRwMustWriteLock(&dst->di_rwsem);
9510 +       AuRwMustWriteLock(&src->di_rwsem);
9511 +
9512 +       dst->di_btop = src->di_btop;
9513 +       dst->di_bbot = src->di_bbot;
9514 +       dst->di_bwh = src->di_bwh;
9515 +       dst->di_bdiropq = src->di_bdiropq;
9516 +       /* smp_mb(); */
9517 +}
9518 +
9519 +int au_di_init(struct dentry *dentry)
9520 +{
9521 +       int err;
9522 +       struct super_block *sb;
9523 +       struct au_dinfo *dinfo;
9524 +
9525 +       err = 0;
9526 +       sb = dentry->d_sb;
9527 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9528 +       if (dinfo) {
9529 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9530 +               /* smp_mb(); */ /* atomic_set */
9531 +               dentry->d_fsdata = dinfo;
9532 +       } else
9533 +               err = -ENOMEM;
9534 +
9535 +       return err;
9536 +}
9537 +
9538 +void au_di_fin(struct dentry *dentry)
9539 +{
9540 +       struct au_dinfo *dinfo;
9541 +
9542 +       dinfo = au_di(dentry);
9543 +       AuRwDestroy(&dinfo->di_rwsem);
9544 +       au_di_free(dinfo);
9545 +}
9546 +
9547 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9548 +{
9549 +       int err, sz;
9550 +       struct au_hdentry *hdp;
9551 +
9552 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9553 +
9554 +       err = -ENOMEM;
9555 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9556 +       if (!sz)
9557 +               sz = sizeof(*hdp);
9558 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9559 +                          may_shrink);
9560 +       if (hdp) {
9561 +               dinfo->di_hdentry = hdp;
9562 +               err = 0;
9563 +       }
9564 +
9565 +       return err;
9566 +}
9567 +
9568 +/* ---------------------------------------------------------------------- */
9569 +
9570 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9571 +{
9572 +       switch (lsc) {
9573 +       case AuLsc_DI_CHILD:
9574 +               ii_write_lock_child(inode);
9575 +               break;
9576 +       case AuLsc_DI_CHILD2:
9577 +               ii_write_lock_child2(inode);
9578 +               break;
9579 +       case AuLsc_DI_CHILD3:
9580 +               ii_write_lock_child3(inode);
9581 +               break;
9582 +       case AuLsc_DI_PARENT:
9583 +               ii_write_lock_parent(inode);
9584 +               break;
9585 +       case AuLsc_DI_PARENT2:
9586 +               ii_write_lock_parent2(inode);
9587 +               break;
9588 +       case AuLsc_DI_PARENT3:
9589 +               ii_write_lock_parent3(inode);
9590 +               break;
9591 +       default:
9592 +               BUG();
9593 +       }
9594 +}
9595 +
9596 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9597 +{
9598 +       switch (lsc) {
9599 +       case AuLsc_DI_CHILD:
9600 +               ii_read_lock_child(inode);
9601 +               break;
9602 +       case AuLsc_DI_CHILD2:
9603 +               ii_read_lock_child2(inode);
9604 +               break;
9605 +       case AuLsc_DI_CHILD3:
9606 +               ii_read_lock_child3(inode);
9607 +               break;
9608 +       case AuLsc_DI_PARENT:
9609 +               ii_read_lock_parent(inode);
9610 +               break;
9611 +       case AuLsc_DI_PARENT2:
9612 +               ii_read_lock_parent2(inode);
9613 +               break;
9614 +       case AuLsc_DI_PARENT3:
9615 +               ii_read_lock_parent3(inode);
9616 +               break;
9617 +       default:
9618 +               BUG();
9619 +       }
9620 +}
9621 +
9622 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9623 +{
9624 +       struct inode *inode;
9625 +
9626 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9627 +       if (d_really_is_positive(d)) {
9628 +               inode = d_inode(d);
9629 +               if (au_ftest_lock(flags, IW))
9630 +                       do_ii_write_lock(inode, lsc);
9631 +               else if (au_ftest_lock(flags, IR))
9632 +                       do_ii_read_lock(inode, lsc);
9633 +       }
9634 +}
9635 +
9636 +void di_read_unlock(struct dentry *d, int flags)
9637 +{
9638 +       struct inode *inode;
9639 +
9640 +       if (d_really_is_positive(d)) {
9641 +               inode = d_inode(d);
9642 +               if (au_ftest_lock(flags, IW)) {
9643 +                       au_dbg_verify_dinode(d);
9644 +                       ii_write_unlock(inode);
9645 +               } else if (au_ftest_lock(flags, IR)) {
9646 +                       au_dbg_verify_dinode(d);
9647 +                       ii_read_unlock(inode);
9648 +               }
9649 +       }
9650 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9651 +}
9652 +
9653 +void di_downgrade_lock(struct dentry *d, int flags)
9654 +{
9655 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9656 +               ii_downgrade_lock(d_inode(d));
9657 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9658 +}
9659 +
9660 +void di_write_lock(struct dentry *d, unsigned int lsc)
9661 +{
9662 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9663 +       if (d_really_is_positive(d))
9664 +               do_ii_write_lock(d_inode(d), lsc);
9665 +}
9666 +
9667 +void di_write_unlock(struct dentry *d)
9668 +{
9669 +       au_dbg_verify_dinode(d);
9670 +       if (d_really_is_positive(d))
9671 +               ii_write_unlock(d_inode(d));
9672 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9673 +}
9674 +
9675 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9676 +{
9677 +       AuDebugOn(d1 == d2
9678 +                 || d_inode(d1) == d_inode(d2)
9679 +                 || d1->d_sb != d2->d_sb);
9680 +
9681 +       if ((isdir && au_test_subdir(d1, d2))
9682 +           || d1 < d2) {
9683 +               di_write_lock_child(d1);
9684 +               di_write_lock_child2(d2);
9685 +       } else {
9686 +               di_write_lock_child(d2);
9687 +               di_write_lock_child2(d1);
9688 +       }
9689 +}
9690 +
9691 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9692 +{
9693 +       AuDebugOn(d1 == d2
9694 +                 || d_inode(d1) == d_inode(d2)
9695 +                 || d1->d_sb != d2->d_sb);
9696 +
9697 +       if ((isdir && au_test_subdir(d1, d2))
9698 +           || d1 < d2) {
9699 +               di_write_lock_parent(d1);
9700 +               di_write_lock_parent2(d2);
9701 +       } else {
9702 +               di_write_lock_parent(d2);
9703 +               di_write_lock_parent2(d1);
9704 +       }
9705 +}
9706 +
9707 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9708 +{
9709 +       di_write_unlock(d1);
9710 +       if (d_inode(d1) == d_inode(d2))
9711 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9712 +       else
9713 +               di_write_unlock(d2);
9714 +}
9715 +
9716 +/* ---------------------------------------------------------------------- */
9717 +
9718 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9719 +{
9720 +       struct dentry *d;
9721 +
9722 +       DiMustAnyLock(dentry);
9723 +
9724 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9725 +               return NULL;
9726 +       AuDebugOn(bindex < 0);
9727 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9728 +       AuDebugOn(d && au_dcount(d) <= 0);
9729 +       return d;
9730 +}
9731 +
9732 +/*
9733 + * extended version of au_h_dptr().
9734 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9735 + * error.
9736 + */
9737 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9738 +{
9739 +       struct dentry *h_dentry;
9740 +       struct inode *inode, *h_inode;
9741 +
9742 +       AuDebugOn(d_really_is_negative(dentry));
9743 +
9744 +       h_dentry = NULL;
9745 +       if (au_dbtop(dentry) <= bindex
9746 +           && bindex <= au_dbbot(dentry))
9747 +               h_dentry = au_h_dptr(dentry, bindex);
9748 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9749 +               dget(h_dentry);
9750 +               goto out; /* success */
9751 +       }
9752 +
9753 +       inode = d_inode(dentry);
9754 +       AuDebugOn(bindex < au_ibtop(inode));
9755 +       AuDebugOn(au_ibbot(inode) < bindex);
9756 +       h_inode = au_h_iptr(inode, bindex);
9757 +       h_dentry = d_find_alias(h_inode);
9758 +       if (h_dentry) {
9759 +               if (!IS_ERR(h_dentry)) {
9760 +                       if (!au_d_linkable(h_dentry))
9761 +                               goto out; /* success */
9762 +                       dput(h_dentry);
9763 +               } else
9764 +                       goto out;
9765 +       }
9766 +
9767 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9768 +               h_dentry = au_plink_lkup(inode, bindex);
9769 +               AuDebugOn(!h_dentry);
9770 +               if (!IS_ERR(h_dentry)) {
9771 +                       if (!au_d_hashed_positive(h_dentry))
9772 +                               goto out; /* success */
9773 +                       dput(h_dentry);
9774 +                       h_dentry = NULL;
9775 +               }
9776 +       }
9777 +
9778 +out:
9779 +       AuDbgDentry(h_dentry);
9780 +       return h_dentry;
9781 +}
9782 +
9783 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9784 +{
9785 +       aufs_bindex_t bbot, bwh;
9786 +
9787 +       bbot = au_dbbot(dentry);
9788 +       if (0 <= bbot) {
9789 +               bwh = au_dbwh(dentry);
9790 +               if (!bwh)
9791 +                       return bwh;
9792 +               if (0 < bwh && bwh < bbot)
9793 +                       return bwh - 1;
9794 +       }
9795 +       return bbot;
9796 +}
9797 +
9798 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9799 +{
9800 +       aufs_bindex_t bbot, bopq;
9801 +
9802 +       bbot = au_dbtail(dentry);
9803 +       if (0 <= bbot) {
9804 +               bopq = au_dbdiropq(dentry);
9805 +               if (0 <= bopq && bopq < bbot)
9806 +                       bbot = bopq;
9807 +       }
9808 +       return bbot;
9809 +}
9810 +
9811 +/* ---------------------------------------------------------------------- */
9812 +
9813 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9814 +                  struct dentry *h_dentry)
9815 +{
9816 +       struct au_dinfo *dinfo;
9817 +       struct au_hdentry *hd;
9818 +       struct au_branch *br;
9819 +
9820 +       DiMustWriteLock(dentry);
9821 +
9822 +       dinfo = au_di(dentry);
9823 +       hd = au_hdentry(dinfo, bindex);
9824 +       au_hdput(hd);
9825 +       hd->hd_dentry = h_dentry;
9826 +       if (h_dentry) {
9827 +               br = au_sbr(dentry->d_sb, bindex);
9828 +               hd->hd_id = br->br_id;
9829 +       }
9830 +}
9831 +
9832 +int au_dbrange_test(struct dentry *dentry)
9833 +{
9834 +       int err;
9835 +       aufs_bindex_t btop, bbot;
9836 +
9837 +       err = 0;
9838 +       btop = au_dbtop(dentry);
9839 +       bbot = au_dbbot(dentry);
9840 +       if (btop >= 0)
9841 +               AuDebugOn(bbot < 0 && btop > bbot);
9842 +       else {
9843 +               err = -EIO;
9844 +               AuDebugOn(bbot >= 0);
9845 +       }
9846 +
9847 +       return err;
9848 +}
9849 +
9850 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9851 +{
9852 +       int err;
9853 +
9854 +       err = 0;
9855 +       if (unlikely(au_digen(dentry) != sigen
9856 +                    || au_iigen_test(d_inode(dentry), sigen)))
9857 +               err = -EIO;
9858 +
9859 +       return err;
9860 +}
9861 +
9862 +void au_update_digen(struct dentry *dentry)
9863 +{
9864 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9865 +       /* smp_mb(); */ /* atomic_set */
9866 +}
9867 +
9868 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9869 +{
9870 +       struct au_dinfo *dinfo;
9871 +       struct dentry *h_d;
9872 +       struct au_hdentry *hdp;
9873 +       aufs_bindex_t bindex, bbot;
9874 +
9875 +       DiMustWriteLock(dentry);
9876 +
9877 +       dinfo = au_di(dentry);
9878 +       if (!dinfo || dinfo->di_btop < 0)
9879 +               return;
9880 +
9881 +       if (do_put_zero) {
9882 +               bbot = dinfo->di_bbot;
9883 +               bindex = dinfo->di_btop;
9884 +               hdp = au_hdentry(dinfo, bindex);
9885 +               for (; bindex <= bbot; bindex++, hdp++) {
9886 +                       h_d = hdp->hd_dentry;
9887 +                       if (h_d && d_is_negative(h_d))
9888 +                               au_set_h_dptr(dentry, bindex, NULL);
9889 +               }
9890 +       }
9891 +
9892 +       dinfo->di_btop = 0;
9893 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9894 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9895 +               if (hdp->hd_dentry)
9896 +                       break;
9897 +       if (dinfo->di_btop > dinfo->di_bbot) {
9898 +               dinfo->di_btop = -1;
9899 +               dinfo->di_bbot = -1;
9900 +               return;
9901 +       }
9902 +
9903 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9904 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9905 +               if (hdp->hd_dentry)
9906 +                       break;
9907 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9908 +}
9909 +
9910 +void au_update_dbtop(struct dentry *dentry)
9911 +{
9912 +       aufs_bindex_t bindex, bbot;
9913 +       struct dentry *h_dentry;
9914 +
9915 +       bbot = au_dbbot(dentry);
9916 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9917 +               h_dentry = au_h_dptr(dentry, bindex);
9918 +               if (!h_dentry)
9919 +                       continue;
9920 +               if (d_is_positive(h_dentry)) {
9921 +                       au_set_dbtop(dentry, bindex);
9922 +                       return;
9923 +               }
9924 +               au_set_h_dptr(dentry, bindex, NULL);
9925 +       }
9926 +}
9927 +
9928 +void au_update_dbbot(struct dentry *dentry)
9929 +{
9930 +       aufs_bindex_t bindex, btop;
9931 +       struct dentry *h_dentry;
9932 +
9933 +       btop = au_dbtop(dentry);
9934 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9935 +               h_dentry = au_h_dptr(dentry, bindex);
9936 +               if (!h_dentry)
9937 +                       continue;
9938 +               if (d_is_positive(h_dentry)) {
9939 +                       au_set_dbbot(dentry, bindex);
9940 +                       return;
9941 +               }
9942 +               au_set_h_dptr(dentry, bindex, NULL);
9943 +       }
9944 +}
9945 +
9946 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9947 +{
9948 +       aufs_bindex_t bindex, bbot;
9949 +
9950 +       bbot = au_dbbot(dentry);
9951 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9952 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9953 +                       return bindex;
9954 +       return -1;
9955 +}
9956 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9957 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9958 +++ linux/fs/aufs/dir.c 2022-08-02 12:12:45.162308621 +0200
9959 @@ -0,0 +1,765 @@
9960 +// SPDX-License-Identifier: GPL-2.0
9961 +/*
9962 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9963 + *
9964 + * This program, aufs is free software; you can redistribute it and/or modify
9965 + * it under the terms of the GNU General Public License as published by
9966 + * the Free Software Foundation; either version 2 of the License, or
9967 + * (at your option) any later version.
9968 + *
9969 + * This program is distributed in the hope that it will be useful,
9970 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9971 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9972 + * GNU General Public License for more details.
9973 + *
9974 + * You should have received a copy of the GNU General Public License
9975 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9976 + */
9977 +
9978 +/*
9979 + * directory operations
9980 + */
9981 +
9982 +#include <linux/fs_stack.h>
9983 +#include <linux/iversion.h>
9984 +#include "aufs.h"
9985 +
9986 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9987 +{
9988 +       unsigned int nlink;
9989 +
9990 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9991 +
9992 +       nlink = dir->i_nlink;
9993 +       nlink += h_dir->i_nlink - 2;
9994 +       if (h_dir->i_nlink < 2)
9995 +               nlink += 2;
9996 +       smp_mb(); /* for i_nlink */
9997 +       /* 0 can happen in revaliding */
9998 +       set_nlink(dir, nlink);
9999 +}
10000 +
10001 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10002 +{
10003 +       unsigned int nlink;
10004 +
10005 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10006 +
10007 +       nlink = dir->i_nlink;
10008 +       nlink -= h_dir->i_nlink - 2;
10009 +       if (h_dir->i_nlink < 2)
10010 +               nlink -= 2;
10011 +       smp_mb(); /* for i_nlink */
10012 +       /* nlink == 0 means the branch-fs is broken */
10013 +       set_nlink(dir, nlink);
10014 +}
10015 +
10016 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10017 +{
10018 +       loff_t sz;
10019 +       aufs_bindex_t bindex, bbot;
10020 +       struct file *h_file;
10021 +       struct dentry *h_dentry;
10022 +
10023 +       sz = 0;
10024 +       if (file) {
10025 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10026 +
10027 +               bbot = au_fbbot_dir(file);
10028 +               for (bindex = au_fbtop(file);
10029 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10030 +                    bindex++) {
10031 +                       h_file = au_hf_dir(file, bindex);
10032 +                       if (h_file && file_inode(h_file))
10033 +                               sz += vfsub_f_size_read(h_file);
10034 +               }
10035 +       } else {
10036 +               AuDebugOn(!dentry);
10037 +               AuDebugOn(!d_is_dir(dentry));
10038 +
10039 +               bbot = au_dbtaildir(dentry);
10040 +               for (bindex = au_dbtop(dentry);
10041 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10042 +                    bindex++) {
10043 +                       h_dentry = au_h_dptr(dentry, bindex);
10044 +                       if (h_dentry && d_is_positive(h_dentry))
10045 +                               sz += i_size_read(d_inode(h_dentry));
10046 +               }
10047 +       }
10048 +       if (sz < KMALLOC_MAX_SIZE)
10049 +               sz = roundup_pow_of_two(sz);
10050 +       if (sz > KMALLOC_MAX_SIZE)
10051 +               sz = KMALLOC_MAX_SIZE;
10052 +       else if (sz < NAME_MAX) {
10053 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10054 +               sz = AUFS_RDBLK_DEF;
10055 +       }
10056 +       return sz;
10057 +}
10058 +
10059 +struct au_dir_ts_arg {
10060 +       struct dentry *dentry;
10061 +       aufs_bindex_t brid;
10062 +};
10063 +
10064 +static void au_do_dir_ts(void *arg)
10065 +{
10066 +       struct au_dir_ts_arg *a = arg;
10067 +       struct au_dtime dt;
10068 +       struct path h_path;
10069 +       struct inode *dir, *h_dir;
10070 +       struct super_block *sb;
10071 +       struct au_branch *br;
10072 +       struct au_hinode *hdir;
10073 +       int err;
10074 +       aufs_bindex_t btop, bindex;
10075 +
10076 +       sb = a->dentry->d_sb;
10077 +       if (d_really_is_negative(a->dentry))
10078 +               goto out;
10079 +       /* no dir->i_mutex lock */
10080 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10081 +
10082 +       dir = d_inode(a->dentry);
10083 +       btop = au_ibtop(dir);
10084 +       bindex = au_br_index(sb, a->brid);
10085 +       if (bindex < btop)
10086 +               goto out_unlock;
10087 +
10088 +       br = au_sbr(sb, bindex);
10089 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10090 +       if (!h_path.dentry)
10091 +               goto out_unlock;
10092 +       h_path.mnt = au_br_mnt(br);
10093 +       au_dtime_store(&dt, a->dentry, &h_path);
10094 +
10095 +       br = au_sbr(sb, btop);
10096 +       if (!au_br_writable(br->br_perm))
10097 +               goto out_unlock;
10098 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10099 +       h_path.mnt = au_br_mnt(br);
10100 +       err = vfsub_mnt_want_write(h_path.mnt);
10101 +       if (err)
10102 +               goto out_unlock;
10103 +       hdir = au_hi(dir, btop);
10104 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10105 +       h_dir = au_h_iptr(dir, btop);
10106 +       if (h_dir->i_nlink
10107 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10108 +               dt.dt_h_path = h_path;
10109 +               au_dtime_revert(&dt);
10110 +       }
10111 +       au_hn_inode_unlock(hdir);
10112 +       vfsub_mnt_drop_write(h_path.mnt);
10113 +       au_cpup_attr_timesizes(dir);
10114 +
10115 +out_unlock:
10116 +       aufs_read_unlock(a->dentry, AuLock_DW);
10117 +out:
10118 +       dput(a->dentry);
10119 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10120 +       au_kfree_try_rcu(arg);
10121 +}
10122 +
10123 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10124 +{
10125 +       int perm, wkq_err;
10126 +       aufs_bindex_t btop;
10127 +       struct au_dir_ts_arg *arg;
10128 +       struct dentry *dentry;
10129 +       struct super_block *sb;
10130 +
10131 +       IMustLock(dir);
10132 +
10133 +       dentry = d_find_any_alias(dir);
10134 +       AuDebugOn(!dentry);
10135 +       sb = dentry->d_sb;
10136 +       btop = au_ibtop(dir);
10137 +       if (btop == bindex) {
10138 +               au_cpup_attr_timesizes(dir);
10139 +               goto out;
10140 +       }
10141 +
10142 +       perm = au_sbr_perm(sb, btop);
10143 +       if (!au_br_writable(perm))
10144 +               goto out;
10145 +
10146 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10147 +       if (!arg)
10148 +               goto out;
10149 +
10150 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10151 +       arg->brid = au_sbr_id(sb, bindex);
10152 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10153 +       if (unlikely(wkq_err)) {
10154 +               pr_err("wkq %d\n", wkq_err);
10155 +               dput(dentry);
10156 +               au_kfree_try_rcu(arg);
10157 +       }
10158 +
10159 +out:
10160 +       dput(dentry);
10161 +}
10162 +
10163 +/* ---------------------------------------------------------------------- */
10164 +
10165 +static int reopen_dir(struct file *file)
10166 +{
10167 +       int err;
10168 +       unsigned int flags;
10169 +       aufs_bindex_t bindex, btail, btop;
10170 +       struct dentry *dentry, *h_dentry;
10171 +       struct file *h_file;
10172 +
10173 +       /* open all lower dirs */
10174 +       dentry = file->f_path.dentry;
10175 +       btop = au_dbtop(dentry);
10176 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10177 +               au_set_h_fptr(file, bindex, NULL);
10178 +       au_set_fbtop(file, btop);
10179 +
10180 +       btail = au_dbtaildir(dentry);
10181 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10182 +               au_set_h_fptr(file, bindex, NULL);
10183 +       au_set_fbbot_dir(file, btail);
10184 +
10185 +       flags = vfsub_file_flags(file);
10186 +       for (bindex = btop; bindex <= btail; bindex++) {
10187 +               h_dentry = au_h_dptr(dentry, bindex);
10188 +               if (!h_dentry)
10189 +                       continue;
10190 +               h_file = au_hf_dir(file, bindex);
10191 +               if (h_file)
10192 +                       continue;
10193 +
10194 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10195 +               err = PTR_ERR(h_file);
10196 +               if (IS_ERR(h_file))
10197 +                       goto out; /* close all? */
10198 +               au_set_h_fptr(file, bindex, h_file);
10199 +       }
10200 +       au_update_figen(file);
10201 +       /* todo: necessary? */
10202 +       /* file->f_ra = h_file->f_ra; */
10203 +       err = 0;
10204 +
10205 +out:
10206 +       return err;
10207 +}
10208 +
10209 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10210 +{
10211 +       int err;
10212 +       aufs_bindex_t bindex, btail;
10213 +       struct dentry *dentry, *h_dentry;
10214 +       struct vfsmount *mnt;
10215 +
10216 +       FiMustWriteLock(file);
10217 +       AuDebugOn(h_file);
10218 +
10219 +       err = 0;
10220 +       mnt = file->f_path.mnt;
10221 +       dentry = file->f_path.dentry;
10222 +       file->f_version = inode_query_iversion(d_inode(dentry));
10223 +       bindex = au_dbtop(dentry);
10224 +       au_set_fbtop(file, bindex);
10225 +       btail = au_dbtaildir(dentry);
10226 +       au_set_fbbot_dir(file, btail);
10227 +       for (; !err && bindex <= btail; bindex++) {
10228 +               h_dentry = au_h_dptr(dentry, bindex);
10229 +               if (!h_dentry)
10230 +                       continue;
10231 +
10232 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10233 +               if (unlikely(err))
10234 +                       break;
10235 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10236 +               if (IS_ERR(h_file)) {
10237 +                       err = PTR_ERR(h_file);
10238 +                       break;
10239 +               }
10240 +               au_set_h_fptr(file, bindex, h_file);
10241 +       }
10242 +       au_update_figen(file);
10243 +       /* todo: necessary? */
10244 +       /* file->f_ra = h_file->f_ra; */
10245 +       if (!err)
10246 +               return 0; /* success */
10247 +
10248 +       /* close all */
10249 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10250 +               au_set_h_fptr(file, bindex, NULL);
10251 +       au_set_fbtop(file, -1);
10252 +       au_set_fbbot_dir(file, -1);
10253 +
10254 +       return err;
10255 +}
10256 +
10257 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10258 +                        struct file *file)
10259 +{
10260 +       int err;
10261 +       struct super_block *sb;
10262 +       struct au_fidir *fidir;
10263 +
10264 +       err = -ENOMEM;
10265 +       sb = file->f_path.dentry->d_sb;
10266 +       si_read_lock(sb, AuLock_FLUSH);
10267 +       fidir = au_fidir_alloc(sb);
10268 +       if (fidir) {
10269 +               struct au_do_open_args args = {
10270 +                       .open   = do_open_dir,
10271 +                       .fidir  = fidir
10272 +               };
10273 +               err = au_do_open(file, &args);
10274 +               if (unlikely(err))
10275 +                       au_kfree_rcu(fidir);
10276 +       }
10277 +       si_read_unlock(sb);
10278 +       return err;
10279 +}
10280 +
10281 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10282 +                           struct file *file)
10283 +{
10284 +       struct au_vdir *vdir_cache;
10285 +       struct au_finfo *finfo;
10286 +       struct au_fidir *fidir;
10287 +       struct au_hfile *hf;
10288 +       aufs_bindex_t bindex, bbot;
10289 +
10290 +       finfo = au_fi(file);
10291 +       fidir = finfo->fi_hdir;
10292 +       if (fidir) {
10293 +               au_hbl_del(&finfo->fi_hlist,
10294 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10295 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10296 +               if (vdir_cache)
10297 +                       au_vdir_free(vdir_cache);
10298 +
10299 +               bindex = finfo->fi_btop;
10300 +               if (bindex >= 0) {
10301 +                       hf = fidir->fd_hfile + bindex;
10302 +                       /*
10303 +                        * calls fput() instead of filp_close(),
10304 +                        * since no dnotify or lock for the lower file.
10305 +                        */
10306 +                       bbot = fidir->fd_bbot;
10307 +                       for (; bindex <= bbot; bindex++, hf++)
10308 +                               if (hf->hf_file)
10309 +                                       au_hfput(hf, /*execed*/0);
10310 +               }
10311 +               au_kfree_rcu(fidir);
10312 +               finfo->fi_hdir = NULL;
10313 +       }
10314 +       au_finfo_fin(file);
10315 +       return 0;
10316 +}
10317 +
10318 +/* ---------------------------------------------------------------------- */
10319 +
10320 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10321 +{
10322 +       int err;
10323 +       aufs_bindex_t bindex, bbot;
10324 +       struct file *h_file;
10325 +
10326 +       err = 0;
10327 +       bbot = au_fbbot_dir(file);
10328 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10329 +               h_file = au_hf_dir(file, bindex);
10330 +               if (h_file)
10331 +                       err = vfsub_flush(h_file, id);
10332 +       }
10333 +       return err;
10334 +}
10335 +
10336 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10337 +{
10338 +       return au_do_flush(file, id, au_do_flush_dir);
10339 +}
10340 +
10341 +/* ---------------------------------------------------------------------- */
10342 +
10343 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10344 +{
10345 +       int err;
10346 +       aufs_bindex_t bbot, bindex;
10347 +       struct inode *inode;
10348 +       struct super_block *sb;
10349 +
10350 +       err = 0;
10351 +       sb = dentry->d_sb;
10352 +       inode = d_inode(dentry);
10353 +       IMustLock(inode);
10354 +       bbot = au_dbbot(dentry);
10355 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10356 +               struct path h_path;
10357 +
10358 +               if (au_test_ro(sb, bindex, inode))
10359 +                       continue;
10360 +               h_path.dentry = au_h_dptr(dentry, bindex);
10361 +               if (!h_path.dentry)
10362 +                       continue;
10363 +
10364 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10365 +               err = vfsub_fsync(NULL, &h_path, datasync);
10366 +       }
10367 +
10368 +       return err;
10369 +}
10370 +
10371 +static int au_do_fsync_dir(struct file *file, int datasync)
10372 +{
10373 +       int err;
10374 +       aufs_bindex_t bbot, bindex;
10375 +       struct file *h_file;
10376 +       struct super_block *sb;
10377 +       struct inode *inode;
10378 +
10379 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10380 +       if (unlikely(err))
10381 +               goto out;
10382 +
10383 +       inode = file_inode(file);
10384 +       sb = inode->i_sb;
10385 +       bbot = au_fbbot_dir(file);
10386 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10387 +               h_file = au_hf_dir(file, bindex);
10388 +               if (!h_file || au_test_ro(sb, bindex, inode))
10389 +                       continue;
10390 +
10391 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10392 +       }
10393 +
10394 +out:
10395 +       return err;
10396 +}
10397 +
10398 +/*
10399 + * @file may be NULL
10400 + */
10401 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10402 +                         int datasync)
10403 +{
10404 +       int err;
10405 +       struct dentry *dentry;
10406 +       struct inode *inode;
10407 +       struct super_block *sb;
10408 +
10409 +       err = 0;
10410 +       dentry = file->f_path.dentry;
10411 +       inode = d_inode(dentry);
10412 +       inode_lock(inode);
10413 +       sb = dentry->d_sb;
10414 +       si_noflush_read_lock(sb);
10415 +       if (file)
10416 +               err = au_do_fsync_dir(file, datasync);
10417 +       else {
10418 +               di_write_lock_child(dentry);
10419 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10420 +       }
10421 +       au_cpup_attr_timesizes(inode);
10422 +       di_write_unlock(dentry);
10423 +       if (file)
10424 +               fi_write_unlock(file);
10425 +
10426 +       si_read_unlock(sb);
10427 +       inode_unlock(inode);
10428 +       return err;
10429 +}
10430 +
10431 +/* ---------------------------------------------------------------------- */
10432 +
10433 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10434 +{
10435 +       int err;
10436 +       struct dentry *dentry;
10437 +       struct inode *inode, *h_inode;
10438 +       struct super_block *sb;
10439 +
10440 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10441 +
10442 +       dentry = file->f_path.dentry;
10443 +       inode = d_inode(dentry);
10444 +       IMustLock(inode);
10445 +
10446 +       sb = dentry->d_sb;
10447 +       si_read_lock(sb, AuLock_FLUSH);
10448 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10449 +       if (unlikely(err))
10450 +               goto out;
10451 +       err = au_alive_dir(dentry);
10452 +       if (!err)
10453 +               err = au_vdir_init(file);
10454 +       di_downgrade_lock(dentry, AuLock_IR);
10455 +       if (unlikely(err))
10456 +               goto out_unlock;
10457 +
10458 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10459 +       if (!au_test_nfsd()) {
10460 +               err = au_vdir_fill_de(file, ctx);
10461 +               fsstack_copy_attr_atime(inode, h_inode);
10462 +       } else {
10463 +               /*
10464 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10465 +                * encode_fh() and others.
10466 +                */
10467 +               atomic_inc(&h_inode->i_count);
10468 +               di_read_unlock(dentry, AuLock_IR);
10469 +               si_read_unlock(sb);
10470 +               err = au_vdir_fill_de(file, ctx);
10471 +               fsstack_copy_attr_atime(inode, h_inode);
10472 +               fi_write_unlock(file);
10473 +               iput(h_inode);
10474 +
10475 +               AuTraceErr(err);
10476 +               return err;
10477 +       }
10478 +
10479 +out_unlock:
10480 +       di_read_unlock(dentry, AuLock_IR);
10481 +       fi_write_unlock(file);
10482 +out:
10483 +       si_read_unlock(sb);
10484 +       return err;
10485 +}
10486 +
10487 +/* ---------------------------------------------------------------------- */
10488 +
10489 +#define AuTestEmpty_WHONLY     1
10490 +#define AuTestEmpty_CALLED     (1 << 1)
10491 +#define AuTestEmpty_SHWH       (1 << 2)
10492 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10493 +#define au_fset_testempty(flags, name) \
10494 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10495 +#define au_fclr_testempty(flags, name) \
10496 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10497 +
10498 +#ifndef CONFIG_AUFS_SHWH
10499 +#undef AuTestEmpty_SHWH
10500 +#define AuTestEmpty_SHWH       0
10501 +#endif
10502 +
10503 +struct test_empty_arg {
10504 +       struct dir_context ctx;
10505 +       struct au_nhash *whlist;
10506 +       unsigned int flags;
10507 +       int err;
10508 +       aufs_bindex_t bindex;
10509 +};
10510 +
10511 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10512 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10513 +                        unsigned int d_type)
10514 +{
10515 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10516 +                                                 ctx);
10517 +       char *name = (void *)__name;
10518 +
10519 +       arg->err = 0;
10520 +       au_fset_testempty(arg->flags, CALLED);
10521 +       /* smp_mb(); */
10522 +       if (name[0] == '.'
10523 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10524 +               goto out; /* success */
10525 +
10526 +       if (namelen <= AUFS_WH_PFX_LEN
10527 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10528 +               if (au_ftest_testempty(arg->flags, WHONLY)
10529 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10530 +                       arg->err = -ENOTEMPTY;
10531 +               goto out;
10532 +       }
10533 +
10534 +       name += AUFS_WH_PFX_LEN;
10535 +       namelen -= AUFS_WH_PFX_LEN;
10536 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10537 +               arg->err = au_nhash_append_wh
10538 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10539 +                        au_ftest_testempty(arg->flags, SHWH));
10540 +
10541 +out:
10542 +       /* smp_mb(); */
10543 +       AuTraceErr(arg->err);
10544 +       return arg->err;
10545 +}
10546 +
10547 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10548 +{
10549 +       int err;
10550 +       struct file *h_file;
10551 +       struct au_branch *br;
10552 +
10553 +       h_file = au_h_open(dentry, arg->bindex,
10554 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10555 +                          /*file*/NULL, /*force_wr*/0);
10556 +       err = PTR_ERR(h_file);
10557 +       if (IS_ERR(h_file))
10558 +               goto out;
10559 +
10560 +       err = 0;
10561 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10562 +           && !file_inode(h_file)->i_nlink)
10563 +               goto out_put;
10564 +
10565 +       do {
10566 +               arg->err = 0;
10567 +               au_fclr_testempty(arg->flags, CALLED);
10568 +               /* smp_mb(); */
10569 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10570 +               if (err >= 0)
10571 +                       err = arg->err;
10572 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10573 +
10574 +out_put:
10575 +       fput(h_file);
10576 +       br = au_sbr(dentry->d_sb, arg->bindex);
10577 +       au_lcnt_dec(&br->br_nfiles);
10578 +out:
10579 +       return err;
10580 +}
10581 +
10582 +struct do_test_empty_args {
10583 +       int *errp;
10584 +       struct dentry *dentry;
10585 +       struct test_empty_arg *arg;
10586 +};
10587 +
10588 +static void call_do_test_empty(void *args)
10589 +{
10590 +       struct do_test_empty_args *a = args;
10591 +       *a->errp = do_test_empty(a->dentry, a->arg);
10592 +}
10593 +
10594 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10595 +{
10596 +       int err, wkq_err;
10597 +       struct dentry *h_dentry;
10598 +       struct inode *h_inode;
10599 +       struct user_namespace *h_userns;
10600 +
10601 +       h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
10602 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10603 +       h_inode = d_inode(h_dentry);
10604 +       /* todo: i_mode changes anytime? */
10605 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10606 +       err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
10607 +       inode_unlock_shared(h_inode);
10608 +       if (!err)
10609 +               err = do_test_empty(dentry, arg);
10610 +       else {
10611 +               struct do_test_empty_args args = {
10612 +                       .errp   = &err,
10613 +                       .dentry = dentry,
10614 +                       .arg    = arg
10615 +               };
10616 +               unsigned int flags = arg->flags;
10617 +
10618 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10619 +               if (unlikely(wkq_err))
10620 +                       err = wkq_err;
10621 +               arg->flags = flags;
10622 +       }
10623 +
10624 +       return err;
10625 +}
10626 +
10627 +int au_test_empty_lower(struct dentry *dentry)
10628 +{
10629 +       int err;
10630 +       unsigned int rdhash;
10631 +       aufs_bindex_t bindex, btop, btail;
10632 +       struct au_nhash whlist;
10633 +       struct test_empty_arg arg = {
10634 +               .ctx = {
10635 +                       .actor = test_empty_cb
10636 +               }
10637 +       };
10638 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10639 +
10640 +       SiMustAnyLock(dentry->d_sb);
10641 +
10642 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10643 +       if (!rdhash)
10644 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10645 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10646 +       if (unlikely(err))
10647 +               goto out;
10648 +
10649 +       arg.flags = 0;
10650 +       arg.whlist = &whlist;
10651 +       btop = au_dbtop(dentry);
10652 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10653 +               au_fset_testempty(arg.flags, SHWH);
10654 +       test_empty = do_test_empty;
10655 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10656 +               test_empty = sio_test_empty;
10657 +       arg.bindex = btop;
10658 +       err = test_empty(dentry, &arg);
10659 +       if (unlikely(err))
10660 +               goto out_whlist;
10661 +
10662 +       au_fset_testempty(arg.flags, WHONLY);
10663 +       btail = au_dbtaildir(dentry);
10664 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10665 +               struct dentry *h_dentry;
10666 +
10667 +               h_dentry = au_h_dptr(dentry, bindex);
10668 +               if (h_dentry && d_is_positive(h_dentry)) {
10669 +                       arg.bindex = bindex;
10670 +                       err = test_empty(dentry, &arg);
10671 +               }
10672 +       }
10673 +
10674 +out_whlist:
10675 +       au_nhash_wh_free(&whlist);
10676 +out:
10677 +       return err;
10678 +}
10679 +
10680 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10681 +{
10682 +       int err;
10683 +       struct test_empty_arg arg = {
10684 +               .ctx = {
10685 +                       .actor = test_empty_cb
10686 +               }
10687 +       };
10688 +       aufs_bindex_t bindex, btail;
10689 +
10690 +       err = 0;
10691 +       arg.whlist = whlist;
10692 +       arg.flags = AuTestEmpty_WHONLY;
10693 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10694 +               au_fset_testempty(arg.flags, SHWH);
10695 +       btail = au_dbtaildir(dentry);
10696 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10697 +               struct dentry *h_dentry;
10698 +
10699 +               h_dentry = au_h_dptr(dentry, bindex);
10700 +               if (h_dentry && d_is_positive(h_dentry)) {
10701 +                       arg.bindex = bindex;
10702 +                       err = sio_test_empty(dentry, &arg);
10703 +               }
10704 +       }
10705 +
10706 +       return err;
10707 +}
10708 +
10709 +/* ---------------------------------------------------------------------- */
10710 +
10711 +const struct file_operations aufs_dir_fop = {
10712 +       .owner          = THIS_MODULE,
10713 +       .llseek         = default_llseek,
10714 +       .read           = generic_read_dir,
10715 +       .iterate_shared = aufs_iterate_shared,
10716 +       .unlocked_ioctl = aufs_ioctl_dir,
10717 +#ifdef CONFIG_COMPAT
10718 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10719 +#endif
10720 +       .open           = aufs_open_dir,
10721 +       .release        = aufs_release_dir,
10722 +       .flush          = aufs_flush_dir,
10723 +       .fsync          = aufs_fsync_dir
10724 +};
10725 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10726 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10727 +++ linux/fs/aufs/dir.h 2022-08-02 12:12:45.162308621 +0200
10728 @@ -0,0 +1,134 @@
10729 +/* SPDX-License-Identifier: GPL-2.0 */
10730 +/*
10731 + * Copyright (C) 2005-2021 Junjiro R. Okajima
10732 + *
10733 + * This program, aufs is free software; you can redistribute it and/or modify
10734 + * it under the terms of the GNU General Public License as published by
10735 + * the Free Software Foundation; either version 2 of the License, or
10736 + * (at your option) any later version.
10737 + *
10738 + * This program is distributed in the hope that it will be useful,
10739 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10740 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10741 + * GNU General Public License for more details.
10742 + *
10743 + * You should have received a copy of the GNU General Public License
10744 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10745 + */
10746 +
10747 +/*
10748 + * directory operations
10749 + */
10750 +
10751 +#ifndef __AUFS_DIR_H__
10752 +#define __AUFS_DIR_H__
10753 +
10754 +#ifdef __KERNEL__
10755 +
10756 +#include <linux/fs.h>
10757 +
10758 +/* ---------------------------------------------------------------------- */
10759 +
10760 +/* need to be faster and smaller */
10761 +
10762 +struct au_nhash {
10763 +       unsigned int            nh_num;
10764 +       struct hlist_head       *nh_head;
10765 +};
10766 +
10767 +struct au_vdir_destr {
10768 +       unsigned char   len;
10769 +       unsigned char   name[];
10770 +} __packed;
10771 +
10772 +struct au_vdir_dehstr {
10773 +       struct hlist_node       hash;
10774 +       struct au_vdir_destr    *str;
10775 +       struct rcu_head         rcu;
10776 +} ____cacheline_aligned_in_smp;
10777 +
10778 +struct au_vdir_de {
10779 +       ino_t                   de_ino;
10780 +       unsigned char           de_type;
10781 +       /* caution: packed */
10782 +       struct au_vdir_destr    de_str;
10783 +} __packed;
10784 +
10785 +struct au_vdir_wh {
10786 +       struct hlist_node       wh_hash;
10787 +#ifdef CONFIG_AUFS_SHWH
10788 +       ino_t                   wh_ino;
10789 +       aufs_bindex_t           wh_bindex;
10790 +       unsigned char           wh_type;
10791 +#else
10792 +       aufs_bindex_t           wh_bindex;
10793 +#endif
10794 +       /* caution: packed */
10795 +       struct au_vdir_destr    wh_str;
10796 +} __packed;
10797 +
10798 +union au_vdir_deblk_p {
10799 +       unsigned char           *deblk;
10800 +       struct au_vdir_de       *de;
10801 +};
10802 +
10803 +struct au_vdir {
10804 +       unsigned char   **vd_deblk;
10805 +       unsigned long   vd_nblk;
10806 +       struct {
10807 +               unsigned long           ul;
10808 +               union au_vdir_deblk_p   p;
10809 +       } vd_last;
10810 +
10811 +       u64             vd_version;
10812 +       unsigned int    vd_deblk_sz;
10813 +       unsigned long   vd_jiffy;
10814 +       struct rcu_head rcu;
10815 +} ____cacheline_aligned_in_smp;
10816 +
10817 +/* ---------------------------------------------------------------------- */
10818 +
10819 +/* dir.c */
10820 +extern const struct file_operations aufs_dir_fop;
10821 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10822 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10823 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10824 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10825 +int au_test_empty_lower(struct dentry *dentry);
10826 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10827 +
10828 +/* vdir.c */
10829 +unsigned int au_rdhash_est(loff_t sz);
10830 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10831 +void au_nhash_wh_free(struct au_nhash *whlist);
10832 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10833 +                           int limit);
10834 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10835 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10836 +                      unsigned int d_type, aufs_bindex_t bindex,
10837 +                      unsigned char shwh);
10838 +void au_vdir_free(struct au_vdir *vdir);
10839 +int au_vdir_init(struct file *file);
10840 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10841 +
10842 +/* ioctl.c */
10843 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10844 +
10845 +#ifdef CONFIG_AUFS_RDU
10846 +/* rdu.c */
10847 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10848 +#ifdef CONFIG_COMPAT
10849 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10850 +                        unsigned long arg);
10851 +#endif
10852 +#else
10853 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10854 +       unsigned int cmd, unsigned long arg)
10855 +#ifdef CONFIG_COMPAT
10856 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10857 +       unsigned int cmd, unsigned long arg)
10858 +#endif
10859 +#endif
10860 +
10861 +#endif /* __KERNEL__ */
10862 +#endif /* __AUFS_DIR_H__ */
10863 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10864 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10865 +++ linux/fs/aufs/dirren.c      2022-08-02 12:12:45.165641939 +0200
10866 @@ -0,0 +1,1315 @@
10867 +// SPDX-License-Identifier: GPL-2.0
10868 +/*
10869 + * Copyright (C) 2017-2021 Junjiro R. Okajima
10870 + *
10871 + * This program, aufs is free software; you can redistribute it and/or modify
10872 + * it under the terms of the GNU General Public License as published by
10873 + * the Free Software Foundation; either version 2 of the License, or
10874 + * (at your option) any later version.
10875 + *
10876 + * This program is distributed in the hope that it will be useful,
10877 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10878 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10879 + * GNU General Public License for more details.
10880 + *
10881 + * You should have received a copy of the GNU General Public License
10882 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10883 + */
10884 +
10885 +/*
10886 + * special handling in renaming a directory
10887 + * in order to support looking-up the before-renamed name on the lower readonly
10888 + * branches
10889 + */
10890 +
10891 +#include <linux/byteorder/generic.h>
10892 +#include "aufs.h"
10893 +
10894 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10895 +{
10896 +       int idx;
10897 +
10898 +       idx = au_dr_ihash(ent->dr_h_ino);
10899 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10900 +}
10901 +
10902 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10903 +{
10904 +       int ret, i;
10905 +       struct hlist_bl_head *hbl;
10906 +
10907 +       ret = 1;
10908 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10909 +               hbl = dr->dr_h_ino + i;
10910 +               hlist_bl_lock(hbl);
10911 +               ret &= hlist_bl_empty(hbl);
10912 +               hlist_bl_unlock(hbl);
10913 +       }
10914 +
10915 +       return ret;
10916 +}
10917 +
10918 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10919 +{
10920 +       struct au_dr_hino *found, *ent;
10921 +       struct hlist_bl_head *hbl;
10922 +       struct hlist_bl_node *pos;
10923 +       int idx;
10924 +
10925 +       found = NULL;
10926 +       idx = au_dr_ihash(ino);
10927 +       hbl = dr->dr_h_ino + idx;
10928 +       hlist_bl_lock(hbl);
10929 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10930 +               if (ent->dr_h_ino == ino) {
10931 +                       found = ent;
10932 +                       break;
10933 +               }
10934 +       hlist_bl_unlock(hbl);
10935 +
10936 +       return found;
10937 +}
10938 +
10939 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10940 +                       struct au_dr_hino *add_ent)
10941 +{
10942 +       int found, idx;
10943 +       struct hlist_bl_head *hbl;
10944 +       struct hlist_bl_node *pos;
10945 +       struct au_dr_hino *ent;
10946 +
10947 +       found = 0;
10948 +       idx = au_dr_ihash(ino);
10949 +       hbl = dr->dr_h_ino + idx;
10950 +#if 0 /* debug print */
10951 +       {
10952 +               struct hlist_bl_node *tmp;
10953 +
10954 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10955 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10956 +       }
10957 +#endif
10958 +       hlist_bl_lock(hbl);
10959 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10960 +               if (ent->dr_h_ino == ino) {
10961 +                       found = 1;
10962 +                       break;
10963 +               }
10964 +       if (!found && add_ent)
10965 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10966 +       hlist_bl_unlock(hbl);
10967 +
10968 +       if (!found && add_ent)
10969 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10970 +
10971 +       return found;
10972 +}
10973 +
10974 +void au_dr_hino_free(struct au_dr_br *dr)
10975 +{
10976 +       int i;
10977 +       struct hlist_bl_head *hbl;
10978 +       struct hlist_bl_node *pos, *tmp;
10979 +       struct au_dr_hino *ent;
10980 +
10981 +       /* SiMustWriteLock(sb); */
10982 +
10983 +       for (i = 0; i < AuDirren_NHASH; i++) {
10984 +               hbl = dr->dr_h_ino + i;
10985 +               /* no spinlock since sbinfo must be write-locked */
10986 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10987 +                       au_kfree_rcu(ent);
10988 +               INIT_HLIST_BL_HEAD(hbl);
10989 +       }
10990 +}
10991 +
10992 +/* returns the number of inodes or an error */
10993 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10994 +                           struct file *hinofile)
10995 +{
10996 +       int err, i;
10997 +       ssize_t ssz;
10998 +       loff_t pos, oldsize;
10999 +       __be64 u64;
11000 +       struct inode *hinoinode;
11001 +       struct hlist_bl_head *hbl;
11002 +       struct hlist_bl_node *n1, *n2;
11003 +       struct au_dr_hino *ent;
11004 +
11005 +       SiMustWriteLock(sb);
11006 +       AuDebugOn(!au_br_writable(br->br_perm));
11007 +
11008 +       hinoinode = file_inode(hinofile);
11009 +       oldsize = i_size_read(hinoinode);
11010 +
11011 +       err = 0;
11012 +       pos = 0;
11013 +       hbl = br->br_dirren.dr_h_ino;
11014 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11015 +               /* no bit-lock since sbinfo must be write-locked */
11016 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11017 +                       AuDbg("hi%llu, %pD2\n",
11018 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11019 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11020 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11021 +                       if (ssz == sizeof(u64))
11022 +                               continue;
11023 +
11024 +                       /* write error */
11025 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11026 +                       err = -ENOSPC;
11027 +                       if (ssz < 0)
11028 +                               err = ssz;
11029 +                       break;
11030 +               }
11031 +       }
11032 +       /* regardless the error */
11033 +       if (pos < oldsize) {
11034 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11035 +               AuTraceErr(err);
11036 +       }
11037 +
11038 +       AuTraceErr(err);
11039 +       return err;
11040 +}
11041 +
11042 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11043 +{
11044 +       int err, hidx;
11045 +       ssize_t ssz;
11046 +       size_t sz, n;
11047 +       loff_t pos;
11048 +       uint64_t u64;
11049 +       struct au_dr_hino *ent;
11050 +       struct inode *hinoinode;
11051 +       struct hlist_bl_head *hbl;
11052 +
11053 +       err = 0;
11054 +       pos = 0;
11055 +       hbl = dr->dr_h_ino;
11056 +       hinoinode = file_inode(hinofile);
11057 +       sz = i_size_read(hinoinode);
11058 +       AuDebugOn(sz % sizeof(u64));
11059 +       n = sz / sizeof(u64);
11060 +       while (n--) {
11061 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11062 +               if (unlikely(ssz != sizeof(u64))) {
11063 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11064 +                       err = -EINVAL;
11065 +                       if (ssz < 0)
11066 +                               err = ssz;
11067 +                       goto out_free;
11068 +               }
11069 +
11070 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11071 +               if (!ent) {
11072 +                       err = -ENOMEM;
11073 +                       AuTraceErr(err);
11074 +                       goto out_free;
11075 +               }
11076 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11077 +               AuDbg("hi%llu, %pD2\n",
11078 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11079 +               hidx = au_dr_ihash(ent->dr_h_ino);
11080 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11081 +       }
11082 +       goto out; /* success */
11083 +
11084 +out_free:
11085 +       au_dr_hino_free(dr);
11086 +out:
11087 +       AuTraceErr(err);
11088 +       return err;
11089 +}
11090 +
11091 +/*
11092 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11093 + * @path is a switch to distinguish load and store.
11094 + */
11095 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11096 +                     struct au_branch *br, const struct path *path)
11097 +{
11098 +       int err, flags;
11099 +       unsigned char load, suspend;
11100 +       struct file *hinofile;
11101 +       struct au_hinode *hdir;
11102 +       struct inode *dir, *delegated;
11103 +       struct path hinopath;
11104 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11105 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11106 +
11107 +       AuDebugOn(bindex < 0 && !br);
11108 +       AuDebugOn(bindex >= 0 && br);
11109 +
11110 +       err = -EINVAL;
11111 +       suspend = !br;
11112 +       if (suspend)
11113 +               br = au_sbr(sb, bindex);
11114 +       load = !!path;
11115 +       if (!load) {
11116 +               path = &br->br_path;
11117 +               AuDebugOn(!au_br_writable(br->br_perm));
11118 +               if (unlikely(!au_br_writable(br->br_perm)))
11119 +                       goto out;
11120 +       }
11121 +
11122 +       hdir = NULL;
11123 +       if (suspend) {
11124 +               dir = d_inode(sb->s_root);
11125 +               hdir = au_hinode(au_ii(dir), bindex);
11126 +               dir = hdir->hi_inode;
11127 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11128 +       } else {
11129 +               dir = d_inode(path->dentry);
11130 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11131 +       }
11132 +       hinopath.mnt = path->mnt;
11133 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11134 +       err = PTR_ERR(hinopath.dentry);
11135 +       if (IS_ERR(hinopath.dentry))
11136 +               goto out_unlock;
11137 +
11138 +       err = 0;
11139 +       flags = O_RDONLY;
11140 +       if (load) {
11141 +               if (d_is_negative(hinopath.dentry))
11142 +                       goto out_dput; /* success */
11143 +       } else {
11144 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11145 +                       if (d_is_positive(hinopath.dentry)) {
11146 +                               delegated = NULL;
11147 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11148 +                                                  /*force*/0);
11149 +                               AuTraceErr(err);
11150 +                               if (unlikely(err))
11151 +                                       pr_err("ignored err %d, %pd2\n",
11152 +                                              err, hinopath.dentry);
11153 +                               if (unlikely(err == -EWOULDBLOCK))
11154 +                                       iput(delegated);
11155 +                               err = 0;
11156 +                       }
11157 +                       goto out_dput;
11158 +               } else if (!d_is_positive(hinopath.dentry)) {
11159 +                       err = vfsub_create(dir, &hinopath, 0600,
11160 +                                          /*want_excl*/false);
11161 +                       AuTraceErr(err);
11162 +                       if (unlikely(err))
11163 +                               goto out_dput;
11164 +               }
11165 +               flags = O_WRONLY;
11166 +       }
11167 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11168 +       if (suspend)
11169 +               au_hn_inode_unlock(hdir);
11170 +       else
11171 +               inode_unlock(dir);
11172 +       dput(hinopath.dentry);
11173 +       AuTraceErrPtr(hinofile);
11174 +       if (IS_ERR(hinofile)) {
11175 +               err = PTR_ERR(hinofile);
11176 +               goto out;
11177 +       }
11178 +
11179 +       if (load)
11180 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11181 +       else
11182 +               err = au_dr_hino_store(sb, br, hinofile);
11183 +       fput(hinofile);
11184 +       goto out;
11185 +
11186 +out_dput:
11187 +       dput(hinopath.dentry);
11188 +out_unlock:
11189 +       if (suspend)
11190 +               au_hn_inode_unlock(hdir);
11191 +       else
11192 +               inode_unlock(dir);
11193 +out:
11194 +       AuTraceErr(err);
11195 +       return err;
11196 +}
11197 +
11198 +/* ---------------------------------------------------------------------- */
11199 +
11200 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11201 +{
11202 +       int err;
11203 +       struct kstatfs kstfs;
11204 +       dev_t dev;
11205 +       struct dentry *dentry;
11206 +       struct super_block *sb;
11207 +
11208 +       err = vfs_statfs((void *)path, &kstfs);
11209 +       AuTraceErr(err);
11210 +       if (unlikely(err))
11211 +               goto out;
11212 +
11213 +       /* todo: support for UUID */
11214 +
11215 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11216 +               brid->type = AuBrid_FSID;
11217 +               brid->fsid = kstfs.f_fsid;
11218 +       } else {
11219 +               dentry = path->dentry;
11220 +               sb = dentry->d_sb;
11221 +               dev = sb->s_dev;
11222 +               if (dev) {
11223 +                       brid->type = AuBrid_DEV;
11224 +                       brid->dev = dev;
11225 +               }
11226 +       }
11227 +
11228 +out:
11229 +       return err;
11230 +}
11231 +
11232 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11233 +                 const struct path *path)
11234 +{
11235 +       int err, i;
11236 +       struct au_dr_br *dr;
11237 +       struct hlist_bl_head *hbl;
11238 +
11239 +       dr = &br->br_dirren;
11240 +       hbl = dr->dr_h_ino;
11241 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11242 +               INIT_HLIST_BL_HEAD(hbl);
11243 +
11244 +       err = au_dr_brid_init(&dr->dr_brid, path);
11245 +       if (unlikely(err))
11246 +               goto out;
11247 +
11248 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11249 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11250 +
11251 +out:
11252 +       AuTraceErr(err);
11253 +       return err;
11254 +}
11255 +
11256 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11257 +{
11258 +       int err;
11259 +
11260 +       err = 0;
11261 +       if (au_br_writable(br->br_perm))
11262 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11263 +       if (!err)
11264 +               au_dr_hino_free(&br->br_dirren);
11265 +
11266 +       return err;
11267 +}
11268 +
11269 +/* ---------------------------------------------------------------------- */
11270 +
11271 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11272 +                      char *buf, size_t sz)
11273 +{
11274 +       int err;
11275 +       unsigned int major, minor;
11276 +       char *p;
11277 +
11278 +       p = buf;
11279 +       err = snprintf(p, sz, "%d_", brid->type);
11280 +       AuDebugOn(err > sz);
11281 +       p += err;
11282 +       sz -= err;
11283 +       switch (brid->type) {
11284 +       case AuBrid_Unset:
11285 +               return -EINVAL;
11286 +       case AuBrid_UUID:
11287 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11288 +               break;
11289 +       case AuBrid_FSID:
11290 +               err = snprintf(p, sz, "%08x-%08x",
11291 +                              brid->fsid.val[0], brid->fsid.val[1]);
11292 +               break;
11293 +       case AuBrid_DEV:
11294 +               major = MAJOR(brid->dev);
11295 +               minor = MINOR(brid->dev);
11296 +               if (major <= 0xff && minor <= 0xff)
11297 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11298 +               else
11299 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11300 +               break;
11301 +       }
11302 +       AuDebugOn(err > sz);
11303 +       p += err;
11304 +       sz -= err;
11305 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11306 +       AuDebugOn(err > sz);
11307 +       p += err;
11308 +       sz -= err;
11309 +
11310 +       return p - buf;
11311 +}
11312 +
11313 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11314 +{
11315 +       int rlen;
11316 +       struct dentry *br_dentry;
11317 +       struct inode *br_inode;
11318 +
11319 +       br_dentry = au_br_dentry(br);
11320 +       br_inode = d_inode(br_dentry);
11321 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11322 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11323 +       AuDebugOn(rlen > len);
11324 +
11325 +       return rlen;
11326 +}
11327 +
11328 +/* ---------------------------------------------------------------------- */
11329 +
11330 +/*
11331 + * from the given @h_dentry, construct drinfo at @*fdata.
11332 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11333 + * @allocated.
11334 + */
11335 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11336 +                              struct dentry *h_dentry,
11337 +                              unsigned char *allocated)
11338 +{
11339 +       int err, v;
11340 +       struct au_drinfo_fdata *f, *p;
11341 +       struct au_drinfo *drinfo;
11342 +       struct inode *h_inode;
11343 +       struct qstr *qname;
11344 +
11345 +       err = 0;
11346 +       f = *fdata;
11347 +       h_inode = d_inode(h_dentry);
11348 +       qname = &h_dentry->d_name;
11349 +       drinfo = &f->drinfo;
11350 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11351 +       drinfo->oldnamelen = qname->len;
11352 +       if (*allocated < sizeof(*f) + qname->len) {
11353 +               v = roundup_pow_of_two(*allocated + qname->len);
11354 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11355 +               if (unlikely(!p)) {
11356 +                       err = -ENOMEM;
11357 +                       AuTraceErr(err);
11358 +                       goto out;
11359 +               }
11360 +               f = p;
11361 +               *fdata = f;
11362 +               *allocated = v;
11363 +               drinfo = &f->drinfo;
11364 +       }
11365 +       memcpy(drinfo->oldname, qname->name, qname->len);
11366 +       AuDbg("i%llu, %.*s\n",
11367 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11368 +             drinfo->oldname);
11369 +
11370 +out:
11371 +       AuTraceErr(err);
11372 +       return err;
11373 +}
11374 +
11375 +/* callers have to free the return value */
11376 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11377 +{
11378 +       struct au_drinfo *ret, *drinfo;
11379 +       struct au_drinfo_fdata fdata;
11380 +       int len;
11381 +       loff_t pos;
11382 +       ssize_t ssz;
11383 +
11384 +       ret = ERR_PTR(-EIO);
11385 +       pos = 0;
11386 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11387 +       if (unlikely(ssz != sizeof(fdata))) {
11388 +               AuIOErr("ssz %zd, %u, %pD2\n",
11389 +                       ssz, (unsigned int)sizeof(fdata), file);
11390 +               goto out;
11391 +       }
11392 +
11393 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11394 +       switch (fdata.magic) {
11395 +       case AUFS_DRINFO_MAGIC_V1:
11396 +               break;
11397 +       default:
11398 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11399 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11400 +               goto out;
11401 +       }
11402 +
11403 +       drinfo = &fdata.drinfo;
11404 +       len = drinfo->oldnamelen;
11405 +       if (!len) {
11406 +               AuIOErr("broken drinfo %pD2\n", file);
11407 +               goto out;
11408 +       }
11409 +
11410 +       ret = NULL;
11411 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11412 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11413 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11414 +                     (unsigned long long)drinfo->ino,
11415 +                     (unsigned long long)h_ino, file);
11416 +               goto out; /* success */
11417 +       }
11418 +
11419 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11420 +       if (unlikely(!ret)) {
11421 +               ret = ERR_PTR(-ENOMEM);
11422 +               AuTraceErrPtr(ret);
11423 +               goto out;
11424 +       }
11425 +
11426 +       *ret = *drinfo;
11427 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11428 +       if (unlikely(ssz != len)) {
11429 +               au_kfree_rcu(ret);
11430 +               ret = ERR_PTR(-EIO);
11431 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11432 +               goto out;
11433 +       }
11434 +
11435 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11436 +
11437 +out:
11438 +       return ret;
11439 +}
11440 +
11441 +/* ---------------------------------------------------------------------- */
11442 +
11443 +/* in order to be revertible */
11444 +struct au_drinfo_rev_elm {
11445 +       int                     created;
11446 +       struct dentry           *info_dentry;
11447 +       struct au_drinfo        *info_last;
11448 +};
11449 +
11450 +struct au_drinfo_rev {
11451 +       unsigned char                   already;
11452 +       aufs_bindex_t                   nelm;
11453 +       struct au_drinfo_rev_elm        elm[];
11454 +};
11455 +
11456 +/* todo: isn't it too large? */
11457 +struct au_drinfo_store {
11458 +       struct path h_ppath;
11459 +       struct dentry *h_dentry;
11460 +       struct au_drinfo_fdata *fdata;
11461 +       char *infoname;                 /* inside of whname, just after PFX */
11462 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11463 +       aufs_bindex_t btgt, btail;
11464 +       unsigned char no_sio,
11465 +               allocated,              /* current size of *fdata */
11466 +               infonamelen,            /* room size for p */
11467 +               whnamelen,              /* length of the generated name */
11468 +               renameback;             /* renamed back */
11469 +};
11470 +
11471 +/* on rename(2) error, the caller should revert it using @elm */
11472 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11473 +                             struct au_drinfo_rev_elm *elm)
11474 +{
11475 +       int err, len;
11476 +       ssize_t ssz;
11477 +       loff_t pos;
11478 +       struct path infopath = {
11479 +               .mnt = w->h_ppath.mnt
11480 +       };
11481 +       struct inode *h_dir, *h_inode, *delegated;
11482 +       struct file *infofile;
11483 +       struct qstr *qname;
11484 +
11485 +       AuDebugOn(elm
11486 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11487 +
11488 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11489 +                                              w->whnamelen);
11490 +       AuTraceErrPtr(infopath.dentry);
11491 +       if (IS_ERR(infopath.dentry)) {
11492 +               err = PTR_ERR(infopath.dentry);
11493 +               goto out;
11494 +       }
11495 +
11496 +       err = 0;
11497 +       h_dir = d_inode(w->h_ppath.dentry);
11498 +       if (elm && d_is_negative(infopath.dentry)) {
11499 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11500 +               AuTraceErr(err);
11501 +               if (unlikely(err))
11502 +                       goto out_dput;
11503 +               elm->created = 1;
11504 +               elm->info_dentry = dget(infopath.dentry);
11505 +       }
11506 +
11507 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11508 +       AuTraceErrPtr(infofile);
11509 +       if (IS_ERR(infofile)) {
11510 +               err = PTR_ERR(infofile);
11511 +               goto out_dput;
11512 +       }
11513 +
11514 +       h_inode = d_inode(infopath.dentry);
11515 +       if (elm && i_size_read(h_inode)) {
11516 +               h_inode = d_inode(w->h_dentry);
11517 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11518 +               AuTraceErrPtr(elm->info_last);
11519 +               if (IS_ERR(elm->info_last)) {
11520 +                       err = PTR_ERR(elm->info_last);
11521 +                       elm->info_last = NULL;
11522 +                       AuDebugOn(elm->info_dentry);
11523 +                       goto out_fput;
11524 +               }
11525 +       }
11526 +
11527 +       if (elm && w->renameback) {
11528 +               delegated = NULL;
11529 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11530 +               AuTraceErr(err);
11531 +               if (unlikely(err == -EWOULDBLOCK))
11532 +                       iput(delegated);
11533 +               goto out_fput;
11534 +       }
11535 +
11536 +       pos = 0;
11537 +       qname = &w->h_dentry->d_name;
11538 +       len = sizeof(*w->fdata) + qname->len;
11539 +       if (!elm)
11540 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11541 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11542 +       if (ssz == len) {
11543 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11544 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11545 +               goto out_fput; /* success */
11546 +       } else {
11547 +               err = -EIO;
11548 +               if (ssz < 0)
11549 +                       err = ssz;
11550 +               /* the caller should revert it using @elm */
11551 +       }
11552 +
11553 +out_fput:
11554 +       fput(infofile);
11555 +out_dput:
11556 +       dput(infopath.dentry);
11557 +out:
11558 +       AuTraceErr(err);
11559 +       return err;
11560 +}
11561 +
11562 +struct au_call_drinfo_do_store_args {
11563 +       int *errp;
11564 +       struct au_drinfo_store *w;
11565 +       struct au_drinfo_rev_elm *elm;
11566 +};
11567 +
11568 +static void au_call_drinfo_do_store(void *args)
11569 +{
11570 +       struct au_call_drinfo_do_store_args *a = args;
11571 +
11572 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11573 +}
11574 +
11575 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11576 +                              struct au_drinfo_rev_elm *elm)
11577 +{
11578 +       int err, wkq_err;
11579 +
11580 +       if (w->no_sio)
11581 +               err = au_drinfo_do_store(w, elm);
11582 +       else {
11583 +               struct au_call_drinfo_do_store_args a = {
11584 +                       .errp   = &err,
11585 +                       .w      = w,
11586 +                       .elm    = elm
11587 +               };
11588 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11589 +               if (unlikely(wkq_err))
11590 +                       err = wkq_err;
11591 +       }
11592 +       AuTraceErr(err);
11593 +
11594 +       return err;
11595 +}
11596 +
11597 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11598 +                                    aufs_bindex_t btgt)
11599 +{
11600 +       int err;
11601 +
11602 +       memset(w, 0, sizeof(*w));
11603 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11604 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11605 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11606 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11607 +       w->btgt = btgt;
11608 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11609 +
11610 +       err = -ENOMEM;
11611 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11612 +       if (unlikely(!w->fdata)) {
11613 +               AuTraceErr(err);
11614 +               goto out;
11615 +       }
11616 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11617 +       err = 0;
11618 +
11619 +out:
11620 +       return err;
11621 +}
11622 +
11623 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11624 +{
11625 +       au_kfree_rcu(w->fdata);
11626 +}
11627 +
11628 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11629 +                               struct au_drinfo_store *w)
11630 +{
11631 +       struct au_drinfo_rev_elm *elm;
11632 +       struct inode *h_dir, *delegated;
11633 +       int err, nelm;
11634 +       struct path infopath = {
11635 +               .mnt = w->h_ppath.mnt
11636 +       };
11637 +
11638 +       h_dir = d_inode(w->h_ppath.dentry);
11639 +       IMustLock(h_dir);
11640 +
11641 +       err = 0;
11642 +       elm = rev->elm;
11643 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11644 +               AuDebugOn(elm->created && elm->info_last);
11645 +               if (elm->created) {
11646 +                       AuDbg("here\n");
11647 +                       delegated = NULL;
11648 +                       infopath.dentry = elm->info_dentry;
11649 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11650 +                                          !w->no_sio);
11651 +                       AuTraceErr(err);
11652 +                       if (unlikely(err == -EWOULDBLOCK))
11653 +                               iput(delegated);
11654 +                       dput(elm->info_dentry);
11655 +               } else if (elm->info_last) {
11656 +                       AuDbg("here\n");
11657 +                       w->fdata->drinfo = *elm->info_last;
11658 +                       memcpy(w->fdata->drinfo.oldname,
11659 +                              elm->info_last->oldname,
11660 +                              elm->info_last->oldnamelen);
11661 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11662 +                       au_kfree_rcu(elm->info_last);
11663 +               }
11664 +               if (unlikely(err))
11665 +                       AuIOErr("%d, %s\n", err, w->whname);
11666 +               /* go on even if err */
11667 +       }
11668 +}
11669 +
11670 +/* caller has to call au_dr_rename_fin() later */
11671 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11672 +                          struct qstr *dst_name, void *_rev)
11673 +{
11674 +       int err, sz, nelm;
11675 +       aufs_bindex_t bindex, btail;
11676 +       struct au_drinfo_store work;
11677 +       struct au_drinfo_rev *rev, **p;
11678 +       struct au_drinfo_rev_elm *elm;
11679 +       struct super_block *sb;
11680 +       struct au_branch *br;
11681 +       struct au_hinode *hdir;
11682 +
11683 +       err = au_drinfo_store_work_init(&work, btgt);
11684 +       AuTraceErr(err);
11685 +       if (unlikely(err))
11686 +               goto out;
11687 +
11688 +       err = -ENOMEM;
11689 +       btail = au_dbtaildir(dentry);
11690 +       nelm = btail - btgt;
11691 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11692 +       rev = kcalloc(1, sz, GFP_NOFS);
11693 +       if (unlikely(!rev)) {
11694 +               AuTraceErr(err);
11695 +               goto out_args;
11696 +       }
11697 +       rev->nelm = nelm;
11698 +       elm = rev->elm;
11699 +       p = _rev;
11700 +       *p = rev;
11701 +
11702 +       err = 0;
11703 +       sb = dentry->d_sb;
11704 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11705 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11706 +       hdir = au_hi(d_inode(dentry), btgt);
11707 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11708 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11709 +               work.h_dentry = au_h_dptr(dentry, bindex);
11710 +               if (!work.h_dentry)
11711 +                       continue;
11712 +
11713 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11714 +                                         &work.allocated);
11715 +               AuTraceErr(err);
11716 +               if (unlikely(err))
11717 +                       break;
11718 +
11719 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11720 +               br = au_sbr(sb, bindex);
11721 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11722 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11723 +                                                work.infonamelen);
11724 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11725 +                     work.whnamelen, work.whname,
11726 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11727 +                     work.fdata->drinfo.oldnamelen,
11728 +                     work.fdata->drinfo.oldname);
11729 +
11730 +               err = au_drinfo_store_sio(&work, elm);
11731 +               AuTraceErr(err);
11732 +               if (unlikely(err))
11733 +                       break;
11734 +       }
11735 +       if (unlikely(err)) {
11736 +               /* revert all drinfo */
11737 +               au_drinfo_store_rev(rev, &work);
11738 +               au_kfree_try_rcu(rev);
11739 +               *p = NULL;
11740 +       }
11741 +       au_hn_inode_unlock(hdir);
11742 +
11743 +out_args:
11744 +       au_drinfo_store_work_fin(&work);
11745 +out:
11746 +       return err;
11747 +}
11748 +
11749 +/* ---------------------------------------------------------------------- */
11750 +
11751 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11752 +                struct qstr *dst_name, void *_rev)
11753 +{
11754 +       int err, already;
11755 +       ino_t ino;
11756 +       struct super_block *sb;
11757 +       struct au_branch *br;
11758 +       struct au_dr_br *dr;
11759 +       struct dentry *h_dentry;
11760 +       struct inode *h_inode;
11761 +       struct au_dr_hino *ent;
11762 +       struct au_drinfo_rev *rev, **p;
11763 +
11764 +       AuDbg("bindex %d\n", bindex);
11765 +
11766 +       err = -ENOMEM;
11767 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11768 +       if (unlikely(!ent))
11769 +               goto out;
11770 +
11771 +       sb = src->d_sb;
11772 +       br = au_sbr(sb, bindex);
11773 +       dr = &br->br_dirren;
11774 +       h_dentry = au_h_dptr(src, bindex);
11775 +       h_inode = d_inode(h_dentry);
11776 +       ino = h_inode->i_ino;
11777 +       ent->dr_h_ino = ino;
11778 +       already = au_dr_hino_test_add(dr, ino, ent);
11779 +       AuDbg("b%d, hi%llu, already %d\n",
11780 +             bindex, (unsigned long long)ino, already);
11781 +
11782 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11783 +       AuTraceErr(err);
11784 +       if (!err) {
11785 +               p = _rev;
11786 +               rev = *p;
11787 +               rev->already = already;
11788 +               goto out; /* success */
11789 +       }
11790 +
11791 +       /* revert */
11792 +       if (!already)
11793 +               au_dr_hino_del(dr, ent);
11794 +       au_kfree_rcu(ent);
11795 +
11796 +out:
11797 +       AuTraceErr(err);
11798 +       return err;
11799 +}
11800 +
11801 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11802 +{
11803 +       struct au_drinfo_rev *rev;
11804 +       struct au_drinfo_rev_elm *elm;
11805 +       int nelm;
11806 +
11807 +       rev = _rev;
11808 +       elm = rev->elm;
11809 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11810 +               dput(elm->info_dentry);
11811 +               au_kfree_rcu(elm->info_last);
11812 +       }
11813 +       au_kfree_try_rcu(rev);
11814 +}
11815 +
11816 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11817 +{
11818 +       int err;
11819 +       struct au_drinfo_store work;
11820 +       struct au_drinfo_rev *rev = _rev;
11821 +       struct super_block *sb;
11822 +       struct au_branch *br;
11823 +       struct inode *h_inode;
11824 +       struct au_dr_br *dr;
11825 +       struct au_dr_hino *ent;
11826 +
11827 +       err = au_drinfo_store_work_init(&work, btgt);
11828 +       if (unlikely(err))
11829 +               goto out;
11830 +
11831 +       sb = src->d_sb;
11832 +       br = au_sbr(sb, btgt);
11833 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11834 +       work.h_ppath.mnt = au_br_mnt(br);
11835 +       au_drinfo_store_rev(rev, &work);
11836 +       au_drinfo_store_work_fin(&work);
11837 +       if (rev->already)
11838 +               goto out;
11839 +
11840 +       dr = &br->br_dirren;
11841 +       h_inode = d_inode(work.h_ppath.dentry);
11842 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11843 +       BUG_ON(!ent);
11844 +       au_dr_hino_del(dr, ent);
11845 +       au_kfree_rcu(ent);
11846 +
11847 +out:
11848 +       au_kfree_try_rcu(rev);
11849 +       if (unlikely(err))
11850 +               pr_err("failed to remove dirren info\n");
11851 +}
11852 +
11853 +/* ---------------------------------------------------------------------- */
11854 +
11855 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11856 +                                          char *whname, int whnamelen,
11857 +                                          struct dentry **info_dentry)
11858 +{
11859 +       struct au_drinfo *drinfo;
11860 +       struct file *f;
11861 +       struct inode *h_dir;
11862 +       struct path infopath;
11863 +       int unlocked;
11864 +
11865 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11866 +
11867 +       *info_dentry = NULL;
11868 +       drinfo = NULL;
11869 +       unlocked = 0;
11870 +       h_dir = d_inode(h_ppath->dentry);
11871 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11872 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11873 +       if (IS_ERR(infopath.dentry)) {
11874 +               drinfo = (void *)infopath.dentry;
11875 +               goto out;
11876 +       }
11877 +
11878 +       if (d_is_negative(infopath.dentry))
11879 +               goto out_dput; /* success */
11880 +
11881 +       infopath.mnt = h_ppath->mnt;
11882 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11883 +       inode_unlock_shared(h_dir);
11884 +       unlocked = 1;
11885 +       if (IS_ERR(f)) {
11886 +               drinfo = (void *)f;
11887 +               goto out_dput;
11888 +       }
11889 +
11890 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11891 +       if (IS_ERR_OR_NULL(drinfo))
11892 +               goto out_fput;
11893 +
11894 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11895 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11896 +
11897 +out_fput:
11898 +       fput(f);
11899 +out_dput:
11900 +       dput(infopath.dentry);
11901 +out:
11902 +       if (!unlocked)
11903 +               inode_unlock_shared(h_dir);
11904 +       AuTraceErrPtr(drinfo);
11905 +       return drinfo;
11906 +}
11907 +
11908 +struct au_drinfo_do_load_args {
11909 +       struct au_drinfo **drinfop;
11910 +       struct path *h_ppath;
11911 +       char *whname;
11912 +       int whnamelen;
11913 +       struct dentry **info_dentry;
11914 +};
11915 +
11916 +static void au_call_drinfo_do_load(void *args)
11917 +{
11918 +       struct au_drinfo_do_load_args *a = args;
11919 +
11920 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11921 +                                       a->info_dentry);
11922 +}
11923 +
11924 +struct au_drinfo_load {
11925 +       struct path h_ppath;
11926 +       struct qstr *qname;
11927 +       unsigned char no_sio;
11928 +
11929 +       aufs_bindex_t ninfo;
11930 +       struct au_drinfo **drinfo;
11931 +};
11932 +
11933 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11934 +                         struct au_branch *br)
11935 +{
11936 +       int err, wkq_err, whnamelen, e;
11937 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11938 +               = AUFS_WH_DR_INFO_PFX;
11939 +       struct au_drinfo *drinfo;
11940 +       struct qstr oldname;
11941 +       struct inode *h_dir, *delegated;
11942 +       struct dentry *info_dentry;
11943 +       struct path infopath;
11944 +
11945 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11946 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11947 +                                   sizeof(whname) - whnamelen);
11948 +       if (w->no_sio)
11949 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11950 +                                          &info_dentry);
11951 +       else {
11952 +               struct au_drinfo_do_load_args args = {
11953 +                       .drinfop        = &drinfo,
11954 +                       .h_ppath        = &w->h_ppath,
11955 +                       .whname         = whname,
11956 +                       .whnamelen      = whnamelen,
11957 +                       .info_dentry    = &info_dentry
11958 +               };
11959 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11960 +               if (unlikely(wkq_err))
11961 +                       drinfo = ERR_PTR(wkq_err);
11962 +       }
11963 +       err = PTR_ERR(drinfo);
11964 +       if (IS_ERR_OR_NULL(drinfo))
11965 +               goto out;
11966 +
11967 +       err = 0;
11968 +       oldname.len = drinfo->oldnamelen;
11969 +       oldname.name = drinfo->oldname;
11970 +       if (au_qstreq(w->qname, &oldname)) {
11971 +               /* the name is renamed back */
11972 +               au_kfree_rcu(drinfo);
11973 +               drinfo = NULL;
11974 +
11975 +               infopath.dentry = info_dentry;
11976 +               infopath.mnt = w->h_ppath.mnt;
11977 +               h_dir = d_inode(w->h_ppath.dentry);
11978 +               delegated = NULL;
11979 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11980 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11981 +               inode_unlock(h_dir);
11982 +               if (unlikely(e))
11983 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11984 +               if (unlikely(e == -EWOULDBLOCK))
11985 +                       iput(delegated);
11986 +       }
11987 +       au_kfree_rcu(w->drinfo[bindex]);
11988 +       w->drinfo[bindex] = drinfo;
11989 +       dput(info_dentry);
11990 +
11991 +out:
11992 +       AuTraceErr(err);
11993 +       return err;
11994 +}
11995 +
11996 +/* ---------------------------------------------------------------------- */
11997 +
11998 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
11999 +{
12000 +       struct au_drinfo **p = drinfo;
12001 +
12002 +       while (n-- > 0)
12003 +               au_kfree_rcu(*drinfo++);
12004 +       au_kfree_try_rcu(p);
12005 +}
12006 +
12007 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12008 +              aufs_bindex_t btgt)
12009 +{
12010 +       int err, ninfo;
12011 +       struct au_drinfo_load w;
12012 +       aufs_bindex_t bindex, bbot;
12013 +       struct au_branch *br;
12014 +       struct inode *h_dir;
12015 +       struct au_dr_hino *ent;
12016 +       struct super_block *sb;
12017 +
12018 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12019 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12020 +             AuLNPair(&lkup->whname), btgt);
12021 +
12022 +       sb = dentry->d_sb;
12023 +       bbot = au_sbbot(sb);
12024 +       w.ninfo = bbot + 1;
12025 +       if (!lkup->dirren.drinfo) {
12026 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12027 +                                             sizeof(*lkup->dirren.drinfo),
12028 +                                             GFP_NOFS);
12029 +               if (unlikely(!lkup->dirren.drinfo)) {
12030 +                       err = -ENOMEM;
12031 +                       goto out;
12032 +               }
12033 +               lkup->dirren.ninfo = w.ninfo;
12034 +       }
12035 +       w.drinfo = lkup->dirren.drinfo;
12036 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12037 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12038 +       AuDebugOn(!w.h_ppath.dentry);
12039 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12040 +       w.qname = &dentry->d_name;
12041 +
12042 +       ninfo = 0;
12043 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12044 +               br = au_sbr(sb, bindex);
12045 +               err = au_drinfo_load(&w, bindex, br);
12046 +               if (unlikely(err))
12047 +                       goto out_free;
12048 +               if (w.drinfo[bindex])
12049 +                       ninfo++;
12050 +       }
12051 +       if (!ninfo) {
12052 +               br = au_sbr(sb, btgt);
12053 +               h_dir = d_inode(w.h_ppath.dentry);
12054 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12055 +               AuDebugOn(!ent);
12056 +               au_dr_hino_del(&br->br_dirren, ent);
12057 +               au_kfree_rcu(ent);
12058 +       }
12059 +       goto out; /* success */
12060 +
12061 +out_free:
12062 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12063 +       lkup->dirren.ninfo = 0;
12064 +       lkup->dirren.drinfo = NULL;
12065 +out:
12066 +       AuTraceErr(err);
12067 +       return err;
12068 +}
12069 +
12070 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12071 +{
12072 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12073 +}
12074 +
12075 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12076 +{
12077 +       int err;
12078 +       struct au_drinfo *drinfo;
12079 +
12080 +       err = 0;
12081 +       if (!lkup->dirren.drinfo)
12082 +               goto out;
12083 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12084 +       drinfo = lkup->dirren.drinfo[btgt];
12085 +       if (!drinfo)
12086 +               goto out;
12087 +
12088 +       au_kfree_try_rcu(lkup->whname.name);
12089 +       lkup->whname.name = NULL;
12090 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12091 +       lkup->dirren.dr_name.name = drinfo->oldname;
12092 +       lkup->name = &lkup->dirren.dr_name;
12093 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12094 +       if (!err)
12095 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12096 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12097 +                     btgt);
12098 +
12099 +out:
12100 +       AuTraceErr(err);
12101 +       return err;
12102 +}
12103 +
12104 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12105 +                    ino_t h_ino)
12106 +{
12107 +       int match;
12108 +       struct au_drinfo *drinfo;
12109 +
12110 +       match = 1;
12111 +       if (!lkup->dirren.drinfo)
12112 +               goto out;
12113 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12114 +       drinfo = lkup->dirren.drinfo[bindex];
12115 +       if (!drinfo)
12116 +               goto out;
12117 +
12118 +       match = (drinfo->ino == h_ino);
12119 +       AuDbg("match %d\n", match);
12120 +
12121 +out:
12122 +       return match;
12123 +}
12124 +
12125 +/* ---------------------------------------------------------------------- */
12126 +
12127 +int au_dr_opt_set(struct super_block *sb)
12128 +{
12129 +       int err;
12130 +       aufs_bindex_t bindex, bbot;
12131 +       struct au_branch *br;
12132 +
12133 +       err = 0;
12134 +       bbot = au_sbbot(sb);
12135 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12136 +               br = au_sbr(sb, bindex);
12137 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12138 +       }
12139 +
12140 +       return err;
12141 +}
12142 +
12143 +int au_dr_opt_flush(struct super_block *sb)
12144 +{
12145 +       int err;
12146 +       aufs_bindex_t bindex, bbot;
12147 +       struct au_branch *br;
12148 +
12149 +       err = 0;
12150 +       bbot = au_sbbot(sb);
12151 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12152 +               br = au_sbr(sb, bindex);
12153 +               if (au_br_writable(br->br_perm))
12154 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12155 +       }
12156 +
12157 +       return err;
12158 +}
12159 +
12160 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12161 +{
12162 +       int err;
12163 +       aufs_bindex_t bindex, bbot;
12164 +       struct au_branch *br;
12165 +
12166 +       err = 0;
12167 +       if (!no_flush) {
12168 +               err = au_dr_opt_flush(sb);
12169 +               if (unlikely(err))
12170 +                       goto out;
12171 +       }
12172 +
12173 +       bbot = au_sbbot(sb);
12174 +       for (bindex = 0; bindex <= bbot; bindex++) {
12175 +               br = au_sbr(sb, bindex);
12176 +               au_dr_hino_free(&br->br_dirren);
12177 +       }
12178 +
12179 +out:
12180 +       return err;
12181 +}
12182 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12183 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12184 +++ linux/fs/aufs/dirren.h      2022-08-02 12:12:45.165641939 +0200
12185 @@ -0,0 +1,140 @@
12186 +/* SPDX-License-Identifier: GPL-2.0 */
12187 +/*
12188 + * Copyright (C) 2017-2021 Junjiro R. Okajima
12189 + *
12190 + * This program, aufs is free software; you can redistribute it and/or modify
12191 + * it under the terms of the GNU General Public License as published by
12192 + * the Free Software Foundation; either version 2 of the License, or
12193 + * (at your option) any later version.
12194 + *
12195 + * This program is distributed in the hope that it will be useful,
12196 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12197 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12198 + * GNU General Public License for more details.
12199 + *
12200 + * You should have received a copy of the GNU General Public License
12201 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12202 + */
12203 +
12204 +/*
12205 + * renamed dir info
12206 + */
12207 +
12208 +#ifndef __AUFS_DIRREN_H__
12209 +#define __AUFS_DIRREN_H__
12210 +
12211 +#ifdef __KERNEL__
12212 +
12213 +#include <linux/dcache.h>
12214 +#include <linux/statfs.h>
12215 +#include <linux/uuid.h>
12216 +#include "hbl.h"
12217 +
12218 +#define AuDirren_NHASH 100
12219 +
12220 +#ifdef CONFIG_AUFS_DIRREN
12221 +enum au_brid_type {
12222 +       AuBrid_Unset,
12223 +       AuBrid_UUID,
12224 +       AuBrid_FSID,
12225 +       AuBrid_DEV
12226 +};
12227 +
12228 +struct au_dr_brid {
12229 +       enum au_brid_type       type;
12230 +       union {
12231 +               uuid_t  uuid;   /* unimplemented yet */
12232 +               fsid_t  fsid;
12233 +               dev_t   dev;
12234 +       };
12235 +};
12236 +
12237 +/* 20 is the max digits length of ulong 64 */
12238 +/* brid-type "_" uuid "_" inum */
12239 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12240 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12241 +
12242 +struct au_dr_hino {
12243 +       struct hlist_bl_node    dr_hnode;
12244 +       ino_t                   dr_h_ino;
12245 +};
12246 +
12247 +struct au_dr_br {
12248 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12249 +       struct au_dr_brid       dr_brid;
12250 +};
12251 +
12252 +struct au_dr_lookup {
12253 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12254 +       struct qstr             dr_name; /* subset of dr_info */
12255 +       aufs_bindex_t           ninfo;
12256 +       struct au_drinfo        **drinfo;
12257 +};
12258 +#else
12259 +struct au_dr_hino;
12260 +/* empty */
12261 +struct au_dr_br { };
12262 +struct au_dr_lookup { };
12263 +#endif
12264 +
12265 +/* ---------------------------------------------------------------------- */
12266 +
12267 +struct au_branch;
12268 +struct au_do_lookup_args;
12269 +struct au_hinode;
12270 +#ifdef CONFIG_AUFS_DIRREN
12271 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12272 +                       struct au_dr_hino *add_ent);
12273 +void au_dr_hino_free(struct au_dr_br *dr);
12274 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12275 +                 const struct path *path);
12276 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12277 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12278 +                struct qstr *dst_name, void *_rev);
12279 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12280 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12281 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12282 +              aufs_bindex_t bindex);
12283 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12284 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12285 +                    ino_t h_ino);
12286 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12287 +int au_dr_opt_set(struct super_block *sb);
12288 +int au_dr_opt_flush(struct super_block *sb);
12289 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12290 +#else
12291 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12292 +          struct au_dr_hino *add_ent);
12293 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12294 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12295 +          const struct path *path);
12296 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12297 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12298 +          struct qstr *dst_name, void *_rev);
12299 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12300 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12301 +          void *rev);
12302 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12303 +          aufs_bindex_t bindex);
12304 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12305 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12306 +          aufs_bindex_t bindex, ino_t h_ino);
12307 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12308 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12309 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12310 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12311 +#endif
12312 +
12313 +/* ---------------------------------------------------------------------- */
12314 +
12315 +#ifdef CONFIG_AUFS_DIRREN
12316 +static inline int au_dr_ihash(ino_t h_ino)
12317 +{
12318 +       return h_ino % AuDirren_NHASH;
12319 +}
12320 +#else
12321 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12322 +#endif
12323 +
12324 +#endif /* __KERNEL__ */
12325 +#endif /* __AUFS_DIRREN_H__ */
12326 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12327 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12328 +++ linux/fs/aufs/dynop.c       2022-08-02 12:12:45.165641939 +0200
12329 @@ -0,0 +1,368 @@
12330 +// SPDX-License-Identifier: GPL-2.0
12331 +/*
12332 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12333 + *
12334 + * This program, aufs is free software; you can redistribute it and/or modify
12335 + * it under the terms of the GNU General Public License as published by
12336 + * the Free Software Foundation; either version 2 of the License, or
12337 + * (at your option) any later version.
12338 + *
12339 + * This program is distributed in the hope that it will be useful,
12340 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12341 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12342 + * GNU General Public License for more details.
12343 + *
12344 + * You should have received a copy of the GNU General Public License
12345 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12346 + */
12347 +
12348 +/*
12349 + * dynamically customizable operations for regular files
12350 + */
12351 +
12352 +#include "aufs.h"
12353 +
12354 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12355 +
12356 +/*
12357 + * How large will these lists be?
12358 + * Usually just a few elements, 20-30 at most for each, I guess.
12359 + */
12360 +static struct hlist_bl_head dynop[AuDyLast];
12361 +
12362 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12363 +                                    const void *h_op)
12364 +{
12365 +       struct au_dykey *key, *tmp;
12366 +       struct hlist_bl_node *pos;
12367 +
12368 +       key = NULL;
12369 +       hlist_bl_lock(hbl);
12370 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12371 +               if (tmp->dk_op.dy_hop == h_op) {
12372 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12373 +                               key = tmp;
12374 +                       break;
12375 +               }
12376 +       hlist_bl_unlock(hbl);
12377 +
12378 +       return key;
12379 +}
12380 +
12381 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12382 +{
12383 +       struct au_dykey **k, *found;
12384 +       const void *h_op = key->dk_op.dy_hop;
12385 +       int i;
12386 +
12387 +       found = NULL;
12388 +       k = br->br_dykey;
12389 +       for (i = 0; i < AuBrDynOp; i++)
12390 +               if (k[i]) {
12391 +                       if (k[i]->dk_op.dy_hop == h_op) {
12392 +                               found = k[i];
12393 +                               break;
12394 +                       }
12395 +               } else
12396 +                       break;
12397 +       if (!found) {
12398 +               spin_lock(&br->br_dykey_lock);
12399 +               for (; i < AuBrDynOp; i++)
12400 +                       if (k[i]) {
12401 +                               if (k[i]->dk_op.dy_hop == h_op) {
12402 +                                       found = k[i];
12403 +                                       break;
12404 +                               }
12405 +                       } else {
12406 +                               k[i] = key;
12407 +                               break;
12408 +                       }
12409 +               spin_unlock(&br->br_dykey_lock);
12410 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12411 +       }
12412 +
12413 +       return found;
12414 +}
12415 +
12416 +/* kref_get() if @key is already added */
12417 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12418 +{
12419 +       struct au_dykey *tmp, *found;
12420 +       struct hlist_bl_node *pos;
12421 +       const void *h_op = key->dk_op.dy_hop;
12422 +
12423 +       found = NULL;
12424 +       hlist_bl_lock(hbl);
12425 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12426 +               if (tmp->dk_op.dy_hop == h_op) {
12427 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12428 +                               found = tmp;
12429 +                       break;
12430 +               }
12431 +       if (!found)
12432 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12433 +       hlist_bl_unlock(hbl);
12434 +
12435 +       if (!found)
12436 +               DyPrSym(key);
12437 +       return found;
12438 +}
12439 +
12440 +static void dy_free_rcu(struct rcu_head *rcu)
12441 +{
12442 +       struct au_dykey *key;
12443 +
12444 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12445 +       DyPrSym(key);
12446 +       kfree(key);
12447 +}
12448 +
12449 +static void dy_free(struct kref *kref)
12450 +{
12451 +       struct au_dykey *key;
12452 +       struct hlist_bl_head *hbl;
12453 +
12454 +       key = container_of(kref, struct au_dykey, dk_kref);
12455 +       hbl = dynop + key->dk_op.dy_type;
12456 +       au_hbl_del(&key->dk_hnode, hbl);
12457 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12458 +}
12459 +
12460 +void au_dy_put(struct au_dykey *key)
12461 +{
12462 +       kref_put(&key->dk_kref, dy_free);
12463 +}
12464 +
12465 +/* ---------------------------------------------------------------------- */
12466 +
12467 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12468 +
12469 +#ifdef CONFIG_AUFS_DEBUG
12470 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12471 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12472 +#else
12473 +#define DyDbgDeclare(cnt)      do {} while (0)
12474 +#define DyDbgInc(cnt)          do {} while (0)
12475 +#endif
12476 +
12477 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12478 +       DyDbgInc(cnt);                                                  \
12479 +       if (h_op->func) {                                               \
12480 +               if (src.func)                                           \
12481 +                       dst.func = src.func;                            \
12482 +               else                                                    \
12483 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12484 +       }                                                               \
12485 +} while (0)
12486 +
12487 +#define DySetForce(func, dst, src) do {                \
12488 +       AuDebugOn(!src.func);                   \
12489 +       DyDbgInc(cnt);                          \
12490 +       dst.func = src.func;                    \
12491 +} while (0)
12492 +
12493 +#define DySetAop(func) \
12494 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12495 +#define DySetAopForce(func) \
12496 +       DySetForce(func, dyaop->da_op, aufs_aop)
12497 +
12498 +static void dy_aop(struct au_dykey *key, const void *h_op,
12499 +                  struct super_block *h_sb __maybe_unused)
12500 +{
12501 +       struct au_dyaop *dyaop = (void *)key;
12502 +       const struct address_space_operations *h_aop = h_op;
12503 +       DyDbgDeclare(cnt);
12504 +
12505 +       AuDbg("%s\n", au_sbtype(h_sb));
12506 +
12507 +       DySetAop(writepage);
12508 +       DySetAopForce(read_folio);      /* force */
12509 +       DySetAop(writepages);
12510 +       DySetAop(dirty_folio);
12511 +       DySetAop(invalidate_folio);
12512 +       DySetAop(readahead);
12513 +       DySetAop(write_begin);
12514 +       DySetAop(write_end);
12515 +       DySetAop(bmap);
12516 +       DySetAop(release_folio);
12517 +       DySetAop(free_folio);
12518 +       /* this one will be changed according to an aufs mount option */
12519 +       DySetAop(direct_IO);
12520 +       DySetAop(migratepage);
12521 +       DySetAop(isolate_page);
12522 +       DySetAop(putback_page);
12523 +       DySetAop(launder_folio);
12524 +       DySetAop(is_partially_uptodate);
12525 +       DySetAop(is_dirty_writeback);
12526 +       DySetAop(error_remove_page);
12527 +       DySetAop(swap_activate);
12528 +       DySetAop(swap_deactivate);
12529 +       DySetAop(swap_rw);
12530 +
12531 +       DyDbgSize(cnt, *h_aop);
12532 +}
12533 +
12534 +/* ---------------------------------------------------------------------- */
12535 +
12536 +static void dy_bug(struct kref *kref)
12537 +{
12538 +       BUG();
12539 +}
12540 +
12541 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12542 +{
12543 +       struct au_dykey *key, *old;
12544 +       struct hlist_bl_head *hbl;
12545 +       struct op {
12546 +               unsigned int sz;
12547 +               void (*set)(struct au_dykey *key, const void *h_op,
12548 +                           struct super_block *h_sb __maybe_unused);
12549 +       };
12550 +       static const struct op a[] = {
12551 +               [AuDy_AOP] = {
12552 +                       .sz     = sizeof(struct au_dyaop),
12553 +                       .set    = dy_aop
12554 +               }
12555 +       };
12556 +       const struct op *p;
12557 +
12558 +       hbl = dynop + op->dy_type;
12559 +       key = dy_gfind_get(hbl, op->dy_hop);
12560 +       if (key)
12561 +               goto out_add; /* success */
12562 +
12563 +       p = a + op->dy_type;
12564 +       key = kzalloc(p->sz, GFP_NOFS);
12565 +       if (unlikely(!key)) {
12566 +               key = ERR_PTR(-ENOMEM);
12567 +               goto out;
12568 +       }
12569 +
12570 +       key->dk_op.dy_hop = op->dy_hop;
12571 +       kref_init(&key->dk_kref);
12572 +       p->set(key, op->dy_hop, au_br_sb(br));
12573 +       old = dy_gadd(hbl, key);
12574 +       if (old) {
12575 +               au_kfree_rcu(key);
12576 +               key = old;
12577 +       }
12578 +
12579 +out_add:
12580 +       old = dy_bradd(br, key);
12581 +       if (old)
12582 +               /* its ref-count should never be zero here */
12583 +               kref_put(&key->dk_kref, dy_bug);
12584 +out:
12585 +       return key;
12586 +}
12587 +
12588 +/* ---------------------------------------------------------------------- */
12589 +/*
12590 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12591 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12592 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12593 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12594 + * See the aufs manual in detail.
12595 + */
12596 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12597 +{
12598 +       if (!do_dx)
12599 +               dyaop->da_op.direct_IO = NULL;
12600 +       else
12601 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12602 +}
12603 +
12604 +static struct au_dyaop *dy_aget(struct au_branch *br,
12605 +                               const struct address_space_operations *h_aop,
12606 +                               int do_dx)
12607 +{
12608 +       struct au_dyaop *dyaop;
12609 +       struct au_dynop op;
12610 +
12611 +       op.dy_type = AuDy_AOP;
12612 +       op.dy_haop = h_aop;
12613 +       dyaop = (void *)dy_get(&op, br);
12614 +       if (IS_ERR(dyaop))
12615 +               goto out;
12616 +       dy_adx(dyaop, do_dx);
12617 +
12618 +out:
12619 +       return dyaop;
12620 +}
12621 +
12622 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12623 +               struct inode *h_inode)
12624 +{
12625 +       int err, do_dx;
12626 +       struct super_block *sb;
12627 +       struct au_branch *br;
12628 +       struct au_dyaop *dyaop;
12629 +
12630 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12631 +       IiMustWriteLock(inode);
12632 +
12633 +       sb = inode->i_sb;
12634 +       br = au_sbr(sb, bindex);
12635 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12636 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12637 +       err = PTR_ERR(dyaop);
12638 +       if (IS_ERR(dyaop))
12639 +               /* unnecessary to call dy_fput() */
12640 +               goto out;
12641 +
12642 +       err = 0;
12643 +       inode->i_mapping->a_ops = &dyaop->da_op;
12644 +
12645 +out:
12646 +       return err;
12647 +}
12648 +
12649 +/*
12650 + * Is it safe to replace a_ops during the inode/file is in operation?
12651 + * Yes, I hope so.
12652 + */
12653 +int au_dy_irefresh(struct inode *inode)
12654 +{
12655 +       int err;
12656 +       aufs_bindex_t btop;
12657 +       struct inode *h_inode;
12658 +
12659 +       err = 0;
12660 +       if (S_ISREG(inode->i_mode)) {
12661 +               btop = au_ibtop(inode);
12662 +               h_inode = au_h_iptr(inode, btop);
12663 +               err = au_dy_iaop(inode, btop, h_inode);
12664 +       }
12665 +       return err;
12666 +}
12667 +
12668 +void au_dy_arefresh(int do_dx)
12669 +{
12670 +       struct hlist_bl_head *hbl;
12671 +       struct hlist_bl_node *pos;
12672 +       struct au_dykey *key;
12673 +
12674 +       hbl = dynop + AuDy_AOP;
12675 +       hlist_bl_lock(hbl);
12676 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12677 +               dy_adx((void *)key, do_dx);
12678 +       hlist_bl_unlock(hbl);
12679 +}
12680 +
12681 +/* ---------------------------------------------------------------------- */
12682 +
12683 +void __init au_dy_init(void)
12684 +{
12685 +       int i;
12686 +
12687 +       for (i = 0; i < AuDyLast; i++)
12688 +               INIT_HLIST_BL_HEAD(dynop + i);
12689 +}
12690 +
12691 +void au_dy_fin(void)
12692 +{
12693 +       int i;
12694 +
12695 +       for (i = 0; i < AuDyLast; i++)
12696 +               WARN_ON(!hlist_bl_empty(dynop + i));
12697 +}
12698 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12699 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12700 +++ linux/fs/aufs/dynop.h       2022-08-02 12:12:45.165641939 +0200
12701 @@ -0,0 +1,77 @@
12702 +/* SPDX-License-Identifier: GPL-2.0 */
12703 +/*
12704 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12705 + *
12706 + * This program, aufs is free software; you can redistribute it and/or modify
12707 + * it under the terms of the GNU General Public License as published by
12708 + * the Free Software Foundation; either version 2 of the License, or
12709 + * (at your option) any later version.
12710 + *
12711 + * This program is distributed in the hope that it will be useful,
12712 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12713 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12714 + * GNU General Public License for more details.
12715 + *
12716 + * You should have received a copy of the GNU General Public License
12717 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12718 + */
12719 +
12720 +/*
12721 + * dynamically customizable operations (for regular files only)
12722 + */
12723 +
12724 +#ifndef __AUFS_DYNOP_H__
12725 +#define __AUFS_DYNOP_H__
12726 +
12727 +#ifdef __KERNEL__
12728 +
12729 +#include <linux/fs.h>
12730 +#include <linux/kref.h>
12731 +
12732 +enum {AuDy_AOP, AuDyLast};
12733 +
12734 +struct au_dynop {
12735 +       int                                             dy_type;
12736 +       union {
12737 +               const void                              *dy_hop;
12738 +               const struct address_space_operations   *dy_haop;
12739 +       };
12740 +};
12741 +
12742 +struct au_dykey {
12743 +       union {
12744 +               struct hlist_bl_node    dk_hnode;
12745 +               struct rcu_head         dk_rcu;
12746 +       };
12747 +       struct au_dynop         dk_op;
12748 +
12749 +       /*
12750 +        * during I am in the branch local array, kref is gotten. when the
12751 +        * branch is removed, kref is put.
12752 +        */
12753 +       struct kref             dk_kref;
12754 +};
12755 +
12756 +/* stop unioning since their sizes are very different from each other */
12757 +struct au_dyaop {
12758 +       struct au_dykey                 da_key;
12759 +       struct address_space_operations da_op; /* not const */
12760 +};
12761 +/* make sure that 'struct au_dykey *' can be any type */
12762 +static_assert(!offsetof(struct au_dyaop, da_key));
12763 +
12764 +/* ---------------------------------------------------------------------- */
12765 +
12766 +/* dynop.c */
12767 +struct au_branch;
12768 +void au_dy_put(struct au_dykey *key);
12769 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12770 +               struct inode *h_inode);
12771 +int au_dy_irefresh(struct inode *inode);
12772 +void au_dy_arefresh(int do_dio);
12773 +
12774 +void __init au_dy_init(void);
12775 +void au_dy_fin(void);
12776 +
12777 +#endif /* __KERNEL__ */
12778 +#endif /* __AUFS_DYNOP_H__ */
12779 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12780 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12781 +++ linux/fs/aufs/export.c      2022-08-02 12:12:45.165641939 +0200
12782 @@ -0,0 +1,830 @@
12783 +// SPDX-License-Identifier: GPL-2.0
12784 +/*
12785 + * Copyright (C) 2005-2021 Junjiro R. Okajima
12786 + *
12787 + * This program, aufs is free software; you can redistribute it and/or modify
12788 + * it under the terms of the GNU General Public License as published by
12789 + * the Free Software Foundation; either version 2 of the License, or
12790 + * (at your option) any later version.
12791 + *
12792 + * This program is distributed in the hope that it will be useful,
12793 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12794 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12795 + * GNU General Public License for more details.
12796 + *
12797 + * You should have received a copy of the GNU General Public License
12798 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12799 + */
12800 +
12801 +/*
12802 + * export via nfs
12803 + */
12804 +
12805 +#include <linux/exportfs.h>
12806 +#include <linux/fs_struct.h>
12807 +#include <linux/nsproxy.h>
12808 +#include <linux/random.h>
12809 +#include <linux/writeback.h>
12810 +#include "aufs.h"
12811 +
12812 +union conv {
12813 +#ifdef CONFIG_AUFS_INO_T_64
12814 +       __u32 a[2];
12815 +#else
12816 +       __u32 a[1];
12817 +#endif
12818 +       ino_t ino;
12819 +};
12820 +
12821 +static ino_t decode_ino(__u32 *a)
12822 +{
12823 +       union conv u;
12824 +
12825 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12826 +       u.a[0] = a[0];
12827 +#ifdef CONFIG_AUFS_INO_T_64
12828 +       u.a[1] = a[1];
12829 +#endif
12830 +       return u.ino;
12831 +}
12832 +
12833 +static void encode_ino(__u32 *a, ino_t ino)
12834 +{
12835 +       union conv u;
12836 +
12837 +       u.ino = ino;
12838 +       a[0] = u.a[0];
12839 +#ifdef CONFIG_AUFS_INO_T_64
12840 +       a[1] = u.a[1];
12841 +#endif
12842 +}
12843 +
12844 +/* NFS file handle */
12845 +enum {
12846 +       Fh_br_id,
12847 +       Fh_sigen,
12848 +#ifdef CONFIG_AUFS_INO_T_64
12849 +       /* support 64bit inode number */
12850 +       Fh_ino1,
12851 +       Fh_ino2,
12852 +       Fh_dir_ino1,
12853 +       Fh_dir_ino2,
12854 +#else
12855 +       Fh_ino1,
12856 +       Fh_dir_ino1,
12857 +#endif
12858 +       Fh_igen,
12859 +       Fh_h_type,
12860 +       Fh_tail,
12861 +
12862 +       Fh_ino = Fh_ino1,
12863 +       Fh_dir_ino = Fh_dir_ino1
12864 +};
12865 +
12866 +static int au_test_anon(struct dentry *dentry)
12867 +{
12868 +       /* note: read d_flags without d_lock */
12869 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12870 +}
12871 +
12872 +int au_test_nfsd(void)
12873 +{
12874 +       int ret;
12875 +       struct task_struct *tsk = current;
12876 +       char comm[sizeof(tsk->comm)];
12877 +
12878 +       ret = 0;
12879 +       if (tsk->flags & PF_KTHREAD) {
12880 +               get_task_comm(comm, tsk);
12881 +               ret = !strcmp(comm, "nfsd");
12882 +       }
12883 +
12884 +       return ret;
12885 +}
12886 +
12887 +/* ---------------------------------------------------------------------- */
12888 +/* inode generation external table */
12889 +
12890 +void au_xigen_inc(struct inode *inode)
12891 +{
12892 +       loff_t pos;
12893 +       ssize_t sz;
12894 +       __u32 igen;
12895 +       struct super_block *sb;
12896 +       struct au_sbinfo *sbinfo;
12897 +
12898 +       sb = inode->i_sb;
12899 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12900 +
12901 +       sbinfo = au_sbi(sb);
12902 +       pos = inode->i_ino;
12903 +       pos *= sizeof(igen);
12904 +       igen = inode->i_generation + 1;
12905 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12906 +       if (sz == sizeof(igen))
12907 +               return; /* success */
12908 +
12909 +       if (unlikely(sz >= 0))
12910 +               AuIOErr("xigen error (%zd)\n", sz);
12911 +}
12912 +
12913 +int au_xigen_new(struct inode *inode)
12914 +{
12915 +       int err;
12916 +       loff_t pos;
12917 +       ssize_t sz;
12918 +       struct super_block *sb;
12919 +       struct au_sbinfo *sbinfo;
12920 +       struct file *file;
12921 +
12922 +       err = 0;
12923 +       /* todo: dirty, at mount time */
12924 +       if (inode->i_ino == AUFS_ROOT_INO)
12925 +               goto out;
12926 +       sb = inode->i_sb;
12927 +       SiMustAnyLock(sb);
12928 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12929 +               goto out;
12930 +
12931 +       err = -EFBIG;
12932 +       pos = inode->i_ino;
12933 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12934 +               AuIOErr1("too large i%lld\n", pos);
12935 +               goto out;
12936 +       }
12937 +       pos *= sizeof(inode->i_generation);
12938 +
12939 +       err = 0;
12940 +       sbinfo = au_sbi(sb);
12941 +       file = sbinfo->si_xigen;
12942 +       BUG_ON(!file);
12943 +
12944 +       if (vfsub_f_size_read(file)
12945 +           < pos + sizeof(inode->i_generation)) {
12946 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12947 +               sz = xino_fwrite(file, &inode->i_generation,
12948 +                                sizeof(inode->i_generation), &pos);
12949 +       } else
12950 +               sz = xino_fread(file, &inode->i_generation,
12951 +                               sizeof(inode->i_generation), &pos);
12952 +       if (sz == sizeof(inode->i_generation))
12953 +               goto out; /* success */
12954 +
12955 +       err = sz;
12956 +       if (unlikely(sz >= 0)) {
12957 +               err = -EIO;
12958 +               AuIOErr("xigen error (%zd)\n", sz);
12959 +       }
12960 +
12961 +out:
12962 +       return err;
12963 +}
12964 +
12965 +int au_xigen_set(struct super_block *sb, struct path *path)
12966 +{
12967 +       int err;
12968 +       struct au_sbinfo *sbinfo;
12969 +       struct file *file;
12970 +
12971 +       SiMustWriteLock(sb);
12972 +
12973 +       sbinfo = au_sbi(sb);
12974 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12975 +       err = PTR_ERR(file);
12976 +       if (IS_ERR(file))
12977 +               goto out;
12978 +       err = 0;
12979 +       if (sbinfo->si_xigen)
12980 +               fput(sbinfo->si_xigen);
12981 +       sbinfo->si_xigen = file;
12982 +
12983 +out:
12984 +       AuTraceErr(err);
12985 +       return err;
12986 +}
12987 +
12988 +void au_xigen_clr(struct super_block *sb)
12989 +{
12990 +       struct au_sbinfo *sbinfo;
12991 +
12992 +       SiMustWriteLock(sb);
12993 +
12994 +       sbinfo = au_sbi(sb);
12995 +       if (sbinfo->si_xigen) {
12996 +               fput(sbinfo->si_xigen);
12997 +               sbinfo->si_xigen = NULL;
12998 +       }
12999 +}
13000 +
13001 +/* ---------------------------------------------------------------------- */
13002 +
13003 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13004 +                                   ino_t dir_ino)
13005 +{
13006 +       struct dentry *dentry, *d;
13007 +       struct inode *inode;
13008 +       unsigned int sigen;
13009 +
13010 +       dentry = NULL;
13011 +       inode = ilookup(sb, ino);
13012 +       if (!inode)
13013 +               goto out;
13014 +
13015 +       dentry = ERR_PTR(-ESTALE);
13016 +       sigen = au_sigen(sb);
13017 +       if (unlikely(au_is_bad_inode(inode)
13018 +                    || IS_DEADDIR(inode)
13019 +                    || sigen != au_iigen(inode, NULL)))
13020 +               goto out_iput;
13021 +
13022 +       dentry = NULL;
13023 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13024 +               dentry = d_find_alias(inode);
13025 +       else {
13026 +               spin_lock(&inode->i_lock);
13027 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13028 +                       spin_lock(&d->d_lock);
13029 +                       if (!au_test_anon(d)
13030 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13031 +                               dentry = dget_dlock(d);
13032 +                               spin_unlock(&d->d_lock);
13033 +                               break;
13034 +                       }
13035 +                       spin_unlock(&d->d_lock);
13036 +               }
13037 +               spin_unlock(&inode->i_lock);
13038 +       }
13039 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13040 +               /* need to refresh */
13041 +               dput(dentry);
13042 +               dentry = NULL;
13043 +       }
13044 +
13045 +out_iput:
13046 +       iput(inode);
13047 +out:
13048 +       AuTraceErrPtr(dentry);
13049 +       return dentry;
13050 +}
13051 +
13052 +/* ---------------------------------------------------------------------- */
13053 +
13054 +/* todo: dirty? */
13055 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13056 +
13057 +struct au_compare_mnt_args {
13058 +       /* input */
13059 +       struct super_block *sb;
13060 +
13061 +       /* output */
13062 +       struct vfsmount *mnt;
13063 +};
13064 +
13065 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13066 +{
13067 +       struct au_compare_mnt_args *a = arg;
13068 +
13069 +       if (mnt->mnt_sb != a->sb)
13070 +               return 0;
13071 +       a->mnt = mntget(mnt);
13072 +       return 1;
13073 +}
13074 +
13075 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13076 +{
13077 +       int err;
13078 +       struct path root;
13079 +       struct au_compare_mnt_args args = {
13080 +               .sb = sb
13081 +       };
13082 +
13083 +       get_fs_root(current->fs, &root);
13084 +       rcu_read_lock();
13085 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13086 +       rcu_read_unlock();
13087 +       path_put(&root);
13088 +       AuDebugOn(!err);
13089 +       AuDebugOn(!args.mnt);
13090 +       return args.mnt;
13091 +}
13092 +
13093 +struct au_nfsd_si_lock {
13094 +       unsigned int sigen;
13095 +       aufs_bindex_t bindex, br_id;
13096 +       unsigned char force_lock;
13097 +};
13098 +
13099 +static int si_nfsd_read_lock(struct super_block *sb,
13100 +                            struct au_nfsd_si_lock *nsi_lock)
13101 +{
13102 +       int err;
13103 +       aufs_bindex_t bindex;
13104 +
13105 +       si_read_lock(sb, AuLock_FLUSH);
13106 +
13107 +       /* branch id may be wrapped around */
13108 +       err = 0;
13109 +       bindex = au_br_index(sb, nsi_lock->br_id);
13110 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13111 +               goto out; /* success */
13112 +
13113 +       err = -ESTALE;
13114 +       bindex = -1;
13115 +       if (!nsi_lock->force_lock)
13116 +               si_read_unlock(sb);
13117 +
13118 +out:
13119 +       nsi_lock->bindex = bindex;
13120 +       return err;
13121 +}
13122 +
13123 +struct find_name_by_ino {
13124 +       struct dir_context ctx;
13125 +       int called, found;
13126 +       ino_t ino;
13127 +       char *name;
13128 +       int namelen;
13129 +};
13130 +
13131 +static int
13132 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13133 +                loff_t offset, u64 ino, unsigned int d_type)
13134 +{
13135 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13136 +                                                 ctx);
13137 +
13138 +       a->called++;
13139 +       if (a->ino != ino)
13140 +               return 0;
13141 +
13142 +       memcpy(a->name, name, namelen);
13143 +       a->namelen = namelen;
13144 +       a->found = 1;
13145 +       return 1;
13146 +}
13147 +
13148 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13149 +                                    struct au_nfsd_si_lock *nsi_lock)
13150 +{
13151 +       struct dentry *dentry, *parent;
13152 +       struct file *file;
13153 +       struct inode *dir;
13154 +       struct find_name_by_ino arg = {
13155 +               .ctx = {
13156 +                       .actor = find_name_by_ino
13157 +               }
13158 +       };
13159 +       int err;
13160 +
13161 +       parent = path->dentry;
13162 +       if (nsi_lock)
13163 +               si_read_unlock(parent->d_sb);
13164 +       file = vfsub_dentry_open(path, au_dir_roflags);
13165 +       dentry = (void *)file;
13166 +       if (IS_ERR(file))
13167 +               goto out;
13168 +
13169 +       dentry = ERR_PTR(-ENOMEM);
13170 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13171 +       if (unlikely(!arg.name))
13172 +               goto out_file;
13173 +       arg.ino = ino;
13174 +       arg.found = 0;
13175 +       do {
13176 +               arg.called = 0;
13177 +               /* smp_mb(); */
13178 +               err = vfsub_iterate_dir(file, &arg.ctx);
13179 +       } while (!err && !arg.found && arg.called);
13180 +       dentry = ERR_PTR(err);
13181 +       if (unlikely(err))
13182 +               goto out_name;
13183 +       /* instead of ENOENT */
13184 +       dentry = ERR_PTR(-ESTALE);
13185 +       if (!arg.found)
13186 +               goto out_name;
13187 +
13188 +       /* do not call vfsub_lkup_one() */
13189 +       dir = d_inode(parent);
13190 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13191 +       AuTraceErrPtr(dentry);
13192 +       if (IS_ERR(dentry))
13193 +               goto out_name;
13194 +       AuDebugOn(au_test_anon(dentry));
13195 +       if (unlikely(d_really_is_negative(dentry))) {
13196 +               dput(dentry);
13197 +               dentry = ERR_PTR(-ENOENT);
13198 +       }
13199 +
13200 +out_name:
13201 +       free_page((unsigned long)arg.name);
13202 +out_file:
13203 +       fput(file);
13204 +out:
13205 +       if (unlikely(nsi_lock
13206 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13207 +               if (!IS_ERR(dentry)) {
13208 +                       dput(dentry);
13209 +                       dentry = ERR_PTR(-ESTALE);
13210 +               }
13211 +       AuTraceErrPtr(dentry);
13212 +       return dentry;
13213 +}
13214 +
13215 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13216 +                                       ino_t dir_ino,
13217 +                                       struct au_nfsd_si_lock *nsi_lock)
13218 +{
13219 +       struct dentry *dentry;
13220 +       struct path path;
13221 +
13222 +       if (dir_ino != AUFS_ROOT_INO) {
13223 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13224 +               dentry = path.dentry;
13225 +               if (!path.dentry || IS_ERR(path.dentry))
13226 +                       goto out;
13227 +               AuDebugOn(au_test_anon(path.dentry));
13228 +       } else
13229 +               path.dentry = dget(sb->s_root);
13230 +
13231 +       path.mnt = au_mnt_get(sb);
13232 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13233 +       path_put(&path);
13234 +
13235 +out:
13236 +       AuTraceErrPtr(dentry);
13237 +       return dentry;
13238 +}
13239 +
13240 +/* ---------------------------------------------------------------------- */
13241 +
13242 +static int h_acceptable(void *expv, struct dentry *dentry)
13243 +{
13244 +       return 1;
13245 +}
13246 +
13247 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13248 +                          char *buf, int len, struct super_block *sb)
13249 +{
13250 +       char *p;
13251 +       int n;
13252 +       struct path path;
13253 +
13254 +       p = d_path(h_rootpath, buf, len);
13255 +       if (IS_ERR(p))
13256 +               goto out;
13257 +       n = strlen(p);
13258 +
13259 +       path.mnt = h_rootpath->mnt;
13260 +       path.dentry = h_parent;
13261 +       p = d_path(&path, buf, len);
13262 +       if (IS_ERR(p))
13263 +               goto out;
13264 +       if (n != 1)
13265 +               p += n;
13266 +
13267 +       path.mnt = au_mnt_get(sb);
13268 +       path.dentry = sb->s_root;
13269 +       p = d_path(&path, buf, len - strlen(p));
13270 +       mntput(path.mnt);
13271 +       if (IS_ERR(p))
13272 +               goto out;
13273 +       if (n != 1)
13274 +               p[strlen(p)] = '/';
13275 +
13276 +out:
13277 +       AuTraceErrPtr(p);
13278 +       return p;
13279 +}
13280 +
13281 +static
13282 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13283 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13284 +{
13285 +       struct dentry *dentry, *h_parent, *root;
13286 +       struct super_block *h_sb;
13287 +       char *pathname, *p;
13288 +       struct vfsmount *h_mnt;
13289 +       struct au_branch *br;
13290 +       int err;
13291 +       struct path path;
13292 +
13293 +       br = au_sbr(sb, nsi_lock->bindex);
13294 +       h_mnt = au_br_mnt(br);
13295 +       h_sb = h_mnt->mnt_sb;
13296 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13297 +       lockdep_off();
13298 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13299 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13300 +                                     h_acceptable, /*context*/NULL);
13301 +       lockdep_on();
13302 +       dentry = h_parent;
13303 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13304 +               AuWarn1("%s decode_fh failed, %ld\n",
13305 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13306 +               goto out;
13307 +       }
13308 +       dentry = NULL;
13309 +       if (unlikely(au_test_anon(h_parent))) {
13310 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13311 +                       au_sbtype(h_sb));
13312 +               goto out_h_parent;
13313 +       }
13314 +
13315 +       dentry = ERR_PTR(-ENOMEM);
13316 +       pathname = (void *)__get_free_page(GFP_NOFS);
13317 +       if (unlikely(!pathname))
13318 +               goto out_h_parent;
13319 +
13320 +       root = sb->s_root;
13321 +       path.mnt = h_mnt;
13322 +       di_read_lock_parent(root, !AuLock_IR);
13323 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13324 +       di_read_unlock(root, !AuLock_IR);
13325 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13326 +       dentry = (void *)p;
13327 +       if (IS_ERR(p))
13328 +               goto out_pathname;
13329 +
13330 +       si_read_unlock(sb);
13331 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13332 +       dentry = ERR_PTR(err);
13333 +       if (unlikely(err))
13334 +               goto out_relock;
13335 +
13336 +       dentry = ERR_PTR(-ENOENT);
13337 +       AuDebugOn(au_test_anon(path.dentry));
13338 +       if (unlikely(d_really_is_negative(path.dentry)))
13339 +               goto out_path;
13340 +
13341 +       if (ino != d_inode(path.dentry)->i_ino)
13342 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13343 +       else
13344 +               dentry = dget(path.dentry);
13345 +
13346 +out_path:
13347 +       path_put(&path);
13348 +out_relock:
13349 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13350 +               if (!IS_ERR(dentry)) {
13351 +                       dput(dentry);
13352 +                       dentry = ERR_PTR(-ESTALE);
13353 +               }
13354 +out_pathname:
13355 +       free_page((unsigned long)pathname);
13356 +out_h_parent:
13357 +       dput(h_parent);
13358 +out:
13359 +       AuTraceErrPtr(dentry);
13360 +       return dentry;
13361 +}
13362 +
13363 +/* ---------------------------------------------------------------------- */
13364 +
13365 +static struct dentry *
13366 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13367 +                 int fh_type)
13368 +{
13369 +       struct dentry *dentry;
13370 +       __u32 *fh = fid->raw;
13371 +       struct au_branch *br;
13372 +       ino_t ino, dir_ino;
13373 +       struct au_nfsd_si_lock nsi_lock = {
13374 +               .force_lock     = 0
13375 +       };
13376 +
13377 +       dentry = ERR_PTR(-ESTALE);
13378 +       /* it should never happen, but the file handle is unreliable */
13379 +       if (unlikely(fh_len < Fh_tail))
13380 +               goto out;
13381 +       nsi_lock.sigen = fh[Fh_sigen];
13382 +       nsi_lock.br_id = fh[Fh_br_id];
13383 +
13384 +       /* branch id may be wrapped around */
13385 +       br = NULL;
13386 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13387 +               goto out;
13388 +       nsi_lock.force_lock = 1;
13389 +
13390 +       /* is this inode still cached? */
13391 +       ino = decode_ino(fh + Fh_ino);
13392 +       /* it should never happen */
13393 +       if (unlikely(ino == AUFS_ROOT_INO))
13394 +               goto out_unlock;
13395 +
13396 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13397 +       dentry = decode_by_ino(sb, ino, dir_ino);
13398 +       if (IS_ERR(dentry))
13399 +               goto out_unlock;
13400 +       if (dentry)
13401 +               goto accept;
13402 +
13403 +       /* is the parent dir cached? */
13404 +       br = au_sbr(sb, nsi_lock.bindex);
13405 +       au_lcnt_inc(&br->br_nfiles);
13406 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13407 +       if (IS_ERR(dentry))
13408 +               goto out_unlock;
13409 +       if (dentry)
13410 +               goto accept;
13411 +
13412 +       /* lookup path */
13413 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13414 +       if (IS_ERR(dentry))
13415 +               goto out_unlock;
13416 +       if (unlikely(!dentry))
13417 +               /* todo?: make it ESTALE */
13418 +               goto out_unlock;
13419 +
13420 +accept:
13421 +       if (!au_digen_test(dentry, au_sigen(sb))
13422 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13423 +               goto out_unlock; /* success */
13424 +
13425 +       dput(dentry);
13426 +       dentry = ERR_PTR(-ESTALE);
13427 +out_unlock:
13428 +       if (br)
13429 +               au_lcnt_dec(&br->br_nfiles);
13430 +       si_read_unlock(sb);
13431 +out:
13432 +       AuTraceErrPtr(dentry);
13433 +       return dentry;
13434 +}
13435 +
13436 +#if 0 /* reserved for future use */
13437 +/* support subtreecheck option */
13438 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13439 +                                       int fh_len, int fh_type)
13440 +{
13441 +       struct dentry *parent;
13442 +       __u32 *fh = fid->raw;
13443 +       ino_t dir_ino;
13444 +
13445 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13446 +       parent = decode_by_ino(sb, dir_ino, 0);
13447 +       if (IS_ERR(parent))
13448 +               goto out;
13449 +       if (!parent)
13450 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13451 +                                       dir_ino, fh, fh_len);
13452 +
13453 +out:
13454 +       AuTraceErrPtr(parent);
13455 +       return parent;
13456 +}
13457 +#endif
13458 +
13459 +/* ---------------------------------------------------------------------- */
13460 +
13461 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13462 +                         struct inode *dir)
13463 +{
13464 +       int err;
13465 +       aufs_bindex_t bindex;
13466 +       struct super_block *sb, *h_sb;
13467 +       struct dentry *dentry, *parent, *h_parent;
13468 +       struct inode *h_dir;
13469 +       struct au_branch *br;
13470 +
13471 +       err = -ENOSPC;
13472 +       if (unlikely(*max_len <= Fh_tail)) {
13473 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13474 +               goto out;
13475 +       }
13476 +
13477 +       err = FILEID_ROOT;
13478 +       if (inode->i_ino == AUFS_ROOT_INO) {
13479 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13480 +               goto out;
13481 +       }
13482 +
13483 +       h_parent = NULL;
13484 +       sb = inode->i_sb;
13485 +       err = si_read_lock(sb, AuLock_FLUSH);
13486 +       if (unlikely(err))
13487 +               goto out;
13488 +
13489 +#ifdef CONFIG_AUFS_DEBUG
13490 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13491 +               AuWarn1("NFS-exporting requires xino\n");
13492 +#endif
13493 +       err = -EIO;
13494 +       parent = NULL;
13495 +       ii_read_lock_child(inode);
13496 +       bindex = au_ibtop(inode);
13497 +       if (!dir) {
13498 +               dentry = d_find_any_alias(inode);
13499 +               if (unlikely(!dentry))
13500 +                       goto out_unlock;
13501 +               AuDebugOn(au_test_anon(dentry));
13502 +               parent = dget_parent(dentry);
13503 +               dput(dentry);
13504 +               if (unlikely(!parent))
13505 +                       goto out_unlock;
13506 +               if (d_really_is_positive(parent))
13507 +                       dir = d_inode(parent);
13508 +       }
13509 +
13510 +       ii_read_lock_parent(dir);
13511 +       h_dir = au_h_iptr(dir, bindex);
13512 +       ii_read_unlock(dir);
13513 +       if (unlikely(!h_dir))
13514 +               goto out_parent;
13515 +       h_parent = d_find_any_alias(h_dir);
13516 +       if (unlikely(!h_parent))
13517 +               goto out_hparent;
13518 +
13519 +       err = -EPERM;
13520 +       br = au_sbr(sb, bindex);
13521 +       h_sb = au_br_sb(br);
13522 +       if (unlikely(!h_sb->s_export_op)) {
13523 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13524 +               goto out_hparent;
13525 +       }
13526 +
13527 +       fh[Fh_br_id] = br->br_id;
13528 +       fh[Fh_sigen] = au_sigen(sb);
13529 +       encode_ino(fh + Fh_ino, inode->i_ino);
13530 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13531 +       fh[Fh_igen] = inode->i_generation;
13532 +
13533 +       *max_len -= Fh_tail;
13534 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13535 +                                          max_len,
13536 +                                          /*connectable or subtreecheck*/0);
13537 +       err = fh[Fh_h_type];
13538 +       *max_len += Fh_tail;
13539 +       /* todo: macros? */
13540 +       if (err != FILEID_INVALID)
13541 +               err = 99;
13542 +       else
13543 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13544 +
13545 +out_hparent:
13546 +       dput(h_parent);
13547 +out_parent:
13548 +       dput(parent);
13549 +out_unlock:
13550 +       ii_read_unlock(inode);
13551 +       si_read_unlock(sb);
13552 +out:
13553 +       if (unlikely(err < 0))
13554 +               err = FILEID_INVALID;
13555 +       return err;
13556 +}
13557 +
13558 +/* ---------------------------------------------------------------------- */
13559 +
13560 +static int aufs_commit_metadata(struct inode *inode)
13561 +{
13562 +       int err;
13563 +       aufs_bindex_t bindex;
13564 +       struct super_block *sb;
13565 +       struct inode *h_inode;
13566 +       int (*f)(struct inode *inode);
13567 +
13568 +       sb = inode->i_sb;
13569 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13570 +       ii_write_lock_child(inode);
13571 +       bindex = au_ibtop(inode);
13572 +       AuDebugOn(bindex < 0);
13573 +       h_inode = au_h_iptr(inode, bindex);
13574 +
13575 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13576 +       if (f)
13577 +               err = f(h_inode);
13578 +       else
13579 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13580 +
13581 +       au_cpup_attr_timesizes(inode);
13582 +       ii_write_unlock(inode);
13583 +       si_read_unlock(sb);
13584 +       return err;
13585 +}
13586 +
13587 +/* ---------------------------------------------------------------------- */
13588 +
13589 +static struct export_operations aufs_export_op = {
13590 +       .fh_to_dentry           = aufs_fh_to_dentry,
13591 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13592 +       .encode_fh              = aufs_encode_fh,
13593 +       .commit_metadata        = aufs_commit_metadata
13594 +};
13595 +
13596 +void au_export_init(struct super_block *sb)
13597 +{
13598 +       struct au_sbinfo *sbinfo;
13599 +       __u32 u;
13600 +
13601 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13602 +                        && IS_MODULE(CONFIG_EXPORTFS),
13603 +                        AUFS_NAME ": unsupported configuration "
13604 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13605 +
13606 +       sb->s_export_op = &aufs_export_op;
13607 +       sbinfo = au_sbi(sb);
13608 +       sbinfo->si_xigen = NULL;
13609 +       get_random_bytes(&u, sizeof(u));
13610 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13611 +       atomic_set(&sbinfo->si_xigen_next, u);
13612 +}
13613 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13614 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13615 +++ linux/fs/aufs/fhsm.c        2022-08-02 12:12:45.165641939 +0200
13616 @@ -0,0 +1,427 @@
13617 +// SPDX-License-Identifier: GPL-2.0
13618 +/*
13619 + * Copyright (C) 2011-2021 Junjiro R. Okajima
13620 + *
13621 + * This program, aufs is free software; you can redistribute it and/or modify
13622 + * it under the terms of the GNU General Public License as published by
13623 + * the Free Software Foundation; either version 2 of the License, or
13624 + * (at your option) any later version.
13625 + *
13626 + * This program is distributed in the hope that it will be useful,
13627 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13628 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13629 + * GNU General Public License for more details.
13630 + *
13631 + * You should have received a copy of the GNU General Public License
13632 + * along with this program; if not, write to the Free Software
13633 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13634 + */
13635 +
13636 +/*
13637 + * File-based Hierarchy Storage Management
13638 + */
13639 +
13640 +#include <linux/anon_inodes.h>
13641 +#include <linux/poll.h>
13642 +#include <linux/seq_file.h>
13643 +#include <linux/statfs.h>
13644 +#include "aufs.h"
13645 +
13646 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13647 +{
13648 +       struct au_sbinfo *sbinfo;
13649 +       struct au_fhsm *fhsm;
13650 +
13651 +       SiMustAnyLock(sb);
13652 +
13653 +       sbinfo = au_sbi(sb);
13654 +       fhsm = &sbinfo->si_fhsm;
13655 +       AuDebugOn(!fhsm);
13656 +       return fhsm->fhsm_bottom;
13657 +}
13658 +
13659 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13660 +{
13661 +       struct au_sbinfo *sbinfo;
13662 +       struct au_fhsm *fhsm;
13663 +
13664 +       SiMustWriteLock(sb);
13665 +
13666 +       sbinfo = au_sbi(sb);
13667 +       fhsm = &sbinfo->si_fhsm;
13668 +       AuDebugOn(!fhsm);
13669 +       fhsm->fhsm_bottom = bindex;
13670 +}
13671 +
13672 +/* ---------------------------------------------------------------------- */
13673 +
13674 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13675 +{
13676 +       struct au_br_fhsm *bf;
13677 +
13678 +       bf = br->br_fhsm;
13679 +       MtxMustLock(&bf->bf_lock);
13680 +
13681 +       return !bf->bf_readable
13682 +               || time_after(jiffies,
13683 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13684 +}
13685 +
13686 +/* ---------------------------------------------------------------------- */
13687 +
13688 +static void au_fhsm_notify(struct super_block *sb, int val)
13689 +{
13690 +       struct au_sbinfo *sbinfo;
13691 +       struct au_fhsm *fhsm;
13692 +
13693 +       SiMustAnyLock(sb);
13694 +
13695 +       sbinfo = au_sbi(sb);
13696 +       fhsm = &sbinfo->si_fhsm;
13697 +       if (au_fhsm_pid(fhsm)
13698 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13699 +               atomic_set(&fhsm->fhsm_readable, val);
13700 +               if (val)
13701 +                       wake_up(&fhsm->fhsm_wqh);
13702 +       }
13703 +}
13704 +
13705 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13706 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13707 +{
13708 +       int err;
13709 +       struct au_branch *br;
13710 +       struct au_br_fhsm *bf;
13711 +
13712 +       br = au_sbr(sb, bindex);
13713 +       AuDebugOn(au_br_rdonly(br));
13714 +       bf = br->br_fhsm;
13715 +       AuDebugOn(!bf);
13716 +
13717 +       if (do_lock)
13718 +               mutex_lock(&bf->bf_lock);
13719 +       else
13720 +               MtxMustLock(&bf->bf_lock);
13721 +
13722 +       /* sb->s_root for NFS is unreliable */
13723 +       err = au_br_stfs(br, &bf->bf_stfs);
13724 +       if (unlikely(err)) {
13725 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13726 +               goto out;
13727 +       }
13728 +
13729 +       bf->bf_jiffy = jiffies;
13730 +       bf->bf_readable = 1;
13731 +       if (do_notify)
13732 +               au_fhsm_notify(sb, /*val*/1);
13733 +       if (rstfs)
13734 +               *rstfs = bf->bf_stfs;
13735 +
13736 +out:
13737 +       if (do_lock)
13738 +               mutex_unlock(&bf->bf_lock);
13739 +       au_fhsm_notify(sb, /*val*/1);
13740 +
13741 +       return err;
13742 +}
13743 +
13744 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13745 +{
13746 +       int err;
13747 +       struct au_sbinfo *sbinfo;
13748 +       struct au_fhsm *fhsm;
13749 +       struct au_branch *br;
13750 +       struct au_br_fhsm *bf;
13751 +
13752 +       AuDbg("b%d, force %d\n", bindex, force);
13753 +       SiMustAnyLock(sb);
13754 +
13755 +       sbinfo = au_sbi(sb);
13756 +       fhsm = &sbinfo->si_fhsm;
13757 +       if (!au_ftest_si(sbinfo, FHSM)
13758 +           || fhsm->fhsm_bottom == bindex)
13759 +               return;
13760 +
13761 +       br = au_sbr(sb, bindex);
13762 +       bf = br->br_fhsm;
13763 +       AuDebugOn(!bf);
13764 +       mutex_lock(&bf->bf_lock);
13765 +       if (force
13766 +           || au_fhsm_pid(fhsm)
13767 +           || au_fhsm_test_jiffy(sbinfo, br))
13768 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13769 +                                 /*do_notify*/1);
13770 +       mutex_unlock(&bf->bf_lock);
13771 +}
13772 +
13773 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13774 +{
13775 +       aufs_bindex_t bindex, bbot;
13776 +       struct au_branch *br;
13777 +
13778 +       /* exclude the bottom */
13779 +       bbot = au_fhsm_bottom(sb);
13780 +       for (bindex = 0; bindex < bbot; bindex++) {
13781 +               br = au_sbr(sb, bindex);
13782 +               if (au_br_fhsm(br->br_perm))
13783 +                       au_fhsm_wrote(sb, bindex, force);
13784 +       }
13785 +}
13786 +
13787 +/* ---------------------------------------------------------------------- */
13788 +
13789 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13790 +{
13791 +       __poll_t mask;
13792 +       struct au_sbinfo *sbinfo;
13793 +       struct au_fhsm *fhsm;
13794 +
13795 +       mask = 0;
13796 +       sbinfo = file->private_data;
13797 +       fhsm = &sbinfo->si_fhsm;
13798 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13799 +       if (atomic_read(&fhsm->fhsm_readable))
13800 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13801 +
13802 +       if (!mask)
13803 +               AuDbg("mask 0x%x\n", mask);
13804 +       return mask;
13805 +}
13806 +
13807 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13808 +                             struct aufs_stfs *stfs, __s16 brid)
13809 +{
13810 +       int err;
13811 +
13812 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13813 +       if (!err)
13814 +               err = __put_user(brid, &stbr->brid);
13815 +       if (unlikely(err))
13816 +               err = -EFAULT;
13817 +
13818 +       return err;
13819 +}
13820 +
13821 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13822 +                              struct aufs_stbr __user *stbr, size_t count)
13823 +{
13824 +       ssize_t err;
13825 +       int nstbr;
13826 +       aufs_bindex_t bindex, bbot;
13827 +       struct au_branch *br;
13828 +       struct au_br_fhsm *bf;
13829 +
13830 +       /* except the bottom branch */
13831 +       err = 0;
13832 +       nstbr = 0;
13833 +       bbot = au_fhsm_bottom(sb);
13834 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13835 +               br = au_sbr(sb, bindex);
13836 +               if (!au_br_fhsm(br->br_perm))
13837 +                       continue;
13838 +
13839 +               bf = br->br_fhsm;
13840 +               mutex_lock(&bf->bf_lock);
13841 +               if (bf->bf_readable) {
13842 +                       err = -EFAULT;
13843 +                       if (count >= sizeof(*stbr))
13844 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13845 +                                                         br->br_id);
13846 +                       if (!err) {
13847 +                               bf->bf_readable = 0;
13848 +                               count -= sizeof(*stbr);
13849 +                               nstbr++;
13850 +                       }
13851 +               }
13852 +               mutex_unlock(&bf->bf_lock);
13853 +       }
13854 +       if (!err)
13855 +               err = sizeof(*stbr) * nstbr;
13856 +
13857 +       return err;
13858 +}
13859 +
13860 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13861 +                          loff_t *pos)
13862 +{
13863 +       ssize_t err;
13864 +       int readable;
13865 +       aufs_bindex_t nfhsm, bindex, bbot;
13866 +       struct au_sbinfo *sbinfo;
13867 +       struct au_fhsm *fhsm;
13868 +       struct au_branch *br;
13869 +       struct super_block *sb;
13870 +
13871 +       err = 0;
13872 +       sbinfo = file->private_data;
13873 +       fhsm = &sbinfo->si_fhsm;
13874 +need_data:
13875 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13876 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13877 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13878 +                       err = -EAGAIN;
13879 +               else
13880 +                       err = wait_event_interruptible_locked_irq
13881 +                               (fhsm->fhsm_wqh,
13882 +                                atomic_read(&fhsm->fhsm_readable));
13883 +       }
13884 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13885 +       if (unlikely(err))
13886 +               goto out;
13887 +
13888 +       /* sb may already be dead */
13889 +       au_rw_read_lock(&sbinfo->si_rwsem);
13890 +       readable = atomic_read(&fhsm->fhsm_readable);
13891 +       if (readable > 0) {
13892 +               sb = sbinfo->si_sb;
13893 +               AuDebugOn(!sb);
13894 +               /* exclude the bottom branch */
13895 +               nfhsm = 0;
13896 +               bbot = au_fhsm_bottom(sb);
13897 +               for (bindex = 0; bindex < bbot; bindex++) {
13898 +                       br = au_sbr(sb, bindex);
13899 +                       if (au_br_fhsm(br->br_perm))
13900 +                               nfhsm++;
13901 +               }
13902 +               err = -EMSGSIZE;
13903 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13904 +                       atomic_set(&fhsm->fhsm_readable, 0);
13905 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13906 +                                            count);
13907 +               }
13908 +       }
13909 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13910 +       if (!readable)
13911 +               goto need_data;
13912 +
13913 +out:
13914 +       return err;
13915 +}
13916 +
13917 +static int au_fhsm_release(struct inode *inode, struct file *file)
13918 +{
13919 +       struct au_sbinfo *sbinfo;
13920 +       struct au_fhsm *fhsm;
13921 +
13922 +       /* sb may already be dead */
13923 +       sbinfo = file->private_data;
13924 +       fhsm = &sbinfo->si_fhsm;
13925 +       spin_lock(&fhsm->fhsm_spin);
13926 +       fhsm->fhsm_pid = 0;
13927 +       spin_unlock(&fhsm->fhsm_spin);
13928 +       kobject_put(&sbinfo->si_kobj);
13929 +
13930 +       return 0;
13931 +}
13932 +
13933 +static const struct file_operations au_fhsm_fops = {
13934 +       .owner          = THIS_MODULE,
13935 +       .llseek         = noop_llseek,
13936 +       .read           = au_fhsm_read,
13937 +       .poll           = au_fhsm_poll,
13938 +       .release        = au_fhsm_release
13939 +};
13940 +
13941 +int au_fhsm_fd(struct super_block *sb, int oflags)
13942 +{
13943 +       int err, fd;
13944 +       struct au_sbinfo *sbinfo;
13945 +       struct au_fhsm *fhsm;
13946 +
13947 +       err = -EPERM;
13948 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13949 +               goto out;
13950 +
13951 +       err = -EINVAL;
13952 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13953 +               goto out;
13954 +
13955 +       err = 0;
13956 +       sbinfo = au_sbi(sb);
13957 +       fhsm = &sbinfo->si_fhsm;
13958 +       spin_lock(&fhsm->fhsm_spin);
13959 +       if (!fhsm->fhsm_pid)
13960 +               fhsm->fhsm_pid = current->pid;
13961 +       else
13962 +               err = -EBUSY;
13963 +       spin_unlock(&fhsm->fhsm_spin);
13964 +       if (unlikely(err))
13965 +               goto out;
13966 +
13967 +       oflags |= O_RDONLY;
13968 +       /* oflags |= FMODE_NONOTIFY; */
13969 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13970 +       err = fd;
13971 +       if (unlikely(fd < 0))
13972 +               goto out_pid;
13973 +
13974 +       /* succeed regardless 'fhsm' status */
13975 +       kobject_get(&sbinfo->si_kobj);
13976 +       si_noflush_read_lock(sb);
13977 +       if (au_ftest_si(sbinfo, FHSM))
13978 +               au_fhsm_wrote_all(sb, /*force*/0);
13979 +       si_read_unlock(sb);
13980 +       goto out; /* success */
13981 +
13982 +out_pid:
13983 +       spin_lock(&fhsm->fhsm_spin);
13984 +       fhsm->fhsm_pid = 0;
13985 +       spin_unlock(&fhsm->fhsm_spin);
13986 +out:
13987 +       AuTraceErr(err);
13988 +       return err;
13989 +}
13990 +
13991 +/* ---------------------------------------------------------------------- */
13992 +
13993 +int au_fhsm_br_alloc(struct au_branch *br)
13994 +{
13995 +       int err;
13996 +
13997 +       err = 0;
13998 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
13999 +       if (br->br_fhsm)
14000 +               au_br_fhsm_init(br->br_fhsm);
14001 +       else
14002 +               err = -ENOMEM;
14003 +
14004 +       return err;
14005 +}
14006 +
14007 +/* ---------------------------------------------------------------------- */
14008 +
14009 +void au_fhsm_fin(struct super_block *sb)
14010 +{
14011 +       au_fhsm_notify(sb, /*val*/-1);
14012 +}
14013 +
14014 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14015 +{
14016 +       struct au_fhsm *fhsm;
14017 +
14018 +       fhsm = &sbinfo->si_fhsm;
14019 +       spin_lock_init(&fhsm->fhsm_spin);
14020 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14021 +       atomic_set(&fhsm->fhsm_readable, 0);
14022 +       fhsm->fhsm_expire
14023 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14024 +       fhsm->fhsm_bottom = -1;
14025 +}
14026 +
14027 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14028 +{
14029 +       sbinfo->si_fhsm.fhsm_expire
14030 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14031 +}
14032 +
14033 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14034 +{
14035 +       unsigned int u;
14036 +
14037 +       if (!au_ftest_si(sbinfo, FHSM))
14038 +               return;
14039 +
14040 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14041 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14042 +               seq_printf(seq, ",fhsm_sec=%u", u);
14043 +}
14044 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14045 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14046 +++ linux/fs/aufs/file.c        2022-08-02 12:12:45.165641939 +0200
14047 @@ -0,0 +1,864 @@
14048 +// SPDX-License-Identifier: GPL-2.0
14049 +/*
14050 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14051 + *
14052 + * This program, aufs is free software; you can redistribute it and/or modify
14053 + * it under the terms of the GNU General Public License as published by
14054 + * the Free Software Foundation; either version 2 of the License, or
14055 + * (at your option) any later version.
14056 + *
14057 + * This program is distributed in the hope that it will be useful,
14058 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14059 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14060 + * GNU General Public License for more details.
14061 + *
14062 + * You should have received a copy of the GNU General Public License
14063 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14064 + */
14065 +
14066 +/*
14067 + * handling file/dir, and address_space operation
14068 + */
14069 +
14070 +#ifdef CONFIG_AUFS_DEBUG
14071 +#include <linux/migrate.h>
14072 +#endif
14073 +#include <linux/pagemap.h>
14074 +#include "aufs.h"
14075 +
14076 +/* drop flags for writing */
14077 +unsigned int au_file_roflags(unsigned int flags)
14078 +{
14079 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14080 +       flags |= O_RDONLY | O_NOATIME;
14081 +       return flags;
14082 +}
14083 +
14084 +/* common functions to regular file and dir */
14085 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14086 +                      struct file *file, int force_wr)
14087 +{
14088 +       struct file *h_file;
14089 +       struct dentry *h_dentry;
14090 +       struct inode *h_inode;
14091 +       struct super_block *sb;
14092 +       struct au_branch *br;
14093 +       struct path h_path;
14094 +       int err;
14095 +
14096 +       /* a race condition can happen between open and unlink/rmdir */
14097 +       h_file = ERR_PTR(-ENOENT);
14098 +       h_dentry = au_h_dptr(dentry, bindex);
14099 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14100 +               goto out;
14101 +       h_inode = d_inode(h_dentry);
14102 +       spin_lock(&h_dentry->d_lock);
14103 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14104 +               /* || !d_inode(dentry)->i_nlink */
14105 +               ;
14106 +       spin_unlock(&h_dentry->d_lock);
14107 +       if (unlikely(err))
14108 +               goto out;
14109 +
14110 +       sb = dentry->d_sb;
14111 +       br = au_sbr(sb, bindex);
14112 +       err = au_br_test_oflag(flags, br);
14113 +       h_file = ERR_PTR(err);
14114 +       if (unlikely(err))
14115 +               goto out;
14116 +
14117 +       /* drop flags for writing */
14118 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14119 +               if (force_wr && !(flags & O_WRONLY))
14120 +                       force_wr = 0;
14121 +               flags = au_file_roflags(flags);
14122 +               if (force_wr) {
14123 +                       h_file = ERR_PTR(-EROFS);
14124 +                       flags = au_file_roflags(flags);
14125 +                       if (unlikely(vfsub_native_ro(h_inode)
14126 +                                    || IS_APPEND(h_inode)))
14127 +                               goto out;
14128 +                       flags &= ~O_ACCMODE;
14129 +                       flags |= O_WRONLY;
14130 +               }
14131 +       }
14132 +       flags &= ~O_CREAT;
14133 +       au_lcnt_inc(&br->br_nfiles);
14134 +       h_path.dentry = h_dentry;
14135 +       h_path.mnt = au_br_mnt(br);
14136 +       h_file = vfsub_dentry_open(&h_path, flags);
14137 +       if (IS_ERR(h_file))
14138 +               goto out_br;
14139 +
14140 +       if (flags & __FMODE_EXEC) {
14141 +               err = deny_write_access(h_file);
14142 +               if (unlikely(err)) {
14143 +                       fput(h_file);
14144 +                       h_file = ERR_PTR(err);
14145 +                       goto out_br;
14146 +               }
14147 +       }
14148 +       fsnotify_open(h_file);
14149 +       goto out; /* success */
14150 +
14151 +out_br:
14152 +       au_lcnt_dec(&br->br_nfiles);
14153 +out:
14154 +       return h_file;
14155 +}
14156 +
14157 +static int au_cmoo(struct dentry *dentry)
14158 +{
14159 +       int err, cmoo, matched;
14160 +       unsigned int udba;
14161 +       struct path h_path;
14162 +       struct au_pin pin;
14163 +       struct au_cp_generic cpg = {
14164 +               .dentry = dentry,
14165 +               .bdst   = -1,
14166 +               .bsrc   = -1,
14167 +               .len    = -1,
14168 +               .pin    = &pin,
14169 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14170 +       };
14171 +       struct inode *delegated;
14172 +       struct super_block *sb;
14173 +       struct au_sbinfo *sbinfo;
14174 +       struct au_fhsm *fhsm;
14175 +       pid_t pid;
14176 +       struct au_branch *br;
14177 +       struct dentry *parent;
14178 +       struct au_hinode *hdir;
14179 +
14180 +       DiMustWriteLock(dentry);
14181 +       IiMustWriteLock(d_inode(dentry));
14182 +
14183 +       err = 0;
14184 +       if (IS_ROOT(dentry))
14185 +               goto out;
14186 +       cpg.bsrc = au_dbtop(dentry);
14187 +       if (!cpg.bsrc)
14188 +               goto out;
14189 +
14190 +       sb = dentry->d_sb;
14191 +       sbinfo = au_sbi(sb);
14192 +       fhsm = &sbinfo->si_fhsm;
14193 +       pid = au_fhsm_pid(fhsm);
14194 +       rcu_read_lock();
14195 +       matched = (pid
14196 +                  && (current->pid == pid
14197 +                      || rcu_dereference(current->real_parent)->pid == pid));
14198 +       rcu_read_unlock();
14199 +       if (matched)
14200 +               goto out;
14201 +
14202 +       br = au_sbr(sb, cpg.bsrc);
14203 +       cmoo = au_br_cmoo(br->br_perm);
14204 +       if (!cmoo)
14205 +               goto out;
14206 +       if (!d_is_reg(dentry))
14207 +               cmoo &= AuBrAttr_COO_ALL;
14208 +       if (!cmoo)
14209 +               goto out;
14210 +
14211 +       parent = dget_parent(dentry);
14212 +       di_write_lock_parent(parent);
14213 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14214 +       cpg.bdst = err;
14215 +       if (unlikely(err < 0)) {
14216 +               err = 0;        /* there is no upper writable branch */
14217 +               goto out_dgrade;
14218 +       }
14219 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14220 +
14221 +       /* do not respect the coo attrib for the target branch */
14222 +       err = au_cpup_dirs(dentry, cpg.bdst);
14223 +       if (unlikely(err))
14224 +               goto out_dgrade;
14225 +
14226 +       di_downgrade_lock(parent, AuLock_IR);
14227 +       udba = au_opt_udba(sb);
14228 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14229 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14230 +       if (unlikely(err))
14231 +               goto out_parent;
14232 +
14233 +       err = au_sio_cpup_simple(&cpg);
14234 +       au_unpin(&pin);
14235 +       if (unlikely(err))
14236 +               goto out_parent;
14237 +       if (!(cmoo & AuBrWAttr_MOO))
14238 +               goto out_parent; /* success */
14239 +
14240 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14241 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14242 +       if (unlikely(err))
14243 +               goto out_parent;
14244 +
14245 +       h_path.mnt = au_br_mnt(br);
14246 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14247 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14248 +       delegated = NULL;
14249 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14250 +       au_unpin(&pin);
14251 +       /* todo: keep h_dentry or not? */
14252 +       if (unlikely(err == -EWOULDBLOCK)) {
14253 +               pr_warn("cannot retry for NFSv4 delegation"
14254 +                       " for an internal unlink\n");
14255 +               iput(delegated);
14256 +       }
14257 +       if (unlikely(err)) {
14258 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14259 +                      dentry, err);
14260 +               err = 0;
14261 +       }
14262 +       goto out_parent; /* success */
14263 +
14264 +out_dgrade:
14265 +       di_downgrade_lock(parent, AuLock_IR);
14266 +out_parent:
14267 +       di_read_unlock(parent, AuLock_IR);
14268 +       dput(parent);
14269 +out:
14270 +       AuTraceErr(err);
14271 +       return err;
14272 +}
14273 +
14274 +int au_do_open(struct file *file, struct au_do_open_args *args)
14275 +{
14276 +       int err, aopen = args->aopen;
14277 +       struct dentry *dentry;
14278 +       struct au_finfo *finfo;
14279 +
14280 +       if (!aopen)
14281 +               err = au_finfo_init(file, args->fidir);
14282 +       else {
14283 +               lockdep_off();
14284 +               err = au_finfo_init(file, args->fidir);
14285 +               lockdep_on();
14286 +       }
14287 +       if (unlikely(err))
14288 +               goto out;
14289 +
14290 +       dentry = file->f_path.dentry;
14291 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14292 +       di_write_lock_child(dentry);
14293 +       err = au_cmoo(dentry);
14294 +       di_downgrade_lock(dentry, AuLock_IR);
14295 +       if (!err) {
14296 +               if (!aopen)
14297 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14298 +               else {
14299 +                       lockdep_off();
14300 +                       err = args->open(file, vfsub_file_flags(file),
14301 +                                        args->h_file);
14302 +                       lockdep_on();
14303 +               }
14304 +       }
14305 +       di_read_unlock(dentry, AuLock_IR);
14306 +
14307 +       finfo = au_fi(file);
14308 +       if (!err) {
14309 +               finfo->fi_file = file;
14310 +               au_hbl_add(&finfo->fi_hlist,
14311 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14312 +       }
14313 +       if (!aopen)
14314 +               fi_write_unlock(file);
14315 +       else {
14316 +               lockdep_off();
14317 +               fi_write_unlock(file);
14318 +               lockdep_on();
14319 +       }
14320 +       if (unlikely(err)) {
14321 +               finfo->fi_hdir = NULL;
14322 +               au_finfo_fin(file);
14323 +       }
14324 +
14325 +out:
14326 +       AuTraceErr(err);
14327 +       return err;
14328 +}
14329 +
14330 +int au_reopen_nondir(struct file *file)
14331 +{
14332 +       int err;
14333 +       aufs_bindex_t btop;
14334 +       struct dentry *dentry;
14335 +       struct au_branch *br;
14336 +       struct file *h_file, *h_file_tmp;
14337 +
14338 +       dentry = file->f_path.dentry;
14339 +       btop = au_dbtop(dentry);
14340 +       br = au_sbr(dentry->d_sb, btop);
14341 +       h_file_tmp = NULL;
14342 +       if (au_fbtop(file) == btop) {
14343 +               h_file = au_hf_top(file);
14344 +               if (file->f_mode == h_file->f_mode)
14345 +                       return 0; /* success */
14346 +               h_file_tmp = h_file;
14347 +               get_file(h_file_tmp);
14348 +               au_lcnt_inc(&br->br_nfiles);
14349 +               au_set_h_fptr(file, btop, NULL);
14350 +       }
14351 +       AuDebugOn(au_fi(file)->fi_hdir);
14352 +       /*
14353 +        * it can happen
14354 +        * file exists on both of rw and ro
14355 +        * open --> dbtop and fbtop are both 0
14356 +        * prepend a branch as rw, "rw" become ro
14357 +        * remove rw/file
14358 +        * delete the top branch, "rw" becomes rw again
14359 +        *      --> dbtop is 1, fbtop is still 0
14360 +        * write --> fbtop is 0 but dbtop is 1
14361 +        */
14362 +       /* AuDebugOn(au_fbtop(file) < btop); */
14363 +
14364 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14365 +                          file, /*force_wr*/0);
14366 +       err = PTR_ERR(h_file);
14367 +       if (IS_ERR(h_file)) {
14368 +               if (h_file_tmp) {
14369 +                       /* revert */
14370 +                       au_set_h_fptr(file, btop, h_file_tmp);
14371 +                       h_file_tmp = NULL;
14372 +               }
14373 +               goto out; /* todo: close all? */
14374 +       }
14375 +
14376 +       err = 0;
14377 +       au_set_fbtop(file, btop);
14378 +       au_set_h_fptr(file, btop, h_file);
14379 +       au_update_figen(file);
14380 +       /* todo: necessary? */
14381 +       /* file->f_ra = h_file->f_ra; */
14382 +
14383 +out:
14384 +       if (h_file_tmp) {
14385 +               fput(h_file_tmp);
14386 +               au_lcnt_dec(&br->br_nfiles);
14387 +       }
14388 +       return err;
14389 +}
14390 +
14391 +/* ---------------------------------------------------------------------- */
14392 +
14393 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14394 +                       struct dentry *hi_wh)
14395 +{
14396 +       int err;
14397 +       aufs_bindex_t btop;
14398 +       struct au_dinfo *dinfo;
14399 +       struct dentry *h_dentry;
14400 +       struct au_hdentry *hdp;
14401 +
14402 +       dinfo = au_di(file->f_path.dentry);
14403 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14404 +
14405 +       btop = dinfo->di_btop;
14406 +       dinfo->di_btop = btgt;
14407 +       hdp = au_hdentry(dinfo, btgt);
14408 +       h_dentry = hdp->hd_dentry;
14409 +       hdp->hd_dentry = hi_wh;
14410 +       err = au_reopen_nondir(file);
14411 +       hdp->hd_dentry = h_dentry;
14412 +       dinfo->di_btop = btop;
14413 +
14414 +       return err;
14415 +}
14416 +
14417 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14418 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14419 +{
14420 +       int err;
14421 +       struct inode *inode, *h_inode;
14422 +       struct dentry *h_dentry, *hi_wh;
14423 +       struct au_cp_generic cpg = {
14424 +               .dentry = file->f_path.dentry,
14425 +               .bdst   = bcpup,
14426 +               .bsrc   = -1,
14427 +               .len    = len,
14428 +               .pin    = pin
14429 +       };
14430 +
14431 +       au_update_dbtop(cpg.dentry);
14432 +       inode = d_inode(cpg.dentry);
14433 +       h_inode = NULL;
14434 +       if (au_dbtop(cpg.dentry) <= bcpup
14435 +           && au_dbbot(cpg.dentry) >= bcpup) {
14436 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14437 +               if (h_dentry && d_is_positive(h_dentry))
14438 +                       h_inode = d_inode(h_dentry);
14439 +       }
14440 +       hi_wh = au_hi_wh(inode, bcpup);
14441 +       if (!hi_wh && !h_inode)
14442 +               err = au_sio_cpup_wh(&cpg, file);
14443 +       else
14444 +               /* already copied-up after unlink */
14445 +               err = au_reopen_wh(file, bcpup, hi_wh);
14446 +
14447 +       if (!err
14448 +           && (inode->i_nlink > 1
14449 +               || (inode->i_state & I_LINKABLE))
14450 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14451 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14452 +
14453 +       return err;
14454 +}
14455 +
14456 +/*
14457 + * prepare the @file for writing.
14458 + */
14459 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14460 +{
14461 +       int err;
14462 +       aufs_bindex_t dbtop;
14463 +       struct dentry *parent;
14464 +       struct inode *inode;
14465 +       struct super_block *sb;
14466 +       struct file *h_file;
14467 +       struct au_cp_generic cpg = {
14468 +               .dentry = file->f_path.dentry,
14469 +               .bdst   = -1,
14470 +               .bsrc   = -1,
14471 +               .len    = len,
14472 +               .pin    = pin,
14473 +               .flags  = AuCpup_DTIME
14474 +       };
14475 +
14476 +       sb = cpg.dentry->d_sb;
14477 +       inode = d_inode(cpg.dentry);
14478 +       cpg.bsrc = au_fbtop(file);
14479 +       err = au_test_ro(sb, cpg.bsrc, inode);
14480 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14481 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14482 +                            /*flags*/0);
14483 +               goto out;
14484 +       }
14485 +
14486 +       /* need to cpup or reopen */
14487 +       parent = dget_parent(cpg.dentry);
14488 +       di_write_lock_parent(parent);
14489 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14490 +       cpg.bdst = err;
14491 +       if (unlikely(err < 0))
14492 +               goto out_dgrade;
14493 +       err = 0;
14494 +
14495 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14496 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14497 +               if (unlikely(err))
14498 +                       goto out_dgrade;
14499 +       }
14500 +
14501 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14502 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14503 +       if (unlikely(err))
14504 +               goto out_dgrade;
14505 +
14506 +       dbtop = au_dbtop(cpg.dentry);
14507 +       if (dbtop <= cpg.bdst)
14508 +               cpg.bsrc = cpg.bdst;
14509 +
14510 +       if (dbtop <= cpg.bdst           /* just reopen */
14511 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14512 +               ) {
14513 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14514 +               if (IS_ERR(h_file))
14515 +                       err = PTR_ERR(h_file);
14516 +               else {
14517 +                       di_downgrade_lock(parent, AuLock_IR);
14518 +                       if (dbtop > cpg.bdst)
14519 +                               err = au_sio_cpup_simple(&cpg);
14520 +                       if (!err)
14521 +                               err = au_reopen_nondir(file);
14522 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14523 +               }
14524 +       } else {                        /* copyup as wh and reopen */
14525 +               /*
14526 +                * since writable hfsplus branch is not supported,
14527 +                * h_open_pre/post() are unnecessary.
14528 +                */
14529 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14530 +               di_downgrade_lock(parent, AuLock_IR);
14531 +       }
14532 +
14533 +       if (!err) {
14534 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14535 +               goto out_dput; /* success */
14536 +       }
14537 +       au_unpin(pin);
14538 +       goto out_unlock;
14539 +
14540 +out_dgrade:
14541 +       di_downgrade_lock(parent, AuLock_IR);
14542 +out_unlock:
14543 +       di_read_unlock(parent, AuLock_IR);
14544 +out_dput:
14545 +       dput(parent);
14546 +out:
14547 +       return err;
14548 +}
14549 +
14550 +/* ---------------------------------------------------------------------- */
14551 +
14552 +int au_do_flush(struct file *file, fl_owner_t id,
14553 +               int (*flush)(struct file *file, fl_owner_t id))
14554 +{
14555 +       int err;
14556 +       struct super_block *sb;
14557 +       struct inode *inode;
14558 +
14559 +       inode = file_inode(file);
14560 +       sb = inode->i_sb;
14561 +       si_noflush_read_lock(sb);
14562 +       fi_read_lock(file);
14563 +       ii_read_lock_child(inode);
14564 +
14565 +       err = flush(file, id);
14566 +       au_cpup_attr_timesizes(inode);
14567 +
14568 +       ii_read_unlock(inode);
14569 +       fi_read_unlock(file);
14570 +       si_read_unlock(sb);
14571 +       return err;
14572 +}
14573 +
14574 +/* ---------------------------------------------------------------------- */
14575 +
14576 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14577 +{
14578 +       int err;
14579 +       struct au_pin pin;
14580 +       struct au_finfo *finfo;
14581 +       struct dentry *parent, *hi_wh;
14582 +       struct inode *inode;
14583 +       struct super_block *sb;
14584 +       struct au_cp_generic cpg = {
14585 +               .dentry = file->f_path.dentry,
14586 +               .bdst   = -1,
14587 +               .bsrc   = -1,
14588 +               .len    = -1,
14589 +               .pin    = &pin,
14590 +               .flags  = AuCpup_DTIME
14591 +       };
14592 +
14593 +       FiMustWriteLock(file);
14594 +
14595 +       err = 0;
14596 +       finfo = au_fi(file);
14597 +       sb = cpg.dentry->d_sb;
14598 +       inode = d_inode(cpg.dentry);
14599 +       cpg.bdst = au_ibtop(inode);
14600 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14601 +               goto out;
14602 +
14603 +       parent = dget_parent(cpg.dentry);
14604 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14605 +               di_read_lock_parent(parent, !AuLock_IR);
14606 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14607 +               cpg.bdst = err;
14608 +               di_read_unlock(parent, !AuLock_IR);
14609 +               if (unlikely(err < 0))
14610 +                       goto out_parent;
14611 +               err = 0;
14612 +       }
14613 +
14614 +       di_read_lock_parent(parent, AuLock_IR);
14615 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14616 +       if (!S_ISDIR(inode->i_mode)
14617 +           && au_opt_test(au_mntflags(sb), PLINK)
14618 +           && au_plink_test(inode)
14619 +           && !d_unhashed(cpg.dentry)
14620 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14621 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14622 +               if (unlikely(err))
14623 +                       goto out_unlock;
14624 +
14625 +               /* always superio. */
14626 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14627 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14628 +               if (!err) {
14629 +                       err = au_sio_cpup_simple(&cpg);
14630 +                       au_unpin(&pin);
14631 +               }
14632 +       } else if (hi_wh) {
14633 +               /* already copied-up after unlink */
14634 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14635 +               *need_reopen = 0;
14636 +       }
14637 +
14638 +out_unlock:
14639 +       di_read_unlock(parent, AuLock_IR);
14640 +out_parent:
14641 +       dput(parent);
14642 +out:
14643 +       return err;
14644 +}
14645 +
14646 +static void au_do_refresh_dir(struct file *file)
14647 +{
14648 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14649 +       struct au_hfile *p, tmp, *q;
14650 +       struct au_finfo *finfo;
14651 +       struct super_block *sb;
14652 +       struct au_fidir *fidir;
14653 +
14654 +       FiMustWriteLock(file);
14655 +
14656 +       sb = file->f_path.dentry->d_sb;
14657 +       finfo = au_fi(file);
14658 +       fidir = finfo->fi_hdir;
14659 +       AuDebugOn(!fidir);
14660 +       p = fidir->fd_hfile + finfo->fi_btop;
14661 +       brid = p->hf_br->br_id;
14662 +       bbot = fidir->fd_bbot;
14663 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14664 +               if (!p->hf_file)
14665 +                       continue;
14666 +
14667 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14668 +               if (new_bindex == bindex)
14669 +                       continue;
14670 +               if (new_bindex < 0) {
14671 +                       au_set_h_fptr(file, bindex, NULL);
14672 +                       continue;
14673 +               }
14674 +
14675 +               /* swap two lower inode, and loop again */
14676 +               q = fidir->fd_hfile + new_bindex;
14677 +               tmp = *q;
14678 +               *q = *p;
14679 +               *p = tmp;
14680 +               if (tmp.hf_file) {
14681 +                       bindex--;
14682 +                       p--;
14683 +               }
14684 +       }
14685 +
14686 +       p = fidir->fd_hfile;
14687 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14688 +               bbot = au_sbbot(sb);
14689 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14690 +                    finfo->fi_btop++, p++)
14691 +                       if (p->hf_file) {
14692 +                               if (file_inode(p->hf_file))
14693 +                                       break;
14694 +                               au_hfput(p, /*execed*/0);
14695 +                       }
14696 +       } else {
14697 +               bbot = au_br_index(sb, brid);
14698 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14699 +                    finfo->fi_btop++, p++)
14700 +                       if (p->hf_file)
14701 +                               au_hfput(p, /*execed*/0);
14702 +               bbot = au_sbbot(sb);
14703 +       }
14704 +
14705 +       p = fidir->fd_hfile + bbot;
14706 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14707 +            fidir->fd_bbot--, p--)
14708 +               if (p->hf_file) {
14709 +                       if (file_inode(p->hf_file))
14710 +                               break;
14711 +                       au_hfput(p, /*execed*/0);
14712 +               }
14713 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14714 +}
14715 +
14716 +/*
14717 + * after branch manipulating, refresh the file.
14718 + */
14719 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14720 +{
14721 +       int err, need_reopen, nbr;
14722 +       aufs_bindex_t bbot, bindex;
14723 +       struct dentry *dentry;
14724 +       struct super_block *sb;
14725 +       struct au_finfo *finfo;
14726 +       struct au_hfile *hfile;
14727 +
14728 +       dentry = file->f_path.dentry;
14729 +       sb = dentry->d_sb;
14730 +       nbr = au_sbbot(sb) + 1;
14731 +       finfo = au_fi(file);
14732 +       if (!finfo->fi_hdir) {
14733 +               hfile = &finfo->fi_htop;
14734 +               AuDebugOn(!hfile->hf_file);
14735 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14736 +               AuDebugOn(bindex < 0);
14737 +               if (bindex != finfo->fi_btop)
14738 +                       au_set_fbtop(file, bindex);
14739 +       } else {
14740 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14741 +               if (unlikely(err))
14742 +                       goto out;
14743 +               au_do_refresh_dir(file);
14744 +       }
14745 +
14746 +       err = 0;
14747 +       need_reopen = 1;
14748 +       if (!au_test_mmapped(file))
14749 +               err = au_file_refresh_by_inode(file, &need_reopen);
14750 +       if (finfo->fi_hdir)
14751 +               /* harmless if err */
14752 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14753 +       if (!err && need_reopen && !d_unlinked(dentry))
14754 +               err = reopen(file);
14755 +       if (!err) {
14756 +               au_update_figen(file);
14757 +               goto out; /* success */
14758 +       }
14759 +
14760 +       /* error, close all lower files */
14761 +       if (finfo->fi_hdir) {
14762 +               bbot = au_fbbot_dir(file);
14763 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14764 +                       au_set_h_fptr(file, bindex, NULL);
14765 +       }
14766 +
14767 +out:
14768 +       return err;
14769 +}
14770 +
14771 +/* common function to regular file and dir */
14772 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14773 +                         int wlock, unsigned int fi_lsc)
14774 +{
14775 +       int err;
14776 +       unsigned int sigen, figen;
14777 +       aufs_bindex_t btop;
14778 +       unsigned char pseudo_link;
14779 +       struct dentry *dentry;
14780 +       struct inode *inode;
14781 +
14782 +       err = 0;
14783 +       dentry = file->f_path.dentry;
14784 +       inode = d_inode(dentry);
14785 +       sigen = au_sigen(dentry->d_sb);
14786 +       fi_write_lock_nested(file, fi_lsc);
14787 +       figen = au_figen(file);
14788 +       if (!fi_lsc)
14789 +               di_write_lock_child(dentry);
14790 +       else
14791 +               di_write_lock_child2(dentry);
14792 +       btop = au_dbtop(dentry);
14793 +       pseudo_link = (btop != au_ibtop(inode));
14794 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14795 +               if (!wlock) {
14796 +                       di_downgrade_lock(dentry, AuLock_IR);
14797 +                       fi_downgrade_lock(file);
14798 +               }
14799 +               goto out; /* success */
14800 +       }
14801 +
14802 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14803 +       if (au_digen_test(dentry, sigen)) {
14804 +               err = au_reval_dpath(dentry, sigen);
14805 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14806 +       }
14807 +
14808 +       if (!err)
14809 +               err = refresh_file(file, reopen);
14810 +       if (!err) {
14811 +               if (!wlock) {
14812 +                       di_downgrade_lock(dentry, AuLock_IR);
14813 +                       fi_downgrade_lock(file);
14814 +               }
14815 +       } else {
14816 +               di_write_unlock(dentry);
14817 +               fi_write_unlock(file);
14818 +       }
14819 +
14820 +out:
14821 +       return err;
14822 +}
14823 +
14824 +/* ---------------------------------------------------------------------- */
14825 +
14826 +/* cf. aufs_nopage() */
14827 +/* for madvise(2) */
14828 +static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
14829 +{
14830 +       folio_unlock(folio);
14831 +       return 0;
14832 +}
14833 +
14834 +/* it will never be called, but necessary to support O_DIRECT */
14835 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14836 +{ BUG(); return 0; }
14837 +
14838 +/* they will never be called. */
14839 +#ifdef CONFIG_AUFS_DEBUG
14840 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14841 +                           loff_t pos, unsigned len,
14842 +                           struct page **pagep, void **fsdata)
14843 +{ AuUnsupport(); return 0; }
14844 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14845 +                         loff_t pos, unsigned len, unsigned copied,
14846 +                         struct page *page, void *fsdata)
14847 +{ AuUnsupport(); return 0; }
14848 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14849 +{ AuUnsupport(); return 0; }
14850 +
14851 +static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
14852 +{ AuUnsupport(); return true; }
14853 +static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
14854 +{ AuUnsupport(); }
14855 +static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
14856 +{ AuUnsupport(); return true; }
14857 +#if 0 /* called by memory compaction regardless file */
14858 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14859 +                           struct page *page, enum migrate_mode mode)
14860 +{ AuUnsupport(); return 0; }
14861 +#endif
14862 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14863 +{ AuUnsupport(); return true; }
14864 +static void aufs_putback_page(struct page *page)
14865 +{ AuUnsupport(); }
14866 +static int aufs_launder_folio(struct folio *folio)
14867 +{ AuUnsupport(); return 0; }
14868 +static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
14869 +                                     size_t count)
14870 +{ AuUnsupport(); return true; }
14871 +static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
14872 +                                   bool *writeback)
14873 +{ AuUnsupport(); }
14874 +static int aufs_error_remove_page(struct address_space *mapping,
14875 +                                 struct page *page)
14876 +{ AuUnsupport(); return 0; }
14877 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14878 +                             sector_t *span)
14879 +{ AuUnsupport(); return 0; }
14880 +static void aufs_swap_deactivate(struct file *file)
14881 +{ AuUnsupport(); }
14882 +static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
14883 +{ AuUnsupport(); return 0; }
14884 +#endif /* CONFIG_AUFS_DEBUG */
14885 +
14886 +const struct address_space_operations aufs_aop = {
14887 +       .read_folio             = aufs_read_folio,
14888 +       .direct_IO              = aufs_direct_IO,
14889 +#ifdef CONFIG_AUFS_DEBUG
14890 +       .writepage              = aufs_writepage,
14891 +       /* no writepages, because of writepage */
14892 +       .dirty_folio            = aufs_dirty_folio,
14893 +       /* no readpages, because of readpage */
14894 +       .write_begin            = aufs_write_begin,
14895 +       .write_end              = aufs_write_end,
14896 +       /* no bmap, no block device */
14897 +       .invalidate_folio       = aufs_invalidate_folio,
14898 +       .release_folio          = aufs_release_folio,
14899 +       /* is fallback_migrate_page ok? */
14900 +       /* .migratepage         = aufs_migratepage, */
14901 +       .isolate_page           = aufs_isolate_page,
14902 +       .putback_page           = aufs_putback_page,
14903 +       .launder_folio          = aufs_launder_folio,
14904 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14905 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14906 +       .error_remove_page      = aufs_error_remove_page,
14907 +       .swap_activate          = aufs_swap_activate,
14908 +       .swap_deactivate        = aufs_swap_deactivate,
14909 +       .swap_rw                = aufs_swap_rw
14910 +#endif /* CONFIG_AUFS_DEBUG */
14911 +};
14912 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14913 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14914 +++ linux/fs/aufs/file.h        2022-08-02 12:12:45.165641939 +0200
14915 @@ -0,0 +1,342 @@
14916 +/* SPDX-License-Identifier: GPL-2.0 */
14917 +/*
14918 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14919 + *
14920 + * This program, aufs is free software; you can redistribute it and/or modify
14921 + * it under the terms of the GNU General Public License as published by
14922 + * the Free Software Foundation; either version 2 of the License, or
14923 + * (at your option) any later version.
14924 + *
14925 + * This program is distributed in the hope that it will be useful,
14926 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14927 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14928 + * GNU General Public License for more details.
14929 + *
14930 + * You should have received a copy of the GNU General Public License
14931 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14932 + */
14933 +
14934 +/*
14935 + * file operations
14936 + */
14937 +
14938 +#ifndef __AUFS_FILE_H__
14939 +#define __AUFS_FILE_H__
14940 +
14941 +#ifdef __KERNEL__
14942 +
14943 +#include <linux/file.h>
14944 +#include <linux/fs.h>
14945 +#include <linux/mm_types.h>
14946 +#include <linux/poll.h>
14947 +#include "rwsem.h"
14948 +
14949 +struct au_branch;
14950 +struct au_hfile {
14951 +       struct file             *hf_file;
14952 +       struct au_branch        *hf_br;
14953 +};
14954 +
14955 +struct au_vdir;
14956 +struct au_fidir {
14957 +       aufs_bindex_t           fd_bbot;
14958 +       aufs_bindex_t           fd_nent;
14959 +       struct au_vdir          *fd_vdir_cache;
14960 +       struct au_hfile         fd_hfile[];
14961 +};
14962 +
14963 +static inline int au_fidir_sz(int nent)
14964 +{
14965 +       AuDebugOn(nent < 0);
14966 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14967 +}
14968 +
14969 +struct au_finfo {
14970 +       atomic_t                fi_generation;
14971 +
14972 +       struct au_rwsem         fi_rwsem;
14973 +       aufs_bindex_t           fi_btop;
14974 +
14975 +       /* do not union them */
14976 +       struct {                                /* for non-dir */
14977 +               struct au_hfile                 fi_htop;
14978 +               atomic_t                        fi_mmapped;
14979 +       };
14980 +       struct au_fidir         *fi_hdir;       /* for dir only */
14981 +
14982 +       struct hlist_bl_node    fi_hlist;
14983 +       struct file             *fi_file;       /* very ugly */
14984 +       struct rcu_head         rcu;
14985 +} ____cacheline_aligned_in_smp;
14986 +
14987 +/* ---------------------------------------------------------------------- */
14988 +
14989 +/* file.c */
14990 +extern const struct address_space_operations aufs_aop;
14991 +unsigned int au_file_roflags(unsigned int flags);
14992 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14993 +                      struct file *file, int force_wr);
14994 +struct au_do_open_args {
14995 +       int             aopen;
14996 +       int             (*open)(struct file *file, int flags,
14997 +                               struct file *h_file);
14998 +       struct au_fidir *fidir;
14999 +       struct file     *h_file;
15000 +};
15001 +int au_do_open(struct file *file, struct au_do_open_args *args);
15002 +int au_reopen_nondir(struct file *file);
15003 +struct au_pin;
15004 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15005 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15006 +                         int wlock, unsigned int fi_lsc);
15007 +int au_do_flush(struct file *file, fl_owner_t id,
15008 +               int (*flush)(struct file *file, fl_owner_t id));
15009 +
15010 +/* poll.c */
15011 +#ifdef CONFIG_AUFS_POLL
15012 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15013 +#endif
15014 +
15015 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15016 +/* hfsplus.c */
15017 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15018 +                          int force_wr);
15019 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15020 +                   struct file *h_file);
15021 +#else
15022 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15023 +       aufs_bindex_t bindex, int force_wr)
15024 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15025 +          struct file *h_file);
15026 +#endif
15027 +
15028 +/* f_op.c */
15029 +extern const struct file_operations aufs_file_fop;
15030 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15031 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15032 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15033 +
15034 +/* finfo.c */
15035 +void au_hfput(struct au_hfile *hf, int execed);
15036 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15037 +                  struct file *h_file);
15038 +
15039 +void au_update_figen(struct file *file);
15040 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15041 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15042 +
15043 +void au_fi_init_once(void *_fi);
15044 +void au_finfo_fin(struct file *file);
15045 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15046 +
15047 +/* ioctl.c */
15048 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15049 +#ifdef CONFIG_COMPAT
15050 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15051 +                          unsigned long arg);
15052 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15053 +                             unsigned long arg);
15054 +#endif
15055 +
15056 +/* ---------------------------------------------------------------------- */
15057 +
15058 +static inline struct au_finfo *au_fi(struct file *file)
15059 +{
15060 +       return file->private_data;
15061 +}
15062 +
15063 +/* ---------------------------------------------------------------------- */
15064 +
15065 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15066 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15067 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15068 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15069 +/*
15070 +#define fi_read_trylock_nested(f) \
15071 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15072 +#define fi_write_trylock_nested(f) \
15073 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15074 +*/
15075 +
15076 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15077 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15078 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15079 +
15080 +/* lock subclass for finfo */
15081 +enum {
15082 +       AuLsc_FI_1,
15083 +       AuLsc_FI_2
15084 +};
15085 +
15086 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15087 +{
15088 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15089 +}
15090 +
15091 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15092 +{
15093 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15094 +}
15095 +
15096 +/*
15097 + * fi_read_lock_1, fi_write_lock_1,
15098 + * fi_read_lock_2, fi_write_lock_2
15099 + */
15100 +#define AuReadLockFunc(name) \
15101 +static inline void fi_read_lock_##name(struct file *f) \
15102 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15103 +
15104 +#define AuWriteLockFunc(name) \
15105 +static inline void fi_write_lock_##name(struct file *f) \
15106 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15107 +
15108 +#define AuRWLockFuncs(name) \
15109 +       AuReadLockFunc(name) \
15110 +       AuWriteLockFunc(name)
15111 +
15112 +AuRWLockFuncs(1);
15113 +AuRWLockFuncs(2);
15114 +
15115 +#undef AuReadLockFunc
15116 +#undef AuWriteLockFunc
15117 +#undef AuRWLockFuncs
15118 +
15119 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15120 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15121 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15122 +
15123 +/* ---------------------------------------------------------------------- */
15124 +
15125 +/* todo: hard/soft set? */
15126 +static inline aufs_bindex_t au_fbtop(struct file *file)
15127 +{
15128 +       FiMustAnyLock(file);
15129 +       return au_fi(file)->fi_btop;
15130 +}
15131 +
15132 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15133 +{
15134 +       FiMustAnyLock(file);
15135 +       AuDebugOn(!au_fi(file)->fi_hdir);
15136 +       return au_fi(file)->fi_hdir->fd_bbot;
15137 +}
15138 +
15139 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15140 +{
15141 +       FiMustAnyLock(file);
15142 +       AuDebugOn(!au_fi(file)->fi_hdir);
15143 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15144 +}
15145 +
15146 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15147 +{
15148 +       FiMustWriteLock(file);
15149 +       au_fi(file)->fi_btop = bindex;
15150 +}
15151 +
15152 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15153 +{
15154 +       FiMustWriteLock(file);
15155 +       AuDebugOn(!au_fi(file)->fi_hdir);
15156 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15157 +}
15158 +
15159 +static inline void au_set_fvdir_cache(struct file *file,
15160 +                                     struct au_vdir *vdir_cache)
15161 +{
15162 +       FiMustWriteLock(file);
15163 +       AuDebugOn(!au_fi(file)->fi_hdir);
15164 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15165 +}
15166 +
15167 +static inline struct file *au_hf_top(struct file *file)
15168 +{
15169 +       FiMustAnyLock(file);
15170 +       AuDebugOn(au_fi(file)->fi_hdir);
15171 +       return au_fi(file)->fi_htop.hf_file;
15172 +}
15173 +
15174 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15175 +{
15176 +       FiMustAnyLock(file);
15177 +       AuDebugOn(!au_fi(file)->fi_hdir);
15178 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15179 +}
15180 +
15181 +/* todo: memory barrier? */
15182 +static inline unsigned int au_figen(struct file *f)
15183 +{
15184 +       return atomic_read(&au_fi(f)->fi_generation);
15185 +}
15186 +
15187 +static inline void au_set_mmapped(struct file *f)
15188 +{
15189 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15190 +               return;
15191 +       pr_warn("fi_mmapped wrapped around\n");
15192 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15193 +               ;
15194 +}
15195 +
15196 +static inline void au_unset_mmapped(struct file *f)
15197 +{
15198 +       atomic_dec(&au_fi(f)->fi_mmapped);
15199 +}
15200 +
15201 +static inline int au_test_mmapped(struct file *f)
15202 +{
15203 +       return atomic_read(&au_fi(f)->fi_mmapped);
15204 +}
15205 +
15206 +/* customize vma->vm_file */
15207 +
15208 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15209 +                                      struct file *file)
15210 +{
15211 +       struct file *f;
15212 +
15213 +       f = vma->vm_file;
15214 +       get_file(file);
15215 +       vma->vm_file = file;
15216 +       fput(f);
15217 +}
15218 +
15219 +#ifdef CONFIG_MMU
15220 +#define AuDbgVmRegion(file, vma) do {} while (0)
15221 +
15222 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15223 +                                   struct file *file)
15224 +{
15225 +       au_do_vm_file_reset(vma, file);
15226 +}
15227 +#else
15228 +#define AuDbgVmRegion(file, vma) \
15229 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15230 +
15231 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15232 +                                   struct file *file)
15233 +{
15234 +       struct file *f;
15235 +
15236 +       au_do_vm_file_reset(vma, file);
15237 +       f = vma->vm_region->vm_file;
15238 +       get_file(file);
15239 +       vma->vm_region->vm_file = file;
15240 +       fput(f);
15241 +}
15242 +#endif /* CONFIG_MMU */
15243 +
15244 +/* handle vma->vm_prfile */
15245 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15246 +                                   struct file *file)
15247 +{
15248 +       get_file(file);
15249 +       vma->vm_prfile = file;
15250 +#ifndef CONFIG_MMU
15251 +       get_file(file);
15252 +       vma->vm_region->vm_prfile = file;
15253 +#endif
15254 +}
15255 +
15256 +#endif /* __KERNEL__ */
15257 +#endif /* __AUFS_FILE_H__ */
15258 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15259 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15260 +++ linux/fs/aufs/finfo.c       2022-08-02 12:12:45.165641939 +0200
15261 @@ -0,0 +1,149 @@
15262 +// SPDX-License-Identifier: GPL-2.0
15263 +/*
15264 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15265 + *
15266 + * This program, aufs is free software; you can redistribute it and/or modify
15267 + * it under the terms of the GNU General Public License as published by
15268 + * the Free Software Foundation; either version 2 of the License, or
15269 + * (at your option) any later version.
15270 + *
15271 + * This program is distributed in the hope that it will be useful,
15272 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15273 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15274 + * GNU General Public License for more details.
15275 + *
15276 + * You should have received a copy of the GNU General Public License
15277 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15278 + */
15279 +
15280 +/*
15281 + * file private data
15282 + */
15283 +
15284 +#include "aufs.h"
15285 +
15286 +void au_hfput(struct au_hfile *hf, int execed)
15287 +{
15288 +       if (execed)
15289 +               allow_write_access(hf->hf_file);
15290 +       fput(hf->hf_file);
15291 +       hf->hf_file = NULL;
15292 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15293 +       hf->hf_br = NULL;
15294 +}
15295 +
15296 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15297 +{
15298 +       struct au_finfo *finfo = au_fi(file);
15299 +       struct au_hfile *hf;
15300 +       struct au_fidir *fidir;
15301 +
15302 +       fidir = finfo->fi_hdir;
15303 +       if (!fidir) {
15304 +               AuDebugOn(finfo->fi_btop != bindex);
15305 +               hf = &finfo->fi_htop;
15306 +       } else
15307 +               hf = fidir->fd_hfile + bindex;
15308 +
15309 +       if (hf && hf->hf_file)
15310 +               au_hfput(hf, vfsub_file_execed(file));
15311 +       if (val) {
15312 +               FiMustWriteLock(file);
15313 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15314 +               hf->hf_file = val;
15315 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15316 +       }
15317 +}
15318 +
15319 +void au_update_figen(struct file *file)
15320 +{
15321 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15322 +       /* smp_mb(); */ /* atomic_set */
15323 +}
15324 +
15325 +/* ---------------------------------------------------------------------- */
15326 +
15327 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15328 +{
15329 +       struct au_fidir *fidir;
15330 +       int nbr;
15331 +
15332 +       nbr = au_sbbot(sb) + 1;
15333 +       if (nbr < 2)
15334 +               nbr = 2; /* initial allocate for 2 branches */
15335 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15336 +       if (fidir) {
15337 +               fidir->fd_bbot = -1;
15338 +               fidir->fd_nent = nbr;
15339 +       }
15340 +
15341 +       return fidir;
15342 +}
15343 +
15344 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15345 +{
15346 +       int err;
15347 +       struct au_fidir *fidir, *p;
15348 +
15349 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15350 +       fidir = finfo->fi_hdir;
15351 +       AuDebugOn(!fidir);
15352 +
15353 +       err = -ENOMEM;
15354 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15355 +                        GFP_NOFS, may_shrink);
15356 +       if (p) {
15357 +               p->fd_nent = nbr;
15358 +               finfo->fi_hdir = p;
15359 +               err = 0;
15360 +       }
15361 +
15362 +       return err;
15363 +}
15364 +
15365 +/* ---------------------------------------------------------------------- */
15366 +
15367 +void au_finfo_fin(struct file *file)
15368 +{
15369 +       struct au_finfo *finfo;
15370 +
15371 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15372 +
15373 +       finfo = au_fi(file);
15374 +       AuDebugOn(finfo->fi_hdir);
15375 +       AuRwDestroy(&finfo->fi_rwsem);
15376 +       au_cache_free_finfo(finfo);
15377 +}
15378 +
15379 +void au_fi_init_once(void *_finfo)
15380 +{
15381 +       struct au_finfo *finfo = _finfo;
15382 +
15383 +       au_rw_init(&finfo->fi_rwsem);
15384 +}
15385 +
15386 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15387 +{
15388 +       int err;
15389 +       struct au_finfo *finfo;
15390 +       struct dentry *dentry;
15391 +
15392 +       err = -ENOMEM;
15393 +       dentry = file->f_path.dentry;
15394 +       finfo = au_cache_alloc_finfo();
15395 +       if (unlikely(!finfo))
15396 +               goto out;
15397 +
15398 +       err = 0;
15399 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15400 +       au_rw_write_lock(&finfo->fi_rwsem);
15401 +       finfo->fi_btop = -1;
15402 +       finfo->fi_hdir = fidir;
15403 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15404 +       /* smp_mb(); */ /* atomic_set */
15405 +
15406 +       file->private_data = finfo;
15407 +
15408 +out:
15409 +       return err;
15410 +}
15411 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15412 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15413 +++ linux/fs/aufs/f_op.c        2022-08-02 12:12:45.165641939 +0200
15414 @@ -0,0 +1,771 @@
15415 +// SPDX-License-Identifier: GPL-2.0
15416 +/*
15417 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15418 + *
15419 + * This program, aufs is free software; you can redistribute it and/or modify
15420 + * it under the terms of the GNU General Public License as published by
15421 + * the Free Software Foundation; either version 2 of the License, or
15422 + * (at your option) any later version.
15423 + *
15424 + * This program is distributed in the hope that it will be useful,
15425 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15426 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15427 + * GNU General Public License for more details.
15428 + *
15429 + * You should have received a copy of the GNU General Public License
15430 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15431 + */
15432 +
15433 +/*
15434 + * file and vm operations
15435 + */
15436 +
15437 +#include <linux/aio.h>
15438 +#include <linux/fs_stack.h>
15439 +#include <linux/mman.h>
15440 +#include <linux/security.h>
15441 +#include "aufs.h"
15442 +
15443 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15444 +{
15445 +       int err;
15446 +       aufs_bindex_t bindex;
15447 +       struct dentry *dentry, *h_dentry;
15448 +       struct au_finfo *finfo;
15449 +       struct inode *h_inode;
15450 +
15451 +       FiMustWriteLock(file);
15452 +
15453 +       err = 0;
15454 +       dentry = file->f_path.dentry;
15455 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15456 +       finfo = au_fi(file);
15457 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15458 +       atomic_set(&finfo->fi_mmapped, 0);
15459 +       bindex = au_dbtop(dentry);
15460 +       if (!h_file) {
15461 +               h_dentry = au_h_dptr(dentry, bindex);
15462 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15463 +               if (unlikely(err))
15464 +                       goto out;
15465 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15466 +               if (IS_ERR(h_file)) {
15467 +                       err = PTR_ERR(h_file);
15468 +                       goto out;
15469 +               }
15470 +       } else {
15471 +               h_dentry = h_file->f_path.dentry;
15472 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15473 +               if (unlikely(err))
15474 +                       goto out;
15475 +               /* br ref is already inc-ed */
15476 +       }
15477 +
15478 +       if ((flags & __O_TMPFILE)
15479 +           && !(flags & O_EXCL)) {
15480 +               h_inode = file_inode(h_file);
15481 +               spin_lock(&h_inode->i_lock);
15482 +               h_inode->i_state |= I_LINKABLE;
15483 +               spin_unlock(&h_inode->i_lock);
15484 +       }
15485 +       au_set_fbtop(file, bindex);
15486 +       au_set_h_fptr(file, bindex, h_file);
15487 +       au_update_figen(file);
15488 +       /* todo: necessary? */
15489 +       /* file->f_ra = h_file->f_ra; */
15490 +
15491 +out:
15492 +       return err;
15493 +}
15494 +
15495 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15496 +                           struct file *file)
15497 +{
15498 +       int err;
15499 +       struct super_block *sb;
15500 +       struct au_do_open_args args = {
15501 +               .open   = au_do_open_nondir
15502 +       };
15503 +
15504 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15505 +             file, vfsub_file_flags(file), file->f_mode);
15506 +
15507 +       sb = file->f_path.dentry->d_sb;
15508 +       si_read_lock(sb, AuLock_FLUSH);
15509 +       err = au_do_open(file, &args);
15510 +       si_read_unlock(sb);
15511 +       return err;
15512 +}
15513 +
15514 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15515 +{
15516 +       struct au_finfo *finfo;
15517 +       aufs_bindex_t bindex;
15518 +
15519 +       finfo = au_fi(file);
15520 +       au_hbl_del(&finfo->fi_hlist,
15521 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15522 +       bindex = finfo->fi_btop;
15523 +       if (bindex >= 0)
15524 +               au_set_h_fptr(file, bindex, NULL);
15525 +
15526 +       au_finfo_fin(file);
15527 +       return 0;
15528 +}
15529 +
15530 +/* ---------------------------------------------------------------------- */
15531 +
15532 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15533 +{
15534 +       int err;
15535 +       struct file *h_file;
15536 +
15537 +       err = 0;
15538 +       h_file = au_hf_top(file);
15539 +       if (h_file)
15540 +               err = vfsub_flush(h_file, id);
15541 +       return err;
15542 +}
15543 +
15544 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15545 +{
15546 +       return au_do_flush(file, id, au_do_flush_nondir);
15547 +}
15548 +
15549 +/* ---------------------------------------------------------------------- */
15550 +/*
15551 + * read and write functions acquire [fdi]_rwsem once, but release before
15552 + * mmap_sem. This is because to stop a race condition between mmap(2).
15553 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15554 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15555 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15556 + */
15557 +
15558 +/* Callers should call au_read_post() or fput() in the end */
15559 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15560 +{
15561 +       struct file *h_file;
15562 +       int err;
15563 +
15564 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15565 +       if (!err) {
15566 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15567 +               h_file = au_hf_top(file);
15568 +               get_file(h_file);
15569 +               if (!keep_fi)
15570 +                       fi_read_unlock(file);
15571 +       } else
15572 +               h_file = ERR_PTR(err);
15573 +
15574 +       return h_file;
15575 +}
15576 +
15577 +static void au_read_post(struct inode *inode, struct file *h_file)
15578 +{
15579 +       /* update without lock, I don't think it a problem */
15580 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15581 +       fput(h_file);
15582 +}
15583 +
15584 +struct au_write_pre {
15585 +       /* input */
15586 +       unsigned int lsc;
15587 +
15588 +       /* output */
15589 +       blkcnt_t blks;
15590 +       aufs_bindex_t btop;
15591 +};
15592 +
15593 +/*
15594 + * return with iinfo is write-locked
15595 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15596 + * end
15597 + */
15598 +static struct file *au_write_pre(struct file *file, int do_ready,
15599 +                                struct au_write_pre *wpre)
15600 +{
15601 +       struct file *h_file;
15602 +       struct dentry *dentry;
15603 +       int err;
15604 +       unsigned int lsc;
15605 +       struct au_pin pin;
15606 +
15607 +       lsc = 0;
15608 +       if (wpre)
15609 +               lsc = wpre->lsc;
15610 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15611 +       h_file = ERR_PTR(err);
15612 +       if (unlikely(err))
15613 +               goto out;
15614 +
15615 +       dentry = file->f_path.dentry;
15616 +       if (do_ready) {
15617 +               err = au_ready_to_write(file, -1, &pin);
15618 +               if (unlikely(err)) {
15619 +                       h_file = ERR_PTR(err);
15620 +                       di_write_unlock(dentry);
15621 +                       goto out_fi;
15622 +               }
15623 +       }
15624 +
15625 +       di_downgrade_lock(dentry, /*flags*/0);
15626 +       if (wpre)
15627 +               wpre->btop = au_fbtop(file);
15628 +       h_file = au_hf_top(file);
15629 +       get_file(h_file);
15630 +       if (wpre)
15631 +               wpre->blks = file_inode(h_file)->i_blocks;
15632 +       if (do_ready)
15633 +               au_unpin(&pin);
15634 +       di_read_unlock(dentry, /*flags*/0);
15635 +
15636 +out_fi:
15637 +       fi_write_unlock(file);
15638 +out:
15639 +       return h_file;
15640 +}
15641 +
15642 +static void au_write_post(struct inode *inode, struct file *h_file,
15643 +                         struct au_write_pre *wpre, ssize_t written)
15644 +{
15645 +       struct inode *h_inode;
15646 +
15647 +       au_cpup_attr_timesizes(inode);
15648 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15649 +       h_inode = file_inode(h_file);
15650 +       inode->i_mode = h_inode->i_mode;
15651 +       ii_write_unlock(inode);
15652 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15653 +       if (written > 0)
15654 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15655 +                             /*force*/h_inode->i_blocks > wpre->blks);
15656 +       fput(h_file);
15657 +}
15658 +
15659 +/*
15660 + * todo: very ugly
15661 + * it locks both of i_mutex and si_rwsem for read in safe.
15662 + * if the plink maintenance mode continues forever (that is the problem),
15663 + * may loop forever.
15664 + */
15665 +static void au_mtx_and_read_lock(struct inode *inode)
15666 +{
15667 +       int err;
15668 +       struct super_block *sb = inode->i_sb;
15669 +
15670 +       while (1) {
15671 +               inode_lock(inode);
15672 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15673 +               if (!err)
15674 +                       break;
15675 +               inode_unlock(inode);
15676 +               si_read_lock(sb, AuLock_NOPLMW);
15677 +               si_read_unlock(sb);
15678 +       }
15679 +}
15680 +
15681 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15682 +                         struct iov_iter *iov_iter)
15683 +{
15684 +       ssize_t err;
15685 +       struct file *file;
15686 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15687 +
15688 +       err = security_file_permission(h_file, rw);
15689 +       if (unlikely(err))
15690 +               goto out;
15691 +
15692 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15693 +       iter = NULL;
15694 +       if (rw == MAY_READ)
15695 +               iter = h_file->f_op->read_iter;
15696 +       else if (rw == MAY_WRITE)
15697 +               iter = h_file->f_op->write_iter;
15698 +
15699 +       file = kio->ki_filp;
15700 +       kio->ki_filp = h_file;
15701 +       if (iter) {
15702 +               lockdep_off();
15703 +               err = iter(kio, iov_iter);
15704 +               lockdep_on();
15705 +       } else
15706 +               /* currently there is no such fs */
15707 +               WARN_ON_ONCE(1);
15708 +       kio->ki_filp = file;
15709 +
15710 +out:
15711 +       return err;
15712 +}
15713 +
15714 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15715 +{
15716 +       ssize_t err;
15717 +       struct file *file, *h_file;
15718 +       struct inode *inode;
15719 +       struct super_block *sb;
15720 +
15721 +       file = kio->ki_filp;
15722 +       inode = file_inode(file);
15723 +       sb = inode->i_sb;
15724 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15725 +
15726 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15727 +       err = PTR_ERR(h_file);
15728 +       if (IS_ERR(h_file))
15729 +               goto out;
15730 +
15731 +       if (au_test_loopback_kthread()) {
15732 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15733 +               if (file->f_mapping != h_file->f_mapping) {
15734 +                       file->f_mapping = h_file->f_mapping;
15735 +                       smp_mb(); /* unnecessary? */
15736 +               }
15737 +       }
15738 +       fi_read_unlock(file);
15739 +
15740 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15741 +       /* todo: necessary? */
15742 +       /* file->f_ra = h_file->f_ra; */
15743 +       au_read_post(inode, h_file);
15744 +
15745 +out:
15746 +       si_read_unlock(sb);
15747 +       return err;
15748 +}
15749 +
15750 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15751 +{
15752 +       ssize_t err;
15753 +       struct au_write_pre wpre;
15754 +       struct inode *inode;
15755 +       struct file *file, *h_file;
15756 +
15757 +       file = kio->ki_filp;
15758 +       inode = file_inode(file);
15759 +       au_mtx_and_read_lock(inode);
15760 +
15761 +       wpre.lsc = 0;
15762 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15763 +       err = PTR_ERR(h_file);
15764 +       if (IS_ERR(h_file))
15765 +               goto out;
15766 +
15767 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15768 +       au_write_post(inode, h_file, &wpre, err);
15769 +
15770 +out:
15771 +       si_read_unlock(inode->i_sb);
15772 +       inode_unlock(inode);
15773 +       return err;
15774 +}
15775 +
15776 +/*
15777 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15778 + * don't have their own .splice_{read,write} implimentations, and they use
15779 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15780 + * simple converters to f_op->iter_read() and ->iter_write().
15781 + * But we keep our own implementations because some non-mainlined FSes may have
15782 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15783 + * away an opportunity to co-work with aufs from them.
15784 + */
15785 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15786 +                               struct pipe_inode_info *pipe, size_t len,
15787 +                               unsigned int flags)
15788 +{
15789 +       ssize_t err;
15790 +       struct file *h_file;
15791 +       struct inode *inode;
15792 +       struct super_block *sb;
15793 +
15794 +       inode = file_inode(file);
15795 +       sb = inode->i_sb;
15796 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15797 +
15798 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15799 +       err = PTR_ERR(h_file);
15800 +       if (IS_ERR(h_file))
15801 +               goto out;
15802 +
15803 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15804 +       /* todo: necessary? */
15805 +       /* file->f_ra = h_file->f_ra; */
15806 +       au_read_post(inode, h_file);
15807 +
15808 +out:
15809 +       si_read_unlock(sb);
15810 +       return err;
15811 +}
15812 +
15813 +static ssize_t
15814 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15815 +                 size_t len, unsigned int flags)
15816 +{
15817 +       ssize_t err;
15818 +       struct au_write_pre wpre;
15819 +       struct inode *inode;
15820 +       struct file *h_file;
15821 +
15822 +       inode = file_inode(file);
15823 +       au_mtx_and_read_lock(inode);
15824 +
15825 +       wpre.lsc = 0;
15826 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15827 +       err = PTR_ERR(h_file);
15828 +       if (IS_ERR(h_file))
15829 +               goto out;
15830 +
15831 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15832 +       au_write_post(inode, h_file, &wpre, err);
15833 +
15834 +out:
15835 +       si_read_unlock(inode->i_sb);
15836 +       inode_unlock(inode);
15837 +       return err;
15838 +}
15839 +
15840 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15841 +                          loff_t len)
15842 +{
15843 +       long err;
15844 +       struct au_write_pre wpre;
15845 +       struct inode *inode;
15846 +       struct file *h_file;
15847 +
15848 +       inode = file_inode(file);
15849 +       au_mtx_and_read_lock(inode);
15850 +
15851 +       wpre.lsc = 0;
15852 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15853 +       err = PTR_ERR(h_file);
15854 +       if (IS_ERR(h_file))
15855 +               goto out;
15856 +
15857 +       lockdep_off();
15858 +       err = vfs_fallocate(h_file, mode, offset, len);
15859 +       lockdep_on();
15860 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15861 +
15862 +out:
15863 +       si_read_unlock(inode->i_sb);
15864 +       inode_unlock(inode);
15865 +       return err;
15866 +}
15867 +
15868 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15869 +                                   struct file *dst, loff_t dst_pos,
15870 +                                   size_t len, unsigned int flags)
15871 +{
15872 +       ssize_t err;
15873 +       struct au_write_pre wpre;
15874 +       enum { SRC, DST };
15875 +       struct {
15876 +               struct inode *inode;
15877 +               struct file *h_file;
15878 +               struct super_block *h_sb;
15879 +       } a[2];
15880 +#define a_src  a[SRC]
15881 +#define a_dst  a[DST]
15882 +
15883 +       err = -EINVAL;
15884 +       a_src.inode = file_inode(src);
15885 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15886 +               goto out;
15887 +       a_dst.inode = file_inode(dst);
15888 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15889 +               goto out;
15890 +
15891 +       au_mtx_and_read_lock(a_dst.inode);
15892 +       /*
15893 +        * in order to match the order in di_write_lock2_{child,parent}(),
15894 +        * use f_path.dentry for this comparison.
15895 +        */
15896 +       if (src->f_path.dentry < dst->f_path.dentry) {
15897 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15898 +               err = PTR_ERR(a_src.h_file);
15899 +               if (IS_ERR(a_src.h_file))
15900 +                       goto out_si;
15901 +
15902 +               wpre.lsc = AuLsc_FI_2;
15903 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15904 +               err = PTR_ERR(a_dst.h_file);
15905 +               if (IS_ERR(a_dst.h_file)) {
15906 +                       au_read_post(a_src.inode, a_src.h_file);
15907 +                       goto out_si;
15908 +               }
15909 +       } else {
15910 +               wpre.lsc = AuLsc_FI_1;
15911 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15912 +               err = PTR_ERR(a_dst.h_file);
15913 +               if (IS_ERR(a_dst.h_file))
15914 +                       goto out_si;
15915 +
15916 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15917 +               err = PTR_ERR(a_src.h_file);
15918 +               if (IS_ERR(a_src.h_file)) {
15919 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15920 +                                     /*written*/0);
15921 +                       goto out_si;
15922 +               }
15923 +       }
15924 +
15925 +       err = -EXDEV;
15926 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15927 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15928 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15929 +               AuDbgFile(src);
15930 +               AuDbgFile(dst);
15931 +               goto out_file;
15932 +       }
15933 +
15934 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15935 +                                   dst_pos, len, flags);
15936 +
15937 +out_file:
15938 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15939 +       fi_read_unlock(src);
15940 +       au_read_post(a_src.inode, a_src.h_file);
15941 +out_si:
15942 +       si_read_unlock(a_dst.inode->i_sb);
15943 +       inode_unlock(a_dst.inode);
15944 +out:
15945 +       return err;
15946 +#undef a_src
15947 +#undef a_dst
15948 +}
15949 +
15950 +/* ---------------------------------------------------------------------- */
15951 +
15952 +/*
15953 + * The locking order around current->mmap_sem.
15954 + * - in most and regular cases
15955 + *   file I/O syscall -- aufs_read() or something
15956 + *     -- si_rwsem for read -- mmap_sem
15957 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15958 + * - in mmap case
15959 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15960 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15961 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15962 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15963 + * It means that when aufs acquires si_rwsem for write, the process should never
15964 + * acquire mmap_sem.
15965 + *
15966 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15967 + * problem either since any directory is not able to be mmap-ed.
15968 + * The similar scenario is applied to aufs_readlink() too.
15969 + */
15970 +
15971 +#if 0 /* stop calling security_file_mmap() */
15972 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15973 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15974 +
15975 +static unsigned long au_arch_prot_conv(unsigned long flags)
15976 +{
15977 +       /* currently ppc64 only */
15978 +#ifdef CONFIG_PPC64
15979 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15980 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15981 +       return AuConv_VM_PROT(flags, SAO);
15982 +#else
15983 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15984 +       return 0;
15985 +#endif
15986 +}
15987 +
15988 +static unsigned long au_prot_conv(unsigned long flags)
15989 +{
15990 +       return AuConv_VM_PROT(flags, READ)
15991 +               | AuConv_VM_PROT(flags, WRITE)
15992 +               | AuConv_VM_PROT(flags, EXEC)
15993 +               | au_arch_prot_conv(flags);
15994 +}
15995 +
15996 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
15997 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
15998 +
15999 +static unsigned long au_flag_conv(unsigned long flags)
16000 +{
16001 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16002 +               | AuConv_VM_MAP(flags, DENYWRITE)
16003 +               | AuConv_VM_MAP(flags, LOCKED);
16004 +}
16005 +#endif
16006 +
16007 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16008 +{
16009 +       int err;
16010 +       const unsigned char wlock
16011 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16012 +       struct super_block *sb;
16013 +       struct file *h_file;
16014 +       struct inode *inode;
16015 +
16016 +       AuDbgVmRegion(file, vma);
16017 +
16018 +       inode = file_inode(file);
16019 +       sb = inode->i_sb;
16020 +       lockdep_off();
16021 +       si_read_lock(sb, AuLock_NOPLMW);
16022 +
16023 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16024 +       lockdep_on();
16025 +       err = PTR_ERR(h_file);
16026 +       if (IS_ERR(h_file))
16027 +               goto out;
16028 +
16029 +       err = 0;
16030 +       au_set_mmapped(file);
16031 +       au_vm_file_reset(vma, h_file);
16032 +       /*
16033 +        * we cannot call security_mmap_file() here since it may acquire
16034 +        * mmap_sem or i_mutex.
16035 +        *
16036 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16037 +        *                       au_flag_conv(vma->vm_flags));
16038 +        */
16039 +       if (!err)
16040 +               err = call_mmap(h_file, vma);
16041 +       if (!err) {
16042 +               au_vm_prfile_set(vma, file);
16043 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16044 +               goto out_fput; /* success */
16045 +       }
16046 +       au_unset_mmapped(file);
16047 +       au_vm_file_reset(vma, file);
16048 +
16049 +out_fput:
16050 +       lockdep_off();
16051 +       ii_write_unlock(inode);
16052 +       lockdep_on();
16053 +       fput(h_file);
16054 +out:
16055 +       lockdep_off();
16056 +       si_read_unlock(sb);
16057 +       lockdep_on();
16058 +       AuTraceErr(err);
16059 +       return err;
16060 +}
16061 +
16062 +/* ---------------------------------------------------------------------- */
16063 +
16064 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16065 +                            int datasync)
16066 +{
16067 +       int err;
16068 +       struct au_write_pre wpre;
16069 +       struct inode *inode;
16070 +       struct file *h_file;
16071 +
16072 +       err = 0; /* -EBADF; */ /* posix? */
16073 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16074 +               goto out;
16075 +
16076 +       inode = file_inode(file);
16077 +       au_mtx_and_read_lock(inode);
16078 +
16079 +       wpre.lsc = 0;
16080 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16081 +       err = PTR_ERR(h_file);
16082 +       if (IS_ERR(h_file))
16083 +               goto out_unlock;
16084 +
16085 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16086 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16087 +
16088 +out_unlock:
16089 +       si_read_unlock(inode->i_sb);
16090 +       inode_unlock(inode);
16091 +out:
16092 +       return err;
16093 +}
16094 +
16095 +static int aufs_fasync(int fd, struct file *file, int flag)
16096 +{
16097 +       int err;
16098 +       struct file *h_file;
16099 +       struct super_block *sb;
16100 +
16101 +       sb = file->f_path.dentry->d_sb;
16102 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16103 +
16104 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16105 +       err = PTR_ERR(h_file);
16106 +       if (IS_ERR(h_file))
16107 +               goto out;
16108 +
16109 +       if (h_file->f_op->fasync)
16110 +               err = h_file->f_op->fasync(fd, h_file, flag);
16111 +       fput(h_file); /* instead of au_read_post() */
16112 +
16113 +out:
16114 +       si_read_unlock(sb);
16115 +       return err;
16116 +}
16117 +
16118 +static int aufs_setfl(struct file *file, unsigned long arg)
16119 +{
16120 +       int err;
16121 +       struct file *h_file;
16122 +       struct super_block *sb;
16123 +
16124 +       sb = file->f_path.dentry->d_sb;
16125 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16126 +
16127 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16128 +       err = PTR_ERR(h_file);
16129 +       if (IS_ERR(h_file))
16130 +               goto out;
16131 +
16132 +       /* stop calling h_file->fasync */
16133 +       arg |= vfsub_file_flags(file) & FASYNC;
16134 +       err = setfl(/*unused fd*/-1, h_file, arg);
16135 +       fput(h_file); /* instead of au_read_post() */
16136 +
16137 +out:
16138 +       si_read_unlock(sb);
16139 +       return err;
16140 +}
16141 +
16142 +/* ---------------------------------------------------------------------- */
16143 +
16144 +/* no one supports this operation, currently */
16145 +#if 0 /* reserved for future use */
16146 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16147 +                            size_t len, loff_t *pos, int more)
16148 +{
16149 +}
16150 +#endif
16151 +
16152 +/* ---------------------------------------------------------------------- */
16153 +
16154 +const struct file_operations aufs_file_fop = {
16155 +       .owner          = THIS_MODULE,
16156 +
16157 +       .llseek         = default_llseek,
16158 +
16159 +       .read_iter      = aufs_read_iter,
16160 +       .write_iter     = aufs_write_iter,
16161 +
16162 +#ifdef CONFIG_AUFS_POLL
16163 +       .poll           = aufs_poll,
16164 +#endif
16165 +       .unlocked_ioctl = aufs_ioctl_nondir,
16166 +#ifdef CONFIG_COMPAT
16167 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16168 +#endif
16169 +       .mmap           = aufs_mmap,
16170 +       .open           = aufs_open_nondir,
16171 +       .flush          = aufs_flush_nondir,
16172 +       .release        = aufs_release_nondir,
16173 +       .fsync          = aufs_fsync_nondir,
16174 +       .fasync         = aufs_fasync,
16175 +       /* .sendpage    = aufs_sendpage, */
16176 +       .setfl          = aufs_setfl,
16177 +       .splice_write   = aufs_splice_write,
16178 +       .splice_read    = aufs_splice_read,
16179 +#if 0 /* reserved for future use */
16180 +       .aio_splice_write = aufs_aio_splice_write,
16181 +       .aio_splice_read  = aufs_aio_splice_read,
16182 +#endif
16183 +       .fallocate      = aufs_fallocate,
16184 +       .copy_file_range = aufs_copy_file_range
16185 +};
16186 diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16187 --- /usr/share/empty/fs/aufs/fsctx.c    1970-01-01 01:00:00.000000000 +0100
16188 +++ linux/fs/aufs/fsctx.c       2022-08-02 12:12:45.165641939 +0200
16189 @@ -0,0 +1,1242 @@
16190 +// SPDX-License-Identifier: GPL-2.0
16191 +/*
16192 + * Copyright (C) 2022 Junjiro R. Okajima
16193 + *
16194 + * This program, aufs is free software; you can redistribute it and/or modify
16195 + * it under the terms of the GNU General Public License as published by
16196 + * the Free Software Foundation; either version 2 of the License, or
16197 + * (at your option) any later version.
16198 + *
16199 + * This program is distributed in the hope that it will be useful,
16200 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16201 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16202 + * GNU General Public License for more details.
16203 + *
16204 + * You should have received a copy of the GNU General Public License
16205 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16206 + */
16207 +
16208 +/*
16209 + * fs context, aka new mount api
16210 + */
16211 +
16212 +#include <linux/fs_context.h>
16213 +#include "aufs.h"
16214 +
16215 +struct au_fsctx_opts {
16216 +       aufs_bindex_t bindex;
16217 +       unsigned char skipped;
16218 +       struct au_opt *opt, *opt_tail;
16219 +       struct super_block *sb;
16220 +       struct au_sbinfo *sbinfo;
16221 +       struct au_opts opts;
16222 +};
16223 +
16224 +/* stop extra interpretation of errno in mount(8), and strange error messages */
16225 +static int cvt_err(int err)
16226 +{
16227 +       AuTraceErr(err);
16228 +
16229 +       switch (err) {
16230 +       case -ENOENT:
16231 +       case -ENOTDIR:
16232 +       case -EEXIST:
16233 +       case -EIO:
16234 +               err = -EINVAL;
16235 +       }
16236 +       return err;
16237 +}
16238 +
16239 +static int au_fsctx_reconfigure(struct fs_context *fc)
16240 +{
16241 +       int err, do_dx;
16242 +       unsigned int mntflags;
16243 +       struct dentry *root;
16244 +       struct super_block *sb;
16245 +       struct inode *inode;
16246 +       struct au_fsctx_opts *a = fc->fs_private;
16247 +
16248 +       AuDbg("fc %p\n", fc);
16249 +
16250 +       root = fc->root;
16251 +       sb = root->d_sb;
16252 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16253 +       if (!err) {
16254 +               di_write_lock_child(root);
16255 +               err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16256 +               aufs_write_unlock(root);
16257 +       }
16258 +
16259 +       inode = d_inode(root);
16260 +       inode_lock(inode);
16261 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16262 +       if (unlikely(err))
16263 +               goto out;
16264 +       di_write_lock_child(root);
16265 +
16266 +       /* au_opts_remount() may return an error */
16267 +       err = au_opts_remount(sb, &a->opts);
16268 +
16269 +       if (au_ftest_opts(a->opts.flags, REFRESH))
16270 +               au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16271 +                                                    REFRESH_IDOP));
16272 +
16273 +       if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16274 +               mntflags = au_mntflags(sb);
16275 +               do_dx = !!au_opt_test(mntflags, DIO);
16276 +               au_dy_arefresh(do_dx);
16277 +       }
16278 +
16279 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16280 +       aufs_write_unlock(root);
16281 +
16282 +out:
16283 +       inode_unlock(inode);
16284 +       err = cvt_err(err);
16285 +       AuTraceErr(err);
16286 +
16287 +       return err;
16288 +}
16289 +
16290 +/* ---------------------------------------------------------------------- */
16291 +
16292 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16293 +{
16294 +       int err;
16295 +       struct au_fsctx_opts *a = fc->fs_private;
16296 +       struct au_sbinfo *sbinfo = a->sbinfo;
16297 +       struct dentry *root;
16298 +       struct inode *inode;
16299 +
16300 +       sbinfo->si_sb = sb;
16301 +       sb->s_fs_info = sbinfo;
16302 +       kobject_get(&sbinfo->si_kobj);
16303 +
16304 +       __si_write_lock(sb);
16305 +       si_pid_set(sb);
16306 +       au_sbilist_add(sb);
16307 +
16308 +       /* all timestamps always follow the ones on the branch */
16309 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16310 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16311 +       sb->s_op = &aufs_sop;
16312 +       sb->s_d_op = &aufs_dop;
16313 +       sb->s_magic = AUFS_SUPER_MAGIC;
16314 +       sb->s_maxbytes = 0;
16315 +       sb->s_stack_depth = 1;
16316 +       au_export_init(sb);
16317 +       au_xattr_init(sb);
16318 +
16319 +       err = au_alloc_root(sb);
16320 +       if (unlikely(err)) {
16321 +               si_write_unlock(sb);
16322 +               goto out;
16323 +       }
16324 +       root = sb->s_root;
16325 +       inode = d_inode(root);
16326 +       ii_write_lock_parent(inode);
16327 +       aufs_write_unlock(root);
16328 +
16329 +       /* lock vfs_inode first, then aufs. */
16330 +       inode_lock(inode);
16331 +       aufs_write_lock(root);
16332 +       err = au_opts_mount(sb, &a->opts);
16333 +       AuTraceErr(err);
16334 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16335 +               sb->s_d_op = &aufs_dop_noreval;
16336 +               /* infofc(fc, "%ps", sb->s_d_op); */
16337 +               pr_info("%ps\n", sb->s_d_op);
16338 +               au_refresh_dop(root, /*force_reval*/0);
16339 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
16340 +               au_refresh_iop(inode, /*force_getattr*/0);
16341 +       }
16342 +       aufs_write_unlock(root);
16343 +       inode_unlock(inode);
16344 +       if (!err)
16345 +               goto out; /* success */
16346 +
16347 +       dput(root);
16348 +       sb->s_root = NULL;
16349 +
16350 +out:
16351 +       if (unlikely(err))
16352 +               kobject_put(&sbinfo->si_kobj);
16353 +       AuTraceErr(err);
16354 +       err = cvt_err(err);
16355 +       AuTraceErr(err);
16356 +       return err;
16357 +}
16358 +
16359 +static int au_fsctx_get_tree(struct fs_context *fc)
16360 +{
16361 +       int err;
16362 +
16363 +       AuDbg("fc %p\n", fc);
16364 +       err = get_tree_nodev(fc, au_fsctx_fill_super);
16365 +
16366 +       AuTraceErr(err);
16367 +       return err;
16368 +}
16369 +
16370 +/* ---------------------------------------------------------------------- */
16371 +
16372 +static void au_fsctx_dump(struct au_opts *opts)
16373 +{
16374 +#ifdef CONFIG_AUFS_DEBUG
16375 +       /* reduce stack space */
16376 +       union {
16377 +               struct au_opt_add *add;
16378 +               struct au_opt_del *del;
16379 +               struct au_opt_mod *mod;
16380 +               struct au_opt_xino *xino;
16381 +               struct au_opt_xino_itrunc *xino_itrunc;
16382 +               struct au_opt_wbr_create *create;
16383 +       } u;
16384 +       struct au_opt *opt;
16385 +
16386 +       opt = opts->opt;
16387 +       while (opt->type != Opt_tail) {
16388 +               switch (opt->type) {
16389 +               case Opt_add:
16390 +                       u.add = &opt->add;
16391 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16392 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16393 +                                 u.add->path.dentry);
16394 +                       break;
16395 +               case Opt_del:
16396 +                       fallthrough;
16397 +               case Opt_idel:
16398 +                       u.del = &opt->del;
16399 +                       AuDbg("del {%s, %p}\n",
16400 +                             u.del->pathname, u.del->h_path.dentry);
16401 +                       break;
16402 +               case Opt_mod:
16403 +                       fallthrough;
16404 +               case Opt_imod:
16405 +                       u.mod = &opt->mod;
16406 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16407 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16408 +                       break;
16409 +               case Opt_append:
16410 +                       u.add = &opt->add;
16411 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16412 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16413 +                                 u.add->path.dentry);
16414 +                       break;
16415 +               case Opt_prepend:
16416 +                       u.add = &opt->add;
16417 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16418 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16419 +                                 u.add->path.dentry);
16420 +                       break;
16421 +
16422 +               case Opt_dirwh:
16423 +                       AuDbg("dirwh %d\n", opt->dirwh);
16424 +                       break;
16425 +               case Opt_rdcache:
16426 +                       AuDbg("rdcache %d\n", opt->rdcache);
16427 +                       break;
16428 +               case Opt_rdblk:
16429 +                       AuDbg("rdblk %d\n", opt->rdblk);
16430 +                       break;
16431 +               case Opt_rdhash:
16432 +                       AuDbg("rdhash %u\n", opt->rdhash);
16433 +                       break;
16434 +
16435 +               case Opt_xino:
16436 +                       u.xino = &opt->xino;
16437 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16438 +                       break;
16439 +
16440 +#define au_fsctx_TF(name)                                        \
16441 +                       case Opt_##name:                          \
16442 +                               if (opt->tf)                      \
16443 +                                       AuLabel(name);            \
16444 +                               else                              \
16445 +                                       AuLabel(no##name);        \
16446 +                               break;
16447 +
16448 +               /* simple true/false flag */
16449 +               au_fsctx_TF(trunc_xino);
16450 +               au_fsctx_TF(trunc_xib);
16451 +               au_fsctx_TF(dirperm1);
16452 +               au_fsctx_TF(plink);
16453 +               au_fsctx_TF(shwh);
16454 +               au_fsctx_TF(dio);
16455 +               au_fsctx_TF(warn_perm);
16456 +               au_fsctx_TF(verbose);
16457 +               au_fsctx_TF(sum);
16458 +               au_fsctx_TF(dirren);
16459 +               au_fsctx_TF(acl);
16460 +#undef au_fsctx_TF
16461 +
16462 +               case Opt_trunc_xino_path:
16463 +                       fallthrough;
16464 +               case Opt_itrunc_xino:
16465 +                       u.xino_itrunc = &opt->xino_itrunc;
16466 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16467 +                       break;
16468 +               case Opt_noxino:
16469 +                       AuLabel(noxino);
16470 +                       break;
16471 +
16472 +               case Opt_list_plink:
16473 +                       AuLabel(list_plink);
16474 +                       break;
16475 +               case Opt_udba:
16476 +                       AuDbg("udba %d, %s\n",
16477 +                                 opt->udba, au_optstr_udba(opt->udba));
16478 +                       break;
16479 +               case Opt_diropq_a:
16480 +                       AuLabel(diropq_a);
16481 +                       break;
16482 +               case Opt_diropq_w:
16483 +                       AuLabel(diropq_w);
16484 +                       break;
16485 +               case Opt_wsum:
16486 +                       AuLabel(wsum);
16487 +                       break;
16488 +               case Opt_wbr_create:
16489 +                       u.create = &opt->wbr_create;
16490 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
16491 +                                 au_optstr_wbr_create(u.create->wbr_create));
16492 +                       switch (u.create->wbr_create) {
16493 +                       case AuWbrCreate_MFSV:
16494 +                               fallthrough;
16495 +                       case AuWbrCreate_PMFSV:
16496 +                               AuDbg("%d sec\n", u.create->mfs_second);
16497 +                               break;
16498 +                       case AuWbrCreate_MFSRR:
16499 +                               fallthrough;
16500 +                       case AuWbrCreate_TDMFS:
16501 +                               AuDbg("%llu watermark\n",
16502 +                                         u.create->mfsrr_watermark);
16503 +                               break;
16504 +                       case AuWbrCreate_MFSRRV:
16505 +                               fallthrough;
16506 +                       case AuWbrCreate_TDMFSV:
16507 +                               fallthrough;
16508 +                       case AuWbrCreate_PMFSRRV:
16509 +                               AuDbg("%llu watermark, %d sec\n",
16510 +                                         u.create->mfsrr_watermark,
16511 +                                         u.create->mfs_second);
16512 +                               break;
16513 +                       }
16514 +                       break;
16515 +               case Opt_wbr_copyup:
16516 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16517 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
16518 +                       break;
16519 +               case Opt_fhsm_sec:
16520 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16521 +                       break;
16522 +
16523 +               default:
16524 +                       AuDbg("type %d\n", opt->type);
16525 +                       BUG();
16526 +               }
16527 +               opt++;
16528 +       }
16529 +#endif
16530 +}
16531 +
16532 +/* ---------------------------------------------------------------------- */
16533 +
16534 +/*
16535 + * For conditionally compiled mount options.
16536 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16537 + */
16538 +#define au_ignore_flag(name, action)           \
16539 +       fsparam_flag(name, action),             \
16540 +       fsparam_flag("no" name, Opt_ignore_silent)
16541 +
16542 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16543 +       fsparam_string("br", Opt_br),
16544 +
16545 +       /* "add=%d:%s" or "ins=%d:%s" */
16546 +       fsparam_string("add", Opt_add),
16547 +       fsparam_string("ins", Opt_add),
16548 +       fsparam_path("append", Opt_append),
16549 +       fsparam_path("prepend", Opt_prepend),
16550 +
16551 +       fsparam_path("del", Opt_del),
16552 +       /* fsparam_s32("idel", Opt_idel), */
16553 +       fsparam_path("mod", Opt_mod),
16554 +       /* fsparam_string("imod", Opt_imod), */
16555 +
16556 +       fsparam_s32("dirwh", Opt_dirwh),
16557 +
16558 +       fsparam_path("xino", Opt_xino),
16559 +       fsparam_flag("noxino", Opt_noxino),
16560 +       fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16561 +       /* "trunc_xino_v=%d:%d" */
16562 +       /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16563 +       fsparam_path("trunc_xino", Opt_trunc_xino_path),
16564 +       fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16565 +       /* fsparam_path("zxino", Opt_zxino), */
16566 +       fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16567 +
16568 +#ifdef CONFIG_PROC_FS
16569 +       fsparam_flag_no("plink", Opt_plink),
16570 +#else
16571 +       au_ignore_flag("plink", Opt_ignore),
16572 +#endif
16573 +
16574 +#ifdef CONFIG_AUFS_DEBUG
16575 +       fsparam_flag("list_plink", Opt_list_plink),
16576 +#endif
16577 +
16578 +       fsparam_string("udba", Opt_udba),
16579 +
16580 +       fsparam_flag_no("dio", Opt_dio),
16581 +
16582 +#ifdef CONFIG_AUFS_DIRREN
16583 +       fsparam_flag_no("dirren", Opt_dirren),
16584 +#else
16585 +       au_ignore_flag("dirren", Opt_ignore),
16586 +#endif
16587 +
16588 +#ifdef CONFIG_AUFS_FHSM
16589 +       fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16590 +#else
16591 +       fsparam_s32("fhsm_sec", Opt_ignore),
16592 +#endif
16593 +
16594 +       /* always | a | whiteouted | w */
16595 +       fsparam_string("diropq", Opt_diropq),
16596 +
16597 +       fsparam_flag_no("warn_perm", Opt_warn_perm),
16598 +
16599 +#ifdef CONFIG_AUFS_SHWH
16600 +       fsparam_flag_no("shwh", Opt_shwh),
16601 +#else
16602 +       au_ignore_flag("shwh", Opt_err),
16603 +#endif
16604 +
16605 +       fsparam_flag_no("dirperm1", Opt_dirperm1),
16606 +
16607 +       fsparam_flag_no("verbose", Opt_verbose),
16608 +       fsparam_flag("v", Opt_verbose),
16609 +       fsparam_flag("quiet", Opt_noverbose),
16610 +       fsparam_flag("q", Opt_noverbose),
16611 +       /* user-space may handle this */
16612 +       fsparam_flag("silent", Opt_noverbose),
16613 +
16614 +       fsparam_flag_no("sum", Opt_sum),
16615 +       fsparam_flag("wsum", Opt_wsum),
16616 +
16617 +       fsparam_s32("rdcache", Opt_rdcache),
16618 +       /* "def" or s32 */
16619 +       fsparam_string("rdblk", Opt_rdblk),
16620 +       /* "def" or s32 */
16621 +       fsparam_string("rdhash", Opt_rdhash),
16622 +
16623 +       fsparam_string("create", Opt_wbr_create),
16624 +       fsparam_string("create_policy", Opt_wbr_create),
16625 +       fsparam_string("cpup", Opt_wbr_copyup),
16626 +       fsparam_string("copyup", Opt_wbr_copyup),
16627 +       fsparam_string("copyup_policy", Opt_wbr_copyup),
16628 +
16629 +       /* generic VFS flag */
16630 +#ifdef CONFIG_FS_POSIX_ACL
16631 +       fsparam_flag_no("acl", Opt_acl),
16632 +#else
16633 +       au_ignore_flag("acl", Opt_ignore),
16634 +#endif
16635 +
16636 +       /* internal use for the scripts */
16637 +       fsparam_string("si", Opt_ignore_silent),
16638 +
16639 +       /* obsoleted, keep them temporary */
16640 +       fsparam_flag("nodlgt", Opt_ignore_silent),
16641 +       fsparam_flag("clean_plink", Opt_ignore),
16642 +       fsparam_string("dirs", Opt_br),
16643 +       fsparam_u32("debug", Opt_ignore),
16644 +       /* "whiteout" or "all" */
16645 +       fsparam_string("delete", Opt_ignore),
16646 +       fsparam_string("imap", Opt_ignore),
16647 +
16648 +       /* temporary workaround, due to old mount(8)? */
16649 +       fsparam_flag("relatime", Opt_ignore_silent),
16650 +
16651 +       {}
16652 +};
16653 +
16654 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16655 +                                char *brspec, size_t speclen,
16656 +                                aufs_bindex_t bindex)
16657 +{
16658 +       int err;
16659 +       char *p;
16660 +
16661 +       AuDbg("brspec %s\n", brspec);
16662 +
16663 +       err = -ENOMEM;
16664 +       if (!speclen)
16665 +               speclen = strlen(brspec);
16666 +       /* will be freed by au_fsctx_free() */
16667 +       p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16668 +       if (unlikely(!p)) {
16669 +               errorfc(fc, "failed in %s", brspec);
16670 +               goto out;
16671 +       }
16672 +       err = au_opt_add(opt, p, fc->sb_flags, bindex);
16673 +
16674 +out:
16675 +       AuTraceErr(err);
16676 +       return err;
16677 +}
16678 +
16679 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16680 +{
16681 +       int err;
16682 +       char *p;
16683 +       struct au_fsctx_opts *a = fc->fs_private;
16684 +       struct au_opt *opt = a->opt;
16685 +       aufs_bindex_t bindex = a->bindex;
16686 +
16687 +       AuDbg("brspec %s\n", brspec);
16688 +
16689 +       err = -EINVAL;
16690 +       while ((p = strsep(&brspec, ":")) && *p) {
16691 +               err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16692 +               AuTraceErr(err);
16693 +               if (unlikely(err))
16694 +                       break;
16695 +               bindex++;
16696 +               opt++;
16697 +               if (unlikely(opt > a->opt_tail)) {
16698 +                       err = -E2BIG;
16699 +                       bindex--;
16700 +                       opt--;
16701 +                       break;
16702 +               }
16703 +               opt->type = Opt_tail;
16704 +               a->skipped = 1;
16705 +       }
16706 +       a->bindex = bindex;
16707 +       a->opt = opt;
16708 +
16709 +       AuTraceErr(err);
16710 +       return err;
16711 +}
16712 +
16713 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16714 +{
16715 +       int err, n;
16716 +       char *p;
16717 +       struct au_fsctx_opts *a = fc->fs_private;
16718 +       struct au_opt *opt = a->opt;
16719 +
16720 +       err = -EINVAL;
16721 +       p = strchr(addspec, ':');
16722 +       if (unlikely(!p)) {
16723 +               errorfc(fc, "bad arg in %s", addspec);
16724 +               goto out;
16725 +       }
16726 +       *p++ = '\0';
16727 +       err = kstrtoint(addspec, 0, &n);
16728 +       if (unlikely(err)) {
16729 +               errorfc(fc, "bad integer in %s", addspec);
16730 +               goto out;
16731 +       }
16732 +       AuDbg("n %d\n", n);
16733 +       err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16734 +
16735 +out:
16736 +       AuTraceErr(err);
16737 +       return err;
16738 +}
16739 +
16740 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16741 +                             struct fs_parameter *param)
16742 +{
16743 +       int err;
16744 +
16745 +       err = -ENOMEM;
16746 +       /* will be freed by au_fsctx_free() */
16747 +       del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16748 +       if (unlikely(!del->pathname))
16749 +               goto out;
16750 +       AuDbg("del %s\n", del->pathname);
16751 +       err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16752 +       if (unlikely(err))
16753 +               errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16754 +
16755 +out:
16756 +       AuTraceErr(err);
16757 +       return err;
16758 +}
16759 +
16760 +#if 0 /* reserved for future use */
16761 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16762 +                              aufs_bindex_t bindex)
16763 +{
16764 +       int err;
16765 +       struct super_block *sb;
16766 +       struct dentry *root;
16767 +       struct au_fsctx_opts *a = fc->fs_private;
16768 +
16769 +       sb = a->sb;
16770 +       AuDebugOn(!sb);
16771 +
16772 +       err = -EINVAL;
16773 +       root = sb->s_root;
16774 +       aufs_read_lock(root, AuLock_FLUSH);
16775 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
16776 +               errorfc(fc, "out of bounds, %d", bindex);
16777 +               goto out;
16778 +       }
16779 +
16780 +       err = 0;
16781 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
16782 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16783 +
16784 +out:
16785 +       aufs_read_unlock(root, !AuLock_IR);
16786 +       AuTraceErr(err);
16787 +       return err;
16788 +}
16789 +#endif
16790 +
16791 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16792 +                             struct fs_parameter *param)
16793 +{
16794 +       int err;
16795 +       struct path path;
16796 +       char *p;
16797 +
16798 +       err = -ENOMEM;
16799 +       /* will be freed by au_fsctx_free() */
16800 +       mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16801 +       if (unlikely(!mod->path))
16802 +               goto out;
16803 +
16804 +       err = -EINVAL;
16805 +       p = strchr(mod->path, '=');
16806 +       if (unlikely(!p)) {
16807 +               errorfc(fc, "no permission %s", mod->path);
16808 +               goto out;
16809 +       }
16810 +
16811 +       *p++ = 0;
16812 +       err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16813 +       if (unlikely(err)) {
16814 +               errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16815 +               goto out;
16816 +       }
16817 +
16818 +       mod->perm = au_br_perm_val(p);
16819 +       AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16820 +       mod->h_root = dget(path.dentry);
16821 +       path_put(&path);
16822 +
16823 +out:
16824 +       AuTraceErr(err);
16825 +       return err;
16826 +}
16827 +
16828 +#if 0 /* reserved for future use */
16829 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16830 +                              char *ibrspec)
16831 +{
16832 +       int err, n;
16833 +       char *p;
16834 +       struct super_block *sb;
16835 +       struct dentry *root;
16836 +       struct au_fsctx_opts *a = fc->fs_private;
16837 +
16838 +       sb = a->sb;
16839 +       AuDebugOn(!sb);
16840 +
16841 +       err = -EINVAL;
16842 +       p = strchr(ibrspec, ':');
16843 +       if (unlikely(!p)) {
16844 +               errorfc(fc, "no index, %s", ibrspec);
16845 +               goto out;
16846 +       }
16847 +       *p++ = '\0';
16848 +       err = kstrtoint(ibrspec, 0, &n);
16849 +       if (unlikely(err)) {
16850 +               errorfc(fc, "bad integer in %s", ibrspec);
16851 +               goto out;
16852 +       }
16853 +       AuDbg("n %d\n", n);
16854 +
16855 +       root = sb->s_root;
16856 +       aufs_read_lock(root, AuLock_FLUSH);
16857 +       if (n < 0 || au_sbbot(sb) < n) {
16858 +               errorfc(fc, "out of bounds, %d", bindex);
16859 +               goto out_root;
16860 +       }
16861 +
16862 +       err = 0;
16863 +       mod->perm = au_br_perm_val(p);
16864 +       AuDbg("mod path %s, perm 0x%x, %s\n",
16865 +             mod->path, mod->perm, p);
16866 +       mod->h_root = dget(au_h_dptr(root, bindex));
16867 +
16868 +out_root:
16869 +       aufs_read_unlock(root, !AuLock_IR);
16870 +out:
16871 +       AuTraceErr(err);
16872 +       return err;
16873 +}
16874 +#endif
16875 +
16876 +static int au_fsctx_parse_xino(struct fs_context *fc,
16877 +                              struct au_opt_xino *xino,
16878 +                              struct fs_parameter *param)
16879 +{
16880 +       int err;
16881 +       struct au_fsctx_opts *a = fc->fs_private;
16882 +
16883 +       err = -ENOMEM;
16884 +       /* will be freed by au_opts_free() */
16885 +       xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16886 +       if (unlikely(!xino->path))
16887 +               goto out;
16888 +       AuDbg("path %s\n", xino->path);
16889 +
16890 +       xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16891 +                                   /*wbrtop*/0);
16892 +       err = PTR_ERR(xino->file);
16893 +       if (IS_ERR(xino->file)) {
16894 +               xino->file = NULL;
16895 +               goto out;
16896 +       }
16897 +
16898 +       err = 0;
16899 +       if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16900 +               err = -EINVAL;
16901 +               errorfc(fc, "%s must be outside", xino->path);
16902 +       }
16903 +
16904 +out:
16905 +       AuTraceErr(err);
16906 +       return err;
16907 +}
16908 +
16909 +static
16910 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16911 +                                   struct au_opt_xino_itrunc *xino_itrunc,
16912 +                                   char *pathname)
16913 +{
16914 +       int err;
16915 +       aufs_bindex_t bbot, bindex;
16916 +       struct path path;
16917 +       struct dentry *root;
16918 +       struct au_fsctx_opts *a = fc->fs_private;
16919 +
16920 +       AuDebugOn(!a->sb);
16921 +
16922 +       err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16923 +       if (unlikely(err)) {
16924 +               errorfc(fc, "lookup failed %s (%d)", pathname, err);
16925 +               goto out;
16926 +       }
16927 +
16928 +       xino_itrunc->bindex = -1;
16929 +       root = a->sb->s_root;
16930 +       aufs_read_lock(root, AuLock_FLUSH);
16931 +       bbot = au_sbbot(a->sb);
16932 +       for (bindex = 0; bindex <= bbot; bindex++) {
16933 +               if (au_h_dptr(root, bindex) == path.dentry) {
16934 +                       xino_itrunc->bindex = bindex;
16935 +                       break;
16936 +               }
16937 +       }
16938 +       aufs_read_unlock(root, !AuLock_IR);
16939 +       path_put(&path);
16940 +
16941 +       if (unlikely(xino_itrunc->bindex < 0)) {
16942 +               err = -EINVAL;
16943 +               errorfc(fc, "no such branch %s", pathname);
16944 +       }
16945 +
16946 +out:
16947 +       AuTraceErr(err);
16948 +       return err;
16949 +}
16950 +
16951 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16952 +                                     struct au_opt_xino_itrunc *xino_itrunc,
16953 +                                     unsigned int bindex)
16954 +{
16955 +       int err;
16956 +       aufs_bindex_t bbot;
16957 +       struct super_block *sb;
16958 +       struct au_fsctx_opts *a = fc->fs_private;
16959 +
16960 +       sb = a->sb;
16961 +       AuDebugOn(!sb);
16962 +
16963 +       err = 0;
16964 +       si_noflush_read_lock(sb);
16965 +       bbot = au_sbbot(sb);
16966 +       si_read_unlock(sb);
16967 +       if (bindex <= bbot)
16968 +               xino_itrunc->bindex = bindex;
16969 +       else {
16970 +               err = -EINVAL;
16971 +               errorfc(fc, "out of bounds, %u", bindex);
16972 +       }
16973 +
16974 +       AuTraceErr(err);
16975 +       return err;
16976 +}
16977 +
16978 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16979 +{
16980 +       int err, token;
16981 +       struct fs_parse_result result;
16982 +       struct au_fsctx_opts *a = fc->fs_private;
16983 +       struct au_opt *opt = a->opt;
16984 +
16985 +       AuDbg("fc %p, param {key %s, string %s}\n",
16986 +             fc, param->key, param->string);
16987 +       err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
16988 +       if (unlikely(err < 0))
16989 +               goto out;
16990 +       token = err;
16991 +       AuDbg("token %d, res{negated %d, uint64 %llu}\n",
16992 +             token, result.negated, result.uint_64);
16993 +
16994 +       err = -EINVAL;
16995 +       a->skipped = 0;
16996 +       switch (token) {
16997 +       case Opt_br:
16998 +               err = au_fsctx_parse_br(fc, param->string);
16999 +               break;
17000 +       case Opt_add:
17001 +               err = au_fsctx_parse_add(fc, param->string);
17002 +               break;
17003 +       case Opt_append:
17004 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17005 +                                           /*dummy bindex*/1);
17006 +               break;
17007 +       case Opt_prepend:
17008 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17009 +                                           /*bindex*/0);
17010 +               break;
17011 +
17012 +       case Opt_del:
17013 +               err = au_fsctx_parse_del(fc, &opt->del, param);
17014 +               break;
17015 +#if 0 /* reserved for future use */
17016 +       case Opt_idel:
17017 +               if (!a->sb) {
17018 +                       err = 0;
17019 +                       a->skipped = 1;
17020 +                       break;
17021 +               }
17022 +               del->pathname = "(indexed)";
17023 +               err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17024 +               break;
17025 +#endif
17026 +
17027 +       case Opt_mod:
17028 +               err = au_fsctx_parse_mod(fc, &opt->mod, param);
17029 +               break;
17030 +#ifdef IMOD /* reserved for future use */
17031 +       case Opt_imod:
17032 +               if (!a->sb) {
17033 +                       err = 0;
17034 +                       a->skipped = 1;
17035 +                       break;
17036 +               }
17037 +               u.mod->path = "(indexed)";
17038 +               err = au_opts_parse_imod(fc, &opt->mod, param->string);
17039 +               break;
17040 +#endif
17041 +
17042 +       case Opt_xino:
17043 +               err = au_fsctx_parse_xino(fc, &opt->xino, param);
17044 +               break;
17045 +       case Opt_trunc_xino_path:
17046 +               if (!a->sb) {
17047 +                       errorfc(fc, "no such branch %s", param->string);
17048 +                       break;
17049 +               }
17050 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17051 +                                                     param->string);
17052 +               break;
17053 +#if 0
17054 +       case Opt_trunc_xino_v:
17055 +               if (!a->sb) {
17056 +                       err = 0;
17057 +                       a->skipped = 1;
17058 +                       break;
17059 +               }
17060 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17061 +                                                     param->string);
17062 +               break;
17063 +#endif
17064 +       case Opt_itrunc_xino:
17065 +               if (!a->sb) {
17066 +                       errorfc(fc, "out of bounds %s", param->string);
17067 +                       break;
17068 +               }
17069 +               err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17070 +                                                result.int_32);
17071 +               break;
17072 +
17073 +       case Opt_dirwh:
17074 +               err = 0;
17075 +               opt->dirwh = result.int_32;
17076 +               break;
17077 +
17078 +       case Opt_rdcache:
17079 +               if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17080 +                       errorfc(fc, "rdcache must be smaller than %d",
17081 +                               AUFS_RDCACHE_MAX);
17082 +                       break;
17083 +               }
17084 +               err = 0;
17085 +               opt->rdcache = result.int_32;
17086 +               break;
17087 +
17088 +       case Opt_rdblk:
17089 +               err = 0;
17090 +               opt->rdblk = AUFS_RDBLK_DEF;
17091 +               if (!strcmp(param->string, "def"))
17092 +                       break;
17093 +
17094 +               err = kstrtoint(param->string, 0, &result.int_32);
17095 +               if (unlikely(err)) {
17096 +                       errorfc(fc, "bad value in %s", param->key);
17097 +                       break;
17098 +               }
17099 +               err = -EINVAL;
17100 +               if (unlikely(result.int_32 < 0
17101 +                            || result.int_32 > KMALLOC_MAX_SIZE)) {
17102 +                       errorfc(fc, "bad value in %s", param->key);
17103 +                       break;
17104 +               }
17105 +               if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17106 +                       errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17107 +                       break;
17108 +               }
17109 +               err = 0;
17110 +               opt->rdblk = result.int_32;
17111 +               break;
17112 +
17113 +       case Opt_rdhash:
17114 +               err = 0;
17115 +               opt->rdhash = AUFS_RDHASH_DEF;
17116 +               if (!strcmp(param->string, "def"))
17117 +                       break;
17118 +
17119 +               err = kstrtoint(param->string, 0, &result.int_32);
17120 +               if (unlikely(err)) {
17121 +                       errorfc(fc, "bad value in %s", param->key);
17122 +                       break;
17123 +               }
17124 +               /* how about zero? */
17125 +               if (result.int_32 < 0
17126 +                   || result.int_32 * sizeof(struct hlist_head)
17127 +                   > KMALLOC_MAX_SIZE) {
17128 +                       err = -EINVAL;
17129 +                       errorfc(fc, "bad integer in %s", param->key);
17130 +                       break;
17131 +               }
17132 +               opt->rdhash = result.int_32;
17133 +               break;
17134 +
17135 +       case Opt_diropq:
17136 +               /*
17137 +                * As other options, fs/aufs/opts.c can handle these strings by
17138 +                * match_token().  But "diropq=" is deprecated now and will
17139 +                * never have other value.  So simple strcmp() is enough here.
17140 +                */
17141 +               if (!strcmp(param->string, "a") ||
17142 +                   !strcmp(param->string, "always")) {
17143 +                       err = 0;
17144 +                       opt->type = Opt_diropq_a;
17145 +               } else if (!strcmp(param->string, "w") ||
17146 +                          !strcmp(param->string, "whiteouted")) {
17147 +                       err = 0;
17148 +                       opt->type = Opt_diropq_w;
17149 +               } else
17150 +                       errorfc(fc, "unknown value %s", param->string);
17151 +               break;
17152 +
17153 +       case Opt_udba:
17154 +               opt->udba = au_udba_val(param->string);
17155 +               if (opt->udba >= 0)
17156 +                       err = 0;
17157 +               else
17158 +                       errorf(fc, "wrong value, %s", param->string);
17159 +               break;
17160 +
17161 +       case Opt_wbr_create:
17162 +               opt->wbr_create.wbr_create
17163 +                       = au_wbr_create_val(param->string, &opt->wbr_create);
17164 +               if (opt->wbr_create.wbr_create >= 0)
17165 +                       err = 0;
17166 +               else
17167 +                       errorf(fc, "wrong value, %s", param->key);
17168 +               break;
17169 +
17170 +       case Opt_wbr_copyup:
17171 +               opt->wbr_copyup = au_wbr_copyup_val(param->string);
17172 +               if (opt->wbr_copyup >= 0)
17173 +                       err = 0;
17174 +               else
17175 +                       errorfc(fc, "wrong value, %s", param->key);
17176 +               break;
17177 +
17178 +       case Opt_fhsm_sec:
17179 +               if (unlikely(result.int_32 < 0)) {
17180 +                       errorfc(fc, "bad integer in %s\n", param->key);
17181 +                       break;
17182 +               }
17183 +               err = 0;
17184 +               if (sysaufs_brs)
17185 +                       opt->fhsm_second = result.int_32;
17186 +               else
17187 +                       warnfc(fc, "ignored %s %s", param->key, param->string);
17188 +               break;
17189 +
17190 +       /* simple true/false flag */
17191 +#define au_fsctx_TF(name)                              \
17192 +               case Opt_##name:                        \
17193 +                       err = 0;                        \
17194 +                       opt->tf = !result.negated;      \
17195 +                       break
17196 +       au_fsctx_TF(trunc_xino);
17197 +       au_fsctx_TF(trunc_xib);
17198 +       au_fsctx_TF(dirperm1);
17199 +       au_fsctx_TF(plink);
17200 +       au_fsctx_TF(shwh);
17201 +       au_fsctx_TF(dio);
17202 +       au_fsctx_TF(warn_perm);
17203 +       au_fsctx_TF(verbose);
17204 +       au_fsctx_TF(sum);
17205 +       au_fsctx_TF(dirren);
17206 +       au_fsctx_TF(acl);
17207 +#undef au_fsctx_TF
17208 +
17209 +       case Opt_noverbose:
17210 +               err = 0;
17211 +               opt->type = Opt_verbose;
17212 +               opt->tf = false;
17213 +               break;
17214 +
17215 +       case Opt_noxino:
17216 +               fallthrough;
17217 +       case Opt_list_plink:
17218 +               fallthrough;
17219 +       case Opt_wsum:
17220 +               err = 0;
17221 +               break;
17222 +
17223 +       case Opt_ignore:
17224 +               warnfc(fc, "ignored %s", param->key);
17225 +               fallthrough;
17226 +       case Opt_ignore_silent:
17227 +               a->skipped = 1;
17228 +               err = 0;
17229 +               break;
17230 +       default:
17231 +               a->skipped = 1;
17232 +               err = -ENOPARAM;
17233 +               break;
17234 +       }
17235 +       if (unlikely(err))
17236 +               goto out;
17237 +       if (a->skipped)
17238 +               goto out;
17239 +
17240 +       switch (token) {
17241 +       case Opt_br:
17242 +               fallthrough;
17243 +       case Opt_noverbose:
17244 +               fallthrough;
17245 +       case Opt_diropq:
17246 +               break;
17247 +       default:
17248 +               opt->type = token;
17249 +               break;
17250 +       }
17251 +       opt++;
17252 +       if (unlikely(opt > a->opt_tail)) {
17253 +               err = -E2BIG;
17254 +               opt--;
17255 +       }
17256 +       opt->type = Opt_tail;
17257 +       a->opt = opt;
17258 +
17259 +out:
17260 +       return err;
17261 +}
17262 +
17263 +/*
17264 + * these options accept both 'name=val' and 'name:val' form.
17265 + * some accept optional '=' in its value.
17266 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17267 + */
17268 +static inline unsigned int is_colonopt(char *str)
17269 +{
17270 +#define do_test(name)                                  \
17271 +       if (!strncmp(str, name ":", sizeof(name)))      \
17272 +               return sizeof(name) - 1
17273 +       do_test("br");
17274 +       do_test("add");
17275 +       do_test("ins");
17276 +       do_test("append");
17277 +       do_test("prepend");
17278 +       do_test("del");
17279 +       /* do_test("idel"); */
17280 +       do_test("mod");
17281 +       /* do_test("imod"); */
17282 +#undef do_test
17283 +
17284 +       return 0;
17285 +}
17286 +
17287 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17288 +{
17289 +       int err;
17290 +       unsigned int u;
17291 +       char *str;
17292 +       struct au_fsctx_opts *a = fc->fs_private;
17293 +
17294 +       str = data;
17295 +       AuDbg("str %s\n", str);
17296 +       while (str) {
17297 +               u = is_colonopt(str);
17298 +               if (u)
17299 +                       str[u] = '=';
17300 +               str = strchr(str, ',');
17301 +               if (!str)
17302 +                       break;
17303 +               str++;
17304 +       }
17305 +       str = data;
17306 +       AuDbg("str %s\n", str);
17307 +
17308 +       err = generic_parse_monolithic(fc, str);
17309 +       AuTraceErr(err);
17310 +       au_fsctx_dump(&a->opts);
17311 +
17312 +       return err;
17313 +}
17314 +
17315 +/* ---------------------------------------------------------------------- */
17316 +
17317 +static void au_fsctx_opts_free(struct au_opts *opts)
17318 +{
17319 +       struct au_opt *opt;
17320 +
17321 +       opt = opts->opt;
17322 +       while (opt->type != Opt_tail) {
17323 +               switch (opt->type) {
17324 +               case Opt_add:
17325 +                       fallthrough;
17326 +               case Opt_append:
17327 +                       fallthrough;
17328 +               case Opt_prepend:
17329 +                       kfree(opt->add.pathname);
17330 +                       path_put(&opt->add.path);
17331 +                       break;
17332 +               case Opt_del:
17333 +                       kfree(opt->del.pathname);
17334 +                       fallthrough;
17335 +               case Opt_idel:
17336 +                       path_put(&opt->del.h_path);
17337 +                       break;
17338 +               case Opt_mod:
17339 +                       kfree(opt->mod.path);
17340 +                       fallthrough;
17341 +               case Opt_imod:
17342 +                       dput(opt->mod.h_root);
17343 +                       break;
17344 +               case Opt_xino:
17345 +                       kfree(opt->xino.path);
17346 +                       fput(opt->xino.file);
17347 +                       break;
17348 +               }
17349 +               opt++;
17350 +       }
17351 +}
17352 +
17353 +static void au_fsctx_free(struct fs_context *fc)
17354 +{
17355 +       struct au_fsctx_opts *a = fc->fs_private;
17356 +
17357 +       /* fs_type=%p, root=%pD */
17358 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17359 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17360 +
17361 +       kobject_put(&a->sbinfo->si_kobj);
17362 +       au_fsctx_opts_free(&a->opts);
17363 +       free_page((unsigned long)a->opts.opt);
17364 +       au_kfree_rcu(a);
17365 +}
17366 +
17367 +static const struct fs_context_operations au_fsctx_ops = {
17368 +       .free                   = au_fsctx_free,
17369 +       .parse_param            = au_fsctx_parse_param,
17370 +       .parse_monolithic       = au_fsctx_parse_monolithic,
17371 +       .get_tree               = au_fsctx_get_tree,
17372 +       .reconfigure            = au_fsctx_reconfigure
17373 +       /*
17374 +        * nfs4 requires ->dup()? No.
17375 +        * I don't know what is this ->dup() for.
17376 +        */
17377 +};
17378 +
17379 +int aufs_fsctx_init(struct fs_context *fc)
17380 +{
17381 +       int err;
17382 +       struct au_fsctx_opts *a;
17383 +
17384 +       /* fs_type=%p, root=%pD */
17385 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17386 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17387 +
17388 +       /* they will be freed by au_fsctx_free() */
17389 +       err = -ENOMEM;
17390 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17391 +       if (unlikely(!a))
17392 +               goto out;
17393 +       a->bindex = 0;
17394 +       a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17395 +       if (unlikely(!a->opts.opt))
17396 +               goto out_a;
17397 +       a->opt = a->opts.opt;
17398 +       a->opt->type = Opt_tail;
17399 +       a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17400 +       a->opt_tail = a->opt + a->opts.max_opt - 1;
17401 +       a->opts.sb_flags = fc->sb_flags;
17402 +
17403 +       a->sb = NULL;
17404 +       if (fc->root) {
17405 +               AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17406 +               a->opts.flags = AuOpts_REMOUNT;
17407 +               a->sb = fc->root->d_sb;
17408 +               a->sbinfo = au_sbi(a->sb);
17409 +               kobject_get(&a->sbinfo->si_kobj);
17410 +       } else {
17411 +               a->sbinfo = au_si_alloc(a->sb);
17412 +               AuDebugOn(!a->sbinfo);
17413 +               err = PTR_ERR(a->sbinfo);
17414 +               if (IS_ERR(a->sbinfo))
17415 +                       goto out_opt;
17416 +               au_rw_write_unlock(&a->sbinfo->si_rwsem);
17417 +       }
17418 +
17419 +       err = 0;
17420 +       fc->fs_private = a;
17421 +       fc->ops = &au_fsctx_ops;
17422 +       goto out; /* success */
17423 +
17424 +out_opt:
17425 +       free_page((unsigned long)a->opts.opt);
17426 +out_a:
17427 +       au_kfree_rcu(a);
17428 +out:
17429 +       AuTraceErr(err);
17430 +       return err;
17431 +}
17432 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
17433 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
17434 +++ linux/fs/aufs/fstype.h      2022-08-02 12:12:45.165641939 +0200
17435 @@ -0,0 +1,401 @@
17436 +/* SPDX-License-Identifier: GPL-2.0 */
17437 +/*
17438 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17439 + *
17440 + * This program, aufs is free software; you can redistribute it and/or modify
17441 + * it under the terms of the GNU General Public License as published by
17442 + * the Free Software Foundation; either version 2 of the License, or
17443 + * (at your option) any later version.
17444 + *
17445 + * This program is distributed in the hope that it will be useful,
17446 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17447 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17448 + * GNU General Public License for more details.
17449 + *
17450 + * You should have received a copy of the GNU General Public License
17451 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17452 + */
17453 +
17454 +/*
17455 + * judging filesystem type
17456 + */
17457 +
17458 +#ifndef __AUFS_FSTYPE_H__
17459 +#define __AUFS_FSTYPE_H__
17460 +
17461 +#ifdef __KERNEL__
17462 +
17463 +#include <linux/fs.h>
17464 +#include <linux/magic.h>
17465 +#include <linux/nfs_fs.h>
17466 +#include <linux/romfs_fs.h>
17467 +
17468 +static inline int au_test_aufs(struct super_block *sb)
17469 +{
17470 +       return sb->s_magic == AUFS_SUPER_MAGIC;
17471 +}
17472 +
17473 +static inline const char *au_sbtype(struct super_block *sb)
17474 +{
17475 +       return sb->s_type->name;
17476 +}
17477 +
17478 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17479 +{
17480 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17481 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
17482 +#else
17483 +       return 0;
17484 +#endif
17485 +}
17486 +
17487 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17488 +{
17489 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17490 +       return sb->s_magic == ROMFS_MAGIC;
17491 +#else
17492 +       return 0;
17493 +#endif
17494 +}
17495 +
17496 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17497 +{
17498 +#if IS_ENABLED(CONFIG_CRAMFS)
17499 +       return sb->s_magic == CRAMFS_MAGIC;
17500 +#endif
17501 +       return 0;
17502 +}
17503 +
17504 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17505 +{
17506 +#if IS_ENABLED(CONFIG_NFS_FS)
17507 +       return sb->s_magic == NFS_SUPER_MAGIC;
17508 +#else
17509 +       return 0;
17510 +#endif
17511 +}
17512 +
17513 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17514 +{
17515 +#if IS_ENABLED(CONFIG_FUSE_FS)
17516 +       return sb->s_magic == FUSE_SUPER_MAGIC;
17517 +#else
17518 +       return 0;
17519 +#endif
17520 +}
17521 +
17522 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17523 +{
17524 +#if IS_ENABLED(CONFIG_XFS_FS)
17525 +       return sb->s_magic == XFS_SB_MAGIC;
17526 +#else
17527 +       return 0;
17528 +#endif
17529 +}
17530 +
17531 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17532 +{
17533 +#ifdef CONFIG_TMPFS
17534 +       return sb->s_magic == TMPFS_MAGIC;
17535 +#else
17536 +       return 0;
17537 +#endif
17538 +}
17539 +
17540 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17541 +{
17542 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17543 +       return !strcmp(au_sbtype(sb), "ecryptfs");
17544 +#else
17545 +       return 0;
17546 +#endif
17547 +}
17548 +
17549 +static inline int au_test_ramfs(struct super_block *sb)
17550 +{
17551 +       return sb->s_magic == RAMFS_MAGIC;
17552 +}
17553 +
17554 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17555 +{
17556 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17557 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
17558 +#else
17559 +       return 0;
17560 +#endif
17561 +}
17562 +
17563 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17564 +{
17565 +#ifdef CONFIG_PROC_FS
17566 +       return sb->s_magic == PROC_SUPER_MAGIC;
17567 +#else
17568 +       return 0;
17569 +#endif
17570 +}
17571 +
17572 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17573 +{
17574 +#ifdef CONFIG_SYSFS
17575 +       return sb->s_magic == SYSFS_MAGIC;
17576 +#else
17577 +       return 0;
17578 +#endif
17579 +}
17580 +
17581 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17582 +{
17583 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17584 +       return sb->s_magic == CONFIGFS_MAGIC;
17585 +#else
17586 +       return 0;
17587 +#endif
17588 +}
17589 +
17590 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17591 +{
17592 +#if IS_ENABLED(CONFIG_MINIX_FS)
17593 +       return sb->s_magic == MINIX3_SUPER_MAGIC
17594 +               || sb->s_magic == MINIX2_SUPER_MAGIC
17595 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
17596 +               || sb->s_magic == MINIX_SUPER_MAGIC
17597 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
17598 +#else
17599 +       return 0;
17600 +#endif
17601 +}
17602 +
17603 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17604 +{
17605 +#if IS_ENABLED(CONFIG_FAT_FS)
17606 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
17607 +#else
17608 +       return 0;
17609 +#endif
17610 +}
17611 +
17612 +static inline int au_test_msdos(struct super_block *sb)
17613 +{
17614 +       return au_test_fat(sb);
17615 +}
17616 +
17617 +static inline int au_test_vfat(struct super_block *sb)
17618 +{
17619 +       return au_test_fat(sb);
17620 +}
17621 +
17622 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17623 +{
17624 +#ifdef CONFIG_SECURITYFS
17625 +       return sb->s_magic == SECURITYFS_MAGIC;
17626 +#else
17627 +       return 0;
17628 +#endif
17629 +}
17630 +
17631 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17632 +{
17633 +#if IS_ENABLED(CONFIG_SQUASHFS)
17634 +       return sb->s_magic == SQUASHFS_MAGIC;
17635 +#else
17636 +       return 0;
17637 +#endif
17638 +}
17639 +
17640 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17641 +{
17642 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17643 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
17644 +#else
17645 +       return 0;
17646 +#endif
17647 +}
17648 +
17649 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17650 +{
17651 +#if IS_ENABLED(CONFIG_XENFS)
17652 +       return sb->s_magic == XENFS_SUPER_MAGIC;
17653 +#else
17654 +       return 0;
17655 +#endif
17656 +}
17657 +
17658 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17659 +{
17660 +#ifdef CONFIG_DEBUG_FS
17661 +       return sb->s_magic == DEBUGFS_MAGIC;
17662 +#else
17663 +       return 0;
17664 +#endif
17665 +}
17666 +
17667 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17668 +{
17669 +#if IS_ENABLED(CONFIG_NILFS)
17670 +       return sb->s_magic == NILFS_SUPER_MAGIC;
17671 +#else
17672 +       return 0;
17673 +#endif
17674 +}
17675 +
17676 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17677 +{
17678 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17679 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17680 +#else
17681 +       return 0;
17682 +#endif
17683 +}
17684 +
17685 +/* ---------------------------------------------------------------------- */
17686 +/*
17687 + * they can't be an aufs branch.
17688 + */
17689 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17690 +{
17691 +       return
17692 +#ifndef CONFIG_AUFS_BR_RAMFS
17693 +               au_test_ramfs(sb) ||
17694 +#endif
17695 +               au_test_procfs(sb)
17696 +               || au_test_sysfs(sb)
17697 +               || au_test_configfs(sb)
17698 +               || au_test_debugfs(sb)
17699 +               || au_test_securityfs(sb)
17700 +               || au_test_xenfs(sb)
17701 +               || au_test_ecryptfs(sb)
17702 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
17703 +               || au_test_aufs(sb); /* will be supported in next version */
17704 +}
17705 +
17706 +static inline int au_test_fs_remote(struct super_block *sb)
17707 +{
17708 +       return !au_test_tmpfs(sb)
17709 +#ifdef CONFIG_AUFS_BR_RAMFS
17710 +               && !au_test_ramfs(sb)
17711 +#endif
17712 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17713 +}
17714 +
17715 +/* ---------------------------------------------------------------------- */
17716 +
17717 +/*
17718 + * Note: these functions (below) are created after reading ->getattr() in all
17719 + * filesystems under linux/fs. it means we have to do so in every update...
17720 + */
17721 +
17722 +/*
17723 + * some filesystems require getattr to refresh the inode attributes before
17724 + * referencing.
17725 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17726 + * and leave the work for d_revalidate()
17727 + */
17728 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17729 +{
17730 +       return au_test_nfs(sb)
17731 +               || au_test_fuse(sb)
17732 +               /* || au_test_btrfs(sb) */      /* untested */
17733 +               ;
17734 +}
17735 +
17736 +/*
17737 + * filesystems which don't maintain i_size or i_blocks.
17738 + */
17739 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17740 +{
17741 +       return au_test_xfs(sb)
17742 +               || au_test_btrfs(sb)
17743 +               || au_test_ubifs(sb)
17744 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
17745 +               /* || au_test_minix(sb) */      /* untested */
17746 +               ;
17747 +}
17748 +
17749 +/*
17750 + * filesystems which don't store the correct value in some of their inode
17751 + * attributes.
17752 + */
17753 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17754 +{
17755 +       return au_test_fs_bad_iattr_size(sb)
17756 +               || au_test_fat(sb)
17757 +               || au_test_msdos(sb)
17758 +               || au_test_vfat(sb);
17759 +}
17760 +
17761 +/* they don't check i_nlink in link(2) */
17762 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17763 +{
17764 +       return au_test_tmpfs(sb)
17765 +#ifdef CONFIG_AUFS_BR_RAMFS
17766 +               || au_test_ramfs(sb)
17767 +#endif
17768 +               || au_test_ubifs(sb)
17769 +               || au_test_hfsplus(sb);
17770 +}
17771 +
17772 +/*
17773 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17774 + */
17775 +static inline int au_test_fs_notime(struct super_block *sb)
17776 +{
17777 +       return au_test_nfs(sb)
17778 +               || au_test_fuse(sb)
17779 +               || au_test_ubifs(sb)
17780 +               ;
17781 +}
17782 +
17783 +/* temporary support for i#1 in cramfs */
17784 +static inline int au_test_fs_unique_ino(struct inode *inode)
17785 +{
17786 +       if (au_test_cramfs(inode->i_sb))
17787 +               return inode->i_ino != 1;
17788 +       return 1;
17789 +}
17790 +
17791 +/* ---------------------------------------------------------------------- */
17792 +
17793 +/*
17794 + * the filesystem where the xino files placed must support i/o after unlink and
17795 + * maintain i_size and i_blocks.
17796 + */
17797 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17798 +{
17799 +       return au_test_fs_remote(sb)
17800 +               || au_test_fs_bad_iattr_size(sb)
17801 +               /* don't want unnecessary work for xino */
17802 +               || au_test_aufs(sb)
17803 +               || au_test_ecryptfs(sb)
17804 +               || au_test_nilfs(sb);
17805 +}
17806 +
17807 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17808 +{
17809 +       return au_test_tmpfs(sb)
17810 +               || au_test_ramfs(sb);
17811 +}
17812 +
17813 +/*
17814 + * test if the @sb is real-readonly.
17815 + */
17816 +static inline int au_test_fs_rr(struct super_block *sb)
17817 +{
17818 +       return au_test_squashfs(sb)
17819 +               || au_test_iso9660(sb)
17820 +               || au_test_cramfs(sb)
17821 +               || au_test_romfs(sb);
17822 +}
17823 +
17824 +/*
17825 + * test if the @inode is nfs with 'noacl' option
17826 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17827 + */
17828 +static inline int au_test_nfs_noacl(struct inode *inode)
17829 +{
17830 +       return au_test_nfs(inode->i_sb)
17831 +               /* && IS_POSIXACL(inode) */
17832 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
17833 +}
17834 +
17835 +#endif /* __KERNEL__ */
17836 +#endif /* __AUFS_FSTYPE_H__ */
17837 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
17838 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
17839 +++ linux/fs/aufs/hbl.h 2022-08-02 12:12:45.165641939 +0200
17840 @@ -0,0 +1,65 @@
17841 +/* SPDX-License-Identifier: GPL-2.0 */
17842 +/*
17843 + * Copyright (C) 2017-2021 Junjiro R. Okajima
17844 + *
17845 + * This program, aufs is free software; you can redistribute it and/or modify
17846 + * it under the terms of the GNU General Public License as published by
17847 + * the Free Software Foundation; either version 2 of the License, or
17848 + * (at your option) any later version.
17849 + *
17850 + * This program is distributed in the hope that it will be useful,
17851 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17852 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17853 + * GNU General Public License for more details.
17854 + *
17855 + * You should have received a copy of the GNU General Public License
17856 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17857 + */
17858 +
17859 +/*
17860 + * helpers for hlist_bl.h
17861 + */
17862 +
17863 +#ifndef __AUFS_HBL_H__
17864 +#define __AUFS_HBL_H__
17865 +
17866 +#ifdef __KERNEL__
17867 +
17868 +#include <linux/list_bl.h>
17869 +
17870 +static inline void au_hbl_add(struct hlist_bl_node *node,
17871 +                             struct hlist_bl_head *hbl)
17872 +{
17873 +       hlist_bl_lock(hbl);
17874 +       hlist_bl_add_head(node, hbl);
17875 +       hlist_bl_unlock(hbl);
17876 +}
17877 +
17878 +static inline void au_hbl_del(struct hlist_bl_node *node,
17879 +                             struct hlist_bl_head *hbl)
17880 +{
17881 +       hlist_bl_lock(hbl);
17882 +       hlist_bl_del(node);
17883 +       hlist_bl_unlock(hbl);
17884 +}
17885 +
17886 +#define au_hbl_for_each(pos, head)                                     \
17887 +       for (pos = hlist_bl_first(head);                                \
17888 +            pos;                                                       \
17889 +            pos = pos->next)
17890 +
17891 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17892 +{
17893 +       unsigned long cnt;
17894 +       struct hlist_bl_node *pos;
17895 +
17896 +       cnt = 0;
17897 +       hlist_bl_lock(hbl);
17898 +       au_hbl_for_each(pos, hbl)
17899 +               cnt++;
17900 +       hlist_bl_unlock(hbl);
17901 +       return cnt;
17902 +}
17903 +
17904 +#endif /* __KERNEL__ */
17905 +#endif /* __AUFS_HBL_H__ */
17906 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
17907 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
17908 +++ linux/fs/aufs/hfsnotify.c   2022-08-02 12:12:45.165641939 +0200
17909 @@ -0,0 +1,290 @@
17910 +// SPDX-License-Identifier: GPL-2.0
17911 +/*
17912 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17913 + *
17914 + * This program, aufs is free software; you can redistribute it and/or modify
17915 + * it under the terms of the GNU General Public License as published by
17916 + * the Free Software Foundation; either version 2 of the License, or
17917 + * (at your option) any later version.
17918 + *
17919 + * This program is distributed in the hope that it will be useful,
17920 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17921 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17922 + * GNU General Public License for more details.
17923 + *
17924 + * You should have received a copy of the GNU General Public License
17925 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17926 + */
17927 +
17928 +/*
17929 + * fsnotify for the lower directories
17930 + */
17931 +
17932 +#include "aufs.h"
17933 +
17934 +/* FS_IN_IGNORED is unnecessary */
17935 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17936 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
17937 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17938 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17939 +
17940 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17941 +{
17942 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17943 +                                            hn_mark);
17944 +       /* AuDbg("here\n"); */
17945 +       au_cache_free_hnotify(hn);
17946 +       smp_mb__before_atomic(); /* for atomic64_dec */
17947 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
17948 +               wake_up(&au_hfsn_wq);
17949 +}
17950 +
17951 +static int au_hfsn_alloc(struct au_hinode *hinode)
17952 +{
17953 +       int err;
17954 +       struct au_hnotify *hn;
17955 +       struct super_block *sb;
17956 +       struct au_branch *br;
17957 +       struct fsnotify_mark *mark;
17958 +       aufs_bindex_t bindex;
17959 +
17960 +       hn = hinode->hi_notify;
17961 +       sb = hn->hn_aufs_inode->i_sb;
17962 +       bindex = au_br_index(sb, hinode->hi_id);
17963 +       br = au_sbr(sb, bindex);
17964 +       AuDebugOn(!br->br_hfsn);
17965 +
17966 +       mark = &hn->hn_mark;
17967 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17968 +       mark->mask = AuHfsnMask;
17969 +       /*
17970 +        * by udba rename or rmdir, aufs assign a new inode to the known
17971 +        * h_inode, so specify 1 to allow dups.
17972 +        */
17973 +       lockdep_off();
17974 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
17975 +       lockdep_on();
17976 +
17977 +       return err;
17978 +}
17979 +
17980 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
17981 +{
17982 +       struct fsnotify_mark *mark;
17983 +       unsigned long long ull;
17984 +       struct fsnotify_group *group;
17985 +
17986 +       ull = atomic64_inc_return(&au_hfsn_ifree);
17987 +       BUG_ON(!ull);
17988 +
17989 +       mark = &hn->hn_mark;
17990 +       spin_lock(&mark->lock);
17991 +       group = mark->group;
17992 +       fsnotify_get_group(group);
17993 +       spin_unlock(&mark->lock);
17994 +       lockdep_off();
17995 +       fsnotify_destroy_mark(mark, group);
17996 +       fsnotify_put_mark(mark);
17997 +       fsnotify_put_group(group);
17998 +       lockdep_on();
17999 +
18000 +       /* free hn by myself */
18001 +       return 0;
18002 +}
18003 +
18004 +/* ---------------------------------------------------------------------- */
18005 +
18006 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
18007 +{
18008 +       struct fsnotify_mark *mark;
18009 +
18010 +       mark = &hinode->hi_notify->hn_mark;
18011 +       spin_lock(&mark->lock);
18012 +       if (do_set) {
18013 +               AuDebugOn(mark->mask & AuHfsnMask);
18014 +               mark->mask |= AuHfsnMask;
18015 +       } else {
18016 +               AuDebugOn(!(mark->mask & AuHfsnMask));
18017 +               mark->mask &= ~AuHfsnMask;
18018 +       }
18019 +       spin_unlock(&mark->lock);
18020 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
18021 +}
18022 +
18023 +/* ---------------------------------------------------------------------- */
18024 +
18025 +/* #define AuDbgHnotify */
18026 +#ifdef AuDbgHnotify
18027 +static char *au_hfsn_name(u32 mask)
18028 +{
18029 +#ifdef CONFIG_AUFS_DEBUG
18030 +#define test_ret(flag)                         \
18031 +       do {                                    \
18032 +               if (mask & flag)                \
18033 +                       return #flag;           \
18034 +       } while (0)
18035 +       test_ret(FS_ACCESS);
18036 +       test_ret(FS_MODIFY);
18037 +       test_ret(FS_ATTRIB);
18038 +       test_ret(FS_CLOSE_WRITE);
18039 +       test_ret(FS_CLOSE_NOWRITE);
18040 +       test_ret(FS_OPEN);
18041 +       test_ret(FS_MOVED_FROM);
18042 +       test_ret(FS_MOVED_TO);
18043 +       test_ret(FS_CREATE);
18044 +       test_ret(FS_DELETE);
18045 +       test_ret(FS_DELETE_SELF);
18046 +       test_ret(FS_MOVE_SELF);
18047 +       test_ret(FS_UNMOUNT);
18048 +       test_ret(FS_Q_OVERFLOW);
18049 +       test_ret(FS_IN_IGNORED);
18050 +       test_ret(FS_ISDIR);
18051 +       test_ret(FS_IN_ONESHOT);
18052 +       test_ret(FS_EVENT_ON_CHILD);
18053 +       return "";
18054 +#undef test_ret
18055 +#else
18056 +       return "??";
18057 +#endif
18058 +}
18059 +#endif
18060 +
18061 +/* ---------------------------------------------------------------------- */
18062 +
18063 +static void au_hfsn_free_group(struct fsnotify_group *group)
18064 +{
18065 +       struct au_br_hfsnotify *hfsn = group->private;
18066 +
18067 +       /* AuDbg("here\n"); */
18068 +       au_kfree_try_rcu(hfsn);
18069 +}
18070 +
18071 +static int au_hfsn_handle_event(struct fsnotify_group *group,
18072 +                               u32 mask, const void *data, int data_type,
18073 +                               struct inode *dir,
18074 +                               const struct qstr *file_name, u32 cookie,
18075 +                               struct fsnotify_iter_info *iter_info)
18076 +{
18077 +       int err;
18078 +       struct au_hnotify *hnotify;
18079 +       struct inode *h_dir, *h_inode;
18080 +       struct fsnotify_mark *inode_mark;
18081 +
18082 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18083 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
18084 +
18085 +       err = 0;
18086 +       /* if FS_UNMOUNT happens, there must be another bug */
18087 +       AuDebugOn(mask & FS_UNMOUNT);
18088 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
18089 +               goto out;
18090 +
18091 +       h_dir = dir;
18092 +       h_inode = NULL;
18093 +#ifdef AuDbgHnotify
18094 +       au_debug_on();
18095 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
18096 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
18097 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18098 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
18099 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
18100 +               /* WARN_ON(1); */
18101 +       }
18102 +       au_debug_off();
18103 +#endif
18104 +
18105 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
18106 +       AuDebugOn(!inode_mark);
18107 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
18108 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
18109 +
18110 +out:
18111 +       return err;
18112 +}
18113 +
18114 +static struct fsnotify_ops au_hfsn_ops = {
18115 +       .handle_event           = au_hfsn_handle_event,
18116 +       .free_group_priv        = au_hfsn_free_group,
18117 +       .free_mark              = au_hfsn_free_mark
18118 +};
18119 +
18120 +/* ---------------------------------------------------------------------- */
18121 +
18122 +static void au_hfsn_fin_br(struct au_branch *br)
18123 +{
18124 +       struct au_br_hfsnotify *hfsn;
18125 +
18126 +       hfsn = br->br_hfsn;
18127 +       if (hfsn) {
18128 +               lockdep_off();
18129 +               fsnotify_put_group(hfsn->hfsn_group);
18130 +               lockdep_on();
18131 +       }
18132 +}
18133 +
18134 +static int au_hfsn_init_br(struct au_branch *br, int perm)
18135 +{
18136 +       int err;
18137 +       struct fsnotify_group *group;
18138 +       struct au_br_hfsnotify *hfsn;
18139 +
18140 +       err = 0;
18141 +       br->br_hfsn = NULL;
18142 +       if (!au_br_hnotifyable(perm))
18143 +               goto out;
18144 +
18145 +       err = -ENOMEM;
18146 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18147 +       if (unlikely(!hfsn))
18148 +               goto out;
18149 +
18150 +       err = 0;
18151 +       group = fsnotify_alloc_group(&au_hfsn_ops,
18152 +                                    /*flags - not for userspace*/0);
18153 +       if (IS_ERR(group)) {
18154 +               err = PTR_ERR(group);
18155 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
18156 +               goto out_hfsn;
18157 +       }
18158 +
18159 +       group->private = hfsn;
18160 +       hfsn->hfsn_group = group;
18161 +       br->br_hfsn = hfsn;
18162 +       goto out; /* success */
18163 +
18164 +out_hfsn:
18165 +       au_kfree_try_rcu(hfsn);
18166 +out:
18167 +       return err;
18168 +}
18169 +
18170 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18171 +{
18172 +       int err;
18173 +
18174 +       err = 0;
18175 +       if (!br->br_hfsn)
18176 +               err = au_hfsn_init_br(br, perm);
18177 +
18178 +       return err;
18179 +}
18180 +
18181 +/* ---------------------------------------------------------------------- */
18182 +
18183 +static void au_hfsn_fin(void)
18184 +{
18185 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18186 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18187 +}
18188 +
18189 +const struct au_hnotify_op au_hnotify_op = {
18190 +       .ctl            = au_hfsn_ctl,
18191 +       .alloc          = au_hfsn_alloc,
18192 +       .free           = au_hfsn_free,
18193 +
18194 +       .fin            = au_hfsn_fin,
18195 +
18196 +       .reset_br       = au_hfsn_reset_br,
18197 +       .fin_br         = au_hfsn_fin_br,
18198 +       .init_br        = au_hfsn_init_br
18199 +};
18200 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
18201 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
18202 +++ linux/fs/aufs/hfsplus.c     2022-08-02 12:12:45.165641939 +0200
18203 @@ -0,0 +1,60 @@
18204 +// SPDX-License-Identifier: GPL-2.0
18205 +/*
18206 + * Copyright (C) 2010-2021 Junjiro R. Okajima
18207 + *
18208 + * This program, aufs is free software; you can redistribute it and/or modify
18209 + * it under the terms of the GNU General Public License as published by
18210 + * the Free Software Foundation; either version 2 of the License, or
18211 + * (at your option) any later version.
18212 + *
18213 + * This program is distributed in the hope that it will be useful,
18214 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18215 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18216 + * GNU General Public License for more details.
18217 + *
18218 + * You should have received a copy of the GNU General Public License
18219 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18220 + */
18221 +
18222 +/*
18223 + * special support for filesystems which acquires an inode mutex
18224 + * at final closing a file, eg, hfsplus.
18225 + *
18226 + * This trick is very simple and stupid, just to open the file before really
18227 + * necessary open to tell hfsplus that this is not the final closing.
18228 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
18229 + * and au_h_open_post() after releasing it.
18230 + */
18231 +
18232 +#include "aufs.h"
18233 +
18234 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18235 +                          int force_wr)
18236 +{
18237 +       struct file *h_file;
18238 +       struct dentry *h_dentry;
18239 +
18240 +       h_dentry = au_h_dptr(dentry, bindex);
18241 +       AuDebugOn(!h_dentry);
18242 +       AuDebugOn(d_is_negative(h_dentry));
18243 +
18244 +       h_file = NULL;
18245 +       if (au_test_hfsplus(h_dentry->d_sb)
18246 +           && d_is_reg(h_dentry))
18247 +               h_file = au_h_open(dentry, bindex,
18248 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
18249 +                                  /*file*/NULL, force_wr);
18250 +       return h_file;
18251 +}
18252 +
18253 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18254 +                   struct file *h_file)
18255 +{
18256 +       struct au_branch *br;
18257 +
18258 +       if (h_file) {
18259 +               fput(h_file);
18260 +               br = au_sbr(dentry->d_sb, bindex);
18261 +               au_lcnt_dec(&br->br_nfiles);
18262 +       }
18263 +}
18264 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
18265 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
18266 +++ linux/fs/aufs/hnotify.c     2022-08-02 12:12:45.165641939 +0200
18267 @@ -0,0 +1,715 @@
18268 +// SPDX-License-Identifier: GPL-2.0
18269 +/*
18270 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18271 + *
18272 + * This program, aufs is free software; you can redistribute it and/or modify
18273 + * it under the terms of the GNU General Public License as published by
18274 + * the Free Software Foundation; either version 2 of the License, or
18275 + * (at your option) any later version.
18276 + *
18277 + * This program is distributed in the hope that it will be useful,
18278 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18279 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18280 + * GNU General Public License for more details.
18281 + *
18282 + * You should have received a copy of the GNU General Public License
18283 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18284 + */
18285 +
18286 +/*
18287 + * abstraction to notify the direct changes on lower directories
18288 + */
18289 +
18290 +/* #include <linux/iversion.h> */
18291 +#include "aufs.h"
18292 +
18293 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18294 +{
18295 +       int err;
18296 +       struct au_hnotify *hn;
18297 +
18298 +       err = -ENOMEM;
18299 +       hn = au_cache_alloc_hnotify();
18300 +       if (hn) {
18301 +               hn->hn_aufs_inode = inode;
18302 +               hinode->hi_notify = hn;
18303 +               err = au_hnotify_op.alloc(hinode);
18304 +               AuTraceErr(err);
18305 +               if (unlikely(err)) {
18306 +                       hinode->hi_notify = NULL;
18307 +                       au_cache_free_hnotify(hn);
18308 +                       /*
18309 +                        * The upper dir was removed by udba, but the same named
18310 +                        * dir left. In this case, aufs assigns a new inode
18311 +                        * number and set the monitor again.
18312 +                        * For the lower dir, the old monitor is still left.
18313 +                        */
18314 +                       if (err == -EEXIST)
18315 +                               err = 0;
18316 +               }
18317 +       }
18318 +
18319 +       AuTraceErr(err);
18320 +       return err;
18321 +}
18322 +
18323 +void au_hn_free(struct au_hinode *hinode)
18324 +{
18325 +       struct au_hnotify *hn;
18326 +
18327 +       hn = hinode->hi_notify;
18328 +       if (hn) {
18329 +               hinode->hi_notify = NULL;
18330 +               if (au_hnotify_op.free(hinode, hn))
18331 +                       au_cache_free_hnotify(hn);
18332 +       }
18333 +}
18334 +
18335 +/* ---------------------------------------------------------------------- */
18336 +
18337 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18338 +{
18339 +       if (hinode->hi_notify)
18340 +               au_hnotify_op.ctl(hinode, do_set);
18341 +}
18342 +
18343 +void au_hn_reset(struct inode *inode, unsigned int flags)
18344 +{
18345 +       aufs_bindex_t bindex, bbot;
18346 +       struct inode *hi;
18347 +       struct dentry *iwhdentry;
18348 +
18349 +       bbot = au_ibbot(inode);
18350 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18351 +               hi = au_h_iptr(inode, bindex);
18352 +               if (!hi)
18353 +                       continue;
18354 +
18355 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18356 +               iwhdentry = au_hi_wh(inode, bindex);
18357 +               if (iwhdentry)
18358 +                       dget(iwhdentry);
18359 +               au_igrab(hi);
18360 +               au_set_h_iptr(inode, bindex, NULL, 0);
18361 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
18362 +                             flags & ~AuHi_XINO);
18363 +               iput(hi);
18364 +               dput(iwhdentry);
18365 +               /* inode_unlock(hi); */
18366 +       }
18367 +}
18368 +
18369 +/* ---------------------------------------------------------------------- */
18370 +
18371 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18372 +{
18373 +       int err;
18374 +       aufs_bindex_t bindex, bbot, bfound, btop;
18375 +       struct inode *h_i;
18376 +
18377 +       err = 0;
18378 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18379 +               pr_warn("branch root dir was changed\n");
18380 +               goto out;
18381 +       }
18382 +
18383 +       bfound = -1;
18384 +       bbot = au_ibbot(inode);
18385 +       btop = au_ibtop(inode);
18386 +#if 0 /* reserved for future use */
18387 +       if (bindex == bbot) {
18388 +               /* keep this ino in rename case */
18389 +               goto out;
18390 +       }
18391 +#endif
18392 +       for (bindex = btop; bindex <= bbot; bindex++)
18393 +               if (au_h_iptr(inode, bindex) == h_inode) {
18394 +                       bfound = bindex;
18395 +                       break;
18396 +               }
18397 +       if (bfound < 0)
18398 +               goto out;
18399 +
18400 +       for (bindex = btop; bindex <= bbot; bindex++) {
18401 +               h_i = au_h_iptr(inode, bindex);
18402 +               if (!h_i)
18403 +                       continue;
18404 +
18405 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18406 +               /* ignore this error */
18407 +               /* bad action? */
18408 +       }
18409 +
18410 +       /* children inode number will be broken */
18411 +
18412 +out:
18413 +       AuTraceErr(err);
18414 +       return err;
18415 +}
18416 +
18417 +static int hn_gen_tree(struct dentry *dentry)
18418 +{
18419 +       int err, i, j, ndentry;
18420 +       struct au_dcsub_pages dpages;
18421 +       struct au_dpage *dpage;
18422 +       struct dentry **dentries;
18423 +
18424 +       err = au_dpages_init(&dpages, GFP_NOFS);
18425 +       if (unlikely(err))
18426 +               goto out;
18427 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18428 +       if (unlikely(err))
18429 +               goto out_dpages;
18430 +
18431 +       for (i = 0; i < dpages.ndpage; i++) {
18432 +               dpage = dpages.dpages + i;
18433 +               dentries = dpage->dentries;
18434 +               ndentry = dpage->ndentry;
18435 +               for (j = 0; j < ndentry; j++) {
18436 +                       struct dentry *d;
18437 +
18438 +                       d = dentries[j];
18439 +                       if (IS_ROOT(d))
18440 +                               continue;
18441 +
18442 +                       au_digen_dec(d);
18443 +                       if (d_really_is_positive(d))
18444 +                               /* todo: reset children xino?
18445 +                                  cached children only? */
18446 +                               au_iigen_dec(d_inode(d));
18447 +               }
18448 +       }
18449 +
18450 +out_dpages:
18451 +       au_dpages_free(&dpages);
18452 +out:
18453 +       return err;
18454 +}
18455 +
18456 +/*
18457 + * return 0 if processed.
18458 + */
18459 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18460 +                          const unsigned int isdir)
18461 +{
18462 +       int err;
18463 +       struct dentry *d;
18464 +       struct qstr *dname;
18465 +
18466 +       err = 1;
18467 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18468 +               pr_warn("branch root dir was changed\n");
18469 +               err = 0;
18470 +               goto out;
18471 +       }
18472 +
18473 +       if (!isdir) {
18474 +               AuDebugOn(!name);
18475 +               au_iigen_dec(inode);
18476 +               spin_lock(&inode->i_lock);
18477 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18478 +                       spin_lock(&d->d_lock);
18479 +                       dname = &d->d_name;
18480 +                       if (dname->len != nlen
18481 +                           && memcmp(dname->name, name, nlen)) {
18482 +                               spin_unlock(&d->d_lock);
18483 +                               continue;
18484 +                       }
18485 +                       err = 0;
18486 +                       au_digen_dec(d);
18487 +                       spin_unlock(&d->d_lock);
18488 +                       break;
18489 +               }
18490 +               spin_unlock(&inode->i_lock);
18491 +       } else {
18492 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18493 +               d = d_find_any_alias(inode);
18494 +               if (!d) {
18495 +                       au_iigen_dec(inode);
18496 +                       goto out;
18497 +               }
18498 +
18499 +               spin_lock(&d->d_lock);
18500 +               dname = &d->d_name;
18501 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18502 +                       spin_unlock(&d->d_lock);
18503 +                       err = hn_gen_tree(d);
18504 +                       spin_lock(&d->d_lock);
18505 +               }
18506 +               spin_unlock(&d->d_lock);
18507 +               dput(d);
18508 +       }
18509 +
18510 +out:
18511 +       AuTraceErr(err);
18512 +       return err;
18513 +}
18514 +
18515 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18516 +{
18517 +       int err;
18518 +
18519 +       if (IS_ROOT(dentry)) {
18520 +               pr_warn("branch root dir was changed\n");
18521 +               return 0;
18522 +       }
18523 +
18524 +       err = 0;
18525 +       if (!isdir) {
18526 +               au_digen_dec(dentry);
18527 +               if (d_really_is_positive(dentry))
18528 +                       au_iigen_dec(d_inode(dentry));
18529 +       } else {
18530 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18531 +               if (d_really_is_positive(dentry))
18532 +                       err = hn_gen_tree(dentry);
18533 +       }
18534 +
18535 +       AuTraceErr(err);
18536 +       return err;
18537 +}
18538 +
18539 +/* ---------------------------------------------------------------------- */
18540 +
18541 +/* hnotify job flags */
18542 +#define AuHnJob_XINO0          1
18543 +#define AuHnJob_GEN            (1 << 1)
18544 +#define AuHnJob_DIRENT         (1 << 2)
18545 +#define AuHnJob_ISDIR          (1 << 3)
18546 +#define AuHnJob_TRYXINO0       (1 << 4)
18547 +#define AuHnJob_MNTPNT         (1 << 5)
18548 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
18549 +#define au_fset_hnjob(flags, name) \
18550 +       do { (flags) |= AuHnJob_##name; } while (0)
18551 +#define au_fclr_hnjob(flags, name) \
18552 +       do { (flags) &= ~AuHnJob_##name; } while (0)
18553 +
18554 +enum {
18555 +       AuHn_CHILD,
18556 +       AuHn_PARENT,
18557 +       AuHnLast
18558 +};
18559 +
18560 +struct au_hnotify_args {
18561 +       struct inode *h_dir, *dir, *h_child_inode;
18562 +       u32 mask;
18563 +       unsigned int flags[AuHnLast];
18564 +       unsigned int h_child_nlen;
18565 +       char h_child_name[];
18566 +};
18567 +
18568 +struct hn_job_args {
18569 +       unsigned int flags;
18570 +       struct inode *inode, *h_inode, *dir, *h_dir;
18571 +       struct dentry *dentry;
18572 +       char *h_name;
18573 +       int h_nlen;
18574 +};
18575 +
18576 +static int hn_job(struct hn_job_args *a)
18577 +{
18578 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18579 +       int e;
18580 +
18581 +       /* reset xino */
18582 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18583 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
18584 +
18585 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
18586 +           && a->inode
18587 +           && a->h_inode) {
18588 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18589 +               if (!a->h_inode->i_nlink
18590 +                   && !(a->h_inode->i_state & I_LINKABLE))
18591 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
18592 +               inode_unlock_shared(a->h_inode);
18593 +       }
18594 +
18595 +       /* make the generation obsolete */
18596 +       if (au_ftest_hnjob(a->flags, GEN)) {
18597 +               e = -1;
18598 +               if (a->inode)
18599 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18600 +                                             isdir);
18601 +               if (e && a->dentry)
18602 +                       hn_gen_by_name(a->dentry, isdir);
18603 +               /* ignore this error */
18604 +       }
18605 +
18606 +       /* make dir entries obsolete */
18607 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18608 +               struct au_vdir *vdir;
18609 +
18610 +               vdir = au_ivdir(a->inode);
18611 +               if (vdir)
18612 +                       vdir->vd_jiffy = 0;
18613 +               /* IMustLock(a->inode); */
18614 +               /* inode_inc_iversion(a->inode); */
18615 +       }
18616 +
18617 +       /* can do nothing but warn */
18618 +       if (au_ftest_hnjob(a->flags, MNTPNT)
18619 +           && a->dentry
18620 +           && d_mountpoint(a->dentry))
18621 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18622 +
18623 +       return 0;
18624 +}
18625 +
18626 +/* ---------------------------------------------------------------------- */
18627 +
18628 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18629 +                                          struct inode *dir)
18630 +{
18631 +       struct dentry *dentry, *d, *parent;
18632 +       struct qstr *dname;
18633 +
18634 +       parent = d_find_any_alias(dir);
18635 +       if (!parent)
18636 +               return NULL;
18637 +
18638 +       dentry = NULL;
18639 +       spin_lock(&parent->d_lock);
18640 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
18641 +               /* AuDbg("%pd\n", d); */
18642 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18643 +               dname = &d->d_name;
18644 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
18645 +                       goto cont_unlock;
18646 +               if (au_di(d))
18647 +                       au_digen_dec(d);
18648 +               else
18649 +                       goto cont_unlock;
18650 +               if (au_dcount(d) > 0) {
18651 +                       dentry = dget_dlock(d);
18652 +                       spin_unlock(&d->d_lock);
18653 +                       break;
18654 +               }
18655 +
18656 +cont_unlock:
18657 +               spin_unlock(&d->d_lock);
18658 +       }
18659 +       spin_unlock(&parent->d_lock);
18660 +       dput(parent);
18661 +
18662 +       if (dentry)
18663 +               di_write_lock_child(dentry);
18664 +
18665 +       return dentry;
18666 +}
18667 +
18668 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18669 +                                        aufs_bindex_t bindex, ino_t h_ino)
18670 +{
18671 +       struct inode *inode;
18672 +       ino_t ino;
18673 +       int err;
18674 +
18675 +       inode = NULL;
18676 +       err = au_xino_read(sb, bindex, h_ino, &ino);
18677 +       if (!err && ino)
18678 +               inode = ilookup(sb, ino);
18679 +       if (!inode)
18680 +               goto out;
18681 +
18682 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18683 +               pr_warn("wrong root branch\n");
18684 +               iput(inode);
18685 +               inode = NULL;
18686 +               goto out;
18687 +       }
18688 +
18689 +       ii_write_lock_child(inode);
18690 +
18691 +out:
18692 +       return inode;
18693 +}
18694 +
18695 +static void au_hn_bh(void *_args)
18696 +{
18697 +       struct au_hnotify_args *a = _args;
18698 +       struct super_block *sb;
18699 +       aufs_bindex_t bindex, bbot, bfound;
18700 +       unsigned char xino, try_iput;
18701 +       int err;
18702 +       struct inode *inode;
18703 +       ino_t h_ino;
18704 +       struct hn_job_args args;
18705 +       struct dentry *dentry;
18706 +       struct au_sbinfo *sbinfo;
18707 +
18708 +       AuDebugOn(!_args);
18709 +       AuDebugOn(!a->h_dir);
18710 +       AuDebugOn(!a->dir);
18711 +       AuDebugOn(!a->mask);
18712 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18713 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
18714 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
18715 +
18716 +       inode = NULL;
18717 +       dentry = NULL;
18718 +       /*
18719 +        * do not lock a->dir->i_mutex here
18720 +        * because of d_revalidate() may cause a deadlock.
18721 +        */
18722 +       sb = a->dir->i_sb;
18723 +       AuDebugOn(!sb);
18724 +       sbinfo = au_sbi(sb);
18725 +       AuDebugOn(!sbinfo);
18726 +       si_write_lock(sb, AuLock_NOPLMW);
18727 +
18728 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18729 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18730 +               case FS_MOVED_FROM:
18731 +               case FS_MOVED_TO:
18732 +                       AuWarn1("DIRREN with UDBA may not work correctly "
18733 +                               "for the direct rename(2)\n");
18734 +               }
18735 +
18736 +       ii_read_lock_parent(a->dir);
18737 +       bfound = -1;
18738 +       bbot = au_ibbot(a->dir);
18739 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18740 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18741 +                       bfound = bindex;
18742 +                       break;
18743 +               }
18744 +       ii_read_unlock(a->dir);
18745 +       if (unlikely(bfound < 0))
18746 +               goto out;
18747 +
18748 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
18749 +       h_ino = 0;
18750 +       if (a->h_child_inode)
18751 +               h_ino = a->h_child_inode->i_ino;
18752 +
18753 +       if (a->h_child_nlen
18754 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18755 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18756 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18757 +                                             a->dir);
18758 +       try_iput = 0;
18759 +       if (dentry && d_really_is_positive(dentry))
18760 +               inode = d_inode(dentry);
18761 +       if (xino && !inode && h_ino
18762 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18763 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18764 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18765 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18766 +               try_iput = 1;
18767 +       }
18768 +
18769 +       args.flags = a->flags[AuHn_CHILD];
18770 +       args.dentry = dentry;
18771 +       args.inode = inode;
18772 +       args.h_inode = a->h_child_inode;
18773 +       args.dir = a->dir;
18774 +       args.h_dir = a->h_dir;
18775 +       args.h_name = a->h_child_name;
18776 +       args.h_nlen = a->h_child_nlen;
18777 +       err = hn_job(&args);
18778 +       if (dentry) {
18779 +               if (au_di(dentry))
18780 +                       di_write_unlock(dentry);
18781 +               dput(dentry);
18782 +       }
18783 +       if (inode && try_iput) {
18784 +               ii_write_unlock(inode);
18785 +               iput(inode);
18786 +       }
18787 +
18788 +       ii_write_lock_parent(a->dir);
18789 +       args.flags = a->flags[AuHn_PARENT];
18790 +       args.dentry = NULL;
18791 +       args.inode = a->dir;
18792 +       args.h_inode = a->h_dir;
18793 +       args.dir = NULL;
18794 +       args.h_dir = NULL;
18795 +       args.h_name = NULL;
18796 +       args.h_nlen = 0;
18797 +       err = hn_job(&args);
18798 +       ii_write_unlock(a->dir);
18799 +
18800 +out:
18801 +       iput(a->h_child_inode);
18802 +       iput(a->h_dir);
18803 +       iput(a->dir);
18804 +       si_write_unlock(sb);
18805 +       au_nwt_done(&sbinfo->si_nowait);
18806 +       au_kfree_rcu(a);
18807 +}
18808 +
18809 +/* ---------------------------------------------------------------------- */
18810 +
18811 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18812 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
18813 +{
18814 +       int err, len;
18815 +       unsigned int flags[AuHnLast], f;
18816 +       unsigned char isdir, isroot, wh;
18817 +       struct inode *dir;
18818 +       struct au_hnotify_args *args;
18819 +       char *p, *h_child_name;
18820 +
18821 +       err = 0;
18822 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18823 +       dir = igrab(hnotify->hn_aufs_inode);
18824 +       if (!dir)
18825 +               goto out;
18826 +
18827 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
18828 +       wh = 0;
18829 +       h_child_name = (void *)h_child_qstr->name;
18830 +       len = h_child_qstr->len;
18831 +       if (h_child_name) {
18832 +               if (len > AUFS_WH_PFX_LEN
18833 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18834 +                       h_child_name += AUFS_WH_PFX_LEN;
18835 +                       len -= AUFS_WH_PFX_LEN;
18836 +                       wh = 1;
18837 +               }
18838 +       }
18839 +
18840 +       isdir = 0;
18841 +       if (h_child_inode)
18842 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
18843 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
18844 +       flags[AuHn_CHILD] = 0;
18845 +       if (isdir)
18846 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
18847 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18848 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
18849 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18850 +       case FS_MOVED_FROM:
18851 +       case FS_MOVED_TO:
18852 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18853 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18854 +               fallthrough;
18855 +       case FS_CREATE:
18856 +               AuDebugOn(!h_child_name);
18857 +               break;
18858 +
18859 +       case FS_DELETE:
18860 +               /*
18861 +                * aufs never be able to get this child inode.
18862 +                * revalidation should be in d_revalidate()
18863 +                * by checking i_nlink, i_generation or d_unhashed().
18864 +                */
18865 +               AuDebugOn(!h_child_name);
18866 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18867 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18868 +               break;
18869 +
18870 +       default:
18871 +               AuDebugOn(1);
18872 +       }
18873 +
18874 +       if (wh)
18875 +               h_child_inode = NULL;
18876 +
18877 +       err = -ENOMEM;
18878 +       /* iput() and kfree() will be called in au_hnotify() */
18879 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18880 +       if (unlikely(!args)) {
18881 +               AuErr1("no memory\n");
18882 +               iput(dir);
18883 +               goto out;
18884 +       }
18885 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18886 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18887 +       args->mask = mask;
18888 +       args->dir = dir;
18889 +       args->h_dir = igrab(h_dir);
18890 +       if (h_child_inode)
18891 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
18892 +       args->h_child_inode = h_child_inode;
18893 +       args->h_child_nlen = len;
18894 +       if (len) {
18895 +               p = (void *)args;
18896 +               p += sizeof(*args);
18897 +               memcpy(p, h_child_name, len);
18898 +               p[len] = 0;
18899 +       }
18900 +
18901 +       /* NFS fires the event for silly-renamed one from kworker */
18902 +       f = 0;
18903 +       if (!dir->i_nlink
18904 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18905 +               f = AuWkq_NEST;
18906 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18907 +       if (unlikely(err)) {
18908 +               pr_err("wkq %d\n", err);
18909 +               iput(args->h_child_inode);
18910 +               iput(args->h_dir);
18911 +               iput(args->dir);
18912 +               au_kfree_rcu(args);
18913 +       }
18914 +
18915 +out:
18916 +       return err;
18917 +}
18918 +
18919 +/* ---------------------------------------------------------------------- */
18920 +
18921 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18922 +{
18923 +       int err;
18924 +
18925 +       AuDebugOn(!(udba & AuOptMask_UDBA));
18926 +
18927 +       err = 0;
18928 +       if (au_hnotify_op.reset_br)
18929 +               err = au_hnotify_op.reset_br(udba, br, perm);
18930 +
18931 +       return err;
18932 +}
18933 +
18934 +int au_hnotify_init_br(struct au_branch *br, int perm)
18935 +{
18936 +       int err;
18937 +
18938 +       err = 0;
18939 +       if (au_hnotify_op.init_br)
18940 +               err = au_hnotify_op.init_br(br, perm);
18941 +
18942 +       return err;
18943 +}
18944 +
18945 +void au_hnotify_fin_br(struct au_branch *br)
18946 +{
18947 +       if (au_hnotify_op.fin_br)
18948 +               au_hnotify_op.fin_br(br);
18949 +}
18950 +
18951 +static void au_hn_destroy_cache(void)
18952 +{
18953 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18954 +       au_cache[AuCache_HNOTIFY] = NULL;
18955 +}
18956 +
18957 +int __init au_hnotify_init(void)
18958 +{
18959 +       int err;
18960 +
18961 +       err = -ENOMEM;
18962 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18963 +       if (au_cache[AuCache_HNOTIFY]) {
18964 +               err = 0;
18965 +               if (au_hnotify_op.init)
18966 +                       err = au_hnotify_op.init();
18967 +               if (unlikely(err))
18968 +                       au_hn_destroy_cache();
18969 +       }
18970 +       AuTraceErr(err);
18971 +       return err;
18972 +}
18973 +
18974 +void au_hnotify_fin(void)
18975 +{
18976 +       if (au_hnotify_op.fin)
18977 +               au_hnotify_op.fin();
18978 +
18979 +       /* cf. au_cache_fin() */
18980 +       if (au_cache[AuCache_HNOTIFY])
18981 +               au_hn_destroy_cache();
18982 +}
18983 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
18984 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
18985 +++ linux/fs/aufs/iinfo.c       2022-08-02 12:12:45.165641939 +0200
18986 @@ -0,0 +1,286 @@
18987 +// SPDX-License-Identifier: GPL-2.0
18988 +/*
18989 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18990 + *
18991 + * This program, aufs is free software; you can redistribute it and/or modify
18992 + * it under the terms of the GNU General Public License as published by
18993 + * the Free Software Foundation; either version 2 of the License, or
18994 + * (at your option) any later version.
18995 + *
18996 + * This program is distributed in the hope that it will be useful,
18997 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18998 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18999 + * GNU General Public License for more details.
19000 + *
19001 + * You should have received a copy of the GNU General Public License
19002 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19003 + */
19004 +
19005 +/*
19006 + * inode private data
19007 + */
19008 +
19009 +#include "aufs.h"
19010 +
19011 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
19012 +{
19013 +       struct inode *h_inode;
19014 +       struct au_hinode *hinode;
19015 +
19016 +       IiMustAnyLock(inode);
19017 +
19018 +       hinode = au_hinode(au_ii(inode), bindex);
19019 +       h_inode = hinode->hi_inode;
19020 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19021 +       return h_inode;
19022 +}
19023 +
19024 +/* todo: hard/soft set? */
19025 +void au_hiput(struct au_hinode *hinode)
19026 +{
19027 +       au_hn_free(hinode);
19028 +       dput(hinode->hi_whdentry);
19029 +       iput(hinode->hi_inode);
19030 +}
19031 +
19032 +unsigned int au_hi_flags(struct inode *inode, int isdir)
19033 +{
19034 +       unsigned int flags;
19035 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
19036 +
19037 +       flags = 0;
19038 +       if (au_opt_test(mnt_flags, XINO))
19039 +               au_fset_hi(flags, XINO);
19040 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19041 +               au_fset_hi(flags, HNOTIFY);
19042 +       return flags;
19043 +}
19044 +
19045 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19046 +                  struct inode *h_inode, unsigned int flags)
19047 +{
19048 +       struct au_hinode *hinode;
19049 +       struct inode *hi;
19050 +       struct au_iinfo *iinfo = au_ii(inode);
19051 +
19052 +       IiMustWriteLock(inode);
19053 +
19054 +       hinode = au_hinode(iinfo, bindex);
19055 +       hi = hinode->hi_inode;
19056 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19057 +
19058 +       if (hi)
19059 +               au_hiput(hinode);
19060 +       hinode->hi_inode = h_inode;
19061 +       if (h_inode) {
19062 +               int err;
19063 +               struct super_block *sb = inode->i_sb;
19064 +               struct au_branch *br;
19065 +
19066 +               AuDebugOn(inode->i_mode
19067 +                         && (h_inode->i_mode & S_IFMT)
19068 +                         != (inode->i_mode & S_IFMT));
19069 +               if (bindex == iinfo->ii_btop)
19070 +                       au_cpup_igen(inode, h_inode);
19071 +               br = au_sbr(sb, bindex);
19072 +               hinode->hi_id = br->br_id;
19073 +               if (au_ftest_hi(flags, XINO)) {
19074 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
19075 +                                           inode->i_ino);
19076 +                       if (unlikely(err))
19077 +                               AuIOErr1("failed au_xino_write() %d\n", err);
19078 +               }
19079 +
19080 +               if (au_ftest_hi(flags, HNOTIFY)
19081 +                   && au_br_hnotifyable(br->br_perm)) {
19082 +                       err = au_hn_alloc(hinode, inode);
19083 +                       if (unlikely(err))
19084 +                               AuIOErr1("au_hn_alloc() %d\n", err);
19085 +               }
19086 +       }
19087 +}
19088 +
19089 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19090 +                 struct dentry *h_wh)
19091 +{
19092 +       struct au_hinode *hinode;
19093 +
19094 +       IiMustWriteLock(inode);
19095 +
19096 +       hinode = au_hinode(au_ii(inode), bindex);
19097 +       AuDebugOn(hinode->hi_whdentry);
19098 +       hinode->hi_whdentry = h_wh;
19099 +}
19100 +
19101 +void au_update_iigen(struct inode *inode, int half)
19102 +{
19103 +       struct au_iinfo *iinfo;
19104 +       struct au_iigen *iigen;
19105 +       unsigned int sigen;
19106 +
19107 +       sigen = au_sigen(inode->i_sb);
19108 +       iinfo = au_ii(inode);
19109 +       iigen = &iinfo->ii_generation;
19110 +       spin_lock(&iigen->ig_spin);
19111 +       iigen->ig_generation = sigen;
19112 +       if (half)
19113 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19114 +       else
19115 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
19116 +       spin_unlock(&iigen->ig_spin);
19117 +}
19118 +
19119 +/* it may be called at remount time, too */
19120 +void au_update_ibrange(struct inode *inode, int do_put_zero)
19121 +{
19122 +       struct au_iinfo *iinfo;
19123 +       aufs_bindex_t bindex, bbot;
19124 +
19125 +       AuDebugOn(au_is_bad_inode(inode));
19126 +       IiMustWriteLock(inode);
19127 +
19128 +       iinfo = au_ii(inode);
19129 +       if (do_put_zero && iinfo->ii_btop >= 0) {
19130 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19131 +                    bindex++) {
19132 +                       struct inode *h_i;
19133 +
19134 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
19135 +                       if (h_i
19136 +                           && !h_i->i_nlink
19137 +                           && !(h_i->i_state & I_LINKABLE))
19138 +                               au_set_h_iptr(inode, bindex, NULL, 0);
19139 +               }
19140 +       }
19141 +
19142 +       iinfo->ii_btop = -1;
19143 +       iinfo->ii_bbot = -1;
19144 +       bbot = au_sbbot(inode->i_sb);
19145 +       for (bindex = 0; bindex <= bbot; bindex++)
19146 +               if (au_hinode(iinfo, bindex)->hi_inode) {
19147 +                       iinfo->ii_btop = bindex;
19148 +                       break;
19149 +               }
19150 +       if (iinfo->ii_btop >= 0)
19151 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19152 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
19153 +                               iinfo->ii_bbot = bindex;
19154 +                               break;
19155 +                       }
19156 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
19157 +}
19158 +
19159 +/* ---------------------------------------------------------------------- */
19160 +
19161 +void au_icntnr_init_once(void *_c)
19162 +{
19163 +       struct au_icntnr *c = _c;
19164 +       struct au_iinfo *iinfo = &c->iinfo;
19165 +
19166 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
19167 +       au_rw_init(&iinfo->ii_rwsem);
19168 +       inode_init_once(&c->vfs_inode);
19169 +}
19170 +
19171 +void au_hinode_init(struct au_hinode *hinode)
19172 +{
19173 +       hinode->hi_inode = NULL;
19174 +       hinode->hi_id = -1;
19175 +       au_hn_init(hinode);
19176 +       hinode->hi_whdentry = NULL;
19177 +}
19178 +
19179 +int au_iinfo_init(struct inode *inode)
19180 +{
19181 +       struct au_iinfo *iinfo;
19182 +       struct super_block *sb;
19183 +       struct au_hinode *hi;
19184 +       int nbr, i;
19185 +
19186 +       sb = inode->i_sb;
19187 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19188 +       nbr = au_sbbot(sb) + 1;
19189 +       if (unlikely(nbr <= 0))
19190 +               nbr = 1;
19191 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19192 +       if (hi) {
19193 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
19194 +
19195 +               iinfo->ii_hinode = hi;
19196 +               for (i = 0; i < nbr; i++, hi++)
19197 +                       au_hinode_init(hi);
19198 +
19199 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
19200 +               iinfo->ii_btop = -1;
19201 +               iinfo->ii_bbot = -1;
19202 +               iinfo->ii_vdir = NULL;
19203 +               return 0;
19204 +       }
19205 +       return -ENOMEM;
19206 +}
19207 +
19208 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
19209 +{
19210 +       int err, i;
19211 +       struct au_hinode *hip;
19212 +
19213 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
19214 +
19215 +       err = -ENOMEM;
19216 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19217 +                         may_shrink);
19218 +       if (hip) {
19219 +               iinfo->ii_hinode = hip;
19220 +               i = iinfo->ii_bbot + 1;
19221 +               hip += i;
19222 +               for (; i < nbr; i++, hip++)
19223 +                       au_hinode_init(hip);
19224 +               err = 0;
19225 +       }
19226 +
19227 +       return err;
19228 +}
19229 +
19230 +void au_iinfo_fin(struct inode *inode)
19231 +{
19232 +       struct au_iinfo *iinfo;
19233 +       struct au_hinode *hi;
19234 +       struct super_block *sb;
19235 +       aufs_bindex_t bindex, bbot;
19236 +       const unsigned char unlinked = !inode->i_nlink;
19237 +
19238 +       AuDebugOn(au_is_bad_inode(inode));
19239 +
19240 +       sb = inode->i_sb;
19241 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
19242 +       if (si_pid_test(sb))
19243 +               au_xino_delete_inode(inode, unlinked);
19244 +       else {
19245 +               /*
19246 +                * it is safe to hide the dependency between sbinfo and
19247 +                * sb->s_umount.
19248 +                */
19249 +               lockdep_off();
19250 +               si_noflush_read_lock(sb);
19251 +               au_xino_delete_inode(inode, unlinked);
19252 +               si_read_unlock(sb);
19253 +               lockdep_on();
19254 +       }
19255 +
19256 +       iinfo = au_ii(inode);
19257 +       if (iinfo->ii_vdir)
19258 +               au_vdir_free(iinfo->ii_vdir);
19259 +
19260 +       bindex = iinfo->ii_btop;
19261 +       if (bindex >= 0) {
19262 +               hi = au_hinode(iinfo, bindex);
19263 +               bbot = iinfo->ii_bbot;
19264 +               while (bindex++ <= bbot) {
19265 +                       if (hi->hi_inode)
19266 +                               au_hiput(hi);
19267 +                       hi++;
19268 +               }
19269 +       }
19270 +       au_kfree_rcu(iinfo->ii_hinode);
19271 +       AuRwDestroy(&iinfo->ii_rwsem);
19272 +}
19273 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
19274 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
19275 +++ linux/fs/aufs/inode.c       2022-08-02 12:12:45.165641939 +0200
19276 @@ -0,0 +1,531 @@
19277 +// SPDX-License-Identifier: GPL-2.0
19278 +/*
19279 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19280 + *
19281 + * This program, aufs is free software; you can redistribute it and/or modify
19282 + * it under the terms of the GNU General Public License as published by
19283 + * the Free Software Foundation; either version 2 of the License, or
19284 + * (at your option) any later version.
19285 + *
19286 + * This program is distributed in the hope that it will be useful,
19287 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19288 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19289 + * GNU General Public License for more details.
19290 + *
19291 + * You should have received a copy of the GNU General Public License
19292 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19293 + */
19294 +
19295 +/*
19296 + * inode functions
19297 + */
19298 +
19299 +#include <linux/iversion.h>
19300 +#include "aufs.h"
19301 +
19302 +struct inode *au_igrab(struct inode *inode)
19303 +{
19304 +       if (inode) {
19305 +               AuDebugOn(!atomic_read(&inode->i_count));
19306 +               ihold(inode);
19307 +       }
19308 +       return inode;
19309 +}
19310 +
19311 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19312 +{
19313 +       au_cpup_attr_all(inode, /*force*/0);
19314 +       au_update_iigen(inode, /*half*/1);
19315 +       if (do_version)
19316 +               inode_inc_iversion(inode);
19317 +}
19318 +
19319 +static int au_ii_refresh(struct inode *inode, int *update)
19320 +{
19321 +       int err, e, nbr;
19322 +       umode_t type;
19323 +       aufs_bindex_t bindex, new_bindex;
19324 +       struct super_block *sb;
19325 +       struct au_iinfo *iinfo;
19326 +       struct au_hinode *p, *q, tmp;
19327 +
19328 +       AuDebugOn(au_is_bad_inode(inode));
19329 +       IiMustWriteLock(inode);
19330 +
19331 +       *update = 0;
19332 +       sb = inode->i_sb;
19333 +       nbr = au_sbbot(sb) + 1;
19334 +       type = inode->i_mode & S_IFMT;
19335 +       iinfo = au_ii(inode);
19336 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
19337 +       if (unlikely(err))
19338 +               goto out;
19339 +
19340 +       AuDebugOn(iinfo->ii_btop < 0);
19341 +       p = au_hinode(iinfo, iinfo->ii_btop);
19342 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19343 +            bindex++, p++) {
19344 +               if (!p->hi_inode)
19345 +                       continue;
19346 +
19347 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
19348 +               new_bindex = au_br_index(sb, p->hi_id);
19349 +               if (new_bindex == bindex)
19350 +                       continue;
19351 +
19352 +               if (new_bindex < 0) {
19353 +                       *update = 1;
19354 +                       au_hiput(p);
19355 +                       p->hi_inode = NULL;
19356 +                       continue;
19357 +               }
19358 +
19359 +               if (new_bindex < iinfo->ii_btop)
19360 +                       iinfo->ii_btop = new_bindex;
19361 +               if (iinfo->ii_bbot < new_bindex)
19362 +                       iinfo->ii_bbot = new_bindex;
19363 +               /* swap two lower inode, and loop again */
19364 +               q = au_hinode(iinfo, new_bindex);
19365 +               tmp = *q;
19366 +               *q = *p;
19367 +               *p = tmp;
19368 +               if (tmp.hi_inode) {
19369 +                       bindex--;
19370 +                       p--;
19371 +               }
19372 +       }
19373 +       au_update_ibrange(inode, /*do_put_zero*/0);
19374 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
19375 +       e = au_dy_irefresh(inode);
19376 +       if (unlikely(e && !err))
19377 +               err = e;
19378 +
19379 +out:
19380 +       AuTraceErr(err);
19381 +       return err;
19382 +}
19383 +
19384 +void au_refresh_iop(struct inode *inode, int force_getattr)
19385 +{
19386 +       int type;
19387 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19388 +       const struct inode_operations *iop
19389 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
19390 +
19391 +       if (inode->i_op == iop)
19392 +               return;
19393 +
19394 +       switch (inode->i_mode & S_IFMT) {
19395 +       case S_IFDIR:
19396 +               type = AuIop_DIR;
19397 +               break;
19398 +       case S_IFLNK:
19399 +               type = AuIop_SYMLINK;
19400 +               break;
19401 +       default:
19402 +               type = AuIop_OTHER;
19403 +               break;
19404 +       }
19405 +
19406 +       inode->i_op = iop + type;
19407 +       /* unnecessary smp_wmb() */
19408 +}
19409 +
19410 +int au_refresh_hinode_self(struct inode *inode)
19411 +{
19412 +       int err, update;
19413 +
19414 +       err = au_ii_refresh(inode, &update);
19415 +       if (!err)
19416 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19417 +
19418 +       AuTraceErr(err);
19419 +       return err;
19420 +}
19421 +
19422 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19423 +{
19424 +       int err, e, update;
19425 +       unsigned int flags;
19426 +       umode_t mode;
19427 +       aufs_bindex_t bindex, bbot;
19428 +       unsigned char isdir;
19429 +       struct au_hinode *p;
19430 +       struct au_iinfo *iinfo;
19431 +
19432 +       err = au_ii_refresh(inode, &update);
19433 +       if (unlikely(err))
19434 +               goto out;
19435 +
19436 +       update = 0;
19437 +       iinfo = au_ii(inode);
19438 +       p = au_hinode(iinfo, iinfo->ii_btop);
19439 +       mode = (inode->i_mode & S_IFMT);
19440 +       isdir = S_ISDIR(mode);
19441 +       flags = au_hi_flags(inode, isdir);
19442 +       bbot = au_dbbot(dentry);
19443 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
19444 +               struct inode *h_i, *h_inode;
19445 +               struct dentry *h_d;
19446 +
19447 +               h_d = au_h_dptr(dentry, bindex);
19448 +               if (!h_d || d_is_negative(h_d))
19449 +                       continue;
19450 +
19451 +               h_inode = d_inode(h_d);
19452 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
19453 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
19454 +                       h_i = au_h_iptr(inode, bindex);
19455 +                       if (h_i) {
19456 +                               if (h_i == h_inode)
19457 +                                       continue;
19458 +                               err = -EIO;
19459 +                               break;
19460 +                       }
19461 +               }
19462 +               if (bindex < iinfo->ii_btop)
19463 +                       iinfo->ii_btop = bindex;
19464 +               if (iinfo->ii_bbot < bindex)
19465 +                       iinfo->ii_bbot = bindex;
19466 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
19467 +               update = 1;
19468 +       }
19469 +       au_update_ibrange(inode, /*do_put_zero*/0);
19470 +       e = au_dy_irefresh(inode);
19471 +       if (unlikely(e && !err))
19472 +               err = e;
19473 +       if (!err)
19474 +               au_refresh_hinode_attr(inode, update && isdir);
19475 +
19476 +out:
19477 +       AuTraceErr(err);
19478 +       return err;
19479 +}
19480 +
19481 +static int set_inode(struct inode *inode, struct dentry *dentry)
19482 +{
19483 +       int err;
19484 +       unsigned int flags;
19485 +       umode_t mode;
19486 +       aufs_bindex_t bindex, btop, btail;
19487 +       unsigned char isdir;
19488 +       struct dentry *h_dentry;
19489 +       struct inode *h_inode;
19490 +       struct au_iinfo *iinfo;
19491 +       const struct inode_operations *iop;
19492 +
19493 +       IiMustWriteLock(inode);
19494 +
19495 +       err = 0;
19496 +       isdir = 0;
19497 +       iop = au_sbi(inode->i_sb)->si_iop_array;
19498 +       btop = au_dbtop(dentry);
19499 +       h_dentry = au_h_dptr(dentry, btop);
19500 +       h_inode = d_inode(h_dentry);
19501 +       mode = h_inode->i_mode;
19502 +       switch (mode & S_IFMT) {
19503 +       case S_IFREG:
19504 +               btail = au_dbtail(dentry);
19505 +               inode->i_op = iop + AuIop_OTHER;
19506 +               inode->i_fop = &aufs_file_fop;
19507 +               err = au_dy_iaop(inode, btop, h_inode);
19508 +               if (unlikely(err))
19509 +                       goto out;
19510 +               break;
19511 +       case S_IFDIR:
19512 +               isdir = 1;
19513 +               btail = au_dbtaildir(dentry);
19514 +               inode->i_op = iop + AuIop_DIR;
19515 +               inode->i_fop = &aufs_dir_fop;
19516 +               break;
19517 +       case S_IFLNK:
19518 +               btail = au_dbtail(dentry);
19519 +               inode->i_op = iop + AuIop_SYMLINK;
19520 +               break;
19521 +       case S_IFBLK:
19522 +       case S_IFCHR:
19523 +       case S_IFIFO:
19524 +       case S_IFSOCK:
19525 +               btail = au_dbtail(dentry);
19526 +               inode->i_op = iop + AuIop_OTHER;
19527 +               init_special_inode(inode, mode, h_inode->i_rdev);
19528 +               break;
19529 +       default:
19530 +               AuIOErr("Unknown file type 0%o\n", mode);
19531 +               err = -EIO;
19532 +               goto out;
19533 +       }
19534 +
19535 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
19536 +       flags = au_hi_flags(inode, isdir);
19537 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19538 +           && au_ftest_hi(flags, HNOTIFY)
19539 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
19540 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19541 +               au_fclr_hi(flags, HNOTIFY);
19542 +       iinfo = au_ii(inode);
19543 +       iinfo->ii_btop = btop;
19544 +       iinfo->ii_bbot = btail;
19545 +       for (bindex = btop; bindex <= btail; bindex++) {
19546 +               h_dentry = au_h_dptr(dentry, bindex);
19547 +               if (h_dentry)
19548 +                       au_set_h_iptr(inode, bindex,
19549 +                                     au_igrab(d_inode(h_dentry)), flags);
19550 +       }
19551 +       au_cpup_attr_all(inode, /*force*/1);
19552 +       /*
19553 +        * to force calling aufs_get_acl() every time,
19554 +        * do not call cache_no_acl() for aufs inode.
19555 +        */
19556 +
19557 +out:
19558 +       return err;
19559 +}
19560 +
19561 +/*
19562 + * successful returns with iinfo write_locked
19563 + * minus: errno
19564 + * zero: success, matched
19565 + * plus: no error, but unmatched
19566 + */
19567 +static int reval_inode(struct inode *inode, struct dentry *dentry)
19568 +{
19569 +       int err;
19570 +       unsigned int gen, igflags;
19571 +       aufs_bindex_t bindex, bbot;
19572 +       struct inode *h_inode, *h_dinode;
19573 +       struct dentry *h_dentry;
19574 +
19575 +       /*
19576 +        * before this function, if aufs got any iinfo lock, it must be only
19577 +        * one, the parent dir.
19578 +        * it can happen by UDBA and the obsoleted inode number.
19579 +        */
19580 +       err = -EIO;
19581 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
19582 +               goto out;
19583 +
19584 +       err = 1;
19585 +       ii_write_lock_new_child(inode);
19586 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
19587 +       h_dinode = d_inode(h_dentry);
19588 +       bbot = au_ibbot(inode);
19589 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
19590 +               h_inode = au_h_iptr(inode, bindex);
19591 +               if (!h_inode || h_inode != h_dinode)
19592 +                       continue;
19593 +
19594 +               err = 0;
19595 +               gen = au_iigen(inode, &igflags);
19596 +               if (gen == au_digen(dentry)
19597 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
19598 +                       break;
19599 +
19600 +               /* fully refresh inode using dentry */
19601 +               err = au_refresh_hinode(inode, dentry);
19602 +               if (!err)
19603 +                       au_update_iigen(inode, /*half*/0);
19604 +               break;
19605 +       }
19606 +
19607 +       if (unlikely(err))
19608 +               ii_write_unlock(inode);
19609 +out:
19610 +       return err;
19611 +}
19612 +
19613 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19614 +          unsigned int d_type, ino_t *ino)
19615 +{
19616 +       int err, idx;
19617 +       const int isnondir = d_type != DT_DIR;
19618 +
19619 +       /* prevent hardlinked inode number from race condition */
19620 +       if (isnondir) {
19621 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19622 +               if (unlikely(err))
19623 +                       goto out;
19624 +       }
19625 +
19626 +       err = au_xino_read(sb, bindex, h_ino, ino);
19627 +       if (unlikely(err))
19628 +               goto out_xinondir;
19629 +
19630 +       if (!*ino) {
19631 +               err = -EIO;
19632 +               *ino = au_xino_new_ino(sb);
19633 +               if (unlikely(!*ino))
19634 +                       goto out_xinondir;
19635 +               err = au_xino_write(sb, bindex, h_ino, *ino);
19636 +               if (unlikely(err))
19637 +                       goto out_xinondir;
19638 +       }
19639 +
19640 +out_xinondir:
19641 +       if (isnondir && idx >= 0)
19642 +               au_xinondir_leave(sb, bindex, h_ino, idx);
19643 +out:
19644 +       return err;
19645 +}
19646 +
19647 +/* successful returns with iinfo write_locked */
19648 +/* todo: return with unlocked? */
19649 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
19650 +{
19651 +       struct inode *inode, *h_inode;
19652 +       struct dentry *h_dentry;
19653 +       struct super_block *sb;
19654 +       ino_t h_ino, ino;
19655 +       int err, idx, hlinked;
19656 +       aufs_bindex_t btop;
19657 +
19658 +       sb = dentry->d_sb;
19659 +       btop = au_dbtop(dentry);
19660 +       h_dentry = au_h_dptr(dentry, btop);
19661 +       h_inode = d_inode(h_dentry);
19662 +       h_ino = h_inode->i_ino;
19663 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
19664 +
19665 +new_ino:
19666 +       /*
19667 +        * stop 'race'-ing between hardlinks under different
19668 +        * parents.
19669 +        */
19670 +       if (hlinked) {
19671 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
19672 +               inode = ERR_PTR(err);
19673 +               if (unlikely(err))
19674 +                       goto out;
19675 +       }
19676 +
19677 +       err = au_xino_read(sb, btop, h_ino, &ino);
19678 +       inode = ERR_PTR(err);
19679 +       if (unlikely(err))
19680 +               goto out_xinondir;
19681 +
19682 +       if (!ino) {
19683 +               ino = au_xino_new_ino(sb);
19684 +               if (unlikely(!ino)) {
19685 +                       inode = ERR_PTR(-EIO);
19686 +                       goto out_xinondir;
19687 +               }
19688 +       }
19689 +
19690 +       AuDbg("i%lu\n", (unsigned long)ino);
19691 +       inode = au_iget_locked(sb, ino);
19692 +       err = PTR_ERR(inode);
19693 +       if (IS_ERR(inode))
19694 +               goto out_xinondir;
19695 +
19696 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19697 +       if (inode->i_state & I_NEW) {
19698 +               ii_write_lock_new_child(inode);
19699 +               err = set_inode(inode, dentry);
19700 +               if (!err) {
19701 +                       unlock_new_inode(inode);
19702 +                       goto out_xinondir; /* success */
19703 +               }
19704 +
19705 +               /*
19706 +                * iget_failed() calls iput(), but we need to call
19707 +                * ii_write_unlock() after iget_failed(). so dirty hack for
19708 +                * i_count.
19709 +                */
19710 +               atomic_inc(&inode->i_count);
19711 +               iget_failed(inode);
19712 +               ii_write_unlock(inode);
19713 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
19714 +               /* ignore this error */
19715 +               goto out_iput;
19716 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
19717 +               /*
19718 +                * horrible race condition between lookup, readdir and copyup
19719 +                * (or something).
19720 +                */
19721 +               if (hlinked && idx >= 0)
19722 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19723 +               err = reval_inode(inode, dentry);
19724 +               if (unlikely(err < 0)) {
19725 +                       hlinked = 0;
19726 +                       goto out_iput;
19727 +               }
19728 +               if (!err)
19729 +                       goto out; /* success */
19730 +               else if (hlinked && idx >= 0) {
19731 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
19732 +                       if (unlikely(err)) {
19733 +                               iput(inode);
19734 +                               inode = ERR_PTR(err);
19735 +                               goto out;
19736 +                       }
19737 +               }
19738 +       }
19739 +
19740 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
19741 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
19742 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
19743 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
19744 +                       (unsigned long)h_ino, (unsigned long)ino);
19745 +       ino = 0;
19746 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
19747 +       if (!err) {
19748 +               iput(inode);
19749 +               if (hlinked && idx >= 0)
19750 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19751 +               goto new_ino;
19752 +       }
19753 +
19754 +out_iput:
19755 +       iput(inode);
19756 +       inode = ERR_PTR(err);
19757 +out_xinondir:
19758 +       if (hlinked && idx >= 0)
19759 +               au_xinondir_leave(sb, btop, h_ino, idx);
19760 +out:
19761 +       return inode;
19762 +}
19763 +
19764 +/* ---------------------------------------------------------------------- */
19765 +
19766 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19767 +              struct inode *inode)
19768 +{
19769 +       int err;
19770 +       struct inode *hi;
19771 +
19772 +       err = au_br_rdonly(au_sbr(sb, bindex));
19773 +
19774 +       /* pseudo-link after flushed may happen out of bounds */
19775 +       if (!err
19776 +           && inode
19777 +           && au_ibtop(inode) <= bindex
19778 +           && bindex <= au_ibbot(inode)) {
19779 +               /*
19780 +                * permission check is unnecessary since vfsub routine
19781 +                * will be called later
19782 +                */
19783 +               hi = au_h_iptr(inode, bindex);
19784 +               if (hi)
19785 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
19786 +       }
19787 +
19788 +       return err;
19789 +}
19790 +
19791 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19792 +                  int mask)
19793 +{
19794 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
19795 +               return 0;
19796 +       return inode_permission(h_userns, h_inode, mask);
19797 +}
19798 +
19799 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19800 +                      int mask)
19801 +{
19802 +       if (au_test_nfs(h_inode->i_sb)
19803 +           && (mask & MAY_WRITE)
19804 +           && S_ISDIR(h_inode->i_mode))
19805 +               mask |= MAY_READ; /* force permission check */
19806 +       return au_test_h_perm(h_userns, h_inode, mask);
19807 +}
19808 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
19809 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
19810 +++ linux/fs/aufs/inode.h       2022-08-02 12:12:45.165641939 +0200
19811 @@ -0,0 +1,705 @@
19812 +/* SPDX-License-Identifier: GPL-2.0 */
19813 +/*
19814 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19815 + *
19816 + * This program, aufs is free software; you can redistribute it and/or modify
19817 + * it under the terms of the GNU General Public License as published by
19818 + * the Free Software Foundation; either version 2 of the License, or
19819 + * (at your option) any later version.
19820 + *
19821 + * This program is distributed in the hope that it will be useful,
19822 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19823 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19824 + * GNU General Public License for more details.
19825 + *
19826 + * You should have received a copy of the GNU General Public License
19827 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19828 + */
19829 +
19830 +/*
19831 + * inode operations
19832 + */
19833 +
19834 +#ifndef __AUFS_INODE_H__
19835 +#define __AUFS_INODE_H__
19836 +
19837 +#ifdef __KERNEL__
19838 +
19839 +#include <linux/fsnotify.h>
19840 +#include "rwsem.h"
19841 +
19842 +struct vfsmount;
19843 +
19844 +struct au_hnotify {
19845 +#ifdef CONFIG_AUFS_HNOTIFY
19846 +#ifdef CONFIG_AUFS_HFSNOTIFY
19847 +       /* never use fsnotify_add_vfsmount_mark() */
19848 +       struct fsnotify_mark            hn_mark;
19849 +#endif
19850 +       struct inode            *hn_aufs_inode; /* no get/put */
19851 +       struct rcu_head         rcu;
19852 +#endif
19853 +} ____cacheline_aligned_in_smp;
19854 +
19855 +struct au_hinode {
19856 +       struct inode            *hi_inode;
19857 +       aufs_bindex_t           hi_id;
19858 +#ifdef CONFIG_AUFS_HNOTIFY
19859 +       struct au_hnotify       *hi_notify;
19860 +#endif
19861 +
19862 +       /* reference to the copied-up whiteout with get/put */
19863 +       struct dentry           *hi_whdentry;
19864 +};
19865 +
19866 +/* ig_flags */
19867 +#define AuIG_HALF_REFRESHED            1
19868 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
19869 +#define au_ig_fset(flags, name) \
19870 +       do { (flags) |= AuIG_##name; } while (0)
19871 +#define au_ig_fclr(flags, name) \
19872 +       do { (flags) &= ~AuIG_##name; } while (0)
19873 +
19874 +struct au_iigen {
19875 +       spinlock_t      ig_spin;
19876 +       __u32           ig_generation, ig_flags;
19877 +};
19878 +
19879 +struct au_vdir;
19880 +struct au_iinfo {
19881 +       struct au_iigen         ii_generation;
19882 +       struct super_block      *ii_hsb1;       /* no get/put */
19883 +
19884 +       struct au_rwsem         ii_rwsem;
19885 +       aufs_bindex_t           ii_btop, ii_bbot;
19886 +       __u32                   ii_higen;
19887 +       struct au_hinode        *ii_hinode;
19888 +       struct au_vdir          *ii_vdir;
19889 +};
19890 +
19891 +struct au_icntnr {
19892 +       struct au_iinfo         iinfo;
19893 +       struct inode            vfs_inode;
19894 +       struct hlist_bl_node    plink;
19895 +       struct rcu_head         rcu;
19896 +} ____cacheline_aligned_in_smp;
19897 +
19898 +/* au_pin flags */
19899 +#define AuPin_DI_LOCKED                1
19900 +#define AuPin_MNT_WRITE                (1 << 1)
19901 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
19902 +#define au_fset_pin(flags, name) \
19903 +       do { (flags) |= AuPin_##name; } while (0)
19904 +#define au_fclr_pin(flags, name) \
19905 +       do { (flags) &= ~AuPin_##name; } while (0)
19906 +
19907 +struct au_pin {
19908 +       /* input */
19909 +       struct dentry *dentry;
19910 +       unsigned int udba;
19911 +       unsigned char lsc_di, lsc_hi, flags;
19912 +       aufs_bindex_t bindex;
19913 +
19914 +       /* output */
19915 +       struct dentry *parent;
19916 +       struct au_hinode *hdir;
19917 +       struct vfsmount *h_mnt;
19918 +
19919 +       /* temporary unlock/relock for copyup */
19920 +       struct dentry *h_dentry, *h_parent;
19921 +       struct au_branch *br;
19922 +       struct task_struct *task;
19923 +};
19924 +
19925 +void au_pin_hdir_unlock(struct au_pin *p);
19926 +int au_pin_hdir_lock(struct au_pin *p);
19927 +int au_pin_hdir_relock(struct au_pin *p);
19928 +void au_pin_hdir_acquire_nest(struct au_pin *p);
19929 +void au_pin_hdir_release(struct au_pin *p);
19930 +
19931 +/* ---------------------------------------------------------------------- */
19932 +
19933 +static inline struct au_iinfo *au_ii(struct inode *inode)
19934 +{
19935 +       BUG_ON(is_bad_inode(inode));
19936 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19937 +}
19938 +
19939 +/* ---------------------------------------------------------------------- */
19940 +
19941 +/* inode.c */
19942 +struct inode *au_igrab(struct inode *inode);
19943 +void au_refresh_iop(struct inode *inode, int force_getattr);
19944 +int au_refresh_hinode_self(struct inode *inode);
19945 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19946 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19947 +          unsigned int d_type, ino_t *ino);
19948 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
19949 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19950 +              struct inode *inode);
19951 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19952 +                  int mask);
19953 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19954 +                      int mask);
19955 +
19956 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19957 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
19958 +{
19959 +#ifdef CONFIG_AUFS_SHWH
19960 +       return au_ino(sb, bindex, h_ino, d_type, ino);
19961 +#else
19962 +       return 0;
19963 +#endif
19964 +}
19965 +
19966 +/* i_op.c */
19967 +enum {
19968 +       AuIop_SYMLINK,
19969 +       AuIop_DIR,
19970 +       AuIop_OTHER,
19971 +       AuIop_Last
19972 +};
19973 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
19974 +       aufs_iop_nogetattr[AuIop_Last];
19975 +
19976 +/* au_wr_dir flags */
19977 +#define AuWrDir_ADD_ENTRY      1
19978 +#define AuWrDir_ISDIR          (1 << 1)
19979 +#define AuWrDir_TMPFILE                (1 << 2)
19980 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
19981 +#define au_fset_wrdir(flags, name) \
19982 +       do { (flags) |= AuWrDir_##name; } while (0)
19983 +#define au_fclr_wrdir(flags, name) \
19984 +       do { (flags) &= ~AuWrDir_##name; } while (0)
19985 +
19986 +struct au_wr_dir_args {
19987 +       aufs_bindex_t force_btgt;
19988 +       unsigned char flags;
19989 +};
19990 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19991 +             struct au_wr_dir_args *args);
19992 +
19993 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
19994 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
19995 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19996 +                unsigned int udba, unsigned char flags);
19997 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19998 +          unsigned int udba, unsigned char flags) __must_check;
19999 +int au_do_pin(struct au_pin *pin) __must_check;
20000 +void au_unpin(struct au_pin *pin);
20001 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
20002 +
20003 +#define AuIcpup_DID_CPUP       1
20004 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
20005 +#define au_fset_icpup(flags, name) \
20006 +       do { (flags) |= AuIcpup_##name; } while (0)
20007 +#define au_fclr_icpup(flags, name) \
20008 +       do { (flags) &= ~AuIcpup_##name; } while (0)
20009 +
20010 +struct au_icpup_args {
20011 +       unsigned char flags;
20012 +       unsigned char pin_flags;
20013 +       aufs_bindex_t btgt;
20014 +       unsigned int udba;
20015 +       struct au_pin pin;
20016 +       struct path h_path;
20017 +       struct inode *h_inode;
20018 +};
20019 +
20020 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20021 +                    struct au_icpup_args *a);
20022 +
20023 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20024 +                     struct path *h_path, int locked);
20025 +
20026 +/* i_op_add.c */
20027 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20028 +              struct dentry *h_parent, int isdir);
20029 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
20030 +              struct dentry *dentry, umode_t mode, dev_t dev);
20031 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
20032 +                struct dentry *dentry, const char *symname);
20033 +int aufs_create(struct user_namespace *userns, struct inode *dir,
20034 +               struct dentry *dentry, umode_t mode, bool want_excl);
20035 +struct vfsub_aopen_args;
20036 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20037 +                      struct vfsub_aopen_args *args);
20038 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
20039 +                struct dentry *dentry, umode_t mode);
20040 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20041 +             struct dentry *dentry);
20042 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20043 +              struct dentry *dentry, umode_t mode);
20044 +
20045 +/* i_op_del.c */
20046 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20047 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20048 +              struct dentry *h_parent, int isdir);
20049 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
20050 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
20051 +
20052 +/* i_op_ren.c */
20053 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
20054 +int aufs_rename(struct user_namespace *userns,
20055 +               struct inode *_src_dir, struct dentry *_src_dentry,
20056 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
20057 +               unsigned int _flags);
20058 +
20059 +/* iinfo.c */
20060 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20061 +void au_hiput(struct au_hinode *hinode);
20062 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20063 +                 struct dentry *h_wh);
20064 +unsigned int au_hi_flags(struct inode *inode, int isdir);
20065 +
20066 +/* hinode flags */
20067 +#define AuHi_XINO      1
20068 +#define AuHi_HNOTIFY   (1 << 1)
20069 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
20070 +#define au_fset_hi(flags, name) \
20071 +       do { (flags) |= AuHi_##name; } while (0)
20072 +#define au_fclr_hi(flags, name) \
20073 +       do { (flags) &= ~AuHi_##name; } while (0)
20074 +
20075 +#ifndef CONFIG_AUFS_HNOTIFY
20076 +#undef AuHi_HNOTIFY
20077 +#define AuHi_HNOTIFY   0
20078 +#endif
20079 +
20080 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20081 +                  struct inode *h_inode, unsigned int flags);
20082 +
20083 +void au_update_iigen(struct inode *inode, int half);
20084 +void au_update_ibrange(struct inode *inode, int do_put_zero);
20085 +
20086 +void au_icntnr_init_once(void *_c);
20087 +void au_hinode_init(struct au_hinode *hinode);
20088 +int au_iinfo_init(struct inode *inode);
20089 +void au_iinfo_fin(struct inode *inode);
20090 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
20091 +
20092 +#ifdef CONFIG_PROC_FS
20093 +/* plink.c */
20094 +int au_plink_maint(struct super_block *sb, int flags);
20095 +struct au_sbinfo;
20096 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20097 +int au_plink_maint_enter(struct super_block *sb);
20098 +#ifdef CONFIG_AUFS_DEBUG
20099 +void au_plink_list(struct super_block *sb);
20100 +#else
20101 +AuStubVoid(au_plink_list, struct super_block *sb)
20102 +#endif
20103 +int au_plink_test(struct inode *inode);
20104 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20105 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20106 +                    struct dentry *h_dentry);
20107 +void au_plink_put(struct super_block *sb, int verbose);
20108 +void au_plink_clean(struct super_block *sb, int verbose);
20109 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
20110 +#else
20111 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20112 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20113 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20114 +AuStubVoid(au_plink_list, struct super_block *sb);
20115 +AuStubInt0(au_plink_test, struct inode *inode);
20116 +AuStub(struct dentry *, au_plink_lkup, return NULL,
20117 +       struct inode *inode, aufs_bindex_t bindex);
20118 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20119 +          struct dentry *h_dentry);
20120 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20121 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20122 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20123 +#endif /* CONFIG_PROC_FS */
20124 +
20125 +#ifdef CONFIG_AUFS_XATTR
20126 +/* xattr.c */
20127 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
20128 +                 unsigned int verbose);
20129 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
20130 +void au_xattr_init(struct super_block *sb);
20131 +#else
20132 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
20133 +          int ignore_flags, unsigned int verbose);
20134 +AuStubVoid(au_xattr_init, struct super_block *sb);
20135 +#endif
20136 +
20137 +#ifdef CONFIG_FS_POSIX_ACL
20138 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu);
20139 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
20140 +                struct posix_acl *acl, int type);
20141 +#endif
20142 +
20143 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20144 +enum {
20145 +       AU_XATTR_SET,
20146 +       AU_ACL_SET
20147 +};
20148 +
20149 +struct au_sxattr {
20150 +       int type;
20151 +       union {
20152 +               struct {
20153 +                       const char      *name;
20154 +                       const void      *value;
20155 +                       size_t          size;
20156 +                       int             flags;
20157 +               } set;
20158 +               struct {
20159 +                       struct posix_acl *acl;
20160 +                       int             type;
20161 +               } acl_set;
20162 +       } u;
20163 +};
20164 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20165 +                 struct au_sxattr *arg);
20166 +#endif
20167 +
20168 +/* ---------------------------------------------------------------------- */
20169 +
20170 +/* lock subclass for iinfo */
20171 +enum {
20172 +       AuLsc_II_CHILD,         /* child first */
20173 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
20174 +       AuLsc_II_CHILD3,        /* copyup dirs */
20175 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
20176 +       AuLsc_II_PARENT2,
20177 +       AuLsc_II_PARENT3,       /* copyup dirs */
20178 +       AuLsc_II_NEW_CHILD
20179 +};
20180 +
20181 +/*
20182 + * ii_read_lock_child, ii_write_lock_child,
20183 + * ii_read_lock_child2, ii_write_lock_child2,
20184 + * ii_read_lock_child3, ii_write_lock_child3,
20185 + * ii_read_lock_parent, ii_write_lock_parent,
20186 + * ii_read_lock_parent2, ii_write_lock_parent2,
20187 + * ii_read_lock_parent3, ii_write_lock_parent3,
20188 + * ii_read_lock_new_child, ii_write_lock_new_child,
20189 + */
20190 +#define AuReadLockFunc(name, lsc) \
20191 +static inline void ii_read_lock_##name(struct inode *i) \
20192 +{ \
20193 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20194 +}
20195 +
20196 +#define AuWriteLockFunc(name, lsc) \
20197 +static inline void ii_write_lock_##name(struct inode *i) \
20198 +{ \
20199 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20200 +}
20201 +
20202 +#define AuRWLockFuncs(name, lsc) \
20203 +       AuReadLockFunc(name, lsc) \
20204 +       AuWriteLockFunc(name, lsc)
20205 +
20206 +AuRWLockFuncs(child, CHILD);
20207 +AuRWLockFuncs(child2, CHILD2);
20208 +AuRWLockFuncs(child3, CHILD3);
20209 +AuRWLockFuncs(parent, PARENT);
20210 +AuRWLockFuncs(parent2, PARENT2);
20211 +AuRWLockFuncs(parent3, PARENT3);
20212 +AuRWLockFuncs(new_child, NEW_CHILD);
20213 +
20214 +#undef AuReadLockFunc
20215 +#undef AuWriteLockFunc
20216 +#undef AuRWLockFuncs
20217 +
20218 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20219 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20220 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
20221 +
20222 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20223 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20224 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
20225 +
20226 +/* ---------------------------------------------------------------------- */
20227 +
20228 +static inline void au_icntnr_init(struct au_icntnr *c)
20229 +{
20230 +#ifdef CONFIG_AUFS_DEBUG
20231 +       c->vfs_inode.i_mode = 0;
20232 +#endif
20233 +}
20234 +
20235 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
20236 +{
20237 +       unsigned int gen;
20238 +       struct au_iinfo *iinfo;
20239 +       struct au_iigen *iigen;
20240 +
20241 +       iinfo = au_ii(inode);
20242 +       iigen = &iinfo->ii_generation;
20243 +       spin_lock(&iigen->ig_spin);
20244 +       if (igflags)
20245 +               *igflags = iigen->ig_flags;
20246 +       gen = iigen->ig_generation;
20247 +       spin_unlock(&iigen->ig_spin);
20248 +
20249 +       return gen;
20250 +}
20251 +
20252 +/* tiny test for inode number */
20253 +/* tmpfs generation is too rough */
20254 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20255 +{
20256 +       struct au_iinfo *iinfo;
20257 +
20258 +       iinfo = au_ii(inode);
20259 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
20260 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
20261 +                && iinfo->ii_higen == h_inode->i_generation);
20262 +}
20263 +
20264 +static inline void au_iigen_dec(struct inode *inode)
20265 +{
20266 +       struct au_iinfo *iinfo;
20267 +       struct au_iigen *iigen;
20268 +
20269 +       iinfo = au_ii(inode);
20270 +       iigen = &iinfo->ii_generation;
20271 +       spin_lock(&iigen->ig_spin);
20272 +       iigen->ig_generation--;
20273 +       spin_unlock(&iigen->ig_spin);
20274 +}
20275 +
20276 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20277 +{
20278 +       int err;
20279 +
20280 +       err = 0;
20281 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
20282 +               err = -EIO;
20283 +
20284 +       return err;
20285 +}
20286 +
20287 +/* ---------------------------------------------------------------------- */
20288 +
20289 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20290 +                                         aufs_bindex_t bindex)
20291 +{
20292 +       return iinfo->ii_hinode + bindex;
20293 +}
20294 +
20295 +static inline int au_is_bad_inode(struct inode *inode)
20296 +{
20297 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20298 +}
20299 +
20300 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20301 +                                       aufs_bindex_t bindex)
20302 +{
20303 +       IiMustAnyLock(inode);
20304 +       return au_hinode(au_ii(inode), bindex)->hi_id;
20305 +}
20306 +
20307 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
20308 +{
20309 +       IiMustAnyLock(inode);
20310 +       return au_ii(inode)->ii_btop;
20311 +}
20312 +
20313 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
20314 +{
20315 +       IiMustAnyLock(inode);
20316 +       return au_ii(inode)->ii_bbot;
20317 +}
20318 +
20319 +static inline struct au_vdir *au_ivdir(struct inode *inode)
20320 +{
20321 +       IiMustAnyLock(inode);
20322 +       return au_ii(inode)->ii_vdir;
20323 +}
20324 +
20325 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20326 +{
20327 +       IiMustAnyLock(inode);
20328 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
20329 +}
20330 +
20331 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
20332 +{
20333 +       IiMustWriteLock(inode);
20334 +       au_ii(inode)->ii_btop = bindex;
20335 +}
20336 +
20337 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
20338 +{
20339 +       IiMustWriteLock(inode);
20340 +       au_ii(inode)->ii_bbot = bindex;
20341 +}
20342 +
20343 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20344 +{
20345 +       IiMustWriteLock(inode);
20346 +       au_ii(inode)->ii_vdir = vdir;
20347 +}
20348 +
20349 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
20350 +{
20351 +       IiMustAnyLock(inode);
20352 +       return au_hinode(au_ii(inode), bindex);
20353 +}
20354 +
20355 +/* ---------------------------------------------------------------------- */
20356 +
20357 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20358 +{
20359 +       if (pin)
20360 +               return pin->parent;
20361 +       return NULL;
20362 +}
20363 +
20364 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
20365 +{
20366 +       if (pin && pin->hdir)
20367 +               return pin->hdir->hi_inode;
20368 +       return NULL;
20369 +}
20370 +
20371 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20372 +{
20373 +       if (pin)
20374 +               return pin->hdir;
20375 +       return NULL;
20376 +}
20377 +
20378 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
20379 +{
20380 +       if (pin)
20381 +               pin->dentry = dentry;
20382 +}
20383 +
20384 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20385 +                                          unsigned char lflag)
20386 +{
20387 +       if (pin) {
20388 +               if (lflag)
20389 +                       au_fset_pin(pin->flags, DI_LOCKED);
20390 +               else
20391 +                       au_fclr_pin(pin->flags, DI_LOCKED);
20392 +       }
20393 +}
20394 +
20395 +#if 0 /* reserved */
20396 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20397 +{
20398 +       if (pin) {
20399 +               dput(pin->parent);
20400 +               pin->parent = dget(parent);
20401 +       }
20402 +}
20403 +#endif
20404 +
20405 +/* ---------------------------------------------------------------------- */
20406 +
20407 +struct au_branch;
20408 +#ifdef CONFIG_AUFS_HNOTIFY
20409 +struct au_hnotify_op {
20410 +       void (*ctl)(struct au_hinode *hinode, int do_set);
20411 +       int (*alloc)(struct au_hinode *hinode);
20412 +
20413 +       /*
20414 +        * if it returns true, the caller should free hinode->hi_notify,
20415 +        * otherwise ->free() frees it.
20416 +        */
20417 +       int (*free)(struct au_hinode *hinode,
20418 +                   struct au_hnotify *hn) __must_check;
20419 +
20420 +       void (*fin)(void);
20421 +       int (*init)(void);
20422 +
20423 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20424 +       void (*fin_br)(struct au_branch *br);
20425 +       int (*init_br)(struct au_branch *br, int perm);
20426 +};
20427 +
20428 +/* hnotify.c */
20429 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
20430 +void au_hn_free(struct au_hinode *hinode);
20431 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
20432 +void au_hn_reset(struct inode *inode, unsigned int flags);
20433 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
20434 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
20435 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20436 +int au_hnotify_init_br(struct au_branch *br, int perm);
20437 +void au_hnotify_fin_br(struct au_branch *br);
20438 +int __init au_hnotify_init(void);
20439 +void au_hnotify_fin(void);
20440 +
20441 +/* hfsnotify.c */
20442 +extern const struct au_hnotify_op au_hnotify_op;
20443 +
20444 +static inline
20445 +void au_hn_init(struct au_hinode *hinode)
20446 +{
20447 +       hinode->hi_notify = NULL;
20448 +}
20449 +
20450 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20451 +{
20452 +       return hinode->hi_notify;
20453 +}
20454 +
20455 +#else
20456 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20457 +       struct au_hinode *hinode __maybe_unused,
20458 +       struct inode *inode __maybe_unused)
20459 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
20460 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20461 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20462 +          int do_set __maybe_unused)
20463 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20464 +          unsigned int flags __maybe_unused)
20465 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20466 +          struct au_branch *br __maybe_unused,
20467 +          int perm __maybe_unused)
20468 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20469 +          int perm __maybe_unused)
20470 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
20471 +AuStubInt0(__init au_hnotify_init, void)
20472 +AuStubVoid(au_hnotify_fin, void)
20473 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20474 +#endif /* CONFIG_AUFS_HNOTIFY */
20475 +
20476 +static inline void au_hn_suspend(struct au_hinode *hdir)
20477 +{
20478 +       au_hn_ctl(hdir, /*do_set*/0);
20479 +}
20480 +
20481 +static inline void au_hn_resume(struct au_hinode *hdir)
20482 +{
20483 +       au_hn_ctl(hdir, /*do_set*/1);
20484 +}
20485 +
20486 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
20487 +{
20488 +       inode_lock(hdir->hi_inode);
20489 +       au_hn_suspend(hdir);
20490 +}
20491 +
20492 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
20493 +                                         unsigned int sc __maybe_unused)
20494 +{
20495 +       inode_lock_nested(hdir->hi_inode, sc);
20496 +       au_hn_suspend(hdir);
20497 +}
20498 +
20499 +#if 0 /* unused */
20500 +#include "vfsub.h"
20501 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20502 +                                                 unsigned int sc)
20503 +{
20504 +       inode_lock_shared_nested(hdir->hi_inode, sc);
20505 +       au_hn_suspend(hdir);
20506 +}
20507 +#endif
20508 +
20509 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
20510 +{
20511 +       au_hn_resume(hdir);
20512 +       inode_unlock(hdir->hi_inode);
20513 +}
20514 +
20515 +#endif /* __KERNEL__ */
20516 +#endif /* __AUFS_INODE_H__ */
20517 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
20518 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
20519 +++ linux/fs/aufs/ioctl.c       2022-08-02 12:12:45.165641939 +0200
20520 @@ -0,0 +1,220 @@
20521 +// SPDX-License-Identifier: GPL-2.0
20522 +/*
20523 + * Copyright (C) 2005-2021 Junjiro R. Okajima
20524 + *
20525 + * This program, aufs is free software; you can redistribute it and/or modify
20526 + * it under the terms of the GNU General Public License as published by
20527 + * the Free Software Foundation; either version 2 of the License, or
20528 + * (at your option) any later version.
20529 + *
20530 + * This program is distributed in the hope that it will be useful,
20531 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20532 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20533 + * GNU General Public License for more details.
20534 + *
20535 + * You should have received a copy of the GNU General Public License
20536 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20537 + */
20538 +
20539 +/*
20540 + * ioctl
20541 + * plink-management and readdir in userspace.
20542 + * assist the pathconf(3) wrapper library.
20543 + * move-down
20544 + * File-based Hierarchical Storage Management.
20545 + */
20546 +
20547 +#include <linux/compat.h>
20548 +#include <linux/file.h>
20549 +#include "aufs.h"
20550 +
20551 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
20552 +{
20553 +       int err, fd;
20554 +       aufs_bindex_t wbi, bindex, bbot;
20555 +       struct file *h_file;
20556 +       struct super_block *sb;
20557 +       struct dentry *root;
20558 +       struct au_branch *br;
20559 +       struct aufs_wbr_fd wbrfd = {
20560 +               .oflags = au_dir_roflags,
20561 +               .brid   = -1
20562 +       };
20563 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20564 +               | O_NOATIME | O_CLOEXEC;
20565 +
20566 +       AuDebugOn(wbrfd.oflags & ~valid);
20567 +
20568 +       if (arg) {
20569 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20570 +               if (unlikely(err)) {
20571 +                       err = -EFAULT;
20572 +                       goto out;
20573 +               }
20574 +
20575 +               err = -EINVAL;
20576 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20577 +               wbrfd.oflags |= au_dir_roflags;
20578 +               AuDbg("0%o\n", wbrfd.oflags);
20579 +               if (unlikely(wbrfd.oflags & ~valid))
20580 +                       goto out;
20581 +       }
20582 +
20583 +       fd = get_unused_fd_flags(0);
20584 +       err = fd;
20585 +       if (unlikely(fd < 0))
20586 +               goto out;
20587 +
20588 +       h_file = ERR_PTR(-EINVAL);
20589 +       wbi = 0;
20590 +       br = NULL;
20591 +       sb = path->dentry->d_sb;
20592 +       root = sb->s_root;
20593 +       aufs_read_lock(root, AuLock_IR);
20594 +       bbot = au_sbbot(sb);
20595 +       if (wbrfd.brid >= 0) {
20596 +               wbi = au_br_index(sb, wbrfd.brid);
20597 +               if (unlikely(wbi < 0 || wbi > bbot))
20598 +                       goto out_unlock;
20599 +       }
20600 +
20601 +       h_file = ERR_PTR(-ENOENT);
20602 +       br = au_sbr(sb, wbi);
20603 +       if (!au_br_writable(br->br_perm)) {
20604 +               if (arg)
20605 +                       goto out_unlock;
20606 +
20607 +               bindex = wbi + 1;
20608 +               wbi = -1;
20609 +               for (; bindex <= bbot; bindex++) {
20610 +                       br = au_sbr(sb, bindex);
20611 +                       if (au_br_writable(br->br_perm)) {
20612 +                               wbi = bindex;
20613 +                               br = au_sbr(sb, wbi);
20614 +                               break;
20615 +                       }
20616 +               }
20617 +       }
20618 +       AuDbg("wbi %d\n", wbi);
20619 +       if (wbi >= 0)
20620 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20621 +                                  /*force_wr*/0);
20622 +
20623 +out_unlock:
20624 +       aufs_read_unlock(root, AuLock_IR);
20625 +       err = PTR_ERR(h_file);
20626 +       if (IS_ERR(h_file))
20627 +               goto out_fd;
20628 +
20629 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
20630 +       fd_install(fd, h_file);
20631 +       err = fd;
20632 +       goto out; /* success */
20633 +
20634 +out_fd:
20635 +       put_unused_fd(fd);
20636 +out:
20637 +       AuTraceErr(err);
20638 +       return err;
20639 +}
20640 +
20641 +/* ---------------------------------------------------------------------- */
20642 +
20643 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20644 +{
20645 +       long err;
20646 +       struct dentry *dentry;
20647 +
20648 +       switch (cmd) {
20649 +       case AUFS_CTL_RDU:
20650 +       case AUFS_CTL_RDU_INO:
20651 +               err = au_rdu_ioctl(file, cmd, arg);
20652 +               break;
20653 +
20654 +       case AUFS_CTL_WBR_FD:
20655 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20656 +               break;
20657 +
20658 +       case AUFS_CTL_IBUSY:
20659 +               err = au_ibusy_ioctl(file, arg);
20660 +               break;
20661 +
20662 +       case AUFS_CTL_BRINFO:
20663 +               err = au_brinfo_ioctl(file, arg);
20664 +               break;
20665 +
20666 +       case AUFS_CTL_FHSM_FD:
20667 +               dentry = file->f_path.dentry;
20668 +               if (IS_ROOT(dentry))
20669 +                       err = au_fhsm_fd(dentry->d_sb, arg);
20670 +               else
20671 +                       err = -ENOTTY;
20672 +               break;
20673 +
20674 +       default:
20675 +               /* do not call the lower */
20676 +               AuDbg("0x%x\n", cmd);
20677 +               err = -ENOTTY;
20678 +       }
20679 +
20680 +       AuTraceErr(err);
20681 +       return err;
20682 +}
20683 +
20684 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20685 +{
20686 +       long err;
20687 +
20688 +       switch (cmd) {
20689 +       case AUFS_CTL_MVDOWN:
20690 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
20691 +               break;
20692 +
20693 +       case AUFS_CTL_WBR_FD:
20694 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20695 +               break;
20696 +
20697 +       default:
20698 +               /* do not call the lower */
20699 +               AuDbg("0x%x\n", cmd);
20700 +               err = -ENOTTY;
20701 +       }
20702 +
20703 +       AuTraceErr(err);
20704 +       return err;
20705 +}
20706 +
20707 +#ifdef CONFIG_COMPAT
20708 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20709 +                          unsigned long arg)
20710 +{
20711 +       long err;
20712 +
20713 +       switch (cmd) {
20714 +       case AUFS_CTL_RDU:
20715 +       case AUFS_CTL_RDU_INO:
20716 +               err = au_rdu_compat_ioctl(file, cmd, arg);
20717 +               break;
20718 +
20719 +       case AUFS_CTL_IBUSY:
20720 +               err = au_ibusy_compat_ioctl(file, arg);
20721 +               break;
20722 +
20723 +       case AUFS_CTL_BRINFO:
20724 +               err = au_brinfo_compat_ioctl(file, arg);
20725 +               break;
20726 +
20727 +       default:
20728 +               err = aufs_ioctl_dir(file, cmd, arg);
20729 +       }
20730 +
20731 +       AuTraceErr(err);
20732 +       return err;
20733 +}
20734 +
20735 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20736 +                             unsigned long arg)
20737 +{
20738 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20739 +}
20740 +#endif
20741 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
20742 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
20743 +++ linux/fs/aufs/i_op_add.c    2022-08-02 12:12:45.165641939 +0200
20744 @@ -0,0 +1,941 @@
20745 +// SPDX-License-Identifier: GPL-2.0
20746 +/*
20747 + * Copyright (C) 2005-2021 Junjiro R. Okajima
20748 + *
20749 + * This program, aufs is free software; you can redistribute it and/or modify
20750 + * it under the terms of the GNU General Public License as published by
20751 + * the Free Software Foundation; either version 2 of the License, or
20752 + * (at your option) any later version.
20753 + *
20754 + * This program is distributed in the hope that it will be useful,
20755 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20756 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20757 + * GNU General Public License for more details.
20758 + *
20759 + * You should have received a copy of the GNU General Public License
20760 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20761 + */
20762 +
20763 +/*
20764 + * inode operations (add entry)
20765 + */
20766 +
20767 +#include <linux/iversion.h>
20768 +#include "aufs.h"
20769 +
20770 +/*
20771 + * final procedure of adding a new entry, except link(2).
20772 + * remove whiteout, instantiate, copyup the parent dir's times and size
20773 + * and update version.
20774 + * if it failed, re-create the removed whiteout.
20775 + */
20776 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20777 +                 struct dentry *wh_dentry, struct dentry *dentry)
20778 +{
20779 +       int err, rerr;
20780 +       aufs_bindex_t bwh;
20781 +       struct path h_path;
20782 +       struct super_block *sb;
20783 +       struct inode *inode, *h_dir;
20784 +       struct dentry *wh;
20785 +
20786 +       bwh = -1;
20787 +       sb = dir->i_sb;
20788 +       if (wh_dentry) {
20789 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20790 +               IMustLock(h_dir);
20791 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20792 +               bwh = au_dbwh(dentry);
20793 +               h_path.dentry = wh_dentry;
20794 +               h_path.mnt = au_sbr_mnt(sb, bindex);
20795 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20796 +                                         dentry);
20797 +               if (unlikely(err))
20798 +                       goto out;
20799 +       }
20800 +
20801 +       inode = au_new_inode(dentry, /*must_new*/1);
20802 +       if (!IS_ERR(inode)) {
20803 +               d_instantiate(dentry, inode);
20804 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
20805 +               IMustLock(dir);
20806 +               au_dir_ts(dir, bindex);
20807 +               inode_inc_iversion(dir);
20808 +               au_fhsm_wrote(sb, bindex, /*force*/0);
20809 +               return 0; /* success */
20810 +       }
20811 +
20812 +       err = PTR_ERR(inode);
20813 +       if (!wh_dentry)
20814 +               goto out;
20815 +
20816 +       /* revert */
20817 +       /* dir inode is locked */
20818 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20819 +       rerr = PTR_ERR(wh);
20820 +       if (IS_ERR(wh)) {
20821 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20822 +                       dentry, err, rerr);
20823 +               err = -EIO;
20824 +       } else
20825 +               dput(wh);
20826 +
20827 +out:
20828 +       return err;
20829 +}
20830 +
20831 +static int au_d_may_add(struct dentry *dentry)
20832 +{
20833 +       int err;
20834 +
20835 +       err = 0;
20836 +       if (unlikely(d_unhashed(dentry)))
20837 +               err = -ENOENT;
20838 +       if (unlikely(d_really_is_positive(dentry)))
20839 +               err = -EEXIST;
20840 +       return err;
20841 +}
20842 +
20843 +/*
20844 + * simple tests for the adding inode operations.
20845 + * following the checks in vfs, plus the parent-child relationship.
20846 + */
20847 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20848 +              struct dentry *h_parent, int isdir)
20849 +{
20850 +       int err;
20851 +       umode_t h_mode;
20852 +       struct dentry *h_dentry;
20853 +       struct inode *h_inode;
20854 +
20855 +       err = -ENAMETOOLONG;
20856 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20857 +               goto out;
20858 +
20859 +       h_dentry = au_h_dptr(dentry, bindex);
20860 +       if (d_really_is_negative(dentry)) {
20861 +               err = -EEXIST;
20862 +               if (unlikely(d_is_positive(h_dentry)))
20863 +                       goto out;
20864 +       } else {
20865 +               /* rename(2) case */
20866 +               err = -EIO;
20867 +               if (unlikely(d_is_negative(h_dentry)))
20868 +                       goto out;
20869 +               h_inode = d_inode(h_dentry);
20870 +               if (unlikely(!h_inode->i_nlink))
20871 +                       goto out;
20872 +
20873 +               h_mode = h_inode->i_mode;
20874 +               if (!isdir) {
20875 +                       err = -EISDIR;
20876 +                       if (unlikely(S_ISDIR(h_mode)))
20877 +                               goto out;
20878 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20879 +                       err = -ENOTDIR;
20880 +                       goto out;
20881 +               }
20882 +       }
20883 +
20884 +       err = 0;
20885 +       /* expected parent dir is locked */
20886 +       if (unlikely(h_parent != h_dentry->d_parent))
20887 +               err = -EIO;
20888 +
20889 +out:
20890 +       AuTraceErr(err);
20891 +       return err;
20892 +}
20893 +
20894 +/*
20895 + * initial procedure of adding a new entry.
20896 + * prepare writable branch and the parent dir, lock it,
20897 + * and lookup whiteout for the new entry.
20898 + */
20899 +static struct dentry*
20900 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20901 +                 struct dentry *src_dentry, struct au_pin *pin,
20902 +                 struct au_wr_dir_args *wr_dir_args)
20903 +{
20904 +       struct dentry *wh_dentry, *h_parent;
20905 +       struct super_block *sb;
20906 +       struct au_branch *br;
20907 +       int err;
20908 +       unsigned int udba;
20909 +       aufs_bindex_t bcpup;
20910 +
20911 +       AuDbg("%pd\n", dentry);
20912 +
20913 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20914 +       bcpup = err;
20915 +       wh_dentry = ERR_PTR(err);
20916 +       if (unlikely(err < 0))
20917 +               goto out;
20918 +
20919 +       sb = dentry->d_sb;
20920 +       udba = au_opt_udba(sb);
20921 +       err = au_pin(pin, dentry, bcpup, udba,
20922 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20923 +       wh_dentry = ERR_PTR(err);
20924 +       if (unlikely(err))
20925 +               goto out;
20926 +
20927 +       h_parent = au_pinned_h_parent(pin);
20928 +       if (udba != AuOpt_UDBA_NONE
20929 +           && au_dbtop(dentry) == bcpup)
20930 +               err = au_may_add(dentry, bcpup, h_parent,
20931 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20932 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20933 +               err = -ENAMETOOLONG;
20934 +       wh_dentry = ERR_PTR(err);
20935 +       if (unlikely(err))
20936 +               goto out_unpin;
20937 +
20938 +       br = au_sbr(sb, bcpup);
20939 +       if (dt) {
20940 +               struct path tmp = {
20941 +                       .dentry = h_parent,
20942 +                       .mnt    = au_br_mnt(br)
20943 +               };
20944 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20945 +       }
20946 +
20947 +       wh_dentry = NULL;
20948 +       if (bcpup != au_dbwh(dentry))
20949 +               goto out; /* success */
20950 +
20951 +       /*
20952 +        * ENAMETOOLONG here means that if we allowed create such name, then it
20953 +        * would not be able to removed in the future. So we don't allow such
20954 +        * name here and we don't handle ENAMETOOLONG differently here.
20955 +        */
20956 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20957 +
20958 +out_unpin:
20959 +       if (IS_ERR(wh_dentry))
20960 +               au_unpin(pin);
20961 +out:
20962 +       return wh_dentry;
20963 +}
20964 +
20965 +/* ---------------------------------------------------------------------- */
20966 +
20967 +enum { Mknod, Symlink, Creat };
20968 +struct simple_arg {
20969 +       int type;
20970 +       union {
20971 +               struct {
20972 +                       umode_t                 mode;
20973 +                       bool                    want_excl;
20974 +                       bool                    try_aopen;
20975 +                       struct vfsub_aopen_args *aopen;
20976 +               } c;
20977 +               struct {
20978 +                       const char *symname;
20979 +               } s;
20980 +               struct {
20981 +                       umode_t mode;
20982 +                       dev_t dev;
20983 +               } m;
20984 +       } u;
20985 +};
20986 +
20987 +static int add_simple(struct inode *dir, struct dentry *dentry,
20988 +                     struct simple_arg *arg)
20989 +{
20990 +       int err, rerr;
20991 +       aufs_bindex_t btop;
20992 +       unsigned char created;
20993 +       const unsigned char try_aopen
20994 +               = (arg->type == Creat && arg->u.c.try_aopen);
20995 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
20996 +       struct dentry *wh_dentry, *parent;
20997 +       struct inode *h_dir;
20998 +       struct super_block *sb;
20999 +       struct au_branch *br;
21000 +       /* to reduce stack size */
21001 +       struct {
21002 +               struct au_dtime dt;
21003 +               struct au_pin pin;
21004 +               struct path h_path;
21005 +               struct au_wr_dir_args wr_dir_args;
21006 +       } *a;
21007 +
21008 +       AuDbg("%pd\n", dentry);
21009 +       IMustLock(dir);
21010 +
21011 +       err = -ENOMEM;
21012 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21013 +       if (unlikely(!a))
21014 +               goto out;
21015 +       a->wr_dir_args.force_btgt = -1;
21016 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21017 +
21018 +       parent = dentry->d_parent; /* dir inode is locked */
21019 +       if (!try_aopen) {
21020 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21021 +               if (unlikely(err))
21022 +                       goto out_free;
21023 +       }
21024 +       err = au_d_may_add(dentry);
21025 +       if (unlikely(err))
21026 +               goto out_unlock;
21027 +       if (!try_aopen)
21028 +               di_write_lock_parent(parent);
21029 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21030 +                                     &a->pin, &a->wr_dir_args);
21031 +       err = PTR_ERR(wh_dentry);
21032 +       if (IS_ERR(wh_dentry))
21033 +               goto out_parent;
21034 +
21035 +       btop = au_dbtop(dentry);
21036 +       sb = dentry->d_sb;
21037 +       br = au_sbr(sb, btop);
21038 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21039 +       a->h_path.mnt = au_br_mnt(br);
21040 +       h_dir = au_pinned_h_dir(&a->pin);
21041 +       switch (arg->type) {
21042 +       case Creat:
21043 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
21044 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21045 +                                          arg->u.c.want_excl);
21046 +                       created = !err;
21047 +                       if (!err && try_aopen)
21048 +                               aopen->file->f_mode |= FMODE_CREATED;
21049 +               } else {
21050 +                       aopen->br = br;
21051 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21052 +                       AuDbg("err %d\n", err);
21053 +                       AuDbgFile(aopen->file);
21054 +                       created = err >= 0
21055 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
21056 +               }
21057 +               break;
21058 +       case Symlink:
21059 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
21060 +               created = !err;
21061 +               break;
21062 +       case Mknod:
21063 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21064 +                                 arg->u.m.dev);
21065 +               created = !err;
21066 +               break;
21067 +       default:
21068 +               BUG();
21069 +       }
21070 +       if (unlikely(err < 0))
21071 +               goto out_unpin;
21072 +
21073 +       err = epilog(dir, btop, wh_dentry, dentry);
21074 +       if (!err)
21075 +               goto out_unpin; /* success */
21076 +
21077 +       /* revert */
21078 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
21079 +               /* no delegation since it is just created */
21080 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21081 +                                   /*force*/0);
21082 +               if (rerr) {
21083 +                       AuIOErr("%pd revert failure(%d, %d)\n",
21084 +                               dentry, err, rerr);
21085 +                       err = -EIO;
21086 +               }
21087 +               au_dtime_revert(&a->dt);
21088 +       }
21089 +       if (try_aopen && h_dir->i_op->atomic_open
21090 +           && (aopen->file->f_mode & FMODE_OPENED))
21091 +               /* aopen->file is still opened */
21092 +               au_lcnt_dec(&aopen->br->br_nfiles);
21093 +
21094 +out_unpin:
21095 +       au_unpin(&a->pin);
21096 +       dput(wh_dentry);
21097 +out_parent:
21098 +       if (!try_aopen)
21099 +               di_write_unlock(parent);
21100 +out_unlock:
21101 +       if (unlikely(err)) {
21102 +               au_update_dbtop(dentry);
21103 +               d_drop(dentry);
21104 +       }
21105 +       if (!try_aopen)
21106 +               aufs_read_unlock(dentry, AuLock_DW);
21107 +out_free:
21108 +       au_kfree_rcu(a);
21109 +out:
21110 +       return err;
21111 +}
21112 +
21113 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
21114 +              struct dentry *dentry, umode_t mode, dev_t dev)
21115 +{
21116 +       struct simple_arg arg = {
21117 +               .type = Mknod,
21118 +               .u.m = {
21119 +                       .mode   = mode,
21120 +                       .dev    = dev
21121 +               }
21122 +       };
21123 +       return add_simple(dir, dentry, &arg);
21124 +}
21125 +
21126 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
21127 +                struct dentry *dentry, const char *symname)
21128 +{
21129 +       struct simple_arg arg = {
21130 +               .type = Symlink,
21131 +               .u.s.symname = symname
21132 +       };
21133 +       return add_simple(dir, dentry, &arg);
21134 +}
21135 +
21136 +int aufs_create(struct user_namespace *userns, struct inode *dir,
21137 +               struct dentry *dentry, umode_t mode, bool want_excl)
21138 +{
21139 +       struct simple_arg arg = {
21140 +               .type = Creat,
21141 +               .u.c = {
21142 +                       .mode           = mode,
21143 +                       .want_excl      = want_excl
21144 +               }
21145 +       };
21146 +       return add_simple(dir, dentry, &arg);
21147 +}
21148 +
21149 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21150 +                      struct vfsub_aopen_args *aopen_args)
21151 +{
21152 +       struct simple_arg arg = {
21153 +               .type = Creat,
21154 +               .u.c = {
21155 +                       .mode           = aopen_args->create_mode,
21156 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
21157 +                       .try_aopen      = true,
21158 +                       .aopen          = aopen_args
21159 +               }
21160 +       };
21161 +       return add_simple(dir, dentry, &arg);
21162 +}
21163 +
21164 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
21165 +                struct dentry *dentry, umode_t mode)
21166 +{
21167 +       int err;
21168 +       aufs_bindex_t bindex;
21169 +       struct super_block *sb;
21170 +       struct dentry *parent, *h_parent, *h_dentry;
21171 +       struct inode *h_dir, *inode;
21172 +       struct vfsmount *h_mnt;
21173 +       struct user_namespace *h_userns;
21174 +       struct au_wr_dir_args wr_dir_args = {
21175 +               .force_btgt     = -1,
21176 +               .flags          = AuWrDir_TMPFILE
21177 +       };
21178 +
21179 +       /* copy-up may happen */
21180 +       inode_lock(dir);
21181 +
21182 +       sb = dir->i_sb;
21183 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21184 +       if (unlikely(err))
21185 +               goto out;
21186 +
21187 +       err = au_di_init(dentry);
21188 +       if (unlikely(err))
21189 +               goto out_si;
21190 +
21191 +       err = -EBUSY;
21192 +       parent = d_find_any_alias(dir);
21193 +       AuDebugOn(!parent);
21194 +       di_write_lock_parent(parent);
21195 +       if (unlikely(d_inode(parent) != dir))
21196 +               goto out_parent;
21197 +
21198 +       err = au_digen_test(parent, au_sigen(sb));
21199 +       if (unlikely(err))
21200 +               goto out_parent;
21201 +
21202 +       bindex = au_dbtop(parent);
21203 +       au_set_dbtop(dentry, bindex);
21204 +       au_set_dbbot(dentry, bindex);
21205 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21206 +       bindex = err;
21207 +       if (unlikely(err < 0))
21208 +               goto out_parent;
21209 +
21210 +       err = -EOPNOTSUPP;
21211 +       h_dir = au_h_iptr(dir, bindex);
21212 +       if (unlikely(!h_dir->i_op->tmpfile))
21213 +               goto out_parent;
21214 +
21215 +       h_mnt = au_sbr_mnt(sb, bindex);
21216 +       err = vfsub_mnt_want_write(h_mnt);
21217 +       if (unlikely(err))
21218 +               goto out_parent;
21219 +
21220 +       h_userns = mnt_user_ns(h_mnt);
21221 +       h_parent = au_h_dptr(parent, bindex);
21222 +       h_dentry = vfs_tmpfile(h_userns, h_parent, mode, /*open_flag*/0);
21223 +       if (IS_ERR(h_dentry)) {
21224 +               err = PTR_ERR(h_dentry);
21225 +               goto out_mnt;
21226 +       }
21227 +
21228 +       au_set_dbtop(dentry, bindex);
21229 +       au_set_dbbot(dentry, bindex);
21230 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
21231 +       inode = au_new_inode(dentry, /*must_new*/1);
21232 +       if (IS_ERR(inode)) {
21233 +               err = PTR_ERR(inode);
21234 +               au_set_h_dptr(dentry, bindex, NULL);
21235 +               au_set_dbtop(dentry, -1);
21236 +               au_set_dbbot(dentry, -1);
21237 +       } else {
21238 +               if (!inode->i_nlink)
21239 +                       set_nlink(inode, 1);
21240 +               d_tmpfile(dentry, inode);
21241 +               au_di(dentry)->di_tmpfile = 1;
21242 +
21243 +               /* update without i_mutex */
21244 +               if (au_ibtop(dir) == au_dbtop(dentry))
21245 +                       au_cpup_attr_timesizes(dir);
21246 +       }
21247 +       dput(h_dentry);
21248 +
21249 +out_mnt:
21250 +       vfsub_mnt_drop_write(h_mnt);
21251 +out_parent:
21252 +       di_write_unlock(parent);
21253 +       dput(parent);
21254 +       di_write_unlock(dentry);
21255 +       if (unlikely(err)) {
21256 +               au_di_fin(dentry);
21257 +               dentry->d_fsdata = NULL;
21258 +       }
21259 +out_si:
21260 +       si_read_unlock(sb);
21261 +out:
21262 +       inode_unlock(dir);
21263 +       return err;
21264 +}
21265 +
21266 +/* ---------------------------------------------------------------------- */
21267 +
21268 +struct au_link_args {
21269 +       aufs_bindex_t bdst, bsrc;
21270 +       struct au_pin pin;
21271 +       struct path h_path;
21272 +       struct dentry *src_parent, *parent;
21273 +};
21274 +
21275 +static int au_cpup_before_link(struct dentry *src_dentry,
21276 +                              struct au_link_args *a)
21277 +{
21278 +       int err;
21279 +       struct dentry *h_src_dentry;
21280 +       struct au_cp_generic cpg = {
21281 +               .dentry = src_dentry,
21282 +               .bdst   = a->bdst,
21283 +               .bsrc   = a->bsrc,
21284 +               .len    = -1,
21285 +               .pin    = &a->pin,
21286 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21287 +       };
21288 +
21289 +       di_read_lock_parent(a->src_parent, AuLock_IR);
21290 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21291 +       if (unlikely(err))
21292 +               goto out;
21293 +
21294 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
21295 +       err = au_pin(&a->pin, src_dentry, a->bdst,
21296 +                    au_opt_udba(src_dentry->d_sb),
21297 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21298 +       if (unlikely(err))
21299 +               goto out;
21300 +
21301 +       err = au_sio_cpup_simple(&cpg);
21302 +       au_unpin(&a->pin);
21303 +
21304 +out:
21305 +       di_read_unlock(a->src_parent, AuLock_IR);
21306 +       return err;
21307 +}
21308 +
21309 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21310 +                          struct au_link_args *a)
21311 +{
21312 +       int err;
21313 +       unsigned char plink;
21314 +       aufs_bindex_t bbot;
21315 +       struct dentry *h_src_dentry;
21316 +       struct inode *h_inode, *inode, *delegated;
21317 +       struct super_block *sb;
21318 +       struct file *h_file;
21319 +
21320 +       plink = 0;
21321 +       h_inode = NULL;
21322 +       sb = src_dentry->d_sb;
21323 +       inode = d_inode(src_dentry);
21324 +       if (au_ibtop(inode) <= a->bdst)
21325 +               h_inode = au_h_iptr(inode, a->bdst);
21326 +       if (!h_inode || !h_inode->i_nlink) {
21327 +               /* copyup src_dentry as the name of dentry. */
21328 +               bbot = au_dbbot(dentry);
21329 +               if (bbot < a->bsrc)
21330 +                       au_set_dbbot(dentry, a->bsrc);
21331 +               au_set_h_dptr(dentry, a->bsrc,
21332 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
21333 +               dget(a->h_path.dentry);
21334 +               au_set_h_dptr(dentry, a->bdst, NULL);
21335 +               AuDbg("temporary d_inode...\n");
21336 +               spin_lock(&dentry->d_lock);
21337 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
21338 +               spin_unlock(&dentry->d_lock);
21339 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
21340 +               if (IS_ERR(h_file))
21341 +                       err = PTR_ERR(h_file);
21342 +               else {
21343 +                       struct au_cp_generic cpg = {
21344 +                               .dentry = dentry,
21345 +                               .bdst   = a->bdst,
21346 +                               .bsrc   = -1,
21347 +                               .len    = -1,
21348 +                               .pin    = &a->pin,
21349 +                               .flags  = AuCpup_KEEPLINO
21350 +                       };
21351 +                       err = au_sio_cpup_simple(&cpg);
21352 +                       au_h_open_post(dentry, a->bsrc, h_file);
21353 +                       if (!err) {
21354 +                               dput(a->h_path.dentry);
21355 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21356 +                       } else
21357 +                               au_set_h_dptr(dentry, a->bdst,
21358 +                                             a->h_path.dentry);
21359 +               }
21360 +               spin_lock(&dentry->d_lock);
21361 +               dentry->d_inode = NULL; /* restore */
21362 +               spin_unlock(&dentry->d_lock);
21363 +               AuDbg("temporary d_inode...done\n");
21364 +               au_set_h_dptr(dentry, a->bsrc, NULL);
21365 +               au_set_dbbot(dentry, bbot);
21366 +       } else {
21367 +               /* the inode of src_dentry already exists on a.bdst branch */
21368 +               h_src_dentry = d_find_alias(h_inode);
21369 +               if (!h_src_dentry && au_plink_test(inode)) {
21370 +                       plink = 1;
21371 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
21372 +                       err = PTR_ERR(h_src_dentry);
21373 +                       if (IS_ERR(h_src_dentry))
21374 +                               goto out;
21375 +
21376 +                       if (unlikely(d_is_negative(h_src_dentry))) {
21377 +                               dput(h_src_dentry);
21378 +                               h_src_dentry = NULL;
21379 +                       }
21380 +
21381 +               }
21382 +               if (h_src_dentry) {
21383 +                       delegated = NULL;
21384 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21385 +                                        &a->h_path, &delegated);
21386 +                       if (unlikely(err == -EWOULDBLOCK)) {
21387 +                               pr_warn("cannot retry for NFSv4 delegation"
21388 +                                       " for an internal link\n");
21389 +                               iput(delegated);
21390 +                       }
21391 +                       dput(h_src_dentry);
21392 +               } else {
21393 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
21394 +                               h_inode->i_ino, a->bdst);
21395 +                       err = -EIO;
21396 +               }
21397 +       }
21398 +
21399 +       if (!err && !plink)
21400 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
21401 +
21402 +out:
21403 +       AuTraceErr(err);
21404 +       return err;
21405 +}
21406 +
21407 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
21408 +             struct dentry *dentry)
21409 +{
21410 +       int err, rerr;
21411 +       struct au_dtime dt;
21412 +       struct au_link_args *a;
21413 +       struct dentry *wh_dentry, *h_src_dentry;
21414 +       struct inode *inode, *delegated;
21415 +       struct super_block *sb;
21416 +       struct au_wr_dir_args wr_dir_args = {
21417 +               /* .force_btgt  = -1, */
21418 +               .flags          = AuWrDir_ADD_ENTRY
21419 +       };
21420 +
21421 +       IMustLock(dir);
21422 +       inode = d_inode(src_dentry);
21423 +       IMustLock(inode);
21424 +
21425 +       err = -ENOMEM;
21426 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21427 +       if (unlikely(!a))
21428 +               goto out;
21429 +
21430 +       a->parent = dentry->d_parent; /* dir inode is locked */
21431 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
21432 +                                       AuLock_NOPLM | AuLock_GEN);
21433 +       if (unlikely(err))
21434 +               goto out_kfree;
21435 +       err = au_d_linkable(src_dentry);
21436 +       if (unlikely(err))
21437 +               goto out_unlock;
21438 +       err = au_d_may_add(dentry);
21439 +       if (unlikely(err))
21440 +               goto out_unlock;
21441 +
21442 +       a->src_parent = dget_parent(src_dentry);
21443 +       wr_dir_args.force_btgt = au_ibtop(inode);
21444 +
21445 +       di_write_lock_parent(a->parent);
21446 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21447 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21448 +                                     &wr_dir_args);
21449 +       err = PTR_ERR(wh_dentry);
21450 +       if (IS_ERR(wh_dentry))
21451 +               goto out_parent;
21452 +
21453 +       err = 0;
21454 +       sb = dentry->d_sb;
21455 +       a->bdst = au_dbtop(dentry);
21456 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21457 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21458 +       a->bsrc = au_ibtop(inode);
21459 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21460 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21461 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21462 +       if (!h_src_dentry) {
21463 +               a->bsrc = au_dbtop(src_dentry);
21464 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21465 +               AuDebugOn(!h_src_dentry);
21466 +       } else if (IS_ERR(h_src_dentry)) {
21467 +               err = PTR_ERR(h_src_dentry);
21468 +               goto out_parent;
21469 +       }
21470 +
21471 +       /*
21472 +        * aufs doesn't touch the credential so
21473 +        * security_dentry_create_files_as() is unnecessary.
21474 +        */
21475 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21476 +               if (a->bdst < a->bsrc
21477 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21478 +                       err = au_cpup_or_link(src_dentry, dentry, a);
21479 +               else {
21480 +                       delegated = NULL;
21481 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21482 +                                        &a->h_path, &delegated);
21483 +                       if (unlikely(err == -EWOULDBLOCK)) {
21484 +                               pr_warn("cannot retry for NFSv4 delegation"
21485 +                                       " for an internal link\n");
21486 +                               iput(delegated);
21487 +                       }
21488 +               }
21489 +               dput(h_src_dentry);
21490 +       } else {
21491 +               /*
21492 +                * copyup src_dentry to the branch we process,
21493 +                * and then link(2) to it.
21494 +                */
21495 +               dput(h_src_dentry);
21496 +               if (a->bdst < a->bsrc
21497 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21498 +                       au_unpin(&a->pin);
21499 +                       di_write_unlock(a->parent);
21500 +                       err = au_cpup_before_link(src_dentry, a);
21501 +                       di_write_lock_parent(a->parent);
21502 +                       if (!err)
21503 +                               err = au_pin(&a->pin, dentry, a->bdst,
21504 +                                            au_opt_udba(sb),
21505 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21506 +                       if (unlikely(err))
21507 +                               goto out_wh;
21508 +               }
21509 +               if (!err) {
21510 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21511 +                       err = -ENOENT;
21512 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
21513 +                               delegated = NULL;
21514 +                               err = vfsub_link(h_src_dentry,
21515 +                                                au_pinned_h_dir(&a->pin),
21516 +                                                &a->h_path, &delegated);
21517 +                               if (unlikely(err == -EWOULDBLOCK)) {
21518 +                                       pr_warn("cannot retry"
21519 +                                               " for NFSv4 delegation"
21520 +                                               " for an internal link\n");
21521 +                                       iput(delegated);
21522 +                               }
21523 +                       }
21524 +               }
21525 +       }
21526 +       if (unlikely(err))
21527 +               goto out_unpin;
21528 +
21529 +       if (wh_dentry) {
21530 +               a->h_path.dentry = wh_dentry;
21531 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21532 +                                         dentry);
21533 +               if (unlikely(err))
21534 +                       goto out_revert;
21535 +       }
21536 +
21537 +       au_dir_ts(dir, a->bdst);
21538 +       inode_inc_iversion(dir);
21539 +       inc_nlink(inode);
21540 +       inode->i_ctime = dir->i_ctime;
21541 +       d_instantiate(dentry, au_igrab(inode));
21542 +       if (d_unhashed(a->h_path.dentry))
21543 +               /* some filesystem calls d_drop() */
21544 +               d_drop(dentry);
21545 +       /* some filesystems consume an inode even hardlink */
21546 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
21547 +       goto out_unpin; /* success */
21548 +
21549 +out_revert:
21550 +       /* no delegation since it is just created */
21551 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21552 +                           /*delegated*/NULL, /*force*/0);
21553 +       if (unlikely(rerr)) {
21554 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21555 +               err = -EIO;
21556 +       }
21557 +       au_dtime_revert(&dt);
21558 +out_unpin:
21559 +       au_unpin(&a->pin);
21560 +out_wh:
21561 +       dput(wh_dentry);
21562 +out_parent:
21563 +       di_write_unlock(a->parent);
21564 +       dput(a->src_parent);
21565 +out_unlock:
21566 +       if (unlikely(err)) {
21567 +               au_update_dbtop(dentry);
21568 +               d_drop(dentry);
21569 +       }
21570 +       aufs_read_and_write_unlock2(dentry, src_dentry);
21571 +out_kfree:
21572 +       au_kfree_rcu(a);
21573 +out:
21574 +       AuTraceErr(err);
21575 +       return err;
21576 +}
21577 +
21578 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
21579 +              struct dentry *dentry, umode_t mode)
21580 +{
21581 +       int err, rerr;
21582 +       aufs_bindex_t bindex;
21583 +       unsigned char diropq;
21584 +       struct path h_path;
21585 +       struct dentry *wh_dentry, *parent, *opq_dentry;
21586 +       struct inode *h_inode;
21587 +       struct super_block *sb;
21588 +       struct {
21589 +               struct au_pin pin;
21590 +               struct au_dtime dt;
21591 +       } *a; /* reduce the stack usage */
21592 +       struct au_wr_dir_args wr_dir_args = {
21593 +               .force_btgt     = -1,
21594 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21595 +       };
21596 +
21597 +       IMustLock(dir);
21598 +
21599 +       err = -ENOMEM;
21600 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21601 +       if (unlikely(!a))
21602 +               goto out;
21603 +
21604 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21605 +       if (unlikely(err))
21606 +               goto out_free;
21607 +       err = au_d_may_add(dentry);
21608 +       if (unlikely(err))
21609 +               goto out_unlock;
21610 +
21611 +       parent = dentry->d_parent; /* dir inode is locked */
21612 +       di_write_lock_parent(parent);
21613 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21614 +                                     &a->pin, &wr_dir_args);
21615 +       err = PTR_ERR(wh_dentry);
21616 +       if (IS_ERR(wh_dentry))
21617 +               goto out_parent;
21618 +
21619 +       sb = dentry->d_sb;
21620 +       bindex = au_dbtop(dentry);
21621 +       h_path.dentry = au_h_dptr(dentry, bindex);
21622 +       h_path.mnt = au_sbr_mnt(sb, bindex);
21623 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21624 +       if (unlikely(err))
21625 +               goto out_unpin;
21626 +
21627 +       /* make the dir opaque */
21628 +       diropq = 0;
21629 +       h_inode = d_inode(h_path.dentry);
21630 +       if (wh_dentry
21631 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21632 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21633 +               opq_dentry = au_diropq_create(dentry, bindex);
21634 +               inode_unlock(h_inode);
21635 +               err = PTR_ERR(opq_dentry);
21636 +               if (IS_ERR(opq_dentry))
21637 +                       goto out_dir;
21638 +               dput(opq_dentry);
21639 +               diropq = 1;
21640 +       }
21641 +
21642 +       err = epilog(dir, bindex, wh_dentry, dentry);
21643 +       if (!err) {
21644 +               inc_nlink(dir);
21645 +               goto out_unpin; /* success */
21646 +       }
21647 +
21648 +       /* revert */
21649 +       if (diropq) {
21650 +               AuLabel(revert opq);
21651 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21652 +               rerr = au_diropq_remove(dentry, bindex);
21653 +               inode_unlock(h_inode);
21654 +               if (rerr) {
21655 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21656 +                               dentry, err, rerr);
21657 +                       err = -EIO;
21658 +               }
21659 +       }
21660 +
21661 +out_dir:
21662 +       AuLabel(revert dir);
21663 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21664 +       if (rerr) {
21665 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
21666 +                       dentry, err, rerr);
21667 +               err = -EIO;
21668 +       }
21669 +       au_dtime_revert(&a->dt);
21670 +out_unpin:
21671 +       au_unpin(&a->pin);
21672 +       dput(wh_dentry);
21673 +out_parent:
21674 +       di_write_unlock(parent);
21675 +out_unlock:
21676 +       if (unlikely(err)) {
21677 +               au_update_dbtop(dentry);
21678 +               d_drop(dentry);
21679 +       }
21680 +       aufs_read_unlock(dentry, AuLock_DW);
21681 +out_free:
21682 +       au_kfree_rcu(a);
21683 +out:
21684 +       return err;
21685 +}
21686 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
21687 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
21688 +++ linux/fs/aufs/i_op.c        2022-08-02 12:12:45.165641939 +0200
21689 @@ -0,0 +1,1512 @@
21690 +// SPDX-License-Identifier: GPL-2.0
21691 +/*
21692 + * Copyright (C) 2005-2021 Junjiro R. Okajima
21693 + *
21694 + * This program, aufs is free software; you can redistribute it and/or modify
21695 + * it under the terms of the GNU General Public License as published by
21696 + * the Free Software Foundation; either version 2 of the License, or
21697 + * (at your option) any later version.
21698 + *
21699 + * This program is distributed in the hope that it will be useful,
21700 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21701 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21702 + * GNU General Public License for more details.
21703 + *
21704 + * You should have received a copy of the GNU General Public License
21705 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21706 + */
21707 +
21708 +/*
21709 + * inode operations (except add/del/rename)
21710 + */
21711 +
21712 +#include <linux/device_cgroup.h>
21713 +#include <linux/fs_stack.h>
21714 +#include <linux/iversion.h>
21715 +#include <linux/security.h>
21716 +#include "aufs.h"
21717 +
21718 +static int h_permission(struct inode *h_inode, int mask,
21719 +                       struct path *h_path, int brperm)
21720 +{
21721 +       int err;
21722 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21723 +       struct user_namespace *h_userns;
21724 +
21725 +       err = -EPERM;
21726 +       if (write_mask && IS_IMMUTABLE(h_inode))
21727 +               goto out;
21728 +
21729 +       err = -EACCES;
21730 +       if (((mask & MAY_EXEC)
21731 +            && S_ISREG(h_inode->i_mode)
21732 +            && (path_noexec(h_path)
21733 +                || !(h_inode->i_mode & 0111))))
21734 +               goto out;
21735 +
21736 +       /*
21737 +        * - skip the lower fs test in the case of write to ro branch.
21738 +        * - nfs dir permission write check is optimized, but a policy for
21739 +        *   link/rename requires a real check.
21740 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
21741 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
21742 +        */
21743 +       h_userns = mnt_user_ns(h_path->mnt);
21744 +       if ((write_mask && !au_br_writable(brperm))
21745 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21746 +               && write_mask && !(mask & MAY_READ))
21747 +           || !h_inode->i_op->permission) {
21748 +               /* AuLabel(generic_permission); */
21749 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
21750 +               err = generic_permission(h_userns, h_inode, mask);
21751 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
21752 +                       err = h_inode->i_op->permission(h_userns, h_inode,
21753 +                                                       mask);
21754 +               AuTraceErr(err);
21755 +       } else {
21756 +               /* AuLabel(h_inode->permission); */
21757 +               err = h_inode->i_op->permission(h_userns, h_inode, mask);
21758 +               AuTraceErr(err);
21759 +       }
21760 +
21761 +       if (!err)
21762 +               err = devcgroup_inode_permission(h_inode, mask);
21763 +       if (!err)
21764 +               err = security_inode_permission(h_inode, mask);
21765 +
21766 +out:
21767 +       return err;
21768 +}
21769 +
21770 +static int aufs_permission(struct user_namespace *userns, struct inode *inode,
21771 +                          int mask)
21772 +{
21773 +       int err;
21774 +       aufs_bindex_t bindex, bbot;
21775 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21776 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21777 +       struct inode *h_inode;
21778 +       struct super_block *sb;
21779 +       struct au_branch *br;
21780 +
21781 +       /* todo: support rcu-walk? */
21782 +       if (mask & MAY_NOT_BLOCK)
21783 +               return -ECHILD;
21784 +
21785 +       sb = inode->i_sb;
21786 +       si_read_lock(sb, AuLock_FLUSH);
21787 +       ii_read_lock_child(inode);
21788 +#if 0 /* reserved for future use */
21789 +       /*
21790 +        * This test may be rather 'too much' since the test is essentially done
21791 +        * in the aufs_lookup().  Theoretically it is possible that the inode
21792 +        * generation doesn't match to the superblock's here.  But it isn't a
21793 +        * big deal I suppose.
21794 +        */
21795 +       err = au_iigen_test(inode, au_sigen(sb));
21796 +       if (unlikely(err))
21797 +               goto out;
21798 +#endif
21799 +
21800 +       if (!isdir
21801 +           || write_mask
21802 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
21803 +               err = au_busy_or_stale();
21804 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
21805 +               if (unlikely(!h_inode
21806 +                            || (h_inode->i_mode & S_IFMT)
21807 +                            != (inode->i_mode & S_IFMT)))
21808 +                       goto out;
21809 +
21810 +               err = 0;
21811 +               bindex = au_ibtop(inode);
21812 +               br = au_sbr(sb, bindex);
21813 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
21814 +               if (write_mask
21815 +                   && !err
21816 +                   && !special_file(h_inode->i_mode)) {
21817 +                       /* test whether the upper writable branch exists */
21818 +                       err = -EROFS;
21819 +                       for (; bindex >= 0; bindex--)
21820 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
21821 +                                       err = 0;
21822 +                                       break;
21823 +                               }
21824 +               }
21825 +               goto out;
21826 +       }
21827 +
21828 +       /* non-write to dir */
21829 +       err = 0;
21830 +       bbot = au_ibbot(inode);
21831 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
21832 +               h_inode = au_h_iptr(inode, bindex);
21833 +               if (h_inode) {
21834 +                       err = au_busy_or_stale();
21835 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
21836 +                               break;
21837 +
21838 +                       br = au_sbr(sb, bindex);
21839 +                       err = h_permission(h_inode, mask, &br->br_path,
21840 +                                          br->br_perm);
21841 +               }
21842 +       }
21843 +
21844 +out:
21845 +       ii_read_unlock(inode);
21846 +       si_read_unlock(sb);
21847 +       return err;
21848 +}
21849 +
21850 +/* ---------------------------------------------------------------------- */
21851 +
21852 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
21853 +                                 unsigned int flags)
21854 +{
21855 +       struct dentry *ret, *parent;
21856 +       struct inode *inode;
21857 +       struct super_block *sb;
21858 +       int err, npositive;
21859 +
21860 +       IMustLock(dir);
21861 +
21862 +       /* todo: support rcu-walk? */
21863 +       ret = ERR_PTR(-ECHILD);
21864 +       if (flags & LOOKUP_RCU)
21865 +               goto out;
21866 +
21867 +       ret = ERR_PTR(-ENAMETOOLONG);
21868 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21869 +               goto out;
21870 +
21871 +       sb = dir->i_sb;
21872 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21873 +       ret = ERR_PTR(err);
21874 +       if (unlikely(err))
21875 +               goto out;
21876 +
21877 +       err = au_di_init(dentry);
21878 +       ret = ERR_PTR(err);
21879 +       if (unlikely(err))
21880 +               goto out_si;
21881 +
21882 +       inode = NULL;
21883 +       npositive = 0; /* suppress a warning */
21884 +       parent = dentry->d_parent; /* dir inode is locked */
21885 +       di_read_lock_parent(parent, AuLock_IR);
21886 +       err = au_alive_dir(parent);
21887 +       if (!err)
21888 +               err = au_digen_test(parent, au_sigen(sb));
21889 +       if (!err) {
21890 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21891 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21892 +                                          AuLkup_ALLOW_NEG);
21893 +               err = npositive;
21894 +       }
21895 +       di_read_unlock(parent, AuLock_IR);
21896 +       ret = ERR_PTR(err);
21897 +       if (unlikely(err < 0))
21898 +               goto out_unlock;
21899 +
21900 +       if (npositive) {
21901 +               inode = au_new_inode(dentry, /*must_new*/0);
21902 +               if (IS_ERR(inode)) {
21903 +                       ret = (void *)inode;
21904 +                       inode = NULL;
21905 +                       goto out_unlock;
21906 +               }
21907 +       }
21908 +
21909 +       if (inode)
21910 +               atomic_inc(&inode->i_count);
21911 +       ret = d_splice_alias(inode, dentry);
21912 +#if 0 /* reserved for future use */
21913 +       if (unlikely(d_need_lookup(dentry))) {
21914 +               spin_lock(&dentry->d_lock);
21915 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21916 +               spin_unlock(&dentry->d_lock);
21917 +       } else
21918 +#endif
21919 +       if (inode) {
21920 +               if (!IS_ERR(ret)) {
21921 +                       iput(inode);
21922 +                       if (ret && ret != dentry)
21923 +                               ii_write_unlock(inode);
21924 +               } else {
21925 +                       ii_write_unlock(inode);
21926 +                       iput(inode);
21927 +                       inode = NULL;
21928 +               }
21929 +       }
21930 +
21931 +out_unlock:
21932 +       di_write_unlock(dentry);
21933 +out_si:
21934 +       si_read_unlock(sb);
21935 +out:
21936 +       return ret;
21937 +}
21938 +
21939 +/* ---------------------------------------------------------------------- */
21940 +
21941 +/*
21942 + * very dirty and complicated aufs ->atomic_open().
21943 + * aufs_atomic_open()
21944 + * + au_aopen_or_create()
21945 + *   + add_simple()
21946 + *     + vfsub_atomic_open()
21947 + *       + branch fs ->atomic_open()
21948 + *        may call the actual 'open' for h_file
21949 + *       + inc br_nfiles only if opened
21950 + * + au_aopen_no_open() or au_aopen_do_open()
21951 + *
21952 + * au_aopen_do_open()
21953 + * + finish_open()
21954 + *   + au_do_aopen()
21955 + *     + au_do_open() the body of all 'open'
21956 + *       + au_do_open_nondir()
21957 + *        set the passed h_file
21958 + *
21959 + * au_aopen_no_open()
21960 + * + finish_no_open()
21961 + */
21962 +
21963 +struct aopen_node {
21964 +       struct hlist_bl_node hblist;
21965 +       struct file *file, *h_file;
21966 +};
21967 +
21968 +static int au_do_aopen(struct inode *inode, struct file *file)
21969 +{
21970 +       struct hlist_bl_head *aopen;
21971 +       struct hlist_bl_node *pos;
21972 +       struct aopen_node *node;
21973 +       struct au_do_open_args args = {
21974 +               .aopen  = 1,
21975 +               .open   = au_do_open_nondir
21976 +       };
21977 +
21978 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
21979 +       hlist_bl_lock(aopen);
21980 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
21981 +               if (node->file == file) {
21982 +                       args.h_file = node->h_file;
21983 +                       break;
21984 +               }
21985 +       hlist_bl_unlock(aopen);
21986 +       /* AuDebugOn(!args.h_file); */
21987 +
21988 +       return au_do_open(file, &args);
21989 +}
21990 +
21991 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
21992 +                           struct aopen_node *aopen_node)
21993 +{
21994 +       int err;
21995 +       struct hlist_bl_head *aopen;
21996 +
21997 +       AuLabel(here);
21998 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
21999 +       au_hbl_add(&aopen_node->hblist, aopen);
22000 +       err = finish_open(file, dentry, au_do_aopen);
22001 +       au_hbl_del(&aopen_node->hblist, aopen);
22002 +       /* AuDbgFile(file); */
22003 +       AuDbg("%pd%s%s\n", dentry,
22004 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
22005 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
22006 +
22007 +       AuTraceErr(err);
22008 +       return err;
22009 +}
22010 +
22011 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
22012 +{
22013 +       int err;
22014 +
22015 +       AuLabel(here);
22016 +       dget(dentry);
22017 +       err = finish_no_open(file, dentry);
22018 +
22019 +       AuTraceErr(err);
22020 +       return err;
22021 +}
22022 +
22023 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22024 +                           struct file *file, unsigned int open_flag,
22025 +                           umode_t create_mode)
22026 +{
22027 +       int err, did_open;
22028 +       unsigned int lkup_flags;
22029 +       aufs_bindex_t bindex;
22030 +       struct super_block *sb;
22031 +       struct dentry *parent, *d;
22032 +       struct vfsub_aopen_args args = {
22033 +               .open_flag      = open_flag,
22034 +               .create_mode    = create_mode
22035 +       };
22036 +       struct aopen_node aopen_node = {
22037 +               .file   = file
22038 +       };
22039 +
22040 +       IMustLock(dir);
22041 +       AuDbg("open_flag 0%o\n", open_flag);
22042 +       AuDbgDentry(dentry);
22043 +
22044 +       err = 0;
22045 +       if (!au_di(dentry)) {
22046 +               lkup_flags = LOOKUP_OPEN;
22047 +               if (open_flag & O_CREAT)
22048 +                       lkup_flags |= LOOKUP_CREATE;
22049 +               d = aufs_lookup(dir, dentry, lkup_flags);
22050 +               if (IS_ERR(d)) {
22051 +                       err = PTR_ERR(d);
22052 +                       AuTraceErr(err);
22053 +                       goto out;
22054 +               } else if (d) {
22055 +                       /*
22056 +                        * obsoleted dentry found.
22057 +                        * another error will be returned later.
22058 +                        */
22059 +                       d_drop(d);
22060 +                       AuDbgDentry(d);
22061 +                       dput(d);
22062 +               }
22063 +               AuDbgDentry(dentry);
22064 +       }
22065 +
22066 +       if (d_is_positive(dentry)
22067 +           || d_unhashed(dentry)
22068 +           || d_unlinked(dentry)
22069 +           || !(open_flag & O_CREAT)) {
22070 +               err = au_aopen_no_open(file, dentry);
22071 +               goto out; /* success */
22072 +       }
22073 +
22074 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22075 +       if (unlikely(err))
22076 +               goto out;
22077 +
22078 +       sb = dentry->d_sb;
22079 +       parent = dentry->d_parent;      /* dir is locked */
22080 +       di_write_lock_parent(parent);
22081 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
22082 +       if (unlikely(err < 0))
22083 +               goto out_parent;
22084 +
22085 +       AuDbgDentry(dentry);
22086 +       if (d_is_positive(dentry)) {
22087 +               err = au_aopen_no_open(file, dentry);
22088 +               goto out_parent; /* success */
22089 +       }
22090 +
22091 +       args.file = alloc_empty_file(file->f_flags, current_cred());
22092 +       err = PTR_ERR(args.file);
22093 +       if (IS_ERR(args.file))
22094 +               goto out_parent;
22095 +
22096 +       bindex = au_dbtop(dentry);
22097 +       err = au_aopen_or_create(dir, dentry, &args);
22098 +       AuTraceErr(err);
22099 +       AuDbgFile(args.file);
22100 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22101 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
22102 +       if (!did_open) {
22103 +               fput(args.file);
22104 +               args.file = NULL;
22105 +       }
22106 +       di_write_unlock(parent);
22107 +       di_write_unlock(dentry);
22108 +       if (unlikely(err < 0)) {
22109 +               if (args.file)
22110 +                       fput(args.file);
22111 +               goto out_sb;
22112 +       }
22113 +
22114 +       if (!did_open)
22115 +               err = au_aopen_no_open(file, dentry);
22116 +       else {
22117 +               aopen_node.h_file = args.file;
22118 +               err = au_aopen_do_open(file, dentry, &aopen_node);
22119 +       }
22120 +       if (unlikely(err < 0)) {
22121 +               if (args.file)
22122 +                       fput(args.file);
22123 +               if (did_open)
22124 +                       au_lcnt_dec(&args.br->br_nfiles);
22125 +       }
22126 +       goto out_sb; /* success */
22127 +
22128 +out_parent:
22129 +       di_write_unlock(parent);
22130 +       di_write_unlock(dentry);
22131 +out_sb:
22132 +       si_read_unlock(sb);
22133 +out:
22134 +       AuTraceErr(err);
22135 +       AuDbgFile(file);
22136 +       return err;
22137 +}
22138 +
22139 +
22140 +/* ---------------------------------------------------------------------- */
22141 +
22142 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22143 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
22144 +                         aufs_bindex_t btop)
22145 +{
22146 +       int err;
22147 +       struct dentry *h_parent;
22148 +       struct inode *h_dir;
22149 +
22150 +       if (add_entry)
22151 +               IMustLock(d_inode(parent));
22152 +       else
22153 +               di_write_lock_parent(parent);
22154 +
22155 +       err = 0;
22156 +       if (!au_h_dptr(parent, bcpup)) {
22157 +               if (btop > bcpup)
22158 +                       err = au_cpup_dirs(dentry, bcpup);
22159 +               else if (btop < bcpup)
22160 +                       err = au_cpdown_dirs(dentry, bcpup);
22161 +               else
22162 +                       BUG();
22163 +       }
22164 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
22165 +               h_parent = au_h_dptr(parent, bcpup);
22166 +               h_dir = d_inode(h_parent);
22167 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
22168 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
22169 +               /* todo: no unlock here */
22170 +               inode_unlock_shared(h_dir);
22171 +
22172 +               AuDbg("bcpup %d\n", bcpup);
22173 +               if (!err) {
22174 +                       if (d_really_is_negative(dentry))
22175 +                               au_set_h_dptr(dentry, btop, NULL);
22176 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
22177 +               }
22178 +       }
22179 +
22180 +       if (!add_entry)
22181 +               di_write_unlock(parent);
22182 +       if (!err)
22183 +               err = bcpup; /* success */
22184 +
22185 +       AuTraceErr(err);
22186 +       return err;
22187 +}
22188 +
22189 +/*
22190 + * decide the branch and the parent dir where we will create a new entry.
22191 + * returns new bindex or an error.
22192 + * copyup the parent dir if needed.
22193 + */
22194 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22195 +             struct au_wr_dir_args *args)
22196 +{
22197 +       int err;
22198 +       unsigned int flags;
22199 +       aufs_bindex_t bcpup, btop, src_btop;
22200 +       const unsigned char add_entry
22201 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
22202 +               | au_ftest_wrdir(args->flags, TMPFILE);
22203 +       struct super_block *sb;
22204 +       struct dentry *parent;
22205 +       struct au_sbinfo *sbinfo;
22206 +
22207 +       sb = dentry->d_sb;
22208 +       sbinfo = au_sbi(sb);
22209 +       parent = dget_parent(dentry);
22210 +       btop = au_dbtop(dentry);
22211 +       bcpup = btop;
22212 +       if (args->force_btgt < 0) {
22213 +               if (src_dentry) {
22214 +                       src_btop = au_dbtop(src_dentry);
22215 +                       if (src_btop < btop)
22216 +                               bcpup = src_btop;
22217 +               } else if (add_entry) {
22218 +                       flags = 0;
22219 +                       if (au_ftest_wrdir(args->flags, ISDIR))
22220 +                               au_fset_wbr(flags, DIR);
22221 +                       err = AuWbrCreate(sbinfo, dentry, flags);
22222 +                       bcpup = err;
22223 +               }
22224 +
22225 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
22226 +                       if (add_entry)
22227 +                               err = AuWbrCopyup(sbinfo, dentry);
22228 +                       else {
22229 +                               if (!IS_ROOT(dentry)) {
22230 +                                       di_read_lock_parent(parent, !AuLock_IR);
22231 +                                       err = AuWbrCopyup(sbinfo, dentry);
22232 +                                       di_read_unlock(parent, !AuLock_IR);
22233 +                               } else
22234 +                                       err = AuWbrCopyup(sbinfo, dentry);
22235 +                       }
22236 +                       bcpup = err;
22237 +                       if (unlikely(err < 0))
22238 +                               goto out;
22239 +               }
22240 +       } else {
22241 +               bcpup = args->force_btgt;
22242 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
22243 +       }
22244 +
22245 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
22246 +       err = bcpup;
22247 +       if (bcpup == btop)
22248 +               goto out; /* success */
22249 +
22250 +       /* copyup the new parent into the branch we process */
22251 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
22252 +       if (err >= 0) {
22253 +               if (d_really_is_negative(dentry)) {
22254 +                       au_set_h_dptr(dentry, btop, NULL);
22255 +                       au_set_dbtop(dentry, bcpup);
22256 +                       au_set_dbbot(dentry, bcpup);
22257 +               }
22258 +               AuDebugOn(add_entry
22259 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
22260 +                         && !au_h_dptr(dentry, bcpup));
22261 +       }
22262 +
22263 +out:
22264 +       dput(parent);
22265 +       return err;
22266 +}
22267 +
22268 +/* ---------------------------------------------------------------------- */
22269 +
22270 +void au_pin_hdir_unlock(struct au_pin *p)
22271 +{
22272 +       if (p->hdir)
22273 +               au_hn_inode_unlock(p->hdir);
22274 +}
22275 +
22276 +int au_pin_hdir_lock(struct au_pin *p)
22277 +{
22278 +       int err;
22279 +
22280 +       err = 0;
22281 +       if (!p->hdir)
22282 +               goto out;
22283 +
22284 +       /* even if an error happens later, keep this lock */
22285 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
22286 +
22287 +       err = -EBUSY;
22288 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
22289 +               goto out;
22290 +
22291 +       err = 0;
22292 +       if (p->h_dentry)
22293 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22294 +                                 p->h_parent, p->br);
22295 +
22296 +out:
22297 +       return err;
22298 +}
22299 +
22300 +int au_pin_hdir_relock(struct au_pin *p)
22301 +{
22302 +       int err, i;
22303 +       struct inode *h_i;
22304 +       struct dentry *h_d[] = {
22305 +               p->h_dentry,
22306 +               p->h_parent
22307 +       };
22308 +
22309 +       err = au_pin_hdir_lock(p);
22310 +       if (unlikely(err))
22311 +               goto out;
22312 +
22313 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22314 +               if (!h_d[i])
22315 +                       continue;
22316 +               if (d_is_positive(h_d[i])) {
22317 +                       h_i = d_inode(h_d[i]);
22318 +                       err = !h_i->i_nlink;
22319 +               }
22320 +       }
22321 +
22322 +out:
22323 +       return err;
22324 +}
22325 +
22326 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
22327 +{
22328 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
22329 +}
22330 +
22331 +void au_pin_hdir_acquire_nest(struct au_pin *p)
22332 +{
22333 +       if (p->hdir) {
22334 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
22335 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
22336 +               au_pin_hdir_set_owner(p, current);
22337 +       }
22338 +}
22339 +
22340 +void au_pin_hdir_release(struct au_pin *p)
22341 +{
22342 +       if (p->hdir) {
22343 +               au_pin_hdir_set_owner(p, p->task);
22344 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
22345 +       }
22346 +}
22347 +
22348 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
22349 +{
22350 +       if (pin && pin->parent)
22351 +               return au_h_dptr(pin->parent, pin->bindex);
22352 +       return NULL;
22353 +}
22354 +
22355 +void au_unpin(struct au_pin *p)
22356 +{
22357 +       if (p->hdir)
22358 +               au_pin_hdir_unlock(p);
22359 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
22360 +               vfsub_mnt_drop_write(p->h_mnt);
22361 +       if (!p->hdir)
22362 +               return;
22363 +
22364 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22365 +               di_read_unlock(p->parent, AuLock_IR);
22366 +       iput(p->hdir->hi_inode);
22367 +       dput(p->parent);
22368 +       p->parent = NULL;
22369 +       p->hdir = NULL;
22370 +       p->h_mnt = NULL;
22371 +       /* do not clear p->task */
22372 +}
22373 +
22374 +int au_do_pin(struct au_pin *p)
22375 +{
22376 +       int err;
22377 +       struct super_block *sb;
22378 +       struct inode *h_dir;
22379 +
22380 +       err = 0;
22381 +       sb = p->dentry->d_sb;
22382 +       p->br = au_sbr(sb, p->bindex);
22383 +       if (IS_ROOT(p->dentry)) {
22384 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
22385 +                       p->h_mnt = au_br_mnt(p->br);
22386 +                       err = vfsub_mnt_want_write(p->h_mnt);
22387 +                       if (unlikely(err)) {
22388 +                               au_fclr_pin(p->flags, MNT_WRITE);
22389 +                               goto out_err;
22390 +                       }
22391 +               }
22392 +               goto out;
22393 +       }
22394 +
22395 +       p->h_dentry = NULL;
22396 +       if (p->bindex <= au_dbbot(p->dentry))
22397 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
22398 +
22399 +       p->parent = dget_parent(p->dentry);
22400 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22401 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
22402 +
22403 +       h_dir = NULL;
22404 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
22405 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
22406 +       if (p->hdir)
22407 +               h_dir = p->hdir->hi_inode;
22408 +
22409 +       /*
22410 +        * udba case, or
22411 +        * if DI_LOCKED is not set, then p->parent may be different
22412 +        * and h_parent can be NULL.
22413 +        */
22414 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
22415 +               err = -EBUSY;
22416 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
22417 +                       di_read_unlock(p->parent, AuLock_IR);
22418 +               dput(p->parent);
22419 +               p->parent = NULL;
22420 +               goto out_err;
22421 +       }
22422 +
22423 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
22424 +               p->h_mnt = au_br_mnt(p->br);
22425 +               err = vfsub_mnt_want_write(p->h_mnt);
22426 +               if (unlikely(err)) {
22427 +                       au_fclr_pin(p->flags, MNT_WRITE);
22428 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
22429 +                               di_read_unlock(p->parent, AuLock_IR);
22430 +                       dput(p->parent);
22431 +                       p->parent = NULL;
22432 +                       goto out_err;
22433 +               }
22434 +       }
22435 +
22436 +       au_igrab(h_dir);
22437 +       err = au_pin_hdir_lock(p);
22438 +       if (!err)
22439 +               goto out; /* success */
22440 +
22441 +       au_unpin(p);
22442 +
22443 +out_err:
22444 +       pr_err("err %d\n", err);
22445 +       err = au_busy_or_stale();
22446 +out:
22447 +       return err;
22448 +}
22449 +
22450 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
22451 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22452 +                unsigned int udba, unsigned char flags)
22453 +{
22454 +       p->dentry = dentry;
22455 +       p->udba = udba;
22456 +       p->lsc_di = lsc_di;
22457 +       p->lsc_hi = lsc_hi;
22458 +       p->flags = flags;
22459 +       p->bindex = bindex;
22460 +
22461 +       p->parent = NULL;
22462 +       p->hdir = NULL;
22463 +       p->h_mnt = NULL;
22464 +
22465 +       p->h_dentry = NULL;
22466 +       p->h_parent = NULL;
22467 +       p->br = NULL;
22468 +       p->task = current;
22469 +}
22470 +
22471 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22472 +          unsigned int udba, unsigned char flags)
22473 +{
22474 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22475 +                   udba, flags);
22476 +       return au_do_pin(pin);
22477 +}
22478 +
22479 +/* ---------------------------------------------------------------------- */
22480 +
22481 +/*
22482 + * ->setattr() and ->getattr() are called in various cases.
22483 + * chmod, stat: dentry is revalidated.
22484 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22485 + *               unhashed.
22486 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
22487 + */
22488 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
22489 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
22490 +{
22491 +       int err;
22492 +       struct dentry *parent;
22493 +
22494 +       err = 0;
22495 +       if (au_digen_test(dentry, sigen)) {
22496 +               parent = dget_parent(dentry);
22497 +               di_read_lock_parent(parent, AuLock_IR);
22498 +               err = au_refresh_dentry(dentry, parent);
22499 +               di_read_unlock(parent, AuLock_IR);
22500 +               dput(parent);
22501 +       }
22502 +
22503 +       AuTraceErr(err);
22504 +       return err;
22505 +}
22506 +
22507 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22508 +                    struct au_icpup_args *a)
22509 +{
22510 +       int err;
22511 +       loff_t sz;
22512 +       aufs_bindex_t btop, ibtop;
22513 +       struct dentry *hi_wh, *parent;
22514 +       struct inode *inode;
22515 +       struct au_wr_dir_args wr_dir_args = {
22516 +               .force_btgt     = -1,
22517 +               .flags          = 0
22518 +       };
22519 +
22520 +       if (d_is_dir(dentry))
22521 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
22522 +       /* plink or hi_wh() case */
22523 +       btop = au_dbtop(dentry);
22524 +       inode = d_inode(dentry);
22525 +       ibtop = au_ibtop(inode);
22526 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22527 +               wr_dir_args.force_btgt = ibtop;
22528 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22529 +       if (unlikely(err < 0))
22530 +               goto out;
22531 +       a->btgt = err;
22532 +       if (err != btop)
22533 +               au_fset_icpup(a->flags, DID_CPUP);
22534 +
22535 +       err = 0;
22536 +       a->pin_flags = AuPin_MNT_WRITE;
22537 +       parent = NULL;
22538 +       if (!IS_ROOT(dentry)) {
22539 +               au_fset_pin(a->pin_flags, DI_LOCKED);
22540 +               parent = dget_parent(dentry);
22541 +               di_write_lock_parent(parent);
22542 +       }
22543 +
22544 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22545 +       if (unlikely(err))
22546 +               goto out_parent;
22547 +
22548 +       sz = -1;
22549 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22550 +       a->h_inode = d_inode(a->h_path.dentry);
22551 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
22552 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
22553 +               if (ia->ia_size < i_size_read(a->h_inode))
22554 +                       sz = ia->ia_size;
22555 +               inode_unlock_shared(a->h_inode);
22556 +       }
22557 +
22558 +       hi_wh = NULL;
22559 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
22560 +               hi_wh = au_hi_wh(inode, a->btgt);
22561 +               if (!hi_wh) {
22562 +                       struct au_cp_generic cpg = {
22563 +                               .dentry = dentry,
22564 +                               .bdst   = a->btgt,
22565 +                               .bsrc   = -1,
22566 +                               .len    = sz,
22567 +                               .pin    = &a->pin
22568 +                       };
22569 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
22570 +                       if (unlikely(err))
22571 +                               goto out_unlock;
22572 +                       hi_wh = au_hi_wh(inode, a->btgt);
22573 +                       /* todo: revalidate hi_wh? */
22574 +               }
22575 +       }
22576 +
22577 +       if (parent) {
22578 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22579 +               di_downgrade_lock(parent, AuLock_IR);
22580 +               dput(parent);
22581 +               parent = NULL;
22582 +       }
22583 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
22584 +               goto out; /* success */
22585 +
22586 +       if (!d_unhashed(dentry)) {
22587 +               struct au_cp_generic cpg = {
22588 +                       .dentry = dentry,
22589 +                       .bdst   = a->btgt,
22590 +                       .bsrc   = btop,
22591 +                       .len    = sz,
22592 +                       .pin    = &a->pin,
22593 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
22594 +               };
22595 +               err = au_sio_cpup_simple(&cpg);
22596 +               if (!err)
22597 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22598 +       } else if (!hi_wh)
22599 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22600 +       else
22601 +               a->h_path.dentry = hi_wh; /* do not dget here */
22602 +
22603 +out_unlock:
22604 +       a->h_inode = d_inode(a->h_path.dentry);
22605 +       if (!err)
22606 +               goto out; /* success */
22607 +       au_unpin(&a->pin);
22608 +out_parent:
22609 +       if (parent) {
22610 +               di_write_unlock(parent);
22611 +               dput(parent);
22612 +       }
22613 +out:
22614 +       if (!err)
22615 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22616 +       return err;
22617 +}
22618 +
22619 +static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
22620 +                       struct iattr *ia)
22621 +{
22622 +       int err;
22623 +       struct inode *inode, *delegated;
22624 +       struct super_block *sb;
22625 +       struct file *file;
22626 +       struct au_icpup_args *a;
22627 +       struct user_namespace *h_userns;
22628 +
22629 +       inode = d_inode(dentry);
22630 +       IMustLock(inode);
22631 +
22632 +       err = setattr_prepare(userns, dentry, ia);
22633 +       if (unlikely(err))
22634 +               goto out;
22635 +
22636 +       err = -ENOMEM;
22637 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22638 +       if (unlikely(!a))
22639 +               goto out;
22640 +
22641 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22642 +               ia->ia_valid &= ~ATTR_MODE;
22643 +
22644 +       file = NULL;
22645 +       sb = dentry->d_sb;
22646 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22647 +       if (unlikely(err))
22648 +               goto out_kfree;
22649 +
22650 +       if (ia->ia_valid & ATTR_FILE) {
22651 +               /* currently ftruncate(2) only */
22652 +               AuDebugOn(!d_is_reg(dentry));
22653 +               file = ia->ia_file;
22654 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22655 +                                           /*fi_lsc*/0);
22656 +               if (unlikely(err))
22657 +                       goto out_si;
22658 +               ia->ia_file = au_hf_top(file);
22659 +               a->udba = AuOpt_UDBA_NONE;
22660 +       } else {
22661 +               /* fchmod() doesn't pass ia_file */
22662 +               a->udba = au_opt_udba(sb);
22663 +               di_write_lock_child(dentry);
22664 +               /* no d_unlinked(), to set UDBA_NONE for root */
22665 +               if (d_unhashed(dentry))
22666 +                       a->udba = AuOpt_UDBA_NONE;
22667 +               if (a->udba != AuOpt_UDBA_NONE) {
22668 +                       AuDebugOn(IS_ROOT(dentry));
22669 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
22670 +                       if (unlikely(err))
22671 +                               goto out_dentry;
22672 +               }
22673 +       }
22674 +
22675 +       err = au_pin_and_icpup(dentry, ia, a);
22676 +       if (unlikely(err < 0))
22677 +               goto out_dentry;
22678 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
22679 +               ia->ia_file = NULL;
22680 +               ia->ia_valid &= ~ATTR_FILE;
22681 +       }
22682 +
22683 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22684 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22685 +           == (ATTR_MODE | ATTR_CTIME)) {
22686 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
22687 +               if (unlikely(err))
22688 +                       goto out_unlock;
22689 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22690 +                  && (ia->ia_valid & ATTR_CTIME)) {
22691 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
22692 +               if (unlikely(err))
22693 +                       goto out_unlock;
22694 +       }
22695 +
22696 +       if (ia->ia_valid & ATTR_SIZE) {
22697 +               struct file *f;
22698 +
22699 +               if (ia->ia_size < i_size_read(inode))
22700 +                       /* unmap only */
22701 +                       truncate_setsize(inode, ia->ia_size);
22702 +
22703 +               f = NULL;
22704 +               if (ia->ia_valid & ATTR_FILE)
22705 +                       f = ia->ia_file;
22706 +               inode_unlock(a->h_inode);
22707 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
22708 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22709 +       } else {
22710 +               delegated = NULL;
22711 +               while (1) {
22712 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
22713 +                       if (delegated) {
22714 +                               err = break_deleg_wait(&delegated);
22715 +                               if (!err)
22716 +                                       continue;
22717 +                       }
22718 +                       break;
22719 +               }
22720 +       }
22721 +       /*
22722 +        * regardless aufs 'acl' option setting.
22723 +        * why don't all acl-aware fs call this func from their ->setattr()?
22724 +        */
22725 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
22726 +               h_userns = mnt_user_ns(a->h_path.mnt);
22727 +               err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
22728 +       }
22729 +       if (!err)
22730 +               au_cpup_attr_changeable(inode);
22731 +
22732 +out_unlock:
22733 +       inode_unlock(a->h_inode);
22734 +       au_unpin(&a->pin);
22735 +       if (unlikely(err))
22736 +               au_update_dbtop(dentry);
22737 +out_dentry:
22738 +       di_write_unlock(dentry);
22739 +       if (file) {
22740 +               fi_write_unlock(file);
22741 +               ia->ia_file = file;
22742 +               ia->ia_valid |= ATTR_FILE;
22743 +       }
22744 +out_si:
22745 +       si_read_unlock(sb);
22746 +out_kfree:
22747 +       au_kfree_rcu(a);
22748 +out:
22749 +       AuTraceErr(err);
22750 +       return err;
22751 +}
22752 +
22753 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22754 +static int au_h_path_to_set_attr(struct dentry *dentry,
22755 +                                struct au_icpup_args *a, struct path *h_path)
22756 +{
22757 +       int err;
22758 +       struct super_block *sb;
22759 +
22760 +       sb = dentry->d_sb;
22761 +       a->udba = au_opt_udba(sb);
22762 +       /* no d_unlinked(), to set UDBA_NONE for root */
22763 +       if (d_unhashed(dentry))
22764 +               a->udba = AuOpt_UDBA_NONE;
22765 +       if (a->udba != AuOpt_UDBA_NONE) {
22766 +               AuDebugOn(IS_ROOT(dentry));
22767 +               err = au_reval_for_attr(dentry, au_sigen(sb));
22768 +               if (unlikely(err))
22769 +                       goto out;
22770 +       }
22771 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22772 +       if (unlikely(err < 0))
22773 +               goto out;
22774 +
22775 +       h_path->dentry = a->h_path.dentry;
22776 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
22777 +
22778 +out:
22779 +       return err;
22780 +}
22781 +
22782 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22783 +                 struct au_sxattr *arg)
22784 +{
22785 +       int err;
22786 +       struct path h_path;
22787 +       struct super_block *sb;
22788 +       struct au_icpup_args *a;
22789 +       struct inode *h_inode;
22790 +       struct user_namespace *h_userns;
22791 +
22792 +       IMustLock(inode);
22793 +
22794 +       err = -ENOMEM;
22795 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22796 +       if (unlikely(!a))
22797 +               goto out;
22798 +
22799 +       sb = dentry->d_sb;
22800 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22801 +       if (unlikely(err))
22802 +               goto out_kfree;
22803 +
22804 +       h_path.dentry = NULL;   /* silence gcc */
22805 +       di_write_lock_child(dentry);
22806 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
22807 +       if (unlikely(err))
22808 +               goto out_di;
22809 +       h_userns = mnt_user_ns(h_path.mnt);
22810 +
22811 +       inode_unlock(a->h_inode);
22812 +       switch (arg->type) {
22813 +       case AU_XATTR_SET:
22814 +               AuDebugOn(d_is_negative(h_path.dentry));
22815 +               err = vfsub_setxattr(h_userns, h_path.dentry,
22816 +                                    arg->u.set.name, arg->u.set.value,
22817 +                                    arg->u.set.size, arg->u.set.flags);
22818 +               break;
22819 +       case AU_ACL_SET:
22820 +               err = -EOPNOTSUPP;
22821 +               h_inode = d_inode(h_path.dentry);
22822 +               if (h_inode->i_op->set_acl) {
22823 +                       /* this will call posix_acl_update_mode */
22824 +                       err = h_inode->i_op->set_acl(h_userns, h_inode,
22825 +                                                    arg->u.acl_set.acl,
22826 +                                                    arg->u.acl_set.type);
22827 +               }
22828 +               break;
22829 +       }
22830 +       if (!err)
22831 +               au_cpup_attr_timesizes(inode);
22832 +
22833 +       au_unpin(&a->pin);
22834 +       if (unlikely(err))
22835 +               au_update_dbtop(dentry);
22836 +
22837 +out_di:
22838 +       di_write_unlock(dentry);
22839 +       si_read_unlock(sb);
22840 +out_kfree:
22841 +       au_kfree_rcu(a);
22842 +out:
22843 +       AuTraceErr(err);
22844 +       return err;
22845 +}
22846 +#endif
22847 +
22848 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22849 +                            unsigned int nlink)
22850 +{
22851 +       unsigned int n;
22852 +
22853 +       inode->i_mode = st->mode;
22854 +       /* don't i_[ug]id_write() here */
22855 +       inode->i_uid = st->uid;
22856 +       inode->i_gid = st->gid;
22857 +       inode->i_atime = st->atime;
22858 +       inode->i_mtime = st->mtime;
22859 +       inode->i_ctime = st->ctime;
22860 +
22861 +       au_cpup_attr_nlink(inode, /*force*/0);
22862 +       if (S_ISDIR(inode->i_mode)) {
22863 +               n = inode->i_nlink;
22864 +               n -= nlink;
22865 +               n += st->nlink;
22866 +               smp_mb(); /* for i_nlink */
22867 +               /* 0 can happen */
22868 +               set_nlink(inode, n);
22869 +       }
22870 +
22871 +       spin_lock(&inode->i_lock);
22872 +       inode->i_blocks = st->blocks;
22873 +       i_size_write(inode, st->size);
22874 +       spin_unlock(&inode->i_lock);
22875 +}
22876 +
22877 +/*
22878 + * common routine for aufs_getattr() and au_getxattr().
22879 + * returns zero or negative (an error).
22880 + * @dentry will be read-locked in success.
22881 + */
22882 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22883 +                     struct path *h_path, int locked)
22884 +{
22885 +       int err;
22886 +       unsigned int mnt_flags, sigen;
22887 +       unsigned char udba_none;
22888 +       aufs_bindex_t bindex;
22889 +       struct super_block *sb, *h_sb;
22890 +
22891 +       h_path->mnt = NULL;
22892 +       h_path->dentry = NULL;
22893 +
22894 +       err = 0;
22895 +       sb = dentry->d_sb;
22896 +       mnt_flags = au_mntflags(sb);
22897 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
22898 +
22899 +       if (unlikely(locked))
22900 +               goto body; /* skip locking dinfo */
22901 +
22902 +       /* support fstat(2) */
22903 +       if (!d_unlinked(dentry) && !udba_none) {
22904 +               sigen = au_sigen(sb);
22905 +               err = au_digen_test(dentry, sigen);
22906 +               if (!err) {
22907 +                       di_read_lock_child(dentry, AuLock_IR);
22908 +                       err = au_dbrange_test(dentry);
22909 +                       if (unlikely(err)) {
22910 +                               di_read_unlock(dentry, AuLock_IR);
22911 +                               goto out;
22912 +                       }
22913 +               } else {
22914 +                       AuDebugOn(IS_ROOT(dentry));
22915 +                       di_write_lock_child(dentry);
22916 +                       err = au_dbrange_test(dentry);
22917 +                       if (!err)
22918 +                               err = au_reval_for_attr(dentry, sigen);
22919 +                       if (!err)
22920 +                               di_downgrade_lock(dentry, AuLock_IR);
22921 +                       else {
22922 +                               di_write_unlock(dentry);
22923 +                               goto out;
22924 +                       }
22925 +               }
22926 +       } else
22927 +               di_read_lock_child(dentry, AuLock_IR);
22928 +
22929 +body:
22930 +       if (!inode) {
22931 +               inode = d_inode(dentry);
22932 +               if (unlikely(!inode))
22933 +                       goto out;
22934 +       }
22935 +       bindex = au_ibtop(inode);
22936 +       h_path->mnt = au_sbr_mnt(sb, bindex);
22937 +       h_sb = h_path->mnt->mnt_sb;
22938 +       if (!force
22939 +           && !au_test_fs_bad_iattr(h_sb)
22940 +           && udba_none)
22941 +               goto out; /* success */
22942 +
22943 +       if (au_dbtop(dentry) == bindex)
22944 +               h_path->dentry = au_h_dptr(dentry, bindex);
22945 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
22946 +               h_path->dentry = au_plink_lkup(inode, bindex);
22947 +               if (IS_ERR(h_path->dentry))
22948 +                       /* pretending success */
22949 +                       h_path->dentry = NULL;
22950 +               else
22951 +                       dput(h_path->dentry);
22952 +       }
22953 +
22954 +out:
22955 +       return err;
22956 +}
22957 +
22958 +static int aufs_getattr(struct user_namespace *userns, const struct path *path,
22959 +                       struct kstat *st, u32 request, unsigned int query)
22960 +{
22961 +       int err;
22962 +       unsigned char positive;
22963 +       struct path h_path;
22964 +       struct dentry *dentry;
22965 +       struct inode *inode;
22966 +       struct super_block *sb;
22967 +
22968 +       dentry = path->dentry;
22969 +       inode = d_inode(dentry);
22970 +       sb = dentry->d_sb;
22971 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22972 +       if (unlikely(err))
22973 +               goto out;
22974 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
22975 +                               /*locked*/0);
22976 +       if (unlikely(err))
22977 +               goto out_si;
22978 +       if (unlikely(!h_path.dentry))
22979 +               /* illegally overlapped or something */
22980 +               goto out_fill; /* pretending success */
22981 +
22982 +       positive = d_is_positive(h_path.dentry);
22983 +       if (positive)
22984 +               /* no vfsub version */
22985 +               err = vfs_getattr(&h_path, st, request, query);
22986 +       if (!err) {
22987 +               if (positive)
22988 +                       au_refresh_iattr(inode, st,
22989 +                                        d_inode(h_path.dentry)->i_nlink);
22990 +               goto out_fill; /* success */
22991 +       }
22992 +       AuTraceErr(err);
22993 +       goto out_di;
22994 +
22995 +out_fill:
22996 +       generic_fillattr(userns, inode, st);
22997 +out_di:
22998 +       di_read_unlock(dentry, AuLock_IR);
22999 +out_si:
23000 +       si_read_unlock(sb);
23001 +out:
23002 +       AuTraceErr(err);
23003 +       return err;
23004 +}
23005 +
23006 +/* ---------------------------------------------------------------------- */
23007 +
23008 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
23009 +                                struct delayed_call *done)
23010 +{
23011 +       const char *ret;
23012 +       struct dentry *h_dentry;
23013 +       struct inode *h_inode;
23014 +       int err;
23015 +       aufs_bindex_t bindex;
23016 +
23017 +       ret = NULL; /* suppress a warning */
23018 +       err = -ECHILD;
23019 +       if (!dentry)
23020 +               goto out;
23021 +
23022 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23023 +       if (unlikely(err))
23024 +               goto out;
23025 +
23026 +       err = au_d_hashed_positive(dentry);
23027 +       if (unlikely(err))
23028 +               goto out_unlock;
23029 +
23030 +       err = -EINVAL;
23031 +       inode = d_inode(dentry);
23032 +       bindex = au_ibtop(inode);
23033 +       h_inode = au_h_iptr(inode, bindex);
23034 +       if (unlikely(!h_inode->i_op->get_link))
23035 +               goto out_unlock;
23036 +
23037 +       err = -EBUSY;
23038 +       h_dentry = NULL;
23039 +       if (au_dbtop(dentry) <= bindex) {
23040 +               h_dentry = au_h_dptr(dentry, bindex);
23041 +               if (h_dentry)
23042 +                       dget(h_dentry);
23043 +       }
23044 +       if (!h_dentry) {
23045 +               h_dentry = d_find_any_alias(h_inode);
23046 +               if (IS_ERR(h_dentry)) {
23047 +                       err = PTR_ERR(h_dentry);
23048 +                       goto out_unlock;
23049 +               }
23050 +       }
23051 +       if (unlikely(!h_dentry))
23052 +               goto out_unlock;
23053 +
23054 +       err = 0;
23055 +       AuDbg("%ps\n", h_inode->i_op->get_link);
23056 +       AuDbgDentry(h_dentry);
23057 +       ret = vfs_get_link(h_dentry, done);
23058 +       dput(h_dentry);
23059 +       if (IS_ERR(ret))
23060 +               err = PTR_ERR(ret);
23061 +
23062 +out_unlock:
23063 +       aufs_read_unlock(dentry, AuLock_IR);
23064 +out:
23065 +       if (unlikely(err))
23066 +               ret = ERR_PTR(err);
23067 +       AuTraceErrPtr(ret);
23068 +       return ret;
23069 +}
23070 +
23071 +/* ---------------------------------------------------------------------- */
23072 +
23073 +static int au_is_special(struct inode *inode)
23074 +{
23075 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23076 +}
23077 +
23078 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
23079 +                           int flags)
23080 +{
23081 +       int err;
23082 +       aufs_bindex_t bindex;
23083 +       struct super_block *sb;
23084 +       struct inode *h_inode;
23085 +       struct vfsmount *h_mnt;
23086 +
23087 +       sb = inode->i_sb;
23088 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23089 +                 "unexpected s_flags 0x%lx", sb->s_flags);
23090 +
23091 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
23092 +       lockdep_off();
23093 +       si_read_lock(sb, AuLock_FLUSH);
23094 +       ii_write_lock_child(inode);
23095 +
23096 +       err = 0;
23097 +       bindex = au_ibtop(inode);
23098 +       h_inode = au_h_iptr(inode, bindex);
23099 +       if (!au_test_ro(sb, bindex, inode)) {
23100 +               h_mnt = au_sbr_mnt(sb, bindex);
23101 +               err = vfsub_mnt_want_write(h_mnt);
23102 +               if (!err) {
23103 +                       err = vfsub_update_time(h_inode, ts, flags);
23104 +                       vfsub_mnt_drop_write(h_mnt);
23105 +               }
23106 +       } else if (au_is_special(h_inode)) {
23107 +               /*
23108 +                * Never copy-up here.
23109 +                * These special files may already be opened and used for
23110 +                * communicating. If we copied it up, then the communication
23111 +                * would be corrupted.
23112 +                */
23113 +               AuWarn1("timestamps for i%lu are ignored "
23114 +                       "since it is on readonly branch (hi%lu).\n",
23115 +                       inode->i_ino, h_inode->i_ino);
23116 +       } else if (flags & ~S_ATIME) {
23117 +               err = -EIO;
23118 +               AuIOErr1("unexpected flags 0x%x\n", flags);
23119 +               AuDebugOn(1);
23120 +       }
23121 +
23122 +       if (!err)
23123 +               au_cpup_attr_timesizes(inode);
23124 +       ii_write_unlock(inode);
23125 +       si_read_unlock(sb);
23126 +       lockdep_on();
23127 +
23128 +       if (!err && (flags & S_VERSION))
23129 +               inode_inc_iversion(inode);
23130 +
23131 +       return err;
23132 +}
23133 +
23134 +/* ---------------------------------------------------------------------- */
23135 +
23136 +/* no getattr version will be set by module.c:aufs_init() */
23137 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23138 +       aufs_iop[] = {
23139 +       [AuIop_SYMLINK] = {
23140 +               .permission     = aufs_permission,
23141 +#ifdef CONFIG_FS_POSIX_ACL
23142 +               .get_acl        = aufs_get_acl,
23143 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
23144 +#endif
23145 +
23146 +               .setattr        = aufs_setattr,
23147 +               .getattr        = aufs_getattr,
23148 +
23149 +#ifdef CONFIG_AUFS_XATTR
23150 +               .listxattr      = aufs_listxattr,
23151 +#endif
23152 +
23153 +               .get_link       = aufs_get_link,
23154 +
23155 +               /* .update_time = aufs_update_time */
23156 +       },
23157 +       [AuIop_DIR] = {
23158 +               .create         = aufs_create,
23159 +               .lookup         = aufs_lookup,
23160 +               .link           = aufs_link,
23161 +               .unlink         = aufs_unlink,
23162 +               .symlink        = aufs_symlink,
23163 +               .mkdir          = aufs_mkdir,
23164 +               .rmdir          = aufs_rmdir,
23165 +               .mknod          = aufs_mknod,
23166 +               .rename         = aufs_rename,
23167 +
23168 +               .permission     = aufs_permission,
23169 +#ifdef CONFIG_FS_POSIX_ACL
23170 +               .get_acl        = aufs_get_acl,
23171 +               .set_acl        = aufs_set_acl,
23172 +#endif
23173 +
23174 +               .setattr        = aufs_setattr,
23175 +               .getattr        = aufs_getattr,
23176 +
23177 +#ifdef CONFIG_AUFS_XATTR
23178 +               .listxattr      = aufs_listxattr,
23179 +#endif
23180 +
23181 +               .update_time    = aufs_update_time,
23182 +               .atomic_open    = aufs_atomic_open,
23183 +               .tmpfile        = aufs_tmpfile
23184 +       },
23185 +       [AuIop_OTHER] = {
23186 +               .permission     = aufs_permission,
23187 +#ifdef CONFIG_FS_POSIX_ACL
23188 +               .get_acl        = aufs_get_acl,
23189 +               .set_acl        = aufs_set_acl,
23190 +#endif
23191 +
23192 +               .setattr        = aufs_setattr,
23193 +               .getattr        = aufs_getattr,
23194 +
23195 +#ifdef CONFIG_AUFS_XATTR
23196 +               .listxattr      = aufs_listxattr,
23197 +#endif
23198 +
23199 +               .update_time    = aufs_update_time
23200 +       }
23201 +};
23202 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
23203 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
23204 +++ linux/fs/aufs/i_op_del.c    2022-08-02 12:12:45.165641939 +0200
23205 @@ -0,0 +1,522 @@
23206 +// SPDX-License-Identifier: GPL-2.0
23207 +/*
23208 + * Copyright (C) 2005-2021 Junjiro R. Okajima
23209 + *
23210 + * This program, aufs is free software; you can redistribute it and/or modify
23211 + * it under the terms of the GNU General Public License as published by
23212 + * the Free Software Foundation; either version 2 of the License, or
23213 + * (at your option) any later version.
23214 + *
23215 + * This program is distributed in the hope that it will be useful,
23216 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23217 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23218 + * GNU General Public License for more details.
23219 + *
23220 + * You should have received a copy of the GNU General Public License
23221 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23222 + */
23223 +
23224 +/*
23225 + * inode operations (del entry)
23226 + */
23227 +
23228 +#include <linux/iversion.h>
23229 +#include "aufs.h"
23230 +
23231 +/*
23232 + * decide if a new whiteout for @dentry is necessary or not.
23233 + * when it is necessary, prepare the parent dir for the upper branch whose
23234 + * branch index is @bcpup for creation. the actual creation of the whiteout will
23235 + * be done by caller.
23236 + * return value:
23237 + * 0: wh is unnecessary
23238 + * plus: wh is necessary
23239 + * minus: error
23240 + */
23241 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
23242 +{
23243 +       int need_wh, err;
23244 +       aufs_bindex_t btop;
23245 +       struct super_block *sb;
23246 +
23247 +       sb = dentry->d_sb;
23248 +       btop = au_dbtop(dentry);
23249 +       if (*bcpup < 0) {
23250 +               *bcpup = btop;
23251 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
23252 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
23253 +                       *bcpup = err;
23254 +                       if (unlikely(err < 0))
23255 +                               goto out;
23256 +               }
23257 +       } else
23258 +               AuDebugOn(btop < *bcpup
23259 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
23260 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
23261 +
23262 +       if (*bcpup != btop) {
23263 +               err = au_cpup_dirs(dentry, *bcpup);
23264 +               if (unlikely(err))
23265 +                       goto out;
23266 +               need_wh = 1;
23267 +       } else {
23268 +               struct au_dinfo *dinfo, *tmp;
23269 +
23270 +               need_wh = -ENOMEM;
23271 +               dinfo = au_di(dentry);
23272 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23273 +               if (tmp) {
23274 +                       au_di_cp(tmp, dinfo);
23275 +                       au_di_swap(tmp, dinfo);
23276 +                       /* returns the number of positive dentries */
23277 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
23278 +                                                /* AuLkup_IGNORE_PERM */ 0);
23279 +                       au_di_swap(tmp, dinfo);
23280 +                       au_rw_write_unlock(&tmp->di_rwsem);
23281 +                       au_di_free(tmp);
23282 +               }
23283 +       }
23284 +       AuDbg("need_wh %d\n", need_wh);
23285 +       err = need_wh;
23286 +
23287 +out:
23288 +       return err;
23289 +}
23290 +
23291 +/*
23292 + * simple tests for the del-entry operations.
23293 + * following the checks in vfs, plus the parent-child relationship.
23294 + */
23295 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23296 +              struct dentry *h_parent, int isdir)
23297 +{
23298 +       int err;
23299 +       umode_t h_mode;
23300 +       struct dentry *h_dentry, *h_latest;
23301 +       struct inode *h_inode;
23302 +       struct path h_ppath;
23303 +       struct super_block *sb;
23304 +       struct au_branch *br;
23305 +       struct user_namespace *h_userns;
23306 +
23307 +       h_dentry = au_h_dptr(dentry, bindex);
23308 +       if (d_really_is_positive(dentry)) {
23309 +               err = -ENOENT;
23310 +               if (unlikely(d_is_negative(h_dentry)))
23311 +                       goto out;
23312 +               h_inode = d_inode(h_dentry);
23313 +               if (unlikely(!h_inode->i_nlink))
23314 +                       goto out;
23315 +
23316 +               h_mode = h_inode->i_mode;
23317 +               if (!isdir) {
23318 +                       err = -EISDIR;
23319 +                       if (unlikely(S_ISDIR(h_mode)))
23320 +                               goto out;
23321 +               } else if (unlikely(!S_ISDIR(h_mode))) {
23322 +                       err = -ENOTDIR;
23323 +                       goto out;
23324 +               }
23325 +       } else {
23326 +               /* rename(2) case */
23327 +               err = -EIO;
23328 +               if (unlikely(d_is_positive(h_dentry)))
23329 +                       goto out;
23330 +       }
23331 +
23332 +       err = -ENOENT;
23333 +       /* expected parent dir is locked */
23334 +       if (unlikely(h_parent != h_dentry->d_parent))
23335 +               goto out;
23336 +       err = 0;
23337 +
23338 +       /*
23339 +        * rmdir a dir may break the consistency on some filesystem.
23340 +        * let's try heavy test.
23341 +        */
23342 +       err = -EACCES;
23343 +       sb = dentry->d_sb;
23344 +       br = au_sbr(sb, bindex);
23345 +       h_userns = au_br_userns(br);
23346 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
23347 +                    && au_test_h_perm(h_userns, d_inode(h_parent),
23348 +                                      MAY_EXEC | MAY_WRITE)))
23349 +               goto out;
23350 +
23351 +       h_ppath.dentry = h_parent;
23352 +       h_ppath.mnt = au_br_mnt(br);
23353 +       h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
23354 +       err = -EIO;
23355 +       if (IS_ERR(h_latest))
23356 +               goto out;
23357 +       if (h_latest == h_dentry)
23358 +               err = 0;
23359 +       dput(h_latest);
23360 +
23361 +out:
23362 +       return err;
23363 +}
23364 +
23365 +/*
23366 + * decide the branch where we operate for @dentry. the branch index will be set
23367 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
23368 + * dir for reverting.
23369 + * when a new whiteout is necessary, create it.
23370 + */
23371 +static struct dentry*
23372 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23373 +                   struct au_dtime *dt, struct au_pin *pin)
23374 +{
23375 +       struct dentry *wh_dentry;
23376 +       struct super_block *sb;
23377 +       struct path h_path;
23378 +       int err, need_wh;
23379 +       unsigned int udba;
23380 +       aufs_bindex_t bcpup;
23381 +
23382 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23383 +       wh_dentry = ERR_PTR(need_wh);
23384 +       if (unlikely(need_wh < 0))
23385 +               goto out;
23386 +
23387 +       sb = dentry->d_sb;
23388 +       udba = au_opt_udba(sb);
23389 +       bcpup = *rbcpup;
23390 +       err = au_pin(pin, dentry, bcpup, udba,
23391 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23392 +       wh_dentry = ERR_PTR(err);
23393 +       if (unlikely(err))
23394 +               goto out;
23395 +
23396 +       h_path.dentry = au_pinned_h_parent(pin);
23397 +       if (udba != AuOpt_UDBA_NONE
23398 +           && au_dbtop(dentry) == bcpup) {
23399 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23400 +               wh_dentry = ERR_PTR(err);
23401 +               if (unlikely(err))
23402 +                       goto out_unpin;
23403 +       }
23404 +
23405 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
23406 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23407 +       wh_dentry = NULL;
23408 +       if (!need_wh)
23409 +               goto out; /* success, no need to create whiteout */
23410 +
23411 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23412 +       if (IS_ERR(wh_dentry))
23413 +               goto out_unpin;
23414 +
23415 +       /* returns with the parent is locked and wh_dentry is dget-ed */
23416 +       goto out; /* success */
23417 +
23418 +out_unpin:
23419 +       au_unpin(pin);
23420 +out:
23421 +       return wh_dentry;
23422 +}
23423 +
23424 +/*
23425 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
23426 + * in order to be revertible and save time for removing many child whiteouts
23427 + * under the dir.
23428 + * returns 1 when there are too many child whiteout and caller should remove
23429 + * them asynchronously. returns 0 when the number of children is enough small to
23430 + * remove now or the branch fs is a remote fs.
23431 + * otherwise return an error.
23432 + */
23433 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23434 +                          struct au_nhash *whlist, struct inode *dir)
23435 +{
23436 +       int rmdir_later, err, dirwh;
23437 +       struct dentry *h_dentry;
23438 +       struct super_block *sb;
23439 +       struct inode *inode;
23440 +
23441 +       sb = dentry->d_sb;
23442 +       SiMustAnyLock(sb);
23443 +       h_dentry = au_h_dptr(dentry, bindex);
23444 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23445 +       if (unlikely(err))
23446 +               goto out;
23447 +
23448 +       /* stop monitoring */
23449 +       inode = d_inode(dentry);
23450 +       au_hn_free(au_hi(inode, bindex));
23451 +
23452 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
23453 +               dirwh = au_sbi(sb)->si_dirwh;
23454 +               rmdir_later = (dirwh <= 1);
23455 +               if (!rmdir_later)
23456 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23457 +                                                             dirwh);
23458 +               if (rmdir_later)
23459 +                       return rmdir_later;
23460 +       }
23461 +
23462 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23463 +       if (unlikely(err)) {
23464 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23465 +                       h_dentry, bindex, err);
23466 +               err = 0;
23467 +       }
23468 +
23469 +out:
23470 +       AuTraceErr(err);
23471 +       return err;
23472 +}
23473 +
23474 +/*
23475 + * final procedure for deleting a entry.
23476 + * maintain dentry and iattr.
23477 + */
23478 +static void epilog(struct inode *dir, struct dentry *dentry,
23479 +                  aufs_bindex_t bindex)
23480 +{
23481 +       struct inode *inode;
23482 +
23483 +       inode = d_inode(dentry);
23484 +       d_drop(dentry);
23485 +       inode->i_ctime = dir->i_ctime;
23486 +
23487 +       au_dir_ts(dir, bindex);
23488 +       inode_inc_iversion(dir);
23489 +}
23490 +
23491 +/*
23492 + * when an error happened, remove the created whiteout and revert everything.
23493 + */
23494 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23495 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
23496 +                    struct dentry *dentry, struct au_dtime *dt)
23497 +{
23498 +       int rerr;
23499 +       struct path h_path = {
23500 +               .dentry = wh_dentry,
23501 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
23502 +       };
23503 +
23504 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
23505 +       if (!rerr) {
23506 +               au_set_dbwh(dentry, bwh);
23507 +               au_dtime_revert(dt);
23508 +               return 0;
23509 +       }
23510 +
23511 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
23512 +       return -EIO;
23513 +}
23514 +
23515 +/* ---------------------------------------------------------------------- */
23516 +
23517 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
23518 +{
23519 +       int err;
23520 +       aufs_bindex_t bwh, bindex, btop;
23521 +       struct inode *inode, *h_dir, *delegated;
23522 +       struct dentry *parent, *wh_dentry;
23523 +       /* to reduce stack size */
23524 +       struct {
23525 +               struct au_dtime dt;
23526 +               struct au_pin pin;
23527 +               struct path h_path;
23528 +       } *a;
23529 +
23530 +       IMustLock(dir);
23531 +
23532 +       err = -ENOMEM;
23533 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23534 +       if (unlikely(!a))
23535 +               goto out;
23536 +
23537 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23538 +       if (unlikely(err))
23539 +               goto out_free;
23540 +       err = au_d_hashed_positive(dentry);
23541 +       if (unlikely(err))
23542 +               goto out_unlock;
23543 +       inode = d_inode(dentry);
23544 +       IMustLock(inode);
23545 +       err = -EISDIR;
23546 +       if (unlikely(d_is_dir(dentry)))
23547 +               goto out_unlock; /* possible? */
23548 +
23549 +       btop = au_dbtop(dentry);
23550 +       bwh = au_dbwh(dentry);
23551 +       bindex = -1;
23552 +       parent = dentry->d_parent; /* dir inode is locked */
23553 +       di_write_lock_parent(parent);
23554 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23555 +                                       &a->pin);
23556 +       err = PTR_ERR(wh_dentry);
23557 +       if (IS_ERR(wh_dentry))
23558 +               goto out_parent;
23559 +
23560 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23561 +       a->h_path.dentry = au_h_dptr(dentry, btop);
23562 +       dget(a->h_path.dentry);
23563 +       if (bindex == btop) {
23564 +               h_dir = au_pinned_h_dir(&a->pin);
23565 +               delegated = NULL;
23566 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23567 +               if (unlikely(err == -EWOULDBLOCK)) {
23568 +                       pr_warn("cannot retry for NFSv4 delegation"
23569 +                               " for an internal unlink\n");
23570 +                       iput(delegated);
23571 +               }
23572 +       } else {
23573 +               /* dir inode is locked */
23574 +               h_dir = d_inode(wh_dentry->d_parent);
23575 +               IMustLock(h_dir);
23576 +               err = 0;
23577 +       }
23578 +
23579 +       if (!err) {
23580 +               vfsub_drop_nlink(inode);
23581 +               epilog(dir, dentry, bindex);
23582 +
23583 +               /* update target timestamps */
23584 +               if (bindex == btop) {
23585 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23586 +                       /*ignore*/
23587 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23588 +               } else
23589 +                       /* todo: this timestamp may be reverted later */
23590 +                       inode->i_ctime = h_dir->i_ctime;
23591 +               goto out_unpin; /* success */
23592 +       }
23593 +
23594 +       /* revert */
23595 +       if (wh_dentry) {
23596 +               int rerr;
23597 +
23598 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23599 +                                &a->dt);
23600 +               if (rerr)
23601 +                       err = rerr;
23602 +       }
23603 +
23604 +out_unpin:
23605 +       au_unpin(&a->pin);
23606 +       dput(wh_dentry);
23607 +       dput(a->h_path.dentry);
23608 +out_parent:
23609 +       di_write_unlock(parent);
23610 +out_unlock:
23611 +       aufs_read_unlock(dentry, AuLock_DW);
23612 +out_free:
23613 +       au_kfree_rcu(a);
23614 +out:
23615 +       return err;
23616 +}
23617 +
23618 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
23619 +{
23620 +       int err, rmdir_later;
23621 +       aufs_bindex_t bwh, bindex, btop;
23622 +       struct inode *inode;
23623 +       struct dentry *parent, *wh_dentry, *h_dentry;
23624 +       struct au_whtmp_rmdir *args;
23625 +       /* to reduce stack size */
23626 +       struct {
23627 +               struct au_dtime dt;
23628 +               struct au_pin pin;
23629 +       } *a;
23630 +
23631 +       IMustLock(dir);
23632 +
23633 +       err = -ENOMEM;
23634 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23635 +       if (unlikely(!a))
23636 +               goto out;
23637 +
23638 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23639 +       if (unlikely(err))
23640 +               goto out_free;
23641 +       err = au_alive_dir(dentry);
23642 +       if (unlikely(err))
23643 +               goto out_unlock;
23644 +       inode = d_inode(dentry);
23645 +       IMustLock(inode);
23646 +       err = -ENOTDIR;
23647 +       if (unlikely(!d_is_dir(dentry)))
23648 +               goto out_unlock; /* possible? */
23649 +
23650 +       err = -ENOMEM;
23651 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23652 +       if (unlikely(!args))
23653 +               goto out_unlock;
23654 +
23655 +       parent = dentry->d_parent; /* dir inode is locked */
23656 +       di_write_lock_parent(parent);
23657 +       err = au_test_empty(dentry, &args->whlist);
23658 +       if (unlikely(err))
23659 +               goto out_parent;
23660 +
23661 +       btop = au_dbtop(dentry);
23662 +       bwh = au_dbwh(dentry);
23663 +       bindex = -1;
23664 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23665 +                                       &a->pin);
23666 +       err = PTR_ERR(wh_dentry);
23667 +       if (IS_ERR(wh_dentry))
23668 +               goto out_parent;
23669 +
23670 +       h_dentry = au_h_dptr(dentry, btop);
23671 +       dget(h_dentry);
23672 +       rmdir_later = 0;
23673 +       if (bindex == btop) {
23674 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
23675 +               if (err > 0) {
23676 +                       rmdir_later = err;
23677 +                       err = 0;
23678 +               }
23679 +       } else {
23680 +               /* stop monitoring */
23681 +               au_hn_free(au_hi(inode, btop));
23682 +
23683 +               /* dir inode is locked */
23684 +               IMustLock(d_inode(wh_dentry->d_parent));
23685 +               err = 0;
23686 +       }
23687 +
23688 +       if (!err) {
23689 +               vfsub_dead_dir(inode);
23690 +               au_set_dbdiropq(dentry, -1);
23691 +               epilog(dir, dentry, bindex);
23692 +
23693 +               if (rmdir_later) {
23694 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
23695 +                       args = NULL;
23696 +               }
23697 +
23698 +               goto out_unpin; /* success */
23699 +       }
23700 +
23701 +       /* revert */
23702 +       AuLabel(revert);
23703 +       if (wh_dentry) {
23704 +               int rerr;
23705 +
23706 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23707 +                                &a->dt);
23708 +               if (rerr)
23709 +                       err = rerr;
23710 +       }
23711 +
23712 +out_unpin:
23713 +       au_unpin(&a->pin);
23714 +       dput(wh_dentry);
23715 +       dput(h_dentry);
23716 +out_parent:
23717 +       di_write_unlock(parent);
23718 +       if (args)
23719 +               au_whtmp_rmdir_free(args);
23720 +out_unlock:
23721 +       aufs_read_unlock(dentry, AuLock_DW);
23722 +out_free:
23723 +       au_kfree_rcu(a);
23724 +out:
23725 +       AuTraceErr(err);
23726 +       return err;
23727 +}
23728 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
23729 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
23730 +++ linux/fs/aufs/i_op_ren.c    2022-08-02 12:12:45.165641939 +0200
23731 @@ -0,0 +1,1257 @@
23732 +// SPDX-License-Identifier: GPL-2.0
23733 +/*
23734 + * Copyright (C) 2005-2021 Junjiro R. Okajima
23735 + *
23736 + * This program, aufs is free software; you can redistribute it and/or modify
23737 + * it under the terms of the GNU General Public License as published by
23738 + * the Free Software Foundation; either version 2 of the License, or
23739 + * (at your option) any later version.
23740 + *
23741 + * This program is distributed in the hope that it will be useful,
23742 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23743 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23744 + * GNU General Public License for more details.
23745 + *
23746 + * You should have received a copy of the GNU General Public License
23747 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23748 + */
23749 +
23750 +/*
23751 + * inode operation (rename entry)
23752 + * todo: this is crazy monster
23753 + */
23754 +
23755 +#include <linux/iversion.h>
23756 +#include "aufs.h"
23757 +
23758 +enum { AuSRC, AuDST, AuSrcDst };
23759 +enum { AuPARENT, AuCHILD, AuParentChild };
23760 +
23761 +#define AuRen_ISDIR_SRC                1
23762 +#define AuRen_ISDIR_DST                (1 << 1)
23763 +#define AuRen_ISSAMEDIR                (1 << 2)
23764 +#define AuRen_WHSRC            (1 << 3)
23765 +#define AuRen_WHDST            (1 << 4)
23766 +#define AuRen_MNT_WRITE                (1 << 5)
23767 +#define AuRen_DT_DSTDIR                (1 << 6)
23768 +#define AuRen_DIROPQ_SRC       (1 << 7)
23769 +#define AuRen_DIROPQ_DST       (1 << 8)
23770 +#define AuRen_DIRREN           (1 << 9)
23771 +#define AuRen_DROPPED_SRC      (1 << 10)
23772 +#define AuRen_DROPPED_DST      (1 << 11)
23773 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
23774 +#define au_fset_ren(flags, name) \
23775 +       do { (flags) |= AuRen_##name; } while (0)
23776 +#define au_fclr_ren(flags, name) \
23777 +       do { (flags) &= ~AuRen_##name; } while (0)
23778 +
23779 +#ifndef CONFIG_AUFS_DIRREN
23780 +#undef AuRen_DIRREN
23781 +#define AuRen_DIRREN           0
23782 +#endif
23783 +
23784 +struct au_ren_args {
23785 +       struct {
23786 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
23787 +                       *wh_dentry;
23788 +               struct inode *dir, *inode;
23789 +               struct au_hinode *hdir, *hinode;
23790 +               struct au_dtime dt[AuParentChild];
23791 +               aufs_bindex_t btop, bdiropq;
23792 +       } sd[AuSrcDst];
23793 +
23794 +#define src_dentry     sd[AuSRC].dentry
23795 +#define src_dir                sd[AuSRC].dir
23796 +#define src_inode      sd[AuSRC].inode
23797 +#define src_h_dentry   sd[AuSRC].h_dentry
23798 +#define src_parent     sd[AuSRC].parent
23799 +#define src_h_parent   sd[AuSRC].h_parent
23800 +#define src_wh_dentry  sd[AuSRC].wh_dentry
23801 +#define src_hdir       sd[AuSRC].hdir
23802 +#define src_hinode     sd[AuSRC].hinode
23803 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
23804 +#define src_dt         sd[AuSRC].dt
23805 +#define src_btop       sd[AuSRC].btop
23806 +#define src_bdiropq    sd[AuSRC].bdiropq
23807 +
23808 +#define dst_dentry     sd[AuDST].dentry
23809 +#define dst_dir                sd[AuDST].dir
23810 +#define dst_inode      sd[AuDST].inode
23811 +#define dst_h_dentry   sd[AuDST].h_dentry
23812 +#define dst_parent     sd[AuDST].parent
23813 +#define dst_h_parent   sd[AuDST].h_parent
23814 +#define dst_wh_dentry  sd[AuDST].wh_dentry
23815 +#define dst_hdir       sd[AuDST].hdir
23816 +#define dst_hinode     sd[AuDST].hinode
23817 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
23818 +#define dst_dt         sd[AuDST].dt
23819 +#define dst_btop       sd[AuDST].btop
23820 +#define dst_bdiropq    sd[AuDST].bdiropq
23821 +
23822 +       struct dentry *h_trap;
23823 +       struct au_branch *br;
23824 +       struct path h_path;
23825 +       struct au_nhash whlist;
23826 +       aufs_bindex_t btgt, src_bwh;
23827 +
23828 +       struct {
23829 +               unsigned short auren_flags;
23830 +               unsigned char flags;    /* syscall parameter */
23831 +               unsigned char exchange;
23832 +       } __packed;
23833 +
23834 +       struct au_whtmp_rmdir *thargs;
23835 +       struct dentry *h_dst;
23836 +       struct au_hinode *h_root;
23837 +};
23838 +
23839 +/* ---------------------------------------------------------------------- */
23840 +
23841 +/*
23842 + * functions for reverting.
23843 + * when an error happened in a single rename systemcall, we should revert
23844 + * everything as if nothing happened.
23845 + * we don't need to revert the copied-up/down the parent dir since they are
23846 + * harmless.
23847 + */
23848 +
23849 +#define RevertFailure(fmt, ...) do { \
23850 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23851 +               ##__VA_ARGS__, err, rerr); \
23852 +       err = -EIO; \
23853 +} while (0)
23854 +
23855 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
23856 +{
23857 +       int rerr;
23858 +       struct dentry *d;
23859 +#define src_or_dst(member) a->sd[idx].member
23860 +
23861 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
23862 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23863 +       rerr = au_diropq_remove(d, a->btgt);
23864 +       au_hn_inode_unlock(src_or_dst(hinode));
23865 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
23866 +       if (rerr)
23867 +               RevertFailure("remove diropq %pd", d);
23868 +
23869 +#undef src_or_dst_
23870 +}
23871 +
23872 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23873 +{
23874 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23875 +               au_ren_do_rev_diropq(err, a, AuSRC);
23876 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23877 +               au_ren_do_rev_diropq(err, a, AuDST);
23878 +}
23879 +
23880 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
23881 +{
23882 +       int rerr;
23883 +       struct inode *delegated;
23884 +       struct path h_ppath = {
23885 +               .dentry = a->src_h_parent,
23886 +               .mnt    = a->h_path.mnt
23887 +       };
23888 +
23889 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
23890 +       rerr = PTR_ERR(a->h_path.dentry);
23891 +       if (IS_ERR(a->h_path.dentry)) {
23892 +               RevertFailure("lkup one %pd", a->src_dentry);
23893 +               return;
23894 +       }
23895 +
23896 +       delegated = NULL;
23897 +       rerr = vfsub_rename(a->dst_h_dir,
23898 +                           au_h_dptr(a->src_dentry, a->btgt),
23899 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
23900 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23901 +               pr_warn("cannot retry for NFSv4 delegation"
23902 +                       " for an internal rename\n");
23903 +               iput(delegated);
23904 +       }
23905 +       d_drop(a->h_path.dentry);
23906 +       dput(a->h_path.dentry);
23907 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23908 +       if (rerr)
23909 +               RevertFailure("rename %pd", a->src_dentry);
23910 +}
23911 +
23912 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
23913 +{
23914 +       int rerr;
23915 +       struct inode *delegated;
23916 +       struct path h_ppath = {
23917 +               .dentry = a->dst_h_parent,
23918 +               .mnt    = a->h_path.mnt
23919 +       };
23920 +
23921 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
23922 +       rerr = PTR_ERR(a->h_path.dentry);
23923 +       if (IS_ERR(a->h_path.dentry)) {
23924 +               RevertFailure("lkup one %pd", a->dst_dentry);
23925 +               return;
23926 +       }
23927 +       if (d_is_positive(a->h_path.dentry)) {
23928 +               d_drop(a->h_path.dentry);
23929 +               dput(a->h_path.dentry);
23930 +               return;
23931 +       }
23932 +
23933 +       delegated = NULL;
23934 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
23935 +                           &delegated, a->flags);
23936 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23937 +               pr_warn("cannot retry for NFSv4 delegation"
23938 +                       " for an internal rename\n");
23939 +               iput(delegated);
23940 +       }
23941 +       d_drop(a->h_path.dentry);
23942 +       dput(a->h_path.dentry);
23943 +       if (!rerr)
23944 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
23945 +       else
23946 +               RevertFailure("rename %pd", a->h_dst);
23947 +}
23948 +
23949 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
23950 +{
23951 +       int rerr;
23952 +
23953 +       a->h_path.dentry = a->src_wh_dentry;
23954 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
23955 +       au_set_dbwh(a->src_dentry, a->src_bwh);
23956 +       if (rerr)
23957 +               RevertFailure("unlink %pd", a->src_wh_dentry);
23958 +}
23959 +#undef RevertFailure
23960 +
23961 +/* ---------------------------------------------------------------------- */
23962 +
23963 +/*
23964 + * when we have to copyup the renaming entry, do it with the rename-target name
23965 + * in order to minimize the cost (the later actual rename is unnecessary).
23966 + * otherwise rename it on the target branch.
23967 + */
23968 +static int au_ren_or_cpup(struct au_ren_args *a)
23969 +{
23970 +       int err;
23971 +       struct dentry *d;
23972 +       struct inode *delegated;
23973 +
23974 +       d = a->src_dentry;
23975 +       if (au_dbtop(d) == a->btgt) {
23976 +               a->h_path.dentry = a->dst_h_dentry;
23977 +               AuDebugOn(au_dbtop(d) != a->btgt);
23978 +               delegated = NULL;
23979 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
23980 +                                  a->dst_h_dir, &a->h_path, &delegated,
23981 +                                  a->flags);
23982 +               if (unlikely(err == -EWOULDBLOCK)) {
23983 +                       pr_warn("cannot retry for NFSv4 delegation"
23984 +                               " for an internal rename\n");
23985 +                       iput(delegated);
23986 +               }
23987 +       } else
23988 +               BUG();
23989 +
23990 +       if (!err && a->h_dst)
23991 +               /* it will be set to dinfo later */
23992 +               dget(a->h_dst);
23993 +
23994 +       return err;
23995 +}
23996 +
23997 +/* cf. aufs_rmdir() */
23998 +static int au_ren_del_whtmp(struct au_ren_args *a)
23999 +{
24000 +       int err;
24001 +       struct inode *dir;
24002 +
24003 +       dir = a->dst_dir;
24004 +       SiMustAnyLock(dir->i_sb);
24005 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
24006 +                                    au_sbi(dir->i_sb)->si_dirwh)
24007 +           || au_test_fs_remote(a->h_dst->d_sb)) {
24008 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
24009 +               if (unlikely(err))
24010 +                       pr_warn("failed removing whtmp dir %pd (%d), "
24011 +                               "ignored.\n", a->h_dst, err);
24012 +       } else {
24013 +               au_nhash_wh_free(&a->thargs->whlist);
24014 +               a->thargs->whlist = a->whlist;
24015 +               a->whlist.nh_num = 0;
24016 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24017 +               dput(a->h_dst);
24018 +               a->thargs = NULL;
24019 +       }
24020 +
24021 +       return 0;
24022 +}
24023 +
24024 +/* make it 'opaque' dir. */
24025 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
24026 +{
24027 +       int err;
24028 +       struct dentry *d, *diropq;
24029 +#define src_or_dst(member) a->sd[idx].member
24030 +
24031 +       err = 0;
24032 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
24033 +       src_or_dst(bdiropq) = au_dbdiropq(d);
24034 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24035 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24036 +       diropq = au_diropq_create(d, a->btgt);
24037 +       au_hn_inode_unlock(src_or_dst(hinode));
24038 +       if (IS_ERR(diropq))
24039 +               err = PTR_ERR(diropq);
24040 +       else
24041 +               dput(diropq);
24042 +
24043 +#undef src_or_dst_
24044 +       return err;
24045 +}
24046 +
24047 +static int au_ren_diropq(struct au_ren_args *a)
24048 +{
24049 +       int err;
24050 +       unsigned char always;
24051 +       struct dentry *d;
24052 +
24053 +       err = 0;
24054 +       d = a->dst_dentry; /* already renamed on the branch */
24055 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24056 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24057 +           && !au_ftest_ren(a->auren_flags, DIRREN)
24058 +           && a->btgt != au_dbdiropq(a->src_dentry)
24059 +           && (a->dst_wh_dentry
24060 +               || a->btgt <= au_dbdiropq(d)
24061 +               /* hide the lower to keep xino */
24062 +               /* the lowers may not be a dir, but we hide them anyway */
24063 +               || a->btgt < au_dbbot(d)
24064 +               || always)) {
24065 +               AuDbg("here\n");
24066 +               err = au_ren_do_diropq(a, AuSRC);
24067 +               if (unlikely(err))
24068 +                       goto out;
24069 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
24070 +       }
24071 +       if (!a->exchange)
24072 +               goto out; /* success */
24073 +
24074 +       d = a->src_dentry; /* already renamed on the branch */
24075 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24076 +           && a->btgt != au_dbdiropq(a->dst_dentry)
24077 +           && (a->btgt < au_dbdiropq(d)
24078 +               || a->btgt < au_dbbot(d)
24079 +               || always)) {
24080 +               AuDbgDentry(a->src_dentry);
24081 +               AuDbgDentry(a->dst_dentry);
24082 +               err = au_ren_do_diropq(a, AuDST);
24083 +               if (unlikely(err))
24084 +                       goto out_rev_src;
24085 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
24086 +       }
24087 +       goto out; /* success */
24088 +
24089 +out_rev_src:
24090 +       AuDbg("err %d, reverting src\n", err);
24091 +       au_ren_rev_diropq(err, a);
24092 +out:
24093 +       return err;
24094 +}
24095 +
24096 +static int do_rename(struct au_ren_args *a)
24097 +{
24098 +       int err;
24099 +       struct dentry *d, *h_d;
24100 +
24101 +       if (!a->exchange) {
24102 +               /* prepare workqueue args for asynchronous rmdir */
24103 +               h_d = a->dst_h_dentry;
24104 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24105 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
24106 +                   && d_is_positive(h_d)) {
24107 +                       err = -ENOMEM;
24108 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24109 +                                                        GFP_NOFS);
24110 +                       if (unlikely(!a->thargs))
24111 +                               goto out;
24112 +                       a->h_dst = dget(h_d);
24113 +               }
24114 +
24115 +               /* create whiteout for src_dentry */
24116 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
24117 +                       a->src_bwh = au_dbwh(a->src_dentry);
24118 +                       AuDebugOn(a->src_bwh >= 0);
24119 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24120 +                                                       a->src_h_parent);
24121 +                       err = PTR_ERR(a->src_wh_dentry);
24122 +                       if (IS_ERR(a->src_wh_dentry))
24123 +                               goto out_thargs;
24124 +               }
24125 +
24126 +               /* lookup whiteout for dentry */
24127 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
24128 +                       h_d = au_wh_lkup(a->dst_h_parent,
24129 +                                        &a->dst_dentry->d_name, a->br);
24130 +                       err = PTR_ERR(h_d);
24131 +                       if (IS_ERR(h_d))
24132 +                               goto out_whsrc;
24133 +                       if (d_is_negative(h_d))
24134 +                               dput(h_d);
24135 +                       else
24136 +                               a->dst_wh_dentry = h_d;
24137 +               }
24138 +
24139 +               /* rename dentry to tmpwh */
24140 +               if (a->thargs) {
24141 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
24142 +                       if (unlikely(err))
24143 +                               goto out_whdst;
24144 +
24145 +                       d = a->dst_dentry;
24146 +                       au_set_h_dptr(d, a->btgt, NULL);
24147 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
24148 +                       if (unlikely(err))
24149 +                               goto out_whtmp;
24150 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
24151 +               }
24152 +       }
24153 +
24154 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
24155 +#if 0 /* debugging */
24156 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24157 +              && d_is_positive(a->dst_h_dentry)
24158 +              && a->src_btop != a->btgt);
24159 +#endif
24160 +
24161 +       /* rename by vfs_rename or cpup */
24162 +       err = au_ren_or_cpup(a);
24163 +       if (unlikely(err))
24164 +               /* leave the copied-up one */
24165 +               goto out_whtmp;
24166 +
24167 +       /* make dir opaque */
24168 +       err = au_ren_diropq(a);
24169 +       if (unlikely(err))
24170 +               goto out_rename;
24171 +
24172 +       /* update target timestamps */
24173 +       if (a->exchange) {
24174 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24175 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24176 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24177 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24178 +       }
24179 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
24180 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24181 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24182 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24183 +
24184 +       if (!a->exchange) {
24185 +               /* remove whiteout for dentry */
24186 +               if (a->dst_wh_dentry) {
24187 +                       a->h_path.dentry = a->dst_wh_dentry;
24188 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24189 +                                                 a->dst_dentry);
24190 +                       if (unlikely(err))
24191 +                               goto out_diropq;
24192 +               }
24193 +
24194 +               /* remove whtmp */
24195 +               if (a->thargs)
24196 +                       au_ren_del_whtmp(a); /* ignore this error */
24197 +
24198 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24199 +       }
24200 +       err = 0;
24201 +       goto out_success;
24202 +
24203 +out_diropq:
24204 +       au_ren_rev_diropq(err, a);
24205 +out_rename:
24206 +       au_ren_rev_rename(err, a);
24207 +       dput(a->h_dst);
24208 +out_whtmp:
24209 +       if (a->thargs)
24210 +               au_ren_rev_whtmp(err, a);
24211 +out_whdst:
24212 +       dput(a->dst_wh_dentry);
24213 +       a->dst_wh_dentry = NULL;
24214 +out_whsrc:
24215 +       if (a->src_wh_dentry)
24216 +               au_ren_rev_whsrc(err, a);
24217 +out_success:
24218 +       dput(a->src_wh_dentry);
24219 +       dput(a->dst_wh_dentry);
24220 +out_thargs:
24221 +       if (a->thargs) {
24222 +               dput(a->h_dst);
24223 +               au_whtmp_rmdir_free(a->thargs);
24224 +               a->thargs = NULL;
24225 +       }
24226 +out:
24227 +       return err;
24228 +}
24229 +
24230 +/* ---------------------------------------------------------------------- */
24231 +
24232 +/*
24233 + * test if @dentry dir can be rename destination or not.
24234 + * success means, it is a logically empty dir.
24235 + */
24236 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
24237 +{
24238 +       return au_test_empty(dentry, whlist);
24239 +}
24240 +
24241 +/*
24242 + * test if @a->src_dentry dir can be rename source or not.
24243 + * if it can, return 0.
24244 + * success means,
24245 + * - it is a logically empty dir.
24246 + * - or, it exists on writable branch and has no children including whiteouts
24247 + *   on the lower branch unless DIRREN is on.
24248 + */
24249 +static int may_rename_srcdir(struct au_ren_args *a)
24250 +{
24251 +       int err;
24252 +       unsigned int rdhash;
24253 +       aufs_bindex_t btop, btgt;
24254 +       struct dentry *dentry;
24255 +       struct super_block *sb;
24256 +       struct au_sbinfo *sbinfo;
24257 +
24258 +       dentry = a->src_dentry;
24259 +       sb = dentry->d_sb;
24260 +       sbinfo = au_sbi(sb);
24261 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24262 +               au_fset_ren(a->auren_flags, DIRREN);
24263 +
24264 +       btgt = a->btgt;
24265 +       btop = au_dbtop(dentry);
24266 +       if (btop != btgt) {
24267 +               struct au_nhash whlist;
24268 +
24269 +               SiMustAnyLock(sb);
24270 +               rdhash = sbinfo->si_rdhash;
24271 +               if (!rdhash)
24272 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24273 +                                                          dentry));
24274 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24275 +               if (unlikely(err))
24276 +                       goto out;
24277 +               err = au_test_empty(dentry, &whlist);
24278 +               au_nhash_wh_free(&whlist);
24279 +               goto out;
24280 +       }
24281 +
24282 +       if (btop == au_dbtaildir(dentry))
24283 +               return 0; /* success */
24284 +
24285 +       err = au_test_empty_lower(dentry);
24286 +
24287 +out:
24288 +       if (err == -ENOTEMPTY) {
24289 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
24290 +                       err = 0;
24291 +               } else {
24292 +                       AuWarn1("renaming dir who has child(ren) on multiple "
24293 +                               "branches, is not supported\n");
24294 +                       err = -EXDEV;
24295 +               }
24296 +       }
24297 +       return err;
24298 +}
24299 +
24300 +/* side effect: sets whlist and h_dentry */
24301 +static int au_ren_may_dir(struct au_ren_args *a)
24302 +{
24303 +       int err;
24304 +       unsigned int rdhash;
24305 +       struct dentry *d;
24306 +
24307 +       d = a->dst_dentry;
24308 +       SiMustAnyLock(d->d_sb);
24309 +
24310 +       err = 0;
24311 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
24312 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
24313 +               if (!rdhash)
24314 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24315 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24316 +               if (unlikely(err))
24317 +                       goto out;
24318 +
24319 +               if (!a->exchange) {
24320 +                       au_set_dbtop(d, a->dst_btop);
24321 +                       err = may_rename_dstdir(d, &a->whlist);
24322 +                       au_set_dbtop(d, a->btgt);
24323 +               } else
24324 +                       err = may_rename_srcdir(a);
24325 +       }
24326 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
24327 +       if (unlikely(err))
24328 +               goto out;
24329 +
24330 +       d = a->src_dentry;
24331 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
24332 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24333 +               err = may_rename_srcdir(a);
24334 +               if (unlikely(err)) {
24335 +                       au_nhash_wh_free(&a->whlist);
24336 +                       a->whlist.nh_num = 0;
24337 +               }
24338 +       }
24339 +out:
24340 +       return err;
24341 +}
24342 +
24343 +/* ---------------------------------------------------------------------- */
24344 +
24345 +/*
24346 + * simple tests for rename.
24347 + * following the checks in vfs, plus the parent-child relationship.
24348 + */
24349 +static int au_may_ren(struct au_ren_args *a)
24350 +{
24351 +       int err, isdir;
24352 +       struct inode *h_inode;
24353 +
24354 +       if (a->src_btop == a->btgt) {
24355 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
24356 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
24357 +               if (unlikely(err))
24358 +                       goto out;
24359 +               err = -EINVAL;
24360 +               if (unlikely(a->src_h_dentry == a->h_trap))
24361 +                       goto out;
24362 +       }
24363 +
24364 +       err = 0;
24365 +       if (a->dst_btop != a->btgt)
24366 +               goto out;
24367 +
24368 +       err = -ENOTEMPTY;
24369 +       if (unlikely(a->dst_h_dentry == a->h_trap))
24370 +               goto out;
24371 +
24372 +       err = -EIO;
24373 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
24374 +       if (d_really_is_negative(a->dst_dentry)) {
24375 +               if (d_is_negative(a->dst_h_dentry))
24376 +                       err = au_may_add(a->dst_dentry, a->btgt,
24377 +                                        a->dst_h_parent, isdir);
24378 +       } else {
24379 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
24380 +                       goto out;
24381 +               h_inode = d_inode(a->dst_h_dentry);
24382 +               if (h_inode->i_nlink)
24383 +                       err = au_may_del(a->dst_dentry, a->btgt,
24384 +                                        a->dst_h_parent, isdir);
24385 +       }
24386 +
24387 +out:
24388 +       if (unlikely(err == -ENOENT || err == -EEXIST))
24389 +               err = -EIO;
24390 +       AuTraceErr(err);
24391 +       return err;
24392 +}
24393 +
24394 +/* ---------------------------------------------------------------------- */
24395 +
24396 +/*
24397 + * locking order
24398 + * (VFS)
24399 + * - src_dir and dir by lock_rename()
24400 + * - inode if exists
24401 + * (aufs)
24402 + * - lock all
24403 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24404 + *     + si_read_lock
24405 + *     + di_write_lock2_child()
24406 + *       + di_write_lock_child()
24407 + *        + ii_write_lock_child()
24408 + *       + di_write_lock_child2()
24409 + *        + ii_write_lock_child2()
24410 + *     + src_parent and parent
24411 + *       + di_write_lock_parent()
24412 + *        + ii_write_lock_parent()
24413 + *       + di_write_lock_parent2()
24414 + *        + ii_write_lock_parent2()
24415 + *   + lower src_dir and dir by vfsub_lock_rename()
24416 + *   + verify the every relationships between child and parent. if any
24417 + *     of them failed, unlock all and return -EBUSY.
24418 + */
24419 +static void au_ren_unlock(struct au_ren_args *a)
24420 +{
24421 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24422 +                           a->dst_h_parent, a->dst_hdir);
24423 +       if (au_ftest_ren(a->auren_flags, DIRREN)
24424 +           && a->h_root)
24425 +               au_hn_inode_unlock(a->h_root);
24426 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
24427 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
24428 +}
24429 +
24430 +static int au_ren_lock(struct au_ren_args *a)
24431 +{
24432 +       int err;
24433 +       unsigned int udba;
24434 +
24435 +       err = 0;
24436 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24437 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
24438 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24439 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
24440 +
24441 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
24442 +       if (unlikely(err))
24443 +               goto out;
24444 +       au_fset_ren(a->auren_flags, MNT_WRITE);
24445 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24446 +               struct dentry *root;
24447 +               struct inode *dir;
24448 +
24449 +               /*
24450 +                * sbinfo is already locked, so this ii_read_lock is
24451 +                * unnecessary. but our debugging feature checks it.
24452 +                */
24453 +               root = a->src_inode->i_sb->s_root;
24454 +               if (root != a->src_parent && root != a->dst_parent) {
24455 +                       dir = d_inode(root);
24456 +                       ii_read_lock_parent3(dir);
24457 +                       a->h_root = au_hi(dir, a->btgt);
24458 +                       ii_read_unlock(dir);
24459 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24460 +               }
24461 +       }
24462 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24463 +                                     a->dst_h_parent, a->dst_hdir);
24464 +       udba = au_opt_udba(a->src_dentry->d_sb);
24465 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24466 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
24467 +               err = au_busy_or_stale();
24468 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
24469 +               err = au_h_verify(a->src_h_dentry, udba,
24470 +                                 d_inode(a->src_h_parent), a->src_h_parent,
24471 +                                 a->br);
24472 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
24473 +               err = au_h_verify(a->dst_h_dentry, udba,
24474 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
24475 +                                 a->br);
24476 +       if (!err)
24477 +               goto out; /* success */
24478 +
24479 +       err = au_busy_or_stale();
24480 +       au_ren_unlock(a);
24481 +
24482 +out:
24483 +       return err;
24484 +}
24485 +
24486 +/* ---------------------------------------------------------------------- */
24487 +
24488 +static void au_ren_refresh_dir(struct au_ren_args *a)
24489 +{
24490 +       struct inode *dir;
24491 +
24492 +       dir = a->dst_dir;
24493 +       inode_inc_iversion(dir);
24494 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24495 +               /* is this updating defined in POSIX? */
24496 +               au_cpup_attr_timesizes(a->src_inode);
24497 +               au_cpup_attr_nlink(dir, /*force*/1);
24498 +       }
24499 +       au_dir_ts(dir, a->btgt);
24500 +
24501 +       if (a->exchange) {
24502 +               dir = a->src_dir;
24503 +               inode_inc_iversion(dir);
24504 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24505 +                       /* is this updating defined in POSIX? */
24506 +                       au_cpup_attr_timesizes(a->dst_inode);
24507 +                       au_cpup_attr_nlink(dir, /*force*/1);
24508 +               }
24509 +               au_dir_ts(dir, a->btgt);
24510 +       }
24511 +
24512 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24513 +               return;
24514 +
24515 +       dir = a->src_dir;
24516 +       inode_inc_iversion(dir);
24517 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
24518 +               au_cpup_attr_nlink(dir, /*force*/1);
24519 +       au_dir_ts(dir, a->btgt);
24520 +}
24521 +
24522 +static void au_ren_refresh(struct au_ren_args *a)
24523 +{
24524 +       aufs_bindex_t bbot, bindex;
24525 +       struct dentry *d, *h_d;
24526 +       struct inode *i, *h_i;
24527 +       struct super_block *sb;
24528 +
24529 +       d = a->dst_dentry;
24530 +       d_drop(d);
24531 +       if (a->h_dst)
24532 +               /* already dget-ed by au_ren_or_cpup() */
24533 +               au_set_h_dptr(d, a->btgt, a->h_dst);
24534 +
24535 +       i = a->dst_inode;
24536 +       if (i) {
24537 +               if (!a->exchange) {
24538 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24539 +                               vfsub_drop_nlink(i);
24540 +                       else {
24541 +                               vfsub_dead_dir(i);
24542 +                               au_cpup_attr_timesizes(i);
24543 +                       }
24544 +                       au_update_dbrange(d, /*do_put_zero*/1);
24545 +               } else
24546 +                       au_cpup_attr_nlink(i, /*force*/1);
24547 +       } else {
24548 +               bbot = a->btgt;
24549 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
24550 +                       au_set_h_dptr(d, bindex, NULL);
24551 +               bbot = au_dbbot(d);
24552 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
24553 +                       au_set_h_dptr(d, bindex, NULL);
24554 +               au_update_dbrange(d, /*do_put_zero*/0);
24555 +       }
24556 +
24557 +       if (a->exchange
24558 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
24559 +               d_drop(a->src_dentry);
24560 +               if (au_ftest_ren(a->auren_flags, DIRREN))
24561 +                       au_set_dbwh(a->src_dentry, -1);
24562 +               return;
24563 +       }
24564 +
24565 +       d = a->src_dentry;
24566 +       au_set_dbwh(d, -1);
24567 +       bbot = au_dbbot(d);
24568 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24569 +               h_d = au_h_dptr(d, bindex);
24570 +               if (h_d)
24571 +                       au_set_h_dptr(d, bindex, NULL);
24572 +       }
24573 +       au_set_dbbot(d, a->btgt);
24574 +
24575 +       sb = d->d_sb;
24576 +       i = a->src_inode;
24577 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24578 +               return; /* success */
24579 +
24580 +       bbot = au_ibbot(i);
24581 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24582 +               h_i = au_h_iptr(i, bindex);
24583 +               if (h_i) {
24584 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24585 +                       /* ignore this error */
24586 +                       au_set_h_iptr(i, bindex, NULL, 0);
24587 +               }
24588 +       }
24589 +       au_set_ibbot(i, a->btgt);
24590 +}
24591 +
24592 +/* ---------------------------------------------------------------------- */
24593 +
24594 +/* mainly for link(2) and rename(2) */
24595 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
24596 +{
24597 +       aufs_bindex_t bdiropq, bwh;
24598 +       struct dentry *parent;
24599 +       struct au_branch *br;
24600 +
24601 +       parent = dentry->d_parent;
24602 +       IMustLock(d_inode(parent)); /* dir is locked */
24603 +
24604 +       bdiropq = au_dbdiropq(parent);
24605 +       bwh = au_dbwh(dentry);
24606 +       br = au_sbr(dentry->d_sb, btgt);
24607 +       if (au_br_rdonly(br)
24608 +           || (0 <= bdiropq && bdiropq < btgt)
24609 +           || (0 <= bwh && bwh < btgt))
24610 +               btgt = -1;
24611 +
24612 +       AuDbg("btgt %d\n", btgt);
24613 +       return btgt;
24614 +}
24615 +
24616 +/* sets src_btop, dst_btop and btgt */
24617 +static int au_ren_wbr(struct au_ren_args *a)
24618 +{
24619 +       int err;
24620 +       struct au_wr_dir_args wr_dir_args = {
24621 +               /* .force_btgt  = -1, */
24622 +               .flags          = AuWrDir_ADD_ENTRY
24623 +       };
24624 +
24625 +       a->src_btop = au_dbtop(a->src_dentry);
24626 +       a->dst_btop = au_dbtop(a->dst_dentry);
24627 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24628 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
24629 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
24630 +       wr_dir_args.force_btgt = a->src_btop;
24631 +       if (a->dst_inode && a->dst_btop < a->src_btop)
24632 +               wr_dir_args.force_btgt = a->dst_btop;
24633 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24634 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24635 +       a->btgt = err;
24636 +       if (a->exchange)
24637 +               au_update_dbtop(a->dst_dentry);
24638 +
24639 +       return err;
24640 +}
24641 +
24642 +static void au_ren_dt(struct au_ren_args *a)
24643 +{
24644 +       a->h_path.dentry = a->src_h_parent;
24645 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
24646 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
24647 +               a->h_path.dentry = a->dst_h_parent;
24648 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24649 +       }
24650 +
24651 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
24652 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24653 +           && !a->exchange)
24654 +               return;
24655 +
24656 +       a->h_path.dentry = a->src_h_dentry;
24657 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
24658 +       if (d_is_positive(a->dst_h_dentry)) {
24659 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
24660 +               a->h_path.dentry = a->dst_h_dentry;
24661 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24662 +       }
24663 +}
24664 +
24665 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
24666 +{
24667 +       struct dentry *h_d;
24668 +       struct inode *h_inode;
24669 +
24670 +       au_dtime_revert(a->src_dt + AuPARENT);
24671 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
24672 +               au_dtime_revert(a->dst_dt + AuPARENT);
24673 +
24674 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
24675 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
24676 +               h_inode = d_inode(h_d);
24677 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
24678 +               au_dtime_revert(a->src_dt + AuCHILD);
24679 +               inode_unlock(h_inode);
24680 +
24681 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
24682 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
24683 +                       h_inode = d_inode(h_d);
24684 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
24685 +                       au_dtime_revert(a->dst_dt + AuCHILD);
24686 +                       inode_unlock(h_inode);
24687 +               }
24688 +       }
24689 +}
24690 +
24691 +/* ---------------------------------------------------------------------- */
24692 +
24693 +int aufs_rename(struct user_namespace *userns,
24694 +               struct inode *_src_dir, struct dentry *_src_dentry,
24695 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
24696 +               unsigned int _flags)
24697 +{
24698 +       int err, lock_flags;
24699 +       void *rev;
24700 +       /* reduce stack space */
24701 +       struct au_ren_args *a;
24702 +       struct au_pin pin;
24703 +
24704 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
24705 +       IMustLock(_src_dir);
24706 +       IMustLock(_dst_dir);
24707 +
24708 +       err = -EINVAL;
24709 +       if (unlikely(_flags & RENAME_WHITEOUT))
24710 +               goto out;
24711 +
24712 +       err = -ENOMEM;
24713 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24714 +       a = kzalloc(sizeof(*a), GFP_NOFS);
24715 +       if (unlikely(!a))
24716 +               goto out;
24717 +
24718 +       a->flags = _flags;
24719 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24720 +                    && RENAME_EXCHANGE > U8_MAX);
24721 +       a->exchange = _flags & RENAME_EXCHANGE;
24722 +       a->src_dir = _src_dir;
24723 +       a->src_dentry = _src_dentry;
24724 +       a->src_inode = NULL;
24725 +       if (d_really_is_positive(a->src_dentry))
24726 +               a->src_inode = d_inode(a->src_dentry);
24727 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24728 +       a->dst_dir = _dst_dir;
24729 +       a->dst_dentry = _dst_dentry;
24730 +       a->dst_inode = NULL;
24731 +       if (d_really_is_positive(a->dst_dentry))
24732 +               a->dst_inode = d_inode(a->dst_dentry);
24733 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24734 +       if (a->dst_inode) {
24735 +               /*
24736 +                * if EXCHANGE && src is non-dir && dst is dir,
24737 +                * dst is not locked.
24738 +                */
24739 +               /* IMustLock(a->dst_inode); */
24740 +               au_igrab(a->dst_inode);
24741 +       }
24742 +
24743 +       err = -ENOTDIR;
24744 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
24745 +       if (d_is_dir(a->src_dentry)) {
24746 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
24747 +               if (unlikely(!a->exchange
24748 +                            && d_really_is_positive(a->dst_dentry)
24749 +                            && !d_is_dir(a->dst_dentry)))
24750 +                       goto out_free;
24751 +               lock_flags |= AuLock_DIRS;
24752 +       }
24753 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24754 +               au_fset_ren(a->auren_flags, ISDIR_DST);
24755 +               if (unlikely(!a->exchange
24756 +                            && d_really_is_positive(a->src_dentry)
24757 +                            && !d_is_dir(a->src_dentry)))
24758 +                       goto out_free;
24759 +               lock_flags |= AuLock_DIRS;
24760 +       }
24761 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24762 +                                       lock_flags);
24763 +       if (unlikely(err))
24764 +               goto out_free;
24765 +
24766 +       err = au_d_hashed_positive(a->src_dentry);
24767 +       if (unlikely(err))
24768 +               goto out_unlock;
24769 +       err = -ENOENT;
24770 +       if (a->dst_inode) {
24771 +               /*
24772 +                * If it is a dir, VFS unhash it before this
24773 +                * function. It means we cannot rely upon d_unhashed().
24774 +                */
24775 +               if (unlikely(!a->dst_inode->i_nlink))
24776 +                       goto out_unlock;
24777 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24778 +                       err = au_d_hashed_positive(a->dst_dentry);
24779 +                       if (unlikely(err && !a->exchange))
24780 +                               goto out_unlock;
24781 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24782 +                       goto out_unlock;
24783 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
24784 +               goto out_unlock;
24785 +
24786 +       /*
24787 +        * is it possible?
24788 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
24789 +        * there may exist a problem somewhere else.
24790 +        */
24791 +       err = -EINVAL;
24792 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
24793 +               goto out_unlock;
24794 +
24795 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
24796 +       di_write_lock_parent(a->dst_parent);
24797 +
24798 +       /* which branch we process */
24799 +       err = au_ren_wbr(a);
24800 +       if (unlikely(err < 0))
24801 +               goto out_parent;
24802 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
24803 +       a->h_path.mnt = au_br_mnt(a->br);
24804 +
24805 +       /* are they available to be renamed */
24806 +       err = au_ren_may_dir(a);
24807 +       if (unlikely(err))
24808 +               goto out_children;
24809 +
24810 +       /* prepare the writable parent dir on the same branch */
24811 +       if (a->dst_btop == a->btgt) {
24812 +               au_fset_ren(a->auren_flags, WHDST);
24813 +       } else {
24814 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
24815 +               if (unlikely(err))
24816 +                       goto out_children;
24817 +       }
24818 +
24819 +       err = 0;
24820 +       if (!a->exchange) {
24821 +               if (a->src_dir != a->dst_dir) {
24822 +                       /*
24823 +                        * this temporary unlock is safe,
24824 +                        * because both dir->i_mutex are locked.
24825 +                        */
24826 +                       di_write_unlock(a->dst_parent);
24827 +                       di_write_lock_parent(a->src_parent);
24828 +                       err = au_wr_dir_need_wh(a->src_dentry,
24829 +                                               au_ftest_ren(a->auren_flags,
24830 +                                                            ISDIR_SRC),
24831 +                                               &a->btgt);
24832 +                       di_write_unlock(a->src_parent);
24833 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
24834 +                                             /*isdir*/1);
24835 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
24836 +               } else
24837 +                       err = au_wr_dir_need_wh(a->src_dentry,
24838 +                                               au_ftest_ren(a->auren_flags,
24839 +                                                            ISDIR_SRC),
24840 +                                               &a->btgt);
24841 +       }
24842 +       if (unlikely(err < 0))
24843 +               goto out_children;
24844 +       if (err)
24845 +               au_fset_ren(a->auren_flags, WHSRC);
24846 +
24847 +       /* cpup src */
24848 +       if (a->src_btop != a->btgt) {
24849 +               err = au_pin(&pin, a->src_dentry, a->btgt,
24850 +                            au_opt_udba(a->src_dentry->d_sb),
24851 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24852 +               if (!err) {
24853 +                       struct au_cp_generic cpg = {
24854 +                               .dentry = a->src_dentry,
24855 +                               .bdst   = a->btgt,
24856 +                               .bsrc   = a->src_btop,
24857 +                               .len    = -1,
24858 +                               .pin    = &pin,
24859 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24860 +                       };
24861 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
24862 +                       err = au_sio_cpup_simple(&cpg);
24863 +                       au_unpin(&pin);
24864 +               }
24865 +               if (unlikely(err))
24866 +                       goto out_children;
24867 +               a->src_btop = a->btgt;
24868 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
24869 +               if (!a->exchange)
24870 +                       au_fset_ren(a->auren_flags, WHSRC);
24871 +       }
24872 +
24873 +       /* cpup dst */
24874 +       if (a->exchange && a->dst_inode
24875 +           && a->dst_btop != a->btgt) {
24876 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
24877 +                            au_opt_udba(a->dst_dentry->d_sb),
24878 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24879 +               if (!err) {
24880 +                       struct au_cp_generic cpg = {
24881 +                               .dentry = a->dst_dentry,
24882 +                               .bdst   = a->btgt,
24883 +                               .bsrc   = a->dst_btop,
24884 +                               .len    = -1,
24885 +                               .pin    = &pin,
24886 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24887 +                       };
24888 +                       err = au_sio_cpup_simple(&cpg);
24889 +                       au_unpin(&pin);
24890 +               }
24891 +               if (unlikely(err))
24892 +                       goto out_children;
24893 +               a->dst_btop = a->btgt;
24894 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
24895 +       }
24896 +
24897 +       /* lock them all */
24898 +       err = au_ren_lock(a);
24899 +       if (unlikely(err))
24900 +               /* leave the copied-up one */
24901 +               goto out_children;
24902 +
24903 +       if (!a->exchange) {
24904 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24905 +                       err = au_may_ren(a);
24906 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24907 +                       err = -ENAMETOOLONG;
24908 +               if (unlikely(err))
24909 +                       goto out_hdir;
24910 +       }
24911 +
24912 +       /* store timestamps to be revertible */
24913 +       au_ren_dt(a);
24914 +
24915 +       /* store dirren info */
24916 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24917 +               err = au_dr_rename(a->src_dentry, a->btgt,
24918 +                                  &a->dst_dentry->d_name, &rev);
24919 +               AuTraceErr(err);
24920 +               if (unlikely(err))
24921 +                       goto out_dt;
24922 +       }
24923 +
24924 +       /* here we go */
24925 +       err = do_rename(a);
24926 +       if (unlikely(err))
24927 +               goto out_dirren;
24928 +
24929 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24930 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
24931 +
24932 +       /* update dir attributes */
24933 +       au_ren_refresh_dir(a);
24934 +
24935 +       /* dput/iput all lower dentries */
24936 +       au_ren_refresh(a);
24937 +
24938 +       goto out_hdir; /* success */
24939 +
24940 +out_dirren:
24941 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24942 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
24943 +out_dt:
24944 +       au_ren_rev_dt(err, a);
24945 +out_hdir:
24946 +       au_ren_unlock(a);
24947 +out_children:
24948 +       au_nhash_wh_free(&a->whlist);
24949 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
24950 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
24951 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
24952 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
24953 +       }
24954 +out_parent:
24955 +       if (!err) {
24956 +               if (d_unhashed(a->src_dentry))
24957 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
24958 +               if (d_unhashed(a->dst_dentry))
24959 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
24960 +               if (!a->exchange)
24961 +                       d_move(a->src_dentry, a->dst_dentry);
24962 +               else {
24963 +                       d_exchange(a->src_dentry, a->dst_dentry);
24964 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
24965 +                               d_drop(a->dst_dentry);
24966 +               }
24967 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
24968 +                       d_drop(a->src_dentry);
24969 +       } else {
24970 +               au_update_dbtop(a->dst_dentry);
24971 +               if (!a->dst_inode)
24972 +                       d_drop(a->dst_dentry);
24973 +       }
24974 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24975 +               di_write_unlock(a->dst_parent);
24976 +       else
24977 +               di_write_unlock2(a->src_parent, a->dst_parent);
24978 +out_unlock:
24979 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
24980 +out_free:
24981 +       iput(a->dst_inode);
24982 +       if (a->thargs)
24983 +               au_whtmp_rmdir_free(a->thargs);
24984 +       au_kfree_rcu(a);
24985 +out:
24986 +       AuTraceErr(err);
24987 +       return err;
24988 +}
24989 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
24990 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
24991 +++ linux/fs/aufs/Kconfig       2022-08-02 12:12:45.162308621 +0200
24992 @@ -0,0 +1,199 @@
24993 +# SPDX-License-Identifier: GPL-2.0
24994 +config AUFS_FS
24995 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
24996 +       help
24997 +       Aufs is a stackable unification filesystem such as Unionfs,
24998 +       which unifies several directories and provides a merged single
24999 +       directory.
25000 +       In the early days, aufs was entirely re-designed and
25001 +       re-implemented Unionfs Version 1.x series. Introducing many
25002 +       original ideas, approaches and improvements, it becomes totally
25003 +       different from Unionfs while keeping the basic features.
25004 +
25005 +if AUFS_FS
25006 +choice
25007 +       prompt "Maximum number of branches"
25008 +       default AUFS_BRANCH_MAX_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_127
25014 +       bool "127"
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_511
25020 +       bool "511"
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_1023
25026 +       bool "1023"
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 +config AUFS_BRANCH_MAX_32767
25032 +       bool "32767"
25033 +       help
25034 +       Specifies the maximum number of branches (or member directories)
25035 +       in a single aufs. The larger value consumes more system
25036 +       resources and has a minor impact to performance.
25037 +endchoice
25038 +
25039 +config AUFS_SBILIST
25040 +       bool
25041 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
25042 +       default y
25043 +       help
25044 +       Automatic configuration for internal use.
25045 +       When aufs supports Magic SysRq or /proc, enabled automatically.
25046 +
25047 +config AUFS_HNOTIFY
25048 +       bool "Detect direct branch access (bypassing aufs)"
25049 +       help
25050 +       If you want to modify files on branches directly, eg. bypassing aufs,
25051 +       and want aufs to detect the changes of them fully, then enable this
25052 +       option and use 'udba=notify' mount option.
25053 +       Currently there is only one available configuration, "fsnotify".
25054 +       It will have a negative impact to the performance.
25055 +       See detail in aufs.5.
25056 +
25057 +choice
25058 +       prompt "method" if AUFS_HNOTIFY
25059 +       default AUFS_HFSNOTIFY
25060 +config AUFS_HFSNOTIFY
25061 +       bool "fsnotify"
25062 +       select FSNOTIFY
25063 +endchoice
25064 +
25065 +config AUFS_EXPORT
25066 +       bool "NFS-exportable aufs"
25067 +       depends on EXPORTFS
25068 +       help
25069 +       If you want to export your mounted aufs via NFS, then enable this
25070 +       option. There are several requirements for this configuration.
25071 +       See detail in aufs.5.
25072 +
25073 +config AUFS_INO_T_64
25074 +       bool
25075 +       depends on AUFS_EXPORT
25076 +       depends on 64BIT && !(ALPHA || S390)
25077 +       default y
25078 +       help
25079 +       Automatic configuration for internal use.
25080 +       /* typedef unsigned long/int __kernel_ino_t */
25081 +       /* alpha and s390x are int */
25082 +
25083 +config AUFS_XATTR
25084 +       bool "support for XATTR/EA (including Security Labels)"
25085 +       help
25086 +       If your branch fs supports XATTR/EA and you want to make them
25087 +       available in aufs too, then enable this opsion and specify the
25088 +       branch attributes for EA.
25089 +       See detail in aufs.5.
25090 +
25091 +config AUFS_FHSM
25092 +       bool "File-based Hierarchical Storage Management"
25093 +       help
25094 +       Hierarchical Storage Management (or HSM) is a well-known feature
25095 +       in the storage world. Aufs provides this feature as file-based.
25096 +       with multiple branches.
25097 +       These multiple branches are prioritized, ie. the topmost one
25098 +       should be the fastest drive and be used heavily.
25099 +
25100 +config AUFS_RDU
25101 +       bool "Readdir in userspace"
25102 +       help
25103 +       Aufs has two methods to provide a merged view for a directory,
25104 +       by a user-space library and by kernel-space natively. The latter
25105 +       is always enabled but sometimes large and slow.
25106 +       If you enable this option, install the library in aufs2-util
25107 +       package, and set some environment variables for your readdir(3),
25108 +       then the work will be handled in user-space which generally
25109 +       shows better performance in most cases.
25110 +       See detail in aufs.5.
25111 +
25112 +config AUFS_DIRREN
25113 +       bool "Workaround for rename(2)-ing a directory"
25114 +       help
25115 +       By default, aufs returns EXDEV error in renameing a dir who has
25116 +       his child on the lower branch, since it is a bad idea to issue
25117 +       rename(2) internally for every lower branch. But user may not
25118 +       accept this behaviour. So here is a workaround to allow such
25119 +       rename(2) and store some extra information on the writable
25120 +       branch. Obviously this costs high (and I don't like it).
25121 +       To use this feature, you need to enable this configuration AND
25122 +       to specify the mount option `dirren.'
25123 +       See details in aufs.5 and the design documents.
25124 +
25125 +config AUFS_SHWH
25126 +       bool "Show whiteouts"
25127 +       help
25128 +       If you want to make the whiteouts in aufs visible, then enable
25129 +       this option and specify 'shwh' mount option. Although it may
25130 +       sounds like philosophy or something, but in technically it
25131 +       simply shows the name of whiteout with keeping its behaviour.
25132 +
25133 +config AUFS_BR_RAMFS
25134 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
25135 +       help
25136 +       If you want to use ramfs as an aufs branch fs, then enable this
25137 +       option. Generally tmpfs is recommended.
25138 +       Aufs prohibited them to be a branch fs by default, because
25139 +       initramfs becomes unusable after switch_root or something
25140 +       generally. If you sets initramfs as an aufs branch and boot your
25141 +       system by switch_root, you will meet a problem easily since the
25142 +       files in initramfs may be inaccessible.
25143 +       Unless you are going to use ramfs as an aufs branch fs without
25144 +       switch_root or something, leave it N.
25145 +
25146 +config AUFS_BR_FUSE
25147 +       bool "Fuse fs as an aufs branch"
25148 +       depends on FUSE_FS
25149 +       select AUFS_POLL
25150 +       help
25151 +       If you want to use fuse-based userspace filesystem as an aufs
25152 +       branch fs, then enable this option.
25153 +       It implements the internal poll(2) operation which is
25154 +       implemented by fuse only (curretnly).
25155 +
25156 +config AUFS_POLL
25157 +       bool
25158 +       help
25159 +       Automatic configuration for internal use.
25160 +
25161 +config AUFS_BR_HFSPLUS
25162 +       bool "Hfsplus as an aufs branch"
25163 +       depends on HFSPLUS_FS
25164 +       default y
25165 +       help
25166 +       If you want to use hfsplus fs as an aufs branch fs, then enable
25167 +       this option. This option introduces a small overhead at
25168 +       copying-up a file on hfsplus.
25169 +
25170 +config AUFS_BDEV_LOOP
25171 +       bool
25172 +       depends on BLK_DEV_LOOP
25173 +       default y
25174 +       help
25175 +       Automatic configuration for internal use.
25176 +       Convert =[ym] into =y.
25177 +
25178 +config AUFS_DEBUG
25179 +       bool "Debug aufs"
25180 +       help
25181 +       Enable this to compile aufs internal debug code.
25182 +       It will have a negative impact to the performance.
25183 +
25184 +config AUFS_MAGIC_SYSRQ
25185 +       bool
25186 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
25187 +       default y
25188 +       help
25189 +       Automatic configuration for internal use.
25190 +       When aufs supports Magic SysRq, enabled automatically.
25191 +endif
25192 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
25193 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
25194 +++ linux/fs/aufs/lcnt.h        2022-08-02 12:12:45.165641939 +0200
25195 @@ -0,0 +1,186 @@
25196 +/* SPDX-License-Identifier: GPL-2.0 */
25197 +/*
25198 + * Copyright (C) 2018-2021 Junjiro R. Okajima
25199 + *
25200 + * This program, aufs is free software; you can redistribute it and/or modify
25201 + * it under the terms of the GNU General Public License as published by
25202 + * the Free Software Foundation; either version 2 of the License, or
25203 + * (at your option) any later version.
25204 + *
25205 + * This program is distributed in the hope that it will be useful,
25206 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25207 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25208 + * GNU General Public License for more details.
25209 + *
25210 + * You should have received a copy of the GNU General Public License
25211 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25212 + */
25213 +
25214 +/*
25215 + * simple long counter wrapper
25216 + */
25217 +
25218 +#ifndef __AUFS_LCNT_H__
25219 +#define __AUFS_LCNT_H__
25220 +
25221 +#ifdef __KERNEL__
25222 +
25223 +#include "debug.h"
25224 +
25225 +#define AuLCntATOMIC   1
25226 +#define AuLCntPCPUCNT  2
25227 +/*
25228 + * why does percpu_refcount require extra synchronize_rcu()s in
25229 + * au_br_do_free()
25230 + */
25231 +#define AuLCntPCPUREF  3
25232 +
25233 +/* #define AuLCntChosen        AuLCntATOMIC */
25234 +#define AuLCntChosen   AuLCntPCPUCNT
25235 +/* #define AuLCntChosen        AuLCntPCPUREF */
25236 +
25237 +#if AuLCntChosen == AuLCntATOMIC
25238 +#include <linux/atomic.h>
25239 +
25240 +typedef atomic_long_t au_lcnt_t;
25241 +
25242 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25243 +{
25244 +       atomic_long_set(cnt, 0);
25245 +       return 0;
25246 +}
25247 +
25248 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25249 +{
25250 +       /* empty */
25251 +}
25252 +
25253 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25254 +                              int do_sync __maybe_unused)
25255 +{
25256 +       /* empty */
25257 +}
25258 +
25259 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25260 +{
25261 +       atomic_long_inc(cnt);
25262 +}
25263 +
25264 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25265 +{
25266 +       atomic_long_dec(cnt);
25267 +}
25268 +
25269 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25270 +{
25271 +       return atomic_long_read(cnt);
25272 +}
25273 +#endif
25274 +
25275 +#if AuLCntChosen == AuLCntPCPUCNT
25276 +#include <linux/percpu_counter.h>
25277 +
25278 +typedef struct percpu_counter au_lcnt_t;
25279 +
25280 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25281 +{
25282 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
25283 +}
25284 +
25285 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25286 +{
25287 +       /* empty */
25288 +}
25289 +
25290 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25291 +{
25292 +       percpu_counter_destroy(cnt);
25293 +}
25294 +
25295 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25296 +{
25297 +       percpu_counter_inc(cnt);
25298 +}
25299 +
25300 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25301 +{
25302 +       percpu_counter_dec(cnt);
25303 +}
25304 +
25305 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25306 +{
25307 +       s64 n;
25308 +
25309 +       n = percpu_counter_sum(cnt);
25310 +       BUG_ON(n < 0);
25311 +       if (LONG_MAX != LLONG_MAX
25312 +           && n > LONG_MAX)
25313 +               AuWarn1("%s\n", "wrap-around");
25314 +
25315 +       return n;
25316 +}
25317 +#endif
25318 +
25319 +#if AuLCntChosen == AuLCntPCPUREF
25320 +#include <linux/percpu-refcount.h>
25321 +
25322 +typedef struct percpu_ref au_lcnt_t;
25323 +
25324 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25325 +{
25326 +       if (!release)
25327 +               release = percpu_ref_exit;
25328 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25329 +}
25330 +
25331 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25332 +{
25333 +       synchronize_rcu();
25334 +}
25335 +
25336 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25337 +{
25338 +       percpu_ref_kill(cnt);
25339 +       if (do_sync)
25340 +               au_lcnt_wait_for_fin(cnt);
25341 +}
25342 +
25343 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25344 +{
25345 +       percpu_ref_get(cnt);
25346 +}
25347 +
25348 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25349 +{
25350 +       percpu_ref_put(cnt);
25351 +}
25352 +
25353 +/*
25354 + * avoid calling this func as possible.
25355 + */
25356 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25357 +{
25358 +       long l;
25359 +
25360 +       percpu_ref_switch_to_atomic_sync(cnt);
25361 +       l = atomic_long_read(&cnt->count);
25362 +       if (do_rev)
25363 +               percpu_ref_switch_to_percpu(cnt);
25364 +
25365 +       /* percpu_ref is initialized by 1 instead of 0 */
25366 +       return l - 1;
25367 +}
25368 +#endif
25369 +
25370 +#ifdef CONFIG_AUFS_DEBUG
25371 +#define AuLCntZero(val) do {                   \
25372 +       long l = val;                           \
25373 +       if (l)                                  \
25374 +               AuDbg("%s = %ld\n", #val, l);   \
25375 +} while (0)
25376 +#else
25377 +#define AuLCntZero(val)                do {} while (0)
25378 +#endif
25379 +
25380 +#endif /* __KERNEL__ */
25381 +#endif /* __AUFS_LCNT_H__ */
25382 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
25383 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
25384 +++ linux/fs/aufs/loop.c        2022-08-02 12:12:45.165641939 +0200
25385 @@ -0,0 +1,148 @@
25386 +// SPDX-License-Identifier: GPL-2.0
25387 +/*
25388 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25389 + *
25390 + * This program, aufs is free software; you can redistribute it and/or modify
25391 + * it under the terms of the GNU General Public License as published by
25392 + * the Free Software Foundation; either version 2 of the License, or
25393 + * (at your option) any later version.
25394 + *
25395 + * This program is distributed in the hope that it will be useful,
25396 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25397 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25398 + * GNU General Public License for more details.
25399 + *
25400 + * You should have received a copy of the GNU General Public License
25401 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25402 + */
25403 +
25404 +/*
25405 + * support for loopback block device as a branch
25406 + */
25407 +
25408 +#include "aufs.h"
25409 +
25410 +/* added into drivers/block/loop.c */
25411 +static struct file *(*backing_file_func)(struct super_block *sb);
25412 +
25413 +/*
25414 + * test if two lower dentries have overlapping branches.
25415 + */
25416 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25417 +{
25418 +       struct super_block *h_sb;
25419 +       struct file *backing_file;
25420 +
25421 +       if (unlikely(!backing_file_func)) {
25422 +               /* don't load "loop" module here */
25423 +               backing_file_func = symbol_get(loop_backing_file);
25424 +               if (unlikely(!backing_file_func))
25425 +                       /* "loop" module is not loaded */
25426 +                       return 0;
25427 +       }
25428 +
25429 +       h_sb = h_adding->d_sb;
25430 +       backing_file = backing_file_func(h_sb);
25431 +       if (!backing_file)
25432 +               return 0;
25433 +
25434 +       h_adding = backing_file->f_path.dentry;
25435 +       /*
25436 +        * h_adding can be local NFS.
25437 +        * in this case aufs cannot detect the loop.
25438 +        */
25439 +       if (unlikely(h_adding->d_sb == sb))
25440 +               return 1;
25441 +       return !!au_test_subdir(h_adding, sb->s_root);
25442 +}
25443 +
25444 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25445 +int au_test_loopback_kthread(void)
25446 +{
25447 +       int ret;
25448 +       struct task_struct *tsk = current;
25449 +       char c, comm[sizeof(tsk->comm)];
25450 +
25451 +       ret = 0;
25452 +       if (tsk->flags & PF_KTHREAD) {
25453 +               get_task_comm(comm, tsk);
25454 +               c = comm[4];
25455 +               ret = ('0' <= c && c <= '9'
25456 +                      && !strncmp(comm, "loop", 4));
25457 +       }
25458 +
25459 +       return ret;
25460 +}
25461 +
25462 +/* ---------------------------------------------------------------------- */
25463 +
25464 +#define au_warn_loopback_step  16
25465 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25466 +static unsigned long *au_warn_loopback_array;
25467 +
25468 +void au_warn_loopback(struct super_block *h_sb)
25469 +{
25470 +       int i, new_nelem;
25471 +       unsigned long *a, magic;
25472 +       static DEFINE_SPINLOCK(spin);
25473 +
25474 +       magic = h_sb->s_magic;
25475 +       spin_lock(&spin);
25476 +       a = au_warn_loopback_array;
25477 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25478 +               if (a[i] == magic) {
25479 +                       spin_unlock(&spin);
25480 +                       return;
25481 +               }
25482 +
25483 +       /* h_sb is new to us, print it */
25484 +       if (i < au_warn_loopback_nelem) {
25485 +               a[i] = magic;
25486 +               goto pr;
25487 +       }
25488 +
25489 +       /* expand the array */
25490 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25491 +       a = au_kzrealloc(au_warn_loopback_array,
25492 +                        au_warn_loopback_nelem * sizeof(unsigned long),
25493 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25494 +                        /*may_shrink*/0);
25495 +       if (a) {
25496 +               au_warn_loopback_nelem = new_nelem;
25497 +               au_warn_loopback_array = a;
25498 +               a[i] = magic;
25499 +               goto pr;
25500 +       }
25501 +
25502 +       spin_unlock(&spin);
25503 +       AuWarn1("realloc failed, ignored\n");
25504 +       return;
25505 +
25506 +pr:
25507 +       spin_unlock(&spin);
25508 +       pr_warn("you may want to try another patch for loopback file "
25509 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25510 +}
25511 +
25512 +int au_loopback_init(void)
25513 +{
25514 +       int err;
25515 +       struct super_block *sb __maybe_unused;
25516 +
25517 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25518 +
25519 +       err = 0;
25520 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25521 +                                        sizeof(unsigned long), GFP_NOFS);
25522 +       if (unlikely(!au_warn_loopback_array))
25523 +               err = -ENOMEM;
25524 +
25525 +       return err;
25526 +}
25527 +
25528 +void au_loopback_fin(void)
25529 +{
25530 +       if (backing_file_func)
25531 +               symbol_put(loop_backing_file);
25532 +       au_kfree_try_rcu(au_warn_loopback_array);
25533 +}
25534 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
25535 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
25536 +++ linux/fs/aufs/loop.h        2022-08-02 12:12:45.165641939 +0200
25537 @@ -0,0 +1,55 @@
25538 +/* SPDX-License-Identifier: GPL-2.0 */
25539 +/*
25540 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25541 + *
25542 + * This program, aufs is free software; you can redistribute it and/or modify
25543 + * it under the terms of the GNU General Public License as published by
25544 + * the Free Software Foundation; either version 2 of the License, or
25545 + * (at your option) any later version.
25546 + *
25547 + * This program is distributed in the hope that it will be useful,
25548 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25549 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25550 + * GNU General Public License for more details.
25551 + *
25552 + * You should have received a copy of the GNU General Public License
25553 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25554 + */
25555 +
25556 +/*
25557 + * support for loopback mount as a branch
25558 + */
25559 +
25560 +#ifndef __AUFS_LOOP_H__
25561 +#define __AUFS_LOOP_H__
25562 +
25563 +#ifdef __KERNEL__
25564 +
25565 +struct dentry;
25566 +struct super_block;
25567 +
25568 +#ifdef CONFIG_AUFS_BDEV_LOOP
25569 +/* drivers/block/loop.c */
25570 +struct file *loop_backing_file(struct super_block *sb);
25571 +
25572 +/* loop.c */
25573 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25574 +int au_test_loopback_kthread(void);
25575 +void au_warn_loopback(struct super_block *h_sb);
25576 +
25577 +int au_loopback_init(void);
25578 +void au_loopback_fin(void);
25579 +#else
25580 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25581 +
25582 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25583 +          struct dentry *h_adding)
25584 +AuStubInt0(au_test_loopback_kthread, void)
25585 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25586 +
25587 +AuStubInt0(au_loopback_init, void)
25588 +AuStubVoid(au_loopback_fin, void)
25589 +#endif /* BLK_DEV_LOOP */
25590 +
25591 +#endif /* __KERNEL__ */
25592 +#endif /* __AUFS_LOOP_H__ */
25593 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
25594 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
25595 +++ linux/fs/aufs/magic.mk      2022-08-02 12:12:45.165641939 +0200
25596 @@ -0,0 +1,31 @@
25597 +# SPDX-License-Identifier: GPL-2.0
25598 +
25599 +# defined in ${srctree}/fs/fuse/inode.c
25600 +# tristate
25601 +ifdef CONFIG_FUSE_FS
25602 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25603 +endif
25604 +
25605 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25606 +# tristate
25607 +ifdef CONFIG_XFS_FS
25608 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25609 +endif
25610 +
25611 +# defined in ${srctree}/fs/configfs/mount.c
25612 +# tristate
25613 +ifdef CONFIG_CONFIGFS_FS
25614 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25615 +endif
25616 +
25617 +# defined in ${srctree}/fs/ubifs/ubifs.h
25618 +# tristate
25619 +ifdef CONFIG_UBIFS_FS
25620 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25621 +endif
25622 +
25623 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25624 +# tristate
25625 +ifdef CONFIG_HFSPLUS_FS
25626 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25627 +endif
25628 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
25629 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
25630 +++ linux/fs/aufs/Makefile      2022-08-02 12:12:45.162308621 +0200
25631 @@ -0,0 +1,46 @@
25632 +# SPDX-License-Identifier: GPL-2.0
25633 +
25634 +include ${src}/magic.mk
25635 +ifeq (${CONFIG_AUFS_FS},m)
25636 +include ${src}/conf.mk
25637 +endif
25638 +-include ${src}/priv_def.mk
25639 +
25640 +# cf. include/linux/kernel.h
25641 +# enable pr_debug
25642 +ccflags-y += -DDEBUG
25643 +# sparse requires the full pathname
25644 +ifdef M
25645 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
25646 +else
25647 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
25648 +endif
25649 +
25650 +obj-$(CONFIG_AUFS_FS) += aufs.o
25651 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
25652 +       wkq.o vfsub.o dcsub.o \
25653 +       cpup.o whout.o wbr_policy.o \
25654 +       dinfo.o dentry.o \
25655 +       dynop.o \
25656 +       finfo.o file.o f_op.o \
25657 +       dir.o vdir.o \
25658 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
25659 +       mvdown.o ioctl.o
25660 +
25661 +# all are boolean
25662 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
25663 +aufs-$(CONFIG_SYSFS) += sysfs.o
25664 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25665 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25666 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25667 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
25668 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
25669 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25670 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
25671 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
25672 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
25673 +aufs-$(CONFIG_AUFS_POLL) += poll.o
25674 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
25675 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25676 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25677 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
25678 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
25679 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
25680 +++ linux/fs/aufs/module.c      2022-08-02 12:12:45.165641939 +0200
25681 @@ -0,0 +1,273 @@
25682 +// SPDX-License-Identifier: GPL-2.0
25683 +/*
25684 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25685 + *
25686 + * This program, aufs is free software; you can redistribute it and/or modify
25687 + * it under the terms of the GNU General Public License as published by
25688 + * the Free Software Foundation; either version 2 of the License, or
25689 + * (at your option) any later version.
25690 + *
25691 + * This program is distributed in the hope that it will be useful,
25692 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25693 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25694 + * GNU General Public License for more details.
25695 + *
25696 + * You should have received a copy of the GNU General Public License
25697 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25698 + */
25699 +
25700 +/*
25701 + * module global variables and operations
25702 + */
25703 +
25704 +#include <linux/module.h>
25705 +#include <linux/seq_file.h>
25706 +#include "aufs.h"
25707 +
25708 +/* shrinkable realloc */
25709 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25710 +{
25711 +       size_t sz;
25712 +       int diff;
25713 +
25714 +       sz = 0;
25715 +       diff = -1;
25716 +       if (p) {
25717 +#if 0 /* unused */
25718 +               if (!new_sz) {
25719 +                       au_kfree_rcu(p);
25720 +                       p = NULL;
25721 +                       goto out;
25722 +               }
25723 +#else
25724 +               AuDebugOn(!new_sz);
25725 +#endif
25726 +               sz = ksize(p);
25727 +               diff = au_kmidx_sub(sz, new_sz);
25728 +       }
25729 +       if (sz && !diff)
25730 +               goto out;
25731 +
25732 +       if (sz < new_sz)
25733 +               /* expand or SLOB */
25734 +               p = krealloc(p, new_sz, gfp);
25735 +       else if (new_sz < sz && may_shrink) {
25736 +               /* shrink */
25737 +               void *q;
25738 +
25739 +               q = kmalloc(new_sz, gfp);
25740 +               if (q) {
25741 +                       if (p) {
25742 +                               memcpy(q, p, new_sz);
25743 +                               au_kfree_try_rcu(p);
25744 +                       }
25745 +                       p = q;
25746 +               } else
25747 +                       p = NULL;
25748 +       }
25749 +
25750 +out:
25751 +       return p;
25752 +}
25753 +
25754 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25755 +                  int may_shrink)
25756 +{
25757 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
25758 +       if (p && new_sz > nused)
25759 +               memset(p + nused, 0, new_sz - nused);
25760 +       return p;
25761 +}
25762 +
25763 +/* ---------------------------------------------------------------------- */
25764 +/*
25765 + * aufs caches
25766 + */
25767 +struct kmem_cache *au_cache[AuCache_Last];
25768 +
25769 +static void au_cache_fin(void)
25770 +{
25771 +       int i;
25772 +
25773 +       /*
25774 +        * Make sure all delayed rcu free inodes are flushed before we
25775 +        * destroy cache.
25776 +        */
25777 +       rcu_barrier();
25778 +
25779 +       /* excluding AuCache_HNOTIFY */
25780 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25781 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
25782 +               kmem_cache_destroy(au_cache[i]);
25783 +               au_cache[i] = NULL;
25784 +       }
25785 +}
25786 +
25787 +static int __init au_cache_init(void)
25788 +{
25789 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25790 +       if (au_cache[AuCache_DINFO])
25791 +               /* SLAB_DESTROY_BY_RCU */
25792 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25793 +                                                      au_icntnr_init_once);
25794 +       if (au_cache[AuCache_ICNTNR])
25795 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25796 +                                                     au_fi_init_once);
25797 +       if (au_cache[AuCache_FINFO])
25798 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
25799 +       if (au_cache[AuCache_VDIR])
25800 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25801 +       if (au_cache[AuCache_DEHSTR])
25802 +               return 0;
25803 +
25804 +       au_cache_fin();
25805 +       return -ENOMEM;
25806 +}
25807 +
25808 +/* ---------------------------------------------------------------------- */
25809 +
25810 +int au_dir_roflags;
25811 +
25812 +#ifdef CONFIG_AUFS_SBILIST
25813 +/*
25814 + * iterate_supers_type() doesn't protect us from
25815 + * remounting (branch management)
25816 + */
25817 +struct hlist_bl_head au_sbilist;
25818 +#endif
25819 +
25820 +/*
25821 + * functions for module interface.
25822 + */
25823 +MODULE_LICENSE("GPL");
25824 +/* MODULE_LICENSE("GPL v2"); */
25825 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25826 +MODULE_DESCRIPTION(AUFS_NAME
25827 +       " -- Advanced multi layered unification filesystem");
25828 +MODULE_VERSION(AUFS_VERSION);
25829 +MODULE_ALIAS_FS(AUFS_NAME);
25830 +
25831 +/* this module parameter has no meaning when SYSFS is disabled */
25832 +int sysaufs_brs = 1;
25833 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25834 +module_param_named(brs, sysaufs_brs, int, 0444);
25835 +
25836 +/* this module parameter has no meaning when USER_NS is disabled */
25837 +bool au_userns;
25838 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25839 +module_param_named(allow_userns, au_userns, bool, 0444);
25840 +
25841 +/* ---------------------------------------------------------------------- */
25842 +
25843 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25844 +
25845 +int au_seq_path(struct seq_file *seq, struct path *path)
25846 +{
25847 +       int err;
25848 +
25849 +       err = seq_path(seq, path, au_esc_chars);
25850 +       if (err >= 0)
25851 +               err = 0;
25852 +       else
25853 +               err = -ENOMEM;
25854 +
25855 +       return err;
25856 +}
25857 +
25858 +/* ---------------------------------------------------------------------- */
25859 +
25860 +static int __init aufs_init(void)
25861 +{
25862 +       int err, i;
25863 +       char *p;
25864 +
25865 +       p = au_esc_chars;
25866 +       for (i = 1; i <= ' '; i++)
25867 +               *p++ = i;
25868 +       *p++ = '\\';
25869 +       *p++ = '\x7f';
25870 +       *p = 0;
25871 +
25872 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25873 +
25874 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25875 +       for (i = 0; i < AuIop_Last; i++)
25876 +               aufs_iop_nogetattr[i].getattr = NULL;
25877 +
25878 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
25879 +
25880 +       au_sbilist_init();
25881 +       sysaufs_brs_init();
25882 +       au_debug_init();
25883 +       au_dy_init();
25884 +       err = sysaufs_init();
25885 +       if (unlikely(err))
25886 +               goto out;
25887 +       err = dbgaufs_init();
25888 +       if (unlikely(err))
25889 +               goto out_sysaufs;
25890 +       err = au_procfs_init();
25891 +       if (unlikely(err))
25892 +               goto out_dbgaufs;
25893 +       err = au_wkq_init();
25894 +       if (unlikely(err))
25895 +               goto out_procfs;
25896 +       err = au_loopback_init();
25897 +       if (unlikely(err))
25898 +               goto out_wkq;
25899 +       err = au_hnotify_init();
25900 +       if (unlikely(err))
25901 +               goto out_loopback;
25902 +       err = au_sysrq_init();
25903 +       if (unlikely(err))
25904 +               goto out_hin;
25905 +       err = au_cache_init();
25906 +       if (unlikely(err))
25907 +               goto out_sysrq;
25908 +
25909 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25910 +       err = register_filesystem(&aufs_fs_type);
25911 +       if (unlikely(err))
25912 +               goto out_cache;
25913 +
25914 +       /* since we define pr_fmt, call printk directly */
25915 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25916 +       goto out; /* success */
25917 +
25918 +out_cache:
25919 +       au_cache_fin();
25920 +out_sysrq:
25921 +       au_sysrq_fin();
25922 +out_hin:
25923 +       au_hnotify_fin();
25924 +out_loopback:
25925 +       au_loopback_fin();
25926 +out_wkq:
25927 +       au_wkq_fin();
25928 +out_procfs:
25929 +       au_procfs_fin();
25930 +out_dbgaufs:
25931 +       dbgaufs_fin();
25932 +out_sysaufs:
25933 +       sysaufs_fin();
25934 +       au_dy_fin();
25935 +out:
25936 +       return err;
25937 +}
25938 +
25939 +static void __exit aufs_exit(void)
25940 +{
25941 +       unregister_filesystem(&aufs_fs_type);
25942 +       au_cache_fin();
25943 +       au_sysrq_fin();
25944 +       au_hnotify_fin();
25945 +       au_loopback_fin();
25946 +       au_wkq_fin();
25947 +       au_procfs_fin();
25948 +       dbgaufs_fin();
25949 +       sysaufs_fin();
25950 +       au_dy_fin();
25951 +}
25952 +
25953 +module_init(aufs_init);
25954 +module_exit(aufs_exit);
25955 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
25956 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
25957 +++ linux/fs/aufs/module.h      2022-08-02 12:12:45.165641939 +0200
25958 @@ -0,0 +1,180 @@
25959 +/* SPDX-License-Identifier: GPL-2.0 */
25960 +/*
25961 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25962 + *
25963 + * This program, aufs is free software; you can redistribute it and/or modify
25964 + * it under the terms of the GNU General Public License as published by
25965 + * the Free Software Foundation; either version 2 of the License, or
25966 + * (at your option) any later version.
25967 + *
25968 + * This program is distributed in the hope that it will be useful,
25969 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25970 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25971 + * GNU General Public License for more details.
25972 + *
25973 + * You should have received a copy of the GNU General Public License
25974 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25975 + */
25976 +
25977 +/*
25978 + * module initialization and module-global
25979 + */
25980 +
25981 +#ifndef __AUFS_MODULE_H__
25982 +#define __AUFS_MODULE_H__
25983 +
25984 +#ifdef __KERNEL__
25985 +
25986 +#include <linux/slab.h>
25987 +#include "debug.h"
25988 +#include "dentry.h"
25989 +#include "dir.h"
25990 +#include "file.h"
25991 +#include "inode.h"
25992 +
25993 +struct path;
25994 +struct seq_file;
25995 +
25996 +/* module parameters */
25997 +extern int sysaufs_brs;
25998 +extern bool au_userns;
25999 +
26000 +/* ---------------------------------------------------------------------- */
26001 +
26002 +extern int au_dir_roflags;
26003 +
26004 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
26005 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
26006 +                  int may_shrink);
26007 +
26008 +/*
26009 + * Comparing the size of the object with sizeof(struct rcu_head)
26010 + * case 1: object is always larger
26011 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
26012 + * case 2: object is always smaller
26013 + *     --> au_kfree_small()
26014 + * case 3: object can be any size
26015 + *     --> au_kfree_try_rcu()
26016 + */
26017 +
26018 +static inline void au_kfree_do_rcu(const void *p)
26019 +{
26020 +       struct {
26021 +               struct rcu_head rcu;
26022 +       } *a = (void *)p;
26023 +
26024 +       kfree_rcu(a, rcu);
26025 +}
26026 +
26027 +#define au_kfree_rcu(_p) do {                                          \
26028 +               typeof(_p) p = (_p);                                    \
26029 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
26030 +               if (p)                                                  \
26031 +                       au_kfree_do_rcu(p);                             \
26032 +       } while (0)
26033 +
26034 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
26035 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
26036 +
26037 +static inline void au_kfree_try_rcu(const void *p)
26038 +{
26039 +       if (!p)
26040 +               return;
26041 +       if (au_kfree_sz_test(p))
26042 +               au_kfree_do_rcu(p);
26043 +       else
26044 +               kfree(p);
26045 +}
26046 +
26047 +static inline void au_kfree_small(const void *p)
26048 +{
26049 +       if (!p)
26050 +               return;
26051 +       AuDebugOn(au_kfree_sz_test(p));
26052 +       kfree(p);
26053 +}
26054 +
26055 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26056 +{
26057 +#ifndef CONFIG_SLOB
26058 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
26059 +#else
26060 +       return -1; /* SLOB is untested */
26061 +#endif
26062 +}
26063 +
26064 +int au_seq_path(struct seq_file *seq, struct path *path);
26065 +
26066 +#ifdef CONFIG_PROC_FS
26067 +/* procfs.c */
26068 +int __init au_procfs_init(void);
26069 +void au_procfs_fin(void);
26070 +#else
26071 +AuStubInt0(au_procfs_init, void);
26072 +AuStubVoid(au_procfs_fin, void);
26073 +#endif
26074 +
26075 +/* ---------------------------------------------------------------------- */
26076 +
26077 +/* kmem cache */
26078 +enum {
26079 +       AuCache_DINFO,
26080 +       AuCache_ICNTNR,
26081 +       AuCache_FINFO,
26082 +       AuCache_VDIR,
26083 +       AuCache_DEHSTR,
26084 +       AuCache_HNOTIFY, /* must be last */
26085 +       AuCache_Last
26086 +};
26087 +
26088 +extern struct kmem_cache *au_cache[AuCache_Last];
26089 +
26090 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26091 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
26092 +#define AuCacheCtor(type, ctor)        \
26093 +       kmem_cache_create(#type, sizeof(struct type), \
26094 +                         __alignof__(struct type), AuCacheFlags, ctor)
26095 +
26096 +#define AuCacheFuncAlloc(name, index)                                  \
26097 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
26098 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
26099 +
26100 +#define AuCacheFuncs(name, index)                                      \
26101 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26102 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
26103 +                                                                       \
26104 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26105 +       { void *p = rcu;                                                \
26106 +               p -= offsetof(struct au_##name, rcu);                   \
26107 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
26108 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26109 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26110 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
26111 +                                                                       \
26112 +       static inline void au_cache_free_##name(struct au_##name *p)    \
26113 +       { /* au_cache_free_##name##_norcu(p); */                        \
26114 +               au_cache_free_##name##_rcu(p); }
26115 +
26116 +AuCacheFuncs(dinfo, DINFO);
26117 +AuCacheFuncAlloc(dinfo, DINFO);
26118 +
26119 +AuCacheFuncs(icntnr, ICNTNR);
26120 +static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
26121 +{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
26122 +
26123 +AuCacheFuncs(finfo, FINFO);
26124 +AuCacheFuncAlloc(finfo, FINFO);
26125 +
26126 +AuCacheFuncs(vdir, VDIR);
26127 +AuCacheFuncAlloc(vdir, VDIR);
26128 +
26129 +AuCacheFuncs(vdir_dehstr, DEHSTR);
26130 +AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
26131 +
26132 +#ifdef CONFIG_AUFS_HNOTIFY
26133 +AuCacheFuncs(hnotify, HNOTIFY);
26134 +AuCacheFuncAlloc(hnotify, HNOTIFY);
26135 +#endif
26136 +
26137 +#endif /* __KERNEL__ */
26138 +#endif /* __AUFS_MODULE_H__ */
26139 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
26140 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
26141 +++ linux/fs/aufs/mvdown.c      2022-08-02 12:12:45.165641939 +0200
26142 @@ -0,0 +1,706 @@
26143 +// SPDX-License-Identifier: GPL-2.0
26144 +/*
26145 + * Copyright (C) 2011-2021 Junjiro R. Okajima
26146 + *
26147 + * This program, aufs is free software; you can redistribute it and/or modify
26148 + * it under the terms of the GNU General Public License as published by
26149 + * the Free Software Foundation; either version 2 of the License, or
26150 + * (at your option) any later version.
26151 + *
26152 + * This program is distributed in the hope that it will be useful,
26153 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26154 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26155 + * GNU General Public License for more details.
26156 + *
26157 + * You should have received a copy of the GNU General Public License
26158 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26159 + */
26160 +
26161 +/*
26162 + * move-down, opposite of copy-up
26163 + */
26164 +
26165 +#include "aufs.h"
26166 +
26167 +struct au_mvd_args {
26168 +       struct {
26169 +               struct super_block *h_sb;
26170 +               struct dentry *h_parent;
26171 +               struct au_hinode *hdir;
26172 +               struct inode *h_dir, *h_inode;
26173 +               struct au_pin pin;
26174 +       } info[AUFS_MVDOWN_NARRAY];
26175 +
26176 +       struct aufs_mvdown mvdown;
26177 +       struct dentry *dentry, *parent;
26178 +       struct inode *inode, *dir;
26179 +       struct super_block *sb;
26180 +       aufs_bindex_t bopq, bwh, bfound;
26181 +       unsigned char rename_lock;
26182 +};
26183 +
26184 +#define mvd_errno              mvdown.au_errno
26185 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26186 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26187 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26188 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
26189 +
26190 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
26191 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
26192 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
26193 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
26194 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
26195 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
26196 +
26197 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
26198 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
26199 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
26200 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
26201 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
26202 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
26203 +
26204 +#define AU_MVD_PR(flag, ...) do {                      \
26205 +               if (flag)                               \
26206 +                       pr_err(__VA_ARGS__);            \
26207 +       } while (0)
26208 +
26209 +static int find_lower_writable(struct au_mvd_args *a)
26210 +{
26211 +       struct super_block *sb;
26212 +       aufs_bindex_t bindex, bbot;
26213 +       struct au_branch *br;
26214 +
26215 +       sb = a->sb;
26216 +       bindex = a->mvd_bsrc;
26217 +       bbot = au_sbbot(sb);
26218 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
26219 +               for (bindex++; bindex <= bbot; bindex++) {
26220 +                       br = au_sbr(sb, bindex);
26221 +                       if (au_br_fhsm(br->br_perm)
26222 +                           && !sb_rdonly(au_br_sb(br)))
26223 +                               return bindex;
26224 +               }
26225 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
26226 +               for (bindex++; bindex <= bbot; bindex++) {
26227 +                       br = au_sbr(sb, bindex);
26228 +                       if (!au_br_rdonly(br))
26229 +                               return bindex;
26230 +               }
26231 +       else
26232 +               for (bindex++; bindex <= bbot; bindex++) {
26233 +                       br = au_sbr(sb, bindex);
26234 +                       if (!sb_rdonly(au_br_sb(br))) {
26235 +                               if (au_br_rdonly(br))
26236 +                                       a->mvdown.flags
26237 +                                               |= AUFS_MVDOWN_ROLOWER_R;
26238 +                               return bindex;
26239 +                       }
26240 +               }
26241 +
26242 +       return -1;
26243 +}
26244 +
26245 +/* make the parent dir on bdst */
26246 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
26247 +{
26248 +       int err;
26249 +
26250 +       err = 0;
26251 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26252 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26253 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26254 +       a->mvd_h_dst_parent = NULL;
26255 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
26256 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26257 +       if (!a->mvd_h_dst_parent) {
26258 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26259 +               if (unlikely(err)) {
26260 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
26261 +                       goto out;
26262 +               }
26263 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26264 +       }
26265 +
26266 +out:
26267 +       AuTraceErr(err);
26268 +       return err;
26269 +}
26270 +
26271 +/* lock them all */
26272 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
26273 +{
26274 +       int err;
26275 +       struct dentry *h_trap;
26276 +
26277 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26278 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
26279 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26280 +                    au_opt_udba(a->sb),
26281 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26282 +       AuTraceErr(err);
26283 +       if (unlikely(err)) {
26284 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
26285 +               goto out;
26286 +       }
26287 +
26288 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26289 +               a->rename_lock = 0;
26290 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26291 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26292 +                           au_opt_udba(a->sb),
26293 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26294 +               err = au_do_pin(&a->mvd_pin_src);
26295 +               AuTraceErr(err);
26296 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26297 +               if (unlikely(err)) {
26298 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
26299 +                       goto out_dst;
26300 +               }
26301 +               goto out; /* success */
26302 +       }
26303 +
26304 +       a->rename_lock = 1;
26305 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
26306 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26307 +                    au_opt_udba(a->sb),
26308 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26309 +       AuTraceErr(err);
26310 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26311 +       if (unlikely(err)) {
26312 +               AU_MVD_PR(dmsg, "pin_src failed\n");
26313 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26314 +               goto out_dst;
26315 +       }
26316 +       au_pin_hdir_unlock(&a->mvd_pin_src);
26317 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26318 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
26319 +       if (h_trap) {
26320 +               err = (h_trap != a->mvd_h_src_parent);
26321 +               if (err)
26322 +                       err = (h_trap != a->mvd_h_dst_parent);
26323 +       }
26324 +       BUG_ON(err); /* it should never happen */
26325 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26326 +               err = -EBUSY;
26327 +               AuTraceErr(err);
26328 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26329 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26330 +               au_pin_hdir_lock(&a->mvd_pin_src);
26331 +               au_unpin(&a->mvd_pin_src);
26332 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26333 +               goto out_dst;
26334 +       }
26335 +       goto out; /* success */
26336 +
26337 +out_dst:
26338 +       au_unpin(&a->mvd_pin_dst);
26339 +out:
26340 +       AuTraceErr(err);
26341 +       return err;
26342 +}
26343 +
26344 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
26345 +{
26346 +       if (!a->rename_lock)
26347 +               au_unpin(&a->mvd_pin_src);
26348 +       else {
26349 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26350 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26351 +               au_pin_hdir_lock(&a->mvd_pin_src);
26352 +               au_unpin(&a->mvd_pin_src);
26353 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26354 +       }
26355 +       au_unpin(&a->mvd_pin_dst);
26356 +}
26357 +
26358 +/* copy-down the file */
26359 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
26360 +{
26361 +       int err;
26362 +       struct au_cp_generic cpg = {
26363 +               .dentry = a->dentry,
26364 +               .bdst   = a->mvd_bdst,
26365 +               .bsrc   = a->mvd_bsrc,
26366 +               .len    = -1,
26367 +               .pin    = &a->mvd_pin_dst,
26368 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
26369 +       };
26370 +
26371 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
26372 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26373 +               au_fset_cpup(cpg.flags, OVERWRITE);
26374 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26375 +               au_fset_cpup(cpg.flags, RWDST);
26376 +       err = au_sio_cpdown_simple(&cpg);
26377 +       if (unlikely(err))
26378 +               AU_MVD_PR(dmsg, "cpdown failed\n");
26379 +
26380 +       AuTraceErr(err);
26381 +       return err;
26382 +}
26383 +
26384 +/*
26385 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
26386 + * were sleeping
26387 + */
26388 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
26389 +{
26390 +       int err;
26391 +       struct path h_path;
26392 +       struct au_branch *br;
26393 +       struct inode *delegated;
26394 +
26395 +       br = au_sbr(a->sb, a->mvd_bdst);
26396 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26397 +       err = PTR_ERR(h_path.dentry);
26398 +       if (IS_ERR(h_path.dentry)) {
26399 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
26400 +               goto out;
26401 +       }
26402 +
26403 +       err = 0;
26404 +       if (d_is_positive(h_path.dentry)) {
26405 +               h_path.mnt = au_br_mnt(br);
26406 +               delegated = NULL;
26407 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
26408 +                                  &delegated, /*force*/0);
26409 +               if (unlikely(err == -EWOULDBLOCK)) {
26410 +                       pr_warn("cannot retry for NFSv4 delegation"
26411 +                               " for an internal unlink\n");
26412 +                       iput(delegated);
26413 +               }
26414 +               if (unlikely(err))
26415 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
26416 +       }
26417 +       dput(h_path.dentry);
26418 +
26419 +out:
26420 +       AuTraceErr(err);
26421 +       return err;
26422 +}
26423 +
26424 +/*
26425 + * unlink the topmost h_dentry
26426 + */
26427 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
26428 +{
26429 +       int err;
26430 +       struct path h_path;
26431 +       struct inode *delegated;
26432 +
26433 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26434 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26435 +       delegated = NULL;
26436 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26437 +       if (unlikely(err == -EWOULDBLOCK)) {
26438 +               pr_warn("cannot retry for NFSv4 delegation"
26439 +                       " for an internal unlink\n");
26440 +               iput(delegated);
26441 +       }
26442 +       if (unlikely(err))
26443 +               AU_MVD_PR(dmsg, "unlink failed\n");
26444 +
26445 +       AuTraceErr(err);
26446 +       return err;
26447 +}
26448 +
26449 +/* Since mvdown succeeded, we ignore an error of this function */
26450 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26451 +{
26452 +       int err;
26453 +       struct au_branch *br;
26454 +
26455 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26456 +       br = au_sbr(a->sb, a->mvd_bsrc);
26457 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26458 +       if (!err) {
26459 +               br = au_sbr(a->sb, a->mvd_bdst);
26460 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26461 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26462 +       }
26463 +       if (!err)
26464 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26465 +       else
26466 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26467 +}
26468 +
26469 +/*
26470 + * copy-down the file and unlink the bsrc file.
26471 + * - unlink the bdst whout if exist
26472 + * - copy-down the file (with whtmp name and rename)
26473 + * - unlink the bsrc file
26474 + */
26475 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26476 +{
26477 +       int err;
26478 +
26479 +       err = au_do_mkdir(dmsg, a);
26480 +       if (!err)
26481 +               err = au_do_lock(dmsg, a);
26482 +       if (unlikely(err))
26483 +               goto out;
26484 +
26485 +       /*
26486 +        * do not revert the activities we made on bdst since they should be
26487 +        * harmless in aufs.
26488 +        */
26489 +
26490 +       err = au_do_cpdown(dmsg, a);
26491 +       if (!err)
26492 +               err = au_do_unlink_wh(dmsg, a);
26493 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26494 +               err = au_do_unlink(dmsg, a);
26495 +       if (unlikely(err))
26496 +               goto out_unlock;
26497 +
26498 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
26499 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26500 +       if (find_lower_writable(a) < 0)
26501 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26502 +
26503 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26504 +               au_do_stfs(dmsg, a);
26505 +
26506 +       /* maintain internal array */
26507 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26508 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26509 +               au_set_dbtop(a->dentry, a->mvd_bdst);
26510 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26511 +               au_set_ibtop(a->inode, a->mvd_bdst);
26512 +       } else {
26513 +               /* hide the lower */
26514 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26515 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
26516 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26517 +               au_set_ibbot(a->inode, a->mvd_bsrc);
26518 +       }
26519 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
26520 +               au_set_dbbot(a->dentry, a->mvd_bdst);
26521 +       if (au_ibbot(a->inode) < a->mvd_bdst)
26522 +               au_set_ibbot(a->inode, a->mvd_bdst);
26523 +
26524 +out_unlock:
26525 +       au_do_unlock(dmsg, a);
26526 +out:
26527 +       AuTraceErr(err);
26528 +       return err;
26529 +}
26530 +
26531 +/* ---------------------------------------------------------------------- */
26532 +
26533 +/* make sure the file is idle */
26534 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26535 +{
26536 +       int err, plinked;
26537 +
26538 +       err = 0;
26539 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26540 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
26541 +           && au_dcount(a->dentry) == 1
26542 +           && atomic_read(&a->inode->i_count) == 1
26543 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
26544 +           && (!plinked || !au_plink_test(a->inode))
26545 +           && a->inode->i_nlink == 1)
26546 +               goto out;
26547 +
26548 +       err = -EBUSY;
26549 +       AU_MVD_PR(dmsg,
26550 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26551 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26552 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
26553 +                 a->mvd_h_src_inode->i_nlink,
26554 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
26555 +
26556 +out:
26557 +       AuTraceErr(err);
26558 +       return err;
26559 +}
26560 +
26561 +/* make sure the parent dir is fine */
26562 +static int au_mvd_args_parent(const unsigned char dmsg,
26563 +                             struct au_mvd_args *a)
26564 +{
26565 +       int err;
26566 +       aufs_bindex_t bindex;
26567 +
26568 +       err = 0;
26569 +       if (unlikely(au_alive_dir(a->parent))) {
26570 +               err = -ENOENT;
26571 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
26572 +               goto out;
26573 +       }
26574 +
26575 +       a->bopq = au_dbdiropq(a->parent);
26576 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26577 +       AuDbg("b%d\n", bindex);
26578 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26579 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26580 +               err = -EINVAL;
26581 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
26582 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26583 +                         a->bopq, a->mvd_bdst);
26584 +       }
26585 +
26586 +out:
26587 +       AuTraceErr(err);
26588 +       return err;
26589 +}
26590 +
26591 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26592 +                                   struct au_mvd_args *a)
26593 +{
26594 +       int err;
26595 +       struct au_dinfo *dinfo, *tmp;
26596 +
26597 +       /* lookup the next lower positive entry */
26598 +       err = -ENOMEM;
26599 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26600 +       if (unlikely(!tmp))
26601 +               goto out;
26602 +
26603 +       a->bfound = -1;
26604 +       a->bwh = -1;
26605 +       dinfo = au_di(a->dentry);
26606 +       au_di_cp(tmp, dinfo);
26607 +       au_di_swap(tmp, dinfo);
26608 +
26609 +       /* returns the number of positive dentries */
26610 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26611 +                            /* AuLkup_IGNORE_PERM */ 0);
26612 +       if (!err)
26613 +               a->bwh = au_dbwh(a->dentry);
26614 +       else if (err > 0)
26615 +               a->bfound = au_dbtop(a->dentry);
26616 +
26617 +       au_di_swap(tmp, dinfo);
26618 +       au_rw_write_unlock(&tmp->di_rwsem);
26619 +       au_di_free(tmp);
26620 +       if (unlikely(err < 0))
26621 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
26622 +
26623 +       /*
26624 +        * here, we have these cases.
26625 +        * bfound == -1
26626 +        *      no positive dentry under bsrc. there are more sub-cases.
26627 +        *      bwh < 0
26628 +        *              there no whiteout, we can safely move-down.
26629 +        *      bwh <= bsrc
26630 +        *              impossible
26631 +        *      bsrc < bwh && bwh < bdst
26632 +        *              there is a whiteout on RO branch. cannot proceed.
26633 +        *      bwh == bdst
26634 +        *              there is a whiteout on the RW target branch. it should
26635 +        *              be removed.
26636 +        *      bdst < bwh
26637 +        *              there is a whiteout somewhere unrelated branch.
26638 +        * -1 < bfound && bfound <= bsrc
26639 +        *      impossible.
26640 +        * bfound < bdst
26641 +        *      found, but it is on RO branch between bsrc and bdst. cannot
26642 +        *      proceed.
26643 +        * bfound == bdst
26644 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26645 +        *      error.
26646 +        * bdst < bfound
26647 +        *      found, after we create the file on bdst, it will be hidden.
26648 +        */
26649 +
26650 +       AuDebugOn(a->bfound == -1
26651 +                 && a->bwh != -1
26652 +                 && a->bwh <= a->mvd_bsrc);
26653 +       AuDebugOn(-1 < a->bfound
26654 +                 && a->bfound <= a->mvd_bsrc);
26655 +
26656 +       err = -EINVAL;
26657 +       if (a->bfound == -1
26658 +           && a->mvd_bsrc < a->bwh
26659 +           && a->bwh != -1
26660 +           && a->bwh < a->mvd_bdst) {
26661 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26662 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26663 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26664 +               goto out;
26665 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26666 +               a->mvd_errno = EAU_MVDOWN_UPPER;
26667 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26668 +                         a->mvd_bdst, a->bfound);
26669 +               goto out;
26670 +       }
26671 +
26672 +       err = 0; /* success */
26673 +
26674 +out:
26675 +       AuTraceErr(err);
26676 +       return err;
26677 +}
26678 +
26679 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26680 +{
26681 +       int err;
26682 +
26683 +       err = 0;
26684 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26685 +           && a->bfound == a->mvd_bdst)
26686 +               err = -EEXIST;
26687 +       AuTraceErr(err);
26688 +       return err;
26689 +}
26690 +
26691 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26692 +{
26693 +       int err;
26694 +       struct au_branch *br;
26695 +
26696 +       err = -EISDIR;
26697 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
26698 +               goto out;
26699 +
26700 +       err = -EINVAL;
26701 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26702 +               a->mvd_bsrc = au_ibtop(a->inode);
26703 +       else {
26704 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26705 +               if (unlikely(a->mvd_bsrc < 0
26706 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
26707 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
26708 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
26709 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
26710 +                                || au_ibbot(a->inode) < a->mvd_bsrc
26711 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26712 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
26713 +                       AU_MVD_PR(dmsg, "no upper\n");
26714 +                       goto out;
26715 +               }
26716 +       }
26717 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26718 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
26719 +               AU_MVD_PR(dmsg, "on the bottom\n");
26720 +               goto out;
26721 +       }
26722 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26723 +       br = au_sbr(a->sb, a->mvd_bsrc);
26724 +       err = au_br_rdonly(br);
26725 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26726 +               if (unlikely(err))
26727 +                       goto out;
26728 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26729 +                    || IS_APPEND(a->mvd_h_src_inode))) {
26730 +               if (err)
26731 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26732 +               /* go on */
26733 +       } else
26734 +               goto out;
26735 +
26736 +       err = -EINVAL;
26737 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26738 +               a->mvd_bdst = find_lower_writable(a);
26739 +               if (unlikely(a->mvd_bdst < 0)) {
26740 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
26741 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
26742 +                       goto out;
26743 +               }
26744 +       } else {
26745 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26746 +               if (unlikely(a->mvd_bdst < 0
26747 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
26748 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26749 +                       AU_MVD_PR(dmsg, "no lower brid\n");
26750 +                       goto out;
26751 +               }
26752 +       }
26753 +
26754 +       err = au_mvd_args_busy(dmsg, a);
26755 +       if (!err)
26756 +               err = au_mvd_args_parent(dmsg, a);
26757 +       if (!err)
26758 +               err = au_mvd_args_intermediate(dmsg, a);
26759 +       if (!err)
26760 +               err = au_mvd_args_exist(dmsg, a);
26761 +       if (!err)
26762 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26763 +
26764 +out:
26765 +       AuTraceErr(err);
26766 +       return err;
26767 +}
26768 +
26769 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26770 +{
26771 +       int err, e;
26772 +       unsigned char dmsg;
26773 +       struct au_mvd_args *args;
26774 +       struct inode *inode;
26775 +
26776 +       inode = d_inode(dentry);
26777 +       err = -EPERM;
26778 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26779 +               goto out;
26780 +
26781 +       err = -ENOMEM;
26782 +       args = kmalloc(sizeof(*args), GFP_NOFS);
26783 +       if (unlikely(!args))
26784 +               goto out;
26785 +
26786 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26787 +       if (!err)
26788 +               /* VERIFY_WRITE */
26789 +               err = !access_ok(uarg, sizeof(*uarg));
26790 +       if (unlikely(err)) {
26791 +               err = -EFAULT;
26792 +               AuTraceErr(err);
26793 +               goto out_free;
26794 +       }
26795 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
26796 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26797 +       args->mvdown.au_errno = 0;
26798 +       args->dentry = dentry;
26799 +       args->inode = inode;
26800 +       args->sb = dentry->d_sb;
26801 +
26802 +       err = -ENOENT;
26803 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26804 +       args->parent = dget_parent(dentry);
26805 +       args->dir = d_inode(args->parent);
26806 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
26807 +       dput(args->parent);
26808 +       if (unlikely(args->parent != dentry->d_parent)) {
26809 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
26810 +               goto out_dir;
26811 +       }
26812 +
26813 +       inode_lock_nested(inode, I_MUTEX_CHILD);
26814 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26815 +       if (unlikely(err))
26816 +               goto out_inode;
26817 +
26818 +       di_write_lock_parent(args->parent);
26819 +       err = au_mvd_args(dmsg, args);
26820 +       if (unlikely(err))
26821 +               goto out_parent;
26822 +
26823 +       err = au_do_mvdown(dmsg, args);
26824 +       if (unlikely(err))
26825 +               goto out_parent;
26826 +
26827 +       au_cpup_attr_timesizes(args->dir);
26828 +       au_cpup_attr_timesizes(inode);
26829 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26830 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26831 +       /* au_digen_dec(dentry); */
26832 +
26833 +out_parent:
26834 +       di_write_unlock(args->parent);
26835 +       aufs_read_unlock(dentry, AuLock_DW);
26836 +out_inode:
26837 +       inode_unlock(inode);
26838 +out_dir:
26839 +       inode_unlock(args->dir);
26840 +out_free:
26841 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26842 +       if (unlikely(e))
26843 +               err = -EFAULT;
26844 +       au_kfree_rcu(args);
26845 +out:
26846 +       AuTraceErr(err);
26847 +       return err;
26848 +}
26849 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
26850 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
26851 +++ linux/fs/aufs/opts.c        2022-08-02 12:12:45.165641939 +0200
26852 @@ -0,0 +1,1032 @@
26853 +// SPDX-License-Identifier: GPL-2.0
26854 +/*
26855 + * Copyright (C) 2005-2021 Junjiro R. Okajima
26856 + *
26857 + * This program, aufs is free software; you can redistribute it and/or modify
26858 + * it under the terms of the GNU General Public License as published by
26859 + * the Free Software Foundation; either version 2 of the License, or
26860 + * (at your option) any later version.
26861 + *
26862 + * This program is distributed in the hope that it will be useful,
26863 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26864 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26865 + * GNU General Public License for more details.
26866 + *
26867 + * You should have received a copy of the GNU General Public License
26868 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26869 + */
26870 +
26871 +/*
26872 + * mount options/flags
26873 + */
26874 +
26875 +#include <linux/types.h> /* a distribution requires */
26876 +#include <linux/parser.h>
26877 +#include "aufs.h"
26878 +
26879 +/* ---------------------------------------------------------------------- */
26880 +
26881 +static const char *au_parser_pattern(int val, match_table_t tbl)
26882 +{
26883 +       struct match_token *p;
26884 +
26885 +       p = tbl;
26886 +       while (p->pattern) {
26887 +               if (p->token == val)
26888 +                       return p->pattern;
26889 +               p++;
26890 +       }
26891 +       BUG();
26892 +       return "??";
26893 +}
26894 +
26895 +static const char *au_optstr(int *val, match_table_t tbl)
26896 +{
26897 +       struct match_token *p;
26898 +       int v;
26899 +
26900 +       v = *val;
26901 +       if (!v)
26902 +               goto out;
26903 +       p = tbl;
26904 +       while (p->pattern) {
26905 +               if (p->token
26906 +                   && (v & p->token) == p->token) {
26907 +                       *val &= ~p->token;
26908 +                       return p->pattern;
26909 +               }
26910 +               p++;
26911 +       }
26912 +
26913 +out:
26914 +       return NULL;
26915 +}
26916 +
26917 +/* ---------------------------------------------------------------------- */
26918 +
26919 +static match_table_t brperm = {
26920 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
26921 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
26922 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
26923 +       {0, NULL}
26924 +};
26925 +
26926 +static match_table_t brattr = {
26927 +       /* general */
26928 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26929 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
26930 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
26931 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
26932 +#ifdef CONFIG_AUFS_FHSM
26933 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
26934 +#endif
26935 +#ifdef CONFIG_AUFS_XATTR
26936 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26937 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26938 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26939 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26940 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26941 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
26942 +#endif
26943 +
26944 +       /* ro/rr branch */
26945 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
26946 +
26947 +       /* rw branch */
26948 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
26949 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
26950 +
26951 +       {0, NULL}
26952 +};
26953 +
26954 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
26955 +{
26956 +       int attr, v;
26957 +       char *p;
26958 +
26959 +       attr = 0;
26960 +       do {
26961 +               p = strchr(str, '+');
26962 +               if (p)
26963 +                       *p = 0;
26964 +               v = match_token(str, table, args);
26965 +               if (v) {
26966 +                       if (v & AuBrAttr_CMOO_Mask)
26967 +                               attr &= ~AuBrAttr_CMOO_Mask;
26968 +                       attr |= v;
26969 +               } else {
26970 +                       if (p)
26971 +                               *p = '+';
26972 +                       pr_warn("ignored branch attribute %s\n", str);
26973 +                       break;
26974 +               }
26975 +               if (p)
26976 +                       str = p + 1;
26977 +       } while (p);
26978 +
26979 +       return attr;
26980 +}
26981 +
26982 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
26983 +{
26984 +       int sz;
26985 +       const char *p;
26986 +       char *q;
26987 +
26988 +       q = str->a;
26989 +       *q = 0;
26990 +       p = au_optstr(&perm, brattr);
26991 +       if (p) {
26992 +               sz = strlen(p);
26993 +               memcpy(q, p, sz + 1);
26994 +               q += sz;
26995 +       } else
26996 +               goto out;
26997 +
26998 +       do {
26999 +               p = au_optstr(&perm, brattr);
27000 +               if (p) {
27001 +                       *q++ = '+';
27002 +                       sz = strlen(p);
27003 +                       memcpy(q, p, sz + 1);
27004 +                       q += sz;
27005 +               }
27006 +       } while (p);
27007 +
27008 +out:
27009 +       return q - str->a;
27010 +}
27011 +
27012 +int au_br_perm_val(char *perm)
27013 +{
27014 +       int val, bad, sz;
27015 +       char *p;
27016 +       substring_t args[MAX_OPT_ARGS];
27017 +       au_br_perm_str_t attr;
27018 +
27019 +       p = strchr(perm, '+');
27020 +       if (p)
27021 +               *p = 0;
27022 +       val = match_token(perm, brperm, args);
27023 +       if (!val) {
27024 +               if (p)
27025 +                       *p = '+';
27026 +               pr_warn("ignored branch permission %s\n", perm);
27027 +               val = AuBrPerm_RO;
27028 +               goto out;
27029 +       }
27030 +       if (!p)
27031 +               goto out;
27032 +
27033 +       val |= br_attr_val(p + 1, brattr, args);
27034 +
27035 +       bad = 0;
27036 +       switch (val & AuBrPerm_Mask) {
27037 +       case AuBrPerm_RO:
27038 +       case AuBrPerm_RR:
27039 +               bad = val & AuBrWAttr_Mask;
27040 +               val &= ~AuBrWAttr_Mask;
27041 +               break;
27042 +       case AuBrPerm_RW:
27043 +               bad = val & AuBrRAttr_Mask;
27044 +               val &= ~AuBrRAttr_Mask;
27045 +               break;
27046 +       }
27047 +
27048 +       /*
27049 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27050 +        * does not treat it as an error, just warning.
27051 +        * this is a tiny guard for the user operation.
27052 +        */
27053 +       if (val & AuBrAttr_UNPIN) {
27054 +               bad |= AuBrAttr_UNPIN;
27055 +               val &= ~AuBrAttr_UNPIN;
27056 +       }
27057 +
27058 +       if (unlikely(bad)) {
27059 +               sz = au_do_optstr_br_attr(&attr, bad);
27060 +               AuDebugOn(!sz);
27061 +               pr_warn("ignored branch attribute %s\n", attr.a);
27062 +       }
27063 +
27064 +out:
27065 +       return val;
27066 +}
27067 +
27068 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
27069 +{
27070 +       au_br_perm_str_t attr;
27071 +       const char *p;
27072 +       char *q;
27073 +       int sz;
27074 +
27075 +       q = str->a;
27076 +       p = au_optstr(&perm, brperm);
27077 +       AuDebugOn(!p || !*p);
27078 +       sz = strlen(p);
27079 +       memcpy(q, p, sz + 1);
27080 +       q += sz;
27081 +
27082 +       sz = au_do_optstr_br_attr(&attr, perm);
27083 +       if (sz) {
27084 +               *q++ = '+';
27085 +               memcpy(q, attr.a, sz + 1);
27086 +       }
27087 +
27088 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
27089 +}
27090 +
27091 +/* ---------------------------------------------------------------------- */
27092 +
27093 +static match_table_t udbalevel = {
27094 +       {AuOpt_UDBA_REVAL, "reval"},
27095 +       {AuOpt_UDBA_NONE, "none"},
27096 +#ifdef CONFIG_AUFS_HNOTIFY
27097 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27098 +#ifdef CONFIG_AUFS_HFSNOTIFY
27099 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
27100 +#endif
27101 +#endif
27102 +       {-1, NULL}
27103 +};
27104 +
27105 +int au_udba_val(char *str)
27106 +{
27107 +       substring_t args[MAX_OPT_ARGS];
27108 +
27109 +       return match_token(str, udbalevel, args);
27110 +}
27111 +
27112 +const char *au_optstr_udba(int udba)
27113 +{
27114 +       return au_parser_pattern(udba, udbalevel);
27115 +}
27116 +
27117 +/* ---------------------------------------------------------------------- */
27118 +
27119 +static match_table_t au_wbr_create_policy = {
27120 +       {AuWbrCreate_TDP, "tdp"},
27121 +       {AuWbrCreate_TDP, "top-down-parent"},
27122 +       {AuWbrCreate_RR, "rr"},
27123 +       {AuWbrCreate_RR, "round-robin"},
27124 +       {AuWbrCreate_MFS, "mfs"},
27125 +       {AuWbrCreate_MFS, "most-free-space"},
27126 +       {AuWbrCreate_MFSV, "mfs:%d"},
27127 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
27128 +
27129 +       /* top-down regardless the parent, and then mfs */
27130 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
27131 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27132 +
27133 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
27134 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27135 +       {AuWbrCreate_PMFS, "pmfs"},
27136 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
27137 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27138 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
27139 +
27140 +       {-1, NULL}
27141 +};
27142 +
27143 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27144 +                           struct au_opt_wbr_create *create)
27145 +{
27146 +       int err;
27147 +       unsigned long long ull;
27148 +
27149 +       err = 0;
27150 +       if (!match_u64(arg, &ull))
27151 +               create->mfsrr_watermark = ull;
27152 +       else {
27153 +               pr_err("bad integer in %s\n", str);
27154 +               err = -EINVAL;
27155 +       }
27156 +
27157 +       return err;
27158 +}
27159 +
27160 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
27161 +                         struct au_opt_wbr_create *create)
27162 +{
27163 +       int n, err;
27164 +
27165 +       err = 0;
27166 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
27167 +               create->mfs_second = n;
27168 +       else {
27169 +               pr_err("bad integer in %s\n", str);
27170 +               err = -EINVAL;
27171 +       }
27172 +
27173 +       return err;
27174 +}
27175 +
27176 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
27177 +{
27178 +       int err, e;
27179 +       substring_t args[MAX_OPT_ARGS];
27180 +
27181 +       err = match_token(str, au_wbr_create_policy, args);
27182 +       create->wbr_create = err;
27183 +       switch (err) {
27184 +       case AuWbrCreate_MFSRRV:
27185 +       case AuWbrCreate_TDMFSV:
27186 +       case AuWbrCreate_PMFSRRV:
27187 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27188 +               if (!e)
27189 +                       e = au_wbr_mfs_sec(&args[1], str, create);
27190 +               if (unlikely(e))
27191 +                       err = e;
27192 +               break;
27193 +       case AuWbrCreate_MFSRR:
27194 +       case AuWbrCreate_TDMFS:
27195 +       case AuWbrCreate_PMFSRR:
27196 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27197 +               if (unlikely(e)) {
27198 +                       err = e;
27199 +                       break;
27200 +               }
27201 +               fallthrough;
27202 +       case AuWbrCreate_MFS:
27203 +       case AuWbrCreate_PMFS:
27204 +               create->mfs_second = AUFS_MFS_DEF_SEC;
27205 +               break;
27206 +       case AuWbrCreate_MFSV:
27207 +       case AuWbrCreate_PMFSV:
27208 +               e = au_wbr_mfs_sec(&args[0], str, create);
27209 +               if (unlikely(e))
27210 +                       err = e;
27211 +               break;
27212 +       }
27213 +
27214 +       return err;
27215 +}
27216 +
27217 +const char *au_optstr_wbr_create(int wbr_create)
27218 +{
27219 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
27220 +}
27221 +
27222 +static match_table_t au_wbr_copyup_policy = {
27223 +       {AuWbrCopyup_TDP, "tdp"},
27224 +       {AuWbrCopyup_TDP, "top-down-parent"},
27225 +       {AuWbrCopyup_BUP, "bup"},
27226 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
27227 +       {AuWbrCopyup_BU, "bu"},
27228 +       {AuWbrCopyup_BU, "bottom-up"},
27229 +       {-1, NULL}
27230 +};
27231 +
27232 +int au_wbr_copyup_val(char *str)
27233 +{
27234 +       substring_t args[MAX_OPT_ARGS];
27235 +
27236 +       return match_token(str, au_wbr_copyup_policy, args);
27237 +}
27238 +
27239 +const char *au_optstr_wbr_copyup(int wbr_copyup)
27240 +{
27241 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
27242 +}
27243 +
27244 +/* ---------------------------------------------------------------------- */
27245 +
27246 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27247 +              aufs_bindex_t bindex)
27248 +{
27249 +       int err;
27250 +       struct au_opt_add *add = &opt->add;
27251 +       char *p;
27252 +
27253 +       add->bindex = bindex;
27254 +       add->perm = AuBrPerm_RO;
27255 +       add->pathname = opt_str;
27256 +       p = strchr(opt_str, '=');
27257 +       if (p) {
27258 +               *p++ = 0;
27259 +               if (*p)
27260 +                       add->perm = au_br_perm_val(p);
27261 +       }
27262 +
27263 +       err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
27264 +       if (!err) {
27265 +               if (!p) {
27266 +                       add->perm = AuBrPerm_RO;
27267 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
27268 +                               add->perm = AuBrPerm_RR;
27269 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
27270 +                               add->perm = AuBrPerm_RW;
27271 +               }
27272 +               opt->type = Opt_add;
27273 +               goto out;
27274 +       }
27275 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
27276 +       err = -EINVAL;
27277 +
27278 +out:
27279 +       return err;
27280 +}
27281 +
27282 +static int au_opt_wbr_create(struct super_block *sb,
27283 +                            struct au_opt_wbr_create *create)
27284 +{
27285 +       int err;
27286 +       struct au_sbinfo *sbinfo;
27287 +
27288 +       SiMustWriteLock(sb);
27289 +
27290 +       err = 1; /* handled */
27291 +       sbinfo = au_sbi(sb);
27292 +       if (sbinfo->si_wbr_create_ops->fin) {
27293 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27294 +               if (!err)
27295 +                       err = 1;
27296 +       }
27297 +
27298 +       sbinfo->si_wbr_create = create->wbr_create;
27299 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27300 +       switch (create->wbr_create) {
27301 +       case AuWbrCreate_MFSRRV:
27302 +       case AuWbrCreate_MFSRR:
27303 +       case AuWbrCreate_TDMFS:
27304 +       case AuWbrCreate_TDMFSV:
27305 +       case AuWbrCreate_PMFSRR:
27306 +       case AuWbrCreate_PMFSRRV:
27307 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27308 +               fallthrough;
27309 +       case AuWbrCreate_MFS:
27310 +       case AuWbrCreate_MFSV:
27311 +       case AuWbrCreate_PMFS:
27312 +       case AuWbrCreate_PMFSV:
27313 +               sbinfo->si_wbr_mfs.mfs_expire
27314 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27315 +               break;
27316 +       }
27317 +
27318 +       if (sbinfo->si_wbr_create_ops->init)
27319 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27320 +
27321 +       return err;
27322 +}
27323 +
27324 +/*
27325 + * returns,
27326 + * plus: processed without an error
27327 + * zero: unprocessed
27328 + */
27329 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27330 +                        struct au_opts *opts)
27331 +{
27332 +       int err;
27333 +       struct au_sbinfo *sbinfo;
27334 +
27335 +       SiMustWriteLock(sb);
27336 +
27337 +       err = 1; /* handled */
27338 +       sbinfo = au_sbi(sb);
27339 +       switch (opt->type) {
27340 +       case Opt_udba:
27341 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27342 +               sbinfo->si_mntflags |= opt->udba;
27343 +               opts->given_udba |= opt->udba;
27344 +               break;
27345 +
27346 +       case Opt_plink:
27347 +               if (opt->tf)
27348 +                       au_opt_set(sbinfo->si_mntflags, PLINK);
27349 +               else {
27350 +                       if (au_opt_test(sbinfo->si_mntflags, PLINK))
27351 +                               au_plink_put(sb, /*verbose*/1);
27352 +                       au_opt_clr(sbinfo->si_mntflags, PLINK);
27353 +               }
27354 +               break;
27355 +       case Opt_list_plink:
27356 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27357 +                       au_plink_list(sb);
27358 +               break;
27359 +
27360 +       case Opt_dio:
27361 +               if (opt->tf) {
27362 +                       au_opt_set(sbinfo->si_mntflags, DIO);
27363 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27364 +               } else {
27365 +                       au_opt_clr(sbinfo->si_mntflags, DIO);
27366 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27367 +               }
27368 +               break;
27369 +
27370 +       case Opt_fhsm_sec:
27371 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27372 +               break;
27373 +
27374 +       case Opt_diropq_a:
27375 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27376 +               break;
27377 +       case Opt_diropq_w:
27378 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27379 +               break;
27380 +
27381 +       case Opt_warn_perm:
27382 +               if (opt->tf)
27383 +                       au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27384 +               else
27385 +                       au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27386 +               break;
27387 +
27388 +       case Opt_verbose:
27389 +               if (opt->tf)
27390 +                       au_opt_set(sbinfo->si_mntflags, VERBOSE);
27391 +               else
27392 +                       au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27393 +               break;
27394 +
27395 +       case Opt_sum:
27396 +               if (opt->tf)
27397 +                       au_opt_set(sbinfo->si_mntflags, SUM);
27398 +               else {
27399 +                       au_opt_clr(sbinfo->si_mntflags, SUM);
27400 +                       au_opt_clr(sbinfo->si_mntflags, SUM_W);
27401 +               }
27402 +               break;
27403 +       case Opt_wsum:
27404 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27405 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27406 +               break;
27407 +
27408 +       case Opt_wbr_create:
27409 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27410 +               break;
27411 +       case Opt_wbr_copyup:
27412 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27413 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27414 +               break;
27415 +
27416 +       case Opt_dirwh:
27417 +               sbinfo->si_dirwh = opt->dirwh;
27418 +               break;
27419 +
27420 +       case Opt_rdcache:
27421 +               sbinfo->si_rdcache
27422 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27423 +               break;
27424 +       case Opt_rdblk:
27425 +               sbinfo->si_rdblk = opt->rdblk;
27426 +               break;
27427 +       case Opt_rdhash:
27428 +               sbinfo->si_rdhash = opt->rdhash;
27429 +               break;
27430 +
27431 +       case Opt_shwh:
27432 +               if (opt->tf)
27433 +                       au_opt_set(sbinfo->si_mntflags, SHWH);
27434 +               else
27435 +                       au_opt_clr(sbinfo->si_mntflags, SHWH);
27436 +               break;
27437 +
27438 +       case Opt_dirperm1:
27439 +               if (opt->tf)
27440 +                       au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27441 +               else
27442 +                       au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27443 +               break;
27444 +
27445 +       case Opt_trunc_xino:
27446 +               if (opt->tf)
27447 +                       au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27448 +               else
27449 +                       au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27450 +               break;
27451 +
27452 +       case Opt_trunc_xino_path:
27453 +       case Opt_itrunc_xino:
27454 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27455 +                                   /*idx_begin*/0);
27456 +               if (!err)
27457 +                       err = 1;
27458 +               break;
27459 +
27460 +       case Opt_trunc_xib:
27461 +               if (opt->tf)
27462 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27463 +               else
27464 +                       au_fclr_opts(opts->flags, TRUNC_XIB);
27465 +               break;
27466 +
27467 +       case Opt_dirren:
27468 +               err = 1;
27469 +               if (opt->tf) {
27470 +                       if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27471 +                               err = au_dr_opt_set(sb);
27472 +                               if (!err)
27473 +                                       err = 1;
27474 +                       }
27475 +                       if (err == 1)
27476 +                               au_opt_set(sbinfo->si_mntflags, DIRREN);
27477 +               } else {
27478 +                       if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27479 +                               err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27480 +                                                                     DR_FLUSHED));
27481 +                               if (!err)
27482 +                                       err = 1;
27483 +                       }
27484 +                       if (err == 1)
27485 +                               au_opt_clr(sbinfo->si_mntflags, DIRREN);
27486 +               }
27487 +               break;
27488 +
27489 +       case Opt_acl:
27490 +               if (opt->tf)
27491 +                       sb->s_flags |= SB_POSIXACL;
27492 +               else
27493 +                       sb->s_flags &= ~SB_POSIXACL;
27494 +               break;
27495 +
27496 +       default:
27497 +               err = 0;
27498 +               break;
27499 +       }
27500 +
27501 +       return err;
27502 +}
27503 +
27504 +/*
27505 + * returns tri-state.
27506 + * plus: processed without an error
27507 + * zero: unprocessed
27508 + * minus: error
27509 + */
27510 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27511 +                    struct au_opts *opts)
27512 +{
27513 +       int err, do_refresh;
27514 +
27515 +       err = 0;
27516 +       switch (opt->type) {
27517 +       case Opt_append:
27518 +               opt->add.bindex = au_sbbot(sb) + 1;
27519 +               if (opt->add.bindex < 0)
27520 +                       opt->add.bindex = 0;
27521 +               goto add;
27522 +               /* Always goto add, not fallthrough */
27523 +       case Opt_prepend:
27524 +               opt->add.bindex = 0;
27525 +               fallthrough;
27526 +       add: /* indented label */
27527 +       case Opt_add:
27528 +               err = au_br_add(sb, &opt->add,
27529 +                               au_ftest_opts(opts->flags, REMOUNT));
27530 +               if (!err) {
27531 +                       err = 1;
27532 +                       au_fset_opts(opts->flags, REFRESH);
27533 +               }
27534 +               break;
27535 +
27536 +       case Opt_del:
27537 +       case Opt_idel:
27538 +               err = au_br_del(sb, &opt->del,
27539 +                               au_ftest_opts(opts->flags, REMOUNT));
27540 +               if (!err) {
27541 +                       err = 1;
27542 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27543 +                       au_fset_opts(opts->flags, REFRESH);
27544 +               }
27545 +               break;
27546 +
27547 +       case Opt_mod:
27548 +       case Opt_imod:
27549 +               err = au_br_mod(sb, &opt->mod,
27550 +                               au_ftest_opts(opts->flags, REMOUNT),
27551 +                               &do_refresh);
27552 +               if (!err) {
27553 +                       err = 1;
27554 +                       if (do_refresh)
27555 +                               au_fset_opts(opts->flags, REFRESH);
27556 +               }
27557 +               break;
27558 +       }
27559 +       return err;
27560 +}
27561 +
27562 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27563 +                      struct au_opt_xino **opt_xino,
27564 +                      struct au_opts *opts)
27565 +{
27566 +       int err;
27567 +
27568 +       err = 0;
27569 +       switch (opt->type) {
27570 +       case Opt_xino:
27571 +               err = au_xino_set(sb, &opt->xino,
27572 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27573 +               if (!err)
27574 +                       *opt_xino = &opt->xino;
27575 +               break;
27576 +       case Opt_noxino:
27577 +               au_xino_clr(sb);
27578 +               *opt_xino = (void *)-1;
27579 +               break;
27580 +       }
27581 +
27582 +       return err;
27583 +}
27584 +
27585 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27586 +                  unsigned int pending)
27587 +{
27588 +       int err, fhsm;
27589 +       aufs_bindex_t bindex, bbot;
27590 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27591 +       struct au_branch *br;
27592 +       struct au_wbr *wbr;
27593 +       struct dentry *root, *dentry;
27594 +       struct inode *dir, *h_dir;
27595 +       struct au_sbinfo *sbinfo;
27596 +       struct au_hinode *hdir;
27597 +
27598 +       SiMustAnyLock(sb);
27599 +
27600 +       sbinfo = au_sbi(sb);
27601 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27602 +
27603 +       if (!(sb_flags & SB_RDONLY)) {
27604 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27605 +                       pr_warn("first branch should be rw\n");
27606 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27607 +                       pr_warn_once("shwh should be used with ro\n");
27608 +       }
27609 +
27610 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27611 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27612 +               pr_warn_once("udba=*notify requires xino\n");
27613 +
27614 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27615 +               pr_warn_once("dirperm1 breaks the protection"
27616 +                            " by the permission bits on the lower branch\n");
27617 +
27618 +       err = 0;
27619 +       fhsm = 0;
27620 +       root = sb->s_root;
27621 +       dir = d_inode(root);
27622 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27623 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27624 +                                     UDBA_NONE);
27625 +       bbot = au_sbbot(sb);
27626 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27627 +               skip = 0;
27628 +               h_dir = au_h_iptr(dir, bindex);
27629 +               br = au_sbr(sb, bindex);
27630 +
27631 +               if ((br->br_perm & AuBrAttr_ICEX)
27632 +                   && !h_dir->i_op->listxattr)
27633 +                       br->br_perm &= ~AuBrAttr_ICEX;
27634 +#if 0 /* untested */
27635 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27636 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27637 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27638 +#endif
27639 +
27640 +               do_free = 0;
27641 +               wbr = br->br_wbr;
27642 +               if (wbr)
27643 +                       wbr_wh_read_lock(wbr);
27644 +
27645 +               if (!au_br_writable(br->br_perm)) {
27646 +                       do_free = !!wbr;
27647 +                       skip = (!wbr
27648 +                               || (!wbr->wbr_whbase
27649 +                                   && !wbr->wbr_plink
27650 +                                   && !wbr->wbr_orph));
27651 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27652 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27653 +                       skip = (!wbr || !wbr->wbr_whbase);
27654 +                       if (skip && wbr) {
27655 +                               if (do_plink)
27656 +                                       skip = !!wbr->wbr_plink;
27657 +                               else
27658 +                                       skip = !wbr->wbr_plink;
27659 +                       }
27660 +               } else {
27661 +                       /* skip = (br->br_whbase && br->br_ohph); */
27662 +                       skip = (wbr && wbr->wbr_whbase);
27663 +                       if (skip) {
27664 +                               if (do_plink)
27665 +                                       skip = !!wbr->wbr_plink;
27666 +                               else
27667 +                                       skip = !wbr->wbr_plink;
27668 +                       }
27669 +               }
27670 +               if (wbr)
27671 +                       wbr_wh_read_unlock(wbr);
27672 +
27673 +               if (can_no_dreval) {
27674 +                       dentry = br->br_path.dentry;
27675 +                       spin_lock(&dentry->d_lock);
27676 +                       if (dentry->d_flags &
27677 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27678 +                               can_no_dreval = 0;
27679 +                       spin_unlock(&dentry->d_lock);
27680 +               }
27681 +
27682 +               if (au_br_fhsm(br->br_perm)) {
27683 +                       fhsm++;
27684 +                       AuDebugOn(!br->br_fhsm);
27685 +               }
27686 +
27687 +               if (skip)
27688 +                       continue;
27689 +
27690 +               hdir = au_hi(dir, bindex);
27691 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27692 +               if (wbr)
27693 +                       wbr_wh_write_lock(wbr);
27694 +               err = au_wh_init(br, sb);
27695 +               if (wbr)
27696 +                       wbr_wh_write_unlock(wbr);
27697 +               au_hn_inode_unlock(hdir);
27698 +
27699 +               if (!err && do_free) {
27700 +                       au_kfree_rcu(wbr);
27701 +                       br->br_wbr = NULL;
27702 +               }
27703 +       }
27704 +
27705 +       if (can_no_dreval)
27706 +               au_fset_si(sbinfo, NO_DREVAL);
27707 +       else
27708 +               au_fclr_si(sbinfo, NO_DREVAL);
27709 +
27710 +       if (fhsm >= 2) {
27711 +               au_fset_si(sbinfo, FHSM);
27712 +               for (bindex = bbot; bindex >= 0; bindex--) {
27713 +                       br = au_sbr(sb, bindex);
27714 +                       if (au_br_fhsm(br->br_perm)) {
27715 +                               au_fhsm_set_bottom(sb, bindex);
27716 +                               break;
27717 +                       }
27718 +               }
27719 +       } else {
27720 +               au_fclr_si(sbinfo, FHSM);
27721 +               au_fhsm_set_bottom(sb, -1);
27722 +       }
27723 +
27724 +       return err;
27725 +}
27726 +
27727 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27728 +{
27729 +       int err;
27730 +       unsigned int tmp;
27731 +       aufs_bindex_t bindex, bbot;
27732 +       struct au_opt *opt;
27733 +       struct au_opt_xino *opt_xino, xino;
27734 +       struct au_sbinfo *sbinfo;
27735 +       struct au_branch *br;
27736 +       struct inode *dir;
27737 +
27738 +       SiMustWriteLock(sb);
27739 +
27740 +       err = 0;
27741 +       opt_xino = NULL;
27742 +       opt = opts->opt;
27743 +       while (err >= 0 && opt->type != Opt_tail)
27744 +               err = au_opt_simple(sb, opt++, opts);
27745 +       if (err > 0)
27746 +               err = 0;
27747 +       else if (unlikely(err < 0))
27748 +               goto out;
27749 +
27750 +       /* disable xino and udba temporary */
27751 +       sbinfo = au_sbi(sb);
27752 +       tmp = sbinfo->si_mntflags;
27753 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27754 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27755 +
27756 +       opt = opts->opt;
27757 +       while (err >= 0 && opt->type != Opt_tail)
27758 +               err = au_opt_br(sb, opt++, opts);
27759 +       if (err > 0)
27760 +               err = 0;
27761 +       else if (unlikely(err < 0))
27762 +               goto out;
27763 +
27764 +       bbot = au_sbbot(sb);
27765 +       if (unlikely(bbot < 0)) {
27766 +               err = -EINVAL;
27767 +               pr_err("no branches\n");
27768 +               goto out;
27769 +       }
27770 +
27771 +       if (au_opt_test(tmp, XINO))
27772 +               au_opt_set(sbinfo->si_mntflags, XINO);
27773 +       opt = opts->opt;
27774 +       while (!err && opt->type != Opt_tail)
27775 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27776 +       if (unlikely(err))
27777 +               goto out;
27778 +
27779 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27780 +       if (unlikely(err))
27781 +               goto out;
27782 +
27783 +       /* restore xino */
27784 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27785 +               xino.file = au_xino_def(sb);
27786 +               err = PTR_ERR(xino.file);
27787 +               if (IS_ERR(xino.file))
27788 +                       goto out;
27789 +
27790 +               err = au_xino_set(sb, &xino, /*remount*/0);
27791 +               fput(xino.file);
27792 +               if (unlikely(err))
27793 +                       goto out;
27794 +       }
27795 +
27796 +       /* restore udba */
27797 +       tmp &= AuOptMask_UDBA;
27798 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27799 +       sbinfo->si_mntflags |= tmp;
27800 +       bbot = au_sbbot(sb);
27801 +       for (bindex = 0; bindex <= bbot; bindex++) {
27802 +               br = au_sbr(sb, bindex);
27803 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27804 +               if (unlikely(err))
27805 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27806 +                               bindex, err);
27807 +               /* go on even if err */
27808 +       }
27809 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27810 +               dir = d_inode(sb->s_root);
27811 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27812 +       }
27813 +
27814 +out:
27815 +       return err;
27816 +}
27817 +
27818 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27819 +{
27820 +       int err, rerr;
27821 +       unsigned char no_dreval;
27822 +       struct inode *dir;
27823 +       struct au_opt_xino *opt_xino;
27824 +       struct au_opt *opt;
27825 +       struct au_sbinfo *sbinfo;
27826 +
27827 +       SiMustWriteLock(sb);
27828 +
27829 +       err = au_dr_opt_flush(sb);
27830 +       if (unlikely(err))
27831 +               goto out;
27832 +       au_fset_opts(opts->flags, DR_FLUSHED);
27833 +
27834 +       dir = d_inode(sb->s_root);
27835 +       sbinfo = au_sbi(sb);
27836 +       opt_xino = NULL;
27837 +       opt = opts->opt;
27838 +       while (err >= 0 && opt->type != Opt_tail) {
27839 +               err = au_opt_simple(sb, opt, opts);
27840 +               if (!err)
27841 +                       err = au_opt_br(sb, opt, opts);
27842 +               if (!err)
27843 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27844 +               opt++;
27845 +       }
27846 +       if (err > 0)
27847 +               err = 0;
27848 +       AuTraceErr(err);
27849 +       /* go on even err */
27850 +
27851 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27852 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27853 +       if (unlikely(rerr && !err))
27854 +               err = rerr;
27855 +
27856 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27857 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27858 +
27859 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27860 +               rerr = au_xib_trunc(sb);
27861 +               if (unlikely(rerr && !err))
27862 +                       err = rerr;
27863 +       }
27864 +
27865 +       /* will be handled by the caller */
27866 +       if (!au_ftest_opts(opts->flags, REFRESH)
27867 +           && (opts->given_udba
27868 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27869 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27870 +                   ))
27871 +               au_fset_opts(opts->flags, REFRESH);
27872 +
27873 +       AuDbg("status 0x%x\n", opts->flags);
27874 +
27875 +out:
27876 +       return err;
27877 +}
27878 +
27879 +/* ---------------------------------------------------------------------- */
27880 +
27881 +unsigned int au_opt_udba(struct super_block *sb)
27882 +{
27883 +       return au_mntflags(sb) & AuOptMask_UDBA;
27884 +}
27885 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27886 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27887 +++ linux/fs/aufs/opts.h        2022-08-02 12:12:45.165641939 +0200
27888 @@ -0,0 +1,263 @@
27889 +/* SPDX-License-Identifier: GPL-2.0 */
27890 +/*
27891 + * Copyright (C) 2005-2021 Junjiro R. Okajima
27892 + *
27893 + * This program, aufs is free software; you can redistribute it and/or modify
27894 + * it under the terms of the GNU General Public License as published by
27895 + * the Free Software Foundation; either version 2 of the License, or
27896 + * (at your option) any later version.
27897 + *
27898 + * This program is distributed in the hope that it will be useful,
27899 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27900 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27901 + * GNU General Public License for more details.
27902 + *
27903 + * You should have received a copy of the GNU General Public License
27904 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27905 + */
27906 +
27907 +/*
27908 + * mount options/flags
27909 + */
27910 +
27911 +#ifndef __AUFS_OPTS_H__
27912 +#define __AUFS_OPTS_H__
27913 +
27914 +#ifdef __KERNEL__
27915 +
27916 +#include <linux/fs_parser.h>
27917 +#include <linux/namei.h>
27918 +#include <linux/path.h>
27919 +
27920 +enum {
27921 +       Opt_br,
27922 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27923 +       Opt_idel, Opt_imod,
27924 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27925 +       Opt_xino, Opt_noxino,
27926 +       Opt_trunc_xino, Opt_trunc_xino_v,
27927 +       Opt_trunc_xino_path, Opt_itrunc_xino,
27928 +       Opt_trunc_xib,
27929 +       Opt_shwh,
27930 +       Opt_plink, Opt_list_plink,
27931 +       Opt_udba,
27932 +       Opt_dio,
27933 +       Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27934 +       Opt_warn_perm,
27935 +       Opt_wbr_copyup, Opt_wbr_create,
27936 +       Opt_fhsm_sec,
27937 +       Opt_verbose, Opt_noverbose,
27938 +       Opt_sum, Opt_wsum,
27939 +       Opt_dirperm1,
27940 +       Opt_dirren,
27941 +       Opt_acl,
27942 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
27943 +};
27944 +
27945 +/* ---------------------------------------------------------------------- */
27946 +
27947 +/* mount flags */
27948 +#define AuOpt_XINO             1               /* external inode number bitmap
27949 +                                                  and translation table */
27950 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27951 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27952 +#define AuOpt_UDBA_REVAL       (1 << 3)
27953 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27954 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27955 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27956 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27957 +                                                  bits */
27958 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27959 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27960 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27961 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27962 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27963 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27964 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27965 +
27966 +#ifndef CONFIG_AUFS_HNOTIFY
27967 +#undef AuOpt_UDBA_HNOTIFY
27968 +#define AuOpt_UDBA_HNOTIFY     0
27969 +#endif
27970 +#ifndef CONFIG_AUFS_DIRREN
27971 +#undef AuOpt_DIRREN
27972 +#define AuOpt_DIRREN           0
27973 +#endif
27974 +#ifndef CONFIG_AUFS_SHWH
27975 +#undef AuOpt_SHWH
27976 +#define AuOpt_SHWH             0
27977 +#endif
27978 +
27979 +#define AuOpt_Def      (AuOpt_XINO \
27980 +                        | AuOpt_UDBA_REVAL \
27981 +                        | AuOpt_PLINK \
27982 +                        /* | AuOpt_DIRPERM1 */ \
27983 +                        | AuOpt_WARN_PERM)
27984 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27985 +                        | AuOpt_UDBA_REVAL \
27986 +                        | AuOpt_UDBA_HNOTIFY)
27987 +
27988 +#define AuOpt_LkupDirFlags     (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
27989 +
27990 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27991 +#define au_opt_set(flags, name) do { \
27992 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27993 +       ((flags) |= AuOpt_##name); \
27994 +} while (0)
27995 +#define au_opt_set_udba(flags, name) do { \
27996 +       (flags) &= ~AuOptMask_UDBA; \
27997 +       ((flags) |= AuOpt_##name); \
27998 +} while (0)
27999 +#define au_opt_clr(flags, name) do { \
28000 +       ((flags) &= ~AuOpt_##name); \
28001 +} while (0)
28002 +
28003 +static inline unsigned int au_opts_plink(unsigned int mntflags)
28004 +{
28005 +#ifdef CONFIG_PROC_FS
28006 +       return mntflags;
28007 +#else
28008 +       return mntflags & ~AuOpt_PLINK;
28009 +#endif
28010 +}
28011 +
28012 +/* ---------------------------------------------------------------------- */
28013 +
28014 +/* policies to select one among multiple writable branches */
28015 +enum {
28016 +       AuWbrCreate_TDP,        /* top down parent */
28017 +       AuWbrCreate_RR,         /* round robin */
28018 +       AuWbrCreate_MFS,        /* most free space */
28019 +       AuWbrCreate_MFSV,       /* mfs with seconds */
28020 +       AuWbrCreate_MFSRR,      /* mfs then rr */
28021 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
28022 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
28023 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
28024 +       AuWbrCreate_PMFS,       /* parent and mfs */
28025 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
28026 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
28027 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
28028 +
28029 +       AuWbrCreate_Def = AuWbrCreate_TDP
28030 +};
28031 +
28032 +enum {
28033 +       AuWbrCopyup_TDP,        /* top down parent */
28034 +       AuWbrCopyup_BUP,        /* bottom up parent */
28035 +       AuWbrCopyup_BU,         /* bottom up */
28036 +
28037 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
28038 +};
28039 +
28040 +/* ---------------------------------------------------------------------- */
28041 +
28042 +struct file;
28043 +
28044 +struct au_opt_add {
28045 +       aufs_bindex_t   bindex;
28046 +       char            *pathname;
28047 +       int             perm;
28048 +       struct path     path;
28049 +};
28050 +
28051 +struct au_opt_del {
28052 +       char            *pathname;
28053 +       struct path     h_path;
28054 +};
28055 +
28056 +struct au_opt_mod {
28057 +       char            *path;
28058 +       int             perm;
28059 +       struct dentry   *h_root;
28060 +};
28061 +
28062 +struct au_opt_xino {
28063 +       char            *path;
28064 +       struct file     *file;
28065 +};
28066 +
28067 +struct au_opt_xino_itrunc {
28068 +       aufs_bindex_t   bindex;
28069 +};
28070 +
28071 +struct au_opt_wbr_create {
28072 +       int                     wbr_create;
28073 +       int                     mfs_second;
28074 +       unsigned long long      mfsrr_watermark;
28075 +};
28076 +
28077 +struct au_opt {
28078 +       int type;
28079 +       union {
28080 +               struct au_opt_xino      xino;
28081 +               struct au_opt_xino_itrunc xino_itrunc;
28082 +               struct au_opt_add       add;
28083 +               struct au_opt_del       del;
28084 +               struct au_opt_mod       mod;
28085 +               int                     dirwh;
28086 +               int                     rdcache;
28087 +               unsigned int            rdblk;
28088 +               unsigned int            rdhash;
28089 +               int                     udba;
28090 +               struct au_opt_wbr_create wbr_create;
28091 +               int                     wbr_copyup;
28092 +               unsigned int            fhsm_second;
28093 +               bool                    tf; /* generic flag, true or false */
28094 +       };
28095 +};
28096 +
28097 +/* opts flags */
28098 +#define AuOpts_REMOUNT         1
28099 +#define AuOpts_REFRESH         (1 << 1)
28100 +#define AuOpts_TRUNC_XIB       (1 << 2)
28101 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
28102 +#define AuOpts_REFRESH_IDOP    (1 << 4)
28103 +#define AuOpts_DR_FLUSHED      (1 << 5)
28104 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
28105 +#define au_fset_opts(flags, name) \
28106 +       do { (flags) |= AuOpts_##name; } while (0)
28107 +#define au_fclr_opts(flags, name) \
28108 +       do { (flags) &= ~AuOpts_##name; } while (0)
28109 +
28110 +#ifndef CONFIG_AUFS_DIRREN
28111 +#undef AuOpts_DR_FLUSHED
28112 +#define AuOpts_DR_FLUSHED      0
28113 +#endif
28114 +
28115 +struct au_opts {
28116 +       struct au_opt   *opt;
28117 +       int             max_opt;
28118 +
28119 +       unsigned int    given_udba;
28120 +       unsigned int    flags;
28121 +       unsigned long   sb_flags;
28122 +};
28123 +
28124 +/* ---------------------------------------------------------------------- */
28125 +
28126 +/* opts.c */
28127 +int au_br_perm_val(char *perm);
28128 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
28129 +int au_udba_val(char *str);
28130 +const char *au_optstr_udba(int udba);
28131 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
28132 +const char *au_optstr_wbr_create(int wbr_create);
28133 +int au_wbr_copyup_val(char *str);
28134 +const char *au_optstr_wbr_copyup(int wbr_copyup);
28135 +
28136 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28137 +              aufs_bindex_t bindex);
28138 +struct super_block;
28139 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28140 +                  unsigned int pending);
28141 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28142 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28143 +
28144 +unsigned int au_opt_udba(struct super_block *sb);
28145 +
28146 +/* fsctx.c */
28147 +int aufs_fsctx_init(struct fs_context *fc);
28148 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28149 +
28150 +#endif /* __KERNEL__ */
28151 +#endif /* __AUFS_OPTS_H__ */
28152 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
28153 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
28154 +++ linux/fs/aufs/plink.c       2022-08-02 12:12:45.165641939 +0200
28155 @@ -0,0 +1,516 @@
28156 +// SPDX-License-Identifier: GPL-2.0
28157 +/*
28158 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28159 + *
28160 + * This program, aufs is free software; you can redistribute it and/or modify
28161 + * it under the terms of the GNU General Public License as published by
28162 + * the Free Software Foundation; either version 2 of the License, or
28163 + * (at your option) any later version.
28164 + *
28165 + * This program is distributed in the hope that it will be useful,
28166 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28167 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28168 + * GNU General Public License for more details.
28169 + *
28170 + * You should have received a copy of the GNU General Public License
28171 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28172 + */
28173 +
28174 +/*
28175 + * pseudo-link
28176 + */
28177 +
28178 +#include "aufs.h"
28179 +
28180 +/*
28181 + * the pseudo-link maintenance mode.
28182 + * during a user process maintains the pseudo-links,
28183 + * prohibit adding a new plink and branch manipulation.
28184 + *
28185 + * Flags
28186 + * NOPLM:
28187 + *     For entry functions which will handle plink, and i_mutex is already held
28188 + *     in VFS.
28189 + *     They cannot wait and should return an error at once.
28190 + *     Callers has to check the error.
28191 + * NOPLMW:
28192 + *     For entry functions which will handle plink, but i_mutex is not held
28193 + *     in VFS.
28194 + *     They can wait the plink maintenance mode to finish.
28195 + *
28196 + * They behave like F_SETLK and F_SETLKW.
28197 + * If the caller never handle plink, then both flags are unnecessary.
28198 + */
28199 +
28200 +int au_plink_maint(struct super_block *sb, int flags)
28201 +{
28202 +       int err;
28203 +       pid_t pid, ppid;
28204 +       struct task_struct *parent, *prev;
28205 +       struct au_sbinfo *sbi;
28206 +
28207 +       SiMustAnyLock(sb);
28208 +
28209 +       err = 0;
28210 +       if (!au_opt_test(au_mntflags(sb), PLINK))
28211 +               goto out;
28212 +
28213 +       sbi = au_sbi(sb);
28214 +       pid = sbi->si_plink_maint_pid;
28215 +       if (!pid || pid == current->pid)
28216 +               goto out;
28217 +
28218 +       /* todo: it highly depends upon /sbin/mount.aufs */
28219 +       prev = NULL;
28220 +       parent = current;
28221 +       ppid = 0;
28222 +       rcu_read_lock();
28223 +       while (1) {
28224 +               parent = rcu_dereference(parent->real_parent);
28225 +               if (parent == prev)
28226 +                       break;
28227 +               ppid = task_pid_vnr(parent);
28228 +               if (pid == ppid) {
28229 +                       rcu_read_unlock();
28230 +                       goto out;
28231 +               }
28232 +               prev = parent;
28233 +       }
28234 +       rcu_read_unlock();
28235 +
28236 +       if (au_ftest_lock(flags, NOPLMW)) {
28237 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
28238 +               /* AuDebugOn(!lockdep_depth(current)); */
28239 +               while (sbi->si_plink_maint_pid) {
28240 +                       si_read_unlock(sb);
28241 +                       /* gave up wake_up_bit() */
28242 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28243 +
28244 +                       if (au_ftest_lock(flags, FLUSH))
28245 +                               au_nwt_flush(&sbi->si_nowait);
28246 +                       si_noflush_read_lock(sb);
28247 +               }
28248 +       } else if (au_ftest_lock(flags, NOPLM)) {
28249 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
28250 +               err = -EAGAIN;
28251 +       }
28252 +
28253 +out:
28254 +       return err;
28255 +}
28256 +
28257 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28258 +{
28259 +       spin_lock(&sbinfo->si_plink_maint_lock);
28260 +       sbinfo->si_plink_maint_pid = 0;
28261 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28262 +       wake_up_all(&sbinfo->si_plink_wq);
28263 +}
28264 +
28265 +int au_plink_maint_enter(struct super_block *sb)
28266 +{
28267 +       int err;
28268 +       struct au_sbinfo *sbinfo;
28269 +
28270 +       err = 0;
28271 +       sbinfo = au_sbi(sb);
28272 +       /* make sure i am the only one in this fs */
28273 +       si_write_lock(sb, AuLock_FLUSH);
28274 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28275 +               spin_lock(&sbinfo->si_plink_maint_lock);
28276 +               if (!sbinfo->si_plink_maint_pid)
28277 +                       sbinfo->si_plink_maint_pid = current->pid;
28278 +               else
28279 +                       err = -EBUSY;
28280 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28281 +       }
28282 +       si_write_unlock(sb);
28283 +
28284 +       return err;
28285 +}
28286 +
28287 +/* ---------------------------------------------------------------------- */
28288 +
28289 +#ifdef CONFIG_AUFS_DEBUG
28290 +void au_plink_list(struct super_block *sb)
28291 +{
28292 +       int i;
28293 +       struct au_sbinfo *sbinfo;
28294 +       struct hlist_bl_head *hbl;
28295 +       struct hlist_bl_node *pos;
28296 +       struct au_icntnr *icntnr;
28297 +
28298 +       SiMustAnyLock(sb);
28299 +
28300 +       sbinfo = au_sbi(sb);
28301 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28302 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28303 +
28304 +       for (i = 0; i < AuPlink_NHASH; i++) {
28305 +               hbl = sbinfo->si_plink + i;
28306 +               hlist_bl_lock(hbl);
28307 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28308 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28309 +               hlist_bl_unlock(hbl);
28310 +       }
28311 +}
28312 +#endif
28313 +
28314 +/* is the inode pseudo-linked? */
28315 +int au_plink_test(struct inode *inode)
28316 +{
28317 +       int found, i;
28318 +       struct au_sbinfo *sbinfo;
28319 +       struct hlist_bl_head *hbl;
28320 +       struct hlist_bl_node *pos;
28321 +       struct au_icntnr *icntnr;
28322 +
28323 +       sbinfo = au_sbi(inode->i_sb);
28324 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28325 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28326 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28327 +
28328 +       found = 0;
28329 +       i = au_plink_hash(inode->i_ino);
28330 +       hbl =  sbinfo->si_plink + i;
28331 +       hlist_bl_lock(hbl);
28332 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28333 +               if (&icntnr->vfs_inode == inode) {
28334 +                       found = 1;
28335 +                       break;
28336 +               }
28337 +       hlist_bl_unlock(hbl);
28338 +       return found;
28339 +}
28340 +
28341 +/* ---------------------------------------------------------------------- */
28342 +
28343 +/*
28344 + * generate a name for plink.
28345 + * the file will be stored under AUFS_WH_PLINKDIR.
28346 + */
28347 +/* 20 is max digits length of ulong 64 */
28348 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28349 +
28350 +static int plink_name(char *name, int len, struct inode *inode,
28351 +                     aufs_bindex_t bindex)
28352 +{
28353 +       int rlen;
28354 +       struct inode *h_inode;
28355 +
28356 +       h_inode = au_h_iptr(inode, bindex);
28357 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28358 +       return rlen;
28359 +}
28360 +
28361 +struct au_do_plink_lkup_args {
28362 +       struct dentry **errp;
28363 +       struct qstr *tgtname;
28364 +       struct path *h_ppath;
28365 +};
28366 +
28367 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28368 +                                      struct path *h_ppath)
28369 +{
28370 +       struct dentry *h_dentry;
28371 +       struct inode *h_inode;
28372 +
28373 +       h_inode = d_inode(h_ppath->dentry);
28374 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28375 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
28376 +       inode_unlock_shared(h_inode);
28377 +
28378 +       return h_dentry;
28379 +}
28380 +
28381 +static void au_call_do_plink_lkup(void *args)
28382 +{
28383 +       struct au_do_plink_lkup_args *a = args;
28384 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
28385 +}
28386 +
28387 +/* lookup the plink-ed @inode under the branch at @bindex */
28388 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28389 +{
28390 +       struct dentry *h_dentry;
28391 +       struct au_branch *br;
28392 +       struct path h_ppath;
28393 +       int wkq_err;
28394 +       char a[PLINK_NAME_LEN];
28395 +       struct qstr tgtname = QSTR_INIT(a, 0);
28396 +
28397 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28398 +
28399 +       br = au_sbr(inode->i_sb, bindex);
28400 +       h_ppath.dentry = br->br_wbr->wbr_plink;
28401 +       h_ppath.mnt = au_br_mnt(br);
28402 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28403 +
28404 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28405 +               struct au_do_plink_lkup_args args = {
28406 +                       .errp           = &h_dentry,
28407 +                       .tgtname        = &tgtname,
28408 +                       .h_ppath        = &h_ppath
28409 +               };
28410 +
28411 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28412 +               if (unlikely(wkq_err))
28413 +                       h_dentry = ERR_PTR(wkq_err);
28414 +       } else
28415 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28416 +
28417 +       return h_dentry;
28418 +}
28419 +
28420 +/* create a pseudo-link */
28421 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28422 +                     struct dentry *h_dentry)
28423 +{
28424 +       int err;
28425 +       struct path h_path;
28426 +       struct inode *h_dir, *delegated;
28427 +
28428 +       h_dir = d_inode(h_ppath->dentry);
28429 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28430 +       h_path.mnt = h_ppath->mnt;
28431 +again:
28432 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28433 +       err = PTR_ERR(h_path.dentry);
28434 +       if (IS_ERR(h_path.dentry))
28435 +               goto out;
28436 +
28437 +       err = 0;
28438 +       /* wh.plink dir is not monitored */
28439 +       /* todo: is it really safe? */
28440 +       if (d_is_positive(h_path.dentry)
28441 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28442 +               delegated = NULL;
28443 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28444 +               if (unlikely(err == -EWOULDBLOCK)) {
28445 +                       pr_warn("cannot retry for NFSv4 delegation"
28446 +                               " for an internal unlink\n");
28447 +                       iput(delegated);
28448 +               }
28449 +               dput(h_path.dentry);
28450 +               h_path.dentry = NULL;
28451 +               if (!err)
28452 +                       goto again;
28453 +       }
28454 +       if (!err && d_is_negative(h_path.dentry)) {
28455 +               delegated = NULL;
28456 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28457 +               if (unlikely(err == -EWOULDBLOCK)) {
28458 +                       pr_warn("cannot retry for NFSv4 delegation"
28459 +                               " for an internal link\n");
28460 +                       iput(delegated);
28461 +               }
28462 +       }
28463 +       dput(h_path.dentry);
28464 +
28465 +out:
28466 +       inode_unlock(h_dir);
28467 +       return err;
28468 +}
28469 +
28470 +struct do_whplink_args {
28471 +       int *errp;
28472 +       struct qstr *tgt;
28473 +       struct path *h_ppath;
28474 +       struct dentry *h_dentry;
28475 +};
28476 +
28477 +static void call_do_whplink(void *args)
28478 +{
28479 +       struct do_whplink_args *a = args;
28480 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28481 +}
28482 +
28483 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28484 +                  aufs_bindex_t bindex)
28485 +{
28486 +       int err, wkq_err;
28487 +       struct au_branch *br;
28488 +       struct au_wbr *wbr;
28489 +       struct path h_ppath;
28490 +       char a[PLINK_NAME_LEN];
28491 +       struct qstr tgtname = QSTR_INIT(a, 0);
28492 +
28493 +       br = au_sbr(inode->i_sb, bindex);
28494 +       wbr = br->br_wbr;
28495 +       h_ppath.dentry = wbr->wbr_plink;
28496 +       h_ppath.mnt = au_br_mnt(br);
28497 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28498 +
28499 +       /* always superio. */
28500 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28501 +               struct do_whplink_args args = {
28502 +                       .errp           = &err,
28503 +                       .tgt            = &tgtname,
28504 +                       .h_ppath        = &h_ppath,
28505 +                       .h_dentry       = h_dentry
28506 +               };
28507 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28508 +               if (unlikely(wkq_err))
28509 +                       err = wkq_err;
28510 +       } else
28511 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28512 +
28513 +       return err;
28514 +}
28515 +
28516 +/*
28517 + * create a new pseudo-link for @h_dentry on @bindex.
28518 + * the linked inode is held in aufs @inode.
28519 + */
28520 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28521 +                    struct dentry *h_dentry)
28522 +{
28523 +       struct super_block *sb;
28524 +       struct au_sbinfo *sbinfo;
28525 +       struct hlist_bl_head *hbl;
28526 +       struct hlist_bl_node *pos;
28527 +       struct au_icntnr *icntnr;
28528 +       int found, err, cnt, i;
28529 +
28530 +       sb = inode->i_sb;
28531 +       sbinfo = au_sbi(sb);
28532 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28533 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28534 +
28535 +       found = au_plink_test(inode);
28536 +       if (found)
28537 +               return;
28538 +
28539 +       i = au_plink_hash(inode->i_ino);
28540 +       hbl = sbinfo->si_plink + i;
28541 +       au_igrab(inode);
28542 +
28543 +       hlist_bl_lock(hbl);
28544 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28545 +               if (&icntnr->vfs_inode == inode) {
28546 +                       found = 1;
28547 +                       break;
28548 +               }
28549 +       }
28550 +       if (!found) {
28551 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28552 +               hlist_bl_add_head(&icntnr->plink, hbl);
28553 +       }
28554 +       hlist_bl_unlock(hbl);
28555 +       if (!found) {
28556 +               cnt = au_hbl_count(hbl);
28557 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28558 +               if (cnt > AUFS_PLINK_WARN)
28559 +                       AuWarn1(msg ", %d\n", cnt);
28560 +#undef msg
28561 +               err = whplink(h_dentry, inode, bindex);
28562 +               if (unlikely(err)) {
28563 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28564 +                       au_hbl_del(&icntnr->plink, hbl);
28565 +                       iput(&icntnr->vfs_inode);
28566 +               }
28567 +       } else
28568 +               iput(&icntnr->vfs_inode);
28569 +}
28570 +
28571 +/* free all plinks */
28572 +void au_plink_put(struct super_block *sb, int verbose)
28573 +{
28574 +       int i, warned;
28575 +       struct au_sbinfo *sbinfo;
28576 +       struct hlist_bl_head *hbl;
28577 +       struct hlist_bl_node *pos, *tmp;
28578 +       struct au_icntnr *icntnr;
28579 +
28580 +       SiMustWriteLock(sb);
28581 +
28582 +       sbinfo = au_sbi(sb);
28583 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28584 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28585 +
28586 +       /* no spin_lock since sbinfo is write-locked */
28587 +       warned = 0;
28588 +       for (i = 0; i < AuPlink_NHASH; i++) {
28589 +               hbl = sbinfo->si_plink + i;
28590 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28591 +                       pr_warn("pseudo-link is not flushed");
28592 +                       warned = 1;
28593 +               }
28594 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28595 +                       iput(&icntnr->vfs_inode);
28596 +               INIT_HLIST_BL_HEAD(hbl);
28597 +       }
28598 +}
28599 +
28600 +void au_plink_clean(struct super_block *sb, int verbose)
28601 +{
28602 +       struct dentry *root;
28603 +
28604 +       root = sb->s_root;
28605 +       aufs_write_lock(root);
28606 +       if (au_opt_test(au_mntflags(sb), PLINK))
28607 +               au_plink_put(sb, verbose);
28608 +       aufs_write_unlock(root);
28609 +}
28610 +
28611 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28612 +{
28613 +       int do_put;
28614 +       aufs_bindex_t btop, bbot, bindex;
28615 +
28616 +       do_put = 0;
28617 +       btop = au_ibtop(inode);
28618 +       bbot = au_ibbot(inode);
28619 +       if (btop >= 0) {
28620 +               for (bindex = btop; bindex <= bbot; bindex++) {
28621 +                       if (!au_h_iptr(inode, bindex)
28622 +                           || au_ii_br_id(inode, bindex) != br_id)
28623 +                               continue;
28624 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28625 +                       do_put = 1;
28626 +                       break;
28627 +               }
28628 +               if (do_put)
28629 +                       for (bindex = btop; bindex <= bbot; bindex++)
28630 +                               if (au_h_iptr(inode, bindex)) {
28631 +                                       do_put = 0;
28632 +                                       break;
28633 +                               }
28634 +       } else
28635 +               do_put = 1;
28636 +
28637 +       return do_put;
28638 +}
28639 +
28640 +/* free the plinks on a branch specified by @br_id */
28641 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28642 +{
28643 +       struct au_sbinfo *sbinfo;
28644 +       struct hlist_bl_head *hbl;
28645 +       struct hlist_bl_node *pos, *tmp;
28646 +       struct au_icntnr *icntnr;
28647 +       struct inode *inode;
28648 +       int i, do_put;
28649 +
28650 +       SiMustWriteLock(sb);
28651 +
28652 +       sbinfo = au_sbi(sb);
28653 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28654 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28655 +
28656 +       /* no bit_lock since sbinfo is write-locked */
28657 +       for (i = 0; i < AuPlink_NHASH; i++) {
28658 +               hbl = sbinfo->si_plink + i;
28659 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28660 +                       inode = au_igrab(&icntnr->vfs_inode);
28661 +                       ii_write_lock_child(inode);
28662 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28663 +                       if (do_put) {
28664 +                               hlist_bl_del(&icntnr->plink);
28665 +                               iput(inode);
28666 +                       }
28667 +                       ii_write_unlock(inode);
28668 +                       iput(inode);
28669 +               }
28670 +       }
28671 +}
28672 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28673 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28674 +++ linux/fs/aufs/poll.c        2022-08-02 12:12:45.168975258 +0200
28675 @@ -0,0 +1,51 @@
28676 +// SPDX-License-Identifier: GPL-2.0
28677 +/*
28678 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28679 + *
28680 + * This program, aufs is free software; you can redistribute it and/or modify
28681 + * it under the terms of the GNU General Public License as published by
28682 + * the Free Software Foundation; either version 2 of the License, or
28683 + * (at your option) any later version.
28684 + *
28685 + * This program is distributed in the hope that it will be useful,
28686 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28687 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28688 + * GNU General Public License for more details.
28689 + *
28690 + * You should have received a copy of the GNU General Public License
28691 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28692 + */
28693 +
28694 +/*
28695 + * poll operation
28696 + * There is only one filesystem which implements ->poll operation, currently.
28697 + */
28698 +
28699 +#include "aufs.h"
28700 +
28701 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28702 +{
28703 +       __poll_t mask;
28704 +       struct file *h_file;
28705 +       struct super_block *sb;
28706 +
28707 +       /* We should pretend an error happened. */
28708 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28709 +       sb = file->f_path.dentry->d_sb;
28710 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28711 +
28712 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28713 +       if (IS_ERR(h_file)) {
28714 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28715 +               goto out;
28716 +       }
28717 +
28718 +       mask = vfs_poll(h_file, pt);
28719 +       fput(h_file); /* instead of au_read_post() */
28720 +
28721 +out:
28722 +       si_read_unlock(sb);
28723 +       if (mask & EPOLLERR)
28724 +               AuDbg("mask 0x%x\n", mask);
28725 +       return mask;
28726 +}
28727 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28728 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28729 +++ linux/fs/aufs/posix_acl.c   2022-08-02 12:12:45.168975258 +0200
28730 @@ -0,0 +1,111 @@
28731 +// SPDX-License-Identifier: GPL-2.0
28732 +/*
28733 + * Copyright (C) 2014-2021 Junjiro R. Okajima
28734 + *
28735 + * This program, aufs is free software; you can redistribute it and/or modify
28736 + * it under the terms of the GNU General Public License as published by
28737 + * the Free Software Foundation; either version 2 of the License, or
28738 + * (at your option) any later version.
28739 + *
28740 + * This program is distributed in the hope that it will be useful,
28741 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28742 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28743 + * GNU General Public License for more details.
28744 + *
28745 + * You should have received a copy of the GNU General Public License
28746 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28747 + */
28748 +
28749 +/*
28750 + * posix acl operations
28751 + */
28752 +
28753 +#include <linux/fs.h>
28754 +#include "aufs.h"
28755 +
28756 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu)
28757 +{
28758 +       struct posix_acl *acl;
28759 +       int err;
28760 +       aufs_bindex_t bindex;
28761 +       struct inode *h_inode;
28762 +       struct super_block *sb;
28763 +
28764 +       acl = ERR_PTR(-ECHILD);
28765 +       if (rcu)
28766 +               goto out;
28767 +
28768 +       acl = NULL;
28769 +       sb = inode->i_sb;
28770 +       si_read_lock(sb, AuLock_FLUSH);
28771 +       ii_read_lock_child(inode);
28772 +       if (!(sb->s_flags & SB_POSIXACL))
28773 +               goto unlock;
28774 +
28775 +       bindex = au_ibtop(inode);
28776 +       h_inode = au_h_iptr(inode, bindex);
28777 +       if (unlikely(!h_inode
28778 +                    || ((h_inode->i_mode & S_IFMT)
28779 +                        != (inode->i_mode & S_IFMT)))) {
28780 +               err = au_busy_or_stale();
28781 +               acl = ERR_PTR(err);
28782 +               goto unlock;
28783 +       }
28784 +
28785 +       /* always topmost only */
28786 +       acl = get_acl(h_inode, type);
28787 +       if (IS_ERR(acl))
28788 +               forget_cached_acl(inode, type);
28789 +       else
28790 +               set_cached_acl(inode, type, acl);
28791 +
28792 +unlock:
28793 +       ii_read_unlock(inode);
28794 +       si_read_unlock(sb);
28795 +
28796 +out:
28797 +       AuTraceErrPtr(acl);
28798 +       return acl;
28799 +}
28800 +
28801 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
28802 +                struct posix_acl *acl, int type)
28803 +{
28804 +       int err;
28805 +       ssize_t ssz;
28806 +       struct dentry *dentry;
28807 +       struct au_sxattr arg = {
28808 +               .type = AU_ACL_SET,
28809 +               .u.acl_set = {
28810 +                       .acl    = acl,
28811 +                       .type   = type
28812 +               },
28813 +       };
28814 +
28815 +       IMustLock(inode);
28816 +
28817 +       if (inode->i_ino == AUFS_ROOT_INO)
28818 +               dentry = dget(inode->i_sb->s_root);
28819 +       else {
28820 +               dentry = d_find_alias(inode);
28821 +               if (!dentry)
28822 +                       dentry = d_find_any_alias(inode);
28823 +               if (!dentry) {
28824 +                       pr_warn("cannot handle this inode, "
28825 +                               "please report to aufs-users ML\n");
28826 +                       err = -ENOENT;
28827 +                       goto out;
28828 +               }
28829 +       }
28830 +
28831 +       ssz = au_sxattr(dentry, inode, &arg);
28832 +       /* forget even it if succeeds since the branch might set differently */
28833 +       forget_cached_acl(inode, type);
28834 +       dput(dentry);
28835 +       err = ssz;
28836 +       if (ssz >= 0)
28837 +               err = 0;
28838 +
28839 +out:
28840 +       return err;
28841 +}
28842 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28843 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28844 +++ linux/fs/aufs/procfs.c      2022-08-02 12:12:45.168975258 +0200
28845 @@ -0,0 +1,170 @@
28846 +// SPDX-License-Identifier: GPL-2.0
28847 +/*
28848 + * Copyright (C) 2010-2021 Junjiro R. Okajima
28849 + *
28850 + * This program, aufs is free software; you can redistribute it and/or modify
28851 + * it under the terms of the GNU General Public License as published by
28852 + * the Free Software Foundation; either version 2 of the License, or
28853 + * (at your option) any later version.
28854 + *
28855 + * This program is distributed in the hope that it will be useful,
28856 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28857 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28858 + * GNU General Public License for more details.
28859 + *
28860 + * You should have received a copy of the GNU General Public License
28861 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28862 + */
28863 +
28864 +/*
28865 + * procfs interfaces
28866 + */
28867 +
28868 +#include <linux/proc_fs.h>
28869 +#include "aufs.h"
28870 +
28871 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28872 +{
28873 +       struct au_sbinfo *sbinfo;
28874 +
28875 +       sbinfo = file->private_data;
28876 +       if (sbinfo) {
28877 +               au_plink_maint_leave(sbinfo);
28878 +               kobject_put(&sbinfo->si_kobj);
28879 +       }
28880 +
28881 +       return 0;
28882 +}
28883 +
28884 +static void au_procfs_plm_write_clean(struct file *file)
28885 +{
28886 +       struct au_sbinfo *sbinfo;
28887 +
28888 +       sbinfo = file->private_data;
28889 +       if (sbinfo)
28890 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28891 +}
28892 +
28893 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28894 +{
28895 +       int err;
28896 +       struct super_block *sb;
28897 +       struct au_sbinfo *sbinfo;
28898 +       struct hlist_bl_node *pos;
28899 +
28900 +       err = -EBUSY;
28901 +       if (unlikely(file->private_data))
28902 +               goto out;
28903 +
28904 +       sb = NULL;
28905 +       /* don't use au_sbilist_lock() here */
28906 +       hlist_bl_lock(&au_sbilist);
28907 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28908 +               if (id == sysaufs_si_id(sbinfo)) {
28909 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28910 +                               sb = sbinfo->si_sb;
28911 +                       break;
28912 +               }
28913 +       hlist_bl_unlock(&au_sbilist);
28914 +
28915 +       err = -EINVAL;
28916 +       if (unlikely(!sb))
28917 +               goto out;
28918 +
28919 +       err = au_plink_maint_enter(sb);
28920 +       if (!err)
28921 +               /* keep kobject_get() */
28922 +               file->private_data = sbinfo;
28923 +       else
28924 +               kobject_put(&sbinfo->si_kobj);
28925 +out:
28926 +       return err;
28927 +}
28928 +
28929 +/*
28930 + * Accept a valid "si=xxxx" only.
28931 + * Once it is accepted successfully, accept "clean" too.
28932 + */
28933 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28934 +                                  size_t count, loff_t *ppos)
28935 +{
28936 +       ssize_t err;
28937 +       unsigned long id;
28938 +       /* last newline is allowed */
28939 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28940 +
28941 +       err = -EACCES;
28942 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28943 +               goto out;
28944 +
28945 +       err = -EINVAL;
28946 +       if (unlikely(count > sizeof(buf)))
28947 +               goto out;
28948 +
28949 +       err = copy_from_user(buf, ubuf, count);
28950 +       if (unlikely(err)) {
28951 +               err = -EFAULT;
28952 +               goto out;
28953 +       }
28954 +       buf[count] = 0;
28955 +
28956 +       err = -EINVAL;
28957 +       if (!strcmp("clean", buf)) {
28958 +               au_procfs_plm_write_clean(file);
28959 +               goto out_success;
28960 +       } else if (unlikely(strncmp("si=", buf, 3)))
28961 +               goto out;
28962 +
28963 +       err = kstrtoul(buf + 3, 16, &id);
28964 +       if (unlikely(err))
28965 +               goto out;
28966 +
28967 +       err = au_procfs_plm_write_si(file, id);
28968 +       if (unlikely(err))
28969 +               goto out;
28970 +
28971 +out_success:
28972 +       err = count; /* success */
28973 +out:
28974 +       return err;
28975 +}
28976 +
28977 +static const struct proc_ops au_procfs_plm_op = {
28978 +       .proc_write     = au_procfs_plm_write,
28979 +       .proc_release   = au_procfs_plm_release
28980 +};
28981 +
28982 +/* ---------------------------------------------------------------------- */
28983 +
28984 +static struct proc_dir_entry *au_procfs_dir;
28985 +
28986 +void au_procfs_fin(void)
28987 +{
28988 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28989 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28990 +}
28991 +
28992 +int __init au_procfs_init(void)
28993 +{
28994 +       int err;
28995 +       struct proc_dir_entry *entry;
28996 +
28997 +       err = -ENOMEM;
28998 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28999 +       if (unlikely(!au_procfs_dir))
29000 +               goto out;
29001 +
29002 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
29003 +                           au_procfs_dir, &au_procfs_plm_op);
29004 +       if (unlikely(!entry))
29005 +               goto out_dir;
29006 +
29007 +       err = 0;
29008 +       goto out; /* success */
29009 +
29010 +
29011 +out_dir:
29012 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29013 +out:
29014 +       return err;
29015 +}
29016 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
29017 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
29018 +++ linux/fs/aufs/rdu.c 2022-08-02 12:12:45.168975258 +0200
29019 @@ -0,0 +1,384 @@
29020 +// SPDX-License-Identifier: GPL-2.0
29021 +/*
29022 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29023 + *
29024 + * This program, aufs is free software; you can redistribute it and/or modify
29025 + * it under the terms of the GNU General Public License as published by
29026 + * the Free Software Foundation; either version 2 of the License, or
29027 + * (at your option) any later version.
29028 + *
29029 + * This program is distributed in the hope that it will be useful,
29030 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29031 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29032 + * GNU General Public License for more details.
29033 + *
29034 + * You should have received a copy of the GNU General Public License
29035 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29036 + */
29037 +
29038 +/*
29039 + * readdir in userspace.
29040 + */
29041 +
29042 +#include <linux/compat.h>
29043 +#include <linux/fs_stack.h>
29044 +#include <linux/security.h>
29045 +#include "aufs.h"
29046 +
29047 +/* bits for struct aufs_rdu.flags */
29048 +#define        AuRdu_CALLED    1
29049 +#define        AuRdu_CONT      (1 << 1)
29050 +#define        AuRdu_FULL      (1 << 2)
29051 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
29052 +#define au_fset_rdu(flags, name) \
29053 +       do { (flags) |= AuRdu_##name; } while (0)
29054 +#define au_fclr_rdu(flags, name) \
29055 +       do { (flags) &= ~AuRdu_##name; } while (0)
29056 +
29057 +struct au_rdu_arg {
29058 +       struct dir_context              ctx;
29059 +       struct aufs_rdu                 *rdu;
29060 +       union au_rdu_ent_ul             ent;
29061 +       unsigned long                   end;
29062 +
29063 +       struct super_block              *sb;
29064 +       int                             err;
29065 +};
29066 +
29067 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29068 +                      loff_t offset, u64 h_ino, unsigned int d_type)
29069 +{
29070 +       int err, len;
29071 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
29072 +       struct aufs_rdu *rdu = arg->rdu;
29073 +       struct au_rdu_ent ent;
29074 +
29075 +       err = 0;
29076 +       arg->err = 0;
29077 +       au_fset_rdu(rdu->cookie.flags, CALLED);
29078 +       len = au_rdu_len(nlen);
29079 +       if (arg->ent.ul + len  < arg->end) {
29080 +               ent.ino = h_ino;
29081 +               ent.bindex = rdu->cookie.bindex;
29082 +               ent.type = d_type;
29083 +               ent.nlen = nlen;
29084 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
29085 +                       ent.type = DT_UNKNOWN;
29086 +
29087 +               /* unnecessary to support mmap_sem since this is a dir */
29088 +               err = -EFAULT;
29089 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29090 +                       goto out;
29091 +               if (copy_to_user(arg->ent.e->name, name, nlen))
29092 +                       goto out;
29093 +               /* the terminating NULL */
29094 +               if (__put_user(0, arg->ent.e->name + nlen))
29095 +                       goto out;
29096 +               err = 0;
29097 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29098 +               arg->ent.ul += len;
29099 +               rdu->rent++;
29100 +       } else {
29101 +               err = -EFAULT;
29102 +               au_fset_rdu(rdu->cookie.flags, FULL);
29103 +               rdu->full = 1;
29104 +               rdu->tail = arg->ent;
29105 +       }
29106 +
29107 +out:
29108 +       /* AuTraceErr(err); */
29109 +       return err;
29110 +}
29111 +
29112 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29113 +{
29114 +       int err;
29115 +       loff_t offset;
29116 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29117 +
29118 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
29119 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29120 +       err = offset;
29121 +       if (unlikely(offset != cookie->h_pos))
29122 +               goto out;
29123 +
29124 +       err = 0;
29125 +       do {
29126 +               arg->err = 0;
29127 +               au_fclr_rdu(cookie->flags, CALLED);
29128 +               /* smp_mb(); */
29129 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
29130 +               if (err >= 0)
29131 +                       err = arg->err;
29132 +       } while (!err
29133 +                && au_ftest_rdu(cookie->flags, CALLED)
29134 +                && !au_ftest_rdu(cookie->flags, FULL));
29135 +       cookie->h_pos = h_file->f_pos;
29136 +
29137 +out:
29138 +       AuTraceErr(err);
29139 +       return err;
29140 +}
29141 +
29142 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29143 +{
29144 +       int err;
29145 +       aufs_bindex_t bbot;
29146 +       struct au_rdu_arg arg = {
29147 +               .ctx = {
29148 +                       .actor = au_rdu_fill
29149 +               }
29150 +       };
29151 +       struct dentry *dentry;
29152 +       struct inode *inode;
29153 +       struct file *h_file;
29154 +       struct au_rdu_cookie *cookie = &rdu->cookie;
29155 +
29156 +       /* VERIFY_WRITE */
29157 +       err = !access_ok(rdu->ent.e, rdu->sz);
29158 +       if (unlikely(err)) {
29159 +               err = -EFAULT;
29160 +               AuTraceErr(err);
29161 +               goto out;
29162 +       }
29163 +       rdu->rent = 0;
29164 +       rdu->tail = rdu->ent;
29165 +       rdu->full = 0;
29166 +       arg.rdu = rdu;
29167 +       arg.ent = rdu->ent;
29168 +       arg.end = arg.ent.ul;
29169 +       arg.end += rdu->sz;
29170 +
29171 +       err = -ENOTDIR;
29172 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
29173 +               goto out;
29174 +
29175 +       err = security_file_permission(file, MAY_READ);
29176 +       AuTraceErr(err);
29177 +       if (unlikely(err))
29178 +               goto out;
29179 +
29180 +       dentry = file->f_path.dentry;
29181 +       inode = d_inode(dentry);
29182 +       inode_lock_shared(inode);
29183 +
29184 +       arg.sb = inode->i_sb;
29185 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29186 +       if (unlikely(err))
29187 +               goto out_mtx;
29188 +       err = au_alive_dir(dentry);
29189 +       if (unlikely(err))
29190 +               goto out_si;
29191 +       /* todo: reval? */
29192 +       fi_read_lock(file);
29193 +
29194 +       err = -EAGAIN;
29195 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29196 +                    && cookie->generation != au_figen(file)))
29197 +               goto out_unlock;
29198 +
29199 +       err = 0;
29200 +       if (!rdu->blk) {
29201 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
29202 +               if (!rdu->blk)
29203 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
29204 +       }
29205 +       bbot = au_fbtop(file);
29206 +       if (cookie->bindex < bbot)
29207 +               cookie->bindex = bbot;
29208 +       bbot = au_fbbot_dir(file);
29209 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29210 +       for (; !err && cookie->bindex <= bbot;
29211 +            cookie->bindex++, cookie->h_pos = 0) {
29212 +               h_file = au_hf_dir(file, cookie->bindex);
29213 +               if (!h_file)
29214 +                       continue;
29215 +
29216 +               au_fclr_rdu(cookie->flags, FULL);
29217 +               err = au_rdu_do(h_file, &arg);
29218 +               AuTraceErr(err);
29219 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29220 +                       break;
29221 +       }
29222 +       AuDbg("rent %llu\n", rdu->rent);
29223 +
29224 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29225 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29226 +               au_fset_rdu(cookie->flags, CONT);
29227 +               cookie->generation = au_figen(file);
29228 +       }
29229 +
29230 +       ii_read_lock_child(inode);
29231 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
29232 +       ii_read_unlock(inode);
29233 +
29234 +out_unlock:
29235 +       fi_read_unlock(file);
29236 +out_si:
29237 +       si_read_unlock(arg.sb);
29238 +out_mtx:
29239 +       inode_unlock_shared(inode);
29240 +out:
29241 +       AuTraceErr(err);
29242 +       return err;
29243 +}
29244 +
29245 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29246 +{
29247 +       int err;
29248 +       ino_t ino;
29249 +       unsigned long long nent;
29250 +       union au_rdu_ent_ul *u;
29251 +       struct au_rdu_ent ent;
29252 +       struct super_block *sb;
29253 +
29254 +       err = 0;
29255 +       nent = rdu->nent;
29256 +       u = &rdu->ent;
29257 +       sb = file->f_path.dentry->d_sb;
29258 +       si_read_lock(sb, AuLock_FLUSH);
29259 +       while (nent-- > 0) {
29260 +               /* unnecessary to support mmap_sem since this is a dir */
29261 +               err = copy_from_user(&ent, u->e, sizeof(ent));
29262 +               if (!err)
29263 +                       /* VERIFY_WRITE */
29264 +                       err = !access_ok(&u->e->ino, sizeof(ino));
29265 +               if (unlikely(err)) {
29266 +                       err = -EFAULT;
29267 +                       AuTraceErr(err);
29268 +                       break;
29269 +               }
29270 +
29271 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29272 +               if (!ent.wh)
29273 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29274 +               else
29275 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29276 +                                       &ino);
29277 +               if (unlikely(err)) {
29278 +                       AuTraceErr(err);
29279 +                       break;
29280 +               }
29281 +
29282 +               err = __put_user(ino, &u->e->ino);
29283 +               if (unlikely(err)) {
29284 +                       err = -EFAULT;
29285 +                       AuTraceErr(err);
29286 +                       break;
29287 +               }
29288 +               u->ul += au_rdu_len(ent.nlen);
29289 +       }
29290 +       si_read_unlock(sb);
29291 +
29292 +       return err;
29293 +}
29294 +
29295 +/* ---------------------------------------------------------------------- */
29296 +
29297 +static int au_rdu_verify(struct aufs_rdu *rdu)
29298 +{
29299 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29300 +             "%llu, b%d, 0x%x, g%u}\n",
29301 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29302 +             rdu->blk,
29303 +             rdu->rent, rdu->shwh, rdu->full,
29304 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29305 +             rdu->cookie.generation);
29306 +
29307 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29308 +               return 0;
29309 +
29310 +       AuDbg("%u:%u\n",
29311 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29312 +       return -EINVAL;
29313 +}
29314 +
29315 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29316 +{
29317 +       long err, e;
29318 +       struct aufs_rdu rdu;
29319 +       void __user *p = (void __user *)arg;
29320 +
29321 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29322 +       if (unlikely(err)) {
29323 +               err = -EFAULT;
29324 +               AuTraceErr(err);
29325 +               goto out;
29326 +       }
29327 +       err = au_rdu_verify(&rdu);
29328 +       if (unlikely(err))
29329 +               goto out;
29330 +
29331 +       switch (cmd) {
29332 +       case AUFS_CTL_RDU:
29333 +               err = au_rdu(file, &rdu);
29334 +               if (unlikely(err))
29335 +                       break;
29336 +
29337 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29338 +               if (unlikely(e)) {
29339 +                       err = -EFAULT;
29340 +                       AuTraceErr(err);
29341 +               }
29342 +               break;
29343 +       case AUFS_CTL_RDU_INO:
29344 +               err = au_rdu_ino(file, &rdu);
29345 +               break;
29346 +
29347 +       default:
29348 +               /* err = -ENOTTY; */
29349 +               err = -EINVAL;
29350 +       }
29351 +
29352 +out:
29353 +       AuTraceErr(err);
29354 +       return err;
29355 +}
29356 +
29357 +#ifdef CONFIG_COMPAT
29358 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29359 +{
29360 +       long err, e;
29361 +       struct aufs_rdu rdu;
29362 +       void __user *p = compat_ptr(arg);
29363 +
29364 +       /* todo: get_user()? */
29365 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29366 +       if (unlikely(err)) {
29367 +               err = -EFAULT;
29368 +               AuTraceErr(err);
29369 +               goto out;
29370 +       }
29371 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29372 +       err = au_rdu_verify(&rdu);
29373 +       if (unlikely(err))
29374 +               goto out;
29375 +
29376 +       switch (cmd) {
29377 +       case AUFS_CTL_RDU:
29378 +               err = au_rdu(file, &rdu);
29379 +               if (unlikely(err))
29380 +                       break;
29381 +
29382 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29383 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29384 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29385 +               if (unlikely(e)) {
29386 +                       err = -EFAULT;
29387 +                       AuTraceErr(err);
29388 +               }
29389 +               break;
29390 +       case AUFS_CTL_RDU_INO:
29391 +               err = au_rdu_ino(file, &rdu);
29392 +               break;
29393 +
29394 +       default:
29395 +               /* err = -ENOTTY; */
29396 +               err = -EINVAL;
29397 +       }
29398 +
29399 +out:
29400 +       AuTraceErr(err);
29401 +       return err;
29402 +}
29403 +#endif
29404 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29405 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29406 +++ linux/fs/aufs/rwsem.h       2022-08-02 12:12:45.168975258 +0200
29407 @@ -0,0 +1,85 @@
29408 +/* SPDX-License-Identifier: GPL-2.0 */
29409 +/*
29410 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29411 + *
29412 + * This program, aufs is free software; you can redistribute it and/or modify
29413 + * it under the terms of the GNU General Public License as published by
29414 + * the Free Software Foundation; either version 2 of the License, or
29415 + * (at your option) any later version.
29416 + *
29417 + * This program is distributed in the hope that it will be useful,
29418 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29419 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29420 + * GNU General Public License for more details.
29421 + *
29422 + * You should have received a copy of the GNU General Public License
29423 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29424 + */
29425 +
29426 +/*
29427 + * simple read-write semaphore wrappers
29428 + */
29429 +
29430 +#ifndef __AUFS_RWSEM_H__
29431 +#define __AUFS_RWSEM_H__
29432 +
29433 +#ifdef __KERNEL__
29434 +
29435 +#include "debug.h"
29436 +
29437 +/* in the future, the name 'au_rwsem' will be totally gone */
29438 +#define au_rwsem       rw_semaphore
29439 +
29440 +/* to debug easier, do not make them inlined functions */
29441 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29442 +
29443 +#ifdef CONFIG_LOCKDEP
29444 +/* rwsem_is_locked() is unusable */
29445 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29446 +                                         && !lockdep_recursing(current) \
29447 +                                         && debug_locks                \
29448 +                                         && !lockdep_is_held_type(rw, 1))
29449 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29450 +                                         && !lockdep_recursing(current) \
29451 +                                         && debug_locks                \
29452 +                                         && !lockdep_is_held_type(rw, 0))
29453 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29454 +                                         && !lockdep_recursing(current) \
29455 +                                         && debug_locks                \
29456 +                                         && !lockdep_is_held(rw))
29457 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29458 +                                         && !lockdep_recursing(current) \
29459 +                                         && debug_locks                \
29460 +                                         && lockdep_is_held(rw))
29461 +#else
29462 +#define AuRwMustReadLock(rw)   do {} while (0)
29463 +#define AuRwMustWriteLock(rw)  do {} while (0)
29464 +#define AuRwMustAnyLock(rw)    do {} while (0)
29465 +#define AuRwDestroy(rw)                do {} while (0)
29466 +#endif
29467 +
29468 +#define au_rw_init(rw) init_rwsem(rw)
29469 +
29470 +#define au_rw_init_wlock(rw) do {              \
29471 +               au_rw_init(rw);                 \
29472 +               down_write(rw);                 \
29473 +       } while (0)
29474 +
29475 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29476 +               au_rw_init(rw);                 \
29477 +               down_write_nested(rw, lsc);     \
29478 +       } while (0)
29479 +
29480 +#define au_rw_read_lock(rw)            down_read(rw)
29481 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29482 +#define au_rw_read_unlock(rw)          up_read(rw)
29483 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29484 +#define au_rw_write_lock(rw)           down_write(rw)
29485 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29486 +#define au_rw_write_unlock(rw)         up_write(rw)
29487 +/* why is not _nested version defined? */
29488 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29489 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29490 +
29491 +#endif /* __KERNEL__ */
29492 +#endif /* __AUFS_RWSEM_H__ */
29493 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29494 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29495 +++ linux/fs/aufs/sbinfo.c      2022-08-02 12:12:45.168975258 +0200
29496 @@ -0,0 +1,316 @@
29497 +// SPDX-License-Identifier: GPL-2.0
29498 +/*
29499 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29500 + *
29501 + * This program, aufs is free software; you can redistribute it and/or modify
29502 + * it under the terms of the GNU General Public License as published by
29503 + * the Free Software Foundation; either version 2 of the License, or
29504 + * (at your option) any later version.
29505 + *
29506 + * This program is distributed in the hope that it will be useful,
29507 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29508 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29509 + * GNU General Public License for more details.
29510 + *
29511 + * You should have received a copy of the GNU General Public License
29512 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29513 + */
29514 +
29515 +/*
29516 + * superblock private data
29517 + */
29518 +
29519 +#include <linux/iversion.h>
29520 +#include "aufs.h"
29521 +
29522 +/*
29523 + * they are necessary regardless sysfs is disabled.
29524 + */
29525 +void au_si_free(struct kobject *kobj)
29526 +{
29527 +       int i;
29528 +       struct au_sbinfo *sbinfo;
29529 +       char *locked __maybe_unused; /* debug only */
29530 +
29531 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29532 +       for (i = 0; i < AuPlink_NHASH; i++)
29533 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29534 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29535 +
29536 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29537 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29538 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29539 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29540 +
29541 +       dbgaufs_si_fin(sbinfo);
29542 +       au_rw_write_lock(&sbinfo->si_rwsem);
29543 +       au_br_free(sbinfo);
29544 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29545 +
29546 +       au_kfree_try_rcu(sbinfo->si_branch);
29547 +       mutex_destroy(&sbinfo->si_xib_mtx);
29548 +       AuRwDestroy(&sbinfo->si_rwsem);
29549 +
29550 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29551 +       /* si_nfiles is waited too */
29552 +       au_kfree_rcu(sbinfo);
29553 +}
29554 +
29555 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29556 +{
29557 +       struct au_sbinfo *sbinfo;
29558 +       int err, i;
29559 +
29560 +       err = -ENOMEM;
29561 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29562 +       if (unlikely(!sbinfo))
29563 +               goto out;
29564 +
29565 +       /* will be reallocated separately */
29566 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29567 +       if (unlikely(!sbinfo->si_branch))
29568 +               goto out_sbinfo;
29569 +
29570 +       err = sysaufs_si_init(sbinfo);
29571 +       if (!err) {
29572 +               dbgaufs_si_null(sbinfo);
29573 +               err = dbgaufs_si_init(sbinfo);
29574 +               if (unlikely(err))
29575 +                       kobject_put(&sbinfo->si_kobj);
29576 +       }
29577 +       if (unlikely(err))
29578 +               goto out_br;
29579 +
29580 +       au_nwt_init(&sbinfo->si_nowait);
29581 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29582 +
29583 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29584 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29585 +
29586 +       sbinfo->si_bbot = -1;
29587 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29588 +
29589 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29590 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29591 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29592 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29593 +
29594 +       au_fhsm_init(sbinfo);
29595 +
29596 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29597 +
29598 +       sbinfo->si_xino_jiffy = jiffies;
29599 +       sbinfo->si_xino_expire
29600 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29601 +       mutex_init(&sbinfo->si_xib_mtx);
29602 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29603 +
29604 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29605 +
29606 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29607 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29608 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29609 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29610 +
29611 +       for (i = 0; i < AuPlink_NHASH; i++)
29612 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29613 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29614 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29615 +
29616 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29617 +
29618 +       /* with getattr by default */
29619 +       sbinfo->si_iop_array = aufs_iop;
29620 +
29621 +       /* leave other members for sysaufs and si_mnt. */
29622 +       sbinfo->si_sb = sb;
29623 +       if (sb) {
29624 +               sb->s_fs_info = sbinfo;
29625 +               si_pid_set(sb);
29626 +       }
29627 +       return sbinfo; /* success */
29628 +
29629 +out_br:
29630 +       au_kfree_try_rcu(sbinfo->si_branch);
29631 +out_sbinfo:
29632 +       au_kfree_rcu(sbinfo);
29633 +out:
29634 +       return ERR_PTR(err);
29635 +}
29636 +
29637 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29638 +{
29639 +       int err, sz;
29640 +       struct au_branch **brp;
29641 +
29642 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29643 +
29644 +       err = -ENOMEM;
29645 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29646 +       if (unlikely(!sz))
29647 +               sz = sizeof(*brp);
29648 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29649 +                          may_shrink);
29650 +       if (brp) {
29651 +               sbinfo->si_branch = brp;
29652 +               err = 0;
29653 +       }
29654 +
29655 +       return err;
29656 +}
29657 +
29658 +/* ---------------------------------------------------------------------- */
29659 +
29660 +unsigned int au_sigen_inc(struct super_block *sb)
29661 +{
29662 +       unsigned int gen;
29663 +       struct inode *inode;
29664 +
29665 +       SiMustWriteLock(sb);
29666 +
29667 +       gen = ++au_sbi(sb)->si_generation;
29668 +       au_update_digen(sb->s_root);
29669 +       inode = d_inode(sb->s_root);
29670 +       au_update_iigen(inode, /*half*/0);
29671 +       inode_inc_iversion(inode);
29672 +       return gen;
29673 +}
29674 +
29675 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29676 +{
29677 +       aufs_bindex_t br_id;
29678 +       int i;
29679 +       struct au_sbinfo *sbinfo;
29680 +
29681 +       SiMustWriteLock(sb);
29682 +
29683 +       sbinfo = au_sbi(sb);
29684 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29685 +               br_id = ++sbinfo->si_last_br_id;
29686 +               AuDebugOn(br_id < 0);
29687 +               if (br_id && au_br_index(sb, br_id) < 0)
29688 +                       return br_id;
29689 +       }
29690 +
29691 +       return -1;
29692 +}
29693 +
29694 +/* ---------------------------------------------------------------------- */
29695 +
29696 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29697 +int si_read_lock(struct super_block *sb, int flags)
29698 +{
29699 +       int err;
29700 +
29701 +       err = 0;
29702 +       if (au_ftest_lock(flags, FLUSH))
29703 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29704 +
29705 +       si_noflush_read_lock(sb);
29706 +       err = au_plink_maint(sb, flags);
29707 +       if (unlikely(err))
29708 +               si_read_unlock(sb);
29709 +
29710 +       return err;
29711 +}
29712 +
29713 +int si_write_lock(struct super_block *sb, int flags)
29714 +{
29715 +       int err;
29716 +
29717 +       if (au_ftest_lock(flags, FLUSH))
29718 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29719 +
29720 +       si_noflush_write_lock(sb);
29721 +       err = au_plink_maint(sb, flags);
29722 +       if (unlikely(err))
29723 +               si_write_unlock(sb);
29724 +
29725 +       return err;
29726 +}
29727 +
29728 +/* dentry and super_block lock. call at entry point */
29729 +int aufs_read_lock(struct dentry *dentry, int flags)
29730 +{
29731 +       int err;
29732 +       struct super_block *sb;
29733 +
29734 +       sb = dentry->d_sb;
29735 +       err = si_read_lock(sb, flags);
29736 +       if (unlikely(err))
29737 +               goto out;
29738 +
29739 +       if (au_ftest_lock(flags, DW))
29740 +               di_write_lock_child(dentry);
29741 +       else
29742 +               di_read_lock_child(dentry, flags);
29743 +
29744 +       if (au_ftest_lock(flags, GEN)) {
29745 +               err = au_digen_test(dentry, au_sigen(sb));
29746 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29747 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29748 +               else if (!err)
29749 +                       err = au_dbrange_test(dentry);
29750 +               if (unlikely(err))
29751 +                       aufs_read_unlock(dentry, flags);
29752 +       }
29753 +
29754 +out:
29755 +       return err;
29756 +}
29757 +
29758 +void aufs_read_unlock(struct dentry *dentry, int flags)
29759 +{
29760 +       if (au_ftest_lock(flags, DW))
29761 +               di_write_unlock(dentry);
29762 +       else
29763 +               di_read_unlock(dentry, flags);
29764 +       si_read_unlock(dentry->d_sb);
29765 +}
29766 +
29767 +void aufs_write_lock(struct dentry *dentry)
29768 +{
29769 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29770 +       di_write_lock_child(dentry);
29771 +}
29772 +
29773 +void aufs_write_unlock(struct dentry *dentry)
29774 +{
29775 +       di_write_unlock(dentry);
29776 +       si_write_unlock(dentry->d_sb);
29777 +}
29778 +
29779 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29780 +{
29781 +       int err;
29782 +       unsigned int sigen;
29783 +       struct super_block *sb;
29784 +
29785 +       sb = d1->d_sb;
29786 +       err = si_read_lock(sb, flags);
29787 +       if (unlikely(err))
29788 +               goto out;
29789 +
29790 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29791 +
29792 +       if (au_ftest_lock(flags, GEN)) {
29793 +               sigen = au_sigen(sb);
29794 +               err = au_digen_test(d1, sigen);
29795 +               AuDebugOn(!err && au_dbrange_test(d1));
29796 +               if (!err) {
29797 +                       err = au_digen_test(d2, sigen);
29798 +                       AuDebugOn(!err && au_dbrange_test(d2));
29799 +               }
29800 +               if (unlikely(err))
29801 +                       aufs_read_and_write_unlock2(d1, d2);
29802 +       }
29803 +
29804 +out:
29805 +       return err;
29806 +}
29807 +
29808 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29809 +{
29810 +       di_write_unlock2(d1, d2);
29811 +       si_read_unlock(d1->d_sb);
29812 +}
29813 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29814 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29815 +++ linux/fs/aufs/super.c       2022-08-02 12:12:45.168975258 +0200
29816 @@ -0,0 +1,871 @@
29817 +// SPDX-License-Identifier: GPL-2.0
29818 +/*
29819 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29820 + *
29821 + * This program, aufs is free software; you can redistribute it and/or modify
29822 + * it under the terms of the GNU General Public License as published by
29823 + * the Free Software Foundation; either version 2 of the License, or
29824 + * (at your option) any later version.
29825 + *
29826 + * This program is distributed in the hope that it will be useful,
29827 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29828 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29829 + * GNU General Public License for more details.
29830 + *
29831 + * You should have received a copy of the GNU General Public License
29832 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29833 + */
29834 +
29835 +/*
29836 + * mount and super_block operations
29837 + */
29838 +
29839 +#include <linux/iversion.h>
29840 +#include <linux/mm.h>
29841 +#include <linux/seq_file.h>
29842 +#include <linux/statfs.h>
29843 +#include <linux/vmalloc.h>
29844 +#include "aufs.h"
29845 +
29846 +/*
29847 + * super_operations
29848 + */
29849 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29850 +{
29851 +       struct au_icntnr *c;
29852 +
29853 +       c = au_cache_alloc_icntnr(sb);
29854 +       if (c) {
29855 +               au_icntnr_init(c);
29856 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29857 +               c->iinfo.ii_hinode = NULL;
29858 +               return &c->vfs_inode;
29859 +       }
29860 +       return NULL;
29861 +}
29862 +
29863 +static void aufs_destroy_inode(struct inode *inode)
29864 +{
29865 +       if (!au_is_bad_inode(inode))
29866 +               au_iinfo_fin(inode);
29867 +}
29868 +
29869 +static void aufs_free_inode(struct inode *inode)
29870 +{
29871 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29872 +}
29873 +
29874 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29875 +{
29876 +       struct inode *inode;
29877 +       int err;
29878 +
29879 +       inode = iget_locked(sb, ino);
29880 +       if (unlikely(!inode)) {
29881 +               inode = ERR_PTR(-ENOMEM);
29882 +               goto out;
29883 +       }
29884 +       if (!(inode->i_state & I_NEW))
29885 +               goto out;
29886 +
29887 +       err = au_xigen_new(inode);
29888 +       if (!err)
29889 +               err = au_iinfo_init(inode);
29890 +       if (!err)
29891 +               inode_inc_iversion(inode);
29892 +       else {
29893 +               iget_failed(inode);
29894 +               inode = ERR_PTR(err);
29895 +       }
29896 +
29897 +out:
29898 +       /* never return NULL */
29899 +       AuDebugOn(!inode);
29900 +       AuTraceErrPtr(inode);
29901 +       return inode;
29902 +}
29903 +
29904 +/* lock free root dinfo */
29905 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29906 +{
29907 +       int err;
29908 +       aufs_bindex_t bindex, bbot;
29909 +       struct path path;
29910 +       struct au_hdentry *hdp;
29911 +       struct au_branch *br;
29912 +       au_br_perm_str_t perm;
29913 +
29914 +       err = 0;
29915 +       bbot = au_sbbot(sb);
29916 +       bindex = 0;
29917 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29918 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29919 +               br = au_sbr(sb, bindex);
29920 +               path.mnt = au_br_mnt(br);
29921 +               path.dentry = hdp->hd_dentry;
29922 +               err = au_seq_path(seq, &path);
29923 +               if (!err) {
29924 +                       au_optstr_br_perm(&perm, br->br_perm);
29925 +                       seq_printf(seq, "=%s", perm.a);
29926 +                       if (bindex != bbot)
29927 +                               seq_putc(seq, ':');
29928 +               }
29929 +       }
29930 +       if (unlikely(err || seq_has_overflowed(seq)))
29931 +               err = -E2BIG;
29932 +
29933 +       return err;
29934 +}
29935 +
29936 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29937 +                      const char *append)
29938 +{
29939 +       char *p;
29940 +
29941 +       p = fmt;
29942 +       while (*pat != ':')
29943 +               *p++ = *pat++;
29944 +       *p++ = *pat++;
29945 +       strcpy(p, append);
29946 +       AuDebugOn(strlen(fmt) >= len);
29947 +}
29948 +
29949 +static void au_show_wbr_create(struct seq_file *m, int v,
29950 +                              struct au_sbinfo *sbinfo)
29951 +{
29952 +       const char *pat;
29953 +       char fmt[32];
29954 +       struct au_wbr_mfs *mfs;
29955 +
29956 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29957 +
29958 +       seq_puts(m, ",create=");
29959 +       pat = au_optstr_wbr_create(v);
29960 +       mfs = &sbinfo->si_wbr_mfs;
29961 +       switch (v) {
29962 +       case AuWbrCreate_TDP:
29963 +       case AuWbrCreate_RR:
29964 +       case AuWbrCreate_MFS:
29965 +       case AuWbrCreate_PMFS:
29966 +               seq_puts(m, pat);
29967 +               break;
29968 +       case AuWbrCreate_MFSRR:
29969 +       case AuWbrCreate_TDMFS:
29970 +       case AuWbrCreate_PMFSRR:
29971 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29972 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29973 +               break;
29974 +       case AuWbrCreate_MFSV:
29975 +       case AuWbrCreate_PMFSV:
29976 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29977 +               seq_printf(m, fmt,
29978 +                          jiffies_to_msecs(mfs->mfs_expire)
29979 +                          / MSEC_PER_SEC);
29980 +               break;
29981 +       case AuWbrCreate_MFSRRV:
29982 +       case AuWbrCreate_TDMFSV:
29983 +       case AuWbrCreate_PMFSRRV:
29984 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29985 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29986 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29987 +               break;
29988 +       default:
29989 +               BUG();
29990 +       }
29991 +}
29992 +
29993 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29994 +{
29995 +#ifdef CONFIG_SYSFS
29996 +       return 0;
29997 +#else
29998 +       int err;
29999 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
30000 +       aufs_bindex_t bindex, brid;
30001 +       struct qstr *name;
30002 +       struct file *f;
30003 +       struct dentry *d, *h_root;
30004 +       struct au_branch *br;
30005 +
30006 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30007 +
30008 +       err = 0;
30009 +       f = au_sbi(sb)->si_xib;
30010 +       if (!f)
30011 +               goto out;
30012 +
30013 +       /* stop printing the default xino path on the first writable branch */
30014 +       h_root = NULL;
30015 +       bindex = au_xi_root(sb, f->f_path.dentry);
30016 +       if (bindex >= 0) {
30017 +               br = au_sbr_sb(sb, bindex);
30018 +               h_root = au_br_dentry(br);
30019 +       }
30020 +
30021 +       d = f->f_path.dentry;
30022 +       name = &d->d_name;
30023 +       /* safe ->d_parent because the file is unlinked */
30024 +       if (d->d_parent == h_root
30025 +           && name->len == len
30026 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
30027 +               goto out;
30028 +
30029 +       seq_puts(seq, ",xino=");
30030 +       err = au_xino_path(seq, f);
30031 +
30032 +out:
30033 +       return err;
30034 +#endif
30035 +}
30036 +
30037 +/* seq_file will re-call me in case of too long string */
30038 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
30039 +{
30040 +       int err;
30041 +       unsigned int mnt_flags, v;
30042 +       struct super_block *sb;
30043 +       struct au_sbinfo *sbinfo;
30044 +
30045 +#define AuBool(name, str) do { \
30046 +       v = au_opt_test(mnt_flags, name); \
30047 +       if (v != au_opt_test(AuOpt_Def, name)) \
30048 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
30049 +} while (0)
30050 +
30051 +#define AuStr(name, str) do { \
30052 +       v = mnt_flags & AuOptMask_##name; \
30053 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
30054 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30055 +} while (0)
30056 +
30057 +#define AuUInt(name, str, val) do { \
30058 +       if (val != AUFS_##name##_DEF) \
30059 +               seq_printf(m, "," #str "=%u", val); \
30060 +} while (0)
30061 +
30062 +       sb = dentry->d_sb;
30063 +       if (sb->s_flags & SB_POSIXACL)
30064 +               seq_puts(m, ",acl");
30065 +#if 0 /* reserved for future use */
30066 +       if (sb->s_flags & SB_I_VERSION)
30067 +               seq_puts(m, ",i_version");
30068 +#endif
30069 +
30070 +       /* lock free root dinfo */
30071 +       si_noflush_read_lock(sb);
30072 +       sbinfo = au_sbi(sb);
30073 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30074 +
30075 +       mnt_flags = au_mntflags(sb);
30076 +       if (au_opt_test(mnt_flags, XINO)) {
30077 +               err = au_show_xino(m, sb);
30078 +               if (unlikely(err))
30079 +                       goto out;
30080 +       } else
30081 +               seq_puts(m, ",noxino");
30082 +
30083 +       AuBool(TRUNC_XINO, trunc_xino);
30084 +       AuStr(UDBA, udba);
30085 +       AuBool(SHWH, shwh);
30086 +       AuBool(PLINK, plink);
30087 +       AuBool(DIO, dio);
30088 +       AuBool(DIRPERM1, dirperm1);
30089 +
30090 +       v = sbinfo->si_wbr_create;
30091 +       if (v != AuWbrCreate_Def)
30092 +               au_show_wbr_create(m, v, sbinfo);
30093 +
30094 +       v = sbinfo->si_wbr_copyup;
30095 +       if (v != AuWbrCopyup_Def)
30096 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30097 +
30098 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30099 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30100 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30101 +
30102 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30103 +
30104 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30105 +       AuUInt(RDCACHE, rdcache, v);
30106 +
30107 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30108 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30109 +
30110 +       au_fhsm_show(m, sbinfo);
30111 +
30112 +       AuBool(DIRREN, dirren);
30113 +       AuBool(SUM, sum);
30114 +       /* AuBool(SUM_W, wsum); */
30115 +       AuBool(WARN_PERM, warn_perm);
30116 +       AuBool(VERBOSE, verbose);
30117 +
30118 +out:
30119 +       /* be sure to print "br:" last */
30120 +       if (!sysaufs_brs) {
30121 +               seq_puts(m, ",br:");
30122 +               au_show_brs(m, sb);
30123 +       }
30124 +       si_read_unlock(sb);
30125 +       return 0;
30126 +
30127 +#undef AuBool
30128 +#undef AuStr
30129 +#undef AuUInt
30130 +}
30131 +
30132 +/* ---------------------------------------------------------------------- */
30133 +
30134 +/* sum mode which returns the summation for statfs(2) */
30135 +
30136 +static u64 au_add_till_max(u64 a, u64 b)
30137 +{
30138 +       u64 old;
30139 +
30140 +       old = a;
30141 +       a += b;
30142 +       if (old <= a)
30143 +               return a;
30144 +       return ULLONG_MAX;
30145 +}
30146 +
30147 +static u64 au_mul_till_max(u64 a, long mul)
30148 +{
30149 +       u64 old;
30150 +
30151 +       old = a;
30152 +       a *= mul;
30153 +       if (old <= a)
30154 +               return a;
30155 +       return ULLONG_MAX;
30156 +}
30157 +
30158 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30159 +{
30160 +       int err;
30161 +       long bsize, factor;
30162 +       u64 blocks, bfree, bavail, files, ffree;
30163 +       aufs_bindex_t bbot, bindex, i;
30164 +       unsigned char shared;
30165 +       struct path h_path;
30166 +       struct super_block *h_sb;
30167 +
30168 +       err = 0;
30169 +       bsize = LONG_MAX;
30170 +       files = 0;
30171 +       ffree = 0;
30172 +       blocks = 0;
30173 +       bfree = 0;
30174 +       bavail = 0;
30175 +       bbot = au_sbbot(sb);
30176 +       for (bindex = 0; bindex <= bbot; bindex++) {
30177 +               h_path.mnt = au_sbr_mnt(sb, bindex);
30178 +               h_sb = h_path.mnt->mnt_sb;
30179 +               shared = 0;
30180 +               for (i = 0; !shared && i < bindex; i++)
30181 +                       shared = (au_sbr_sb(sb, i) == h_sb);
30182 +               if (shared)
30183 +                       continue;
30184 +
30185 +               /* sb->s_root for NFS is unreliable */
30186 +               h_path.dentry = h_path.mnt->mnt_root;
30187 +               err = vfs_statfs(&h_path, buf);
30188 +               if (unlikely(err))
30189 +                       goto out;
30190 +
30191 +               if (bsize > buf->f_bsize) {
30192 +                       /*
30193 +                        * we will reduce bsize, so we have to expand blocks
30194 +                        * etc. to match them again
30195 +                        */
30196 +                       factor = (bsize / buf->f_bsize);
30197 +                       blocks = au_mul_till_max(blocks, factor);
30198 +                       bfree = au_mul_till_max(bfree, factor);
30199 +                       bavail = au_mul_till_max(bavail, factor);
30200 +                       bsize = buf->f_bsize;
30201 +               }
30202 +
30203 +               factor = (buf->f_bsize / bsize);
30204 +               blocks = au_add_till_max(blocks,
30205 +                               au_mul_till_max(buf->f_blocks, factor));
30206 +               bfree = au_add_till_max(bfree,
30207 +                               au_mul_till_max(buf->f_bfree, factor));
30208 +               bavail = au_add_till_max(bavail,
30209 +                               au_mul_till_max(buf->f_bavail, factor));
30210 +               files = au_add_till_max(files, buf->f_files);
30211 +               ffree = au_add_till_max(ffree, buf->f_ffree);
30212 +       }
30213 +
30214 +       buf->f_bsize = bsize;
30215 +       buf->f_blocks = blocks;
30216 +       buf->f_bfree = bfree;
30217 +       buf->f_bavail = bavail;
30218 +       buf->f_files = files;
30219 +       buf->f_ffree = ffree;
30220 +       buf->f_frsize = 0;
30221 +
30222 +out:
30223 +       return err;
30224 +}
30225 +
30226 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30227 +{
30228 +       int err;
30229 +       struct path h_path;
30230 +       struct super_block *sb;
30231 +
30232 +       /* lock free root dinfo */
30233 +       sb = dentry->d_sb;
30234 +       si_noflush_read_lock(sb);
30235 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
30236 +               /* sb->s_root for NFS is unreliable */
30237 +               h_path.mnt = au_sbr_mnt(sb, 0);
30238 +               h_path.dentry = h_path.mnt->mnt_root;
30239 +               err = vfs_statfs(&h_path, buf);
30240 +       } else
30241 +               err = au_statfs_sum(sb, buf);
30242 +       si_read_unlock(sb);
30243 +
30244 +       if (!err) {
30245 +               buf->f_type = AUFS_SUPER_MAGIC;
30246 +               buf->f_namelen = AUFS_MAX_NAMELEN;
30247 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30248 +       }
30249 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30250 +
30251 +       return err;
30252 +}
30253 +
30254 +/* ---------------------------------------------------------------------- */
30255 +
30256 +static int aufs_sync_fs(struct super_block *sb, int wait)
30257 +{
30258 +       int err, e;
30259 +       aufs_bindex_t bbot, bindex;
30260 +       struct au_branch *br;
30261 +       struct super_block *h_sb;
30262 +
30263 +       err = 0;
30264 +       si_noflush_read_lock(sb);
30265 +       bbot = au_sbbot(sb);
30266 +       for (bindex = 0; bindex <= bbot; bindex++) {
30267 +               br = au_sbr(sb, bindex);
30268 +               if (!au_br_writable(br->br_perm))
30269 +                       continue;
30270 +
30271 +               h_sb = au_sbr_sb(sb, bindex);
30272 +               e = vfsub_sync_filesystem(h_sb);
30273 +               if (unlikely(e && !err))
30274 +                       err = e;
30275 +               /* go on even if an error happens */
30276 +       }
30277 +       si_read_unlock(sb);
30278 +
30279 +       return err;
30280 +}
30281 +
30282 +/* ---------------------------------------------------------------------- */
30283 +
30284 +/* final actions when unmounting a file system */
30285 +static void aufs_put_super(struct super_block *sb)
30286 +{
30287 +       struct au_sbinfo *sbinfo;
30288 +
30289 +       sbinfo = au_sbi(sb);
30290 +       if (sbinfo)
30291 +               kobject_put(&sbinfo->si_kobj);
30292 +}
30293 +
30294 +/* ---------------------------------------------------------------------- */
30295 +
30296 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30297 +                    struct super_block *sb, void *arg)
30298 +{
30299 +       void *array;
30300 +       unsigned long long n, sz;
30301 +
30302 +       array = NULL;
30303 +       n = 0;
30304 +       if (!*hint)
30305 +               goto out;
30306 +
30307 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30308 +               array = ERR_PTR(-EMFILE);
30309 +               pr_err("hint %llu\n", *hint);
30310 +               goto out;
30311 +       }
30312 +
30313 +       sz = sizeof(array) * *hint;
30314 +       array = kzalloc(sz, GFP_NOFS);
30315 +       if (unlikely(!array))
30316 +               array = vzalloc(sz);
30317 +       if (unlikely(!array)) {
30318 +               array = ERR_PTR(-ENOMEM);
30319 +               goto out;
30320 +       }
30321 +
30322 +       n = cb(sb, array, *hint, arg);
30323 +       AuDebugOn(n > *hint);
30324 +
30325 +out:
30326 +       *hint = n;
30327 +       return array;
30328 +}
30329 +
30330 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30331 +                                      unsigned long long max __maybe_unused,
30332 +                                      void *arg)
30333 +{
30334 +       unsigned long long n;
30335 +       struct inode **p, *inode;
30336 +       struct list_head *head;
30337 +
30338 +       n = 0;
30339 +       p = a;
30340 +       head = arg;
30341 +       spin_lock(&sb->s_inode_list_lock);
30342 +       list_for_each_entry(inode, head, i_sb_list) {
30343 +               if (!au_is_bad_inode(inode)
30344 +                   && au_ii(inode)->ii_btop >= 0) {
30345 +                       spin_lock(&inode->i_lock);
30346 +                       if (atomic_read(&inode->i_count)) {
30347 +                               au_igrab(inode);
30348 +                               *p++ = inode;
30349 +                               n++;
30350 +                               AuDebugOn(n > max);
30351 +                       }
30352 +                       spin_unlock(&inode->i_lock);
30353 +               }
30354 +       }
30355 +       spin_unlock(&sb->s_inode_list_lock);
30356 +
30357 +       return n;
30358 +}
30359 +
30360 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30361 +{
30362 +       struct au_sbinfo *sbi;
30363 +
30364 +       sbi = au_sbi(sb);
30365 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30366 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30367 +}
30368 +
30369 +void au_iarray_free(struct inode **a, unsigned long long max)
30370 +{
30371 +       unsigned long long ull;
30372 +
30373 +       for (ull = 0; ull < max; ull++)
30374 +               iput(a[ull]);
30375 +       kvfree(a);
30376 +}
30377 +
30378 +/* ---------------------------------------------------------------------- */
30379 +
30380 +/*
30381 + * refresh dentry and inode at remount time.
30382 + */
30383 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30384 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30385 +                     struct dentry *parent)
30386 +{
30387 +       int err;
30388 +
30389 +       di_write_lock_child(dentry);
30390 +       di_read_lock_parent(parent, AuLock_IR);
30391 +       err = au_refresh_dentry(dentry, parent);
30392 +       if (!err && dir_flags)
30393 +               au_hn_reset(d_inode(dentry), dir_flags);
30394 +       di_read_unlock(parent, AuLock_IR);
30395 +       di_write_unlock(dentry);
30396 +
30397 +       return err;
30398 +}
30399 +
30400 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30401 +                          struct au_sbinfo *sbinfo,
30402 +                          const unsigned int dir_flags, unsigned int do_idop)
30403 +{
30404 +       int err;
30405 +       struct dentry *parent;
30406 +
30407 +       err = 0;
30408 +       parent = dget_parent(dentry);
30409 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30410 +               if (d_really_is_positive(dentry)) {
30411 +                       if (!d_is_dir(dentry))
30412 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30413 +                                                parent);
30414 +                       else {
30415 +                               err = au_do_refresh(dentry, dir_flags, parent);
30416 +                               if (unlikely(err))
30417 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30418 +                       }
30419 +               } else
30420 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30421 +               AuDbgDentry(dentry);
30422 +       }
30423 +       dput(parent);
30424 +
30425 +       if (!err) {
30426 +               if (do_idop)
30427 +                       au_refresh_dop(dentry, /*force_reval*/0);
30428 +       } else
30429 +               au_refresh_dop(dentry, /*force_reval*/1);
30430 +
30431 +       AuTraceErr(err);
30432 +       return err;
30433 +}
30434 +
30435 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30436 +{
30437 +       int err, i, j, ndentry, e;
30438 +       unsigned int sigen;
30439 +       struct au_dcsub_pages dpages;
30440 +       struct au_dpage *dpage;
30441 +       struct dentry **dentries, *d;
30442 +       struct au_sbinfo *sbinfo;
30443 +       struct dentry *root = sb->s_root;
30444 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30445 +
30446 +       if (do_idop)
30447 +               au_refresh_dop(root, /*force_reval*/0);
30448 +
30449 +       err = au_dpages_init(&dpages, GFP_NOFS);
30450 +       if (unlikely(err))
30451 +               goto out;
30452 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30453 +       if (unlikely(err))
30454 +               goto out_dpages;
30455 +
30456 +       sigen = au_sigen(sb);
30457 +       sbinfo = au_sbi(sb);
30458 +       for (i = 0; i < dpages.ndpage; i++) {
30459 +               dpage = dpages.dpages + i;
30460 +               dentries = dpage->dentries;
30461 +               ndentry = dpage->ndentry;
30462 +               for (j = 0; j < ndentry; j++) {
30463 +                       d = dentries[j];
30464 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30465 +                                           do_idop);
30466 +                       if (unlikely(e && !err))
30467 +                               err = e;
30468 +                       /* go on even err */
30469 +               }
30470 +       }
30471 +
30472 +out_dpages:
30473 +       au_dpages_free(&dpages);
30474 +out:
30475 +       return err;
30476 +}
30477 +
30478 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30479 +{
30480 +       int err, e;
30481 +       unsigned int sigen;
30482 +       unsigned long long max, ull;
30483 +       struct inode *inode, **array;
30484 +
30485 +       array = au_iarray_alloc(sb, &max);
30486 +       err = PTR_ERR(array);
30487 +       if (IS_ERR(array))
30488 +               goto out;
30489 +
30490 +       err = 0;
30491 +       sigen = au_sigen(sb);
30492 +       for (ull = 0; ull < max; ull++) {
30493 +               inode = array[ull];
30494 +               if (unlikely(!inode))
30495 +                       break;
30496 +
30497 +               e = 0;
30498 +               ii_write_lock_child(inode);
30499 +               if (au_iigen(inode, NULL) != sigen) {
30500 +                       e = au_refresh_hinode_self(inode);
30501 +                       if (unlikely(e)) {
30502 +                               au_refresh_iop(inode, /*force_getattr*/1);
30503 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30504 +                               if (!err)
30505 +                                       err = e;
30506 +                               /* go on even if err */
30507 +                       }
30508 +               }
30509 +               if (!e && do_idop)
30510 +                       au_refresh_iop(inode, /*force_getattr*/0);
30511 +               ii_write_unlock(inode);
30512 +       }
30513 +
30514 +       au_iarray_free(array, max);
30515 +
30516 +out:
30517 +       return err;
30518 +}
30519 +
30520 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30521 +{
30522 +       int err, e;
30523 +       unsigned int udba;
30524 +       aufs_bindex_t bindex, bbot;
30525 +       struct dentry *root;
30526 +       struct inode *inode;
30527 +       struct au_branch *br;
30528 +       struct au_sbinfo *sbi;
30529 +
30530 +       au_sigen_inc(sb);
30531 +       sbi = au_sbi(sb);
30532 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30533 +
30534 +       root = sb->s_root;
30535 +       DiMustNoWaiters(root);
30536 +       inode = d_inode(root);
30537 +       IiMustNoWaiters(inode);
30538 +
30539 +       udba = au_opt_udba(sb);
30540 +       bbot = au_sbbot(sb);
30541 +       for (bindex = 0; bindex <= bbot; bindex++) {
30542 +               br = au_sbr(sb, bindex);
30543 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30544 +               if (unlikely(err))
30545 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30546 +                               bindex, err);
30547 +               /* go on even if err */
30548 +       }
30549 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30550 +
30551 +       if (do_idop) {
30552 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30553 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30554 +                       sb->s_d_op = &aufs_dop_noreval;
30555 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30556 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30557 +               } else {
30558 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30559 +                       sb->s_d_op = &aufs_dop;
30560 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30561 +                       sbi->si_iop_array = aufs_iop;
30562 +               }
30563 +               pr_info("reset to %ps and %ps\n",
30564 +                       sb->s_d_op, sbi->si_iop_array);
30565 +       }
30566 +
30567 +       di_write_unlock(root);
30568 +       err = au_refresh_d(sb, do_idop);
30569 +       e = au_refresh_i(sb, do_idop);
30570 +       if (unlikely(e && !err))
30571 +               err = e;
30572 +       /* aufs_write_lock() calls ..._child() */
30573 +       di_write_lock_child(root);
30574 +
30575 +       au_cpup_attr_all(inode, /*force*/1);
30576 +
30577 +       if (unlikely(err))
30578 +               AuIOErr("refresh failed, ignored, %d\n", err);
30579 +}
30580 +
30581 +const struct super_operations aufs_sop = {
30582 +       .alloc_inode    = aufs_alloc_inode,
30583 +       .destroy_inode  = aufs_destroy_inode,
30584 +       .free_inode     = aufs_free_inode,
30585 +       /* always deleting, no clearing */
30586 +       .drop_inode     = generic_delete_inode,
30587 +       .show_options   = aufs_show_options,
30588 +       .statfs         = aufs_statfs,
30589 +       .put_super      = aufs_put_super,
30590 +       .sync_fs        = aufs_sync_fs
30591 +};
30592 +
30593 +/* ---------------------------------------------------------------------- */
30594 +
30595 +int au_alloc_root(struct super_block *sb)
30596 +{
30597 +       int err;
30598 +       struct inode *inode;
30599 +       struct dentry *root;
30600 +
30601 +       err = -ENOMEM;
30602 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30603 +       err = PTR_ERR(inode);
30604 +       if (IS_ERR(inode))
30605 +               goto out;
30606 +
30607 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30608 +       inode->i_fop = &aufs_dir_fop;
30609 +       inode->i_mode = S_IFDIR;
30610 +       set_nlink(inode, 2);
30611 +       unlock_new_inode(inode);
30612 +
30613 +       root = d_make_root(inode);
30614 +       if (unlikely(!root))
30615 +               goto out;
30616 +       err = PTR_ERR(root);
30617 +       if (IS_ERR(root))
30618 +               goto out;
30619 +
30620 +       err = au_di_init(root);
30621 +       if (!err) {
30622 +               sb->s_root = root;
30623 +               return 0; /* success */
30624 +       }
30625 +       dput(root);
30626 +
30627 +out:
30628 +       return err;
30629 +}
30630 +
30631 +/* ---------------------------------------------------------------------- */
30632 +
30633 +static void aufs_kill_sb(struct super_block *sb)
30634 +{
30635 +       struct au_sbinfo *sbinfo;
30636 +       struct dentry *root;
30637 +
30638 +       sbinfo = au_sbi(sb);
30639 +       if (!sbinfo)
30640 +               goto out;
30641 +
30642 +       au_sbilist_del(sb);
30643 +
30644 +       root = sb->s_root;
30645 +       if (root)
30646 +               aufs_write_lock(root);
30647 +       else
30648 +               __si_write_lock(sb);
30649 +
30650 +       au_fhsm_fin(sb);
30651 +       if (sbinfo->si_wbr_create_ops->fin)
30652 +               sbinfo->si_wbr_create_ops->fin(sb);
30653 +       if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30654 +               au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30655 +               au_remount_refresh(sb, /*do_idop*/0);
30656 +       }
30657 +       if (au_opt_test(sbinfo->si_mntflags, PLINK))
30658 +               au_plink_put(sb, /*verbose*/1);
30659 +       au_xino_clr(sb);
30660 +       if (root)
30661 +               au_dr_opt_flush(sb);
30662 +
30663 +       if (root)
30664 +               aufs_write_unlock(root);
30665 +       else
30666 +               __si_write_unlock(sb);
30667 +
30668 +       sbinfo->si_sb = NULL;
30669 +       au_nwt_flush(&sbinfo->si_nowait);
30670 +
30671 +out:
30672 +       kill_anon_super(sb);
30673 +}
30674 +
30675 +struct file_system_type aufs_fs_type = {
30676 +       .name           = AUFS_FSTYPE,
30677 +       /* a race between rename and others */
30678 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30679 +                               /* untested */
30680 +                               /*| FS_ALLOW_IDMAP*/
30681 +                               ,
30682 +       .init_fs_context = aufs_fsctx_init,
30683 +       .parameters     = aufs_fsctx_paramspec,
30684 +       .kill_sb        = aufs_kill_sb,
30685 +       /* no need to __module_get() and module_put(). */
30686 +       .owner          = THIS_MODULE,
30687 +};
30688 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30689 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30690 +++ linux/fs/aufs/super.h       2022-08-02 12:12:45.168975258 +0200
30691 @@ -0,0 +1,592 @@
30692 +/* SPDX-License-Identifier: GPL-2.0 */
30693 +/*
30694 + * Copyright (C) 2005-2021 Junjiro R. Okajima
30695 + *
30696 + * This program, aufs is free software; you can redistribute it and/or modify
30697 + * it under the terms of the GNU General Public License as published by
30698 + * the Free Software Foundation; either version 2 of the License, or
30699 + * (at your option) any later version.
30700 + *
30701 + * This program is distributed in the hope that it will be useful,
30702 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30703 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30704 + * GNU General Public License for more details.
30705 + *
30706 + * You should have received a copy of the GNU General Public License
30707 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30708 + */
30709 +
30710 +/*
30711 + * super_block operations
30712 + */
30713 +
30714 +#ifndef __AUFS_SUPER_H__
30715 +#define __AUFS_SUPER_H__
30716 +
30717 +#ifdef __KERNEL__
30718 +
30719 +#include <linux/fs.h>
30720 +#include <linux/kobject.h>
30721 +#include "hbl.h"
30722 +#include "lcnt.h"
30723 +#include "rwsem.h"
30724 +#include "wkq.h"
30725 +
30726 +/* policies to select one among multiple writable branches */
30727 +struct au_wbr_copyup_operations {
30728 +       int (*copyup)(struct dentry *dentry);
30729 +};
30730 +
30731 +#define AuWbr_DIR      1               /* target is a dir */
30732 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30733 +
30734 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30735 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30736 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30737 +
30738 +struct au_wbr_create_operations {
30739 +       int (*create)(struct dentry *dentry, unsigned int flags);
30740 +       int (*init)(struct super_block *sb);
30741 +       int (*fin)(struct super_block *sb);
30742 +};
30743 +
30744 +struct au_wbr_mfs {
30745 +       struct mutex    mfs_lock; /* protect this structure */
30746 +       unsigned long   mfs_jiffy;
30747 +       unsigned long   mfs_expire;
30748 +       aufs_bindex_t   mfs_bindex;
30749 +
30750 +       unsigned long long      mfsrr_bytes;
30751 +       unsigned long long      mfsrr_watermark;
30752 +};
30753 +
30754 +#define AuPlink_NHASH 100
30755 +static inline int au_plink_hash(ino_t ino)
30756 +{
30757 +       return ino % AuPlink_NHASH;
30758 +}
30759 +
30760 +/* File-based Hierarchical Storage Management */
30761 +struct au_fhsm {
30762 +#ifdef CONFIG_AUFS_FHSM
30763 +       /* allow only one process who can receive the notification */
30764 +       spinlock_t              fhsm_spin;
30765 +       pid_t                   fhsm_pid;
30766 +       wait_queue_head_t       fhsm_wqh;
30767 +       atomic_t                fhsm_readable;
30768 +
30769 +       /* these are protected by si_rwsem */
30770 +       unsigned long           fhsm_expire;
30771 +       aufs_bindex_t           fhsm_bottom;
30772 +#endif
30773 +};
30774 +
30775 +struct au_branch;
30776 +struct au_sbinfo {
30777 +       /* nowait tasks in the system-wide workqueue */
30778 +       struct au_nowait_tasks  si_nowait;
30779 +
30780 +       /*
30781 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30782 +        * rwsem for au_sbinfo is necessary.
30783 +        */
30784 +       struct au_rwsem         si_rwsem;
30785 +
30786 +       /*
30787 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30788 +        * remount.
30789 +        */
30790 +       au_lcnt_t               si_ninodes, si_nfiles;
30791 +
30792 +       /* branch management */
30793 +       unsigned int            si_generation;
30794 +
30795 +       /* see AuSi_ flags */
30796 +       unsigned char           au_si_status;
30797 +
30798 +       aufs_bindex_t           si_bbot;
30799 +
30800 +       /* dirty trick to keep br_id plus */
30801 +       unsigned int            si_last_br_id :
30802 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30803 +       struct au_branch        **si_branch;
30804 +
30805 +       /* policy to select a writable branch */
30806 +       unsigned char           si_wbr_copyup;
30807 +       unsigned char           si_wbr_create;
30808 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30809 +       struct au_wbr_create_operations *si_wbr_create_ops;
30810 +
30811 +       /* round robin */
30812 +       atomic_t                si_wbr_rr_next;
30813 +
30814 +       /* most free space */
30815 +       struct au_wbr_mfs       si_wbr_mfs;
30816 +
30817 +       /* File-based Hierarchical Storage Management */
30818 +       struct au_fhsm          si_fhsm;
30819 +
30820 +       /* mount flags */
30821 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30822 +       unsigned int            si_mntflags;
30823 +
30824 +       /* external inode number (bitmap and translation table) */
30825 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30826 +
30827 +       struct file             *si_xib;
30828 +       struct mutex            si_xib_mtx; /* protect xib members */
30829 +       unsigned long           *si_xib_buf;
30830 +       unsigned long           si_xib_last_pindex;
30831 +       int                     si_xib_next_bit;
30832 +
30833 +       unsigned long           si_xino_jiffy;
30834 +       unsigned long           si_xino_expire;
30835 +       /* reserved for future use */
30836 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30837 +
30838 +#ifdef CONFIG_AUFS_EXPORT
30839 +       /* i_generation */
30840 +       /* todo: make xigen file an array to support many inode numbers */
30841 +       struct file             *si_xigen;
30842 +       atomic_t                si_xigen_next;
30843 +#endif
30844 +
30845 +       /* dirty trick to support atomic_open */
30846 +       struct hlist_bl_head    si_aopen;
30847 +
30848 +       /* vdir parameters */
30849 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30850 +       unsigned int            si_rdblk;       /* deblk size */
30851 +       unsigned int            si_rdhash;      /* hash size */
30852 +
30853 +       /*
30854 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30855 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30856 +        * future fsck.aufs or kernel thread will remove them later.
30857 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30858 +        */
30859 +       unsigned int            si_dirwh;
30860 +
30861 +       /* pseudo_link list */
30862 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30863 +       wait_queue_head_t       si_plink_wq;
30864 +       spinlock_t              si_plink_maint_lock;
30865 +       pid_t                   si_plink_maint_pid;
30866 +
30867 +       /* file list */
30868 +       struct hlist_bl_head    si_files;
30869 +
30870 +       /* with/without getattr, brother of sb->s_d_op */
30871 +       const struct inode_operations *si_iop_array;
30872 +
30873 +       /*
30874 +        * sysfs and lifetime management.
30875 +        * this is not a small structure and it may be a waste of memory in case
30876 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30877 +        * but using sysfs is majority.
30878 +        */
30879 +       struct kobject          si_kobj;
30880 +#ifdef CONFIG_DEBUG_FS
30881 +       struct dentry            *si_dbgaufs;
30882 +       struct dentry            *si_dbgaufs_plink;
30883 +       struct dentry            *si_dbgaufs_xib;
30884 +#ifdef CONFIG_AUFS_EXPORT
30885 +       struct dentry            *si_dbgaufs_xigen;
30886 +#endif
30887 +#endif
30888 +
30889 +#ifdef CONFIG_AUFS_SBILIST
30890 +       struct hlist_bl_node    si_list;
30891 +#endif
30892 +
30893 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30894 +       struct super_block      *si_sb;
30895 +};
30896 +
30897 +/* sbinfo status flags */
30898 +/*
30899 + * set true when refresh_dirs() failed at remount time.
30900 + * then try refreshing dirs at access time again.
30901 + * if it is false, refreshing dirs at access time is unnecessary
30902 + */
30903 +#define AuSi_FAILED_REFRESH_DIR        1
30904 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30905 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30906 +
30907 +#ifndef CONFIG_AUFS_FHSM
30908 +#undef AuSi_FHSM
30909 +#define AuSi_FHSM              0
30910 +#endif
30911 +
30912 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30913 +                                          unsigned int flag)
30914 +{
30915 +       AuRwMustAnyLock(&sbi->si_rwsem);
30916 +       return sbi->au_si_status & flag;
30917 +}
30918 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30919 +#define au_fset_si(sbinfo, name) do { \
30920 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30921 +       (sbinfo)->au_si_status |= AuSi_##name; \
30922 +} while (0)
30923 +#define au_fclr_si(sbinfo, name) do { \
30924 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30925 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30926 +} while (0)
30927 +
30928 +/* ---------------------------------------------------------------------- */
30929 +
30930 +/* policy to select one among writable branches */
30931 +#define AuWbrCopyup(sbinfo, ...) \
30932 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30933 +#define AuWbrCreate(sbinfo, ...) \
30934 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30935 +
30936 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30937 +#define AuLock_DW              1               /* write-lock dentry */
30938 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30939 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30940 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30941 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30942 +                                               /* except RENAME_EXCHANGE */
30943 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30944 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30945 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30946 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30947 +#define au_fset_lock(flags, name) \
30948 +       do { (flags) |= AuLock_##name; } while (0)
30949 +#define au_fclr_lock(flags, name) \
30950 +       do { (flags) &= ~AuLock_##name; } while (0)
30951 +
30952 +/* ---------------------------------------------------------------------- */
30953 +
30954 +/* super.c */
30955 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30956 +
30957 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30958 +                                          unsigned long long max, void *arg);
30959 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30960 +                    struct super_block *sb, void *arg);
30961 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30962 +void au_iarray_free(struct inode **a, unsigned long long max);
30963 +
30964 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
30965 +extern const struct super_operations aufs_sop;
30966 +int au_alloc_root(struct super_block *sb);
30967 +extern struct file_system_type aufs_fs_type;
30968 +
30969 +/* sbinfo.c */
30970 +void au_si_free(struct kobject *kobj);
30971 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
30972 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30973 +
30974 +unsigned int au_sigen_inc(struct super_block *sb);
30975 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30976 +
30977 +int si_read_lock(struct super_block *sb, int flags);
30978 +int si_write_lock(struct super_block *sb, int flags);
30979 +int aufs_read_lock(struct dentry *dentry, int flags);
30980 +void aufs_read_unlock(struct dentry *dentry, int flags);
30981 +void aufs_write_lock(struct dentry *dentry);
30982 +void aufs_write_unlock(struct dentry *dentry);
30983 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30984 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30985 +
30986 +/* wbr_policy.c */
30987 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30988 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30989 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30990 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30991 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30992 +
30993 +/* mvdown.c */
30994 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30995 +
30996 +#ifdef CONFIG_AUFS_FHSM
30997 +/* fhsm.c */
30998 +
30999 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
31000 +{
31001 +       pid_t pid;
31002 +
31003 +       spin_lock(&fhsm->fhsm_spin);
31004 +       pid = fhsm->fhsm_pid;
31005 +       spin_unlock(&fhsm->fhsm_spin);
31006 +
31007 +       return pid;
31008 +}
31009 +
31010 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
31011 +void au_fhsm_wrote_all(struct super_block *sb, int force);
31012 +int au_fhsm_fd(struct super_block *sb, int oflags);
31013 +int au_fhsm_br_alloc(struct au_branch *br);
31014 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
31015 +void au_fhsm_fin(struct super_block *sb);
31016 +void au_fhsm_init(struct au_sbinfo *sbinfo);
31017 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
31018 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
31019 +#else
31020 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
31021 +          int force)
31022 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
31023 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
31024 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
31025 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
31026 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
31027 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
31028 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
31029 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
31030 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31031 +#endif
31032 +
31033 +/* ---------------------------------------------------------------------- */
31034 +
31035 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31036 +{
31037 +       return sb->s_fs_info;
31038 +}
31039 +
31040 +/* ---------------------------------------------------------------------- */
31041 +
31042 +#ifdef CONFIG_AUFS_EXPORT
31043 +int au_test_nfsd(void);
31044 +void au_export_init(struct super_block *sb);
31045 +void au_xigen_inc(struct inode *inode);
31046 +int au_xigen_new(struct inode *inode);
31047 +int au_xigen_set(struct super_block *sb, struct path *path);
31048 +void au_xigen_clr(struct super_block *sb);
31049 +
31050 +static inline int au_busy_or_stale(void)
31051 +{
31052 +       if (!au_test_nfsd())
31053 +               return -EBUSY;
31054 +       return -ESTALE;
31055 +}
31056 +#else
31057 +AuStubInt0(au_test_nfsd, void)
31058 +AuStubVoid(au_export_init, struct super_block *sb)
31059 +AuStubVoid(au_xigen_inc, struct inode *inode)
31060 +AuStubInt0(au_xigen_new, struct inode *inode)
31061 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
31062 +AuStubVoid(au_xigen_clr, struct super_block *sb)
31063 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
31064 +#endif /* CONFIG_AUFS_EXPORT */
31065 +
31066 +/* ---------------------------------------------------------------------- */
31067 +
31068 +#ifdef CONFIG_AUFS_SBILIST
31069 +/* module.c */
31070 +extern struct hlist_bl_head au_sbilist;
31071 +
31072 +static inline void au_sbilist_init(void)
31073 +{
31074 +       INIT_HLIST_BL_HEAD(&au_sbilist);
31075 +}
31076 +
31077 +static inline void au_sbilist_add(struct super_block *sb)
31078 +{
31079 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
31080 +}
31081 +
31082 +static inline void au_sbilist_del(struct super_block *sb)
31083 +{
31084 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
31085 +}
31086 +
31087 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31088 +static inline void au_sbilist_lock(void)
31089 +{
31090 +       hlist_bl_lock(&au_sbilist);
31091 +}
31092 +
31093 +static inline void au_sbilist_unlock(void)
31094 +{
31095 +       hlist_bl_unlock(&au_sbilist);
31096 +}
31097 +#define AuGFP_SBILIST  GFP_ATOMIC
31098 +#else
31099 +AuStubVoid(au_sbilist_lock, void)
31100 +AuStubVoid(au_sbilist_unlock, void)
31101 +#define AuGFP_SBILIST  GFP_NOFS
31102 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
31103 +#else
31104 +AuStubVoid(au_sbilist_init, void)
31105 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31106 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31107 +AuStubVoid(au_sbilist_lock, void)
31108 +AuStubVoid(au_sbilist_unlock, void)
31109 +#define AuGFP_SBILIST  GFP_NOFS
31110 +#endif
31111 +
31112 +/* ---------------------------------------------------------------------- */
31113 +
31114 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31115 +{
31116 +       /*
31117 +        * This function is a dynamic '__init' function actually,
31118 +        * so the tiny check for si_rwsem is unnecessary.
31119 +        */
31120 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31121 +#ifdef CONFIG_DEBUG_FS
31122 +       sbinfo->si_dbgaufs = NULL;
31123 +       sbinfo->si_dbgaufs_plink = NULL;
31124 +       sbinfo->si_dbgaufs_xib = NULL;
31125 +#ifdef CONFIG_AUFS_EXPORT
31126 +       sbinfo->si_dbgaufs_xigen = NULL;
31127 +#endif
31128 +#endif
31129 +}
31130 +
31131 +/* ---------------------------------------------------------------------- */
31132 +
31133 +/* current->atomic_flags */
31134 +/* this value should never corrupt the ones defined in linux/sched.h */
31135 +#define PFA_AUFS       0x10
31136 +
31137 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31138 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31139 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31140 +
31141 +static inline int si_pid_test(struct super_block *sb)
31142 +{
31143 +       return !!task_test_aufs(current);
31144 +}
31145 +
31146 +static inline void si_pid_clr(struct super_block *sb)
31147 +{
31148 +       AuDebugOn(!task_test_aufs(current));
31149 +       task_clear_aufs(current);
31150 +}
31151 +
31152 +static inline void si_pid_set(struct super_block *sb)
31153 +{
31154 +       AuDebugOn(task_test_aufs(current));
31155 +       task_set_aufs(current);
31156 +}
31157 +
31158 +/* ---------------------------------------------------------------------- */
31159 +
31160 +/* lock superblock. mainly for entry point functions */
31161 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31162 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31163 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31164 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31165 +/*
31166 +#define __si_read_trylock_nested(sb) \
31167 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31168 +#define __si_write_trylock_nested(sb) \
31169 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31170 +*/
31171 +
31172 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31173 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31174 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31175 +
31176 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31177 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31178 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31179 +
31180 +static inline void si_noflush_read_lock(struct super_block *sb)
31181 +{
31182 +       __si_read_lock(sb);
31183 +       si_pid_set(sb);
31184 +}
31185 +
31186 +static inline int si_noflush_read_trylock(struct super_block *sb)
31187 +{
31188 +       int locked;
31189 +
31190 +       locked = __si_read_trylock(sb);
31191 +       if (locked)
31192 +               si_pid_set(sb);
31193 +       return locked;
31194 +}
31195 +
31196 +static inline void si_noflush_write_lock(struct super_block *sb)
31197 +{
31198 +       __si_write_lock(sb);
31199 +       si_pid_set(sb);
31200 +}
31201 +
31202 +static inline int si_noflush_write_trylock(struct super_block *sb)
31203 +{
31204 +       int locked;
31205 +
31206 +       locked = __si_write_trylock(sb);
31207 +       if (locked)
31208 +               si_pid_set(sb);
31209 +       return locked;
31210 +}
31211 +
31212 +#if 0 /* reserved */
31213 +static inline int si_read_trylock(struct super_block *sb, int flags)
31214 +{
31215 +       if (au_ftest_lock(flags, FLUSH))
31216 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31217 +       return si_noflush_read_trylock(sb);
31218 +}
31219 +#endif
31220 +
31221 +static inline void si_read_unlock(struct super_block *sb)
31222 +{
31223 +       si_pid_clr(sb);
31224 +       __si_read_unlock(sb);
31225 +}
31226 +
31227 +#if 0 /* reserved */
31228 +static inline int si_write_trylock(struct super_block *sb, int flags)
31229 +{
31230 +       if (au_ftest_lock(flags, FLUSH))
31231 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31232 +       return si_noflush_write_trylock(sb);
31233 +}
31234 +#endif
31235 +
31236 +static inline void si_write_unlock(struct super_block *sb)
31237 +{
31238 +       si_pid_clr(sb);
31239 +       __si_write_unlock(sb);
31240 +}
31241 +
31242 +#if 0 /* reserved */
31243 +static inline void si_downgrade_lock(struct super_block *sb)
31244 +{
31245 +       __si_downgrade_lock(sb);
31246 +}
31247 +#endif
31248 +
31249 +/* ---------------------------------------------------------------------- */
31250 +
31251 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31252 +{
31253 +       SiMustAnyLock(sb);
31254 +       return au_sbi(sb)->si_bbot;
31255 +}
31256 +
31257 +static inline unsigned int au_mntflags(struct super_block *sb)
31258 +{
31259 +       SiMustAnyLock(sb);
31260 +       return au_sbi(sb)->si_mntflags;
31261 +}
31262 +
31263 +static inline unsigned int au_sigen(struct super_block *sb)
31264 +{
31265 +       SiMustAnyLock(sb);
31266 +       return au_sbi(sb)->si_generation;
31267 +}
31268 +
31269 +static inline struct au_branch *au_sbr(struct super_block *sb,
31270 +                                      aufs_bindex_t bindex)
31271 +{
31272 +       SiMustAnyLock(sb);
31273 +       return au_sbi(sb)->si_branch[0 + bindex];
31274 +}
31275 +
31276 +static inline loff_t au_xi_maxent(struct super_block *sb)
31277 +{
31278 +       SiMustAnyLock(sb);
31279 +       return au_sbi(sb)->si_ximaxent;
31280 +}
31281 +
31282 +#endif /* __KERNEL__ */
31283 +#endif /* __AUFS_SUPER_H__ */
31284 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31285 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31286 +++ linux/fs/aufs/sysaufs.c     2022-08-02 12:12:45.168975258 +0200
31287 @@ -0,0 +1,94 @@
31288 +// SPDX-License-Identifier: GPL-2.0
31289 +/*
31290 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31291 + *
31292 + * This program, aufs is free software; you can redistribute it and/or modify
31293 + * it under the terms of the GNU General Public License as published by
31294 + * the Free Software Foundation; either version 2 of the License, or
31295 + * (at your option) any later version.
31296 + *
31297 + * This program is distributed in the hope that it will be useful,
31298 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31299 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31300 + * GNU General Public License for more details.
31301 + *
31302 + * You should have received a copy of the GNU General Public License
31303 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31304 + */
31305 +
31306 +/*
31307 + * sysfs interface and lifetime management
31308 + * they are necessary regardless sysfs is disabled.
31309 + */
31310 +
31311 +#include <linux/random.h>
31312 +#include "aufs.h"
31313 +
31314 +unsigned long sysaufs_si_mask;
31315 +struct kset *sysaufs_kset;
31316 +
31317 +#define AuSiAttr(_name) { \
31318 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31319 +       .show   = sysaufs_si_##_name,                           \
31320 +}
31321 +
31322 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31323 +struct attribute *sysaufs_si_attrs[] = {
31324 +       &sysaufs_si_attr_xi_path.attr,
31325 +       NULL,
31326 +};
31327 +ATTRIBUTE_GROUPS(sysaufs_si);
31328 +
31329 +static const struct sysfs_ops au_sbi_ops = {
31330 +       .show   = sysaufs_si_show
31331 +};
31332 +
31333 +static struct kobj_type au_sbi_ktype = {
31334 +       .release        = au_si_free,
31335 +       .sysfs_ops      = &au_sbi_ops,
31336 +       .default_groups = sysaufs_si_groups
31337 +};
31338 +
31339 +/* ---------------------------------------------------------------------- */
31340 +
31341 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31342 +{
31343 +       int err;
31344 +
31345 +       sbinfo->si_kobj.kset = sysaufs_kset;
31346 +       /* cf. sysaufs_name() */
31347 +       err = kobject_init_and_add
31348 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31349 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31350 +
31351 +       return err;
31352 +}
31353 +
31354 +void sysaufs_fin(void)
31355 +{
31356 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31357 +       kset_unregister(sysaufs_kset);
31358 +}
31359 +
31360 +int __init sysaufs_init(void)
31361 +{
31362 +       int err;
31363 +
31364 +       do {
31365 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31366 +       } while (!sysaufs_si_mask);
31367 +
31368 +       err = -EINVAL;
31369 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31370 +       if (unlikely(!sysaufs_kset))
31371 +               goto out;
31372 +       err = PTR_ERR(sysaufs_kset);
31373 +       if (IS_ERR(sysaufs_kset))
31374 +               goto out;
31375 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31376 +       if (unlikely(err))
31377 +               kset_unregister(sysaufs_kset);
31378 +
31379 +out:
31380 +       return err;
31381 +}
31382 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31383 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31384 +++ linux/fs/aufs/sysaufs.h     2022-08-02 12:12:45.168975258 +0200
31385 @@ -0,0 +1,102 @@
31386 +/* SPDX-License-Identifier: GPL-2.0 */
31387 +/*
31388 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31389 + *
31390 + * This program, aufs is free software; you can redistribute it and/or modify
31391 + * it under the terms of the GNU General Public License as published by
31392 + * the Free Software Foundation; either version 2 of the License, or
31393 + * (at your option) any later version.
31394 + *
31395 + * This program is distributed in the hope that it will be useful,
31396 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31397 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31398 + * GNU General Public License for more details.
31399 + *
31400 + * You should have received a copy of the GNU General Public License
31401 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31402 + */
31403 +
31404 +/*
31405 + * sysfs interface and mount lifetime management
31406 + */
31407 +
31408 +#ifndef __SYSAUFS_H__
31409 +#define __SYSAUFS_H__
31410 +
31411 +#ifdef __KERNEL__
31412 +
31413 +#include <linux/sysfs.h>
31414 +#include "module.h"
31415 +
31416 +struct super_block;
31417 +struct au_sbinfo;
31418 +
31419 +struct sysaufs_si_attr {
31420 +       struct attribute attr;
31421 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31422 +};
31423 +
31424 +/* ---------------------------------------------------------------------- */
31425 +
31426 +/* sysaufs.c */
31427 +extern unsigned long sysaufs_si_mask;
31428 +extern struct kset *sysaufs_kset;
31429 +extern struct attribute *sysaufs_si_attrs[];
31430 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31431 +int __init sysaufs_init(void);
31432 +void sysaufs_fin(void);
31433 +
31434 +/* ---------------------------------------------------------------------- */
31435 +
31436 +/* some people doesn't like to show a pointer in kernel */
31437 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31438 +{
31439 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31440 +}
31441 +
31442 +#define SysaufsSiNamePrefix    "si_"
31443 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31444 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31445 +{
31446 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31447 +                sysaufs_si_id(sbinfo));
31448 +}
31449 +
31450 +struct au_branch;
31451 +#ifdef CONFIG_SYSFS
31452 +/* sysfs.c */
31453 +extern struct attribute_group *sysaufs_attr_group;
31454 +
31455 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31456 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31457 +                        char *buf);
31458 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31459 +#ifdef CONFIG_COMPAT
31460 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31461 +#endif
31462 +
31463 +void sysaufs_br_init(struct au_branch *br);
31464 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31465 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31466 +
31467 +#define sysaufs_brs_init()     do {} while (0)
31468 +
31469 +#else
31470 +#define sysaufs_attr_group     NULL
31471 +
31472 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31473 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31474 +       struct attribute *attr, char *buf)
31475 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31476 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31477 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31478 +
31479 +static inline void sysaufs_brs_init(void)
31480 +{
31481 +       sysaufs_brs = 0;
31482 +}
31483 +
31484 +#endif /* CONFIG_SYSFS */
31485 +
31486 +#endif /* __KERNEL__ */
31487 +#endif /* __SYSAUFS_H__ */
31488 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31489 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31490 +++ linux/fs/aufs/sysfs.c       2022-08-02 12:12:45.168975258 +0200
31491 @@ -0,0 +1,374 @@
31492 +// SPDX-License-Identifier: GPL-2.0
31493 +/*
31494 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31495 + *
31496 + * This program, aufs is free software; you can redistribute it and/or modify
31497 + * it under the terms of the GNU General Public License as published by
31498 + * the Free Software Foundation; either version 2 of the License, or
31499 + * (at your option) any later version.
31500 + *
31501 + * This program is distributed in the hope that it will be useful,
31502 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31503 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31504 + * GNU General Public License for more details.
31505 + *
31506 + * You should have received a copy of the GNU General Public License
31507 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31508 + */
31509 +
31510 +/*
31511 + * sysfs interface
31512 + */
31513 +
31514 +#include <linux/compat.h>
31515 +#include <linux/seq_file.h>
31516 +#include "aufs.h"
31517 +
31518 +#ifdef CONFIG_AUFS_FS_MODULE
31519 +/* this entry violates the "one line per file" policy of sysfs */
31520 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31521 +                          char *buf)
31522 +{
31523 +       ssize_t err;
31524 +       static char *conf =
31525 +/* this file is generated at compiling */
31526 +#include "conf.str"
31527 +               ;
31528 +
31529 +       err = snprintf(buf, PAGE_SIZE, conf);
31530 +       if (unlikely(err >= PAGE_SIZE))
31531 +               err = -EFBIG;
31532 +       return err;
31533 +}
31534 +
31535 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31536 +#endif
31537 +
31538 +static struct attribute *au_attr[] = {
31539 +#ifdef CONFIG_AUFS_FS_MODULE
31540 +       &au_config_attr.attr,
31541 +#endif
31542 +       NULL,   /* need to NULL terminate the list of attributes */
31543 +};
31544 +
31545 +static struct attribute_group sysaufs_attr_group_body = {
31546 +       .attrs = au_attr
31547 +};
31548 +
31549 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31550 +
31551 +/* ---------------------------------------------------------------------- */
31552 +
31553 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31554 +{
31555 +       int err;
31556 +
31557 +       SiMustAnyLock(sb);
31558 +
31559 +       err = 0;
31560 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31561 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31562 +               seq_putc(seq, '\n');
31563 +       }
31564 +       return err;
31565 +}
31566 +
31567 +/*
31568 + * the lifetime of branch is independent from the entry under sysfs.
31569 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31570 + * unlinked.
31571 + */
31572 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31573 +                        aufs_bindex_t bindex, int idx)
31574 +{
31575 +       int err;
31576 +       struct path path;
31577 +       struct dentry *root;
31578 +       struct au_branch *br;
31579 +       au_br_perm_str_t perm;
31580 +
31581 +       AuDbg("b%d\n", bindex);
31582 +
31583 +       err = 0;
31584 +       root = sb->s_root;
31585 +       di_read_lock_parent(root, !AuLock_IR);
31586 +       br = au_sbr(sb, bindex);
31587 +
31588 +       switch (idx) {
31589 +       case AuBrSysfs_BR:
31590 +               path.mnt = au_br_mnt(br);
31591 +               path.dentry = au_h_dptr(root, bindex);
31592 +               err = au_seq_path(seq, &path);
31593 +               if (!err) {
31594 +                       au_optstr_br_perm(&perm, br->br_perm);
31595 +                       seq_printf(seq, "=%s\n", perm.a);
31596 +               }
31597 +               break;
31598 +       case AuBrSysfs_BRID:
31599 +               seq_printf(seq, "%d\n", br->br_id);
31600 +               break;
31601 +       }
31602 +       di_read_unlock(root, !AuLock_IR);
31603 +       if (unlikely(err || seq_has_overflowed(seq)))
31604 +               err = -E2BIG;
31605 +
31606 +       return err;
31607 +}
31608 +
31609 +/* ---------------------------------------------------------------------- */
31610 +
31611 +static struct seq_file *au_seq(char *p, ssize_t len)
31612 +{
31613 +       struct seq_file *seq;
31614 +
31615 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31616 +       if (seq) {
31617 +               /* mutex_init(&seq.lock); */
31618 +               seq->buf = p;
31619 +               seq->size = len;
31620 +               return seq; /* success */
31621 +       }
31622 +
31623 +       seq = ERR_PTR(-ENOMEM);
31624 +       return seq;
31625 +}
31626 +
31627 +#define SysaufsBr_PREFIX       "br"
31628 +#define SysaufsBrid_PREFIX     "brid"
31629 +
31630 +/* todo: file size may exceed PAGE_SIZE */
31631 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31632 +                       char *buf)
31633 +{
31634 +       ssize_t err;
31635 +       int idx;
31636 +       long l;
31637 +       aufs_bindex_t bbot;
31638 +       struct au_sbinfo *sbinfo;
31639 +       struct super_block *sb;
31640 +       struct seq_file *seq;
31641 +       char *name;
31642 +       struct attribute **cattr;
31643 +
31644 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31645 +       sb = sbinfo->si_sb;
31646 +
31647 +       /*
31648 +        * prevent a race condition between sysfs and aufs.
31649 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31650 +        * prohibits maintaining the sysfs entries.
31651 +        * hew we acquire read lock after sysfs_get_active_two().
31652 +        * on the other hand, the remount process may maintain the sysfs/aufs
31653 +        * entries after acquiring write lock.
31654 +        * it can cause a deadlock.
31655 +        * simply we gave up processing read here.
31656 +        */
31657 +       err = -EBUSY;
31658 +       if (unlikely(!si_noflush_read_trylock(sb)))
31659 +               goto out;
31660 +
31661 +       seq = au_seq(buf, PAGE_SIZE);
31662 +       err = PTR_ERR(seq);
31663 +       if (IS_ERR(seq))
31664 +               goto out_unlock;
31665 +
31666 +       name = (void *)attr->name;
31667 +       cattr = sysaufs_si_attrs;
31668 +       while (*cattr) {
31669 +               if (!strcmp(name, (*cattr)->name)) {
31670 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31671 +                               ->show(seq, sb);
31672 +                       goto out_seq;
31673 +               }
31674 +               cattr++;
31675 +       }
31676 +
31677 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31678 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31679 +               idx = AuBrSysfs_BRID;
31680 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31681 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31682 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31683 +               idx = AuBrSysfs_BR;
31684 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31685 +       } else
31686 +                 BUG();
31687 +
31688 +       err = kstrtol(name, 10, &l);
31689 +       if (!err) {
31690 +               bbot = au_sbbot(sb);
31691 +               if (l <= bbot)
31692 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31693 +               else
31694 +                       err = -ENOENT;
31695 +       }
31696 +
31697 +out_seq:
31698 +       if (!err) {
31699 +               err = seq->count;
31700 +               /* sysfs limit */
31701 +               if (unlikely(err == PAGE_SIZE))
31702 +                       err = -EFBIG;
31703 +       }
31704 +       au_kfree_rcu(seq);
31705 +out_unlock:
31706 +       si_read_unlock(sb);
31707 +out:
31708 +       return err;
31709 +}
31710 +
31711 +/* ---------------------------------------------------------------------- */
31712 +
31713 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31714 +{
31715 +       int err;
31716 +       int16_t brid;
31717 +       aufs_bindex_t bindex, bbot;
31718 +       size_t sz;
31719 +       char *buf;
31720 +       struct seq_file *seq;
31721 +       struct au_branch *br;
31722 +
31723 +       si_read_lock(sb, AuLock_FLUSH);
31724 +       bbot = au_sbbot(sb);
31725 +       err = bbot + 1;
31726 +       if (!arg)
31727 +               goto out;
31728 +
31729 +       err = -ENOMEM;
31730 +       buf = (void *)__get_free_page(GFP_NOFS);
31731 +       if (unlikely(!buf))
31732 +               goto out;
31733 +
31734 +       seq = au_seq(buf, PAGE_SIZE);
31735 +       err = PTR_ERR(seq);
31736 +       if (IS_ERR(seq))
31737 +               goto out_buf;
31738 +
31739 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31740 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31741 +               /* VERIFY_WRITE */
31742 +               err = !access_ok(arg, sizeof(*arg));
31743 +               if (unlikely(err))
31744 +                       break;
31745 +
31746 +               br = au_sbr(sb, bindex);
31747 +               brid = br->br_id;
31748 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31749 +               err = __put_user(brid, &arg->id);
31750 +               if (unlikely(err))
31751 +                       break;
31752 +
31753 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31754 +               err = __put_user(br->br_perm, &arg->perm);
31755 +               if (unlikely(err))
31756 +                       break;
31757 +
31758 +               err = au_seq_path(seq, &br->br_path);
31759 +               if (unlikely(err))
31760 +                       break;
31761 +               seq_putc(seq, '\0');
31762 +               if (!seq_has_overflowed(seq)) {
31763 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31764 +                       seq->count = 0;
31765 +                       if (unlikely(err))
31766 +                               break;
31767 +               } else {
31768 +                       err = -E2BIG;
31769 +                       goto out_seq;
31770 +               }
31771 +       }
31772 +       if (unlikely(err))
31773 +               err = -EFAULT;
31774 +
31775 +out_seq:
31776 +       au_kfree_rcu(seq);
31777 +out_buf:
31778 +       free_page((unsigned long)buf);
31779 +out:
31780 +       si_read_unlock(sb);
31781 +       return err;
31782 +}
31783 +
31784 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31785 +{
31786 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31787 +}
31788 +
31789 +#ifdef CONFIG_COMPAT
31790 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31791 +{
31792 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31793 +}
31794 +#endif
31795 +
31796 +/* ---------------------------------------------------------------------- */
31797 +
31798 +void sysaufs_br_init(struct au_branch *br)
31799 +{
31800 +       int i;
31801 +       struct au_brsysfs *br_sysfs;
31802 +       struct attribute *attr;
31803 +
31804 +       br_sysfs = br->br_sysfs;
31805 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31806 +               attr = &br_sysfs->attr;
31807 +               sysfs_attr_init(attr);
31808 +               attr->name = br_sysfs->name;
31809 +               attr->mode = 0444;
31810 +               br_sysfs++;
31811 +       }
31812 +}
31813 +
31814 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31815 +{
31816 +       struct au_branch *br;
31817 +       struct kobject *kobj;
31818 +       struct au_brsysfs *br_sysfs;
31819 +       int i;
31820 +       aufs_bindex_t bbot;
31821 +
31822 +       if (!sysaufs_brs)
31823 +               return;
31824 +
31825 +       kobj = &au_sbi(sb)->si_kobj;
31826 +       bbot = au_sbbot(sb);
31827 +       for (; bindex <= bbot; bindex++) {
31828 +               br = au_sbr(sb, bindex);
31829 +               br_sysfs = br->br_sysfs;
31830 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31831 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31832 +                       br_sysfs++;
31833 +               }
31834 +       }
31835 +}
31836 +
31837 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31838 +{
31839 +       int err, i;
31840 +       aufs_bindex_t bbot;
31841 +       struct kobject *kobj;
31842 +       struct au_branch *br;
31843 +       struct au_brsysfs *br_sysfs;
31844 +
31845 +       if (!sysaufs_brs)
31846 +               return;
31847 +
31848 +       kobj = &au_sbi(sb)->si_kobj;
31849 +       bbot = au_sbbot(sb);
31850 +       for (; bindex <= bbot; bindex++) {
31851 +               br = au_sbr(sb, bindex);
31852 +               br_sysfs = br->br_sysfs;
31853 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31854 +                        SysaufsBr_PREFIX "%d", bindex);
31855 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31856 +                        SysaufsBrid_PREFIX "%d", bindex);
31857 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31858 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31859 +                       if (unlikely(err))
31860 +                               pr_warn("failed %s under sysfs(%d)\n",
31861 +                                       br_sysfs->name, err);
31862 +                       br_sysfs++;
31863 +               }
31864 +       }
31865 +}
31866 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31867 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31868 +++ linux/fs/aufs/sysrq.c       2022-08-02 12:12:45.168975258 +0200
31869 @@ -0,0 +1,149 @@
31870 +// SPDX-License-Identifier: GPL-2.0
31871 +/*
31872 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31873 + *
31874 + * This program, aufs is free software; you can redistribute it and/or modify
31875 + * it under the terms of the GNU General Public License as published by
31876 + * the Free Software Foundation; either version 2 of the License, or
31877 + * (at your option) any later version.
31878 + *
31879 + * This program is distributed in the hope that it will be useful,
31880 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31881 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31882 + * GNU General Public License for more details.
31883 + *
31884 + * You should have received a copy of the GNU General Public License
31885 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31886 + */
31887 +
31888 +/*
31889 + * magic sysrq handler
31890 + */
31891 +
31892 +/* #include <linux/sysrq.h> */
31893 +#include <linux/writeback.h>
31894 +#include "aufs.h"
31895 +
31896 +/* ---------------------------------------------------------------------- */
31897 +
31898 +static void sysrq_sb(struct super_block *sb)
31899 +{
31900 +       char *plevel;
31901 +       struct au_sbinfo *sbinfo;
31902 +       struct file *file;
31903 +       struct hlist_bl_head *files;
31904 +       struct hlist_bl_node *pos;
31905 +       struct au_finfo *finfo;
31906 +       struct inode *i;
31907 +
31908 +       plevel = au_plevel;
31909 +       au_plevel = KERN_WARNING;
31910 +
31911 +       /* since we define pr_fmt, call printk directly */
31912 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31913 +
31914 +       sbinfo = au_sbi(sb);
31915 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31916 +       pr("superblock\n");
31917 +       au_dpri_sb(sb);
31918 +
31919 +#if 0 /* reserved */
31920 +       do {
31921 +               int err, i, j, ndentry;
31922 +               struct au_dcsub_pages dpages;
31923 +               struct au_dpage *dpage;
31924 +
31925 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31926 +               if (unlikely(err))
31927 +                       break;
31928 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31929 +               if (!err)
31930 +                       for (i = 0; i < dpages.ndpage; i++) {
31931 +                               dpage = dpages.dpages + i;
31932 +                               ndentry = dpage->ndentry;
31933 +                               for (j = 0; j < ndentry; j++)
31934 +                                       au_dpri_dentry(dpage->dentries[j]);
31935 +                       }
31936 +               au_dpages_free(&dpages);
31937 +       } while (0);
31938 +#endif
31939 +
31940 +       pr("isolated inode\n");
31941 +       spin_lock(&sb->s_inode_list_lock);
31942 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31943 +               spin_lock(&i->i_lock);
31944 +               if (hlist_empty(&i->i_dentry))
31945 +                       au_dpri_inode(i);
31946 +               spin_unlock(&i->i_lock);
31947 +       }
31948 +       spin_unlock(&sb->s_inode_list_lock);
31949 +
31950 +       pr("files\n");
31951 +       files = &au_sbi(sb)->si_files;
31952 +       hlist_bl_lock(files);
31953 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31954 +               umode_t mode;
31955 +
31956 +               file = finfo->fi_file;
31957 +               mode = file_inode(file)->i_mode;
31958 +               if (!special_file(mode))
31959 +                       au_dpri_file(file);
31960 +       }
31961 +       hlist_bl_unlock(files);
31962 +       pr("done\n");
31963 +
31964 +#undef pr
31965 +       au_plevel = plevel;
31966 +}
31967 +
31968 +/* ---------------------------------------------------------------------- */
31969 +
31970 +/* module parameter */
31971 +static char *aufs_sysrq_key = "a";
31972 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31973 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31974 +
31975 +static void au_sysrq(int key __maybe_unused)
31976 +{
31977 +       struct au_sbinfo *sbinfo;
31978 +       struct hlist_bl_node *pos;
31979 +
31980 +       lockdep_off();
31981 +       au_sbilist_lock();
31982 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31983 +               sysrq_sb(sbinfo->si_sb);
31984 +       au_sbilist_unlock();
31985 +       lockdep_on();
31986 +}
31987 +
31988 +static struct sysrq_key_op au_sysrq_op = {
31989 +       .handler        = au_sysrq,
31990 +       .help_msg       = "Aufs",
31991 +       .action_msg     = "Aufs",
31992 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31993 +};
31994 +
31995 +/* ---------------------------------------------------------------------- */
31996 +
31997 +int __init au_sysrq_init(void)
31998 +{
31999 +       int err;
32000 +       char key;
32001 +
32002 +       err = -1;
32003 +       key = *aufs_sysrq_key;
32004 +       if ('a' <= key && key <= 'z')
32005 +               err = register_sysrq_key(key, &au_sysrq_op);
32006 +       if (unlikely(err))
32007 +               pr_err("err %d, sysrq=%c\n", err, key);
32008 +       return err;
32009 +}
32010 +
32011 +void au_sysrq_fin(void)
32012 +{
32013 +       int err;
32014 +
32015 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
32016 +       if (unlikely(err))
32017 +               pr_err("err %d (ignored)\n", err);
32018 +}
32019 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
32020 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
32021 +++ linux/fs/aufs/vdir.c        2022-08-02 12:12:45.168975258 +0200
32022 @@ -0,0 +1,896 @@
32023 +// SPDX-License-Identifier: GPL-2.0
32024 +/*
32025 + * Copyright (C) 2005-2021 Junjiro R. Okajima
32026 + *
32027 + * This program, aufs is free software; you can redistribute it and/or modify
32028 + * it under the terms of the GNU General Public License as published by
32029 + * the Free Software Foundation; either version 2 of the License, or
32030 + * (at your option) any later version.
32031 + *
32032 + * This program is distributed in the hope that it will be useful,
32033 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32034 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32035 + * GNU General Public License for more details.
32036 + *
32037 + * You should have received a copy of the GNU General Public License
32038 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32039 + */
32040 +
32041 +/*
32042 + * virtual or vertical directory
32043 + */
32044 +
32045 +#include <linux/iversion.h>
32046 +#include "aufs.h"
32047 +
32048 +static unsigned int calc_size(int nlen)
32049 +{
32050 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
32051 +}
32052 +
32053 +static int set_deblk_end(union au_vdir_deblk_p *p,
32054 +                        union au_vdir_deblk_p *deblk_end)
32055 +{
32056 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32057 +               p->de->de_str.len = 0;
32058 +               /* smp_mb(); */
32059 +               return 0;
32060 +       }
32061 +       return -1; /* error */
32062 +}
32063 +
32064 +/* returns true or false */
32065 +static int is_deblk_end(union au_vdir_deblk_p *p,
32066 +                       union au_vdir_deblk_p *deblk_end)
32067 +{
32068 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
32069 +               return !p->de->de_str.len;
32070 +       return 1;
32071 +}
32072 +
32073 +static unsigned char *last_deblk(struct au_vdir *vdir)
32074 +{
32075 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
32076 +}
32077 +
32078 +/* ---------------------------------------------------------------------- */
32079 +
32080 +/* estimate the appropriate size for name hash table */
32081 +unsigned int au_rdhash_est(loff_t sz)
32082 +{
32083 +       unsigned int n;
32084 +
32085 +       n = UINT_MAX;
32086 +       sz >>= 10;
32087 +       if (sz < n)
32088 +               n = sz;
32089 +       if (sz < AUFS_RDHASH_DEF)
32090 +               n = AUFS_RDHASH_DEF;
32091 +       /* pr_info("n %u\n", n); */
32092 +       return n;
32093 +}
32094 +
32095 +/*
32096 + * the allocated memory has to be freed by
32097 + * au_nhash_wh_free() or au_nhash_de_free().
32098 + */
32099 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32100 +{
32101 +       struct hlist_head *head;
32102 +       unsigned int u;
32103 +       size_t sz;
32104 +
32105 +       sz = sizeof(*nhash->nh_head) * num_hash;
32106 +       head = kmalloc(sz, gfp);
32107 +       if (head) {
32108 +               nhash->nh_num = num_hash;
32109 +               nhash->nh_head = head;
32110 +               for (u = 0; u < num_hash; u++)
32111 +                       INIT_HLIST_HEAD(head++);
32112 +               return 0; /* success */
32113 +       }
32114 +
32115 +       return -ENOMEM;
32116 +}
32117 +
32118 +static void nhash_count(struct hlist_head *head)
32119 +{
32120 +#if 0 /* debugging */
32121 +       unsigned long n;
32122 +       struct hlist_node *pos;
32123 +
32124 +       n = 0;
32125 +       hlist_for_each(pos, head)
32126 +               n++;
32127 +       pr_info("%lu\n", n);
32128 +#endif
32129 +}
32130 +
32131 +static void au_nhash_wh_do_free(struct hlist_head *head)
32132 +{
32133 +       struct au_vdir_wh *pos;
32134 +       struct hlist_node *node;
32135 +
32136 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32137 +               au_kfree_rcu(pos);
32138 +}
32139 +
32140 +static void au_nhash_de_do_free(struct hlist_head *head)
32141 +{
32142 +       struct au_vdir_dehstr *pos;
32143 +       struct hlist_node *node;
32144 +
32145 +       hlist_for_each_entry_safe(pos, node, head, hash)
32146 +               au_cache_free_vdir_dehstr(pos);
32147 +}
32148 +
32149 +static void au_nhash_do_free(struct au_nhash *nhash,
32150 +                            void (*free)(struct hlist_head *head))
32151 +{
32152 +       unsigned int n;
32153 +       struct hlist_head *head;
32154 +
32155 +       n = nhash->nh_num;
32156 +       if (!n)
32157 +               return;
32158 +
32159 +       head = nhash->nh_head;
32160 +       while (n-- > 0) {
32161 +               nhash_count(head);
32162 +               free(head++);
32163 +       }
32164 +       au_kfree_try_rcu(nhash->nh_head);
32165 +}
32166 +
32167 +void au_nhash_wh_free(struct au_nhash *whlist)
32168 +{
32169 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32170 +}
32171 +
32172 +static void au_nhash_de_free(struct au_nhash *delist)
32173 +{
32174 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32175 +}
32176 +
32177 +/* ---------------------------------------------------------------------- */
32178 +
32179 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32180 +                           int limit)
32181 +{
32182 +       int num;
32183 +       unsigned int u, n;
32184 +       struct hlist_head *head;
32185 +       struct au_vdir_wh *pos;
32186 +
32187 +       num = 0;
32188 +       n = whlist->nh_num;
32189 +       head = whlist->nh_head;
32190 +       for (u = 0; u < n; u++, head++)
32191 +               hlist_for_each_entry(pos, head, wh_hash)
32192 +                       if (pos->wh_bindex == btgt && ++num > limit)
32193 +                               return 1;
32194 +       return 0;
32195 +}
32196 +
32197 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32198 +                                      unsigned char *name,
32199 +                                      unsigned int len)
32200 +{
32201 +       unsigned int v;
32202 +       /* const unsigned int magic_bit = 12; */
32203 +
32204 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32205 +
32206 +       v = 0;
32207 +       if (len > 8)
32208 +               len = 8;
32209 +       while (len--)
32210 +               v += *name++;
32211 +       /* v = hash_long(v, magic_bit); */
32212 +       v %= nhash->nh_num;
32213 +       return nhash->nh_head + v;
32214 +}
32215 +
32216 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32217 +                             int nlen)
32218 +{
32219 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32220 +}
32221 +
32222 +/* returns found or not */
32223 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32224 +{
32225 +       struct hlist_head *head;
32226 +       struct au_vdir_wh *pos;
32227 +       struct au_vdir_destr *str;
32228 +
32229 +       head = au_name_hash(whlist, name, nlen);
32230 +       hlist_for_each_entry(pos, head, wh_hash) {
32231 +               str = &pos->wh_str;
32232 +               AuDbg("%.*s\n", str->len, str->name);
32233 +               if (au_nhash_test_name(str, name, nlen))
32234 +                       return 1;
32235 +       }
32236 +       return 0;
32237 +}
32238 +
32239 +/* returns found(true) or not */
32240 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32241 +{
32242 +       struct hlist_head *head;
32243 +       struct au_vdir_dehstr *pos;
32244 +       struct au_vdir_destr *str;
32245 +
32246 +       head = au_name_hash(delist, name, nlen);
32247 +       hlist_for_each_entry(pos, head, hash) {
32248 +               str = pos->str;
32249 +               AuDbg("%.*s\n", str->len, str->name);
32250 +               if (au_nhash_test_name(str, name, nlen))
32251 +                       return 1;
32252 +       }
32253 +       return 0;
32254 +}
32255 +
32256 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32257 +                           unsigned char d_type)
32258 +{
32259 +#ifdef CONFIG_AUFS_SHWH
32260 +       wh->wh_ino = ino;
32261 +       wh->wh_type = d_type;
32262 +#endif
32263 +}
32264 +
32265 +/* ---------------------------------------------------------------------- */
32266 +
32267 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32268 +                      unsigned int d_type, aufs_bindex_t bindex,
32269 +                      unsigned char shwh)
32270 +{
32271 +       int err;
32272 +       struct au_vdir_destr *str;
32273 +       struct au_vdir_wh *wh;
32274 +
32275 +       AuDbg("%.*s\n", nlen, name);
32276 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32277 +
32278 +       err = -ENOMEM;
32279 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32280 +       if (unlikely(!wh))
32281 +               goto out;
32282 +
32283 +       err = 0;
32284 +       wh->wh_bindex = bindex;
32285 +       if (shwh)
32286 +               au_shwh_init_wh(wh, ino, d_type);
32287 +       str = &wh->wh_str;
32288 +       str->len = nlen;
32289 +       memcpy(str->name, name, nlen);
32290 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32291 +       /* smp_mb(); */
32292 +
32293 +out:
32294 +       return err;
32295 +}
32296 +
32297 +static int append_deblk(struct au_vdir *vdir)
32298 +{
32299 +       int err;
32300 +       unsigned long ul;
32301 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32302 +       union au_vdir_deblk_p p, deblk_end;
32303 +       unsigned char **o;
32304 +
32305 +       err = -ENOMEM;
32306 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32307 +                       GFP_NOFS, /*may_shrink*/0);
32308 +       if (unlikely(!o))
32309 +               goto out;
32310 +
32311 +       vdir->vd_deblk = o;
32312 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32313 +       if (p.deblk) {
32314 +               ul = vdir->vd_nblk++;
32315 +               vdir->vd_deblk[ul] = p.deblk;
32316 +               vdir->vd_last.ul = ul;
32317 +               vdir->vd_last.p.deblk = p.deblk;
32318 +               deblk_end.deblk = p.deblk + deblk_sz;
32319 +               err = set_deblk_end(&p, &deblk_end);
32320 +       }
32321 +
32322 +out:
32323 +       return err;
32324 +}
32325 +
32326 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32327 +                    unsigned int d_type, struct au_nhash *delist)
32328 +{
32329 +       int err;
32330 +       unsigned int sz;
32331 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32332 +       union au_vdir_deblk_p p, *room, deblk_end;
32333 +       struct au_vdir_dehstr *dehstr;
32334 +
32335 +       p.deblk = last_deblk(vdir);
32336 +       deblk_end.deblk = p.deblk + deblk_sz;
32337 +       room = &vdir->vd_last.p;
32338 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32339 +                 || !is_deblk_end(room, &deblk_end));
32340 +
32341 +       sz = calc_size(nlen);
32342 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32343 +               err = append_deblk(vdir);
32344 +               if (unlikely(err))
32345 +                       goto out;
32346 +
32347 +               p.deblk = last_deblk(vdir);
32348 +               deblk_end.deblk = p.deblk + deblk_sz;
32349 +               /* smp_mb(); */
32350 +               AuDebugOn(room->deblk != p.deblk);
32351 +       }
32352 +
32353 +       err = -ENOMEM;
32354 +       dehstr = au_cache_alloc_vdir_dehstr();
32355 +       if (unlikely(!dehstr))
32356 +               goto out;
32357 +
32358 +       dehstr->str = &room->de->de_str;
32359 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32360 +       room->de->de_ino = ino;
32361 +       room->de->de_type = d_type;
32362 +       room->de->de_str.len = nlen;
32363 +       memcpy(room->de->de_str.name, name, nlen);
32364 +
32365 +       err = 0;
32366 +       room->deblk += sz;
32367 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32368 +               err = append_deblk(vdir);
32369 +       /* smp_mb(); */
32370 +
32371 +out:
32372 +       return err;
32373 +}
32374 +
32375 +/* ---------------------------------------------------------------------- */
32376 +
32377 +void au_vdir_free(struct au_vdir *vdir)
32378 +{
32379 +       unsigned char **deblk;
32380 +
32381 +       deblk = vdir->vd_deblk;
32382 +       while (vdir->vd_nblk--)
32383 +               au_kfree_try_rcu(*deblk++);
32384 +       au_kfree_try_rcu(vdir->vd_deblk);
32385 +       au_cache_free_vdir(vdir);
32386 +}
32387 +
32388 +static struct au_vdir *alloc_vdir(struct file *file)
32389 +{
32390 +       struct au_vdir *vdir;
32391 +       struct super_block *sb;
32392 +       int err;
32393 +
32394 +       sb = file->f_path.dentry->d_sb;
32395 +       SiMustAnyLock(sb);
32396 +
32397 +       err = -ENOMEM;
32398 +       vdir = au_cache_alloc_vdir();
32399 +       if (unlikely(!vdir))
32400 +               goto out;
32401 +
32402 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32403 +       if (unlikely(!vdir->vd_deblk))
32404 +               goto out_free;
32405 +
32406 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32407 +       if (!vdir->vd_deblk_sz) {
32408 +               /* estimate the appropriate size for deblk */
32409 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32410 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32411 +       }
32412 +       vdir->vd_nblk = 0;
32413 +       vdir->vd_version = 0;
32414 +       vdir->vd_jiffy = 0;
32415 +       err = append_deblk(vdir);
32416 +       if (!err)
32417 +               return vdir; /* success */
32418 +
32419 +       au_kfree_try_rcu(vdir->vd_deblk);
32420 +
32421 +out_free:
32422 +       au_cache_free_vdir(vdir);
32423 +out:
32424 +       vdir = ERR_PTR(err);
32425 +       return vdir;
32426 +}
32427 +
32428 +static int reinit_vdir(struct au_vdir *vdir)
32429 +{
32430 +       int err;
32431 +       union au_vdir_deblk_p p, deblk_end;
32432 +
32433 +       while (vdir->vd_nblk > 1) {
32434 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32435 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32436 +               vdir->vd_nblk--;
32437 +       }
32438 +       p.deblk = vdir->vd_deblk[0];
32439 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32440 +       err = set_deblk_end(&p, &deblk_end);
32441 +       /* keep vd_dblk_sz */
32442 +       vdir->vd_last.ul = 0;
32443 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32444 +       vdir->vd_version = 0;
32445 +       vdir->vd_jiffy = 0;
32446 +       /* smp_mb(); */
32447 +       return err;
32448 +}
32449 +
32450 +/* ---------------------------------------------------------------------- */
32451 +
32452 +#define AuFillVdir_CALLED      1
32453 +#define AuFillVdir_WHABLE      (1 << 1)
32454 +#define AuFillVdir_SHWH                (1 << 2)
32455 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32456 +#define au_fset_fillvdir(flags, name) \
32457 +       do { (flags) |= AuFillVdir_##name; } while (0)
32458 +#define au_fclr_fillvdir(flags, name) \
32459 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32460 +
32461 +#ifndef CONFIG_AUFS_SHWH
32462 +#undef AuFillVdir_SHWH
32463 +#define AuFillVdir_SHWH                0
32464 +#endif
32465 +
32466 +struct fillvdir_arg {
32467 +       struct dir_context      ctx;
32468 +       struct file             *file;
32469 +       struct au_vdir          *vdir;
32470 +       struct au_nhash         delist;
32471 +       struct au_nhash         whlist;
32472 +       aufs_bindex_t           bindex;
32473 +       unsigned int            flags;
32474 +       int                     err;
32475 +};
32476 +
32477 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32478 +                   loff_t offset __maybe_unused, u64 h_ino,
32479 +                   unsigned int d_type)
32480 +{
32481 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32482 +       char *name = (void *)__name;
32483 +       struct super_block *sb;
32484 +       ino_t ino;
32485 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32486 +
32487 +       arg->err = 0;
32488 +       sb = arg->file->f_path.dentry->d_sb;
32489 +       au_fset_fillvdir(arg->flags, CALLED);
32490 +       /* smp_mb(); */
32491 +       if (nlen <= AUFS_WH_PFX_LEN
32492 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32493 +               if (test_known(&arg->delist, name, nlen)
32494 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32495 +                       goto out; /* already exists or whiteouted */
32496 +
32497 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32498 +               if (!arg->err) {
32499 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32500 +                               d_type = DT_UNKNOWN;
32501 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32502 +                                            d_type, &arg->delist);
32503 +               }
32504 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32505 +               name += AUFS_WH_PFX_LEN;
32506 +               nlen -= AUFS_WH_PFX_LEN;
32507 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32508 +                       goto out; /* already whiteouted */
32509 +
32510 +               ino = 0; /* just to suppress a warning */
32511 +               if (shwh)
32512 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32513 +                                            &ino);
32514 +               if (!arg->err) {
32515 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32516 +                               d_type = DT_UNKNOWN;
32517 +                       arg->err = au_nhash_append_wh
32518 +                               (&arg->whlist, name, nlen, ino, d_type,
32519 +                                arg->bindex, shwh);
32520 +               }
32521 +       }
32522 +
32523 +out:
32524 +       if (!arg->err)
32525 +               arg->vdir->vd_jiffy = jiffies;
32526 +       /* smp_mb(); */
32527 +       AuTraceErr(arg->err);
32528 +       return arg->err;
32529 +}
32530 +
32531 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32532 +                         struct au_nhash *whlist, struct au_nhash *delist)
32533 +{
32534 +#ifdef CONFIG_AUFS_SHWH
32535 +       int err;
32536 +       unsigned int nh, u;
32537 +       struct hlist_head *head;
32538 +       struct au_vdir_wh *pos;
32539 +       struct hlist_node *n;
32540 +       char *p, *o;
32541 +       struct au_vdir_destr *destr;
32542 +
32543 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32544 +
32545 +       err = -ENOMEM;
32546 +       o = p = (void *)__get_free_page(GFP_NOFS);
32547 +       if (unlikely(!p))
32548 +               goto out;
32549 +
32550 +       err = 0;
32551 +       nh = whlist->nh_num;
32552 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32553 +       p += AUFS_WH_PFX_LEN;
32554 +       for (u = 0; u < nh; u++) {
32555 +               head = whlist->nh_head + u;
32556 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32557 +                       destr = &pos->wh_str;
32558 +                       memcpy(p, destr->name, destr->len);
32559 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32560 +                                       pos->wh_ino, pos->wh_type, delist);
32561 +                       if (unlikely(err))
32562 +                               break;
32563 +               }
32564 +       }
32565 +
32566 +       free_page((unsigned long)o);
32567 +
32568 +out:
32569 +       AuTraceErr(err);
32570 +       return err;
32571 +#else
32572 +       return 0;
32573 +#endif
32574 +}
32575 +
32576 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32577 +{
32578 +       int err;
32579 +       unsigned int rdhash;
32580 +       loff_t offset;
32581 +       aufs_bindex_t bbot, bindex, btop;
32582 +       unsigned char shwh;
32583 +       struct file *hf, *file;
32584 +       struct super_block *sb;
32585 +
32586 +       file = arg->file;
32587 +       sb = file->f_path.dentry->d_sb;
32588 +       SiMustAnyLock(sb);
32589 +
32590 +       rdhash = au_sbi(sb)->si_rdhash;
32591 +       if (!rdhash)
32592 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32593 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32594 +       if (unlikely(err))
32595 +               goto out;
32596 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32597 +       if (unlikely(err))
32598 +               goto out_delist;
32599 +
32600 +       err = 0;
32601 +       arg->flags = 0;
32602 +       shwh = 0;
32603 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32604 +               shwh = 1;
32605 +               au_fset_fillvdir(arg->flags, SHWH);
32606 +       }
32607 +       btop = au_fbtop(file);
32608 +       bbot = au_fbbot_dir(file);
32609 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32610 +               hf = au_hf_dir(file, bindex);
32611 +               if (!hf)
32612 +                       continue;
32613 +
32614 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32615 +               err = offset;
32616 +               if (unlikely(offset))
32617 +                       break;
32618 +
32619 +               arg->bindex = bindex;
32620 +               au_fclr_fillvdir(arg->flags, WHABLE);
32621 +               if (shwh
32622 +                   || (bindex != bbot
32623 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32624 +                       au_fset_fillvdir(arg->flags, WHABLE);
32625 +               do {
32626 +                       arg->err = 0;
32627 +                       au_fclr_fillvdir(arg->flags, CALLED);
32628 +                       /* smp_mb(); */
32629 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32630 +                       if (err >= 0)
32631 +                               err = arg->err;
32632 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32633 +
32634 +               /*
32635 +                * dir_relax() may be good for concurrency, but aufs should not
32636 +                * use it since it will cause a lockdep problem.
32637 +                */
32638 +       }
32639 +
32640 +       if (!err && shwh)
32641 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32642 +
32643 +       au_nhash_wh_free(&arg->whlist);
32644 +
32645 +out_delist:
32646 +       au_nhash_de_free(&arg->delist);
32647 +out:
32648 +       return err;
32649 +}
32650 +
32651 +static int read_vdir(struct file *file, int may_read)
32652 +{
32653 +       int err;
32654 +       unsigned long expire;
32655 +       unsigned char do_read;
32656 +       struct fillvdir_arg arg = {
32657 +               .ctx = {
32658 +                       .actor = fillvdir
32659 +               }
32660 +       };
32661 +       struct inode *inode;
32662 +       struct au_vdir *vdir, *allocated;
32663 +
32664 +       err = 0;
32665 +       inode = file_inode(file);
32666 +       IMustLock(inode);
32667 +       IiMustWriteLock(inode);
32668 +       SiMustAnyLock(inode->i_sb);
32669 +
32670 +       allocated = NULL;
32671 +       do_read = 0;
32672 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32673 +       vdir = au_ivdir(inode);
32674 +       if (!vdir) {
32675 +               do_read = 1;
32676 +               vdir = alloc_vdir(file);
32677 +               err = PTR_ERR(vdir);
32678 +               if (IS_ERR(vdir))
32679 +                       goto out;
32680 +               err = 0;
32681 +               allocated = vdir;
32682 +       } else if (may_read
32683 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32684 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32685 +               do_read = 1;
32686 +               err = reinit_vdir(vdir);
32687 +               if (unlikely(err))
32688 +                       goto out;
32689 +       }
32690 +
32691 +       if (!do_read)
32692 +               return 0; /* success */
32693 +
32694 +       arg.file = file;
32695 +       arg.vdir = vdir;
32696 +       err = au_do_read_vdir(&arg);
32697 +       if (!err) {
32698 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32699 +               vdir->vd_version = inode_query_iversion(inode);
32700 +               vdir->vd_last.ul = 0;
32701 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32702 +               if (allocated)
32703 +                       au_set_ivdir(inode, allocated);
32704 +       } else if (allocated)
32705 +               au_vdir_free(allocated);
32706 +
32707 +out:
32708 +       return err;
32709 +}
32710 +
32711 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32712 +{
32713 +       int err, rerr;
32714 +       unsigned long ul, n;
32715 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32716 +
32717 +       AuDebugOn(tgt->vd_nblk != 1);
32718 +
32719 +       err = -ENOMEM;
32720 +       if (tgt->vd_nblk < src->vd_nblk) {
32721 +               unsigned char **p;
32722 +
32723 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32724 +                               GFP_NOFS, /*may_shrink*/0);
32725 +               if (unlikely(!p))
32726 +                       goto out;
32727 +               tgt->vd_deblk = p;
32728 +       }
32729 +
32730 +       if (tgt->vd_deblk_sz != deblk_sz) {
32731 +               unsigned char *p;
32732 +
32733 +               tgt->vd_deblk_sz = deblk_sz;
32734 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32735 +                               /*may_shrink*/1);
32736 +               if (unlikely(!p))
32737 +                       goto out;
32738 +               tgt->vd_deblk[0] = p;
32739 +       }
32740 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32741 +       tgt->vd_version = src->vd_version;
32742 +       tgt->vd_jiffy = src->vd_jiffy;
32743 +
32744 +       n = src->vd_nblk;
32745 +       for (ul = 1; ul < n; ul++) {
32746 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32747 +                                           GFP_NOFS);
32748 +               if (unlikely(!tgt->vd_deblk[ul]))
32749 +                       goto out;
32750 +               tgt->vd_nblk++;
32751 +       }
32752 +       tgt->vd_nblk = n;
32753 +       tgt->vd_last.ul = tgt->vd_last.ul;
32754 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32755 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32756 +               - src->vd_deblk[src->vd_last.ul];
32757 +       /* smp_mb(); */
32758 +       return 0; /* success */
32759 +
32760 +out:
32761 +       rerr = reinit_vdir(tgt);
32762 +       BUG_ON(rerr);
32763 +       return err;
32764 +}
32765 +
32766 +int au_vdir_init(struct file *file)
32767 +{
32768 +       int err;
32769 +       struct inode *inode;
32770 +       struct au_vdir *vdir_cache, *allocated;
32771 +
32772 +       /* test file->f_pos here instead of ctx->pos */
32773 +       err = read_vdir(file, !file->f_pos);
32774 +       if (unlikely(err))
32775 +               goto out;
32776 +
32777 +       allocated = NULL;
32778 +       vdir_cache = au_fvdir_cache(file);
32779 +       if (!vdir_cache) {
32780 +               vdir_cache = alloc_vdir(file);
32781 +               err = PTR_ERR(vdir_cache);
32782 +               if (IS_ERR(vdir_cache))
32783 +                       goto out;
32784 +               allocated = vdir_cache;
32785 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32786 +               /* test file->f_pos here instead of ctx->pos */
32787 +               err = reinit_vdir(vdir_cache);
32788 +               if (unlikely(err))
32789 +                       goto out;
32790 +       } else
32791 +               return 0; /* success */
32792 +
32793 +       inode = file_inode(file);
32794 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32795 +       if (!err) {
32796 +               file->f_version = inode_query_iversion(inode);
32797 +               if (allocated)
32798 +                       au_set_fvdir_cache(file, allocated);
32799 +       } else if (allocated)
32800 +               au_vdir_free(allocated);
32801 +
32802 +out:
32803 +       return err;
32804 +}
32805 +
32806 +static loff_t calc_offset(struct au_vdir *vdir)
32807 +{
32808 +       loff_t offset;
32809 +       union au_vdir_deblk_p p;
32810 +
32811 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32812 +       offset = vdir->vd_last.p.deblk - p.deblk;
32813 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32814 +       return offset;
32815 +}
32816 +
32817 +/* returns true or false */
32818 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32819 +{
32820 +       int valid;
32821 +       unsigned int deblk_sz;
32822 +       unsigned long ul, n;
32823 +       loff_t offset;
32824 +       union au_vdir_deblk_p p, deblk_end;
32825 +       struct au_vdir *vdir_cache;
32826 +
32827 +       valid = 1;
32828 +       vdir_cache = au_fvdir_cache(file);
32829 +       offset = calc_offset(vdir_cache);
32830 +       AuDbg("offset %lld\n", offset);
32831 +       if (ctx->pos == offset)
32832 +               goto out;
32833 +
32834 +       vdir_cache->vd_last.ul = 0;
32835 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32836 +       if (!ctx->pos)
32837 +               goto out;
32838 +
32839 +       valid = 0;
32840 +       deblk_sz = vdir_cache->vd_deblk_sz;
32841 +       ul = div64_u64(ctx->pos, deblk_sz);
32842 +       AuDbg("ul %lu\n", ul);
32843 +       if (ul >= vdir_cache->vd_nblk)
32844 +               goto out;
32845 +
32846 +       n = vdir_cache->vd_nblk;
32847 +       for (; ul < n; ul++) {
32848 +               p.deblk = vdir_cache->vd_deblk[ul];
32849 +               deblk_end.deblk = p.deblk + deblk_sz;
32850 +               offset = ul;
32851 +               offset *= deblk_sz;
32852 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32853 +                       unsigned int l;
32854 +
32855 +                       l = calc_size(p.de->de_str.len);
32856 +                       offset += l;
32857 +                       p.deblk += l;
32858 +               }
32859 +               if (!is_deblk_end(&p, &deblk_end)) {
32860 +                       valid = 1;
32861 +                       vdir_cache->vd_last.ul = ul;
32862 +                       vdir_cache->vd_last.p = p;
32863 +                       break;
32864 +               }
32865 +       }
32866 +
32867 +out:
32868 +       /* smp_mb(); */
32869 +       if (!valid)
32870 +               AuDbg("valid %d\n", !valid);
32871 +       return valid;
32872 +}
32873 +
32874 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32875 +{
32876 +       unsigned int l, deblk_sz;
32877 +       union au_vdir_deblk_p deblk_end;
32878 +       struct au_vdir *vdir_cache;
32879 +       struct au_vdir_de *de;
32880 +
32881 +       if (!seek_vdir(file, ctx))
32882 +               return 0;
32883 +
32884 +       vdir_cache = au_fvdir_cache(file);
32885 +       deblk_sz = vdir_cache->vd_deblk_sz;
32886 +       while (1) {
32887 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32888 +               deblk_end.deblk += deblk_sz;
32889 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32890 +                       de = vdir_cache->vd_last.p.de;
32891 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32892 +                             de->de_str.len, de->de_str.name, ctx->pos,
32893 +                             (unsigned long)de->de_ino, de->de_type);
32894 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32895 +                                              de->de_str.len, de->de_ino,
32896 +                                              de->de_type))) {
32897 +                               /* todo: ignore the error caused by udba? */
32898 +                               /* return err; */
32899 +                               return 0;
32900 +                       }
32901 +
32902 +                       l = calc_size(de->de_str.len);
32903 +                       vdir_cache->vd_last.p.deblk += l;
32904 +                       ctx->pos += l;
32905 +               }
32906 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32907 +                       vdir_cache->vd_last.ul++;
32908 +                       vdir_cache->vd_last.p.deblk
32909 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32910 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32911 +                       continue;
32912 +               }
32913 +               break;
32914 +       }
32915 +
32916 +       /* smp_mb(); */
32917 +       return 0;
32918 +}
32919 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32920 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32921 +++ linux/fs/aufs/vfsub.c       2022-08-02 12:12:45.168975258 +0200
32922 @@ -0,0 +1,918 @@
32923 +// SPDX-License-Identifier: GPL-2.0
32924 +/*
32925 + * Copyright (C) 2005-2021 Junjiro R. Okajima
32926 + *
32927 + * This program, aufs is free software; you can redistribute it and/or modify
32928 + * it under the terms of the GNU General Public License as published by
32929 + * the Free Software Foundation; either version 2 of the License, or
32930 + * (at your option) any later version.
32931 + *
32932 + * This program is distributed in the hope that it will be useful,
32933 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32934 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32935 + * GNU General Public License for more details.
32936 + *
32937 + * You should have received a copy of the GNU General Public License
32938 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32939 + */
32940 +
32941 +/*
32942 + * sub-routines for VFS
32943 + */
32944 +
32945 +#include <linux/mnt_namespace.h>
32946 +#include <linux/nsproxy.h>
32947 +#include <linux/security.h>
32948 +#include <linux/splice.h>
32949 +#include "aufs.h"
32950 +
32951 +#ifdef CONFIG_AUFS_BR_FUSE
32952 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32953 +{
32954 +       if (!au_test_fuse(h_sb) || !au_userns)
32955 +               return 0;
32956 +
32957 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32958 +}
32959 +#endif
32960 +
32961 +int vfsub_sync_filesystem(struct super_block *h_sb)
32962 +{
32963 +       int err;
32964 +
32965 +       lockdep_off();
32966 +       down_read(&h_sb->s_umount);
32967 +       err = sync_filesystem(h_sb);
32968 +       up_read(&h_sb->s_umount);
32969 +       lockdep_on();
32970 +
32971 +       return err;
32972 +}
32973 +
32974 +/* ---------------------------------------------------------------------- */
32975 +
32976 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32977 +{
32978 +       int err;
32979 +       struct kstat st;
32980 +       struct super_block *h_sb;
32981 +
32982 +       /*
32983 +        * Always needs h_path->mnt for LSM or FUSE branch.
32984 +        */
32985 +       AuDebugOn(!h_path->mnt);
32986 +
32987 +       /* for remote fs, leave work for its getattr or d_revalidate */
32988 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32989 +       /* still some fs may acquire i_mutex. we need to skip them */
32990 +       err = 0;
32991 +       if (!did)
32992 +               did = &err;
32993 +       h_sb = h_path->dentry->d_sb;
32994 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32995 +       if (*did)
32996 +               err = vfsub_getattr(h_path, &st);
32997 +
32998 +       return err;
32999 +}
33000 +
33001 +/* ---------------------------------------------------------------------- */
33002 +
33003 +struct file *vfsub_dentry_open(struct path *path, int flags)
33004 +{
33005 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
33006 +                          current_cred());
33007 +}
33008 +
33009 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
33010 +{
33011 +       struct file *file;
33012 +
33013 +       lockdep_off();
33014 +       file = filp_open(path,
33015 +                        oflags /* | __FMODE_NONOTIFY */,
33016 +                        mode);
33017 +       lockdep_on();
33018 +       if (IS_ERR(file))
33019 +               goto out;
33020 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33021 +
33022 +out:
33023 +       return file;
33024 +}
33025 +
33026 +/*
33027 + * Ideally this function should call VFS:do_last() in order to keep all its
33028 + * checkings. But it is very hard for aufs to regenerate several VFS internal
33029 + * structure such as nameidata. This is a second (or third) best approach.
33030 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
33031 + */
33032 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33033 +                     struct vfsub_aopen_args *args)
33034 +{
33035 +       int err;
33036 +       struct au_branch *br = args->br;
33037 +       struct file *file = args->file;
33038 +       /* copied from linux/fs/namei.c:atomic_open() */
33039 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33040 +
33041 +       IMustLock(dir);
33042 +       AuDebugOn(!dir->i_op->atomic_open);
33043 +
33044 +       err = au_br_test_oflag(args->open_flag, br);
33045 +       if (unlikely(err))
33046 +               goto out;
33047 +
33048 +       au_lcnt_inc(&br->br_nfiles);
33049 +       file->f_path.dentry = DENTRY_NOT_SET;
33050 +       file->f_path.mnt = au_br_mnt(br);
33051 +       AuDbg("%ps\n", dir->i_op->atomic_open);
33052 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
33053 +                                    args->create_mode);
33054 +       if (unlikely(err < 0)) {
33055 +               au_lcnt_dec(&br->br_nfiles);
33056 +               goto out;
33057 +       }
33058 +
33059 +       /* temporary workaround for nfsv4 branch */
33060 +       if (au_test_nfs(dir->i_sb))
33061 +               nfs_mark_for_revalidate(dir);
33062 +
33063 +       if (file->f_mode & FMODE_CREATED)
33064 +               fsnotify_create(dir, dentry);
33065 +       if (!(file->f_mode & FMODE_OPENED)) {
33066 +               au_lcnt_dec(&br->br_nfiles);
33067 +               goto out;
33068 +       }
33069 +
33070 +       /* todo: call VFS:may_open() here */
33071 +       /* todo: ima_file_check() too? */
33072 +       if (!err && (args->open_flag & __FMODE_EXEC))
33073 +               err = deny_write_access(file);
33074 +       if (!err)
33075 +               fsnotify_open(file);
33076 +       else
33077 +               au_lcnt_dec(&br->br_nfiles);
33078 +       /* note that the file is created and still opened */
33079 +
33080 +out:
33081 +       return err;
33082 +}
33083 +
33084 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33085 +{
33086 +       int err;
33087 +
33088 +       err = kern_path(name, flags, path);
33089 +       if (!err && d_is_positive(path->dentry))
33090 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33091 +       return err;
33092 +}
33093 +
33094 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33095 +                                            struct path *ppath, int len)
33096 +{
33097 +       struct path path;
33098 +
33099 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
33100 +       if (IS_ERR(path.dentry))
33101 +               goto out;
33102 +       if (d_is_positive(path.dentry)) {
33103 +               path.mnt = ppath->mnt;
33104 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33105 +       }
33106 +
33107 +out:
33108 +       AuTraceErrPtr(path.dentry);
33109 +       return path.dentry;
33110 +}
33111 +
33112 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33113 +                                   int len)
33114 +{
33115 +       struct path path;
33116 +
33117 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33118 +       IMustLock(d_inode(ppath->dentry));
33119 +
33120 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
33121 +       if (IS_ERR(path.dentry))
33122 +               goto out;
33123 +       if (d_is_positive(path.dentry)) {
33124 +               path.mnt = ppath->mnt;
33125 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33126 +       }
33127 +
33128 +out:
33129 +       AuTraceErrPtr(path.dentry);
33130 +       return path.dentry;
33131 +}
33132 +
33133 +void vfsub_call_lkup_one(void *args)
33134 +{
33135 +       struct vfsub_lkup_one_args *a = args;
33136 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
33137 +}
33138 +
33139 +/* ---------------------------------------------------------------------- */
33140 +
33141 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33142 +                                struct dentry *d2, struct au_hinode *hdir2)
33143 +{
33144 +       struct dentry *d;
33145 +
33146 +       lockdep_off();
33147 +       d = lock_rename(d1, d2);
33148 +       lockdep_on();
33149 +       au_hn_suspend(hdir1);
33150 +       if (hdir1 != hdir2)
33151 +               au_hn_suspend(hdir2);
33152 +
33153 +       return d;
33154 +}
33155 +
33156 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33157 +                        struct dentry *d2, struct au_hinode *hdir2)
33158 +{
33159 +       au_hn_resume(hdir1);
33160 +       if (hdir1 != hdir2)
33161 +               au_hn_resume(hdir2);
33162 +       lockdep_off();
33163 +       unlock_rename(d1, d2);
33164 +       lockdep_on();
33165 +}
33166 +
33167 +/* ---------------------------------------------------------------------- */
33168 +
33169 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33170 +{
33171 +       int err;
33172 +       struct dentry *d;
33173 +       struct user_namespace *userns;
33174 +
33175 +       IMustLock(dir);
33176 +
33177 +       d = path->dentry;
33178 +       path->dentry = d->d_parent;
33179 +       err = security_path_mknod(path, d, mode, 0);
33180 +       path->dentry = d;
33181 +       if (unlikely(err))
33182 +               goto out;
33183 +       userns = mnt_user_ns(path->mnt);
33184 +
33185 +       lockdep_off();
33186 +       err = vfs_create(userns, dir, path->dentry, mode, want_excl);
33187 +       lockdep_on();
33188 +       if (!err) {
33189 +               struct path tmp = *path;
33190 +               int did;
33191 +
33192 +               vfsub_update_h_iattr(&tmp, &did);
33193 +               if (did) {
33194 +                       tmp.dentry = path->dentry->d_parent;
33195 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33196 +               }
33197 +               /*ignore*/
33198 +       }
33199 +
33200 +out:
33201 +       return err;
33202 +}
33203 +
33204 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33205 +{
33206 +       int err;
33207 +       struct dentry *d;
33208 +       struct user_namespace *userns;
33209 +
33210 +       IMustLock(dir);
33211 +
33212 +       d = path->dentry;
33213 +       path->dentry = d->d_parent;
33214 +       err = security_path_symlink(path, d, symname);
33215 +       path->dentry = d;
33216 +       if (unlikely(err))
33217 +               goto out;
33218 +       userns = mnt_user_ns(path->mnt);
33219 +
33220 +       lockdep_off();
33221 +       err = vfs_symlink(userns, dir, path->dentry, symname);
33222 +       lockdep_on();
33223 +       if (!err) {
33224 +               struct path tmp = *path;
33225 +               int did;
33226 +
33227 +               vfsub_update_h_iattr(&tmp, &did);
33228 +               if (did) {
33229 +                       tmp.dentry = path->dentry->d_parent;
33230 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33231 +               }
33232 +               /*ignore*/
33233 +       }
33234 +
33235 +out:
33236 +       return err;
33237 +}
33238 +
33239 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33240 +{
33241 +       int err;
33242 +       struct dentry *d;
33243 +       struct user_namespace *userns;
33244 +
33245 +       IMustLock(dir);
33246 +
33247 +       d = path->dentry;
33248 +       path->dentry = d->d_parent;
33249 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33250 +       path->dentry = d;
33251 +       if (unlikely(err))
33252 +               goto out;
33253 +       userns = mnt_user_ns(path->mnt);
33254 +
33255 +       lockdep_off();
33256 +       err = vfs_mknod(userns, dir, path->dentry, mode, dev);
33257 +       lockdep_on();
33258 +       if (!err) {
33259 +               struct path tmp = *path;
33260 +               int did;
33261 +
33262 +               vfsub_update_h_iattr(&tmp, &did);
33263 +               if (did) {
33264 +                       tmp.dentry = path->dentry->d_parent;
33265 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33266 +               }
33267 +               /*ignore*/
33268 +       }
33269 +
33270 +out:
33271 +       return err;
33272 +}
33273 +
33274 +static int au_test_nlink(struct inode *inode)
33275 +{
33276 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33277 +
33278 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33279 +           || inode->i_nlink < link_max)
33280 +               return 0;
33281 +       return -EMLINK;
33282 +}
33283 +
33284 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33285 +              struct inode **delegated_inode)
33286 +{
33287 +       int err;
33288 +       struct dentry *d;
33289 +       struct user_namespace *userns;
33290 +
33291 +       IMustLock(dir);
33292 +
33293 +       err = au_test_nlink(d_inode(src_dentry));
33294 +       if (unlikely(err))
33295 +               return err;
33296 +
33297 +       /* we don't call may_linkat() */
33298 +       d = path->dentry;
33299 +       path->dentry = d->d_parent;
33300 +       err = security_path_link(src_dentry, path, d);
33301 +       path->dentry = d;
33302 +       if (unlikely(err))
33303 +               goto out;
33304 +       userns = mnt_user_ns(path->mnt);
33305 +
33306 +       lockdep_off();
33307 +       err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
33308 +       lockdep_on();
33309 +       if (!err) {
33310 +               struct path tmp = *path;
33311 +               int did;
33312 +
33313 +               /* fuse has different memory inode for the same inumber */
33314 +               vfsub_update_h_iattr(&tmp, &did);
33315 +               if (did) {
33316 +                       tmp.dentry = path->dentry->d_parent;
33317 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33318 +                       tmp.dentry = src_dentry;
33319 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33320 +               }
33321 +               /*ignore*/
33322 +       }
33323 +
33324 +out:
33325 +       return err;
33326 +}
33327 +
33328 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33329 +                struct inode *dir, struct path *path,
33330 +                struct inode **delegated_inode, unsigned int flags)
33331 +{
33332 +       int err;
33333 +       struct renamedata rd;
33334 +       struct path tmp = {
33335 +               .mnt    = path->mnt
33336 +       };
33337 +       struct dentry *d;
33338 +
33339 +       IMustLock(dir);
33340 +       IMustLock(src_dir);
33341 +
33342 +       d = path->dentry;
33343 +       path->dentry = d->d_parent;
33344 +       tmp.dentry = src_dentry->d_parent;
33345 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33346 +       path->dentry = d;
33347 +       if (unlikely(err))
33348 +               goto out;
33349 +
33350 +       rd.old_mnt_userns = mnt_user_ns(path->mnt);
33351 +       rd.old_dir = src_dir;
33352 +       rd.old_dentry = src_dentry;
33353 +       rd.new_mnt_userns = rd.old_mnt_userns;
33354 +       rd.new_dir = dir;
33355 +       rd.new_dentry = path->dentry;
33356 +       rd.delegated_inode = delegated_inode;
33357 +       rd.flags = flags;
33358 +       lockdep_off();
33359 +       err = vfs_rename(&rd);
33360 +       lockdep_on();
33361 +       if (!err) {
33362 +               int did;
33363 +
33364 +               tmp.dentry = d->d_parent;
33365 +               vfsub_update_h_iattr(&tmp, &did);
33366 +               if (did) {
33367 +                       tmp.dentry = src_dentry;
33368 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33369 +                       tmp.dentry = src_dentry->d_parent;
33370 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33371 +               }
33372 +               /*ignore*/
33373 +       }
33374 +
33375 +out:
33376 +       return err;
33377 +}
33378 +
33379 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33380 +{
33381 +       int err;
33382 +       struct dentry *d;
33383 +       struct user_namespace *userns;
33384 +
33385 +       IMustLock(dir);
33386 +
33387 +       d = path->dentry;
33388 +       path->dentry = d->d_parent;
33389 +       err = security_path_mkdir(path, d, mode);
33390 +       path->dentry = d;
33391 +       if (unlikely(err))
33392 +               goto out;
33393 +       userns = mnt_user_ns(path->mnt);
33394 +
33395 +       lockdep_off();
33396 +       err = vfs_mkdir(userns, dir, path->dentry, mode);
33397 +       lockdep_on();
33398 +       if (!err) {
33399 +               struct path tmp = *path;
33400 +               int did;
33401 +
33402 +               vfsub_update_h_iattr(&tmp, &did);
33403 +               if (did) {
33404 +                       tmp.dentry = path->dentry->d_parent;
33405 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33406 +               }
33407 +               /*ignore*/
33408 +       }
33409 +
33410 +out:
33411 +       return err;
33412 +}
33413 +
33414 +int vfsub_rmdir(struct inode *dir, struct path *path)
33415 +{
33416 +       int err;
33417 +       struct dentry *d;
33418 +       struct user_namespace *userns;
33419 +
33420 +       IMustLock(dir);
33421 +
33422 +       d = path->dentry;
33423 +       path->dentry = d->d_parent;
33424 +       err = security_path_rmdir(path, d);
33425 +       path->dentry = d;
33426 +       if (unlikely(err))
33427 +               goto out;
33428 +       userns = mnt_user_ns(path->mnt);
33429 +
33430 +       lockdep_off();
33431 +       err = vfs_rmdir(userns, dir, path->dentry);
33432 +       lockdep_on();
33433 +       if (!err) {
33434 +               struct path tmp = {
33435 +                       .dentry = path->dentry->d_parent,
33436 +                       .mnt    = path->mnt
33437 +               };
33438 +
33439 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33440 +       }
33441 +
33442 +out:
33443 +       return err;
33444 +}
33445 +
33446 +/* ---------------------------------------------------------------------- */
33447 +
33448 +/* todo: support mmap_sem? */
33449 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33450 +                    loff_t *ppos)
33451 +{
33452 +       ssize_t err;
33453 +
33454 +       lockdep_off();
33455 +       err = vfs_read(file, ubuf, count, ppos);
33456 +       lockdep_on();
33457 +       if (err >= 0)
33458 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33459 +       return err;
33460 +}
33461 +
33462 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33463 +                    loff_t *ppos)
33464 +{
33465 +       ssize_t err;
33466 +
33467 +       lockdep_off();
33468 +       err = kernel_read(file, kbuf, count, ppos);
33469 +       lockdep_on();
33470 +       AuTraceErr(err);
33471 +       if (err >= 0)
33472 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33473 +       return err;
33474 +}
33475 +
33476 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33477 +                     loff_t *ppos)
33478 +{
33479 +       ssize_t err;
33480 +
33481 +       lockdep_off();
33482 +       err = vfs_write(file, ubuf, count, ppos);
33483 +       lockdep_on();
33484 +       if (err >= 0)
33485 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33486 +       return err;
33487 +}
33488 +
33489 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33490 +{
33491 +       ssize_t err;
33492 +
33493 +       lockdep_off();
33494 +       err = kernel_write(file, kbuf, count, ppos);
33495 +       lockdep_on();
33496 +       if (err >= 0)
33497 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33498 +       return err;
33499 +}
33500 +
33501 +int vfsub_flush(struct file *file, fl_owner_t id)
33502 +{
33503 +       int err;
33504 +
33505 +       err = 0;
33506 +       if (file->f_op->flush) {
33507 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33508 +                       err = file->f_op->flush(file, id);
33509 +               else {
33510 +                       lockdep_off();
33511 +                       err = file->f_op->flush(file, id);
33512 +                       lockdep_on();
33513 +               }
33514 +               if (!err)
33515 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33516 +               /*ignore*/
33517 +       }
33518 +       return err;
33519 +}
33520 +
33521 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33522 +{
33523 +       int err;
33524 +
33525 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33526 +
33527 +       lockdep_off();
33528 +       err = iterate_dir(file, ctx);
33529 +       lockdep_on();
33530 +       if (err >= 0)
33531 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33532 +
33533 +       return err;
33534 +}
33535 +
33536 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33537 +                    struct pipe_inode_info *pipe, size_t len,
33538 +                    unsigned int flags)
33539 +{
33540 +       long err;
33541 +
33542 +       lockdep_off();
33543 +       err = do_splice_to(in, ppos, pipe, len, flags);
33544 +       lockdep_on();
33545 +       file_accessed(in);
33546 +       if (err >= 0)
33547 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33548 +       return err;
33549 +}
33550 +
33551 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33552 +                      loff_t *ppos, size_t len, unsigned int flags)
33553 +{
33554 +       long err;
33555 +
33556 +       lockdep_off();
33557 +       err = do_splice_from(pipe, out, ppos, len, flags);
33558 +       lockdep_on();
33559 +       if (err >= 0)
33560 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33561 +       return err;
33562 +}
33563 +
33564 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33565 +{
33566 +       int err;
33567 +
33568 +       /* file can be NULL */
33569 +       lockdep_off();
33570 +       err = vfs_fsync(file, datasync);
33571 +       lockdep_on();
33572 +       if (!err) {
33573 +               if (!path) {
33574 +                       AuDebugOn(!file);
33575 +                       path = &file->f_path;
33576 +               }
33577 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33578 +       }
33579 +       return err;
33580 +}
33581 +
33582 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33583 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33584 +               struct file *h_file)
33585 +{
33586 +       int err;
33587 +       struct inode *h_inode;
33588 +       struct super_block *h_sb;
33589 +       struct user_namespace *h_userns;
33590 +
33591 +       if (!h_file) {
33592 +               err = vfsub_truncate(h_path, length);
33593 +               goto out;
33594 +       }
33595 +
33596 +       h_inode = d_inode(h_path->dentry);
33597 +       h_sb = h_inode->i_sb;
33598 +       lockdep_off();
33599 +       sb_start_write(h_sb);
33600 +       lockdep_on();
33601 +       err = security_path_truncate(h_path);
33602 +       if (!err) {
33603 +               h_userns = mnt_user_ns(h_path->mnt);
33604 +               lockdep_off();
33605 +               err = do_truncate(h_userns, h_path->dentry, length, attr,
33606 +                                 h_file);
33607 +               lockdep_on();
33608 +       }
33609 +       lockdep_off();
33610 +       sb_end_write(h_sb);
33611 +       lockdep_on();
33612 +
33613 +out:
33614 +       return err;
33615 +}
33616 +
33617 +/* ---------------------------------------------------------------------- */
33618 +
33619 +struct au_vfsub_mkdir_args {
33620 +       int *errp;
33621 +       struct inode *dir;
33622 +       struct path *path;
33623 +       int mode;
33624 +};
33625 +
33626 +static void au_call_vfsub_mkdir(void *args)
33627 +{
33628 +       struct au_vfsub_mkdir_args *a = args;
33629 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33630 +}
33631 +
33632 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33633 +{
33634 +       int err, do_sio, wkq_err;
33635 +       struct user_namespace *userns;
33636 +
33637 +       userns = mnt_user_ns(path->mnt);
33638 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33639 +       if (!do_sio) {
33640 +               lockdep_off();
33641 +               err = vfsub_mkdir(dir, path, mode);
33642 +               lockdep_on();
33643 +       } else {
33644 +               struct au_vfsub_mkdir_args args = {
33645 +                       .errp   = &err,
33646 +                       .dir    = dir,
33647 +                       .path   = path,
33648 +                       .mode   = mode
33649 +               };
33650 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33651 +               if (unlikely(wkq_err))
33652 +                       err = wkq_err;
33653 +       }
33654 +
33655 +       return err;
33656 +}
33657 +
33658 +struct au_vfsub_rmdir_args {
33659 +       int *errp;
33660 +       struct inode *dir;
33661 +       struct path *path;
33662 +};
33663 +
33664 +static void au_call_vfsub_rmdir(void *args)
33665 +{
33666 +       struct au_vfsub_rmdir_args *a = args;
33667 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33668 +}
33669 +
33670 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33671 +{
33672 +       int err, do_sio, wkq_err;
33673 +       struct user_namespace *userns;
33674 +
33675 +       userns = mnt_user_ns(path->mnt);
33676 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33677 +       if (!do_sio) {
33678 +               lockdep_off();
33679 +               err = vfsub_rmdir(dir, path);
33680 +               lockdep_on();
33681 +       } else {
33682 +               struct au_vfsub_rmdir_args args = {
33683 +                       .errp   = &err,
33684 +                       .dir    = dir,
33685 +                       .path   = path
33686 +               };
33687 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33688 +               if (unlikely(wkq_err))
33689 +                       err = wkq_err;
33690 +       }
33691 +
33692 +       return err;
33693 +}
33694 +
33695 +/* ---------------------------------------------------------------------- */
33696 +
33697 +struct notify_change_args {
33698 +       int *errp;
33699 +       struct path *path;
33700 +       struct iattr *ia;
33701 +       struct inode **delegated_inode;
33702 +};
33703 +
33704 +static void call_notify_change(void *args)
33705 +{
33706 +       struct notify_change_args *a = args;
33707 +       struct inode *h_inode;
33708 +       struct user_namespace *userns;
33709 +
33710 +       h_inode = d_inode(a->path->dentry);
33711 +       IMustLock(h_inode);
33712 +
33713 +       *a->errp = -EPERM;
33714 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33715 +               userns = mnt_user_ns(a->path->mnt);
33716 +               lockdep_off();
33717 +               *a->errp = notify_change(userns, a->path->dentry, a->ia,
33718 +                                        a->delegated_inode);
33719 +               lockdep_on();
33720 +               if (!*a->errp)
33721 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33722 +       }
33723 +       AuTraceErr(*a->errp);
33724 +}
33725 +
33726 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33727 +                       struct inode **delegated_inode)
33728 +{
33729 +       int err;
33730 +       struct notify_change_args args = {
33731 +               .errp                   = &err,
33732 +               .path                   = path,
33733 +               .ia                     = ia,
33734 +               .delegated_inode        = delegated_inode
33735 +       };
33736 +
33737 +       call_notify_change(&args);
33738 +
33739 +       return err;
33740 +}
33741 +
33742 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33743 +                           struct inode **delegated_inode)
33744 +{
33745 +       int err, wkq_err;
33746 +       struct notify_change_args args = {
33747 +               .errp                   = &err,
33748 +               .path                   = path,
33749 +               .ia                     = ia,
33750 +               .delegated_inode        = delegated_inode
33751 +       };
33752 +
33753 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33754 +       if (unlikely(wkq_err))
33755 +               err = wkq_err;
33756 +
33757 +       return err;
33758 +}
33759 +
33760 +/* ---------------------------------------------------------------------- */
33761 +
33762 +struct unlink_args {
33763 +       int *errp;
33764 +       struct inode *dir;
33765 +       struct path *path;
33766 +       struct inode **delegated_inode;
33767 +};
33768 +
33769 +static void call_unlink(void *args)
33770 +{
33771 +       struct unlink_args *a = args;
33772 +       struct dentry *d = a->path->dentry;
33773 +       struct inode *h_inode;
33774 +       struct user_namespace *userns;
33775 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33776 +                                     && au_dcount(d) == 1);
33777 +
33778 +       IMustLock(a->dir);
33779 +
33780 +       a->path->dentry = d->d_parent;
33781 +       *a->errp = security_path_unlink(a->path, d);
33782 +       a->path->dentry = d;
33783 +       if (unlikely(*a->errp))
33784 +               return;
33785 +
33786 +       if (!stop_sillyrename)
33787 +               dget(d);
33788 +       h_inode = NULL;
33789 +       if (d_is_positive(d)) {
33790 +               h_inode = d_inode(d);
33791 +               ihold(h_inode);
33792 +       }
33793 +
33794 +       userns = mnt_user_ns(a->path->mnt);
33795 +       lockdep_off();
33796 +       *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
33797 +       lockdep_on();
33798 +       if (!*a->errp) {
33799 +               struct path tmp = {
33800 +                       .dentry = d->d_parent,
33801 +                       .mnt    = a->path->mnt
33802 +               };
33803 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33804 +       }
33805 +
33806 +       if (!stop_sillyrename)
33807 +               dput(d);
33808 +       if (h_inode)
33809 +               iput(h_inode);
33810 +
33811 +       AuTraceErr(*a->errp);
33812 +}
33813 +
33814 +/*
33815 + * @dir: must be locked.
33816 + * @dentry: target dentry.
33817 + */
33818 +int vfsub_unlink(struct inode *dir, struct path *path,
33819 +                struct inode **delegated_inode, int force)
33820 +{
33821 +       int err;
33822 +       struct unlink_args args = {
33823 +               .errp                   = &err,
33824 +               .dir                    = dir,
33825 +               .path                   = path,
33826 +               .delegated_inode        = delegated_inode
33827 +       };
33828 +
33829 +       if (!force)
33830 +               call_unlink(&args);
33831 +       else {
33832 +               int wkq_err;
33833 +
33834 +               wkq_err = au_wkq_wait(call_unlink, &args);
33835 +               if (unlikely(wkq_err))
33836 +                       err = wkq_err;
33837 +       }
33838 +
33839 +       return err;
33840 +}
33841 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33842 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33843 +++ linux/fs/aufs/vfsub.h       2022-08-02 12:12:45.168975258 +0200
33844 @@ -0,0 +1,358 @@
33845 +/* SPDX-License-Identifier: GPL-2.0 */
33846 +/*
33847 + * Copyright (C) 2005-2021 Junjiro R. Okajima
33848 + *
33849 + * This program, aufs is free software; you can redistribute it and/or modify
33850 + * it under the terms of the GNU General Public License as published by
33851 + * the Free Software Foundation; either version 2 of the License, or
33852 + * (at your option) any later version.
33853 + *
33854 + * This program is distributed in the hope that it will be useful,
33855 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33856 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33857 + * GNU General Public License for more details.
33858 + *
33859 + * You should have received a copy of the GNU General Public License
33860 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33861 + */
33862 +
33863 +/*
33864 + * sub-routines for VFS
33865 + */
33866 +
33867 +#ifndef __AUFS_VFSUB_H__
33868 +#define __AUFS_VFSUB_H__
33869 +
33870 +#ifdef __KERNEL__
33871 +
33872 +#include <linux/fs.h>
33873 +#include <linux/mount.h>
33874 +#include <linux/posix_acl.h>
33875 +#include <linux/xattr.h>
33876 +#include "debug.h"
33877 +
33878 +/* copied from linux/fs/internal.h */
33879 +/* todo: BAD approach!! */
33880 +extern void __mnt_drop_write(struct vfsmount *);
33881 +extern struct file *alloc_empty_file(int, const struct cred *);
33882 +
33883 +/* ---------------------------------------------------------------------- */
33884 +
33885 +/* lock subclass for lower inode */
33886 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33887 +/* reduce? gave up. */
33888 +enum {
33889 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33890 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33891 +       AuLsc_I_PARENT2,        /* copyup dirs */
33892 +       AuLsc_I_PARENT3,        /* copyup wh */
33893 +       AuLsc_I_CHILD,
33894 +       AuLsc_I_CHILD2,
33895 +       AuLsc_I_End
33896 +};
33897 +
33898 +/* to debug easier, do not make them inlined functions */
33899 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33900 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33901 +
33902 +/* ---------------------------------------------------------------------- */
33903 +
33904 +static inline void vfsub_drop_nlink(struct inode *inode)
33905 +{
33906 +       AuDebugOn(!inode->i_nlink);
33907 +       drop_nlink(inode);
33908 +}
33909 +
33910 +static inline void vfsub_dead_dir(struct inode *inode)
33911 +{
33912 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33913 +       inode->i_flags |= S_DEAD;
33914 +       clear_nlink(inode);
33915 +}
33916 +
33917 +static inline int vfsub_native_ro(struct inode *inode)
33918 +{
33919 +       return sb_rdonly(inode->i_sb)
33920 +               || IS_RDONLY(inode)
33921 +               /* || IS_APPEND(inode) */
33922 +               || IS_IMMUTABLE(inode);
33923 +}
33924 +
33925 +#ifdef CONFIG_AUFS_BR_FUSE
33926 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33927 +#else
33928 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33929 +#endif
33930 +
33931 +int vfsub_sync_filesystem(struct super_block *h_sb);
33932 +
33933 +/* ---------------------------------------------------------------------- */
33934 +
33935 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33936 +struct file *vfsub_dentry_open(struct path *path, int flags);
33937 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33938 +struct au_branch;
33939 +struct vfsub_aopen_args {
33940 +       struct file             *file;
33941 +       unsigned int            open_flag;
33942 +       umode_t                 create_mode;
33943 +       struct au_branch        *br;
33944 +};
33945 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33946 +                     struct vfsub_aopen_args *args);
33947 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33948 +
33949 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33950 +                                            struct path *ppath, int len);
33951 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33952 +                                   int len);
33953 +
33954 +struct vfsub_lkup_one_args {
33955 +       struct dentry **errp;
33956 +       struct qstr *name;
33957 +       struct path *ppath;
33958 +};
33959 +
33960 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33961 +                                           struct path *ppath)
33962 +{
33963 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
33964 +}
33965 +
33966 +void vfsub_call_lkup_one(void *args);
33967 +
33968 +/* ---------------------------------------------------------------------- */
33969 +
33970 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33971 +{
33972 +       int err;
33973 +
33974 +       lockdep_off();
33975 +       err = mnt_want_write(mnt);
33976 +       lockdep_on();
33977 +       return err;
33978 +}
33979 +
33980 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33981 +{
33982 +       lockdep_off();
33983 +       mnt_drop_write(mnt);
33984 +       lockdep_on();
33985 +}
33986 +
33987 +#if 0 /* reserved */
33988 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33989 +{
33990 +       lockdep_off();
33991 +       mnt_drop_write_file(file);
33992 +       lockdep_on();
33993 +}
33994 +#endif
33995 +
33996 +/* ---------------------------------------------------------------------- */
33997 +
33998 +struct au_hinode;
33999 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
34000 +                                struct dentry *d2, struct au_hinode *hdir2);
34001 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
34002 +                        struct dentry *d2, struct au_hinode *hdir2);
34003 +
34004 +int vfsub_create(struct inode *dir, struct path *path, int mode,
34005 +                bool want_excl);
34006 +int vfsub_symlink(struct inode *dir, struct path *path,
34007 +                 const char *symname);
34008 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
34009 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
34010 +              struct path *path, struct inode **delegated_inode);
34011 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
34012 +                struct inode *hdir, struct path *path,
34013 +                struct inode **delegated_inode, unsigned int flags);
34014 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
34015 +int vfsub_rmdir(struct inode *dir, struct path *path);
34016 +
34017 +/* ---------------------------------------------------------------------- */
34018 +
34019 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
34020 +                    loff_t *ppos);
34021 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
34022 +                       loff_t *ppos);
34023 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
34024 +                     loff_t *ppos);
34025 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
34026 +                     loff_t *ppos);
34027 +int vfsub_flush(struct file *file, fl_owner_t id);
34028 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
34029 +
34030 +static inline loff_t vfsub_f_size_read(struct file *file)
34031 +{
34032 +       return i_size_read(file_inode(file));
34033 +}
34034 +
34035 +static inline unsigned int vfsub_file_flags(struct file *file)
34036 +{
34037 +       unsigned int flags;
34038 +
34039 +       spin_lock(&file->f_lock);
34040 +       flags = file->f_flags;
34041 +       spin_unlock(&file->f_lock);
34042 +
34043 +       return flags;
34044 +}
34045 +
34046 +static inline int vfsub_file_execed(struct file *file)
34047 +{
34048 +       /* todo: direct access f_flags */
34049 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34050 +}
34051 +
34052 +#if 0 /* reserved */
34053 +static inline void vfsub_file_accessed(struct file *h_file)
34054 +{
34055 +       file_accessed(h_file);
34056 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34057 +}
34058 +#endif
34059 +
34060 +#if 0 /* reserved */
34061 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34062 +                                    struct dentry *h_dentry)
34063 +{
34064 +       struct path h_path = {
34065 +               .dentry = h_dentry,
34066 +               .mnt    = h_mnt
34067 +       };
34068 +       touch_atime(&h_path);
34069 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34070 +}
34071 +#endif
34072 +
34073 +static inline int vfsub_update_time(struct inode *h_inode,
34074 +                                   struct timespec64 *ts, int flags)
34075 +{
34076 +       return inode_update_time(h_inode, ts, flags);
34077 +       /* no vfsub_update_h_iattr() since we don't have struct path */
34078 +}
34079 +
34080 +#ifdef CONFIG_FS_POSIX_ACL
34081 +static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
34082 +                                 struct inode *h_inode, umode_t h_mode)
34083 +{
34084 +       int err;
34085 +
34086 +       err = posix_acl_chmod(h_userns, h_inode, h_mode);
34087 +       if (err == -EOPNOTSUPP)
34088 +               err = 0;
34089 +       return err;
34090 +}
34091 +#else
34092 +AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
34093 +          struct inode *h_inode, umode_t h_mode);
34094 +#endif
34095 +
34096 +long vfsub_splice_to(struct file *in, loff_t *ppos,
34097 +                    struct pipe_inode_info *pipe, size_t len,
34098 +                    unsigned int flags);
34099 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34100 +                      loff_t *ppos, size_t len, unsigned int flags);
34101 +
34102 +static inline long vfsub_truncate(struct path *path, loff_t length)
34103 +{
34104 +       long err;
34105 +
34106 +       lockdep_off();
34107 +       err = vfs_truncate(path, length);
34108 +       lockdep_on();
34109 +       return err;
34110 +}
34111 +
34112 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34113 +               struct file *h_file);
34114 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34115 +
34116 +/*
34117 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34118 + * ioctl.
34119 + */
34120 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34121 +                                           loff_t len)
34122 +{
34123 +       loff_t err;
34124 +
34125 +       lockdep_off();
34126 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34127 +       lockdep_on();
34128 +
34129 +       return err;
34130 +}
34131 +
34132 +/* copy_file_range(2) is a systemcall */
34133 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34134 +                                           struct file *dst, loff_t dst_pos,
34135 +                                           size_t len, unsigned int flags)
34136 +{
34137 +       ssize_t ssz;
34138 +
34139 +       lockdep_off();
34140 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34141 +       lockdep_on();
34142 +
34143 +       return ssz;
34144 +}
34145 +
34146 +/* ---------------------------------------------------------------------- */
34147 +
34148 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34149 +{
34150 +       loff_t err;
34151 +
34152 +       lockdep_off();
34153 +       err = vfs_llseek(file, offset, origin);
34154 +       lockdep_on();
34155 +       return err;
34156 +}
34157 +
34158 +/* ---------------------------------------------------------------------- */
34159 +
34160 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34161 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34162 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34163 +                           struct inode **delegated_inode);
34164 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34165 +                       struct inode **delegated_inode);
34166 +int vfsub_unlink(struct inode *dir, struct path *path,
34167 +                struct inode **delegated_inode, int force);
34168 +
34169 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34170 +{
34171 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34172 +}
34173 +
34174 +/* ---------------------------------------------------------------------- */
34175 +
34176 +static inline int vfsub_setxattr(struct user_namespace *userns,
34177 +                                struct dentry *dentry, const char *name,
34178 +                                const void *value, size_t size, int flags)
34179 +{
34180 +       int err;
34181 +
34182 +       lockdep_off();
34183 +       err = vfs_setxattr(userns, dentry, name, value, size, flags);
34184 +       lockdep_on();
34185 +
34186 +       return err;
34187 +}
34188 +
34189 +static inline int vfsub_removexattr(struct user_namespace *userns,
34190 +                                   struct dentry *dentry, const char *name)
34191 +{
34192 +       int err;
34193 +
34194 +       lockdep_off();
34195 +       err = vfs_removexattr(userns, dentry, name);
34196 +       lockdep_on();
34197 +
34198 +       return err;
34199 +}
34200 +
34201 +#endif /* __KERNEL__ */
34202 +#endif /* __AUFS_VFSUB_H__ */
34203 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34204 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34205 +++ linux/fs/aufs/wbr_policy.c  2022-08-02 12:12:45.168975258 +0200
34206 @@ -0,0 +1,830 @@
34207 +// SPDX-License-Identifier: GPL-2.0
34208 +/*
34209 + * Copyright (C) 2005-2021 Junjiro R. Okajima
34210 + *
34211 + * This program, aufs is free software; you can redistribute it and/or modify
34212 + * it under the terms of the GNU General Public License as published by
34213 + * the Free Software Foundation; either version 2 of the License, or
34214 + * (at your option) any later version.
34215 + *
34216 + * This program is distributed in the hope that it will be useful,
34217 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34218 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34219 + * GNU General Public License for more details.
34220 + *
34221 + * You should have received a copy of the GNU General Public License
34222 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34223 + */
34224 +
34225 +/*
34226 + * policies for selecting one among multiple writable branches
34227 + */
34228 +
34229 +#include <linux/statfs.h>
34230 +#include "aufs.h"
34231 +
34232 +/* subset of cpup_attr() */
34233 +static noinline_for_stack
34234 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34235 +{
34236 +       int err, sbits;
34237 +       struct iattr ia;
34238 +       struct inode *h_isrc;
34239 +
34240 +       h_isrc = d_inode(h_src);
34241 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34242 +       ia.ia_mode = h_isrc->i_mode;
34243 +       ia.ia_uid = h_isrc->i_uid;
34244 +       ia.ia_gid = h_isrc->i_gid;
34245 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34246 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34247 +       /* no delegation since it is just created */
34248 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34249 +
34250 +       /* is this nfs only? */
34251 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34252 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34253 +               ia.ia_mode = h_isrc->i_mode;
34254 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34255 +       }
34256 +
34257 +       return err;
34258 +}
34259 +
34260 +#define AuCpdown_PARENT_OPQ    1
34261 +#define AuCpdown_WHED          (1 << 1)
34262 +#define AuCpdown_MADE_DIR      (1 << 2)
34263 +#define AuCpdown_DIROPQ                (1 << 3)
34264 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34265 +#define au_fset_cpdown(flags, name) \
34266 +       do { (flags) |= AuCpdown_##name; } while (0)
34267 +#define au_fclr_cpdown(flags, name) \
34268 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34269 +
34270 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34271 +                            unsigned int *flags)
34272 +{
34273 +       int err;
34274 +       struct dentry *opq_dentry;
34275 +
34276 +       opq_dentry = au_diropq_create(dentry, bdst);
34277 +       err = PTR_ERR(opq_dentry);
34278 +       if (IS_ERR(opq_dentry))
34279 +               goto out;
34280 +       dput(opq_dentry);
34281 +       au_fset_cpdown(*flags, DIROPQ);
34282 +
34283 +out:
34284 +       return err;
34285 +}
34286 +
34287 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34288 +                           struct inode *dir, aufs_bindex_t bdst)
34289 +{
34290 +       int err;
34291 +       struct path h_path;
34292 +       struct au_branch *br;
34293 +
34294 +       br = au_sbr(dentry->d_sb, bdst);
34295 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34296 +       err = PTR_ERR(h_path.dentry);
34297 +       if (IS_ERR(h_path.dentry))
34298 +               goto out;
34299 +
34300 +       err = 0;
34301 +       if (d_is_positive(h_path.dentry)) {
34302 +               h_path.mnt = au_br_mnt(br);
34303 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34304 +                                         dentry);
34305 +       }
34306 +       dput(h_path.dentry);
34307 +
34308 +out:
34309 +       return err;
34310 +}
34311 +
34312 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34313 +                        struct au_pin *pin,
34314 +                        struct dentry *h_parent, void *arg)
34315 +{
34316 +       int err, rerr;
34317 +       aufs_bindex_t bopq, btop;
34318 +       struct path h_path;
34319 +       struct dentry *parent;
34320 +       struct inode *h_dir, *h_inode, *inode, *dir;
34321 +       unsigned int *flags = arg;
34322 +
34323 +       btop = au_dbtop(dentry);
34324 +       /* dentry is di-locked */
34325 +       parent = dget_parent(dentry);
34326 +       dir = d_inode(parent);
34327 +       h_dir = d_inode(h_parent);
34328 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34329 +       IMustLock(h_dir);
34330 +
34331 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34332 +       if (unlikely(err < 0))
34333 +               goto out;
34334 +       h_path.dentry = au_h_dptr(dentry, bdst);
34335 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34336 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34337 +       if (unlikely(err))
34338 +               goto out_put;
34339 +       au_fset_cpdown(*flags, MADE_DIR);
34340 +
34341 +       bopq = au_dbdiropq(dentry);
34342 +       au_fclr_cpdown(*flags, WHED);
34343 +       au_fclr_cpdown(*flags, DIROPQ);
34344 +       if (au_dbwh(dentry) == bdst)
34345 +               au_fset_cpdown(*flags, WHED);
34346 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34347 +               au_fset_cpdown(*flags, PARENT_OPQ);
34348 +       h_inode = d_inode(h_path.dentry);
34349 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34350 +       if (au_ftest_cpdown(*flags, WHED)) {
34351 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34352 +               if (unlikely(err)) {
34353 +                       inode_unlock(h_inode);
34354 +                       goto out_dir;
34355 +               }
34356 +       }
34357 +
34358 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34359 +       inode_unlock(h_inode);
34360 +       if (unlikely(err))
34361 +               goto out_opq;
34362 +
34363 +       if (au_ftest_cpdown(*flags, WHED)) {
34364 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34365 +               if (unlikely(err))
34366 +                       goto out_opq;
34367 +       }
34368 +
34369 +       inode = d_inode(dentry);
34370 +       if (au_ibbot(inode) < bdst)
34371 +               au_set_ibbot(inode, bdst);
34372 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34373 +                     au_hi_flags(inode, /*isdir*/1));
34374 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34375 +       goto out; /* success */
34376 +
34377 +       /* revert */
34378 +out_opq:
34379 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34380 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34381 +               rerr = au_diropq_remove(dentry, bdst);
34382 +               inode_unlock(h_inode);
34383 +               if (unlikely(rerr)) {
34384 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34385 +                               dentry, bdst, rerr);
34386 +                       err = -EIO;
34387 +                       goto out;
34388 +               }
34389 +       }
34390 +out_dir:
34391 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34392 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34393 +               if (unlikely(rerr)) {
34394 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34395 +                               dentry, bdst, rerr);
34396 +                       err = -EIO;
34397 +               }
34398 +       }
34399 +out_put:
34400 +       au_set_h_dptr(dentry, bdst, NULL);
34401 +       if (au_dbbot(dentry) == bdst)
34402 +               au_update_dbbot(dentry);
34403 +out:
34404 +       dput(parent);
34405 +       return err;
34406 +}
34407 +
34408 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34409 +{
34410 +       int err;
34411 +       unsigned int flags;
34412 +
34413 +       flags = 0;
34414 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34415 +
34416 +       return err;
34417 +}
34418 +
34419 +/* ---------------------------------------------------------------------- */
34420 +
34421 +/* policies for create */
34422 +
34423 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34424 +{
34425 +       int err, i, j, ndentry;
34426 +       aufs_bindex_t bopq;
34427 +       struct au_dcsub_pages dpages;
34428 +       struct au_dpage *dpage;
34429 +       struct dentry **dentries, *parent, *d;
34430 +
34431 +       err = au_dpages_init(&dpages, GFP_NOFS);
34432 +       if (unlikely(err))
34433 +               goto out;
34434 +       parent = dget_parent(dentry);
34435 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34436 +       if (unlikely(err))
34437 +               goto out_free;
34438 +
34439 +       err = bindex;
34440 +       for (i = 0; i < dpages.ndpage; i++) {
34441 +               dpage = dpages.dpages + i;
34442 +               dentries = dpage->dentries;
34443 +               ndentry = dpage->ndentry;
34444 +               for (j = 0; j < ndentry; j++) {
34445 +                       d = dentries[j];
34446 +                       di_read_lock_parent2(d, !AuLock_IR);
34447 +                       bopq = au_dbdiropq(d);
34448 +                       di_read_unlock(d, !AuLock_IR);
34449 +                       if (bopq >= 0 && bopq < err)
34450 +                               err = bopq;
34451 +               }
34452 +       }
34453 +
34454 +out_free:
34455 +       dput(parent);
34456 +       au_dpages_free(&dpages);
34457 +out:
34458 +       return err;
34459 +}
34460 +
34461 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34462 +{
34463 +       for (; bindex >= 0; bindex--)
34464 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34465 +                       return bindex;
34466 +       return -EROFS;
34467 +}
34468 +
34469 +/* top down parent */
34470 +static int au_wbr_create_tdp(struct dentry *dentry,
34471 +                            unsigned int flags __maybe_unused)
34472 +{
34473 +       int err;
34474 +       aufs_bindex_t btop, bindex;
34475 +       struct super_block *sb;
34476 +       struct dentry *parent, *h_parent;
34477 +
34478 +       sb = dentry->d_sb;
34479 +       btop = au_dbtop(dentry);
34480 +       err = btop;
34481 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34482 +               goto out;
34483 +
34484 +       err = -EROFS;
34485 +       parent = dget_parent(dentry);
34486 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34487 +               h_parent = au_h_dptr(parent, bindex);
34488 +               if (!h_parent || d_is_negative(h_parent))
34489 +                       continue;
34490 +
34491 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34492 +                       err = bindex;
34493 +                       break;
34494 +               }
34495 +       }
34496 +       dput(parent);
34497 +
34498 +       /* bottom up here */
34499 +       if (unlikely(err < 0)) {
34500 +               err = au_wbr_bu(sb, btop - 1);
34501 +               if (err >= 0)
34502 +                       err = au_wbr_nonopq(dentry, err);
34503 +       }
34504 +
34505 +out:
34506 +       AuDbg("b%d\n", err);
34507 +       return err;
34508 +}
34509 +
34510 +/* ---------------------------------------------------------------------- */
34511 +
34512 +/* an exception for the policy other than tdp */
34513 +static int au_wbr_create_exp(struct dentry *dentry)
34514 +{
34515 +       int err;
34516 +       aufs_bindex_t bwh, bdiropq;
34517 +       struct dentry *parent;
34518 +
34519 +       err = -1;
34520 +       bwh = au_dbwh(dentry);
34521 +       parent = dget_parent(dentry);
34522 +       bdiropq = au_dbdiropq(parent);
34523 +       if (bwh >= 0) {
34524 +               if (bdiropq >= 0)
34525 +                       err = min(bdiropq, bwh);
34526 +               else
34527 +                       err = bwh;
34528 +               AuDbg("%d\n", err);
34529 +       } else if (bdiropq >= 0) {
34530 +               err = bdiropq;
34531 +               AuDbg("%d\n", err);
34532 +       }
34533 +       dput(parent);
34534 +
34535 +       if (err >= 0)
34536 +               err = au_wbr_nonopq(dentry, err);
34537 +
34538 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34539 +               err = -1;
34540 +
34541 +       AuDbg("%d\n", err);
34542 +       return err;
34543 +}
34544 +
34545 +/* ---------------------------------------------------------------------- */
34546 +
34547 +/* round robin */
34548 +static int au_wbr_create_init_rr(struct super_block *sb)
34549 +{
34550 +       int err;
34551 +
34552 +       err = au_wbr_bu(sb, au_sbbot(sb));
34553 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34554 +       /* smp_mb(); */
34555 +
34556 +       AuDbg("b%d\n", err);
34557 +       return err;
34558 +}
34559 +
34560 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34561 +{
34562 +       int err, nbr;
34563 +       unsigned int u;
34564 +       aufs_bindex_t bindex, bbot;
34565 +       struct super_block *sb;
34566 +       atomic_t *next;
34567 +
34568 +       err = au_wbr_create_exp(dentry);
34569 +       if (err >= 0)
34570 +               goto out;
34571 +
34572 +       sb = dentry->d_sb;
34573 +       next = &au_sbi(sb)->si_wbr_rr_next;
34574 +       bbot = au_sbbot(sb);
34575 +       nbr = bbot + 1;
34576 +       for (bindex = 0; bindex <= bbot; bindex++) {
34577 +               if (!au_ftest_wbr(flags, DIR)) {
34578 +                       err = atomic_dec_return(next) + 1;
34579 +                       /* modulo for 0 is meaningless */
34580 +                       if (unlikely(!err))
34581 +                               err = atomic_dec_return(next) + 1;
34582 +               } else
34583 +                       err = atomic_read(next);
34584 +               AuDbg("%d\n", err);
34585 +               u = err;
34586 +               err = u % nbr;
34587 +               AuDbg("%d\n", err);
34588 +               if (!au_br_rdonly(au_sbr(sb, err)))
34589 +                       break;
34590 +               err = -EROFS;
34591 +       }
34592 +
34593 +       if (err >= 0)
34594 +               err = au_wbr_nonopq(dentry, err);
34595 +
34596 +out:
34597 +       AuDbg("%d\n", err);
34598 +       return err;
34599 +}
34600 +
34601 +/* ---------------------------------------------------------------------- */
34602 +
34603 +/* most free space */
34604 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34605 +{
34606 +       struct super_block *sb;
34607 +       struct au_branch *br;
34608 +       struct au_wbr_mfs *mfs;
34609 +       struct dentry *h_parent;
34610 +       aufs_bindex_t bindex, bbot;
34611 +       int err;
34612 +       unsigned long long b, bavail;
34613 +       struct path h_path;
34614 +       /* reduce the stack usage */
34615 +       struct kstatfs *st;
34616 +
34617 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34618 +       if (unlikely(!st)) {
34619 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34620 +               return;
34621 +       }
34622 +
34623 +       bavail = 0;
34624 +       sb = dentry->d_sb;
34625 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34626 +       MtxMustLock(&mfs->mfs_lock);
34627 +       mfs->mfs_bindex = -EROFS;
34628 +       mfs->mfsrr_bytes = 0;
34629 +       if (!parent) {
34630 +               bindex = 0;
34631 +               bbot = au_sbbot(sb);
34632 +       } else {
34633 +               bindex = au_dbtop(parent);
34634 +               bbot = au_dbtaildir(parent);
34635 +       }
34636 +
34637 +       for (; bindex <= bbot; bindex++) {
34638 +               if (parent) {
34639 +                       h_parent = au_h_dptr(parent, bindex);
34640 +                       if (!h_parent || d_is_negative(h_parent))
34641 +                               continue;
34642 +               }
34643 +               br = au_sbr(sb, bindex);
34644 +               if (au_br_rdonly(br))
34645 +                       continue;
34646 +
34647 +               /* sb->s_root for NFS is unreliable */
34648 +               h_path.mnt = au_br_mnt(br);
34649 +               h_path.dentry = h_path.mnt->mnt_root;
34650 +               err = vfs_statfs(&h_path, st);
34651 +               if (unlikely(err)) {
34652 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34653 +                       continue;
34654 +               }
34655 +
34656 +               /* when the available size is equal, select the lower one */
34657 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34658 +                            || sizeof(b) < sizeof(st->f_bsize));
34659 +               b = st->f_bavail * st->f_bsize;
34660 +               br->br_wbr->wbr_bytes = b;
34661 +               if (b >= bavail) {
34662 +                       bavail = b;
34663 +                       mfs->mfs_bindex = bindex;
34664 +                       mfs->mfs_jiffy = jiffies;
34665 +               }
34666 +       }
34667 +
34668 +       mfs->mfsrr_bytes = bavail;
34669 +       AuDbg("b%d\n", mfs->mfs_bindex);
34670 +       au_kfree_rcu(st);
34671 +}
34672 +
34673 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34674 +{
34675 +       int err;
34676 +       struct dentry *parent;
34677 +       struct super_block *sb;
34678 +       struct au_wbr_mfs *mfs;
34679 +
34680 +       err = au_wbr_create_exp(dentry);
34681 +       if (err >= 0)
34682 +               goto out;
34683 +
34684 +       sb = dentry->d_sb;
34685 +       parent = NULL;
34686 +       if (au_ftest_wbr(flags, PARENT))
34687 +               parent = dget_parent(dentry);
34688 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34689 +       mutex_lock(&mfs->mfs_lock);
34690 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34691 +           || mfs->mfs_bindex < 0
34692 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34693 +               au_mfs(dentry, parent);
34694 +       mutex_unlock(&mfs->mfs_lock);
34695 +       err = mfs->mfs_bindex;
34696 +       dput(parent);
34697 +
34698 +       if (err >= 0)
34699 +               err = au_wbr_nonopq(dentry, err);
34700 +
34701 +out:
34702 +       AuDbg("b%d\n", err);
34703 +       return err;
34704 +}
34705 +
34706 +static int au_wbr_create_init_mfs(struct super_block *sb)
34707 +{
34708 +       struct au_wbr_mfs *mfs;
34709 +
34710 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34711 +       mutex_init(&mfs->mfs_lock);
34712 +       mfs->mfs_jiffy = 0;
34713 +       mfs->mfs_bindex = -EROFS;
34714 +
34715 +       return 0;
34716 +}
34717 +
34718 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34719 +{
34720 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34721 +       return 0;
34722 +}
34723 +
34724 +/* ---------------------------------------------------------------------- */
34725 +
34726 +/* top down regardless parent, and then mfs */
34727 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34728 +                              unsigned int flags __maybe_unused)
34729 +{
34730 +       int err;
34731 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34732 +       unsigned long long watermark;
34733 +       struct super_block *sb;
34734 +       struct au_wbr_mfs *mfs;
34735 +       struct au_branch *br;
34736 +       struct dentry *parent;
34737 +
34738 +       sb = dentry->d_sb;
34739 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34740 +       mutex_lock(&mfs->mfs_lock);
34741 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34742 +           || mfs->mfs_bindex < 0)
34743 +               au_mfs(dentry, /*parent*/NULL);
34744 +       watermark = mfs->mfsrr_watermark;
34745 +       bmfs = mfs->mfs_bindex;
34746 +       mutex_unlock(&mfs->mfs_lock);
34747 +
34748 +       /* another style of au_wbr_create_exp() */
34749 +       bwh = au_dbwh(dentry);
34750 +       parent = dget_parent(dentry);
34751 +       btail = au_dbtaildir(parent);
34752 +       if (bwh >= 0 && bwh < btail)
34753 +               btail = bwh;
34754 +
34755 +       err = au_wbr_nonopq(dentry, btail);
34756 +       if (unlikely(err < 0))
34757 +               goto out;
34758 +       btail = err;
34759 +       bfound = -1;
34760 +       for (bindex = 0; bindex <= btail; bindex++) {
34761 +               br = au_sbr(sb, bindex);
34762 +               if (au_br_rdonly(br))
34763 +                       continue;
34764 +               if (br->br_wbr->wbr_bytes > watermark) {
34765 +                       bfound = bindex;
34766 +                       break;
34767 +               }
34768 +       }
34769 +       err = bfound;
34770 +       if (err < 0)
34771 +               err = bmfs;
34772 +
34773 +out:
34774 +       dput(parent);
34775 +       AuDbg("b%d\n", err);
34776 +       return err;
34777 +}
34778 +
34779 +/* ---------------------------------------------------------------------- */
34780 +
34781 +/* most free space and then round robin */
34782 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34783 +{
34784 +       int err;
34785 +       struct au_wbr_mfs *mfs;
34786 +
34787 +       err = au_wbr_create_mfs(dentry, flags);
34788 +       if (err >= 0) {
34789 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34790 +               mutex_lock(&mfs->mfs_lock);
34791 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34792 +                       err = au_wbr_create_rr(dentry, flags);
34793 +               mutex_unlock(&mfs->mfs_lock);
34794 +       }
34795 +
34796 +       AuDbg("b%d\n", err);
34797 +       return err;
34798 +}
34799 +
34800 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34801 +{
34802 +       int err;
34803 +
34804 +       au_wbr_create_init_mfs(sb); /* ignore */
34805 +       err = au_wbr_create_init_rr(sb);
34806 +
34807 +       return err;
34808 +}
34809 +
34810 +/* ---------------------------------------------------------------------- */
34811 +
34812 +/* top down parent and most free space */
34813 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34814 +{
34815 +       int err, e2;
34816 +       unsigned long long b;
34817 +       aufs_bindex_t bindex, btop, bbot;
34818 +       struct super_block *sb;
34819 +       struct dentry *parent, *h_parent;
34820 +       struct au_branch *br;
34821 +
34822 +       err = au_wbr_create_tdp(dentry, flags);
34823 +       if (unlikely(err < 0))
34824 +               goto out;
34825 +       parent = dget_parent(dentry);
34826 +       btop = au_dbtop(parent);
34827 +       bbot = au_dbtaildir(parent);
34828 +       if (btop == bbot)
34829 +               goto out_parent; /* success */
34830 +
34831 +       e2 = au_wbr_create_mfs(dentry, flags);
34832 +       if (e2 < 0)
34833 +               goto out_parent; /* success */
34834 +
34835 +       /* when the available size is equal, select upper one */
34836 +       sb = dentry->d_sb;
34837 +       br = au_sbr(sb, err);
34838 +       b = br->br_wbr->wbr_bytes;
34839 +       AuDbg("b%d, %llu\n", err, b);
34840 +
34841 +       for (bindex = btop; bindex <= bbot; bindex++) {
34842 +               h_parent = au_h_dptr(parent, bindex);
34843 +               if (!h_parent || d_is_negative(h_parent))
34844 +                       continue;
34845 +
34846 +               br = au_sbr(sb, bindex);
34847 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34848 +                       b = br->br_wbr->wbr_bytes;
34849 +                       err = bindex;
34850 +                       AuDbg("b%d, %llu\n", err, b);
34851 +               }
34852 +       }
34853 +
34854 +       if (err >= 0)
34855 +               err = au_wbr_nonopq(dentry, err);
34856 +
34857 +out_parent:
34858 +       dput(parent);
34859 +out:
34860 +       AuDbg("b%d\n", err);
34861 +       return err;
34862 +}
34863 +
34864 +/* ---------------------------------------------------------------------- */
34865 +
34866 +/*
34867 + * - top down parent
34868 + * - most free space with parent
34869 + * - most free space round-robin regardless parent
34870 + */
34871 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34872 +{
34873 +       int err;
34874 +       unsigned long long watermark;
34875 +       struct super_block *sb;
34876 +       struct au_branch *br;
34877 +       struct au_wbr_mfs *mfs;
34878 +
34879 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34880 +       if (unlikely(err < 0))
34881 +               goto out;
34882 +
34883 +       sb = dentry->d_sb;
34884 +       br = au_sbr(sb, err);
34885 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34886 +       mutex_lock(&mfs->mfs_lock);
34887 +       watermark = mfs->mfsrr_watermark;
34888 +       mutex_unlock(&mfs->mfs_lock);
34889 +       if (br->br_wbr->wbr_bytes < watermark)
34890 +               /* regardless the parent dir */
34891 +               err = au_wbr_create_mfsrr(dentry, flags);
34892 +
34893 +out:
34894 +       AuDbg("b%d\n", err);
34895 +       return err;
34896 +}
34897 +
34898 +/* ---------------------------------------------------------------------- */
34899 +
34900 +/* policies for copyup */
34901 +
34902 +/* top down parent */
34903 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34904 +{
34905 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34906 +}
34907 +
34908 +/* bottom up parent */
34909 +static int au_wbr_copyup_bup(struct dentry *dentry)
34910 +{
34911 +       int err;
34912 +       aufs_bindex_t bindex, btop;
34913 +       struct dentry *parent, *h_parent;
34914 +       struct super_block *sb;
34915 +
34916 +       err = -EROFS;
34917 +       sb = dentry->d_sb;
34918 +       parent = dget_parent(dentry);
34919 +       btop = au_dbtop(parent);
34920 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34921 +               h_parent = au_h_dptr(parent, bindex);
34922 +               if (!h_parent || d_is_negative(h_parent))
34923 +                       continue;
34924 +
34925 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34926 +                       err = bindex;
34927 +                       break;
34928 +               }
34929 +       }
34930 +       dput(parent);
34931 +
34932 +       /* bottom up here */
34933 +       if (unlikely(err < 0))
34934 +               err = au_wbr_bu(sb, btop - 1);
34935 +
34936 +       AuDbg("b%d\n", err);
34937 +       return err;
34938 +}
34939 +
34940 +/* bottom up */
34941 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34942 +{
34943 +       int err;
34944 +
34945 +       err = au_wbr_bu(dentry->d_sb, btop);
34946 +       AuDbg("b%d\n", err);
34947 +       if (err > btop)
34948 +               err = au_wbr_nonopq(dentry, err);
34949 +
34950 +       AuDbg("b%d\n", err);
34951 +       return err;
34952 +}
34953 +
34954 +static int au_wbr_copyup_bu(struct dentry *dentry)
34955 +{
34956 +       int err;
34957 +       aufs_bindex_t btop;
34958 +
34959 +       btop = au_dbtop(dentry);
34960 +       err = au_wbr_do_copyup_bu(dentry, btop);
34961 +       return err;
34962 +}
34963 +
34964 +/* ---------------------------------------------------------------------- */
34965 +
34966 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34967 +       [AuWbrCopyup_TDP] = {
34968 +               .copyup = au_wbr_copyup_tdp
34969 +       },
34970 +       [AuWbrCopyup_BUP] = {
34971 +               .copyup = au_wbr_copyup_bup
34972 +       },
34973 +       [AuWbrCopyup_BU] = {
34974 +               .copyup = au_wbr_copyup_bu
34975 +       }
34976 +};
34977 +
34978 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34979 +       [AuWbrCreate_TDP] = {
34980 +               .create = au_wbr_create_tdp
34981 +       },
34982 +       [AuWbrCreate_RR] = {
34983 +               .create = au_wbr_create_rr,
34984 +               .init   = au_wbr_create_init_rr
34985 +       },
34986 +       [AuWbrCreate_MFS] = {
34987 +               .create = au_wbr_create_mfs,
34988 +               .init   = au_wbr_create_init_mfs,
34989 +               .fin    = au_wbr_create_fin_mfs
34990 +       },
34991 +       [AuWbrCreate_MFSV] = {
34992 +               .create = au_wbr_create_mfs,
34993 +               .init   = au_wbr_create_init_mfs,
34994 +               .fin    = au_wbr_create_fin_mfs
34995 +       },
34996 +       [AuWbrCreate_MFSRR] = {
34997 +               .create = au_wbr_create_mfsrr,
34998 +               .init   = au_wbr_create_init_mfsrr,
34999 +               .fin    = au_wbr_create_fin_mfs
35000 +       },
35001 +       [AuWbrCreate_MFSRRV] = {
35002 +               .create = au_wbr_create_mfsrr,
35003 +               .init   = au_wbr_create_init_mfsrr,
35004 +               .fin    = au_wbr_create_fin_mfs
35005 +       },
35006 +       [AuWbrCreate_TDMFS] = {
35007 +               .create = au_wbr_create_tdmfs,
35008 +               .init   = au_wbr_create_init_mfs,
35009 +               .fin    = au_wbr_create_fin_mfs
35010 +       },
35011 +       [AuWbrCreate_TDMFSV] = {
35012 +               .create = au_wbr_create_tdmfs,
35013 +               .init   = au_wbr_create_init_mfs,
35014 +               .fin    = au_wbr_create_fin_mfs
35015 +       },
35016 +       [AuWbrCreate_PMFS] = {
35017 +               .create = au_wbr_create_pmfs,
35018 +               .init   = au_wbr_create_init_mfs,
35019 +               .fin    = au_wbr_create_fin_mfs
35020 +       },
35021 +       [AuWbrCreate_PMFSV] = {
35022 +               .create = au_wbr_create_pmfs,
35023 +               .init   = au_wbr_create_init_mfs,
35024 +               .fin    = au_wbr_create_fin_mfs
35025 +       },
35026 +       [AuWbrCreate_PMFSRR] = {
35027 +               .create = au_wbr_create_pmfsrr,
35028 +               .init   = au_wbr_create_init_mfsrr,
35029 +               .fin    = au_wbr_create_fin_mfs
35030 +       },
35031 +       [AuWbrCreate_PMFSRRV] = {
35032 +               .create = au_wbr_create_pmfsrr,
35033 +               .init   = au_wbr_create_init_mfsrr,
35034 +               .fin    = au_wbr_create_fin_mfs
35035 +       }
35036 +};
35037 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
35038 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
35039 +++ linux/fs/aufs/whout.c       2022-08-02 12:12:45.168975258 +0200
35040 @@ -0,0 +1,1072 @@
35041 +// SPDX-License-Identifier: GPL-2.0
35042 +/*
35043 + * Copyright (C) 2005-2021 Junjiro R. Okajima
35044 + *
35045 + * This program, aufs is free software; you can redistribute it and/or modify
35046 + * it under the terms of the GNU General Public License as published by
35047 + * the Free Software Foundation; either version 2 of the License, or
35048 + * (at your option) any later version.
35049 + *
35050 + * This program is distributed in the hope that it will be useful,
35051 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35052 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35053 + * GNU General Public License for more details.
35054 + *
35055 + * You should have received a copy of the GNU General Public License
35056 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35057 + */
35058 +
35059 +/*
35060 + * whiteout for logical deletion and opaque directory
35061 + */
35062 +
35063 +#include "aufs.h"
35064 +
35065 +#define WH_MASK                        0444
35066 +
35067 +/*
35068 + * If a directory contains this file, then it is opaque.  We start with the
35069 + * .wh. flag so that it is blocked by lookup.
35070 + */
35071 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35072 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
35073 +
35074 +/*
35075 + * generate whiteout name, which is NOT terminated by NULL.
35076 + * @name: original d_name.name
35077 + * @len: original d_name.len
35078 + * @wh: whiteout qstr
35079 + * returns zero when succeeds, otherwise error.
35080 + * succeeded value as wh->name should be freed by kfree().
35081 + */
35082 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35083 +{
35084 +       char *p;
35085 +
35086 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35087 +               return -ENAMETOOLONG;
35088 +
35089 +       wh->len = name->len + AUFS_WH_PFX_LEN;
35090 +       p = kmalloc(wh->len, GFP_NOFS);
35091 +       wh->name = p;
35092 +       if (p) {
35093 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35094 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35095 +               /* smp_mb(); */
35096 +               return 0;
35097 +       }
35098 +       return -ENOMEM;
35099 +}
35100 +
35101 +/* ---------------------------------------------------------------------- */
35102 +
35103 +/*
35104 + * test if the @wh_name exists under @h_ppath.
35105 + * @try_sio specifies the necessary of super-io.
35106 + */
35107 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
35108 +              struct qstr *wh_name, int try_sio)
35109 +{
35110 +       int err;
35111 +       struct dentry *wh_dentry;
35112 +
35113 +       if (!try_sio)
35114 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
35115 +       else
35116 +               wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_ppath);
35117 +       err = PTR_ERR(wh_dentry);
35118 +       if (IS_ERR(wh_dentry)) {
35119 +               if (err == -ENAMETOOLONG)
35120 +                       err = 0;
35121 +               goto out;
35122 +       }
35123 +
35124 +       err = 0;
35125 +       if (d_is_negative(wh_dentry))
35126 +               goto out_wh; /* success */
35127 +
35128 +       err = 1;
35129 +       if (d_is_reg(wh_dentry))
35130 +               goto out_wh; /* success */
35131 +
35132 +       err = -EIO;
35133 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35134 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35135 +
35136 +out_wh:
35137 +       dput(wh_dentry);
35138 +out:
35139 +       return err;
35140 +}
35141 +
35142 +/*
35143 + * test if the @h_path->dentry sets opaque or not.
35144 + */
35145 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path)
35146 +{
35147 +       int err;
35148 +       struct inode *h_dir;
35149 +
35150 +       h_dir = d_inode(h_path->dentry);
35151 +       err = au_wh_test(h_userns, h_path, &diropq_name,
35152 +                        au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
35153 +       return err;
35154 +}
35155 +
35156 +/*
35157 + * returns a negative dentry whose name is unique and temporary.
35158 + */
35159 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35160 +                            struct qstr *prefix)
35161 +{
35162 +       struct dentry *dentry;
35163 +       int i;
35164 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35165 +               *name, *p;
35166 +       /* strict atomic_t is unnecessary here */
35167 +       static unsigned short cnt;
35168 +       struct qstr qs;
35169 +       struct path h_ppath;
35170 +       struct user_namespace *h_userns;
35171 +
35172 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35173 +
35174 +       name = defname;
35175 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35176 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35177 +               dentry = ERR_PTR(-ENAMETOOLONG);
35178 +               if (unlikely(qs.len > NAME_MAX))
35179 +                       goto out;
35180 +               dentry = ERR_PTR(-ENOMEM);
35181 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35182 +               if (unlikely(!name))
35183 +                       goto out;
35184 +       }
35185 +
35186 +       /* doubly whiteout-ed */
35187 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35188 +       p = name + AUFS_WH_PFX_LEN * 2;
35189 +       memcpy(p, prefix->name, prefix->len);
35190 +       p += prefix->len;
35191 +       *p++ = '.';
35192 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35193 +
35194 +       h_ppath.dentry = h_parent;
35195 +       h_ppath.mnt = au_br_mnt(br);
35196 +       h_userns = au_br_userns(br);
35197 +       qs.name = name;
35198 +       for (i = 0; i < 3; i++) {
35199 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35200 +               dentry = au_sio_lkup_one(h_userns, &qs, &h_ppath);
35201 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35202 +                       goto out_name;
35203 +               dput(dentry);
35204 +       }
35205 +       /* pr_warn("could not get random name\n"); */
35206 +       dentry = ERR_PTR(-EEXIST);
35207 +       AuDbg("%.*s\n", AuLNPair(&qs));
35208 +       BUG();
35209 +
35210 +out_name:
35211 +       if (name != defname)
35212 +               au_kfree_try_rcu(name);
35213 +out:
35214 +       AuTraceErrPtr(dentry);
35215 +       return dentry;
35216 +}
35217 +
35218 +/*
35219 + * rename the @h_dentry on @br to the whiteouted temporary name.
35220 + */
35221 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35222 +{
35223 +       int err;
35224 +       struct path h_path = {
35225 +               .mnt = au_br_mnt(br)
35226 +       };
35227 +       struct inode *h_dir, *delegated;
35228 +       struct dentry *h_parent;
35229 +
35230 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35231 +       h_dir = d_inode(h_parent);
35232 +       IMustLock(h_dir);
35233 +
35234 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35235 +       err = PTR_ERR(h_path.dentry);
35236 +       if (IS_ERR(h_path.dentry))
35237 +               goto out;
35238 +
35239 +       /* under the same dir, no need to lock_rename() */
35240 +       delegated = NULL;
35241 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35242 +                          /*flags*/0);
35243 +       AuTraceErr(err);
35244 +       if (unlikely(err == -EWOULDBLOCK)) {
35245 +               pr_warn("cannot retry for NFSv4 delegation"
35246 +                       " for an internal rename\n");
35247 +               iput(delegated);
35248 +       }
35249 +       dput(h_path.dentry);
35250 +
35251 +out:
35252 +       AuTraceErr(err);
35253 +       return err;
35254 +}
35255 +
35256 +/* ---------------------------------------------------------------------- */
35257 +/*
35258 + * functions for removing a whiteout
35259 + */
35260 +
35261 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35262 +{
35263 +       int err, force;
35264 +       struct inode *delegated;
35265 +
35266 +       /*
35267 +        * forces superio when the dir has a sticky bit.
35268 +        * this may be a violation of unix fs semantics.
35269 +        */
35270 +       force = (h_dir->i_mode & S_ISVTX)
35271 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35272 +       delegated = NULL;
35273 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35274 +       if (unlikely(err == -EWOULDBLOCK)) {
35275 +               pr_warn("cannot retry for NFSv4 delegation"
35276 +                       " for an internal unlink\n");
35277 +               iput(delegated);
35278 +       }
35279 +       return err;
35280 +}
35281 +
35282 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35283 +                       struct dentry *dentry)
35284 +{
35285 +       int err;
35286 +
35287 +       err = do_unlink_wh(h_dir, h_path);
35288 +       if (!err && dentry)
35289 +               au_set_dbwh(dentry, -1);
35290 +
35291 +       return err;
35292 +}
35293 +
35294 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35295 +{
35296 +       int err;
35297 +       struct path h_path;
35298 +
35299 +       err = 0;
35300 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35301 +       if (IS_ERR(h_path.dentry))
35302 +               err = PTR_ERR(h_path.dentry);
35303 +       else {
35304 +               if (d_is_reg(h_path.dentry)) {
35305 +                       h_path.mnt = h_ppath->mnt;
35306 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35307 +               }
35308 +               dput(h_path.dentry);
35309 +       }
35310 +
35311 +       return err;
35312 +}
35313 +
35314 +/* ---------------------------------------------------------------------- */
35315 +/*
35316 + * initialize/clean whiteout for a branch
35317 + */
35318 +
35319 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35320 +                       const int isdir)
35321 +{
35322 +       int err;
35323 +       struct inode *delegated;
35324 +
35325 +       if (d_is_negative(whpath->dentry))
35326 +               return;
35327 +
35328 +       if (isdir)
35329 +               err = vfsub_rmdir(h_dir, whpath);
35330 +       else {
35331 +               delegated = NULL;
35332 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35333 +               if (unlikely(err == -EWOULDBLOCK)) {
35334 +                       pr_warn("cannot retry for NFSv4 delegation"
35335 +                               " for an internal unlink\n");
35336 +                       iput(delegated);
35337 +               }
35338 +       }
35339 +       if (unlikely(err))
35340 +               pr_warn("failed removing %pd (%d), ignored.\n",
35341 +                       whpath->dentry, err);
35342 +}
35343 +
35344 +static int test_linkable(struct dentry *h_root)
35345 +{
35346 +       struct inode *h_dir = d_inode(h_root);
35347 +
35348 +       if (h_dir->i_op->link)
35349 +               return 0;
35350 +
35351 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35352 +              h_root, au_sbtype(h_root->d_sb));
35353 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35354 +}
35355 +
35356 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35357 +static int au_whdir(struct inode *h_dir, struct path *path)
35358 +{
35359 +       int err;
35360 +
35361 +       err = -EEXIST;
35362 +       if (d_is_negative(path->dentry)) {
35363 +               int mode = 0700;
35364 +
35365 +               if (au_test_nfs(path->dentry->d_sb))
35366 +                       mode |= 0111;
35367 +               err = vfsub_mkdir(h_dir, path, mode);
35368 +       } else if (d_is_dir(path->dentry))
35369 +               err = 0;
35370 +       else
35371 +               pr_err("unknown %pd exists\n", path->dentry);
35372 +
35373 +       return err;
35374 +}
35375 +
35376 +struct au_wh_base {
35377 +       const struct qstr *name;
35378 +       struct dentry *dentry;
35379 +};
35380 +
35381 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35382 +                         struct path *h_path)
35383 +{
35384 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35385 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35386 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35387 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35388 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35389 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35390 +}
35391 +
35392 +/*
35393 + * returns tri-state,
35394 + * minus: error, caller should print the message
35395 + * zero: success
35396 + * plus: error, caller should NOT print the message
35397 + */
35398 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35399 +                               int do_plink, struct au_wh_base base[],
35400 +                               struct path *h_path)
35401 +{
35402 +       int err;
35403 +       struct inode *h_dir;
35404 +
35405 +       h_dir = d_inode(h_root);
35406 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35407 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35408 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35409 +       if (do_plink) {
35410 +               err = test_linkable(h_root);
35411 +               if (unlikely(err)) {
35412 +                       err = 1;
35413 +                       goto out;
35414 +               }
35415 +
35416 +               err = au_whdir(h_dir, h_path);
35417 +               if (unlikely(err))
35418 +                       goto out;
35419 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35420 +       } else
35421 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35422 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35423 +       err = au_whdir(h_dir, h_path);
35424 +       if (unlikely(err))
35425 +               goto out;
35426 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35427 +
35428 +out:
35429 +       return err;
35430 +}
35431 +
35432 +/*
35433 + * for the moment, aufs supports the branch filesystem which does not support
35434 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35435 + * copyup failed. finally, such filesystem will not be used as the writable
35436 + * branch.
35437 + *
35438 + * returns tri-state, see above.
35439 + */
35440 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35441 +                        int do_plink, struct au_wh_base base[],
35442 +                        struct path *h_path)
35443 +{
35444 +       int err;
35445 +       struct inode *h_dir;
35446 +
35447 +       WbrWhMustWriteLock(wbr);
35448 +
35449 +       err = test_linkable(h_root);
35450 +       if (unlikely(err)) {
35451 +               err = 1;
35452 +               goto out;
35453 +       }
35454 +
35455 +       /*
35456 +        * todo: should this create be done in /sbin/mount.aufs helper?
35457 +        */
35458 +       err = -EEXIST;
35459 +       h_dir = d_inode(h_root);
35460 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35461 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35462 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35463 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35464 +               err = 0;
35465 +       else
35466 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35467 +       if (unlikely(err))
35468 +               goto out;
35469 +
35470 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35471 +       if (do_plink) {
35472 +               err = au_whdir(h_dir, h_path);
35473 +               if (unlikely(err))
35474 +                       goto out;
35475 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35476 +       } else
35477 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35478 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35479 +
35480 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35481 +       err = au_whdir(h_dir, h_path);
35482 +       if (unlikely(err))
35483 +               goto out;
35484 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35485 +
35486 +out:
35487 +       return err;
35488 +}
35489 +
35490 +/*
35491 + * initialize the whiteout base file/dir for @br.
35492 + */
35493 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35494 +{
35495 +       int err, i;
35496 +       const unsigned char do_plink
35497 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35498 +       struct inode *h_dir;
35499 +       struct path path = br->br_path;
35500 +       struct dentry *h_root = path.dentry;
35501 +       struct au_wbr *wbr = br->br_wbr;
35502 +       static const struct qstr base_name[] = {
35503 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35504 +                                         sizeof(AUFS_BASE_NAME) - 1),
35505 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35506 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35507 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35508 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35509 +       };
35510 +       struct au_wh_base base[] = {
35511 +               [AuBrWh_BASE] = {
35512 +                       .name   = base_name + AuBrWh_BASE,
35513 +                       .dentry = NULL
35514 +               },
35515 +               [AuBrWh_PLINK] = {
35516 +                       .name   = base_name + AuBrWh_PLINK,
35517 +                       .dentry = NULL
35518 +               },
35519 +               [AuBrWh_ORPH] = {
35520 +                       .name   = base_name + AuBrWh_ORPH,
35521 +                       .dentry = NULL
35522 +               }
35523 +       };
35524 +
35525 +       if (wbr)
35526 +               WbrWhMustWriteLock(wbr);
35527 +
35528 +       for (i = 0; i < AuBrWh_Last; i++) {
35529 +               /* doubly whiteouted */
35530 +               struct dentry *d;
35531 +
35532 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35533 +               err = PTR_ERR(d);
35534 +               if (IS_ERR(d))
35535 +                       goto out;
35536 +
35537 +               base[i].dentry = d;
35538 +               AuDebugOn(wbr
35539 +                         && wbr->wbr_wh[i]
35540 +                         && wbr->wbr_wh[i] != base[i].dentry);
35541 +       }
35542 +
35543 +       if (wbr)
35544 +               for (i = 0; i < AuBrWh_Last; i++) {
35545 +                       dput(wbr->wbr_wh[i]);
35546 +                       wbr->wbr_wh[i] = NULL;
35547 +               }
35548 +
35549 +       err = 0;
35550 +       if (!au_br_writable(br->br_perm)) {
35551 +               h_dir = d_inode(h_root);
35552 +               au_wh_init_ro(h_dir, base, &path);
35553 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35554 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35555 +               if (err > 0)
35556 +                       goto out;
35557 +               else if (err)
35558 +                       goto out_err;
35559 +       } else {
35560 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35561 +               if (err > 0)
35562 +                       goto out;
35563 +               else if (err)
35564 +                       goto out_err;
35565 +       }
35566 +       goto out; /* success */
35567 +
35568 +out_err:
35569 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35570 +              err, h_root, au_sbtype(h_root->d_sb));
35571 +out:
35572 +       for (i = 0; i < AuBrWh_Last; i++)
35573 +               dput(base[i].dentry);
35574 +       return err;
35575 +}
35576 +
35577 +/* ---------------------------------------------------------------------- */
35578 +/*
35579 + * whiteouts are all hard-linked usually.
35580 + * when its link count reaches a ceiling, we create a new whiteout base
35581 + * asynchronously.
35582 + */
35583 +
35584 +struct reinit_br_wh {
35585 +       struct super_block *sb;
35586 +       struct au_branch *br;
35587 +};
35588 +
35589 +static void reinit_br_wh(void *arg)
35590 +{
35591 +       int err;
35592 +       aufs_bindex_t bindex;
35593 +       struct path h_path;
35594 +       struct reinit_br_wh *a = arg;
35595 +       struct au_wbr *wbr;
35596 +       struct inode *dir, *delegated;
35597 +       struct dentry *h_root;
35598 +       struct au_hinode *hdir;
35599 +
35600 +       err = 0;
35601 +       wbr = a->br->br_wbr;
35602 +       /* big aufs lock */
35603 +       si_noflush_write_lock(a->sb);
35604 +       if (!au_br_writable(a->br->br_perm))
35605 +               goto out;
35606 +       bindex = au_br_index(a->sb, a->br->br_id);
35607 +       if (unlikely(bindex < 0))
35608 +               goto out;
35609 +
35610 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35611 +       dir = d_inode(a->sb->s_root);
35612 +       hdir = au_hi(dir, bindex);
35613 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35614 +       AuDebugOn(h_root != au_br_dentry(a->br));
35615 +
35616 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35617 +       wbr_wh_write_lock(wbr);
35618 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35619 +                         h_root, a->br);
35620 +       if (!err) {
35621 +               h_path.dentry = wbr->wbr_whbase;
35622 +               h_path.mnt = au_br_mnt(a->br);
35623 +               delegated = NULL;
35624 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35625 +                                  /*force*/0);
35626 +               if (unlikely(err == -EWOULDBLOCK)) {
35627 +                       pr_warn("cannot retry for NFSv4 delegation"
35628 +                               " for an internal unlink\n");
35629 +                       iput(delegated);
35630 +               }
35631 +       } else {
35632 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35633 +               err = 0;
35634 +       }
35635 +       dput(wbr->wbr_whbase);
35636 +       wbr->wbr_whbase = NULL;
35637 +       if (!err)
35638 +               err = au_wh_init(a->br, a->sb);
35639 +       wbr_wh_write_unlock(wbr);
35640 +       au_hn_inode_unlock(hdir);
35641 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35642 +       if (!err)
35643 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35644 +
35645 +out:
35646 +       if (wbr)
35647 +               atomic_dec(&wbr->wbr_wh_running);
35648 +       au_lcnt_dec(&a->br->br_count);
35649 +       si_write_unlock(a->sb);
35650 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35651 +       au_kfree_rcu(a);
35652 +       if (unlikely(err))
35653 +               AuIOErr("err %d\n", err);
35654 +}
35655 +
35656 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35657 +{
35658 +       int do_dec, wkq_err;
35659 +       struct reinit_br_wh *arg;
35660 +
35661 +       do_dec = 1;
35662 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35663 +               goto out;
35664 +
35665 +       /* ignore ENOMEM */
35666 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35667 +       if (arg) {
35668 +               /*
35669 +                * dec(wh_running), kfree(arg) and dec(br_count)
35670 +                * in reinit function
35671 +                */
35672 +               arg->sb = sb;
35673 +               arg->br = br;
35674 +               au_lcnt_inc(&br->br_count);
35675 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35676 +               if (unlikely(wkq_err)) {
35677 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35678 +                       au_lcnt_dec(&br->br_count);
35679 +                       au_kfree_rcu(arg);
35680 +               }
35681 +               do_dec = 0;
35682 +       }
35683 +
35684 +out:
35685 +       if (do_dec)
35686 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35687 +}
35688 +
35689 +/* ---------------------------------------------------------------------- */
35690 +
35691 +/*
35692 + * create the whiteout @wh.
35693 + */
35694 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35695 +                            struct dentry *wh)
35696 +{
35697 +       int err;
35698 +       struct path h_path = {
35699 +               .dentry = wh
35700 +       };
35701 +       struct au_branch *br;
35702 +       struct au_wbr *wbr;
35703 +       struct dentry *h_parent;
35704 +       struct inode *h_dir, *delegated;
35705 +
35706 +       h_parent = wh->d_parent; /* dir inode is locked */
35707 +       h_dir = d_inode(h_parent);
35708 +       IMustLock(h_dir);
35709 +
35710 +       br = au_sbr(sb, bindex);
35711 +       h_path.mnt = au_br_mnt(br);
35712 +       wbr = br->br_wbr;
35713 +       wbr_wh_read_lock(wbr);
35714 +       if (wbr->wbr_whbase) {
35715 +               delegated = NULL;
35716 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35717 +               if (unlikely(err == -EWOULDBLOCK)) {
35718 +                       pr_warn("cannot retry for NFSv4 delegation"
35719 +                               " for an internal link\n");
35720 +                       iput(delegated);
35721 +               }
35722 +               if (!err || err != -EMLINK)
35723 +                       goto out;
35724 +
35725 +               /* link count full. re-initialize br_whbase. */
35726 +               kick_reinit_br_wh(sb, br);
35727 +       }
35728 +
35729 +       /* return this error in this context */
35730 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35731 +       if (!err)
35732 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35733 +
35734 +out:
35735 +       wbr_wh_read_unlock(wbr);
35736 +       return err;
35737 +}
35738 +
35739 +/* ---------------------------------------------------------------------- */
35740 +
35741 +/*
35742 + * create or remove the diropq.
35743 + */
35744 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35745 +                               unsigned int flags)
35746 +{
35747 +       struct dentry *opq_dentry;
35748 +       struct super_block *sb;
35749 +       struct au_branch *br;
35750 +       struct path h_path;
35751 +       int err;
35752 +
35753 +       sb = dentry->d_sb;
35754 +       br = au_sbr(sb, bindex);
35755 +       h_path.dentry = au_h_dptr(dentry, bindex);
35756 +       h_path.mnt = au_br_mnt(br);
35757 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35758 +       if (IS_ERR(opq_dentry))
35759 +               goto out;
35760 +
35761 +       if (au_ftest_diropq(flags, CREATE)) {
35762 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35763 +               if (!err) {
35764 +                       au_set_dbdiropq(dentry, bindex);
35765 +                       goto out; /* success */
35766 +               }
35767 +       } else {
35768 +               h_path.dentry = opq_dentry;
35769 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35770 +               if (!err)
35771 +                       au_set_dbdiropq(dentry, -1);
35772 +       }
35773 +       dput(opq_dentry);
35774 +       opq_dentry = ERR_PTR(err);
35775 +
35776 +out:
35777 +       return opq_dentry;
35778 +}
35779 +
35780 +struct do_diropq_args {
35781 +       struct dentry **errp;
35782 +       struct dentry *dentry;
35783 +       aufs_bindex_t bindex;
35784 +       unsigned int flags;
35785 +};
35786 +
35787 +static void call_do_diropq(void *args)
35788 +{
35789 +       struct do_diropq_args *a = args;
35790 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35791 +}
35792 +
35793 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35794 +                            unsigned int flags)
35795 +{
35796 +       struct dentry *diropq, *h_dentry;
35797 +       struct user_namespace *h_userns;
35798 +
35799 +       h_userns = au_sbr_userns(dentry->d_sb, bindex);
35800 +       h_dentry = au_h_dptr(dentry, bindex);
35801 +       if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35802 +                               MAY_EXEC | MAY_WRITE))
35803 +               diropq = do_diropq(dentry, bindex, flags);
35804 +       else {
35805 +               int wkq_err;
35806 +               struct do_diropq_args args = {
35807 +                       .errp           = &diropq,
35808 +                       .dentry         = dentry,
35809 +                       .bindex         = bindex,
35810 +                       .flags          = flags
35811 +               };
35812 +
35813 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35814 +               if (unlikely(wkq_err))
35815 +                       diropq = ERR_PTR(wkq_err);
35816 +       }
35817 +
35818 +       return diropq;
35819 +}
35820 +
35821 +/* ---------------------------------------------------------------------- */
35822 +
35823 +/*
35824 + * lookup whiteout dentry.
35825 + * @h_parent: lower parent dentry which must exist and be locked
35826 + * @base_name: name of dentry which will be whiteouted
35827 + * returns dentry for whiteout.
35828 + */
35829 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35830 +                         struct au_branch *br)
35831 +{
35832 +       int err;
35833 +       struct qstr wh_name;
35834 +       struct dentry *wh_dentry;
35835 +       struct path h_path;
35836 +
35837 +       err = au_wh_name_alloc(&wh_name, base_name);
35838 +       wh_dentry = ERR_PTR(err);
35839 +       if (!err) {
35840 +               h_path.dentry = h_parent;
35841 +               h_path.mnt = au_br_mnt(br);
35842 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35843 +               au_kfree_try_rcu(wh_name.name);
35844 +       }
35845 +       return wh_dentry;
35846 +}
35847 +
35848 +/*
35849 + * link/create a whiteout for @dentry on @bindex.
35850 + */
35851 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35852 +                           struct dentry *h_parent)
35853 +{
35854 +       struct dentry *wh_dentry;
35855 +       struct super_block *sb;
35856 +       int err;
35857 +
35858 +       sb = dentry->d_sb;
35859 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35860 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35861 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35862 +               if (!err) {
35863 +                       au_set_dbwh(dentry, bindex);
35864 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35865 +               } else {
35866 +                       dput(wh_dentry);
35867 +                       wh_dentry = ERR_PTR(err);
35868 +               }
35869 +       }
35870 +
35871 +       return wh_dentry;
35872 +}
35873 +
35874 +/* ---------------------------------------------------------------------- */
35875 +
35876 +/* Delete all whiteouts in this directory on branch bindex. */
35877 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35878 +                          aufs_bindex_t bindex)
35879 +{
35880 +       int err;
35881 +       unsigned long ul, n;
35882 +       struct qstr wh_name;
35883 +       char *p;
35884 +       struct hlist_head *head;
35885 +       struct au_vdir_wh *pos;
35886 +       struct au_vdir_destr *str;
35887 +
35888 +       err = -ENOMEM;
35889 +       p = (void *)__get_free_page(GFP_NOFS);
35890 +       wh_name.name = p;
35891 +       if (unlikely(!wh_name.name))
35892 +               goto out;
35893 +
35894 +       err = 0;
35895 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35896 +       p += AUFS_WH_PFX_LEN;
35897 +       n = whlist->nh_num;
35898 +       head = whlist->nh_head;
35899 +       for (ul = 0; !err && ul < n; ul++, head++) {
35900 +               hlist_for_each_entry(pos, head, wh_hash) {
35901 +                       if (pos->wh_bindex != bindex)
35902 +                               continue;
35903 +
35904 +                       str = &pos->wh_str;
35905 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35906 +                               memcpy(p, str->name, str->len);
35907 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35908 +                               err = unlink_wh_name(h_path, &wh_name);
35909 +                               if (!err)
35910 +                                       continue;
35911 +                               break;
35912 +                       }
35913 +                       AuIOErr("whiteout name too long %.*s\n",
35914 +                               str->len, str->name);
35915 +                       err = -EIO;
35916 +                       break;
35917 +               }
35918 +       }
35919 +       free_page((unsigned long)wh_name.name);
35920 +
35921 +out:
35922 +       return err;
35923 +}
35924 +
35925 +struct del_wh_children_args {
35926 +       int *errp;
35927 +       struct path *h_path;
35928 +       struct au_nhash *whlist;
35929 +       aufs_bindex_t bindex;
35930 +};
35931 +
35932 +static void call_del_wh_children(void *args)
35933 +{
35934 +       struct del_wh_children_args *a = args;
35935 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
35936 +}
35937 +
35938 +/* ---------------------------------------------------------------------- */
35939 +
35940 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35941 +{
35942 +       struct au_whtmp_rmdir *whtmp;
35943 +       int err;
35944 +       unsigned int rdhash;
35945 +
35946 +       SiMustAnyLock(sb);
35947 +
35948 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35949 +       if (unlikely(!whtmp)) {
35950 +               whtmp = ERR_PTR(-ENOMEM);
35951 +               goto out;
35952 +       }
35953 +
35954 +       /* no estimation for dir size */
35955 +       rdhash = au_sbi(sb)->si_rdhash;
35956 +       if (!rdhash)
35957 +               rdhash = AUFS_RDHASH_DEF;
35958 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35959 +       if (unlikely(err)) {
35960 +               au_kfree_rcu(whtmp);
35961 +               whtmp = ERR_PTR(err);
35962 +       }
35963 +
35964 +out:
35965 +       return whtmp;
35966 +}
35967 +
35968 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35969 +{
35970 +       if (whtmp->br)
35971 +               au_lcnt_dec(&whtmp->br->br_count);
35972 +       dput(whtmp->wh_dentry);
35973 +       iput(whtmp->dir);
35974 +       au_nhash_wh_free(&whtmp->whlist);
35975 +       au_kfree_rcu(whtmp);
35976 +}
35977 +
35978 +/*
35979 + * rmdir the whiteouted temporary named dir @h_dentry.
35980 + * @whlist: whiteouted children.
35981 + */
35982 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35983 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35984 +{
35985 +       int err;
35986 +       unsigned int h_nlink;
35987 +       struct path wh_path;
35988 +       struct inode *wh_inode, *h_dir;
35989 +       struct au_branch *br;
35990 +       struct user_namespace *h_userns;
35991 +
35992 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35993 +       IMustLock(h_dir);
35994 +
35995 +       br = au_sbr(dir->i_sb, bindex);
35996 +       wh_path.dentry = wh_dentry;
35997 +       wh_path.mnt = au_br_mnt(br);
35998 +       h_userns = au_br_userns(br);
35999 +       wh_inode = d_inode(wh_dentry);
36000 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
36001 +
36002 +       /*
36003 +        * someone else might change some whiteouts while we were sleeping.
36004 +        * it means this whlist may have an obsoleted entry.
36005 +        */
36006 +       if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
36007 +               err = del_wh_children(&wh_path, whlist, bindex);
36008 +       else {
36009 +               int wkq_err;
36010 +               struct del_wh_children_args args = {
36011 +                       .errp           = &err,
36012 +                       .h_path         = &wh_path,
36013 +                       .whlist         = whlist,
36014 +                       .bindex         = bindex
36015 +               };
36016 +
36017 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
36018 +               if (unlikely(wkq_err))
36019 +                       err = wkq_err;
36020 +       }
36021 +       inode_unlock(wh_inode);
36022 +
36023 +       if (!err) {
36024 +               h_nlink = h_dir->i_nlink;
36025 +               err = vfsub_rmdir(h_dir, &wh_path);
36026 +               /* some fs doesn't change the parent nlink in some cases */
36027 +               h_nlink -= h_dir->i_nlink;
36028 +       }
36029 +
36030 +       if (!err) {
36031 +               if (au_ibtop(dir) == bindex) {
36032 +                       /* todo: dir->i_mutex is necessary */
36033 +                       au_cpup_attr_timesizes(dir);
36034 +                       if (h_nlink)
36035 +                               vfsub_drop_nlink(dir);
36036 +               }
36037 +               return 0; /* success */
36038 +       }
36039 +
36040 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
36041 +       return err;
36042 +}
36043 +
36044 +static void call_rmdir_whtmp(void *args)
36045 +{
36046 +       int err;
36047 +       aufs_bindex_t bindex;
36048 +       struct au_whtmp_rmdir *a = args;
36049 +       struct super_block *sb;
36050 +       struct dentry *h_parent;
36051 +       struct inode *h_dir;
36052 +       struct au_hinode *hdir;
36053 +
36054 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
36055 +       /* inode_lock(a->dir); */
36056 +       err = -EROFS;
36057 +       sb = a->dir->i_sb;
36058 +       si_read_lock(sb, !AuLock_FLUSH);
36059 +       if (!au_br_writable(a->br->br_perm))
36060 +               goto out;
36061 +       bindex = au_br_index(sb, a->br->br_id);
36062 +       if (unlikely(bindex < 0))
36063 +               goto out;
36064 +
36065 +       err = -EIO;
36066 +       ii_write_lock_parent(a->dir);
36067 +       h_parent = dget_parent(a->wh_dentry);
36068 +       h_dir = d_inode(h_parent);
36069 +       hdir = au_hi(a->dir, bindex);
36070 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
36071 +       if (unlikely(err))
36072 +               goto out_mnt;
36073 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
36074 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36075 +                         a->br);
36076 +       if (!err)
36077 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
36078 +       au_hn_inode_unlock(hdir);
36079 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
36080 +
36081 +out_mnt:
36082 +       dput(h_parent);
36083 +       ii_write_unlock(a->dir);
36084 +out:
36085 +       /* inode_unlock(a->dir); */
36086 +       au_whtmp_rmdir_free(a);
36087 +       si_read_unlock(sb);
36088 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36089 +       if (unlikely(err))
36090 +               AuIOErr("err %d\n", err);
36091 +}
36092 +
36093 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36094 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36095 +{
36096 +       int wkq_err;
36097 +       struct super_block *sb;
36098 +
36099 +       IMustLock(dir);
36100 +
36101 +       /* all post-process will be done in do_rmdir_whtmp(). */
36102 +       sb = dir->i_sb;
36103 +       args->dir = au_igrab(dir);
36104 +       args->br = au_sbr(sb, bindex);
36105 +       au_lcnt_inc(&args->br->br_count);
36106 +       args->wh_dentry = dget(wh_dentry);
36107 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
36108 +       if (unlikely(wkq_err)) {
36109 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
36110 +               au_whtmp_rmdir_free(args);
36111 +       }
36112 +}
36113 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
36114 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
36115 +++ linux/fs/aufs/whout.h       2022-08-02 12:12:45.168975258 +0200
36116 @@ -0,0 +1,87 @@
36117 +/* SPDX-License-Identifier: GPL-2.0 */
36118 +/*
36119 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36120 + *
36121 + * This program, aufs is free software; you can redistribute it and/or modify
36122 + * it under the terms of the GNU General Public License as published by
36123 + * the Free Software Foundation; either version 2 of the License, or
36124 + * (at your option) any later version.
36125 + *
36126 + * This program is distributed in the hope that it will be useful,
36127 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36128 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36129 + * GNU General Public License for more details.
36130 + *
36131 + * You should have received a copy of the GNU General Public License
36132 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36133 + */
36134 +
36135 +/*
36136 + * whiteout for logical deletion and opaque directory
36137 + */
36138 +
36139 +#ifndef __AUFS_WHOUT_H__
36140 +#define __AUFS_WHOUT_H__
36141 +
36142 +#ifdef __KERNEL__
36143 +
36144 +#include "dir.h"
36145 +
36146 +/* whout.c */
36147 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36148 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
36149 +              struct qstr *wh_name, int try_sio);
36150 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path);
36151 +struct au_branch;
36152 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36153 +                            struct qstr *prefix);
36154 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36155 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36156 +                       struct dentry *dentry);
36157 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36158 +
36159 +/* diropq flags */
36160 +#define AuDiropq_CREATE        1
36161 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36162 +#define au_fset_diropq(flags, name) \
36163 +       do { (flags) |= AuDiropq_##name; } while (0)
36164 +#define au_fclr_diropq(flags, name) \
36165 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36166 +
36167 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36168 +                            unsigned int flags);
36169 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36170 +                         struct au_branch *br);
36171 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36172 +                           struct dentry *h_parent);
36173 +
36174 +/* real rmdir for the whiteout-ed dir */
36175 +struct au_whtmp_rmdir {
36176 +       struct inode *dir;
36177 +       struct au_branch *br;
36178 +       struct dentry *wh_dentry;
36179 +       struct au_nhash whlist;
36180 +};
36181 +
36182 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36183 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36184 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36185 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36186 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36187 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36188 +
36189 +/* ---------------------------------------------------------------------- */
36190 +
36191 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36192 +                                             aufs_bindex_t bindex)
36193 +{
36194 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36195 +}
36196 +
36197 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36198 +{
36199 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36200 +}
36201 +
36202 +#endif /* __KERNEL__ */
36203 +#endif /* __AUFS_WHOUT_H__ */
36204 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36205 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36206 +++ linux/fs/aufs/wkq.c 2022-08-02 12:12:45.168975258 +0200
36207 @@ -0,0 +1,372 @@
36208 +// SPDX-License-Identifier: GPL-2.0
36209 +/*
36210 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36211 + *
36212 + * This program, aufs is free software; you can redistribute it and/or modify
36213 + * it under the terms of the GNU General Public License as published by
36214 + * the Free Software Foundation; either version 2 of the License, or
36215 + * (at your option) any later version.
36216 + *
36217 + * This program is distributed in the hope that it will be useful,
36218 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36219 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36220 + * GNU General Public License for more details.
36221 + *
36222 + * You should have received a copy of the GNU General Public License
36223 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36224 + */
36225 +
36226 +/*
36227 + * workqueue for asynchronous/super-io operations
36228 + * todo: try new credential scheme
36229 + */
36230 +
36231 +#include <linux/module.h>
36232 +#include "aufs.h"
36233 +
36234 +/* internal workqueue named AUFS_WKQ_NAME */
36235 +
36236 +static struct workqueue_struct *au_wkq;
36237 +
36238 +struct au_wkinfo {
36239 +       struct work_struct wk;
36240 +       struct kobject *kobj;
36241 +
36242 +       unsigned int flags; /* see wkq.h */
36243 +
36244 +       au_wkq_func_t func;
36245 +       void *args;
36246 +
36247 +#ifdef CONFIG_LOCKDEP
36248 +       int dont_check;
36249 +       struct held_lock **hlock;
36250 +#endif
36251 +
36252 +       struct completion *comp;
36253 +};
36254 +
36255 +/* ---------------------------------------------------------------------- */
36256 +/*
36257 + * Aufs passes some operations to the workqueue such as the internal copyup.
36258 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36259 + * job run by workqueue depends upon the locks acquired in the other task.
36260 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36261 + * information too. And the job in the workqueue restores the info in order to
36262 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36263 + * correctly and expectedly.
36264 + */
36265 +
36266 +#ifndef CONFIG_LOCKDEP
36267 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36268 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36269 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36270 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36271 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36272 +#else
36273 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36274 +{
36275 +       wkinfo->hlock = NULL;
36276 +       wkinfo->dont_check = 0;
36277 +}
36278 +
36279 +/*
36280 + * 1: matched
36281 + * 0: unmatched
36282 + */
36283 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36284 +{
36285 +       static DEFINE_SPINLOCK(spin);
36286 +       static struct {
36287 +               char *name;
36288 +               struct lock_class_key *key;
36289 +       } a[] = {
36290 +               { .name = "&sbinfo->si_rwsem" },
36291 +               { .name = "&finfo->fi_rwsem" },
36292 +               { .name = "&dinfo->di_rwsem" },
36293 +               { .name = "&iinfo->ii_rwsem" }
36294 +       };
36295 +       static int set;
36296 +       int i;
36297 +
36298 +       /* lockless read from 'set.' see below */
36299 +       if (set == ARRAY_SIZE(a)) {
36300 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36301 +                       if (a[i].key == key)
36302 +                               goto match;
36303 +               goto unmatch;
36304 +       }
36305 +
36306 +       spin_lock(&spin);
36307 +       if (set)
36308 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36309 +                       if (a[i].key == key) {
36310 +                               spin_unlock(&spin);
36311 +                               goto match;
36312 +                       }
36313 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36314 +               if (a[i].key) {
36315 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36316 +                               spin_unlock(&spin);
36317 +                               goto match;
36318 +                       } else
36319 +                               continue;
36320 +               }
36321 +               if (strstr(a[i].name, name)) {
36322 +                       /*
36323 +                        * the order of these three lines is important for the
36324 +                        * lockless read above.
36325 +                        */
36326 +                       a[i].key = key;
36327 +                       spin_unlock(&spin);
36328 +                       set++;
36329 +                       /* AuDbg("%d, %s\n", set, name); */
36330 +                       goto match;
36331 +               }
36332 +       }
36333 +       spin_unlock(&spin);
36334 +       goto unmatch;
36335 +
36336 +match:
36337 +       return 1;
36338 +unmatch:
36339 +       return 0;
36340 +}
36341 +
36342 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36343 +{
36344 +       int err, n;
36345 +       struct task_struct *curr;
36346 +       struct held_lock **hl, *held_locks, *p;
36347 +
36348 +       err = 0;
36349 +       curr = current;
36350 +       wkinfo->dont_check = lockdep_recursing(curr);
36351 +       if (wkinfo->dont_check)
36352 +               goto out;
36353 +       n = curr->lockdep_depth;
36354 +       if (!n)
36355 +               goto out;
36356 +
36357 +       err = -ENOMEM;
36358 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36359 +       if (unlikely(!wkinfo->hlock))
36360 +               goto out;
36361 +
36362 +       err = 0;
36363 +#if 0 /* left for debugging */
36364 +       if (0 && au_debug_test())
36365 +               lockdep_print_held_locks(curr);
36366 +#endif
36367 +       held_locks = curr->held_locks;
36368 +       hl = wkinfo->hlock;
36369 +       while (n--) {
36370 +               p = held_locks++;
36371 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36372 +                       *hl++ = p;
36373 +       }
36374 +       *hl = NULL;
36375 +
36376 +out:
36377 +       return err;
36378 +}
36379 +
36380 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36381 +{
36382 +       au_kfree_try_rcu(wkinfo->hlock);
36383 +}
36384 +
36385 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36386 +{
36387 +       struct held_lock *p, **hl = wkinfo->hlock;
36388 +       int subclass;
36389 +
36390 +       if (wkinfo->dont_check)
36391 +               lockdep_off();
36392 +       if (!hl)
36393 +               return;
36394 +       while ((p = *hl++)) { /* assignment */
36395 +               subclass = lockdep_hlock_class(p)->subclass;
36396 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36397 +               if (p->read)
36398 +                       rwsem_acquire_read(p->instance, subclass, 0,
36399 +                                          /*p->acquire_ip*/_RET_IP_);
36400 +               else
36401 +                       rwsem_acquire(p->instance, subclass, 0,
36402 +                                     /*p->acquire_ip*/_RET_IP_);
36403 +       }
36404 +}
36405 +
36406 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36407 +{
36408 +       struct held_lock *p, **hl = wkinfo->hlock;
36409 +
36410 +       if (wkinfo->dont_check)
36411 +               lockdep_on();
36412 +       if (!hl)
36413 +               return;
36414 +       while ((p = *hl++)) /* assignment */
36415 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36416 +}
36417 +#endif
36418 +
36419 +static void wkq_func(struct work_struct *wk)
36420 +{
36421 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36422 +
36423 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36424 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36425 +
36426 +       au_wkq_lockdep_pre(wkinfo);
36427 +       wkinfo->func(wkinfo->args);
36428 +       au_wkq_lockdep_post(wkinfo);
36429 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36430 +               complete(wkinfo->comp);
36431 +       else {
36432 +               kobject_put(wkinfo->kobj);
36433 +               module_put(THIS_MODULE); /* todo: ?? */
36434 +               au_kfree_rcu(wkinfo);
36435 +       }
36436 +}
36437 +
36438 +/*
36439 + * Since struct completion is large, try allocating it dynamically.
36440 + */
36441 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36442 +
36443 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36444 +{
36445 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36446 +       if (*comp) {
36447 +               init_completion(*comp);
36448 +               wkinfo->comp = *comp;
36449 +               return 0;
36450 +       }
36451 +       return -ENOMEM;
36452 +}
36453 +
36454 +static void au_wkq_comp_free(struct completion *comp)
36455 +{
36456 +       au_kfree_rcu(comp);
36457 +}
36458 +
36459 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36460 +{
36461 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36462 +               if (au_wkq_test()) {
36463 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36464 +                               " due to a dead dir by UDBA,"
36465 +                               " or async xino write?\n");
36466 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36467 +               }
36468 +       } else
36469 +               au_dbg_verify_kthread();
36470 +
36471 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36472 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36473 +               queue_work(au_wkq, &wkinfo->wk);
36474 +       } else {
36475 +               INIT_WORK(&wkinfo->wk, wkq_func);
36476 +               schedule_work(&wkinfo->wk);
36477 +       }
36478 +}
36479 +
36480 +/*
36481 + * Be careful. It is easy to make deadlock happen.
36482 + * processA: lock, wkq and wait
36483 + * processB: wkq and wait, lock in wkq
36484 + * --> deadlock
36485 + */
36486 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36487 +{
36488 +       int err;
36489 +       AuWkqCompDeclare(comp);
36490 +       struct au_wkinfo wkinfo = {
36491 +               .flags  = flags,
36492 +               .func   = func,
36493 +               .args   = args
36494 +       };
36495 +
36496 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36497 +       if (unlikely(err))
36498 +               goto out;
36499 +       err = au_wkq_lockdep_alloc(&wkinfo);
36500 +       if (unlikely(err))
36501 +               goto out_comp;
36502 +       if (!err) {
36503 +               au_wkq_run(&wkinfo);
36504 +               /* no timeout, no interrupt */
36505 +               wait_for_completion(wkinfo.comp);
36506 +       }
36507 +       au_wkq_lockdep_free(&wkinfo);
36508 +
36509 +out_comp:
36510 +       au_wkq_comp_free(comp);
36511 +out:
36512 +       destroy_work_on_stack(&wkinfo.wk);
36513 +       return err;
36514 +}
36515 +
36516 +/*
36517 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36518 + * problem in a concurrent umounting.
36519 + */
36520 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36521 +                 unsigned int flags)
36522 +{
36523 +       int err;
36524 +       struct au_wkinfo *wkinfo;
36525 +
36526 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36527 +
36528 +       /*
36529 +        * wkq_func() must free this wkinfo.
36530 +        * it highly depends upon the implementation of workqueue.
36531 +        */
36532 +       err = 0;
36533 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36534 +       if (wkinfo) {
36535 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36536 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36537 +               wkinfo->func = func;
36538 +               wkinfo->args = args;
36539 +               wkinfo->comp = NULL;
36540 +               au_wkq_lockdep_init(wkinfo);
36541 +               kobject_get(wkinfo->kobj);
36542 +               __module_get(THIS_MODULE); /* todo: ?? */
36543 +
36544 +               au_wkq_run(wkinfo);
36545 +       } else {
36546 +               err = -ENOMEM;
36547 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36548 +       }
36549 +
36550 +       return err;
36551 +}
36552 +
36553 +/* ---------------------------------------------------------------------- */
36554 +
36555 +void au_nwt_init(struct au_nowait_tasks *nwt)
36556 +{
36557 +       atomic_set(&nwt->nw_len, 0);
36558 +       /* smp_mb(); */ /* atomic_set */
36559 +       init_waitqueue_head(&nwt->nw_wq);
36560 +}
36561 +
36562 +void au_wkq_fin(void)
36563 +{
36564 +       destroy_workqueue(au_wkq);
36565 +}
36566 +
36567 +int __init au_wkq_init(void)
36568 +{
36569 +       int err;
36570 +
36571 +       err = 0;
36572 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36573 +       if (IS_ERR(au_wkq))
36574 +               err = PTR_ERR(au_wkq);
36575 +       else if (!au_wkq)
36576 +               err = -ENOMEM;
36577 +
36578 +       return err;
36579 +}
36580 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36581 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36582 +++ linux/fs/aufs/wkq.h 2022-08-02 12:12:45.168975258 +0200
36583 @@ -0,0 +1,89 @@
36584 +/* SPDX-License-Identifier: GPL-2.0 */
36585 +/*
36586 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36587 + *
36588 + * This program, aufs is free software; you can redistribute it and/or modify
36589 + * it under the terms of the GNU General Public License as published by
36590 + * the Free Software Foundation; either version 2 of the License, or
36591 + * (at your option) any later version.
36592 + *
36593 + * This program is distributed in the hope that it will be useful,
36594 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36595 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36596 + * GNU General Public License for more details.
36597 + *
36598 + * You should have received a copy of the GNU General Public License
36599 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36600 + */
36601 +
36602 +/*
36603 + * workqueue for asynchronous/super-io operations
36604 + * todo: try new credentials management scheme
36605 + */
36606 +
36607 +#ifndef __AUFS_WKQ_H__
36608 +#define __AUFS_WKQ_H__
36609 +
36610 +#ifdef __KERNEL__
36611 +
36612 +#include <linux/wait.h>
36613 +
36614 +struct super_block;
36615 +
36616 +/* ---------------------------------------------------------------------- */
36617 +
36618 +/*
36619 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36620 + */
36621 +struct au_nowait_tasks {
36622 +       atomic_t                nw_len;
36623 +       wait_queue_head_t       nw_wq;
36624 +};
36625 +
36626 +/* ---------------------------------------------------------------------- */
36627 +
36628 +typedef void (*au_wkq_func_t)(void *args);
36629 +
36630 +/* wkq flags */
36631 +#define AuWkq_WAIT     1
36632 +#define AuWkq_NEST     (1 << 1)
36633 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36634 +#define au_fset_wkq(flags, name) \
36635 +       do { (flags) |= AuWkq_##name; } while (0)
36636 +#define au_fclr_wkq(flags, name) \
36637 +       do { (flags) &= ~AuWkq_##name; } while (0)
36638 +
36639 +/* wkq.c */
36640 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36641 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36642 +                 unsigned int flags);
36643 +void au_nwt_init(struct au_nowait_tasks *nwt);
36644 +int __init au_wkq_init(void);
36645 +void au_wkq_fin(void);
36646 +
36647 +/* ---------------------------------------------------------------------- */
36648 +
36649 +static inline int au_wkq_test(void)
36650 +{
36651 +       return current->flags & PF_WQ_WORKER;
36652 +}
36653 +
36654 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36655 +{
36656 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36657 +}
36658 +
36659 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36660 +{
36661 +       if (atomic_dec_and_test(&nwt->nw_len))
36662 +               wake_up_all(&nwt->nw_wq);
36663 +}
36664 +
36665 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36666 +{
36667 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36668 +       return 0;
36669 +}
36670 +
36671 +#endif /* __KERNEL__ */
36672 +#endif /* __AUFS_WKQ_H__ */
36673 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36674 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36675 +++ linux/fs/aufs/xattr.c       2022-08-02 12:12:45.168975258 +0200
36676 @@ -0,0 +1,368 @@
36677 +// SPDX-License-Identifier: GPL-2.0
36678 +/*
36679 + * Copyright (C) 2014-2021 Junjiro R. Okajima
36680 + *
36681 + * This program, aufs is free software; you can redistribute it and/or modify
36682 + * it under the terms of the GNU General Public License as published by
36683 + * the Free Software Foundation; either version 2 of the License, or
36684 + * (at your option) any later version.
36685 + *
36686 + * This program is distributed in the hope that it will be useful,
36687 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36688 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36689 + * GNU General Public License for more details.
36690 + *
36691 + * You should have received a copy of the GNU General Public License
36692 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36693 + */
36694 +
36695 +/*
36696 + * handling xattr functions
36697 + */
36698 +
36699 +#include <linux/fs.h>
36700 +#include <linux/posix_acl_xattr.h>
36701 +#include <linux/xattr.h>
36702 +#include "aufs.h"
36703 +
36704 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36705 +{
36706 +       if (!ignore_flags)
36707 +               goto out;
36708 +       switch (err) {
36709 +       case -ENOMEM:
36710 +       case -EDQUOT:
36711 +               goto out;
36712 +       }
36713 +
36714 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36715 +               err = 0;
36716 +               goto out;
36717 +       }
36718 +
36719 +#define cmp(brattr, prefix) do {                                       \
36720 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36721 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36722 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36723 +                               err = 0;                                \
36724 +                       goto out;                                       \
36725 +               }                                                       \
36726 +       } while (0)
36727 +
36728 +       cmp(SEC, SECURITY);
36729 +       cmp(SYS, SYSTEM);
36730 +       cmp(TR, TRUSTED);
36731 +       cmp(USR, USER);
36732 +#undef cmp
36733 +
36734 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36735 +               err = 0;
36736 +
36737 +out:
36738 +       return err;
36739 +}
36740 +
36741 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36742 +
36743 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36744 +                           char *name, char **buf, unsigned int ignore_flags,
36745 +                           unsigned int verbose)
36746 +{
36747 +       int err;
36748 +       ssize_t ssz;
36749 +       struct inode *h_idst;
36750 +       struct dentry *h_dst_dentry, *h_src_dentry;
36751 +       struct user_namespace *h_dst_userns, *h_src_userns;
36752 +
36753 +       h_src_userns = mnt_user_ns(h_src->mnt);
36754 +       h_src_dentry = h_src->dentry;
36755 +       ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36756 +                                GFP_NOFS);
36757 +       err = ssz;
36758 +       if (unlikely(err <= 0)) {
36759 +               if (err == -ENODATA
36760 +                   || (err == -EOPNOTSUPP
36761 +                       && ((ignore_flags & au_xattr_out_of_list)
36762 +                           || (au_test_nfs_noacl(d_inode(h_src_dentry))
36763 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36764 +                                   || !strcmp(name,
36765 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36766 +                           ))
36767 +                       err = 0;
36768 +               if (err && (verbose || au_debug_test()))
36769 +                       pr_err("%s, err %d\n", name, err);
36770 +               goto out;
36771 +       }
36772 +
36773 +       /* unlock it temporary */
36774 +       h_dst_userns = mnt_user_ns(h_dst->mnt);
36775 +       h_dst_dentry = h_dst->dentry;
36776 +       h_idst = d_inode(h_dst_dentry);
36777 +       inode_unlock(h_idst);
36778 +       err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36779 +                            /*flags*/0);
36780 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36781 +       if (unlikely(err)) {
36782 +               if (verbose || au_debug_test())
36783 +                       pr_err("%s, err %d\n", name, err);
36784 +               err = au_xattr_ignore(err, name, ignore_flags);
36785 +       }
36786 +
36787 +out:
36788 +       return err;
36789 +}
36790 +
36791 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36792 +                 unsigned int verbose)
36793 +{
36794 +       int err, unlocked, acl_access, acl_default;
36795 +       ssize_t ssz;
36796 +       struct dentry *h_dst_dentry, *h_src_dentry;
36797 +       struct inode *h_isrc, *h_idst;
36798 +       char *value, *p, *o, *e;
36799 +
36800 +       /* try stopping to update the source inode while we are referencing */
36801 +       /* there should not be the parent-child relationship between them */
36802 +       h_dst_dentry = h_dst->dentry;
36803 +       h_idst = d_inode(h_dst_dentry);
36804 +       h_src_dentry = h_src->dentry;
36805 +       h_isrc = d_inode(h_src_dentry);
36806 +       inode_unlock(h_idst);
36807 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36808 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36809 +       unlocked = 0;
36810 +
36811 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36812 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36813 +       err = ssz;
36814 +       if (unlikely(err < 0)) {
36815 +               AuTraceErr(err);
36816 +               if (err == -ENODATA
36817 +                   || err == -EOPNOTSUPP)
36818 +                       err = 0;        /* ignore */
36819 +               goto out;
36820 +       }
36821 +
36822 +       err = 0;
36823 +       p = NULL;
36824 +       o = NULL;
36825 +       if (ssz) {
36826 +               err = -ENOMEM;
36827 +               p = kmalloc(ssz, GFP_NOFS);
36828 +               o = p;
36829 +               if (unlikely(!p))
36830 +                       goto out;
36831 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36832 +       }
36833 +       inode_unlock_shared(h_isrc);
36834 +       unlocked = 1;
36835 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36836 +       if (unlikely(err < 0))
36837 +               goto out_free;
36838 +
36839 +       err = 0;
36840 +       e = p + ssz;
36841 +       value = NULL;
36842 +       acl_access = 0;
36843 +       acl_default = 0;
36844 +       while (!err && p < e) {
36845 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36846 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36847 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36848 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36849 +                                       - 1);
36850 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36851 +                                      verbose);
36852 +               p += strlen(p) + 1;
36853 +       }
36854 +       AuTraceErr(err);
36855 +       ignore_flags |= au_xattr_out_of_list;
36856 +       if (!err && !acl_access) {
36857 +               err = au_do_cpup_xattr(h_dst, h_src,
36858 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36859 +                                      ignore_flags, verbose);
36860 +               AuTraceErr(err);
36861 +       }
36862 +       if (!err && !acl_default) {
36863 +               err = au_do_cpup_xattr(h_dst, h_src,
36864 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36865 +                                      ignore_flags, verbose);
36866 +               AuTraceErr(err);
36867 +       }
36868 +
36869 +       au_kfree_try_rcu(value);
36870 +
36871 +out_free:
36872 +       au_kfree_try_rcu(o);
36873 +out:
36874 +       if (!unlocked)
36875 +               inode_unlock_shared(h_isrc);
36876 +       AuTraceErr(err);
36877 +       return err;
36878 +}
36879 +
36880 +/* ---------------------------------------------------------------------- */
36881 +
36882 +static int au_smack_reentering(struct super_block *sb)
36883 +{
36884 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36885 +       /*
36886 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36887 +        * i_op->getxattr(). ouch.
36888 +        */
36889 +       return si_pid_test(sb);
36890 +#else
36891 +       return 0;
36892 +#endif
36893 +}
36894 +
36895 +enum {
36896 +       AU_XATTR_LIST,
36897 +       AU_XATTR_GET
36898 +};
36899 +
36900 +struct au_lgxattr {
36901 +       int type;
36902 +       union {
36903 +               struct {
36904 +                       char    *list;
36905 +                       size_t  size;
36906 +               } list;
36907 +               struct {
36908 +                       const char      *name;
36909 +                       void            *value;
36910 +                       size_t          size;
36911 +               } get;
36912 +       } u;
36913 +};
36914 +
36915 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36916 +                         struct au_lgxattr *arg)
36917 +{
36918 +       ssize_t err;
36919 +       int reenter;
36920 +       struct path h_path;
36921 +       struct super_block *sb;
36922 +
36923 +       sb = dentry->d_sb;
36924 +       reenter = au_smack_reentering(sb);
36925 +       if (!reenter) {
36926 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36927 +               if (unlikely(err))
36928 +                       goto out;
36929 +       }
36930 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36931 +       if (unlikely(err))
36932 +               goto out_si;
36933 +       if (unlikely(!h_path.dentry))
36934 +               /* illegally overlapped or something */
36935 +               goto out_di; /* pretending success */
36936 +
36937 +       /* always topmost entry only */
36938 +       switch (arg->type) {
36939 +       case AU_XATTR_LIST:
36940 +               err = vfs_listxattr(h_path.dentry,
36941 +                                   arg->u.list.list, arg->u.list.size);
36942 +               break;
36943 +       case AU_XATTR_GET:
36944 +               AuDebugOn(d_is_negative(h_path.dentry));
36945 +               err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
36946 +                                  arg->u.get.name, arg->u.get.value,
36947 +                                  arg->u.get.size);
36948 +               break;
36949 +       }
36950 +
36951 +out_di:
36952 +       if (!reenter)
36953 +               di_read_unlock(dentry, AuLock_IR);
36954 +out_si:
36955 +       if (!reenter)
36956 +               si_read_unlock(sb);
36957 +out:
36958 +       AuTraceErr(err);
36959 +       return err;
36960 +}
36961 +
36962 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36963 +{
36964 +       struct au_lgxattr arg = {
36965 +               .type = AU_XATTR_LIST,
36966 +               .u.list = {
36967 +                       .list   = list,
36968 +                       .size   = size
36969 +               },
36970 +       };
36971 +
36972 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
36973 +}
36974 +
36975 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36976 +                          const char *name, void *value, size_t size)
36977 +{
36978 +       struct au_lgxattr arg = {
36979 +               .type = AU_XATTR_GET,
36980 +               .u.get = {
36981 +                       .name   = name,
36982 +                       .value  = value,
36983 +                       .size   = size
36984 +               },
36985 +       };
36986 +
36987 +       return au_lgxattr(dentry, inode, &arg);
36988 +}
36989 +
36990 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36991 +                      const char *name, const void *value, size_t size,
36992 +                      int flags)
36993 +{
36994 +       struct au_sxattr arg = {
36995 +               .type = AU_XATTR_SET,
36996 +               .u.set = {
36997 +                       .name   = name,
36998 +                       .value  = value,
36999 +                       .size   = size,
37000 +                       .flags  = flags
37001 +               },
37002 +       };
37003 +
37004 +       return au_sxattr(dentry, inode, &arg);
37005 +}
37006 +
37007 +/* ---------------------------------------------------------------------- */
37008 +
37009 +static int au_xattr_get(const struct xattr_handler *handler,
37010 +                       struct dentry *dentry, struct inode *inode,
37011 +                       const char *name, void *buffer, size_t size)
37012 +{
37013 +       return au_getxattr(dentry, inode, name, buffer, size);
37014 +}
37015 +
37016 +static int au_xattr_set(const struct xattr_handler *handler,
37017 +                       struct user_namespace *userns,
37018 +                       struct dentry *dentry, struct inode *inode,
37019 +                       const char *name, const void *value, size_t size,
37020 +                       int flags)
37021 +{
37022 +       return au_setxattr(dentry, inode, name, value, size, flags);
37023 +}
37024 +
37025 +static const struct xattr_handler au_xattr_handler = {
37026 +       .name   = "",
37027 +       .prefix = "",
37028 +       .get    = au_xattr_get,
37029 +       .set    = au_xattr_set
37030 +};
37031 +
37032 +static const struct xattr_handler *au_xattr_handlers[] = {
37033 +#ifdef CONFIG_FS_POSIX_ACL
37034 +       &posix_acl_access_xattr_handler,
37035 +       &posix_acl_default_xattr_handler,
37036 +#endif
37037 +       &au_xattr_handler, /* must be last */
37038 +       NULL
37039 +};
37040 +
37041 +void au_xattr_init(struct super_block *sb)
37042 +{
37043 +       sb->s_xattr = au_xattr_handlers;
37044 +}
37045 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
37046 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
37047 +++ linux/fs/aufs/xino.c        2022-08-02 12:12:45.168975258 +0200
37048 @@ -0,0 +1,1926 @@
37049 +// SPDX-License-Identifier: GPL-2.0
37050 +/*
37051 + * Copyright (C) 2005-2021 Junjiro R. Okajima
37052 + *
37053 + * This program, aufs is free software; you can redistribute it and/or modify
37054 + * it under the terms of the GNU General Public License as published by
37055 + * the Free Software Foundation; either version 2 of the License, or
37056 + * (at your option) any later version.
37057 + *
37058 + * This program is distributed in the hope that it will be useful,
37059 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37060 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37061 + * GNU General Public License for more details.
37062 + *
37063 + * You should have received a copy of the GNU General Public License
37064 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37065 + */
37066 +
37067 +/*
37068 + * external inode number translation table and bitmap
37069 + *
37070 + * things to consider
37071 + * - the lifetime
37072 + *   + au_xino object
37073 + *   + XINO files (xino, xib, xigen)
37074 + *   + dynamic debugfs entries (xiN)
37075 + *   + static debugfs entries (xib, xigen)
37076 + *   + static sysfs entry (xi_path)
37077 + * - several entry points to handle them.
37078 + *   + mount(2) without xino option (default)
37079 + *   + mount(2) with xino option
37080 + *   + mount(2) with noxino option
37081 + *   + umount(2)
37082 + *   + remount with add/del branches
37083 + *   + remount with xino/noxino options
37084 + */
37085 +
37086 +#include <linux/seq_file.h>
37087 +#include <linux/statfs.h>
37088 +#include "aufs.h"
37089 +
37090 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37091 +                                    aufs_bindex_t bbot,
37092 +                                    struct super_block *h_sb)
37093 +{
37094 +       /* todo: try binary-search if the branches are many */
37095 +       for (; btop <= bbot; btop++)
37096 +               if (h_sb == au_sbr_sb(sb, btop))
37097 +                       return btop;
37098 +       return -1;
37099 +}
37100 +
37101 +/*
37102 + * find another branch who is on the same filesystem of the specified
37103 + * branch{@btgt}. search until @bbot.
37104 + */
37105 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37106 +                                 aufs_bindex_t bbot)
37107 +{
37108 +       aufs_bindex_t bindex;
37109 +       struct super_block *tgt_sb;
37110 +
37111 +       tgt_sb = au_sbr_sb(sb, btgt);
37112 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37113 +       if (bindex < 0)
37114 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
37115 +
37116 +       return bindex;
37117 +}
37118 +
37119 +/* ---------------------------------------------------------------------- */
37120 +
37121 +/*
37122 + * stop unnecessary notify events at creating xino files
37123 + */
37124 +
37125 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37126 +{
37127 +       aufs_bindex_t bfound, bindex, bbot;
37128 +       struct dentry *parent;
37129 +       struct au_branch *br;
37130 +
37131 +       bfound = -1;
37132 +       parent = dentry->d_parent; /* safe d_parent access */
37133 +       bbot = au_sbbot(sb);
37134 +       for (bindex = 0; bindex <= bbot; bindex++) {
37135 +               br = au_sbr(sb, bindex);
37136 +               if (au_br_dentry(br) == parent) {
37137 +                       bfound = bindex;
37138 +                       break;
37139 +               }
37140 +       }
37141 +
37142 +       AuDbg("bfound b%d\n", bfound);
37143 +       return bfound;
37144 +}
37145 +
37146 +struct au_xino_lock_dir {
37147 +       struct au_hinode *hdir;
37148 +       struct dentry *parent;
37149 +       struct inode *dir;
37150 +};
37151 +
37152 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37153 +                                         unsigned int lsc)
37154 +{
37155 +       struct dentry *parent;
37156 +       struct inode *dir;
37157 +
37158 +       parent = dget_parent(dentry);
37159 +       dir = d_inode(parent);
37160 +       inode_lock_nested(dir, lsc);
37161 +#if 0 /* it should not happen */
37162 +       spin_lock(&dentry->d_lock);
37163 +       if (unlikely(dentry->d_parent != parent)) {
37164 +               spin_unlock(&dentry->d_lock);
37165 +               inode_unlock(dir);
37166 +               dput(parent);
37167 +               parent = NULL;
37168 +               goto out;
37169 +       }
37170 +       spin_unlock(&dentry->d_lock);
37171 +
37172 +out:
37173 +#endif
37174 +       return parent;
37175 +}
37176 +
37177 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37178 +                            struct au_xino_lock_dir *ldir)
37179 +{
37180 +       aufs_bindex_t bindex;
37181 +
37182 +       ldir->hdir = NULL;
37183 +       bindex = au_xi_root(sb, xipath->dentry);
37184 +       if (bindex >= 0) {
37185 +               /* rw branch root */
37186 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37187 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37188 +       } else {
37189 +               /* other */
37190 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37191 +                                                  AuLsc_I_PARENT);
37192 +               ldir->dir = d_inode(ldir->parent);
37193 +       }
37194 +}
37195 +
37196 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37197 +{
37198 +       if (ldir->hdir)
37199 +               au_hn_inode_unlock(ldir->hdir);
37200 +       else {
37201 +               inode_unlock(ldir->dir);
37202 +               dput(ldir->parent);
37203 +       }
37204 +}
37205 +
37206 +/* ---------------------------------------------------------------------- */
37207 +
37208 +/*
37209 + * create and set a new xino file
37210 + */
37211 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37212 +                           int wbrtop)
37213 +{
37214 +       struct file *file;
37215 +       struct dentry *h_parent, *d;
37216 +       struct inode *h_dir, *inode;
37217 +       int err;
37218 +       static DEFINE_MUTEX(mtx);
37219 +
37220 +       /*
37221 +        * at mount-time, and the xino file is the default path,
37222 +        * hnotify is disabled so we have no notify events to ignore.
37223 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37224 +        */
37225 +       if (!wbrtop)
37226 +               mutex_lock(&mtx);
37227 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37228 +                              /* | __FMODE_NONOTIFY */,
37229 +                              0666);
37230 +       if (IS_ERR(file)) {
37231 +               if (!wbrtop)
37232 +                       mutex_unlock(&mtx);
37233 +               if (!silent)
37234 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37235 +               return file;
37236 +       }
37237 +
37238 +       /* keep file count */
37239 +       err = 0;
37240 +       d = file->f_path.dentry;
37241 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37242 +       if (!wbrtop)
37243 +               mutex_unlock(&mtx);
37244 +       /* mnt_want_write() is unnecessary here */
37245 +       h_dir = d_inode(h_parent);
37246 +       inode = file_inode(file);
37247 +       /* no delegation since it is just created */
37248 +       if (inode->i_nlink)
37249 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37250 +                                  /*force*/0);
37251 +       inode_unlock(h_dir);
37252 +       dput(h_parent);
37253 +       if (unlikely(err)) {
37254 +               if (!silent)
37255 +                       pr_err("unlink %s(%d)\n", fpath, err);
37256 +               goto out;
37257 +       }
37258 +
37259 +       err = -EINVAL;
37260 +       if (unlikely(sb && sb == d->d_sb)) {
37261 +               if (!silent)
37262 +                       pr_err("%s must be outside\n", fpath);
37263 +               goto out;
37264 +       }
37265 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37266 +               if (!silent)
37267 +                       pr_err("xino doesn't support %s(%s)\n",
37268 +                              fpath, au_sbtype(d->d_sb));
37269 +               goto out;
37270 +       }
37271 +       return file; /* success */
37272 +
37273 +out:
37274 +       fput(file);
37275 +       file = ERR_PTR(err);
37276 +       return file;
37277 +}
37278 +
37279 +/*
37280 + * create a new xinofile at the same place/path as @base.
37281 + */
37282 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37283 +                            struct file *copy_src)
37284 +{
37285 +       struct file *file;
37286 +       struct dentry *dentry;
37287 +       struct inode *dir, *delegated;
37288 +       struct qstr *name;
37289 +       struct path ppath, path;
37290 +       int err, do_unlock;
37291 +       struct au_xino_lock_dir ldir;
37292 +
37293 +       do_unlock = 1;
37294 +       au_xino_lock_dir(sb, base, &ldir);
37295 +       dentry = base->dentry;
37296 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37297 +       ppath.mnt = base->mnt;
37298 +       dir = d_inode(ppath.dentry);
37299 +       IMustLock(dir);
37300 +
37301 +       name = &dentry->d_name;
37302 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37303 +       if (IS_ERR(path.dentry)) {
37304 +               file = (void *)path.dentry;
37305 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37306 +               goto out;
37307 +       }
37308 +
37309 +       /* no need to mnt_want_write() since we call dentry_open() later */
37310 +       err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
37311 +       if (unlikely(err)) {
37312 +               file = ERR_PTR(err);
37313 +               pr_err("%pd create err %d\n", dentry, err);
37314 +               goto out_dput;
37315 +       }
37316 +
37317 +       path.mnt = base->mnt;
37318 +       file = vfsub_dentry_open(&path,
37319 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37320 +                                /* | __FMODE_NONOTIFY */);
37321 +       if (IS_ERR(file)) {
37322 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37323 +               goto out_dput;
37324 +       }
37325 +
37326 +       delegated = NULL;
37327 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37328 +       au_xino_unlock_dir(&ldir);
37329 +       do_unlock = 0;
37330 +       if (unlikely(err == -EWOULDBLOCK)) {
37331 +               pr_warn("cannot retry for NFSv4 delegation"
37332 +                       " for an internal unlink\n");
37333 +               iput(delegated);
37334 +       }
37335 +       if (unlikely(err)) {
37336 +               pr_err("%pd unlink err %d\n", dentry, err);
37337 +               goto out_fput;
37338 +       }
37339 +
37340 +       if (copy_src) {
37341 +               /* no one can touch copy_src xino */
37342 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37343 +               if (unlikely(err)) {
37344 +                       pr_err("%pd copy err %d\n", dentry, err);
37345 +                       goto out_fput;
37346 +               }
37347 +       }
37348 +       goto out_dput; /* success */
37349 +
37350 +out_fput:
37351 +       fput(file);
37352 +       file = ERR_PTR(err);
37353 +out_dput:
37354 +       dput(path.dentry);
37355 +out:
37356 +       if (do_unlock)
37357 +               au_xino_unlock_dir(&ldir);
37358 +       return file;
37359 +}
37360 +
37361 +struct file *au_xino_file1(struct au_xino *xi)
37362 +{
37363 +       struct file *file;
37364 +       unsigned int u, nfile;
37365 +
37366 +       file = NULL;
37367 +       nfile = xi->xi_nfile;
37368 +       for (u = 0; u < nfile; u++) {
37369 +               file = xi->xi_file[u];
37370 +               if (file)
37371 +                       break;
37372 +       }
37373 +
37374 +       return file;
37375 +}
37376 +
37377 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37378 +{
37379 +       int err;
37380 +       struct file *f;
37381 +       void *p;
37382 +
37383 +       if (file)
37384 +               get_file(file);
37385 +
37386 +       err = 0;
37387 +       f = NULL;
37388 +       if (idx < xi->xi_nfile) {
37389 +               f = xi->xi_file[idx];
37390 +               if (f)
37391 +                       fput(f);
37392 +       } else {
37393 +               p = au_kzrealloc(xi->xi_file,
37394 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37395 +                                sizeof(*xi->xi_file) * (idx + 1),
37396 +                                GFP_NOFS, /*may_shrink*/0);
37397 +               if (p) {
37398 +                       MtxMustLock(&xi->xi_mtx);
37399 +                       xi->xi_file = p;
37400 +                       xi->xi_nfile = idx + 1;
37401 +               } else {
37402 +                       err = -ENOMEM;
37403 +                       if (file)
37404 +                               fput(file);
37405 +                       goto out;
37406 +               }
37407 +       }
37408 +       xi->xi_file[idx] = file;
37409 +
37410 +out:
37411 +       return err;
37412 +}
37413 +
37414 +/*
37415 + * if @xinew->xi is not set, then create new xigen file.
37416 + */
37417 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37418 +{
37419 +       struct file *file;
37420 +       int err;
37421 +
37422 +       SiMustAnyLock(sb);
37423 +
37424 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37425 +       if (IS_ERR(file)) {
37426 +               err = PTR_ERR(file);
37427 +               pr_err("%s[%d], err %d\n",
37428 +                      xinew->xi ? "xino" : "xigen",
37429 +                      xinew->idx, err);
37430 +               goto out;
37431 +       }
37432 +
37433 +       if (xinew->xi)
37434 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37435 +       else {
37436 +               BUG();
37437 +               /* todo: make xigen file an array */
37438 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37439 +       }
37440 +       fput(file);
37441 +       if (unlikely(err))
37442 +               file = ERR_PTR(err);
37443 +
37444 +out:
37445 +       return file;
37446 +}
37447 +
37448 +/* ---------------------------------------------------------------------- */
37449 +
37450 +/*
37451 + * truncate xino files
37452 + */
37453 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37454 +                           int idx, struct kstatfs *st)
37455 +{
37456 +       int err;
37457 +       blkcnt_t blocks;
37458 +       struct file *file, *new_xino;
37459 +       struct au_xi_new xinew = {
37460 +               .idx = idx
37461 +       };
37462 +
37463 +       err = 0;
37464 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37465 +       file = au_xino_file(xinew.xi, idx);
37466 +       if (!file)
37467 +               goto out;
37468 +
37469 +       xinew.base = &file->f_path;
37470 +       err = vfs_statfs(xinew.base, st);
37471 +       if (unlikely(err)) {
37472 +               AuErr1("statfs err %d, ignored\n", err);
37473 +               err = 0;
37474 +               goto out;
37475 +       }
37476 +
37477 +       blocks = file_inode(file)->i_blocks;
37478 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37479 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37480 +
37481 +       xinew.copy_src = file;
37482 +       new_xino = au_xi_new(sb, &xinew);
37483 +       if (IS_ERR(new_xino)) {
37484 +               err = PTR_ERR(new_xino);
37485 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37486 +               goto out;
37487 +       }
37488 +
37489 +       err = vfs_statfs(&new_xino->f_path, st);
37490 +       if (!err)
37491 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37492 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37493 +                       st->f_bfree, st->f_blocks);
37494 +       else {
37495 +               AuErr1("statfs err %d, ignored\n", err);
37496 +               err = 0;
37497 +       }
37498 +
37499 +out:
37500 +       return err;
37501 +}
37502 +
37503 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37504 +{
37505 +       int err, i;
37506 +       unsigned long jiffy;
37507 +       aufs_bindex_t bbot;
37508 +       struct kstatfs *st;
37509 +       struct au_branch *br;
37510 +       struct au_xino *xi;
37511 +
37512 +       err = -ENOMEM;
37513 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37514 +       if (unlikely(!st))
37515 +               goto out;
37516 +
37517 +       err = -EINVAL;
37518 +       bbot = au_sbbot(sb);
37519 +       if (unlikely(bindex < 0 || bbot < bindex))
37520 +               goto out_st;
37521 +
37522 +       err = 0;
37523 +       jiffy = jiffies;
37524 +       br = au_sbr(sb, bindex);
37525 +       xi = br->br_xino;
37526 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37527 +               err = au_xino_do_trunc(sb, bindex, i, st);
37528 +       if (!err)
37529 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37530 +
37531 +out_st:
37532 +       au_kfree_rcu(st);
37533 +out:
37534 +       return err;
37535 +}
37536 +
37537 +struct xino_do_trunc_args {
37538 +       struct super_block *sb;
37539 +       struct au_branch *br;
37540 +       int idx;
37541 +};
37542 +
37543 +static void xino_do_trunc(void *_args)
37544 +{
37545 +       struct xino_do_trunc_args *args = _args;
37546 +       struct super_block *sb;
37547 +       struct au_branch *br;
37548 +       struct inode *dir;
37549 +       int err, idx;
37550 +       aufs_bindex_t bindex;
37551 +
37552 +       err = 0;
37553 +       sb = args->sb;
37554 +       dir = d_inode(sb->s_root);
37555 +       br = args->br;
37556 +       idx = args->idx;
37557 +
37558 +       si_noflush_write_lock(sb);
37559 +       ii_read_lock_parent(dir);
37560 +       bindex = au_br_index(sb, br->br_id);
37561 +       err = au_xino_trunc(sb, bindex, idx);
37562 +       ii_read_unlock(dir);
37563 +       if (unlikely(err))
37564 +               pr_warn("err b%d, (%d)\n", bindex, err);
37565 +       atomic_dec(&br->br_xino->xi_truncating);
37566 +       au_lcnt_dec(&br->br_count);
37567 +       si_write_unlock(sb);
37568 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37569 +       au_kfree_rcu(args);
37570 +}
37571 +
37572 +/*
37573 + * returns the index in the xi_file array whose corresponding file is necessary
37574 + * to truncate, or -1 which means no need to truncate.
37575 + */
37576 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37577 +{
37578 +       int err;
37579 +       unsigned int u;
37580 +       struct kstatfs st;
37581 +       struct au_sbinfo *sbinfo;
37582 +       struct au_xino *xi;
37583 +       struct file *file;
37584 +
37585 +       /* todo: si_xino_expire and the ratio should be customizable */
37586 +       sbinfo = au_sbi(sb);
37587 +       if (time_before(jiffies,
37588 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37589 +               return -1;
37590 +
37591 +       /* truncation border */
37592 +       xi = br->br_xino;
37593 +       for (u = 0; u < xi->xi_nfile; u++) {
37594 +               file = au_xino_file(xi, u);
37595 +               if (!file)
37596 +                       continue;
37597 +
37598 +               err = vfs_statfs(&file->f_path, &st);
37599 +               if (unlikely(err)) {
37600 +                       AuErr1("statfs err %d, ignored\n", err);
37601 +                       return -1;
37602 +               }
37603 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37604 +                   >= AUFS_XINO_DEF_TRUNC)
37605 +                       return u;
37606 +       }
37607 +
37608 +       return -1;
37609 +}
37610 +
37611 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37612 +{
37613 +       int idx;
37614 +       struct xino_do_trunc_args *args;
37615 +       int wkq_err;
37616 +
37617 +       idx = xino_trunc_test(sb, br);
37618 +       if (idx < 0)
37619 +               return;
37620 +
37621 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37622 +               goto out;
37623 +
37624 +       /* lock and kfree() will be called in trunc_xino() */
37625 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37626 +       if (unlikely(!args)) {
37627 +               AuErr1("no memory\n");
37628 +               goto out;
37629 +       }
37630 +
37631 +       au_lcnt_inc(&br->br_count);
37632 +       args->sb = sb;
37633 +       args->br = br;
37634 +       args->idx = idx;
37635 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37636 +       if (!wkq_err)
37637 +               return; /* success */
37638 +
37639 +       pr_err("wkq %d\n", wkq_err);
37640 +       au_lcnt_dec(&br->br_count);
37641 +       au_kfree_rcu(args);
37642 +
37643 +out:
37644 +       atomic_dec(&br->br_xino->xi_truncating);
37645 +}
37646 +
37647 +/* ---------------------------------------------------------------------- */
37648 +
37649 +struct au_xi_calc {
37650 +       int idx;
37651 +       loff_t pos;
37652 +};
37653 +
37654 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37655 +                      struct au_xi_calc *calc)
37656 +{
37657 +       loff_t maxent;
37658 +
37659 +       maxent = au_xi_maxent(sb);
37660 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37661 +       calc->pos *= sizeof(ino_t);
37662 +}
37663 +
37664 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37665 +                               struct au_xi_calc *calc)
37666 +{
37667 +       int err;
37668 +       struct file *file;
37669 +       struct au_xino *xi = br->br_xino;
37670 +       struct au_xi_new xinew = {
37671 +               .xi = xi
37672 +       };
37673 +
37674 +       SiMustAnyLock(sb);
37675 +
37676 +       err = 0;
37677 +       if (!xi)
37678 +               goto out;
37679 +
37680 +       mutex_lock(&xi->xi_mtx);
37681 +       file = au_xino_file(xi, calc->idx);
37682 +       if (file)
37683 +               goto out_mtx;
37684 +
37685 +       file = au_xino_file(xi, /*idx*/-1);
37686 +       AuDebugOn(!file);
37687 +       xinew.idx = calc->idx;
37688 +       xinew.base = &file->f_path;
37689 +       /* xinew.copy_src = NULL; */
37690 +       file = au_xi_new(sb, &xinew);
37691 +       if (IS_ERR(file))
37692 +               err = PTR_ERR(file);
37693 +
37694 +out_mtx:
37695 +       mutex_unlock(&xi->xi_mtx);
37696 +out:
37697 +       return err;
37698 +}
37699 +
37700 +struct au_xino_do_new_async_args {
37701 +       struct super_block *sb;
37702 +       struct au_branch *br;
37703 +       struct au_xi_calc calc;
37704 +       ino_t ino;
37705 +};
37706 +
37707 +struct au_xi_writing {
37708 +       struct hlist_bl_node node;
37709 +       ino_t h_ino, ino;
37710 +};
37711 +
37712 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37713 +                           ino_t ino);
37714 +
37715 +static void au_xino_call_do_new_async(void *args)
37716 +{
37717 +       struct au_xino_do_new_async_args *a = args;
37718 +       struct au_branch *br;
37719 +       struct super_block *sb;
37720 +       struct au_sbinfo *sbi;
37721 +       struct inode *root;
37722 +       struct file *file;
37723 +       struct au_xi_writing *del, *p;
37724 +       struct hlist_bl_head *hbl;
37725 +       struct hlist_bl_node *pos;
37726 +       int err;
37727 +
37728 +       br = a->br;
37729 +       sb = a->sb;
37730 +       sbi = au_sbi(sb);
37731 +       si_noflush_read_lock(sb);
37732 +       root = d_inode(sb->s_root);
37733 +       ii_read_lock_child(root);
37734 +       err = au_xino_do_new_async(sb, br, &a->calc);
37735 +       if (unlikely(err)) {
37736 +               AuIOErr("err %d\n", err);
37737 +               goto out;
37738 +       }
37739 +
37740 +       file = au_xino_file(br->br_xino, a->calc.idx);
37741 +       AuDebugOn(!file);
37742 +       err = au_xino_do_write(file, &a->calc, a->ino);
37743 +       if (unlikely(err)) {
37744 +               AuIOErr("err %d\n", err);
37745 +               goto out;
37746 +       }
37747 +
37748 +       del = NULL;
37749 +       hbl = &br->br_xino->xi_writing;
37750 +       hlist_bl_lock(hbl);
37751 +       au_hbl_for_each(pos, hbl) {
37752 +               p = container_of(pos, struct au_xi_writing, node);
37753 +               if (p->ino == a->ino) {
37754 +                       del = p;
37755 +                       hlist_bl_del(&p->node);
37756 +                       break;
37757 +               }
37758 +       }
37759 +       hlist_bl_unlock(hbl);
37760 +       au_kfree_rcu(del);
37761 +
37762 +out:
37763 +       au_lcnt_dec(&br->br_count);
37764 +       ii_read_unlock(root);
37765 +       si_read_unlock(sb);
37766 +       au_nwt_done(&sbi->si_nowait);
37767 +       au_kfree_rcu(a);
37768 +}
37769 +
37770 +/*
37771 + * create a new xino file asynchronously
37772 + */
37773 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37774 +                            struct au_xi_calc *calc, ino_t ino)
37775 +{
37776 +       int err;
37777 +       struct au_xino_do_new_async_args *arg;
37778 +
37779 +       err = -ENOMEM;
37780 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37781 +       if (unlikely(!arg))
37782 +               goto out;
37783 +
37784 +       arg->sb = sb;
37785 +       arg->br = br;
37786 +       arg->calc = *calc;
37787 +       arg->ino = ino;
37788 +       au_lcnt_inc(&br->br_count);
37789 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37790 +       if (unlikely(err)) {
37791 +               pr_err("wkq %d\n", err);
37792 +               au_lcnt_dec(&br->br_count);
37793 +               au_kfree_rcu(arg);
37794 +       }
37795 +
37796 +out:
37797 +       return err;
37798 +}
37799 +
37800 +/*
37801 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37802 + * at the position of @h_ino.
37803 + */
37804 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37805 +                ino_t *ino)
37806 +{
37807 +       int err;
37808 +       ssize_t sz;
37809 +       struct au_xi_calc calc;
37810 +       struct au_sbinfo *sbinfo;
37811 +       struct file *file;
37812 +       struct au_xino *xi;
37813 +       struct hlist_bl_head *hbl;
37814 +       struct hlist_bl_node *pos;
37815 +       struct au_xi_writing *p;
37816 +
37817 +       *ino = 0;
37818 +       if (!au_opt_test(au_mntflags(sb), XINO))
37819 +               return 0; /* no xino */
37820 +
37821 +       err = 0;
37822 +       au_xi_calc(sb, h_ino, &calc);
37823 +       xi = au_sbr(sb, bindex)->br_xino;
37824 +       file = au_xino_file(xi, calc.idx);
37825 +       if (!file) {
37826 +               hbl = &xi->xi_writing;
37827 +               hlist_bl_lock(hbl);
37828 +               au_hbl_for_each(pos, hbl) {
37829 +                       p = container_of(pos, struct au_xi_writing, node);
37830 +                       if (p->h_ino == h_ino) {
37831 +                               AuDbg("hi%llu, i%llu, found\n",
37832 +                                     (u64)p->h_ino, (u64)p->ino);
37833 +                               *ino = p->ino;
37834 +                               break;
37835 +                       }
37836 +               }
37837 +               hlist_bl_unlock(hbl);
37838 +               return 0;
37839 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37840 +               return 0; /* no xino */
37841 +
37842 +       sbinfo = au_sbi(sb);
37843 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37844 +       if (sz == sizeof(*ino))
37845 +               return 0; /* success */
37846 +
37847 +       err = sz;
37848 +       if (unlikely(sz >= 0)) {
37849 +               err = -EIO;
37850 +               AuIOErr("xino read error (%zd)\n", sz);
37851 +       }
37852 +       return err;
37853 +}
37854 +
37855 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37856 +                           ino_t ino)
37857 +{
37858 +       ssize_t sz;
37859 +
37860 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37861 +       if (sz == sizeof(ino))
37862 +               return 0; /* success */
37863 +
37864 +       AuIOErr("write failed (%zd)\n", sz);
37865 +       return -EIO;
37866 +}
37867 +
37868 +/*
37869 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37870 + * at the position of @h_ino.
37871 + * even if @ino is zero, it is written to the xinofile and means no entry.
37872 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37873 + * try truncating it.
37874 + */
37875 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37876 +                 ino_t ino)
37877 +{
37878 +       int err;
37879 +       unsigned int mnt_flags;
37880 +       struct au_xi_calc calc;
37881 +       struct file *file;
37882 +       struct au_branch *br;
37883 +       struct au_xino *xi;
37884 +       struct au_xi_writing *p;
37885 +
37886 +       SiMustAnyLock(sb);
37887 +
37888 +       mnt_flags = au_mntflags(sb);
37889 +       if (!au_opt_test(mnt_flags, XINO))
37890 +               return 0;
37891 +
37892 +       au_xi_calc(sb, h_ino, &calc);
37893 +       br = au_sbr(sb, bindex);
37894 +       xi = br->br_xino;
37895 +       file = au_xino_file(xi, calc.idx);
37896 +       if (!file) {
37897 +               /* store the inum pair into the list */
37898 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37899 +               p->h_ino = h_ino;
37900 +               p->ino = ino;
37901 +               au_hbl_add(&p->node, &xi->xi_writing);
37902 +
37903 +               /* create and write a new xino file asynchronously */
37904 +               err = au_xino_new_async(sb, br, &calc, ino);
37905 +               if (!err)
37906 +                       return 0; /* success */
37907 +               goto out;
37908 +       }
37909 +
37910 +       err = au_xino_do_write(file, &calc, ino);
37911 +       if (!err) {
37912 +               br = au_sbr(sb, bindex);
37913 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37914 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37915 +                       xino_try_trunc(sb, br);
37916 +               return 0; /* success */
37917 +       }
37918 +
37919 +out:
37920 +       AuIOErr("write failed (%d)\n", err);
37921 +       return -EIO;
37922 +}
37923 +
37924 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37925 +                             loff_t *pos);
37926 +
37927 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37928 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37929 +{
37930 +       ssize_t err;
37931 +       int i;
37932 +       const int prevent_endless = 10;
37933 +
37934 +       i = 0;
37935 +       do {
37936 +               err = vfsub_read_k(file, kbuf, size, pos);
37937 +               if (err == -EINTR
37938 +                   && !au_wkq_test()
37939 +                   && fatal_signal_pending(current)) {
37940 +                       err = xino_fread_wkq(file, kbuf, size, pos);
37941 +                       BUG_ON(err == -EINTR);
37942 +               }
37943 +       } while (i++ < prevent_endless
37944 +                && (err == -EAGAIN || err == -EINTR));
37945 +
37946 +#if 0 /* reserved for future use */
37947 +       if (err > 0)
37948 +               fsnotify_access(file->f_path.dentry);
37949 +#endif
37950 +
37951 +       return err;
37952 +}
37953 +
37954 +struct xino_fread_args {
37955 +       ssize_t *errp;
37956 +       struct file *file;
37957 +       void *buf;
37958 +       size_t size;
37959 +       loff_t *pos;
37960 +};
37961 +
37962 +static void call_xino_fread(void *args)
37963 +{
37964 +       struct xino_fread_args *a = args;
37965 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37966 +}
37967 +
37968 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37969 +                             loff_t *pos)
37970 +{
37971 +       ssize_t err;
37972 +       int wkq_err;
37973 +       struct xino_fread_args args = {
37974 +               .errp   = &err,
37975 +               .file   = file,
37976 +               .buf    = buf,
37977 +               .size   = size,
37978 +               .pos    = pos
37979 +       };
37980 +
37981 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37982 +       if (unlikely(wkq_err))
37983 +               err = wkq_err;
37984 +
37985 +       return err;
37986 +}
37987 +
37988 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37989 +                              loff_t *pos);
37990 +
37991 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37992 +                             loff_t *pos)
37993 +{
37994 +       ssize_t err;
37995 +       int i;
37996 +       const int prevent_endless = 10;
37997 +
37998 +       i = 0;
37999 +       do {
38000 +               err = vfsub_write_k(file, kbuf, size, pos);
38001 +               if (err == -EINTR
38002 +                   && !au_wkq_test()
38003 +                   && fatal_signal_pending(current)) {
38004 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
38005 +                       BUG_ON(err == -EINTR);
38006 +               }
38007 +       } while (i++ < prevent_endless
38008 +                && (err == -EAGAIN || err == -EINTR));
38009 +
38010 +#if 0 /* reserved for future use */
38011 +       if (err > 0)
38012 +               fsnotify_modify(file->f_path.dentry);
38013 +#endif
38014 +
38015 +       return err;
38016 +}
38017 +
38018 +struct do_xino_fwrite_args {
38019 +       ssize_t *errp;
38020 +       struct file *file;
38021 +       void *buf;
38022 +       size_t size;
38023 +       loff_t *pos;
38024 +};
38025 +
38026 +static void call_do_xino_fwrite(void *args)
38027 +{
38028 +       struct do_xino_fwrite_args *a = args;
38029 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
38030 +}
38031 +
38032 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38033 +                              loff_t *pos)
38034 +{
38035 +       ssize_t err;
38036 +       int wkq_err;
38037 +       struct do_xino_fwrite_args args = {
38038 +               .errp   = &err,
38039 +               .file   = file,
38040 +               .buf    = buf,
38041 +               .size   = size,
38042 +               .pos    = pos
38043 +       };
38044 +
38045 +       /*
38046 +        * it breaks RLIMIT_FSIZE and normal user's limit,
38047 +        * users should care about quota and real 'filesystem full.'
38048 +        */
38049 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38050 +       if (unlikely(wkq_err))
38051 +               err = wkq_err;
38052 +
38053 +       return err;
38054 +}
38055 +
38056 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
38057 +{
38058 +       ssize_t err;
38059 +
38060 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38061 +               lockdep_off();
38062 +               err = do_xino_fwrite(file, buf, size, pos);
38063 +               lockdep_on();
38064 +       } else {
38065 +               lockdep_off();
38066 +               err = xino_fwrite_wkq(file, buf, size, pos);
38067 +               lockdep_on();
38068 +       }
38069 +
38070 +       return err;
38071 +}
38072 +
38073 +/* ---------------------------------------------------------------------- */
38074 +
38075 +/*
38076 + * inode number bitmap
38077 + */
38078 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38079 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
38080 +{
38081 +       ino_t ino;
38082 +
38083 +       AuDebugOn(bit < 0 || page_bits <= bit);
38084 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38085 +       return ino;
38086 +}
38087 +
38088 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38089 +{
38090 +       AuDebugOn(ino < AUFS_FIRST_INO);
38091 +       ino -= AUFS_FIRST_INO;
38092 +       *pindex = ino / page_bits;
38093 +       *bit = ino % page_bits;
38094 +}
38095 +
38096 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
38097 +{
38098 +       int err;
38099 +       loff_t pos;
38100 +       ssize_t sz;
38101 +       struct au_sbinfo *sbinfo;
38102 +       struct file *xib;
38103 +       unsigned long *p;
38104 +
38105 +       sbinfo = au_sbi(sb);
38106 +       MtxMustLock(&sbinfo->si_xib_mtx);
38107 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38108 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38109 +
38110 +       if (pindex == sbinfo->si_xib_last_pindex)
38111 +               return 0;
38112 +
38113 +       xib = sbinfo->si_xib;
38114 +       p = sbinfo->si_xib_buf;
38115 +       pos = sbinfo->si_xib_last_pindex;
38116 +       pos *= PAGE_SIZE;
38117 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38118 +       if (unlikely(sz != PAGE_SIZE))
38119 +               goto out;
38120 +
38121 +       pos = pindex;
38122 +       pos *= PAGE_SIZE;
38123 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38124 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
38125 +       else {
38126 +               memset(p, 0, PAGE_SIZE);
38127 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38128 +       }
38129 +       if (sz == PAGE_SIZE) {
38130 +               sbinfo->si_xib_last_pindex = pindex;
38131 +               return 0; /* success */
38132 +       }
38133 +
38134 +out:
38135 +       AuIOErr1("write failed (%zd)\n", sz);
38136 +       err = sz;
38137 +       if (sz >= 0)
38138 +               err = -EIO;
38139 +       return err;
38140 +}
38141 +
38142 +static void au_xib_clear_bit(struct inode *inode)
38143 +{
38144 +       int err, bit;
38145 +       unsigned long pindex;
38146 +       struct super_block *sb;
38147 +       struct au_sbinfo *sbinfo;
38148 +
38149 +       AuDebugOn(inode->i_nlink);
38150 +
38151 +       sb = inode->i_sb;
38152 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38153 +       AuDebugOn(page_bits <= bit);
38154 +       sbinfo = au_sbi(sb);
38155 +       mutex_lock(&sbinfo->si_xib_mtx);
38156 +       err = xib_pindex(sb, pindex);
38157 +       if (!err) {
38158 +               clear_bit(bit, sbinfo->si_xib_buf);
38159 +               sbinfo->si_xib_next_bit = bit;
38160 +       }
38161 +       mutex_unlock(&sbinfo->si_xib_mtx);
38162 +}
38163 +
38164 +/* ---------------------------------------------------------------------- */
38165 +
38166 +/*
38167 + * truncate a xino bitmap file
38168 + */
38169 +
38170 +/* todo: slow */
38171 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38172 +{
38173 +       int err, bit;
38174 +       ssize_t sz;
38175 +       unsigned long pindex;
38176 +       loff_t pos, pend;
38177 +       struct au_sbinfo *sbinfo;
38178 +       ino_t *ino;
38179 +       unsigned long *p;
38180 +
38181 +       err = 0;
38182 +       sbinfo = au_sbi(sb);
38183 +       MtxMustLock(&sbinfo->si_xib_mtx);
38184 +       p = sbinfo->si_xib_buf;
38185 +       pend = vfsub_f_size_read(file);
38186 +       pos = 0;
38187 +       while (pos < pend) {
38188 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
38189 +               err = sz;
38190 +               if (unlikely(sz <= 0))
38191 +                       goto out;
38192 +
38193 +               err = 0;
38194 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38195 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38196 +                               continue;
38197 +
38198 +                       xib_calc_bit(*ino, &pindex, &bit);
38199 +                       AuDebugOn(page_bits <= bit);
38200 +                       err = xib_pindex(sb, pindex);
38201 +                       if (!err)
38202 +                               set_bit(bit, p);
38203 +                       else
38204 +                               goto out;
38205 +               }
38206 +       }
38207 +
38208 +out:
38209 +       return err;
38210 +}
38211 +
38212 +static int xib_restore(struct super_block *sb)
38213 +{
38214 +       int err, i;
38215 +       unsigned int nfile;
38216 +       aufs_bindex_t bindex, bbot;
38217 +       void *page;
38218 +       struct au_branch *br;
38219 +       struct au_xino *xi;
38220 +       struct file *file;
38221 +
38222 +       err = -ENOMEM;
38223 +       page = (void *)__get_free_page(GFP_NOFS);
38224 +       if (unlikely(!page))
38225 +               goto out;
38226 +
38227 +       err = 0;
38228 +       bbot = au_sbbot(sb);
38229 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38230 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38231 +                       br = au_sbr(sb, bindex);
38232 +                       xi = br->br_xino;
38233 +                       nfile = xi->xi_nfile;
38234 +                       for (i = 0; i < nfile; i++) {
38235 +                               file = au_xino_file(xi, i);
38236 +                               if (file)
38237 +                                       err = do_xib_restore(sb, file, page);
38238 +                       }
38239 +               } else
38240 +                       AuDbg("skip shared b%d\n", bindex);
38241 +       free_page((unsigned long)page);
38242 +
38243 +out:
38244 +       return err;
38245 +}
38246 +
38247 +int au_xib_trunc(struct super_block *sb)
38248 +{
38249 +       int err;
38250 +       ssize_t sz;
38251 +       loff_t pos;
38252 +       struct au_sbinfo *sbinfo;
38253 +       unsigned long *p;
38254 +       struct file *file;
38255 +
38256 +       SiMustWriteLock(sb);
38257 +
38258 +       err = 0;
38259 +       sbinfo = au_sbi(sb);
38260 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38261 +               goto out;
38262 +
38263 +       file = sbinfo->si_xib;
38264 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38265 +               goto out;
38266 +
38267 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38268 +       err = PTR_ERR(file);
38269 +       if (IS_ERR(file))
38270 +               goto out;
38271 +       fput(sbinfo->si_xib);
38272 +       sbinfo->si_xib = file;
38273 +
38274 +       p = sbinfo->si_xib_buf;
38275 +       memset(p, 0, PAGE_SIZE);
38276 +       pos = 0;
38277 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38278 +       if (unlikely(sz != PAGE_SIZE)) {
38279 +               err = sz;
38280 +               AuIOErr("err %d\n", err);
38281 +               if (sz >= 0)
38282 +                       err = -EIO;
38283 +               goto out;
38284 +       }
38285 +
38286 +       mutex_lock(&sbinfo->si_xib_mtx);
38287 +       /* mnt_want_write() is unnecessary here */
38288 +       err = xib_restore(sb);
38289 +       mutex_unlock(&sbinfo->si_xib_mtx);
38290 +
38291 +out:
38292 +       return err;
38293 +}
38294 +
38295 +/* ---------------------------------------------------------------------- */
38296 +
38297 +struct au_xino *au_xino_alloc(unsigned int nfile)
38298 +{
38299 +       struct au_xino *xi;
38300 +
38301 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38302 +       if (unlikely(!xi))
38303 +               goto out;
38304 +       xi->xi_nfile = nfile;
38305 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38306 +       if (unlikely(!xi->xi_file))
38307 +               goto out_free;
38308 +
38309 +       xi->xi_nondir.total = 8; /* initial size */
38310 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38311 +                                     GFP_NOFS);
38312 +       if (unlikely(!xi->xi_nondir.array))
38313 +               goto out_file;
38314 +
38315 +       spin_lock_init(&xi->xi_nondir.spin);
38316 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38317 +       mutex_init(&xi->xi_mtx);
38318 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38319 +       atomic_set(&xi->xi_truncating, 0);
38320 +       kref_init(&xi->xi_kref);
38321 +       goto out; /* success */
38322 +
38323 +out_file:
38324 +       au_kfree_try_rcu(xi->xi_file);
38325 +out_free:
38326 +       au_kfree_rcu(xi);
38327 +       xi = NULL;
38328 +out:
38329 +       return xi;
38330 +}
38331 +
38332 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38333 +{
38334 +       int err;
38335 +       struct au_xino *xi;
38336 +
38337 +       err = 0;
38338 +       xi = au_xino_alloc(idx + 1);
38339 +       if (unlikely(!xi)) {
38340 +               err = -ENOMEM;
38341 +               goto out;
38342 +       }
38343 +
38344 +       if (file)
38345 +               get_file(file);
38346 +       xi->xi_file[idx] = file;
38347 +       AuDebugOn(br->br_xino);
38348 +       br->br_xino = xi;
38349 +
38350 +out:
38351 +       return err;
38352 +}
38353 +
38354 +static void au_xino_release(struct kref *kref)
38355 +{
38356 +       struct au_xino *xi;
38357 +       int i;
38358 +       unsigned long ul;
38359 +       struct hlist_bl_head *hbl;
38360 +       struct hlist_bl_node *pos, *n;
38361 +       struct au_xi_writing *p;
38362 +
38363 +       xi = container_of(kref, struct au_xino, xi_kref);
38364 +       for (i = 0; i < xi->xi_nfile; i++)
38365 +               if (xi->xi_file[i])
38366 +                       fput(xi->xi_file[i]);
38367 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38368 +               AuDebugOn(xi->xi_nondir.array[i]);
38369 +       mutex_destroy(&xi->xi_mtx);
38370 +       hbl = &xi->xi_writing;
38371 +       ul = au_hbl_count(hbl);
38372 +       if (unlikely(ul)) {
38373 +               pr_warn("xi_writing %lu\n", ul);
38374 +               hlist_bl_lock(hbl);
38375 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38376 +                       hlist_bl_del(&p->node);
38377 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38378 +                       kfree(p);
38379 +               }
38380 +               hlist_bl_unlock(hbl);
38381 +       }
38382 +       au_kfree_try_rcu(xi->xi_file);
38383 +       au_kfree_try_rcu(xi->xi_nondir.array);
38384 +       au_kfree_rcu(xi);
38385 +}
38386 +
38387 +int au_xino_put(struct au_branch *br)
38388 +{
38389 +       int ret;
38390 +       struct au_xino *xi;
38391 +
38392 +       ret = 0;
38393 +       xi = br->br_xino;
38394 +       if (xi) {
38395 +               br->br_xino = NULL;
38396 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38397 +       }
38398 +
38399 +       return ret;
38400 +}
38401 +
38402 +/* ---------------------------------------------------------------------- */
38403 +
38404 +/*
38405 + * xino mount option handlers
38406 + */
38407 +
38408 +/* xino bitmap */
38409 +static void xino_clear_xib(struct super_block *sb)
38410 +{
38411 +       struct au_sbinfo *sbinfo;
38412 +
38413 +       SiMustWriteLock(sb);
38414 +
38415 +       sbinfo = au_sbi(sb);
38416 +       if (sbinfo->si_xib)
38417 +               fput(sbinfo->si_xib);
38418 +       sbinfo->si_xib = NULL;
38419 +       if (sbinfo->si_xib_buf)
38420 +               free_page((unsigned long)sbinfo->si_xib_buf);
38421 +       sbinfo->si_xib_buf = NULL;
38422 +}
38423 +
38424 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38425 +{
38426 +       int err;
38427 +       loff_t pos;
38428 +       struct au_sbinfo *sbinfo;
38429 +       struct file *file;
38430 +       struct super_block *xi_sb;
38431 +
38432 +       SiMustWriteLock(sb);
38433 +
38434 +       sbinfo = au_sbi(sb);
38435 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38436 +       err = PTR_ERR(file);
38437 +       if (IS_ERR(file))
38438 +               goto out;
38439 +       if (sbinfo->si_xib)
38440 +               fput(sbinfo->si_xib);
38441 +       sbinfo->si_xib = file;
38442 +       xi_sb = file_inode(file)->i_sb;
38443 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38444 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38445 +               err = -EIO;
38446 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38447 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38448 +               goto out_unset;
38449 +       }
38450 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38451 +
38452 +       err = -ENOMEM;
38453 +       if (!sbinfo->si_xib_buf)
38454 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38455 +       if (unlikely(!sbinfo->si_xib_buf))
38456 +               goto out_unset;
38457 +
38458 +       sbinfo->si_xib_last_pindex = 0;
38459 +       sbinfo->si_xib_next_bit = 0;
38460 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38461 +               pos = 0;
38462 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38463 +               if (unlikely(err != PAGE_SIZE))
38464 +                       goto out_free;
38465 +       }
38466 +       err = 0;
38467 +       goto out; /* success */
38468 +
38469 +out_free:
38470 +       if (sbinfo->si_xib_buf)
38471 +               free_page((unsigned long)sbinfo->si_xib_buf);
38472 +       sbinfo->si_xib_buf = NULL;
38473 +       if (err >= 0)
38474 +               err = -EIO;
38475 +out_unset:
38476 +       fput(sbinfo->si_xib);
38477 +       sbinfo->si_xib = NULL;
38478 +out:
38479 +       AuTraceErr(err);
38480 +       return err;
38481 +}
38482 +
38483 +/* xino for each branch */
38484 +static void xino_clear_br(struct super_block *sb)
38485 +{
38486 +       aufs_bindex_t bindex, bbot;
38487 +       struct au_branch *br;
38488 +
38489 +       bbot = au_sbbot(sb);
38490 +       for (bindex = 0; bindex <= bbot; bindex++) {
38491 +               br = au_sbr(sb, bindex);
38492 +               AuDebugOn(!br);
38493 +               au_xino_put(br);
38494 +       }
38495 +}
38496 +
38497 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38498 +                                 aufs_bindex_t bshared)
38499 +{
38500 +       struct au_branch *brshared;
38501 +
38502 +       brshared = au_sbr(sb, bshared);
38503 +       AuDebugOn(!brshared->br_xino);
38504 +       AuDebugOn(!brshared->br_xino->xi_file);
38505 +       if (br->br_xino != brshared->br_xino) {
38506 +               au_xino_get(brshared);
38507 +               au_xino_put(br);
38508 +               br->br_xino = brshared->br_xino;
38509 +       }
38510 +}
38511 +
38512 +struct au_xino_do_set_br {
38513 +       struct au_branch *br;
38514 +       ino_t h_ino;
38515 +       aufs_bindex_t bshared;
38516 +};
38517 +
38518 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38519 +                            struct au_xino_do_set_br *args)
38520 +{
38521 +       int err;
38522 +       struct au_xi_calc calc;
38523 +       struct file *file;
38524 +       struct au_branch *br;
38525 +       struct au_xi_new xinew = {
38526 +               .base = path
38527 +       };
38528 +
38529 +       br = args->br;
38530 +       xinew.xi = br->br_xino;
38531 +       au_xi_calc(sb, args->h_ino, &calc);
38532 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38533 +       if (args->bshared >= 0)
38534 +               /* shared xino */
38535 +               au_xino_set_br_shared(sb, br, args->bshared);
38536 +       else if (!xinew.xi) {
38537 +               /* new xino */
38538 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38539 +               if (unlikely(err))
38540 +                       goto out;
38541 +       }
38542 +
38543 +       /* force re-creating */
38544 +       xinew.xi = br->br_xino;
38545 +       xinew.idx = calc.idx;
38546 +       mutex_lock(&xinew.xi->xi_mtx);
38547 +       file = au_xi_new(sb, &xinew);
38548 +       mutex_unlock(&xinew.xi->xi_mtx);
38549 +       err = PTR_ERR(file);
38550 +       if (IS_ERR(file))
38551 +               goto out;
38552 +       AuDebugOn(!file);
38553 +
38554 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38555 +       if (unlikely(err))
38556 +               au_xino_put(br);
38557 +
38558 +out:
38559 +       AuTraceErr(err);
38560 +       return err;
38561 +}
38562 +
38563 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38564 +{
38565 +       int err;
38566 +       aufs_bindex_t bindex, bbot;
38567 +       struct au_xino_do_set_br args;
38568 +       struct inode *inode;
38569 +
38570 +       SiMustWriteLock(sb);
38571 +
38572 +       bbot = au_sbbot(sb);
38573 +       inode = d_inode(sb->s_root);
38574 +       for (bindex = 0; bindex <= bbot; bindex++) {
38575 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38576 +               args.br = au_sbr(sb, bindex);
38577 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38578 +               err = au_xino_do_set_br(sb, path, &args);
38579 +               if (unlikely(err))
38580 +                       break;
38581 +       }
38582 +
38583 +       AuTraceErr(err);
38584 +       return err;
38585 +}
38586 +
38587 +void au_xino_clr(struct super_block *sb)
38588 +{
38589 +       struct au_sbinfo *sbinfo;
38590 +
38591 +       au_xigen_clr(sb);
38592 +       xino_clear_xib(sb);
38593 +       xino_clear_br(sb);
38594 +       dbgaufs_brs_del(sb, 0);
38595 +       sbinfo = au_sbi(sb);
38596 +       /* lvalue, do not call au_mntflags() */
38597 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38598 +}
38599 +
38600 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38601 +{
38602 +       int err, skip;
38603 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38604 +       struct qstr *dname, *cur_name;
38605 +       struct file *cur_xino;
38606 +       struct au_sbinfo *sbinfo;
38607 +       struct path *path, *cur_path;
38608 +
38609 +       SiMustWriteLock(sb);
38610 +
38611 +       err = 0;
38612 +       sbinfo = au_sbi(sb);
38613 +       path = &xiopt->file->f_path;
38614 +       dentry = path->dentry;
38615 +       parent = dget_parent(dentry);
38616 +       if (remount) {
38617 +               skip = 0;
38618 +               cur_xino = sbinfo->si_xib;
38619 +               if (cur_xino) {
38620 +                       cur_path = &cur_xino->f_path;
38621 +                       cur_dentry = cur_path->dentry;
38622 +                       cur_parent = dget_parent(cur_dentry);
38623 +                       cur_name = &cur_dentry->d_name;
38624 +                       dname = &dentry->d_name;
38625 +                       skip = (cur_parent == parent
38626 +                               && au_qstreq(dname, cur_name));
38627 +                       dput(cur_parent);
38628 +               }
38629 +               if (skip)
38630 +                       goto out;
38631 +       }
38632 +
38633 +       au_opt_set(sbinfo->si_mntflags, XINO);
38634 +       err = au_xino_set_xib(sb, path);
38635 +       /* si_x{read,write} are set */
38636 +       if (!err)
38637 +               err = au_xigen_set(sb, path);
38638 +       if (!err)
38639 +               err = au_xino_set_br(sb, path);
38640 +       if (!err) {
38641 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38642 +               goto out; /* success */
38643 +       }
38644 +
38645 +       /* reset all */
38646 +       AuIOErr("failed setting xino(%d).\n", err);
38647 +       au_xino_clr(sb);
38648 +
38649 +out:
38650 +       dput(parent);
38651 +       return err;
38652 +}
38653 +
38654 +/*
38655 + * create a xinofile at the default place/path.
38656 + */
38657 +struct file *au_xino_def(struct super_block *sb)
38658 +{
38659 +       struct file *file;
38660 +       char *page, *p;
38661 +       struct au_branch *br;
38662 +       struct super_block *h_sb;
38663 +       struct path path;
38664 +       aufs_bindex_t bbot, bindex, bwr;
38665 +
38666 +       br = NULL;
38667 +       bbot = au_sbbot(sb);
38668 +       bwr = -1;
38669 +       for (bindex = 0; bindex <= bbot; bindex++) {
38670 +               br = au_sbr(sb, bindex);
38671 +               if (au_br_writable(br->br_perm)
38672 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38673 +                       bwr = bindex;
38674 +                       break;
38675 +               }
38676 +       }
38677 +
38678 +       if (bwr >= 0) {
38679 +               file = ERR_PTR(-ENOMEM);
38680 +               page = (void *)__get_free_page(GFP_NOFS);
38681 +               if (unlikely(!page))
38682 +                       goto out;
38683 +               path.mnt = au_br_mnt(br);
38684 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38685 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38686 +               file = (void *)p;
38687 +               if (!IS_ERR(p)) {
38688 +                       strcat(p, "/" AUFS_XINO_FNAME);
38689 +                       AuDbg("%s\n", p);
38690 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38691 +               }
38692 +               free_page((unsigned long)page);
38693 +       } else {
38694 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38695 +                                     /*wbrtop*/0);
38696 +               if (IS_ERR(file))
38697 +                       goto out;
38698 +               h_sb = file->f_path.dentry->d_sb;
38699 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38700 +                       pr_err("xino doesn't support %s(%s)\n",
38701 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38702 +                       fput(file);
38703 +                       file = ERR_PTR(-EINVAL);
38704 +               }
38705 +       }
38706 +
38707 +out:
38708 +       return file;
38709 +}
38710 +
38711 +/* ---------------------------------------------------------------------- */
38712 +
38713 +/*
38714 + * initialize the xinofile for the specified branch @br
38715 + * at the place/path where @base_file indicates.
38716 + * test whether another branch is on the same filesystem or not,
38717 + * if found then share the xinofile with another branch.
38718 + */
38719 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38720 +                   struct path *base)
38721 +{
38722 +       int err;
38723 +       struct au_xino_do_set_br args = {
38724 +               .h_ino  = h_ino,
38725 +               .br     = br
38726 +       };
38727 +
38728 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38729 +                                      au_br_sb(br));
38730 +       err = au_xino_do_set_br(sb, base, &args);
38731 +       if (unlikely(err))
38732 +               au_xino_put(br);
38733 +
38734 +       return err;
38735 +}
38736 +
38737 +/* ---------------------------------------------------------------------- */
38738 +
38739 +/*
38740 + * get an unused inode number from bitmap
38741 + */
38742 +ino_t au_xino_new_ino(struct super_block *sb)
38743 +{
38744 +       ino_t ino;
38745 +       unsigned long *p, pindex, ul, pend;
38746 +       struct au_sbinfo *sbinfo;
38747 +       struct file *file;
38748 +       int free_bit, err;
38749 +
38750 +       if (!au_opt_test(au_mntflags(sb), XINO))
38751 +               return iunique(sb, AUFS_FIRST_INO);
38752 +
38753 +       sbinfo = au_sbi(sb);
38754 +       mutex_lock(&sbinfo->si_xib_mtx);
38755 +       p = sbinfo->si_xib_buf;
38756 +       free_bit = sbinfo->si_xib_next_bit;
38757 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38758 +               goto out; /* success */
38759 +       free_bit = find_first_zero_bit(p, page_bits);
38760 +       if (free_bit < page_bits)
38761 +               goto out; /* success */
38762 +
38763 +       pindex = sbinfo->si_xib_last_pindex;
38764 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38765 +               err = xib_pindex(sb, ul);
38766 +               if (unlikely(err))
38767 +                       goto out_err;
38768 +               free_bit = find_first_zero_bit(p, page_bits);
38769 +               if (free_bit < page_bits)
38770 +                       goto out; /* success */
38771 +       }
38772 +
38773 +       file = sbinfo->si_xib;
38774 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38775 +       for (ul = pindex + 1; ul <= pend; ul++) {
38776 +               err = xib_pindex(sb, ul);
38777 +               if (unlikely(err))
38778 +                       goto out_err;
38779 +               free_bit = find_first_zero_bit(p, page_bits);
38780 +               if (free_bit < page_bits)
38781 +                       goto out; /* success */
38782 +       }
38783 +       BUG();
38784 +
38785 +out:
38786 +       set_bit(free_bit, p);
38787 +       sbinfo->si_xib_next_bit = free_bit + 1;
38788 +       pindex = sbinfo->si_xib_last_pindex;
38789 +       mutex_unlock(&sbinfo->si_xib_mtx);
38790 +       ino = xib_calc_ino(pindex, free_bit);
38791 +       AuDbg("i%lu\n", (unsigned long)ino);
38792 +       return ino;
38793 +out_err:
38794 +       mutex_unlock(&sbinfo->si_xib_mtx);
38795 +       AuDbg("i0\n");
38796 +       return 0;
38797 +}
38798 +
38799 +/* for s_op->delete_inode() */
38800 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38801 +{
38802 +       int err;
38803 +       unsigned int mnt_flags;
38804 +       aufs_bindex_t bindex, bbot, bi;
38805 +       unsigned char try_trunc;
38806 +       struct au_iinfo *iinfo;
38807 +       struct super_block *sb;
38808 +       struct au_hinode *hi;
38809 +       struct inode *h_inode;
38810 +       struct au_branch *br;
38811 +       struct au_xi_calc calc;
38812 +       struct file *file;
38813 +
38814 +       AuDebugOn(au_is_bad_inode(inode));
38815 +
38816 +       sb = inode->i_sb;
38817 +       mnt_flags = au_mntflags(sb);
38818 +       if (!au_opt_test(mnt_flags, XINO)
38819 +           || inode->i_ino == AUFS_ROOT_INO)
38820 +               return;
38821 +
38822 +       if (unlinked) {
38823 +               au_xigen_inc(inode);
38824 +               au_xib_clear_bit(inode);
38825 +       }
38826 +
38827 +       iinfo = au_ii(inode);
38828 +       bindex = iinfo->ii_btop;
38829 +       if (bindex < 0)
38830 +               return;
38831 +
38832 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38833 +       hi = au_hinode(iinfo, bindex);
38834 +       bbot = iinfo->ii_bbot;
38835 +       for (; bindex <= bbot; bindex++, hi++) {
38836 +               h_inode = hi->hi_inode;
38837 +               if (!h_inode
38838 +                   || (!unlinked && h_inode->i_nlink))
38839 +                       continue;
38840 +
38841 +               /* inode may not be revalidated */
38842 +               bi = au_br_index(sb, hi->hi_id);
38843 +               if (bi < 0)
38844 +                       continue;
38845 +
38846 +               br = au_sbr(sb, bi);
38847 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38848 +               file = au_xino_file(br->br_xino, calc.idx);
38849 +               if (IS_ERR_OR_NULL(file))
38850 +                       continue;
38851 +
38852 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38853 +               if (!err && try_trunc
38854 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38855 +                       xino_try_trunc(sb, br);
38856 +       }
38857 +}
38858 +
38859 +/* ---------------------------------------------------------------------- */
38860 +
38861 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38862 +{
38863 +       int found, total, i;
38864 +
38865 +       found = -1;
38866 +       total = xi->xi_nondir.total;
38867 +       for (i = 0; i < total; i++) {
38868 +               if (xi->xi_nondir.array[i] != h_ino)
38869 +                       continue;
38870 +               found = i;
38871 +               break;
38872 +       }
38873 +
38874 +       return found;
38875 +}
38876 +
38877 +static int au_xinondir_expand(struct au_xino *xi)
38878 +{
38879 +       int err, sz;
38880 +       ino_t *p;
38881 +
38882 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38883 +
38884 +       err = -ENOMEM;
38885 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38886 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38887 +               goto out;
38888 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38889 +                        /*may_shrink*/0);
38890 +       if (p) {
38891 +               xi->xi_nondir.array = p;
38892 +               xi->xi_nondir.total <<= 1;
38893 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38894 +               err = 0;
38895 +       }
38896 +
38897 +out:
38898 +       return err;
38899 +}
38900 +
38901 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38902 +                      ino_t h_ino, int idx)
38903 +{
38904 +       struct au_xino *xi;
38905 +
38906 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38907 +       xi = au_sbr(sb, bindex)->br_xino;
38908 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38909 +
38910 +       spin_lock(&xi->xi_nondir.spin);
38911 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38912 +       xi->xi_nondir.array[idx] = 0;
38913 +       spin_unlock(&xi->xi_nondir.spin);
38914 +       wake_up_all(&xi->xi_nondir.wqh);
38915 +}
38916 +
38917 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38918 +                     int *idx)
38919 +{
38920 +       int err, found, empty;
38921 +       struct au_xino *xi;
38922 +
38923 +       err = 0;
38924 +       *idx = -1;
38925 +       if (!au_opt_test(au_mntflags(sb), XINO))
38926 +               goto out; /* no xino */
38927 +
38928 +       xi = au_sbr(sb, bindex)->br_xino;
38929 +
38930 +again:
38931 +       spin_lock(&xi->xi_nondir.spin);
38932 +       found = au_xinondir_find(xi, h_ino);
38933 +       if (found == -1) {
38934 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38935 +               if (empty == -1) {
38936 +                       empty = xi->xi_nondir.total;
38937 +                       err = au_xinondir_expand(xi);
38938 +                       if (unlikely(err))
38939 +                               goto out_unlock;
38940 +               }
38941 +               xi->xi_nondir.array[empty] = h_ino;
38942 +               *idx = empty;
38943 +       } else {
38944 +               spin_unlock(&xi->xi_nondir.spin);
38945 +               wait_event(xi->xi_nondir.wqh,
38946 +                          xi->xi_nondir.array[found] != h_ino);
38947 +               goto again;
38948 +       }
38949 +
38950 +out_unlock:
38951 +       spin_unlock(&xi->xi_nondir.spin);
38952 +out:
38953 +       return err;
38954 +}
38955 +
38956 +/* ---------------------------------------------------------------------- */
38957 +
38958 +int au_xino_path(struct seq_file *seq, struct file *file)
38959 +{
38960 +       int err;
38961 +
38962 +       err = au_seq_path(seq, &file->f_path);
38963 +       if (unlikely(err))
38964 +               goto out;
38965 +
38966 +#define Deleted "\\040(deleted)"
38967 +       seq->count -= sizeof(Deleted) - 1;
38968 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38969 +                        sizeof(Deleted) - 1));
38970 +#undef Deleted
38971 +
38972 +out:
38973 +       return err;
38974 +}
38975 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38976 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38977 +++ linux/include/uapi/linux/aufs_type.h        2022-08-02 12:12:45.168975258 +0200
38978 @@ -0,0 +1,452 @@
38979 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38980 +/*
38981 + * Copyright (C) 2005-2021 Junjiro R. Okajima
38982 + *
38983 + * This program, aufs is free software; you can redistribute it and/or modify
38984 + * it under the terms of the GNU General Public License as published by
38985 + * the Free Software Foundation; either version 2 of the License, or
38986 + * (at your option) any later version.
38987 + *
38988 + * This program is distributed in the hope that it will be useful,
38989 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38990 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38991 + * GNU General Public License for more details.
38992 + *
38993 + * You should have received a copy of the GNU General Public License
38994 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38995 + */
38996 +
38997 +#ifndef __AUFS_TYPE_H__
38998 +#define __AUFS_TYPE_H__
38999 +
39000 +#define AUFS_NAME      "aufs"
39001 +
39002 +#ifdef __KERNEL__
39003 +/*
39004 + * define it before including all other headers.
39005 + * sched.h may use pr_* macros before defining "current", so define the
39006 + * no-current version first, and re-define later.
39007 + */
39008 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39009 +#include <linux/sched.h>
39010 +#undef pr_fmt
39011 +#define pr_fmt(fmt) \
39012 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39013 +               (int)sizeof(current->comm), current->comm, current->pid
39014 +#include <linux/limits.h>
39015 +#else
39016 +#include <stdint.h>
39017 +#include <sys/types.h>
39018 +#include <limits.h>
39019 +#endif /* __KERNEL__ */
39020 +
39021 +#define AUFS_VERSION   "5.x-rcN-20220711"
39022 +
39023 +/* todo? move this to linux-2.6.19/include/magic.h */
39024 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39025 +
39026 +/* ---------------------------------------------------------------------- */
39027 +
39028 +#ifdef __KERNEL__
39029 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
39030 +typedef int8_t aufs_bindex_t;
39031 +#define AUFS_BRANCH_MAX 127
39032 +#else
39033 +typedef int16_t aufs_bindex_t;
39034 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
39035 +#define AUFS_BRANCH_MAX 511
39036 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39037 +#define AUFS_BRANCH_MAX 1023
39038 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39039 +#define AUFS_BRANCH_MAX 32767
39040 +#endif
39041 +#endif
39042 +
39043 +#ifndef AUFS_BRANCH_MAX
39044 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39045 +#endif
39046 +#endif /* __KERNEL__ */
39047 +
39048 +/* ---------------------------------------------------------------------- */
39049 +
39050 +#define AUFS_FSTYPE            AUFS_NAME
39051 +
39052 +#define AUFS_ROOT_INO          2
39053 +#define AUFS_FIRST_INO         11
39054 +
39055 +#define AUFS_WH_PFX            ".wh."
39056 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
39057 +#define AUFS_WH_TMP_LEN                4
39058 +/* a limit for rmdir/rename a dir and copyup */
39059 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
39060 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
39061 +                               - 1                     /* dot */\
39062 +                               - AUFS_WH_TMP_LEN)      /* hex */
39063 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
39064 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
39065 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
39066 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
39067 +#define AUFS_DIRWH_DEF         3
39068 +#define AUFS_RDCACHE_DEF       10 /* seconds */
39069 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
39070 +#define AUFS_RDBLK_DEF         512 /* bytes */
39071 +#define AUFS_RDHASH_DEF                32
39072 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
39073 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
39074 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
39075 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
39076 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
39077 +
39078 +/* pseudo-link maintenace under /proc */
39079 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
39080 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
39081 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39082 +
39083 +/* dirren, renamed dir */
39084 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
39085 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
39086 +/* whiteouted doubly */
39087 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
39088 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39089 +
39090 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39091 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39092 +
39093 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39094 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39095 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39096 +
39097 +/* doubly whiteouted */
39098 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39099 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39100 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39101 +
39102 +/* branch permissions and attributes */
39103 +#define AUFS_BRPERM_RW         "rw"
39104 +#define AUFS_BRPERM_RO         "ro"
39105 +#define AUFS_BRPERM_RR         "rr"
39106 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39107 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39108 +#define AUFS_BRATTR_FHSM       "fhsm"
39109 +#define AUFS_BRATTR_UNPIN      "unpin"
39110 +#define AUFS_BRATTR_ICEX       "icex"
39111 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39112 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39113 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39114 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39115 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39116 +#define AUFS_BRRATTR_WH                "wh"
39117 +#define AUFS_BRWATTR_NLWH      "nolwh"
39118 +#define AUFS_BRWATTR_MOO       "moo"
39119 +
39120 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39121 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39122 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39123 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39124 +
39125 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39126 +#define AuBrAttr_COO_ALL       (1 << 4)
39127 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39128 +
39129 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39130 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39131 +                                                  branch. meaningless since
39132 +                                                  linux-3.18-rc1 */
39133 +
39134 +/* ignore error in copying XATTR */
39135 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39136 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39137 +#define AuBrAttr_ICEX_TR       (1 << 9)
39138 +#define AuBrAttr_ICEX_USR      (1 << 10)
39139 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39140 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39141 +                                | AuBrAttr_ICEX_SYS    \
39142 +                                | AuBrAttr_ICEX_TR     \
39143 +                                | AuBrAttr_ICEX_USR    \
39144 +                                | AuBrAttr_ICEX_OTH)
39145 +
39146 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39147 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39148 +
39149 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39150 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39151 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39152 +
39153 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39154 +
39155 +/* #warning test userspace */
39156 +#ifdef __KERNEL__
39157 +#ifndef CONFIG_AUFS_FHSM
39158 +#undef AuBrAttr_FHSM
39159 +#define AuBrAttr_FHSM          0
39160 +#endif
39161 +#ifndef CONFIG_AUFS_XATTR
39162 +#undef AuBrAttr_ICEX
39163 +#define AuBrAttr_ICEX          0
39164 +#undef AuBrAttr_ICEX_SEC
39165 +#define AuBrAttr_ICEX_SEC      0
39166 +#undef AuBrAttr_ICEX_SYS
39167 +#define AuBrAttr_ICEX_SYS      0
39168 +#undef AuBrAttr_ICEX_TR
39169 +#define AuBrAttr_ICEX_TR       0
39170 +#undef AuBrAttr_ICEX_USR
39171 +#define AuBrAttr_ICEX_USR      0
39172 +#undef AuBrAttr_ICEX_OTH
39173 +#define AuBrAttr_ICEX_OTH      0
39174 +#endif
39175 +#endif
39176 +
39177 +/* the longest combination */
39178 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39179 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39180 +                              "+" AUFS_BRATTR_COO_REG          \
39181 +                              "+" AUFS_BRATTR_FHSM             \
39182 +                              "+" AUFS_BRATTR_UNPIN            \
39183 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39184 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39185 +                              "+" AUFS_BRATTR_ICEX_USR         \
39186 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39187 +                              "+" AUFS_BRWATTR_NLWH)
39188 +
39189 +typedef struct {
39190 +       char a[AuBrPermStrSz];
39191 +} au_br_perm_str_t;
39192 +
39193 +static inline int au_br_writable(int brperm)
39194 +{
39195 +       return brperm & AuBrPerm_RW;
39196 +}
39197 +
39198 +static inline int au_br_whable(int brperm)
39199 +{
39200 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39201 +}
39202 +
39203 +static inline int au_br_wh_linkable(int brperm)
39204 +{
39205 +       return !(brperm & AuBrWAttr_NoLinkWH);
39206 +}
39207 +
39208 +static inline int au_br_cmoo(int brperm)
39209 +{
39210 +       return brperm & AuBrAttr_CMOO_Mask;
39211 +}
39212 +
39213 +static inline int au_br_fhsm(int brperm)
39214 +{
39215 +       return brperm & AuBrAttr_FHSM;
39216 +}
39217 +
39218 +/* ---------------------------------------------------------------------- */
39219 +
39220 +/* ioctl */
39221 +enum {
39222 +       /* readdir in userspace */
39223 +       AuCtl_RDU,
39224 +       AuCtl_RDU_INO,
39225 +
39226 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39227 +       AuCtl_IBUSY,    /* busy inode */
39228 +       AuCtl_MVDOWN,   /* move-down */
39229 +       AuCtl_BR,       /* info about branches */
39230 +       AuCtl_FHSM_FD   /* connection for fhsm */
39231 +};
39232 +
39233 +/* borrowed from linux/include/linux/kernel.h */
39234 +#ifndef ALIGN
39235 +#ifdef _GNU_SOURCE
39236 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39237 +#else
39238 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39239 +#endif
39240 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39241 +#endif
39242 +
39243 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39244 +#ifndef __aligned
39245 +#define __aligned(x)                   __attribute__((aligned(x)))
39246 +#endif
39247 +
39248 +#ifdef __KERNEL__
39249 +#ifndef __packed
39250 +#define __packed                       __attribute__((packed))
39251 +#endif
39252 +#endif
39253 +
39254 +struct au_rdu_cookie {
39255 +       uint64_t        h_pos;
39256 +       int16_t         bindex;
39257 +       uint8_t         flags;
39258 +       uint8_t         pad;
39259 +       uint32_t        generation;
39260 +} __aligned(8);
39261 +
39262 +struct au_rdu_ent {
39263 +       uint64_t        ino;
39264 +       int16_t         bindex;
39265 +       uint8_t         type;
39266 +       uint8_t         nlen;
39267 +       uint8_t         wh;
39268 +       char            name[];
39269 +} __aligned(8);
39270 +
39271 +static inline int au_rdu_len(int nlen)
39272 +{
39273 +       /* include the terminating NULL */
39274 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39275 +                    sizeof(uint64_t));
39276 +}
39277 +
39278 +union au_rdu_ent_ul {
39279 +       struct au_rdu_ent __user        *e;
39280 +       uint64_t                        ul;
39281 +};
39282 +
39283 +enum {
39284 +       AufsCtlRduV_SZ,
39285 +       AufsCtlRduV_End
39286 +};
39287 +
39288 +struct aufs_rdu {
39289 +       /* input */
39290 +       union {
39291 +               uint64_t        sz;     /* AuCtl_RDU */
39292 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39293 +       };
39294 +       union au_rdu_ent_ul     ent;
39295 +       uint16_t                verify[AufsCtlRduV_End];
39296 +
39297 +       /* input/output */
39298 +       uint32_t                blk;
39299 +
39300 +       /* output */
39301 +       union au_rdu_ent_ul     tail;
39302 +       /* number of entries which were added in a single call */
39303 +       uint64_t                rent;
39304 +       uint8_t                 full;
39305 +       uint8_t                 shwh;
39306 +
39307 +       struct au_rdu_cookie    cookie;
39308 +} __aligned(8);
39309 +
39310 +/* ---------------------------------------------------------------------- */
39311 +
39312 +/* dirren. the branch is identified by the filename who contains this */
39313 +struct au_drinfo {
39314 +       uint64_t ino;
39315 +       union {
39316 +               uint8_t oldnamelen;
39317 +               uint64_t _padding;
39318 +       };
39319 +       uint8_t oldname[];
39320 +} __aligned(8);
39321 +
39322 +struct au_drinfo_fdata {
39323 +       uint32_t magic;
39324 +       struct au_drinfo drinfo;
39325 +} __aligned(8);
39326 +
39327 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39328 +/* future */
39329 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39330 +
39331 +/* ---------------------------------------------------------------------- */
39332 +
39333 +struct aufs_wbr_fd {
39334 +       uint32_t        oflags;
39335 +       int16_t         brid;
39336 +} __aligned(8);
39337 +
39338 +/* ---------------------------------------------------------------------- */
39339 +
39340 +struct aufs_ibusy {
39341 +       uint64_t        ino, h_ino;
39342 +       int16_t         bindex;
39343 +} __aligned(8);
39344 +
39345 +/* ---------------------------------------------------------------------- */
39346 +
39347 +/* error code for move-down */
39348 +/* the actual message strings are implemented in aufs-util.git */
39349 +enum {
39350 +       EAU_MVDOWN_OPAQUE = 1,
39351 +       EAU_MVDOWN_WHITEOUT,
39352 +       EAU_MVDOWN_UPPER,
39353 +       EAU_MVDOWN_BOTTOM,
39354 +       EAU_MVDOWN_NOUPPER,
39355 +       EAU_MVDOWN_NOLOWERBR,
39356 +       EAU_Last
39357 +};
39358 +
39359 +/* flags for move-down */
39360 +#define AUFS_MVDOWN_DMSG       1
39361 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39362 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39363 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39364 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39365 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39366 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39367 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39368 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39369 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39370 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39371 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39372 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39373 +
39374 +/* index for move-down */
39375 +enum {
39376 +       AUFS_MVDOWN_UPPER,
39377 +       AUFS_MVDOWN_LOWER,
39378 +       AUFS_MVDOWN_NARRAY
39379 +};
39380 +
39381 +/*
39382 + * additional info of move-down
39383 + * number of free blocks and inodes.
39384 + * subset of struct kstatfs, but smaller and always 64bit.
39385 + */
39386 +struct aufs_stfs {
39387 +       uint64_t        f_blocks;
39388 +       uint64_t        f_bavail;
39389 +       uint64_t        f_files;
39390 +       uint64_t        f_ffree;
39391 +};
39392 +
39393 +struct aufs_stbr {
39394 +       int16_t                 brid;   /* optional input */
39395 +       int16_t                 bindex; /* output */
39396 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39397 +} __aligned(8);
39398 +
39399 +struct aufs_mvdown {
39400 +       uint32_t                flags;                  /* input/output */
39401 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39402 +       int8_t                  au_errno;               /* output */
39403 +} __aligned(8);
39404 +
39405 +/* ---------------------------------------------------------------------- */
39406 +
39407 +union aufs_brinfo {
39408 +       /* PATH_MAX may differ between kernel-space and user-space */
39409 +       char    _spacer[4096];
39410 +       struct {
39411 +               int16_t id;
39412 +               int     perm;
39413 +               char    path[];
39414 +       };
39415 +} __aligned(8);
39416 +
39417 +/* ---------------------------------------------------------------------- */
39418 +
39419 +#define AuCtlType              'A'
39420 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39421 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39422 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39423 +                                    struct aufs_wbr_fd)
39424 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39425 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39426 +                                     struct aufs_mvdown)
39427 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39428 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39429 +
39430 +#endif /* __AUFS_TYPE_H__ */
39431 SPDX-License-Identifier: GPL-2.0
39432 aufs5.x-rcN loopback patch
39433
39434 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39435 index a705173cf452..642a93f4fdee 100644
39436 --- a/drivers/block/loop.c
39437 +++ b/drivers/block/loop.c
39438 @@ -54,7 +54,7 @@ struct loop_device {
39439         int             lo_flags;
39440         char            lo_file_name[LO_NAME_SIZE];
39441  
39442 -       struct file *   lo_backing_file;
39443 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39444         struct block_device *lo_device;
39445  
39446         gfp_t           old_gfp_mask;
39447 @@ -510,6 +510,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39448                                 lo->use_dio);
39449  }
39450  
39451 +static struct file *loop_real_file(struct file *file)
39452 +{
39453 +       struct file *f = NULL;
39454 +
39455 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39456 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39457 +       return f;
39458 +}
39459 +
39460  static void loop_reread_partitions(struct loop_device *lo)
39461  {
39462         int rc;
39463 @@ -567,6 +576,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39464  {
39465         struct file *file = fget(arg);
39466         struct file *old_file;
39467 +       struct file *f, *virt_file = NULL, *old_virt_file;
39468         int error;
39469         bool partscan;
39470         bool is_loop;
39471 @@ -590,11 +600,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39472         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39473                 goto out_err;
39474  
39475 +       f = loop_real_file(file);
39476 +       if (f) {
39477 +               virt_file = file;
39478 +               file = f;
39479 +               get_file(file);
39480 +       }
39481 +
39482         error = loop_validate_file(file, bdev);
39483         if (error)
39484                 goto out_err;
39485  
39486         old_file = lo->lo_backing_file;
39487 +       old_virt_file = lo->lo_backing_virt_file;
39488  
39489         error = -EINVAL;
39490  
39491 @@ -607,6 +625,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39492         blk_mq_freeze_queue(lo->lo_queue);
39493         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39494         lo->lo_backing_file = file;
39495 +       lo->lo_backing_virt_file = virt_file;
39496         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39497         mapping_set_gfp_mask(file->f_mapping,
39498                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39499 @@ -629,6 +648,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39500          * dependency.
39501          */
39502         fput(old_file);
39503 +       if (old_virt_file)
39504 +               fput(old_virt_file);
39505         if (partscan)
39506                 loop_reread_partitions(lo);
39507  
39508 @@ -642,6 +663,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39509         loop_global_unlock(lo, is_loop);
39510  out_putf:
39511         fput(file);
39512 +       if (virt_file)
39513 +               fput(virt_file);
39514         goto done;
39515  }
39516  
39517 @@ -1008,6 +1031,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39518                           const struct loop_config *config)
39519  {
39520         struct file *file = fget(config->fd);
39521 +       struct file *f, *virt_file = NULL;
39522         struct inode *inode;
39523         struct address_space *mapping;
39524         int error;
39525 @@ -1026,6 +1050,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39526         /* suppress uevents while reconfiguring the device */
39527         dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 1);
39528  
39529 +       f = loop_real_file(file);
39530 +       if (f) {
39531 +               virt_file = file;
39532 +               file = f;
39533 +               get_file(file);
39534 +       }
39535 +
39536         /*
39537          * If we don't hold exclusive handle for the device, upgrade to it
39538          * here to avoid changing device under exclusive owner.
39539 @@ -1086,6 +1117,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39540         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39541         lo->lo_device = bdev;
39542         lo->lo_backing_file = file;
39543 +       lo->lo_backing_virt_file = virt_file;
39544         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39545         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39546  
39547 @@ -1141,6 +1173,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39548                 bd_abort_claiming(bdev, loop_configure);
39549  out_putf:
39550         fput(file);
39551 +       if (virt_file)
39552 +               fput(virt_file);
39553         /* This is safe: open() is still holding a reference. */
39554         module_put(THIS_MODULE);
39555         goto done;
39556 @@ -1149,6 +1183,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39557  static void __loop_clr_fd(struct loop_device *lo, bool release)
39558  {
39559         struct file *filp;
39560 +       struct file *virt_filp = lo->lo_backing_virt_file;
39561         gfp_t gfp = lo->old_gfp_mask;
39562  
39563         if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
39564 @@ -1165,6 +1200,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39565         spin_lock_irq(&lo->lo_lock);
39566         filp = lo->lo_backing_file;
39567         lo->lo_backing_file = NULL;
39568 +       lo->lo_backing_virt_file = NULL;
39569         spin_unlock_irq(&lo->lo_lock);
39570  
39571         lo->lo_device = NULL;
39572 @@ -1227,6 +1263,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39573          * fput can take open_mutex which is usually taken before lo_mutex.
39574          */
39575         fput(filp);
39576 +       if (virt_filp)
39577 +               fput(virt_filp);
39578  }
39579  
39580  static int loop_clr_fd(struct loop_device *lo)
39581 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39582 index 99ceca144044..e49dfe855032 100644
39583 --- a/fs/aufs/f_op.c
39584 +++ b/fs/aufs/f_op.c
39585 @@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39586         if (IS_ERR(h_file))
39587                 goto out;
39588  
39589 -       if (au_test_loopback_kthread()) {
39590 +       if (0 && au_test_loopback_kthread()) {
39591                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39592                 if (file->f_mapping != h_file->f_mapping) {
39593                         file->f_mapping = h_file->f_mapping;
39594 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39595 index 74347bd75b38..5ef888a1d53f 100644
39596 --- a/fs/aufs/loop.c
39597 +++ b/fs/aufs/loop.c
39598 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39599                 symbol_put(loop_backing_file);
39600         au_kfree_try_rcu(au_warn_loopback_array);
39601  }
39602 +
39603 +/* ---------------------------------------------------------------------- */
39604 +
39605 +/* support the loopback block device insude aufs */
39606 +
39607 +struct file *aufs_real_loop(struct file *file)
39608 +{
39609 +       struct file *f;
39610 +
39611 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39612 +       fi_read_lock(file);
39613 +       f = au_hf_top(file);
39614 +       fi_read_unlock(file);
39615 +       AuDebugOn(!f);
39616 +       return f;
39617 +}
39618 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39619 index 7293bee427f9..3345c098d0d4 100644
39620 --- a/fs/aufs/loop.h
39621 +++ b/fs/aufs/loop.h
39622 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39623  
39624  int au_loopback_init(void);
39625  void au_loopback_fin(void);
39626 +
39627 +struct file *aufs_real_loop(struct file *file);
39628  #else
39629  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39630  
39631 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39632  
39633  AuStubInt0(au_loopback_init, void)
39634  AuStubVoid(au_loopback_fin, void)
39635 +
39636 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39637  #endif /* BLK_DEV_LOOP */
39638  
39639  #endif /* __KERNEL__ */
39640 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39641 index 7d9bcc074c87..0eefe350c01c 100644
39642 --- a/fs/aufs/super.c
39643 +++ b/fs/aufs/super.c
39644 @@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39645         .show_options   = aufs_show_options,
39646         .statfs         = aufs_statfs,
39647         .put_super      = aufs_put_super,
39648 -       .sync_fs        = aufs_sync_fs
39649 +       .sync_fs        = aufs_sync_fs,
39650 +#ifdef CONFIG_AUFS_BDEV_LOOP
39651 +       .real_loop      = aufs_real_loop
39652 +#endif
39653  };
39654  
39655  /* ---------------------------------------------------------------------- */
39656 diff --git a/include/linux/fs.h b/include/linux/fs.h
39657 index f5aed17b2ce2..6b2d2256be5f 100644
39658 --- a/include/linux/fs.h
39659 +++ b/include/linux/fs.h
39660 @@ -2121,6 +2121,10 @@ struct super_operations {
39661                                   struct shrink_control *);
39662         long (*free_cached_objects)(struct super_block *,
39663                                     struct shrink_control *);
39664 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39665 +       /* and aufs */
39666 +       struct file *(*real_loop)(struct file *);
39667 +#endif
39668  };
39669  
39670  /*
This page took 4.532652 seconds and 3 git commands to generate.