]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs.patch
- 6.9.3
[packages/kernel.git] / kernel-aufs.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs6.x-rcN kbuild patch
3
4 diff --git a/fs/Kconfig b/fs/Kconfig
5 index a46b0cbc4d8f..d90a3b0dbc53 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -337,6 +337,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 6ecc9b0a53f2..255420c2676e 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -129,3 +129,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 aufs6.x-rcN base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index aa3b947fb080..fb7020b24f7c 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3396,6 +3396,19 @@ F:       kernel/audit*
33  F:     lib/*audit.c
34  K:     \baudit_[a-z_0-9]\+\b
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 BUS DRIVER
50  M:     Greg Kroah-Hartman <gregkh@linuxfoundation.org>
51  R:     Dave Ertman <david.m.ertman@intel.com>
52 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53 index 28a95fd366fe..6bd18fb1e53a 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -641,6 +641,26 @@ 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 +/* Just to make the compiler silence, declare it */
65 +struct file *loop_backing_file(struct super_block *sb);
66 +struct file *loop_backing_file(struct super_block *sb)
67 +{
68 +       struct file *ret;
69 +       struct loop_device *l;
70 +
71 +       ret = NULL;
72 +       if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
73 +               l = sb->s_bdev->bd_disk->private_data;
74 +               ret = l->lo_backing_file;
75 +       }
76 +       return ret;
77 +}
78 +EXPORT_SYMBOL_GPL(loop_backing_file);
79 +
80  /* loop sysfs attributes */
81  
82  static ssize_t loop_attr_show(struct device *dev, char *page,
83 diff --git a/fs/dcache.c b/fs/dcache.c
84 index 71a8e943a0fa..13afe5474f86 100644
85 --- a/fs/dcache.c
86 +++ b/fs/dcache.c
87 @@ -1211,6 +1211,9 @@ enum d_walk_ret {
88         D_WALK_SKIP,
89  };
90  
91 +/* AUFS calls d_walk(). Just to make the compiler silence, declare it */
92 +void d_walk(struct dentry *parent, void *data,
93 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
94  /**
95   * d_walk - walk the dentry tree
96   * @parent:    start of walk
97 @@ -1219,7 +1222,7 @@ enum d_walk_ret {
98   *
99   * The @enter() callbacks are called with d_lock held.
100   */
101 -static void d_walk(struct dentry *parent, void *data,
102 +void d_walk(struct dentry *parent, void *data,
103                    enum d_walk_ret (*enter)(void *, struct dentry *))
104  {
105         struct dentry *this_parent, *dentry;
106 diff --git a/fs/fcntl.c b/fs/fcntl.c
107 index 54cc85d3338e..5dcfe97a5ba5 100644
108 --- a/fs/fcntl.c
109 +++ b/fs/fcntl.c
110 @@ -35,7 +35,7 @@
111  
112  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
113  
114 -static int setfl(int fd, struct file * filp, unsigned int arg)
115 +int setfl(int fd, struct file * filp, unsigned int arg)
116  {
117         struct inode * inode = file_inode(filp);
118         int error = 0;
119 @@ -65,6 +65,8 @@ static int setfl(int fd, struct file * filp, unsigned int arg)
120  
121         if (filp->f_op->check_flags)
122                 error = filp->f_op->check_flags(arg);
123 +       if (!error && filp->f_op->setfl)
124 +               error = filp->f_op->setfl(filp, arg);
125         if (error)
126                 return error;
127  
128 diff --git a/fs/namespace.c b/fs/namespace.c
129 index 5a51315c6678..a4b6ce51848e 100644
130 --- a/fs/namespace.c
131 +++ b/fs/namespace.c
132 @@ -860,6 +860,12 @@ static inline int check_mnt(struct mount *mnt)
133         return mnt->mnt_ns == current->nsproxy->mnt_ns;
134  }
135  
136 +/* for aufs, CONFIG_AUFS_BR_FUSE */
137 +int is_current_mnt_ns(struct vfsmount *mnt)
138 +{
139 +       return check_mnt(real_mount(mnt));
140 +}
141 +
142  /*
143   * vfsmount lock must be held for write
144   */
145 diff --git a/fs/splice.c b/fs/splice.c
146 index 218e24b1ac40..7210ddc5aa81 100644
147 --- a/fs/splice.c
148 +++ b/fs/splice.c
149 @@ -933,7 +933,7 @@ static int warn_unsupported(struct file *file, const char *op)
150  /*
151   * Attempt to initiate a splice from pipe to file.
152   */
153 -static ssize_t do_splice_from(struct pipe_inode_info *pipe, struct file *out,
154 +ssize_t do_splice_from(struct pipe_inode_info *pipe, struct file *out,
155                               loff_t *ppos, size_t len, unsigned int flags)
156  {
157         if (unlikely(!out->f_op->splice_write))
158 diff --git a/include/linux/fs.h b/include/linux/fs.h
159 index 00fc429b0af0..b3a73aed62b8 100644
160 --- a/include/linux/fs.h
161 +++ b/include/linux/fs.h
162 @@ -1113,6 +1113,7 @@ extern void fasync_free(struct fasync_struct *);
163  /* can be called from interrupts */
164  extern void kill_fasync(struct fasync_struct **, int, int);
165  
166 +extern int setfl(int fd, struct file *filp, unsigned int arg);
167  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
168  extern int f_setown(struct file *filp, int who, int force);
169  extern void f_delown(struct file *filp);
170 @@ -2024,6 +2025,7 @@ struct file_operations {
171         int (*lock) (struct file *, int, struct file_lock *);
172         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
173         int (*check_flags)(int);
174 +       int (*setfl)(struct file *, unsigned long);
175         int (*flock) (struct file *, int, struct file_lock *);
176         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
177         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
178 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
179 index 08b0d1d9d78b..009f69c4e09d 100644
180 --- a/include/linux/lockdep.h
181 +++ b/include/linux/lockdep.h
182 @@ -192,6 +192,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
183         return lock->key == key;
184  }
185  
186 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
187 +
188  /*
189   * Acquire a lock.
190   *
191 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
192 index 8f882f5881e8..6b9808f09843 100644
193 --- a/include/linux/mnt_namespace.h
194 +++ b/include/linux/mnt_namespace.h
195 @@ -7,12 +7,15 @@ struct mnt_namespace;
196  struct fs_struct;
197  struct user_namespace;
198  struct ns_common;
199 +struct vfsmount;
200  
201  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
202                 struct user_namespace *, struct fs_struct *);
203  extern void put_mnt_ns(struct mnt_namespace *ns);
204  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
205  
206 +extern int is_current_mnt_ns(struct vfsmount *mnt);
207 +
208  extern const struct file_operations proc_mounts_operations;
209  extern const struct file_operations proc_mountinfo_operations;
210  extern const struct file_operations proc_mountstats_operations;
211 diff --git a/include/linux/splice.h b/include/linux/splice.h
212 index 9dec4861d09f..14583d846864 100644
213 --- a/include/linux/splice.h
214 +++ b/include/linux/splice.h
215 @@ -108,4 +108,7 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
216  
217  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
218  extern const struct pipe_buf_operations default_pipe_buf_ops;
219 +
220 +extern ssize_t do_splice_from(struct pipe_inode_info *pipe, struct file *out,
221 +                             loff_t *ppos, size_t len, unsigned int flags);
222  #endif
223 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
224 index 151bd3de5936..2223ee909c10 100644
225 --- a/kernel/locking/lockdep.c
226 +++ b/kernel/locking/lockdep.c
227 @@ -218,7 +218,7 @@ unsigned long max_lock_class_idx;
228  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
229  DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
230  
231 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
232 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
233  {
234         unsigned int class_idx = hlock->class_idx;
235  
236 @@ -239,6 +239,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
237          */
238         return lock_classes + class_idx;
239  }
240 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
241  
242  #ifdef CONFIG_LOCK_STAT
243  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
244 SPDX-License-Identifier: GPL-2.0
245 aufs6.x-rcN mmap patch
246
247 diff --git a/fs/proc/base.c b/fs/proc/base.c
248 index 18550c071d71..560be8fb4fca 100644
249 --- a/fs/proc/base.c
250 +++ b/fs/proc/base.c
251 @@ -2213,7 +2213,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
252         rc = -ENOENT;
253         vma = find_exact_vma(mm, vm_start, vm_end);
254         if (vma && vma->vm_file) {
255 -               *path = *file_user_path(vma->vm_file);
256 +               *path = vma_pr_or_file(vma)->f_path;
257                 path_get(path);
258                 rc = 0;
259         }
260 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
261 index c6e7ebc63756..d7ccfd909764 100644
262 --- a/fs/proc/nommu.c
263 +++ b/fs/proc/nommu.c
264 @@ -39,7 +39,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
265         file = region->vm_file;
266  
267         if (file) {
268 -               struct inode *inode = file_inode(region->vm_file);
269 +               struct inode *inode;
270 +
271 +               file = vmr_pr_or_file(region);
272 +               inode = file_inode(file);
273                 dev = inode->i_sb->s_dev;
274                 ino = inode->i_ino;
275         }
276 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
277 index 23fbab954c20..6faced8ef8fe 100644
278 --- a/fs/proc/task_mmu.c
279 +++ b/fs/proc/task_mmu.c
280 @@ -273,7 +273,8 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
281         const char *name = NULL;
282  
283         if (file) {
284 -               const struct inode *inode = file_user_inode(vma->vm_file);
285 +               const struct inode *inode
286 +                       = file_inode(file = vma_pr_or_file(vma));
287  
288                 dev = inode->i_sb->s_dev;
289                 ino = inode->i_ino;
290 @@ -2700,7 +2701,7 @@ static int show_numa_map(struct seq_file *m, void *v)
291         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
292         struct vm_area_struct *vma = v;
293         struct numa_maps *md = &numa_priv->md;
294 -       struct file *file = vma->vm_file;
295 +       struct file *file = vma_pr_or_file(vma);
296         struct mm_struct *mm = vma->vm_mm;
297         char buffer[64];
298         struct mempolicy *pol;
299 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
300 index bce674533000..b12b5a75c799 100644
301 --- a/fs/proc/task_nommu.c
302 +++ b/fs/proc/task_nommu.c
303 @@ -137,7 +137,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
304         file = vma->vm_file;
305  
306         if (file) {
307 -               struct inode *inode = file_inode(vma->vm_file);
308 +               struct inode *inode;
309 +
310 +               file = vma_pr_or_file(vma);
311 +               inode = file_inode(file);
312                 dev = inode->i_sb->s_dev;
313                 ino = inode->i_ino;
314                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
315 diff --git a/include/linux/mm.h b/include/linux/mm.h
316 index 0436b919f1c7..a04c7dde4bc2 100644
317 --- a/include/linux/mm.h
318 +++ b/include/linux/mm.h
319 @@ -2452,6 +2452,43 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
320  static inline struct vm_area_struct *vma_lookup(struct mm_struct *mm,
321                                                 unsigned long addr);
322  
323 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
324 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
325 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
326 +                                     int);
327 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
328 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
329 +
330 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
331 +                                                               __LINE__)
332 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
333 +                                                         __LINE__)
334 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
335 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
336 +
337 +#ifndef CONFIG_MMU
338 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
339 +extern void vmr_do_fput(struct vm_region *, const char[], int);
340 +
341 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
342 +                                                         __LINE__)
343 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
344 +#endif /* !CONFIG_MMU */
345 +
346 +#else
347 +
348 +#define vma_file_update_time(vma)      file_update_time((vma)->vm_file)
349 +#define vma_pr_or_file(vma)            (vma)->vm_file
350 +#define vma_get_file(vma)              get_file((vma)->vm_file)
351 +#define vma_fput(vma)                  fput((vma)->vm_file)
352 +
353 +#ifndef CONFIG_MMU
354 +#define vmr_pr_or_file(region)         (region)->vm_file
355 +#define vmr_fput(region)               fput((region)->vm_file)
356 +#endif /* !CONFIG_MMU */
357 +
358 +#endif /* CONFIG_AUFS_FS */
359 +
360  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
361                 void *buf, int len, unsigned int gup_flags);
362  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
363 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
364 index 5240bd7bca33..f91323e92ef9 100644
365 --- a/include/linux/mm_types.h
366 +++ b/include/linux/mm_types.h
367 @@ -555,6 +555,9 @@ struct vm_region {
368         unsigned long   vm_top;         /* region allocated to here */
369         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
370         struct file     *vm_file;       /* the backing file or NULL */
371 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
372 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
373 +#endif
374  
375         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
376         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
377 @@ -719,6 +722,9 @@ struct vm_area_struct {
378         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
379                                            units */
380         struct file * vm_file;          /* File we map to (can be NULL). */
381 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
382 +       struct file *vm_prfile;         /* shadow of vm_file */
383 +#endif
384         void * vm_private_data;         /* was vm_pte (shared mem) */
385  
386  #ifdef CONFIG_ANON_VMA_NAME
387 diff --git a/kernel/fork.c b/kernel/fork.c
388 index 39a5046c2f0b..478a22f5f367 100644
389 --- a/kernel/fork.c
390 +++ b/kernel/fork.c
391 @@ -718,7 +718,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
392                 if (file) {
393                         struct address_space *mapping = file->f_mapping;
394  
395 -                       get_file(file);
396 +                       vma_get_file(tmp);
397                         i_mmap_lock_write(mapping);
398                         if (vma_is_shared_maywrite(tmp))
399                                 mapping_allow_writable(mapping);
400 diff --git a/mm/Makefile b/mm/Makefile
401 index e4b5b75aaec9..2ec2544009fb 100644
402 --- a/mm/Makefile
403 +++ b/mm/Makefile
404 @@ -134,3 +134,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o
405  obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
406  obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
407  obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
408 +obj-y += prfile.o
409 diff --git a/mm/filemap.c b/mm/filemap.c
410 index 7437b2bd75c1..cecf1956f7a9 100644
411 --- a/mm/filemap.c
412 +++ b/mm/filemap.c
413 @@ -3631,7 +3631,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
414         vm_fault_t ret = VM_FAULT_LOCKED;
415  
416         sb_start_pagefault(mapping->host->i_sb);
417 -       file_update_time(vmf->vma->vm_file);
418 +       vma_file_update_time(vmf->vma);
419         folio_lock(folio);
420         if (folio->mapping != mapping) {
421                 folio_unlock(folio);
422 diff --git a/mm/mmap.c b/mm/mmap.c
423 index 6dbda99a47da..8610432aa357 100644
424 --- a/mm/mmap.c
425 +++ b/mm/mmap.c
426 @@ -140,7 +140,7 @@ static void remove_vma(struct vm_area_struct *vma, bool unreachable)
427         if (vma->vm_ops && vma->vm_ops->close)
428                 vma->vm_ops->close(vma);
429         if (vma->vm_file)
430 -               fput(vma->vm_file);
431 +               vma_fput(vma);
432         mpol_put(vma_policy(vma));
433         if (unreachable)
434                 __vm_area_free(vma);
435 @@ -557,7 +557,7 @@ static inline void vma_complete(struct vma_prepare *vp,
436                 if (vp->file) {
437                         uprobe_munmap(vp->remove, vp->remove->vm_start,
438                                       vp->remove->vm_end);
439 -                       fput(vp->file);
440 +                       vma_fput(vp->vma);
441                 }
442                 if (vp->remove->anon_vma)
443                         anon_vma_merge(vp->vma, vp->remove);
444 @@ -2361,7 +2361,7 @@ static int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
445                 goto out_free_mpol;
446  
447         if (new->vm_file)
448 -               get_file(new->vm_file);
449 +               vma_get_file(new);
450  
451         if (new->vm_ops && new->vm_ops->open)
452                 new->vm_ops->open(new);
453 @@ -2854,7 +2854,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
454                                  * and cause general protection fault
455                                  * ultimately.
456                                  */
457 -                               fput(vma->vm_file);
458 +                               vma_fput(vma);
459                                 vm_area_free(vma);
460                                 vma = merge;
461                                 /* Update vm_flags to pick up the change. */
462 @@ -2940,7 +2940,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
463  
464         if (file || vma->vm_file) {
465  unmap_and_free_vma:
466 -               fput(vma->vm_file);
467 +               vma_fput(vma);
468                 vma->vm_file = NULL;
469  
470                 vma_iter_set(&vmi, vma->vm_end);
471 @@ -3002,6 +3002,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
472         unsigned long populate = 0;
473         unsigned long ret = -EINVAL;
474         struct file *file;
475 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
476 +       struct file *prfile;
477 +#endif
478  
479         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
480                      current->comm, current->pid);
481 @@ -3060,10 +3063,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
482         if (vma->vm_flags & VM_LOCKED)
483                 flags |= MAP_LOCKED;
484  
485 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
486 +       vma_get_file(vma);
487 +       file = vma->vm_file;
488 +       prfile = vma->vm_prfile;
489 +       ret = do_mmap(vma->vm_file, start, size,
490 +                       prot, flags, /*vm_flags*/0, pgoff, &populate, NULL);
491 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
492 +               struct vm_area_struct *new_vma;
493 +
494 +               new_vma = find_vma(mm, ret);
495 +               if (!new_vma->vm_prfile)
496 +                       new_vma->vm_prfile = prfile;
497 +               if (prfile)
498 +                       get_file(prfile);
499 +       }
500 +       /*
501 +        * two fput()s instead of vma_fput(vma),
502 +        * coz vma may not be available anymore.
503 +        */
504 +       fput(file);
505 +       if (prfile)
506 +               fput(prfile);
507 +#else
508         file = get_file(vma->vm_file);
509         ret = do_mmap(vma->vm_file, start, size,
510                         prot, flags, 0, pgoff, &populate, NULL);
511         fput(file);
512 +#endif /* CONFIG_AUFS_FS */
513  out:
514         mmap_write_unlock(mm);
515         if (populate)
516 @@ -3406,7 +3433,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
517                 if (anon_vma_clone(new_vma, vma))
518                         goto out_free_mempol;
519                 if (new_vma->vm_file)
520 -                       get_file(new_vma->vm_file);
521 +                       vma_get_file(new_vma);
522                 if (new_vma->vm_ops && new_vma->vm_ops->open)
523                         new_vma->vm_ops->open(new_vma);
524                 if (vma_link(mm, new_vma))
525 @@ -3420,7 +3447,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
526                 new_vma->vm_ops->close(new_vma);
527  
528         if (new_vma->vm_file)
529 -               fput(new_vma->vm_file);
530 +               vma_fput(new_vma);
531  
532         unlink_anon_vmas(new_vma);
533  out_free_mempol:
534 diff --git a/mm/nommu.c b/mm/nommu.c
535 index 5ec8f44e7ce9..efda87a7f99a 100644
536 --- a/mm/nommu.c
537 +++ b/mm/nommu.c
538 @@ -521,7 +521,7 @@ static void __put_nommu_region(struct vm_region *region)
539                 up_write(&nommu_region_sem);
540  
541                 if (region->vm_file)
542 -                       fput(region->vm_file);
543 +                       vmr_fput(region);
544  
545                 /* IO memory and memory shared directly out of the pagecache
546                  * from ramfs/tmpfs mustn't be released here */
547 @@ -601,7 +601,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
548         if (vma->vm_ops && vma->vm_ops->close)
549                 vma->vm_ops->close(vma);
550         if (vma->vm_file)
551 -               fput(vma->vm_file);
552 +               vma_fput(vma);
553         put_nommu_region(vma->vm_region);
554         vm_area_free(vma);
555  }
556 @@ -1133,7 +1133,7 @@ unsigned long do_mmap(struct file *file,
557                                         goto error_just_free;
558                                 }
559                         }
560 -                       fput(region->vm_file);
561 +                       vmr_fput(region);
562                         kmem_cache_free(vm_region_jar, region);
563                         region = pregion;
564                         result = start;
565 @@ -1219,10 +1219,10 @@ unsigned long do_mmap(struct file *file,
566  error:
567         vma_iter_free(&vmi);
568         if (region->vm_file)
569 -               fput(region->vm_file);
570 +               vmr_fput(region);
571         kmem_cache_free(vm_region_jar, region);
572         if (vma->vm_file)
573 -               fput(vma->vm_file);
574 +               vma_fput(vma);
575         vm_area_free(vma);
576         return ret;
577  
578 diff --git a/mm/prfile.c b/mm/prfile.c
579 new file mode 100644
580 index 000000000000..8f820a235364
581 --- /dev/null
582 +++ b/mm/prfile.c
583 @@ -0,0 +1,86 @@
584 +// SPDX-License-Identifier: GPL-2.0
585 +/*
586 + * Mainly for aufs which mmap(2) different file and wants to print different
587 + * path in /proc/PID/maps.
588 + * Call these functions via macros defined in linux/mm.h.
589 + *
590 + * See Documentation/filesystems/aufs/design/06mmap.txt
591 + *
592 + * Copyright (c) 2014-2022 Junjro R. Okajima
593 + * Copyright (c) 2014 Ian Campbell
594 + */
595 +
596 +#include <linux/mm.h>
597 +#include <linux/file.h>
598 +#include <linux/fs.h>
599 +
600 +/* #define PRFILE_TRACE */
601 +static inline void prfile_trace(struct file *f, struct file *pr,
602 +                             const char func[], int line, const char func2[])
603 +{
604 +#ifdef PRFILE_TRACE
605 +       if (pr)
606 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
607 +#endif
608 +}
609 +
610 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
611 +                            int line)
612 +{
613 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
614 +
615 +       prfile_trace(f, pr, func, line, __func__);
616 +       file_update_time(f);
617 +       if (f && pr)
618 +               file_update_time(pr);
619 +}
620 +
621 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
622 +                              int line)
623 +{
624 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
625 +
626 +       prfile_trace(f, pr, func, line, __func__);
627 +       return (f && pr) ? pr : f;
628 +}
629 +
630 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
631 +{
632 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
633 +
634 +       prfile_trace(f, pr, func, line, __func__);
635 +       get_file(f);
636 +       if (f && pr)
637 +               get_file(pr);
638 +}
639 +
640 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
641 +{
642 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
643 +
644 +       prfile_trace(f, pr, func, line, __func__);
645 +       fput(f);
646 +       if (f && pr)
647 +               fput(pr);
648 +}
649 +
650 +#ifndef CONFIG_MMU
651 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
652 +                              int line)
653 +{
654 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
655 +
656 +       prfile_trace(f, pr, func, line, __func__);
657 +       return (f && pr) ? pr : f;
658 +}
659 +
660 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
661 +{
662 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
663 +
664 +       prfile_trace(f, pr, func, line, __func__);
665 +       fput(f);
666 +       if (f && pr)
667 +               fput(pr);
668 +}
669 +#endif /* !CONFIG_MMU */
670 SPDX-License-Identifier: GPL-2.0
671 aufs6.x-rcN standalone patch
672
673 diff --git a/fs/dcache.c b/fs/dcache.c
674 index 13afe5474f86..05d9dab666bb 100644
675 --- a/fs/dcache.c
676 +++ b/fs/dcache.c
677 @@ -1321,6 +1321,7 @@ void d_walk(struct dentry *parent, void *data,
678         seq = 1;
679         goto again;
680  }
681 +EXPORT_SYMBOL_GPL(d_walk);
682  
683  struct check_mount {
684         struct vfsmount *mnt;
685 @@ -2883,6 +2884,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
686  
687         write_sequnlock(&rename_lock);
688  }
689 +EXPORT_SYMBOL_GPL(d_exchange);
690  
691  /**
692   * d_ancestor - search for an ancestor
693 diff --git a/fs/exec.c b/fs/exec.c
694 index ff6f26671cfc..ea7aa14a730e 100644
695 --- a/fs/exec.c
696 +++ b/fs/exec.c
697 @@ -113,6 +113,7 @@ bool path_noexec(const struct path *path)
698         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
699                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
700  }
701 +EXPORT_SYMBOL_GPL(path_noexec);
702  
703  #ifdef CONFIG_USELIB
704  /*
705 diff --git a/fs/fcntl.c b/fs/fcntl.c
706 index 5dcfe97a5ba5..48adde670824 100644
707 --- a/fs/fcntl.c
708 +++ b/fs/fcntl.c
709 @@ -88,6 +88,7 @@ int setfl(int fd, struct file * filp, unsigned int arg)
710   out:
711         return error;
712  }
713 +EXPORT_SYMBOL_GPL(setfl);
714  
715  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
716                       int force)
717 diff --git a/fs/file_table.c b/fs/file_table.c
718 index 4f03beed4737..1f0527dad5e1 100644
719 --- a/fs/file_table.c
720 +++ b/fs/file_table.c
721 @@ -221,6 +221,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
722         }
723         return ERR_PTR(-ENFILE);
724  }
725 +EXPORT_SYMBOL_GPL(alloc_empty_file);
726  
727  /*
728   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
729 diff --git a/fs/namespace.c b/fs/namespace.c
730 index a4b6ce51848e..c7415f75ab4c 100644
731 --- a/fs/namespace.c
732 +++ b/fs/namespace.c
733 @@ -865,6 +865,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
734  {
735         return check_mnt(real_mount(mnt));
736  }
737 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
738  
739  /*
740   * vfsmount lock must be held for write
741 @@ -2053,6 +2054,7 @@ struct vfsmount *collect_mounts(const struct path *path)
742                 return ERR_CAST(tree);
743         return &tree->mnt;
744  }
745 +EXPORT_SYMBOL_GPL(collect_mounts);
746  
747  static void free_mnt_ns(struct mnt_namespace *);
748  static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
749 @@ -2083,6 +2085,7 @@ void drop_collected_mounts(struct vfsmount *mnt)
750         unlock_mount_hash();
751         namespace_unlock();
752  }
753 +EXPORT_SYMBOL_GPL(drop_collected_mounts);
754  
755  static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
756  {
757 @@ -2154,6 +2157,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
758         }
759         return 0;
760  }
761 +EXPORT_SYMBOL_GPL(iterate_mounts);
762  
763  static void lock_mnt_tree(struct mount *mnt)
764  {
765 diff --git a/fs/notify/group.c b/fs/notify/group.c
766 index 1de6631a3925..3008eb37a18d 100644
767 --- a/fs/notify/group.c
768 +++ b/fs/notify/group.c
769 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
770  {
771         refcount_inc(&group->refcnt);
772  }
773 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
774  
775  /*
776   * Drop a reference to a group.  Free it if it's through.
777 diff --git a/fs/open.c b/fs/open.c
778 index ee8460c83c77..cb296bf7770f 100644
779 --- a/fs/open.c
780 +++ b/fs/open.c
781 @@ -66,6 +66,7 @@ int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
782         inode_unlock(dentry->d_inode);
783         return ret;
784  }
785 +EXPORT_SYMBOL_GPL(do_truncate);
786  
787  long vfs_truncate(const struct path *path, loff_t length)
788  {
789 diff --git a/fs/read_write.c b/fs/read_write.c
790 index d4c036e82b6c..c61361ff5e67 100644
791 --- a/fs/read_write.c
792 +++ b/fs/read_write.c
793 @@ -483,6 +483,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
794         inc_syscr(current);
795         return ret;
796  }
797 +EXPORT_SYMBOL_GPL(vfs_read);
798  
799  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
800  {
801 @@ -598,6 +599,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
802         file_end_write(file);
803         return ret;
804  }
805 +EXPORT_SYMBOL_GPL(vfs_write);
806  
807  /* file_ppos returns &file->f_pos or NULL if file is stream */
808  static inline loff_t *file_ppos(struct file *file)
809 diff --git a/fs/splice.c b/fs/splice.c
810 index 7210ddc5aa81..9e83f46eacc8 100644
811 --- a/fs/splice.c
812 +++ b/fs/splice.c
813 @@ -940,6 +940,7 @@ ssize_t do_splice_from(struct pipe_inode_info *pipe, struct file *out,
814                 return warn_unsupported(out, "write");
815         return out->f_op->splice_write(pipe, out, ppos, len, flags);
816  }
817 +EXPORT_SYMBOL_GPL(do_splice_from);
818  
819  /*
820   * Indicate to the caller that there was a premature EOF when reading from the
821 diff --git a/fs/xattr.c b/fs/xattr.c
822 index f8b643f91a98..9a2d9b3aaf0a 100644
823 --- a/fs/xattr.c
824 +++ b/fs/xattr.c
825 @@ -405,6 +405,7 @@ vfs_getxattr_alloc(struct mnt_idmap *idmap, struct dentry *dentry,
826         *xattr_value = value;
827         return error;
828  }
829 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
830  
831  ssize_t
832  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
833 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
834 index 2223ee909c10..d0ac4fc69a31 100644
835 --- a/kernel/locking/lockdep.c
836 +++ b/kernel/locking/lockdep.c
837 @@ -239,6 +239,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
838          */
839         return lock_classes + class_idx;
840  }
841 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
842  #define hlock_class(hlock) lockdep_hlock_class(hlock)
843  
844  #ifdef CONFIG_LOCK_STAT
845 diff --git a/kernel/task_work.c b/kernel/task_work.c
846 index 95a7e1b7f1da..5053670775d3 100644
847 --- a/kernel/task_work.c
848 +++ b/kernel/task_work.c
849 @@ -183,3 +183,4 @@ void task_work_run(void)
850                 } while (work);
851         }
852  }
853 +EXPORT_SYMBOL_GPL(task_work_run);
854 diff --git a/security/security.c b/security/security.c
855 index 7e118858b545..4b5bbe8c6889 100644
856 --- a/security/security.c
857 +++ b/security/security.c
858 @@ -1840,6 +1840,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
859                 return 0;
860         return call_int_hook(path_rmdir, dir, dentry);
861  }
862 +EXPORT_SYMBOL_GPL(security_path_rmdir);
863  
864  /**
865   * security_path_unlink() - Check if removing a hard link is allowed
866 @@ -1875,6 +1876,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
867                 return 0;
868         return call_int_hook(path_symlink, dir, dentry, old_name);
869  }
870 +EXPORT_SYMBOL_GPL(security_path_symlink);
871  
872  /**
873   * security_path_link - Check if creating a hard link is allowed
874 @@ -1893,6 +1895,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
875                 return 0;
876         return call_int_hook(path_link, old_dentry, new_dir, new_dentry);
877  }
878 +EXPORT_SYMBOL_GPL(security_path_link);
879  
880  /**
881   * security_path_rename() - Check if renaming a file is allowed
882 @@ -1954,6 +1957,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
883                 return 0;
884         return call_int_hook(path_chmod, path, mode);
885  }
886 +EXPORT_SYMBOL_GPL(security_path_chmod);
887  
888  /**
889   * security_path_chown() - Check if changing the file's owner/group is allowed
890 @@ -1971,6 +1975,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
891                 return 0;
892         return call_int_hook(path_chown, path, uid, gid);
893  }
894 +EXPORT_SYMBOL_GPL(security_path_chown);
895  
896  /**
897   * security_path_chroot() - Check if changing the root directory is allowed
898 @@ -2215,6 +2220,7 @@ int security_inode_permission(struct inode *inode, int mask)
899                 return 0;
900         return call_int_hook(inode_permission, inode, mask);
901  }
902 +EXPORT_SYMBOL_GPL(security_inode_permission);
903  
904  /**
905   * security_inode_setattr() - Check if setting file attributes is allowed
906 @@ -2694,6 +2700,7 @@ int security_file_permission(struct file *file, int mask)
907  {
908         return call_int_hook(file_permission, file, mask);
909  }
910 +EXPORT_SYMBOL_GPL(security_file_permission);
911  
912  /**
913   * security_file_alloc() - Allocate and init a file's LSM blob
914 @@ -2996,6 +3003,7 @@ int security_file_truncate(struct file *file)
915  {
916         return call_int_hook(file_truncate, file);
917  }
918 +EXPORT_SYMBOL_GPL(security_file_truncate);
919  
920  /**
921   * security_task_alloc() - Allocate a task's LSM blob
922 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
923 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
924 +++ linux/Documentation/ABI/testing/debugfs-aufs        2024-05-13 17:13:00.614737564 +0200
925 @@ -0,0 +1,55 @@
926 +What:          /debug/aufs/si_<id>/
927 +Date:          March 2009
928 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
929 +Description:
930 +               Under /debug/aufs, a directory named si_<id> is created
931 +               per aufs mount, where <id> is a unique id generated
932 +               internally.
933 +
934 +What:          /debug/aufs/si_<id>/plink
935 +Date:          Apr 2013
936 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
937 +Description:
938 +               It has three lines and shows the information about the
939 +               pseudo-link. The first line is a single number
940 +               representing a number of buckets. The second line is a
941 +               number of pseudo-links per buckets (separated by a
942 +               blank). The last line is a single number representing a
943 +               total number of psedo-links.
944 +               When the aufs mount option 'noplink' is specified, it
945 +               will show "1\n0\n0\n".
946 +
947 +What:          /debug/aufs/si_<id>/xib
948 +Date:          March 2009
949 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
950 +Description:
951 +               It shows the consumed blocks by xib (External Inode Number
952 +               Bitmap), its block size and file size.
953 +               When the aufs mount option 'noxino' is specified, it
954 +               will be empty. About XINO files, see the aufs manual.
955 +
956 +What:          /debug/aufs/si_<id>/xi<branch-index>
957 +Date:          March 2009
958 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
959 +Description:
960 +               It shows the consumed blocks by xino (External Inode Number
961 +               Translation Table), its link count, block size and file
962 +               size.
963 +               Due to the file size limit, there may exist multiple
964 +               xino files per branch.  In this case, "-N" is added to
965 +               the filename and it corresponds to the index of the
966 +               internal xino array.  "-0" is omitted.
967 +               When the aufs mount option 'noxino' is specified, Those
968 +               entries won't exist.  About XINO files, see the aufs
969 +               manual.
970 +
971 +What:          /debug/aufs/si_<id>/xigen
972 +Date:          March 2009
973 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
974 +Description:
975 +               It shows the consumed blocks by xigen (External Inode
976 +               Generation Table), its block size and file size.
977 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
978 +               be created.
979 +               When the aufs mount option 'noxino' is specified, it
980 +               will be empty. About XINO files, see the aufs manual.
981 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
982 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
983 +++ linux/Documentation/ABI/testing/sysfs-aufs  2024-05-13 17:13:00.614737564 +0200
984 @@ -0,0 +1,31 @@
985 +What:          /sys/fs/aufs/si_<id>/
986 +Date:          March 2009
987 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
988 +Description:
989 +               Under /sys/fs/aufs, a directory named si_<id> is created
990 +               per aufs mount, where <id> is a unique id generated
991 +               internally.
992 +
993 +What:          /sys/fs/aufs/si_<id>/br<idx>
994 +Date:          March 2009
995 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
996 +Description:
997 +               It shows the abolute path of a member directory (which
998 +               is called branch) in aufs, and its permission.
999 +
1000 +What:          /sys/fs/aufs/si_<id>/brid<idx>
1001 +Date:          July 2013
1002 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1003 +Description:
1004 +               It shows the id of a member directory (which is called
1005 +               branch) in aufs.
1006 +
1007 +What:          /sys/fs/aufs/si_<id>/xi_path
1008 +Date:          March 2009
1009 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1010 +Description:
1011 +               It shows the abolute path of XINO (External Inode Number
1012 +               Bitmap, Translation Table and Generation Table) file
1013 +               even if it is the default path.
1014 +               When the aufs mount option 'noxino' is specified, it
1015 +               will be empty. About XINO files, see the aufs manual.
1016 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1017 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1018 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2024-05-13 17:13:00.614737564 +0200
1019 @@ -0,0 +1,171 @@
1020 +
1021 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1022 +#
1023 +# This program is free software; you can redistribute it and/or modify
1024 +# it under the terms of the GNU General Public License as published by
1025 +# the Free Software Foundation; either version 2 of the License, or
1026 +# (at your option) any later version.
1027 +#
1028 +# This program is distributed in the hope that it will be useful,
1029 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1030 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1031 +# GNU General Public License for more details.
1032 +#
1033 +# You should have received a copy of the GNU General Public License
1034 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1035 +
1036 +Introduction
1037 +----------------------------------------
1038 +
1039 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1040 +1. abbrev. for "advanced multi-layered unification filesystem".
1041 +2. abbrev. for "another unionfs".
1042 +3. abbrev. for "auf das" in German which means "on the" in English.
1043 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1044 +   But "Filesystem aufs Filesystem" is hard to understand.
1045 +4. abbrev. for "African Urban Fashion Show".
1046 +
1047 +AUFS is a filesystem with features:
1048 +- multi layered stackable unification filesystem, the member directory
1049 +  is called as a branch.
1050 +- branch permission and attribute, 'readonly', 'real-readonly',
1051 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1052 +  combination.
1053 +- internal "file copy-on-write".
1054 +- logical deletion, whiteout.
1055 +- dynamic branch manipulation, adding, deleting and changing permission.
1056 +- allow bypassing aufs, user's direct branch access.
1057 +- external inode number translation table and bitmap which maintains the
1058 +  persistent aufs inode number.
1059 +- seekable directory, including NFS readdir.
1060 +- file mapping, mmap and sharing pages.
1061 +- pseudo-link, hardlink over branches.
1062 +- loopback mounted filesystem as a branch.
1063 +- several policies to select one among multiple writable branches.
1064 +- revert a single systemcall when an error occurs in aufs.
1065 +- and more...
1066 +
1067 +
1068 +Multi Layered Stackable Unification Filesystem
1069 +----------------------------------------------------------------------
1070 +Most people already knows what it is.
1071 +It is a filesystem which unifies several directories and provides a
1072 +merged single directory. When users access a file, the access will be
1073 +passed/re-directed/converted (sorry, I am not sure which English word is
1074 +correct) to the real file on the member filesystem. The member
1075 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1076 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1077 +readonly branch is handled by creating 'whiteout' on the upper writable
1078 +branch.
1079 +
1080 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1081 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1082 +different approaches to implement the merged-view.
1083 +The former tries putting it into VFS, and the latter implements as a
1084 +separate filesystem.
1085 +(If I misunderstand about these implementations, please let me know and
1086 +I shall correct it. Because it is a long time ago when I read their
1087 +source files last time).
1088 +
1089 +UnionMount's approach will be able to small, but may be hard to share
1090 +branches between several UnionMount since the whiteout in it is
1091 +implemented in the inode on branch filesystem and always
1092 +shared. According to Bharata's post, readdir does not seems to be
1093 +finished yet.
1094 +There are several missing features known in this implementations such as
1095 +- for users, the inode number may change silently. eg. copy-up.
1096 +- link(2) may break by copy-up.
1097 +- read(2) may get an obsoleted filedata (fstat(2) too).
1098 +- fcntl(F_SETLK) may be broken by copy-up.
1099 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1100 +  open(O_RDWR).
1101 +
1102 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1103 +merged into mainline. This is another implementation of UnionMount as a
1104 +separated filesystem. All the limitations and known problems which
1105 +UnionMount are equally inherited to "overlay" filesystem.
1106 +
1107 +Unionfs has a longer history. When I started implementing a stackable
1108 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1109 +inode, dentry and file objects and they have an array pointing lower
1110 +same kind objects. After contributing many patches for Unionfs, I
1111 +re-started my project AUFS (Jun 2006).
1112 +
1113 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1114 +implemented my own ideas, approaches and enhancements and it became
1115 +totally different one.
1116 +
1117 +Comparing DM snapshot and fs based implementation
1118 +- the number of bytes to be copied between devices is much smaller.
1119 +- the type of filesystem must be one and only.
1120 +- the fs must be writable, no readonly fs, even for the lower original
1121 +  device. so the compression fs will not be usable. but if we use
1122 +  loopback mount, we may address this issue.
1123 +  for instance,
1124 +       mount /cdrom/squashfs.img /sq
1125 +       losetup /sq/ext2.img
1126 +       losetup /somewhere/cow
1127 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1128 +- it will be difficult (or needs more operations) to extract the
1129 +  difference between the original device and COW.
1130 +- DM snapshot-merge may help a lot when users try merging. in the
1131 +  fs-layer union, users will use rsync(1).
1132 +
1133 +You may want to read my old paper "Filesystems in LiveCD"
1134 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1135 +
1136 +
1137 +Several characters/aspects/persona of aufs
1138 +----------------------------------------------------------------------
1139 +
1140 +Aufs has several characters, aspects or persona.
1141 +1. a filesystem, callee of VFS helper
1142 +2. sub-VFS, caller of VFS helper for branches
1143 +3. a virtual filesystem which maintains persistent inode number
1144 +4. reader/writer of files on branches such like an application
1145 +
1146 +1. Callee of VFS Helper
1147 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1148 +unlink(2) from an application reaches sys_unlink() kernel function and
1149 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1150 +calls filesystem specific unlink operation. Actually aufs implements the
1151 +unlink operation but it behaves like a redirector.
1152 +
1153 +2. Caller of VFS Helper for Branches
1154 +aufs_unlink() passes the unlink request to the branch filesystem as if
1155 +it were called from VFS. So the called unlink operation of the branch
1156 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1157 +every necessary pre/post operation for the branch filesystem.
1158 +- acquire the lock for the parent dir on a branch
1159 +- lookup in a branch
1160 +- revalidate dentry on a branch
1161 +- mnt_want_write() for a branch
1162 +- vfs_unlink() for a branch
1163 +- mnt_drop_write() for a branch
1164 +- release the lock on a branch
1165 +
1166 +3. Persistent Inode Number
1167 +One of the most important issue for a filesystem is to maintain inode
1168 +numbers. This is particularly important to support exporting a
1169 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1170 +backend block device for its own. But some storage is necessary to
1171 +keep and maintain the inode numbers. It may be a large space and may not
1172 +suit to keep in memory. Aufs rents some space from its first writable
1173 +branch filesystem (by default) and creates file(s) on it. These files
1174 +are created by aufs internally and removed soon (currently) keeping
1175 +opened.
1176 +Note: Because these files are removed, they are totally gone after
1177 +      unmounting aufs. It means the inode numbers are not persistent
1178 +      across unmount or reboot. I have a plan to make them really
1179 +      persistent which will be important for aufs on NFS server.
1180 +
1181 +4. Read/Write Files Internally (copy-on-write)
1182 +Because a branch can be readonly, when you write a file on it, aufs will
1183 +"copy-up" it to the upper writable branch internally. And then write the
1184 +originally requested thing to the file. Generally kernel doesn't
1185 +open/read/write file actively. In aufs, even a single write may cause a
1186 +internal "file copy". This behaviour is very similar to cp(1) command.
1187 +
1188 +Some people may think it is better to pass such work to user space
1189 +helper, instead of doing in kernel space. Actually I am still thinking
1190 +about it. But currently I have implemented it in kernel space.
1191 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1192 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1193 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2024-05-13 17:13:00.614737564 +0200
1194 @@ -0,0 +1,258 @@
1195 +
1196 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1197 +#
1198 +# This program is free software; you can redistribute it and/or modify
1199 +# it under the terms of the GNU General Public License as published by
1200 +# the Free Software Foundation; either version 2 of the License, or
1201 +# (at your option) any later version.
1202 +#
1203 +# This program is distributed in the hope that it will be useful,
1204 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1205 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1206 +# GNU General Public License for more details.
1207 +#
1208 +# You should have received a copy of the GNU General Public License
1209 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1210 +
1211 +Basic Aufs Internal Structure
1212 +
1213 +Superblock/Inode/Dentry/File Objects
1214 +----------------------------------------------------------------------
1215 +As like an ordinary filesystem, aufs has its own
1216 +superblock/inode/dentry/file objects. All these objects have a
1217 +dynamically allocated array and store the same kind of pointers to the
1218 +lower filesystem, branch.
1219 +For example, when you build a union with one readwrite branch and one
1220 +readonly, mounted /au, /rw and /ro respectively.
1221 +- /au = /rw + /ro
1222 +- /ro/fileA exists but /rw/fileA
1223 +
1224 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1225 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1226 +- [0] = NULL (because /rw/fileA doesn't exist)
1227 +- [1] = /ro/fileA
1228 +
1229 +This style of an array is essentially same to the aufs
1230 +superblock/inode/dentry/file objects.
1231 +
1232 +Because aufs supports manipulating branches, ie. add/delete/change
1233 +branches dynamically, these objects has its own generation. When
1234 +branches are changed, the generation in aufs superblock is
1235 +incremented. And a generation in other object are compared when it is
1236 +accessed. When a generation in other objects are obsoleted, aufs
1237 +refreshes the internal array.
1238 +
1239 +
1240 +Superblock
1241 +----------------------------------------------------------------------
1242 +Additionally aufs superblock has some data for policies to select one
1243 +among multiple writable branches, XIB files, pseudo-links and kobject.
1244 +See below in detail.
1245 +About the policies which supports copy-down a directory, see
1246 +wbr_policy.txt too.
1247 +
1248 +
1249 +Branch and XINO(External Inode Number Translation Table)
1250 +----------------------------------------------------------------------
1251 +Every branch has its own xino (external inode number translation table)
1252 +file. The xino file is created and unlinked by aufs internally. When two
1253 +members of a union exist on the same filesystem, they share the single
1254 +xino file.
1255 +The struct of a xino file is simple, just a sequence of aufs inode
1256 +numbers which is indexed by the lower inode number.
1257 +In the above sample, assume the inode number of /ro/fileA is i111 and
1258 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1259 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1260 +
1261 +When the inode numbers are not contiguous, the xino file will be sparse
1262 +which has a hole in it and doesn't consume as much disk space as it
1263 +might appear. If your branch filesystem consumes disk space for such
1264 +holes, then you should specify 'xino=' option at mounting aufs.
1265 +
1266 +Aufs has a mount option to free the disk blocks for such holes in XINO
1267 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1268 +meet a problem of disk shortage due to XINO files, then you should try
1269 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1270 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1271 +the holes in XINO files.
1272 +
1273 +Also a writable branch has three kinds of "whiteout bases". All these
1274 +are existed when the branch is joined to aufs, and their names are
1275 +whiteout-ed doubly, so that users will never see their names in aufs
1276 +hierarchy.
1277 +1. a regular file which will be hardlinked to all whiteouts.
1278 +2. a directory to store a pseudo-link.
1279 +3. a directory to store an "orphan"-ed file temporary.
1280 +
1281 +1. Whiteout Base
1282 +   When you remove a file on a readonly branch, aufs handles it as a
1283 +   logical deletion and creates a whiteout on the upper writable branch
1284 +   as a hardlink of this file in order not to consume inode on the
1285 +   writable branch.
1286 +2. Pseudo-link Dir
1287 +   See below, Pseudo-link.
1288 +3. Step-Parent Dir
1289 +   When "fileC" exists on the lower readonly branch only and it is
1290 +   opened and removed with its parent dir, and then user writes
1291 +   something into it, then aufs copies-up fileC to this
1292 +   directory. Because there is no other dir to store fileC. After
1293 +   creating a file under this dir, the file is unlinked.
1294 +
1295 +Because aufs supports manipulating branches, ie. add/delete/change
1296 +dynamically, a branch has its own id. When the branch order changes,
1297 +aufs finds the new index by searching the branch id.
1298 +
1299 +
1300 +Pseudo-link
1301 +----------------------------------------------------------------------
1302 +Assume "fileA" exists on the lower readonly branch only and it is
1303 +hardlinked to "fileB" on the branch. When you write something to fileA,
1304 +aufs copies-up it to the upper writable branch. Additionally aufs
1305 +creates a hardlink under the Pseudo-link Directory of the writable
1306 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1307 +simple list. If fileB is read after unlinking fileA, aufs returns
1308 +filedata from the pseudo-link instead of the lower readonly
1309 +branch. Because the pseudo-link is based upon the inode, to keep the
1310 +inode number by xino (see above) is essentially necessary.
1311 +
1312 +All the hardlinks under the Pseudo-link Directory of the writable branch
1313 +should be restored in a proper location later. Aufs provides a utility
1314 +to do this. The userspace helpers executed at remounting and unmounting
1315 +aufs by default.
1316 +During this utility is running, it puts aufs into the pseudo-link
1317 +maintenance mode. In this mode, only the process which began the
1318 +maintenance mode (and its child processes) is allowed to operate in
1319 +aufs. Some other processes which are not related to the pseudo-link will
1320 +be allowed to run too, but the rest have to return an error or wait
1321 +until the maintenance mode ends. If a process already acquires an inode
1322 +mutex (in VFS), it has to return an error.
1323 +
1324 +
1325 +XIB(external inode number bitmap)
1326 +----------------------------------------------------------------------
1327 +Addition to the xino file per a branch, aufs has an external inode number
1328 +bitmap in a superblock object. It is also an internal file such like a
1329 +xino file.
1330 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1331 +not.
1332 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1333 +
1334 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1335 +reduce the number of consumed disk blocks for these files.
1336 +
1337 +
1338 +Virtual or Vertical Dir, and Readdir in Userspace
1339 +----------------------------------------------------------------------
1340 +In order to support multiple layers (branches), aufs readdir operation
1341 +constructs a virtual dir block on memory. For readdir, aufs calls
1342 +vfs_readdir() internally for each dir on branches, merges their entries
1343 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1344 +object. So the file object has its entry list until it is closed. The
1345 +entry list will be updated when the file position is zero and becomes
1346 +obsoleted. This decision is made in aufs automatically.
1347 +
1348 +The dynamically allocated memory block for the name of entries has a
1349 +unit of 512 bytes (by default) and stores the names contiguously (no
1350 +padding). Another block for each entry is handled by kmem_cache too.
1351 +During building dir blocks, aufs creates hash list and judging whether
1352 +the entry is whiteouted by its upper branch or already listed.
1353 +The merged result is cached in the corresponding inode object and
1354 +maintained by a customizable life-time option.
1355 +
1356 +Some people may call it can be a security hole or invite DoS attack
1357 +since the opened and once readdir-ed dir (file object) holds its entry
1358 +list and becomes a pressure for system memory. But I'd say it is similar
1359 +to files under /proc or /sys. The virtual files in them also holds a
1360 +memory page (generally) while they are opened. When an idea to reduce
1361 +memory for them is introduced, it will be applied to aufs too.
1362 +For those who really hate this situation, I've developed readdir(3)
1363 +library which operates this merging in userspace. You just need to set
1364 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1365 +kernel space for readdir(3).
1366 +
1367 +
1368 +Workqueue
1369 +----------------------------------------------------------------------
1370 +Aufs sometimes requires privilege access to a branch. For instance,
1371 +in copy-up/down operation. When a user process is going to make changes
1372 +to a file which exists in the lower readonly branch only, and the mode
1373 +of one of ancestor directories may not be writable by a user
1374 +process. Here aufs copy-up the file with its ancestors and they may
1375 +require privilege to set its owner/group/mode/etc.
1376 +This is a typical case of a application character of aufs (see
1377 +Introduction).
1378 +
1379 +Aufs uses workqueue synchronously for this case. It creates its own
1380 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1381 +passes the request to call mkdir or write (for example), and wait for
1382 +its completion. This approach solves a problem of a signal handler
1383 +simply.
1384 +If aufs didn't adopt the workqueue and changed the privilege of the
1385 +process, then the process may receive the unexpected SIGXFSZ or other
1386 +signals.
1387 +
1388 +Also aufs uses the system global workqueue ("events" kernel thread) too
1389 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1390 +whiteout base and etc. This is unrelated to a privilege.
1391 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1392 +superblock at the beginning, at the same time waits for the completion
1393 +of all queued asynchronous tasks.
1394 +
1395 +
1396 +Whiteout
1397 +----------------------------------------------------------------------
1398 +The whiteout in aufs is very similar to Unionfs's. That is represented
1399 +by its filename. UnionMount takes an approach of a file mode, but I am
1400 +afraid several utilities (find(1) or something) will have to support it.
1401 +
1402 +Basically the whiteout represents "logical deletion" which stops aufs to
1403 +lookup further, but also it represents "dir is opaque" which also stop
1404 +further lookup.
1405 +
1406 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1407 +In order to make several functions in a single systemcall to be
1408 +revertible, aufs adopts an approach to rename a directory to a temporary
1409 +unique whiteouted name.
1410 +For example, in rename(2) dir where the target dir already existed, aufs
1411 +renames the target dir to a temporary unique whiteouted name before the
1412 +actual rename on a branch, and then handles other actions (make it opaque,
1413 +update the attributes, etc). If an error happens in these actions, aufs
1414 +simply renames the whiteouted name back and returns an error. If all are
1415 +succeeded, aufs registers a function to remove the whiteouted unique
1416 +temporary name completely and asynchronously to the system global
1417 +workqueue.
1418 +
1419 +
1420 +Copy-up
1421 +----------------------------------------------------------------------
1422 +It is a well-known feature or concept.
1423 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1424 +internally and makes change to the new file on the upper writable branch.
1425 +When the trigger systemcall does not update the timestamps of the parent
1426 +dir, aufs reverts it after copy-up.
1427 +
1428 +
1429 +Move-down (aufs3.9 and later)
1430 +----------------------------------------------------------------------
1431 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1432 +the lower readonly branch to the upper writable branch when a user
1433 +changes something about the file.
1434 +"Move-down" is an opposite action of copy-up. Basically this action is
1435 +ran manually instead of automatically and internally.
1436 +For desgin and implementation, aufs has to consider these issues.
1437 +- whiteout for the file may exist on the lower branch.
1438 +- ancestor directories may not exist on the lower branch.
1439 +- diropq for the ancestor directories may exist on the upper branch.
1440 +- free space on the lower branch will reduce.
1441 +- another access to the file may happen during moving-down, including
1442 +  UDBA (see "Revalidate Dentry and UDBA").
1443 +- the file should not be hard-linked nor pseudo-linked. they should be
1444 +  handled by auplink utility later.
1445 +
1446 +Sometimes users want to move-down a file from the upper writable branch
1447 +to the lower readonly or writable branch. For instance,
1448 +- the free space of the upper writable branch is going to run out.
1449 +- create a new intermediate branch between the upper and lower branch.
1450 +- etc.
1451 +
1452 +For this purpose, use "aumvdown" command in aufs-util.git.
1453 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1454 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1455 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2024-05-13 17:13:00.614737564 +0200
1456 @@ -0,0 +1,85 @@
1457 +
1458 +# Copyright (C) 2015-2022 Junjiro R. Okajima
1459 +#
1460 +# This program is free software; you can redistribute it and/or modify
1461 +# it under the terms of the GNU General Public License as published by
1462 +# the Free Software Foundation; either version 2 of the License, or
1463 +# (at your option) any later version.
1464 +#
1465 +# This program is distributed in the hope that it will be useful,
1466 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1467 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1468 +# GNU General Public License for more details.
1469 +#
1470 +# You should have received a copy of the GNU General Public License
1471 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1472 +
1473 +Support for a branch who has its ->atomic_open()
1474 +----------------------------------------------------------------------
1475 +The filesystems who implement its ->atomic_open() are not majority. For
1476 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1477 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1478 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1479 +sure whether all filesystems who have ->atomic_open() behave like this,
1480 +but NFSv4 surely returns the error.
1481 +
1482 +In order to support ->atomic_open() for aufs, there are a few
1483 +approaches.
1484 +
1485 +A. Introduce aufs_atomic_open()
1486 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1487 +     branch fs.
1488 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1489 +   an aufs user Pip Cet's approach
1490 +   - calls aufs_create(), VFS finish_open() and notify_change().
1491 +   - pass fake-mode to finish_open(), and then correct the mode by
1492 +     notify_change().
1493 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1494 +   - no aufs_atomic_open().
1495 +   - aufs_lookup() registers the TID to an aufs internal object.
1496 +   - aufs_create() does nothing when the matching TID is registered, but
1497 +     registers the mode.
1498 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1499 +     TID is registered.
1500 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1501 +   credential
1502 +   - no aufs_atomic_open().
1503 +   - aufs_create() registers the TID to an internal object. this info
1504 +     represents "this process created this file just now."
1505 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1506 +     registered TID and re-try open() with superuser's credential.
1507 +
1508 +Pros and cons for each approach.
1509 +
1510 +A.
1511 +   - straightforward but highly depends upon VFS internal.
1512 +   - the atomic behavaiour is kept.
1513 +   - some of parameters such as nameidata are hard to reproduce for
1514 +     branch fs.
1515 +   - large overhead.
1516 +B.
1517 +   - easy to implement.
1518 +   - the atomic behavaiour is lost.
1519 +C.
1520 +   - the atomic behavaiour is kept.
1521 +   - dirty and tricky.
1522 +   - VFS checks whether the file is created correctly after calling
1523 +     ->create(), which means this approach doesn't work.
1524 +D.
1525 +   - easy to implement.
1526 +   - the atomic behavaiour is lost.
1527 +   - to open a file with superuser's credential and give it to a user
1528 +     process is a bad idea, since the file object keeps the credential
1529 +     in it. It may affect LSM or something. This approach doesn't work
1530 +     either.
1531 +
1532 +The approach A is ideal, but it hard to implement. So here is a
1533 +variation of A, which is to be implemented.
1534 +
1535 +A-1. Introduce aufs_atomic_open()
1536 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1537 +       vfs_create() and finish_open().
1538 +     - the demerit is that the several checks after branch fs
1539 +       ->atomic_open() are lost. in the ordinary case, the checks are
1540 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1541 +       be implemented in aufs, but not all I am afraid.
1542 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1543 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1544 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2024-05-13 17:13:00.614737564 +0200
1545 @@ -0,0 +1,113 @@
1546 +
1547 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1548 +#
1549 +# This program is free software; you can redistribute it and/or modify
1550 +# it under the terms of the GNU General Public License as published by
1551 +# the Free Software Foundation; either version 2 of the License, or
1552 +# (at your option) any later version.
1553 +#
1554 +# This program is distributed in the hope that it will be useful,
1555 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1556 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1557 +# GNU General Public License for more details.
1558 +#
1559 +# You should have received a copy of the GNU General Public License
1560 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1561 +
1562 +Lookup in a Branch
1563 +----------------------------------------------------------------------
1564 +Since aufs has a character of sub-VFS (see Introduction), it operates
1565 +lookup for branches as VFS does. It may be a heavy work. But almost all
1566 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1567 +directly connected to its parent. Digging down the directory hierarchy
1568 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1569 +aufs calls it.
1570 +
1571 +When a branch is a remote filesystem, aufs basically relies upon its
1572 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1573 +them.
1574 +For d_revalidate, aufs implements three levels of revalidate tests. See
1575 +"Revalidate Dentry and UDBA" in detail.
1576 +
1577 +
1578 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1579 +----------------------------------------------------------------------
1580 +Let's try case study.
1581 +- aufs has two branches, upper readwrite and lower readonly.
1582 +  /au = /rw + /ro
1583 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1584 +- user invoked "chmod a+rx /au/dirA"
1585 +- the internal copy-up is activated and "/rw/dirA" is created and its
1586 +  permission bits are set to world readable.
1587 +- then "/au/dirA" becomes world readable?
1588 +
1589 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1590 +or it may be a natively readonly filesystem. If aufs respects the lower
1591 +branch, it should not respond readdir request from other users. But user
1592 +allowed it by chmod. Should really aufs rejects showing the entries
1593 +under /ro/dirA?
1594 +
1595 +To be honest, I don't have a good solution for this case. So aufs
1596 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1597 +users.
1598 +When dirperm1 is specified, aufs checks only the highest one for the
1599 +directory permission, and shows the entries. Otherwise, as usual, checks
1600 +every dir existing on all branches and rejects the request.
1601 +
1602 +As a side effect, dirperm1 option improves the performance of aufs
1603 +because the number of permission check is reduced when the number of
1604 +branch is many.
1605 +
1606 +
1607 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1608 +----------------------------------------------------------------------
1609 +Generally VFS helpers re-validate a dentry as a part of lookup.
1610 +0. digging down the directory hierarchy.
1611 +1. lock the parent dir by its i_mutex.
1612 +2. lookup the final (child) entry.
1613 +3. revalidate it.
1614 +4. call the actual operation (create, unlink, etc.)
1615 +5. unlock the parent dir
1616 +
1617 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1618 +called. Actually aufs implements it and checks the dentry on a branch is
1619 +still valid.
1620 +But it is not enough. Because aufs has to release the lock for the
1621 +parent dir on a branch at the end of ->lookup() (step 2) and
1622 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1623 +held by VFS.
1624 +If the file on a branch is changed directly, eg. bypassing aufs, after
1625 +aufs released the lock, then the subsequent operation may cause
1626 +something unpleasant result.
1627 +
1628 +This situation is a result of VFS architecture, ->lookup() and
1629 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1630 +design from VFS's point of view. It is just not suitable for sub-VFS
1631 +character in aufs.
1632 +
1633 +Aufs supports such case by three level of revalidation which is
1634 +selectable by user.
1635 +1. Simple Revalidate
1636 +   Addition to the native flow in VFS's, confirm the child-parent
1637 +   relationship on the branch just after locking the parent dir on the
1638 +   branch in the "actual operation" (step 4). When this validation
1639 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1640 +   checks the validation of the dentry on branches.
1641 +2. Monitor Changes Internally by Inotify/Fsnotify
1642 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1643 +   the dentry on the branch, and returns EBUSY if it finds different
1644 +   dentry.
1645 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1646 +   during it is in cache. When the event is notified, aufs registers a
1647 +   function to kernel 'events' thread by schedule_work(). And the
1648 +   function sets some special status to the cached aufs dentry and inode
1649 +   private data. If they are not cached, then aufs has nothing to
1650 +   do. When the same file is accessed through aufs (step 0-3) later,
1651 +   aufs will detect the status and refresh all necessary data.
1652 +   In this mode, aufs has to ignore the event which is fired by aufs
1653 +   itself.
1654 +3. No Extra Validation
1655 +   This is the simplest test and doesn't add any additional revalidation
1656 +   test, and skip the revalidation in step 4. It is useful and improves
1657 +   aufs performance when system surely hide the aufs branches from user,
1658 +   by over-mounting something (or another method).
1659 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1660 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1661 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2024-05-13 17:13:00.614737564 +0200
1662 @@ -0,0 +1,74 @@
1663 +
1664 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1665 +#
1666 +# This program is free software; you can redistribute it and/or modify
1667 +# it under the terms of the GNU General Public License as published by
1668 +# the Free Software Foundation; either version 2 of the License, or
1669 +# (at your option) any later version.
1670 +#
1671 +# This program is distributed in the hope that it will be useful,
1672 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1673 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1674 +# GNU General Public License for more details.
1675 +#
1676 +# You should have received a copy of the GNU General Public License
1677 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1678 +
1679 +Branch Manipulation
1680 +
1681 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1682 +and changing its permission/attribute, there are a lot of works to do.
1683 +
1684 +
1685 +Add a Branch
1686 +----------------------------------------------------------------------
1687 +o Confirm the adding dir exists outside of aufs, including loopback
1688 +  mount, and its various attributes.
1689 +o Initialize the xino file and whiteout bases if necessary.
1690 +  See struct.txt.
1691 +
1692 +o Check the owner/group/mode of the directory
1693 +  When the owner/group/mode of the adding directory differs from the
1694 +  existing branch, aufs issues a warning because it may impose a
1695 +  security risk.
1696 +  For example, when a upper writable branch has a world writable empty
1697 +  top directory, a malicious user can create any files on the writable
1698 +  branch directly, like copy-up and modify manually. If something like
1699 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1700 +  writable branch, and the writable branch is world-writable, then a
1701 +  malicious guy may create /etc/passwd on the writable branch directly
1702 +  and the infected file will be valid in aufs.
1703 +  I am afraid it can be a security issue, but aufs can do nothing except
1704 +  producing a warning.
1705 +
1706 +
1707 +Delete a Branch
1708 +----------------------------------------------------------------------
1709 +o Confirm the deleting branch is not busy
1710 +  To be general, there is one merit to adopt "remount" interface to
1711 +  manipulate branches. It is to discard caches. At deleting a branch,
1712 +  aufs checks the still cached (and connected) dentries and inodes. If
1713 +  there are any, then they are all in-use. An inode without its
1714 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1715 +
1716 +  For the cached one, aufs checks whether the same named entry exists on
1717 +  other branches.
1718 +  If the cached one is a directory, because aufs provides a merged view
1719 +  to users, as long as one dir is left on any branch aufs can show the
1720 +  dir to users. In this case, the branch can be removed from aufs.
1721 +  Otherwise aufs rejects deleting the branch.
1722 +
1723 +  If any file on the deleting branch is opened by aufs, then aufs
1724 +  rejects deleting.
1725 +
1726 +
1727 +Modify the Permission of a Branch
1728 +----------------------------------------------------------------------
1729 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1730 +  See struct.txt.
1731 +
1732 +o rw --> ro: Confirm the modifying branch is not busy
1733 +  Aufs rejects the request if any of these conditions are true.
1734 +  - a file on the branch is mmap-ed.
1735 +  - a regular file on the branch is opened for write and there is no
1736 +    same named entry on the upper branch.
1737 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1738 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1739 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2024-05-13 17:13:00.614737564 +0200
1740 @@ -0,0 +1,64 @@
1741 +
1742 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1743 +#
1744 +# This program is free software; you can redistribute it and/or modify
1745 +# it under the terms of the GNU General Public License as published by
1746 +# the Free Software Foundation; either version 2 of the License, or
1747 +# (at your option) any later version.
1748 +#
1749 +# This program is distributed in the hope that it will be useful,
1750 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1751 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1752 +# GNU General Public License for more details.
1753 +#
1754 +# You should have received a copy of the GNU General Public License
1755 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1756 +
1757 +Policies to Select One among Multiple Writable Branches
1758 +----------------------------------------------------------------------
1759 +When the number of writable branch is more than one, aufs has to decide
1760 +the target branch for file creation or copy-up. By default, the highest
1761 +writable branch which has the parent (or ancestor) dir of the target
1762 +file is chosen (top-down-parent policy).
1763 +By user's request, aufs implements some other policies to select the
1764 +writable branch, for file creation several policies, round-robin,
1765 +most-free-space, and other policies. For copy-up, top-down-parent,
1766 +bottom-up-parent, bottom-up and others.
1767 +
1768 +As expected, the round-robin policy selects the branch in circular. When
1769 +you have two writable branches and creates 10 new files, 5 files will be
1770 +created for each branch. mkdir(2) systemcall is an exception. When you
1771 +create 10 new directories, all will be created on the same branch.
1772 +And the most-free-space policy selects the one which has most free
1773 +space among the writable branches. The amount of free space will be
1774 +checked by aufs internally, and users can specify its time interval.
1775 +
1776 +The policies for copy-up is more simple,
1777 +top-down-parent is equivalent to the same named on in create policy,
1778 +bottom-up-parent selects the writable branch where the parent dir
1779 +exists and the nearest upper one from the copyup-source,
1780 +bottom-up selects the nearest upper writable branch from the
1781 +copyup-source, regardless the existence of the parent dir.
1782 +
1783 +There are some rules or exceptions to apply these policies.
1784 +- If there is a readonly branch above the policy-selected branch and
1785 +  the parent dir is marked as opaque (a variation of whiteout), or the
1786 +  target (creating) file is whiteout-ed on the upper readonly branch,
1787 +  then the result of the policy is ignored and the target file will be
1788 +  created on the nearest upper writable branch than the readonly branch.
1789 +- If there is a writable branch above the policy-selected branch and
1790 +  the parent dir is marked as opaque or the target file is whiteouted
1791 +  on the branch, then the result of the policy is ignored and the target
1792 +  file will be created on the highest one among the upper writable
1793 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1794 +  it as usual.
1795 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1796 +  They try selecting the branch where the source exists as possible
1797 +  since copyup a large file will take long time. If it can't be,
1798 +  ie. the branch where the source exists is readonly, then they will
1799 +  follow the copyup policy.
1800 +- There is an exception for rename(2) when the target exists.
1801 +  If the rename target exists, aufs compares the index of the branches
1802 +  where the source and the target exists and selects the higher
1803 +  one. If the selected branch is readonly, then aufs follows the
1804 +  copyup policy.
1805 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1806 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1807 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2024-05-13 17:13:00.614737564 +0200
1808 @@ -0,0 +1,44 @@
1809 +
1810 +// to view this graph, run dot(1) command in GRAPHVIZ.
1811 +//
1812 +// This program is free software; you can redistribute it and/or modify
1813 +// it under the terms of the GNU General Public License as published by
1814 +// the Free Software Foundation; either version 2 of the License, or
1815 +// (at your option) any later version.
1816 +//
1817 +// This program is distributed in the hope that it will be useful,
1818 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
1819 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1820 +// GNU General Public License for more details.
1821 +//
1822 +// You should have received a copy of the GNU General Public License
1823 +// along with this program.  If not, see <http://www.gnu.org/licenses/>.
1824 +
1825 +digraph G {
1826 +node [shape=box];
1827 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1828 +
1829 +node [shape=oval];
1830 +
1831 +aufs_rename -> whinfo [label="store/remove"];
1832 +
1833 +node [shape=oval];
1834 +inode_list [label="h_inum list in branch\ncache"];
1835 +
1836 +node [shape=box];
1837 +whinode [label="h_inum list file"];
1838 +
1839 +node [shape=oval];
1840 +brmgmt [label="br_add/del/mod/umount"];
1841 +
1842 +brmgmt -> inode_list [label="create/remove"];
1843 +brmgmt -> whinode [label="load/store"];
1844 +
1845 +inode_list -> whinode [style=dashed,dir=both];
1846 +
1847 +aufs_rename -> inode_list [label="add/del"];
1848 +
1849 +aufs_lookup -> inode_list [label="search"];
1850 +
1851 +aufs_lookup -> whinfo [label="load/remove"];
1852 +}
1853 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1854 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1855 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2024-05-13 17:13:00.614737564 +0200
1856 @@ -0,0 +1,102 @@
1857 +
1858 +# Copyright (C) 2017-2022 Junjiro R. Okajima
1859 +#
1860 +# This program is free software; you can redistribute it and/or modify
1861 +# it under the terms of the GNU General Public License as published by
1862 +# the Free Software Foundation; either version 2 of the License, or
1863 +# (at your option) any later version.
1864 +#
1865 +# This program is distributed in the hope that it will be useful,
1866 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1867 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1868 +# GNU General Public License for more details.
1869 +#
1870 +# You should have received a copy of the GNU General Public License
1871 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1872 +
1873 +Special handling for renaming a directory (DIRREN)
1874 +----------------------------------------------------------------------
1875 +First, let's assume we have a simple usecase.
1876 +
1877 +- /u = /rw + /ro
1878 +- /rw/dirA exists
1879 +- /ro/dirA and /ro/dirA/file exist too
1880 +- there is no dirB on both branches
1881 +- a user issues rename("dirA", "dirB")
1882 +
1883 +Now, what should aufs behave against this rename(2)?
1884 +There are a few possible cases.
1885 +
1886 +A. returns EROFS.
1887 +   since dirA exists on a readonly branch which cannot be renamed.
1888 +B. returns EXDEV.
1889 +   it is possible to copy-up dirA (only the dir itself), but the child
1890 +   entries ("file" in this case) should not be. it must be a bad
1891 +   approach to copy-up recursively.
1892 +C. returns a success.
1893 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1894 +   is a violation of aufs' policy.
1895 +D. construct an extra information which indicates that /ro/dirA should
1896 +   be handled as the name of dirB.
1897 +   overlayfs has a similar feature called REDIRECT.
1898 +
1899 +Until now, aufs implements the case B only which returns EXDEV, and
1900 +expects the userspace application behaves like mv(1) which tries
1901 +issueing rename(2) recursively.
1902 +
1903 +A new aufs feature called DIRREN is introduced which implements the case
1904 +D. There are several "extra information" added.
1905 +
1906 +1. detailed info per renamed directory
1907 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1908 +2. the inode-number list of directories on a branch
1909 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1910 +
1911 +The filename of "detailed info per directory" represents the lower
1912 +branch, and its format is
1913 +- a type of the branch id
1914 +  one of these.
1915 +  + uuid (not implemented yet)
1916 +  + fsid
1917 +  + dev
1918 +- the inode-number of the branch root dir
1919 +
1920 +And it contains these info in a single regular file.
1921 +- magic number
1922 +- branch's inode-number of the logically renamed dir
1923 +- the name of the before-renamed dir
1924 +
1925 +The "detailed info per directory" file is created in aufs rename(2), and
1926 +loaded in any lookup.
1927 +The info is considered in lookup for the matching case only. Here
1928 +"matching" means that the root of branch (in the info filename) is same
1929 +to the current looking-up branch. After looking-up the before-renamed
1930 +name, the inode-number is compared. And the matched dentry is used.
1931 +
1932 +The "inode-number list of directories" is a regular file which contains
1933 +simply the inode-numbers on the branch. The file is created or updated
1934 +in removing the branch, and loaded in adding the branch. Its lifetime is
1935 +equal to the branch.
1936 +The list is referred in lookup, and when the current target inode is
1937 +found in the list, the aufs tries loading the "detailed info per
1938 +directory" and get the changed and valid name of the dir.
1939 +
1940 +Theoretically these "extra informaiton" may be able to be put into XATTR
1941 +in the dir inode. But aufs doesn't choose this way because
1942 +1. XATTR may not be supported by the branch (or its configuration)
1943 +2. XATTR may have its size limit.
1944 +3. XATTR may be less easy to convert than a regular file, when the
1945 +   format of the info is changed in the future.
1946 +At the same time, I agree that the regular file approach is much slower
1947 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1948 +better approach.
1949 +
1950 +This DIRREN feature is enabled by aufs configuration, and is activated
1951 +by a new mount option.
1952 +
1953 +For the more complicated case, there is a work with UDBA option, which
1954 +is to dected the direct access to the branches (by-passing aufs) and to
1955 +maintain the cashes in aufs. Since a single cached aufs dentry may
1956 +contains two names, before- and after-rename, the name comparision in
1957 +UDBA handler may not work correctly. In this case, the behaviour will be
1958 +equivalen to udba=reval case.
1959 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1960 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1961 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2024-05-13 17:13:00.614737564 +0200
1962 @@ -0,0 +1,118 @@
1963 +
1964 +# Copyright (C) 2011-2022 Junjiro R. Okajima
1965 +#
1966 +# This program is free software; you can redistribute it and/or modify
1967 +# it under the terms of the GNU General Public License as published by
1968 +# the Free Software Foundation; either version 2 of the License, or
1969 +# (at your option) any later version.
1970 +#
1971 +# This program is distributed in the hope that it will be useful,
1972 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1973 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1974 +# GNU General Public License for more details.
1975 +#
1976 +# You should have received a copy of the GNU General Public License
1977 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1978 +
1979 +File-based Hierarchical Storage Management (FHSM)
1980 +----------------------------------------------------------------------
1981 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1982 +storage world. Aufs provides this feature as file-based with multiple
1983 +writable branches, based upon the principle of "Colder, the Lower".
1984 +Here the word "colder" means that the less used files, and "lower" means
1985 +that the position in the order of the stacked branches vertically.
1986 +These multiple writable branches are prioritized, ie. the topmost one
1987 +should be the fastest drive and be used heavily.
1988 +
1989 +o Characters in aufs FHSM story
1990 +- aufs itself and a new branch attribute.
1991 +- a new ioctl interface to move-down and to establish a connection with
1992 +  the daemon ("move-down" is a converse of "copy-up").
1993 +- userspace tool and daemon.
1994 +
1995 +The userspace daemon establishes a connection with aufs and waits for
1996 +the notification. The notified information is very similar to struct
1997 +statfs containing the number of consumed blocks and inodes.
1998 +When the consumed blocks/inodes of a branch exceeds the user-specified
1999 +upper watermark, the daemon activates its move-down process until the
2000 +consumed blocks/inodes reaches the user-specified lower watermark.
2001 +
2002 +The actual move-down is done by aufs based upon the request from
2003 +user-space since we need to maintain the inode number and the internal
2004 +pointer arrays in aufs.
2005 +
2006 +Currently aufs FHSM handles the regular files only. Additionally they
2007 +must not be hard-linked nor pseudo-linked.
2008 +
2009 +
2010 +o Cowork of aufs and the user-space daemon
2011 +  During the userspace daemon established the connection, aufs sends a
2012 +  small notification to it whenever aufs writes something into the
2013 +  writable branch. But it may cost high since aufs issues statfs(2)
2014 +  internally. So user can specify a new option to cache the
2015 +  info. Actually the notification is controlled by these factors.
2016 +  + the specified cache time.
2017 +  + classified as "force" by aufs internally.
2018 +  Until the specified time expires, aufs doesn't send the info
2019 +  except the forced cases. When aufs decide forcing, the info is always
2020 +  notified to userspace.
2021 +  For example, the number of free inodes is generally large enough and
2022 +  the shortage of it happens rarely. So aufs doesn't force the
2023 +  notification when creating a new file, directory and others. This is
2024 +  the typical case which aufs doesn't force.
2025 +  When aufs writes the actual filedata and the files consumes any of new
2026 +  blocks, the aufs forces notifying.
2027 +
2028 +
2029 +o Interfaces in aufs
2030 +- New branch attribute.
2031 +  + fhsm
2032 +    Specifies that the branch is managed by FHSM feature. In other word,
2033 +    participant in the FHSM.
2034 +    When nofhsm is set to the branch, it will not be the source/target
2035 +    branch of the move-down operation. This attribute is set
2036 +    independently from coo and moo attributes, and if you want full
2037 +    FHSM, you should specify them as well.
2038 +- New mount option.
2039 +  + fhsm_sec
2040 +    Specifies a second to suppress many less important info to be
2041 +    notified.
2042 +- New ioctl.
2043 +  + AUFS_CTL_FHSM_FD
2044 +    create a new file descriptor which userspace can read the notification
2045 +    (a subset of struct statfs) from aufs.
2046 +- Module parameter 'brs'
2047 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2048 +  be set.
2049 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2050 +  When there are two or more branches with fhsm attributes,
2051 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2052 +  terminates it. As a result of remounting and branch-manipulation, the
2053 +  number of branches with fhsm attribute can be one. In this case,
2054 +  /sbin/mount.aufs will terminate the user-space daemon.
2055 +
2056 +
2057 +Finally the operation is done as these steps in kernel-space.
2058 +- make sure that,
2059 +  + no one else is using the file.
2060 +  + the file is not hard-linked.
2061 +  + the file is not pseudo-linked.
2062 +  + the file is a regular file.
2063 +  + the parent dir is not opaqued.
2064 +- find the target writable branch.
2065 +- make sure the file is not whiteout-ed by the upper (than the target)
2066 +  branch.
2067 +- make the parent dir on the target branch.
2068 +- mutex lock the inode on the branch.
2069 +- unlink the whiteout on the target branch (if exists).
2070 +- lookup and create the whiteout-ed temporary name on the target branch.
2071 +- copy the file as the whiteout-ed temporary name on the target branch.
2072 +- rename the whiteout-ed temporary name to the original name.
2073 +- unlink the file on the source branch.
2074 +- maintain the internal pointer array and the external inode number
2075 +  table (XINO).
2076 +- maintain the timestamps and other attributes of the parent dir and the
2077 +  file.
2078 +
2079 +And of course, in every step, an error may happen. So the operation
2080 +should restore the original file state after an error happens.
2081 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2082 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2083 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2024-05-13 17:13:00.614737564 +0200
2084 @@ -0,0 +1,72 @@
2085 +
2086 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2087 +#
2088 +# This program is free software; you can redistribute it and/or modify
2089 +# it under the terms of the GNU General Public License as published by
2090 +# the Free Software Foundation; either version 2 of the License, or
2091 +# (at your option) any later version.
2092 +#
2093 +# This program is distributed in the hope that it will be useful,
2094 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2095 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2096 +# GNU General Public License for more details.
2097 +#
2098 +# You should have received a copy of the GNU General Public License
2099 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2100 +
2101 +mmap(2) -- File Memory Mapping
2102 +----------------------------------------------------------------------
2103 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2104 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2105 +->mmap().
2106 +This approach is simple and good, but there is one problem.
2107 +Under /proc, several entries show the mmapped files by its path (with
2108 +device and inode number), and the printed path will be the path on the
2109 +branch fs's instead of virtual aufs's.
2110 +This is not a problem in most cases, but some utilities lsof(1) (and its
2111 +user) may expect the path on aufs.
2112 +
2113 +To address this issue, aufs adds a new member called vm_prfile in struct
2114 +vm_area_struct (and struct vm_region). The original vm_file points to
2115 +the file on the branch fs in order to handle everything correctly as
2116 +usual. The new vm_prfile points to a virtual file in aufs, and the
2117 +show-functions in procfs refers to vm_prfile if it is set.
2118 +Also we need to maintain several other places where touching vm_file
2119 +such like
2120 +- fork()/clone() copies vma and the reference count of vm_file is
2121 +  incremented.
2122 +- merging vma maintains the ref count too.
2123 +
2124 +This is not a good approach. It just fakes the printed path. But it
2125 +leaves all behaviour around f_mapping unchanged. This is surely an
2126 +advantage.
2127 +Actually aufs had adopted another complicated approach which calls
2128 +generic_file_mmap() and handles struct vm_operations_struct. In this
2129 +approach, aufs met a hard problem and I could not solve it without
2130 +switching the approach.
2131 +
2132 +There may be one more another approach which is
2133 +- bind-mount the branch-root onto the aufs-root internally
2134 +- grab the new vfsmount (ie. struct mount)
2135 +- lazy-umount the branch-root internally
2136 +- in open(2) the aufs-file, open the branch-file with the hidden
2137 +  vfsmount (instead of the original branch's vfsmount)
2138 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2139 +  but it may be possible from userspace by the mount helper.
2140 +
2141 +Adding the internal hidden vfsmount and using it in opening a file, the
2142 +file path under /proc will be printed correctly. This approach looks
2143 +smarter, but is not possible I am afraid.
2144 +- aufs-root may be bind-mount later. when it happens, another hidden
2145 +  vfsmount will be required.
2146 +- it is hard to get the chance to bind-mount and lazy-umount
2147 +  + in kernel-space, FS can have vfsmount in open(2) via
2148 +    file->f_path, and aufs can know its vfsmount. But several locks are
2149 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2150 +    here, then it may cause a deadlock.
2151 +  + in user-space, bind-mount doesn't invoke the mount helper.
2152 +- since /proc shows dev and ino, aufs has to give vma these info. it
2153 +  means a new member vm_prinode will be necessary. this is essentially
2154 +  equivalent to vm_prfile described above.
2155 +
2156 +I have to give up this "looks-smater" approach.
2157 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2158 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2159 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2024-05-13 17:13:00.614737564 +0200
2160 @@ -0,0 +1,94 @@
2161 +
2162 +# Copyright (C) 2014-2022 Junjiro R. Okajima
2163 +#
2164 +# This program is free software; you can redistribute it and/or modify
2165 +# it under the terms of the GNU General Public License as published by
2166 +# the Free Software Foundation; either version 2 of the License, or
2167 +# (at your option) any later version.
2168 +#
2169 +# This program is distributed in the hope that it will be useful,
2170 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2171 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2172 +# GNU General Public License for more details.
2173 +#
2174 +# You should have received a copy of the GNU General Public License
2175 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2176 +
2177 +Listing XATTR/EA and getting the value
2178 +----------------------------------------------------------------------
2179 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2180 +shows the values from the topmost existing file. This behaviour is good
2181 +for the non-dir entries since the bahaviour exactly matches the shown
2182 +information. But for the directories, aufs considers all the same named
2183 +entries on the lower branches. Which means, if one of the lower entry
2184 +rejects readdir call, then aufs returns an error even if the topmost
2185 +entry allows it. This behaviour is necessary to respect the branch fs's
2186 +security, but can make users confused since the user-visible standard
2187 +attributes don't match the behaviour.
2188 +To address this issue, aufs has a mount option called dirperm1 which
2189 +checks the permission for the topmost entry only, and ignores the lower
2190 +entry's permission.
2191 +
2192 +A similar issue can happen around XATTR.
2193 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2194 +always set. Otherwise these very unpleasant situation would happen.
2195 +- listxattr(2) may return the duplicated entries.
2196 +- users may not be able to remove or reset the XATTR forever,
2197 +
2198 +
2199 +XATTR/EA support in the internal (copy,move)-(up,down)
2200 +----------------------------------------------------------------------
2201 +Generally the extended attributes of inode are categorized as these.
2202 +- "security" for LSM and capability.
2203 +- "system" for posix ACL, 'acl' mount option is required for the branch
2204 +  fs generally.
2205 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2206 +- "user" for userspace, 'user_xattr' mount option is required for the
2207 +  branch fs generally.
2208 +
2209 +Moreover there are some other categories. Aufs handles these rather
2210 +unpopular categories as the ordinary ones, ie. there is no special
2211 +condition nor exception.
2212 +
2213 +In copy-up, the support for XATTR on the dst branch may differ from the
2214 +src branch. In this case, the copy-up operation will get an error and
2215 +the original user operation which triggered the copy-up will fail. It
2216 +can happen that even all copy-up will fail.
2217 +When both of src and dst branches support XATTR and if an error occurs
2218 +during copying XATTR, then the copy-up should fail obviously. That is a
2219 +good reason and aufs should return an error to userspace. But when only
2220 +the src branch support that XATTR, aufs should not return an error.
2221 +For example, the src branch supports ACL but the dst branch doesn't
2222 +because the dst branch may natively un-support it or temporary
2223 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2224 +may NOT return an error even if the XATTR is not supported. It is
2225 +totally up to the branch fs.
2226 +
2227 +Anyway when the aufs internal copy-up gets an error from the dst branch
2228 +fs, then aufs tries removing the just copied entry and returns the error
2229 +to the userspace. The worst case of this situation will be all copy-up
2230 +will fail.
2231 +
2232 +For the copy-up operation, there two basic approaches.
2233 +- copy the specified XATTR only (by category above), and return the
2234 +  error unconditionally if it happens.
2235 +- copy all XATTR, and ignore the error on the specified category only.
2236 +
2237 +In order to support XATTR and to implement the correct behaviour, aufs
2238 +chooses the latter approach and introduces some new branch attributes,
2239 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2240 +They correspond to the XATTR namespaces (see above). Additionally, to be
2241 +convenient, "icex" is also provided which means all "icex*" attributes
2242 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2243 +
2244 +The meaning of these attributes is to ignore the error from setting
2245 +XATTR on that branch.
2246 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2247 +error from the dst branch according to the specified attributes.
2248 +
2249 +Some XATTR may have its default value. The default value may come from
2250 +the parent dir or the environment. If the default value is set at the
2251 +file creating-time, it will be overwritten by copy-up.
2252 +Some contradiction may happen I am afraid.
2253 +Do we need another attribute to stop copying XATTR? I am unsure. For
2254 +now, aufs implements the branch attributes to ignore the error.
2255 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2256 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2257 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2024-05-13 17:13:00.614737564 +0200
2258 @@ -0,0 +1,58 @@
2259 +
2260 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2261 +#
2262 +# This program is free software; you can redistribute it and/or modify
2263 +# it under the terms of the GNU General Public License as published by
2264 +# the Free Software Foundation; either version 2 of the License, or
2265 +# (at your option) any later version.
2266 +#
2267 +# This program is distributed in the hope that it will be useful,
2268 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2269 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2270 +# GNU General Public License for more details.
2271 +#
2272 +# You should have received a copy of the GNU General Public License
2273 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2274 +
2275 +Export Aufs via NFS
2276 +----------------------------------------------------------------------
2277 +Here is an approach.
2278 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2279 +  generation.
2280 +- iget_locked(): initialize aufs inode generation for a new inode, and
2281 +  store it in xigen file.
2282 +- destroy_inode(): increment aufs inode generation and store it in xigen
2283 +  file. it is necessary even if it is not unlinked, because any data of
2284 +  inode may be changed by UDBA.
2285 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2286 +  build file handle by
2287 +  + branch id (4 bytes)
2288 +  + superblock generation (4 bytes)
2289 +  + inode number (4 or 8 bytes)
2290 +  + parent dir inode number (4 or 8 bytes)
2291 +  + inode generation (4 bytes))
2292 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2293 +    bytes)
2294 +  + file handle for a branch (by exportfs_encode_fh())
2295 +- fh_to_dentry():
2296 +  + find the index of a branch from its id in handle, and check it is
2297 +    still exist in aufs.
2298 +  + 1st level: get the inode number from handle and search it in cache.
2299 +  + 2nd level: if not found in cache, get the parent inode number from
2300 +    the handle and search it in cache. and then open the found parent
2301 +    dir, find the matching inode number by vfs_readdir() and get its
2302 +    name, and call lookup_one_len() for the target dentry.
2303 +  + 3rd level: if the parent dir is not cached, call
2304 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2305 +    build a pathname of it, convert it a pathname in aufs, call
2306 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2307 +    the 2nd level.
2308 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2309 +    for every branch, but not itself. to get this, (currently) aufs
2310 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2311 +    idea, but I didn't get other approach.
2312 +  + test the generation of the gotten inode.
2313 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2314 +  convert it into ESTALE for NFSD.
2315 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2316 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2317 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2318 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2319 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2024-05-13 17:13:00.614737564 +0200
2320 @@ -0,0 +1,52 @@
2321 +
2322 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2323 +#
2324 +# This program is free software; you can redistribute it and/or modify
2325 +# it under the terms of the GNU General Public License as published by
2326 +# the Free Software Foundation; either version 2 of the License, or
2327 +# (at your option) any later version.
2328 +#
2329 +# This program is distributed in the hope that it will be useful,
2330 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2331 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2332 +# GNU General Public License for more details.
2333 +#
2334 +# You should have received a copy of the GNU General Public License
2335 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2336 +
2337 +Show Whiteout Mode (shwh)
2338 +----------------------------------------------------------------------
2339 +Generally aufs hides the name of whiteouts. But in some cases, to show
2340 +them is very useful for users. For instance, creating a new middle layer
2341 +(branch) by merging existing layers.
2342 +
2343 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2344 +When you have three branches,
2345 +- Bottom: 'system', squashfs (underlying base system), read-only
2346 +- Middle: 'mods', squashfs, read-only
2347 +- Top: 'overlay', ram (tmpfs), read-write
2348 +
2349 +The top layer is loaded at boot time and saved at shutdown, to preserve
2350 +the changes made to the system during the session.
2351 +When larger changes have been made, or smaller changes have accumulated,
2352 +the size of the saved top layer data grows. At this point, it would be
2353 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2354 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2355 +restoring save and load speed.
2356 +
2357 +This merging is simplified by the use of another aufs mount, of just the
2358 +two overlay branches using the 'shwh' option.
2359 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2360 +       aufs /livesys/merge_union
2361 +
2362 +A merged view of these two branches is then available at
2363 +/livesys/merge_union, and the new feature is that the whiteouts are
2364 +visible!
2365 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2366 +writing to all branches. Also the default mode for all branches is 'ro'.
2367 +It is now possible to save the combined contents of the two overlay
2368 +branches to a new squashfs, e.g.:
2369 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2370 +
2371 +This new squashfs archive can be stored on the boot device and the
2372 +initramfs will use it to replace the old one at the next boot.
2373 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2374 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2375 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2024-05-13 17:13:00.614737564 +0200
2376 @@ -0,0 +1,47 @@
2377 +
2378 +# Copyright (C) 2010-2022 Junjiro R. Okajima
2379 +#
2380 +# This program is free software; you can redistribute it and/or modify
2381 +# it under the terms of the GNU General Public License as published by
2382 +# the Free Software Foundation; either version 2 of the License, or
2383 +# (at your option) any later version.
2384 +#
2385 +# This program is distributed in the hope that it will be useful,
2386 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2387 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2388 +# GNU General Public License for more details.
2389 +#
2390 +# You should have received a copy of the GNU General Public License
2391 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2392 +
2393 +Dynamically customizable FS operations
2394 +----------------------------------------------------------------------
2395 +Generally FS operations (struct inode_operations, struct
2396 +address_space_operations, struct file_operations, etc.) are defined as
2397 +"static const", but it never means that FS have only one set of
2398 +operation. Some FS have multiple sets of them. For instance, ext2 has
2399 +three sets, one for XIP, for NOBH, and for normal.
2400 +Since aufs overrides and redirects these operations, sometimes aufs has
2401 +to change its behaviour according to the branch FS type. More importantly
2402 +VFS acts differently if a function (member in the struct) is set or
2403 +not. It means aufs should have several sets of operations and select one
2404 +among them according to the branch FS definition.
2405 +
2406 +In order to solve this problem and not to affect the behaviour of VFS,
2407 +aufs defines these operations dynamically. For instance, aufs defines
2408 +dummy direct_IO function for struct address_space_operations, but it may
2409 +not be set to the address_space_operations actually. When the branch FS
2410 +doesn't have it, aufs doesn't set it to its address_space_operations
2411 +while the function definition itself is still alive. So the behaviour
2412 +itself will not change, and it will return an error when direct_IO is
2413 +not set.
2414 +
2415 +The lifetime of these dynamically generated operation object is
2416 +maintained by aufs branch object. When the branch is removed from aufs,
2417 +the reference counter of the object is decremented. When it reaches
2418 +zero, the dynamically generated operation object will be freed.
2419 +
2420 +This approach is designed to support AIO (io_submit), Direct I/O and
2421 +XIP (DAX) mainly.
2422 +Currently this approach is applied to address_space_operations for
2423 +regular files only.
2424 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2425 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2426 +++ linux/Documentation/filesystems/aufs/README 2024-05-13 17:13:00.614737564 +0200
2427 @@ -0,0 +1,409 @@
2428 +
2429 +Aufs6 -- advanced multi layered unification filesystem version 6.x
2430 +http://aufs.sf.net
2431 +Junjiro R. Okajima
2432 +
2433 +
2434 +0. Introduction
2435 +----------------------------------------
2436 +In the early days, aufs was entirely re-designed and re-implemented
2437 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2438 +improvements and implementations, it became totally different from
2439 +Unionfs while keeping the basic features.
2440 +Later, Unionfs Version 2.x series began taking some of the same
2441 +approaches to aufs1's.
2442 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2443 +University and his team.
2444 +
2445 +Aufs6 supports linux-v6.0 and later, try aufs6.0 branch in
2446 +aufs-linux.git or aufs-standalone.git.
2447 +If you want older kernel version support,
2448 +- for linux-v5.x series, try aufs-linux.git or aufs-standalone.git
2449 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2450 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2451 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2452 +  or aufs1 from CVS on SourceForge.
2453 +
2454 +Note: the name of aufs5-linux.git and aufs5-standalone.git on github
2455 +      were changed. Now they are aufs-linux.git and
2456 +      aufs-standalone.git and they contain aufs5 and later branches.
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 aufs6.
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 aufs6, aufs-linux.git,
2535 +aufs-standalone.git, and aufs-util.git.
2536 +While the aufs-util is always necessary, you need either of aufs-linux
2537 +or aufs-standalone.
2538 +
2539 +The aufs-linux tree includes the whole linux mainline GIT tree,
2540 +git://git.kernel.org/.../torvalds/linux.git.
2541 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2542 +build aufs6 as an external kernel module.
2543 +Several extra patches are not included in this tree. Only
2544 +aufs-standalone tree contains them. They are described in the later
2545 +section "Configuration and Compilation."
2546 +
2547 +On the other hand, the aufs-standalone tree has only aufs source files
2548 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2549 +But you need to apply all aufs patches manually.
2550 +
2551 +You will find GIT branches whose name is in form of "aufs6.x" where "x"
2552 +represents the linux kernel version, "linux-6.x". For instance,
2553 +"aufs6.0" is for linux-6.0. For latest "linux-6.x-rcN", use
2554 +"aufs6.x-rcN" branch.
2555 +
2556 +o aufs-linux tree
2557 +$ git clone --reference /your/linux/git/tree \
2558 +       git://github.com/sfjro/aufs-linux.git aufs-linux.git
2559 +- if you don't have linux GIT tree, then remove "--reference ..."
2560 +$ cd aufs-linux.git
2561 +$ git checkout origin/aufs6.0
2562 +
2563 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2564 +leave the patch-work to GIT.
2565 +$ cd /your/linux/git/tree
2566 +$ git remote add aufs git://github.com/sfjro/aufs-linux.git
2567 +$ git fetch aufs
2568 +$ git checkout -b my6.0 v6.0
2569 +$ (add your local change...)
2570 +$ git pull aufs aufs6.0
2571 +- now you have v6.0 + your_changes + aufs6.0 in you my6.0 branch.
2572 +- you may need to solve some conflicts between your_changes and
2573 +  aufs6.0. in this case, git-rerere is recommended so that you can
2574 +  solve the similar conflicts automatically when you upgrade to 6.1 or
2575 +  later in the future.
2576 +
2577 +o aufs-standalone tree
2578 +$ git clone git://github.com/sfjro/aufs-standalone.git aufs-standalone.git
2579 +$ cd aufs-standalone.git
2580 +$ git checkout origin/aufs6.0
2581 +
2582 +o aufs-util tree
2583 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2584 +- note that the public aufs-util.git is on SourceForge instead of
2585 +  GitHUB.
2586 +$ cd aufs-util.git
2587 +$ git checkout origin/aufs6.0
2588 +
2589 +Note: The 6.x-rcN branch is to be used with `rc' kernel versions ONLY.
2590 +The minor version number, 'x' in '6.x', of aufs may not always
2591 +follow the minor version number of the kernel.
2592 +Because changes in the kernel that cause the use of a new
2593 +minor version number do not always require changes to aufs-util.
2594 +
2595 +Since aufs-util has its own minor version number, you may not be
2596 +able to find a GIT branch in aufs-util for your kernel's
2597 +exact minor version number.
2598 +In this case, you should git-checkout the branch for the
2599 +nearest lower number.
2600 +
2601 +For (an unreleased) example:
2602 +If you are using "linux-6.10" and the "aufs6.10" branch
2603 +does not exist in aufs-util repository, then "aufs6.9", "aufs6.8"
2604 +or something numerically smaller is the branch for your kernel.
2605 +
2606 +Also you can view all branches by
2607 +       $ git branch -a
2608 +
2609 +
2610 +3. Configuration and Compilation
2611 +----------------------------------------
2612 +Make sure you have git-checkout'ed the correct branch.
2613 +
2614 +For aufs-linux tree,
2615 +- enable CONFIG_AUFS_FS.
2616 +- set other aufs configurations if necessary.
2617 +- for aufs5.13 and later
2618 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2619 +  also a caller of VFS functions for branch filesystems, subclassing of
2620 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2621 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2622 +  need to customize some LOCKDEP numbers. Here are what I use on my
2623 +  test environment.
2624 +       CONFIG_LOCKDEP_BITS=21
2625 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2626 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2627 +  Also you will need to expand some constant values in LOCKDEP. Refer
2628 +  to lockdep-debug.patch in aufs-standalone.git.
2629 +
2630 +For aufs-standalone tree,
2631 +There are several ways to build.
2632 +
2633 +1.
2634 +- apply ./aufs6-kbuild.patch to your kernel source files.
2635 +- apply ./aufs6-base.patch too.
2636 +- apply ./aufs6-mmap.patch too.
2637 +- apply ./aufs6-standalone.patch too, if you have a plan to set
2638 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs-standalone.patch.
2639 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2640 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2641 +- enable CONFIG_AUFS_FS, you can select either
2642 +  =m or =y.
2643 +- and build your kernel as usual.
2644 +- install the built kernel.
2645 +- install the header files too by "make headers_install" to the
2646 +  directory where you specify. By default, it is $PWD/usr.
2647 +  "make help" shows a brief note for headers_install.
2648 +- and reboot your system.
2649 +
2650 +2.
2651 +- module only (CONFIG_AUFS_FS=m).
2652 +- apply ./aufs6-base.patch to your kernel source files.
2653 +- apply ./aufs6-mmap.patch too.
2654 +- apply ./aufs6-standalone.patch too.
2655 +- build your kernel, don't forget "make headers_install", and reboot.
2656 +- edit ./config.mk and set other aufs configurations if necessary.
2657 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2658 +  every aufs configurations.
2659 +- build the module by simple "make".
2660 +- you can specify ${KDIR} make variable which points to your kernel
2661 +  source tree.
2662 +- install the files
2663 +  + run "make install" to install the aufs module, or copy the built
2664 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2665 +  + run "make install_headers" (instead of headers_install) to install
2666 +    the modified aufs header file (you can specify DESTDIR which is
2667 +    available in aufs standalone version's Makefile only), or copy
2668 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2669 +    you like manually. By default, the target directory is $PWD/usr.
2670 +- no need to apply aufs6-kbuild.patch, nor copying source files to your
2671 +  kernel source tree.
2672 +
2673 +Note: The header file aufs_type.h is necessary to build aufs-util
2674 +      as well as "make headers_install" in the kernel source tree.
2675 +      headers_install is subject to be forgotten, but it is essentially
2676 +      necessary, not only for building aufs-util.
2677 +      You may not meet problems without headers_install in some older
2678 +      version though.
2679 +
2680 +And then,
2681 +- read README in aufs-util, build and install it
2682 +- note that your distribution may contain an obsoleted version of
2683 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2684 +  utilities, make sure that your compiler refers the correct aufs header
2685 +  file which is built by "make headers_install."
2686 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2687 +  then run "make install_ulib" too. And refer to the aufs manual in
2688 +  detail.
2689 +
2690 +There several other patches in aufs-standalone.git. They are all
2691 +optional. When you meet some problems, they will help you.
2692 +- aufs6-loopback.patch
2693 +  Supports a nested loopback mount in a branch-fs. This patch is
2694 +  unnecessary until aufs produces a message like "you may want to try
2695 +  another patch for loopback file".
2696 +- vfs-ino.patch
2697 +  Modifies a system global kernel internal function get_next_ino() in
2698 +  order to stop assigning 0 for an inode-number. Not directly related to
2699 +  aufs, but recommended generally.
2700 +- tmpfs-idr.patch
2701 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2702 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2703 +  duplication of inode number, which is important for backup tools and
2704 +  other utilities. When you find aufs XINO files for tmpfs branch
2705 +  growing too much, try this patch.
2706 +- lockdep-debug.patch
2707 +  Similar to some kernel configurations for LOCKDEP (see the top of
2708 +  this section), you will need expand some constants in LOCKDEP for
2709 +  aufs if you enable CONFIG_LOCKDEP.
2710 +
2711 +
2712 +4. Usage
2713 +----------------------------------------
2714 +At first, make sure aufs-util are installed, and please read the aufs
2715 +manual, aufs.5 in aufs-util.git tree.
2716 +$ man -l aufs.5
2717 +
2718 +And then,
2719 +$ mkdir /tmp/rw /tmp/aufs
2720 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2721 +
2722 +Here is another example. The result is equivalent.
2723 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2724 +  Or
2725 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2726 +# mount -o remount,append:${HOME} /tmp/aufs
2727 +
2728 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2729 +you modify a file under /tmp/aufs, the one on your home directory is
2730 +not affected, instead the same named file will be newly created under
2731 +/tmp/rw. And all of your modification to a file will be applied to
2732 +the one under /tmp/rw. This is called the file based Copy on Write
2733 +(COW) method.
2734 +Aufs mount options are described in aufs.5.
2735 +If you run chroot or something and make your aufs as a root directory,
2736 +then you need to customize the shutdown script. See the aufs manual in
2737 +detail.
2738 +
2739 +Additionally, there are some sample usages of aufs which are a
2740 +diskless system with network booting, and LiveCD over NFS.
2741 +See sample dir in CVS tree on SourceForge.
2742 +
2743 +
2744 +5. Contact
2745 +----------------------------------------
2746 +When you have any problems or strange behaviour in aufs, please let me
2747 +know with:
2748 +- /proc/mounts (instead of the output of mount(8))
2749 +- /sys/module/aufs/*
2750 +- /sys/fs/aufs/* (if you have them)
2751 +- /debug/aufs/* (if you have them)
2752 +- linux kernel version
2753 +  if your kernel is not plain, for example modified by distributor,
2754 +  the url where i can download its source is necessary too.
2755 +- aufs version which was printed at loading the module or booting the
2756 +  system, instead of the date you downloaded.
2757 +- configuration (define/undefine CONFIG_AUFS_xxx)
2758 +- kernel configuration or /proc/config.gz (if you have it)
2759 +- LSM (linux security module, if you are using)
2760 +- behaviour which you think to be incorrect
2761 +- actual operation, reproducible one is better
2762 +- mailto: aufs-users at lists.sourceforge.net
2763 +
2764 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2765 +and Feature Requests) on SourceForge. Please join and write to
2766 +aufs-users ML.
2767 +
2768 +
2769 +6. Acknowledgements
2770 +----------------------------------------
2771 +Thanks to everyone who have tried and are using aufs, whoever
2772 +have reported a bug or any feedback.
2773 +
2774 +Especially donators:
2775 +Tomas Matejicek(slax.org) made a donation (much more than once).
2776 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2777 +       scripts) is making "doubling" donations.
2778 +       Unfortunately I cannot list all of the donators, but I really
2779 +       appreciate.
2780 +       It ends Aug 2010, but the ordinary donation URL is still available.
2781 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2782 +Dai Itasaka made a donation (2007/8).
2783 +Chuck Smith made a donation (2008/4, 10 and 12).
2784 +Henk Schoneveld made a donation (2008/9).
2785 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2786 +Francois Dupoux made a donation (2008/11).
2787 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2788 +       aufs2 GIT tree (2009/2).
2789 +William Grant made a donation (2009/3).
2790 +Patrick Lane made a donation (2009/4).
2791 +The Mail Archive (mail-archive.com) made donations (2009/5).
2792 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2793 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2794 +Pavel Pronskiy made a donation (2011/2).
2795 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2796 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2797 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2798 +11).
2799 +Sam Liddicott made a donation (2011/9).
2800 +Era Scarecrow made a donation (2013/4).
2801 +Bor Ratajc made a donation (2013/4).
2802 +Alessandro Gorreta made a donation (2013/4).
2803 +POIRETTE Marc made a donation (2013/4).
2804 +Alessandro Gorreta made a donation (2013/4).
2805 +lauri kasvandik made a donation (2013/5).
2806 +"pemasu from Finland" made a donation (2013/7).
2807 +The Parted Magic Project made a donation (2013/9 and 11).
2808 +Pavel Barta made a donation (2013/10).
2809 +Nikolay Pertsev made a donation (2014/5).
2810 +James B made a donation (2014/7, 2015/7, and 2021/12).
2811 +Stefano Di Biase made a donation (2014/8).
2812 +Daniel Epellei made a donation (2015/1).
2813 +OmegaPhil made a donation (2016/1, 2018/4).
2814 +Tomasz Szewczyk made a donation (2016/4).
2815 +James Burry made a donation (2016/12).
2816 +Carsten Rose made a donation (2018/9).
2817 +Porteus Kiosk made a donation (2018/10).
2818 +huronOS team: Enya Quetzalli made donations (2022/5, 2023/5 and 8).
2819 +Vasily Mikhaylichenko made a donation (2023/5).
2820 +
2821 +Thank you very much.
2822 +Donations are always, including future donations, very important and
2823 +helpful for me to keep on developing aufs.
2824 +
2825 +
2826 +7.
2827 +----------------------------------------
2828 +If you are an experienced user, no explanation is needed. Aufs is
2829 +just a linux filesystem.
2830 +
2831 +
2832 +Enjoy!
2833 +
2834 +# Local variables: ;
2835 +# mode: text;
2836 +# End: ;
2837 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2838 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2839 +++ linux/fs/aufs/aufs.h        2024-05-13 17:13:00.618070924 +0200
2840 @@ -0,0 +1,62 @@
2841 +/* SPDX-License-Identifier: GPL-2.0 */
2842 +/*
2843 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2844 + *
2845 + * This program is free software; you can redistribute it and/or modify
2846 + * it under the terms of the GNU General Public License as published by
2847 + * the Free Software Foundation; either version 2 of the License, or
2848 + * (at your option) any later version.
2849 + *
2850 + * This program is distributed in the hope that it will be useful,
2851 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2852 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2853 + * GNU General Public License for more details.
2854 + *
2855 + * You should have received a copy of the GNU General Public License
2856 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2857 + */
2858 +
2859 +/*
2860 + * all header files
2861 + */
2862 +
2863 +#ifndef __AUFS_H__
2864 +#define __AUFS_H__
2865 +
2866 +#ifdef __KERNEL__
2867 +
2868 +#define AuStub(type, name, body, ...) \
2869 +       static inline type name(__VA_ARGS__) { body; }
2870 +
2871 +#define AuStubVoid(name, ...) \
2872 +       AuStub(void, name, , __VA_ARGS__)
2873 +#define AuStubInt0(name, ...) \
2874 +       AuStub(int, name, return 0, __VA_ARGS__)
2875 +
2876 +#include "debug.h"
2877 +
2878 +#include "branch.h"
2879 +#include "cpup.h"
2880 +#include "dcsub.h"
2881 +#include "dbgaufs.h"
2882 +#include "dentry.h"
2883 +#include "dir.h"
2884 +#include "dirren.h"
2885 +#include "dynop.h"
2886 +#include "file.h"
2887 +#include "fstype.h"
2888 +#include "hbl.h"
2889 +#include "inode.h"
2890 +#include "lcnt.h"
2891 +#include "loop.h"
2892 +#include "module.h"
2893 +#include "opts.h"
2894 +#include "rwsem.h"
2895 +#include "super.h"
2896 +#include "sysaufs.h"
2897 +#include "vfsub.h"
2898 +#include "whout.h"
2899 +#include "wkq.h"
2900 +
2901 +#endif /* __KERNEL__ */
2902 +#endif /* __AUFS_H__ */
2903 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2904 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2905 +++ linux/fs/aufs/branch.c      2024-05-13 17:13:00.618070924 +0200
2906 @@ -0,0 +1,1427 @@
2907 +// SPDX-License-Identifier: GPL-2.0
2908 +/*
2909 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2910 + *
2911 + * This program is free software; you can redistribute it and/or modify
2912 + * it under the terms of the GNU General Public License as published by
2913 + * the Free Software Foundation; either version 2 of the License, or
2914 + * (at your option) any later version.
2915 + *
2916 + * This program is distributed in the hope that it will be useful,
2917 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2918 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2919 + * GNU General Public License for more details.
2920 + *
2921 + * You should have received a copy of the GNU General Public License
2922 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2923 + */
2924 +
2925 +/*
2926 + * branch management
2927 + */
2928 +
2929 +#include <linux/compat.h>
2930 +#include <linux/statfs.h>
2931 +#include "aufs.h"
2932 +
2933 +/*
2934 + * free a single branch
2935 + */
2936 +static void au_br_do_free(struct au_branch *br)
2937 +{
2938 +       int i;
2939 +       struct au_wbr *wbr;
2940 +       struct au_dykey **key;
2941 +
2942 +       au_hnotify_fin_br(br);
2943 +       /* always, regardless the mount option */
2944 +       au_dr_hino_free(&br->br_dirren);
2945 +       au_xino_put(br);
2946 +
2947 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2948 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2949 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2950 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2951 +
2952 +       wbr = br->br_wbr;
2953 +       if (wbr) {
2954 +               for (i = 0; i < AuBrWh_Last; i++)
2955 +                       dput(wbr->wbr_wh[i]);
2956 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2957 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2958 +       }
2959 +
2960 +       if (br->br_fhsm) {
2961 +               au_br_fhsm_fin(br->br_fhsm);
2962 +               au_kfree_try_rcu(br->br_fhsm);
2963 +       }
2964 +
2965 +       key = br->br_dykey;
2966 +       for (i = 0; i < AuBrDynOp; i++, key++)
2967 +               if (*key)
2968 +                       au_dy_put(*key);
2969 +               else
2970 +                       break;
2971 +
2972 +       /* recursive lock, s_umount of branch's */
2973 +       /* synchronize_rcu(); */ /* why? */
2974 +       lockdep_off();
2975 +       path_put(&br->br_path);
2976 +       lockdep_on();
2977 +       au_kfree_rcu(wbr);
2978 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2979 +       au_lcnt_wait_for_fin(&br->br_count);
2980 +       /* I don't know why, but percpu_refcount requires this */
2981 +       /* synchronize_rcu(); */
2982 +       au_kfree_rcu(br);
2983 +}
2984 +
2985 +/*
2986 + * frees all branches
2987 + */
2988 +void au_br_free(struct au_sbinfo *sbinfo)
2989 +{
2990 +       aufs_bindex_t bmax;
2991 +       struct au_branch **br;
2992 +
2993 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2994 +
2995 +       bmax = sbinfo->si_bbot + 1;
2996 +       br = sbinfo->si_branch;
2997 +       while (bmax--)
2998 +               au_br_do_free(*br++);
2999 +}
3000 +
3001 +/*
3002 + * find the index of a branch which is specified by @br_id.
3003 + */
3004 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3005 +{
3006 +       aufs_bindex_t bindex, bbot;
3007 +
3008 +       bbot = au_sbbot(sb);
3009 +       for (bindex = 0; bindex <= bbot; bindex++)
3010 +               if (au_sbr_id(sb, bindex) == br_id)
3011 +                       return bindex;
3012 +       return -1;
3013 +}
3014 +
3015 +/* ---------------------------------------------------------------------- */
3016 +
3017 +/*
3018 + * add a branch
3019 + */
3020 +
3021 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3022 +                       struct dentry *h_root)
3023 +{
3024 +       if (unlikely(h_adding == h_root
3025 +                    || au_test_loopback_overlap(sb, h_adding)))
3026 +               return 1;
3027 +       if (h_adding->d_sb != h_root->d_sb)
3028 +               return 0;
3029 +       return au_test_subdir(h_adding, h_root)
3030 +               || au_test_subdir(h_root, h_adding);
3031 +}
3032 +
3033 +/*
3034 + * returns a newly allocated branch. @new_nbranch is a number of branches
3035 + * after adding a branch.
3036 + */
3037 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3038 +                                    int perm)
3039 +{
3040 +       struct au_branch *add_branch;
3041 +       struct dentry *root;
3042 +       struct inode *inode;
3043 +       int err;
3044 +
3045 +       err = -ENOMEM;
3046 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3047 +       if (unlikely(!add_branch))
3048 +               goto out;
3049 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3050 +       if (unlikely(!add_branch->br_xino))
3051 +               goto out_br;
3052 +       err = au_hnotify_init_br(add_branch, perm);
3053 +       if (unlikely(err))
3054 +               goto out_xino;
3055 +
3056 +       if (au_br_writable(perm)) {
3057 +               /* may be freed separately at changing the branch permission */
3058 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3059 +                                            GFP_NOFS);
3060 +               if (unlikely(!add_branch->br_wbr))
3061 +                       goto out_hnotify;
3062 +       }
3063 +
3064 +       if (au_br_fhsm(perm)) {
3065 +               err = au_fhsm_br_alloc(add_branch);
3066 +               if (unlikely(err))
3067 +                       goto out_wbr;
3068 +       }
3069 +
3070 +       root = sb->s_root;
3071 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3072 +       if (!err)
3073 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3074 +       if (!err) {
3075 +               inode = d_inode(root);
3076 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3077 +                                       /*may_shrink*/0);
3078 +       }
3079 +       if (!err)
3080 +               return add_branch; /* success */
3081 +
3082 +out_wbr:
3083 +       au_kfree_rcu(add_branch->br_wbr);
3084 +out_hnotify:
3085 +       au_hnotify_fin_br(add_branch);
3086 +out_xino:
3087 +       au_xino_put(add_branch);
3088 +out_br:
3089 +       au_kfree_rcu(add_branch);
3090 +out:
3091 +       return ERR_PTR(err);
3092 +}
3093 +
3094 +/*
3095 + * test if the branch permission is legal or not.
3096 + */
3097 +static int test_br(struct inode *inode, int brperm, char *path)
3098 +{
3099 +       int err;
3100 +
3101 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3102 +       if (!err)
3103 +               goto out;
3104 +
3105 +       err = -EINVAL;
3106 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3107 +
3108 +out:
3109 +       return err;
3110 +}
3111 +
3112 +/*
3113 + * returns:
3114 + * 0: success, the caller will add it
3115 + * plus: success, it is already unified, the caller should ignore it
3116 + * minus: error
3117 + */
3118 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3119 +{
3120 +       int err;
3121 +       aufs_bindex_t bbot, bindex;
3122 +       struct dentry *root, *h_dentry;
3123 +       struct inode *inode, *h_inode;
3124 +
3125 +       root = sb->s_root;
3126 +       bbot = au_sbbot(sb);
3127 +       if (unlikely(bbot >= 0
3128 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3129 +               err = 1;
3130 +               if (!remount) {
3131 +                       err = -EINVAL;
3132 +                       pr_err("%s duplicated\n", add->pathname);
3133 +               }
3134 +               goto out;
3135 +       }
3136 +
3137 +       err = -ENOSPC; /* -E2BIG; */
3138 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3139 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3140 +               pr_err("number of branches exceeded %s\n", add->pathname);
3141 +               goto out;
3142 +       }
3143 +
3144 +       err = -EDOM;
3145 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3146 +               pr_err("bad index %d\n", add->bindex);
3147 +               goto out;
3148 +       }
3149 +
3150 +       inode = d_inode(add->path.dentry);
3151 +       err = -ENOENT;
3152 +       if (unlikely(!inode->i_nlink)) {
3153 +               pr_err("no existence %s\n", add->pathname);
3154 +               goto out;
3155 +       }
3156 +
3157 +       err = -EINVAL;
3158 +       if (unlikely(inode->i_sb == sb)) {
3159 +               pr_err("%s must be outside\n", add->pathname);
3160 +               goto out;
3161 +       }
3162 +
3163 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3164 +               pr_err("unsupported filesystem, %s (%s)\n",
3165 +                      add->pathname, au_sbtype(inode->i_sb));
3166 +               goto out;
3167 +       }
3168 +
3169 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3170 +               pr_err("already stacked, %s (%s)\n",
3171 +                      add->pathname, au_sbtype(inode->i_sb));
3172 +               goto out;
3173 +       }
3174 +
3175 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3176 +       if (unlikely(err))
3177 +               goto out;
3178 +
3179 +       if (bbot < 0)
3180 +               return 0; /* success */
3181 +
3182 +       err = -EINVAL;
3183 +       for (bindex = 0; bindex <= bbot; bindex++)
3184 +               if (unlikely(test_overlap(sb, add->path.dentry,
3185 +                                         au_h_dptr(root, bindex)))) {
3186 +                       pr_err("%s is overlapped\n", add->pathname);
3187 +                       goto out;
3188 +               }
3189 +
3190 +       err = 0;
3191 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3192 +               h_dentry = au_h_dptr(root, 0);
3193 +               h_inode = d_inode(h_dentry);
3194 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3195 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3196 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3197 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3198 +                               add->pathname,
3199 +                               i_uid_read(inode), i_gid_read(inode),
3200 +                               (inode->i_mode & S_IALLUGO),
3201 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3202 +                               (h_inode->i_mode & S_IALLUGO));
3203 +       }
3204 +
3205 +out:
3206 +       return err;
3207 +}
3208 +
3209 +/*
3210 + * initialize or clean the whiteouts for an adding branch
3211 + */
3212 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3213 +                        int new_perm)
3214 +{
3215 +       int err, old_perm;
3216 +       aufs_bindex_t bindex;
3217 +       struct inode *h_inode;
3218 +       struct au_wbr *wbr;
3219 +       struct au_hinode *hdir;
3220 +       struct dentry *h_dentry;
3221 +
3222 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3223 +       if (unlikely(err))
3224 +               goto out;
3225 +
3226 +       wbr = br->br_wbr;
3227 +       old_perm = br->br_perm;
3228 +       br->br_perm = new_perm;
3229 +       hdir = NULL;
3230 +       h_inode = NULL;
3231 +       bindex = au_br_index(sb, br->br_id);
3232 +       if (0 <= bindex) {
3233 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3234 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3235 +       } else {
3236 +               h_dentry = au_br_dentry(br);
3237 +               h_inode = d_inode(h_dentry);
3238 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3239 +       }
3240 +       if (!wbr)
3241 +               err = au_wh_init(br, sb);
3242 +       else {
3243 +               wbr_wh_write_lock(wbr);
3244 +               err = au_wh_init(br, sb);
3245 +               wbr_wh_write_unlock(wbr);
3246 +       }
3247 +       if (hdir)
3248 +               au_hn_inode_unlock(hdir);
3249 +       else
3250 +               inode_unlock(h_inode);
3251 +       vfsub_mnt_drop_write(au_br_mnt(br));
3252 +       br->br_perm = old_perm;
3253 +
3254 +       if (!err && wbr && !au_br_writable(new_perm)) {
3255 +               au_kfree_rcu(wbr);
3256 +               br->br_wbr = NULL;
3257 +       }
3258 +
3259 +out:
3260 +       return err;
3261 +}
3262 +
3263 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3264 +                      int perm)
3265 +{
3266 +       int err;
3267 +       struct kstatfs kst;
3268 +       struct au_wbr *wbr;
3269 +
3270 +       wbr = br->br_wbr;
3271 +       au_rw_init(&wbr->wbr_wh_rwsem);
3272 +       atomic_set(&wbr->wbr_wh_running, 0);
3273 +
3274 +       /*
3275 +        * a limit for rmdir/rename a dir
3276 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3277 +        */
3278 +       err = vfs_statfs(&br->br_path, &kst);
3279 +       if (unlikely(err))
3280 +               goto out;
3281 +       err = -EINVAL;
3282 +       if (kst.f_namelen >= NAME_MAX)
3283 +               err = au_br_init_wh(sb, br, perm);
3284 +       else
3285 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3286 +                      au_br_dentry(br),
3287 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3288 +
3289 +out:
3290 +       return err;
3291 +}
3292 +
3293 +/* initialize a new branch */
3294 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3295 +                     struct au_opt_add *add)
3296 +{
3297 +       int err;
3298 +       struct au_branch *brbase;
3299 +       struct file *xf;
3300 +       struct inode *h_inode;
3301 +
3302 +       err = 0;
3303 +       br->br_perm = add->perm;
3304 +       br->br_path = add->path; /* set first, path_get() later */
3305 +       spin_lock_init(&br->br_dykey_lock);
3306 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3307 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3308 +       br->br_id = au_new_br_id(sb);
3309 +       AuDebugOn(br->br_id < 0);
3310 +
3311 +       /* always, regardless the given option */
3312 +       err = au_dr_br_init(sb, br, &add->path);
3313 +       if (unlikely(err))
3314 +               goto out_err;
3315 +
3316 +       if (au_br_writable(add->perm)) {
3317 +               err = au_wbr_init(br, sb, add->perm);
3318 +               if (unlikely(err))
3319 +                       goto out_err;
3320 +       }
3321 +
3322 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3323 +               brbase = au_sbr(sb, 0);
3324 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3325 +               AuDebugOn(!xf);
3326 +               h_inode = d_inode(add->path.dentry);
3327 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3328 +               if (unlikely(err)) {
3329 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3330 +                       goto out_err;
3331 +               }
3332 +       }
3333 +
3334 +       sysaufs_br_init(br);
3335 +       path_get(&br->br_path);
3336 +       goto out; /* success */
3337 +
3338 +out_err:
3339 +       memset(&br->br_path, 0, sizeof(br->br_path));
3340 +out:
3341 +       return err;
3342 +}
3343 +
3344 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3345 +                            struct au_branch *br, aufs_bindex_t bbot,
3346 +                            aufs_bindex_t amount)
3347 +{
3348 +       struct au_branch **brp;
3349 +
3350 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3351 +
3352 +       brp = sbinfo->si_branch + bindex;
3353 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3354 +       *brp = br;
3355 +       sbinfo->si_bbot++;
3356 +       if (unlikely(bbot < 0))
3357 +               sbinfo->si_bbot = 0;
3358 +}
3359 +
3360 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3361 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3362 +{
3363 +       struct au_hdentry *hdp;
3364 +
3365 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3366 +
3367 +       hdp = au_hdentry(dinfo, bindex);
3368 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3369 +       au_h_dentry_init(hdp);
3370 +       dinfo->di_bbot++;
3371 +       if (unlikely(bbot < 0))
3372 +               dinfo->di_btop = 0;
3373 +}
3374 +
3375 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3376 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3377 +{
3378 +       struct au_hinode *hip;
3379 +
3380 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3381 +
3382 +       hip = au_hinode(iinfo, bindex);
3383 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3384 +       au_hinode_init(hip);
3385 +       iinfo->ii_bbot++;
3386 +       if (unlikely(bbot < 0))
3387 +               iinfo->ii_btop = 0;
3388 +}
3389 +
3390 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3391 +                        aufs_bindex_t bindex)
3392 +{
3393 +       struct dentry *root, *h_dentry;
3394 +       struct inode *root_inode, *h_inode;
3395 +       aufs_bindex_t bbot, amount;
3396 +
3397 +       root = sb->s_root;
3398 +       root_inode = d_inode(root);
3399 +       bbot = au_sbbot(sb);
3400 +       amount = bbot + 1 - bindex;
3401 +       h_dentry = au_br_dentry(br);
3402 +       au_sbilist_lock();
3403 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3404 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3405 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3406 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3407 +       h_inode = d_inode(h_dentry);
3408 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3409 +       au_sbilist_unlock();
3410 +}
3411 +
3412 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3413 +{
3414 +       int err;
3415 +       aufs_bindex_t bbot, add_bindex;
3416 +       struct dentry *root, *h_dentry;
3417 +       struct inode *root_inode;
3418 +       struct au_branch *add_branch;
3419 +
3420 +       root = sb->s_root;
3421 +       root_inode = d_inode(root);
3422 +       IMustLock(root_inode);
3423 +       IiMustWriteLock(root_inode);
3424 +       err = test_add(sb, add, remount);
3425 +       if (unlikely(err < 0))
3426 +               goto out;
3427 +       if (err) {
3428 +               err = 0;
3429 +               goto out; /* success */
3430 +       }
3431 +
3432 +       bbot = au_sbbot(sb);
3433 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3434 +       err = PTR_ERR(add_branch);
3435 +       if (IS_ERR(add_branch))
3436 +               goto out;
3437 +
3438 +       err = au_br_init(add_branch, sb, add);
3439 +       if (unlikely(err)) {
3440 +               au_br_do_free(add_branch);
3441 +               goto out;
3442 +       }
3443 +
3444 +       add_bindex = add->bindex;
3445 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3446 +       au_br_do_add(sb, add_branch, add_bindex);
3447 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3448 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3449 +
3450 +       h_dentry = add->path.dentry;
3451 +       if (!add_bindex) {
3452 +               au_cpup_attr_all(root_inode, /*force*/1);
3453 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3454 +       } else
3455 +               au_add_nlink(root_inode, d_inode(h_dentry));
3456 +
3457 +out:
3458 +       return err;
3459 +}
3460 +
3461 +/* ---------------------------------------------------------------------- */
3462 +
3463 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3464 +                                      unsigned long long max __maybe_unused,
3465 +                                      void *arg)
3466 +{
3467 +       unsigned long long n;
3468 +       struct file **p, *f;
3469 +       struct hlist_bl_head *files;
3470 +       struct hlist_bl_node *pos;
3471 +       struct au_finfo *finfo;
3472 +
3473 +       n = 0;
3474 +       p = a;
3475 +       files = &au_sbi(sb)->si_files;
3476 +       hlist_bl_lock(files);
3477 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3478 +               f = finfo->fi_file;
3479 +               if (file_count(f)
3480 +                   && !special_file(file_inode(f)->i_mode)) {
3481 +                       get_file(f);
3482 +                       *p++ = f;
3483 +                       n++;
3484 +                       AuDebugOn(n > max);
3485 +               }
3486 +       }
3487 +       hlist_bl_unlock(files);
3488 +
3489 +       return n;
3490 +}
3491 +
3492 +static struct file **au_farray_alloc(struct super_block *sb,
3493 +                                    unsigned long long *max)
3494 +{
3495 +       struct au_sbinfo *sbi;
3496 +
3497 +       sbi = au_sbi(sb);
3498 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3499 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3500 +}
3501 +
3502 +static void au_farray_free(struct file **a, unsigned long long max)
3503 +{
3504 +       unsigned long long ull;
3505 +
3506 +       for (ull = 0; ull < max; ull++)
3507 +               if (a[ull])
3508 +                       fput(a[ull]);
3509 +       kvfree(a);
3510 +}
3511 +
3512 +/* ---------------------------------------------------------------------- */
3513 +
3514 +/*
3515 + * delete a branch
3516 + */
3517 +
3518 +/* to show the line number, do not make it inlined function */
3519 +#define AuVerbose(do_info, fmt, ...) do { \
3520 +       if (do_info) \
3521 +               pr_info(fmt, ##__VA_ARGS__); \
3522 +} while (0)
3523 +
3524 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3525 +                        aufs_bindex_t bbot)
3526 +{
3527 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3528 +}
3529 +
3530 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3531 +                        aufs_bindex_t bbot)
3532 +{
3533 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3534 +}
3535 +
3536 +/*
3537 + * test if the branch is deletable or not.
3538 + */
3539 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3540 +                           unsigned int sigen, const unsigned int verbose)
3541 +{
3542 +       int err, i, j, ndentry;
3543 +       aufs_bindex_t btop, bbot;
3544 +       struct au_dcsub_pages dpages;
3545 +       struct au_dpage *dpage;
3546 +       struct dentry *d;
3547 +
3548 +       err = au_dpages_init(&dpages, GFP_NOFS);
3549 +       if (unlikely(err))
3550 +               goto out;
3551 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3552 +       if (unlikely(err))
3553 +               goto out_dpages;
3554 +
3555 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3556 +               dpage = dpages.dpages + i;
3557 +               ndentry = dpage->ndentry;
3558 +               for (j = 0; !err && j < ndentry; j++) {
3559 +                       d = dpage->dentries[j];
3560 +                       AuDebugOn(au_dcount(d) <= 0);
3561 +                       if (!au_digen_test(d, sigen)) {
3562 +                               di_read_lock_child(d, AuLock_IR);
3563 +                               if (unlikely(au_dbrange_test(d))) {
3564 +                                       di_read_unlock(d, AuLock_IR);
3565 +                                       continue;
3566 +                               }
3567 +                       } else {
3568 +                               di_write_lock_child(d);
3569 +                               if (unlikely(au_dbrange_test(d))) {
3570 +                                       di_write_unlock(d);
3571 +                                       continue;
3572 +                               }
3573 +                               err = au_reval_dpath(d, sigen);
3574 +                               if (!err)
3575 +                                       di_downgrade_lock(d, AuLock_IR);
3576 +                               else {
3577 +                                       di_write_unlock(d);
3578 +                                       break;
3579 +                               }
3580 +                       }
3581 +
3582 +                       /* AuDbgDentry(d); */
3583 +                       btop = au_dbtop(d);
3584 +                       bbot = au_dbbot(d);
3585 +                       if (btop <= bindex
3586 +                           && bindex <= bbot
3587 +                           && au_h_dptr(d, bindex)
3588 +                           && au_test_dbusy(d, btop, bbot)) {
3589 +                               err = -EBUSY;
3590 +                               AuVerbose(verbose, "busy %pd\n", d);
3591 +                               AuDbgDentry(d);
3592 +                       }
3593 +                       di_read_unlock(d, AuLock_IR);
3594 +               }
3595 +       }
3596 +
3597 +out_dpages:
3598 +       au_dpages_free(&dpages);
3599 +out:
3600 +       return err;
3601 +}
3602 +
3603 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3604 +                          unsigned int sigen, const unsigned int verbose)
3605 +{
3606 +       int err;
3607 +       unsigned long long max, ull;
3608 +       struct inode *i, **array;
3609 +       aufs_bindex_t btop, bbot;
3610 +
3611 +       array = au_iarray_alloc(sb, &max);
3612 +       err = PTR_ERR(array);
3613 +       if (IS_ERR(array))
3614 +               goto out;
3615 +
3616 +       err = 0;
3617 +       AuDbg("b%d\n", bindex);
3618 +       for (ull = 0; !err && ull < max; ull++) {
3619 +               i = array[ull];
3620 +               if (unlikely(!i))
3621 +                       break;
3622 +               if (i->i_ino == AUFS_ROOT_INO)
3623 +                       continue;
3624 +
3625 +               /* AuDbgInode(i); */
3626 +               if (au_iigen(i, NULL) == sigen)
3627 +                       ii_read_lock_child(i);
3628 +               else {
3629 +                       ii_write_lock_child(i);
3630 +                       err = au_refresh_hinode_self(i);
3631 +                       au_iigen_dec(i);
3632 +                       if (!err)
3633 +                               ii_downgrade_lock(i);
3634 +                       else {
3635 +                               ii_write_unlock(i);
3636 +                               break;
3637 +                       }
3638 +               }
3639 +
3640 +               btop = au_ibtop(i);
3641 +               bbot = au_ibbot(i);
3642 +               if (btop <= bindex
3643 +                   && bindex <= bbot
3644 +                   && au_h_iptr(i, bindex)
3645 +                   && au_test_ibusy(i, btop, bbot)) {
3646 +                       err = -EBUSY;
3647 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3648 +                       AuDbgInode(i);
3649 +               }
3650 +               ii_read_unlock(i);
3651 +       }
3652 +       au_iarray_free(array, max);
3653 +
3654 +out:
3655 +       return err;
3656 +}
3657 +
3658 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3659 +                             const unsigned int verbose)
3660 +{
3661 +       int err;
3662 +       unsigned int sigen;
3663 +
3664 +       sigen = au_sigen(root->d_sb);
3665 +       DiMustNoWaiters(root);
3666 +       IiMustNoWaiters(d_inode(root));
3667 +       di_write_unlock(root);
3668 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3669 +       if (!err)
3670 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3671 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3672 +
3673 +       return err;
3674 +}
3675 +
3676 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3677 +                        struct file **to_free, int *idx)
3678 +{
3679 +       int err;
3680 +       unsigned char matched, root;
3681 +       aufs_bindex_t bindex, bbot;
3682 +       struct au_fidir *fidir;
3683 +       struct au_hfile *hfile;
3684 +
3685 +       err = 0;
3686 +       root = IS_ROOT(file->f_path.dentry);
3687 +       if (root) {
3688 +               get_file(file);
3689 +               to_free[*idx] = file;
3690 +               (*idx)++;
3691 +               goto out;
3692 +       }
3693 +
3694 +       matched = 0;
3695 +       fidir = au_fi(file)->fi_hdir;
3696 +       AuDebugOn(!fidir);
3697 +       bbot = au_fbbot_dir(file);
3698 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3699 +               hfile = fidir->fd_hfile + bindex;
3700 +               if (!hfile->hf_file)
3701 +                       continue;
3702 +
3703 +               if (hfile->hf_br->br_id == br_id) {
3704 +                       matched = 1;
3705 +                       break;
3706 +               }
3707 +       }
3708 +       if (matched)
3709 +               err = -EBUSY;
3710 +
3711 +out:
3712 +       return err;
3713 +}
3714 +
3715 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3716 +                         struct file **to_free, int opened)
3717 +{
3718 +       int err, idx;
3719 +       unsigned long long ull, max;
3720 +       aufs_bindex_t btop;
3721 +       struct file *file, **array;
3722 +       struct dentry *root;
3723 +       struct au_hfile *hfile;
3724 +
3725 +       array = au_farray_alloc(sb, &max);
3726 +       err = PTR_ERR(array);
3727 +       if (IS_ERR(array))
3728 +               goto out;
3729 +
3730 +       err = 0;
3731 +       idx = 0;
3732 +       root = sb->s_root;
3733 +       di_write_unlock(root);
3734 +       for (ull = 0; ull < max; ull++) {
3735 +               file = array[ull];
3736 +               if (unlikely(!file))
3737 +                       break;
3738 +
3739 +               /* AuDbg("%pD\n", file); */
3740 +               fi_read_lock(file);
3741 +               btop = au_fbtop(file);
3742 +               if (!d_is_dir(file->f_path.dentry)) {
3743 +                       hfile = &au_fi(file)->fi_htop;
3744 +                       if (hfile->hf_br->br_id == br_id)
3745 +                               err = -EBUSY;
3746 +               } else
3747 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3748 +               fi_read_unlock(file);
3749 +               if (unlikely(err))
3750 +                       break;
3751 +       }
3752 +       di_write_lock_child(root);
3753 +       au_farray_free(array, max);
3754 +       AuDebugOn(idx > opened);
3755 +
3756 +out:
3757 +       return err;
3758 +}
3759 +
3760 +static void br_del_file(struct file **to_free, unsigned long long opened,
3761 +                       aufs_bindex_t br_id)
3762 +{
3763 +       unsigned long long ull;
3764 +       aufs_bindex_t bindex, btop, bbot, bfound;
3765 +       struct file *file;
3766 +       struct au_fidir *fidir;
3767 +       struct au_hfile *hfile;
3768 +
3769 +       for (ull = 0; ull < opened; ull++) {
3770 +               file = to_free[ull];
3771 +               if (unlikely(!file))
3772 +                       break;
3773 +
3774 +               /* AuDbg("%pD\n", file); */
3775 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3776 +               bfound = -1;
3777 +               fidir = au_fi(file)->fi_hdir;
3778 +               AuDebugOn(!fidir);
3779 +               fi_write_lock(file);
3780 +               btop = au_fbtop(file);
3781 +               bbot = au_fbbot_dir(file);
3782 +               for (bindex = btop; bindex <= bbot; bindex++) {
3783 +                       hfile = fidir->fd_hfile + bindex;
3784 +                       if (!hfile->hf_file)
3785 +                               continue;
3786 +
3787 +                       if (hfile->hf_br->br_id == br_id) {
3788 +                               bfound = bindex;
3789 +                               break;
3790 +                       }
3791 +               }
3792 +               AuDebugOn(bfound < 0);
3793 +               au_set_h_fptr(file, bfound, NULL);
3794 +               if (bfound == btop) {
3795 +                       for (btop++; btop <= bbot; btop++)
3796 +                               if (au_hf_dir(file, btop)) {
3797 +                                       au_set_fbtop(file, btop);
3798 +                                       break;
3799 +                               }
3800 +               }
3801 +               fi_write_unlock(file);
3802 +       }
3803 +}
3804 +
3805 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3806 +                            const aufs_bindex_t bindex,
3807 +                            const aufs_bindex_t bbot)
3808 +{
3809 +       struct au_branch **brp, **p;
3810 +
3811 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3812 +
3813 +       brp = sbinfo->si_branch + bindex;
3814 +       if (bindex < bbot)
3815 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3816 +       sbinfo->si_branch[0 + bbot] = NULL;
3817 +       sbinfo->si_bbot--;
3818 +
3819 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3820 +                       /*may_shrink*/1);
3821 +       if (p)
3822 +               sbinfo->si_branch = p;
3823 +       /* harmless error */
3824 +}
3825 +
3826 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3827 +                            const aufs_bindex_t bbot)
3828 +{
3829 +       struct au_hdentry *hdp, *p;
3830 +
3831 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3832 +
3833 +       hdp = au_hdentry(dinfo, bindex);
3834 +       if (bindex < bbot)
3835 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3836 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3837 +       dinfo->di_bbot--;
3838 +
3839 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3840 +                       /*may_shrink*/1);
3841 +       if (p)
3842 +               dinfo->di_hdentry = p;
3843 +       /* harmless error */
3844 +}
3845 +
3846 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3847 +                            const aufs_bindex_t bbot)
3848 +{
3849 +       struct au_hinode *hip, *p;
3850 +
3851 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3852 +
3853 +       hip = au_hinode(iinfo, bindex);
3854 +       if (bindex < bbot)
3855 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3856 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3857 +       iinfo->ii_bbot--;
3858 +
3859 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3860 +                       /*may_shrink*/1);
3861 +       if (p)
3862 +               iinfo->ii_hinode = p;
3863 +       /* harmless error */
3864 +}
3865 +
3866 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3867 +                        struct au_branch *br)
3868 +{
3869 +       aufs_bindex_t bbot;
3870 +       struct au_sbinfo *sbinfo;
3871 +       struct dentry *root, *h_root;
3872 +       struct inode *inode, *h_inode;
3873 +       struct au_hinode *hinode;
3874 +
3875 +       SiMustWriteLock(sb);
3876 +
3877 +       root = sb->s_root;
3878 +       inode = d_inode(root);
3879 +       sbinfo = au_sbi(sb);
3880 +       bbot = sbinfo->si_bbot;
3881 +
3882 +       h_root = au_h_dptr(root, bindex);
3883 +       hinode = au_hi(inode, bindex);
3884 +       h_inode = au_igrab(hinode->hi_inode);
3885 +       au_hiput(hinode);
3886 +
3887 +       au_sbilist_lock();
3888 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3889 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3890 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3891 +       au_sbilist_unlock();
3892 +
3893 +       /* ignore an error */
3894 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3895 +
3896 +       dput(h_root);
3897 +       iput(h_inode);
3898 +       au_br_do_free(br);
3899 +}
3900 +
3901 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3902 +                                  unsigned long long max, void *arg)
3903 +{
3904 +       return max;
3905 +}
3906 +
3907 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3908 +{
3909 +       int err, rerr, i;
3910 +       unsigned long long opened;
3911 +       unsigned int mnt_flags;
3912 +       aufs_bindex_t bindex, bbot, br_id;
3913 +       unsigned char do_wh, verbose;
3914 +       struct au_branch *br;
3915 +       struct au_wbr *wbr;
3916 +       struct dentry *root;
3917 +       struct file **to_free;
3918 +
3919 +       err = 0;
3920 +       opened = 0;
3921 +       to_free = NULL;
3922 +       root = sb->s_root;
3923 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3924 +       if (bindex < 0) {
3925 +               if (remount)
3926 +                       goto out; /* success */
3927 +               err = -ENOENT;
3928 +               pr_err("%s no such branch\n", del->pathname);
3929 +               goto out;
3930 +       }
3931 +       AuDbg("bindex b%d\n", bindex);
3932 +
3933 +       err = -EBUSY;
3934 +       mnt_flags = au_mntflags(sb);
3935 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3936 +       bbot = au_sbbot(sb);
3937 +       if (unlikely(!bbot)) {
3938 +               AuVerbose(verbose, "no more branches left\n");
3939 +               goto out;
3940 +       }
3941 +
3942 +       br = au_sbr(sb, bindex);
3943 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3944 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3945 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3946 +               goto out;
3947 +       }
3948 +
3949 +       br_id = br->br_id;
3950 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3951 +       if (unlikely(opened)) {
3952 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3953 +               err = PTR_ERR(to_free);
3954 +               if (IS_ERR(to_free))
3955 +                       goto out;
3956 +
3957 +               err = test_file_busy(sb, br_id, to_free, opened);
3958 +               if (unlikely(err)) {
3959 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3960 +                       goto out;
3961 +               }
3962 +       }
3963 +
3964 +       wbr = br->br_wbr;
3965 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3966 +       if (do_wh) {
3967 +               /* instead of WbrWhMustWriteLock(wbr) */
3968 +               SiMustWriteLock(sb);
3969 +               for (i = 0; i < AuBrWh_Last; i++) {
3970 +                       dput(wbr->wbr_wh[i]);
3971 +                       wbr->wbr_wh[i] = NULL;
3972 +               }
3973 +       }
3974 +
3975 +       err = test_children_busy(root, bindex, verbose);
3976 +       if (unlikely(err)) {
3977 +               if (do_wh)
3978 +                       goto out_wh;
3979 +               goto out;
3980 +       }
3981 +
3982 +       err = 0;
3983 +       if (to_free) {
3984 +               /*
3985 +                * now we confirmed the branch is deletable.
3986 +                * let's free the remaining opened dirs on the branch.
3987 +                */
3988 +               di_write_unlock(root);
3989 +               br_del_file(to_free, opened, br_id);
3990 +               di_write_lock_child(root);
3991 +       }
3992 +
3993 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3994 +       dbgaufs_xino_del(br);           /* remove one */
3995 +       au_br_do_del(sb, bindex, br);
3996 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3997 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
3998 +
3999 +       if (!bindex) {
4000 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4001 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4002 +       } else
4003 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4004 +       if (au_opt_test(mnt_flags, PLINK))
4005 +               au_plink_half_refresh(sb, br_id);
4006 +
4007 +       goto out; /* success */
4008 +
4009 +out_wh:
4010 +       /* revert */
4011 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4012 +       if (rerr)
4013 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4014 +                       del->pathname, rerr);
4015 +out:
4016 +       if (to_free)
4017 +               au_farray_free(to_free, opened);
4018 +       return err;
4019 +}
4020 +
4021 +/* ---------------------------------------------------------------------- */
4022 +
4023 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4024 +{
4025 +       int err;
4026 +       aufs_bindex_t btop, bbot;
4027 +       struct aufs_ibusy ibusy;
4028 +       struct inode *inode, *h_inode;
4029 +
4030 +       err = -EPERM;
4031 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4032 +               goto out;
4033 +
4034 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4035 +       if (!err)
4036 +               /* VERIFY_WRITE */
4037 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4038 +       if (unlikely(err)) {
4039 +               err = -EFAULT;
4040 +               AuTraceErr(err);
4041 +               goto out;
4042 +       }
4043 +
4044 +       err = -EINVAL;
4045 +       si_read_lock(sb, AuLock_FLUSH);
4046 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4047 +               goto out_unlock;
4048 +
4049 +       err = 0;
4050 +       ibusy.h_ino = 0; /* invalid */
4051 +       inode = ilookup(sb, ibusy.ino);
4052 +       if (!inode
4053 +           || inode->i_ino == AUFS_ROOT_INO
4054 +           || au_is_bad_inode(inode))
4055 +               goto out_unlock;
4056 +
4057 +       ii_read_lock_child(inode);
4058 +       btop = au_ibtop(inode);
4059 +       bbot = au_ibbot(inode);
4060 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4061 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4062 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4063 +                       ibusy.h_ino = h_inode->i_ino;
4064 +       }
4065 +       ii_read_unlock(inode);
4066 +       iput(inode);
4067 +
4068 +out_unlock:
4069 +       si_read_unlock(sb);
4070 +       if (!err) {
4071 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4072 +               if (unlikely(err)) {
4073 +                       err = -EFAULT;
4074 +                       AuTraceErr(err);
4075 +               }
4076 +       }
4077 +out:
4078 +       return err;
4079 +}
4080 +
4081 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4082 +{
4083 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4084 +}
4085 +
4086 +#ifdef CONFIG_COMPAT
4087 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4088 +{
4089 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4090 +}
4091 +#endif
4092 +
4093 +/* ---------------------------------------------------------------------- */
4094 +
4095 +/*
4096 + * change a branch permission
4097 + */
4098 +
4099 +static void au_warn_ima(void)
4100 +{
4101 +#ifdef CONFIG_IMA
4102 +       /* since it doesn't support mark_files_ro() */
4103 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4104 +#endif
4105 +}
4106 +
4107 +static int do_need_sigen_inc(int a, int b)
4108 +{
4109 +       return au_br_whable(a) && !au_br_whable(b);
4110 +}
4111 +
4112 +static int need_sigen_inc(int old, int new)
4113 +{
4114 +       return do_need_sigen_inc(old, new)
4115 +               || do_need_sigen_inc(new, old);
4116 +}
4117 +
4118 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4119 +{
4120 +       int err, do_warn;
4121 +       unsigned int mnt_flags;
4122 +       unsigned long long ull, max;
4123 +       aufs_bindex_t br_id;
4124 +       unsigned char verbose, writer;
4125 +       struct file *file, *hf, **array;
4126 +       struct au_hfile *hfile;
4127 +       struct inode *h_inode;
4128 +
4129 +       mnt_flags = au_mntflags(sb);
4130 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4131 +
4132 +       array = au_farray_alloc(sb, &max);
4133 +       err = PTR_ERR(array);
4134 +       if (IS_ERR(array))
4135 +               goto out;
4136 +
4137 +       do_warn = 0;
4138 +       br_id = au_sbr_id(sb, bindex);
4139 +       for (ull = 0; ull < max; ull++) {
4140 +               file = array[ull];
4141 +               if (unlikely(!file))
4142 +                       break;
4143 +
4144 +               /* AuDbg("%pD\n", file); */
4145 +               fi_read_lock(file);
4146 +               if (unlikely(au_test_mmapped(file))) {
4147 +                       err = -EBUSY;
4148 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4149 +                       AuDbgFile(file);
4150 +                       FiMustNoWaiters(file);
4151 +                       fi_read_unlock(file);
4152 +                       goto out_array;
4153 +               }
4154 +
4155 +               hfile = &au_fi(file)->fi_htop;
4156 +               hf = hfile->hf_file;
4157 +               if (!d_is_reg(file->f_path.dentry)
4158 +                   || !(file->f_mode & FMODE_WRITE)
4159 +                   || hfile->hf_br->br_id != br_id
4160 +                   || !(hf->f_mode & FMODE_WRITE))
4161 +                       array[ull] = NULL;
4162 +               else {
4163 +                       do_warn = 1;
4164 +                       get_file(file);
4165 +               }
4166 +
4167 +               FiMustNoWaiters(file);
4168 +               fi_read_unlock(file);
4169 +               fput(file);
4170 +       }
4171 +
4172 +       err = 0;
4173 +       if (do_warn)
4174 +               au_warn_ima();
4175 +
4176 +       for (ull = 0; ull < max; ull++) {
4177 +               file = array[ull];
4178 +               if (!file)
4179 +                       continue;
4180 +
4181 +               /* todo: already flushed? */
4182 +               /*
4183 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4184 +                * approach which resets f_mode and calls mnt_drop_write() and
4185 +                * file_release_write() for each file, because the branch
4186 +                * attribute in aufs world is totally different from the native
4187 +                * fs rw/ro mode.
4188 +                */
4189 +               /* fi_read_lock(file); */
4190 +               hfile = &au_fi(file)->fi_htop;
4191 +               hf = hfile->hf_file;
4192 +               /* fi_read_unlock(file); */
4193 +               spin_lock(&hf->f_lock);
4194 +               writer = !!(hf->f_mode & FMODE_WRITER);
4195 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4196 +               spin_unlock(&hf->f_lock);
4197 +               if (writer) {
4198 +                       h_inode = file_inode(hf);
4199 +                       if (hf->f_mode & FMODE_READ)
4200 +                               i_readcount_inc(h_inode);
4201 +                       put_write_access(h_inode);
4202 +                       mnt_put_write_access(hf->f_path.mnt);
4203 +               }
4204 +       }
4205 +
4206 +out_array:
4207 +       au_farray_free(array, max);
4208 +out:
4209 +       AuTraceErr(err);
4210 +       return err;
4211 +}
4212 +
4213 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4214 +             int *do_refresh)
4215 +{
4216 +       int err, rerr;
4217 +       aufs_bindex_t bindex;
4218 +       struct dentry *root;
4219 +       struct au_branch *br;
4220 +       struct au_br_fhsm *bf;
4221 +
4222 +       root = sb->s_root;
4223 +       bindex = au_find_dbindex(root, mod->h_root);
4224 +       if (bindex < 0) {
4225 +               if (remount)
4226 +                       return 0; /* success */
4227 +               err = -ENOENT;
4228 +               pr_err("%s no such branch\n", mod->path);
4229 +               goto out;
4230 +       }
4231 +       AuDbg("bindex b%d\n", bindex);
4232 +
4233 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4234 +       if (unlikely(err))
4235 +               goto out;
4236 +
4237 +       br = au_sbr(sb, bindex);
4238 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4239 +       if (br->br_perm == mod->perm)
4240 +               return 0; /* success */
4241 +
4242 +       /* pre-allocate for non-fhsm --> fhsm */
4243 +       bf = NULL;
4244 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4245 +               err = au_fhsm_br_alloc(br);
4246 +               if (unlikely(err))
4247 +                       goto out;
4248 +               bf = br->br_fhsm;
4249 +               br->br_fhsm = NULL;
4250 +       }
4251 +
4252 +       if (au_br_writable(br->br_perm)) {
4253 +               /* remove whiteout base */
4254 +               err = au_br_init_wh(sb, br, mod->perm);
4255 +               if (unlikely(err))
4256 +                       goto out_bf;
4257 +
4258 +               if (!au_br_writable(mod->perm)) {
4259 +                       /* rw --> ro, file might be mmapped */
4260 +                       DiMustNoWaiters(root);
4261 +                       IiMustNoWaiters(d_inode(root));
4262 +                       di_write_unlock(root);
4263 +                       err = au_br_mod_files_ro(sb, bindex);
4264 +                       /* aufs_write_lock() calls ..._child() */
4265 +                       di_write_lock_child(root);
4266 +
4267 +                       if (unlikely(err)) {
4268 +                               rerr = -ENOMEM;
4269 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4270 +                                                    GFP_NOFS);
4271 +                               if (br->br_wbr)
4272 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4273 +                               if (unlikely(rerr)) {
4274 +                                       AuIOErr("nested error %d (%d)\n",
4275 +                                               rerr, err);
4276 +                                       br->br_perm = mod->perm;
4277 +                               }
4278 +                       }
4279 +               }
4280 +       } else if (au_br_writable(mod->perm)) {
4281 +               /* ro --> rw */
4282 +               err = -ENOMEM;
4283 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4284 +               if (br->br_wbr) {
4285 +                       err = au_wbr_init(br, sb, mod->perm);
4286 +                       if (unlikely(err)) {
4287 +                               au_kfree_rcu(br->br_wbr);
4288 +                               br->br_wbr = NULL;
4289 +                       }
4290 +               }
4291 +       }
4292 +       if (unlikely(err))
4293 +               goto out_bf;
4294 +
4295 +       if (au_br_fhsm(br->br_perm)) {
4296 +               if (!au_br_fhsm(mod->perm)) {
4297 +                       /* fhsm --> non-fhsm */
4298 +                       au_br_fhsm_fin(br->br_fhsm);
4299 +                       au_kfree_rcu(br->br_fhsm);
4300 +                       br->br_fhsm = NULL;
4301 +               }
4302 +       } else if (au_br_fhsm(mod->perm))
4303 +               /* non-fhsm --> fhsm */
4304 +               br->br_fhsm = bf;
4305 +
4306 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4307 +       br->br_perm = mod->perm;
4308 +       goto out; /* success */
4309 +
4310 +out_bf:
4311 +       au_kfree_try_rcu(bf);
4312 +out:
4313 +       AuTraceErr(err);
4314 +       return err;
4315 +}
4316 +
4317 +/* ---------------------------------------------------------------------- */
4318 +
4319 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4320 +{
4321 +       int err;
4322 +       struct kstatfs kstfs;
4323 +
4324 +       err = vfs_statfs(&br->br_path, &kstfs);
4325 +       if (!err) {
4326 +               stfs->f_blocks = kstfs.f_blocks;
4327 +               stfs->f_bavail = kstfs.f_bavail;
4328 +               stfs->f_files = kstfs.f_files;
4329 +               stfs->f_ffree = kstfs.f_ffree;
4330 +       }
4331 +
4332 +       return err;
4333 +}
4334 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4335 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4336 +++ linux/fs/aufs/branch.h      2024-05-13 17:13:00.618070924 +0200
4337 @@ -0,0 +1,375 @@
4338 +/* SPDX-License-Identifier: GPL-2.0 */
4339 +/*
4340 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4341 + *
4342 + * This program is free software; you can redistribute it and/or modify
4343 + * it under the terms of the GNU General Public License as published by
4344 + * the Free Software Foundation; either version 2 of the License, or
4345 + * (at your option) any later version.
4346 + *
4347 + * This program is distributed in the hope that it will be useful,
4348 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4349 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4350 + * GNU General Public License for more details.
4351 + *
4352 + * You should have received a copy of the GNU General Public License
4353 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4354 + */
4355 +
4356 +/*
4357 + * branch filesystems and xino for them
4358 + */
4359 +
4360 +#ifndef __AUFS_BRANCH_H__
4361 +#define __AUFS_BRANCH_H__
4362 +
4363 +#ifdef __KERNEL__
4364 +
4365 +#include <linux/mount.h>
4366 +#include "dirren.h"
4367 +#include "dynop.h"
4368 +#include "lcnt.h"
4369 +#include "rwsem.h"
4370 +#include "super.h"
4371 +
4372 +/* ---------------------------------------------------------------------- */
4373 +
4374 +/* a xino file */
4375 +struct au_xino {
4376 +       struct file             **xi_file;
4377 +       unsigned int            xi_nfile;
4378 +
4379 +       struct {
4380 +               spinlock_t              spin;
4381 +               ino_t                   *array;
4382 +               int                     total;
4383 +               /* reserved for future use */
4384 +               /* unsigned long        *bitmap; */
4385 +               wait_queue_head_t       wqh;
4386 +       } xi_nondir;
4387 +
4388 +       struct mutex            xi_mtx; /* protects xi_file array */
4389 +       struct hlist_bl_head    xi_writing;
4390 +
4391 +       atomic_t                xi_truncating;
4392 +
4393 +       struct kref             xi_kref;
4394 +};
4395 +
4396 +/* File-based Hierarchical Storage Management */
4397 +struct au_br_fhsm {
4398 +#ifdef CONFIG_AUFS_FHSM
4399 +       struct mutex            bf_lock;
4400 +       unsigned long           bf_jiffy;
4401 +       struct aufs_stfs        bf_stfs;
4402 +       int                     bf_readable;
4403 +#endif
4404 +};
4405 +
4406 +/* members for writable branch only */
4407 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4408 +struct au_wbr {
4409 +       struct au_rwsem         wbr_wh_rwsem;
4410 +       struct dentry           *wbr_wh[AuBrWh_Last];
4411 +       atomic_t                wbr_wh_running;
4412 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4413 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4414 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4415 +
4416 +       /* mfs mode */
4417 +       unsigned long long      wbr_bytes;
4418 +};
4419 +
4420 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4421 +#define AuBrDynOp (AuDyLast * 4)
4422 +
4423 +#ifdef CONFIG_AUFS_HFSNOTIFY
4424 +/* support for asynchronous destruction */
4425 +struct au_br_hfsnotify {
4426 +       struct fsnotify_group   *hfsn_group;
4427 +};
4428 +#endif
4429 +
4430 +/* sysfs entries */
4431 +struct au_brsysfs {
4432 +       char                    name[16];
4433 +       struct attribute        attr;
4434 +};
4435 +
4436 +enum {
4437 +       AuBrSysfs_BR,
4438 +       AuBrSysfs_BRID,
4439 +       AuBrSysfs_Last
4440 +};
4441 +
4442 +/* protected by superblock rwsem */
4443 +struct au_branch {
4444 +       struct au_xino          *br_xino;
4445 +
4446 +       aufs_bindex_t           br_id;
4447 +
4448 +       int                     br_perm;
4449 +       struct path             br_path;
4450 +       spinlock_t              br_dykey_lock;
4451 +       struct au_dykey         *br_dykey[AuBrDynOp];
4452 +       au_lcnt_t               br_nfiles;      /* opened files */
4453 +       au_lcnt_t               br_count;       /* in-use for other */
4454 +
4455 +       struct au_wbr           *br_wbr;
4456 +       struct au_br_fhsm       *br_fhsm;
4457 +
4458 +#ifdef CONFIG_AUFS_HFSNOTIFY
4459 +       struct au_br_hfsnotify  *br_hfsn;
4460 +#endif
4461 +
4462 +#ifdef CONFIG_SYSFS
4463 +       /* entries under sysfs per mount-point */
4464 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4465 +#endif
4466 +
4467 +#ifdef CONFIG_DEBUG_FS
4468 +       struct dentry            *br_dbgaufs; /* xino */
4469 +#endif
4470 +
4471 +       struct au_dr_br         br_dirren;
4472 +};
4473 +
4474 +/* ---------------------------------------------------------------------- */
4475 +
4476 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4477 +{
4478 +       return br->br_path.mnt;
4479 +}
4480 +
4481 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4482 +{
4483 +       return br->br_path.dentry;
4484 +}
4485 +
4486 +static inline struct mnt_idmap *au_br_idmap(struct au_branch *br)
4487 +{
4488 +       return mnt_idmap(br->br_path.mnt);
4489 +}
4490 +
4491 +static inline struct super_block *au_br_sb(struct au_branch *br)
4492 +{
4493 +       return au_br_mnt(br)->mnt_sb;
4494 +}
4495 +
4496 +static inline int au_br_rdonly(struct au_branch *br)
4497 +{
4498 +       return (sb_rdonly(au_br_sb(br))
4499 +               || !au_br_writable(br->br_perm))
4500 +               ? -EROFS : 0;
4501 +}
4502 +
4503 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4504 +{
4505 +#ifdef CONFIG_AUFS_HNOTIFY
4506 +       return !(brperm & AuBrPerm_RR);
4507 +#else
4508 +       return 0;
4509 +#endif
4510 +}
4511 +
4512 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4513 +{
4514 +       int err, exec_flag;
4515 +
4516 +       err = 0;
4517 +       exec_flag = oflag & __FMODE_EXEC;
4518 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4519 +               err = -EACCES;
4520 +
4521 +       return err;
4522 +}
4523 +
4524 +static inline void au_xino_get(struct au_branch *br)
4525 +{
4526 +       struct au_xino *xi;
4527 +
4528 +       xi = br->br_xino;
4529 +       if (xi)
4530 +               kref_get(&xi->xi_kref);
4531 +}
4532 +
4533 +static inline int au_xino_count(struct au_branch *br)
4534 +{
4535 +       int v;
4536 +       struct au_xino *xi;
4537 +
4538 +       v = 0;
4539 +       xi = br->br_xino;
4540 +       if (xi)
4541 +               v = kref_read(&xi->xi_kref);
4542 +
4543 +       return v;
4544 +}
4545 +
4546 +/* ---------------------------------------------------------------------- */
4547 +
4548 +/* branch.c */
4549 +struct au_sbinfo;
4550 +void au_br_free(struct au_sbinfo *sinfo);
4551 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4552 +struct au_opt_add;
4553 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4554 +struct au_opt_del;
4555 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4556 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4557 +#ifdef CONFIG_COMPAT
4558 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4559 +#endif
4560 +struct au_opt_mod;
4561 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4562 +             int *do_refresh);
4563 +struct aufs_stfs;
4564 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4565 +
4566 +/* xino.c */
4567 +static const loff_t au_loff_max = LLONG_MAX;
4568 +
4569 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4570 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4571 +                           int wbrtop);
4572 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4573 +                            struct file *copy_src);
4574 +struct au_xi_new {
4575 +       struct au_xino *xi;     /* switch between xino and xigen */
4576 +       int idx;
4577 +       struct path *base;
4578 +       struct file *copy_src;
4579 +};
4580 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4581 +
4582 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4583 +                ino_t *ino);
4584 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4585 +                 ino_t ino);
4586 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4587 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4588 +
4589 +int au_xib_trunc(struct super_block *sb);
4590 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4591 +
4592 +struct au_xino *au_xino_alloc(unsigned int nfile);
4593 +int au_xino_put(struct au_branch *br);
4594 +struct file *au_xino_file1(struct au_xino *xi);
4595 +
4596 +struct au_opt_xino;
4597 +void au_xino_clr(struct super_block *sb);
4598 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4599 +struct file *au_xino_def(struct super_block *sb);
4600 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4601 +                   struct path *base);
4602 +
4603 +ino_t au_xino_new_ino(struct super_block *sb);
4604 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4605 +
4606 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4607 +                      ino_t h_ino, int idx);
4608 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4609 +                     int *idx);
4610 +
4611 +int au_xino_path(struct seq_file *seq, struct file *file);
4612 +
4613 +/* ---------------------------------------------------------------------- */
4614 +
4615 +/* @idx is signed to accept -1 meaning the first file */
4616 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4617 +{
4618 +       struct file *file;
4619 +
4620 +       file = NULL;
4621 +       if (!xi)
4622 +               goto out;
4623 +
4624 +       if (idx >= 0) {
4625 +               if (idx < xi->xi_nfile)
4626 +                       file = xi->xi_file[idx];
4627 +       } else
4628 +               file = au_xino_file1(xi);
4629 +
4630 +out:
4631 +       return file;
4632 +}
4633 +
4634 +/* ---------------------------------------------------------------------- */
4635 +
4636 +/* Superblock to branch */
4637 +static inline
4638 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4639 +{
4640 +       return au_sbr(sb, bindex)->br_id;
4641 +}
4642 +
4643 +static inline
4644 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4645 +{
4646 +       return au_br_mnt(au_sbr(sb, bindex));
4647 +}
4648 +
4649 +static inline
4650 +struct mnt_idmap *au_sbr_idmap(struct super_block *sb, aufs_bindex_t bindex)
4651 +{
4652 +       return au_br_idmap(au_sbr(sb, bindex));
4653 +}
4654 +
4655 +static inline
4656 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4657 +{
4658 +       return au_br_sb(au_sbr(sb, bindex));
4659 +}
4660 +
4661 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4662 +{
4663 +       return au_sbr(sb, bindex)->br_perm;
4664 +}
4665 +
4666 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4667 +{
4668 +       return au_br_whable(au_sbr_perm(sb, bindex));
4669 +}
4670 +
4671 +/* ---------------------------------------------------------------------- */
4672 +
4673 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4674 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4675 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4676 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4677 +/*
4678 +#define wbr_wh_read_trylock_nested(wbr) \
4679 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4680 +#define wbr_wh_write_trylock_nested(wbr) \
4681 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4682 +*/
4683 +
4684 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4685 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4686 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4687 +
4688 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4689 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4690 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4691 +
4692 +/* ---------------------------------------------------------------------- */
4693 +
4694 +#ifdef CONFIG_AUFS_FHSM
4695 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4696 +{
4697 +       mutex_init(&brfhsm->bf_lock);
4698 +       brfhsm->bf_jiffy = 0;
4699 +       brfhsm->bf_readable = 0;
4700 +}
4701 +
4702 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4703 +{
4704 +       mutex_destroy(&brfhsm->bf_lock);
4705 +}
4706 +#else
4707 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4708 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4709 +#endif
4710 +
4711 +#endif /* __KERNEL__ */
4712 +#endif /* __AUFS_BRANCH_H__ */
4713 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4714 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4715 +++ linux/fs/aufs/conf.mk       2024-05-13 17:13:00.618070924 +0200
4716 @@ -0,0 +1,40 @@
4717 +# SPDX-License-Identifier: GPL-2.0
4718 +
4719 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4720 +
4721 +define AuConf
4722 +ifdef ${1}
4723 +AuConfStr += ${1}=${${1}}
4724 +endif
4725 +endef
4726 +
4727 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4728 +       SBILIST \
4729 +       HNOTIFY HFSNOTIFY \
4730 +       EXPORT INO_T_64 \
4731 +       XATTR \
4732 +       FHSM \
4733 +       RDU \
4734 +       DIRREN \
4735 +       SHWH \
4736 +       BR_RAMFS \
4737 +       BR_FUSE POLL \
4738 +       BR_HFSPLUS \
4739 +       BDEV_LOOP \
4740 +       DEBUG MAGIC_SYSRQ
4741 +$(foreach i, ${AuConfAll}, \
4742 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4743 +
4744 +AuConfName = ${obj}/conf.str
4745 +${AuConfName}.tmp: FORCE
4746 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4747 +${AuConfName}: ${AuConfName}.tmp
4748 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4749 +       echo '  GEN    ' $@; \
4750 +       cp -p $< $@; \
4751 +       }
4752 +FORCE:
4753 +clean-files += ${AuConfName} ${AuConfName}.tmp
4754 +${obj}/sysfs.o: ${AuConfName}
4755 +
4756 +-include ${srctree}/${src}/conf_priv.mk
4757 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4758 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4759 +++ linux/fs/aufs/cpup.c        2024-05-13 17:13:00.618070924 +0200
4760 @@ -0,0 +1,1459 @@
4761 +// SPDX-License-Identifier: GPL-2.0
4762 +/*
4763 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4764 + *
4765 + * This program is free software; you can redistribute it and/or modify
4766 + * it under the terms of the GNU General Public License as published by
4767 + * the Free Software Foundation; either version 2 of the License, or
4768 + * (at your option) any later version.
4769 + *
4770 + * This program is distributed in the hope that it will be useful,
4771 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4772 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4773 + * GNU General Public License for more details.
4774 + *
4775 + * You should have received a copy of the GNU General Public License
4776 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4777 + */
4778 +
4779 +/*
4780 + * copy-up functions, see wbr_policy.c for copy-down
4781 + */
4782 +
4783 +#include <linux/fs_stack.h>
4784 +#include <linux/mm.h>
4785 +#include <linux/task_work.h>
4786 +#include "aufs.h"
4787 +
4788 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4789 +{
4790 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4791 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4792 +
4793 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4794 +
4795 +       dst->i_flags |= iflags & ~mask;
4796 +       if (au_test_fs_notime(dst->i_sb))
4797 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4798 +}
4799 +
4800 +void au_cpup_attr_timesizes(struct inode *inode)
4801 +{
4802 +       struct inode *h_inode;
4803 +
4804 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4805 +       fsstack_copy_attr_times(inode, h_inode);
4806 +       fsstack_copy_inode_size(inode, h_inode);
4807 +}
4808 +
4809 +void au_cpup_attr_nlink(struct inode *inode, int force)
4810 +{
4811 +       struct inode *h_inode;
4812 +       struct super_block *sb;
4813 +       aufs_bindex_t bindex, bbot;
4814 +
4815 +       sb = inode->i_sb;
4816 +       bindex = au_ibtop(inode);
4817 +       h_inode = au_h_iptr(inode, bindex);
4818 +       if (!force
4819 +           && !S_ISDIR(h_inode->i_mode)
4820 +           && au_opt_test(au_mntflags(sb), PLINK)
4821 +           && au_plink_test(inode))
4822 +               return;
4823 +
4824 +       /*
4825 +        * 0 can happen in revalidating.
4826 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4827 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4828 +        * case.
4829 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4830 +        *       the incorrect link count.
4831 +        */
4832 +       set_nlink(inode, h_inode->i_nlink);
4833 +
4834 +       /*
4835 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4836 +        * it may includes whplink directory.
4837 +        */
4838 +       if (S_ISDIR(h_inode->i_mode)) {
4839 +               bbot = au_ibbot(inode);
4840 +               for (bindex++; bindex <= bbot; bindex++) {
4841 +                       h_inode = au_h_iptr(inode, bindex);
4842 +                       if (h_inode)
4843 +                               au_add_nlink(inode, h_inode);
4844 +               }
4845 +       }
4846 +}
4847 +
4848 +void au_cpup_attr_changeable(struct inode *inode)
4849 +{
4850 +       struct inode *h_inode;
4851 +
4852 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4853 +       inode->i_mode = h_inode->i_mode;
4854 +       inode->i_uid = h_inode->i_uid;
4855 +       inode->i_gid = h_inode->i_gid;
4856 +       au_cpup_attr_timesizes(inode);
4857 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4858 +}
4859 +
4860 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4861 +{
4862 +       struct au_iinfo *iinfo = au_ii(inode);
4863 +
4864 +       IiMustWriteLock(inode);
4865 +
4866 +       iinfo->ii_higen = h_inode->i_generation;
4867 +       iinfo->ii_hsb1 = h_inode->i_sb;
4868 +}
4869 +
4870 +void au_cpup_attr_all(struct inode *inode, int force)
4871 +{
4872 +       struct inode *h_inode;
4873 +
4874 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4875 +       au_cpup_attr_changeable(inode);
4876 +       if (inode->i_nlink > 0)
4877 +               au_cpup_attr_nlink(inode, force);
4878 +       inode->i_rdev = h_inode->i_rdev;
4879 +       inode->i_blkbits = h_inode->i_blkbits;
4880 +       au_cpup_igen(inode, h_inode);
4881 +}
4882 +
4883 +/* ---------------------------------------------------------------------- */
4884 +
4885 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4886 +
4887 +/* keep the timestamps of the parent dir when cpup */
4888 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4889 +                   struct path *h_path)
4890 +{
4891 +       struct inode *h_inode;
4892 +
4893 +       dt->dt_dentry = dentry;
4894 +       dt->dt_h_path = *h_path;
4895 +       h_inode = d_inode(h_path->dentry);
4896 +       dt->dt_atime = inode_get_atime(h_inode);
4897 +       dt->dt_mtime = inode_get_mtime(h_inode);
4898 +       /* smp_mb(); */
4899 +}
4900 +
4901 +void au_dtime_revert(struct au_dtime *dt)
4902 +{
4903 +       struct iattr attr;
4904 +       int err;
4905 +
4906 +       attr.ia_atime = dt->dt_atime;
4907 +       attr.ia_mtime = dt->dt_mtime;
4908 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4909 +               | ATTR_ATIME | ATTR_ATIME_SET;
4910 +
4911 +       /* no delegation since this is a directory */
4912 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4913 +       if (unlikely(err))
4914 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4915 +}
4916 +
4917 +/* ---------------------------------------------------------------------- */
4918 +
4919 +/* internal use only */
4920 +struct au_cpup_reg_attr {
4921 +       int             valid;
4922 +       struct kstat    st;
4923 +       unsigned int    iflags; /* inode->i_flags */
4924 +};
4925 +
4926 +static noinline_for_stack
4927 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4928 +              struct au_cpup_reg_attr *h_src_attr)
4929 +{
4930 +       int err, sbits, icex;
4931 +       unsigned int mnt_flags;
4932 +       unsigned char verbose;
4933 +       struct iattr ia;
4934 +       struct path h_path;
4935 +       struct inode *h_isrc, *h_idst;
4936 +       struct kstat *h_st;
4937 +       struct au_branch *br;
4938 +
4939 +       br = au_sbr(dst->d_sb, bindex);
4940 +       h_path.mnt = au_br_mnt(br);
4941 +       h_path.dentry = au_h_dptr(dst, bindex);
4942 +       h_idst = d_inode(h_path.dentry);
4943 +       h_isrc = d_inode(h_src->dentry);
4944 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4945 +               | ATTR_ATIME | ATTR_MTIME
4946 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4947 +       if (h_src_attr && h_src_attr->valid) {
4948 +               h_st = &h_src_attr->st;
4949 +               ia.ia_uid = h_st->uid;
4950 +               ia.ia_gid = h_st->gid;
4951 +               ia.ia_atime = h_st->atime;
4952 +               ia.ia_mtime = h_st->mtime;
4953 +               if (h_idst->i_mode != h_st->mode
4954 +                   && !S_ISLNK(h_idst->i_mode)) {
4955 +                       ia.ia_valid |= ATTR_MODE;
4956 +                       ia.ia_mode = h_st->mode;
4957 +               }
4958 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4959 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4960 +       } else {
4961 +               ia.ia_uid = h_isrc->i_uid;
4962 +               ia.ia_gid = h_isrc->i_gid;
4963 +               ia.ia_atime = inode_get_atime(h_isrc);
4964 +               ia.ia_mtime = inode_get_mtime(h_isrc);
4965 +               if (h_idst->i_mode != h_isrc->i_mode
4966 +                   && !S_ISLNK(h_idst->i_mode)) {
4967 +                       ia.ia_valid |= ATTR_MODE;
4968 +                       ia.ia_mode = h_isrc->i_mode;
4969 +               }
4970 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4971 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4972 +       }
4973 +       /* no delegation since it is just created */
4974 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4975 +
4976 +       /* is this nfs only? */
4977 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4978 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4979 +               ia.ia_mode = h_isrc->i_mode;
4980 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4981 +       }
4982 +
4983 +       icex = br->br_perm & AuBrAttr_ICEX;
4984 +       if (!err) {
4985 +               mnt_flags = au_mntflags(dst->d_sb);
4986 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4987 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4988 +       }
4989 +
4990 +       return err;
4991 +}
4992 +
4993 +/* ---------------------------------------------------------------------- */
4994 +
4995 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4996 +                          char *buf, unsigned long blksize)
4997 +{
4998 +       int err;
4999 +       size_t sz, rbytes, wbytes;
5000 +       unsigned char all_zero;
5001 +       char *p, *zp;
5002 +       struct inode *h_inode;
5003 +       /* reduce stack usage */
5004 +       struct iattr *ia;
5005 +
5006 +       zp = page_address(ZERO_PAGE(0));
5007 +       if (unlikely(!zp))
5008 +               return -ENOMEM; /* possible? */
5009 +
5010 +       err = 0;
5011 +       all_zero = 0;
5012 +       while (len) {
5013 +               AuDbg("len %lld\n", len);
5014 +               sz = blksize;
5015 +               if (len < blksize)
5016 +                       sz = len;
5017 +
5018 +               rbytes = 0;
5019 +               /* todo: signal_pending? */
5020 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5021 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5022 +                       err = rbytes;
5023 +               }
5024 +               if (unlikely(err < 0))
5025 +                       break;
5026 +
5027 +               all_zero = 0;
5028 +               if (len >= rbytes && rbytes == blksize)
5029 +                       all_zero = !memcmp(buf, zp, rbytes);
5030 +               if (!all_zero) {
5031 +                       wbytes = rbytes;
5032 +                       p = buf;
5033 +                       while (wbytes) {
5034 +                               size_t b;
5035 +
5036 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5037 +                               err = b;
5038 +                               /* todo: signal_pending? */
5039 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5040 +                                       continue;
5041 +                               if (unlikely(err < 0))
5042 +                                       break;
5043 +                               wbytes -= b;
5044 +                               p += b;
5045 +                       }
5046 +                       if (unlikely(err < 0))
5047 +                               break;
5048 +               } else {
5049 +                       loff_t res;
5050 +
5051 +                       AuLabel(hole);
5052 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5053 +                       err = res;
5054 +                       if (unlikely(res < 0))
5055 +                               break;
5056 +               }
5057 +               len -= rbytes;
5058 +               err = 0;
5059 +       }
5060 +
5061 +       /* the last block may be a hole */
5062 +       if (!err && all_zero) {
5063 +               AuLabel(last hole);
5064 +
5065 +               err = 1;
5066 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5067 +                       /* nfs requires this step to make last hole */
5068 +                       /* is this only nfs? */
5069 +                       do {
5070 +                               /* todo: signal_pending? */
5071 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5072 +                       } while (err == -EAGAIN || err == -EINTR);
5073 +                       if (err == 1)
5074 +                               dst->f_pos--;
5075 +               }
5076 +
5077 +               if (err == 1) {
5078 +                       ia = (void *)buf;
5079 +                       ia->ia_size = dst->f_pos;
5080 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5081 +                       ia->ia_file = dst;
5082 +                       h_inode = file_inode(dst);
5083 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5084 +                       /* no delegation since it is just created */
5085 +                       err = vfsub_notify_change(&dst->f_path, ia,
5086 +                                                 /*delegated*/NULL);
5087 +                       inode_unlock(h_inode);
5088 +               }
5089 +       }
5090 +
5091 +       return err;
5092 +}
5093 +
5094 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5095 +{
5096 +       int err;
5097 +       unsigned long blksize;
5098 +       unsigned char do_kfree;
5099 +       char *buf;
5100 +       struct super_block *h_sb;
5101 +
5102 +       err = -ENOMEM;
5103 +       h_sb = file_inode(dst)->i_sb;
5104 +       blksize = h_sb->s_blocksize;
5105 +       if (!blksize || PAGE_SIZE < blksize)
5106 +               blksize = PAGE_SIZE;
5107 +       AuDbg("blksize %lu\n", blksize);
5108 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5109 +       if (do_kfree)
5110 +               buf = kmalloc(blksize, GFP_NOFS);
5111 +       else
5112 +               buf = (void *)__get_free_page(GFP_NOFS);
5113 +       if (unlikely(!buf))
5114 +               goto out;
5115 +
5116 +       if (len > (1 << 22))
5117 +               AuDbg("copying a large file %lld\n", (long long)len);
5118 +
5119 +       src->f_pos = 0;
5120 +       dst->f_pos = 0;
5121 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5122 +       if (do_kfree) {
5123 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5124 +               au_kfree_do_rcu(buf);
5125 +       } else
5126 +               free_page((unsigned long)buf);
5127 +
5128 +out:
5129 +       return err;
5130 +}
5131 +
5132 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5133 +{
5134 +       int err;
5135 +       struct super_block *h_src_sb;
5136 +       struct inode *h_src_inode;
5137 +
5138 +       h_src_inode = file_inode(src);
5139 +       h_src_sb = h_src_inode->i_sb;
5140 +
5141 +       /* XFS acquires inode_lock */
5142 +       if (!au_test_xfs(h_src_sb))
5143 +               err = au_copy_file(dst, src, len);
5144 +       else {
5145 +               inode_unlock_shared(h_src_inode);
5146 +               err = au_copy_file(dst, src, len);
5147 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5148 +       }
5149 +
5150 +       return err;
5151 +}
5152 +
5153 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5154 +{
5155 +       int err;
5156 +       loff_t lo;
5157 +       struct super_block *h_src_sb;
5158 +       struct inode *h_src_inode;
5159 +
5160 +       h_src_inode = file_inode(src);
5161 +       h_src_sb = h_src_inode->i_sb;
5162 +       if (h_src_sb != file_inode(dst)->i_sb
5163 +           || !dst->f_op->remap_file_range) {
5164 +               err = au_do_copy(dst, src, len);
5165 +               goto out;
5166 +       }
5167 +
5168 +       if (!au_test_nfs(h_src_sb)) {
5169 +               inode_unlock_shared(h_src_inode);
5170 +               lo = vfsub_clone_file_range(src, dst, len);
5171 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5172 +       } else
5173 +               lo = vfsub_clone_file_range(src, dst, len);
5174 +       if (lo == len) {
5175 +               err = 0;
5176 +               goto out; /* success */
5177 +       } else if (lo >= 0)
5178 +               /* todo: possible? */
5179 +               /* paritially succeeded */
5180 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5181 +       else if (lo != -EOPNOTSUPP) {
5182 +               /* older XFS has a condition in cloning */
5183 +               err = lo;
5184 +               goto out;
5185 +       }
5186 +
5187 +       /* the backend fs on NFS may not support cloning */
5188 +       err = au_do_copy(dst, src, len);
5189 +
5190 +out:
5191 +       AuTraceErr(err);
5192 +       return err;
5193 +}
5194 +
5195 +/*
5196 + * to support a sparse file which is opened with O_APPEND,
5197 + * we need to close the file.
5198 + */
5199 +static int au_cp_regular(struct au_cp_generic *cpg)
5200 +{
5201 +       int err, i;
5202 +       enum { SRC, DST };
5203 +       struct {
5204 +               aufs_bindex_t bindex;
5205 +               unsigned int flags;
5206 +               struct dentry *dentry;
5207 +               int force_wr;
5208 +               struct file *file;
5209 +       } *f, file[] = {
5210 +               {
5211 +                       .bindex = cpg->bsrc,
5212 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5213 +               },
5214 +               {
5215 +                       .bindex = cpg->bdst,
5216 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5217 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5218 +               }
5219 +       };
5220 +       struct au_branch *br;
5221 +       struct super_block *sb, *h_src_sb;
5222 +       struct inode *h_src_inode;
5223 +       struct task_struct *tsk = current;
5224 +
5225 +       /* bsrc branch can be ro/rw. */
5226 +       sb = cpg->dentry->d_sb;
5227 +       f = file;
5228 +       for (i = 0; i < 2; i++, f++) {
5229 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5230 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5231 +                                   /*file*/NULL, f->force_wr);
5232 +               if (IS_ERR(f->file)) {
5233 +                       err = PTR_ERR(f->file);
5234 +                       if (i == SRC)
5235 +                               goto out;
5236 +                       else
5237 +                               goto out_src;
5238 +               }
5239 +       }
5240 +
5241 +       /* try stopping to update while we copyup */
5242 +       h_src_inode = d_inode(file[SRC].dentry);
5243 +       h_src_sb = h_src_inode->i_sb;
5244 +       if (!au_test_nfs(h_src_sb))
5245 +               IMustLock(h_src_inode);
5246 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5247 +
5248 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5249 +       if (tsk->flags & PF_KTHREAD)
5250 +               __fput_sync(file[DST].file);
5251 +       else {
5252 +               /* it happened actually */
5253 +               fput(file[DST].file);
5254 +               /*
5255 +                * too bad.
5256 +                * we have to call both since we don't know which place the file
5257 +                * was added to.
5258 +                */
5259 +               task_work_run();
5260 +               flush_delayed_fput();
5261 +       }
5262 +       br = au_sbr(sb, file[DST].bindex);
5263 +       au_lcnt_dec(&br->br_nfiles);
5264 +
5265 +out_src:
5266 +       fput(file[SRC].file);
5267 +       br = au_sbr(sb, file[SRC].bindex);
5268 +       au_lcnt_dec(&br->br_nfiles);
5269 +out:
5270 +       return err;
5271 +}
5272 +
5273 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5274 +                             struct au_cpup_reg_attr *h_src_attr)
5275 +{
5276 +       int err, rerr;
5277 +       loff_t l;
5278 +       struct path h_path;
5279 +       struct inode *h_src_inode, *h_dst_inode;
5280 +
5281 +       err = 0;
5282 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5283 +       l = i_size_read(h_src_inode);
5284 +       if (cpg->len == -1 || l < cpg->len)
5285 +               cpg->len = l;
5286 +       if (cpg->len) {
5287 +               /* try stopping to update while we are referencing */
5288 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5289 +               au_pin_hdir_unlock(cpg->pin);
5290 +
5291 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5292 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5293 +               h_src_attr->iflags = h_src_inode->i_flags;
5294 +               if (!au_test_nfs(h_src_inode->i_sb))
5295 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5296 +               else {
5297 +                       inode_unlock_shared(h_src_inode);
5298 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5299 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5300 +               }
5301 +               if (unlikely(err)) {
5302 +                       inode_unlock_shared(h_src_inode);
5303 +                       goto out;
5304 +               }
5305 +               h_src_attr->valid = 1;
5306 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5307 +                       err = au_cp_regular(cpg);
5308 +                       inode_unlock_shared(h_src_inode);
5309 +               } else {
5310 +                       inode_unlock_shared(h_src_inode);
5311 +                       err = au_cp_regular(cpg);
5312 +               }
5313 +               rerr = au_pin_hdir_relock(cpg->pin);
5314 +               if (!err && rerr)
5315 +                       err = rerr;
5316 +       }
5317 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5318 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5319 +               h_dst_inode = d_inode(h_path.dentry);
5320 +               spin_lock(&h_dst_inode->i_lock);
5321 +               h_dst_inode->i_state |= I_LINKABLE;
5322 +               spin_unlock(&h_dst_inode->i_lock);
5323 +       }
5324 +
5325 +out:
5326 +       return err;
5327 +}
5328 +
5329 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5330 +                             struct inode *h_dir)
5331 +{
5332 +       int err;
5333 +       DEFINE_DELAYED_CALL(done);
5334 +       const char *sym;
5335 +
5336 +       sym = vfs_get_link(h_src, &done);
5337 +       err = PTR_ERR(sym);
5338 +       if (IS_ERR(sym))
5339 +               goto out;
5340 +
5341 +       err = vfsub_symlink(h_dir, h_path, sym);
5342 +
5343 +out:
5344 +       do_delayed_call(&done);
5345 +       return err;
5346 +}
5347 +
5348 +/*
5349 + * regardless 'acl' option, reset all ACL.
5350 + * All ACL will be copied up later from the original entry on the lower branch.
5351 + */
5352 +static int au_reset_acl(struct path *h_path, umode_t mode)
5353 +{
5354 +       int err;
5355 +       struct dentry *h_dentry;
5356 +       /* struct inode *h_inode; */
5357 +       struct mnt_idmap *h_idmap;
5358 +
5359 +       h_idmap = mnt_idmap(h_path->mnt);
5360 +       h_dentry = h_path->dentry;
5361 +       /* h_inode = d_inode(h_dentry); */
5362 +       /* forget_all_cached_acls(h_inode)); */
5363 +       err = vfsub_remove_acl(h_idmap, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5364 +       AuTraceErr(err);
5365 +       if (err == -EOPNOTSUPP)
5366 +               err = 0;
5367 +       if (!err)
5368 +               err = vfsub_acl_chmod(h_idmap, h_dentry, mode);
5369 +
5370 +       AuTraceErr(err);
5371 +       return err;
5372 +}
5373 +
5374 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5375 +                         struct inode *h_dir, struct path *h_path)
5376 +{
5377 +       int err;
5378 +       struct inode *dir, *inode;
5379 +       struct mnt_idmap *h_idmap;
5380 +
5381 +       h_idmap = mnt_idmap(h_path->mnt);
5382 +       err = vfsub_remove_acl(h_idmap, h_path->dentry,
5383 +                              XATTR_NAME_POSIX_ACL_DEFAULT);
5384 +       AuTraceErr(err);
5385 +       if (err == -EOPNOTSUPP)
5386 +               err = 0;
5387 +       if (unlikely(err))
5388 +               goto out;
5389 +
5390 +       /*
5391 +        * strange behaviour from the users view,
5392 +        * particularly setattr case
5393 +        */
5394 +       dir = d_inode(dst_parent);
5395 +       if (au_ibtop(dir) == cpg->bdst)
5396 +               au_cpup_attr_nlink(dir, /*force*/1);
5397 +       inode = d_inode(cpg->dentry);
5398 +       au_cpup_attr_nlink(inode, /*force*/1);
5399 +
5400 +out:
5401 +       return err;
5402 +}
5403 +
5404 +static noinline_for_stack
5405 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5406 +              struct au_cpup_reg_attr *h_src_attr)
5407 +{
5408 +       int err;
5409 +       umode_t mode;
5410 +       unsigned int mnt_flags;
5411 +       unsigned char isdir, isreg, force;
5412 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5413 +       struct au_dtime dt;
5414 +       struct path h_path;
5415 +       struct dentry *h_src, *h_dst, *h_parent;
5416 +       struct inode *h_inode, *h_dir;
5417 +       struct super_block *sb;
5418 +
5419 +       /* bsrc branch can be ro/rw. */
5420 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5421 +       h_inode = d_inode(h_src);
5422 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5423 +
5424 +       /* try stopping to be referenced while we are creating */
5425 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5426 +       if (au_ftest_cpup(cpg->flags, RENAME))
5427 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5428 +                                 AUFS_WH_PFX_LEN));
5429 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5430 +       h_dir = d_inode(h_parent);
5431 +       IMustLock(h_dir);
5432 +       AuDebugOn(h_parent != h_dst->d_parent);
5433 +
5434 +       sb = cpg->dentry->d_sb;
5435 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5436 +       if (do_dt) {
5437 +               h_path.dentry = h_parent;
5438 +               au_dtime_store(&dt, dst_parent, &h_path);
5439 +       }
5440 +       h_path.dentry = h_dst;
5441 +
5442 +       isreg = 0;
5443 +       isdir = 0;
5444 +       mode = h_inode->i_mode;
5445 +       switch (mode & S_IFMT) {
5446 +       case S_IFREG:
5447 +               isreg = 1;
5448 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5449 +               if (!err)
5450 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5451 +               break;
5452 +       case S_IFDIR:
5453 +               isdir = 1;
5454 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5455 +               if (!err)
5456 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5457 +               break;
5458 +       case S_IFLNK:
5459 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5460 +               break;
5461 +       case S_IFCHR:
5462 +       case S_IFBLK:
5463 +               AuDebugOn(!capable(CAP_MKNOD));
5464 +               fallthrough;
5465 +       case S_IFIFO:
5466 +       case S_IFSOCK:
5467 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5468 +               break;
5469 +       default:
5470 +               AuIOErr("Unknown inode type 0%o\n", mode);
5471 +               err = -EIO;
5472 +       }
5473 +       if (!err)
5474 +               err = au_reset_acl(&h_path, mode);
5475 +
5476 +       mnt_flags = au_mntflags(sb);
5477 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5478 +           && !isdir
5479 +           && au_opt_test(mnt_flags, XINO)
5480 +           && (h_inode->i_nlink == 1
5481 +               || (h_inode->i_state & I_LINKABLE))
5482 +           /* todo: unnecessary? */
5483 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5484 +           && cpg->bdst < cpg->bsrc
5485 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5486 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5487 +               /* ignore this error */
5488 +
5489 +       if (!err) {
5490 +               force = 0;
5491 +               if (isreg) {
5492 +                       force = !!cpg->len;
5493 +                       if (cpg->len == -1)
5494 +                               force = !!i_size_read(h_inode);
5495 +               }
5496 +               au_fhsm_wrote(sb, cpg->bdst, force);
5497 +       }
5498 +
5499 +       if (do_dt)
5500 +               au_dtime_revert(&dt);
5501 +       return err;
5502 +}
5503 +
5504 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5505 +{
5506 +       int err;
5507 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5508 +       struct path h_ppath;
5509 +       struct inode *h_dir;
5510 +       aufs_bindex_t bdst;
5511 +
5512 +       dentry = cpg->dentry;
5513 +       bdst = cpg->bdst;
5514 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5515 +       h_dentry = au_h_dptr(dentry, bdst);
5516 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5517 +               dget(h_dentry);
5518 +               au_set_h_dptr(dentry, bdst, NULL);
5519 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5520 +               if (!err)
5521 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5522 +               au_set_h_dptr(dentry, bdst, h_dentry);
5523 +       } else {
5524 +               err = 0;
5525 +               parent = dget_parent(dentry);
5526 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5527 +               dput(parent);
5528 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5529 +               if (IS_ERR(h_path->dentry))
5530 +                       err = PTR_ERR(h_path->dentry);
5531 +       }
5532 +       if (unlikely(err))
5533 +               goto out;
5534 +
5535 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5536 +       h_dir = d_inode(h_parent);
5537 +       IMustLock(h_dir);
5538 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5539 +       /* no delegation since it is just created */
5540 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5541 +                          /*flags*/0);
5542 +       dput(h_path->dentry);
5543 +
5544 +out:
5545 +       return err;
5546 +}
5547 +
5548 +/*
5549 + * copyup the @dentry from @bsrc to @bdst.
5550 + * the caller must set the both of lower dentries.
5551 + * @len is for truncating when it is -1 copyup the entire file.
5552 + * in link/rename cases, @dst_parent may be different from the real one.
5553 + * basic->bsrc can be larger than basic->bdst.
5554 + * aufs doesn't touch the credential so
5555 + * security_inode_copy_up{,_xattr}() are unnecessary.
5556 + */
5557 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5558 +{
5559 +       int err, rerr;
5560 +       aufs_bindex_t old_ibtop;
5561 +       unsigned char isdir, plink;
5562 +       struct dentry *h_src, *h_dst, *h_parent;
5563 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5564 +       struct super_block *sb;
5565 +       struct au_branch *br;
5566 +       struct path h_src_path;
5567 +       /* to reduce stack size */
5568 +       struct {
5569 +               struct au_dtime dt;
5570 +               struct path h_path;
5571 +               struct au_cpup_reg_attr h_src_attr;
5572 +       } *a;
5573 +
5574 +       err = -ENOMEM;
5575 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5576 +       if (unlikely(!a))
5577 +               goto out;
5578 +       a->h_src_attr.valid = 0;
5579 +
5580 +       sb = cpg->dentry->d_sb;
5581 +       br = au_sbr(sb, cpg->bdst);
5582 +       a->h_path.mnt = au_br_mnt(br);
5583 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5584 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5585 +       h_dir = d_inode(h_parent);
5586 +       IMustLock(h_dir);
5587 +
5588 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5589 +       inode = d_inode(cpg->dentry);
5590 +
5591 +       if (!dst_parent)
5592 +               dst_parent = dget_parent(cpg->dentry);
5593 +       else
5594 +               dget(dst_parent);
5595 +
5596 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5597 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5598 +       if (dst_inode) {
5599 +               if (unlikely(!plink)) {
5600 +                       err = -EIO;
5601 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5602 +                               "but plink is disabled\n",
5603 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5604 +                       goto out_parent;
5605 +               }
5606 +
5607 +               if (dst_inode->i_nlink) {
5608 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5609 +
5610 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5611 +                       err = PTR_ERR(h_src);
5612 +                       if (IS_ERR(h_src))
5613 +                               goto out_parent;
5614 +                       if (unlikely(d_is_negative(h_src))) {
5615 +                               err = -EIO;
5616 +                               AuIOErr("i%lu exists on b%d "
5617 +                                       "but not pseudo-linked\n",
5618 +                                       inode->i_ino, cpg->bdst);
5619 +                               dput(h_src);
5620 +                               goto out_parent;
5621 +                       }
5622 +
5623 +                       if (do_dt) {
5624 +                               a->h_path.dentry = h_parent;
5625 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5626 +                       }
5627 +
5628 +                       a->h_path.dentry = h_dst;
5629 +                       delegated = NULL;
5630 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5631 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5632 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5633 +                       if (do_dt)
5634 +                               au_dtime_revert(&a->dt);
5635 +                       if (unlikely(err == -EWOULDBLOCK)) {
5636 +                               pr_warn("cannot retry for NFSv4 delegation"
5637 +                                       " for an internal link\n");
5638 +                               iput(delegated);
5639 +                       }
5640 +                       dput(h_src);
5641 +                       goto out_parent;
5642 +               } else
5643 +                       /* todo: cpup_wh_file? */
5644 +                       /* udba work */
5645 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5646 +       }
5647 +
5648 +       isdir = S_ISDIR(inode->i_mode);
5649 +       old_ibtop = au_ibtop(inode);
5650 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5651 +       if (unlikely(err))
5652 +               goto out_rev;
5653 +       dst_inode = d_inode(h_dst);
5654 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5655 +       /* todo: necessary? */
5656 +       /* au_pin_hdir_unlock(cpg->pin); */
5657 +
5658 +       h_src_path.dentry = h_src;
5659 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5660 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5661 +       if (unlikely(err)) {
5662 +               /* todo: necessary? */
5663 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5664 +               inode_unlock(dst_inode);
5665 +               goto out_rev;
5666 +       }
5667 +
5668 +       if (cpg->bdst < old_ibtop) {
5669 +               if (S_ISREG(inode->i_mode)) {
5670 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5671 +                       if (unlikely(err)) {
5672 +                               /* ignore an error */
5673 +                               /* au_pin_hdir_relock(cpg->pin); */
5674 +                               inode_unlock(dst_inode);
5675 +                               goto out_rev;
5676 +                       }
5677 +               }
5678 +               au_set_ibtop(inode, cpg->bdst);
5679 +       } else
5680 +               au_set_ibbot(inode, cpg->bdst);
5681 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5682 +                     au_hi_flags(inode, isdir));
5683 +
5684 +       /* todo: necessary? */
5685 +       /* err = au_pin_hdir_relock(cpg->pin); */
5686 +       inode_unlock(dst_inode);
5687 +       if (unlikely(err))
5688 +               goto out_rev;
5689 +
5690 +       src_inode = d_inode(h_src);
5691 +       if (!isdir
5692 +           && (src_inode->i_nlink > 1
5693 +               || src_inode->i_state & I_LINKABLE)
5694 +           && plink)
5695 +               au_plink_append(inode, cpg->bdst, h_dst);
5696 +
5697 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5698 +               a->h_path.dentry = h_dst;
5699 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5700 +       }
5701 +       if (!err)
5702 +               goto out_parent; /* success */
5703 +
5704 +       /* revert */
5705 +out_rev:
5706 +       a->h_path.dentry = h_parent;
5707 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5708 +       a->h_path.dentry = h_dst;
5709 +       rerr = 0;
5710 +       if (d_is_positive(h_dst)) {
5711 +               if (!isdir) {
5712 +                       /* no delegation since it is just created */
5713 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5714 +                                           /*delegated*/NULL, /*force*/0);
5715 +               } else
5716 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5717 +       }
5718 +       au_dtime_revert(&a->dt);
5719 +       if (rerr) {
5720 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5721 +               err = -EIO;
5722 +       }
5723 +out_parent:
5724 +       dput(dst_parent);
5725 +       au_kfree_rcu(a);
5726 +out:
5727 +       return err;
5728 +}
5729 +
5730 +#if 0 /* reserved */
5731 +struct au_cpup_single_args {
5732 +       int *errp;
5733 +       struct au_cp_generic *cpg;
5734 +       struct dentry *dst_parent;
5735 +};
5736 +
5737 +static void au_call_cpup_single(void *args)
5738 +{
5739 +       struct au_cpup_single_args *a = args;
5740 +
5741 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5742 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5743 +       au_pin_hdir_release(a->cpg->pin);
5744 +}
5745 +#endif
5746 +
5747 +/*
5748 + * prevent SIGXFSZ in copy-up.
5749 + * testing CAP_MKNOD is for generic fs,
5750 + * but CAP_FSETID is for xfs only, currently.
5751 + */
5752 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5753 +{
5754 +       int do_sio;
5755 +       struct super_block *sb;
5756 +       struct inode *h_dir;
5757 +
5758 +       do_sio = 0;
5759 +       sb = au_pinned_parent(pin)->d_sb;
5760 +       if (!au_wkq_test()
5761 +           && (!au_sbi(sb)->si_plink_maint_pid
5762 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5763 +               switch (mode & S_IFMT) {
5764 +               case S_IFREG:
5765 +                       /* no condition about RLIMIT_FSIZE and the file size */
5766 +                       do_sio = 1;
5767 +                       break;
5768 +               case S_IFCHR:
5769 +               case S_IFBLK:
5770 +                       do_sio = !capable(CAP_MKNOD);
5771 +                       break;
5772 +               }
5773 +               if (!do_sio)
5774 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5775 +                                 && !capable(CAP_FSETID));
5776 +               /* this workaround may be removed in the future */
5777 +               if (!do_sio) {
5778 +                       h_dir = au_pinned_h_dir(pin);
5779 +                       do_sio = h_dir->i_mode & S_ISVTX;
5780 +               }
5781 +       }
5782 +
5783 +       return do_sio;
5784 +}
5785 +
5786 +#if 0 /* reserved */
5787 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5788 +{
5789 +       int err, wkq_err;
5790 +       struct dentry *h_dentry;
5791 +
5792 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5793 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5794 +               err = au_cpup_single(cpg, dst_parent);
5795 +       else {
5796 +               struct au_cpup_single_args args = {
5797 +                       .errp           = &err,
5798 +                       .cpg            = cpg,
5799 +                       .dst_parent     = dst_parent
5800 +               };
5801 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5802 +               if (unlikely(wkq_err))
5803 +                       err = wkq_err;
5804 +       }
5805 +
5806 +       return err;
5807 +}
5808 +#endif
5809 +
5810 +/*
5811 + * copyup the @dentry from the first active lower branch to @bdst,
5812 + * using au_cpup_single().
5813 + */
5814 +static int au_cpup_simple(struct au_cp_generic *cpg)
5815 +{
5816 +       int err;
5817 +       unsigned int flags_orig;
5818 +       struct dentry *dentry;
5819 +
5820 +       AuDebugOn(cpg->bsrc < 0);
5821 +
5822 +       dentry = cpg->dentry;
5823 +       DiMustWriteLock(dentry);
5824 +
5825 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5826 +       if (!err) {
5827 +               flags_orig = cpg->flags;
5828 +               au_fset_cpup(cpg->flags, RENAME);
5829 +               err = au_cpup_single(cpg, NULL);
5830 +               cpg->flags = flags_orig;
5831 +               if (!err)
5832 +                       return 0; /* success */
5833 +
5834 +               /* revert */
5835 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5836 +               au_set_dbtop(dentry, cpg->bsrc);
5837 +       }
5838 +
5839 +       return err;
5840 +}
5841 +
5842 +struct au_cpup_simple_args {
5843 +       int *errp;
5844 +       struct au_cp_generic *cpg;
5845 +};
5846 +
5847 +static void au_call_cpup_simple(void *args)
5848 +{
5849 +       struct au_cpup_simple_args *a = args;
5850 +
5851 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5852 +       *a->errp = au_cpup_simple(a->cpg);
5853 +       au_pin_hdir_release(a->cpg->pin);
5854 +}
5855 +
5856 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5857 +{
5858 +       int err, wkq_err;
5859 +       struct dentry *dentry, *parent;
5860 +       struct file *h_file;
5861 +       struct inode *h_dir;
5862 +       struct mnt_idmap *h_idmap;
5863 +
5864 +       dentry = cpg->dentry;
5865 +       h_file = NULL;
5866 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5867 +               AuDebugOn(cpg->bsrc < 0);
5868 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5869 +               err = PTR_ERR(h_file);
5870 +               if (IS_ERR(h_file))
5871 +                       goto out;
5872 +       }
5873 +
5874 +       parent = dget_parent(dentry);
5875 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5876 +       h_idmap = au_sbr_idmap(dentry->d_sb, cpg->bdst);
5877 +       if (!au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC | MAY_WRITE)
5878 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5879 +               err = au_cpup_simple(cpg);
5880 +       else {
5881 +               struct au_cpup_simple_args args = {
5882 +                       .errp           = &err,
5883 +                       .cpg            = cpg
5884 +               };
5885 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5886 +               if (unlikely(wkq_err))
5887 +                       err = wkq_err;
5888 +       }
5889 +
5890 +       dput(parent);
5891 +       if (h_file)
5892 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5893 +
5894 +out:
5895 +       return err;
5896 +}
5897 +
5898 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5899 +{
5900 +       aufs_bindex_t bsrc, bbot;
5901 +       struct dentry *dentry, *h_dentry;
5902 +
5903 +       if (cpg->bsrc < 0) {
5904 +               dentry = cpg->dentry;
5905 +               bbot = au_dbbot(dentry);
5906 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5907 +                       h_dentry = au_h_dptr(dentry, bsrc);
5908 +                       if (h_dentry) {
5909 +                               AuDebugOn(d_is_negative(h_dentry));
5910 +                               break;
5911 +                       }
5912 +               }
5913 +               AuDebugOn(bsrc > bbot);
5914 +               cpg->bsrc = bsrc;
5915 +       }
5916 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5917 +       return au_do_sio_cpup_simple(cpg);
5918 +}
5919 +
5920 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5921 +{
5922 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5923 +       return au_do_sio_cpup_simple(cpg);
5924 +}
5925 +
5926 +/* ---------------------------------------------------------------------- */
5927 +
5928 +/*
5929 + * copyup the deleted file for writing.
5930 + */
5931 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5932 +                        struct file *file)
5933 +{
5934 +       int err;
5935 +       unsigned int flags_orig;
5936 +       aufs_bindex_t bsrc_orig;
5937 +       struct au_dinfo *dinfo;
5938 +       struct {
5939 +               struct au_hdentry *hd;
5940 +               struct dentry *h_dentry;
5941 +       } hdst, hsrc;
5942 +
5943 +       dinfo = au_di(cpg->dentry);
5944 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5945 +
5946 +       bsrc_orig = cpg->bsrc;
5947 +       cpg->bsrc = dinfo->di_btop;
5948 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5949 +       hdst.h_dentry = hdst.hd->hd_dentry;
5950 +       hdst.hd->hd_dentry = wh_dentry;
5951 +       dinfo->di_btop = cpg->bdst;
5952 +
5953 +       hsrc.h_dentry = NULL;
5954 +       if (file) {
5955 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5956 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5957 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5958 +       }
5959 +       flags_orig = cpg->flags;
5960 +       cpg->flags = !AuCpup_DTIME;
5961 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5962 +       cpg->flags = flags_orig;
5963 +       if (file) {
5964 +               if (!err)
5965 +                       err = au_reopen_nondir(file);
5966 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5967 +       }
5968 +       hdst.hd->hd_dentry = hdst.h_dentry;
5969 +       dinfo->di_btop = cpg->bsrc;
5970 +       cpg->bsrc = bsrc_orig;
5971 +
5972 +       return err;
5973 +}
5974 +
5975 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5976 +{
5977 +       int err;
5978 +       aufs_bindex_t bdst;
5979 +       struct au_dtime dt;
5980 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5981 +       struct au_branch *br;
5982 +       struct path h_path;
5983 +
5984 +       dentry = cpg->dentry;
5985 +       bdst = cpg->bdst;
5986 +       br = au_sbr(dentry->d_sb, bdst);
5987 +       parent = dget_parent(dentry);
5988 +       h_parent = au_h_dptr(parent, bdst);
5989 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5990 +       err = PTR_ERR(wh_dentry);
5991 +       if (IS_ERR(wh_dentry))
5992 +               goto out;
5993 +
5994 +       h_path.dentry = h_parent;
5995 +       h_path.mnt = au_br_mnt(br);
5996 +       au_dtime_store(&dt, parent, &h_path);
5997 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5998 +       if (unlikely(err))
5999 +               goto out_wh;
6000 +
6001 +       dget(wh_dentry);
6002 +       h_path.dentry = wh_dentry;
6003 +       if (!d_is_dir(wh_dentry)) {
6004 +               /* no delegation since it is just created */
6005 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6006 +                                  /*delegated*/NULL, /*force*/0);
6007 +       } else
6008 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6009 +       if (unlikely(err)) {
6010 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6011 +                       wh_dentry, err);
6012 +               err = -EIO;
6013 +       }
6014 +       au_dtime_revert(&dt);
6015 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6016 +
6017 +out_wh:
6018 +       dput(wh_dentry);
6019 +out:
6020 +       dput(parent);
6021 +       return err;
6022 +}
6023 +
6024 +struct au_cpup_wh_args {
6025 +       int *errp;
6026 +       struct au_cp_generic *cpg;
6027 +       struct file *file;
6028 +};
6029 +
6030 +static void au_call_cpup_wh(void *args)
6031 +{
6032 +       struct au_cpup_wh_args *a = args;
6033 +
6034 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6035 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6036 +       au_pin_hdir_release(a->cpg->pin);
6037 +}
6038 +
6039 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6040 +{
6041 +       int err, wkq_err;
6042 +       aufs_bindex_t bdst;
6043 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6044 +       struct inode *dir, *h_dir, *h_tmpdir;
6045 +       struct au_wbr *wbr;
6046 +       struct au_pin wh_pin, *pin_orig;
6047 +       struct mnt_idmap *h_idmap;
6048 +
6049 +       dentry = cpg->dentry;
6050 +       bdst = cpg->bdst;
6051 +       parent = dget_parent(dentry);
6052 +       dir = d_inode(parent);
6053 +       h_orph = NULL;
6054 +       h_parent = NULL;
6055 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6056 +       h_tmpdir = h_dir;
6057 +       pin_orig = NULL;
6058 +       if (!h_dir->i_nlink) {
6059 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6060 +               h_orph = wbr->wbr_orph;
6061 +
6062 +               h_parent = dget(au_h_dptr(parent, bdst));
6063 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6064 +               h_tmpdir = d_inode(h_orph);
6065 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6066 +
6067 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6068 +               /* todo: au_h_open_pre()? */
6069 +
6070 +               pin_orig = cpg->pin;
6071 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6072 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6073 +               cpg->pin = &wh_pin;
6074 +       }
6075 +
6076 +       h_idmap = au_sbr_idmap(dentry->d_sb, bdst);
6077 +       if (!au_test_h_perm_sio(h_idmap, h_tmpdir, MAY_EXEC | MAY_WRITE)
6078 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6079 +               err = au_cpup_wh(cpg, file);
6080 +       else {
6081 +               struct au_cpup_wh_args args = {
6082 +                       .errp   = &err,
6083 +                       .cpg    = cpg,
6084 +                       .file   = file
6085 +               };
6086 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6087 +               if (unlikely(wkq_err))
6088 +                       err = wkq_err;
6089 +       }
6090 +
6091 +       if (h_orph) {
6092 +               inode_unlock(h_tmpdir);
6093 +               /* todo: au_h_open_post()? */
6094 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6095 +               au_set_h_dptr(parent, bdst, h_parent);
6096 +               AuDebugOn(!pin_orig);
6097 +               cpg->pin = pin_orig;
6098 +       }
6099 +       iput(h_dir);
6100 +       dput(parent);
6101 +
6102 +       return err;
6103 +}
6104 +
6105 +/* ---------------------------------------------------------------------- */
6106 +
6107 +/*
6108 + * generic routine for both of copy-up and copy-down.
6109 + */
6110 +/* cf. revalidate function in file.c */
6111 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6112 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6113 +                        struct au_pin *pin,
6114 +                        struct dentry *h_parent, void *arg),
6115 +              void *arg)
6116 +{
6117 +       int err;
6118 +       struct au_pin pin;
6119 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6120 +
6121 +       err = 0;
6122 +       parent = dget_parent(dentry);
6123 +       if (IS_ROOT(parent))
6124 +               goto out;
6125 +
6126 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6127 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6128 +
6129 +       /* do not use au_dpage */
6130 +       real_parent = parent;
6131 +       while (1) {
6132 +               dput(parent);
6133 +               parent = dget_parent(dentry);
6134 +               h_parent = au_h_dptr(parent, bdst);
6135 +               if (h_parent)
6136 +                       goto out; /* success */
6137 +
6138 +               /* find top dir which is necessary to cpup */
6139 +               do {
6140 +                       d = parent;
6141 +                       dput(parent);
6142 +                       parent = dget_parent(d);
6143 +                       di_read_lock_parent3(parent, !AuLock_IR);
6144 +                       h_parent = au_h_dptr(parent, bdst);
6145 +                       di_read_unlock(parent, !AuLock_IR);
6146 +               } while (!h_parent);
6147 +
6148 +               if (d != real_parent)
6149 +                       di_write_lock_child3(d);
6150 +
6151 +               /* somebody else might create while we were sleeping */
6152 +               h_dentry = au_h_dptr(d, bdst);
6153 +               if (!h_dentry || d_is_negative(h_dentry)) {
6154 +                       if (h_dentry)
6155 +                               au_update_dbtop(d);
6156 +
6157 +                       au_pin_set_dentry(&pin, d);
6158 +                       err = au_do_pin(&pin);
6159 +                       if (!err) {
6160 +                               err = cp(d, bdst, &pin, h_parent, arg);
6161 +                               au_unpin(&pin);
6162 +                       }
6163 +               }
6164 +
6165 +               if (d != real_parent)
6166 +                       di_write_unlock(d);
6167 +               if (unlikely(err))
6168 +                       break;
6169 +       }
6170 +
6171 +out:
6172 +       dput(parent);
6173 +       return err;
6174 +}
6175 +
6176 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6177 +                      struct au_pin *pin,
6178 +                      struct dentry *h_parent __maybe_unused,
6179 +                      void *arg __maybe_unused)
6180 +{
6181 +       struct au_cp_generic cpg = {
6182 +               .dentry = dentry,
6183 +               .bdst   = bdst,
6184 +               .bsrc   = -1,
6185 +               .len    = 0,
6186 +               .pin    = pin,
6187 +               .flags  = AuCpup_DTIME
6188 +       };
6189 +       return au_sio_cpup_simple(&cpg);
6190 +}
6191 +
6192 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6193 +{
6194 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6195 +}
6196 +
6197 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6198 +{
6199 +       int err;
6200 +       struct dentry *parent;
6201 +       struct inode *dir;
6202 +
6203 +       parent = dget_parent(dentry);
6204 +       dir = d_inode(parent);
6205 +       err = 0;
6206 +       if (au_h_iptr(dir, bdst))
6207 +               goto out;
6208 +
6209 +       di_read_unlock(parent, AuLock_IR);
6210 +       di_write_lock_parent(parent);
6211 +       /* someone else might change our inode while we were sleeping */
6212 +       if (!au_h_iptr(dir, bdst))
6213 +               err = au_cpup_dirs(dentry, bdst);
6214 +       di_downgrade_lock(parent, AuLock_IR);
6215 +
6216 +out:
6217 +       dput(parent);
6218 +       return err;
6219 +}
6220 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6221 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6222 +++ linux/fs/aufs/cpup.h        2024-05-13 17:13:00.618070924 +0200
6223 @@ -0,0 +1,100 @@
6224 +/* SPDX-License-Identifier: GPL-2.0 */
6225 +/*
6226 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6227 + *
6228 + * This program is free software; you can redistribute it and/or modify
6229 + * it under the terms of the GNU General Public License as published by
6230 + * the Free Software Foundation; either version 2 of the License, or
6231 + * (at your option) any later version.
6232 + *
6233 + * This program is distributed in the hope that it will be useful,
6234 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6235 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6236 + * GNU General Public License for more details.
6237 + *
6238 + * You should have received a copy of the GNU General Public License
6239 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6240 + */
6241 +
6242 +/*
6243 + * copy-up/down functions
6244 + */
6245 +
6246 +#ifndef __AUFS_CPUP_H__
6247 +#define __AUFS_CPUP_H__
6248 +
6249 +#ifdef __KERNEL__
6250 +
6251 +#include <linux/path.h>
6252 +
6253 +struct inode;
6254 +struct file;
6255 +struct au_pin;
6256 +
6257 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6258 +void au_cpup_attr_timesizes(struct inode *inode);
6259 +void au_cpup_attr_nlink(struct inode *inode, int force);
6260 +void au_cpup_attr_changeable(struct inode *inode);
6261 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6262 +void au_cpup_attr_all(struct inode *inode, int force);
6263 +
6264 +/* ---------------------------------------------------------------------- */
6265 +
6266 +struct au_cp_generic {
6267 +       struct dentry   *dentry;
6268 +       aufs_bindex_t   bdst, bsrc;
6269 +       loff_t          len;
6270 +       struct au_pin   *pin;
6271 +       unsigned int    flags;
6272 +};
6273 +
6274 +/* cpup flags */
6275 +#define AuCpup_DTIME           BIT(0)          /* do dtime_store/revert */
6276 +#define AuCpup_KEEPLINO                BIT(1)          /* do not clear the lower xino,
6277 +                                                  for link(2) */
6278 +#define AuCpup_RENAME          BIT(2)          /* rename after cpup */
6279 +#define AuCpup_HOPEN           BIT(3)          /* call h_open_pre/post() in
6280 +                                                  cpup */
6281 +#define AuCpup_OVERWRITE       BIT(4)          /* allow overwriting the
6282 +                                                  existing entry */
6283 +#define AuCpup_RWDST           BIT(5)          /* force write target even if
6284 +                                                  the branch is marked as RO */
6285 +
6286 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6287 +#undef AuCpup_HOPEN
6288 +#define AuCpup_HOPEN           0
6289 +#endif
6290 +
6291 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6292 +#define au_fset_cpup(flags, name) \
6293 +       do { (flags) |= AuCpup_##name; } while (0)
6294 +#define au_fclr_cpup(flags, name) \
6295 +       do { (flags) &= ~AuCpup_##name; } while (0)
6296 +
6297 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6298 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6299 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6300 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6301 +
6302 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6303 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6304 +                        struct au_pin *pin,
6305 +                        struct dentry *h_parent, void *arg),
6306 +              void *arg);
6307 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6308 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6309 +
6310 +/* ---------------------------------------------------------------------- */
6311 +
6312 +/* keep timestamps when copyup */
6313 +struct au_dtime {
6314 +       struct dentry *dt_dentry;
6315 +       struct path dt_h_path;
6316 +       struct timespec64 dt_atime, dt_mtime;
6317 +};
6318 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6319 +                   struct path *h_path);
6320 +void au_dtime_revert(struct au_dtime *dt);
6321 +
6322 +#endif /* __KERNEL__ */
6323 +#endif /* __AUFS_CPUP_H__ */
6324 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6325 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6326 +++ linux/fs/aufs/dbgaufs.c     2024-05-13 17:13:00.618070924 +0200
6327 @@ -0,0 +1,526 @@
6328 +// SPDX-License-Identifier: GPL-2.0
6329 +/*
6330 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6331 + *
6332 + * This program is free software; you can redistribute it and/or modify
6333 + * it under the terms of the GNU General Public License as published by
6334 + * the Free Software Foundation; either version 2 of the License, or
6335 + * (at your option) any later version.
6336 + *
6337 + * This program is distributed in the hope that it will be useful,
6338 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6339 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6340 + * GNU General Public License for more details.
6341 + *
6342 + * You should have received a copy of the GNU General Public License
6343 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6344 + */
6345 +
6346 +/*
6347 + * debugfs interface
6348 + */
6349 +
6350 +#include <linux/debugfs.h>
6351 +#include "aufs.h"
6352 +
6353 +#ifndef CONFIG_SYSFS
6354 +#error DEBUG_FS depends upon SYSFS
6355 +#endif
6356 +
6357 +static struct dentry *dbgaufs;
6358 +static const mode_t dbgaufs_mode = 0444;
6359 +
6360 +/* 20 is max digits length of ulong 64 */
6361 +struct dbgaufs_arg {
6362 +       int n;
6363 +       char a[20 * 4];
6364 +};
6365 +
6366 +/*
6367 + * common function for all XINO files
6368 + */
6369 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6370 +                             struct file *file)
6371 +{
6372 +       void *p;
6373 +
6374 +       p = file->private_data;
6375 +       if (p) {
6376 +               /* this is struct dbgaufs_arg */
6377 +               AuDebugOn(!au_kfree_sz_test(p));
6378 +               au_kfree_do_rcu(p);
6379 +       }
6380 +       return 0;
6381 +}
6382 +
6383 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6384 +                          int cnt)
6385 +{
6386 +       int err;
6387 +       struct kstat st;
6388 +       struct dbgaufs_arg *p;
6389 +
6390 +       err = -ENOMEM;
6391 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6392 +       if (unlikely(!p))
6393 +               goto out;
6394 +
6395 +       err = 0;
6396 +       p->n = 0;
6397 +       file->private_data = p;
6398 +       if (!xf)
6399 +               goto out;
6400 +
6401 +       err = vfsub_getattr(&xf->f_path, &st);
6402 +       if (!err) {
6403 +               if (do_fcnt)
6404 +                       p->n = snprintf
6405 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6406 +                                cnt, st.blocks, st.blksize,
6407 +                                (long long)st.size);
6408 +               else
6409 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6410 +                                       st.blocks, st.blksize,
6411 +                                       (long long)st.size);
6412 +               AuDebugOn(p->n >= sizeof(p->a));
6413 +       } else {
6414 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6415 +               err = 0;
6416 +       }
6417 +
6418 +out:
6419 +       return err;
6420 +}
6421 +
6422 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6423 +                              size_t count, loff_t *ppos)
6424 +{
6425 +       struct dbgaufs_arg *p;
6426 +
6427 +       p = file->private_data;
6428 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6429 +}
6430 +
6431 +/* ---------------------------------------------------------------------- */
6432 +
6433 +struct dbgaufs_plink_arg {
6434 +       int n;
6435 +       char a[];
6436 +};
6437 +
6438 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6439 +                                struct file *file)
6440 +{
6441 +       free_page((unsigned long)file->private_data);
6442 +       return 0;
6443 +}
6444 +
6445 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6446 +{
6447 +       int err, i, limit;
6448 +       unsigned long n, sum;
6449 +       struct dbgaufs_plink_arg *p;
6450 +       struct au_sbinfo *sbinfo;
6451 +       struct super_block *sb;
6452 +       struct hlist_bl_head *hbl;
6453 +
6454 +       err = -ENOMEM;
6455 +       p = (void *)get_zeroed_page(GFP_NOFS);
6456 +       if (unlikely(!p))
6457 +               goto out;
6458 +
6459 +       err = -EFBIG;
6460 +       sbinfo = inode->i_private;
6461 +       sb = sbinfo->si_sb;
6462 +       si_noflush_read_lock(sb);
6463 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6464 +               limit = PAGE_SIZE - sizeof(p->n);
6465 +
6466 +               /* the number of buckets */
6467 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6468 +               p->n += n;
6469 +               limit -= n;
6470 +
6471 +               sum = 0;
6472 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6473 +                    i++, hbl++) {
6474 +                       n = au_hbl_count(hbl);
6475 +                       sum += n;
6476 +
6477 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6478 +                       p->n += n;
6479 +                       limit -= n;
6480 +                       if (unlikely(limit <= 0))
6481 +                               goto out_free;
6482 +               }
6483 +               p->a[p->n - 1] = '\n';
6484 +
6485 +               /* the sum of plinks */
6486 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6487 +               p->n += n;
6488 +               limit -= n;
6489 +               if (unlikely(limit <= 0))
6490 +                       goto out_free;
6491 +       } else {
6492 +#define str "1\n0\n0\n"
6493 +               p->n = sizeof(str) - 1;
6494 +               strscpy(p->a, str, sizeof(str));
6495 +#undef str
6496 +       }
6497 +       si_read_unlock(sb);
6498 +
6499 +       err = 0;
6500 +       file->private_data = p;
6501 +       goto out; /* success */
6502 +
6503 +out_free:
6504 +       free_page((unsigned long)p);
6505 +out:
6506 +       return err;
6507 +}
6508 +
6509 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6510 +                                 size_t count, loff_t *ppos)
6511 +{
6512 +       struct dbgaufs_plink_arg *p;
6513 +
6514 +       p = file->private_data;
6515 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6516 +}
6517 +
6518 +static const struct file_operations dbgaufs_plink_fop = {
6519 +       .owner          = THIS_MODULE,
6520 +       .open           = dbgaufs_plink_open,
6521 +       .release        = dbgaufs_plink_release,
6522 +       .read           = dbgaufs_plink_read
6523 +};
6524 +
6525 +/* ---------------------------------------------------------------------- */
6526 +
6527 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6528 +{
6529 +       int err;
6530 +       struct au_sbinfo *sbinfo;
6531 +       struct super_block *sb;
6532 +
6533 +       sbinfo = inode->i_private;
6534 +       sb = sbinfo->si_sb;
6535 +       si_noflush_read_lock(sb);
6536 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6537 +       si_read_unlock(sb);
6538 +       return err;
6539 +}
6540 +
6541 +static const struct file_operations dbgaufs_xib_fop = {
6542 +       .owner          = THIS_MODULE,
6543 +       .open           = dbgaufs_xib_open,
6544 +       .release        = dbgaufs_xi_release,
6545 +       .read           = dbgaufs_xi_read
6546 +};
6547 +
6548 +/* ---------------------------------------------------------------------- */
6549 +
6550 +#define DbgaufsXi_PREFIX "xi"
6551 +
6552 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6553 +{
6554 +       int err, idx;
6555 +       long l;
6556 +       aufs_bindex_t bindex;
6557 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6558 +       struct au_sbinfo *sbinfo;
6559 +       struct super_block *sb;
6560 +       struct au_xino *xi;
6561 +       struct file *xf;
6562 +       struct qstr *name;
6563 +       struct au_branch *br;
6564 +
6565 +       err = -ENOENT;
6566 +       name = &file->f_path.dentry->d_name;
6567 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6568 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6569 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6570 +               goto out;
6571 +
6572 +       AuDebugOn(name->len >= sizeof(a));
6573 +       memcpy(a, name->name, name->len);
6574 +       a[name->len] = '\0';
6575 +       p = strchr(a, '-');
6576 +       if (p)
6577 +               *p = '\0';
6578 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6579 +       if (unlikely(err))
6580 +               goto out;
6581 +       bindex = l;
6582 +       idx = 0;
6583 +       if (p) {
6584 +               err = kstrtol(p + 1, 10, &l);
6585 +               if (unlikely(err))
6586 +                       goto out;
6587 +               idx = l;
6588 +       }
6589 +
6590 +       err = -ENOENT;
6591 +       sbinfo = inode->i_private;
6592 +       sb = sbinfo->si_sb;
6593 +       si_noflush_read_lock(sb);
6594 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6595 +               goto out_si;
6596 +       br = au_sbr(sb, bindex);
6597 +       xi = br->br_xino;
6598 +       if (unlikely(idx >= xi->xi_nfile))
6599 +               goto out_si;
6600 +       xf = au_xino_file(xi, idx);
6601 +       if (xf)
6602 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6603 +                                     au_xino_count(br));
6604 +
6605 +out_si:
6606 +       si_read_unlock(sb);
6607 +out:
6608 +       AuTraceErr(err);
6609 +       return err;
6610 +}
6611 +
6612 +static const struct file_operations dbgaufs_xino_fop = {
6613 +       .owner          = THIS_MODULE,
6614 +       .open           = dbgaufs_xino_open,
6615 +       .release        = dbgaufs_xi_release,
6616 +       .read           = dbgaufs_xi_read
6617 +};
6618 +
6619 +void dbgaufs_xino_del(struct au_branch *br)
6620 +{
6621 +       struct dentry *dbgaufs;
6622 +
6623 +       dbgaufs = br->br_dbgaufs;
6624 +       if (!dbgaufs)
6625 +               return;
6626 +
6627 +       br->br_dbgaufs = NULL;
6628 +       /* debugfs acquires the parent i_mutex */
6629 +       lockdep_off();
6630 +       debugfs_remove(dbgaufs);
6631 +       lockdep_on();
6632 +}
6633 +
6634 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6635 +{
6636 +       aufs_bindex_t bbot;
6637 +       struct au_branch *br;
6638 +
6639 +       if (!au_sbi(sb)->si_dbgaufs)
6640 +               return;
6641 +
6642 +       bbot = au_sbbot(sb);
6643 +       for (; bindex <= bbot; bindex++) {
6644 +               br = au_sbr(sb, bindex);
6645 +               dbgaufs_xino_del(br);
6646 +       }
6647 +}
6648 +
6649 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6650 +                             unsigned int idx, struct dentry *parent,
6651 +                             struct au_sbinfo *sbinfo)
6652 +{
6653 +       struct au_branch *br;
6654 +       struct dentry *d;
6655 +       /* "xi" bindex(5) "-" idx(2) NULL */
6656 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6657 +
6658 +       if (!idx)
6659 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6660 +       else
6661 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6662 +                        bindex, idx);
6663 +       br = au_sbr(sb, bindex);
6664 +       if (br->br_dbgaufs) {
6665 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6666 +
6667 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6668 +                       /* debugfs acquires the parent i_mutex */
6669 +                       lockdep_off();
6670 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6671 +                                          name);
6672 +                       lockdep_on();
6673 +                       if (unlikely(!d))
6674 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6675 +                                       parent, name);
6676 +               }
6677 +       } else {
6678 +               lockdep_off();
6679 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6680 +                                                    sbinfo, &dbgaufs_xino_fop);
6681 +               lockdep_on();
6682 +               if (unlikely(!br->br_dbgaufs))
6683 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6684 +                               parent, name);
6685 +       }
6686 +}
6687 +
6688 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6689 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6690 +{
6691 +       struct au_branch *br;
6692 +       struct au_xino *xi;
6693 +       unsigned int u;
6694 +
6695 +       br = au_sbr(sb, bindex);
6696 +       xi = br->br_xino;
6697 +       for (u = 0; u < xi->xi_nfile; u++)
6698 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6699 +}
6700 +
6701 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6702 +{
6703 +       struct au_sbinfo *sbinfo;
6704 +       struct dentry *parent;
6705 +       aufs_bindex_t bbot;
6706 +
6707 +       if (!au_opt_test(au_mntflags(sb), XINO))
6708 +               return;
6709 +
6710 +       sbinfo = au_sbi(sb);
6711 +       parent = sbinfo->si_dbgaufs;
6712 +       if (!parent)
6713 +               return;
6714 +
6715 +       bbot = au_sbbot(sb);
6716 +       if (topdown)
6717 +               for (; bindex <= bbot; bindex++)
6718 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6719 +       else
6720 +               for (; bbot >= bindex; bbot--)
6721 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6722 +}
6723 +
6724 +/* ---------------------------------------------------------------------- */
6725 +
6726 +#ifdef CONFIG_AUFS_EXPORT
6727 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6728 +{
6729 +       int err;
6730 +       struct au_sbinfo *sbinfo;
6731 +       struct super_block *sb;
6732 +
6733 +       sbinfo = inode->i_private;
6734 +       sb = sbinfo->si_sb;
6735 +       si_noflush_read_lock(sb);
6736 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6737 +       si_read_unlock(sb);
6738 +       return err;
6739 +}
6740 +
6741 +static const struct file_operations dbgaufs_xigen_fop = {
6742 +       .owner          = THIS_MODULE,
6743 +       .open           = dbgaufs_xigen_open,
6744 +       .release        = dbgaufs_xi_release,
6745 +       .read           = dbgaufs_xi_read
6746 +};
6747 +
6748 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6749 +{
6750 +       int err;
6751 +
6752 +       /*
6753 +        * This function is a dynamic '__init' function actually,
6754 +        * so the tiny check for si_rwsem is unnecessary.
6755 +        */
6756 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6757 +
6758 +       err = -EIO;
6759 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6760 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6761 +                &dbgaufs_xigen_fop);
6762 +       if (sbinfo->si_dbgaufs_xigen)
6763 +               err = 0;
6764 +
6765 +       return err;
6766 +}
6767 +#else
6768 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6769 +{
6770 +       return 0;
6771 +}
6772 +#endif /* CONFIG_AUFS_EXPORT */
6773 +
6774 +/* ---------------------------------------------------------------------- */
6775 +
6776 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6777 +{
6778 +       /*
6779 +        * This function is a dynamic '__fin' function actually,
6780 +        * so the tiny check for si_rwsem is unnecessary.
6781 +        */
6782 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6783 +
6784 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6785 +       sbinfo->si_dbgaufs = NULL;
6786 +}
6787 +
6788 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6789 +{
6790 +       int err;
6791 +       char name[SysaufsSiNameLen];
6792 +
6793 +       /*
6794 +        * This function is a dynamic '__init' function actually,
6795 +        * so the tiny check for si_rwsem is unnecessary.
6796 +        */
6797 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6798 +
6799 +       err = -ENOENT;
6800 +       if (!dbgaufs) {
6801 +               AuErr1("/debug/aufs is uninitialized\n");
6802 +               goto out;
6803 +       }
6804 +
6805 +       err = -EIO;
6806 +       sysaufs_name(sbinfo, name);
6807 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6808 +       if (unlikely(!sbinfo->si_dbgaufs))
6809 +               goto out;
6810 +
6811 +       /* regardless plink/noplink option */
6812 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6813 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6814 +                &dbgaufs_plink_fop);
6815 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6816 +               goto out_dir;
6817 +
6818 +       /* regardless xino/noxino option */
6819 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6820 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6821 +                &dbgaufs_xib_fop);
6822 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6823 +               goto out_dir;
6824 +
6825 +       err = dbgaufs_xigen_init(sbinfo);
6826 +       if (!err)
6827 +               goto out; /* success */
6828 +
6829 +out_dir:
6830 +       dbgaufs_si_fin(sbinfo);
6831 +out:
6832 +       if (unlikely(err))
6833 +               pr_err("debugfs/aufs failed\n");
6834 +       return err;
6835 +}
6836 +
6837 +/* ---------------------------------------------------------------------- */
6838 +
6839 +void dbgaufs_fin(void)
6840 +{
6841 +       debugfs_remove(dbgaufs);
6842 +}
6843 +
6844 +int __init dbgaufs_init(void)
6845 +{
6846 +       int err;
6847 +
6848 +       err = -EIO;
6849 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6850 +       if (dbgaufs)
6851 +               err = 0;
6852 +       return err;
6853 +}
6854 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6855 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6856 +++ linux/fs/aufs/dbgaufs.h     2024-05-13 17:13:00.618070924 +0200
6857 @@ -0,0 +1,53 @@
6858 +/* SPDX-License-Identifier: GPL-2.0 */
6859 +/*
6860 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6861 + *
6862 + * This program is free software; you can redistribute it and/or modify
6863 + * it under the terms of the GNU General Public License as published by
6864 + * the Free Software Foundation; either version 2 of the License, or
6865 + * (at your option) any later version.
6866 + *
6867 + * This program is distributed in the hope that it will be useful,
6868 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6869 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6870 + * GNU General Public License for more details.
6871 + *
6872 + * You should have received a copy of the GNU General Public License
6873 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6874 + */
6875 +
6876 +/*
6877 + * debugfs interface
6878 + */
6879 +
6880 +#ifndef __DBGAUFS_H__
6881 +#define __DBGAUFS_H__
6882 +
6883 +#ifdef __KERNEL__
6884 +
6885 +struct super_block;
6886 +struct au_sbinfo;
6887 +struct au_branch;
6888 +
6889 +#ifdef CONFIG_DEBUG_FS
6890 +/* dbgaufs.c */
6891 +void dbgaufs_xino_del(struct au_branch *br);
6892 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6893 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6894 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6895 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6896 +void dbgaufs_fin(void);
6897 +int __init dbgaufs_init(void);
6898 +#else
6899 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6900 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6901 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6902 +          int topdown)
6903 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6904 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6905 +AuStubVoid(dbgaufs_fin, void)
6906 +AuStubInt0(__init dbgaufs_init, void)
6907 +#endif /* CONFIG_DEBUG_FS */
6908 +
6909 +#endif /* __KERNEL__ */
6910 +#endif /* __DBGAUFS_H__ */
6911 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6912 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6913 +++ linux/fs/aufs/dcsub.c       2024-05-13 17:13:00.618070924 +0200
6914 @@ -0,0 +1,225 @@
6915 +// SPDX-License-Identifier: GPL-2.0
6916 +/*
6917 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6918 + *
6919 + * This program is free software; you can redistribute it and/or modify
6920 + * it under the terms of the GNU General Public License as published by
6921 + * the Free Software Foundation; either version 2 of the License, or
6922 + * (at your option) any later version.
6923 + *
6924 + * This program is distributed in the hope that it will be useful,
6925 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6926 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6927 + * GNU General Public License for more details.
6928 + *
6929 + * You should have received a copy of the GNU General Public License
6930 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6931 + */
6932 +
6933 +/*
6934 + * sub-routines for dentry cache
6935 + */
6936 +
6937 +#include "aufs.h"
6938 +
6939 +static void au_dpage_free(struct au_dpage *dpage)
6940 +{
6941 +       int i;
6942 +       struct dentry **p;
6943 +
6944 +       p = dpage->dentries;
6945 +       for (i = 0; i < dpage->ndentry; i++)
6946 +               dput(*p++);
6947 +       free_page((unsigned long)dpage->dentries);
6948 +}
6949 +
6950 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6951 +{
6952 +       int err;
6953 +       void *p;
6954 +
6955 +       err = -ENOMEM;
6956 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6957 +       if (unlikely(!dpages->dpages))
6958 +               goto out;
6959 +
6960 +       p = (void *)__get_free_page(gfp);
6961 +       if (unlikely(!p))
6962 +               goto out_dpages;
6963 +
6964 +       dpages->dpages[0].ndentry = 0;
6965 +       dpages->dpages[0].dentries = p;
6966 +       dpages->ndpage = 1;
6967 +       return 0; /* success */
6968 +
6969 +out_dpages:
6970 +       au_kfree_try_rcu(dpages->dpages);
6971 +out:
6972 +       return err;
6973 +}
6974 +
6975 +void au_dpages_free(struct au_dcsub_pages *dpages)
6976 +{
6977 +       int i;
6978 +       struct au_dpage *p;
6979 +
6980 +       p = dpages->dpages;
6981 +       for (i = 0; i < dpages->ndpage; i++)
6982 +               au_dpage_free(p++);
6983 +       au_kfree_try_rcu(dpages->dpages);
6984 +}
6985 +
6986 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6987 +                           struct dentry *dentry, gfp_t gfp)
6988 +{
6989 +       int err, sz;
6990 +       struct au_dpage *dpage;
6991 +       void *p;
6992 +
6993 +       dpage = dpages->dpages + dpages->ndpage - 1;
6994 +       sz = PAGE_SIZE / sizeof(dentry);
6995 +       if (unlikely(dpage->ndentry >= sz)) {
6996 +               AuLabel(new dpage);
6997 +               err = -ENOMEM;
6998 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6999 +               p = au_kzrealloc(dpages->dpages, sz,
7000 +                                sz + sizeof(*dpages->dpages), gfp,
7001 +                                /*may_shrink*/0);
7002 +               if (unlikely(!p))
7003 +                       goto out;
7004 +
7005 +               dpages->dpages = p;
7006 +               dpage = dpages->dpages + dpages->ndpage;
7007 +               p = (void *)__get_free_page(gfp);
7008 +               if (unlikely(!p))
7009 +                       goto out;
7010 +
7011 +               dpage->ndentry = 0;
7012 +               dpage->dentries = p;
7013 +               dpages->ndpage++;
7014 +       }
7015 +
7016 +       AuDebugOn(au_dcount(dentry) <= 0);
7017 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7018 +       return 0; /* success */
7019 +
7020 +out:
7021 +       return err;
7022 +}
7023 +
7024 +/* todo: BAD approach */
7025 +/* copied from linux/fs/dcache.c */
7026 +enum d_walk_ret {
7027 +       D_WALK_CONTINUE,
7028 +       D_WALK_QUIT,
7029 +       D_WALK_NORETRY,
7030 +       D_WALK_SKIP,
7031 +};
7032 +
7033 +extern void d_walk(struct dentry *parent, void *data,
7034 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7035 +
7036 +struct ac_dpages_arg {
7037 +       int err;
7038 +       struct au_dcsub_pages *dpages;
7039 +       struct super_block *sb;
7040 +       au_dpages_test test;
7041 +       void *arg;
7042 +};
7043 +
7044 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7045 +{
7046 +       enum d_walk_ret ret;
7047 +       struct ac_dpages_arg *arg = _arg;
7048 +
7049 +       ret = D_WALK_CONTINUE;
7050 +       if (dentry->d_sb == arg->sb
7051 +           && !IS_ROOT(dentry)
7052 +           && au_dcount(dentry) > 0
7053 +           && au_di(dentry)
7054 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7055 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7056 +               if (unlikely(arg->err))
7057 +                       ret = D_WALK_QUIT;
7058 +       }
7059 +
7060 +       return ret;
7061 +}
7062 +
7063 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7064 +                  au_dpages_test test, void *arg)
7065 +{
7066 +       struct ac_dpages_arg args = {
7067 +               .err    = 0,
7068 +               .dpages = dpages,
7069 +               .sb     = root->d_sb,
7070 +               .test   = test,
7071 +               .arg    = arg
7072 +       };
7073 +
7074 +       d_walk(root, &args, au_call_dpages_append);
7075 +
7076 +       return args.err;
7077 +}
7078 +
7079 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7080 +                      int do_include, au_dpages_test test, void *arg)
7081 +{
7082 +       int err;
7083 +
7084 +       err = 0;
7085 +       write_seqlock(&rename_lock);
7086 +       spin_lock(&dentry->d_lock);
7087 +       if (do_include
7088 +           && au_dcount(dentry) > 0
7089 +           && (!test || test(dentry, arg)))
7090 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7091 +       spin_unlock(&dentry->d_lock);
7092 +       if (unlikely(err))
7093 +               goto out;
7094 +
7095 +       /*
7096 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7097 +        * mount
7098 +        */
7099 +       while (!IS_ROOT(dentry)) {
7100 +               dentry = dentry->d_parent; /* rename_lock is locked */
7101 +               spin_lock(&dentry->d_lock);
7102 +               if (au_dcount(dentry) > 0
7103 +                   && (!test || test(dentry, arg)))
7104 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7105 +               spin_unlock(&dentry->d_lock);
7106 +               if (unlikely(err))
7107 +                       break;
7108 +       }
7109 +
7110 +out:
7111 +       write_sequnlock(&rename_lock);
7112 +       return err;
7113 +}
7114 +
7115 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7116 +{
7117 +       return au_di(dentry) && dentry->d_sb == arg;
7118 +}
7119 +
7120 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7121 +                           struct dentry *dentry, int do_include)
7122 +{
7123 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7124 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7125 +}
7126 +
7127 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7128 +{
7129 +       struct path path[2] = {
7130 +               {
7131 +                       .dentry = d1
7132 +               },
7133 +               {
7134 +                       .dentry = d2
7135 +               }
7136 +       };
7137 +
7138 +       return path_is_under(path + 0, path + 1);
7139 +}
7140 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7141 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7142 +++ linux/fs/aufs/dcsub.h       2024-05-13 17:13:00.618070924 +0200
7143 @@ -0,0 +1,137 @@
7144 +/* SPDX-License-Identifier: GPL-2.0 */
7145 +/*
7146 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7147 + *
7148 + * This program is free software; you can redistribute it and/or modify
7149 + * it under the terms of the GNU General Public License as published by
7150 + * the Free Software Foundation; either version 2 of the License, or
7151 + * (at your option) any later version.
7152 + *
7153 + * This program is distributed in the hope that it will be useful,
7154 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7155 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7156 + * GNU General Public License for more details.
7157 + *
7158 + * You should have received a copy of the GNU General Public License
7159 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7160 + */
7161 +
7162 +/*
7163 + * sub-routines for dentry cache
7164 + */
7165 +
7166 +#ifndef __AUFS_DCSUB_H__
7167 +#define __AUFS_DCSUB_H__
7168 +
7169 +#ifdef __KERNEL__
7170 +
7171 +#include <linux/dcache.h>
7172 +#include <linux/fs.h>
7173 +
7174 +struct au_dpage {
7175 +       int ndentry;
7176 +       struct dentry **dentries;
7177 +};
7178 +
7179 +struct au_dcsub_pages {
7180 +       int ndpage;
7181 +       struct au_dpage *dpages;
7182 +};
7183 +
7184 +/* ---------------------------------------------------------------------- */
7185 +
7186 +/* dcsub.c */
7187 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7188 +void au_dpages_free(struct au_dcsub_pages *dpages);
7189 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7190 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7191 +                  au_dpages_test test, void *arg);
7192 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7193 +                      int do_include, au_dpages_test test, void *arg);
7194 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7195 +                           struct dentry *dentry, int do_include);
7196 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7197 +
7198 +/* ---------------------------------------------------------------------- */
7199 +
7200 +/*
7201 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7202 + * include/linux/dcache.h. Try them (in the future).
7203 + */
7204 +
7205 +static inline int au_d_hashed_positive(struct dentry *d)
7206 +{
7207 +       int err;
7208 +       struct inode *inode = d_inode(d);
7209 +
7210 +       err = 0;
7211 +       if (unlikely(d_unhashed(d)
7212 +                    || d_is_negative(d)
7213 +                    || !inode->i_nlink))
7214 +               err = -ENOENT;
7215 +       return err;
7216 +}
7217 +
7218 +static inline int au_d_linkable(struct dentry *d)
7219 +{
7220 +       int err;
7221 +       struct inode *inode = d_inode(d);
7222 +
7223 +       err = au_d_hashed_positive(d);
7224 +       if (err
7225 +           && d_is_positive(d)
7226 +           && (inode->i_state & I_LINKABLE))
7227 +               err = 0;
7228 +       return err;
7229 +}
7230 +
7231 +static inline int au_d_alive(struct dentry *d)
7232 +{
7233 +       int err;
7234 +       struct inode *inode;
7235 +
7236 +       err = 0;
7237 +       if (!IS_ROOT(d))
7238 +               err = au_d_hashed_positive(d);
7239 +       else {
7240 +               inode = d_inode(d);
7241 +               if (unlikely(d_unlinked(d)
7242 +                            || d_is_negative(d)
7243 +                            || !inode->i_nlink))
7244 +                       err = -ENOENT;
7245 +       }
7246 +       return err;
7247 +}
7248 +
7249 +static inline int au_alive_dir(struct dentry *d)
7250 +{
7251 +       int err;
7252 +
7253 +       err = au_d_alive(d);
7254 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7255 +               err = -ENOENT;
7256 +       return err;
7257 +}
7258 +
7259 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7260 +{
7261 +       return a->len == b->len
7262 +               && !memcmp(a->name, b->name, a->len);
7263 +}
7264 +
7265 +/*
7266 + * by the commit
7267 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7268 + *                     taking d_lock
7269 + * the type of d_lockref.count became int, but the inlined function d_count()
7270 + * still returns unsigned int.
7271 + * I don't know why. Maybe it is for every d_count() users?
7272 + * Anyway au_dcount() lives on.
7273 + */
7274 +static inline int au_dcount(struct dentry *d)
7275 +{
7276 +       return (int)d_count(d);
7277 +}
7278 +
7279 +#endif /* __KERNEL__ */
7280 +#endif /* __AUFS_DCSUB_H__ */
7281 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7282 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7283 +++ linux/fs/aufs/debug.c       2024-05-13 17:13:00.618070924 +0200
7284 @@ -0,0 +1,448 @@
7285 +// SPDX-License-Identifier: GPL-2.0
7286 +/*
7287 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7288 + *
7289 + * This program is free software; you can redistribute it and/or modify
7290 + * it under the terms of the GNU General Public License as published by
7291 + * the Free Software Foundation; either version 2 of the License, or
7292 + * (at your option) any later version.
7293 + *
7294 + * This program is distributed in the hope that it will be useful,
7295 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7296 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7297 + * GNU General Public License for more details.
7298 + *
7299 + * You should have received a copy of the GNU General Public License
7300 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7301 + */
7302 +
7303 +/*
7304 + * debug print functions
7305 + */
7306 +
7307 +#include <linux/iversion.h>
7308 +#include "aufs.h"
7309 +
7310 +/* Returns 0, or -errno.  arg is in kp->arg. */
7311 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7312 +{
7313 +       int err, n;
7314 +
7315 +       err = kstrtoint(val, 0, &n);
7316 +       if (!err) {
7317 +               if (n > 0)
7318 +                       au_debug_on();
7319 +               else
7320 +                       au_debug_off();
7321 +       }
7322 +       return err;
7323 +}
7324 +
7325 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7326 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7327 +{
7328 +       atomic_t *a;
7329 +
7330 +       a = kp->arg;
7331 +       return sprintf(buffer, "%d", atomic_read(a));
7332 +}
7333 +
7334 +static const struct kernel_param_ops param_ops_atomic_t = {
7335 +       .set = param_atomic_t_set,
7336 +       .get = param_atomic_t_get
7337 +       /* void (*free)(void *arg) */
7338 +};
7339 +
7340 +atomic_t aufs_debug = ATOMIC_INIT(0);
7341 +MODULE_PARM_DESC(debug, "debug print");
7342 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7343 +
7344 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7345 +char *au_plevel = KERN_DEBUG;
7346 +#define dpri(fmt, ...) do {                                    \
7347 +       if ((au_plevel                                          \
7348 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7349 +           || au_debug_test())                                 \
7350 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7351 +} while (0)
7352 +
7353 +/* ---------------------------------------------------------------------- */
7354 +
7355 +void au_dpri_whlist(struct au_nhash *whlist)
7356 +{
7357 +       unsigned long ul, n;
7358 +       struct hlist_head *head;
7359 +       struct au_vdir_wh *pos;
7360 +
7361 +       n = whlist->nh_num;
7362 +       head = whlist->nh_head;
7363 +       for (ul = 0; ul < n; ul++) {
7364 +               hlist_for_each_entry(pos, head, wh_hash)
7365 +                       dpri("b%d, %.*s, %d\n",
7366 +                            pos->wh_bindex,
7367 +                            pos->wh_str.len, pos->wh_str.name,
7368 +                            pos->wh_str.len);
7369 +               head++;
7370 +       }
7371 +}
7372 +
7373 +void au_dpri_vdir(struct au_vdir *vdir)
7374 +{
7375 +       unsigned long ul;
7376 +       union au_vdir_deblk_p p;
7377 +       unsigned char *o;
7378 +
7379 +       if (!vdir || IS_ERR(vdir)) {
7380 +               dpri("err %ld\n", PTR_ERR(vdir));
7381 +               return;
7382 +       }
7383 +
7384 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7385 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7386 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7387 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7388 +               p.deblk = vdir->vd_deblk[ul];
7389 +               o = p.deblk;
7390 +               dpri("[%lu]: %p\n", ul, o);
7391 +       }
7392 +}
7393 +
7394 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7395 +                       struct dentry *wh)
7396 +{
7397 +       char *n = NULL;
7398 +       int l = 0;
7399 +       struct timespec64 ctime;
7400 +
7401 +       if (!inode || IS_ERR(inode)) {
7402 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7403 +               return -1;
7404 +       }
7405 +
7406 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7407 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7408 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7409 +       if (wh) {
7410 +               n = (void *)wh->d_name.name;
7411 +               l = wh->d_name.len;
7412 +       }
7413 +
7414 +       ctime = inode_get_ctime(inode);
7415 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7416 +            " acl %p, def_acl %p,"
7417 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7418 +            bindex, inode,
7419 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7420 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7421 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7422 +            inode->i_acl, inode->i_default_acl,
7423 +            hn, (long long)timespec64_to_ns(&ctime) & 0x0ffff,
7424 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7425 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7426 +            inode->i_generation,
7427 +            l ? ", wh " : "", l, n);
7428 +       return 0;
7429 +}
7430 +
7431 +void au_dpri_inode(struct inode *inode)
7432 +{
7433 +       struct au_iinfo *iinfo;
7434 +       struct au_hinode *hi;
7435 +       aufs_bindex_t bindex;
7436 +       int err, hn;
7437 +
7438 +       err = do_pri_inode(-1, inode, -1, NULL);
7439 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7440 +               return;
7441 +
7442 +       iinfo = au_ii(inode);
7443 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7444 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7445 +       if (iinfo->ii_btop < 0)
7446 +               return;
7447 +       hn = 0;
7448 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7449 +               hi = au_hinode(iinfo, bindex);
7450 +               hn = !!au_hn(hi);
7451 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7452 +       }
7453 +}
7454 +
7455 +void au_dpri_dalias(struct inode *inode)
7456 +{
7457 +       struct dentry *d;
7458 +
7459 +       spin_lock(&inode->i_lock);
7460 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7461 +               au_dpri_dentry(d);
7462 +       spin_unlock(&inode->i_lock);
7463 +}
7464 +
7465 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7466 +{
7467 +       struct dentry *wh = NULL;
7468 +       int hn;
7469 +       struct inode *inode;
7470 +       struct au_iinfo *iinfo;
7471 +       struct au_hinode *hi;
7472 +
7473 +       if (!dentry || IS_ERR(dentry)) {
7474 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7475 +               return -1;
7476 +       }
7477 +       /* do not call dget_parent() here */
7478 +       /* note: access d_xxx without d_lock */
7479 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7480 +            bindex, dentry, dentry,
7481 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7482 +            au_dcount(dentry), dentry->d_flags,
7483 +            d_unhashed(dentry) ? "un" : "");
7484 +       hn = -1;
7485 +       inode = NULL;
7486 +       if (d_is_positive(dentry))
7487 +               inode = d_inode(dentry);
7488 +       if (inode
7489 +           && au_test_aufs(dentry->d_sb)
7490 +           && bindex >= 0
7491 +           && !au_is_bad_inode(inode)) {
7492 +               iinfo = au_ii(inode);
7493 +               hi = au_hinode(iinfo, bindex);
7494 +               hn = !!au_hn(hi);
7495 +               wh = hi->hi_whdentry;
7496 +       }
7497 +       do_pri_inode(bindex, inode, hn, wh);
7498 +       return 0;
7499 +}
7500 +
7501 +void au_dpri_dentry(struct dentry *dentry)
7502 +{
7503 +       struct au_dinfo *dinfo;
7504 +       aufs_bindex_t bindex;
7505 +       int err;
7506 +
7507 +       err = do_pri_dentry(-1, dentry);
7508 +       if (err || !au_test_aufs(dentry->d_sb))
7509 +               return;
7510 +
7511 +       dinfo = au_di(dentry);
7512 +       if (!dinfo)
7513 +               return;
7514 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7515 +            dinfo->di_btop, dinfo->di_bbot,
7516 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7517 +            dinfo->di_tmpfile);
7518 +       if (dinfo->di_btop < 0)
7519 +               return;
7520 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7521 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7522 +}
7523 +
7524 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7525 +{
7526 +       char a[32];
7527 +
7528 +       if (!file || IS_ERR(file)) {
7529 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7530 +               return -1;
7531 +       }
7532 +       a[0] = 0;
7533 +       if (bindex < 0
7534 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7535 +           && au_test_aufs(file->f_path.dentry->d_sb)
7536 +           && au_fi(file))
7537 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7538 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7539 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7540 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7541 +            file->f_version, file->f_pos, a);
7542 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7543 +               do_pri_dentry(bindex, file->f_path.dentry);
7544 +       return 0;
7545 +}
7546 +
7547 +void au_dpri_file(struct file *file)
7548 +{
7549 +       struct au_finfo *finfo;
7550 +       struct au_fidir *fidir;
7551 +       struct au_hfile *hfile;
7552 +       aufs_bindex_t bindex;
7553 +       int err;
7554 +
7555 +       err = do_pri_file(-1, file);
7556 +       if (err
7557 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7558 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7559 +               return;
7560 +
7561 +       finfo = au_fi(file);
7562 +       if (!finfo)
7563 +               return;
7564 +       if (finfo->fi_btop < 0)
7565 +               return;
7566 +       fidir = finfo->fi_hdir;
7567 +       if (!fidir)
7568 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7569 +       else
7570 +               for (bindex = finfo->fi_btop;
7571 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7572 +                    bindex++) {
7573 +                       hfile = fidir->fd_hfile + bindex;
7574 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7575 +               }
7576 +}
7577 +
7578 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7579 +{
7580 +       struct vfsmount *mnt;
7581 +       struct super_block *sb;
7582 +
7583 +       if (!br || IS_ERR(br))
7584 +               goto out;
7585 +       mnt = au_br_mnt(br);
7586 +       if (!mnt || IS_ERR(mnt))
7587 +               goto out;
7588 +       sb = mnt->mnt_sb;
7589 +       if (!sb || IS_ERR(sb))
7590 +               goto out;
7591 +
7592 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7593 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7594 +            "xino %d\n",
7595 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7596 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7597 +            sb->s_flags, sb->s_count,
7598 +            atomic_read(&sb->s_active),
7599 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7600 +       return 0;
7601 +
7602 +out:
7603 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7604 +       return -1;
7605 +}
7606 +
7607 +void au_dpri_sb(struct super_block *sb)
7608 +{
7609 +       struct au_sbinfo *sbinfo;
7610 +       aufs_bindex_t bindex;
7611 +       int err;
7612 +       /* to reduce stack size */
7613 +       struct {
7614 +               struct vfsmount mnt;
7615 +               struct au_branch fake;
7616 +       } *a;
7617 +
7618 +       /* this function can be called from magic sysrq */
7619 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7620 +       if (unlikely(!a)) {
7621 +               dpri("no memory\n");
7622 +               return;
7623 +       }
7624 +
7625 +       a->mnt.mnt_sb = sb;
7626 +       a->fake.br_path.mnt = &a->mnt;
7627 +       err = do_pri_br(-1, &a->fake);
7628 +       au_kfree_rcu(a);
7629 +       dpri("dev 0x%x\n", sb->s_dev);
7630 +       if (err || !au_test_aufs(sb))
7631 +               return;
7632 +
7633 +       sbinfo = au_sbi(sb);
7634 +       if (!sbinfo)
7635 +               return;
7636 +       dpri("nw %d, gen %u, kobj %d\n",
7637 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7638 +            kref_read(&sbinfo->si_kobj.kref));
7639 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7640 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7641 +}
7642 +
7643 +/* ---------------------------------------------------------------------- */
7644 +
7645 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7646 +{
7647 +       struct inode *h_inode, *inode = d_inode(dentry);
7648 +       struct dentry *h_dentry;
7649 +       aufs_bindex_t bindex, bbot, bi;
7650 +
7651 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7652 +               return;
7653 +
7654 +       bbot = au_dbbot(dentry);
7655 +       bi = au_ibbot(inode);
7656 +       if (bi < bbot)
7657 +               bbot = bi;
7658 +       bindex = au_dbtop(dentry);
7659 +       bi = au_ibtop(inode);
7660 +       if (bi > bindex)
7661 +               bindex = bi;
7662 +
7663 +       for (; bindex <= bbot; bindex++) {
7664 +               h_dentry = au_h_dptr(dentry, bindex);
7665 +               if (!h_dentry)
7666 +                       continue;
7667 +               h_inode = au_h_iptr(inode, bindex);
7668 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7669 +                       au_debug_on();
7670 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7671 +                       AuDbgDentry(dentry);
7672 +                       AuDbgInode(inode);
7673 +                       au_debug_off();
7674 +                       if (au_test_fuse(h_inode->i_sb))
7675 +                               WARN_ON_ONCE(1);
7676 +                       else
7677 +                               BUG();
7678 +               }
7679 +       }
7680 +}
7681 +
7682 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7683 +{
7684 +       int err, i, j;
7685 +       struct au_dcsub_pages dpages;
7686 +       struct au_dpage *dpage;
7687 +       struct dentry **dentries;
7688 +
7689 +       err = au_dpages_init(&dpages, GFP_NOFS);
7690 +       AuDebugOn(err);
7691 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7692 +       AuDebugOn(err);
7693 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7694 +               dpage = dpages.dpages + i;
7695 +               dentries = dpage->dentries;
7696 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7697 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7698 +       }
7699 +       au_dpages_free(&dpages);
7700 +}
7701 +
7702 +void au_dbg_verify_kthread(void)
7703 +{
7704 +       if (au_wkq_test()) {
7705 +               au_dbg_blocked();
7706 +               /*
7707 +                * It may be recursive, but udba=notify between two aufs mounts,
7708 +                * where a single ro branch is shared, is not a problem.
7709 +                */
7710 +               /* WARN_ON(1); */
7711 +       }
7712 +}
7713 +
7714 +/* ---------------------------------------------------------------------- */
7715 +
7716 +int __init au_debug_init(void)
7717 +{
7718 +       aufs_bindex_t bindex;
7719 +       struct au_vdir_destr destr;
7720 +
7721 +       bindex = -1;
7722 +       AuDebugOn(bindex >= 0);
7723 +
7724 +       destr.len = -1;
7725 +       AuDebugOn(destr.len < NAME_MAX);
7726 +
7727 +#ifdef CONFIG_4KSTACKS
7728 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7729 +#endif
7730 +
7731 +       return 0;
7732 +}
7733 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7734 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7735 +++ linux/fs/aufs/debug.h       2024-05-13 17:13:00.618070924 +0200
7736 @@ -0,0 +1,226 @@
7737 +/* SPDX-License-Identifier: GPL-2.0 */
7738 +/*
7739 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7740 + *
7741 + * This program is free software; you can redistribute it and/or modify
7742 + * it under the terms of the GNU General Public License as published by
7743 + * the Free Software Foundation; either version 2 of the License, or
7744 + * (at your option) any later version.
7745 + *
7746 + * This program is distributed in the hope that it will be useful,
7747 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7748 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7749 + * GNU General Public License for more details.
7750 + *
7751 + * You should have received a copy of the GNU General Public License
7752 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7753 + */
7754 +
7755 +/*
7756 + * debug print functions
7757 + */
7758 +
7759 +#ifndef __AUFS_DEBUG_H__
7760 +#define __AUFS_DEBUG_H__
7761 +
7762 +#ifdef __KERNEL__
7763 +
7764 +#include <linux/atomic.h>
7765 +#include <linux/module.h>
7766 +#include <linux/kallsyms.h>
7767 +#include <linux/sysrq.h>
7768 +
7769 +#ifdef CONFIG_AUFS_DEBUG
7770 +#define AuDebugOn(a)           BUG_ON(a)
7771 +
7772 +/* module parameter */
7773 +extern atomic_t aufs_debug;
7774 +static inline void au_debug_on(void)
7775 +{
7776 +       atomic_inc(&aufs_debug);
7777 +}
7778 +static inline void au_debug_off(void)
7779 +{
7780 +       atomic_dec_if_positive(&aufs_debug);
7781 +}
7782 +
7783 +static inline int au_debug_test(void)
7784 +{
7785 +       return atomic_read(&aufs_debug) > 0;
7786 +}
7787 +#else
7788 +#define AuDebugOn(a)           do {} while (0)
7789 +AuStubVoid(au_debug_on, void)
7790 +AuStubVoid(au_debug_off, void)
7791 +AuStubInt0(au_debug_test, void)
7792 +#endif /* CONFIG_AUFS_DEBUG */
7793 +
7794 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7795 +
7796 +/* ---------------------------------------------------------------------- */
7797 +
7798 +/* debug print */
7799 +
7800 +#define AuDbg(fmt, ...) do { \
7801 +       if (au_debug_test()) \
7802 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7803 +} while (0)
7804 +#define AuLabel(l)             AuDbg(#l "\n")
7805 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7806 +#define AuWarn1(fmt, ...) do { \
7807 +       static unsigned char _c; \
7808 +       if (!_c++) \
7809 +               pr_warn(fmt, ##__VA_ARGS__); \
7810 +} while (0)
7811 +
7812 +#define AuErr1(fmt, ...) do { \
7813 +       static unsigned char _c; \
7814 +       if (!_c++) \
7815 +               pr_err(fmt, ##__VA_ARGS__); \
7816 +} while (0)
7817 +
7818 +#define AuIOErr1(fmt, ...) do { \
7819 +       static unsigned char _c; \
7820 +       if (!_c++) \
7821 +               AuIOErr(fmt, ##__VA_ARGS__); \
7822 +} while (0)
7823 +
7824 +#define AuUnsupportMsg "This operation is not supported." \
7825 +                       " Please report this application to aufs-users ML."
7826 +#define AuUnsupport(fmt, ...) do { \
7827 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7828 +       dump_stack(); \
7829 +} while (0)
7830 +
7831 +#define AuTraceErr(e) do { \
7832 +       if (unlikely((e) < 0)) \
7833 +               AuDbg("err %d\n", (int)(e)); \
7834 +} while (0)
7835 +
7836 +#define AuTraceErrPtr(p) do { \
7837 +       if (IS_ERR(p)) \
7838 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7839 +} while (0)
7840 +
7841 +/* dirty macros for debug print, use with "%.*s" and caution */
7842 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7843 +
7844 +/* ---------------------------------------------------------------------- */
7845 +
7846 +struct dentry;
7847 +#ifdef CONFIG_AUFS_DEBUG
7848 +extern struct mutex au_dbg_mtx;
7849 +extern char *au_plevel;
7850 +struct au_nhash;
7851 +void au_dpri_whlist(struct au_nhash *whlist);
7852 +struct au_vdir;
7853 +void au_dpri_vdir(struct au_vdir *vdir);
7854 +struct inode;
7855 +void au_dpri_inode(struct inode *inode);
7856 +void au_dpri_dalias(struct inode *inode);
7857 +void au_dpri_dentry(struct dentry *dentry);
7858 +struct file;
7859 +void au_dpri_file(struct file *filp);
7860 +struct super_block;
7861 +void au_dpri_sb(struct super_block *sb);
7862 +
7863 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7864 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7865 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7866 +void au_dbg_verify_kthread(void);
7867 +
7868 +int __init au_debug_init(void);
7869 +
7870 +#define AuDbgWhlist(w) do { \
7871 +       mutex_lock(&au_dbg_mtx); \
7872 +       AuDbg(#w "\n"); \
7873 +       au_dpri_whlist(w); \
7874 +       mutex_unlock(&au_dbg_mtx); \
7875 +} while (0)
7876 +
7877 +#define AuDbgVdir(v) do { \
7878 +       mutex_lock(&au_dbg_mtx); \
7879 +       AuDbg(#v "\n"); \
7880 +       au_dpri_vdir(v); \
7881 +       mutex_unlock(&au_dbg_mtx); \
7882 +} while (0)
7883 +
7884 +#define AuDbgInode(i) do { \
7885 +       mutex_lock(&au_dbg_mtx); \
7886 +       AuDbg(#i "\n"); \
7887 +       au_dpri_inode(i); \
7888 +       mutex_unlock(&au_dbg_mtx); \
7889 +} while (0)
7890 +
7891 +#define AuDbgDAlias(i) do { \
7892 +       mutex_lock(&au_dbg_mtx); \
7893 +       AuDbg(#i "\n"); \
7894 +       au_dpri_dalias(i); \
7895 +       mutex_unlock(&au_dbg_mtx); \
7896 +} while (0)
7897 +
7898 +#define AuDbgDentry(d) do { \
7899 +       mutex_lock(&au_dbg_mtx); \
7900 +       AuDbg(#d "\n"); \
7901 +       au_dpri_dentry(d); \
7902 +       mutex_unlock(&au_dbg_mtx); \
7903 +} while (0)
7904 +
7905 +#define AuDbgFile(f) do { \
7906 +       mutex_lock(&au_dbg_mtx); \
7907 +       AuDbg(#f "\n"); \
7908 +       au_dpri_file(f); \
7909 +       mutex_unlock(&au_dbg_mtx); \
7910 +} while (0)
7911 +
7912 +#define AuDbgSb(sb) do { \
7913 +       mutex_lock(&au_dbg_mtx); \
7914 +       AuDbg(#sb "\n"); \
7915 +       au_dpri_sb(sb); \
7916 +       mutex_unlock(&au_dbg_mtx); \
7917 +} while (0)
7918 +
7919 +#define AuDbgSym(addr) do {                            \
7920 +       char sym[KSYM_SYMBOL_LEN];                      \
7921 +       sprint_symbol(sym, (unsigned long)addr);        \
7922 +       AuDbg("%s\n", sym);                             \
7923 +} while (0)
7924 +#else
7925 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7926 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7927 +AuStubVoid(au_dbg_verify_kthread, void)
7928 +AuStubInt0(__init au_debug_init, void)
7929 +
7930 +#define AuDbgWhlist(w)         do {} while (0)
7931 +#define AuDbgVdir(v)           do {} while (0)
7932 +#define AuDbgInode(i)          do {} while (0)
7933 +#define AuDbgDAlias(i)         do {} while (0)
7934 +#define AuDbgDentry(d)         do {} while (0)
7935 +#define AuDbgFile(f)           do {} while (0)
7936 +#define AuDbgSb(sb)            do {} while (0)
7937 +#define AuDbgSym(addr)         do {} while (0)
7938 +#endif /* CONFIG_AUFS_DEBUG */
7939 +
7940 +/* ---------------------------------------------------------------------- */
7941 +
7942 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7943 +int __init au_sysrq_init(void);
7944 +void au_sysrq_fin(void);
7945 +
7946 +#ifdef CONFIG_HW_CONSOLE
7947 +#define au_dbg_blocked() do { \
7948 +       WARN_ON(1); \
7949 +       handle_sysrq('w'); \
7950 +} while (0)
7951 +#else
7952 +AuStubVoid(au_dbg_blocked, void)
7953 +#endif
7954 +
7955 +#else
7956 +AuStubInt0(__init au_sysrq_init, void)
7957 +AuStubVoid(au_sysrq_fin, void)
7958 +AuStubVoid(au_dbg_blocked, void)
7959 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7960 +
7961 +#endif /* __KERNEL__ */
7962 +#endif /* __AUFS_DEBUG_H__ */
7963 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7964 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7965 +++ linux/fs/aufs/dentry.c      2024-05-13 17:13:00.618070924 +0200
7966 @@ -0,0 +1,1168 @@
7967 +// SPDX-License-Identifier: GPL-2.0
7968 +/*
7969 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7970 + *
7971 + * This program is free software; you can redistribute it and/or modify
7972 + * it under the terms of the GNU General Public License as published by
7973 + * the Free Software Foundation; either version 2 of the License, or
7974 + * (at your option) any later version.
7975 + *
7976 + * This program is distributed in the hope that it will be useful,
7977 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7978 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7979 + * GNU General Public License for more details.
7980 + *
7981 + * You should have received a copy of the GNU General Public License
7982 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7983 + */
7984 +
7985 +/*
7986 + * lookup and dentry operations
7987 + */
7988 +
7989 +#include <linux/iversion.h>
7990 +#include "aufs.h"
7991 +
7992 +/*
7993 + * returns positive/negative dentry, NULL or an error.
7994 + * NULL means whiteout-ed or not-found.
7995 + */
7996 +static struct dentry*
7997 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7998 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7999 +{
8000 +       struct dentry *h_dentry;
8001 +       struct inode *h_inode;
8002 +       struct au_branch *br;
8003 +       struct mnt_idmap *h_idmap;
8004 +       struct path h_path;
8005 +       int wh_found, opq;
8006 +       unsigned char wh_able;
8007 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8008 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8009 +                                                         IGNORE_PERM);
8010 +
8011 +       wh_found = 0;
8012 +       br = au_sbr(dentry->d_sb, bindex);
8013 +       h_path.dentry = h_parent;
8014 +       h_path.mnt = au_br_mnt(br);
8015 +       h_idmap = au_br_idmap(br);
8016 +       wh_able = !!au_br_whable(br->br_perm);
8017 +       if (wh_able)
8018 +               wh_found = au_wh_test(h_idmap, &h_path, &args->whname,
8019 +                                     ignore_perm);
8020 +       h_dentry = ERR_PTR(wh_found);
8021 +       if (!wh_found)
8022 +               goto real_lookup;
8023 +       if (unlikely(wh_found < 0))
8024 +               goto out;
8025 +
8026 +       /* We found a whiteout */
8027 +       /* au_set_dbbot(dentry, bindex); */
8028 +       au_set_dbwh(dentry, bindex);
8029 +       if (!allow_neg)
8030 +               return NULL; /* success */
8031 +
8032 +real_lookup:
8033 +       if (!ignore_perm)
8034 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8035 +       else
8036 +               h_dentry = au_sio_lkup_one(h_idmap, args->name, &h_path);
8037 +       if (IS_ERR(h_dentry)) {
8038 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8039 +                   && !allow_neg)
8040 +                       h_dentry = NULL;
8041 +               goto out;
8042 +       }
8043 +
8044 +       h_inode = d_inode(h_dentry);
8045 +       if (d_is_negative(h_dentry)) {
8046 +               if (!allow_neg)
8047 +                       goto out_neg;
8048 +       } else if (wh_found
8049 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8050 +               goto out_neg;
8051 +       else if (au_ftest_lkup(args->flags, DIRREN)
8052 +                /* && h_inode */
8053 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8054 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8055 +                     (unsigned long long)h_inode->i_ino);
8056 +               goto out_neg;
8057 +       }
8058 +
8059 +       if (au_dbbot(dentry) <= bindex)
8060 +               au_set_dbbot(dentry, bindex);
8061 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8062 +               au_set_dbtop(dentry, bindex);
8063 +       au_set_h_dptr(dentry, bindex, h_dentry);
8064 +
8065 +       if (!d_is_dir(h_dentry)
8066 +           || !wh_able
8067 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8068 +               goto out; /* success */
8069 +
8070 +       h_path.dentry = h_dentry;
8071 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8072 +       opq = au_diropq_test(h_idmap, &h_path);
8073 +       inode_unlock_shared(h_inode);
8074 +       if (opq > 0)
8075 +               au_set_dbdiropq(dentry, bindex);
8076 +       else if (unlikely(opq < 0)) {
8077 +               au_set_h_dptr(dentry, bindex, NULL);
8078 +               h_dentry = ERR_PTR(opq);
8079 +       }
8080 +       goto out;
8081 +
8082 +out_neg:
8083 +       dput(h_dentry);
8084 +       h_dentry = NULL;
8085 +out:
8086 +       return h_dentry;
8087 +}
8088 +
8089 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8090 +{
8091 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8092 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8093 +               return -EPERM;
8094 +       return 0;
8095 +}
8096 +
8097 +/*
8098 + * returns the number of lower positive dentries,
8099 + * otherwise an error.
8100 + * can be called at unlinking with @type is zero.
8101 + */
8102 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8103 +                  unsigned int flags)
8104 +{
8105 +       int npositive, err;
8106 +       aufs_bindex_t bindex, btail, bdiropq;
8107 +       unsigned char isdir, dirperm1, dirren;
8108 +       struct au_do_lookup_args args = {
8109 +               .flags          = flags,
8110 +               .name           = &dentry->d_name
8111 +       };
8112 +       struct dentry *parent;
8113 +       struct super_block *sb;
8114 +
8115 +       sb = dentry->d_sb;
8116 +       err = au_test_shwh(sb, args.name);
8117 +       if (unlikely(err))
8118 +               goto out;
8119 +
8120 +       err = au_wh_name_alloc(&args.whname, args.name);
8121 +       if (unlikely(err))
8122 +               goto out;
8123 +
8124 +       isdir = !!d_is_dir(dentry);
8125 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8126 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8127 +       if (dirren)
8128 +               au_fset_lkup(args.flags, DIRREN);
8129 +
8130 +       npositive = 0;
8131 +       parent = dget_parent(dentry);
8132 +       btail = au_dbtaildir(parent);
8133 +       for (bindex = btop; bindex <= btail; bindex++) {
8134 +               struct dentry *h_parent, *h_dentry;
8135 +               struct inode *h_inode, *h_dir;
8136 +               struct au_branch *br;
8137 +
8138 +               h_dentry = au_h_dptr(dentry, bindex);
8139 +               if (h_dentry) {
8140 +                       if (d_is_positive(h_dentry))
8141 +                               npositive++;
8142 +                       break;
8143 +               }
8144 +               h_parent = au_h_dptr(parent, bindex);
8145 +               if (!h_parent || !d_is_dir(h_parent))
8146 +                       continue;
8147 +
8148 +               if (dirren) {
8149 +                       /* if the inum matches, then use the prepared name */
8150 +                       err = au_dr_lkup_name(&args, bindex);
8151 +                       if (unlikely(err))
8152 +                               goto out_parent;
8153 +               }
8154 +
8155 +               h_dir = d_inode(h_parent);
8156 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8157 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8158 +               inode_unlock_shared(h_dir);
8159 +               err = PTR_ERR(h_dentry);
8160 +               if (IS_ERR(h_dentry))
8161 +                       goto out_parent;
8162 +               if (h_dentry)
8163 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8164 +               if (dirperm1)
8165 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8166 +
8167 +               if (au_dbwh(dentry) == bindex)
8168 +                       break;
8169 +               if (!h_dentry)
8170 +                       continue;
8171 +               if (d_is_negative(h_dentry))
8172 +                       continue;
8173 +               h_inode = d_inode(h_dentry);
8174 +               npositive++;
8175 +               if (!args.type)
8176 +                       args.type = h_inode->i_mode & S_IFMT;
8177 +               if (args.type != S_IFDIR)
8178 +                       break;
8179 +               else if (isdir) {
8180 +                       /* the type of lower may be different */
8181 +                       bdiropq = au_dbdiropq(dentry);
8182 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8183 +                               break;
8184 +               }
8185 +               br = au_sbr(sb, bindex);
8186 +               if (dirren
8187 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8188 +                                          /*add_ent*/NULL)) {
8189 +                       /* prepare next name to lookup */
8190 +                       err = au_dr_lkup(&args, dentry, bindex);
8191 +                       if (unlikely(err))
8192 +                               goto out_parent;
8193 +               }
8194 +       }
8195 +
8196 +       if (npositive) {
8197 +               AuLabel(positive);
8198 +               au_update_dbtop(dentry);
8199 +       }
8200 +       err = npositive;
8201 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8202 +                    && au_dbtop(dentry) < 0)) {
8203 +               err = -EIO;
8204 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8205 +                       dentry, err);
8206 +       }
8207 +
8208 +out_parent:
8209 +       dput(parent);
8210 +       au_kfree_try_rcu(args.whname.name);
8211 +       if (dirren)
8212 +               au_dr_lkup_fin(&args);
8213 +out:
8214 +       return err;
8215 +}
8216 +
8217 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
8218 +                              struct path *ppath)
8219 +{
8220 +       struct dentry *dentry;
8221 +       int wkq_err;
8222 +
8223 +       if (!au_test_h_perm_sio(idmap, d_inode(ppath->dentry), MAY_EXEC))
8224 +               dentry = vfsub_lkup_one(name, ppath);
8225 +       else {
8226 +               struct vfsub_lkup_one_args args = {
8227 +                       .errp   = &dentry,
8228 +                       .name   = name,
8229 +                       .ppath  = ppath
8230 +               };
8231 +
8232 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8233 +               if (unlikely(wkq_err))
8234 +                       dentry = ERR_PTR(wkq_err);
8235 +       }
8236 +
8237 +       return dentry;
8238 +}
8239 +
8240 +/*
8241 + * lookup @dentry on @bindex which should be negative.
8242 + */
8243 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8244 +{
8245 +       int err;
8246 +       struct dentry *parent, *h_dentry;
8247 +       struct au_branch *br;
8248 +       struct mnt_idmap *h_idmap;
8249 +       struct path h_ppath;
8250 +
8251 +       parent = dget_parent(dentry);
8252 +       br = au_sbr(dentry->d_sb, bindex);
8253 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8254 +       h_ppath.mnt = au_br_mnt(br);
8255 +       h_idmap = au_br_idmap(br);
8256 +       if (wh)
8257 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8258 +       else
8259 +               h_dentry = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
8260 +       err = PTR_ERR(h_dentry);
8261 +       if (IS_ERR(h_dentry))
8262 +               goto out;
8263 +       if (unlikely(d_is_positive(h_dentry))) {
8264 +               err = -EIO;
8265 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8266 +               dput(h_dentry);
8267 +               goto out;
8268 +       }
8269 +
8270 +       err = 0;
8271 +       if (bindex < au_dbtop(dentry))
8272 +               au_set_dbtop(dentry, bindex);
8273 +       if (au_dbbot(dentry) < bindex)
8274 +               au_set_dbbot(dentry, bindex);
8275 +       au_set_h_dptr(dentry, bindex, h_dentry);
8276 +
8277 +out:
8278 +       dput(parent);
8279 +       return err;
8280 +}
8281 +
8282 +/* ---------------------------------------------------------------------- */
8283 +
8284 +/* subset of struct inode */
8285 +struct au_iattr {
8286 +       unsigned long           i_ino;
8287 +       /* unsigned int         i_nlink; */
8288 +       kuid_t                  i_uid;
8289 +       kgid_t                  i_gid;
8290 +       u64                     i_version;
8291 +/*
8292 +       loff_t                  i_size;
8293 +       blkcnt_t                i_blocks;
8294 +*/
8295 +       umode_t                 i_mode;
8296 +};
8297 +
8298 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8299 +{
8300 +       ia->i_ino = h_inode->i_ino;
8301 +       /* ia->i_nlink = h_inode->i_nlink; */
8302 +       ia->i_uid = h_inode->i_uid;
8303 +       ia->i_gid = h_inode->i_gid;
8304 +       ia->i_version = inode_query_iversion(h_inode);
8305 +/*
8306 +       ia->i_size = h_inode->i_size;
8307 +       ia->i_blocks = h_inode->i_blocks;
8308 +*/
8309 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8310 +}
8311 +
8312 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8313 +{
8314 +       return ia->i_ino != h_inode->i_ino
8315 +               /* || ia->i_nlink != h_inode->i_nlink */
8316 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8317 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8318 +               || !inode_eq_iversion(h_inode, ia->i_version)
8319 +/*
8320 +               || ia->i_size != h_inode->i_size
8321 +               || ia->i_blocks != h_inode->i_blocks
8322 +*/
8323 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8324 +}
8325 +
8326 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8327 +                             struct au_branch *br)
8328 +{
8329 +       int err;
8330 +       struct au_iattr ia;
8331 +       struct inode *h_inode;
8332 +       struct dentry *h_d;
8333 +       struct super_block *h_sb;
8334 +       struct path h_ppath;
8335 +
8336 +       err = 0;
8337 +       memset(&ia, -1, sizeof(ia));
8338 +       h_sb = h_dentry->d_sb;
8339 +       h_inode = NULL;
8340 +       if (d_is_positive(h_dentry)) {
8341 +               h_inode = d_inode(h_dentry);
8342 +               au_iattr_save(&ia, h_inode);
8343 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8344 +               /* nfs d_revalidate may return 0 for negative dentry */
8345 +               /* fuse d_revalidate always return 0 for negative dentry */
8346 +               goto out;
8347 +
8348 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8349 +       h_ppath.dentry = h_parent;
8350 +       h_ppath.mnt = au_br_mnt(br);
8351 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8352 +       err = PTR_ERR(h_d);
8353 +       if (IS_ERR(h_d))
8354 +               goto out;
8355 +
8356 +       err = 0;
8357 +       if (unlikely(h_d != h_dentry
8358 +                    || d_inode(h_d) != h_inode
8359 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8360 +               err = au_busy_or_stale();
8361 +       dput(h_d);
8362 +
8363 +out:
8364 +       AuTraceErr(err);
8365 +       return err;
8366 +}
8367 +
8368 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8369 +               struct dentry *h_parent, struct au_branch *br)
8370 +{
8371 +       int err;
8372 +
8373 +       err = 0;
8374 +       if (udba == AuOpt_UDBA_REVAL
8375 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8376 +               IMustLock(h_dir);
8377 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8378 +       } else if (udba != AuOpt_UDBA_NONE)
8379 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8380 +
8381 +       return err;
8382 +}
8383 +
8384 +/* ---------------------------------------------------------------------- */
8385 +
8386 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8387 +{
8388 +       int err;
8389 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8390 +       struct au_hdentry tmp, *p, *q;
8391 +       struct au_dinfo *dinfo;
8392 +       struct super_block *sb;
8393 +
8394 +       DiMustWriteLock(dentry);
8395 +
8396 +       sb = dentry->d_sb;
8397 +       dinfo = au_di(dentry);
8398 +       bbot = dinfo->di_bbot;
8399 +       bwh = dinfo->di_bwh;
8400 +       bdiropq = dinfo->di_bdiropq;
8401 +       bindex = dinfo->di_btop;
8402 +       p = au_hdentry(dinfo, bindex);
8403 +       for (; bindex <= bbot; bindex++, p++) {
8404 +               if (!p->hd_dentry)
8405 +                       continue;
8406 +
8407 +               new_bindex = au_br_index(sb, p->hd_id);
8408 +               if (new_bindex == bindex)
8409 +                       continue;
8410 +
8411 +               if (dinfo->di_bwh == bindex)
8412 +                       bwh = new_bindex;
8413 +               if (dinfo->di_bdiropq == bindex)
8414 +                       bdiropq = new_bindex;
8415 +               if (new_bindex < 0) {
8416 +                       au_hdput(p);
8417 +                       p->hd_dentry = NULL;
8418 +                       continue;
8419 +               }
8420 +
8421 +               /* swap two lower dentries, and loop again */
8422 +               q = au_hdentry(dinfo, new_bindex);
8423 +               tmp = *q;
8424 +               *q = *p;
8425 +               *p = tmp;
8426 +               if (tmp.hd_dentry) {
8427 +                       bindex--;
8428 +                       p--;
8429 +               }
8430 +       }
8431 +
8432 +       dinfo->di_bwh = -1;
8433 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8434 +               dinfo->di_bwh = bwh;
8435 +
8436 +       dinfo->di_bdiropq = -1;
8437 +       if (bdiropq >= 0
8438 +           && bdiropq <= au_sbbot(sb)
8439 +           && au_sbr_whable(sb, bdiropq))
8440 +               dinfo->di_bdiropq = bdiropq;
8441 +
8442 +       err = -EIO;
8443 +       dinfo->di_btop = -1;
8444 +       dinfo->di_bbot = -1;
8445 +       bbot = au_dbbot(parent);
8446 +       bindex = 0;
8447 +       p = au_hdentry(dinfo, bindex);
8448 +       for (; bindex <= bbot; bindex++, p++)
8449 +               if (p->hd_dentry) {
8450 +                       dinfo->di_btop = bindex;
8451 +                       break;
8452 +               }
8453 +
8454 +       if (dinfo->di_btop >= 0) {
8455 +               bindex = bbot;
8456 +               p = au_hdentry(dinfo, bindex);
8457 +               for (; bindex >= 0; bindex--, p--)
8458 +                       if (p->hd_dentry) {
8459 +                               dinfo->di_bbot = bindex;
8460 +                               err = 0;
8461 +                               break;
8462 +                       }
8463 +       }
8464 +
8465 +       return err;
8466 +}
8467 +
8468 +static void au_do_hide(struct dentry *dentry)
8469 +{
8470 +       struct inode *inode;
8471 +
8472 +       if (d_really_is_positive(dentry)) {
8473 +               inode = d_inode(dentry);
8474 +               if (!d_is_dir(dentry)) {
8475 +                       if (inode->i_nlink && !d_unhashed(dentry))
8476 +                               drop_nlink(inode);
8477 +               } else {
8478 +                       clear_nlink(inode);
8479 +                       /* stop next lookup */
8480 +                       inode->i_flags |= S_DEAD;
8481 +               }
8482 +               smp_mb(); /* necessary? */
8483 +       }
8484 +       d_drop(dentry);
8485 +}
8486 +
8487 +static int au_hide_children(struct dentry *parent)
8488 +{
8489 +       int err, i, j, ndentry;
8490 +       struct au_dcsub_pages dpages;
8491 +       struct au_dpage *dpage;
8492 +       struct dentry *dentry;
8493 +
8494 +       err = au_dpages_init(&dpages, GFP_NOFS);
8495 +       if (unlikely(err))
8496 +               goto out;
8497 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8498 +       if (unlikely(err))
8499 +               goto out_dpages;
8500 +
8501 +       /* in reverse order */
8502 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8503 +               dpage = dpages.dpages + i;
8504 +               ndentry = dpage->ndentry;
8505 +               for (j = ndentry - 1; j >= 0; j--) {
8506 +                       dentry = dpage->dentries[j];
8507 +                       if (dentry != parent)
8508 +                               au_do_hide(dentry);
8509 +               }
8510 +       }
8511 +
8512 +out_dpages:
8513 +       au_dpages_free(&dpages);
8514 +out:
8515 +       return err;
8516 +}
8517 +
8518 +static void au_hide(struct dentry *dentry)
8519 +{
8520 +       int err;
8521 +
8522 +       AuDbgDentry(dentry);
8523 +       if (d_is_dir(dentry)) {
8524 +               /* shrink_dcache_parent(dentry); */
8525 +               err = au_hide_children(dentry);
8526 +               if (unlikely(err))
8527 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8528 +                               dentry, err);
8529 +       }
8530 +       au_do_hide(dentry);
8531 +}
8532 +
8533 +/*
8534 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8535 + *
8536 + * a dirty branch is added
8537 + * - on the top of layers
8538 + * - in the middle of layers
8539 + * - to the bottom of layers
8540 + *
8541 + * on the added branch there exists
8542 + * - a whiteout
8543 + * - a diropq
8544 + * - a same named entry
8545 + *   + exist
8546 + *     * negative --> positive
8547 + *     * positive --> positive
8548 + *      - type is unchanged
8549 + *      - type is changed
8550 + *   + doesn't exist
8551 + *     * negative --> negative
8552 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8553 + * - none
8554 + */
8555 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8556 +                              struct au_dinfo *tmp)
8557 +{
8558 +       int err;
8559 +       aufs_bindex_t bindex, bbot;
8560 +       struct {
8561 +               struct dentry *dentry;
8562 +               struct inode *inode;
8563 +               mode_t mode;
8564 +       } orig_h, tmp_h = {
8565 +               .dentry = NULL
8566 +       };
8567 +       struct au_hdentry *hd;
8568 +       struct inode *inode, *h_inode;
8569 +       struct dentry *h_dentry;
8570 +
8571 +       err = 0;
8572 +       AuDebugOn(dinfo->di_btop < 0);
8573 +       orig_h.mode = 0;
8574 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8575 +       orig_h.inode = NULL;
8576 +       if (d_is_positive(orig_h.dentry)) {
8577 +               orig_h.inode = d_inode(orig_h.dentry);
8578 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8579 +       }
8580 +       if (tmp->di_btop >= 0) {
8581 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8582 +               if (d_is_positive(tmp_h.dentry)) {
8583 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8584 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8585 +               }
8586 +       }
8587 +
8588 +       inode = NULL;
8589 +       if (d_really_is_positive(dentry))
8590 +               inode = d_inode(dentry);
8591 +       if (!orig_h.inode) {
8592 +               AuDbg("negative originally\n");
8593 +               if (inode) {
8594 +                       au_hide(dentry);
8595 +                       goto out;
8596 +               }
8597 +               AuDebugOn(inode);
8598 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8599 +               AuDebugOn(dinfo->di_bdiropq != -1);
8600 +
8601 +               if (!tmp_h.inode) {
8602 +                       AuDbg("negative --> negative\n");
8603 +                       /* should have only one negative lower */
8604 +                       if (tmp->di_btop >= 0
8605 +                           && tmp->di_btop < dinfo->di_btop) {
8606 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8607 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8608 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8609 +                               au_di_cp(dinfo, tmp);
8610 +                               hd = au_hdentry(tmp, tmp->di_btop);
8611 +                               au_set_h_dptr(dentry, tmp->di_btop,
8612 +                                             dget(hd->hd_dentry));
8613 +                       }
8614 +                       au_dbg_verify_dinode(dentry);
8615 +               } else {
8616 +                       AuDbg("negative --> positive\n");
8617 +                       /*
8618 +                        * similar to the behaviour of creating with bypassing
8619 +                        * aufs.
8620 +                        * unhash it in order to force an error in the
8621 +                        * succeeding create operation.
8622 +                        * we should not set S_DEAD here.
8623 +                        */
8624 +                       d_drop(dentry);
8625 +                       /* au_di_swap(tmp, dinfo); */
8626 +                       au_dbg_verify_dinode(dentry);
8627 +               }
8628 +       } else {
8629 +               AuDbg("positive originally\n");
8630 +               /* inode may be NULL */
8631 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8632 +               if (!tmp_h.inode) {
8633 +                       AuDbg("positive --> negative\n");
8634 +                       /* or bypassing aufs */
8635 +                       au_hide(dentry);
8636 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8637 +                               dinfo->di_bwh = tmp->di_bwh;
8638 +                       if (inode)
8639 +                               err = au_refresh_hinode_self(inode);
8640 +                       au_dbg_verify_dinode(dentry);
8641 +               } else if (orig_h.mode == tmp_h.mode) {
8642 +                       AuDbg("positive --> positive, same type\n");
8643 +                       if (!S_ISDIR(orig_h.mode)
8644 +                           && dinfo->di_btop > tmp->di_btop) {
8645 +                               /*
8646 +                                * similar to the behaviour of removing and
8647 +                                * creating.
8648 +                                */
8649 +                               au_hide(dentry);
8650 +                               if (inode)
8651 +                                       err = au_refresh_hinode_self(inode);
8652 +                               au_dbg_verify_dinode(dentry);
8653 +                       } else {
8654 +                               /* fill empty slots */
8655 +                               if (dinfo->di_btop > tmp->di_btop)
8656 +                                       dinfo->di_btop = tmp->di_btop;
8657 +                               if (dinfo->di_bbot < tmp->di_bbot)
8658 +                                       dinfo->di_bbot = tmp->di_bbot;
8659 +                               dinfo->di_bwh = tmp->di_bwh;
8660 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8661 +                               bbot = dinfo->di_bbot;
8662 +                               bindex = tmp->di_btop;
8663 +                               hd = au_hdentry(tmp, bindex);
8664 +                               for (; bindex <= bbot; bindex++, hd++) {
8665 +                                       if (au_h_dptr(dentry, bindex))
8666 +                                               continue;
8667 +                                       h_dentry = hd->hd_dentry;
8668 +                                       if (!h_dentry)
8669 +                                               continue;
8670 +                                       AuDebugOn(d_is_negative(h_dentry));
8671 +                                       h_inode = d_inode(h_dentry);
8672 +                                       AuDebugOn(orig_h.mode
8673 +                                                 != (h_inode->i_mode
8674 +                                                     & S_IFMT));
8675 +                                       au_set_h_dptr(dentry, bindex,
8676 +                                                     dget(h_dentry));
8677 +                               }
8678 +                               if (inode)
8679 +                                       err = au_refresh_hinode(inode, dentry);
8680 +                               au_dbg_verify_dinode(dentry);
8681 +                       }
8682 +               } else {
8683 +                       AuDbg("positive --> positive, different type\n");
8684 +                       /* similar to the behaviour of removing and creating */
8685 +                       au_hide(dentry);
8686 +                       if (inode)
8687 +                               err = au_refresh_hinode_self(inode);
8688 +                       au_dbg_verify_dinode(dentry);
8689 +               }
8690 +       }
8691 +
8692 +out:
8693 +       return err;
8694 +}
8695 +
8696 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8697 +{
8698 +       const struct dentry_operations *dop
8699 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8700 +       static const unsigned int mask
8701 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8702 +
8703 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8704 +
8705 +       if (dentry->d_op == dop)
8706 +               return;
8707 +
8708 +       AuDbg("%pd\n", dentry);
8709 +       spin_lock(&dentry->d_lock);
8710 +       if (dop == &aufs_dop)
8711 +               dentry->d_flags |= mask;
8712 +       else
8713 +               dentry->d_flags &= ~mask;
8714 +       dentry->d_op = dop;
8715 +       spin_unlock(&dentry->d_lock);
8716 +}
8717 +
8718 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8719 +{
8720 +       int err, ebrange, nbr;
8721 +       unsigned int sigen;
8722 +       struct au_dinfo *dinfo, *tmp;
8723 +       struct super_block *sb;
8724 +       struct inode *inode;
8725 +
8726 +       DiMustWriteLock(dentry);
8727 +       AuDebugOn(IS_ROOT(dentry));
8728 +       AuDebugOn(d_really_is_negative(parent));
8729 +
8730 +       sb = dentry->d_sb;
8731 +       sigen = au_sigen(sb);
8732 +       err = au_digen_test(parent, sigen);
8733 +       if (unlikely(err))
8734 +               goto out;
8735 +
8736 +       nbr = au_sbbot(sb) + 1;
8737 +       dinfo = au_di(dentry);
8738 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8739 +       if (unlikely(err))
8740 +               goto out;
8741 +       ebrange = au_dbrange_test(dentry);
8742 +       if (!ebrange)
8743 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8744 +
8745 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8746 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8747 +               if (d_really_is_positive(dentry)) {
8748 +                       inode = d_inode(dentry);
8749 +                       err = au_refresh_hinode_self(inode);
8750 +               }
8751 +               au_dbg_verify_dinode(dentry);
8752 +               if (!err)
8753 +                       goto out_dgen; /* success */
8754 +               goto out;
8755 +       }
8756 +
8757 +       /* temporary dinfo */
8758 +       AuDbgDentry(dentry);
8759 +       err = -ENOMEM;
8760 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8761 +       if (unlikely(!tmp))
8762 +               goto out;
8763 +       au_di_swap(tmp, dinfo);
8764 +       /* returns the number of positive dentries */
8765 +       /*
8766 +        * if current working dir is removed, it returns an error.
8767 +        * but the dentry is legal.
8768 +        */
8769 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8770 +       AuDbgDentry(dentry);
8771 +       au_di_swap(tmp, dinfo);
8772 +       if (err == -ENOENT)
8773 +               err = 0;
8774 +       if (err >= 0) {
8775 +               /* compare/refresh by dinfo */
8776 +               AuDbgDentry(dentry);
8777 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8778 +               au_dbg_verify_dinode(dentry);
8779 +               AuTraceErr(err);
8780 +       }
8781 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8782 +       au_rw_write_unlock(&tmp->di_rwsem);
8783 +       au_di_free(tmp);
8784 +       if (unlikely(err))
8785 +               goto out;
8786 +
8787 +out_dgen:
8788 +       au_update_digen(dentry);
8789 +out:
8790 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8791 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8792 +               AuDbgDentry(dentry);
8793 +       }
8794 +       AuTraceErr(err);
8795 +       return err;
8796 +}
8797 +
8798 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8799 +                          struct dentry *dentry, aufs_bindex_t bindex)
8800 +{
8801 +       int err, valid;
8802 +
8803 +       err = 0;
8804 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8805 +               goto out;
8806 +
8807 +       AuDbg("b%d\n", bindex);
8808 +       /*
8809 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8810 +        * due to whiteout and branch permission.
8811 +        */
8812 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8813 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8814 +       /* it may return tri-state */
8815 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8816 +
8817 +       if (unlikely(valid < 0))
8818 +               err = valid;
8819 +       else if (!valid)
8820 +               err = -EINVAL;
8821 +
8822 +out:
8823 +       AuTraceErr(err);
8824 +       return err;
8825 +}
8826 +
8827 +/* todo: remove this */
8828 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8829 +                         unsigned int flags, int do_udba, int dirren)
8830 +{
8831 +       int err;
8832 +       umode_t mode, h_mode;
8833 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8834 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8835 +       struct inode *h_inode, *h_cached_inode;
8836 +       struct dentry *h_dentry;
8837 +       struct qstr *name, *h_name;
8838 +
8839 +       err = 0;
8840 +       plus = 0;
8841 +       mode = 0;
8842 +       ibs = -1;
8843 +       ibe = -1;
8844 +       unhashed = !!d_unhashed(dentry);
8845 +       is_root = !!IS_ROOT(dentry);
8846 +       name = &dentry->d_name;
8847 +       tmpfile = au_di(dentry)->di_tmpfile;
8848 +
8849 +       /*
8850 +        * Theoretically, REVAL test should be unnecessary in case of
8851 +        * {FS,I}NOTIFY.
8852 +        * But {fs,i}notify doesn't fire some necessary events,
8853 +        *      IN_ATTRIB for atime/nlink/pageio
8854 +        * Let's do REVAL test too.
8855 +        */
8856 +       if (do_udba && inode) {
8857 +               mode = (inode->i_mode & S_IFMT);
8858 +               plus = (inode->i_nlink > 0);
8859 +               ibs = au_ibtop(inode);
8860 +               ibe = au_ibbot(inode);
8861 +       }
8862 +
8863 +       btop = au_dbtop(dentry);
8864 +       btail = btop;
8865 +       if (inode && S_ISDIR(inode->i_mode))
8866 +               btail = au_dbtaildir(dentry);
8867 +       for (bindex = btop; bindex <= btail; bindex++) {
8868 +               h_dentry = au_h_dptr(dentry, bindex);
8869 +               if (!h_dentry)
8870 +                       continue;
8871 +
8872 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8873 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8874 +               spin_lock(&h_dentry->d_lock);
8875 +               h_name = &h_dentry->d_name;
8876 +               if (unlikely(do_udba
8877 +                            && !is_root
8878 +                            && ((!h_nfs
8879 +                                 && (unhashed != !!d_unhashed(h_dentry)
8880 +                                     || (!tmpfile && !dirren
8881 +                                         && !au_qstreq(name, h_name))
8882 +                                         ))
8883 +                                || (h_nfs
8884 +                                    && !(flags & LOOKUP_OPEN)
8885 +                                    && (h_dentry->d_flags
8886 +                                        & DCACHE_NFSFS_RENAMED)))
8887 +                           )) {
8888 +                       int h_unhashed;
8889 +
8890 +                       h_unhashed = d_unhashed(h_dentry);
8891 +                       spin_unlock(&h_dentry->d_lock);
8892 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8893 +                             unhashed, h_unhashed, dentry, h_dentry);
8894 +                       goto err;
8895 +               }
8896 +               spin_unlock(&h_dentry->d_lock);
8897 +
8898 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8899 +               if (unlikely(err))
8900 +                       /* do not goto err, to keep the errno */
8901 +                       break;
8902 +
8903 +               /* todo: plink too? */
8904 +               if (!do_udba)
8905 +                       continue;
8906 +
8907 +               /* UDBA tests */
8908 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8909 +                       goto err;
8910 +
8911 +               h_inode = NULL;
8912 +               if (d_is_positive(h_dentry))
8913 +                       h_inode = d_inode(h_dentry);
8914 +               h_plus = plus;
8915 +               h_mode = mode;
8916 +               h_cached_inode = h_inode;
8917 +               if (h_inode) {
8918 +                       h_mode = (h_inode->i_mode & S_IFMT);
8919 +                       h_plus = (h_inode->i_nlink > 0);
8920 +               }
8921 +               if (inode && ibs <= bindex && bindex <= ibe)
8922 +                       h_cached_inode = au_h_iptr(inode, bindex);
8923 +
8924 +               if (!h_nfs) {
8925 +                       if (unlikely(plus != h_plus && !tmpfile))
8926 +                               goto err;
8927 +               } else {
8928 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8929 +                                    && !is_root
8930 +                                    && !IS_ROOT(h_dentry)
8931 +                                    && unhashed != d_unhashed(h_dentry)))
8932 +                               goto err;
8933 +               }
8934 +               if (unlikely(mode != h_mode
8935 +                            || h_cached_inode != h_inode))
8936 +                       goto err;
8937 +               continue;
8938 +
8939 +err:
8940 +               err = -EINVAL;
8941 +               break;
8942 +       }
8943 +
8944 +       AuTraceErr(err);
8945 +       return err;
8946 +}
8947 +
8948 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8949 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8950 +{
8951 +       int err;
8952 +       struct dentry *parent;
8953 +
8954 +       if (!au_digen_test(dentry, sigen))
8955 +               return 0;
8956 +
8957 +       parent = dget_parent(dentry);
8958 +       di_read_lock_parent(parent, AuLock_IR);
8959 +       AuDebugOn(au_digen_test(parent, sigen));
8960 +       au_dbg_verify_gen(parent, sigen);
8961 +       err = au_refresh_dentry(dentry, parent);
8962 +       di_read_unlock(parent, AuLock_IR);
8963 +       dput(parent);
8964 +       AuTraceErr(err);
8965 +       return err;
8966 +}
8967 +
8968 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8969 +{
8970 +       int err;
8971 +       struct dentry *d, *parent;
8972 +
8973 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8974 +               return simple_reval_dpath(dentry, sigen);
8975 +
8976 +       /* slow loop, keep it simple and stupid */
8977 +       /* cf: au_cpup_dirs() */
8978 +       err = 0;
8979 +       parent = NULL;
8980 +       while (au_digen_test(dentry, sigen)) {
8981 +               d = dentry;
8982 +               while (1) {
8983 +                       dput(parent);
8984 +                       parent = dget_parent(d);
8985 +                       if (!au_digen_test(parent, sigen))
8986 +                               break;
8987 +                       d = parent;
8988 +               }
8989 +
8990 +               if (d != dentry)
8991 +                       di_write_lock_child2(d);
8992 +
8993 +               /* someone might update our dentry while we were sleeping */
8994 +               if (au_digen_test(d, sigen)) {
8995 +                       /*
8996 +                        * todo: consolidate with simple_reval_dpath(),
8997 +                        * do_refresh() and au_reval_for_attr().
8998 +                        */
8999 +                       di_read_lock_parent(parent, AuLock_IR);
9000 +                       err = au_refresh_dentry(d, parent);
9001 +                       di_read_unlock(parent, AuLock_IR);
9002 +               }
9003 +
9004 +               if (d != dentry)
9005 +                       di_write_unlock(d);
9006 +               dput(parent);
9007 +               if (unlikely(err))
9008 +                       break;
9009 +       }
9010 +
9011 +       return err;
9012 +}
9013 +
9014 +/*
9015 + * if valid returns 1, otherwise 0.
9016 + */
9017 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9018 +{
9019 +       int valid, err;
9020 +       unsigned int sigen;
9021 +       unsigned char do_udba, dirren;
9022 +       struct super_block *sb;
9023 +       struct inode *inode;
9024 +
9025 +       /* todo: support rcu-walk? */
9026 +       if (flags & LOOKUP_RCU)
9027 +               return -ECHILD;
9028 +
9029 +       valid = 0;
9030 +       if (unlikely(!au_di(dentry)))
9031 +               goto out;
9032 +
9033 +       valid = 1;
9034 +       sb = dentry->d_sb;
9035 +       /*
9036 +        * todo: very ugly
9037 +        * i_mutex of parent dir may be held,
9038 +        * but we should not return 'invalid' due to busy.
9039 +        */
9040 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9041 +       if (unlikely(err)) {
9042 +               valid = err;
9043 +               AuTraceErr(err);
9044 +               goto out;
9045 +       }
9046 +       inode = NULL;
9047 +       if (d_really_is_positive(dentry))
9048 +               inode = d_inode(dentry);
9049 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9050 +               err = -EINVAL;
9051 +               AuTraceErr(err);
9052 +               goto out_dgrade;
9053 +       }
9054 +       if (unlikely(au_dbrange_test(dentry))) {
9055 +               err = -EINVAL;
9056 +               AuTraceErr(err);
9057 +               goto out_dgrade;
9058 +       }
9059 +
9060 +       sigen = au_sigen(sb);
9061 +       if (au_digen_test(dentry, sigen)) {
9062 +               AuDebugOn(IS_ROOT(dentry));
9063 +               err = au_reval_dpath(dentry, sigen);
9064 +               if (unlikely(err)) {
9065 +                       AuTraceErr(err);
9066 +                       goto out_dgrade;
9067 +               }
9068 +       }
9069 +       di_downgrade_lock(dentry, AuLock_IR);
9070 +
9071 +       err = -EINVAL;
9072 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9073 +           && inode
9074 +           && !(inode->i_state && I_LINKABLE)
9075 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9076 +               AuTraceErr(err);
9077 +               goto out_inval;
9078 +       }
9079 +
9080 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9081 +       if (do_udba && inode) {
9082 +               aufs_bindex_t btop = au_ibtop(inode);
9083 +               struct inode *h_inode;
9084 +
9085 +               if (btop >= 0) {
9086 +                       h_inode = au_h_iptr(inode, btop);
9087 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9088 +                               AuTraceErr(err);
9089 +                               goto out_inval;
9090 +                       }
9091 +               }
9092 +       }
9093 +
9094 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9095 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9096 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9097 +               err = -EIO;
9098 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9099 +                     dentry, err);
9100 +       }
9101 +       goto out_inval;
9102 +
9103 +out_dgrade:
9104 +       di_downgrade_lock(dentry, AuLock_IR);
9105 +out_inval:
9106 +       aufs_read_unlock(dentry, AuLock_IR);
9107 +       AuTraceErr(err);
9108 +       valid = !err;
9109 +out:
9110 +       if (!valid) {
9111 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9112 +               d_drop(dentry);
9113 +       }
9114 +       return valid;
9115 +}
9116 +
9117 +static void aufs_d_release(struct dentry *dentry)
9118 +{
9119 +       if (au_di(dentry)) {
9120 +               au_di_fin(dentry);
9121 +               au_hn_di_reinit(dentry);
9122 +       }
9123 +}
9124 +
9125 +const struct dentry_operations aufs_dop = {
9126 +       .d_revalidate           = aufs_d_revalidate,
9127 +       .d_weak_revalidate      = aufs_d_revalidate,
9128 +       .d_release              = aufs_d_release
9129 +};
9130 +
9131 +/* aufs_dop without d_revalidate */
9132 +const struct dentry_operations aufs_dop_noreval = {
9133 +       .d_release              = aufs_d_release
9134 +};
9135 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9136 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9137 +++ linux/fs/aufs/dentry.h      2024-05-13 17:13:00.618070924 +0200
9138 @@ -0,0 +1,270 @@
9139 +/* SPDX-License-Identifier: GPL-2.0 */
9140 +/*
9141 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9142 + *
9143 + * This program is free software; you can redistribute it and/or modify
9144 + * it under the terms of the GNU General Public License as published by
9145 + * the Free Software Foundation; either version 2 of the License, or
9146 + * (at your option) any later version.
9147 + *
9148 + * This program is distributed in the hope that it will be useful,
9149 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9150 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9151 + * GNU General Public License for more details.
9152 + *
9153 + * You should have received a copy of the GNU General Public License
9154 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9155 + */
9156 +
9157 +/*
9158 + * lookup and dentry operations
9159 + */
9160 +
9161 +#ifndef __AUFS_DENTRY_H__
9162 +#define __AUFS_DENTRY_H__
9163 +
9164 +#ifdef __KERNEL__
9165 +
9166 +#include <linux/dcache.h>
9167 +#include "dirren.h"
9168 +#include "rwsem.h"
9169 +
9170 +struct au_hdentry {
9171 +       struct dentry           *hd_dentry;
9172 +       aufs_bindex_t           hd_id;
9173 +};
9174 +
9175 +struct au_dinfo {
9176 +       atomic_t                di_generation;
9177 +
9178 +       struct au_rwsem         di_rwsem;
9179 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9180 +       unsigned char           di_tmpfile; /* to allow the different name */
9181 +       struct au_hdentry       *di_hdentry;
9182 +       struct file             *di_htmpfile;
9183 +       struct rcu_head         rcu;
9184 +} ____cacheline_aligned_in_smp;
9185 +
9186 +/* ---------------------------------------------------------------------- */
9187 +
9188 +/* flags for au_lkup_dentry() */
9189 +#define AuLkup_ALLOW_NEG       BIT(0)
9190 +#define AuLkup_IGNORE_PERM     BIT(1)
9191 +#define AuLkup_DIRREN          BIT(2)
9192 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9193 +#define au_fset_lkup(flags, name) \
9194 +       do { (flags) |= AuLkup_##name; } while (0)
9195 +#define au_fclr_lkup(flags, name) \
9196 +       do { (flags) &= ~AuLkup_##name; } while (0)
9197 +
9198 +#ifndef CONFIG_AUFS_DIRREN
9199 +#undef AuLkup_DIRREN
9200 +#define AuLkup_DIRREN 0
9201 +#endif
9202 +
9203 +struct au_do_lookup_args {
9204 +       unsigned int            flags;
9205 +       mode_t                  type;
9206 +       struct qstr             whname, *name;
9207 +       struct au_dr_lookup     dirren;
9208 +};
9209 +
9210 +/* ---------------------------------------------------------------------- */
9211 +
9212 +/* dentry.c */
9213 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9214 +struct au_branch;
9215 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
9216 +                              struct path *ppath);
9217 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9218 +               struct dentry *h_parent, struct au_branch *br);
9219 +
9220 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9221 +                  unsigned int flags);
9222 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9223 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9224 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9225 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9226 +
9227 +/* dinfo.c */
9228 +void au_di_init_once(void *_di);
9229 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9230 +void au_di_free(struct au_dinfo *dinfo);
9231 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9232 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9233 +int au_di_init(struct dentry *dentry);
9234 +void au_di_fin(struct dentry *dentry);
9235 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9236 +
9237 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9238 +void di_read_unlock(struct dentry *d, int flags);
9239 +void di_downgrade_lock(struct dentry *d, int flags);
9240 +void di_write_lock(struct dentry *d, unsigned int lsc);
9241 +void di_write_unlock(struct dentry *d);
9242 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9243 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9244 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9245 +
9246 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9247 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9248 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9249 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9250 +
9251 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9252 +                  struct dentry *h_dentry);
9253 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9254 +int au_dbrange_test(struct dentry *dentry);
9255 +void au_update_digen(struct dentry *dentry);
9256 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9257 +void au_update_dbtop(struct dentry *dentry);
9258 +void au_update_dbbot(struct dentry *dentry);
9259 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9260 +
9261 +/* ---------------------------------------------------------------------- */
9262 +
9263 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9264 +{
9265 +       return dentry->d_fsdata;
9266 +}
9267 +
9268 +/* ---------------------------------------------------------------------- */
9269 +
9270 +/* lock subclass for dinfo */
9271 +enum {
9272 +       AuLsc_DI_CHILD,         /* child first */
9273 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9274 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9275 +       AuLsc_DI_PARENT,
9276 +       AuLsc_DI_PARENT2,
9277 +       AuLsc_DI_PARENT3,
9278 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9279 +};
9280 +
9281 +/*
9282 + * di_read_lock_child, di_write_lock_child,
9283 + * di_read_lock_child2, di_write_lock_child2,
9284 + * di_read_lock_child3, di_write_lock_child3,
9285 + * di_read_lock_parent, di_write_lock_parent,
9286 + * di_read_lock_parent2, di_write_lock_parent2,
9287 + * di_read_lock_parent3, di_write_lock_parent3,
9288 + */
9289 +#define AuReadLockFunc(name, lsc) \
9290 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9291 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9292 +
9293 +#define AuWriteLockFunc(name, lsc) \
9294 +static inline void di_write_lock_##name(struct dentry *d) \
9295 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9296 +
9297 +#define AuRWLockFuncs(name, lsc) \
9298 +       AuReadLockFunc(name, lsc) \
9299 +       AuWriteLockFunc(name, lsc)
9300 +
9301 +AuRWLockFuncs(child, CHILD);
9302 +AuRWLockFuncs(child2, CHILD2);
9303 +AuRWLockFuncs(child3, CHILD3);
9304 +AuRWLockFuncs(parent, PARENT);
9305 +AuRWLockFuncs(parent2, PARENT2);
9306 +AuRWLockFuncs(parent3, PARENT3);
9307 +
9308 +#undef AuReadLockFunc
9309 +#undef AuWriteLockFunc
9310 +#undef AuRWLockFuncs
9311 +
9312 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9313 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9314 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9315 +
9316 +/* ---------------------------------------------------------------------- */
9317 +
9318 +/* todo: memory barrier? */
9319 +static inline unsigned int au_digen(struct dentry *d)
9320 +{
9321 +       return atomic_read(&au_di(d)->di_generation);
9322 +}
9323 +
9324 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9325 +{
9326 +       hdentry->hd_dentry = NULL;
9327 +}
9328 +
9329 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9330 +                                           aufs_bindex_t bindex)
9331 +{
9332 +       return di->di_hdentry + bindex;
9333 +}
9334 +
9335 +static inline void au_hdput(struct au_hdentry *hd)
9336 +{
9337 +       if (hd)
9338 +               dput(hd->hd_dentry);
9339 +}
9340 +
9341 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9342 +{
9343 +       DiMustAnyLock(dentry);
9344 +       return au_di(dentry)->di_btop;
9345 +}
9346 +
9347 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9348 +{
9349 +       DiMustAnyLock(dentry);
9350 +       return au_di(dentry)->di_bbot;
9351 +}
9352 +
9353 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9354 +{
9355 +       DiMustAnyLock(dentry);
9356 +       return au_di(dentry)->di_bwh;
9357 +}
9358 +
9359 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9360 +{
9361 +       DiMustAnyLock(dentry);
9362 +       return au_di(dentry)->di_bdiropq;
9363 +}
9364 +
9365 +/* todo: hard/soft set? */
9366 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9367 +{
9368 +       DiMustWriteLock(dentry);
9369 +       au_di(dentry)->di_btop = bindex;
9370 +}
9371 +
9372 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9373 +{
9374 +       DiMustWriteLock(dentry);
9375 +       au_di(dentry)->di_bbot = bindex;
9376 +}
9377 +
9378 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9379 +{
9380 +       DiMustWriteLock(dentry);
9381 +       /* dbwh can be outside of btop - bbot range */
9382 +       au_di(dentry)->di_bwh = bindex;
9383 +}
9384 +
9385 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9386 +{
9387 +       DiMustWriteLock(dentry);
9388 +       au_di(dentry)->di_bdiropq = bindex;
9389 +}
9390 +
9391 +/* ---------------------------------------------------------------------- */
9392 +
9393 +#ifdef CONFIG_AUFS_HNOTIFY
9394 +static inline void au_digen_dec(struct dentry *d)
9395 +{
9396 +       atomic_dec(&au_di(d)->di_generation);
9397 +}
9398 +
9399 +static inline void au_hn_di_reinit(struct dentry *dentry)
9400 +{
9401 +       dentry->d_fsdata = NULL;
9402 +}
9403 +#else
9404 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9405 +#endif /* CONFIG_AUFS_HNOTIFY */
9406 +
9407 +#endif /* __KERNEL__ */
9408 +#endif /* __AUFS_DENTRY_H__ */
9409 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9410 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9411 +++ linux/fs/aufs/dinfo.c       2024-05-13 17:13:00.618070924 +0200
9412 @@ -0,0 +1,555 @@
9413 +// SPDX-License-Identifier: GPL-2.0
9414 +/*
9415 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9416 + *
9417 + * This program is free software; you can redistribute it and/or modify
9418 + * it under the terms of the GNU General Public License as published by
9419 + * the Free Software Foundation; either version 2 of the License, or
9420 + * (at your option) any later version.
9421 + *
9422 + * This program is distributed in the hope that it will be useful,
9423 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9424 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9425 + * GNU General Public License for more details.
9426 + *
9427 + * You should have received a copy of the GNU General Public License
9428 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9429 + */
9430 +
9431 +/*
9432 + * dentry private data
9433 + */
9434 +
9435 +#include "aufs.h"
9436 +
9437 +void au_di_init_once(void *_dinfo)
9438 +{
9439 +       struct au_dinfo *dinfo = _dinfo;
9440 +
9441 +       au_rw_init(&dinfo->di_rwsem);
9442 +}
9443 +
9444 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9445 +{
9446 +       struct au_dinfo *dinfo;
9447 +       int nbr, i;
9448 +
9449 +       dinfo = au_cache_alloc_dinfo();
9450 +       if (unlikely(!dinfo))
9451 +               goto out;
9452 +
9453 +       nbr = au_sbbot(sb) + 1;
9454 +       if (nbr <= 0)
9455 +               nbr = 1;
9456 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9457 +       if (dinfo->di_hdentry) {
9458 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9459 +               dinfo->di_btop = -1;
9460 +               dinfo->di_bbot = -1;
9461 +               dinfo->di_bwh = -1;
9462 +               dinfo->di_bdiropq = -1;
9463 +               dinfo->di_tmpfile = 0;
9464 +               for (i = 0; i < nbr; i++)
9465 +                       dinfo->di_hdentry[i].hd_id = -1;
9466 +               dinfo->di_htmpfile = NULL;
9467 +               goto out;
9468 +       }
9469 +
9470 +       au_cache_free_dinfo(dinfo);
9471 +       dinfo = NULL;
9472 +
9473 +out:
9474 +       return dinfo;
9475 +}
9476 +
9477 +void au_di_free(struct au_dinfo *dinfo)
9478 +{
9479 +       struct au_hdentry *p;
9480 +       aufs_bindex_t bbot, bindex;
9481 +
9482 +       /* dentry may not be revalidated */
9483 +       bindex = dinfo->di_btop;
9484 +       if (bindex >= 0) {
9485 +               bbot = dinfo->di_bbot;
9486 +               p = au_hdentry(dinfo, bindex);
9487 +               while (bindex++ <= bbot)
9488 +                       au_hdput(p++);
9489 +       }
9490 +       au_kfree_try_rcu(dinfo->di_hdentry);
9491 +       au_cache_free_dinfo(dinfo);
9492 +}
9493 +
9494 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9495 +{
9496 +       struct au_hdentry *p;
9497 +       aufs_bindex_t bi;
9498 +
9499 +       AuRwMustWriteLock(&a->di_rwsem);
9500 +       AuRwMustWriteLock(&b->di_rwsem);
9501 +
9502 +#define DiSwap(v, name)                                \
9503 +       do {                                    \
9504 +               v = a->di_##name;               \
9505 +               a->di_##name = b->di_##name;    \
9506 +               b->di_##name = v;               \
9507 +       } while (0)
9508 +
9509 +       DiSwap(p, hdentry);
9510 +       DiSwap(bi, btop);
9511 +       DiSwap(bi, bbot);
9512 +       DiSwap(bi, bwh);
9513 +       DiSwap(bi, bdiropq);
9514 +       /* smp_mb(); */
9515 +
9516 +#undef DiSwap
9517 +}
9518 +
9519 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9520 +{
9521 +       AuRwMustWriteLock(&dst->di_rwsem);
9522 +       AuRwMustWriteLock(&src->di_rwsem);
9523 +
9524 +       dst->di_btop = src->di_btop;
9525 +       dst->di_bbot = src->di_bbot;
9526 +       dst->di_bwh = src->di_bwh;
9527 +       dst->di_bdiropq = src->di_bdiropq;
9528 +       /* smp_mb(); */
9529 +}
9530 +
9531 +int au_di_init(struct dentry *dentry)
9532 +{
9533 +       int err;
9534 +       struct super_block *sb;
9535 +       struct au_dinfo *dinfo;
9536 +
9537 +       err = 0;
9538 +       sb = dentry->d_sb;
9539 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9540 +       if (dinfo) {
9541 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9542 +               /* smp_mb(); */ /* atomic_set */
9543 +               dentry->d_fsdata = dinfo;
9544 +       } else
9545 +               err = -ENOMEM;
9546 +
9547 +       return err;
9548 +}
9549 +
9550 +void au_di_fin(struct dentry *dentry)
9551 +{
9552 +       struct au_dinfo *dinfo;
9553 +
9554 +       dinfo = au_di(dentry);
9555 +       AuRwDestroy(&dinfo->di_rwsem);
9556 +       au_di_free(dinfo);
9557 +}
9558 +
9559 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9560 +{
9561 +       int err, sz;
9562 +       struct au_hdentry *hdp;
9563 +
9564 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9565 +
9566 +       err = -ENOMEM;
9567 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9568 +       if (!sz)
9569 +               sz = sizeof(*hdp);
9570 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9571 +                          may_shrink);
9572 +       if (hdp) {
9573 +               dinfo->di_hdentry = hdp;
9574 +               err = 0;
9575 +       }
9576 +
9577 +       return err;
9578 +}
9579 +
9580 +/* ---------------------------------------------------------------------- */
9581 +
9582 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9583 +{
9584 +       switch (lsc) {
9585 +       case AuLsc_DI_CHILD:
9586 +               ii_write_lock_child(inode);
9587 +               break;
9588 +       case AuLsc_DI_CHILD2:
9589 +               ii_write_lock_child2(inode);
9590 +               break;
9591 +       case AuLsc_DI_CHILD3:
9592 +               ii_write_lock_child3(inode);
9593 +               break;
9594 +       case AuLsc_DI_PARENT:
9595 +               ii_write_lock_parent(inode);
9596 +               break;
9597 +       case AuLsc_DI_PARENT2:
9598 +               ii_write_lock_parent2(inode);
9599 +               break;
9600 +       case AuLsc_DI_PARENT3:
9601 +               ii_write_lock_parent3(inode);
9602 +               break;
9603 +       default:
9604 +               BUG();
9605 +       }
9606 +}
9607 +
9608 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9609 +{
9610 +       switch (lsc) {
9611 +       case AuLsc_DI_CHILD:
9612 +               ii_read_lock_child(inode);
9613 +               break;
9614 +       case AuLsc_DI_CHILD2:
9615 +               ii_read_lock_child2(inode);
9616 +               break;
9617 +       case AuLsc_DI_CHILD3:
9618 +               ii_read_lock_child3(inode);
9619 +               break;
9620 +       case AuLsc_DI_PARENT:
9621 +               ii_read_lock_parent(inode);
9622 +               break;
9623 +       case AuLsc_DI_PARENT2:
9624 +               ii_read_lock_parent2(inode);
9625 +               break;
9626 +       case AuLsc_DI_PARENT3:
9627 +               ii_read_lock_parent3(inode);
9628 +               break;
9629 +       default:
9630 +               BUG();
9631 +       }
9632 +}
9633 +
9634 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9635 +{
9636 +       struct inode *inode;
9637 +
9638 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9639 +       if (d_really_is_positive(d)) {
9640 +               inode = d_inode(d);
9641 +               if (au_ftest_lock(flags, IW))
9642 +                       do_ii_write_lock(inode, lsc);
9643 +               else if (au_ftest_lock(flags, IR))
9644 +                       do_ii_read_lock(inode, lsc);
9645 +       }
9646 +}
9647 +
9648 +void di_read_unlock(struct dentry *d, int flags)
9649 +{
9650 +       struct inode *inode;
9651 +
9652 +       if (d_really_is_positive(d)) {
9653 +               inode = d_inode(d);
9654 +               if (au_ftest_lock(flags, IW)) {
9655 +                       au_dbg_verify_dinode(d);
9656 +                       ii_write_unlock(inode);
9657 +               } else if (au_ftest_lock(flags, IR)) {
9658 +                       au_dbg_verify_dinode(d);
9659 +                       ii_read_unlock(inode);
9660 +               }
9661 +       }
9662 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9663 +}
9664 +
9665 +void di_downgrade_lock(struct dentry *d, int flags)
9666 +{
9667 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9668 +               ii_downgrade_lock(d_inode(d));
9669 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9670 +}
9671 +
9672 +void di_write_lock(struct dentry *d, unsigned int lsc)
9673 +{
9674 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9675 +       if (d_really_is_positive(d))
9676 +               do_ii_write_lock(d_inode(d), lsc);
9677 +}
9678 +
9679 +void di_write_unlock(struct dentry *d)
9680 +{
9681 +       au_dbg_verify_dinode(d);
9682 +       if (d_really_is_positive(d))
9683 +               ii_write_unlock(d_inode(d));
9684 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9685 +}
9686 +
9687 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9688 +{
9689 +       AuDebugOn(d1 == d2
9690 +                 || d_inode(d1) == d_inode(d2)
9691 +                 || d1->d_sb != d2->d_sb);
9692 +
9693 +       if ((isdir && au_test_subdir(d1, d2))
9694 +           || d1 < d2) {
9695 +               di_write_lock_child(d1);
9696 +               di_write_lock_child2(d2);
9697 +       } else {
9698 +               di_write_lock_child(d2);
9699 +               di_write_lock_child2(d1);
9700 +       }
9701 +}
9702 +
9703 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9704 +{
9705 +       AuDebugOn(d1 == d2
9706 +                 || d_inode(d1) == d_inode(d2)
9707 +                 || d1->d_sb != d2->d_sb);
9708 +
9709 +       if ((isdir && au_test_subdir(d1, d2))
9710 +           || d1 < d2) {
9711 +               di_write_lock_parent(d1);
9712 +               di_write_lock_parent2(d2);
9713 +       } else {
9714 +               di_write_lock_parent(d2);
9715 +               di_write_lock_parent2(d1);
9716 +       }
9717 +}
9718 +
9719 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9720 +{
9721 +       di_write_unlock(d1);
9722 +       if (d_inode(d1) == d_inode(d2))
9723 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9724 +       else
9725 +               di_write_unlock(d2);
9726 +}
9727 +
9728 +/* ---------------------------------------------------------------------- */
9729 +
9730 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9731 +{
9732 +       struct dentry *d;
9733 +
9734 +       DiMustAnyLock(dentry);
9735 +
9736 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9737 +               return NULL;
9738 +       AuDebugOn(bindex < 0);
9739 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9740 +       AuDebugOn(d && au_dcount(d) <= 0);
9741 +       return d;
9742 +}
9743 +
9744 +/*
9745 + * extended version of au_h_dptr().
9746 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9747 + * error.
9748 + */
9749 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9750 +{
9751 +       struct dentry *h_dentry;
9752 +       struct inode *inode, *h_inode;
9753 +
9754 +       AuDebugOn(d_really_is_negative(dentry));
9755 +
9756 +       h_dentry = NULL;
9757 +       if (au_dbtop(dentry) <= bindex
9758 +           && bindex <= au_dbbot(dentry))
9759 +               h_dentry = au_h_dptr(dentry, bindex);
9760 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9761 +               dget(h_dentry);
9762 +               goto out; /* success */
9763 +       }
9764 +
9765 +       inode = d_inode(dentry);
9766 +       AuDebugOn(bindex < au_ibtop(inode));
9767 +       AuDebugOn(au_ibbot(inode) < bindex);
9768 +       h_inode = au_h_iptr(inode, bindex);
9769 +       h_dentry = d_find_alias(h_inode);
9770 +       if (h_dentry) {
9771 +               if (!IS_ERR(h_dentry)) {
9772 +                       if (!au_d_linkable(h_dentry))
9773 +                               goto out; /* success */
9774 +                       dput(h_dentry);
9775 +               } else
9776 +                       goto out;
9777 +       }
9778 +
9779 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9780 +               h_dentry = au_plink_lkup(inode, bindex);
9781 +               AuDebugOn(!h_dentry);
9782 +               if (!IS_ERR(h_dentry)) {
9783 +                       if (!au_d_hashed_positive(h_dentry))
9784 +                               goto out; /* success */
9785 +                       dput(h_dentry);
9786 +                       h_dentry = NULL;
9787 +               }
9788 +       }
9789 +
9790 +out:
9791 +       AuDbgDentry(h_dentry);
9792 +       return h_dentry;
9793 +}
9794 +
9795 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9796 +{
9797 +       aufs_bindex_t bbot, bwh;
9798 +
9799 +       bbot = au_dbbot(dentry);
9800 +       if (0 <= bbot) {
9801 +               bwh = au_dbwh(dentry);
9802 +               if (!bwh)
9803 +                       return bwh;
9804 +               if (0 < bwh && bwh < bbot)
9805 +                       return bwh - 1;
9806 +       }
9807 +       return bbot;
9808 +}
9809 +
9810 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9811 +{
9812 +       aufs_bindex_t bbot, bopq;
9813 +
9814 +       bbot = au_dbtail(dentry);
9815 +       if (0 <= bbot) {
9816 +               bopq = au_dbdiropq(dentry);
9817 +               if (0 <= bopq && bopq < bbot)
9818 +                       bbot = bopq;
9819 +       }
9820 +       return bbot;
9821 +}
9822 +
9823 +/* ---------------------------------------------------------------------- */
9824 +
9825 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9826 +                  struct dentry *h_dentry)
9827 +{
9828 +       struct au_dinfo *dinfo;
9829 +       struct au_hdentry *hd;
9830 +       struct au_branch *br;
9831 +
9832 +       DiMustWriteLock(dentry);
9833 +
9834 +       dinfo = au_di(dentry);
9835 +       hd = au_hdentry(dinfo, bindex);
9836 +       au_hdput(hd);
9837 +       hd->hd_dentry = h_dentry;
9838 +       if (h_dentry) {
9839 +               br = au_sbr(dentry->d_sb, bindex);
9840 +               hd->hd_id = br->br_id;
9841 +       }
9842 +}
9843 +
9844 +int au_dbrange_test(struct dentry *dentry)
9845 +{
9846 +       int err;
9847 +       aufs_bindex_t btop, bbot;
9848 +
9849 +       err = 0;
9850 +       btop = au_dbtop(dentry);
9851 +       bbot = au_dbbot(dentry);
9852 +       if (btop >= 0)
9853 +               AuDebugOn(bbot < 0 && btop > bbot);
9854 +       else {
9855 +               err = -EIO;
9856 +               AuDebugOn(bbot >= 0);
9857 +       }
9858 +
9859 +       return err;
9860 +}
9861 +
9862 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9863 +{
9864 +       int err;
9865 +
9866 +       err = 0;
9867 +       if (unlikely(au_digen(dentry) != sigen
9868 +                    || au_iigen_test(d_inode(dentry), sigen)))
9869 +               err = -EIO;
9870 +
9871 +       return err;
9872 +}
9873 +
9874 +void au_update_digen(struct dentry *dentry)
9875 +{
9876 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9877 +       /* smp_mb(); */ /* atomic_set */
9878 +}
9879 +
9880 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9881 +{
9882 +       struct au_dinfo *dinfo;
9883 +       struct dentry *h_d;
9884 +       struct au_hdentry *hdp;
9885 +       aufs_bindex_t bindex, bbot;
9886 +
9887 +       DiMustWriteLock(dentry);
9888 +
9889 +       dinfo = au_di(dentry);
9890 +       if (!dinfo || dinfo->di_btop < 0)
9891 +               return;
9892 +
9893 +       if (do_put_zero) {
9894 +               bbot = dinfo->di_bbot;
9895 +               bindex = dinfo->di_btop;
9896 +               hdp = au_hdentry(dinfo, bindex);
9897 +               for (; bindex <= bbot; bindex++, hdp++) {
9898 +                       h_d = hdp->hd_dentry;
9899 +                       if (h_d && d_is_negative(h_d))
9900 +                               au_set_h_dptr(dentry, bindex, NULL);
9901 +               }
9902 +       }
9903 +
9904 +       dinfo->di_btop = 0;
9905 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9906 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9907 +               if (hdp->hd_dentry)
9908 +                       break;
9909 +       if (dinfo->di_btop > dinfo->di_bbot) {
9910 +               dinfo->di_btop = -1;
9911 +               dinfo->di_bbot = -1;
9912 +               return;
9913 +       }
9914 +
9915 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9916 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9917 +               if (hdp->hd_dentry)
9918 +                       break;
9919 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9920 +}
9921 +
9922 +void au_update_dbtop(struct dentry *dentry)
9923 +{
9924 +       aufs_bindex_t bindex, bbot;
9925 +       struct dentry *h_dentry;
9926 +
9927 +       bbot = au_dbbot(dentry);
9928 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9929 +               h_dentry = au_h_dptr(dentry, bindex);
9930 +               if (!h_dentry)
9931 +                       continue;
9932 +               if (d_is_positive(h_dentry)) {
9933 +                       au_set_dbtop(dentry, bindex);
9934 +                       return;
9935 +               }
9936 +               au_set_h_dptr(dentry, bindex, NULL);
9937 +       }
9938 +}
9939 +
9940 +void au_update_dbbot(struct dentry *dentry)
9941 +{
9942 +       aufs_bindex_t bindex, btop;
9943 +       struct dentry *h_dentry;
9944 +
9945 +       btop = au_dbtop(dentry);
9946 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9947 +               h_dentry = au_h_dptr(dentry, bindex);
9948 +               if (!h_dentry)
9949 +                       continue;
9950 +               if (d_is_positive(h_dentry)) {
9951 +                       au_set_dbbot(dentry, bindex);
9952 +                       return;
9953 +               }
9954 +               au_set_h_dptr(dentry, bindex, NULL);
9955 +       }
9956 +}
9957 +
9958 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9959 +{
9960 +       aufs_bindex_t bindex, bbot;
9961 +
9962 +       bbot = au_dbbot(dentry);
9963 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9964 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9965 +                       return bindex;
9966 +       return -1;
9967 +}
9968 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9969 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9970 +++ linux/fs/aufs/dir.c 2024-05-13 17:13:00.618070924 +0200
9971 @@ -0,0 +1,766 @@
9972 +// SPDX-License-Identifier: GPL-2.0
9973 +/*
9974 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9975 + *
9976 + * This program is free software; you can redistribute it and/or modify
9977 + * it under the terms of the GNU General Public License as published by
9978 + * the Free Software Foundation; either version 2 of the License, or
9979 + * (at your option) any later version.
9980 + *
9981 + * This program is distributed in the hope that it will be useful,
9982 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9983 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9984 + * GNU General Public License for more details.
9985 + *
9986 + * You should have received a copy of the GNU General Public License
9987 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9988 + */
9989 +
9990 +/*
9991 + * directory operations
9992 + */
9993 +
9994 +#include <linux/fs_stack.h>
9995 +#include <linux/iversion.h>
9996 +#include "aufs.h"
9997 +
9998 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9999 +{
10000 +       unsigned int nlink;
10001 +
10002 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10003 +
10004 +       nlink = dir->i_nlink;
10005 +       nlink += h_dir->i_nlink - 2;
10006 +       if (h_dir->i_nlink < 2)
10007 +               nlink += 2;
10008 +       smp_mb(); /* for i_nlink */
10009 +       /* 0 can happen in revaliding */
10010 +       set_nlink(dir, nlink);
10011 +}
10012 +
10013 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10014 +{
10015 +       unsigned int nlink;
10016 +
10017 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10018 +
10019 +       nlink = dir->i_nlink;
10020 +       nlink -= h_dir->i_nlink - 2;
10021 +       if (h_dir->i_nlink < 2)
10022 +               nlink -= 2;
10023 +       smp_mb(); /* for i_nlink */
10024 +       /* nlink == 0 means the branch-fs is broken */
10025 +       set_nlink(dir, nlink);
10026 +}
10027 +
10028 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10029 +{
10030 +       loff_t sz;
10031 +       aufs_bindex_t bindex, bbot;
10032 +       struct file *h_file;
10033 +       struct dentry *h_dentry;
10034 +
10035 +       sz = 0;
10036 +       if (file) {
10037 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10038 +
10039 +               bbot = au_fbbot_dir(file);
10040 +               for (bindex = au_fbtop(file);
10041 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10042 +                    bindex++) {
10043 +                       h_file = au_hf_dir(file, bindex);
10044 +                       if (h_file && file_inode(h_file))
10045 +                               sz += vfsub_f_size_read(h_file);
10046 +               }
10047 +       } else {
10048 +               AuDebugOn(!dentry);
10049 +               AuDebugOn(!d_is_dir(dentry));
10050 +
10051 +               bbot = au_dbtaildir(dentry);
10052 +               for (bindex = au_dbtop(dentry);
10053 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10054 +                    bindex++) {
10055 +                       h_dentry = au_h_dptr(dentry, bindex);
10056 +                       if (h_dentry && d_is_positive(h_dentry))
10057 +                               sz += i_size_read(d_inode(h_dentry));
10058 +               }
10059 +       }
10060 +       if (sz < KMALLOC_MAX_SIZE)
10061 +               sz = roundup_pow_of_two(sz);
10062 +       if (sz > KMALLOC_MAX_SIZE)
10063 +               sz = KMALLOC_MAX_SIZE;
10064 +       else if (sz < NAME_MAX) {
10065 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10066 +               sz = AUFS_RDBLK_DEF;
10067 +       }
10068 +       return sz;
10069 +}
10070 +
10071 +struct au_dir_ts_arg {
10072 +       struct dentry *dentry;
10073 +       aufs_bindex_t brid;
10074 +};
10075 +
10076 +static void au_do_dir_ts(void *arg)
10077 +{
10078 +       struct au_dir_ts_arg *a = arg;
10079 +       struct au_dtime dt;
10080 +       struct path h_path;
10081 +       struct timespec64 ts;
10082 +       struct inode *dir, *h_dir;
10083 +       struct super_block *sb;
10084 +       struct au_branch *br;
10085 +       struct au_hinode *hdir;
10086 +       int err;
10087 +       aufs_bindex_t btop, bindex;
10088 +
10089 +       sb = a->dentry->d_sb;
10090 +       if (d_really_is_negative(a->dentry))
10091 +               goto out;
10092 +       /* no dir->i_mutex lock */
10093 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10094 +
10095 +       dir = d_inode(a->dentry);
10096 +       btop = au_ibtop(dir);
10097 +       bindex = au_br_index(sb, a->brid);
10098 +       if (bindex < btop)
10099 +               goto out_unlock;
10100 +
10101 +       br = au_sbr(sb, bindex);
10102 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10103 +       if (!h_path.dentry)
10104 +               goto out_unlock;
10105 +       h_path.mnt = au_br_mnt(br);
10106 +       au_dtime_store(&dt, a->dentry, &h_path);
10107 +
10108 +       br = au_sbr(sb, btop);
10109 +       if (!au_br_writable(br->br_perm))
10110 +               goto out_unlock;
10111 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10112 +       h_path.mnt = au_br_mnt(br);
10113 +       err = vfsub_mnt_want_write(h_path.mnt);
10114 +       if (err)
10115 +               goto out_unlock;
10116 +       hdir = au_hi(dir, btop);
10117 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10118 +       h_dir = au_h_iptr(dir, btop);
10119 +       ts = inode_get_mtime(h_dir);
10120 +       if (h_dir->i_nlink
10121 +           && timespec64_compare(&ts, &dt.dt_mtime) < 0) {
10122 +               dt.dt_h_path = h_path;
10123 +               au_dtime_revert(&dt);
10124 +       }
10125 +       au_hn_inode_unlock(hdir);
10126 +       vfsub_mnt_drop_write(h_path.mnt);
10127 +       au_cpup_attr_timesizes(dir);
10128 +
10129 +out_unlock:
10130 +       aufs_read_unlock(a->dentry, AuLock_DW);
10131 +out:
10132 +       dput(a->dentry);
10133 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10134 +       au_kfree_try_rcu(arg);
10135 +}
10136 +
10137 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10138 +{
10139 +       int perm, wkq_err;
10140 +       aufs_bindex_t btop;
10141 +       struct au_dir_ts_arg *arg;
10142 +       struct dentry *dentry;
10143 +       struct super_block *sb;
10144 +
10145 +       IMustLock(dir);
10146 +
10147 +       dentry = d_find_any_alias(dir);
10148 +       AuDebugOn(!dentry);
10149 +       sb = dentry->d_sb;
10150 +       btop = au_ibtop(dir);
10151 +       if (btop == bindex) {
10152 +               au_cpup_attr_timesizes(dir);
10153 +               goto out;
10154 +       }
10155 +
10156 +       perm = au_sbr_perm(sb, btop);
10157 +       if (!au_br_writable(perm))
10158 +               goto out;
10159 +
10160 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10161 +       if (!arg)
10162 +               goto out;
10163 +
10164 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10165 +       arg->brid = au_sbr_id(sb, bindex);
10166 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10167 +       if (unlikely(wkq_err)) {
10168 +               pr_err("wkq %d\n", wkq_err);
10169 +               dput(dentry);
10170 +               au_kfree_try_rcu(arg);
10171 +       }
10172 +
10173 +out:
10174 +       dput(dentry);
10175 +}
10176 +
10177 +/* ---------------------------------------------------------------------- */
10178 +
10179 +static int reopen_dir(struct file *file)
10180 +{
10181 +       int err;
10182 +       unsigned int flags;
10183 +       aufs_bindex_t bindex, btail, btop;
10184 +       struct dentry *dentry, *h_dentry;
10185 +       struct file *h_file;
10186 +
10187 +       /* open all lower dirs */
10188 +       dentry = file->f_path.dentry;
10189 +       btop = au_dbtop(dentry);
10190 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10191 +               au_set_h_fptr(file, bindex, NULL);
10192 +       au_set_fbtop(file, btop);
10193 +
10194 +       btail = au_dbtaildir(dentry);
10195 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10196 +               au_set_h_fptr(file, bindex, NULL);
10197 +       au_set_fbbot_dir(file, btail);
10198 +
10199 +       flags = vfsub_file_flags(file);
10200 +       for (bindex = btop; bindex <= btail; bindex++) {
10201 +               h_dentry = au_h_dptr(dentry, bindex);
10202 +               if (!h_dentry)
10203 +                       continue;
10204 +               h_file = au_hf_dir(file, bindex);
10205 +               if (h_file)
10206 +                       continue;
10207 +
10208 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10209 +               err = PTR_ERR(h_file);
10210 +               if (IS_ERR(h_file))
10211 +                       goto out; /* close all? */
10212 +               au_set_h_fptr(file, bindex, h_file);
10213 +       }
10214 +       au_update_figen(file);
10215 +       /* todo: necessary? */
10216 +       /* file->f_ra = h_file->f_ra; */
10217 +       err = 0;
10218 +
10219 +out:
10220 +       return err;
10221 +}
10222 +
10223 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10224 +{
10225 +       int err;
10226 +       aufs_bindex_t bindex, btail;
10227 +       struct dentry *dentry, *h_dentry;
10228 +       struct vfsmount *mnt;
10229 +
10230 +       FiMustWriteLock(file);
10231 +       AuDebugOn(h_file);
10232 +
10233 +       err = 0;
10234 +       mnt = file->f_path.mnt;
10235 +       dentry = file->f_path.dentry;
10236 +       file->f_version = inode_query_iversion(d_inode(dentry));
10237 +       bindex = au_dbtop(dentry);
10238 +       au_set_fbtop(file, bindex);
10239 +       btail = au_dbtaildir(dentry);
10240 +       au_set_fbbot_dir(file, btail);
10241 +       for (; !err && bindex <= btail; bindex++) {
10242 +               h_dentry = au_h_dptr(dentry, bindex);
10243 +               if (!h_dentry)
10244 +                       continue;
10245 +
10246 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10247 +               if (unlikely(err))
10248 +                       break;
10249 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10250 +               if (IS_ERR(h_file)) {
10251 +                       err = PTR_ERR(h_file);
10252 +                       break;
10253 +               }
10254 +               au_set_h_fptr(file, bindex, h_file);
10255 +       }
10256 +       au_update_figen(file);
10257 +       /* todo: necessary? */
10258 +       /* file->f_ra = h_file->f_ra; */
10259 +       if (!err)
10260 +               return 0; /* success */
10261 +
10262 +       /* close all */
10263 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10264 +               au_set_h_fptr(file, bindex, NULL);
10265 +       au_set_fbtop(file, -1);
10266 +       au_set_fbbot_dir(file, -1);
10267 +
10268 +       return err;
10269 +}
10270 +
10271 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10272 +                        struct file *file)
10273 +{
10274 +       int err;
10275 +       struct super_block *sb;
10276 +       struct au_fidir *fidir;
10277 +
10278 +       err = -ENOMEM;
10279 +       sb = file->f_path.dentry->d_sb;
10280 +       si_read_lock(sb, AuLock_FLUSH);
10281 +       fidir = au_fidir_alloc(sb);
10282 +       if (fidir) {
10283 +               struct au_do_open_args args = {
10284 +                       .open   = do_open_dir,
10285 +                       .fidir  = fidir
10286 +               };
10287 +               err = au_do_open(file, &args);
10288 +               if (unlikely(err))
10289 +                       au_kfree_rcu(fidir);
10290 +       }
10291 +       si_read_unlock(sb);
10292 +       return err;
10293 +}
10294 +
10295 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10296 +                           struct file *file)
10297 +{
10298 +       struct au_vdir *vdir_cache;
10299 +       struct au_finfo *finfo;
10300 +       struct au_fidir *fidir;
10301 +       struct au_hfile *hf;
10302 +       aufs_bindex_t bindex, bbot;
10303 +
10304 +       finfo = au_fi(file);
10305 +       fidir = finfo->fi_hdir;
10306 +       if (fidir) {
10307 +               au_hbl_del(&finfo->fi_hlist,
10308 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10309 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10310 +               if (vdir_cache)
10311 +                       au_vdir_free(vdir_cache);
10312 +
10313 +               bindex = finfo->fi_btop;
10314 +               if (bindex >= 0) {
10315 +                       hf = fidir->fd_hfile + bindex;
10316 +                       /*
10317 +                        * calls fput() instead of filp_close(),
10318 +                        * since no dnotify or lock for the lower file.
10319 +                        */
10320 +                       bbot = fidir->fd_bbot;
10321 +                       for (; bindex <= bbot; bindex++, hf++)
10322 +                               if (hf->hf_file)
10323 +                                       au_hfput(hf, /*execed*/0);
10324 +               }
10325 +               au_kfree_rcu(fidir);
10326 +               finfo->fi_hdir = NULL;
10327 +       }
10328 +       au_finfo_fin(file);
10329 +       return 0;
10330 +}
10331 +
10332 +/* ---------------------------------------------------------------------- */
10333 +
10334 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10335 +{
10336 +       int err;
10337 +       aufs_bindex_t bindex, bbot;
10338 +       struct file *h_file;
10339 +
10340 +       err = 0;
10341 +       bbot = au_fbbot_dir(file);
10342 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10343 +               h_file = au_hf_dir(file, bindex);
10344 +               if (h_file)
10345 +                       err = vfsub_flush(h_file, id);
10346 +       }
10347 +       return err;
10348 +}
10349 +
10350 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10351 +{
10352 +       return au_do_flush(file, id, au_do_flush_dir);
10353 +}
10354 +
10355 +/* ---------------------------------------------------------------------- */
10356 +
10357 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10358 +{
10359 +       int err;
10360 +       aufs_bindex_t bbot, bindex;
10361 +       struct inode *inode;
10362 +       struct super_block *sb;
10363 +
10364 +       err = 0;
10365 +       sb = dentry->d_sb;
10366 +       inode = d_inode(dentry);
10367 +       IMustLock(inode);
10368 +       bbot = au_dbbot(dentry);
10369 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10370 +               struct path h_path;
10371 +
10372 +               if (au_test_ro(sb, bindex, inode))
10373 +                       continue;
10374 +               h_path.dentry = au_h_dptr(dentry, bindex);
10375 +               if (!h_path.dentry)
10376 +                       continue;
10377 +
10378 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10379 +               err = vfsub_fsync(NULL, &h_path, datasync);
10380 +       }
10381 +
10382 +       return err;
10383 +}
10384 +
10385 +static int au_do_fsync_dir(struct file *file, int datasync)
10386 +{
10387 +       int err;
10388 +       aufs_bindex_t bbot, bindex;
10389 +       struct file *h_file;
10390 +       struct super_block *sb;
10391 +       struct inode *inode;
10392 +
10393 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10394 +       if (unlikely(err))
10395 +               goto out;
10396 +
10397 +       inode = file_inode(file);
10398 +       sb = inode->i_sb;
10399 +       bbot = au_fbbot_dir(file);
10400 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10401 +               h_file = au_hf_dir(file, bindex);
10402 +               if (!h_file || au_test_ro(sb, bindex, inode))
10403 +                       continue;
10404 +
10405 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10406 +       }
10407 +
10408 +out:
10409 +       return err;
10410 +}
10411 +
10412 +/*
10413 + * @file may be NULL
10414 + */
10415 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10416 +                         int datasync)
10417 +{
10418 +       int err;
10419 +       struct dentry *dentry;
10420 +       struct inode *inode;
10421 +       struct super_block *sb;
10422 +
10423 +       err = 0;
10424 +       dentry = file->f_path.dentry;
10425 +       inode = d_inode(dentry);
10426 +       inode_lock(inode);
10427 +       sb = dentry->d_sb;
10428 +       si_noflush_read_lock(sb);
10429 +       if (file)
10430 +               err = au_do_fsync_dir(file, datasync);
10431 +       else {
10432 +               di_write_lock_child(dentry);
10433 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10434 +       }
10435 +       au_cpup_attr_timesizes(inode);
10436 +       di_write_unlock(dentry);
10437 +       if (file)
10438 +               fi_write_unlock(file);
10439 +
10440 +       si_read_unlock(sb);
10441 +       inode_unlock(inode);
10442 +       return err;
10443 +}
10444 +
10445 +/* ---------------------------------------------------------------------- */
10446 +
10447 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10448 +{
10449 +       int err;
10450 +       struct dentry *dentry;
10451 +       struct inode *inode, *h_inode;
10452 +       struct super_block *sb;
10453 +
10454 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10455 +
10456 +       dentry = file->f_path.dentry;
10457 +       inode = d_inode(dentry);
10458 +       IMustLock(inode);
10459 +
10460 +       sb = dentry->d_sb;
10461 +       si_read_lock(sb, AuLock_FLUSH);
10462 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10463 +       if (unlikely(err))
10464 +               goto out;
10465 +       err = au_alive_dir(dentry);
10466 +       if (!err)
10467 +               err = au_vdir_init(file);
10468 +       di_downgrade_lock(dentry, AuLock_IR);
10469 +       if (unlikely(err))
10470 +               goto out_unlock;
10471 +
10472 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10473 +       if (!au_test_nfsd()) {
10474 +               err = au_vdir_fill_de(file, ctx);
10475 +               fsstack_copy_attr_atime(inode, h_inode);
10476 +       } else {
10477 +               /*
10478 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10479 +                * encode_fh() and others.
10480 +                */
10481 +               atomic_inc(&h_inode->i_count);
10482 +               di_read_unlock(dentry, AuLock_IR);
10483 +               si_read_unlock(sb);
10484 +               err = au_vdir_fill_de(file, ctx);
10485 +               fsstack_copy_attr_atime(inode, h_inode);
10486 +               fi_write_unlock(file);
10487 +               iput(h_inode);
10488 +
10489 +               AuTraceErr(err);
10490 +               return err;
10491 +       }
10492 +
10493 +out_unlock:
10494 +       di_read_unlock(dentry, AuLock_IR);
10495 +       fi_write_unlock(file);
10496 +out:
10497 +       si_read_unlock(sb);
10498 +       return err;
10499 +}
10500 +
10501 +/* ---------------------------------------------------------------------- */
10502 +
10503 +#define AuTestEmpty_WHONLY     BIT(0)
10504 +#define AuTestEmpty_CALLED     BIT(1)
10505 +#define AuTestEmpty_SHWH       BIT(2)
10506 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10507 +#define au_fset_testempty(flags, name) \
10508 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10509 +#define au_fclr_testempty(flags, name) \
10510 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10511 +
10512 +#ifndef CONFIG_AUFS_SHWH
10513 +#undef AuTestEmpty_SHWH
10514 +#define AuTestEmpty_SHWH       0
10515 +#endif
10516 +
10517 +struct test_empty_arg {
10518 +       struct dir_context ctx;
10519 +       struct au_nhash *whlist;
10520 +       unsigned int flags;
10521 +       int err;
10522 +       aufs_bindex_t bindex;
10523 +};
10524 +
10525 +static bool test_empty_cb(struct dir_context *ctx, const char *__name,
10526 +                         int namelen, loff_t offset __maybe_unused, u64 ino,
10527 +                         unsigned int d_type)
10528 +{
10529 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10530 +                                                 ctx);
10531 +       char *name = (void *)__name;
10532 +
10533 +       arg->err = 0;
10534 +       au_fset_testempty(arg->flags, CALLED);
10535 +       /* smp_mb(); */
10536 +       if (is_dot_dotdot(name, namelen))
10537 +               goto out; /* success */
10538 +
10539 +       if (namelen <= AUFS_WH_PFX_LEN
10540 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10541 +               if (au_ftest_testempty(arg->flags, WHONLY)
10542 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10543 +                       arg->err = -ENOTEMPTY;
10544 +               goto out;
10545 +       }
10546 +
10547 +       name += AUFS_WH_PFX_LEN;
10548 +       namelen -= AUFS_WH_PFX_LEN;
10549 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10550 +               arg->err = au_nhash_append_wh
10551 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10552 +                        au_ftest_testempty(arg->flags, SHWH));
10553 +
10554 +out:
10555 +       /* smp_mb(); */
10556 +       AuTraceErr(arg->err);
10557 +       return !arg->err;
10558 +}
10559 +
10560 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10561 +{
10562 +       int err;
10563 +       struct file *h_file;
10564 +       struct au_branch *br;
10565 +
10566 +       h_file = au_h_open(dentry, arg->bindex,
10567 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10568 +                          /*file*/NULL, /*force_wr*/0);
10569 +       err = PTR_ERR(h_file);
10570 +       if (IS_ERR(h_file))
10571 +               goto out;
10572 +
10573 +       err = 0;
10574 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10575 +           && !file_inode(h_file)->i_nlink)
10576 +               goto out_put;
10577 +
10578 +       do {
10579 +               arg->err = 0;
10580 +               au_fclr_testempty(arg->flags, CALLED);
10581 +               /* smp_mb(); */
10582 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10583 +               if (err >= 0)
10584 +                       err = arg->err;
10585 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10586 +
10587 +out_put:
10588 +       fput(h_file);
10589 +       br = au_sbr(dentry->d_sb, arg->bindex);
10590 +       au_lcnt_dec(&br->br_nfiles);
10591 +out:
10592 +       return err;
10593 +}
10594 +
10595 +struct do_test_empty_args {
10596 +       int *errp;
10597 +       struct dentry *dentry;
10598 +       struct test_empty_arg *arg;
10599 +};
10600 +
10601 +static void call_do_test_empty(void *args)
10602 +{
10603 +       struct do_test_empty_args *a = args;
10604 +       *a->errp = do_test_empty(a->dentry, a->arg);
10605 +}
10606 +
10607 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10608 +{
10609 +       int err, wkq_err;
10610 +       struct dentry *h_dentry;
10611 +       struct inode *h_inode;
10612 +       struct mnt_idmap *h_idmap;
10613 +
10614 +       h_idmap = au_sbr_idmap(dentry->d_sb, arg->bindex);
10615 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10616 +       h_inode = d_inode(h_dentry);
10617 +       /* todo: i_mode changes anytime? */
10618 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10619 +       err = au_test_h_perm_sio(h_idmap, h_inode, MAY_EXEC | MAY_READ);
10620 +       inode_unlock_shared(h_inode);
10621 +       if (!err)
10622 +               err = do_test_empty(dentry, arg);
10623 +       else {
10624 +               struct do_test_empty_args args = {
10625 +                       .errp   = &err,
10626 +                       .dentry = dentry,
10627 +                       .arg    = arg
10628 +               };
10629 +               unsigned int flags = arg->flags;
10630 +
10631 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10632 +               if (unlikely(wkq_err))
10633 +                       err = wkq_err;
10634 +               arg->flags = flags;
10635 +       }
10636 +
10637 +       return err;
10638 +}
10639 +
10640 +int au_test_empty_lower(struct dentry *dentry)
10641 +{
10642 +       int err;
10643 +       unsigned int rdhash;
10644 +       aufs_bindex_t bindex, btop, btail;
10645 +       struct au_nhash whlist;
10646 +       struct test_empty_arg arg = {
10647 +               .ctx = {
10648 +                       .actor = test_empty_cb
10649 +               }
10650 +       };
10651 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10652 +
10653 +       SiMustAnyLock(dentry->d_sb);
10654 +
10655 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10656 +       if (!rdhash)
10657 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10658 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10659 +       if (unlikely(err))
10660 +               goto out;
10661 +
10662 +       arg.flags = 0;
10663 +       arg.whlist = &whlist;
10664 +       btop = au_dbtop(dentry);
10665 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10666 +               au_fset_testempty(arg.flags, SHWH);
10667 +       test_empty = do_test_empty;
10668 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10669 +               test_empty = sio_test_empty;
10670 +       arg.bindex = btop;
10671 +       err = test_empty(dentry, &arg);
10672 +       if (unlikely(err))
10673 +               goto out_whlist;
10674 +
10675 +       au_fset_testempty(arg.flags, WHONLY);
10676 +       btail = au_dbtaildir(dentry);
10677 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10678 +               struct dentry *h_dentry;
10679 +
10680 +               h_dentry = au_h_dptr(dentry, bindex);
10681 +               if (h_dentry && d_is_positive(h_dentry)) {
10682 +                       arg.bindex = bindex;
10683 +                       err = test_empty(dentry, &arg);
10684 +               }
10685 +       }
10686 +
10687 +out_whlist:
10688 +       au_nhash_wh_free(&whlist);
10689 +out:
10690 +       return err;
10691 +}
10692 +
10693 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10694 +{
10695 +       int err;
10696 +       struct test_empty_arg arg = {
10697 +               .ctx = {
10698 +                       .actor = test_empty_cb
10699 +               }
10700 +       };
10701 +       aufs_bindex_t bindex, btail;
10702 +
10703 +       err = 0;
10704 +       arg.whlist = whlist;
10705 +       arg.flags = AuTestEmpty_WHONLY;
10706 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10707 +               au_fset_testempty(arg.flags, SHWH);
10708 +       btail = au_dbtaildir(dentry);
10709 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10710 +               struct dentry *h_dentry;
10711 +
10712 +               h_dentry = au_h_dptr(dentry, bindex);
10713 +               if (h_dentry && d_is_positive(h_dentry)) {
10714 +                       arg.bindex = bindex;
10715 +                       err = sio_test_empty(dentry, &arg);
10716 +               }
10717 +       }
10718 +
10719 +       return err;
10720 +}
10721 +
10722 +/* ---------------------------------------------------------------------- */
10723 +
10724 +const struct file_operations aufs_dir_fop = {
10725 +       .owner          = THIS_MODULE,
10726 +       .llseek         = default_llseek,
10727 +       .read           = generic_read_dir,
10728 +       .iterate_shared = aufs_iterate_shared,
10729 +       .unlocked_ioctl = aufs_ioctl_dir,
10730 +#ifdef CONFIG_COMPAT
10731 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10732 +#endif
10733 +       .open           = aufs_open_dir,
10734 +       .release        = aufs_release_dir,
10735 +       .flush          = aufs_flush_dir,
10736 +       .fsync          = aufs_fsync_dir
10737 +};
10738 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10739 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10740 +++ linux/fs/aufs/dir.h 2024-05-13 17:13:00.618070924 +0200
10741 @@ -0,0 +1,134 @@
10742 +/* SPDX-License-Identifier: GPL-2.0 */
10743 +/*
10744 + * Copyright (C) 2005-2022 Junjiro R. Okajima
10745 + *
10746 + * This program is free software; you can redistribute it and/or modify
10747 + * it under the terms of the GNU General Public License as published by
10748 + * the Free Software Foundation; either version 2 of the License, or
10749 + * (at your option) any later version.
10750 + *
10751 + * This program is distributed in the hope that it will be useful,
10752 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10753 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10754 + * GNU General Public License for more details.
10755 + *
10756 + * You should have received a copy of the GNU General Public License
10757 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10758 + */
10759 +
10760 +/*
10761 + * directory operations
10762 + */
10763 +
10764 +#ifndef __AUFS_DIR_H__
10765 +#define __AUFS_DIR_H__
10766 +
10767 +#ifdef __KERNEL__
10768 +
10769 +#include <linux/fs.h>
10770 +
10771 +/* ---------------------------------------------------------------------- */
10772 +
10773 +/* need to be faster and smaller */
10774 +
10775 +struct au_nhash {
10776 +       unsigned int            nh_num;
10777 +       struct hlist_head       *nh_head;
10778 +};
10779 +
10780 +struct au_vdir_destr {
10781 +       unsigned char   len;
10782 +       unsigned char   name[];
10783 +} __packed;
10784 +
10785 +struct au_vdir_dehstr {
10786 +       struct hlist_node       hash;
10787 +       struct au_vdir_destr    *str;
10788 +       struct rcu_head         rcu;
10789 +} ____cacheline_aligned_in_smp;
10790 +
10791 +struct au_vdir_de {
10792 +       ino_t                   de_ino;
10793 +       unsigned char           de_type;
10794 +       /* caution: packed */
10795 +       struct au_vdir_destr    de_str;
10796 +} __packed;
10797 +
10798 +struct au_vdir_wh {
10799 +       struct hlist_node       wh_hash;
10800 +#ifdef CONFIG_AUFS_SHWH
10801 +       ino_t                   wh_ino;
10802 +       aufs_bindex_t           wh_bindex;
10803 +       unsigned char           wh_type;
10804 +#else
10805 +       aufs_bindex_t           wh_bindex;
10806 +#endif
10807 +       /* caution: packed */
10808 +       struct au_vdir_destr    wh_str;
10809 +} __packed;
10810 +
10811 +union au_vdir_deblk_p {
10812 +       unsigned char           *deblk;
10813 +       struct au_vdir_de       *de;
10814 +};
10815 +
10816 +struct au_vdir {
10817 +       unsigned char   **vd_deblk;
10818 +       unsigned long   vd_nblk;
10819 +       struct {
10820 +               unsigned long           ul;
10821 +               union au_vdir_deblk_p   p;
10822 +       } vd_last;
10823 +
10824 +       u64             vd_version;
10825 +       unsigned int    vd_deblk_sz;
10826 +       unsigned long   vd_jiffy;
10827 +       struct rcu_head rcu;
10828 +} ____cacheline_aligned_in_smp;
10829 +
10830 +/* ---------------------------------------------------------------------- */
10831 +
10832 +/* dir.c */
10833 +extern const struct file_operations aufs_dir_fop;
10834 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10835 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10836 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10837 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10838 +int au_test_empty_lower(struct dentry *dentry);
10839 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10840 +
10841 +/* vdir.c */
10842 +unsigned int au_rdhash_est(loff_t sz);
10843 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10844 +void au_nhash_wh_free(struct au_nhash *whlist);
10845 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10846 +                           int limit);
10847 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10848 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10849 +                      unsigned int d_type, aufs_bindex_t bindex,
10850 +                      unsigned char shwh);
10851 +void au_vdir_free(struct au_vdir *vdir);
10852 +int au_vdir_init(struct file *file);
10853 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10854 +
10855 +/* ioctl.c */
10856 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10857 +
10858 +#ifdef CONFIG_AUFS_RDU
10859 +/* rdu.c */
10860 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10861 +#ifdef CONFIG_COMPAT
10862 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10863 +                        unsigned long arg);
10864 +#endif
10865 +#else
10866 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10867 +       unsigned int cmd, unsigned long arg)
10868 +#ifdef CONFIG_COMPAT
10869 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10870 +       unsigned int cmd, unsigned long arg)
10871 +#endif
10872 +#endif
10873 +
10874 +#endif /* __KERNEL__ */
10875 +#endif /* __AUFS_DIR_H__ */
10876 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10877 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10878 +++ linux/fs/aufs/dirren.c      2024-05-13 17:13:00.618070924 +0200
10879 @@ -0,0 +1,1315 @@
10880 +// SPDX-License-Identifier: GPL-2.0
10881 +/*
10882 + * Copyright (C) 2017-2022 Junjiro R. Okajima
10883 + *
10884 + * This program is free software; you can redistribute it and/or modify
10885 + * it under the terms of the GNU General Public License as published by
10886 + * the Free Software Foundation; either version 2 of the License, or
10887 + * (at your option) any later version.
10888 + *
10889 + * This program is distributed in the hope that it will be useful,
10890 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10891 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10892 + * GNU General Public License for more details.
10893 + *
10894 + * You should have received a copy of the GNU General Public License
10895 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10896 + */
10897 +
10898 +/*
10899 + * special handling in renaming a directory
10900 + * in order to support looking-up the before-renamed name on the lower readonly
10901 + * branches
10902 + */
10903 +
10904 +#include <linux/byteorder/generic.h>
10905 +#include "aufs.h"
10906 +
10907 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10908 +{
10909 +       int idx;
10910 +
10911 +       idx = au_dr_ihash(ent->dr_h_ino);
10912 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10913 +}
10914 +
10915 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10916 +{
10917 +       int ret, i;
10918 +       struct hlist_bl_head *hbl;
10919 +
10920 +       ret = 1;
10921 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10922 +               hbl = dr->dr_h_ino + i;
10923 +               hlist_bl_lock(hbl);
10924 +               ret &= hlist_bl_empty(hbl);
10925 +               hlist_bl_unlock(hbl);
10926 +       }
10927 +
10928 +       return ret;
10929 +}
10930 +
10931 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10932 +{
10933 +       struct au_dr_hino *found, *ent;
10934 +       struct hlist_bl_head *hbl;
10935 +       struct hlist_bl_node *pos;
10936 +       int idx;
10937 +
10938 +       found = NULL;
10939 +       idx = au_dr_ihash(ino);
10940 +       hbl = dr->dr_h_ino + idx;
10941 +       hlist_bl_lock(hbl);
10942 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10943 +               if (ent->dr_h_ino == ino) {
10944 +                       found = ent;
10945 +                       break;
10946 +               }
10947 +       hlist_bl_unlock(hbl);
10948 +
10949 +       return found;
10950 +}
10951 +
10952 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10953 +                       struct au_dr_hino *add_ent)
10954 +{
10955 +       int found, idx;
10956 +       struct hlist_bl_head *hbl;
10957 +       struct hlist_bl_node *pos;
10958 +       struct au_dr_hino *ent;
10959 +
10960 +       found = 0;
10961 +       idx = au_dr_ihash(ino);
10962 +       hbl = dr->dr_h_ino + idx;
10963 +#if 0 /* debug print */
10964 +       {
10965 +               struct hlist_bl_node *tmp;
10966 +
10967 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10968 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10969 +       }
10970 +#endif
10971 +       hlist_bl_lock(hbl);
10972 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10973 +               if (ent->dr_h_ino == ino) {
10974 +                       found = 1;
10975 +                       break;
10976 +               }
10977 +       if (!found && add_ent)
10978 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10979 +       hlist_bl_unlock(hbl);
10980 +
10981 +       if (!found && add_ent)
10982 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10983 +
10984 +       return found;
10985 +}
10986 +
10987 +void au_dr_hino_free(struct au_dr_br *dr)
10988 +{
10989 +       int i;
10990 +       struct hlist_bl_head *hbl;
10991 +       struct hlist_bl_node *pos, *tmp;
10992 +       struct au_dr_hino *ent;
10993 +
10994 +       /* SiMustWriteLock(sb); */
10995 +
10996 +       for (i = 0; i < AuDirren_NHASH; i++) {
10997 +               hbl = dr->dr_h_ino + i;
10998 +               /* no spinlock since sbinfo must be write-locked */
10999 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11000 +                       au_kfree_rcu(ent);
11001 +               INIT_HLIST_BL_HEAD(hbl);
11002 +       }
11003 +}
11004 +
11005 +/* returns the number of inodes or an error */
11006 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11007 +                           struct file *hinofile)
11008 +{
11009 +       int err, i;
11010 +       ssize_t ssz;
11011 +       loff_t pos, oldsize;
11012 +       __be64 u64;
11013 +       struct inode *hinoinode;
11014 +       struct hlist_bl_head *hbl;
11015 +       struct hlist_bl_node *n1, *n2;
11016 +       struct au_dr_hino *ent;
11017 +
11018 +       SiMustWriteLock(sb);
11019 +       AuDebugOn(!au_br_writable(br->br_perm));
11020 +
11021 +       hinoinode = file_inode(hinofile);
11022 +       oldsize = i_size_read(hinoinode);
11023 +
11024 +       err = 0;
11025 +       pos = 0;
11026 +       hbl = br->br_dirren.dr_h_ino;
11027 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11028 +               /* no bit-lock since sbinfo must be write-locked */
11029 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11030 +                       AuDbg("hi%llu, %pD2\n",
11031 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11032 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11033 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11034 +                       if (ssz == sizeof(u64))
11035 +                               continue;
11036 +
11037 +                       /* write error */
11038 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11039 +                       err = -ENOSPC;
11040 +                       if (ssz < 0)
11041 +                               err = ssz;
11042 +                       break;
11043 +               }
11044 +       }
11045 +       /* regardless the error */
11046 +       if (pos < oldsize) {
11047 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11048 +               AuTraceErr(err);
11049 +       }
11050 +
11051 +       AuTraceErr(err);
11052 +       return err;
11053 +}
11054 +
11055 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11056 +{
11057 +       int err, hidx;
11058 +       ssize_t ssz;
11059 +       size_t sz, n;
11060 +       loff_t pos;
11061 +       uint64_t u64;
11062 +       struct au_dr_hino *ent;
11063 +       struct inode *hinoinode;
11064 +       struct hlist_bl_head *hbl;
11065 +
11066 +       err = 0;
11067 +       pos = 0;
11068 +       hbl = dr->dr_h_ino;
11069 +       hinoinode = file_inode(hinofile);
11070 +       sz = i_size_read(hinoinode);
11071 +       AuDebugOn(sz % sizeof(u64));
11072 +       n = sz / sizeof(u64);
11073 +       while (n--) {
11074 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11075 +               if (unlikely(ssz != sizeof(u64))) {
11076 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11077 +                       err = -EINVAL;
11078 +                       if (ssz < 0)
11079 +                               err = ssz;
11080 +                       goto out_free;
11081 +               }
11082 +
11083 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11084 +               if (!ent) {
11085 +                       err = -ENOMEM;
11086 +                       AuTraceErr(err);
11087 +                       goto out_free;
11088 +               }
11089 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11090 +               AuDbg("hi%llu, %pD2\n",
11091 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11092 +               hidx = au_dr_ihash(ent->dr_h_ino);
11093 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11094 +       }
11095 +       goto out; /* success */
11096 +
11097 +out_free:
11098 +       au_dr_hino_free(dr);
11099 +out:
11100 +       AuTraceErr(err);
11101 +       return err;
11102 +}
11103 +
11104 +/*
11105 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11106 + * @path is a switch to distinguish load and store.
11107 + */
11108 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11109 +                     struct au_branch *br, const struct path *path)
11110 +{
11111 +       int err, flags;
11112 +       unsigned char load, suspend;
11113 +       struct file *hinofile;
11114 +       struct au_hinode *hdir;
11115 +       struct inode *dir, *delegated;
11116 +       struct path hinopath;
11117 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11118 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11119 +
11120 +       AuDebugOn(bindex < 0 && !br);
11121 +       AuDebugOn(bindex >= 0 && br);
11122 +
11123 +       err = -EINVAL;
11124 +       suspend = !br;
11125 +       if (suspend)
11126 +               br = au_sbr(sb, bindex);
11127 +       load = !!path;
11128 +       if (!load) {
11129 +               path = &br->br_path;
11130 +               AuDebugOn(!au_br_writable(br->br_perm));
11131 +               if (unlikely(!au_br_writable(br->br_perm)))
11132 +                       goto out;
11133 +       }
11134 +
11135 +       hdir = NULL;
11136 +       if (suspend) {
11137 +               dir = d_inode(sb->s_root);
11138 +               hdir = au_hinode(au_ii(dir), bindex);
11139 +               dir = hdir->hi_inode;
11140 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11141 +       } else {
11142 +               dir = d_inode(path->dentry);
11143 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11144 +       }
11145 +       hinopath.mnt = path->mnt;
11146 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11147 +       err = PTR_ERR(hinopath.dentry);
11148 +       if (IS_ERR(hinopath.dentry))
11149 +               goto out_unlock;
11150 +
11151 +       err = 0;
11152 +       flags = O_RDONLY;
11153 +       if (load) {
11154 +               if (d_is_negative(hinopath.dentry))
11155 +                       goto out_dput; /* success */
11156 +       } else {
11157 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11158 +                       if (d_is_positive(hinopath.dentry)) {
11159 +                               delegated = NULL;
11160 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11161 +                                                  /*force*/0);
11162 +                               AuTraceErr(err);
11163 +                               if (unlikely(err))
11164 +                                       pr_err("ignored err %d, %pd2\n",
11165 +                                              err, hinopath.dentry);
11166 +                               if (unlikely(err == -EWOULDBLOCK))
11167 +                                       iput(delegated);
11168 +                               err = 0;
11169 +                       }
11170 +                       goto out_dput;
11171 +               } else if (!d_is_positive(hinopath.dentry)) {
11172 +                       err = vfsub_create(dir, &hinopath, 0600,
11173 +                                          /*want_excl*/false);
11174 +                       AuTraceErr(err);
11175 +                       if (unlikely(err))
11176 +                               goto out_dput;
11177 +               }
11178 +               flags = O_WRONLY;
11179 +       }
11180 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11181 +       if (suspend)
11182 +               au_hn_inode_unlock(hdir);
11183 +       else
11184 +               inode_unlock(dir);
11185 +       dput(hinopath.dentry);
11186 +       AuTraceErrPtr(hinofile);
11187 +       if (IS_ERR(hinofile)) {
11188 +               err = PTR_ERR(hinofile);
11189 +               goto out;
11190 +       }
11191 +
11192 +       if (load)
11193 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11194 +       else
11195 +               err = au_dr_hino_store(sb, br, hinofile);
11196 +       fput(hinofile);
11197 +       goto out;
11198 +
11199 +out_dput:
11200 +       dput(hinopath.dentry);
11201 +out_unlock:
11202 +       if (suspend)
11203 +               au_hn_inode_unlock(hdir);
11204 +       else
11205 +               inode_unlock(dir);
11206 +out:
11207 +       AuTraceErr(err);
11208 +       return err;
11209 +}
11210 +
11211 +/* ---------------------------------------------------------------------- */
11212 +
11213 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11214 +{
11215 +       int err;
11216 +       struct kstatfs kstfs;
11217 +       dev_t dev;
11218 +       struct dentry *dentry;
11219 +       struct super_block *sb;
11220 +
11221 +       err = vfs_statfs((void *)path, &kstfs);
11222 +       AuTraceErr(err);
11223 +       if (unlikely(err))
11224 +               goto out;
11225 +
11226 +       /* todo: support for UUID */
11227 +
11228 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11229 +               brid->type = AuBrid_FSID;
11230 +               brid->fsid = kstfs.f_fsid;
11231 +       } else {
11232 +               dentry = path->dentry;
11233 +               sb = dentry->d_sb;
11234 +               dev = sb->s_dev;
11235 +               if (dev) {
11236 +                       brid->type = AuBrid_DEV;
11237 +                       brid->dev = dev;
11238 +               }
11239 +       }
11240 +
11241 +out:
11242 +       return err;
11243 +}
11244 +
11245 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11246 +                 const struct path *path)
11247 +{
11248 +       int err, i;
11249 +       struct au_dr_br *dr;
11250 +       struct hlist_bl_head *hbl;
11251 +
11252 +       dr = &br->br_dirren;
11253 +       hbl = dr->dr_h_ino;
11254 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11255 +               INIT_HLIST_BL_HEAD(hbl);
11256 +
11257 +       err = au_dr_brid_init(&dr->dr_brid, path);
11258 +       if (unlikely(err))
11259 +               goto out;
11260 +
11261 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11262 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11263 +
11264 +out:
11265 +       AuTraceErr(err);
11266 +       return err;
11267 +}
11268 +
11269 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11270 +{
11271 +       int err;
11272 +
11273 +       err = 0;
11274 +       if (au_br_writable(br->br_perm))
11275 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11276 +       if (!err)
11277 +               au_dr_hino_free(&br->br_dirren);
11278 +
11279 +       return err;
11280 +}
11281 +
11282 +/* ---------------------------------------------------------------------- */
11283 +
11284 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11285 +                      char *buf, size_t sz)
11286 +{
11287 +       int err;
11288 +       unsigned int major, minor;
11289 +       char *p;
11290 +
11291 +       p = buf;
11292 +       err = snprintf(p, sz, "%d_", brid->type);
11293 +       AuDebugOn(err > sz);
11294 +       p += err;
11295 +       sz -= err;
11296 +       switch (brid->type) {
11297 +       case AuBrid_Unset:
11298 +               return -EINVAL;
11299 +       case AuBrid_UUID:
11300 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11301 +               break;
11302 +       case AuBrid_FSID:
11303 +               err = snprintf(p, sz, "%08x-%08x",
11304 +                              brid->fsid.val[0], brid->fsid.val[1]);
11305 +               break;
11306 +       case AuBrid_DEV:
11307 +               major = MAJOR(brid->dev);
11308 +               minor = MINOR(brid->dev);
11309 +               if (major <= 0xff && minor <= 0xff)
11310 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11311 +               else
11312 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11313 +               break;
11314 +       }
11315 +       AuDebugOn(err > sz);
11316 +       p += err;
11317 +       sz -= err;
11318 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11319 +       AuDebugOn(err > sz);
11320 +       p += err;
11321 +       sz -= err;
11322 +
11323 +       return p - buf;
11324 +}
11325 +
11326 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11327 +{
11328 +       int rlen;
11329 +       struct dentry *br_dentry;
11330 +       struct inode *br_inode;
11331 +
11332 +       br_dentry = au_br_dentry(br);
11333 +       br_inode = d_inode(br_dentry);
11334 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11335 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11336 +       AuDebugOn(rlen > len);
11337 +
11338 +       return rlen;
11339 +}
11340 +
11341 +/* ---------------------------------------------------------------------- */
11342 +
11343 +/*
11344 + * from the given @h_dentry, construct drinfo at @*fdata.
11345 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11346 + * @allocated.
11347 + */
11348 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11349 +                              struct dentry *h_dentry,
11350 +                              unsigned char *allocated)
11351 +{
11352 +       int err, v;
11353 +       struct au_drinfo_fdata *f, *p;
11354 +       struct au_drinfo *drinfo;
11355 +       struct inode *h_inode;
11356 +       struct qstr *qname;
11357 +
11358 +       err = 0;
11359 +       f = *fdata;
11360 +       h_inode = d_inode(h_dentry);
11361 +       qname = &h_dentry->d_name;
11362 +       drinfo = &f->drinfo;
11363 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11364 +       drinfo->oldnamelen = qname->len;
11365 +       if (*allocated < sizeof(*f) + qname->len) {
11366 +               v = roundup_pow_of_two(*allocated + qname->len);
11367 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11368 +               if (unlikely(!p)) {
11369 +                       err = -ENOMEM;
11370 +                       AuTraceErr(err);
11371 +                       goto out;
11372 +               }
11373 +               f = p;
11374 +               *fdata = f;
11375 +               *allocated = v;
11376 +               drinfo = &f->drinfo;
11377 +       }
11378 +       memcpy(drinfo->oldname, qname->name, qname->len);
11379 +       AuDbg("i%llu, %.*s\n",
11380 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11381 +             drinfo->oldname);
11382 +
11383 +out:
11384 +       AuTraceErr(err);
11385 +       return err;
11386 +}
11387 +
11388 +/* callers have to free the return value */
11389 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11390 +{
11391 +       struct au_drinfo *ret, *drinfo;
11392 +       struct au_drinfo_fdata fdata;
11393 +       int len;
11394 +       loff_t pos;
11395 +       ssize_t ssz;
11396 +
11397 +       ret = ERR_PTR(-EIO);
11398 +       pos = 0;
11399 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11400 +       if (unlikely(ssz != sizeof(fdata))) {
11401 +               AuIOErr("ssz %zd, %u, %pD2\n",
11402 +                       ssz, (unsigned int)sizeof(fdata), file);
11403 +               goto out;
11404 +       }
11405 +
11406 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11407 +       switch (fdata.magic) {
11408 +       case AUFS_DRINFO_MAGIC_V1:
11409 +               break;
11410 +       default:
11411 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11412 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11413 +               goto out;
11414 +       }
11415 +
11416 +       drinfo = &fdata.drinfo;
11417 +       len = drinfo->oldnamelen;
11418 +       if (!len) {
11419 +               AuIOErr("broken drinfo %pD2\n", file);
11420 +               goto out;
11421 +       }
11422 +
11423 +       ret = NULL;
11424 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11425 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11426 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11427 +                     (unsigned long long)drinfo->ino,
11428 +                     (unsigned long long)h_ino, file);
11429 +               goto out; /* success */
11430 +       }
11431 +
11432 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11433 +       if (unlikely(!ret)) {
11434 +               ret = ERR_PTR(-ENOMEM);
11435 +               AuTraceErrPtr(ret);
11436 +               goto out;
11437 +       }
11438 +
11439 +       *ret = *drinfo;
11440 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11441 +       if (unlikely(ssz != len)) {
11442 +               au_kfree_rcu(ret);
11443 +               ret = ERR_PTR(-EIO);
11444 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11445 +               goto out;
11446 +       }
11447 +
11448 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11449 +
11450 +out:
11451 +       return ret;
11452 +}
11453 +
11454 +/* ---------------------------------------------------------------------- */
11455 +
11456 +/* in order to be revertible */
11457 +struct au_drinfo_rev_elm {
11458 +       int                     created;
11459 +       struct dentry           *info_dentry;
11460 +       struct au_drinfo        *info_last;
11461 +};
11462 +
11463 +struct au_drinfo_rev {
11464 +       unsigned char                   already;
11465 +       aufs_bindex_t                   nelm;
11466 +       struct au_drinfo_rev_elm        elm[];
11467 +};
11468 +
11469 +/* todo: isn't it too large? */
11470 +struct au_drinfo_store {
11471 +       struct path h_ppath;
11472 +       struct dentry *h_dentry;
11473 +       struct au_drinfo_fdata *fdata;
11474 +       char *infoname;                 /* inside of whname, just after PFX */
11475 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11476 +       aufs_bindex_t btgt, btail;
11477 +       unsigned char no_sio,
11478 +               allocated,              /* current size of *fdata */
11479 +               infonamelen,            /* room size for p */
11480 +               whnamelen,              /* length of the generated name */
11481 +               renameback;             /* renamed back */
11482 +};
11483 +
11484 +/* on rename(2) error, the caller should revert it using @elm */
11485 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11486 +                             struct au_drinfo_rev_elm *elm)
11487 +{
11488 +       int err, len;
11489 +       ssize_t ssz;
11490 +       loff_t pos;
11491 +       struct path infopath = {
11492 +               .mnt = w->h_ppath.mnt
11493 +       };
11494 +       struct inode *h_dir, *h_inode, *delegated;
11495 +       struct file *infofile;
11496 +       struct qstr *qname;
11497 +
11498 +       AuDebugOn(elm
11499 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11500 +
11501 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11502 +                                              w->whnamelen);
11503 +       AuTraceErrPtr(infopath.dentry);
11504 +       if (IS_ERR(infopath.dentry)) {
11505 +               err = PTR_ERR(infopath.dentry);
11506 +               goto out;
11507 +       }
11508 +
11509 +       err = 0;
11510 +       h_dir = d_inode(w->h_ppath.dentry);
11511 +       if (elm && d_is_negative(infopath.dentry)) {
11512 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11513 +               AuTraceErr(err);
11514 +               if (unlikely(err))
11515 +                       goto out_dput;
11516 +               elm->created = 1;
11517 +               elm->info_dentry = dget(infopath.dentry);
11518 +       }
11519 +
11520 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11521 +       AuTraceErrPtr(infofile);
11522 +       if (IS_ERR(infofile)) {
11523 +               err = PTR_ERR(infofile);
11524 +               goto out_dput;
11525 +       }
11526 +
11527 +       h_inode = d_inode(infopath.dentry);
11528 +       if (elm && i_size_read(h_inode)) {
11529 +               h_inode = d_inode(w->h_dentry);
11530 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11531 +               AuTraceErrPtr(elm->info_last);
11532 +               if (IS_ERR(elm->info_last)) {
11533 +                       err = PTR_ERR(elm->info_last);
11534 +                       elm->info_last = NULL;
11535 +                       AuDebugOn(elm->info_dentry);
11536 +                       goto out_fput;
11537 +               }
11538 +       }
11539 +
11540 +       if (elm && w->renameback) {
11541 +               delegated = NULL;
11542 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11543 +               AuTraceErr(err);
11544 +               if (unlikely(err == -EWOULDBLOCK))
11545 +                       iput(delegated);
11546 +               goto out_fput;
11547 +       }
11548 +
11549 +       pos = 0;
11550 +       qname = &w->h_dentry->d_name;
11551 +       len = sizeof(*w->fdata) + qname->len;
11552 +       if (!elm)
11553 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11554 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11555 +       if (ssz == len) {
11556 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11557 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11558 +               goto out_fput; /* success */
11559 +       } else {
11560 +               err = -EIO;
11561 +               if (ssz < 0)
11562 +                       err = ssz;
11563 +               /* the caller should revert it using @elm */
11564 +       }
11565 +
11566 +out_fput:
11567 +       fput(infofile);
11568 +out_dput:
11569 +       dput(infopath.dentry);
11570 +out:
11571 +       AuTraceErr(err);
11572 +       return err;
11573 +}
11574 +
11575 +struct au_call_drinfo_do_store_args {
11576 +       int *errp;
11577 +       struct au_drinfo_store *w;
11578 +       struct au_drinfo_rev_elm *elm;
11579 +};
11580 +
11581 +static void au_call_drinfo_do_store(void *args)
11582 +{
11583 +       struct au_call_drinfo_do_store_args *a = args;
11584 +
11585 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11586 +}
11587 +
11588 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11589 +                              struct au_drinfo_rev_elm *elm)
11590 +{
11591 +       int err, wkq_err;
11592 +
11593 +       if (w->no_sio)
11594 +               err = au_drinfo_do_store(w, elm);
11595 +       else {
11596 +               struct au_call_drinfo_do_store_args a = {
11597 +                       .errp   = &err,
11598 +                       .w      = w,
11599 +                       .elm    = elm
11600 +               };
11601 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11602 +               if (unlikely(wkq_err))
11603 +                       err = wkq_err;
11604 +       }
11605 +       AuTraceErr(err);
11606 +
11607 +       return err;
11608 +}
11609 +
11610 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11611 +                                    aufs_bindex_t btgt)
11612 +{
11613 +       int err;
11614 +
11615 +       memset(w, 0, sizeof(*w));
11616 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11617 +       strscpy(w->whname, AUFS_WH_DR_INFO_PFX, sizeof(AUFS_WH_DR_INFO_PFX));
11618 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11619 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11620 +       w->btgt = btgt;
11621 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11622 +
11623 +       err = -ENOMEM;
11624 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11625 +       if (unlikely(!w->fdata)) {
11626 +               AuTraceErr(err);
11627 +               goto out;
11628 +       }
11629 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11630 +       err = 0;
11631 +
11632 +out:
11633 +       return err;
11634 +}
11635 +
11636 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11637 +{
11638 +       au_kfree_rcu(w->fdata);
11639 +}
11640 +
11641 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11642 +                               struct au_drinfo_store *w)
11643 +{
11644 +       struct au_drinfo_rev_elm *elm;
11645 +       struct inode *h_dir, *delegated;
11646 +       int err, nelm;
11647 +       struct path infopath = {
11648 +               .mnt = w->h_ppath.mnt
11649 +       };
11650 +
11651 +       h_dir = d_inode(w->h_ppath.dentry);
11652 +       IMustLock(h_dir);
11653 +
11654 +       err = 0;
11655 +       elm = rev->elm;
11656 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11657 +               AuDebugOn(elm->created && elm->info_last);
11658 +               if (elm->created) {
11659 +                       AuDbg("here\n");
11660 +                       delegated = NULL;
11661 +                       infopath.dentry = elm->info_dentry;
11662 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11663 +                                          !w->no_sio);
11664 +                       AuTraceErr(err);
11665 +                       if (unlikely(err == -EWOULDBLOCK))
11666 +                               iput(delegated);
11667 +                       dput(elm->info_dentry);
11668 +               } else if (elm->info_last) {
11669 +                       AuDbg("here\n");
11670 +                       w->fdata->drinfo = *elm->info_last;
11671 +                       memcpy(w->fdata->drinfo.oldname,
11672 +                              elm->info_last->oldname,
11673 +                              elm->info_last->oldnamelen);
11674 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11675 +                       au_kfree_rcu(elm->info_last);
11676 +               }
11677 +               if (unlikely(err))
11678 +                       AuIOErr("%d, %s\n", err, w->whname);
11679 +               /* go on even if err */
11680 +       }
11681 +}
11682 +
11683 +/* caller has to call au_dr_rename_fin() later */
11684 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11685 +                          struct qstr *dst_name, void *_rev)
11686 +{
11687 +       int err, sz, nelm;
11688 +       aufs_bindex_t bindex, btail;
11689 +       struct au_drinfo_store work;
11690 +       struct au_drinfo_rev *rev, **p;
11691 +       struct au_drinfo_rev_elm *elm;
11692 +       struct super_block *sb;
11693 +       struct au_branch *br;
11694 +       struct au_hinode *hdir;
11695 +
11696 +       err = au_drinfo_store_work_init(&work, btgt);
11697 +       AuTraceErr(err);
11698 +       if (unlikely(err))
11699 +               goto out;
11700 +
11701 +       err = -ENOMEM;
11702 +       btail = au_dbtaildir(dentry);
11703 +       nelm = btail - btgt;
11704 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11705 +       rev = kcalloc(1, sz, GFP_NOFS);
11706 +       if (unlikely(!rev)) {
11707 +               AuTraceErr(err);
11708 +               goto out_args;
11709 +       }
11710 +       rev->nelm = nelm;
11711 +       elm = rev->elm;
11712 +       p = _rev;
11713 +       *p = rev;
11714 +
11715 +       err = 0;
11716 +       sb = dentry->d_sb;
11717 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11718 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11719 +       hdir = au_hi(d_inode(dentry), btgt);
11720 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11721 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11722 +               work.h_dentry = au_h_dptr(dentry, bindex);
11723 +               if (!work.h_dentry)
11724 +                       continue;
11725 +
11726 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11727 +                                         &work.allocated);
11728 +               AuTraceErr(err);
11729 +               if (unlikely(err))
11730 +                       break;
11731 +
11732 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11733 +               br = au_sbr(sb, bindex);
11734 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11735 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11736 +                                                work.infonamelen);
11737 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11738 +                     work.whnamelen, work.whname,
11739 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11740 +                     work.fdata->drinfo.oldnamelen,
11741 +                     work.fdata->drinfo.oldname);
11742 +
11743 +               err = au_drinfo_store_sio(&work, elm);
11744 +               AuTraceErr(err);
11745 +               if (unlikely(err))
11746 +                       break;
11747 +       }
11748 +       if (unlikely(err)) {
11749 +               /* revert all drinfo */
11750 +               au_drinfo_store_rev(rev, &work);
11751 +               au_kfree_try_rcu(rev);
11752 +               *p = NULL;
11753 +       }
11754 +       au_hn_inode_unlock(hdir);
11755 +
11756 +out_args:
11757 +       au_drinfo_store_work_fin(&work);
11758 +out:
11759 +       return err;
11760 +}
11761 +
11762 +/* ---------------------------------------------------------------------- */
11763 +
11764 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11765 +                struct qstr *dst_name, void *_rev)
11766 +{
11767 +       int err, already;
11768 +       ino_t ino;
11769 +       struct super_block *sb;
11770 +       struct au_branch *br;
11771 +       struct au_dr_br *dr;
11772 +       struct dentry *h_dentry;
11773 +       struct inode *h_inode;
11774 +       struct au_dr_hino *ent;
11775 +       struct au_drinfo_rev *rev, **p;
11776 +
11777 +       AuDbg("bindex %d\n", bindex);
11778 +
11779 +       err = -ENOMEM;
11780 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11781 +       if (unlikely(!ent))
11782 +               goto out;
11783 +
11784 +       sb = src->d_sb;
11785 +       br = au_sbr(sb, bindex);
11786 +       dr = &br->br_dirren;
11787 +       h_dentry = au_h_dptr(src, bindex);
11788 +       h_inode = d_inode(h_dentry);
11789 +       ino = h_inode->i_ino;
11790 +       ent->dr_h_ino = ino;
11791 +       already = au_dr_hino_test_add(dr, ino, ent);
11792 +       AuDbg("b%d, hi%llu, already %d\n",
11793 +             bindex, (unsigned long long)ino, already);
11794 +
11795 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11796 +       AuTraceErr(err);
11797 +       if (!err) {
11798 +               p = _rev;
11799 +               rev = *p;
11800 +               rev->already = already;
11801 +               goto out; /* success */
11802 +       }
11803 +
11804 +       /* revert */
11805 +       if (!already)
11806 +               au_dr_hino_del(dr, ent);
11807 +       au_kfree_rcu(ent);
11808 +
11809 +out:
11810 +       AuTraceErr(err);
11811 +       return err;
11812 +}
11813 +
11814 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11815 +{
11816 +       struct au_drinfo_rev *rev;
11817 +       struct au_drinfo_rev_elm *elm;
11818 +       int nelm;
11819 +
11820 +       rev = _rev;
11821 +       elm = rev->elm;
11822 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11823 +               dput(elm->info_dentry);
11824 +               au_kfree_rcu(elm->info_last);
11825 +       }
11826 +       au_kfree_try_rcu(rev);
11827 +}
11828 +
11829 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11830 +{
11831 +       int err;
11832 +       struct au_drinfo_store work;
11833 +       struct au_drinfo_rev *rev = _rev;
11834 +       struct super_block *sb;
11835 +       struct au_branch *br;
11836 +       struct inode *h_inode;
11837 +       struct au_dr_br *dr;
11838 +       struct au_dr_hino *ent;
11839 +
11840 +       err = au_drinfo_store_work_init(&work, btgt);
11841 +       if (unlikely(err))
11842 +               goto out;
11843 +
11844 +       sb = src->d_sb;
11845 +       br = au_sbr(sb, btgt);
11846 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11847 +       work.h_ppath.mnt = au_br_mnt(br);
11848 +       au_drinfo_store_rev(rev, &work);
11849 +       au_drinfo_store_work_fin(&work);
11850 +       if (rev->already)
11851 +               goto out;
11852 +
11853 +       dr = &br->br_dirren;
11854 +       h_inode = d_inode(work.h_ppath.dentry);
11855 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11856 +       BUG_ON(!ent);
11857 +       au_dr_hino_del(dr, ent);
11858 +       au_kfree_rcu(ent);
11859 +
11860 +out:
11861 +       au_kfree_try_rcu(rev);
11862 +       if (unlikely(err))
11863 +               pr_err("failed to remove dirren info\n");
11864 +}
11865 +
11866 +/* ---------------------------------------------------------------------- */
11867 +
11868 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11869 +                                          char *whname, int whnamelen,
11870 +                                          struct dentry **info_dentry)
11871 +{
11872 +       struct au_drinfo *drinfo;
11873 +       struct file *f;
11874 +       struct inode *h_dir;
11875 +       struct path infopath;
11876 +       int unlocked;
11877 +
11878 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11879 +
11880 +       *info_dentry = NULL;
11881 +       drinfo = NULL;
11882 +       unlocked = 0;
11883 +       h_dir = d_inode(h_ppath->dentry);
11884 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11885 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11886 +       if (IS_ERR(infopath.dentry)) {
11887 +               drinfo = (void *)infopath.dentry;
11888 +               goto out;
11889 +       }
11890 +
11891 +       if (d_is_negative(infopath.dentry))
11892 +               goto out_dput; /* success */
11893 +
11894 +       infopath.mnt = h_ppath->mnt;
11895 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11896 +       inode_unlock_shared(h_dir);
11897 +       unlocked = 1;
11898 +       if (IS_ERR(f)) {
11899 +               drinfo = (void *)f;
11900 +               goto out_dput;
11901 +       }
11902 +
11903 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11904 +       if (IS_ERR_OR_NULL(drinfo))
11905 +               goto out_fput;
11906 +
11907 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11908 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11909 +
11910 +out_fput:
11911 +       fput(f);
11912 +out_dput:
11913 +       dput(infopath.dentry);
11914 +out:
11915 +       if (!unlocked)
11916 +               inode_unlock_shared(h_dir);
11917 +       AuTraceErrPtr(drinfo);
11918 +       return drinfo;
11919 +}
11920 +
11921 +struct au_drinfo_do_load_args {
11922 +       struct au_drinfo **drinfop;
11923 +       struct path *h_ppath;
11924 +       char *whname;
11925 +       int whnamelen;
11926 +       struct dentry **info_dentry;
11927 +};
11928 +
11929 +static void au_call_drinfo_do_load(void *args)
11930 +{
11931 +       struct au_drinfo_do_load_args *a = args;
11932 +
11933 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11934 +                                       a->info_dentry);
11935 +}
11936 +
11937 +struct au_drinfo_load {
11938 +       struct path h_ppath;
11939 +       struct qstr *qname;
11940 +       unsigned char no_sio;
11941 +
11942 +       aufs_bindex_t ninfo;
11943 +       struct au_drinfo **drinfo;
11944 +};
11945 +
11946 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11947 +                         struct au_branch *br)
11948 +{
11949 +       int err, wkq_err, whnamelen, e;
11950 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11951 +               = AUFS_WH_DR_INFO_PFX;
11952 +       struct au_drinfo *drinfo;
11953 +       struct qstr oldname;
11954 +       struct inode *h_dir, *delegated;
11955 +       struct dentry *info_dentry;
11956 +       struct path infopath;
11957 +
11958 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11959 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11960 +                                   sizeof(whname) - whnamelen);
11961 +       if (w->no_sio)
11962 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11963 +                                          &info_dentry);
11964 +       else {
11965 +               struct au_drinfo_do_load_args args = {
11966 +                       .drinfop        = &drinfo,
11967 +                       .h_ppath        = &w->h_ppath,
11968 +                       .whname         = whname,
11969 +                       .whnamelen      = whnamelen,
11970 +                       .info_dentry    = &info_dentry
11971 +               };
11972 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11973 +               if (unlikely(wkq_err))
11974 +                       drinfo = ERR_PTR(wkq_err);
11975 +       }
11976 +       err = PTR_ERR(drinfo);
11977 +       if (IS_ERR_OR_NULL(drinfo))
11978 +               goto out;
11979 +
11980 +       err = 0;
11981 +       oldname.len = drinfo->oldnamelen;
11982 +       oldname.name = drinfo->oldname;
11983 +       if (au_qstreq(w->qname, &oldname)) {
11984 +               /* the name is renamed back */
11985 +               au_kfree_rcu(drinfo);
11986 +               drinfo = NULL;
11987 +
11988 +               infopath.dentry = info_dentry;
11989 +               infopath.mnt = w->h_ppath.mnt;
11990 +               h_dir = d_inode(w->h_ppath.dentry);
11991 +               delegated = NULL;
11992 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11993 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11994 +               inode_unlock(h_dir);
11995 +               if (unlikely(e))
11996 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11997 +               if (unlikely(e == -EWOULDBLOCK))
11998 +                       iput(delegated);
11999 +       }
12000 +       au_kfree_rcu(w->drinfo[bindex]);
12001 +       w->drinfo[bindex] = drinfo;
12002 +       dput(info_dentry);
12003 +
12004 +out:
12005 +       AuTraceErr(err);
12006 +       return err;
12007 +}
12008 +
12009 +/* ---------------------------------------------------------------------- */
12010 +
12011 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12012 +{
12013 +       struct au_drinfo **p = drinfo;
12014 +
12015 +       while (n-- > 0)
12016 +               au_kfree_rcu(*drinfo++);
12017 +       au_kfree_try_rcu(p);
12018 +}
12019 +
12020 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12021 +              aufs_bindex_t btgt)
12022 +{
12023 +       int err, ninfo;
12024 +       struct au_drinfo_load w;
12025 +       aufs_bindex_t bindex, bbot;
12026 +       struct au_branch *br;
12027 +       struct inode *h_dir;
12028 +       struct au_dr_hino *ent;
12029 +       struct super_block *sb;
12030 +
12031 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12032 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12033 +             AuLNPair(&lkup->whname), btgt);
12034 +
12035 +       sb = dentry->d_sb;
12036 +       bbot = au_sbbot(sb);
12037 +       w.ninfo = bbot + 1;
12038 +       if (!lkup->dirren.drinfo) {
12039 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12040 +                                             sizeof(*lkup->dirren.drinfo),
12041 +                                             GFP_NOFS);
12042 +               if (unlikely(!lkup->dirren.drinfo)) {
12043 +                       err = -ENOMEM;
12044 +                       goto out;
12045 +               }
12046 +               lkup->dirren.ninfo = w.ninfo;
12047 +       }
12048 +       w.drinfo = lkup->dirren.drinfo;
12049 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12050 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12051 +       AuDebugOn(!w.h_ppath.dentry);
12052 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12053 +       w.qname = &dentry->d_name;
12054 +
12055 +       ninfo = 0;
12056 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12057 +               br = au_sbr(sb, bindex);
12058 +               err = au_drinfo_load(&w, bindex, br);
12059 +               if (unlikely(err))
12060 +                       goto out_free;
12061 +               if (w.drinfo[bindex])
12062 +                       ninfo++;
12063 +       }
12064 +       if (!ninfo) {
12065 +               br = au_sbr(sb, btgt);
12066 +               h_dir = d_inode(w.h_ppath.dentry);
12067 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12068 +               AuDebugOn(!ent);
12069 +               au_dr_hino_del(&br->br_dirren, ent);
12070 +               au_kfree_rcu(ent);
12071 +       }
12072 +       goto out; /* success */
12073 +
12074 +out_free:
12075 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12076 +       lkup->dirren.ninfo = 0;
12077 +       lkup->dirren.drinfo = NULL;
12078 +out:
12079 +       AuTraceErr(err);
12080 +       return err;
12081 +}
12082 +
12083 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12084 +{
12085 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12086 +}
12087 +
12088 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12089 +{
12090 +       int err;
12091 +       struct au_drinfo *drinfo;
12092 +
12093 +       err = 0;
12094 +       if (!lkup->dirren.drinfo)
12095 +               goto out;
12096 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12097 +       drinfo = lkup->dirren.drinfo[btgt];
12098 +       if (!drinfo)
12099 +               goto out;
12100 +
12101 +       au_kfree_try_rcu(lkup->whname.name);
12102 +       lkup->whname.name = NULL;
12103 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12104 +       lkup->dirren.dr_name.name = drinfo->oldname;
12105 +       lkup->name = &lkup->dirren.dr_name;
12106 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12107 +       if (!err)
12108 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12109 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12110 +                     btgt);
12111 +
12112 +out:
12113 +       AuTraceErr(err);
12114 +       return err;
12115 +}
12116 +
12117 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12118 +                    ino_t h_ino)
12119 +{
12120 +       int match;
12121 +       struct au_drinfo *drinfo;
12122 +
12123 +       match = 1;
12124 +       if (!lkup->dirren.drinfo)
12125 +               goto out;
12126 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12127 +       drinfo = lkup->dirren.drinfo[bindex];
12128 +       if (!drinfo)
12129 +               goto out;
12130 +
12131 +       match = (drinfo->ino == h_ino);
12132 +       AuDbg("match %d\n", match);
12133 +
12134 +out:
12135 +       return match;
12136 +}
12137 +
12138 +/* ---------------------------------------------------------------------- */
12139 +
12140 +int au_dr_opt_set(struct super_block *sb)
12141 +{
12142 +       int err;
12143 +       aufs_bindex_t bindex, bbot;
12144 +       struct au_branch *br;
12145 +
12146 +       err = 0;
12147 +       bbot = au_sbbot(sb);
12148 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12149 +               br = au_sbr(sb, bindex);
12150 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12151 +       }
12152 +
12153 +       return err;
12154 +}
12155 +
12156 +int au_dr_opt_flush(struct super_block *sb)
12157 +{
12158 +       int err;
12159 +       aufs_bindex_t bindex, bbot;
12160 +       struct au_branch *br;
12161 +
12162 +       err = 0;
12163 +       bbot = au_sbbot(sb);
12164 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12165 +               br = au_sbr(sb, bindex);
12166 +               if (au_br_writable(br->br_perm))
12167 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12168 +       }
12169 +
12170 +       return err;
12171 +}
12172 +
12173 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12174 +{
12175 +       int err;
12176 +       aufs_bindex_t bindex, bbot;
12177 +       struct au_branch *br;
12178 +
12179 +       err = 0;
12180 +       if (!no_flush) {
12181 +               err = au_dr_opt_flush(sb);
12182 +               if (unlikely(err))
12183 +                       goto out;
12184 +       }
12185 +
12186 +       bbot = au_sbbot(sb);
12187 +       for (bindex = 0; bindex <= bbot; bindex++) {
12188 +               br = au_sbr(sb, bindex);
12189 +               au_dr_hino_free(&br->br_dirren);
12190 +       }
12191 +
12192 +out:
12193 +       return err;
12194 +}
12195 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12196 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12197 +++ linux/fs/aufs/dirren.h      2024-05-13 17:13:00.618070924 +0200
12198 @@ -0,0 +1,140 @@
12199 +/* SPDX-License-Identifier: GPL-2.0 */
12200 +/*
12201 + * Copyright (C) 2017-2022 Junjiro R. Okajima
12202 + *
12203 + * This program is free software; you can redistribute it and/or modify
12204 + * it under the terms of the GNU General Public License as published by
12205 + * the Free Software Foundation; either version 2 of the License, or
12206 + * (at your option) any later version.
12207 + *
12208 + * This program is distributed in the hope that it will be useful,
12209 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12210 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12211 + * GNU General Public License for more details.
12212 + *
12213 + * You should have received a copy of the GNU General Public License
12214 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12215 + */
12216 +
12217 +/*
12218 + * renamed dir info
12219 + */
12220 +
12221 +#ifndef __AUFS_DIRREN_H__
12222 +#define __AUFS_DIRREN_H__
12223 +
12224 +#ifdef __KERNEL__
12225 +
12226 +#include <linux/dcache.h>
12227 +#include <linux/statfs.h>
12228 +#include <linux/uuid.h>
12229 +#include "hbl.h"
12230 +
12231 +#define AuDirren_NHASH 100
12232 +
12233 +#ifdef CONFIG_AUFS_DIRREN
12234 +enum au_brid_type {
12235 +       AuBrid_Unset,
12236 +       AuBrid_UUID,
12237 +       AuBrid_FSID,
12238 +       AuBrid_DEV
12239 +};
12240 +
12241 +struct au_dr_brid {
12242 +       enum au_brid_type       type;
12243 +       union {
12244 +               uuid_t  uuid;   /* unimplemented yet */
12245 +               fsid_t  fsid;
12246 +               dev_t   dev;
12247 +       };
12248 +};
12249 +
12250 +/* 20 is the max digits length of ulong 64 */
12251 +/* brid-type "_" uuid "_" inum */
12252 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12253 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12254 +
12255 +struct au_dr_hino {
12256 +       struct hlist_bl_node    dr_hnode;
12257 +       ino_t                   dr_h_ino;
12258 +};
12259 +
12260 +struct au_dr_br {
12261 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12262 +       struct au_dr_brid       dr_brid;
12263 +};
12264 +
12265 +struct au_dr_lookup {
12266 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12267 +       struct qstr             dr_name; /* subset of dr_info */
12268 +       aufs_bindex_t           ninfo;
12269 +       struct au_drinfo        **drinfo;
12270 +};
12271 +#else
12272 +struct au_dr_hino;
12273 +/* empty */
12274 +struct au_dr_br { };
12275 +struct au_dr_lookup { };
12276 +#endif
12277 +
12278 +/* ---------------------------------------------------------------------- */
12279 +
12280 +struct au_branch;
12281 +struct au_do_lookup_args;
12282 +struct au_hinode;
12283 +#ifdef CONFIG_AUFS_DIRREN
12284 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12285 +                       struct au_dr_hino *add_ent);
12286 +void au_dr_hino_free(struct au_dr_br *dr);
12287 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12288 +                 const struct path *path);
12289 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12290 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12291 +                struct qstr *dst_name, void *_rev);
12292 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12293 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12294 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12295 +              aufs_bindex_t bindex);
12296 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12297 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12298 +                    ino_t h_ino);
12299 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12300 +int au_dr_opt_set(struct super_block *sb);
12301 +int au_dr_opt_flush(struct super_block *sb);
12302 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12303 +#else
12304 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12305 +          struct au_dr_hino *add_ent);
12306 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12307 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12308 +          const struct path *path);
12309 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12310 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12311 +          struct qstr *dst_name, void *_rev);
12312 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12313 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12314 +          void *rev);
12315 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12316 +          aufs_bindex_t bindex);
12317 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12318 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12319 +          aufs_bindex_t bindex, ino_t h_ino);
12320 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12321 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12322 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12323 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12324 +#endif
12325 +
12326 +/* ---------------------------------------------------------------------- */
12327 +
12328 +#ifdef CONFIG_AUFS_DIRREN
12329 +static inline int au_dr_ihash(ino_t h_ino)
12330 +{
12331 +       return h_ino % AuDirren_NHASH;
12332 +}
12333 +#else
12334 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12335 +#endif
12336 +
12337 +#endif /* __KERNEL__ */
12338 +#endif /* __AUFS_DIRREN_H__ */
12339 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12340 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12341 +++ linux/fs/aufs/dynop.c       2024-05-13 17:13:00.618070924 +0200
12342 @@ -0,0 +1,366 @@
12343 +// SPDX-License-Identifier: GPL-2.0
12344 +/*
12345 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12346 + *
12347 + * This program is free software; you can redistribute it and/or modify
12348 + * it under the terms of the GNU General Public License as published by
12349 + * the Free Software Foundation; either version 2 of the License, or
12350 + * (at your option) any later version.
12351 + *
12352 + * This program is distributed in the hope that it will be useful,
12353 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12354 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12355 + * GNU General Public License for more details.
12356 + *
12357 + * You should have received a copy of the GNU General Public License
12358 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12359 + */
12360 +
12361 +/*
12362 + * dynamically customizable operations for regular files
12363 + */
12364 +
12365 +#include "aufs.h"
12366 +
12367 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12368 +
12369 +/*
12370 + * How large will these lists be?
12371 + * Usually just a few elements, 20-30 at most for each, I guess.
12372 + */
12373 +static struct hlist_bl_head dynop[AuDyLast];
12374 +
12375 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12376 +                                    const void *h_op)
12377 +{
12378 +       struct au_dykey *key, *tmp;
12379 +       struct hlist_bl_node *pos;
12380 +
12381 +       key = NULL;
12382 +       hlist_bl_lock(hbl);
12383 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12384 +               if (tmp->dk_op.dy_hop == h_op) {
12385 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12386 +                               key = tmp;
12387 +                       break;
12388 +               }
12389 +       hlist_bl_unlock(hbl);
12390 +
12391 +       return key;
12392 +}
12393 +
12394 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12395 +{
12396 +       struct au_dykey **k, *found;
12397 +       const void *h_op = key->dk_op.dy_hop;
12398 +       int i;
12399 +
12400 +       found = NULL;
12401 +       k = br->br_dykey;
12402 +       for (i = 0; i < AuBrDynOp; i++)
12403 +               if (k[i]) {
12404 +                       if (k[i]->dk_op.dy_hop == h_op) {
12405 +                               found = k[i];
12406 +                               break;
12407 +                       }
12408 +               } else
12409 +                       break;
12410 +       if (!found) {
12411 +               spin_lock(&br->br_dykey_lock);
12412 +               for (; i < AuBrDynOp; i++)
12413 +                       if (k[i]) {
12414 +                               if (k[i]->dk_op.dy_hop == h_op) {
12415 +                                       found = k[i];
12416 +                                       break;
12417 +                               }
12418 +                       } else {
12419 +                               k[i] = key;
12420 +                               break;
12421 +                       }
12422 +               spin_unlock(&br->br_dykey_lock);
12423 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12424 +       }
12425 +
12426 +       return found;
12427 +}
12428 +
12429 +/* kref_get() if @key is already added */
12430 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12431 +{
12432 +       struct au_dykey *tmp, *found;
12433 +       struct hlist_bl_node *pos;
12434 +       const void *h_op = key->dk_op.dy_hop;
12435 +
12436 +       found = NULL;
12437 +       hlist_bl_lock(hbl);
12438 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12439 +               if (tmp->dk_op.dy_hop == h_op) {
12440 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12441 +                               found = tmp;
12442 +                       break;
12443 +               }
12444 +       if (!found)
12445 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12446 +       hlist_bl_unlock(hbl);
12447 +
12448 +       if (!found)
12449 +               DyPrSym(key);
12450 +       return found;
12451 +}
12452 +
12453 +static void dy_free_rcu(struct rcu_head *rcu)
12454 +{
12455 +       struct au_dykey *key;
12456 +
12457 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12458 +       DyPrSym(key);
12459 +       kfree(key);
12460 +}
12461 +
12462 +static void dy_free(struct kref *kref)
12463 +{
12464 +       struct au_dykey *key;
12465 +       struct hlist_bl_head *hbl;
12466 +
12467 +       key = container_of(kref, struct au_dykey, dk_kref);
12468 +       hbl = dynop + key->dk_op.dy_type;
12469 +       au_hbl_del(&key->dk_hnode, hbl);
12470 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12471 +}
12472 +
12473 +void au_dy_put(struct au_dykey *key)
12474 +{
12475 +       kref_put(&key->dk_kref, dy_free);
12476 +}
12477 +
12478 +/* ---------------------------------------------------------------------- */
12479 +
12480 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12481 +
12482 +#ifdef CONFIG_AUFS_DEBUG
12483 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12484 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12485 +#else
12486 +#define DyDbgDeclare(cnt)      do {} while (0)
12487 +#define DyDbgInc(cnt)          do {} while (0)
12488 +#endif
12489 +
12490 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12491 +       DyDbgInc(cnt);                                                  \
12492 +       if (h_op->func) {                                               \
12493 +               if (src.func)                                           \
12494 +                       dst.func = src.func;                            \
12495 +               else                                                    \
12496 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12497 +       }                                                               \
12498 +} while (0)
12499 +
12500 +#define DySetForce(func, dst, src) do {                \
12501 +       AuDebugOn(!src.func);                   \
12502 +       DyDbgInc(cnt);                          \
12503 +       dst.func = src.func;                    \
12504 +} while (0)
12505 +
12506 +#define DySetAop(func) \
12507 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12508 +#define DySetAopForce(func) \
12509 +       DySetForce(func, dyaop->da_op, aufs_aop)
12510 +
12511 +static void dy_aop(struct au_dykey *key, const void *h_op,
12512 +                  struct super_block *h_sb __maybe_unused)
12513 +{
12514 +       struct au_dyaop *dyaop = (void *)key;
12515 +       const struct address_space_operations *h_aop = h_op;
12516 +       DyDbgDeclare(cnt);
12517 +
12518 +       AuDbg("%s\n", au_sbtype(h_sb));
12519 +
12520 +       DySetAop(writepage);
12521 +       DySetAopForce(read_folio);      /* force */
12522 +       DySetAop(writepages);
12523 +       DySetAop(dirty_folio);
12524 +       DySetAop(invalidate_folio);
12525 +       DySetAop(readahead);
12526 +       DySetAop(write_begin);
12527 +       DySetAop(write_end);
12528 +       DySetAop(bmap);
12529 +       DySetAop(release_folio);
12530 +       DySetAop(free_folio);
12531 +       /* this one will be changed according to an aufs mount option */
12532 +       DySetAop(direct_IO);
12533 +       DySetAop(migrate_folio);
12534 +       DySetAop(launder_folio);
12535 +       DySetAop(is_partially_uptodate);
12536 +       DySetAop(is_dirty_writeback);
12537 +       DySetAop(error_remove_folio);
12538 +       DySetAop(swap_activate);
12539 +       DySetAop(swap_deactivate);
12540 +       DySetAop(swap_rw);
12541 +
12542 +       DyDbgSize(cnt, *h_aop);
12543 +}
12544 +
12545 +/* ---------------------------------------------------------------------- */
12546 +
12547 +static void dy_bug(struct kref *kref)
12548 +{
12549 +       BUG();
12550 +}
12551 +
12552 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12553 +{
12554 +       struct au_dykey *key, *old;
12555 +       struct hlist_bl_head *hbl;
12556 +       struct op {
12557 +               unsigned int sz;
12558 +               void (*set)(struct au_dykey *key, const void *h_op,
12559 +                           struct super_block *h_sb __maybe_unused);
12560 +       };
12561 +       static const struct op a[] = {
12562 +               [AuDy_AOP] = {
12563 +                       .sz     = sizeof(struct au_dyaop),
12564 +                       .set    = dy_aop
12565 +               }
12566 +       };
12567 +       const struct op *p;
12568 +
12569 +       hbl = dynop + op->dy_type;
12570 +       key = dy_gfind_get(hbl, op->dy_hop);
12571 +       if (key)
12572 +               goto out_add; /* success */
12573 +
12574 +       p = a + op->dy_type;
12575 +       key = kzalloc(p->sz, GFP_NOFS);
12576 +       if (unlikely(!key)) {
12577 +               key = ERR_PTR(-ENOMEM);
12578 +               goto out;
12579 +       }
12580 +
12581 +       key->dk_op.dy_hop = op->dy_hop;
12582 +       kref_init(&key->dk_kref);
12583 +       p->set(key, op->dy_hop, au_br_sb(br));
12584 +       old = dy_gadd(hbl, key);
12585 +       if (old) {
12586 +               au_kfree_rcu(key);
12587 +               key = old;
12588 +       }
12589 +
12590 +out_add:
12591 +       old = dy_bradd(br, key);
12592 +       if (old)
12593 +               /* its ref-count should never be zero here */
12594 +               kref_put(&key->dk_kref, dy_bug);
12595 +out:
12596 +       return key;
12597 +}
12598 +
12599 +/* ---------------------------------------------------------------------- */
12600 +/*
12601 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12602 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12603 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12604 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12605 + * See the aufs manual in detail.
12606 + */
12607 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12608 +{
12609 +       if (!do_dx)
12610 +               dyaop->da_op.direct_IO = NULL;
12611 +       else
12612 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12613 +}
12614 +
12615 +static struct au_dyaop *dy_aget(struct au_branch *br,
12616 +                               const struct address_space_operations *h_aop,
12617 +                               int do_dx)
12618 +{
12619 +       struct au_dyaop *dyaop;
12620 +       struct au_dynop op;
12621 +
12622 +       op.dy_type = AuDy_AOP;
12623 +       op.dy_haop = h_aop;
12624 +       dyaop = (void *)dy_get(&op, br);
12625 +       if (IS_ERR(dyaop))
12626 +               goto out;
12627 +       dy_adx(dyaop, do_dx);
12628 +
12629 +out:
12630 +       return dyaop;
12631 +}
12632 +
12633 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12634 +               struct inode *h_inode)
12635 +{
12636 +       int err, do_dx;
12637 +       struct super_block *sb;
12638 +       struct au_branch *br;
12639 +       struct au_dyaop *dyaop;
12640 +
12641 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12642 +       IiMustWriteLock(inode);
12643 +
12644 +       sb = inode->i_sb;
12645 +       br = au_sbr(sb, bindex);
12646 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12647 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12648 +       err = PTR_ERR(dyaop);
12649 +       if (IS_ERR(dyaop))
12650 +               /* unnecessary to call dy_fput() */
12651 +               goto out;
12652 +
12653 +       err = 0;
12654 +       inode->i_mapping->a_ops = &dyaop->da_op;
12655 +
12656 +out:
12657 +       return err;
12658 +}
12659 +
12660 +/*
12661 + * Is it safe to replace a_ops during the inode/file is in operation?
12662 + * Yes, I hope so.
12663 + */
12664 +int au_dy_irefresh(struct inode *inode)
12665 +{
12666 +       int err;
12667 +       aufs_bindex_t btop;
12668 +       struct inode *h_inode;
12669 +
12670 +       err = 0;
12671 +       if (S_ISREG(inode->i_mode)) {
12672 +               btop = au_ibtop(inode);
12673 +               h_inode = au_h_iptr(inode, btop);
12674 +               err = au_dy_iaop(inode, btop, h_inode);
12675 +       }
12676 +       return err;
12677 +}
12678 +
12679 +void au_dy_arefresh(int do_dx)
12680 +{
12681 +       struct hlist_bl_head *hbl;
12682 +       struct hlist_bl_node *pos;
12683 +       struct au_dykey *key;
12684 +
12685 +       hbl = dynop + AuDy_AOP;
12686 +       hlist_bl_lock(hbl);
12687 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12688 +               dy_adx((void *)key, do_dx);
12689 +       hlist_bl_unlock(hbl);
12690 +}
12691 +
12692 +/* ---------------------------------------------------------------------- */
12693 +
12694 +void __init au_dy_init(void)
12695 +{
12696 +       int i;
12697 +
12698 +       for (i = 0; i < AuDyLast; i++)
12699 +               INIT_HLIST_BL_HEAD(dynop + i);
12700 +}
12701 +
12702 +void au_dy_fin(void)
12703 +{
12704 +       int i;
12705 +
12706 +       for (i = 0; i < AuDyLast; i++)
12707 +               WARN_ON(!hlist_bl_empty(dynop + i));
12708 +}
12709 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12710 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12711 +++ linux/fs/aufs/dynop.h       2024-05-13 17:13:00.618070924 +0200
12712 @@ -0,0 +1,77 @@
12713 +/* SPDX-License-Identifier: GPL-2.0 */
12714 +/*
12715 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12716 + *
12717 + * This program is free software; you can redistribute it and/or modify
12718 + * it under the terms of the GNU General Public License as published by
12719 + * the Free Software Foundation; either version 2 of the License, or
12720 + * (at your option) any later version.
12721 + *
12722 + * This program is distributed in the hope that it will be useful,
12723 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12724 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12725 + * GNU General Public License for more details.
12726 + *
12727 + * You should have received a copy of the GNU General Public License
12728 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12729 + */
12730 +
12731 +/*
12732 + * dynamically customizable operations (for regular files only)
12733 + */
12734 +
12735 +#ifndef __AUFS_DYNOP_H__
12736 +#define __AUFS_DYNOP_H__
12737 +
12738 +#ifdef __KERNEL__
12739 +
12740 +#include <linux/fs.h>
12741 +#include <linux/kref.h>
12742 +
12743 +enum {AuDy_AOP, AuDyLast};
12744 +
12745 +struct au_dynop {
12746 +       int                                             dy_type;
12747 +       union {
12748 +               const void                              *dy_hop;
12749 +               const struct address_space_operations   *dy_haop;
12750 +       };
12751 +};
12752 +
12753 +struct au_dykey {
12754 +       union {
12755 +               struct hlist_bl_node    dk_hnode;
12756 +               struct rcu_head         dk_rcu;
12757 +       };
12758 +       struct au_dynop         dk_op;
12759 +
12760 +       /*
12761 +        * during I am in the branch local array, kref is gotten. when the
12762 +        * branch is removed, kref is put.
12763 +        */
12764 +       struct kref             dk_kref;
12765 +};
12766 +
12767 +/* stop unioning since their sizes are very different from each other */
12768 +struct au_dyaop {
12769 +       struct au_dykey                 da_key;
12770 +       struct address_space_operations da_op; /* not const */
12771 +};
12772 +/* make sure that 'struct au_dykey *' can be any type */
12773 +static_assert(!offsetof(struct au_dyaop, da_key));
12774 +
12775 +/* ---------------------------------------------------------------------- */
12776 +
12777 +/* dynop.c */
12778 +struct au_branch;
12779 +void au_dy_put(struct au_dykey *key);
12780 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12781 +               struct inode *h_inode);
12782 +int au_dy_irefresh(struct inode *inode);
12783 +void au_dy_arefresh(int do_dio);
12784 +
12785 +void __init au_dy_init(void);
12786 +void au_dy_fin(void);
12787 +
12788 +#endif /* __KERNEL__ */
12789 +#endif /* __AUFS_DYNOP_H__ */
12790 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12791 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12792 +++ linux/fs/aufs/export.c      2024-05-13 17:13:00.618070924 +0200
12793 @@ -0,0 +1,846 @@
12794 +// SPDX-License-Identifier: GPL-2.0
12795 +/*
12796 + * Copyright (C) 2005-2022 Junjiro R. Okajima
12797 + *
12798 + * This program is free software; you can redistribute it and/or modify
12799 + * it under the terms of the GNU General Public License as published by
12800 + * the Free Software Foundation; either version 2 of the License, or
12801 + * (at your option) any later version.
12802 + *
12803 + * This program is distributed in the hope that it will be useful,
12804 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12805 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12806 + * GNU General Public License for more details.
12807 + *
12808 + * You should have received a copy of the GNU General Public License
12809 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12810 + */
12811 +
12812 +/*
12813 + * export via nfs
12814 + */
12815 +
12816 +#include <linux/exportfs.h>
12817 +#include <linux/fs_struct.h>
12818 +#include <linux/nsproxy.h>
12819 +#include <linux/random.h>
12820 +#include <linux/writeback.h>
12821 +#include "aufs.h"
12822 +
12823 +union conv {
12824 +#ifdef CONFIG_AUFS_INO_T_64
12825 +       __u32 a[2];
12826 +#else
12827 +       __u32 a[1];
12828 +#endif
12829 +       ino_t ino;
12830 +};
12831 +
12832 +static ino_t decode_ino(__u32 *a)
12833 +{
12834 +       union conv u;
12835 +
12836 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12837 +       u.a[0] = a[0];
12838 +#ifdef CONFIG_AUFS_INO_T_64
12839 +       u.a[1] = a[1];
12840 +#endif
12841 +       return u.ino;
12842 +}
12843 +
12844 +static void encode_ino(__u32 *a, ino_t ino)
12845 +{
12846 +       union conv u;
12847 +
12848 +       u.ino = ino;
12849 +       a[0] = u.a[0];
12850 +#ifdef CONFIG_AUFS_INO_T_64
12851 +       a[1] = u.a[1];
12852 +#endif
12853 +}
12854 +
12855 +/* NFS file handle */
12856 +enum {
12857 +       Fh_br_id,
12858 +       Fh_sigen,
12859 +#ifdef CONFIG_AUFS_INO_T_64
12860 +       /* support 64bit inode number */
12861 +       Fh_ino1,
12862 +       Fh_ino2,
12863 +       Fh_dir_ino1,
12864 +       Fh_dir_ino2,
12865 +#else
12866 +       Fh_ino1,
12867 +       Fh_dir_ino1,
12868 +#endif
12869 +       Fh_igen,
12870 +       Fh_h_type,
12871 +       Fh_tail,
12872 +
12873 +       Fh_ino = Fh_ino1,
12874 +       Fh_dir_ino = Fh_dir_ino1
12875 +};
12876 +
12877 +static int au_test_anon(struct dentry *dentry)
12878 +{
12879 +       /* note: read d_flags without d_lock */
12880 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12881 +}
12882 +
12883 +int au_test_nfsd(void)
12884 +{
12885 +       int ret;
12886 +       struct task_struct *tsk = current;
12887 +       char comm[sizeof(tsk->comm)];
12888 +
12889 +       ret = 0;
12890 +       if (tsk->flags & PF_KTHREAD) {
12891 +               get_task_comm(comm, tsk);
12892 +               ret = !strcmp(comm, "nfsd");
12893 +       }
12894 +
12895 +       return ret;
12896 +}
12897 +
12898 +/* ---------------------------------------------------------------------- */
12899 +/* inode generation external table */
12900 +
12901 +void au_xigen_inc(struct inode *inode)
12902 +{
12903 +       loff_t pos;
12904 +       ssize_t sz;
12905 +       __u32 igen;
12906 +       struct super_block *sb;
12907 +       struct au_sbinfo *sbinfo;
12908 +
12909 +       sb = inode->i_sb;
12910 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12911 +
12912 +       sbinfo = au_sbi(sb);
12913 +       pos = inode->i_ino;
12914 +       pos *= sizeof(igen);
12915 +       igen = inode->i_generation + 1;
12916 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12917 +       if (sz == sizeof(igen))
12918 +               return; /* success */
12919 +
12920 +       if (unlikely(sz >= 0))
12921 +               AuIOErr("xigen error (%zd)\n", sz);
12922 +}
12923 +
12924 +int au_xigen_new(struct inode *inode)
12925 +{
12926 +       int err;
12927 +       loff_t pos;
12928 +       ssize_t sz;
12929 +       struct super_block *sb;
12930 +       struct au_sbinfo *sbinfo;
12931 +       struct file *file;
12932 +
12933 +       err = 0;
12934 +       /* todo: dirty, at mount time */
12935 +       if (inode->i_ino == AUFS_ROOT_INO)
12936 +               goto out;
12937 +       sb = inode->i_sb;
12938 +       SiMustAnyLock(sb);
12939 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12940 +               goto out;
12941 +
12942 +       err = -EFBIG;
12943 +       pos = inode->i_ino;
12944 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12945 +               AuIOErr1("too large i%lld\n", pos);
12946 +               goto out;
12947 +       }
12948 +       pos *= sizeof(inode->i_generation);
12949 +
12950 +       err = 0;
12951 +       sbinfo = au_sbi(sb);
12952 +       file = sbinfo->si_xigen;
12953 +       BUG_ON(!file);
12954 +
12955 +       if (vfsub_f_size_read(file)
12956 +           < pos + sizeof(inode->i_generation)) {
12957 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12958 +               sz = xino_fwrite(file, &inode->i_generation,
12959 +                                sizeof(inode->i_generation), &pos);
12960 +       } else
12961 +               sz = xino_fread(file, &inode->i_generation,
12962 +                               sizeof(inode->i_generation), &pos);
12963 +       if (sz == sizeof(inode->i_generation))
12964 +               goto out; /* success */
12965 +
12966 +       err = sz;
12967 +       if (unlikely(sz >= 0)) {
12968 +               err = -EIO;
12969 +               AuIOErr("xigen error (%zd)\n", sz);
12970 +       }
12971 +
12972 +out:
12973 +       return err;
12974 +}
12975 +
12976 +int au_xigen_set(struct super_block *sb, struct path *path)
12977 +{
12978 +       int err;
12979 +       struct au_sbinfo *sbinfo;
12980 +       struct file *file;
12981 +
12982 +       SiMustWriteLock(sb);
12983 +
12984 +       sbinfo = au_sbi(sb);
12985 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12986 +       err = PTR_ERR(file);
12987 +       if (IS_ERR(file))
12988 +               goto out;
12989 +       err = 0;
12990 +       if (sbinfo->si_xigen)
12991 +               fput(sbinfo->si_xigen);
12992 +       sbinfo->si_xigen = file;
12993 +
12994 +out:
12995 +       AuTraceErr(err);
12996 +       return err;
12997 +}
12998 +
12999 +void au_xigen_clr(struct super_block *sb)
13000 +{
13001 +       struct au_sbinfo *sbinfo;
13002 +
13003 +       SiMustWriteLock(sb);
13004 +
13005 +       sbinfo = au_sbi(sb);
13006 +       if (sbinfo->si_xigen) {
13007 +               fput(sbinfo->si_xigen);
13008 +               sbinfo->si_xigen = NULL;
13009 +       }
13010 +}
13011 +
13012 +/* ---------------------------------------------------------------------- */
13013 +
13014 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13015 +                                   ino_t dir_ino)
13016 +{
13017 +       struct dentry *dentry, *d;
13018 +       struct inode *inode;
13019 +       unsigned int sigen;
13020 +
13021 +       dentry = NULL;
13022 +       inode = ilookup(sb, ino);
13023 +       if (!inode)
13024 +               goto out;
13025 +
13026 +       dentry = ERR_PTR(-ESTALE);
13027 +       sigen = au_sigen(sb);
13028 +       if (unlikely(au_is_bad_inode(inode)
13029 +                    || IS_DEADDIR(inode)
13030 +                    || sigen != au_iigen(inode, NULL)))
13031 +               goto out_iput;
13032 +
13033 +       dentry = NULL;
13034 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13035 +               dentry = d_find_alias(inode);
13036 +       else {
13037 +               spin_lock(&inode->i_lock);
13038 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13039 +                       spin_lock(&d->d_lock);
13040 +                       if (!au_test_anon(d)
13041 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13042 +                               dentry = dget_dlock(d);
13043 +                               spin_unlock(&d->d_lock);
13044 +                               break;
13045 +                       }
13046 +                       spin_unlock(&d->d_lock);
13047 +               }
13048 +               spin_unlock(&inode->i_lock);
13049 +       }
13050 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13051 +               /* need to refresh */
13052 +               dput(dentry);
13053 +               dentry = NULL;
13054 +       }
13055 +
13056 +out_iput:
13057 +       iput(inode);
13058 +out:
13059 +       AuTraceErrPtr(dentry);
13060 +       return dentry;
13061 +}
13062 +
13063 +/* ---------------------------------------------------------------------- */
13064 +
13065 +/* todo: dirty? */
13066 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13067 +
13068 +struct au_compare_mnt_args {
13069 +       /* input */
13070 +       struct super_block *sb;
13071 +
13072 +       /* output */
13073 +       struct vfsmount *mnt;
13074 +};
13075 +
13076 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13077 +{
13078 +       struct au_compare_mnt_args *a = arg;
13079 +
13080 +       if (mnt->mnt_sb != a->sb)
13081 +               return 0;
13082 +       a->mnt = mntget(mnt);
13083 +       return 1;
13084 +}
13085 +
13086 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13087 +{
13088 +       int err;
13089 +       struct path root;
13090 +       struct vfsmount *mnt;
13091 +       struct au_compare_mnt_args args = {
13092 +               .sb = sb
13093 +       };
13094 +
13095 +       get_fs_root(current->fs, &root);
13096 +       /*
13097 +        * as long as this sb is alive, this temporary unlock is safe.
13098 +        * Really?
13099 +        */
13100 +       si_read_unlock(sb);
13101 +       mnt = collect_mounts(&root);
13102 +       if (IS_ERR(mnt)) {
13103 +               args.mnt = mnt;
13104 +               goto out;
13105 +       }
13106 +
13107 +       rcu_read_lock();
13108 +       err = iterate_mounts(au_compare_mnt, &args, mnt);
13109 +       rcu_read_unlock();
13110 +       drop_collected_mounts(mnt);
13111 +       AuDebugOn(!err);
13112 +
13113 +out:
13114 +       si_noflush_read_lock(sb);
13115 +       AuDebugOn(!args.mnt);
13116 +       path_put(&root);
13117 +       return args.mnt;
13118 +}
13119 +
13120 +struct au_nfsd_si_lock {
13121 +       unsigned int sigen;
13122 +       aufs_bindex_t bindex, br_id;
13123 +       unsigned char force_lock;
13124 +};
13125 +
13126 +static int si_nfsd_read_lock(struct super_block *sb,
13127 +                            struct au_nfsd_si_lock *nsi_lock)
13128 +{
13129 +       int err;
13130 +       aufs_bindex_t bindex;
13131 +
13132 +       si_read_lock(sb, AuLock_FLUSH);
13133 +
13134 +       /* branch id may be wrapped around */
13135 +       err = 0;
13136 +       bindex = au_br_index(sb, nsi_lock->br_id);
13137 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13138 +               goto out; /* success */
13139 +
13140 +       err = -ESTALE;
13141 +       bindex = -1;
13142 +       if (!nsi_lock->force_lock)
13143 +               si_read_unlock(sb);
13144 +
13145 +out:
13146 +       nsi_lock->bindex = bindex;
13147 +       return err;
13148 +}
13149 +
13150 +struct find_name_by_ino {
13151 +       struct dir_context ctx;
13152 +       int called, found;
13153 +       ino_t ino;
13154 +       char *name;
13155 +       int namelen;
13156 +};
13157 +
13158 +static bool
13159 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13160 +                loff_t offset, u64 ino, unsigned int d_type)
13161 +{
13162 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13163 +                                                 ctx);
13164 +
13165 +       a->called++;
13166 +       if (a->ino != ino)
13167 +               return true;
13168 +
13169 +       memcpy(a->name, name, namelen);
13170 +       a->namelen = namelen;
13171 +       a->found = 1;
13172 +       return false;
13173 +}
13174 +
13175 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13176 +                                    struct au_nfsd_si_lock *nsi_lock)
13177 +{
13178 +       struct dentry *dentry, *parent;
13179 +       struct file *file;
13180 +       struct inode *dir;
13181 +       struct find_name_by_ino arg = {
13182 +               .ctx = {
13183 +                       .actor = find_name_by_ino
13184 +               }
13185 +       };
13186 +       int err;
13187 +
13188 +       parent = path->dentry;
13189 +       if (nsi_lock)
13190 +               si_read_unlock(parent->d_sb);
13191 +       file = vfsub_dentry_open(path, au_dir_roflags);
13192 +       dentry = (void *)file;
13193 +       if (IS_ERR(file))
13194 +               goto out;
13195 +
13196 +       dentry = ERR_PTR(-ENOMEM);
13197 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13198 +       if (unlikely(!arg.name))
13199 +               goto out_file;
13200 +       arg.ino = ino;
13201 +       arg.found = 0;
13202 +       do {
13203 +               arg.called = 0;
13204 +               /* smp_mb(); */
13205 +               err = vfsub_iterate_dir(file, &arg.ctx);
13206 +       } while (!err && !arg.found && arg.called);
13207 +       dentry = ERR_PTR(err);
13208 +       if (unlikely(err))
13209 +               goto out_name;
13210 +       /* instead of ENOENT */
13211 +       dentry = ERR_PTR(-ESTALE);
13212 +       if (!arg.found)
13213 +               goto out_name;
13214 +
13215 +       /* do not call vfsub_lkup_one() */
13216 +       dir = d_inode(parent);
13217 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13218 +       AuTraceErrPtr(dentry);
13219 +       if (IS_ERR(dentry))
13220 +               goto out_name;
13221 +       AuDebugOn(au_test_anon(dentry));
13222 +       if (unlikely(d_really_is_negative(dentry))) {
13223 +               dput(dentry);
13224 +               dentry = ERR_PTR(-ENOENT);
13225 +       }
13226 +
13227 +out_name:
13228 +       free_page((unsigned long)arg.name);
13229 +out_file:
13230 +       fput(file);
13231 +out:
13232 +       if (unlikely(nsi_lock
13233 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13234 +               if (!IS_ERR(dentry)) {
13235 +                       dput(dentry);
13236 +                       dentry = ERR_PTR(-ESTALE);
13237 +               }
13238 +       AuTraceErrPtr(dentry);
13239 +       return dentry;
13240 +}
13241 +
13242 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13243 +                                       ino_t dir_ino,
13244 +                                       struct au_nfsd_si_lock *nsi_lock)
13245 +{
13246 +       struct dentry *dentry;
13247 +       struct path path;
13248 +
13249 +       if (dir_ino != AUFS_ROOT_INO) {
13250 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13251 +               dentry = path.dentry;
13252 +               if (!path.dentry || IS_ERR(path.dentry))
13253 +                       goto out;
13254 +               AuDebugOn(au_test_anon(path.dentry));
13255 +       } else
13256 +               path.dentry = dget(sb->s_root);
13257 +
13258 +       path.mnt = au_mnt_get(sb);
13259 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13260 +       path_put(&path);
13261 +
13262 +out:
13263 +       AuTraceErrPtr(dentry);
13264 +       return dentry;
13265 +}
13266 +
13267 +/* ---------------------------------------------------------------------- */
13268 +
13269 +static int h_acceptable(void *expv, struct dentry *dentry)
13270 +{
13271 +       return 1;
13272 +}
13273 +
13274 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13275 +                          char *buf, int len, struct super_block *sb)
13276 +{
13277 +       char *p;
13278 +       int n;
13279 +       struct path path;
13280 +
13281 +       p = d_path(h_rootpath, buf, len);
13282 +       if (IS_ERR(p))
13283 +               goto out;
13284 +       n = strlen(p);
13285 +
13286 +       path.mnt = h_rootpath->mnt;
13287 +       path.dentry = h_parent;
13288 +       p = d_path(&path, buf, len);
13289 +       if (IS_ERR(p))
13290 +               goto out;
13291 +       if (n != 1)
13292 +               p += n;
13293 +
13294 +       path.mnt = au_mnt_get(sb);
13295 +       path.dentry = sb->s_root;
13296 +       p = d_path(&path, buf, len - strlen(p));
13297 +       mntput(path.mnt);
13298 +       if (IS_ERR(p))
13299 +               goto out;
13300 +       if (n != 1)
13301 +               p[strlen(p)] = '/';
13302 +
13303 +out:
13304 +       AuTraceErrPtr(p);
13305 +       return p;
13306 +}
13307 +
13308 +static
13309 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13310 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13311 +{
13312 +       struct dentry *dentry, *h_parent, *root;
13313 +       struct super_block *h_sb;
13314 +       char *pathname, *p;
13315 +       struct vfsmount *h_mnt;
13316 +       struct au_branch *br;
13317 +       int err;
13318 +       struct path path;
13319 +
13320 +       br = au_sbr(sb, nsi_lock->bindex);
13321 +       h_mnt = au_br_mnt(br);
13322 +       h_sb = h_mnt->mnt_sb;
13323 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13324 +       lockdep_off();
13325 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13326 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13327 +                                     h_acceptable, /*context*/NULL);
13328 +       lockdep_on();
13329 +       dentry = h_parent;
13330 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13331 +               AuWarn1("%s decode_fh failed, %ld\n",
13332 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13333 +               goto out;
13334 +       }
13335 +       dentry = NULL;
13336 +       if (unlikely(au_test_anon(h_parent))) {
13337 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13338 +                       au_sbtype(h_sb));
13339 +               goto out_h_parent;
13340 +       }
13341 +
13342 +       dentry = ERR_PTR(-ENOMEM);
13343 +       pathname = (void *)__get_free_page(GFP_NOFS);
13344 +       if (unlikely(!pathname))
13345 +               goto out_h_parent;
13346 +
13347 +       root = sb->s_root;
13348 +       path.mnt = h_mnt;
13349 +       di_read_lock_parent(root, !AuLock_IR);
13350 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13351 +       di_read_unlock(root, !AuLock_IR);
13352 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13353 +       dentry = (void *)p;
13354 +       if (IS_ERR(p))
13355 +               goto out_pathname;
13356 +
13357 +       si_read_unlock(sb);
13358 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13359 +       dentry = ERR_PTR(err);
13360 +       if (unlikely(err))
13361 +               goto out_relock;
13362 +
13363 +       dentry = ERR_PTR(-ENOENT);
13364 +       AuDebugOn(au_test_anon(path.dentry));
13365 +       if (unlikely(d_really_is_negative(path.dentry)))
13366 +               goto out_path;
13367 +
13368 +       if (ino != d_inode(path.dentry)->i_ino)
13369 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13370 +       else
13371 +               dentry = dget(path.dentry);
13372 +
13373 +out_path:
13374 +       path_put(&path);
13375 +out_relock:
13376 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13377 +               if (!IS_ERR(dentry)) {
13378 +                       dput(dentry);
13379 +                       dentry = ERR_PTR(-ESTALE);
13380 +               }
13381 +out_pathname:
13382 +       free_page((unsigned long)pathname);
13383 +out_h_parent:
13384 +       dput(h_parent);
13385 +out:
13386 +       AuTraceErrPtr(dentry);
13387 +       return dentry;
13388 +}
13389 +
13390 +/* ---------------------------------------------------------------------- */
13391 +
13392 +static struct dentry *
13393 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13394 +                 int fh_type)
13395 +{
13396 +       struct dentry *dentry;
13397 +       __u32 *fh = fid->raw;
13398 +       struct au_branch *br;
13399 +       ino_t ino, dir_ino;
13400 +       struct au_nfsd_si_lock nsi_lock = {
13401 +               .force_lock     = 0
13402 +       };
13403 +
13404 +       dentry = ERR_PTR(-ESTALE);
13405 +       /* it should never happen, but the file handle is unreliable */
13406 +       if (unlikely(fh_len < Fh_tail))
13407 +               goto out;
13408 +       nsi_lock.sigen = fh[Fh_sigen];
13409 +       nsi_lock.br_id = fh[Fh_br_id];
13410 +
13411 +       /* branch id may be wrapped around */
13412 +       br = NULL;
13413 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13414 +               goto out;
13415 +       nsi_lock.force_lock = 1;
13416 +
13417 +       /* is this inode still cached? */
13418 +       ino = decode_ino(fh + Fh_ino);
13419 +       /* it should never happen */
13420 +       if (unlikely(ino == AUFS_ROOT_INO))
13421 +               goto out_unlock;
13422 +
13423 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13424 +       dentry = decode_by_ino(sb, ino, dir_ino);
13425 +       if (IS_ERR(dentry))
13426 +               goto out_unlock;
13427 +       if (dentry)
13428 +               goto accept;
13429 +
13430 +       /* is the parent dir cached? */
13431 +       br = au_sbr(sb, nsi_lock.bindex);
13432 +       au_lcnt_inc(&br->br_nfiles);
13433 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13434 +       if (IS_ERR(dentry))
13435 +               goto out_unlock;
13436 +       if (dentry)
13437 +               goto accept;
13438 +
13439 +       /* lookup path */
13440 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13441 +       if (IS_ERR(dentry))
13442 +               goto out_unlock;
13443 +       if (unlikely(!dentry))
13444 +               /* todo?: make it ESTALE */
13445 +               goto out_unlock;
13446 +
13447 +accept:
13448 +       if (!au_digen_test(dentry, au_sigen(sb))
13449 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13450 +               goto out_unlock; /* success */
13451 +
13452 +       dput(dentry);
13453 +       dentry = ERR_PTR(-ESTALE);
13454 +out_unlock:
13455 +       if (br)
13456 +               au_lcnt_dec(&br->br_nfiles);
13457 +       si_read_unlock(sb);
13458 +out:
13459 +       AuTraceErrPtr(dentry);
13460 +       return dentry;
13461 +}
13462 +
13463 +#if 0 /* reserved for future use */
13464 +/* support subtreecheck option */
13465 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13466 +                                       int fh_len, int fh_type)
13467 +{
13468 +       struct dentry *parent;
13469 +       __u32 *fh = fid->raw;
13470 +       ino_t dir_ino;
13471 +
13472 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13473 +       parent = decode_by_ino(sb, dir_ino, 0);
13474 +       if (IS_ERR(parent))
13475 +               goto out;
13476 +       if (!parent)
13477 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13478 +                                       dir_ino, fh, fh_len);
13479 +
13480 +out:
13481 +       AuTraceErrPtr(parent);
13482 +       return parent;
13483 +}
13484 +#endif
13485 +
13486 +/* ---------------------------------------------------------------------- */
13487 +
13488 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13489 +                         struct inode *dir)
13490 +{
13491 +       int err;
13492 +       aufs_bindex_t bindex;
13493 +       struct super_block *sb, *h_sb;
13494 +       struct dentry *dentry, *parent, *h_parent;
13495 +       struct inode *h_dir;
13496 +       struct au_branch *br;
13497 +
13498 +       err = -ENOSPC;
13499 +       if (unlikely(*max_len <= Fh_tail)) {
13500 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13501 +               goto out;
13502 +       }
13503 +
13504 +       err = FILEID_ROOT;
13505 +       if (inode->i_ino == AUFS_ROOT_INO) {
13506 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13507 +               goto out;
13508 +       }
13509 +
13510 +       h_parent = NULL;
13511 +       sb = inode->i_sb;
13512 +       err = si_read_lock(sb, AuLock_FLUSH);
13513 +       if (unlikely(err))
13514 +               goto out;
13515 +
13516 +#ifdef CONFIG_AUFS_DEBUG
13517 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13518 +               AuWarn1("NFS-exporting requires xino\n");
13519 +#endif
13520 +       err = -EIO;
13521 +       parent = NULL;
13522 +       ii_read_lock_child(inode);
13523 +       bindex = au_ibtop(inode);
13524 +       if (!dir) {
13525 +               dentry = d_find_any_alias(inode);
13526 +               if (unlikely(!dentry))
13527 +                       goto out_unlock;
13528 +               AuDebugOn(au_test_anon(dentry));
13529 +               parent = dget_parent(dentry);
13530 +               dput(dentry);
13531 +               if (unlikely(!parent))
13532 +                       goto out_unlock;
13533 +               if (d_really_is_positive(parent))
13534 +                       dir = d_inode(parent);
13535 +       }
13536 +
13537 +       ii_read_lock_parent(dir);
13538 +       h_dir = au_h_iptr(dir, bindex);
13539 +       ii_read_unlock(dir);
13540 +       if (unlikely(!h_dir))
13541 +               goto out_parent;
13542 +       h_parent = d_find_any_alias(h_dir);
13543 +       if (unlikely(!h_parent))
13544 +               goto out_hparent;
13545 +
13546 +       err = -EPERM;
13547 +       br = au_sbr(sb, bindex);
13548 +       h_sb = au_br_sb(br);
13549 +       if (unlikely(!h_sb->s_export_op)) {
13550 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13551 +               goto out_hparent;
13552 +       }
13553 +
13554 +       fh[Fh_br_id] = br->br_id;
13555 +       fh[Fh_sigen] = au_sigen(sb);
13556 +       encode_ino(fh + Fh_ino, inode->i_ino);
13557 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13558 +       fh[Fh_igen] = inode->i_generation;
13559 +
13560 +       *max_len -= Fh_tail;
13561 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13562 +                                          max_len,
13563 +                                          /*connectable or subtreecheck*/0);
13564 +       err = fh[Fh_h_type];
13565 +       *max_len += Fh_tail;
13566 +       /* todo: macros? */
13567 +       if (err != FILEID_INVALID)
13568 +               err = 99;
13569 +       else
13570 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13571 +
13572 +out_hparent:
13573 +       dput(h_parent);
13574 +out_parent:
13575 +       dput(parent);
13576 +out_unlock:
13577 +       ii_read_unlock(inode);
13578 +       si_read_unlock(sb);
13579 +out:
13580 +       if (unlikely(err < 0))
13581 +               err = FILEID_INVALID;
13582 +       return err;
13583 +}
13584 +
13585 +/* ---------------------------------------------------------------------- */
13586 +
13587 +static int aufs_commit_metadata(struct inode *inode)
13588 +{
13589 +       int err;
13590 +       aufs_bindex_t bindex;
13591 +       struct super_block *sb;
13592 +       struct inode *h_inode;
13593 +       int (*f)(struct inode *inode);
13594 +
13595 +       sb = inode->i_sb;
13596 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13597 +       ii_write_lock_child(inode);
13598 +       bindex = au_ibtop(inode);
13599 +       AuDebugOn(bindex < 0);
13600 +       h_inode = au_h_iptr(inode, bindex);
13601 +
13602 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13603 +       if (f)
13604 +               err = f(h_inode);
13605 +       else
13606 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13607 +
13608 +       au_cpup_attr_timesizes(inode);
13609 +       ii_write_unlock(inode);
13610 +       si_read_unlock(sb);
13611 +       return err;
13612 +}
13613 +
13614 +/* ---------------------------------------------------------------------- */
13615 +
13616 +static struct export_operations aufs_export_op = {
13617 +       .fh_to_dentry           = aufs_fh_to_dentry,
13618 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13619 +       .encode_fh              = aufs_encode_fh,
13620 +       .commit_metadata        = aufs_commit_metadata
13621 +};
13622 +
13623 +void au_export_init(struct super_block *sb)
13624 +{
13625 +       struct au_sbinfo *sbinfo;
13626 +       __u32 u;
13627 +
13628 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13629 +                        && IS_MODULE(CONFIG_EXPORTFS),
13630 +                        AUFS_NAME ": unsupported configuration "
13631 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13632 +
13633 +       sb->s_export_op = &aufs_export_op;
13634 +       sbinfo = au_sbi(sb);
13635 +       sbinfo->si_xigen = NULL;
13636 +       get_random_bytes(&u, sizeof(u));
13637 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13638 +       atomic_set(&sbinfo->si_xigen_next, u);
13639 +}
13640 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13641 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13642 +++ linux/fs/aufs/fhsm.c        2024-05-13 17:13:00.618070924 +0200
13643 @@ -0,0 +1,426 @@
13644 +// SPDX-License-Identifier: GPL-2.0
13645 +/*
13646 + * Copyright (C) 2011-2022 Junjiro R. Okajima
13647 + *
13648 + * This program is free software; you can redistribute it and/or modify
13649 + * it under the terms of the GNU General Public License as published by
13650 + * the Free Software Foundation; either version 2 of the License, or
13651 + * (at your option) any later version.
13652 + *
13653 + * This program is distributed in the hope that it will be useful,
13654 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13655 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13656 + * GNU General Public License for more details.
13657 + *
13658 + * You should have received a copy of the GNU General Public License
13659 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13660 + */
13661 +
13662 +/*
13663 + * File-based Hierarchy Storage Management
13664 + */
13665 +
13666 +#include <linux/anon_inodes.h>
13667 +#include <linux/poll.h>
13668 +#include <linux/seq_file.h>
13669 +#include <linux/statfs.h>
13670 +#include "aufs.h"
13671 +
13672 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13673 +{
13674 +       struct au_sbinfo *sbinfo;
13675 +       struct au_fhsm *fhsm;
13676 +
13677 +       SiMustAnyLock(sb);
13678 +
13679 +       sbinfo = au_sbi(sb);
13680 +       fhsm = &sbinfo->si_fhsm;
13681 +       AuDebugOn(!fhsm);
13682 +       return fhsm->fhsm_bottom;
13683 +}
13684 +
13685 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13686 +{
13687 +       struct au_sbinfo *sbinfo;
13688 +       struct au_fhsm *fhsm;
13689 +
13690 +       SiMustWriteLock(sb);
13691 +
13692 +       sbinfo = au_sbi(sb);
13693 +       fhsm = &sbinfo->si_fhsm;
13694 +       AuDebugOn(!fhsm);
13695 +       fhsm->fhsm_bottom = bindex;
13696 +}
13697 +
13698 +/* ---------------------------------------------------------------------- */
13699 +
13700 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13701 +{
13702 +       struct au_br_fhsm *bf;
13703 +
13704 +       bf = br->br_fhsm;
13705 +       MtxMustLock(&bf->bf_lock);
13706 +
13707 +       return !bf->bf_readable
13708 +               || time_after(jiffies,
13709 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13710 +}
13711 +
13712 +/* ---------------------------------------------------------------------- */
13713 +
13714 +static void au_fhsm_notify(struct super_block *sb, int val)
13715 +{
13716 +       struct au_sbinfo *sbinfo;
13717 +       struct au_fhsm *fhsm;
13718 +
13719 +       SiMustAnyLock(sb);
13720 +
13721 +       sbinfo = au_sbi(sb);
13722 +       fhsm = &sbinfo->si_fhsm;
13723 +       if (au_fhsm_pid(fhsm)
13724 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13725 +               atomic_set(&fhsm->fhsm_readable, val);
13726 +               if (val)
13727 +                       wake_up(&fhsm->fhsm_wqh);
13728 +       }
13729 +}
13730 +
13731 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13732 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13733 +{
13734 +       int err;
13735 +       struct au_branch *br;
13736 +       struct au_br_fhsm *bf;
13737 +
13738 +       br = au_sbr(sb, bindex);
13739 +       AuDebugOn(au_br_rdonly(br));
13740 +       bf = br->br_fhsm;
13741 +       AuDebugOn(!bf);
13742 +
13743 +       if (do_lock)
13744 +               mutex_lock(&bf->bf_lock);
13745 +       else
13746 +               MtxMustLock(&bf->bf_lock);
13747 +
13748 +       /* sb->s_root for NFS is unreliable */
13749 +       err = au_br_stfs(br, &bf->bf_stfs);
13750 +       if (unlikely(err)) {
13751 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13752 +               goto out;
13753 +       }
13754 +
13755 +       bf->bf_jiffy = jiffies;
13756 +       bf->bf_readable = 1;
13757 +       if (do_notify)
13758 +               au_fhsm_notify(sb, /*val*/1);
13759 +       if (rstfs)
13760 +               *rstfs = bf->bf_stfs;
13761 +
13762 +out:
13763 +       if (do_lock)
13764 +               mutex_unlock(&bf->bf_lock);
13765 +       au_fhsm_notify(sb, /*val*/1);
13766 +
13767 +       return err;
13768 +}
13769 +
13770 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13771 +{
13772 +       int err;
13773 +       struct au_sbinfo *sbinfo;
13774 +       struct au_fhsm *fhsm;
13775 +       struct au_branch *br;
13776 +       struct au_br_fhsm *bf;
13777 +
13778 +       AuDbg("b%d, force %d\n", bindex, force);
13779 +       SiMustAnyLock(sb);
13780 +
13781 +       sbinfo = au_sbi(sb);
13782 +       fhsm = &sbinfo->si_fhsm;
13783 +       if (!au_ftest_si(sbinfo, FHSM)
13784 +           || fhsm->fhsm_bottom == bindex)
13785 +               return;
13786 +
13787 +       br = au_sbr(sb, bindex);
13788 +       bf = br->br_fhsm;
13789 +       AuDebugOn(!bf);
13790 +       mutex_lock(&bf->bf_lock);
13791 +       if (force
13792 +           || au_fhsm_pid(fhsm)
13793 +           || au_fhsm_test_jiffy(sbinfo, br))
13794 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13795 +                                 /*do_notify*/1);
13796 +       mutex_unlock(&bf->bf_lock);
13797 +}
13798 +
13799 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13800 +{
13801 +       aufs_bindex_t bindex, bbot;
13802 +       struct au_branch *br;
13803 +
13804 +       /* exclude the bottom */
13805 +       bbot = au_fhsm_bottom(sb);
13806 +       for (bindex = 0; bindex < bbot; bindex++) {
13807 +               br = au_sbr(sb, bindex);
13808 +               if (au_br_fhsm(br->br_perm))
13809 +                       au_fhsm_wrote(sb, bindex, force);
13810 +       }
13811 +}
13812 +
13813 +/* ---------------------------------------------------------------------- */
13814 +
13815 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13816 +{
13817 +       __poll_t mask;
13818 +       struct au_sbinfo *sbinfo;
13819 +       struct au_fhsm *fhsm;
13820 +
13821 +       mask = 0;
13822 +       sbinfo = file->private_data;
13823 +       fhsm = &sbinfo->si_fhsm;
13824 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13825 +       if (atomic_read(&fhsm->fhsm_readable))
13826 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13827 +
13828 +       if (!mask)
13829 +               AuDbg("mask 0x%x\n", mask);
13830 +       return mask;
13831 +}
13832 +
13833 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13834 +                             struct aufs_stfs *stfs, __s16 brid)
13835 +{
13836 +       int err;
13837 +
13838 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13839 +       if (!err)
13840 +               err = __put_user(brid, &stbr->brid);
13841 +       if (unlikely(err))
13842 +               err = -EFAULT;
13843 +
13844 +       return err;
13845 +}
13846 +
13847 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13848 +                              struct aufs_stbr __user *stbr, size_t count)
13849 +{
13850 +       ssize_t err;
13851 +       int nstbr;
13852 +       aufs_bindex_t bindex, bbot;
13853 +       struct au_branch *br;
13854 +       struct au_br_fhsm *bf;
13855 +
13856 +       /* except the bottom branch */
13857 +       err = 0;
13858 +       nstbr = 0;
13859 +       bbot = au_fhsm_bottom(sb);
13860 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13861 +               br = au_sbr(sb, bindex);
13862 +               if (!au_br_fhsm(br->br_perm))
13863 +                       continue;
13864 +
13865 +               bf = br->br_fhsm;
13866 +               mutex_lock(&bf->bf_lock);
13867 +               if (bf->bf_readable) {
13868 +                       err = -EFAULT;
13869 +                       if (count >= sizeof(*stbr))
13870 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13871 +                                                         br->br_id);
13872 +                       if (!err) {
13873 +                               bf->bf_readable = 0;
13874 +                               count -= sizeof(*stbr);
13875 +                               nstbr++;
13876 +                       }
13877 +               }
13878 +               mutex_unlock(&bf->bf_lock);
13879 +       }
13880 +       if (!err)
13881 +               err = sizeof(*stbr) * nstbr;
13882 +
13883 +       return err;
13884 +}
13885 +
13886 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13887 +                          loff_t *pos)
13888 +{
13889 +       ssize_t err;
13890 +       int readable;
13891 +       aufs_bindex_t nfhsm, bindex, bbot;
13892 +       struct au_sbinfo *sbinfo;
13893 +       struct au_fhsm *fhsm;
13894 +       struct au_branch *br;
13895 +       struct super_block *sb;
13896 +
13897 +       err = 0;
13898 +       sbinfo = file->private_data;
13899 +       fhsm = &sbinfo->si_fhsm;
13900 +need_data:
13901 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13902 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13903 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13904 +                       err = -EAGAIN;
13905 +               else
13906 +                       err = wait_event_interruptible_locked_irq
13907 +                               (fhsm->fhsm_wqh,
13908 +                                atomic_read(&fhsm->fhsm_readable));
13909 +       }
13910 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13911 +       if (unlikely(err))
13912 +               goto out;
13913 +
13914 +       /* sb may already be dead */
13915 +       au_rw_read_lock(&sbinfo->si_rwsem);
13916 +       readable = atomic_read(&fhsm->fhsm_readable);
13917 +       if (readable > 0) {
13918 +               sb = sbinfo->si_sb;
13919 +               AuDebugOn(!sb);
13920 +               /* exclude the bottom branch */
13921 +               nfhsm = 0;
13922 +               bbot = au_fhsm_bottom(sb);
13923 +               for (bindex = 0; bindex < bbot; bindex++) {
13924 +                       br = au_sbr(sb, bindex);
13925 +                       if (au_br_fhsm(br->br_perm))
13926 +                               nfhsm++;
13927 +               }
13928 +               err = -EMSGSIZE;
13929 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13930 +                       atomic_set(&fhsm->fhsm_readable, 0);
13931 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13932 +                                            count);
13933 +               }
13934 +       }
13935 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13936 +       if (!readable)
13937 +               goto need_data;
13938 +
13939 +out:
13940 +       return err;
13941 +}
13942 +
13943 +static int au_fhsm_release(struct inode *inode, struct file *file)
13944 +{
13945 +       struct au_sbinfo *sbinfo;
13946 +       struct au_fhsm *fhsm;
13947 +
13948 +       /* sb may already be dead */
13949 +       sbinfo = file->private_data;
13950 +       fhsm = &sbinfo->si_fhsm;
13951 +       spin_lock(&fhsm->fhsm_spin);
13952 +       fhsm->fhsm_pid = 0;
13953 +       spin_unlock(&fhsm->fhsm_spin);
13954 +       kobject_put(&sbinfo->si_kobj);
13955 +
13956 +       return 0;
13957 +}
13958 +
13959 +static const struct file_operations au_fhsm_fops = {
13960 +       .owner          = THIS_MODULE,
13961 +       .llseek         = noop_llseek,
13962 +       .read           = au_fhsm_read,
13963 +       .poll           = au_fhsm_poll,
13964 +       .release        = au_fhsm_release
13965 +};
13966 +
13967 +int au_fhsm_fd(struct super_block *sb, int oflags)
13968 +{
13969 +       int err, fd;
13970 +       struct au_sbinfo *sbinfo;
13971 +       struct au_fhsm *fhsm;
13972 +
13973 +       err = -EPERM;
13974 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13975 +               goto out;
13976 +
13977 +       err = -EINVAL;
13978 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13979 +               goto out;
13980 +
13981 +       err = 0;
13982 +       sbinfo = au_sbi(sb);
13983 +       fhsm = &sbinfo->si_fhsm;
13984 +       spin_lock(&fhsm->fhsm_spin);
13985 +       if (!fhsm->fhsm_pid)
13986 +               fhsm->fhsm_pid = current->pid;
13987 +       else
13988 +               err = -EBUSY;
13989 +       spin_unlock(&fhsm->fhsm_spin);
13990 +       if (unlikely(err))
13991 +               goto out;
13992 +
13993 +       oflags |= O_RDONLY;
13994 +       /* oflags |= FMODE_NONOTIFY; */
13995 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13996 +       err = fd;
13997 +       if (unlikely(fd < 0))
13998 +               goto out_pid;
13999 +
14000 +       /* succeed regardless 'fhsm' status */
14001 +       kobject_get(&sbinfo->si_kobj);
14002 +       si_noflush_read_lock(sb);
14003 +       if (au_ftest_si(sbinfo, FHSM))
14004 +               au_fhsm_wrote_all(sb, /*force*/0);
14005 +       si_read_unlock(sb);
14006 +       goto out; /* success */
14007 +
14008 +out_pid:
14009 +       spin_lock(&fhsm->fhsm_spin);
14010 +       fhsm->fhsm_pid = 0;
14011 +       spin_unlock(&fhsm->fhsm_spin);
14012 +out:
14013 +       AuTraceErr(err);
14014 +       return err;
14015 +}
14016 +
14017 +/* ---------------------------------------------------------------------- */
14018 +
14019 +int au_fhsm_br_alloc(struct au_branch *br)
14020 +{
14021 +       int err;
14022 +
14023 +       err = 0;
14024 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14025 +       if (br->br_fhsm)
14026 +               au_br_fhsm_init(br->br_fhsm);
14027 +       else
14028 +               err = -ENOMEM;
14029 +
14030 +       return err;
14031 +}
14032 +
14033 +/* ---------------------------------------------------------------------- */
14034 +
14035 +void au_fhsm_fin(struct super_block *sb)
14036 +{
14037 +       au_fhsm_notify(sb, /*val*/-1);
14038 +}
14039 +
14040 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14041 +{
14042 +       struct au_fhsm *fhsm;
14043 +
14044 +       fhsm = &sbinfo->si_fhsm;
14045 +       spin_lock_init(&fhsm->fhsm_spin);
14046 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14047 +       atomic_set(&fhsm->fhsm_readable, 0);
14048 +       fhsm->fhsm_expire
14049 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14050 +       fhsm->fhsm_bottom = -1;
14051 +}
14052 +
14053 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14054 +{
14055 +       sbinfo->si_fhsm.fhsm_expire
14056 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14057 +}
14058 +
14059 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14060 +{
14061 +       unsigned int u;
14062 +
14063 +       if (!au_ftest_si(sbinfo, FHSM))
14064 +               return;
14065 +
14066 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14067 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14068 +               seq_printf(seq, ",fhsm_sec=%u", u);
14069 +}
14070 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14071 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14072 +++ linux/fs/aufs/file.c        2024-05-13 17:13:00.618070924 +0200
14073 @@ -0,0 +1,865 @@
14074 +// SPDX-License-Identifier: GPL-2.0
14075 +/*
14076 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14077 + *
14078 + * This program is free software; you can redistribute it and/or modify
14079 + * it under the terms of the GNU General Public License as published by
14080 + * the Free Software Foundation; either version 2 of the License, or
14081 + * (at your option) any later version.
14082 + *
14083 + * This program is distributed in the hope that it will be useful,
14084 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14085 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14086 + * GNU General Public License for more details.
14087 + *
14088 + * You should have received a copy of the GNU General Public License
14089 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14090 + */
14091 +
14092 +/*
14093 + * handling file/dir, and address_space operation
14094 + */
14095 +
14096 +#ifdef CONFIG_AUFS_DEBUG
14097 +#include <linux/migrate.h>
14098 +#endif
14099 +#include <linux/pagemap.h>
14100 +#include "aufs.h"
14101 +
14102 +/* drop flags for writing */
14103 +unsigned int au_file_roflags(unsigned int flags)
14104 +{
14105 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14106 +       flags |= O_RDONLY | O_NOATIME;
14107 +       return flags;
14108 +}
14109 +
14110 +/* common functions to regular file and dir */
14111 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14112 +                      struct file *file, int force_wr)
14113 +{
14114 +       struct file *h_file;
14115 +       struct dentry *h_dentry;
14116 +       struct inode *h_inode;
14117 +       struct super_block *sb;
14118 +       struct au_branch *br;
14119 +       struct path h_path;
14120 +       int err;
14121 +
14122 +       /* a race condition can happen between open and unlink/rmdir */
14123 +       h_file = ERR_PTR(-ENOENT);
14124 +       h_dentry = au_h_dptr(dentry, bindex);
14125 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14126 +               goto out;
14127 +       h_inode = d_inode(h_dentry);
14128 +       spin_lock(&h_dentry->d_lock);
14129 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14130 +               /* || !d_inode(dentry)->i_nlink */
14131 +               ;
14132 +       spin_unlock(&h_dentry->d_lock);
14133 +       if (unlikely(err))
14134 +               goto out;
14135 +
14136 +       sb = dentry->d_sb;
14137 +       br = au_sbr(sb, bindex);
14138 +       err = au_br_test_oflag(flags, br);
14139 +       h_file = ERR_PTR(err);
14140 +       if (unlikely(err))
14141 +               goto out;
14142 +
14143 +       /* drop flags for writing */
14144 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14145 +               if (force_wr && !(flags & O_WRONLY))
14146 +                       force_wr = 0;
14147 +               flags = au_file_roflags(flags);
14148 +               if (force_wr) {
14149 +                       h_file = ERR_PTR(-EROFS);
14150 +                       flags = au_file_roflags(flags);
14151 +                       if (unlikely(vfsub_native_ro(h_inode)
14152 +                                    || IS_APPEND(h_inode)))
14153 +                               goto out;
14154 +                       flags &= ~O_ACCMODE;
14155 +                       flags |= O_WRONLY;
14156 +               }
14157 +       }
14158 +       flags &= ~O_CREAT;
14159 +       au_lcnt_inc(&br->br_nfiles);
14160 +       h_path.dentry = h_dentry;
14161 +       h_path.mnt = au_br_mnt(br);
14162 +       /*
14163 +        * vfs::backing_file_open() looks promising since it can get rid of
14164 +        * mm::vm_prfile approach from my mind.
14165 +        * but I keep current approach for a while.
14166 +        */
14167 +       h_file = vfsub_dentry_open(&h_path, flags);
14168 +       if (IS_ERR(h_file))
14169 +               goto out_br;
14170 +
14171 +       if (flags & __FMODE_EXEC) {
14172 +               err = deny_write_access(h_file);
14173 +               if (unlikely(err)) {
14174 +                       fput(h_file);
14175 +                       h_file = ERR_PTR(err);
14176 +                       goto out_br;
14177 +               }
14178 +       }
14179 +       fsnotify_open(h_file);
14180 +       goto out; /* success */
14181 +
14182 +out_br:
14183 +       au_lcnt_dec(&br->br_nfiles);
14184 +out:
14185 +       return h_file;
14186 +}
14187 +
14188 +static int au_cmoo(struct dentry *dentry)
14189 +{
14190 +       int err, cmoo, matched;
14191 +       unsigned int udba;
14192 +       struct path h_path;
14193 +       struct au_pin pin;
14194 +       struct au_cp_generic cpg = {
14195 +               .dentry = dentry,
14196 +               .bdst   = -1,
14197 +               .bsrc   = -1,
14198 +               .len    = -1,
14199 +               .pin    = &pin,
14200 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14201 +       };
14202 +       struct inode *delegated;
14203 +       struct super_block *sb;
14204 +       struct au_sbinfo *sbinfo;
14205 +       struct au_fhsm *fhsm;
14206 +       pid_t pid;
14207 +       struct au_branch *br;
14208 +       struct dentry *parent;
14209 +       struct au_hinode *hdir;
14210 +
14211 +       DiMustWriteLock(dentry);
14212 +       IiMustWriteLock(d_inode(dentry));
14213 +
14214 +       err = 0;
14215 +       if (IS_ROOT(dentry))
14216 +               goto out;
14217 +       cpg.bsrc = au_dbtop(dentry);
14218 +       if (!cpg.bsrc)
14219 +               goto out;
14220 +
14221 +       sb = dentry->d_sb;
14222 +       sbinfo = au_sbi(sb);
14223 +       fhsm = &sbinfo->si_fhsm;
14224 +       pid = au_fhsm_pid(fhsm);
14225 +       rcu_read_lock();
14226 +       matched = (pid
14227 +                  && (current->pid == pid
14228 +                      || rcu_dereference(current->real_parent)->pid == pid));
14229 +       rcu_read_unlock();
14230 +       if (matched)
14231 +               goto out;
14232 +
14233 +       br = au_sbr(sb, cpg.bsrc);
14234 +       cmoo = au_br_cmoo(br->br_perm);
14235 +       if (!cmoo)
14236 +               goto out;
14237 +       if (!d_is_reg(dentry))
14238 +               cmoo &= AuBrAttr_COO_ALL;
14239 +       if (!cmoo)
14240 +               goto out;
14241 +
14242 +       parent = dget_parent(dentry);
14243 +       di_write_lock_parent(parent);
14244 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14245 +       cpg.bdst = err;
14246 +       if (unlikely(err < 0)) {
14247 +               err = 0;        /* there is no upper writable branch */
14248 +               goto out_dgrade;
14249 +       }
14250 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14251 +
14252 +       /* do not respect the coo attrib for the target branch */
14253 +       err = au_cpup_dirs(dentry, cpg.bdst);
14254 +       if (unlikely(err))
14255 +               goto out_dgrade;
14256 +
14257 +       di_downgrade_lock(parent, AuLock_IR);
14258 +       udba = au_opt_udba(sb);
14259 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14260 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14261 +       if (unlikely(err))
14262 +               goto out_parent;
14263 +
14264 +       err = au_sio_cpup_simple(&cpg);
14265 +       au_unpin(&pin);
14266 +       if (unlikely(err))
14267 +               goto out_parent;
14268 +       if (!(cmoo & AuBrWAttr_MOO))
14269 +               goto out_parent; /* success */
14270 +
14271 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14272 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14273 +       if (unlikely(err))
14274 +               goto out_parent;
14275 +
14276 +       h_path.mnt = au_br_mnt(br);
14277 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14278 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14279 +       delegated = NULL;
14280 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14281 +       au_unpin(&pin);
14282 +       /* todo: keep h_dentry or not? */
14283 +       if (unlikely(err == -EWOULDBLOCK)) {
14284 +               pr_warn("cannot retry for NFSv4 delegation"
14285 +                       " for an internal unlink\n");
14286 +               iput(delegated);
14287 +       }
14288 +       if (unlikely(err)) {
14289 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14290 +                      dentry, err);
14291 +               err = 0;
14292 +       }
14293 +       goto out_parent; /* success */
14294 +
14295 +out_dgrade:
14296 +       di_downgrade_lock(parent, AuLock_IR);
14297 +out_parent:
14298 +       di_read_unlock(parent, AuLock_IR);
14299 +       dput(parent);
14300 +out:
14301 +       AuTraceErr(err);
14302 +       return err;
14303 +}
14304 +
14305 +int au_do_open(struct file *file, struct au_do_open_args *args)
14306 +{
14307 +       int err, aopen = args->aopen;
14308 +       struct dentry *dentry;
14309 +       struct au_finfo *finfo;
14310 +
14311 +       if (!aopen)
14312 +               err = au_finfo_init(file, args->fidir);
14313 +       else {
14314 +               lockdep_off();
14315 +               err = au_finfo_init(file, args->fidir);
14316 +               lockdep_on();
14317 +       }
14318 +       if (unlikely(err))
14319 +               goto out;
14320 +
14321 +       dentry = file->f_path.dentry;
14322 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14323 +       di_write_lock_child(dentry);
14324 +       err = au_cmoo(dentry);
14325 +       if (!err) {
14326 +               if (!aopen) {
14327 +                       err = args->open(file, vfsub_file_flags(file),
14328 +                                        au_di(dentry)->di_htmpfile);
14329 +                       di_write_unlock(dentry);
14330 +               } else {
14331 +                       di_downgrade_lock(dentry, AuLock_IR);
14332 +                       lockdep_off();
14333 +                       err = args->open(file, vfsub_file_flags(file),
14334 +                                        args->h_file);
14335 +                       lockdep_on();
14336 +                       di_read_unlock(dentry, AuLock_IR);
14337 +               }
14338 +       }
14339 +
14340 +       finfo = au_fi(file);
14341 +       if (!err) {
14342 +               finfo->fi_file = file;
14343 +               au_hbl_add(&finfo->fi_hlist,
14344 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14345 +       }
14346 +       if (!aopen)
14347 +               fi_write_unlock(file);
14348 +       else {
14349 +               lockdep_off();
14350 +               fi_write_unlock(file);
14351 +               lockdep_on();
14352 +       }
14353 +       if (unlikely(err)) {
14354 +               finfo->fi_hdir = NULL;
14355 +               au_finfo_fin(file);
14356 +       }
14357 +
14358 +out:
14359 +       AuTraceErr(err);
14360 +       return err;
14361 +}
14362 +
14363 +int au_reopen_nondir(struct file *file)
14364 +{
14365 +       int err;
14366 +       aufs_bindex_t btop;
14367 +       struct dentry *dentry;
14368 +       struct au_branch *br;
14369 +       struct file *h_file, *h_file_tmp;
14370 +
14371 +       dentry = file->f_path.dentry;
14372 +       btop = au_dbtop(dentry);
14373 +       br = au_sbr(dentry->d_sb, btop);
14374 +       h_file_tmp = NULL;
14375 +       if (au_fbtop(file) == btop) {
14376 +               h_file = au_hf_top(file);
14377 +               if (file->f_mode == h_file->f_mode)
14378 +                       return 0; /* success */
14379 +               h_file_tmp = h_file;
14380 +               get_file(h_file_tmp);
14381 +               au_lcnt_inc(&br->br_nfiles);
14382 +               au_set_h_fptr(file, btop, NULL);
14383 +       }
14384 +       AuDebugOn(au_fi(file)->fi_hdir);
14385 +       /*
14386 +        * it can happen
14387 +        * file exists on both of rw and ro
14388 +        * open --> dbtop and fbtop are both 0
14389 +        * prepend a branch as rw, "rw" become ro
14390 +        * remove rw/file
14391 +        * delete the top branch, "rw" becomes rw again
14392 +        *      --> dbtop is 1, fbtop is still 0
14393 +        * write --> fbtop is 0 but dbtop is 1
14394 +        */
14395 +       /* AuDebugOn(au_fbtop(file) < btop); */
14396 +
14397 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14398 +                          file, /*force_wr*/0);
14399 +       err = PTR_ERR(h_file);
14400 +       if (IS_ERR(h_file)) {
14401 +               if (h_file_tmp) {
14402 +                       /* revert */
14403 +                       au_set_h_fptr(file, btop, h_file_tmp);
14404 +                       h_file_tmp = NULL;
14405 +               }
14406 +               goto out; /* todo: close all? */
14407 +       }
14408 +
14409 +       err = 0;
14410 +       au_set_fbtop(file, btop);
14411 +       au_set_h_fptr(file, btop, h_file);
14412 +       au_update_figen(file);
14413 +       /* todo: necessary? */
14414 +       /* file->f_ra = h_file->f_ra; */
14415 +
14416 +out:
14417 +       if (h_file_tmp) {
14418 +               fput(h_file_tmp);
14419 +               au_lcnt_dec(&br->br_nfiles);
14420 +       }
14421 +       return err;
14422 +}
14423 +
14424 +/* ---------------------------------------------------------------------- */
14425 +
14426 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14427 +                       struct dentry *hi_wh)
14428 +{
14429 +       int err;
14430 +       aufs_bindex_t btop;
14431 +       struct au_dinfo *dinfo;
14432 +       struct dentry *h_dentry;
14433 +       struct au_hdentry *hdp;
14434 +
14435 +       dinfo = au_di(file->f_path.dentry);
14436 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14437 +
14438 +       btop = dinfo->di_btop;
14439 +       dinfo->di_btop = btgt;
14440 +       hdp = au_hdentry(dinfo, btgt);
14441 +       h_dentry = hdp->hd_dentry;
14442 +       hdp->hd_dentry = hi_wh;
14443 +       err = au_reopen_nondir(file);
14444 +       hdp->hd_dentry = h_dentry;
14445 +       dinfo->di_btop = btop;
14446 +
14447 +       return err;
14448 +}
14449 +
14450 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14451 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14452 +{
14453 +       int err;
14454 +       struct inode *inode, *h_inode;
14455 +       struct dentry *h_dentry, *hi_wh;
14456 +       struct au_cp_generic cpg = {
14457 +               .dentry = file->f_path.dentry,
14458 +               .bdst   = bcpup,
14459 +               .bsrc   = -1,
14460 +               .len    = len,
14461 +               .pin    = pin
14462 +       };
14463 +
14464 +       au_update_dbtop(cpg.dentry);
14465 +       inode = d_inode(cpg.dentry);
14466 +       h_inode = NULL;
14467 +       if (au_dbtop(cpg.dentry) <= bcpup
14468 +           && au_dbbot(cpg.dentry) >= bcpup) {
14469 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14470 +               if (h_dentry && d_is_positive(h_dentry))
14471 +                       h_inode = d_inode(h_dentry);
14472 +       }
14473 +       hi_wh = au_hi_wh(inode, bcpup);
14474 +       if (!hi_wh && !h_inode)
14475 +               err = au_sio_cpup_wh(&cpg, file);
14476 +       else
14477 +               /* already copied-up after unlink */
14478 +               err = au_reopen_wh(file, bcpup, hi_wh);
14479 +
14480 +       if (!err
14481 +           && (inode->i_nlink > 1
14482 +               || (inode->i_state & I_LINKABLE))
14483 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14484 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14485 +
14486 +       return err;
14487 +}
14488 +
14489 +/*
14490 + * prepare the @file for writing.
14491 + */
14492 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14493 +{
14494 +       int err;
14495 +       aufs_bindex_t dbtop;
14496 +       struct dentry *parent;
14497 +       struct inode *inode;
14498 +       struct super_block *sb;
14499 +       struct file *h_file;
14500 +       struct au_cp_generic cpg = {
14501 +               .dentry = file->f_path.dentry,
14502 +               .bdst   = -1,
14503 +               .bsrc   = -1,
14504 +               .len    = len,
14505 +               .pin    = pin,
14506 +               .flags  = AuCpup_DTIME
14507 +       };
14508 +
14509 +       sb = cpg.dentry->d_sb;
14510 +       inode = d_inode(cpg.dentry);
14511 +       cpg.bsrc = au_fbtop(file);
14512 +       err = au_test_ro(sb, cpg.bsrc, inode);
14513 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14514 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14515 +                            /*flags*/0);
14516 +               goto out;
14517 +       }
14518 +
14519 +       /* need to cpup or reopen */
14520 +       parent = dget_parent(cpg.dentry);
14521 +       di_write_lock_parent(parent);
14522 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14523 +       cpg.bdst = err;
14524 +       if (unlikely(err < 0))
14525 +               goto out_dgrade;
14526 +       err = 0;
14527 +
14528 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14529 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14530 +               if (unlikely(err))
14531 +                       goto out_dgrade;
14532 +       }
14533 +
14534 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14535 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14536 +       if (unlikely(err))
14537 +               goto out_dgrade;
14538 +
14539 +       dbtop = au_dbtop(cpg.dentry);
14540 +       if (dbtop <= cpg.bdst)
14541 +               cpg.bsrc = cpg.bdst;
14542 +
14543 +       if (dbtop <= cpg.bdst           /* just reopen */
14544 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14545 +               ) {
14546 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14547 +               if (IS_ERR(h_file))
14548 +                       err = PTR_ERR(h_file);
14549 +               else {
14550 +                       di_downgrade_lock(parent, AuLock_IR);
14551 +                       if (dbtop > cpg.bdst)
14552 +                               err = au_sio_cpup_simple(&cpg);
14553 +                       if (!err)
14554 +                               err = au_reopen_nondir(file);
14555 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14556 +               }
14557 +       } else {                        /* copyup as wh and reopen */
14558 +               /*
14559 +                * since writable hfsplus branch is not supported,
14560 +                * h_open_pre/post() are unnecessary.
14561 +                */
14562 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14563 +               di_downgrade_lock(parent, AuLock_IR);
14564 +       }
14565 +
14566 +       if (!err) {
14567 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14568 +               goto out_dput; /* success */
14569 +       }
14570 +       au_unpin(pin);
14571 +       goto out_unlock;
14572 +
14573 +out_dgrade:
14574 +       di_downgrade_lock(parent, AuLock_IR);
14575 +out_unlock:
14576 +       di_read_unlock(parent, AuLock_IR);
14577 +out_dput:
14578 +       dput(parent);
14579 +out:
14580 +       return err;
14581 +}
14582 +
14583 +/* ---------------------------------------------------------------------- */
14584 +
14585 +int au_do_flush(struct file *file, fl_owner_t id,
14586 +               int (*flush)(struct file *file, fl_owner_t id))
14587 +{
14588 +       int err;
14589 +       struct super_block *sb;
14590 +       struct inode *inode;
14591 +
14592 +       inode = file_inode(file);
14593 +       sb = inode->i_sb;
14594 +       si_noflush_read_lock(sb);
14595 +       fi_read_lock(file);
14596 +       ii_read_lock_child(inode);
14597 +
14598 +       err = flush(file, id);
14599 +       au_cpup_attr_timesizes(inode);
14600 +
14601 +       ii_read_unlock(inode);
14602 +       fi_read_unlock(file);
14603 +       si_read_unlock(sb);
14604 +       return err;
14605 +}
14606 +
14607 +/* ---------------------------------------------------------------------- */
14608 +
14609 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14610 +{
14611 +       int err;
14612 +       struct au_pin pin;
14613 +       struct au_finfo *finfo;
14614 +       struct dentry *parent, *hi_wh;
14615 +       struct inode *inode;
14616 +       struct super_block *sb;
14617 +       struct au_cp_generic cpg = {
14618 +               .dentry = file->f_path.dentry,
14619 +               .bdst   = -1,
14620 +               .bsrc   = -1,
14621 +               .len    = -1,
14622 +               .pin    = &pin,
14623 +               .flags  = AuCpup_DTIME
14624 +       };
14625 +
14626 +       FiMustWriteLock(file);
14627 +
14628 +       err = 0;
14629 +       finfo = au_fi(file);
14630 +       sb = cpg.dentry->d_sb;
14631 +       inode = d_inode(cpg.dentry);
14632 +       cpg.bdst = au_ibtop(inode);
14633 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14634 +               goto out;
14635 +
14636 +       parent = dget_parent(cpg.dentry);
14637 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14638 +               di_read_lock_parent(parent, !AuLock_IR);
14639 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14640 +               cpg.bdst = err;
14641 +               di_read_unlock(parent, !AuLock_IR);
14642 +               if (unlikely(err < 0))
14643 +                       goto out_parent;
14644 +               err = 0;
14645 +       }
14646 +
14647 +       di_read_lock_parent(parent, AuLock_IR);
14648 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14649 +       if (!S_ISDIR(inode->i_mode)
14650 +           && au_opt_test(au_mntflags(sb), PLINK)
14651 +           && au_plink_test(inode)
14652 +           && !d_unhashed(cpg.dentry)
14653 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14654 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14655 +               if (unlikely(err))
14656 +                       goto out_unlock;
14657 +
14658 +               /* always superio. */
14659 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14660 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14661 +               if (!err) {
14662 +                       err = au_sio_cpup_simple(&cpg);
14663 +                       au_unpin(&pin);
14664 +               }
14665 +       } else if (hi_wh) {
14666 +               /* already copied-up after unlink */
14667 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14668 +               *need_reopen = 0;
14669 +       }
14670 +
14671 +out_unlock:
14672 +       di_read_unlock(parent, AuLock_IR);
14673 +out_parent:
14674 +       dput(parent);
14675 +out:
14676 +       return err;
14677 +}
14678 +
14679 +static void au_do_refresh_dir(struct file *file)
14680 +{
14681 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14682 +       struct au_hfile *p, tmp, *q;
14683 +       struct au_finfo *finfo;
14684 +       struct super_block *sb;
14685 +       struct au_fidir *fidir;
14686 +
14687 +       FiMustWriteLock(file);
14688 +
14689 +       sb = file->f_path.dentry->d_sb;
14690 +       finfo = au_fi(file);
14691 +       fidir = finfo->fi_hdir;
14692 +       AuDebugOn(!fidir);
14693 +       p = fidir->fd_hfile + finfo->fi_btop;
14694 +       brid = p->hf_br->br_id;
14695 +       bbot = fidir->fd_bbot;
14696 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14697 +               if (!p->hf_file)
14698 +                       continue;
14699 +
14700 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14701 +               if (new_bindex == bindex)
14702 +                       continue;
14703 +               if (new_bindex < 0) {
14704 +                       au_set_h_fptr(file, bindex, NULL);
14705 +                       continue;
14706 +               }
14707 +
14708 +               /* swap two lower inode, and loop again */
14709 +               q = fidir->fd_hfile + new_bindex;
14710 +               tmp = *q;
14711 +               *q = *p;
14712 +               *p = tmp;
14713 +               if (tmp.hf_file) {
14714 +                       bindex--;
14715 +                       p--;
14716 +               }
14717 +       }
14718 +
14719 +       p = fidir->fd_hfile;
14720 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14721 +               bbot = au_sbbot(sb);
14722 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14723 +                    finfo->fi_btop++, p++)
14724 +                       if (p->hf_file) {
14725 +                               if (file_inode(p->hf_file))
14726 +                                       break;
14727 +                               au_hfput(p, /*execed*/0);
14728 +                       }
14729 +       } else {
14730 +               bbot = au_br_index(sb, brid);
14731 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14732 +                    finfo->fi_btop++, p++)
14733 +                       if (p->hf_file)
14734 +                               au_hfput(p, /*execed*/0);
14735 +               bbot = au_sbbot(sb);
14736 +       }
14737 +
14738 +       p = fidir->fd_hfile + bbot;
14739 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14740 +            fidir->fd_bbot--, p--)
14741 +               if (p->hf_file) {
14742 +                       if (file_inode(p->hf_file))
14743 +                               break;
14744 +                       au_hfput(p, /*execed*/0);
14745 +               }
14746 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14747 +}
14748 +
14749 +/*
14750 + * after branch manipulating, refresh the file.
14751 + */
14752 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14753 +{
14754 +       int err, need_reopen, nbr;
14755 +       aufs_bindex_t bbot, bindex;
14756 +       struct dentry *dentry;
14757 +       struct super_block *sb;
14758 +       struct au_finfo *finfo;
14759 +       struct au_hfile *hfile;
14760 +
14761 +       dentry = file->f_path.dentry;
14762 +       sb = dentry->d_sb;
14763 +       nbr = au_sbbot(sb) + 1;
14764 +       finfo = au_fi(file);
14765 +       if (!finfo->fi_hdir) {
14766 +               hfile = &finfo->fi_htop;
14767 +               AuDebugOn(!hfile->hf_file);
14768 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14769 +               AuDebugOn(bindex < 0);
14770 +               if (bindex != finfo->fi_btop)
14771 +                       au_set_fbtop(file, bindex);
14772 +       } else {
14773 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14774 +               if (unlikely(err))
14775 +                       goto out;
14776 +               au_do_refresh_dir(file);
14777 +       }
14778 +
14779 +       err = 0;
14780 +       need_reopen = 1;
14781 +       if (!au_test_mmapped(file))
14782 +               err = au_file_refresh_by_inode(file, &need_reopen);
14783 +       if (finfo->fi_hdir)
14784 +               /* harmless if err */
14785 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14786 +       if (!err && need_reopen && !d_unlinked(dentry))
14787 +               err = reopen(file);
14788 +       if (!err) {
14789 +               au_update_figen(file);
14790 +               goto out; /* success */
14791 +       }
14792 +
14793 +       /* error, close all lower files */
14794 +       if (finfo->fi_hdir) {
14795 +               bbot = au_fbbot_dir(file);
14796 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14797 +                       au_set_h_fptr(file, bindex, NULL);
14798 +       }
14799 +
14800 +out:
14801 +       return err;
14802 +}
14803 +
14804 +/* common function to regular file and dir */
14805 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14806 +                         int wlock, unsigned int fi_lsc)
14807 +{
14808 +       int err;
14809 +       unsigned int sigen, figen;
14810 +       aufs_bindex_t btop;
14811 +       unsigned char pseudo_link;
14812 +       struct dentry *dentry;
14813 +       struct inode *inode;
14814 +
14815 +       err = 0;
14816 +       dentry = file->f_path.dentry;
14817 +       inode = d_inode(dentry);
14818 +       sigen = au_sigen(dentry->d_sb);
14819 +       fi_write_lock_nested(file, fi_lsc);
14820 +       figen = au_figen(file);
14821 +       if (!fi_lsc)
14822 +               di_write_lock_child(dentry);
14823 +       else
14824 +               di_write_lock_child2(dentry);
14825 +       btop = au_dbtop(dentry);
14826 +       pseudo_link = (btop != au_ibtop(inode));
14827 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14828 +               if (!wlock) {
14829 +                       di_downgrade_lock(dentry, AuLock_IR);
14830 +                       fi_downgrade_lock(file);
14831 +               }
14832 +               goto out; /* success */
14833 +       }
14834 +
14835 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14836 +       if (au_digen_test(dentry, sigen)) {
14837 +               err = au_reval_dpath(dentry, sigen);
14838 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14839 +       }
14840 +
14841 +       if (!err)
14842 +               err = refresh_file(file, reopen);
14843 +       if (!err) {
14844 +               if (!wlock) {
14845 +                       di_downgrade_lock(dentry, AuLock_IR);
14846 +                       fi_downgrade_lock(file);
14847 +               }
14848 +       } else {
14849 +               di_write_unlock(dentry);
14850 +               fi_write_unlock(file);
14851 +       }
14852 +
14853 +out:
14854 +       return err;
14855 +}
14856 +
14857 +/* ---------------------------------------------------------------------- */
14858 +
14859 +/* cf. aufs_nopage() */
14860 +/* for madvise(2) */
14861 +static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
14862 +{
14863 +       folio_unlock(folio);
14864 +       return 0;
14865 +}
14866 +
14867 +/* it will never be called, but necessary to support O_DIRECT */
14868 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14869 +{ BUG(); return 0; }
14870 +
14871 +/* they will never be called. */
14872 +#ifdef CONFIG_AUFS_DEBUG
14873 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14874 +                           loff_t pos, unsigned len,
14875 +                           struct page **pagep, void **fsdata)
14876 +{ AuUnsupport(); return 0; }
14877 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14878 +                         loff_t pos, unsigned len, unsigned copied,
14879 +                         struct page *page, void *fsdata)
14880 +{ AuUnsupport(); return 0; }
14881 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14882 +{ AuUnsupport(); return 0; }
14883 +
14884 +static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
14885 +{ AuUnsupport(); return true; }
14886 +static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
14887 +{ AuUnsupport(); }
14888 +static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
14889 +{ AuUnsupport(); return true; }
14890 +#if 0 /* called by memory compaction regardless file */
14891 +static int aufs_migrate_folio(struct address_space *mapping, struct folio *dst,
14892 +                             struct folio *src, enum migrate_mode mode)
14893 +{ AuUnsupport(); return 0; }
14894 +#endif
14895 +static int aufs_launder_folio(struct folio *folio)
14896 +{ AuUnsupport(); return 0; }
14897 +static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
14898 +                                     size_t count)
14899 +{ AuUnsupport(); return true; }
14900 +static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
14901 +                                   bool *writeback)
14902 +{ AuUnsupport(); }
14903 +static int aufs_error_remove_folio(struct address_space *mapping,
14904 +                                  struct folio *folio)
14905 +{ AuUnsupport(); return 0; }
14906 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14907 +                             sector_t *span)
14908 +{ AuUnsupport(); return 0; }
14909 +static void aufs_swap_deactivate(struct file *file)
14910 +{ AuUnsupport(); }
14911 +static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
14912 +{ AuUnsupport(); return 0; }
14913 +#endif /* CONFIG_AUFS_DEBUG */
14914 +
14915 +const struct address_space_operations aufs_aop = {
14916 +       .read_folio             = aufs_read_folio,
14917 +       .direct_IO              = aufs_direct_IO,
14918 +#ifdef CONFIG_AUFS_DEBUG
14919 +       .writepage              = aufs_writepage,
14920 +       /* no writepages, because of writepage */
14921 +       .dirty_folio            = aufs_dirty_folio,
14922 +       /* no readpages, because of readpage */
14923 +       .write_begin            = aufs_write_begin,
14924 +       .write_end              = aufs_write_end,
14925 +       /* no bmap, no block device */
14926 +       .invalidate_folio       = aufs_invalidate_folio,
14927 +       .release_folio          = aufs_release_folio,
14928 +       /* is fallback_migrate_page ok? */
14929 +       /* .migrate_folio       = aufs_migrate_folio, */
14930 +       .launder_folio          = aufs_launder_folio,
14931 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14932 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14933 +       .error_remove_folio     = aufs_error_remove_folio,
14934 +       .swap_activate          = aufs_swap_activate,
14935 +       .swap_deactivate        = aufs_swap_deactivate,
14936 +       .swap_rw                = aufs_swap_rw
14937 +#endif /* CONFIG_AUFS_DEBUG */
14938 +};
14939 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14940 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14941 +++ linux/fs/aufs/file.h        2024-05-13 17:13:00.618070924 +0200
14942 @@ -0,0 +1,342 @@
14943 +/* SPDX-License-Identifier: GPL-2.0 */
14944 +/*
14945 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14946 + *
14947 + * This program is free software; you can redistribute it and/or modify
14948 + * it under the terms of the GNU General Public License as published by
14949 + * the Free Software Foundation; either version 2 of the License, or
14950 + * (at your option) any later version.
14951 + *
14952 + * This program is distributed in the hope that it will be useful,
14953 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14954 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14955 + * GNU General Public License for more details.
14956 + *
14957 + * You should have received a copy of the GNU General Public License
14958 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14959 + */
14960 +
14961 +/*
14962 + * file operations
14963 + */
14964 +
14965 +#ifndef __AUFS_FILE_H__
14966 +#define __AUFS_FILE_H__
14967 +
14968 +#ifdef __KERNEL__
14969 +
14970 +#include <linux/file.h>
14971 +#include <linux/fs.h>
14972 +#include <linux/mm_types.h>
14973 +#include <linux/poll.h>
14974 +#include "rwsem.h"
14975 +
14976 +struct au_branch;
14977 +struct au_hfile {
14978 +       struct file             *hf_file;
14979 +       struct au_branch        *hf_br;
14980 +};
14981 +
14982 +struct au_vdir;
14983 +struct au_fidir {
14984 +       aufs_bindex_t           fd_bbot;
14985 +       aufs_bindex_t           fd_nent;
14986 +       struct au_vdir          *fd_vdir_cache;
14987 +       struct au_hfile         fd_hfile[];
14988 +};
14989 +
14990 +static inline int au_fidir_sz(int nent)
14991 +{
14992 +       AuDebugOn(nent < 0);
14993 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14994 +}
14995 +
14996 +struct au_finfo {
14997 +       atomic_t                fi_generation;
14998 +
14999 +       struct au_rwsem         fi_rwsem;
15000 +       aufs_bindex_t           fi_btop;
15001 +
15002 +       /* do not union them */
15003 +       struct {                                /* for non-dir */
15004 +               struct au_hfile                 fi_htop;
15005 +               atomic_t                        fi_mmapped;
15006 +       };
15007 +       struct au_fidir         *fi_hdir;       /* for dir only */
15008 +
15009 +       struct hlist_bl_node    fi_hlist;
15010 +       struct file             *fi_file;       /* very ugly */
15011 +       struct rcu_head         rcu;
15012 +} ____cacheline_aligned_in_smp;
15013 +
15014 +/* ---------------------------------------------------------------------- */
15015 +
15016 +/* file.c */
15017 +extern const struct address_space_operations aufs_aop;
15018 +unsigned int au_file_roflags(unsigned int flags);
15019 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15020 +                      struct file *file, int force_wr);
15021 +struct au_do_open_args {
15022 +       int             aopen;
15023 +       int             (*open)(struct file *file, int flags,
15024 +                               struct file *h_file);
15025 +       struct au_fidir *fidir;
15026 +       struct file     *h_file;
15027 +};
15028 +int au_do_open(struct file *file, struct au_do_open_args *args);
15029 +int au_reopen_nondir(struct file *file);
15030 +struct au_pin;
15031 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15032 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15033 +                         int wlock, unsigned int fi_lsc);
15034 +int au_do_flush(struct file *file, fl_owner_t id,
15035 +               int (*flush)(struct file *file, fl_owner_t id));
15036 +
15037 +/* poll.c */
15038 +#ifdef CONFIG_AUFS_POLL
15039 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15040 +#endif
15041 +
15042 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15043 +/* hfsplus.c */
15044 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15045 +                          int force_wr);
15046 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15047 +                   struct file *h_file);
15048 +#else
15049 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15050 +       aufs_bindex_t bindex, int force_wr)
15051 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15052 +          struct file *h_file);
15053 +#endif
15054 +
15055 +/* f_op.c */
15056 +extern const struct file_operations aufs_file_fop;
15057 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15058 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15059 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15060 +
15061 +/* finfo.c */
15062 +void au_hfput(struct au_hfile *hf, int execed);
15063 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15064 +                  struct file *h_file);
15065 +
15066 +void au_update_figen(struct file *file);
15067 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15068 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15069 +
15070 +void au_fi_init_once(void *_fi);
15071 +void au_finfo_fin(struct file *file);
15072 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15073 +
15074 +/* ioctl.c */
15075 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15076 +#ifdef CONFIG_COMPAT
15077 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15078 +                          unsigned long arg);
15079 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15080 +                             unsigned long arg);
15081 +#endif
15082 +
15083 +/* ---------------------------------------------------------------------- */
15084 +
15085 +static inline struct au_finfo *au_fi(struct file *file)
15086 +{
15087 +       return file->private_data;
15088 +}
15089 +
15090 +/* ---------------------------------------------------------------------- */
15091 +
15092 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15093 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15094 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15095 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15096 +/*
15097 +#define fi_read_trylock_nested(f) \
15098 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15099 +#define fi_write_trylock_nested(f) \
15100 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15101 +*/
15102 +
15103 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15104 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15105 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15106 +
15107 +/* lock subclass for finfo */
15108 +enum {
15109 +       AuLsc_FI_1,
15110 +       AuLsc_FI_2
15111 +};
15112 +
15113 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15114 +{
15115 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15116 +}
15117 +
15118 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15119 +{
15120 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15121 +}
15122 +
15123 +/*
15124 + * fi_read_lock_1, fi_write_lock_1,
15125 + * fi_read_lock_2, fi_write_lock_2
15126 + */
15127 +#define AuReadLockFunc(name) \
15128 +static inline void fi_read_lock_##name(struct file *f) \
15129 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15130 +
15131 +#define AuWriteLockFunc(name) \
15132 +static inline void fi_write_lock_##name(struct file *f) \
15133 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15134 +
15135 +#define AuRWLockFuncs(name) \
15136 +       AuReadLockFunc(name) \
15137 +       AuWriteLockFunc(name)
15138 +
15139 +AuRWLockFuncs(1);
15140 +AuRWLockFuncs(2);
15141 +
15142 +#undef AuReadLockFunc
15143 +#undef AuWriteLockFunc
15144 +#undef AuRWLockFuncs
15145 +
15146 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15147 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15148 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15149 +
15150 +/* ---------------------------------------------------------------------- */
15151 +
15152 +/* todo: hard/soft set? */
15153 +static inline aufs_bindex_t au_fbtop(struct file *file)
15154 +{
15155 +       FiMustAnyLock(file);
15156 +       return au_fi(file)->fi_btop;
15157 +}
15158 +
15159 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15160 +{
15161 +       FiMustAnyLock(file);
15162 +       AuDebugOn(!au_fi(file)->fi_hdir);
15163 +       return au_fi(file)->fi_hdir->fd_bbot;
15164 +}
15165 +
15166 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15167 +{
15168 +       FiMustAnyLock(file);
15169 +       AuDebugOn(!au_fi(file)->fi_hdir);
15170 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15171 +}
15172 +
15173 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15174 +{
15175 +       FiMustWriteLock(file);
15176 +       au_fi(file)->fi_btop = bindex;
15177 +}
15178 +
15179 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15180 +{
15181 +       FiMustWriteLock(file);
15182 +       AuDebugOn(!au_fi(file)->fi_hdir);
15183 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15184 +}
15185 +
15186 +static inline void au_set_fvdir_cache(struct file *file,
15187 +                                     struct au_vdir *vdir_cache)
15188 +{
15189 +       FiMustWriteLock(file);
15190 +       AuDebugOn(!au_fi(file)->fi_hdir);
15191 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15192 +}
15193 +
15194 +static inline struct file *au_hf_top(struct file *file)
15195 +{
15196 +       FiMustAnyLock(file);
15197 +       AuDebugOn(au_fi(file)->fi_hdir);
15198 +       return au_fi(file)->fi_htop.hf_file;
15199 +}
15200 +
15201 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15202 +{
15203 +       FiMustAnyLock(file);
15204 +       AuDebugOn(!au_fi(file)->fi_hdir);
15205 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15206 +}
15207 +
15208 +/* todo: memory barrier? */
15209 +static inline unsigned int au_figen(struct file *f)
15210 +{
15211 +       return atomic_read(&au_fi(f)->fi_generation);
15212 +}
15213 +
15214 +static inline void au_set_mmapped(struct file *f)
15215 +{
15216 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15217 +               return;
15218 +       pr_warn("fi_mmapped wrapped around\n");
15219 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15220 +               ;
15221 +}
15222 +
15223 +static inline void au_unset_mmapped(struct file *f)
15224 +{
15225 +       atomic_dec(&au_fi(f)->fi_mmapped);
15226 +}
15227 +
15228 +static inline int au_test_mmapped(struct file *f)
15229 +{
15230 +       return atomic_read(&au_fi(f)->fi_mmapped);
15231 +}
15232 +
15233 +/* customize vma->vm_file */
15234 +
15235 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15236 +                                      struct file *file)
15237 +{
15238 +       struct file *f;
15239 +
15240 +       f = vma->vm_file;
15241 +       get_file(file);
15242 +       vma->vm_file = file;
15243 +       fput(f);
15244 +}
15245 +
15246 +#ifdef CONFIG_MMU
15247 +#define AuDbgVmRegion(file, vma) do {} while (0)
15248 +
15249 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15250 +                                   struct file *file)
15251 +{
15252 +       au_do_vm_file_reset(vma, file);
15253 +}
15254 +#else
15255 +#define AuDbgVmRegion(file, vma) \
15256 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15257 +
15258 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15259 +                                   struct file *file)
15260 +{
15261 +       struct file *f;
15262 +
15263 +       au_do_vm_file_reset(vma, file);
15264 +       f = vma->vm_region->vm_file;
15265 +       get_file(file);
15266 +       vma->vm_region->vm_file = file;
15267 +       fput(f);
15268 +}
15269 +#endif /* CONFIG_MMU */
15270 +
15271 +/* handle vma->vm_prfile */
15272 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15273 +                                   struct file *file)
15274 +{
15275 +       get_file(file);
15276 +       vma->vm_prfile = file;
15277 +#ifndef CONFIG_MMU
15278 +       get_file(file);
15279 +       vma->vm_region->vm_prfile = file;
15280 +#endif
15281 +}
15282 +
15283 +#endif /* __KERNEL__ */
15284 +#endif /* __AUFS_FILE_H__ */
15285 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15286 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15287 +++ linux/fs/aufs/finfo.c       2024-05-13 17:13:00.618070924 +0200
15288 @@ -0,0 +1,149 @@
15289 +// SPDX-License-Identifier: GPL-2.0
15290 +/*
15291 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15292 + *
15293 + * This program is free software; you can redistribute it and/or modify
15294 + * it under the terms of the GNU General Public License as published by
15295 + * the Free Software Foundation; either version 2 of the License, or
15296 + * (at your option) any later version.
15297 + *
15298 + * This program is distributed in the hope that it will be useful,
15299 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15300 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15301 + * GNU General Public License for more details.
15302 + *
15303 + * You should have received a copy of the GNU General Public License
15304 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15305 + */
15306 +
15307 +/*
15308 + * file private data
15309 + */
15310 +
15311 +#include "aufs.h"
15312 +
15313 +void au_hfput(struct au_hfile *hf, int execed)
15314 +{
15315 +       if (execed)
15316 +               allow_write_access(hf->hf_file);
15317 +       fput(hf->hf_file);
15318 +       hf->hf_file = NULL;
15319 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15320 +       hf->hf_br = NULL;
15321 +}
15322 +
15323 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15324 +{
15325 +       struct au_finfo *finfo = au_fi(file);
15326 +       struct au_hfile *hf;
15327 +       struct au_fidir *fidir;
15328 +
15329 +       fidir = finfo->fi_hdir;
15330 +       if (!fidir) {
15331 +               AuDebugOn(finfo->fi_btop != bindex);
15332 +               hf = &finfo->fi_htop;
15333 +       } else
15334 +               hf = fidir->fd_hfile + bindex;
15335 +
15336 +       if (hf && hf->hf_file)
15337 +               au_hfput(hf, vfsub_file_execed(file));
15338 +       if (val) {
15339 +               FiMustWriteLock(file);
15340 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15341 +               hf->hf_file = val;
15342 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15343 +       }
15344 +}
15345 +
15346 +void au_update_figen(struct file *file)
15347 +{
15348 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15349 +       /* smp_mb(); */ /* atomic_set */
15350 +}
15351 +
15352 +/* ---------------------------------------------------------------------- */
15353 +
15354 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15355 +{
15356 +       struct au_fidir *fidir;
15357 +       int nbr;
15358 +
15359 +       nbr = au_sbbot(sb) + 1;
15360 +       if (nbr < 2)
15361 +               nbr = 2; /* initial allocate for 2 branches */
15362 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15363 +       if (fidir) {
15364 +               fidir->fd_bbot = -1;
15365 +               fidir->fd_nent = nbr;
15366 +       }
15367 +
15368 +       return fidir;
15369 +}
15370 +
15371 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15372 +{
15373 +       int err;
15374 +       struct au_fidir *fidir, *p;
15375 +
15376 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15377 +       fidir = finfo->fi_hdir;
15378 +       AuDebugOn(!fidir);
15379 +
15380 +       err = -ENOMEM;
15381 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15382 +                        GFP_NOFS, may_shrink);
15383 +       if (p) {
15384 +               p->fd_nent = nbr;
15385 +               finfo->fi_hdir = p;
15386 +               err = 0;
15387 +       }
15388 +
15389 +       return err;
15390 +}
15391 +
15392 +/* ---------------------------------------------------------------------- */
15393 +
15394 +void au_finfo_fin(struct file *file)
15395 +{
15396 +       struct au_finfo *finfo;
15397 +
15398 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15399 +
15400 +       finfo = au_fi(file);
15401 +       AuDebugOn(finfo->fi_hdir);
15402 +       AuRwDestroy(&finfo->fi_rwsem);
15403 +       au_cache_free_finfo(finfo);
15404 +}
15405 +
15406 +void au_fi_init_once(void *_finfo)
15407 +{
15408 +       struct au_finfo *finfo = _finfo;
15409 +
15410 +       au_rw_init(&finfo->fi_rwsem);
15411 +}
15412 +
15413 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15414 +{
15415 +       int err;
15416 +       struct au_finfo *finfo;
15417 +       struct dentry *dentry;
15418 +
15419 +       err = -ENOMEM;
15420 +       dentry = file->f_path.dentry;
15421 +       finfo = au_cache_alloc_finfo();
15422 +       if (unlikely(!finfo))
15423 +               goto out;
15424 +
15425 +       err = 0;
15426 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15427 +       au_rw_write_lock(&finfo->fi_rwsem);
15428 +       finfo->fi_btop = -1;
15429 +       finfo->fi_hdir = fidir;
15430 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15431 +       /* smp_mb(); */ /* atomic_set */
15432 +
15433 +       file->private_data = finfo;
15434 +
15435 +out:
15436 +       return err;
15437 +}
15438 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15439 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15440 +++ linux/fs/aufs/f_op.c        2024-05-13 17:13:00.618070924 +0200
15441 @@ -0,0 +1,771 @@
15442 +// SPDX-License-Identifier: GPL-2.0
15443 +/*
15444 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15445 + *
15446 + * This program is free software; you can redistribute it and/or modify
15447 + * it under the terms of the GNU General Public License as published by
15448 + * the Free Software Foundation; either version 2 of the License, or
15449 + * (at your option) any later version.
15450 + *
15451 + * This program is distributed in the hope that it will be useful,
15452 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15453 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15454 + * GNU General Public License for more details.
15455 + *
15456 + * You should have received a copy of the GNU General Public License
15457 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15458 + */
15459 +
15460 +/*
15461 + * file and vm operations
15462 + */
15463 +
15464 +#include <linux/aio.h>
15465 +#include <linux/fs_stack.h>
15466 +#include <linux/mman.h>
15467 +#include <linux/security.h>
15468 +#include "aufs.h"
15469 +
15470 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15471 +{
15472 +       int err;
15473 +       aufs_bindex_t bindex;
15474 +       struct dentry *dentry, *h_dentry;
15475 +       struct au_finfo *finfo;
15476 +       struct inode *h_inode;
15477 +
15478 +       FiMustWriteLock(file);
15479 +
15480 +       err = 0;
15481 +       dentry = file->f_path.dentry;
15482 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15483 +       finfo = au_fi(file);
15484 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15485 +       atomic_set(&finfo->fi_mmapped, 0);
15486 +       bindex = au_dbtop(dentry);
15487 +       if (!h_file) {
15488 +               h_dentry = au_h_dptr(dentry, bindex);
15489 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15490 +               if (unlikely(err))
15491 +                       goto out;
15492 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15493 +               if (IS_ERR(h_file)) {
15494 +                       err = PTR_ERR(h_file);
15495 +                       goto out;
15496 +               }
15497 +       } else {
15498 +               h_dentry = h_file->f_path.dentry;
15499 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15500 +               if (unlikely(err))
15501 +                       goto out;
15502 +               /* br ref is already inc-ed */
15503 +       }
15504 +
15505 +       if (flags & __O_TMPFILE) {
15506 +               AuDebugOn(!h_file);
15507 +               AuDebugOn(h_file != au_di(dentry)->di_htmpfile);
15508 +               au_di(dentry)->di_htmpfile = NULL;
15509 +
15510 +               if (!(flags & O_EXCL)) {
15511 +                       h_inode = file_inode(h_file);
15512 +                       spin_lock(&h_inode->i_lock);
15513 +                       h_inode->i_state |= I_LINKABLE;
15514 +                       spin_unlock(&h_inode->i_lock);
15515 +               }
15516 +       }
15517 +       au_set_fbtop(file, bindex);
15518 +       au_set_h_fptr(file, bindex, h_file);
15519 +       au_update_figen(file);
15520 +       /* todo: necessary? */
15521 +       /* file->f_ra = h_file->f_ra; */
15522 +
15523 +out:
15524 +       return err;
15525 +}
15526 +
15527 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15528 +                           struct file *file)
15529 +{
15530 +       int err;
15531 +       struct super_block *sb;
15532 +       struct au_do_open_args args = {
15533 +               .open   = au_do_open_nondir
15534 +       };
15535 +
15536 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15537 +             file, vfsub_file_flags(file), file->f_mode);
15538 +
15539 +       sb = file->f_path.dentry->d_sb;
15540 +       si_read_lock(sb, AuLock_FLUSH);
15541 +       err = au_do_open(file, &args);
15542 +       si_read_unlock(sb);
15543 +       return err;
15544 +}
15545 +
15546 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15547 +{
15548 +       struct au_finfo *finfo;
15549 +       aufs_bindex_t bindex;
15550 +
15551 +       finfo = au_fi(file);
15552 +       au_hbl_del(&finfo->fi_hlist,
15553 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15554 +       bindex = finfo->fi_btop;
15555 +       if (bindex >= 0)
15556 +               au_set_h_fptr(file, bindex, NULL);
15557 +
15558 +       au_finfo_fin(file);
15559 +       return 0;
15560 +}
15561 +
15562 +/* ---------------------------------------------------------------------- */
15563 +
15564 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15565 +{
15566 +       int err;
15567 +       struct file *h_file;
15568 +
15569 +       err = 0;
15570 +       h_file = au_hf_top(file);
15571 +       if (h_file)
15572 +               err = vfsub_flush(h_file, id);
15573 +       return err;
15574 +}
15575 +
15576 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15577 +{
15578 +       return au_do_flush(file, id, au_do_flush_nondir);
15579 +}
15580 +
15581 +/* ---------------------------------------------------------------------- */
15582 +/*
15583 + * read and write functions acquire [fdi]_rwsem once, but release before
15584 + * mmap_sem. This is because to stop a race condition between mmap(2).
15585 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15586 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15587 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15588 + */
15589 +
15590 +/* Callers should call au_read_post() or fput() in the end */
15591 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15592 +{
15593 +       struct file *h_file;
15594 +       int err;
15595 +
15596 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15597 +       if (!err) {
15598 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15599 +               h_file = au_hf_top(file);
15600 +               get_file(h_file);
15601 +               if (!keep_fi)
15602 +                       fi_read_unlock(file);
15603 +       } else
15604 +               h_file = ERR_PTR(err);
15605 +
15606 +       return h_file;
15607 +}
15608 +
15609 +static void au_read_post(struct inode *inode, struct file *h_file)
15610 +{
15611 +       /* update without lock, I don't think it a problem */
15612 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15613 +       fput(h_file);
15614 +}
15615 +
15616 +struct au_write_pre {
15617 +       /* input */
15618 +       unsigned int lsc;
15619 +
15620 +       /* output */
15621 +       blkcnt_t blks;
15622 +       aufs_bindex_t btop;
15623 +};
15624 +
15625 +/*
15626 + * return with iinfo is write-locked
15627 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15628 + * end
15629 + */
15630 +static struct file *au_write_pre(struct file *file, int do_ready,
15631 +                                struct au_write_pre *wpre)
15632 +{
15633 +       struct file *h_file;
15634 +       struct dentry *dentry;
15635 +       int err;
15636 +       unsigned int lsc;
15637 +       struct au_pin pin;
15638 +
15639 +       lsc = 0;
15640 +       if (wpre)
15641 +               lsc = wpre->lsc;
15642 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15643 +       h_file = ERR_PTR(err);
15644 +       if (unlikely(err))
15645 +               goto out;
15646 +
15647 +       dentry = file->f_path.dentry;
15648 +       if (do_ready) {
15649 +               err = au_ready_to_write(file, -1, &pin);
15650 +               if (unlikely(err)) {
15651 +                       h_file = ERR_PTR(err);
15652 +                       di_write_unlock(dentry);
15653 +                       goto out_fi;
15654 +               }
15655 +       }
15656 +
15657 +       di_downgrade_lock(dentry, /*flags*/0);
15658 +       if (wpre)
15659 +               wpre->btop = au_fbtop(file);
15660 +       h_file = au_hf_top(file);
15661 +       get_file(h_file);
15662 +       if (wpre)
15663 +               wpre->blks = file_inode(h_file)->i_blocks;
15664 +       if (do_ready)
15665 +               au_unpin(&pin);
15666 +       di_read_unlock(dentry, /*flags*/0);
15667 +       vfsub_file_start_write(h_file);
15668 +
15669 +out_fi:
15670 +       fi_write_unlock(file);
15671 +out:
15672 +       return h_file;
15673 +}
15674 +
15675 +static void au_write_post(struct inode *inode, struct file *h_file,
15676 +                         struct au_write_pre *wpre, ssize_t written)
15677 +{
15678 +       struct inode *h_inode;
15679 +
15680 +       vfsub_file_end_write(h_file);
15681 +       au_cpup_attr_timesizes(inode);
15682 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15683 +       h_inode = file_inode(h_file);
15684 +       inode->i_mode = h_inode->i_mode;
15685 +       ii_write_unlock(inode);
15686 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15687 +       if (written > 0)
15688 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15689 +                             /*force*/h_inode->i_blocks > wpre->blks);
15690 +       fput(h_file);
15691 +}
15692 +
15693 +/*
15694 + * todo: very ugly
15695 + * it locks both of i_mutex and si_rwsem for read in safe.
15696 + * if the plink maintenance mode continues forever (that is the problem),
15697 + * may loop forever.
15698 + */
15699 +static void au_mtx_and_read_lock(struct inode *inode)
15700 +{
15701 +       int err;
15702 +       struct super_block *sb = inode->i_sb;
15703 +
15704 +       while (1) {
15705 +               inode_lock(inode);
15706 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15707 +               if (!err)
15708 +                       break;
15709 +               inode_unlock(inode);
15710 +               si_read_lock(sb, AuLock_NOPLMW);
15711 +               si_read_unlock(sb);
15712 +       }
15713 +}
15714 +
15715 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15716 +                         struct iov_iter *iov_iter)
15717 +{
15718 +       ssize_t err;
15719 +       struct file *file;
15720 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15721 +
15722 +       err = security_file_permission(h_file, rw);
15723 +       if (unlikely(err))
15724 +               goto out;
15725 +
15726 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15727 +       iter = NULL;
15728 +       if (rw == MAY_READ)
15729 +               iter = h_file->f_op->read_iter;
15730 +       else if (rw == MAY_WRITE)
15731 +               iter = h_file->f_op->write_iter;
15732 +
15733 +       file = kio->ki_filp;
15734 +       kio->ki_filp = h_file;
15735 +       if (iter) {
15736 +               lockdep_off();
15737 +               err = iter(kio, iov_iter);
15738 +               lockdep_on();
15739 +       } else
15740 +               /* currently there is no such fs */
15741 +               WARN_ON_ONCE(1);
15742 +       kio->ki_filp = file;
15743 +
15744 +out:
15745 +       return err;
15746 +}
15747 +
15748 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15749 +{
15750 +       ssize_t err;
15751 +       struct file *file, *h_file;
15752 +       struct inode *inode;
15753 +       struct super_block *sb;
15754 +
15755 +       file = kio->ki_filp;
15756 +       inode = file_inode(file);
15757 +       sb = inode->i_sb;
15758 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15759 +
15760 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15761 +       err = PTR_ERR(h_file);
15762 +       if (IS_ERR(h_file))
15763 +               goto out;
15764 +
15765 +       if (au_test_loopback_kthread()) {
15766 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15767 +               if (file->f_mapping != h_file->f_mapping) {
15768 +                       file->f_mapping = h_file->f_mapping;
15769 +                       smp_mb(); /* unnecessary? */
15770 +               }
15771 +       }
15772 +       fi_read_unlock(file);
15773 +
15774 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15775 +       /* todo: necessary? */
15776 +       /* file->f_ra = h_file->f_ra; */
15777 +       au_read_post(inode, h_file);
15778 +
15779 +out:
15780 +       si_read_unlock(sb);
15781 +       return err;
15782 +}
15783 +
15784 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15785 +{
15786 +       ssize_t err;
15787 +       struct au_write_pre wpre;
15788 +       struct inode *inode;
15789 +       struct file *file, *h_file;
15790 +
15791 +       file = kio->ki_filp;
15792 +       inode = file_inode(file);
15793 +       au_mtx_and_read_lock(inode);
15794 +
15795 +       wpre.lsc = 0;
15796 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15797 +       err = PTR_ERR(h_file);
15798 +       if (IS_ERR(h_file))
15799 +               goto out;
15800 +
15801 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15802 +       au_write_post(inode, h_file, &wpre, err);
15803 +
15804 +out:
15805 +       si_read_unlock(inode->i_sb);
15806 +       inode_unlock(inode);
15807 +       return err;
15808 +}
15809 +
15810 +/*
15811 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15812 + * don't have their own .splice_{read,write} implimentations, and they use
15813 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15814 + * simple converters to f_op->iter_read() and ->iter_write().
15815 + * But we keep our own implementations because some non-mainlined FSes may have
15816 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15817 + * away an opportunity to co-work with aufs from them.
15818 + */
15819 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15820 +                               struct pipe_inode_info *pipe, size_t len,
15821 +                               unsigned int flags)
15822 +{
15823 +       ssize_t err;
15824 +       struct file *h_file;
15825 +       struct inode *inode;
15826 +       struct super_block *sb;
15827 +
15828 +       inode = file_inode(file);
15829 +       sb = inode->i_sb;
15830 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15831 +
15832 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15833 +       err = PTR_ERR(h_file);
15834 +       if (IS_ERR(h_file))
15835 +               goto out;
15836 +
15837 +       err = vfsub_splice_read(h_file, ppos, pipe, len, flags);
15838 +       /* todo: necessary? */
15839 +       /* file->f_ra = h_file->f_ra; */
15840 +       au_read_post(inode, h_file);
15841 +
15842 +out:
15843 +       si_read_unlock(sb);
15844 +       return err;
15845 +}
15846 +
15847 +static ssize_t
15848 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15849 +                 size_t len, unsigned int flags)
15850 +{
15851 +       ssize_t err;
15852 +       struct au_write_pre wpre;
15853 +       struct inode *inode;
15854 +       struct file *h_file;
15855 +
15856 +       inode = file_inode(file);
15857 +       au_mtx_and_read_lock(inode);
15858 +
15859 +       wpre.lsc = 0;
15860 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15861 +       err = PTR_ERR(h_file);
15862 +       if (IS_ERR(h_file))
15863 +               goto out;
15864 +
15865 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15866 +       au_write_post(inode, h_file, &wpre, err);
15867 +
15868 +out:
15869 +       si_read_unlock(inode->i_sb);
15870 +       inode_unlock(inode);
15871 +       return err;
15872 +}
15873 +
15874 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15875 +                          loff_t len)
15876 +{
15877 +       long err;
15878 +       struct au_write_pre wpre;
15879 +       struct inode *inode;
15880 +       struct file *h_file;
15881 +
15882 +       inode = file_inode(file);
15883 +       au_mtx_and_read_lock(inode);
15884 +
15885 +       wpre.lsc = 0;
15886 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15887 +       err = PTR_ERR(h_file);
15888 +       if (IS_ERR(h_file))
15889 +               goto out;
15890 +
15891 +       lockdep_off();
15892 +       err = vfs_fallocate(h_file, mode, offset, len);
15893 +       lockdep_on();
15894 +       /*
15895 +        * we don't need to call file_modifed() here since au_write_post()
15896 +        * is equivalent and copies-up all timestamps and permission bits.
15897 +        */
15898 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15899 +
15900 +out:
15901 +       si_read_unlock(inode->i_sb);
15902 +       inode_unlock(inode);
15903 +       return err;
15904 +}
15905 +
15906 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15907 +                                   struct file *dst, loff_t dst_pos,
15908 +                                   size_t len, unsigned int flags)
15909 +{
15910 +       ssize_t err;
15911 +       struct au_write_pre wpre;
15912 +       enum { SRC, DST };
15913 +       struct {
15914 +               struct inode *inode;
15915 +               struct file *h_file;
15916 +               struct super_block *h_sb;
15917 +       } a[2];
15918 +#define a_src  a[SRC]
15919 +#define a_dst  a[DST]
15920 +
15921 +       err = -EINVAL;
15922 +       a_src.inode = file_inode(src);
15923 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15924 +               goto out;
15925 +       a_dst.inode = file_inode(dst);
15926 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15927 +               goto out;
15928 +
15929 +       au_mtx_and_read_lock(a_dst.inode);
15930 +       /*
15931 +        * in order to match the order in di_write_lock2_{child,parent}(),
15932 +        * use f_path.dentry for this comparison.
15933 +        */
15934 +       if (src->f_path.dentry < dst->f_path.dentry) {
15935 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15936 +               err = PTR_ERR(a_src.h_file);
15937 +               if (IS_ERR(a_src.h_file))
15938 +                       goto out_si;
15939 +
15940 +               wpre.lsc = AuLsc_FI_2;
15941 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15942 +               err = PTR_ERR(a_dst.h_file);
15943 +               if (IS_ERR(a_dst.h_file)) {
15944 +                       au_read_post(a_src.inode, a_src.h_file);
15945 +                       goto out_si;
15946 +               }
15947 +       } else {
15948 +               wpre.lsc = AuLsc_FI_1;
15949 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15950 +               err = PTR_ERR(a_dst.h_file);
15951 +               if (IS_ERR(a_dst.h_file))
15952 +                       goto out_si;
15953 +
15954 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15955 +               err = PTR_ERR(a_src.h_file);
15956 +               if (IS_ERR(a_src.h_file)) {
15957 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15958 +                                     /*written*/0);
15959 +                       goto out_si;
15960 +               }
15961 +       }
15962 +
15963 +       err = -EXDEV;
15964 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15965 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15966 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15967 +               AuDbgFile(src);
15968 +               AuDbgFile(dst);
15969 +               goto out_file;
15970 +       }
15971 +
15972 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15973 +                                   dst_pos, len, flags);
15974 +
15975 +out_file:
15976 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15977 +       fi_read_unlock(src);
15978 +       au_read_post(a_src.inode, a_src.h_file);
15979 +out_si:
15980 +       si_read_unlock(a_dst.inode->i_sb);
15981 +       inode_unlock(a_dst.inode);
15982 +out:
15983 +       return err;
15984 +#undef a_src
15985 +#undef a_dst
15986 +}
15987 +
15988 +/* ---------------------------------------------------------------------- */
15989 +
15990 +/*
15991 + * The locking order around current->mmap_sem.
15992 + * - in most and regular cases
15993 + *   file I/O syscall -- aufs_read() or something
15994 + *     -- si_rwsem for read -- mmap_sem
15995 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15996 + * - in mmap case
15997 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15998 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15999 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16000 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16001 + * It means that when aufs acquires si_rwsem for write, the process should never
16002 + * acquire mmap_sem.
16003 + *
16004 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
16005 + * problem either since any directory is not able to be mmap-ed.
16006 + * The similar scenario is applied to aufs_readlink() too.
16007 + */
16008 +
16009 +#if 0 /* stop calling security_file_mmap() */
16010 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16011 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
16012 +
16013 +static unsigned long au_arch_prot_conv(unsigned long flags)
16014 +{
16015 +       /* currently ppc64 only */
16016 +#ifdef CONFIG_PPC64
16017 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
16018 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16019 +       return AuConv_VM_PROT(flags, SAO);
16020 +#else
16021 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
16022 +       return 0;
16023 +#endif
16024 +}
16025 +
16026 +static unsigned long au_prot_conv(unsigned long flags)
16027 +{
16028 +       return AuConv_VM_PROT(flags, READ)
16029 +               | AuConv_VM_PROT(flags, WRITE)
16030 +               | AuConv_VM_PROT(flags, EXEC)
16031 +               | au_arch_prot_conv(flags);
16032 +}
16033 +
16034 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16035 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16036 +
16037 +static unsigned long au_flag_conv(unsigned long flags)
16038 +{
16039 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16040 +               | AuConv_VM_MAP(flags, DENYWRITE)
16041 +               | AuConv_VM_MAP(flags, LOCKED);
16042 +}
16043 +#endif
16044 +
16045 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16046 +{
16047 +       int err;
16048 +       const unsigned char wlock
16049 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16050 +       struct super_block *sb;
16051 +       struct file *h_file;
16052 +       struct inode *inode;
16053 +
16054 +       AuDbgVmRegion(file, vma);
16055 +
16056 +       inode = file_inode(file);
16057 +       sb = inode->i_sb;
16058 +       lockdep_off();
16059 +       si_read_lock(sb, AuLock_NOPLMW);
16060 +
16061 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16062 +       lockdep_on();
16063 +       err = PTR_ERR(h_file);
16064 +       if (IS_ERR(h_file))
16065 +               goto out;
16066 +
16067 +       err = 0;
16068 +       au_set_mmapped(file);
16069 +       au_vm_file_reset(vma, h_file);
16070 +       /*
16071 +        * we cannot call security_mmap_file() here since it may acquire
16072 +        * mmap_sem or i_mutex.
16073 +        *
16074 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16075 +        *                       au_flag_conv(vma->vm_flags));
16076 +        */
16077 +       if (!err)
16078 +               err = call_mmap(h_file, vma);
16079 +       if (!err) {
16080 +               au_vm_prfile_set(vma, file);
16081 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16082 +               goto out_fput; /* success */
16083 +       }
16084 +       au_unset_mmapped(file);
16085 +       au_vm_file_reset(vma, file);
16086 +
16087 +out_fput:
16088 +       lockdep_off();
16089 +       ii_write_unlock(inode);
16090 +       lockdep_on();
16091 +       fput(h_file);
16092 +out:
16093 +       lockdep_off();
16094 +       si_read_unlock(sb);
16095 +       lockdep_on();
16096 +       AuTraceErr(err);
16097 +       return err;
16098 +}
16099 +
16100 +/* ---------------------------------------------------------------------- */
16101 +
16102 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16103 +                            int datasync)
16104 +{
16105 +       int err;
16106 +       struct au_write_pre wpre;
16107 +       struct inode *inode;
16108 +       struct file *h_file;
16109 +
16110 +       err = 0; /* -EBADF; */ /* posix? */
16111 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16112 +               goto out;
16113 +
16114 +       inode = file_inode(file);
16115 +       au_mtx_and_read_lock(inode);
16116 +
16117 +       wpre.lsc = 0;
16118 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16119 +       err = PTR_ERR(h_file);
16120 +       if (IS_ERR(h_file))
16121 +               goto out_unlock;
16122 +
16123 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16124 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16125 +
16126 +out_unlock:
16127 +       si_read_unlock(inode->i_sb);
16128 +       inode_unlock(inode);
16129 +out:
16130 +       return err;
16131 +}
16132 +
16133 +static int aufs_fasync(int fd, struct file *file, int flag)
16134 +{
16135 +       int err;
16136 +       struct file *h_file;
16137 +       struct super_block *sb;
16138 +
16139 +       sb = file->f_path.dentry->d_sb;
16140 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16141 +
16142 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16143 +       err = PTR_ERR(h_file);
16144 +       if (IS_ERR(h_file))
16145 +               goto out;
16146 +
16147 +       if (h_file->f_op->fasync)
16148 +               err = h_file->f_op->fasync(fd, h_file, flag);
16149 +       fput(h_file); /* instead of au_read_post() */
16150 +
16151 +out:
16152 +       si_read_unlock(sb);
16153 +       return err;
16154 +}
16155 +
16156 +static int aufs_setfl(struct file *file, unsigned long arg)
16157 +{
16158 +       int err;
16159 +       struct file *h_file;
16160 +       struct super_block *sb;
16161 +
16162 +       sb = file->f_path.dentry->d_sb;
16163 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16164 +
16165 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16166 +       err = PTR_ERR(h_file);
16167 +       if (IS_ERR(h_file))
16168 +               goto out;
16169 +
16170 +       /* stop calling h_file->fasync */
16171 +       arg |= vfsub_file_flags(file) & FASYNC;
16172 +       err = setfl(/*unused fd*/-1, h_file, arg);
16173 +       fput(h_file); /* instead of au_read_post() */
16174 +
16175 +out:
16176 +       si_read_unlock(sb);
16177 +       return err;
16178 +}
16179 +
16180 +/* ---------------------------------------------------------------------- */
16181 +
16182 +const struct file_operations aufs_file_fop = {
16183 +       .owner          = THIS_MODULE,
16184 +
16185 +       .llseek         = default_llseek,
16186 +
16187 +       .read_iter      = aufs_read_iter,
16188 +       .write_iter     = aufs_write_iter,
16189 +
16190 +#ifdef CONFIG_AUFS_POLL
16191 +       .poll           = aufs_poll,
16192 +#endif
16193 +       .unlocked_ioctl = aufs_ioctl_nondir,
16194 +#ifdef CONFIG_COMPAT
16195 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16196 +#endif
16197 +       .mmap           = aufs_mmap,
16198 +       .open           = aufs_open_nondir,
16199 +       .flush          = aufs_flush_nondir,
16200 +       .release        = aufs_release_nondir,
16201 +       .fsync          = aufs_fsync_nondir,
16202 +       .fasync         = aufs_fasync,
16203 +       .setfl          = aufs_setfl,
16204 +       .splice_write   = aufs_splice_write,
16205 +       .splice_read    = aufs_splice_read,
16206 +#if 0 /* reserved for future use */
16207 +       .aio_splice_write = aufs_aio_splice_write,
16208 +       .aio_splice_read  = aufs_aio_splice_read,
16209 +#endif
16210 +       .fallocate      = aufs_fallocate,
16211 +       .copy_file_range = aufs_copy_file_range
16212 +};
16213 diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16214 --- /usr/share/empty/fs/aufs/fsctx.c    1970-01-01 01:00:00.000000000 +0100
16215 +++ linux/fs/aufs/fsctx.c       2024-05-13 17:13:00.618070924 +0200
16216 @@ -0,0 +1,1242 @@
16217 +// SPDX-License-Identifier: GPL-2.0
16218 +/*
16219 + * Copyright (C) 2022 Junjiro R. Okajima
16220 + *
16221 + * This program is free software; you can redistribute it and/or modify
16222 + * it under the terms of the GNU General Public License as published by
16223 + * the Free Software Foundation; either version 2 of the License, or
16224 + * (at your option) any later version.
16225 + *
16226 + * This program is distributed in the hope that it will be useful,
16227 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16228 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16229 + * GNU General Public License for more details.
16230 + *
16231 + * You should have received a copy of the GNU General Public License
16232 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16233 + */
16234 +
16235 +/*
16236 + * fs context, aka new mount api
16237 + */
16238 +
16239 +#include <linux/fs_context.h>
16240 +#include "aufs.h"
16241 +
16242 +struct au_fsctx_opts {
16243 +       aufs_bindex_t bindex;
16244 +       unsigned char skipped;
16245 +       struct au_opt *opt, *opt_tail;
16246 +       struct super_block *sb;
16247 +       struct au_sbinfo *sbinfo;
16248 +       struct au_opts opts;
16249 +};
16250 +
16251 +/* stop extra interpretation of errno in mount(8), and strange error messages */
16252 +static int cvt_err(int err)
16253 +{
16254 +       AuTraceErr(err);
16255 +
16256 +       switch (err) {
16257 +       case -ENOENT:
16258 +       case -ENOTDIR:
16259 +       case -EEXIST:
16260 +       case -EIO:
16261 +               err = -EINVAL;
16262 +       }
16263 +       return err;
16264 +}
16265 +
16266 +static int au_fsctx_reconfigure(struct fs_context *fc)
16267 +{
16268 +       int err, do_dx;
16269 +       unsigned int mntflags;
16270 +       struct dentry *root;
16271 +       struct super_block *sb;
16272 +       struct inode *inode;
16273 +       struct au_fsctx_opts *a = fc->fs_private;
16274 +
16275 +       AuDbg("fc %p\n", fc);
16276 +
16277 +       root = fc->root;
16278 +       sb = root->d_sb;
16279 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16280 +       if (!err) {
16281 +               di_write_lock_child(root);
16282 +               err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16283 +               aufs_write_unlock(root);
16284 +       }
16285 +
16286 +       inode = d_inode(root);
16287 +       inode_lock(inode);
16288 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16289 +       if (unlikely(err))
16290 +               goto out;
16291 +       di_write_lock_child(root);
16292 +
16293 +       /* au_opts_remount() may return an error */
16294 +       err = au_opts_remount(sb, &a->opts);
16295 +
16296 +       if (au_ftest_opts(a->opts.flags, REFRESH))
16297 +               au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16298 +                                                    REFRESH_IDOP));
16299 +
16300 +       if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16301 +               mntflags = au_mntflags(sb);
16302 +               do_dx = !!au_opt_test(mntflags, DIO);
16303 +               au_dy_arefresh(do_dx);
16304 +       }
16305 +
16306 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16307 +       aufs_write_unlock(root);
16308 +
16309 +out:
16310 +       inode_unlock(inode);
16311 +       err = cvt_err(err);
16312 +       AuTraceErr(err);
16313 +
16314 +       return err;
16315 +}
16316 +
16317 +/* ---------------------------------------------------------------------- */
16318 +
16319 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16320 +{
16321 +       int err;
16322 +       struct au_fsctx_opts *a = fc->fs_private;
16323 +       struct au_sbinfo *sbinfo = a->sbinfo;
16324 +       struct dentry *root;
16325 +       struct inode *inode;
16326 +
16327 +       sbinfo->si_sb = sb;
16328 +       sb->s_fs_info = sbinfo;
16329 +       kobject_get(&sbinfo->si_kobj);
16330 +
16331 +       __si_write_lock(sb);
16332 +       si_pid_set(sb);
16333 +       au_sbilist_add(sb);
16334 +
16335 +       /* all timestamps always follow the ones on the branch */
16336 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16337 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16338 +       sb->s_op = &aufs_sop;
16339 +       sb->s_d_op = &aufs_dop;
16340 +       sb->s_magic = AUFS_SUPER_MAGIC;
16341 +       sb->s_maxbytes = 0;
16342 +       sb->s_stack_depth = 1;
16343 +       au_export_init(sb);
16344 +       au_xattr_init(sb);
16345 +
16346 +       err = au_alloc_root(sb);
16347 +       if (unlikely(err)) {
16348 +               si_write_unlock(sb);
16349 +               goto out;
16350 +       }
16351 +       root = sb->s_root;
16352 +       inode = d_inode(root);
16353 +       ii_write_lock_parent(inode);
16354 +       aufs_write_unlock(root);
16355 +
16356 +       /* lock vfs_inode first, then aufs. */
16357 +       inode_lock(inode);
16358 +       aufs_write_lock(root);
16359 +       err = au_opts_mount(sb, &a->opts);
16360 +       AuTraceErr(err);
16361 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16362 +               sb->s_d_op = &aufs_dop_noreval;
16363 +               /* infofc(fc, "%ps", sb->s_d_op); */
16364 +               pr_info("%ps\n", sb->s_d_op);
16365 +               au_refresh_dop(root, /*force_reval*/0);
16366 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
16367 +               au_refresh_iop(inode, /*force_getattr*/0);
16368 +       }
16369 +       aufs_write_unlock(root);
16370 +       inode_unlock(inode);
16371 +       if (!err)
16372 +               goto out; /* success */
16373 +
16374 +       dput(root);
16375 +       sb->s_root = NULL;
16376 +
16377 +out:
16378 +       if (unlikely(err))
16379 +               kobject_put(&sbinfo->si_kobj);
16380 +       AuTraceErr(err);
16381 +       err = cvt_err(err);
16382 +       AuTraceErr(err);
16383 +       return err;
16384 +}
16385 +
16386 +static int au_fsctx_get_tree(struct fs_context *fc)
16387 +{
16388 +       int err;
16389 +
16390 +       AuDbg("fc %p\n", fc);
16391 +       err = get_tree_nodev(fc, au_fsctx_fill_super);
16392 +
16393 +       AuTraceErr(err);
16394 +       return err;
16395 +}
16396 +
16397 +/* ---------------------------------------------------------------------- */
16398 +
16399 +static void au_fsctx_dump(struct au_opts *opts)
16400 +{
16401 +#ifdef CONFIG_AUFS_DEBUG
16402 +       /* reduce stack space */
16403 +       union {
16404 +               struct au_opt_add *add;
16405 +               struct au_opt_del *del;
16406 +               struct au_opt_mod *mod;
16407 +               struct au_opt_xino *xino;
16408 +               struct au_opt_xino_itrunc *xino_itrunc;
16409 +               struct au_opt_wbr_create *create;
16410 +       } u;
16411 +       struct au_opt *opt;
16412 +
16413 +       opt = opts->opt;
16414 +       while (opt->type != Opt_tail) {
16415 +               switch (opt->type) {
16416 +               case Opt_add:
16417 +                       u.add = &opt->add;
16418 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16419 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16420 +                                 u.add->path.dentry);
16421 +                       break;
16422 +               case Opt_del:
16423 +                       fallthrough;
16424 +               case Opt_idel:
16425 +                       u.del = &opt->del;
16426 +                       AuDbg("del {%s, %p}\n",
16427 +                             u.del->pathname, u.del->h_path.dentry);
16428 +                       break;
16429 +               case Opt_mod:
16430 +                       fallthrough;
16431 +               case Opt_imod:
16432 +                       u.mod = &opt->mod;
16433 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16434 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16435 +                       break;
16436 +               case Opt_append:
16437 +                       u.add = &opt->add;
16438 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16439 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16440 +                                 u.add->path.dentry);
16441 +                       break;
16442 +               case Opt_prepend:
16443 +                       u.add = &opt->add;
16444 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16445 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16446 +                                 u.add->path.dentry);
16447 +                       break;
16448 +
16449 +               case Opt_dirwh:
16450 +                       AuDbg("dirwh %d\n", opt->dirwh);
16451 +                       break;
16452 +               case Opt_rdcache:
16453 +                       AuDbg("rdcache %d\n", opt->rdcache);
16454 +                       break;
16455 +               case Opt_rdblk:
16456 +                       AuDbg("rdblk %d\n", opt->rdblk);
16457 +                       break;
16458 +               case Opt_rdhash:
16459 +                       AuDbg("rdhash %u\n", opt->rdhash);
16460 +                       break;
16461 +
16462 +               case Opt_xino:
16463 +                       u.xino = &opt->xino;
16464 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16465 +                       break;
16466 +
16467 +#define au_fsctx_TF(name)                                        \
16468 +                       case Opt_##name:                          \
16469 +                               if (opt->tf)                      \
16470 +                                       AuLabel(name);            \
16471 +                               else                              \
16472 +                                       AuLabel(no##name);        \
16473 +                               break;
16474 +
16475 +               /* simple true/false flag */
16476 +               au_fsctx_TF(trunc_xino);
16477 +               au_fsctx_TF(trunc_xib);
16478 +               au_fsctx_TF(dirperm1);
16479 +               au_fsctx_TF(plink);
16480 +               au_fsctx_TF(shwh);
16481 +               au_fsctx_TF(dio);
16482 +               au_fsctx_TF(warn_perm);
16483 +               au_fsctx_TF(verbose);
16484 +               au_fsctx_TF(sum);
16485 +               au_fsctx_TF(dirren);
16486 +               au_fsctx_TF(acl);
16487 +#undef au_fsctx_TF
16488 +
16489 +               case Opt_trunc_xino_path:
16490 +                       fallthrough;
16491 +               case Opt_itrunc_xino:
16492 +                       u.xino_itrunc = &opt->xino_itrunc;
16493 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16494 +                       break;
16495 +               case Opt_noxino:
16496 +                       AuLabel(noxino);
16497 +                       break;
16498 +
16499 +               case Opt_list_plink:
16500 +                       AuLabel(list_plink);
16501 +                       break;
16502 +               case Opt_udba:
16503 +                       AuDbg("udba %d, %s\n",
16504 +                                 opt->udba, au_optstr_udba(opt->udba));
16505 +                       break;
16506 +               case Opt_diropq_a:
16507 +                       AuLabel(diropq_a);
16508 +                       break;
16509 +               case Opt_diropq_w:
16510 +                       AuLabel(diropq_w);
16511 +                       break;
16512 +               case Opt_wsum:
16513 +                       AuLabel(wsum);
16514 +                       break;
16515 +               case Opt_wbr_create:
16516 +                       u.create = &opt->wbr_create;
16517 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
16518 +                                 au_optstr_wbr_create(u.create->wbr_create));
16519 +                       switch (u.create->wbr_create) {
16520 +                       case AuWbrCreate_MFSV:
16521 +                               fallthrough;
16522 +                       case AuWbrCreate_PMFSV:
16523 +                               AuDbg("%d sec\n", u.create->mfs_second);
16524 +                               break;
16525 +                       case AuWbrCreate_MFSRR:
16526 +                               fallthrough;
16527 +                       case AuWbrCreate_TDMFS:
16528 +                               AuDbg("%llu watermark\n",
16529 +                                         u.create->mfsrr_watermark);
16530 +                               break;
16531 +                       case AuWbrCreate_MFSRRV:
16532 +                               fallthrough;
16533 +                       case AuWbrCreate_TDMFSV:
16534 +                               fallthrough;
16535 +                       case AuWbrCreate_PMFSRRV:
16536 +                               AuDbg("%llu watermark, %d sec\n",
16537 +                                         u.create->mfsrr_watermark,
16538 +                                         u.create->mfs_second);
16539 +                               break;
16540 +                       }
16541 +                       break;
16542 +               case Opt_wbr_copyup:
16543 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16544 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
16545 +                       break;
16546 +               case Opt_fhsm_sec:
16547 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16548 +                       break;
16549 +
16550 +               default:
16551 +                       AuDbg("type %d\n", opt->type);
16552 +                       BUG();
16553 +               }
16554 +               opt++;
16555 +       }
16556 +#endif
16557 +}
16558 +
16559 +/* ---------------------------------------------------------------------- */
16560 +
16561 +/*
16562 + * For conditionally compiled mount options.
16563 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16564 + */
16565 +#define au_ignore_flag(name, action)           \
16566 +       fsparam_flag(name, action),             \
16567 +       fsparam_flag("no" name, Opt_ignore_silent)
16568 +
16569 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16570 +       fsparam_string("br", Opt_br),
16571 +
16572 +       /* "add=%d:%s" or "ins=%d:%s" */
16573 +       fsparam_string("add", Opt_add),
16574 +       fsparam_string("ins", Opt_add),
16575 +       fsparam_path("append", Opt_append),
16576 +       fsparam_path("prepend", Opt_prepend),
16577 +
16578 +       fsparam_path("del", Opt_del),
16579 +       /* fsparam_s32("idel", Opt_idel), */
16580 +       fsparam_path("mod", Opt_mod),
16581 +       /* fsparam_string("imod", Opt_imod), */
16582 +
16583 +       fsparam_s32("dirwh", Opt_dirwh),
16584 +
16585 +       fsparam_path("xino", Opt_xino),
16586 +       fsparam_flag("noxino", Opt_noxino),
16587 +       fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16588 +       /* "trunc_xino_v=%d:%d" */
16589 +       /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16590 +       fsparam_path("trunc_xino", Opt_trunc_xino_path),
16591 +       fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16592 +       /* fsparam_path("zxino", Opt_zxino), */
16593 +       fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16594 +
16595 +#ifdef CONFIG_PROC_FS
16596 +       fsparam_flag_no("plink", Opt_plink),
16597 +#else
16598 +       au_ignore_flag("plink", Opt_ignore),
16599 +#endif
16600 +
16601 +#ifdef CONFIG_AUFS_DEBUG
16602 +       fsparam_flag("list_plink", Opt_list_plink),
16603 +#endif
16604 +
16605 +       fsparam_string("udba", Opt_udba),
16606 +
16607 +       fsparam_flag_no("dio", Opt_dio),
16608 +
16609 +#ifdef CONFIG_AUFS_DIRREN
16610 +       fsparam_flag_no("dirren", Opt_dirren),
16611 +#else
16612 +       au_ignore_flag("dirren", Opt_ignore),
16613 +#endif
16614 +
16615 +#ifdef CONFIG_AUFS_FHSM
16616 +       fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16617 +#else
16618 +       fsparam_s32("fhsm_sec", Opt_ignore),
16619 +#endif
16620 +
16621 +       /* always | a | whiteouted | w */
16622 +       fsparam_string("diropq", Opt_diropq),
16623 +
16624 +       fsparam_flag_no("warn_perm", Opt_warn_perm),
16625 +
16626 +#ifdef CONFIG_AUFS_SHWH
16627 +       fsparam_flag_no("shwh", Opt_shwh),
16628 +#else
16629 +       au_ignore_flag("shwh", Opt_err),
16630 +#endif
16631 +
16632 +       fsparam_flag_no("dirperm1", Opt_dirperm1),
16633 +
16634 +       fsparam_flag_no("verbose", Opt_verbose),
16635 +       fsparam_flag("v", Opt_verbose),
16636 +       fsparam_flag("quiet", Opt_noverbose),
16637 +       fsparam_flag("q", Opt_noverbose),
16638 +       /* user-space may handle this */
16639 +       fsparam_flag("silent", Opt_noverbose),
16640 +
16641 +       fsparam_flag_no("sum", Opt_sum),
16642 +       fsparam_flag("wsum", Opt_wsum),
16643 +
16644 +       fsparam_s32("rdcache", Opt_rdcache),
16645 +       /* "def" or s32 */
16646 +       fsparam_string("rdblk", Opt_rdblk),
16647 +       /* "def" or s32 */
16648 +       fsparam_string("rdhash", Opt_rdhash),
16649 +
16650 +       fsparam_string("create", Opt_wbr_create),
16651 +       fsparam_string("create_policy", Opt_wbr_create),
16652 +       fsparam_string("cpup", Opt_wbr_copyup),
16653 +       fsparam_string("copyup", Opt_wbr_copyup),
16654 +       fsparam_string("copyup_policy", Opt_wbr_copyup),
16655 +
16656 +       /* generic VFS flag */
16657 +#ifdef CONFIG_FS_POSIX_ACL
16658 +       fsparam_flag_no("acl", Opt_acl),
16659 +#else
16660 +       au_ignore_flag("acl", Opt_ignore),
16661 +#endif
16662 +
16663 +       /* internal use for the scripts */
16664 +       fsparam_string("si", Opt_ignore_silent),
16665 +
16666 +       /* obsoleted, keep them temporary */
16667 +       fsparam_flag("nodlgt", Opt_ignore_silent),
16668 +       fsparam_flag("clean_plink", Opt_ignore),
16669 +       fsparam_string("dirs", Opt_br),
16670 +       fsparam_u32("debug", Opt_ignore),
16671 +       /* "whiteout" or "all" */
16672 +       fsparam_string("delete", Opt_ignore),
16673 +       fsparam_string("imap", Opt_ignore),
16674 +
16675 +       /* temporary workaround, due to old mount(8)? */
16676 +       fsparam_flag("relatime", Opt_ignore_silent),
16677 +
16678 +       {}
16679 +};
16680 +
16681 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16682 +                                char *brspec, size_t speclen,
16683 +                                aufs_bindex_t bindex)
16684 +{
16685 +       int err;
16686 +       char *p;
16687 +
16688 +       AuDbg("brspec %s\n", brspec);
16689 +
16690 +       err = -ENOMEM;
16691 +       if (!speclen)
16692 +               speclen = strlen(brspec);
16693 +       /* will be freed by au_fsctx_free() */
16694 +       p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16695 +       if (unlikely(!p)) {
16696 +               errorfc(fc, "failed in %s", brspec);
16697 +               goto out;
16698 +       }
16699 +       err = au_opt_add(opt, p, fc->sb_flags, bindex);
16700 +
16701 +out:
16702 +       AuTraceErr(err);
16703 +       return err;
16704 +}
16705 +
16706 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16707 +{
16708 +       int err;
16709 +       char *p;
16710 +       struct au_fsctx_opts *a = fc->fs_private;
16711 +       struct au_opt *opt = a->opt;
16712 +       aufs_bindex_t bindex = a->bindex;
16713 +
16714 +       AuDbg("brspec %s\n", brspec);
16715 +
16716 +       err = -EINVAL;
16717 +       while ((p = strsep(&brspec, ":")) && *p) {
16718 +               err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16719 +               AuTraceErr(err);
16720 +               if (unlikely(err))
16721 +                       break;
16722 +               bindex++;
16723 +               opt++;
16724 +               if (unlikely(opt > a->opt_tail)) {
16725 +                       err = -E2BIG;
16726 +                       bindex--;
16727 +                       opt--;
16728 +                       break;
16729 +               }
16730 +               opt->type = Opt_tail;
16731 +               a->skipped = 1;
16732 +       }
16733 +       a->bindex = bindex;
16734 +       a->opt = opt;
16735 +
16736 +       AuTraceErr(err);
16737 +       return err;
16738 +}
16739 +
16740 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16741 +{
16742 +       int err, n;
16743 +       char *p;
16744 +       struct au_fsctx_opts *a = fc->fs_private;
16745 +       struct au_opt *opt = a->opt;
16746 +
16747 +       err = -EINVAL;
16748 +       p = strchr(addspec, ':');
16749 +       if (unlikely(!p)) {
16750 +               errorfc(fc, "bad arg in %s", addspec);
16751 +               goto out;
16752 +       }
16753 +       *p++ = '\0';
16754 +       err = kstrtoint(addspec, 0, &n);
16755 +       if (unlikely(err)) {
16756 +               errorfc(fc, "bad integer in %s", addspec);
16757 +               goto out;
16758 +       }
16759 +       AuDbg("n %d\n", n);
16760 +       err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16761 +
16762 +out:
16763 +       AuTraceErr(err);
16764 +       return err;
16765 +}
16766 +
16767 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16768 +                             struct fs_parameter *param)
16769 +{
16770 +       int err;
16771 +
16772 +       err = -ENOMEM;
16773 +       /* will be freed by au_fsctx_free() */
16774 +       del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16775 +       if (unlikely(!del->pathname))
16776 +               goto out;
16777 +       AuDbg("del %s\n", del->pathname);
16778 +       err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16779 +       if (unlikely(err))
16780 +               errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16781 +
16782 +out:
16783 +       AuTraceErr(err);
16784 +       return err;
16785 +}
16786 +
16787 +#if 0 /* reserved for future use */
16788 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16789 +                              aufs_bindex_t bindex)
16790 +{
16791 +       int err;
16792 +       struct super_block *sb;
16793 +       struct dentry *root;
16794 +       struct au_fsctx_opts *a = fc->fs_private;
16795 +
16796 +       sb = a->sb;
16797 +       AuDebugOn(!sb);
16798 +
16799 +       err = -EINVAL;
16800 +       root = sb->s_root;
16801 +       aufs_read_lock(root, AuLock_FLUSH);
16802 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
16803 +               errorfc(fc, "out of bounds, %d", bindex);
16804 +               goto out;
16805 +       }
16806 +
16807 +       err = 0;
16808 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
16809 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16810 +
16811 +out:
16812 +       aufs_read_unlock(root, !AuLock_IR);
16813 +       AuTraceErr(err);
16814 +       return err;
16815 +}
16816 +#endif
16817 +
16818 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16819 +                             struct fs_parameter *param)
16820 +{
16821 +       int err;
16822 +       struct path path;
16823 +       char *p;
16824 +
16825 +       err = -ENOMEM;
16826 +       /* will be freed by au_fsctx_free() */
16827 +       mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16828 +       if (unlikely(!mod->path))
16829 +               goto out;
16830 +
16831 +       err = -EINVAL;
16832 +       p = strchr(mod->path, '=');
16833 +       if (unlikely(!p)) {
16834 +               errorfc(fc, "no permission %s", mod->path);
16835 +               goto out;
16836 +       }
16837 +
16838 +       *p++ = 0;
16839 +       err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16840 +       if (unlikely(err)) {
16841 +               errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16842 +               goto out;
16843 +       }
16844 +
16845 +       mod->perm = au_br_perm_val(p);
16846 +       AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16847 +       mod->h_root = dget(path.dentry);
16848 +       path_put(&path);
16849 +
16850 +out:
16851 +       AuTraceErr(err);
16852 +       return err;
16853 +}
16854 +
16855 +#if 0 /* reserved for future use */
16856 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16857 +                              char *ibrspec)
16858 +{
16859 +       int err, n;
16860 +       char *p;
16861 +       struct super_block *sb;
16862 +       struct dentry *root;
16863 +       struct au_fsctx_opts *a = fc->fs_private;
16864 +
16865 +       sb = a->sb;
16866 +       AuDebugOn(!sb);
16867 +
16868 +       err = -EINVAL;
16869 +       p = strchr(ibrspec, ':');
16870 +       if (unlikely(!p)) {
16871 +               errorfc(fc, "no index, %s", ibrspec);
16872 +               goto out;
16873 +       }
16874 +       *p++ = '\0';
16875 +       err = kstrtoint(ibrspec, 0, &n);
16876 +       if (unlikely(err)) {
16877 +               errorfc(fc, "bad integer in %s", ibrspec);
16878 +               goto out;
16879 +       }
16880 +       AuDbg("n %d\n", n);
16881 +
16882 +       root = sb->s_root;
16883 +       aufs_read_lock(root, AuLock_FLUSH);
16884 +       if (n < 0 || au_sbbot(sb) < n) {
16885 +               errorfc(fc, "out of bounds, %d", bindex);
16886 +               goto out_root;
16887 +       }
16888 +
16889 +       err = 0;
16890 +       mod->perm = au_br_perm_val(p);
16891 +       AuDbg("mod path %s, perm 0x%x, %s\n",
16892 +             mod->path, mod->perm, p);
16893 +       mod->h_root = dget(au_h_dptr(root, bindex));
16894 +
16895 +out_root:
16896 +       aufs_read_unlock(root, !AuLock_IR);
16897 +out:
16898 +       AuTraceErr(err);
16899 +       return err;
16900 +}
16901 +#endif
16902 +
16903 +static int au_fsctx_parse_xino(struct fs_context *fc,
16904 +                              struct au_opt_xino *xino,
16905 +                              struct fs_parameter *param)
16906 +{
16907 +       int err;
16908 +       struct au_fsctx_opts *a = fc->fs_private;
16909 +
16910 +       err = -ENOMEM;
16911 +       /* will be freed by au_opts_free() */
16912 +       xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16913 +       if (unlikely(!xino->path))
16914 +               goto out;
16915 +       AuDbg("path %s\n", xino->path);
16916 +
16917 +       xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16918 +                                   /*wbrtop*/0);
16919 +       err = PTR_ERR(xino->file);
16920 +       if (IS_ERR(xino->file)) {
16921 +               xino->file = NULL;
16922 +               goto out;
16923 +       }
16924 +
16925 +       err = 0;
16926 +       if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16927 +               err = -EINVAL;
16928 +               errorfc(fc, "%s must be outside", xino->path);
16929 +       }
16930 +
16931 +out:
16932 +       AuTraceErr(err);
16933 +       return err;
16934 +}
16935 +
16936 +static
16937 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16938 +                                   struct au_opt_xino_itrunc *xino_itrunc,
16939 +                                   char *pathname)
16940 +{
16941 +       int err;
16942 +       aufs_bindex_t bbot, bindex;
16943 +       struct path path;
16944 +       struct dentry *root;
16945 +       struct au_fsctx_opts *a = fc->fs_private;
16946 +
16947 +       AuDebugOn(!a->sb);
16948 +
16949 +       err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16950 +       if (unlikely(err)) {
16951 +               errorfc(fc, "lookup failed %s (%d)", pathname, err);
16952 +               goto out;
16953 +       }
16954 +
16955 +       xino_itrunc->bindex = -1;
16956 +       root = a->sb->s_root;
16957 +       aufs_read_lock(root, AuLock_FLUSH);
16958 +       bbot = au_sbbot(a->sb);
16959 +       for (bindex = 0; bindex <= bbot; bindex++) {
16960 +               if (au_h_dptr(root, bindex) == path.dentry) {
16961 +                       xino_itrunc->bindex = bindex;
16962 +                       break;
16963 +               }
16964 +       }
16965 +       aufs_read_unlock(root, !AuLock_IR);
16966 +       path_put(&path);
16967 +
16968 +       if (unlikely(xino_itrunc->bindex < 0)) {
16969 +               err = -EINVAL;
16970 +               errorfc(fc, "no such branch %s", pathname);
16971 +       }
16972 +
16973 +out:
16974 +       AuTraceErr(err);
16975 +       return err;
16976 +}
16977 +
16978 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16979 +                                     struct au_opt_xino_itrunc *xino_itrunc,
16980 +                                     unsigned int bindex)
16981 +{
16982 +       int err;
16983 +       aufs_bindex_t bbot;
16984 +       struct super_block *sb;
16985 +       struct au_fsctx_opts *a = fc->fs_private;
16986 +
16987 +       sb = a->sb;
16988 +       AuDebugOn(!sb);
16989 +
16990 +       err = 0;
16991 +       si_noflush_read_lock(sb);
16992 +       bbot = au_sbbot(sb);
16993 +       si_read_unlock(sb);
16994 +       if (bindex <= bbot)
16995 +               xino_itrunc->bindex = bindex;
16996 +       else {
16997 +               err = -EINVAL;
16998 +               errorfc(fc, "out of bounds, %u", bindex);
16999 +       }
17000 +
17001 +       AuTraceErr(err);
17002 +       return err;
17003 +}
17004 +
17005 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
17006 +{
17007 +       int err, token;
17008 +       struct fs_parse_result result;
17009 +       struct au_fsctx_opts *a = fc->fs_private;
17010 +       struct au_opt *opt = a->opt;
17011 +
17012 +       AuDbg("fc %p, param {key %s, string %s}\n",
17013 +             fc, param->key, param->string);
17014 +       err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
17015 +       if (unlikely(err < 0))
17016 +               goto out;
17017 +       token = err;
17018 +       AuDbg("token %d, res{negated %d, uint64 %llu}\n",
17019 +             token, result.negated, result.uint_64);
17020 +
17021 +       err = -EINVAL;
17022 +       a->skipped = 0;
17023 +       switch (token) {
17024 +       case Opt_br:
17025 +               err = au_fsctx_parse_br(fc, param->string);
17026 +               break;
17027 +       case Opt_add:
17028 +               err = au_fsctx_parse_add(fc, param->string);
17029 +               break;
17030 +       case Opt_append:
17031 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17032 +                                           /*dummy bindex*/1);
17033 +               break;
17034 +       case Opt_prepend:
17035 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17036 +                                           /*bindex*/0);
17037 +               break;
17038 +
17039 +       case Opt_del:
17040 +               err = au_fsctx_parse_del(fc, &opt->del, param);
17041 +               break;
17042 +#if 0 /* reserved for future use */
17043 +       case Opt_idel:
17044 +               if (!a->sb) {
17045 +                       err = 0;
17046 +                       a->skipped = 1;
17047 +                       break;
17048 +               }
17049 +               del->pathname = "(indexed)";
17050 +               err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17051 +               break;
17052 +#endif
17053 +
17054 +       case Opt_mod:
17055 +               err = au_fsctx_parse_mod(fc, &opt->mod, param);
17056 +               break;
17057 +#ifdef IMOD /* reserved for future use */
17058 +       case Opt_imod:
17059 +               if (!a->sb) {
17060 +                       err = 0;
17061 +                       a->skipped = 1;
17062 +                       break;
17063 +               }
17064 +               u.mod->path = "(indexed)";
17065 +               err = au_opts_parse_imod(fc, &opt->mod, param->string);
17066 +               break;
17067 +#endif
17068 +
17069 +       case Opt_xino:
17070 +               err = au_fsctx_parse_xino(fc, &opt->xino, param);
17071 +               break;
17072 +       case Opt_trunc_xino_path:
17073 +               if (!a->sb) {
17074 +                       errorfc(fc, "no such branch %s", param->string);
17075 +                       break;
17076 +               }
17077 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17078 +                                                     param->string);
17079 +               break;
17080 +#if 0
17081 +       case Opt_trunc_xino_v:
17082 +               if (!a->sb) {
17083 +                       err = 0;
17084 +                       a->skipped = 1;
17085 +                       break;
17086 +               }
17087 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17088 +                                                     param->string);
17089 +               break;
17090 +#endif
17091 +       case Opt_itrunc_xino:
17092 +               if (!a->sb) {
17093 +                       errorfc(fc, "out of bounds %s", param->string);
17094 +                       break;
17095 +               }
17096 +               err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17097 +                                                result.int_32);
17098 +               break;
17099 +
17100 +       case Opt_dirwh:
17101 +               err = 0;
17102 +               opt->dirwh = result.int_32;
17103 +               break;
17104 +
17105 +       case Opt_rdcache:
17106 +               if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17107 +                       errorfc(fc, "rdcache must be smaller than %d",
17108 +                               AUFS_RDCACHE_MAX);
17109 +                       break;
17110 +               }
17111 +               err = 0;
17112 +               opt->rdcache = result.int_32;
17113 +               break;
17114 +
17115 +       case Opt_rdblk:
17116 +               err = 0;
17117 +               opt->rdblk = AUFS_RDBLK_DEF;
17118 +               if (!strcmp(param->string, "def"))
17119 +                       break;
17120 +
17121 +               err = kstrtoint(param->string, 0, &result.int_32);
17122 +               if (unlikely(err)) {
17123 +                       errorfc(fc, "bad value in %s", param->key);
17124 +                       break;
17125 +               }
17126 +               err = -EINVAL;
17127 +               if (unlikely(result.int_32 < 0
17128 +                            || result.int_32 > KMALLOC_MAX_SIZE)) {
17129 +                       errorfc(fc, "bad value in %s", param->key);
17130 +                       break;
17131 +               }
17132 +               if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17133 +                       errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17134 +                       break;
17135 +               }
17136 +               err = 0;
17137 +               opt->rdblk = result.int_32;
17138 +               break;
17139 +
17140 +       case Opt_rdhash:
17141 +               err = 0;
17142 +               opt->rdhash = AUFS_RDHASH_DEF;
17143 +               if (!strcmp(param->string, "def"))
17144 +                       break;
17145 +
17146 +               err = kstrtoint(param->string, 0, &result.int_32);
17147 +               if (unlikely(err)) {
17148 +                       errorfc(fc, "bad value in %s", param->key);
17149 +                       break;
17150 +               }
17151 +               /* how about zero? */
17152 +               if (result.int_32 < 0
17153 +                   || result.int_32 * sizeof(struct hlist_head)
17154 +                   > KMALLOC_MAX_SIZE) {
17155 +                       err = -EINVAL;
17156 +                       errorfc(fc, "bad integer in %s", param->key);
17157 +                       break;
17158 +               }
17159 +               opt->rdhash = result.int_32;
17160 +               break;
17161 +
17162 +       case Opt_diropq:
17163 +               /*
17164 +                * As other options, fs/aufs/opts.c can handle these strings by
17165 +                * match_token().  But "diropq=" is deprecated now and will
17166 +                * never have other value.  So simple strcmp() is enough here.
17167 +                */
17168 +               if (!strcmp(param->string, "a") ||
17169 +                   !strcmp(param->string, "always")) {
17170 +                       err = 0;
17171 +                       opt->type = Opt_diropq_a;
17172 +               } else if (!strcmp(param->string, "w") ||
17173 +                          !strcmp(param->string, "whiteouted")) {
17174 +                       err = 0;
17175 +                       opt->type = Opt_diropq_w;
17176 +               } else
17177 +                       errorfc(fc, "unknown value %s", param->string);
17178 +               break;
17179 +
17180 +       case Opt_udba:
17181 +               opt->udba = au_udba_val(param->string);
17182 +               if (opt->udba >= 0)
17183 +                       err = 0;
17184 +               else
17185 +                       errorf(fc, "wrong value, %s", param->string);
17186 +               break;
17187 +
17188 +       case Opt_wbr_create:
17189 +               opt->wbr_create.wbr_create
17190 +                       = au_wbr_create_val(param->string, &opt->wbr_create);
17191 +               if (opt->wbr_create.wbr_create >= 0)
17192 +                       err = 0;
17193 +               else
17194 +                       errorf(fc, "wrong value, %s", param->key);
17195 +               break;
17196 +
17197 +       case Opt_wbr_copyup:
17198 +               opt->wbr_copyup = au_wbr_copyup_val(param->string);
17199 +               if (opt->wbr_copyup >= 0)
17200 +                       err = 0;
17201 +               else
17202 +                       errorfc(fc, "wrong value, %s", param->key);
17203 +               break;
17204 +
17205 +       case Opt_fhsm_sec:
17206 +               if (unlikely(result.int_32 < 0)) {
17207 +                       errorfc(fc, "bad integer in %s\n", param->key);
17208 +                       break;
17209 +               }
17210 +               err = 0;
17211 +               if (sysaufs_brs)
17212 +                       opt->fhsm_second = result.int_32;
17213 +               else
17214 +                       warnfc(fc, "ignored %s %s", param->key, param->string);
17215 +               break;
17216 +
17217 +       /* simple true/false flag */
17218 +#define au_fsctx_TF(name)                              \
17219 +               case Opt_##name:                        \
17220 +                       err = 0;                        \
17221 +                       opt->tf = !result.negated;      \
17222 +                       break
17223 +       au_fsctx_TF(trunc_xino);
17224 +       au_fsctx_TF(trunc_xib);
17225 +       au_fsctx_TF(dirperm1);
17226 +       au_fsctx_TF(plink);
17227 +       au_fsctx_TF(shwh);
17228 +       au_fsctx_TF(dio);
17229 +       au_fsctx_TF(warn_perm);
17230 +       au_fsctx_TF(verbose);
17231 +       au_fsctx_TF(sum);
17232 +       au_fsctx_TF(dirren);
17233 +       au_fsctx_TF(acl);
17234 +#undef au_fsctx_TF
17235 +
17236 +       case Opt_noverbose:
17237 +               err = 0;
17238 +               opt->type = Opt_verbose;
17239 +               opt->tf = false;
17240 +               break;
17241 +
17242 +       case Opt_noxino:
17243 +               fallthrough;
17244 +       case Opt_list_plink:
17245 +               fallthrough;
17246 +       case Opt_wsum:
17247 +               err = 0;
17248 +               break;
17249 +
17250 +       case Opt_ignore:
17251 +               warnfc(fc, "ignored %s", param->key);
17252 +               fallthrough;
17253 +       case Opt_ignore_silent:
17254 +               a->skipped = 1;
17255 +               err = 0;
17256 +               break;
17257 +       default:
17258 +               a->skipped = 1;
17259 +               err = -ENOPARAM;
17260 +               break;
17261 +       }
17262 +       if (unlikely(err))
17263 +               goto out;
17264 +       if (a->skipped)
17265 +               goto out;
17266 +
17267 +       switch (token) {
17268 +       case Opt_br:
17269 +               fallthrough;
17270 +       case Opt_noverbose:
17271 +               fallthrough;
17272 +       case Opt_diropq:
17273 +               break;
17274 +       default:
17275 +               opt->type = token;
17276 +               break;
17277 +       }
17278 +       opt++;
17279 +       if (unlikely(opt > a->opt_tail)) {
17280 +               err = -E2BIG;
17281 +               opt--;
17282 +       }
17283 +       opt->type = Opt_tail;
17284 +       a->opt = opt;
17285 +
17286 +out:
17287 +       return err;
17288 +}
17289 +
17290 +/*
17291 + * these options accept both 'name=val' and 'name:val' form.
17292 + * some accept optional '=' in its value.
17293 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17294 + */
17295 +static inline unsigned int is_colonopt(char *str)
17296 +{
17297 +#define do_test(name)                                  \
17298 +       if (!strncmp(str, name ":", sizeof(name)))      \
17299 +               return sizeof(name) - 1
17300 +       do_test("br");
17301 +       do_test("add");
17302 +       do_test("ins");
17303 +       do_test("append");
17304 +       do_test("prepend");
17305 +       do_test("del");
17306 +       /* do_test("idel"); */
17307 +       do_test("mod");
17308 +       /* do_test("imod"); */
17309 +#undef do_test
17310 +
17311 +       return 0;
17312 +}
17313 +
17314 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17315 +{
17316 +       int err;
17317 +       unsigned int u;
17318 +       char *str;
17319 +       struct au_fsctx_opts *a = fc->fs_private;
17320 +
17321 +       str = data;
17322 +       AuDbg("str %s\n", str);
17323 +       while (str) {
17324 +               u = is_colonopt(str);
17325 +               if (u)
17326 +                       str[u] = '=';
17327 +               str = strchr(str, ',');
17328 +               if (!str)
17329 +                       break;
17330 +               str++;
17331 +       }
17332 +       str = data;
17333 +       AuDbg("str %s\n", str);
17334 +
17335 +       err = generic_parse_monolithic(fc, str);
17336 +       AuTraceErr(err);
17337 +       au_fsctx_dump(&a->opts);
17338 +
17339 +       return err;
17340 +}
17341 +
17342 +/* ---------------------------------------------------------------------- */
17343 +
17344 +static void au_fsctx_opts_free(struct au_opts *opts)
17345 +{
17346 +       struct au_opt *opt;
17347 +
17348 +       opt = opts->opt;
17349 +       while (opt->type != Opt_tail) {
17350 +               switch (opt->type) {
17351 +               case Opt_add:
17352 +                       fallthrough;
17353 +               case Opt_append:
17354 +                       fallthrough;
17355 +               case Opt_prepend:
17356 +                       kfree(opt->add.pathname);
17357 +                       path_put(&opt->add.path);
17358 +                       break;
17359 +               case Opt_del:
17360 +                       kfree(opt->del.pathname);
17361 +                       fallthrough;
17362 +               case Opt_idel:
17363 +                       path_put(&opt->del.h_path);
17364 +                       break;
17365 +               case Opt_mod:
17366 +                       kfree(opt->mod.path);
17367 +                       fallthrough;
17368 +               case Opt_imod:
17369 +                       dput(opt->mod.h_root);
17370 +                       break;
17371 +               case Opt_xino:
17372 +                       kfree(opt->xino.path);
17373 +                       fput(opt->xino.file);
17374 +                       break;
17375 +               }
17376 +               opt++;
17377 +       }
17378 +}
17379 +
17380 +static void au_fsctx_free(struct fs_context *fc)
17381 +{
17382 +       struct au_fsctx_opts *a = fc->fs_private;
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 +       kobject_put(&a->sbinfo->si_kobj);
17389 +       au_fsctx_opts_free(&a->opts);
17390 +       free_page((unsigned long)a->opts.opt);
17391 +       au_kfree_rcu(a);
17392 +}
17393 +
17394 +static const struct fs_context_operations au_fsctx_ops = {
17395 +       .free                   = au_fsctx_free,
17396 +       .parse_param            = au_fsctx_parse_param,
17397 +       .parse_monolithic       = au_fsctx_parse_monolithic,
17398 +       .get_tree               = au_fsctx_get_tree,
17399 +       .reconfigure            = au_fsctx_reconfigure
17400 +       /*
17401 +        * nfs4 requires ->dup()? No.
17402 +        * I don't know what is this ->dup() for.
17403 +        */
17404 +};
17405 +
17406 +int aufs_fsctx_init(struct fs_context *fc)
17407 +{
17408 +       int err;
17409 +       struct au_fsctx_opts *a;
17410 +
17411 +       /* fs_type=%p, root=%pD */
17412 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17413 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17414 +
17415 +       /* they will be freed by au_fsctx_free() */
17416 +       err = -ENOMEM;
17417 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17418 +       if (unlikely(!a))
17419 +               goto out;
17420 +       a->bindex = 0;
17421 +       a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17422 +       if (unlikely(!a->opts.opt))
17423 +               goto out_a;
17424 +       a->opt = a->opts.opt;
17425 +       a->opt->type = Opt_tail;
17426 +       a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17427 +       a->opt_tail = a->opt + a->opts.max_opt - 1;
17428 +       a->opts.sb_flags = fc->sb_flags;
17429 +
17430 +       a->sb = NULL;
17431 +       if (fc->root) {
17432 +               AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17433 +               a->opts.flags = AuOpts_REMOUNT;
17434 +               a->sb = fc->root->d_sb;
17435 +               a->sbinfo = au_sbi(a->sb);
17436 +               kobject_get(&a->sbinfo->si_kobj);
17437 +       } else {
17438 +               a->sbinfo = au_si_alloc(a->sb);
17439 +               AuDebugOn(!a->sbinfo);
17440 +               err = PTR_ERR(a->sbinfo);
17441 +               if (IS_ERR(a->sbinfo))
17442 +                       goto out_opt;
17443 +               au_rw_write_unlock(&a->sbinfo->si_rwsem);
17444 +       }
17445 +
17446 +       err = 0;
17447 +       fc->fs_private = a;
17448 +       fc->ops = &au_fsctx_ops;
17449 +       goto out; /* success */
17450 +
17451 +out_opt:
17452 +       free_page((unsigned long)a->opts.opt);
17453 +out_a:
17454 +       au_kfree_rcu(a);
17455 +out:
17456 +       AuTraceErr(err);
17457 +       return err;
17458 +}
17459 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
17460 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
17461 +++ linux/fs/aufs/fstype.h      2024-05-13 17:13:00.618070924 +0200
17462 @@ -0,0 +1,401 @@
17463 +/* SPDX-License-Identifier: GPL-2.0 */
17464 +/*
17465 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17466 + *
17467 + * This program is free software; you can redistribute it and/or modify
17468 + * it under the terms of the GNU General Public License as published by
17469 + * the Free Software Foundation; either version 2 of the License, or
17470 + * (at your option) any later version.
17471 + *
17472 + * This program is distributed in the hope that it will be useful,
17473 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17474 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17475 + * GNU General Public License for more details.
17476 + *
17477 + * You should have received a copy of the GNU General Public License
17478 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17479 + */
17480 +
17481 +/*
17482 + * judging filesystem type
17483 + */
17484 +
17485 +#ifndef __AUFS_FSTYPE_H__
17486 +#define __AUFS_FSTYPE_H__
17487 +
17488 +#ifdef __KERNEL__
17489 +
17490 +#include <linux/fs.h>
17491 +#include <linux/magic.h>
17492 +#include <linux/nfs_fs.h>
17493 +#include <linux/romfs_fs.h>
17494 +
17495 +static inline int au_test_aufs(struct super_block *sb)
17496 +{
17497 +       return sb->s_magic == AUFS_SUPER_MAGIC;
17498 +}
17499 +
17500 +static inline const char *au_sbtype(struct super_block *sb)
17501 +{
17502 +       return sb->s_type->name;
17503 +}
17504 +
17505 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17506 +{
17507 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17508 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
17509 +#else
17510 +       return 0;
17511 +#endif
17512 +}
17513 +
17514 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17515 +{
17516 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17517 +       return sb->s_magic == ROMFS_MAGIC;
17518 +#else
17519 +       return 0;
17520 +#endif
17521 +}
17522 +
17523 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17524 +{
17525 +#if IS_ENABLED(CONFIG_CRAMFS)
17526 +       return sb->s_magic == CRAMFS_MAGIC;
17527 +#endif
17528 +       return 0;
17529 +}
17530 +
17531 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17532 +{
17533 +#if IS_ENABLED(CONFIG_NFS_FS)
17534 +       return sb->s_magic == NFS_SUPER_MAGIC;
17535 +#else
17536 +       return 0;
17537 +#endif
17538 +}
17539 +
17540 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17541 +{
17542 +#if IS_ENABLED(CONFIG_FUSE_FS)
17543 +       return sb->s_magic == FUSE_SUPER_MAGIC;
17544 +#else
17545 +       return 0;
17546 +#endif
17547 +}
17548 +
17549 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17550 +{
17551 +#if IS_ENABLED(CONFIG_XFS_FS)
17552 +       return sb->s_magic == XFS_SB_MAGIC;
17553 +#else
17554 +       return 0;
17555 +#endif
17556 +}
17557 +
17558 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17559 +{
17560 +#ifdef CONFIG_TMPFS
17561 +       return sb->s_magic == TMPFS_MAGIC;
17562 +#else
17563 +       return 0;
17564 +#endif
17565 +}
17566 +
17567 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17568 +{
17569 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17570 +       return !strcmp(au_sbtype(sb), "ecryptfs");
17571 +#else
17572 +       return 0;
17573 +#endif
17574 +}
17575 +
17576 +static inline int au_test_ramfs(struct super_block *sb)
17577 +{
17578 +       return sb->s_magic == RAMFS_MAGIC;
17579 +}
17580 +
17581 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17582 +{
17583 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17584 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
17585 +#else
17586 +       return 0;
17587 +#endif
17588 +}
17589 +
17590 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17591 +{
17592 +#ifdef CONFIG_PROC_FS
17593 +       return sb->s_magic == PROC_SUPER_MAGIC;
17594 +#else
17595 +       return 0;
17596 +#endif
17597 +}
17598 +
17599 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17600 +{
17601 +#ifdef CONFIG_SYSFS
17602 +       return sb->s_magic == SYSFS_MAGIC;
17603 +#else
17604 +       return 0;
17605 +#endif
17606 +}
17607 +
17608 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17609 +{
17610 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17611 +       return sb->s_magic == CONFIGFS_MAGIC;
17612 +#else
17613 +       return 0;
17614 +#endif
17615 +}
17616 +
17617 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17618 +{
17619 +#if IS_ENABLED(CONFIG_MINIX_FS)
17620 +       return sb->s_magic == MINIX3_SUPER_MAGIC
17621 +               || sb->s_magic == MINIX2_SUPER_MAGIC
17622 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
17623 +               || sb->s_magic == MINIX_SUPER_MAGIC
17624 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
17625 +#else
17626 +       return 0;
17627 +#endif
17628 +}
17629 +
17630 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17631 +{
17632 +#if IS_ENABLED(CONFIG_FAT_FS)
17633 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
17634 +#else
17635 +       return 0;
17636 +#endif
17637 +}
17638 +
17639 +static inline int au_test_msdos(struct super_block *sb)
17640 +{
17641 +       return au_test_fat(sb);
17642 +}
17643 +
17644 +static inline int au_test_vfat(struct super_block *sb)
17645 +{
17646 +       return au_test_fat(sb);
17647 +}
17648 +
17649 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17650 +{
17651 +#ifdef CONFIG_SECURITYFS
17652 +       return sb->s_magic == SECURITYFS_MAGIC;
17653 +#else
17654 +       return 0;
17655 +#endif
17656 +}
17657 +
17658 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17659 +{
17660 +#if IS_ENABLED(CONFIG_SQUASHFS)
17661 +       return sb->s_magic == SQUASHFS_MAGIC;
17662 +#else
17663 +       return 0;
17664 +#endif
17665 +}
17666 +
17667 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17668 +{
17669 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17670 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
17671 +#else
17672 +       return 0;
17673 +#endif
17674 +}
17675 +
17676 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17677 +{
17678 +#if IS_ENABLED(CONFIG_XENFS)
17679 +       return sb->s_magic == XENFS_SUPER_MAGIC;
17680 +#else
17681 +       return 0;
17682 +#endif
17683 +}
17684 +
17685 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17686 +{
17687 +#ifdef CONFIG_DEBUG_FS
17688 +       return sb->s_magic == DEBUGFS_MAGIC;
17689 +#else
17690 +       return 0;
17691 +#endif
17692 +}
17693 +
17694 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17695 +{
17696 +#if IS_ENABLED(CONFIG_NILFS)
17697 +       return sb->s_magic == NILFS_SUPER_MAGIC;
17698 +#else
17699 +       return 0;
17700 +#endif
17701 +}
17702 +
17703 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17704 +{
17705 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17706 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17707 +#else
17708 +       return 0;
17709 +#endif
17710 +}
17711 +
17712 +/* ---------------------------------------------------------------------- */
17713 +/*
17714 + * they can't be an aufs branch.
17715 + */
17716 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17717 +{
17718 +       return
17719 +#ifndef CONFIG_AUFS_BR_RAMFS
17720 +               au_test_ramfs(sb) ||
17721 +#endif
17722 +               au_test_procfs(sb)
17723 +               || au_test_sysfs(sb)
17724 +               || au_test_configfs(sb)
17725 +               || au_test_debugfs(sb)
17726 +               || au_test_securityfs(sb)
17727 +               || au_test_xenfs(sb)
17728 +               || au_test_ecryptfs(sb)
17729 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
17730 +               || au_test_aufs(sb); /* will be supported in next version */
17731 +}
17732 +
17733 +static inline int au_test_fs_remote(struct super_block *sb)
17734 +{
17735 +       return !au_test_tmpfs(sb)
17736 +#ifdef CONFIG_AUFS_BR_RAMFS
17737 +               && !au_test_ramfs(sb)
17738 +#endif
17739 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17740 +}
17741 +
17742 +/* ---------------------------------------------------------------------- */
17743 +
17744 +/*
17745 + * Note: these functions (below) are created after reading ->getattr() in all
17746 + * filesystems under linux/fs. it means we have to do so in every update...
17747 + */
17748 +
17749 +/*
17750 + * some filesystems require getattr to refresh the inode attributes before
17751 + * referencing.
17752 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17753 + * and leave the work for d_revalidate()
17754 + */
17755 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17756 +{
17757 +       return au_test_nfs(sb)
17758 +               || au_test_fuse(sb)
17759 +               /* || au_test_btrfs(sb) */      /* untested */
17760 +               ;
17761 +}
17762 +
17763 +/*
17764 + * filesystems which don't maintain i_size or i_blocks.
17765 + */
17766 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17767 +{
17768 +       return au_test_xfs(sb)
17769 +               || au_test_btrfs(sb)
17770 +               || au_test_ubifs(sb)
17771 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
17772 +               /* || au_test_minix(sb) */      /* untested */
17773 +               ;
17774 +}
17775 +
17776 +/*
17777 + * filesystems which don't store the correct value in some of their inode
17778 + * attributes.
17779 + */
17780 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17781 +{
17782 +       return au_test_fs_bad_iattr_size(sb)
17783 +               || au_test_fat(sb)
17784 +               || au_test_msdos(sb)
17785 +               || au_test_vfat(sb);
17786 +}
17787 +
17788 +/* they don't check i_nlink in link(2) */
17789 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17790 +{
17791 +       return au_test_tmpfs(sb)
17792 +#ifdef CONFIG_AUFS_BR_RAMFS
17793 +               || au_test_ramfs(sb)
17794 +#endif
17795 +               || au_test_ubifs(sb)
17796 +               || au_test_hfsplus(sb);
17797 +}
17798 +
17799 +/*
17800 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17801 + */
17802 +static inline int au_test_fs_notime(struct super_block *sb)
17803 +{
17804 +       return au_test_nfs(sb)
17805 +               || au_test_fuse(sb)
17806 +               || au_test_ubifs(sb)
17807 +               ;
17808 +}
17809 +
17810 +/* temporary support for i#1 in cramfs */
17811 +static inline int au_test_fs_unique_ino(struct inode *inode)
17812 +{
17813 +       if (au_test_cramfs(inode->i_sb))
17814 +               return inode->i_ino != 1;
17815 +       return 1;
17816 +}
17817 +
17818 +/* ---------------------------------------------------------------------- */
17819 +
17820 +/*
17821 + * the filesystem where the xino files placed must support i/o after unlink and
17822 + * maintain i_size and i_blocks.
17823 + */
17824 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17825 +{
17826 +       return au_test_fs_remote(sb)
17827 +               || au_test_fs_bad_iattr_size(sb)
17828 +               /* don't want unnecessary work for xino */
17829 +               || au_test_aufs(sb)
17830 +               || au_test_ecryptfs(sb)
17831 +               || au_test_nilfs(sb);
17832 +}
17833 +
17834 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17835 +{
17836 +       return au_test_tmpfs(sb)
17837 +               || au_test_ramfs(sb);
17838 +}
17839 +
17840 +/*
17841 + * test if the @sb is real-readonly.
17842 + */
17843 +static inline int au_test_fs_rr(struct super_block *sb)
17844 +{
17845 +       return au_test_squashfs(sb)
17846 +               || au_test_iso9660(sb)
17847 +               || au_test_cramfs(sb)
17848 +               || au_test_romfs(sb);
17849 +}
17850 +
17851 +/*
17852 + * test if the @inode is nfs with 'noacl' option
17853 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17854 + */
17855 +static inline int au_test_nfs_noacl(struct inode *inode)
17856 +{
17857 +       return au_test_nfs(inode->i_sb)
17858 +               /* && IS_POSIXACL(inode) */
17859 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
17860 +}
17861 +
17862 +#endif /* __KERNEL__ */
17863 +#endif /* __AUFS_FSTYPE_H__ */
17864 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
17865 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
17866 +++ linux/fs/aufs/hbl.h 2024-05-13 17:13:00.618070924 +0200
17867 @@ -0,0 +1,65 @@
17868 +/* SPDX-License-Identifier: GPL-2.0 */
17869 +/*
17870 + * Copyright (C) 2017-2022 Junjiro R. Okajima
17871 + *
17872 + * This program is free software; you can redistribute it and/or modify
17873 + * it under the terms of the GNU General Public License as published by
17874 + * the Free Software Foundation; either version 2 of the License, or
17875 + * (at your option) any later version.
17876 + *
17877 + * This program is distributed in the hope that it will be useful,
17878 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17879 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17880 + * GNU General Public License for more details.
17881 + *
17882 + * You should have received a copy of the GNU General Public License
17883 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17884 + */
17885 +
17886 +/*
17887 + * helpers for hlist_bl.h
17888 + */
17889 +
17890 +#ifndef __AUFS_HBL_H__
17891 +#define __AUFS_HBL_H__
17892 +
17893 +#ifdef __KERNEL__
17894 +
17895 +#include <linux/list_bl.h>
17896 +
17897 +static inline void au_hbl_add(struct hlist_bl_node *node,
17898 +                             struct hlist_bl_head *hbl)
17899 +{
17900 +       hlist_bl_lock(hbl);
17901 +       hlist_bl_add_head(node, hbl);
17902 +       hlist_bl_unlock(hbl);
17903 +}
17904 +
17905 +static inline void au_hbl_del(struct hlist_bl_node *node,
17906 +                             struct hlist_bl_head *hbl)
17907 +{
17908 +       hlist_bl_lock(hbl);
17909 +       hlist_bl_del(node);
17910 +       hlist_bl_unlock(hbl);
17911 +}
17912 +
17913 +#define au_hbl_for_each(pos, head)                                     \
17914 +       for (pos = hlist_bl_first(head);                                \
17915 +            pos;                                                       \
17916 +            pos = pos->next)
17917 +
17918 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17919 +{
17920 +       unsigned long cnt;
17921 +       struct hlist_bl_node *pos;
17922 +
17923 +       cnt = 0;
17924 +       hlist_bl_lock(hbl);
17925 +       au_hbl_for_each(pos, hbl)
17926 +               cnt++;
17927 +       hlist_bl_unlock(hbl);
17928 +       return cnt;
17929 +}
17930 +
17931 +#endif /* __KERNEL__ */
17932 +#endif /* __AUFS_HBL_H__ */
17933 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
17934 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
17935 +++ linux/fs/aufs/hfsnotify.c   2024-05-13 17:13:00.618070924 +0200
17936 @@ -0,0 +1,290 @@
17937 +// SPDX-License-Identifier: GPL-2.0
17938 +/*
17939 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17940 + *
17941 + * This program is free software; you can redistribute it and/or modify
17942 + * it under the terms of the GNU General Public License as published by
17943 + * the Free Software Foundation; either version 2 of the License, or
17944 + * (at your option) any later version.
17945 + *
17946 + * This program is distributed in the hope that it will be useful,
17947 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17948 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17949 + * GNU General Public License for more details.
17950 + *
17951 + * You should have received a copy of the GNU General Public License
17952 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17953 + */
17954 +
17955 +/*
17956 + * fsnotify for the lower directories
17957 + */
17958 +
17959 +#include "aufs.h"
17960 +
17961 +/* FS_IN_IGNORED is unnecessary */
17962 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17963 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
17964 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17965 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17966 +
17967 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17968 +{
17969 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17970 +                                            hn_mark);
17971 +       /* AuDbg("here\n"); */
17972 +       au_cache_free_hnotify(hn);
17973 +       smp_mb__before_atomic(); /* for atomic64_dec */
17974 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
17975 +               wake_up(&au_hfsn_wq);
17976 +}
17977 +
17978 +static int au_hfsn_alloc(struct au_hinode *hinode)
17979 +{
17980 +       int err;
17981 +       struct au_hnotify *hn;
17982 +       struct super_block *sb;
17983 +       struct au_branch *br;
17984 +       struct fsnotify_mark *mark;
17985 +       aufs_bindex_t bindex;
17986 +
17987 +       hn = hinode->hi_notify;
17988 +       sb = hn->hn_aufs_inode->i_sb;
17989 +       bindex = au_br_index(sb, hinode->hi_id);
17990 +       br = au_sbr(sb, bindex);
17991 +       AuDebugOn(!br->br_hfsn);
17992 +
17993 +       mark = &hn->hn_mark;
17994 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17995 +       mark->mask = AuHfsnMask;
17996 +       /*
17997 +        * by udba rename or rmdir, aufs assign a new inode to the known
17998 +        * h_inode, so specify 1 to allow dups.
17999 +        */
18000 +       lockdep_off();
18001 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
18002 +       lockdep_on();
18003 +
18004 +       return err;
18005 +}
18006 +
18007 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
18008 +{
18009 +       struct fsnotify_mark *mark;
18010 +       unsigned long long ull;
18011 +       struct fsnotify_group *group;
18012 +
18013 +       ull = atomic64_inc_return(&au_hfsn_ifree);
18014 +       BUG_ON(!ull);
18015 +
18016 +       mark = &hn->hn_mark;
18017 +       spin_lock(&mark->lock);
18018 +       group = mark->group;
18019 +       fsnotify_get_group(group);
18020 +       spin_unlock(&mark->lock);
18021 +       lockdep_off();
18022 +       fsnotify_destroy_mark(mark, group);
18023 +       fsnotify_put_mark(mark);
18024 +       fsnotify_put_group(group);
18025 +       lockdep_on();
18026 +
18027 +       /* free hn by myself */
18028 +       return 0;
18029 +}
18030 +
18031 +/* ---------------------------------------------------------------------- */
18032 +
18033 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
18034 +{
18035 +       struct fsnotify_mark *mark;
18036 +
18037 +       mark = &hinode->hi_notify->hn_mark;
18038 +       spin_lock(&mark->lock);
18039 +       if (do_set) {
18040 +               AuDebugOn(mark->mask & AuHfsnMask);
18041 +               mark->mask |= AuHfsnMask;
18042 +       } else {
18043 +               AuDebugOn(!(mark->mask & AuHfsnMask));
18044 +               mark->mask &= ~AuHfsnMask;
18045 +       }
18046 +       spin_unlock(&mark->lock);
18047 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
18048 +}
18049 +
18050 +/* ---------------------------------------------------------------------- */
18051 +
18052 +/* #define AuDbgHnotify */
18053 +#ifdef AuDbgHnotify
18054 +static char *au_hfsn_name(u32 mask)
18055 +{
18056 +#ifdef CONFIG_AUFS_DEBUG
18057 +#define test_ret(flag)                         \
18058 +       do {                                    \
18059 +               if (mask & flag)                \
18060 +                       return #flag;           \
18061 +       } while (0)
18062 +       test_ret(FS_ACCESS);
18063 +       test_ret(FS_MODIFY);
18064 +       test_ret(FS_ATTRIB);
18065 +       test_ret(FS_CLOSE_WRITE);
18066 +       test_ret(FS_CLOSE_NOWRITE);
18067 +       test_ret(FS_OPEN);
18068 +       test_ret(FS_MOVED_FROM);
18069 +       test_ret(FS_MOVED_TO);
18070 +       test_ret(FS_CREATE);
18071 +       test_ret(FS_DELETE);
18072 +       test_ret(FS_DELETE_SELF);
18073 +       test_ret(FS_MOVE_SELF);
18074 +       test_ret(FS_UNMOUNT);
18075 +       test_ret(FS_Q_OVERFLOW);
18076 +       test_ret(FS_IN_IGNORED);
18077 +       test_ret(FS_ISDIR);
18078 +       test_ret(FS_IN_ONESHOT);
18079 +       test_ret(FS_EVENT_ON_CHILD);
18080 +       return "";
18081 +#undef test_ret
18082 +#else
18083 +       return "??";
18084 +#endif
18085 +}
18086 +#endif
18087 +
18088 +/* ---------------------------------------------------------------------- */
18089 +
18090 +static void au_hfsn_free_group(struct fsnotify_group *group)
18091 +{
18092 +       struct au_br_hfsnotify *hfsn = group->private;
18093 +
18094 +       /* AuDbg("here\n"); */
18095 +       au_kfree_try_rcu(hfsn);
18096 +}
18097 +
18098 +static int au_hfsn_handle_event(struct fsnotify_group *group,
18099 +                               u32 mask, const void *data, int data_type,
18100 +                               struct inode *dir,
18101 +                               const struct qstr *file_name, u32 cookie,
18102 +                               struct fsnotify_iter_info *iter_info)
18103 +{
18104 +       int err;
18105 +       struct au_hnotify *hnotify;
18106 +       struct inode *h_dir, *h_inode;
18107 +       struct fsnotify_mark *inode_mark;
18108 +
18109 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18110 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
18111 +
18112 +       err = 0;
18113 +       /* if FS_UNMOUNT happens, there must be another bug */
18114 +       AuDebugOn(mask & FS_UNMOUNT);
18115 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
18116 +               goto out;
18117 +
18118 +       h_dir = dir;
18119 +       h_inode = NULL;
18120 +#ifdef AuDbgHnotify
18121 +       au_debug_on();
18122 +       if (1 || file_name.len != sizeof(AUFS_XINO_FNAME) - 1
18123 +           || strncmp(file_name.name, AUFS_XINO_FNAME, file_name.len)) {
18124 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18125 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
18126 +                     AuLNPair(file_name), h_inode ? h_inode->i_ino : 0);
18127 +               /* WARN_ON(1); */
18128 +       }
18129 +       au_debug_off();
18130 +#endif
18131 +
18132 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
18133 +       AuDebugOn(!inode_mark);
18134 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
18135 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
18136 +
18137 +out:
18138 +       return err;
18139 +}
18140 +
18141 +static struct fsnotify_ops au_hfsn_ops = {
18142 +       .handle_event           = au_hfsn_handle_event,
18143 +       .free_group_priv        = au_hfsn_free_group,
18144 +       .free_mark              = au_hfsn_free_mark
18145 +};
18146 +
18147 +/* ---------------------------------------------------------------------- */
18148 +
18149 +static void au_hfsn_fin_br(struct au_branch *br)
18150 +{
18151 +       struct au_br_hfsnotify *hfsn;
18152 +
18153 +       hfsn = br->br_hfsn;
18154 +       if (hfsn) {
18155 +               lockdep_off();
18156 +               fsnotify_put_group(hfsn->hfsn_group);
18157 +               lockdep_on();
18158 +       }
18159 +}
18160 +
18161 +static int au_hfsn_init_br(struct au_branch *br, int perm)
18162 +{
18163 +       int err;
18164 +       struct fsnotify_group *group;
18165 +       struct au_br_hfsnotify *hfsn;
18166 +
18167 +       err = 0;
18168 +       br->br_hfsn = NULL;
18169 +       if (!au_br_hnotifyable(perm))
18170 +               goto out;
18171 +
18172 +       err = -ENOMEM;
18173 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18174 +       if (unlikely(!hfsn))
18175 +               goto out;
18176 +
18177 +       err = 0;
18178 +       group = fsnotify_alloc_group(&au_hfsn_ops,
18179 +                                    /*flags - not for userspace*/0);
18180 +       if (IS_ERR(group)) {
18181 +               err = PTR_ERR(group);
18182 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
18183 +               goto out_hfsn;
18184 +       }
18185 +
18186 +       group->private = hfsn;
18187 +       hfsn->hfsn_group = group;
18188 +       br->br_hfsn = hfsn;
18189 +       goto out; /* success */
18190 +
18191 +out_hfsn:
18192 +       au_kfree_try_rcu(hfsn);
18193 +out:
18194 +       return err;
18195 +}
18196 +
18197 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18198 +{
18199 +       int err;
18200 +
18201 +       err = 0;
18202 +       if (!br->br_hfsn)
18203 +               err = au_hfsn_init_br(br, perm);
18204 +
18205 +       return err;
18206 +}
18207 +
18208 +/* ---------------------------------------------------------------------- */
18209 +
18210 +static void au_hfsn_fin(void)
18211 +{
18212 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18213 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18214 +}
18215 +
18216 +const struct au_hnotify_op au_hnotify_op = {
18217 +       .ctl            = au_hfsn_ctl,
18218 +       .alloc          = au_hfsn_alloc,
18219 +       .free           = au_hfsn_free,
18220 +
18221 +       .fin            = au_hfsn_fin,
18222 +
18223 +       .reset_br       = au_hfsn_reset_br,
18224 +       .fin_br         = au_hfsn_fin_br,
18225 +       .init_br        = au_hfsn_init_br
18226 +};
18227 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
18228 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
18229 +++ linux/fs/aufs/hfsplus.c     2024-05-13 17:13:00.618070924 +0200
18230 @@ -0,0 +1,60 @@
18231 +// SPDX-License-Identifier: GPL-2.0
18232 +/*
18233 + * Copyright (C) 2010-2022 Junjiro R. Okajima
18234 + *
18235 + * This program is free software; you can redistribute it and/or modify
18236 + * it under the terms of the GNU General Public License as published by
18237 + * the Free Software Foundation; either version 2 of the License, or
18238 + * (at your option) any later version.
18239 + *
18240 + * This program is distributed in the hope that it will be useful,
18241 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18242 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18243 + * GNU General Public License for more details.
18244 + *
18245 + * You should have received a copy of the GNU General Public License
18246 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18247 + */
18248 +
18249 +/*
18250 + * special support for filesystems which acquires an inode mutex
18251 + * at final closing a file, eg, hfsplus.
18252 + *
18253 + * This trick is very simple and stupid, just to open the file before really
18254 + * necessary open to tell hfsplus that this is not the final closing.
18255 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
18256 + * and au_h_open_post() after releasing it.
18257 + */
18258 +
18259 +#include "aufs.h"
18260 +
18261 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18262 +                          int force_wr)
18263 +{
18264 +       struct file *h_file;
18265 +       struct dentry *h_dentry;
18266 +
18267 +       h_dentry = au_h_dptr(dentry, bindex);
18268 +       AuDebugOn(!h_dentry);
18269 +       AuDebugOn(d_is_negative(h_dentry));
18270 +
18271 +       h_file = NULL;
18272 +       if (au_test_hfsplus(h_dentry->d_sb)
18273 +           && d_is_reg(h_dentry))
18274 +               h_file = au_h_open(dentry, bindex,
18275 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
18276 +                                  /*file*/NULL, force_wr);
18277 +       return h_file;
18278 +}
18279 +
18280 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18281 +                   struct file *h_file)
18282 +{
18283 +       struct au_branch *br;
18284 +
18285 +       if (h_file) {
18286 +               fput(h_file);
18287 +               br = au_sbr(dentry->d_sb, bindex);
18288 +               au_lcnt_dec(&br->br_nfiles);
18289 +       }
18290 +}
18291 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
18292 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
18293 +++ linux/fs/aufs/hnotify.c     2024-05-13 17:13:00.618070924 +0200
18294 @@ -0,0 +1,715 @@
18295 +// SPDX-License-Identifier: GPL-2.0
18296 +/*
18297 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18298 + *
18299 + * This program is free software; you can redistribute it and/or modify
18300 + * it under the terms of the GNU General Public License as published by
18301 + * the Free Software Foundation; either version 2 of the License, or
18302 + * (at your option) any later version.
18303 + *
18304 + * This program is distributed in the hope that it will be useful,
18305 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18306 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18307 + * GNU General Public License for more details.
18308 + *
18309 + * You should have received a copy of the GNU General Public License
18310 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18311 + */
18312 +
18313 +/*
18314 + * abstraction to notify the direct changes on lower directories
18315 + */
18316 +
18317 +/* #include <linux/iversion.h> */
18318 +#include "aufs.h"
18319 +
18320 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18321 +{
18322 +       int err;
18323 +       struct au_hnotify *hn;
18324 +
18325 +       err = -ENOMEM;
18326 +       hn = au_cache_alloc_hnotify();
18327 +       if (hn) {
18328 +               hn->hn_aufs_inode = inode;
18329 +               hinode->hi_notify = hn;
18330 +               err = au_hnotify_op.alloc(hinode);
18331 +               AuTraceErr(err);
18332 +               if (unlikely(err)) {
18333 +                       hinode->hi_notify = NULL;
18334 +                       au_cache_free_hnotify(hn);
18335 +                       /*
18336 +                        * The upper dir was removed by udba, but the same named
18337 +                        * dir left. In this case, aufs assigns a new inode
18338 +                        * number and set the monitor again.
18339 +                        * For the lower dir, the old monitor is still left.
18340 +                        */
18341 +                       if (err == -EEXIST)
18342 +                               err = 0;
18343 +               }
18344 +       }
18345 +
18346 +       AuTraceErr(err);
18347 +       return err;
18348 +}
18349 +
18350 +void au_hn_free(struct au_hinode *hinode)
18351 +{
18352 +       struct au_hnotify *hn;
18353 +
18354 +       hn = hinode->hi_notify;
18355 +       if (hn) {
18356 +               hinode->hi_notify = NULL;
18357 +               if (au_hnotify_op.free(hinode, hn))
18358 +                       au_cache_free_hnotify(hn);
18359 +       }
18360 +}
18361 +
18362 +/* ---------------------------------------------------------------------- */
18363 +
18364 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18365 +{
18366 +       if (hinode->hi_notify)
18367 +               au_hnotify_op.ctl(hinode, do_set);
18368 +}
18369 +
18370 +void au_hn_reset(struct inode *inode, unsigned int flags)
18371 +{
18372 +       aufs_bindex_t bindex, bbot;
18373 +       struct inode *hi;
18374 +       struct dentry *iwhdentry;
18375 +
18376 +       bbot = au_ibbot(inode);
18377 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18378 +               hi = au_h_iptr(inode, bindex);
18379 +               if (!hi)
18380 +                       continue;
18381 +
18382 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18383 +               iwhdentry = au_hi_wh(inode, bindex);
18384 +               if (iwhdentry)
18385 +                       dget(iwhdentry);
18386 +               au_igrab(hi);
18387 +               au_set_h_iptr(inode, bindex, NULL, 0);
18388 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
18389 +                             flags & ~AuHi_XINO);
18390 +               iput(hi);
18391 +               dput(iwhdentry);
18392 +               /* inode_unlock(hi); */
18393 +       }
18394 +}
18395 +
18396 +/* ---------------------------------------------------------------------- */
18397 +
18398 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18399 +{
18400 +       int err;
18401 +       aufs_bindex_t bindex, bbot, bfound, btop;
18402 +       struct inode *h_i;
18403 +
18404 +       err = 0;
18405 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18406 +               pr_warn("branch root dir was changed\n");
18407 +               goto out;
18408 +       }
18409 +
18410 +       bfound = -1;
18411 +       bbot = au_ibbot(inode);
18412 +       btop = au_ibtop(inode);
18413 +#if 0 /* reserved for future use */
18414 +       if (bindex == bbot) {
18415 +               /* keep this ino in rename case */
18416 +               goto out;
18417 +       }
18418 +#endif
18419 +       for (bindex = btop; bindex <= bbot; bindex++)
18420 +               if (au_h_iptr(inode, bindex) == h_inode) {
18421 +                       bfound = bindex;
18422 +                       break;
18423 +               }
18424 +       if (bfound < 0)
18425 +               goto out;
18426 +
18427 +       for (bindex = btop; bindex <= bbot; bindex++) {
18428 +               h_i = au_h_iptr(inode, bindex);
18429 +               if (!h_i)
18430 +                       continue;
18431 +
18432 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18433 +               /* ignore this error */
18434 +               /* bad action? */
18435 +       }
18436 +
18437 +       /* children inode number will be broken */
18438 +
18439 +out:
18440 +       AuTraceErr(err);
18441 +       return err;
18442 +}
18443 +
18444 +static int hn_gen_tree(struct dentry *dentry)
18445 +{
18446 +       int err, i, j, ndentry;
18447 +       struct au_dcsub_pages dpages;
18448 +       struct au_dpage *dpage;
18449 +       struct dentry **dentries;
18450 +
18451 +       err = au_dpages_init(&dpages, GFP_NOFS);
18452 +       if (unlikely(err))
18453 +               goto out;
18454 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18455 +       if (unlikely(err))
18456 +               goto out_dpages;
18457 +
18458 +       for (i = 0; i < dpages.ndpage; i++) {
18459 +               dpage = dpages.dpages + i;
18460 +               dentries = dpage->dentries;
18461 +               ndentry = dpage->ndentry;
18462 +               for (j = 0; j < ndentry; j++) {
18463 +                       struct dentry *d;
18464 +
18465 +                       d = dentries[j];
18466 +                       if (IS_ROOT(d))
18467 +                               continue;
18468 +
18469 +                       au_digen_dec(d);
18470 +                       if (d_really_is_positive(d))
18471 +                               /* todo: reset children xino?
18472 +                                  cached children only? */
18473 +                               au_iigen_dec(d_inode(d));
18474 +               }
18475 +       }
18476 +
18477 +out_dpages:
18478 +       au_dpages_free(&dpages);
18479 +out:
18480 +       return err;
18481 +}
18482 +
18483 +/*
18484 + * return 0 if processed.
18485 + */
18486 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18487 +                          const unsigned int isdir)
18488 +{
18489 +       int err;
18490 +       struct dentry *d;
18491 +       struct qstr *dname;
18492 +
18493 +       err = 1;
18494 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18495 +               pr_warn("branch root dir was changed\n");
18496 +               err = 0;
18497 +               goto out;
18498 +       }
18499 +
18500 +       if (!isdir) {
18501 +               AuDebugOn(!name);
18502 +               au_iigen_dec(inode);
18503 +               spin_lock(&inode->i_lock);
18504 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18505 +                       spin_lock(&d->d_lock);
18506 +                       dname = &d->d_name;
18507 +                       if (dname->len != nlen
18508 +                           && memcmp(dname->name, name, nlen)) {
18509 +                               spin_unlock(&d->d_lock);
18510 +                               continue;
18511 +                       }
18512 +                       err = 0;
18513 +                       au_digen_dec(d);
18514 +                       spin_unlock(&d->d_lock);
18515 +                       break;
18516 +               }
18517 +               spin_unlock(&inode->i_lock);
18518 +       } else {
18519 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18520 +               d = d_find_any_alias(inode);
18521 +               if (!d) {
18522 +                       au_iigen_dec(inode);
18523 +                       goto out;
18524 +               }
18525 +
18526 +               spin_lock(&d->d_lock);
18527 +               dname = &d->d_name;
18528 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18529 +                       spin_unlock(&d->d_lock);
18530 +                       err = hn_gen_tree(d);
18531 +                       spin_lock(&d->d_lock);
18532 +               }
18533 +               spin_unlock(&d->d_lock);
18534 +               dput(d);
18535 +       }
18536 +
18537 +out:
18538 +       AuTraceErr(err);
18539 +       return err;
18540 +}
18541 +
18542 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18543 +{
18544 +       int err;
18545 +
18546 +       if (IS_ROOT(dentry)) {
18547 +               pr_warn("branch root dir was changed\n");
18548 +               return 0;
18549 +       }
18550 +
18551 +       err = 0;
18552 +       if (!isdir) {
18553 +               au_digen_dec(dentry);
18554 +               if (d_really_is_positive(dentry))
18555 +                       au_iigen_dec(d_inode(dentry));
18556 +       } else {
18557 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18558 +               if (d_really_is_positive(dentry))
18559 +                       err = hn_gen_tree(dentry);
18560 +       }
18561 +
18562 +       AuTraceErr(err);
18563 +       return err;
18564 +}
18565 +
18566 +/* ---------------------------------------------------------------------- */
18567 +
18568 +/* hnotify job flags */
18569 +#define AuHnJob_XINO0          BIT(0)
18570 +#define AuHnJob_GEN            BIT(1)
18571 +#define AuHnJob_DIRENT         BIT(2)
18572 +#define AuHnJob_ISDIR          BIT(3)
18573 +#define AuHnJob_TRYXINO0       BIT(4)
18574 +#define AuHnJob_MNTPNT         BIT(5)
18575 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
18576 +#define au_fset_hnjob(flags, name) \
18577 +       do { (flags) |= AuHnJob_##name; } while (0)
18578 +#define au_fclr_hnjob(flags, name) \
18579 +       do { (flags) &= ~AuHnJob_##name; } while (0)
18580 +
18581 +enum {
18582 +       AuHn_CHILD,
18583 +       AuHn_PARENT,
18584 +       AuHnLast
18585 +};
18586 +
18587 +struct au_hnotify_args {
18588 +       struct inode *h_dir, *dir, *h_child_inode;
18589 +       u32 mask;
18590 +       unsigned int flags[AuHnLast];
18591 +       unsigned int h_child_nlen;
18592 +       char h_child_name[];
18593 +};
18594 +
18595 +struct hn_job_args {
18596 +       unsigned int flags;
18597 +       struct inode *inode, *h_inode, *dir, *h_dir;
18598 +       struct dentry *dentry;
18599 +       char *h_name;
18600 +       int h_nlen;
18601 +};
18602 +
18603 +static int hn_job(struct hn_job_args *a)
18604 +{
18605 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18606 +       int e;
18607 +
18608 +       /* reset xino */
18609 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18610 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
18611 +
18612 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
18613 +           && a->inode
18614 +           && a->h_inode) {
18615 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18616 +               if (!a->h_inode->i_nlink
18617 +                   && !(a->h_inode->i_state & I_LINKABLE))
18618 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
18619 +               inode_unlock_shared(a->h_inode);
18620 +       }
18621 +
18622 +       /* make the generation obsolete */
18623 +       if (au_ftest_hnjob(a->flags, GEN)) {
18624 +               e = -1;
18625 +               if (a->inode)
18626 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18627 +                                             isdir);
18628 +               if (e && a->dentry)
18629 +                       hn_gen_by_name(a->dentry, isdir);
18630 +               /* ignore this error */
18631 +       }
18632 +
18633 +       /* make dir entries obsolete */
18634 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18635 +               struct au_vdir *vdir;
18636 +
18637 +               vdir = au_ivdir(a->inode);
18638 +               if (vdir)
18639 +                       vdir->vd_jiffy = 0;
18640 +               /* IMustLock(a->inode); */
18641 +               /* inode_inc_iversion(a->inode); */
18642 +       }
18643 +
18644 +       /* can do nothing but warn */
18645 +       if (au_ftest_hnjob(a->flags, MNTPNT)
18646 +           && a->dentry
18647 +           && d_mountpoint(a->dentry))
18648 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18649 +
18650 +       return 0;
18651 +}
18652 +
18653 +/* ---------------------------------------------------------------------- */
18654 +
18655 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18656 +                                          struct inode *dir)
18657 +{
18658 +       struct dentry *dentry, *d, *parent;
18659 +       struct qstr *dname;
18660 +
18661 +       parent = d_find_any_alias(dir);
18662 +       if (!parent)
18663 +               return NULL;
18664 +
18665 +       dentry = NULL;
18666 +       spin_lock(&parent->d_lock);
18667 +       hlist_for_each_entry(d, &parent->d_children, d_sib) {
18668 +               /* AuDbg("%pd\n", d); */
18669 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18670 +               dname = &d->d_name;
18671 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
18672 +                       goto cont_unlock;
18673 +               if (au_di(d))
18674 +                       au_digen_dec(d);
18675 +               else
18676 +                       goto cont_unlock;
18677 +               if (au_dcount(d) > 0) {
18678 +                       dentry = dget_dlock(d);
18679 +                       spin_unlock(&d->d_lock);
18680 +                       break;
18681 +               }
18682 +
18683 +cont_unlock:
18684 +               spin_unlock(&d->d_lock);
18685 +       }
18686 +       spin_unlock(&parent->d_lock);
18687 +       dput(parent);
18688 +
18689 +       if (dentry)
18690 +               di_write_lock_child(dentry);
18691 +
18692 +       return dentry;
18693 +}
18694 +
18695 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18696 +                                        aufs_bindex_t bindex, ino_t h_ino)
18697 +{
18698 +       struct inode *inode;
18699 +       ino_t ino;
18700 +       int err;
18701 +
18702 +       inode = NULL;
18703 +       err = au_xino_read(sb, bindex, h_ino, &ino);
18704 +       if (!err && ino)
18705 +               inode = ilookup(sb, ino);
18706 +       if (!inode)
18707 +               goto out;
18708 +
18709 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18710 +               pr_warn("wrong root branch\n");
18711 +               iput(inode);
18712 +               inode = NULL;
18713 +               goto out;
18714 +       }
18715 +
18716 +       ii_write_lock_child(inode);
18717 +
18718 +out:
18719 +       return inode;
18720 +}
18721 +
18722 +static void au_hn_bh(void *_args)
18723 +{
18724 +       struct au_hnotify_args *a = _args;
18725 +       struct super_block *sb;
18726 +       aufs_bindex_t bindex, bbot, bfound;
18727 +       unsigned char xino, try_iput;
18728 +       int err;
18729 +       struct inode *inode;
18730 +       ino_t h_ino;
18731 +       struct hn_job_args args;
18732 +       struct dentry *dentry;
18733 +       struct au_sbinfo *sbinfo;
18734 +
18735 +       AuDebugOn(!_args);
18736 +       AuDebugOn(!a->h_dir);
18737 +       AuDebugOn(!a->dir);
18738 +       AuDebugOn(!a->mask);
18739 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18740 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
18741 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
18742 +
18743 +       inode = NULL;
18744 +       dentry = NULL;
18745 +       /*
18746 +        * do not lock a->dir->i_mutex here
18747 +        * because of d_revalidate() may cause a deadlock.
18748 +        */
18749 +       sb = a->dir->i_sb;
18750 +       AuDebugOn(!sb);
18751 +       sbinfo = au_sbi(sb);
18752 +       AuDebugOn(!sbinfo);
18753 +       si_write_lock(sb, AuLock_NOPLMW);
18754 +
18755 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18756 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18757 +               case FS_MOVED_FROM:
18758 +               case FS_MOVED_TO:
18759 +                       AuWarn1("DIRREN with UDBA may not work correctly "
18760 +                               "for the direct rename(2)\n");
18761 +               }
18762 +
18763 +       ii_read_lock_parent(a->dir);
18764 +       bfound = -1;
18765 +       bbot = au_ibbot(a->dir);
18766 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18767 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18768 +                       bfound = bindex;
18769 +                       break;
18770 +               }
18771 +       ii_read_unlock(a->dir);
18772 +       if (unlikely(bfound < 0))
18773 +               goto out;
18774 +
18775 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
18776 +       h_ino = 0;
18777 +       if (a->h_child_inode)
18778 +               h_ino = a->h_child_inode->i_ino;
18779 +
18780 +       if (a->h_child_nlen
18781 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18782 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18783 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18784 +                                             a->dir);
18785 +       try_iput = 0;
18786 +       if (dentry && d_really_is_positive(dentry))
18787 +               inode = d_inode(dentry);
18788 +       if (xino && !inode && h_ino
18789 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18790 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18791 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18792 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18793 +               try_iput = 1;
18794 +       }
18795 +
18796 +       args.flags = a->flags[AuHn_CHILD];
18797 +       args.dentry = dentry;
18798 +       args.inode = inode;
18799 +       args.h_inode = a->h_child_inode;
18800 +       args.dir = a->dir;
18801 +       args.h_dir = a->h_dir;
18802 +       args.h_name = a->h_child_name;
18803 +       args.h_nlen = a->h_child_nlen;
18804 +       err = hn_job(&args);
18805 +       if (dentry) {
18806 +               if (au_di(dentry))
18807 +                       di_write_unlock(dentry);
18808 +               dput(dentry);
18809 +       }
18810 +       if (inode && try_iput) {
18811 +               ii_write_unlock(inode);
18812 +               iput(inode);
18813 +       }
18814 +
18815 +       ii_write_lock_parent(a->dir);
18816 +       args.flags = a->flags[AuHn_PARENT];
18817 +       args.dentry = NULL;
18818 +       args.inode = a->dir;
18819 +       args.h_inode = a->h_dir;
18820 +       args.dir = NULL;
18821 +       args.h_dir = NULL;
18822 +       args.h_name = NULL;
18823 +       args.h_nlen = 0;
18824 +       err = hn_job(&args);
18825 +       ii_write_unlock(a->dir);
18826 +
18827 +out:
18828 +       iput(a->h_child_inode);
18829 +       iput(a->h_dir);
18830 +       iput(a->dir);
18831 +       si_write_unlock(sb);
18832 +       au_nwt_done(&sbinfo->si_nowait);
18833 +       au_kfree_rcu(a);
18834 +}
18835 +
18836 +/* ---------------------------------------------------------------------- */
18837 +
18838 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18839 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
18840 +{
18841 +       int err, len;
18842 +       unsigned int flags[AuHnLast], f;
18843 +       unsigned char isdir, isroot, wh;
18844 +       struct inode *dir;
18845 +       struct au_hnotify_args *args;
18846 +       char *p, *h_child_name;
18847 +
18848 +       err = 0;
18849 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18850 +       dir = igrab(hnotify->hn_aufs_inode);
18851 +       if (!dir)
18852 +               goto out;
18853 +
18854 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
18855 +       wh = 0;
18856 +       h_child_name = (void *)h_child_qstr->name;
18857 +       len = h_child_qstr->len;
18858 +       if (h_child_name) {
18859 +               if (len > AUFS_WH_PFX_LEN
18860 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18861 +                       h_child_name += AUFS_WH_PFX_LEN;
18862 +                       len -= AUFS_WH_PFX_LEN;
18863 +                       wh = 1;
18864 +               }
18865 +       }
18866 +
18867 +       isdir = 0;
18868 +       if (h_child_inode)
18869 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
18870 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
18871 +       flags[AuHn_CHILD] = 0;
18872 +       if (isdir)
18873 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
18874 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18875 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
18876 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18877 +       case FS_MOVED_FROM:
18878 +       case FS_MOVED_TO:
18879 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18880 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18881 +               fallthrough;
18882 +       case FS_CREATE:
18883 +               AuDebugOn(!h_child_name);
18884 +               break;
18885 +
18886 +       case FS_DELETE:
18887 +               /*
18888 +                * aufs never be able to get this child inode.
18889 +                * revalidation should be in d_revalidate()
18890 +                * by checking i_nlink, i_generation or d_unhashed().
18891 +                */
18892 +               AuDebugOn(!h_child_name);
18893 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18894 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18895 +               break;
18896 +
18897 +       default:
18898 +               AuDebugOn(1);
18899 +       }
18900 +
18901 +       if (wh)
18902 +               h_child_inode = NULL;
18903 +
18904 +       err = -ENOMEM;
18905 +       /* iput() and kfree() will be called in au_hnotify() */
18906 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18907 +       if (unlikely(!args)) {
18908 +               AuErr1("no memory\n");
18909 +               iput(dir);
18910 +               goto out;
18911 +       }
18912 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18913 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18914 +       args->mask = mask;
18915 +       args->dir = dir;
18916 +       args->h_dir = igrab(h_dir);
18917 +       if (h_child_inode)
18918 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
18919 +       args->h_child_inode = h_child_inode;
18920 +       args->h_child_nlen = len;
18921 +       if (len) {
18922 +               p = (void *)args;
18923 +               p += sizeof(*args);
18924 +               memcpy(p, h_child_name, len);
18925 +               p[len] = 0;
18926 +       }
18927 +
18928 +       /* NFS fires the event for silly-renamed one from kworker */
18929 +       f = 0;
18930 +       if (!dir->i_nlink
18931 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18932 +               f = AuWkq_NEST;
18933 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18934 +       if (unlikely(err)) {
18935 +               pr_err("wkq %d\n", err);
18936 +               iput(args->h_child_inode);
18937 +               iput(args->h_dir);
18938 +               iput(args->dir);
18939 +               au_kfree_rcu(args);
18940 +       }
18941 +
18942 +out:
18943 +       return err;
18944 +}
18945 +
18946 +/* ---------------------------------------------------------------------- */
18947 +
18948 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18949 +{
18950 +       int err;
18951 +
18952 +       AuDebugOn(!(udba & AuOptMask_UDBA));
18953 +
18954 +       err = 0;
18955 +       if (au_hnotify_op.reset_br)
18956 +               err = au_hnotify_op.reset_br(udba, br, perm);
18957 +
18958 +       return err;
18959 +}
18960 +
18961 +int au_hnotify_init_br(struct au_branch *br, int perm)
18962 +{
18963 +       int err;
18964 +
18965 +       err = 0;
18966 +       if (au_hnotify_op.init_br)
18967 +               err = au_hnotify_op.init_br(br, perm);
18968 +
18969 +       return err;
18970 +}
18971 +
18972 +void au_hnotify_fin_br(struct au_branch *br)
18973 +{
18974 +       if (au_hnotify_op.fin_br)
18975 +               au_hnotify_op.fin_br(br);
18976 +}
18977 +
18978 +static void au_hn_destroy_cache(void)
18979 +{
18980 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18981 +       au_cache[AuCache_HNOTIFY] = NULL;
18982 +}
18983 +
18984 +int __init au_hnotify_init(void)
18985 +{
18986 +       int err;
18987 +
18988 +       err = -ENOMEM;
18989 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18990 +       if (au_cache[AuCache_HNOTIFY]) {
18991 +               err = 0;
18992 +               if (au_hnotify_op.init)
18993 +                       err = au_hnotify_op.init();
18994 +               if (unlikely(err))
18995 +                       au_hn_destroy_cache();
18996 +       }
18997 +       AuTraceErr(err);
18998 +       return err;
18999 +}
19000 +
19001 +void au_hnotify_fin(void)
19002 +{
19003 +       if (au_hnotify_op.fin)
19004 +               au_hnotify_op.fin();
19005 +
19006 +       /* cf. au_cache_fin() */
19007 +       if (au_cache[AuCache_HNOTIFY])
19008 +               au_hn_destroy_cache();
19009 +}
19010 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
19011 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
19012 +++ linux/fs/aufs/iinfo.c       2024-05-13 17:13:00.621404284 +0200
19013 @@ -0,0 +1,286 @@
19014 +// SPDX-License-Identifier: GPL-2.0
19015 +/*
19016 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19017 + *
19018 + * This program is free software; you can redistribute it and/or modify
19019 + * it under the terms of the GNU General Public License as published by
19020 + * the Free Software Foundation; either version 2 of the License, or
19021 + * (at your option) any later version.
19022 + *
19023 + * This program is distributed in the hope that it will be useful,
19024 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19025 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19026 + * GNU General Public License for more details.
19027 + *
19028 + * You should have received a copy of the GNU General Public License
19029 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19030 + */
19031 +
19032 +/*
19033 + * inode private data
19034 + */
19035 +
19036 +#include "aufs.h"
19037 +
19038 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
19039 +{
19040 +       struct inode *h_inode;
19041 +       struct au_hinode *hinode;
19042 +
19043 +       IiMustAnyLock(inode);
19044 +
19045 +       hinode = au_hinode(au_ii(inode), bindex);
19046 +       h_inode = hinode->hi_inode;
19047 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19048 +       return h_inode;
19049 +}
19050 +
19051 +/* todo: hard/soft set? */
19052 +void au_hiput(struct au_hinode *hinode)
19053 +{
19054 +       au_hn_free(hinode);
19055 +       dput(hinode->hi_whdentry);
19056 +       iput(hinode->hi_inode);
19057 +}
19058 +
19059 +unsigned int au_hi_flags(struct inode *inode, int isdir)
19060 +{
19061 +       unsigned int flags;
19062 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
19063 +
19064 +       flags = 0;
19065 +       if (au_opt_test(mnt_flags, XINO))
19066 +               au_fset_hi(flags, XINO);
19067 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19068 +               au_fset_hi(flags, HNOTIFY);
19069 +       return flags;
19070 +}
19071 +
19072 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19073 +                  struct inode *h_inode, unsigned int flags)
19074 +{
19075 +       struct au_hinode *hinode;
19076 +       struct inode *hi;
19077 +       struct au_iinfo *iinfo = au_ii(inode);
19078 +
19079 +       IiMustWriteLock(inode);
19080 +
19081 +       hinode = au_hinode(iinfo, bindex);
19082 +       hi = hinode->hi_inode;
19083 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19084 +
19085 +       if (hi)
19086 +               au_hiput(hinode);
19087 +       hinode->hi_inode = h_inode;
19088 +       if (h_inode) {
19089 +               int err;
19090 +               struct super_block *sb = inode->i_sb;
19091 +               struct au_branch *br;
19092 +
19093 +               AuDebugOn(inode->i_mode
19094 +                         && (h_inode->i_mode & S_IFMT)
19095 +                         != (inode->i_mode & S_IFMT));
19096 +               if (bindex == iinfo->ii_btop)
19097 +                       au_cpup_igen(inode, h_inode);
19098 +               br = au_sbr(sb, bindex);
19099 +               hinode->hi_id = br->br_id;
19100 +               if (au_ftest_hi(flags, XINO)) {
19101 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
19102 +                                           inode->i_ino);
19103 +                       if (unlikely(err))
19104 +                               AuIOErr1("failed au_xino_write() %d\n", err);
19105 +               }
19106 +
19107 +               if (au_ftest_hi(flags, HNOTIFY)
19108 +                   && au_br_hnotifyable(br->br_perm)) {
19109 +                       err = au_hn_alloc(hinode, inode);
19110 +                       if (unlikely(err))
19111 +                               AuIOErr1("au_hn_alloc() %d\n", err);
19112 +               }
19113 +       }
19114 +}
19115 +
19116 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19117 +                 struct dentry *h_wh)
19118 +{
19119 +       struct au_hinode *hinode;
19120 +
19121 +       IiMustWriteLock(inode);
19122 +
19123 +       hinode = au_hinode(au_ii(inode), bindex);
19124 +       AuDebugOn(hinode->hi_whdentry);
19125 +       hinode->hi_whdentry = h_wh;
19126 +}
19127 +
19128 +void au_update_iigen(struct inode *inode, int half)
19129 +{
19130 +       struct au_iinfo *iinfo;
19131 +       struct au_iigen *iigen;
19132 +       unsigned int sigen;
19133 +
19134 +       sigen = au_sigen(inode->i_sb);
19135 +       iinfo = au_ii(inode);
19136 +       iigen = &iinfo->ii_generation;
19137 +       spin_lock(&iigen->ig_spin);
19138 +       iigen->ig_generation = sigen;
19139 +       if (half)
19140 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19141 +       else
19142 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
19143 +       spin_unlock(&iigen->ig_spin);
19144 +}
19145 +
19146 +/* it may be called at remount time, too */
19147 +void au_update_ibrange(struct inode *inode, int do_put_zero)
19148 +{
19149 +       struct au_iinfo *iinfo;
19150 +       aufs_bindex_t bindex, bbot;
19151 +
19152 +       AuDebugOn(au_is_bad_inode(inode));
19153 +       IiMustWriteLock(inode);
19154 +
19155 +       iinfo = au_ii(inode);
19156 +       if (do_put_zero && iinfo->ii_btop >= 0) {
19157 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19158 +                    bindex++) {
19159 +                       struct inode *h_i;
19160 +
19161 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
19162 +                       if (h_i
19163 +                           && !h_i->i_nlink
19164 +                           && !(h_i->i_state & I_LINKABLE))
19165 +                               au_set_h_iptr(inode, bindex, NULL, 0);
19166 +               }
19167 +       }
19168 +
19169 +       iinfo->ii_btop = -1;
19170 +       iinfo->ii_bbot = -1;
19171 +       bbot = au_sbbot(inode->i_sb);
19172 +       for (bindex = 0; bindex <= bbot; bindex++)
19173 +               if (au_hinode(iinfo, bindex)->hi_inode) {
19174 +                       iinfo->ii_btop = bindex;
19175 +                       break;
19176 +               }
19177 +       if (iinfo->ii_btop >= 0)
19178 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19179 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
19180 +                               iinfo->ii_bbot = bindex;
19181 +                               break;
19182 +                       }
19183 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
19184 +}
19185 +
19186 +/* ---------------------------------------------------------------------- */
19187 +
19188 +void au_icntnr_init_once(void *_c)
19189 +{
19190 +       struct au_icntnr *c = _c;
19191 +       struct au_iinfo *iinfo = &c->iinfo;
19192 +
19193 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
19194 +       au_rw_init(&iinfo->ii_rwsem);
19195 +       inode_init_once(&c->vfs_inode);
19196 +}
19197 +
19198 +void au_hinode_init(struct au_hinode *hinode)
19199 +{
19200 +       hinode->hi_inode = NULL;
19201 +       hinode->hi_id = -1;
19202 +       au_hn_init(hinode);
19203 +       hinode->hi_whdentry = NULL;
19204 +}
19205 +
19206 +int au_iinfo_init(struct inode *inode)
19207 +{
19208 +       struct au_iinfo *iinfo;
19209 +       struct super_block *sb;
19210 +       struct au_hinode *hi;
19211 +       int nbr, i;
19212 +
19213 +       sb = inode->i_sb;
19214 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19215 +       nbr = au_sbbot(sb) + 1;
19216 +       if (unlikely(nbr <= 0))
19217 +               nbr = 1;
19218 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19219 +       if (hi) {
19220 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
19221 +
19222 +               iinfo->ii_hinode = hi;
19223 +               for (i = 0; i < nbr; i++, hi++)
19224 +                       au_hinode_init(hi);
19225 +
19226 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
19227 +               iinfo->ii_btop = -1;
19228 +               iinfo->ii_bbot = -1;
19229 +               iinfo->ii_vdir = NULL;
19230 +               return 0;
19231 +       }
19232 +       return -ENOMEM;
19233 +}
19234 +
19235 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
19236 +{
19237 +       int err, i;
19238 +       struct au_hinode *hip;
19239 +
19240 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
19241 +
19242 +       err = -ENOMEM;
19243 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19244 +                         may_shrink);
19245 +       if (hip) {
19246 +               iinfo->ii_hinode = hip;
19247 +               i = iinfo->ii_bbot + 1;
19248 +               hip += i;
19249 +               for (; i < nbr; i++, hip++)
19250 +                       au_hinode_init(hip);
19251 +               err = 0;
19252 +       }
19253 +
19254 +       return err;
19255 +}
19256 +
19257 +void au_iinfo_fin(struct inode *inode)
19258 +{
19259 +       struct au_iinfo *iinfo;
19260 +       struct au_hinode *hi;
19261 +       struct super_block *sb;
19262 +       aufs_bindex_t bindex, bbot;
19263 +       const unsigned char unlinked = !inode->i_nlink;
19264 +
19265 +       AuDebugOn(au_is_bad_inode(inode));
19266 +
19267 +       sb = inode->i_sb;
19268 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
19269 +       if (si_pid_test(sb))
19270 +               au_xino_delete_inode(inode, unlinked);
19271 +       else {
19272 +               /*
19273 +                * it is safe to hide the dependency between sbinfo and
19274 +                * sb->s_umount.
19275 +                */
19276 +               lockdep_off();
19277 +               si_noflush_read_lock(sb);
19278 +               au_xino_delete_inode(inode, unlinked);
19279 +               si_read_unlock(sb);
19280 +               lockdep_on();
19281 +       }
19282 +
19283 +       iinfo = au_ii(inode);
19284 +       if (iinfo->ii_vdir)
19285 +               au_vdir_free(iinfo->ii_vdir);
19286 +
19287 +       bindex = iinfo->ii_btop;
19288 +       if (bindex >= 0) {
19289 +               hi = au_hinode(iinfo, bindex);
19290 +               bbot = iinfo->ii_bbot;
19291 +               while (bindex++ <= bbot) {
19292 +                       if (hi->hi_inode)
19293 +                               au_hiput(hi);
19294 +                       hi++;
19295 +               }
19296 +       }
19297 +       au_kfree_rcu(iinfo->ii_hinode);
19298 +       AuRwDestroy(&iinfo->ii_rwsem);
19299 +}
19300 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
19301 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
19302 +++ linux/fs/aufs/inode.c       2024-05-13 17:13:00.621404284 +0200
19303 @@ -0,0 +1,531 @@
19304 +// SPDX-License-Identifier: GPL-2.0
19305 +/*
19306 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19307 + *
19308 + * This program is free software; you can redistribute it and/or modify
19309 + * it under the terms of the GNU General Public License as published by
19310 + * the Free Software Foundation; either version 2 of the License, or
19311 + * (at your option) any later version.
19312 + *
19313 + * This program is distributed in the hope that it will be useful,
19314 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19315 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19316 + * GNU General Public License for more details.
19317 + *
19318 + * You should have received a copy of the GNU General Public License
19319 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19320 + */
19321 +
19322 +/*
19323 + * inode functions
19324 + */
19325 +
19326 +#include <linux/iversion.h>
19327 +#include "aufs.h"
19328 +
19329 +struct inode *au_igrab(struct inode *inode)
19330 +{
19331 +       if (inode) {
19332 +               AuDebugOn(!atomic_read(&inode->i_count));
19333 +               ihold(inode);
19334 +       }
19335 +       return inode;
19336 +}
19337 +
19338 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19339 +{
19340 +       au_cpup_attr_all(inode, /*force*/0);
19341 +       au_update_iigen(inode, /*half*/1);
19342 +       if (do_version)
19343 +               inode_inc_iversion(inode);
19344 +}
19345 +
19346 +static int au_ii_refresh(struct inode *inode, int *update)
19347 +{
19348 +       int err, e, nbr;
19349 +       umode_t type;
19350 +       aufs_bindex_t bindex, new_bindex;
19351 +       struct super_block *sb;
19352 +       struct au_iinfo *iinfo;
19353 +       struct au_hinode *p, *q, tmp;
19354 +
19355 +       AuDebugOn(au_is_bad_inode(inode));
19356 +       IiMustWriteLock(inode);
19357 +
19358 +       *update = 0;
19359 +       sb = inode->i_sb;
19360 +       nbr = au_sbbot(sb) + 1;
19361 +       type = inode->i_mode & S_IFMT;
19362 +       iinfo = au_ii(inode);
19363 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
19364 +       if (unlikely(err))
19365 +               goto out;
19366 +
19367 +       AuDebugOn(iinfo->ii_btop < 0);
19368 +       p = au_hinode(iinfo, iinfo->ii_btop);
19369 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19370 +            bindex++, p++) {
19371 +               if (!p->hi_inode)
19372 +                       continue;
19373 +
19374 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
19375 +               new_bindex = au_br_index(sb, p->hi_id);
19376 +               if (new_bindex == bindex)
19377 +                       continue;
19378 +
19379 +               if (new_bindex < 0) {
19380 +                       *update = 1;
19381 +                       au_hiput(p);
19382 +                       p->hi_inode = NULL;
19383 +                       continue;
19384 +               }
19385 +
19386 +               if (new_bindex < iinfo->ii_btop)
19387 +                       iinfo->ii_btop = new_bindex;
19388 +               if (iinfo->ii_bbot < new_bindex)
19389 +                       iinfo->ii_bbot = new_bindex;
19390 +               /* swap two lower inode, and loop again */
19391 +               q = au_hinode(iinfo, new_bindex);
19392 +               tmp = *q;
19393 +               *q = *p;
19394 +               *p = tmp;
19395 +               if (tmp.hi_inode) {
19396 +                       bindex--;
19397 +                       p--;
19398 +               }
19399 +       }
19400 +       au_update_ibrange(inode, /*do_put_zero*/0);
19401 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
19402 +       e = au_dy_irefresh(inode);
19403 +       if (unlikely(e && !err))
19404 +               err = e;
19405 +
19406 +out:
19407 +       AuTraceErr(err);
19408 +       return err;
19409 +}
19410 +
19411 +void au_refresh_iop(struct inode *inode, int force_getattr)
19412 +{
19413 +       int type;
19414 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19415 +       const struct inode_operations *iop
19416 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
19417 +
19418 +       if (inode->i_op == iop)
19419 +               return;
19420 +
19421 +       switch (inode->i_mode & S_IFMT) {
19422 +       case S_IFDIR:
19423 +               type = AuIop_DIR;
19424 +               break;
19425 +       case S_IFLNK:
19426 +               type = AuIop_SYMLINK;
19427 +               break;
19428 +       default:
19429 +               type = AuIop_OTHER;
19430 +               break;
19431 +       }
19432 +
19433 +       inode->i_op = iop + type;
19434 +       /* unnecessary smp_wmb() */
19435 +}
19436 +
19437 +int au_refresh_hinode_self(struct inode *inode)
19438 +{
19439 +       int err, update;
19440 +
19441 +       err = au_ii_refresh(inode, &update);
19442 +       if (!err)
19443 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19444 +
19445 +       AuTraceErr(err);
19446 +       return err;
19447 +}
19448 +
19449 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19450 +{
19451 +       int err, e, update;
19452 +       unsigned int flags;
19453 +       umode_t mode;
19454 +       aufs_bindex_t bindex, bbot;
19455 +       unsigned char isdir;
19456 +       struct au_hinode *p;
19457 +       struct au_iinfo *iinfo;
19458 +
19459 +       err = au_ii_refresh(inode, &update);
19460 +       if (unlikely(err))
19461 +               goto out;
19462 +
19463 +       update = 0;
19464 +       iinfo = au_ii(inode);
19465 +       p = au_hinode(iinfo, iinfo->ii_btop);
19466 +       mode = (inode->i_mode & S_IFMT);
19467 +       isdir = S_ISDIR(mode);
19468 +       flags = au_hi_flags(inode, isdir);
19469 +       bbot = au_dbbot(dentry);
19470 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
19471 +               struct inode *h_i, *h_inode;
19472 +               struct dentry *h_d;
19473 +
19474 +               h_d = au_h_dptr(dentry, bindex);
19475 +               if (!h_d || d_is_negative(h_d))
19476 +                       continue;
19477 +
19478 +               h_inode = d_inode(h_d);
19479 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
19480 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
19481 +                       h_i = au_h_iptr(inode, bindex);
19482 +                       if (h_i) {
19483 +                               if (h_i == h_inode)
19484 +                                       continue;
19485 +                               err = -EIO;
19486 +                               break;
19487 +                       }
19488 +               }
19489 +               if (bindex < iinfo->ii_btop)
19490 +                       iinfo->ii_btop = bindex;
19491 +               if (iinfo->ii_bbot < bindex)
19492 +                       iinfo->ii_bbot = bindex;
19493 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
19494 +               update = 1;
19495 +       }
19496 +       au_update_ibrange(inode, /*do_put_zero*/0);
19497 +       e = au_dy_irefresh(inode);
19498 +       if (unlikely(e && !err))
19499 +               err = e;
19500 +       if (!err)
19501 +               au_refresh_hinode_attr(inode, update && isdir);
19502 +
19503 +out:
19504 +       AuTraceErr(err);
19505 +       return err;
19506 +}
19507 +
19508 +static int set_inode(struct inode *inode, struct dentry *dentry)
19509 +{
19510 +       int err;
19511 +       unsigned int flags;
19512 +       umode_t mode;
19513 +       aufs_bindex_t bindex, btop, btail;
19514 +       unsigned char isdir;
19515 +       struct dentry *h_dentry;
19516 +       struct inode *h_inode;
19517 +       struct au_iinfo *iinfo;
19518 +       const struct inode_operations *iop;
19519 +
19520 +       IiMustWriteLock(inode);
19521 +
19522 +       err = 0;
19523 +       isdir = 0;
19524 +       iop = au_sbi(inode->i_sb)->si_iop_array;
19525 +       btop = au_dbtop(dentry);
19526 +       h_dentry = au_h_dptr(dentry, btop);
19527 +       h_inode = d_inode(h_dentry);
19528 +       mode = h_inode->i_mode;
19529 +       switch (mode & S_IFMT) {
19530 +       case S_IFREG:
19531 +               btail = au_dbtail(dentry);
19532 +               inode->i_op = iop + AuIop_OTHER;
19533 +               inode->i_fop = &aufs_file_fop;
19534 +               err = au_dy_iaop(inode, btop, h_inode);
19535 +               if (unlikely(err))
19536 +                       goto out;
19537 +               break;
19538 +       case S_IFDIR:
19539 +               isdir = 1;
19540 +               btail = au_dbtaildir(dentry);
19541 +               inode->i_op = iop + AuIop_DIR;
19542 +               inode->i_fop = &aufs_dir_fop;
19543 +               break;
19544 +       case S_IFLNK:
19545 +               btail = au_dbtail(dentry);
19546 +               inode->i_op = iop + AuIop_SYMLINK;
19547 +               break;
19548 +       case S_IFBLK:
19549 +       case S_IFCHR:
19550 +       case S_IFIFO:
19551 +       case S_IFSOCK:
19552 +               btail = au_dbtail(dentry);
19553 +               inode->i_op = iop + AuIop_OTHER;
19554 +               init_special_inode(inode, mode, h_inode->i_rdev);
19555 +               break;
19556 +       default:
19557 +               AuIOErr("Unknown file type 0%o\n", mode);
19558 +               err = -EIO;
19559 +               goto out;
19560 +       }
19561 +
19562 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
19563 +       flags = au_hi_flags(inode, isdir);
19564 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19565 +           && au_ftest_hi(flags, HNOTIFY)
19566 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
19567 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19568 +               au_fclr_hi(flags, HNOTIFY);
19569 +       iinfo = au_ii(inode);
19570 +       iinfo->ii_btop = btop;
19571 +       iinfo->ii_bbot = btail;
19572 +       for (bindex = btop; bindex <= btail; bindex++) {
19573 +               h_dentry = au_h_dptr(dentry, bindex);
19574 +               if (h_dentry)
19575 +                       au_set_h_iptr(inode, bindex,
19576 +                                     au_igrab(d_inode(h_dentry)), flags);
19577 +       }
19578 +       au_cpup_attr_all(inode, /*force*/1);
19579 +       /*
19580 +        * to force calling aufs_get_inode_acl() every time,
19581 +        * do not call cache_no_acl() for aufs inode.
19582 +        */
19583 +
19584 +out:
19585 +       return err;
19586 +}
19587 +
19588 +/*
19589 + * successful returns with iinfo write_locked
19590 + * minus: errno
19591 + * zero: success, matched
19592 + * plus: no error, but unmatched
19593 + */
19594 +static int reval_inode(struct inode *inode, struct dentry *dentry)
19595 +{
19596 +       int err;
19597 +       unsigned int gen, igflags;
19598 +       aufs_bindex_t bindex, bbot;
19599 +       struct inode *h_inode, *h_dinode;
19600 +       struct dentry *h_dentry;
19601 +
19602 +       /*
19603 +        * before this function, if aufs got any iinfo lock, it must be only
19604 +        * one, the parent dir.
19605 +        * it can happen by UDBA and the obsoleted inode number.
19606 +        */
19607 +       err = -EIO;
19608 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
19609 +               goto out;
19610 +
19611 +       err = 1;
19612 +       ii_write_lock_new_child(inode);
19613 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
19614 +       h_dinode = d_inode(h_dentry);
19615 +       bbot = au_ibbot(inode);
19616 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
19617 +               h_inode = au_h_iptr(inode, bindex);
19618 +               if (!h_inode || h_inode != h_dinode)
19619 +                       continue;
19620 +
19621 +               err = 0;
19622 +               gen = au_iigen(inode, &igflags);
19623 +               if (gen == au_digen(dentry)
19624 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
19625 +                       break;
19626 +
19627 +               /* fully refresh inode using dentry */
19628 +               err = au_refresh_hinode(inode, dentry);
19629 +               if (!err)
19630 +                       au_update_iigen(inode, /*half*/0);
19631 +               break;
19632 +       }
19633 +
19634 +       if (unlikely(err))
19635 +               ii_write_unlock(inode);
19636 +out:
19637 +       return err;
19638 +}
19639 +
19640 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19641 +          unsigned int d_type, ino_t *ino)
19642 +{
19643 +       int err, idx;
19644 +       const int isnondir = d_type != DT_DIR;
19645 +
19646 +       /* prevent hardlinked inode number from race condition */
19647 +       if (isnondir) {
19648 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19649 +               if (unlikely(err))
19650 +                       goto out;
19651 +       }
19652 +
19653 +       err = au_xino_read(sb, bindex, h_ino, ino);
19654 +       if (unlikely(err))
19655 +               goto out_xinondir;
19656 +
19657 +       if (!*ino) {
19658 +               err = -EIO;
19659 +               *ino = au_xino_new_ino(sb);
19660 +               if (unlikely(!*ino))
19661 +                       goto out_xinondir;
19662 +               err = au_xino_write(sb, bindex, h_ino, *ino);
19663 +               if (unlikely(err))
19664 +                       goto out_xinondir;
19665 +       }
19666 +
19667 +out_xinondir:
19668 +       if (isnondir && idx >= 0)
19669 +               au_xinondir_leave(sb, bindex, h_ino, idx);
19670 +out:
19671 +       return err;
19672 +}
19673 +
19674 +/* successful returns with iinfo write_locked */
19675 +/* todo: return with unlocked? */
19676 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
19677 +{
19678 +       struct inode *inode, *h_inode;
19679 +       struct dentry *h_dentry;
19680 +       struct super_block *sb;
19681 +       ino_t h_ino, ino;
19682 +       int err, idx, hlinked;
19683 +       aufs_bindex_t btop;
19684 +
19685 +       sb = dentry->d_sb;
19686 +       btop = au_dbtop(dentry);
19687 +       h_dentry = au_h_dptr(dentry, btop);
19688 +       h_inode = d_inode(h_dentry);
19689 +       h_ino = h_inode->i_ino;
19690 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
19691 +
19692 +new_ino:
19693 +       /*
19694 +        * stop 'race'-ing between hardlinks under different
19695 +        * parents.
19696 +        */
19697 +       if (hlinked) {
19698 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
19699 +               inode = ERR_PTR(err);
19700 +               if (unlikely(err))
19701 +                       goto out;
19702 +       }
19703 +
19704 +       err = au_xino_read(sb, btop, h_ino, &ino);
19705 +       inode = ERR_PTR(err);
19706 +       if (unlikely(err))
19707 +               goto out_xinondir;
19708 +
19709 +       if (!ino) {
19710 +               ino = au_xino_new_ino(sb);
19711 +               if (unlikely(!ino)) {
19712 +                       inode = ERR_PTR(-EIO);
19713 +                       goto out_xinondir;
19714 +               }
19715 +       }
19716 +
19717 +       AuDbg("i%lu\n", (unsigned long)ino);
19718 +       inode = au_iget_locked(sb, ino);
19719 +       err = PTR_ERR(inode);
19720 +       if (IS_ERR(inode))
19721 +               goto out_xinondir;
19722 +
19723 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19724 +       if (inode->i_state & I_NEW) {
19725 +               ii_write_lock_new_child(inode);
19726 +               err = set_inode(inode, dentry);
19727 +               if (!err) {
19728 +                       unlock_new_inode(inode);
19729 +                       goto out_xinondir; /* success */
19730 +               }
19731 +
19732 +               /*
19733 +                * iget_failed() calls iput(), but we need to call
19734 +                * ii_write_unlock() after iget_failed(). so dirty hack for
19735 +                * i_count.
19736 +                */
19737 +               atomic_inc(&inode->i_count);
19738 +               iget_failed(inode);
19739 +               ii_write_unlock(inode);
19740 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
19741 +               /* ignore this error */
19742 +               goto out_iput;
19743 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
19744 +               /*
19745 +                * horrible race condition between lookup, readdir and copyup
19746 +                * (or something).
19747 +                */
19748 +               if (hlinked && idx >= 0)
19749 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19750 +               err = reval_inode(inode, dentry);
19751 +               if (unlikely(err < 0)) {
19752 +                       hlinked = 0;
19753 +                       goto out_iput;
19754 +               }
19755 +               if (!err)
19756 +                       goto out; /* success */
19757 +               else if (hlinked && idx >= 0) {
19758 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
19759 +                       if (unlikely(err)) {
19760 +                               iput(inode);
19761 +                               inode = ERR_PTR(err);
19762 +                               goto out;
19763 +                       }
19764 +               }
19765 +       }
19766 +
19767 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
19768 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
19769 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
19770 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
19771 +                       (unsigned long)h_ino, (unsigned long)ino);
19772 +       ino = 0;
19773 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
19774 +       if (!err) {
19775 +               iput(inode);
19776 +               if (hlinked && idx >= 0)
19777 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19778 +               goto new_ino;
19779 +       }
19780 +
19781 +out_iput:
19782 +       iput(inode);
19783 +       inode = ERR_PTR(err);
19784 +out_xinondir:
19785 +       if (hlinked && idx >= 0)
19786 +               au_xinondir_leave(sb, btop, h_ino, idx);
19787 +out:
19788 +       return inode;
19789 +}
19790 +
19791 +/* ---------------------------------------------------------------------- */
19792 +
19793 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19794 +              struct inode *inode)
19795 +{
19796 +       int err;
19797 +       struct inode *hi;
19798 +
19799 +       err = au_br_rdonly(au_sbr(sb, bindex));
19800 +
19801 +       /* pseudo-link after flushed may happen out of bounds */
19802 +       if (!err
19803 +           && inode
19804 +           && au_ibtop(inode) <= bindex
19805 +           && bindex <= au_ibbot(inode)) {
19806 +               /*
19807 +                * permission check is unnecessary since vfsub routine
19808 +                * will be called later
19809 +                */
19810 +               hi = au_h_iptr(inode, bindex);
19811 +               if (hi)
19812 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
19813 +       }
19814 +
19815 +       return err;
19816 +}
19817 +
19818 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19819 +                  int mask)
19820 +{
19821 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
19822 +               return 0;
19823 +       return inode_permission(h_idmap, h_inode, mask);
19824 +}
19825 +
19826 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19827 +                      int mask)
19828 +{
19829 +       if (au_test_nfs(h_inode->i_sb)
19830 +           && (mask & MAY_WRITE)
19831 +           && S_ISDIR(h_inode->i_mode))
19832 +               mask |= MAY_READ; /* force permission check */
19833 +       return au_test_h_perm(h_idmap, h_inode, mask);
19834 +}
19835 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
19836 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
19837 +++ linux/fs/aufs/inode.h       2024-05-13 17:13:00.621404284 +0200
19838 @@ -0,0 +1,707 @@
19839 +/* SPDX-License-Identifier: GPL-2.0 */
19840 +/*
19841 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19842 + *
19843 + * This program is free software; you can redistribute it and/or modify
19844 + * it under the terms of the GNU General Public License as published by
19845 + * the Free Software Foundation; either version 2 of the License, or
19846 + * (at your option) any later version.
19847 + *
19848 + * This program is distributed in the hope that it will be useful,
19849 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19850 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19851 + * GNU General Public License for more details.
19852 + *
19853 + * You should have received a copy of the GNU General Public License
19854 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19855 + */
19856 +
19857 +/*
19858 + * inode operations
19859 + */
19860 +
19861 +#ifndef __AUFS_INODE_H__
19862 +#define __AUFS_INODE_H__
19863 +
19864 +#ifdef __KERNEL__
19865 +
19866 +#include <linux/fsnotify.h>
19867 +#include "rwsem.h"
19868 +
19869 +struct vfsmount;
19870 +
19871 +struct au_hnotify {
19872 +#ifdef CONFIG_AUFS_HNOTIFY
19873 +#ifdef CONFIG_AUFS_HFSNOTIFY
19874 +       /* never use fsnotify_add_vfsmount_mark() */
19875 +       struct fsnotify_mark            hn_mark;
19876 +#endif
19877 +       struct inode            *hn_aufs_inode; /* no get/put */
19878 +       struct rcu_head         rcu;
19879 +#endif
19880 +} ____cacheline_aligned_in_smp;
19881 +
19882 +struct au_hinode {
19883 +       struct inode            *hi_inode;
19884 +       aufs_bindex_t           hi_id;
19885 +#ifdef CONFIG_AUFS_HNOTIFY
19886 +       struct au_hnotify       *hi_notify;
19887 +#endif
19888 +
19889 +       /* reference to the copied-up whiteout with get/put */
19890 +       struct dentry           *hi_whdentry;
19891 +};
19892 +
19893 +/* ig_flags */
19894 +#define AuIG_HALF_REFRESHED            1
19895 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
19896 +#define au_ig_fset(flags, name) \
19897 +       do { (flags) |= AuIG_##name; } while (0)
19898 +#define au_ig_fclr(flags, name) \
19899 +       do { (flags) &= ~AuIG_##name; } while (0)
19900 +
19901 +struct au_iigen {
19902 +       spinlock_t      ig_spin;
19903 +       __u32           ig_generation, ig_flags;
19904 +};
19905 +
19906 +struct au_vdir;
19907 +struct au_iinfo {
19908 +       struct au_iigen         ii_generation;
19909 +       struct super_block      *ii_hsb1;       /* no get/put */
19910 +
19911 +       struct au_rwsem         ii_rwsem;
19912 +       aufs_bindex_t           ii_btop, ii_bbot;
19913 +       __u32                   ii_higen;
19914 +       struct au_hinode        *ii_hinode;
19915 +       struct au_vdir          *ii_vdir;
19916 +};
19917 +
19918 +struct au_icntnr {
19919 +       struct au_iinfo         iinfo;
19920 +       struct inode            vfs_inode;
19921 +       struct hlist_bl_node    plink;
19922 +       struct rcu_head         rcu;
19923 +} ____cacheline_aligned_in_smp;
19924 +
19925 +/* au_pin flags */
19926 +#define AuPin_DI_LOCKED                BIT(0)
19927 +#define AuPin_MNT_WRITE                BIT(1)
19928 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
19929 +#define au_fset_pin(flags, name) \
19930 +       do { (flags) |= AuPin_##name; } while (0)
19931 +#define au_fclr_pin(flags, name) \
19932 +       do { (flags) &= ~AuPin_##name; } while (0)
19933 +
19934 +struct au_pin {
19935 +       /* input */
19936 +       struct dentry *dentry;
19937 +       unsigned int udba;
19938 +       unsigned char lsc_di, lsc_hi, flags;
19939 +       aufs_bindex_t bindex;
19940 +
19941 +       /* output */
19942 +       struct dentry *parent;
19943 +       struct au_hinode *hdir;
19944 +       struct vfsmount *h_mnt;
19945 +
19946 +       /* temporary unlock/relock for copyup */
19947 +       struct dentry *h_dentry, *h_parent;
19948 +       struct au_branch *br;
19949 +       struct task_struct *task;
19950 +};
19951 +
19952 +void au_pin_hdir_unlock(struct au_pin *p);
19953 +int au_pin_hdir_lock(struct au_pin *p);
19954 +int au_pin_hdir_relock(struct au_pin *p);
19955 +void au_pin_hdir_acquire_nest(struct au_pin *p);
19956 +void au_pin_hdir_release(struct au_pin *p);
19957 +
19958 +/* ---------------------------------------------------------------------- */
19959 +
19960 +static inline struct au_iinfo *au_ii(struct inode *inode)
19961 +{
19962 +       BUG_ON(is_bad_inode(inode));
19963 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19964 +}
19965 +
19966 +/* ---------------------------------------------------------------------- */
19967 +
19968 +/* inode.c */
19969 +struct inode *au_igrab(struct inode *inode);
19970 +void au_refresh_iop(struct inode *inode, int force_getattr);
19971 +int au_refresh_hinode_self(struct inode *inode);
19972 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19973 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19974 +          unsigned int d_type, ino_t *ino);
19975 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
19976 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19977 +              struct inode *inode);
19978 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19979 +                  int mask);
19980 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19981 +                      int mask);
19982 +
19983 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19984 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
19985 +{
19986 +#ifdef CONFIG_AUFS_SHWH
19987 +       return au_ino(sb, bindex, h_ino, d_type, ino);
19988 +#else
19989 +       return 0;
19990 +#endif
19991 +}
19992 +
19993 +/* i_op.c */
19994 +enum {
19995 +       AuIop_SYMLINK,
19996 +       AuIop_DIR,
19997 +       AuIop_OTHER,
19998 +       AuIop_Last
19999 +};
20000 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
20001 +       aufs_iop_nogetattr[AuIop_Last];
20002 +
20003 +/* au_wr_dir flags */
20004 +#define AuWrDir_ADD_ENTRY      BIT(0)
20005 +#define AuWrDir_ISDIR          BIT(1)
20006 +#define AuWrDir_TMPFILE                BIT(2)
20007 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
20008 +#define au_fset_wrdir(flags, name) \
20009 +       do { (flags) |= AuWrDir_##name; } while (0)
20010 +#define au_fclr_wrdir(flags, name) \
20011 +       do { (flags) &= ~AuWrDir_##name; } while (0)
20012 +
20013 +struct au_wr_dir_args {
20014 +       aufs_bindex_t force_btgt;
20015 +       unsigned char flags;
20016 +};
20017 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20018 +             struct au_wr_dir_args *args);
20019 +
20020 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
20021 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
20022 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
20023 +                unsigned int udba, unsigned char flags);
20024 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
20025 +          unsigned int udba, unsigned char flags) __must_check;
20026 +int au_do_pin(struct au_pin *pin) __must_check;
20027 +void au_unpin(struct au_pin *pin);
20028 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
20029 +
20030 +#define AuIcpup_DID_CPUP       1
20031 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
20032 +#define au_fset_icpup(flags, name) \
20033 +       do { (flags) |= AuIcpup_##name; } while (0)
20034 +#define au_fclr_icpup(flags, name) \
20035 +       do { (flags) &= ~AuIcpup_##name; } while (0)
20036 +
20037 +struct au_icpup_args {
20038 +       unsigned char flags;
20039 +       unsigned char pin_flags;
20040 +       aufs_bindex_t btgt;
20041 +       unsigned int udba;
20042 +       struct au_pin pin;
20043 +       struct path h_path;
20044 +       struct inode *h_inode;
20045 +};
20046 +
20047 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20048 +                    struct au_icpup_args *a);
20049 +
20050 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20051 +                     struct path *h_path, int locked);
20052 +
20053 +/* i_op_add.c */
20054 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20055 +              struct dentry *h_parent, int isdir);
20056 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
20057 +              struct dentry *dentry, umode_t mode, dev_t dev);
20058 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
20059 +                struct dentry *dentry, const char *symname);
20060 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
20061 +               struct dentry *dentry, umode_t mode, bool want_excl);
20062 +struct vfsub_aopen_args;
20063 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20064 +                      struct vfsub_aopen_args *args);
20065 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
20066 +                struct file *file, umode_t mode);
20067 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20068 +             struct dentry *dentry);
20069 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
20070 +              struct dentry *dentry, umode_t mode);
20071 +
20072 +/* i_op_del.c */
20073 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20074 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20075 +              struct dentry *h_parent, int isdir);
20076 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
20077 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
20078 +
20079 +/* i_op_ren.c */
20080 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
20081 +int aufs_rename(struct mnt_idmap *idmap,
20082 +               struct inode *_src_dir, struct dentry *_src_dentry,
20083 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
20084 +               unsigned int _flags);
20085 +
20086 +/* iinfo.c */
20087 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20088 +void au_hiput(struct au_hinode *hinode);
20089 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20090 +                 struct dentry *h_wh);
20091 +unsigned int au_hi_flags(struct inode *inode, int isdir);
20092 +
20093 +/* hinode flags */
20094 +#define AuHi_XINO      BIT(0)
20095 +#define AuHi_HNOTIFY   BIT(1)
20096 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
20097 +#define au_fset_hi(flags, name) \
20098 +       do { (flags) |= AuHi_##name; } while (0)
20099 +#define au_fclr_hi(flags, name) \
20100 +       do { (flags) &= ~AuHi_##name; } while (0)
20101 +
20102 +#ifndef CONFIG_AUFS_HNOTIFY
20103 +#undef AuHi_HNOTIFY
20104 +#define AuHi_HNOTIFY   0
20105 +#endif
20106 +
20107 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20108 +                  struct inode *h_inode, unsigned int flags);
20109 +
20110 +void au_update_iigen(struct inode *inode, int half);
20111 +void au_update_ibrange(struct inode *inode, int do_put_zero);
20112 +
20113 +void au_icntnr_init_once(void *_c);
20114 +void au_hinode_init(struct au_hinode *hinode);
20115 +int au_iinfo_init(struct inode *inode);
20116 +void au_iinfo_fin(struct inode *inode);
20117 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
20118 +
20119 +#ifdef CONFIG_PROC_FS
20120 +/* plink.c */
20121 +int au_plink_maint(struct super_block *sb, int flags);
20122 +struct au_sbinfo;
20123 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20124 +int au_plink_maint_enter(struct super_block *sb);
20125 +#ifdef CONFIG_AUFS_DEBUG
20126 +void au_plink_list(struct super_block *sb);
20127 +#else
20128 +AuStubVoid(au_plink_list, struct super_block *sb)
20129 +#endif
20130 +int au_plink_test(struct inode *inode);
20131 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20132 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20133 +                    struct dentry *h_dentry);
20134 +void au_plink_put(struct super_block *sb, int verbose);
20135 +void au_plink_clean(struct super_block *sb, int verbose);
20136 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
20137 +#else
20138 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20139 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20140 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20141 +AuStubVoid(au_plink_list, struct super_block *sb);
20142 +AuStubInt0(au_plink_test, struct inode *inode);
20143 +AuStub(struct dentry *, au_plink_lkup, return NULL,
20144 +       struct inode *inode, aufs_bindex_t bindex);
20145 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20146 +          struct dentry *h_dentry);
20147 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20148 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20149 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20150 +#endif /* CONFIG_PROC_FS */
20151 +
20152 +#ifdef CONFIG_AUFS_XATTR
20153 +/* xattr.c */
20154 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
20155 +                 unsigned int verbose);
20156 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
20157 +void au_xattr_init(struct super_block *sb);
20158 +#else
20159 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
20160 +          int ignore_flags, unsigned int verbose);
20161 +AuStubVoid(au_xattr_init, struct super_block *sb);
20162 +#endif
20163 +
20164 +#ifdef CONFIG_FS_POSIX_ACL
20165 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu);
20166 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
20167 +                              struct dentry *dentry, int type);
20168 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
20169 +                struct posix_acl *acl, int type);
20170 +#endif
20171 +
20172 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20173 +enum {
20174 +       AU_XATTR_SET,
20175 +       AU_ACL_SET
20176 +};
20177 +
20178 +struct au_sxattr {
20179 +       int type;
20180 +       union {
20181 +               struct {
20182 +                       const char      *name;
20183 +                       const void      *value;
20184 +                       size_t          size;
20185 +                       int             flags;
20186 +               } set;
20187 +               struct {
20188 +                       struct posix_acl *acl;
20189 +                       int             type;
20190 +               } acl_set;
20191 +       } u;
20192 +};
20193 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20194 +                 struct au_sxattr *arg);
20195 +#endif
20196 +
20197 +/* ---------------------------------------------------------------------- */
20198 +
20199 +/* lock subclass for iinfo */
20200 +enum {
20201 +       AuLsc_II_CHILD,         /* child first */
20202 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
20203 +       AuLsc_II_CHILD3,        /* copyup dirs */
20204 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
20205 +       AuLsc_II_PARENT2,
20206 +       AuLsc_II_PARENT3,       /* copyup dirs */
20207 +       AuLsc_II_NEW_CHILD
20208 +};
20209 +
20210 +/*
20211 + * ii_read_lock_child, ii_write_lock_child,
20212 + * ii_read_lock_child2, ii_write_lock_child2,
20213 + * ii_read_lock_child3, ii_write_lock_child3,
20214 + * ii_read_lock_parent, ii_write_lock_parent,
20215 + * ii_read_lock_parent2, ii_write_lock_parent2,
20216 + * ii_read_lock_parent3, ii_write_lock_parent3,
20217 + * ii_read_lock_new_child, ii_write_lock_new_child,
20218 + */
20219 +#define AuReadLockFunc(name, lsc) \
20220 +static inline void ii_read_lock_##name(struct inode *i) \
20221 +{ \
20222 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20223 +}
20224 +
20225 +#define AuWriteLockFunc(name, lsc) \
20226 +static inline void ii_write_lock_##name(struct inode *i) \
20227 +{ \
20228 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20229 +}
20230 +
20231 +#define AuRWLockFuncs(name, lsc) \
20232 +       AuReadLockFunc(name, lsc) \
20233 +       AuWriteLockFunc(name, lsc)
20234 +
20235 +AuRWLockFuncs(child, CHILD);
20236 +AuRWLockFuncs(child2, CHILD2);
20237 +AuRWLockFuncs(child3, CHILD3);
20238 +AuRWLockFuncs(parent, PARENT);
20239 +AuRWLockFuncs(parent2, PARENT2);
20240 +AuRWLockFuncs(parent3, PARENT3);
20241 +AuRWLockFuncs(new_child, NEW_CHILD);
20242 +
20243 +#undef AuReadLockFunc
20244 +#undef AuWriteLockFunc
20245 +#undef AuRWLockFuncs
20246 +
20247 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20248 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20249 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
20250 +
20251 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20252 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20253 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
20254 +
20255 +/* ---------------------------------------------------------------------- */
20256 +
20257 +static inline void au_icntnr_init(struct au_icntnr *c)
20258 +{
20259 +#ifdef CONFIG_AUFS_DEBUG
20260 +       c->vfs_inode.i_mode = 0;
20261 +#endif
20262 +}
20263 +
20264 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
20265 +{
20266 +       unsigned int gen;
20267 +       struct au_iinfo *iinfo;
20268 +       struct au_iigen *iigen;
20269 +
20270 +       iinfo = au_ii(inode);
20271 +       iigen = &iinfo->ii_generation;
20272 +       spin_lock(&iigen->ig_spin);
20273 +       if (igflags)
20274 +               *igflags = iigen->ig_flags;
20275 +       gen = iigen->ig_generation;
20276 +       spin_unlock(&iigen->ig_spin);
20277 +
20278 +       return gen;
20279 +}
20280 +
20281 +/* tiny test for inode number */
20282 +/* tmpfs generation is too rough */
20283 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20284 +{
20285 +       struct au_iinfo *iinfo;
20286 +
20287 +       iinfo = au_ii(inode);
20288 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
20289 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
20290 +                && iinfo->ii_higen == h_inode->i_generation);
20291 +}
20292 +
20293 +static inline void au_iigen_dec(struct inode *inode)
20294 +{
20295 +       struct au_iinfo *iinfo;
20296 +       struct au_iigen *iigen;
20297 +
20298 +       iinfo = au_ii(inode);
20299 +       iigen = &iinfo->ii_generation;
20300 +       spin_lock(&iigen->ig_spin);
20301 +       iigen->ig_generation--;
20302 +       spin_unlock(&iigen->ig_spin);
20303 +}
20304 +
20305 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20306 +{
20307 +       int err;
20308 +
20309 +       err = 0;
20310 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
20311 +               err = -EIO;
20312 +
20313 +       return err;
20314 +}
20315 +
20316 +/* ---------------------------------------------------------------------- */
20317 +
20318 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20319 +                                         aufs_bindex_t bindex)
20320 +{
20321 +       return iinfo->ii_hinode + bindex;
20322 +}
20323 +
20324 +static inline int au_is_bad_inode(struct inode *inode)
20325 +{
20326 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20327 +}
20328 +
20329 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20330 +                                       aufs_bindex_t bindex)
20331 +{
20332 +       IiMustAnyLock(inode);
20333 +       return au_hinode(au_ii(inode), bindex)->hi_id;
20334 +}
20335 +
20336 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
20337 +{
20338 +       IiMustAnyLock(inode);
20339 +       return au_ii(inode)->ii_btop;
20340 +}
20341 +
20342 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
20343 +{
20344 +       IiMustAnyLock(inode);
20345 +       return au_ii(inode)->ii_bbot;
20346 +}
20347 +
20348 +static inline struct au_vdir *au_ivdir(struct inode *inode)
20349 +{
20350 +       IiMustAnyLock(inode);
20351 +       return au_ii(inode)->ii_vdir;
20352 +}
20353 +
20354 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20355 +{
20356 +       IiMustAnyLock(inode);
20357 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
20358 +}
20359 +
20360 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
20361 +{
20362 +       IiMustWriteLock(inode);
20363 +       au_ii(inode)->ii_btop = bindex;
20364 +}
20365 +
20366 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
20367 +{
20368 +       IiMustWriteLock(inode);
20369 +       au_ii(inode)->ii_bbot = bindex;
20370 +}
20371 +
20372 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20373 +{
20374 +       IiMustWriteLock(inode);
20375 +       au_ii(inode)->ii_vdir = vdir;
20376 +}
20377 +
20378 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
20379 +{
20380 +       IiMustAnyLock(inode);
20381 +       return au_hinode(au_ii(inode), bindex);
20382 +}
20383 +
20384 +/* ---------------------------------------------------------------------- */
20385 +
20386 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20387 +{
20388 +       if (pin)
20389 +               return pin->parent;
20390 +       return NULL;
20391 +}
20392 +
20393 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
20394 +{
20395 +       if (pin && pin->hdir)
20396 +               return pin->hdir->hi_inode;
20397 +       return NULL;
20398 +}
20399 +
20400 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20401 +{
20402 +       if (pin)
20403 +               return pin->hdir;
20404 +       return NULL;
20405 +}
20406 +
20407 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
20408 +{
20409 +       if (pin)
20410 +               pin->dentry = dentry;
20411 +}
20412 +
20413 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20414 +                                          unsigned char lflag)
20415 +{
20416 +       if (pin) {
20417 +               if (lflag)
20418 +                       au_fset_pin(pin->flags, DI_LOCKED);
20419 +               else
20420 +                       au_fclr_pin(pin->flags, DI_LOCKED);
20421 +       }
20422 +}
20423 +
20424 +#if 0 /* reserved */
20425 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20426 +{
20427 +       if (pin) {
20428 +               dput(pin->parent);
20429 +               pin->parent = dget(parent);
20430 +       }
20431 +}
20432 +#endif
20433 +
20434 +/* ---------------------------------------------------------------------- */
20435 +
20436 +struct au_branch;
20437 +#ifdef CONFIG_AUFS_HNOTIFY
20438 +struct au_hnotify_op {
20439 +       void (*ctl)(struct au_hinode *hinode, int do_set);
20440 +       int (*alloc)(struct au_hinode *hinode);
20441 +
20442 +       /*
20443 +        * if it returns true, the caller should free hinode->hi_notify,
20444 +        * otherwise ->free() frees it.
20445 +        */
20446 +       int (*free)(struct au_hinode *hinode,
20447 +                   struct au_hnotify *hn) __must_check;
20448 +
20449 +       void (*fin)(void);
20450 +       int (*init)(void);
20451 +
20452 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20453 +       void (*fin_br)(struct au_branch *br);
20454 +       int (*init_br)(struct au_branch *br, int perm);
20455 +};
20456 +
20457 +/* hnotify.c */
20458 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
20459 +void au_hn_free(struct au_hinode *hinode);
20460 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
20461 +void au_hn_reset(struct inode *inode, unsigned int flags);
20462 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
20463 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
20464 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20465 +int au_hnotify_init_br(struct au_branch *br, int perm);
20466 +void au_hnotify_fin_br(struct au_branch *br);
20467 +int __init au_hnotify_init(void);
20468 +void au_hnotify_fin(void);
20469 +
20470 +/* hfsnotify.c */
20471 +extern const struct au_hnotify_op au_hnotify_op;
20472 +
20473 +static inline
20474 +void au_hn_init(struct au_hinode *hinode)
20475 +{
20476 +       hinode->hi_notify = NULL;
20477 +}
20478 +
20479 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20480 +{
20481 +       return hinode->hi_notify;
20482 +}
20483 +
20484 +#else
20485 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20486 +       struct au_hinode *hinode __maybe_unused,
20487 +       struct inode *inode __maybe_unused)
20488 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
20489 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20490 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20491 +          int do_set __maybe_unused)
20492 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20493 +          unsigned int flags __maybe_unused)
20494 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20495 +          struct au_branch *br __maybe_unused,
20496 +          int perm __maybe_unused)
20497 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20498 +          int perm __maybe_unused)
20499 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
20500 +AuStubInt0(__init au_hnotify_init, void)
20501 +AuStubVoid(au_hnotify_fin, void)
20502 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20503 +#endif /* CONFIG_AUFS_HNOTIFY */
20504 +
20505 +static inline void au_hn_suspend(struct au_hinode *hdir)
20506 +{
20507 +       au_hn_ctl(hdir, /*do_set*/0);
20508 +}
20509 +
20510 +static inline void au_hn_resume(struct au_hinode *hdir)
20511 +{
20512 +       au_hn_ctl(hdir, /*do_set*/1);
20513 +}
20514 +
20515 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
20516 +{
20517 +       inode_lock(hdir->hi_inode);
20518 +       au_hn_suspend(hdir);
20519 +}
20520 +
20521 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
20522 +                                         unsigned int sc __maybe_unused)
20523 +{
20524 +       inode_lock_nested(hdir->hi_inode, sc);
20525 +       au_hn_suspend(hdir);
20526 +}
20527 +
20528 +#if 0 /* unused */
20529 +#include "vfsub.h"
20530 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20531 +                                                 unsigned int sc)
20532 +{
20533 +       inode_lock_shared_nested(hdir->hi_inode, sc);
20534 +       au_hn_suspend(hdir);
20535 +}
20536 +#endif
20537 +
20538 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
20539 +{
20540 +       au_hn_resume(hdir);
20541 +       inode_unlock(hdir->hi_inode);
20542 +}
20543 +
20544 +#endif /* __KERNEL__ */
20545 +#endif /* __AUFS_INODE_H__ */
20546 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
20547 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
20548 +++ linux/fs/aufs/ioctl.c       2024-05-13 17:13:00.621404284 +0200
20549 @@ -0,0 +1,220 @@
20550 +// SPDX-License-Identifier: GPL-2.0
20551 +/*
20552 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20553 + *
20554 + * This program is free software; you can redistribute it and/or modify
20555 + * it under the terms of the GNU General Public License as published by
20556 + * the Free Software Foundation; either version 2 of the License, or
20557 + * (at your option) any later version.
20558 + *
20559 + * This program is distributed in the hope that it will be useful,
20560 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20561 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20562 + * GNU General Public License for more details.
20563 + *
20564 + * You should have received a copy of the GNU General Public License
20565 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20566 + */
20567 +
20568 +/*
20569 + * ioctl
20570 + * plink-management and readdir in userspace.
20571 + * assist the pathconf(3) wrapper library.
20572 + * move-down
20573 + * File-based Hierarchical Storage Management.
20574 + */
20575 +
20576 +#include <linux/compat.h>
20577 +#include <linux/file.h>
20578 +#include "aufs.h"
20579 +
20580 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
20581 +{
20582 +       int err, fd;
20583 +       aufs_bindex_t wbi, bindex, bbot;
20584 +       struct file *h_file;
20585 +       struct super_block *sb;
20586 +       struct dentry *root;
20587 +       struct au_branch *br;
20588 +       struct aufs_wbr_fd wbrfd = {
20589 +               .oflags = au_dir_roflags,
20590 +               .brid   = -1
20591 +       };
20592 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20593 +               | O_NOATIME | O_CLOEXEC;
20594 +
20595 +       AuDebugOn(wbrfd.oflags & ~valid);
20596 +
20597 +       if (arg) {
20598 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20599 +               if (unlikely(err)) {
20600 +                       err = -EFAULT;
20601 +                       goto out;
20602 +               }
20603 +
20604 +               err = -EINVAL;
20605 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20606 +               wbrfd.oflags |= au_dir_roflags;
20607 +               AuDbg("0%o\n", wbrfd.oflags);
20608 +               if (unlikely(wbrfd.oflags & ~valid))
20609 +                       goto out;
20610 +       }
20611 +
20612 +       fd = get_unused_fd_flags(0);
20613 +       err = fd;
20614 +       if (unlikely(fd < 0))
20615 +               goto out;
20616 +
20617 +       h_file = ERR_PTR(-EINVAL);
20618 +       wbi = 0;
20619 +       br = NULL;
20620 +       sb = path->dentry->d_sb;
20621 +       root = sb->s_root;
20622 +       aufs_read_lock(root, AuLock_IR);
20623 +       bbot = au_sbbot(sb);
20624 +       if (wbrfd.brid >= 0) {
20625 +               wbi = au_br_index(sb, wbrfd.brid);
20626 +               if (unlikely(wbi < 0 || wbi > bbot))
20627 +                       goto out_unlock;
20628 +       }
20629 +
20630 +       h_file = ERR_PTR(-ENOENT);
20631 +       br = au_sbr(sb, wbi);
20632 +       if (!au_br_writable(br->br_perm)) {
20633 +               if (arg)
20634 +                       goto out_unlock;
20635 +
20636 +               bindex = wbi + 1;
20637 +               wbi = -1;
20638 +               for (; bindex <= bbot; bindex++) {
20639 +                       br = au_sbr(sb, bindex);
20640 +                       if (au_br_writable(br->br_perm)) {
20641 +                               wbi = bindex;
20642 +                               br = au_sbr(sb, wbi);
20643 +                               break;
20644 +                       }
20645 +               }
20646 +       }
20647 +       AuDbg("wbi %d\n", wbi);
20648 +       if (wbi >= 0)
20649 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20650 +                                  /*force_wr*/0);
20651 +
20652 +out_unlock:
20653 +       aufs_read_unlock(root, AuLock_IR);
20654 +       err = PTR_ERR(h_file);
20655 +       if (IS_ERR(h_file))
20656 +               goto out_fd;
20657 +
20658 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
20659 +       fd_install(fd, h_file);
20660 +       err = fd;
20661 +       goto out; /* success */
20662 +
20663 +out_fd:
20664 +       put_unused_fd(fd);
20665 +out:
20666 +       AuTraceErr(err);
20667 +       return err;
20668 +}
20669 +
20670 +/* ---------------------------------------------------------------------- */
20671 +
20672 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20673 +{
20674 +       long err;
20675 +       struct dentry *dentry;
20676 +
20677 +       switch (cmd) {
20678 +       case AUFS_CTL_RDU:
20679 +       case AUFS_CTL_RDU_INO:
20680 +               err = au_rdu_ioctl(file, cmd, arg);
20681 +               break;
20682 +
20683 +       case AUFS_CTL_WBR_FD:
20684 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20685 +               break;
20686 +
20687 +       case AUFS_CTL_IBUSY:
20688 +               err = au_ibusy_ioctl(file, arg);
20689 +               break;
20690 +
20691 +       case AUFS_CTL_BRINFO:
20692 +               err = au_brinfo_ioctl(file, arg);
20693 +               break;
20694 +
20695 +       case AUFS_CTL_FHSM_FD:
20696 +               dentry = file->f_path.dentry;
20697 +               if (IS_ROOT(dentry))
20698 +                       err = au_fhsm_fd(dentry->d_sb, arg);
20699 +               else
20700 +                       err = -ENOTTY;
20701 +               break;
20702 +
20703 +       default:
20704 +               /* do not call the lower */
20705 +               AuDbg("0x%x\n", cmd);
20706 +               err = -ENOTTY;
20707 +       }
20708 +
20709 +       AuTraceErr(err);
20710 +       return err;
20711 +}
20712 +
20713 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20714 +{
20715 +       long err;
20716 +
20717 +       switch (cmd) {
20718 +       case AUFS_CTL_MVDOWN:
20719 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
20720 +               break;
20721 +
20722 +       case AUFS_CTL_WBR_FD:
20723 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20724 +               break;
20725 +
20726 +       default:
20727 +               /* do not call the lower */
20728 +               AuDbg("0x%x\n", cmd);
20729 +               err = -ENOTTY;
20730 +       }
20731 +
20732 +       AuTraceErr(err);
20733 +       return err;
20734 +}
20735 +
20736 +#ifdef CONFIG_COMPAT
20737 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20738 +                          unsigned long arg)
20739 +{
20740 +       long err;
20741 +
20742 +       switch (cmd) {
20743 +       case AUFS_CTL_RDU:
20744 +       case AUFS_CTL_RDU_INO:
20745 +               err = au_rdu_compat_ioctl(file, cmd, arg);
20746 +               break;
20747 +
20748 +       case AUFS_CTL_IBUSY:
20749 +               err = au_ibusy_compat_ioctl(file, arg);
20750 +               break;
20751 +
20752 +       case AUFS_CTL_BRINFO:
20753 +               err = au_brinfo_compat_ioctl(file, arg);
20754 +               break;
20755 +
20756 +       default:
20757 +               err = aufs_ioctl_dir(file, cmd, arg);
20758 +       }
20759 +
20760 +       AuTraceErr(err);
20761 +       return err;
20762 +}
20763 +
20764 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20765 +                             unsigned long arg)
20766 +{
20767 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20768 +}
20769 +#endif
20770 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
20771 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
20772 +++ linux/fs/aufs/i_op_add.c    2024-05-13 17:13:00.618070924 +0200
20773 @@ -0,0 +1,972 @@
20774 +// SPDX-License-Identifier: GPL-2.0
20775 +/*
20776 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20777 + *
20778 + * This program is free software; you can redistribute it and/or modify
20779 + * it under the terms of the GNU General Public License as published by
20780 + * the Free Software Foundation; either version 2 of the License, or
20781 + * (at your option) any later version.
20782 + *
20783 + * This program is distributed in the hope that it will be useful,
20784 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20785 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20786 + * GNU General Public License for more details.
20787 + *
20788 + * You should have received a copy of the GNU General Public License
20789 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20790 + */
20791 +
20792 +/*
20793 + * inode operations (add entry)
20794 + */
20795 +
20796 +#include <linux/iversion.h>
20797 +#include "aufs.h"
20798 +
20799 +/*
20800 + * final procedure of adding a new entry, except link(2).
20801 + * remove whiteout, instantiate, copyup the parent dir's times and size
20802 + * and update version.
20803 + * if it failed, re-create the removed whiteout.
20804 + */
20805 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20806 +                 struct dentry *wh_dentry, struct dentry *dentry)
20807 +{
20808 +       int err, rerr;
20809 +       aufs_bindex_t bwh;
20810 +       struct path h_path;
20811 +       struct super_block *sb;
20812 +       struct inode *inode, *h_dir;
20813 +       struct dentry *wh;
20814 +
20815 +       bwh = -1;
20816 +       sb = dir->i_sb;
20817 +       if (wh_dentry) {
20818 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20819 +               IMustLock(h_dir);
20820 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20821 +               bwh = au_dbwh(dentry);
20822 +               h_path.dentry = wh_dentry;
20823 +               h_path.mnt = au_sbr_mnt(sb, bindex);
20824 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20825 +                                         dentry);
20826 +               if (unlikely(err))
20827 +                       goto out;
20828 +       }
20829 +
20830 +       inode = au_new_inode(dentry, /*must_new*/1);
20831 +       if (!IS_ERR(inode)) {
20832 +               d_instantiate(dentry, inode);
20833 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
20834 +               IMustLock(dir);
20835 +               au_dir_ts(dir, bindex);
20836 +               inode_inc_iversion(dir);
20837 +               au_fhsm_wrote(sb, bindex, /*force*/0);
20838 +               return 0; /* success */
20839 +       }
20840 +
20841 +       err = PTR_ERR(inode);
20842 +       if (!wh_dentry)
20843 +               goto out;
20844 +
20845 +       /* revert */
20846 +       /* dir inode is locked */
20847 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20848 +       rerr = PTR_ERR(wh);
20849 +       if (IS_ERR(wh)) {
20850 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20851 +                       dentry, err, rerr);
20852 +               err = -EIO;
20853 +       } else
20854 +               dput(wh);
20855 +
20856 +out:
20857 +       return err;
20858 +}
20859 +
20860 +static int au_d_may_add(struct dentry *dentry)
20861 +{
20862 +       int err;
20863 +
20864 +       err = 0;
20865 +       if (unlikely(d_unhashed(dentry)))
20866 +               err = -ENOENT;
20867 +       if (unlikely(d_really_is_positive(dentry)))
20868 +               err = -EEXIST;
20869 +       return err;
20870 +}
20871 +
20872 +/*
20873 + * simple tests for the adding inode operations.
20874 + * following the checks in vfs, plus the parent-child relationship.
20875 + */
20876 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20877 +              struct dentry *h_parent, int isdir)
20878 +{
20879 +       int err;
20880 +       umode_t h_mode;
20881 +       struct dentry *h_dentry;
20882 +       struct inode *h_inode;
20883 +
20884 +       err = -ENAMETOOLONG;
20885 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20886 +               goto out;
20887 +
20888 +       h_dentry = au_h_dptr(dentry, bindex);
20889 +       if (d_really_is_negative(dentry)) {
20890 +               err = -EEXIST;
20891 +               if (unlikely(d_is_positive(h_dentry)))
20892 +                       goto out;
20893 +       } else {
20894 +               /* rename(2) case */
20895 +               err = -EIO;
20896 +               if (unlikely(d_is_negative(h_dentry)))
20897 +                       goto out;
20898 +               h_inode = d_inode(h_dentry);
20899 +               if (unlikely(!h_inode->i_nlink))
20900 +                       goto out;
20901 +
20902 +               h_mode = h_inode->i_mode;
20903 +               if (!isdir) {
20904 +                       err = -EISDIR;
20905 +                       if (unlikely(S_ISDIR(h_mode)))
20906 +                               goto out;
20907 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20908 +                       err = -ENOTDIR;
20909 +                       goto out;
20910 +               }
20911 +       }
20912 +
20913 +       err = 0;
20914 +       /* expected parent dir is locked */
20915 +       if (unlikely(h_parent != h_dentry->d_parent))
20916 +               err = -EIO;
20917 +
20918 +out:
20919 +       AuTraceErr(err);
20920 +       return err;
20921 +}
20922 +
20923 +/*
20924 + * initial procedure of adding a new entry.
20925 + * prepare writable branch and the parent dir, lock it,
20926 + * and lookup whiteout for the new entry.
20927 + */
20928 +static struct dentry*
20929 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20930 +                 struct dentry *src_dentry, struct au_pin *pin,
20931 +                 struct au_wr_dir_args *wr_dir_args)
20932 +{
20933 +       struct dentry *wh_dentry, *h_parent;
20934 +       struct super_block *sb;
20935 +       struct au_branch *br;
20936 +       int err;
20937 +       unsigned int udba;
20938 +       aufs_bindex_t bcpup;
20939 +
20940 +       AuDbg("%pd\n", dentry);
20941 +
20942 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20943 +       bcpup = err;
20944 +       wh_dentry = ERR_PTR(err);
20945 +       if (unlikely(err < 0))
20946 +               goto out;
20947 +
20948 +       sb = dentry->d_sb;
20949 +       udba = au_opt_udba(sb);
20950 +       err = au_pin(pin, dentry, bcpup, udba,
20951 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20952 +       wh_dentry = ERR_PTR(err);
20953 +       if (unlikely(err))
20954 +               goto out;
20955 +
20956 +       h_parent = au_pinned_h_parent(pin);
20957 +       if (udba != AuOpt_UDBA_NONE
20958 +           && au_dbtop(dentry) == bcpup)
20959 +               err = au_may_add(dentry, bcpup, h_parent,
20960 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20961 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20962 +               err = -ENAMETOOLONG;
20963 +       wh_dentry = ERR_PTR(err);
20964 +       if (unlikely(err))
20965 +               goto out_unpin;
20966 +
20967 +       br = au_sbr(sb, bcpup);
20968 +       if (dt) {
20969 +               struct path tmp = {
20970 +                       .dentry = h_parent,
20971 +                       .mnt    = au_br_mnt(br)
20972 +               };
20973 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20974 +       }
20975 +
20976 +       wh_dentry = NULL;
20977 +       if (bcpup != au_dbwh(dentry))
20978 +               goto out; /* success */
20979 +
20980 +       /*
20981 +        * ENAMETOOLONG here means that if we allowed create such name, then it
20982 +        * would not be able to removed in the future. So we don't allow such
20983 +        * name here and we don't handle ENAMETOOLONG differently here.
20984 +        */
20985 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20986 +
20987 +out_unpin:
20988 +       if (IS_ERR(wh_dentry))
20989 +               au_unpin(pin);
20990 +out:
20991 +       return wh_dentry;
20992 +}
20993 +
20994 +/* ---------------------------------------------------------------------- */
20995 +
20996 +enum { Mknod, Symlink, Creat };
20997 +struct simple_arg {
20998 +       int type;
20999 +       union {
21000 +               struct {
21001 +                       umode_t                 mode;
21002 +                       bool                    want_excl;
21003 +                       bool                    try_aopen;
21004 +                       struct vfsub_aopen_args *aopen;
21005 +               } c;
21006 +               struct {
21007 +                       const char *symname;
21008 +               } s;
21009 +               struct {
21010 +                       umode_t mode;
21011 +                       dev_t dev;
21012 +               } m;
21013 +       } u;
21014 +};
21015 +
21016 +static int add_simple(struct inode *dir, struct dentry *dentry,
21017 +                     struct simple_arg *arg)
21018 +{
21019 +       int err, rerr;
21020 +       aufs_bindex_t btop;
21021 +       unsigned char created;
21022 +       const unsigned char try_aopen
21023 +               = (arg->type == Creat && arg->u.c.try_aopen);
21024 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
21025 +       struct dentry *wh_dentry, *parent;
21026 +       struct inode *h_dir;
21027 +       struct super_block *sb;
21028 +       struct au_branch *br;
21029 +       /* to reduce stack size */
21030 +       struct {
21031 +               struct au_dtime dt;
21032 +               struct au_pin pin;
21033 +               struct path h_path;
21034 +               struct au_wr_dir_args wr_dir_args;
21035 +       } *a;
21036 +
21037 +       AuDbg("%pd\n", dentry);
21038 +       IMustLock(dir);
21039 +
21040 +       err = -ENOMEM;
21041 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21042 +       if (unlikely(!a))
21043 +               goto out;
21044 +       a->wr_dir_args.force_btgt = -1;
21045 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21046 +
21047 +       parent = dentry->d_parent; /* dir inode is locked */
21048 +       if (!try_aopen) {
21049 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21050 +               if (unlikely(err))
21051 +                       goto out_free;
21052 +       }
21053 +       err = au_d_may_add(dentry);
21054 +       if (unlikely(err))
21055 +               goto out_unlock;
21056 +       if (!try_aopen)
21057 +               di_write_lock_parent(parent);
21058 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21059 +                                     &a->pin, &a->wr_dir_args);
21060 +       err = PTR_ERR(wh_dentry);
21061 +       if (IS_ERR(wh_dentry))
21062 +               goto out_parent;
21063 +
21064 +       btop = au_dbtop(dentry);
21065 +       sb = dentry->d_sb;
21066 +       br = au_sbr(sb, btop);
21067 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21068 +       a->h_path.mnt = au_br_mnt(br);
21069 +       h_dir = au_pinned_h_dir(&a->pin);
21070 +       switch (arg->type) {
21071 +       case Creat:
21072 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
21073 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21074 +                                          arg->u.c.want_excl);
21075 +                       created = !err;
21076 +                       if (!err && try_aopen)
21077 +                               aopen->file->f_mode |= FMODE_CREATED;
21078 +               } else {
21079 +                       aopen->br = br;
21080 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21081 +                       AuDbg("err %d\n", err);
21082 +                       AuDbgFile(aopen->file);
21083 +                       created = err >= 0
21084 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
21085 +               }
21086 +               break;
21087 +       case Symlink:
21088 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
21089 +               created = !err;
21090 +               break;
21091 +       case Mknod:
21092 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21093 +                                 arg->u.m.dev);
21094 +               created = !err;
21095 +               break;
21096 +       default:
21097 +               BUG();
21098 +       }
21099 +       if (unlikely(err < 0))
21100 +               goto out_unpin;
21101 +
21102 +       err = epilog(dir, btop, wh_dentry, dentry);
21103 +       if (!err)
21104 +               goto out_unpin; /* success */
21105 +
21106 +       /* revert */
21107 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
21108 +               /* no delegation since it is just created */
21109 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21110 +                                   /*force*/0);
21111 +               if (rerr) {
21112 +                       AuIOErr("%pd revert failure(%d, %d)\n",
21113 +                               dentry, err, rerr);
21114 +                       err = -EIO;
21115 +               }
21116 +               au_dtime_revert(&a->dt);
21117 +       }
21118 +       if (try_aopen && h_dir->i_op->atomic_open
21119 +           && (aopen->file->f_mode & FMODE_OPENED))
21120 +               /* aopen->file is still opened */
21121 +               au_lcnt_dec(&aopen->br->br_nfiles);
21122 +
21123 +out_unpin:
21124 +       au_unpin(&a->pin);
21125 +       dput(wh_dentry);
21126 +out_parent:
21127 +       if (!try_aopen)
21128 +               di_write_unlock(parent);
21129 +out_unlock:
21130 +       if (unlikely(err)) {
21131 +               au_update_dbtop(dentry);
21132 +               d_drop(dentry);
21133 +       }
21134 +       if (!try_aopen)
21135 +               aufs_read_unlock(dentry, AuLock_DW);
21136 +out_free:
21137 +       au_kfree_rcu(a);
21138 +out:
21139 +       return err;
21140 +}
21141 +
21142 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
21143 +              struct dentry *dentry, umode_t mode, dev_t dev)
21144 +{
21145 +       struct simple_arg arg = {
21146 +               .type = Mknod,
21147 +               .u.m = {
21148 +                       .mode   = mode,
21149 +                       .dev    = dev
21150 +               }
21151 +       };
21152 +       return add_simple(dir, dentry, &arg);
21153 +}
21154 +
21155 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
21156 +                struct dentry *dentry, const char *symname)
21157 +{
21158 +       struct simple_arg arg = {
21159 +               .type = Symlink,
21160 +               .u.s.symname = symname
21161 +       };
21162 +       return add_simple(dir, dentry, &arg);
21163 +}
21164 +
21165 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
21166 +               struct dentry *dentry, umode_t mode, bool want_excl)
21167 +{
21168 +       struct simple_arg arg = {
21169 +               .type = Creat,
21170 +               .u.c = {
21171 +                       .mode           = mode,
21172 +                       .want_excl      = want_excl
21173 +               }
21174 +       };
21175 +       return add_simple(dir, dentry, &arg);
21176 +}
21177 +
21178 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21179 +                      struct vfsub_aopen_args *aopen_args)
21180 +{
21181 +       struct simple_arg arg = {
21182 +               .type = Creat,
21183 +               .u.c = {
21184 +                       .mode           = aopen_args->create_mode,
21185 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
21186 +                       .try_aopen      = true,
21187 +                       .aopen          = aopen_args
21188 +               }
21189 +       };
21190 +       return add_simple(dir, dentry, &arg);
21191 +}
21192 +
21193 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
21194 +                struct file *file, umode_t mode)
21195 +{
21196 +       int err;
21197 +       aufs_bindex_t bindex;
21198 +       struct path h_ppath;
21199 +       struct super_block *sb;
21200 +       struct au_branch *br;
21201 +       struct dentry *dentry, *parent, *h_parent, *h_dentry;
21202 +       struct inode *h_dir, *inode;
21203 +       struct vfsmount *h_mnt;
21204 +       struct mnt_idmap *h_idmap;
21205 +       struct file *h_file;
21206 +       struct au_wr_dir_args wr_dir_args = {
21207 +               .force_btgt     = -1,
21208 +               .flags          = AuWrDir_TMPFILE
21209 +       };
21210 +
21211 +       /* copy-up may happen */
21212 +       inode_lock(dir);
21213 +
21214 +       h_file = NULL;
21215 +       sb = dir->i_sb;
21216 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21217 +       if (unlikely(err))
21218 +               goto out;
21219 +
21220 +       dentry = file->f_path.dentry;
21221 +       err = au_di_init(dentry);
21222 +       if (unlikely(err))
21223 +               goto out_si;
21224 +
21225 +       err = -EBUSY;
21226 +       parent = d_find_any_alias(dir);
21227 +       AuDebugOn(!parent);
21228 +       di_write_lock_parent(parent);
21229 +       if (unlikely(d_inode(parent) != dir))
21230 +               goto out_parent;
21231 +
21232 +       err = au_digen_test(parent, au_sigen(sb));
21233 +       if (unlikely(err))
21234 +               goto out_parent;
21235 +
21236 +       bindex = au_dbtop(parent);
21237 +       au_set_dbtop(dentry, bindex);
21238 +       au_set_dbbot(dentry, bindex);
21239 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21240 +       bindex = err;
21241 +       if (unlikely(err < 0))
21242 +               goto out_parent;
21243 +
21244 +       err = -EOPNOTSUPP;
21245 +       h_dir = au_h_iptr(dir, bindex);
21246 +       if (unlikely(!h_dir->i_op->tmpfile))
21247 +               goto out_parent;
21248 +
21249 +       br = au_sbr(sb, bindex);
21250 +       h_mnt = au_br_mnt(br);
21251 +       err = vfsub_mnt_want_write(h_mnt);
21252 +       if (unlikely(err))
21253 +               goto out_parent;
21254 +
21255 +       h_idmap = mnt_idmap(h_mnt);
21256 +       h_parent = au_h_dptr(parent, bindex);
21257 +       h_ppath.mnt = h_mnt;
21258 +       h_ppath.dentry = h_parent;
21259 +       h_file = kernel_tmpfile_open(h_idmap, &h_ppath, mode, /*open_flag*/0,
21260 +                                    current_cred());
21261 +       if (IS_ERR(h_file)) {
21262 +               err = PTR_ERR(h_file);
21263 +               h_file = NULL;
21264 +               goto out_mnt;
21265 +       }
21266 +
21267 +       h_dentry = h_file->f_path.dentry;
21268 +       au_set_dbtop(dentry, bindex);
21269 +       au_set_dbbot(dentry, bindex);
21270 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
21271 +       inode = au_new_inode(dentry, /*must_new*/1);
21272 +       if (IS_ERR(inode)) {
21273 +               err = PTR_ERR(inode);
21274 +               au_set_h_dptr(dentry, bindex, NULL);
21275 +               au_set_dbtop(dentry, -1);
21276 +               au_set_dbbot(dentry, -1);
21277 +               goto out_h_file;
21278 +       }
21279 +
21280 +       if (!inode->i_nlink)
21281 +               set_nlink(inode, 1);
21282 +       d_tmpfile(file, inode);
21283 +       au_di(dentry)->di_tmpfile = 1;
21284 +       get_file(h_file);
21285 +       au_di(dentry)->di_htmpfile = h_file;
21286 +
21287 +       /* update without i_mutex */
21288 +       if (au_ibtop(dir) == au_dbtop(dentry))
21289 +               au_cpup_attr_timesizes(dir);
21290 +
21291 +out_h_file:
21292 +       fput(h_file);
21293 +out_mnt:
21294 +       vfsub_mnt_drop_write(h_mnt);
21295 +out_parent:
21296 +       di_write_unlock(parent);
21297 +       dput(parent);
21298 +       di_write_unlock(dentry);
21299 +       if (!err)
21300 +               goto out_si;
21301 +       if (h_file)
21302 +               fput(h_file);
21303 +       au_di(dentry)->di_htmpfile = NULL;
21304 +       au_di_fin(dentry);
21305 +       dentry->d_fsdata = NULL;
21306 +out_si:
21307 +       si_read_unlock(sb);
21308 +       if (!err && h_file) {
21309 +               /* finally... */
21310 +               err = finish_open_simple(file, err);
21311 +               if (!err)
21312 +                       au_lcnt_inc(&br->br_nfiles);
21313 +               else {
21314 +                       fput(h_file);
21315 +                       au_di(dentry)->di_htmpfile = NULL;
21316 +                       au_di_fin(dentry);
21317 +                       dentry->d_fsdata = NULL;
21318 +               }
21319 +       }
21320 +out:
21321 +       inode_unlock(dir);
21322 +       AuTraceErr(err);
21323 +       return err;
21324 +}
21325 +
21326 +/* ---------------------------------------------------------------------- */
21327 +
21328 +struct au_link_args {
21329 +       aufs_bindex_t bdst, bsrc;
21330 +       struct au_pin pin;
21331 +       struct path h_path;
21332 +       struct dentry *src_parent, *parent;
21333 +};
21334 +
21335 +static int au_cpup_before_link(struct dentry *src_dentry,
21336 +                              struct au_link_args *a)
21337 +{
21338 +       int err;
21339 +       struct dentry *h_src_dentry;
21340 +       struct au_cp_generic cpg = {
21341 +               .dentry = src_dentry,
21342 +               .bdst   = a->bdst,
21343 +               .bsrc   = a->bsrc,
21344 +               .len    = -1,
21345 +               .pin    = &a->pin,
21346 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21347 +       };
21348 +
21349 +       di_read_lock_parent(a->src_parent, AuLock_IR);
21350 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21351 +       if (unlikely(err))
21352 +               goto out;
21353 +
21354 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
21355 +       err = au_pin(&a->pin, src_dentry, a->bdst,
21356 +                    au_opt_udba(src_dentry->d_sb),
21357 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21358 +       if (unlikely(err))
21359 +               goto out;
21360 +
21361 +       err = au_sio_cpup_simple(&cpg);
21362 +       au_unpin(&a->pin);
21363 +
21364 +out:
21365 +       di_read_unlock(a->src_parent, AuLock_IR);
21366 +       return err;
21367 +}
21368 +
21369 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21370 +                          struct au_link_args *a)
21371 +{
21372 +       int err;
21373 +       unsigned char plink;
21374 +       aufs_bindex_t bbot;
21375 +       struct dentry *h_src_dentry;
21376 +       struct inode *h_inode, *inode, *delegated;
21377 +       struct super_block *sb;
21378 +       struct file *h_file;
21379 +
21380 +       plink = 0;
21381 +       h_inode = NULL;
21382 +       sb = src_dentry->d_sb;
21383 +       inode = d_inode(src_dentry);
21384 +       if (au_ibtop(inode) <= a->bdst)
21385 +               h_inode = au_h_iptr(inode, a->bdst);
21386 +       if (!h_inode || !h_inode->i_nlink) {
21387 +               /* copyup src_dentry as the name of dentry. */
21388 +               bbot = au_dbbot(dentry);
21389 +               if (bbot < a->bsrc)
21390 +                       au_set_dbbot(dentry, a->bsrc);
21391 +               au_set_h_dptr(dentry, a->bsrc,
21392 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
21393 +               dget(a->h_path.dentry);
21394 +               au_set_h_dptr(dentry, a->bdst, NULL);
21395 +               AuDbg("temporary d_inode...\n");
21396 +               spin_lock(&dentry->d_lock);
21397 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
21398 +               spin_unlock(&dentry->d_lock);
21399 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
21400 +               if (IS_ERR(h_file))
21401 +                       err = PTR_ERR(h_file);
21402 +               else {
21403 +                       struct au_cp_generic cpg = {
21404 +                               .dentry = dentry,
21405 +                               .bdst   = a->bdst,
21406 +                               .bsrc   = -1,
21407 +                               .len    = -1,
21408 +                               .pin    = &a->pin,
21409 +                               .flags  = AuCpup_KEEPLINO
21410 +                       };
21411 +                       err = au_sio_cpup_simple(&cpg);
21412 +                       au_h_open_post(dentry, a->bsrc, h_file);
21413 +                       if (!err) {
21414 +                               dput(a->h_path.dentry);
21415 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21416 +                       } else
21417 +                               au_set_h_dptr(dentry, a->bdst,
21418 +                                             a->h_path.dentry);
21419 +               }
21420 +               spin_lock(&dentry->d_lock);
21421 +               dentry->d_inode = NULL; /* restore */
21422 +               spin_unlock(&dentry->d_lock);
21423 +               AuDbg("temporary d_inode...done\n");
21424 +               au_set_h_dptr(dentry, a->bsrc, NULL);
21425 +               au_set_dbbot(dentry, bbot);
21426 +       } else {
21427 +               /* the inode of src_dentry already exists on a.bdst branch */
21428 +               h_src_dentry = d_find_alias(h_inode);
21429 +               if (!h_src_dentry && au_plink_test(inode)) {
21430 +                       plink = 1;
21431 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
21432 +                       err = PTR_ERR(h_src_dentry);
21433 +                       if (IS_ERR(h_src_dentry))
21434 +                               goto out;
21435 +
21436 +                       if (unlikely(d_is_negative(h_src_dentry))) {
21437 +                               dput(h_src_dentry);
21438 +                               h_src_dentry = NULL;
21439 +                       }
21440 +
21441 +               }
21442 +               if (h_src_dentry) {
21443 +                       delegated = NULL;
21444 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21445 +                                        &a->h_path, &delegated);
21446 +                       if (unlikely(err == -EWOULDBLOCK)) {
21447 +                               pr_warn("cannot retry for NFSv4 delegation"
21448 +                                       " for an internal link\n");
21449 +                               iput(delegated);
21450 +                       }
21451 +                       dput(h_src_dentry);
21452 +               } else {
21453 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
21454 +                               h_inode->i_ino, a->bdst);
21455 +                       err = -EIO;
21456 +               }
21457 +       }
21458 +
21459 +       if (!err && !plink)
21460 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
21461 +
21462 +out:
21463 +       AuTraceErr(err);
21464 +       return err;
21465 +}
21466 +
21467 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
21468 +             struct dentry *dentry)
21469 +{
21470 +       int err, rerr;
21471 +       struct au_dtime dt;
21472 +       struct au_link_args *a;
21473 +       struct dentry *wh_dentry, *h_src_dentry;
21474 +       struct inode *inode, *delegated;
21475 +       struct super_block *sb;
21476 +       struct au_wr_dir_args wr_dir_args = {
21477 +               /* .force_btgt  = -1, */
21478 +               .flags          = AuWrDir_ADD_ENTRY
21479 +       };
21480 +
21481 +       IMustLock(dir);
21482 +       inode = d_inode(src_dentry);
21483 +       IMustLock(inode);
21484 +
21485 +       err = -ENOMEM;
21486 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21487 +       if (unlikely(!a))
21488 +               goto out;
21489 +
21490 +       a->parent = dentry->d_parent; /* dir inode is locked */
21491 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
21492 +                                       AuLock_NOPLM | AuLock_GEN);
21493 +       if (unlikely(err))
21494 +               goto out_kfree;
21495 +       err = au_d_linkable(src_dentry);
21496 +       if (unlikely(err))
21497 +               goto out_unlock;
21498 +       err = au_d_may_add(dentry);
21499 +       if (unlikely(err))
21500 +               goto out_unlock;
21501 +
21502 +       a->src_parent = dget_parent(src_dentry);
21503 +       wr_dir_args.force_btgt = au_ibtop(inode);
21504 +
21505 +       di_write_lock_parent(a->parent);
21506 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21507 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21508 +                                     &wr_dir_args);
21509 +       err = PTR_ERR(wh_dentry);
21510 +       if (IS_ERR(wh_dentry))
21511 +               goto out_parent;
21512 +
21513 +       err = 0;
21514 +       sb = dentry->d_sb;
21515 +       a->bdst = au_dbtop(dentry);
21516 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21517 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21518 +       a->bsrc = au_ibtop(inode);
21519 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21520 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21521 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21522 +       if (!h_src_dentry) {
21523 +               a->bsrc = au_dbtop(src_dentry);
21524 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21525 +               AuDebugOn(!h_src_dentry);
21526 +       } else if (IS_ERR(h_src_dentry)) {
21527 +               err = PTR_ERR(h_src_dentry);
21528 +               goto out_parent;
21529 +       }
21530 +
21531 +       /*
21532 +        * aufs doesn't touch the credential so
21533 +        * security_dentry_create_files_as() is unnecessary.
21534 +        */
21535 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21536 +               if (a->bdst < a->bsrc
21537 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21538 +                       err = au_cpup_or_link(src_dentry, dentry, a);
21539 +               else {
21540 +                       delegated = NULL;
21541 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21542 +                                        &a->h_path, &delegated);
21543 +                       if (unlikely(err == -EWOULDBLOCK)) {
21544 +                               pr_warn("cannot retry for NFSv4 delegation"
21545 +                                       " for an internal link\n");
21546 +                               iput(delegated);
21547 +                       }
21548 +               }
21549 +               dput(h_src_dentry);
21550 +       } else {
21551 +               /*
21552 +                * copyup src_dentry to the branch we process,
21553 +                * and then link(2) to it.
21554 +                */
21555 +               dput(h_src_dentry);
21556 +               if (a->bdst < a->bsrc
21557 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21558 +                       au_unpin(&a->pin);
21559 +                       di_write_unlock(a->parent);
21560 +                       err = au_cpup_before_link(src_dentry, a);
21561 +                       di_write_lock_parent(a->parent);
21562 +                       if (!err)
21563 +                               err = au_pin(&a->pin, dentry, a->bdst,
21564 +                                            au_opt_udba(sb),
21565 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21566 +                       if (unlikely(err))
21567 +                               goto out_wh;
21568 +               }
21569 +               if (!err) {
21570 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21571 +                       err = -ENOENT;
21572 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
21573 +                               delegated = NULL;
21574 +                               err = vfsub_link(h_src_dentry,
21575 +                                                au_pinned_h_dir(&a->pin),
21576 +                                                &a->h_path, &delegated);
21577 +                               if (unlikely(err == -EWOULDBLOCK)) {
21578 +                                       pr_warn("cannot retry"
21579 +                                               " for NFSv4 delegation"
21580 +                                               " for an internal link\n");
21581 +                                       iput(delegated);
21582 +                               }
21583 +                       }
21584 +               }
21585 +       }
21586 +       if (unlikely(err))
21587 +               goto out_unpin;
21588 +
21589 +       if (wh_dentry) {
21590 +               a->h_path.dentry = wh_dentry;
21591 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21592 +                                         dentry);
21593 +               if (unlikely(err))
21594 +                       goto out_revert;
21595 +       }
21596 +
21597 +       au_dir_ts(dir, a->bdst);
21598 +       inode_inc_iversion(dir);
21599 +       inc_nlink(inode);
21600 +       inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
21601 +       d_instantiate(dentry, au_igrab(inode));
21602 +       if (d_unhashed(a->h_path.dentry))
21603 +               /* some filesystem calls d_drop() */
21604 +               d_drop(dentry);
21605 +       /* some filesystems consume an inode even hardlink */
21606 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
21607 +       goto out_unpin; /* success */
21608 +
21609 +out_revert:
21610 +       /* no delegation since it is just created */
21611 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21612 +                           /*delegated*/NULL, /*force*/0);
21613 +       if (unlikely(rerr)) {
21614 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21615 +               err = -EIO;
21616 +       }
21617 +       au_dtime_revert(&dt);
21618 +out_unpin:
21619 +       au_unpin(&a->pin);
21620 +out_wh:
21621 +       dput(wh_dentry);
21622 +out_parent:
21623 +       di_write_unlock(a->parent);
21624 +       dput(a->src_parent);
21625 +out_unlock:
21626 +       if (unlikely(err)) {
21627 +               au_update_dbtop(dentry);
21628 +               d_drop(dentry);
21629 +       }
21630 +       aufs_read_and_write_unlock2(dentry, src_dentry);
21631 +out_kfree:
21632 +       au_kfree_rcu(a);
21633 +out:
21634 +       AuTraceErr(err);
21635 +       return err;
21636 +}
21637 +
21638 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
21639 +              struct dentry *dentry, umode_t mode)
21640 +{
21641 +       int err, rerr;
21642 +       aufs_bindex_t bindex;
21643 +       unsigned char diropq;
21644 +       struct path h_path;
21645 +       struct dentry *wh_dentry, *parent, *opq_dentry;
21646 +       struct inode *h_inode;
21647 +       struct super_block *sb;
21648 +       struct {
21649 +               struct au_pin pin;
21650 +               struct au_dtime dt;
21651 +       } *a; /* reduce the stack usage */
21652 +       struct au_wr_dir_args wr_dir_args = {
21653 +               .force_btgt     = -1,
21654 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21655 +       };
21656 +
21657 +       IMustLock(dir);
21658 +
21659 +       err = -ENOMEM;
21660 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21661 +       if (unlikely(!a))
21662 +               goto out;
21663 +
21664 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21665 +       if (unlikely(err))
21666 +               goto out_free;
21667 +       err = au_d_may_add(dentry);
21668 +       if (unlikely(err))
21669 +               goto out_unlock;
21670 +
21671 +       parent = dentry->d_parent; /* dir inode is locked */
21672 +       di_write_lock_parent(parent);
21673 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21674 +                                     &a->pin, &wr_dir_args);
21675 +       err = PTR_ERR(wh_dentry);
21676 +       if (IS_ERR(wh_dentry))
21677 +               goto out_parent;
21678 +
21679 +       sb = dentry->d_sb;
21680 +       bindex = au_dbtop(dentry);
21681 +       h_path.dentry = au_h_dptr(dentry, bindex);
21682 +       h_path.mnt = au_sbr_mnt(sb, bindex);
21683 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21684 +       if (unlikely(err))
21685 +               goto out_unpin;
21686 +
21687 +       /* make the dir opaque */
21688 +       diropq = 0;
21689 +       h_inode = d_inode(h_path.dentry);
21690 +       if (wh_dentry
21691 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21692 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21693 +               opq_dentry = au_diropq_create(dentry, bindex);
21694 +               inode_unlock(h_inode);
21695 +               err = PTR_ERR(opq_dentry);
21696 +               if (IS_ERR(opq_dentry))
21697 +                       goto out_dir;
21698 +               dput(opq_dentry);
21699 +               diropq = 1;
21700 +       }
21701 +
21702 +       err = epilog(dir, bindex, wh_dentry, dentry);
21703 +       if (!err) {
21704 +               inc_nlink(dir);
21705 +               goto out_unpin; /* success */
21706 +       }
21707 +
21708 +       /* revert */
21709 +       if (diropq) {
21710 +               AuLabel(revert opq);
21711 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21712 +               rerr = au_diropq_remove(dentry, bindex);
21713 +               inode_unlock(h_inode);
21714 +               if (rerr) {
21715 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21716 +                               dentry, err, rerr);
21717 +                       err = -EIO;
21718 +               }
21719 +       }
21720 +
21721 +out_dir:
21722 +       AuLabel(revert dir);
21723 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21724 +       if (rerr) {
21725 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
21726 +                       dentry, err, rerr);
21727 +               err = -EIO;
21728 +       }
21729 +       au_dtime_revert(&a->dt);
21730 +out_unpin:
21731 +       au_unpin(&a->pin);
21732 +       dput(wh_dentry);
21733 +out_parent:
21734 +       di_write_unlock(parent);
21735 +out_unlock:
21736 +       if (unlikely(err)) {
21737 +               au_update_dbtop(dentry);
21738 +               d_drop(dentry);
21739 +       }
21740 +       aufs_read_unlock(dentry, AuLock_DW);
21741 +out_free:
21742 +       au_kfree_rcu(a);
21743 +out:
21744 +       return err;
21745 +}
21746 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
21747 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
21748 +++ linux/fs/aufs/i_op.c        2024-05-13 17:13:00.618070924 +0200
21749 @@ -0,0 +1,1520 @@
21750 +// SPDX-License-Identifier: GPL-2.0
21751 +/*
21752 + * Copyright (C) 2005-2022 Junjiro R. Okajima
21753 + *
21754 + * This program is free software; you can redistribute it and/or modify
21755 + * it under the terms of the GNU General Public License as published by
21756 + * the Free Software Foundation; either version 2 of the License, or
21757 + * (at your option) any later version.
21758 + *
21759 + * This program is distributed in the hope that it will be useful,
21760 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21761 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21762 + * GNU General Public License for more details.
21763 + *
21764 + * You should have received a copy of the GNU General Public License
21765 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21766 + */
21767 +
21768 +/*
21769 + * inode operations (except add/del/rename)
21770 + */
21771 +
21772 +#include <linux/device_cgroup.h>
21773 +#include <linux/filelock.h>
21774 +#include <linux/fs_stack.h>
21775 +#include <linux/iversion.h>
21776 +#include <linux/security.h>
21777 +#include "aufs.h"
21778 +
21779 +static int h_permission(struct inode *h_inode, int mask,
21780 +                       struct path *h_path, int brperm)
21781 +{
21782 +       int err;
21783 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21784 +       struct mnt_idmap *h_idmap;
21785 +
21786 +       err = -EPERM;
21787 +       if (write_mask && IS_IMMUTABLE(h_inode))
21788 +               goto out;
21789 +
21790 +       err = -EACCES;
21791 +       if (((mask & MAY_EXEC)
21792 +            && S_ISREG(h_inode->i_mode)
21793 +            && (path_noexec(h_path)
21794 +                || !(h_inode->i_mode & 0111))))
21795 +               goto out;
21796 +
21797 +       /*
21798 +        * - skip the lower fs test in the case of write to ro branch.
21799 +        * - nfs dir permission write check is optimized, but a policy for
21800 +        *   link/rename requires a real check.
21801 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
21802 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
21803 +        */
21804 +       h_idmap = mnt_idmap(h_path->mnt);
21805 +       if ((write_mask && !au_br_writable(brperm))
21806 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21807 +               && write_mask && !(mask & MAY_READ))
21808 +           || !h_inode->i_op->permission) {
21809 +               /* AuLabel(generic_permission); */
21810 +               /* AuDbg("get_inode_acl %ps\n",
21811 +                  h_inode->i_op->get_inode_acl); */
21812 +               err = generic_permission(h_idmap, h_inode, mask);
21813 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
21814 +                       err = h_inode->i_op->permission(h_idmap, h_inode,
21815 +                                                       mask);
21816 +               AuTraceErr(err);
21817 +       } else {
21818 +               /* AuLabel(h_inode->permission); */
21819 +               err = h_inode->i_op->permission(h_idmap, h_inode, mask);
21820 +               AuTraceErr(err);
21821 +       }
21822 +
21823 +       if (!err)
21824 +               err = devcgroup_inode_permission(h_inode, mask);
21825 +       if (!err)
21826 +               err = security_inode_permission(h_inode, mask);
21827 +
21828 +out:
21829 +       return err;
21830 +}
21831 +
21832 +static int aufs_permission(struct mnt_idmap *idmap, struct inode *inode,
21833 +                          int mask)
21834 +{
21835 +       int err;
21836 +       aufs_bindex_t bindex, bbot;
21837 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21838 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21839 +       struct inode *h_inode;
21840 +       struct super_block *sb;
21841 +       struct au_branch *br;
21842 +
21843 +       /* todo: support rcu-walk? */
21844 +       if (mask & MAY_NOT_BLOCK)
21845 +               return -ECHILD;
21846 +
21847 +       sb = inode->i_sb;
21848 +       si_read_lock(sb, AuLock_FLUSH);
21849 +       ii_read_lock_child(inode);
21850 +#if 0 /* reserved for future use */
21851 +       /*
21852 +        * This test may be rather 'too much' since the test is essentially done
21853 +        * in the aufs_lookup().  Theoretically it is possible that the inode
21854 +        * generation doesn't match to the superblock's here.  But it isn't a
21855 +        * big deal I suppose.
21856 +        */
21857 +       err = au_iigen_test(inode, au_sigen(sb));
21858 +       if (unlikely(err))
21859 +               goto out;
21860 +#endif
21861 +
21862 +       if (!isdir
21863 +           || write_mask
21864 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
21865 +               err = au_busy_or_stale();
21866 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
21867 +               if (unlikely(!h_inode
21868 +                            || (h_inode->i_mode & S_IFMT)
21869 +                            != (inode->i_mode & S_IFMT)))
21870 +                       goto out;
21871 +
21872 +               err = 0;
21873 +               bindex = au_ibtop(inode);
21874 +               br = au_sbr(sb, bindex);
21875 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
21876 +               if (write_mask
21877 +                   && !err
21878 +                   && !special_file(h_inode->i_mode)) {
21879 +                       /* test whether the upper writable branch exists */
21880 +                       err = -EROFS;
21881 +                       for (; bindex >= 0; bindex--)
21882 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
21883 +                                       err = 0;
21884 +                                       break;
21885 +                               }
21886 +               }
21887 +               goto out;
21888 +       }
21889 +
21890 +       /* non-write to dir */
21891 +       err = 0;
21892 +       bbot = au_ibbot(inode);
21893 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
21894 +               h_inode = au_h_iptr(inode, bindex);
21895 +               if (h_inode) {
21896 +                       err = au_busy_or_stale();
21897 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
21898 +                               break;
21899 +
21900 +                       br = au_sbr(sb, bindex);
21901 +                       err = h_permission(h_inode, mask, &br->br_path,
21902 +                                          br->br_perm);
21903 +               }
21904 +       }
21905 +
21906 +out:
21907 +       ii_read_unlock(inode);
21908 +       si_read_unlock(sb);
21909 +       return err;
21910 +}
21911 +
21912 +/* ---------------------------------------------------------------------- */
21913 +
21914 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
21915 +                                 unsigned int flags)
21916 +{
21917 +       struct dentry *ret, *parent;
21918 +       struct inode *inode;
21919 +       struct super_block *sb;
21920 +       int err, npositive;
21921 +
21922 +       IMustLock(dir);
21923 +
21924 +       /* todo: support rcu-walk? */
21925 +       ret = ERR_PTR(-ECHILD);
21926 +       if (flags & LOOKUP_RCU)
21927 +               goto out;
21928 +
21929 +       ret = ERR_PTR(-ENAMETOOLONG);
21930 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21931 +               goto out;
21932 +
21933 +       sb = dir->i_sb;
21934 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21935 +       ret = ERR_PTR(err);
21936 +       if (unlikely(err))
21937 +               goto out;
21938 +
21939 +       err = au_di_init(dentry);
21940 +       ret = ERR_PTR(err);
21941 +       if (unlikely(err))
21942 +               goto out_si;
21943 +
21944 +       inode = NULL;
21945 +       npositive = 0; /* suppress a warning */
21946 +       parent = dentry->d_parent; /* dir inode is locked */
21947 +       di_read_lock_parent(parent, AuLock_IR);
21948 +       err = au_alive_dir(parent);
21949 +       if (!err)
21950 +               err = au_digen_test(parent, au_sigen(sb));
21951 +       if (!err) {
21952 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21953 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21954 +                                          AuLkup_ALLOW_NEG);
21955 +               err = npositive;
21956 +       }
21957 +       di_read_unlock(parent, AuLock_IR);
21958 +       ret = ERR_PTR(err);
21959 +       if (unlikely(err < 0))
21960 +               goto out_unlock;
21961 +
21962 +       if (npositive) {
21963 +               inode = au_new_inode(dentry, /*must_new*/0);
21964 +               if (IS_ERR(inode)) {
21965 +                       ret = (void *)inode;
21966 +                       inode = NULL;
21967 +                       goto out_unlock;
21968 +               }
21969 +       }
21970 +
21971 +       if (inode)
21972 +               atomic_inc(&inode->i_count);
21973 +       ret = d_splice_alias(inode, dentry);
21974 +#if 0 /* reserved for future use */
21975 +       if (unlikely(d_need_lookup(dentry))) {
21976 +               spin_lock(&dentry->d_lock);
21977 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21978 +               spin_unlock(&dentry->d_lock);
21979 +       } else
21980 +#endif
21981 +       if (inode) {
21982 +               if (!IS_ERR(ret)) {
21983 +                       iput(inode);
21984 +                       if (ret && ret != dentry)
21985 +                               ii_write_unlock(inode);
21986 +               } else {
21987 +                       ii_write_unlock(inode);
21988 +                       iput(inode);
21989 +                       inode = NULL;
21990 +               }
21991 +       }
21992 +
21993 +out_unlock:
21994 +       di_write_unlock(dentry);
21995 +out_si:
21996 +       si_read_unlock(sb);
21997 +out:
21998 +       return ret;
21999 +}
22000 +
22001 +/* ---------------------------------------------------------------------- */
22002 +
22003 +/*
22004 + * very dirty and complicated aufs ->atomic_open().
22005 + * aufs_atomic_open()
22006 + * + au_aopen_or_create()
22007 + *   + add_simple()
22008 + *     + vfsub_atomic_open()
22009 + *       + branch fs ->atomic_open()
22010 + *        may call the actual 'open' for h_file
22011 + *       + inc br_nfiles only if opened
22012 + * + au_aopen_no_open() or au_aopen_do_open()
22013 + *
22014 + * au_aopen_do_open()
22015 + * + finish_open()
22016 + *   + au_do_aopen()
22017 + *     + au_do_open() the body of all 'open'
22018 + *       + au_do_open_nondir()
22019 + *        set the passed h_file
22020 + *
22021 + * au_aopen_no_open()
22022 + * + finish_no_open()
22023 + */
22024 +
22025 +struct aopen_node {
22026 +       struct hlist_bl_node hblist;
22027 +       struct file *file, *h_file;
22028 +};
22029 +
22030 +static int au_do_aopen(struct inode *inode, struct file *file)
22031 +{
22032 +       struct hlist_bl_head *aopen;
22033 +       struct hlist_bl_node *pos;
22034 +       struct aopen_node *node;
22035 +       struct au_do_open_args args = {
22036 +               .aopen  = 1,
22037 +               .open   = au_do_open_nondir
22038 +       };
22039 +
22040 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
22041 +       hlist_bl_lock(aopen);
22042 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
22043 +               if (node->file == file) {
22044 +                       args.h_file = node->h_file;
22045 +                       break;
22046 +               }
22047 +       hlist_bl_unlock(aopen);
22048 +       /* AuDebugOn(!args.h_file); */
22049 +
22050 +       return au_do_open(file, &args);
22051 +}
22052 +
22053 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
22054 +                           struct aopen_node *aopen_node)
22055 +{
22056 +       int err;
22057 +       struct hlist_bl_head *aopen;
22058 +
22059 +       AuLabel(here);
22060 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
22061 +       au_hbl_add(&aopen_node->hblist, aopen);
22062 +       err = finish_open(file, dentry, au_do_aopen);
22063 +       au_hbl_del(&aopen_node->hblist, aopen);
22064 +       /* AuDbgFile(file); */
22065 +       AuDbg("%pd%s%s\n", dentry,
22066 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
22067 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
22068 +
22069 +       AuTraceErr(err);
22070 +       return err;
22071 +}
22072 +
22073 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
22074 +{
22075 +       int err;
22076 +
22077 +       AuLabel(here);
22078 +       dget(dentry);
22079 +       err = finish_no_open(file, dentry);
22080 +
22081 +       AuTraceErr(err);
22082 +       return err;
22083 +}
22084 +
22085 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22086 +                           struct file *file, unsigned int open_flag,
22087 +                           umode_t create_mode)
22088 +{
22089 +       int err, did_open;
22090 +       unsigned int lkup_flags;
22091 +       aufs_bindex_t bindex;
22092 +       struct super_block *sb;
22093 +       struct dentry *parent, *d;
22094 +       struct vfsub_aopen_args args = {
22095 +               .open_flag      = open_flag,
22096 +               .create_mode    = create_mode
22097 +       };
22098 +       struct aopen_node aopen_node = {
22099 +               .file   = file
22100 +       };
22101 +
22102 +       IMustLock(dir);
22103 +       AuDbg("open_flag 0%o\n", open_flag);
22104 +       AuDbgDentry(dentry);
22105 +
22106 +       err = 0;
22107 +       if (!au_di(dentry)) {
22108 +               lkup_flags = LOOKUP_OPEN;
22109 +               if (open_flag & O_CREAT)
22110 +                       lkup_flags |= LOOKUP_CREATE;
22111 +               d = aufs_lookup(dir, dentry, lkup_flags);
22112 +               if (IS_ERR(d)) {
22113 +                       err = PTR_ERR(d);
22114 +                       AuTraceErr(err);
22115 +                       goto out;
22116 +               } else if (d) {
22117 +                       /*
22118 +                        * obsoleted dentry found.
22119 +                        * another error will be returned later.
22120 +                        */
22121 +                       d_drop(d);
22122 +                       AuDbgDentry(d);
22123 +                       dput(d);
22124 +               }
22125 +               AuDbgDentry(dentry);
22126 +       }
22127 +
22128 +       if (d_is_positive(dentry)
22129 +           || d_unhashed(dentry)
22130 +           || d_unlinked(dentry)
22131 +           || !(open_flag & O_CREAT)) {
22132 +               err = au_aopen_no_open(file, dentry);
22133 +               goto out; /* success */
22134 +       }
22135 +
22136 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22137 +       if (unlikely(err))
22138 +               goto out;
22139 +
22140 +       sb = dentry->d_sb;
22141 +       parent = dentry->d_parent;      /* dir is locked */
22142 +       di_write_lock_parent(parent);
22143 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
22144 +       if (unlikely(err < 0))
22145 +               goto out_parent;
22146 +
22147 +       AuDbgDentry(dentry);
22148 +       if (d_is_positive(dentry)) {
22149 +               err = au_aopen_no_open(file, dentry);
22150 +               goto out_parent; /* success */
22151 +       }
22152 +
22153 +       args.file = alloc_empty_file(file->f_flags, current_cred());
22154 +       err = PTR_ERR(args.file);
22155 +       if (IS_ERR(args.file))
22156 +               goto out_parent;
22157 +
22158 +       bindex = au_dbtop(dentry);
22159 +       err = au_aopen_or_create(dir, dentry, &args);
22160 +       AuTraceErr(err);
22161 +       AuDbgFile(args.file);
22162 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22163 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
22164 +       if (!did_open) {
22165 +               fput(args.file);
22166 +               args.file = NULL;
22167 +       }
22168 +       di_write_unlock(parent);
22169 +       di_write_unlock(dentry);
22170 +       if (unlikely(err < 0)) {
22171 +               if (args.file)
22172 +                       fput(args.file);
22173 +               goto out_sb;
22174 +       }
22175 +
22176 +       if (!did_open)
22177 +               err = au_aopen_no_open(file, dentry);
22178 +       else {
22179 +               aopen_node.h_file = args.file;
22180 +               err = au_aopen_do_open(file, dentry, &aopen_node);
22181 +       }
22182 +       if (unlikely(err < 0)) {
22183 +               if (args.file)
22184 +                       fput(args.file);
22185 +               if (did_open)
22186 +                       au_lcnt_dec(&args.br->br_nfiles);
22187 +       }
22188 +       goto out_sb; /* success */
22189 +
22190 +out_parent:
22191 +       di_write_unlock(parent);
22192 +       di_write_unlock(dentry);
22193 +out_sb:
22194 +       si_read_unlock(sb);
22195 +out:
22196 +       AuTraceErr(err);
22197 +       AuDbgFile(file);
22198 +       return err;
22199 +}
22200 +
22201 +
22202 +/* ---------------------------------------------------------------------- */
22203 +
22204 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22205 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
22206 +                         aufs_bindex_t btop)
22207 +{
22208 +       int err;
22209 +       struct dentry *h_parent;
22210 +       struct inode *h_dir;
22211 +
22212 +       if (add_entry)
22213 +               IMustLock(d_inode(parent));
22214 +       else
22215 +               di_write_lock_parent(parent);
22216 +
22217 +       err = 0;
22218 +       if (!au_h_dptr(parent, bcpup)) {
22219 +               if (btop > bcpup)
22220 +                       err = au_cpup_dirs(dentry, bcpup);
22221 +               else if (btop < bcpup)
22222 +                       err = au_cpdown_dirs(dentry, bcpup);
22223 +               else
22224 +                       BUG();
22225 +       }
22226 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
22227 +               h_parent = au_h_dptr(parent, bcpup);
22228 +               h_dir = d_inode(h_parent);
22229 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
22230 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
22231 +               /* todo: no unlock here */
22232 +               inode_unlock_shared(h_dir);
22233 +
22234 +               AuDbg("bcpup %d\n", bcpup);
22235 +               if (!err) {
22236 +                       if (d_really_is_negative(dentry))
22237 +                               au_set_h_dptr(dentry, btop, NULL);
22238 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
22239 +               }
22240 +       }
22241 +
22242 +       if (!add_entry)
22243 +               di_write_unlock(parent);
22244 +       if (!err)
22245 +               err = bcpup; /* success */
22246 +
22247 +       AuTraceErr(err);
22248 +       return err;
22249 +}
22250 +
22251 +/*
22252 + * decide the branch and the parent dir where we will create a new entry.
22253 + * returns new bindex or an error.
22254 + * copyup the parent dir if needed.
22255 + */
22256 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22257 +             struct au_wr_dir_args *args)
22258 +{
22259 +       int err;
22260 +       unsigned int flags;
22261 +       aufs_bindex_t bcpup, btop, src_btop;
22262 +       const unsigned char add_entry
22263 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
22264 +               | au_ftest_wrdir(args->flags, TMPFILE);
22265 +       struct super_block *sb;
22266 +       struct dentry *parent;
22267 +       struct au_sbinfo *sbinfo;
22268 +
22269 +       sb = dentry->d_sb;
22270 +       sbinfo = au_sbi(sb);
22271 +       parent = dget_parent(dentry);
22272 +       btop = au_dbtop(dentry);
22273 +       bcpup = btop;
22274 +       if (args->force_btgt < 0) {
22275 +               if (src_dentry) {
22276 +                       src_btop = au_dbtop(src_dentry);
22277 +                       if (src_btop < btop)
22278 +                               bcpup = src_btop;
22279 +               } else if (add_entry) {
22280 +                       flags = 0;
22281 +                       if (au_ftest_wrdir(args->flags, ISDIR))
22282 +                               au_fset_wbr(flags, DIR);
22283 +                       err = AuWbrCreate(sbinfo, dentry, flags);
22284 +                       bcpup = err;
22285 +               }
22286 +
22287 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
22288 +                       if (add_entry)
22289 +                               err = AuWbrCopyup(sbinfo, dentry);
22290 +                       else {
22291 +                               if (!IS_ROOT(dentry)) {
22292 +                                       di_read_lock_parent(parent, !AuLock_IR);
22293 +                                       err = AuWbrCopyup(sbinfo, dentry);
22294 +                                       di_read_unlock(parent, !AuLock_IR);
22295 +                               } else
22296 +                                       err = AuWbrCopyup(sbinfo, dentry);
22297 +                       }
22298 +                       bcpup = err;
22299 +                       if (unlikely(err < 0))
22300 +                               goto out;
22301 +               }
22302 +       } else {
22303 +               bcpup = args->force_btgt;
22304 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
22305 +       }
22306 +
22307 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
22308 +       err = bcpup;
22309 +       if (bcpup == btop)
22310 +               goto out; /* success */
22311 +
22312 +       /* copyup the new parent into the branch we process */
22313 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
22314 +       if (err >= 0) {
22315 +               if (d_really_is_negative(dentry)) {
22316 +                       au_set_h_dptr(dentry, btop, NULL);
22317 +                       au_set_dbtop(dentry, bcpup);
22318 +                       au_set_dbbot(dentry, bcpup);
22319 +               }
22320 +               AuDebugOn(add_entry
22321 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
22322 +                         && !au_h_dptr(dentry, bcpup));
22323 +       }
22324 +
22325 +out:
22326 +       dput(parent);
22327 +       return err;
22328 +}
22329 +
22330 +/* ---------------------------------------------------------------------- */
22331 +
22332 +void au_pin_hdir_unlock(struct au_pin *p)
22333 +{
22334 +       if (p->hdir)
22335 +               au_hn_inode_unlock(p->hdir);
22336 +}
22337 +
22338 +int au_pin_hdir_lock(struct au_pin *p)
22339 +{
22340 +       int err;
22341 +
22342 +       err = 0;
22343 +       if (!p->hdir)
22344 +               goto out;
22345 +
22346 +       /* even if an error happens later, keep this lock */
22347 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
22348 +
22349 +       err = -EBUSY;
22350 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
22351 +               goto out;
22352 +
22353 +       err = 0;
22354 +       if (p->h_dentry)
22355 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22356 +                                 p->h_parent, p->br);
22357 +
22358 +out:
22359 +       return err;
22360 +}
22361 +
22362 +int au_pin_hdir_relock(struct au_pin *p)
22363 +{
22364 +       int err, i;
22365 +       struct inode *h_i;
22366 +       struct dentry *h_d[] = {
22367 +               p->h_dentry,
22368 +               p->h_parent
22369 +       };
22370 +
22371 +       err = au_pin_hdir_lock(p);
22372 +       if (unlikely(err))
22373 +               goto out;
22374 +
22375 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22376 +               if (!h_d[i])
22377 +                       continue;
22378 +               if (d_is_positive(h_d[i])) {
22379 +                       h_i = d_inode(h_d[i]);
22380 +                       err = !h_i->i_nlink;
22381 +               }
22382 +       }
22383 +
22384 +out:
22385 +       return err;
22386 +}
22387 +
22388 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
22389 +{
22390 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
22391 +}
22392 +
22393 +void au_pin_hdir_acquire_nest(struct au_pin *p)
22394 +{
22395 +       if (p->hdir) {
22396 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
22397 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
22398 +               au_pin_hdir_set_owner(p, current);
22399 +       }
22400 +}
22401 +
22402 +void au_pin_hdir_release(struct au_pin *p)
22403 +{
22404 +       if (p->hdir) {
22405 +               au_pin_hdir_set_owner(p, p->task);
22406 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
22407 +       }
22408 +}
22409 +
22410 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
22411 +{
22412 +       if (pin && pin->parent)
22413 +               return au_h_dptr(pin->parent, pin->bindex);
22414 +       return NULL;
22415 +}
22416 +
22417 +void au_unpin(struct au_pin *p)
22418 +{
22419 +       if (p->hdir)
22420 +               au_pin_hdir_unlock(p);
22421 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
22422 +               vfsub_mnt_drop_write(p->h_mnt);
22423 +       if (!p->hdir)
22424 +               return;
22425 +
22426 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22427 +               di_read_unlock(p->parent, AuLock_IR);
22428 +       iput(p->hdir->hi_inode);
22429 +       dput(p->parent);
22430 +       p->parent = NULL;
22431 +       p->hdir = NULL;
22432 +       p->h_mnt = NULL;
22433 +       /* do not clear p->task */
22434 +}
22435 +
22436 +int au_do_pin(struct au_pin *p)
22437 +{
22438 +       int err;
22439 +       struct super_block *sb;
22440 +       struct inode *h_dir;
22441 +
22442 +       err = 0;
22443 +       sb = p->dentry->d_sb;
22444 +       p->br = au_sbr(sb, p->bindex);
22445 +       if (IS_ROOT(p->dentry)) {
22446 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
22447 +                       p->h_mnt = au_br_mnt(p->br);
22448 +                       err = vfsub_mnt_want_write(p->h_mnt);
22449 +                       if (unlikely(err)) {
22450 +                               au_fclr_pin(p->flags, MNT_WRITE);
22451 +                               goto out_err;
22452 +                       }
22453 +               }
22454 +               goto out;
22455 +       }
22456 +
22457 +       p->h_dentry = NULL;
22458 +       if (p->bindex <= au_dbbot(p->dentry))
22459 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
22460 +
22461 +       p->parent = dget_parent(p->dentry);
22462 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22463 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
22464 +
22465 +       h_dir = NULL;
22466 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
22467 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
22468 +       if (p->hdir)
22469 +               h_dir = p->hdir->hi_inode;
22470 +
22471 +       /*
22472 +        * udba case, or
22473 +        * if DI_LOCKED is not set, then p->parent may be different
22474 +        * and h_parent can be NULL.
22475 +        */
22476 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
22477 +               err = -EBUSY;
22478 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
22479 +                       di_read_unlock(p->parent, AuLock_IR);
22480 +               dput(p->parent);
22481 +               p->parent = NULL;
22482 +               goto out_err;
22483 +       }
22484 +
22485 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
22486 +               p->h_mnt = au_br_mnt(p->br);
22487 +               err = vfsub_mnt_want_write(p->h_mnt);
22488 +               if (unlikely(err)) {
22489 +                       au_fclr_pin(p->flags, MNT_WRITE);
22490 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
22491 +                               di_read_unlock(p->parent, AuLock_IR);
22492 +                       dput(p->parent);
22493 +                       p->parent = NULL;
22494 +                       goto out_err;
22495 +               }
22496 +       }
22497 +
22498 +       au_igrab(h_dir);
22499 +       err = au_pin_hdir_lock(p);
22500 +       if (!err)
22501 +               goto out; /* success */
22502 +
22503 +       au_unpin(p);
22504 +
22505 +out_err:
22506 +       pr_err("err %d\n", err);
22507 +       err = au_busy_or_stale();
22508 +out:
22509 +       return err;
22510 +}
22511 +
22512 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
22513 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22514 +                unsigned int udba, unsigned char flags)
22515 +{
22516 +       p->dentry = dentry;
22517 +       p->udba = udba;
22518 +       p->lsc_di = lsc_di;
22519 +       p->lsc_hi = lsc_hi;
22520 +       p->flags = flags;
22521 +       p->bindex = bindex;
22522 +
22523 +       p->parent = NULL;
22524 +       p->hdir = NULL;
22525 +       p->h_mnt = NULL;
22526 +
22527 +       p->h_dentry = NULL;
22528 +       p->h_parent = NULL;
22529 +       p->br = NULL;
22530 +       p->task = current;
22531 +}
22532 +
22533 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22534 +          unsigned int udba, unsigned char flags)
22535 +{
22536 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22537 +                   udba, flags);
22538 +       return au_do_pin(pin);
22539 +}
22540 +
22541 +/* ---------------------------------------------------------------------- */
22542 +
22543 +/*
22544 + * ->setattr() and ->getattr() are called in various cases.
22545 + * chmod, stat: dentry is revalidated.
22546 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22547 + *               unhashed.
22548 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
22549 + */
22550 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
22551 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
22552 +{
22553 +       int err;
22554 +       struct dentry *parent;
22555 +
22556 +       err = 0;
22557 +       if (au_digen_test(dentry, sigen)) {
22558 +               parent = dget_parent(dentry);
22559 +               di_read_lock_parent(parent, AuLock_IR);
22560 +               err = au_refresh_dentry(dentry, parent);
22561 +               di_read_unlock(parent, AuLock_IR);
22562 +               dput(parent);
22563 +       }
22564 +
22565 +       AuTraceErr(err);
22566 +       return err;
22567 +}
22568 +
22569 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22570 +                    struct au_icpup_args *a)
22571 +{
22572 +       int err;
22573 +       loff_t sz;
22574 +       aufs_bindex_t btop, ibtop;
22575 +       struct dentry *hi_wh, *parent;
22576 +       struct inode *inode;
22577 +       struct au_wr_dir_args wr_dir_args = {
22578 +               .force_btgt     = -1,
22579 +               .flags          = 0
22580 +       };
22581 +
22582 +       if (d_is_dir(dentry))
22583 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
22584 +       /* plink or hi_wh() case */
22585 +       btop = au_dbtop(dentry);
22586 +       inode = d_inode(dentry);
22587 +       ibtop = au_ibtop(inode);
22588 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22589 +               wr_dir_args.force_btgt = ibtop;
22590 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22591 +       if (unlikely(err < 0))
22592 +               goto out;
22593 +       a->btgt = err;
22594 +       if (err != btop)
22595 +               au_fset_icpup(a->flags, DID_CPUP);
22596 +
22597 +       err = 0;
22598 +       a->pin_flags = AuPin_MNT_WRITE;
22599 +       parent = NULL;
22600 +       if (!IS_ROOT(dentry)) {
22601 +               au_fset_pin(a->pin_flags, DI_LOCKED);
22602 +               parent = dget_parent(dentry);
22603 +               di_write_lock_parent(parent);
22604 +       }
22605 +
22606 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22607 +       if (unlikely(err))
22608 +               goto out_parent;
22609 +
22610 +       sz = -1;
22611 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22612 +       a->h_inode = d_inode(a->h_path.dentry);
22613 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
22614 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
22615 +               if (ia->ia_size < i_size_read(a->h_inode))
22616 +                       sz = ia->ia_size;
22617 +               inode_unlock_shared(a->h_inode);
22618 +       }
22619 +
22620 +       hi_wh = NULL;
22621 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
22622 +               hi_wh = au_hi_wh(inode, a->btgt);
22623 +               if (!hi_wh) {
22624 +                       struct au_cp_generic cpg = {
22625 +                               .dentry = dentry,
22626 +                               .bdst   = a->btgt,
22627 +                               .bsrc   = -1,
22628 +                               .len    = sz,
22629 +                               .pin    = &a->pin
22630 +                       };
22631 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
22632 +                       if (unlikely(err))
22633 +                               goto out_unlock;
22634 +                       hi_wh = au_hi_wh(inode, a->btgt);
22635 +                       /* todo: revalidate hi_wh? */
22636 +               }
22637 +       }
22638 +
22639 +       if (parent) {
22640 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22641 +               di_downgrade_lock(parent, AuLock_IR);
22642 +               dput(parent);
22643 +               parent = NULL;
22644 +       }
22645 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
22646 +               goto out; /* success */
22647 +
22648 +       if (!d_unhashed(dentry)) {
22649 +               struct au_cp_generic cpg = {
22650 +                       .dentry = dentry,
22651 +                       .bdst   = a->btgt,
22652 +                       .bsrc   = btop,
22653 +                       .len    = sz,
22654 +                       .pin    = &a->pin,
22655 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
22656 +               };
22657 +               err = au_sio_cpup_simple(&cpg);
22658 +               if (!err)
22659 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22660 +       } else if (!hi_wh)
22661 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22662 +       else
22663 +               a->h_path.dentry = hi_wh; /* do not dget here */
22664 +
22665 +out_unlock:
22666 +       a->h_inode = d_inode(a->h_path.dentry);
22667 +       if (!err)
22668 +               goto out; /* success */
22669 +       au_unpin(&a->pin);
22670 +out_parent:
22671 +       if (parent) {
22672 +               di_write_unlock(parent);
22673 +               dput(parent);
22674 +       }
22675 +out:
22676 +       if (!err)
22677 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22678 +       return err;
22679 +}
22680 +
22681 +static int aufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
22682 +                       struct iattr *ia)
22683 +{
22684 +       int err;
22685 +       struct inode *inode, *delegated;
22686 +       struct super_block *sb;
22687 +       struct file *file;
22688 +       struct au_icpup_args *a;
22689 +       struct mnt_idmap *h_idmap;
22690 +
22691 +       inode = d_inode(dentry);
22692 +       IMustLock(inode);
22693 +
22694 +       err = setattr_prepare(idmap, dentry, ia);
22695 +       if (unlikely(err))
22696 +               goto out;
22697 +
22698 +       err = -ENOMEM;
22699 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22700 +       if (unlikely(!a))
22701 +               goto out;
22702 +
22703 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22704 +               ia->ia_valid &= ~ATTR_MODE;
22705 +
22706 +       file = NULL;
22707 +       sb = dentry->d_sb;
22708 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22709 +       if (unlikely(err))
22710 +               goto out_kfree;
22711 +
22712 +       if (ia->ia_valid & ATTR_FILE) {
22713 +               /* currently ftruncate(2) only */
22714 +               AuDebugOn(!d_is_reg(dentry));
22715 +               file = ia->ia_file;
22716 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22717 +                                           /*fi_lsc*/0);
22718 +               if (unlikely(err))
22719 +                       goto out_si;
22720 +               ia->ia_file = au_hf_top(file);
22721 +               a->udba = AuOpt_UDBA_NONE;
22722 +       } else {
22723 +               /* fchmod() doesn't pass ia_file */
22724 +               a->udba = au_opt_udba(sb);
22725 +               di_write_lock_child(dentry);
22726 +               /* no d_unlinked(), to set UDBA_NONE for root */
22727 +               if (d_unhashed(dentry))
22728 +                       a->udba = AuOpt_UDBA_NONE;
22729 +               if (a->udba != AuOpt_UDBA_NONE) {
22730 +                       AuDebugOn(IS_ROOT(dentry));
22731 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
22732 +                       if (unlikely(err))
22733 +                               goto out_dentry;
22734 +               }
22735 +       }
22736 +
22737 +       err = au_pin_and_icpup(dentry, ia, a);
22738 +       if (unlikely(err < 0))
22739 +               goto out_dentry;
22740 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
22741 +               ia->ia_file = NULL;
22742 +               ia->ia_valid &= ~ATTR_FILE;
22743 +       }
22744 +
22745 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22746 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22747 +           == (ATTR_MODE | ATTR_CTIME)) {
22748 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
22749 +               if (unlikely(err))
22750 +                       goto out_unlock;
22751 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22752 +                  && (ia->ia_valid & ATTR_CTIME)) {
22753 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
22754 +               if (unlikely(err))
22755 +                       goto out_unlock;
22756 +       }
22757 +
22758 +       if (ia->ia_valid & ATTR_SIZE) {
22759 +               struct file *f;
22760 +
22761 +               if (ia->ia_size < i_size_read(inode))
22762 +                       /* unmap only */
22763 +                       truncate_setsize(inode, ia->ia_size);
22764 +
22765 +               f = NULL;
22766 +               if (ia->ia_valid & ATTR_FILE)
22767 +                       f = ia->ia_file;
22768 +               inode_unlock(a->h_inode);
22769 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
22770 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22771 +       } else {
22772 +               delegated = NULL;
22773 +               while (1) {
22774 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
22775 +                       if (delegated) {
22776 +                               err = break_deleg_wait(&delegated);
22777 +                               if (!err)
22778 +                                       continue;
22779 +                       }
22780 +                       break;
22781 +               }
22782 +       }
22783 +       /*
22784 +        * regardless aufs 'acl' option setting.
22785 +        * why don't all acl-aware fs call this func from their ->setattr()?
22786 +        */
22787 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
22788 +               h_idmap = mnt_idmap(a->h_path.mnt);
22789 +               err = vfsub_acl_chmod(h_idmap, a->h_path.dentry, ia->ia_mode);
22790 +       }
22791 +       if (!err)
22792 +               au_cpup_attr_changeable(inode);
22793 +
22794 +out_unlock:
22795 +       inode_unlock(a->h_inode);
22796 +       au_unpin(&a->pin);
22797 +       if (unlikely(err))
22798 +               au_update_dbtop(dentry);
22799 +out_dentry:
22800 +       di_write_unlock(dentry);
22801 +       if (file) {
22802 +               fi_write_unlock(file);
22803 +               ia->ia_file = file;
22804 +               ia->ia_valid |= ATTR_FILE;
22805 +       }
22806 +out_si:
22807 +       si_read_unlock(sb);
22808 +out_kfree:
22809 +       au_kfree_rcu(a);
22810 +out:
22811 +       AuTraceErr(err);
22812 +       return err;
22813 +}
22814 +
22815 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22816 +static int au_h_path_to_set_attr(struct dentry *dentry,
22817 +                                struct au_icpup_args *a, struct path *h_path)
22818 +{
22819 +       int err;
22820 +       struct super_block *sb;
22821 +
22822 +       sb = dentry->d_sb;
22823 +       a->udba = au_opt_udba(sb);
22824 +       /* no d_unlinked(), to set UDBA_NONE for root */
22825 +       if (d_unhashed(dentry))
22826 +               a->udba = AuOpt_UDBA_NONE;
22827 +       if (a->udba != AuOpt_UDBA_NONE) {
22828 +               AuDebugOn(IS_ROOT(dentry));
22829 +               err = au_reval_for_attr(dentry, au_sigen(sb));
22830 +               if (unlikely(err))
22831 +                       goto out;
22832 +       }
22833 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22834 +       if (unlikely(err < 0))
22835 +               goto out;
22836 +
22837 +       h_path->dentry = a->h_path.dentry;
22838 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
22839 +
22840 +out:
22841 +       return err;
22842 +}
22843 +
22844 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22845 +                 struct au_sxattr *arg)
22846 +{
22847 +       int err;
22848 +       struct path h_path;
22849 +       struct super_block *sb;
22850 +       struct au_icpup_args *a;
22851 +       struct inode *h_inode;
22852 +       struct mnt_idmap *h_idmap;
22853 +
22854 +       IMustLock(inode);
22855 +
22856 +       err = -ENOMEM;
22857 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22858 +       if (unlikely(!a))
22859 +               goto out;
22860 +
22861 +       sb = dentry->d_sb;
22862 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22863 +       if (unlikely(err))
22864 +               goto out_kfree;
22865 +
22866 +       h_path.dentry = NULL;   /* silence gcc */
22867 +       di_write_lock_child(dentry);
22868 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
22869 +       if (unlikely(err))
22870 +               goto out_di;
22871 +       h_idmap = mnt_idmap(h_path.mnt);
22872 +
22873 +       inode_unlock(a->h_inode);
22874 +       switch (arg->type) {
22875 +       case AU_XATTR_SET:
22876 +               AuDebugOn(d_is_negative(h_path.dentry));
22877 +               err = vfsub_setxattr(h_idmap, h_path.dentry,
22878 +                                    arg->u.set.name, arg->u.set.value,
22879 +                                    arg->u.set.size, arg->u.set.flags);
22880 +               break;
22881 +       case AU_ACL_SET:
22882 +               err = -EOPNOTSUPP;
22883 +               h_inode = d_inode(h_path.dentry);
22884 +               if (h_inode->i_op->set_acl) {
22885 +                       /* this will call posix_acl_update_mode */
22886 +                       err = h_inode->i_op->set_acl(h_idmap, h_path.dentry,
22887 +                                                    arg->u.acl_set.acl,
22888 +                                                    arg->u.acl_set.type);
22889 +               }
22890 +               break;
22891 +       }
22892 +       if (!err)
22893 +               au_cpup_attr_timesizes(inode);
22894 +
22895 +       au_unpin(&a->pin);
22896 +       if (unlikely(err))
22897 +               au_update_dbtop(dentry);
22898 +
22899 +out_di:
22900 +       di_write_unlock(dentry);
22901 +       si_read_unlock(sb);
22902 +out_kfree:
22903 +       au_kfree_rcu(a);
22904 +out:
22905 +       AuTraceErr(err);
22906 +       return err;
22907 +}
22908 +#endif
22909 +
22910 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22911 +                            unsigned int nlink)
22912 +{
22913 +       unsigned int n;
22914 +
22915 +       inode->i_mode = st->mode;
22916 +       /* don't i_[ug]id_write() here */
22917 +       inode->i_uid = st->uid;
22918 +       inode->i_gid = st->gid;
22919 +       inode_set_atime_to_ts(inode, st->atime);
22920 +       inode_set_mtime_to_ts(inode, st->mtime);
22921 +       inode_set_ctime_to_ts(inode, st->ctime);
22922 +
22923 +       au_cpup_attr_nlink(inode, /*force*/0);
22924 +       if (S_ISDIR(inode->i_mode)) {
22925 +               n = inode->i_nlink;
22926 +               n -= nlink;
22927 +               n += st->nlink;
22928 +               smp_mb(); /* for i_nlink */
22929 +               /* 0 can happen */
22930 +               set_nlink(inode, n);
22931 +       }
22932 +
22933 +       spin_lock(&inode->i_lock);
22934 +       inode->i_blocks = st->blocks;
22935 +       i_size_write(inode, st->size);
22936 +       spin_unlock(&inode->i_lock);
22937 +}
22938 +
22939 +/*
22940 + * common routine for aufs_getattr() and au_getxattr().
22941 + * returns zero or negative (an error).
22942 + * @dentry will be read-locked in success.
22943 + */
22944 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22945 +                     struct path *h_path, int locked)
22946 +{
22947 +       int err;
22948 +       unsigned int mnt_flags, sigen;
22949 +       unsigned char udba_none;
22950 +       aufs_bindex_t bindex;
22951 +       struct super_block *sb, *h_sb;
22952 +
22953 +       h_path->mnt = NULL;
22954 +       h_path->dentry = NULL;
22955 +
22956 +       err = 0;
22957 +       sb = dentry->d_sb;
22958 +       mnt_flags = au_mntflags(sb);
22959 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
22960 +
22961 +       if (unlikely(locked))
22962 +               goto body; /* skip locking dinfo */
22963 +
22964 +       /* support fstat(2) */
22965 +       if (!d_unlinked(dentry) && !udba_none) {
22966 +               sigen = au_sigen(sb);
22967 +               err = au_digen_test(dentry, sigen);
22968 +               if (!err) {
22969 +                       di_read_lock_child(dentry, AuLock_IR);
22970 +                       err = au_dbrange_test(dentry);
22971 +                       if (unlikely(err)) {
22972 +                               di_read_unlock(dentry, AuLock_IR);
22973 +                               goto out;
22974 +                       }
22975 +               } else {
22976 +                       AuDebugOn(IS_ROOT(dentry));
22977 +                       di_write_lock_child(dentry);
22978 +                       err = au_dbrange_test(dentry);
22979 +                       if (!err)
22980 +                               err = au_reval_for_attr(dentry, sigen);
22981 +                       if (!err)
22982 +                               di_downgrade_lock(dentry, AuLock_IR);
22983 +                       else {
22984 +                               di_write_unlock(dentry);
22985 +                               goto out;
22986 +                       }
22987 +               }
22988 +       } else
22989 +               di_read_lock_child(dentry, AuLock_IR);
22990 +
22991 +body:
22992 +       if (!inode) {
22993 +               inode = d_inode(dentry);
22994 +               if (unlikely(!inode))
22995 +                       goto out;
22996 +       }
22997 +       bindex = au_ibtop(inode);
22998 +       h_path->mnt = au_sbr_mnt(sb, bindex);
22999 +       h_sb = h_path->mnt->mnt_sb;
23000 +       if (!force
23001 +           && !au_test_fs_bad_iattr(h_sb)
23002 +           && udba_none)
23003 +               goto out; /* success */
23004 +
23005 +       if (au_dbtop(dentry) == bindex)
23006 +               h_path->dentry = au_h_dptr(dentry, bindex);
23007 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
23008 +               h_path->dentry = au_plink_lkup(inode, bindex);
23009 +               if (IS_ERR(h_path->dentry))
23010 +                       /* pretending success */
23011 +                       h_path->dentry = NULL;
23012 +               else
23013 +                       dput(h_path->dentry);
23014 +       }
23015 +
23016 +out:
23017 +       return err;
23018 +}
23019 +
23020 +static int aufs_getattr(struct mnt_idmap *idmap, const struct path *path,
23021 +                       struct kstat *st, u32 request, unsigned int query)
23022 +{
23023 +       int err;
23024 +       unsigned char positive;
23025 +       struct path h_path;
23026 +       struct dentry *dentry;
23027 +       struct inode *inode;
23028 +       struct super_block *sb;
23029 +
23030 +       dentry = path->dentry;
23031 +       inode = d_inode(dentry);
23032 +       sb = dentry->d_sb;
23033 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23034 +       if (unlikely(err))
23035 +               goto out;
23036 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
23037 +                               /*locked*/0);
23038 +       if (unlikely(err))
23039 +               goto out_si;
23040 +       if (unlikely(!h_path.dentry))
23041 +               /* illegally overlapped or something */
23042 +               goto out_fill; /* pretending success */
23043 +
23044 +       positive = d_is_positive(h_path.dentry);
23045 +       if (positive) {
23046 +               /* no vfsub version */
23047 +               if (query & AT_GETATTR_NOSEC)
23048 +                       err = vfs_getattr_nosec(&h_path, st, request, query);
23049 +               else
23050 +                       err = vfs_getattr(&h_path, st, request, query);
23051 +       }
23052 +       if (!err) {
23053 +               if (positive)
23054 +                       au_refresh_iattr(inode, st,
23055 +                                        d_inode(h_path.dentry)->i_nlink);
23056 +               goto out_fill; /* success */
23057 +       }
23058 +       AuTraceErr(err);
23059 +       goto out_di;
23060 +
23061 +out_fill:
23062 +       generic_fillattr(idmap, request, inode, st);
23063 +out_di:
23064 +       di_read_unlock(dentry, AuLock_IR);
23065 +out_si:
23066 +       si_read_unlock(sb);
23067 +out:
23068 +       AuTraceErr(err);
23069 +       return err;
23070 +}
23071 +
23072 +/* ---------------------------------------------------------------------- */
23073 +
23074 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
23075 +                                struct delayed_call *done)
23076 +{
23077 +       const char *ret;
23078 +       struct dentry *h_dentry;
23079 +       struct inode *h_inode;
23080 +       int err;
23081 +       aufs_bindex_t bindex;
23082 +
23083 +       ret = NULL; /* suppress a warning */
23084 +       err = -ECHILD;
23085 +       if (!dentry)
23086 +               goto out;
23087 +
23088 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23089 +       if (unlikely(err))
23090 +               goto out;
23091 +
23092 +       err = au_d_hashed_positive(dentry);
23093 +       if (unlikely(err))
23094 +               goto out_unlock;
23095 +
23096 +       err = -EINVAL;
23097 +       inode = d_inode(dentry);
23098 +       bindex = au_ibtop(inode);
23099 +       h_inode = au_h_iptr(inode, bindex);
23100 +       if (unlikely(!h_inode->i_op->get_link))
23101 +               goto out_unlock;
23102 +
23103 +       err = -EBUSY;
23104 +       h_dentry = NULL;
23105 +       if (au_dbtop(dentry) <= bindex) {
23106 +               h_dentry = au_h_dptr(dentry, bindex);
23107 +               if (h_dentry)
23108 +                       dget(h_dentry);
23109 +       }
23110 +       if (!h_dentry) {
23111 +               h_dentry = d_find_any_alias(h_inode);
23112 +               if (IS_ERR(h_dentry)) {
23113 +                       err = PTR_ERR(h_dentry);
23114 +                       goto out_unlock;
23115 +               }
23116 +       }
23117 +       if (unlikely(!h_dentry))
23118 +               goto out_unlock;
23119 +
23120 +       err = 0;
23121 +       AuDbg("%ps\n", h_inode->i_op->get_link);
23122 +       AuDbgDentry(h_dentry);
23123 +       ret = vfs_get_link(h_dentry, done);
23124 +       dput(h_dentry);
23125 +       if (IS_ERR(ret))
23126 +               err = PTR_ERR(ret);
23127 +
23128 +out_unlock:
23129 +       aufs_read_unlock(dentry, AuLock_IR);
23130 +out:
23131 +       if (unlikely(err))
23132 +               ret = ERR_PTR(err);
23133 +       AuTraceErrPtr(ret);
23134 +       return ret;
23135 +}
23136 +
23137 +/* ---------------------------------------------------------------------- */
23138 +
23139 +static int au_is_special(struct inode *inode)
23140 +{
23141 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23142 +}
23143 +
23144 +static int aufs_update_time(struct inode *inode, int flags)
23145 +{
23146 +       int err;
23147 +       aufs_bindex_t bindex;
23148 +       struct super_block *sb;
23149 +       struct inode *h_inode;
23150 +       struct vfsmount *h_mnt;
23151 +
23152 +       sb = inode->i_sb;
23153 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23154 +                 "unexpected s_flags 0x%lx", sb->s_flags);
23155 +
23156 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
23157 +       lockdep_off();
23158 +       si_read_lock(sb, AuLock_FLUSH);
23159 +       ii_write_lock_child(inode);
23160 +
23161 +       err = 0;
23162 +       bindex = au_ibtop(inode);
23163 +       h_inode = au_h_iptr(inode, bindex);
23164 +       if (!au_test_ro(sb, bindex, inode)) {
23165 +               h_mnt = au_sbr_mnt(sb, bindex);
23166 +               err = vfsub_mnt_want_write(h_mnt);
23167 +               if (!err) {
23168 +                       err = vfsub_update_time(h_inode, flags);
23169 +                       vfsub_mnt_drop_write(h_mnt);
23170 +               }
23171 +       } else if (au_is_special(h_inode)) {
23172 +               /*
23173 +                * Never copy-up here.
23174 +                * These special files may already be opened and used for
23175 +                * communicating. If we copied it up, then the communication
23176 +                * would be corrupted.
23177 +                */
23178 +               AuWarn1("timestamps for i%lu are ignored "
23179 +                       "since it is on readonly branch (hi%lu).\n",
23180 +                       inode->i_ino, h_inode->i_ino);
23181 +       } else if (flags & ~S_ATIME) {
23182 +               err = -EIO;
23183 +               AuIOErr1("unexpected flags 0x%x\n", flags);
23184 +               AuDebugOn(1);
23185 +       }
23186 +
23187 +       if (!err)
23188 +               au_cpup_attr_timesizes(inode);
23189 +       ii_write_unlock(inode);
23190 +       si_read_unlock(sb);
23191 +       lockdep_on();
23192 +
23193 +       if (!err && (flags & S_VERSION))
23194 +               inode_inc_iversion(inode);
23195 +
23196 +       return err;
23197 +}
23198 +
23199 +/* ---------------------------------------------------------------------- */
23200 +
23201 +/* no getattr version will be set by module.c:aufs_init() */
23202 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23203 +       aufs_iop[] = {
23204 +       [AuIop_SYMLINK] = {
23205 +               .permission     = aufs_permission,
23206 +#ifdef CONFIG_FS_POSIX_ACL
23207 +               .get_inode_acl  = aufs_get_inode_acl,
23208 +               .get_acl        = aufs_get_acl,
23209 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
23210 +#endif
23211 +
23212 +               .setattr        = aufs_setattr,
23213 +               .getattr        = aufs_getattr,
23214 +
23215 +#ifdef CONFIG_AUFS_XATTR
23216 +               .listxattr      = aufs_listxattr,
23217 +#endif
23218 +
23219 +               .get_link       = aufs_get_link
23220 +
23221 +               /* .update_time = aufs_update_time */
23222 +       },
23223 +       [AuIop_DIR] = {
23224 +               .create         = aufs_create,
23225 +               .lookup         = aufs_lookup,
23226 +               .link           = aufs_link,
23227 +               .unlink         = aufs_unlink,
23228 +               .symlink        = aufs_symlink,
23229 +               .mkdir          = aufs_mkdir,
23230 +               .rmdir          = aufs_rmdir,
23231 +               .mknod          = aufs_mknod,
23232 +               .rename         = aufs_rename,
23233 +
23234 +               .permission     = aufs_permission,
23235 +#ifdef CONFIG_FS_POSIX_ACL
23236 +               .get_inode_acl  = aufs_get_inode_acl,
23237 +               .get_acl        = aufs_get_acl,
23238 +               .set_acl        = aufs_set_acl,
23239 +#endif
23240 +
23241 +               .setattr        = aufs_setattr,
23242 +               .getattr        = aufs_getattr,
23243 +
23244 +#ifdef CONFIG_AUFS_XATTR
23245 +               .listxattr      = aufs_listxattr,
23246 +#endif
23247 +
23248 +               .update_time    = aufs_update_time,
23249 +               .atomic_open    = aufs_atomic_open,
23250 +               .tmpfile        = aufs_tmpfile
23251 +       },
23252 +       [AuIop_OTHER] = {
23253 +               .permission     = aufs_permission,
23254 +#ifdef CONFIG_FS_POSIX_ACL
23255 +               .get_inode_acl  = aufs_get_inode_acl,
23256 +               .get_acl        = aufs_get_acl,
23257 +               .set_acl        = aufs_set_acl,
23258 +#endif
23259 +
23260 +               .setattr        = aufs_setattr,
23261 +               .getattr        = aufs_getattr,
23262 +
23263 +#ifdef CONFIG_AUFS_XATTR
23264 +               .listxattr      = aufs_listxattr,
23265 +#endif
23266 +
23267 +               .update_time    = aufs_update_time
23268 +       }
23269 +};
23270 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
23271 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
23272 +++ linux/fs/aufs/i_op_del.c    2024-05-13 17:13:00.618070924 +0200
23273 @@ -0,0 +1,523 @@
23274 +// SPDX-License-Identifier: GPL-2.0
23275 +/*
23276 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23277 + *
23278 + * This program is free software; you can redistribute it and/or modify
23279 + * it under the terms of the GNU General Public License as published by
23280 + * the Free Software Foundation; either version 2 of the License, or
23281 + * (at your option) any later version.
23282 + *
23283 + * This program is distributed in the hope that it will be useful,
23284 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23285 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23286 + * GNU General Public License for more details.
23287 + *
23288 + * You should have received a copy of the GNU General Public License
23289 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23290 + */
23291 +
23292 +/*
23293 + * inode operations (del entry)
23294 + */
23295 +
23296 +#include <linux/iversion.h>
23297 +#include "aufs.h"
23298 +
23299 +/*
23300 + * decide if a new whiteout for @dentry is necessary or not.
23301 + * when it is necessary, prepare the parent dir for the upper branch whose
23302 + * branch index is @bcpup for creation. the actual creation of the whiteout will
23303 + * be done by caller.
23304 + * return value:
23305 + * 0: wh is unnecessary
23306 + * plus: wh is necessary
23307 + * minus: error
23308 + */
23309 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
23310 +{
23311 +       int need_wh, err;
23312 +       aufs_bindex_t btop;
23313 +       struct super_block *sb;
23314 +
23315 +       sb = dentry->d_sb;
23316 +       btop = au_dbtop(dentry);
23317 +       if (*bcpup < 0) {
23318 +               *bcpup = btop;
23319 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
23320 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
23321 +                       *bcpup = err;
23322 +                       if (unlikely(err < 0))
23323 +                               goto out;
23324 +               }
23325 +       } else
23326 +               AuDebugOn(btop < *bcpup
23327 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
23328 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
23329 +
23330 +       if (*bcpup != btop) {
23331 +               err = au_cpup_dirs(dentry, *bcpup);
23332 +               if (unlikely(err))
23333 +                       goto out;
23334 +               need_wh = 1;
23335 +       } else {
23336 +               struct au_dinfo *dinfo, *tmp;
23337 +
23338 +               need_wh = -ENOMEM;
23339 +               dinfo = au_di(dentry);
23340 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23341 +               if (tmp) {
23342 +                       au_di_cp(tmp, dinfo);
23343 +                       au_di_swap(tmp, dinfo);
23344 +                       /* returns the number of positive dentries */
23345 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
23346 +                                                /* AuLkup_IGNORE_PERM */ 0);
23347 +                       au_di_swap(tmp, dinfo);
23348 +                       au_rw_write_unlock(&tmp->di_rwsem);
23349 +                       au_di_free(tmp);
23350 +               }
23351 +       }
23352 +       AuDbg("need_wh %d\n", need_wh);
23353 +       err = need_wh;
23354 +
23355 +out:
23356 +       return err;
23357 +}
23358 +
23359 +/*
23360 + * simple tests for the del-entry operations.
23361 + * following the checks in vfs, plus the parent-child relationship.
23362 + */
23363 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23364 +              struct dentry *h_parent, int isdir)
23365 +{
23366 +       int err;
23367 +       umode_t h_mode;
23368 +       struct dentry *h_dentry, *h_latest;
23369 +       struct inode *h_inode;
23370 +       struct path h_ppath;
23371 +       struct super_block *sb;
23372 +       struct au_branch *br;
23373 +       struct mnt_idmap *h_idmap;
23374 +
23375 +       h_dentry = au_h_dptr(dentry, bindex);
23376 +       if (d_really_is_positive(dentry)) {
23377 +               err = -ENOENT;
23378 +               if (unlikely(d_is_negative(h_dentry)))
23379 +                       goto out;
23380 +               h_inode = d_inode(h_dentry);
23381 +               if (unlikely(!h_inode->i_nlink))
23382 +                       goto out;
23383 +
23384 +               h_mode = h_inode->i_mode;
23385 +               if (!isdir) {
23386 +                       err = -EISDIR;
23387 +                       if (unlikely(S_ISDIR(h_mode)))
23388 +                               goto out;
23389 +               } else if (unlikely(!S_ISDIR(h_mode))) {
23390 +                       err = -ENOTDIR;
23391 +                       goto out;
23392 +               }
23393 +       } else {
23394 +               /* rename(2) case */
23395 +               err = -EIO;
23396 +               if (unlikely(d_is_positive(h_dentry)))
23397 +                       goto out;
23398 +       }
23399 +
23400 +       err = -ENOENT;
23401 +       /* expected parent dir is locked */
23402 +       if (unlikely(h_parent != h_dentry->d_parent))
23403 +               goto out;
23404 +       err = 0;
23405 +
23406 +       /*
23407 +        * rmdir a dir may break the consistency on some filesystem.
23408 +        * let's try heavy test.
23409 +        */
23410 +       err = -EACCES;
23411 +       sb = dentry->d_sb;
23412 +       br = au_sbr(sb, bindex);
23413 +       h_idmap = au_br_idmap(br);
23414 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
23415 +                    && au_test_h_perm(h_idmap, d_inode(h_parent),
23416 +                                      MAY_EXEC | MAY_WRITE)))
23417 +               goto out;
23418 +
23419 +       h_ppath.dentry = h_parent;
23420 +       h_ppath.mnt = au_br_mnt(br);
23421 +       h_latest = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
23422 +       err = -EIO;
23423 +       if (IS_ERR(h_latest))
23424 +               goto out;
23425 +       if (h_latest == h_dentry)
23426 +               err = 0;
23427 +       dput(h_latest);
23428 +
23429 +out:
23430 +       return err;
23431 +}
23432 +
23433 +/*
23434 + * decide the branch where we operate for @dentry. the branch index will be set
23435 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
23436 + * dir for reverting.
23437 + * when a new whiteout is necessary, create it.
23438 + */
23439 +static struct dentry*
23440 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23441 +                   struct au_dtime *dt, struct au_pin *pin)
23442 +{
23443 +       struct dentry *wh_dentry;
23444 +       struct super_block *sb;
23445 +       struct path h_path;
23446 +       int err, need_wh;
23447 +       unsigned int udba;
23448 +       aufs_bindex_t bcpup;
23449 +
23450 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23451 +       wh_dentry = ERR_PTR(need_wh);
23452 +       if (unlikely(need_wh < 0))
23453 +               goto out;
23454 +
23455 +       sb = dentry->d_sb;
23456 +       udba = au_opt_udba(sb);
23457 +       bcpup = *rbcpup;
23458 +       err = au_pin(pin, dentry, bcpup, udba,
23459 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23460 +       wh_dentry = ERR_PTR(err);
23461 +       if (unlikely(err))
23462 +               goto out;
23463 +
23464 +       h_path.dentry = au_pinned_h_parent(pin);
23465 +       if (udba != AuOpt_UDBA_NONE
23466 +           && au_dbtop(dentry) == bcpup) {
23467 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23468 +               wh_dentry = ERR_PTR(err);
23469 +               if (unlikely(err))
23470 +                       goto out_unpin;
23471 +       }
23472 +
23473 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
23474 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23475 +       wh_dentry = NULL;
23476 +       if (!need_wh)
23477 +               goto out; /* success, no need to create whiteout */
23478 +
23479 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23480 +       if (IS_ERR(wh_dentry))
23481 +               goto out_unpin;
23482 +
23483 +       /* returns with the parent is locked and wh_dentry is dget-ed */
23484 +       goto out; /* success */
23485 +
23486 +out_unpin:
23487 +       au_unpin(pin);
23488 +out:
23489 +       return wh_dentry;
23490 +}
23491 +
23492 +/*
23493 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
23494 + * in order to be revertible and save time for removing many child whiteouts
23495 + * under the dir.
23496 + * returns 1 when there are too many child whiteout and caller should remove
23497 + * them asynchronously. returns 0 when the number of children is enough small to
23498 + * remove now or the branch fs is a remote fs.
23499 + * otherwise return an error.
23500 + */
23501 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23502 +                          struct au_nhash *whlist, struct inode *dir)
23503 +{
23504 +       int rmdir_later, err, dirwh;
23505 +       struct dentry *h_dentry;
23506 +       struct super_block *sb;
23507 +       struct inode *inode;
23508 +
23509 +       sb = dentry->d_sb;
23510 +       SiMustAnyLock(sb);
23511 +       h_dentry = au_h_dptr(dentry, bindex);
23512 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23513 +       if (unlikely(err))
23514 +               goto out;
23515 +
23516 +       /* stop monitoring */
23517 +       inode = d_inode(dentry);
23518 +       au_hn_free(au_hi(inode, bindex));
23519 +
23520 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
23521 +               dirwh = au_sbi(sb)->si_dirwh;
23522 +               rmdir_later = (dirwh <= 1);
23523 +               if (!rmdir_later)
23524 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23525 +                                                             dirwh);
23526 +               if (rmdir_later)
23527 +                       return rmdir_later;
23528 +       }
23529 +
23530 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23531 +       if (unlikely(err)) {
23532 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23533 +                       h_dentry, bindex, err);
23534 +               err = 0;
23535 +       }
23536 +
23537 +out:
23538 +       AuTraceErr(err);
23539 +       return err;
23540 +}
23541 +
23542 +/*
23543 + * final procedure for deleting a entry.
23544 + * maintain dentry and iattr.
23545 + */
23546 +static void epilog(struct inode *dir, struct dentry *dentry,
23547 +                  aufs_bindex_t bindex)
23548 +{
23549 +       struct inode *inode;
23550 +
23551 +       inode = d_inode(dentry);
23552 +       d_drop(dentry);
23553 +       inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
23554 +
23555 +       au_dir_ts(dir, bindex);
23556 +       inode_inc_iversion(dir);
23557 +}
23558 +
23559 +/*
23560 + * when an error happened, remove the created whiteout and revert everything.
23561 + */
23562 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23563 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
23564 +                    struct dentry *dentry, struct au_dtime *dt)
23565 +{
23566 +       int rerr;
23567 +       struct path h_path = {
23568 +               .dentry = wh_dentry,
23569 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
23570 +       };
23571 +
23572 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
23573 +       if (!rerr) {
23574 +               au_set_dbwh(dentry, bwh);
23575 +               au_dtime_revert(dt);
23576 +               return 0;
23577 +       }
23578 +
23579 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
23580 +       return -EIO;
23581 +}
23582 +
23583 +/* ---------------------------------------------------------------------- */
23584 +
23585 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
23586 +{
23587 +       int err;
23588 +       aufs_bindex_t bwh, bindex, btop;
23589 +       struct inode *inode, *h_dir, *delegated, *h_inode;
23590 +       struct dentry *parent, *wh_dentry;
23591 +       /* to reduce stack size */
23592 +       struct {
23593 +               struct au_dtime dt;
23594 +               struct au_pin pin;
23595 +               struct path h_path;
23596 +       } *a;
23597 +
23598 +       IMustLock(dir);
23599 +
23600 +       err = -ENOMEM;
23601 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23602 +       if (unlikely(!a))
23603 +               goto out;
23604 +
23605 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23606 +       if (unlikely(err))
23607 +               goto out_free;
23608 +       err = au_d_hashed_positive(dentry);
23609 +       if (unlikely(err))
23610 +               goto out_unlock;
23611 +       inode = d_inode(dentry);
23612 +       IMustLock(inode);
23613 +       err = -EISDIR;
23614 +       if (unlikely(d_is_dir(dentry)))
23615 +               goto out_unlock; /* possible? */
23616 +
23617 +       btop = au_dbtop(dentry);
23618 +       bwh = au_dbwh(dentry);
23619 +       bindex = -1;
23620 +       parent = dentry->d_parent; /* dir inode is locked */
23621 +       di_write_lock_parent(parent);
23622 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23623 +                                       &a->pin);
23624 +       err = PTR_ERR(wh_dentry);
23625 +       if (IS_ERR(wh_dentry))
23626 +               goto out_parent;
23627 +
23628 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23629 +       a->h_path.dentry = au_h_dptr(dentry, btop);
23630 +       dget(a->h_path.dentry);
23631 +       if (bindex == btop) {
23632 +               h_dir = au_pinned_h_dir(&a->pin);
23633 +               delegated = NULL;
23634 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23635 +               if (unlikely(err == -EWOULDBLOCK)) {
23636 +                       pr_warn("cannot retry for NFSv4 delegation"
23637 +                               " for an internal unlink\n");
23638 +                       iput(delegated);
23639 +               }
23640 +       } else {
23641 +               /* dir inode is locked */
23642 +               h_dir = d_inode(wh_dentry->d_parent);
23643 +               IMustLock(h_dir);
23644 +               err = 0;
23645 +       }
23646 +
23647 +       if (!err) {
23648 +               vfsub_drop_nlink(inode);
23649 +               epilog(dir, dentry, bindex);
23650 +
23651 +               /* update target timestamps */
23652 +               if (bindex == btop) {
23653 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23654 +                       /*ignore*/
23655 +                       h_inode = d_inode(a->h_path.dentry);
23656 +                       inode_set_ctime_to_ts(inode, inode_get_ctime(h_inode));
23657 +               } else
23658 +                       /* todo: this timestamp may be reverted later */
23659 +                       inode_set_ctime_to_ts(inode, inode_get_ctime(h_dir));
23660 +               goto out_unpin; /* success */
23661 +       }
23662 +
23663 +       /* revert */
23664 +       if (wh_dentry) {
23665 +               int rerr;
23666 +
23667 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23668 +                                &a->dt);
23669 +               if (rerr)
23670 +                       err = rerr;
23671 +       }
23672 +
23673 +out_unpin:
23674 +       au_unpin(&a->pin);
23675 +       dput(wh_dentry);
23676 +       dput(a->h_path.dentry);
23677 +out_parent:
23678 +       di_write_unlock(parent);
23679 +out_unlock:
23680 +       aufs_read_unlock(dentry, AuLock_DW);
23681 +out_free:
23682 +       au_kfree_rcu(a);
23683 +out:
23684 +       return err;
23685 +}
23686 +
23687 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
23688 +{
23689 +       int err, rmdir_later;
23690 +       aufs_bindex_t bwh, bindex, btop;
23691 +       struct inode *inode;
23692 +       struct dentry *parent, *wh_dentry, *h_dentry;
23693 +       struct au_whtmp_rmdir *args;
23694 +       /* to reduce stack size */
23695 +       struct {
23696 +               struct au_dtime dt;
23697 +               struct au_pin pin;
23698 +       } *a;
23699 +
23700 +       IMustLock(dir);
23701 +
23702 +       err = -ENOMEM;
23703 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23704 +       if (unlikely(!a))
23705 +               goto out;
23706 +
23707 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23708 +       if (unlikely(err))
23709 +               goto out_free;
23710 +       err = au_alive_dir(dentry);
23711 +       if (unlikely(err))
23712 +               goto out_unlock;
23713 +       inode = d_inode(dentry);
23714 +       IMustLock(inode);
23715 +       err = -ENOTDIR;
23716 +       if (unlikely(!d_is_dir(dentry)))
23717 +               goto out_unlock; /* possible? */
23718 +
23719 +       err = -ENOMEM;
23720 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23721 +       if (unlikely(!args))
23722 +               goto out_unlock;
23723 +
23724 +       parent = dentry->d_parent; /* dir inode is locked */
23725 +       di_write_lock_parent(parent);
23726 +       err = au_test_empty(dentry, &args->whlist);
23727 +       if (unlikely(err))
23728 +               goto out_parent;
23729 +
23730 +       btop = au_dbtop(dentry);
23731 +       bwh = au_dbwh(dentry);
23732 +       bindex = -1;
23733 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23734 +                                       &a->pin);
23735 +       err = PTR_ERR(wh_dentry);
23736 +       if (IS_ERR(wh_dentry))
23737 +               goto out_parent;
23738 +
23739 +       h_dentry = au_h_dptr(dentry, btop);
23740 +       dget(h_dentry);
23741 +       rmdir_later = 0;
23742 +       if (bindex == btop) {
23743 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
23744 +               if (err > 0) {
23745 +                       rmdir_later = err;
23746 +                       err = 0;
23747 +               }
23748 +       } else {
23749 +               /* stop monitoring */
23750 +               au_hn_free(au_hi(inode, btop));
23751 +
23752 +               /* dir inode is locked */
23753 +               IMustLock(d_inode(wh_dentry->d_parent));
23754 +               err = 0;
23755 +       }
23756 +
23757 +       if (!err) {
23758 +               vfsub_dead_dir(inode);
23759 +               au_set_dbdiropq(dentry, -1);
23760 +               epilog(dir, dentry, bindex);
23761 +
23762 +               if (rmdir_later) {
23763 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
23764 +                       args = NULL;
23765 +               }
23766 +
23767 +               goto out_unpin; /* success */
23768 +       }
23769 +
23770 +       /* revert */
23771 +       AuLabel(revert);
23772 +       if (wh_dentry) {
23773 +               int rerr;
23774 +
23775 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23776 +                                &a->dt);
23777 +               if (rerr)
23778 +                       err = rerr;
23779 +       }
23780 +
23781 +out_unpin:
23782 +       au_unpin(&a->pin);
23783 +       dput(wh_dentry);
23784 +       dput(h_dentry);
23785 +out_parent:
23786 +       di_write_unlock(parent);
23787 +       if (args)
23788 +               au_whtmp_rmdir_free(args);
23789 +out_unlock:
23790 +       aufs_read_unlock(dentry, AuLock_DW);
23791 +out_free:
23792 +       au_kfree_rcu(a);
23793 +out:
23794 +       AuTraceErr(err);
23795 +       return err;
23796 +}
23797 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
23798 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
23799 +++ linux/fs/aufs/i_op_ren.c    2024-05-13 17:13:00.621404284 +0200
23800 @@ -0,0 +1,1264 @@
23801 +// SPDX-License-Identifier: GPL-2.0
23802 +/*
23803 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23804 + *
23805 + * This program is free software; you can redistribute it and/or modify
23806 + * it under the terms of the GNU General Public License as published by
23807 + * the Free Software Foundation; either version 2 of the License, or
23808 + * (at your option) any later version.
23809 + *
23810 + * This program is distributed in the hope that it will be useful,
23811 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23812 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23813 + * GNU General Public License for more details.
23814 + *
23815 + * You should have received a copy of the GNU General Public License
23816 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23817 + */
23818 +
23819 +/*
23820 + * inode operation (rename entry)
23821 + * todo: this is crazy monster
23822 + */
23823 +
23824 +#include <linux/iversion.h>
23825 +#include "aufs.h"
23826 +
23827 +enum { AuSRC, AuDST, AuSrcDst };
23828 +enum { AuPARENT, AuCHILD, AuParentChild };
23829 +
23830 +#define AuRen_ISDIR_SRC                BIT(0)
23831 +#define AuRen_ISDIR_DST                BIT(1)
23832 +#define AuRen_ISSAMEDIR                BIT(2)
23833 +#define AuRen_WHSRC            BIT(3)
23834 +#define AuRen_WHDST            BIT(4)
23835 +#define AuRen_MNT_WRITE                BIT(5)
23836 +#define AuRen_DT_DSTDIR                BIT(6)
23837 +#define AuRen_DIROPQ_SRC       BIT(7)
23838 +#define AuRen_DIROPQ_DST       BIT(8)
23839 +#define AuRen_DIRREN           BIT(9)
23840 +#define AuRen_DROPPED_SRC      BIT(10)
23841 +#define AuRen_DROPPED_DST      BIT(11)
23842 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
23843 +#define au_fset_ren(flags, name) \
23844 +       do { (flags) |= AuRen_##name; } while (0)
23845 +#define au_fclr_ren(flags, name) \
23846 +       do { (flags) &= ~AuRen_##name; } while (0)
23847 +
23848 +#ifndef CONFIG_AUFS_DIRREN
23849 +#undef AuRen_DIRREN
23850 +#define AuRen_DIRREN           0
23851 +#endif
23852 +
23853 +struct au_ren_args {
23854 +       struct {
23855 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
23856 +                       *wh_dentry;
23857 +               struct inode *dir, *inode;
23858 +               struct au_hinode *hdir, *hinode;
23859 +               struct au_dtime dt[AuParentChild];
23860 +               aufs_bindex_t btop, bdiropq;
23861 +       } sd[AuSrcDst];
23862 +
23863 +#define src_dentry     sd[AuSRC].dentry
23864 +#define src_dir                sd[AuSRC].dir
23865 +#define src_inode      sd[AuSRC].inode
23866 +#define src_h_dentry   sd[AuSRC].h_dentry
23867 +#define src_parent     sd[AuSRC].parent
23868 +#define src_h_parent   sd[AuSRC].h_parent
23869 +#define src_wh_dentry  sd[AuSRC].wh_dentry
23870 +#define src_hdir       sd[AuSRC].hdir
23871 +#define src_hinode     sd[AuSRC].hinode
23872 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
23873 +#define src_dt         sd[AuSRC].dt
23874 +#define src_btop       sd[AuSRC].btop
23875 +#define src_bdiropq    sd[AuSRC].bdiropq
23876 +
23877 +#define dst_dentry     sd[AuDST].dentry
23878 +#define dst_dir                sd[AuDST].dir
23879 +#define dst_inode      sd[AuDST].inode
23880 +#define dst_h_dentry   sd[AuDST].h_dentry
23881 +#define dst_parent     sd[AuDST].parent
23882 +#define dst_h_parent   sd[AuDST].h_parent
23883 +#define dst_wh_dentry  sd[AuDST].wh_dentry
23884 +#define dst_hdir       sd[AuDST].hdir
23885 +#define dst_hinode     sd[AuDST].hinode
23886 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
23887 +#define dst_dt         sd[AuDST].dt
23888 +#define dst_btop       sd[AuDST].btop
23889 +#define dst_bdiropq    sd[AuDST].bdiropq
23890 +
23891 +       struct dentry *h_trap;
23892 +       struct au_branch *br;
23893 +       struct path h_path;
23894 +       struct au_nhash whlist;
23895 +       aufs_bindex_t btgt, src_bwh;
23896 +
23897 +       struct {
23898 +               unsigned short auren_flags;
23899 +               unsigned char flags;    /* syscall parameter */
23900 +               unsigned char exchange;
23901 +       } __packed;
23902 +
23903 +       struct au_whtmp_rmdir *thargs;
23904 +       struct dentry *h_dst;
23905 +       struct au_hinode *h_root;
23906 +};
23907 +
23908 +/* ---------------------------------------------------------------------- */
23909 +
23910 +/*
23911 + * functions for reverting.
23912 + * when an error happened in a single rename systemcall, we should revert
23913 + * everything as if nothing happened.
23914 + * we don't need to revert the copied-up/down the parent dir since they are
23915 + * harmless.
23916 + */
23917 +
23918 +#define RevertFailure(fmt, ...) do { \
23919 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23920 +               ##__VA_ARGS__, err, rerr); \
23921 +       err = -EIO; \
23922 +} while (0)
23923 +
23924 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
23925 +{
23926 +       int rerr;
23927 +       struct dentry *d;
23928 +#define src_or_dst(member) a->sd[idx].member
23929 +
23930 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
23931 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23932 +       rerr = au_diropq_remove(d, a->btgt);
23933 +       au_hn_inode_unlock(src_or_dst(hinode));
23934 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
23935 +       if (rerr)
23936 +               RevertFailure("remove diropq %pd", d);
23937 +
23938 +#undef src_or_dst_
23939 +}
23940 +
23941 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23942 +{
23943 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23944 +               au_ren_do_rev_diropq(err, a, AuSRC);
23945 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23946 +               au_ren_do_rev_diropq(err, a, AuDST);
23947 +}
23948 +
23949 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
23950 +{
23951 +       int rerr;
23952 +       struct inode *delegated;
23953 +       struct path h_ppath = {
23954 +               .dentry = a->src_h_parent,
23955 +               .mnt    = a->h_path.mnt
23956 +       };
23957 +
23958 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
23959 +       rerr = PTR_ERR(a->h_path.dentry);
23960 +       if (IS_ERR(a->h_path.dentry)) {
23961 +               RevertFailure("lkup one %pd", a->src_dentry);
23962 +               return;
23963 +       }
23964 +
23965 +       delegated = NULL;
23966 +       rerr = vfsub_rename(a->dst_h_dir,
23967 +                           au_h_dptr(a->src_dentry, a->btgt),
23968 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
23969 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23970 +               pr_warn("cannot retry for NFSv4 delegation"
23971 +                       " for an internal rename\n");
23972 +               iput(delegated);
23973 +       }
23974 +       d_drop(a->h_path.dentry);
23975 +       dput(a->h_path.dentry);
23976 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23977 +       if (rerr)
23978 +               RevertFailure("rename %pd", a->src_dentry);
23979 +}
23980 +
23981 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
23982 +{
23983 +       int rerr;
23984 +       struct inode *delegated;
23985 +       struct path h_ppath = {
23986 +               .dentry = a->dst_h_parent,
23987 +               .mnt    = a->h_path.mnt
23988 +       };
23989 +
23990 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
23991 +       rerr = PTR_ERR(a->h_path.dentry);
23992 +       if (IS_ERR(a->h_path.dentry)) {
23993 +               RevertFailure("lkup one %pd", a->dst_dentry);
23994 +               return;
23995 +       }
23996 +       if (d_is_positive(a->h_path.dentry)) {
23997 +               d_drop(a->h_path.dentry);
23998 +               dput(a->h_path.dentry);
23999 +               return;
24000 +       }
24001 +
24002 +       delegated = NULL;
24003 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
24004 +                           &delegated, a->flags);
24005 +       if (unlikely(rerr == -EWOULDBLOCK)) {
24006 +               pr_warn("cannot retry for NFSv4 delegation"
24007 +                       " for an internal rename\n");
24008 +               iput(delegated);
24009 +       }
24010 +       d_drop(a->h_path.dentry);
24011 +       dput(a->h_path.dentry);
24012 +       if (!rerr)
24013 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
24014 +       else
24015 +               RevertFailure("rename %pd", a->h_dst);
24016 +}
24017 +
24018 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
24019 +{
24020 +       int rerr;
24021 +
24022 +       a->h_path.dentry = a->src_wh_dentry;
24023 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
24024 +       au_set_dbwh(a->src_dentry, a->src_bwh);
24025 +       if (rerr)
24026 +               RevertFailure("unlink %pd", a->src_wh_dentry);
24027 +}
24028 +#undef RevertFailure
24029 +
24030 +/* ---------------------------------------------------------------------- */
24031 +
24032 +/*
24033 + * when we have to copyup the renaming entry, do it with the rename-target name
24034 + * in order to minimize the cost (the later actual rename is unnecessary).
24035 + * otherwise rename it on the target branch.
24036 + */
24037 +static int au_ren_or_cpup(struct au_ren_args *a)
24038 +{
24039 +       int err;
24040 +       struct dentry *d;
24041 +       struct inode *delegated;
24042 +
24043 +       d = a->src_dentry;
24044 +       if (au_dbtop(d) == a->btgt) {
24045 +               a->h_path.dentry = a->dst_h_dentry;
24046 +               AuDebugOn(au_dbtop(d) != a->btgt);
24047 +               delegated = NULL;
24048 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
24049 +                                  a->dst_h_dir, &a->h_path, &delegated,
24050 +                                  a->flags);
24051 +               if (unlikely(err == -EWOULDBLOCK)) {
24052 +                       pr_warn("cannot retry for NFSv4 delegation"
24053 +                               " for an internal rename\n");
24054 +                       iput(delegated);
24055 +               }
24056 +       } else
24057 +               BUG();
24058 +
24059 +       if (!err && a->h_dst)
24060 +               /* it will be set to dinfo later */
24061 +               dget(a->h_dst);
24062 +
24063 +       return err;
24064 +}
24065 +
24066 +/* cf. aufs_rmdir() */
24067 +static int au_ren_del_whtmp(struct au_ren_args *a)
24068 +{
24069 +       int err;
24070 +       struct inode *dir;
24071 +
24072 +       dir = a->dst_dir;
24073 +       SiMustAnyLock(dir->i_sb);
24074 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
24075 +                                    au_sbi(dir->i_sb)->si_dirwh)
24076 +           || au_test_fs_remote(a->h_dst->d_sb)) {
24077 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
24078 +               if (unlikely(err))
24079 +                       pr_warn("failed removing whtmp dir %pd (%d), "
24080 +                               "ignored.\n", a->h_dst, err);
24081 +       } else {
24082 +               au_nhash_wh_free(&a->thargs->whlist);
24083 +               a->thargs->whlist = a->whlist;
24084 +               a->whlist.nh_num = 0;
24085 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24086 +               dput(a->h_dst);
24087 +               a->thargs = NULL;
24088 +       }
24089 +
24090 +       return 0;
24091 +}
24092 +
24093 +/* make it 'opaque' dir. */
24094 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
24095 +{
24096 +       int err;
24097 +       struct dentry *d, *diropq;
24098 +#define src_or_dst(member) a->sd[idx].member
24099 +
24100 +       err = 0;
24101 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
24102 +       src_or_dst(bdiropq) = au_dbdiropq(d);
24103 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24104 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24105 +       diropq = au_diropq_create(d, a->btgt);
24106 +       au_hn_inode_unlock(src_or_dst(hinode));
24107 +       if (IS_ERR(diropq))
24108 +               err = PTR_ERR(diropq);
24109 +       else
24110 +               dput(diropq);
24111 +
24112 +#undef src_or_dst_
24113 +       return err;
24114 +}
24115 +
24116 +static int au_ren_diropq(struct au_ren_args *a)
24117 +{
24118 +       int err;
24119 +       unsigned char always;
24120 +       struct dentry *d;
24121 +
24122 +       err = 0;
24123 +       d = a->dst_dentry; /* already renamed on the branch */
24124 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24125 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24126 +           && !au_ftest_ren(a->auren_flags, DIRREN)
24127 +           && a->btgt != au_dbdiropq(a->src_dentry)
24128 +           && (a->dst_wh_dentry
24129 +               || a->btgt <= au_dbdiropq(d)
24130 +               /* hide the lower to keep xino */
24131 +               /* the lowers may not be a dir, but we hide them anyway */
24132 +               || a->btgt < au_dbbot(d)
24133 +               || always)) {
24134 +               AuDbg("here\n");
24135 +               err = au_ren_do_diropq(a, AuSRC);
24136 +               if (unlikely(err))
24137 +                       goto out;
24138 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
24139 +       }
24140 +       if (!a->exchange)
24141 +               goto out; /* success */
24142 +
24143 +       d = a->src_dentry; /* already renamed on the branch */
24144 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24145 +           && a->btgt != au_dbdiropq(a->dst_dentry)
24146 +           && (a->btgt < au_dbdiropq(d)
24147 +               || a->btgt < au_dbbot(d)
24148 +               || always)) {
24149 +               AuDbgDentry(a->src_dentry);
24150 +               AuDbgDentry(a->dst_dentry);
24151 +               err = au_ren_do_diropq(a, AuDST);
24152 +               if (unlikely(err))
24153 +                       goto out_rev_src;
24154 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
24155 +       }
24156 +       goto out; /* success */
24157 +
24158 +out_rev_src:
24159 +       AuDbg("err %d, reverting src\n", err);
24160 +       au_ren_rev_diropq(err, a);
24161 +out:
24162 +       return err;
24163 +}
24164 +
24165 +static int do_rename(struct au_ren_args *a)
24166 +{
24167 +       int err;
24168 +       struct dentry *d, *h_d;
24169 +       struct inode *h_inode;
24170 +
24171 +       if (!a->exchange) {
24172 +               /* prepare workqueue args for asynchronous rmdir */
24173 +               h_d = a->dst_h_dentry;
24174 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24175 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
24176 +                   && d_is_positive(h_d)) {
24177 +                       err = -ENOMEM;
24178 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24179 +                                                        GFP_NOFS);
24180 +                       if (unlikely(!a->thargs))
24181 +                               goto out;
24182 +                       a->h_dst = dget(h_d);
24183 +               }
24184 +
24185 +               /* create whiteout for src_dentry */
24186 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
24187 +                       a->src_bwh = au_dbwh(a->src_dentry);
24188 +                       AuDebugOn(a->src_bwh >= 0);
24189 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24190 +                                                       a->src_h_parent);
24191 +                       err = PTR_ERR(a->src_wh_dentry);
24192 +                       if (IS_ERR(a->src_wh_dentry))
24193 +                               goto out_thargs;
24194 +               }
24195 +
24196 +               /* lookup whiteout for dentry */
24197 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
24198 +                       h_d = au_wh_lkup(a->dst_h_parent,
24199 +                                        &a->dst_dentry->d_name, a->br);
24200 +                       err = PTR_ERR(h_d);
24201 +                       if (IS_ERR(h_d))
24202 +                               goto out_whsrc;
24203 +                       if (d_is_negative(h_d))
24204 +                               dput(h_d);
24205 +                       else
24206 +                               a->dst_wh_dentry = h_d;
24207 +               }
24208 +
24209 +               /* rename dentry to tmpwh */
24210 +               if (a->thargs) {
24211 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
24212 +                       if (unlikely(err))
24213 +                               goto out_whdst;
24214 +
24215 +                       d = a->dst_dentry;
24216 +                       au_set_h_dptr(d, a->btgt, NULL);
24217 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
24218 +                       if (unlikely(err))
24219 +                               goto out_whtmp;
24220 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
24221 +               }
24222 +       }
24223 +
24224 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
24225 +#if 0 /* debugging */
24226 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24227 +              && d_is_positive(a->dst_h_dentry)
24228 +              && a->src_btop != a->btgt);
24229 +#endif
24230 +
24231 +       /* rename by vfs_rename or cpup */
24232 +       err = au_ren_or_cpup(a);
24233 +       if (unlikely(err))
24234 +               /* leave the copied-up one */
24235 +               goto out_whtmp;
24236 +
24237 +       /* make dir opaque */
24238 +       err = au_ren_diropq(a);
24239 +       if (unlikely(err))
24240 +               goto out_rename;
24241 +
24242 +       /* update target timestamps */
24243 +       if (a->exchange) {
24244 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24245 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24246 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24247 +               h_inode = d_inode(a->h_path.dentry);
24248 +               inode_set_ctime_to_ts(a->dst_inode, inode_get_ctime(h_inode));
24249 +       }
24250 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
24251 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24252 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24253 +       h_inode = d_inode(a->h_path.dentry);
24254 +       inode_set_ctime_to_ts(a->src_inode, inode_get_ctime(h_inode));
24255 +
24256 +       if (!a->exchange) {
24257 +               /* remove whiteout for dentry */
24258 +               if (a->dst_wh_dentry) {
24259 +                       a->h_path.dentry = a->dst_wh_dentry;
24260 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24261 +                                                 a->dst_dentry);
24262 +                       if (unlikely(err))
24263 +                               goto out_diropq;
24264 +               }
24265 +
24266 +               /* remove whtmp */
24267 +               if (a->thargs)
24268 +                       au_ren_del_whtmp(a); /* ignore this error */
24269 +
24270 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24271 +       }
24272 +       err = 0;
24273 +       goto out_success;
24274 +
24275 +out_diropq:
24276 +       au_ren_rev_diropq(err, a);
24277 +out_rename:
24278 +       au_ren_rev_rename(err, a);
24279 +       dput(a->h_dst);
24280 +out_whtmp:
24281 +       if (a->thargs)
24282 +               au_ren_rev_whtmp(err, a);
24283 +out_whdst:
24284 +       dput(a->dst_wh_dentry);
24285 +       a->dst_wh_dentry = NULL;
24286 +out_whsrc:
24287 +       if (a->src_wh_dentry)
24288 +               au_ren_rev_whsrc(err, a);
24289 +out_success:
24290 +       dput(a->src_wh_dentry);
24291 +       dput(a->dst_wh_dentry);
24292 +out_thargs:
24293 +       if (a->thargs) {
24294 +               dput(a->h_dst);
24295 +               au_whtmp_rmdir_free(a->thargs);
24296 +               a->thargs = NULL;
24297 +       }
24298 +out:
24299 +       return err;
24300 +}
24301 +
24302 +/* ---------------------------------------------------------------------- */
24303 +
24304 +/*
24305 + * test if @dentry dir can be rename destination or not.
24306 + * success means, it is a logically empty dir.
24307 + */
24308 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
24309 +{
24310 +       return au_test_empty(dentry, whlist);
24311 +}
24312 +
24313 +/*
24314 + * test if @a->src_dentry dir can be rename source or not.
24315 + * if it can, return 0.
24316 + * success means,
24317 + * - it is a logically empty dir.
24318 + * - or, it exists on writable branch and has no children including whiteouts
24319 + *   on the lower branch unless DIRREN is on.
24320 + */
24321 +static int may_rename_srcdir(struct au_ren_args *a)
24322 +{
24323 +       int err;
24324 +       unsigned int rdhash;
24325 +       aufs_bindex_t btop, btgt;
24326 +       struct dentry *dentry;
24327 +       struct super_block *sb;
24328 +       struct au_sbinfo *sbinfo;
24329 +
24330 +       dentry = a->src_dentry;
24331 +       sb = dentry->d_sb;
24332 +       sbinfo = au_sbi(sb);
24333 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24334 +               au_fset_ren(a->auren_flags, DIRREN);
24335 +
24336 +       btgt = a->btgt;
24337 +       btop = au_dbtop(dentry);
24338 +       if (btop != btgt) {
24339 +               struct au_nhash whlist;
24340 +
24341 +               SiMustAnyLock(sb);
24342 +               rdhash = sbinfo->si_rdhash;
24343 +               if (!rdhash)
24344 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24345 +                                                          dentry));
24346 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24347 +               if (unlikely(err))
24348 +                       goto out;
24349 +               err = au_test_empty(dentry, &whlist);
24350 +               au_nhash_wh_free(&whlist);
24351 +               goto out;
24352 +       }
24353 +
24354 +       if (btop == au_dbtaildir(dentry))
24355 +               return 0; /* success */
24356 +
24357 +       err = au_test_empty_lower(dentry);
24358 +
24359 +out:
24360 +       if (err == -ENOTEMPTY) {
24361 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
24362 +                       err = 0;
24363 +               } else {
24364 +                       AuWarn1("renaming dir who has child(ren) on multiple "
24365 +                               "branches, is not supported\n");
24366 +                       err = -EXDEV;
24367 +               }
24368 +       }
24369 +       return err;
24370 +}
24371 +
24372 +/* side effect: sets whlist and h_dentry */
24373 +static int au_ren_may_dir(struct au_ren_args *a)
24374 +{
24375 +       int err;
24376 +       unsigned int rdhash;
24377 +       struct dentry *d;
24378 +
24379 +       d = a->dst_dentry;
24380 +       SiMustAnyLock(d->d_sb);
24381 +
24382 +       err = 0;
24383 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
24384 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
24385 +               if (!rdhash)
24386 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24387 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24388 +               if (unlikely(err))
24389 +                       goto out;
24390 +
24391 +               if (!a->exchange) {
24392 +                       au_set_dbtop(d, a->dst_btop);
24393 +                       err = may_rename_dstdir(d, &a->whlist);
24394 +                       au_set_dbtop(d, a->btgt);
24395 +               } else
24396 +                       err = may_rename_srcdir(a);
24397 +       }
24398 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
24399 +       if (unlikely(err))
24400 +               goto out;
24401 +
24402 +       d = a->src_dentry;
24403 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
24404 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24405 +               err = may_rename_srcdir(a);
24406 +               if (unlikely(err)) {
24407 +                       au_nhash_wh_free(&a->whlist);
24408 +                       a->whlist.nh_num = 0;
24409 +               }
24410 +       }
24411 +out:
24412 +       return err;
24413 +}
24414 +
24415 +/* ---------------------------------------------------------------------- */
24416 +
24417 +/*
24418 + * simple tests for rename.
24419 + * following the checks in vfs, plus the parent-child relationship.
24420 + */
24421 +static int au_may_ren(struct au_ren_args *a)
24422 +{
24423 +       int err, isdir;
24424 +       struct inode *h_inode;
24425 +
24426 +       if (a->src_btop == a->btgt) {
24427 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
24428 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
24429 +               if (unlikely(err))
24430 +                       goto out;
24431 +               err = -EINVAL;
24432 +               if (unlikely(a->src_h_dentry == a->h_trap))
24433 +                       goto out;
24434 +       }
24435 +
24436 +       err = 0;
24437 +       if (a->dst_btop != a->btgt)
24438 +               goto out;
24439 +
24440 +       err = -ENOTEMPTY;
24441 +       if (unlikely(a->dst_h_dentry == a->h_trap))
24442 +               goto out;
24443 +
24444 +       err = -EIO;
24445 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
24446 +       if (d_really_is_negative(a->dst_dentry)) {
24447 +               if (d_is_negative(a->dst_h_dentry))
24448 +                       err = au_may_add(a->dst_dentry, a->btgt,
24449 +                                        a->dst_h_parent, isdir);
24450 +       } else {
24451 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
24452 +                       goto out;
24453 +               h_inode = d_inode(a->dst_h_dentry);
24454 +               if (h_inode->i_nlink)
24455 +                       err = au_may_del(a->dst_dentry, a->btgt,
24456 +                                        a->dst_h_parent, isdir);
24457 +       }
24458 +
24459 +out:
24460 +       if (unlikely(err == -ENOENT || err == -EEXIST))
24461 +               err = -EIO;
24462 +       AuTraceErr(err);
24463 +       return err;
24464 +}
24465 +
24466 +/* ---------------------------------------------------------------------- */
24467 +
24468 +/*
24469 + * locking order
24470 + * (VFS)
24471 + * - src_dir and dir by lock_rename()
24472 + * - inode if exists
24473 + * (aufs)
24474 + * - lock all
24475 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24476 + *     + si_read_lock
24477 + *     + di_write_lock2_child()
24478 + *       + di_write_lock_child()
24479 + *        + ii_write_lock_child()
24480 + *       + di_write_lock_child2()
24481 + *        + ii_write_lock_child2()
24482 + *     + src_parent and parent
24483 + *       + di_write_lock_parent()
24484 + *        + ii_write_lock_parent()
24485 + *       + di_write_lock_parent2()
24486 + *        + ii_write_lock_parent2()
24487 + *   + lower src_dir and dir by vfsub_lock_rename()
24488 + *   + verify the every relationships between child and parent. if any
24489 + *     of them failed, unlock all and return -EBUSY.
24490 + */
24491 +static void au_ren_unlock(struct au_ren_args *a)
24492 +{
24493 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24494 +                           a->dst_h_parent, a->dst_hdir);
24495 +       if (au_ftest_ren(a->auren_flags, DIRREN)
24496 +           && a->h_root)
24497 +               au_hn_inode_unlock(a->h_root);
24498 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
24499 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
24500 +}
24501 +
24502 +static int au_ren_lock(struct au_ren_args *a)
24503 +{
24504 +       int err;
24505 +       unsigned int udba;
24506 +
24507 +       err = 0;
24508 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24509 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
24510 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24511 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
24512 +
24513 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
24514 +       if (unlikely(err))
24515 +               goto out;
24516 +       au_fset_ren(a->auren_flags, MNT_WRITE);
24517 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24518 +               struct dentry *root;
24519 +               struct inode *dir;
24520 +
24521 +               /*
24522 +                * sbinfo is already locked, so this ii_read_lock is
24523 +                * unnecessary. but our debugging feature checks it.
24524 +                */
24525 +               root = a->src_inode->i_sb->s_root;
24526 +               if (root != a->src_parent && root != a->dst_parent) {
24527 +                       dir = d_inode(root);
24528 +                       ii_read_lock_parent3(dir);
24529 +                       a->h_root = au_hi(dir, a->btgt);
24530 +                       ii_read_unlock(dir);
24531 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24532 +               }
24533 +       }
24534 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24535 +                                     a->dst_h_parent, a->dst_hdir);
24536 +       if (IS_ERR(a->h_trap)) {
24537 +               err = PTR_ERR(a->h_trap);
24538 +               goto out;
24539 +       }
24540 +       udba = au_opt_udba(a->src_dentry->d_sb);
24541 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24542 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
24543 +               err = au_busy_or_stale();
24544 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
24545 +               err = au_h_verify(a->src_h_dentry, udba,
24546 +                                 d_inode(a->src_h_parent), a->src_h_parent,
24547 +                                 a->br);
24548 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
24549 +               err = au_h_verify(a->dst_h_dentry, udba,
24550 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
24551 +                                 a->br);
24552 +       if (!err)
24553 +               goto out; /* success */
24554 +
24555 +       err = au_busy_or_stale();
24556 +       au_ren_unlock(a);
24557 +
24558 +out:
24559 +       return err;
24560 +}
24561 +
24562 +/* ---------------------------------------------------------------------- */
24563 +
24564 +static void au_ren_refresh_dir(struct au_ren_args *a)
24565 +{
24566 +       struct inode *dir;
24567 +
24568 +       dir = a->dst_dir;
24569 +       inode_inc_iversion(dir);
24570 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24571 +               /* is this updating defined in POSIX? */
24572 +               au_cpup_attr_timesizes(a->src_inode);
24573 +               au_cpup_attr_nlink(dir, /*force*/1);
24574 +       }
24575 +       au_dir_ts(dir, a->btgt);
24576 +
24577 +       if (a->exchange) {
24578 +               dir = a->src_dir;
24579 +               inode_inc_iversion(dir);
24580 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24581 +                       /* is this updating defined in POSIX? */
24582 +                       au_cpup_attr_timesizes(a->dst_inode);
24583 +                       au_cpup_attr_nlink(dir, /*force*/1);
24584 +               }
24585 +               au_dir_ts(dir, a->btgt);
24586 +       }
24587 +
24588 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24589 +               return;
24590 +
24591 +       dir = a->src_dir;
24592 +       inode_inc_iversion(dir);
24593 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
24594 +               au_cpup_attr_nlink(dir, /*force*/1);
24595 +       au_dir_ts(dir, a->btgt);
24596 +}
24597 +
24598 +static void au_ren_refresh(struct au_ren_args *a)
24599 +{
24600 +       aufs_bindex_t bbot, bindex;
24601 +       struct dentry *d, *h_d;
24602 +       struct inode *i, *h_i;
24603 +       struct super_block *sb;
24604 +
24605 +       d = a->dst_dentry;
24606 +       d_drop(d);
24607 +       if (a->h_dst)
24608 +               /* already dget-ed by au_ren_or_cpup() */
24609 +               au_set_h_dptr(d, a->btgt, a->h_dst);
24610 +
24611 +       i = a->dst_inode;
24612 +       if (i) {
24613 +               if (!a->exchange) {
24614 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24615 +                               vfsub_drop_nlink(i);
24616 +                       else {
24617 +                               vfsub_dead_dir(i);
24618 +                               au_cpup_attr_timesizes(i);
24619 +                       }
24620 +                       au_update_dbrange(d, /*do_put_zero*/1);
24621 +               } else
24622 +                       au_cpup_attr_nlink(i, /*force*/1);
24623 +       } else {
24624 +               bbot = a->btgt;
24625 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
24626 +                       au_set_h_dptr(d, bindex, NULL);
24627 +               bbot = au_dbbot(d);
24628 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
24629 +                       au_set_h_dptr(d, bindex, NULL);
24630 +               au_update_dbrange(d, /*do_put_zero*/0);
24631 +       }
24632 +
24633 +       if (a->exchange
24634 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
24635 +               d_drop(a->src_dentry);
24636 +               if (au_ftest_ren(a->auren_flags, DIRREN))
24637 +                       au_set_dbwh(a->src_dentry, -1);
24638 +               return;
24639 +       }
24640 +
24641 +       d = a->src_dentry;
24642 +       au_set_dbwh(d, -1);
24643 +       bbot = au_dbbot(d);
24644 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24645 +               h_d = au_h_dptr(d, bindex);
24646 +               if (h_d)
24647 +                       au_set_h_dptr(d, bindex, NULL);
24648 +       }
24649 +       au_set_dbbot(d, a->btgt);
24650 +
24651 +       sb = d->d_sb;
24652 +       i = a->src_inode;
24653 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24654 +               return; /* success */
24655 +
24656 +       bbot = au_ibbot(i);
24657 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24658 +               h_i = au_h_iptr(i, bindex);
24659 +               if (h_i) {
24660 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24661 +                       /* ignore this error */
24662 +                       au_set_h_iptr(i, bindex, NULL, 0);
24663 +               }
24664 +       }
24665 +       au_set_ibbot(i, a->btgt);
24666 +}
24667 +
24668 +/* ---------------------------------------------------------------------- */
24669 +
24670 +/* mainly for link(2) and rename(2) */
24671 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
24672 +{
24673 +       aufs_bindex_t bdiropq, bwh;
24674 +       struct dentry *parent;
24675 +       struct au_branch *br;
24676 +
24677 +       parent = dentry->d_parent;
24678 +       IMustLock(d_inode(parent)); /* dir is locked */
24679 +
24680 +       bdiropq = au_dbdiropq(parent);
24681 +       bwh = au_dbwh(dentry);
24682 +       br = au_sbr(dentry->d_sb, btgt);
24683 +       if (au_br_rdonly(br)
24684 +           || (0 <= bdiropq && bdiropq < btgt)
24685 +           || (0 <= bwh && bwh < btgt))
24686 +               btgt = -1;
24687 +
24688 +       AuDbg("btgt %d\n", btgt);
24689 +       return btgt;
24690 +}
24691 +
24692 +/* sets src_btop, dst_btop and btgt */
24693 +static int au_ren_wbr(struct au_ren_args *a)
24694 +{
24695 +       int err;
24696 +       struct au_wr_dir_args wr_dir_args = {
24697 +               /* .force_btgt  = -1, */
24698 +               .flags          = AuWrDir_ADD_ENTRY
24699 +       };
24700 +
24701 +       a->src_btop = au_dbtop(a->src_dentry);
24702 +       a->dst_btop = au_dbtop(a->dst_dentry);
24703 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24704 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
24705 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
24706 +       wr_dir_args.force_btgt = a->src_btop;
24707 +       if (a->dst_inode && a->dst_btop < a->src_btop)
24708 +               wr_dir_args.force_btgt = a->dst_btop;
24709 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24710 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24711 +       a->btgt = err;
24712 +       if (a->exchange)
24713 +               au_update_dbtop(a->dst_dentry);
24714 +
24715 +       return err;
24716 +}
24717 +
24718 +static void au_ren_dt(struct au_ren_args *a)
24719 +{
24720 +       a->h_path.dentry = a->src_h_parent;
24721 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
24722 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
24723 +               a->h_path.dentry = a->dst_h_parent;
24724 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24725 +       }
24726 +
24727 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
24728 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24729 +           && !a->exchange)
24730 +               return;
24731 +
24732 +       a->h_path.dentry = a->src_h_dentry;
24733 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
24734 +       if (d_is_positive(a->dst_h_dentry)) {
24735 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
24736 +               a->h_path.dentry = a->dst_h_dentry;
24737 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24738 +       }
24739 +}
24740 +
24741 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
24742 +{
24743 +       struct dentry *h_d;
24744 +       struct inode *h_inode;
24745 +
24746 +       au_dtime_revert(a->src_dt + AuPARENT);
24747 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
24748 +               au_dtime_revert(a->dst_dt + AuPARENT);
24749 +
24750 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
24751 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
24752 +               h_inode = d_inode(h_d);
24753 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
24754 +               au_dtime_revert(a->src_dt + AuCHILD);
24755 +               inode_unlock(h_inode);
24756 +
24757 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
24758 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
24759 +                       h_inode = d_inode(h_d);
24760 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
24761 +                       au_dtime_revert(a->dst_dt + AuCHILD);
24762 +                       inode_unlock(h_inode);
24763 +               }
24764 +       }
24765 +}
24766 +
24767 +/* ---------------------------------------------------------------------- */
24768 +
24769 +int aufs_rename(struct mnt_idmap *idmap,
24770 +               struct inode *_src_dir, struct dentry *_src_dentry,
24771 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
24772 +               unsigned int _flags)
24773 +{
24774 +       int err, lock_flags;
24775 +       void *rev;
24776 +       /* reduce stack space */
24777 +       struct au_ren_args *a;
24778 +       struct au_pin pin;
24779 +
24780 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
24781 +       IMustLock(_src_dir);
24782 +       IMustLock(_dst_dir);
24783 +
24784 +       err = -EINVAL;
24785 +       if (unlikely(_flags & RENAME_WHITEOUT))
24786 +               goto out;
24787 +
24788 +       err = -ENOMEM;
24789 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24790 +       a = kzalloc(sizeof(*a), GFP_NOFS);
24791 +       if (unlikely(!a))
24792 +               goto out;
24793 +
24794 +       a->flags = _flags;
24795 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24796 +                    && RENAME_EXCHANGE > U8_MAX);
24797 +       a->exchange = _flags & RENAME_EXCHANGE;
24798 +       a->src_dir = _src_dir;
24799 +       a->src_dentry = _src_dentry;
24800 +       a->src_inode = NULL;
24801 +       if (d_really_is_positive(a->src_dentry))
24802 +               a->src_inode = d_inode(a->src_dentry);
24803 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24804 +       a->dst_dir = _dst_dir;
24805 +       a->dst_dentry = _dst_dentry;
24806 +       a->dst_inode = NULL;
24807 +       if (d_really_is_positive(a->dst_dentry))
24808 +               a->dst_inode = d_inode(a->dst_dentry);
24809 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24810 +       if (a->dst_inode) {
24811 +               /*
24812 +                * if EXCHANGE && src is non-dir && dst is dir,
24813 +                * dst is not locked.
24814 +                */
24815 +               /* IMustLock(a->dst_inode); */
24816 +               au_igrab(a->dst_inode);
24817 +       }
24818 +
24819 +       err = -ENOTDIR;
24820 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
24821 +       if (d_is_dir(a->src_dentry)) {
24822 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
24823 +               if (unlikely(!a->exchange
24824 +                            && d_really_is_positive(a->dst_dentry)
24825 +                            && !d_is_dir(a->dst_dentry)))
24826 +                       goto out_free;
24827 +               lock_flags |= AuLock_DIRS;
24828 +       }
24829 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24830 +               au_fset_ren(a->auren_flags, ISDIR_DST);
24831 +               if (unlikely(!a->exchange
24832 +                            && d_really_is_positive(a->src_dentry)
24833 +                            && !d_is_dir(a->src_dentry)))
24834 +                       goto out_free;
24835 +               lock_flags |= AuLock_DIRS;
24836 +       }
24837 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24838 +                                       lock_flags);
24839 +       if (unlikely(err))
24840 +               goto out_free;
24841 +
24842 +       err = au_d_hashed_positive(a->src_dentry);
24843 +       if (unlikely(err))
24844 +               goto out_unlock;
24845 +       err = -ENOENT;
24846 +       if (a->dst_inode) {
24847 +               /*
24848 +                * If it is a dir, VFS unhash it before this
24849 +                * function. It means we cannot rely upon d_unhashed().
24850 +                */
24851 +               if (unlikely(!a->dst_inode->i_nlink))
24852 +                       goto out_unlock;
24853 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24854 +                       err = au_d_hashed_positive(a->dst_dentry);
24855 +                       if (unlikely(err && !a->exchange))
24856 +                               goto out_unlock;
24857 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24858 +                       goto out_unlock;
24859 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
24860 +               goto out_unlock;
24861 +
24862 +       /*
24863 +        * is it possible?
24864 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
24865 +        * there may exist a problem somewhere else.
24866 +        */
24867 +       err = -EINVAL;
24868 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
24869 +               goto out_unlock;
24870 +
24871 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
24872 +       di_write_lock_parent(a->dst_parent);
24873 +
24874 +       /* which branch we process */
24875 +       err = au_ren_wbr(a);
24876 +       if (unlikely(err < 0))
24877 +               goto out_parent;
24878 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
24879 +       a->h_path.mnt = au_br_mnt(a->br);
24880 +
24881 +       /* are they available to be renamed */
24882 +       err = au_ren_may_dir(a);
24883 +       if (unlikely(err))
24884 +               goto out_children;
24885 +
24886 +       /* prepare the writable parent dir on the same branch */
24887 +       if (a->dst_btop == a->btgt) {
24888 +               au_fset_ren(a->auren_flags, WHDST);
24889 +       } else {
24890 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
24891 +               if (unlikely(err))
24892 +                       goto out_children;
24893 +       }
24894 +
24895 +       err = 0;
24896 +       if (!a->exchange) {
24897 +               if (a->src_dir != a->dst_dir) {
24898 +                       /*
24899 +                        * this temporary unlock is safe,
24900 +                        * because both dir->i_mutex are locked.
24901 +                        */
24902 +                       di_write_unlock(a->dst_parent);
24903 +                       di_write_lock_parent(a->src_parent);
24904 +                       err = au_wr_dir_need_wh(a->src_dentry,
24905 +                                               au_ftest_ren(a->auren_flags,
24906 +                                                            ISDIR_SRC),
24907 +                                               &a->btgt);
24908 +                       di_write_unlock(a->src_parent);
24909 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
24910 +                                             /*isdir*/1);
24911 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
24912 +               } else
24913 +                       err = au_wr_dir_need_wh(a->src_dentry,
24914 +                                               au_ftest_ren(a->auren_flags,
24915 +                                                            ISDIR_SRC),
24916 +                                               &a->btgt);
24917 +       }
24918 +       if (unlikely(err < 0))
24919 +               goto out_children;
24920 +       if (err)
24921 +               au_fset_ren(a->auren_flags, WHSRC);
24922 +
24923 +       /* cpup src */
24924 +       if (a->src_btop != a->btgt) {
24925 +               err = au_pin(&pin, a->src_dentry, a->btgt,
24926 +                            au_opt_udba(a->src_dentry->d_sb),
24927 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24928 +               if (!err) {
24929 +                       struct au_cp_generic cpg = {
24930 +                               .dentry = a->src_dentry,
24931 +                               .bdst   = a->btgt,
24932 +                               .bsrc   = a->src_btop,
24933 +                               .len    = -1,
24934 +                               .pin    = &pin,
24935 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24936 +                       };
24937 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
24938 +                       err = au_sio_cpup_simple(&cpg);
24939 +                       au_unpin(&pin);
24940 +               }
24941 +               if (unlikely(err))
24942 +                       goto out_children;
24943 +               a->src_btop = a->btgt;
24944 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
24945 +               if (!a->exchange)
24946 +                       au_fset_ren(a->auren_flags, WHSRC);
24947 +       }
24948 +
24949 +       /* cpup dst */
24950 +       if (a->exchange && a->dst_inode
24951 +           && a->dst_btop != a->btgt) {
24952 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
24953 +                            au_opt_udba(a->dst_dentry->d_sb),
24954 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24955 +               if (!err) {
24956 +                       struct au_cp_generic cpg = {
24957 +                               .dentry = a->dst_dentry,
24958 +                               .bdst   = a->btgt,
24959 +                               .bsrc   = a->dst_btop,
24960 +                               .len    = -1,
24961 +                               .pin    = &pin,
24962 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24963 +                       };
24964 +                       err = au_sio_cpup_simple(&cpg);
24965 +                       au_unpin(&pin);
24966 +               }
24967 +               if (unlikely(err))
24968 +                       goto out_children;
24969 +               a->dst_btop = a->btgt;
24970 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
24971 +       }
24972 +
24973 +       /* lock them all */
24974 +       err = au_ren_lock(a);
24975 +       if (unlikely(err))
24976 +               /* leave the copied-up one */
24977 +               goto out_children;
24978 +
24979 +       if (!a->exchange) {
24980 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24981 +                       err = au_may_ren(a);
24982 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24983 +                       err = -ENAMETOOLONG;
24984 +               if (unlikely(err))
24985 +                       goto out_hdir;
24986 +       }
24987 +
24988 +       /* store timestamps to be revertible */
24989 +       au_ren_dt(a);
24990 +
24991 +       /* store dirren info */
24992 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24993 +               err = au_dr_rename(a->src_dentry, a->btgt,
24994 +                                  &a->dst_dentry->d_name, &rev);
24995 +               AuTraceErr(err);
24996 +               if (unlikely(err))
24997 +                       goto out_dt;
24998 +       }
24999 +
25000 +       /* here we go */
25001 +       err = do_rename(a);
25002 +       if (unlikely(err))
25003 +               goto out_dirren;
25004 +
25005 +       if (au_ftest_ren(a->auren_flags, DIRREN))
25006 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
25007 +
25008 +       /* update dir attributes */
25009 +       au_ren_refresh_dir(a);
25010 +
25011 +       /* dput/iput all lower dentries */
25012 +       au_ren_refresh(a);
25013 +
25014 +       goto out_hdir; /* success */
25015 +
25016 +out_dirren:
25017 +       if (au_ftest_ren(a->auren_flags, DIRREN))
25018 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
25019 +out_dt:
25020 +       au_ren_rev_dt(err, a);
25021 +out_hdir:
25022 +       au_ren_unlock(a);
25023 +out_children:
25024 +       au_nhash_wh_free(&a->whlist);
25025 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
25026 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
25027 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
25028 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
25029 +       }
25030 +out_parent:
25031 +       if (!err) {
25032 +               if (d_unhashed(a->src_dentry))
25033 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
25034 +               if (d_unhashed(a->dst_dentry))
25035 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
25036 +               if (!a->exchange)
25037 +                       d_move(a->src_dentry, a->dst_dentry);
25038 +               else {
25039 +                       d_exchange(a->src_dentry, a->dst_dentry);
25040 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
25041 +                               d_drop(a->dst_dentry);
25042 +               }
25043 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
25044 +                       d_drop(a->src_dentry);
25045 +       } else {
25046 +               au_update_dbtop(a->dst_dentry);
25047 +               if (!a->dst_inode)
25048 +                       d_drop(a->dst_dentry);
25049 +       }
25050 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
25051 +               di_write_unlock(a->dst_parent);
25052 +       else
25053 +               di_write_unlock2(a->src_parent, a->dst_parent);
25054 +out_unlock:
25055 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
25056 +out_free:
25057 +       iput(a->dst_inode);
25058 +       if (a->thargs)
25059 +               au_whtmp_rmdir_free(a->thargs);
25060 +       au_kfree_rcu(a);
25061 +out:
25062 +       AuTraceErr(err);
25063 +       return err;
25064 +}
25065 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
25066 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
25067 +++ linux/fs/aufs/Kconfig       2024-05-13 17:13:00.614737564 +0200
25068 @@ -0,0 +1,199 @@
25069 +# SPDX-License-Identifier: GPL-2.0
25070 +config AUFS_FS
25071 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
25072 +       help
25073 +       Aufs is a stackable unification filesystem such as Unionfs,
25074 +       which unifies several directories and provides a merged single
25075 +       directory.
25076 +       In the early days, aufs was entirely re-designed and
25077 +       re-implemented Unionfs Version 1.x series. Introducing many
25078 +       original ideas, approaches and improvements, it becomes totally
25079 +       different from Unionfs while keeping the basic features.
25080 +
25081 +if AUFS_FS
25082 +choice
25083 +       prompt "Maximum number of branches"
25084 +       default AUFS_BRANCH_MAX_127
25085 +       help
25086 +       Specifies the maximum number of branches (or member directories)
25087 +       in a single aufs. The larger value consumes more system
25088 +       resources and has a minor impact to performance.
25089 +config AUFS_BRANCH_MAX_127
25090 +       bool "127"
25091 +       help
25092 +       Specifies the maximum number of branches (or member directories)
25093 +       in a single aufs. The larger value consumes more system
25094 +       resources and has a minor impact to performance.
25095 +config AUFS_BRANCH_MAX_511
25096 +       bool "511"
25097 +       help
25098 +       Specifies the maximum number of branches (or member directories)
25099 +       in a single aufs. The larger value consumes more system
25100 +       resources and has a minor impact to performance.
25101 +config AUFS_BRANCH_MAX_1023
25102 +       bool "1023"
25103 +       help
25104 +       Specifies the maximum number of branches (or member directories)
25105 +       in a single aufs. The larger value consumes more system
25106 +       resources and has a minor impact to performance.
25107 +config AUFS_BRANCH_MAX_32767
25108 +       bool "32767"
25109 +       help
25110 +       Specifies the maximum number of branches (or member directories)
25111 +       in a single aufs. The larger value consumes more system
25112 +       resources and has a minor impact to performance.
25113 +endchoice
25114 +
25115 +config AUFS_SBILIST
25116 +       bool
25117 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
25118 +       default y
25119 +       help
25120 +       Automatic configuration for internal use.
25121 +       When aufs supports Magic SysRq or /proc, enabled automatically.
25122 +
25123 +config AUFS_HNOTIFY
25124 +       bool "Detect direct branch access (bypassing aufs)"
25125 +       help
25126 +       If you want to modify files on branches directly, eg. bypassing aufs,
25127 +       and want aufs to detect the changes of them fully, then enable this
25128 +       option and use 'udba=notify' mount option.
25129 +       Currently there is only one available configuration, "fsnotify".
25130 +       It will have a negative impact to the performance.
25131 +       See detail in aufs.5.
25132 +
25133 +choice
25134 +       prompt "method" if AUFS_HNOTIFY
25135 +       default AUFS_HFSNOTIFY
25136 +config AUFS_HFSNOTIFY
25137 +       bool "fsnotify"
25138 +       select FSNOTIFY
25139 +endchoice
25140 +
25141 +config AUFS_EXPORT
25142 +       bool "NFS-exportable aufs"
25143 +       depends on EXPORTFS
25144 +       help
25145 +       If you want to export your mounted aufs via NFS, then enable this
25146 +       option. There are several requirements for this configuration.
25147 +       See detail in aufs.5.
25148 +
25149 +config AUFS_INO_T_64
25150 +       bool
25151 +       depends on AUFS_EXPORT
25152 +       depends on 64BIT && !(ALPHA || S390)
25153 +       default y
25154 +       help
25155 +       Automatic configuration for internal use.
25156 +       /* typedef unsigned long/int __kernel_ino_t */
25157 +       /* alpha and s390x are int */
25158 +
25159 +config AUFS_XATTR
25160 +       bool "support for XATTR/EA (including Security Labels)"
25161 +       help
25162 +       If your branch fs supports XATTR/EA and you want to make them
25163 +       available in aufs too, then enable this opsion and specify the
25164 +       branch attributes for EA.
25165 +       See detail in aufs.5.
25166 +
25167 +config AUFS_FHSM
25168 +       bool "File-based Hierarchical Storage Management"
25169 +       help
25170 +       Hierarchical Storage Management (or HSM) is a well-known feature
25171 +       in the storage world. Aufs provides this feature as file-based.
25172 +       with multiple branches.
25173 +       These multiple branches are prioritized, ie. the topmost one
25174 +       should be the fastest drive and be used heavily.
25175 +
25176 +config AUFS_RDU
25177 +       bool "Readdir in userspace"
25178 +       help
25179 +       Aufs has two methods to provide a merged view for a directory,
25180 +       by a user-space library and by kernel-space natively. The latter
25181 +       is always enabled but sometimes large and slow.
25182 +       If you enable this option, install the library in aufs2-util
25183 +       package, and set some environment variables for your readdir(3),
25184 +       then the work will be handled in user-space which generally
25185 +       shows better performance in most cases.
25186 +       See detail in aufs.5.
25187 +
25188 +config AUFS_DIRREN
25189 +       bool "Workaround for rename(2)-ing a directory"
25190 +       help
25191 +       By default, aufs returns EXDEV error in renameing a dir who has
25192 +       his child on the lower branch, since it is a bad idea to issue
25193 +       rename(2) internally for every lower branch. But user may not
25194 +       accept this behaviour. So here is a workaround to allow such
25195 +       rename(2) and store some extra information on the writable
25196 +       branch. Obviously this costs high (and I don't like it).
25197 +       To use this feature, you need to enable this configuration AND
25198 +       to specify the mount option `dirren.'
25199 +       See details in aufs.5 and the design documents.
25200 +
25201 +config AUFS_SHWH
25202 +       bool "Show whiteouts"
25203 +       help
25204 +       If you want to make the whiteouts in aufs visible, then enable
25205 +       this option and specify 'shwh' mount option. Although it may
25206 +       sounds like philosophy or something, but in technically it
25207 +       simply shows the name of whiteout with keeping its behaviour.
25208 +
25209 +config AUFS_BR_RAMFS
25210 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
25211 +       help
25212 +       If you want to use ramfs as an aufs branch fs, then enable this
25213 +       option. Generally tmpfs is recommended.
25214 +       Aufs prohibited them to be a branch fs by default, because
25215 +       initramfs becomes unusable after switch_root or something
25216 +       generally. If you sets initramfs as an aufs branch and boot your
25217 +       system by switch_root, you will meet a problem easily since the
25218 +       files in initramfs may be inaccessible.
25219 +       Unless you are going to use ramfs as an aufs branch fs without
25220 +       switch_root or something, leave it N.
25221 +
25222 +config AUFS_BR_FUSE
25223 +       bool "Fuse fs as an aufs branch"
25224 +       depends on FUSE_FS
25225 +       select AUFS_POLL
25226 +       help
25227 +       If you want to use fuse-based userspace filesystem as an aufs
25228 +       branch fs, then enable this option.
25229 +       It implements the internal poll(2) operation which is
25230 +       implemented by fuse only (curretnly).
25231 +
25232 +config AUFS_POLL
25233 +       bool
25234 +       help
25235 +       Automatic configuration for internal use.
25236 +
25237 +config AUFS_BR_HFSPLUS
25238 +       bool "Hfsplus as an aufs branch"
25239 +       depends on HFSPLUS_FS
25240 +       default y
25241 +       help
25242 +       If you want to use hfsplus fs as an aufs branch fs, then enable
25243 +       this option. This option introduces a small overhead at
25244 +       copying-up a file on hfsplus.
25245 +
25246 +config AUFS_BDEV_LOOP
25247 +       bool
25248 +       depends on BLK_DEV_LOOP
25249 +       default y
25250 +       help
25251 +       Automatic configuration for internal use.
25252 +       Convert =[ym] into =y.
25253 +
25254 +config AUFS_DEBUG
25255 +       bool "Debug aufs"
25256 +       help
25257 +       Enable this to compile aufs internal debug code.
25258 +       It will have a negative impact to the performance.
25259 +
25260 +config AUFS_MAGIC_SYSRQ
25261 +       bool
25262 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
25263 +       default y
25264 +       help
25265 +       Automatic configuration for internal use.
25266 +       When aufs supports Magic SysRq, enabled automatically.
25267 +endif
25268 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
25269 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
25270 +++ linux/fs/aufs/lcnt.h        2024-05-13 17:13:00.621404284 +0200
25271 @@ -0,0 +1,186 @@
25272 +/* SPDX-License-Identifier: GPL-2.0 */
25273 +/*
25274 + * Copyright (C) 2018-2022 Junjiro R. Okajima
25275 + *
25276 + * This program is free software; you can redistribute it and/or modify
25277 + * it under the terms of the GNU General Public License as published by
25278 + * the Free Software Foundation; either version 2 of the License, or
25279 + * (at your option) any later version.
25280 + *
25281 + * This program is distributed in the hope that it will be useful,
25282 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25283 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25284 + * GNU General Public License for more details.
25285 + *
25286 + * You should have received a copy of the GNU General Public License
25287 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25288 + */
25289 +
25290 +/*
25291 + * simple long counter wrapper
25292 + */
25293 +
25294 +#ifndef __AUFS_LCNT_H__
25295 +#define __AUFS_LCNT_H__
25296 +
25297 +#ifdef __KERNEL__
25298 +
25299 +#include "debug.h"
25300 +
25301 +#define AuLCntATOMIC   1
25302 +#define AuLCntPCPUCNT  2
25303 +/*
25304 + * why does percpu_refcount require extra synchronize_rcu()s in
25305 + * au_br_do_free()
25306 + */
25307 +#define AuLCntPCPUREF  3
25308 +
25309 +/* #define AuLCntChosen        AuLCntATOMIC */
25310 +#define AuLCntChosen   AuLCntPCPUCNT
25311 +/* #define AuLCntChosen        AuLCntPCPUREF */
25312 +
25313 +#if AuLCntChosen == AuLCntATOMIC
25314 +#include <linux/atomic.h>
25315 +
25316 +typedef atomic_long_t au_lcnt_t;
25317 +
25318 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25319 +{
25320 +       atomic_long_set(cnt, 0);
25321 +       return 0;
25322 +}
25323 +
25324 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25325 +{
25326 +       /* empty */
25327 +}
25328 +
25329 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25330 +                              int do_sync __maybe_unused)
25331 +{
25332 +       /* empty */
25333 +}
25334 +
25335 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25336 +{
25337 +       atomic_long_inc(cnt);
25338 +}
25339 +
25340 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25341 +{
25342 +       atomic_long_dec(cnt);
25343 +}
25344 +
25345 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25346 +{
25347 +       return atomic_long_read(cnt);
25348 +}
25349 +#endif
25350 +
25351 +#if AuLCntChosen == AuLCntPCPUCNT
25352 +#include <linux/percpu_counter.h>
25353 +
25354 +typedef struct percpu_counter au_lcnt_t;
25355 +
25356 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25357 +{
25358 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
25359 +}
25360 +
25361 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25362 +{
25363 +       /* empty */
25364 +}
25365 +
25366 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25367 +{
25368 +       percpu_counter_destroy(cnt);
25369 +}
25370 +
25371 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25372 +{
25373 +       percpu_counter_inc(cnt);
25374 +}
25375 +
25376 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25377 +{
25378 +       percpu_counter_dec(cnt);
25379 +}
25380 +
25381 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25382 +{
25383 +       s64 n;
25384 +
25385 +       n = percpu_counter_sum(cnt);
25386 +       BUG_ON(n < 0);
25387 +       if (LONG_MAX != LLONG_MAX
25388 +           && n > LONG_MAX)
25389 +               AuWarn1("%s\n", "wrap-around");
25390 +
25391 +       return n;
25392 +}
25393 +#endif
25394 +
25395 +#if AuLCntChosen == AuLCntPCPUREF
25396 +#include <linux/percpu-refcount.h>
25397 +
25398 +typedef struct percpu_ref au_lcnt_t;
25399 +
25400 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25401 +{
25402 +       if (!release)
25403 +               release = percpu_ref_exit;
25404 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25405 +}
25406 +
25407 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25408 +{
25409 +       synchronize_rcu();
25410 +}
25411 +
25412 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25413 +{
25414 +       percpu_ref_kill(cnt);
25415 +       if (do_sync)
25416 +               au_lcnt_wait_for_fin(cnt);
25417 +}
25418 +
25419 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25420 +{
25421 +       percpu_ref_get(cnt);
25422 +}
25423 +
25424 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25425 +{
25426 +       percpu_ref_put(cnt);
25427 +}
25428 +
25429 +/*
25430 + * avoid calling this func as possible.
25431 + */
25432 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25433 +{
25434 +       long l;
25435 +
25436 +       percpu_ref_switch_to_atomic_sync(cnt);
25437 +       l = atomic_long_read(&cnt->count);
25438 +       if (do_rev)
25439 +               percpu_ref_switch_to_percpu(cnt);
25440 +
25441 +       /* percpu_ref is initialized by 1 instead of 0 */
25442 +       return l - 1;
25443 +}
25444 +#endif
25445 +
25446 +#ifdef CONFIG_AUFS_DEBUG
25447 +#define AuLCntZero(val) do {                   \
25448 +       long l = val;                           \
25449 +       if (l)                                  \
25450 +               AuDbg("%s = %ld\n", #val, l);   \
25451 +} while (0)
25452 +#else
25453 +#define AuLCntZero(val)                do {} while (0)
25454 +#endif
25455 +
25456 +#endif /* __KERNEL__ */
25457 +#endif /* __AUFS_LCNT_H__ */
25458 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
25459 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
25460 +++ linux/fs/aufs/loop.c        2024-05-13 17:13:00.621404284 +0200
25461 @@ -0,0 +1,148 @@
25462 +// SPDX-License-Identifier: GPL-2.0
25463 +/*
25464 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25465 + *
25466 + * This program is free software; you can redistribute it and/or modify
25467 + * it under the terms of the GNU General Public License as published by
25468 + * the Free Software Foundation; either version 2 of the License, or
25469 + * (at your option) any later version.
25470 + *
25471 + * This program is distributed in the hope that it will be useful,
25472 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25473 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25474 + * GNU General Public License for more details.
25475 + *
25476 + * You should have received a copy of the GNU General Public License
25477 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25478 + */
25479 +
25480 +/*
25481 + * support for loopback block device as a branch
25482 + */
25483 +
25484 +#include "aufs.h"
25485 +
25486 +/* added into drivers/block/loop.c */
25487 +static struct file *(*backing_file_func)(struct super_block *sb);
25488 +
25489 +/*
25490 + * test if two lower dentries have overlapping branches.
25491 + */
25492 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25493 +{
25494 +       struct super_block *h_sb;
25495 +       struct file *backing_file;
25496 +
25497 +       if (unlikely(!backing_file_func)) {
25498 +               /* don't load "loop" module here */
25499 +               backing_file_func = symbol_get(loop_backing_file);
25500 +               if (unlikely(!backing_file_func))
25501 +                       /* "loop" module is not loaded */
25502 +                       return 0;
25503 +       }
25504 +
25505 +       h_sb = h_adding->d_sb;
25506 +       backing_file = backing_file_func(h_sb);
25507 +       if (!backing_file)
25508 +               return 0;
25509 +
25510 +       h_adding = backing_file->f_path.dentry;
25511 +       /*
25512 +        * h_adding can be local NFS.
25513 +        * in this case aufs cannot detect the loop.
25514 +        */
25515 +       if (unlikely(h_adding->d_sb == sb))
25516 +               return 1;
25517 +       return !!au_test_subdir(h_adding, sb->s_root);
25518 +}
25519 +
25520 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25521 +int au_test_loopback_kthread(void)
25522 +{
25523 +       int ret;
25524 +       struct task_struct *tsk = current;
25525 +       char c, comm[sizeof(tsk->comm)];
25526 +
25527 +       ret = 0;
25528 +       if (tsk->flags & PF_KTHREAD) {
25529 +               get_task_comm(comm, tsk);
25530 +               c = comm[4];
25531 +               ret = ('0' <= c && c <= '9'
25532 +                      && !strncmp(comm, "loop", 4));
25533 +       }
25534 +
25535 +       return ret;
25536 +}
25537 +
25538 +/* ---------------------------------------------------------------------- */
25539 +
25540 +#define au_warn_loopback_step  16
25541 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25542 +static unsigned long *au_warn_loopback_array;
25543 +
25544 +void au_warn_loopback(struct super_block *h_sb)
25545 +{
25546 +       int i, new_nelem;
25547 +       unsigned long *a, magic;
25548 +       static DEFINE_SPINLOCK(spin);
25549 +
25550 +       magic = h_sb->s_magic;
25551 +       spin_lock(&spin);
25552 +       a = au_warn_loopback_array;
25553 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25554 +               if (a[i] == magic) {
25555 +                       spin_unlock(&spin);
25556 +                       return;
25557 +               }
25558 +
25559 +       /* h_sb is new to us, print it */
25560 +       if (i < au_warn_loopback_nelem) {
25561 +               a[i] = magic;
25562 +               goto pr;
25563 +       }
25564 +
25565 +       /* expand the array */
25566 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25567 +       a = au_kzrealloc(au_warn_loopback_array,
25568 +                        au_warn_loopback_nelem * sizeof(unsigned long),
25569 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25570 +                        /*may_shrink*/0);
25571 +       if (a) {
25572 +               au_warn_loopback_nelem = new_nelem;
25573 +               au_warn_loopback_array = a;
25574 +               a[i] = magic;
25575 +               goto pr;
25576 +       }
25577 +
25578 +       spin_unlock(&spin);
25579 +       AuWarn1("realloc failed, ignored\n");
25580 +       return;
25581 +
25582 +pr:
25583 +       spin_unlock(&spin);
25584 +       pr_warn("you may want to try another patch for loopback file "
25585 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25586 +}
25587 +
25588 +int au_loopback_init(void)
25589 +{
25590 +       int err;
25591 +       struct super_block *sb __maybe_unused;
25592 +
25593 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25594 +
25595 +       err = 0;
25596 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25597 +                                        sizeof(unsigned long), GFP_NOFS);
25598 +       if (unlikely(!au_warn_loopback_array))
25599 +               err = -ENOMEM;
25600 +
25601 +       return err;
25602 +}
25603 +
25604 +void au_loopback_fin(void)
25605 +{
25606 +       if (backing_file_func)
25607 +               symbol_put(loop_backing_file);
25608 +       au_kfree_try_rcu(au_warn_loopback_array);
25609 +}
25610 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
25611 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
25612 +++ linux/fs/aufs/loop.h        2024-05-13 17:13:00.621404284 +0200
25613 @@ -0,0 +1,55 @@
25614 +/* SPDX-License-Identifier: GPL-2.0 */
25615 +/*
25616 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25617 + *
25618 + * This program is free software; you can redistribute it and/or modify
25619 + * it under the terms of the GNU General Public License as published by
25620 + * the Free Software Foundation; either version 2 of the License, or
25621 + * (at your option) any later version.
25622 + *
25623 + * This program is distributed in the hope that it will be useful,
25624 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25625 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25626 + * GNU General Public License for more details.
25627 + *
25628 + * You should have received a copy of the GNU General Public License
25629 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25630 + */
25631 +
25632 +/*
25633 + * support for loopback mount as a branch
25634 + */
25635 +
25636 +#ifndef __AUFS_LOOP_H__
25637 +#define __AUFS_LOOP_H__
25638 +
25639 +#ifdef __KERNEL__
25640 +
25641 +struct dentry;
25642 +struct super_block;
25643 +
25644 +#ifdef CONFIG_AUFS_BDEV_LOOP
25645 +/* drivers/block/loop.c */
25646 +struct file *loop_backing_file(struct super_block *sb);
25647 +
25648 +/* loop.c */
25649 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25650 +int au_test_loopback_kthread(void);
25651 +void au_warn_loopback(struct super_block *h_sb);
25652 +
25653 +int au_loopback_init(void);
25654 +void au_loopback_fin(void);
25655 +#else
25656 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25657 +
25658 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25659 +          struct dentry *h_adding)
25660 +AuStubInt0(au_test_loopback_kthread, void)
25661 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25662 +
25663 +AuStubInt0(au_loopback_init, void)
25664 +AuStubVoid(au_loopback_fin, void)
25665 +#endif /* BLK_DEV_LOOP */
25666 +
25667 +#endif /* __KERNEL__ */
25668 +#endif /* __AUFS_LOOP_H__ */
25669 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
25670 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
25671 +++ linux/fs/aufs/magic.mk      2024-05-13 17:13:00.621404284 +0200
25672 @@ -0,0 +1,31 @@
25673 +# SPDX-License-Identifier: GPL-2.0
25674 +
25675 +# defined in ${srctree}/fs/fuse/inode.c
25676 +# tristate
25677 +ifdef CONFIG_FUSE_FS
25678 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25679 +endif
25680 +
25681 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25682 +# tristate
25683 +ifdef CONFIG_XFS_FS
25684 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25685 +endif
25686 +
25687 +# defined in ${srctree}/fs/configfs/mount.c
25688 +# tristate
25689 +ifdef CONFIG_CONFIGFS_FS
25690 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25691 +endif
25692 +
25693 +# defined in ${srctree}/fs/ubifs/ubifs.h
25694 +# tristate
25695 +ifdef CONFIG_UBIFS_FS
25696 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25697 +endif
25698 +
25699 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25700 +# tristate
25701 +ifdef CONFIG_HFSPLUS_FS
25702 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25703 +endif
25704 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
25705 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
25706 +++ linux/fs/aufs/Makefile      2024-05-13 17:13:00.614737564 +0200
25707 @@ -0,0 +1,46 @@
25708 +# SPDX-License-Identifier: GPL-2.0
25709 +
25710 +include ${src}/magic.mk
25711 +ifeq (${CONFIG_AUFS_FS},m)
25712 +include ${src}/conf.mk
25713 +endif
25714 +-include ${src}/priv_def.mk
25715 +
25716 +# cf. include/linux/kernel.h
25717 +# enable pr_debug
25718 +ccflags-y += -DDEBUG
25719 +# sparse requires the full pathname
25720 +ifdef M
25721 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
25722 +else
25723 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
25724 +endif
25725 +
25726 +obj-$(CONFIG_AUFS_FS) += aufs.o
25727 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
25728 +       wkq.o vfsub.o dcsub.o \
25729 +       cpup.o whout.o wbr_policy.o \
25730 +       dinfo.o dentry.o \
25731 +       dynop.o \
25732 +       finfo.o file.o f_op.o \
25733 +       dir.o vdir.o \
25734 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
25735 +       mvdown.o ioctl.o
25736 +
25737 +# all are boolean
25738 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
25739 +aufs-$(CONFIG_SYSFS) += sysfs.o
25740 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25741 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25742 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25743 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
25744 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
25745 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25746 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
25747 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
25748 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
25749 +aufs-$(CONFIG_AUFS_POLL) += poll.o
25750 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
25751 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25752 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25753 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
25754 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
25755 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
25756 +++ linux/fs/aufs/module.c      2024-05-13 17:13:00.621404284 +0200
25757 @@ -0,0 +1,273 @@
25758 +// SPDX-License-Identifier: GPL-2.0
25759 +/*
25760 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25761 + *
25762 + * This program is free software; you can redistribute it and/or modify
25763 + * it under the terms of the GNU General Public License as published by
25764 + * the Free Software Foundation; either version 2 of the License, or
25765 + * (at your option) any later version.
25766 + *
25767 + * This program is distributed in the hope that it will be useful,
25768 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25769 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25770 + * GNU General Public License for more details.
25771 + *
25772 + * You should have received a copy of the GNU General Public License
25773 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25774 + */
25775 +
25776 +/*
25777 + * module global variables and operations
25778 + */
25779 +
25780 +#include <linux/module.h>
25781 +#include <linux/seq_file.h>
25782 +#include "aufs.h"
25783 +
25784 +/* shrinkable realloc */
25785 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25786 +{
25787 +       size_t sz;
25788 +       int diff;
25789 +
25790 +       sz = 0;
25791 +       diff = -1;
25792 +       if (p) {
25793 +#if 0 /* unused */
25794 +               if (!new_sz) {
25795 +                       au_kfree_rcu(p);
25796 +                       p = NULL;
25797 +                       goto out;
25798 +               }
25799 +#else
25800 +               AuDebugOn(!new_sz);
25801 +#endif
25802 +               sz = ksize(p);
25803 +               diff = au_kmidx_sub(sz, new_sz);
25804 +       }
25805 +       if (sz && !diff)
25806 +               goto out;
25807 +
25808 +       if (sz < new_sz)
25809 +               /* expand or SLOB */
25810 +               p = krealloc(p, new_sz, gfp);
25811 +       else if (new_sz < sz && may_shrink) {
25812 +               /* shrink */
25813 +               void *q;
25814 +
25815 +               q = kmalloc(new_sz, gfp);
25816 +               if (q) {
25817 +                       if (p) {
25818 +                               memcpy(q, p, new_sz);
25819 +                               au_kfree_try_rcu(p);
25820 +                       }
25821 +                       p = q;
25822 +               } else
25823 +                       p = NULL;
25824 +       }
25825 +
25826 +out:
25827 +       return p;
25828 +}
25829 +
25830 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25831 +                  int may_shrink)
25832 +{
25833 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
25834 +       if (p && new_sz > nused)
25835 +               memset(p + nused, 0, new_sz - nused);
25836 +       return p;
25837 +}
25838 +
25839 +/* ---------------------------------------------------------------------- */
25840 +/*
25841 + * aufs caches
25842 + */
25843 +struct kmem_cache *au_cache[AuCache_Last];
25844 +
25845 +static void au_cache_fin(void)
25846 +{
25847 +       int i;
25848 +
25849 +       /*
25850 +        * Make sure all delayed rcu free inodes are flushed before we
25851 +        * destroy cache.
25852 +        */
25853 +       rcu_barrier();
25854 +
25855 +       /* excluding AuCache_HNOTIFY */
25856 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25857 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
25858 +               kmem_cache_destroy(au_cache[i]);
25859 +               au_cache[i] = NULL;
25860 +       }
25861 +}
25862 +
25863 +static int __init au_cache_init(void)
25864 +{
25865 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25866 +       if (au_cache[AuCache_DINFO])
25867 +               /* SLAB_DESTROY_BY_RCU */
25868 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25869 +                                                      au_icntnr_init_once);
25870 +       if (au_cache[AuCache_ICNTNR])
25871 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25872 +                                                     au_fi_init_once);
25873 +       if (au_cache[AuCache_FINFO])
25874 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
25875 +       if (au_cache[AuCache_VDIR])
25876 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25877 +       if (au_cache[AuCache_DEHSTR])
25878 +               return 0;
25879 +
25880 +       au_cache_fin();
25881 +       return -ENOMEM;
25882 +}
25883 +
25884 +/* ---------------------------------------------------------------------- */
25885 +
25886 +int au_dir_roflags;
25887 +
25888 +#ifdef CONFIG_AUFS_SBILIST
25889 +/*
25890 + * iterate_supers_type() doesn't protect us from
25891 + * remounting (branch management)
25892 + */
25893 +struct hlist_bl_head au_sbilist;
25894 +#endif
25895 +
25896 +/*
25897 + * functions for module interface.
25898 + */
25899 +MODULE_LICENSE("GPL");
25900 +/* MODULE_LICENSE("GPL v2"); */
25901 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25902 +MODULE_DESCRIPTION(AUFS_NAME
25903 +       " -- Advanced multi layered unification filesystem");
25904 +MODULE_VERSION(AUFS_VERSION);
25905 +MODULE_ALIAS_FS(AUFS_NAME);
25906 +
25907 +/* this module parameter has no meaning when SYSFS is disabled */
25908 +int sysaufs_brs = 1;
25909 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25910 +module_param_named(brs, sysaufs_brs, int, 0444);
25911 +
25912 +/* this module parameter has no meaning when USER_NS is disabled */
25913 +bool au_userns;
25914 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25915 +module_param_named(allow_userns, au_userns, bool, 0444);
25916 +
25917 +/* ---------------------------------------------------------------------- */
25918 +
25919 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25920 +
25921 +int au_seq_path(struct seq_file *seq, struct path *path)
25922 +{
25923 +       int err;
25924 +
25925 +       err = seq_path(seq, path, au_esc_chars);
25926 +       if (err >= 0)
25927 +               err = 0;
25928 +       else
25929 +               err = -ENOMEM;
25930 +
25931 +       return err;
25932 +}
25933 +
25934 +/* ---------------------------------------------------------------------- */
25935 +
25936 +static int __init aufs_init(void)
25937 +{
25938 +       int err, i;
25939 +       char *p;
25940 +
25941 +       p = au_esc_chars;
25942 +       for (i = 1; i <= ' '; i++)
25943 +               *p++ = i;
25944 +       *p++ = '\\';
25945 +       *p++ = '\x7f';
25946 +       *p = 0;
25947 +
25948 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25949 +
25950 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25951 +       for (i = 0; i < AuIop_Last; i++)
25952 +               aufs_iop_nogetattr[i].getattr = NULL;
25953 +
25954 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
25955 +
25956 +       au_sbilist_init();
25957 +       sysaufs_brs_init();
25958 +       au_debug_init();
25959 +       au_dy_init();
25960 +       err = sysaufs_init();
25961 +       if (unlikely(err))
25962 +               goto out;
25963 +       err = dbgaufs_init();
25964 +       if (unlikely(err))
25965 +               goto out_sysaufs;
25966 +       err = au_procfs_init();
25967 +       if (unlikely(err))
25968 +               goto out_dbgaufs;
25969 +       err = au_wkq_init();
25970 +       if (unlikely(err))
25971 +               goto out_procfs;
25972 +       err = au_loopback_init();
25973 +       if (unlikely(err))
25974 +               goto out_wkq;
25975 +       err = au_hnotify_init();
25976 +       if (unlikely(err))
25977 +               goto out_loopback;
25978 +       err = au_sysrq_init();
25979 +       if (unlikely(err))
25980 +               goto out_hin;
25981 +       err = au_cache_init();
25982 +       if (unlikely(err))
25983 +               goto out_sysrq;
25984 +
25985 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25986 +       err = register_filesystem(&aufs_fs_type);
25987 +       if (unlikely(err))
25988 +               goto out_cache;
25989 +
25990 +       /* since we define pr_fmt, call printk directly */
25991 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25992 +       goto out; /* success */
25993 +
25994 +out_cache:
25995 +       au_cache_fin();
25996 +out_sysrq:
25997 +       au_sysrq_fin();
25998 +out_hin:
25999 +       au_hnotify_fin();
26000 +out_loopback:
26001 +       au_loopback_fin();
26002 +out_wkq:
26003 +       au_wkq_fin();
26004 +out_procfs:
26005 +       au_procfs_fin();
26006 +out_dbgaufs:
26007 +       dbgaufs_fin();
26008 +out_sysaufs:
26009 +       sysaufs_fin();
26010 +       au_dy_fin();
26011 +out:
26012 +       return err;
26013 +}
26014 +
26015 +static void __exit aufs_exit(void)
26016 +{
26017 +       unregister_filesystem(&aufs_fs_type);
26018 +       au_cache_fin();
26019 +       au_sysrq_fin();
26020 +       au_hnotify_fin();
26021 +       au_loopback_fin();
26022 +       au_wkq_fin();
26023 +       au_procfs_fin();
26024 +       dbgaufs_fin();
26025 +       sysaufs_fin();
26026 +       au_dy_fin();
26027 +}
26028 +
26029 +module_init(aufs_init);
26030 +module_exit(aufs_exit);
26031 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
26032 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
26033 +++ linux/fs/aufs/module.h      2024-05-13 17:13:00.621404284 +0200
26034 @@ -0,0 +1,180 @@
26035 +/* SPDX-License-Identifier: GPL-2.0 */
26036 +/*
26037 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26038 + *
26039 + * This program is free software; you can redistribute it and/or modify
26040 + * it under the terms of the GNU General Public License as published by
26041 + * the Free Software Foundation; either version 2 of the License, or
26042 + * (at your option) any later version.
26043 + *
26044 + * This program is distributed in the hope that it will be useful,
26045 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26046 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26047 + * GNU General Public License for more details.
26048 + *
26049 + * You should have received a copy of the GNU General Public License
26050 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26051 + */
26052 +
26053 +/*
26054 + * module initialization and module-global
26055 + */
26056 +
26057 +#ifndef __AUFS_MODULE_H__
26058 +#define __AUFS_MODULE_H__
26059 +
26060 +#ifdef __KERNEL__
26061 +
26062 +#include <linux/slab.h>
26063 +#include "debug.h"
26064 +#include "dentry.h"
26065 +#include "dir.h"
26066 +#include "file.h"
26067 +#include "inode.h"
26068 +
26069 +struct path;
26070 +struct seq_file;
26071 +
26072 +/* module parameters */
26073 +extern int sysaufs_brs;
26074 +extern bool au_userns;
26075 +
26076 +/* ---------------------------------------------------------------------- */
26077 +
26078 +extern int au_dir_roflags;
26079 +
26080 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
26081 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
26082 +                  int may_shrink);
26083 +
26084 +/*
26085 + * Comparing the size of the object with sizeof(struct rcu_head)
26086 + * case 1: object is always larger
26087 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
26088 + * case 2: object is always smaller
26089 + *     --> au_kfree_small()
26090 + * case 3: object can be any size
26091 + *     --> au_kfree_try_rcu()
26092 + */
26093 +
26094 +static inline void au_kfree_do_rcu(const void *p)
26095 +{
26096 +       struct {
26097 +               struct rcu_head rcu;
26098 +       } *a = (void *)p;
26099 +
26100 +       kfree_rcu(a, rcu);
26101 +}
26102 +
26103 +#define au_kfree_rcu(_p) do {                                          \
26104 +               typeof(_p) p = (_p);                                    \
26105 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
26106 +               if (p)                                                  \
26107 +                       au_kfree_do_rcu(p);                             \
26108 +       } while (0)
26109 +
26110 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
26111 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
26112 +
26113 +static inline void au_kfree_try_rcu(const void *p)
26114 +{
26115 +       if (!p)
26116 +               return;
26117 +       if (au_kfree_sz_test(p))
26118 +               au_kfree_do_rcu(p);
26119 +       else
26120 +               kfree(p);
26121 +}
26122 +
26123 +static inline void au_kfree_small(const void *p)
26124 +{
26125 +       if (!p)
26126 +               return;
26127 +       AuDebugOn(au_kfree_sz_test(p));
26128 +       kfree(p);
26129 +}
26130 +
26131 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26132 +{
26133 +#ifndef CONFIG_SLOB
26134 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
26135 +#else
26136 +       return -1; /* SLOB is untested */
26137 +#endif
26138 +}
26139 +
26140 +int au_seq_path(struct seq_file *seq, struct path *path);
26141 +
26142 +#ifdef CONFIG_PROC_FS
26143 +/* procfs.c */
26144 +int __init au_procfs_init(void);
26145 +void au_procfs_fin(void);
26146 +#else
26147 +AuStubInt0(au_procfs_init, void);
26148 +AuStubVoid(au_procfs_fin, void);
26149 +#endif
26150 +
26151 +/* ---------------------------------------------------------------------- */
26152 +
26153 +/* kmem cache */
26154 +enum {
26155 +       AuCache_DINFO,
26156 +       AuCache_ICNTNR,
26157 +       AuCache_FINFO,
26158 +       AuCache_VDIR,
26159 +       AuCache_DEHSTR,
26160 +       AuCache_HNOTIFY, /* must be last */
26161 +       AuCache_Last
26162 +};
26163 +
26164 +extern struct kmem_cache *au_cache[AuCache_Last];
26165 +
26166 +#define AuCacheFlags           SLAB_RECLAIM_ACCOUNT
26167 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
26168 +#define AuCacheCtor(type, ctor)        \
26169 +       kmem_cache_create(#type, sizeof(struct type), \
26170 +                         __alignof__(struct type), AuCacheFlags, ctor)
26171 +
26172 +#define AuCacheFuncAlloc(name, index)                                  \
26173 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
26174 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
26175 +
26176 +#define AuCacheFuncs(name, index)                                      \
26177 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26178 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
26179 +                                                                       \
26180 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26181 +       { void *p = rcu;                                                \
26182 +               p -= offsetof(struct au_##name, rcu);                   \
26183 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
26184 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26185 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26186 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
26187 +                                                                       \
26188 +       static inline void au_cache_free_##name(struct au_##name *p)    \
26189 +       { /* au_cache_free_##name##_norcu(p); */                        \
26190 +               au_cache_free_##name##_rcu(p); }
26191 +
26192 +AuCacheFuncs(dinfo, DINFO);
26193 +AuCacheFuncAlloc(dinfo, DINFO);
26194 +
26195 +AuCacheFuncs(icntnr, ICNTNR);
26196 +static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
26197 +{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
26198 +
26199 +AuCacheFuncs(finfo, FINFO);
26200 +AuCacheFuncAlloc(finfo, FINFO);
26201 +
26202 +AuCacheFuncs(vdir, VDIR);
26203 +AuCacheFuncAlloc(vdir, VDIR);
26204 +
26205 +AuCacheFuncs(vdir_dehstr, DEHSTR);
26206 +AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
26207 +
26208 +#ifdef CONFIG_AUFS_HNOTIFY
26209 +AuCacheFuncs(hnotify, HNOTIFY);
26210 +AuCacheFuncAlloc(hnotify, HNOTIFY);
26211 +#endif
26212 +
26213 +#endif /* __KERNEL__ */
26214 +#endif /* __AUFS_MODULE_H__ */
26215 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
26216 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
26217 +++ linux/fs/aufs/mvdown.c      2024-05-13 17:13:00.621404284 +0200
26218 @@ -0,0 +1,713 @@
26219 +// SPDX-License-Identifier: GPL-2.0
26220 +/*
26221 + * Copyright (C) 2011-2022 Junjiro R. Okajima
26222 + *
26223 + * This program is free software; you can redistribute it and/or modify
26224 + * it under the terms of the GNU General Public License as published by
26225 + * the Free Software Foundation; either version 2 of the License, or
26226 + * (at your option) any later version.
26227 + *
26228 + * This program is distributed in the hope that it will be useful,
26229 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26230 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26231 + * GNU General Public License for more details.
26232 + *
26233 + * You should have received a copy of the GNU General Public License
26234 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26235 + */
26236 +
26237 +/*
26238 + * move-down, opposite of copy-up
26239 + */
26240 +
26241 +#include "aufs.h"
26242 +
26243 +struct au_mvd_args {
26244 +       struct {
26245 +               struct super_block *h_sb;
26246 +               struct dentry *h_parent;
26247 +               struct au_hinode *hdir;
26248 +               struct inode *h_dir, *h_inode;
26249 +               struct au_pin pin;
26250 +       } info[AUFS_MVDOWN_NARRAY];
26251 +
26252 +       struct aufs_mvdown mvdown;
26253 +       struct dentry *dentry, *parent;
26254 +       struct inode *inode, *dir;
26255 +       struct super_block *sb;
26256 +       aufs_bindex_t bopq, bwh, bfound;
26257 +       unsigned char rename_lock;
26258 +};
26259 +
26260 +#define mvd_errno              mvdown.au_errno
26261 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26262 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26263 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26264 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
26265 +
26266 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
26267 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
26268 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
26269 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
26270 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
26271 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
26272 +
26273 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
26274 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
26275 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
26276 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
26277 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
26278 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
26279 +
26280 +#define AU_MVD_PR(flag, ...) do {                      \
26281 +               if (flag)                               \
26282 +                       pr_err(__VA_ARGS__);            \
26283 +       } while (0)
26284 +
26285 +static int find_lower_writable(struct au_mvd_args *a)
26286 +{
26287 +       struct super_block *sb;
26288 +       aufs_bindex_t bindex, bbot;
26289 +       struct au_branch *br;
26290 +
26291 +       sb = a->sb;
26292 +       bindex = a->mvd_bsrc;
26293 +       bbot = au_sbbot(sb);
26294 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
26295 +               for (bindex++; bindex <= bbot; bindex++) {
26296 +                       br = au_sbr(sb, bindex);
26297 +                       if (au_br_fhsm(br->br_perm)
26298 +                           && !sb_rdonly(au_br_sb(br)))
26299 +                               return bindex;
26300 +               }
26301 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
26302 +               for (bindex++; bindex <= bbot; bindex++) {
26303 +                       br = au_sbr(sb, bindex);
26304 +                       if (!au_br_rdonly(br))
26305 +                               return bindex;
26306 +               }
26307 +       else
26308 +               for (bindex++; bindex <= bbot; bindex++) {
26309 +                       br = au_sbr(sb, bindex);
26310 +                       if (!sb_rdonly(au_br_sb(br))) {
26311 +                               if (au_br_rdonly(br))
26312 +                                       a->mvdown.flags
26313 +                                               |= AUFS_MVDOWN_ROLOWER_R;
26314 +                               return bindex;
26315 +                       }
26316 +               }
26317 +
26318 +       return -1;
26319 +}
26320 +
26321 +/* make the parent dir on bdst */
26322 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
26323 +{
26324 +       int err;
26325 +
26326 +       err = 0;
26327 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26328 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26329 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26330 +       a->mvd_h_dst_parent = NULL;
26331 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
26332 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26333 +       if (!a->mvd_h_dst_parent) {
26334 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26335 +               if (unlikely(err)) {
26336 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
26337 +                       goto out;
26338 +               }
26339 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26340 +       }
26341 +
26342 +out:
26343 +       AuTraceErr(err);
26344 +       return err;
26345 +}
26346 +
26347 +/* lock them all */
26348 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
26349 +{
26350 +       int err;
26351 +       struct dentry *h_trap;
26352 +
26353 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26354 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
26355 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26356 +                    au_opt_udba(a->sb),
26357 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26358 +       AuTraceErr(err);
26359 +       if (unlikely(err)) {
26360 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
26361 +               goto out;
26362 +       }
26363 +
26364 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26365 +               a->rename_lock = 0;
26366 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26367 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26368 +                           au_opt_udba(a->sb),
26369 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26370 +               err = au_do_pin(&a->mvd_pin_src);
26371 +               AuTraceErr(err);
26372 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26373 +               if (unlikely(err)) {
26374 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
26375 +                       goto out_dst;
26376 +               }
26377 +               goto out; /* success */
26378 +       }
26379 +
26380 +       a->rename_lock = 1;
26381 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
26382 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26383 +                    au_opt_udba(a->sb),
26384 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26385 +       AuTraceErr(err);
26386 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26387 +       if (unlikely(err)) {
26388 +               AU_MVD_PR(dmsg, "pin_src failed\n");
26389 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26390 +               goto out_dst;
26391 +       }
26392 +       au_pin_hdir_unlock(&a->mvd_pin_src);
26393 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26394 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
26395 +       if (IS_ERR(h_trap)) {
26396 +               err = PTR_ERR(h_trap);
26397 +               au_pin_hdir_lock(&a->mvd_pin_src);
26398 +               au_unpin(&a->mvd_pin_src);
26399 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26400 +               goto out_dst;
26401 +       }
26402 +       if (h_trap) {
26403 +               err = (h_trap != a->mvd_h_src_parent);
26404 +               if (err)
26405 +                       err = (h_trap != a->mvd_h_dst_parent);
26406 +       }
26407 +       BUG_ON(err); /* it should never happen */
26408 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26409 +               err = -EBUSY;
26410 +               AuTraceErr(err);
26411 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26412 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26413 +               au_pin_hdir_lock(&a->mvd_pin_src);
26414 +               au_unpin(&a->mvd_pin_src);
26415 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26416 +               goto out_dst;
26417 +       }
26418 +       goto out; /* success */
26419 +
26420 +out_dst:
26421 +       au_unpin(&a->mvd_pin_dst);
26422 +out:
26423 +       AuTraceErr(err);
26424 +       return err;
26425 +}
26426 +
26427 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
26428 +{
26429 +       if (!a->rename_lock)
26430 +               au_unpin(&a->mvd_pin_src);
26431 +       else {
26432 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26433 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26434 +               au_pin_hdir_lock(&a->mvd_pin_src);
26435 +               au_unpin(&a->mvd_pin_src);
26436 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26437 +       }
26438 +       au_unpin(&a->mvd_pin_dst);
26439 +}
26440 +
26441 +/* copy-down the file */
26442 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
26443 +{
26444 +       int err;
26445 +       struct au_cp_generic cpg = {
26446 +               .dentry = a->dentry,
26447 +               .bdst   = a->mvd_bdst,
26448 +               .bsrc   = a->mvd_bsrc,
26449 +               .len    = -1,
26450 +               .pin    = &a->mvd_pin_dst,
26451 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
26452 +       };
26453 +
26454 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
26455 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26456 +               au_fset_cpup(cpg.flags, OVERWRITE);
26457 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26458 +               au_fset_cpup(cpg.flags, RWDST);
26459 +       err = au_sio_cpdown_simple(&cpg);
26460 +       if (unlikely(err))
26461 +               AU_MVD_PR(dmsg, "cpdown failed\n");
26462 +
26463 +       AuTraceErr(err);
26464 +       return err;
26465 +}
26466 +
26467 +/*
26468 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
26469 + * were sleeping
26470 + */
26471 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
26472 +{
26473 +       int err;
26474 +       struct path h_path;
26475 +       struct au_branch *br;
26476 +       struct inode *delegated;
26477 +
26478 +       br = au_sbr(a->sb, a->mvd_bdst);
26479 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26480 +       err = PTR_ERR(h_path.dentry);
26481 +       if (IS_ERR(h_path.dentry)) {
26482 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
26483 +               goto out;
26484 +       }
26485 +
26486 +       err = 0;
26487 +       if (d_is_positive(h_path.dentry)) {
26488 +               h_path.mnt = au_br_mnt(br);
26489 +               delegated = NULL;
26490 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
26491 +                                  &delegated, /*force*/0);
26492 +               if (unlikely(err == -EWOULDBLOCK)) {
26493 +                       pr_warn("cannot retry for NFSv4 delegation"
26494 +                               " for an internal unlink\n");
26495 +                       iput(delegated);
26496 +               }
26497 +               if (unlikely(err))
26498 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
26499 +       }
26500 +       dput(h_path.dentry);
26501 +
26502 +out:
26503 +       AuTraceErr(err);
26504 +       return err;
26505 +}
26506 +
26507 +/*
26508 + * unlink the topmost h_dentry
26509 + */
26510 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
26511 +{
26512 +       int err;
26513 +       struct path h_path;
26514 +       struct inode *delegated;
26515 +
26516 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26517 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26518 +       delegated = NULL;
26519 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26520 +       if (unlikely(err == -EWOULDBLOCK)) {
26521 +               pr_warn("cannot retry for NFSv4 delegation"
26522 +                       " for an internal unlink\n");
26523 +               iput(delegated);
26524 +       }
26525 +       if (unlikely(err))
26526 +               AU_MVD_PR(dmsg, "unlink failed\n");
26527 +
26528 +       AuTraceErr(err);
26529 +       return err;
26530 +}
26531 +
26532 +/* Since mvdown succeeded, we ignore an error of this function */
26533 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26534 +{
26535 +       int err;
26536 +       struct au_branch *br;
26537 +
26538 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26539 +       br = au_sbr(a->sb, a->mvd_bsrc);
26540 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26541 +       if (!err) {
26542 +               br = au_sbr(a->sb, a->mvd_bdst);
26543 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26544 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26545 +       }
26546 +       if (!err)
26547 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26548 +       else
26549 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26550 +}
26551 +
26552 +/*
26553 + * copy-down the file and unlink the bsrc file.
26554 + * - unlink the bdst whout if exist
26555 + * - copy-down the file (with whtmp name and rename)
26556 + * - unlink the bsrc file
26557 + */
26558 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26559 +{
26560 +       int err;
26561 +
26562 +       err = au_do_mkdir(dmsg, a);
26563 +       if (!err)
26564 +               err = au_do_lock(dmsg, a);
26565 +       if (unlikely(err))
26566 +               goto out;
26567 +
26568 +       /*
26569 +        * do not revert the activities we made on bdst since they should be
26570 +        * harmless in aufs.
26571 +        */
26572 +
26573 +       err = au_do_cpdown(dmsg, a);
26574 +       if (!err)
26575 +               err = au_do_unlink_wh(dmsg, a);
26576 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26577 +               err = au_do_unlink(dmsg, a);
26578 +       if (unlikely(err))
26579 +               goto out_unlock;
26580 +
26581 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
26582 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26583 +       if (find_lower_writable(a) < 0)
26584 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26585 +
26586 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26587 +               au_do_stfs(dmsg, a);
26588 +
26589 +       /* maintain internal array */
26590 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26591 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26592 +               au_set_dbtop(a->dentry, a->mvd_bdst);
26593 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26594 +               au_set_ibtop(a->inode, a->mvd_bdst);
26595 +       } else {
26596 +               /* hide the lower */
26597 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26598 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
26599 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26600 +               au_set_ibbot(a->inode, a->mvd_bsrc);
26601 +       }
26602 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
26603 +               au_set_dbbot(a->dentry, a->mvd_bdst);
26604 +       if (au_ibbot(a->inode) < a->mvd_bdst)
26605 +               au_set_ibbot(a->inode, a->mvd_bdst);
26606 +
26607 +out_unlock:
26608 +       au_do_unlock(dmsg, a);
26609 +out:
26610 +       AuTraceErr(err);
26611 +       return err;
26612 +}
26613 +
26614 +/* ---------------------------------------------------------------------- */
26615 +
26616 +/* make sure the file is idle */
26617 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26618 +{
26619 +       int err, plinked;
26620 +
26621 +       err = 0;
26622 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26623 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
26624 +           && au_dcount(a->dentry) == 1
26625 +           && atomic_read(&a->inode->i_count) == 1
26626 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
26627 +           && (!plinked || !au_plink_test(a->inode))
26628 +           && a->inode->i_nlink == 1)
26629 +               goto out;
26630 +
26631 +       err = -EBUSY;
26632 +       AU_MVD_PR(dmsg,
26633 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26634 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26635 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
26636 +                 a->mvd_h_src_inode->i_nlink,
26637 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
26638 +
26639 +out:
26640 +       AuTraceErr(err);
26641 +       return err;
26642 +}
26643 +
26644 +/* make sure the parent dir is fine */
26645 +static int au_mvd_args_parent(const unsigned char dmsg,
26646 +                             struct au_mvd_args *a)
26647 +{
26648 +       int err;
26649 +       aufs_bindex_t bindex;
26650 +
26651 +       err = 0;
26652 +       if (unlikely(au_alive_dir(a->parent))) {
26653 +               err = -ENOENT;
26654 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
26655 +               goto out;
26656 +       }
26657 +
26658 +       a->bopq = au_dbdiropq(a->parent);
26659 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26660 +       AuDbg("b%d\n", bindex);
26661 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26662 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26663 +               err = -EINVAL;
26664 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
26665 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26666 +                         a->bopq, a->mvd_bdst);
26667 +       }
26668 +
26669 +out:
26670 +       AuTraceErr(err);
26671 +       return err;
26672 +}
26673 +
26674 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26675 +                                   struct au_mvd_args *a)
26676 +{
26677 +       int err;
26678 +       struct au_dinfo *dinfo, *tmp;
26679 +
26680 +       /* lookup the next lower positive entry */
26681 +       err = -ENOMEM;
26682 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26683 +       if (unlikely(!tmp))
26684 +               goto out;
26685 +
26686 +       a->bfound = -1;
26687 +       a->bwh = -1;
26688 +       dinfo = au_di(a->dentry);
26689 +       au_di_cp(tmp, dinfo);
26690 +       au_di_swap(tmp, dinfo);
26691 +
26692 +       /* returns the number of positive dentries */
26693 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26694 +                            /* AuLkup_IGNORE_PERM */ 0);
26695 +       if (!err)
26696 +               a->bwh = au_dbwh(a->dentry);
26697 +       else if (err > 0)
26698 +               a->bfound = au_dbtop(a->dentry);
26699 +
26700 +       au_di_swap(tmp, dinfo);
26701 +       au_rw_write_unlock(&tmp->di_rwsem);
26702 +       au_di_free(tmp);
26703 +       if (unlikely(err < 0))
26704 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
26705 +
26706 +       /*
26707 +        * here, we have these cases.
26708 +        * bfound == -1
26709 +        *      no positive dentry under bsrc. there are more sub-cases.
26710 +        *      bwh < 0
26711 +        *              there no whiteout, we can safely move-down.
26712 +        *      bwh <= bsrc
26713 +        *              impossible
26714 +        *      bsrc < bwh && bwh < bdst
26715 +        *              there is a whiteout on RO branch. cannot proceed.
26716 +        *      bwh == bdst
26717 +        *              there is a whiteout on the RW target branch. it should
26718 +        *              be removed.
26719 +        *      bdst < bwh
26720 +        *              there is a whiteout somewhere unrelated branch.
26721 +        * -1 < bfound && bfound <= bsrc
26722 +        *      impossible.
26723 +        * bfound < bdst
26724 +        *      found, but it is on RO branch between bsrc and bdst. cannot
26725 +        *      proceed.
26726 +        * bfound == bdst
26727 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26728 +        *      error.
26729 +        * bdst < bfound
26730 +        *      found, after we create the file on bdst, it will be hidden.
26731 +        */
26732 +
26733 +       AuDebugOn(a->bfound == -1
26734 +                 && a->bwh != -1
26735 +                 && a->bwh <= a->mvd_bsrc);
26736 +       AuDebugOn(-1 < a->bfound
26737 +                 && a->bfound <= a->mvd_bsrc);
26738 +
26739 +       err = -EINVAL;
26740 +       if (a->bfound == -1
26741 +           && a->mvd_bsrc < a->bwh
26742 +           && a->bwh != -1
26743 +           && a->bwh < a->mvd_bdst) {
26744 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26745 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26746 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26747 +               goto out;
26748 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26749 +               a->mvd_errno = EAU_MVDOWN_UPPER;
26750 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26751 +                         a->mvd_bdst, a->bfound);
26752 +               goto out;
26753 +       }
26754 +
26755 +       err = 0; /* success */
26756 +
26757 +out:
26758 +       AuTraceErr(err);
26759 +       return err;
26760 +}
26761 +
26762 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26763 +{
26764 +       int err;
26765 +
26766 +       err = 0;
26767 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26768 +           && a->bfound == a->mvd_bdst)
26769 +               err = -EEXIST;
26770 +       AuTraceErr(err);
26771 +       return err;
26772 +}
26773 +
26774 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26775 +{
26776 +       int err;
26777 +       struct au_branch *br;
26778 +
26779 +       err = -EISDIR;
26780 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
26781 +               goto out;
26782 +
26783 +       err = -EINVAL;
26784 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26785 +               a->mvd_bsrc = au_ibtop(a->inode);
26786 +       else {
26787 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26788 +               if (unlikely(a->mvd_bsrc < 0
26789 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
26790 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
26791 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
26792 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
26793 +                                || au_ibbot(a->inode) < a->mvd_bsrc
26794 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26795 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
26796 +                       AU_MVD_PR(dmsg, "no upper\n");
26797 +                       goto out;
26798 +               }
26799 +       }
26800 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26801 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
26802 +               AU_MVD_PR(dmsg, "on the bottom\n");
26803 +               goto out;
26804 +       }
26805 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26806 +       br = au_sbr(a->sb, a->mvd_bsrc);
26807 +       err = au_br_rdonly(br);
26808 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26809 +               if (unlikely(err))
26810 +                       goto out;
26811 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26812 +                    || IS_APPEND(a->mvd_h_src_inode))) {
26813 +               if (err)
26814 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26815 +               /* go on */
26816 +       } else
26817 +               goto out;
26818 +
26819 +       err = -EINVAL;
26820 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26821 +               a->mvd_bdst = find_lower_writable(a);
26822 +               if (unlikely(a->mvd_bdst < 0)) {
26823 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
26824 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
26825 +                       goto out;
26826 +               }
26827 +       } else {
26828 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26829 +               if (unlikely(a->mvd_bdst < 0
26830 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
26831 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26832 +                       AU_MVD_PR(dmsg, "no lower brid\n");
26833 +                       goto out;
26834 +               }
26835 +       }
26836 +
26837 +       err = au_mvd_args_busy(dmsg, a);
26838 +       if (!err)
26839 +               err = au_mvd_args_parent(dmsg, a);
26840 +       if (!err)
26841 +               err = au_mvd_args_intermediate(dmsg, a);
26842 +       if (!err)
26843 +               err = au_mvd_args_exist(dmsg, a);
26844 +       if (!err)
26845 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26846 +
26847 +out:
26848 +       AuTraceErr(err);
26849 +       return err;
26850 +}
26851 +
26852 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26853 +{
26854 +       int err, e;
26855 +       unsigned char dmsg;
26856 +       struct au_mvd_args *args;
26857 +       struct inode *inode;
26858 +
26859 +       inode = d_inode(dentry);
26860 +       err = -EPERM;
26861 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26862 +               goto out;
26863 +
26864 +       err = -ENOMEM;
26865 +       args = kmalloc(sizeof(*args), GFP_NOFS);
26866 +       if (unlikely(!args))
26867 +               goto out;
26868 +
26869 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26870 +       if (!err)
26871 +               /* VERIFY_WRITE */
26872 +               err = !access_ok(uarg, sizeof(*uarg));
26873 +       if (unlikely(err)) {
26874 +               err = -EFAULT;
26875 +               AuTraceErr(err);
26876 +               goto out_free;
26877 +       }
26878 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
26879 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26880 +       args->mvdown.au_errno = 0;
26881 +       args->dentry = dentry;
26882 +       args->inode = inode;
26883 +       args->sb = dentry->d_sb;
26884 +
26885 +       err = -ENOENT;
26886 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26887 +       args->parent = dget_parent(dentry);
26888 +       args->dir = d_inode(args->parent);
26889 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
26890 +       dput(args->parent);
26891 +       if (unlikely(args->parent != dentry->d_parent)) {
26892 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
26893 +               goto out_dir;
26894 +       }
26895 +
26896 +       inode_lock_nested(inode, I_MUTEX_CHILD);
26897 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26898 +       if (unlikely(err))
26899 +               goto out_inode;
26900 +
26901 +       di_write_lock_parent(args->parent);
26902 +       err = au_mvd_args(dmsg, args);
26903 +       if (unlikely(err))
26904 +               goto out_parent;
26905 +
26906 +       err = au_do_mvdown(dmsg, args);
26907 +       if (unlikely(err))
26908 +               goto out_parent;
26909 +
26910 +       au_cpup_attr_timesizes(args->dir);
26911 +       au_cpup_attr_timesizes(inode);
26912 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26913 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26914 +       /* au_digen_dec(dentry); */
26915 +
26916 +out_parent:
26917 +       di_write_unlock(args->parent);
26918 +       aufs_read_unlock(dentry, AuLock_DW);
26919 +out_inode:
26920 +       inode_unlock(inode);
26921 +out_dir:
26922 +       inode_unlock(args->dir);
26923 +out_free:
26924 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26925 +       if (unlikely(e))
26926 +               err = -EFAULT;
26927 +       au_kfree_rcu(args);
26928 +out:
26929 +       AuTraceErr(err);
26930 +       return err;
26931 +}
26932 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
26933 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
26934 +++ linux/fs/aufs/opts.c        2024-05-13 17:13:00.621404284 +0200
26935 @@ -0,0 +1,1032 @@
26936 +// SPDX-License-Identifier: GPL-2.0
26937 +/*
26938 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26939 + *
26940 + * This program is free software; you can redistribute it and/or modify
26941 + * it under the terms of the GNU General Public License as published by
26942 + * the Free Software Foundation; either version 2 of the License, or
26943 + * (at your option) any later version.
26944 + *
26945 + * This program is distributed in the hope that it will be useful,
26946 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26947 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26948 + * GNU General Public License for more details.
26949 + *
26950 + * You should have received a copy of the GNU General Public License
26951 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26952 + */
26953 +
26954 +/*
26955 + * mount options/flags
26956 + */
26957 +
26958 +#include <linux/types.h> /* a distribution requires */
26959 +#include <linux/parser.h>
26960 +#include "aufs.h"
26961 +
26962 +/* ---------------------------------------------------------------------- */
26963 +
26964 +static const char *au_parser_pattern(int val, match_table_t tbl)
26965 +{
26966 +       struct match_token *p;
26967 +
26968 +       p = tbl;
26969 +       while (p->pattern) {
26970 +               if (p->token == val)
26971 +                       return p->pattern;
26972 +               p++;
26973 +       }
26974 +       BUG();
26975 +       return "??";
26976 +}
26977 +
26978 +static const char *au_optstr(int *val, match_table_t tbl)
26979 +{
26980 +       struct match_token *p;
26981 +       int v;
26982 +
26983 +       v = *val;
26984 +       if (!v)
26985 +               goto out;
26986 +       p = tbl;
26987 +       while (p->pattern) {
26988 +               if (p->token
26989 +                   && (v & p->token) == p->token) {
26990 +                       *val &= ~p->token;
26991 +                       return p->pattern;
26992 +               }
26993 +               p++;
26994 +       }
26995 +
26996 +out:
26997 +       return NULL;
26998 +}
26999 +
27000 +/* ---------------------------------------------------------------------- */
27001 +
27002 +static match_table_t brperm = {
27003 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
27004 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
27005 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
27006 +       {0, NULL}
27007 +};
27008 +
27009 +static match_table_t brattr = {
27010 +       /* general */
27011 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
27012 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
27013 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
27014 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
27015 +#ifdef CONFIG_AUFS_FHSM
27016 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
27017 +#endif
27018 +#ifdef CONFIG_AUFS_XATTR
27019 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
27020 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
27021 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
27022 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
27023 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
27024 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
27025 +#endif
27026 +
27027 +       /* ro/rr branch */
27028 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
27029 +
27030 +       /* rw branch */
27031 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
27032 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
27033 +
27034 +       {0, NULL}
27035 +};
27036 +
27037 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
27038 +{
27039 +       int attr, v;
27040 +       char *p;
27041 +
27042 +       attr = 0;
27043 +       do {
27044 +               p = strchr(str, '+');
27045 +               if (p)
27046 +                       *p = 0;
27047 +               v = match_token(str, table, args);
27048 +               if (v) {
27049 +                       if (v & AuBrAttr_CMOO_Mask)
27050 +                               attr &= ~AuBrAttr_CMOO_Mask;
27051 +                       attr |= v;
27052 +               } else {
27053 +                       if (p)
27054 +                               *p = '+';
27055 +                       pr_warn("ignored branch attribute %s\n", str);
27056 +                       break;
27057 +               }
27058 +               if (p)
27059 +                       str = p + 1;
27060 +       } while (p);
27061 +
27062 +       return attr;
27063 +}
27064 +
27065 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
27066 +{
27067 +       int sz;
27068 +       const char *p;
27069 +       char *q;
27070 +
27071 +       q = str->a;
27072 +       *q = 0;
27073 +       p = au_optstr(&perm, brattr);
27074 +       if (p) {
27075 +               sz = strlen(p);
27076 +               memcpy(q, p, sz + 1);
27077 +               q += sz;
27078 +       } else
27079 +               goto out;
27080 +
27081 +       do {
27082 +               p = au_optstr(&perm, brattr);
27083 +               if (p) {
27084 +                       *q++ = '+';
27085 +                       sz = strlen(p);
27086 +                       memcpy(q, p, sz + 1);
27087 +                       q += sz;
27088 +               }
27089 +       } while (p);
27090 +
27091 +out:
27092 +       return q - str->a;
27093 +}
27094 +
27095 +int au_br_perm_val(char *perm)
27096 +{
27097 +       int val, bad, sz;
27098 +       char *p;
27099 +       substring_t args[MAX_OPT_ARGS];
27100 +       au_br_perm_str_t attr;
27101 +
27102 +       p = strchr(perm, '+');
27103 +       if (p)
27104 +               *p = 0;
27105 +       val = match_token(perm, brperm, args);
27106 +       if (!val) {
27107 +               if (p)
27108 +                       *p = '+';
27109 +               pr_warn("ignored branch permission %s\n", perm);
27110 +               val = AuBrPerm_RO;
27111 +               goto out;
27112 +       }
27113 +       if (!p)
27114 +               goto out;
27115 +
27116 +       val |= br_attr_val(p + 1, brattr, args);
27117 +
27118 +       bad = 0;
27119 +       switch (val & AuBrPerm_Mask) {
27120 +       case AuBrPerm_RO:
27121 +       case AuBrPerm_RR:
27122 +               bad = val & AuBrWAttr_Mask;
27123 +               val &= ~AuBrWAttr_Mask;
27124 +               break;
27125 +       case AuBrPerm_RW:
27126 +               bad = val & AuBrRAttr_Mask;
27127 +               val &= ~AuBrRAttr_Mask;
27128 +               break;
27129 +       }
27130 +
27131 +       /*
27132 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27133 +        * does not treat it as an error, just warning.
27134 +        * this is a tiny guard for the user operation.
27135 +        */
27136 +       if (val & AuBrAttr_UNPIN) {
27137 +               bad |= AuBrAttr_UNPIN;
27138 +               val &= ~AuBrAttr_UNPIN;
27139 +       }
27140 +
27141 +       if (unlikely(bad)) {
27142 +               sz = au_do_optstr_br_attr(&attr, bad);
27143 +               AuDebugOn(!sz);
27144 +               pr_warn("ignored branch attribute %s\n", attr.a);
27145 +       }
27146 +
27147 +out:
27148 +       return val;
27149 +}
27150 +
27151 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
27152 +{
27153 +       au_br_perm_str_t attr;
27154 +       const char *p;
27155 +       char *q;
27156 +       int sz;
27157 +
27158 +       q = str->a;
27159 +       p = au_optstr(&perm, brperm);
27160 +       AuDebugOn(!p || !*p);
27161 +       sz = strlen(p);
27162 +       memcpy(q, p, sz + 1);
27163 +       q += sz;
27164 +
27165 +       sz = au_do_optstr_br_attr(&attr, perm);
27166 +       if (sz) {
27167 +               *q++ = '+';
27168 +               memcpy(q, attr.a, sz + 1);
27169 +       }
27170 +
27171 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
27172 +}
27173 +
27174 +/* ---------------------------------------------------------------------- */
27175 +
27176 +static match_table_t udbalevel = {
27177 +       {AuOpt_UDBA_REVAL, "reval"},
27178 +       {AuOpt_UDBA_NONE, "none"},
27179 +#ifdef CONFIG_AUFS_HNOTIFY
27180 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27181 +#ifdef CONFIG_AUFS_HFSNOTIFY
27182 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
27183 +#endif
27184 +#endif
27185 +       {-1, NULL}
27186 +};
27187 +
27188 +int au_udba_val(char *str)
27189 +{
27190 +       substring_t args[MAX_OPT_ARGS];
27191 +
27192 +       return match_token(str, udbalevel, args);
27193 +}
27194 +
27195 +const char *au_optstr_udba(int udba)
27196 +{
27197 +       return au_parser_pattern(udba, udbalevel);
27198 +}
27199 +
27200 +/* ---------------------------------------------------------------------- */
27201 +
27202 +static match_table_t au_wbr_create_policy = {
27203 +       {AuWbrCreate_TDP, "tdp"},
27204 +       {AuWbrCreate_TDP, "top-down-parent"},
27205 +       {AuWbrCreate_RR, "rr"},
27206 +       {AuWbrCreate_RR, "round-robin"},
27207 +       {AuWbrCreate_MFS, "mfs"},
27208 +       {AuWbrCreate_MFS, "most-free-space"},
27209 +       {AuWbrCreate_MFSV, "mfs:%d"},
27210 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
27211 +
27212 +       /* top-down regardless the parent, and then mfs */
27213 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
27214 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27215 +
27216 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
27217 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27218 +       {AuWbrCreate_PMFS, "pmfs"},
27219 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
27220 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27221 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
27222 +
27223 +       {-1, NULL}
27224 +};
27225 +
27226 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27227 +                           struct au_opt_wbr_create *create)
27228 +{
27229 +       int err;
27230 +       unsigned long long ull;
27231 +
27232 +       err = 0;
27233 +       if (!match_u64(arg, &ull))
27234 +               create->mfsrr_watermark = ull;
27235 +       else {
27236 +               pr_err("bad integer in %s\n", str);
27237 +               err = -EINVAL;
27238 +       }
27239 +
27240 +       return err;
27241 +}
27242 +
27243 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
27244 +                         struct au_opt_wbr_create *create)
27245 +{
27246 +       int n, err;
27247 +
27248 +       err = 0;
27249 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
27250 +               create->mfs_second = n;
27251 +       else {
27252 +               pr_err("bad integer in %s\n", str);
27253 +               err = -EINVAL;
27254 +       }
27255 +
27256 +       return err;
27257 +}
27258 +
27259 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
27260 +{
27261 +       int err, e;
27262 +       substring_t args[MAX_OPT_ARGS];
27263 +
27264 +       err = match_token(str, au_wbr_create_policy, args);
27265 +       create->wbr_create = err;
27266 +       switch (err) {
27267 +       case AuWbrCreate_MFSRRV:
27268 +       case AuWbrCreate_TDMFSV:
27269 +       case AuWbrCreate_PMFSRRV:
27270 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27271 +               if (!e)
27272 +                       e = au_wbr_mfs_sec(&args[1], str, create);
27273 +               if (unlikely(e))
27274 +                       err = e;
27275 +               break;
27276 +       case AuWbrCreate_MFSRR:
27277 +       case AuWbrCreate_TDMFS:
27278 +       case AuWbrCreate_PMFSRR:
27279 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27280 +               if (unlikely(e)) {
27281 +                       err = e;
27282 +                       break;
27283 +               }
27284 +               fallthrough;
27285 +       case AuWbrCreate_MFS:
27286 +       case AuWbrCreate_PMFS:
27287 +               create->mfs_second = AUFS_MFS_DEF_SEC;
27288 +               break;
27289 +       case AuWbrCreate_MFSV:
27290 +       case AuWbrCreate_PMFSV:
27291 +               e = au_wbr_mfs_sec(&args[0], str, create);
27292 +               if (unlikely(e))
27293 +                       err = e;
27294 +               break;
27295 +       }
27296 +
27297 +       return err;
27298 +}
27299 +
27300 +const char *au_optstr_wbr_create(int wbr_create)
27301 +{
27302 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
27303 +}
27304 +
27305 +static match_table_t au_wbr_copyup_policy = {
27306 +       {AuWbrCopyup_TDP, "tdp"},
27307 +       {AuWbrCopyup_TDP, "top-down-parent"},
27308 +       {AuWbrCopyup_BUP, "bup"},
27309 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
27310 +       {AuWbrCopyup_BU, "bu"},
27311 +       {AuWbrCopyup_BU, "bottom-up"},
27312 +       {-1, NULL}
27313 +};
27314 +
27315 +int au_wbr_copyup_val(char *str)
27316 +{
27317 +       substring_t args[MAX_OPT_ARGS];
27318 +
27319 +       return match_token(str, au_wbr_copyup_policy, args);
27320 +}
27321 +
27322 +const char *au_optstr_wbr_copyup(int wbr_copyup)
27323 +{
27324 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
27325 +}
27326 +
27327 +/* ---------------------------------------------------------------------- */
27328 +
27329 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27330 +              aufs_bindex_t bindex)
27331 +{
27332 +       int err;
27333 +       struct au_opt_add *add = &opt->add;
27334 +       char *p;
27335 +
27336 +       add->bindex = bindex;
27337 +       add->perm = AuBrPerm_RO;
27338 +       add->pathname = opt_str;
27339 +       p = strchr(opt_str, '=');
27340 +       if (p) {
27341 +               *p++ = 0;
27342 +               if (*p)
27343 +                       add->perm = au_br_perm_val(p);
27344 +       }
27345 +
27346 +       err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
27347 +       if (!err) {
27348 +               if (!p) {
27349 +                       add->perm = AuBrPerm_RO;
27350 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
27351 +                               add->perm = AuBrPerm_RR;
27352 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
27353 +                               add->perm = AuBrPerm_RW;
27354 +               }
27355 +               opt->type = Opt_add;
27356 +               goto out;
27357 +       }
27358 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
27359 +       err = -EINVAL;
27360 +
27361 +out:
27362 +       return err;
27363 +}
27364 +
27365 +static int au_opt_wbr_create(struct super_block *sb,
27366 +                            struct au_opt_wbr_create *create)
27367 +{
27368 +       int err;
27369 +       struct au_sbinfo *sbinfo;
27370 +
27371 +       SiMustWriteLock(sb);
27372 +
27373 +       err = 1; /* handled */
27374 +       sbinfo = au_sbi(sb);
27375 +       if (sbinfo->si_wbr_create_ops->fin) {
27376 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27377 +               if (!err)
27378 +                       err = 1;
27379 +       }
27380 +
27381 +       sbinfo->si_wbr_create = create->wbr_create;
27382 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27383 +       switch (create->wbr_create) {
27384 +       case AuWbrCreate_MFSRRV:
27385 +       case AuWbrCreate_MFSRR:
27386 +       case AuWbrCreate_TDMFS:
27387 +       case AuWbrCreate_TDMFSV:
27388 +       case AuWbrCreate_PMFSRR:
27389 +       case AuWbrCreate_PMFSRRV:
27390 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27391 +               fallthrough;
27392 +       case AuWbrCreate_MFS:
27393 +       case AuWbrCreate_MFSV:
27394 +       case AuWbrCreate_PMFS:
27395 +       case AuWbrCreate_PMFSV:
27396 +               sbinfo->si_wbr_mfs.mfs_expire
27397 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27398 +               break;
27399 +       }
27400 +
27401 +       if (sbinfo->si_wbr_create_ops->init)
27402 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27403 +
27404 +       return err;
27405 +}
27406 +
27407 +/*
27408 + * returns,
27409 + * plus: processed without an error
27410 + * zero: unprocessed
27411 + */
27412 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27413 +                        struct au_opts *opts)
27414 +{
27415 +       int err;
27416 +       struct au_sbinfo *sbinfo;
27417 +
27418 +       SiMustWriteLock(sb);
27419 +
27420 +       err = 1; /* handled */
27421 +       sbinfo = au_sbi(sb);
27422 +       switch (opt->type) {
27423 +       case Opt_udba:
27424 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27425 +               sbinfo->si_mntflags |= opt->udba;
27426 +               opts->given_udba |= opt->udba;
27427 +               break;
27428 +
27429 +       case Opt_plink:
27430 +               if (opt->tf)
27431 +                       au_opt_set(sbinfo->si_mntflags, PLINK);
27432 +               else {
27433 +                       if (au_opt_test(sbinfo->si_mntflags, PLINK))
27434 +                               au_plink_put(sb, /*verbose*/1);
27435 +                       au_opt_clr(sbinfo->si_mntflags, PLINK);
27436 +               }
27437 +               break;
27438 +       case Opt_list_plink:
27439 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27440 +                       au_plink_list(sb);
27441 +               break;
27442 +
27443 +       case Opt_dio:
27444 +               if (opt->tf) {
27445 +                       au_opt_set(sbinfo->si_mntflags, DIO);
27446 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27447 +               } else {
27448 +                       au_opt_clr(sbinfo->si_mntflags, DIO);
27449 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27450 +               }
27451 +               break;
27452 +
27453 +       case Opt_fhsm_sec:
27454 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27455 +               break;
27456 +
27457 +       case Opt_diropq_a:
27458 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27459 +               break;
27460 +       case Opt_diropq_w:
27461 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27462 +               break;
27463 +
27464 +       case Opt_warn_perm:
27465 +               if (opt->tf)
27466 +                       au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27467 +               else
27468 +                       au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27469 +               break;
27470 +
27471 +       case Opt_verbose:
27472 +               if (opt->tf)
27473 +                       au_opt_set(sbinfo->si_mntflags, VERBOSE);
27474 +               else
27475 +                       au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27476 +               break;
27477 +
27478 +       case Opt_sum:
27479 +               if (opt->tf)
27480 +                       au_opt_set(sbinfo->si_mntflags, SUM);
27481 +               else {
27482 +                       au_opt_clr(sbinfo->si_mntflags, SUM);
27483 +                       au_opt_clr(sbinfo->si_mntflags, SUM_W);
27484 +               }
27485 +               break;
27486 +       case Opt_wsum:
27487 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27488 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27489 +               break;
27490 +
27491 +       case Opt_wbr_create:
27492 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27493 +               break;
27494 +       case Opt_wbr_copyup:
27495 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27496 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27497 +               break;
27498 +
27499 +       case Opt_dirwh:
27500 +               sbinfo->si_dirwh = opt->dirwh;
27501 +               break;
27502 +
27503 +       case Opt_rdcache:
27504 +               sbinfo->si_rdcache
27505 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27506 +               break;
27507 +       case Opt_rdblk:
27508 +               sbinfo->si_rdblk = opt->rdblk;
27509 +               break;
27510 +       case Opt_rdhash:
27511 +               sbinfo->si_rdhash = opt->rdhash;
27512 +               break;
27513 +
27514 +       case Opt_shwh:
27515 +               if (opt->tf)
27516 +                       au_opt_set(sbinfo->si_mntflags, SHWH);
27517 +               else
27518 +                       au_opt_clr(sbinfo->si_mntflags, SHWH);
27519 +               break;
27520 +
27521 +       case Opt_dirperm1:
27522 +               if (opt->tf)
27523 +                       au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27524 +               else
27525 +                       au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27526 +               break;
27527 +
27528 +       case Opt_trunc_xino:
27529 +               if (opt->tf)
27530 +                       au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27531 +               else
27532 +                       au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27533 +               break;
27534 +
27535 +       case Opt_trunc_xino_path:
27536 +       case Opt_itrunc_xino:
27537 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27538 +                                   /*idx_begin*/0);
27539 +               if (!err)
27540 +                       err = 1;
27541 +               break;
27542 +
27543 +       case Opt_trunc_xib:
27544 +               if (opt->tf)
27545 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27546 +               else
27547 +                       au_fclr_opts(opts->flags, TRUNC_XIB);
27548 +               break;
27549 +
27550 +       case Opt_dirren:
27551 +               err = 1;
27552 +               if (opt->tf) {
27553 +                       if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27554 +                               err = au_dr_opt_set(sb);
27555 +                               if (!err)
27556 +                                       err = 1;
27557 +                       }
27558 +                       if (err == 1)
27559 +                               au_opt_set(sbinfo->si_mntflags, DIRREN);
27560 +               } else {
27561 +                       if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27562 +                               err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27563 +                                                                     DR_FLUSHED));
27564 +                               if (!err)
27565 +                                       err = 1;
27566 +                       }
27567 +                       if (err == 1)
27568 +                               au_opt_clr(sbinfo->si_mntflags, DIRREN);
27569 +               }
27570 +               break;
27571 +
27572 +       case Opt_acl:
27573 +               if (opt->tf)
27574 +                       sb->s_flags |= SB_POSIXACL;
27575 +               else
27576 +                       sb->s_flags &= ~SB_POSIXACL;
27577 +               break;
27578 +
27579 +       default:
27580 +               err = 0;
27581 +               break;
27582 +       }
27583 +
27584 +       return err;
27585 +}
27586 +
27587 +/*
27588 + * returns tri-state.
27589 + * plus: processed without an error
27590 + * zero: unprocessed
27591 + * minus: error
27592 + */
27593 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27594 +                    struct au_opts *opts)
27595 +{
27596 +       int err, do_refresh;
27597 +
27598 +       err = 0;
27599 +       switch (opt->type) {
27600 +       case Opt_append:
27601 +               opt->add.bindex = au_sbbot(sb) + 1;
27602 +               if (opt->add.bindex < 0)
27603 +                       opt->add.bindex = 0;
27604 +               goto add;
27605 +               /* Always goto add, not fallthrough */
27606 +       case Opt_prepend:
27607 +               opt->add.bindex = 0;
27608 +               fallthrough;
27609 +       case Opt_add:
27610 +       add: /* indented label */
27611 +               err = au_br_add(sb, &opt->add,
27612 +                               au_ftest_opts(opts->flags, REMOUNT));
27613 +               if (!err) {
27614 +                       err = 1;
27615 +                       au_fset_opts(opts->flags, REFRESH);
27616 +               }
27617 +               break;
27618 +
27619 +       case Opt_del:
27620 +       case Opt_idel:
27621 +               err = au_br_del(sb, &opt->del,
27622 +                               au_ftest_opts(opts->flags, REMOUNT));
27623 +               if (!err) {
27624 +                       err = 1;
27625 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27626 +                       au_fset_opts(opts->flags, REFRESH);
27627 +               }
27628 +               break;
27629 +
27630 +       case Opt_mod:
27631 +       case Opt_imod:
27632 +               err = au_br_mod(sb, &opt->mod,
27633 +                               au_ftest_opts(opts->flags, REMOUNT),
27634 +                               &do_refresh);
27635 +               if (!err) {
27636 +                       err = 1;
27637 +                       if (do_refresh)
27638 +                               au_fset_opts(opts->flags, REFRESH);
27639 +               }
27640 +               break;
27641 +       }
27642 +       return err;
27643 +}
27644 +
27645 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27646 +                      struct au_opt_xino **opt_xino,
27647 +                      struct au_opts *opts)
27648 +{
27649 +       int err;
27650 +
27651 +       err = 0;
27652 +       switch (opt->type) {
27653 +       case Opt_xino:
27654 +               err = au_xino_set(sb, &opt->xino,
27655 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27656 +               if (!err)
27657 +                       *opt_xino = &opt->xino;
27658 +               break;
27659 +       case Opt_noxino:
27660 +               au_xino_clr(sb);
27661 +               *opt_xino = (void *)-1;
27662 +               break;
27663 +       }
27664 +
27665 +       return err;
27666 +}
27667 +
27668 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27669 +                  unsigned int pending)
27670 +{
27671 +       int err, fhsm;
27672 +       aufs_bindex_t bindex, bbot;
27673 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27674 +       struct au_branch *br;
27675 +       struct au_wbr *wbr;
27676 +       struct dentry *root, *dentry;
27677 +       struct inode *dir, *h_dir;
27678 +       struct au_sbinfo *sbinfo;
27679 +       struct au_hinode *hdir;
27680 +
27681 +       SiMustAnyLock(sb);
27682 +
27683 +       sbinfo = au_sbi(sb);
27684 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27685 +
27686 +       if (!(sb_flags & SB_RDONLY)) {
27687 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27688 +                       pr_warn("first branch should be rw\n");
27689 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27690 +                       pr_warn_once("shwh should be used with ro\n");
27691 +       }
27692 +
27693 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27694 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27695 +               pr_warn_once("udba=*notify requires xino\n");
27696 +
27697 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27698 +               pr_warn_once("dirperm1 breaks the protection"
27699 +                            " by the permission bits on the lower branch\n");
27700 +
27701 +       err = 0;
27702 +       fhsm = 0;
27703 +       root = sb->s_root;
27704 +       dir = d_inode(root);
27705 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27706 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27707 +                                     UDBA_NONE);
27708 +       bbot = au_sbbot(sb);
27709 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27710 +               skip = 0;
27711 +               h_dir = au_h_iptr(dir, bindex);
27712 +               br = au_sbr(sb, bindex);
27713 +
27714 +               if ((br->br_perm & AuBrAttr_ICEX)
27715 +                   && !h_dir->i_op->listxattr)
27716 +                       br->br_perm &= ~AuBrAttr_ICEX;
27717 +#if 0 /* untested */
27718 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27719 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27720 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27721 +#endif
27722 +
27723 +               do_free = 0;
27724 +               wbr = br->br_wbr;
27725 +               if (wbr)
27726 +                       wbr_wh_read_lock(wbr);
27727 +
27728 +               if (!au_br_writable(br->br_perm)) {
27729 +                       do_free = !!wbr;
27730 +                       skip = (!wbr
27731 +                               || (!wbr->wbr_whbase
27732 +                                   && !wbr->wbr_plink
27733 +                                   && !wbr->wbr_orph));
27734 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27735 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27736 +                       skip = (!wbr || !wbr->wbr_whbase);
27737 +                       if (skip && wbr) {
27738 +                               if (do_plink)
27739 +                                       skip = !!wbr->wbr_plink;
27740 +                               else
27741 +                                       skip = !wbr->wbr_plink;
27742 +                       }
27743 +               } else {
27744 +                       /* skip = (br->br_whbase && br->br_ohph); */
27745 +                       skip = (wbr && wbr->wbr_whbase);
27746 +                       if (skip) {
27747 +                               if (do_plink)
27748 +                                       skip = !!wbr->wbr_plink;
27749 +                               else
27750 +                                       skip = !wbr->wbr_plink;
27751 +                       }
27752 +               }
27753 +               if (wbr)
27754 +                       wbr_wh_read_unlock(wbr);
27755 +
27756 +               if (can_no_dreval) {
27757 +                       dentry = br->br_path.dentry;
27758 +                       spin_lock(&dentry->d_lock);
27759 +                       if (dentry->d_flags &
27760 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27761 +                               can_no_dreval = 0;
27762 +                       spin_unlock(&dentry->d_lock);
27763 +               }
27764 +
27765 +               if (au_br_fhsm(br->br_perm)) {
27766 +                       fhsm++;
27767 +                       AuDebugOn(!br->br_fhsm);
27768 +               }
27769 +
27770 +               if (skip)
27771 +                       continue;
27772 +
27773 +               hdir = au_hi(dir, bindex);
27774 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27775 +               if (wbr)
27776 +                       wbr_wh_write_lock(wbr);
27777 +               err = au_wh_init(br, sb);
27778 +               if (wbr)
27779 +                       wbr_wh_write_unlock(wbr);
27780 +               au_hn_inode_unlock(hdir);
27781 +
27782 +               if (!err && do_free) {
27783 +                       au_kfree_rcu(wbr);
27784 +                       br->br_wbr = NULL;
27785 +               }
27786 +       }
27787 +
27788 +       if (can_no_dreval)
27789 +               au_fset_si(sbinfo, NO_DREVAL);
27790 +       else
27791 +               au_fclr_si(sbinfo, NO_DREVAL);
27792 +
27793 +       if (fhsm >= 2) {
27794 +               au_fset_si(sbinfo, FHSM);
27795 +               for (bindex = bbot; bindex >= 0; bindex--) {
27796 +                       br = au_sbr(sb, bindex);
27797 +                       if (au_br_fhsm(br->br_perm)) {
27798 +                               au_fhsm_set_bottom(sb, bindex);
27799 +                               break;
27800 +                       }
27801 +               }
27802 +       } else {
27803 +               au_fclr_si(sbinfo, FHSM);
27804 +               au_fhsm_set_bottom(sb, -1);
27805 +       }
27806 +
27807 +       return err;
27808 +}
27809 +
27810 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27811 +{
27812 +       int err;
27813 +       unsigned int tmp;
27814 +       aufs_bindex_t bindex, bbot;
27815 +       struct au_opt *opt;
27816 +       struct au_opt_xino *opt_xino, xino;
27817 +       struct au_sbinfo *sbinfo;
27818 +       struct au_branch *br;
27819 +       struct inode *dir;
27820 +
27821 +       SiMustWriteLock(sb);
27822 +
27823 +       err = 0;
27824 +       opt_xino = NULL;
27825 +       opt = opts->opt;
27826 +       while (err >= 0 && opt->type != Opt_tail)
27827 +               err = au_opt_simple(sb, opt++, opts);
27828 +       if (err > 0)
27829 +               err = 0;
27830 +       else if (unlikely(err < 0))
27831 +               goto out;
27832 +
27833 +       /* disable xino and udba temporary */
27834 +       sbinfo = au_sbi(sb);
27835 +       tmp = sbinfo->si_mntflags;
27836 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27837 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27838 +
27839 +       opt = opts->opt;
27840 +       while (err >= 0 && opt->type != Opt_tail)
27841 +               err = au_opt_br(sb, opt++, opts);
27842 +       if (err > 0)
27843 +               err = 0;
27844 +       else if (unlikely(err < 0))
27845 +               goto out;
27846 +
27847 +       bbot = au_sbbot(sb);
27848 +       if (unlikely(bbot < 0)) {
27849 +               err = -EINVAL;
27850 +               pr_err("no branches\n");
27851 +               goto out;
27852 +       }
27853 +
27854 +       if (au_opt_test(tmp, XINO))
27855 +               au_opt_set(sbinfo->si_mntflags, XINO);
27856 +       opt = opts->opt;
27857 +       while (!err && opt->type != Opt_tail)
27858 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27859 +       if (unlikely(err))
27860 +               goto out;
27861 +
27862 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27863 +       if (unlikely(err))
27864 +               goto out;
27865 +
27866 +       /* restore xino */
27867 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27868 +               xino.file = au_xino_def(sb);
27869 +               err = PTR_ERR(xino.file);
27870 +               if (IS_ERR(xino.file))
27871 +                       goto out;
27872 +
27873 +               err = au_xino_set(sb, &xino, /*remount*/0);
27874 +               fput(xino.file);
27875 +               if (unlikely(err))
27876 +                       goto out;
27877 +       }
27878 +
27879 +       /* restore udba */
27880 +       tmp &= AuOptMask_UDBA;
27881 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27882 +       sbinfo->si_mntflags |= tmp;
27883 +       bbot = au_sbbot(sb);
27884 +       for (bindex = 0; bindex <= bbot; bindex++) {
27885 +               br = au_sbr(sb, bindex);
27886 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27887 +               if (unlikely(err))
27888 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27889 +                               bindex, err);
27890 +               /* go on even if err */
27891 +       }
27892 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27893 +               dir = d_inode(sb->s_root);
27894 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27895 +       }
27896 +
27897 +out:
27898 +       return err;
27899 +}
27900 +
27901 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27902 +{
27903 +       int err, rerr;
27904 +       unsigned char no_dreval;
27905 +       struct inode *dir;
27906 +       struct au_opt_xino *opt_xino;
27907 +       struct au_opt *opt;
27908 +       struct au_sbinfo *sbinfo;
27909 +
27910 +       SiMustWriteLock(sb);
27911 +
27912 +       err = au_dr_opt_flush(sb);
27913 +       if (unlikely(err))
27914 +               goto out;
27915 +       au_fset_opts(opts->flags, DR_FLUSHED);
27916 +
27917 +       dir = d_inode(sb->s_root);
27918 +       sbinfo = au_sbi(sb);
27919 +       opt_xino = NULL;
27920 +       opt = opts->opt;
27921 +       while (err >= 0 && opt->type != Opt_tail) {
27922 +               err = au_opt_simple(sb, opt, opts);
27923 +               if (!err)
27924 +                       err = au_opt_br(sb, opt, opts);
27925 +               if (!err)
27926 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27927 +               opt++;
27928 +       }
27929 +       if (err > 0)
27930 +               err = 0;
27931 +       AuTraceErr(err);
27932 +       /* go on even err */
27933 +
27934 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27935 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27936 +       if (unlikely(rerr && !err))
27937 +               err = rerr;
27938 +
27939 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27940 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27941 +
27942 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27943 +               rerr = au_xib_trunc(sb);
27944 +               if (unlikely(rerr && !err))
27945 +                       err = rerr;
27946 +       }
27947 +
27948 +       /* will be handled by the caller */
27949 +       if (!au_ftest_opts(opts->flags, REFRESH)
27950 +           && (opts->given_udba
27951 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27952 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27953 +                   ))
27954 +               au_fset_opts(opts->flags, REFRESH);
27955 +
27956 +       AuDbg("status 0x%x\n", opts->flags);
27957 +
27958 +out:
27959 +       return err;
27960 +}
27961 +
27962 +/* ---------------------------------------------------------------------- */
27963 +
27964 +unsigned int au_opt_udba(struct super_block *sb)
27965 +{
27966 +       return au_mntflags(sb) & AuOptMask_UDBA;
27967 +}
27968 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27969 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27970 +++ linux/fs/aufs/opts.h        2024-05-13 17:13:00.621404284 +0200
27971 @@ -0,0 +1,263 @@
27972 +/* SPDX-License-Identifier: GPL-2.0 */
27973 +/*
27974 + * Copyright (C) 2005-2022 Junjiro R. Okajima
27975 + *
27976 + * This program is free software; you can redistribute it and/or modify
27977 + * it under the terms of the GNU General Public License as published by
27978 + * the Free Software Foundation; either version 2 of the License, or
27979 + * (at your option) any later version.
27980 + *
27981 + * This program is distributed in the hope that it will be useful,
27982 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27983 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27984 + * GNU General Public License for more details.
27985 + *
27986 + * You should have received a copy of the GNU General Public License
27987 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27988 + */
27989 +
27990 +/*
27991 + * mount options/flags
27992 + */
27993 +
27994 +#ifndef __AUFS_OPTS_H__
27995 +#define __AUFS_OPTS_H__
27996 +
27997 +#ifdef __KERNEL__
27998 +
27999 +#include <linux/fs_parser.h>
28000 +#include <linux/namei.h>
28001 +#include <linux/path.h>
28002 +
28003 +enum {
28004 +       Opt_br,
28005 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
28006 +       Opt_idel, Opt_imod,
28007 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
28008 +       Opt_xino, Opt_noxino,
28009 +       Opt_trunc_xino, Opt_trunc_xino_v,
28010 +       Opt_trunc_xino_path, Opt_itrunc_xino,
28011 +       Opt_trunc_xib,
28012 +       Opt_shwh,
28013 +       Opt_plink, Opt_list_plink,
28014 +       Opt_udba,
28015 +       Opt_dio,
28016 +       Opt_diropq, Opt_diropq_a, Opt_diropq_w,
28017 +       Opt_warn_perm,
28018 +       Opt_wbr_copyup, Opt_wbr_create,
28019 +       Opt_fhsm_sec,
28020 +       Opt_verbose, Opt_noverbose,
28021 +       Opt_sum, Opt_wsum,
28022 +       Opt_dirperm1,
28023 +       Opt_dirren,
28024 +       Opt_acl,
28025 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
28026 +};
28027 +
28028 +/* ---------------------------------------------------------------------- */
28029 +
28030 +/* mount flags */
28031 +#define AuOpt_XINO             BIT(0)          /* external inode number bitmap
28032 +                                                  and translation table */
28033 +#define AuOpt_TRUNC_XINO       BIT(1)          /* truncate xino files */
28034 +#define AuOpt_UDBA_NONE                BIT(2)          /* users direct branch access */
28035 +#define AuOpt_UDBA_REVAL       BIT(3)
28036 +#define AuOpt_UDBA_HNOTIFY     BIT(4)
28037 +#define AuOpt_SHWH             BIT(5)          /* show whiteout */
28038 +#define AuOpt_PLINK            BIT(6)          /* pseudo-link */
28039 +#define AuOpt_DIRPERM1         BIT(7)          /* ignore the lower dir's perm
28040 +                                                  bits */
28041 +#define AuOpt_ALWAYS_DIROPQ    BIT(8)          /* policy to creating diropq */
28042 +#define AuOpt_SUM              BIT(9)          /* summation for statfs(2) */
28043 +#define AuOpt_SUM_W            BIT(10)         /* unimplemented */
28044 +#define AuOpt_WARN_PERM                BIT(11)         /* warn when add-branch */
28045 +#define AuOpt_VERBOSE          BIT(12)         /* print the cause of error */
28046 +#define AuOpt_DIO              BIT(13)         /* direct io */
28047 +#define AuOpt_DIRREN           BIT(14)         /* directory rename */
28048 +
28049 +#ifndef CONFIG_AUFS_HNOTIFY
28050 +#undef AuOpt_UDBA_HNOTIFY
28051 +#define AuOpt_UDBA_HNOTIFY     0
28052 +#endif
28053 +#ifndef CONFIG_AUFS_DIRREN
28054 +#undef AuOpt_DIRREN
28055 +#define AuOpt_DIRREN           0
28056 +#endif
28057 +#ifndef CONFIG_AUFS_SHWH
28058 +#undef AuOpt_SHWH
28059 +#define AuOpt_SHWH             0
28060 +#endif
28061 +
28062 +#define AuOpt_Def      (AuOpt_XINO \
28063 +                        | AuOpt_UDBA_REVAL \
28064 +                        | AuOpt_PLINK \
28065 +                        /* | AuOpt_DIRPERM1 */ \
28066 +                        | AuOpt_WARN_PERM)
28067 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
28068 +                        | AuOpt_UDBA_REVAL \
28069 +                        | AuOpt_UDBA_HNOTIFY)
28070 +
28071 +#define AuOpt_LkupDirFlags     (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
28072 +
28073 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
28074 +#define au_opt_set(flags, name) do { \
28075 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
28076 +       ((flags) |= AuOpt_##name); \
28077 +} while (0)
28078 +#define au_opt_set_udba(flags, name) do { \
28079 +       (flags) &= ~AuOptMask_UDBA; \
28080 +       ((flags) |= AuOpt_##name); \
28081 +} while (0)
28082 +#define au_opt_clr(flags, name) do { \
28083 +       ((flags) &= ~AuOpt_##name); \
28084 +} while (0)
28085 +
28086 +static inline unsigned int au_opts_plink(unsigned int mntflags)
28087 +{
28088 +#ifdef CONFIG_PROC_FS
28089 +       return mntflags;
28090 +#else
28091 +       return mntflags & ~AuOpt_PLINK;
28092 +#endif
28093 +}
28094 +
28095 +/* ---------------------------------------------------------------------- */
28096 +
28097 +/* policies to select one among multiple writable branches */
28098 +enum {
28099 +       AuWbrCreate_TDP,        /* top down parent */
28100 +       AuWbrCreate_RR,         /* round robin */
28101 +       AuWbrCreate_MFS,        /* most free space */
28102 +       AuWbrCreate_MFSV,       /* mfs with seconds */
28103 +       AuWbrCreate_MFSRR,      /* mfs then rr */
28104 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
28105 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
28106 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
28107 +       AuWbrCreate_PMFS,       /* parent and mfs */
28108 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
28109 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
28110 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
28111 +
28112 +       AuWbrCreate_Def = AuWbrCreate_TDP
28113 +};
28114 +
28115 +enum {
28116 +       AuWbrCopyup_TDP,        /* top down parent */
28117 +       AuWbrCopyup_BUP,        /* bottom up parent */
28118 +       AuWbrCopyup_BU,         /* bottom up */
28119 +
28120 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
28121 +};
28122 +
28123 +/* ---------------------------------------------------------------------- */
28124 +
28125 +struct file;
28126 +
28127 +struct au_opt_add {
28128 +       aufs_bindex_t   bindex;
28129 +       char            *pathname;
28130 +       int             perm;
28131 +       struct path     path;
28132 +};
28133 +
28134 +struct au_opt_del {
28135 +       char            *pathname;
28136 +       struct path     h_path;
28137 +};
28138 +
28139 +struct au_opt_mod {
28140 +       char            *path;
28141 +       int             perm;
28142 +       struct dentry   *h_root;
28143 +};
28144 +
28145 +struct au_opt_xino {
28146 +       char            *path;
28147 +       struct file     *file;
28148 +};
28149 +
28150 +struct au_opt_xino_itrunc {
28151 +       aufs_bindex_t   bindex;
28152 +};
28153 +
28154 +struct au_opt_wbr_create {
28155 +       int                     wbr_create;
28156 +       int                     mfs_second;
28157 +       unsigned long long      mfsrr_watermark;
28158 +};
28159 +
28160 +struct au_opt {
28161 +       int type;
28162 +       union {
28163 +               struct au_opt_xino      xino;
28164 +               struct au_opt_xino_itrunc xino_itrunc;
28165 +               struct au_opt_add       add;
28166 +               struct au_opt_del       del;
28167 +               struct au_opt_mod       mod;
28168 +               int                     dirwh;
28169 +               int                     rdcache;
28170 +               unsigned int            rdblk;
28171 +               unsigned int            rdhash;
28172 +               int                     udba;
28173 +               struct au_opt_wbr_create wbr_create;
28174 +               int                     wbr_copyup;
28175 +               unsigned int            fhsm_second;
28176 +               bool                    tf; /* generic flag, true or false */
28177 +       };
28178 +};
28179 +
28180 +/* opts flags */
28181 +#define AuOpts_REMOUNT         BIT(0)
28182 +#define AuOpts_REFRESH         BIT(1)
28183 +#define AuOpts_TRUNC_XIB       BIT(2)
28184 +#define AuOpts_REFRESH_DYAOP   BIT(3)
28185 +#define AuOpts_REFRESH_IDOP    BIT(4)
28186 +#define AuOpts_DR_FLUSHED      BIT(5)
28187 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
28188 +#define au_fset_opts(flags, name) \
28189 +       do { (flags) |= AuOpts_##name; } while (0)
28190 +#define au_fclr_opts(flags, name) \
28191 +       do { (flags) &= ~AuOpts_##name; } while (0)
28192 +
28193 +#ifndef CONFIG_AUFS_DIRREN
28194 +#undef AuOpts_DR_FLUSHED
28195 +#define AuOpts_DR_FLUSHED      0
28196 +#endif
28197 +
28198 +struct au_opts {
28199 +       struct au_opt   *opt;
28200 +       int             max_opt;
28201 +
28202 +       unsigned int    given_udba;
28203 +       unsigned int    flags;
28204 +       unsigned long   sb_flags;
28205 +};
28206 +
28207 +/* ---------------------------------------------------------------------- */
28208 +
28209 +/* opts.c */
28210 +int au_br_perm_val(char *perm);
28211 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
28212 +int au_udba_val(char *str);
28213 +const char *au_optstr_udba(int udba);
28214 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
28215 +const char *au_optstr_wbr_create(int wbr_create);
28216 +int au_wbr_copyup_val(char *str);
28217 +const char *au_optstr_wbr_copyup(int wbr_copyup);
28218 +
28219 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28220 +              aufs_bindex_t bindex);
28221 +struct super_block;
28222 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28223 +                  unsigned int pending);
28224 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28225 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28226 +
28227 +unsigned int au_opt_udba(struct super_block *sb);
28228 +
28229 +/* fsctx.c */
28230 +int aufs_fsctx_init(struct fs_context *fc);
28231 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28232 +
28233 +#endif /* __KERNEL__ */
28234 +#endif /* __AUFS_OPTS_H__ */
28235 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
28236 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
28237 +++ linux/fs/aufs/plink.c       2024-05-13 17:13:00.621404284 +0200
28238 @@ -0,0 +1,516 @@
28239 +// SPDX-License-Identifier: GPL-2.0
28240 +/*
28241 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28242 + *
28243 + * This program is free software; you can redistribute it and/or modify
28244 + * it under the terms of the GNU General Public License as published by
28245 + * the Free Software Foundation; either version 2 of the License, or
28246 + * (at your option) any later version.
28247 + *
28248 + * This program is distributed in the hope that it will be useful,
28249 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28250 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28251 + * GNU General Public License for more details.
28252 + *
28253 + * You should have received a copy of the GNU General Public License
28254 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28255 + */
28256 +
28257 +/*
28258 + * pseudo-link
28259 + */
28260 +
28261 +#include "aufs.h"
28262 +
28263 +/*
28264 + * the pseudo-link maintenance mode.
28265 + * during a user process maintains the pseudo-links,
28266 + * prohibit adding a new plink and branch manipulation.
28267 + *
28268 + * Flags
28269 + * NOPLM:
28270 + *     For entry functions which will handle plink, and i_mutex is already held
28271 + *     in VFS.
28272 + *     They cannot wait and should return an error at once.
28273 + *     Callers has to check the error.
28274 + * NOPLMW:
28275 + *     For entry functions which will handle plink, but i_mutex is not held
28276 + *     in VFS.
28277 + *     They can wait the plink maintenance mode to finish.
28278 + *
28279 + * They behave like F_SETLK and F_SETLKW.
28280 + * If the caller never handle plink, then both flags are unnecessary.
28281 + */
28282 +
28283 +int au_plink_maint(struct super_block *sb, int flags)
28284 +{
28285 +       int err;
28286 +       pid_t pid, ppid;
28287 +       struct task_struct *parent, *prev;
28288 +       struct au_sbinfo *sbi;
28289 +
28290 +       SiMustAnyLock(sb);
28291 +
28292 +       err = 0;
28293 +       if (!au_opt_test(au_mntflags(sb), PLINK))
28294 +               goto out;
28295 +
28296 +       sbi = au_sbi(sb);
28297 +       pid = sbi->si_plink_maint_pid;
28298 +       if (!pid || pid == current->pid)
28299 +               goto out;
28300 +
28301 +       /* todo: it highly depends upon /sbin/mount.aufs */
28302 +       prev = NULL;
28303 +       parent = current;
28304 +       ppid = 0;
28305 +       rcu_read_lock();
28306 +       while (1) {
28307 +               parent = rcu_dereference(parent->real_parent);
28308 +               if (parent == prev)
28309 +                       break;
28310 +               ppid = task_pid_vnr(parent);
28311 +               if (pid == ppid) {
28312 +                       rcu_read_unlock();
28313 +                       goto out;
28314 +               }
28315 +               prev = parent;
28316 +       }
28317 +       rcu_read_unlock();
28318 +
28319 +       if (au_ftest_lock(flags, NOPLMW)) {
28320 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
28321 +               /* AuDebugOn(!lockdep_depth(current)); */
28322 +               while (sbi->si_plink_maint_pid) {
28323 +                       si_read_unlock(sb);
28324 +                       /* gave up wake_up_bit() */
28325 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28326 +
28327 +                       if (au_ftest_lock(flags, FLUSH))
28328 +                               au_nwt_flush(&sbi->si_nowait);
28329 +                       si_noflush_read_lock(sb);
28330 +               }
28331 +       } else if (au_ftest_lock(flags, NOPLM)) {
28332 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
28333 +               err = -EAGAIN;
28334 +       }
28335 +
28336 +out:
28337 +       return err;
28338 +}
28339 +
28340 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28341 +{
28342 +       spin_lock(&sbinfo->si_plink_maint_lock);
28343 +       sbinfo->si_plink_maint_pid = 0;
28344 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28345 +       wake_up_all(&sbinfo->si_plink_wq);
28346 +}
28347 +
28348 +int au_plink_maint_enter(struct super_block *sb)
28349 +{
28350 +       int err;
28351 +       struct au_sbinfo *sbinfo;
28352 +
28353 +       err = 0;
28354 +       sbinfo = au_sbi(sb);
28355 +       /* make sure i am the only one in this fs */
28356 +       si_write_lock(sb, AuLock_FLUSH);
28357 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28358 +               spin_lock(&sbinfo->si_plink_maint_lock);
28359 +               if (!sbinfo->si_plink_maint_pid)
28360 +                       sbinfo->si_plink_maint_pid = current->pid;
28361 +               else
28362 +                       err = -EBUSY;
28363 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28364 +       }
28365 +       si_write_unlock(sb);
28366 +
28367 +       return err;
28368 +}
28369 +
28370 +/* ---------------------------------------------------------------------- */
28371 +
28372 +#ifdef CONFIG_AUFS_DEBUG
28373 +void au_plink_list(struct super_block *sb)
28374 +{
28375 +       int i;
28376 +       struct au_sbinfo *sbinfo;
28377 +       struct hlist_bl_head *hbl;
28378 +       struct hlist_bl_node *pos;
28379 +       struct au_icntnr *icntnr;
28380 +
28381 +       SiMustAnyLock(sb);
28382 +
28383 +       sbinfo = au_sbi(sb);
28384 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28385 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28386 +
28387 +       for (i = 0; i < AuPlink_NHASH; i++) {
28388 +               hbl = sbinfo->si_plink + i;
28389 +               hlist_bl_lock(hbl);
28390 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28391 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28392 +               hlist_bl_unlock(hbl);
28393 +       }
28394 +}
28395 +#endif
28396 +
28397 +/* is the inode pseudo-linked? */
28398 +int au_plink_test(struct inode *inode)
28399 +{
28400 +       int found, i;
28401 +       struct au_sbinfo *sbinfo;
28402 +       struct hlist_bl_head *hbl;
28403 +       struct hlist_bl_node *pos;
28404 +       struct au_icntnr *icntnr;
28405 +
28406 +       sbinfo = au_sbi(inode->i_sb);
28407 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28408 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28409 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28410 +
28411 +       found = 0;
28412 +       i = au_plink_hash(inode->i_ino);
28413 +       hbl =  sbinfo->si_plink + i;
28414 +       hlist_bl_lock(hbl);
28415 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28416 +               if (&icntnr->vfs_inode == inode) {
28417 +                       found = 1;
28418 +                       break;
28419 +               }
28420 +       hlist_bl_unlock(hbl);
28421 +       return found;
28422 +}
28423 +
28424 +/* ---------------------------------------------------------------------- */
28425 +
28426 +/*
28427 + * generate a name for plink.
28428 + * the file will be stored under AUFS_WH_PLINKDIR.
28429 + */
28430 +/* 20 is max digits length of ulong 64 */
28431 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28432 +
28433 +static int plink_name(char *name, int len, struct inode *inode,
28434 +                     aufs_bindex_t bindex)
28435 +{
28436 +       int rlen;
28437 +       struct inode *h_inode;
28438 +
28439 +       h_inode = au_h_iptr(inode, bindex);
28440 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28441 +       return rlen;
28442 +}
28443 +
28444 +struct au_do_plink_lkup_args {
28445 +       struct dentry **errp;
28446 +       struct qstr *tgtname;
28447 +       struct path *h_ppath;
28448 +};
28449 +
28450 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28451 +                                      struct path *h_ppath)
28452 +{
28453 +       struct dentry *h_dentry;
28454 +       struct inode *h_inode;
28455 +
28456 +       h_inode = d_inode(h_ppath->dentry);
28457 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28458 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
28459 +       inode_unlock_shared(h_inode);
28460 +
28461 +       return h_dentry;
28462 +}
28463 +
28464 +static void au_call_do_plink_lkup(void *args)
28465 +{
28466 +       struct au_do_plink_lkup_args *a = args;
28467 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
28468 +}
28469 +
28470 +/* lookup the plink-ed @inode under the branch at @bindex */
28471 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28472 +{
28473 +       struct dentry *h_dentry;
28474 +       struct au_branch *br;
28475 +       struct path h_ppath;
28476 +       int wkq_err;
28477 +       char a[PLINK_NAME_LEN];
28478 +       struct qstr tgtname = QSTR_INIT(a, 0);
28479 +
28480 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28481 +
28482 +       br = au_sbr(inode->i_sb, bindex);
28483 +       h_ppath.dentry = br->br_wbr->wbr_plink;
28484 +       h_ppath.mnt = au_br_mnt(br);
28485 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28486 +
28487 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28488 +               struct au_do_plink_lkup_args args = {
28489 +                       .errp           = &h_dentry,
28490 +                       .tgtname        = &tgtname,
28491 +                       .h_ppath        = &h_ppath
28492 +               };
28493 +
28494 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28495 +               if (unlikely(wkq_err))
28496 +                       h_dentry = ERR_PTR(wkq_err);
28497 +       } else
28498 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28499 +
28500 +       return h_dentry;
28501 +}
28502 +
28503 +/* create a pseudo-link */
28504 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28505 +                     struct dentry *h_dentry)
28506 +{
28507 +       int err;
28508 +       struct path h_path;
28509 +       struct inode *h_dir, *delegated;
28510 +
28511 +       h_dir = d_inode(h_ppath->dentry);
28512 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28513 +       h_path.mnt = h_ppath->mnt;
28514 +again:
28515 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28516 +       err = PTR_ERR(h_path.dentry);
28517 +       if (IS_ERR(h_path.dentry))
28518 +               goto out;
28519 +
28520 +       err = 0;
28521 +       /* wh.plink dir is not monitored */
28522 +       /* todo: is it really safe? */
28523 +       if (d_is_positive(h_path.dentry)
28524 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28525 +               delegated = NULL;
28526 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28527 +               if (unlikely(err == -EWOULDBLOCK)) {
28528 +                       pr_warn("cannot retry for NFSv4 delegation"
28529 +                               " for an internal unlink\n");
28530 +                       iput(delegated);
28531 +               }
28532 +               dput(h_path.dentry);
28533 +               h_path.dentry = NULL;
28534 +               if (!err)
28535 +                       goto again;
28536 +       }
28537 +       if (!err && d_is_negative(h_path.dentry)) {
28538 +               delegated = NULL;
28539 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28540 +               if (unlikely(err == -EWOULDBLOCK)) {
28541 +                       pr_warn("cannot retry for NFSv4 delegation"
28542 +                               " for an internal link\n");
28543 +                       iput(delegated);
28544 +               }
28545 +       }
28546 +       dput(h_path.dentry);
28547 +
28548 +out:
28549 +       inode_unlock(h_dir);
28550 +       return err;
28551 +}
28552 +
28553 +struct do_whplink_args {
28554 +       int *errp;
28555 +       struct qstr *tgt;
28556 +       struct path *h_ppath;
28557 +       struct dentry *h_dentry;
28558 +};
28559 +
28560 +static void call_do_whplink(void *args)
28561 +{
28562 +       struct do_whplink_args *a = args;
28563 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28564 +}
28565 +
28566 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28567 +                  aufs_bindex_t bindex)
28568 +{
28569 +       int err, wkq_err;
28570 +       struct au_branch *br;
28571 +       struct au_wbr *wbr;
28572 +       struct path h_ppath;
28573 +       char a[PLINK_NAME_LEN];
28574 +       struct qstr tgtname = QSTR_INIT(a, 0);
28575 +
28576 +       br = au_sbr(inode->i_sb, bindex);
28577 +       wbr = br->br_wbr;
28578 +       h_ppath.dentry = wbr->wbr_plink;
28579 +       h_ppath.mnt = au_br_mnt(br);
28580 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28581 +
28582 +       /* always superio. */
28583 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28584 +               struct do_whplink_args args = {
28585 +                       .errp           = &err,
28586 +                       .tgt            = &tgtname,
28587 +                       .h_ppath        = &h_ppath,
28588 +                       .h_dentry       = h_dentry
28589 +               };
28590 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28591 +               if (unlikely(wkq_err))
28592 +                       err = wkq_err;
28593 +       } else
28594 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28595 +
28596 +       return err;
28597 +}
28598 +
28599 +/*
28600 + * create a new pseudo-link for @h_dentry on @bindex.
28601 + * the linked inode is held in aufs @inode.
28602 + */
28603 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28604 +                    struct dentry *h_dentry)
28605 +{
28606 +       struct super_block *sb;
28607 +       struct au_sbinfo *sbinfo;
28608 +       struct hlist_bl_head *hbl;
28609 +       struct hlist_bl_node *pos;
28610 +       struct au_icntnr *icntnr;
28611 +       int found, err, cnt, i;
28612 +
28613 +       sb = inode->i_sb;
28614 +       sbinfo = au_sbi(sb);
28615 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28616 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28617 +
28618 +       found = au_plink_test(inode);
28619 +       if (found)
28620 +               return;
28621 +
28622 +       i = au_plink_hash(inode->i_ino);
28623 +       hbl = sbinfo->si_plink + i;
28624 +       au_igrab(inode);
28625 +
28626 +       hlist_bl_lock(hbl);
28627 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28628 +               if (&icntnr->vfs_inode == inode) {
28629 +                       found = 1;
28630 +                       break;
28631 +               }
28632 +       }
28633 +       if (!found) {
28634 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28635 +               hlist_bl_add_head(&icntnr->plink, hbl);
28636 +       }
28637 +       hlist_bl_unlock(hbl);
28638 +       if (!found) {
28639 +               cnt = au_hbl_count(hbl);
28640 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28641 +               if (cnt > AUFS_PLINK_WARN)
28642 +                       AuWarn1(msg ", %d\n", cnt);
28643 +#undef msg
28644 +               err = whplink(h_dentry, inode, bindex);
28645 +               if (unlikely(err)) {
28646 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28647 +                       au_hbl_del(&icntnr->plink, hbl);
28648 +                       iput(&icntnr->vfs_inode);
28649 +               }
28650 +       } else
28651 +               iput(&icntnr->vfs_inode);
28652 +}
28653 +
28654 +/* free all plinks */
28655 +void au_plink_put(struct super_block *sb, int verbose)
28656 +{
28657 +       int i, warned;
28658 +       struct au_sbinfo *sbinfo;
28659 +       struct hlist_bl_head *hbl;
28660 +       struct hlist_bl_node *pos, *tmp;
28661 +       struct au_icntnr *icntnr;
28662 +
28663 +       SiMustWriteLock(sb);
28664 +
28665 +       sbinfo = au_sbi(sb);
28666 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28667 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28668 +
28669 +       /* no spin_lock since sbinfo is write-locked */
28670 +       warned = 0;
28671 +       for (i = 0; i < AuPlink_NHASH; i++) {
28672 +               hbl = sbinfo->si_plink + i;
28673 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28674 +                       pr_warn("pseudo-link is not flushed");
28675 +                       warned = 1;
28676 +               }
28677 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28678 +                       iput(&icntnr->vfs_inode);
28679 +               INIT_HLIST_BL_HEAD(hbl);
28680 +       }
28681 +}
28682 +
28683 +void au_plink_clean(struct super_block *sb, int verbose)
28684 +{
28685 +       struct dentry *root;
28686 +
28687 +       root = sb->s_root;
28688 +       aufs_write_lock(root);
28689 +       if (au_opt_test(au_mntflags(sb), PLINK))
28690 +               au_plink_put(sb, verbose);
28691 +       aufs_write_unlock(root);
28692 +}
28693 +
28694 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28695 +{
28696 +       int do_put;
28697 +       aufs_bindex_t btop, bbot, bindex;
28698 +
28699 +       do_put = 0;
28700 +       btop = au_ibtop(inode);
28701 +       bbot = au_ibbot(inode);
28702 +       if (btop >= 0) {
28703 +               for (bindex = btop; bindex <= bbot; bindex++) {
28704 +                       if (!au_h_iptr(inode, bindex)
28705 +                           || au_ii_br_id(inode, bindex) != br_id)
28706 +                               continue;
28707 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28708 +                       do_put = 1;
28709 +                       break;
28710 +               }
28711 +               if (do_put)
28712 +                       for (bindex = btop; bindex <= bbot; bindex++)
28713 +                               if (au_h_iptr(inode, bindex)) {
28714 +                                       do_put = 0;
28715 +                                       break;
28716 +                               }
28717 +       } else
28718 +               do_put = 1;
28719 +
28720 +       return do_put;
28721 +}
28722 +
28723 +/* free the plinks on a branch specified by @br_id */
28724 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28725 +{
28726 +       struct au_sbinfo *sbinfo;
28727 +       struct hlist_bl_head *hbl;
28728 +       struct hlist_bl_node *pos, *tmp;
28729 +       struct au_icntnr *icntnr;
28730 +       struct inode *inode;
28731 +       int i, do_put;
28732 +
28733 +       SiMustWriteLock(sb);
28734 +
28735 +       sbinfo = au_sbi(sb);
28736 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28737 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28738 +
28739 +       /* no bit_lock since sbinfo is write-locked */
28740 +       for (i = 0; i < AuPlink_NHASH; i++) {
28741 +               hbl = sbinfo->si_plink + i;
28742 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28743 +                       inode = au_igrab(&icntnr->vfs_inode);
28744 +                       ii_write_lock_child(inode);
28745 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28746 +                       if (do_put) {
28747 +                               hlist_bl_del(&icntnr->plink);
28748 +                               iput(inode);
28749 +                       }
28750 +                       ii_write_unlock(inode);
28751 +                       iput(inode);
28752 +               }
28753 +       }
28754 +}
28755 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28756 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28757 +++ linux/fs/aufs/poll.c        2024-05-13 17:13:00.621404284 +0200
28758 @@ -0,0 +1,51 @@
28759 +// SPDX-License-Identifier: GPL-2.0
28760 +/*
28761 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28762 + *
28763 + * This program is free software; you can redistribute it and/or modify
28764 + * it under the terms of the GNU General Public License as published by
28765 + * the Free Software Foundation; either version 2 of the License, or
28766 + * (at your option) any later version.
28767 + *
28768 + * This program is distributed in the hope that it will be useful,
28769 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28770 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28771 + * GNU General Public License for more details.
28772 + *
28773 + * You should have received a copy of the GNU General Public License
28774 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28775 + */
28776 +
28777 +/*
28778 + * poll operation
28779 + * There is only one filesystem which implements ->poll operation, currently.
28780 + */
28781 +
28782 +#include "aufs.h"
28783 +
28784 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28785 +{
28786 +       __poll_t mask;
28787 +       struct file *h_file;
28788 +       struct super_block *sb;
28789 +
28790 +       /* We should pretend an error happened. */
28791 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28792 +       sb = file->f_path.dentry->d_sb;
28793 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28794 +
28795 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28796 +       if (IS_ERR(h_file)) {
28797 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28798 +               goto out;
28799 +       }
28800 +
28801 +       mask = vfs_poll(h_file, pt);
28802 +       fput(h_file); /* instead of au_read_post() */
28803 +
28804 +out:
28805 +       si_read_unlock(sb);
28806 +       if (mask & EPOLLERR)
28807 +               AuDbg("mask 0x%x\n", mask);
28808 +       return mask;
28809 +}
28810 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28811 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28812 +++ linux/fs/aufs/posix_acl.c   2024-05-13 17:13:00.621404284 +0200
28813 @@ -0,0 +1,108 @@
28814 +// SPDX-License-Identifier: GPL-2.0
28815 +/*
28816 + * Copyright (C) 2014-2022 Junjiro R. Okajima
28817 + *
28818 + * This program is free software; you can redistribute it and/or modify
28819 + * it under the terms of the GNU General Public License as published by
28820 + * the Free Software Foundation; either version 2 of the License, or
28821 + * (at your option) any later version.
28822 + *
28823 + * This program is distributed in the hope that it will be useful,
28824 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28825 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28826 + * GNU General Public License for more details.
28827 + *
28828 + * You should have received a copy of the GNU General Public License
28829 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28830 + */
28831 +
28832 +/*
28833 + * posix acl operations
28834 + */
28835 +
28836 +#include <linux/fs.h>
28837 +#include "aufs.h"
28838 +
28839 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu)
28840 +{
28841 +       struct posix_acl *acl;
28842 +       int err;
28843 +       aufs_bindex_t bindex;
28844 +       struct inode *h_inode;
28845 +       struct super_block *sb;
28846 +
28847 +       acl = ERR_PTR(-ECHILD);
28848 +       if (rcu)
28849 +               goto out;
28850 +
28851 +       acl = NULL;
28852 +       sb = inode->i_sb;
28853 +       si_read_lock(sb, AuLock_FLUSH);
28854 +       ii_read_lock_child(inode);
28855 +       if (!(sb->s_flags & SB_POSIXACL))
28856 +               goto unlock;
28857 +
28858 +       bindex = au_ibtop(inode);
28859 +       h_inode = au_h_iptr(inode, bindex);
28860 +       if (unlikely(!h_inode
28861 +                    || ((h_inode->i_mode & S_IFMT)
28862 +                        != (inode->i_mode & S_IFMT)))) {
28863 +               err = au_busy_or_stale();
28864 +               acl = ERR_PTR(err);
28865 +               goto unlock;
28866 +       }
28867 +
28868 +       /* always topmost only */
28869 +       acl = get_inode_acl(h_inode, type);
28870 +       if (IS_ERR(acl))
28871 +               forget_cached_acl(inode, type);
28872 +       else
28873 +               set_cached_acl(inode, type, acl);
28874 +
28875 +unlock:
28876 +       ii_read_unlock(inode);
28877 +       si_read_unlock(sb);
28878 +
28879 +out:
28880 +       AuTraceErrPtr(acl);
28881 +       return acl;
28882 +}
28883 +
28884 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
28885 +                              struct dentry *dentry, int type)
28886 +{
28887 +       struct posix_acl *acl;
28888 +       struct inode *inode;
28889 +
28890 +       inode = d_inode(dentry);
28891 +       acl = aufs_get_inode_acl(inode, type, /*rcu*/false);
28892 +
28893 +       return acl;
28894 +}
28895 +
28896 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
28897 +                struct posix_acl *acl, int type)
28898 +{
28899 +       int err;
28900 +       ssize_t ssz;
28901 +       struct inode *inode;
28902 +       struct au_sxattr arg = {
28903 +               .type = AU_ACL_SET,
28904 +               .u.acl_set = {
28905 +                       .acl    = acl,
28906 +                       .type   = type
28907 +               },
28908 +       };
28909 +
28910 +       inode = d_inode(dentry);
28911 +       IMustLock(inode);
28912 +
28913 +       ssz = au_sxattr(dentry, inode, &arg);
28914 +       /* forget even it if succeeds since the branch might set differently */
28915 +       forget_cached_acl(inode, type);
28916 +       err = ssz;
28917 +       if (ssz >= 0)
28918 +               err = 0;
28919 +
28920 +       return err;
28921 +}
28922 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28923 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28924 +++ linux/fs/aufs/procfs.c      2024-05-13 17:13:00.621404284 +0200
28925 @@ -0,0 +1,170 @@
28926 +// SPDX-License-Identifier: GPL-2.0
28927 +/*
28928 + * Copyright (C) 2010-2022 Junjiro R. Okajima
28929 + *
28930 + * This program is free software; you can redistribute it and/or modify
28931 + * it under the terms of the GNU General Public License as published by
28932 + * the Free Software Foundation; either version 2 of the License, or
28933 + * (at your option) any later version.
28934 + *
28935 + * This program is distributed in the hope that it will be useful,
28936 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28937 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28938 + * GNU General Public License for more details.
28939 + *
28940 + * You should have received a copy of the GNU General Public License
28941 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28942 + */
28943 +
28944 +/*
28945 + * procfs interfaces
28946 + */
28947 +
28948 +#include <linux/proc_fs.h>
28949 +#include "aufs.h"
28950 +
28951 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28952 +{
28953 +       struct au_sbinfo *sbinfo;
28954 +
28955 +       sbinfo = file->private_data;
28956 +       if (sbinfo) {
28957 +               au_plink_maint_leave(sbinfo);
28958 +               kobject_put(&sbinfo->si_kobj);
28959 +       }
28960 +
28961 +       return 0;
28962 +}
28963 +
28964 +static void au_procfs_plm_write_clean(struct file *file)
28965 +{
28966 +       struct au_sbinfo *sbinfo;
28967 +
28968 +       sbinfo = file->private_data;
28969 +       if (sbinfo)
28970 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28971 +}
28972 +
28973 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28974 +{
28975 +       int err;
28976 +       struct super_block *sb;
28977 +       struct au_sbinfo *sbinfo;
28978 +       struct hlist_bl_node *pos;
28979 +
28980 +       err = -EBUSY;
28981 +       if (unlikely(file->private_data))
28982 +               goto out;
28983 +
28984 +       sb = NULL;
28985 +       /* don't use au_sbilist_lock() here */
28986 +       hlist_bl_lock(&au_sbilist);
28987 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28988 +               if (id == sysaufs_si_id(sbinfo)) {
28989 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28990 +                               sb = sbinfo->si_sb;
28991 +                       break;
28992 +               }
28993 +       hlist_bl_unlock(&au_sbilist);
28994 +
28995 +       err = -EINVAL;
28996 +       if (unlikely(!sb))
28997 +               goto out;
28998 +
28999 +       err = au_plink_maint_enter(sb);
29000 +       if (!err)
29001 +               /* keep kobject_get() */
29002 +               file->private_data = sbinfo;
29003 +       else
29004 +               kobject_put(&sbinfo->si_kobj);
29005 +out:
29006 +       return err;
29007 +}
29008 +
29009 +/*
29010 + * Accept a valid "si=xxxx" only.
29011 + * Once it is accepted successfully, accept "clean" too.
29012 + */
29013 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
29014 +                                  size_t count, loff_t *ppos)
29015 +{
29016 +       ssize_t err;
29017 +       unsigned long id;
29018 +       /* last newline is allowed */
29019 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
29020 +
29021 +       err = -EACCES;
29022 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
29023 +               goto out;
29024 +
29025 +       err = -EINVAL;
29026 +       if (unlikely(count > sizeof(buf)))
29027 +               goto out;
29028 +
29029 +       err = copy_from_user(buf, ubuf, count);
29030 +       if (unlikely(err)) {
29031 +               err = -EFAULT;
29032 +               goto out;
29033 +       }
29034 +       buf[count] = 0;
29035 +
29036 +       err = -EINVAL;
29037 +       if (!strcmp("clean", buf)) {
29038 +               au_procfs_plm_write_clean(file);
29039 +               goto out_success;
29040 +       } else if (unlikely(strncmp("si=", buf, 3)))
29041 +               goto out;
29042 +
29043 +       err = kstrtoul(buf + 3, 16, &id);
29044 +       if (unlikely(err))
29045 +               goto out;
29046 +
29047 +       err = au_procfs_plm_write_si(file, id);
29048 +       if (unlikely(err))
29049 +               goto out;
29050 +
29051 +out_success:
29052 +       err = count; /* success */
29053 +out:
29054 +       return err;
29055 +}
29056 +
29057 +static const struct proc_ops au_procfs_plm_op = {
29058 +       .proc_write     = au_procfs_plm_write,
29059 +       .proc_release   = au_procfs_plm_release
29060 +};
29061 +
29062 +/* ---------------------------------------------------------------------- */
29063 +
29064 +static struct proc_dir_entry *au_procfs_dir;
29065 +
29066 +void au_procfs_fin(void)
29067 +{
29068 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
29069 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29070 +}
29071 +
29072 +int __init au_procfs_init(void)
29073 +{
29074 +       int err;
29075 +       struct proc_dir_entry *entry;
29076 +
29077 +       err = -ENOMEM;
29078 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
29079 +       if (unlikely(!au_procfs_dir))
29080 +               goto out;
29081 +
29082 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
29083 +                           au_procfs_dir, &au_procfs_plm_op);
29084 +       if (unlikely(!entry))
29085 +               goto out_dir;
29086 +
29087 +       err = 0;
29088 +       goto out; /* success */
29089 +
29090 +
29091 +out_dir:
29092 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29093 +out:
29094 +       return err;
29095 +}
29096 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
29097 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
29098 +++ linux/fs/aufs/rdu.c 2024-05-13 17:13:00.621404284 +0200
29099 @@ -0,0 +1,384 @@
29100 +// SPDX-License-Identifier: GPL-2.0
29101 +/*
29102 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29103 + *
29104 + * This program is free software; you can redistribute it and/or modify
29105 + * it under the terms of the GNU General Public License as published by
29106 + * the Free Software Foundation; either version 2 of the License, or
29107 + * (at your option) any later version.
29108 + *
29109 + * This program is distributed in the hope that it will be useful,
29110 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29111 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29112 + * GNU General Public License for more details.
29113 + *
29114 + * You should have received a copy of the GNU General Public License
29115 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29116 + */
29117 +
29118 +/*
29119 + * readdir in userspace.
29120 + */
29121 +
29122 +#include <linux/compat.h>
29123 +#include <linux/fs_stack.h>
29124 +#include <linux/security.h>
29125 +#include "aufs.h"
29126 +
29127 +/* bits for struct aufs_rdu.flags */
29128 +#define        AuRdu_CALLED    BIT(0)
29129 +#define        AuRdu_CONT      BIT(1)
29130 +#define        AuRdu_FULL      BIT(2)
29131 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
29132 +#define au_fset_rdu(flags, name) \
29133 +       do { (flags) |= AuRdu_##name; } while (0)
29134 +#define au_fclr_rdu(flags, name) \
29135 +       do { (flags) &= ~AuRdu_##name; } while (0)
29136 +
29137 +struct au_rdu_arg {
29138 +       struct dir_context              ctx;
29139 +       struct aufs_rdu                 *rdu;
29140 +       union au_rdu_ent_ul             ent;
29141 +       unsigned long                   end;
29142 +
29143 +       struct super_block              *sb;
29144 +       int                             err;
29145 +};
29146 +
29147 +static bool au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29148 +                       loff_t offset, u64 h_ino, unsigned int d_type)
29149 +{
29150 +       int err, len;
29151 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
29152 +       struct aufs_rdu *rdu = arg->rdu;
29153 +       struct au_rdu_ent ent;
29154 +
29155 +       err = 0;
29156 +       arg->err = 0;
29157 +       au_fset_rdu(rdu->cookie.flags, CALLED);
29158 +       len = au_rdu_len(nlen);
29159 +       if (arg->ent.ul + len  < arg->end) {
29160 +               ent.ino = h_ino;
29161 +               ent.bindex = rdu->cookie.bindex;
29162 +               ent.type = d_type;
29163 +               ent.nlen = nlen;
29164 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
29165 +                       ent.type = DT_UNKNOWN;
29166 +
29167 +               /* unnecessary to support mmap_sem since this is a dir */
29168 +               err = -EFAULT;
29169 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29170 +                       goto out;
29171 +               if (copy_to_user(arg->ent.e->name, name, nlen))
29172 +                       goto out;
29173 +               /* the terminating NULL */
29174 +               if (__put_user(0, arg->ent.e->name + nlen))
29175 +                       goto out;
29176 +               err = 0;
29177 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29178 +               arg->ent.ul += len;
29179 +               rdu->rent++;
29180 +       } else {
29181 +               err = -EFAULT;
29182 +               au_fset_rdu(rdu->cookie.flags, FULL);
29183 +               rdu->full = 1;
29184 +               rdu->tail = arg->ent;
29185 +       }
29186 +
29187 +out:
29188 +       /* AuTraceErr(err); */
29189 +       return !err;
29190 +}
29191 +
29192 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29193 +{
29194 +       int err;
29195 +       loff_t offset;
29196 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29197 +
29198 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
29199 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29200 +       err = offset;
29201 +       if (unlikely(offset != cookie->h_pos))
29202 +               goto out;
29203 +
29204 +       err = 0;
29205 +       do {
29206 +               arg->err = 0;
29207 +               au_fclr_rdu(cookie->flags, CALLED);
29208 +               /* smp_mb(); */
29209 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
29210 +               if (err >= 0)
29211 +                       err = arg->err;
29212 +       } while (!err
29213 +                && au_ftest_rdu(cookie->flags, CALLED)
29214 +                && !au_ftest_rdu(cookie->flags, FULL));
29215 +       cookie->h_pos = h_file->f_pos;
29216 +
29217 +out:
29218 +       AuTraceErr(err);
29219 +       return err;
29220 +}
29221 +
29222 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29223 +{
29224 +       int err;
29225 +       aufs_bindex_t bbot;
29226 +       struct au_rdu_arg arg = {
29227 +               .ctx = {
29228 +                       .actor = au_rdu_fill
29229 +               }
29230 +       };
29231 +       struct dentry *dentry;
29232 +       struct inode *inode;
29233 +       struct file *h_file;
29234 +       struct au_rdu_cookie *cookie = &rdu->cookie;
29235 +
29236 +       /* VERIFY_WRITE */
29237 +       err = !access_ok(rdu->ent.e, rdu->sz);
29238 +       if (unlikely(err)) {
29239 +               err = -EFAULT;
29240 +               AuTraceErr(err);
29241 +               goto out;
29242 +       }
29243 +       rdu->rent = 0;
29244 +       rdu->tail = rdu->ent;
29245 +       rdu->full = 0;
29246 +       arg.rdu = rdu;
29247 +       arg.ent = rdu->ent;
29248 +       arg.end = arg.ent.ul;
29249 +       arg.end += rdu->sz;
29250 +
29251 +       err = -ENOTDIR;
29252 +       if (unlikely(!file->f_op->iterate_shared))
29253 +               goto out;
29254 +
29255 +       err = security_file_permission(file, MAY_READ);
29256 +       AuTraceErr(err);
29257 +       if (unlikely(err))
29258 +               goto out;
29259 +
29260 +       dentry = file->f_path.dentry;
29261 +       inode = d_inode(dentry);
29262 +       inode_lock_shared(inode);
29263 +
29264 +       arg.sb = inode->i_sb;
29265 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29266 +       if (unlikely(err))
29267 +               goto out_mtx;
29268 +       err = au_alive_dir(dentry);
29269 +       if (unlikely(err))
29270 +               goto out_si;
29271 +       /* todo: reval? */
29272 +       fi_read_lock(file);
29273 +
29274 +       err = -EAGAIN;
29275 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29276 +                    && cookie->generation != au_figen(file)))
29277 +               goto out_unlock;
29278 +
29279 +       err = 0;
29280 +       if (!rdu->blk) {
29281 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
29282 +               if (!rdu->blk)
29283 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
29284 +       }
29285 +       bbot = au_fbtop(file);
29286 +       if (cookie->bindex < bbot)
29287 +               cookie->bindex = bbot;
29288 +       bbot = au_fbbot_dir(file);
29289 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29290 +       for (; !err && cookie->bindex <= bbot;
29291 +            cookie->bindex++, cookie->h_pos = 0) {
29292 +               h_file = au_hf_dir(file, cookie->bindex);
29293 +               if (!h_file)
29294 +                       continue;
29295 +
29296 +               au_fclr_rdu(cookie->flags, FULL);
29297 +               err = au_rdu_do(h_file, &arg);
29298 +               AuTraceErr(err);
29299 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29300 +                       break;
29301 +       }
29302 +       AuDbg("rent %llu\n", rdu->rent);
29303 +
29304 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29305 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29306 +               au_fset_rdu(cookie->flags, CONT);
29307 +               cookie->generation = au_figen(file);
29308 +       }
29309 +
29310 +       ii_read_lock_child(inode);
29311 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
29312 +       ii_read_unlock(inode);
29313 +
29314 +out_unlock:
29315 +       fi_read_unlock(file);
29316 +out_si:
29317 +       si_read_unlock(arg.sb);
29318 +out_mtx:
29319 +       inode_unlock_shared(inode);
29320 +out:
29321 +       AuTraceErr(err);
29322 +       return err;
29323 +}
29324 +
29325 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29326 +{
29327 +       int err;
29328 +       ino_t ino;
29329 +       unsigned long long nent;
29330 +       union au_rdu_ent_ul *u;
29331 +       struct au_rdu_ent ent;
29332 +       struct super_block *sb;
29333 +
29334 +       err = 0;
29335 +       nent = rdu->nent;
29336 +       u = &rdu->ent;
29337 +       sb = file->f_path.dentry->d_sb;
29338 +       si_read_lock(sb, AuLock_FLUSH);
29339 +       while (nent-- > 0) {
29340 +               /* unnecessary to support mmap_sem since this is a dir */
29341 +               err = copy_from_user(&ent, u->e, sizeof(ent));
29342 +               if (!err)
29343 +                       /* VERIFY_WRITE */
29344 +                       err = !access_ok(&u->e->ino, sizeof(ino));
29345 +               if (unlikely(err)) {
29346 +                       err = -EFAULT;
29347 +                       AuTraceErr(err);
29348 +                       break;
29349 +               }
29350 +
29351 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29352 +               if (!ent.wh)
29353 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29354 +               else
29355 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29356 +                                       &ino);
29357 +               if (unlikely(err)) {
29358 +                       AuTraceErr(err);
29359 +                       break;
29360 +               }
29361 +
29362 +               err = __put_user(ino, &u->e->ino);
29363 +               if (unlikely(err)) {
29364 +                       err = -EFAULT;
29365 +                       AuTraceErr(err);
29366 +                       break;
29367 +               }
29368 +               u->ul += au_rdu_len(ent.nlen);
29369 +       }
29370 +       si_read_unlock(sb);
29371 +
29372 +       return err;
29373 +}
29374 +
29375 +/* ---------------------------------------------------------------------- */
29376 +
29377 +static int au_rdu_verify(struct aufs_rdu *rdu)
29378 +{
29379 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29380 +             "%llu, b%d, 0x%x, g%u}\n",
29381 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29382 +             rdu->blk,
29383 +             rdu->rent, rdu->shwh, rdu->full,
29384 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29385 +             rdu->cookie.generation);
29386 +
29387 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29388 +               return 0;
29389 +
29390 +       AuDbg("%u:%u\n",
29391 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29392 +       return -EINVAL;
29393 +}
29394 +
29395 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29396 +{
29397 +       long err, e;
29398 +       struct aufs_rdu rdu;
29399 +       void __user *p = (void __user *)arg;
29400 +
29401 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29402 +       if (unlikely(err)) {
29403 +               err = -EFAULT;
29404 +               AuTraceErr(err);
29405 +               goto out;
29406 +       }
29407 +       err = au_rdu_verify(&rdu);
29408 +       if (unlikely(err))
29409 +               goto out;
29410 +
29411 +       switch (cmd) {
29412 +       case AUFS_CTL_RDU:
29413 +               err = au_rdu(file, &rdu);
29414 +               if (unlikely(err))
29415 +                       break;
29416 +
29417 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29418 +               if (unlikely(e)) {
29419 +                       err = -EFAULT;
29420 +                       AuTraceErr(err);
29421 +               }
29422 +               break;
29423 +       case AUFS_CTL_RDU_INO:
29424 +               err = au_rdu_ino(file, &rdu);
29425 +               break;
29426 +
29427 +       default:
29428 +               /* err = -ENOTTY; */
29429 +               err = -EINVAL;
29430 +       }
29431 +
29432 +out:
29433 +       AuTraceErr(err);
29434 +       return err;
29435 +}
29436 +
29437 +#ifdef CONFIG_COMPAT
29438 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29439 +{
29440 +       long err, e;
29441 +       struct aufs_rdu rdu;
29442 +       void __user *p = compat_ptr(arg);
29443 +
29444 +       /* todo: get_user()? */
29445 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29446 +       if (unlikely(err)) {
29447 +               err = -EFAULT;
29448 +               AuTraceErr(err);
29449 +               goto out;
29450 +       }
29451 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29452 +       err = au_rdu_verify(&rdu);
29453 +       if (unlikely(err))
29454 +               goto out;
29455 +
29456 +       switch (cmd) {
29457 +       case AUFS_CTL_RDU:
29458 +               err = au_rdu(file, &rdu);
29459 +               if (unlikely(err))
29460 +                       break;
29461 +
29462 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29463 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29464 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29465 +               if (unlikely(e)) {
29466 +                       err = -EFAULT;
29467 +                       AuTraceErr(err);
29468 +               }
29469 +               break;
29470 +       case AUFS_CTL_RDU_INO:
29471 +               err = au_rdu_ino(file, &rdu);
29472 +               break;
29473 +
29474 +       default:
29475 +               /* err = -ENOTTY; */
29476 +               err = -EINVAL;
29477 +       }
29478 +
29479 +out:
29480 +       AuTraceErr(err);
29481 +       return err;
29482 +}
29483 +#endif
29484 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29485 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29486 +++ linux/fs/aufs/rwsem.h       2024-05-13 17:13:00.621404284 +0200
29487 @@ -0,0 +1,85 @@
29488 +/* SPDX-License-Identifier: GPL-2.0 */
29489 +/*
29490 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29491 + *
29492 + * This program is free software; you can redistribute it and/or modify
29493 + * it under the terms of the GNU General Public License as published by
29494 + * the Free Software Foundation; either version 2 of the License, or
29495 + * (at your option) any later version.
29496 + *
29497 + * This program is distributed in the hope that it will be useful,
29498 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29499 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29500 + * GNU General Public License for more details.
29501 + *
29502 + * You should have received a copy of the GNU General Public License
29503 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29504 + */
29505 +
29506 +/*
29507 + * simple read-write semaphore wrappers
29508 + */
29509 +
29510 +#ifndef __AUFS_RWSEM_H__
29511 +#define __AUFS_RWSEM_H__
29512 +
29513 +#ifdef __KERNEL__
29514 +
29515 +#include "debug.h"
29516 +
29517 +/* in the future, the name 'au_rwsem' will be totally gone */
29518 +#define au_rwsem       rw_semaphore
29519 +
29520 +/* to debug easier, do not make them inlined functions */
29521 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29522 +
29523 +#ifdef CONFIG_LOCKDEP
29524 +/* rwsem_is_locked() is unusable */
29525 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29526 +                                         && !lockdep_recursing(current) \
29527 +                                         && debug_locks                \
29528 +                                         && !lockdep_is_held_type(rw, 1))
29529 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29530 +                                         && !lockdep_recursing(current) \
29531 +                                         && debug_locks                \
29532 +                                         && !lockdep_is_held_type(rw, 0))
29533 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29534 +                                         && !lockdep_recursing(current) \
29535 +                                         && debug_locks                \
29536 +                                         && !lockdep_is_held(rw))
29537 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29538 +                                         && !lockdep_recursing(current) \
29539 +                                         && debug_locks                \
29540 +                                         && lockdep_is_held(rw))
29541 +#else
29542 +#define AuRwMustReadLock(rw)   do {} while (0)
29543 +#define AuRwMustWriteLock(rw)  do {} while (0)
29544 +#define AuRwMustAnyLock(rw)    do {} while (0)
29545 +#define AuRwDestroy(rw)                do {} while (0)
29546 +#endif
29547 +
29548 +#define au_rw_init(rw) init_rwsem(rw)
29549 +
29550 +#define au_rw_init_wlock(rw) do {              \
29551 +               au_rw_init(rw);                 \
29552 +               down_write(rw);                 \
29553 +       } while (0)
29554 +
29555 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29556 +               au_rw_init(rw);                 \
29557 +               down_write_nested(rw, lsc);     \
29558 +       } while (0)
29559 +
29560 +#define au_rw_read_lock(rw)            down_read(rw)
29561 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29562 +#define au_rw_read_unlock(rw)          up_read(rw)
29563 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29564 +#define au_rw_write_lock(rw)           down_write(rw)
29565 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29566 +#define au_rw_write_unlock(rw)         up_write(rw)
29567 +/* why is not _nested version defined? */
29568 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29569 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29570 +
29571 +#endif /* __KERNEL__ */
29572 +#endif /* __AUFS_RWSEM_H__ */
29573 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29574 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29575 +++ linux/fs/aufs/sbinfo.c      2024-05-13 17:13:00.621404284 +0200
29576 @@ -0,0 +1,316 @@
29577 +// SPDX-License-Identifier: GPL-2.0
29578 +/*
29579 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29580 + *
29581 + * This program is free software; you can redistribute it and/or modify
29582 + * it under the terms of the GNU General Public License as published by
29583 + * the Free Software Foundation; either version 2 of the License, or
29584 + * (at your option) any later version.
29585 + *
29586 + * This program is distributed in the hope that it will be useful,
29587 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29588 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29589 + * GNU General Public License for more details.
29590 + *
29591 + * You should have received a copy of the GNU General Public License
29592 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29593 + */
29594 +
29595 +/*
29596 + * superblock private data
29597 + */
29598 +
29599 +#include <linux/iversion.h>
29600 +#include "aufs.h"
29601 +
29602 +/*
29603 + * they are necessary regardless sysfs is disabled.
29604 + */
29605 +void au_si_free(struct kobject *kobj)
29606 +{
29607 +       int i;
29608 +       struct au_sbinfo *sbinfo;
29609 +       char *locked __maybe_unused; /* debug only */
29610 +
29611 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29612 +       for (i = 0; i < AuPlink_NHASH; i++)
29613 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29614 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29615 +
29616 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29617 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29618 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29619 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29620 +
29621 +       dbgaufs_si_fin(sbinfo);
29622 +       au_rw_write_lock(&sbinfo->si_rwsem);
29623 +       au_br_free(sbinfo);
29624 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29625 +
29626 +       au_kfree_try_rcu(sbinfo->si_branch);
29627 +       mutex_destroy(&sbinfo->si_xib_mtx);
29628 +       AuRwDestroy(&sbinfo->si_rwsem);
29629 +
29630 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29631 +       /* si_nfiles is waited too */
29632 +       au_kfree_rcu(sbinfo);
29633 +}
29634 +
29635 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29636 +{
29637 +       struct au_sbinfo *sbinfo;
29638 +       int err, i;
29639 +
29640 +       err = -ENOMEM;
29641 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29642 +       if (unlikely(!sbinfo))
29643 +               goto out;
29644 +
29645 +       /* will be reallocated separately */
29646 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29647 +       if (unlikely(!sbinfo->si_branch))
29648 +               goto out_sbinfo;
29649 +
29650 +       err = sysaufs_si_init(sbinfo);
29651 +       if (!err) {
29652 +               dbgaufs_si_null(sbinfo);
29653 +               err = dbgaufs_si_init(sbinfo);
29654 +               if (unlikely(err))
29655 +                       kobject_put(&sbinfo->si_kobj);
29656 +       }
29657 +       if (unlikely(err))
29658 +               goto out_br;
29659 +
29660 +       au_nwt_init(&sbinfo->si_nowait);
29661 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29662 +
29663 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29664 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29665 +
29666 +       sbinfo->si_bbot = -1;
29667 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29668 +
29669 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29670 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29671 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29672 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29673 +
29674 +       au_fhsm_init(sbinfo);
29675 +
29676 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29677 +
29678 +       sbinfo->si_xino_jiffy = jiffies;
29679 +       sbinfo->si_xino_expire
29680 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29681 +       mutex_init(&sbinfo->si_xib_mtx);
29682 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29683 +
29684 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29685 +
29686 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29687 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29688 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29689 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29690 +
29691 +       for (i = 0; i < AuPlink_NHASH; i++)
29692 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29693 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29694 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29695 +
29696 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29697 +
29698 +       /* with getattr by default */
29699 +       sbinfo->si_iop_array = aufs_iop;
29700 +
29701 +       /* leave other members for sysaufs and si_mnt. */
29702 +       sbinfo->si_sb = sb;
29703 +       if (sb) {
29704 +               sb->s_fs_info = sbinfo;
29705 +               si_pid_set(sb);
29706 +       }
29707 +       return sbinfo; /* success */
29708 +
29709 +out_br:
29710 +       au_kfree_try_rcu(sbinfo->si_branch);
29711 +out_sbinfo:
29712 +       au_kfree_rcu(sbinfo);
29713 +out:
29714 +       return ERR_PTR(err);
29715 +}
29716 +
29717 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29718 +{
29719 +       int err, sz;
29720 +       struct au_branch **brp;
29721 +
29722 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29723 +
29724 +       err = -ENOMEM;
29725 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29726 +       if (unlikely(!sz))
29727 +               sz = sizeof(*brp);
29728 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29729 +                          may_shrink);
29730 +       if (brp) {
29731 +               sbinfo->si_branch = brp;
29732 +               err = 0;
29733 +       }
29734 +
29735 +       return err;
29736 +}
29737 +
29738 +/* ---------------------------------------------------------------------- */
29739 +
29740 +unsigned int au_sigen_inc(struct super_block *sb)
29741 +{
29742 +       unsigned int gen;
29743 +       struct inode *inode;
29744 +
29745 +       SiMustWriteLock(sb);
29746 +
29747 +       gen = ++au_sbi(sb)->si_generation;
29748 +       au_update_digen(sb->s_root);
29749 +       inode = d_inode(sb->s_root);
29750 +       au_update_iigen(inode, /*half*/0);
29751 +       inode_inc_iversion(inode);
29752 +       return gen;
29753 +}
29754 +
29755 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29756 +{
29757 +       aufs_bindex_t br_id;
29758 +       int i;
29759 +       struct au_sbinfo *sbinfo;
29760 +
29761 +       SiMustWriteLock(sb);
29762 +
29763 +       sbinfo = au_sbi(sb);
29764 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29765 +               br_id = ++sbinfo->si_last_br_id;
29766 +               AuDebugOn(br_id < 0);
29767 +               if (br_id && au_br_index(sb, br_id) < 0)
29768 +                       return br_id;
29769 +       }
29770 +
29771 +       return -1;
29772 +}
29773 +
29774 +/* ---------------------------------------------------------------------- */
29775 +
29776 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29777 +int si_read_lock(struct super_block *sb, int flags)
29778 +{
29779 +       int err;
29780 +
29781 +       err = 0;
29782 +       if (au_ftest_lock(flags, FLUSH))
29783 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29784 +
29785 +       si_noflush_read_lock(sb);
29786 +       err = au_plink_maint(sb, flags);
29787 +       if (unlikely(err))
29788 +               si_read_unlock(sb);
29789 +
29790 +       return err;
29791 +}
29792 +
29793 +int si_write_lock(struct super_block *sb, int flags)
29794 +{
29795 +       int err;
29796 +
29797 +       if (au_ftest_lock(flags, FLUSH))
29798 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29799 +
29800 +       si_noflush_write_lock(sb);
29801 +       err = au_plink_maint(sb, flags);
29802 +       if (unlikely(err))
29803 +               si_write_unlock(sb);
29804 +
29805 +       return err;
29806 +}
29807 +
29808 +/* dentry and super_block lock. call at entry point */
29809 +int aufs_read_lock(struct dentry *dentry, int flags)
29810 +{
29811 +       int err;
29812 +       struct super_block *sb;
29813 +
29814 +       sb = dentry->d_sb;
29815 +       err = si_read_lock(sb, flags);
29816 +       if (unlikely(err))
29817 +               goto out;
29818 +
29819 +       if (au_ftest_lock(flags, DW))
29820 +               di_write_lock_child(dentry);
29821 +       else
29822 +               di_read_lock_child(dentry, flags);
29823 +
29824 +       if (au_ftest_lock(flags, GEN)) {
29825 +               err = au_digen_test(dentry, au_sigen(sb));
29826 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29827 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29828 +               else if (!err)
29829 +                       err = au_dbrange_test(dentry);
29830 +               if (unlikely(err))
29831 +                       aufs_read_unlock(dentry, flags);
29832 +       }
29833 +
29834 +out:
29835 +       return err;
29836 +}
29837 +
29838 +void aufs_read_unlock(struct dentry *dentry, int flags)
29839 +{
29840 +       if (au_ftest_lock(flags, DW))
29841 +               di_write_unlock(dentry);
29842 +       else
29843 +               di_read_unlock(dentry, flags);
29844 +       si_read_unlock(dentry->d_sb);
29845 +}
29846 +
29847 +void aufs_write_lock(struct dentry *dentry)
29848 +{
29849 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29850 +       di_write_lock_child(dentry);
29851 +}
29852 +
29853 +void aufs_write_unlock(struct dentry *dentry)
29854 +{
29855 +       di_write_unlock(dentry);
29856 +       si_write_unlock(dentry->d_sb);
29857 +}
29858 +
29859 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29860 +{
29861 +       int err;
29862 +       unsigned int sigen;
29863 +       struct super_block *sb;
29864 +
29865 +       sb = d1->d_sb;
29866 +       err = si_read_lock(sb, flags);
29867 +       if (unlikely(err))
29868 +               goto out;
29869 +
29870 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29871 +
29872 +       if (au_ftest_lock(flags, GEN)) {
29873 +               sigen = au_sigen(sb);
29874 +               err = au_digen_test(d1, sigen);
29875 +               AuDebugOn(!err && au_dbrange_test(d1));
29876 +               if (!err) {
29877 +                       err = au_digen_test(d2, sigen);
29878 +                       AuDebugOn(!err && au_dbrange_test(d2));
29879 +               }
29880 +               if (unlikely(err))
29881 +                       aufs_read_and_write_unlock2(d1, d2);
29882 +       }
29883 +
29884 +out:
29885 +       return err;
29886 +}
29887 +
29888 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29889 +{
29890 +       di_write_unlock2(d1, d2);
29891 +       si_read_unlock(d1->d_sb);
29892 +}
29893 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29894 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29895 +++ linux/fs/aufs/super.c       2024-05-13 17:13:00.621404284 +0200
29896 @@ -0,0 +1,871 @@
29897 +// SPDX-License-Identifier: GPL-2.0
29898 +/*
29899 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29900 + *
29901 + * This program is free software; you can redistribute it and/or modify
29902 + * it under the terms of the GNU General Public License as published by
29903 + * the Free Software Foundation; either version 2 of the License, or
29904 + * (at your option) any later version.
29905 + *
29906 + * This program is distributed in the hope that it will be useful,
29907 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29908 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29909 + * GNU General Public License for more details.
29910 + *
29911 + * You should have received a copy of the GNU General Public License
29912 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29913 + */
29914 +
29915 +/*
29916 + * mount and super_block operations
29917 + */
29918 +
29919 +#include <linux/iversion.h>
29920 +#include <linux/mm.h>
29921 +#include <linux/seq_file.h>
29922 +#include <linux/statfs.h>
29923 +#include <linux/vmalloc.h>
29924 +#include "aufs.h"
29925 +
29926 +/*
29927 + * super_operations
29928 + */
29929 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29930 +{
29931 +       struct au_icntnr *c;
29932 +
29933 +       c = au_cache_alloc_icntnr(sb);
29934 +       if (c) {
29935 +               au_icntnr_init(c);
29936 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29937 +               c->iinfo.ii_hinode = NULL;
29938 +               return &c->vfs_inode;
29939 +       }
29940 +       return NULL;
29941 +}
29942 +
29943 +static void aufs_destroy_inode(struct inode *inode)
29944 +{
29945 +       if (!au_is_bad_inode(inode))
29946 +               au_iinfo_fin(inode);
29947 +}
29948 +
29949 +static void aufs_free_inode(struct inode *inode)
29950 +{
29951 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29952 +}
29953 +
29954 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29955 +{
29956 +       struct inode *inode;
29957 +       int err;
29958 +
29959 +       inode = iget_locked(sb, ino);
29960 +       if (unlikely(!inode)) {
29961 +               inode = ERR_PTR(-ENOMEM);
29962 +               goto out;
29963 +       }
29964 +       if (!(inode->i_state & I_NEW))
29965 +               goto out;
29966 +
29967 +       err = au_xigen_new(inode);
29968 +       if (!err)
29969 +               err = au_iinfo_init(inode);
29970 +       if (!err)
29971 +               inode_inc_iversion(inode);
29972 +       else {
29973 +               iget_failed(inode);
29974 +               inode = ERR_PTR(err);
29975 +       }
29976 +
29977 +out:
29978 +       /* never return NULL */
29979 +       AuDebugOn(!inode);
29980 +       AuTraceErrPtr(inode);
29981 +       return inode;
29982 +}
29983 +
29984 +/* lock free root dinfo */
29985 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29986 +{
29987 +       int err;
29988 +       aufs_bindex_t bindex, bbot;
29989 +       struct path path;
29990 +       struct au_hdentry *hdp;
29991 +       struct au_branch *br;
29992 +       au_br_perm_str_t perm;
29993 +
29994 +       err = 0;
29995 +       bbot = au_sbbot(sb);
29996 +       bindex = 0;
29997 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29998 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29999 +               br = au_sbr(sb, bindex);
30000 +               path.mnt = au_br_mnt(br);
30001 +               path.dentry = hdp->hd_dentry;
30002 +               err = au_seq_path(seq, &path);
30003 +               if (!err) {
30004 +                       au_optstr_br_perm(&perm, br->br_perm);
30005 +                       seq_printf(seq, "=%s", perm.a);
30006 +                       if (bindex != bbot)
30007 +                               seq_putc(seq, ':');
30008 +               }
30009 +       }
30010 +       if (unlikely(err || seq_has_overflowed(seq)))
30011 +               err = -E2BIG;
30012 +
30013 +       return err;
30014 +}
30015 +
30016 +static void au_gen_fmt(char *fmt, int len, const char *pat,
30017 +                      const char *append)
30018 +{
30019 +       char *p;
30020 +
30021 +       p = fmt;
30022 +       while (*pat != ':')
30023 +               *p++ = *pat++;
30024 +       *p++ = *pat++;
30025 +       strscpy(p, append, len - (p - fmt));
30026 +       AuDebugOn(strlen(fmt) >= len);
30027 +}
30028 +
30029 +static void au_show_wbr_create(struct seq_file *m, int v,
30030 +                              struct au_sbinfo *sbinfo)
30031 +{
30032 +       const char *pat;
30033 +       char fmt[32];
30034 +       struct au_wbr_mfs *mfs;
30035 +
30036 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30037 +
30038 +       seq_puts(m, ",create=");
30039 +       pat = au_optstr_wbr_create(v);
30040 +       mfs = &sbinfo->si_wbr_mfs;
30041 +       switch (v) {
30042 +       case AuWbrCreate_TDP:
30043 +       case AuWbrCreate_RR:
30044 +       case AuWbrCreate_MFS:
30045 +       case AuWbrCreate_PMFS:
30046 +               seq_puts(m, pat);
30047 +               break;
30048 +       case AuWbrCreate_MFSRR:
30049 +       case AuWbrCreate_TDMFS:
30050 +       case AuWbrCreate_PMFSRR:
30051 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
30052 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
30053 +               break;
30054 +       case AuWbrCreate_MFSV:
30055 +       case AuWbrCreate_PMFSV:
30056 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
30057 +               seq_printf(m, fmt,
30058 +                          jiffies_to_msecs(mfs->mfs_expire)
30059 +                          / MSEC_PER_SEC);
30060 +               break;
30061 +       case AuWbrCreate_MFSRRV:
30062 +       case AuWbrCreate_TDMFSV:
30063 +       case AuWbrCreate_PMFSRRV:
30064 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
30065 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
30066 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
30067 +               break;
30068 +       default:
30069 +               BUG();
30070 +       }
30071 +}
30072 +
30073 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
30074 +{
30075 +#ifdef CONFIG_SYSFS
30076 +       return 0;
30077 +#else
30078 +       int err;
30079 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
30080 +       aufs_bindex_t bindex, brid;
30081 +       struct qstr *name;
30082 +       struct file *f;
30083 +       struct dentry *d, *h_root;
30084 +       struct au_branch *br;
30085 +
30086 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30087 +
30088 +       err = 0;
30089 +       f = au_sbi(sb)->si_xib;
30090 +       if (!f)
30091 +               goto out;
30092 +
30093 +       /* stop printing the default xino path on the first writable branch */
30094 +       h_root = NULL;
30095 +       bindex = au_xi_root(sb, f->f_path.dentry);
30096 +       if (bindex >= 0) {
30097 +               br = au_sbr_sb(sb, bindex);
30098 +               h_root = au_br_dentry(br);
30099 +       }
30100 +
30101 +       d = f->f_path.dentry;
30102 +       name = &d->d_name;
30103 +       /* safe ->d_parent because the file is unlinked */
30104 +       if (d->d_parent == h_root
30105 +           && name->len == len
30106 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
30107 +               goto out;
30108 +
30109 +       seq_puts(seq, ",xino=");
30110 +       err = au_xino_path(seq, f);
30111 +
30112 +out:
30113 +       return err;
30114 +#endif
30115 +}
30116 +
30117 +/* seq_file will re-call me in case of too long string */
30118 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
30119 +{
30120 +       int err;
30121 +       unsigned int mnt_flags, v;
30122 +       struct super_block *sb;
30123 +       struct au_sbinfo *sbinfo;
30124 +
30125 +#define AuBool(name, str) do { \
30126 +       v = au_opt_test(mnt_flags, name); \
30127 +       if (v != au_opt_test(AuOpt_Def, name)) \
30128 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
30129 +} while (0)
30130 +
30131 +#define AuStr(name, str) do { \
30132 +       v = mnt_flags & AuOptMask_##name; \
30133 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
30134 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30135 +} while (0)
30136 +
30137 +#define AuUInt(name, str, val) do { \
30138 +       if (val != AUFS_##name##_DEF) \
30139 +               seq_printf(m, "," #str "=%u", val); \
30140 +} while (0)
30141 +
30142 +       sb = dentry->d_sb;
30143 +       if (sb->s_flags & SB_POSIXACL)
30144 +               seq_puts(m, ",acl");
30145 +#if 0 /* reserved for future use */
30146 +       if (sb->s_flags & SB_I_VERSION)
30147 +               seq_puts(m, ",i_version");
30148 +#endif
30149 +
30150 +       /* lock free root dinfo */
30151 +       si_noflush_read_lock(sb);
30152 +       sbinfo = au_sbi(sb);
30153 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30154 +
30155 +       mnt_flags = au_mntflags(sb);
30156 +       if (au_opt_test(mnt_flags, XINO)) {
30157 +               err = au_show_xino(m, sb);
30158 +               if (unlikely(err))
30159 +                       goto out;
30160 +       } else
30161 +               seq_puts(m, ",noxino");
30162 +
30163 +       AuBool(TRUNC_XINO, trunc_xino);
30164 +       AuStr(UDBA, udba);
30165 +       AuBool(SHWH, shwh);
30166 +       AuBool(PLINK, plink);
30167 +       AuBool(DIO, dio);
30168 +       AuBool(DIRPERM1, dirperm1);
30169 +
30170 +       v = sbinfo->si_wbr_create;
30171 +       if (v != AuWbrCreate_Def)
30172 +               au_show_wbr_create(m, v, sbinfo);
30173 +
30174 +       v = sbinfo->si_wbr_copyup;
30175 +       if (v != AuWbrCopyup_Def)
30176 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30177 +
30178 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30179 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30180 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30181 +
30182 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30183 +
30184 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30185 +       AuUInt(RDCACHE, rdcache, v);
30186 +
30187 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30188 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30189 +
30190 +       au_fhsm_show(m, sbinfo);
30191 +
30192 +       AuBool(DIRREN, dirren);
30193 +       AuBool(SUM, sum);
30194 +       /* AuBool(SUM_W, wsum); */
30195 +       AuBool(WARN_PERM, warn_perm);
30196 +       AuBool(VERBOSE, verbose);
30197 +
30198 +out:
30199 +       /* be sure to print "br:" last */
30200 +       if (!sysaufs_brs) {
30201 +               seq_puts(m, ",br:");
30202 +               au_show_brs(m, sb);
30203 +       }
30204 +       si_read_unlock(sb);
30205 +       return 0;
30206 +
30207 +#undef AuBool
30208 +#undef AuStr
30209 +#undef AuUInt
30210 +}
30211 +
30212 +/* ---------------------------------------------------------------------- */
30213 +
30214 +/* sum mode which returns the summation for statfs(2) */
30215 +
30216 +static u64 au_add_till_max(u64 a, u64 b)
30217 +{
30218 +       u64 old;
30219 +
30220 +       old = a;
30221 +       a += b;
30222 +       if (old <= a)
30223 +               return a;
30224 +       return ULLONG_MAX;
30225 +}
30226 +
30227 +static u64 au_mul_till_max(u64 a, long mul)
30228 +{
30229 +       u64 old;
30230 +
30231 +       old = a;
30232 +       a *= mul;
30233 +       if (old <= a)
30234 +               return a;
30235 +       return ULLONG_MAX;
30236 +}
30237 +
30238 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30239 +{
30240 +       int err;
30241 +       long bsize, factor;
30242 +       u64 blocks, bfree, bavail, files, ffree;
30243 +       aufs_bindex_t bbot, bindex, i;
30244 +       unsigned char shared;
30245 +       struct path h_path;
30246 +       struct super_block *h_sb;
30247 +
30248 +       err = 0;
30249 +       bsize = LONG_MAX;
30250 +       files = 0;
30251 +       ffree = 0;
30252 +       blocks = 0;
30253 +       bfree = 0;
30254 +       bavail = 0;
30255 +       bbot = au_sbbot(sb);
30256 +       for (bindex = 0; bindex <= bbot; bindex++) {
30257 +               h_path.mnt = au_sbr_mnt(sb, bindex);
30258 +               h_sb = h_path.mnt->mnt_sb;
30259 +               shared = 0;
30260 +               for (i = 0; !shared && i < bindex; i++)
30261 +                       shared = (au_sbr_sb(sb, i) == h_sb);
30262 +               if (shared)
30263 +                       continue;
30264 +
30265 +               /* sb->s_root for NFS is unreliable */
30266 +               h_path.dentry = h_path.mnt->mnt_root;
30267 +               err = vfs_statfs(&h_path, buf);
30268 +               if (unlikely(err))
30269 +                       goto out;
30270 +
30271 +               if (bsize > buf->f_bsize) {
30272 +                       /*
30273 +                        * we will reduce bsize, so we have to expand blocks
30274 +                        * etc. to match them again
30275 +                        */
30276 +                       factor = (bsize / buf->f_bsize);
30277 +                       blocks = au_mul_till_max(blocks, factor);
30278 +                       bfree = au_mul_till_max(bfree, factor);
30279 +                       bavail = au_mul_till_max(bavail, factor);
30280 +                       bsize = buf->f_bsize;
30281 +               }
30282 +
30283 +               factor = (buf->f_bsize / bsize);
30284 +               blocks = au_add_till_max(blocks,
30285 +                               au_mul_till_max(buf->f_blocks, factor));
30286 +               bfree = au_add_till_max(bfree,
30287 +                               au_mul_till_max(buf->f_bfree, factor));
30288 +               bavail = au_add_till_max(bavail,
30289 +                               au_mul_till_max(buf->f_bavail, factor));
30290 +               files = au_add_till_max(files, buf->f_files);
30291 +               ffree = au_add_till_max(ffree, buf->f_ffree);
30292 +       }
30293 +
30294 +       buf->f_bsize = bsize;
30295 +       buf->f_blocks = blocks;
30296 +       buf->f_bfree = bfree;
30297 +       buf->f_bavail = bavail;
30298 +       buf->f_files = files;
30299 +       buf->f_ffree = ffree;
30300 +       buf->f_frsize = 0;
30301 +
30302 +out:
30303 +       return err;
30304 +}
30305 +
30306 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30307 +{
30308 +       int err;
30309 +       struct path h_path;
30310 +       struct super_block *sb;
30311 +
30312 +       /* lock free root dinfo */
30313 +       sb = dentry->d_sb;
30314 +       si_noflush_read_lock(sb);
30315 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
30316 +               /* sb->s_root for NFS is unreliable */
30317 +               h_path.mnt = au_sbr_mnt(sb, 0);
30318 +               h_path.dentry = h_path.mnt->mnt_root;
30319 +               err = vfs_statfs(&h_path, buf);
30320 +       } else
30321 +               err = au_statfs_sum(sb, buf);
30322 +       si_read_unlock(sb);
30323 +
30324 +       if (!err) {
30325 +               buf->f_type = AUFS_SUPER_MAGIC;
30326 +               buf->f_namelen = AUFS_MAX_NAMELEN;
30327 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30328 +       }
30329 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30330 +
30331 +       return err;
30332 +}
30333 +
30334 +/* ---------------------------------------------------------------------- */
30335 +
30336 +static int aufs_sync_fs(struct super_block *sb, int wait)
30337 +{
30338 +       int err, e;
30339 +       aufs_bindex_t bbot, bindex;
30340 +       struct au_branch *br;
30341 +       struct super_block *h_sb;
30342 +
30343 +       err = 0;
30344 +       si_noflush_read_lock(sb);
30345 +       bbot = au_sbbot(sb);
30346 +       for (bindex = 0; bindex <= bbot; bindex++) {
30347 +               br = au_sbr(sb, bindex);
30348 +               if (!au_br_writable(br->br_perm))
30349 +                       continue;
30350 +
30351 +               h_sb = au_sbr_sb(sb, bindex);
30352 +               e = vfsub_sync_filesystem(h_sb);
30353 +               if (unlikely(e && !err))
30354 +                       err = e;
30355 +               /* go on even if an error happens */
30356 +       }
30357 +       si_read_unlock(sb);
30358 +
30359 +       return err;
30360 +}
30361 +
30362 +/* ---------------------------------------------------------------------- */
30363 +
30364 +/* final actions when unmounting a file system */
30365 +static void aufs_put_super(struct super_block *sb)
30366 +{
30367 +       struct au_sbinfo *sbinfo;
30368 +
30369 +       sbinfo = au_sbi(sb);
30370 +       if (sbinfo)
30371 +               kobject_put(&sbinfo->si_kobj);
30372 +}
30373 +
30374 +/* ---------------------------------------------------------------------- */
30375 +
30376 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30377 +                    struct super_block *sb, void *arg)
30378 +{
30379 +       void *array;
30380 +       unsigned long long n, sz;
30381 +
30382 +       array = NULL;
30383 +       n = 0;
30384 +       if (!*hint)
30385 +               goto out;
30386 +
30387 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30388 +               array = ERR_PTR(-EMFILE);
30389 +               pr_err("hint %llu\n", *hint);
30390 +               goto out;
30391 +       }
30392 +
30393 +       sz = sizeof(array) * *hint;
30394 +       array = kzalloc(sz, GFP_NOFS);
30395 +       if (unlikely(!array))
30396 +               array = vzalloc(sz);
30397 +       if (unlikely(!array)) {
30398 +               array = ERR_PTR(-ENOMEM);
30399 +               goto out;
30400 +       }
30401 +
30402 +       n = cb(sb, array, *hint, arg);
30403 +       AuDebugOn(n > *hint);
30404 +
30405 +out:
30406 +       *hint = n;
30407 +       return array;
30408 +}
30409 +
30410 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30411 +                                      unsigned long long max __maybe_unused,
30412 +                                      void *arg)
30413 +{
30414 +       unsigned long long n;
30415 +       struct inode **p, *inode;
30416 +       struct list_head *head;
30417 +
30418 +       n = 0;
30419 +       p = a;
30420 +       head = arg;
30421 +       spin_lock(&sb->s_inode_list_lock);
30422 +       list_for_each_entry(inode, head, i_sb_list) {
30423 +               if (!au_is_bad_inode(inode)
30424 +                   && au_ii(inode)->ii_btop >= 0) {
30425 +                       spin_lock(&inode->i_lock);
30426 +                       if (atomic_read(&inode->i_count)) {
30427 +                               au_igrab(inode);
30428 +                               *p++ = inode;
30429 +                               n++;
30430 +                               AuDebugOn(n > max);
30431 +                       }
30432 +                       spin_unlock(&inode->i_lock);
30433 +               }
30434 +       }
30435 +       spin_unlock(&sb->s_inode_list_lock);
30436 +
30437 +       return n;
30438 +}
30439 +
30440 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30441 +{
30442 +       struct au_sbinfo *sbi;
30443 +
30444 +       sbi = au_sbi(sb);
30445 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30446 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30447 +}
30448 +
30449 +void au_iarray_free(struct inode **a, unsigned long long max)
30450 +{
30451 +       unsigned long long ull;
30452 +
30453 +       for (ull = 0; ull < max; ull++)
30454 +               iput(a[ull]);
30455 +       kvfree(a);
30456 +}
30457 +
30458 +/* ---------------------------------------------------------------------- */
30459 +
30460 +/*
30461 + * refresh dentry and inode at remount time.
30462 + */
30463 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30464 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30465 +                     struct dentry *parent)
30466 +{
30467 +       int err;
30468 +
30469 +       di_write_lock_child(dentry);
30470 +       di_read_lock_parent(parent, AuLock_IR);
30471 +       err = au_refresh_dentry(dentry, parent);
30472 +       if (!err && dir_flags)
30473 +               au_hn_reset(d_inode(dentry), dir_flags);
30474 +       di_read_unlock(parent, AuLock_IR);
30475 +       di_write_unlock(dentry);
30476 +
30477 +       return err;
30478 +}
30479 +
30480 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30481 +                          struct au_sbinfo *sbinfo,
30482 +                          const unsigned int dir_flags, unsigned int do_idop)
30483 +{
30484 +       int err;
30485 +       struct dentry *parent;
30486 +
30487 +       err = 0;
30488 +       parent = dget_parent(dentry);
30489 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30490 +               if (d_really_is_positive(dentry)) {
30491 +                       if (!d_is_dir(dentry))
30492 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30493 +                                                parent);
30494 +                       else {
30495 +                               err = au_do_refresh(dentry, dir_flags, parent);
30496 +                               if (unlikely(err))
30497 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30498 +                       }
30499 +               } else
30500 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30501 +               AuDbgDentry(dentry);
30502 +       }
30503 +       dput(parent);
30504 +
30505 +       if (!err) {
30506 +               if (do_idop)
30507 +                       au_refresh_dop(dentry, /*force_reval*/0);
30508 +       } else
30509 +               au_refresh_dop(dentry, /*force_reval*/1);
30510 +
30511 +       AuTraceErr(err);
30512 +       return err;
30513 +}
30514 +
30515 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30516 +{
30517 +       int err, i, j, ndentry, e;
30518 +       unsigned int sigen;
30519 +       struct au_dcsub_pages dpages;
30520 +       struct au_dpage *dpage;
30521 +       struct dentry **dentries, *d;
30522 +       struct au_sbinfo *sbinfo;
30523 +       struct dentry *root = sb->s_root;
30524 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30525 +
30526 +       if (do_idop)
30527 +               au_refresh_dop(root, /*force_reval*/0);
30528 +
30529 +       err = au_dpages_init(&dpages, GFP_NOFS);
30530 +       if (unlikely(err))
30531 +               goto out;
30532 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30533 +       if (unlikely(err))
30534 +               goto out_dpages;
30535 +
30536 +       sigen = au_sigen(sb);
30537 +       sbinfo = au_sbi(sb);
30538 +       for (i = 0; i < dpages.ndpage; i++) {
30539 +               dpage = dpages.dpages + i;
30540 +               dentries = dpage->dentries;
30541 +               ndentry = dpage->ndentry;
30542 +               for (j = 0; j < ndentry; j++) {
30543 +                       d = dentries[j];
30544 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30545 +                                           do_idop);
30546 +                       if (unlikely(e && !err))
30547 +                               err = e;
30548 +                       /* go on even err */
30549 +               }
30550 +       }
30551 +
30552 +out_dpages:
30553 +       au_dpages_free(&dpages);
30554 +out:
30555 +       return err;
30556 +}
30557 +
30558 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30559 +{
30560 +       int err, e;
30561 +       unsigned int sigen;
30562 +       unsigned long long max, ull;
30563 +       struct inode *inode, **array;
30564 +
30565 +       array = au_iarray_alloc(sb, &max);
30566 +       err = PTR_ERR(array);
30567 +       if (IS_ERR(array))
30568 +               goto out;
30569 +
30570 +       err = 0;
30571 +       sigen = au_sigen(sb);
30572 +       for (ull = 0; ull < max; ull++) {
30573 +               inode = array[ull];
30574 +               if (unlikely(!inode))
30575 +                       break;
30576 +
30577 +               e = 0;
30578 +               ii_write_lock_child(inode);
30579 +               if (au_iigen(inode, NULL) != sigen) {
30580 +                       e = au_refresh_hinode_self(inode);
30581 +                       if (unlikely(e)) {
30582 +                               au_refresh_iop(inode, /*force_getattr*/1);
30583 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30584 +                               if (!err)
30585 +                                       err = e;
30586 +                               /* go on even if err */
30587 +                       }
30588 +               }
30589 +               if (!e && do_idop)
30590 +                       au_refresh_iop(inode, /*force_getattr*/0);
30591 +               ii_write_unlock(inode);
30592 +       }
30593 +
30594 +       au_iarray_free(array, max);
30595 +
30596 +out:
30597 +       return err;
30598 +}
30599 +
30600 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30601 +{
30602 +       int err, e;
30603 +       unsigned int udba;
30604 +       aufs_bindex_t bindex, bbot;
30605 +       struct dentry *root;
30606 +       struct inode *inode;
30607 +       struct au_branch *br;
30608 +       struct au_sbinfo *sbi;
30609 +
30610 +       au_sigen_inc(sb);
30611 +       sbi = au_sbi(sb);
30612 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30613 +
30614 +       root = sb->s_root;
30615 +       DiMustNoWaiters(root);
30616 +       inode = d_inode(root);
30617 +       IiMustNoWaiters(inode);
30618 +
30619 +       udba = au_opt_udba(sb);
30620 +       bbot = au_sbbot(sb);
30621 +       for (bindex = 0; bindex <= bbot; bindex++) {
30622 +               br = au_sbr(sb, bindex);
30623 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30624 +               if (unlikely(err))
30625 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30626 +                               bindex, err);
30627 +               /* go on even if err */
30628 +       }
30629 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30630 +
30631 +       if (do_idop) {
30632 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30633 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30634 +                       sb->s_d_op = &aufs_dop_noreval;
30635 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30636 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30637 +               } else {
30638 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30639 +                       sb->s_d_op = &aufs_dop;
30640 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30641 +                       sbi->si_iop_array = aufs_iop;
30642 +               }
30643 +               pr_info("reset to %ps and %ps\n",
30644 +                       sb->s_d_op, sbi->si_iop_array);
30645 +       }
30646 +
30647 +       di_write_unlock(root);
30648 +       err = au_refresh_d(sb, do_idop);
30649 +       e = au_refresh_i(sb, do_idop);
30650 +       if (unlikely(e && !err))
30651 +               err = e;
30652 +       /* aufs_write_lock() calls ..._child() */
30653 +       di_write_lock_child(root);
30654 +
30655 +       au_cpup_attr_all(inode, /*force*/1);
30656 +
30657 +       if (unlikely(err))
30658 +               AuIOErr("refresh failed, ignored, %d\n", err);
30659 +}
30660 +
30661 +const struct super_operations aufs_sop = {
30662 +       .alloc_inode    = aufs_alloc_inode,
30663 +       .destroy_inode  = aufs_destroy_inode,
30664 +       .free_inode     = aufs_free_inode,
30665 +       /* always deleting, no clearing */
30666 +       .drop_inode     = generic_delete_inode,
30667 +       .show_options   = aufs_show_options,
30668 +       .statfs         = aufs_statfs,
30669 +       .put_super      = aufs_put_super,
30670 +       .sync_fs        = aufs_sync_fs
30671 +};
30672 +
30673 +/* ---------------------------------------------------------------------- */
30674 +
30675 +int au_alloc_root(struct super_block *sb)
30676 +{
30677 +       int err;
30678 +       struct inode *inode;
30679 +       struct dentry *root;
30680 +
30681 +       err = -ENOMEM;
30682 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30683 +       err = PTR_ERR(inode);
30684 +       if (IS_ERR(inode))
30685 +               goto out;
30686 +
30687 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30688 +       inode->i_fop = &aufs_dir_fop;
30689 +       inode->i_mode = S_IFDIR;
30690 +       set_nlink(inode, 2);
30691 +       unlock_new_inode(inode);
30692 +
30693 +       root = d_make_root(inode);
30694 +       if (unlikely(!root))
30695 +               goto out;
30696 +       err = PTR_ERR(root);
30697 +       if (IS_ERR(root))
30698 +               goto out;
30699 +
30700 +       err = au_di_init(root);
30701 +       if (!err) {
30702 +               sb->s_root = root;
30703 +               return 0; /* success */
30704 +       }
30705 +       dput(root);
30706 +
30707 +out:
30708 +       return err;
30709 +}
30710 +
30711 +/* ---------------------------------------------------------------------- */
30712 +
30713 +static void aufs_kill_sb(struct super_block *sb)
30714 +{
30715 +       struct au_sbinfo *sbinfo;
30716 +       struct dentry *root;
30717 +
30718 +       sbinfo = au_sbi(sb);
30719 +       if (!sbinfo)
30720 +               goto out;
30721 +
30722 +       au_sbilist_del(sb);
30723 +
30724 +       root = sb->s_root;
30725 +       if (root)
30726 +               aufs_write_lock(root);
30727 +       else
30728 +               __si_write_lock(sb);
30729 +
30730 +       au_fhsm_fin(sb);
30731 +       if (sbinfo->si_wbr_create_ops->fin)
30732 +               sbinfo->si_wbr_create_ops->fin(sb);
30733 +       if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30734 +               au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30735 +               au_remount_refresh(sb, /*do_idop*/0);
30736 +       }
30737 +       if (au_opt_test(sbinfo->si_mntflags, PLINK))
30738 +               au_plink_put(sb, /*verbose*/1);
30739 +       au_xino_clr(sb);
30740 +       if (root)
30741 +               au_dr_opt_flush(sb);
30742 +
30743 +       if (root)
30744 +               aufs_write_unlock(root);
30745 +       else
30746 +               __si_write_unlock(sb);
30747 +
30748 +       sbinfo->si_sb = NULL;
30749 +       au_nwt_flush(&sbinfo->si_nowait);
30750 +
30751 +out:
30752 +       kill_anon_super(sb);
30753 +}
30754 +
30755 +struct file_system_type aufs_fs_type = {
30756 +       .name           = AUFS_FSTYPE,
30757 +       /* a race between rename and others */
30758 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30759 +                               /* untested */
30760 +                               /*| FS_ALLOW_IDMAP*/
30761 +                               ,
30762 +       .init_fs_context = aufs_fsctx_init,
30763 +       .parameters     = aufs_fsctx_paramspec,
30764 +       .kill_sb        = aufs_kill_sb,
30765 +       /* no need to __module_get() and module_put(). */
30766 +       .owner          = THIS_MODULE,
30767 +};
30768 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30769 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30770 +++ linux/fs/aufs/super.h       2024-05-13 17:13:00.621404284 +0200
30771 @@ -0,0 +1,592 @@
30772 +/* SPDX-License-Identifier: GPL-2.0 */
30773 +/*
30774 + * Copyright (C) 2005-2022 Junjiro R. Okajima
30775 + *
30776 + * This program is free software; you can redistribute it and/or modify
30777 + * it under the terms of the GNU General Public License as published by
30778 + * the Free Software Foundation; either version 2 of the License, or
30779 + * (at your option) any later version.
30780 + *
30781 + * This program is distributed in the hope that it will be useful,
30782 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30783 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30784 + * GNU General Public License for more details.
30785 + *
30786 + * You should have received a copy of the GNU General Public License
30787 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30788 + */
30789 +
30790 +/*
30791 + * super_block operations
30792 + */
30793 +
30794 +#ifndef __AUFS_SUPER_H__
30795 +#define __AUFS_SUPER_H__
30796 +
30797 +#ifdef __KERNEL__
30798 +
30799 +#include <linux/fs.h>
30800 +#include <linux/kobject.h>
30801 +#include "hbl.h"
30802 +#include "lcnt.h"
30803 +#include "rwsem.h"
30804 +#include "wkq.h"
30805 +
30806 +/* policies to select one among multiple writable branches */
30807 +struct au_wbr_copyup_operations {
30808 +       int (*copyup)(struct dentry *dentry);
30809 +};
30810 +
30811 +#define AuWbr_DIR      BIT(0)          /* target is a dir */
30812 +#define AuWbr_PARENT   BIT(1)          /* always require a parent */
30813 +
30814 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30815 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30816 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30817 +
30818 +struct au_wbr_create_operations {
30819 +       int (*create)(struct dentry *dentry, unsigned int flags);
30820 +       int (*init)(struct super_block *sb);
30821 +       int (*fin)(struct super_block *sb);
30822 +};
30823 +
30824 +struct au_wbr_mfs {
30825 +       struct mutex    mfs_lock; /* protect this structure */
30826 +       unsigned long   mfs_jiffy;
30827 +       unsigned long   mfs_expire;
30828 +       aufs_bindex_t   mfs_bindex;
30829 +
30830 +       unsigned long long      mfsrr_bytes;
30831 +       unsigned long long      mfsrr_watermark;
30832 +};
30833 +
30834 +#define AuPlink_NHASH 100
30835 +static inline int au_plink_hash(ino_t ino)
30836 +{
30837 +       return ino % AuPlink_NHASH;
30838 +}
30839 +
30840 +/* File-based Hierarchical Storage Management */
30841 +struct au_fhsm {
30842 +#ifdef CONFIG_AUFS_FHSM
30843 +       /* allow only one process who can receive the notification */
30844 +       spinlock_t              fhsm_spin;
30845 +       pid_t                   fhsm_pid;
30846 +       wait_queue_head_t       fhsm_wqh;
30847 +       atomic_t                fhsm_readable;
30848 +
30849 +       /* these are protected by si_rwsem */
30850 +       unsigned long           fhsm_expire;
30851 +       aufs_bindex_t           fhsm_bottom;
30852 +#endif
30853 +};
30854 +
30855 +struct au_branch;
30856 +struct au_sbinfo {
30857 +       /* nowait tasks in the system-wide workqueue */
30858 +       struct au_nowait_tasks  si_nowait;
30859 +
30860 +       /*
30861 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30862 +        * rwsem for au_sbinfo is necessary.
30863 +        */
30864 +       struct au_rwsem         si_rwsem;
30865 +
30866 +       /*
30867 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30868 +        * remount.
30869 +        */
30870 +       au_lcnt_t               si_ninodes, si_nfiles;
30871 +
30872 +       /* branch management */
30873 +       unsigned int            si_generation;
30874 +
30875 +       /* see AuSi_ flags */
30876 +       unsigned char           au_si_status;
30877 +
30878 +       aufs_bindex_t           si_bbot;
30879 +
30880 +       /* dirty trick to keep br_id plus */
30881 +       unsigned int            si_last_br_id :
30882 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30883 +       struct au_branch        **si_branch;
30884 +
30885 +       /* policy to select a writable branch */
30886 +       unsigned char           si_wbr_copyup;
30887 +       unsigned char           si_wbr_create;
30888 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30889 +       struct au_wbr_create_operations *si_wbr_create_ops;
30890 +
30891 +       /* round robin */
30892 +       atomic_t                si_wbr_rr_next;
30893 +
30894 +       /* most free space */
30895 +       struct au_wbr_mfs       si_wbr_mfs;
30896 +
30897 +       /* File-based Hierarchical Storage Management */
30898 +       struct au_fhsm          si_fhsm;
30899 +
30900 +       /* mount flags */
30901 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30902 +       unsigned int            si_mntflags;
30903 +
30904 +       /* external inode number (bitmap and translation table) */
30905 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30906 +
30907 +       struct file             *si_xib;
30908 +       struct mutex            si_xib_mtx; /* protect xib members */
30909 +       unsigned long           *si_xib_buf;
30910 +       unsigned long           si_xib_last_pindex;
30911 +       int                     si_xib_next_bit;
30912 +
30913 +       unsigned long           si_xino_jiffy;
30914 +       unsigned long           si_xino_expire;
30915 +       /* reserved for future use */
30916 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30917 +
30918 +#ifdef CONFIG_AUFS_EXPORT
30919 +       /* i_generation */
30920 +       /* todo: make xigen file an array to support many inode numbers */
30921 +       struct file             *si_xigen;
30922 +       atomic_t                si_xigen_next;
30923 +#endif
30924 +
30925 +       /* dirty trick to support atomic_open */
30926 +       struct hlist_bl_head    si_aopen;
30927 +
30928 +       /* vdir parameters */
30929 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30930 +       unsigned int            si_rdblk;       /* deblk size */
30931 +       unsigned int            si_rdhash;      /* hash size */
30932 +
30933 +       /*
30934 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30935 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30936 +        * future fsck.aufs or kernel thread will remove them later.
30937 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30938 +        */
30939 +       unsigned int            si_dirwh;
30940 +
30941 +       /* pseudo_link list */
30942 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30943 +       wait_queue_head_t       si_plink_wq;
30944 +       spinlock_t              si_plink_maint_lock;
30945 +       pid_t                   si_plink_maint_pid;
30946 +
30947 +       /* file list */
30948 +       struct hlist_bl_head    si_files;
30949 +
30950 +       /* with/without getattr, brother of sb->s_d_op */
30951 +       const struct inode_operations *si_iop_array;
30952 +
30953 +       /*
30954 +        * sysfs and lifetime management.
30955 +        * this is not a small structure and it may be a waste of memory in case
30956 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30957 +        * but using sysfs is majority.
30958 +        */
30959 +       struct kobject          si_kobj;
30960 +#ifdef CONFIG_DEBUG_FS
30961 +       struct dentry            *si_dbgaufs;
30962 +       struct dentry            *si_dbgaufs_plink;
30963 +       struct dentry            *si_dbgaufs_xib;
30964 +#ifdef CONFIG_AUFS_EXPORT
30965 +       struct dentry            *si_dbgaufs_xigen;
30966 +#endif
30967 +#endif
30968 +
30969 +#ifdef CONFIG_AUFS_SBILIST
30970 +       struct hlist_bl_node    si_list;
30971 +#endif
30972 +
30973 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30974 +       struct super_block      *si_sb;
30975 +};
30976 +
30977 +/* sbinfo status flags */
30978 +/*
30979 + * set true when refresh_dirs() failed at remount time.
30980 + * then try refreshing dirs at access time again.
30981 + * if it is false, refreshing dirs at access time is unnecessary
30982 + */
30983 +#define AuSi_FAILED_REFRESH_DIR        BIT(0)
30984 +#define AuSi_FHSM              BIT(1)          /* fhsm is active now */
30985 +#define AuSi_NO_DREVAL         BIT(2)          /* disable all d_revalidate */
30986 +
30987 +#ifndef CONFIG_AUFS_FHSM
30988 +#undef AuSi_FHSM
30989 +#define AuSi_FHSM              0
30990 +#endif
30991 +
30992 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30993 +                                          unsigned int flag)
30994 +{
30995 +       AuRwMustAnyLock(&sbi->si_rwsem);
30996 +       return sbi->au_si_status & flag;
30997 +}
30998 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30999 +#define au_fset_si(sbinfo, name) do { \
31000 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
31001 +       (sbinfo)->au_si_status |= AuSi_##name; \
31002 +} while (0)
31003 +#define au_fclr_si(sbinfo, name) do { \
31004 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
31005 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
31006 +} while (0)
31007 +
31008 +/* ---------------------------------------------------------------------- */
31009 +
31010 +/* policy to select one among writable branches */
31011 +#define AuWbrCopyup(sbinfo, ...) \
31012 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
31013 +#define AuWbrCreate(sbinfo, ...) \
31014 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
31015 +
31016 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
31017 +#define AuLock_DW              BIT(0)          /* write-lock dentry */
31018 +#define AuLock_IR              BIT(1)          /* read-lock inode */
31019 +#define AuLock_IW              BIT(2)          /* write-lock inode */
31020 +#define AuLock_FLUSH           BIT(3)          /* wait for 'nowait' tasks */
31021 +#define AuLock_DIRS            BIT(4)          /* target is a pair of dirs */
31022 +                                               /* except RENAME_EXCHANGE */
31023 +#define AuLock_NOPLM           BIT(5)          /* return err in plm mode */
31024 +#define AuLock_NOPLMW          BIT(6)          /* wait for plm mode ends */
31025 +#define AuLock_GEN             BIT(7)          /* test digen/iigen */
31026 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
31027 +#define au_fset_lock(flags, name) \
31028 +       do { (flags) |= AuLock_##name; } while (0)
31029 +#define au_fclr_lock(flags, name) \
31030 +       do { (flags) &= ~AuLock_##name; } while (0)
31031 +
31032 +/* ---------------------------------------------------------------------- */
31033 +
31034 +/* super.c */
31035 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
31036 +
31037 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
31038 +                                          unsigned long long max, void *arg);
31039 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
31040 +                    struct super_block *sb, void *arg);
31041 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
31042 +void au_iarray_free(struct inode **a, unsigned long long max);
31043 +
31044 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
31045 +extern const struct super_operations aufs_sop;
31046 +int au_alloc_root(struct super_block *sb);
31047 +extern struct file_system_type aufs_fs_type;
31048 +
31049 +/* sbinfo.c */
31050 +void au_si_free(struct kobject *kobj);
31051 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
31052 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
31053 +
31054 +unsigned int au_sigen_inc(struct super_block *sb);
31055 +aufs_bindex_t au_new_br_id(struct super_block *sb);
31056 +
31057 +int si_read_lock(struct super_block *sb, int flags);
31058 +int si_write_lock(struct super_block *sb, int flags);
31059 +int aufs_read_lock(struct dentry *dentry, int flags);
31060 +void aufs_read_unlock(struct dentry *dentry, int flags);
31061 +void aufs_write_lock(struct dentry *dentry);
31062 +void aufs_write_unlock(struct dentry *dentry);
31063 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
31064 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
31065 +
31066 +/* wbr_policy.c */
31067 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
31068 +extern struct au_wbr_create_operations au_wbr_create_ops[];
31069 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
31070 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
31071 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
31072 +
31073 +/* mvdown.c */
31074 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
31075 +
31076 +#ifdef CONFIG_AUFS_FHSM
31077 +/* fhsm.c */
31078 +
31079 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
31080 +{
31081 +       pid_t pid;
31082 +
31083 +       spin_lock(&fhsm->fhsm_spin);
31084 +       pid = fhsm->fhsm_pid;
31085 +       spin_unlock(&fhsm->fhsm_spin);
31086 +
31087 +       return pid;
31088 +}
31089 +
31090 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
31091 +void au_fhsm_wrote_all(struct super_block *sb, int force);
31092 +int au_fhsm_fd(struct super_block *sb, int oflags);
31093 +int au_fhsm_br_alloc(struct au_branch *br);
31094 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
31095 +void au_fhsm_fin(struct super_block *sb);
31096 +void au_fhsm_init(struct au_sbinfo *sbinfo);
31097 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
31098 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
31099 +#else
31100 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
31101 +          int force)
31102 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
31103 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
31104 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
31105 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
31106 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
31107 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
31108 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
31109 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
31110 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31111 +#endif
31112 +
31113 +/* ---------------------------------------------------------------------- */
31114 +
31115 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31116 +{
31117 +       return sb->s_fs_info;
31118 +}
31119 +
31120 +/* ---------------------------------------------------------------------- */
31121 +
31122 +#ifdef CONFIG_AUFS_EXPORT
31123 +int au_test_nfsd(void);
31124 +void au_export_init(struct super_block *sb);
31125 +void au_xigen_inc(struct inode *inode);
31126 +int au_xigen_new(struct inode *inode);
31127 +int au_xigen_set(struct super_block *sb, struct path *path);
31128 +void au_xigen_clr(struct super_block *sb);
31129 +
31130 +static inline int au_busy_or_stale(void)
31131 +{
31132 +       if (!au_test_nfsd())
31133 +               return -EBUSY;
31134 +       return -ESTALE;
31135 +}
31136 +#else
31137 +AuStubInt0(au_test_nfsd, void)
31138 +AuStubVoid(au_export_init, struct super_block *sb)
31139 +AuStubVoid(au_xigen_inc, struct inode *inode)
31140 +AuStubInt0(au_xigen_new, struct inode *inode)
31141 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
31142 +AuStubVoid(au_xigen_clr, struct super_block *sb)
31143 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
31144 +#endif /* CONFIG_AUFS_EXPORT */
31145 +
31146 +/* ---------------------------------------------------------------------- */
31147 +
31148 +#ifdef CONFIG_AUFS_SBILIST
31149 +/* module.c */
31150 +extern struct hlist_bl_head au_sbilist;
31151 +
31152 +static inline void au_sbilist_init(void)
31153 +{
31154 +       INIT_HLIST_BL_HEAD(&au_sbilist);
31155 +}
31156 +
31157 +static inline void au_sbilist_add(struct super_block *sb)
31158 +{
31159 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
31160 +}
31161 +
31162 +static inline void au_sbilist_del(struct super_block *sb)
31163 +{
31164 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
31165 +}
31166 +
31167 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31168 +static inline void au_sbilist_lock(void)
31169 +{
31170 +       hlist_bl_lock(&au_sbilist);
31171 +}
31172 +
31173 +static inline void au_sbilist_unlock(void)
31174 +{
31175 +       hlist_bl_unlock(&au_sbilist);
31176 +}
31177 +#define AuGFP_SBILIST  GFP_ATOMIC
31178 +#else
31179 +AuStubVoid(au_sbilist_lock, void)
31180 +AuStubVoid(au_sbilist_unlock, void)
31181 +#define AuGFP_SBILIST  GFP_NOFS
31182 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
31183 +#else
31184 +AuStubVoid(au_sbilist_init, void)
31185 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31186 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31187 +AuStubVoid(au_sbilist_lock, void)
31188 +AuStubVoid(au_sbilist_unlock, void)
31189 +#define AuGFP_SBILIST  GFP_NOFS
31190 +#endif
31191 +
31192 +/* ---------------------------------------------------------------------- */
31193 +
31194 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31195 +{
31196 +       /*
31197 +        * This function is a dynamic '__init' function actually,
31198 +        * so the tiny check for si_rwsem is unnecessary.
31199 +        */
31200 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31201 +#ifdef CONFIG_DEBUG_FS
31202 +       sbinfo->si_dbgaufs = NULL;
31203 +       sbinfo->si_dbgaufs_plink = NULL;
31204 +       sbinfo->si_dbgaufs_xib = NULL;
31205 +#ifdef CONFIG_AUFS_EXPORT
31206 +       sbinfo->si_dbgaufs_xigen = NULL;
31207 +#endif
31208 +#endif
31209 +}
31210 +
31211 +/* ---------------------------------------------------------------------- */
31212 +
31213 +/* current->atomic_flags */
31214 +/* this value should never corrupt the ones defined in linux/sched.h */
31215 +#define PFA_AUFS       0x10
31216 +
31217 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31218 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31219 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31220 +
31221 +static inline int si_pid_test(struct super_block *sb)
31222 +{
31223 +       return !!task_test_aufs(current);
31224 +}
31225 +
31226 +static inline void si_pid_clr(struct super_block *sb)
31227 +{
31228 +       AuDebugOn(!task_test_aufs(current));
31229 +       task_clear_aufs(current);
31230 +}
31231 +
31232 +static inline void si_pid_set(struct super_block *sb)
31233 +{
31234 +       AuDebugOn(task_test_aufs(current));
31235 +       task_set_aufs(current);
31236 +}
31237 +
31238 +/* ---------------------------------------------------------------------- */
31239 +
31240 +/* lock superblock. mainly for entry point functions */
31241 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31242 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31243 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31244 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31245 +/*
31246 +#define __si_read_trylock_nested(sb) \
31247 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31248 +#define __si_write_trylock_nested(sb) \
31249 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31250 +*/
31251 +
31252 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31253 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31254 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31255 +
31256 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31257 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31258 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31259 +
31260 +static inline void si_noflush_read_lock(struct super_block *sb)
31261 +{
31262 +       __si_read_lock(sb);
31263 +       si_pid_set(sb);
31264 +}
31265 +
31266 +static inline int si_noflush_read_trylock(struct super_block *sb)
31267 +{
31268 +       int locked;
31269 +
31270 +       locked = __si_read_trylock(sb);
31271 +       if (locked)
31272 +               si_pid_set(sb);
31273 +       return locked;
31274 +}
31275 +
31276 +static inline void si_noflush_write_lock(struct super_block *sb)
31277 +{
31278 +       __si_write_lock(sb);
31279 +       si_pid_set(sb);
31280 +}
31281 +
31282 +static inline int si_noflush_write_trylock(struct super_block *sb)
31283 +{
31284 +       int locked;
31285 +
31286 +       locked = __si_write_trylock(sb);
31287 +       if (locked)
31288 +               si_pid_set(sb);
31289 +       return locked;
31290 +}
31291 +
31292 +#if 0 /* reserved */
31293 +static inline int si_read_trylock(struct super_block *sb, int flags)
31294 +{
31295 +       if (au_ftest_lock(flags, FLUSH))
31296 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31297 +       return si_noflush_read_trylock(sb);
31298 +}
31299 +#endif
31300 +
31301 +static inline void si_read_unlock(struct super_block *sb)
31302 +{
31303 +       si_pid_clr(sb);
31304 +       __si_read_unlock(sb);
31305 +}
31306 +
31307 +#if 0 /* reserved */
31308 +static inline int si_write_trylock(struct super_block *sb, int flags)
31309 +{
31310 +       if (au_ftest_lock(flags, FLUSH))
31311 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31312 +       return si_noflush_write_trylock(sb);
31313 +}
31314 +#endif
31315 +
31316 +static inline void si_write_unlock(struct super_block *sb)
31317 +{
31318 +       si_pid_clr(sb);
31319 +       __si_write_unlock(sb);
31320 +}
31321 +
31322 +#if 0 /* reserved */
31323 +static inline void si_downgrade_lock(struct super_block *sb)
31324 +{
31325 +       __si_downgrade_lock(sb);
31326 +}
31327 +#endif
31328 +
31329 +/* ---------------------------------------------------------------------- */
31330 +
31331 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31332 +{
31333 +       SiMustAnyLock(sb);
31334 +       return au_sbi(sb)->si_bbot;
31335 +}
31336 +
31337 +static inline unsigned int au_mntflags(struct super_block *sb)
31338 +{
31339 +       SiMustAnyLock(sb);
31340 +       return au_sbi(sb)->si_mntflags;
31341 +}
31342 +
31343 +static inline unsigned int au_sigen(struct super_block *sb)
31344 +{
31345 +       SiMustAnyLock(sb);
31346 +       return au_sbi(sb)->si_generation;
31347 +}
31348 +
31349 +static inline struct au_branch *au_sbr(struct super_block *sb,
31350 +                                      aufs_bindex_t bindex)
31351 +{
31352 +       SiMustAnyLock(sb);
31353 +       return au_sbi(sb)->si_branch[0 + bindex];
31354 +}
31355 +
31356 +static inline loff_t au_xi_maxent(struct super_block *sb)
31357 +{
31358 +       SiMustAnyLock(sb);
31359 +       return au_sbi(sb)->si_ximaxent;
31360 +}
31361 +
31362 +#endif /* __KERNEL__ */
31363 +#endif /* __AUFS_SUPER_H__ */
31364 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31365 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31366 +++ linux/fs/aufs/sysaufs.c     2024-05-13 17:13:00.621404284 +0200
31367 @@ -0,0 +1,94 @@
31368 +// SPDX-License-Identifier: GPL-2.0
31369 +/*
31370 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31371 + *
31372 + * This program is free software; you can redistribute it and/or modify
31373 + * it under the terms of the GNU General Public License as published by
31374 + * the Free Software Foundation; either version 2 of the License, or
31375 + * (at your option) any later version.
31376 + *
31377 + * This program is distributed in the hope that it will be useful,
31378 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31379 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31380 + * GNU General Public License for more details.
31381 + *
31382 + * You should have received a copy of the GNU General Public License
31383 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31384 + */
31385 +
31386 +/*
31387 + * sysfs interface and lifetime management
31388 + * they are necessary regardless sysfs is disabled.
31389 + */
31390 +
31391 +#include <linux/random.h>
31392 +#include "aufs.h"
31393 +
31394 +unsigned long sysaufs_si_mask;
31395 +struct kset *sysaufs_kset;
31396 +
31397 +#define AuSiAttr(_name) { \
31398 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31399 +       .show   = sysaufs_si_##_name,                           \
31400 +}
31401 +
31402 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31403 +struct attribute *sysaufs_si_attrs[] = {
31404 +       &sysaufs_si_attr_xi_path.attr,
31405 +       NULL,
31406 +};
31407 +ATTRIBUTE_GROUPS(sysaufs_si);
31408 +
31409 +static const struct sysfs_ops au_sbi_ops = {
31410 +       .show   = sysaufs_si_show
31411 +};
31412 +
31413 +static const struct kobj_type au_sbi_ktype = {
31414 +       .release        = au_si_free,
31415 +       .sysfs_ops      = &au_sbi_ops,
31416 +       .default_groups = sysaufs_si_groups
31417 +};
31418 +
31419 +/* ---------------------------------------------------------------------- */
31420 +
31421 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31422 +{
31423 +       int err;
31424 +
31425 +       sbinfo->si_kobj.kset = sysaufs_kset;
31426 +       /* cf. sysaufs_name() */
31427 +       err = kobject_init_and_add
31428 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31429 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31430 +
31431 +       return err;
31432 +}
31433 +
31434 +void sysaufs_fin(void)
31435 +{
31436 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31437 +       kset_unregister(sysaufs_kset);
31438 +}
31439 +
31440 +int __init sysaufs_init(void)
31441 +{
31442 +       int err;
31443 +
31444 +       do {
31445 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31446 +       } while (!sysaufs_si_mask);
31447 +
31448 +       err = -EINVAL;
31449 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31450 +       if (unlikely(!sysaufs_kset))
31451 +               goto out;
31452 +       err = PTR_ERR(sysaufs_kset);
31453 +       if (IS_ERR(sysaufs_kset))
31454 +               goto out;
31455 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31456 +       if (unlikely(err))
31457 +               kset_unregister(sysaufs_kset);
31458 +
31459 +out:
31460 +       return err;
31461 +}
31462 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31463 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31464 +++ linux/fs/aufs/sysaufs.h     2024-05-13 17:13:00.621404284 +0200
31465 @@ -0,0 +1,102 @@
31466 +/* SPDX-License-Identifier: GPL-2.0 */
31467 +/*
31468 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31469 + *
31470 + * This program is free software; you can redistribute it and/or modify
31471 + * it under the terms of the GNU General Public License as published by
31472 + * the Free Software Foundation; either version 2 of the License, or
31473 + * (at your option) any later version.
31474 + *
31475 + * This program is distributed in the hope that it will be useful,
31476 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31477 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31478 + * GNU General Public License for more details.
31479 + *
31480 + * You should have received a copy of the GNU General Public License
31481 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31482 + */
31483 +
31484 +/*
31485 + * sysfs interface and mount lifetime management
31486 + */
31487 +
31488 +#ifndef __SYSAUFS_H__
31489 +#define __SYSAUFS_H__
31490 +
31491 +#ifdef __KERNEL__
31492 +
31493 +#include <linux/sysfs.h>
31494 +#include "module.h"
31495 +
31496 +struct super_block;
31497 +struct au_sbinfo;
31498 +
31499 +struct sysaufs_si_attr {
31500 +       struct attribute attr;
31501 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31502 +};
31503 +
31504 +/* ---------------------------------------------------------------------- */
31505 +
31506 +/* sysaufs.c */
31507 +extern unsigned long sysaufs_si_mask;
31508 +extern struct kset *sysaufs_kset;
31509 +extern struct attribute *sysaufs_si_attrs[];
31510 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31511 +int __init sysaufs_init(void);
31512 +void sysaufs_fin(void);
31513 +
31514 +/* ---------------------------------------------------------------------- */
31515 +
31516 +/* some people doesn't like to show a pointer in kernel */
31517 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31518 +{
31519 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31520 +}
31521 +
31522 +#define SysaufsSiNamePrefix    "si_"
31523 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31524 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31525 +{
31526 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31527 +                sysaufs_si_id(sbinfo));
31528 +}
31529 +
31530 +struct au_branch;
31531 +#ifdef CONFIG_SYSFS
31532 +/* sysfs.c */
31533 +extern struct attribute_group *sysaufs_attr_group;
31534 +
31535 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31536 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31537 +                        char *buf);
31538 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31539 +#ifdef CONFIG_COMPAT
31540 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31541 +#endif
31542 +
31543 +void sysaufs_br_init(struct au_branch *br);
31544 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31545 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31546 +
31547 +#define sysaufs_brs_init()     do {} while (0)
31548 +
31549 +#else
31550 +#define sysaufs_attr_group     NULL
31551 +
31552 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31553 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31554 +       struct attribute *attr, char *buf)
31555 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31556 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31557 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31558 +
31559 +static inline void sysaufs_brs_init(void)
31560 +{
31561 +       sysaufs_brs = 0;
31562 +}
31563 +
31564 +#endif /* CONFIG_SYSFS */
31565 +
31566 +#endif /* __KERNEL__ */
31567 +#endif /* __SYSAUFS_H__ */
31568 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31569 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31570 +++ linux/fs/aufs/sysfs.c       2024-05-13 17:13:00.621404284 +0200
31571 @@ -0,0 +1,374 @@
31572 +// SPDX-License-Identifier: GPL-2.0
31573 +/*
31574 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31575 + *
31576 + * This program is free software; you can redistribute it and/or modify
31577 + * it under the terms of the GNU General Public License as published by
31578 + * the Free Software Foundation; either version 2 of the License, or
31579 + * (at your option) any later version.
31580 + *
31581 + * This program is distributed in the hope that it will be useful,
31582 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31583 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31584 + * GNU General Public License for more details.
31585 + *
31586 + * You should have received a copy of the GNU General Public License
31587 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31588 + */
31589 +
31590 +/*
31591 + * sysfs interface
31592 + */
31593 +
31594 +#include <linux/compat.h>
31595 +#include <linux/seq_file.h>
31596 +#include "aufs.h"
31597 +
31598 +#ifdef CONFIG_AUFS_FS_MODULE
31599 +/* this entry violates the "one line per file" policy of sysfs */
31600 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31601 +                          char *buf)
31602 +{
31603 +       ssize_t err;
31604 +       static char *conf =
31605 +/* this file is generated at compiling */
31606 +#include "conf.str"
31607 +               ;
31608 +
31609 +       err = snprintf(buf, PAGE_SIZE, conf);
31610 +       if (unlikely(err >= PAGE_SIZE))
31611 +               err = -EFBIG;
31612 +       return err;
31613 +}
31614 +
31615 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31616 +#endif
31617 +
31618 +static struct attribute *au_attr[] = {
31619 +#ifdef CONFIG_AUFS_FS_MODULE
31620 +       &au_config_attr.attr,
31621 +#endif
31622 +       NULL,   /* need to NULL terminate the list of attributes */
31623 +};
31624 +
31625 +static struct attribute_group sysaufs_attr_group_body = {
31626 +       .attrs = au_attr
31627 +};
31628 +
31629 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31630 +
31631 +/* ---------------------------------------------------------------------- */
31632 +
31633 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31634 +{
31635 +       int err;
31636 +
31637 +       SiMustAnyLock(sb);
31638 +
31639 +       err = 0;
31640 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31641 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31642 +               seq_putc(seq, '\n');
31643 +       }
31644 +       return err;
31645 +}
31646 +
31647 +/*
31648 + * the lifetime of branch is independent from the entry under sysfs.
31649 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31650 + * unlinked.
31651 + */
31652 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31653 +                        aufs_bindex_t bindex, int idx)
31654 +{
31655 +       int err;
31656 +       struct path path;
31657 +       struct dentry *root;
31658 +       struct au_branch *br;
31659 +       au_br_perm_str_t perm;
31660 +
31661 +       AuDbg("b%d\n", bindex);
31662 +
31663 +       err = 0;
31664 +       root = sb->s_root;
31665 +       di_read_lock_parent(root, !AuLock_IR);
31666 +       br = au_sbr(sb, bindex);
31667 +
31668 +       switch (idx) {
31669 +       case AuBrSysfs_BR:
31670 +               path.mnt = au_br_mnt(br);
31671 +               path.dentry = au_h_dptr(root, bindex);
31672 +               err = au_seq_path(seq, &path);
31673 +               if (!err) {
31674 +                       au_optstr_br_perm(&perm, br->br_perm);
31675 +                       seq_printf(seq, "=%s\n", perm.a);
31676 +               }
31677 +               break;
31678 +       case AuBrSysfs_BRID:
31679 +               seq_printf(seq, "%d\n", br->br_id);
31680 +               break;
31681 +       }
31682 +       di_read_unlock(root, !AuLock_IR);
31683 +       if (unlikely(err || seq_has_overflowed(seq)))
31684 +               err = -E2BIG;
31685 +
31686 +       return err;
31687 +}
31688 +
31689 +/* ---------------------------------------------------------------------- */
31690 +
31691 +static struct seq_file *au_seq(char *p, ssize_t len)
31692 +{
31693 +       struct seq_file *seq;
31694 +
31695 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31696 +       if (seq) {
31697 +               /* mutex_init(&seq.lock); */
31698 +               seq->buf = p;
31699 +               seq->size = len;
31700 +               return seq; /* success */
31701 +       }
31702 +
31703 +       seq = ERR_PTR(-ENOMEM);
31704 +       return seq;
31705 +}
31706 +
31707 +#define SysaufsBr_PREFIX       "br"
31708 +#define SysaufsBrid_PREFIX     "brid"
31709 +
31710 +/* todo: file size may exceed PAGE_SIZE */
31711 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31712 +                       char *buf)
31713 +{
31714 +       ssize_t err;
31715 +       int idx;
31716 +       long l;
31717 +       aufs_bindex_t bbot;
31718 +       struct au_sbinfo *sbinfo;
31719 +       struct super_block *sb;
31720 +       struct seq_file *seq;
31721 +       char *name;
31722 +       struct attribute **cattr;
31723 +
31724 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31725 +       sb = sbinfo->si_sb;
31726 +
31727 +       /*
31728 +        * prevent a race condition between sysfs and aufs.
31729 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31730 +        * prohibits maintaining the sysfs entries.
31731 +        * hew we acquire read lock after sysfs_get_active_two().
31732 +        * on the other hand, the remount process may maintain the sysfs/aufs
31733 +        * entries after acquiring write lock.
31734 +        * it can cause a deadlock.
31735 +        * simply we gave up processing read here.
31736 +        */
31737 +       err = -EBUSY;
31738 +       if (unlikely(!si_noflush_read_trylock(sb)))
31739 +               goto out;
31740 +
31741 +       seq = au_seq(buf, PAGE_SIZE);
31742 +       err = PTR_ERR(seq);
31743 +       if (IS_ERR(seq))
31744 +               goto out_unlock;
31745 +
31746 +       name = (void *)attr->name;
31747 +       cattr = sysaufs_si_attrs;
31748 +       while (*cattr) {
31749 +               if (!strcmp(name, (*cattr)->name)) {
31750 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31751 +                               ->show(seq, sb);
31752 +                       goto out_seq;
31753 +               }
31754 +               cattr++;
31755 +       }
31756 +
31757 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31758 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31759 +               idx = AuBrSysfs_BRID;
31760 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31761 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31762 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31763 +               idx = AuBrSysfs_BR;
31764 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31765 +       } else
31766 +                 BUG();
31767 +
31768 +       err = kstrtol(name, 10, &l);
31769 +       if (!err) {
31770 +               bbot = au_sbbot(sb);
31771 +               if (l <= bbot)
31772 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31773 +               else
31774 +                       err = -ENOENT;
31775 +       }
31776 +
31777 +out_seq:
31778 +       if (!err) {
31779 +               err = seq->count;
31780 +               /* sysfs limit */
31781 +               if (unlikely(err == PAGE_SIZE))
31782 +                       err = -EFBIG;
31783 +       }
31784 +       au_kfree_rcu(seq);
31785 +out_unlock:
31786 +       si_read_unlock(sb);
31787 +out:
31788 +       return err;
31789 +}
31790 +
31791 +/* ---------------------------------------------------------------------- */
31792 +
31793 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31794 +{
31795 +       int err;
31796 +       int16_t brid;
31797 +       aufs_bindex_t bindex, bbot;
31798 +       size_t sz;
31799 +       char *buf;
31800 +       struct seq_file *seq;
31801 +       struct au_branch *br;
31802 +
31803 +       si_read_lock(sb, AuLock_FLUSH);
31804 +       bbot = au_sbbot(sb);
31805 +       err = bbot + 1;
31806 +       if (!arg)
31807 +               goto out;
31808 +
31809 +       err = -ENOMEM;
31810 +       buf = (void *)__get_free_page(GFP_NOFS);
31811 +       if (unlikely(!buf))
31812 +               goto out;
31813 +
31814 +       seq = au_seq(buf, PAGE_SIZE);
31815 +       err = PTR_ERR(seq);
31816 +       if (IS_ERR(seq))
31817 +               goto out_buf;
31818 +
31819 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31820 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31821 +               /* VERIFY_WRITE */
31822 +               err = !access_ok(arg, sizeof(*arg));
31823 +               if (unlikely(err))
31824 +                       break;
31825 +
31826 +               br = au_sbr(sb, bindex);
31827 +               brid = br->br_id;
31828 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31829 +               err = __put_user(brid, &arg->id);
31830 +               if (unlikely(err))
31831 +                       break;
31832 +
31833 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31834 +               err = __put_user(br->br_perm, &arg->perm);
31835 +               if (unlikely(err))
31836 +                       break;
31837 +
31838 +               err = au_seq_path(seq, &br->br_path);
31839 +               if (unlikely(err))
31840 +                       break;
31841 +               seq_putc(seq, '\0');
31842 +               if (!seq_has_overflowed(seq)) {
31843 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31844 +                       seq->count = 0;
31845 +                       if (unlikely(err))
31846 +                               break;
31847 +               } else {
31848 +                       err = -E2BIG;
31849 +                       goto out_seq;
31850 +               }
31851 +       }
31852 +       if (unlikely(err))
31853 +               err = -EFAULT;
31854 +
31855 +out_seq:
31856 +       au_kfree_rcu(seq);
31857 +out_buf:
31858 +       free_page((unsigned long)buf);
31859 +out:
31860 +       si_read_unlock(sb);
31861 +       return err;
31862 +}
31863 +
31864 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31865 +{
31866 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31867 +}
31868 +
31869 +#ifdef CONFIG_COMPAT
31870 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31871 +{
31872 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31873 +}
31874 +#endif
31875 +
31876 +/* ---------------------------------------------------------------------- */
31877 +
31878 +void sysaufs_br_init(struct au_branch *br)
31879 +{
31880 +       int i;
31881 +       struct au_brsysfs *br_sysfs;
31882 +       struct attribute *attr;
31883 +
31884 +       br_sysfs = br->br_sysfs;
31885 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31886 +               attr = &br_sysfs->attr;
31887 +               sysfs_attr_init(attr);
31888 +               attr->name = br_sysfs->name;
31889 +               attr->mode = 0444;
31890 +               br_sysfs++;
31891 +       }
31892 +}
31893 +
31894 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31895 +{
31896 +       struct au_branch *br;
31897 +       struct kobject *kobj;
31898 +       struct au_brsysfs *br_sysfs;
31899 +       int i;
31900 +       aufs_bindex_t bbot;
31901 +
31902 +       if (!sysaufs_brs)
31903 +               return;
31904 +
31905 +       kobj = &au_sbi(sb)->si_kobj;
31906 +       bbot = au_sbbot(sb);
31907 +       for (; bindex <= bbot; bindex++) {
31908 +               br = au_sbr(sb, bindex);
31909 +               br_sysfs = br->br_sysfs;
31910 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31911 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31912 +                       br_sysfs++;
31913 +               }
31914 +       }
31915 +}
31916 +
31917 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31918 +{
31919 +       int err, i;
31920 +       aufs_bindex_t bbot;
31921 +       struct kobject *kobj;
31922 +       struct au_branch *br;
31923 +       struct au_brsysfs *br_sysfs;
31924 +
31925 +       if (!sysaufs_brs)
31926 +               return;
31927 +
31928 +       kobj = &au_sbi(sb)->si_kobj;
31929 +       bbot = au_sbbot(sb);
31930 +       for (; bindex <= bbot; bindex++) {
31931 +               br = au_sbr(sb, bindex);
31932 +               br_sysfs = br->br_sysfs;
31933 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31934 +                        SysaufsBr_PREFIX "%d", bindex);
31935 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31936 +                        SysaufsBrid_PREFIX "%d", bindex);
31937 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31938 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31939 +                       if (unlikely(err))
31940 +                               pr_warn("failed %s under sysfs(%d)\n",
31941 +                                       br_sysfs->name, err);
31942 +                       br_sysfs++;
31943 +               }
31944 +       }
31945 +}
31946 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31947 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31948 +++ linux/fs/aufs/sysrq.c       2024-05-13 17:13:00.621404284 +0200
31949 @@ -0,0 +1,149 @@
31950 +// SPDX-License-Identifier: GPL-2.0
31951 +/*
31952 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31953 + *
31954 + * This program is free software; you can redistribute it and/or modify
31955 + * it under the terms of the GNU General Public License as published by
31956 + * the Free Software Foundation; either version 2 of the License, or
31957 + * (at your option) any later version.
31958 + *
31959 + * This program is distributed in the hope that it will be useful,
31960 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31961 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31962 + * GNU General Public License for more details.
31963 + *
31964 + * You should have received a copy of the GNU General Public License
31965 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31966 + */
31967 +
31968 +/*
31969 + * magic sysrq handler
31970 + */
31971 +
31972 +/* #include <linux/sysrq.h> */
31973 +#include <linux/writeback.h>
31974 +#include "aufs.h"
31975 +
31976 +/* ---------------------------------------------------------------------- */
31977 +
31978 +static void sysrq_sb(struct super_block *sb)
31979 +{
31980 +       char *plevel;
31981 +       struct au_sbinfo *sbinfo;
31982 +       struct file *file;
31983 +       struct hlist_bl_head *files;
31984 +       struct hlist_bl_node *pos;
31985 +       struct au_finfo *finfo;
31986 +       struct inode *i;
31987 +
31988 +       plevel = au_plevel;
31989 +       au_plevel = KERN_WARNING;
31990 +
31991 +       /* since we define pr_fmt, call printk directly */
31992 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31993 +
31994 +       sbinfo = au_sbi(sb);
31995 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31996 +       pr("superblock\n");
31997 +       au_dpri_sb(sb);
31998 +
31999 +#if 0 /* reserved */
32000 +       do {
32001 +               int err, i, j, ndentry;
32002 +               struct au_dcsub_pages dpages;
32003 +               struct au_dpage *dpage;
32004 +
32005 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
32006 +               if (unlikely(err))
32007 +                       break;
32008 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
32009 +               if (!err)
32010 +                       for (i = 0; i < dpages.ndpage; i++) {
32011 +                               dpage = dpages.dpages + i;
32012 +                               ndentry = dpage->ndentry;
32013 +                               for (j = 0; j < ndentry; j++)
32014 +                                       au_dpri_dentry(dpage->dentries[j]);
32015 +                       }
32016 +               au_dpages_free(&dpages);
32017 +       } while (0);
32018 +#endif
32019 +
32020 +       pr("isolated inode\n");
32021 +       spin_lock(&sb->s_inode_list_lock);
32022 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
32023 +               spin_lock(&i->i_lock);
32024 +               if (hlist_empty(&i->i_dentry))
32025 +                       au_dpri_inode(i);
32026 +               spin_unlock(&i->i_lock);
32027 +       }
32028 +       spin_unlock(&sb->s_inode_list_lock);
32029 +
32030 +       pr("files\n");
32031 +       files = &au_sbi(sb)->si_files;
32032 +       hlist_bl_lock(files);
32033 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
32034 +               umode_t mode;
32035 +
32036 +               file = finfo->fi_file;
32037 +               mode = file_inode(file)->i_mode;
32038 +               if (!special_file(mode))
32039 +                       au_dpri_file(file);
32040 +       }
32041 +       hlist_bl_unlock(files);
32042 +       pr("done\n");
32043 +
32044 +#undef pr
32045 +       au_plevel = plevel;
32046 +}
32047 +
32048 +/* ---------------------------------------------------------------------- */
32049 +
32050 +/* module parameter */
32051 +static char *aufs_sysrq_key = "a";
32052 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
32053 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
32054 +
32055 +static void au_sysrq(u8 key __maybe_unused)
32056 +{
32057 +       struct au_sbinfo *sbinfo;
32058 +       struct hlist_bl_node *pos;
32059 +
32060 +       lockdep_off();
32061 +       au_sbilist_lock();
32062 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
32063 +               sysrq_sb(sbinfo->si_sb);
32064 +       au_sbilist_unlock();
32065 +       lockdep_on();
32066 +}
32067 +
32068 +static struct sysrq_key_op au_sysrq_op = {
32069 +       .handler        = au_sysrq,
32070 +       .help_msg       = "Aufs",
32071 +       .action_msg     = "Aufs",
32072 +       .enable_mask    = SYSRQ_ENABLE_DUMP
32073 +};
32074 +
32075 +/* ---------------------------------------------------------------------- */
32076 +
32077 +int __init au_sysrq_init(void)
32078 +{
32079 +       int err;
32080 +       char key;
32081 +
32082 +       err = -1;
32083 +       key = *aufs_sysrq_key;
32084 +       if ('a' <= key && key <= 'z')
32085 +               err = register_sysrq_key(key, &au_sysrq_op);
32086 +       if (unlikely(err))
32087 +               pr_err("err %d, sysrq=%c\n", err, key);
32088 +       return err;
32089 +}
32090 +
32091 +void au_sysrq_fin(void)
32092 +{
32093 +       int err;
32094 +
32095 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
32096 +       if (unlikely(err))
32097 +               pr_err("err %d (ignored)\n", err);
32098 +}
32099 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
32100 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
32101 +++ linux/fs/aufs/vdir.c        2024-05-13 17:13:00.621404284 +0200
32102 @@ -0,0 +1,896 @@
32103 +// SPDX-License-Identifier: GPL-2.0
32104 +/*
32105 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32106 + *
32107 + * This program is free software; you can redistribute it and/or modify
32108 + * it under the terms of the GNU General Public License as published by
32109 + * the Free Software Foundation; either version 2 of the License, or
32110 + * (at your option) any later version.
32111 + *
32112 + * This program is distributed in the hope that it will be useful,
32113 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32114 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32115 + * GNU General Public License for more details.
32116 + *
32117 + * You should have received a copy of the GNU General Public License
32118 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32119 + */
32120 +
32121 +/*
32122 + * virtual or vertical directory
32123 + */
32124 +
32125 +#include <linux/iversion.h>
32126 +#include "aufs.h"
32127 +
32128 +static unsigned int calc_size(int nlen)
32129 +{
32130 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
32131 +}
32132 +
32133 +static int set_deblk_end(union au_vdir_deblk_p *p,
32134 +                        union au_vdir_deblk_p *deblk_end)
32135 +{
32136 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32137 +               p->de->de_str.len = 0;
32138 +               /* smp_mb(); */
32139 +               return 0;
32140 +       }
32141 +       return -1; /* error */
32142 +}
32143 +
32144 +/* returns true or false */
32145 +static int is_deblk_end(union au_vdir_deblk_p *p,
32146 +                       union au_vdir_deblk_p *deblk_end)
32147 +{
32148 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
32149 +               return !p->de->de_str.len;
32150 +       return 1;
32151 +}
32152 +
32153 +static unsigned char *last_deblk(struct au_vdir *vdir)
32154 +{
32155 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
32156 +}
32157 +
32158 +/* ---------------------------------------------------------------------- */
32159 +
32160 +/* estimate the appropriate size for name hash table */
32161 +unsigned int au_rdhash_est(loff_t sz)
32162 +{
32163 +       unsigned int n;
32164 +
32165 +       n = UINT_MAX;
32166 +       sz >>= 10;
32167 +       if (sz < n)
32168 +               n = sz;
32169 +       if (sz < AUFS_RDHASH_DEF)
32170 +               n = AUFS_RDHASH_DEF;
32171 +       /* pr_info("n %u\n", n); */
32172 +       return n;
32173 +}
32174 +
32175 +/*
32176 + * the allocated memory has to be freed by
32177 + * au_nhash_wh_free() or au_nhash_de_free().
32178 + */
32179 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32180 +{
32181 +       struct hlist_head *head;
32182 +       unsigned int u;
32183 +       size_t sz;
32184 +
32185 +       sz = sizeof(*nhash->nh_head) * num_hash;
32186 +       head = kmalloc(sz, gfp);
32187 +       if (head) {
32188 +               nhash->nh_num = num_hash;
32189 +               nhash->nh_head = head;
32190 +               for (u = 0; u < num_hash; u++)
32191 +                       INIT_HLIST_HEAD(head++);
32192 +               return 0; /* success */
32193 +       }
32194 +
32195 +       return -ENOMEM;
32196 +}
32197 +
32198 +static void nhash_count(struct hlist_head *head)
32199 +{
32200 +#if 0 /* debugging */
32201 +       unsigned long n;
32202 +       struct hlist_node *pos;
32203 +
32204 +       n = 0;
32205 +       hlist_for_each(pos, head)
32206 +               n++;
32207 +       pr_info("%lu\n", n);
32208 +#endif
32209 +}
32210 +
32211 +static void au_nhash_wh_do_free(struct hlist_head *head)
32212 +{
32213 +       struct au_vdir_wh *pos;
32214 +       struct hlist_node *node;
32215 +
32216 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32217 +               au_kfree_rcu(pos);
32218 +}
32219 +
32220 +static void au_nhash_de_do_free(struct hlist_head *head)
32221 +{
32222 +       struct au_vdir_dehstr *pos;
32223 +       struct hlist_node *node;
32224 +
32225 +       hlist_for_each_entry_safe(pos, node, head, hash)
32226 +               au_cache_free_vdir_dehstr(pos);
32227 +}
32228 +
32229 +static void au_nhash_do_free(struct au_nhash *nhash,
32230 +                            void (*free)(struct hlist_head *head))
32231 +{
32232 +       unsigned int n;
32233 +       struct hlist_head *head;
32234 +
32235 +       n = nhash->nh_num;
32236 +       if (!n)
32237 +               return;
32238 +
32239 +       head = nhash->nh_head;
32240 +       while (n-- > 0) {
32241 +               nhash_count(head);
32242 +               free(head++);
32243 +       }
32244 +       au_kfree_try_rcu(nhash->nh_head);
32245 +}
32246 +
32247 +void au_nhash_wh_free(struct au_nhash *whlist)
32248 +{
32249 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32250 +}
32251 +
32252 +static void au_nhash_de_free(struct au_nhash *delist)
32253 +{
32254 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32255 +}
32256 +
32257 +/* ---------------------------------------------------------------------- */
32258 +
32259 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32260 +                           int limit)
32261 +{
32262 +       int num;
32263 +       unsigned int u, n;
32264 +       struct hlist_head *head;
32265 +       struct au_vdir_wh *pos;
32266 +
32267 +       num = 0;
32268 +       n = whlist->nh_num;
32269 +       head = whlist->nh_head;
32270 +       for (u = 0; u < n; u++, head++)
32271 +               hlist_for_each_entry(pos, head, wh_hash)
32272 +                       if (pos->wh_bindex == btgt && ++num > limit)
32273 +                               return 1;
32274 +       return 0;
32275 +}
32276 +
32277 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32278 +                                      unsigned char *name,
32279 +                                      unsigned int len)
32280 +{
32281 +       unsigned int v;
32282 +       /* const unsigned int magic_bit = 12; */
32283 +
32284 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32285 +
32286 +       v = 0;
32287 +       if (len > 8)
32288 +               len = 8;
32289 +       while (len--)
32290 +               v += *name++;
32291 +       /* v = hash_long(v, magic_bit); */
32292 +       v %= nhash->nh_num;
32293 +       return nhash->nh_head + v;
32294 +}
32295 +
32296 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32297 +                             int nlen)
32298 +{
32299 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32300 +}
32301 +
32302 +/* returns found or not */
32303 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32304 +{
32305 +       struct hlist_head *head;
32306 +       struct au_vdir_wh *pos;
32307 +       struct au_vdir_destr *str;
32308 +
32309 +       head = au_name_hash(whlist, name, nlen);
32310 +       hlist_for_each_entry(pos, head, wh_hash) {
32311 +               str = &pos->wh_str;
32312 +               AuDbg("%.*s\n", str->len, str->name);
32313 +               if (au_nhash_test_name(str, name, nlen))
32314 +                       return 1;
32315 +       }
32316 +       return 0;
32317 +}
32318 +
32319 +/* returns found(true) or not */
32320 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32321 +{
32322 +       struct hlist_head *head;
32323 +       struct au_vdir_dehstr *pos;
32324 +       struct au_vdir_destr *str;
32325 +
32326 +       head = au_name_hash(delist, name, nlen);
32327 +       hlist_for_each_entry(pos, head, hash) {
32328 +               str = pos->str;
32329 +               AuDbg("%.*s\n", str->len, str->name);
32330 +               if (au_nhash_test_name(str, name, nlen))
32331 +                       return 1;
32332 +       }
32333 +       return 0;
32334 +}
32335 +
32336 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32337 +                           unsigned char d_type)
32338 +{
32339 +#ifdef CONFIG_AUFS_SHWH
32340 +       wh->wh_ino = ino;
32341 +       wh->wh_type = d_type;
32342 +#endif
32343 +}
32344 +
32345 +/* ---------------------------------------------------------------------- */
32346 +
32347 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32348 +                      unsigned int d_type, aufs_bindex_t bindex,
32349 +                      unsigned char shwh)
32350 +{
32351 +       int err;
32352 +       struct au_vdir_destr *str;
32353 +       struct au_vdir_wh *wh;
32354 +
32355 +       AuDbg("%.*s\n", nlen, name);
32356 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32357 +
32358 +       err = -ENOMEM;
32359 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32360 +       if (unlikely(!wh))
32361 +               goto out;
32362 +
32363 +       err = 0;
32364 +       wh->wh_bindex = bindex;
32365 +       if (shwh)
32366 +               au_shwh_init_wh(wh, ino, d_type);
32367 +       str = &wh->wh_str;
32368 +       str->len = nlen;
32369 +       memcpy(str->name, name, nlen);
32370 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32371 +       /* smp_mb(); */
32372 +
32373 +out:
32374 +       return err;
32375 +}
32376 +
32377 +static int append_deblk(struct au_vdir *vdir)
32378 +{
32379 +       int err;
32380 +       unsigned long ul;
32381 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32382 +       union au_vdir_deblk_p p, deblk_end;
32383 +       unsigned char **o;
32384 +
32385 +       err = -ENOMEM;
32386 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32387 +                       GFP_NOFS, /*may_shrink*/0);
32388 +       if (unlikely(!o))
32389 +               goto out;
32390 +
32391 +       vdir->vd_deblk = o;
32392 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32393 +       if (p.deblk) {
32394 +               ul = vdir->vd_nblk++;
32395 +               vdir->vd_deblk[ul] = p.deblk;
32396 +               vdir->vd_last.ul = ul;
32397 +               vdir->vd_last.p.deblk = p.deblk;
32398 +               deblk_end.deblk = p.deblk + deblk_sz;
32399 +               err = set_deblk_end(&p, &deblk_end);
32400 +       }
32401 +
32402 +out:
32403 +       return err;
32404 +}
32405 +
32406 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32407 +                    unsigned int d_type, struct au_nhash *delist)
32408 +{
32409 +       int err;
32410 +       unsigned int sz;
32411 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32412 +       union au_vdir_deblk_p p, *room, deblk_end;
32413 +       struct au_vdir_dehstr *dehstr;
32414 +
32415 +       p.deblk = last_deblk(vdir);
32416 +       deblk_end.deblk = p.deblk + deblk_sz;
32417 +       room = &vdir->vd_last.p;
32418 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32419 +                 || !is_deblk_end(room, &deblk_end));
32420 +
32421 +       sz = calc_size(nlen);
32422 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32423 +               err = append_deblk(vdir);
32424 +               if (unlikely(err))
32425 +                       goto out;
32426 +
32427 +               p.deblk = last_deblk(vdir);
32428 +               deblk_end.deblk = p.deblk + deblk_sz;
32429 +               /* smp_mb(); */
32430 +               AuDebugOn(room->deblk != p.deblk);
32431 +       }
32432 +
32433 +       err = -ENOMEM;
32434 +       dehstr = au_cache_alloc_vdir_dehstr();
32435 +       if (unlikely(!dehstr))
32436 +               goto out;
32437 +
32438 +       dehstr->str = &room->de->de_str;
32439 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32440 +       room->de->de_ino = ino;
32441 +       room->de->de_type = d_type;
32442 +       room->de->de_str.len = nlen;
32443 +       memcpy(room->de->de_str.name, name, nlen);
32444 +
32445 +       err = 0;
32446 +       room->deblk += sz;
32447 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32448 +               err = append_deblk(vdir);
32449 +       /* smp_mb(); */
32450 +
32451 +out:
32452 +       return err;
32453 +}
32454 +
32455 +/* ---------------------------------------------------------------------- */
32456 +
32457 +void au_vdir_free(struct au_vdir *vdir)
32458 +{
32459 +       unsigned char **deblk;
32460 +
32461 +       deblk = vdir->vd_deblk;
32462 +       while (vdir->vd_nblk--)
32463 +               au_kfree_try_rcu(*deblk++);
32464 +       au_kfree_try_rcu(vdir->vd_deblk);
32465 +       au_cache_free_vdir(vdir);
32466 +}
32467 +
32468 +static struct au_vdir *alloc_vdir(struct file *file)
32469 +{
32470 +       struct au_vdir *vdir;
32471 +       struct super_block *sb;
32472 +       int err;
32473 +
32474 +       sb = file->f_path.dentry->d_sb;
32475 +       SiMustAnyLock(sb);
32476 +
32477 +       err = -ENOMEM;
32478 +       vdir = au_cache_alloc_vdir();
32479 +       if (unlikely(!vdir))
32480 +               goto out;
32481 +
32482 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32483 +       if (unlikely(!vdir->vd_deblk))
32484 +               goto out_free;
32485 +
32486 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32487 +       if (!vdir->vd_deblk_sz) {
32488 +               /* estimate the appropriate size for deblk */
32489 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32490 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32491 +       }
32492 +       vdir->vd_nblk = 0;
32493 +       vdir->vd_version = 0;
32494 +       vdir->vd_jiffy = 0;
32495 +       err = append_deblk(vdir);
32496 +       if (!err)
32497 +               return vdir; /* success */
32498 +
32499 +       au_kfree_try_rcu(vdir->vd_deblk);
32500 +
32501 +out_free:
32502 +       au_cache_free_vdir(vdir);
32503 +out:
32504 +       vdir = ERR_PTR(err);
32505 +       return vdir;
32506 +}
32507 +
32508 +static int reinit_vdir(struct au_vdir *vdir)
32509 +{
32510 +       int err;
32511 +       union au_vdir_deblk_p p, deblk_end;
32512 +
32513 +       while (vdir->vd_nblk > 1) {
32514 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32515 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32516 +               vdir->vd_nblk--;
32517 +       }
32518 +       p.deblk = vdir->vd_deblk[0];
32519 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32520 +       err = set_deblk_end(&p, &deblk_end);
32521 +       /* keep vd_dblk_sz */
32522 +       vdir->vd_last.ul = 0;
32523 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32524 +       vdir->vd_version = 0;
32525 +       vdir->vd_jiffy = 0;
32526 +       /* smp_mb(); */
32527 +       return err;
32528 +}
32529 +
32530 +/* ---------------------------------------------------------------------- */
32531 +
32532 +#define AuFillVdir_CALLED      BIT(0)
32533 +#define AuFillVdir_WHABLE      BIT(1)
32534 +#define AuFillVdir_SHWH                BIT(2)
32535 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32536 +#define au_fset_fillvdir(flags, name) \
32537 +       do { (flags) |= AuFillVdir_##name; } while (0)
32538 +#define au_fclr_fillvdir(flags, name) \
32539 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32540 +
32541 +#ifndef CONFIG_AUFS_SHWH
32542 +#undef AuFillVdir_SHWH
32543 +#define AuFillVdir_SHWH                0
32544 +#endif
32545 +
32546 +struct fillvdir_arg {
32547 +       struct dir_context      ctx;
32548 +       struct file             *file;
32549 +       struct au_vdir          *vdir;
32550 +       struct au_nhash         delist;
32551 +       struct au_nhash         whlist;
32552 +       aufs_bindex_t           bindex;
32553 +       unsigned int            flags;
32554 +       int                     err;
32555 +};
32556 +
32557 +static bool fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32558 +                   loff_t offset __maybe_unused, u64 h_ino,
32559 +                   unsigned int d_type)
32560 +{
32561 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32562 +       char *name = (void *)__name;
32563 +       struct super_block *sb;
32564 +       ino_t ino;
32565 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32566 +
32567 +       arg->err = 0;
32568 +       sb = arg->file->f_path.dentry->d_sb;
32569 +       au_fset_fillvdir(arg->flags, CALLED);
32570 +       /* smp_mb(); */
32571 +       if (nlen <= AUFS_WH_PFX_LEN
32572 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32573 +               if (test_known(&arg->delist, name, nlen)
32574 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32575 +                       goto out; /* already exists or whiteouted */
32576 +
32577 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32578 +               if (!arg->err) {
32579 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32580 +                               d_type = DT_UNKNOWN;
32581 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32582 +                                            d_type, &arg->delist);
32583 +               }
32584 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32585 +               name += AUFS_WH_PFX_LEN;
32586 +               nlen -= AUFS_WH_PFX_LEN;
32587 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32588 +                       goto out; /* already whiteouted */
32589 +
32590 +               ino = 0; /* just to suppress a warning */
32591 +               if (shwh)
32592 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32593 +                                            &ino);
32594 +               if (!arg->err) {
32595 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32596 +                               d_type = DT_UNKNOWN;
32597 +                       arg->err = au_nhash_append_wh
32598 +                               (&arg->whlist, name, nlen, ino, d_type,
32599 +                                arg->bindex, shwh);
32600 +               }
32601 +       }
32602 +
32603 +out:
32604 +       if (!arg->err)
32605 +               arg->vdir->vd_jiffy = jiffies;
32606 +       /* smp_mb(); */
32607 +       AuTraceErr(arg->err);
32608 +       return !arg->err;
32609 +}
32610 +
32611 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32612 +                         struct au_nhash *whlist, struct au_nhash *delist)
32613 +{
32614 +#ifdef CONFIG_AUFS_SHWH
32615 +       int err;
32616 +       unsigned int nh, u;
32617 +       struct hlist_head *head;
32618 +       struct au_vdir_wh *pos;
32619 +       struct hlist_node *n;
32620 +       char *p, *o;
32621 +       struct au_vdir_destr *destr;
32622 +
32623 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32624 +
32625 +       err = -ENOMEM;
32626 +       o = p = (void *)__get_free_page(GFP_NOFS);
32627 +       if (unlikely(!p))
32628 +               goto out;
32629 +
32630 +       err = 0;
32631 +       nh = whlist->nh_num;
32632 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32633 +       p += AUFS_WH_PFX_LEN;
32634 +       for (u = 0; u < nh; u++) {
32635 +               head = whlist->nh_head + u;
32636 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32637 +                       destr = &pos->wh_str;
32638 +                       memcpy(p, destr->name, destr->len);
32639 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32640 +                                       pos->wh_ino, pos->wh_type, delist);
32641 +                       if (unlikely(err))
32642 +                               break;
32643 +               }
32644 +       }
32645 +
32646 +       free_page((unsigned long)o);
32647 +
32648 +out:
32649 +       AuTraceErr(err);
32650 +       return err;
32651 +#else
32652 +       return 0;
32653 +#endif
32654 +}
32655 +
32656 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32657 +{
32658 +       int err;
32659 +       unsigned int rdhash;
32660 +       loff_t offset;
32661 +       aufs_bindex_t bbot, bindex, btop;
32662 +       unsigned char shwh;
32663 +       struct file *hf, *file;
32664 +       struct super_block *sb;
32665 +
32666 +       file = arg->file;
32667 +       sb = file->f_path.dentry->d_sb;
32668 +       SiMustAnyLock(sb);
32669 +
32670 +       rdhash = au_sbi(sb)->si_rdhash;
32671 +       if (!rdhash)
32672 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32673 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32674 +       if (unlikely(err))
32675 +               goto out;
32676 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32677 +       if (unlikely(err))
32678 +               goto out_delist;
32679 +
32680 +       err = 0;
32681 +       arg->flags = 0;
32682 +       shwh = 0;
32683 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32684 +               shwh = 1;
32685 +               au_fset_fillvdir(arg->flags, SHWH);
32686 +       }
32687 +       btop = au_fbtop(file);
32688 +       bbot = au_fbbot_dir(file);
32689 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32690 +               hf = au_hf_dir(file, bindex);
32691 +               if (!hf)
32692 +                       continue;
32693 +
32694 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32695 +               err = offset;
32696 +               if (unlikely(offset))
32697 +                       break;
32698 +
32699 +               arg->bindex = bindex;
32700 +               au_fclr_fillvdir(arg->flags, WHABLE);
32701 +               if (shwh
32702 +                   || (bindex != bbot
32703 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32704 +                       au_fset_fillvdir(arg->flags, WHABLE);
32705 +               do {
32706 +                       arg->err = 0;
32707 +                       au_fclr_fillvdir(arg->flags, CALLED);
32708 +                       /* smp_mb(); */
32709 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32710 +                       if (err >= 0)
32711 +                               err = arg->err;
32712 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32713 +
32714 +               /*
32715 +                * dir_relax() may be good for concurrency, but aufs should not
32716 +                * use it since it will cause a lockdep problem.
32717 +                */
32718 +       }
32719 +
32720 +       if (!err && shwh)
32721 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32722 +
32723 +       au_nhash_wh_free(&arg->whlist);
32724 +
32725 +out_delist:
32726 +       au_nhash_de_free(&arg->delist);
32727 +out:
32728 +       return err;
32729 +}
32730 +
32731 +static int read_vdir(struct file *file, int may_read)
32732 +{
32733 +       int err;
32734 +       unsigned long expire;
32735 +       unsigned char do_read;
32736 +       struct fillvdir_arg arg = {
32737 +               .ctx = {
32738 +                       .actor = fillvdir
32739 +               }
32740 +       };
32741 +       struct inode *inode;
32742 +       struct au_vdir *vdir, *allocated;
32743 +
32744 +       err = 0;
32745 +       inode = file_inode(file);
32746 +       IMustLock(inode);
32747 +       IiMustWriteLock(inode);
32748 +       SiMustAnyLock(inode->i_sb);
32749 +
32750 +       allocated = NULL;
32751 +       do_read = 0;
32752 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32753 +       vdir = au_ivdir(inode);
32754 +       if (!vdir) {
32755 +               do_read = 1;
32756 +               vdir = alloc_vdir(file);
32757 +               err = PTR_ERR(vdir);
32758 +               if (IS_ERR(vdir))
32759 +                       goto out;
32760 +               err = 0;
32761 +               allocated = vdir;
32762 +       } else if (may_read
32763 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32764 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32765 +               do_read = 1;
32766 +               err = reinit_vdir(vdir);
32767 +               if (unlikely(err))
32768 +                       goto out;
32769 +       }
32770 +
32771 +       if (!do_read)
32772 +               return 0; /* success */
32773 +
32774 +       arg.file = file;
32775 +       arg.vdir = vdir;
32776 +       err = au_do_read_vdir(&arg);
32777 +       if (!err) {
32778 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32779 +               vdir->vd_version = inode_query_iversion(inode);
32780 +               vdir->vd_last.ul = 0;
32781 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32782 +               if (allocated)
32783 +                       au_set_ivdir(inode, allocated);
32784 +       } else if (allocated)
32785 +               au_vdir_free(allocated);
32786 +
32787 +out:
32788 +       return err;
32789 +}
32790 +
32791 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32792 +{
32793 +       int err, rerr;
32794 +       unsigned long ul, n;
32795 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32796 +
32797 +       AuDebugOn(tgt->vd_nblk != 1);
32798 +
32799 +       err = -ENOMEM;
32800 +       if (tgt->vd_nblk < src->vd_nblk) {
32801 +               unsigned char **p;
32802 +
32803 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32804 +                               GFP_NOFS, /*may_shrink*/0);
32805 +               if (unlikely(!p))
32806 +                       goto out;
32807 +               tgt->vd_deblk = p;
32808 +       }
32809 +
32810 +       if (tgt->vd_deblk_sz != deblk_sz) {
32811 +               unsigned char *p;
32812 +
32813 +               tgt->vd_deblk_sz = deblk_sz;
32814 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32815 +                               /*may_shrink*/1);
32816 +               if (unlikely(!p))
32817 +                       goto out;
32818 +               tgt->vd_deblk[0] = p;
32819 +       }
32820 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32821 +       tgt->vd_version = src->vd_version;
32822 +       tgt->vd_jiffy = src->vd_jiffy;
32823 +
32824 +       n = src->vd_nblk;
32825 +       for (ul = 1; ul < n; ul++) {
32826 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32827 +                                           GFP_NOFS);
32828 +               if (unlikely(!tgt->vd_deblk[ul]))
32829 +                       goto out;
32830 +               tgt->vd_nblk++;
32831 +       }
32832 +       tgt->vd_nblk = n;
32833 +       tgt->vd_last.ul = tgt->vd_last.ul;
32834 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32835 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32836 +               - src->vd_deblk[src->vd_last.ul];
32837 +       /* smp_mb(); */
32838 +       return 0; /* success */
32839 +
32840 +out:
32841 +       rerr = reinit_vdir(tgt);
32842 +       BUG_ON(rerr);
32843 +       return err;
32844 +}
32845 +
32846 +int au_vdir_init(struct file *file)
32847 +{
32848 +       int err;
32849 +       struct inode *inode;
32850 +       struct au_vdir *vdir_cache, *allocated;
32851 +
32852 +       /* test file->f_pos here instead of ctx->pos */
32853 +       err = read_vdir(file, !file->f_pos);
32854 +       if (unlikely(err))
32855 +               goto out;
32856 +
32857 +       allocated = NULL;
32858 +       vdir_cache = au_fvdir_cache(file);
32859 +       if (!vdir_cache) {
32860 +               vdir_cache = alloc_vdir(file);
32861 +               err = PTR_ERR(vdir_cache);
32862 +               if (IS_ERR(vdir_cache))
32863 +                       goto out;
32864 +               allocated = vdir_cache;
32865 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32866 +               /* test file->f_pos here instead of ctx->pos */
32867 +               err = reinit_vdir(vdir_cache);
32868 +               if (unlikely(err))
32869 +                       goto out;
32870 +       } else
32871 +               return 0; /* success */
32872 +
32873 +       inode = file_inode(file);
32874 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32875 +       if (!err) {
32876 +               file->f_version = inode_query_iversion(inode);
32877 +               if (allocated)
32878 +                       au_set_fvdir_cache(file, allocated);
32879 +       } else if (allocated)
32880 +               au_vdir_free(allocated);
32881 +
32882 +out:
32883 +       return err;
32884 +}
32885 +
32886 +static loff_t calc_offset(struct au_vdir *vdir)
32887 +{
32888 +       loff_t offset;
32889 +       union au_vdir_deblk_p p;
32890 +
32891 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32892 +       offset = vdir->vd_last.p.deblk - p.deblk;
32893 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32894 +       return offset;
32895 +}
32896 +
32897 +/* returns true or false */
32898 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32899 +{
32900 +       int valid;
32901 +       unsigned int deblk_sz;
32902 +       unsigned long ul, n;
32903 +       loff_t offset;
32904 +       union au_vdir_deblk_p p, deblk_end;
32905 +       struct au_vdir *vdir_cache;
32906 +
32907 +       valid = 1;
32908 +       vdir_cache = au_fvdir_cache(file);
32909 +       offset = calc_offset(vdir_cache);
32910 +       AuDbg("offset %lld\n", offset);
32911 +       if (ctx->pos == offset)
32912 +               goto out;
32913 +
32914 +       vdir_cache->vd_last.ul = 0;
32915 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32916 +       if (!ctx->pos)
32917 +               goto out;
32918 +
32919 +       valid = 0;
32920 +       deblk_sz = vdir_cache->vd_deblk_sz;
32921 +       ul = div64_u64(ctx->pos, deblk_sz);
32922 +       AuDbg("ul %lu\n", ul);
32923 +       if (ul >= vdir_cache->vd_nblk)
32924 +               goto out;
32925 +
32926 +       n = vdir_cache->vd_nblk;
32927 +       for (; ul < n; ul++) {
32928 +               p.deblk = vdir_cache->vd_deblk[ul];
32929 +               deblk_end.deblk = p.deblk + deblk_sz;
32930 +               offset = ul;
32931 +               offset *= deblk_sz;
32932 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32933 +                       unsigned int l;
32934 +
32935 +                       l = calc_size(p.de->de_str.len);
32936 +                       offset += l;
32937 +                       p.deblk += l;
32938 +               }
32939 +               if (!is_deblk_end(&p, &deblk_end)) {
32940 +                       valid = 1;
32941 +                       vdir_cache->vd_last.ul = ul;
32942 +                       vdir_cache->vd_last.p = p;
32943 +                       break;
32944 +               }
32945 +       }
32946 +
32947 +out:
32948 +       /* smp_mb(); */
32949 +       if (!valid)
32950 +               AuDbg("valid %d\n", !valid);
32951 +       return valid;
32952 +}
32953 +
32954 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32955 +{
32956 +       unsigned int l, deblk_sz;
32957 +       union au_vdir_deblk_p deblk_end;
32958 +       struct au_vdir *vdir_cache;
32959 +       struct au_vdir_de *de;
32960 +
32961 +       if (!seek_vdir(file, ctx))
32962 +               return 0;
32963 +
32964 +       vdir_cache = au_fvdir_cache(file);
32965 +       deblk_sz = vdir_cache->vd_deblk_sz;
32966 +       while (1) {
32967 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32968 +               deblk_end.deblk += deblk_sz;
32969 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32970 +                       de = vdir_cache->vd_last.p.de;
32971 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32972 +                             de->de_str.len, de->de_str.name, ctx->pos,
32973 +                             (unsigned long)de->de_ino, de->de_type);
32974 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32975 +                                              de->de_str.len, de->de_ino,
32976 +                                              de->de_type))) {
32977 +                               /* todo: ignore the error caused by udba? */
32978 +                               /* return err; */
32979 +                               return 0;
32980 +                       }
32981 +
32982 +                       l = calc_size(de->de_str.len);
32983 +                       vdir_cache->vd_last.p.deblk += l;
32984 +                       ctx->pos += l;
32985 +               }
32986 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32987 +                       vdir_cache->vd_last.ul++;
32988 +                       vdir_cache->vd_last.p.deblk
32989 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32990 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32991 +                       continue;
32992 +               }
32993 +               break;
32994 +       }
32995 +
32996 +       /* smp_mb(); */
32997 +       return 0;
32998 +}
32999 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
33000 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
33001 +++ linux/fs/aufs/vfsub.c       2024-05-13 17:13:00.621404284 +0200
33002 @@ -0,0 +1,921 @@
33003 +// SPDX-License-Identifier: GPL-2.0
33004 +/*
33005 + * Copyright (C) 2005-2022 Junjiro R. Okajima
33006 + *
33007 + * This program is free software; you can redistribute it and/or modify
33008 + * it under the terms of the GNU General Public License as published by
33009 + * the Free Software Foundation; either version 2 of the License, or
33010 + * (at your option) any later version.
33011 + *
33012 + * This program is distributed in the hope that it will be useful,
33013 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33014 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33015 + * GNU General Public License for more details.
33016 + *
33017 + * You should have received a copy of the GNU General Public License
33018 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33019 + */
33020 +
33021 +/*
33022 + * sub-routines for VFS
33023 + */
33024 +
33025 +#include <linux/mnt_namespace.h>
33026 +#include <linux/nsproxy.h>
33027 +#include <linux/security.h>
33028 +#include <linux/splice.h>
33029 +#include "aufs.h"
33030 +
33031 +#ifdef CONFIG_AUFS_BR_FUSE
33032 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
33033 +{
33034 +       if (!au_test_fuse(h_sb) || !au_userns)
33035 +               return 0;
33036 +
33037 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
33038 +}
33039 +#endif
33040 +
33041 +int vfsub_sync_filesystem(struct super_block *h_sb)
33042 +{
33043 +       int err;
33044 +
33045 +       lockdep_off();
33046 +       down_read(&h_sb->s_umount);
33047 +       err = sync_filesystem(h_sb);
33048 +       up_read(&h_sb->s_umount);
33049 +       lockdep_on();
33050 +
33051 +       return err;
33052 +}
33053 +
33054 +/* ---------------------------------------------------------------------- */
33055 +
33056 +int vfsub_update_h_iattr(struct path *h_path, int *did)
33057 +{
33058 +       int err;
33059 +       struct kstat st;
33060 +       struct super_block *h_sb;
33061 +
33062 +       /*
33063 +        * Always needs h_path->mnt for LSM or FUSE branch.
33064 +        */
33065 +       AuDebugOn(!h_path->mnt);
33066 +
33067 +       /* for remote fs, leave work for its getattr or d_revalidate */
33068 +       /* for bad i_attr fs, handle them in aufs_getattr() */
33069 +       /* still some fs may acquire i_mutex. we need to skip them */
33070 +       err = 0;
33071 +       if (!did)
33072 +               did = &err;
33073 +       h_sb = h_path->dentry->d_sb;
33074 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
33075 +       if (*did)
33076 +               err = vfsub_getattr(h_path, &st);
33077 +
33078 +       return err;
33079 +}
33080 +
33081 +/* ---------------------------------------------------------------------- */
33082 +
33083 +struct file *vfsub_dentry_open(struct path *path, int flags)
33084 +{
33085 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
33086 +                          current_cred());
33087 +}
33088 +
33089 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
33090 +{
33091 +       struct file *file;
33092 +
33093 +       lockdep_off();
33094 +       file = filp_open(path,
33095 +                        oflags /* | __FMODE_NONOTIFY */,
33096 +                        mode);
33097 +       lockdep_on();
33098 +       if (IS_ERR(file))
33099 +               goto out;
33100 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33101 +
33102 +out:
33103 +       return file;
33104 +}
33105 +
33106 +/*
33107 + * Ideally this function should call VFS:do_last() in order to keep all its
33108 + * checkings. But it is very hard for aufs to regenerate several VFS internal
33109 + * structure such as nameidata. This is a second (or third) best approach.
33110 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
33111 + */
33112 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33113 +                     struct vfsub_aopen_args *args)
33114 +{
33115 +       int err;
33116 +       struct au_branch *br = args->br;
33117 +       struct file *file = args->file;
33118 +       /* copied from linux/fs/namei.c:atomic_open() */
33119 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33120 +
33121 +       IMustLock(dir);
33122 +       AuDebugOn(!dir->i_op->atomic_open);
33123 +
33124 +       err = au_br_test_oflag(args->open_flag, br);
33125 +       if (unlikely(err))
33126 +               goto out;
33127 +
33128 +       au_lcnt_inc(&br->br_nfiles);
33129 +       file->f_path.dentry = DENTRY_NOT_SET;
33130 +       file->f_path.mnt = au_br_mnt(br);
33131 +       AuDbg("%ps\n", dir->i_op->atomic_open);
33132 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
33133 +                                    args->create_mode);
33134 +       if (unlikely(err < 0)) {
33135 +               au_lcnt_dec(&br->br_nfiles);
33136 +               goto out;
33137 +       }
33138 +
33139 +       /* temporary workaround for nfsv4 branch */
33140 +       if (au_test_nfs(dir->i_sb))
33141 +               nfs_mark_for_revalidate(dir);
33142 +
33143 +       if (file->f_mode & FMODE_CREATED)
33144 +               fsnotify_create(dir, dentry);
33145 +       if (!(file->f_mode & FMODE_OPENED)) {
33146 +               au_lcnt_dec(&br->br_nfiles);
33147 +               goto out;
33148 +       }
33149 +
33150 +       /* todo: call VFS:may_open() here */
33151 +       /* todo: ima_file_check() too? */
33152 +       if (!err && (args->open_flag & __FMODE_EXEC))
33153 +               err = deny_write_access(file);
33154 +       if (!err)
33155 +               fsnotify_open(file);
33156 +       else
33157 +               au_lcnt_dec(&br->br_nfiles);
33158 +       /* note that the file is created and still opened */
33159 +
33160 +out:
33161 +       return err;
33162 +}
33163 +
33164 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33165 +{
33166 +       int err;
33167 +
33168 +       err = kern_path(name, flags, path);
33169 +       if (!err && d_is_positive(path->dentry))
33170 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33171 +       return err;
33172 +}
33173 +
33174 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33175 +                                            struct path *ppath, int len)
33176 +{
33177 +       struct path path;
33178 +
33179 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
33180 +       if (IS_ERR(path.dentry))
33181 +               goto out;
33182 +       if (d_is_positive(path.dentry)) {
33183 +               path.mnt = ppath->mnt;
33184 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33185 +       }
33186 +
33187 +out:
33188 +       AuTraceErrPtr(path.dentry);
33189 +       return path.dentry;
33190 +}
33191 +
33192 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33193 +                                   int len)
33194 +{
33195 +       struct path path;
33196 +
33197 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33198 +       IMustLock(d_inode(ppath->dentry));
33199 +
33200 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
33201 +       if (IS_ERR(path.dentry))
33202 +               goto out;
33203 +       if (d_is_positive(path.dentry)) {
33204 +               path.mnt = ppath->mnt;
33205 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33206 +       }
33207 +
33208 +out:
33209 +       AuTraceErrPtr(path.dentry);
33210 +       return path.dentry;
33211 +}
33212 +
33213 +void vfsub_call_lkup_one(void *args)
33214 +{
33215 +       struct vfsub_lkup_one_args *a = args;
33216 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
33217 +}
33218 +
33219 +/* ---------------------------------------------------------------------- */
33220 +
33221 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33222 +                                struct dentry *d2, struct au_hinode *hdir2)
33223 +{
33224 +       struct dentry *d;
33225 +
33226 +       lockdep_off();
33227 +       d = lock_rename(d1, d2);
33228 +       lockdep_on();
33229 +       if (IS_ERR(d))
33230 +               goto out;
33231 +       au_hn_suspend(hdir1);
33232 +       if (hdir1 != hdir2)
33233 +               au_hn_suspend(hdir2);
33234 +
33235 +out:
33236 +       return d;
33237 +}
33238 +
33239 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33240 +                        struct dentry *d2, struct au_hinode *hdir2)
33241 +{
33242 +       au_hn_resume(hdir1);
33243 +       if (hdir1 != hdir2)
33244 +               au_hn_resume(hdir2);
33245 +       lockdep_off();
33246 +       unlock_rename(d1, d2);
33247 +       lockdep_on();
33248 +}
33249 +
33250 +/* ---------------------------------------------------------------------- */
33251 +
33252 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33253 +{
33254 +       int err;
33255 +       struct dentry *d;
33256 +       struct mnt_idmap *idmap;
33257 +
33258 +       IMustLock(dir);
33259 +
33260 +       d = path->dentry;
33261 +       path->dentry = d->d_parent;
33262 +       err = security_path_mknod(path, d, mode, 0);
33263 +       path->dentry = d;
33264 +       if (unlikely(err))
33265 +               goto out;
33266 +       idmap = mnt_idmap(path->mnt);
33267 +
33268 +       lockdep_off();
33269 +       err = vfs_create(idmap, dir, path->dentry, mode, want_excl);
33270 +       lockdep_on();
33271 +       if (!err) {
33272 +               struct path tmp = *path;
33273 +               int did;
33274 +
33275 +               vfsub_update_h_iattr(&tmp, &did);
33276 +               if (did) {
33277 +                       tmp.dentry = path->dentry->d_parent;
33278 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33279 +               }
33280 +               /*ignore*/
33281 +       }
33282 +
33283 +out:
33284 +       return err;
33285 +}
33286 +
33287 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33288 +{
33289 +       int err;
33290 +       struct dentry *d;
33291 +       struct mnt_idmap *idmap;
33292 +
33293 +       IMustLock(dir);
33294 +
33295 +       d = path->dentry;
33296 +       path->dentry = d->d_parent;
33297 +       err = security_path_symlink(path, d, symname);
33298 +       path->dentry = d;
33299 +       if (unlikely(err))
33300 +               goto out;
33301 +       idmap = mnt_idmap(path->mnt);
33302 +
33303 +       lockdep_off();
33304 +       err = vfs_symlink(idmap, dir, path->dentry, symname);
33305 +       lockdep_on();
33306 +       if (!err) {
33307 +               struct path tmp = *path;
33308 +               int did;
33309 +
33310 +               vfsub_update_h_iattr(&tmp, &did);
33311 +               if (did) {
33312 +                       tmp.dentry = path->dentry->d_parent;
33313 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33314 +               }
33315 +               /*ignore*/
33316 +       }
33317 +
33318 +out:
33319 +       return err;
33320 +}
33321 +
33322 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33323 +{
33324 +       int err;
33325 +       struct dentry *d;
33326 +       struct mnt_idmap *idmap;
33327 +
33328 +       IMustLock(dir);
33329 +
33330 +       d = path->dentry;
33331 +       path->dentry = d->d_parent;
33332 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33333 +       path->dentry = d;
33334 +       if (unlikely(err))
33335 +               goto out;
33336 +       idmap = mnt_idmap(path->mnt);
33337 +
33338 +       lockdep_off();
33339 +       err = vfs_mknod(idmap, dir, path->dentry, mode, dev);
33340 +       lockdep_on();
33341 +       if (!err) {
33342 +               struct path tmp = *path;
33343 +               int did;
33344 +
33345 +               vfsub_update_h_iattr(&tmp, &did);
33346 +               if (did) {
33347 +                       tmp.dentry = path->dentry->d_parent;
33348 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33349 +               }
33350 +               /*ignore*/
33351 +       }
33352 +
33353 +out:
33354 +       return err;
33355 +}
33356 +
33357 +static int au_test_nlink(struct inode *inode)
33358 +{
33359 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33360 +
33361 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33362 +           || inode->i_nlink < link_max)
33363 +               return 0;
33364 +       return -EMLINK;
33365 +}
33366 +
33367 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33368 +              struct inode **delegated_inode)
33369 +{
33370 +       int err;
33371 +       struct dentry *d;
33372 +       struct mnt_idmap *idmap;
33373 +
33374 +       IMustLock(dir);
33375 +
33376 +       err = au_test_nlink(d_inode(src_dentry));
33377 +       if (unlikely(err))
33378 +               return err;
33379 +
33380 +       /* we don't call may_linkat() */
33381 +       d = path->dentry;
33382 +       path->dentry = d->d_parent;
33383 +       err = security_path_link(src_dentry, path, d);
33384 +       path->dentry = d;
33385 +       if (unlikely(err))
33386 +               goto out;
33387 +       idmap = mnt_idmap(path->mnt);
33388 +
33389 +       lockdep_off();
33390 +       err = vfs_link(src_dentry, idmap, dir, path->dentry, delegated_inode);
33391 +       lockdep_on();
33392 +       if (!err) {
33393 +               struct path tmp = *path;
33394 +               int did;
33395 +
33396 +               /* fuse has different memory inode for the same inumber */
33397 +               vfsub_update_h_iattr(&tmp, &did);
33398 +               if (did) {
33399 +                       tmp.dentry = path->dentry->d_parent;
33400 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33401 +                       tmp.dentry = src_dentry;
33402 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33403 +               }
33404 +               /*ignore*/
33405 +       }
33406 +
33407 +out:
33408 +       return err;
33409 +}
33410 +
33411 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33412 +                struct inode *dir, struct path *path,
33413 +                struct inode **delegated_inode, unsigned int flags)
33414 +{
33415 +       int err;
33416 +       struct renamedata rd;
33417 +       struct path tmp = {
33418 +               .mnt    = path->mnt
33419 +       };
33420 +       struct dentry *d;
33421 +
33422 +       IMustLock(dir);
33423 +       IMustLock(src_dir);
33424 +
33425 +       d = path->dentry;
33426 +       path->dentry = d->d_parent;
33427 +       tmp.dentry = src_dentry->d_parent;
33428 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33429 +       path->dentry = d;
33430 +       if (unlikely(err))
33431 +               goto out;
33432 +
33433 +       rd.old_mnt_idmap = mnt_idmap(path->mnt);
33434 +       rd.old_dir = src_dir;
33435 +       rd.old_dentry = src_dentry;
33436 +       rd.new_mnt_idmap = rd.old_mnt_idmap;
33437 +       rd.new_dir = dir;
33438 +       rd.new_dentry = path->dentry;
33439 +       rd.delegated_inode = delegated_inode;
33440 +       rd.flags = flags;
33441 +       lockdep_off();
33442 +       err = vfs_rename(&rd);
33443 +       lockdep_on();
33444 +       if (!err) {
33445 +               int did;
33446 +
33447 +               tmp.dentry = d->d_parent;
33448 +               vfsub_update_h_iattr(&tmp, &did);
33449 +               if (did) {
33450 +                       tmp.dentry = src_dentry;
33451 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33452 +                       tmp.dentry = src_dentry->d_parent;
33453 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33454 +               }
33455 +               /*ignore*/
33456 +       }
33457 +
33458 +out:
33459 +       return err;
33460 +}
33461 +
33462 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33463 +{
33464 +       int err;
33465 +       struct dentry *d;
33466 +       struct mnt_idmap *idmap;
33467 +
33468 +       IMustLock(dir);
33469 +
33470 +       d = path->dentry;
33471 +       path->dentry = d->d_parent;
33472 +       err = security_path_mkdir(path, d, mode);
33473 +       path->dentry = d;
33474 +       if (unlikely(err))
33475 +               goto out;
33476 +       idmap = mnt_idmap(path->mnt);
33477 +
33478 +       lockdep_off();
33479 +       err = vfs_mkdir(idmap, dir, path->dentry, mode);
33480 +       lockdep_on();
33481 +       if (!err) {
33482 +               struct path tmp = *path;
33483 +               int did;
33484 +
33485 +               vfsub_update_h_iattr(&tmp, &did);
33486 +               if (did) {
33487 +                       tmp.dentry = path->dentry->d_parent;
33488 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33489 +               }
33490 +               /*ignore*/
33491 +       }
33492 +
33493 +out:
33494 +       return err;
33495 +}
33496 +
33497 +int vfsub_rmdir(struct inode *dir, struct path *path)
33498 +{
33499 +       int err;
33500 +       struct dentry *d;
33501 +       struct mnt_idmap *idmap;
33502 +
33503 +       IMustLock(dir);
33504 +
33505 +       d = path->dentry;
33506 +       path->dentry = d->d_parent;
33507 +       err = security_path_rmdir(path, d);
33508 +       path->dentry = d;
33509 +       if (unlikely(err))
33510 +               goto out;
33511 +       idmap = mnt_idmap(path->mnt);
33512 +
33513 +       lockdep_off();
33514 +       err = vfs_rmdir(idmap, dir, path->dentry);
33515 +       lockdep_on();
33516 +       if (!err) {
33517 +               struct path tmp = {
33518 +                       .dentry = path->dentry->d_parent,
33519 +                       .mnt    = path->mnt
33520 +               };
33521 +
33522 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33523 +       }
33524 +
33525 +out:
33526 +       return err;
33527 +}
33528 +
33529 +/* ---------------------------------------------------------------------- */
33530 +
33531 +/* todo: support mmap_sem? */
33532 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33533 +                    loff_t *ppos)
33534 +{
33535 +       ssize_t err;
33536 +
33537 +       lockdep_off();
33538 +       err = vfs_read(file, ubuf, count, ppos);
33539 +       lockdep_on();
33540 +       if (err >= 0)
33541 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33542 +       return err;
33543 +}
33544 +
33545 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33546 +                    loff_t *ppos)
33547 +{
33548 +       ssize_t err;
33549 +
33550 +       lockdep_off();
33551 +       err = kernel_read(file, kbuf, count, ppos);
33552 +       lockdep_on();
33553 +       AuTraceErr(err);
33554 +       if (err >= 0)
33555 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33556 +       return err;
33557 +}
33558 +
33559 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33560 +                     loff_t *ppos)
33561 +{
33562 +       ssize_t err;
33563 +
33564 +       lockdep_off();
33565 +       err = vfs_write(file, ubuf, count, ppos);
33566 +       lockdep_on();
33567 +       if (err >= 0)
33568 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33569 +       return err;
33570 +}
33571 +
33572 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33573 +{
33574 +       ssize_t err;
33575 +
33576 +       lockdep_off();
33577 +       err = kernel_write(file, kbuf, count, ppos);
33578 +       lockdep_on();
33579 +       if (err >= 0)
33580 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33581 +       return err;
33582 +}
33583 +
33584 +int vfsub_flush(struct file *file, fl_owner_t id)
33585 +{
33586 +       int err;
33587 +
33588 +       err = 0;
33589 +       if (file->f_op->flush) {
33590 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33591 +                       err = file->f_op->flush(file, id);
33592 +               else {
33593 +                       lockdep_off();
33594 +                       err = file->f_op->flush(file, id);
33595 +                       lockdep_on();
33596 +               }
33597 +               if (!err)
33598 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33599 +               /*ignore*/
33600 +       }
33601 +       return err;
33602 +}
33603 +
33604 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33605 +{
33606 +       int err;
33607 +
33608 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33609 +
33610 +       lockdep_off();
33611 +       err = iterate_dir(file, ctx);
33612 +       lockdep_on();
33613 +       if (err >= 0)
33614 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33615 +
33616 +       return err;
33617 +}
33618 +
33619 +ssize_t vfsub_splice_read(struct file *in, loff_t *ppos,
33620 +                         struct pipe_inode_info *pipe, size_t len,
33621 +                         unsigned int flags)
33622 +{
33623 +       ssize_t err;
33624 +
33625 +       lockdep_off();
33626 +       err = vfs_splice_read(in, ppos, pipe, len, flags);
33627 +       lockdep_on();
33628 +       file_accessed(in);
33629 +       if (err >= 0)
33630 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33631 +       return err;
33632 +}
33633 +
33634 +ssize_t vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33635 +                         loff_t *ppos, size_t len, unsigned int flags)
33636 +{
33637 +       ssize_t err;
33638 +
33639 +       lockdep_off();
33640 +       err = do_splice_from(pipe, out, ppos, len, flags);
33641 +       lockdep_on();
33642 +       if (err >= 0)
33643 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33644 +       return err;
33645 +}
33646 +
33647 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33648 +{
33649 +       int err;
33650 +
33651 +       /* file can be NULL */
33652 +       lockdep_off();
33653 +       err = vfs_fsync(file, datasync);
33654 +       lockdep_on();
33655 +       if (!err) {
33656 +               if (!path) {
33657 +                       AuDebugOn(!file);
33658 +                       path = &file->f_path;
33659 +               }
33660 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33661 +       }
33662 +       return err;
33663 +}
33664 +
33665 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33666 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33667 +               struct file *h_file)
33668 +{
33669 +       int err;
33670 +       struct inode *h_inode;
33671 +       struct super_block *h_sb;
33672 +       struct mnt_idmap *h_idmap;
33673 +
33674 +       if (!h_file) {
33675 +               err = vfsub_truncate(h_path, length);
33676 +               goto out;
33677 +       }
33678 +
33679 +       h_inode = d_inode(h_path->dentry);
33680 +       h_sb = h_inode->i_sb;
33681 +       lockdep_off();
33682 +       sb_start_write(h_sb);
33683 +       lockdep_on();
33684 +       err = security_file_truncate(h_file);
33685 +       if (!err) {
33686 +               h_idmap = mnt_idmap(h_path->mnt);
33687 +               lockdep_off();
33688 +               err = do_truncate(h_idmap, h_path->dentry, length, attr,
33689 +                                 h_file);
33690 +               lockdep_on();
33691 +       }
33692 +       lockdep_off();
33693 +       sb_end_write(h_sb);
33694 +       lockdep_on();
33695 +
33696 +out:
33697 +       return err;
33698 +}
33699 +
33700 +/* ---------------------------------------------------------------------- */
33701 +
33702 +struct au_vfsub_mkdir_args {
33703 +       int *errp;
33704 +       struct inode *dir;
33705 +       struct path *path;
33706 +       int mode;
33707 +};
33708 +
33709 +static void au_call_vfsub_mkdir(void *args)
33710 +{
33711 +       struct au_vfsub_mkdir_args *a = args;
33712 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33713 +}
33714 +
33715 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33716 +{
33717 +       int err, do_sio, wkq_err;
33718 +       struct mnt_idmap *idmap;
33719 +
33720 +       idmap = mnt_idmap(path->mnt);
33721 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33722 +       if (!do_sio) {
33723 +               lockdep_off();
33724 +               err = vfsub_mkdir(dir, path, mode);
33725 +               lockdep_on();
33726 +       } else {
33727 +               struct au_vfsub_mkdir_args args = {
33728 +                       .errp   = &err,
33729 +                       .dir    = dir,
33730 +                       .path   = path,
33731 +                       .mode   = mode
33732 +               };
33733 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33734 +               if (unlikely(wkq_err))
33735 +                       err = wkq_err;
33736 +       }
33737 +
33738 +       return err;
33739 +}
33740 +
33741 +struct au_vfsub_rmdir_args {
33742 +       int *errp;
33743 +       struct inode *dir;
33744 +       struct path *path;
33745 +};
33746 +
33747 +static void au_call_vfsub_rmdir(void *args)
33748 +{
33749 +       struct au_vfsub_rmdir_args *a = args;
33750 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33751 +}
33752 +
33753 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33754 +{
33755 +       int err, do_sio, wkq_err;
33756 +       struct mnt_idmap *idmap;
33757 +
33758 +       idmap = mnt_idmap(path->mnt);
33759 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33760 +       if (!do_sio) {
33761 +               lockdep_off();
33762 +               err = vfsub_rmdir(dir, path);
33763 +               lockdep_on();
33764 +       } else {
33765 +               struct au_vfsub_rmdir_args args = {
33766 +                       .errp   = &err,
33767 +                       .dir    = dir,
33768 +                       .path   = path
33769 +               };
33770 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33771 +               if (unlikely(wkq_err))
33772 +                       err = wkq_err;
33773 +       }
33774 +
33775 +       return err;
33776 +}
33777 +
33778 +/* ---------------------------------------------------------------------- */
33779 +
33780 +struct notify_change_args {
33781 +       int *errp;
33782 +       struct path *path;
33783 +       struct iattr *ia;
33784 +       struct inode **delegated_inode;
33785 +};
33786 +
33787 +static void call_notify_change(void *args)
33788 +{
33789 +       struct notify_change_args *a = args;
33790 +       struct inode *h_inode;
33791 +       struct mnt_idmap *idmap;
33792 +
33793 +       h_inode = d_inode(a->path->dentry);
33794 +       IMustLock(h_inode);
33795 +
33796 +       *a->errp = -EPERM;
33797 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33798 +               idmap = mnt_idmap(a->path->mnt);
33799 +               lockdep_off();
33800 +               *a->errp = notify_change(idmap, a->path->dentry, a->ia,
33801 +                                        a->delegated_inode);
33802 +               lockdep_on();
33803 +               if (!*a->errp)
33804 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33805 +       }
33806 +       AuTraceErr(*a->errp);
33807 +}
33808 +
33809 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33810 +                       struct inode **delegated_inode)
33811 +{
33812 +       int err;
33813 +       struct notify_change_args args = {
33814 +               .errp                   = &err,
33815 +               .path                   = path,
33816 +               .ia                     = ia,
33817 +               .delegated_inode        = delegated_inode
33818 +       };
33819 +
33820 +       call_notify_change(&args);
33821 +
33822 +       return err;
33823 +}
33824 +
33825 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33826 +                           struct inode **delegated_inode)
33827 +{
33828 +       int err, wkq_err;
33829 +       struct notify_change_args args = {
33830 +               .errp                   = &err,
33831 +               .path                   = path,
33832 +               .ia                     = ia,
33833 +               .delegated_inode        = delegated_inode
33834 +       };
33835 +
33836 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33837 +       if (unlikely(wkq_err))
33838 +               err = wkq_err;
33839 +
33840 +       return err;
33841 +}
33842 +
33843 +/* ---------------------------------------------------------------------- */
33844 +
33845 +struct unlink_args {
33846 +       int *errp;
33847 +       struct inode *dir;
33848 +       struct path *path;
33849 +       struct inode **delegated_inode;
33850 +};
33851 +
33852 +static void call_unlink(void *args)
33853 +{
33854 +       struct unlink_args *a = args;
33855 +       struct dentry *d = a->path->dentry;
33856 +       struct inode *h_inode;
33857 +       struct mnt_idmap *idmap;
33858 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33859 +                                     && au_dcount(d) == 1);
33860 +
33861 +       IMustLock(a->dir);
33862 +
33863 +       a->path->dentry = d->d_parent;
33864 +       *a->errp = security_path_unlink(a->path, d);
33865 +       a->path->dentry = d;
33866 +       if (unlikely(*a->errp))
33867 +               return;
33868 +
33869 +       if (!stop_sillyrename)
33870 +               dget(d);
33871 +       h_inode = NULL;
33872 +       if (d_is_positive(d)) {
33873 +               h_inode = d_inode(d);
33874 +               ihold(h_inode);
33875 +       }
33876 +
33877 +       idmap = mnt_idmap(a->path->mnt);
33878 +       lockdep_off();
33879 +       *a->errp = vfs_unlink(idmap, a->dir, d, a->delegated_inode);
33880 +       lockdep_on();
33881 +       if (!*a->errp) {
33882 +               struct path tmp = {
33883 +                       .dentry = d->d_parent,
33884 +                       .mnt    = a->path->mnt
33885 +               };
33886 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33887 +       }
33888 +
33889 +       if (!stop_sillyrename)
33890 +               dput(d);
33891 +       if (h_inode)
33892 +               iput(h_inode);
33893 +
33894 +       AuTraceErr(*a->errp);
33895 +}
33896 +
33897 +/*
33898 + * @dir: must be locked.
33899 + * @dentry: target dentry.
33900 + */
33901 +int vfsub_unlink(struct inode *dir, struct path *path,
33902 +                struct inode **delegated_inode, int force)
33903 +{
33904 +       int err;
33905 +       struct unlink_args args = {
33906 +               .errp                   = &err,
33907 +               .dir                    = dir,
33908 +               .path                   = path,
33909 +               .delegated_inode        = delegated_inode
33910 +       };
33911 +
33912 +       if (!force)
33913 +               call_unlink(&args);
33914 +       else {
33915 +               int wkq_err;
33916 +
33917 +               wkq_err = au_wkq_wait(call_unlink, &args);
33918 +               if (unlikely(wkq_err))
33919 +                       err = wkq_err;
33920 +       }
33921 +
33922 +       return err;
33923 +}
33924 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33925 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33926 +++ linux/fs/aufs/vfsub.h       2024-05-13 17:13:00.621404284 +0200
33927 @@ -0,0 +1,402 @@
33928 +/* SPDX-License-Identifier: GPL-2.0 */
33929 +/*
33930 + * Copyright (C) 2005-2022 Junjiro R. Okajima
33931 + *
33932 + * This program is free software; you can redistribute it and/or modify
33933 + * it under the terms of the GNU General Public License as published by
33934 + * the Free Software Foundation; either version 2 of the License, or
33935 + * (at your option) any later version.
33936 + *
33937 + * This program is distributed in the hope that it will be useful,
33938 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33939 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33940 + * GNU General Public License for more details.
33941 + *
33942 + * You should have received a copy of the GNU General Public License
33943 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33944 + */
33945 +
33946 +/*
33947 + * sub-routines for VFS
33948 + */
33949 +
33950 +#ifndef __AUFS_VFSUB_H__
33951 +#define __AUFS_VFSUB_H__
33952 +
33953 +#ifdef __KERNEL__
33954 +
33955 +#include <linux/fs.h>
33956 +#include <linux/mount.h>
33957 +#include <linux/posix_acl.h>
33958 +#include <linux/xattr.h>
33959 +#include "debug.h"
33960 +
33961 +/* copied from linux/fs/internal.h */
33962 +/* todo: BAD approach!! */
33963 +extern struct file *alloc_empty_file(int, const struct cred *);
33964 +
33965 +/* ---------------------------------------------------------------------- */
33966 +
33967 +/* lock subclass for lower inode */
33968 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33969 +/* reduce? gave up. */
33970 +enum {
33971 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33972 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33973 +       AuLsc_I_PARENT2,        /* copyup dirs */
33974 +       AuLsc_I_PARENT3,        /* copyup wh */
33975 +       AuLsc_I_CHILD,
33976 +       AuLsc_I_CHILD2,
33977 +       AuLsc_I_End
33978 +};
33979 +
33980 +/* to debug easier, do not make them inlined functions */
33981 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33982 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33983 +
33984 +/* ---------------------------------------------------------------------- */
33985 +
33986 +static inline void vfsub_drop_nlink(struct inode *inode)
33987 +{
33988 +       AuDebugOn(!inode->i_nlink);
33989 +       drop_nlink(inode);
33990 +}
33991 +
33992 +static inline void vfsub_dead_dir(struct inode *inode)
33993 +{
33994 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33995 +       inode->i_flags |= S_DEAD;
33996 +       clear_nlink(inode);
33997 +}
33998 +
33999 +static inline int vfsub_native_ro(struct inode *inode)
34000 +{
34001 +       return sb_rdonly(inode->i_sb)
34002 +               || IS_RDONLY(inode)
34003 +               /* || IS_APPEND(inode) */
34004 +               || IS_IMMUTABLE(inode);
34005 +}
34006 +
34007 +#ifdef CONFIG_AUFS_BR_FUSE
34008 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
34009 +#else
34010 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
34011 +#endif
34012 +
34013 +int vfsub_sync_filesystem(struct super_block *h_sb);
34014 +
34015 +/* ---------------------------------------------------------------------- */
34016 +
34017 +int vfsub_update_h_iattr(struct path *h_path, int *did);
34018 +struct file *vfsub_dentry_open(struct path *path, int flags);
34019 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
34020 +struct au_branch;
34021 +struct vfsub_aopen_args {
34022 +       struct file             *file;
34023 +       unsigned int            open_flag;
34024 +       umode_t                 create_mode;
34025 +       struct au_branch        *br;
34026 +};
34027 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
34028 +                     struct vfsub_aopen_args *args);
34029 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
34030 +
34031 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
34032 +                                            struct path *ppath, int len);
34033 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
34034 +                                   int len);
34035 +
34036 +struct vfsub_lkup_one_args {
34037 +       struct dentry **errp;
34038 +       struct qstr *name;
34039 +       struct path *ppath;
34040 +};
34041 +
34042 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
34043 +                                           struct path *ppath)
34044 +{
34045 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
34046 +}
34047 +
34048 +void vfsub_call_lkup_one(void *args);
34049 +
34050 +/* ---------------------------------------------------------------------- */
34051 +
34052 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
34053 +{
34054 +       int err;
34055 +
34056 +       lockdep_off();
34057 +       err = mnt_want_write(mnt);
34058 +       lockdep_on();
34059 +       return err;
34060 +}
34061 +
34062 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
34063 +{
34064 +       lockdep_off();
34065 +       mnt_drop_write(mnt);
34066 +       lockdep_on();
34067 +}
34068 +
34069 +#if 0 /* reserved */
34070 +static inline void vfsub_mnt_drop_write_file(struct file *file)
34071 +{
34072 +       lockdep_off();
34073 +       mnt_drop_write_file(file);
34074 +       lockdep_on();
34075 +}
34076 +#endif
34077 +
34078 +static inline void vfsub_file_start_write(struct file *file)
34079 +{
34080 +       lockdep_off();
34081 +       file_start_write(file);
34082 +       lockdep_on();
34083 +}
34084 +
34085 +static inline void vfsub_file_end_write(struct file *file)
34086 +{
34087 +       lockdep_off();
34088 +       file_end_write(file);
34089 +       lockdep_on();
34090 +}
34091 +
34092 +/* ---------------------------------------------------------------------- */
34093 +
34094 +struct au_hinode;
34095 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
34096 +                                struct dentry *d2, struct au_hinode *hdir2);
34097 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
34098 +                        struct dentry *d2, struct au_hinode *hdir2);
34099 +
34100 +int vfsub_create(struct inode *dir, struct path *path, int mode,
34101 +                bool want_excl);
34102 +int vfsub_symlink(struct inode *dir, struct path *path,
34103 +                 const char *symname);
34104 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
34105 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
34106 +              struct path *path, struct inode **delegated_inode);
34107 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
34108 +                struct inode *hdir, struct path *path,
34109 +                struct inode **delegated_inode, unsigned int flags);
34110 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
34111 +int vfsub_rmdir(struct inode *dir, struct path *path);
34112 +
34113 +/* ---------------------------------------------------------------------- */
34114 +
34115 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
34116 +                    loff_t *ppos);
34117 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
34118 +                       loff_t *ppos);
34119 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
34120 +                     loff_t *ppos);
34121 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
34122 +                     loff_t *ppos);
34123 +int vfsub_flush(struct file *file, fl_owner_t id);
34124 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
34125 +
34126 +static inline loff_t vfsub_f_size_read(struct file *file)
34127 +{
34128 +       return i_size_read(file_inode(file));
34129 +}
34130 +
34131 +static inline unsigned int vfsub_file_flags(struct file *file)
34132 +{
34133 +       unsigned int flags;
34134 +
34135 +       spin_lock(&file->f_lock);
34136 +       flags = file->f_flags;
34137 +       spin_unlock(&file->f_lock);
34138 +
34139 +       return flags;
34140 +}
34141 +
34142 +static inline int vfsub_file_execed(struct file *file)
34143 +{
34144 +       /* todo: direct access f_flags */
34145 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34146 +}
34147 +
34148 +#if 0 /* reserved */
34149 +static inline void vfsub_file_accessed(struct file *h_file)
34150 +{
34151 +       file_accessed(h_file);
34152 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34153 +}
34154 +#endif
34155 +
34156 +#if 0 /* reserved */
34157 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34158 +                                    struct dentry *h_dentry)
34159 +{
34160 +       struct path h_path = {
34161 +               .dentry = h_dentry,
34162 +               .mnt    = h_mnt
34163 +       };
34164 +       touch_atime(&h_path);
34165 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34166 +}
34167 +#endif
34168 +
34169 +static inline int vfsub_update_time(struct inode *h_inode, int flags)
34170 +{
34171 +       return inode_update_time(h_inode, flags);
34172 +       /* no vfsub_update_h_iattr() since we don't have struct path */
34173 +}
34174 +
34175 +#ifdef CONFIG_FS_POSIX_ACL
34176 +static inline int vfsub_acl_chmod(struct mnt_idmap *h_idmap,
34177 +                                 struct dentry *h_dentry, umode_t h_mode)
34178 +{
34179 +       int err;
34180 +
34181 +       err = posix_acl_chmod(h_idmap, h_dentry, h_mode);
34182 +       if (err == -EOPNOTSUPP)
34183 +               err = 0;
34184 +       return err;
34185 +}
34186 +#else
34187 +AuStubInt0(vfsub_acl_chmod, struct mnt_idmap *h_idmap,
34188 +          struct dentry *h_dentry, umode_t h_mode);
34189 +#endif
34190 +
34191 +ssize_t vfsub_splice_read(struct file *in, loff_t *ppos,
34192 +                         struct pipe_inode_info *pipe, size_t len,
34193 +                         unsigned int flags);
34194 +ssize_t vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34195 +                         loff_t *ppos, size_t len, unsigned int flags);
34196 +
34197 +static inline long vfsub_truncate(struct path *path, loff_t length)
34198 +{
34199 +       long err;
34200 +
34201 +       lockdep_off();
34202 +       err = vfs_truncate(path, length);
34203 +       lockdep_on();
34204 +       return err;
34205 +}
34206 +
34207 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34208 +               struct file *h_file);
34209 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34210 +
34211 +/*
34212 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34213 + * ioctl.
34214 + */
34215 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34216 +                                           loff_t len)
34217 +{
34218 +       loff_t err;
34219 +
34220 +       lockdep_off();
34221 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34222 +       lockdep_on();
34223 +
34224 +       return err;
34225 +}
34226 +
34227 +/* copy_file_range(2) is a systemcall */
34228 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34229 +                                           struct file *dst, loff_t dst_pos,
34230 +                                           size_t len, unsigned int flags)
34231 +{
34232 +       ssize_t ssz;
34233 +
34234 +       lockdep_off();
34235 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34236 +       lockdep_on();
34237 +
34238 +       return ssz;
34239 +}
34240 +
34241 +/* ---------------------------------------------------------------------- */
34242 +
34243 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34244 +{
34245 +       loff_t err;
34246 +
34247 +       lockdep_off();
34248 +       err = vfs_llseek(file, offset, origin);
34249 +       lockdep_on();
34250 +       return err;
34251 +}
34252 +
34253 +/* ---------------------------------------------------------------------- */
34254 +
34255 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34256 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34257 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34258 +                           struct inode **delegated_inode);
34259 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34260 +                       struct inode **delegated_inode);
34261 +int vfsub_unlink(struct inode *dir, struct path *path,
34262 +                struct inode **delegated_inode, int force);
34263 +
34264 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34265 +{
34266 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34267 +}
34268 +
34269 +/* ---------------------------------------------------------------------- */
34270 +
34271 +static inline int vfsub_setxattr(struct mnt_idmap *idmap,
34272 +                                struct dentry *dentry, const char *name,
34273 +                                const void *value, size_t size, int flags)
34274 +{
34275 +       int err;
34276 +
34277 +       lockdep_off();
34278 +       err = vfs_setxattr(idmap, dentry, name, value, size, flags);
34279 +       lockdep_on();
34280 +
34281 +       return err;
34282 +}
34283 +
34284 +static inline int vfsub_removexattr(struct mnt_idmap *idmap,
34285 +                                   struct dentry *dentry, const char *name)
34286 +{
34287 +       int err;
34288 +
34289 +       lockdep_off();
34290 +       err = vfs_removexattr(idmap, dentry, name);
34291 +       lockdep_on();
34292 +
34293 +       return err;
34294 +}
34295 +
34296 +#ifdef CONFIG_FS_POSIX_ACL
34297 +static inline int vfsub_set_acl(struct mnt_idmap *idmap,
34298 +                               struct dentry *dentry, const char *name,
34299 +                               struct posix_acl *acl)
34300 +{
34301 +       int err;
34302 +
34303 +       lockdep_off();
34304 +       err = vfs_set_acl(idmap, dentry, name, acl);
34305 +       lockdep_on();
34306 +
34307 +       return err;
34308 +}
34309 +
34310 +static inline int vfsub_remove_acl(struct mnt_idmap *idmap,
34311 +                                  struct dentry *dentry, const char *name)
34312 +{
34313 +       int err;
34314 +
34315 +       lockdep_off();
34316 +       err = vfs_remove_acl(idmap, dentry, name);
34317 +       lockdep_on();
34318 +
34319 +       return err;
34320 +}
34321 +#else
34322 +AuStubInt0(vfsub_set_acl, struct mnt_idmap *idmap, struct dentry *dentry,
34323 +          const char *name, struct posix_acl *acl);
34324 +AuStubInt0(vfsub_remove_acl, struct mnt_idmap *idmap,
34325 +          struct dentry *dentry, const char *name);
34326 +#endif
34327 +
34328 +#endif /* __KERNEL__ */
34329 +#endif /* __AUFS_VFSUB_H__ */
34330 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34331 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34332 +++ linux/fs/aufs/wbr_policy.c  2024-05-13 17:13:00.621404284 +0200
34333 @@ -0,0 +1,830 @@
34334 +// SPDX-License-Identifier: GPL-2.0
34335 +/*
34336 + * Copyright (C) 2005-2022 Junjiro R. Okajima
34337 + *
34338 + * This program is free software; you can redistribute it and/or modify
34339 + * it under the terms of the GNU General Public License as published by
34340 + * the Free Software Foundation; either version 2 of the License, or
34341 + * (at your option) any later version.
34342 + *
34343 + * This program is distributed in the hope that it will be useful,
34344 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34345 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34346 + * GNU General Public License for more details.
34347 + *
34348 + * You should have received a copy of the GNU General Public License
34349 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34350 + */
34351 +
34352 +/*
34353 + * policies for selecting one among multiple writable branches
34354 + */
34355 +
34356 +#include <linux/statfs.h>
34357 +#include "aufs.h"
34358 +
34359 +/* subset of cpup_attr() */
34360 +static noinline_for_stack
34361 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34362 +{
34363 +       int err, sbits;
34364 +       struct iattr ia;
34365 +       struct inode *h_isrc;
34366 +
34367 +       h_isrc = d_inode(h_src);
34368 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34369 +       ia.ia_mode = h_isrc->i_mode;
34370 +       ia.ia_uid = h_isrc->i_uid;
34371 +       ia.ia_gid = h_isrc->i_gid;
34372 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34373 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34374 +       /* no delegation since it is just created */
34375 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34376 +
34377 +       /* is this nfs only? */
34378 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34379 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34380 +               ia.ia_mode = h_isrc->i_mode;
34381 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34382 +       }
34383 +
34384 +       return err;
34385 +}
34386 +
34387 +#define AuCpdown_PARENT_OPQ    BIT(0)
34388 +#define AuCpdown_WHED          BIT(1)
34389 +#define AuCpdown_MADE_DIR      BIT(2)
34390 +#define AuCpdown_DIROPQ                BIT(3)
34391 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34392 +#define au_fset_cpdown(flags, name) \
34393 +       do { (flags) |= AuCpdown_##name; } while (0)
34394 +#define au_fclr_cpdown(flags, name) \
34395 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34396 +
34397 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34398 +                            unsigned int *flags)
34399 +{
34400 +       int err;
34401 +       struct dentry *opq_dentry;
34402 +
34403 +       opq_dentry = au_diropq_create(dentry, bdst);
34404 +       err = PTR_ERR(opq_dentry);
34405 +       if (IS_ERR(opq_dentry))
34406 +               goto out;
34407 +       dput(opq_dentry);
34408 +       au_fset_cpdown(*flags, DIROPQ);
34409 +
34410 +out:
34411 +       return err;
34412 +}
34413 +
34414 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34415 +                           struct inode *dir, aufs_bindex_t bdst)
34416 +{
34417 +       int err;
34418 +       struct path h_path;
34419 +       struct au_branch *br;
34420 +
34421 +       br = au_sbr(dentry->d_sb, bdst);
34422 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34423 +       err = PTR_ERR(h_path.dentry);
34424 +       if (IS_ERR(h_path.dentry))
34425 +               goto out;
34426 +
34427 +       err = 0;
34428 +       if (d_is_positive(h_path.dentry)) {
34429 +               h_path.mnt = au_br_mnt(br);
34430 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34431 +                                         dentry);
34432 +       }
34433 +       dput(h_path.dentry);
34434 +
34435 +out:
34436 +       return err;
34437 +}
34438 +
34439 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34440 +                        struct au_pin *pin,
34441 +                        struct dentry *h_parent, void *arg)
34442 +{
34443 +       int err, rerr;
34444 +       aufs_bindex_t bopq, btop;
34445 +       struct path h_path;
34446 +       struct dentry *parent;
34447 +       struct inode *h_dir, *h_inode, *inode, *dir;
34448 +       unsigned int *flags = arg;
34449 +
34450 +       btop = au_dbtop(dentry);
34451 +       /* dentry is di-locked */
34452 +       parent = dget_parent(dentry);
34453 +       dir = d_inode(parent);
34454 +       h_dir = d_inode(h_parent);
34455 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34456 +       IMustLock(h_dir);
34457 +
34458 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34459 +       if (unlikely(err < 0))
34460 +               goto out;
34461 +       h_path.dentry = au_h_dptr(dentry, bdst);
34462 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34463 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34464 +       if (unlikely(err))
34465 +               goto out_put;
34466 +       au_fset_cpdown(*flags, MADE_DIR);
34467 +
34468 +       bopq = au_dbdiropq(dentry);
34469 +       au_fclr_cpdown(*flags, WHED);
34470 +       au_fclr_cpdown(*flags, DIROPQ);
34471 +       if (au_dbwh(dentry) == bdst)
34472 +               au_fset_cpdown(*flags, WHED);
34473 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34474 +               au_fset_cpdown(*flags, PARENT_OPQ);
34475 +       h_inode = d_inode(h_path.dentry);
34476 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34477 +       if (au_ftest_cpdown(*flags, WHED)) {
34478 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34479 +               if (unlikely(err)) {
34480 +                       inode_unlock(h_inode);
34481 +                       goto out_dir;
34482 +               }
34483 +       }
34484 +
34485 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34486 +       inode_unlock(h_inode);
34487 +       if (unlikely(err))
34488 +               goto out_opq;
34489 +
34490 +       if (au_ftest_cpdown(*flags, WHED)) {
34491 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34492 +               if (unlikely(err))
34493 +                       goto out_opq;
34494 +       }
34495 +
34496 +       inode = d_inode(dentry);
34497 +       if (au_ibbot(inode) < bdst)
34498 +               au_set_ibbot(inode, bdst);
34499 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34500 +                     au_hi_flags(inode, /*isdir*/1));
34501 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34502 +       goto out; /* success */
34503 +
34504 +       /* revert */
34505 +out_opq:
34506 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34507 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34508 +               rerr = au_diropq_remove(dentry, bdst);
34509 +               inode_unlock(h_inode);
34510 +               if (unlikely(rerr)) {
34511 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34512 +                               dentry, bdst, rerr);
34513 +                       err = -EIO;
34514 +                       goto out;
34515 +               }
34516 +       }
34517 +out_dir:
34518 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34519 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34520 +               if (unlikely(rerr)) {
34521 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34522 +                               dentry, bdst, rerr);
34523 +                       err = -EIO;
34524 +               }
34525 +       }
34526 +out_put:
34527 +       au_set_h_dptr(dentry, bdst, NULL);
34528 +       if (au_dbbot(dentry) == bdst)
34529 +               au_update_dbbot(dentry);
34530 +out:
34531 +       dput(parent);
34532 +       return err;
34533 +}
34534 +
34535 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34536 +{
34537 +       int err;
34538 +       unsigned int flags;
34539 +
34540 +       flags = 0;
34541 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34542 +
34543 +       return err;
34544 +}
34545 +
34546 +/* ---------------------------------------------------------------------- */
34547 +
34548 +/* policies for create */
34549 +
34550 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34551 +{
34552 +       int err, i, j, ndentry;
34553 +       aufs_bindex_t bopq;
34554 +       struct au_dcsub_pages dpages;
34555 +       struct au_dpage *dpage;
34556 +       struct dentry **dentries, *parent, *d;
34557 +
34558 +       err = au_dpages_init(&dpages, GFP_NOFS);
34559 +       if (unlikely(err))
34560 +               goto out;
34561 +       parent = dget_parent(dentry);
34562 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34563 +       if (unlikely(err))
34564 +               goto out_free;
34565 +
34566 +       err = bindex;
34567 +       for (i = 0; i < dpages.ndpage; i++) {
34568 +               dpage = dpages.dpages + i;
34569 +               dentries = dpage->dentries;
34570 +               ndentry = dpage->ndentry;
34571 +               for (j = 0; j < ndentry; j++) {
34572 +                       d = dentries[j];
34573 +                       di_read_lock_parent2(d, !AuLock_IR);
34574 +                       bopq = au_dbdiropq(d);
34575 +                       di_read_unlock(d, !AuLock_IR);
34576 +                       if (bopq >= 0 && bopq < err)
34577 +                               err = bopq;
34578 +               }
34579 +       }
34580 +
34581 +out_free:
34582 +       dput(parent);
34583 +       au_dpages_free(&dpages);
34584 +out:
34585 +       return err;
34586 +}
34587 +
34588 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34589 +{
34590 +       for (; bindex >= 0; bindex--)
34591 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34592 +                       return bindex;
34593 +       return -EROFS;
34594 +}
34595 +
34596 +/* top down parent */
34597 +static int au_wbr_create_tdp(struct dentry *dentry,
34598 +                            unsigned int flags __maybe_unused)
34599 +{
34600 +       int err;
34601 +       aufs_bindex_t btop, bindex;
34602 +       struct super_block *sb;
34603 +       struct dentry *parent, *h_parent;
34604 +
34605 +       sb = dentry->d_sb;
34606 +       btop = au_dbtop(dentry);
34607 +       err = btop;
34608 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34609 +               goto out;
34610 +
34611 +       err = -EROFS;
34612 +       parent = dget_parent(dentry);
34613 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34614 +               h_parent = au_h_dptr(parent, bindex);
34615 +               if (!h_parent || d_is_negative(h_parent))
34616 +                       continue;
34617 +
34618 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34619 +                       err = bindex;
34620 +                       break;
34621 +               }
34622 +       }
34623 +       dput(parent);
34624 +
34625 +       /* bottom up here */
34626 +       if (unlikely(err < 0)) {
34627 +               err = au_wbr_bu(sb, btop - 1);
34628 +               if (err >= 0)
34629 +                       err = au_wbr_nonopq(dentry, err);
34630 +       }
34631 +
34632 +out:
34633 +       AuDbg("b%d\n", err);
34634 +       return err;
34635 +}
34636 +
34637 +/* ---------------------------------------------------------------------- */
34638 +
34639 +/* an exception for the policy other than tdp */
34640 +static int au_wbr_create_exp(struct dentry *dentry)
34641 +{
34642 +       int err;
34643 +       aufs_bindex_t bwh, bdiropq;
34644 +       struct dentry *parent;
34645 +
34646 +       err = -1;
34647 +       bwh = au_dbwh(dentry);
34648 +       parent = dget_parent(dentry);
34649 +       bdiropq = au_dbdiropq(parent);
34650 +       if (bwh >= 0) {
34651 +               if (bdiropq >= 0)
34652 +                       err = min(bdiropq, bwh);
34653 +               else
34654 +                       err = bwh;
34655 +               AuDbg("%d\n", err);
34656 +       } else if (bdiropq >= 0) {
34657 +               err = bdiropq;
34658 +               AuDbg("%d\n", err);
34659 +       }
34660 +       dput(parent);
34661 +
34662 +       if (err >= 0)
34663 +               err = au_wbr_nonopq(dentry, err);
34664 +
34665 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34666 +               err = -1;
34667 +
34668 +       AuDbg("%d\n", err);
34669 +       return err;
34670 +}
34671 +
34672 +/* ---------------------------------------------------------------------- */
34673 +
34674 +/* round robin */
34675 +static int au_wbr_create_init_rr(struct super_block *sb)
34676 +{
34677 +       int err;
34678 +
34679 +       err = au_wbr_bu(sb, au_sbbot(sb));
34680 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34681 +       /* smp_mb(); */
34682 +
34683 +       AuDbg("b%d\n", err);
34684 +       return err;
34685 +}
34686 +
34687 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34688 +{
34689 +       int err, nbr;
34690 +       unsigned int u;
34691 +       aufs_bindex_t bindex, bbot;
34692 +       struct super_block *sb;
34693 +       atomic_t *next;
34694 +
34695 +       err = au_wbr_create_exp(dentry);
34696 +       if (err >= 0)
34697 +               goto out;
34698 +
34699 +       sb = dentry->d_sb;
34700 +       next = &au_sbi(sb)->si_wbr_rr_next;
34701 +       bbot = au_sbbot(sb);
34702 +       nbr = bbot + 1;
34703 +       for (bindex = 0; bindex <= bbot; bindex++) {
34704 +               if (!au_ftest_wbr(flags, DIR)) {
34705 +                       err = atomic_dec_return(next) + 1;
34706 +                       /* modulo for 0 is meaningless */
34707 +                       if (unlikely(!err))
34708 +                               err = atomic_dec_return(next) + 1;
34709 +               } else
34710 +                       err = atomic_read(next);
34711 +               AuDbg("%d\n", err);
34712 +               u = err;
34713 +               err = u % nbr;
34714 +               AuDbg("%d\n", err);
34715 +               if (!au_br_rdonly(au_sbr(sb, err)))
34716 +                       break;
34717 +               err = -EROFS;
34718 +       }
34719 +
34720 +       if (err >= 0)
34721 +               err = au_wbr_nonopq(dentry, err);
34722 +
34723 +out:
34724 +       AuDbg("%d\n", err);
34725 +       return err;
34726 +}
34727 +
34728 +/* ---------------------------------------------------------------------- */
34729 +
34730 +/* most free space */
34731 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34732 +{
34733 +       struct super_block *sb;
34734 +       struct au_branch *br;
34735 +       struct au_wbr_mfs *mfs;
34736 +       struct dentry *h_parent;
34737 +       aufs_bindex_t bindex, bbot;
34738 +       int err;
34739 +       unsigned long long b, bavail;
34740 +       struct path h_path;
34741 +       /* reduce the stack usage */
34742 +       struct kstatfs *st;
34743 +
34744 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34745 +       if (unlikely(!st)) {
34746 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34747 +               return;
34748 +       }
34749 +
34750 +       bavail = 0;
34751 +       sb = dentry->d_sb;
34752 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34753 +       MtxMustLock(&mfs->mfs_lock);
34754 +       mfs->mfs_bindex = -EROFS;
34755 +       mfs->mfsrr_bytes = 0;
34756 +       if (!parent) {
34757 +               bindex = 0;
34758 +               bbot = au_sbbot(sb);
34759 +       } else {
34760 +               bindex = au_dbtop(parent);
34761 +               bbot = au_dbtaildir(parent);
34762 +       }
34763 +
34764 +       for (; bindex <= bbot; bindex++) {
34765 +               if (parent) {
34766 +                       h_parent = au_h_dptr(parent, bindex);
34767 +                       if (!h_parent || d_is_negative(h_parent))
34768 +                               continue;
34769 +               }
34770 +               br = au_sbr(sb, bindex);
34771 +               if (au_br_rdonly(br))
34772 +                       continue;
34773 +
34774 +               /* sb->s_root for NFS is unreliable */
34775 +               h_path.mnt = au_br_mnt(br);
34776 +               h_path.dentry = h_path.mnt->mnt_root;
34777 +               err = vfs_statfs(&h_path, st);
34778 +               if (unlikely(err)) {
34779 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34780 +                       continue;
34781 +               }
34782 +
34783 +               /* when the available size is equal, select the lower one */
34784 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34785 +                            || sizeof(b) < sizeof(st->f_bsize));
34786 +               b = st->f_bavail * st->f_bsize;
34787 +               br->br_wbr->wbr_bytes = b;
34788 +               if (b >= bavail) {
34789 +                       bavail = b;
34790 +                       mfs->mfs_bindex = bindex;
34791 +                       mfs->mfs_jiffy = jiffies;
34792 +               }
34793 +       }
34794 +
34795 +       mfs->mfsrr_bytes = bavail;
34796 +       AuDbg("b%d\n", mfs->mfs_bindex);
34797 +       au_kfree_rcu(st);
34798 +}
34799 +
34800 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34801 +{
34802 +       int err;
34803 +       struct dentry *parent;
34804 +       struct super_block *sb;
34805 +       struct au_wbr_mfs *mfs;
34806 +
34807 +       err = au_wbr_create_exp(dentry);
34808 +       if (err >= 0)
34809 +               goto out;
34810 +
34811 +       sb = dentry->d_sb;
34812 +       parent = NULL;
34813 +       if (au_ftest_wbr(flags, PARENT))
34814 +               parent = dget_parent(dentry);
34815 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34816 +       mutex_lock(&mfs->mfs_lock);
34817 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34818 +           || mfs->mfs_bindex < 0
34819 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34820 +               au_mfs(dentry, parent);
34821 +       mutex_unlock(&mfs->mfs_lock);
34822 +       err = mfs->mfs_bindex;
34823 +       dput(parent);
34824 +
34825 +       if (err >= 0)
34826 +               err = au_wbr_nonopq(dentry, err);
34827 +
34828 +out:
34829 +       AuDbg("b%d\n", err);
34830 +       return err;
34831 +}
34832 +
34833 +static int au_wbr_create_init_mfs(struct super_block *sb)
34834 +{
34835 +       struct au_wbr_mfs *mfs;
34836 +
34837 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34838 +       mutex_init(&mfs->mfs_lock);
34839 +       mfs->mfs_jiffy = 0;
34840 +       mfs->mfs_bindex = -EROFS;
34841 +
34842 +       return 0;
34843 +}
34844 +
34845 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34846 +{
34847 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34848 +       return 0;
34849 +}
34850 +
34851 +/* ---------------------------------------------------------------------- */
34852 +
34853 +/* top down regardless parent, and then mfs */
34854 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34855 +                              unsigned int flags __maybe_unused)
34856 +{
34857 +       int err;
34858 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34859 +       unsigned long long watermark;
34860 +       struct super_block *sb;
34861 +       struct au_wbr_mfs *mfs;
34862 +       struct au_branch *br;
34863 +       struct dentry *parent;
34864 +
34865 +       sb = dentry->d_sb;
34866 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34867 +       mutex_lock(&mfs->mfs_lock);
34868 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34869 +           || mfs->mfs_bindex < 0)
34870 +               au_mfs(dentry, /*parent*/NULL);
34871 +       watermark = mfs->mfsrr_watermark;
34872 +       bmfs = mfs->mfs_bindex;
34873 +       mutex_unlock(&mfs->mfs_lock);
34874 +
34875 +       /* another style of au_wbr_create_exp() */
34876 +       bwh = au_dbwh(dentry);
34877 +       parent = dget_parent(dentry);
34878 +       btail = au_dbtaildir(parent);
34879 +       if (bwh >= 0 && bwh < btail)
34880 +               btail = bwh;
34881 +
34882 +       err = au_wbr_nonopq(dentry, btail);
34883 +       if (unlikely(err < 0))
34884 +               goto out;
34885 +       btail = err;
34886 +       bfound = -1;
34887 +       for (bindex = 0; bindex <= btail; bindex++) {
34888 +               br = au_sbr(sb, bindex);
34889 +               if (au_br_rdonly(br))
34890 +                       continue;
34891 +               if (br->br_wbr->wbr_bytes > watermark) {
34892 +                       bfound = bindex;
34893 +                       break;
34894 +               }
34895 +       }
34896 +       err = bfound;
34897 +       if (err < 0)
34898 +               err = bmfs;
34899 +
34900 +out:
34901 +       dput(parent);
34902 +       AuDbg("b%d\n", err);
34903 +       return err;
34904 +}
34905 +
34906 +/* ---------------------------------------------------------------------- */
34907 +
34908 +/* most free space and then round robin */
34909 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34910 +{
34911 +       int err;
34912 +       struct au_wbr_mfs *mfs;
34913 +
34914 +       err = au_wbr_create_mfs(dentry, flags);
34915 +       if (err >= 0) {
34916 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34917 +               mutex_lock(&mfs->mfs_lock);
34918 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34919 +                       err = au_wbr_create_rr(dentry, flags);
34920 +               mutex_unlock(&mfs->mfs_lock);
34921 +       }
34922 +
34923 +       AuDbg("b%d\n", err);
34924 +       return err;
34925 +}
34926 +
34927 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34928 +{
34929 +       int err;
34930 +
34931 +       au_wbr_create_init_mfs(sb); /* ignore */
34932 +       err = au_wbr_create_init_rr(sb);
34933 +
34934 +       return err;
34935 +}
34936 +
34937 +/* ---------------------------------------------------------------------- */
34938 +
34939 +/* top down parent and most free space */
34940 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34941 +{
34942 +       int err, e2;
34943 +       unsigned long long b;
34944 +       aufs_bindex_t bindex, btop, bbot;
34945 +       struct super_block *sb;
34946 +       struct dentry *parent, *h_parent;
34947 +       struct au_branch *br;
34948 +
34949 +       err = au_wbr_create_tdp(dentry, flags);
34950 +       if (unlikely(err < 0))
34951 +               goto out;
34952 +       parent = dget_parent(dentry);
34953 +       btop = au_dbtop(parent);
34954 +       bbot = au_dbtaildir(parent);
34955 +       if (btop == bbot)
34956 +               goto out_parent; /* success */
34957 +
34958 +       e2 = au_wbr_create_mfs(dentry, flags);
34959 +       if (e2 < 0)
34960 +               goto out_parent; /* success */
34961 +
34962 +       /* when the available size is equal, select upper one */
34963 +       sb = dentry->d_sb;
34964 +       br = au_sbr(sb, err);
34965 +       b = br->br_wbr->wbr_bytes;
34966 +       AuDbg("b%d, %llu\n", err, b);
34967 +
34968 +       for (bindex = btop; bindex <= bbot; bindex++) {
34969 +               h_parent = au_h_dptr(parent, bindex);
34970 +               if (!h_parent || d_is_negative(h_parent))
34971 +                       continue;
34972 +
34973 +               br = au_sbr(sb, bindex);
34974 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34975 +                       b = br->br_wbr->wbr_bytes;
34976 +                       err = bindex;
34977 +                       AuDbg("b%d, %llu\n", err, b);
34978 +               }
34979 +       }
34980 +
34981 +       if (err >= 0)
34982 +               err = au_wbr_nonopq(dentry, err);
34983 +
34984 +out_parent:
34985 +       dput(parent);
34986 +out:
34987 +       AuDbg("b%d\n", err);
34988 +       return err;
34989 +}
34990 +
34991 +/* ---------------------------------------------------------------------- */
34992 +
34993 +/*
34994 + * - top down parent
34995 + * - most free space with parent
34996 + * - most free space round-robin regardless parent
34997 + */
34998 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34999 +{
35000 +       int err;
35001 +       unsigned long long watermark;
35002 +       struct super_block *sb;
35003 +       struct au_branch *br;
35004 +       struct au_wbr_mfs *mfs;
35005 +
35006 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
35007 +       if (unlikely(err < 0))
35008 +               goto out;
35009 +
35010 +       sb = dentry->d_sb;
35011 +       br = au_sbr(sb, err);
35012 +       mfs = &au_sbi(sb)->si_wbr_mfs;
35013 +       mutex_lock(&mfs->mfs_lock);
35014 +       watermark = mfs->mfsrr_watermark;
35015 +       mutex_unlock(&mfs->mfs_lock);
35016 +       if (br->br_wbr->wbr_bytes < watermark)
35017 +               /* regardless the parent dir */
35018 +               err = au_wbr_create_mfsrr(dentry, flags);
35019 +
35020 +out:
35021 +       AuDbg("b%d\n", err);
35022 +       return err;
35023 +}
35024 +
35025 +/* ---------------------------------------------------------------------- */
35026 +
35027 +/* policies for copyup */
35028 +
35029 +/* top down parent */
35030 +static int au_wbr_copyup_tdp(struct dentry *dentry)
35031 +{
35032 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
35033 +}
35034 +
35035 +/* bottom up parent */
35036 +static int au_wbr_copyup_bup(struct dentry *dentry)
35037 +{
35038 +       int err;
35039 +       aufs_bindex_t bindex, btop;
35040 +       struct dentry *parent, *h_parent;
35041 +       struct super_block *sb;
35042 +
35043 +       err = -EROFS;
35044 +       sb = dentry->d_sb;
35045 +       parent = dget_parent(dentry);
35046 +       btop = au_dbtop(parent);
35047 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
35048 +               h_parent = au_h_dptr(parent, bindex);
35049 +               if (!h_parent || d_is_negative(h_parent))
35050 +                       continue;
35051 +
35052 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
35053 +                       err = bindex;
35054 +                       break;
35055 +               }
35056 +       }
35057 +       dput(parent);
35058 +
35059 +       /* bottom up here */
35060 +       if (unlikely(err < 0))
35061 +               err = au_wbr_bu(sb, btop - 1);
35062 +
35063 +       AuDbg("b%d\n", err);
35064 +       return err;
35065 +}
35066 +
35067 +/* bottom up */
35068 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
35069 +{
35070 +       int err;
35071 +
35072 +       err = au_wbr_bu(dentry->d_sb, btop);
35073 +       AuDbg("b%d\n", err);
35074 +       if (err > btop)
35075 +               err = au_wbr_nonopq(dentry, err);
35076 +
35077 +       AuDbg("b%d\n", err);
35078 +       return err;
35079 +}
35080 +
35081 +static int au_wbr_copyup_bu(struct dentry *dentry)
35082 +{
35083 +       int err;
35084 +       aufs_bindex_t btop;
35085 +
35086 +       btop = au_dbtop(dentry);
35087 +       err = au_wbr_do_copyup_bu(dentry, btop);
35088 +       return err;
35089 +}
35090 +
35091 +/* ---------------------------------------------------------------------- */
35092 +
35093 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
35094 +       [AuWbrCopyup_TDP] = {
35095 +               .copyup = au_wbr_copyup_tdp
35096 +       },
35097 +       [AuWbrCopyup_BUP] = {
35098 +               .copyup = au_wbr_copyup_bup
35099 +       },
35100 +       [AuWbrCopyup_BU] = {
35101 +               .copyup = au_wbr_copyup_bu
35102 +       }
35103 +};
35104 +
35105 +struct au_wbr_create_operations au_wbr_create_ops[] = {
35106 +       [AuWbrCreate_TDP] = {
35107 +               .create = au_wbr_create_tdp
35108 +       },
35109 +       [AuWbrCreate_RR] = {
35110 +               .create = au_wbr_create_rr,
35111 +               .init   = au_wbr_create_init_rr
35112 +       },
35113 +       [AuWbrCreate_MFS] = {
35114 +               .create = au_wbr_create_mfs,
35115 +               .init   = au_wbr_create_init_mfs,
35116 +               .fin    = au_wbr_create_fin_mfs
35117 +       },
35118 +       [AuWbrCreate_MFSV] = {
35119 +               .create = au_wbr_create_mfs,
35120 +               .init   = au_wbr_create_init_mfs,
35121 +               .fin    = au_wbr_create_fin_mfs
35122 +       },
35123 +       [AuWbrCreate_MFSRR] = {
35124 +               .create = au_wbr_create_mfsrr,
35125 +               .init   = au_wbr_create_init_mfsrr,
35126 +               .fin    = au_wbr_create_fin_mfs
35127 +       },
35128 +       [AuWbrCreate_MFSRRV] = {
35129 +               .create = au_wbr_create_mfsrr,
35130 +               .init   = au_wbr_create_init_mfsrr,
35131 +               .fin    = au_wbr_create_fin_mfs
35132 +       },
35133 +       [AuWbrCreate_TDMFS] = {
35134 +               .create = au_wbr_create_tdmfs,
35135 +               .init   = au_wbr_create_init_mfs,
35136 +               .fin    = au_wbr_create_fin_mfs
35137 +       },
35138 +       [AuWbrCreate_TDMFSV] = {
35139 +               .create = au_wbr_create_tdmfs,
35140 +               .init   = au_wbr_create_init_mfs,
35141 +               .fin    = au_wbr_create_fin_mfs
35142 +       },
35143 +       [AuWbrCreate_PMFS] = {
35144 +               .create = au_wbr_create_pmfs,
35145 +               .init   = au_wbr_create_init_mfs,
35146 +               .fin    = au_wbr_create_fin_mfs
35147 +       },
35148 +       [AuWbrCreate_PMFSV] = {
35149 +               .create = au_wbr_create_pmfs,
35150 +               .init   = au_wbr_create_init_mfs,
35151 +               .fin    = au_wbr_create_fin_mfs
35152 +       },
35153 +       [AuWbrCreate_PMFSRR] = {
35154 +               .create = au_wbr_create_pmfsrr,
35155 +               .init   = au_wbr_create_init_mfsrr,
35156 +               .fin    = au_wbr_create_fin_mfs
35157 +       },
35158 +       [AuWbrCreate_PMFSRRV] = {
35159 +               .create = au_wbr_create_pmfsrr,
35160 +               .init   = au_wbr_create_init_mfsrr,
35161 +               .fin    = au_wbr_create_fin_mfs
35162 +       }
35163 +};
35164 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
35165 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
35166 +++ linux/fs/aufs/whout.c       2024-05-13 17:13:00.621404284 +0200
35167 @@ -0,0 +1,1072 @@
35168 +// SPDX-License-Identifier: GPL-2.0
35169 +/*
35170 + * Copyright (C) 2005-2022 Junjiro R. Okajima
35171 + *
35172 + * This program is free software; you can redistribute it and/or modify
35173 + * it under the terms of the GNU General Public License as published by
35174 + * the Free Software Foundation; either version 2 of the License, or
35175 + * (at your option) any later version.
35176 + *
35177 + * This program is distributed in the hope that it will be useful,
35178 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35179 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35180 + * GNU General Public License for more details.
35181 + *
35182 + * You should have received a copy of the GNU General Public License
35183 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35184 + */
35185 +
35186 +/*
35187 + * whiteout for logical deletion and opaque directory
35188 + */
35189 +
35190 +#include "aufs.h"
35191 +
35192 +#define WH_MASK                        0444
35193 +
35194 +/*
35195 + * If a directory contains this file, then it is opaque.  We start with the
35196 + * .wh. flag so that it is blocked by lookup.
35197 + */
35198 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35199 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
35200 +
35201 +/*
35202 + * generate whiteout name, which is NOT terminated by NULL.
35203 + * @name: original d_name.name
35204 + * @len: original d_name.len
35205 + * @wh: whiteout qstr
35206 + * returns zero when succeeds, otherwise error.
35207 + * succeeded value as wh->name should be freed by kfree().
35208 + */
35209 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35210 +{
35211 +       char *p;
35212 +
35213 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35214 +               return -ENAMETOOLONG;
35215 +
35216 +       wh->len = name->len + AUFS_WH_PFX_LEN;
35217 +       p = kmalloc(wh->len, GFP_NOFS);
35218 +       wh->name = p;
35219 +       if (p) {
35220 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35221 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35222 +               /* smp_mb(); */
35223 +               return 0;
35224 +       }
35225 +       return -ENOMEM;
35226 +}
35227 +
35228 +/* ---------------------------------------------------------------------- */
35229 +
35230 +/*
35231 + * test if the @wh_name exists under @h_ppath.
35232 + * @try_sio specifies the necessary of super-io.
35233 + */
35234 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
35235 +              struct qstr *wh_name, int try_sio)
35236 +{
35237 +       int err;
35238 +       struct dentry *wh_dentry;
35239 +
35240 +       if (!try_sio)
35241 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
35242 +       else
35243 +               wh_dentry = au_sio_lkup_one(h_idmap, wh_name, h_ppath);
35244 +       err = PTR_ERR(wh_dentry);
35245 +       if (IS_ERR(wh_dentry)) {
35246 +               if (err == -ENAMETOOLONG)
35247 +                       err = 0;
35248 +               goto out;
35249 +       }
35250 +
35251 +       err = 0;
35252 +       if (d_is_negative(wh_dentry))
35253 +               goto out_wh; /* success */
35254 +
35255 +       err = 1;
35256 +       if (d_is_reg(wh_dentry))
35257 +               goto out_wh; /* success */
35258 +
35259 +       err = -EIO;
35260 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35261 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35262 +
35263 +out_wh:
35264 +       dput(wh_dentry);
35265 +out:
35266 +       return err;
35267 +}
35268 +
35269 +/*
35270 + * test if the @h_path->dentry sets opaque or not.
35271 + */
35272 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path)
35273 +{
35274 +       int err;
35275 +       struct inode *h_dir;
35276 +
35277 +       h_dir = d_inode(h_path->dentry);
35278 +       err = au_wh_test(h_idmap, h_path, &diropq_name,
35279 +                        au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC));
35280 +       return err;
35281 +}
35282 +
35283 +/*
35284 + * returns a negative dentry whose name is unique and temporary.
35285 + */
35286 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35287 +                            struct qstr *prefix)
35288 +{
35289 +       struct dentry *dentry;
35290 +       int i;
35291 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35292 +               *name, *p;
35293 +       /* strict atomic_t is unnecessary here */
35294 +       static unsigned short cnt;
35295 +       struct qstr qs;
35296 +       struct path h_ppath;
35297 +       struct mnt_idmap *h_idmap;
35298 +
35299 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35300 +
35301 +       name = defname;
35302 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35303 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35304 +               dentry = ERR_PTR(-ENAMETOOLONG);
35305 +               if (unlikely(qs.len > NAME_MAX))
35306 +                       goto out;
35307 +               dentry = ERR_PTR(-ENOMEM);
35308 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35309 +               if (unlikely(!name))
35310 +                       goto out;
35311 +       }
35312 +
35313 +       /* doubly whiteout-ed */
35314 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35315 +       p = name + AUFS_WH_PFX_LEN * 2;
35316 +       memcpy(p, prefix->name, prefix->len);
35317 +       p += prefix->len;
35318 +       *p++ = '.';
35319 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35320 +
35321 +       h_ppath.dentry = h_parent;
35322 +       h_ppath.mnt = au_br_mnt(br);
35323 +       h_idmap = au_br_idmap(br);
35324 +       qs.name = name;
35325 +       for (i = 0; i < 3; i++) {
35326 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35327 +               dentry = au_sio_lkup_one(h_idmap, &qs, &h_ppath);
35328 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35329 +                       goto out_name;
35330 +               dput(dentry);
35331 +       }
35332 +       /* pr_warn("could not get random name\n"); */
35333 +       dentry = ERR_PTR(-EEXIST);
35334 +       AuDbg("%.*s\n", AuLNPair(&qs));
35335 +       BUG();
35336 +
35337 +out_name:
35338 +       if (name != defname)
35339 +               au_kfree_try_rcu(name);
35340 +out:
35341 +       AuTraceErrPtr(dentry);
35342 +       return dentry;
35343 +}
35344 +
35345 +/*
35346 + * rename the @h_dentry on @br to the whiteouted temporary name.
35347 + */
35348 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35349 +{
35350 +       int err;
35351 +       struct path h_path = {
35352 +               .mnt = au_br_mnt(br)
35353 +       };
35354 +       struct inode *h_dir, *delegated;
35355 +       struct dentry *h_parent;
35356 +
35357 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35358 +       h_dir = d_inode(h_parent);
35359 +       IMustLock(h_dir);
35360 +
35361 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35362 +       err = PTR_ERR(h_path.dentry);
35363 +       if (IS_ERR(h_path.dentry))
35364 +               goto out;
35365 +
35366 +       /* under the same dir, no need to lock_rename() */
35367 +       delegated = NULL;
35368 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35369 +                          /*flags*/0);
35370 +       AuTraceErr(err);
35371 +       if (unlikely(err == -EWOULDBLOCK)) {
35372 +               pr_warn("cannot retry for NFSv4 delegation"
35373 +                       " for an internal rename\n");
35374 +               iput(delegated);
35375 +       }
35376 +       dput(h_path.dentry);
35377 +
35378 +out:
35379 +       AuTraceErr(err);
35380 +       return err;
35381 +}
35382 +
35383 +/* ---------------------------------------------------------------------- */
35384 +/*
35385 + * functions for removing a whiteout
35386 + */
35387 +
35388 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35389 +{
35390 +       int err, force;
35391 +       struct inode *delegated;
35392 +
35393 +       /*
35394 +        * forces superio when the dir has a sticky bit.
35395 +        * this may be a violation of unix fs semantics.
35396 +        */
35397 +       force = (h_dir->i_mode & S_ISVTX)
35398 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35399 +       delegated = NULL;
35400 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35401 +       if (unlikely(err == -EWOULDBLOCK)) {
35402 +               pr_warn("cannot retry for NFSv4 delegation"
35403 +                       " for an internal unlink\n");
35404 +               iput(delegated);
35405 +       }
35406 +       return err;
35407 +}
35408 +
35409 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35410 +                       struct dentry *dentry)
35411 +{
35412 +       int err;
35413 +
35414 +       err = do_unlink_wh(h_dir, h_path);
35415 +       if (!err && dentry)
35416 +               au_set_dbwh(dentry, -1);
35417 +
35418 +       return err;
35419 +}
35420 +
35421 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35422 +{
35423 +       int err;
35424 +       struct path h_path;
35425 +
35426 +       err = 0;
35427 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35428 +       if (IS_ERR(h_path.dentry))
35429 +               err = PTR_ERR(h_path.dentry);
35430 +       else {
35431 +               if (d_is_reg(h_path.dentry)) {
35432 +                       h_path.mnt = h_ppath->mnt;
35433 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35434 +               }
35435 +               dput(h_path.dentry);
35436 +       }
35437 +
35438 +       return err;
35439 +}
35440 +
35441 +/* ---------------------------------------------------------------------- */
35442 +/*
35443 + * initialize/clean whiteout for a branch
35444 + */
35445 +
35446 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35447 +                       const int isdir)
35448 +{
35449 +       int err;
35450 +       struct inode *delegated;
35451 +
35452 +       if (d_is_negative(whpath->dentry))
35453 +               return;
35454 +
35455 +       if (isdir)
35456 +               err = vfsub_rmdir(h_dir, whpath);
35457 +       else {
35458 +               delegated = NULL;
35459 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35460 +               if (unlikely(err == -EWOULDBLOCK)) {
35461 +                       pr_warn("cannot retry for NFSv4 delegation"
35462 +                               " for an internal unlink\n");
35463 +                       iput(delegated);
35464 +               }
35465 +       }
35466 +       if (unlikely(err))
35467 +               pr_warn("failed removing %pd (%d), ignored.\n",
35468 +                       whpath->dentry, err);
35469 +}
35470 +
35471 +static int test_linkable(struct dentry *h_root)
35472 +{
35473 +       struct inode *h_dir = d_inode(h_root);
35474 +
35475 +       if (h_dir->i_op->link)
35476 +               return 0;
35477 +
35478 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35479 +              h_root, au_sbtype(h_root->d_sb));
35480 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35481 +}
35482 +
35483 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35484 +static int au_whdir(struct inode *h_dir, struct path *path)
35485 +{
35486 +       int err;
35487 +
35488 +       err = -EEXIST;
35489 +       if (d_is_negative(path->dentry)) {
35490 +               int mode = 0700;
35491 +
35492 +               if (au_test_nfs(path->dentry->d_sb))
35493 +                       mode |= 0111;
35494 +               err = vfsub_mkdir(h_dir, path, mode);
35495 +       } else if (d_is_dir(path->dentry))
35496 +               err = 0;
35497 +       else
35498 +               pr_err("unknown %pd exists\n", path->dentry);
35499 +
35500 +       return err;
35501 +}
35502 +
35503 +struct au_wh_base {
35504 +       const struct qstr *name;
35505 +       struct dentry *dentry;
35506 +};
35507 +
35508 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35509 +                         struct path *h_path)
35510 +{
35511 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35512 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35513 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35514 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35515 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35516 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35517 +}
35518 +
35519 +/*
35520 + * returns tri-state,
35521 + * minus: error, caller should print the message
35522 + * zero: success
35523 + * plus: error, caller should NOT print the message
35524 + */
35525 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35526 +                               int do_plink, struct au_wh_base base[],
35527 +                               struct path *h_path)
35528 +{
35529 +       int err;
35530 +       struct inode *h_dir;
35531 +
35532 +       h_dir = d_inode(h_root);
35533 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35534 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35535 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35536 +       if (do_plink) {
35537 +               err = test_linkable(h_root);
35538 +               if (unlikely(err)) {
35539 +                       err = 1;
35540 +                       goto out;
35541 +               }
35542 +
35543 +               err = au_whdir(h_dir, h_path);
35544 +               if (unlikely(err))
35545 +                       goto out;
35546 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35547 +       } else
35548 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35549 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35550 +       err = au_whdir(h_dir, h_path);
35551 +       if (unlikely(err))
35552 +               goto out;
35553 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35554 +
35555 +out:
35556 +       return err;
35557 +}
35558 +
35559 +/*
35560 + * for the moment, aufs supports the branch filesystem which does not support
35561 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35562 + * copyup failed. finally, such filesystem will not be used as the writable
35563 + * branch.
35564 + *
35565 + * returns tri-state, see above.
35566 + */
35567 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35568 +                        int do_plink, struct au_wh_base base[],
35569 +                        struct path *h_path)
35570 +{
35571 +       int err;
35572 +       struct inode *h_dir;
35573 +
35574 +       WbrWhMustWriteLock(wbr);
35575 +
35576 +       err = test_linkable(h_root);
35577 +       if (unlikely(err)) {
35578 +               err = 1;
35579 +               goto out;
35580 +       }
35581 +
35582 +       /*
35583 +        * todo: should this create be done in /sbin/mount.aufs helper?
35584 +        */
35585 +       err = -EEXIST;
35586 +       h_dir = d_inode(h_root);
35587 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35588 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35589 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35590 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35591 +               err = 0;
35592 +       else
35593 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35594 +       if (unlikely(err))
35595 +               goto out;
35596 +
35597 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35598 +       if (do_plink) {
35599 +               err = au_whdir(h_dir, h_path);
35600 +               if (unlikely(err))
35601 +                       goto out;
35602 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35603 +       } else
35604 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35605 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35606 +
35607 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35608 +       err = au_whdir(h_dir, h_path);
35609 +       if (unlikely(err))
35610 +               goto out;
35611 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35612 +
35613 +out:
35614 +       return err;
35615 +}
35616 +
35617 +/*
35618 + * initialize the whiteout base file/dir for @br.
35619 + */
35620 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35621 +{
35622 +       int err, i;
35623 +       const unsigned char do_plink
35624 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35625 +       struct inode *h_dir;
35626 +       struct path path = br->br_path;
35627 +       struct dentry *h_root = path.dentry;
35628 +       struct au_wbr *wbr = br->br_wbr;
35629 +       static const struct qstr base_name[] = {
35630 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35631 +                                         sizeof(AUFS_BASE_NAME) - 1),
35632 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35633 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35634 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35635 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35636 +       };
35637 +       struct au_wh_base base[] = {
35638 +               [AuBrWh_BASE] = {
35639 +                       .name   = base_name + AuBrWh_BASE,
35640 +                       .dentry = NULL
35641 +               },
35642 +               [AuBrWh_PLINK] = {
35643 +                       .name   = base_name + AuBrWh_PLINK,
35644 +                       .dentry = NULL
35645 +               },
35646 +               [AuBrWh_ORPH] = {
35647 +                       .name   = base_name + AuBrWh_ORPH,
35648 +                       .dentry = NULL
35649 +               }
35650 +       };
35651 +
35652 +       if (wbr)
35653 +               WbrWhMustWriteLock(wbr);
35654 +
35655 +       for (i = 0; i < AuBrWh_Last; i++) {
35656 +               /* doubly whiteouted */
35657 +               struct dentry *d;
35658 +
35659 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35660 +               err = PTR_ERR(d);
35661 +               if (IS_ERR(d))
35662 +                       goto out;
35663 +
35664 +               base[i].dentry = d;
35665 +               AuDebugOn(wbr
35666 +                         && wbr->wbr_wh[i]
35667 +                         && wbr->wbr_wh[i] != base[i].dentry);
35668 +       }
35669 +
35670 +       if (wbr)
35671 +               for (i = 0; i < AuBrWh_Last; i++) {
35672 +                       dput(wbr->wbr_wh[i]);
35673 +                       wbr->wbr_wh[i] = NULL;
35674 +               }
35675 +
35676 +       err = 0;
35677 +       if (!au_br_writable(br->br_perm)) {
35678 +               h_dir = d_inode(h_root);
35679 +               au_wh_init_ro(h_dir, base, &path);
35680 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35681 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35682 +               if (err > 0)
35683 +                       goto out;
35684 +               else if (err)
35685 +                       goto out_err;
35686 +       } else {
35687 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35688 +               if (err > 0)
35689 +                       goto out;
35690 +               else if (err)
35691 +                       goto out_err;
35692 +       }
35693 +       goto out; /* success */
35694 +
35695 +out_err:
35696 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35697 +              err, h_root, au_sbtype(h_root->d_sb));
35698 +out:
35699 +       for (i = 0; i < AuBrWh_Last; i++)
35700 +               dput(base[i].dentry);
35701 +       return err;
35702 +}
35703 +
35704 +/* ---------------------------------------------------------------------- */
35705 +/*
35706 + * whiteouts are all hard-linked usually.
35707 + * when its link count reaches a ceiling, we create a new whiteout base
35708 + * asynchronously.
35709 + */
35710 +
35711 +struct reinit_br_wh {
35712 +       struct super_block *sb;
35713 +       struct au_branch *br;
35714 +};
35715 +
35716 +static void reinit_br_wh(void *arg)
35717 +{
35718 +       int err;
35719 +       aufs_bindex_t bindex;
35720 +       struct path h_path;
35721 +       struct reinit_br_wh *a = arg;
35722 +       struct au_wbr *wbr;
35723 +       struct inode *dir, *delegated;
35724 +       struct dentry *h_root;
35725 +       struct au_hinode *hdir;
35726 +
35727 +       err = 0;
35728 +       wbr = a->br->br_wbr;
35729 +       /* big aufs lock */
35730 +       si_noflush_write_lock(a->sb);
35731 +       if (!au_br_writable(a->br->br_perm))
35732 +               goto out;
35733 +       bindex = au_br_index(a->sb, a->br->br_id);
35734 +       if (unlikely(bindex < 0))
35735 +               goto out;
35736 +
35737 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35738 +       dir = d_inode(a->sb->s_root);
35739 +       hdir = au_hi(dir, bindex);
35740 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35741 +       AuDebugOn(h_root != au_br_dentry(a->br));
35742 +
35743 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35744 +       wbr_wh_write_lock(wbr);
35745 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35746 +                         h_root, a->br);
35747 +       if (!err) {
35748 +               h_path.dentry = wbr->wbr_whbase;
35749 +               h_path.mnt = au_br_mnt(a->br);
35750 +               delegated = NULL;
35751 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35752 +                                  /*force*/0);
35753 +               if (unlikely(err == -EWOULDBLOCK)) {
35754 +                       pr_warn("cannot retry for NFSv4 delegation"
35755 +                               " for an internal unlink\n");
35756 +                       iput(delegated);
35757 +               }
35758 +       } else {
35759 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35760 +               err = 0;
35761 +       }
35762 +       dput(wbr->wbr_whbase);
35763 +       wbr->wbr_whbase = NULL;
35764 +       if (!err)
35765 +               err = au_wh_init(a->br, a->sb);
35766 +       wbr_wh_write_unlock(wbr);
35767 +       au_hn_inode_unlock(hdir);
35768 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35769 +       if (!err)
35770 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35771 +
35772 +out:
35773 +       if (wbr)
35774 +               atomic_dec(&wbr->wbr_wh_running);
35775 +       au_lcnt_dec(&a->br->br_count);
35776 +       si_write_unlock(a->sb);
35777 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35778 +       au_kfree_rcu(a);
35779 +       if (unlikely(err))
35780 +               AuIOErr("err %d\n", err);
35781 +}
35782 +
35783 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35784 +{
35785 +       int do_dec, wkq_err;
35786 +       struct reinit_br_wh *arg;
35787 +
35788 +       do_dec = 1;
35789 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35790 +               goto out;
35791 +
35792 +       /* ignore ENOMEM */
35793 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35794 +       if (arg) {
35795 +               /*
35796 +                * dec(wh_running), kfree(arg) and dec(br_count)
35797 +                * in reinit function
35798 +                */
35799 +               arg->sb = sb;
35800 +               arg->br = br;
35801 +               au_lcnt_inc(&br->br_count);
35802 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35803 +               if (unlikely(wkq_err)) {
35804 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35805 +                       au_lcnt_dec(&br->br_count);
35806 +                       au_kfree_rcu(arg);
35807 +               }
35808 +               do_dec = 0;
35809 +       }
35810 +
35811 +out:
35812 +       if (do_dec)
35813 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35814 +}
35815 +
35816 +/* ---------------------------------------------------------------------- */
35817 +
35818 +/*
35819 + * create the whiteout @wh.
35820 + */
35821 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35822 +                            struct dentry *wh)
35823 +{
35824 +       int err;
35825 +       struct path h_path = {
35826 +               .dentry = wh
35827 +       };
35828 +       struct au_branch *br;
35829 +       struct au_wbr *wbr;
35830 +       struct dentry *h_parent;
35831 +       struct inode *h_dir, *delegated;
35832 +
35833 +       h_parent = wh->d_parent; /* dir inode is locked */
35834 +       h_dir = d_inode(h_parent);
35835 +       IMustLock(h_dir);
35836 +
35837 +       br = au_sbr(sb, bindex);
35838 +       h_path.mnt = au_br_mnt(br);
35839 +       wbr = br->br_wbr;
35840 +       wbr_wh_read_lock(wbr);
35841 +       if (wbr->wbr_whbase) {
35842 +               delegated = NULL;
35843 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35844 +               if (unlikely(err == -EWOULDBLOCK)) {
35845 +                       pr_warn("cannot retry for NFSv4 delegation"
35846 +                               " for an internal link\n");
35847 +                       iput(delegated);
35848 +               }
35849 +               if (!err || err != -EMLINK)
35850 +                       goto out;
35851 +
35852 +               /* link count full. re-initialize br_whbase. */
35853 +               kick_reinit_br_wh(sb, br);
35854 +       }
35855 +
35856 +       /* return this error in this context */
35857 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35858 +       if (!err)
35859 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35860 +
35861 +out:
35862 +       wbr_wh_read_unlock(wbr);
35863 +       return err;
35864 +}
35865 +
35866 +/* ---------------------------------------------------------------------- */
35867 +
35868 +/*
35869 + * create or remove the diropq.
35870 + */
35871 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35872 +                               unsigned int flags)
35873 +{
35874 +       struct dentry *opq_dentry;
35875 +       struct super_block *sb;
35876 +       struct au_branch *br;
35877 +       struct path h_path;
35878 +       int err;
35879 +
35880 +       sb = dentry->d_sb;
35881 +       br = au_sbr(sb, bindex);
35882 +       h_path.dentry = au_h_dptr(dentry, bindex);
35883 +       h_path.mnt = au_br_mnt(br);
35884 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35885 +       if (IS_ERR(opq_dentry))
35886 +               goto out;
35887 +
35888 +       if (au_ftest_diropq(flags, CREATE)) {
35889 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35890 +               if (!err) {
35891 +                       au_set_dbdiropq(dentry, bindex);
35892 +                       goto out; /* success */
35893 +               }
35894 +       } else {
35895 +               h_path.dentry = opq_dentry;
35896 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35897 +               if (!err)
35898 +                       au_set_dbdiropq(dentry, -1);
35899 +       }
35900 +       dput(opq_dentry);
35901 +       opq_dentry = ERR_PTR(err);
35902 +
35903 +out:
35904 +       return opq_dentry;
35905 +}
35906 +
35907 +struct do_diropq_args {
35908 +       struct dentry **errp;
35909 +       struct dentry *dentry;
35910 +       aufs_bindex_t bindex;
35911 +       unsigned int flags;
35912 +};
35913 +
35914 +static void call_do_diropq(void *args)
35915 +{
35916 +       struct do_diropq_args *a = args;
35917 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35918 +}
35919 +
35920 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35921 +                            unsigned int flags)
35922 +{
35923 +       struct dentry *diropq, *h_dentry;
35924 +       struct mnt_idmap *h_idmap;
35925 +
35926 +       h_idmap = au_sbr_idmap(dentry->d_sb, bindex);
35927 +       h_dentry = au_h_dptr(dentry, bindex);
35928 +       if (!au_test_h_perm_sio(h_idmap, d_inode(h_dentry),
35929 +                               MAY_EXEC | MAY_WRITE))
35930 +               diropq = do_diropq(dentry, bindex, flags);
35931 +       else {
35932 +               int wkq_err;
35933 +               struct do_diropq_args args = {
35934 +                       .errp           = &diropq,
35935 +                       .dentry         = dentry,
35936 +                       .bindex         = bindex,
35937 +                       .flags          = flags
35938 +               };
35939 +
35940 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35941 +               if (unlikely(wkq_err))
35942 +                       diropq = ERR_PTR(wkq_err);
35943 +       }
35944 +
35945 +       return diropq;
35946 +}
35947 +
35948 +/* ---------------------------------------------------------------------- */
35949 +
35950 +/*
35951 + * lookup whiteout dentry.
35952 + * @h_parent: lower parent dentry which must exist and be locked
35953 + * @base_name: name of dentry which will be whiteouted
35954 + * returns dentry for whiteout.
35955 + */
35956 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35957 +                         struct au_branch *br)
35958 +{
35959 +       int err;
35960 +       struct qstr wh_name;
35961 +       struct dentry *wh_dentry;
35962 +       struct path h_path;
35963 +
35964 +       err = au_wh_name_alloc(&wh_name, base_name);
35965 +       wh_dentry = ERR_PTR(err);
35966 +       if (!err) {
35967 +               h_path.dentry = h_parent;
35968 +               h_path.mnt = au_br_mnt(br);
35969 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35970 +               au_kfree_try_rcu(wh_name.name);
35971 +       }
35972 +       return wh_dentry;
35973 +}
35974 +
35975 +/*
35976 + * link/create a whiteout for @dentry on @bindex.
35977 + */
35978 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35979 +                           struct dentry *h_parent)
35980 +{
35981 +       struct dentry *wh_dentry;
35982 +       struct super_block *sb;
35983 +       int err;
35984 +
35985 +       sb = dentry->d_sb;
35986 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35987 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35988 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35989 +               if (!err) {
35990 +                       au_set_dbwh(dentry, bindex);
35991 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35992 +               } else {
35993 +                       dput(wh_dentry);
35994 +                       wh_dentry = ERR_PTR(err);
35995 +               }
35996 +       }
35997 +
35998 +       return wh_dentry;
35999 +}
36000 +
36001 +/* ---------------------------------------------------------------------- */
36002 +
36003 +/* Delete all whiteouts in this directory on branch bindex. */
36004 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
36005 +                          aufs_bindex_t bindex)
36006 +{
36007 +       int err;
36008 +       unsigned long ul, n;
36009 +       struct qstr wh_name;
36010 +       char *p;
36011 +       struct hlist_head *head;
36012 +       struct au_vdir_wh *pos;
36013 +       struct au_vdir_destr *str;
36014 +
36015 +       err = -ENOMEM;
36016 +       p = (void *)__get_free_page(GFP_NOFS);
36017 +       wh_name.name = p;
36018 +       if (unlikely(!wh_name.name))
36019 +               goto out;
36020 +
36021 +       err = 0;
36022 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
36023 +       p += AUFS_WH_PFX_LEN;
36024 +       n = whlist->nh_num;
36025 +       head = whlist->nh_head;
36026 +       for (ul = 0; !err && ul < n; ul++, head++) {
36027 +               hlist_for_each_entry(pos, head, wh_hash) {
36028 +                       if (pos->wh_bindex != bindex)
36029 +                               continue;
36030 +
36031 +                       str = &pos->wh_str;
36032 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
36033 +                               memcpy(p, str->name, str->len);
36034 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
36035 +                               err = unlink_wh_name(h_path, &wh_name);
36036 +                               if (!err)
36037 +                                       continue;
36038 +                               break;
36039 +                       }
36040 +                       AuIOErr("whiteout name too long %.*s\n",
36041 +                               str->len, str->name);
36042 +                       err = -EIO;
36043 +                       break;
36044 +               }
36045 +       }
36046 +       free_page((unsigned long)wh_name.name);
36047 +
36048 +out:
36049 +       return err;
36050 +}
36051 +
36052 +struct del_wh_children_args {
36053 +       int *errp;
36054 +       struct path *h_path;
36055 +       struct au_nhash *whlist;
36056 +       aufs_bindex_t bindex;
36057 +};
36058 +
36059 +static void call_del_wh_children(void *args)
36060 +{
36061 +       struct del_wh_children_args *a = args;
36062 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
36063 +}
36064 +
36065 +/* ---------------------------------------------------------------------- */
36066 +
36067 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
36068 +{
36069 +       struct au_whtmp_rmdir *whtmp;
36070 +       int err;
36071 +       unsigned int rdhash;
36072 +
36073 +       SiMustAnyLock(sb);
36074 +
36075 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
36076 +       if (unlikely(!whtmp)) {
36077 +               whtmp = ERR_PTR(-ENOMEM);
36078 +               goto out;
36079 +       }
36080 +
36081 +       /* no estimation for dir size */
36082 +       rdhash = au_sbi(sb)->si_rdhash;
36083 +       if (!rdhash)
36084 +               rdhash = AUFS_RDHASH_DEF;
36085 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
36086 +       if (unlikely(err)) {
36087 +               au_kfree_rcu(whtmp);
36088 +               whtmp = ERR_PTR(err);
36089 +       }
36090 +
36091 +out:
36092 +       return whtmp;
36093 +}
36094 +
36095 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
36096 +{
36097 +       if (whtmp->br)
36098 +               au_lcnt_dec(&whtmp->br->br_count);
36099 +       dput(whtmp->wh_dentry);
36100 +       iput(whtmp->dir);
36101 +       au_nhash_wh_free(&whtmp->whlist);
36102 +       au_kfree_rcu(whtmp);
36103 +}
36104 +
36105 +/*
36106 + * rmdir the whiteouted temporary named dir @h_dentry.
36107 + * @whlist: whiteouted children.
36108 + */
36109 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36110 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
36111 +{
36112 +       int err;
36113 +       unsigned int h_nlink;
36114 +       struct path wh_path;
36115 +       struct inode *wh_inode, *h_dir;
36116 +       struct au_branch *br;
36117 +       struct mnt_idmap *h_idmap;
36118 +
36119 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
36120 +       IMustLock(h_dir);
36121 +
36122 +       br = au_sbr(dir->i_sb, bindex);
36123 +       wh_path.dentry = wh_dentry;
36124 +       wh_path.mnt = au_br_mnt(br);
36125 +       h_idmap = au_br_idmap(br);
36126 +       wh_inode = d_inode(wh_dentry);
36127 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
36128 +
36129 +       /*
36130 +        * someone else might change some whiteouts while we were sleeping.
36131 +        * it means this whlist may have an obsoleted entry.
36132 +        */
36133 +       if (!au_test_h_perm_sio(h_idmap, wh_inode, MAY_EXEC | MAY_WRITE))
36134 +               err = del_wh_children(&wh_path, whlist, bindex);
36135 +       else {
36136 +               int wkq_err;
36137 +               struct del_wh_children_args args = {
36138 +                       .errp           = &err,
36139 +                       .h_path         = &wh_path,
36140 +                       .whlist         = whlist,
36141 +                       .bindex         = bindex
36142 +               };
36143 +
36144 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
36145 +               if (unlikely(wkq_err))
36146 +                       err = wkq_err;
36147 +       }
36148 +       inode_unlock(wh_inode);
36149 +
36150 +       if (!err) {
36151 +               h_nlink = h_dir->i_nlink;
36152 +               err = vfsub_rmdir(h_dir, &wh_path);
36153 +               /* some fs doesn't change the parent nlink in some cases */
36154 +               h_nlink -= h_dir->i_nlink;
36155 +       }
36156 +
36157 +       if (!err) {
36158 +               if (au_ibtop(dir) == bindex) {
36159 +                       /* todo: dir->i_mutex is necessary */
36160 +                       au_cpup_attr_timesizes(dir);
36161 +                       if (h_nlink)
36162 +                               vfsub_drop_nlink(dir);
36163 +               }
36164 +               return 0; /* success */
36165 +       }
36166 +
36167 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
36168 +       return err;
36169 +}
36170 +
36171 +static void call_rmdir_whtmp(void *args)
36172 +{
36173 +       int err;
36174 +       aufs_bindex_t bindex;
36175 +       struct au_whtmp_rmdir *a = args;
36176 +       struct super_block *sb;
36177 +       struct dentry *h_parent;
36178 +       struct inode *h_dir;
36179 +       struct au_hinode *hdir;
36180 +
36181 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
36182 +       /* inode_lock(a->dir); */
36183 +       err = -EROFS;
36184 +       sb = a->dir->i_sb;
36185 +       si_read_lock(sb, !AuLock_FLUSH);
36186 +       if (!au_br_writable(a->br->br_perm))
36187 +               goto out;
36188 +       bindex = au_br_index(sb, a->br->br_id);
36189 +       if (unlikely(bindex < 0))
36190 +               goto out;
36191 +
36192 +       err = -EIO;
36193 +       ii_write_lock_parent(a->dir);
36194 +       h_parent = dget_parent(a->wh_dentry);
36195 +       h_dir = d_inode(h_parent);
36196 +       hdir = au_hi(a->dir, bindex);
36197 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
36198 +       if (unlikely(err))
36199 +               goto out_mnt;
36200 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
36201 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36202 +                         a->br);
36203 +       if (!err)
36204 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
36205 +       au_hn_inode_unlock(hdir);
36206 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
36207 +
36208 +out_mnt:
36209 +       dput(h_parent);
36210 +       ii_write_unlock(a->dir);
36211 +out:
36212 +       /* inode_unlock(a->dir); */
36213 +       au_whtmp_rmdir_free(a);
36214 +       si_read_unlock(sb);
36215 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36216 +       if (unlikely(err))
36217 +               AuIOErr("err %d\n", err);
36218 +}
36219 +
36220 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36221 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36222 +{
36223 +       int wkq_err;
36224 +       struct super_block *sb;
36225 +
36226 +       IMustLock(dir);
36227 +
36228 +       /* all post-process will be done in do_rmdir_whtmp(). */
36229 +       sb = dir->i_sb;
36230 +       args->dir = au_igrab(dir);
36231 +       args->br = au_sbr(sb, bindex);
36232 +       au_lcnt_inc(&args->br->br_count);
36233 +       args->wh_dentry = dget(wh_dentry);
36234 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
36235 +       if (unlikely(wkq_err)) {
36236 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
36237 +               au_whtmp_rmdir_free(args);
36238 +       }
36239 +}
36240 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
36241 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
36242 +++ linux/fs/aufs/whout.h       2024-05-13 17:13:00.621404284 +0200
36243 @@ -0,0 +1,87 @@
36244 +/* SPDX-License-Identifier: GPL-2.0 */
36245 +/*
36246 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36247 + *
36248 + * This program is free software; you can redistribute it and/or modify
36249 + * it under the terms of the GNU General Public License as published by
36250 + * the Free Software Foundation; either version 2 of the License, or
36251 + * (at your option) any later version.
36252 + *
36253 + * This program is distributed in the hope that it will be useful,
36254 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36255 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36256 + * GNU General Public License for more details.
36257 + *
36258 + * You should have received a copy of the GNU General Public License
36259 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36260 + */
36261 +
36262 +/*
36263 + * whiteout for logical deletion and opaque directory
36264 + */
36265 +
36266 +#ifndef __AUFS_WHOUT_H__
36267 +#define __AUFS_WHOUT_H__
36268 +
36269 +#ifdef __KERNEL__
36270 +
36271 +#include "dir.h"
36272 +
36273 +/* whout.c */
36274 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36275 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
36276 +              struct qstr *wh_name, int try_sio);
36277 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path);
36278 +struct au_branch;
36279 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36280 +                            struct qstr *prefix);
36281 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36282 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36283 +                       struct dentry *dentry);
36284 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36285 +
36286 +/* diropq flags */
36287 +#define AuDiropq_CREATE        1
36288 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36289 +#define au_fset_diropq(flags, name) \
36290 +       do { (flags) |= AuDiropq_##name; } while (0)
36291 +#define au_fclr_diropq(flags, name) \
36292 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36293 +
36294 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36295 +                            unsigned int flags);
36296 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36297 +                         struct au_branch *br);
36298 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36299 +                           struct dentry *h_parent);
36300 +
36301 +/* real rmdir for the whiteout-ed dir */
36302 +struct au_whtmp_rmdir {
36303 +       struct inode *dir;
36304 +       struct au_branch *br;
36305 +       struct dentry *wh_dentry;
36306 +       struct au_nhash whlist;
36307 +};
36308 +
36309 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36310 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36311 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36312 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36313 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36314 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36315 +
36316 +/* ---------------------------------------------------------------------- */
36317 +
36318 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36319 +                                             aufs_bindex_t bindex)
36320 +{
36321 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36322 +}
36323 +
36324 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36325 +{
36326 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36327 +}
36328 +
36329 +#endif /* __KERNEL__ */
36330 +#endif /* __AUFS_WHOUT_H__ */
36331 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36332 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36333 +++ linux/fs/aufs/wkq.c 2024-05-13 17:13:00.621404284 +0200
36334 @@ -0,0 +1,372 @@
36335 +// SPDX-License-Identifier: GPL-2.0
36336 +/*
36337 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36338 + *
36339 + * This program is free software; you can redistribute it and/or modify
36340 + * it under the terms of the GNU General Public License as published by
36341 + * the Free Software Foundation; either version 2 of the License, or
36342 + * (at your option) any later version.
36343 + *
36344 + * This program is distributed in the hope that it will be useful,
36345 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36346 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36347 + * GNU General Public License for more details.
36348 + *
36349 + * You should have received a copy of the GNU General Public License
36350 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36351 + */
36352 +
36353 +/*
36354 + * workqueue for asynchronous/super-io operations
36355 + * todo: try new credential scheme
36356 + */
36357 +
36358 +#include <linux/module.h>
36359 +#include "aufs.h"
36360 +
36361 +/* internal workqueue named AUFS_WKQ_NAME */
36362 +
36363 +static struct workqueue_struct *au_wkq;
36364 +
36365 +struct au_wkinfo {
36366 +       struct work_struct wk;
36367 +       struct kobject *kobj;
36368 +
36369 +       unsigned int flags; /* see wkq.h */
36370 +
36371 +       au_wkq_func_t func;
36372 +       void *args;
36373 +
36374 +#ifdef CONFIG_LOCKDEP
36375 +       int dont_check;
36376 +       struct held_lock **hlock;
36377 +#endif
36378 +
36379 +       struct completion *comp;
36380 +};
36381 +
36382 +/* ---------------------------------------------------------------------- */
36383 +/*
36384 + * Aufs passes some operations to the workqueue such as the internal copyup.
36385 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36386 + * job run by workqueue depends upon the locks acquired in the other task.
36387 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36388 + * information too. And the job in the workqueue restores the info in order to
36389 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36390 + * correctly and expectedly.
36391 + */
36392 +
36393 +#ifndef CONFIG_LOCKDEP
36394 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36395 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36396 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36397 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36398 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36399 +#else
36400 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36401 +{
36402 +       wkinfo->hlock = NULL;
36403 +       wkinfo->dont_check = 0;
36404 +}
36405 +
36406 +/*
36407 + * 1: matched
36408 + * 0: unmatched
36409 + */
36410 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36411 +{
36412 +       static DEFINE_SPINLOCK(spin);
36413 +       static struct {
36414 +               char *name;
36415 +               struct lock_class_key *key;
36416 +       } a[] = {
36417 +               { .name = "&sbinfo->si_rwsem" },
36418 +               { .name = "&finfo->fi_rwsem" },
36419 +               { .name = "&dinfo->di_rwsem" },
36420 +               { .name = "&iinfo->ii_rwsem" }
36421 +       };
36422 +       static int set;
36423 +       int i;
36424 +
36425 +       /* lockless read from 'set.' see below */
36426 +       if (set == ARRAY_SIZE(a)) {
36427 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36428 +                       if (a[i].key == key)
36429 +                               goto match;
36430 +               goto unmatch;
36431 +       }
36432 +
36433 +       spin_lock(&spin);
36434 +       if (set)
36435 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36436 +                       if (a[i].key == key) {
36437 +                               spin_unlock(&spin);
36438 +                               goto match;
36439 +                       }
36440 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36441 +               if (a[i].key) {
36442 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36443 +                               spin_unlock(&spin);
36444 +                               goto match;
36445 +                       } else
36446 +                               continue;
36447 +               }
36448 +               if (strstr(a[i].name, name)) {
36449 +                       /*
36450 +                        * the order of these three lines is important for the
36451 +                        * lockless read above.
36452 +                        */
36453 +                       a[i].key = key;
36454 +                       spin_unlock(&spin);
36455 +                       set++;
36456 +                       /* AuDbg("%d, %s\n", set, name); */
36457 +                       goto match;
36458 +               }
36459 +       }
36460 +       spin_unlock(&spin);
36461 +       goto unmatch;
36462 +
36463 +match:
36464 +       return 1;
36465 +unmatch:
36466 +       return 0;
36467 +}
36468 +
36469 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36470 +{
36471 +       int err, n;
36472 +       struct task_struct *curr;
36473 +       struct held_lock **hl, *held_locks, *p;
36474 +
36475 +       err = 0;
36476 +       curr = current;
36477 +       wkinfo->dont_check = lockdep_recursing(curr);
36478 +       if (wkinfo->dont_check)
36479 +               goto out;
36480 +       n = curr->lockdep_depth;
36481 +       if (!n)
36482 +               goto out;
36483 +
36484 +       err = -ENOMEM;
36485 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36486 +       if (unlikely(!wkinfo->hlock))
36487 +               goto out;
36488 +
36489 +       err = 0;
36490 +#if 0 /* left for debugging */
36491 +       if (0 && au_debug_test())
36492 +               lockdep_print_held_locks(curr);
36493 +#endif
36494 +       held_locks = curr->held_locks;
36495 +       hl = wkinfo->hlock;
36496 +       while (n--) {
36497 +               p = held_locks++;
36498 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36499 +                       *hl++ = p;
36500 +       }
36501 +       *hl = NULL;
36502 +
36503 +out:
36504 +       return err;
36505 +}
36506 +
36507 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36508 +{
36509 +       au_kfree_try_rcu(wkinfo->hlock);
36510 +}
36511 +
36512 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36513 +{
36514 +       struct held_lock *p, **hl = wkinfo->hlock;
36515 +       int subclass;
36516 +
36517 +       if (wkinfo->dont_check)
36518 +               lockdep_off();
36519 +       if (!hl)
36520 +               return;
36521 +       while ((p = *hl++)) { /* assignment */
36522 +               subclass = lockdep_hlock_class(p)->subclass;
36523 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36524 +               if (p->read)
36525 +                       rwsem_acquire_read(p->instance, subclass, 0,
36526 +                                          /*p->acquire_ip*/_RET_IP_);
36527 +               else
36528 +                       rwsem_acquire(p->instance, subclass, 0,
36529 +                                     /*p->acquire_ip*/_RET_IP_);
36530 +       }
36531 +}
36532 +
36533 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36534 +{
36535 +       struct held_lock *p, **hl = wkinfo->hlock;
36536 +
36537 +       if (wkinfo->dont_check)
36538 +               lockdep_on();
36539 +       if (!hl)
36540 +               return;
36541 +       while ((p = *hl++)) /* assignment */
36542 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36543 +}
36544 +#endif
36545 +
36546 +static void wkq_func(struct work_struct *wk)
36547 +{
36548 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36549 +
36550 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36551 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36552 +
36553 +       au_wkq_lockdep_pre(wkinfo);
36554 +       wkinfo->func(wkinfo->args);
36555 +       au_wkq_lockdep_post(wkinfo);
36556 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36557 +               complete(wkinfo->comp);
36558 +       else {
36559 +               kobject_put(wkinfo->kobj);
36560 +               module_put(THIS_MODULE); /* todo: ?? */
36561 +               au_kfree_rcu(wkinfo);
36562 +       }
36563 +}
36564 +
36565 +/*
36566 + * Since struct completion is large, try allocating it dynamically.
36567 + */
36568 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36569 +
36570 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36571 +{
36572 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36573 +       if (*comp) {
36574 +               init_completion(*comp);
36575 +               wkinfo->comp = *comp;
36576 +               return 0;
36577 +       }
36578 +       return -ENOMEM;
36579 +}
36580 +
36581 +static void au_wkq_comp_free(struct completion *comp)
36582 +{
36583 +       au_kfree_rcu(comp);
36584 +}
36585 +
36586 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36587 +{
36588 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36589 +               if (au_wkq_test()) {
36590 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36591 +                               " due to a dead dir by UDBA,"
36592 +                               " or async xino write?\n");
36593 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36594 +               }
36595 +       } else
36596 +               au_dbg_verify_kthread();
36597 +
36598 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36599 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36600 +               queue_work(au_wkq, &wkinfo->wk);
36601 +       } else {
36602 +               INIT_WORK(&wkinfo->wk, wkq_func);
36603 +               schedule_work(&wkinfo->wk);
36604 +       }
36605 +}
36606 +
36607 +/*
36608 + * Be careful. It is easy to make deadlock happen.
36609 + * processA: lock, wkq and wait
36610 + * processB: wkq and wait, lock in wkq
36611 + * --> deadlock
36612 + */
36613 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36614 +{
36615 +       int err;
36616 +       AuWkqCompDeclare(comp);
36617 +       struct au_wkinfo wkinfo = {
36618 +               .flags  = flags,
36619 +               .func   = func,
36620 +               .args   = args
36621 +       };
36622 +
36623 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36624 +       if (unlikely(err))
36625 +               goto out;
36626 +       err = au_wkq_lockdep_alloc(&wkinfo);
36627 +       if (unlikely(err))
36628 +               goto out_comp;
36629 +       if (!err) {
36630 +               au_wkq_run(&wkinfo);
36631 +               /* no timeout, no interrupt */
36632 +               wait_for_completion(wkinfo.comp);
36633 +       }
36634 +       au_wkq_lockdep_free(&wkinfo);
36635 +
36636 +out_comp:
36637 +       au_wkq_comp_free(comp);
36638 +out:
36639 +       destroy_work_on_stack(&wkinfo.wk);
36640 +       return err;
36641 +}
36642 +
36643 +/*
36644 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36645 + * problem in a concurrent umounting.
36646 + */
36647 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36648 +                 unsigned int flags)
36649 +{
36650 +       int err;
36651 +       struct au_wkinfo *wkinfo;
36652 +
36653 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36654 +
36655 +       /*
36656 +        * wkq_func() must free this wkinfo.
36657 +        * it highly depends upon the implementation of workqueue.
36658 +        */
36659 +       err = 0;
36660 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36661 +       if (wkinfo) {
36662 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36663 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36664 +               wkinfo->func = func;
36665 +               wkinfo->args = args;
36666 +               wkinfo->comp = NULL;
36667 +               au_wkq_lockdep_init(wkinfo);
36668 +               kobject_get(wkinfo->kobj);
36669 +               __module_get(THIS_MODULE); /* todo: ?? */
36670 +
36671 +               au_wkq_run(wkinfo);
36672 +       } else {
36673 +               err = -ENOMEM;
36674 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36675 +       }
36676 +
36677 +       return err;
36678 +}
36679 +
36680 +/* ---------------------------------------------------------------------- */
36681 +
36682 +void au_nwt_init(struct au_nowait_tasks *nwt)
36683 +{
36684 +       atomic_set(&nwt->nw_len, 0);
36685 +       /* smp_mb(); */ /* atomic_set */
36686 +       init_waitqueue_head(&nwt->nw_wq);
36687 +}
36688 +
36689 +void au_wkq_fin(void)
36690 +{
36691 +       destroy_workqueue(au_wkq);
36692 +}
36693 +
36694 +int __init au_wkq_init(void)
36695 +{
36696 +       int err;
36697 +
36698 +       err = 0;
36699 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36700 +       if (IS_ERR(au_wkq))
36701 +               err = PTR_ERR(au_wkq);
36702 +       else if (!au_wkq)
36703 +               err = -ENOMEM;
36704 +
36705 +       return err;
36706 +}
36707 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36708 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36709 +++ linux/fs/aufs/wkq.h 2024-05-13 17:13:00.621404284 +0200
36710 @@ -0,0 +1,89 @@
36711 +/* SPDX-License-Identifier: GPL-2.0 */
36712 +/*
36713 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36714 + *
36715 + * This program is free software; you can redistribute it and/or modify
36716 + * it under the terms of the GNU General Public License as published by
36717 + * the Free Software Foundation; either version 2 of the License, or
36718 + * (at your option) any later version.
36719 + *
36720 + * This program is distributed in the hope that it will be useful,
36721 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36722 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36723 + * GNU General Public License for more details.
36724 + *
36725 + * You should have received a copy of the GNU General Public License
36726 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36727 + */
36728 +
36729 +/*
36730 + * workqueue for asynchronous/super-io operations
36731 + * todo: try new credentials management scheme
36732 + */
36733 +
36734 +#ifndef __AUFS_WKQ_H__
36735 +#define __AUFS_WKQ_H__
36736 +
36737 +#ifdef __KERNEL__
36738 +
36739 +#include <linux/wait.h>
36740 +
36741 +struct super_block;
36742 +
36743 +/* ---------------------------------------------------------------------- */
36744 +
36745 +/*
36746 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36747 + */
36748 +struct au_nowait_tasks {
36749 +       atomic_t                nw_len;
36750 +       wait_queue_head_t       nw_wq;
36751 +};
36752 +
36753 +/* ---------------------------------------------------------------------- */
36754 +
36755 +typedef void (*au_wkq_func_t)(void *args);
36756 +
36757 +/* wkq flags */
36758 +#define AuWkq_WAIT     BIT(0)
36759 +#define AuWkq_NEST     BIT(1)
36760 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36761 +#define au_fset_wkq(flags, name) \
36762 +       do { (flags) |= AuWkq_##name; } while (0)
36763 +#define au_fclr_wkq(flags, name) \
36764 +       do { (flags) &= ~AuWkq_##name; } while (0)
36765 +
36766 +/* wkq.c */
36767 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36768 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36769 +                 unsigned int flags);
36770 +void au_nwt_init(struct au_nowait_tasks *nwt);
36771 +int __init au_wkq_init(void);
36772 +void au_wkq_fin(void);
36773 +
36774 +/* ---------------------------------------------------------------------- */
36775 +
36776 +static inline int au_wkq_test(void)
36777 +{
36778 +       return current->flags & PF_WQ_WORKER;
36779 +}
36780 +
36781 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36782 +{
36783 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36784 +}
36785 +
36786 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36787 +{
36788 +       if (atomic_dec_and_test(&nwt->nw_len))
36789 +               wake_up_all(&nwt->nw_wq);
36790 +}
36791 +
36792 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36793 +{
36794 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36795 +       return 0;
36796 +}
36797 +
36798 +#endif /* __KERNEL__ */
36799 +#endif /* __AUFS_WKQ_H__ */
36800 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36801 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36802 +++ linux/fs/aufs/xattr.c       2024-05-13 17:13:00.621404284 +0200
36803 @@ -0,0 +1,360 @@
36804 +// SPDX-License-Identifier: GPL-2.0
36805 +/*
36806 + * Copyright (C) 2014-2022 Junjiro R. Okajima
36807 + *
36808 + * This program is free software; you can redistribute it and/or modify
36809 + * it under the terms of the GNU General Public License as published by
36810 + * the Free Software Foundation; either version 2 of the License, or
36811 + * (at your option) any later version.
36812 + *
36813 + * This program is distributed in the hope that it will be useful,
36814 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36815 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36816 + * GNU General Public License for more details.
36817 + *
36818 + * You should have received a copy of the GNU General Public License
36819 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36820 + */
36821 +
36822 +/*
36823 + * handling xattr functions
36824 + */
36825 +
36826 +#include <linux/fs.h>
36827 +#include <linux/xattr.h>
36828 +#include "aufs.h"
36829 +
36830 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36831 +{
36832 +       if (!ignore_flags)
36833 +               goto out;
36834 +       switch (err) {
36835 +       case -ENOMEM:
36836 +       case -EDQUOT:
36837 +               goto out;
36838 +       }
36839 +
36840 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36841 +               err = 0;
36842 +               goto out;
36843 +       }
36844 +
36845 +#define cmp(brattr, prefix) do {                                       \
36846 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36847 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36848 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36849 +                               err = 0;                                \
36850 +                       goto out;                                       \
36851 +               }                                                       \
36852 +       } while (0)
36853 +
36854 +       cmp(SEC, SECURITY);
36855 +       cmp(SYS, SYSTEM);
36856 +       cmp(TR, TRUSTED);
36857 +       cmp(USR, USER);
36858 +#undef cmp
36859 +
36860 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36861 +               err = 0;
36862 +
36863 +out:
36864 +       return err;
36865 +}
36866 +
36867 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36868 +
36869 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36870 +                           char *name, char **buf, unsigned int ignore_flags,
36871 +                           unsigned int verbose)
36872 +{
36873 +       int err, is_acl;
36874 +       ssize_t ssz;
36875 +       struct inode *h_idst;
36876 +       struct dentry *h_dst_dentry, *h_src_dentry;
36877 +       struct mnt_idmap *h_dst_idmap, *h_src_idmap;
36878 +       struct posix_acl *acl;
36879 +
36880 +       is_acl = !!is_posix_acl_xattr(name);
36881 +       h_src_idmap = mnt_idmap(h_src->mnt);
36882 +       h_src_dentry = h_src->dentry;
36883 +       if (is_acl) {
36884 +               acl = vfs_get_acl(h_src_idmap, h_src_dentry, name);
36885 +               AuDebugOn(!acl);
36886 +               if (IS_ERR(acl)) {
36887 +                       err = PTR_ERR(acl);
36888 +                       if (err == -ENODATA)
36889 +                               err = 0;
36890 +                       else if (err == -EOPNOTSUPP
36891 +                                && au_test_nfs_noacl(d_inode(h_src_dentry)))
36892 +                               err = 0;
36893 +                       else if (verbose || au_debug_test())
36894 +                               pr_err("%s, err %d\n", name, err);
36895 +                       goto out;
36896 +               }
36897 +       } else {
36898 +               ssz = vfs_getxattr_alloc(h_src_idmap, h_src_dentry, name, buf,
36899 +                                        0, GFP_NOFS);
36900 +               if (unlikely(ssz <= 0)) {
36901 +                       err = ssz;
36902 +                       if (err == -ENODATA)
36903 +                               err = 0;
36904 +                       else if (err == -EOPNOTSUPP
36905 +                                && (ignore_flags & au_xattr_out_of_list))
36906 +                                err = 0;
36907 +                       else if (err && (verbose || au_debug_test()))
36908 +                               pr_err("%s, err %d\n", name, err);
36909 +                       goto out;
36910 +               }
36911 +       }
36912 +
36913 +       /* unlock it temporary */
36914 +       h_dst_idmap = mnt_idmap(h_dst->mnt);
36915 +       h_dst_dentry = h_dst->dentry;
36916 +       h_idst = d_inode(h_dst_dentry);
36917 +       inode_unlock(h_idst);
36918 +       if (is_acl) {
36919 +               err = vfsub_set_acl(h_dst_idmap, h_dst_dentry, name, acl);
36920 +               posix_acl_release(acl);
36921 +       } else
36922 +               err = vfsub_setxattr(h_dst_idmap, h_dst_dentry, name, *buf,
36923 +                                    ssz, /*flags*/0);
36924 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36925 +       if (unlikely(err)) {
36926 +               if (verbose || au_debug_test())
36927 +                       pr_err("%s, err %d\n", name, err);
36928 +               err = au_xattr_ignore(err, name, ignore_flags);
36929 +       }
36930 +
36931 +out:
36932 +       return err;
36933 +}
36934 +
36935 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36936 +                 unsigned int verbose)
36937 +{
36938 +       int err, unlocked;
36939 +       ssize_t ssz;
36940 +       struct dentry *h_dst_dentry, *h_src_dentry;
36941 +       struct inode *h_isrc, *h_idst;
36942 +       char *value, *p, *o, *e;
36943 +
36944 +       /* try stopping to update the source inode while we are referencing */
36945 +       /* there should not be the parent-child relationship between them */
36946 +       h_dst_dentry = h_dst->dentry;
36947 +       h_idst = d_inode(h_dst_dentry);
36948 +       h_src_dentry = h_src->dentry;
36949 +       h_isrc = d_inode(h_src_dentry);
36950 +       inode_unlock(h_idst);
36951 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36952 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36953 +       unlocked = 0;
36954 +
36955 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36956 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36957 +       err = ssz;
36958 +       if (unlikely(err < 0)) {
36959 +               AuTraceErr(err);
36960 +               if (err == -ENODATA
36961 +                   || err == -EOPNOTSUPP)
36962 +                       err = 0;        /* ignore */
36963 +               goto out;
36964 +       }
36965 +
36966 +       err = 0;
36967 +       p = NULL;
36968 +       o = NULL;
36969 +       if (ssz) {
36970 +               err = -ENOMEM;
36971 +               p = kmalloc(ssz, GFP_NOFS);
36972 +               o = p;
36973 +               if (unlikely(!p))
36974 +                       goto out;
36975 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36976 +       }
36977 +       inode_unlock_shared(h_isrc);
36978 +       unlocked = 1;
36979 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36980 +       if (unlikely(err < 0))
36981 +               goto out_free;
36982 +
36983 +       err = 0;
36984 +       e = p + ssz;
36985 +       value = NULL;
36986 +       ignore_flags |= au_xattr_out_of_list;
36987 +       while (!err && p < e) {
36988 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36989 +                                      verbose);
36990 +               p += strlen(p) + 1;
36991 +       }
36992 +       au_kfree_try_rcu(value);
36993 +
36994 +out_free:
36995 +       au_kfree_try_rcu(o);
36996 +out:
36997 +       if (!unlocked)
36998 +               inode_unlock_shared(h_isrc);
36999 +       AuTraceErr(err);
37000 +       return err;
37001 +}
37002 +
37003 +/* ---------------------------------------------------------------------- */
37004 +
37005 +static int au_smack_reentering(struct super_block *sb)
37006 +{
37007 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
37008 +       /*
37009 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
37010 +        * i_op->getxattr(). ouch.
37011 +        */
37012 +       return si_pid_test(sb);
37013 +#else
37014 +       return 0;
37015 +#endif
37016 +}
37017 +
37018 +enum {
37019 +       AU_XATTR_LIST,
37020 +       AU_XATTR_GET
37021 +};
37022 +
37023 +struct au_lgxattr {
37024 +       int type;
37025 +       union {
37026 +               struct {
37027 +                       char    *list;
37028 +                       size_t  size;
37029 +               } list;
37030 +               struct {
37031 +                       const char      *name;
37032 +                       void            *value;
37033 +                       size_t          size;
37034 +               } get;
37035 +       } u;
37036 +};
37037 +
37038 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
37039 +                         struct au_lgxattr *arg)
37040 +{
37041 +       ssize_t err;
37042 +       int reenter;
37043 +       struct path h_path;
37044 +       struct super_block *sb;
37045 +
37046 +       sb = dentry->d_sb;
37047 +       reenter = au_smack_reentering(sb);
37048 +       if (!reenter) {
37049 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
37050 +               if (unlikely(err))
37051 +                       goto out;
37052 +       }
37053 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
37054 +       if (unlikely(err))
37055 +               goto out_si;
37056 +       if (unlikely(!h_path.dentry))
37057 +               /* illegally overlapped or something */
37058 +               goto out_di; /* pretending success */
37059 +
37060 +       /* always topmost entry only */
37061 +       switch (arg->type) {
37062 +       case AU_XATTR_LIST:
37063 +               err = vfs_listxattr(h_path.dentry,
37064 +                                   arg->u.list.list, arg->u.list.size);
37065 +               break;
37066 +       case AU_XATTR_GET:
37067 +               AuDebugOn(d_is_negative(h_path.dentry));
37068 +               err = vfs_getxattr(mnt_idmap(h_path.mnt), h_path.dentry,
37069 +                                  arg->u.get.name, arg->u.get.value,
37070 +                                  arg->u.get.size);
37071 +               break;
37072 +       }
37073 +
37074 +out_di:
37075 +       if (!reenter)
37076 +               di_read_unlock(dentry, AuLock_IR);
37077 +out_si:
37078 +       if (!reenter)
37079 +               si_read_unlock(sb);
37080 +out:
37081 +       AuTraceErr(err);
37082 +       return err;
37083 +}
37084 +
37085 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
37086 +{
37087 +       struct au_lgxattr arg = {
37088 +               .type = AU_XATTR_LIST,
37089 +               .u.list = {
37090 +                       .list   = list,
37091 +                       .size   = size
37092 +               },
37093 +       };
37094 +
37095 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
37096 +}
37097 +
37098 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
37099 +                          const char *name, void *value, size_t size)
37100 +{
37101 +       struct au_lgxattr arg = {
37102 +               .type = AU_XATTR_GET,
37103 +               .u.get = {
37104 +                       .name   = name,
37105 +                       .value  = value,
37106 +                       .size   = size
37107 +               },
37108 +       };
37109 +
37110 +       return au_lgxattr(dentry, inode, &arg);
37111 +}
37112 +
37113 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
37114 +                      const char *name, const void *value, size_t size,
37115 +                      int flags)
37116 +{
37117 +       struct au_sxattr arg = {
37118 +               .type = AU_XATTR_SET,
37119 +               .u.set = {
37120 +                       .name   = name,
37121 +                       .value  = value,
37122 +                       .size   = size,
37123 +                       .flags  = flags
37124 +               },
37125 +       };
37126 +
37127 +       return au_sxattr(dentry, inode, &arg);
37128 +}
37129 +
37130 +/* ---------------------------------------------------------------------- */
37131 +
37132 +static int au_xattr_get(const struct xattr_handler *handler,
37133 +                       struct dentry *dentry, struct inode *inode,
37134 +                       const char *name, void *buffer, size_t size)
37135 +{
37136 +       return au_getxattr(dentry, inode, name, buffer, size);
37137 +}
37138 +
37139 +static int au_xattr_set(const struct xattr_handler *handler,
37140 +                       struct mnt_idmap *idmap,
37141 +                       struct dentry *dentry, struct inode *inode,
37142 +                       const char *name, const void *value, size_t size,
37143 +                       int flags)
37144 +{
37145 +       return au_setxattr(dentry, inode, name, value, size, flags);
37146 +}
37147 +
37148 +static const struct xattr_handler au_xattr_handler = {
37149 +       .name   = "",
37150 +       .prefix = "",
37151 +       .get    = au_xattr_get,
37152 +       .set    = au_xattr_set
37153 +};
37154 +
37155 +static const struct xattr_handler *au_xattr_handlers[] = {
37156 +       &au_xattr_handler, /* must be last */
37157 +       NULL
37158 +};
37159 +
37160 +void au_xattr_init(struct super_block *sb)
37161 +{
37162 +       sb->s_xattr = au_xattr_handlers;
37163 +}
37164 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
37165 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
37166 +++ linux/fs/aufs/xino.c        2024-05-13 17:13:00.624737645 +0200
37167 @@ -0,0 +1,1926 @@
37168 +// SPDX-License-Identifier: GPL-2.0
37169 +/*
37170 + * Copyright (C) 2005-2022 Junjiro R. Okajima
37171 + *
37172 + * This program is free software; you can redistribute it and/or modify
37173 + * it under the terms of the GNU General Public License as published by
37174 + * the Free Software Foundation; either version 2 of the License, or
37175 + * (at your option) any later version.
37176 + *
37177 + * This program is distributed in the hope that it will be useful,
37178 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37179 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37180 + * GNU General Public License for more details.
37181 + *
37182 + * You should have received a copy of the GNU General Public License
37183 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37184 + */
37185 +
37186 +/*
37187 + * external inode number translation table and bitmap
37188 + *
37189 + * things to consider
37190 + * - the lifetime
37191 + *   + au_xino object
37192 + *   + XINO files (xino, xib, xigen)
37193 + *   + dynamic debugfs entries (xiN)
37194 + *   + static debugfs entries (xib, xigen)
37195 + *   + static sysfs entry (xi_path)
37196 + * - several entry points to handle them.
37197 + *   + mount(2) without xino option (default)
37198 + *   + mount(2) with xino option
37199 + *   + mount(2) with noxino option
37200 + *   + umount(2)
37201 + *   + remount with add/del branches
37202 + *   + remount with xino/noxino options
37203 + */
37204 +
37205 +#include <linux/seq_file.h>
37206 +#include <linux/statfs.h>
37207 +#include "aufs.h"
37208 +
37209 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37210 +                                    aufs_bindex_t bbot,
37211 +                                    struct super_block *h_sb)
37212 +{
37213 +       /* todo: try binary-search if the branches are many */
37214 +       for (; btop <= bbot; btop++)
37215 +               if (h_sb == au_sbr_sb(sb, btop))
37216 +                       return btop;
37217 +       return -1;
37218 +}
37219 +
37220 +/*
37221 + * find another branch who is on the same filesystem of the specified
37222 + * branch{@btgt}. search until @bbot.
37223 + */
37224 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37225 +                                 aufs_bindex_t bbot)
37226 +{
37227 +       aufs_bindex_t bindex;
37228 +       struct super_block *tgt_sb;
37229 +
37230 +       tgt_sb = au_sbr_sb(sb, btgt);
37231 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37232 +       if (bindex < 0)
37233 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
37234 +
37235 +       return bindex;
37236 +}
37237 +
37238 +/* ---------------------------------------------------------------------- */
37239 +
37240 +/*
37241 + * stop unnecessary notify events at creating xino files
37242 + */
37243 +
37244 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37245 +{
37246 +       aufs_bindex_t bfound, bindex, bbot;
37247 +       struct dentry *parent;
37248 +       struct au_branch *br;
37249 +
37250 +       bfound = -1;
37251 +       parent = dentry->d_parent; /* safe d_parent access */
37252 +       bbot = au_sbbot(sb);
37253 +       for (bindex = 0; bindex <= bbot; bindex++) {
37254 +               br = au_sbr(sb, bindex);
37255 +               if (au_br_dentry(br) == parent) {
37256 +                       bfound = bindex;
37257 +                       break;
37258 +               }
37259 +       }
37260 +
37261 +       AuDbg("bfound b%d\n", bfound);
37262 +       return bfound;
37263 +}
37264 +
37265 +struct au_xino_lock_dir {
37266 +       struct au_hinode *hdir;
37267 +       struct dentry *parent;
37268 +       struct inode *dir;
37269 +};
37270 +
37271 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37272 +                                         unsigned int lsc)
37273 +{
37274 +       struct dentry *parent;
37275 +       struct inode *dir;
37276 +
37277 +       parent = dget_parent(dentry);
37278 +       dir = d_inode(parent);
37279 +       inode_lock_nested(dir, lsc);
37280 +#if 0 /* it should not happen */
37281 +       spin_lock(&dentry->d_lock);
37282 +       if (unlikely(dentry->d_parent != parent)) {
37283 +               spin_unlock(&dentry->d_lock);
37284 +               inode_unlock(dir);
37285 +               dput(parent);
37286 +               parent = NULL;
37287 +               goto out;
37288 +       }
37289 +       spin_unlock(&dentry->d_lock);
37290 +
37291 +out:
37292 +#endif
37293 +       return parent;
37294 +}
37295 +
37296 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37297 +                            struct au_xino_lock_dir *ldir)
37298 +{
37299 +       aufs_bindex_t bindex;
37300 +
37301 +       ldir->hdir = NULL;
37302 +       bindex = au_xi_root(sb, xipath->dentry);
37303 +       if (bindex >= 0) {
37304 +               /* rw branch root */
37305 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37306 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37307 +       } else {
37308 +               /* other */
37309 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37310 +                                                  AuLsc_I_PARENT);
37311 +               ldir->dir = d_inode(ldir->parent);
37312 +       }
37313 +}
37314 +
37315 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37316 +{
37317 +       if (ldir->hdir)
37318 +               au_hn_inode_unlock(ldir->hdir);
37319 +       else {
37320 +               inode_unlock(ldir->dir);
37321 +               dput(ldir->parent);
37322 +       }
37323 +}
37324 +
37325 +/* ---------------------------------------------------------------------- */
37326 +
37327 +/*
37328 + * create and set a new xino file
37329 + */
37330 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37331 +                           int wbrtop)
37332 +{
37333 +       struct file *file;
37334 +       struct dentry *h_parent, *d;
37335 +       struct inode *h_dir, *inode;
37336 +       int err;
37337 +       static DEFINE_MUTEX(mtx);
37338 +
37339 +       /*
37340 +        * at mount-time, and the xino file is the default path,
37341 +        * hnotify is disabled so we have no notify events to ignore.
37342 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37343 +        */
37344 +       if (!wbrtop)
37345 +               mutex_lock(&mtx);
37346 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37347 +                              /* | __FMODE_NONOTIFY */,
37348 +                              0666);
37349 +       if (IS_ERR(file)) {
37350 +               if (!wbrtop)
37351 +                       mutex_unlock(&mtx);
37352 +               if (!silent)
37353 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37354 +               return file;
37355 +       }
37356 +
37357 +       /* keep file count */
37358 +       err = 0;
37359 +       d = file->f_path.dentry;
37360 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37361 +       if (!wbrtop)
37362 +               mutex_unlock(&mtx);
37363 +       /* mnt_want_write() is unnecessary here */
37364 +       h_dir = d_inode(h_parent);
37365 +       inode = file_inode(file);
37366 +       /* no delegation since it is just created */
37367 +       if (inode->i_nlink)
37368 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37369 +                                  /*force*/0);
37370 +       inode_unlock(h_dir);
37371 +       dput(h_parent);
37372 +       if (unlikely(err)) {
37373 +               if (!silent)
37374 +                       pr_err("unlink %s(%d)\n", fpath, err);
37375 +               goto out;
37376 +       }
37377 +
37378 +       err = -EINVAL;
37379 +       if (unlikely(sb && sb == d->d_sb)) {
37380 +               if (!silent)
37381 +                       pr_err("%s must be outside\n", fpath);
37382 +               goto out;
37383 +       }
37384 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37385 +               if (!silent)
37386 +                       pr_err("xino doesn't support %s(%s)\n",
37387 +                              fpath, au_sbtype(d->d_sb));
37388 +               goto out;
37389 +       }
37390 +       return file; /* success */
37391 +
37392 +out:
37393 +       fput(file);
37394 +       file = ERR_PTR(err);
37395 +       return file;
37396 +}
37397 +
37398 +/*
37399 + * create a new xinofile at the same place/path as @base.
37400 + */
37401 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37402 +                            struct file *copy_src)
37403 +{
37404 +       struct file *file;
37405 +       struct dentry *dentry;
37406 +       struct inode *dir, *delegated;
37407 +       struct qstr *name;
37408 +       struct path ppath, path;
37409 +       int err, do_unlock;
37410 +       struct au_xino_lock_dir ldir;
37411 +
37412 +       do_unlock = 1;
37413 +       au_xino_lock_dir(sb, base, &ldir);
37414 +       dentry = base->dentry;
37415 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37416 +       ppath.mnt = base->mnt;
37417 +       dir = d_inode(ppath.dentry);
37418 +       IMustLock(dir);
37419 +
37420 +       name = &dentry->d_name;
37421 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37422 +       if (IS_ERR(path.dentry)) {
37423 +               file = (void *)path.dentry;
37424 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37425 +               goto out;
37426 +       }
37427 +
37428 +       /* no need to mnt_want_write() since we call dentry_open() later */
37429 +       err = vfs_create(mnt_idmap(base->mnt), dir, path.dentry, 0666, NULL);
37430 +       if (unlikely(err)) {
37431 +               file = ERR_PTR(err);
37432 +               pr_err("%pd create err %d\n", dentry, err);
37433 +               goto out_dput;
37434 +       }
37435 +
37436 +       path.mnt = base->mnt;
37437 +       file = vfsub_dentry_open(&path,
37438 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37439 +                                /* | __FMODE_NONOTIFY */);
37440 +       if (IS_ERR(file)) {
37441 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37442 +               goto out_dput;
37443 +       }
37444 +
37445 +       delegated = NULL;
37446 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37447 +       au_xino_unlock_dir(&ldir);
37448 +       do_unlock = 0;
37449 +       if (unlikely(err == -EWOULDBLOCK)) {
37450 +               pr_warn("cannot retry for NFSv4 delegation"
37451 +                       " for an internal unlink\n");
37452 +               iput(delegated);
37453 +       }
37454 +       if (unlikely(err)) {
37455 +               pr_err("%pd unlink err %d\n", dentry, err);
37456 +               goto out_fput;
37457 +       }
37458 +
37459 +       if (copy_src) {
37460 +               /* no one can touch copy_src xino */
37461 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37462 +               if (unlikely(err)) {
37463 +                       pr_err("%pd copy err %d\n", dentry, err);
37464 +                       goto out_fput;
37465 +               }
37466 +       }
37467 +       goto out_dput; /* success */
37468 +
37469 +out_fput:
37470 +       fput(file);
37471 +       file = ERR_PTR(err);
37472 +out_dput:
37473 +       dput(path.dentry);
37474 +out:
37475 +       if (do_unlock)
37476 +               au_xino_unlock_dir(&ldir);
37477 +       return file;
37478 +}
37479 +
37480 +struct file *au_xino_file1(struct au_xino *xi)
37481 +{
37482 +       struct file *file;
37483 +       unsigned int u, nfile;
37484 +
37485 +       file = NULL;
37486 +       nfile = xi->xi_nfile;
37487 +       for (u = 0; u < nfile; u++) {
37488 +               file = xi->xi_file[u];
37489 +               if (file)
37490 +                       break;
37491 +       }
37492 +
37493 +       return file;
37494 +}
37495 +
37496 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37497 +{
37498 +       int err;
37499 +       struct file *f;
37500 +       void *p;
37501 +
37502 +       if (file)
37503 +               get_file(file);
37504 +
37505 +       err = 0;
37506 +       f = NULL;
37507 +       if (idx < xi->xi_nfile) {
37508 +               f = xi->xi_file[idx];
37509 +               if (f)
37510 +                       fput(f);
37511 +       } else {
37512 +               p = au_kzrealloc(xi->xi_file,
37513 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37514 +                                sizeof(*xi->xi_file) * (idx + 1),
37515 +                                GFP_NOFS, /*may_shrink*/0);
37516 +               if (p) {
37517 +                       MtxMustLock(&xi->xi_mtx);
37518 +                       xi->xi_file = p;
37519 +                       xi->xi_nfile = idx + 1;
37520 +               } else {
37521 +                       err = -ENOMEM;
37522 +                       if (file)
37523 +                               fput(file);
37524 +                       goto out;
37525 +               }
37526 +       }
37527 +       xi->xi_file[idx] = file;
37528 +
37529 +out:
37530 +       return err;
37531 +}
37532 +
37533 +/*
37534 + * if @xinew->xi is not set, then create new xigen file.
37535 + */
37536 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37537 +{
37538 +       struct file *file;
37539 +       int err;
37540 +
37541 +       SiMustAnyLock(sb);
37542 +
37543 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37544 +       if (IS_ERR(file)) {
37545 +               err = PTR_ERR(file);
37546 +               pr_err("%s[%d], err %d\n",
37547 +                      xinew->xi ? "xino" : "xigen",
37548 +                      xinew->idx, err);
37549 +               goto out;
37550 +       }
37551 +
37552 +       if (xinew->xi)
37553 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37554 +       else {
37555 +               BUG();
37556 +               /* todo: make xigen file an array */
37557 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37558 +       }
37559 +       fput(file);
37560 +       if (unlikely(err))
37561 +               file = ERR_PTR(err);
37562 +
37563 +out:
37564 +       return file;
37565 +}
37566 +
37567 +/* ---------------------------------------------------------------------- */
37568 +
37569 +/*
37570 + * truncate xino files
37571 + */
37572 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37573 +                           int idx, struct kstatfs *st)
37574 +{
37575 +       int err;
37576 +       blkcnt_t blocks;
37577 +       struct file *file, *new_xino;
37578 +       struct au_xi_new xinew = {
37579 +               .idx = idx
37580 +       };
37581 +
37582 +       err = 0;
37583 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37584 +       file = au_xino_file(xinew.xi, idx);
37585 +       if (!file)
37586 +               goto out;
37587 +
37588 +       xinew.base = &file->f_path;
37589 +       err = vfs_statfs(xinew.base, st);
37590 +       if (unlikely(err)) {
37591 +               AuErr1("statfs err %d, ignored\n", err);
37592 +               err = 0;
37593 +               goto out;
37594 +       }
37595 +
37596 +       blocks = file_inode(file)->i_blocks;
37597 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37598 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37599 +
37600 +       xinew.copy_src = file;
37601 +       new_xino = au_xi_new(sb, &xinew);
37602 +       if (IS_ERR(new_xino)) {
37603 +               err = PTR_ERR(new_xino);
37604 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37605 +               goto out;
37606 +       }
37607 +
37608 +       err = vfs_statfs(&new_xino->f_path, st);
37609 +       if (!err)
37610 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37611 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37612 +                       st->f_bfree, st->f_blocks);
37613 +       else {
37614 +               AuErr1("statfs err %d, ignored\n", err);
37615 +               err = 0;
37616 +       }
37617 +
37618 +out:
37619 +       return err;
37620 +}
37621 +
37622 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37623 +{
37624 +       int err, i;
37625 +       unsigned long jiffy;
37626 +       aufs_bindex_t bbot;
37627 +       struct kstatfs *st;
37628 +       struct au_branch *br;
37629 +       struct au_xino *xi;
37630 +
37631 +       err = -ENOMEM;
37632 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37633 +       if (unlikely(!st))
37634 +               goto out;
37635 +
37636 +       err = -EINVAL;
37637 +       bbot = au_sbbot(sb);
37638 +       if (unlikely(bindex < 0 || bbot < bindex))
37639 +               goto out_st;
37640 +
37641 +       err = 0;
37642 +       jiffy = jiffies;
37643 +       br = au_sbr(sb, bindex);
37644 +       xi = br->br_xino;
37645 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37646 +               err = au_xino_do_trunc(sb, bindex, i, st);
37647 +       if (!err)
37648 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37649 +
37650 +out_st:
37651 +       au_kfree_rcu(st);
37652 +out:
37653 +       return err;
37654 +}
37655 +
37656 +struct xino_do_trunc_args {
37657 +       struct super_block *sb;
37658 +       struct au_branch *br;
37659 +       int idx;
37660 +};
37661 +
37662 +static void xino_do_trunc(void *_args)
37663 +{
37664 +       struct xino_do_trunc_args *args = _args;
37665 +       struct super_block *sb;
37666 +       struct au_branch *br;
37667 +       struct inode *dir;
37668 +       int err, idx;
37669 +       aufs_bindex_t bindex;
37670 +
37671 +       err = 0;
37672 +       sb = args->sb;
37673 +       dir = d_inode(sb->s_root);
37674 +       br = args->br;
37675 +       idx = args->idx;
37676 +
37677 +       si_noflush_write_lock(sb);
37678 +       ii_read_lock_parent(dir);
37679 +       bindex = au_br_index(sb, br->br_id);
37680 +       err = au_xino_trunc(sb, bindex, idx);
37681 +       ii_read_unlock(dir);
37682 +       if (unlikely(err))
37683 +               pr_warn("err b%d, (%d)\n", bindex, err);
37684 +       atomic_dec(&br->br_xino->xi_truncating);
37685 +       au_lcnt_dec(&br->br_count);
37686 +       si_write_unlock(sb);
37687 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37688 +       au_kfree_rcu(args);
37689 +}
37690 +
37691 +/*
37692 + * returns the index in the xi_file array whose corresponding file is necessary
37693 + * to truncate, or -1 which means no need to truncate.
37694 + */
37695 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37696 +{
37697 +       int err;
37698 +       unsigned int u;
37699 +       struct kstatfs st;
37700 +       struct au_sbinfo *sbinfo;
37701 +       struct au_xino *xi;
37702 +       struct file *file;
37703 +
37704 +       /* todo: si_xino_expire and the ratio should be customizable */
37705 +       sbinfo = au_sbi(sb);
37706 +       if (time_before(jiffies,
37707 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37708 +               return -1;
37709 +
37710 +       /* truncation border */
37711 +       xi = br->br_xino;
37712 +       for (u = 0; u < xi->xi_nfile; u++) {
37713 +               file = au_xino_file(xi, u);
37714 +               if (!file)
37715 +                       continue;
37716 +
37717 +               err = vfs_statfs(&file->f_path, &st);
37718 +               if (unlikely(err)) {
37719 +                       AuErr1("statfs err %d, ignored\n", err);
37720 +                       return -1;
37721 +               }
37722 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37723 +                   >= AUFS_XINO_DEF_TRUNC)
37724 +                       return u;
37725 +       }
37726 +
37727 +       return -1;
37728 +}
37729 +
37730 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37731 +{
37732 +       int idx;
37733 +       struct xino_do_trunc_args *args;
37734 +       int wkq_err;
37735 +
37736 +       idx = xino_trunc_test(sb, br);
37737 +       if (idx < 0)
37738 +               return;
37739 +
37740 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37741 +               goto out;
37742 +
37743 +       /* lock and kfree() will be called in trunc_xino() */
37744 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37745 +       if (unlikely(!args)) {
37746 +               AuErr1("no memory\n");
37747 +               goto out;
37748 +       }
37749 +
37750 +       au_lcnt_inc(&br->br_count);
37751 +       args->sb = sb;
37752 +       args->br = br;
37753 +       args->idx = idx;
37754 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37755 +       if (!wkq_err)
37756 +               return; /* success */
37757 +
37758 +       pr_err("wkq %d\n", wkq_err);
37759 +       au_lcnt_dec(&br->br_count);
37760 +       au_kfree_rcu(args);
37761 +
37762 +out:
37763 +       atomic_dec(&br->br_xino->xi_truncating);
37764 +}
37765 +
37766 +/* ---------------------------------------------------------------------- */
37767 +
37768 +struct au_xi_calc {
37769 +       int idx;
37770 +       loff_t pos;
37771 +};
37772 +
37773 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37774 +                      struct au_xi_calc *calc)
37775 +{
37776 +       loff_t maxent;
37777 +
37778 +       maxent = au_xi_maxent(sb);
37779 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37780 +       calc->pos *= sizeof(ino_t);
37781 +}
37782 +
37783 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37784 +                               struct au_xi_calc *calc)
37785 +{
37786 +       int err;
37787 +       struct file *file;
37788 +       struct au_xino *xi = br->br_xino;
37789 +       struct au_xi_new xinew = {
37790 +               .xi = xi
37791 +       };
37792 +
37793 +       SiMustAnyLock(sb);
37794 +
37795 +       err = 0;
37796 +       if (!xi)
37797 +               goto out;
37798 +
37799 +       mutex_lock(&xi->xi_mtx);
37800 +       file = au_xino_file(xi, calc->idx);
37801 +       if (file)
37802 +               goto out_mtx;
37803 +
37804 +       file = au_xino_file(xi, /*idx*/-1);
37805 +       AuDebugOn(!file);
37806 +       xinew.idx = calc->idx;
37807 +       xinew.base = &file->f_path;
37808 +       /* xinew.copy_src = NULL; */
37809 +       file = au_xi_new(sb, &xinew);
37810 +       if (IS_ERR(file))
37811 +               err = PTR_ERR(file);
37812 +
37813 +out_mtx:
37814 +       mutex_unlock(&xi->xi_mtx);
37815 +out:
37816 +       return err;
37817 +}
37818 +
37819 +struct au_xino_do_new_async_args {
37820 +       struct super_block *sb;
37821 +       struct au_branch *br;
37822 +       struct au_xi_calc calc;
37823 +       ino_t ino;
37824 +};
37825 +
37826 +struct au_xi_writing {
37827 +       struct hlist_bl_node node;
37828 +       ino_t h_ino, ino;
37829 +};
37830 +
37831 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37832 +                           ino_t ino);
37833 +
37834 +static void au_xino_call_do_new_async(void *args)
37835 +{
37836 +       struct au_xino_do_new_async_args *a = args;
37837 +       struct au_branch *br;
37838 +       struct super_block *sb;
37839 +       struct au_sbinfo *sbi;
37840 +       struct inode *root;
37841 +       struct file *file;
37842 +       struct au_xi_writing *del, *p;
37843 +       struct hlist_bl_head *hbl;
37844 +       struct hlist_bl_node *pos;
37845 +       int err;
37846 +
37847 +       br = a->br;
37848 +       sb = a->sb;
37849 +       sbi = au_sbi(sb);
37850 +       si_noflush_read_lock(sb);
37851 +       root = d_inode(sb->s_root);
37852 +       ii_read_lock_child(root);
37853 +       err = au_xino_do_new_async(sb, br, &a->calc);
37854 +       if (unlikely(err)) {
37855 +               AuIOErr("err %d\n", err);
37856 +               goto out;
37857 +       }
37858 +
37859 +       file = au_xino_file(br->br_xino, a->calc.idx);
37860 +       AuDebugOn(!file);
37861 +       err = au_xino_do_write(file, &a->calc, a->ino);
37862 +       if (unlikely(err)) {
37863 +               AuIOErr("err %d\n", err);
37864 +               goto out;
37865 +       }
37866 +
37867 +       del = NULL;
37868 +       hbl = &br->br_xino->xi_writing;
37869 +       hlist_bl_lock(hbl);
37870 +       au_hbl_for_each(pos, hbl) {
37871 +               p = container_of(pos, struct au_xi_writing, node);
37872 +               if (p->ino == a->ino) {
37873 +                       del = p;
37874 +                       hlist_bl_del(&p->node);
37875 +                       break;
37876 +               }
37877 +       }
37878 +       hlist_bl_unlock(hbl);
37879 +       au_kfree_rcu(del);
37880 +
37881 +out:
37882 +       au_lcnt_dec(&br->br_count);
37883 +       ii_read_unlock(root);
37884 +       si_read_unlock(sb);
37885 +       au_nwt_done(&sbi->si_nowait);
37886 +       au_kfree_rcu(a);
37887 +}
37888 +
37889 +/*
37890 + * create a new xino file asynchronously
37891 + */
37892 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37893 +                            struct au_xi_calc *calc, ino_t ino)
37894 +{
37895 +       int err;
37896 +       struct au_xino_do_new_async_args *arg;
37897 +
37898 +       err = -ENOMEM;
37899 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37900 +       if (unlikely(!arg))
37901 +               goto out;
37902 +
37903 +       arg->sb = sb;
37904 +       arg->br = br;
37905 +       arg->calc = *calc;
37906 +       arg->ino = ino;
37907 +       au_lcnt_inc(&br->br_count);
37908 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37909 +       if (unlikely(err)) {
37910 +               pr_err("wkq %d\n", err);
37911 +               au_lcnt_dec(&br->br_count);
37912 +               au_kfree_rcu(arg);
37913 +       }
37914 +
37915 +out:
37916 +       return err;
37917 +}
37918 +
37919 +/*
37920 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37921 + * at the position of @h_ino.
37922 + */
37923 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37924 +                ino_t *ino)
37925 +{
37926 +       int err;
37927 +       ssize_t sz;
37928 +       struct au_xi_calc calc;
37929 +       struct au_sbinfo *sbinfo;
37930 +       struct file *file;
37931 +       struct au_xino *xi;
37932 +       struct hlist_bl_head *hbl;
37933 +       struct hlist_bl_node *pos;
37934 +       struct au_xi_writing *p;
37935 +
37936 +       *ino = 0;
37937 +       if (!au_opt_test(au_mntflags(sb), XINO))
37938 +               return 0; /* no xino */
37939 +
37940 +       err = 0;
37941 +       au_xi_calc(sb, h_ino, &calc);
37942 +       xi = au_sbr(sb, bindex)->br_xino;
37943 +       file = au_xino_file(xi, calc.idx);
37944 +       if (!file) {
37945 +               hbl = &xi->xi_writing;
37946 +               hlist_bl_lock(hbl);
37947 +               au_hbl_for_each(pos, hbl) {
37948 +                       p = container_of(pos, struct au_xi_writing, node);
37949 +                       if (p->h_ino == h_ino) {
37950 +                               AuDbg("hi%llu, i%llu, found\n",
37951 +                                     (u64)p->h_ino, (u64)p->ino);
37952 +                               *ino = p->ino;
37953 +                               break;
37954 +                       }
37955 +               }
37956 +               hlist_bl_unlock(hbl);
37957 +               return 0;
37958 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37959 +               return 0; /* no xino */
37960 +
37961 +       sbinfo = au_sbi(sb);
37962 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37963 +       if (sz == sizeof(*ino))
37964 +               return 0; /* success */
37965 +
37966 +       err = sz;
37967 +       if (unlikely(sz >= 0)) {
37968 +               err = -EIO;
37969 +               AuIOErr("xino read error (%zd)\n", sz);
37970 +       }
37971 +       return err;
37972 +}
37973 +
37974 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37975 +                           ino_t ino)
37976 +{
37977 +       ssize_t sz;
37978 +
37979 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37980 +       if (sz == sizeof(ino))
37981 +               return 0; /* success */
37982 +
37983 +       AuIOErr("write failed (%zd)\n", sz);
37984 +       return -EIO;
37985 +}
37986 +
37987 +/*
37988 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37989 + * at the position of @h_ino.
37990 + * even if @ino is zero, it is written to the xinofile and means no entry.
37991 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37992 + * try truncating it.
37993 + */
37994 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37995 +                 ino_t ino)
37996 +{
37997 +       int err;
37998 +       unsigned int mnt_flags;
37999 +       struct au_xi_calc calc;
38000 +       struct file *file;
38001 +       struct au_branch *br;
38002 +       struct au_xino *xi;
38003 +       struct au_xi_writing *p;
38004 +
38005 +       SiMustAnyLock(sb);
38006 +
38007 +       mnt_flags = au_mntflags(sb);
38008 +       if (!au_opt_test(mnt_flags, XINO))
38009 +               return 0;
38010 +
38011 +       au_xi_calc(sb, h_ino, &calc);
38012 +       br = au_sbr(sb, bindex);
38013 +       xi = br->br_xino;
38014 +       file = au_xino_file(xi, calc.idx);
38015 +       if (!file) {
38016 +               /* store the inum pair into the list */
38017 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
38018 +               p->h_ino = h_ino;
38019 +               p->ino = ino;
38020 +               au_hbl_add(&p->node, &xi->xi_writing);
38021 +
38022 +               /* create and write a new xino file asynchronously */
38023 +               err = au_xino_new_async(sb, br, &calc, ino);
38024 +               if (!err)
38025 +                       return 0; /* success */
38026 +               goto out;
38027 +       }
38028 +
38029 +       err = au_xino_do_write(file, &calc, ino);
38030 +       if (!err) {
38031 +               br = au_sbr(sb, bindex);
38032 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
38033 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38034 +                       xino_try_trunc(sb, br);
38035 +               return 0; /* success */
38036 +       }
38037 +
38038 +out:
38039 +       AuIOErr("write failed (%d)\n", err);
38040 +       return -EIO;
38041 +}
38042 +
38043 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38044 +                             loff_t *pos);
38045 +
38046 +/* todo: unnecessary to support mmap_sem since kernel-space? */
38047 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
38048 +{
38049 +       ssize_t err;
38050 +       int i;
38051 +       const int prevent_endless = 10;
38052 +
38053 +       i = 0;
38054 +       do {
38055 +               err = vfsub_read_k(file, kbuf, size, pos);
38056 +               if (err == -EINTR
38057 +                   && !au_wkq_test()
38058 +                   && fatal_signal_pending(current)) {
38059 +                       err = xino_fread_wkq(file, kbuf, size, pos);
38060 +                       BUG_ON(err == -EINTR);
38061 +               }
38062 +       } while (i++ < prevent_endless
38063 +                && (err == -EAGAIN || err == -EINTR));
38064 +
38065 +#if 0 /* reserved for future use */
38066 +       if (err > 0)
38067 +               fsnotify_access(file->f_path.dentry);
38068 +#endif
38069 +
38070 +       return err;
38071 +}
38072 +
38073 +struct xino_fread_args {
38074 +       ssize_t *errp;
38075 +       struct file *file;
38076 +       void *buf;
38077 +       size_t size;
38078 +       loff_t *pos;
38079 +};
38080 +
38081 +static void call_xino_fread(void *args)
38082 +{
38083 +       struct xino_fread_args *a = args;
38084 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
38085 +}
38086 +
38087 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38088 +                             loff_t *pos)
38089 +{
38090 +       ssize_t err;
38091 +       int wkq_err;
38092 +       struct xino_fread_args args = {
38093 +               .errp   = &err,
38094 +               .file   = file,
38095 +               .buf    = buf,
38096 +               .size   = size,
38097 +               .pos    = pos
38098 +       };
38099 +
38100 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
38101 +       if (unlikely(wkq_err))
38102 +               err = wkq_err;
38103 +
38104 +       return err;
38105 +}
38106 +
38107 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38108 +                              loff_t *pos);
38109 +
38110 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
38111 +                             loff_t *pos)
38112 +{
38113 +       ssize_t err;
38114 +       int i;
38115 +       const int prevent_endless = 10;
38116 +
38117 +       i = 0;
38118 +       do {
38119 +               err = vfsub_write_k(file, kbuf, size, pos);
38120 +               if (err == -EINTR
38121 +                   && !au_wkq_test()
38122 +                   && fatal_signal_pending(current)) {
38123 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
38124 +                       BUG_ON(err == -EINTR);
38125 +               }
38126 +       } while (i++ < prevent_endless
38127 +                && (err == -EAGAIN || err == -EINTR));
38128 +
38129 +#if 0 /* reserved for future use */
38130 +       if (err > 0)
38131 +               fsnotify_modify(file->f_path.dentry);
38132 +#endif
38133 +
38134 +       return err;
38135 +}
38136 +
38137 +struct do_xino_fwrite_args {
38138 +       ssize_t *errp;
38139 +       struct file *file;
38140 +       void *buf;
38141 +       size_t size;
38142 +       loff_t *pos;
38143 +};
38144 +
38145 +static void call_do_xino_fwrite(void *args)
38146 +{
38147 +       struct do_xino_fwrite_args *a = args;
38148 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
38149 +}
38150 +
38151 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38152 +                              loff_t *pos)
38153 +{
38154 +       ssize_t err;
38155 +       int wkq_err;
38156 +       struct do_xino_fwrite_args args = {
38157 +               .errp   = &err,
38158 +               .file   = file,
38159 +               .buf    = buf,
38160 +               .size   = size,
38161 +               .pos    = pos
38162 +       };
38163 +
38164 +       /*
38165 +        * it breaks RLIMIT_FSIZE and normal user's limit,
38166 +        * users should care about quota and real 'filesystem full.'
38167 +        */
38168 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38169 +       if (unlikely(wkq_err))
38170 +               err = wkq_err;
38171 +
38172 +       return err;
38173 +}
38174 +
38175 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
38176 +{
38177 +       ssize_t err;
38178 +
38179 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38180 +               lockdep_off();
38181 +               err = do_xino_fwrite(file, buf, size, pos);
38182 +               lockdep_on();
38183 +       } else {
38184 +               lockdep_off();
38185 +               err = xino_fwrite_wkq(file, buf, size, pos);
38186 +               lockdep_on();
38187 +       }
38188 +
38189 +       return err;
38190 +}
38191 +
38192 +/* ---------------------------------------------------------------------- */
38193 +
38194 +/*
38195 + * inode number bitmap
38196 + */
38197 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38198 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
38199 +{
38200 +       ino_t ino;
38201 +
38202 +       AuDebugOn(bit < 0 || page_bits <= bit);
38203 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38204 +       return ino;
38205 +}
38206 +
38207 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38208 +{
38209 +       AuDebugOn(ino < AUFS_FIRST_INO);
38210 +       ino -= AUFS_FIRST_INO;
38211 +       *pindex = ino / page_bits;
38212 +       *bit = ino % page_bits;
38213 +}
38214 +
38215 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
38216 +{
38217 +       int err;
38218 +       loff_t pos;
38219 +       ssize_t sz;
38220 +       struct au_sbinfo *sbinfo;
38221 +       struct file *xib;
38222 +       unsigned long *p;
38223 +
38224 +       sbinfo = au_sbi(sb);
38225 +       MtxMustLock(&sbinfo->si_xib_mtx);
38226 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38227 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38228 +
38229 +       if (pindex == sbinfo->si_xib_last_pindex)
38230 +               return 0;
38231 +
38232 +       xib = sbinfo->si_xib;
38233 +       p = sbinfo->si_xib_buf;
38234 +       pos = sbinfo->si_xib_last_pindex;
38235 +       pos *= PAGE_SIZE;
38236 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38237 +       if (unlikely(sz != PAGE_SIZE))
38238 +               goto out;
38239 +
38240 +       pos = pindex;
38241 +       pos *= PAGE_SIZE;
38242 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38243 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
38244 +       else {
38245 +               memset(p, 0, PAGE_SIZE);
38246 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38247 +       }
38248 +       if (sz == PAGE_SIZE) {
38249 +               sbinfo->si_xib_last_pindex = pindex;
38250 +               return 0; /* success */
38251 +       }
38252 +
38253 +out:
38254 +       AuIOErr1("write failed (%zd)\n", sz);
38255 +       err = sz;
38256 +       if (sz >= 0)
38257 +               err = -EIO;
38258 +       return err;
38259 +}
38260 +
38261 +static void au_xib_clear_bit(struct inode *inode)
38262 +{
38263 +       int err, bit;
38264 +       unsigned long pindex;
38265 +       struct super_block *sb;
38266 +       struct au_sbinfo *sbinfo;
38267 +
38268 +       AuDebugOn(inode->i_nlink);
38269 +
38270 +       sb = inode->i_sb;
38271 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38272 +       AuDebugOn(page_bits <= bit);
38273 +       sbinfo = au_sbi(sb);
38274 +       mutex_lock(&sbinfo->si_xib_mtx);
38275 +       err = xib_pindex(sb, pindex);
38276 +       if (!err) {
38277 +               clear_bit(bit, sbinfo->si_xib_buf);
38278 +               sbinfo->si_xib_next_bit = bit;
38279 +       }
38280 +       mutex_unlock(&sbinfo->si_xib_mtx);
38281 +}
38282 +
38283 +/* ---------------------------------------------------------------------- */
38284 +
38285 +/*
38286 + * truncate a xino bitmap file
38287 + */
38288 +
38289 +/* todo: slow */
38290 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38291 +{
38292 +       int err, bit;
38293 +       ssize_t sz;
38294 +       unsigned long pindex;
38295 +       loff_t pos, pend;
38296 +       struct au_sbinfo *sbinfo;
38297 +       ino_t *ino;
38298 +       unsigned long *p;
38299 +
38300 +       err = 0;
38301 +       sbinfo = au_sbi(sb);
38302 +       MtxMustLock(&sbinfo->si_xib_mtx);
38303 +       p = sbinfo->si_xib_buf;
38304 +       pend = vfsub_f_size_read(file);
38305 +       pos = 0;
38306 +       while (pos < pend) {
38307 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
38308 +               err = sz;
38309 +               if (unlikely(sz <= 0))
38310 +                       goto out;
38311 +
38312 +               err = 0;
38313 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38314 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38315 +                               continue;
38316 +
38317 +                       xib_calc_bit(*ino, &pindex, &bit);
38318 +                       AuDebugOn(page_bits <= bit);
38319 +                       err = xib_pindex(sb, pindex);
38320 +                       if (!err)
38321 +                               set_bit(bit, p);
38322 +                       else
38323 +                               goto out;
38324 +               }
38325 +       }
38326 +
38327 +out:
38328 +       return err;
38329 +}
38330 +
38331 +static int xib_restore(struct super_block *sb)
38332 +{
38333 +       int err, i;
38334 +       unsigned int nfile;
38335 +       aufs_bindex_t bindex, bbot;
38336 +       void *page;
38337 +       struct au_branch *br;
38338 +       struct au_xino *xi;
38339 +       struct file *file;
38340 +
38341 +       err = -ENOMEM;
38342 +       page = (void *)__get_free_page(GFP_NOFS);
38343 +       if (unlikely(!page))
38344 +               goto out;
38345 +
38346 +       err = 0;
38347 +       bbot = au_sbbot(sb);
38348 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38349 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38350 +                       br = au_sbr(sb, bindex);
38351 +                       xi = br->br_xino;
38352 +                       nfile = xi->xi_nfile;
38353 +                       for (i = 0; i < nfile; i++) {
38354 +                               file = au_xino_file(xi, i);
38355 +                               if (file)
38356 +                                       err = do_xib_restore(sb, file, page);
38357 +                       }
38358 +               } else
38359 +                       AuDbg("skip shared b%d\n", bindex);
38360 +       free_page((unsigned long)page);
38361 +
38362 +out:
38363 +       return err;
38364 +}
38365 +
38366 +int au_xib_trunc(struct super_block *sb)
38367 +{
38368 +       int err;
38369 +       ssize_t sz;
38370 +       loff_t pos;
38371 +       struct au_sbinfo *sbinfo;
38372 +       unsigned long *p;
38373 +       struct file *file;
38374 +
38375 +       SiMustWriteLock(sb);
38376 +
38377 +       err = 0;
38378 +       sbinfo = au_sbi(sb);
38379 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38380 +               goto out;
38381 +
38382 +       file = sbinfo->si_xib;
38383 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38384 +               goto out;
38385 +
38386 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38387 +       err = PTR_ERR(file);
38388 +       if (IS_ERR(file))
38389 +               goto out;
38390 +       fput(sbinfo->si_xib);
38391 +       sbinfo->si_xib = file;
38392 +
38393 +       p = sbinfo->si_xib_buf;
38394 +       memset(p, 0, PAGE_SIZE);
38395 +       pos = 0;
38396 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38397 +       if (unlikely(sz != PAGE_SIZE)) {
38398 +               err = sz;
38399 +               AuIOErr("err %d\n", err);
38400 +               if (sz >= 0)
38401 +                       err = -EIO;
38402 +               goto out;
38403 +       }
38404 +
38405 +       mutex_lock(&sbinfo->si_xib_mtx);
38406 +       /* mnt_want_write() is unnecessary here */
38407 +       err = xib_restore(sb);
38408 +       mutex_unlock(&sbinfo->si_xib_mtx);
38409 +
38410 +out:
38411 +       return err;
38412 +}
38413 +
38414 +/* ---------------------------------------------------------------------- */
38415 +
38416 +struct au_xino *au_xino_alloc(unsigned int nfile)
38417 +{
38418 +       struct au_xino *xi;
38419 +
38420 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38421 +       if (unlikely(!xi))
38422 +               goto out;
38423 +       xi->xi_nfile = nfile;
38424 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38425 +       if (unlikely(!xi->xi_file))
38426 +               goto out_free;
38427 +
38428 +       xi->xi_nondir.total = 8; /* initial size */
38429 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38430 +                                     GFP_NOFS);
38431 +       if (unlikely(!xi->xi_nondir.array))
38432 +               goto out_file;
38433 +
38434 +       spin_lock_init(&xi->xi_nondir.spin);
38435 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38436 +       mutex_init(&xi->xi_mtx);
38437 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38438 +       atomic_set(&xi->xi_truncating, 0);
38439 +       kref_init(&xi->xi_kref);
38440 +       goto out; /* success */
38441 +
38442 +out_file:
38443 +       au_kfree_try_rcu(xi->xi_file);
38444 +out_free:
38445 +       au_kfree_rcu(xi);
38446 +       xi = NULL;
38447 +out:
38448 +       return xi;
38449 +}
38450 +
38451 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38452 +{
38453 +       int err;
38454 +       struct au_xino *xi;
38455 +
38456 +       err = 0;
38457 +       xi = au_xino_alloc(idx + 1);
38458 +       if (unlikely(!xi)) {
38459 +               err = -ENOMEM;
38460 +               goto out;
38461 +       }
38462 +
38463 +       if (file)
38464 +               get_file(file);
38465 +       xi->xi_file[idx] = file;
38466 +       AuDebugOn(br->br_xino);
38467 +       br->br_xino = xi;
38468 +
38469 +out:
38470 +       return err;
38471 +}
38472 +
38473 +static void au_xino_release(struct kref *kref)
38474 +{
38475 +       struct au_xino *xi;
38476 +       int i;
38477 +       unsigned long ul;
38478 +       struct hlist_bl_head *hbl;
38479 +       struct hlist_bl_node *pos, *n;
38480 +       struct au_xi_writing *p;
38481 +
38482 +       xi = container_of(kref, struct au_xino, xi_kref);
38483 +       for (i = 0; i < xi->xi_nfile; i++)
38484 +               if (xi->xi_file[i])
38485 +                       fput(xi->xi_file[i]);
38486 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38487 +               AuDebugOn(xi->xi_nondir.array[i]);
38488 +       mutex_destroy(&xi->xi_mtx);
38489 +       hbl = &xi->xi_writing;
38490 +       ul = au_hbl_count(hbl);
38491 +       if (unlikely(ul)) {
38492 +               pr_warn("xi_writing %lu\n", ul);
38493 +               hlist_bl_lock(hbl);
38494 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38495 +                       hlist_bl_del(&p->node);
38496 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38497 +                       kfree(p);
38498 +               }
38499 +               hlist_bl_unlock(hbl);
38500 +       }
38501 +       au_kfree_try_rcu(xi->xi_file);
38502 +       au_kfree_try_rcu(xi->xi_nondir.array);
38503 +       au_kfree_rcu(xi);
38504 +}
38505 +
38506 +int au_xino_put(struct au_branch *br)
38507 +{
38508 +       int ret;
38509 +       struct au_xino *xi;
38510 +
38511 +       ret = 0;
38512 +       xi = br->br_xino;
38513 +       if (xi) {
38514 +               br->br_xino = NULL;
38515 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38516 +       }
38517 +
38518 +       return ret;
38519 +}
38520 +
38521 +/* ---------------------------------------------------------------------- */
38522 +
38523 +/*
38524 + * xino mount option handlers
38525 + */
38526 +
38527 +/* xino bitmap */
38528 +static void xino_clear_xib(struct super_block *sb)
38529 +{
38530 +       struct au_sbinfo *sbinfo;
38531 +
38532 +       SiMustWriteLock(sb);
38533 +
38534 +       sbinfo = au_sbi(sb);
38535 +       if (sbinfo->si_xib)
38536 +               fput(sbinfo->si_xib);
38537 +       sbinfo->si_xib = NULL;
38538 +       if (sbinfo->si_xib_buf)
38539 +               free_page((unsigned long)sbinfo->si_xib_buf);
38540 +       sbinfo->si_xib_buf = NULL;
38541 +}
38542 +
38543 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38544 +{
38545 +       int err;
38546 +       loff_t pos;
38547 +       struct au_sbinfo *sbinfo;
38548 +       struct file *file;
38549 +       struct super_block *xi_sb;
38550 +
38551 +       SiMustWriteLock(sb);
38552 +
38553 +       sbinfo = au_sbi(sb);
38554 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38555 +       err = PTR_ERR(file);
38556 +       if (IS_ERR(file))
38557 +               goto out;
38558 +       if (sbinfo->si_xib)
38559 +               fput(sbinfo->si_xib);
38560 +       sbinfo->si_xib = file;
38561 +       xi_sb = file_inode(file)->i_sb;
38562 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38563 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38564 +               err = -EIO;
38565 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38566 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38567 +               goto out_unset;
38568 +       }
38569 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38570 +
38571 +       err = -ENOMEM;
38572 +       if (!sbinfo->si_xib_buf)
38573 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38574 +       if (unlikely(!sbinfo->si_xib_buf))
38575 +               goto out_unset;
38576 +
38577 +       sbinfo->si_xib_last_pindex = 0;
38578 +       sbinfo->si_xib_next_bit = 0;
38579 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38580 +               pos = 0;
38581 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38582 +               if (unlikely(err != PAGE_SIZE))
38583 +                       goto out_free;
38584 +       }
38585 +       err = 0;
38586 +       goto out; /* success */
38587 +
38588 +out_free:
38589 +       if (sbinfo->si_xib_buf)
38590 +               free_page((unsigned long)sbinfo->si_xib_buf);
38591 +       sbinfo->si_xib_buf = NULL;
38592 +       if (err >= 0)
38593 +               err = -EIO;
38594 +out_unset:
38595 +       fput(sbinfo->si_xib);
38596 +       sbinfo->si_xib = NULL;
38597 +out:
38598 +       AuTraceErr(err);
38599 +       return err;
38600 +}
38601 +
38602 +/* xino for each branch */
38603 +static void xino_clear_br(struct super_block *sb)
38604 +{
38605 +       aufs_bindex_t bindex, bbot;
38606 +       struct au_branch *br;
38607 +
38608 +       bbot = au_sbbot(sb);
38609 +       for (bindex = 0; bindex <= bbot; bindex++) {
38610 +               br = au_sbr(sb, bindex);
38611 +               AuDebugOn(!br);
38612 +               au_xino_put(br);
38613 +       }
38614 +}
38615 +
38616 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38617 +                                 aufs_bindex_t bshared)
38618 +{
38619 +       struct au_branch *brshared;
38620 +
38621 +       brshared = au_sbr(sb, bshared);
38622 +       AuDebugOn(!brshared->br_xino);
38623 +       AuDebugOn(!brshared->br_xino->xi_file);
38624 +       if (br->br_xino != brshared->br_xino) {
38625 +               au_xino_get(brshared);
38626 +               au_xino_put(br);
38627 +               br->br_xino = brshared->br_xino;
38628 +       }
38629 +}
38630 +
38631 +struct au_xino_do_set_br {
38632 +       struct au_branch *br;
38633 +       ino_t h_ino;
38634 +       aufs_bindex_t bshared;
38635 +};
38636 +
38637 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38638 +                            struct au_xino_do_set_br *args)
38639 +{
38640 +       int err;
38641 +       struct au_xi_calc calc;
38642 +       struct file *file;
38643 +       struct au_branch *br;
38644 +       struct au_xi_new xinew = {
38645 +               .base = path
38646 +       };
38647 +
38648 +       br = args->br;
38649 +       xinew.xi = br->br_xino;
38650 +       au_xi_calc(sb, args->h_ino, &calc);
38651 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38652 +       if (args->bshared >= 0)
38653 +               /* shared xino */
38654 +               au_xino_set_br_shared(sb, br, args->bshared);
38655 +       else if (!xinew.xi) {
38656 +               /* new xino */
38657 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38658 +               if (unlikely(err))
38659 +                       goto out;
38660 +       }
38661 +
38662 +       /* force re-creating */
38663 +       xinew.xi = br->br_xino;
38664 +       xinew.idx = calc.idx;
38665 +       mutex_lock(&xinew.xi->xi_mtx);
38666 +       file = au_xi_new(sb, &xinew);
38667 +       mutex_unlock(&xinew.xi->xi_mtx);
38668 +       err = PTR_ERR(file);
38669 +       if (IS_ERR(file))
38670 +               goto out;
38671 +       AuDebugOn(!file);
38672 +
38673 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38674 +       if (unlikely(err))
38675 +               au_xino_put(br);
38676 +
38677 +out:
38678 +       AuTraceErr(err);
38679 +       return err;
38680 +}
38681 +
38682 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38683 +{
38684 +       int err;
38685 +       aufs_bindex_t bindex, bbot;
38686 +       struct au_xino_do_set_br args;
38687 +       struct inode *inode;
38688 +
38689 +       SiMustWriteLock(sb);
38690 +
38691 +       bbot = au_sbbot(sb);
38692 +       inode = d_inode(sb->s_root);
38693 +       for (bindex = 0; bindex <= bbot; bindex++) {
38694 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38695 +               args.br = au_sbr(sb, bindex);
38696 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38697 +               err = au_xino_do_set_br(sb, path, &args);
38698 +               if (unlikely(err))
38699 +                       break;
38700 +       }
38701 +
38702 +       AuTraceErr(err);
38703 +       return err;
38704 +}
38705 +
38706 +void au_xino_clr(struct super_block *sb)
38707 +{
38708 +       struct au_sbinfo *sbinfo;
38709 +
38710 +       au_xigen_clr(sb);
38711 +       xino_clear_xib(sb);
38712 +       xino_clear_br(sb);
38713 +       dbgaufs_brs_del(sb, 0);
38714 +       sbinfo = au_sbi(sb);
38715 +       /* lvalue, do not call au_mntflags() */
38716 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38717 +}
38718 +
38719 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38720 +{
38721 +       int err, skip;
38722 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38723 +       struct qstr *dname, *cur_name;
38724 +       struct file *cur_xino;
38725 +       struct au_sbinfo *sbinfo;
38726 +       struct path *path, *cur_path;
38727 +
38728 +       SiMustWriteLock(sb);
38729 +
38730 +       err = 0;
38731 +       sbinfo = au_sbi(sb);
38732 +       path = &xiopt->file->f_path;
38733 +       dentry = path->dentry;
38734 +       parent = dget_parent(dentry);
38735 +       if (remount) {
38736 +               skip = 0;
38737 +               cur_xino = sbinfo->si_xib;
38738 +               if (cur_xino) {
38739 +                       cur_path = &cur_xino->f_path;
38740 +                       cur_dentry = cur_path->dentry;
38741 +                       cur_parent = dget_parent(cur_dentry);
38742 +                       cur_name = &cur_dentry->d_name;
38743 +                       dname = &dentry->d_name;
38744 +                       skip = (cur_parent == parent
38745 +                               && au_qstreq(dname, cur_name));
38746 +                       dput(cur_parent);
38747 +               }
38748 +               if (skip)
38749 +                       goto out;
38750 +       }
38751 +
38752 +       au_opt_set(sbinfo->si_mntflags, XINO);
38753 +       err = au_xino_set_xib(sb, path);
38754 +       /* si_x{read,write} are set */
38755 +       if (!err)
38756 +               err = au_xigen_set(sb, path);
38757 +       if (!err)
38758 +               err = au_xino_set_br(sb, path);
38759 +       if (!err) {
38760 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38761 +               goto out; /* success */
38762 +       }
38763 +
38764 +       /* reset all */
38765 +       AuIOErr("failed setting xino(%d).\n", err);
38766 +       au_xino_clr(sb);
38767 +
38768 +out:
38769 +       dput(parent);
38770 +       return err;
38771 +}
38772 +
38773 +/*
38774 + * create a xinofile at the default place/path.
38775 + */
38776 +struct file *au_xino_def(struct super_block *sb)
38777 +{
38778 +       struct file *file;
38779 +       char *page, *p;
38780 +       struct au_branch *br;
38781 +       struct super_block *h_sb;
38782 +       struct path path;
38783 +       aufs_bindex_t bbot, bindex, bwr;
38784 +
38785 +       br = NULL;
38786 +       bbot = au_sbbot(sb);
38787 +       bwr = -1;
38788 +       for (bindex = 0; bindex <= bbot; bindex++) {
38789 +               br = au_sbr(sb, bindex);
38790 +               if (au_br_writable(br->br_perm)
38791 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38792 +                       bwr = bindex;
38793 +                       break;
38794 +               }
38795 +       }
38796 +
38797 +       if (bwr >= 0) {
38798 +               file = ERR_PTR(-ENOMEM);
38799 +               page = (void *)__get_free_page(GFP_NOFS);
38800 +               if (unlikely(!page))
38801 +                       goto out;
38802 +               path.mnt = au_br_mnt(br);
38803 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38804 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38805 +               file = (void *)p;
38806 +               if (!IS_ERR(p)) {
38807 +                       strcat(p, "/" AUFS_XINO_FNAME);
38808 +                       AuDbg("%s\n", p);
38809 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38810 +               }
38811 +               free_page((unsigned long)page);
38812 +       } else {
38813 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38814 +                                     /*wbrtop*/0);
38815 +               if (IS_ERR(file))
38816 +                       goto out;
38817 +               h_sb = file->f_path.dentry->d_sb;
38818 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38819 +                       pr_err("xino doesn't support %s(%s)\n",
38820 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38821 +                       fput(file);
38822 +                       file = ERR_PTR(-EINVAL);
38823 +               }
38824 +       }
38825 +
38826 +out:
38827 +       return file;
38828 +}
38829 +
38830 +/* ---------------------------------------------------------------------- */
38831 +
38832 +/*
38833 + * initialize the xinofile for the specified branch @br
38834 + * at the place/path where @base_file indicates.
38835 + * test whether another branch is on the same filesystem or not,
38836 + * if found then share the xinofile with another branch.
38837 + */
38838 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38839 +                   struct path *base)
38840 +{
38841 +       int err;
38842 +       struct au_xino_do_set_br args = {
38843 +               .h_ino  = h_ino,
38844 +               .br     = br
38845 +       };
38846 +
38847 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38848 +                                      au_br_sb(br));
38849 +       err = au_xino_do_set_br(sb, base, &args);
38850 +       if (unlikely(err))
38851 +               au_xino_put(br);
38852 +
38853 +       return err;
38854 +}
38855 +
38856 +/* ---------------------------------------------------------------------- */
38857 +
38858 +/*
38859 + * get an unused inode number from bitmap
38860 + */
38861 +ino_t au_xino_new_ino(struct super_block *sb)
38862 +{
38863 +       ino_t ino;
38864 +       unsigned long *p, pindex, ul, pend;
38865 +       struct au_sbinfo *sbinfo;
38866 +       struct file *file;
38867 +       int free_bit, err;
38868 +
38869 +       if (!au_opt_test(au_mntflags(sb), XINO))
38870 +               return iunique(sb, AUFS_FIRST_INO);
38871 +
38872 +       sbinfo = au_sbi(sb);
38873 +       mutex_lock(&sbinfo->si_xib_mtx);
38874 +       p = sbinfo->si_xib_buf;
38875 +       free_bit = sbinfo->si_xib_next_bit;
38876 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38877 +               goto out; /* success */
38878 +       free_bit = find_first_zero_bit(p, page_bits);
38879 +       if (free_bit < page_bits)
38880 +               goto out; /* success */
38881 +
38882 +       pindex = sbinfo->si_xib_last_pindex;
38883 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38884 +               err = xib_pindex(sb, ul);
38885 +               if (unlikely(err))
38886 +                       goto out_err;
38887 +               free_bit = find_first_zero_bit(p, page_bits);
38888 +               if (free_bit < page_bits)
38889 +                       goto out; /* success */
38890 +       }
38891 +
38892 +       file = sbinfo->si_xib;
38893 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38894 +       for (ul = pindex + 1; ul <= pend; ul++) {
38895 +               err = xib_pindex(sb, ul);
38896 +               if (unlikely(err))
38897 +                       goto out_err;
38898 +               free_bit = find_first_zero_bit(p, page_bits);
38899 +               if (free_bit < page_bits)
38900 +                       goto out; /* success */
38901 +       }
38902 +       BUG();
38903 +
38904 +out:
38905 +       set_bit(free_bit, p);
38906 +       sbinfo->si_xib_next_bit = free_bit + 1;
38907 +       pindex = sbinfo->si_xib_last_pindex;
38908 +       mutex_unlock(&sbinfo->si_xib_mtx);
38909 +       ino = xib_calc_ino(pindex, free_bit);
38910 +       AuDbg("i%lu\n", (unsigned long)ino);
38911 +       return ino;
38912 +out_err:
38913 +       mutex_unlock(&sbinfo->si_xib_mtx);
38914 +       AuDbg("i0\n");
38915 +       return 0;
38916 +}
38917 +
38918 +/* for s_op->delete_inode() */
38919 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38920 +{
38921 +       int err;
38922 +       unsigned int mnt_flags;
38923 +       aufs_bindex_t bindex, bbot, bi;
38924 +       unsigned char try_trunc;
38925 +       struct au_iinfo *iinfo;
38926 +       struct super_block *sb;
38927 +       struct au_hinode *hi;
38928 +       struct inode *h_inode;
38929 +       struct au_branch *br;
38930 +       struct au_xi_calc calc;
38931 +       struct file *file;
38932 +
38933 +       AuDebugOn(au_is_bad_inode(inode));
38934 +
38935 +       sb = inode->i_sb;
38936 +       mnt_flags = au_mntflags(sb);
38937 +       if (!au_opt_test(mnt_flags, XINO)
38938 +           || inode->i_ino == AUFS_ROOT_INO)
38939 +               return;
38940 +
38941 +       if (unlinked) {
38942 +               au_xigen_inc(inode);
38943 +               au_xib_clear_bit(inode);
38944 +       }
38945 +
38946 +       iinfo = au_ii(inode);
38947 +       bindex = iinfo->ii_btop;
38948 +       if (bindex < 0)
38949 +               return;
38950 +
38951 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38952 +       hi = au_hinode(iinfo, bindex);
38953 +       bbot = iinfo->ii_bbot;
38954 +       for (; bindex <= bbot; bindex++, hi++) {
38955 +               h_inode = hi->hi_inode;
38956 +               if (!h_inode
38957 +                   || (!unlinked && h_inode->i_nlink))
38958 +                       continue;
38959 +
38960 +               /* inode may not be revalidated */
38961 +               bi = au_br_index(sb, hi->hi_id);
38962 +               if (bi < 0)
38963 +                       continue;
38964 +
38965 +               br = au_sbr(sb, bi);
38966 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38967 +               file = au_xino_file(br->br_xino, calc.idx);
38968 +               if (IS_ERR_OR_NULL(file))
38969 +                       continue;
38970 +
38971 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38972 +               if (!err && try_trunc
38973 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38974 +                       xino_try_trunc(sb, br);
38975 +       }
38976 +}
38977 +
38978 +/* ---------------------------------------------------------------------- */
38979 +
38980 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38981 +{
38982 +       int found, total, i;
38983 +
38984 +       found = -1;
38985 +       total = xi->xi_nondir.total;
38986 +       for (i = 0; i < total; i++) {
38987 +               if (xi->xi_nondir.array[i] != h_ino)
38988 +                       continue;
38989 +               found = i;
38990 +               break;
38991 +       }
38992 +
38993 +       return found;
38994 +}
38995 +
38996 +static int au_xinondir_expand(struct au_xino *xi)
38997 +{
38998 +       int err, sz;
38999 +       ino_t *p;
39000 +
39001 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
39002 +
39003 +       err = -ENOMEM;
39004 +       sz = xi->xi_nondir.total * sizeof(ino_t);
39005 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
39006 +               goto out;
39007 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
39008 +                        /*may_shrink*/0);
39009 +       if (p) {
39010 +               xi->xi_nondir.array = p;
39011 +               xi->xi_nondir.total <<= 1;
39012 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
39013 +               err = 0;
39014 +       }
39015 +
39016 +out:
39017 +       return err;
39018 +}
39019 +
39020 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
39021 +                      ino_t h_ino, int idx)
39022 +{
39023 +       struct au_xino *xi;
39024 +
39025 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
39026 +       xi = au_sbr(sb, bindex)->br_xino;
39027 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
39028 +
39029 +       spin_lock(&xi->xi_nondir.spin);
39030 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
39031 +       xi->xi_nondir.array[idx] = 0;
39032 +       spin_unlock(&xi->xi_nondir.spin);
39033 +       wake_up_all(&xi->xi_nondir.wqh);
39034 +}
39035 +
39036 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
39037 +                     int *idx)
39038 +{
39039 +       int err, found, empty;
39040 +       struct au_xino *xi;
39041 +
39042 +       err = 0;
39043 +       *idx = -1;
39044 +       if (!au_opt_test(au_mntflags(sb), XINO))
39045 +               goto out; /* no xino */
39046 +
39047 +       xi = au_sbr(sb, bindex)->br_xino;
39048 +
39049 +again:
39050 +       spin_lock(&xi->xi_nondir.spin);
39051 +       found = au_xinondir_find(xi, h_ino);
39052 +       if (found == -1) {
39053 +               empty = au_xinondir_find(xi, /*h_ino*/0);
39054 +               if (empty == -1) {
39055 +                       empty = xi->xi_nondir.total;
39056 +                       err = au_xinondir_expand(xi);
39057 +                       if (unlikely(err))
39058 +                               goto out_unlock;
39059 +               }
39060 +               xi->xi_nondir.array[empty] = h_ino;
39061 +               *idx = empty;
39062 +       } else {
39063 +               spin_unlock(&xi->xi_nondir.spin);
39064 +               wait_event(xi->xi_nondir.wqh,
39065 +                          xi->xi_nondir.array[found] != h_ino);
39066 +               goto again;
39067 +       }
39068 +
39069 +out_unlock:
39070 +       spin_unlock(&xi->xi_nondir.spin);
39071 +out:
39072 +       return err;
39073 +}
39074 +
39075 +/* ---------------------------------------------------------------------- */
39076 +
39077 +int au_xino_path(struct seq_file *seq, struct file *file)
39078 +{
39079 +       int err;
39080 +
39081 +       err = au_seq_path(seq, &file->f_path);
39082 +       if (unlikely(err))
39083 +               goto out;
39084 +
39085 +#define Deleted "\\040(deleted)"
39086 +       seq->count -= sizeof(Deleted) - 1;
39087 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
39088 +                        sizeof(Deleted) - 1));
39089 +#undef Deleted
39090 +
39091 +out:
39092 +       return err;
39093 +}
39094 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
39095 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
39096 +++ linux/include/uapi/linux/aufs_type.h        2024-05-13 17:13:00.624737645 +0200
39097 @@ -0,0 +1,452 @@
39098 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
39099 +/*
39100 + * Copyright (C) 2005-2022 Junjiro R. Okajima
39101 + *
39102 + * This program is free software; you can redistribute it and/or modify
39103 + * it under the terms of the GNU General Public License as published by
39104 + * the Free Software Foundation; either version 2 of the License, or
39105 + * (at your option) any later version.
39106 + *
39107 + * This program is distributed in the hope that it will be useful,
39108 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
39109 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39110 + * GNU General Public License for more details.
39111 + *
39112 + * You should have received a copy of the GNU General Public License
39113 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
39114 + */
39115 +
39116 +#ifndef __AUFS_TYPE_H__
39117 +#define __AUFS_TYPE_H__
39118 +
39119 +#define AUFS_NAME      "aufs"
39120 +
39121 +#ifdef __KERNEL__
39122 +/*
39123 + * define it before including all other headers.
39124 + * sched.h may use pr_* macros before defining "current", so define the
39125 + * no-current version first, and re-define later.
39126 + */
39127 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39128 +#include <linux/sched.h>
39129 +#undef pr_fmt
39130 +#define pr_fmt(fmt) \
39131 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39132 +               (int)sizeof(current->comm), current->comm, current->pid
39133 +#include <linux/limits.h>
39134 +#else
39135 +#include <stdint.h>
39136 +#include <sys/types.h>
39137 +#include <limits.h>
39138 +#endif /* __KERNEL__ */
39139 +
39140 +#define AUFS_VERSION   "6.x-rcN-20240401"
39141 +
39142 +/* todo? move this to linux-2.6.19/include/magic.h */
39143 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39144 +
39145 +/* ---------------------------------------------------------------------- */
39146 +
39147 +#ifdef __KERNEL__
39148 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
39149 +typedef int8_t aufs_bindex_t;
39150 +#define AUFS_BRANCH_MAX 127
39151 +#else
39152 +typedef int16_t aufs_bindex_t;
39153 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
39154 +#define AUFS_BRANCH_MAX 511
39155 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39156 +#define AUFS_BRANCH_MAX 1023
39157 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39158 +#define AUFS_BRANCH_MAX 32767
39159 +#endif
39160 +#endif
39161 +
39162 +#ifndef AUFS_BRANCH_MAX
39163 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39164 +#endif
39165 +#endif /* __KERNEL__ */
39166 +
39167 +/* ---------------------------------------------------------------------- */
39168 +
39169 +#define AUFS_FSTYPE            AUFS_NAME
39170 +
39171 +#define AUFS_ROOT_INO          2
39172 +#define AUFS_FIRST_INO         11
39173 +
39174 +#define AUFS_WH_PFX            ".wh."
39175 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
39176 +#define AUFS_WH_TMP_LEN                4
39177 +/* a limit for rmdir/rename a dir and copyup */
39178 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
39179 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
39180 +                               - 1                     /* dot */\
39181 +                               - AUFS_WH_TMP_LEN)      /* hex */
39182 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
39183 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
39184 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
39185 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
39186 +#define AUFS_DIRWH_DEF         3
39187 +#define AUFS_RDCACHE_DEF       10 /* seconds */
39188 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
39189 +#define AUFS_RDBLK_DEF         512 /* bytes */
39190 +#define AUFS_RDHASH_DEF                32
39191 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
39192 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
39193 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
39194 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
39195 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
39196 +
39197 +/* pseudo-link maintenace under /proc */
39198 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
39199 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
39200 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39201 +
39202 +/* dirren, renamed dir */
39203 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
39204 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
39205 +/* whiteouted doubly */
39206 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
39207 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39208 +
39209 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39210 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39211 +
39212 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39213 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39214 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39215 +
39216 +/* doubly whiteouted */
39217 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39218 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39219 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39220 +
39221 +/* branch permissions and attributes */
39222 +#define AUFS_BRPERM_RW         "rw"
39223 +#define AUFS_BRPERM_RO         "ro"
39224 +#define AUFS_BRPERM_RR         "rr"
39225 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39226 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39227 +#define AUFS_BRATTR_FHSM       "fhsm"
39228 +#define AUFS_BRATTR_UNPIN      "unpin"
39229 +#define AUFS_BRATTR_ICEX       "icex"
39230 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39231 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39232 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39233 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39234 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39235 +#define AUFS_BRRATTR_WH                "wh"
39236 +#define AUFS_BRWATTR_NLWH      "nolwh"
39237 +#define AUFS_BRWATTR_MOO       "moo"
39238 +
39239 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39240 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39241 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39242 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39243 +
39244 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39245 +#define AuBrAttr_COO_ALL       (1 << 4)
39246 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39247 +
39248 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39249 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39250 +                                                  branch. meaningless since
39251 +                                                  linux-3.18-rc1 */
39252 +
39253 +/* ignore error in copying XATTR */
39254 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39255 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39256 +#define AuBrAttr_ICEX_TR       (1 << 9)
39257 +#define AuBrAttr_ICEX_USR      (1 << 10)
39258 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39259 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39260 +                                | AuBrAttr_ICEX_SYS    \
39261 +                                | AuBrAttr_ICEX_TR     \
39262 +                                | AuBrAttr_ICEX_USR    \
39263 +                                | AuBrAttr_ICEX_OTH)
39264 +
39265 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39266 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39267 +
39268 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39269 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39270 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39271 +
39272 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39273 +
39274 +/* #warning test userspace */
39275 +#ifdef __KERNEL__
39276 +#ifndef CONFIG_AUFS_FHSM
39277 +#undef AuBrAttr_FHSM
39278 +#define AuBrAttr_FHSM          0
39279 +#endif
39280 +#ifndef CONFIG_AUFS_XATTR
39281 +#undef AuBrAttr_ICEX
39282 +#define AuBrAttr_ICEX          0
39283 +#undef AuBrAttr_ICEX_SEC
39284 +#define AuBrAttr_ICEX_SEC      0
39285 +#undef AuBrAttr_ICEX_SYS
39286 +#define AuBrAttr_ICEX_SYS      0
39287 +#undef AuBrAttr_ICEX_TR
39288 +#define AuBrAttr_ICEX_TR       0
39289 +#undef AuBrAttr_ICEX_USR
39290 +#define AuBrAttr_ICEX_USR      0
39291 +#undef AuBrAttr_ICEX_OTH
39292 +#define AuBrAttr_ICEX_OTH      0
39293 +#endif
39294 +#endif
39295 +
39296 +/* the longest combination */
39297 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39298 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39299 +                              "+" AUFS_BRATTR_COO_REG          \
39300 +                              "+" AUFS_BRATTR_FHSM             \
39301 +                              "+" AUFS_BRATTR_UNPIN            \
39302 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39303 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39304 +                              "+" AUFS_BRATTR_ICEX_USR         \
39305 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39306 +                              "+" AUFS_BRWATTR_NLWH)
39307 +
39308 +typedef struct {
39309 +       char a[AuBrPermStrSz];
39310 +} au_br_perm_str_t;
39311 +
39312 +static inline int au_br_writable(int brperm)
39313 +{
39314 +       return brperm & AuBrPerm_RW;
39315 +}
39316 +
39317 +static inline int au_br_whable(int brperm)
39318 +{
39319 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39320 +}
39321 +
39322 +static inline int au_br_wh_linkable(int brperm)
39323 +{
39324 +       return !(brperm & AuBrWAttr_NoLinkWH);
39325 +}
39326 +
39327 +static inline int au_br_cmoo(int brperm)
39328 +{
39329 +       return brperm & AuBrAttr_CMOO_Mask;
39330 +}
39331 +
39332 +static inline int au_br_fhsm(int brperm)
39333 +{
39334 +       return brperm & AuBrAttr_FHSM;
39335 +}
39336 +
39337 +/* ---------------------------------------------------------------------- */
39338 +
39339 +/* ioctl */
39340 +enum {
39341 +       /* readdir in userspace */
39342 +       AuCtl_RDU,
39343 +       AuCtl_RDU_INO,
39344 +
39345 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39346 +       AuCtl_IBUSY,    /* busy inode */
39347 +       AuCtl_MVDOWN,   /* move-down */
39348 +       AuCtl_BR,       /* info about branches */
39349 +       AuCtl_FHSM_FD   /* connection for fhsm */
39350 +};
39351 +
39352 +/* borrowed from linux/include/linux/kernel.h */
39353 +#ifndef ALIGN
39354 +#ifdef _GNU_SOURCE
39355 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39356 +#else
39357 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39358 +#endif
39359 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39360 +#endif
39361 +
39362 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39363 +#ifndef __aligned
39364 +#define __aligned(x)                   __attribute__((aligned(x)))
39365 +#endif
39366 +
39367 +#ifdef __KERNEL__
39368 +#ifndef __packed
39369 +#define __packed                       __attribute__((packed))
39370 +#endif
39371 +#endif
39372 +
39373 +struct au_rdu_cookie {
39374 +       uint64_t        h_pos;
39375 +       int16_t         bindex;
39376 +       uint8_t         flags;
39377 +       uint8_t         pad;
39378 +       uint32_t        generation;
39379 +} __aligned(8);
39380 +
39381 +struct au_rdu_ent {
39382 +       uint64_t        ino;
39383 +       int16_t         bindex;
39384 +       uint8_t         type;
39385 +       uint8_t         nlen;
39386 +       uint8_t         wh;
39387 +       char            name[];
39388 +} __aligned(8);
39389 +
39390 +static inline int au_rdu_len(int nlen)
39391 +{
39392 +       /* include the terminating NULL */
39393 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39394 +                    sizeof(uint64_t));
39395 +}
39396 +
39397 +union au_rdu_ent_ul {
39398 +       struct au_rdu_ent __user        *e;
39399 +       uint64_t                        ul;
39400 +};
39401 +
39402 +enum {
39403 +       AufsCtlRduV_SZ,
39404 +       AufsCtlRduV_End
39405 +};
39406 +
39407 +struct aufs_rdu {
39408 +       /* input */
39409 +       union {
39410 +               uint64_t        sz;     /* AuCtl_RDU */
39411 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39412 +       };
39413 +       union au_rdu_ent_ul     ent;
39414 +       uint16_t                verify[AufsCtlRduV_End];
39415 +
39416 +       /* input/output */
39417 +       uint32_t                blk;
39418 +
39419 +       /* output */
39420 +       union au_rdu_ent_ul     tail;
39421 +       /* number of entries which were added in a single call */
39422 +       uint64_t                rent;
39423 +       uint8_t                 full;
39424 +       uint8_t                 shwh;
39425 +
39426 +       struct au_rdu_cookie    cookie;
39427 +} __aligned(8);
39428 +
39429 +/* ---------------------------------------------------------------------- */
39430 +
39431 +/* dirren. the branch is identified by the filename who contains this */
39432 +struct au_drinfo {
39433 +       uint64_t ino;
39434 +       union {
39435 +               uint8_t oldnamelen;
39436 +               uint64_t _padding;
39437 +       };
39438 +       uint8_t oldname[];
39439 +} __aligned(8);
39440 +
39441 +struct au_drinfo_fdata {
39442 +       uint32_t magic;
39443 +       struct au_drinfo drinfo;
39444 +} __aligned(8);
39445 +
39446 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39447 +/* future */
39448 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39449 +
39450 +/* ---------------------------------------------------------------------- */
39451 +
39452 +struct aufs_wbr_fd {
39453 +       uint32_t        oflags;
39454 +       int16_t         brid;
39455 +} __aligned(8);
39456 +
39457 +/* ---------------------------------------------------------------------- */
39458 +
39459 +struct aufs_ibusy {
39460 +       uint64_t        ino, h_ino;
39461 +       int16_t         bindex;
39462 +} __aligned(8);
39463 +
39464 +/* ---------------------------------------------------------------------- */
39465 +
39466 +/* error code for move-down */
39467 +/* the actual message strings are implemented in aufs-util.git */
39468 +enum {
39469 +       EAU_MVDOWN_OPAQUE = 1,
39470 +       EAU_MVDOWN_WHITEOUT,
39471 +       EAU_MVDOWN_UPPER,
39472 +       EAU_MVDOWN_BOTTOM,
39473 +       EAU_MVDOWN_NOUPPER,
39474 +       EAU_MVDOWN_NOLOWERBR,
39475 +       EAU_Last
39476 +};
39477 +
39478 +/* flags for move-down */
39479 +#define AUFS_MVDOWN_DMSG       1
39480 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39481 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39482 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39483 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39484 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39485 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39486 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39487 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39488 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39489 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39490 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39491 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39492 +
39493 +/* index for move-down */
39494 +enum {
39495 +       AUFS_MVDOWN_UPPER,
39496 +       AUFS_MVDOWN_LOWER,
39497 +       AUFS_MVDOWN_NARRAY
39498 +};
39499 +
39500 +/*
39501 + * additional info of move-down
39502 + * number of free blocks and inodes.
39503 + * subset of struct kstatfs, but smaller and always 64bit.
39504 + */
39505 +struct aufs_stfs {
39506 +       uint64_t        f_blocks;
39507 +       uint64_t        f_bavail;
39508 +       uint64_t        f_files;
39509 +       uint64_t        f_ffree;
39510 +};
39511 +
39512 +struct aufs_stbr {
39513 +       int16_t                 brid;   /* optional input */
39514 +       int16_t                 bindex; /* output */
39515 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39516 +} __aligned(8);
39517 +
39518 +struct aufs_mvdown {
39519 +       uint32_t                flags;                  /* input/output */
39520 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39521 +       int8_t                  au_errno;               /* output */
39522 +} __aligned(8);
39523 +
39524 +/* ---------------------------------------------------------------------- */
39525 +
39526 +union aufs_brinfo {
39527 +       /* PATH_MAX may differ between kernel-space and user-space */
39528 +       char    _spacer[4096];
39529 +       struct {
39530 +               int16_t id;
39531 +               int     perm;
39532 +               char    path[];
39533 +       };
39534 +} __aligned(8);
39535 +
39536 +/* ---------------------------------------------------------------------- */
39537 +
39538 +#define AuCtlType              'A'
39539 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39540 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39541 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39542 +                                    struct aufs_wbr_fd)
39543 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39544 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39545 +                                     struct aufs_mvdown)
39546 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39547 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39548 +
39549 +#endif /* __AUFS_TYPE_H__ */
39550 SPDX-License-Identifier: GPL-2.0
39551 aufs6.x-rcN loopback patch
39552
39553 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39554 index 6bd18fb1e53a..a93fab1797e2 100644
39555 --- a/drivers/block/loop.c
39556 +++ b/drivers/block/loop.c
39557 @@ -54,7 +54,7 @@ struct loop_device {
39558         int             lo_flags;
39559         char            lo_file_name[LO_NAME_SIZE];
39560  
39561 -       struct file *   lo_backing_file;
39562 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39563         struct block_device *lo_device;
39564  
39565         gfp_t           old_gfp_mask;
39566 @@ -506,6 +506,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39567                                 lo->use_dio);
39568  }
39569  
39570 +static struct file *loop_real_file(struct file *file)
39571 +{
39572 +       struct file *f = NULL;
39573 +
39574 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39575 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39576 +       return f;
39577 +}
39578 +
39579  static void loop_reread_partitions(struct loop_device *lo)
39580  {
39581         int rc;
39582 @@ -563,6 +572,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39583  {
39584         struct file *file = fget(arg);
39585         struct file *old_file;
39586 +       struct file *f, *virt_file = NULL, *old_virt_file;
39587         int error;
39588         bool partscan;
39589         bool is_loop;
39590 @@ -586,11 +596,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39591         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39592                 goto out_err;
39593  
39594 +       f = loop_real_file(file);
39595 +       if (f) {
39596 +               virt_file = file;
39597 +               file = f;
39598 +               get_file(file);
39599 +       }
39600 +
39601         error = loop_validate_file(file, bdev);
39602         if (error)
39603                 goto out_err;
39604  
39605         old_file = lo->lo_backing_file;
39606 +       old_virt_file = lo->lo_backing_virt_file;
39607  
39608         error = -EINVAL;
39609  
39610 @@ -603,6 +621,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39611         blk_mq_freeze_queue(lo->lo_queue);
39612         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39613         lo->lo_backing_file = file;
39614 +       lo->lo_backing_virt_file = virt_file;
39615         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39616         mapping_set_gfp_mask(file->f_mapping,
39617                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39618 @@ -625,6 +644,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39619          * dependency.
39620          */
39621         fput(old_file);
39622 +       if (old_virt_file)
39623 +               fput(old_virt_file);
39624         if (partscan)
39625                 loop_reread_partitions(lo);
39626  
39627 @@ -638,6 +659,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39628         loop_global_unlock(lo, is_loop);
39629  out_putf:
39630         fput(file);
39631 +       if (virt_file)
39632 +               fput(virt_file);
39633         goto done;
39634  }
39635  
39636 @@ -1014,6 +1037,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39637                           const struct loop_config *config)
39638  {
39639         struct file *file = fget(config->fd);
39640 +       struct file *f, *virt_file = NULL;
39641         struct inode *inode;
39642         struct address_space *mapping;
39643         int error;
39644 @@ -1029,6 +1053,13 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39645         /* This is safe, since we have a reference from open(). */
39646         __module_get(THIS_MODULE);
39647  
39648 +       f = loop_real_file(file);
39649 +       if (f) {
39650 +               virt_file = file;
39651 +               file = f;
39652 +               get_file(file);
39653 +       }
39654 +
39655         /*
39656          * If we don't hold exclusive handle for the device, upgrade to it
39657          * here to avoid changing device under exclusive owner.
39658 @@ -1092,6 +1123,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39659         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39660         lo->lo_device = bdev;
39661         lo->lo_backing_file = file;
39662 +       lo->lo_backing_virt_file = virt_file;
39663         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39664         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39665  
39666 @@ -1146,6 +1178,8 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39667                 bd_abort_claiming(bdev, loop_configure);
39668  out_putf:
39669         fput(file);
39670 +       if (virt_file)
39671 +               fput(virt_file);
39672         /* This is safe: open() is still holding a reference. */
39673         module_put(THIS_MODULE);
39674         return error;
39675 @@ -1154,6 +1188,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39676  static void __loop_clr_fd(struct loop_device *lo, bool release)
39677  {
39678         struct file *filp;
39679 +       struct file *virt_filp = lo->lo_backing_virt_file;
39680         gfp_t gfp = lo->old_gfp_mask;
39681  
39682         if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
39683 @@ -1170,6 +1205,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39684         spin_lock_irq(&lo->lo_lock);
39685         filp = lo->lo_backing_file;
39686         lo->lo_backing_file = NULL;
39687 +       lo->lo_backing_virt_file = NULL;
39688         spin_unlock_irq(&lo->lo_lock);
39689  
39690         lo->lo_device = NULL;
39691 @@ -1230,6 +1266,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39692          * fput can take open_mutex which is usually taken before lo_mutex.
39693          */
39694         fput(filp);
39695 +       if (virt_filp)
39696 +               fput(virt_filp);
39697  }
39698  
39699  static int loop_clr_fd(struct loop_device *lo)
39700 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39701 index fa8a517ffd0c..c18f7bcef81b 100644
39702 --- a/fs/aufs/f_op.c
39703 +++ b/fs/aufs/f_op.c
39704 @@ -311,7 +311,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39705         if (IS_ERR(h_file))
39706                 goto out;
39707  
39708 -       if (au_test_loopback_kthread()) {
39709 +       if (0 && au_test_loopback_kthread()) {
39710                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39711                 if (file->f_mapping != h_file->f_mapping) {
39712                         file->f_mapping = h_file->f_mapping;
39713 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39714 index 58043e31e5f3..e2bfae6f9d59 100644
39715 --- a/fs/aufs/loop.c
39716 +++ b/fs/aufs/loop.c
39717 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39718                 symbol_put(loop_backing_file);
39719         au_kfree_try_rcu(au_warn_loopback_array);
39720  }
39721 +
39722 +/* ---------------------------------------------------------------------- */
39723 +
39724 +/* support the loopback block device insude aufs */
39725 +
39726 +struct file *aufs_real_loop(struct file *file)
39727 +{
39728 +       struct file *f;
39729 +
39730 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39731 +       fi_read_lock(file);
39732 +       f = au_hf_top(file);
39733 +       fi_read_unlock(file);
39734 +       AuDebugOn(!f);
39735 +       return f;
39736 +}
39737 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39738 index 03d4908a6c03..34d356e181d5 100644
39739 --- a/fs/aufs/loop.h
39740 +++ b/fs/aufs/loop.h
39741 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39742  
39743  int au_loopback_init(void);
39744  void au_loopback_fin(void);
39745 +
39746 +struct file *aufs_real_loop(struct file *file);
39747  #else
39748  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39749  
39750 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39751  
39752  AuStubInt0(au_loopback_init, void)
39753  AuStubVoid(au_loopback_fin, void)
39754 +
39755 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39756  #endif /* BLK_DEV_LOOP */
39757  
39758  #endif /* __KERNEL__ */
39759 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39760 index 07d3412e950f..c4a00f620e57 100644
39761 --- a/fs/aufs/super.c
39762 +++ b/fs/aufs/super.c
39763 @@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39764         .show_options   = aufs_show_options,
39765         .statfs         = aufs_statfs,
39766         .put_super      = aufs_put_super,
39767 -       .sync_fs        = aufs_sync_fs
39768 +       .sync_fs        = aufs_sync_fs,
39769 +#ifdef CONFIG_AUFS_BDEV_LOOP
39770 +       .real_loop      = aufs_real_loop
39771 +#endif
39772  };
39773  
39774  /* ---------------------------------------------------------------------- */
39775 diff --git a/include/linux/fs.h b/include/linux/fs.h
39776 index b3a73aed62b8..97aa5e94fa31 100644
39777 --- a/include/linux/fs.h
39778 +++ b/include/linux/fs.h
39779 @@ -2188,6 +2188,11 @@ struct super_operations {
39780         long (*free_cached_objects)(struct super_block *,
39781                                     struct shrink_control *);
39782         void (*shutdown)(struct super_block *sb);
39783 +
39784 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39785 +       /* and aufs */
39786 +       struct file *(*real_loop)(struct file *);
39787 +#endif
39788  };
39789  
39790  /*
This page took 3.200305 seconds and 4 git commands to generate.