]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs.patch
drop ia64 support following upstream removal
[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 aa7e03cc1941..bf780967b6c4 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -331,6 +331,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 f9541f40be4e..3a0e13ee39e7 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 bf0f54c24f81..39b1844bce57 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3316,6 +3316,19 @@ F:       include/uapi/linux/audit.h
33  F:     kernel/audit*
34  F:     lib/*audit.c
35  
36 +AUFS (advanced multi layered unification filesystem) FILESYSTEM
37 +M:     "J. R. Okajima" <hooanon05g@gmail.com>
38 +L:     aufs-users@lists.sourceforge.net (members only)
39 +L:     linux-unionfs@vger.kernel.org
40 +S:     Supported
41 +W:     http://aufs.sourceforge.net
42 +T:     git://github.com/sfjro/aufs4-linux.git
43 +F:     Documentation/ABI/testing/debugfs-aufs
44 +F:     Documentation/ABI/testing/sysfs-aufs
45 +F:     Documentation/filesystems/aufs/
46 +F:     fs/aufs/
47 +F:     include/uapi/linux/aufs_type.h
48 +
49  AUXILIARY 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 9f2d412fc560..1fefc6a8d049 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -645,6 +645,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
57         goto done;
58  }
59  
60 +/*
61 + * for AUFS
62 + * no get/put for file.
63 + */
64 +struct file *loop_backing_file(struct super_block *sb)
65 +{
66 +       struct file *ret;
67 +       struct loop_device *l;
68 +
69 +       ret = NULL;
70 +       if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
71 +               l = sb->s_bdev->bd_disk->private_data;
72 +               ret = l->lo_backing_file;
73 +       }
74 +       return ret;
75 +}
76 +EXPORT_SYMBOL_GPL(loop_backing_file);
77 +
78  /* loop sysfs attributes */
79  
80  static ssize_t loop_attr_show(struct device *dev, char *page,
81 diff --git a/fs/dcache.c b/fs/dcache.c
82 index 25ac74d30bff..6c930ceed526 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1345,7 +1345,7 @@ enum d_walk_ret {
86   *
87   * The @enter() callbacks are called with d_lock held.
88   */
89 -static void d_walk(struct dentry *parent, void *data,
90 +void d_walk(struct dentry *parent, void *data,
91                    enum d_walk_ret (*enter)(void *, struct dentry *))
92  {
93         struct dentry *this_parent;
94 diff --git a/fs/fcntl.c b/fs/fcntl.c
95 index e871009f6c88..d62e114c1b1a 100644
96 --- a/fs/fcntl.c
97 +++ b/fs/fcntl.c
98 @@ -34,7 +34,7 @@
99  
100  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101  
102 -static int setfl(int fd, struct file * filp, unsigned int arg)
103 +int setfl(int fd, struct file * filp, unsigned int arg)
104  {
105         struct inode * inode = file_inode(filp);
106         int error = 0;
107 @@ -64,6 +64,8 @@ static int setfl(int fd, struct file * filp, unsigned int arg)
108  
109         if (filp->f_op->check_flags)
110                 error = filp->f_op->check_flags(arg);
111 +       if (!error && filp->f_op->setfl)
112 +               error = filp->f_op->setfl(filp, arg);
113         if (error)
114                 return error;
115  
116 diff --git a/fs/namespace.c b/fs/namespace.c
117 index e157efc54023..6c57487f126b 100644
118 --- a/fs/namespace.c
119 +++ b/fs/namespace.c
120 @@ -872,6 +872,12 @@ static inline int check_mnt(struct mount *mnt)
121         return mnt->mnt_ns == current->nsproxy->mnt_ns;
122  }
123  
124 +/* for aufs, CONFIG_AUFS_BR_FUSE */
125 +int is_current_mnt_ns(struct vfsmount *mnt)
126 +{
127 +       return check_mnt(real_mount(mnt));
128 +}
129 +
130  /*
131   * vfsmount lock must be held for write
132   */
133 diff --git a/fs/splice.c b/fs/splice.c
134 index d983d375ff11..7216ef993b5f 100644
135 --- a/fs/splice.c
136 +++ b/fs/splice.c
137 @@ -925,8 +925,8 @@ static int warn_unsupported(struct file *file, const char *op)
138  /*
139   * Attempt to initiate a splice from pipe to file.
140   */
141 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
142 -                          loff_t *ppos, size_t len, unsigned int flags)
143 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
144 +                   loff_t *ppos, size_t len, unsigned int flags)
145  {
146         if (unlikely(!out->f_op->splice_write))
147                 return warn_unsupported(out, "write");
148 diff --git a/include/linux/fs.h b/include/linux/fs.h
149 index 4aeb3fa11927..dd5871d0c429 100644
150 --- a/include/linux/fs.h
151 +++ b/include/linux/fs.h
152 @@ -1099,6 +1099,7 @@ extern void fasync_free(struct fasync_struct *);
153  /* can be called from interrupts */
154  extern void kill_fasync(struct fasync_struct **, int, int);
155  
156 +extern int setfl(int fd, struct file *filp, unsigned int arg);
157  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
158  extern int f_setown(struct file *filp, int who, int force);
159  extern void f_delown(struct file *filp);
160 @@ -1901,6 +1902,7 @@ struct file_operations {
161         int (*lock) (struct file *, int, struct file_lock *);
162         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
163         int (*check_flags)(int);
164 +       int (*setfl)(struct file *, unsigned long);
165         int (*flock) (struct file *, int, struct file_lock *);
166         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
167         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
168 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
169 index dc2844b071c2..069ffb776c2c 100644
170 --- a/include/linux/lockdep.h
171 +++ b/include/linux/lockdep.h
172 @@ -249,6 +249,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
173         return lock->key == key;
174  }
175  
176 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
177 +
178  /*
179   * Acquire a lock.
180   *
181 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
182 index 8f882f5881e8..6b9808f09843 100644
183 --- a/include/linux/mnt_namespace.h
184 +++ b/include/linux/mnt_namespace.h
185 @@ -7,12 +7,15 @@ struct mnt_namespace;
186  struct fs_struct;
187  struct user_namespace;
188  struct ns_common;
189 +struct vfsmount;
190  
191  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
192                 struct user_namespace *, struct fs_struct *);
193  extern void put_mnt_ns(struct mnt_namespace *ns);
194  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
195  
196 +extern int is_current_mnt_ns(struct vfsmount *mnt);
197 +
198  extern const struct file_operations proc_mounts_operations;
199  extern const struct file_operations proc_mountinfo_operations;
200  extern const struct file_operations proc_mountstats_operations;
201 diff --git a/include/linux/splice.h b/include/linux/splice.h
202 index 6c461573434d..7416cf375ad1 100644
203 --- a/include/linux/splice.h
204 +++ b/include/linux/splice.h
205 @@ -99,4 +99,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
206  
207  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
208  extern const struct pipe_buf_operations default_pipe_buf_ops;
209 +
210 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
211 +                          loff_t *ppos, size_t len, unsigned int flags);
212 +extern long do_splice_to(struct file *in, loff_t *ppos,
213 +                        struct pipe_inode_info *pipe, size_t len,
214 +                        unsigned int flags);
215  #endif
216 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
217 index e85b5ad3e206..db4297f60bd3 100644
218 --- a/kernel/locking/lockdep.c
219 +++ b/kernel/locking/lockdep.c
220 @@ -218,7 +218,7 @@ unsigned long max_lock_class_idx;
221  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
222  DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
223  
224 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
225 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
226  {
227         unsigned int class_idx = hlock->class_idx;
228  
229 @@ -239,6 +239,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
230          */
231         return lock_classes + class_idx;
232  }
233 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
234  
235  #ifdef CONFIG_LOCK_STAT
236  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
237 SPDX-License-Identifier: GPL-2.0
238 aufs6.x-rcN mmap patch
239
240 diff --git a/fs/proc/base.c b/fs/proc/base.c
241 index ffd54617c354..29ec720c8038 100644
242 --- a/fs/proc/base.c
243 +++ b/fs/proc/base.c
244 @@ -2218,7 +2218,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
245         rc = -ENOENT;
246         vma = find_exact_vma(mm, vm_start, vm_end);
247         if (vma && vma->vm_file) {
248 -               *path = vma->vm_file->f_path;
249 +               *path = vma_pr_or_file(vma)->f_path;
250                 path_get(path);
251                 rc = 0;
252         }
253 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
254 index 4d3493579458..42edd9a42c78 100644
255 --- a/fs/proc/nommu.c
256 +++ b/fs/proc/nommu.c
257 @@ -39,7 +39,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
258         file = region->vm_file;
259  
260         if (file) {
261 -               struct inode *inode = file_inode(region->vm_file);
262 +               struct inode *inode;
263 +
264 +               file = vmr_pr_or_file(region);
265 +               inode = file_inode(file);
266                 dev = inode->i_sb->s_dev;
267                 ino = inode->i_ino;
268         }
269 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
270 index 3dd5be96691b..40d9d970b308 100644
271 --- a/fs/proc/task_mmu.c
272 +++ b/fs/proc/task_mmu.c
273 @@ -271,7 +271,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
274         const char *name = NULL;
275  
276         if (file) {
277 -               struct inode *inode = file_inode(vma->vm_file);
278 +               struct inode *inode;
279 +
280 +               file = vma_pr_or_file(vma);
281 +               inode = file_inode(file);
282                 dev = inode->i_sb->s_dev;
283                 ino = inode->i_ino;
284                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
285 @@ -1943,7 +1946,7 @@ static int show_numa_map(struct seq_file *m, void *v)
286         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
287         struct vm_area_struct *vma = v;
288         struct numa_maps *md = &numa_priv->md;
289 -       struct file *file = vma->vm_file;
290 +       struct file *file = vma_pr_or_file(vma);
291         struct mm_struct *mm = vma->vm_mm;
292         struct mempolicy *pol;
293         char buffer[64];
294 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
295 index a8ac0dd8041e..9fca456e2259 100644
296 --- a/fs/proc/task_nommu.c
297 +++ b/fs/proc/task_nommu.c
298 @@ -137,7 +137,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
299         file = vma->vm_file;
300  
301         if (file) {
302 -               struct inode *inode = file_inode(vma->vm_file);
303 +               struct inode *inode;
304 +
305 +               file = vma_pr_or_file(vma);
306 +               inode = file_inode(file);
307                 dev = inode->i_sb->s_dev;
308                 ino = inode->i_ino;
309                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
310 diff --git a/include/linux/mm.h b/include/linux/mm.h
311 index bf5d0b1b16f4..94b956eff452 100644
312 --- a/include/linux/mm.h
313 +++ b/include/linux/mm.h
314 @@ -2409,6 +2409,43 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
315  static inline struct vm_area_struct *vma_lookup(struct mm_struct *mm,
316                                                 unsigned long addr);
317  
318 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
319 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
320 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
321 +                                     int);
322 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
323 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
324 +
325 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
326 +                                                               __LINE__)
327 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
328 +                                                         __LINE__)
329 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
330 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
331 +
332 +#ifndef CONFIG_MMU
333 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
334 +extern void vmr_do_fput(struct vm_region *, const char[], int);
335 +
336 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
337 +                                                         __LINE__)
338 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
339 +#endif /* !CONFIG_MMU */
340 +
341 +#else
342 +
343 +#define vma_file_update_time(vma)      file_update_time((vma)->vm_file)
344 +#define vma_pr_or_file(vma)            (vma)->vm_file
345 +#define vma_get_file(vma)              get_file((vma)->vm_file)
346 +#define vma_fput(vma)                  fput((vma)->vm_file)
347 +
348 +#ifndef CONFIG_MMU
349 +#define vmr_pr_or_file(region)         (region)->vm_file
350 +#define vmr_fput(region)               fput((region)->vm_file)
351 +#endif /* !CONFIG_MMU */
352 +
353 +#endif /* CONFIG_AUFS_FS */
354 +
355  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
356                 void *buf, int len, unsigned int gup_flags);
357  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
358 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
359 index 36c5b43999e6..ce93e97f76a6 100644
360 --- a/include/linux/mm_types.h
361 +++ b/include/linux/mm_types.h
362 @@ -524,6 +524,9 @@ struct vm_region {
363         unsigned long   vm_top;         /* region allocated to here */
364         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
365         struct file     *vm_file;       /* the backing file or NULL */
366 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
367 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
368 +#endif
369  
370         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
371         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
372 @@ -637,6 +640,9 @@ struct vm_area_struct {
373         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
374                                            units */
375         struct file * vm_file;          /* File we map to (can be NULL). */
376 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
377 +       struct file *vm_prfile;         /* shadow of vm_file */
378 +#endif
379         void * vm_private_data;         /* was vm_pte (shared mem) */
380  
381  #ifdef CONFIG_ANON_VMA_NAME
382 diff --git a/kernel/fork.c b/kernel/fork.c
383 index 3b6d20dfb9a8..ccad0325cfa9 100644
384 --- a/kernel/fork.c
385 +++ b/kernel/fork.c
386 @@ -731,7 +731,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
387                 if (file) {
388                         struct address_space *mapping = file->f_mapping;
389  
390 -                       get_file(file);
391 +                       vma_get_file(tmp);
392                         i_mmap_lock_write(mapping);
393                         if (tmp->vm_flags & VM_SHARED)
394                                 mapping_allow_writable(mapping);
395 diff --git a/mm/Makefile b/mm/Makefile
396 index ec65984e2ade..d59461647ccd 100644
397 --- a/mm/Makefile
398 +++ b/mm/Makefile
399 @@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o
400  obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
401  obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
402  obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
403 +obj-y += prfile.o
404 diff --git a/mm/filemap.c b/mm/filemap.c
405 index 582f5317ff71..c024ebb30073 100644
406 --- a/mm/filemap.c
407 +++ b/mm/filemap.c
408 @@ -3599,7 +3599,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
409         vm_fault_t ret = VM_FAULT_LOCKED;
410  
411         sb_start_pagefault(mapping->host->i_sb);
412 -       file_update_time(vmf->vma->vm_file);
413 +       vma_file_update_time(vmf->vma);
414         folio_lock(folio);
415         if (folio->mapping != mapping) {
416                 folio_unlock(folio);
417 diff --git a/mm/mmap.c b/mm/mmap.c
418 index b56a7f0c9f85..5eb114409e07 100644
419 --- a/mm/mmap.c
420 +++ b/mm/mmap.c
421 @@ -140,7 +140,7 @@ static void remove_vma(struct vm_area_struct *vma, bool unreachable)
422         if (vma->vm_ops && vma->vm_ops->close)
423                 vma->vm_ops->close(vma);
424         if (vma->vm_file)
425 -               fput(vma->vm_file);
426 +               vma_fput(vma);
427         mpol_put(vma_policy(vma));
428         if (unreachable)
429                 __vm_area_free(vma);
430 @@ -554,7 +554,7 @@ static inline void vma_complete(struct vma_prepare *vp,
431                 if (vp->file) {
432                         uprobe_munmap(vp->remove, vp->remove->vm_start,
433                                       vp->remove->vm_end);
434 -                       fput(vp->file);
435 +                       vma_fput(vp->vma);
436                 }
437                 if (vp->remove->anon_vma)
438                         anon_vma_merge(vp->vma, vp->remove);
439 @@ -2364,7 +2364,7 @@ int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
440                 goto out_free_mpol;
441  
442         if (new->vm_file)
443 -               get_file(new->vm_file);
444 +               vma_get_file(new);
445  
446         if (new->vm_ops && new->vm_ops->open)
447                 new->vm_ops->open(new);
448 @@ -2781,7 +2781,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
449                                  * and cause general protection fault
450                                  * ultimately.
451                                  */
452 -                               fput(vma->vm_file);
453 +                               vma_fput(vma);
454                                 vm_area_free(vma);
455                                 vma = merge;
456                                 /* Update vm_flags to pick up the change. */
457 @@ -2876,7 +2876,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
458  
459         if (file || vma->vm_file) {
460  unmap_and_free_vma:
461 -               fput(vma->vm_file);
462 +               vma_fput(vma);
463                 vma->vm_file = NULL;
464  
465                 vma_iter_set(&vmi, vma->vm_end);
466 @@ -2938,6 +2938,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
467         unsigned long populate = 0;
468         unsigned long ret = -EINVAL;
469         struct file *file;
470 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
471 +       struct file *prfile;
472 +#endif
473  
474         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
475                      current->comm, current->pid);
476 @@ -2996,10 +2999,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
477         if (vma->vm_flags & VM_LOCKED)
478                 flags |= MAP_LOCKED;
479  
480 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
481 +       vma_get_file(vma);
482 +       file = vma->vm_file;
483 +       prfile = vma->vm_prfile;
484 +       ret = do_mmap(vma->vm_file, start, size,
485 +                       prot, flags, /*vm_flags*/0, pgoff, &populate, NULL);
486 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
487 +               struct vm_area_struct *new_vma;
488 +
489 +               new_vma = find_vma(mm, ret);
490 +               if (!new_vma->vm_prfile)
491 +                       new_vma->vm_prfile = prfile;
492 +               if (prfile)
493 +                       get_file(prfile);
494 +       }
495 +       /*
496 +        * two fput()s instead of vma_fput(vma),
497 +        * coz vma may not be available anymore.
498 +        */
499 +       fput(file);
500 +       if (prfile)
501 +               fput(prfile);
502 +#else
503         file = get_file(vma->vm_file);
504         ret = do_mmap(vma->vm_file, start, size,
505                         prot, flags, 0, pgoff, &populate, NULL);
506         fput(file);
507 +#endif /* CONFIG_AUFS_FS */
508  out:
509         mmap_write_unlock(mm);
510         if (populate)
511 @@ -3350,7 +3377,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
512                 if (anon_vma_clone(new_vma, vma))
513                         goto out_free_mempol;
514                 if (new_vma->vm_file)
515 -                       get_file(new_vma->vm_file);
516 +                       vma_get_file(new_vma);
517                 if (new_vma->vm_ops && new_vma->vm_ops->open)
518                         new_vma->vm_ops->open(new_vma);
519                 if (vma_link(mm, new_vma))
520 @@ -3364,7 +3391,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
521                 new_vma->vm_ops->close(new_vma);
522  
523         if (new_vma->vm_file)
524 -               fput(new_vma->vm_file);
525 +               vma_fput(new_vma);
526  
527         unlink_anon_vmas(new_vma);
528  out_free_mempol:
529 diff --git a/mm/nommu.c b/mm/nommu.c
530 index 7f9e9e5a0e12..69663f2bd4c4 100644
531 --- a/mm/nommu.c
532 +++ b/mm/nommu.c
533 @@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
534                 up_write(&nommu_region_sem);
535  
536                 if (region->vm_file)
537 -                       fput(region->vm_file);
538 +                       vmr_fput(region);
539  
540                 /* IO memory and memory shared directly out of the pagecache
541                  * from ramfs/tmpfs mustn't be released here */
542 @@ -603,7 +603,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
543         if (vma->vm_ops && vma->vm_ops->close)
544                 vma->vm_ops->close(vma);
545         if (vma->vm_file)
546 -               fput(vma->vm_file);
547 +               vma_fput(vma);
548         put_nommu_region(vma->vm_region);
549         vm_area_free(vma);
550  }
551 @@ -1135,7 +1135,7 @@ unsigned long do_mmap(struct file *file,
552                                         goto error_just_free;
553                                 }
554                         }
555 -                       fput(region->vm_file);
556 +                       vmr_fput(region);
557                         kmem_cache_free(vm_region_jar, region);
558                         region = pregion;
559                         result = start;
560 @@ -1221,10 +1221,10 @@ unsigned long do_mmap(struct file *file,
561  error:
562         vma_iter_free(&vmi);
563         if (region->vm_file)
564 -               fput(region->vm_file);
565 +               vmr_fput(region);
566         kmem_cache_free(vm_region_jar, region);
567         if (vma->vm_file)
568 -               fput(vma->vm_file);
569 +               vma_fput(vma);
570         vm_area_free(vma);
571         return ret;
572  
573 diff --git a/mm/prfile.c b/mm/prfile.c
574 new file mode 100644
575 index 000000000000..8f820a235364
576 --- /dev/null
577 +++ b/mm/prfile.c
578 @@ -0,0 +1,86 @@
579 +// SPDX-License-Identifier: GPL-2.0
580 +/*
581 + * Mainly for aufs which mmap(2) different file and wants to print different
582 + * path in /proc/PID/maps.
583 + * Call these functions via macros defined in linux/mm.h.
584 + *
585 + * See Documentation/filesystems/aufs/design/06mmap.txt
586 + *
587 + * Copyright (c) 2014-2022 Junjro R. Okajima
588 + * Copyright (c) 2014 Ian Campbell
589 + */
590 +
591 +#include <linux/mm.h>
592 +#include <linux/file.h>
593 +#include <linux/fs.h>
594 +
595 +/* #define PRFILE_TRACE */
596 +static inline void prfile_trace(struct file *f, struct file *pr,
597 +                             const char func[], int line, const char func2[])
598 +{
599 +#ifdef PRFILE_TRACE
600 +       if (pr)
601 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
602 +#endif
603 +}
604 +
605 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
606 +                            int line)
607 +{
608 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
609 +
610 +       prfile_trace(f, pr, func, line, __func__);
611 +       file_update_time(f);
612 +       if (f && pr)
613 +               file_update_time(pr);
614 +}
615 +
616 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
617 +                              int line)
618 +{
619 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
620 +
621 +       prfile_trace(f, pr, func, line, __func__);
622 +       return (f && pr) ? pr : f;
623 +}
624 +
625 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
626 +{
627 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
628 +
629 +       prfile_trace(f, pr, func, line, __func__);
630 +       get_file(f);
631 +       if (f && pr)
632 +               get_file(pr);
633 +}
634 +
635 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
636 +{
637 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
638 +
639 +       prfile_trace(f, pr, func, line, __func__);
640 +       fput(f);
641 +       if (f && pr)
642 +               fput(pr);
643 +}
644 +
645 +#ifndef CONFIG_MMU
646 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
647 +                              int line)
648 +{
649 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
650 +
651 +       prfile_trace(f, pr, func, line, __func__);
652 +       return (f && pr) ? pr : f;
653 +}
654 +
655 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
656 +{
657 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
658 +
659 +       prfile_trace(f, pr, func, line, __func__);
660 +       fput(f);
661 +       if (f && pr)
662 +               fput(pr);
663 +}
664 +#endif /* !CONFIG_MMU */
665 SPDX-License-Identifier: GPL-2.0
666 aufs6.x-rcN standalone patch
667
668 diff --git a/fs/dcache.c b/fs/dcache.c
669 index 6c930ceed526..576ad162cdec 100644
670 --- a/fs/dcache.c
671 +++ b/fs/dcache.c
672 @@ -1450,6 +1450,7 @@ void d_walk(struct dentry *parent, void *data,
673         seq = 1;
674         goto again;
675  }
676 +EXPORT_SYMBOL_GPL(d_walk);
677  
678  struct check_mount {
679         struct vfsmount *mnt;
680 @@ -3051,6 +3052,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
681  
682         write_sequnlock(&rename_lock);
683  }
684 +EXPORT_SYMBOL_GPL(d_exchange);
685  
686  /**
687   * d_ancestor - search for an ancestor
688 diff --git a/fs/exec.c b/fs/exec.c
689 index 6518e33ea813..b67efac6a1ad 100644
690 --- a/fs/exec.c
691 +++ b/fs/exec.c
692 @@ -112,6 +112,7 @@ bool path_noexec(const struct path *path)
693         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
694                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
695  }
696 +EXPORT_SYMBOL_GPL(path_noexec);
697  
698  #ifdef CONFIG_USELIB
699  /*
700 diff --git a/fs/fcntl.c b/fs/fcntl.c
701 index d62e114c1b1a..ceef001775bd 100644
702 --- a/fs/fcntl.c
703 +++ b/fs/fcntl.c
704 @@ -87,6 +87,7 @@ int setfl(int fd, struct file * filp, unsigned int arg)
705   out:
706         return error;
707  }
708 +EXPORT_SYMBOL_GPL(setfl);
709  
710  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
711                       int force)
712 diff --git a/fs/file_table.c b/fs/file_table.c
713 index ee21b3da9d08..c45ac36795dd 100644
714 --- a/fs/file_table.c
715 +++ b/fs/file_table.c
716 @@ -225,6 +225,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
717         }
718         return ERR_PTR(-ENFILE);
719  }
720 +EXPORT_SYMBOL_GPL(alloc_empty_file);
721  
722  /*
723   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
724 diff --git a/fs/namespace.c b/fs/namespace.c
725 index 6c57487f126b..16be9ac1c734 100644
726 --- a/fs/namespace.c
727 +++ b/fs/namespace.c
728 @@ -466,6 +466,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
729         mnt_dec_writers(real_mount(mnt));
730         preempt_enable();
731  }
732 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
733  
734  /**
735   * mnt_drop_write - give up write access to a mount
736 @@ -877,6 +878,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
737  {
738         return check_mnt(real_mount(mnt));
739  }
740 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
741  
742  /*
743   * vfsmount lock must be held for write
744 @@ -2152,6 +2154,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
745         }
746         return 0;
747  }
748 +EXPORT_SYMBOL_GPL(iterate_mounts);
749  
750  static void lock_mnt_tree(struct mount *mnt)
751  {
752 diff --git a/fs/notify/group.c b/fs/notify/group.c
753 index 1de6631a3925..3008eb37a18d 100644
754 --- a/fs/notify/group.c
755 +++ b/fs/notify/group.c
756 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
757  {
758         refcount_inc(&group->refcnt);
759  }
760 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
761  
762  /*
763   * Drop a reference to a group.  Free it if it's through.
764 diff --git a/fs/open.c b/fs/open.c
765 index 98f6601fbac6..8624e4ffa15b 100644
766 --- a/fs/open.c
767 +++ b/fs/open.c
768 @@ -67,6 +67,7 @@ int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
769         inode_unlock(dentry->d_inode);
770         return ret;
771  }
772 +EXPORT_SYMBOL_GPL(do_truncate);
773  
774  long vfs_truncate(const struct path *path, loff_t length)
775  {
776 diff --git a/fs/read_write.c b/fs/read_write.c
777 index 4771701c896b..c79270aba792 100644
778 --- a/fs/read_write.c
779 +++ b/fs/read_write.c
780 @@ -477,6 +477,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
781         inc_syscr(current);
782         return ret;
783  }
784 +EXPORT_SYMBOL_GPL(vfs_read);
785  
786  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
787  {
788 @@ -592,6 +593,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
789         file_end_write(file);
790         return ret;
791  }
792 +EXPORT_SYMBOL_GPL(vfs_write);
793  
794  /* file_ppos returns &file->f_pos or NULL if file is stream */
795  static inline loff_t *file_ppos(struct file *file)
796 diff --git a/fs/splice.c b/fs/splice.c
797 index 7216ef993b5f..7ce1f1bc4268 100644
798 --- a/fs/splice.c
799 +++ b/fs/splice.c
800 @@ -932,6 +932,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
801                 return warn_unsupported(out, "write");
802         return out->f_op->splice_write(pipe, out, ppos, len, flags);
803  }
804 +EXPORT_SYMBOL_GPL(do_splice_from);
805  
806  /*
807   * Indicate to the caller that there was a premature EOF when reading from the
808 diff --git a/fs/xattr.c b/fs/xattr.c
809 index efd4736bc94b..ce1a2c39ab23 100644
810 --- a/fs/xattr.c
811 +++ b/fs/xattr.c
812 @@ -406,6 +406,7 @@ vfs_getxattr_alloc(struct mnt_idmap *idmap, struct dentry *dentry,
813         *xattr_value = value;
814         return error;
815  }
816 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
817  
818  ssize_t
819  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
820 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
821 index db4297f60bd3..9aca18312afb 100644
822 --- a/kernel/locking/lockdep.c
823 +++ b/kernel/locking/lockdep.c
824 @@ -239,6 +239,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
825          */
826         return lock_classes + class_idx;
827  }
828 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
829  #define hlock_class(hlock) lockdep_hlock_class(hlock)
830  
831  #ifdef CONFIG_LOCK_STAT
832 diff --git a/kernel/task_work.c b/kernel/task_work.c
833 index 065e1ef8fc8d..c623c6f0c645 100644
834 --- a/kernel/task_work.c
835 +++ b/kernel/task_work.c
836 @@ -182,3 +182,4 @@ void task_work_run(void)
837                 } while (work);
838         }
839  }
840 +EXPORT_SYMBOL_GPL(task_work_run);
841 diff --git a/security/security.c b/security/security.c
842 index 23b129d482a7..fca4c5707a1c 100644
843 --- a/security/security.c
844 +++ b/security/security.c
845 @@ -1750,6 +1750,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
846                 return 0;
847         return call_int_hook(path_rmdir, 0, dir, dentry);
848  }
849 +EXPORT_SYMBOL_GPL(security_path_rmdir);
850  
851  /**
852   * security_path_unlink() - Check if removing a hard link is allowed
853 @@ -1785,6 +1786,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
854                 return 0;
855         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
856  }
857 +EXPORT_SYMBOL_GPL(security_path_symlink);
858  
859  /**
860   * security_path_link - Check if creating a hard link is allowed
861 @@ -1803,6 +1805,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
862                 return 0;
863         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
864  }
865 +EXPORT_SYMBOL_GPL(security_path_link);
866  
867  /**
868   * security_path_rename() - Check if renaming a file is allowed
869 @@ -1864,6 +1867,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
870                 return 0;
871         return call_int_hook(path_chmod, 0, path, mode);
872  }
873 +EXPORT_SYMBOL_GPL(security_path_chmod);
874  
875  /**
876   * security_path_chown() - Check if changing the file's owner/group is allowed
877 @@ -1881,6 +1885,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
878                 return 0;
879         return call_int_hook(path_chown, 0, path, uid, gid);
880  }
881 +EXPORT_SYMBOL_GPL(security_path_chown);
882  
883  /**
884   * security_path_chroot() - Check if changing the root directory is allowed
885 @@ -2110,6 +2115,7 @@ int security_inode_permission(struct inode *inode, int mask)
886                 return 0;
887         return call_int_hook(inode_permission, 0, inode, mask);
888  }
889 +EXPORT_SYMBOL_GPL(security_inode_permission);
890  
891  /**
892   * security_inode_setattr() - Check if setting file attributes is allowed
893 @@ -2588,6 +2594,7 @@ int security_file_permission(struct file *file, int mask)
894  
895         return fsnotify_perm(file, mask);
896  }
897 +EXPORT_SYMBOL_GPL(security_file_permission);
898  
899  /**
900   * security_file_alloc() - Allocate and init a file's LSM blob
901 @@ -2854,6 +2861,7 @@ int security_file_truncate(struct file *file)
902  {
903         return call_int_hook(file_truncate, 0, file);
904  }
905 +EXPORT_SYMBOL_GPL(security_file_truncate);
906  
907  /**
908   * security_task_alloc() - Allocate a task's LSM blob
909 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
910 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
911 +++ linux/Documentation/ABI/testing/debugfs-aufs        2022-11-05 23:02:18.955889283 +0100
912 @@ -0,0 +1,55 @@
913 +What:          /debug/aufs/si_<id>/
914 +Date:          March 2009
915 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
916 +Description:
917 +               Under /debug/aufs, a directory named si_<id> is created
918 +               per aufs mount, where <id> is a unique id generated
919 +               internally.
920 +
921 +What:          /debug/aufs/si_<id>/plink
922 +Date:          Apr 2013
923 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
924 +Description:
925 +               It has three lines and shows the information about the
926 +               pseudo-link. The first line is a single number
927 +               representing a number of buckets. The second line is a
928 +               number of pseudo-links per buckets (separated by a
929 +               blank). The last line is a single number representing a
930 +               total number of psedo-links.
931 +               When the aufs mount option 'noplink' is specified, it
932 +               will show "1\n0\n0\n".
933 +
934 +What:          /debug/aufs/si_<id>/xib
935 +Date:          March 2009
936 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
937 +Description:
938 +               It shows the consumed blocks by xib (External Inode Number
939 +               Bitmap), its block size and file size.
940 +               When the aufs mount option 'noxino' is specified, it
941 +               will be empty. About XINO files, see the aufs manual.
942 +
943 +What:          /debug/aufs/si_<id>/xi<branch-index>
944 +Date:          March 2009
945 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
946 +Description:
947 +               It shows the consumed blocks by xino (External Inode Number
948 +               Translation Table), its link count, block size and file
949 +               size.
950 +               Due to the file size limit, there may exist multiple
951 +               xino files per branch.  In this case, "-N" is added to
952 +               the filename and it corresponds to the index of the
953 +               internal xino array.  "-0" is omitted.
954 +               When the aufs mount option 'noxino' is specified, Those
955 +               entries won't exist.  About XINO files, see the aufs
956 +               manual.
957 +
958 +What:          /debug/aufs/si_<id>/xigen
959 +Date:          March 2009
960 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
961 +Description:
962 +               It shows the consumed blocks by xigen (External Inode
963 +               Generation Table), its block size and file size.
964 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
965 +               be created.
966 +               When the aufs mount option 'noxino' is specified, it
967 +               will be empty. About XINO files, see the aufs manual.
968 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
969 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
970 +++ linux/Documentation/ABI/testing/sysfs-aufs  2022-11-05 23:02:18.955889283 +0100
971 @@ -0,0 +1,31 @@
972 +What:          /sys/fs/aufs/si_<id>/
973 +Date:          March 2009
974 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
975 +Description:
976 +               Under /sys/fs/aufs, a directory named si_<id> is created
977 +               per aufs mount, where <id> is a unique id generated
978 +               internally.
979 +
980 +What:          /sys/fs/aufs/si_<id>/br<idx>
981 +Date:          March 2009
982 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
983 +Description:
984 +               It shows the abolute path of a member directory (which
985 +               is called branch) in aufs, and its permission.
986 +
987 +What:          /sys/fs/aufs/si_<id>/brid<idx>
988 +Date:          July 2013
989 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
990 +Description:
991 +               It shows the id of a member directory (which is called
992 +               branch) in aufs.
993 +
994 +What:          /sys/fs/aufs/si_<id>/xi_path
995 +Date:          March 2009
996 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
997 +Description:
998 +               It shows the abolute path of XINO (External Inode Number
999 +               Bitmap, Translation Table and Generation Table) file
1000 +               even if it is the default path.
1001 +               When the aufs mount option 'noxino' is specified, it
1002 +               will be empty. About XINO files, see the aufs manual.
1003 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1004 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1005 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2022-11-05 23:02:18.955889283 +0100
1006 @@ -0,0 +1,171 @@
1007 +
1008 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1009 +#
1010 +# This program is free software; you can redistribute it and/or modify
1011 +# it under the terms of the GNU General Public License as published by
1012 +# the Free Software Foundation; either version 2 of the License, or
1013 +# (at your option) any later version.
1014 +#
1015 +# This program is distributed in the hope that it will be useful,
1016 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1017 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1018 +# GNU General Public License for more details.
1019 +#
1020 +# You should have received a copy of the GNU General Public License
1021 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1022 +
1023 +Introduction
1024 +----------------------------------------
1025 +
1026 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1027 +1. abbrev. for "advanced multi-layered unification filesystem".
1028 +2. abbrev. for "another unionfs".
1029 +3. abbrev. for "auf das" in German which means "on the" in English.
1030 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1031 +   But "Filesystem aufs Filesystem" is hard to understand.
1032 +4. abbrev. for "African Urban Fashion Show".
1033 +
1034 +AUFS is a filesystem with features:
1035 +- multi layered stackable unification filesystem, the member directory
1036 +  is called as a branch.
1037 +- branch permission and attribute, 'readonly', 'real-readonly',
1038 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1039 +  combination.
1040 +- internal "file copy-on-write".
1041 +- logical deletion, whiteout.
1042 +- dynamic branch manipulation, adding, deleting and changing permission.
1043 +- allow bypassing aufs, user's direct branch access.
1044 +- external inode number translation table and bitmap which maintains the
1045 +  persistent aufs inode number.
1046 +- seekable directory, including NFS readdir.
1047 +- file mapping, mmap and sharing pages.
1048 +- pseudo-link, hardlink over branches.
1049 +- loopback mounted filesystem as a branch.
1050 +- several policies to select one among multiple writable branches.
1051 +- revert a single systemcall when an error occurs in aufs.
1052 +- and more...
1053 +
1054 +
1055 +Multi Layered Stackable Unification Filesystem
1056 +----------------------------------------------------------------------
1057 +Most people already knows what it is.
1058 +It is a filesystem which unifies several directories and provides a
1059 +merged single directory. When users access a file, the access will be
1060 +passed/re-directed/converted (sorry, I am not sure which English word is
1061 +correct) to the real file on the member filesystem. The member
1062 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1063 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1064 +readonly branch is handled by creating 'whiteout' on the upper writable
1065 +branch.
1066 +
1067 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1068 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1069 +different approaches to implement the merged-view.
1070 +The former tries putting it into VFS, and the latter implements as a
1071 +separate filesystem.
1072 +(If I misunderstand about these implementations, please let me know and
1073 +I shall correct it. Because it is a long time ago when I read their
1074 +source files last time).
1075 +
1076 +UnionMount's approach will be able to small, but may be hard to share
1077 +branches between several UnionMount since the whiteout in it is
1078 +implemented in the inode on branch filesystem and always
1079 +shared. According to Bharata's post, readdir does not seems to be
1080 +finished yet.
1081 +There are several missing features known in this implementations such as
1082 +- for users, the inode number may change silently. eg. copy-up.
1083 +- link(2) may break by copy-up.
1084 +- read(2) may get an obsoleted filedata (fstat(2) too).
1085 +- fcntl(F_SETLK) may be broken by copy-up.
1086 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1087 +  open(O_RDWR).
1088 +
1089 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1090 +merged into mainline. This is another implementation of UnionMount as a
1091 +separated filesystem. All the limitations and known problems which
1092 +UnionMount are equally inherited to "overlay" filesystem.
1093 +
1094 +Unionfs has a longer history. When I started implementing a stackable
1095 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1096 +inode, dentry and file objects and they have an array pointing lower
1097 +same kind objects. After contributing many patches for Unionfs, I
1098 +re-started my project AUFS (Jun 2006).
1099 +
1100 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1101 +implemented my own ideas, approaches and enhancements and it became
1102 +totally different one.
1103 +
1104 +Comparing DM snapshot and fs based implementation
1105 +- the number of bytes to be copied between devices is much smaller.
1106 +- the type of filesystem must be one and only.
1107 +- the fs must be writable, no readonly fs, even for the lower original
1108 +  device. so the compression fs will not be usable. but if we use
1109 +  loopback mount, we may address this issue.
1110 +  for instance,
1111 +       mount /cdrom/squashfs.img /sq
1112 +       losetup /sq/ext2.img
1113 +       losetup /somewhere/cow
1114 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1115 +- it will be difficult (or needs more operations) to extract the
1116 +  difference between the original device and COW.
1117 +- DM snapshot-merge may help a lot when users try merging. in the
1118 +  fs-layer union, users will use rsync(1).
1119 +
1120 +You may want to read my old paper "Filesystems in LiveCD"
1121 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1122 +
1123 +
1124 +Several characters/aspects/persona of aufs
1125 +----------------------------------------------------------------------
1126 +
1127 +Aufs has several characters, aspects or persona.
1128 +1. a filesystem, callee of VFS helper
1129 +2. sub-VFS, caller of VFS helper for branches
1130 +3. a virtual filesystem which maintains persistent inode number
1131 +4. reader/writer of files on branches such like an application
1132 +
1133 +1. Callee of VFS Helper
1134 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1135 +unlink(2) from an application reaches sys_unlink() kernel function and
1136 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1137 +calls filesystem specific unlink operation. Actually aufs implements the
1138 +unlink operation but it behaves like a redirector.
1139 +
1140 +2. Caller of VFS Helper for Branches
1141 +aufs_unlink() passes the unlink request to the branch filesystem as if
1142 +it were called from VFS. So the called unlink operation of the branch
1143 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1144 +every necessary pre/post operation for the branch filesystem.
1145 +- acquire the lock for the parent dir on a branch
1146 +- lookup in a branch
1147 +- revalidate dentry on a branch
1148 +- mnt_want_write() for a branch
1149 +- vfs_unlink() for a branch
1150 +- mnt_drop_write() for a branch
1151 +- release the lock on a branch
1152 +
1153 +3. Persistent Inode Number
1154 +One of the most important issue for a filesystem is to maintain inode
1155 +numbers. This is particularly important to support exporting a
1156 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1157 +backend block device for its own. But some storage is necessary to
1158 +keep and maintain the inode numbers. It may be a large space and may not
1159 +suit to keep in memory. Aufs rents some space from its first writable
1160 +branch filesystem (by default) and creates file(s) on it. These files
1161 +are created by aufs internally and removed soon (currently) keeping
1162 +opened.
1163 +Note: Because these files are removed, they are totally gone after
1164 +      unmounting aufs. It means the inode numbers are not persistent
1165 +      across unmount or reboot. I have a plan to make them really
1166 +      persistent which will be important for aufs on NFS server.
1167 +
1168 +4. Read/Write Files Internally (copy-on-write)
1169 +Because a branch can be readonly, when you write a file on it, aufs will
1170 +"copy-up" it to the upper writable branch internally. And then write the
1171 +originally requested thing to the file. Generally kernel doesn't
1172 +open/read/write file actively. In aufs, even a single write may cause a
1173 +internal "file copy". This behaviour is very similar to cp(1) command.
1174 +
1175 +Some people may think it is better to pass such work to user space
1176 +helper, instead of doing in kernel space. Actually I am still thinking
1177 +about it. But currently I have implemented it in kernel space.
1178 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1179 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1180 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2022-11-05 23:02:18.955889283 +0100
1181 @@ -0,0 +1,258 @@
1182 +
1183 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1184 +#
1185 +# This program is free software; you can redistribute it and/or modify
1186 +# it under the terms of the GNU General Public License as published by
1187 +# the Free Software Foundation; either version 2 of the License, or
1188 +# (at your option) any later version.
1189 +#
1190 +# This program is distributed in the hope that it will be useful,
1191 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1192 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1193 +# GNU General Public License for more details.
1194 +#
1195 +# You should have received a copy of the GNU General Public License
1196 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1197 +
1198 +Basic Aufs Internal Structure
1199 +
1200 +Superblock/Inode/Dentry/File Objects
1201 +----------------------------------------------------------------------
1202 +As like an ordinary filesystem, aufs has its own
1203 +superblock/inode/dentry/file objects. All these objects have a
1204 +dynamically allocated array and store the same kind of pointers to the
1205 +lower filesystem, branch.
1206 +For example, when you build a union with one readwrite branch and one
1207 +readonly, mounted /au, /rw and /ro respectively.
1208 +- /au = /rw + /ro
1209 +- /ro/fileA exists but /rw/fileA
1210 +
1211 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1212 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1213 +- [0] = NULL (because /rw/fileA doesn't exist)
1214 +- [1] = /ro/fileA
1215 +
1216 +This style of an array is essentially same to the aufs
1217 +superblock/inode/dentry/file objects.
1218 +
1219 +Because aufs supports manipulating branches, ie. add/delete/change
1220 +branches dynamically, these objects has its own generation. When
1221 +branches are changed, the generation in aufs superblock is
1222 +incremented. And a generation in other object are compared when it is
1223 +accessed. When a generation in other objects are obsoleted, aufs
1224 +refreshes the internal array.
1225 +
1226 +
1227 +Superblock
1228 +----------------------------------------------------------------------
1229 +Additionally aufs superblock has some data for policies to select one
1230 +among multiple writable branches, XIB files, pseudo-links and kobject.
1231 +See below in detail.
1232 +About the policies which supports copy-down a directory, see
1233 +wbr_policy.txt too.
1234 +
1235 +
1236 +Branch and XINO(External Inode Number Translation Table)
1237 +----------------------------------------------------------------------
1238 +Every branch has its own xino (external inode number translation table)
1239 +file. The xino file is created and unlinked by aufs internally. When two
1240 +members of a union exist on the same filesystem, they share the single
1241 +xino file.
1242 +The struct of a xino file is simple, just a sequence of aufs inode
1243 +numbers which is indexed by the lower inode number.
1244 +In the above sample, assume the inode number of /ro/fileA is i111 and
1245 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1246 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1247 +
1248 +When the inode numbers are not contiguous, the xino file will be sparse
1249 +which has a hole in it and doesn't consume as much disk space as it
1250 +might appear. If your branch filesystem consumes disk space for such
1251 +holes, then you should specify 'xino=' option at mounting aufs.
1252 +
1253 +Aufs has a mount option to free the disk blocks for such holes in XINO
1254 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1255 +meet a problem of disk shortage due to XINO files, then you should try
1256 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1257 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1258 +the holes in XINO files.
1259 +
1260 +Also a writable branch has three kinds of "whiteout bases". All these
1261 +are existed when the branch is joined to aufs, and their names are
1262 +whiteout-ed doubly, so that users will never see their names in aufs
1263 +hierarchy.
1264 +1. a regular file which will be hardlinked to all whiteouts.
1265 +2. a directory to store a pseudo-link.
1266 +3. a directory to store an "orphan"-ed file temporary.
1267 +
1268 +1. Whiteout Base
1269 +   When you remove a file on a readonly branch, aufs handles it as a
1270 +   logical deletion and creates a whiteout on the upper writable branch
1271 +   as a hardlink of this file in order not to consume inode on the
1272 +   writable branch.
1273 +2. Pseudo-link Dir
1274 +   See below, Pseudo-link.
1275 +3. Step-Parent Dir
1276 +   When "fileC" exists on the lower readonly branch only and it is
1277 +   opened and removed with its parent dir, and then user writes
1278 +   something into it, then aufs copies-up fileC to this
1279 +   directory. Because there is no other dir to store fileC. After
1280 +   creating a file under this dir, the file is unlinked.
1281 +
1282 +Because aufs supports manipulating branches, ie. add/delete/change
1283 +dynamically, a branch has its own id. When the branch order changes,
1284 +aufs finds the new index by searching the branch id.
1285 +
1286 +
1287 +Pseudo-link
1288 +----------------------------------------------------------------------
1289 +Assume "fileA" exists on the lower readonly branch only and it is
1290 +hardlinked to "fileB" on the branch. When you write something to fileA,
1291 +aufs copies-up it to the upper writable branch. Additionally aufs
1292 +creates a hardlink under the Pseudo-link Directory of the writable
1293 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1294 +simple list. If fileB is read after unlinking fileA, aufs returns
1295 +filedata from the pseudo-link instead of the lower readonly
1296 +branch. Because the pseudo-link is based upon the inode, to keep the
1297 +inode number by xino (see above) is essentially necessary.
1298 +
1299 +All the hardlinks under the Pseudo-link Directory of the writable branch
1300 +should be restored in a proper location later. Aufs provides a utility
1301 +to do this. The userspace helpers executed at remounting and unmounting
1302 +aufs by default.
1303 +During this utility is running, it puts aufs into the pseudo-link
1304 +maintenance mode. In this mode, only the process which began the
1305 +maintenance mode (and its child processes) is allowed to operate in
1306 +aufs. Some other processes which are not related to the pseudo-link will
1307 +be allowed to run too, but the rest have to return an error or wait
1308 +until the maintenance mode ends. If a process already acquires an inode
1309 +mutex (in VFS), it has to return an error.
1310 +
1311 +
1312 +XIB(external inode number bitmap)
1313 +----------------------------------------------------------------------
1314 +Addition to the xino file per a branch, aufs has an external inode number
1315 +bitmap in a superblock object. It is also an internal file such like a
1316 +xino file.
1317 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1318 +not.
1319 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1320 +
1321 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1322 +reduce the number of consumed disk blocks for these files.
1323 +
1324 +
1325 +Virtual or Vertical Dir, and Readdir in Userspace
1326 +----------------------------------------------------------------------
1327 +In order to support multiple layers (branches), aufs readdir operation
1328 +constructs a virtual dir block on memory. For readdir, aufs calls
1329 +vfs_readdir() internally for each dir on branches, merges their entries
1330 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1331 +object. So the file object has its entry list until it is closed. The
1332 +entry list will be updated when the file position is zero and becomes
1333 +obsoleted. This decision is made in aufs automatically.
1334 +
1335 +The dynamically allocated memory block for the name of entries has a
1336 +unit of 512 bytes (by default) and stores the names contiguously (no
1337 +padding). Another block for each entry is handled by kmem_cache too.
1338 +During building dir blocks, aufs creates hash list and judging whether
1339 +the entry is whiteouted by its upper branch or already listed.
1340 +The merged result is cached in the corresponding inode object and
1341 +maintained by a customizable life-time option.
1342 +
1343 +Some people may call it can be a security hole or invite DoS attack
1344 +since the opened and once readdir-ed dir (file object) holds its entry
1345 +list and becomes a pressure for system memory. But I'd say it is similar
1346 +to files under /proc or /sys. The virtual files in them also holds a
1347 +memory page (generally) while they are opened. When an idea to reduce
1348 +memory for them is introduced, it will be applied to aufs too.
1349 +For those who really hate this situation, I've developed readdir(3)
1350 +library which operates this merging in userspace. You just need to set
1351 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1352 +kernel space for readdir(3).
1353 +
1354 +
1355 +Workqueue
1356 +----------------------------------------------------------------------
1357 +Aufs sometimes requires privilege access to a branch. For instance,
1358 +in copy-up/down operation. When a user process is going to make changes
1359 +to a file which exists in the lower readonly branch only, and the mode
1360 +of one of ancestor directories may not be writable by a user
1361 +process. Here aufs copy-up the file with its ancestors and they may
1362 +require privilege to set its owner/group/mode/etc.
1363 +This is a typical case of a application character of aufs (see
1364 +Introduction).
1365 +
1366 +Aufs uses workqueue synchronously for this case. It creates its own
1367 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1368 +passes the request to call mkdir or write (for example), and wait for
1369 +its completion. This approach solves a problem of a signal handler
1370 +simply.
1371 +If aufs didn't adopt the workqueue and changed the privilege of the
1372 +process, then the process may receive the unexpected SIGXFSZ or other
1373 +signals.
1374 +
1375 +Also aufs uses the system global workqueue ("events" kernel thread) too
1376 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1377 +whiteout base and etc. This is unrelated to a privilege.
1378 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1379 +superblock at the beginning, at the same time waits for the completion
1380 +of all queued asynchronous tasks.
1381 +
1382 +
1383 +Whiteout
1384 +----------------------------------------------------------------------
1385 +The whiteout in aufs is very similar to Unionfs's. That is represented
1386 +by its filename. UnionMount takes an approach of a file mode, but I am
1387 +afraid several utilities (find(1) or something) will have to support it.
1388 +
1389 +Basically the whiteout represents "logical deletion" which stops aufs to
1390 +lookup further, but also it represents "dir is opaque" which also stop
1391 +further lookup.
1392 +
1393 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1394 +In order to make several functions in a single systemcall to be
1395 +revertible, aufs adopts an approach to rename a directory to a temporary
1396 +unique whiteouted name.
1397 +For example, in rename(2) dir where the target dir already existed, aufs
1398 +renames the target dir to a temporary unique whiteouted name before the
1399 +actual rename on a branch, and then handles other actions (make it opaque,
1400 +update the attributes, etc). If an error happens in these actions, aufs
1401 +simply renames the whiteouted name back and returns an error. If all are
1402 +succeeded, aufs registers a function to remove the whiteouted unique
1403 +temporary name completely and asynchronously to the system global
1404 +workqueue.
1405 +
1406 +
1407 +Copy-up
1408 +----------------------------------------------------------------------
1409 +It is a well-known feature or concept.
1410 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1411 +internally and makes change to the new file on the upper writable branch.
1412 +When the trigger systemcall does not update the timestamps of the parent
1413 +dir, aufs reverts it after copy-up.
1414 +
1415 +
1416 +Move-down (aufs3.9 and later)
1417 +----------------------------------------------------------------------
1418 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1419 +the lower readonly branch to the upper writable branch when a user
1420 +changes something about the file.
1421 +"Move-down" is an opposite action of copy-up. Basically this action is
1422 +ran manually instead of automatically and internally.
1423 +For desgin and implementation, aufs has to consider these issues.
1424 +- whiteout for the file may exist on the lower branch.
1425 +- ancestor directories may not exist on the lower branch.
1426 +- diropq for the ancestor directories may exist on the upper branch.
1427 +- free space on the lower branch will reduce.
1428 +- another access to the file may happen during moving-down, including
1429 +  UDBA (see "Revalidate Dentry and UDBA").
1430 +- the file should not be hard-linked nor pseudo-linked. they should be
1431 +  handled by auplink utility later.
1432 +
1433 +Sometimes users want to move-down a file from the upper writable branch
1434 +to the lower readonly or writable branch. For instance,
1435 +- the free space of the upper writable branch is going to run out.
1436 +- create a new intermediate branch between the upper and lower branch.
1437 +- etc.
1438 +
1439 +For this purpose, use "aumvdown" command in aufs-util.git.
1440 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1441 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1442 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2022-11-05 23:02:18.955889283 +0100
1443 @@ -0,0 +1,85 @@
1444 +
1445 +# Copyright (C) 2015-2022 Junjiro R. Okajima
1446 +#
1447 +# This program is free software; you can redistribute it and/or modify
1448 +# it under the terms of the GNU General Public License as published by
1449 +# the Free Software Foundation; either version 2 of the License, or
1450 +# (at your option) any later version.
1451 +#
1452 +# This program is distributed in the hope that it will be useful,
1453 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1454 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1455 +# GNU General Public License for more details.
1456 +#
1457 +# You should have received a copy of the GNU General Public License
1458 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1459 +
1460 +Support for a branch who has its ->atomic_open()
1461 +----------------------------------------------------------------------
1462 +The filesystems who implement its ->atomic_open() are not majority. For
1463 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1464 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1465 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1466 +sure whether all filesystems who have ->atomic_open() behave like this,
1467 +but NFSv4 surely returns the error.
1468 +
1469 +In order to support ->atomic_open() for aufs, there are a few
1470 +approaches.
1471 +
1472 +A. Introduce aufs_atomic_open()
1473 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1474 +     branch fs.
1475 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1476 +   an aufs user Pip Cet's approach
1477 +   - calls aufs_create(), VFS finish_open() and notify_change().
1478 +   - pass fake-mode to finish_open(), and then correct the mode by
1479 +     notify_change().
1480 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1481 +   - no aufs_atomic_open().
1482 +   - aufs_lookup() registers the TID to an aufs internal object.
1483 +   - aufs_create() does nothing when the matching TID is registered, but
1484 +     registers the mode.
1485 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1486 +     TID is registered.
1487 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1488 +   credential
1489 +   - no aufs_atomic_open().
1490 +   - aufs_create() registers the TID to an internal object. this info
1491 +     represents "this process created this file just now."
1492 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1493 +     registered TID and re-try open() with superuser's credential.
1494 +
1495 +Pros and cons for each approach.
1496 +
1497 +A.
1498 +   - straightforward but highly depends upon VFS internal.
1499 +   - the atomic behavaiour is kept.
1500 +   - some of parameters such as nameidata are hard to reproduce for
1501 +     branch fs.
1502 +   - large overhead.
1503 +B.
1504 +   - easy to implement.
1505 +   - the atomic behavaiour is lost.
1506 +C.
1507 +   - the atomic behavaiour is kept.
1508 +   - dirty and tricky.
1509 +   - VFS checks whether the file is created correctly after calling
1510 +     ->create(), which means this approach doesn't work.
1511 +D.
1512 +   - easy to implement.
1513 +   - the atomic behavaiour is lost.
1514 +   - to open a file with superuser's credential and give it to a user
1515 +     process is a bad idea, since the file object keeps the credential
1516 +     in it. It may affect LSM or something. This approach doesn't work
1517 +     either.
1518 +
1519 +The approach A is ideal, but it hard to implement. So here is a
1520 +variation of A, which is to be implemented.
1521 +
1522 +A-1. Introduce aufs_atomic_open()
1523 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1524 +       vfs_create() and finish_open().
1525 +     - the demerit is that the several checks after branch fs
1526 +       ->atomic_open() are lost. in the ordinary case, the checks are
1527 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1528 +       be implemented in aufs, but not all I am afraid.
1529 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1530 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1531 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2022-11-05 23:02:18.959222617 +0100
1532 @@ -0,0 +1,113 @@
1533 +
1534 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1535 +#
1536 +# This program is free software; you can redistribute it and/or modify
1537 +# it under the terms of the GNU General Public License as published by
1538 +# the Free Software Foundation; either version 2 of the License, or
1539 +# (at your option) any later version.
1540 +#
1541 +# This program is distributed in the hope that it will be useful,
1542 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1543 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1544 +# GNU General Public License for more details.
1545 +#
1546 +# You should have received a copy of the GNU General Public License
1547 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1548 +
1549 +Lookup in a Branch
1550 +----------------------------------------------------------------------
1551 +Since aufs has a character of sub-VFS (see Introduction), it operates
1552 +lookup for branches as VFS does. It may be a heavy work. But almost all
1553 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1554 +directly connected to its parent. Digging down the directory hierarchy
1555 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1556 +aufs calls it.
1557 +
1558 +When a branch is a remote filesystem, aufs basically relies upon its
1559 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1560 +them.
1561 +For d_revalidate, aufs implements three levels of revalidate tests. See
1562 +"Revalidate Dentry and UDBA" in detail.
1563 +
1564 +
1565 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1566 +----------------------------------------------------------------------
1567 +Let's try case study.
1568 +- aufs has two branches, upper readwrite and lower readonly.
1569 +  /au = /rw + /ro
1570 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1571 +- user invoked "chmod a+rx /au/dirA"
1572 +- the internal copy-up is activated and "/rw/dirA" is created and its
1573 +  permission bits are set to world readable.
1574 +- then "/au/dirA" becomes world readable?
1575 +
1576 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1577 +or it may be a natively readonly filesystem. If aufs respects the lower
1578 +branch, it should not respond readdir request from other users. But user
1579 +allowed it by chmod. Should really aufs rejects showing the entries
1580 +under /ro/dirA?
1581 +
1582 +To be honest, I don't have a good solution for this case. So aufs
1583 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1584 +users.
1585 +When dirperm1 is specified, aufs checks only the highest one for the
1586 +directory permission, and shows the entries. Otherwise, as usual, checks
1587 +every dir existing on all branches and rejects the request.
1588 +
1589 +As a side effect, dirperm1 option improves the performance of aufs
1590 +because the number of permission check is reduced when the number of
1591 +branch is many.
1592 +
1593 +
1594 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1595 +----------------------------------------------------------------------
1596 +Generally VFS helpers re-validate a dentry as a part of lookup.
1597 +0. digging down the directory hierarchy.
1598 +1. lock the parent dir by its i_mutex.
1599 +2. lookup the final (child) entry.
1600 +3. revalidate it.
1601 +4. call the actual operation (create, unlink, etc.)
1602 +5. unlock the parent dir
1603 +
1604 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1605 +called. Actually aufs implements it and checks the dentry on a branch is
1606 +still valid.
1607 +But it is not enough. Because aufs has to release the lock for the
1608 +parent dir on a branch at the end of ->lookup() (step 2) and
1609 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1610 +held by VFS.
1611 +If the file on a branch is changed directly, eg. bypassing aufs, after
1612 +aufs released the lock, then the subsequent operation may cause
1613 +something unpleasant result.
1614 +
1615 +This situation is a result of VFS architecture, ->lookup() and
1616 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1617 +design from VFS's point of view. It is just not suitable for sub-VFS
1618 +character in aufs.
1619 +
1620 +Aufs supports such case by three level of revalidation which is
1621 +selectable by user.
1622 +1. Simple Revalidate
1623 +   Addition to the native flow in VFS's, confirm the child-parent
1624 +   relationship on the branch just after locking the parent dir on the
1625 +   branch in the "actual operation" (step 4). When this validation
1626 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1627 +   checks the validation of the dentry on branches.
1628 +2. Monitor Changes Internally by Inotify/Fsnotify
1629 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1630 +   the dentry on the branch, and returns EBUSY if it finds different
1631 +   dentry.
1632 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1633 +   during it is in cache. When the event is notified, aufs registers a
1634 +   function to kernel 'events' thread by schedule_work(). And the
1635 +   function sets some special status to the cached aufs dentry and inode
1636 +   private data. If they are not cached, then aufs has nothing to
1637 +   do. When the same file is accessed through aufs (step 0-3) later,
1638 +   aufs will detect the status and refresh all necessary data.
1639 +   In this mode, aufs has to ignore the event which is fired by aufs
1640 +   itself.
1641 +3. No Extra Validation
1642 +   This is the simplest test and doesn't add any additional revalidation
1643 +   test, and skip the revalidation in step 4. It is useful and improves
1644 +   aufs performance when system surely hide the aufs branches from user,
1645 +   by over-mounting something (or another method).
1646 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1647 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1648 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2022-11-05 23:02:18.959222617 +0100
1649 @@ -0,0 +1,74 @@
1650 +
1651 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1652 +#
1653 +# This program is free software; you can redistribute it and/or modify
1654 +# it under the terms of the GNU General Public License as published by
1655 +# the Free Software Foundation; either version 2 of the License, or
1656 +# (at your option) any later version.
1657 +#
1658 +# This program is distributed in the hope that it will be useful,
1659 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1660 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1661 +# GNU General Public License for more details.
1662 +#
1663 +# You should have received a copy of the GNU General Public License
1664 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1665 +
1666 +Branch Manipulation
1667 +
1668 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1669 +and changing its permission/attribute, there are a lot of works to do.
1670 +
1671 +
1672 +Add a Branch
1673 +----------------------------------------------------------------------
1674 +o Confirm the adding dir exists outside of aufs, including loopback
1675 +  mount, and its various attributes.
1676 +o Initialize the xino file and whiteout bases if necessary.
1677 +  See struct.txt.
1678 +
1679 +o Check the owner/group/mode of the directory
1680 +  When the owner/group/mode of the adding directory differs from the
1681 +  existing branch, aufs issues a warning because it may impose a
1682 +  security risk.
1683 +  For example, when a upper writable branch has a world writable empty
1684 +  top directory, a malicious user can create any files on the writable
1685 +  branch directly, like copy-up and modify manually. If something like
1686 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1687 +  writable branch, and the writable branch is world-writable, then a
1688 +  malicious guy may create /etc/passwd on the writable branch directly
1689 +  and the infected file will be valid in aufs.
1690 +  I am afraid it can be a security issue, but aufs can do nothing except
1691 +  producing a warning.
1692 +
1693 +
1694 +Delete a Branch
1695 +----------------------------------------------------------------------
1696 +o Confirm the deleting branch is not busy
1697 +  To be general, there is one merit to adopt "remount" interface to
1698 +  manipulate branches. It is to discard caches. At deleting a branch,
1699 +  aufs checks the still cached (and connected) dentries and inodes. If
1700 +  there are any, then they are all in-use. An inode without its
1701 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1702 +
1703 +  For the cached one, aufs checks whether the same named entry exists on
1704 +  other branches.
1705 +  If the cached one is a directory, because aufs provides a merged view
1706 +  to users, as long as one dir is left on any branch aufs can show the
1707 +  dir to users. In this case, the branch can be removed from aufs.
1708 +  Otherwise aufs rejects deleting the branch.
1709 +
1710 +  If any file on the deleting branch is opened by aufs, then aufs
1711 +  rejects deleting.
1712 +
1713 +
1714 +Modify the Permission of a Branch
1715 +----------------------------------------------------------------------
1716 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1717 +  See struct.txt.
1718 +
1719 +o rw --> ro: Confirm the modifying branch is not busy
1720 +  Aufs rejects the request if any of these conditions are true.
1721 +  - a file on the branch is mmap-ed.
1722 +  - a regular file on the branch is opened for write and there is no
1723 +    same named entry on the upper branch.
1724 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1725 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1726 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2022-11-05 23:02:18.959222617 +0100
1727 @@ -0,0 +1,64 @@
1728 +
1729 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1730 +#
1731 +# This program is free software; you can redistribute it and/or modify
1732 +# it under the terms of the GNU General Public License as published by
1733 +# the Free Software Foundation; either version 2 of the License, or
1734 +# (at your option) any later version.
1735 +#
1736 +# This program is distributed in the hope that it will be useful,
1737 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1738 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1739 +# GNU General Public License for more details.
1740 +#
1741 +# You should have received a copy of the GNU General Public License
1742 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1743 +
1744 +Policies to Select One among Multiple Writable Branches
1745 +----------------------------------------------------------------------
1746 +When the number of writable branch is more than one, aufs has to decide
1747 +the target branch for file creation or copy-up. By default, the highest
1748 +writable branch which has the parent (or ancestor) dir of the target
1749 +file is chosen (top-down-parent policy).
1750 +By user's request, aufs implements some other policies to select the
1751 +writable branch, for file creation several policies, round-robin,
1752 +most-free-space, and other policies. For copy-up, top-down-parent,
1753 +bottom-up-parent, bottom-up and others.
1754 +
1755 +As expected, the round-robin policy selects the branch in circular. When
1756 +you have two writable branches and creates 10 new files, 5 files will be
1757 +created for each branch. mkdir(2) systemcall is an exception. When you
1758 +create 10 new directories, all will be created on the same branch.
1759 +And the most-free-space policy selects the one which has most free
1760 +space among the writable branches. The amount of free space will be
1761 +checked by aufs internally, and users can specify its time interval.
1762 +
1763 +The policies for copy-up is more simple,
1764 +top-down-parent is equivalent to the same named on in create policy,
1765 +bottom-up-parent selects the writable branch where the parent dir
1766 +exists and the nearest upper one from the copyup-source,
1767 +bottom-up selects the nearest upper writable branch from the
1768 +copyup-source, regardless the existence of the parent dir.
1769 +
1770 +There are some rules or exceptions to apply these policies.
1771 +- If there is a readonly branch above the policy-selected branch and
1772 +  the parent dir is marked as opaque (a variation of whiteout), or the
1773 +  target (creating) file is whiteout-ed on the upper readonly branch,
1774 +  then the result of the policy is ignored and the target file will be
1775 +  created on the nearest upper writable branch than the readonly branch.
1776 +- If there is a writable branch above the policy-selected branch and
1777 +  the parent dir is marked as opaque or the target file is whiteouted
1778 +  on the branch, then the result of the policy is ignored and the target
1779 +  file will be created on the highest one among the upper writable
1780 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1781 +  it as usual.
1782 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1783 +  They try selecting the branch where the source exists as possible
1784 +  since copyup a large file will take long time. If it can't be,
1785 +  ie. the branch where the source exists is readonly, then they will
1786 +  follow the copyup policy.
1787 +- There is an exception for rename(2) when the target exists.
1788 +  If the rename target exists, aufs compares the index of the branches
1789 +  where the source and the target exists and selects the higher
1790 +  one. If the selected branch is readonly, then aufs follows the
1791 +  copyup policy.
1792 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1793 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1794 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2022-11-05 23:02:18.959222617 +0100
1795 @@ -0,0 +1,44 @@
1796 +
1797 +// to view this graph, run dot(1) command in GRAPHVIZ.
1798 +//
1799 +// This program is free software; you can redistribute it and/or modify
1800 +// it under the terms of the GNU General Public License as published by
1801 +// the Free Software Foundation; either version 2 of the License, or
1802 +// (at your option) any later version.
1803 +//
1804 +// This program is distributed in the hope that it will be useful,
1805 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
1806 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1807 +// GNU General Public License for more details.
1808 +//
1809 +// You should have received a copy of the GNU General Public License
1810 +// along with this program.  If not, see <http://www.gnu.org/licenses/>.
1811 +
1812 +digraph G {
1813 +node [shape=box];
1814 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1815 +
1816 +node [shape=oval];
1817 +
1818 +aufs_rename -> whinfo [label="store/remove"];
1819 +
1820 +node [shape=oval];
1821 +inode_list [label="h_inum list in branch\ncache"];
1822 +
1823 +node [shape=box];
1824 +whinode [label="h_inum list file"];
1825 +
1826 +node [shape=oval];
1827 +brmgmt [label="br_add/del/mod/umount"];
1828 +
1829 +brmgmt -> inode_list [label="create/remove"];
1830 +brmgmt -> whinode [label="load/store"];
1831 +
1832 +inode_list -> whinode [style=dashed,dir=both];
1833 +
1834 +aufs_rename -> inode_list [label="add/del"];
1835 +
1836 +aufs_lookup -> inode_list [label="search"];
1837 +
1838 +aufs_lookup -> whinfo [label="load/remove"];
1839 +}
1840 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1841 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1842 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2022-11-05 23:02:18.959222617 +0100
1843 @@ -0,0 +1,102 @@
1844 +
1845 +# Copyright (C) 2017-2022 Junjiro R. Okajima
1846 +#
1847 +# This program is free software; you can redistribute it and/or modify
1848 +# it under the terms of the GNU General Public License as published by
1849 +# the Free Software Foundation; either version 2 of the License, or
1850 +# (at your option) any later version.
1851 +#
1852 +# This program is distributed in the hope that it will be useful,
1853 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1854 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1855 +# GNU General Public License for more details.
1856 +#
1857 +# You should have received a copy of the GNU General Public License
1858 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1859 +
1860 +Special handling for renaming a directory (DIRREN)
1861 +----------------------------------------------------------------------
1862 +First, let's assume we have a simple usecase.
1863 +
1864 +- /u = /rw + /ro
1865 +- /rw/dirA exists
1866 +- /ro/dirA and /ro/dirA/file exist too
1867 +- there is no dirB on both branches
1868 +- a user issues rename("dirA", "dirB")
1869 +
1870 +Now, what should aufs behave against this rename(2)?
1871 +There are a few possible cases.
1872 +
1873 +A. returns EROFS.
1874 +   since dirA exists on a readonly branch which cannot be renamed.
1875 +B. returns EXDEV.
1876 +   it is possible to copy-up dirA (only the dir itself), but the child
1877 +   entries ("file" in this case) should not be. it must be a bad
1878 +   approach to copy-up recursively.
1879 +C. returns a success.
1880 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1881 +   is a violation of aufs' policy.
1882 +D. construct an extra information which indicates that /ro/dirA should
1883 +   be handled as the name of dirB.
1884 +   overlayfs has a similar feature called REDIRECT.
1885 +
1886 +Until now, aufs implements the case B only which returns EXDEV, and
1887 +expects the userspace application behaves like mv(1) which tries
1888 +issueing rename(2) recursively.
1889 +
1890 +A new aufs feature called DIRREN is introduced which implements the case
1891 +D. There are several "extra information" added.
1892 +
1893 +1. detailed info per renamed directory
1894 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1895 +2. the inode-number list of directories on a branch
1896 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1897 +
1898 +The filename of "detailed info per directory" represents the lower
1899 +branch, and its format is
1900 +- a type of the branch id
1901 +  one of these.
1902 +  + uuid (not implemented yet)
1903 +  + fsid
1904 +  + dev
1905 +- the inode-number of the branch root dir
1906 +
1907 +And it contains these info in a single regular file.
1908 +- magic number
1909 +- branch's inode-number of the logically renamed dir
1910 +- the name of the before-renamed dir
1911 +
1912 +The "detailed info per directory" file is created in aufs rename(2), and
1913 +loaded in any lookup.
1914 +The info is considered in lookup for the matching case only. Here
1915 +"matching" means that the root of branch (in the info filename) is same
1916 +to the current looking-up branch. After looking-up the before-renamed
1917 +name, the inode-number is compared. And the matched dentry is used.
1918 +
1919 +The "inode-number list of directories" is a regular file which contains
1920 +simply the inode-numbers on the branch. The file is created or updated
1921 +in removing the branch, and loaded in adding the branch. Its lifetime is
1922 +equal to the branch.
1923 +The list is referred in lookup, and when the current target inode is
1924 +found in the list, the aufs tries loading the "detailed info per
1925 +directory" and get the changed and valid name of the dir.
1926 +
1927 +Theoretically these "extra informaiton" may be able to be put into XATTR
1928 +in the dir inode. But aufs doesn't choose this way because
1929 +1. XATTR may not be supported by the branch (or its configuration)
1930 +2. XATTR may have its size limit.
1931 +3. XATTR may be less easy to convert than a regular file, when the
1932 +   format of the info is changed in the future.
1933 +At the same time, I agree that the regular file approach is much slower
1934 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1935 +better approach.
1936 +
1937 +This DIRREN feature is enabled by aufs configuration, and is activated
1938 +by a new mount option.
1939 +
1940 +For the more complicated case, there is a work with UDBA option, which
1941 +is to dected the direct access to the branches (by-passing aufs) and to
1942 +maintain the cashes in aufs. Since a single cached aufs dentry may
1943 +contains two names, before- and after-rename, the name comparision in
1944 +UDBA handler may not work correctly. In this case, the behaviour will be
1945 +equivalen to udba=reval case.
1946 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1947 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1948 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2022-11-05 23:02:18.959222617 +0100
1949 @@ -0,0 +1,118 @@
1950 +
1951 +# Copyright (C) 2011-2022 Junjiro R. Okajima
1952 +#
1953 +# This program is free software; you can redistribute it and/or modify
1954 +# it under the terms of the GNU General Public License as published by
1955 +# the Free Software Foundation; either version 2 of the License, or
1956 +# (at your option) any later version.
1957 +#
1958 +# This program is distributed in the hope that it will be useful,
1959 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1960 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1961 +# GNU General Public License for more details.
1962 +#
1963 +# You should have received a copy of the GNU General Public License
1964 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1965 +
1966 +File-based Hierarchical Storage Management (FHSM)
1967 +----------------------------------------------------------------------
1968 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1969 +storage world. Aufs provides this feature as file-based with multiple
1970 +writable branches, based upon the principle of "Colder, the Lower".
1971 +Here the word "colder" means that the less used files, and "lower" means
1972 +that the position in the order of the stacked branches vertically.
1973 +These multiple writable branches are prioritized, ie. the topmost one
1974 +should be the fastest drive and be used heavily.
1975 +
1976 +o Characters in aufs FHSM story
1977 +- aufs itself and a new branch attribute.
1978 +- a new ioctl interface to move-down and to establish a connection with
1979 +  the daemon ("move-down" is a converse of "copy-up").
1980 +- userspace tool and daemon.
1981 +
1982 +The userspace daemon establishes a connection with aufs and waits for
1983 +the notification. The notified information is very similar to struct
1984 +statfs containing the number of consumed blocks and inodes.
1985 +When the consumed blocks/inodes of a branch exceeds the user-specified
1986 +upper watermark, the daemon activates its move-down process until the
1987 +consumed blocks/inodes reaches the user-specified lower watermark.
1988 +
1989 +The actual move-down is done by aufs based upon the request from
1990 +user-space since we need to maintain the inode number and the internal
1991 +pointer arrays in aufs.
1992 +
1993 +Currently aufs FHSM handles the regular files only. Additionally they
1994 +must not be hard-linked nor pseudo-linked.
1995 +
1996 +
1997 +o Cowork of aufs and the user-space daemon
1998 +  During the userspace daemon established the connection, aufs sends a
1999 +  small notification to it whenever aufs writes something into the
2000 +  writable branch. But it may cost high since aufs issues statfs(2)
2001 +  internally. So user can specify a new option to cache the
2002 +  info. Actually the notification is controlled by these factors.
2003 +  + the specified cache time.
2004 +  + classified as "force" by aufs internally.
2005 +  Until the specified time expires, aufs doesn't send the info
2006 +  except the forced cases. When aufs decide forcing, the info is always
2007 +  notified to userspace.
2008 +  For example, the number of free inodes is generally large enough and
2009 +  the shortage of it happens rarely. So aufs doesn't force the
2010 +  notification when creating a new file, directory and others. This is
2011 +  the typical case which aufs doesn't force.
2012 +  When aufs writes the actual filedata and the files consumes any of new
2013 +  blocks, the aufs forces notifying.
2014 +
2015 +
2016 +o Interfaces in aufs
2017 +- New branch attribute.
2018 +  + fhsm
2019 +    Specifies that the branch is managed by FHSM feature. In other word,
2020 +    participant in the FHSM.
2021 +    When nofhsm is set to the branch, it will not be the source/target
2022 +    branch of the move-down operation. This attribute is set
2023 +    independently from coo and moo attributes, and if you want full
2024 +    FHSM, you should specify them as well.
2025 +- New mount option.
2026 +  + fhsm_sec
2027 +    Specifies a second to suppress many less important info to be
2028 +    notified.
2029 +- New ioctl.
2030 +  + AUFS_CTL_FHSM_FD
2031 +    create a new file descriptor which userspace can read the notification
2032 +    (a subset of struct statfs) from aufs.
2033 +- Module parameter 'brs'
2034 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2035 +  be set.
2036 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2037 +  When there are two or more branches with fhsm attributes,
2038 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2039 +  terminates it. As a result of remounting and branch-manipulation, the
2040 +  number of branches with fhsm attribute can be one. In this case,
2041 +  /sbin/mount.aufs will terminate the user-space daemon.
2042 +
2043 +
2044 +Finally the operation is done as these steps in kernel-space.
2045 +- make sure that,
2046 +  + no one else is using the file.
2047 +  + the file is not hard-linked.
2048 +  + the file is not pseudo-linked.
2049 +  + the file is a regular file.
2050 +  + the parent dir is not opaqued.
2051 +- find the target writable branch.
2052 +- make sure the file is not whiteout-ed by the upper (than the target)
2053 +  branch.
2054 +- make the parent dir on the target branch.
2055 +- mutex lock the inode on the branch.
2056 +- unlink the whiteout on the target branch (if exists).
2057 +- lookup and create the whiteout-ed temporary name on the target branch.
2058 +- copy the file as the whiteout-ed temporary name on the target branch.
2059 +- rename the whiteout-ed temporary name to the original name.
2060 +- unlink the file on the source branch.
2061 +- maintain the internal pointer array and the external inode number
2062 +  table (XINO).
2063 +- maintain the timestamps and other attributes of the parent dir and the
2064 +  file.
2065 +
2066 +And of course, in every step, an error may happen. So the operation
2067 +should restore the original file state after an error happens.
2068 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2069 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2070 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2022-11-05 23:02:18.959222617 +0100
2071 @@ -0,0 +1,72 @@
2072 +
2073 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2074 +#
2075 +# This program is free software; you can redistribute it and/or modify
2076 +# it under the terms of the GNU General Public License as published by
2077 +# the Free Software Foundation; either version 2 of the License, or
2078 +# (at your option) any later version.
2079 +#
2080 +# This program is distributed in the hope that it will be useful,
2081 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2082 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2083 +# GNU General Public License for more details.
2084 +#
2085 +# You should have received a copy of the GNU General Public License
2086 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2087 +
2088 +mmap(2) -- File Memory Mapping
2089 +----------------------------------------------------------------------
2090 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2091 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2092 +->mmap().
2093 +This approach is simple and good, but there is one problem.
2094 +Under /proc, several entries show the mmapped files by its path (with
2095 +device and inode number), and the printed path will be the path on the
2096 +branch fs's instead of virtual aufs's.
2097 +This is not a problem in most cases, but some utilities lsof(1) (and its
2098 +user) may expect the path on aufs.
2099 +
2100 +To address this issue, aufs adds a new member called vm_prfile in struct
2101 +vm_area_struct (and struct vm_region). The original vm_file points to
2102 +the file on the branch fs in order to handle everything correctly as
2103 +usual. The new vm_prfile points to a virtual file in aufs, and the
2104 +show-functions in procfs refers to vm_prfile if it is set.
2105 +Also we need to maintain several other places where touching vm_file
2106 +such like
2107 +- fork()/clone() copies vma and the reference count of vm_file is
2108 +  incremented.
2109 +- merging vma maintains the ref count too.
2110 +
2111 +This is not a good approach. It just fakes the printed path. But it
2112 +leaves all behaviour around f_mapping unchanged. This is surely an
2113 +advantage.
2114 +Actually aufs had adopted another complicated approach which calls
2115 +generic_file_mmap() and handles struct vm_operations_struct. In this
2116 +approach, aufs met a hard problem and I could not solve it without
2117 +switching the approach.
2118 +
2119 +There may be one more another approach which is
2120 +- bind-mount the branch-root onto the aufs-root internally
2121 +- grab the new vfsmount (ie. struct mount)
2122 +- lazy-umount the branch-root internally
2123 +- in open(2) the aufs-file, open the branch-file with the hidden
2124 +  vfsmount (instead of the original branch's vfsmount)
2125 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2126 +  but it may be possible from userspace by the mount helper.
2127 +
2128 +Adding the internal hidden vfsmount and using it in opening a file, the
2129 +file path under /proc will be printed correctly. This approach looks
2130 +smarter, but is not possible I am afraid.
2131 +- aufs-root may be bind-mount later. when it happens, another hidden
2132 +  vfsmount will be required.
2133 +- it is hard to get the chance to bind-mount and lazy-umount
2134 +  + in kernel-space, FS can have vfsmount in open(2) via
2135 +    file->f_path, and aufs can know its vfsmount. But several locks are
2136 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2137 +    here, then it may cause a deadlock.
2138 +  + in user-space, bind-mount doesn't invoke the mount helper.
2139 +- since /proc shows dev and ino, aufs has to give vma these info. it
2140 +  means a new member vm_prinode will be necessary. this is essentially
2141 +  equivalent to vm_prfile described above.
2142 +
2143 +I have to give up this "looks-smater" approach.
2144 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2145 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2146 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2022-11-05 23:02:18.959222617 +0100
2147 @@ -0,0 +1,94 @@
2148 +
2149 +# Copyright (C) 2014-2022 Junjiro R. Okajima
2150 +#
2151 +# This program is free software; you can redistribute it and/or modify
2152 +# it under the terms of the GNU General Public License as published by
2153 +# the Free Software Foundation; either version 2 of the License, or
2154 +# (at your option) any later version.
2155 +#
2156 +# This program is distributed in the hope that it will be useful,
2157 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2158 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2159 +# GNU General Public License for more details.
2160 +#
2161 +# You should have received a copy of the GNU General Public License
2162 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2163 +
2164 +Listing XATTR/EA and getting the value
2165 +----------------------------------------------------------------------
2166 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2167 +shows the values from the topmost existing file. This behaviour is good
2168 +for the non-dir entries since the bahaviour exactly matches the shown
2169 +information. But for the directories, aufs considers all the same named
2170 +entries on the lower branches. Which means, if one of the lower entry
2171 +rejects readdir call, then aufs returns an error even if the topmost
2172 +entry allows it. This behaviour is necessary to respect the branch fs's
2173 +security, but can make users confused since the user-visible standard
2174 +attributes don't match the behaviour.
2175 +To address this issue, aufs has a mount option called dirperm1 which
2176 +checks the permission for the topmost entry only, and ignores the lower
2177 +entry's permission.
2178 +
2179 +A similar issue can happen around XATTR.
2180 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2181 +always set. Otherwise these very unpleasant situation would happen.
2182 +- listxattr(2) may return the duplicated entries.
2183 +- users may not be able to remove or reset the XATTR forever,
2184 +
2185 +
2186 +XATTR/EA support in the internal (copy,move)-(up,down)
2187 +----------------------------------------------------------------------
2188 +Generally the extended attributes of inode are categorized as these.
2189 +- "security" for LSM and capability.
2190 +- "system" for posix ACL, 'acl' mount option is required for the branch
2191 +  fs generally.
2192 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2193 +- "user" for userspace, 'user_xattr' mount option is required for the
2194 +  branch fs generally.
2195 +
2196 +Moreover there are some other categories. Aufs handles these rather
2197 +unpopular categories as the ordinary ones, ie. there is no special
2198 +condition nor exception.
2199 +
2200 +In copy-up, the support for XATTR on the dst branch may differ from the
2201 +src branch. In this case, the copy-up operation will get an error and
2202 +the original user operation which triggered the copy-up will fail. It
2203 +can happen that even all copy-up will fail.
2204 +When both of src and dst branches support XATTR and if an error occurs
2205 +during copying XATTR, then the copy-up should fail obviously. That is a
2206 +good reason and aufs should return an error to userspace. But when only
2207 +the src branch support that XATTR, aufs should not return an error.
2208 +For example, the src branch supports ACL but the dst branch doesn't
2209 +because the dst branch may natively un-support it or temporary
2210 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2211 +may NOT return an error even if the XATTR is not supported. It is
2212 +totally up to the branch fs.
2213 +
2214 +Anyway when the aufs internal copy-up gets an error from the dst branch
2215 +fs, then aufs tries removing the just copied entry and returns the error
2216 +to the userspace. The worst case of this situation will be all copy-up
2217 +will fail.
2218 +
2219 +For the copy-up operation, there two basic approaches.
2220 +- copy the specified XATTR only (by category above), and return the
2221 +  error unconditionally if it happens.
2222 +- copy all XATTR, and ignore the error on the specified category only.
2223 +
2224 +In order to support XATTR and to implement the correct behaviour, aufs
2225 +chooses the latter approach and introduces some new branch attributes,
2226 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2227 +They correspond to the XATTR namespaces (see above). Additionally, to be
2228 +convenient, "icex" is also provided which means all "icex*" attributes
2229 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2230 +
2231 +The meaning of these attributes is to ignore the error from setting
2232 +XATTR on that branch.
2233 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2234 +error from the dst branch according to the specified attributes.
2235 +
2236 +Some XATTR may have its default value. The default value may come from
2237 +the parent dir or the environment. If the default value is set at the
2238 +file creating-time, it will be overwritten by copy-up.
2239 +Some contradiction may happen I am afraid.
2240 +Do we need another attribute to stop copying XATTR? I am unsure. For
2241 +now, aufs implements the branch attributes to ignore the error.
2242 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2243 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2244 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2022-11-05 23:02:18.959222617 +0100
2245 @@ -0,0 +1,58 @@
2246 +
2247 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2248 +#
2249 +# This program is free software; you can redistribute it and/or modify
2250 +# it under the terms of the GNU General Public License as published by
2251 +# the Free Software Foundation; either version 2 of the License, or
2252 +# (at your option) any later version.
2253 +#
2254 +# This program is distributed in the hope that it will be useful,
2255 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2256 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2257 +# GNU General Public License for more details.
2258 +#
2259 +# You should have received a copy of the GNU General Public License
2260 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2261 +
2262 +Export Aufs via NFS
2263 +----------------------------------------------------------------------
2264 +Here is an approach.
2265 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2266 +  generation.
2267 +- iget_locked(): initialize aufs inode generation for a new inode, and
2268 +  store it in xigen file.
2269 +- destroy_inode(): increment aufs inode generation and store it in xigen
2270 +  file. it is necessary even if it is not unlinked, because any data of
2271 +  inode may be changed by UDBA.
2272 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2273 +  build file handle by
2274 +  + branch id (4 bytes)
2275 +  + superblock generation (4 bytes)
2276 +  + inode number (4 or 8 bytes)
2277 +  + parent dir inode number (4 or 8 bytes)
2278 +  + inode generation (4 bytes))
2279 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2280 +    bytes)
2281 +  + file handle for a branch (by exportfs_encode_fh())
2282 +- fh_to_dentry():
2283 +  + find the index of a branch from its id in handle, and check it is
2284 +    still exist in aufs.
2285 +  + 1st level: get the inode number from handle and search it in cache.
2286 +  + 2nd level: if not found in cache, get the parent inode number from
2287 +    the handle and search it in cache. and then open the found parent
2288 +    dir, find the matching inode number by vfs_readdir() and get its
2289 +    name, and call lookup_one_len() for the target dentry.
2290 +  + 3rd level: if the parent dir is not cached, call
2291 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2292 +    build a pathname of it, convert it a pathname in aufs, call
2293 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2294 +    the 2nd level.
2295 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2296 +    for every branch, but not itself. to get this, (currently) aufs
2297 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2298 +    idea, but I didn't get other approach.
2299 +  + test the generation of the gotten inode.
2300 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2301 +  convert it into ESTALE for NFSD.
2302 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2303 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2304 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2305 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2306 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2022-11-05 23:02:18.959222617 +0100
2307 @@ -0,0 +1,52 @@
2308 +
2309 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2310 +#
2311 +# This program is free software; you can redistribute it and/or modify
2312 +# it under the terms of the GNU General Public License as published by
2313 +# the Free Software Foundation; either version 2 of the License, or
2314 +# (at your option) any later version.
2315 +#
2316 +# This program is distributed in the hope that it will be useful,
2317 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2318 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2319 +# GNU General Public License for more details.
2320 +#
2321 +# You should have received a copy of the GNU General Public License
2322 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2323 +
2324 +Show Whiteout Mode (shwh)
2325 +----------------------------------------------------------------------
2326 +Generally aufs hides the name of whiteouts. But in some cases, to show
2327 +them is very useful for users. For instance, creating a new middle layer
2328 +(branch) by merging existing layers.
2329 +
2330 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2331 +When you have three branches,
2332 +- Bottom: 'system', squashfs (underlying base system), read-only
2333 +- Middle: 'mods', squashfs, read-only
2334 +- Top: 'overlay', ram (tmpfs), read-write
2335 +
2336 +The top layer is loaded at boot time and saved at shutdown, to preserve
2337 +the changes made to the system during the session.
2338 +When larger changes have been made, or smaller changes have accumulated,
2339 +the size of the saved top layer data grows. At this point, it would be
2340 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2341 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2342 +restoring save and load speed.
2343 +
2344 +This merging is simplified by the use of another aufs mount, of just the
2345 +two overlay branches using the 'shwh' option.
2346 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2347 +       aufs /livesys/merge_union
2348 +
2349 +A merged view of these two branches is then available at
2350 +/livesys/merge_union, and the new feature is that the whiteouts are
2351 +visible!
2352 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2353 +writing to all branches. Also the default mode for all branches is 'ro'.
2354 +It is now possible to save the combined contents of the two overlay
2355 +branches to a new squashfs, e.g.:
2356 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2357 +
2358 +This new squashfs archive can be stored on the boot device and the
2359 +initramfs will use it to replace the old one at the next boot.
2360 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2361 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2362 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2022-11-05 23:02:18.959222617 +0100
2363 @@ -0,0 +1,47 @@
2364 +
2365 +# Copyright (C) 2010-2022 Junjiro R. Okajima
2366 +#
2367 +# This program is free software; you can redistribute it and/or modify
2368 +# it under the terms of the GNU General Public License as published by
2369 +# the Free Software Foundation; either version 2 of the License, or
2370 +# (at your option) any later version.
2371 +#
2372 +# This program is distributed in the hope that it will be useful,
2373 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2374 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2375 +# GNU General Public License for more details.
2376 +#
2377 +# You should have received a copy of the GNU General Public License
2378 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2379 +
2380 +Dynamically customizable FS operations
2381 +----------------------------------------------------------------------
2382 +Generally FS operations (struct inode_operations, struct
2383 +address_space_operations, struct file_operations, etc.) are defined as
2384 +"static const", but it never means that FS have only one set of
2385 +operation. Some FS have multiple sets of them. For instance, ext2 has
2386 +three sets, one for XIP, for NOBH, and for normal.
2387 +Since aufs overrides and redirects these operations, sometimes aufs has
2388 +to change its behaviour according to the branch FS type. More importantly
2389 +VFS acts differently if a function (member in the struct) is set or
2390 +not. It means aufs should have several sets of operations and select one
2391 +among them according to the branch FS definition.
2392 +
2393 +In order to solve this problem and not to affect the behaviour of VFS,
2394 +aufs defines these operations dynamically. For instance, aufs defines
2395 +dummy direct_IO function for struct address_space_operations, but it may
2396 +not be set to the address_space_operations actually. When the branch FS
2397 +doesn't have it, aufs doesn't set it to its address_space_operations
2398 +while the function definition itself is still alive. So the behaviour
2399 +itself will not change, and it will return an error when direct_IO is
2400 +not set.
2401 +
2402 +The lifetime of these dynamically generated operation object is
2403 +maintained by aufs branch object. When the branch is removed from aufs,
2404 +the reference counter of the object is decremented. When it reaches
2405 +zero, the dynamically generated operation object will be freed.
2406 +
2407 +This approach is designed to support AIO (io_submit), Direct I/O and
2408 +XIP (DAX) mainly.
2409 +Currently this approach is applied to address_space_operations for
2410 +regular files only.
2411 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2412 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2413 +++ linux/Documentation/filesystems/aufs/README 2023-09-02 12:00:06.376642958 +0200
2414 @@ -0,0 +1,409 @@
2415 +
2416 +Aufs6 -- advanced multi layered unification filesystem version 6.x
2417 +http://aufs.sf.net
2418 +Junjiro R. Okajima
2419 +
2420 +
2421 +0. Introduction
2422 +----------------------------------------
2423 +In the early days, aufs was entirely re-designed and re-implemented
2424 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2425 +improvements and implementations, it became totally different from
2426 +Unionfs while keeping the basic features.
2427 +Later, Unionfs Version 2.x series began taking some of the same
2428 +approaches to aufs1's.
2429 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2430 +University and his team.
2431 +
2432 +Aufs6 supports linux-v6.0 and later, try aufs6.0 branch in
2433 +aufs-linux.git or aufs-standalone.git.
2434 +If you want older kernel version support,
2435 +- for linux-v5.x series, try aufs-linux.git or aufs-standalone.git
2436 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2437 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2438 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2439 +  or aufs1 from CVS on SourceForge.
2440 +
2441 +Note: the name of aufs5-linux.git and aufs5-standalone.git on github
2442 +      were changed. Now they are aufs-linux.git and
2443 +      aufs-standalone.git and they contain aufs5 and later branches.
2444 +
2445 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2446 +      According to Christoph Hellwig, linux rejects all union-type
2447 +      filesystems but UnionMount.
2448 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2449 +
2450 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2451 +    UnionMount, and he pointed out an issue around a directory mutex
2452 +    lock and aufs addressed it. But it is still unsure whether aufs will
2453 +    be merged (or any other union solution).
2454 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2455 +
2456 +
2457 +1. Features
2458 +----------------------------------------
2459 +- unite several directories into a single virtual filesystem. The member
2460 +  directory is called as a branch.
2461 +- you can specify the permission flags to the branch, which are 'readonly',
2462 +  'readwrite' and 'whiteout-able.'
2463 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2464 +  readonly branch are modifiable logically.
2465 +- dynamic branch manipulation, add, del.
2466 +- etc...
2467 +
2468 +Also there are many enhancements in aufs, such as:
2469 +- test only the highest one for the directory permission (dirperm1)
2470 +- copyup on open (coo=)
2471 +- 'move' policy for copy-up between two writable branches, after
2472 +  checking free space.
2473 +- xattr, acl
2474 +- readdir(3) in userspace.
2475 +- keep inode number by external inode number table
2476 +- keep the timestamps of file/dir in internal copyup operation
2477 +- seekable directory, supporting NFS readdir.
2478 +- whiteout is hardlinked in order to reduce the consumption of inodes
2479 +  on branch
2480 +- do not copyup, nor create a whiteout when it is unnecessary
2481 +- revert a single systemcall when an error occurs in aufs
2482 +- remount interface instead of ioctl
2483 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2484 +- loopback mounted filesystem as a branch
2485 +- kernel thread for removing the dir who has a plenty of whiteouts
2486 +- support copyup sparse file (a file which has a 'hole' in it)
2487 +- default permission flags for branches
2488 +- selectable permission flags for ro branch, whether whiteout can
2489 +  exist or not
2490 +- export via NFS.
2491 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2492 +- support multiple writable branches, some policies to select one
2493 +  among multiple writable branches.
2494 +- a new semantics for link(2) and rename(2) to support multiple
2495 +  writable branches.
2496 +- no glibc changes are required.
2497 +- pseudo hardlink (hardlink over branches)
2498 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2499 +  including NFS or remote filesystem branch.
2500 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2501 +- and more...
2502 +
2503 +Currently these features are dropped temporary from aufs6.
2504 +See design/08plan.txt in detail.
2505 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2506 +  (robr)
2507 +- statistics of aufs thread (/sys/fs/aufs/stat)
2508 +
2509 +Features or just an idea in the future (see also design/*.txt),
2510 +- reorder the branch index without del/re-add.
2511 +- permanent xino files for NFSD
2512 +- an option for refreshing the opened files after add/del branches
2513 +- light version, without branch manipulation. (unnecessary?)
2514 +- copyup in userspace
2515 +- inotify in userspace
2516 +- readv/writev
2517 +
2518 +
2519 +2. Download
2520 +----------------------------------------
2521 +There are three GIT trees for aufs6, aufs-linux.git,
2522 +aufs-standalone.git, and aufs-util.git.
2523 +While the aufs-util is always necessary, you need either of aufs-linux
2524 +or aufs-standalone.
2525 +
2526 +The aufs-linux tree includes the whole linux mainline GIT tree,
2527 +git://git.kernel.org/.../torvalds/linux.git.
2528 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2529 +build aufs6 as an external kernel module.
2530 +Several extra patches are not included in this tree. Only
2531 +aufs-standalone tree contains them. They are described in the later
2532 +section "Configuration and Compilation."
2533 +
2534 +On the other hand, the aufs-standalone tree has only aufs source files
2535 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2536 +But you need to apply all aufs patches manually.
2537 +
2538 +You will find GIT branches whose name is in form of "aufs6.x" where "x"
2539 +represents the linux kernel version, "linux-6.x". For instance,
2540 +"aufs6.0" is for linux-6.0. For latest "linux-6.x-rcN", use
2541 +"aufs6.x-rcN" branch.
2542 +
2543 +o aufs-linux tree
2544 +$ git clone --reference /your/linux/git/tree \
2545 +       git://github.com/sfjro/aufs-linux.git aufs-linux.git
2546 +- if you don't have linux GIT tree, then remove "--reference ..."
2547 +$ cd aufs-linux.git
2548 +$ git checkout origin/aufs6.0
2549 +
2550 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2551 +leave the patch-work to GIT.
2552 +$ cd /your/linux/git/tree
2553 +$ git remote add aufs git://github.com/sfjro/aufs-linux.git
2554 +$ git fetch aufs
2555 +$ git checkout -b my6.0 v6.0
2556 +$ (add your local change...)
2557 +$ git pull aufs aufs6.0
2558 +- now you have v6.0 + your_changes + aufs6.0 in you my6.0 branch.
2559 +- you may need to solve some conflicts between your_changes and
2560 +  aufs6.0. in this case, git-rerere is recommended so that you can
2561 +  solve the similar conflicts automatically when you upgrade to 6.1 or
2562 +  later in the future.
2563 +
2564 +o aufs-standalone tree
2565 +$ git clone git://github.com/sfjro/aufs-standalone.git aufs-standalone.git
2566 +$ cd aufs-standalone.git
2567 +$ git checkout origin/aufs6.0
2568 +
2569 +o aufs-util tree
2570 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2571 +- note that the public aufs-util.git is on SourceForge instead of
2572 +  GitHUB.
2573 +$ cd aufs-util.git
2574 +$ git checkout origin/aufs6.0
2575 +
2576 +Note: The 6.x-rcN branch is to be used with `rc' kernel versions ONLY.
2577 +The minor version number, 'x' in '6.x', of aufs may not always
2578 +follow the minor version number of the kernel.
2579 +Because changes in the kernel that cause the use of a new
2580 +minor version number do not always require changes to aufs-util.
2581 +
2582 +Since aufs-util has its own minor version number, you may not be
2583 +able to find a GIT branch in aufs-util for your kernel's
2584 +exact minor version number.
2585 +In this case, you should git-checkout the branch for the
2586 +nearest lower number.
2587 +
2588 +For (an unreleased) example:
2589 +If you are using "linux-6.10" and the "aufs6.10" branch
2590 +does not exist in aufs-util repository, then "aufs6.9", "aufs6.8"
2591 +or something numerically smaller is the branch for your kernel.
2592 +
2593 +Also you can view all branches by
2594 +       $ git branch -a
2595 +
2596 +
2597 +3. Configuration and Compilation
2598 +----------------------------------------
2599 +Make sure you have git-checkout'ed the correct branch.
2600 +
2601 +For aufs-linux tree,
2602 +- enable CONFIG_AUFS_FS.
2603 +- set other aufs configurations if necessary.
2604 +- for aufs5.13 and later
2605 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2606 +  also a caller of VFS functions for branch filesystems, subclassing of
2607 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2608 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2609 +  need to customize some LOCKDEP numbers. Here are what I use on my
2610 +  test environment.
2611 +       CONFIG_LOCKDEP_BITS=21
2612 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2613 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2614 +  Also you will need to expand some constant values in LOCKDEP. Refer
2615 +  to lockdep-debug.patch in aufs-standalone.git.
2616 +
2617 +For aufs-standalone tree,
2618 +There are several ways to build.
2619 +
2620 +1.
2621 +- apply ./aufs6-kbuild.patch to your kernel source files.
2622 +- apply ./aufs6-base.patch too.
2623 +- apply ./aufs6-mmap.patch too.
2624 +- apply ./aufs6-standalone.patch too, if you have a plan to set
2625 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs-standalone.patch.
2626 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2627 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2628 +- enable CONFIG_AUFS_FS, you can select either
2629 +  =m or =y.
2630 +- and build your kernel as usual.
2631 +- install the built kernel.
2632 +- install the header files too by "make headers_install" to the
2633 +  directory where you specify. By default, it is $PWD/usr.
2634 +  "make help" shows a brief note for headers_install.
2635 +- and reboot your system.
2636 +
2637 +2.
2638 +- module only (CONFIG_AUFS_FS=m).
2639 +- apply ./aufs6-base.patch to your kernel source files.
2640 +- apply ./aufs6-mmap.patch too.
2641 +- apply ./aufs6-standalone.patch too.
2642 +- build your kernel, don't forget "make headers_install", and reboot.
2643 +- edit ./config.mk and set other aufs configurations if necessary.
2644 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2645 +  every aufs configurations.
2646 +- build the module by simple "make".
2647 +- you can specify ${KDIR} make variable which points to your kernel
2648 +  source tree.
2649 +- install the files
2650 +  + run "make install" to install the aufs module, or copy the built
2651 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2652 +  + run "make install_headers" (instead of headers_install) to install
2653 +    the modified aufs header file (you can specify DESTDIR which is
2654 +    available in aufs standalone version's Makefile only), or copy
2655 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2656 +    you like manually. By default, the target directory is $PWD/usr.
2657 +- no need to apply aufs6-kbuild.patch, nor copying source files to your
2658 +  kernel source tree.
2659 +
2660 +Note: The header file aufs_type.h is necessary to build aufs-util
2661 +      as well as "make headers_install" in the kernel source tree.
2662 +      headers_install is subject to be forgotten, but it is essentially
2663 +      necessary, not only for building aufs-util.
2664 +      You may not meet problems without headers_install in some older
2665 +      version though.
2666 +
2667 +And then,
2668 +- read README in aufs-util, build and install it
2669 +- note that your distribution may contain an obsoleted version of
2670 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2671 +  utilities, make sure that your compiler refers the correct aufs header
2672 +  file which is built by "make headers_install."
2673 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2674 +  then run "make install_ulib" too. And refer to the aufs manual in
2675 +  detail.
2676 +
2677 +There several other patches in aufs-standalone.git. They are all
2678 +optional. When you meet some problems, they will help you.
2679 +- aufs6-loopback.patch
2680 +  Supports a nested loopback mount in a branch-fs. This patch is
2681 +  unnecessary until aufs produces a message like "you may want to try
2682 +  another patch for loopback file".
2683 +- vfs-ino.patch
2684 +  Modifies a system global kernel internal function get_next_ino() in
2685 +  order to stop assigning 0 for an inode-number. Not directly related to
2686 +  aufs, but recommended generally.
2687 +- tmpfs-idr.patch
2688 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2689 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2690 +  duplication of inode number, which is important for backup tools and
2691 +  other utilities. When you find aufs XINO files for tmpfs branch
2692 +  growing too much, try this patch.
2693 +- lockdep-debug.patch
2694 +  Similar to some kernel configurations for LOCKDEP (see the top of
2695 +  this section), you will need expand some constants in LOCKDEP for
2696 +  aufs if you enable CONFIG_LOCKDEP.
2697 +
2698 +
2699 +4. Usage
2700 +----------------------------------------
2701 +At first, make sure aufs-util are installed, and please read the aufs
2702 +manual, aufs.5 in aufs-util.git tree.
2703 +$ man -l aufs.5
2704 +
2705 +And then,
2706 +$ mkdir /tmp/rw /tmp/aufs
2707 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2708 +
2709 +Here is another example. The result is equivalent.
2710 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2711 +  Or
2712 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2713 +# mount -o remount,append:${HOME} /tmp/aufs
2714 +
2715 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2716 +you modify a file under /tmp/aufs, the one on your home directory is
2717 +not affected, instead the same named file will be newly created under
2718 +/tmp/rw. And all of your modification to a file will be applied to
2719 +the one under /tmp/rw. This is called the file based Copy on Write
2720 +(COW) method.
2721 +Aufs mount options are described in aufs.5.
2722 +If you run chroot or something and make your aufs as a root directory,
2723 +then you need to customize the shutdown script. See the aufs manual in
2724 +detail.
2725 +
2726 +Additionally, there are some sample usages of aufs which are a
2727 +diskless system with network booting, and LiveCD over NFS.
2728 +See sample dir in CVS tree on SourceForge.
2729 +
2730 +
2731 +5. Contact
2732 +----------------------------------------
2733 +When you have any problems or strange behaviour in aufs, please let me
2734 +know with:
2735 +- /proc/mounts (instead of the output of mount(8))
2736 +- /sys/module/aufs/*
2737 +- /sys/fs/aufs/* (if you have them)
2738 +- /debug/aufs/* (if you have them)
2739 +- linux kernel version
2740 +  if your kernel is not plain, for example modified by distributor,
2741 +  the url where i can download its source is necessary too.
2742 +- aufs version which was printed at loading the module or booting the
2743 +  system, instead of the date you downloaded.
2744 +- configuration (define/undefine CONFIG_AUFS_xxx)
2745 +- kernel configuration or /proc/config.gz (if you have it)
2746 +- LSM (linux security module, if you are using)
2747 +- behaviour which you think to be incorrect
2748 +- actual operation, reproducible one is better
2749 +- mailto: aufs-users at lists.sourceforge.net
2750 +
2751 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2752 +and Feature Requests) on SourceForge. Please join and write to
2753 +aufs-users ML.
2754 +
2755 +
2756 +6. Acknowledgements
2757 +----------------------------------------
2758 +Thanks to everyone who have tried and are using aufs, whoever
2759 +have reported a bug or any feedback.
2760 +
2761 +Especially donators:
2762 +Tomas Matejicek(slax.org) made a donation (much more than once).
2763 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2764 +       scripts) is making "doubling" donations.
2765 +       Unfortunately I cannot list all of the donators, but I really
2766 +       appreciate.
2767 +       It ends Aug 2010, but the ordinary donation URL is still available.
2768 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2769 +Dai Itasaka made a donation (2007/8).
2770 +Chuck Smith made a donation (2008/4, 10 and 12).
2771 +Henk Schoneveld made a donation (2008/9).
2772 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2773 +Francois Dupoux made a donation (2008/11).
2774 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2775 +       aufs2 GIT tree (2009/2).
2776 +William Grant made a donation (2009/3).
2777 +Patrick Lane made a donation (2009/4).
2778 +The Mail Archive (mail-archive.com) made donations (2009/5).
2779 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2780 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2781 +Pavel Pronskiy made a donation (2011/2).
2782 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2783 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2784 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2785 +11).
2786 +Sam Liddicott made a donation (2011/9).
2787 +Era Scarecrow made a donation (2013/4).
2788 +Bor Ratajc made a donation (2013/4).
2789 +Alessandro Gorreta made a donation (2013/4).
2790 +POIRETTE Marc made a donation (2013/4).
2791 +Alessandro Gorreta made a donation (2013/4).
2792 +lauri kasvandik made a donation (2013/5).
2793 +"pemasu from Finland" made a donation (2013/7).
2794 +The Parted Magic Project made a donation (2013/9 and 11).
2795 +Pavel Barta made a donation (2013/10).
2796 +Nikolay Pertsev made a donation (2014/5).
2797 +James B made a donation (2014/7, 2015/7, and 2021/12).
2798 +Stefano Di Biase made a donation (2014/8).
2799 +Daniel Epellei made a donation (2015/1).
2800 +OmegaPhil made a donation (2016/1, 2018/4).
2801 +Tomasz Szewczyk made a donation (2016/4).
2802 +James Burry made a donation (2016/12).
2803 +Carsten Rose made a donation (2018/9).
2804 +Porteus Kiosk made a donation (2018/10).
2805 +huronOS team: Enya Quetzalli made donations (2022/5, 2023/5 and 8).
2806 +Vasily Mikhaylichenko made a donation (2023/5).
2807 +
2808 +Thank you very much.
2809 +Donations are always, including future donations, very important and
2810 +helpful for me to keep on developing aufs.
2811 +
2812 +
2813 +7.
2814 +----------------------------------------
2815 +If you are an experienced user, no explanation is needed. Aufs is
2816 +just a linux filesystem.
2817 +
2818 +
2819 +Enjoy!
2820 +
2821 +# Local variables: ;
2822 +# mode: text;
2823 +# End: ;
2824 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2825 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2826 +++ linux/fs/aufs/aufs.h        2022-11-05 23:02:18.959222617 +0100
2827 @@ -0,0 +1,62 @@
2828 +/* SPDX-License-Identifier: GPL-2.0 */
2829 +/*
2830 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2831 + *
2832 + * This program is free software; you can redistribute it and/or modify
2833 + * it under the terms of the GNU General Public License as published by
2834 + * the Free Software Foundation; either version 2 of the License, or
2835 + * (at your option) any later version.
2836 + *
2837 + * This program is distributed in the hope that it will be useful,
2838 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2839 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2840 + * GNU General Public License for more details.
2841 + *
2842 + * You should have received a copy of the GNU General Public License
2843 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2844 + */
2845 +
2846 +/*
2847 + * all header files
2848 + */
2849 +
2850 +#ifndef __AUFS_H__
2851 +#define __AUFS_H__
2852 +
2853 +#ifdef __KERNEL__
2854 +
2855 +#define AuStub(type, name, body, ...) \
2856 +       static inline type name(__VA_ARGS__) { body; }
2857 +
2858 +#define AuStubVoid(name, ...) \
2859 +       AuStub(void, name, , __VA_ARGS__)
2860 +#define AuStubInt0(name, ...) \
2861 +       AuStub(int, name, return 0, __VA_ARGS__)
2862 +
2863 +#include "debug.h"
2864 +
2865 +#include "branch.h"
2866 +#include "cpup.h"
2867 +#include "dcsub.h"
2868 +#include "dbgaufs.h"
2869 +#include "dentry.h"
2870 +#include "dir.h"
2871 +#include "dirren.h"
2872 +#include "dynop.h"
2873 +#include "file.h"
2874 +#include "fstype.h"
2875 +#include "hbl.h"
2876 +#include "inode.h"
2877 +#include "lcnt.h"
2878 +#include "loop.h"
2879 +#include "module.h"
2880 +#include "opts.h"
2881 +#include "rwsem.h"
2882 +#include "super.h"
2883 +#include "sysaufs.h"
2884 +#include "vfsub.h"
2885 +#include "whout.h"
2886 +#include "wkq.h"
2887 +
2888 +#endif /* __KERNEL__ */
2889 +#endif /* __AUFS_H__ */
2890 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2891 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2892 +++ linux/fs/aufs/branch.c      2022-11-05 23:02:18.959222617 +0100
2893 @@ -0,0 +1,1427 @@
2894 +// SPDX-License-Identifier: GPL-2.0
2895 +/*
2896 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2897 + *
2898 + * This program is free software; you can redistribute it and/or modify
2899 + * it under the terms of the GNU General Public License as published by
2900 + * the Free Software Foundation; either version 2 of the License, or
2901 + * (at your option) any later version.
2902 + *
2903 + * This program is distributed in the hope that it will be useful,
2904 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2905 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2906 + * GNU General Public License for more details.
2907 + *
2908 + * You should have received a copy of the GNU General Public License
2909 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2910 + */
2911 +
2912 +/*
2913 + * branch management
2914 + */
2915 +
2916 +#include <linux/compat.h>
2917 +#include <linux/statfs.h>
2918 +#include "aufs.h"
2919 +
2920 +/*
2921 + * free a single branch
2922 + */
2923 +static void au_br_do_free(struct au_branch *br)
2924 +{
2925 +       int i;
2926 +       struct au_wbr *wbr;
2927 +       struct au_dykey **key;
2928 +
2929 +       au_hnotify_fin_br(br);
2930 +       /* always, regardless the mount option */
2931 +       au_dr_hino_free(&br->br_dirren);
2932 +       au_xino_put(br);
2933 +
2934 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2935 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2936 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2937 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2938 +
2939 +       wbr = br->br_wbr;
2940 +       if (wbr) {
2941 +               for (i = 0; i < AuBrWh_Last; i++)
2942 +                       dput(wbr->wbr_wh[i]);
2943 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2944 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2945 +       }
2946 +
2947 +       if (br->br_fhsm) {
2948 +               au_br_fhsm_fin(br->br_fhsm);
2949 +               au_kfree_try_rcu(br->br_fhsm);
2950 +       }
2951 +
2952 +       key = br->br_dykey;
2953 +       for (i = 0; i < AuBrDynOp; i++, key++)
2954 +               if (*key)
2955 +                       au_dy_put(*key);
2956 +               else
2957 +                       break;
2958 +
2959 +       /* recursive lock, s_umount of branch's */
2960 +       /* synchronize_rcu(); */ /* why? */
2961 +       lockdep_off();
2962 +       path_put(&br->br_path);
2963 +       lockdep_on();
2964 +       au_kfree_rcu(wbr);
2965 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2966 +       au_lcnt_wait_for_fin(&br->br_count);
2967 +       /* I don't know why, but percpu_refcount requires this */
2968 +       /* synchronize_rcu(); */
2969 +       au_kfree_rcu(br);
2970 +}
2971 +
2972 +/*
2973 + * frees all branches
2974 + */
2975 +void au_br_free(struct au_sbinfo *sbinfo)
2976 +{
2977 +       aufs_bindex_t bmax;
2978 +       struct au_branch **br;
2979 +
2980 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2981 +
2982 +       bmax = sbinfo->si_bbot + 1;
2983 +       br = sbinfo->si_branch;
2984 +       while (bmax--)
2985 +               au_br_do_free(*br++);
2986 +}
2987 +
2988 +/*
2989 + * find the index of a branch which is specified by @br_id.
2990 + */
2991 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2992 +{
2993 +       aufs_bindex_t bindex, bbot;
2994 +
2995 +       bbot = au_sbbot(sb);
2996 +       for (bindex = 0; bindex <= bbot; bindex++)
2997 +               if (au_sbr_id(sb, bindex) == br_id)
2998 +                       return bindex;
2999 +       return -1;
3000 +}
3001 +
3002 +/* ---------------------------------------------------------------------- */
3003 +
3004 +/*
3005 + * add a branch
3006 + */
3007 +
3008 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3009 +                       struct dentry *h_root)
3010 +{
3011 +       if (unlikely(h_adding == h_root
3012 +                    || au_test_loopback_overlap(sb, h_adding)))
3013 +               return 1;
3014 +       if (h_adding->d_sb != h_root->d_sb)
3015 +               return 0;
3016 +       return au_test_subdir(h_adding, h_root)
3017 +               || au_test_subdir(h_root, h_adding);
3018 +}
3019 +
3020 +/*
3021 + * returns a newly allocated branch. @new_nbranch is a number of branches
3022 + * after adding a branch.
3023 + */
3024 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3025 +                                    int perm)
3026 +{
3027 +       struct au_branch *add_branch;
3028 +       struct dentry *root;
3029 +       struct inode *inode;
3030 +       int err;
3031 +
3032 +       err = -ENOMEM;
3033 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3034 +       if (unlikely(!add_branch))
3035 +               goto out;
3036 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3037 +       if (unlikely(!add_branch->br_xino))
3038 +               goto out_br;
3039 +       err = au_hnotify_init_br(add_branch, perm);
3040 +       if (unlikely(err))
3041 +               goto out_xino;
3042 +
3043 +       if (au_br_writable(perm)) {
3044 +               /* may be freed separately at changing the branch permission */
3045 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3046 +                                            GFP_NOFS);
3047 +               if (unlikely(!add_branch->br_wbr))
3048 +                       goto out_hnotify;
3049 +       }
3050 +
3051 +       if (au_br_fhsm(perm)) {
3052 +               err = au_fhsm_br_alloc(add_branch);
3053 +               if (unlikely(err))
3054 +                       goto out_wbr;
3055 +       }
3056 +
3057 +       root = sb->s_root;
3058 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3059 +       if (!err)
3060 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3061 +       if (!err) {
3062 +               inode = d_inode(root);
3063 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3064 +                                       /*may_shrink*/0);
3065 +       }
3066 +       if (!err)
3067 +               return add_branch; /* success */
3068 +
3069 +out_wbr:
3070 +       au_kfree_rcu(add_branch->br_wbr);
3071 +out_hnotify:
3072 +       au_hnotify_fin_br(add_branch);
3073 +out_xino:
3074 +       au_xino_put(add_branch);
3075 +out_br:
3076 +       au_kfree_rcu(add_branch);
3077 +out:
3078 +       return ERR_PTR(err);
3079 +}
3080 +
3081 +/*
3082 + * test if the branch permission is legal or not.
3083 + */
3084 +static int test_br(struct inode *inode, int brperm, char *path)
3085 +{
3086 +       int err;
3087 +
3088 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3089 +       if (!err)
3090 +               goto out;
3091 +
3092 +       err = -EINVAL;
3093 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3094 +
3095 +out:
3096 +       return err;
3097 +}
3098 +
3099 +/*
3100 + * returns:
3101 + * 0: success, the caller will add it
3102 + * plus: success, it is already unified, the caller should ignore it
3103 + * minus: error
3104 + */
3105 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3106 +{
3107 +       int err;
3108 +       aufs_bindex_t bbot, bindex;
3109 +       struct dentry *root, *h_dentry;
3110 +       struct inode *inode, *h_inode;
3111 +
3112 +       root = sb->s_root;
3113 +       bbot = au_sbbot(sb);
3114 +       if (unlikely(bbot >= 0
3115 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3116 +               err = 1;
3117 +               if (!remount) {
3118 +                       err = -EINVAL;
3119 +                       pr_err("%s duplicated\n", add->pathname);
3120 +               }
3121 +               goto out;
3122 +       }
3123 +
3124 +       err = -ENOSPC; /* -E2BIG; */
3125 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3126 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3127 +               pr_err("number of branches exceeded %s\n", add->pathname);
3128 +               goto out;
3129 +       }
3130 +
3131 +       err = -EDOM;
3132 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3133 +               pr_err("bad index %d\n", add->bindex);
3134 +               goto out;
3135 +       }
3136 +
3137 +       inode = d_inode(add->path.dentry);
3138 +       err = -ENOENT;
3139 +       if (unlikely(!inode->i_nlink)) {
3140 +               pr_err("no existence %s\n", add->pathname);
3141 +               goto out;
3142 +       }
3143 +
3144 +       err = -EINVAL;
3145 +       if (unlikely(inode->i_sb == sb)) {
3146 +               pr_err("%s must be outside\n", add->pathname);
3147 +               goto out;
3148 +       }
3149 +
3150 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3151 +               pr_err("unsupported filesystem, %s (%s)\n",
3152 +                      add->pathname, au_sbtype(inode->i_sb));
3153 +               goto out;
3154 +       }
3155 +
3156 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3157 +               pr_err("already stacked, %s (%s)\n",
3158 +                      add->pathname, au_sbtype(inode->i_sb));
3159 +               goto out;
3160 +       }
3161 +
3162 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3163 +       if (unlikely(err))
3164 +               goto out;
3165 +
3166 +       if (bbot < 0)
3167 +               return 0; /* success */
3168 +
3169 +       err = -EINVAL;
3170 +       for (bindex = 0; bindex <= bbot; bindex++)
3171 +               if (unlikely(test_overlap(sb, add->path.dentry,
3172 +                                         au_h_dptr(root, bindex)))) {
3173 +                       pr_err("%s is overlapped\n", add->pathname);
3174 +                       goto out;
3175 +               }
3176 +
3177 +       err = 0;
3178 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3179 +               h_dentry = au_h_dptr(root, 0);
3180 +               h_inode = d_inode(h_dentry);
3181 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3182 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3183 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3184 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3185 +                               add->pathname,
3186 +                               i_uid_read(inode), i_gid_read(inode),
3187 +                               (inode->i_mode & S_IALLUGO),
3188 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3189 +                               (h_inode->i_mode & S_IALLUGO));
3190 +       }
3191 +
3192 +out:
3193 +       return err;
3194 +}
3195 +
3196 +/*
3197 + * initialize or clean the whiteouts for an adding branch
3198 + */
3199 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3200 +                        int new_perm)
3201 +{
3202 +       int err, old_perm;
3203 +       aufs_bindex_t bindex;
3204 +       struct inode *h_inode;
3205 +       struct au_wbr *wbr;
3206 +       struct au_hinode *hdir;
3207 +       struct dentry *h_dentry;
3208 +
3209 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3210 +       if (unlikely(err))
3211 +               goto out;
3212 +
3213 +       wbr = br->br_wbr;
3214 +       old_perm = br->br_perm;
3215 +       br->br_perm = new_perm;
3216 +       hdir = NULL;
3217 +       h_inode = NULL;
3218 +       bindex = au_br_index(sb, br->br_id);
3219 +       if (0 <= bindex) {
3220 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3221 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3222 +       } else {
3223 +               h_dentry = au_br_dentry(br);
3224 +               h_inode = d_inode(h_dentry);
3225 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3226 +       }
3227 +       if (!wbr)
3228 +               err = au_wh_init(br, sb);
3229 +       else {
3230 +               wbr_wh_write_lock(wbr);
3231 +               err = au_wh_init(br, sb);
3232 +               wbr_wh_write_unlock(wbr);
3233 +       }
3234 +       if (hdir)
3235 +               au_hn_inode_unlock(hdir);
3236 +       else
3237 +               inode_unlock(h_inode);
3238 +       vfsub_mnt_drop_write(au_br_mnt(br));
3239 +       br->br_perm = old_perm;
3240 +
3241 +       if (!err && wbr && !au_br_writable(new_perm)) {
3242 +               au_kfree_rcu(wbr);
3243 +               br->br_wbr = NULL;
3244 +       }
3245 +
3246 +out:
3247 +       return err;
3248 +}
3249 +
3250 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3251 +                      int perm)
3252 +{
3253 +       int err;
3254 +       struct kstatfs kst;
3255 +       struct au_wbr *wbr;
3256 +
3257 +       wbr = br->br_wbr;
3258 +       au_rw_init(&wbr->wbr_wh_rwsem);
3259 +       atomic_set(&wbr->wbr_wh_running, 0);
3260 +
3261 +       /*
3262 +        * a limit for rmdir/rename a dir
3263 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3264 +        */
3265 +       err = vfs_statfs(&br->br_path, &kst);
3266 +       if (unlikely(err))
3267 +               goto out;
3268 +       err = -EINVAL;
3269 +       if (kst.f_namelen >= NAME_MAX)
3270 +               err = au_br_init_wh(sb, br, perm);
3271 +       else
3272 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3273 +                      au_br_dentry(br),
3274 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3275 +
3276 +out:
3277 +       return err;
3278 +}
3279 +
3280 +/* initialize a new branch */
3281 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3282 +                     struct au_opt_add *add)
3283 +{
3284 +       int err;
3285 +       struct au_branch *brbase;
3286 +       struct file *xf;
3287 +       struct inode *h_inode;
3288 +
3289 +       err = 0;
3290 +       br->br_perm = add->perm;
3291 +       br->br_path = add->path; /* set first, path_get() later */
3292 +       spin_lock_init(&br->br_dykey_lock);
3293 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3294 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3295 +       br->br_id = au_new_br_id(sb);
3296 +       AuDebugOn(br->br_id < 0);
3297 +
3298 +       /* always, regardless the given option */
3299 +       err = au_dr_br_init(sb, br, &add->path);
3300 +       if (unlikely(err))
3301 +               goto out_err;
3302 +
3303 +       if (au_br_writable(add->perm)) {
3304 +               err = au_wbr_init(br, sb, add->perm);
3305 +               if (unlikely(err))
3306 +                       goto out_err;
3307 +       }
3308 +
3309 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3310 +               brbase = au_sbr(sb, 0);
3311 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3312 +               AuDebugOn(!xf);
3313 +               h_inode = d_inode(add->path.dentry);
3314 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3315 +               if (unlikely(err)) {
3316 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3317 +                       goto out_err;
3318 +               }
3319 +       }
3320 +
3321 +       sysaufs_br_init(br);
3322 +       path_get(&br->br_path);
3323 +       goto out; /* success */
3324 +
3325 +out_err:
3326 +       memset(&br->br_path, 0, sizeof(br->br_path));
3327 +out:
3328 +       return err;
3329 +}
3330 +
3331 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3332 +                            struct au_branch *br, aufs_bindex_t bbot,
3333 +                            aufs_bindex_t amount)
3334 +{
3335 +       struct au_branch **brp;
3336 +
3337 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3338 +
3339 +       brp = sbinfo->si_branch + bindex;
3340 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3341 +       *brp = br;
3342 +       sbinfo->si_bbot++;
3343 +       if (unlikely(bbot < 0))
3344 +               sbinfo->si_bbot = 0;
3345 +}
3346 +
3347 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3348 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3349 +{
3350 +       struct au_hdentry *hdp;
3351 +
3352 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3353 +
3354 +       hdp = au_hdentry(dinfo, bindex);
3355 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3356 +       au_h_dentry_init(hdp);
3357 +       dinfo->di_bbot++;
3358 +       if (unlikely(bbot < 0))
3359 +               dinfo->di_btop = 0;
3360 +}
3361 +
3362 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3363 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3364 +{
3365 +       struct au_hinode *hip;
3366 +
3367 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3368 +
3369 +       hip = au_hinode(iinfo, bindex);
3370 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3371 +       au_hinode_init(hip);
3372 +       iinfo->ii_bbot++;
3373 +       if (unlikely(bbot < 0))
3374 +               iinfo->ii_btop = 0;
3375 +}
3376 +
3377 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3378 +                        aufs_bindex_t bindex)
3379 +{
3380 +       struct dentry *root, *h_dentry;
3381 +       struct inode *root_inode, *h_inode;
3382 +       aufs_bindex_t bbot, amount;
3383 +
3384 +       root = sb->s_root;
3385 +       root_inode = d_inode(root);
3386 +       bbot = au_sbbot(sb);
3387 +       amount = bbot + 1 - bindex;
3388 +       h_dentry = au_br_dentry(br);
3389 +       au_sbilist_lock();
3390 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3391 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3392 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3393 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3394 +       h_inode = d_inode(h_dentry);
3395 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3396 +       au_sbilist_unlock();
3397 +}
3398 +
3399 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3400 +{
3401 +       int err;
3402 +       aufs_bindex_t bbot, add_bindex;
3403 +       struct dentry *root, *h_dentry;
3404 +       struct inode *root_inode;
3405 +       struct au_branch *add_branch;
3406 +
3407 +       root = sb->s_root;
3408 +       root_inode = d_inode(root);
3409 +       IMustLock(root_inode);
3410 +       IiMustWriteLock(root_inode);
3411 +       err = test_add(sb, add, remount);
3412 +       if (unlikely(err < 0))
3413 +               goto out;
3414 +       if (err) {
3415 +               err = 0;
3416 +               goto out; /* success */
3417 +       }
3418 +
3419 +       bbot = au_sbbot(sb);
3420 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3421 +       err = PTR_ERR(add_branch);
3422 +       if (IS_ERR(add_branch))
3423 +               goto out;
3424 +
3425 +       err = au_br_init(add_branch, sb, add);
3426 +       if (unlikely(err)) {
3427 +               au_br_do_free(add_branch);
3428 +               goto out;
3429 +       }
3430 +
3431 +       add_bindex = add->bindex;
3432 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3433 +       au_br_do_add(sb, add_branch, add_bindex);
3434 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3435 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3436 +
3437 +       h_dentry = add->path.dentry;
3438 +       if (!add_bindex) {
3439 +               au_cpup_attr_all(root_inode, /*force*/1);
3440 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3441 +       } else
3442 +               au_add_nlink(root_inode, d_inode(h_dentry));
3443 +
3444 +out:
3445 +       return err;
3446 +}
3447 +
3448 +/* ---------------------------------------------------------------------- */
3449 +
3450 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3451 +                                      unsigned long long max __maybe_unused,
3452 +                                      void *arg)
3453 +{
3454 +       unsigned long long n;
3455 +       struct file **p, *f;
3456 +       struct hlist_bl_head *files;
3457 +       struct hlist_bl_node *pos;
3458 +       struct au_finfo *finfo;
3459 +
3460 +       n = 0;
3461 +       p = a;
3462 +       files = &au_sbi(sb)->si_files;
3463 +       hlist_bl_lock(files);
3464 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3465 +               f = finfo->fi_file;
3466 +               if (file_count(f)
3467 +                   && !special_file(file_inode(f)->i_mode)) {
3468 +                       get_file(f);
3469 +                       *p++ = f;
3470 +                       n++;
3471 +                       AuDebugOn(n > max);
3472 +               }
3473 +       }
3474 +       hlist_bl_unlock(files);
3475 +
3476 +       return n;
3477 +}
3478 +
3479 +static struct file **au_farray_alloc(struct super_block *sb,
3480 +                                    unsigned long long *max)
3481 +{
3482 +       struct au_sbinfo *sbi;
3483 +
3484 +       sbi = au_sbi(sb);
3485 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3486 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3487 +}
3488 +
3489 +static void au_farray_free(struct file **a, unsigned long long max)
3490 +{
3491 +       unsigned long long ull;
3492 +
3493 +       for (ull = 0; ull < max; ull++)
3494 +               if (a[ull])
3495 +                       fput(a[ull]);
3496 +       kvfree(a);
3497 +}
3498 +
3499 +/* ---------------------------------------------------------------------- */
3500 +
3501 +/*
3502 + * delete a branch
3503 + */
3504 +
3505 +/* to show the line number, do not make it inlined function */
3506 +#define AuVerbose(do_info, fmt, ...) do { \
3507 +       if (do_info) \
3508 +               pr_info(fmt, ##__VA_ARGS__); \
3509 +} while (0)
3510 +
3511 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3512 +                        aufs_bindex_t bbot)
3513 +{
3514 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3515 +}
3516 +
3517 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3518 +                        aufs_bindex_t bbot)
3519 +{
3520 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3521 +}
3522 +
3523 +/*
3524 + * test if the branch is deletable or not.
3525 + */
3526 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3527 +                           unsigned int sigen, const unsigned int verbose)
3528 +{
3529 +       int err, i, j, ndentry;
3530 +       aufs_bindex_t btop, bbot;
3531 +       struct au_dcsub_pages dpages;
3532 +       struct au_dpage *dpage;
3533 +       struct dentry *d;
3534 +
3535 +       err = au_dpages_init(&dpages, GFP_NOFS);
3536 +       if (unlikely(err))
3537 +               goto out;
3538 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3539 +       if (unlikely(err))
3540 +               goto out_dpages;
3541 +
3542 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3543 +               dpage = dpages.dpages + i;
3544 +               ndentry = dpage->ndentry;
3545 +               for (j = 0; !err && j < ndentry; j++) {
3546 +                       d = dpage->dentries[j];
3547 +                       AuDebugOn(au_dcount(d) <= 0);
3548 +                       if (!au_digen_test(d, sigen)) {
3549 +                               di_read_lock_child(d, AuLock_IR);
3550 +                               if (unlikely(au_dbrange_test(d))) {
3551 +                                       di_read_unlock(d, AuLock_IR);
3552 +                                       continue;
3553 +                               }
3554 +                       } else {
3555 +                               di_write_lock_child(d);
3556 +                               if (unlikely(au_dbrange_test(d))) {
3557 +                                       di_write_unlock(d);
3558 +                                       continue;
3559 +                               }
3560 +                               err = au_reval_dpath(d, sigen);
3561 +                               if (!err)
3562 +                                       di_downgrade_lock(d, AuLock_IR);
3563 +                               else {
3564 +                                       di_write_unlock(d);
3565 +                                       break;
3566 +                               }
3567 +                       }
3568 +
3569 +                       /* AuDbgDentry(d); */
3570 +                       btop = au_dbtop(d);
3571 +                       bbot = au_dbbot(d);
3572 +                       if (btop <= bindex
3573 +                           && bindex <= bbot
3574 +                           && au_h_dptr(d, bindex)
3575 +                           && au_test_dbusy(d, btop, bbot)) {
3576 +                               err = -EBUSY;
3577 +                               AuVerbose(verbose, "busy %pd\n", d);
3578 +                               AuDbgDentry(d);
3579 +                       }
3580 +                       di_read_unlock(d, AuLock_IR);
3581 +               }
3582 +       }
3583 +
3584 +out_dpages:
3585 +       au_dpages_free(&dpages);
3586 +out:
3587 +       return err;
3588 +}
3589 +
3590 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3591 +                          unsigned int sigen, const unsigned int verbose)
3592 +{
3593 +       int err;
3594 +       unsigned long long max, ull;
3595 +       struct inode *i, **array;
3596 +       aufs_bindex_t btop, bbot;
3597 +
3598 +       array = au_iarray_alloc(sb, &max);
3599 +       err = PTR_ERR(array);
3600 +       if (IS_ERR(array))
3601 +               goto out;
3602 +
3603 +       err = 0;
3604 +       AuDbg("b%d\n", bindex);
3605 +       for (ull = 0; !err && ull < max; ull++) {
3606 +               i = array[ull];
3607 +               if (unlikely(!i))
3608 +                       break;
3609 +               if (i->i_ino == AUFS_ROOT_INO)
3610 +                       continue;
3611 +
3612 +               /* AuDbgInode(i); */
3613 +               if (au_iigen(i, NULL) == sigen)
3614 +                       ii_read_lock_child(i);
3615 +               else {
3616 +                       ii_write_lock_child(i);
3617 +                       err = au_refresh_hinode_self(i);
3618 +                       au_iigen_dec(i);
3619 +                       if (!err)
3620 +                               ii_downgrade_lock(i);
3621 +                       else {
3622 +                               ii_write_unlock(i);
3623 +                               break;
3624 +                       }
3625 +               }
3626 +
3627 +               btop = au_ibtop(i);
3628 +               bbot = au_ibbot(i);
3629 +               if (btop <= bindex
3630 +                   && bindex <= bbot
3631 +                   && au_h_iptr(i, bindex)
3632 +                   && au_test_ibusy(i, btop, bbot)) {
3633 +                       err = -EBUSY;
3634 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3635 +                       AuDbgInode(i);
3636 +               }
3637 +               ii_read_unlock(i);
3638 +       }
3639 +       au_iarray_free(array, max);
3640 +
3641 +out:
3642 +       return err;
3643 +}
3644 +
3645 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3646 +                             const unsigned int verbose)
3647 +{
3648 +       int err;
3649 +       unsigned int sigen;
3650 +
3651 +       sigen = au_sigen(root->d_sb);
3652 +       DiMustNoWaiters(root);
3653 +       IiMustNoWaiters(d_inode(root));
3654 +       di_write_unlock(root);
3655 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3656 +       if (!err)
3657 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3658 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3659 +
3660 +       return err;
3661 +}
3662 +
3663 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3664 +                        struct file **to_free, int *idx)
3665 +{
3666 +       int err;
3667 +       unsigned char matched, root;
3668 +       aufs_bindex_t bindex, bbot;
3669 +       struct au_fidir *fidir;
3670 +       struct au_hfile *hfile;
3671 +
3672 +       err = 0;
3673 +       root = IS_ROOT(file->f_path.dentry);
3674 +       if (root) {
3675 +               get_file(file);
3676 +               to_free[*idx] = file;
3677 +               (*idx)++;
3678 +               goto out;
3679 +       }
3680 +
3681 +       matched = 0;
3682 +       fidir = au_fi(file)->fi_hdir;
3683 +       AuDebugOn(!fidir);
3684 +       bbot = au_fbbot_dir(file);
3685 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3686 +               hfile = fidir->fd_hfile + bindex;
3687 +               if (!hfile->hf_file)
3688 +                       continue;
3689 +
3690 +               if (hfile->hf_br->br_id == br_id) {
3691 +                       matched = 1;
3692 +                       break;
3693 +               }
3694 +       }
3695 +       if (matched)
3696 +               err = -EBUSY;
3697 +
3698 +out:
3699 +       return err;
3700 +}
3701 +
3702 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3703 +                         struct file **to_free, int opened)
3704 +{
3705 +       int err, idx;
3706 +       unsigned long long ull, max;
3707 +       aufs_bindex_t btop;
3708 +       struct file *file, **array;
3709 +       struct dentry *root;
3710 +       struct au_hfile *hfile;
3711 +
3712 +       array = au_farray_alloc(sb, &max);
3713 +       err = PTR_ERR(array);
3714 +       if (IS_ERR(array))
3715 +               goto out;
3716 +
3717 +       err = 0;
3718 +       idx = 0;
3719 +       root = sb->s_root;
3720 +       di_write_unlock(root);
3721 +       for (ull = 0; ull < max; ull++) {
3722 +               file = array[ull];
3723 +               if (unlikely(!file))
3724 +                       break;
3725 +
3726 +               /* AuDbg("%pD\n", file); */
3727 +               fi_read_lock(file);
3728 +               btop = au_fbtop(file);
3729 +               if (!d_is_dir(file->f_path.dentry)) {
3730 +                       hfile = &au_fi(file)->fi_htop;
3731 +                       if (hfile->hf_br->br_id == br_id)
3732 +                               err = -EBUSY;
3733 +               } else
3734 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3735 +               fi_read_unlock(file);
3736 +               if (unlikely(err))
3737 +                       break;
3738 +       }
3739 +       di_write_lock_child(root);
3740 +       au_farray_free(array, max);
3741 +       AuDebugOn(idx > opened);
3742 +
3743 +out:
3744 +       return err;
3745 +}
3746 +
3747 +static void br_del_file(struct file **to_free, unsigned long long opened,
3748 +                       aufs_bindex_t br_id)
3749 +{
3750 +       unsigned long long ull;
3751 +       aufs_bindex_t bindex, btop, bbot, bfound;
3752 +       struct file *file;
3753 +       struct au_fidir *fidir;
3754 +       struct au_hfile *hfile;
3755 +
3756 +       for (ull = 0; ull < opened; ull++) {
3757 +               file = to_free[ull];
3758 +               if (unlikely(!file))
3759 +                       break;
3760 +
3761 +               /* AuDbg("%pD\n", file); */
3762 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3763 +               bfound = -1;
3764 +               fidir = au_fi(file)->fi_hdir;
3765 +               AuDebugOn(!fidir);
3766 +               fi_write_lock(file);
3767 +               btop = au_fbtop(file);
3768 +               bbot = au_fbbot_dir(file);
3769 +               for (bindex = btop; bindex <= bbot; bindex++) {
3770 +                       hfile = fidir->fd_hfile + bindex;
3771 +                       if (!hfile->hf_file)
3772 +                               continue;
3773 +
3774 +                       if (hfile->hf_br->br_id == br_id) {
3775 +                               bfound = bindex;
3776 +                               break;
3777 +                       }
3778 +               }
3779 +               AuDebugOn(bfound < 0);
3780 +               au_set_h_fptr(file, bfound, NULL);
3781 +               if (bfound == btop) {
3782 +                       for (btop++; btop <= bbot; btop++)
3783 +                               if (au_hf_dir(file, btop)) {
3784 +                                       au_set_fbtop(file, btop);
3785 +                                       break;
3786 +                               }
3787 +               }
3788 +               fi_write_unlock(file);
3789 +       }
3790 +}
3791 +
3792 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3793 +                            const aufs_bindex_t bindex,
3794 +                            const aufs_bindex_t bbot)
3795 +{
3796 +       struct au_branch **brp, **p;
3797 +
3798 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3799 +
3800 +       brp = sbinfo->si_branch + bindex;
3801 +       if (bindex < bbot)
3802 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3803 +       sbinfo->si_branch[0 + bbot] = NULL;
3804 +       sbinfo->si_bbot--;
3805 +
3806 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3807 +                       /*may_shrink*/1);
3808 +       if (p)
3809 +               sbinfo->si_branch = p;
3810 +       /* harmless error */
3811 +}
3812 +
3813 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3814 +                            const aufs_bindex_t bbot)
3815 +{
3816 +       struct au_hdentry *hdp, *p;
3817 +
3818 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3819 +
3820 +       hdp = au_hdentry(dinfo, bindex);
3821 +       if (bindex < bbot)
3822 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3823 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3824 +       dinfo->di_bbot--;
3825 +
3826 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3827 +                       /*may_shrink*/1);
3828 +       if (p)
3829 +               dinfo->di_hdentry = p;
3830 +       /* harmless error */
3831 +}
3832 +
3833 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3834 +                            const aufs_bindex_t bbot)
3835 +{
3836 +       struct au_hinode *hip, *p;
3837 +
3838 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3839 +
3840 +       hip = au_hinode(iinfo, bindex);
3841 +       if (bindex < bbot)
3842 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3843 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3844 +       iinfo->ii_bbot--;
3845 +
3846 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3847 +                       /*may_shrink*/1);
3848 +       if (p)
3849 +               iinfo->ii_hinode = p;
3850 +       /* harmless error */
3851 +}
3852 +
3853 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3854 +                        struct au_branch *br)
3855 +{
3856 +       aufs_bindex_t bbot;
3857 +       struct au_sbinfo *sbinfo;
3858 +       struct dentry *root, *h_root;
3859 +       struct inode *inode, *h_inode;
3860 +       struct au_hinode *hinode;
3861 +
3862 +       SiMustWriteLock(sb);
3863 +
3864 +       root = sb->s_root;
3865 +       inode = d_inode(root);
3866 +       sbinfo = au_sbi(sb);
3867 +       bbot = sbinfo->si_bbot;
3868 +
3869 +       h_root = au_h_dptr(root, bindex);
3870 +       hinode = au_hi(inode, bindex);
3871 +       h_inode = au_igrab(hinode->hi_inode);
3872 +       au_hiput(hinode);
3873 +
3874 +       au_sbilist_lock();
3875 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3876 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3877 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3878 +       au_sbilist_unlock();
3879 +
3880 +       /* ignore an error */
3881 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3882 +
3883 +       dput(h_root);
3884 +       iput(h_inode);
3885 +       au_br_do_free(br);
3886 +}
3887 +
3888 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3889 +                                  unsigned long long max, void *arg)
3890 +{
3891 +       return max;
3892 +}
3893 +
3894 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3895 +{
3896 +       int err, rerr, i;
3897 +       unsigned long long opened;
3898 +       unsigned int mnt_flags;
3899 +       aufs_bindex_t bindex, bbot, br_id;
3900 +       unsigned char do_wh, verbose;
3901 +       struct au_branch *br;
3902 +       struct au_wbr *wbr;
3903 +       struct dentry *root;
3904 +       struct file **to_free;
3905 +
3906 +       err = 0;
3907 +       opened = 0;
3908 +       to_free = NULL;
3909 +       root = sb->s_root;
3910 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3911 +       if (bindex < 0) {
3912 +               if (remount)
3913 +                       goto out; /* success */
3914 +               err = -ENOENT;
3915 +               pr_err("%s no such branch\n", del->pathname);
3916 +               goto out;
3917 +       }
3918 +       AuDbg("bindex b%d\n", bindex);
3919 +
3920 +       err = -EBUSY;
3921 +       mnt_flags = au_mntflags(sb);
3922 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3923 +       bbot = au_sbbot(sb);
3924 +       if (unlikely(!bbot)) {
3925 +               AuVerbose(verbose, "no more branches left\n");
3926 +               goto out;
3927 +       }
3928 +
3929 +       br = au_sbr(sb, bindex);
3930 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3931 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3932 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3933 +               goto out;
3934 +       }
3935 +
3936 +       br_id = br->br_id;
3937 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3938 +       if (unlikely(opened)) {
3939 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3940 +               err = PTR_ERR(to_free);
3941 +               if (IS_ERR(to_free))
3942 +                       goto out;
3943 +
3944 +               err = test_file_busy(sb, br_id, to_free, opened);
3945 +               if (unlikely(err)) {
3946 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3947 +                       goto out;
3948 +               }
3949 +       }
3950 +
3951 +       wbr = br->br_wbr;
3952 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3953 +       if (do_wh) {
3954 +               /* instead of WbrWhMustWriteLock(wbr) */
3955 +               SiMustWriteLock(sb);
3956 +               for (i = 0; i < AuBrWh_Last; i++) {
3957 +                       dput(wbr->wbr_wh[i]);
3958 +                       wbr->wbr_wh[i] = NULL;
3959 +               }
3960 +       }
3961 +
3962 +       err = test_children_busy(root, bindex, verbose);
3963 +       if (unlikely(err)) {
3964 +               if (do_wh)
3965 +                       goto out_wh;
3966 +               goto out;
3967 +       }
3968 +
3969 +       err = 0;
3970 +       if (to_free) {
3971 +               /*
3972 +                * now we confirmed the branch is deletable.
3973 +                * let's free the remaining opened dirs on the branch.
3974 +                */
3975 +               di_write_unlock(root);
3976 +               br_del_file(to_free, opened, br_id);
3977 +               di_write_lock_child(root);
3978 +       }
3979 +
3980 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3981 +       dbgaufs_xino_del(br);           /* remove one */
3982 +       au_br_do_del(sb, bindex, br);
3983 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3984 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
3985 +
3986 +       if (!bindex) {
3987 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3988 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3989 +       } else
3990 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3991 +       if (au_opt_test(mnt_flags, PLINK))
3992 +               au_plink_half_refresh(sb, br_id);
3993 +
3994 +       goto out; /* success */
3995 +
3996 +out_wh:
3997 +       /* revert */
3998 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3999 +       if (rerr)
4000 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4001 +                       del->pathname, rerr);
4002 +out:
4003 +       if (to_free)
4004 +               au_farray_free(to_free, opened);
4005 +       return err;
4006 +}
4007 +
4008 +/* ---------------------------------------------------------------------- */
4009 +
4010 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4011 +{
4012 +       int err;
4013 +       aufs_bindex_t btop, bbot;
4014 +       struct aufs_ibusy ibusy;
4015 +       struct inode *inode, *h_inode;
4016 +
4017 +       err = -EPERM;
4018 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4019 +               goto out;
4020 +
4021 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4022 +       if (!err)
4023 +               /* VERIFY_WRITE */
4024 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4025 +       if (unlikely(err)) {
4026 +               err = -EFAULT;
4027 +               AuTraceErr(err);
4028 +               goto out;
4029 +       }
4030 +
4031 +       err = -EINVAL;
4032 +       si_read_lock(sb, AuLock_FLUSH);
4033 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4034 +               goto out_unlock;
4035 +
4036 +       err = 0;
4037 +       ibusy.h_ino = 0; /* invalid */
4038 +       inode = ilookup(sb, ibusy.ino);
4039 +       if (!inode
4040 +           || inode->i_ino == AUFS_ROOT_INO
4041 +           || au_is_bad_inode(inode))
4042 +               goto out_unlock;
4043 +
4044 +       ii_read_lock_child(inode);
4045 +       btop = au_ibtop(inode);
4046 +       bbot = au_ibbot(inode);
4047 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4048 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4049 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4050 +                       ibusy.h_ino = h_inode->i_ino;
4051 +       }
4052 +       ii_read_unlock(inode);
4053 +       iput(inode);
4054 +
4055 +out_unlock:
4056 +       si_read_unlock(sb);
4057 +       if (!err) {
4058 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4059 +               if (unlikely(err)) {
4060 +                       err = -EFAULT;
4061 +                       AuTraceErr(err);
4062 +               }
4063 +       }
4064 +out:
4065 +       return err;
4066 +}
4067 +
4068 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4069 +{
4070 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4071 +}
4072 +
4073 +#ifdef CONFIG_COMPAT
4074 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4075 +{
4076 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4077 +}
4078 +#endif
4079 +
4080 +/* ---------------------------------------------------------------------- */
4081 +
4082 +/*
4083 + * change a branch permission
4084 + */
4085 +
4086 +static void au_warn_ima(void)
4087 +{
4088 +#ifdef CONFIG_IMA
4089 +       /* since it doesn't support mark_files_ro() */
4090 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4091 +#endif
4092 +}
4093 +
4094 +static int do_need_sigen_inc(int a, int b)
4095 +{
4096 +       return au_br_whable(a) && !au_br_whable(b);
4097 +}
4098 +
4099 +static int need_sigen_inc(int old, int new)
4100 +{
4101 +       return do_need_sigen_inc(old, new)
4102 +               || do_need_sigen_inc(new, old);
4103 +}
4104 +
4105 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4106 +{
4107 +       int err, do_warn;
4108 +       unsigned int mnt_flags;
4109 +       unsigned long long ull, max;
4110 +       aufs_bindex_t br_id;
4111 +       unsigned char verbose, writer;
4112 +       struct file *file, *hf, **array;
4113 +       struct au_hfile *hfile;
4114 +       struct inode *h_inode;
4115 +
4116 +       mnt_flags = au_mntflags(sb);
4117 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4118 +
4119 +       array = au_farray_alloc(sb, &max);
4120 +       err = PTR_ERR(array);
4121 +       if (IS_ERR(array))
4122 +               goto out;
4123 +
4124 +       do_warn = 0;
4125 +       br_id = au_sbr_id(sb, bindex);
4126 +       for (ull = 0; ull < max; ull++) {
4127 +               file = array[ull];
4128 +               if (unlikely(!file))
4129 +                       break;
4130 +
4131 +               /* AuDbg("%pD\n", file); */
4132 +               fi_read_lock(file);
4133 +               if (unlikely(au_test_mmapped(file))) {
4134 +                       err = -EBUSY;
4135 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4136 +                       AuDbgFile(file);
4137 +                       FiMustNoWaiters(file);
4138 +                       fi_read_unlock(file);
4139 +                       goto out_array;
4140 +               }
4141 +
4142 +               hfile = &au_fi(file)->fi_htop;
4143 +               hf = hfile->hf_file;
4144 +               if (!d_is_reg(file->f_path.dentry)
4145 +                   || !(file->f_mode & FMODE_WRITE)
4146 +                   || hfile->hf_br->br_id != br_id
4147 +                   || !(hf->f_mode & FMODE_WRITE))
4148 +                       array[ull] = NULL;
4149 +               else {
4150 +                       do_warn = 1;
4151 +                       get_file(file);
4152 +               }
4153 +
4154 +               FiMustNoWaiters(file);
4155 +               fi_read_unlock(file);
4156 +               fput(file);
4157 +       }
4158 +
4159 +       err = 0;
4160 +       if (do_warn)
4161 +               au_warn_ima();
4162 +
4163 +       for (ull = 0; ull < max; ull++) {
4164 +               file = array[ull];
4165 +               if (!file)
4166 +                       continue;
4167 +
4168 +               /* todo: already flushed? */
4169 +               /*
4170 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4171 +                * approach which resets f_mode and calls mnt_drop_write() and
4172 +                * file_release_write() for each file, because the branch
4173 +                * attribute in aufs world is totally different from the native
4174 +                * fs rw/ro mode.
4175 +                */
4176 +               /* fi_read_lock(file); */
4177 +               hfile = &au_fi(file)->fi_htop;
4178 +               hf = hfile->hf_file;
4179 +               /* fi_read_unlock(file); */
4180 +               spin_lock(&hf->f_lock);
4181 +               writer = !!(hf->f_mode & FMODE_WRITER);
4182 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4183 +               spin_unlock(&hf->f_lock);
4184 +               if (writer) {
4185 +                       h_inode = file_inode(hf);
4186 +                       if (hf->f_mode & FMODE_READ)
4187 +                               i_readcount_inc(h_inode);
4188 +                       put_write_access(h_inode);
4189 +                       __mnt_drop_write(hf->f_path.mnt);
4190 +               }
4191 +       }
4192 +
4193 +out_array:
4194 +       au_farray_free(array, max);
4195 +out:
4196 +       AuTraceErr(err);
4197 +       return err;
4198 +}
4199 +
4200 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4201 +             int *do_refresh)
4202 +{
4203 +       int err, rerr;
4204 +       aufs_bindex_t bindex;
4205 +       struct dentry *root;
4206 +       struct au_branch *br;
4207 +       struct au_br_fhsm *bf;
4208 +
4209 +       root = sb->s_root;
4210 +       bindex = au_find_dbindex(root, mod->h_root);
4211 +       if (bindex < 0) {
4212 +               if (remount)
4213 +                       return 0; /* success */
4214 +               err = -ENOENT;
4215 +               pr_err("%s no such branch\n", mod->path);
4216 +               goto out;
4217 +       }
4218 +       AuDbg("bindex b%d\n", bindex);
4219 +
4220 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4221 +       if (unlikely(err))
4222 +               goto out;
4223 +
4224 +       br = au_sbr(sb, bindex);
4225 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4226 +       if (br->br_perm == mod->perm)
4227 +               return 0; /* success */
4228 +
4229 +       /* pre-allocate for non-fhsm --> fhsm */
4230 +       bf = NULL;
4231 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4232 +               err = au_fhsm_br_alloc(br);
4233 +               if (unlikely(err))
4234 +                       goto out;
4235 +               bf = br->br_fhsm;
4236 +               br->br_fhsm = NULL;
4237 +       }
4238 +
4239 +       if (au_br_writable(br->br_perm)) {
4240 +               /* remove whiteout base */
4241 +               err = au_br_init_wh(sb, br, mod->perm);
4242 +               if (unlikely(err))
4243 +                       goto out_bf;
4244 +
4245 +               if (!au_br_writable(mod->perm)) {
4246 +                       /* rw --> ro, file might be mmapped */
4247 +                       DiMustNoWaiters(root);
4248 +                       IiMustNoWaiters(d_inode(root));
4249 +                       di_write_unlock(root);
4250 +                       err = au_br_mod_files_ro(sb, bindex);
4251 +                       /* aufs_write_lock() calls ..._child() */
4252 +                       di_write_lock_child(root);
4253 +
4254 +                       if (unlikely(err)) {
4255 +                               rerr = -ENOMEM;
4256 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4257 +                                                    GFP_NOFS);
4258 +                               if (br->br_wbr)
4259 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4260 +                               if (unlikely(rerr)) {
4261 +                                       AuIOErr("nested error %d (%d)\n",
4262 +                                               rerr, err);
4263 +                                       br->br_perm = mod->perm;
4264 +                               }
4265 +                       }
4266 +               }
4267 +       } else if (au_br_writable(mod->perm)) {
4268 +               /* ro --> rw */
4269 +               err = -ENOMEM;
4270 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4271 +               if (br->br_wbr) {
4272 +                       err = au_wbr_init(br, sb, mod->perm);
4273 +                       if (unlikely(err)) {
4274 +                               au_kfree_rcu(br->br_wbr);
4275 +                               br->br_wbr = NULL;
4276 +                       }
4277 +               }
4278 +       }
4279 +       if (unlikely(err))
4280 +               goto out_bf;
4281 +
4282 +       if (au_br_fhsm(br->br_perm)) {
4283 +               if (!au_br_fhsm(mod->perm)) {
4284 +                       /* fhsm --> non-fhsm */
4285 +                       au_br_fhsm_fin(br->br_fhsm);
4286 +                       au_kfree_rcu(br->br_fhsm);
4287 +                       br->br_fhsm = NULL;
4288 +               }
4289 +       } else if (au_br_fhsm(mod->perm))
4290 +               /* non-fhsm --> fhsm */
4291 +               br->br_fhsm = bf;
4292 +
4293 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4294 +       br->br_perm = mod->perm;
4295 +       goto out; /* success */
4296 +
4297 +out_bf:
4298 +       au_kfree_try_rcu(bf);
4299 +out:
4300 +       AuTraceErr(err);
4301 +       return err;
4302 +}
4303 +
4304 +/* ---------------------------------------------------------------------- */
4305 +
4306 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4307 +{
4308 +       int err;
4309 +       struct kstatfs kstfs;
4310 +
4311 +       err = vfs_statfs(&br->br_path, &kstfs);
4312 +       if (!err) {
4313 +               stfs->f_blocks = kstfs.f_blocks;
4314 +               stfs->f_bavail = kstfs.f_bavail;
4315 +               stfs->f_files = kstfs.f_files;
4316 +               stfs->f_ffree = kstfs.f_ffree;
4317 +       }
4318 +
4319 +       return err;
4320 +}
4321 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4322 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4323 +++ linux/fs/aufs/branch.h      2023-10-31 09:31:04.196547417 +0100
4324 @@ -0,0 +1,375 @@
4325 +/* SPDX-License-Identifier: GPL-2.0 */
4326 +/*
4327 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4328 + *
4329 + * This program is free software; you can redistribute it and/or modify
4330 + * it under the terms of the GNU General Public License as published by
4331 + * the Free Software Foundation; either version 2 of the License, or
4332 + * (at your option) any later version.
4333 + *
4334 + * This program is distributed in the hope that it will be useful,
4335 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4336 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4337 + * GNU General Public License for more details.
4338 + *
4339 + * You should have received a copy of the GNU General Public License
4340 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4341 + */
4342 +
4343 +/*
4344 + * branch filesystems and xino for them
4345 + */
4346 +
4347 +#ifndef __AUFS_BRANCH_H__
4348 +#define __AUFS_BRANCH_H__
4349 +
4350 +#ifdef __KERNEL__
4351 +
4352 +#include <linux/mount.h>
4353 +#include "dirren.h"
4354 +#include "dynop.h"
4355 +#include "lcnt.h"
4356 +#include "rwsem.h"
4357 +#include "super.h"
4358 +
4359 +/* ---------------------------------------------------------------------- */
4360 +
4361 +/* a xino file */
4362 +struct au_xino {
4363 +       struct file             **xi_file;
4364 +       unsigned int            xi_nfile;
4365 +
4366 +       struct {
4367 +               spinlock_t              spin;
4368 +               ino_t                   *array;
4369 +               int                     total;
4370 +               /* reserved for future use */
4371 +               /* unsigned long        *bitmap; */
4372 +               wait_queue_head_t       wqh;
4373 +       } xi_nondir;
4374 +
4375 +       struct mutex            xi_mtx; /* protects xi_file array */
4376 +       struct hlist_bl_head    xi_writing;
4377 +
4378 +       atomic_t                xi_truncating;
4379 +
4380 +       struct kref             xi_kref;
4381 +};
4382 +
4383 +/* File-based Hierarchical Storage Management */
4384 +struct au_br_fhsm {
4385 +#ifdef CONFIG_AUFS_FHSM
4386 +       struct mutex            bf_lock;
4387 +       unsigned long           bf_jiffy;
4388 +       struct aufs_stfs        bf_stfs;
4389 +       int                     bf_readable;
4390 +#endif
4391 +};
4392 +
4393 +/* members for writable branch only */
4394 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4395 +struct au_wbr {
4396 +       struct au_rwsem         wbr_wh_rwsem;
4397 +       struct dentry           *wbr_wh[AuBrWh_Last];
4398 +       atomic_t                wbr_wh_running;
4399 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4400 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4401 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4402 +
4403 +       /* mfs mode */
4404 +       unsigned long long      wbr_bytes;
4405 +};
4406 +
4407 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4408 +#define AuBrDynOp (AuDyLast * 4)
4409 +
4410 +#ifdef CONFIG_AUFS_HFSNOTIFY
4411 +/* support for asynchronous destruction */
4412 +struct au_br_hfsnotify {
4413 +       struct fsnotify_group   *hfsn_group;
4414 +};
4415 +#endif
4416 +
4417 +/* sysfs entries */
4418 +struct au_brsysfs {
4419 +       char                    name[16];
4420 +       struct attribute        attr;
4421 +};
4422 +
4423 +enum {
4424 +       AuBrSysfs_BR,
4425 +       AuBrSysfs_BRID,
4426 +       AuBrSysfs_Last
4427 +};
4428 +
4429 +/* protected by superblock rwsem */
4430 +struct au_branch {
4431 +       struct au_xino          *br_xino;
4432 +
4433 +       aufs_bindex_t           br_id;
4434 +
4435 +       int                     br_perm;
4436 +       struct path             br_path;
4437 +       spinlock_t              br_dykey_lock;
4438 +       struct au_dykey         *br_dykey[AuBrDynOp];
4439 +       au_lcnt_t               br_nfiles;      /* opened files */
4440 +       au_lcnt_t               br_count;       /* in-use for other */
4441 +
4442 +       struct au_wbr           *br_wbr;
4443 +       struct au_br_fhsm       *br_fhsm;
4444 +
4445 +#ifdef CONFIG_AUFS_HFSNOTIFY
4446 +       struct au_br_hfsnotify  *br_hfsn;
4447 +#endif
4448 +
4449 +#ifdef CONFIG_SYSFS
4450 +       /* entries under sysfs per mount-point */
4451 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4452 +#endif
4453 +
4454 +#ifdef CONFIG_DEBUG_FS
4455 +       struct dentry            *br_dbgaufs; /* xino */
4456 +#endif
4457 +
4458 +       struct au_dr_br         br_dirren;
4459 +};
4460 +
4461 +/* ---------------------------------------------------------------------- */
4462 +
4463 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4464 +{
4465 +       return br->br_path.mnt;
4466 +}
4467 +
4468 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4469 +{
4470 +       return br->br_path.dentry;
4471 +}
4472 +
4473 +static inline struct mnt_idmap *au_br_idmap(struct au_branch *br)
4474 +{
4475 +       return mnt_idmap(br->br_path.mnt);
4476 +}
4477 +
4478 +static inline struct super_block *au_br_sb(struct au_branch *br)
4479 +{
4480 +       return au_br_mnt(br)->mnt_sb;
4481 +}
4482 +
4483 +static inline int au_br_rdonly(struct au_branch *br)
4484 +{
4485 +       return (sb_rdonly(au_br_sb(br))
4486 +               || !au_br_writable(br->br_perm))
4487 +               ? -EROFS : 0;
4488 +}
4489 +
4490 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4491 +{
4492 +#ifdef CONFIG_AUFS_HNOTIFY
4493 +       return !(brperm & AuBrPerm_RR);
4494 +#else
4495 +       return 0;
4496 +#endif
4497 +}
4498 +
4499 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4500 +{
4501 +       int err, exec_flag;
4502 +
4503 +       err = 0;
4504 +       exec_flag = oflag & __FMODE_EXEC;
4505 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4506 +               err = -EACCES;
4507 +
4508 +       return err;
4509 +}
4510 +
4511 +static inline void au_xino_get(struct au_branch *br)
4512 +{
4513 +       struct au_xino *xi;
4514 +
4515 +       xi = br->br_xino;
4516 +       if (xi)
4517 +               kref_get(&xi->xi_kref);
4518 +}
4519 +
4520 +static inline int au_xino_count(struct au_branch *br)
4521 +{
4522 +       int v;
4523 +       struct au_xino *xi;
4524 +
4525 +       v = 0;
4526 +       xi = br->br_xino;
4527 +       if (xi)
4528 +               v = kref_read(&xi->xi_kref);
4529 +
4530 +       return v;
4531 +}
4532 +
4533 +/* ---------------------------------------------------------------------- */
4534 +
4535 +/* branch.c */
4536 +struct au_sbinfo;
4537 +void au_br_free(struct au_sbinfo *sinfo);
4538 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4539 +struct au_opt_add;
4540 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4541 +struct au_opt_del;
4542 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4543 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4544 +#ifdef CONFIG_COMPAT
4545 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4546 +#endif
4547 +struct au_opt_mod;
4548 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4549 +             int *do_refresh);
4550 +struct aufs_stfs;
4551 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4552 +
4553 +/* xino.c */
4554 +static const loff_t au_loff_max = LLONG_MAX;
4555 +
4556 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4557 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4558 +                           int wbrtop);
4559 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4560 +                            struct file *copy_src);
4561 +struct au_xi_new {
4562 +       struct au_xino *xi;     /* switch between xino and xigen */
4563 +       int idx;
4564 +       struct path *base;
4565 +       struct file *copy_src;
4566 +};
4567 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4568 +
4569 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4570 +                ino_t *ino);
4571 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4572 +                 ino_t ino);
4573 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4574 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4575 +
4576 +int au_xib_trunc(struct super_block *sb);
4577 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4578 +
4579 +struct au_xino *au_xino_alloc(unsigned int nfile);
4580 +int au_xino_put(struct au_branch *br);
4581 +struct file *au_xino_file1(struct au_xino *xi);
4582 +
4583 +struct au_opt_xino;
4584 +void au_xino_clr(struct super_block *sb);
4585 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4586 +struct file *au_xino_def(struct super_block *sb);
4587 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4588 +                   struct path *base);
4589 +
4590 +ino_t au_xino_new_ino(struct super_block *sb);
4591 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4592 +
4593 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4594 +                      ino_t h_ino, int idx);
4595 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4596 +                     int *idx);
4597 +
4598 +int au_xino_path(struct seq_file *seq, struct file *file);
4599 +
4600 +/* ---------------------------------------------------------------------- */
4601 +
4602 +/* @idx is signed to accept -1 meaning the first file */
4603 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4604 +{
4605 +       struct file *file;
4606 +
4607 +       file = NULL;
4608 +       if (!xi)
4609 +               goto out;
4610 +
4611 +       if (idx >= 0) {
4612 +               if (idx < xi->xi_nfile)
4613 +                       file = xi->xi_file[idx];
4614 +       } else
4615 +               file = au_xino_file1(xi);
4616 +
4617 +out:
4618 +       return file;
4619 +}
4620 +
4621 +/* ---------------------------------------------------------------------- */
4622 +
4623 +/* Superblock to branch */
4624 +static inline
4625 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4626 +{
4627 +       return au_sbr(sb, bindex)->br_id;
4628 +}
4629 +
4630 +static inline
4631 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4632 +{
4633 +       return au_br_mnt(au_sbr(sb, bindex));
4634 +}
4635 +
4636 +static inline
4637 +struct mnt_idmap *au_sbr_idmap(struct super_block *sb, aufs_bindex_t bindex)
4638 +{
4639 +       return au_br_idmap(au_sbr(sb, bindex));
4640 +}
4641 +
4642 +static inline
4643 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4644 +{
4645 +       return au_br_sb(au_sbr(sb, bindex));
4646 +}
4647 +
4648 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4649 +{
4650 +       return au_sbr(sb, bindex)->br_perm;
4651 +}
4652 +
4653 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4654 +{
4655 +       return au_br_whable(au_sbr_perm(sb, bindex));
4656 +}
4657 +
4658 +/* ---------------------------------------------------------------------- */
4659 +
4660 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4661 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4662 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4663 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4664 +/*
4665 +#define wbr_wh_read_trylock_nested(wbr) \
4666 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4667 +#define wbr_wh_write_trylock_nested(wbr) \
4668 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4669 +*/
4670 +
4671 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4672 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4673 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4674 +
4675 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4676 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4677 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4678 +
4679 +/* ---------------------------------------------------------------------- */
4680 +
4681 +#ifdef CONFIG_AUFS_FHSM
4682 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4683 +{
4684 +       mutex_init(&brfhsm->bf_lock);
4685 +       brfhsm->bf_jiffy = 0;
4686 +       brfhsm->bf_readable = 0;
4687 +}
4688 +
4689 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4690 +{
4691 +       mutex_destroy(&brfhsm->bf_lock);
4692 +}
4693 +#else
4694 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4695 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4696 +#endif
4697 +
4698 +#endif /* __KERNEL__ */
4699 +#endif /* __AUFS_BRANCH_H__ */
4700 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4701 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4702 +++ linux/fs/aufs/conf.mk       2022-11-05 23:02:18.959222617 +0100
4703 @@ -0,0 +1,40 @@
4704 +# SPDX-License-Identifier: GPL-2.0
4705 +
4706 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4707 +
4708 +define AuConf
4709 +ifdef ${1}
4710 +AuConfStr += ${1}=${${1}}
4711 +endif
4712 +endef
4713 +
4714 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4715 +       SBILIST \
4716 +       HNOTIFY HFSNOTIFY \
4717 +       EXPORT INO_T_64 \
4718 +       XATTR \
4719 +       FHSM \
4720 +       RDU \
4721 +       DIRREN \
4722 +       SHWH \
4723 +       BR_RAMFS \
4724 +       BR_FUSE POLL \
4725 +       BR_HFSPLUS \
4726 +       BDEV_LOOP \
4727 +       DEBUG MAGIC_SYSRQ
4728 +$(foreach i, ${AuConfAll}, \
4729 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4730 +
4731 +AuConfName = ${obj}/conf.str
4732 +${AuConfName}.tmp: FORCE
4733 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4734 +${AuConfName}: ${AuConfName}.tmp
4735 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4736 +       echo '  GEN    ' $@; \
4737 +       cp -p $< $@; \
4738 +       }
4739 +FORCE:
4740 +clean-files += ${AuConfName} ${AuConfName}.tmp
4741 +${obj}/sysfs.o: ${AuConfName}
4742 +
4743 +-include ${srctree}/${src}/conf_priv.mk
4744 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4745 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4746 +++ linux/fs/aufs/cpup.c        2023-10-31 09:31:04.196547417 +0100
4747 @@ -0,0 +1,1459 @@
4748 +// SPDX-License-Identifier: GPL-2.0
4749 +/*
4750 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4751 + *
4752 + * This program is free software; you can redistribute it and/or modify
4753 + * it under the terms of the GNU General Public License as published by
4754 + * the Free Software Foundation; either version 2 of the License, or
4755 + * (at your option) any later version.
4756 + *
4757 + * This program is distributed in the hope that it will be useful,
4758 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4759 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4760 + * GNU General Public License for more details.
4761 + *
4762 + * You should have received a copy of the GNU General Public License
4763 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4764 + */
4765 +
4766 +/*
4767 + * copy-up functions, see wbr_policy.c for copy-down
4768 + */
4769 +
4770 +#include <linux/fs_stack.h>
4771 +#include <linux/mm.h>
4772 +#include <linux/task_work.h>
4773 +#include "aufs.h"
4774 +
4775 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4776 +{
4777 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4778 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4779 +
4780 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4781 +
4782 +       dst->i_flags |= iflags & ~mask;
4783 +       if (au_test_fs_notime(dst->i_sb))
4784 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4785 +}
4786 +
4787 +void au_cpup_attr_timesizes(struct inode *inode)
4788 +{
4789 +       struct inode *h_inode;
4790 +
4791 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4792 +       fsstack_copy_attr_times(inode, h_inode);
4793 +       fsstack_copy_inode_size(inode, h_inode);
4794 +}
4795 +
4796 +void au_cpup_attr_nlink(struct inode *inode, int force)
4797 +{
4798 +       struct inode *h_inode;
4799 +       struct super_block *sb;
4800 +       aufs_bindex_t bindex, bbot;
4801 +
4802 +       sb = inode->i_sb;
4803 +       bindex = au_ibtop(inode);
4804 +       h_inode = au_h_iptr(inode, bindex);
4805 +       if (!force
4806 +           && !S_ISDIR(h_inode->i_mode)
4807 +           && au_opt_test(au_mntflags(sb), PLINK)
4808 +           && au_plink_test(inode))
4809 +               return;
4810 +
4811 +       /*
4812 +        * 0 can happen in revalidating.
4813 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4814 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4815 +        * case.
4816 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4817 +        *       the incorrect link count.
4818 +        */
4819 +       set_nlink(inode, h_inode->i_nlink);
4820 +
4821 +       /*
4822 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4823 +        * it may includes whplink directory.
4824 +        */
4825 +       if (S_ISDIR(h_inode->i_mode)) {
4826 +               bbot = au_ibbot(inode);
4827 +               for (bindex++; bindex <= bbot; bindex++) {
4828 +                       h_inode = au_h_iptr(inode, bindex);
4829 +                       if (h_inode)
4830 +                               au_add_nlink(inode, h_inode);
4831 +               }
4832 +       }
4833 +}
4834 +
4835 +void au_cpup_attr_changeable(struct inode *inode)
4836 +{
4837 +       struct inode *h_inode;
4838 +
4839 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4840 +       inode->i_mode = h_inode->i_mode;
4841 +       inode->i_uid = h_inode->i_uid;
4842 +       inode->i_gid = h_inode->i_gid;
4843 +       au_cpup_attr_timesizes(inode);
4844 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4845 +}
4846 +
4847 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4848 +{
4849 +       struct au_iinfo *iinfo = au_ii(inode);
4850 +
4851 +       IiMustWriteLock(inode);
4852 +
4853 +       iinfo->ii_higen = h_inode->i_generation;
4854 +       iinfo->ii_hsb1 = h_inode->i_sb;
4855 +}
4856 +
4857 +void au_cpup_attr_all(struct inode *inode, int force)
4858 +{
4859 +       struct inode *h_inode;
4860 +
4861 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4862 +       au_cpup_attr_changeable(inode);
4863 +       if (inode->i_nlink > 0)
4864 +               au_cpup_attr_nlink(inode, force);
4865 +       inode->i_rdev = h_inode->i_rdev;
4866 +       inode->i_blkbits = h_inode->i_blkbits;
4867 +       au_cpup_igen(inode, h_inode);
4868 +}
4869 +
4870 +/* ---------------------------------------------------------------------- */
4871 +
4872 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4873 +
4874 +/* keep the timestamps of the parent dir when cpup */
4875 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4876 +                   struct path *h_path)
4877 +{
4878 +       struct inode *h_inode;
4879 +
4880 +       dt->dt_dentry = dentry;
4881 +       dt->dt_h_path = *h_path;
4882 +       h_inode = d_inode(h_path->dentry);
4883 +       dt->dt_atime = h_inode->i_atime;
4884 +       dt->dt_mtime = h_inode->i_mtime;
4885 +       /* smp_mb(); */
4886 +}
4887 +
4888 +void au_dtime_revert(struct au_dtime *dt)
4889 +{
4890 +       struct iattr attr;
4891 +       int err;
4892 +
4893 +       attr.ia_atime = dt->dt_atime;
4894 +       attr.ia_mtime = dt->dt_mtime;
4895 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4896 +               | ATTR_ATIME | ATTR_ATIME_SET;
4897 +
4898 +       /* no delegation since this is a directory */
4899 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4900 +       if (unlikely(err))
4901 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4902 +}
4903 +
4904 +/* ---------------------------------------------------------------------- */
4905 +
4906 +/* internal use only */
4907 +struct au_cpup_reg_attr {
4908 +       int             valid;
4909 +       struct kstat    st;
4910 +       unsigned int    iflags; /* inode->i_flags */
4911 +};
4912 +
4913 +static noinline_for_stack
4914 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4915 +              struct au_cpup_reg_attr *h_src_attr)
4916 +{
4917 +       int err, sbits, icex;
4918 +       unsigned int mnt_flags;
4919 +       unsigned char verbose;
4920 +       struct iattr ia;
4921 +       struct path h_path;
4922 +       struct inode *h_isrc, *h_idst;
4923 +       struct kstat *h_st;
4924 +       struct au_branch *br;
4925 +
4926 +       br = au_sbr(dst->d_sb, bindex);
4927 +       h_path.mnt = au_br_mnt(br);
4928 +       h_path.dentry = au_h_dptr(dst, bindex);
4929 +       h_idst = d_inode(h_path.dentry);
4930 +       h_isrc = d_inode(h_src->dentry);
4931 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4932 +               | ATTR_ATIME | ATTR_MTIME
4933 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4934 +       if (h_src_attr && h_src_attr->valid) {
4935 +               h_st = &h_src_attr->st;
4936 +               ia.ia_uid = h_st->uid;
4937 +               ia.ia_gid = h_st->gid;
4938 +               ia.ia_atime = h_st->atime;
4939 +               ia.ia_mtime = h_st->mtime;
4940 +               if (h_idst->i_mode != h_st->mode
4941 +                   && !S_ISLNK(h_idst->i_mode)) {
4942 +                       ia.ia_valid |= ATTR_MODE;
4943 +                       ia.ia_mode = h_st->mode;
4944 +               }
4945 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4946 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4947 +       } else {
4948 +               ia.ia_uid = h_isrc->i_uid;
4949 +               ia.ia_gid = h_isrc->i_gid;
4950 +               ia.ia_atime = h_isrc->i_atime;
4951 +               ia.ia_mtime = h_isrc->i_mtime;
4952 +               if (h_idst->i_mode != h_isrc->i_mode
4953 +                   && !S_ISLNK(h_idst->i_mode)) {
4954 +                       ia.ia_valid |= ATTR_MODE;
4955 +                       ia.ia_mode = h_isrc->i_mode;
4956 +               }
4957 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4958 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4959 +       }
4960 +       /* no delegation since it is just created */
4961 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4962 +
4963 +       /* is this nfs only? */
4964 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4965 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4966 +               ia.ia_mode = h_isrc->i_mode;
4967 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4968 +       }
4969 +
4970 +       icex = br->br_perm & AuBrAttr_ICEX;
4971 +       if (!err) {
4972 +               mnt_flags = au_mntflags(dst->d_sb);
4973 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4974 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4975 +       }
4976 +
4977 +       return err;
4978 +}
4979 +
4980 +/* ---------------------------------------------------------------------- */
4981 +
4982 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4983 +                          char *buf, unsigned long blksize)
4984 +{
4985 +       int err;
4986 +       size_t sz, rbytes, wbytes;
4987 +       unsigned char all_zero;
4988 +       char *p, *zp;
4989 +       struct inode *h_inode;
4990 +       /* reduce stack usage */
4991 +       struct iattr *ia;
4992 +
4993 +       zp = page_address(ZERO_PAGE(0));
4994 +       if (unlikely(!zp))
4995 +               return -ENOMEM; /* possible? */
4996 +
4997 +       err = 0;
4998 +       all_zero = 0;
4999 +       while (len) {
5000 +               AuDbg("len %lld\n", len);
5001 +               sz = blksize;
5002 +               if (len < blksize)
5003 +                       sz = len;
5004 +
5005 +               rbytes = 0;
5006 +               /* todo: signal_pending? */
5007 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5008 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5009 +                       err = rbytes;
5010 +               }
5011 +               if (unlikely(err < 0))
5012 +                       break;
5013 +
5014 +               all_zero = 0;
5015 +               if (len >= rbytes && rbytes == blksize)
5016 +                       all_zero = !memcmp(buf, zp, rbytes);
5017 +               if (!all_zero) {
5018 +                       wbytes = rbytes;
5019 +                       p = buf;
5020 +                       while (wbytes) {
5021 +                               size_t b;
5022 +
5023 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5024 +                               err = b;
5025 +                               /* todo: signal_pending? */
5026 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5027 +                                       continue;
5028 +                               if (unlikely(err < 0))
5029 +                                       break;
5030 +                               wbytes -= b;
5031 +                               p += b;
5032 +                       }
5033 +                       if (unlikely(err < 0))
5034 +                               break;
5035 +               } else {
5036 +                       loff_t res;
5037 +
5038 +                       AuLabel(hole);
5039 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5040 +                       err = res;
5041 +                       if (unlikely(res < 0))
5042 +                               break;
5043 +               }
5044 +               len -= rbytes;
5045 +               err = 0;
5046 +       }
5047 +
5048 +       /* the last block may be a hole */
5049 +       if (!err && all_zero) {
5050 +               AuLabel(last hole);
5051 +
5052 +               err = 1;
5053 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5054 +                       /* nfs requires this step to make last hole */
5055 +                       /* is this only nfs? */
5056 +                       do {
5057 +                               /* todo: signal_pending? */
5058 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5059 +                       } while (err == -EAGAIN || err == -EINTR);
5060 +                       if (err == 1)
5061 +                               dst->f_pos--;
5062 +               }
5063 +
5064 +               if (err == 1) {
5065 +                       ia = (void *)buf;
5066 +                       ia->ia_size = dst->f_pos;
5067 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5068 +                       ia->ia_file = dst;
5069 +                       h_inode = file_inode(dst);
5070 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5071 +                       /* no delegation since it is just created */
5072 +                       err = vfsub_notify_change(&dst->f_path, ia,
5073 +                                                 /*delegated*/NULL);
5074 +                       inode_unlock(h_inode);
5075 +               }
5076 +       }
5077 +
5078 +       return err;
5079 +}
5080 +
5081 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5082 +{
5083 +       int err;
5084 +       unsigned long blksize;
5085 +       unsigned char do_kfree;
5086 +       char *buf;
5087 +       struct super_block *h_sb;
5088 +
5089 +       err = -ENOMEM;
5090 +       h_sb = file_inode(dst)->i_sb;
5091 +       blksize = h_sb->s_blocksize;
5092 +       if (!blksize || PAGE_SIZE < blksize)
5093 +               blksize = PAGE_SIZE;
5094 +       AuDbg("blksize %lu\n", blksize);
5095 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5096 +       if (do_kfree)
5097 +               buf = kmalloc(blksize, GFP_NOFS);
5098 +       else
5099 +               buf = (void *)__get_free_page(GFP_NOFS);
5100 +       if (unlikely(!buf))
5101 +               goto out;
5102 +
5103 +       if (len > (1 << 22))
5104 +               AuDbg("copying a large file %lld\n", (long long)len);
5105 +
5106 +       src->f_pos = 0;
5107 +       dst->f_pos = 0;
5108 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5109 +       if (do_kfree) {
5110 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5111 +               au_kfree_do_rcu(buf);
5112 +       } else
5113 +               free_page((unsigned long)buf);
5114 +
5115 +out:
5116 +       return err;
5117 +}
5118 +
5119 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5120 +{
5121 +       int err;
5122 +       struct super_block *h_src_sb;
5123 +       struct inode *h_src_inode;
5124 +
5125 +       h_src_inode = file_inode(src);
5126 +       h_src_sb = h_src_inode->i_sb;
5127 +
5128 +       /* XFS acquires inode_lock */
5129 +       if (!au_test_xfs(h_src_sb))
5130 +               err = au_copy_file(dst, src, len);
5131 +       else {
5132 +               inode_unlock_shared(h_src_inode);
5133 +               err = au_copy_file(dst, src, len);
5134 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5135 +       }
5136 +
5137 +       return err;
5138 +}
5139 +
5140 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5141 +{
5142 +       int err;
5143 +       loff_t lo;
5144 +       struct super_block *h_src_sb;
5145 +       struct inode *h_src_inode;
5146 +
5147 +       h_src_inode = file_inode(src);
5148 +       h_src_sb = h_src_inode->i_sb;
5149 +       if (h_src_sb != file_inode(dst)->i_sb
5150 +           || !dst->f_op->remap_file_range) {
5151 +               err = au_do_copy(dst, src, len);
5152 +               goto out;
5153 +       }
5154 +
5155 +       if (!au_test_nfs(h_src_sb)) {
5156 +               inode_unlock_shared(h_src_inode);
5157 +               lo = vfsub_clone_file_range(src, dst, len);
5158 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5159 +       } else
5160 +               lo = vfsub_clone_file_range(src, dst, len);
5161 +       if (lo == len) {
5162 +               err = 0;
5163 +               goto out; /* success */
5164 +       } else if (lo >= 0)
5165 +               /* todo: possible? */
5166 +               /* paritially succeeded */
5167 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5168 +       else if (lo != -EOPNOTSUPP) {
5169 +               /* older XFS has a condition in cloning */
5170 +               err = lo;
5171 +               goto out;
5172 +       }
5173 +
5174 +       /* the backend fs on NFS may not support cloning */
5175 +       err = au_do_copy(dst, src, len);
5176 +
5177 +out:
5178 +       AuTraceErr(err);
5179 +       return err;
5180 +}
5181 +
5182 +/*
5183 + * to support a sparse file which is opened with O_APPEND,
5184 + * we need to close the file.
5185 + */
5186 +static int au_cp_regular(struct au_cp_generic *cpg)
5187 +{
5188 +       int err, i;
5189 +       enum { SRC, DST };
5190 +       struct {
5191 +               aufs_bindex_t bindex;
5192 +               unsigned int flags;
5193 +               struct dentry *dentry;
5194 +               int force_wr;
5195 +               struct file *file;
5196 +       } *f, file[] = {
5197 +               {
5198 +                       .bindex = cpg->bsrc,
5199 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5200 +               },
5201 +               {
5202 +                       .bindex = cpg->bdst,
5203 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5204 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5205 +               }
5206 +       };
5207 +       struct au_branch *br;
5208 +       struct super_block *sb, *h_src_sb;
5209 +       struct inode *h_src_inode;
5210 +       struct task_struct *tsk = current;
5211 +
5212 +       /* bsrc branch can be ro/rw. */
5213 +       sb = cpg->dentry->d_sb;
5214 +       f = file;
5215 +       for (i = 0; i < 2; i++, f++) {
5216 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5217 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5218 +                                   /*file*/NULL, f->force_wr);
5219 +               if (IS_ERR(f->file)) {
5220 +                       err = PTR_ERR(f->file);
5221 +                       if (i == SRC)
5222 +                               goto out;
5223 +                       else
5224 +                               goto out_src;
5225 +               }
5226 +       }
5227 +
5228 +       /* try stopping to update while we copyup */
5229 +       h_src_inode = d_inode(file[SRC].dentry);
5230 +       h_src_sb = h_src_inode->i_sb;
5231 +       if (!au_test_nfs(h_src_sb))
5232 +               IMustLock(h_src_inode);
5233 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5234 +
5235 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5236 +       if (tsk->flags & PF_KTHREAD)
5237 +               __fput_sync(file[DST].file);
5238 +       else {
5239 +               /* it happened actually */
5240 +               fput(file[DST].file);
5241 +               /*
5242 +                * too bad.
5243 +                * we have to call both since we don't know which place the file
5244 +                * was added to.
5245 +                */
5246 +               task_work_run();
5247 +               flush_delayed_fput();
5248 +       }
5249 +       br = au_sbr(sb, file[DST].bindex);
5250 +       au_lcnt_dec(&br->br_nfiles);
5251 +
5252 +out_src:
5253 +       fput(file[SRC].file);
5254 +       br = au_sbr(sb, file[SRC].bindex);
5255 +       au_lcnt_dec(&br->br_nfiles);
5256 +out:
5257 +       return err;
5258 +}
5259 +
5260 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5261 +                             struct au_cpup_reg_attr *h_src_attr)
5262 +{
5263 +       int err, rerr;
5264 +       loff_t l;
5265 +       struct path h_path;
5266 +       struct inode *h_src_inode, *h_dst_inode;
5267 +
5268 +       err = 0;
5269 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5270 +       l = i_size_read(h_src_inode);
5271 +       if (cpg->len == -1 || l < cpg->len)
5272 +               cpg->len = l;
5273 +       if (cpg->len) {
5274 +               /* try stopping to update while we are referencing */
5275 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5276 +               au_pin_hdir_unlock(cpg->pin);
5277 +
5278 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5279 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5280 +               h_src_attr->iflags = h_src_inode->i_flags;
5281 +               if (!au_test_nfs(h_src_inode->i_sb))
5282 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5283 +               else {
5284 +                       inode_unlock_shared(h_src_inode);
5285 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5286 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5287 +               }
5288 +               if (unlikely(err)) {
5289 +                       inode_unlock_shared(h_src_inode);
5290 +                       goto out;
5291 +               }
5292 +               h_src_attr->valid = 1;
5293 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5294 +                       err = au_cp_regular(cpg);
5295 +                       inode_unlock_shared(h_src_inode);
5296 +               } else {
5297 +                       inode_unlock_shared(h_src_inode);
5298 +                       err = au_cp_regular(cpg);
5299 +               }
5300 +               rerr = au_pin_hdir_relock(cpg->pin);
5301 +               if (!err && rerr)
5302 +                       err = rerr;
5303 +       }
5304 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5305 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5306 +               h_dst_inode = d_inode(h_path.dentry);
5307 +               spin_lock(&h_dst_inode->i_lock);
5308 +               h_dst_inode->i_state |= I_LINKABLE;
5309 +               spin_unlock(&h_dst_inode->i_lock);
5310 +       }
5311 +
5312 +out:
5313 +       return err;
5314 +}
5315 +
5316 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5317 +                             struct inode *h_dir)
5318 +{
5319 +       int err;
5320 +       DEFINE_DELAYED_CALL(done);
5321 +       const char *sym;
5322 +
5323 +       sym = vfs_get_link(h_src, &done);
5324 +       err = PTR_ERR(sym);
5325 +       if (IS_ERR(sym))
5326 +               goto out;
5327 +
5328 +       err = vfsub_symlink(h_dir, h_path, sym);
5329 +
5330 +out:
5331 +       do_delayed_call(&done);
5332 +       return err;
5333 +}
5334 +
5335 +/*
5336 + * regardless 'acl' option, reset all ACL.
5337 + * All ACL will be copied up later from the original entry on the lower branch.
5338 + */
5339 +static int au_reset_acl(struct path *h_path, umode_t mode)
5340 +{
5341 +       int err;
5342 +       struct dentry *h_dentry;
5343 +       /* struct inode *h_inode; */
5344 +       struct mnt_idmap *h_idmap;
5345 +
5346 +       h_idmap = mnt_idmap(h_path->mnt);
5347 +       h_dentry = h_path->dentry;
5348 +       /* h_inode = d_inode(h_dentry); */
5349 +       /* forget_all_cached_acls(h_inode)); */
5350 +       err = vfsub_remove_acl(h_idmap, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5351 +       AuTraceErr(err);
5352 +       if (err == -EOPNOTSUPP)
5353 +               err = 0;
5354 +       if (!err)
5355 +               err = vfsub_acl_chmod(h_idmap, h_dentry, mode);
5356 +
5357 +       AuTraceErr(err);
5358 +       return err;
5359 +}
5360 +
5361 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5362 +                         struct inode *h_dir, struct path *h_path)
5363 +{
5364 +       int err;
5365 +       struct inode *dir, *inode;
5366 +       struct mnt_idmap *h_idmap;
5367 +
5368 +       h_idmap = mnt_idmap(h_path->mnt);
5369 +       err = vfsub_remove_acl(h_idmap, h_path->dentry,
5370 +                              XATTR_NAME_POSIX_ACL_DEFAULT);
5371 +       AuTraceErr(err);
5372 +       if (err == -EOPNOTSUPP)
5373 +               err = 0;
5374 +       if (unlikely(err))
5375 +               goto out;
5376 +
5377 +       /*
5378 +        * strange behaviour from the users view,
5379 +        * particularly setattr case
5380 +        */
5381 +       dir = d_inode(dst_parent);
5382 +       if (au_ibtop(dir) == cpg->bdst)
5383 +               au_cpup_attr_nlink(dir, /*force*/1);
5384 +       inode = d_inode(cpg->dentry);
5385 +       au_cpup_attr_nlink(inode, /*force*/1);
5386 +
5387 +out:
5388 +       return err;
5389 +}
5390 +
5391 +static noinline_for_stack
5392 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5393 +              struct au_cpup_reg_attr *h_src_attr)
5394 +{
5395 +       int err;
5396 +       umode_t mode;
5397 +       unsigned int mnt_flags;
5398 +       unsigned char isdir, isreg, force;
5399 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5400 +       struct au_dtime dt;
5401 +       struct path h_path;
5402 +       struct dentry *h_src, *h_dst, *h_parent;
5403 +       struct inode *h_inode, *h_dir;
5404 +       struct super_block *sb;
5405 +
5406 +       /* bsrc branch can be ro/rw. */
5407 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5408 +       h_inode = d_inode(h_src);
5409 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5410 +
5411 +       /* try stopping to be referenced while we are creating */
5412 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5413 +       if (au_ftest_cpup(cpg->flags, RENAME))
5414 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5415 +                                 AUFS_WH_PFX_LEN));
5416 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5417 +       h_dir = d_inode(h_parent);
5418 +       IMustLock(h_dir);
5419 +       AuDebugOn(h_parent != h_dst->d_parent);
5420 +
5421 +       sb = cpg->dentry->d_sb;
5422 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5423 +       if (do_dt) {
5424 +               h_path.dentry = h_parent;
5425 +               au_dtime_store(&dt, dst_parent, &h_path);
5426 +       }
5427 +       h_path.dentry = h_dst;
5428 +
5429 +       isreg = 0;
5430 +       isdir = 0;
5431 +       mode = h_inode->i_mode;
5432 +       switch (mode & S_IFMT) {
5433 +       case S_IFREG:
5434 +               isreg = 1;
5435 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5436 +               if (!err)
5437 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5438 +               break;
5439 +       case S_IFDIR:
5440 +               isdir = 1;
5441 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5442 +               if (!err)
5443 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5444 +               break;
5445 +       case S_IFLNK:
5446 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5447 +               break;
5448 +       case S_IFCHR:
5449 +       case S_IFBLK:
5450 +               AuDebugOn(!capable(CAP_MKNOD));
5451 +               fallthrough;
5452 +       case S_IFIFO:
5453 +       case S_IFSOCK:
5454 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5455 +               break;
5456 +       default:
5457 +               AuIOErr("Unknown inode type 0%o\n", mode);
5458 +               err = -EIO;
5459 +       }
5460 +       if (!err)
5461 +               err = au_reset_acl(&h_path, mode);
5462 +
5463 +       mnt_flags = au_mntflags(sb);
5464 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5465 +           && !isdir
5466 +           && au_opt_test(mnt_flags, XINO)
5467 +           && (h_inode->i_nlink == 1
5468 +               || (h_inode->i_state & I_LINKABLE))
5469 +           /* todo: unnecessary? */
5470 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5471 +           && cpg->bdst < cpg->bsrc
5472 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5473 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5474 +               /* ignore this error */
5475 +
5476 +       if (!err) {
5477 +               force = 0;
5478 +               if (isreg) {
5479 +                       force = !!cpg->len;
5480 +                       if (cpg->len == -1)
5481 +                               force = !!i_size_read(h_inode);
5482 +               }
5483 +               au_fhsm_wrote(sb, cpg->bdst, force);
5484 +       }
5485 +
5486 +       if (do_dt)
5487 +               au_dtime_revert(&dt);
5488 +       return err;
5489 +}
5490 +
5491 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5492 +{
5493 +       int err;
5494 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5495 +       struct path h_ppath;
5496 +       struct inode *h_dir;
5497 +       aufs_bindex_t bdst;
5498 +
5499 +       dentry = cpg->dentry;
5500 +       bdst = cpg->bdst;
5501 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5502 +       h_dentry = au_h_dptr(dentry, bdst);
5503 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5504 +               dget(h_dentry);
5505 +               au_set_h_dptr(dentry, bdst, NULL);
5506 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5507 +               if (!err)
5508 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5509 +               au_set_h_dptr(dentry, bdst, h_dentry);
5510 +       } else {
5511 +               err = 0;
5512 +               parent = dget_parent(dentry);
5513 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5514 +               dput(parent);
5515 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5516 +               if (IS_ERR(h_path->dentry))
5517 +                       err = PTR_ERR(h_path->dentry);
5518 +       }
5519 +       if (unlikely(err))
5520 +               goto out;
5521 +
5522 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5523 +       h_dir = d_inode(h_parent);
5524 +       IMustLock(h_dir);
5525 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5526 +       /* no delegation since it is just created */
5527 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5528 +                          /*flags*/0);
5529 +       dput(h_path->dentry);
5530 +
5531 +out:
5532 +       return err;
5533 +}
5534 +
5535 +/*
5536 + * copyup the @dentry from @bsrc to @bdst.
5537 + * the caller must set the both of lower dentries.
5538 + * @len is for truncating when it is -1 copyup the entire file.
5539 + * in link/rename cases, @dst_parent may be different from the real one.
5540 + * basic->bsrc can be larger than basic->bdst.
5541 + * aufs doesn't touch the credential so
5542 + * security_inode_copy_up{,_xattr}() are unnecessary.
5543 + */
5544 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5545 +{
5546 +       int err, rerr;
5547 +       aufs_bindex_t old_ibtop;
5548 +       unsigned char isdir, plink;
5549 +       struct dentry *h_src, *h_dst, *h_parent;
5550 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5551 +       struct super_block *sb;
5552 +       struct au_branch *br;
5553 +       struct path h_src_path;
5554 +       /* to reduce stack size */
5555 +       struct {
5556 +               struct au_dtime dt;
5557 +               struct path h_path;
5558 +               struct au_cpup_reg_attr h_src_attr;
5559 +       } *a;
5560 +
5561 +       err = -ENOMEM;
5562 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5563 +       if (unlikely(!a))
5564 +               goto out;
5565 +       a->h_src_attr.valid = 0;
5566 +
5567 +       sb = cpg->dentry->d_sb;
5568 +       br = au_sbr(sb, cpg->bdst);
5569 +       a->h_path.mnt = au_br_mnt(br);
5570 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5571 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5572 +       h_dir = d_inode(h_parent);
5573 +       IMustLock(h_dir);
5574 +
5575 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5576 +       inode = d_inode(cpg->dentry);
5577 +
5578 +       if (!dst_parent)
5579 +               dst_parent = dget_parent(cpg->dentry);
5580 +       else
5581 +               dget(dst_parent);
5582 +
5583 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5584 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5585 +       if (dst_inode) {
5586 +               if (unlikely(!plink)) {
5587 +                       err = -EIO;
5588 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5589 +                               "but plink is disabled\n",
5590 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5591 +                       goto out_parent;
5592 +               }
5593 +
5594 +               if (dst_inode->i_nlink) {
5595 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5596 +
5597 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5598 +                       err = PTR_ERR(h_src);
5599 +                       if (IS_ERR(h_src))
5600 +                               goto out_parent;
5601 +                       if (unlikely(d_is_negative(h_src))) {
5602 +                               err = -EIO;
5603 +                               AuIOErr("i%lu exists on b%d "
5604 +                                       "but not pseudo-linked\n",
5605 +                                       inode->i_ino, cpg->bdst);
5606 +                               dput(h_src);
5607 +                               goto out_parent;
5608 +                       }
5609 +
5610 +                       if (do_dt) {
5611 +                               a->h_path.dentry = h_parent;
5612 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5613 +                       }
5614 +
5615 +                       a->h_path.dentry = h_dst;
5616 +                       delegated = NULL;
5617 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5618 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5619 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5620 +                       if (do_dt)
5621 +                               au_dtime_revert(&a->dt);
5622 +                       if (unlikely(err == -EWOULDBLOCK)) {
5623 +                               pr_warn("cannot retry for NFSv4 delegation"
5624 +                                       " for an internal link\n");
5625 +                               iput(delegated);
5626 +                       }
5627 +                       dput(h_src);
5628 +                       goto out_parent;
5629 +               } else
5630 +                       /* todo: cpup_wh_file? */
5631 +                       /* udba work */
5632 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5633 +       }
5634 +
5635 +       isdir = S_ISDIR(inode->i_mode);
5636 +       old_ibtop = au_ibtop(inode);
5637 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5638 +       if (unlikely(err))
5639 +               goto out_rev;
5640 +       dst_inode = d_inode(h_dst);
5641 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5642 +       /* todo: necessary? */
5643 +       /* au_pin_hdir_unlock(cpg->pin); */
5644 +
5645 +       h_src_path.dentry = h_src;
5646 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5647 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5648 +       if (unlikely(err)) {
5649 +               /* todo: necessary? */
5650 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5651 +               inode_unlock(dst_inode);
5652 +               goto out_rev;
5653 +       }
5654 +
5655 +       if (cpg->bdst < old_ibtop) {
5656 +               if (S_ISREG(inode->i_mode)) {
5657 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5658 +                       if (unlikely(err)) {
5659 +                               /* ignore an error */
5660 +                               /* au_pin_hdir_relock(cpg->pin); */
5661 +                               inode_unlock(dst_inode);
5662 +                               goto out_rev;
5663 +                       }
5664 +               }
5665 +               au_set_ibtop(inode, cpg->bdst);
5666 +       } else
5667 +               au_set_ibbot(inode, cpg->bdst);
5668 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5669 +                     au_hi_flags(inode, isdir));
5670 +
5671 +       /* todo: necessary? */
5672 +       /* err = au_pin_hdir_relock(cpg->pin); */
5673 +       inode_unlock(dst_inode);
5674 +       if (unlikely(err))
5675 +               goto out_rev;
5676 +
5677 +       src_inode = d_inode(h_src);
5678 +       if (!isdir
5679 +           && (src_inode->i_nlink > 1
5680 +               || src_inode->i_state & I_LINKABLE)
5681 +           && plink)
5682 +               au_plink_append(inode, cpg->bdst, h_dst);
5683 +
5684 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5685 +               a->h_path.dentry = h_dst;
5686 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5687 +       }
5688 +       if (!err)
5689 +               goto out_parent; /* success */
5690 +
5691 +       /* revert */
5692 +out_rev:
5693 +       a->h_path.dentry = h_parent;
5694 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5695 +       a->h_path.dentry = h_dst;
5696 +       rerr = 0;
5697 +       if (d_is_positive(h_dst)) {
5698 +               if (!isdir) {
5699 +                       /* no delegation since it is just created */
5700 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5701 +                                           /*delegated*/NULL, /*force*/0);
5702 +               } else
5703 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5704 +       }
5705 +       au_dtime_revert(&a->dt);
5706 +       if (rerr) {
5707 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5708 +               err = -EIO;
5709 +       }
5710 +out_parent:
5711 +       dput(dst_parent);
5712 +       au_kfree_rcu(a);
5713 +out:
5714 +       return err;
5715 +}
5716 +
5717 +#if 0 /* reserved */
5718 +struct au_cpup_single_args {
5719 +       int *errp;
5720 +       struct au_cp_generic *cpg;
5721 +       struct dentry *dst_parent;
5722 +};
5723 +
5724 +static void au_call_cpup_single(void *args)
5725 +{
5726 +       struct au_cpup_single_args *a = args;
5727 +
5728 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5729 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5730 +       au_pin_hdir_release(a->cpg->pin);
5731 +}
5732 +#endif
5733 +
5734 +/*
5735 + * prevent SIGXFSZ in copy-up.
5736 + * testing CAP_MKNOD is for generic fs,
5737 + * but CAP_FSETID is for xfs only, currently.
5738 + */
5739 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5740 +{
5741 +       int do_sio;
5742 +       struct super_block *sb;
5743 +       struct inode *h_dir;
5744 +
5745 +       do_sio = 0;
5746 +       sb = au_pinned_parent(pin)->d_sb;
5747 +       if (!au_wkq_test()
5748 +           && (!au_sbi(sb)->si_plink_maint_pid
5749 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5750 +               switch (mode & S_IFMT) {
5751 +               case S_IFREG:
5752 +                       /* no condition about RLIMIT_FSIZE and the file size */
5753 +                       do_sio = 1;
5754 +                       break;
5755 +               case S_IFCHR:
5756 +               case S_IFBLK:
5757 +                       do_sio = !capable(CAP_MKNOD);
5758 +                       break;
5759 +               }
5760 +               if (!do_sio)
5761 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5762 +                                 && !capable(CAP_FSETID));
5763 +               /* this workaround may be removed in the future */
5764 +               if (!do_sio) {
5765 +                       h_dir = au_pinned_h_dir(pin);
5766 +                       do_sio = h_dir->i_mode & S_ISVTX;
5767 +               }
5768 +       }
5769 +
5770 +       return do_sio;
5771 +}
5772 +
5773 +#if 0 /* reserved */
5774 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5775 +{
5776 +       int err, wkq_err;
5777 +       struct dentry *h_dentry;
5778 +
5779 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5780 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5781 +               err = au_cpup_single(cpg, dst_parent);
5782 +       else {
5783 +               struct au_cpup_single_args args = {
5784 +                       .errp           = &err,
5785 +                       .cpg            = cpg,
5786 +                       .dst_parent     = dst_parent
5787 +               };
5788 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5789 +               if (unlikely(wkq_err))
5790 +                       err = wkq_err;
5791 +       }
5792 +
5793 +       return err;
5794 +}
5795 +#endif
5796 +
5797 +/*
5798 + * copyup the @dentry from the first active lower branch to @bdst,
5799 + * using au_cpup_single().
5800 + */
5801 +static int au_cpup_simple(struct au_cp_generic *cpg)
5802 +{
5803 +       int err;
5804 +       unsigned int flags_orig;
5805 +       struct dentry *dentry;
5806 +
5807 +       AuDebugOn(cpg->bsrc < 0);
5808 +
5809 +       dentry = cpg->dentry;
5810 +       DiMustWriteLock(dentry);
5811 +
5812 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5813 +       if (!err) {
5814 +               flags_orig = cpg->flags;
5815 +               au_fset_cpup(cpg->flags, RENAME);
5816 +               err = au_cpup_single(cpg, NULL);
5817 +               cpg->flags = flags_orig;
5818 +               if (!err)
5819 +                       return 0; /* success */
5820 +
5821 +               /* revert */
5822 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5823 +               au_set_dbtop(dentry, cpg->bsrc);
5824 +       }
5825 +
5826 +       return err;
5827 +}
5828 +
5829 +struct au_cpup_simple_args {
5830 +       int *errp;
5831 +       struct au_cp_generic *cpg;
5832 +};
5833 +
5834 +static void au_call_cpup_simple(void *args)
5835 +{
5836 +       struct au_cpup_simple_args *a = args;
5837 +
5838 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5839 +       *a->errp = au_cpup_simple(a->cpg);
5840 +       au_pin_hdir_release(a->cpg->pin);
5841 +}
5842 +
5843 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5844 +{
5845 +       int err, wkq_err;
5846 +       struct dentry *dentry, *parent;
5847 +       struct file *h_file;
5848 +       struct inode *h_dir;
5849 +       struct mnt_idmap *h_idmap;
5850 +
5851 +       dentry = cpg->dentry;
5852 +       h_file = NULL;
5853 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5854 +               AuDebugOn(cpg->bsrc < 0);
5855 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5856 +               err = PTR_ERR(h_file);
5857 +               if (IS_ERR(h_file))
5858 +                       goto out;
5859 +       }
5860 +
5861 +       parent = dget_parent(dentry);
5862 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5863 +       h_idmap = au_sbr_idmap(dentry->d_sb, cpg->bdst);
5864 +       if (!au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC | MAY_WRITE)
5865 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5866 +               err = au_cpup_simple(cpg);
5867 +       else {
5868 +               struct au_cpup_simple_args args = {
5869 +                       .errp           = &err,
5870 +                       .cpg            = cpg
5871 +               };
5872 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5873 +               if (unlikely(wkq_err))
5874 +                       err = wkq_err;
5875 +       }
5876 +
5877 +       dput(parent);
5878 +       if (h_file)
5879 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5880 +
5881 +out:
5882 +       return err;
5883 +}
5884 +
5885 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5886 +{
5887 +       aufs_bindex_t bsrc, bbot;
5888 +       struct dentry *dentry, *h_dentry;
5889 +
5890 +       if (cpg->bsrc < 0) {
5891 +               dentry = cpg->dentry;
5892 +               bbot = au_dbbot(dentry);
5893 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5894 +                       h_dentry = au_h_dptr(dentry, bsrc);
5895 +                       if (h_dentry) {
5896 +                               AuDebugOn(d_is_negative(h_dentry));
5897 +                               break;
5898 +                       }
5899 +               }
5900 +               AuDebugOn(bsrc > bbot);
5901 +               cpg->bsrc = bsrc;
5902 +       }
5903 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5904 +       return au_do_sio_cpup_simple(cpg);
5905 +}
5906 +
5907 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5908 +{
5909 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5910 +       return au_do_sio_cpup_simple(cpg);
5911 +}
5912 +
5913 +/* ---------------------------------------------------------------------- */
5914 +
5915 +/*
5916 + * copyup the deleted file for writing.
5917 + */
5918 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5919 +                        struct file *file)
5920 +{
5921 +       int err;
5922 +       unsigned int flags_orig;
5923 +       aufs_bindex_t bsrc_orig;
5924 +       struct au_dinfo *dinfo;
5925 +       struct {
5926 +               struct au_hdentry *hd;
5927 +               struct dentry *h_dentry;
5928 +       } hdst, hsrc;
5929 +
5930 +       dinfo = au_di(cpg->dentry);
5931 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5932 +
5933 +       bsrc_orig = cpg->bsrc;
5934 +       cpg->bsrc = dinfo->di_btop;
5935 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5936 +       hdst.h_dentry = hdst.hd->hd_dentry;
5937 +       hdst.hd->hd_dentry = wh_dentry;
5938 +       dinfo->di_btop = cpg->bdst;
5939 +
5940 +       hsrc.h_dentry = NULL;
5941 +       if (file) {
5942 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5943 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5944 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5945 +       }
5946 +       flags_orig = cpg->flags;
5947 +       cpg->flags = !AuCpup_DTIME;
5948 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5949 +       cpg->flags = flags_orig;
5950 +       if (file) {
5951 +               if (!err)
5952 +                       err = au_reopen_nondir(file);
5953 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5954 +       }
5955 +       hdst.hd->hd_dentry = hdst.h_dentry;
5956 +       dinfo->di_btop = cpg->bsrc;
5957 +       cpg->bsrc = bsrc_orig;
5958 +
5959 +       return err;
5960 +}
5961 +
5962 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5963 +{
5964 +       int err;
5965 +       aufs_bindex_t bdst;
5966 +       struct au_dtime dt;
5967 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5968 +       struct au_branch *br;
5969 +       struct path h_path;
5970 +
5971 +       dentry = cpg->dentry;
5972 +       bdst = cpg->bdst;
5973 +       br = au_sbr(dentry->d_sb, bdst);
5974 +       parent = dget_parent(dentry);
5975 +       h_parent = au_h_dptr(parent, bdst);
5976 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5977 +       err = PTR_ERR(wh_dentry);
5978 +       if (IS_ERR(wh_dentry))
5979 +               goto out;
5980 +
5981 +       h_path.dentry = h_parent;
5982 +       h_path.mnt = au_br_mnt(br);
5983 +       au_dtime_store(&dt, parent, &h_path);
5984 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5985 +       if (unlikely(err))
5986 +               goto out_wh;
5987 +
5988 +       dget(wh_dentry);
5989 +       h_path.dentry = wh_dentry;
5990 +       if (!d_is_dir(wh_dentry)) {
5991 +               /* no delegation since it is just created */
5992 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5993 +                                  /*delegated*/NULL, /*force*/0);
5994 +       } else
5995 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5996 +       if (unlikely(err)) {
5997 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5998 +                       wh_dentry, err);
5999 +               err = -EIO;
6000 +       }
6001 +       au_dtime_revert(&dt);
6002 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6003 +
6004 +out_wh:
6005 +       dput(wh_dentry);
6006 +out:
6007 +       dput(parent);
6008 +       return err;
6009 +}
6010 +
6011 +struct au_cpup_wh_args {
6012 +       int *errp;
6013 +       struct au_cp_generic *cpg;
6014 +       struct file *file;
6015 +};
6016 +
6017 +static void au_call_cpup_wh(void *args)
6018 +{
6019 +       struct au_cpup_wh_args *a = args;
6020 +
6021 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6022 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6023 +       au_pin_hdir_release(a->cpg->pin);
6024 +}
6025 +
6026 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6027 +{
6028 +       int err, wkq_err;
6029 +       aufs_bindex_t bdst;
6030 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6031 +       struct inode *dir, *h_dir, *h_tmpdir;
6032 +       struct au_wbr *wbr;
6033 +       struct au_pin wh_pin, *pin_orig;
6034 +       struct mnt_idmap *h_idmap;
6035 +
6036 +       dentry = cpg->dentry;
6037 +       bdst = cpg->bdst;
6038 +       parent = dget_parent(dentry);
6039 +       dir = d_inode(parent);
6040 +       h_orph = NULL;
6041 +       h_parent = NULL;
6042 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6043 +       h_tmpdir = h_dir;
6044 +       pin_orig = NULL;
6045 +       if (!h_dir->i_nlink) {
6046 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6047 +               h_orph = wbr->wbr_orph;
6048 +
6049 +               h_parent = dget(au_h_dptr(parent, bdst));
6050 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6051 +               h_tmpdir = d_inode(h_orph);
6052 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6053 +
6054 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6055 +               /* todo: au_h_open_pre()? */
6056 +
6057 +               pin_orig = cpg->pin;
6058 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6059 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6060 +               cpg->pin = &wh_pin;
6061 +       }
6062 +
6063 +       h_idmap = au_sbr_idmap(dentry->d_sb, bdst);
6064 +       if (!au_test_h_perm_sio(h_idmap, h_tmpdir, MAY_EXEC | MAY_WRITE)
6065 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6066 +               err = au_cpup_wh(cpg, file);
6067 +       else {
6068 +               struct au_cpup_wh_args args = {
6069 +                       .errp   = &err,
6070 +                       .cpg    = cpg,
6071 +                       .file   = file
6072 +               };
6073 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6074 +               if (unlikely(wkq_err))
6075 +                       err = wkq_err;
6076 +       }
6077 +
6078 +       if (h_orph) {
6079 +               inode_unlock(h_tmpdir);
6080 +               /* todo: au_h_open_post()? */
6081 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6082 +               au_set_h_dptr(parent, bdst, h_parent);
6083 +               AuDebugOn(!pin_orig);
6084 +               cpg->pin = pin_orig;
6085 +       }
6086 +       iput(h_dir);
6087 +       dput(parent);
6088 +
6089 +       return err;
6090 +}
6091 +
6092 +/* ---------------------------------------------------------------------- */
6093 +
6094 +/*
6095 + * generic routine for both of copy-up and copy-down.
6096 + */
6097 +/* cf. revalidate function in file.c */
6098 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6099 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6100 +                        struct au_pin *pin,
6101 +                        struct dentry *h_parent, void *arg),
6102 +              void *arg)
6103 +{
6104 +       int err;
6105 +       struct au_pin pin;
6106 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6107 +
6108 +       err = 0;
6109 +       parent = dget_parent(dentry);
6110 +       if (IS_ROOT(parent))
6111 +               goto out;
6112 +
6113 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6114 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6115 +
6116 +       /* do not use au_dpage */
6117 +       real_parent = parent;
6118 +       while (1) {
6119 +               dput(parent);
6120 +               parent = dget_parent(dentry);
6121 +               h_parent = au_h_dptr(parent, bdst);
6122 +               if (h_parent)
6123 +                       goto out; /* success */
6124 +
6125 +               /* find top dir which is necessary to cpup */
6126 +               do {
6127 +                       d = parent;
6128 +                       dput(parent);
6129 +                       parent = dget_parent(d);
6130 +                       di_read_lock_parent3(parent, !AuLock_IR);
6131 +                       h_parent = au_h_dptr(parent, bdst);
6132 +                       di_read_unlock(parent, !AuLock_IR);
6133 +               } while (!h_parent);
6134 +
6135 +               if (d != real_parent)
6136 +                       di_write_lock_child3(d);
6137 +
6138 +               /* somebody else might create while we were sleeping */
6139 +               h_dentry = au_h_dptr(d, bdst);
6140 +               if (!h_dentry || d_is_negative(h_dentry)) {
6141 +                       if (h_dentry)
6142 +                               au_update_dbtop(d);
6143 +
6144 +                       au_pin_set_dentry(&pin, d);
6145 +                       err = au_do_pin(&pin);
6146 +                       if (!err) {
6147 +                               err = cp(d, bdst, &pin, h_parent, arg);
6148 +                               au_unpin(&pin);
6149 +                       }
6150 +               }
6151 +
6152 +               if (d != real_parent)
6153 +                       di_write_unlock(d);
6154 +               if (unlikely(err))
6155 +                       break;
6156 +       }
6157 +
6158 +out:
6159 +       dput(parent);
6160 +       return err;
6161 +}
6162 +
6163 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6164 +                      struct au_pin *pin,
6165 +                      struct dentry *h_parent __maybe_unused,
6166 +                      void *arg __maybe_unused)
6167 +{
6168 +       struct au_cp_generic cpg = {
6169 +               .dentry = dentry,
6170 +               .bdst   = bdst,
6171 +               .bsrc   = -1,
6172 +               .len    = 0,
6173 +               .pin    = pin,
6174 +               .flags  = AuCpup_DTIME
6175 +       };
6176 +       return au_sio_cpup_simple(&cpg);
6177 +}
6178 +
6179 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6180 +{
6181 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6182 +}
6183 +
6184 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6185 +{
6186 +       int err;
6187 +       struct dentry *parent;
6188 +       struct inode *dir;
6189 +
6190 +       parent = dget_parent(dentry);
6191 +       dir = d_inode(parent);
6192 +       err = 0;
6193 +       if (au_h_iptr(dir, bdst))
6194 +               goto out;
6195 +
6196 +       di_read_unlock(parent, AuLock_IR);
6197 +       di_write_lock_parent(parent);
6198 +       /* someone else might change our inode while we were sleeping */
6199 +       if (!au_h_iptr(dir, bdst))
6200 +               err = au_cpup_dirs(dentry, bdst);
6201 +       di_downgrade_lock(parent, AuLock_IR);
6202 +
6203 +out:
6204 +       dput(parent);
6205 +       return err;
6206 +}
6207 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6208 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6209 +++ linux/fs/aufs/cpup.h        2022-11-05 23:02:18.962555950 +0100
6210 @@ -0,0 +1,100 @@
6211 +/* SPDX-License-Identifier: GPL-2.0 */
6212 +/*
6213 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6214 + *
6215 + * This program is free software; you can redistribute it and/or modify
6216 + * it under the terms of the GNU General Public License as published by
6217 + * the Free Software Foundation; either version 2 of the License, or
6218 + * (at your option) any later version.
6219 + *
6220 + * This program is distributed in the hope that it will be useful,
6221 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6222 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6223 + * GNU General Public License for more details.
6224 + *
6225 + * You should have received a copy of the GNU General Public License
6226 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6227 + */
6228 +
6229 +/*
6230 + * copy-up/down functions
6231 + */
6232 +
6233 +#ifndef __AUFS_CPUP_H__
6234 +#define __AUFS_CPUP_H__
6235 +
6236 +#ifdef __KERNEL__
6237 +
6238 +#include <linux/path.h>
6239 +
6240 +struct inode;
6241 +struct file;
6242 +struct au_pin;
6243 +
6244 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6245 +void au_cpup_attr_timesizes(struct inode *inode);
6246 +void au_cpup_attr_nlink(struct inode *inode, int force);
6247 +void au_cpup_attr_changeable(struct inode *inode);
6248 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6249 +void au_cpup_attr_all(struct inode *inode, int force);
6250 +
6251 +/* ---------------------------------------------------------------------- */
6252 +
6253 +struct au_cp_generic {
6254 +       struct dentry   *dentry;
6255 +       aufs_bindex_t   bdst, bsrc;
6256 +       loff_t          len;
6257 +       struct au_pin   *pin;
6258 +       unsigned int    flags;
6259 +};
6260 +
6261 +/* cpup flags */
6262 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6263 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6264 +                                                  for link(2) */
6265 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6266 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6267 +                                                  cpup */
6268 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6269 +                                                  existing entry */
6270 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6271 +                                                  the branch is marked as RO */
6272 +
6273 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6274 +#undef AuCpup_HOPEN
6275 +#define AuCpup_HOPEN           0
6276 +#endif
6277 +
6278 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6279 +#define au_fset_cpup(flags, name) \
6280 +       do { (flags) |= AuCpup_##name; } while (0)
6281 +#define au_fclr_cpup(flags, name) \
6282 +       do { (flags) &= ~AuCpup_##name; } while (0)
6283 +
6284 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6285 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6286 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6287 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6288 +
6289 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6290 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6291 +                        struct au_pin *pin,
6292 +                        struct dentry *h_parent, void *arg),
6293 +              void *arg);
6294 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6295 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6296 +
6297 +/* ---------------------------------------------------------------------- */
6298 +
6299 +/* keep timestamps when copyup */
6300 +struct au_dtime {
6301 +       struct dentry *dt_dentry;
6302 +       struct path dt_h_path;
6303 +       struct timespec64 dt_atime, dt_mtime;
6304 +};
6305 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6306 +                   struct path *h_path);
6307 +void au_dtime_revert(struct au_dtime *dt);
6308 +
6309 +#endif /* __KERNEL__ */
6310 +#endif /* __AUFS_CPUP_H__ */
6311 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6312 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6313 +++ linux/fs/aufs/dbgaufs.c     2023-10-10 22:51:18.033248030 +0200
6314 @@ -0,0 +1,526 @@
6315 +// SPDX-License-Identifier: GPL-2.0
6316 +/*
6317 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6318 + *
6319 + * This program is free software; you can redistribute it and/or modify
6320 + * it under the terms of the GNU General Public License as published by
6321 + * the Free Software Foundation; either version 2 of the License, or
6322 + * (at your option) any later version.
6323 + *
6324 + * This program is distributed in the hope that it will be useful,
6325 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6326 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6327 + * GNU General Public License for more details.
6328 + *
6329 + * You should have received a copy of the GNU General Public License
6330 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6331 + */
6332 +
6333 +/*
6334 + * debugfs interface
6335 + */
6336 +
6337 +#include <linux/debugfs.h>
6338 +#include "aufs.h"
6339 +
6340 +#ifndef CONFIG_SYSFS
6341 +#error DEBUG_FS depends upon SYSFS
6342 +#endif
6343 +
6344 +static struct dentry *dbgaufs;
6345 +static const mode_t dbgaufs_mode = 0444;
6346 +
6347 +/* 20 is max digits length of ulong 64 */
6348 +struct dbgaufs_arg {
6349 +       int n;
6350 +       char a[20 * 4];
6351 +};
6352 +
6353 +/*
6354 + * common function for all XINO files
6355 + */
6356 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6357 +                             struct file *file)
6358 +{
6359 +       void *p;
6360 +
6361 +       p = file->private_data;
6362 +       if (p) {
6363 +               /* this is struct dbgaufs_arg */
6364 +               AuDebugOn(!au_kfree_sz_test(p));
6365 +               au_kfree_do_rcu(p);
6366 +       }
6367 +       return 0;
6368 +}
6369 +
6370 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6371 +                          int cnt)
6372 +{
6373 +       int err;
6374 +       struct kstat st;
6375 +       struct dbgaufs_arg *p;
6376 +
6377 +       err = -ENOMEM;
6378 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6379 +       if (unlikely(!p))
6380 +               goto out;
6381 +
6382 +       err = 0;
6383 +       p->n = 0;
6384 +       file->private_data = p;
6385 +       if (!xf)
6386 +               goto out;
6387 +
6388 +       err = vfsub_getattr(&xf->f_path, &st);
6389 +       if (!err) {
6390 +               if (do_fcnt)
6391 +                       p->n = snprintf
6392 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6393 +                                cnt, st.blocks, st.blksize,
6394 +                                (long long)st.size);
6395 +               else
6396 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6397 +                                       st.blocks, st.blksize,
6398 +                                       (long long)st.size);
6399 +               AuDebugOn(p->n >= sizeof(p->a));
6400 +       } else {
6401 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6402 +               err = 0;
6403 +       }
6404 +
6405 +out:
6406 +       return err;
6407 +}
6408 +
6409 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6410 +                              size_t count, loff_t *ppos)
6411 +{
6412 +       struct dbgaufs_arg *p;
6413 +
6414 +       p = file->private_data;
6415 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6416 +}
6417 +
6418 +/* ---------------------------------------------------------------------- */
6419 +
6420 +struct dbgaufs_plink_arg {
6421 +       int n;
6422 +       char a[];
6423 +};
6424 +
6425 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6426 +                                struct file *file)
6427 +{
6428 +       free_page((unsigned long)file->private_data);
6429 +       return 0;
6430 +}
6431 +
6432 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6433 +{
6434 +       int err, i, limit;
6435 +       unsigned long n, sum;
6436 +       struct dbgaufs_plink_arg *p;
6437 +       struct au_sbinfo *sbinfo;
6438 +       struct super_block *sb;
6439 +       struct hlist_bl_head *hbl;
6440 +
6441 +       err = -ENOMEM;
6442 +       p = (void *)get_zeroed_page(GFP_NOFS);
6443 +       if (unlikely(!p))
6444 +               goto out;
6445 +
6446 +       err = -EFBIG;
6447 +       sbinfo = inode->i_private;
6448 +       sb = sbinfo->si_sb;
6449 +       si_noflush_read_lock(sb);
6450 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6451 +               limit = PAGE_SIZE - sizeof(p->n);
6452 +
6453 +               /* the number of buckets */
6454 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6455 +               p->n += n;
6456 +               limit -= n;
6457 +
6458 +               sum = 0;
6459 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6460 +                    i++, hbl++) {
6461 +                       n = au_hbl_count(hbl);
6462 +                       sum += n;
6463 +
6464 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6465 +                       p->n += n;
6466 +                       limit -= n;
6467 +                       if (unlikely(limit <= 0))
6468 +                               goto out_free;
6469 +               }
6470 +               p->a[p->n - 1] = '\n';
6471 +
6472 +               /* the sum of plinks */
6473 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6474 +               p->n += n;
6475 +               limit -= n;
6476 +               if (unlikely(limit <= 0))
6477 +                       goto out_free;
6478 +       } else {
6479 +#define str "1\n0\n0\n"
6480 +               p->n = sizeof(str) - 1;
6481 +               strscpy(p->a, str, sizeof(str));
6482 +#undef str
6483 +       }
6484 +       si_read_unlock(sb);
6485 +
6486 +       err = 0;
6487 +       file->private_data = p;
6488 +       goto out; /* success */
6489 +
6490 +out_free:
6491 +       free_page((unsigned long)p);
6492 +out:
6493 +       return err;
6494 +}
6495 +
6496 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6497 +                                 size_t count, loff_t *ppos)
6498 +{
6499 +       struct dbgaufs_plink_arg *p;
6500 +
6501 +       p = file->private_data;
6502 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6503 +}
6504 +
6505 +static const struct file_operations dbgaufs_plink_fop = {
6506 +       .owner          = THIS_MODULE,
6507 +       .open           = dbgaufs_plink_open,
6508 +       .release        = dbgaufs_plink_release,
6509 +       .read           = dbgaufs_plink_read
6510 +};
6511 +
6512 +/* ---------------------------------------------------------------------- */
6513 +
6514 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6515 +{
6516 +       int err;
6517 +       struct au_sbinfo *sbinfo;
6518 +       struct super_block *sb;
6519 +
6520 +       sbinfo = inode->i_private;
6521 +       sb = sbinfo->si_sb;
6522 +       si_noflush_read_lock(sb);
6523 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6524 +       si_read_unlock(sb);
6525 +       return err;
6526 +}
6527 +
6528 +static const struct file_operations dbgaufs_xib_fop = {
6529 +       .owner          = THIS_MODULE,
6530 +       .open           = dbgaufs_xib_open,
6531 +       .release        = dbgaufs_xi_release,
6532 +       .read           = dbgaufs_xi_read
6533 +};
6534 +
6535 +/* ---------------------------------------------------------------------- */
6536 +
6537 +#define DbgaufsXi_PREFIX "xi"
6538 +
6539 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6540 +{
6541 +       int err, idx;
6542 +       long l;
6543 +       aufs_bindex_t bindex;
6544 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6545 +       struct au_sbinfo *sbinfo;
6546 +       struct super_block *sb;
6547 +       struct au_xino *xi;
6548 +       struct file *xf;
6549 +       struct qstr *name;
6550 +       struct au_branch *br;
6551 +
6552 +       err = -ENOENT;
6553 +       name = &file->f_path.dentry->d_name;
6554 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6555 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6556 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6557 +               goto out;
6558 +
6559 +       AuDebugOn(name->len >= sizeof(a));
6560 +       memcpy(a, name->name, name->len);
6561 +       a[name->len] = '\0';
6562 +       p = strchr(a, '-');
6563 +       if (p)
6564 +               *p = '\0';
6565 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6566 +       if (unlikely(err))
6567 +               goto out;
6568 +       bindex = l;
6569 +       idx = 0;
6570 +       if (p) {
6571 +               err = kstrtol(p + 1, 10, &l);
6572 +               if (unlikely(err))
6573 +                       goto out;
6574 +               idx = l;
6575 +       }
6576 +
6577 +       err = -ENOENT;
6578 +       sbinfo = inode->i_private;
6579 +       sb = sbinfo->si_sb;
6580 +       si_noflush_read_lock(sb);
6581 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6582 +               goto out_si;
6583 +       br = au_sbr(sb, bindex);
6584 +       xi = br->br_xino;
6585 +       if (unlikely(idx >= xi->xi_nfile))
6586 +               goto out_si;
6587 +       xf = au_xino_file(xi, idx);
6588 +       if (xf)
6589 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6590 +                                     au_xino_count(br));
6591 +
6592 +out_si:
6593 +       si_read_unlock(sb);
6594 +out:
6595 +       AuTraceErr(err);
6596 +       return err;
6597 +}
6598 +
6599 +static const struct file_operations dbgaufs_xino_fop = {
6600 +       .owner          = THIS_MODULE,
6601 +       .open           = dbgaufs_xino_open,
6602 +       .release        = dbgaufs_xi_release,
6603 +       .read           = dbgaufs_xi_read
6604 +};
6605 +
6606 +void dbgaufs_xino_del(struct au_branch *br)
6607 +{
6608 +       struct dentry *dbgaufs;
6609 +
6610 +       dbgaufs = br->br_dbgaufs;
6611 +       if (!dbgaufs)
6612 +               return;
6613 +
6614 +       br->br_dbgaufs = NULL;
6615 +       /* debugfs acquires the parent i_mutex */
6616 +       lockdep_off();
6617 +       debugfs_remove(dbgaufs);
6618 +       lockdep_on();
6619 +}
6620 +
6621 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6622 +{
6623 +       aufs_bindex_t bbot;
6624 +       struct au_branch *br;
6625 +
6626 +       if (!au_sbi(sb)->si_dbgaufs)
6627 +               return;
6628 +
6629 +       bbot = au_sbbot(sb);
6630 +       for (; bindex <= bbot; bindex++) {
6631 +               br = au_sbr(sb, bindex);
6632 +               dbgaufs_xino_del(br);
6633 +       }
6634 +}
6635 +
6636 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6637 +                             unsigned int idx, struct dentry *parent,
6638 +                             struct au_sbinfo *sbinfo)
6639 +{
6640 +       struct au_branch *br;
6641 +       struct dentry *d;
6642 +       /* "xi" bindex(5) "-" idx(2) NULL */
6643 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6644 +
6645 +       if (!idx)
6646 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6647 +       else
6648 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6649 +                        bindex, idx);
6650 +       br = au_sbr(sb, bindex);
6651 +       if (br->br_dbgaufs) {
6652 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6653 +
6654 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6655 +                       /* debugfs acquires the parent i_mutex */
6656 +                       lockdep_off();
6657 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6658 +                                          name);
6659 +                       lockdep_on();
6660 +                       if (unlikely(!d))
6661 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6662 +                                       parent, name);
6663 +               }
6664 +       } else {
6665 +               lockdep_off();
6666 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6667 +                                                    sbinfo, &dbgaufs_xino_fop);
6668 +               lockdep_on();
6669 +               if (unlikely(!br->br_dbgaufs))
6670 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6671 +                               parent, name);
6672 +       }
6673 +}
6674 +
6675 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6676 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6677 +{
6678 +       struct au_branch *br;
6679 +       struct au_xino *xi;
6680 +       unsigned int u;
6681 +
6682 +       br = au_sbr(sb, bindex);
6683 +       xi = br->br_xino;
6684 +       for (u = 0; u < xi->xi_nfile; u++)
6685 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6686 +}
6687 +
6688 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6689 +{
6690 +       struct au_sbinfo *sbinfo;
6691 +       struct dentry *parent;
6692 +       aufs_bindex_t bbot;
6693 +
6694 +       if (!au_opt_test(au_mntflags(sb), XINO))
6695 +               return;
6696 +
6697 +       sbinfo = au_sbi(sb);
6698 +       parent = sbinfo->si_dbgaufs;
6699 +       if (!parent)
6700 +               return;
6701 +
6702 +       bbot = au_sbbot(sb);
6703 +       if (topdown)
6704 +               for (; bindex <= bbot; bindex++)
6705 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6706 +       else
6707 +               for (; bbot >= bindex; bbot--)
6708 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6709 +}
6710 +
6711 +/* ---------------------------------------------------------------------- */
6712 +
6713 +#ifdef CONFIG_AUFS_EXPORT
6714 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6715 +{
6716 +       int err;
6717 +       struct au_sbinfo *sbinfo;
6718 +       struct super_block *sb;
6719 +
6720 +       sbinfo = inode->i_private;
6721 +       sb = sbinfo->si_sb;
6722 +       si_noflush_read_lock(sb);
6723 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6724 +       si_read_unlock(sb);
6725 +       return err;
6726 +}
6727 +
6728 +static const struct file_operations dbgaufs_xigen_fop = {
6729 +       .owner          = THIS_MODULE,
6730 +       .open           = dbgaufs_xigen_open,
6731 +       .release        = dbgaufs_xi_release,
6732 +       .read           = dbgaufs_xi_read
6733 +};
6734 +
6735 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6736 +{
6737 +       int err;
6738 +
6739 +       /*
6740 +        * This function is a dynamic '__init' function actually,
6741 +        * so the tiny check for si_rwsem is unnecessary.
6742 +        */
6743 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6744 +
6745 +       err = -EIO;
6746 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6747 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6748 +                &dbgaufs_xigen_fop);
6749 +       if (sbinfo->si_dbgaufs_xigen)
6750 +               err = 0;
6751 +
6752 +       return err;
6753 +}
6754 +#else
6755 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6756 +{
6757 +       return 0;
6758 +}
6759 +#endif /* CONFIG_AUFS_EXPORT */
6760 +
6761 +/* ---------------------------------------------------------------------- */
6762 +
6763 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6764 +{
6765 +       /*
6766 +        * This function is a dynamic '__fin' function actually,
6767 +        * so the tiny check for si_rwsem is unnecessary.
6768 +        */
6769 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6770 +
6771 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6772 +       sbinfo->si_dbgaufs = NULL;
6773 +}
6774 +
6775 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6776 +{
6777 +       int err;
6778 +       char name[SysaufsSiNameLen];
6779 +
6780 +       /*
6781 +        * This function is a dynamic '__init' function actually,
6782 +        * so the tiny check for si_rwsem is unnecessary.
6783 +        */
6784 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6785 +
6786 +       err = -ENOENT;
6787 +       if (!dbgaufs) {
6788 +               AuErr1("/debug/aufs is uninitialized\n");
6789 +               goto out;
6790 +       }
6791 +
6792 +       err = -EIO;
6793 +       sysaufs_name(sbinfo, name);
6794 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6795 +       if (unlikely(!sbinfo->si_dbgaufs))
6796 +               goto out;
6797 +
6798 +       /* regardless plink/noplink option */
6799 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6800 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6801 +                &dbgaufs_plink_fop);
6802 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6803 +               goto out_dir;
6804 +
6805 +       /* regardless xino/noxino option */
6806 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6807 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6808 +                &dbgaufs_xib_fop);
6809 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6810 +               goto out_dir;
6811 +
6812 +       err = dbgaufs_xigen_init(sbinfo);
6813 +       if (!err)
6814 +               goto out; /* success */
6815 +
6816 +out_dir:
6817 +       dbgaufs_si_fin(sbinfo);
6818 +out:
6819 +       if (unlikely(err))
6820 +               pr_err("debugfs/aufs failed\n");
6821 +       return err;
6822 +}
6823 +
6824 +/* ---------------------------------------------------------------------- */
6825 +
6826 +void dbgaufs_fin(void)
6827 +{
6828 +       debugfs_remove(dbgaufs);
6829 +}
6830 +
6831 +int __init dbgaufs_init(void)
6832 +{
6833 +       int err;
6834 +
6835 +       err = -EIO;
6836 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6837 +       if (dbgaufs)
6838 +               err = 0;
6839 +       return err;
6840 +}
6841 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6842 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6843 +++ linux/fs/aufs/dbgaufs.h     2022-11-05 23:02:18.962555950 +0100
6844 @@ -0,0 +1,53 @@
6845 +/* SPDX-License-Identifier: GPL-2.0 */
6846 +/*
6847 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6848 + *
6849 + * This program is free software; you can redistribute it and/or modify
6850 + * it under the terms of the GNU General Public License as published by
6851 + * the Free Software Foundation; either version 2 of the License, or
6852 + * (at your option) any later version.
6853 + *
6854 + * This program is distributed in the hope that it will be useful,
6855 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6856 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6857 + * GNU General Public License for more details.
6858 + *
6859 + * You should have received a copy of the GNU General Public License
6860 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6861 + */
6862 +
6863 +/*
6864 + * debugfs interface
6865 + */
6866 +
6867 +#ifndef __DBGAUFS_H__
6868 +#define __DBGAUFS_H__
6869 +
6870 +#ifdef __KERNEL__
6871 +
6872 +struct super_block;
6873 +struct au_sbinfo;
6874 +struct au_branch;
6875 +
6876 +#ifdef CONFIG_DEBUG_FS
6877 +/* dbgaufs.c */
6878 +void dbgaufs_xino_del(struct au_branch *br);
6879 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6880 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6881 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6882 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6883 +void dbgaufs_fin(void);
6884 +int __init dbgaufs_init(void);
6885 +#else
6886 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6887 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6888 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6889 +          int topdown)
6890 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6891 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6892 +AuStubVoid(dbgaufs_fin, void)
6893 +AuStubInt0(__init dbgaufs_init, void)
6894 +#endif /* CONFIG_DEBUG_FS */
6895 +
6896 +#endif /* __KERNEL__ */
6897 +#endif /* __DBGAUFS_H__ */
6898 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6899 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6900 +++ linux/fs/aufs/dcsub.c       2022-11-05 23:02:18.962555950 +0100
6901 @@ -0,0 +1,225 @@
6902 +// SPDX-License-Identifier: GPL-2.0
6903 +/*
6904 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6905 + *
6906 + * This program is free software; you can redistribute it and/or modify
6907 + * it under the terms of the GNU General Public License as published by
6908 + * the Free Software Foundation; either version 2 of the License, or
6909 + * (at your option) any later version.
6910 + *
6911 + * This program is distributed in the hope that it will be useful,
6912 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6913 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6914 + * GNU General Public License for more details.
6915 + *
6916 + * You should have received a copy of the GNU General Public License
6917 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6918 + */
6919 +
6920 +/*
6921 + * sub-routines for dentry cache
6922 + */
6923 +
6924 +#include "aufs.h"
6925 +
6926 +static void au_dpage_free(struct au_dpage *dpage)
6927 +{
6928 +       int i;
6929 +       struct dentry **p;
6930 +
6931 +       p = dpage->dentries;
6932 +       for (i = 0; i < dpage->ndentry; i++)
6933 +               dput(*p++);
6934 +       free_page((unsigned long)dpage->dentries);
6935 +}
6936 +
6937 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6938 +{
6939 +       int err;
6940 +       void *p;
6941 +
6942 +       err = -ENOMEM;
6943 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6944 +       if (unlikely(!dpages->dpages))
6945 +               goto out;
6946 +
6947 +       p = (void *)__get_free_page(gfp);
6948 +       if (unlikely(!p))
6949 +               goto out_dpages;
6950 +
6951 +       dpages->dpages[0].ndentry = 0;
6952 +       dpages->dpages[0].dentries = p;
6953 +       dpages->ndpage = 1;
6954 +       return 0; /* success */
6955 +
6956 +out_dpages:
6957 +       au_kfree_try_rcu(dpages->dpages);
6958 +out:
6959 +       return err;
6960 +}
6961 +
6962 +void au_dpages_free(struct au_dcsub_pages *dpages)
6963 +{
6964 +       int i;
6965 +       struct au_dpage *p;
6966 +
6967 +       p = dpages->dpages;
6968 +       for (i = 0; i < dpages->ndpage; i++)
6969 +               au_dpage_free(p++);
6970 +       au_kfree_try_rcu(dpages->dpages);
6971 +}
6972 +
6973 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6974 +                           struct dentry *dentry, gfp_t gfp)
6975 +{
6976 +       int err, sz;
6977 +       struct au_dpage *dpage;
6978 +       void *p;
6979 +
6980 +       dpage = dpages->dpages + dpages->ndpage - 1;
6981 +       sz = PAGE_SIZE / sizeof(dentry);
6982 +       if (unlikely(dpage->ndentry >= sz)) {
6983 +               AuLabel(new dpage);
6984 +               err = -ENOMEM;
6985 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6986 +               p = au_kzrealloc(dpages->dpages, sz,
6987 +                                sz + sizeof(*dpages->dpages), gfp,
6988 +                                /*may_shrink*/0);
6989 +               if (unlikely(!p))
6990 +                       goto out;
6991 +
6992 +               dpages->dpages = p;
6993 +               dpage = dpages->dpages + dpages->ndpage;
6994 +               p = (void *)__get_free_page(gfp);
6995 +               if (unlikely(!p))
6996 +                       goto out;
6997 +
6998 +               dpage->ndentry = 0;
6999 +               dpage->dentries = p;
7000 +               dpages->ndpage++;
7001 +       }
7002 +
7003 +       AuDebugOn(au_dcount(dentry) <= 0);
7004 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7005 +       return 0; /* success */
7006 +
7007 +out:
7008 +       return err;
7009 +}
7010 +
7011 +/* todo: BAD approach */
7012 +/* copied from linux/fs/dcache.c */
7013 +enum d_walk_ret {
7014 +       D_WALK_CONTINUE,
7015 +       D_WALK_QUIT,
7016 +       D_WALK_NORETRY,
7017 +       D_WALK_SKIP,
7018 +};
7019 +
7020 +extern void d_walk(struct dentry *parent, void *data,
7021 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7022 +
7023 +struct ac_dpages_arg {
7024 +       int err;
7025 +       struct au_dcsub_pages *dpages;
7026 +       struct super_block *sb;
7027 +       au_dpages_test test;
7028 +       void *arg;
7029 +};
7030 +
7031 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7032 +{
7033 +       enum d_walk_ret ret;
7034 +       struct ac_dpages_arg *arg = _arg;
7035 +
7036 +       ret = D_WALK_CONTINUE;
7037 +       if (dentry->d_sb == arg->sb
7038 +           && !IS_ROOT(dentry)
7039 +           && au_dcount(dentry) > 0
7040 +           && au_di(dentry)
7041 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7042 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7043 +               if (unlikely(arg->err))
7044 +                       ret = D_WALK_QUIT;
7045 +       }
7046 +
7047 +       return ret;
7048 +}
7049 +
7050 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7051 +                  au_dpages_test test, void *arg)
7052 +{
7053 +       struct ac_dpages_arg args = {
7054 +               .err    = 0,
7055 +               .dpages = dpages,
7056 +               .sb     = root->d_sb,
7057 +               .test   = test,
7058 +               .arg    = arg
7059 +       };
7060 +
7061 +       d_walk(root, &args, au_call_dpages_append);
7062 +
7063 +       return args.err;
7064 +}
7065 +
7066 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7067 +                      int do_include, au_dpages_test test, void *arg)
7068 +{
7069 +       int err;
7070 +
7071 +       err = 0;
7072 +       write_seqlock(&rename_lock);
7073 +       spin_lock(&dentry->d_lock);
7074 +       if (do_include
7075 +           && au_dcount(dentry) > 0
7076 +           && (!test || test(dentry, arg)))
7077 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7078 +       spin_unlock(&dentry->d_lock);
7079 +       if (unlikely(err))
7080 +               goto out;
7081 +
7082 +       /*
7083 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7084 +        * mount
7085 +        */
7086 +       while (!IS_ROOT(dentry)) {
7087 +               dentry = dentry->d_parent; /* rename_lock is locked */
7088 +               spin_lock(&dentry->d_lock);
7089 +               if (au_dcount(dentry) > 0
7090 +                   && (!test || test(dentry, arg)))
7091 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7092 +               spin_unlock(&dentry->d_lock);
7093 +               if (unlikely(err))
7094 +                       break;
7095 +       }
7096 +
7097 +out:
7098 +       write_sequnlock(&rename_lock);
7099 +       return err;
7100 +}
7101 +
7102 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7103 +{
7104 +       return au_di(dentry) && dentry->d_sb == arg;
7105 +}
7106 +
7107 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7108 +                           struct dentry *dentry, int do_include)
7109 +{
7110 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7111 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7112 +}
7113 +
7114 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7115 +{
7116 +       struct path path[2] = {
7117 +               {
7118 +                       .dentry = d1
7119 +               },
7120 +               {
7121 +                       .dentry = d2
7122 +               }
7123 +       };
7124 +
7125 +       return path_is_under(path + 0, path + 1);
7126 +}
7127 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7128 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7129 +++ linux/fs/aufs/dcsub.h       2022-11-05 23:02:18.962555950 +0100
7130 @@ -0,0 +1,137 @@
7131 +/* SPDX-License-Identifier: GPL-2.0 */
7132 +/*
7133 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7134 + *
7135 + * This program is free software; you can redistribute it and/or modify
7136 + * it under the terms of the GNU General Public License as published by
7137 + * the Free Software Foundation; either version 2 of the License, or
7138 + * (at your option) any later version.
7139 + *
7140 + * This program is distributed in the hope that it will be useful,
7141 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7142 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7143 + * GNU General Public License for more details.
7144 + *
7145 + * You should have received a copy of the GNU General Public License
7146 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7147 + */
7148 +
7149 +/*
7150 + * sub-routines for dentry cache
7151 + */
7152 +
7153 +#ifndef __AUFS_DCSUB_H__
7154 +#define __AUFS_DCSUB_H__
7155 +
7156 +#ifdef __KERNEL__
7157 +
7158 +#include <linux/dcache.h>
7159 +#include <linux/fs.h>
7160 +
7161 +struct au_dpage {
7162 +       int ndentry;
7163 +       struct dentry **dentries;
7164 +};
7165 +
7166 +struct au_dcsub_pages {
7167 +       int ndpage;
7168 +       struct au_dpage *dpages;
7169 +};
7170 +
7171 +/* ---------------------------------------------------------------------- */
7172 +
7173 +/* dcsub.c */
7174 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7175 +void au_dpages_free(struct au_dcsub_pages *dpages);
7176 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7177 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7178 +                  au_dpages_test test, void *arg);
7179 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7180 +                      int do_include, au_dpages_test test, void *arg);
7181 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7182 +                           struct dentry *dentry, int do_include);
7183 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7184 +
7185 +/* ---------------------------------------------------------------------- */
7186 +
7187 +/*
7188 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7189 + * include/linux/dcache.h. Try them (in the future).
7190 + */
7191 +
7192 +static inline int au_d_hashed_positive(struct dentry *d)
7193 +{
7194 +       int err;
7195 +       struct inode *inode = d_inode(d);
7196 +
7197 +       err = 0;
7198 +       if (unlikely(d_unhashed(d)
7199 +                    || d_is_negative(d)
7200 +                    || !inode->i_nlink))
7201 +               err = -ENOENT;
7202 +       return err;
7203 +}
7204 +
7205 +static inline int au_d_linkable(struct dentry *d)
7206 +{
7207 +       int err;
7208 +       struct inode *inode = d_inode(d);
7209 +
7210 +       err = au_d_hashed_positive(d);
7211 +       if (err
7212 +           && d_is_positive(d)
7213 +           && (inode->i_state & I_LINKABLE))
7214 +               err = 0;
7215 +       return err;
7216 +}
7217 +
7218 +static inline int au_d_alive(struct dentry *d)
7219 +{
7220 +       int err;
7221 +       struct inode *inode;
7222 +
7223 +       err = 0;
7224 +       if (!IS_ROOT(d))
7225 +               err = au_d_hashed_positive(d);
7226 +       else {
7227 +               inode = d_inode(d);
7228 +               if (unlikely(d_unlinked(d)
7229 +                            || d_is_negative(d)
7230 +                            || !inode->i_nlink))
7231 +                       err = -ENOENT;
7232 +       }
7233 +       return err;
7234 +}
7235 +
7236 +static inline int au_alive_dir(struct dentry *d)
7237 +{
7238 +       int err;
7239 +
7240 +       err = au_d_alive(d);
7241 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7242 +               err = -ENOENT;
7243 +       return err;
7244 +}
7245 +
7246 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7247 +{
7248 +       return a->len == b->len
7249 +               && !memcmp(a->name, b->name, a->len);
7250 +}
7251 +
7252 +/*
7253 + * by the commit
7254 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7255 + *                     taking d_lock
7256 + * the type of d_lockref.count became int, but the inlined function d_count()
7257 + * still returns unsigned int.
7258 + * I don't know why. Maybe it is for every d_count() users?
7259 + * Anyway au_dcount() lives on.
7260 + */
7261 +static inline int au_dcount(struct dentry *d)
7262 +{
7263 +       return (int)d_count(d);
7264 +}
7265 +
7266 +#endif /* __KERNEL__ */
7267 +#endif /* __AUFS_DCSUB_H__ */
7268 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7269 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7270 +++ linux/fs/aufs/debug.c       2023-10-31 09:31:04.196547417 +0100
7271 @@ -0,0 +1,448 @@
7272 +// SPDX-License-Identifier: GPL-2.0
7273 +/*
7274 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7275 + *
7276 + * This program is free software; you can redistribute it and/or modify
7277 + * it under the terms of the GNU General Public License as published by
7278 + * the Free Software Foundation; either version 2 of the License, or
7279 + * (at your option) any later version.
7280 + *
7281 + * This program is distributed in the hope that it will be useful,
7282 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7283 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7284 + * GNU General Public License for more details.
7285 + *
7286 + * You should have received a copy of the GNU General Public License
7287 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7288 + */
7289 +
7290 +/*
7291 + * debug print functions
7292 + */
7293 +
7294 +#include <linux/iversion.h>
7295 +#include "aufs.h"
7296 +
7297 +/* Returns 0, or -errno.  arg is in kp->arg. */
7298 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7299 +{
7300 +       int err, n;
7301 +
7302 +       err = kstrtoint(val, 0, &n);
7303 +       if (!err) {
7304 +               if (n > 0)
7305 +                       au_debug_on();
7306 +               else
7307 +                       au_debug_off();
7308 +       }
7309 +       return err;
7310 +}
7311 +
7312 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7313 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7314 +{
7315 +       atomic_t *a;
7316 +
7317 +       a = kp->arg;
7318 +       return sprintf(buffer, "%d", atomic_read(a));
7319 +}
7320 +
7321 +static const struct kernel_param_ops param_ops_atomic_t = {
7322 +       .set = param_atomic_t_set,
7323 +       .get = param_atomic_t_get
7324 +       /* void (*free)(void *arg) */
7325 +};
7326 +
7327 +atomic_t aufs_debug = ATOMIC_INIT(0);
7328 +MODULE_PARM_DESC(debug, "debug print");
7329 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7330 +
7331 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7332 +char *au_plevel = KERN_DEBUG;
7333 +#define dpri(fmt, ...) do {                                    \
7334 +       if ((au_plevel                                          \
7335 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7336 +           || au_debug_test())                                 \
7337 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7338 +} while (0)
7339 +
7340 +/* ---------------------------------------------------------------------- */
7341 +
7342 +void au_dpri_whlist(struct au_nhash *whlist)
7343 +{
7344 +       unsigned long ul, n;
7345 +       struct hlist_head *head;
7346 +       struct au_vdir_wh *pos;
7347 +
7348 +       n = whlist->nh_num;
7349 +       head = whlist->nh_head;
7350 +       for (ul = 0; ul < n; ul++) {
7351 +               hlist_for_each_entry(pos, head, wh_hash)
7352 +                       dpri("b%d, %.*s, %d\n",
7353 +                            pos->wh_bindex,
7354 +                            pos->wh_str.len, pos->wh_str.name,
7355 +                            pos->wh_str.len);
7356 +               head++;
7357 +       }
7358 +}
7359 +
7360 +void au_dpri_vdir(struct au_vdir *vdir)
7361 +{
7362 +       unsigned long ul;
7363 +       union au_vdir_deblk_p p;
7364 +       unsigned char *o;
7365 +
7366 +       if (!vdir || IS_ERR(vdir)) {
7367 +               dpri("err %ld\n", PTR_ERR(vdir));
7368 +               return;
7369 +       }
7370 +
7371 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7372 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7373 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7374 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7375 +               p.deblk = vdir->vd_deblk[ul];
7376 +               o = p.deblk;
7377 +               dpri("[%lu]: %p\n", ul, o);
7378 +       }
7379 +}
7380 +
7381 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7382 +                       struct dentry *wh)
7383 +{
7384 +       char *n = NULL;
7385 +       int l = 0;
7386 +       struct timespec64 ctime;
7387 +
7388 +       if (!inode || IS_ERR(inode)) {
7389 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7390 +               return -1;
7391 +       }
7392 +
7393 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7394 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7395 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7396 +       if (wh) {
7397 +               n = (void *)wh->d_name.name;
7398 +               l = wh->d_name.len;
7399 +       }
7400 +
7401 +       ctime = inode_get_ctime(inode);
7402 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7403 +            " acl %p, def_acl %p,"
7404 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7405 +            bindex, inode,
7406 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7407 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7408 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7409 +            inode->i_acl, inode->i_default_acl,
7410 +            hn, (long long)timespec64_to_ns(&ctime) & 0x0ffff,
7411 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7412 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7413 +            inode->i_generation,
7414 +            l ? ", wh " : "", l, n);
7415 +       return 0;
7416 +}
7417 +
7418 +void au_dpri_inode(struct inode *inode)
7419 +{
7420 +       struct au_iinfo *iinfo;
7421 +       struct au_hinode *hi;
7422 +       aufs_bindex_t bindex;
7423 +       int err, hn;
7424 +
7425 +       err = do_pri_inode(-1, inode, -1, NULL);
7426 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7427 +               return;
7428 +
7429 +       iinfo = au_ii(inode);
7430 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7431 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7432 +       if (iinfo->ii_btop < 0)
7433 +               return;
7434 +       hn = 0;
7435 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7436 +               hi = au_hinode(iinfo, bindex);
7437 +               hn = !!au_hn(hi);
7438 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7439 +       }
7440 +}
7441 +
7442 +void au_dpri_dalias(struct inode *inode)
7443 +{
7444 +       struct dentry *d;
7445 +
7446 +       spin_lock(&inode->i_lock);
7447 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7448 +               au_dpri_dentry(d);
7449 +       spin_unlock(&inode->i_lock);
7450 +}
7451 +
7452 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7453 +{
7454 +       struct dentry *wh = NULL;
7455 +       int hn;
7456 +       struct inode *inode;
7457 +       struct au_iinfo *iinfo;
7458 +       struct au_hinode *hi;
7459 +
7460 +       if (!dentry || IS_ERR(dentry)) {
7461 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7462 +               return -1;
7463 +       }
7464 +       /* do not call dget_parent() here */
7465 +       /* note: access d_xxx without d_lock */
7466 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7467 +            bindex, dentry, dentry,
7468 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7469 +            au_dcount(dentry), dentry->d_flags,
7470 +            d_unhashed(dentry) ? "un" : "");
7471 +       hn = -1;
7472 +       inode = NULL;
7473 +       if (d_is_positive(dentry))
7474 +               inode = d_inode(dentry);
7475 +       if (inode
7476 +           && au_test_aufs(dentry->d_sb)
7477 +           && bindex >= 0
7478 +           && !au_is_bad_inode(inode)) {
7479 +               iinfo = au_ii(inode);
7480 +               hi = au_hinode(iinfo, bindex);
7481 +               hn = !!au_hn(hi);
7482 +               wh = hi->hi_whdentry;
7483 +       }
7484 +       do_pri_inode(bindex, inode, hn, wh);
7485 +       return 0;
7486 +}
7487 +
7488 +void au_dpri_dentry(struct dentry *dentry)
7489 +{
7490 +       struct au_dinfo *dinfo;
7491 +       aufs_bindex_t bindex;
7492 +       int err;
7493 +
7494 +       err = do_pri_dentry(-1, dentry);
7495 +       if (err || !au_test_aufs(dentry->d_sb))
7496 +               return;
7497 +
7498 +       dinfo = au_di(dentry);
7499 +       if (!dinfo)
7500 +               return;
7501 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7502 +            dinfo->di_btop, dinfo->di_bbot,
7503 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7504 +            dinfo->di_tmpfile);
7505 +       if (dinfo->di_btop < 0)
7506 +               return;
7507 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7508 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7509 +}
7510 +
7511 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7512 +{
7513 +       char a[32];
7514 +
7515 +       if (!file || IS_ERR(file)) {
7516 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7517 +               return -1;
7518 +       }
7519 +       a[0] = 0;
7520 +       if (bindex < 0
7521 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7522 +           && au_test_aufs(file->f_path.dentry->d_sb)
7523 +           && au_fi(file))
7524 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7525 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7526 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7527 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7528 +            file->f_version, file->f_pos, a);
7529 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7530 +               do_pri_dentry(bindex, file->f_path.dentry);
7531 +       return 0;
7532 +}
7533 +
7534 +void au_dpri_file(struct file *file)
7535 +{
7536 +       struct au_finfo *finfo;
7537 +       struct au_fidir *fidir;
7538 +       struct au_hfile *hfile;
7539 +       aufs_bindex_t bindex;
7540 +       int err;
7541 +
7542 +       err = do_pri_file(-1, file);
7543 +       if (err
7544 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7545 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7546 +               return;
7547 +
7548 +       finfo = au_fi(file);
7549 +       if (!finfo)
7550 +               return;
7551 +       if (finfo->fi_btop < 0)
7552 +               return;
7553 +       fidir = finfo->fi_hdir;
7554 +       if (!fidir)
7555 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7556 +       else
7557 +               for (bindex = finfo->fi_btop;
7558 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7559 +                    bindex++) {
7560 +                       hfile = fidir->fd_hfile + bindex;
7561 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7562 +               }
7563 +}
7564 +
7565 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7566 +{
7567 +       struct vfsmount *mnt;
7568 +       struct super_block *sb;
7569 +
7570 +       if (!br || IS_ERR(br))
7571 +               goto out;
7572 +       mnt = au_br_mnt(br);
7573 +       if (!mnt || IS_ERR(mnt))
7574 +               goto out;
7575 +       sb = mnt->mnt_sb;
7576 +       if (!sb || IS_ERR(sb))
7577 +               goto out;
7578 +
7579 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7580 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7581 +            "xino %d\n",
7582 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7583 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7584 +            sb->s_flags, sb->s_count,
7585 +            atomic_read(&sb->s_active),
7586 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7587 +       return 0;
7588 +
7589 +out:
7590 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7591 +       return -1;
7592 +}
7593 +
7594 +void au_dpri_sb(struct super_block *sb)
7595 +{
7596 +       struct au_sbinfo *sbinfo;
7597 +       aufs_bindex_t bindex;
7598 +       int err;
7599 +       /* to reduce stack size */
7600 +       struct {
7601 +               struct vfsmount mnt;
7602 +               struct au_branch fake;
7603 +       } *a;
7604 +
7605 +       /* this function can be called from magic sysrq */
7606 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7607 +       if (unlikely(!a)) {
7608 +               dpri("no memory\n");
7609 +               return;
7610 +       }
7611 +
7612 +       a->mnt.mnt_sb = sb;
7613 +       a->fake.br_path.mnt = &a->mnt;
7614 +       err = do_pri_br(-1, &a->fake);
7615 +       au_kfree_rcu(a);
7616 +       dpri("dev 0x%x\n", sb->s_dev);
7617 +       if (err || !au_test_aufs(sb))
7618 +               return;
7619 +
7620 +       sbinfo = au_sbi(sb);
7621 +       if (!sbinfo)
7622 +               return;
7623 +       dpri("nw %d, gen %u, kobj %d\n",
7624 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7625 +            kref_read(&sbinfo->si_kobj.kref));
7626 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7627 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7628 +}
7629 +
7630 +/* ---------------------------------------------------------------------- */
7631 +
7632 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7633 +{
7634 +       struct inode *h_inode, *inode = d_inode(dentry);
7635 +       struct dentry *h_dentry;
7636 +       aufs_bindex_t bindex, bbot, bi;
7637 +
7638 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7639 +               return;
7640 +
7641 +       bbot = au_dbbot(dentry);
7642 +       bi = au_ibbot(inode);
7643 +       if (bi < bbot)
7644 +               bbot = bi;
7645 +       bindex = au_dbtop(dentry);
7646 +       bi = au_ibtop(inode);
7647 +       if (bi > bindex)
7648 +               bindex = bi;
7649 +
7650 +       for (; bindex <= bbot; bindex++) {
7651 +               h_dentry = au_h_dptr(dentry, bindex);
7652 +               if (!h_dentry)
7653 +                       continue;
7654 +               h_inode = au_h_iptr(inode, bindex);
7655 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7656 +                       au_debug_on();
7657 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7658 +                       AuDbgDentry(dentry);
7659 +                       AuDbgInode(inode);
7660 +                       au_debug_off();
7661 +                       if (au_test_fuse(h_inode->i_sb))
7662 +                               WARN_ON_ONCE(1);
7663 +                       else
7664 +                               BUG();
7665 +               }
7666 +       }
7667 +}
7668 +
7669 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7670 +{
7671 +       int err, i, j;
7672 +       struct au_dcsub_pages dpages;
7673 +       struct au_dpage *dpage;
7674 +       struct dentry **dentries;
7675 +
7676 +       err = au_dpages_init(&dpages, GFP_NOFS);
7677 +       AuDebugOn(err);
7678 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7679 +       AuDebugOn(err);
7680 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7681 +               dpage = dpages.dpages + i;
7682 +               dentries = dpage->dentries;
7683 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7684 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7685 +       }
7686 +       au_dpages_free(&dpages);
7687 +}
7688 +
7689 +void au_dbg_verify_kthread(void)
7690 +{
7691 +       if (au_wkq_test()) {
7692 +               au_dbg_blocked();
7693 +               /*
7694 +                * It may be recursive, but udba=notify between two aufs mounts,
7695 +                * where a single ro branch is shared, is not a problem.
7696 +                */
7697 +               /* WARN_ON(1); */
7698 +       }
7699 +}
7700 +
7701 +/* ---------------------------------------------------------------------- */
7702 +
7703 +int __init au_debug_init(void)
7704 +{
7705 +       aufs_bindex_t bindex;
7706 +       struct au_vdir_destr destr;
7707 +
7708 +       bindex = -1;
7709 +       AuDebugOn(bindex >= 0);
7710 +
7711 +       destr.len = -1;
7712 +       AuDebugOn(destr.len < NAME_MAX);
7713 +
7714 +#ifdef CONFIG_4KSTACKS
7715 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7716 +#endif
7717 +
7718 +       return 0;
7719 +}
7720 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7721 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7722 +++ linux/fs/aufs/debug.h       2022-11-05 23:02:18.962555950 +0100
7723 @@ -0,0 +1,226 @@
7724 +/* SPDX-License-Identifier: GPL-2.0 */
7725 +/*
7726 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7727 + *
7728 + * This program is free software; you can redistribute it and/or modify
7729 + * it under the terms of the GNU General Public License as published by
7730 + * the Free Software Foundation; either version 2 of the License, or
7731 + * (at your option) any later version.
7732 + *
7733 + * This program is distributed in the hope that it will be useful,
7734 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7735 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7736 + * GNU General Public License for more details.
7737 + *
7738 + * You should have received a copy of the GNU General Public License
7739 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7740 + */
7741 +
7742 +/*
7743 + * debug print functions
7744 + */
7745 +
7746 +#ifndef __AUFS_DEBUG_H__
7747 +#define __AUFS_DEBUG_H__
7748 +
7749 +#ifdef __KERNEL__
7750 +
7751 +#include <linux/atomic.h>
7752 +#include <linux/module.h>
7753 +#include <linux/kallsyms.h>
7754 +#include <linux/sysrq.h>
7755 +
7756 +#ifdef CONFIG_AUFS_DEBUG
7757 +#define AuDebugOn(a)           BUG_ON(a)
7758 +
7759 +/* module parameter */
7760 +extern atomic_t aufs_debug;
7761 +static inline void au_debug_on(void)
7762 +{
7763 +       atomic_inc(&aufs_debug);
7764 +}
7765 +static inline void au_debug_off(void)
7766 +{
7767 +       atomic_dec_if_positive(&aufs_debug);
7768 +}
7769 +
7770 +static inline int au_debug_test(void)
7771 +{
7772 +       return atomic_read(&aufs_debug) > 0;
7773 +}
7774 +#else
7775 +#define AuDebugOn(a)           do {} while (0)
7776 +AuStubVoid(au_debug_on, void)
7777 +AuStubVoid(au_debug_off, void)
7778 +AuStubInt0(au_debug_test, void)
7779 +#endif /* CONFIG_AUFS_DEBUG */
7780 +
7781 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7782 +
7783 +/* ---------------------------------------------------------------------- */
7784 +
7785 +/* debug print */
7786 +
7787 +#define AuDbg(fmt, ...) do { \
7788 +       if (au_debug_test()) \
7789 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7790 +} while (0)
7791 +#define AuLabel(l)             AuDbg(#l "\n")
7792 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7793 +#define AuWarn1(fmt, ...) do { \
7794 +       static unsigned char _c; \
7795 +       if (!_c++) \
7796 +               pr_warn(fmt, ##__VA_ARGS__); \
7797 +} while (0)
7798 +
7799 +#define AuErr1(fmt, ...) do { \
7800 +       static unsigned char _c; \
7801 +       if (!_c++) \
7802 +               pr_err(fmt, ##__VA_ARGS__); \
7803 +} while (0)
7804 +
7805 +#define AuIOErr1(fmt, ...) do { \
7806 +       static unsigned char _c; \
7807 +       if (!_c++) \
7808 +               AuIOErr(fmt, ##__VA_ARGS__); \
7809 +} while (0)
7810 +
7811 +#define AuUnsupportMsg "This operation is not supported." \
7812 +                       " Please report this application to aufs-users ML."
7813 +#define AuUnsupport(fmt, ...) do { \
7814 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7815 +       dump_stack(); \
7816 +} while (0)
7817 +
7818 +#define AuTraceErr(e) do { \
7819 +       if (unlikely((e) < 0)) \
7820 +               AuDbg("err %d\n", (int)(e)); \
7821 +} while (0)
7822 +
7823 +#define AuTraceErrPtr(p) do { \
7824 +       if (IS_ERR(p)) \
7825 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7826 +} while (0)
7827 +
7828 +/* dirty macros for debug print, use with "%.*s" and caution */
7829 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7830 +
7831 +/* ---------------------------------------------------------------------- */
7832 +
7833 +struct dentry;
7834 +#ifdef CONFIG_AUFS_DEBUG
7835 +extern struct mutex au_dbg_mtx;
7836 +extern char *au_plevel;
7837 +struct au_nhash;
7838 +void au_dpri_whlist(struct au_nhash *whlist);
7839 +struct au_vdir;
7840 +void au_dpri_vdir(struct au_vdir *vdir);
7841 +struct inode;
7842 +void au_dpri_inode(struct inode *inode);
7843 +void au_dpri_dalias(struct inode *inode);
7844 +void au_dpri_dentry(struct dentry *dentry);
7845 +struct file;
7846 +void au_dpri_file(struct file *filp);
7847 +struct super_block;
7848 +void au_dpri_sb(struct super_block *sb);
7849 +
7850 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7851 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7852 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7853 +void au_dbg_verify_kthread(void);
7854 +
7855 +int __init au_debug_init(void);
7856 +
7857 +#define AuDbgWhlist(w) do { \
7858 +       mutex_lock(&au_dbg_mtx); \
7859 +       AuDbg(#w "\n"); \
7860 +       au_dpri_whlist(w); \
7861 +       mutex_unlock(&au_dbg_mtx); \
7862 +} while (0)
7863 +
7864 +#define AuDbgVdir(v) do { \
7865 +       mutex_lock(&au_dbg_mtx); \
7866 +       AuDbg(#v "\n"); \
7867 +       au_dpri_vdir(v); \
7868 +       mutex_unlock(&au_dbg_mtx); \
7869 +} while (0)
7870 +
7871 +#define AuDbgInode(i) do { \
7872 +       mutex_lock(&au_dbg_mtx); \
7873 +       AuDbg(#i "\n"); \
7874 +       au_dpri_inode(i); \
7875 +       mutex_unlock(&au_dbg_mtx); \
7876 +} while (0)
7877 +
7878 +#define AuDbgDAlias(i) do { \
7879 +       mutex_lock(&au_dbg_mtx); \
7880 +       AuDbg(#i "\n"); \
7881 +       au_dpri_dalias(i); \
7882 +       mutex_unlock(&au_dbg_mtx); \
7883 +} while (0)
7884 +
7885 +#define AuDbgDentry(d) do { \
7886 +       mutex_lock(&au_dbg_mtx); \
7887 +       AuDbg(#d "\n"); \
7888 +       au_dpri_dentry(d); \
7889 +       mutex_unlock(&au_dbg_mtx); \
7890 +} while (0)
7891 +
7892 +#define AuDbgFile(f) do { \
7893 +       mutex_lock(&au_dbg_mtx); \
7894 +       AuDbg(#f "\n"); \
7895 +       au_dpri_file(f); \
7896 +       mutex_unlock(&au_dbg_mtx); \
7897 +} while (0)
7898 +
7899 +#define AuDbgSb(sb) do { \
7900 +       mutex_lock(&au_dbg_mtx); \
7901 +       AuDbg(#sb "\n"); \
7902 +       au_dpri_sb(sb); \
7903 +       mutex_unlock(&au_dbg_mtx); \
7904 +} while (0)
7905 +
7906 +#define AuDbgSym(addr) do {                            \
7907 +       char sym[KSYM_SYMBOL_LEN];                      \
7908 +       sprint_symbol(sym, (unsigned long)addr);        \
7909 +       AuDbg("%s\n", sym);                             \
7910 +} while (0)
7911 +#else
7912 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7913 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7914 +AuStubVoid(au_dbg_verify_kthread, void)
7915 +AuStubInt0(__init au_debug_init, void)
7916 +
7917 +#define AuDbgWhlist(w)         do {} while (0)
7918 +#define AuDbgVdir(v)           do {} while (0)
7919 +#define AuDbgInode(i)          do {} while (0)
7920 +#define AuDbgDAlias(i)         do {} while (0)
7921 +#define AuDbgDentry(d)         do {} while (0)
7922 +#define AuDbgFile(f)           do {} while (0)
7923 +#define AuDbgSb(sb)            do {} while (0)
7924 +#define AuDbgSym(addr)         do {} while (0)
7925 +#endif /* CONFIG_AUFS_DEBUG */
7926 +
7927 +/* ---------------------------------------------------------------------- */
7928 +
7929 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7930 +int __init au_sysrq_init(void);
7931 +void au_sysrq_fin(void);
7932 +
7933 +#ifdef CONFIG_HW_CONSOLE
7934 +#define au_dbg_blocked() do { \
7935 +       WARN_ON(1); \
7936 +       handle_sysrq('w'); \
7937 +} while (0)
7938 +#else
7939 +AuStubVoid(au_dbg_blocked, void)
7940 +#endif
7941 +
7942 +#else
7943 +AuStubInt0(__init au_sysrq_init, void)
7944 +AuStubVoid(au_sysrq_fin, void)
7945 +AuStubVoid(au_dbg_blocked, void)
7946 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7947 +
7948 +#endif /* __KERNEL__ */
7949 +#endif /* __AUFS_DEBUG_H__ */
7950 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7951 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7952 +++ linux/fs/aufs/dentry.c      2023-10-31 09:31:04.196547417 +0100
7953 @@ -0,0 +1,1168 @@
7954 +// SPDX-License-Identifier: GPL-2.0
7955 +/*
7956 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7957 + *
7958 + * This program is free software; you can redistribute it and/or modify
7959 + * it under the terms of the GNU General Public License as published by
7960 + * the Free Software Foundation; either version 2 of the License, or
7961 + * (at your option) any later version.
7962 + *
7963 + * This program is distributed in the hope that it will be useful,
7964 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7965 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7966 + * GNU General Public License for more details.
7967 + *
7968 + * You should have received a copy of the GNU General Public License
7969 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7970 + */
7971 +
7972 +/*
7973 + * lookup and dentry operations
7974 + */
7975 +
7976 +#include <linux/iversion.h>
7977 +#include "aufs.h"
7978 +
7979 +/*
7980 + * returns positive/negative dentry, NULL or an error.
7981 + * NULL means whiteout-ed or not-found.
7982 + */
7983 +static struct dentry*
7984 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7985 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7986 +{
7987 +       struct dentry *h_dentry;
7988 +       struct inode *h_inode;
7989 +       struct au_branch *br;
7990 +       struct mnt_idmap *h_idmap;
7991 +       struct path h_path;
7992 +       int wh_found, opq;
7993 +       unsigned char wh_able;
7994 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7995 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7996 +                                                         IGNORE_PERM);
7997 +
7998 +       wh_found = 0;
7999 +       br = au_sbr(dentry->d_sb, bindex);
8000 +       h_path.dentry = h_parent;
8001 +       h_path.mnt = au_br_mnt(br);
8002 +       h_idmap = au_br_idmap(br);
8003 +       wh_able = !!au_br_whable(br->br_perm);
8004 +       if (wh_able)
8005 +               wh_found = au_wh_test(h_idmap, &h_path, &args->whname,
8006 +                                     ignore_perm);
8007 +       h_dentry = ERR_PTR(wh_found);
8008 +       if (!wh_found)
8009 +               goto real_lookup;
8010 +       if (unlikely(wh_found < 0))
8011 +               goto out;
8012 +
8013 +       /* We found a whiteout */
8014 +       /* au_set_dbbot(dentry, bindex); */
8015 +       au_set_dbwh(dentry, bindex);
8016 +       if (!allow_neg)
8017 +               return NULL; /* success */
8018 +
8019 +real_lookup:
8020 +       if (!ignore_perm)
8021 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8022 +       else
8023 +               h_dentry = au_sio_lkup_one(h_idmap, args->name, &h_path);
8024 +       if (IS_ERR(h_dentry)) {
8025 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8026 +                   && !allow_neg)
8027 +                       h_dentry = NULL;
8028 +               goto out;
8029 +       }
8030 +
8031 +       h_inode = d_inode(h_dentry);
8032 +       if (d_is_negative(h_dentry)) {
8033 +               if (!allow_neg)
8034 +                       goto out_neg;
8035 +       } else if (wh_found
8036 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8037 +               goto out_neg;
8038 +       else if (au_ftest_lkup(args->flags, DIRREN)
8039 +                /* && h_inode */
8040 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8041 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8042 +                     (unsigned long long)h_inode->i_ino);
8043 +               goto out_neg;
8044 +       }
8045 +
8046 +       if (au_dbbot(dentry) <= bindex)
8047 +               au_set_dbbot(dentry, bindex);
8048 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8049 +               au_set_dbtop(dentry, bindex);
8050 +       au_set_h_dptr(dentry, bindex, h_dentry);
8051 +
8052 +       if (!d_is_dir(h_dentry)
8053 +           || !wh_able
8054 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8055 +               goto out; /* success */
8056 +
8057 +       h_path.dentry = h_dentry;
8058 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8059 +       opq = au_diropq_test(h_idmap, &h_path);
8060 +       inode_unlock_shared(h_inode);
8061 +       if (opq > 0)
8062 +               au_set_dbdiropq(dentry, bindex);
8063 +       else if (unlikely(opq < 0)) {
8064 +               au_set_h_dptr(dentry, bindex, NULL);
8065 +               h_dentry = ERR_PTR(opq);
8066 +       }
8067 +       goto out;
8068 +
8069 +out_neg:
8070 +       dput(h_dentry);
8071 +       h_dentry = NULL;
8072 +out:
8073 +       return h_dentry;
8074 +}
8075 +
8076 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8077 +{
8078 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8079 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8080 +               return -EPERM;
8081 +       return 0;
8082 +}
8083 +
8084 +/*
8085 + * returns the number of lower positive dentries,
8086 + * otherwise an error.
8087 + * can be called at unlinking with @type is zero.
8088 + */
8089 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8090 +                  unsigned int flags)
8091 +{
8092 +       int npositive, err;
8093 +       aufs_bindex_t bindex, btail, bdiropq;
8094 +       unsigned char isdir, dirperm1, dirren;
8095 +       struct au_do_lookup_args args = {
8096 +               .flags          = flags,
8097 +               .name           = &dentry->d_name
8098 +       };
8099 +       struct dentry *parent;
8100 +       struct super_block *sb;
8101 +
8102 +       sb = dentry->d_sb;
8103 +       err = au_test_shwh(sb, args.name);
8104 +       if (unlikely(err))
8105 +               goto out;
8106 +
8107 +       err = au_wh_name_alloc(&args.whname, args.name);
8108 +       if (unlikely(err))
8109 +               goto out;
8110 +
8111 +       isdir = !!d_is_dir(dentry);
8112 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8113 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8114 +       if (dirren)
8115 +               au_fset_lkup(args.flags, DIRREN);
8116 +
8117 +       npositive = 0;
8118 +       parent = dget_parent(dentry);
8119 +       btail = au_dbtaildir(parent);
8120 +       for (bindex = btop; bindex <= btail; bindex++) {
8121 +               struct dentry *h_parent, *h_dentry;
8122 +               struct inode *h_inode, *h_dir;
8123 +               struct au_branch *br;
8124 +
8125 +               h_dentry = au_h_dptr(dentry, bindex);
8126 +               if (h_dentry) {
8127 +                       if (d_is_positive(h_dentry))
8128 +                               npositive++;
8129 +                       break;
8130 +               }
8131 +               h_parent = au_h_dptr(parent, bindex);
8132 +               if (!h_parent || !d_is_dir(h_parent))
8133 +                       continue;
8134 +
8135 +               if (dirren) {
8136 +                       /* if the inum matches, then use the prepared name */
8137 +                       err = au_dr_lkup_name(&args, bindex);
8138 +                       if (unlikely(err))
8139 +                               goto out_parent;
8140 +               }
8141 +
8142 +               h_dir = d_inode(h_parent);
8143 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8144 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8145 +               inode_unlock_shared(h_dir);
8146 +               err = PTR_ERR(h_dentry);
8147 +               if (IS_ERR(h_dentry))
8148 +                       goto out_parent;
8149 +               if (h_dentry)
8150 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8151 +               if (dirperm1)
8152 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8153 +
8154 +               if (au_dbwh(dentry) == bindex)
8155 +                       break;
8156 +               if (!h_dentry)
8157 +                       continue;
8158 +               if (d_is_negative(h_dentry))
8159 +                       continue;
8160 +               h_inode = d_inode(h_dentry);
8161 +               npositive++;
8162 +               if (!args.type)
8163 +                       args.type = h_inode->i_mode & S_IFMT;
8164 +               if (args.type != S_IFDIR)
8165 +                       break;
8166 +               else if (isdir) {
8167 +                       /* the type of lower may be different */
8168 +                       bdiropq = au_dbdiropq(dentry);
8169 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8170 +                               break;
8171 +               }
8172 +               br = au_sbr(sb, bindex);
8173 +               if (dirren
8174 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8175 +                                          /*add_ent*/NULL)) {
8176 +                       /* prepare next name to lookup */
8177 +                       err = au_dr_lkup(&args, dentry, bindex);
8178 +                       if (unlikely(err))
8179 +                               goto out_parent;
8180 +               }
8181 +       }
8182 +
8183 +       if (npositive) {
8184 +               AuLabel(positive);
8185 +               au_update_dbtop(dentry);
8186 +       }
8187 +       err = npositive;
8188 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8189 +                    && au_dbtop(dentry) < 0)) {
8190 +               err = -EIO;
8191 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8192 +                       dentry, err);
8193 +       }
8194 +
8195 +out_parent:
8196 +       dput(parent);
8197 +       au_kfree_try_rcu(args.whname.name);
8198 +       if (dirren)
8199 +               au_dr_lkup_fin(&args);
8200 +out:
8201 +       return err;
8202 +}
8203 +
8204 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
8205 +                              struct path *ppath)
8206 +{
8207 +       struct dentry *dentry;
8208 +       int wkq_err;
8209 +
8210 +       if (!au_test_h_perm_sio(idmap, d_inode(ppath->dentry), MAY_EXEC))
8211 +               dentry = vfsub_lkup_one(name, ppath);
8212 +       else {
8213 +               struct vfsub_lkup_one_args args = {
8214 +                       .errp   = &dentry,
8215 +                       .name   = name,
8216 +                       .ppath  = ppath
8217 +               };
8218 +
8219 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8220 +               if (unlikely(wkq_err))
8221 +                       dentry = ERR_PTR(wkq_err);
8222 +       }
8223 +
8224 +       return dentry;
8225 +}
8226 +
8227 +/*
8228 + * lookup @dentry on @bindex which should be negative.
8229 + */
8230 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8231 +{
8232 +       int err;
8233 +       struct dentry *parent, *h_dentry;
8234 +       struct au_branch *br;
8235 +       struct mnt_idmap *h_idmap;
8236 +       struct path h_ppath;
8237 +
8238 +       parent = dget_parent(dentry);
8239 +       br = au_sbr(dentry->d_sb, bindex);
8240 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8241 +       h_ppath.mnt = au_br_mnt(br);
8242 +       h_idmap = au_br_idmap(br);
8243 +       if (wh)
8244 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8245 +       else
8246 +               h_dentry = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
8247 +       err = PTR_ERR(h_dentry);
8248 +       if (IS_ERR(h_dentry))
8249 +               goto out;
8250 +       if (unlikely(d_is_positive(h_dentry))) {
8251 +               err = -EIO;
8252 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8253 +               dput(h_dentry);
8254 +               goto out;
8255 +       }
8256 +
8257 +       err = 0;
8258 +       if (bindex < au_dbtop(dentry))
8259 +               au_set_dbtop(dentry, bindex);
8260 +       if (au_dbbot(dentry) < bindex)
8261 +               au_set_dbbot(dentry, bindex);
8262 +       au_set_h_dptr(dentry, bindex, h_dentry);
8263 +
8264 +out:
8265 +       dput(parent);
8266 +       return err;
8267 +}
8268 +
8269 +/* ---------------------------------------------------------------------- */
8270 +
8271 +/* subset of struct inode */
8272 +struct au_iattr {
8273 +       unsigned long           i_ino;
8274 +       /* unsigned int         i_nlink; */
8275 +       kuid_t                  i_uid;
8276 +       kgid_t                  i_gid;
8277 +       u64                     i_version;
8278 +/*
8279 +       loff_t                  i_size;
8280 +       blkcnt_t                i_blocks;
8281 +*/
8282 +       umode_t                 i_mode;
8283 +};
8284 +
8285 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8286 +{
8287 +       ia->i_ino = h_inode->i_ino;
8288 +       /* ia->i_nlink = h_inode->i_nlink; */
8289 +       ia->i_uid = h_inode->i_uid;
8290 +       ia->i_gid = h_inode->i_gid;
8291 +       ia->i_version = inode_query_iversion(h_inode);
8292 +/*
8293 +       ia->i_size = h_inode->i_size;
8294 +       ia->i_blocks = h_inode->i_blocks;
8295 +*/
8296 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8297 +}
8298 +
8299 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8300 +{
8301 +       return ia->i_ino != h_inode->i_ino
8302 +               /* || ia->i_nlink != h_inode->i_nlink */
8303 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8304 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8305 +               || !inode_eq_iversion(h_inode, ia->i_version)
8306 +/*
8307 +               || ia->i_size != h_inode->i_size
8308 +               || ia->i_blocks != h_inode->i_blocks
8309 +*/
8310 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8311 +}
8312 +
8313 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8314 +                             struct au_branch *br)
8315 +{
8316 +       int err;
8317 +       struct au_iattr ia;
8318 +       struct inode *h_inode;
8319 +       struct dentry *h_d;
8320 +       struct super_block *h_sb;
8321 +       struct path h_ppath;
8322 +
8323 +       err = 0;
8324 +       memset(&ia, -1, sizeof(ia));
8325 +       h_sb = h_dentry->d_sb;
8326 +       h_inode = NULL;
8327 +       if (d_is_positive(h_dentry)) {
8328 +               h_inode = d_inode(h_dentry);
8329 +               au_iattr_save(&ia, h_inode);
8330 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8331 +               /* nfs d_revalidate may return 0 for negative dentry */
8332 +               /* fuse d_revalidate always return 0 for negative dentry */
8333 +               goto out;
8334 +
8335 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8336 +       h_ppath.dentry = h_parent;
8337 +       h_ppath.mnt = au_br_mnt(br);
8338 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8339 +       err = PTR_ERR(h_d);
8340 +       if (IS_ERR(h_d))
8341 +               goto out;
8342 +
8343 +       err = 0;
8344 +       if (unlikely(h_d != h_dentry
8345 +                    || d_inode(h_d) != h_inode
8346 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8347 +               err = au_busy_or_stale();
8348 +       dput(h_d);
8349 +
8350 +out:
8351 +       AuTraceErr(err);
8352 +       return err;
8353 +}
8354 +
8355 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8356 +               struct dentry *h_parent, struct au_branch *br)
8357 +{
8358 +       int err;
8359 +
8360 +       err = 0;
8361 +       if (udba == AuOpt_UDBA_REVAL
8362 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8363 +               IMustLock(h_dir);
8364 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8365 +       } else if (udba != AuOpt_UDBA_NONE)
8366 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8367 +
8368 +       return err;
8369 +}
8370 +
8371 +/* ---------------------------------------------------------------------- */
8372 +
8373 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8374 +{
8375 +       int err;
8376 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8377 +       struct au_hdentry tmp, *p, *q;
8378 +       struct au_dinfo *dinfo;
8379 +       struct super_block *sb;
8380 +
8381 +       DiMustWriteLock(dentry);
8382 +
8383 +       sb = dentry->d_sb;
8384 +       dinfo = au_di(dentry);
8385 +       bbot = dinfo->di_bbot;
8386 +       bwh = dinfo->di_bwh;
8387 +       bdiropq = dinfo->di_bdiropq;
8388 +       bindex = dinfo->di_btop;
8389 +       p = au_hdentry(dinfo, bindex);
8390 +       for (; bindex <= bbot; bindex++, p++) {
8391 +               if (!p->hd_dentry)
8392 +                       continue;
8393 +
8394 +               new_bindex = au_br_index(sb, p->hd_id);
8395 +               if (new_bindex == bindex)
8396 +                       continue;
8397 +
8398 +               if (dinfo->di_bwh == bindex)
8399 +                       bwh = new_bindex;
8400 +               if (dinfo->di_bdiropq == bindex)
8401 +                       bdiropq = new_bindex;
8402 +               if (new_bindex < 0) {
8403 +                       au_hdput(p);
8404 +                       p->hd_dentry = NULL;
8405 +                       continue;
8406 +               }
8407 +
8408 +               /* swap two lower dentries, and loop again */
8409 +               q = au_hdentry(dinfo, new_bindex);
8410 +               tmp = *q;
8411 +               *q = *p;
8412 +               *p = tmp;
8413 +               if (tmp.hd_dentry) {
8414 +                       bindex--;
8415 +                       p--;
8416 +               }
8417 +       }
8418 +
8419 +       dinfo->di_bwh = -1;
8420 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8421 +               dinfo->di_bwh = bwh;
8422 +
8423 +       dinfo->di_bdiropq = -1;
8424 +       if (bdiropq >= 0
8425 +           && bdiropq <= au_sbbot(sb)
8426 +           && au_sbr_whable(sb, bdiropq))
8427 +               dinfo->di_bdiropq = bdiropq;
8428 +
8429 +       err = -EIO;
8430 +       dinfo->di_btop = -1;
8431 +       dinfo->di_bbot = -1;
8432 +       bbot = au_dbbot(parent);
8433 +       bindex = 0;
8434 +       p = au_hdentry(dinfo, bindex);
8435 +       for (; bindex <= bbot; bindex++, p++)
8436 +               if (p->hd_dentry) {
8437 +                       dinfo->di_btop = bindex;
8438 +                       break;
8439 +               }
8440 +
8441 +       if (dinfo->di_btop >= 0) {
8442 +               bindex = bbot;
8443 +               p = au_hdentry(dinfo, bindex);
8444 +               for (; bindex >= 0; bindex--, p--)
8445 +                       if (p->hd_dentry) {
8446 +                               dinfo->di_bbot = bindex;
8447 +                               err = 0;
8448 +                               break;
8449 +                       }
8450 +       }
8451 +
8452 +       return err;
8453 +}
8454 +
8455 +static void au_do_hide(struct dentry *dentry)
8456 +{
8457 +       struct inode *inode;
8458 +
8459 +       if (d_really_is_positive(dentry)) {
8460 +               inode = d_inode(dentry);
8461 +               if (!d_is_dir(dentry)) {
8462 +                       if (inode->i_nlink && !d_unhashed(dentry))
8463 +                               drop_nlink(inode);
8464 +               } else {
8465 +                       clear_nlink(inode);
8466 +                       /* stop next lookup */
8467 +                       inode->i_flags |= S_DEAD;
8468 +               }
8469 +               smp_mb(); /* necessary? */
8470 +       }
8471 +       d_drop(dentry);
8472 +}
8473 +
8474 +static int au_hide_children(struct dentry *parent)
8475 +{
8476 +       int err, i, j, ndentry;
8477 +       struct au_dcsub_pages dpages;
8478 +       struct au_dpage *dpage;
8479 +       struct dentry *dentry;
8480 +
8481 +       err = au_dpages_init(&dpages, GFP_NOFS);
8482 +       if (unlikely(err))
8483 +               goto out;
8484 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8485 +       if (unlikely(err))
8486 +               goto out_dpages;
8487 +
8488 +       /* in reverse order */
8489 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8490 +               dpage = dpages.dpages + i;
8491 +               ndentry = dpage->ndentry;
8492 +               for (j = ndentry - 1; j >= 0; j--) {
8493 +                       dentry = dpage->dentries[j];
8494 +                       if (dentry != parent)
8495 +                               au_do_hide(dentry);
8496 +               }
8497 +       }
8498 +
8499 +out_dpages:
8500 +       au_dpages_free(&dpages);
8501 +out:
8502 +       return err;
8503 +}
8504 +
8505 +static void au_hide(struct dentry *dentry)
8506 +{
8507 +       int err;
8508 +
8509 +       AuDbgDentry(dentry);
8510 +       if (d_is_dir(dentry)) {
8511 +               /* shrink_dcache_parent(dentry); */
8512 +               err = au_hide_children(dentry);
8513 +               if (unlikely(err))
8514 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8515 +                               dentry, err);
8516 +       }
8517 +       au_do_hide(dentry);
8518 +}
8519 +
8520 +/*
8521 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8522 + *
8523 + * a dirty branch is added
8524 + * - on the top of layers
8525 + * - in the middle of layers
8526 + * - to the bottom of layers
8527 + *
8528 + * on the added branch there exists
8529 + * - a whiteout
8530 + * - a diropq
8531 + * - a same named entry
8532 + *   + exist
8533 + *     * negative --> positive
8534 + *     * positive --> positive
8535 + *      - type is unchanged
8536 + *      - type is changed
8537 + *   + doesn't exist
8538 + *     * negative --> negative
8539 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8540 + * - none
8541 + */
8542 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8543 +                              struct au_dinfo *tmp)
8544 +{
8545 +       int err;
8546 +       aufs_bindex_t bindex, bbot;
8547 +       struct {
8548 +               struct dentry *dentry;
8549 +               struct inode *inode;
8550 +               mode_t mode;
8551 +       } orig_h, tmp_h = {
8552 +               .dentry = NULL
8553 +       };
8554 +       struct au_hdentry *hd;
8555 +       struct inode *inode, *h_inode;
8556 +       struct dentry *h_dentry;
8557 +
8558 +       err = 0;
8559 +       AuDebugOn(dinfo->di_btop < 0);
8560 +       orig_h.mode = 0;
8561 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8562 +       orig_h.inode = NULL;
8563 +       if (d_is_positive(orig_h.dentry)) {
8564 +               orig_h.inode = d_inode(orig_h.dentry);
8565 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8566 +       }
8567 +       if (tmp->di_btop >= 0) {
8568 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8569 +               if (d_is_positive(tmp_h.dentry)) {
8570 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8571 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8572 +               }
8573 +       }
8574 +
8575 +       inode = NULL;
8576 +       if (d_really_is_positive(dentry))
8577 +               inode = d_inode(dentry);
8578 +       if (!orig_h.inode) {
8579 +               AuDbg("negative originally\n");
8580 +               if (inode) {
8581 +                       au_hide(dentry);
8582 +                       goto out;
8583 +               }
8584 +               AuDebugOn(inode);
8585 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8586 +               AuDebugOn(dinfo->di_bdiropq != -1);
8587 +
8588 +               if (!tmp_h.inode) {
8589 +                       AuDbg("negative --> negative\n");
8590 +                       /* should have only one negative lower */
8591 +                       if (tmp->di_btop >= 0
8592 +                           && tmp->di_btop < dinfo->di_btop) {
8593 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8594 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8595 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8596 +                               au_di_cp(dinfo, tmp);
8597 +                               hd = au_hdentry(tmp, tmp->di_btop);
8598 +                               au_set_h_dptr(dentry, tmp->di_btop,
8599 +                                             dget(hd->hd_dentry));
8600 +                       }
8601 +                       au_dbg_verify_dinode(dentry);
8602 +               } else {
8603 +                       AuDbg("negative --> positive\n");
8604 +                       /*
8605 +                        * similar to the behaviour of creating with bypassing
8606 +                        * aufs.
8607 +                        * unhash it in order to force an error in the
8608 +                        * succeeding create operation.
8609 +                        * we should not set S_DEAD here.
8610 +                        */
8611 +                       d_drop(dentry);
8612 +                       /* au_di_swap(tmp, dinfo); */
8613 +                       au_dbg_verify_dinode(dentry);
8614 +               }
8615 +       } else {
8616 +               AuDbg("positive originally\n");
8617 +               /* inode may be NULL */
8618 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8619 +               if (!tmp_h.inode) {
8620 +                       AuDbg("positive --> negative\n");
8621 +                       /* or bypassing aufs */
8622 +                       au_hide(dentry);
8623 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8624 +                               dinfo->di_bwh = tmp->di_bwh;
8625 +                       if (inode)
8626 +                               err = au_refresh_hinode_self(inode);
8627 +                       au_dbg_verify_dinode(dentry);
8628 +               } else if (orig_h.mode == tmp_h.mode) {
8629 +                       AuDbg("positive --> positive, same type\n");
8630 +                       if (!S_ISDIR(orig_h.mode)
8631 +                           && dinfo->di_btop > tmp->di_btop) {
8632 +                               /*
8633 +                                * similar to the behaviour of removing and
8634 +                                * creating.
8635 +                                */
8636 +                               au_hide(dentry);
8637 +                               if (inode)
8638 +                                       err = au_refresh_hinode_self(inode);
8639 +                               au_dbg_verify_dinode(dentry);
8640 +                       } else {
8641 +                               /* fill empty slots */
8642 +                               if (dinfo->di_btop > tmp->di_btop)
8643 +                                       dinfo->di_btop = tmp->di_btop;
8644 +                               if (dinfo->di_bbot < tmp->di_bbot)
8645 +                                       dinfo->di_bbot = tmp->di_bbot;
8646 +                               dinfo->di_bwh = tmp->di_bwh;
8647 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8648 +                               bbot = dinfo->di_bbot;
8649 +                               bindex = tmp->di_btop;
8650 +                               hd = au_hdentry(tmp, bindex);
8651 +                               for (; bindex <= bbot; bindex++, hd++) {
8652 +                                       if (au_h_dptr(dentry, bindex))
8653 +                                               continue;
8654 +                                       h_dentry = hd->hd_dentry;
8655 +                                       if (!h_dentry)
8656 +                                               continue;
8657 +                                       AuDebugOn(d_is_negative(h_dentry));
8658 +                                       h_inode = d_inode(h_dentry);
8659 +                                       AuDebugOn(orig_h.mode
8660 +                                                 != (h_inode->i_mode
8661 +                                                     & S_IFMT));
8662 +                                       au_set_h_dptr(dentry, bindex,
8663 +                                                     dget(h_dentry));
8664 +                               }
8665 +                               if (inode)
8666 +                                       err = au_refresh_hinode(inode, dentry);
8667 +                               au_dbg_verify_dinode(dentry);
8668 +                       }
8669 +               } else {
8670 +                       AuDbg("positive --> positive, different type\n");
8671 +                       /* similar to the behaviour of removing and creating */
8672 +                       au_hide(dentry);
8673 +                       if (inode)
8674 +                               err = au_refresh_hinode_self(inode);
8675 +                       au_dbg_verify_dinode(dentry);
8676 +               }
8677 +       }
8678 +
8679 +out:
8680 +       return err;
8681 +}
8682 +
8683 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8684 +{
8685 +       const struct dentry_operations *dop
8686 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8687 +       static const unsigned int mask
8688 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8689 +
8690 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8691 +
8692 +       if (dentry->d_op == dop)
8693 +               return;
8694 +
8695 +       AuDbg("%pd\n", dentry);
8696 +       spin_lock(&dentry->d_lock);
8697 +       if (dop == &aufs_dop)
8698 +               dentry->d_flags |= mask;
8699 +       else
8700 +               dentry->d_flags &= ~mask;
8701 +       dentry->d_op = dop;
8702 +       spin_unlock(&dentry->d_lock);
8703 +}
8704 +
8705 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8706 +{
8707 +       int err, ebrange, nbr;
8708 +       unsigned int sigen;
8709 +       struct au_dinfo *dinfo, *tmp;
8710 +       struct super_block *sb;
8711 +       struct inode *inode;
8712 +
8713 +       DiMustWriteLock(dentry);
8714 +       AuDebugOn(IS_ROOT(dentry));
8715 +       AuDebugOn(d_really_is_negative(parent));
8716 +
8717 +       sb = dentry->d_sb;
8718 +       sigen = au_sigen(sb);
8719 +       err = au_digen_test(parent, sigen);
8720 +       if (unlikely(err))
8721 +               goto out;
8722 +
8723 +       nbr = au_sbbot(sb) + 1;
8724 +       dinfo = au_di(dentry);
8725 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8726 +       if (unlikely(err))
8727 +               goto out;
8728 +       ebrange = au_dbrange_test(dentry);
8729 +       if (!ebrange)
8730 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8731 +
8732 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8733 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8734 +               if (d_really_is_positive(dentry)) {
8735 +                       inode = d_inode(dentry);
8736 +                       err = au_refresh_hinode_self(inode);
8737 +               }
8738 +               au_dbg_verify_dinode(dentry);
8739 +               if (!err)
8740 +                       goto out_dgen; /* success */
8741 +               goto out;
8742 +       }
8743 +
8744 +       /* temporary dinfo */
8745 +       AuDbgDentry(dentry);
8746 +       err = -ENOMEM;
8747 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8748 +       if (unlikely(!tmp))
8749 +               goto out;
8750 +       au_di_swap(tmp, dinfo);
8751 +       /* returns the number of positive dentries */
8752 +       /*
8753 +        * if current working dir is removed, it returns an error.
8754 +        * but the dentry is legal.
8755 +        */
8756 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8757 +       AuDbgDentry(dentry);
8758 +       au_di_swap(tmp, dinfo);
8759 +       if (err == -ENOENT)
8760 +               err = 0;
8761 +       if (err >= 0) {
8762 +               /* compare/refresh by dinfo */
8763 +               AuDbgDentry(dentry);
8764 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8765 +               au_dbg_verify_dinode(dentry);
8766 +               AuTraceErr(err);
8767 +       }
8768 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8769 +       au_rw_write_unlock(&tmp->di_rwsem);
8770 +       au_di_free(tmp);
8771 +       if (unlikely(err))
8772 +               goto out;
8773 +
8774 +out_dgen:
8775 +       au_update_digen(dentry);
8776 +out:
8777 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8778 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8779 +               AuDbgDentry(dentry);
8780 +       }
8781 +       AuTraceErr(err);
8782 +       return err;
8783 +}
8784 +
8785 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8786 +                          struct dentry *dentry, aufs_bindex_t bindex)
8787 +{
8788 +       int err, valid;
8789 +
8790 +       err = 0;
8791 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8792 +               goto out;
8793 +
8794 +       AuDbg("b%d\n", bindex);
8795 +       /*
8796 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8797 +        * due to whiteout and branch permission.
8798 +        */
8799 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8800 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8801 +       /* it may return tri-state */
8802 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8803 +
8804 +       if (unlikely(valid < 0))
8805 +               err = valid;
8806 +       else if (!valid)
8807 +               err = -EINVAL;
8808 +
8809 +out:
8810 +       AuTraceErr(err);
8811 +       return err;
8812 +}
8813 +
8814 +/* todo: remove this */
8815 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8816 +                         unsigned int flags, int do_udba, int dirren)
8817 +{
8818 +       int err;
8819 +       umode_t mode, h_mode;
8820 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8821 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8822 +       struct inode *h_inode, *h_cached_inode;
8823 +       struct dentry *h_dentry;
8824 +       struct qstr *name, *h_name;
8825 +
8826 +       err = 0;
8827 +       plus = 0;
8828 +       mode = 0;
8829 +       ibs = -1;
8830 +       ibe = -1;
8831 +       unhashed = !!d_unhashed(dentry);
8832 +       is_root = !!IS_ROOT(dentry);
8833 +       name = &dentry->d_name;
8834 +       tmpfile = au_di(dentry)->di_tmpfile;
8835 +
8836 +       /*
8837 +        * Theoretically, REVAL test should be unnecessary in case of
8838 +        * {FS,I}NOTIFY.
8839 +        * But {fs,i}notify doesn't fire some necessary events,
8840 +        *      IN_ATTRIB for atime/nlink/pageio
8841 +        * Let's do REVAL test too.
8842 +        */
8843 +       if (do_udba && inode) {
8844 +               mode = (inode->i_mode & S_IFMT);
8845 +               plus = (inode->i_nlink > 0);
8846 +               ibs = au_ibtop(inode);
8847 +               ibe = au_ibbot(inode);
8848 +       }
8849 +
8850 +       btop = au_dbtop(dentry);
8851 +       btail = btop;
8852 +       if (inode && S_ISDIR(inode->i_mode))
8853 +               btail = au_dbtaildir(dentry);
8854 +       for (bindex = btop; bindex <= btail; bindex++) {
8855 +               h_dentry = au_h_dptr(dentry, bindex);
8856 +               if (!h_dentry)
8857 +                       continue;
8858 +
8859 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8860 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8861 +               spin_lock(&h_dentry->d_lock);
8862 +               h_name = &h_dentry->d_name;
8863 +               if (unlikely(do_udba
8864 +                            && !is_root
8865 +                            && ((!h_nfs
8866 +                                 && (unhashed != !!d_unhashed(h_dentry)
8867 +                                     || (!tmpfile && !dirren
8868 +                                         && !au_qstreq(name, h_name))
8869 +                                         ))
8870 +                                || (h_nfs
8871 +                                    && !(flags & LOOKUP_OPEN)
8872 +                                    && (h_dentry->d_flags
8873 +                                        & DCACHE_NFSFS_RENAMED)))
8874 +                           )) {
8875 +                       int h_unhashed;
8876 +
8877 +                       h_unhashed = d_unhashed(h_dentry);
8878 +                       spin_unlock(&h_dentry->d_lock);
8879 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8880 +                             unhashed, h_unhashed, dentry, h_dentry);
8881 +                       goto err;
8882 +               }
8883 +               spin_unlock(&h_dentry->d_lock);
8884 +
8885 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8886 +               if (unlikely(err))
8887 +                       /* do not goto err, to keep the errno */
8888 +                       break;
8889 +
8890 +               /* todo: plink too? */
8891 +               if (!do_udba)
8892 +                       continue;
8893 +
8894 +               /* UDBA tests */
8895 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8896 +                       goto err;
8897 +
8898 +               h_inode = NULL;
8899 +               if (d_is_positive(h_dentry))
8900 +                       h_inode = d_inode(h_dentry);
8901 +               h_plus = plus;
8902 +               h_mode = mode;
8903 +               h_cached_inode = h_inode;
8904 +               if (h_inode) {
8905 +                       h_mode = (h_inode->i_mode & S_IFMT);
8906 +                       h_plus = (h_inode->i_nlink > 0);
8907 +               }
8908 +               if (inode && ibs <= bindex && bindex <= ibe)
8909 +                       h_cached_inode = au_h_iptr(inode, bindex);
8910 +
8911 +               if (!h_nfs) {
8912 +                       if (unlikely(plus != h_plus && !tmpfile))
8913 +                               goto err;
8914 +               } else {
8915 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8916 +                                    && !is_root
8917 +                                    && !IS_ROOT(h_dentry)
8918 +                                    && unhashed != d_unhashed(h_dentry)))
8919 +                               goto err;
8920 +               }
8921 +               if (unlikely(mode != h_mode
8922 +                            || h_cached_inode != h_inode))
8923 +                       goto err;
8924 +               continue;
8925 +
8926 +err:
8927 +               err = -EINVAL;
8928 +               break;
8929 +       }
8930 +
8931 +       AuTraceErr(err);
8932 +       return err;
8933 +}
8934 +
8935 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8936 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8937 +{
8938 +       int err;
8939 +       struct dentry *parent;
8940 +
8941 +       if (!au_digen_test(dentry, sigen))
8942 +               return 0;
8943 +
8944 +       parent = dget_parent(dentry);
8945 +       di_read_lock_parent(parent, AuLock_IR);
8946 +       AuDebugOn(au_digen_test(parent, sigen));
8947 +       au_dbg_verify_gen(parent, sigen);
8948 +       err = au_refresh_dentry(dentry, parent);
8949 +       di_read_unlock(parent, AuLock_IR);
8950 +       dput(parent);
8951 +       AuTraceErr(err);
8952 +       return err;
8953 +}
8954 +
8955 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8956 +{
8957 +       int err;
8958 +       struct dentry *d, *parent;
8959 +
8960 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8961 +               return simple_reval_dpath(dentry, sigen);
8962 +
8963 +       /* slow loop, keep it simple and stupid */
8964 +       /* cf: au_cpup_dirs() */
8965 +       err = 0;
8966 +       parent = NULL;
8967 +       while (au_digen_test(dentry, sigen)) {
8968 +               d = dentry;
8969 +               while (1) {
8970 +                       dput(parent);
8971 +                       parent = dget_parent(d);
8972 +                       if (!au_digen_test(parent, sigen))
8973 +                               break;
8974 +                       d = parent;
8975 +               }
8976 +
8977 +               if (d != dentry)
8978 +                       di_write_lock_child2(d);
8979 +
8980 +               /* someone might update our dentry while we were sleeping */
8981 +               if (au_digen_test(d, sigen)) {
8982 +                       /*
8983 +                        * todo: consolidate with simple_reval_dpath(),
8984 +                        * do_refresh() and au_reval_for_attr().
8985 +                        */
8986 +                       di_read_lock_parent(parent, AuLock_IR);
8987 +                       err = au_refresh_dentry(d, parent);
8988 +                       di_read_unlock(parent, AuLock_IR);
8989 +               }
8990 +
8991 +               if (d != dentry)
8992 +                       di_write_unlock(d);
8993 +               dput(parent);
8994 +               if (unlikely(err))
8995 +                       break;
8996 +       }
8997 +
8998 +       return err;
8999 +}
9000 +
9001 +/*
9002 + * if valid returns 1, otherwise 0.
9003 + */
9004 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9005 +{
9006 +       int valid, err;
9007 +       unsigned int sigen;
9008 +       unsigned char do_udba, dirren;
9009 +       struct super_block *sb;
9010 +       struct inode *inode;
9011 +
9012 +       /* todo: support rcu-walk? */
9013 +       if (flags & LOOKUP_RCU)
9014 +               return -ECHILD;
9015 +
9016 +       valid = 0;
9017 +       if (unlikely(!au_di(dentry)))
9018 +               goto out;
9019 +
9020 +       valid = 1;
9021 +       sb = dentry->d_sb;
9022 +       /*
9023 +        * todo: very ugly
9024 +        * i_mutex of parent dir may be held,
9025 +        * but we should not return 'invalid' due to busy.
9026 +        */
9027 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9028 +       if (unlikely(err)) {
9029 +               valid = err;
9030 +               AuTraceErr(err);
9031 +               goto out;
9032 +       }
9033 +       inode = NULL;
9034 +       if (d_really_is_positive(dentry))
9035 +               inode = d_inode(dentry);
9036 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9037 +               err = -EINVAL;
9038 +               AuTraceErr(err);
9039 +               goto out_dgrade;
9040 +       }
9041 +       if (unlikely(au_dbrange_test(dentry))) {
9042 +               err = -EINVAL;
9043 +               AuTraceErr(err);
9044 +               goto out_dgrade;
9045 +       }
9046 +
9047 +       sigen = au_sigen(sb);
9048 +       if (au_digen_test(dentry, sigen)) {
9049 +               AuDebugOn(IS_ROOT(dentry));
9050 +               err = au_reval_dpath(dentry, sigen);
9051 +               if (unlikely(err)) {
9052 +                       AuTraceErr(err);
9053 +                       goto out_dgrade;
9054 +               }
9055 +       }
9056 +       di_downgrade_lock(dentry, AuLock_IR);
9057 +
9058 +       err = -EINVAL;
9059 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9060 +           && inode
9061 +           && !(inode->i_state && I_LINKABLE)
9062 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9063 +               AuTraceErr(err);
9064 +               goto out_inval;
9065 +       }
9066 +
9067 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9068 +       if (do_udba && inode) {
9069 +               aufs_bindex_t btop = au_ibtop(inode);
9070 +               struct inode *h_inode;
9071 +
9072 +               if (btop >= 0) {
9073 +                       h_inode = au_h_iptr(inode, btop);
9074 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9075 +                               AuTraceErr(err);
9076 +                               goto out_inval;
9077 +                       }
9078 +               }
9079 +       }
9080 +
9081 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9082 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9083 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9084 +               err = -EIO;
9085 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9086 +                     dentry, err);
9087 +       }
9088 +       goto out_inval;
9089 +
9090 +out_dgrade:
9091 +       di_downgrade_lock(dentry, AuLock_IR);
9092 +out_inval:
9093 +       aufs_read_unlock(dentry, AuLock_IR);
9094 +       AuTraceErr(err);
9095 +       valid = !err;
9096 +out:
9097 +       if (!valid) {
9098 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9099 +               d_drop(dentry);
9100 +       }
9101 +       return valid;
9102 +}
9103 +
9104 +static void aufs_d_release(struct dentry *dentry)
9105 +{
9106 +       if (au_di(dentry)) {
9107 +               au_di_fin(dentry);
9108 +               au_hn_di_reinit(dentry);
9109 +       }
9110 +}
9111 +
9112 +const struct dentry_operations aufs_dop = {
9113 +       .d_revalidate           = aufs_d_revalidate,
9114 +       .d_weak_revalidate      = aufs_d_revalidate,
9115 +       .d_release              = aufs_d_release
9116 +};
9117 +
9118 +/* aufs_dop without d_revalidate */
9119 +const struct dentry_operations aufs_dop_noreval = {
9120 +       .d_release              = aufs_d_release
9121 +};
9122 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9123 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9124 +++ linux/fs/aufs/dentry.h      2023-10-31 09:31:04.196547417 +0100
9125 @@ -0,0 +1,270 @@
9126 +/* SPDX-License-Identifier: GPL-2.0 */
9127 +/*
9128 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9129 + *
9130 + * This program is free software; you can redistribute it and/or modify
9131 + * it under the terms of the GNU General Public License as published by
9132 + * the Free Software Foundation; either version 2 of the License, or
9133 + * (at your option) any later version.
9134 + *
9135 + * This program is distributed in the hope that it will be useful,
9136 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9137 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9138 + * GNU General Public License for more details.
9139 + *
9140 + * You should have received a copy of the GNU General Public License
9141 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9142 + */
9143 +
9144 +/*
9145 + * lookup and dentry operations
9146 + */
9147 +
9148 +#ifndef __AUFS_DENTRY_H__
9149 +#define __AUFS_DENTRY_H__
9150 +
9151 +#ifdef __KERNEL__
9152 +
9153 +#include <linux/dcache.h>
9154 +#include "dirren.h"
9155 +#include "rwsem.h"
9156 +
9157 +struct au_hdentry {
9158 +       struct dentry           *hd_dentry;
9159 +       aufs_bindex_t           hd_id;
9160 +};
9161 +
9162 +struct au_dinfo {
9163 +       atomic_t                di_generation;
9164 +
9165 +       struct au_rwsem         di_rwsem;
9166 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9167 +       unsigned char           di_tmpfile; /* to allow the different name */
9168 +       struct au_hdentry       *di_hdentry;
9169 +       struct file             *di_htmpfile;
9170 +       struct rcu_head         rcu;
9171 +} ____cacheline_aligned_in_smp;
9172 +
9173 +/* ---------------------------------------------------------------------- */
9174 +
9175 +/* flags for au_lkup_dentry() */
9176 +#define AuLkup_ALLOW_NEG       1
9177 +#define AuLkup_IGNORE_PERM     (1 << 1)
9178 +#define AuLkup_DIRREN          (1 << 2)
9179 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9180 +#define au_fset_lkup(flags, name) \
9181 +       do { (flags) |= AuLkup_##name; } while (0)
9182 +#define au_fclr_lkup(flags, name) \
9183 +       do { (flags) &= ~AuLkup_##name; } while (0)
9184 +
9185 +#ifndef CONFIG_AUFS_DIRREN
9186 +#undef AuLkup_DIRREN
9187 +#define AuLkup_DIRREN 0
9188 +#endif
9189 +
9190 +struct au_do_lookup_args {
9191 +       unsigned int            flags;
9192 +       mode_t                  type;
9193 +       struct qstr             whname, *name;
9194 +       struct au_dr_lookup     dirren;
9195 +};
9196 +
9197 +/* ---------------------------------------------------------------------- */
9198 +
9199 +/* dentry.c */
9200 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9201 +struct au_branch;
9202 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
9203 +                              struct path *ppath);
9204 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9205 +               struct dentry *h_parent, struct au_branch *br);
9206 +
9207 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9208 +                  unsigned int flags);
9209 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9210 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9211 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9212 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9213 +
9214 +/* dinfo.c */
9215 +void au_di_init_once(void *_di);
9216 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9217 +void au_di_free(struct au_dinfo *dinfo);
9218 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9219 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9220 +int au_di_init(struct dentry *dentry);
9221 +void au_di_fin(struct dentry *dentry);
9222 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9223 +
9224 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9225 +void di_read_unlock(struct dentry *d, int flags);
9226 +void di_downgrade_lock(struct dentry *d, int flags);
9227 +void di_write_lock(struct dentry *d, unsigned int lsc);
9228 +void di_write_unlock(struct dentry *d);
9229 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9230 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9231 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9232 +
9233 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9234 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9235 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9236 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9237 +
9238 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9239 +                  struct dentry *h_dentry);
9240 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9241 +int au_dbrange_test(struct dentry *dentry);
9242 +void au_update_digen(struct dentry *dentry);
9243 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9244 +void au_update_dbtop(struct dentry *dentry);
9245 +void au_update_dbbot(struct dentry *dentry);
9246 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9247 +
9248 +/* ---------------------------------------------------------------------- */
9249 +
9250 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9251 +{
9252 +       return dentry->d_fsdata;
9253 +}
9254 +
9255 +/* ---------------------------------------------------------------------- */
9256 +
9257 +/* lock subclass for dinfo */
9258 +enum {
9259 +       AuLsc_DI_CHILD,         /* child first */
9260 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9261 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9262 +       AuLsc_DI_PARENT,
9263 +       AuLsc_DI_PARENT2,
9264 +       AuLsc_DI_PARENT3,
9265 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9266 +};
9267 +
9268 +/*
9269 + * di_read_lock_child, di_write_lock_child,
9270 + * di_read_lock_child2, di_write_lock_child2,
9271 + * di_read_lock_child3, di_write_lock_child3,
9272 + * di_read_lock_parent, di_write_lock_parent,
9273 + * di_read_lock_parent2, di_write_lock_parent2,
9274 + * di_read_lock_parent3, di_write_lock_parent3,
9275 + */
9276 +#define AuReadLockFunc(name, lsc) \
9277 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9278 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9279 +
9280 +#define AuWriteLockFunc(name, lsc) \
9281 +static inline void di_write_lock_##name(struct dentry *d) \
9282 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9283 +
9284 +#define AuRWLockFuncs(name, lsc) \
9285 +       AuReadLockFunc(name, lsc) \
9286 +       AuWriteLockFunc(name, lsc)
9287 +
9288 +AuRWLockFuncs(child, CHILD);
9289 +AuRWLockFuncs(child2, CHILD2);
9290 +AuRWLockFuncs(child3, CHILD3);
9291 +AuRWLockFuncs(parent, PARENT);
9292 +AuRWLockFuncs(parent2, PARENT2);
9293 +AuRWLockFuncs(parent3, PARENT3);
9294 +
9295 +#undef AuReadLockFunc
9296 +#undef AuWriteLockFunc
9297 +#undef AuRWLockFuncs
9298 +
9299 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9300 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9301 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9302 +
9303 +/* ---------------------------------------------------------------------- */
9304 +
9305 +/* todo: memory barrier? */
9306 +static inline unsigned int au_digen(struct dentry *d)
9307 +{
9308 +       return atomic_read(&au_di(d)->di_generation);
9309 +}
9310 +
9311 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9312 +{
9313 +       hdentry->hd_dentry = NULL;
9314 +}
9315 +
9316 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9317 +                                           aufs_bindex_t bindex)
9318 +{
9319 +       return di->di_hdentry + bindex;
9320 +}
9321 +
9322 +static inline void au_hdput(struct au_hdentry *hd)
9323 +{
9324 +       if (hd)
9325 +               dput(hd->hd_dentry);
9326 +}
9327 +
9328 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9329 +{
9330 +       DiMustAnyLock(dentry);
9331 +       return au_di(dentry)->di_btop;
9332 +}
9333 +
9334 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9335 +{
9336 +       DiMustAnyLock(dentry);
9337 +       return au_di(dentry)->di_bbot;
9338 +}
9339 +
9340 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9341 +{
9342 +       DiMustAnyLock(dentry);
9343 +       return au_di(dentry)->di_bwh;
9344 +}
9345 +
9346 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9347 +{
9348 +       DiMustAnyLock(dentry);
9349 +       return au_di(dentry)->di_bdiropq;
9350 +}
9351 +
9352 +/* todo: hard/soft set? */
9353 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9354 +{
9355 +       DiMustWriteLock(dentry);
9356 +       au_di(dentry)->di_btop = bindex;
9357 +}
9358 +
9359 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9360 +{
9361 +       DiMustWriteLock(dentry);
9362 +       au_di(dentry)->di_bbot = bindex;
9363 +}
9364 +
9365 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9366 +{
9367 +       DiMustWriteLock(dentry);
9368 +       /* dbwh can be outside of btop - bbot range */
9369 +       au_di(dentry)->di_bwh = bindex;
9370 +}
9371 +
9372 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9373 +{
9374 +       DiMustWriteLock(dentry);
9375 +       au_di(dentry)->di_bdiropq = bindex;
9376 +}
9377 +
9378 +/* ---------------------------------------------------------------------- */
9379 +
9380 +#ifdef CONFIG_AUFS_HNOTIFY
9381 +static inline void au_digen_dec(struct dentry *d)
9382 +{
9383 +       atomic_dec(&au_di(d)->di_generation);
9384 +}
9385 +
9386 +static inline void au_hn_di_reinit(struct dentry *dentry)
9387 +{
9388 +       dentry->d_fsdata = NULL;
9389 +}
9390 +#else
9391 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9392 +#endif /* CONFIG_AUFS_HNOTIFY */
9393 +
9394 +#endif /* __KERNEL__ */
9395 +#endif /* __AUFS_DENTRY_H__ */
9396 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9397 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9398 +++ linux/fs/aufs/dinfo.c       2022-12-17 09:21:34.796521861 +0100
9399 @@ -0,0 +1,555 @@
9400 +// SPDX-License-Identifier: GPL-2.0
9401 +/*
9402 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9403 + *
9404 + * This program is free software; you can redistribute it and/or modify
9405 + * it under the terms of the GNU General Public License as published by
9406 + * the Free Software Foundation; either version 2 of the License, or
9407 + * (at your option) any later version.
9408 + *
9409 + * This program is distributed in the hope that it will be useful,
9410 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9411 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9412 + * GNU General Public License for more details.
9413 + *
9414 + * You should have received a copy of the GNU General Public License
9415 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9416 + */
9417 +
9418 +/*
9419 + * dentry private data
9420 + */
9421 +
9422 +#include "aufs.h"
9423 +
9424 +void au_di_init_once(void *_dinfo)
9425 +{
9426 +       struct au_dinfo *dinfo = _dinfo;
9427 +
9428 +       au_rw_init(&dinfo->di_rwsem);
9429 +}
9430 +
9431 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9432 +{
9433 +       struct au_dinfo *dinfo;
9434 +       int nbr, i;
9435 +
9436 +       dinfo = au_cache_alloc_dinfo();
9437 +       if (unlikely(!dinfo))
9438 +               goto out;
9439 +
9440 +       nbr = au_sbbot(sb) + 1;
9441 +       if (nbr <= 0)
9442 +               nbr = 1;
9443 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9444 +       if (dinfo->di_hdentry) {
9445 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9446 +               dinfo->di_btop = -1;
9447 +               dinfo->di_bbot = -1;
9448 +               dinfo->di_bwh = -1;
9449 +               dinfo->di_bdiropq = -1;
9450 +               dinfo->di_tmpfile = 0;
9451 +               for (i = 0; i < nbr; i++)
9452 +                       dinfo->di_hdentry[i].hd_id = -1;
9453 +               dinfo->di_htmpfile = NULL;
9454 +               goto out;
9455 +       }
9456 +
9457 +       au_cache_free_dinfo(dinfo);
9458 +       dinfo = NULL;
9459 +
9460 +out:
9461 +       return dinfo;
9462 +}
9463 +
9464 +void au_di_free(struct au_dinfo *dinfo)
9465 +{
9466 +       struct au_hdentry *p;
9467 +       aufs_bindex_t bbot, bindex;
9468 +
9469 +       /* dentry may not be revalidated */
9470 +       bindex = dinfo->di_btop;
9471 +       if (bindex >= 0) {
9472 +               bbot = dinfo->di_bbot;
9473 +               p = au_hdentry(dinfo, bindex);
9474 +               while (bindex++ <= bbot)
9475 +                       au_hdput(p++);
9476 +       }
9477 +       au_kfree_try_rcu(dinfo->di_hdentry);
9478 +       au_cache_free_dinfo(dinfo);
9479 +}
9480 +
9481 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9482 +{
9483 +       struct au_hdentry *p;
9484 +       aufs_bindex_t bi;
9485 +
9486 +       AuRwMustWriteLock(&a->di_rwsem);
9487 +       AuRwMustWriteLock(&b->di_rwsem);
9488 +
9489 +#define DiSwap(v, name)                                \
9490 +       do {                                    \
9491 +               v = a->di_##name;               \
9492 +               a->di_##name = b->di_##name;    \
9493 +               b->di_##name = v;               \
9494 +       } while (0)
9495 +
9496 +       DiSwap(p, hdentry);
9497 +       DiSwap(bi, btop);
9498 +       DiSwap(bi, bbot);
9499 +       DiSwap(bi, bwh);
9500 +       DiSwap(bi, bdiropq);
9501 +       /* smp_mb(); */
9502 +
9503 +#undef DiSwap
9504 +}
9505 +
9506 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9507 +{
9508 +       AuRwMustWriteLock(&dst->di_rwsem);
9509 +       AuRwMustWriteLock(&src->di_rwsem);
9510 +
9511 +       dst->di_btop = src->di_btop;
9512 +       dst->di_bbot = src->di_bbot;
9513 +       dst->di_bwh = src->di_bwh;
9514 +       dst->di_bdiropq = src->di_bdiropq;
9515 +       /* smp_mb(); */
9516 +}
9517 +
9518 +int au_di_init(struct dentry *dentry)
9519 +{
9520 +       int err;
9521 +       struct super_block *sb;
9522 +       struct au_dinfo *dinfo;
9523 +
9524 +       err = 0;
9525 +       sb = dentry->d_sb;
9526 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9527 +       if (dinfo) {
9528 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9529 +               /* smp_mb(); */ /* atomic_set */
9530 +               dentry->d_fsdata = dinfo;
9531 +       } else
9532 +               err = -ENOMEM;
9533 +
9534 +       return err;
9535 +}
9536 +
9537 +void au_di_fin(struct dentry *dentry)
9538 +{
9539 +       struct au_dinfo *dinfo;
9540 +
9541 +       dinfo = au_di(dentry);
9542 +       AuRwDestroy(&dinfo->di_rwsem);
9543 +       au_di_free(dinfo);
9544 +}
9545 +
9546 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9547 +{
9548 +       int err, sz;
9549 +       struct au_hdentry *hdp;
9550 +
9551 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9552 +
9553 +       err = -ENOMEM;
9554 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9555 +       if (!sz)
9556 +               sz = sizeof(*hdp);
9557 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9558 +                          may_shrink);
9559 +       if (hdp) {
9560 +               dinfo->di_hdentry = hdp;
9561 +               err = 0;
9562 +       }
9563 +
9564 +       return err;
9565 +}
9566 +
9567 +/* ---------------------------------------------------------------------- */
9568 +
9569 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9570 +{
9571 +       switch (lsc) {
9572 +       case AuLsc_DI_CHILD:
9573 +               ii_write_lock_child(inode);
9574 +               break;
9575 +       case AuLsc_DI_CHILD2:
9576 +               ii_write_lock_child2(inode);
9577 +               break;
9578 +       case AuLsc_DI_CHILD3:
9579 +               ii_write_lock_child3(inode);
9580 +               break;
9581 +       case AuLsc_DI_PARENT:
9582 +               ii_write_lock_parent(inode);
9583 +               break;
9584 +       case AuLsc_DI_PARENT2:
9585 +               ii_write_lock_parent2(inode);
9586 +               break;
9587 +       case AuLsc_DI_PARENT3:
9588 +               ii_write_lock_parent3(inode);
9589 +               break;
9590 +       default:
9591 +               BUG();
9592 +       }
9593 +}
9594 +
9595 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9596 +{
9597 +       switch (lsc) {
9598 +       case AuLsc_DI_CHILD:
9599 +               ii_read_lock_child(inode);
9600 +               break;
9601 +       case AuLsc_DI_CHILD2:
9602 +               ii_read_lock_child2(inode);
9603 +               break;
9604 +       case AuLsc_DI_CHILD3:
9605 +               ii_read_lock_child3(inode);
9606 +               break;
9607 +       case AuLsc_DI_PARENT:
9608 +               ii_read_lock_parent(inode);
9609 +               break;
9610 +       case AuLsc_DI_PARENT2:
9611 +               ii_read_lock_parent2(inode);
9612 +               break;
9613 +       case AuLsc_DI_PARENT3:
9614 +               ii_read_lock_parent3(inode);
9615 +               break;
9616 +       default:
9617 +               BUG();
9618 +       }
9619 +}
9620 +
9621 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9622 +{
9623 +       struct inode *inode;
9624 +
9625 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9626 +       if (d_really_is_positive(d)) {
9627 +               inode = d_inode(d);
9628 +               if (au_ftest_lock(flags, IW))
9629 +                       do_ii_write_lock(inode, lsc);
9630 +               else if (au_ftest_lock(flags, IR))
9631 +                       do_ii_read_lock(inode, lsc);
9632 +       }
9633 +}
9634 +
9635 +void di_read_unlock(struct dentry *d, int flags)
9636 +{
9637 +       struct inode *inode;
9638 +
9639 +       if (d_really_is_positive(d)) {
9640 +               inode = d_inode(d);
9641 +               if (au_ftest_lock(flags, IW)) {
9642 +                       au_dbg_verify_dinode(d);
9643 +                       ii_write_unlock(inode);
9644 +               } else if (au_ftest_lock(flags, IR)) {
9645 +                       au_dbg_verify_dinode(d);
9646 +                       ii_read_unlock(inode);
9647 +               }
9648 +       }
9649 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9650 +}
9651 +
9652 +void di_downgrade_lock(struct dentry *d, int flags)
9653 +{
9654 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9655 +               ii_downgrade_lock(d_inode(d));
9656 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9657 +}
9658 +
9659 +void di_write_lock(struct dentry *d, unsigned int lsc)
9660 +{
9661 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9662 +       if (d_really_is_positive(d))
9663 +               do_ii_write_lock(d_inode(d), lsc);
9664 +}
9665 +
9666 +void di_write_unlock(struct dentry *d)
9667 +{
9668 +       au_dbg_verify_dinode(d);
9669 +       if (d_really_is_positive(d))
9670 +               ii_write_unlock(d_inode(d));
9671 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9672 +}
9673 +
9674 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9675 +{
9676 +       AuDebugOn(d1 == d2
9677 +                 || d_inode(d1) == d_inode(d2)
9678 +                 || d1->d_sb != d2->d_sb);
9679 +
9680 +       if ((isdir && au_test_subdir(d1, d2))
9681 +           || d1 < d2) {
9682 +               di_write_lock_child(d1);
9683 +               di_write_lock_child2(d2);
9684 +       } else {
9685 +               di_write_lock_child(d2);
9686 +               di_write_lock_child2(d1);
9687 +       }
9688 +}
9689 +
9690 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9691 +{
9692 +       AuDebugOn(d1 == d2
9693 +                 || d_inode(d1) == d_inode(d2)
9694 +                 || d1->d_sb != d2->d_sb);
9695 +
9696 +       if ((isdir && au_test_subdir(d1, d2))
9697 +           || d1 < d2) {
9698 +               di_write_lock_parent(d1);
9699 +               di_write_lock_parent2(d2);
9700 +       } else {
9701 +               di_write_lock_parent(d2);
9702 +               di_write_lock_parent2(d1);
9703 +       }
9704 +}
9705 +
9706 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9707 +{
9708 +       di_write_unlock(d1);
9709 +       if (d_inode(d1) == d_inode(d2))
9710 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9711 +       else
9712 +               di_write_unlock(d2);
9713 +}
9714 +
9715 +/* ---------------------------------------------------------------------- */
9716 +
9717 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9718 +{
9719 +       struct dentry *d;
9720 +
9721 +       DiMustAnyLock(dentry);
9722 +
9723 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9724 +               return NULL;
9725 +       AuDebugOn(bindex < 0);
9726 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9727 +       AuDebugOn(d && au_dcount(d) <= 0);
9728 +       return d;
9729 +}
9730 +
9731 +/*
9732 + * extended version of au_h_dptr().
9733 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9734 + * error.
9735 + */
9736 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9737 +{
9738 +       struct dentry *h_dentry;
9739 +       struct inode *inode, *h_inode;
9740 +
9741 +       AuDebugOn(d_really_is_negative(dentry));
9742 +
9743 +       h_dentry = NULL;
9744 +       if (au_dbtop(dentry) <= bindex
9745 +           && bindex <= au_dbbot(dentry))
9746 +               h_dentry = au_h_dptr(dentry, bindex);
9747 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9748 +               dget(h_dentry);
9749 +               goto out; /* success */
9750 +       }
9751 +
9752 +       inode = d_inode(dentry);
9753 +       AuDebugOn(bindex < au_ibtop(inode));
9754 +       AuDebugOn(au_ibbot(inode) < bindex);
9755 +       h_inode = au_h_iptr(inode, bindex);
9756 +       h_dentry = d_find_alias(h_inode);
9757 +       if (h_dentry) {
9758 +               if (!IS_ERR(h_dentry)) {
9759 +                       if (!au_d_linkable(h_dentry))
9760 +                               goto out; /* success */
9761 +                       dput(h_dentry);
9762 +               } else
9763 +                       goto out;
9764 +       }
9765 +
9766 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9767 +               h_dentry = au_plink_lkup(inode, bindex);
9768 +               AuDebugOn(!h_dentry);
9769 +               if (!IS_ERR(h_dentry)) {
9770 +                       if (!au_d_hashed_positive(h_dentry))
9771 +                               goto out; /* success */
9772 +                       dput(h_dentry);
9773 +                       h_dentry = NULL;
9774 +               }
9775 +       }
9776 +
9777 +out:
9778 +       AuDbgDentry(h_dentry);
9779 +       return h_dentry;
9780 +}
9781 +
9782 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9783 +{
9784 +       aufs_bindex_t bbot, bwh;
9785 +
9786 +       bbot = au_dbbot(dentry);
9787 +       if (0 <= bbot) {
9788 +               bwh = au_dbwh(dentry);
9789 +               if (!bwh)
9790 +                       return bwh;
9791 +               if (0 < bwh && bwh < bbot)
9792 +                       return bwh - 1;
9793 +       }
9794 +       return bbot;
9795 +}
9796 +
9797 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9798 +{
9799 +       aufs_bindex_t bbot, bopq;
9800 +
9801 +       bbot = au_dbtail(dentry);
9802 +       if (0 <= bbot) {
9803 +               bopq = au_dbdiropq(dentry);
9804 +               if (0 <= bopq && bopq < bbot)
9805 +                       bbot = bopq;
9806 +       }
9807 +       return bbot;
9808 +}
9809 +
9810 +/* ---------------------------------------------------------------------- */
9811 +
9812 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9813 +                  struct dentry *h_dentry)
9814 +{
9815 +       struct au_dinfo *dinfo;
9816 +       struct au_hdentry *hd;
9817 +       struct au_branch *br;
9818 +
9819 +       DiMustWriteLock(dentry);
9820 +
9821 +       dinfo = au_di(dentry);
9822 +       hd = au_hdentry(dinfo, bindex);
9823 +       au_hdput(hd);
9824 +       hd->hd_dentry = h_dentry;
9825 +       if (h_dentry) {
9826 +               br = au_sbr(dentry->d_sb, bindex);
9827 +               hd->hd_id = br->br_id;
9828 +       }
9829 +}
9830 +
9831 +int au_dbrange_test(struct dentry *dentry)
9832 +{
9833 +       int err;
9834 +       aufs_bindex_t btop, bbot;
9835 +
9836 +       err = 0;
9837 +       btop = au_dbtop(dentry);
9838 +       bbot = au_dbbot(dentry);
9839 +       if (btop >= 0)
9840 +               AuDebugOn(bbot < 0 && btop > bbot);
9841 +       else {
9842 +               err = -EIO;
9843 +               AuDebugOn(bbot >= 0);
9844 +       }
9845 +
9846 +       return err;
9847 +}
9848 +
9849 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9850 +{
9851 +       int err;
9852 +
9853 +       err = 0;
9854 +       if (unlikely(au_digen(dentry) != sigen
9855 +                    || au_iigen_test(d_inode(dentry), sigen)))
9856 +               err = -EIO;
9857 +
9858 +       return err;
9859 +}
9860 +
9861 +void au_update_digen(struct dentry *dentry)
9862 +{
9863 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9864 +       /* smp_mb(); */ /* atomic_set */
9865 +}
9866 +
9867 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9868 +{
9869 +       struct au_dinfo *dinfo;
9870 +       struct dentry *h_d;
9871 +       struct au_hdentry *hdp;
9872 +       aufs_bindex_t bindex, bbot;
9873 +
9874 +       DiMustWriteLock(dentry);
9875 +
9876 +       dinfo = au_di(dentry);
9877 +       if (!dinfo || dinfo->di_btop < 0)
9878 +               return;
9879 +
9880 +       if (do_put_zero) {
9881 +               bbot = dinfo->di_bbot;
9882 +               bindex = dinfo->di_btop;
9883 +               hdp = au_hdentry(dinfo, bindex);
9884 +               for (; bindex <= bbot; bindex++, hdp++) {
9885 +                       h_d = hdp->hd_dentry;
9886 +                       if (h_d && d_is_negative(h_d))
9887 +                               au_set_h_dptr(dentry, bindex, NULL);
9888 +               }
9889 +       }
9890 +
9891 +       dinfo->di_btop = 0;
9892 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9893 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9894 +               if (hdp->hd_dentry)
9895 +                       break;
9896 +       if (dinfo->di_btop > dinfo->di_bbot) {
9897 +               dinfo->di_btop = -1;
9898 +               dinfo->di_bbot = -1;
9899 +               return;
9900 +       }
9901 +
9902 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9903 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9904 +               if (hdp->hd_dentry)
9905 +                       break;
9906 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9907 +}
9908 +
9909 +void au_update_dbtop(struct dentry *dentry)
9910 +{
9911 +       aufs_bindex_t bindex, bbot;
9912 +       struct dentry *h_dentry;
9913 +
9914 +       bbot = au_dbbot(dentry);
9915 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9916 +               h_dentry = au_h_dptr(dentry, bindex);
9917 +               if (!h_dentry)
9918 +                       continue;
9919 +               if (d_is_positive(h_dentry)) {
9920 +                       au_set_dbtop(dentry, bindex);
9921 +                       return;
9922 +               }
9923 +               au_set_h_dptr(dentry, bindex, NULL);
9924 +       }
9925 +}
9926 +
9927 +void au_update_dbbot(struct dentry *dentry)
9928 +{
9929 +       aufs_bindex_t bindex, btop;
9930 +       struct dentry *h_dentry;
9931 +
9932 +       btop = au_dbtop(dentry);
9933 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9934 +               h_dentry = au_h_dptr(dentry, bindex);
9935 +               if (!h_dentry)
9936 +                       continue;
9937 +               if (d_is_positive(h_dentry)) {
9938 +                       au_set_dbbot(dentry, bindex);
9939 +                       return;
9940 +               }
9941 +               au_set_h_dptr(dentry, bindex, NULL);
9942 +       }
9943 +}
9944 +
9945 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9946 +{
9947 +       aufs_bindex_t bindex, bbot;
9948 +
9949 +       bbot = au_dbbot(dentry);
9950 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9951 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9952 +                       return bindex;
9953 +       return -1;
9954 +}
9955 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9956 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9957 +++ linux/fs/aufs/dir.c 2023-10-31 09:31:04.196547417 +0100
9958 @@ -0,0 +1,765 @@
9959 +// SPDX-License-Identifier: GPL-2.0
9960 +/*
9961 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9962 + *
9963 + * This program is free software; you can redistribute it and/or modify
9964 + * it under the terms of the GNU General Public License as published by
9965 + * the Free Software Foundation; either version 2 of the License, or
9966 + * (at your option) any later version.
9967 + *
9968 + * This program is distributed in the hope that it will be useful,
9969 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9970 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9971 + * GNU General Public License for more details.
9972 + *
9973 + * You should have received a copy of the GNU General Public License
9974 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9975 + */
9976 +
9977 +/*
9978 + * directory operations
9979 + */
9980 +
9981 +#include <linux/fs_stack.h>
9982 +#include <linux/iversion.h>
9983 +#include "aufs.h"
9984 +
9985 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9986 +{
9987 +       unsigned int nlink;
9988 +
9989 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9990 +
9991 +       nlink = dir->i_nlink;
9992 +       nlink += h_dir->i_nlink - 2;
9993 +       if (h_dir->i_nlink < 2)
9994 +               nlink += 2;
9995 +       smp_mb(); /* for i_nlink */
9996 +       /* 0 can happen in revaliding */
9997 +       set_nlink(dir, nlink);
9998 +}
9999 +
10000 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10001 +{
10002 +       unsigned int nlink;
10003 +
10004 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10005 +
10006 +       nlink = dir->i_nlink;
10007 +       nlink -= h_dir->i_nlink - 2;
10008 +       if (h_dir->i_nlink < 2)
10009 +               nlink -= 2;
10010 +       smp_mb(); /* for i_nlink */
10011 +       /* nlink == 0 means the branch-fs is broken */
10012 +       set_nlink(dir, nlink);
10013 +}
10014 +
10015 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10016 +{
10017 +       loff_t sz;
10018 +       aufs_bindex_t bindex, bbot;
10019 +       struct file *h_file;
10020 +       struct dentry *h_dentry;
10021 +
10022 +       sz = 0;
10023 +       if (file) {
10024 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10025 +
10026 +               bbot = au_fbbot_dir(file);
10027 +               for (bindex = au_fbtop(file);
10028 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10029 +                    bindex++) {
10030 +                       h_file = au_hf_dir(file, bindex);
10031 +                       if (h_file && file_inode(h_file))
10032 +                               sz += vfsub_f_size_read(h_file);
10033 +               }
10034 +       } else {
10035 +               AuDebugOn(!dentry);
10036 +               AuDebugOn(!d_is_dir(dentry));
10037 +
10038 +               bbot = au_dbtaildir(dentry);
10039 +               for (bindex = au_dbtop(dentry);
10040 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10041 +                    bindex++) {
10042 +                       h_dentry = au_h_dptr(dentry, bindex);
10043 +                       if (h_dentry && d_is_positive(h_dentry))
10044 +                               sz += i_size_read(d_inode(h_dentry));
10045 +               }
10046 +       }
10047 +       if (sz < KMALLOC_MAX_SIZE)
10048 +               sz = roundup_pow_of_two(sz);
10049 +       if (sz > KMALLOC_MAX_SIZE)
10050 +               sz = KMALLOC_MAX_SIZE;
10051 +       else if (sz < NAME_MAX) {
10052 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10053 +               sz = AUFS_RDBLK_DEF;
10054 +       }
10055 +       return sz;
10056 +}
10057 +
10058 +struct au_dir_ts_arg {
10059 +       struct dentry *dentry;
10060 +       aufs_bindex_t brid;
10061 +};
10062 +
10063 +static void au_do_dir_ts(void *arg)
10064 +{
10065 +       struct au_dir_ts_arg *a = arg;
10066 +       struct au_dtime dt;
10067 +       struct path h_path;
10068 +       struct inode *dir, *h_dir;
10069 +       struct super_block *sb;
10070 +       struct au_branch *br;
10071 +       struct au_hinode *hdir;
10072 +       int err;
10073 +       aufs_bindex_t btop, bindex;
10074 +
10075 +       sb = a->dentry->d_sb;
10076 +       if (d_really_is_negative(a->dentry))
10077 +               goto out;
10078 +       /* no dir->i_mutex lock */
10079 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10080 +
10081 +       dir = d_inode(a->dentry);
10082 +       btop = au_ibtop(dir);
10083 +       bindex = au_br_index(sb, a->brid);
10084 +       if (bindex < btop)
10085 +               goto out_unlock;
10086 +
10087 +       br = au_sbr(sb, bindex);
10088 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10089 +       if (!h_path.dentry)
10090 +               goto out_unlock;
10091 +       h_path.mnt = au_br_mnt(br);
10092 +       au_dtime_store(&dt, a->dentry, &h_path);
10093 +
10094 +       br = au_sbr(sb, btop);
10095 +       if (!au_br_writable(br->br_perm))
10096 +               goto out_unlock;
10097 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10098 +       h_path.mnt = au_br_mnt(br);
10099 +       err = vfsub_mnt_want_write(h_path.mnt);
10100 +       if (err)
10101 +               goto out_unlock;
10102 +       hdir = au_hi(dir, btop);
10103 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10104 +       h_dir = au_h_iptr(dir, btop);
10105 +       if (h_dir->i_nlink
10106 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10107 +               dt.dt_h_path = h_path;
10108 +               au_dtime_revert(&dt);
10109 +       }
10110 +       au_hn_inode_unlock(hdir);
10111 +       vfsub_mnt_drop_write(h_path.mnt);
10112 +       au_cpup_attr_timesizes(dir);
10113 +
10114 +out_unlock:
10115 +       aufs_read_unlock(a->dentry, AuLock_DW);
10116 +out:
10117 +       dput(a->dentry);
10118 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10119 +       au_kfree_try_rcu(arg);
10120 +}
10121 +
10122 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10123 +{
10124 +       int perm, wkq_err;
10125 +       aufs_bindex_t btop;
10126 +       struct au_dir_ts_arg *arg;
10127 +       struct dentry *dentry;
10128 +       struct super_block *sb;
10129 +
10130 +       IMustLock(dir);
10131 +
10132 +       dentry = d_find_any_alias(dir);
10133 +       AuDebugOn(!dentry);
10134 +       sb = dentry->d_sb;
10135 +       btop = au_ibtop(dir);
10136 +       if (btop == bindex) {
10137 +               au_cpup_attr_timesizes(dir);
10138 +               goto out;
10139 +       }
10140 +
10141 +       perm = au_sbr_perm(sb, btop);
10142 +       if (!au_br_writable(perm))
10143 +               goto out;
10144 +
10145 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10146 +       if (!arg)
10147 +               goto out;
10148 +
10149 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10150 +       arg->brid = au_sbr_id(sb, bindex);
10151 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10152 +       if (unlikely(wkq_err)) {
10153 +               pr_err("wkq %d\n", wkq_err);
10154 +               dput(dentry);
10155 +               au_kfree_try_rcu(arg);
10156 +       }
10157 +
10158 +out:
10159 +       dput(dentry);
10160 +}
10161 +
10162 +/* ---------------------------------------------------------------------- */
10163 +
10164 +static int reopen_dir(struct file *file)
10165 +{
10166 +       int err;
10167 +       unsigned int flags;
10168 +       aufs_bindex_t bindex, btail, btop;
10169 +       struct dentry *dentry, *h_dentry;
10170 +       struct file *h_file;
10171 +
10172 +       /* open all lower dirs */
10173 +       dentry = file->f_path.dentry;
10174 +       btop = au_dbtop(dentry);
10175 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10176 +               au_set_h_fptr(file, bindex, NULL);
10177 +       au_set_fbtop(file, btop);
10178 +
10179 +       btail = au_dbtaildir(dentry);
10180 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10181 +               au_set_h_fptr(file, bindex, NULL);
10182 +       au_set_fbbot_dir(file, btail);
10183 +
10184 +       flags = vfsub_file_flags(file);
10185 +       for (bindex = btop; bindex <= btail; bindex++) {
10186 +               h_dentry = au_h_dptr(dentry, bindex);
10187 +               if (!h_dentry)
10188 +                       continue;
10189 +               h_file = au_hf_dir(file, bindex);
10190 +               if (h_file)
10191 +                       continue;
10192 +
10193 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10194 +               err = PTR_ERR(h_file);
10195 +               if (IS_ERR(h_file))
10196 +                       goto out; /* close all? */
10197 +               au_set_h_fptr(file, bindex, h_file);
10198 +       }
10199 +       au_update_figen(file);
10200 +       /* todo: necessary? */
10201 +       /* file->f_ra = h_file->f_ra; */
10202 +       err = 0;
10203 +
10204 +out:
10205 +       return err;
10206 +}
10207 +
10208 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10209 +{
10210 +       int err;
10211 +       aufs_bindex_t bindex, btail;
10212 +       struct dentry *dentry, *h_dentry;
10213 +       struct vfsmount *mnt;
10214 +
10215 +       FiMustWriteLock(file);
10216 +       AuDebugOn(h_file);
10217 +
10218 +       err = 0;
10219 +       mnt = file->f_path.mnt;
10220 +       dentry = file->f_path.dentry;
10221 +       file->f_version = inode_query_iversion(d_inode(dentry));
10222 +       bindex = au_dbtop(dentry);
10223 +       au_set_fbtop(file, bindex);
10224 +       btail = au_dbtaildir(dentry);
10225 +       au_set_fbbot_dir(file, btail);
10226 +       for (; !err && bindex <= btail; bindex++) {
10227 +               h_dentry = au_h_dptr(dentry, bindex);
10228 +               if (!h_dentry)
10229 +                       continue;
10230 +
10231 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10232 +               if (unlikely(err))
10233 +                       break;
10234 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10235 +               if (IS_ERR(h_file)) {
10236 +                       err = PTR_ERR(h_file);
10237 +                       break;
10238 +               }
10239 +               au_set_h_fptr(file, bindex, h_file);
10240 +       }
10241 +       au_update_figen(file);
10242 +       /* todo: necessary? */
10243 +       /* file->f_ra = h_file->f_ra; */
10244 +       if (!err)
10245 +               return 0; /* success */
10246 +
10247 +       /* close all */
10248 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10249 +               au_set_h_fptr(file, bindex, NULL);
10250 +       au_set_fbtop(file, -1);
10251 +       au_set_fbbot_dir(file, -1);
10252 +
10253 +       return err;
10254 +}
10255 +
10256 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10257 +                        struct file *file)
10258 +{
10259 +       int err;
10260 +       struct super_block *sb;
10261 +       struct au_fidir *fidir;
10262 +
10263 +       err = -ENOMEM;
10264 +       sb = file->f_path.dentry->d_sb;
10265 +       si_read_lock(sb, AuLock_FLUSH);
10266 +       fidir = au_fidir_alloc(sb);
10267 +       if (fidir) {
10268 +               struct au_do_open_args args = {
10269 +                       .open   = do_open_dir,
10270 +                       .fidir  = fidir
10271 +               };
10272 +               err = au_do_open(file, &args);
10273 +               if (unlikely(err))
10274 +                       au_kfree_rcu(fidir);
10275 +       }
10276 +       si_read_unlock(sb);
10277 +       return err;
10278 +}
10279 +
10280 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10281 +                           struct file *file)
10282 +{
10283 +       struct au_vdir *vdir_cache;
10284 +       struct au_finfo *finfo;
10285 +       struct au_fidir *fidir;
10286 +       struct au_hfile *hf;
10287 +       aufs_bindex_t bindex, bbot;
10288 +
10289 +       finfo = au_fi(file);
10290 +       fidir = finfo->fi_hdir;
10291 +       if (fidir) {
10292 +               au_hbl_del(&finfo->fi_hlist,
10293 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10294 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10295 +               if (vdir_cache)
10296 +                       au_vdir_free(vdir_cache);
10297 +
10298 +               bindex = finfo->fi_btop;
10299 +               if (bindex >= 0) {
10300 +                       hf = fidir->fd_hfile + bindex;
10301 +                       /*
10302 +                        * calls fput() instead of filp_close(),
10303 +                        * since no dnotify or lock for the lower file.
10304 +                        */
10305 +                       bbot = fidir->fd_bbot;
10306 +                       for (; bindex <= bbot; bindex++, hf++)
10307 +                               if (hf->hf_file)
10308 +                                       au_hfput(hf, /*execed*/0);
10309 +               }
10310 +               au_kfree_rcu(fidir);
10311 +               finfo->fi_hdir = NULL;
10312 +       }
10313 +       au_finfo_fin(file);
10314 +       return 0;
10315 +}
10316 +
10317 +/* ---------------------------------------------------------------------- */
10318 +
10319 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10320 +{
10321 +       int err;
10322 +       aufs_bindex_t bindex, bbot;
10323 +       struct file *h_file;
10324 +
10325 +       err = 0;
10326 +       bbot = au_fbbot_dir(file);
10327 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10328 +               h_file = au_hf_dir(file, bindex);
10329 +               if (h_file)
10330 +                       err = vfsub_flush(h_file, id);
10331 +       }
10332 +       return err;
10333 +}
10334 +
10335 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10336 +{
10337 +       return au_do_flush(file, id, au_do_flush_dir);
10338 +}
10339 +
10340 +/* ---------------------------------------------------------------------- */
10341 +
10342 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10343 +{
10344 +       int err;
10345 +       aufs_bindex_t bbot, bindex;
10346 +       struct inode *inode;
10347 +       struct super_block *sb;
10348 +
10349 +       err = 0;
10350 +       sb = dentry->d_sb;
10351 +       inode = d_inode(dentry);
10352 +       IMustLock(inode);
10353 +       bbot = au_dbbot(dentry);
10354 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10355 +               struct path h_path;
10356 +
10357 +               if (au_test_ro(sb, bindex, inode))
10358 +                       continue;
10359 +               h_path.dentry = au_h_dptr(dentry, bindex);
10360 +               if (!h_path.dentry)
10361 +                       continue;
10362 +
10363 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10364 +               err = vfsub_fsync(NULL, &h_path, datasync);
10365 +       }
10366 +
10367 +       return err;
10368 +}
10369 +
10370 +static int au_do_fsync_dir(struct file *file, int datasync)
10371 +{
10372 +       int err;
10373 +       aufs_bindex_t bbot, bindex;
10374 +       struct file *h_file;
10375 +       struct super_block *sb;
10376 +       struct inode *inode;
10377 +
10378 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10379 +       if (unlikely(err))
10380 +               goto out;
10381 +
10382 +       inode = file_inode(file);
10383 +       sb = inode->i_sb;
10384 +       bbot = au_fbbot_dir(file);
10385 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10386 +               h_file = au_hf_dir(file, bindex);
10387 +               if (!h_file || au_test_ro(sb, bindex, inode))
10388 +                       continue;
10389 +
10390 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10391 +       }
10392 +
10393 +out:
10394 +       return err;
10395 +}
10396 +
10397 +/*
10398 + * @file may be NULL
10399 + */
10400 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10401 +                         int datasync)
10402 +{
10403 +       int err;
10404 +       struct dentry *dentry;
10405 +       struct inode *inode;
10406 +       struct super_block *sb;
10407 +
10408 +       err = 0;
10409 +       dentry = file->f_path.dentry;
10410 +       inode = d_inode(dentry);
10411 +       inode_lock(inode);
10412 +       sb = dentry->d_sb;
10413 +       si_noflush_read_lock(sb);
10414 +       if (file)
10415 +               err = au_do_fsync_dir(file, datasync);
10416 +       else {
10417 +               di_write_lock_child(dentry);
10418 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10419 +       }
10420 +       au_cpup_attr_timesizes(inode);
10421 +       di_write_unlock(dentry);
10422 +       if (file)
10423 +               fi_write_unlock(file);
10424 +
10425 +       si_read_unlock(sb);
10426 +       inode_unlock(inode);
10427 +       return err;
10428 +}
10429 +
10430 +/* ---------------------------------------------------------------------- */
10431 +
10432 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10433 +{
10434 +       int err;
10435 +       struct dentry *dentry;
10436 +       struct inode *inode, *h_inode;
10437 +       struct super_block *sb;
10438 +
10439 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10440 +
10441 +       dentry = file->f_path.dentry;
10442 +       inode = d_inode(dentry);
10443 +       IMustLock(inode);
10444 +
10445 +       sb = dentry->d_sb;
10446 +       si_read_lock(sb, AuLock_FLUSH);
10447 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10448 +       if (unlikely(err))
10449 +               goto out;
10450 +       err = au_alive_dir(dentry);
10451 +       if (!err)
10452 +               err = au_vdir_init(file);
10453 +       di_downgrade_lock(dentry, AuLock_IR);
10454 +       if (unlikely(err))
10455 +               goto out_unlock;
10456 +
10457 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10458 +       if (!au_test_nfsd()) {
10459 +               err = au_vdir_fill_de(file, ctx);
10460 +               fsstack_copy_attr_atime(inode, h_inode);
10461 +       } else {
10462 +               /*
10463 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10464 +                * encode_fh() and others.
10465 +                */
10466 +               atomic_inc(&h_inode->i_count);
10467 +               di_read_unlock(dentry, AuLock_IR);
10468 +               si_read_unlock(sb);
10469 +               err = au_vdir_fill_de(file, ctx);
10470 +               fsstack_copy_attr_atime(inode, h_inode);
10471 +               fi_write_unlock(file);
10472 +               iput(h_inode);
10473 +
10474 +               AuTraceErr(err);
10475 +               return err;
10476 +       }
10477 +
10478 +out_unlock:
10479 +       di_read_unlock(dentry, AuLock_IR);
10480 +       fi_write_unlock(file);
10481 +out:
10482 +       si_read_unlock(sb);
10483 +       return err;
10484 +}
10485 +
10486 +/* ---------------------------------------------------------------------- */
10487 +
10488 +#define AuTestEmpty_WHONLY     1
10489 +#define AuTestEmpty_CALLED     (1 << 1)
10490 +#define AuTestEmpty_SHWH       (1 << 2)
10491 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10492 +#define au_fset_testempty(flags, name) \
10493 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10494 +#define au_fclr_testempty(flags, name) \
10495 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10496 +
10497 +#ifndef CONFIG_AUFS_SHWH
10498 +#undef AuTestEmpty_SHWH
10499 +#define AuTestEmpty_SHWH       0
10500 +#endif
10501 +
10502 +struct test_empty_arg {
10503 +       struct dir_context ctx;
10504 +       struct au_nhash *whlist;
10505 +       unsigned int flags;
10506 +       int err;
10507 +       aufs_bindex_t bindex;
10508 +};
10509 +
10510 +static bool test_empty_cb(struct dir_context *ctx, const char *__name,
10511 +                         int namelen, loff_t offset __maybe_unused, u64 ino,
10512 +                         unsigned int d_type)
10513 +{
10514 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10515 +                                                 ctx);
10516 +       char *name = (void *)__name;
10517 +
10518 +       arg->err = 0;
10519 +       au_fset_testempty(arg->flags, CALLED);
10520 +       /* smp_mb(); */
10521 +       if (name[0] == '.'
10522 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10523 +               goto out; /* success */
10524 +
10525 +       if (namelen <= AUFS_WH_PFX_LEN
10526 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10527 +               if (au_ftest_testempty(arg->flags, WHONLY)
10528 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10529 +                       arg->err = -ENOTEMPTY;
10530 +               goto out;
10531 +       }
10532 +
10533 +       name += AUFS_WH_PFX_LEN;
10534 +       namelen -= AUFS_WH_PFX_LEN;
10535 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10536 +               arg->err = au_nhash_append_wh
10537 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10538 +                        au_ftest_testempty(arg->flags, SHWH));
10539 +
10540 +out:
10541 +       /* smp_mb(); */
10542 +       AuTraceErr(arg->err);
10543 +       return !arg->err;
10544 +}
10545 +
10546 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10547 +{
10548 +       int err;
10549 +       struct file *h_file;
10550 +       struct au_branch *br;
10551 +
10552 +       h_file = au_h_open(dentry, arg->bindex,
10553 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10554 +                          /*file*/NULL, /*force_wr*/0);
10555 +       err = PTR_ERR(h_file);
10556 +       if (IS_ERR(h_file))
10557 +               goto out;
10558 +
10559 +       err = 0;
10560 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10561 +           && !file_inode(h_file)->i_nlink)
10562 +               goto out_put;
10563 +
10564 +       do {
10565 +               arg->err = 0;
10566 +               au_fclr_testempty(arg->flags, CALLED);
10567 +               /* smp_mb(); */
10568 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10569 +               if (err >= 0)
10570 +                       err = arg->err;
10571 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10572 +
10573 +out_put:
10574 +       fput(h_file);
10575 +       br = au_sbr(dentry->d_sb, arg->bindex);
10576 +       au_lcnt_dec(&br->br_nfiles);
10577 +out:
10578 +       return err;
10579 +}
10580 +
10581 +struct do_test_empty_args {
10582 +       int *errp;
10583 +       struct dentry *dentry;
10584 +       struct test_empty_arg *arg;
10585 +};
10586 +
10587 +static void call_do_test_empty(void *args)
10588 +{
10589 +       struct do_test_empty_args *a = args;
10590 +       *a->errp = do_test_empty(a->dentry, a->arg);
10591 +}
10592 +
10593 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10594 +{
10595 +       int err, wkq_err;
10596 +       struct dentry *h_dentry;
10597 +       struct inode *h_inode;
10598 +       struct mnt_idmap *h_idmap;
10599 +
10600 +       h_idmap = au_sbr_idmap(dentry->d_sb, arg->bindex);
10601 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10602 +       h_inode = d_inode(h_dentry);
10603 +       /* todo: i_mode changes anytime? */
10604 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10605 +       err = au_test_h_perm_sio(h_idmap, h_inode, MAY_EXEC | MAY_READ);
10606 +       inode_unlock_shared(h_inode);
10607 +       if (!err)
10608 +               err = do_test_empty(dentry, arg);
10609 +       else {
10610 +               struct do_test_empty_args args = {
10611 +                       .errp   = &err,
10612 +                       .dentry = dentry,
10613 +                       .arg    = arg
10614 +               };
10615 +               unsigned int flags = arg->flags;
10616 +
10617 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10618 +               if (unlikely(wkq_err))
10619 +                       err = wkq_err;
10620 +               arg->flags = flags;
10621 +       }
10622 +
10623 +       return err;
10624 +}
10625 +
10626 +int au_test_empty_lower(struct dentry *dentry)
10627 +{
10628 +       int err;
10629 +       unsigned int rdhash;
10630 +       aufs_bindex_t bindex, btop, btail;
10631 +       struct au_nhash whlist;
10632 +       struct test_empty_arg arg = {
10633 +               .ctx = {
10634 +                       .actor = test_empty_cb
10635 +               }
10636 +       };
10637 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10638 +
10639 +       SiMustAnyLock(dentry->d_sb);
10640 +
10641 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10642 +       if (!rdhash)
10643 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10644 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10645 +       if (unlikely(err))
10646 +               goto out;
10647 +
10648 +       arg.flags = 0;
10649 +       arg.whlist = &whlist;
10650 +       btop = au_dbtop(dentry);
10651 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10652 +               au_fset_testempty(arg.flags, SHWH);
10653 +       test_empty = do_test_empty;
10654 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10655 +               test_empty = sio_test_empty;
10656 +       arg.bindex = btop;
10657 +       err = test_empty(dentry, &arg);
10658 +       if (unlikely(err))
10659 +               goto out_whlist;
10660 +
10661 +       au_fset_testempty(arg.flags, WHONLY);
10662 +       btail = au_dbtaildir(dentry);
10663 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10664 +               struct dentry *h_dentry;
10665 +
10666 +               h_dentry = au_h_dptr(dentry, bindex);
10667 +               if (h_dentry && d_is_positive(h_dentry)) {
10668 +                       arg.bindex = bindex;
10669 +                       err = test_empty(dentry, &arg);
10670 +               }
10671 +       }
10672 +
10673 +out_whlist:
10674 +       au_nhash_wh_free(&whlist);
10675 +out:
10676 +       return err;
10677 +}
10678 +
10679 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10680 +{
10681 +       int err;
10682 +       struct test_empty_arg arg = {
10683 +               .ctx = {
10684 +                       .actor = test_empty_cb
10685 +               }
10686 +       };
10687 +       aufs_bindex_t bindex, btail;
10688 +
10689 +       err = 0;
10690 +       arg.whlist = whlist;
10691 +       arg.flags = AuTestEmpty_WHONLY;
10692 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10693 +               au_fset_testempty(arg.flags, SHWH);
10694 +       btail = au_dbtaildir(dentry);
10695 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10696 +               struct dentry *h_dentry;
10697 +
10698 +               h_dentry = au_h_dptr(dentry, bindex);
10699 +               if (h_dentry && d_is_positive(h_dentry)) {
10700 +                       arg.bindex = bindex;
10701 +                       err = sio_test_empty(dentry, &arg);
10702 +               }
10703 +       }
10704 +
10705 +       return err;
10706 +}
10707 +
10708 +/* ---------------------------------------------------------------------- */
10709 +
10710 +const struct file_operations aufs_dir_fop = {
10711 +       .owner          = THIS_MODULE,
10712 +       .llseek         = default_llseek,
10713 +       .read           = generic_read_dir,
10714 +       .iterate_shared = aufs_iterate_shared,
10715 +       .unlocked_ioctl = aufs_ioctl_dir,
10716 +#ifdef CONFIG_COMPAT
10717 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10718 +#endif
10719 +       .open           = aufs_open_dir,
10720 +       .release        = aufs_release_dir,
10721 +       .flush          = aufs_flush_dir,
10722 +       .fsync          = aufs_fsync_dir
10723 +};
10724 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10725 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10726 +++ linux/fs/aufs/dir.h 2022-11-05 23:02:18.962555950 +0100
10727 @@ -0,0 +1,134 @@
10728 +/* SPDX-License-Identifier: GPL-2.0 */
10729 +/*
10730 + * Copyright (C) 2005-2022 Junjiro R. Okajima
10731 + *
10732 + * This program is free software; you can redistribute it and/or modify
10733 + * it under the terms of the GNU General Public License as published by
10734 + * the Free Software Foundation; either version 2 of the License, or
10735 + * (at your option) any later version.
10736 + *
10737 + * This program is distributed in the hope that it will be useful,
10738 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10739 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10740 + * GNU General Public License for more details.
10741 + *
10742 + * You should have received a copy of the GNU General Public License
10743 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10744 + */
10745 +
10746 +/*
10747 + * directory operations
10748 + */
10749 +
10750 +#ifndef __AUFS_DIR_H__
10751 +#define __AUFS_DIR_H__
10752 +
10753 +#ifdef __KERNEL__
10754 +
10755 +#include <linux/fs.h>
10756 +
10757 +/* ---------------------------------------------------------------------- */
10758 +
10759 +/* need to be faster and smaller */
10760 +
10761 +struct au_nhash {
10762 +       unsigned int            nh_num;
10763 +       struct hlist_head       *nh_head;
10764 +};
10765 +
10766 +struct au_vdir_destr {
10767 +       unsigned char   len;
10768 +       unsigned char   name[];
10769 +} __packed;
10770 +
10771 +struct au_vdir_dehstr {
10772 +       struct hlist_node       hash;
10773 +       struct au_vdir_destr    *str;
10774 +       struct rcu_head         rcu;
10775 +} ____cacheline_aligned_in_smp;
10776 +
10777 +struct au_vdir_de {
10778 +       ino_t                   de_ino;
10779 +       unsigned char           de_type;
10780 +       /* caution: packed */
10781 +       struct au_vdir_destr    de_str;
10782 +} __packed;
10783 +
10784 +struct au_vdir_wh {
10785 +       struct hlist_node       wh_hash;
10786 +#ifdef CONFIG_AUFS_SHWH
10787 +       ino_t                   wh_ino;
10788 +       aufs_bindex_t           wh_bindex;
10789 +       unsigned char           wh_type;
10790 +#else
10791 +       aufs_bindex_t           wh_bindex;
10792 +#endif
10793 +       /* caution: packed */
10794 +       struct au_vdir_destr    wh_str;
10795 +} __packed;
10796 +
10797 +union au_vdir_deblk_p {
10798 +       unsigned char           *deblk;
10799 +       struct au_vdir_de       *de;
10800 +};
10801 +
10802 +struct au_vdir {
10803 +       unsigned char   **vd_deblk;
10804 +       unsigned long   vd_nblk;
10805 +       struct {
10806 +               unsigned long           ul;
10807 +               union au_vdir_deblk_p   p;
10808 +       } vd_last;
10809 +
10810 +       u64             vd_version;
10811 +       unsigned int    vd_deblk_sz;
10812 +       unsigned long   vd_jiffy;
10813 +       struct rcu_head rcu;
10814 +} ____cacheline_aligned_in_smp;
10815 +
10816 +/* ---------------------------------------------------------------------- */
10817 +
10818 +/* dir.c */
10819 +extern const struct file_operations aufs_dir_fop;
10820 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10821 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10822 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10823 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10824 +int au_test_empty_lower(struct dentry *dentry);
10825 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10826 +
10827 +/* vdir.c */
10828 +unsigned int au_rdhash_est(loff_t sz);
10829 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10830 +void au_nhash_wh_free(struct au_nhash *whlist);
10831 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10832 +                           int limit);
10833 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10834 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10835 +                      unsigned int d_type, aufs_bindex_t bindex,
10836 +                      unsigned char shwh);
10837 +void au_vdir_free(struct au_vdir *vdir);
10838 +int au_vdir_init(struct file *file);
10839 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10840 +
10841 +/* ioctl.c */
10842 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10843 +
10844 +#ifdef CONFIG_AUFS_RDU
10845 +/* rdu.c */
10846 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10847 +#ifdef CONFIG_COMPAT
10848 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10849 +                        unsigned long arg);
10850 +#endif
10851 +#else
10852 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10853 +       unsigned int cmd, unsigned long arg)
10854 +#ifdef CONFIG_COMPAT
10855 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10856 +       unsigned int cmd, unsigned long arg)
10857 +#endif
10858 +#endif
10859 +
10860 +#endif /* __KERNEL__ */
10861 +#endif /* __AUFS_DIR_H__ */
10862 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10863 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10864 +++ linux/fs/aufs/dirren.c      2023-10-10 22:51:18.033248030 +0200
10865 @@ -0,0 +1,1315 @@
10866 +// SPDX-License-Identifier: GPL-2.0
10867 +/*
10868 + * Copyright (C) 2017-2022 Junjiro R. Okajima
10869 + *
10870 + * This program is free software; you can redistribute it and/or modify
10871 + * it under the terms of the GNU General Public License as published by
10872 + * the Free Software Foundation; either version 2 of the License, or
10873 + * (at your option) any later version.
10874 + *
10875 + * This program is distributed in the hope that it will be useful,
10876 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10877 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10878 + * GNU General Public License for more details.
10879 + *
10880 + * You should have received a copy of the GNU General Public License
10881 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10882 + */
10883 +
10884 +/*
10885 + * special handling in renaming a directory
10886 + * in order to support looking-up the before-renamed name on the lower readonly
10887 + * branches
10888 + */
10889 +
10890 +#include <linux/byteorder/generic.h>
10891 +#include "aufs.h"
10892 +
10893 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10894 +{
10895 +       int idx;
10896 +
10897 +       idx = au_dr_ihash(ent->dr_h_ino);
10898 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10899 +}
10900 +
10901 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10902 +{
10903 +       int ret, i;
10904 +       struct hlist_bl_head *hbl;
10905 +
10906 +       ret = 1;
10907 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10908 +               hbl = dr->dr_h_ino + i;
10909 +               hlist_bl_lock(hbl);
10910 +               ret &= hlist_bl_empty(hbl);
10911 +               hlist_bl_unlock(hbl);
10912 +       }
10913 +
10914 +       return ret;
10915 +}
10916 +
10917 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10918 +{
10919 +       struct au_dr_hino *found, *ent;
10920 +       struct hlist_bl_head *hbl;
10921 +       struct hlist_bl_node *pos;
10922 +       int idx;
10923 +
10924 +       found = NULL;
10925 +       idx = au_dr_ihash(ino);
10926 +       hbl = dr->dr_h_ino + idx;
10927 +       hlist_bl_lock(hbl);
10928 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10929 +               if (ent->dr_h_ino == ino) {
10930 +                       found = ent;
10931 +                       break;
10932 +               }
10933 +       hlist_bl_unlock(hbl);
10934 +
10935 +       return found;
10936 +}
10937 +
10938 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10939 +                       struct au_dr_hino *add_ent)
10940 +{
10941 +       int found, idx;
10942 +       struct hlist_bl_head *hbl;
10943 +       struct hlist_bl_node *pos;
10944 +       struct au_dr_hino *ent;
10945 +
10946 +       found = 0;
10947 +       idx = au_dr_ihash(ino);
10948 +       hbl = dr->dr_h_ino + idx;
10949 +#if 0 /* debug print */
10950 +       {
10951 +               struct hlist_bl_node *tmp;
10952 +
10953 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10954 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10955 +       }
10956 +#endif
10957 +       hlist_bl_lock(hbl);
10958 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10959 +               if (ent->dr_h_ino == ino) {
10960 +                       found = 1;
10961 +                       break;
10962 +               }
10963 +       if (!found && add_ent)
10964 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10965 +       hlist_bl_unlock(hbl);
10966 +
10967 +       if (!found && add_ent)
10968 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10969 +
10970 +       return found;
10971 +}
10972 +
10973 +void au_dr_hino_free(struct au_dr_br *dr)
10974 +{
10975 +       int i;
10976 +       struct hlist_bl_head *hbl;
10977 +       struct hlist_bl_node *pos, *tmp;
10978 +       struct au_dr_hino *ent;
10979 +
10980 +       /* SiMustWriteLock(sb); */
10981 +
10982 +       for (i = 0; i < AuDirren_NHASH; i++) {
10983 +               hbl = dr->dr_h_ino + i;
10984 +               /* no spinlock since sbinfo must be write-locked */
10985 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10986 +                       au_kfree_rcu(ent);
10987 +               INIT_HLIST_BL_HEAD(hbl);
10988 +       }
10989 +}
10990 +
10991 +/* returns the number of inodes or an error */
10992 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10993 +                           struct file *hinofile)
10994 +{
10995 +       int err, i;
10996 +       ssize_t ssz;
10997 +       loff_t pos, oldsize;
10998 +       __be64 u64;
10999 +       struct inode *hinoinode;
11000 +       struct hlist_bl_head *hbl;
11001 +       struct hlist_bl_node *n1, *n2;
11002 +       struct au_dr_hino *ent;
11003 +
11004 +       SiMustWriteLock(sb);
11005 +       AuDebugOn(!au_br_writable(br->br_perm));
11006 +
11007 +       hinoinode = file_inode(hinofile);
11008 +       oldsize = i_size_read(hinoinode);
11009 +
11010 +       err = 0;
11011 +       pos = 0;
11012 +       hbl = br->br_dirren.dr_h_ino;
11013 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11014 +               /* no bit-lock since sbinfo must be write-locked */
11015 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11016 +                       AuDbg("hi%llu, %pD2\n",
11017 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11018 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11019 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11020 +                       if (ssz == sizeof(u64))
11021 +                               continue;
11022 +
11023 +                       /* write error */
11024 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11025 +                       err = -ENOSPC;
11026 +                       if (ssz < 0)
11027 +                               err = ssz;
11028 +                       break;
11029 +               }
11030 +       }
11031 +       /* regardless the error */
11032 +       if (pos < oldsize) {
11033 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11034 +               AuTraceErr(err);
11035 +       }
11036 +
11037 +       AuTraceErr(err);
11038 +       return err;
11039 +}
11040 +
11041 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11042 +{
11043 +       int err, hidx;
11044 +       ssize_t ssz;
11045 +       size_t sz, n;
11046 +       loff_t pos;
11047 +       uint64_t u64;
11048 +       struct au_dr_hino *ent;
11049 +       struct inode *hinoinode;
11050 +       struct hlist_bl_head *hbl;
11051 +
11052 +       err = 0;
11053 +       pos = 0;
11054 +       hbl = dr->dr_h_ino;
11055 +       hinoinode = file_inode(hinofile);
11056 +       sz = i_size_read(hinoinode);
11057 +       AuDebugOn(sz % sizeof(u64));
11058 +       n = sz / sizeof(u64);
11059 +       while (n--) {
11060 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11061 +               if (unlikely(ssz != sizeof(u64))) {
11062 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11063 +                       err = -EINVAL;
11064 +                       if (ssz < 0)
11065 +                               err = ssz;
11066 +                       goto out_free;
11067 +               }
11068 +
11069 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11070 +               if (!ent) {
11071 +                       err = -ENOMEM;
11072 +                       AuTraceErr(err);
11073 +                       goto out_free;
11074 +               }
11075 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11076 +               AuDbg("hi%llu, %pD2\n",
11077 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11078 +               hidx = au_dr_ihash(ent->dr_h_ino);
11079 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11080 +       }
11081 +       goto out; /* success */
11082 +
11083 +out_free:
11084 +       au_dr_hino_free(dr);
11085 +out:
11086 +       AuTraceErr(err);
11087 +       return err;
11088 +}
11089 +
11090 +/*
11091 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11092 + * @path is a switch to distinguish load and store.
11093 + */
11094 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11095 +                     struct au_branch *br, const struct path *path)
11096 +{
11097 +       int err, flags;
11098 +       unsigned char load, suspend;
11099 +       struct file *hinofile;
11100 +       struct au_hinode *hdir;
11101 +       struct inode *dir, *delegated;
11102 +       struct path hinopath;
11103 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11104 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11105 +
11106 +       AuDebugOn(bindex < 0 && !br);
11107 +       AuDebugOn(bindex >= 0 && br);
11108 +
11109 +       err = -EINVAL;
11110 +       suspend = !br;
11111 +       if (suspend)
11112 +               br = au_sbr(sb, bindex);
11113 +       load = !!path;
11114 +       if (!load) {
11115 +               path = &br->br_path;
11116 +               AuDebugOn(!au_br_writable(br->br_perm));
11117 +               if (unlikely(!au_br_writable(br->br_perm)))
11118 +                       goto out;
11119 +       }
11120 +
11121 +       hdir = NULL;
11122 +       if (suspend) {
11123 +               dir = d_inode(sb->s_root);
11124 +               hdir = au_hinode(au_ii(dir), bindex);
11125 +               dir = hdir->hi_inode;
11126 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11127 +       } else {
11128 +               dir = d_inode(path->dentry);
11129 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11130 +       }
11131 +       hinopath.mnt = path->mnt;
11132 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11133 +       err = PTR_ERR(hinopath.dentry);
11134 +       if (IS_ERR(hinopath.dentry))
11135 +               goto out_unlock;
11136 +
11137 +       err = 0;
11138 +       flags = O_RDONLY;
11139 +       if (load) {
11140 +               if (d_is_negative(hinopath.dentry))
11141 +                       goto out_dput; /* success */
11142 +       } else {
11143 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11144 +                       if (d_is_positive(hinopath.dentry)) {
11145 +                               delegated = NULL;
11146 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11147 +                                                  /*force*/0);
11148 +                               AuTraceErr(err);
11149 +                               if (unlikely(err))
11150 +                                       pr_err("ignored err %d, %pd2\n",
11151 +                                              err, hinopath.dentry);
11152 +                               if (unlikely(err == -EWOULDBLOCK))
11153 +                                       iput(delegated);
11154 +                               err = 0;
11155 +                       }
11156 +                       goto out_dput;
11157 +               } else if (!d_is_positive(hinopath.dentry)) {
11158 +                       err = vfsub_create(dir, &hinopath, 0600,
11159 +                                          /*want_excl*/false);
11160 +                       AuTraceErr(err);
11161 +                       if (unlikely(err))
11162 +                               goto out_dput;
11163 +               }
11164 +               flags = O_WRONLY;
11165 +       }
11166 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11167 +       if (suspend)
11168 +               au_hn_inode_unlock(hdir);
11169 +       else
11170 +               inode_unlock(dir);
11171 +       dput(hinopath.dentry);
11172 +       AuTraceErrPtr(hinofile);
11173 +       if (IS_ERR(hinofile)) {
11174 +               err = PTR_ERR(hinofile);
11175 +               goto out;
11176 +       }
11177 +
11178 +       if (load)
11179 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11180 +       else
11181 +               err = au_dr_hino_store(sb, br, hinofile);
11182 +       fput(hinofile);
11183 +       goto out;
11184 +
11185 +out_dput:
11186 +       dput(hinopath.dentry);
11187 +out_unlock:
11188 +       if (suspend)
11189 +               au_hn_inode_unlock(hdir);
11190 +       else
11191 +               inode_unlock(dir);
11192 +out:
11193 +       AuTraceErr(err);
11194 +       return err;
11195 +}
11196 +
11197 +/* ---------------------------------------------------------------------- */
11198 +
11199 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11200 +{
11201 +       int err;
11202 +       struct kstatfs kstfs;
11203 +       dev_t dev;
11204 +       struct dentry *dentry;
11205 +       struct super_block *sb;
11206 +
11207 +       err = vfs_statfs((void *)path, &kstfs);
11208 +       AuTraceErr(err);
11209 +       if (unlikely(err))
11210 +               goto out;
11211 +
11212 +       /* todo: support for UUID */
11213 +
11214 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11215 +               brid->type = AuBrid_FSID;
11216 +               brid->fsid = kstfs.f_fsid;
11217 +       } else {
11218 +               dentry = path->dentry;
11219 +               sb = dentry->d_sb;
11220 +               dev = sb->s_dev;
11221 +               if (dev) {
11222 +                       brid->type = AuBrid_DEV;
11223 +                       brid->dev = dev;
11224 +               }
11225 +       }
11226 +
11227 +out:
11228 +       return err;
11229 +}
11230 +
11231 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11232 +                 const struct path *path)
11233 +{
11234 +       int err, i;
11235 +       struct au_dr_br *dr;
11236 +       struct hlist_bl_head *hbl;
11237 +
11238 +       dr = &br->br_dirren;
11239 +       hbl = dr->dr_h_ino;
11240 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11241 +               INIT_HLIST_BL_HEAD(hbl);
11242 +
11243 +       err = au_dr_brid_init(&dr->dr_brid, path);
11244 +       if (unlikely(err))
11245 +               goto out;
11246 +
11247 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11248 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11249 +
11250 +out:
11251 +       AuTraceErr(err);
11252 +       return err;
11253 +}
11254 +
11255 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11256 +{
11257 +       int err;
11258 +
11259 +       err = 0;
11260 +       if (au_br_writable(br->br_perm))
11261 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11262 +       if (!err)
11263 +               au_dr_hino_free(&br->br_dirren);
11264 +
11265 +       return err;
11266 +}
11267 +
11268 +/* ---------------------------------------------------------------------- */
11269 +
11270 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11271 +                      char *buf, size_t sz)
11272 +{
11273 +       int err;
11274 +       unsigned int major, minor;
11275 +       char *p;
11276 +
11277 +       p = buf;
11278 +       err = snprintf(p, sz, "%d_", brid->type);
11279 +       AuDebugOn(err > sz);
11280 +       p += err;
11281 +       sz -= err;
11282 +       switch (brid->type) {
11283 +       case AuBrid_Unset:
11284 +               return -EINVAL;
11285 +       case AuBrid_UUID:
11286 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11287 +               break;
11288 +       case AuBrid_FSID:
11289 +               err = snprintf(p, sz, "%08x-%08x",
11290 +                              brid->fsid.val[0], brid->fsid.val[1]);
11291 +               break;
11292 +       case AuBrid_DEV:
11293 +               major = MAJOR(brid->dev);
11294 +               minor = MINOR(brid->dev);
11295 +               if (major <= 0xff && minor <= 0xff)
11296 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11297 +               else
11298 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11299 +               break;
11300 +       }
11301 +       AuDebugOn(err > sz);
11302 +       p += err;
11303 +       sz -= err;
11304 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11305 +       AuDebugOn(err > sz);
11306 +       p += err;
11307 +       sz -= err;
11308 +
11309 +       return p - buf;
11310 +}
11311 +
11312 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11313 +{
11314 +       int rlen;
11315 +       struct dentry *br_dentry;
11316 +       struct inode *br_inode;
11317 +
11318 +       br_dentry = au_br_dentry(br);
11319 +       br_inode = d_inode(br_dentry);
11320 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11321 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11322 +       AuDebugOn(rlen > len);
11323 +
11324 +       return rlen;
11325 +}
11326 +
11327 +/* ---------------------------------------------------------------------- */
11328 +
11329 +/*
11330 + * from the given @h_dentry, construct drinfo at @*fdata.
11331 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11332 + * @allocated.
11333 + */
11334 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11335 +                              struct dentry *h_dentry,
11336 +                              unsigned char *allocated)
11337 +{
11338 +       int err, v;
11339 +       struct au_drinfo_fdata *f, *p;
11340 +       struct au_drinfo *drinfo;
11341 +       struct inode *h_inode;
11342 +       struct qstr *qname;
11343 +
11344 +       err = 0;
11345 +       f = *fdata;
11346 +       h_inode = d_inode(h_dentry);
11347 +       qname = &h_dentry->d_name;
11348 +       drinfo = &f->drinfo;
11349 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11350 +       drinfo->oldnamelen = qname->len;
11351 +       if (*allocated < sizeof(*f) + qname->len) {
11352 +               v = roundup_pow_of_two(*allocated + qname->len);
11353 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11354 +               if (unlikely(!p)) {
11355 +                       err = -ENOMEM;
11356 +                       AuTraceErr(err);
11357 +                       goto out;
11358 +               }
11359 +               f = p;
11360 +               *fdata = f;
11361 +               *allocated = v;
11362 +               drinfo = &f->drinfo;
11363 +       }
11364 +       memcpy(drinfo->oldname, qname->name, qname->len);
11365 +       AuDbg("i%llu, %.*s\n",
11366 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11367 +             drinfo->oldname);
11368 +
11369 +out:
11370 +       AuTraceErr(err);
11371 +       return err;
11372 +}
11373 +
11374 +/* callers have to free the return value */
11375 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11376 +{
11377 +       struct au_drinfo *ret, *drinfo;
11378 +       struct au_drinfo_fdata fdata;
11379 +       int len;
11380 +       loff_t pos;
11381 +       ssize_t ssz;
11382 +
11383 +       ret = ERR_PTR(-EIO);
11384 +       pos = 0;
11385 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11386 +       if (unlikely(ssz != sizeof(fdata))) {
11387 +               AuIOErr("ssz %zd, %u, %pD2\n",
11388 +                       ssz, (unsigned int)sizeof(fdata), file);
11389 +               goto out;
11390 +       }
11391 +
11392 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11393 +       switch (fdata.magic) {
11394 +       case AUFS_DRINFO_MAGIC_V1:
11395 +               break;
11396 +       default:
11397 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11398 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11399 +               goto out;
11400 +       }
11401 +
11402 +       drinfo = &fdata.drinfo;
11403 +       len = drinfo->oldnamelen;
11404 +       if (!len) {
11405 +               AuIOErr("broken drinfo %pD2\n", file);
11406 +               goto out;
11407 +       }
11408 +
11409 +       ret = NULL;
11410 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11411 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11412 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11413 +                     (unsigned long long)drinfo->ino,
11414 +                     (unsigned long long)h_ino, file);
11415 +               goto out; /* success */
11416 +       }
11417 +
11418 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11419 +       if (unlikely(!ret)) {
11420 +               ret = ERR_PTR(-ENOMEM);
11421 +               AuTraceErrPtr(ret);
11422 +               goto out;
11423 +       }
11424 +
11425 +       *ret = *drinfo;
11426 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11427 +       if (unlikely(ssz != len)) {
11428 +               au_kfree_rcu(ret);
11429 +               ret = ERR_PTR(-EIO);
11430 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11431 +               goto out;
11432 +       }
11433 +
11434 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11435 +
11436 +out:
11437 +       return ret;
11438 +}
11439 +
11440 +/* ---------------------------------------------------------------------- */
11441 +
11442 +/* in order to be revertible */
11443 +struct au_drinfo_rev_elm {
11444 +       int                     created;
11445 +       struct dentry           *info_dentry;
11446 +       struct au_drinfo        *info_last;
11447 +};
11448 +
11449 +struct au_drinfo_rev {
11450 +       unsigned char                   already;
11451 +       aufs_bindex_t                   nelm;
11452 +       struct au_drinfo_rev_elm        elm[];
11453 +};
11454 +
11455 +/* todo: isn't it too large? */
11456 +struct au_drinfo_store {
11457 +       struct path h_ppath;
11458 +       struct dentry *h_dentry;
11459 +       struct au_drinfo_fdata *fdata;
11460 +       char *infoname;                 /* inside of whname, just after PFX */
11461 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11462 +       aufs_bindex_t btgt, btail;
11463 +       unsigned char no_sio,
11464 +               allocated,              /* current size of *fdata */
11465 +               infonamelen,            /* room size for p */
11466 +               whnamelen,              /* length of the generated name */
11467 +               renameback;             /* renamed back */
11468 +};
11469 +
11470 +/* on rename(2) error, the caller should revert it using @elm */
11471 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11472 +                             struct au_drinfo_rev_elm *elm)
11473 +{
11474 +       int err, len;
11475 +       ssize_t ssz;
11476 +       loff_t pos;
11477 +       struct path infopath = {
11478 +               .mnt = w->h_ppath.mnt
11479 +       };
11480 +       struct inode *h_dir, *h_inode, *delegated;
11481 +       struct file *infofile;
11482 +       struct qstr *qname;
11483 +
11484 +       AuDebugOn(elm
11485 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11486 +
11487 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11488 +                                              w->whnamelen);
11489 +       AuTraceErrPtr(infopath.dentry);
11490 +       if (IS_ERR(infopath.dentry)) {
11491 +               err = PTR_ERR(infopath.dentry);
11492 +               goto out;
11493 +       }
11494 +
11495 +       err = 0;
11496 +       h_dir = d_inode(w->h_ppath.dentry);
11497 +       if (elm && d_is_negative(infopath.dentry)) {
11498 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11499 +               AuTraceErr(err);
11500 +               if (unlikely(err))
11501 +                       goto out_dput;
11502 +               elm->created = 1;
11503 +               elm->info_dentry = dget(infopath.dentry);
11504 +       }
11505 +
11506 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11507 +       AuTraceErrPtr(infofile);
11508 +       if (IS_ERR(infofile)) {
11509 +               err = PTR_ERR(infofile);
11510 +               goto out_dput;
11511 +       }
11512 +
11513 +       h_inode = d_inode(infopath.dentry);
11514 +       if (elm && i_size_read(h_inode)) {
11515 +               h_inode = d_inode(w->h_dentry);
11516 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11517 +               AuTraceErrPtr(elm->info_last);
11518 +               if (IS_ERR(elm->info_last)) {
11519 +                       err = PTR_ERR(elm->info_last);
11520 +                       elm->info_last = NULL;
11521 +                       AuDebugOn(elm->info_dentry);
11522 +                       goto out_fput;
11523 +               }
11524 +       }
11525 +
11526 +       if (elm && w->renameback) {
11527 +               delegated = NULL;
11528 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11529 +               AuTraceErr(err);
11530 +               if (unlikely(err == -EWOULDBLOCK))
11531 +                       iput(delegated);
11532 +               goto out_fput;
11533 +       }
11534 +
11535 +       pos = 0;
11536 +       qname = &w->h_dentry->d_name;
11537 +       len = sizeof(*w->fdata) + qname->len;
11538 +       if (!elm)
11539 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11540 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11541 +       if (ssz == len) {
11542 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11543 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11544 +               goto out_fput; /* success */
11545 +       } else {
11546 +               err = -EIO;
11547 +               if (ssz < 0)
11548 +                       err = ssz;
11549 +               /* the caller should revert it using @elm */
11550 +       }
11551 +
11552 +out_fput:
11553 +       fput(infofile);
11554 +out_dput:
11555 +       dput(infopath.dentry);
11556 +out:
11557 +       AuTraceErr(err);
11558 +       return err;
11559 +}
11560 +
11561 +struct au_call_drinfo_do_store_args {
11562 +       int *errp;
11563 +       struct au_drinfo_store *w;
11564 +       struct au_drinfo_rev_elm *elm;
11565 +};
11566 +
11567 +static void au_call_drinfo_do_store(void *args)
11568 +{
11569 +       struct au_call_drinfo_do_store_args *a = args;
11570 +
11571 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11572 +}
11573 +
11574 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11575 +                              struct au_drinfo_rev_elm *elm)
11576 +{
11577 +       int err, wkq_err;
11578 +
11579 +       if (w->no_sio)
11580 +               err = au_drinfo_do_store(w, elm);
11581 +       else {
11582 +               struct au_call_drinfo_do_store_args a = {
11583 +                       .errp   = &err,
11584 +                       .w      = w,
11585 +                       .elm    = elm
11586 +               };
11587 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11588 +               if (unlikely(wkq_err))
11589 +                       err = wkq_err;
11590 +       }
11591 +       AuTraceErr(err);
11592 +
11593 +       return err;
11594 +}
11595 +
11596 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11597 +                                    aufs_bindex_t btgt)
11598 +{
11599 +       int err;
11600 +
11601 +       memset(w, 0, sizeof(*w));
11602 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11603 +       strscpy(w->whname, AUFS_WH_DR_INFO_PFX, sizeof(AUFS_WH_DR_INFO_PFX));
11604 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11605 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11606 +       w->btgt = btgt;
11607 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11608 +
11609 +       err = -ENOMEM;
11610 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11611 +       if (unlikely(!w->fdata)) {
11612 +               AuTraceErr(err);
11613 +               goto out;
11614 +       }
11615 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11616 +       err = 0;
11617 +
11618 +out:
11619 +       return err;
11620 +}
11621 +
11622 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11623 +{
11624 +       au_kfree_rcu(w->fdata);
11625 +}
11626 +
11627 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11628 +                               struct au_drinfo_store *w)
11629 +{
11630 +       struct au_drinfo_rev_elm *elm;
11631 +       struct inode *h_dir, *delegated;
11632 +       int err, nelm;
11633 +       struct path infopath = {
11634 +               .mnt = w->h_ppath.mnt
11635 +       };
11636 +
11637 +       h_dir = d_inode(w->h_ppath.dentry);
11638 +       IMustLock(h_dir);
11639 +
11640 +       err = 0;
11641 +       elm = rev->elm;
11642 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11643 +               AuDebugOn(elm->created && elm->info_last);
11644 +               if (elm->created) {
11645 +                       AuDbg("here\n");
11646 +                       delegated = NULL;
11647 +                       infopath.dentry = elm->info_dentry;
11648 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11649 +                                          !w->no_sio);
11650 +                       AuTraceErr(err);
11651 +                       if (unlikely(err == -EWOULDBLOCK))
11652 +                               iput(delegated);
11653 +                       dput(elm->info_dentry);
11654 +               } else if (elm->info_last) {
11655 +                       AuDbg("here\n");
11656 +                       w->fdata->drinfo = *elm->info_last;
11657 +                       memcpy(w->fdata->drinfo.oldname,
11658 +                              elm->info_last->oldname,
11659 +                              elm->info_last->oldnamelen);
11660 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11661 +                       au_kfree_rcu(elm->info_last);
11662 +               }
11663 +               if (unlikely(err))
11664 +                       AuIOErr("%d, %s\n", err, w->whname);
11665 +               /* go on even if err */
11666 +       }
11667 +}
11668 +
11669 +/* caller has to call au_dr_rename_fin() later */
11670 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11671 +                          struct qstr *dst_name, void *_rev)
11672 +{
11673 +       int err, sz, nelm;
11674 +       aufs_bindex_t bindex, btail;
11675 +       struct au_drinfo_store work;
11676 +       struct au_drinfo_rev *rev, **p;
11677 +       struct au_drinfo_rev_elm *elm;
11678 +       struct super_block *sb;
11679 +       struct au_branch *br;
11680 +       struct au_hinode *hdir;
11681 +
11682 +       err = au_drinfo_store_work_init(&work, btgt);
11683 +       AuTraceErr(err);
11684 +       if (unlikely(err))
11685 +               goto out;
11686 +
11687 +       err = -ENOMEM;
11688 +       btail = au_dbtaildir(dentry);
11689 +       nelm = btail - btgt;
11690 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11691 +       rev = kcalloc(1, sz, GFP_NOFS);
11692 +       if (unlikely(!rev)) {
11693 +               AuTraceErr(err);
11694 +               goto out_args;
11695 +       }
11696 +       rev->nelm = nelm;
11697 +       elm = rev->elm;
11698 +       p = _rev;
11699 +       *p = rev;
11700 +
11701 +       err = 0;
11702 +       sb = dentry->d_sb;
11703 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11704 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11705 +       hdir = au_hi(d_inode(dentry), btgt);
11706 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11707 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11708 +               work.h_dentry = au_h_dptr(dentry, bindex);
11709 +               if (!work.h_dentry)
11710 +                       continue;
11711 +
11712 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11713 +                                         &work.allocated);
11714 +               AuTraceErr(err);
11715 +               if (unlikely(err))
11716 +                       break;
11717 +
11718 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11719 +               br = au_sbr(sb, bindex);
11720 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11721 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11722 +                                                work.infonamelen);
11723 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11724 +                     work.whnamelen, work.whname,
11725 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11726 +                     work.fdata->drinfo.oldnamelen,
11727 +                     work.fdata->drinfo.oldname);
11728 +
11729 +               err = au_drinfo_store_sio(&work, elm);
11730 +               AuTraceErr(err);
11731 +               if (unlikely(err))
11732 +                       break;
11733 +       }
11734 +       if (unlikely(err)) {
11735 +               /* revert all drinfo */
11736 +               au_drinfo_store_rev(rev, &work);
11737 +               au_kfree_try_rcu(rev);
11738 +               *p = NULL;
11739 +       }
11740 +       au_hn_inode_unlock(hdir);
11741 +
11742 +out_args:
11743 +       au_drinfo_store_work_fin(&work);
11744 +out:
11745 +       return err;
11746 +}
11747 +
11748 +/* ---------------------------------------------------------------------- */
11749 +
11750 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11751 +                struct qstr *dst_name, void *_rev)
11752 +{
11753 +       int err, already;
11754 +       ino_t ino;
11755 +       struct super_block *sb;
11756 +       struct au_branch *br;
11757 +       struct au_dr_br *dr;
11758 +       struct dentry *h_dentry;
11759 +       struct inode *h_inode;
11760 +       struct au_dr_hino *ent;
11761 +       struct au_drinfo_rev *rev, **p;
11762 +
11763 +       AuDbg("bindex %d\n", bindex);
11764 +
11765 +       err = -ENOMEM;
11766 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11767 +       if (unlikely(!ent))
11768 +               goto out;
11769 +
11770 +       sb = src->d_sb;
11771 +       br = au_sbr(sb, bindex);
11772 +       dr = &br->br_dirren;
11773 +       h_dentry = au_h_dptr(src, bindex);
11774 +       h_inode = d_inode(h_dentry);
11775 +       ino = h_inode->i_ino;
11776 +       ent->dr_h_ino = ino;
11777 +       already = au_dr_hino_test_add(dr, ino, ent);
11778 +       AuDbg("b%d, hi%llu, already %d\n",
11779 +             bindex, (unsigned long long)ino, already);
11780 +
11781 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11782 +       AuTraceErr(err);
11783 +       if (!err) {
11784 +               p = _rev;
11785 +               rev = *p;
11786 +               rev->already = already;
11787 +               goto out; /* success */
11788 +       }
11789 +
11790 +       /* revert */
11791 +       if (!already)
11792 +               au_dr_hino_del(dr, ent);
11793 +       au_kfree_rcu(ent);
11794 +
11795 +out:
11796 +       AuTraceErr(err);
11797 +       return err;
11798 +}
11799 +
11800 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11801 +{
11802 +       struct au_drinfo_rev *rev;
11803 +       struct au_drinfo_rev_elm *elm;
11804 +       int nelm;
11805 +
11806 +       rev = _rev;
11807 +       elm = rev->elm;
11808 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11809 +               dput(elm->info_dentry);
11810 +               au_kfree_rcu(elm->info_last);
11811 +       }
11812 +       au_kfree_try_rcu(rev);
11813 +}
11814 +
11815 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11816 +{
11817 +       int err;
11818 +       struct au_drinfo_store work;
11819 +       struct au_drinfo_rev *rev = _rev;
11820 +       struct super_block *sb;
11821 +       struct au_branch *br;
11822 +       struct inode *h_inode;
11823 +       struct au_dr_br *dr;
11824 +       struct au_dr_hino *ent;
11825 +
11826 +       err = au_drinfo_store_work_init(&work, btgt);
11827 +       if (unlikely(err))
11828 +               goto out;
11829 +
11830 +       sb = src->d_sb;
11831 +       br = au_sbr(sb, btgt);
11832 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11833 +       work.h_ppath.mnt = au_br_mnt(br);
11834 +       au_drinfo_store_rev(rev, &work);
11835 +       au_drinfo_store_work_fin(&work);
11836 +       if (rev->already)
11837 +               goto out;
11838 +
11839 +       dr = &br->br_dirren;
11840 +       h_inode = d_inode(work.h_ppath.dentry);
11841 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11842 +       BUG_ON(!ent);
11843 +       au_dr_hino_del(dr, ent);
11844 +       au_kfree_rcu(ent);
11845 +
11846 +out:
11847 +       au_kfree_try_rcu(rev);
11848 +       if (unlikely(err))
11849 +               pr_err("failed to remove dirren info\n");
11850 +}
11851 +
11852 +/* ---------------------------------------------------------------------- */
11853 +
11854 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11855 +                                          char *whname, int whnamelen,
11856 +                                          struct dentry **info_dentry)
11857 +{
11858 +       struct au_drinfo *drinfo;
11859 +       struct file *f;
11860 +       struct inode *h_dir;
11861 +       struct path infopath;
11862 +       int unlocked;
11863 +
11864 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11865 +
11866 +       *info_dentry = NULL;
11867 +       drinfo = NULL;
11868 +       unlocked = 0;
11869 +       h_dir = d_inode(h_ppath->dentry);
11870 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11871 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11872 +       if (IS_ERR(infopath.dentry)) {
11873 +               drinfo = (void *)infopath.dentry;
11874 +               goto out;
11875 +       }
11876 +
11877 +       if (d_is_negative(infopath.dentry))
11878 +               goto out_dput; /* success */
11879 +
11880 +       infopath.mnt = h_ppath->mnt;
11881 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11882 +       inode_unlock_shared(h_dir);
11883 +       unlocked = 1;
11884 +       if (IS_ERR(f)) {
11885 +               drinfo = (void *)f;
11886 +               goto out_dput;
11887 +       }
11888 +
11889 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11890 +       if (IS_ERR_OR_NULL(drinfo))
11891 +               goto out_fput;
11892 +
11893 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11894 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11895 +
11896 +out_fput:
11897 +       fput(f);
11898 +out_dput:
11899 +       dput(infopath.dentry);
11900 +out:
11901 +       if (!unlocked)
11902 +               inode_unlock_shared(h_dir);
11903 +       AuTraceErrPtr(drinfo);
11904 +       return drinfo;
11905 +}
11906 +
11907 +struct au_drinfo_do_load_args {
11908 +       struct au_drinfo **drinfop;
11909 +       struct path *h_ppath;
11910 +       char *whname;
11911 +       int whnamelen;
11912 +       struct dentry **info_dentry;
11913 +};
11914 +
11915 +static void au_call_drinfo_do_load(void *args)
11916 +{
11917 +       struct au_drinfo_do_load_args *a = args;
11918 +
11919 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11920 +                                       a->info_dentry);
11921 +}
11922 +
11923 +struct au_drinfo_load {
11924 +       struct path h_ppath;
11925 +       struct qstr *qname;
11926 +       unsigned char no_sio;
11927 +
11928 +       aufs_bindex_t ninfo;
11929 +       struct au_drinfo **drinfo;
11930 +};
11931 +
11932 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11933 +                         struct au_branch *br)
11934 +{
11935 +       int err, wkq_err, whnamelen, e;
11936 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11937 +               = AUFS_WH_DR_INFO_PFX;
11938 +       struct au_drinfo *drinfo;
11939 +       struct qstr oldname;
11940 +       struct inode *h_dir, *delegated;
11941 +       struct dentry *info_dentry;
11942 +       struct path infopath;
11943 +
11944 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11945 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11946 +                                   sizeof(whname) - whnamelen);
11947 +       if (w->no_sio)
11948 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11949 +                                          &info_dentry);
11950 +       else {
11951 +               struct au_drinfo_do_load_args args = {
11952 +                       .drinfop        = &drinfo,
11953 +                       .h_ppath        = &w->h_ppath,
11954 +                       .whname         = whname,
11955 +                       .whnamelen      = whnamelen,
11956 +                       .info_dentry    = &info_dentry
11957 +               };
11958 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11959 +               if (unlikely(wkq_err))
11960 +                       drinfo = ERR_PTR(wkq_err);
11961 +       }
11962 +       err = PTR_ERR(drinfo);
11963 +       if (IS_ERR_OR_NULL(drinfo))
11964 +               goto out;
11965 +
11966 +       err = 0;
11967 +       oldname.len = drinfo->oldnamelen;
11968 +       oldname.name = drinfo->oldname;
11969 +       if (au_qstreq(w->qname, &oldname)) {
11970 +               /* the name is renamed back */
11971 +               au_kfree_rcu(drinfo);
11972 +               drinfo = NULL;
11973 +
11974 +               infopath.dentry = info_dentry;
11975 +               infopath.mnt = w->h_ppath.mnt;
11976 +               h_dir = d_inode(w->h_ppath.dentry);
11977 +               delegated = NULL;
11978 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11979 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11980 +               inode_unlock(h_dir);
11981 +               if (unlikely(e))
11982 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11983 +               if (unlikely(e == -EWOULDBLOCK))
11984 +                       iput(delegated);
11985 +       }
11986 +       au_kfree_rcu(w->drinfo[bindex]);
11987 +       w->drinfo[bindex] = drinfo;
11988 +       dput(info_dentry);
11989 +
11990 +out:
11991 +       AuTraceErr(err);
11992 +       return err;
11993 +}
11994 +
11995 +/* ---------------------------------------------------------------------- */
11996 +
11997 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
11998 +{
11999 +       struct au_drinfo **p = drinfo;
12000 +
12001 +       while (n-- > 0)
12002 +               au_kfree_rcu(*drinfo++);
12003 +       au_kfree_try_rcu(p);
12004 +}
12005 +
12006 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12007 +              aufs_bindex_t btgt)
12008 +{
12009 +       int err, ninfo;
12010 +       struct au_drinfo_load w;
12011 +       aufs_bindex_t bindex, bbot;
12012 +       struct au_branch *br;
12013 +       struct inode *h_dir;
12014 +       struct au_dr_hino *ent;
12015 +       struct super_block *sb;
12016 +
12017 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12018 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12019 +             AuLNPair(&lkup->whname), btgt);
12020 +
12021 +       sb = dentry->d_sb;
12022 +       bbot = au_sbbot(sb);
12023 +       w.ninfo = bbot + 1;
12024 +       if (!lkup->dirren.drinfo) {
12025 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12026 +                                             sizeof(*lkup->dirren.drinfo),
12027 +                                             GFP_NOFS);
12028 +               if (unlikely(!lkup->dirren.drinfo)) {
12029 +                       err = -ENOMEM;
12030 +                       goto out;
12031 +               }
12032 +               lkup->dirren.ninfo = w.ninfo;
12033 +       }
12034 +       w.drinfo = lkup->dirren.drinfo;
12035 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12036 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12037 +       AuDebugOn(!w.h_ppath.dentry);
12038 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12039 +       w.qname = &dentry->d_name;
12040 +
12041 +       ninfo = 0;
12042 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12043 +               br = au_sbr(sb, bindex);
12044 +               err = au_drinfo_load(&w, bindex, br);
12045 +               if (unlikely(err))
12046 +                       goto out_free;
12047 +               if (w.drinfo[bindex])
12048 +                       ninfo++;
12049 +       }
12050 +       if (!ninfo) {
12051 +               br = au_sbr(sb, btgt);
12052 +               h_dir = d_inode(w.h_ppath.dentry);
12053 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12054 +               AuDebugOn(!ent);
12055 +               au_dr_hino_del(&br->br_dirren, ent);
12056 +               au_kfree_rcu(ent);
12057 +       }
12058 +       goto out; /* success */
12059 +
12060 +out_free:
12061 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12062 +       lkup->dirren.ninfo = 0;
12063 +       lkup->dirren.drinfo = NULL;
12064 +out:
12065 +       AuTraceErr(err);
12066 +       return err;
12067 +}
12068 +
12069 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12070 +{
12071 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12072 +}
12073 +
12074 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12075 +{
12076 +       int err;
12077 +       struct au_drinfo *drinfo;
12078 +
12079 +       err = 0;
12080 +       if (!lkup->dirren.drinfo)
12081 +               goto out;
12082 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12083 +       drinfo = lkup->dirren.drinfo[btgt];
12084 +       if (!drinfo)
12085 +               goto out;
12086 +
12087 +       au_kfree_try_rcu(lkup->whname.name);
12088 +       lkup->whname.name = NULL;
12089 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12090 +       lkup->dirren.dr_name.name = drinfo->oldname;
12091 +       lkup->name = &lkup->dirren.dr_name;
12092 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12093 +       if (!err)
12094 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12095 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12096 +                     btgt);
12097 +
12098 +out:
12099 +       AuTraceErr(err);
12100 +       return err;
12101 +}
12102 +
12103 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12104 +                    ino_t h_ino)
12105 +{
12106 +       int match;
12107 +       struct au_drinfo *drinfo;
12108 +
12109 +       match = 1;
12110 +       if (!lkup->dirren.drinfo)
12111 +               goto out;
12112 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12113 +       drinfo = lkup->dirren.drinfo[bindex];
12114 +       if (!drinfo)
12115 +               goto out;
12116 +
12117 +       match = (drinfo->ino == h_ino);
12118 +       AuDbg("match %d\n", match);
12119 +
12120 +out:
12121 +       return match;
12122 +}
12123 +
12124 +/* ---------------------------------------------------------------------- */
12125 +
12126 +int au_dr_opt_set(struct super_block *sb)
12127 +{
12128 +       int err;
12129 +       aufs_bindex_t bindex, bbot;
12130 +       struct au_branch *br;
12131 +
12132 +       err = 0;
12133 +       bbot = au_sbbot(sb);
12134 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12135 +               br = au_sbr(sb, bindex);
12136 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12137 +       }
12138 +
12139 +       return err;
12140 +}
12141 +
12142 +int au_dr_opt_flush(struct super_block *sb)
12143 +{
12144 +       int err;
12145 +       aufs_bindex_t bindex, bbot;
12146 +       struct au_branch *br;
12147 +
12148 +       err = 0;
12149 +       bbot = au_sbbot(sb);
12150 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12151 +               br = au_sbr(sb, bindex);
12152 +               if (au_br_writable(br->br_perm))
12153 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12154 +       }
12155 +
12156 +       return err;
12157 +}
12158 +
12159 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12160 +{
12161 +       int err;
12162 +       aufs_bindex_t bindex, bbot;
12163 +       struct au_branch *br;
12164 +
12165 +       err = 0;
12166 +       if (!no_flush) {
12167 +               err = au_dr_opt_flush(sb);
12168 +               if (unlikely(err))
12169 +                       goto out;
12170 +       }
12171 +
12172 +       bbot = au_sbbot(sb);
12173 +       for (bindex = 0; bindex <= bbot; bindex++) {
12174 +               br = au_sbr(sb, bindex);
12175 +               au_dr_hino_free(&br->br_dirren);
12176 +       }
12177 +
12178 +out:
12179 +       return err;
12180 +}
12181 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12182 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12183 +++ linux/fs/aufs/dirren.h      2022-11-05 23:02:18.962555950 +0100
12184 @@ -0,0 +1,140 @@
12185 +/* SPDX-License-Identifier: GPL-2.0 */
12186 +/*
12187 + * Copyright (C) 2017-2022 Junjiro R. Okajima
12188 + *
12189 + * This program is free software; you can redistribute it and/or modify
12190 + * it under the terms of the GNU General Public License as published by
12191 + * the Free Software Foundation; either version 2 of the License, or
12192 + * (at your option) any later version.
12193 + *
12194 + * This program is distributed in the hope that it will be useful,
12195 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12196 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12197 + * GNU General Public License for more details.
12198 + *
12199 + * You should have received a copy of the GNU General Public License
12200 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12201 + */
12202 +
12203 +/*
12204 + * renamed dir info
12205 + */
12206 +
12207 +#ifndef __AUFS_DIRREN_H__
12208 +#define __AUFS_DIRREN_H__
12209 +
12210 +#ifdef __KERNEL__
12211 +
12212 +#include <linux/dcache.h>
12213 +#include <linux/statfs.h>
12214 +#include <linux/uuid.h>
12215 +#include "hbl.h"
12216 +
12217 +#define AuDirren_NHASH 100
12218 +
12219 +#ifdef CONFIG_AUFS_DIRREN
12220 +enum au_brid_type {
12221 +       AuBrid_Unset,
12222 +       AuBrid_UUID,
12223 +       AuBrid_FSID,
12224 +       AuBrid_DEV
12225 +};
12226 +
12227 +struct au_dr_brid {
12228 +       enum au_brid_type       type;
12229 +       union {
12230 +               uuid_t  uuid;   /* unimplemented yet */
12231 +               fsid_t  fsid;
12232 +               dev_t   dev;
12233 +       };
12234 +};
12235 +
12236 +/* 20 is the max digits length of ulong 64 */
12237 +/* brid-type "_" uuid "_" inum */
12238 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12239 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12240 +
12241 +struct au_dr_hino {
12242 +       struct hlist_bl_node    dr_hnode;
12243 +       ino_t                   dr_h_ino;
12244 +};
12245 +
12246 +struct au_dr_br {
12247 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12248 +       struct au_dr_brid       dr_brid;
12249 +};
12250 +
12251 +struct au_dr_lookup {
12252 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12253 +       struct qstr             dr_name; /* subset of dr_info */
12254 +       aufs_bindex_t           ninfo;
12255 +       struct au_drinfo        **drinfo;
12256 +};
12257 +#else
12258 +struct au_dr_hino;
12259 +/* empty */
12260 +struct au_dr_br { };
12261 +struct au_dr_lookup { };
12262 +#endif
12263 +
12264 +/* ---------------------------------------------------------------------- */
12265 +
12266 +struct au_branch;
12267 +struct au_do_lookup_args;
12268 +struct au_hinode;
12269 +#ifdef CONFIG_AUFS_DIRREN
12270 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12271 +                       struct au_dr_hino *add_ent);
12272 +void au_dr_hino_free(struct au_dr_br *dr);
12273 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12274 +                 const struct path *path);
12275 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12276 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12277 +                struct qstr *dst_name, void *_rev);
12278 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12279 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12280 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12281 +              aufs_bindex_t bindex);
12282 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12283 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12284 +                    ino_t h_ino);
12285 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12286 +int au_dr_opt_set(struct super_block *sb);
12287 +int au_dr_opt_flush(struct super_block *sb);
12288 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12289 +#else
12290 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12291 +          struct au_dr_hino *add_ent);
12292 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12293 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12294 +          const struct path *path);
12295 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12296 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12297 +          struct qstr *dst_name, void *_rev);
12298 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12299 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12300 +          void *rev);
12301 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12302 +          aufs_bindex_t bindex);
12303 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12304 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12305 +          aufs_bindex_t bindex, ino_t h_ino);
12306 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12307 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12308 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12309 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12310 +#endif
12311 +
12312 +/* ---------------------------------------------------------------------- */
12313 +
12314 +#ifdef CONFIG_AUFS_DIRREN
12315 +static inline int au_dr_ihash(ino_t h_ino)
12316 +{
12317 +       return h_ino % AuDirren_NHASH;
12318 +}
12319 +#else
12320 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12321 +#endif
12322 +
12323 +#endif /* __KERNEL__ */
12324 +#endif /* __AUFS_DIRREN_H__ */
12325 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12326 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12327 +++ linux/fs/aufs/dynop.c       2022-11-05 23:02:18.962555950 +0100
12328 @@ -0,0 +1,366 @@
12329 +// SPDX-License-Identifier: GPL-2.0
12330 +/*
12331 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12332 + *
12333 + * This program is free software; you can redistribute it and/or modify
12334 + * it under the terms of the GNU General Public License as published by
12335 + * the Free Software Foundation; either version 2 of the License, or
12336 + * (at your option) any later version.
12337 + *
12338 + * This program is distributed in the hope that it will be useful,
12339 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12340 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12341 + * GNU General Public License for more details.
12342 + *
12343 + * You should have received a copy of the GNU General Public License
12344 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12345 + */
12346 +
12347 +/*
12348 + * dynamically customizable operations for regular files
12349 + */
12350 +
12351 +#include "aufs.h"
12352 +
12353 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12354 +
12355 +/*
12356 + * How large will these lists be?
12357 + * Usually just a few elements, 20-30 at most for each, I guess.
12358 + */
12359 +static struct hlist_bl_head dynop[AuDyLast];
12360 +
12361 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12362 +                                    const void *h_op)
12363 +{
12364 +       struct au_dykey *key, *tmp;
12365 +       struct hlist_bl_node *pos;
12366 +
12367 +       key = NULL;
12368 +       hlist_bl_lock(hbl);
12369 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12370 +               if (tmp->dk_op.dy_hop == h_op) {
12371 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12372 +                               key = tmp;
12373 +                       break;
12374 +               }
12375 +       hlist_bl_unlock(hbl);
12376 +
12377 +       return key;
12378 +}
12379 +
12380 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12381 +{
12382 +       struct au_dykey **k, *found;
12383 +       const void *h_op = key->dk_op.dy_hop;
12384 +       int i;
12385 +
12386 +       found = NULL;
12387 +       k = br->br_dykey;
12388 +       for (i = 0; i < AuBrDynOp; i++)
12389 +               if (k[i]) {
12390 +                       if (k[i]->dk_op.dy_hop == h_op) {
12391 +                               found = k[i];
12392 +                               break;
12393 +                       }
12394 +               } else
12395 +                       break;
12396 +       if (!found) {
12397 +               spin_lock(&br->br_dykey_lock);
12398 +               for (; i < AuBrDynOp; i++)
12399 +                       if (k[i]) {
12400 +                               if (k[i]->dk_op.dy_hop == h_op) {
12401 +                                       found = k[i];
12402 +                                       break;
12403 +                               }
12404 +                       } else {
12405 +                               k[i] = key;
12406 +                               break;
12407 +                       }
12408 +               spin_unlock(&br->br_dykey_lock);
12409 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12410 +       }
12411 +
12412 +       return found;
12413 +}
12414 +
12415 +/* kref_get() if @key is already added */
12416 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12417 +{
12418 +       struct au_dykey *tmp, *found;
12419 +       struct hlist_bl_node *pos;
12420 +       const void *h_op = key->dk_op.dy_hop;
12421 +
12422 +       found = NULL;
12423 +       hlist_bl_lock(hbl);
12424 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12425 +               if (tmp->dk_op.dy_hop == h_op) {
12426 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12427 +                               found = tmp;
12428 +                       break;
12429 +               }
12430 +       if (!found)
12431 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12432 +       hlist_bl_unlock(hbl);
12433 +
12434 +       if (!found)
12435 +               DyPrSym(key);
12436 +       return found;
12437 +}
12438 +
12439 +static void dy_free_rcu(struct rcu_head *rcu)
12440 +{
12441 +       struct au_dykey *key;
12442 +
12443 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12444 +       DyPrSym(key);
12445 +       kfree(key);
12446 +}
12447 +
12448 +static void dy_free(struct kref *kref)
12449 +{
12450 +       struct au_dykey *key;
12451 +       struct hlist_bl_head *hbl;
12452 +
12453 +       key = container_of(kref, struct au_dykey, dk_kref);
12454 +       hbl = dynop + key->dk_op.dy_type;
12455 +       au_hbl_del(&key->dk_hnode, hbl);
12456 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12457 +}
12458 +
12459 +void au_dy_put(struct au_dykey *key)
12460 +{
12461 +       kref_put(&key->dk_kref, dy_free);
12462 +}
12463 +
12464 +/* ---------------------------------------------------------------------- */
12465 +
12466 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12467 +
12468 +#ifdef CONFIG_AUFS_DEBUG
12469 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12470 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12471 +#else
12472 +#define DyDbgDeclare(cnt)      do {} while (0)
12473 +#define DyDbgInc(cnt)          do {} while (0)
12474 +#endif
12475 +
12476 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12477 +       DyDbgInc(cnt);                                                  \
12478 +       if (h_op->func) {                                               \
12479 +               if (src.func)                                           \
12480 +                       dst.func = src.func;                            \
12481 +               else                                                    \
12482 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12483 +       }                                                               \
12484 +} while (0)
12485 +
12486 +#define DySetForce(func, dst, src) do {                \
12487 +       AuDebugOn(!src.func);                   \
12488 +       DyDbgInc(cnt);                          \
12489 +       dst.func = src.func;                    \
12490 +} while (0)
12491 +
12492 +#define DySetAop(func) \
12493 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12494 +#define DySetAopForce(func) \
12495 +       DySetForce(func, dyaop->da_op, aufs_aop)
12496 +
12497 +static void dy_aop(struct au_dykey *key, const void *h_op,
12498 +                  struct super_block *h_sb __maybe_unused)
12499 +{
12500 +       struct au_dyaop *dyaop = (void *)key;
12501 +       const struct address_space_operations *h_aop = h_op;
12502 +       DyDbgDeclare(cnt);
12503 +
12504 +       AuDbg("%s\n", au_sbtype(h_sb));
12505 +
12506 +       DySetAop(writepage);
12507 +       DySetAopForce(read_folio);      /* force */
12508 +       DySetAop(writepages);
12509 +       DySetAop(dirty_folio);
12510 +       DySetAop(invalidate_folio);
12511 +       DySetAop(readahead);
12512 +       DySetAop(write_begin);
12513 +       DySetAop(write_end);
12514 +       DySetAop(bmap);
12515 +       DySetAop(release_folio);
12516 +       DySetAop(free_folio);
12517 +       /* this one will be changed according to an aufs mount option */
12518 +       DySetAop(direct_IO);
12519 +       DySetAop(migrate_folio);
12520 +       DySetAop(launder_folio);
12521 +       DySetAop(is_partially_uptodate);
12522 +       DySetAop(is_dirty_writeback);
12523 +       DySetAop(error_remove_page);
12524 +       DySetAop(swap_activate);
12525 +       DySetAop(swap_deactivate);
12526 +       DySetAop(swap_rw);
12527 +
12528 +       DyDbgSize(cnt, *h_aop);
12529 +}
12530 +
12531 +/* ---------------------------------------------------------------------- */
12532 +
12533 +static void dy_bug(struct kref *kref)
12534 +{
12535 +       BUG();
12536 +}
12537 +
12538 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12539 +{
12540 +       struct au_dykey *key, *old;
12541 +       struct hlist_bl_head *hbl;
12542 +       struct op {
12543 +               unsigned int sz;
12544 +               void (*set)(struct au_dykey *key, const void *h_op,
12545 +                           struct super_block *h_sb __maybe_unused);
12546 +       };
12547 +       static const struct op a[] = {
12548 +               [AuDy_AOP] = {
12549 +                       .sz     = sizeof(struct au_dyaop),
12550 +                       .set    = dy_aop
12551 +               }
12552 +       };
12553 +       const struct op *p;
12554 +
12555 +       hbl = dynop + op->dy_type;
12556 +       key = dy_gfind_get(hbl, op->dy_hop);
12557 +       if (key)
12558 +               goto out_add; /* success */
12559 +
12560 +       p = a + op->dy_type;
12561 +       key = kzalloc(p->sz, GFP_NOFS);
12562 +       if (unlikely(!key)) {
12563 +               key = ERR_PTR(-ENOMEM);
12564 +               goto out;
12565 +       }
12566 +
12567 +       key->dk_op.dy_hop = op->dy_hop;
12568 +       kref_init(&key->dk_kref);
12569 +       p->set(key, op->dy_hop, au_br_sb(br));
12570 +       old = dy_gadd(hbl, key);
12571 +       if (old) {
12572 +               au_kfree_rcu(key);
12573 +               key = old;
12574 +       }
12575 +
12576 +out_add:
12577 +       old = dy_bradd(br, key);
12578 +       if (old)
12579 +               /* its ref-count should never be zero here */
12580 +               kref_put(&key->dk_kref, dy_bug);
12581 +out:
12582 +       return key;
12583 +}
12584 +
12585 +/* ---------------------------------------------------------------------- */
12586 +/*
12587 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12588 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12589 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12590 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12591 + * See the aufs manual in detail.
12592 + */
12593 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12594 +{
12595 +       if (!do_dx)
12596 +               dyaop->da_op.direct_IO = NULL;
12597 +       else
12598 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12599 +}
12600 +
12601 +static struct au_dyaop *dy_aget(struct au_branch *br,
12602 +                               const struct address_space_operations *h_aop,
12603 +                               int do_dx)
12604 +{
12605 +       struct au_dyaop *dyaop;
12606 +       struct au_dynop op;
12607 +
12608 +       op.dy_type = AuDy_AOP;
12609 +       op.dy_haop = h_aop;
12610 +       dyaop = (void *)dy_get(&op, br);
12611 +       if (IS_ERR(dyaop))
12612 +               goto out;
12613 +       dy_adx(dyaop, do_dx);
12614 +
12615 +out:
12616 +       return dyaop;
12617 +}
12618 +
12619 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12620 +               struct inode *h_inode)
12621 +{
12622 +       int err, do_dx;
12623 +       struct super_block *sb;
12624 +       struct au_branch *br;
12625 +       struct au_dyaop *dyaop;
12626 +
12627 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12628 +       IiMustWriteLock(inode);
12629 +
12630 +       sb = inode->i_sb;
12631 +       br = au_sbr(sb, bindex);
12632 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12633 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12634 +       err = PTR_ERR(dyaop);
12635 +       if (IS_ERR(dyaop))
12636 +               /* unnecessary to call dy_fput() */
12637 +               goto out;
12638 +
12639 +       err = 0;
12640 +       inode->i_mapping->a_ops = &dyaop->da_op;
12641 +
12642 +out:
12643 +       return err;
12644 +}
12645 +
12646 +/*
12647 + * Is it safe to replace a_ops during the inode/file is in operation?
12648 + * Yes, I hope so.
12649 + */
12650 +int au_dy_irefresh(struct inode *inode)
12651 +{
12652 +       int err;
12653 +       aufs_bindex_t btop;
12654 +       struct inode *h_inode;
12655 +
12656 +       err = 0;
12657 +       if (S_ISREG(inode->i_mode)) {
12658 +               btop = au_ibtop(inode);
12659 +               h_inode = au_h_iptr(inode, btop);
12660 +               err = au_dy_iaop(inode, btop, h_inode);
12661 +       }
12662 +       return err;
12663 +}
12664 +
12665 +void au_dy_arefresh(int do_dx)
12666 +{
12667 +       struct hlist_bl_head *hbl;
12668 +       struct hlist_bl_node *pos;
12669 +       struct au_dykey *key;
12670 +
12671 +       hbl = dynop + AuDy_AOP;
12672 +       hlist_bl_lock(hbl);
12673 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12674 +               dy_adx((void *)key, do_dx);
12675 +       hlist_bl_unlock(hbl);
12676 +}
12677 +
12678 +/* ---------------------------------------------------------------------- */
12679 +
12680 +void __init au_dy_init(void)
12681 +{
12682 +       int i;
12683 +
12684 +       for (i = 0; i < AuDyLast; i++)
12685 +               INIT_HLIST_BL_HEAD(dynop + i);
12686 +}
12687 +
12688 +void au_dy_fin(void)
12689 +{
12690 +       int i;
12691 +
12692 +       for (i = 0; i < AuDyLast; i++)
12693 +               WARN_ON(!hlist_bl_empty(dynop + i));
12694 +}
12695 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12696 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12697 +++ linux/fs/aufs/dynop.h       2022-11-05 23:02:18.962555950 +0100
12698 @@ -0,0 +1,77 @@
12699 +/* SPDX-License-Identifier: GPL-2.0 */
12700 +/*
12701 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12702 + *
12703 + * This program is free software; you can redistribute it and/or modify
12704 + * it under the terms of the GNU General Public License as published by
12705 + * the Free Software Foundation; either version 2 of the License, or
12706 + * (at your option) any later version.
12707 + *
12708 + * This program is distributed in the hope that it will be useful,
12709 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12710 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12711 + * GNU General Public License for more details.
12712 + *
12713 + * You should have received a copy of the GNU General Public License
12714 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12715 + */
12716 +
12717 +/*
12718 + * dynamically customizable operations (for regular files only)
12719 + */
12720 +
12721 +#ifndef __AUFS_DYNOP_H__
12722 +#define __AUFS_DYNOP_H__
12723 +
12724 +#ifdef __KERNEL__
12725 +
12726 +#include <linux/fs.h>
12727 +#include <linux/kref.h>
12728 +
12729 +enum {AuDy_AOP, AuDyLast};
12730 +
12731 +struct au_dynop {
12732 +       int                                             dy_type;
12733 +       union {
12734 +               const void                              *dy_hop;
12735 +               const struct address_space_operations   *dy_haop;
12736 +       };
12737 +};
12738 +
12739 +struct au_dykey {
12740 +       union {
12741 +               struct hlist_bl_node    dk_hnode;
12742 +               struct rcu_head         dk_rcu;
12743 +       };
12744 +       struct au_dynop         dk_op;
12745 +
12746 +       /*
12747 +        * during I am in the branch local array, kref is gotten. when the
12748 +        * branch is removed, kref is put.
12749 +        */
12750 +       struct kref             dk_kref;
12751 +};
12752 +
12753 +/* stop unioning since their sizes are very different from each other */
12754 +struct au_dyaop {
12755 +       struct au_dykey                 da_key;
12756 +       struct address_space_operations da_op; /* not const */
12757 +};
12758 +/* make sure that 'struct au_dykey *' can be any type */
12759 +static_assert(!offsetof(struct au_dyaop, da_key));
12760 +
12761 +/* ---------------------------------------------------------------------- */
12762 +
12763 +/* dynop.c */
12764 +struct au_branch;
12765 +void au_dy_put(struct au_dykey *key);
12766 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12767 +               struct inode *h_inode);
12768 +int au_dy_irefresh(struct inode *inode);
12769 +void au_dy_arefresh(int do_dio);
12770 +
12771 +void __init au_dy_init(void);
12772 +void au_dy_fin(void);
12773 +
12774 +#endif /* __KERNEL__ */
12775 +#endif /* __AUFS_DYNOP_H__ */
12776 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12777 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12778 +++ linux/fs/aufs/export.c      2022-12-17 09:21:34.796521861 +0100
12779 @@ -0,0 +1,830 @@
12780 +// SPDX-License-Identifier: GPL-2.0
12781 +/*
12782 + * Copyright (C) 2005-2022 Junjiro R. Okajima
12783 + *
12784 + * This program is free software; you can redistribute it and/or modify
12785 + * it under the terms of the GNU General Public License as published by
12786 + * the Free Software Foundation; either version 2 of the License, or
12787 + * (at your option) any later version.
12788 + *
12789 + * This program is distributed in the hope that it will be useful,
12790 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12791 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12792 + * GNU General Public License for more details.
12793 + *
12794 + * You should have received a copy of the GNU General Public License
12795 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12796 + */
12797 +
12798 +/*
12799 + * export via nfs
12800 + */
12801 +
12802 +#include <linux/exportfs.h>
12803 +#include <linux/fs_struct.h>
12804 +#include <linux/nsproxy.h>
12805 +#include <linux/random.h>
12806 +#include <linux/writeback.h>
12807 +#include "aufs.h"
12808 +
12809 +union conv {
12810 +#ifdef CONFIG_AUFS_INO_T_64
12811 +       __u32 a[2];
12812 +#else
12813 +       __u32 a[1];
12814 +#endif
12815 +       ino_t ino;
12816 +};
12817 +
12818 +static ino_t decode_ino(__u32 *a)
12819 +{
12820 +       union conv u;
12821 +
12822 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12823 +       u.a[0] = a[0];
12824 +#ifdef CONFIG_AUFS_INO_T_64
12825 +       u.a[1] = a[1];
12826 +#endif
12827 +       return u.ino;
12828 +}
12829 +
12830 +static void encode_ino(__u32 *a, ino_t ino)
12831 +{
12832 +       union conv u;
12833 +
12834 +       u.ino = ino;
12835 +       a[0] = u.a[0];
12836 +#ifdef CONFIG_AUFS_INO_T_64
12837 +       a[1] = u.a[1];
12838 +#endif
12839 +}
12840 +
12841 +/* NFS file handle */
12842 +enum {
12843 +       Fh_br_id,
12844 +       Fh_sigen,
12845 +#ifdef CONFIG_AUFS_INO_T_64
12846 +       /* support 64bit inode number */
12847 +       Fh_ino1,
12848 +       Fh_ino2,
12849 +       Fh_dir_ino1,
12850 +       Fh_dir_ino2,
12851 +#else
12852 +       Fh_ino1,
12853 +       Fh_dir_ino1,
12854 +#endif
12855 +       Fh_igen,
12856 +       Fh_h_type,
12857 +       Fh_tail,
12858 +
12859 +       Fh_ino = Fh_ino1,
12860 +       Fh_dir_ino = Fh_dir_ino1
12861 +};
12862 +
12863 +static int au_test_anon(struct dentry *dentry)
12864 +{
12865 +       /* note: read d_flags without d_lock */
12866 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12867 +}
12868 +
12869 +int au_test_nfsd(void)
12870 +{
12871 +       int ret;
12872 +       struct task_struct *tsk = current;
12873 +       char comm[sizeof(tsk->comm)];
12874 +
12875 +       ret = 0;
12876 +       if (tsk->flags & PF_KTHREAD) {
12877 +               get_task_comm(comm, tsk);
12878 +               ret = !strcmp(comm, "nfsd");
12879 +       }
12880 +
12881 +       return ret;
12882 +}
12883 +
12884 +/* ---------------------------------------------------------------------- */
12885 +/* inode generation external table */
12886 +
12887 +void au_xigen_inc(struct inode *inode)
12888 +{
12889 +       loff_t pos;
12890 +       ssize_t sz;
12891 +       __u32 igen;
12892 +       struct super_block *sb;
12893 +       struct au_sbinfo *sbinfo;
12894 +
12895 +       sb = inode->i_sb;
12896 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12897 +
12898 +       sbinfo = au_sbi(sb);
12899 +       pos = inode->i_ino;
12900 +       pos *= sizeof(igen);
12901 +       igen = inode->i_generation + 1;
12902 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12903 +       if (sz == sizeof(igen))
12904 +               return; /* success */
12905 +
12906 +       if (unlikely(sz >= 0))
12907 +               AuIOErr("xigen error (%zd)\n", sz);
12908 +}
12909 +
12910 +int au_xigen_new(struct inode *inode)
12911 +{
12912 +       int err;
12913 +       loff_t pos;
12914 +       ssize_t sz;
12915 +       struct super_block *sb;
12916 +       struct au_sbinfo *sbinfo;
12917 +       struct file *file;
12918 +
12919 +       err = 0;
12920 +       /* todo: dirty, at mount time */
12921 +       if (inode->i_ino == AUFS_ROOT_INO)
12922 +               goto out;
12923 +       sb = inode->i_sb;
12924 +       SiMustAnyLock(sb);
12925 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12926 +               goto out;
12927 +
12928 +       err = -EFBIG;
12929 +       pos = inode->i_ino;
12930 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12931 +               AuIOErr1("too large i%lld\n", pos);
12932 +               goto out;
12933 +       }
12934 +       pos *= sizeof(inode->i_generation);
12935 +
12936 +       err = 0;
12937 +       sbinfo = au_sbi(sb);
12938 +       file = sbinfo->si_xigen;
12939 +       BUG_ON(!file);
12940 +
12941 +       if (vfsub_f_size_read(file)
12942 +           < pos + sizeof(inode->i_generation)) {
12943 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12944 +               sz = xino_fwrite(file, &inode->i_generation,
12945 +                                sizeof(inode->i_generation), &pos);
12946 +       } else
12947 +               sz = xino_fread(file, &inode->i_generation,
12948 +                               sizeof(inode->i_generation), &pos);
12949 +       if (sz == sizeof(inode->i_generation))
12950 +               goto out; /* success */
12951 +
12952 +       err = sz;
12953 +       if (unlikely(sz >= 0)) {
12954 +               err = -EIO;
12955 +               AuIOErr("xigen error (%zd)\n", sz);
12956 +       }
12957 +
12958 +out:
12959 +       return err;
12960 +}
12961 +
12962 +int au_xigen_set(struct super_block *sb, struct path *path)
12963 +{
12964 +       int err;
12965 +       struct au_sbinfo *sbinfo;
12966 +       struct file *file;
12967 +
12968 +       SiMustWriteLock(sb);
12969 +
12970 +       sbinfo = au_sbi(sb);
12971 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12972 +       err = PTR_ERR(file);
12973 +       if (IS_ERR(file))
12974 +               goto out;
12975 +       err = 0;
12976 +       if (sbinfo->si_xigen)
12977 +               fput(sbinfo->si_xigen);
12978 +       sbinfo->si_xigen = file;
12979 +
12980 +out:
12981 +       AuTraceErr(err);
12982 +       return err;
12983 +}
12984 +
12985 +void au_xigen_clr(struct super_block *sb)
12986 +{
12987 +       struct au_sbinfo *sbinfo;
12988 +
12989 +       SiMustWriteLock(sb);
12990 +
12991 +       sbinfo = au_sbi(sb);
12992 +       if (sbinfo->si_xigen) {
12993 +               fput(sbinfo->si_xigen);
12994 +               sbinfo->si_xigen = NULL;
12995 +       }
12996 +}
12997 +
12998 +/* ---------------------------------------------------------------------- */
12999 +
13000 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13001 +                                   ino_t dir_ino)
13002 +{
13003 +       struct dentry *dentry, *d;
13004 +       struct inode *inode;
13005 +       unsigned int sigen;
13006 +
13007 +       dentry = NULL;
13008 +       inode = ilookup(sb, ino);
13009 +       if (!inode)
13010 +               goto out;
13011 +
13012 +       dentry = ERR_PTR(-ESTALE);
13013 +       sigen = au_sigen(sb);
13014 +       if (unlikely(au_is_bad_inode(inode)
13015 +                    || IS_DEADDIR(inode)
13016 +                    || sigen != au_iigen(inode, NULL)))
13017 +               goto out_iput;
13018 +
13019 +       dentry = NULL;
13020 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13021 +               dentry = d_find_alias(inode);
13022 +       else {
13023 +               spin_lock(&inode->i_lock);
13024 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13025 +                       spin_lock(&d->d_lock);
13026 +                       if (!au_test_anon(d)
13027 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13028 +                               dentry = dget_dlock(d);
13029 +                               spin_unlock(&d->d_lock);
13030 +                               break;
13031 +                       }
13032 +                       spin_unlock(&d->d_lock);
13033 +               }
13034 +               spin_unlock(&inode->i_lock);
13035 +       }
13036 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13037 +               /* need to refresh */
13038 +               dput(dentry);
13039 +               dentry = NULL;
13040 +       }
13041 +
13042 +out_iput:
13043 +       iput(inode);
13044 +out:
13045 +       AuTraceErrPtr(dentry);
13046 +       return dentry;
13047 +}
13048 +
13049 +/* ---------------------------------------------------------------------- */
13050 +
13051 +/* todo: dirty? */
13052 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13053 +
13054 +struct au_compare_mnt_args {
13055 +       /* input */
13056 +       struct super_block *sb;
13057 +
13058 +       /* output */
13059 +       struct vfsmount *mnt;
13060 +};
13061 +
13062 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13063 +{
13064 +       struct au_compare_mnt_args *a = arg;
13065 +
13066 +       if (mnt->mnt_sb != a->sb)
13067 +               return 0;
13068 +       a->mnt = mntget(mnt);
13069 +       return 1;
13070 +}
13071 +
13072 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13073 +{
13074 +       int err;
13075 +       struct path root;
13076 +       struct au_compare_mnt_args args = {
13077 +               .sb = sb
13078 +       };
13079 +
13080 +       get_fs_root(current->fs, &root);
13081 +       rcu_read_lock();
13082 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13083 +       rcu_read_unlock();
13084 +       path_put(&root);
13085 +       AuDebugOn(!err);
13086 +       AuDebugOn(!args.mnt);
13087 +       return args.mnt;
13088 +}
13089 +
13090 +struct au_nfsd_si_lock {
13091 +       unsigned int sigen;
13092 +       aufs_bindex_t bindex, br_id;
13093 +       unsigned char force_lock;
13094 +};
13095 +
13096 +static int si_nfsd_read_lock(struct super_block *sb,
13097 +                            struct au_nfsd_si_lock *nsi_lock)
13098 +{
13099 +       int err;
13100 +       aufs_bindex_t bindex;
13101 +
13102 +       si_read_lock(sb, AuLock_FLUSH);
13103 +
13104 +       /* branch id may be wrapped around */
13105 +       err = 0;
13106 +       bindex = au_br_index(sb, nsi_lock->br_id);
13107 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13108 +               goto out; /* success */
13109 +
13110 +       err = -ESTALE;
13111 +       bindex = -1;
13112 +       if (!nsi_lock->force_lock)
13113 +               si_read_unlock(sb);
13114 +
13115 +out:
13116 +       nsi_lock->bindex = bindex;
13117 +       return err;
13118 +}
13119 +
13120 +struct find_name_by_ino {
13121 +       struct dir_context ctx;
13122 +       int called, found;
13123 +       ino_t ino;
13124 +       char *name;
13125 +       int namelen;
13126 +};
13127 +
13128 +static bool
13129 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13130 +                loff_t offset, u64 ino, unsigned int d_type)
13131 +{
13132 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13133 +                                                 ctx);
13134 +
13135 +       a->called++;
13136 +       if (a->ino != ino)
13137 +               return true;
13138 +
13139 +       memcpy(a->name, name, namelen);
13140 +       a->namelen = namelen;
13141 +       a->found = 1;
13142 +       return false;
13143 +}
13144 +
13145 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13146 +                                    struct au_nfsd_si_lock *nsi_lock)
13147 +{
13148 +       struct dentry *dentry, *parent;
13149 +       struct file *file;
13150 +       struct inode *dir;
13151 +       struct find_name_by_ino arg = {
13152 +               .ctx = {
13153 +                       .actor = find_name_by_ino
13154 +               }
13155 +       };
13156 +       int err;
13157 +
13158 +       parent = path->dentry;
13159 +       if (nsi_lock)
13160 +               si_read_unlock(parent->d_sb);
13161 +       file = vfsub_dentry_open(path, au_dir_roflags);
13162 +       dentry = (void *)file;
13163 +       if (IS_ERR(file))
13164 +               goto out;
13165 +
13166 +       dentry = ERR_PTR(-ENOMEM);
13167 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13168 +       if (unlikely(!arg.name))
13169 +               goto out_file;
13170 +       arg.ino = ino;
13171 +       arg.found = 0;
13172 +       do {
13173 +               arg.called = 0;
13174 +               /* smp_mb(); */
13175 +               err = vfsub_iterate_dir(file, &arg.ctx);
13176 +       } while (!err && !arg.found && arg.called);
13177 +       dentry = ERR_PTR(err);
13178 +       if (unlikely(err))
13179 +               goto out_name;
13180 +       /* instead of ENOENT */
13181 +       dentry = ERR_PTR(-ESTALE);
13182 +       if (!arg.found)
13183 +               goto out_name;
13184 +
13185 +       /* do not call vfsub_lkup_one() */
13186 +       dir = d_inode(parent);
13187 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13188 +       AuTraceErrPtr(dentry);
13189 +       if (IS_ERR(dentry))
13190 +               goto out_name;
13191 +       AuDebugOn(au_test_anon(dentry));
13192 +       if (unlikely(d_really_is_negative(dentry))) {
13193 +               dput(dentry);
13194 +               dentry = ERR_PTR(-ENOENT);
13195 +       }
13196 +
13197 +out_name:
13198 +       free_page((unsigned long)arg.name);
13199 +out_file:
13200 +       fput(file);
13201 +out:
13202 +       if (unlikely(nsi_lock
13203 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13204 +               if (!IS_ERR(dentry)) {
13205 +                       dput(dentry);
13206 +                       dentry = ERR_PTR(-ESTALE);
13207 +               }
13208 +       AuTraceErrPtr(dentry);
13209 +       return dentry;
13210 +}
13211 +
13212 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13213 +                                       ino_t dir_ino,
13214 +                                       struct au_nfsd_si_lock *nsi_lock)
13215 +{
13216 +       struct dentry *dentry;
13217 +       struct path path;
13218 +
13219 +       if (dir_ino != AUFS_ROOT_INO) {
13220 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13221 +               dentry = path.dentry;
13222 +               if (!path.dentry || IS_ERR(path.dentry))
13223 +                       goto out;
13224 +               AuDebugOn(au_test_anon(path.dentry));
13225 +       } else
13226 +               path.dentry = dget(sb->s_root);
13227 +
13228 +       path.mnt = au_mnt_get(sb);
13229 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13230 +       path_put(&path);
13231 +
13232 +out:
13233 +       AuTraceErrPtr(dentry);
13234 +       return dentry;
13235 +}
13236 +
13237 +/* ---------------------------------------------------------------------- */
13238 +
13239 +static int h_acceptable(void *expv, struct dentry *dentry)
13240 +{
13241 +       return 1;
13242 +}
13243 +
13244 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13245 +                          char *buf, int len, struct super_block *sb)
13246 +{
13247 +       char *p;
13248 +       int n;
13249 +       struct path path;
13250 +
13251 +       p = d_path(h_rootpath, buf, len);
13252 +       if (IS_ERR(p))
13253 +               goto out;
13254 +       n = strlen(p);
13255 +
13256 +       path.mnt = h_rootpath->mnt;
13257 +       path.dentry = h_parent;
13258 +       p = d_path(&path, buf, len);
13259 +       if (IS_ERR(p))
13260 +               goto out;
13261 +       if (n != 1)
13262 +               p += n;
13263 +
13264 +       path.mnt = au_mnt_get(sb);
13265 +       path.dentry = sb->s_root;
13266 +       p = d_path(&path, buf, len - strlen(p));
13267 +       mntput(path.mnt);
13268 +       if (IS_ERR(p))
13269 +               goto out;
13270 +       if (n != 1)
13271 +               p[strlen(p)] = '/';
13272 +
13273 +out:
13274 +       AuTraceErrPtr(p);
13275 +       return p;
13276 +}
13277 +
13278 +static
13279 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13280 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13281 +{
13282 +       struct dentry *dentry, *h_parent, *root;
13283 +       struct super_block *h_sb;
13284 +       char *pathname, *p;
13285 +       struct vfsmount *h_mnt;
13286 +       struct au_branch *br;
13287 +       int err;
13288 +       struct path path;
13289 +
13290 +       br = au_sbr(sb, nsi_lock->bindex);
13291 +       h_mnt = au_br_mnt(br);
13292 +       h_sb = h_mnt->mnt_sb;
13293 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13294 +       lockdep_off();
13295 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13296 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13297 +                                     h_acceptable, /*context*/NULL);
13298 +       lockdep_on();
13299 +       dentry = h_parent;
13300 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13301 +               AuWarn1("%s decode_fh failed, %ld\n",
13302 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13303 +               goto out;
13304 +       }
13305 +       dentry = NULL;
13306 +       if (unlikely(au_test_anon(h_parent))) {
13307 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13308 +                       au_sbtype(h_sb));
13309 +               goto out_h_parent;
13310 +       }
13311 +
13312 +       dentry = ERR_PTR(-ENOMEM);
13313 +       pathname = (void *)__get_free_page(GFP_NOFS);
13314 +       if (unlikely(!pathname))
13315 +               goto out_h_parent;
13316 +
13317 +       root = sb->s_root;
13318 +       path.mnt = h_mnt;
13319 +       di_read_lock_parent(root, !AuLock_IR);
13320 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13321 +       di_read_unlock(root, !AuLock_IR);
13322 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13323 +       dentry = (void *)p;
13324 +       if (IS_ERR(p))
13325 +               goto out_pathname;
13326 +
13327 +       si_read_unlock(sb);
13328 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13329 +       dentry = ERR_PTR(err);
13330 +       if (unlikely(err))
13331 +               goto out_relock;
13332 +
13333 +       dentry = ERR_PTR(-ENOENT);
13334 +       AuDebugOn(au_test_anon(path.dentry));
13335 +       if (unlikely(d_really_is_negative(path.dentry)))
13336 +               goto out_path;
13337 +
13338 +       if (ino != d_inode(path.dentry)->i_ino)
13339 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13340 +       else
13341 +               dentry = dget(path.dentry);
13342 +
13343 +out_path:
13344 +       path_put(&path);
13345 +out_relock:
13346 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13347 +               if (!IS_ERR(dentry)) {
13348 +                       dput(dentry);
13349 +                       dentry = ERR_PTR(-ESTALE);
13350 +               }
13351 +out_pathname:
13352 +       free_page((unsigned long)pathname);
13353 +out_h_parent:
13354 +       dput(h_parent);
13355 +out:
13356 +       AuTraceErrPtr(dentry);
13357 +       return dentry;
13358 +}
13359 +
13360 +/* ---------------------------------------------------------------------- */
13361 +
13362 +static struct dentry *
13363 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13364 +                 int fh_type)
13365 +{
13366 +       struct dentry *dentry;
13367 +       __u32 *fh = fid->raw;
13368 +       struct au_branch *br;
13369 +       ino_t ino, dir_ino;
13370 +       struct au_nfsd_si_lock nsi_lock = {
13371 +               .force_lock     = 0
13372 +       };
13373 +
13374 +       dentry = ERR_PTR(-ESTALE);
13375 +       /* it should never happen, but the file handle is unreliable */
13376 +       if (unlikely(fh_len < Fh_tail))
13377 +               goto out;
13378 +       nsi_lock.sigen = fh[Fh_sigen];
13379 +       nsi_lock.br_id = fh[Fh_br_id];
13380 +
13381 +       /* branch id may be wrapped around */
13382 +       br = NULL;
13383 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13384 +               goto out;
13385 +       nsi_lock.force_lock = 1;
13386 +
13387 +       /* is this inode still cached? */
13388 +       ino = decode_ino(fh + Fh_ino);
13389 +       /* it should never happen */
13390 +       if (unlikely(ino == AUFS_ROOT_INO))
13391 +               goto out_unlock;
13392 +
13393 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13394 +       dentry = decode_by_ino(sb, ino, dir_ino);
13395 +       if (IS_ERR(dentry))
13396 +               goto out_unlock;
13397 +       if (dentry)
13398 +               goto accept;
13399 +
13400 +       /* is the parent dir cached? */
13401 +       br = au_sbr(sb, nsi_lock.bindex);
13402 +       au_lcnt_inc(&br->br_nfiles);
13403 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13404 +       if (IS_ERR(dentry))
13405 +               goto out_unlock;
13406 +       if (dentry)
13407 +               goto accept;
13408 +
13409 +       /* lookup path */
13410 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13411 +       if (IS_ERR(dentry))
13412 +               goto out_unlock;
13413 +       if (unlikely(!dentry))
13414 +               /* todo?: make it ESTALE */
13415 +               goto out_unlock;
13416 +
13417 +accept:
13418 +       if (!au_digen_test(dentry, au_sigen(sb))
13419 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13420 +               goto out_unlock; /* success */
13421 +
13422 +       dput(dentry);
13423 +       dentry = ERR_PTR(-ESTALE);
13424 +out_unlock:
13425 +       if (br)
13426 +               au_lcnt_dec(&br->br_nfiles);
13427 +       si_read_unlock(sb);
13428 +out:
13429 +       AuTraceErrPtr(dentry);
13430 +       return dentry;
13431 +}
13432 +
13433 +#if 0 /* reserved for future use */
13434 +/* support subtreecheck option */
13435 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13436 +                                       int fh_len, int fh_type)
13437 +{
13438 +       struct dentry *parent;
13439 +       __u32 *fh = fid->raw;
13440 +       ino_t dir_ino;
13441 +
13442 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13443 +       parent = decode_by_ino(sb, dir_ino, 0);
13444 +       if (IS_ERR(parent))
13445 +               goto out;
13446 +       if (!parent)
13447 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13448 +                                       dir_ino, fh, fh_len);
13449 +
13450 +out:
13451 +       AuTraceErrPtr(parent);
13452 +       return parent;
13453 +}
13454 +#endif
13455 +
13456 +/* ---------------------------------------------------------------------- */
13457 +
13458 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13459 +                         struct inode *dir)
13460 +{
13461 +       int err;
13462 +       aufs_bindex_t bindex;
13463 +       struct super_block *sb, *h_sb;
13464 +       struct dentry *dentry, *parent, *h_parent;
13465 +       struct inode *h_dir;
13466 +       struct au_branch *br;
13467 +
13468 +       err = -ENOSPC;
13469 +       if (unlikely(*max_len <= Fh_tail)) {
13470 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13471 +               goto out;
13472 +       }
13473 +
13474 +       err = FILEID_ROOT;
13475 +       if (inode->i_ino == AUFS_ROOT_INO) {
13476 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13477 +               goto out;
13478 +       }
13479 +
13480 +       h_parent = NULL;
13481 +       sb = inode->i_sb;
13482 +       err = si_read_lock(sb, AuLock_FLUSH);
13483 +       if (unlikely(err))
13484 +               goto out;
13485 +
13486 +#ifdef CONFIG_AUFS_DEBUG
13487 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13488 +               AuWarn1("NFS-exporting requires xino\n");
13489 +#endif
13490 +       err = -EIO;
13491 +       parent = NULL;
13492 +       ii_read_lock_child(inode);
13493 +       bindex = au_ibtop(inode);
13494 +       if (!dir) {
13495 +               dentry = d_find_any_alias(inode);
13496 +               if (unlikely(!dentry))
13497 +                       goto out_unlock;
13498 +               AuDebugOn(au_test_anon(dentry));
13499 +               parent = dget_parent(dentry);
13500 +               dput(dentry);
13501 +               if (unlikely(!parent))
13502 +                       goto out_unlock;
13503 +               if (d_really_is_positive(parent))
13504 +                       dir = d_inode(parent);
13505 +       }
13506 +
13507 +       ii_read_lock_parent(dir);
13508 +       h_dir = au_h_iptr(dir, bindex);
13509 +       ii_read_unlock(dir);
13510 +       if (unlikely(!h_dir))
13511 +               goto out_parent;
13512 +       h_parent = d_find_any_alias(h_dir);
13513 +       if (unlikely(!h_parent))
13514 +               goto out_hparent;
13515 +
13516 +       err = -EPERM;
13517 +       br = au_sbr(sb, bindex);
13518 +       h_sb = au_br_sb(br);
13519 +       if (unlikely(!h_sb->s_export_op)) {
13520 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13521 +               goto out_hparent;
13522 +       }
13523 +
13524 +       fh[Fh_br_id] = br->br_id;
13525 +       fh[Fh_sigen] = au_sigen(sb);
13526 +       encode_ino(fh + Fh_ino, inode->i_ino);
13527 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13528 +       fh[Fh_igen] = inode->i_generation;
13529 +
13530 +       *max_len -= Fh_tail;
13531 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13532 +                                          max_len,
13533 +                                          /*connectable or subtreecheck*/0);
13534 +       err = fh[Fh_h_type];
13535 +       *max_len += Fh_tail;
13536 +       /* todo: macros? */
13537 +       if (err != FILEID_INVALID)
13538 +               err = 99;
13539 +       else
13540 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13541 +
13542 +out_hparent:
13543 +       dput(h_parent);
13544 +out_parent:
13545 +       dput(parent);
13546 +out_unlock:
13547 +       ii_read_unlock(inode);
13548 +       si_read_unlock(sb);
13549 +out:
13550 +       if (unlikely(err < 0))
13551 +               err = FILEID_INVALID;
13552 +       return err;
13553 +}
13554 +
13555 +/* ---------------------------------------------------------------------- */
13556 +
13557 +static int aufs_commit_metadata(struct inode *inode)
13558 +{
13559 +       int err;
13560 +       aufs_bindex_t bindex;
13561 +       struct super_block *sb;
13562 +       struct inode *h_inode;
13563 +       int (*f)(struct inode *inode);
13564 +
13565 +       sb = inode->i_sb;
13566 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13567 +       ii_write_lock_child(inode);
13568 +       bindex = au_ibtop(inode);
13569 +       AuDebugOn(bindex < 0);
13570 +       h_inode = au_h_iptr(inode, bindex);
13571 +
13572 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13573 +       if (f)
13574 +               err = f(h_inode);
13575 +       else
13576 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13577 +
13578 +       au_cpup_attr_timesizes(inode);
13579 +       ii_write_unlock(inode);
13580 +       si_read_unlock(sb);
13581 +       return err;
13582 +}
13583 +
13584 +/* ---------------------------------------------------------------------- */
13585 +
13586 +static struct export_operations aufs_export_op = {
13587 +       .fh_to_dentry           = aufs_fh_to_dentry,
13588 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13589 +       .encode_fh              = aufs_encode_fh,
13590 +       .commit_metadata        = aufs_commit_metadata
13591 +};
13592 +
13593 +void au_export_init(struct super_block *sb)
13594 +{
13595 +       struct au_sbinfo *sbinfo;
13596 +       __u32 u;
13597 +
13598 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13599 +                        && IS_MODULE(CONFIG_EXPORTFS),
13600 +                        AUFS_NAME ": unsupported configuration "
13601 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13602 +
13603 +       sb->s_export_op = &aufs_export_op;
13604 +       sbinfo = au_sbi(sb);
13605 +       sbinfo->si_xigen = NULL;
13606 +       get_random_bytes(&u, sizeof(u));
13607 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13608 +       atomic_set(&sbinfo->si_xigen_next, u);
13609 +}
13610 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13611 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13612 +++ linux/fs/aufs/fhsm.c        2022-11-05 23:02:18.962555950 +0100
13613 @@ -0,0 +1,426 @@
13614 +// SPDX-License-Identifier: GPL-2.0
13615 +/*
13616 + * Copyright (C) 2011-2022 Junjiro R. Okajima
13617 + *
13618 + * This program is free software; you can redistribute it and/or modify
13619 + * it under the terms of the GNU General Public License as published by
13620 + * the Free Software Foundation; either version 2 of the License, or
13621 + * (at your option) any later version.
13622 + *
13623 + * This program is distributed in the hope that it will be useful,
13624 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13625 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13626 + * GNU General Public License for more details.
13627 + *
13628 + * You should have received a copy of the GNU General Public License
13629 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13630 + */
13631 +
13632 +/*
13633 + * File-based Hierarchy Storage Management
13634 + */
13635 +
13636 +#include <linux/anon_inodes.h>
13637 +#include <linux/poll.h>
13638 +#include <linux/seq_file.h>
13639 +#include <linux/statfs.h>
13640 +#include "aufs.h"
13641 +
13642 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13643 +{
13644 +       struct au_sbinfo *sbinfo;
13645 +       struct au_fhsm *fhsm;
13646 +
13647 +       SiMustAnyLock(sb);
13648 +
13649 +       sbinfo = au_sbi(sb);
13650 +       fhsm = &sbinfo->si_fhsm;
13651 +       AuDebugOn(!fhsm);
13652 +       return fhsm->fhsm_bottom;
13653 +}
13654 +
13655 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13656 +{
13657 +       struct au_sbinfo *sbinfo;
13658 +       struct au_fhsm *fhsm;
13659 +
13660 +       SiMustWriteLock(sb);
13661 +
13662 +       sbinfo = au_sbi(sb);
13663 +       fhsm = &sbinfo->si_fhsm;
13664 +       AuDebugOn(!fhsm);
13665 +       fhsm->fhsm_bottom = bindex;
13666 +}
13667 +
13668 +/* ---------------------------------------------------------------------- */
13669 +
13670 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13671 +{
13672 +       struct au_br_fhsm *bf;
13673 +
13674 +       bf = br->br_fhsm;
13675 +       MtxMustLock(&bf->bf_lock);
13676 +
13677 +       return !bf->bf_readable
13678 +               || time_after(jiffies,
13679 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13680 +}
13681 +
13682 +/* ---------------------------------------------------------------------- */
13683 +
13684 +static void au_fhsm_notify(struct super_block *sb, int val)
13685 +{
13686 +       struct au_sbinfo *sbinfo;
13687 +       struct au_fhsm *fhsm;
13688 +
13689 +       SiMustAnyLock(sb);
13690 +
13691 +       sbinfo = au_sbi(sb);
13692 +       fhsm = &sbinfo->si_fhsm;
13693 +       if (au_fhsm_pid(fhsm)
13694 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13695 +               atomic_set(&fhsm->fhsm_readable, val);
13696 +               if (val)
13697 +                       wake_up(&fhsm->fhsm_wqh);
13698 +       }
13699 +}
13700 +
13701 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13702 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13703 +{
13704 +       int err;
13705 +       struct au_branch *br;
13706 +       struct au_br_fhsm *bf;
13707 +
13708 +       br = au_sbr(sb, bindex);
13709 +       AuDebugOn(au_br_rdonly(br));
13710 +       bf = br->br_fhsm;
13711 +       AuDebugOn(!bf);
13712 +
13713 +       if (do_lock)
13714 +               mutex_lock(&bf->bf_lock);
13715 +       else
13716 +               MtxMustLock(&bf->bf_lock);
13717 +
13718 +       /* sb->s_root for NFS is unreliable */
13719 +       err = au_br_stfs(br, &bf->bf_stfs);
13720 +       if (unlikely(err)) {
13721 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13722 +               goto out;
13723 +       }
13724 +
13725 +       bf->bf_jiffy = jiffies;
13726 +       bf->bf_readable = 1;
13727 +       if (do_notify)
13728 +               au_fhsm_notify(sb, /*val*/1);
13729 +       if (rstfs)
13730 +               *rstfs = bf->bf_stfs;
13731 +
13732 +out:
13733 +       if (do_lock)
13734 +               mutex_unlock(&bf->bf_lock);
13735 +       au_fhsm_notify(sb, /*val*/1);
13736 +
13737 +       return err;
13738 +}
13739 +
13740 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13741 +{
13742 +       int err;
13743 +       struct au_sbinfo *sbinfo;
13744 +       struct au_fhsm *fhsm;
13745 +       struct au_branch *br;
13746 +       struct au_br_fhsm *bf;
13747 +
13748 +       AuDbg("b%d, force %d\n", bindex, force);
13749 +       SiMustAnyLock(sb);
13750 +
13751 +       sbinfo = au_sbi(sb);
13752 +       fhsm = &sbinfo->si_fhsm;
13753 +       if (!au_ftest_si(sbinfo, FHSM)
13754 +           || fhsm->fhsm_bottom == bindex)
13755 +               return;
13756 +
13757 +       br = au_sbr(sb, bindex);
13758 +       bf = br->br_fhsm;
13759 +       AuDebugOn(!bf);
13760 +       mutex_lock(&bf->bf_lock);
13761 +       if (force
13762 +           || au_fhsm_pid(fhsm)
13763 +           || au_fhsm_test_jiffy(sbinfo, br))
13764 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13765 +                                 /*do_notify*/1);
13766 +       mutex_unlock(&bf->bf_lock);
13767 +}
13768 +
13769 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13770 +{
13771 +       aufs_bindex_t bindex, bbot;
13772 +       struct au_branch *br;
13773 +
13774 +       /* exclude the bottom */
13775 +       bbot = au_fhsm_bottom(sb);
13776 +       for (bindex = 0; bindex < bbot; bindex++) {
13777 +               br = au_sbr(sb, bindex);
13778 +               if (au_br_fhsm(br->br_perm))
13779 +                       au_fhsm_wrote(sb, bindex, force);
13780 +       }
13781 +}
13782 +
13783 +/* ---------------------------------------------------------------------- */
13784 +
13785 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13786 +{
13787 +       __poll_t mask;
13788 +       struct au_sbinfo *sbinfo;
13789 +       struct au_fhsm *fhsm;
13790 +
13791 +       mask = 0;
13792 +       sbinfo = file->private_data;
13793 +       fhsm = &sbinfo->si_fhsm;
13794 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13795 +       if (atomic_read(&fhsm->fhsm_readable))
13796 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13797 +
13798 +       if (!mask)
13799 +               AuDbg("mask 0x%x\n", mask);
13800 +       return mask;
13801 +}
13802 +
13803 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13804 +                             struct aufs_stfs *stfs, __s16 brid)
13805 +{
13806 +       int err;
13807 +
13808 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13809 +       if (!err)
13810 +               err = __put_user(brid, &stbr->brid);
13811 +       if (unlikely(err))
13812 +               err = -EFAULT;
13813 +
13814 +       return err;
13815 +}
13816 +
13817 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13818 +                              struct aufs_stbr __user *stbr, size_t count)
13819 +{
13820 +       ssize_t err;
13821 +       int nstbr;
13822 +       aufs_bindex_t bindex, bbot;
13823 +       struct au_branch *br;
13824 +       struct au_br_fhsm *bf;
13825 +
13826 +       /* except the bottom branch */
13827 +       err = 0;
13828 +       nstbr = 0;
13829 +       bbot = au_fhsm_bottom(sb);
13830 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13831 +               br = au_sbr(sb, bindex);
13832 +               if (!au_br_fhsm(br->br_perm))
13833 +                       continue;
13834 +
13835 +               bf = br->br_fhsm;
13836 +               mutex_lock(&bf->bf_lock);
13837 +               if (bf->bf_readable) {
13838 +                       err = -EFAULT;
13839 +                       if (count >= sizeof(*stbr))
13840 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13841 +                                                         br->br_id);
13842 +                       if (!err) {
13843 +                               bf->bf_readable = 0;
13844 +                               count -= sizeof(*stbr);
13845 +                               nstbr++;
13846 +                       }
13847 +               }
13848 +               mutex_unlock(&bf->bf_lock);
13849 +       }
13850 +       if (!err)
13851 +               err = sizeof(*stbr) * nstbr;
13852 +
13853 +       return err;
13854 +}
13855 +
13856 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13857 +                          loff_t *pos)
13858 +{
13859 +       ssize_t err;
13860 +       int readable;
13861 +       aufs_bindex_t nfhsm, bindex, bbot;
13862 +       struct au_sbinfo *sbinfo;
13863 +       struct au_fhsm *fhsm;
13864 +       struct au_branch *br;
13865 +       struct super_block *sb;
13866 +
13867 +       err = 0;
13868 +       sbinfo = file->private_data;
13869 +       fhsm = &sbinfo->si_fhsm;
13870 +need_data:
13871 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13872 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13873 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13874 +                       err = -EAGAIN;
13875 +               else
13876 +                       err = wait_event_interruptible_locked_irq
13877 +                               (fhsm->fhsm_wqh,
13878 +                                atomic_read(&fhsm->fhsm_readable));
13879 +       }
13880 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13881 +       if (unlikely(err))
13882 +               goto out;
13883 +
13884 +       /* sb may already be dead */
13885 +       au_rw_read_lock(&sbinfo->si_rwsem);
13886 +       readable = atomic_read(&fhsm->fhsm_readable);
13887 +       if (readable > 0) {
13888 +               sb = sbinfo->si_sb;
13889 +               AuDebugOn(!sb);
13890 +               /* exclude the bottom branch */
13891 +               nfhsm = 0;
13892 +               bbot = au_fhsm_bottom(sb);
13893 +               for (bindex = 0; bindex < bbot; bindex++) {
13894 +                       br = au_sbr(sb, bindex);
13895 +                       if (au_br_fhsm(br->br_perm))
13896 +                               nfhsm++;
13897 +               }
13898 +               err = -EMSGSIZE;
13899 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13900 +                       atomic_set(&fhsm->fhsm_readable, 0);
13901 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13902 +                                            count);
13903 +               }
13904 +       }
13905 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13906 +       if (!readable)
13907 +               goto need_data;
13908 +
13909 +out:
13910 +       return err;
13911 +}
13912 +
13913 +static int au_fhsm_release(struct inode *inode, struct file *file)
13914 +{
13915 +       struct au_sbinfo *sbinfo;
13916 +       struct au_fhsm *fhsm;
13917 +
13918 +       /* sb may already be dead */
13919 +       sbinfo = file->private_data;
13920 +       fhsm = &sbinfo->si_fhsm;
13921 +       spin_lock(&fhsm->fhsm_spin);
13922 +       fhsm->fhsm_pid = 0;
13923 +       spin_unlock(&fhsm->fhsm_spin);
13924 +       kobject_put(&sbinfo->si_kobj);
13925 +
13926 +       return 0;
13927 +}
13928 +
13929 +static const struct file_operations au_fhsm_fops = {
13930 +       .owner          = THIS_MODULE,
13931 +       .llseek         = noop_llseek,
13932 +       .read           = au_fhsm_read,
13933 +       .poll           = au_fhsm_poll,
13934 +       .release        = au_fhsm_release
13935 +};
13936 +
13937 +int au_fhsm_fd(struct super_block *sb, int oflags)
13938 +{
13939 +       int err, fd;
13940 +       struct au_sbinfo *sbinfo;
13941 +       struct au_fhsm *fhsm;
13942 +
13943 +       err = -EPERM;
13944 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13945 +               goto out;
13946 +
13947 +       err = -EINVAL;
13948 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13949 +               goto out;
13950 +
13951 +       err = 0;
13952 +       sbinfo = au_sbi(sb);
13953 +       fhsm = &sbinfo->si_fhsm;
13954 +       spin_lock(&fhsm->fhsm_spin);
13955 +       if (!fhsm->fhsm_pid)
13956 +               fhsm->fhsm_pid = current->pid;
13957 +       else
13958 +               err = -EBUSY;
13959 +       spin_unlock(&fhsm->fhsm_spin);
13960 +       if (unlikely(err))
13961 +               goto out;
13962 +
13963 +       oflags |= O_RDONLY;
13964 +       /* oflags |= FMODE_NONOTIFY; */
13965 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13966 +       err = fd;
13967 +       if (unlikely(fd < 0))
13968 +               goto out_pid;
13969 +
13970 +       /* succeed regardless 'fhsm' status */
13971 +       kobject_get(&sbinfo->si_kobj);
13972 +       si_noflush_read_lock(sb);
13973 +       if (au_ftest_si(sbinfo, FHSM))
13974 +               au_fhsm_wrote_all(sb, /*force*/0);
13975 +       si_read_unlock(sb);
13976 +       goto out; /* success */
13977 +
13978 +out_pid:
13979 +       spin_lock(&fhsm->fhsm_spin);
13980 +       fhsm->fhsm_pid = 0;
13981 +       spin_unlock(&fhsm->fhsm_spin);
13982 +out:
13983 +       AuTraceErr(err);
13984 +       return err;
13985 +}
13986 +
13987 +/* ---------------------------------------------------------------------- */
13988 +
13989 +int au_fhsm_br_alloc(struct au_branch *br)
13990 +{
13991 +       int err;
13992 +
13993 +       err = 0;
13994 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
13995 +       if (br->br_fhsm)
13996 +               au_br_fhsm_init(br->br_fhsm);
13997 +       else
13998 +               err = -ENOMEM;
13999 +
14000 +       return err;
14001 +}
14002 +
14003 +/* ---------------------------------------------------------------------- */
14004 +
14005 +void au_fhsm_fin(struct super_block *sb)
14006 +{
14007 +       au_fhsm_notify(sb, /*val*/-1);
14008 +}
14009 +
14010 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14011 +{
14012 +       struct au_fhsm *fhsm;
14013 +
14014 +       fhsm = &sbinfo->si_fhsm;
14015 +       spin_lock_init(&fhsm->fhsm_spin);
14016 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14017 +       atomic_set(&fhsm->fhsm_readable, 0);
14018 +       fhsm->fhsm_expire
14019 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14020 +       fhsm->fhsm_bottom = -1;
14021 +}
14022 +
14023 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14024 +{
14025 +       sbinfo->si_fhsm.fhsm_expire
14026 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14027 +}
14028 +
14029 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14030 +{
14031 +       unsigned int u;
14032 +
14033 +       if (!au_ftest_si(sbinfo, FHSM))
14034 +               return;
14035 +
14036 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14037 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14038 +               seq_printf(seq, ",fhsm_sec=%u", u);
14039 +}
14040 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14041 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14042 +++ linux/fs/aufs/file.c        2023-10-31 09:31:04.199880750 +0100
14043 @@ -0,0 +1,865 @@
14044 +// SPDX-License-Identifier: GPL-2.0
14045 +/*
14046 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14047 + *
14048 + * This program is free software; you can redistribute it and/or modify
14049 + * it under the terms of the GNU General Public License as published by
14050 + * the Free Software Foundation; either version 2 of the License, or
14051 + * (at your option) any later version.
14052 + *
14053 + * This program is distributed in the hope that it will be useful,
14054 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14055 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14056 + * GNU General Public License for more details.
14057 + *
14058 + * You should have received a copy of the GNU General Public License
14059 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14060 + */
14061 +
14062 +/*
14063 + * handling file/dir, and address_space operation
14064 + */
14065 +
14066 +#ifdef CONFIG_AUFS_DEBUG
14067 +#include <linux/migrate.h>
14068 +#endif
14069 +#include <linux/pagemap.h>
14070 +#include "aufs.h"
14071 +
14072 +/* drop flags for writing */
14073 +unsigned int au_file_roflags(unsigned int flags)
14074 +{
14075 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14076 +       flags |= O_RDONLY | O_NOATIME;
14077 +       return flags;
14078 +}
14079 +
14080 +/* common functions to regular file and dir */
14081 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14082 +                      struct file *file, int force_wr)
14083 +{
14084 +       struct file *h_file;
14085 +       struct dentry *h_dentry;
14086 +       struct inode *h_inode;
14087 +       struct super_block *sb;
14088 +       struct au_branch *br;
14089 +       struct path h_path;
14090 +       int err;
14091 +
14092 +       /* a race condition can happen between open and unlink/rmdir */
14093 +       h_file = ERR_PTR(-ENOENT);
14094 +       h_dentry = au_h_dptr(dentry, bindex);
14095 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14096 +               goto out;
14097 +       h_inode = d_inode(h_dentry);
14098 +       spin_lock(&h_dentry->d_lock);
14099 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14100 +               /* || !d_inode(dentry)->i_nlink */
14101 +               ;
14102 +       spin_unlock(&h_dentry->d_lock);
14103 +       if (unlikely(err))
14104 +               goto out;
14105 +
14106 +       sb = dentry->d_sb;
14107 +       br = au_sbr(sb, bindex);
14108 +       err = au_br_test_oflag(flags, br);
14109 +       h_file = ERR_PTR(err);
14110 +       if (unlikely(err))
14111 +               goto out;
14112 +
14113 +       /* drop flags for writing */
14114 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14115 +               if (force_wr && !(flags & O_WRONLY))
14116 +                       force_wr = 0;
14117 +               flags = au_file_roflags(flags);
14118 +               if (force_wr) {
14119 +                       h_file = ERR_PTR(-EROFS);
14120 +                       flags = au_file_roflags(flags);
14121 +                       if (unlikely(vfsub_native_ro(h_inode)
14122 +                                    || IS_APPEND(h_inode)))
14123 +                               goto out;
14124 +                       flags &= ~O_ACCMODE;
14125 +                       flags |= O_WRONLY;
14126 +               }
14127 +       }
14128 +       flags &= ~O_CREAT;
14129 +       au_lcnt_inc(&br->br_nfiles);
14130 +       h_path.dentry = h_dentry;
14131 +       h_path.mnt = au_br_mnt(br);
14132 +       /*
14133 +        * vfs::backing_file_open() looks promising since it can get rid of
14134 +        * mm::vm_prfile approach from my mind.
14135 +        * but I keep current approach for a while.
14136 +        */
14137 +       h_file = vfsub_dentry_open(&h_path, flags);
14138 +       if (IS_ERR(h_file))
14139 +               goto out_br;
14140 +
14141 +       if (flags & __FMODE_EXEC) {
14142 +               err = deny_write_access(h_file);
14143 +               if (unlikely(err)) {
14144 +                       fput(h_file);
14145 +                       h_file = ERR_PTR(err);
14146 +                       goto out_br;
14147 +               }
14148 +       }
14149 +       fsnotify_open(h_file);
14150 +       goto out; /* success */
14151 +
14152 +out_br:
14153 +       au_lcnt_dec(&br->br_nfiles);
14154 +out:
14155 +       return h_file;
14156 +}
14157 +
14158 +static int au_cmoo(struct dentry *dentry)
14159 +{
14160 +       int err, cmoo, matched;
14161 +       unsigned int udba;
14162 +       struct path h_path;
14163 +       struct au_pin pin;
14164 +       struct au_cp_generic cpg = {
14165 +               .dentry = dentry,
14166 +               .bdst   = -1,
14167 +               .bsrc   = -1,
14168 +               .len    = -1,
14169 +               .pin    = &pin,
14170 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14171 +       };
14172 +       struct inode *delegated;
14173 +       struct super_block *sb;
14174 +       struct au_sbinfo *sbinfo;
14175 +       struct au_fhsm *fhsm;
14176 +       pid_t pid;
14177 +       struct au_branch *br;
14178 +       struct dentry *parent;
14179 +       struct au_hinode *hdir;
14180 +
14181 +       DiMustWriteLock(dentry);
14182 +       IiMustWriteLock(d_inode(dentry));
14183 +
14184 +       err = 0;
14185 +       if (IS_ROOT(dentry))
14186 +               goto out;
14187 +       cpg.bsrc = au_dbtop(dentry);
14188 +       if (!cpg.bsrc)
14189 +               goto out;
14190 +
14191 +       sb = dentry->d_sb;
14192 +       sbinfo = au_sbi(sb);
14193 +       fhsm = &sbinfo->si_fhsm;
14194 +       pid = au_fhsm_pid(fhsm);
14195 +       rcu_read_lock();
14196 +       matched = (pid
14197 +                  && (current->pid == pid
14198 +                      || rcu_dereference(current->real_parent)->pid == pid));
14199 +       rcu_read_unlock();
14200 +       if (matched)
14201 +               goto out;
14202 +
14203 +       br = au_sbr(sb, cpg.bsrc);
14204 +       cmoo = au_br_cmoo(br->br_perm);
14205 +       if (!cmoo)
14206 +               goto out;
14207 +       if (!d_is_reg(dentry))
14208 +               cmoo &= AuBrAttr_COO_ALL;
14209 +       if (!cmoo)
14210 +               goto out;
14211 +
14212 +       parent = dget_parent(dentry);
14213 +       di_write_lock_parent(parent);
14214 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14215 +       cpg.bdst = err;
14216 +       if (unlikely(err < 0)) {
14217 +               err = 0;        /* there is no upper writable branch */
14218 +               goto out_dgrade;
14219 +       }
14220 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14221 +
14222 +       /* do not respect the coo attrib for the target branch */
14223 +       err = au_cpup_dirs(dentry, cpg.bdst);
14224 +       if (unlikely(err))
14225 +               goto out_dgrade;
14226 +
14227 +       di_downgrade_lock(parent, AuLock_IR);
14228 +       udba = au_opt_udba(sb);
14229 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14230 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14231 +       if (unlikely(err))
14232 +               goto out_parent;
14233 +
14234 +       err = au_sio_cpup_simple(&cpg);
14235 +       au_unpin(&pin);
14236 +       if (unlikely(err))
14237 +               goto out_parent;
14238 +       if (!(cmoo & AuBrWAttr_MOO))
14239 +               goto out_parent; /* success */
14240 +
14241 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14242 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14243 +       if (unlikely(err))
14244 +               goto out_parent;
14245 +
14246 +       h_path.mnt = au_br_mnt(br);
14247 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14248 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14249 +       delegated = NULL;
14250 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14251 +       au_unpin(&pin);
14252 +       /* todo: keep h_dentry or not? */
14253 +       if (unlikely(err == -EWOULDBLOCK)) {
14254 +               pr_warn("cannot retry for NFSv4 delegation"
14255 +                       " for an internal unlink\n");
14256 +               iput(delegated);
14257 +       }
14258 +       if (unlikely(err)) {
14259 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14260 +                      dentry, err);
14261 +               err = 0;
14262 +       }
14263 +       goto out_parent; /* success */
14264 +
14265 +out_dgrade:
14266 +       di_downgrade_lock(parent, AuLock_IR);
14267 +out_parent:
14268 +       di_read_unlock(parent, AuLock_IR);
14269 +       dput(parent);
14270 +out:
14271 +       AuTraceErr(err);
14272 +       return err;
14273 +}
14274 +
14275 +int au_do_open(struct file *file, struct au_do_open_args *args)
14276 +{
14277 +       int err, aopen = args->aopen;
14278 +       struct dentry *dentry;
14279 +       struct au_finfo *finfo;
14280 +
14281 +       if (!aopen)
14282 +               err = au_finfo_init(file, args->fidir);
14283 +       else {
14284 +               lockdep_off();
14285 +               err = au_finfo_init(file, args->fidir);
14286 +               lockdep_on();
14287 +       }
14288 +       if (unlikely(err))
14289 +               goto out;
14290 +
14291 +       dentry = file->f_path.dentry;
14292 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14293 +       di_write_lock_child(dentry);
14294 +       err = au_cmoo(dentry);
14295 +       if (!err) {
14296 +               if (!aopen) {
14297 +                       err = args->open(file, vfsub_file_flags(file),
14298 +                                        au_di(dentry)->di_htmpfile);
14299 +                       di_write_unlock(dentry);
14300 +               } else {
14301 +                       di_downgrade_lock(dentry, AuLock_IR);
14302 +                       lockdep_off();
14303 +                       err = args->open(file, vfsub_file_flags(file),
14304 +                                        args->h_file);
14305 +                       lockdep_on();
14306 +                       di_read_unlock(dentry, AuLock_IR);
14307 +               }
14308 +       }
14309 +
14310 +       finfo = au_fi(file);
14311 +       if (!err) {
14312 +               finfo->fi_file = file;
14313 +               au_hbl_add(&finfo->fi_hlist,
14314 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14315 +       }
14316 +       if (!aopen)
14317 +               fi_write_unlock(file);
14318 +       else {
14319 +               lockdep_off();
14320 +               fi_write_unlock(file);
14321 +               lockdep_on();
14322 +       }
14323 +       if (unlikely(err)) {
14324 +               finfo->fi_hdir = NULL;
14325 +               au_finfo_fin(file);
14326 +       }
14327 +
14328 +out:
14329 +       AuTraceErr(err);
14330 +       return err;
14331 +}
14332 +
14333 +int au_reopen_nondir(struct file *file)
14334 +{
14335 +       int err;
14336 +       aufs_bindex_t btop;
14337 +       struct dentry *dentry;
14338 +       struct au_branch *br;
14339 +       struct file *h_file, *h_file_tmp;
14340 +
14341 +       dentry = file->f_path.dentry;
14342 +       btop = au_dbtop(dentry);
14343 +       br = au_sbr(dentry->d_sb, btop);
14344 +       h_file_tmp = NULL;
14345 +       if (au_fbtop(file) == btop) {
14346 +               h_file = au_hf_top(file);
14347 +               if (file->f_mode == h_file->f_mode)
14348 +                       return 0; /* success */
14349 +               h_file_tmp = h_file;
14350 +               get_file(h_file_tmp);
14351 +               au_lcnt_inc(&br->br_nfiles);
14352 +               au_set_h_fptr(file, btop, NULL);
14353 +       }
14354 +       AuDebugOn(au_fi(file)->fi_hdir);
14355 +       /*
14356 +        * it can happen
14357 +        * file exists on both of rw and ro
14358 +        * open --> dbtop and fbtop are both 0
14359 +        * prepend a branch as rw, "rw" become ro
14360 +        * remove rw/file
14361 +        * delete the top branch, "rw" becomes rw again
14362 +        *      --> dbtop is 1, fbtop is still 0
14363 +        * write --> fbtop is 0 but dbtop is 1
14364 +        */
14365 +       /* AuDebugOn(au_fbtop(file) < btop); */
14366 +
14367 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14368 +                          file, /*force_wr*/0);
14369 +       err = PTR_ERR(h_file);
14370 +       if (IS_ERR(h_file)) {
14371 +               if (h_file_tmp) {
14372 +                       /* revert */
14373 +                       au_set_h_fptr(file, btop, h_file_tmp);
14374 +                       h_file_tmp = NULL;
14375 +               }
14376 +               goto out; /* todo: close all? */
14377 +       }
14378 +
14379 +       err = 0;
14380 +       au_set_fbtop(file, btop);
14381 +       au_set_h_fptr(file, btop, h_file);
14382 +       au_update_figen(file);
14383 +       /* todo: necessary? */
14384 +       /* file->f_ra = h_file->f_ra; */
14385 +
14386 +out:
14387 +       if (h_file_tmp) {
14388 +               fput(h_file_tmp);
14389 +               au_lcnt_dec(&br->br_nfiles);
14390 +       }
14391 +       return err;
14392 +}
14393 +
14394 +/* ---------------------------------------------------------------------- */
14395 +
14396 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14397 +                       struct dentry *hi_wh)
14398 +{
14399 +       int err;
14400 +       aufs_bindex_t btop;
14401 +       struct au_dinfo *dinfo;
14402 +       struct dentry *h_dentry;
14403 +       struct au_hdentry *hdp;
14404 +
14405 +       dinfo = au_di(file->f_path.dentry);
14406 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14407 +
14408 +       btop = dinfo->di_btop;
14409 +       dinfo->di_btop = btgt;
14410 +       hdp = au_hdentry(dinfo, btgt);
14411 +       h_dentry = hdp->hd_dentry;
14412 +       hdp->hd_dentry = hi_wh;
14413 +       err = au_reopen_nondir(file);
14414 +       hdp->hd_dentry = h_dentry;
14415 +       dinfo->di_btop = btop;
14416 +
14417 +       return err;
14418 +}
14419 +
14420 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14421 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14422 +{
14423 +       int err;
14424 +       struct inode *inode, *h_inode;
14425 +       struct dentry *h_dentry, *hi_wh;
14426 +       struct au_cp_generic cpg = {
14427 +               .dentry = file->f_path.dentry,
14428 +               .bdst   = bcpup,
14429 +               .bsrc   = -1,
14430 +               .len    = len,
14431 +               .pin    = pin
14432 +       };
14433 +
14434 +       au_update_dbtop(cpg.dentry);
14435 +       inode = d_inode(cpg.dentry);
14436 +       h_inode = NULL;
14437 +       if (au_dbtop(cpg.dentry) <= bcpup
14438 +           && au_dbbot(cpg.dentry) >= bcpup) {
14439 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14440 +               if (h_dentry && d_is_positive(h_dentry))
14441 +                       h_inode = d_inode(h_dentry);
14442 +       }
14443 +       hi_wh = au_hi_wh(inode, bcpup);
14444 +       if (!hi_wh && !h_inode)
14445 +               err = au_sio_cpup_wh(&cpg, file);
14446 +       else
14447 +               /* already copied-up after unlink */
14448 +               err = au_reopen_wh(file, bcpup, hi_wh);
14449 +
14450 +       if (!err
14451 +           && (inode->i_nlink > 1
14452 +               || (inode->i_state & I_LINKABLE))
14453 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14454 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14455 +
14456 +       return err;
14457 +}
14458 +
14459 +/*
14460 + * prepare the @file for writing.
14461 + */
14462 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14463 +{
14464 +       int err;
14465 +       aufs_bindex_t dbtop;
14466 +       struct dentry *parent;
14467 +       struct inode *inode;
14468 +       struct super_block *sb;
14469 +       struct file *h_file;
14470 +       struct au_cp_generic cpg = {
14471 +               .dentry = file->f_path.dentry,
14472 +               .bdst   = -1,
14473 +               .bsrc   = -1,
14474 +               .len    = len,
14475 +               .pin    = pin,
14476 +               .flags  = AuCpup_DTIME
14477 +       };
14478 +
14479 +       sb = cpg.dentry->d_sb;
14480 +       inode = d_inode(cpg.dentry);
14481 +       cpg.bsrc = au_fbtop(file);
14482 +       err = au_test_ro(sb, cpg.bsrc, inode);
14483 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14484 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14485 +                            /*flags*/0);
14486 +               goto out;
14487 +       }
14488 +
14489 +       /* need to cpup or reopen */
14490 +       parent = dget_parent(cpg.dentry);
14491 +       di_write_lock_parent(parent);
14492 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14493 +       cpg.bdst = err;
14494 +       if (unlikely(err < 0))
14495 +               goto out_dgrade;
14496 +       err = 0;
14497 +
14498 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14499 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14500 +               if (unlikely(err))
14501 +                       goto out_dgrade;
14502 +       }
14503 +
14504 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14505 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14506 +       if (unlikely(err))
14507 +               goto out_dgrade;
14508 +
14509 +       dbtop = au_dbtop(cpg.dentry);
14510 +       if (dbtop <= cpg.bdst)
14511 +               cpg.bsrc = cpg.bdst;
14512 +
14513 +       if (dbtop <= cpg.bdst           /* just reopen */
14514 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14515 +               ) {
14516 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14517 +               if (IS_ERR(h_file))
14518 +                       err = PTR_ERR(h_file);
14519 +               else {
14520 +                       di_downgrade_lock(parent, AuLock_IR);
14521 +                       if (dbtop > cpg.bdst)
14522 +                               err = au_sio_cpup_simple(&cpg);
14523 +                       if (!err)
14524 +                               err = au_reopen_nondir(file);
14525 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14526 +               }
14527 +       } else {                        /* copyup as wh and reopen */
14528 +               /*
14529 +                * since writable hfsplus branch is not supported,
14530 +                * h_open_pre/post() are unnecessary.
14531 +                */
14532 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14533 +               di_downgrade_lock(parent, AuLock_IR);
14534 +       }
14535 +
14536 +       if (!err) {
14537 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14538 +               goto out_dput; /* success */
14539 +       }
14540 +       au_unpin(pin);
14541 +       goto out_unlock;
14542 +
14543 +out_dgrade:
14544 +       di_downgrade_lock(parent, AuLock_IR);
14545 +out_unlock:
14546 +       di_read_unlock(parent, AuLock_IR);
14547 +out_dput:
14548 +       dput(parent);
14549 +out:
14550 +       return err;
14551 +}
14552 +
14553 +/* ---------------------------------------------------------------------- */
14554 +
14555 +int au_do_flush(struct file *file, fl_owner_t id,
14556 +               int (*flush)(struct file *file, fl_owner_t id))
14557 +{
14558 +       int err;
14559 +       struct super_block *sb;
14560 +       struct inode *inode;
14561 +
14562 +       inode = file_inode(file);
14563 +       sb = inode->i_sb;
14564 +       si_noflush_read_lock(sb);
14565 +       fi_read_lock(file);
14566 +       ii_read_lock_child(inode);
14567 +
14568 +       err = flush(file, id);
14569 +       au_cpup_attr_timesizes(inode);
14570 +
14571 +       ii_read_unlock(inode);
14572 +       fi_read_unlock(file);
14573 +       si_read_unlock(sb);
14574 +       return err;
14575 +}
14576 +
14577 +/* ---------------------------------------------------------------------- */
14578 +
14579 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14580 +{
14581 +       int err;
14582 +       struct au_pin pin;
14583 +       struct au_finfo *finfo;
14584 +       struct dentry *parent, *hi_wh;
14585 +       struct inode *inode;
14586 +       struct super_block *sb;
14587 +       struct au_cp_generic cpg = {
14588 +               .dentry = file->f_path.dentry,
14589 +               .bdst   = -1,
14590 +               .bsrc   = -1,
14591 +               .len    = -1,
14592 +               .pin    = &pin,
14593 +               .flags  = AuCpup_DTIME
14594 +       };
14595 +
14596 +       FiMustWriteLock(file);
14597 +
14598 +       err = 0;
14599 +       finfo = au_fi(file);
14600 +       sb = cpg.dentry->d_sb;
14601 +       inode = d_inode(cpg.dentry);
14602 +       cpg.bdst = au_ibtop(inode);
14603 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14604 +               goto out;
14605 +
14606 +       parent = dget_parent(cpg.dentry);
14607 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14608 +               di_read_lock_parent(parent, !AuLock_IR);
14609 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14610 +               cpg.bdst = err;
14611 +               di_read_unlock(parent, !AuLock_IR);
14612 +               if (unlikely(err < 0))
14613 +                       goto out_parent;
14614 +               err = 0;
14615 +       }
14616 +
14617 +       di_read_lock_parent(parent, AuLock_IR);
14618 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14619 +       if (!S_ISDIR(inode->i_mode)
14620 +           && au_opt_test(au_mntflags(sb), PLINK)
14621 +           && au_plink_test(inode)
14622 +           && !d_unhashed(cpg.dentry)
14623 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14624 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14625 +               if (unlikely(err))
14626 +                       goto out_unlock;
14627 +
14628 +               /* always superio. */
14629 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14630 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14631 +               if (!err) {
14632 +                       err = au_sio_cpup_simple(&cpg);
14633 +                       au_unpin(&pin);
14634 +               }
14635 +       } else if (hi_wh) {
14636 +               /* already copied-up after unlink */
14637 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14638 +               *need_reopen = 0;
14639 +       }
14640 +
14641 +out_unlock:
14642 +       di_read_unlock(parent, AuLock_IR);
14643 +out_parent:
14644 +       dput(parent);
14645 +out:
14646 +       return err;
14647 +}
14648 +
14649 +static void au_do_refresh_dir(struct file *file)
14650 +{
14651 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14652 +       struct au_hfile *p, tmp, *q;
14653 +       struct au_finfo *finfo;
14654 +       struct super_block *sb;
14655 +       struct au_fidir *fidir;
14656 +
14657 +       FiMustWriteLock(file);
14658 +
14659 +       sb = file->f_path.dentry->d_sb;
14660 +       finfo = au_fi(file);
14661 +       fidir = finfo->fi_hdir;
14662 +       AuDebugOn(!fidir);
14663 +       p = fidir->fd_hfile + finfo->fi_btop;
14664 +       brid = p->hf_br->br_id;
14665 +       bbot = fidir->fd_bbot;
14666 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14667 +               if (!p->hf_file)
14668 +                       continue;
14669 +
14670 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14671 +               if (new_bindex == bindex)
14672 +                       continue;
14673 +               if (new_bindex < 0) {
14674 +                       au_set_h_fptr(file, bindex, NULL);
14675 +                       continue;
14676 +               }
14677 +
14678 +               /* swap two lower inode, and loop again */
14679 +               q = fidir->fd_hfile + new_bindex;
14680 +               tmp = *q;
14681 +               *q = *p;
14682 +               *p = tmp;
14683 +               if (tmp.hf_file) {
14684 +                       bindex--;
14685 +                       p--;
14686 +               }
14687 +       }
14688 +
14689 +       p = fidir->fd_hfile;
14690 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14691 +               bbot = au_sbbot(sb);
14692 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14693 +                    finfo->fi_btop++, p++)
14694 +                       if (p->hf_file) {
14695 +                               if (file_inode(p->hf_file))
14696 +                                       break;
14697 +                               au_hfput(p, /*execed*/0);
14698 +                       }
14699 +       } else {
14700 +               bbot = au_br_index(sb, brid);
14701 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14702 +                    finfo->fi_btop++, p++)
14703 +                       if (p->hf_file)
14704 +                               au_hfput(p, /*execed*/0);
14705 +               bbot = au_sbbot(sb);
14706 +       }
14707 +
14708 +       p = fidir->fd_hfile + bbot;
14709 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14710 +            fidir->fd_bbot--, p--)
14711 +               if (p->hf_file) {
14712 +                       if (file_inode(p->hf_file))
14713 +                               break;
14714 +                       au_hfput(p, /*execed*/0);
14715 +               }
14716 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14717 +}
14718 +
14719 +/*
14720 + * after branch manipulating, refresh the file.
14721 + */
14722 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14723 +{
14724 +       int err, need_reopen, nbr;
14725 +       aufs_bindex_t bbot, bindex;
14726 +       struct dentry *dentry;
14727 +       struct super_block *sb;
14728 +       struct au_finfo *finfo;
14729 +       struct au_hfile *hfile;
14730 +
14731 +       dentry = file->f_path.dentry;
14732 +       sb = dentry->d_sb;
14733 +       nbr = au_sbbot(sb) + 1;
14734 +       finfo = au_fi(file);
14735 +       if (!finfo->fi_hdir) {
14736 +               hfile = &finfo->fi_htop;
14737 +               AuDebugOn(!hfile->hf_file);
14738 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14739 +               AuDebugOn(bindex < 0);
14740 +               if (bindex != finfo->fi_btop)
14741 +                       au_set_fbtop(file, bindex);
14742 +       } else {
14743 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14744 +               if (unlikely(err))
14745 +                       goto out;
14746 +               au_do_refresh_dir(file);
14747 +       }
14748 +
14749 +       err = 0;
14750 +       need_reopen = 1;
14751 +       if (!au_test_mmapped(file))
14752 +               err = au_file_refresh_by_inode(file, &need_reopen);
14753 +       if (finfo->fi_hdir)
14754 +               /* harmless if err */
14755 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14756 +       if (!err && need_reopen && !d_unlinked(dentry))
14757 +               err = reopen(file);
14758 +       if (!err) {
14759 +               au_update_figen(file);
14760 +               goto out; /* success */
14761 +       }
14762 +
14763 +       /* error, close all lower files */
14764 +       if (finfo->fi_hdir) {
14765 +               bbot = au_fbbot_dir(file);
14766 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14767 +                       au_set_h_fptr(file, bindex, NULL);
14768 +       }
14769 +
14770 +out:
14771 +       return err;
14772 +}
14773 +
14774 +/* common function to regular file and dir */
14775 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14776 +                         int wlock, unsigned int fi_lsc)
14777 +{
14778 +       int err;
14779 +       unsigned int sigen, figen;
14780 +       aufs_bindex_t btop;
14781 +       unsigned char pseudo_link;
14782 +       struct dentry *dentry;
14783 +       struct inode *inode;
14784 +
14785 +       err = 0;
14786 +       dentry = file->f_path.dentry;
14787 +       inode = d_inode(dentry);
14788 +       sigen = au_sigen(dentry->d_sb);
14789 +       fi_write_lock_nested(file, fi_lsc);
14790 +       figen = au_figen(file);
14791 +       if (!fi_lsc)
14792 +               di_write_lock_child(dentry);
14793 +       else
14794 +               di_write_lock_child2(dentry);
14795 +       btop = au_dbtop(dentry);
14796 +       pseudo_link = (btop != au_ibtop(inode));
14797 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14798 +               if (!wlock) {
14799 +                       di_downgrade_lock(dentry, AuLock_IR);
14800 +                       fi_downgrade_lock(file);
14801 +               }
14802 +               goto out; /* success */
14803 +       }
14804 +
14805 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14806 +       if (au_digen_test(dentry, sigen)) {
14807 +               err = au_reval_dpath(dentry, sigen);
14808 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14809 +       }
14810 +
14811 +       if (!err)
14812 +               err = refresh_file(file, reopen);
14813 +       if (!err) {
14814 +               if (!wlock) {
14815 +                       di_downgrade_lock(dentry, AuLock_IR);
14816 +                       fi_downgrade_lock(file);
14817 +               }
14818 +       } else {
14819 +               di_write_unlock(dentry);
14820 +               fi_write_unlock(file);
14821 +       }
14822 +
14823 +out:
14824 +       return err;
14825 +}
14826 +
14827 +/* ---------------------------------------------------------------------- */
14828 +
14829 +/* cf. aufs_nopage() */
14830 +/* for madvise(2) */
14831 +static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
14832 +{
14833 +       folio_unlock(folio);
14834 +       return 0;
14835 +}
14836 +
14837 +/* it will never be called, but necessary to support O_DIRECT */
14838 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14839 +{ BUG(); return 0; }
14840 +
14841 +/* they will never be called. */
14842 +#ifdef CONFIG_AUFS_DEBUG
14843 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14844 +                           loff_t pos, unsigned len,
14845 +                           struct page **pagep, void **fsdata)
14846 +{ AuUnsupport(); return 0; }
14847 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14848 +                         loff_t pos, unsigned len, unsigned copied,
14849 +                         struct page *page, void *fsdata)
14850 +{ AuUnsupport(); return 0; }
14851 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14852 +{ AuUnsupport(); return 0; }
14853 +
14854 +static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
14855 +{ AuUnsupport(); return true; }
14856 +static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
14857 +{ AuUnsupport(); }
14858 +static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
14859 +{ AuUnsupport(); return true; }
14860 +#if 0 /* called by memory compaction regardless file */
14861 +static int aufs_migrate_folio(struct address_space *mapping, struct folio *dst,
14862 +                             struct folio *src, enum migrate_mode mode)
14863 +{ AuUnsupport(); return 0; }
14864 +#endif
14865 +static int aufs_launder_folio(struct folio *folio)
14866 +{ AuUnsupport(); return 0; }
14867 +static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
14868 +                                     size_t count)
14869 +{ AuUnsupport(); return true; }
14870 +static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
14871 +                                   bool *writeback)
14872 +{ AuUnsupport(); }
14873 +static int aufs_error_remove_page(struct address_space *mapping,
14874 +                                 struct page *page)
14875 +{ AuUnsupport(); return 0; }
14876 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14877 +                             sector_t *span)
14878 +{ AuUnsupport(); return 0; }
14879 +static void aufs_swap_deactivate(struct file *file)
14880 +{ AuUnsupport(); }
14881 +static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
14882 +{ AuUnsupport(); return 0; }
14883 +#endif /* CONFIG_AUFS_DEBUG */
14884 +
14885 +const struct address_space_operations aufs_aop = {
14886 +       .read_folio             = aufs_read_folio,
14887 +       .direct_IO              = aufs_direct_IO,
14888 +#ifdef CONFIG_AUFS_DEBUG
14889 +       .writepage              = aufs_writepage,
14890 +       /* no writepages, because of writepage */
14891 +       .dirty_folio            = aufs_dirty_folio,
14892 +       /* no readpages, because of readpage */
14893 +       .write_begin            = aufs_write_begin,
14894 +       .write_end              = aufs_write_end,
14895 +       /* no bmap, no block device */
14896 +       .invalidate_folio       = aufs_invalidate_folio,
14897 +       .release_folio          = aufs_release_folio,
14898 +       /* is fallback_migrate_page ok? */
14899 +       /* .migrate_folio       = aufs_migrate_folio, */
14900 +       .launder_folio          = aufs_launder_folio,
14901 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14902 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14903 +       .error_remove_page      = aufs_error_remove_page,
14904 +       .swap_activate          = aufs_swap_activate,
14905 +       .swap_deactivate        = aufs_swap_deactivate,
14906 +       .swap_rw                = aufs_swap_rw
14907 +#endif /* CONFIG_AUFS_DEBUG */
14908 +};
14909 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14910 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14911 +++ linux/fs/aufs/file.h        2022-11-05 23:02:18.965889284 +0100
14912 @@ -0,0 +1,342 @@
14913 +/* SPDX-License-Identifier: GPL-2.0 */
14914 +/*
14915 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14916 + *
14917 + * This program is free software; you can redistribute it and/or modify
14918 + * it under the terms of the GNU General Public License as published by
14919 + * the Free Software Foundation; either version 2 of the License, or
14920 + * (at your option) any later version.
14921 + *
14922 + * This program is distributed in the hope that it will be useful,
14923 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14924 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14925 + * GNU General Public License for more details.
14926 + *
14927 + * You should have received a copy of the GNU General Public License
14928 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14929 + */
14930 +
14931 +/*
14932 + * file operations
14933 + */
14934 +
14935 +#ifndef __AUFS_FILE_H__
14936 +#define __AUFS_FILE_H__
14937 +
14938 +#ifdef __KERNEL__
14939 +
14940 +#include <linux/file.h>
14941 +#include <linux/fs.h>
14942 +#include <linux/mm_types.h>
14943 +#include <linux/poll.h>
14944 +#include "rwsem.h"
14945 +
14946 +struct au_branch;
14947 +struct au_hfile {
14948 +       struct file             *hf_file;
14949 +       struct au_branch        *hf_br;
14950 +};
14951 +
14952 +struct au_vdir;
14953 +struct au_fidir {
14954 +       aufs_bindex_t           fd_bbot;
14955 +       aufs_bindex_t           fd_nent;
14956 +       struct au_vdir          *fd_vdir_cache;
14957 +       struct au_hfile         fd_hfile[];
14958 +};
14959 +
14960 +static inline int au_fidir_sz(int nent)
14961 +{
14962 +       AuDebugOn(nent < 0);
14963 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14964 +}
14965 +
14966 +struct au_finfo {
14967 +       atomic_t                fi_generation;
14968 +
14969 +       struct au_rwsem         fi_rwsem;
14970 +       aufs_bindex_t           fi_btop;
14971 +
14972 +       /* do not union them */
14973 +       struct {                                /* for non-dir */
14974 +               struct au_hfile                 fi_htop;
14975 +               atomic_t                        fi_mmapped;
14976 +       };
14977 +       struct au_fidir         *fi_hdir;       /* for dir only */
14978 +
14979 +       struct hlist_bl_node    fi_hlist;
14980 +       struct file             *fi_file;       /* very ugly */
14981 +       struct rcu_head         rcu;
14982 +} ____cacheline_aligned_in_smp;
14983 +
14984 +/* ---------------------------------------------------------------------- */
14985 +
14986 +/* file.c */
14987 +extern const struct address_space_operations aufs_aop;
14988 +unsigned int au_file_roflags(unsigned int flags);
14989 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14990 +                      struct file *file, int force_wr);
14991 +struct au_do_open_args {
14992 +       int             aopen;
14993 +       int             (*open)(struct file *file, int flags,
14994 +                               struct file *h_file);
14995 +       struct au_fidir *fidir;
14996 +       struct file     *h_file;
14997 +};
14998 +int au_do_open(struct file *file, struct au_do_open_args *args);
14999 +int au_reopen_nondir(struct file *file);
15000 +struct au_pin;
15001 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15002 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15003 +                         int wlock, unsigned int fi_lsc);
15004 +int au_do_flush(struct file *file, fl_owner_t id,
15005 +               int (*flush)(struct file *file, fl_owner_t id));
15006 +
15007 +/* poll.c */
15008 +#ifdef CONFIG_AUFS_POLL
15009 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15010 +#endif
15011 +
15012 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15013 +/* hfsplus.c */
15014 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15015 +                          int force_wr);
15016 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15017 +                   struct file *h_file);
15018 +#else
15019 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15020 +       aufs_bindex_t bindex, int force_wr)
15021 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15022 +          struct file *h_file);
15023 +#endif
15024 +
15025 +/* f_op.c */
15026 +extern const struct file_operations aufs_file_fop;
15027 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15028 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15029 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15030 +
15031 +/* finfo.c */
15032 +void au_hfput(struct au_hfile *hf, int execed);
15033 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15034 +                  struct file *h_file);
15035 +
15036 +void au_update_figen(struct file *file);
15037 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15038 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15039 +
15040 +void au_fi_init_once(void *_fi);
15041 +void au_finfo_fin(struct file *file);
15042 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15043 +
15044 +/* ioctl.c */
15045 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15046 +#ifdef CONFIG_COMPAT
15047 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15048 +                          unsigned long arg);
15049 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15050 +                             unsigned long arg);
15051 +#endif
15052 +
15053 +/* ---------------------------------------------------------------------- */
15054 +
15055 +static inline struct au_finfo *au_fi(struct file *file)
15056 +{
15057 +       return file->private_data;
15058 +}
15059 +
15060 +/* ---------------------------------------------------------------------- */
15061 +
15062 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15063 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15064 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15065 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15066 +/*
15067 +#define fi_read_trylock_nested(f) \
15068 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15069 +#define fi_write_trylock_nested(f) \
15070 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15071 +*/
15072 +
15073 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15074 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15075 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15076 +
15077 +/* lock subclass for finfo */
15078 +enum {
15079 +       AuLsc_FI_1,
15080 +       AuLsc_FI_2
15081 +};
15082 +
15083 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15084 +{
15085 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15086 +}
15087 +
15088 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15089 +{
15090 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15091 +}
15092 +
15093 +/*
15094 + * fi_read_lock_1, fi_write_lock_1,
15095 + * fi_read_lock_2, fi_write_lock_2
15096 + */
15097 +#define AuReadLockFunc(name) \
15098 +static inline void fi_read_lock_##name(struct file *f) \
15099 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15100 +
15101 +#define AuWriteLockFunc(name) \
15102 +static inline void fi_write_lock_##name(struct file *f) \
15103 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15104 +
15105 +#define AuRWLockFuncs(name) \
15106 +       AuReadLockFunc(name) \
15107 +       AuWriteLockFunc(name)
15108 +
15109 +AuRWLockFuncs(1);
15110 +AuRWLockFuncs(2);
15111 +
15112 +#undef AuReadLockFunc
15113 +#undef AuWriteLockFunc
15114 +#undef AuRWLockFuncs
15115 +
15116 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15117 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15118 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15119 +
15120 +/* ---------------------------------------------------------------------- */
15121 +
15122 +/* todo: hard/soft set? */
15123 +static inline aufs_bindex_t au_fbtop(struct file *file)
15124 +{
15125 +       FiMustAnyLock(file);
15126 +       return au_fi(file)->fi_btop;
15127 +}
15128 +
15129 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15130 +{
15131 +       FiMustAnyLock(file);
15132 +       AuDebugOn(!au_fi(file)->fi_hdir);
15133 +       return au_fi(file)->fi_hdir->fd_bbot;
15134 +}
15135 +
15136 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15137 +{
15138 +       FiMustAnyLock(file);
15139 +       AuDebugOn(!au_fi(file)->fi_hdir);
15140 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15141 +}
15142 +
15143 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15144 +{
15145 +       FiMustWriteLock(file);
15146 +       au_fi(file)->fi_btop = bindex;
15147 +}
15148 +
15149 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15150 +{
15151 +       FiMustWriteLock(file);
15152 +       AuDebugOn(!au_fi(file)->fi_hdir);
15153 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15154 +}
15155 +
15156 +static inline void au_set_fvdir_cache(struct file *file,
15157 +                                     struct au_vdir *vdir_cache)
15158 +{
15159 +       FiMustWriteLock(file);
15160 +       AuDebugOn(!au_fi(file)->fi_hdir);
15161 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15162 +}
15163 +
15164 +static inline struct file *au_hf_top(struct file *file)
15165 +{
15166 +       FiMustAnyLock(file);
15167 +       AuDebugOn(au_fi(file)->fi_hdir);
15168 +       return au_fi(file)->fi_htop.hf_file;
15169 +}
15170 +
15171 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15172 +{
15173 +       FiMustAnyLock(file);
15174 +       AuDebugOn(!au_fi(file)->fi_hdir);
15175 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15176 +}
15177 +
15178 +/* todo: memory barrier? */
15179 +static inline unsigned int au_figen(struct file *f)
15180 +{
15181 +       return atomic_read(&au_fi(f)->fi_generation);
15182 +}
15183 +
15184 +static inline void au_set_mmapped(struct file *f)
15185 +{
15186 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15187 +               return;
15188 +       pr_warn("fi_mmapped wrapped around\n");
15189 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15190 +               ;
15191 +}
15192 +
15193 +static inline void au_unset_mmapped(struct file *f)
15194 +{
15195 +       atomic_dec(&au_fi(f)->fi_mmapped);
15196 +}
15197 +
15198 +static inline int au_test_mmapped(struct file *f)
15199 +{
15200 +       return atomic_read(&au_fi(f)->fi_mmapped);
15201 +}
15202 +
15203 +/* customize vma->vm_file */
15204 +
15205 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15206 +                                      struct file *file)
15207 +{
15208 +       struct file *f;
15209 +
15210 +       f = vma->vm_file;
15211 +       get_file(file);
15212 +       vma->vm_file = file;
15213 +       fput(f);
15214 +}
15215 +
15216 +#ifdef CONFIG_MMU
15217 +#define AuDbgVmRegion(file, vma) do {} while (0)
15218 +
15219 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15220 +                                   struct file *file)
15221 +{
15222 +       au_do_vm_file_reset(vma, file);
15223 +}
15224 +#else
15225 +#define AuDbgVmRegion(file, vma) \
15226 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15227 +
15228 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15229 +                                   struct file *file)
15230 +{
15231 +       struct file *f;
15232 +
15233 +       au_do_vm_file_reset(vma, file);
15234 +       f = vma->vm_region->vm_file;
15235 +       get_file(file);
15236 +       vma->vm_region->vm_file = file;
15237 +       fput(f);
15238 +}
15239 +#endif /* CONFIG_MMU */
15240 +
15241 +/* handle vma->vm_prfile */
15242 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15243 +                                   struct file *file)
15244 +{
15245 +       get_file(file);
15246 +       vma->vm_prfile = file;
15247 +#ifndef CONFIG_MMU
15248 +       get_file(file);
15249 +       vma->vm_region->vm_prfile = file;
15250 +#endif
15251 +}
15252 +
15253 +#endif /* __KERNEL__ */
15254 +#endif /* __AUFS_FILE_H__ */
15255 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15256 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15257 +++ linux/fs/aufs/finfo.c       2022-11-05 23:02:18.965889284 +0100
15258 @@ -0,0 +1,149 @@
15259 +// SPDX-License-Identifier: GPL-2.0
15260 +/*
15261 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15262 + *
15263 + * This program is free software; you can redistribute it and/or modify
15264 + * it under the terms of the GNU General Public License as published by
15265 + * the Free Software Foundation; either version 2 of the License, or
15266 + * (at your option) any later version.
15267 + *
15268 + * This program is distributed in the hope that it will be useful,
15269 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15270 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15271 + * GNU General Public License for more details.
15272 + *
15273 + * You should have received a copy of the GNU General Public License
15274 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15275 + */
15276 +
15277 +/*
15278 + * file private data
15279 + */
15280 +
15281 +#include "aufs.h"
15282 +
15283 +void au_hfput(struct au_hfile *hf, int execed)
15284 +{
15285 +       if (execed)
15286 +               allow_write_access(hf->hf_file);
15287 +       fput(hf->hf_file);
15288 +       hf->hf_file = NULL;
15289 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15290 +       hf->hf_br = NULL;
15291 +}
15292 +
15293 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15294 +{
15295 +       struct au_finfo *finfo = au_fi(file);
15296 +       struct au_hfile *hf;
15297 +       struct au_fidir *fidir;
15298 +
15299 +       fidir = finfo->fi_hdir;
15300 +       if (!fidir) {
15301 +               AuDebugOn(finfo->fi_btop != bindex);
15302 +               hf = &finfo->fi_htop;
15303 +       } else
15304 +               hf = fidir->fd_hfile + bindex;
15305 +
15306 +       if (hf && hf->hf_file)
15307 +               au_hfput(hf, vfsub_file_execed(file));
15308 +       if (val) {
15309 +               FiMustWriteLock(file);
15310 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15311 +               hf->hf_file = val;
15312 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15313 +       }
15314 +}
15315 +
15316 +void au_update_figen(struct file *file)
15317 +{
15318 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15319 +       /* smp_mb(); */ /* atomic_set */
15320 +}
15321 +
15322 +/* ---------------------------------------------------------------------- */
15323 +
15324 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15325 +{
15326 +       struct au_fidir *fidir;
15327 +       int nbr;
15328 +
15329 +       nbr = au_sbbot(sb) + 1;
15330 +       if (nbr < 2)
15331 +               nbr = 2; /* initial allocate for 2 branches */
15332 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15333 +       if (fidir) {
15334 +               fidir->fd_bbot = -1;
15335 +               fidir->fd_nent = nbr;
15336 +       }
15337 +
15338 +       return fidir;
15339 +}
15340 +
15341 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15342 +{
15343 +       int err;
15344 +       struct au_fidir *fidir, *p;
15345 +
15346 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15347 +       fidir = finfo->fi_hdir;
15348 +       AuDebugOn(!fidir);
15349 +
15350 +       err = -ENOMEM;
15351 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15352 +                        GFP_NOFS, may_shrink);
15353 +       if (p) {
15354 +               p->fd_nent = nbr;
15355 +               finfo->fi_hdir = p;
15356 +               err = 0;
15357 +       }
15358 +
15359 +       return err;
15360 +}
15361 +
15362 +/* ---------------------------------------------------------------------- */
15363 +
15364 +void au_finfo_fin(struct file *file)
15365 +{
15366 +       struct au_finfo *finfo;
15367 +
15368 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15369 +
15370 +       finfo = au_fi(file);
15371 +       AuDebugOn(finfo->fi_hdir);
15372 +       AuRwDestroy(&finfo->fi_rwsem);
15373 +       au_cache_free_finfo(finfo);
15374 +}
15375 +
15376 +void au_fi_init_once(void *_finfo)
15377 +{
15378 +       struct au_finfo *finfo = _finfo;
15379 +
15380 +       au_rw_init(&finfo->fi_rwsem);
15381 +}
15382 +
15383 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15384 +{
15385 +       int err;
15386 +       struct au_finfo *finfo;
15387 +       struct dentry *dentry;
15388 +
15389 +       err = -ENOMEM;
15390 +       dentry = file->f_path.dentry;
15391 +       finfo = au_cache_alloc_finfo();
15392 +       if (unlikely(!finfo))
15393 +               goto out;
15394 +
15395 +       err = 0;
15396 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15397 +       au_rw_write_lock(&finfo->fi_rwsem);
15398 +       finfo->fi_btop = -1;
15399 +       finfo->fi_hdir = fidir;
15400 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15401 +       /* smp_mb(); */ /* atomic_set */
15402 +
15403 +       file->private_data = finfo;
15404 +
15405 +out:
15406 +       return err;
15407 +}
15408 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15409 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15410 +++ linux/fs/aufs/f_op.c        2023-10-31 09:31:04.196547417 +0100
15411 @@ -0,0 +1,771 @@
15412 +// SPDX-License-Identifier: GPL-2.0
15413 +/*
15414 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15415 + *
15416 + * This program is free software; you can redistribute it and/or modify
15417 + * it under the terms of the GNU General Public License as published by
15418 + * the Free Software Foundation; either version 2 of the License, or
15419 + * (at your option) any later version.
15420 + *
15421 + * This program is distributed in the hope that it will be useful,
15422 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15423 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15424 + * GNU General Public License for more details.
15425 + *
15426 + * You should have received a copy of the GNU General Public License
15427 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15428 + */
15429 +
15430 +/*
15431 + * file and vm operations
15432 + */
15433 +
15434 +#include <linux/aio.h>
15435 +#include <linux/fs_stack.h>
15436 +#include <linux/mman.h>
15437 +#include <linux/security.h>
15438 +#include "aufs.h"
15439 +
15440 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15441 +{
15442 +       int err;
15443 +       aufs_bindex_t bindex;
15444 +       struct dentry *dentry, *h_dentry;
15445 +       struct au_finfo *finfo;
15446 +       struct inode *h_inode;
15447 +
15448 +       FiMustWriteLock(file);
15449 +
15450 +       err = 0;
15451 +       dentry = file->f_path.dentry;
15452 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15453 +       finfo = au_fi(file);
15454 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15455 +       atomic_set(&finfo->fi_mmapped, 0);
15456 +       bindex = au_dbtop(dentry);
15457 +       if (!h_file) {
15458 +               h_dentry = au_h_dptr(dentry, bindex);
15459 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15460 +               if (unlikely(err))
15461 +                       goto out;
15462 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15463 +               if (IS_ERR(h_file)) {
15464 +                       err = PTR_ERR(h_file);
15465 +                       goto out;
15466 +               }
15467 +       } else {
15468 +               h_dentry = h_file->f_path.dentry;
15469 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15470 +               if (unlikely(err))
15471 +                       goto out;
15472 +               /* br ref is already inc-ed */
15473 +       }
15474 +
15475 +       if (flags & __O_TMPFILE) {
15476 +               AuDebugOn(!h_file);
15477 +               AuDebugOn(h_file != au_di(dentry)->di_htmpfile);
15478 +               au_di(dentry)->di_htmpfile = NULL;
15479 +
15480 +               if (!(flags & O_EXCL)) {
15481 +                       h_inode = file_inode(h_file);
15482 +                       spin_lock(&h_inode->i_lock);
15483 +                       h_inode->i_state |= I_LINKABLE;
15484 +                       spin_unlock(&h_inode->i_lock);
15485 +               }
15486 +       }
15487 +       au_set_fbtop(file, bindex);
15488 +       au_set_h_fptr(file, bindex, h_file);
15489 +       au_update_figen(file);
15490 +       /* todo: necessary? */
15491 +       /* file->f_ra = h_file->f_ra; */
15492 +
15493 +out:
15494 +       return err;
15495 +}
15496 +
15497 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15498 +                           struct file *file)
15499 +{
15500 +       int err;
15501 +       struct super_block *sb;
15502 +       struct au_do_open_args args = {
15503 +               .open   = au_do_open_nondir
15504 +       };
15505 +
15506 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15507 +             file, vfsub_file_flags(file), file->f_mode);
15508 +
15509 +       sb = file->f_path.dentry->d_sb;
15510 +       si_read_lock(sb, AuLock_FLUSH);
15511 +       err = au_do_open(file, &args);
15512 +       si_read_unlock(sb);
15513 +       return err;
15514 +}
15515 +
15516 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15517 +{
15518 +       struct au_finfo *finfo;
15519 +       aufs_bindex_t bindex;
15520 +
15521 +       finfo = au_fi(file);
15522 +       au_hbl_del(&finfo->fi_hlist,
15523 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15524 +       bindex = finfo->fi_btop;
15525 +       if (bindex >= 0)
15526 +               au_set_h_fptr(file, bindex, NULL);
15527 +
15528 +       au_finfo_fin(file);
15529 +       return 0;
15530 +}
15531 +
15532 +/* ---------------------------------------------------------------------- */
15533 +
15534 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15535 +{
15536 +       int err;
15537 +       struct file *h_file;
15538 +
15539 +       err = 0;
15540 +       h_file = au_hf_top(file);
15541 +       if (h_file)
15542 +               err = vfsub_flush(h_file, id);
15543 +       return err;
15544 +}
15545 +
15546 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15547 +{
15548 +       return au_do_flush(file, id, au_do_flush_nondir);
15549 +}
15550 +
15551 +/* ---------------------------------------------------------------------- */
15552 +/*
15553 + * read and write functions acquire [fdi]_rwsem once, but release before
15554 + * mmap_sem. This is because to stop a race condition between mmap(2).
15555 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15556 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15557 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15558 + */
15559 +
15560 +/* Callers should call au_read_post() or fput() in the end */
15561 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15562 +{
15563 +       struct file *h_file;
15564 +       int err;
15565 +
15566 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15567 +       if (!err) {
15568 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15569 +               h_file = au_hf_top(file);
15570 +               get_file(h_file);
15571 +               if (!keep_fi)
15572 +                       fi_read_unlock(file);
15573 +       } else
15574 +               h_file = ERR_PTR(err);
15575 +
15576 +       return h_file;
15577 +}
15578 +
15579 +static void au_read_post(struct inode *inode, struct file *h_file)
15580 +{
15581 +       /* update without lock, I don't think it a problem */
15582 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15583 +       fput(h_file);
15584 +}
15585 +
15586 +struct au_write_pre {
15587 +       /* input */
15588 +       unsigned int lsc;
15589 +
15590 +       /* output */
15591 +       blkcnt_t blks;
15592 +       aufs_bindex_t btop;
15593 +};
15594 +
15595 +/*
15596 + * return with iinfo is write-locked
15597 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15598 + * end
15599 + */
15600 +static struct file *au_write_pre(struct file *file, int do_ready,
15601 +                                struct au_write_pre *wpre)
15602 +{
15603 +       struct file *h_file;
15604 +       struct dentry *dentry;
15605 +       int err;
15606 +       unsigned int lsc;
15607 +       struct au_pin pin;
15608 +
15609 +       lsc = 0;
15610 +       if (wpre)
15611 +               lsc = wpre->lsc;
15612 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15613 +       h_file = ERR_PTR(err);
15614 +       if (unlikely(err))
15615 +               goto out;
15616 +
15617 +       dentry = file->f_path.dentry;
15618 +       if (do_ready) {
15619 +               err = au_ready_to_write(file, -1, &pin);
15620 +               if (unlikely(err)) {
15621 +                       h_file = ERR_PTR(err);
15622 +                       di_write_unlock(dentry);
15623 +                       goto out_fi;
15624 +               }
15625 +       }
15626 +
15627 +       di_downgrade_lock(dentry, /*flags*/0);
15628 +       if (wpre)
15629 +               wpre->btop = au_fbtop(file);
15630 +       h_file = au_hf_top(file);
15631 +       get_file(h_file);
15632 +       if (wpre)
15633 +               wpre->blks = file_inode(h_file)->i_blocks;
15634 +       if (do_ready)
15635 +               au_unpin(&pin);
15636 +       di_read_unlock(dentry, /*flags*/0);
15637 +       vfsub_file_start_write(h_file);
15638 +
15639 +out_fi:
15640 +       fi_write_unlock(file);
15641 +out:
15642 +       return h_file;
15643 +}
15644 +
15645 +static void au_write_post(struct inode *inode, struct file *h_file,
15646 +                         struct au_write_pre *wpre, ssize_t written)
15647 +{
15648 +       struct inode *h_inode;
15649 +
15650 +       vfsub_file_end_write(h_file);
15651 +       au_cpup_attr_timesizes(inode);
15652 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15653 +       h_inode = file_inode(h_file);
15654 +       inode->i_mode = h_inode->i_mode;
15655 +       ii_write_unlock(inode);
15656 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15657 +       if (written > 0)
15658 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15659 +                             /*force*/h_inode->i_blocks > wpre->blks);
15660 +       fput(h_file);
15661 +}
15662 +
15663 +/*
15664 + * todo: very ugly
15665 + * it locks both of i_mutex and si_rwsem for read in safe.
15666 + * if the plink maintenance mode continues forever (that is the problem),
15667 + * may loop forever.
15668 + */
15669 +static void au_mtx_and_read_lock(struct inode *inode)
15670 +{
15671 +       int err;
15672 +       struct super_block *sb = inode->i_sb;
15673 +
15674 +       while (1) {
15675 +               inode_lock(inode);
15676 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15677 +               if (!err)
15678 +                       break;
15679 +               inode_unlock(inode);
15680 +               si_read_lock(sb, AuLock_NOPLMW);
15681 +               si_read_unlock(sb);
15682 +       }
15683 +}
15684 +
15685 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15686 +                         struct iov_iter *iov_iter)
15687 +{
15688 +       ssize_t err;
15689 +       struct file *file;
15690 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15691 +
15692 +       err = security_file_permission(h_file, rw);
15693 +       if (unlikely(err))
15694 +               goto out;
15695 +
15696 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15697 +       iter = NULL;
15698 +       if (rw == MAY_READ)
15699 +               iter = h_file->f_op->read_iter;
15700 +       else if (rw == MAY_WRITE)
15701 +               iter = h_file->f_op->write_iter;
15702 +
15703 +       file = kio->ki_filp;
15704 +       kio->ki_filp = h_file;
15705 +       if (iter) {
15706 +               lockdep_off();
15707 +               err = iter(kio, iov_iter);
15708 +               lockdep_on();
15709 +       } else
15710 +               /* currently there is no such fs */
15711 +               WARN_ON_ONCE(1);
15712 +       kio->ki_filp = file;
15713 +
15714 +out:
15715 +       return err;
15716 +}
15717 +
15718 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15719 +{
15720 +       ssize_t err;
15721 +       struct file *file, *h_file;
15722 +       struct inode *inode;
15723 +       struct super_block *sb;
15724 +
15725 +       file = kio->ki_filp;
15726 +       inode = file_inode(file);
15727 +       sb = inode->i_sb;
15728 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15729 +
15730 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15731 +       err = PTR_ERR(h_file);
15732 +       if (IS_ERR(h_file))
15733 +               goto out;
15734 +
15735 +       if (au_test_loopback_kthread()) {
15736 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15737 +               if (file->f_mapping != h_file->f_mapping) {
15738 +                       file->f_mapping = h_file->f_mapping;
15739 +                       smp_mb(); /* unnecessary? */
15740 +               }
15741 +       }
15742 +       fi_read_unlock(file);
15743 +
15744 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15745 +       /* todo: necessary? */
15746 +       /* file->f_ra = h_file->f_ra; */
15747 +       au_read_post(inode, h_file);
15748 +
15749 +out:
15750 +       si_read_unlock(sb);
15751 +       return err;
15752 +}
15753 +
15754 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15755 +{
15756 +       ssize_t err;
15757 +       struct au_write_pre wpre;
15758 +       struct inode *inode;
15759 +       struct file *file, *h_file;
15760 +
15761 +       file = kio->ki_filp;
15762 +       inode = file_inode(file);
15763 +       au_mtx_and_read_lock(inode);
15764 +
15765 +       wpre.lsc = 0;
15766 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15767 +       err = PTR_ERR(h_file);
15768 +       if (IS_ERR(h_file))
15769 +               goto out;
15770 +
15771 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15772 +       au_write_post(inode, h_file, &wpre, err);
15773 +
15774 +out:
15775 +       si_read_unlock(inode->i_sb);
15776 +       inode_unlock(inode);
15777 +       return err;
15778 +}
15779 +
15780 +/*
15781 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15782 + * don't have their own .splice_{read,write} implimentations, and they use
15783 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15784 + * simple converters to f_op->iter_read() and ->iter_write().
15785 + * But we keep our own implementations because some non-mainlined FSes may have
15786 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15787 + * away an opportunity to co-work with aufs from them.
15788 + */
15789 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15790 +                               struct pipe_inode_info *pipe, size_t len,
15791 +                               unsigned int flags)
15792 +{
15793 +       ssize_t err;
15794 +       struct file *h_file;
15795 +       struct inode *inode;
15796 +       struct super_block *sb;
15797 +
15798 +       inode = file_inode(file);
15799 +       sb = inode->i_sb;
15800 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15801 +
15802 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15803 +       err = PTR_ERR(h_file);
15804 +       if (IS_ERR(h_file))
15805 +               goto out;
15806 +
15807 +       err = vfsub_splice_read(h_file, ppos, pipe, len, flags);
15808 +       /* todo: necessary? */
15809 +       /* file->f_ra = h_file->f_ra; */
15810 +       au_read_post(inode, h_file);
15811 +
15812 +out:
15813 +       si_read_unlock(sb);
15814 +       return err;
15815 +}
15816 +
15817 +static ssize_t
15818 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15819 +                 size_t len, unsigned int flags)
15820 +{
15821 +       ssize_t err;
15822 +       struct au_write_pre wpre;
15823 +       struct inode *inode;
15824 +       struct file *h_file;
15825 +
15826 +       inode = file_inode(file);
15827 +       au_mtx_and_read_lock(inode);
15828 +
15829 +       wpre.lsc = 0;
15830 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15831 +       err = PTR_ERR(h_file);
15832 +       if (IS_ERR(h_file))
15833 +               goto out;
15834 +
15835 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15836 +       au_write_post(inode, h_file, &wpre, err);
15837 +
15838 +out:
15839 +       si_read_unlock(inode->i_sb);
15840 +       inode_unlock(inode);
15841 +       return err;
15842 +}
15843 +
15844 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15845 +                          loff_t len)
15846 +{
15847 +       long err;
15848 +       struct au_write_pre wpre;
15849 +       struct inode *inode;
15850 +       struct file *h_file;
15851 +
15852 +       inode = file_inode(file);
15853 +       au_mtx_and_read_lock(inode);
15854 +
15855 +       wpre.lsc = 0;
15856 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15857 +       err = PTR_ERR(h_file);
15858 +       if (IS_ERR(h_file))
15859 +               goto out;
15860 +
15861 +       lockdep_off();
15862 +       err = vfs_fallocate(h_file, mode, offset, len);
15863 +       lockdep_on();
15864 +       /*
15865 +        * we don't need to call file_modifed() here since au_write_post()
15866 +        * is equivalent and copies-up all timestamps and permission bits.
15867 +        */
15868 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15869 +
15870 +out:
15871 +       si_read_unlock(inode->i_sb);
15872 +       inode_unlock(inode);
15873 +       return err;
15874 +}
15875 +
15876 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15877 +                                   struct file *dst, loff_t dst_pos,
15878 +                                   size_t len, unsigned int flags)
15879 +{
15880 +       ssize_t err;
15881 +       struct au_write_pre wpre;
15882 +       enum { SRC, DST };
15883 +       struct {
15884 +               struct inode *inode;
15885 +               struct file *h_file;
15886 +               struct super_block *h_sb;
15887 +       } a[2];
15888 +#define a_src  a[SRC]
15889 +#define a_dst  a[DST]
15890 +
15891 +       err = -EINVAL;
15892 +       a_src.inode = file_inode(src);
15893 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15894 +               goto out;
15895 +       a_dst.inode = file_inode(dst);
15896 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15897 +               goto out;
15898 +
15899 +       au_mtx_and_read_lock(a_dst.inode);
15900 +       /*
15901 +        * in order to match the order in di_write_lock2_{child,parent}(),
15902 +        * use f_path.dentry for this comparison.
15903 +        */
15904 +       if (src->f_path.dentry < dst->f_path.dentry) {
15905 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15906 +               err = PTR_ERR(a_src.h_file);
15907 +               if (IS_ERR(a_src.h_file))
15908 +                       goto out_si;
15909 +
15910 +               wpre.lsc = AuLsc_FI_2;
15911 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15912 +               err = PTR_ERR(a_dst.h_file);
15913 +               if (IS_ERR(a_dst.h_file)) {
15914 +                       au_read_post(a_src.inode, a_src.h_file);
15915 +                       goto out_si;
15916 +               }
15917 +       } else {
15918 +               wpre.lsc = AuLsc_FI_1;
15919 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15920 +               err = PTR_ERR(a_dst.h_file);
15921 +               if (IS_ERR(a_dst.h_file))
15922 +                       goto out_si;
15923 +
15924 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15925 +               err = PTR_ERR(a_src.h_file);
15926 +               if (IS_ERR(a_src.h_file)) {
15927 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15928 +                                     /*written*/0);
15929 +                       goto out_si;
15930 +               }
15931 +       }
15932 +
15933 +       err = -EXDEV;
15934 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15935 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15936 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15937 +               AuDbgFile(src);
15938 +               AuDbgFile(dst);
15939 +               goto out_file;
15940 +       }
15941 +
15942 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15943 +                                   dst_pos, len, flags);
15944 +
15945 +out_file:
15946 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15947 +       fi_read_unlock(src);
15948 +       au_read_post(a_src.inode, a_src.h_file);
15949 +out_si:
15950 +       si_read_unlock(a_dst.inode->i_sb);
15951 +       inode_unlock(a_dst.inode);
15952 +out:
15953 +       return err;
15954 +#undef a_src
15955 +#undef a_dst
15956 +}
15957 +
15958 +/* ---------------------------------------------------------------------- */
15959 +
15960 +/*
15961 + * The locking order around current->mmap_sem.
15962 + * - in most and regular cases
15963 + *   file I/O syscall -- aufs_read() or something
15964 + *     -- si_rwsem for read -- mmap_sem
15965 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15966 + * - in mmap case
15967 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15968 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15969 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15970 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15971 + * It means that when aufs acquires si_rwsem for write, the process should never
15972 + * acquire mmap_sem.
15973 + *
15974 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15975 + * problem either since any directory is not able to be mmap-ed.
15976 + * The similar scenario is applied to aufs_readlink() too.
15977 + */
15978 +
15979 +#if 0 /* stop calling security_file_mmap() */
15980 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15981 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15982 +
15983 +static unsigned long au_arch_prot_conv(unsigned long flags)
15984 +{
15985 +       /* currently ppc64 only */
15986 +#ifdef CONFIG_PPC64
15987 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15988 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15989 +       return AuConv_VM_PROT(flags, SAO);
15990 +#else
15991 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15992 +       return 0;
15993 +#endif
15994 +}
15995 +
15996 +static unsigned long au_prot_conv(unsigned long flags)
15997 +{
15998 +       return AuConv_VM_PROT(flags, READ)
15999 +               | AuConv_VM_PROT(flags, WRITE)
16000 +               | AuConv_VM_PROT(flags, EXEC)
16001 +               | au_arch_prot_conv(flags);
16002 +}
16003 +
16004 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16005 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16006 +
16007 +static unsigned long au_flag_conv(unsigned long flags)
16008 +{
16009 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16010 +               | AuConv_VM_MAP(flags, DENYWRITE)
16011 +               | AuConv_VM_MAP(flags, LOCKED);
16012 +}
16013 +#endif
16014 +
16015 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16016 +{
16017 +       int err;
16018 +       const unsigned char wlock
16019 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16020 +       struct super_block *sb;
16021 +       struct file *h_file;
16022 +       struct inode *inode;
16023 +
16024 +       AuDbgVmRegion(file, vma);
16025 +
16026 +       inode = file_inode(file);
16027 +       sb = inode->i_sb;
16028 +       lockdep_off();
16029 +       si_read_lock(sb, AuLock_NOPLMW);
16030 +
16031 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16032 +       lockdep_on();
16033 +       err = PTR_ERR(h_file);
16034 +       if (IS_ERR(h_file))
16035 +               goto out;
16036 +
16037 +       err = 0;
16038 +       au_set_mmapped(file);
16039 +       au_vm_file_reset(vma, h_file);
16040 +       /*
16041 +        * we cannot call security_mmap_file() here since it may acquire
16042 +        * mmap_sem or i_mutex.
16043 +        *
16044 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16045 +        *                       au_flag_conv(vma->vm_flags));
16046 +        */
16047 +       if (!err)
16048 +               err = call_mmap(h_file, vma);
16049 +       if (!err) {
16050 +               au_vm_prfile_set(vma, file);
16051 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16052 +               goto out_fput; /* success */
16053 +       }
16054 +       au_unset_mmapped(file);
16055 +       au_vm_file_reset(vma, file);
16056 +
16057 +out_fput:
16058 +       lockdep_off();
16059 +       ii_write_unlock(inode);
16060 +       lockdep_on();
16061 +       fput(h_file);
16062 +out:
16063 +       lockdep_off();
16064 +       si_read_unlock(sb);
16065 +       lockdep_on();
16066 +       AuTraceErr(err);
16067 +       return err;
16068 +}
16069 +
16070 +/* ---------------------------------------------------------------------- */
16071 +
16072 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16073 +                            int datasync)
16074 +{
16075 +       int err;
16076 +       struct au_write_pre wpre;
16077 +       struct inode *inode;
16078 +       struct file *h_file;
16079 +
16080 +       err = 0; /* -EBADF; */ /* posix? */
16081 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16082 +               goto out;
16083 +
16084 +       inode = file_inode(file);
16085 +       au_mtx_and_read_lock(inode);
16086 +
16087 +       wpre.lsc = 0;
16088 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16089 +       err = PTR_ERR(h_file);
16090 +       if (IS_ERR(h_file))
16091 +               goto out_unlock;
16092 +
16093 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16094 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16095 +
16096 +out_unlock:
16097 +       si_read_unlock(inode->i_sb);
16098 +       inode_unlock(inode);
16099 +out:
16100 +       return err;
16101 +}
16102 +
16103 +static int aufs_fasync(int fd, struct file *file, int flag)
16104 +{
16105 +       int err;
16106 +       struct file *h_file;
16107 +       struct super_block *sb;
16108 +
16109 +       sb = file->f_path.dentry->d_sb;
16110 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16111 +
16112 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16113 +       err = PTR_ERR(h_file);
16114 +       if (IS_ERR(h_file))
16115 +               goto out;
16116 +
16117 +       if (h_file->f_op->fasync)
16118 +               err = h_file->f_op->fasync(fd, h_file, flag);
16119 +       fput(h_file); /* instead of au_read_post() */
16120 +
16121 +out:
16122 +       si_read_unlock(sb);
16123 +       return err;
16124 +}
16125 +
16126 +static int aufs_setfl(struct file *file, unsigned long arg)
16127 +{
16128 +       int err;
16129 +       struct file *h_file;
16130 +       struct super_block *sb;
16131 +
16132 +       sb = file->f_path.dentry->d_sb;
16133 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16134 +
16135 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16136 +       err = PTR_ERR(h_file);
16137 +       if (IS_ERR(h_file))
16138 +               goto out;
16139 +
16140 +       /* stop calling h_file->fasync */
16141 +       arg |= vfsub_file_flags(file) & FASYNC;
16142 +       err = setfl(/*unused fd*/-1, h_file, arg);
16143 +       fput(h_file); /* instead of au_read_post() */
16144 +
16145 +out:
16146 +       si_read_unlock(sb);
16147 +       return err;
16148 +}
16149 +
16150 +/* ---------------------------------------------------------------------- */
16151 +
16152 +const struct file_operations aufs_file_fop = {
16153 +       .owner          = THIS_MODULE,
16154 +
16155 +       .llseek         = default_llseek,
16156 +
16157 +       .read_iter      = aufs_read_iter,
16158 +       .write_iter     = aufs_write_iter,
16159 +
16160 +#ifdef CONFIG_AUFS_POLL
16161 +       .poll           = aufs_poll,
16162 +#endif
16163 +       .unlocked_ioctl = aufs_ioctl_nondir,
16164 +#ifdef CONFIG_COMPAT
16165 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16166 +#endif
16167 +       .mmap           = aufs_mmap,
16168 +       .open           = aufs_open_nondir,
16169 +       .flush          = aufs_flush_nondir,
16170 +       .release        = aufs_release_nondir,
16171 +       .fsync          = aufs_fsync_nondir,
16172 +       .fasync         = aufs_fasync,
16173 +       .setfl          = aufs_setfl,
16174 +       .splice_write   = aufs_splice_write,
16175 +       .splice_read    = aufs_splice_read,
16176 +#if 0 /* reserved for future use */
16177 +       .aio_splice_write = aufs_aio_splice_write,
16178 +       .aio_splice_read  = aufs_aio_splice_read,
16179 +#endif
16180 +       .fallocate      = aufs_fallocate,
16181 +       .copy_file_range = aufs_copy_file_range
16182 +};
16183 diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16184 --- /usr/share/empty/fs/aufs/fsctx.c    1970-01-01 01:00:00.000000000 +0100
16185 +++ linux/fs/aufs/fsctx.c       2022-11-05 23:02:18.965889284 +0100
16186 @@ -0,0 +1,1242 @@
16187 +// SPDX-License-Identifier: GPL-2.0
16188 +/*
16189 + * Copyright (C) 2022 Junjiro R. Okajima
16190 + *
16191 + * This program is free software; you can redistribute it and/or modify
16192 + * it under the terms of the GNU General Public License as published by
16193 + * the Free Software Foundation; either version 2 of the License, or
16194 + * (at your option) any later version.
16195 + *
16196 + * This program is distributed in the hope that it will be useful,
16197 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16198 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16199 + * GNU General Public License for more details.
16200 + *
16201 + * You should have received a copy of the GNU General Public License
16202 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16203 + */
16204 +
16205 +/*
16206 + * fs context, aka new mount api
16207 + */
16208 +
16209 +#include <linux/fs_context.h>
16210 +#include "aufs.h"
16211 +
16212 +struct au_fsctx_opts {
16213 +       aufs_bindex_t bindex;
16214 +       unsigned char skipped;
16215 +       struct au_opt *opt, *opt_tail;
16216 +       struct super_block *sb;
16217 +       struct au_sbinfo *sbinfo;
16218 +       struct au_opts opts;
16219 +};
16220 +
16221 +/* stop extra interpretation of errno in mount(8), and strange error messages */
16222 +static int cvt_err(int err)
16223 +{
16224 +       AuTraceErr(err);
16225 +
16226 +       switch (err) {
16227 +       case -ENOENT:
16228 +       case -ENOTDIR:
16229 +       case -EEXIST:
16230 +       case -EIO:
16231 +               err = -EINVAL;
16232 +       }
16233 +       return err;
16234 +}
16235 +
16236 +static int au_fsctx_reconfigure(struct fs_context *fc)
16237 +{
16238 +       int err, do_dx;
16239 +       unsigned int mntflags;
16240 +       struct dentry *root;
16241 +       struct super_block *sb;
16242 +       struct inode *inode;
16243 +       struct au_fsctx_opts *a = fc->fs_private;
16244 +
16245 +       AuDbg("fc %p\n", fc);
16246 +
16247 +       root = fc->root;
16248 +       sb = root->d_sb;
16249 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16250 +       if (!err) {
16251 +               di_write_lock_child(root);
16252 +               err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16253 +               aufs_write_unlock(root);
16254 +       }
16255 +
16256 +       inode = d_inode(root);
16257 +       inode_lock(inode);
16258 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16259 +       if (unlikely(err))
16260 +               goto out;
16261 +       di_write_lock_child(root);
16262 +
16263 +       /* au_opts_remount() may return an error */
16264 +       err = au_opts_remount(sb, &a->opts);
16265 +
16266 +       if (au_ftest_opts(a->opts.flags, REFRESH))
16267 +               au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16268 +                                                    REFRESH_IDOP));
16269 +
16270 +       if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16271 +               mntflags = au_mntflags(sb);
16272 +               do_dx = !!au_opt_test(mntflags, DIO);
16273 +               au_dy_arefresh(do_dx);
16274 +       }
16275 +
16276 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16277 +       aufs_write_unlock(root);
16278 +
16279 +out:
16280 +       inode_unlock(inode);
16281 +       err = cvt_err(err);
16282 +       AuTraceErr(err);
16283 +
16284 +       return err;
16285 +}
16286 +
16287 +/* ---------------------------------------------------------------------- */
16288 +
16289 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16290 +{
16291 +       int err;
16292 +       struct au_fsctx_opts *a = fc->fs_private;
16293 +       struct au_sbinfo *sbinfo = a->sbinfo;
16294 +       struct dentry *root;
16295 +       struct inode *inode;
16296 +
16297 +       sbinfo->si_sb = sb;
16298 +       sb->s_fs_info = sbinfo;
16299 +       kobject_get(&sbinfo->si_kobj);
16300 +
16301 +       __si_write_lock(sb);
16302 +       si_pid_set(sb);
16303 +       au_sbilist_add(sb);
16304 +
16305 +       /* all timestamps always follow the ones on the branch */
16306 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16307 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16308 +       sb->s_op = &aufs_sop;
16309 +       sb->s_d_op = &aufs_dop;
16310 +       sb->s_magic = AUFS_SUPER_MAGIC;
16311 +       sb->s_maxbytes = 0;
16312 +       sb->s_stack_depth = 1;
16313 +       au_export_init(sb);
16314 +       au_xattr_init(sb);
16315 +
16316 +       err = au_alloc_root(sb);
16317 +       if (unlikely(err)) {
16318 +               si_write_unlock(sb);
16319 +               goto out;
16320 +       }
16321 +       root = sb->s_root;
16322 +       inode = d_inode(root);
16323 +       ii_write_lock_parent(inode);
16324 +       aufs_write_unlock(root);
16325 +
16326 +       /* lock vfs_inode first, then aufs. */
16327 +       inode_lock(inode);
16328 +       aufs_write_lock(root);
16329 +       err = au_opts_mount(sb, &a->opts);
16330 +       AuTraceErr(err);
16331 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16332 +               sb->s_d_op = &aufs_dop_noreval;
16333 +               /* infofc(fc, "%ps", sb->s_d_op); */
16334 +               pr_info("%ps\n", sb->s_d_op);
16335 +               au_refresh_dop(root, /*force_reval*/0);
16336 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
16337 +               au_refresh_iop(inode, /*force_getattr*/0);
16338 +       }
16339 +       aufs_write_unlock(root);
16340 +       inode_unlock(inode);
16341 +       if (!err)
16342 +               goto out; /* success */
16343 +
16344 +       dput(root);
16345 +       sb->s_root = NULL;
16346 +
16347 +out:
16348 +       if (unlikely(err))
16349 +               kobject_put(&sbinfo->si_kobj);
16350 +       AuTraceErr(err);
16351 +       err = cvt_err(err);
16352 +       AuTraceErr(err);
16353 +       return err;
16354 +}
16355 +
16356 +static int au_fsctx_get_tree(struct fs_context *fc)
16357 +{
16358 +       int err;
16359 +
16360 +       AuDbg("fc %p\n", fc);
16361 +       err = get_tree_nodev(fc, au_fsctx_fill_super);
16362 +
16363 +       AuTraceErr(err);
16364 +       return err;
16365 +}
16366 +
16367 +/* ---------------------------------------------------------------------- */
16368 +
16369 +static void au_fsctx_dump(struct au_opts *opts)
16370 +{
16371 +#ifdef CONFIG_AUFS_DEBUG
16372 +       /* reduce stack space */
16373 +       union {
16374 +               struct au_opt_add *add;
16375 +               struct au_opt_del *del;
16376 +               struct au_opt_mod *mod;
16377 +               struct au_opt_xino *xino;
16378 +               struct au_opt_xino_itrunc *xino_itrunc;
16379 +               struct au_opt_wbr_create *create;
16380 +       } u;
16381 +       struct au_opt *opt;
16382 +
16383 +       opt = opts->opt;
16384 +       while (opt->type != Opt_tail) {
16385 +               switch (opt->type) {
16386 +               case Opt_add:
16387 +                       u.add = &opt->add;
16388 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16389 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16390 +                                 u.add->path.dentry);
16391 +                       break;
16392 +               case Opt_del:
16393 +                       fallthrough;
16394 +               case Opt_idel:
16395 +                       u.del = &opt->del;
16396 +                       AuDbg("del {%s, %p}\n",
16397 +                             u.del->pathname, u.del->h_path.dentry);
16398 +                       break;
16399 +               case Opt_mod:
16400 +                       fallthrough;
16401 +               case Opt_imod:
16402 +                       u.mod = &opt->mod;
16403 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16404 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16405 +                       break;
16406 +               case Opt_append:
16407 +                       u.add = &opt->add;
16408 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16409 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16410 +                                 u.add->path.dentry);
16411 +                       break;
16412 +               case Opt_prepend:
16413 +                       u.add = &opt->add;
16414 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16415 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16416 +                                 u.add->path.dentry);
16417 +                       break;
16418 +
16419 +               case Opt_dirwh:
16420 +                       AuDbg("dirwh %d\n", opt->dirwh);
16421 +                       break;
16422 +               case Opt_rdcache:
16423 +                       AuDbg("rdcache %d\n", opt->rdcache);
16424 +                       break;
16425 +               case Opt_rdblk:
16426 +                       AuDbg("rdblk %d\n", opt->rdblk);
16427 +                       break;
16428 +               case Opt_rdhash:
16429 +                       AuDbg("rdhash %u\n", opt->rdhash);
16430 +                       break;
16431 +
16432 +               case Opt_xino:
16433 +                       u.xino = &opt->xino;
16434 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16435 +                       break;
16436 +
16437 +#define au_fsctx_TF(name)                                        \
16438 +                       case Opt_##name:                          \
16439 +                               if (opt->tf)                      \
16440 +                                       AuLabel(name);            \
16441 +                               else                              \
16442 +                                       AuLabel(no##name);        \
16443 +                               break;
16444 +
16445 +               /* simple true/false flag */
16446 +               au_fsctx_TF(trunc_xino);
16447 +               au_fsctx_TF(trunc_xib);
16448 +               au_fsctx_TF(dirperm1);
16449 +               au_fsctx_TF(plink);
16450 +               au_fsctx_TF(shwh);
16451 +               au_fsctx_TF(dio);
16452 +               au_fsctx_TF(warn_perm);
16453 +               au_fsctx_TF(verbose);
16454 +               au_fsctx_TF(sum);
16455 +               au_fsctx_TF(dirren);
16456 +               au_fsctx_TF(acl);
16457 +#undef au_fsctx_TF
16458 +
16459 +               case Opt_trunc_xino_path:
16460 +                       fallthrough;
16461 +               case Opt_itrunc_xino:
16462 +                       u.xino_itrunc = &opt->xino_itrunc;
16463 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16464 +                       break;
16465 +               case Opt_noxino:
16466 +                       AuLabel(noxino);
16467 +                       break;
16468 +
16469 +               case Opt_list_plink:
16470 +                       AuLabel(list_plink);
16471 +                       break;
16472 +               case Opt_udba:
16473 +                       AuDbg("udba %d, %s\n",
16474 +                                 opt->udba, au_optstr_udba(opt->udba));
16475 +                       break;
16476 +               case Opt_diropq_a:
16477 +                       AuLabel(diropq_a);
16478 +                       break;
16479 +               case Opt_diropq_w:
16480 +                       AuLabel(diropq_w);
16481 +                       break;
16482 +               case Opt_wsum:
16483 +                       AuLabel(wsum);
16484 +                       break;
16485 +               case Opt_wbr_create:
16486 +                       u.create = &opt->wbr_create;
16487 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
16488 +                                 au_optstr_wbr_create(u.create->wbr_create));
16489 +                       switch (u.create->wbr_create) {
16490 +                       case AuWbrCreate_MFSV:
16491 +                               fallthrough;
16492 +                       case AuWbrCreate_PMFSV:
16493 +                               AuDbg("%d sec\n", u.create->mfs_second);
16494 +                               break;
16495 +                       case AuWbrCreate_MFSRR:
16496 +                               fallthrough;
16497 +                       case AuWbrCreate_TDMFS:
16498 +                               AuDbg("%llu watermark\n",
16499 +                                         u.create->mfsrr_watermark);
16500 +                               break;
16501 +                       case AuWbrCreate_MFSRRV:
16502 +                               fallthrough;
16503 +                       case AuWbrCreate_TDMFSV:
16504 +                               fallthrough;
16505 +                       case AuWbrCreate_PMFSRRV:
16506 +                               AuDbg("%llu watermark, %d sec\n",
16507 +                                         u.create->mfsrr_watermark,
16508 +                                         u.create->mfs_second);
16509 +                               break;
16510 +                       }
16511 +                       break;
16512 +               case Opt_wbr_copyup:
16513 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16514 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
16515 +                       break;
16516 +               case Opt_fhsm_sec:
16517 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16518 +                       break;
16519 +
16520 +               default:
16521 +                       AuDbg("type %d\n", opt->type);
16522 +                       BUG();
16523 +               }
16524 +               opt++;
16525 +       }
16526 +#endif
16527 +}
16528 +
16529 +/* ---------------------------------------------------------------------- */
16530 +
16531 +/*
16532 + * For conditionally compiled mount options.
16533 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16534 + */
16535 +#define au_ignore_flag(name, action)           \
16536 +       fsparam_flag(name, action),             \
16537 +       fsparam_flag("no" name, Opt_ignore_silent)
16538 +
16539 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16540 +       fsparam_string("br", Opt_br),
16541 +
16542 +       /* "add=%d:%s" or "ins=%d:%s" */
16543 +       fsparam_string("add", Opt_add),
16544 +       fsparam_string("ins", Opt_add),
16545 +       fsparam_path("append", Opt_append),
16546 +       fsparam_path("prepend", Opt_prepend),
16547 +
16548 +       fsparam_path("del", Opt_del),
16549 +       /* fsparam_s32("idel", Opt_idel), */
16550 +       fsparam_path("mod", Opt_mod),
16551 +       /* fsparam_string("imod", Opt_imod), */
16552 +
16553 +       fsparam_s32("dirwh", Opt_dirwh),
16554 +
16555 +       fsparam_path("xino", Opt_xino),
16556 +       fsparam_flag("noxino", Opt_noxino),
16557 +       fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16558 +       /* "trunc_xino_v=%d:%d" */
16559 +       /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16560 +       fsparam_path("trunc_xino", Opt_trunc_xino_path),
16561 +       fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16562 +       /* fsparam_path("zxino", Opt_zxino), */
16563 +       fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16564 +
16565 +#ifdef CONFIG_PROC_FS
16566 +       fsparam_flag_no("plink", Opt_plink),
16567 +#else
16568 +       au_ignore_flag("plink", Opt_ignore),
16569 +#endif
16570 +
16571 +#ifdef CONFIG_AUFS_DEBUG
16572 +       fsparam_flag("list_plink", Opt_list_plink),
16573 +#endif
16574 +
16575 +       fsparam_string("udba", Opt_udba),
16576 +
16577 +       fsparam_flag_no("dio", Opt_dio),
16578 +
16579 +#ifdef CONFIG_AUFS_DIRREN
16580 +       fsparam_flag_no("dirren", Opt_dirren),
16581 +#else
16582 +       au_ignore_flag("dirren", Opt_ignore),
16583 +#endif
16584 +
16585 +#ifdef CONFIG_AUFS_FHSM
16586 +       fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16587 +#else
16588 +       fsparam_s32("fhsm_sec", Opt_ignore),
16589 +#endif
16590 +
16591 +       /* always | a | whiteouted | w */
16592 +       fsparam_string("diropq", Opt_diropq),
16593 +
16594 +       fsparam_flag_no("warn_perm", Opt_warn_perm),
16595 +
16596 +#ifdef CONFIG_AUFS_SHWH
16597 +       fsparam_flag_no("shwh", Opt_shwh),
16598 +#else
16599 +       au_ignore_flag("shwh", Opt_err),
16600 +#endif
16601 +
16602 +       fsparam_flag_no("dirperm1", Opt_dirperm1),
16603 +
16604 +       fsparam_flag_no("verbose", Opt_verbose),
16605 +       fsparam_flag("v", Opt_verbose),
16606 +       fsparam_flag("quiet", Opt_noverbose),
16607 +       fsparam_flag("q", Opt_noverbose),
16608 +       /* user-space may handle this */
16609 +       fsparam_flag("silent", Opt_noverbose),
16610 +
16611 +       fsparam_flag_no("sum", Opt_sum),
16612 +       fsparam_flag("wsum", Opt_wsum),
16613 +
16614 +       fsparam_s32("rdcache", Opt_rdcache),
16615 +       /* "def" or s32 */
16616 +       fsparam_string("rdblk", Opt_rdblk),
16617 +       /* "def" or s32 */
16618 +       fsparam_string("rdhash", Opt_rdhash),
16619 +
16620 +       fsparam_string("create", Opt_wbr_create),
16621 +       fsparam_string("create_policy", Opt_wbr_create),
16622 +       fsparam_string("cpup", Opt_wbr_copyup),
16623 +       fsparam_string("copyup", Opt_wbr_copyup),
16624 +       fsparam_string("copyup_policy", Opt_wbr_copyup),
16625 +
16626 +       /* generic VFS flag */
16627 +#ifdef CONFIG_FS_POSIX_ACL
16628 +       fsparam_flag_no("acl", Opt_acl),
16629 +#else
16630 +       au_ignore_flag("acl", Opt_ignore),
16631 +#endif
16632 +
16633 +       /* internal use for the scripts */
16634 +       fsparam_string("si", Opt_ignore_silent),
16635 +
16636 +       /* obsoleted, keep them temporary */
16637 +       fsparam_flag("nodlgt", Opt_ignore_silent),
16638 +       fsparam_flag("clean_plink", Opt_ignore),
16639 +       fsparam_string("dirs", Opt_br),
16640 +       fsparam_u32("debug", Opt_ignore),
16641 +       /* "whiteout" or "all" */
16642 +       fsparam_string("delete", Opt_ignore),
16643 +       fsparam_string("imap", Opt_ignore),
16644 +
16645 +       /* temporary workaround, due to old mount(8)? */
16646 +       fsparam_flag("relatime", Opt_ignore_silent),
16647 +
16648 +       {}
16649 +};
16650 +
16651 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16652 +                                char *brspec, size_t speclen,
16653 +                                aufs_bindex_t bindex)
16654 +{
16655 +       int err;
16656 +       char *p;
16657 +
16658 +       AuDbg("brspec %s\n", brspec);
16659 +
16660 +       err = -ENOMEM;
16661 +       if (!speclen)
16662 +               speclen = strlen(brspec);
16663 +       /* will be freed by au_fsctx_free() */
16664 +       p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16665 +       if (unlikely(!p)) {
16666 +               errorfc(fc, "failed in %s", brspec);
16667 +               goto out;
16668 +       }
16669 +       err = au_opt_add(opt, p, fc->sb_flags, bindex);
16670 +
16671 +out:
16672 +       AuTraceErr(err);
16673 +       return err;
16674 +}
16675 +
16676 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16677 +{
16678 +       int err;
16679 +       char *p;
16680 +       struct au_fsctx_opts *a = fc->fs_private;
16681 +       struct au_opt *opt = a->opt;
16682 +       aufs_bindex_t bindex = a->bindex;
16683 +
16684 +       AuDbg("brspec %s\n", brspec);
16685 +
16686 +       err = -EINVAL;
16687 +       while ((p = strsep(&brspec, ":")) && *p) {
16688 +               err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16689 +               AuTraceErr(err);
16690 +               if (unlikely(err))
16691 +                       break;
16692 +               bindex++;
16693 +               opt++;
16694 +               if (unlikely(opt > a->opt_tail)) {
16695 +                       err = -E2BIG;
16696 +                       bindex--;
16697 +                       opt--;
16698 +                       break;
16699 +               }
16700 +               opt->type = Opt_tail;
16701 +               a->skipped = 1;
16702 +       }
16703 +       a->bindex = bindex;
16704 +       a->opt = opt;
16705 +
16706 +       AuTraceErr(err);
16707 +       return err;
16708 +}
16709 +
16710 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16711 +{
16712 +       int err, n;
16713 +       char *p;
16714 +       struct au_fsctx_opts *a = fc->fs_private;
16715 +       struct au_opt *opt = a->opt;
16716 +
16717 +       err = -EINVAL;
16718 +       p = strchr(addspec, ':');
16719 +       if (unlikely(!p)) {
16720 +               errorfc(fc, "bad arg in %s", addspec);
16721 +               goto out;
16722 +       }
16723 +       *p++ = '\0';
16724 +       err = kstrtoint(addspec, 0, &n);
16725 +       if (unlikely(err)) {
16726 +               errorfc(fc, "bad integer in %s", addspec);
16727 +               goto out;
16728 +       }
16729 +       AuDbg("n %d\n", n);
16730 +       err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16731 +
16732 +out:
16733 +       AuTraceErr(err);
16734 +       return err;
16735 +}
16736 +
16737 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16738 +                             struct fs_parameter *param)
16739 +{
16740 +       int err;
16741 +
16742 +       err = -ENOMEM;
16743 +       /* will be freed by au_fsctx_free() */
16744 +       del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16745 +       if (unlikely(!del->pathname))
16746 +               goto out;
16747 +       AuDbg("del %s\n", del->pathname);
16748 +       err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16749 +       if (unlikely(err))
16750 +               errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16751 +
16752 +out:
16753 +       AuTraceErr(err);
16754 +       return err;
16755 +}
16756 +
16757 +#if 0 /* reserved for future use */
16758 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16759 +                              aufs_bindex_t bindex)
16760 +{
16761 +       int err;
16762 +       struct super_block *sb;
16763 +       struct dentry *root;
16764 +       struct au_fsctx_opts *a = fc->fs_private;
16765 +
16766 +       sb = a->sb;
16767 +       AuDebugOn(!sb);
16768 +
16769 +       err = -EINVAL;
16770 +       root = sb->s_root;
16771 +       aufs_read_lock(root, AuLock_FLUSH);
16772 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
16773 +               errorfc(fc, "out of bounds, %d", bindex);
16774 +               goto out;
16775 +       }
16776 +
16777 +       err = 0;
16778 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
16779 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16780 +
16781 +out:
16782 +       aufs_read_unlock(root, !AuLock_IR);
16783 +       AuTraceErr(err);
16784 +       return err;
16785 +}
16786 +#endif
16787 +
16788 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16789 +                             struct fs_parameter *param)
16790 +{
16791 +       int err;
16792 +       struct path path;
16793 +       char *p;
16794 +
16795 +       err = -ENOMEM;
16796 +       /* will be freed by au_fsctx_free() */
16797 +       mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16798 +       if (unlikely(!mod->path))
16799 +               goto out;
16800 +
16801 +       err = -EINVAL;
16802 +       p = strchr(mod->path, '=');
16803 +       if (unlikely(!p)) {
16804 +               errorfc(fc, "no permission %s", mod->path);
16805 +               goto out;
16806 +       }
16807 +
16808 +       *p++ = 0;
16809 +       err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16810 +       if (unlikely(err)) {
16811 +               errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16812 +               goto out;
16813 +       }
16814 +
16815 +       mod->perm = au_br_perm_val(p);
16816 +       AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16817 +       mod->h_root = dget(path.dentry);
16818 +       path_put(&path);
16819 +
16820 +out:
16821 +       AuTraceErr(err);
16822 +       return err;
16823 +}
16824 +
16825 +#if 0 /* reserved for future use */
16826 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16827 +                              char *ibrspec)
16828 +{
16829 +       int err, n;
16830 +       char *p;
16831 +       struct super_block *sb;
16832 +       struct dentry *root;
16833 +       struct au_fsctx_opts *a = fc->fs_private;
16834 +
16835 +       sb = a->sb;
16836 +       AuDebugOn(!sb);
16837 +
16838 +       err = -EINVAL;
16839 +       p = strchr(ibrspec, ':');
16840 +       if (unlikely(!p)) {
16841 +               errorfc(fc, "no index, %s", ibrspec);
16842 +               goto out;
16843 +       }
16844 +       *p++ = '\0';
16845 +       err = kstrtoint(ibrspec, 0, &n);
16846 +       if (unlikely(err)) {
16847 +               errorfc(fc, "bad integer in %s", ibrspec);
16848 +               goto out;
16849 +       }
16850 +       AuDbg("n %d\n", n);
16851 +
16852 +       root = sb->s_root;
16853 +       aufs_read_lock(root, AuLock_FLUSH);
16854 +       if (n < 0 || au_sbbot(sb) < n) {
16855 +               errorfc(fc, "out of bounds, %d", bindex);
16856 +               goto out_root;
16857 +       }
16858 +
16859 +       err = 0;
16860 +       mod->perm = au_br_perm_val(p);
16861 +       AuDbg("mod path %s, perm 0x%x, %s\n",
16862 +             mod->path, mod->perm, p);
16863 +       mod->h_root = dget(au_h_dptr(root, bindex));
16864 +
16865 +out_root:
16866 +       aufs_read_unlock(root, !AuLock_IR);
16867 +out:
16868 +       AuTraceErr(err);
16869 +       return err;
16870 +}
16871 +#endif
16872 +
16873 +static int au_fsctx_parse_xino(struct fs_context *fc,
16874 +                              struct au_opt_xino *xino,
16875 +                              struct fs_parameter *param)
16876 +{
16877 +       int err;
16878 +       struct au_fsctx_opts *a = fc->fs_private;
16879 +
16880 +       err = -ENOMEM;
16881 +       /* will be freed by au_opts_free() */
16882 +       xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16883 +       if (unlikely(!xino->path))
16884 +               goto out;
16885 +       AuDbg("path %s\n", xino->path);
16886 +
16887 +       xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16888 +                                   /*wbrtop*/0);
16889 +       err = PTR_ERR(xino->file);
16890 +       if (IS_ERR(xino->file)) {
16891 +               xino->file = NULL;
16892 +               goto out;
16893 +       }
16894 +
16895 +       err = 0;
16896 +       if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16897 +               err = -EINVAL;
16898 +               errorfc(fc, "%s must be outside", xino->path);
16899 +       }
16900 +
16901 +out:
16902 +       AuTraceErr(err);
16903 +       return err;
16904 +}
16905 +
16906 +static
16907 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16908 +                                   struct au_opt_xino_itrunc *xino_itrunc,
16909 +                                   char *pathname)
16910 +{
16911 +       int err;
16912 +       aufs_bindex_t bbot, bindex;
16913 +       struct path path;
16914 +       struct dentry *root;
16915 +       struct au_fsctx_opts *a = fc->fs_private;
16916 +
16917 +       AuDebugOn(!a->sb);
16918 +
16919 +       err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16920 +       if (unlikely(err)) {
16921 +               errorfc(fc, "lookup failed %s (%d)", pathname, err);
16922 +               goto out;
16923 +       }
16924 +
16925 +       xino_itrunc->bindex = -1;
16926 +       root = a->sb->s_root;
16927 +       aufs_read_lock(root, AuLock_FLUSH);
16928 +       bbot = au_sbbot(a->sb);
16929 +       for (bindex = 0; bindex <= bbot; bindex++) {
16930 +               if (au_h_dptr(root, bindex) == path.dentry) {
16931 +                       xino_itrunc->bindex = bindex;
16932 +                       break;
16933 +               }
16934 +       }
16935 +       aufs_read_unlock(root, !AuLock_IR);
16936 +       path_put(&path);
16937 +
16938 +       if (unlikely(xino_itrunc->bindex < 0)) {
16939 +               err = -EINVAL;
16940 +               errorfc(fc, "no such branch %s", pathname);
16941 +       }
16942 +
16943 +out:
16944 +       AuTraceErr(err);
16945 +       return err;
16946 +}
16947 +
16948 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16949 +                                     struct au_opt_xino_itrunc *xino_itrunc,
16950 +                                     unsigned int bindex)
16951 +{
16952 +       int err;
16953 +       aufs_bindex_t bbot;
16954 +       struct super_block *sb;
16955 +       struct au_fsctx_opts *a = fc->fs_private;
16956 +
16957 +       sb = a->sb;
16958 +       AuDebugOn(!sb);
16959 +
16960 +       err = 0;
16961 +       si_noflush_read_lock(sb);
16962 +       bbot = au_sbbot(sb);
16963 +       si_read_unlock(sb);
16964 +       if (bindex <= bbot)
16965 +               xino_itrunc->bindex = bindex;
16966 +       else {
16967 +               err = -EINVAL;
16968 +               errorfc(fc, "out of bounds, %u", bindex);
16969 +       }
16970 +
16971 +       AuTraceErr(err);
16972 +       return err;
16973 +}
16974 +
16975 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16976 +{
16977 +       int err, token;
16978 +       struct fs_parse_result result;
16979 +       struct au_fsctx_opts *a = fc->fs_private;
16980 +       struct au_opt *opt = a->opt;
16981 +
16982 +       AuDbg("fc %p, param {key %s, string %s}\n",
16983 +             fc, param->key, param->string);
16984 +       err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
16985 +       if (unlikely(err < 0))
16986 +               goto out;
16987 +       token = err;
16988 +       AuDbg("token %d, res{negated %d, uint64 %llu}\n",
16989 +             token, result.negated, result.uint_64);
16990 +
16991 +       err = -EINVAL;
16992 +       a->skipped = 0;
16993 +       switch (token) {
16994 +       case Opt_br:
16995 +               err = au_fsctx_parse_br(fc, param->string);
16996 +               break;
16997 +       case Opt_add:
16998 +               err = au_fsctx_parse_add(fc, param->string);
16999 +               break;
17000 +       case Opt_append:
17001 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17002 +                                           /*dummy bindex*/1);
17003 +               break;
17004 +       case Opt_prepend:
17005 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17006 +                                           /*bindex*/0);
17007 +               break;
17008 +
17009 +       case Opt_del:
17010 +               err = au_fsctx_parse_del(fc, &opt->del, param);
17011 +               break;
17012 +#if 0 /* reserved for future use */
17013 +       case Opt_idel:
17014 +               if (!a->sb) {
17015 +                       err = 0;
17016 +                       a->skipped = 1;
17017 +                       break;
17018 +               }
17019 +               del->pathname = "(indexed)";
17020 +               err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17021 +               break;
17022 +#endif
17023 +
17024 +       case Opt_mod:
17025 +               err = au_fsctx_parse_mod(fc, &opt->mod, param);
17026 +               break;
17027 +#ifdef IMOD /* reserved for future use */
17028 +       case Opt_imod:
17029 +               if (!a->sb) {
17030 +                       err = 0;
17031 +                       a->skipped = 1;
17032 +                       break;
17033 +               }
17034 +               u.mod->path = "(indexed)";
17035 +               err = au_opts_parse_imod(fc, &opt->mod, param->string);
17036 +               break;
17037 +#endif
17038 +
17039 +       case Opt_xino:
17040 +               err = au_fsctx_parse_xino(fc, &opt->xino, param);
17041 +               break;
17042 +       case Opt_trunc_xino_path:
17043 +               if (!a->sb) {
17044 +                       errorfc(fc, "no such branch %s", param->string);
17045 +                       break;
17046 +               }
17047 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17048 +                                                     param->string);
17049 +               break;
17050 +#if 0
17051 +       case Opt_trunc_xino_v:
17052 +               if (!a->sb) {
17053 +                       err = 0;
17054 +                       a->skipped = 1;
17055 +                       break;
17056 +               }
17057 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17058 +                                                     param->string);
17059 +               break;
17060 +#endif
17061 +       case Opt_itrunc_xino:
17062 +               if (!a->sb) {
17063 +                       errorfc(fc, "out of bounds %s", param->string);
17064 +                       break;
17065 +               }
17066 +               err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17067 +                                                result.int_32);
17068 +               break;
17069 +
17070 +       case Opt_dirwh:
17071 +               err = 0;
17072 +               opt->dirwh = result.int_32;
17073 +               break;
17074 +
17075 +       case Opt_rdcache:
17076 +               if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17077 +                       errorfc(fc, "rdcache must be smaller than %d",
17078 +                               AUFS_RDCACHE_MAX);
17079 +                       break;
17080 +               }
17081 +               err = 0;
17082 +               opt->rdcache = result.int_32;
17083 +               break;
17084 +
17085 +       case Opt_rdblk:
17086 +               err = 0;
17087 +               opt->rdblk = AUFS_RDBLK_DEF;
17088 +               if (!strcmp(param->string, "def"))
17089 +                       break;
17090 +
17091 +               err = kstrtoint(param->string, 0, &result.int_32);
17092 +               if (unlikely(err)) {
17093 +                       errorfc(fc, "bad value in %s", param->key);
17094 +                       break;
17095 +               }
17096 +               err = -EINVAL;
17097 +               if (unlikely(result.int_32 < 0
17098 +                            || result.int_32 > KMALLOC_MAX_SIZE)) {
17099 +                       errorfc(fc, "bad value in %s", param->key);
17100 +                       break;
17101 +               }
17102 +               if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17103 +                       errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17104 +                       break;
17105 +               }
17106 +               err = 0;
17107 +               opt->rdblk = result.int_32;
17108 +               break;
17109 +
17110 +       case Opt_rdhash:
17111 +               err = 0;
17112 +               opt->rdhash = AUFS_RDHASH_DEF;
17113 +               if (!strcmp(param->string, "def"))
17114 +                       break;
17115 +
17116 +               err = kstrtoint(param->string, 0, &result.int_32);
17117 +               if (unlikely(err)) {
17118 +                       errorfc(fc, "bad value in %s", param->key);
17119 +                       break;
17120 +               }
17121 +               /* how about zero? */
17122 +               if (result.int_32 < 0
17123 +                   || result.int_32 * sizeof(struct hlist_head)
17124 +                   > KMALLOC_MAX_SIZE) {
17125 +                       err = -EINVAL;
17126 +                       errorfc(fc, "bad integer in %s", param->key);
17127 +                       break;
17128 +               }
17129 +               opt->rdhash = result.int_32;
17130 +               break;
17131 +
17132 +       case Opt_diropq:
17133 +               /*
17134 +                * As other options, fs/aufs/opts.c can handle these strings by
17135 +                * match_token().  But "diropq=" is deprecated now and will
17136 +                * never have other value.  So simple strcmp() is enough here.
17137 +                */
17138 +               if (!strcmp(param->string, "a") ||
17139 +                   !strcmp(param->string, "always")) {
17140 +                       err = 0;
17141 +                       opt->type = Opt_diropq_a;
17142 +               } else if (!strcmp(param->string, "w") ||
17143 +                          !strcmp(param->string, "whiteouted")) {
17144 +                       err = 0;
17145 +                       opt->type = Opt_diropq_w;
17146 +               } else
17147 +                       errorfc(fc, "unknown value %s", param->string);
17148 +               break;
17149 +
17150 +       case Opt_udba:
17151 +               opt->udba = au_udba_val(param->string);
17152 +               if (opt->udba >= 0)
17153 +                       err = 0;
17154 +               else
17155 +                       errorf(fc, "wrong value, %s", param->string);
17156 +               break;
17157 +
17158 +       case Opt_wbr_create:
17159 +               opt->wbr_create.wbr_create
17160 +                       = au_wbr_create_val(param->string, &opt->wbr_create);
17161 +               if (opt->wbr_create.wbr_create >= 0)
17162 +                       err = 0;
17163 +               else
17164 +                       errorf(fc, "wrong value, %s", param->key);
17165 +               break;
17166 +
17167 +       case Opt_wbr_copyup:
17168 +               opt->wbr_copyup = au_wbr_copyup_val(param->string);
17169 +               if (opt->wbr_copyup >= 0)
17170 +                       err = 0;
17171 +               else
17172 +                       errorfc(fc, "wrong value, %s", param->key);
17173 +               break;
17174 +
17175 +       case Opt_fhsm_sec:
17176 +               if (unlikely(result.int_32 < 0)) {
17177 +                       errorfc(fc, "bad integer in %s\n", param->key);
17178 +                       break;
17179 +               }
17180 +               err = 0;
17181 +               if (sysaufs_brs)
17182 +                       opt->fhsm_second = result.int_32;
17183 +               else
17184 +                       warnfc(fc, "ignored %s %s", param->key, param->string);
17185 +               break;
17186 +
17187 +       /* simple true/false flag */
17188 +#define au_fsctx_TF(name)                              \
17189 +               case Opt_##name:                        \
17190 +                       err = 0;                        \
17191 +                       opt->tf = !result.negated;      \
17192 +                       break
17193 +       au_fsctx_TF(trunc_xino);
17194 +       au_fsctx_TF(trunc_xib);
17195 +       au_fsctx_TF(dirperm1);
17196 +       au_fsctx_TF(plink);
17197 +       au_fsctx_TF(shwh);
17198 +       au_fsctx_TF(dio);
17199 +       au_fsctx_TF(warn_perm);
17200 +       au_fsctx_TF(verbose);
17201 +       au_fsctx_TF(sum);
17202 +       au_fsctx_TF(dirren);
17203 +       au_fsctx_TF(acl);
17204 +#undef au_fsctx_TF
17205 +
17206 +       case Opt_noverbose:
17207 +               err = 0;
17208 +               opt->type = Opt_verbose;
17209 +               opt->tf = false;
17210 +               break;
17211 +
17212 +       case Opt_noxino:
17213 +               fallthrough;
17214 +       case Opt_list_plink:
17215 +               fallthrough;
17216 +       case Opt_wsum:
17217 +               err = 0;
17218 +               break;
17219 +
17220 +       case Opt_ignore:
17221 +               warnfc(fc, "ignored %s", param->key);
17222 +               fallthrough;
17223 +       case Opt_ignore_silent:
17224 +               a->skipped = 1;
17225 +               err = 0;
17226 +               break;
17227 +       default:
17228 +               a->skipped = 1;
17229 +               err = -ENOPARAM;
17230 +               break;
17231 +       }
17232 +       if (unlikely(err))
17233 +               goto out;
17234 +       if (a->skipped)
17235 +               goto out;
17236 +
17237 +       switch (token) {
17238 +       case Opt_br:
17239 +               fallthrough;
17240 +       case Opt_noverbose:
17241 +               fallthrough;
17242 +       case Opt_diropq:
17243 +               break;
17244 +       default:
17245 +               opt->type = token;
17246 +               break;
17247 +       }
17248 +       opt++;
17249 +       if (unlikely(opt > a->opt_tail)) {
17250 +               err = -E2BIG;
17251 +               opt--;
17252 +       }
17253 +       opt->type = Opt_tail;
17254 +       a->opt = opt;
17255 +
17256 +out:
17257 +       return err;
17258 +}
17259 +
17260 +/*
17261 + * these options accept both 'name=val' and 'name:val' form.
17262 + * some accept optional '=' in its value.
17263 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17264 + */
17265 +static inline unsigned int is_colonopt(char *str)
17266 +{
17267 +#define do_test(name)                                  \
17268 +       if (!strncmp(str, name ":", sizeof(name)))      \
17269 +               return sizeof(name) - 1
17270 +       do_test("br");
17271 +       do_test("add");
17272 +       do_test("ins");
17273 +       do_test("append");
17274 +       do_test("prepend");
17275 +       do_test("del");
17276 +       /* do_test("idel"); */
17277 +       do_test("mod");
17278 +       /* do_test("imod"); */
17279 +#undef do_test
17280 +
17281 +       return 0;
17282 +}
17283 +
17284 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17285 +{
17286 +       int err;
17287 +       unsigned int u;
17288 +       char *str;
17289 +       struct au_fsctx_opts *a = fc->fs_private;
17290 +
17291 +       str = data;
17292 +       AuDbg("str %s\n", str);
17293 +       while (str) {
17294 +               u = is_colonopt(str);
17295 +               if (u)
17296 +                       str[u] = '=';
17297 +               str = strchr(str, ',');
17298 +               if (!str)
17299 +                       break;
17300 +               str++;
17301 +       }
17302 +       str = data;
17303 +       AuDbg("str %s\n", str);
17304 +
17305 +       err = generic_parse_monolithic(fc, str);
17306 +       AuTraceErr(err);
17307 +       au_fsctx_dump(&a->opts);
17308 +
17309 +       return err;
17310 +}
17311 +
17312 +/* ---------------------------------------------------------------------- */
17313 +
17314 +static void au_fsctx_opts_free(struct au_opts *opts)
17315 +{
17316 +       struct au_opt *opt;
17317 +
17318 +       opt = opts->opt;
17319 +       while (opt->type != Opt_tail) {
17320 +               switch (opt->type) {
17321 +               case Opt_add:
17322 +                       fallthrough;
17323 +               case Opt_append:
17324 +                       fallthrough;
17325 +               case Opt_prepend:
17326 +                       kfree(opt->add.pathname);
17327 +                       path_put(&opt->add.path);
17328 +                       break;
17329 +               case Opt_del:
17330 +                       kfree(opt->del.pathname);
17331 +                       fallthrough;
17332 +               case Opt_idel:
17333 +                       path_put(&opt->del.h_path);
17334 +                       break;
17335 +               case Opt_mod:
17336 +                       kfree(opt->mod.path);
17337 +                       fallthrough;
17338 +               case Opt_imod:
17339 +                       dput(opt->mod.h_root);
17340 +                       break;
17341 +               case Opt_xino:
17342 +                       kfree(opt->xino.path);
17343 +                       fput(opt->xino.file);
17344 +                       break;
17345 +               }
17346 +               opt++;
17347 +       }
17348 +}
17349 +
17350 +static void au_fsctx_free(struct fs_context *fc)
17351 +{
17352 +       struct au_fsctx_opts *a = fc->fs_private;
17353 +
17354 +       /* fs_type=%p, root=%pD */
17355 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17356 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17357 +
17358 +       kobject_put(&a->sbinfo->si_kobj);
17359 +       au_fsctx_opts_free(&a->opts);
17360 +       free_page((unsigned long)a->opts.opt);
17361 +       au_kfree_rcu(a);
17362 +}
17363 +
17364 +static const struct fs_context_operations au_fsctx_ops = {
17365 +       .free                   = au_fsctx_free,
17366 +       .parse_param            = au_fsctx_parse_param,
17367 +       .parse_monolithic       = au_fsctx_parse_monolithic,
17368 +       .get_tree               = au_fsctx_get_tree,
17369 +       .reconfigure            = au_fsctx_reconfigure
17370 +       /*
17371 +        * nfs4 requires ->dup()? No.
17372 +        * I don't know what is this ->dup() for.
17373 +        */
17374 +};
17375 +
17376 +int aufs_fsctx_init(struct fs_context *fc)
17377 +{
17378 +       int err;
17379 +       struct au_fsctx_opts *a;
17380 +
17381 +       /* fs_type=%p, root=%pD */
17382 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17383 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17384 +
17385 +       /* they will be freed by au_fsctx_free() */
17386 +       err = -ENOMEM;
17387 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17388 +       if (unlikely(!a))
17389 +               goto out;
17390 +       a->bindex = 0;
17391 +       a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17392 +       if (unlikely(!a->opts.opt))
17393 +               goto out_a;
17394 +       a->opt = a->opts.opt;
17395 +       a->opt->type = Opt_tail;
17396 +       a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17397 +       a->opt_tail = a->opt + a->opts.max_opt - 1;
17398 +       a->opts.sb_flags = fc->sb_flags;
17399 +
17400 +       a->sb = NULL;
17401 +       if (fc->root) {
17402 +               AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17403 +               a->opts.flags = AuOpts_REMOUNT;
17404 +               a->sb = fc->root->d_sb;
17405 +               a->sbinfo = au_sbi(a->sb);
17406 +               kobject_get(&a->sbinfo->si_kobj);
17407 +       } else {
17408 +               a->sbinfo = au_si_alloc(a->sb);
17409 +               AuDebugOn(!a->sbinfo);
17410 +               err = PTR_ERR(a->sbinfo);
17411 +               if (IS_ERR(a->sbinfo))
17412 +                       goto out_opt;
17413 +               au_rw_write_unlock(&a->sbinfo->si_rwsem);
17414 +       }
17415 +
17416 +       err = 0;
17417 +       fc->fs_private = a;
17418 +       fc->ops = &au_fsctx_ops;
17419 +       goto out; /* success */
17420 +
17421 +out_opt:
17422 +       free_page((unsigned long)a->opts.opt);
17423 +out_a:
17424 +       au_kfree_rcu(a);
17425 +out:
17426 +       AuTraceErr(err);
17427 +       return err;
17428 +}
17429 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
17430 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
17431 +++ linux/fs/aufs/fstype.h      2022-11-05 23:02:18.965889284 +0100
17432 @@ -0,0 +1,401 @@
17433 +/* SPDX-License-Identifier: GPL-2.0 */
17434 +/*
17435 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17436 + *
17437 + * This program is free software; you can redistribute it and/or modify
17438 + * it under the terms of the GNU General Public License as published by
17439 + * the Free Software Foundation; either version 2 of the License, or
17440 + * (at your option) any later version.
17441 + *
17442 + * This program is distributed in the hope that it will be useful,
17443 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17444 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17445 + * GNU General Public License for more details.
17446 + *
17447 + * You should have received a copy of the GNU General Public License
17448 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17449 + */
17450 +
17451 +/*
17452 + * judging filesystem type
17453 + */
17454 +
17455 +#ifndef __AUFS_FSTYPE_H__
17456 +#define __AUFS_FSTYPE_H__
17457 +
17458 +#ifdef __KERNEL__
17459 +
17460 +#include <linux/fs.h>
17461 +#include <linux/magic.h>
17462 +#include <linux/nfs_fs.h>
17463 +#include <linux/romfs_fs.h>
17464 +
17465 +static inline int au_test_aufs(struct super_block *sb)
17466 +{
17467 +       return sb->s_magic == AUFS_SUPER_MAGIC;
17468 +}
17469 +
17470 +static inline const char *au_sbtype(struct super_block *sb)
17471 +{
17472 +       return sb->s_type->name;
17473 +}
17474 +
17475 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17476 +{
17477 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17478 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
17479 +#else
17480 +       return 0;
17481 +#endif
17482 +}
17483 +
17484 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17485 +{
17486 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17487 +       return sb->s_magic == ROMFS_MAGIC;
17488 +#else
17489 +       return 0;
17490 +#endif
17491 +}
17492 +
17493 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17494 +{
17495 +#if IS_ENABLED(CONFIG_CRAMFS)
17496 +       return sb->s_magic == CRAMFS_MAGIC;
17497 +#endif
17498 +       return 0;
17499 +}
17500 +
17501 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17502 +{
17503 +#if IS_ENABLED(CONFIG_NFS_FS)
17504 +       return sb->s_magic == NFS_SUPER_MAGIC;
17505 +#else
17506 +       return 0;
17507 +#endif
17508 +}
17509 +
17510 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17511 +{
17512 +#if IS_ENABLED(CONFIG_FUSE_FS)
17513 +       return sb->s_magic == FUSE_SUPER_MAGIC;
17514 +#else
17515 +       return 0;
17516 +#endif
17517 +}
17518 +
17519 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17520 +{
17521 +#if IS_ENABLED(CONFIG_XFS_FS)
17522 +       return sb->s_magic == XFS_SB_MAGIC;
17523 +#else
17524 +       return 0;
17525 +#endif
17526 +}
17527 +
17528 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17529 +{
17530 +#ifdef CONFIG_TMPFS
17531 +       return sb->s_magic == TMPFS_MAGIC;
17532 +#else
17533 +       return 0;
17534 +#endif
17535 +}
17536 +
17537 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17538 +{
17539 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17540 +       return !strcmp(au_sbtype(sb), "ecryptfs");
17541 +#else
17542 +       return 0;
17543 +#endif
17544 +}
17545 +
17546 +static inline int au_test_ramfs(struct super_block *sb)
17547 +{
17548 +       return sb->s_magic == RAMFS_MAGIC;
17549 +}
17550 +
17551 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17552 +{
17553 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17554 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
17555 +#else
17556 +       return 0;
17557 +#endif
17558 +}
17559 +
17560 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17561 +{
17562 +#ifdef CONFIG_PROC_FS
17563 +       return sb->s_magic == PROC_SUPER_MAGIC;
17564 +#else
17565 +       return 0;
17566 +#endif
17567 +}
17568 +
17569 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17570 +{
17571 +#ifdef CONFIG_SYSFS
17572 +       return sb->s_magic == SYSFS_MAGIC;
17573 +#else
17574 +       return 0;
17575 +#endif
17576 +}
17577 +
17578 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17579 +{
17580 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17581 +       return sb->s_magic == CONFIGFS_MAGIC;
17582 +#else
17583 +       return 0;
17584 +#endif
17585 +}
17586 +
17587 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17588 +{
17589 +#if IS_ENABLED(CONFIG_MINIX_FS)
17590 +       return sb->s_magic == MINIX3_SUPER_MAGIC
17591 +               || sb->s_magic == MINIX2_SUPER_MAGIC
17592 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
17593 +               || sb->s_magic == MINIX_SUPER_MAGIC
17594 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
17595 +#else
17596 +       return 0;
17597 +#endif
17598 +}
17599 +
17600 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17601 +{
17602 +#if IS_ENABLED(CONFIG_FAT_FS)
17603 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
17604 +#else
17605 +       return 0;
17606 +#endif
17607 +}
17608 +
17609 +static inline int au_test_msdos(struct super_block *sb)
17610 +{
17611 +       return au_test_fat(sb);
17612 +}
17613 +
17614 +static inline int au_test_vfat(struct super_block *sb)
17615 +{
17616 +       return au_test_fat(sb);
17617 +}
17618 +
17619 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17620 +{
17621 +#ifdef CONFIG_SECURITYFS
17622 +       return sb->s_magic == SECURITYFS_MAGIC;
17623 +#else
17624 +       return 0;
17625 +#endif
17626 +}
17627 +
17628 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17629 +{
17630 +#if IS_ENABLED(CONFIG_SQUASHFS)
17631 +       return sb->s_magic == SQUASHFS_MAGIC;
17632 +#else
17633 +       return 0;
17634 +#endif
17635 +}
17636 +
17637 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17638 +{
17639 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17640 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
17641 +#else
17642 +       return 0;
17643 +#endif
17644 +}
17645 +
17646 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17647 +{
17648 +#if IS_ENABLED(CONFIG_XENFS)
17649 +       return sb->s_magic == XENFS_SUPER_MAGIC;
17650 +#else
17651 +       return 0;
17652 +#endif
17653 +}
17654 +
17655 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17656 +{
17657 +#ifdef CONFIG_DEBUG_FS
17658 +       return sb->s_magic == DEBUGFS_MAGIC;
17659 +#else
17660 +       return 0;
17661 +#endif
17662 +}
17663 +
17664 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17665 +{
17666 +#if IS_ENABLED(CONFIG_NILFS)
17667 +       return sb->s_magic == NILFS_SUPER_MAGIC;
17668 +#else
17669 +       return 0;
17670 +#endif
17671 +}
17672 +
17673 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17674 +{
17675 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17676 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17677 +#else
17678 +       return 0;
17679 +#endif
17680 +}
17681 +
17682 +/* ---------------------------------------------------------------------- */
17683 +/*
17684 + * they can't be an aufs branch.
17685 + */
17686 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17687 +{
17688 +       return
17689 +#ifndef CONFIG_AUFS_BR_RAMFS
17690 +               au_test_ramfs(sb) ||
17691 +#endif
17692 +               au_test_procfs(sb)
17693 +               || au_test_sysfs(sb)
17694 +               || au_test_configfs(sb)
17695 +               || au_test_debugfs(sb)
17696 +               || au_test_securityfs(sb)
17697 +               || au_test_xenfs(sb)
17698 +               || au_test_ecryptfs(sb)
17699 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
17700 +               || au_test_aufs(sb); /* will be supported in next version */
17701 +}
17702 +
17703 +static inline int au_test_fs_remote(struct super_block *sb)
17704 +{
17705 +       return !au_test_tmpfs(sb)
17706 +#ifdef CONFIG_AUFS_BR_RAMFS
17707 +               && !au_test_ramfs(sb)
17708 +#endif
17709 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17710 +}
17711 +
17712 +/* ---------------------------------------------------------------------- */
17713 +
17714 +/*
17715 + * Note: these functions (below) are created after reading ->getattr() in all
17716 + * filesystems under linux/fs. it means we have to do so in every update...
17717 + */
17718 +
17719 +/*
17720 + * some filesystems require getattr to refresh the inode attributes before
17721 + * referencing.
17722 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17723 + * and leave the work for d_revalidate()
17724 + */
17725 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17726 +{
17727 +       return au_test_nfs(sb)
17728 +               || au_test_fuse(sb)
17729 +               /* || au_test_btrfs(sb) */      /* untested */
17730 +               ;
17731 +}
17732 +
17733 +/*
17734 + * filesystems which don't maintain i_size or i_blocks.
17735 + */
17736 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17737 +{
17738 +       return au_test_xfs(sb)
17739 +               || au_test_btrfs(sb)
17740 +               || au_test_ubifs(sb)
17741 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
17742 +               /* || au_test_minix(sb) */      /* untested */
17743 +               ;
17744 +}
17745 +
17746 +/*
17747 + * filesystems which don't store the correct value in some of their inode
17748 + * attributes.
17749 + */
17750 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17751 +{
17752 +       return au_test_fs_bad_iattr_size(sb)
17753 +               || au_test_fat(sb)
17754 +               || au_test_msdos(sb)
17755 +               || au_test_vfat(sb);
17756 +}
17757 +
17758 +/* they don't check i_nlink in link(2) */
17759 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17760 +{
17761 +       return au_test_tmpfs(sb)
17762 +#ifdef CONFIG_AUFS_BR_RAMFS
17763 +               || au_test_ramfs(sb)
17764 +#endif
17765 +               || au_test_ubifs(sb)
17766 +               || au_test_hfsplus(sb);
17767 +}
17768 +
17769 +/*
17770 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17771 + */
17772 +static inline int au_test_fs_notime(struct super_block *sb)
17773 +{
17774 +       return au_test_nfs(sb)
17775 +               || au_test_fuse(sb)
17776 +               || au_test_ubifs(sb)
17777 +               ;
17778 +}
17779 +
17780 +/* temporary support for i#1 in cramfs */
17781 +static inline int au_test_fs_unique_ino(struct inode *inode)
17782 +{
17783 +       if (au_test_cramfs(inode->i_sb))
17784 +               return inode->i_ino != 1;
17785 +       return 1;
17786 +}
17787 +
17788 +/* ---------------------------------------------------------------------- */
17789 +
17790 +/*
17791 + * the filesystem where the xino files placed must support i/o after unlink and
17792 + * maintain i_size and i_blocks.
17793 + */
17794 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17795 +{
17796 +       return au_test_fs_remote(sb)
17797 +               || au_test_fs_bad_iattr_size(sb)
17798 +               /* don't want unnecessary work for xino */
17799 +               || au_test_aufs(sb)
17800 +               || au_test_ecryptfs(sb)
17801 +               || au_test_nilfs(sb);
17802 +}
17803 +
17804 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17805 +{
17806 +       return au_test_tmpfs(sb)
17807 +               || au_test_ramfs(sb);
17808 +}
17809 +
17810 +/*
17811 + * test if the @sb is real-readonly.
17812 + */
17813 +static inline int au_test_fs_rr(struct super_block *sb)
17814 +{
17815 +       return au_test_squashfs(sb)
17816 +               || au_test_iso9660(sb)
17817 +               || au_test_cramfs(sb)
17818 +               || au_test_romfs(sb);
17819 +}
17820 +
17821 +/*
17822 + * test if the @inode is nfs with 'noacl' option
17823 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17824 + */
17825 +static inline int au_test_nfs_noacl(struct inode *inode)
17826 +{
17827 +       return au_test_nfs(inode->i_sb)
17828 +               /* && IS_POSIXACL(inode) */
17829 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
17830 +}
17831 +
17832 +#endif /* __KERNEL__ */
17833 +#endif /* __AUFS_FSTYPE_H__ */
17834 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
17835 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
17836 +++ linux/fs/aufs/hbl.h 2022-11-05 23:02:18.965889284 +0100
17837 @@ -0,0 +1,65 @@
17838 +/* SPDX-License-Identifier: GPL-2.0 */
17839 +/*
17840 + * Copyright (C) 2017-2022 Junjiro R. Okajima
17841 + *
17842 + * This program is free software; you can redistribute it and/or modify
17843 + * it under the terms of the GNU General Public License as published by
17844 + * the Free Software Foundation; either version 2 of the License, or
17845 + * (at your option) any later version.
17846 + *
17847 + * This program is distributed in the hope that it will be useful,
17848 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17849 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17850 + * GNU General Public License for more details.
17851 + *
17852 + * You should have received a copy of the GNU General Public License
17853 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17854 + */
17855 +
17856 +/*
17857 + * helpers for hlist_bl.h
17858 + */
17859 +
17860 +#ifndef __AUFS_HBL_H__
17861 +#define __AUFS_HBL_H__
17862 +
17863 +#ifdef __KERNEL__
17864 +
17865 +#include <linux/list_bl.h>
17866 +
17867 +static inline void au_hbl_add(struct hlist_bl_node *node,
17868 +                             struct hlist_bl_head *hbl)
17869 +{
17870 +       hlist_bl_lock(hbl);
17871 +       hlist_bl_add_head(node, hbl);
17872 +       hlist_bl_unlock(hbl);
17873 +}
17874 +
17875 +static inline void au_hbl_del(struct hlist_bl_node *node,
17876 +                             struct hlist_bl_head *hbl)
17877 +{
17878 +       hlist_bl_lock(hbl);
17879 +       hlist_bl_del(node);
17880 +       hlist_bl_unlock(hbl);
17881 +}
17882 +
17883 +#define au_hbl_for_each(pos, head)                                     \
17884 +       for (pos = hlist_bl_first(head);                                \
17885 +            pos;                                                       \
17886 +            pos = pos->next)
17887 +
17888 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17889 +{
17890 +       unsigned long cnt;
17891 +       struct hlist_bl_node *pos;
17892 +
17893 +       cnt = 0;
17894 +       hlist_bl_lock(hbl);
17895 +       au_hbl_for_each(pos, hbl)
17896 +               cnt++;
17897 +       hlist_bl_unlock(hbl);
17898 +       return cnt;
17899 +}
17900 +
17901 +#endif /* __KERNEL__ */
17902 +#endif /* __AUFS_HBL_H__ */
17903 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
17904 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
17905 +++ linux/fs/aufs/hfsnotify.c   2022-11-05 23:02:18.965889284 +0100
17906 @@ -0,0 +1,290 @@
17907 +// SPDX-License-Identifier: GPL-2.0
17908 +/*
17909 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17910 + *
17911 + * This program is free software; you can redistribute it and/or modify
17912 + * it under the terms of the GNU General Public License as published by
17913 + * the Free Software Foundation; either version 2 of the License, or
17914 + * (at your option) any later version.
17915 + *
17916 + * This program is distributed in the hope that it will be useful,
17917 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17918 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17919 + * GNU General Public License for more details.
17920 + *
17921 + * You should have received a copy of the GNU General Public License
17922 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17923 + */
17924 +
17925 +/*
17926 + * fsnotify for the lower directories
17927 + */
17928 +
17929 +#include "aufs.h"
17930 +
17931 +/* FS_IN_IGNORED is unnecessary */
17932 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17933 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
17934 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17935 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17936 +
17937 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17938 +{
17939 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17940 +                                            hn_mark);
17941 +       /* AuDbg("here\n"); */
17942 +       au_cache_free_hnotify(hn);
17943 +       smp_mb__before_atomic(); /* for atomic64_dec */
17944 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
17945 +               wake_up(&au_hfsn_wq);
17946 +}
17947 +
17948 +static int au_hfsn_alloc(struct au_hinode *hinode)
17949 +{
17950 +       int err;
17951 +       struct au_hnotify *hn;
17952 +       struct super_block *sb;
17953 +       struct au_branch *br;
17954 +       struct fsnotify_mark *mark;
17955 +       aufs_bindex_t bindex;
17956 +
17957 +       hn = hinode->hi_notify;
17958 +       sb = hn->hn_aufs_inode->i_sb;
17959 +       bindex = au_br_index(sb, hinode->hi_id);
17960 +       br = au_sbr(sb, bindex);
17961 +       AuDebugOn(!br->br_hfsn);
17962 +
17963 +       mark = &hn->hn_mark;
17964 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17965 +       mark->mask = AuHfsnMask;
17966 +       /*
17967 +        * by udba rename or rmdir, aufs assign a new inode to the known
17968 +        * h_inode, so specify 1 to allow dups.
17969 +        */
17970 +       lockdep_off();
17971 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
17972 +       lockdep_on();
17973 +
17974 +       return err;
17975 +}
17976 +
17977 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
17978 +{
17979 +       struct fsnotify_mark *mark;
17980 +       unsigned long long ull;
17981 +       struct fsnotify_group *group;
17982 +
17983 +       ull = atomic64_inc_return(&au_hfsn_ifree);
17984 +       BUG_ON(!ull);
17985 +
17986 +       mark = &hn->hn_mark;
17987 +       spin_lock(&mark->lock);
17988 +       group = mark->group;
17989 +       fsnotify_get_group(group);
17990 +       spin_unlock(&mark->lock);
17991 +       lockdep_off();
17992 +       fsnotify_destroy_mark(mark, group);
17993 +       fsnotify_put_mark(mark);
17994 +       fsnotify_put_group(group);
17995 +       lockdep_on();
17996 +
17997 +       /* free hn by myself */
17998 +       return 0;
17999 +}
18000 +
18001 +/* ---------------------------------------------------------------------- */
18002 +
18003 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
18004 +{
18005 +       struct fsnotify_mark *mark;
18006 +
18007 +       mark = &hinode->hi_notify->hn_mark;
18008 +       spin_lock(&mark->lock);
18009 +       if (do_set) {
18010 +               AuDebugOn(mark->mask & AuHfsnMask);
18011 +               mark->mask |= AuHfsnMask;
18012 +       } else {
18013 +               AuDebugOn(!(mark->mask & AuHfsnMask));
18014 +               mark->mask &= ~AuHfsnMask;
18015 +       }
18016 +       spin_unlock(&mark->lock);
18017 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
18018 +}
18019 +
18020 +/* ---------------------------------------------------------------------- */
18021 +
18022 +/* #define AuDbgHnotify */
18023 +#ifdef AuDbgHnotify
18024 +static char *au_hfsn_name(u32 mask)
18025 +{
18026 +#ifdef CONFIG_AUFS_DEBUG
18027 +#define test_ret(flag)                         \
18028 +       do {                                    \
18029 +               if (mask & flag)                \
18030 +                       return #flag;           \
18031 +       } while (0)
18032 +       test_ret(FS_ACCESS);
18033 +       test_ret(FS_MODIFY);
18034 +       test_ret(FS_ATTRIB);
18035 +       test_ret(FS_CLOSE_WRITE);
18036 +       test_ret(FS_CLOSE_NOWRITE);
18037 +       test_ret(FS_OPEN);
18038 +       test_ret(FS_MOVED_FROM);
18039 +       test_ret(FS_MOVED_TO);
18040 +       test_ret(FS_CREATE);
18041 +       test_ret(FS_DELETE);
18042 +       test_ret(FS_DELETE_SELF);
18043 +       test_ret(FS_MOVE_SELF);
18044 +       test_ret(FS_UNMOUNT);
18045 +       test_ret(FS_Q_OVERFLOW);
18046 +       test_ret(FS_IN_IGNORED);
18047 +       test_ret(FS_ISDIR);
18048 +       test_ret(FS_IN_ONESHOT);
18049 +       test_ret(FS_EVENT_ON_CHILD);
18050 +       return "";
18051 +#undef test_ret
18052 +#else
18053 +       return "??";
18054 +#endif
18055 +}
18056 +#endif
18057 +
18058 +/* ---------------------------------------------------------------------- */
18059 +
18060 +static void au_hfsn_free_group(struct fsnotify_group *group)
18061 +{
18062 +       struct au_br_hfsnotify *hfsn = group->private;
18063 +
18064 +       /* AuDbg("here\n"); */
18065 +       au_kfree_try_rcu(hfsn);
18066 +}
18067 +
18068 +static int au_hfsn_handle_event(struct fsnotify_group *group,
18069 +                               u32 mask, const void *data, int data_type,
18070 +                               struct inode *dir,
18071 +                               const struct qstr *file_name, u32 cookie,
18072 +                               struct fsnotify_iter_info *iter_info)
18073 +{
18074 +       int err;
18075 +       struct au_hnotify *hnotify;
18076 +       struct inode *h_dir, *h_inode;
18077 +       struct fsnotify_mark *inode_mark;
18078 +
18079 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18080 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
18081 +
18082 +       err = 0;
18083 +       /* if FS_UNMOUNT happens, there must be another bug */
18084 +       AuDebugOn(mask & FS_UNMOUNT);
18085 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
18086 +               goto out;
18087 +
18088 +       h_dir = dir;
18089 +       h_inode = NULL;
18090 +#ifdef AuDbgHnotify
18091 +       au_debug_on();
18092 +       if (1 || file_name.len != sizeof(AUFS_XINO_FNAME) - 1
18093 +           || strncmp(file_name.name, AUFS_XINO_FNAME, file_name.len)) {
18094 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18095 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
18096 +                     AuLNPair(file_name), h_inode ? h_inode->i_ino : 0);
18097 +               /* WARN_ON(1); */
18098 +       }
18099 +       au_debug_off();
18100 +#endif
18101 +
18102 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
18103 +       AuDebugOn(!inode_mark);
18104 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
18105 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
18106 +
18107 +out:
18108 +       return err;
18109 +}
18110 +
18111 +static struct fsnotify_ops au_hfsn_ops = {
18112 +       .handle_event           = au_hfsn_handle_event,
18113 +       .free_group_priv        = au_hfsn_free_group,
18114 +       .free_mark              = au_hfsn_free_mark
18115 +};
18116 +
18117 +/* ---------------------------------------------------------------------- */
18118 +
18119 +static void au_hfsn_fin_br(struct au_branch *br)
18120 +{
18121 +       struct au_br_hfsnotify *hfsn;
18122 +
18123 +       hfsn = br->br_hfsn;
18124 +       if (hfsn) {
18125 +               lockdep_off();
18126 +               fsnotify_put_group(hfsn->hfsn_group);
18127 +               lockdep_on();
18128 +       }
18129 +}
18130 +
18131 +static int au_hfsn_init_br(struct au_branch *br, int perm)
18132 +{
18133 +       int err;
18134 +       struct fsnotify_group *group;
18135 +       struct au_br_hfsnotify *hfsn;
18136 +
18137 +       err = 0;
18138 +       br->br_hfsn = NULL;
18139 +       if (!au_br_hnotifyable(perm))
18140 +               goto out;
18141 +
18142 +       err = -ENOMEM;
18143 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18144 +       if (unlikely(!hfsn))
18145 +               goto out;
18146 +
18147 +       err = 0;
18148 +       group = fsnotify_alloc_group(&au_hfsn_ops,
18149 +                                    /*flags - not for userspace*/0);
18150 +       if (IS_ERR(group)) {
18151 +               err = PTR_ERR(group);
18152 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
18153 +               goto out_hfsn;
18154 +       }
18155 +
18156 +       group->private = hfsn;
18157 +       hfsn->hfsn_group = group;
18158 +       br->br_hfsn = hfsn;
18159 +       goto out; /* success */
18160 +
18161 +out_hfsn:
18162 +       au_kfree_try_rcu(hfsn);
18163 +out:
18164 +       return err;
18165 +}
18166 +
18167 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18168 +{
18169 +       int err;
18170 +
18171 +       err = 0;
18172 +       if (!br->br_hfsn)
18173 +               err = au_hfsn_init_br(br, perm);
18174 +
18175 +       return err;
18176 +}
18177 +
18178 +/* ---------------------------------------------------------------------- */
18179 +
18180 +static void au_hfsn_fin(void)
18181 +{
18182 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18183 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18184 +}
18185 +
18186 +const struct au_hnotify_op au_hnotify_op = {
18187 +       .ctl            = au_hfsn_ctl,
18188 +       .alloc          = au_hfsn_alloc,
18189 +       .free           = au_hfsn_free,
18190 +
18191 +       .fin            = au_hfsn_fin,
18192 +
18193 +       .reset_br       = au_hfsn_reset_br,
18194 +       .fin_br         = au_hfsn_fin_br,
18195 +       .init_br        = au_hfsn_init_br
18196 +};
18197 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
18198 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
18199 +++ linux/fs/aufs/hfsplus.c     2022-11-05 23:02:18.965889284 +0100
18200 @@ -0,0 +1,60 @@
18201 +// SPDX-License-Identifier: GPL-2.0
18202 +/*
18203 + * Copyright (C) 2010-2022 Junjiro R. Okajima
18204 + *
18205 + * This program is free software; you can redistribute it and/or modify
18206 + * it under the terms of the GNU General Public License as published by
18207 + * the Free Software Foundation; either version 2 of the License, or
18208 + * (at your option) any later version.
18209 + *
18210 + * This program is distributed in the hope that it will be useful,
18211 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18212 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18213 + * GNU General Public License for more details.
18214 + *
18215 + * You should have received a copy of the GNU General Public License
18216 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18217 + */
18218 +
18219 +/*
18220 + * special support for filesystems which acquires an inode mutex
18221 + * at final closing a file, eg, hfsplus.
18222 + *
18223 + * This trick is very simple and stupid, just to open the file before really
18224 + * necessary open to tell hfsplus that this is not the final closing.
18225 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
18226 + * and au_h_open_post() after releasing it.
18227 + */
18228 +
18229 +#include "aufs.h"
18230 +
18231 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18232 +                          int force_wr)
18233 +{
18234 +       struct file *h_file;
18235 +       struct dentry *h_dentry;
18236 +
18237 +       h_dentry = au_h_dptr(dentry, bindex);
18238 +       AuDebugOn(!h_dentry);
18239 +       AuDebugOn(d_is_negative(h_dentry));
18240 +
18241 +       h_file = NULL;
18242 +       if (au_test_hfsplus(h_dentry->d_sb)
18243 +           && d_is_reg(h_dentry))
18244 +               h_file = au_h_open(dentry, bindex,
18245 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
18246 +                                  /*file*/NULL, force_wr);
18247 +       return h_file;
18248 +}
18249 +
18250 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18251 +                   struct file *h_file)
18252 +{
18253 +       struct au_branch *br;
18254 +
18255 +       if (h_file) {
18256 +               fput(h_file);
18257 +               br = au_sbr(dentry->d_sb, bindex);
18258 +               au_lcnt_dec(&br->br_nfiles);
18259 +       }
18260 +}
18261 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
18262 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
18263 +++ linux/fs/aufs/hnotify.c     2022-11-05 23:02:18.965889284 +0100
18264 @@ -0,0 +1,715 @@
18265 +// SPDX-License-Identifier: GPL-2.0
18266 +/*
18267 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18268 + *
18269 + * This program is free software; you can redistribute it and/or modify
18270 + * it under the terms of the GNU General Public License as published by
18271 + * the Free Software Foundation; either version 2 of the License, or
18272 + * (at your option) any later version.
18273 + *
18274 + * This program is distributed in the hope that it will be useful,
18275 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18276 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18277 + * GNU General Public License for more details.
18278 + *
18279 + * You should have received a copy of the GNU General Public License
18280 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18281 + */
18282 +
18283 +/*
18284 + * abstraction to notify the direct changes on lower directories
18285 + */
18286 +
18287 +/* #include <linux/iversion.h> */
18288 +#include "aufs.h"
18289 +
18290 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18291 +{
18292 +       int err;
18293 +       struct au_hnotify *hn;
18294 +
18295 +       err = -ENOMEM;
18296 +       hn = au_cache_alloc_hnotify();
18297 +       if (hn) {
18298 +               hn->hn_aufs_inode = inode;
18299 +               hinode->hi_notify = hn;
18300 +               err = au_hnotify_op.alloc(hinode);
18301 +               AuTraceErr(err);
18302 +               if (unlikely(err)) {
18303 +                       hinode->hi_notify = NULL;
18304 +                       au_cache_free_hnotify(hn);
18305 +                       /*
18306 +                        * The upper dir was removed by udba, but the same named
18307 +                        * dir left. In this case, aufs assigns a new inode
18308 +                        * number and set the monitor again.
18309 +                        * For the lower dir, the old monitor is still left.
18310 +                        */
18311 +                       if (err == -EEXIST)
18312 +                               err = 0;
18313 +               }
18314 +       }
18315 +
18316 +       AuTraceErr(err);
18317 +       return err;
18318 +}
18319 +
18320 +void au_hn_free(struct au_hinode *hinode)
18321 +{
18322 +       struct au_hnotify *hn;
18323 +
18324 +       hn = hinode->hi_notify;
18325 +       if (hn) {
18326 +               hinode->hi_notify = NULL;
18327 +               if (au_hnotify_op.free(hinode, hn))
18328 +                       au_cache_free_hnotify(hn);
18329 +       }
18330 +}
18331 +
18332 +/* ---------------------------------------------------------------------- */
18333 +
18334 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18335 +{
18336 +       if (hinode->hi_notify)
18337 +               au_hnotify_op.ctl(hinode, do_set);
18338 +}
18339 +
18340 +void au_hn_reset(struct inode *inode, unsigned int flags)
18341 +{
18342 +       aufs_bindex_t bindex, bbot;
18343 +       struct inode *hi;
18344 +       struct dentry *iwhdentry;
18345 +
18346 +       bbot = au_ibbot(inode);
18347 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18348 +               hi = au_h_iptr(inode, bindex);
18349 +               if (!hi)
18350 +                       continue;
18351 +
18352 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18353 +               iwhdentry = au_hi_wh(inode, bindex);
18354 +               if (iwhdentry)
18355 +                       dget(iwhdentry);
18356 +               au_igrab(hi);
18357 +               au_set_h_iptr(inode, bindex, NULL, 0);
18358 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
18359 +                             flags & ~AuHi_XINO);
18360 +               iput(hi);
18361 +               dput(iwhdentry);
18362 +               /* inode_unlock(hi); */
18363 +       }
18364 +}
18365 +
18366 +/* ---------------------------------------------------------------------- */
18367 +
18368 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18369 +{
18370 +       int err;
18371 +       aufs_bindex_t bindex, bbot, bfound, btop;
18372 +       struct inode *h_i;
18373 +
18374 +       err = 0;
18375 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18376 +               pr_warn("branch root dir was changed\n");
18377 +               goto out;
18378 +       }
18379 +
18380 +       bfound = -1;
18381 +       bbot = au_ibbot(inode);
18382 +       btop = au_ibtop(inode);
18383 +#if 0 /* reserved for future use */
18384 +       if (bindex == bbot) {
18385 +               /* keep this ino in rename case */
18386 +               goto out;
18387 +       }
18388 +#endif
18389 +       for (bindex = btop; bindex <= bbot; bindex++)
18390 +               if (au_h_iptr(inode, bindex) == h_inode) {
18391 +                       bfound = bindex;
18392 +                       break;
18393 +               }
18394 +       if (bfound < 0)
18395 +               goto out;
18396 +
18397 +       for (bindex = btop; bindex <= bbot; bindex++) {
18398 +               h_i = au_h_iptr(inode, bindex);
18399 +               if (!h_i)
18400 +                       continue;
18401 +
18402 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18403 +               /* ignore this error */
18404 +               /* bad action? */
18405 +       }
18406 +
18407 +       /* children inode number will be broken */
18408 +
18409 +out:
18410 +       AuTraceErr(err);
18411 +       return err;
18412 +}
18413 +
18414 +static int hn_gen_tree(struct dentry *dentry)
18415 +{
18416 +       int err, i, j, ndentry;
18417 +       struct au_dcsub_pages dpages;
18418 +       struct au_dpage *dpage;
18419 +       struct dentry **dentries;
18420 +
18421 +       err = au_dpages_init(&dpages, GFP_NOFS);
18422 +       if (unlikely(err))
18423 +               goto out;
18424 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18425 +       if (unlikely(err))
18426 +               goto out_dpages;
18427 +
18428 +       for (i = 0; i < dpages.ndpage; i++) {
18429 +               dpage = dpages.dpages + i;
18430 +               dentries = dpage->dentries;
18431 +               ndentry = dpage->ndentry;
18432 +               for (j = 0; j < ndentry; j++) {
18433 +                       struct dentry *d;
18434 +
18435 +                       d = dentries[j];
18436 +                       if (IS_ROOT(d))
18437 +                               continue;
18438 +
18439 +                       au_digen_dec(d);
18440 +                       if (d_really_is_positive(d))
18441 +                               /* todo: reset children xino?
18442 +                                  cached children only? */
18443 +                               au_iigen_dec(d_inode(d));
18444 +               }
18445 +       }
18446 +
18447 +out_dpages:
18448 +       au_dpages_free(&dpages);
18449 +out:
18450 +       return err;
18451 +}
18452 +
18453 +/*
18454 + * return 0 if processed.
18455 + */
18456 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18457 +                          const unsigned int isdir)
18458 +{
18459 +       int err;
18460 +       struct dentry *d;
18461 +       struct qstr *dname;
18462 +
18463 +       err = 1;
18464 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18465 +               pr_warn("branch root dir was changed\n");
18466 +               err = 0;
18467 +               goto out;
18468 +       }
18469 +
18470 +       if (!isdir) {
18471 +               AuDebugOn(!name);
18472 +               au_iigen_dec(inode);
18473 +               spin_lock(&inode->i_lock);
18474 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18475 +                       spin_lock(&d->d_lock);
18476 +                       dname = &d->d_name;
18477 +                       if (dname->len != nlen
18478 +                           && memcmp(dname->name, name, nlen)) {
18479 +                               spin_unlock(&d->d_lock);
18480 +                               continue;
18481 +                       }
18482 +                       err = 0;
18483 +                       au_digen_dec(d);
18484 +                       spin_unlock(&d->d_lock);
18485 +                       break;
18486 +               }
18487 +               spin_unlock(&inode->i_lock);
18488 +       } else {
18489 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18490 +               d = d_find_any_alias(inode);
18491 +               if (!d) {
18492 +                       au_iigen_dec(inode);
18493 +                       goto out;
18494 +               }
18495 +
18496 +               spin_lock(&d->d_lock);
18497 +               dname = &d->d_name;
18498 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18499 +                       spin_unlock(&d->d_lock);
18500 +                       err = hn_gen_tree(d);
18501 +                       spin_lock(&d->d_lock);
18502 +               }
18503 +               spin_unlock(&d->d_lock);
18504 +               dput(d);
18505 +       }
18506 +
18507 +out:
18508 +       AuTraceErr(err);
18509 +       return err;
18510 +}
18511 +
18512 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18513 +{
18514 +       int err;
18515 +
18516 +       if (IS_ROOT(dentry)) {
18517 +               pr_warn("branch root dir was changed\n");
18518 +               return 0;
18519 +       }
18520 +
18521 +       err = 0;
18522 +       if (!isdir) {
18523 +               au_digen_dec(dentry);
18524 +               if (d_really_is_positive(dentry))
18525 +                       au_iigen_dec(d_inode(dentry));
18526 +       } else {
18527 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18528 +               if (d_really_is_positive(dentry))
18529 +                       err = hn_gen_tree(dentry);
18530 +       }
18531 +
18532 +       AuTraceErr(err);
18533 +       return err;
18534 +}
18535 +
18536 +/* ---------------------------------------------------------------------- */
18537 +
18538 +/* hnotify job flags */
18539 +#define AuHnJob_XINO0          1
18540 +#define AuHnJob_GEN            (1 << 1)
18541 +#define AuHnJob_DIRENT         (1 << 2)
18542 +#define AuHnJob_ISDIR          (1 << 3)
18543 +#define AuHnJob_TRYXINO0       (1 << 4)
18544 +#define AuHnJob_MNTPNT         (1 << 5)
18545 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
18546 +#define au_fset_hnjob(flags, name) \
18547 +       do { (flags) |= AuHnJob_##name; } while (0)
18548 +#define au_fclr_hnjob(flags, name) \
18549 +       do { (flags) &= ~AuHnJob_##name; } while (0)
18550 +
18551 +enum {
18552 +       AuHn_CHILD,
18553 +       AuHn_PARENT,
18554 +       AuHnLast
18555 +};
18556 +
18557 +struct au_hnotify_args {
18558 +       struct inode *h_dir, *dir, *h_child_inode;
18559 +       u32 mask;
18560 +       unsigned int flags[AuHnLast];
18561 +       unsigned int h_child_nlen;
18562 +       char h_child_name[];
18563 +};
18564 +
18565 +struct hn_job_args {
18566 +       unsigned int flags;
18567 +       struct inode *inode, *h_inode, *dir, *h_dir;
18568 +       struct dentry *dentry;
18569 +       char *h_name;
18570 +       int h_nlen;
18571 +};
18572 +
18573 +static int hn_job(struct hn_job_args *a)
18574 +{
18575 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18576 +       int e;
18577 +
18578 +       /* reset xino */
18579 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18580 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
18581 +
18582 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
18583 +           && a->inode
18584 +           && a->h_inode) {
18585 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18586 +               if (!a->h_inode->i_nlink
18587 +                   && !(a->h_inode->i_state & I_LINKABLE))
18588 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
18589 +               inode_unlock_shared(a->h_inode);
18590 +       }
18591 +
18592 +       /* make the generation obsolete */
18593 +       if (au_ftest_hnjob(a->flags, GEN)) {
18594 +               e = -1;
18595 +               if (a->inode)
18596 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18597 +                                             isdir);
18598 +               if (e && a->dentry)
18599 +                       hn_gen_by_name(a->dentry, isdir);
18600 +               /* ignore this error */
18601 +       }
18602 +
18603 +       /* make dir entries obsolete */
18604 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18605 +               struct au_vdir *vdir;
18606 +
18607 +               vdir = au_ivdir(a->inode);
18608 +               if (vdir)
18609 +                       vdir->vd_jiffy = 0;
18610 +               /* IMustLock(a->inode); */
18611 +               /* inode_inc_iversion(a->inode); */
18612 +       }
18613 +
18614 +       /* can do nothing but warn */
18615 +       if (au_ftest_hnjob(a->flags, MNTPNT)
18616 +           && a->dentry
18617 +           && d_mountpoint(a->dentry))
18618 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18619 +
18620 +       return 0;
18621 +}
18622 +
18623 +/* ---------------------------------------------------------------------- */
18624 +
18625 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18626 +                                          struct inode *dir)
18627 +{
18628 +       struct dentry *dentry, *d, *parent;
18629 +       struct qstr *dname;
18630 +
18631 +       parent = d_find_any_alias(dir);
18632 +       if (!parent)
18633 +               return NULL;
18634 +
18635 +       dentry = NULL;
18636 +       spin_lock(&parent->d_lock);
18637 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
18638 +               /* AuDbg("%pd\n", d); */
18639 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18640 +               dname = &d->d_name;
18641 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
18642 +                       goto cont_unlock;
18643 +               if (au_di(d))
18644 +                       au_digen_dec(d);
18645 +               else
18646 +                       goto cont_unlock;
18647 +               if (au_dcount(d) > 0) {
18648 +                       dentry = dget_dlock(d);
18649 +                       spin_unlock(&d->d_lock);
18650 +                       break;
18651 +               }
18652 +
18653 +cont_unlock:
18654 +               spin_unlock(&d->d_lock);
18655 +       }
18656 +       spin_unlock(&parent->d_lock);
18657 +       dput(parent);
18658 +
18659 +       if (dentry)
18660 +               di_write_lock_child(dentry);
18661 +
18662 +       return dentry;
18663 +}
18664 +
18665 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18666 +                                        aufs_bindex_t bindex, ino_t h_ino)
18667 +{
18668 +       struct inode *inode;
18669 +       ino_t ino;
18670 +       int err;
18671 +
18672 +       inode = NULL;
18673 +       err = au_xino_read(sb, bindex, h_ino, &ino);
18674 +       if (!err && ino)
18675 +               inode = ilookup(sb, ino);
18676 +       if (!inode)
18677 +               goto out;
18678 +
18679 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18680 +               pr_warn("wrong root branch\n");
18681 +               iput(inode);
18682 +               inode = NULL;
18683 +               goto out;
18684 +       }
18685 +
18686 +       ii_write_lock_child(inode);
18687 +
18688 +out:
18689 +       return inode;
18690 +}
18691 +
18692 +static void au_hn_bh(void *_args)
18693 +{
18694 +       struct au_hnotify_args *a = _args;
18695 +       struct super_block *sb;
18696 +       aufs_bindex_t bindex, bbot, bfound;
18697 +       unsigned char xino, try_iput;
18698 +       int err;
18699 +       struct inode *inode;
18700 +       ino_t h_ino;
18701 +       struct hn_job_args args;
18702 +       struct dentry *dentry;
18703 +       struct au_sbinfo *sbinfo;
18704 +
18705 +       AuDebugOn(!_args);
18706 +       AuDebugOn(!a->h_dir);
18707 +       AuDebugOn(!a->dir);
18708 +       AuDebugOn(!a->mask);
18709 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18710 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
18711 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
18712 +
18713 +       inode = NULL;
18714 +       dentry = NULL;
18715 +       /*
18716 +        * do not lock a->dir->i_mutex here
18717 +        * because of d_revalidate() may cause a deadlock.
18718 +        */
18719 +       sb = a->dir->i_sb;
18720 +       AuDebugOn(!sb);
18721 +       sbinfo = au_sbi(sb);
18722 +       AuDebugOn(!sbinfo);
18723 +       si_write_lock(sb, AuLock_NOPLMW);
18724 +
18725 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18726 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18727 +               case FS_MOVED_FROM:
18728 +               case FS_MOVED_TO:
18729 +                       AuWarn1("DIRREN with UDBA may not work correctly "
18730 +                               "for the direct rename(2)\n");
18731 +               }
18732 +
18733 +       ii_read_lock_parent(a->dir);
18734 +       bfound = -1;
18735 +       bbot = au_ibbot(a->dir);
18736 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18737 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18738 +                       bfound = bindex;
18739 +                       break;
18740 +               }
18741 +       ii_read_unlock(a->dir);
18742 +       if (unlikely(bfound < 0))
18743 +               goto out;
18744 +
18745 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
18746 +       h_ino = 0;
18747 +       if (a->h_child_inode)
18748 +               h_ino = a->h_child_inode->i_ino;
18749 +
18750 +       if (a->h_child_nlen
18751 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18752 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18753 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18754 +                                             a->dir);
18755 +       try_iput = 0;
18756 +       if (dentry && d_really_is_positive(dentry))
18757 +               inode = d_inode(dentry);
18758 +       if (xino && !inode && h_ino
18759 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18760 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18761 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18762 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18763 +               try_iput = 1;
18764 +       }
18765 +
18766 +       args.flags = a->flags[AuHn_CHILD];
18767 +       args.dentry = dentry;
18768 +       args.inode = inode;
18769 +       args.h_inode = a->h_child_inode;
18770 +       args.dir = a->dir;
18771 +       args.h_dir = a->h_dir;
18772 +       args.h_name = a->h_child_name;
18773 +       args.h_nlen = a->h_child_nlen;
18774 +       err = hn_job(&args);
18775 +       if (dentry) {
18776 +               if (au_di(dentry))
18777 +                       di_write_unlock(dentry);
18778 +               dput(dentry);
18779 +       }
18780 +       if (inode && try_iput) {
18781 +               ii_write_unlock(inode);
18782 +               iput(inode);
18783 +       }
18784 +
18785 +       ii_write_lock_parent(a->dir);
18786 +       args.flags = a->flags[AuHn_PARENT];
18787 +       args.dentry = NULL;
18788 +       args.inode = a->dir;
18789 +       args.h_inode = a->h_dir;
18790 +       args.dir = NULL;
18791 +       args.h_dir = NULL;
18792 +       args.h_name = NULL;
18793 +       args.h_nlen = 0;
18794 +       err = hn_job(&args);
18795 +       ii_write_unlock(a->dir);
18796 +
18797 +out:
18798 +       iput(a->h_child_inode);
18799 +       iput(a->h_dir);
18800 +       iput(a->dir);
18801 +       si_write_unlock(sb);
18802 +       au_nwt_done(&sbinfo->si_nowait);
18803 +       au_kfree_rcu(a);
18804 +}
18805 +
18806 +/* ---------------------------------------------------------------------- */
18807 +
18808 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18809 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
18810 +{
18811 +       int err, len;
18812 +       unsigned int flags[AuHnLast], f;
18813 +       unsigned char isdir, isroot, wh;
18814 +       struct inode *dir;
18815 +       struct au_hnotify_args *args;
18816 +       char *p, *h_child_name;
18817 +
18818 +       err = 0;
18819 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18820 +       dir = igrab(hnotify->hn_aufs_inode);
18821 +       if (!dir)
18822 +               goto out;
18823 +
18824 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
18825 +       wh = 0;
18826 +       h_child_name = (void *)h_child_qstr->name;
18827 +       len = h_child_qstr->len;
18828 +       if (h_child_name) {
18829 +               if (len > AUFS_WH_PFX_LEN
18830 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18831 +                       h_child_name += AUFS_WH_PFX_LEN;
18832 +                       len -= AUFS_WH_PFX_LEN;
18833 +                       wh = 1;
18834 +               }
18835 +       }
18836 +
18837 +       isdir = 0;
18838 +       if (h_child_inode)
18839 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
18840 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
18841 +       flags[AuHn_CHILD] = 0;
18842 +       if (isdir)
18843 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
18844 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18845 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
18846 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18847 +       case FS_MOVED_FROM:
18848 +       case FS_MOVED_TO:
18849 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18850 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18851 +               fallthrough;
18852 +       case FS_CREATE:
18853 +               AuDebugOn(!h_child_name);
18854 +               break;
18855 +
18856 +       case FS_DELETE:
18857 +               /*
18858 +                * aufs never be able to get this child inode.
18859 +                * revalidation should be in d_revalidate()
18860 +                * by checking i_nlink, i_generation or d_unhashed().
18861 +                */
18862 +               AuDebugOn(!h_child_name);
18863 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18864 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18865 +               break;
18866 +
18867 +       default:
18868 +               AuDebugOn(1);
18869 +       }
18870 +
18871 +       if (wh)
18872 +               h_child_inode = NULL;
18873 +
18874 +       err = -ENOMEM;
18875 +       /* iput() and kfree() will be called in au_hnotify() */
18876 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18877 +       if (unlikely(!args)) {
18878 +               AuErr1("no memory\n");
18879 +               iput(dir);
18880 +               goto out;
18881 +       }
18882 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18883 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18884 +       args->mask = mask;
18885 +       args->dir = dir;
18886 +       args->h_dir = igrab(h_dir);
18887 +       if (h_child_inode)
18888 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
18889 +       args->h_child_inode = h_child_inode;
18890 +       args->h_child_nlen = len;
18891 +       if (len) {
18892 +               p = (void *)args;
18893 +               p += sizeof(*args);
18894 +               memcpy(p, h_child_name, len);
18895 +               p[len] = 0;
18896 +       }
18897 +
18898 +       /* NFS fires the event for silly-renamed one from kworker */
18899 +       f = 0;
18900 +       if (!dir->i_nlink
18901 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18902 +               f = AuWkq_NEST;
18903 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18904 +       if (unlikely(err)) {
18905 +               pr_err("wkq %d\n", err);
18906 +               iput(args->h_child_inode);
18907 +               iput(args->h_dir);
18908 +               iput(args->dir);
18909 +               au_kfree_rcu(args);
18910 +       }
18911 +
18912 +out:
18913 +       return err;
18914 +}
18915 +
18916 +/* ---------------------------------------------------------------------- */
18917 +
18918 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18919 +{
18920 +       int err;
18921 +
18922 +       AuDebugOn(!(udba & AuOptMask_UDBA));
18923 +
18924 +       err = 0;
18925 +       if (au_hnotify_op.reset_br)
18926 +               err = au_hnotify_op.reset_br(udba, br, perm);
18927 +
18928 +       return err;
18929 +}
18930 +
18931 +int au_hnotify_init_br(struct au_branch *br, int perm)
18932 +{
18933 +       int err;
18934 +
18935 +       err = 0;
18936 +       if (au_hnotify_op.init_br)
18937 +               err = au_hnotify_op.init_br(br, perm);
18938 +
18939 +       return err;
18940 +}
18941 +
18942 +void au_hnotify_fin_br(struct au_branch *br)
18943 +{
18944 +       if (au_hnotify_op.fin_br)
18945 +               au_hnotify_op.fin_br(br);
18946 +}
18947 +
18948 +static void au_hn_destroy_cache(void)
18949 +{
18950 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18951 +       au_cache[AuCache_HNOTIFY] = NULL;
18952 +}
18953 +
18954 +int __init au_hnotify_init(void)
18955 +{
18956 +       int err;
18957 +
18958 +       err = -ENOMEM;
18959 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18960 +       if (au_cache[AuCache_HNOTIFY]) {
18961 +               err = 0;
18962 +               if (au_hnotify_op.init)
18963 +                       err = au_hnotify_op.init();
18964 +               if (unlikely(err))
18965 +                       au_hn_destroy_cache();
18966 +       }
18967 +       AuTraceErr(err);
18968 +       return err;
18969 +}
18970 +
18971 +void au_hnotify_fin(void)
18972 +{
18973 +       if (au_hnotify_op.fin)
18974 +               au_hnotify_op.fin();
18975 +
18976 +       /* cf. au_cache_fin() */
18977 +       if (au_cache[AuCache_HNOTIFY])
18978 +               au_hn_destroy_cache();
18979 +}
18980 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
18981 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
18982 +++ linux/fs/aufs/iinfo.c       2022-11-05 23:02:18.965889284 +0100
18983 @@ -0,0 +1,286 @@
18984 +// SPDX-License-Identifier: GPL-2.0
18985 +/*
18986 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18987 + *
18988 + * This program is free software; you can redistribute it and/or modify
18989 + * it under the terms of the GNU General Public License as published by
18990 + * the Free Software Foundation; either version 2 of the License, or
18991 + * (at your option) any later version.
18992 + *
18993 + * This program is distributed in the hope that it will be useful,
18994 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18995 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18996 + * GNU General Public License for more details.
18997 + *
18998 + * You should have received a copy of the GNU General Public License
18999 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19000 + */
19001 +
19002 +/*
19003 + * inode private data
19004 + */
19005 +
19006 +#include "aufs.h"
19007 +
19008 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
19009 +{
19010 +       struct inode *h_inode;
19011 +       struct au_hinode *hinode;
19012 +
19013 +       IiMustAnyLock(inode);
19014 +
19015 +       hinode = au_hinode(au_ii(inode), bindex);
19016 +       h_inode = hinode->hi_inode;
19017 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19018 +       return h_inode;
19019 +}
19020 +
19021 +/* todo: hard/soft set? */
19022 +void au_hiput(struct au_hinode *hinode)
19023 +{
19024 +       au_hn_free(hinode);
19025 +       dput(hinode->hi_whdentry);
19026 +       iput(hinode->hi_inode);
19027 +}
19028 +
19029 +unsigned int au_hi_flags(struct inode *inode, int isdir)
19030 +{
19031 +       unsigned int flags;
19032 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
19033 +
19034 +       flags = 0;
19035 +       if (au_opt_test(mnt_flags, XINO))
19036 +               au_fset_hi(flags, XINO);
19037 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19038 +               au_fset_hi(flags, HNOTIFY);
19039 +       return flags;
19040 +}
19041 +
19042 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19043 +                  struct inode *h_inode, unsigned int flags)
19044 +{
19045 +       struct au_hinode *hinode;
19046 +       struct inode *hi;
19047 +       struct au_iinfo *iinfo = au_ii(inode);
19048 +
19049 +       IiMustWriteLock(inode);
19050 +
19051 +       hinode = au_hinode(iinfo, bindex);
19052 +       hi = hinode->hi_inode;
19053 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19054 +
19055 +       if (hi)
19056 +               au_hiput(hinode);
19057 +       hinode->hi_inode = h_inode;
19058 +       if (h_inode) {
19059 +               int err;
19060 +               struct super_block *sb = inode->i_sb;
19061 +               struct au_branch *br;
19062 +
19063 +               AuDebugOn(inode->i_mode
19064 +                         && (h_inode->i_mode & S_IFMT)
19065 +                         != (inode->i_mode & S_IFMT));
19066 +               if (bindex == iinfo->ii_btop)
19067 +                       au_cpup_igen(inode, h_inode);
19068 +               br = au_sbr(sb, bindex);
19069 +               hinode->hi_id = br->br_id;
19070 +               if (au_ftest_hi(flags, XINO)) {
19071 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
19072 +                                           inode->i_ino);
19073 +                       if (unlikely(err))
19074 +                               AuIOErr1("failed au_xino_write() %d\n", err);
19075 +               }
19076 +
19077 +               if (au_ftest_hi(flags, HNOTIFY)
19078 +                   && au_br_hnotifyable(br->br_perm)) {
19079 +                       err = au_hn_alloc(hinode, inode);
19080 +                       if (unlikely(err))
19081 +                               AuIOErr1("au_hn_alloc() %d\n", err);
19082 +               }
19083 +       }
19084 +}
19085 +
19086 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19087 +                 struct dentry *h_wh)
19088 +{
19089 +       struct au_hinode *hinode;
19090 +
19091 +       IiMustWriteLock(inode);
19092 +
19093 +       hinode = au_hinode(au_ii(inode), bindex);
19094 +       AuDebugOn(hinode->hi_whdentry);
19095 +       hinode->hi_whdentry = h_wh;
19096 +}
19097 +
19098 +void au_update_iigen(struct inode *inode, int half)
19099 +{
19100 +       struct au_iinfo *iinfo;
19101 +       struct au_iigen *iigen;
19102 +       unsigned int sigen;
19103 +
19104 +       sigen = au_sigen(inode->i_sb);
19105 +       iinfo = au_ii(inode);
19106 +       iigen = &iinfo->ii_generation;
19107 +       spin_lock(&iigen->ig_spin);
19108 +       iigen->ig_generation = sigen;
19109 +       if (half)
19110 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19111 +       else
19112 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
19113 +       spin_unlock(&iigen->ig_spin);
19114 +}
19115 +
19116 +/* it may be called at remount time, too */
19117 +void au_update_ibrange(struct inode *inode, int do_put_zero)
19118 +{
19119 +       struct au_iinfo *iinfo;
19120 +       aufs_bindex_t bindex, bbot;
19121 +
19122 +       AuDebugOn(au_is_bad_inode(inode));
19123 +       IiMustWriteLock(inode);
19124 +
19125 +       iinfo = au_ii(inode);
19126 +       if (do_put_zero && iinfo->ii_btop >= 0) {
19127 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19128 +                    bindex++) {
19129 +                       struct inode *h_i;
19130 +
19131 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
19132 +                       if (h_i
19133 +                           && !h_i->i_nlink
19134 +                           && !(h_i->i_state & I_LINKABLE))
19135 +                               au_set_h_iptr(inode, bindex, NULL, 0);
19136 +               }
19137 +       }
19138 +
19139 +       iinfo->ii_btop = -1;
19140 +       iinfo->ii_bbot = -1;
19141 +       bbot = au_sbbot(inode->i_sb);
19142 +       for (bindex = 0; bindex <= bbot; bindex++)
19143 +               if (au_hinode(iinfo, bindex)->hi_inode) {
19144 +                       iinfo->ii_btop = bindex;
19145 +                       break;
19146 +               }
19147 +       if (iinfo->ii_btop >= 0)
19148 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19149 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
19150 +                               iinfo->ii_bbot = bindex;
19151 +                               break;
19152 +                       }
19153 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
19154 +}
19155 +
19156 +/* ---------------------------------------------------------------------- */
19157 +
19158 +void au_icntnr_init_once(void *_c)
19159 +{
19160 +       struct au_icntnr *c = _c;
19161 +       struct au_iinfo *iinfo = &c->iinfo;
19162 +
19163 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
19164 +       au_rw_init(&iinfo->ii_rwsem);
19165 +       inode_init_once(&c->vfs_inode);
19166 +}
19167 +
19168 +void au_hinode_init(struct au_hinode *hinode)
19169 +{
19170 +       hinode->hi_inode = NULL;
19171 +       hinode->hi_id = -1;
19172 +       au_hn_init(hinode);
19173 +       hinode->hi_whdentry = NULL;
19174 +}
19175 +
19176 +int au_iinfo_init(struct inode *inode)
19177 +{
19178 +       struct au_iinfo *iinfo;
19179 +       struct super_block *sb;
19180 +       struct au_hinode *hi;
19181 +       int nbr, i;
19182 +
19183 +       sb = inode->i_sb;
19184 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19185 +       nbr = au_sbbot(sb) + 1;
19186 +       if (unlikely(nbr <= 0))
19187 +               nbr = 1;
19188 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19189 +       if (hi) {
19190 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
19191 +
19192 +               iinfo->ii_hinode = hi;
19193 +               for (i = 0; i < nbr; i++, hi++)
19194 +                       au_hinode_init(hi);
19195 +
19196 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
19197 +               iinfo->ii_btop = -1;
19198 +               iinfo->ii_bbot = -1;
19199 +               iinfo->ii_vdir = NULL;
19200 +               return 0;
19201 +       }
19202 +       return -ENOMEM;
19203 +}
19204 +
19205 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
19206 +{
19207 +       int err, i;
19208 +       struct au_hinode *hip;
19209 +
19210 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
19211 +
19212 +       err = -ENOMEM;
19213 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19214 +                         may_shrink);
19215 +       if (hip) {
19216 +               iinfo->ii_hinode = hip;
19217 +               i = iinfo->ii_bbot + 1;
19218 +               hip += i;
19219 +               for (; i < nbr; i++, hip++)
19220 +                       au_hinode_init(hip);
19221 +               err = 0;
19222 +       }
19223 +
19224 +       return err;
19225 +}
19226 +
19227 +void au_iinfo_fin(struct inode *inode)
19228 +{
19229 +       struct au_iinfo *iinfo;
19230 +       struct au_hinode *hi;
19231 +       struct super_block *sb;
19232 +       aufs_bindex_t bindex, bbot;
19233 +       const unsigned char unlinked = !inode->i_nlink;
19234 +
19235 +       AuDebugOn(au_is_bad_inode(inode));
19236 +
19237 +       sb = inode->i_sb;
19238 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
19239 +       if (si_pid_test(sb))
19240 +               au_xino_delete_inode(inode, unlinked);
19241 +       else {
19242 +               /*
19243 +                * it is safe to hide the dependency between sbinfo and
19244 +                * sb->s_umount.
19245 +                */
19246 +               lockdep_off();
19247 +               si_noflush_read_lock(sb);
19248 +               au_xino_delete_inode(inode, unlinked);
19249 +               si_read_unlock(sb);
19250 +               lockdep_on();
19251 +       }
19252 +
19253 +       iinfo = au_ii(inode);
19254 +       if (iinfo->ii_vdir)
19255 +               au_vdir_free(iinfo->ii_vdir);
19256 +
19257 +       bindex = iinfo->ii_btop;
19258 +       if (bindex >= 0) {
19259 +               hi = au_hinode(iinfo, bindex);
19260 +               bbot = iinfo->ii_bbot;
19261 +               while (bindex++ <= bbot) {
19262 +                       if (hi->hi_inode)
19263 +                               au_hiput(hi);
19264 +                       hi++;
19265 +               }
19266 +       }
19267 +       au_kfree_rcu(iinfo->ii_hinode);
19268 +       AuRwDestroy(&iinfo->ii_rwsem);
19269 +}
19270 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
19271 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
19272 +++ linux/fs/aufs/inode.c       2023-10-31 09:31:04.199880750 +0100
19273 @@ -0,0 +1,531 @@
19274 +// SPDX-License-Identifier: GPL-2.0
19275 +/*
19276 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19277 + *
19278 + * This program is free software; you can redistribute it and/or modify
19279 + * it under the terms of the GNU General Public License as published by
19280 + * the Free Software Foundation; either version 2 of the License, or
19281 + * (at your option) any later version.
19282 + *
19283 + * This program is distributed in the hope that it will be useful,
19284 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19285 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19286 + * GNU General Public License for more details.
19287 + *
19288 + * You should have received a copy of the GNU General Public License
19289 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19290 + */
19291 +
19292 +/*
19293 + * inode functions
19294 + */
19295 +
19296 +#include <linux/iversion.h>
19297 +#include "aufs.h"
19298 +
19299 +struct inode *au_igrab(struct inode *inode)
19300 +{
19301 +       if (inode) {
19302 +               AuDebugOn(!atomic_read(&inode->i_count));
19303 +               ihold(inode);
19304 +       }
19305 +       return inode;
19306 +}
19307 +
19308 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19309 +{
19310 +       au_cpup_attr_all(inode, /*force*/0);
19311 +       au_update_iigen(inode, /*half*/1);
19312 +       if (do_version)
19313 +               inode_inc_iversion(inode);
19314 +}
19315 +
19316 +static int au_ii_refresh(struct inode *inode, int *update)
19317 +{
19318 +       int err, e, nbr;
19319 +       umode_t type;
19320 +       aufs_bindex_t bindex, new_bindex;
19321 +       struct super_block *sb;
19322 +       struct au_iinfo *iinfo;
19323 +       struct au_hinode *p, *q, tmp;
19324 +
19325 +       AuDebugOn(au_is_bad_inode(inode));
19326 +       IiMustWriteLock(inode);
19327 +
19328 +       *update = 0;
19329 +       sb = inode->i_sb;
19330 +       nbr = au_sbbot(sb) + 1;
19331 +       type = inode->i_mode & S_IFMT;
19332 +       iinfo = au_ii(inode);
19333 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
19334 +       if (unlikely(err))
19335 +               goto out;
19336 +
19337 +       AuDebugOn(iinfo->ii_btop < 0);
19338 +       p = au_hinode(iinfo, iinfo->ii_btop);
19339 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19340 +            bindex++, p++) {
19341 +               if (!p->hi_inode)
19342 +                       continue;
19343 +
19344 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
19345 +               new_bindex = au_br_index(sb, p->hi_id);
19346 +               if (new_bindex == bindex)
19347 +                       continue;
19348 +
19349 +               if (new_bindex < 0) {
19350 +                       *update = 1;
19351 +                       au_hiput(p);
19352 +                       p->hi_inode = NULL;
19353 +                       continue;
19354 +               }
19355 +
19356 +               if (new_bindex < iinfo->ii_btop)
19357 +                       iinfo->ii_btop = new_bindex;
19358 +               if (iinfo->ii_bbot < new_bindex)
19359 +                       iinfo->ii_bbot = new_bindex;
19360 +               /* swap two lower inode, and loop again */
19361 +               q = au_hinode(iinfo, new_bindex);
19362 +               tmp = *q;
19363 +               *q = *p;
19364 +               *p = tmp;
19365 +               if (tmp.hi_inode) {
19366 +                       bindex--;
19367 +                       p--;
19368 +               }
19369 +       }
19370 +       au_update_ibrange(inode, /*do_put_zero*/0);
19371 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
19372 +       e = au_dy_irefresh(inode);
19373 +       if (unlikely(e && !err))
19374 +               err = e;
19375 +
19376 +out:
19377 +       AuTraceErr(err);
19378 +       return err;
19379 +}
19380 +
19381 +void au_refresh_iop(struct inode *inode, int force_getattr)
19382 +{
19383 +       int type;
19384 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19385 +       const struct inode_operations *iop
19386 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
19387 +
19388 +       if (inode->i_op == iop)
19389 +               return;
19390 +
19391 +       switch (inode->i_mode & S_IFMT) {
19392 +       case S_IFDIR:
19393 +               type = AuIop_DIR;
19394 +               break;
19395 +       case S_IFLNK:
19396 +               type = AuIop_SYMLINK;
19397 +               break;
19398 +       default:
19399 +               type = AuIop_OTHER;
19400 +               break;
19401 +       }
19402 +
19403 +       inode->i_op = iop + type;
19404 +       /* unnecessary smp_wmb() */
19405 +}
19406 +
19407 +int au_refresh_hinode_self(struct inode *inode)
19408 +{
19409 +       int err, update;
19410 +
19411 +       err = au_ii_refresh(inode, &update);
19412 +       if (!err)
19413 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19414 +
19415 +       AuTraceErr(err);
19416 +       return err;
19417 +}
19418 +
19419 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19420 +{
19421 +       int err, e, update;
19422 +       unsigned int flags;
19423 +       umode_t mode;
19424 +       aufs_bindex_t bindex, bbot;
19425 +       unsigned char isdir;
19426 +       struct au_hinode *p;
19427 +       struct au_iinfo *iinfo;
19428 +
19429 +       err = au_ii_refresh(inode, &update);
19430 +       if (unlikely(err))
19431 +               goto out;
19432 +
19433 +       update = 0;
19434 +       iinfo = au_ii(inode);
19435 +       p = au_hinode(iinfo, iinfo->ii_btop);
19436 +       mode = (inode->i_mode & S_IFMT);
19437 +       isdir = S_ISDIR(mode);
19438 +       flags = au_hi_flags(inode, isdir);
19439 +       bbot = au_dbbot(dentry);
19440 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
19441 +               struct inode *h_i, *h_inode;
19442 +               struct dentry *h_d;
19443 +
19444 +               h_d = au_h_dptr(dentry, bindex);
19445 +               if (!h_d || d_is_negative(h_d))
19446 +                       continue;
19447 +
19448 +               h_inode = d_inode(h_d);
19449 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
19450 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
19451 +                       h_i = au_h_iptr(inode, bindex);
19452 +                       if (h_i) {
19453 +                               if (h_i == h_inode)
19454 +                                       continue;
19455 +                               err = -EIO;
19456 +                               break;
19457 +                       }
19458 +               }
19459 +               if (bindex < iinfo->ii_btop)
19460 +                       iinfo->ii_btop = bindex;
19461 +               if (iinfo->ii_bbot < bindex)
19462 +                       iinfo->ii_bbot = bindex;
19463 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
19464 +               update = 1;
19465 +       }
19466 +       au_update_ibrange(inode, /*do_put_zero*/0);
19467 +       e = au_dy_irefresh(inode);
19468 +       if (unlikely(e && !err))
19469 +               err = e;
19470 +       if (!err)
19471 +               au_refresh_hinode_attr(inode, update && isdir);
19472 +
19473 +out:
19474 +       AuTraceErr(err);
19475 +       return err;
19476 +}
19477 +
19478 +static int set_inode(struct inode *inode, struct dentry *dentry)
19479 +{
19480 +       int err;
19481 +       unsigned int flags;
19482 +       umode_t mode;
19483 +       aufs_bindex_t bindex, btop, btail;
19484 +       unsigned char isdir;
19485 +       struct dentry *h_dentry;
19486 +       struct inode *h_inode;
19487 +       struct au_iinfo *iinfo;
19488 +       const struct inode_operations *iop;
19489 +
19490 +       IiMustWriteLock(inode);
19491 +
19492 +       err = 0;
19493 +       isdir = 0;
19494 +       iop = au_sbi(inode->i_sb)->si_iop_array;
19495 +       btop = au_dbtop(dentry);
19496 +       h_dentry = au_h_dptr(dentry, btop);
19497 +       h_inode = d_inode(h_dentry);
19498 +       mode = h_inode->i_mode;
19499 +       switch (mode & S_IFMT) {
19500 +       case S_IFREG:
19501 +               btail = au_dbtail(dentry);
19502 +               inode->i_op = iop + AuIop_OTHER;
19503 +               inode->i_fop = &aufs_file_fop;
19504 +               err = au_dy_iaop(inode, btop, h_inode);
19505 +               if (unlikely(err))
19506 +                       goto out;
19507 +               break;
19508 +       case S_IFDIR:
19509 +               isdir = 1;
19510 +               btail = au_dbtaildir(dentry);
19511 +               inode->i_op = iop + AuIop_DIR;
19512 +               inode->i_fop = &aufs_dir_fop;
19513 +               break;
19514 +       case S_IFLNK:
19515 +               btail = au_dbtail(dentry);
19516 +               inode->i_op = iop + AuIop_SYMLINK;
19517 +               break;
19518 +       case S_IFBLK:
19519 +       case S_IFCHR:
19520 +       case S_IFIFO:
19521 +       case S_IFSOCK:
19522 +               btail = au_dbtail(dentry);
19523 +               inode->i_op = iop + AuIop_OTHER;
19524 +               init_special_inode(inode, mode, h_inode->i_rdev);
19525 +               break;
19526 +       default:
19527 +               AuIOErr("Unknown file type 0%o\n", mode);
19528 +               err = -EIO;
19529 +               goto out;
19530 +       }
19531 +
19532 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
19533 +       flags = au_hi_flags(inode, isdir);
19534 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19535 +           && au_ftest_hi(flags, HNOTIFY)
19536 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
19537 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19538 +               au_fclr_hi(flags, HNOTIFY);
19539 +       iinfo = au_ii(inode);
19540 +       iinfo->ii_btop = btop;
19541 +       iinfo->ii_bbot = btail;
19542 +       for (bindex = btop; bindex <= btail; bindex++) {
19543 +               h_dentry = au_h_dptr(dentry, bindex);
19544 +               if (h_dentry)
19545 +                       au_set_h_iptr(inode, bindex,
19546 +                                     au_igrab(d_inode(h_dentry)), flags);
19547 +       }
19548 +       au_cpup_attr_all(inode, /*force*/1);
19549 +       /*
19550 +        * to force calling aufs_get_inode_acl() every time,
19551 +        * do not call cache_no_acl() for aufs inode.
19552 +        */
19553 +
19554 +out:
19555 +       return err;
19556 +}
19557 +
19558 +/*
19559 + * successful returns with iinfo write_locked
19560 + * minus: errno
19561 + * zero: success, matched
19562 + * plus: no error, but unmatched
19563 + */
19564 +static int reval_inode(struct inode *inode, struct dentry *dentry)
19565 +{
19566 +       int err;
19567 +       unsigned int gen, igflags;
19568 +       aufs_bindex_t bindex, bbot;
19569 +       struct inode *h_inode, *h_dinode;
19570 +       struct dentry *h_dentry;
19571 +
19572 +       /*
19573 +        * before this function, if aufs got any iinfo lock, it must be only
19574 +        * one, the parent dir.
19575 +        * it can happen by UDBA and the obsoleted inode number.
19576 +        */
19577 +       err = -EIO;
19578 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
19579 +               goto out;
19580 +
19581 +       err = 1;
19582 +       ii_write_lock_new_child(inode);
19583 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
19584 +       h_dinode = d_inode(h_dentry);
19585 +       bbot = au_ibbot(inode);
19586 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
19587 +               h_inode = au_h_iptr(inode, bindex);
19588 +               if (!h_inode || h_inode != h_dinode)
19589 +                       continue;
19590 +
19591 +               err = 0;
19592 +               gen = au_iigen(inode, &igflags);
19593 +               if (gen == au_digen(dentry)
19594 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
19595 +                       break;
19596 +
19597 +               /* fully refresh inode using dentry */
19598 +               err = au_refresh_hinode(inode, dentry);
19599 +               if (!err)
19600 +                       au_update_iigen(inode, /*half*/0);
19601 +               break;
19602 +       }
19603 +
19604 +       if (unlikely(err))
19605 +               ii_write_unlock(inode);
19606 +out:
19607 +       return err;
19608 +}
19609 +
19610 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19611 +          unsigned int d_type, ino_t *ino)
19612 +{
19613 +       int err, idx;
19614 +       const int isnondir = d_type != DT_DIR;
19615 +
19616 +       /* prevent hardlinked inode number from race condition */
19617 +       if (isnondir) {
19618 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19619 +               if (unlikely(err))
19620 +                       goto out;
19621 +       }
19622 +
19623 +       err = au_xino_read(sb, bindex, h_ino, ino);
19624 +       if (unlikely(err))
19625 +               goto out_xinondir;
19626 +
19627 +       if (!*ino) {
19628 +               err = -EIO;
19629 +               *ino = au_xino_new_ino(sb);
19630 +               if (unlikely(!*ino))
19631 +                       goto out_xinondir;
19632 +               err = au_xino_write(sb, bindex, h_ino, *ino);
19633 +               if (unlikely(err))
19634 +                       goto out_xinondir;
19635 +       }
19636 +
19637 +out_xinondir:
19638 +       if (isnondir && idx >= 0)
19639 +               au_xinondir_leave(sb, bindex, h_ino, idx);
19640 +out:
19641 +       return err;
19642 +}
19643 +
19644 +/* successful returns with iinfo write_locked */
19645 +/* todo: return with unlocked? */
19646 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
19647 +{
19648 +       struct inode *inode, *h_inode;
19649 +       struct dentry *h_dentry;
19650 +       struct super_block *sb;
19651 +       ino_t h_ino, ino;
19652 +       int err, idx, hlinked;
19653 +       aufs_bindex_t btop;
19654 +
19655 +       sb = dentry->d_sb;
19656 +       btop = au_dbtop(dentry);
19657 +       h_dentry = au_h_dptr(dentry, btop);
19658 +       h_inode = d_inode(h_dentry);
19659 +       h_ino = h_inode->i_ino;
19660 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
19661 +
19662 +new_ino:
19663 +       /*
19664 +        * stop 'race'-ing between hardlinks under different
19665 +        * parents.
19666 +        */
19667 +       if (hlinked) {
19668 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
19669 +               inode = ERR_PTR(err);
19670 +               if (unlikely(err))
19671 +                       goto out;
19672 +       }
19673 +
19674 +       err = au_xino_read(sb, btop, h_ino, &ino);
19675 +       inode = ERR_PTR(err);
19676 +       if (unlikely(err))
19677 +               goto out_xinondir;
19678 +
19679 +       if (!ino) {
19680 +               ino = au_xino_new_ino(sb);
19681 +               if (unlikely(!ino)) {
19682 +                       inode = ERR_PTR(-EIO);
19683 +                       goto out_xinondir;
19684 +               }
19685 +       }
19686 +
19687 +       AuDbg("i%lu\n", (unsigned long)ino);
19688 +       inode = au_iget_locked(sb, ino);
19689 +       err = PTR_ERR(inode);
19690 +       if (IS_ERR(inode))
19691 +               goto out_xinondir;
19692 +
19693 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19694 +       if (inode->i_state & I_NEW) {
19695 +               ii_write_lock_new_child(inode);
19696 +               err = set_inode(inode, dentry);
19697 +               if (!err) {
19698 +                       unlock_new_inode(inode);
19699 +                       goto out_xinondir; /* success */
19700 +               }
19701 +
19702 +               /*
19703 +                * iget_failed() calls iput(), but we need to call
19704 +                * ii_write_unlock() after iget_failed(). so dirty hack for
19705 +                * i_count.
19706 +                */
19707 +               atomic_inc(&inode->i_count);
19708 +               iget_failed(inode);
19709 +               ii_write_unlock(inode);
19710 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
19711 +               /* ignore this error */
19712 +               goto out_iput;
19713 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
19714 +               /*
19715 +                * horrible race condition between lookup, readdir and copyup
19716 +                * (or something).
19717 +                */
19718 +               if (hlinked && idx >= 0)
19719 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19720 +               err = reval_inode(inode, dentry);
19721 +               if (unlikely(err < 0)) {
19722 +                       hlinked = 0;
19723 +                       goto out_iput;
19724 +               }
19725 +               if (!err)
19726 +                       goto out; /* success */
19727 +               else if (hlinked && idx >= 0) {
19728 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
19729 +                       if (unlikely(err)) {
19730 +                               iput(inode);
19731 +                               inode = ERR_PTR(err);
19732 +                               goto out;
19733 +                       }
19734 +               }
19735 +       }
19736 +
19737 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
19738 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
19739 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
19740 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
19741 +                       (unsigned long)h_ino, (unsigned long)ino);
19742 +       ino = 0;
19743 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
19744 +       if (!err) {
19745 +               iput(inode);
19746 +               if (hlinked && idx >= 0)
19747 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19748 +               goto new_ino;
19749 +       }
19750 +
19751 +out_iput:
19752 +       iput(inode);
19753 +       inode = ERR_PTR(err);
19754 +out_xinondir:
19755 +       if (hlinked && idx >= 0)
19756 +               au_xinondir_leave(sb, btop, h_ino, idx);
19757 +out:
19758 +       return inode;
19759 +}
19760 +
19761 +/* ---------------------------------------------------------------------- */
19762 +
19763 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19764 +              struct inode *inode)
19765 +{
19766 +       int err;
19767 +       struct inode *hi;
19768 +
19769 +       err = au_br_rdonly(au_sbr(sb, bindex));
19770 +
19771 +       /* pseudo-link after flushed may happen out of bounds */
19772 +       if (!err
19773 +           && inode
19774 +           && au_ibtop(inode) <= bindex
19775 +           && bindex <= au_ibbot(inode)) {
19776 +               /*
19777 +                * permission check is unnecessary since vfsub routine
19778 +                * will be called later
19779 +                */
19780 +               hi = au_h_iptr(inode, bindex);
19781 +               if (hi)
19782 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
19783 +       }
19784 +
19785 +       return err;
19786 +}
19787 +
19788 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19789 +                  int mask)
19790 +{
19791 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
19792 +               return 0;
19793 +       return inode_permission(h_idmap, h_inode, mask);
19794 +}
19795 +
19796 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19797 +                      int mask)
19798 +{
19799 +       if (au_test_nfs(h_inode->i_sb)
19800 +           && (mask & MAY_WRITE)
19801 +           && S_ISDIR(h_inode->i_mode))
19802 +               mask |= MAY_READ; /* force permission check */
19803 +       return au_test_h_perm(h_idmap, h_inode, mask);
19804 +}
19805 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
19806 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
19807 +++ linux/fs/aufs/inode.h       2023-10-31 09:31:04.199880750 +0100
19808 @@ -0,0 +1,707 @@
19809 +/* SPDX-License-Identifier: GPL-2.0 */
19810 +/*
19811 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19812 + *
19813 + * This program is free software; you can redistribute it and/or modify
19814 + * it under the terms of the GNU General Public License as published by
19815 + * the Free Software Foundation; either version 2 of the License, or
19816 + * (at your option) any later version.
19817 + *
19818 + * This program is distributed in the hope that it will be useful,
19819 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19820 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19821 + * GNU General Public License for more details.
19822 + *
19823 + * You should have received a copy of the GNU General Public License
19824 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19825 + */
19826 +
19827 +/*
19828 + * inode operations
19829 + */
19830 +
19831 +#ifndef __AUFS_INODE_H__
19832 +#define __AUFS_INODE_H__
19833 +
19834 +#ifdef __KERNEL__
19835 +
19836 +#include <linux/fsnotify.h>
19837 +#include "rwsem.h"
19838 +
19839 +struct vfsmount;
19840 +
19841 +struct au_hnotify {
19842 +#ifdef CONFIG_AUFS_HNOTIFY
19843 +#ifdef CONFIG_AUFS_HFSNOTIFY
19844 +       /* never use fsnotify_add_vfsmount_mark() */
19845 +       struct fsnotify_mark            hn_mark;
19846 +#endif
19847 +       struct inode            *hn_aufs_inode; /* no get/put */
19848 +       struct rcu_head         rcu;
19849 +#endif
19850 +} ____cacheline_aligned_in_smp;
19851 +
19852 +struct au_hinode {
19853 +       struct inode            *hi_inode;
19854 +       aufs_bindex_t           hi_id;
19855 +#ifdef CONFIG_AUFS_HNOTIFY
19856 +       struct au_hnotify       *hi_notify;
19857 +#endif
19858 +
19859 +       /* reference to the copied-up whiteout with get/put */
19860 +       struct dentry           *hi_whdentry;
19861 +};
19862 +
19863 +/* ig_flags */
19864 +#define AuIG_HALF_REFRESHED            1
19865 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
19866 +#define au_ig_fset(flags, name) \
19867 +       do { (flags) |= AuIG_##name; } while (0)
19868 +#define au_ig_fclr(flags, name) \
19869 +       do { (flags) &= ~AuIG_##name; } while (0)
19870 +
19871 +struct au_iigen {
19872 +       spinlock_t      ig_spin;
19873 +       __u32           ig_generation, ig_flags;
19874 +};
19875 +
19876 +struct au_vdir;
19877 +struct au_iinfo {
19878 +       struct au_iigen         ii_generation;
19879 +       struct super_block      *ii_hsb1;       /* no get/put */
19880 +
19881 +       struct au_rwsem         ii_rwsem;
19882 +       aufs_bindex_t           ii_btop, ii_bbot;
19883 +       __u32                   ii_higen;
19884 +       struct au_hinode        *ii_hinode;
19885 +       struct au_vdir          *ii_vdir;
19886 +};
19887 +
19888 +struct au_icntnr {
19889 +       struct au_iinfo         iinfo;
19890 +       struct inode            vfs_inode;
19891 +       struct hlist_bl_node    plink;
19892 +       struct rcu_head         rcu;
19893 +} ____cacheline_aligned_in_smp;
19894 +
19895 +/* au_pin flags */
19896 +#define AuPin_DI_LOCKED                1
19897 +#define AuPin_MNT_WRITE                (1 << 1)
19898 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
19899 +#define au_fset_pin(flags, name) \
19900 +       do { (flags) |= AuPin_##name; } while (0)
19901 +#define au_fclr_pin(flags, name) \
19902 +       do { (flags) &= ~AuPin_##name; } while (0)
19903 +
19904 +struct au_pin {
19905 +       /* input */
19906 +       struct dentry *dentry;
19907 +       unsigned int udba;
19908 +       unsigned char lsc_di, lsc_hi, flags;
19909 +       aufs_bindex_t bindex;
19910 +
19911 +       /* output */
19912 +       struct dentry *parent;
19913 +       struct au_hinode *hdir;
19914 +       struct vfsmount *h_mnt;
19915 +
19916 +       /* temporary unlock/relock for copyup */
19917 +       struct dentry *h_dentry, *h_parent;
19918 +       struct au_branch *br;
19919 +       struct task_struct *task;
19920 +};
19921 +
19922 +void au_pin_hdir_unlock(struct au_pin *p);
19923 +int au_pin_hdir_lock(struct au_pin *p);
19924 +int au_pin_hdir_relock(struct au_pin *p);
19925 +void au_pin_hdir_acquire_nest(struct au_pin *p);
19926 +void au_pin_hdir_release(struct au_pin *p);
19927 +
19928 +/* ---------------------------------------------------------------------- */
19929 +
19930 +static inline struct au_iinfo *au_ii(struct inode *inode)
19931 +{
19932 +       BUG_ON(is_bad_inode(inode));
19933 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19934 +}
19935 +
19936 +/* ---------------------------------------------------------------------- */
19937 +
19938 +/* inode.c */
19939 +struct inode *au_igrab(struct inode *inode);
19940 +void au_refresh_iop(struct inode *inode, int force_getattr);
19941 +int au_refresh_hinode_self(struct inode *inode);
19942 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19943 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19944 +          unsigned int d_type, ino_t *ino);
19945 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
19946 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19947 +              struct inode *inode);
19948 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19949 +                  int mask);
19950 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19951 +                      int mask);
19952 +
19953 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19954 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
19955 +{
19956 +#ifdef CONFIG_AUFS_SHWH
19957 +       return au_ino(sb, bindex, h_ino, d_type, ino);
19958 +#else
19959 +       return 0;
19960 +#endif
19961 +}
19962 +
19963 +/* i_op.c */
19964 +enum {
19965 +       AuIop_SYMLINK,
19966 +       AuIop_DIR,
19967 +       AuIop_OTHER,
19968 +       AuIop_Last
19969 +};
19970 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
19971 +       aufs_iop_nogetattr[AuIop_Last];
19972 +
19973 +/* au_wr_dir flags */
19974 +#define AuWrDir_ADD_ENTRY      1
19975 +#define AuWrDir_ISDIR          (1 << 1)
19976 +#define AuWrDir_TMPFILE                (1 << 2)
19977 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
19978 +#define au_fset_wrdir(flags, name) \
19979 +       do { (flags) |= AuWrDir_##name; } while (0)
19980 +#define au_fclr_wrdir(flags, name) \
19981 +       do { (flags) &= ~AuWrDir_##name; } while (0)
19982 +
19983 +struct au_wr_dir_args {
19984 +       aufs_bindex_t force_btgt;
19985 +       unsigned char flags;
19986 +};
19987 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19988 +             struct au_wr_dir_args *args);
19989 +
19990 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
19991 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
19992 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19993 +                unsigned int udba, unsigned char flags);
19994 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19995 +          unsigned int udba, unsigned char flags) __must_check;
19996 +int au_do_pin(struct au_pin *pin) __must_check;
19997 +void au_unpin(struct au_pin *pin);
19998 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
19999 +
20000 +#define AuIcpup_DID_CPUP       1
20001 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
20002 +#define au_fset_icpup(flags, name) \
20003 +       do { (flags) |= AuIcpup_##name; } while (0)
20004 +#define au_fclr_icpup(flags, name) \
20005 +       do { (flags) &= ~AuIcpup_##name; } while (0)
20006 +
20007 +struct au_icpup_args {
20008 +       unsigned char flags;
20009 +       unsigned char pin_flags;
20010 +       aufs_bindex_t btgt;
20011 +       unsigned int udba;
20012 +       struct au_pin pin;
20013 +       struct path h_path;
20014 +       struct inode *h_inode;
20015 +};
20016 +
20017 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20018 +                    struct au_icpup_args *a);
20019 +
20020 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20021 +                     struct path *h_path, int locked);
20022 +
20023 +/* i_op_add.c */
20024 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20025 +              struct dentry *h_parent, int isdir);
20026 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
20027 +              struct dentry *dentry, umode_t mode, dev_t dev);
20028 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
20029 +                struct dentry *dentry, const char *symname);
20030 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
20031 +               struct dentry *dentry, umode_t mode, bool want_excl);
20032 +struct vfsub_aopen_args;
20033 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20034 +                      struct vfsub_aopen_args *args);
20035 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
20036 +                struct file *file, umode_t mode);
20037 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20038 +             struct dentry *dentry);
20039 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
20040 +              struct dentry *dentry, umode_t mode);
20041 +
20042 +/* i_op_del.c */
20043 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20044 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20045 +              struct dentry *h_parent, int isdir);
20046 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
20047 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
20048 +
20049 +/* i_op_ren.c */
20050 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
20051 +int aufs_rename(struct mnt_idmap *idmap,
20052 +               struct inode *_src_dir, struct dentry *_src_dentry,
20053 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
20054 +               unsigned int _flags);
20055 +
20056 +/* iinfo.c */
20057 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20058 +void au_hiput(struct au_hinode *hinode);
20059 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20060 +                 struct dentry *h_wh);
20061 +unsigned int au_hi_flags(struct inode *inode, int isdir);
20062 +
20063 +/* hinode flags */
20064 +#define AuHi_XINO      1
20065 +#define AuHi_HNOTIFY   (1 << 1)
20066 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
20067 +#define au_fset_hi(flags, name) \
20068 +       do { (flags) |= AuHi_##name; } while (0)
20069 +#define au_fclr_hi(flags, name) \
20070 +       do { (flags) &= ~AuHi_##name; } while (0)
20071 +
20072 +#ifndef CONFIG_AUFS_HNOTIFY
20073 +#undef AuHi_HNOTIFY
20074 +#define AuHi_HNOTIFY   0
20075 +#endif
20076 +
20077 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20078 +                  struct inode *h_inode, unsigned int flags);
20079 +
20080 +void au_update_iigen(struct inode *inode, int half);
20081 +void au_update_ibrange(struct inode *inode, int do_put_zero);
20082 +
20083 +void au_icntnr_init_once(void *_c);
20084 +void au_hinode_init(struct au_hinode *hinode);
20085 +int au_iinfo_init(struct inode *inode);
20086 +void au_iinfo_fin(struct inode *inode);
20087 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
20088 +
20089 +#ifdef CONFIG_PROC_FS
20090 +/* plink.c */
20091 +int au_plink_maint(struct super_block *sb, int flags);
20092 +struct au_sbinfo;
20093 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20094 +int au_plink_maint_enter(struct super_block *sb);
20095 +#ifdef CONFIG_AUFS_DEBUG
20096 +void au_plink_list(struct super_block *sb);
20097 +#else
20098 +AuStubVoid(au_plink_list, struct super_block *sb)
20099 +#endif
20100 +int au_plink_test(struct inode *inode);
20101 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20102 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20103 +                    struct dentry *h_dentry);
20104 +void au_plink_put(struct super_block *sb, int verbose);
20105 +void au_plink_clean(struct super_block *sb, int verbose);
20106 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
20107 +#else
20108 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20109 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20110 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20111 +AuStubVoid(au_plink_list, struct super_block *sb);
20112 +AuStubInt0(au_plink_test, struct inode *inode);
20113 +AuStub(struct dentry *, au_plink_lkup, return NULL,
20114 +       struct inode *inode, aufs_bindex_t bindex);
20115 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20116 +          struct dentry *h_dentry);
20117 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20118 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20119 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20120 +#endif /* CONFIG_PROC_FS */
20121 +
20122 +#ifdef CONFIG_AUFS_XATTR
20123 +/* xattr.c */
20124 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
20125 +                 unsigned int verbose);
20126 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
20127 +void au_xattr_init(struct super_block *sb);
20128 +#else
20129 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
20130 +          int ignore_flags, unsigned int verbose);
20131 +AuStubVoid(au_xattr_init, struct super_block *sb);
20132 +#endif
20133 +
20134 +#ifdef CONFIG_FS_POSIX_ACL
20135 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu);
20136 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
20137 +                              struct dentry *dentry, int type);
20138 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
20139 +                struct posix_acl *acl, int type);
20140 +#endif
20141 +
20142 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20143 +enum {
20144 +       AU_XATTR_SET,
20145 +       AU_ACL_SET
20146 +};
20147 +
20148 +struct au_sxattr {
20149 +       int type;
20150 +       union {
20151 +               struct {
20152 +                       const char      *name;
20153 +                       const void      *value;
20154 +                       size_t          size;
20155 +                       int             flags;
20156 +               } set;
20157 +               struct {
20158 +                       struct posix_acl *acl;
20159 +                       int             type;
20160 +               } acl_set;
20161 +       } u;
20162 +};
20163 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20164 +                 struct au_sxattr *arg);
20165 +#endif
20166 +
20167 +/* ---------------------------------------------------------------------- */
20168 +
20169 +/* lock subclass for iinfo */
20170 +enum {
20171 +       AuLsc_II_CHILD,         /* child first */
20172 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
20173 +       AuLsc_II_CHILD3,        /* copyup dirs */
20174 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
20175 +       AuLsc_II_PARENT2,
20176 +       AuLsc_II_PARENT3,       /* copyup dirs */
20177 +       AuLsc_II_NEW_CHILD
20178 +};
20179 +
20180 +/*
20181 + * ii_read_lock_child, ii_write_lock_child,
20182 + * ii_read_lock_child2, ii_write_lock_child2,
20183 + * ii_read_lock_child3, ii_write_lock_child3,
20184 + * ii_read_lock_parent, ii_write_lock_parent,
20185 + * ii_read_lock_parent2, ii_write_lock_parent2,
20186 + * ii_read_lock_parent3, ii_write_lock_parent3,
20187 + * ii_read_lock_new_child, ii_write_lock_new_child,
20188 + */
20189 +#define AuReadLockFunc(name, lsc) \
20190 +static inline void ii_read_lock_##name(struct inode *i) \
20191 +{ \
20192 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20193 +}
20194 +
20195 +#define AuWriteLockFunc(name, lsc) \
20196 +static inline void ii_write_lock_##name(struct inode *i) \
20197 +{ \
20198 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20199 +}
20200 +
20201 +#define AuRWLockFuncs(name, lsc) \
20202 +       AuReadLockFunc(name, lsc) \
20203 +       AuWriteLockFunc(name, lsc)
20204 +
20205 +AuRWLockFuncs(child, CHILD);
20206 +AuRWLockFuncs(child2, CHILD2);
20207 +AuRWLockFuncs(child3, CHILD3);
20208 +AuRWLockFuncs(parent, PARENT);
20209 +AuRWLockFuncs(parent2, PARENT2);
20210 +AuRWLockFuncs(parent3, PARENT3);
20211 +AuRWLockFuncs(new_child, NEW_CHILD);
20212 +
20213 +#undef AuReadLockFunc
20214 +#undef AuWriteLockFunc
20215 +#undef AuRWLockFuncs
20216 +
20217 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20218 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20219 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
20220 +
20221 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20222 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20223 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
20224 +
20225 +/* ---------------------------------------------------------------------- */
20226 +
20227 +static inline void au_icntnr_init(struct au_icntnr *c)
20228 +{
20229 +#ifdef CONFIG_AUFS_DEBUG
20230 +       c->vfs_inode.i_mode = 0;
20231 +#endif
20232 +}
20233 +
20234 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
20235 +{
20236 +       unsigned int gen;
20237 +       struct au_iinfo *iinfo;
20238 +       struct au_iigen *iigen;
20239 +
20240 +       iinfo = au_ii(inode);
20241 +       iigen = &iinfo->ii_generation;
20242 +       spin_lock(&iigen->ig_spin);
20243 +       if (igflags)
20244 +               *igflags = iigen->ig_flags;
20245 +       gen = iigen->ig_generation;
20246 +       spin_unlock(&iigen->ig_spin);
20247 +
20248 +       return gen;
20249 +}
20250 +
20251 +/* tiny test for inode number */
20252 +/* tmpfs generation is too rough */
20253 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20254 +{
20255 +       struct au_iinfo *iinfo;
20256 +
20257 +       iinfo = au_ii(inode);
20258 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
20259 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
20260 +                && iinfo->ii_higen == h_inode->i_generation);
20261 +}
20262 +
20263 +static inline void au_iigen_dec(struct inode *inode)
20264 +{
20265 +       struct au_iinfo *iinfo;
20266 +       struct au_iigen *iigen;
20267 +
20268 +       iinfo = au_ii(inode);
20269 +       iigen = &iinfo->ii_generation;
20270 +       spin_lock(&iigen->ig_spin);
20271 +       iigen->ig_generation--;
20272 +       spin_unlock(&iigen->ig_spin);
20273 +}
20274 +
20275 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20276 +{
20277 +       int err;
20278 +
20279 +       err = 0;
20280 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
20281 +               err = -EIO;
20282 +
20283 +       return err;
20284 +}
20285 +
20286 +/* ---------------------------------------------------------------------- */
20287 +
20288 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20289 +                                         aufs_bindex_t bindex)
20290 +{
20291 +       return iinfo->ii_hinode + bindex;
20292 +}
20293 +
20294 +static inline int au_is_bad_inode(struct inode *inode)
20295 +{
20296 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20297 +}
20298 +
20299 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20300 +                                       aufs_bindex_t bindex)
20301 +{
20302 +       IiMustAnyLock(inode);
20303 +       return au_hinode(au_ii(inode), bindex)->hi_id;
20304 +}
20305 +
20306 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
20307 +{
20308 +       IiMustAnyLock(inode);
20309 +       return au_ii(inode)->ii_btop;
20310 +}
20311 +
20312 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
20313 +{
20314 +       IiMustAnyLock(inode);
20315 +       return au_ii(inode)->ii_bbot;
20316 +}
20317 +
20318 +static inline struct au_vdir *au_ivdir(struct inode *inode)
20319 +{
20320 +       IiMustAnyLock(inode);
20321 +       return au_ii(inode)->ii_vdir;
20322 +}
20323 +
20324 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20325 +{
20326 +       IiMustAnyLock(inode);
20327 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
20328 +}
20329 +
20330 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
20331 +{
20332 +       IiMustWriteLock(inode);
20333 +       au_ii(inode)->ii_btop = bindex;
20334 +}
20335 +
20336 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
20337 +{
20338 +       IiMustWriteLock(inode);
20339 +       au_ii(inode)->ii_bbot = bindex;
20340 +}
20341 +
20342 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20343 +{
20344 +       IiMustWriteLock(inode);
20345 +       au_ii(inode)->ii_vdir = vdir;
20346 +}
20347 +
20348 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
20349 +{
20350 +       IiMustAnyLock(inode);
20351 +       return au_hinode(au_ii(inode), bindex);
20352 +}
20353 +
20354 +/* ---------------------------------------------------------------------- */
20355 +
20356 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20357 +{
20358 +       if (pin)
20359 +               return pin->parent;
20360 +       return NULL;
20361 +}
20362 +
20363 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
20364 +{
20365 +       if (pin && pin->hdir)
20366 +               return pin->hdir->hi_inode;
20367 +       return NULL;
20368 +}
20369 +
20370 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20371 +{
20372 +       if (pin)
20373 +               return pin->hdir;
20374 +       return NULL;
20375 +}
20376 +
20377 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
20378 +{
20379 +       if (pin)
20380 +               pin->dentry = dentry;
20381 +}
20382 +
20383 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20384 +                                          unsigned char lflag)
20385 +{
20386 +       if (pin) {
20387 +               if (lflag)
20388 +                       au_fset_pin(pin->flags, DI_LOCKED);
20389 +               else
20390 +                       au_fclr_pin(pin->flags, DI_LOCKED);
20391 +       }
20392 +}
20393 +
20394 +#if 0 /* reserved */
20395 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20396 +{
20397 +       if (pin) {
20398 +               dput(pin->parent);
20399 +               pin->parent = dget(parent);
20400 +       }
20401 +}
20402 +#endif
20403 +
20404 +/* ---------------------------------------------------------------------- */
20405 +
20406 +struct au_branch;
20407 +#ifdef CONFIG_AUFS_HNOTIFY
20408 +struct au_hnotify_op {
20409 +       void (*ctl)(struct au_hinode *hinode, int do_set);
20410 +       int (*alloc)(struct au_hinode *hinode);
20411 +
20412 +       /*
20413 +        * if it returns true, the caller should free hinode->hi_notify,
20414 +        * otherwise ->free() frees it.
20415 +        */
20416 +       int (*free)(struct au_hinode *hinode,
20417 +                   struct au_hnotify *hn) __must_check;
20418 +
20419 +       void (*fin)(void);
20420 +       int (*init)(void);
20421 +
20422 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20423 +       void (*fin_br)(struct au_branch *br);
20424 +       int (*init_br)(struct au_branch *br, int perm);
20425 +};
20426 +
20427 +/* hnotify.c */
20428 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
20429 +void au_hn_free(struct au_hinode *hinode);
20430 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
20431 +void au_hn_reset(struct inode *inode, unsigned int flags);
20432 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
20433 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
20434 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20435 +int au_hnotify_init_br(struct au_branch *br, int perm);
20436 +void au_hnotify_fin_br(struct au_branch *br);
20437 +int __init au_hnotify_init(void);
20438 +void au_hnotify_fin(void);
20439 +
20440 +/* hfsnotify.c */
20441 +extern const struct au_hnotify_op au_hnotify_op;
20442 +
20443 +static inline
20444 +void au_hn_init(struct au_hinode *hinode)
20445 +{
20446 +       hinode->hi_notify = NULL;
20447 +}
20448 +
20449 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20450 +{
20451 +       return hinode->hi_notify;
20452 +}
20453 +
20454 +#else
20455 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20456 +       struct au_hinode *hinode __maybe_unused,
20457 +       struct inode *inode __maybe_unused)
20458 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
20459 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20460 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20461 +          int do_set __maybe_unused)
20462 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20463 +          unsigned int flags __maybe_unused)
20464 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20465 +          struct au_branch *br __maybe_unused,
20466 +          int perm __maybe_unused)
20467 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20468 +          int perm __maybe_unused)
20469 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
20470 +AuStubInt0(__init au_hnotify_init, void)
20471 +AuStubVoid(au_hnotify_fin, void)
20472 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20473 +#endif /* CONFIG_AUFS_HNOTIFY */
20474 +
20475 +static inline void au_hn_suspend(struct au_hinode *hdir)
20476 +{
20477 +       au_hn_ctl(hdir, /*do_set*/0);
20478 +}
20479 +
20480 +static inline void au_hn_resume(struct au_hinode *hdir)
20481 +{
20482 +       au_hn_ctl(hdir, /*do_set*/1);
20483 +}
20484 +
20485 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
20486 +{
20487 +       inode_lock(hdir->hi_inode);
20488 +       au_hn_suspend(hdir);
20489 +}
20490 +
20491 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
20492 +                                         unsigned int sc __maybe_unused)
20493 +{
20494 +       inode_lock_nested(hdir->hi_inode, sc);
20495 +       au_hn_suspend(hdir);
20496 +}
20497 +
20498 +#if 0 /* unused */
20499 +#include "vfsub.h"
20500 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20501 +                                                 unsigned int sc)
20502 +{
20503 +       inode_lock_shared_nested(hdir->hi_inode, sc);
20504 +       au_hn_suspend(hdir);
20505 +}
20506 +#endif
20507 +
20508 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
20509 +{
20510 +       au_hn_resume(hdir);
20511 +       inode_unlock(hdir->hi_inode);
20512 +}
20513 +
20514 +#endif /* __KERNEL__ */
20515 +#endif /* __AUFS_INODE_H__ */
20516 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
20517 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
20518 +++ linux/fs/aufs/ioctl.c       2022-11-05 23:02:18.965889284 +0100
20519 @@ -0,0 +1,220 @@
20520 +// SPDX-License-Identifier: GPL-2.0
20521 +/*
20522 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20523 + *
20524 + * This program is free software; you can redistribute it and/or modify
20525 + * it under the terms of the GNU General Public License as published by
20526 + * the Free Software Foundation; either version 2 of the License, or
20527 + * (at your option) any later version.
20528 + *
20529 + * This program is distributed in the hope that it will be useful,
20530 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20531 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20532 + * GNU General Public License for more details.
20533 + *
20534 + * You should have received a copy of the GNU General Public License
20535 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20536 + */
20537 +
20538 +/*
20539 + * ioctl
20540 + * plink-management and readdir in userspace.
20541 + * assist the pathconf(3) wrapper library.
20542 + * move-down
20543 + * File-based Hierarchical Storage Management.
20544 + */
20545 +
20546 +#include <linux/compat.h>
20547 +#include <linux/file.h>
20548 +#include "aufs.h"
20549 +
20550 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
20551 +{
20552 +       int err, fd;
20553 +       aufs_bindex_t wbi, bindex, bbot;
20554 +       struct file *h_file;
20555 +       struct super_block *sb;
20556 +       struct dentry *root;
20557 +       struct au_branch *br;
20558 +       struct aufs_wbr_fd wbrfd = {
20559 +               .oflags = au_dir_roflags,
20560 +               .brid   = -1
20561 +       };
20562 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20563 +               | O_NOATIME | O_CLOEXEC;
20564 +
20565 +       AuDebugOn(wbrfd.oflags & ~valid);
20566 +
20567 +       if (arg) {
20568 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20569 +               if (unlikely(err)) {
20570 +                       err = -EFAULT;
20571 +                       goto out;
20572 +               }
20573 +
20574 +               err = -EINVAL;
20575 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20576 +               wbrfd.oflags |= au_dir_roflags;
20577 +               AuDbg("0%o\n", wbrfd.oflags);
20578 +               if (unlikely(wbrfd.oflags & ~valid))
20579 +                       goto out;
20580 +       }
20581 +
20582 +       fd = get_unused_fd_flags(0);
20583 +       err = fd;
20584 +       if (unlikely(fd < 0))
20585 +               goto out;
20586 +
20587 +       h_file = ERR_PTR(-EINVAL);
20588 +       wbi = 0;
20589 +       br = NULL;
20590 +       sb = path->dentry->d_sb;
20591 +       root = sb->s_root;
20592 +       aufs_read_lock(root, AuLock_IR);
20593 +       bbot = au_sbbot(sb);
20594 +       if (wbrfd.brid >= 0) {
20595 +               wbi = au_br_index(sb, wbrfd.brid);
20596 +               if (unlikely(wbi < 0 || wbi > bbot))
20597 +                       goto out_unlock;
20598 +       }
20599 +
20600 +       h_file = ERR_PTR(-ENOENT);
20601 +       br = au_sbr(sb, wbi);
20602 +       if (!au_br_writable(br->br_perm)) {
20603 +               if (arg)
20604 +                       goto out_unlock;
20605 +
20606 +               bindex = wbi + 1;
20607 +               wbi = -1;
20608 +               for (; bindex <= bbot; bindex++) {
20609 +                       br = au_sbr(sb, bindex);
20610 +                       if (au_br_writable(br->br_perm)) {
20611 +                               wbi = bindex;
20612 +                               br = au_sbr(sb, wbi);
20613 +                               break;
20614 +                       }
20615 +               }
20616 +       }
20617 +       AuDbg("wbi %d\n", wbi);
20618 +       if (wbi >= 0)
20619 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20620 +                                  /*force_wr*/0);
20621 +
20622 +out_unlock:
20623 +       aufs_read_unlock(root, AuLock_IR);
20624 +       err = PTR_ERR(h_file);
20625 +       if (IS_ERR(h_file))
20626 +               goto out_fd;
20627 +
20628 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
20629 +       fd_install(fd, h_file);
20630 +       err = fd;
20631 +       goto out; /* success */
20632 +
20633 +out_fd:
20634 +       put_unused_fd(fd);
20635 +out:
20636 +       AuTraceErr(err);
20637 +       return err;
20638 +}
20639 +
20640 +/* ---------------------------------------------------------------------- */
20641 +
20642 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20643 +{
20644 +       long err;
20645 +       struct dentry *dentry;
20646 +
20647 +       switch (cmd) {
20648 +       case AUFS_CTL_RDU:
20649 +       case AUFS_CTL_RDU_INO:
20650 +               err = au_rdu_ioctl(file, cmd, arg);
20651 +               break;
20652 +
20653 +       case AUFS_CTL_WBR_FD:
20654 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20655 +               break;
20656 +
20657 +       case AUFS_CTL_IBUSY:
20658 +               err = au_ibusy_ioctl(file, arg);
20659 +               break;
20660 +
20661 +       case AUFS_CTL_BRINFO:
20662 +               err = au_brinfo_ioctl(file, arg);
20663 +               break;
20664 +
20665 +       case AUFS_CTL_FHSM_FD:
20666 +               dentry = file->f_path.dentry;
20667 +               if (IS_ROOT(dentry))
20668 +                       err = au_fhsm_fd(dentry->d_sb, arg);
20669 +               else
20670 +                       err = -ENOTTY;
20671 +               break;
20672 +
20673 +       default:
20674 +               /* do not call the lower */
20675 +               AuDbg("0x%x\n", cmd);
20676 +               err = -ENOTTY;
20677 +       }
20678 +
20679 +       AuTraceErr(err);
20680 +       return err;
20681 +}
20682 +
20683 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20684 +{
20685 +       long err;
20686 +
20687 +       switch (cmd) {
20688 +       case AUFS_CTL_MVDOWN:
20689 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
20690 +               break;
20691 +
20692 +       case AUFS_CTL_WBR_FD:
20693 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20694 +               break;
20695 +
20696 +       default:
20697 +               /* do not call the lower */
20698 +               AuDbg("0x%x\n", cmd);
20699 +               err = -ENOTTY;
20700 +       }
20701 +
20702 +       AuTraceErr(err);
20703 +       return err;
20704 +}
20705 +
20706 +#ifdef CONFIG_COMPAT
20707 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20708 +                          unsigned long arg)
20709 +{
20710 +       long err;
20711 +
20712 +       switch (cmd) {
20713 +       case AUFS_CTL_RDU:
20714 +       case AUFS_CTL_RDU_INO:
20715 +               err = au_rdu_compat_ioctl(file, cmd, arg);
20716 +               break;
20717 +
20718 +       case AUFS_CTL_IBUSY:
20719 +               err = au_ibusy_compat_ioctl(file, arg);
20720 +               break;
20721 +
20722 +       case AUFS_CTL_BRINFO:
20723 +               err = au_brinfo_compat_ioctl(file, arg);
20724 +               break;
20725 +
20726 +       default:
20727 +               err = aufs_ioctl_dir(file, cmd, arg);
20728 +       }
20729 +
20730 +       AuTraceErr(err);
20731 +       return err;
20732 +}
20733 +
20734 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20735 +                             unsigned long arg)
20736 +{
20737 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20738 +}
20739 +#endif
20740 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
20741 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
20742 +++ linux/fs/aufs/i_op_add.c    2023-10-31 09:31:04.199880750 +0100
20743 @@ -0,0 +1,972 @@
20744 +// SPDX-License-Identifier: GPL-2.0
20745 +/*
20746 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20747 + *
20748 + * This program is free software; you can redistribute it and/or modify
20749 + * it under the terms of the GNU General Public License as published by
20750 + * the Free Software Foundation; either version 2 of the License, or
20751 + * (at your option) any later version.
20752 + *
20753 + * This program is distributed in the hope that it will be useful,
20754 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20755 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20756 + * GNU General Public License for more details.
20757 + *
20758 + * You should have received a copy of the GNU General Public License
20759 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20760 + */
20761 +
20762 +/*
20763 + * inode operations (add entry)
20764 + */
20765 +
20766 +#include <linux/iversion.h>
20767 +#include "aufs.h"
20768 +
20769 +/*
20770 + * final procedure of adding a new entry, except link(2).
20771 + * remove whiteout, instantiate, copyup the parent dir's times and size
20772 + * and update version.
20773 + * if it failed, re-create the removed whiteout.
20774 + */
20775 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20776 +                 struct dentry *wh_dentry, struct dentry *dentry)
20777 +{
20778 +       int err, rerr;
20779 +       aufs_bindex_t bwh;
20780 +       struct path h_path;
20781 +       struct super_block *sb;
20782 +       struct inode *inode, *h_dir;
20783 +       struct dentry *wh;
20784 +
20785 +       bwh = -1;
20786 +       sb = dir->i_sb;
20787 +       if (wh_dentry) {
20788 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20789 +               IMustLock(h_dir);
20790 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20791 +               bwh = au_dbwh(dentry);
20792 +               h_path.dentry = wh_dentry;
20793 +               h_path.mnt = au_sbr_mnt(sb, bindex);
20794 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20795 +                                         dentry);
20796 +               if (unlikely(err))
20797 +                       goto out;
20798 +       }
20799 +
20800 +       inode = au_new_inode(dentry, /*must_new*/1);
20801 +       if (!IS_ERR(inode)) {
20802 +               d_instantiate(dentry, inode);
20803 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
20804 +               IMustLock(dir);
20805 +               au_dir_ts(dir, bindex);
20806 +               inode_inc_iversion(dir);
20807 +               au_fhsm_wrote(sb, bindex, /*force*/0);
20808 +               return 0; /* success */
20809 +       }
20810 +
20811 +       err = PTR_ERR(inode);
20812 +       if (!wh_dentry)
20813 +               goto out;
20814 +
20815 +       /* revert */
20816 +       /* dir inode is locked */
20817 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20818 +       rerr = PTR_ERR(wh);
20819 +       if (IS_ERR(wh)) {
20820 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20821 +                       dentry, err, rerr);
20822 +               err = -EIO;
20823 +       } else
20824 +               dput(wh);
20825 +
20826 +out:
20827 +       return err;
20828 +}
20829 +
20830 +static int au_d_may_add(struct dentry *dentry)
20831 +{
20832 +       int err;
20833 +
20834 +       err = 0;
20835 +       if (unlikely(d_unhashed(dentry)))
20836 +               err = -ENOENT;
20837 +       if (unlikely(d_really_is_positive(dentry)))
20838 +               err = -EEXIST;
20839 +       return err;
20840 +}
20841 +
20842 +/*
20843 + * simple tests for the adding inode operations.
20844 + * following the checks in vfs, plus the parent-child relationship.
20845 + */
20846 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20847 +              struct dentry *h_parent, int isdir)
20848 +{
20849 +       int err;
20850 +       umode_t h_mode;
20851 +       struct dentry *h_dentry;
20852 +       struct inode *h_inode;
20853 +
20854 +       err = -ENAMETOOLONG;
20855 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20856 +               goto out;
20857 +
20858 +       h_dentry = au_h_dptr(dentry, bindex);
20859 +       if (d_really_is_negative(dentry)) {
20860 +               err = -EEXIST;
20861 +               if (unlikely(d_is_positive(h_dentry)))
20862 +                       goto out;
20863 +       } else {
20864 +               /* rename(2) case */
20865 +               err = -EIO;
20866 +               if (unlikely(d_is_negative(h_dentry)))
20867 +                       goto out;
20868 +               h_inode = d_inode(h_dentry);
20869 +               if (unlikely(!h_inode->i_nlink))
20870 +                       goto out;
20871 +
20872 +               h_mode = h_inode->i_mode;
20873 +               if (!isdir) {
20874 +                       err = -EISDIR;
20875 +                       if (unlikely(S_ISDIR(h_mode)))
20876 +                               goto out;
20877 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20878 +                       err = -ENOTDIR;
20879 +                       goto out;
20880 +               }
20881 +       }
20882 +
20883 +       err = 0;
20884 +       /* expected parent dir is locked */
20885 +       if (unlikely(h_parent != h_dentry->d_parent))
20886 +               err = -EIO;
20887 +
20888 +out:
20889 +       AuTraceErr(err);
20890 +       return err;
20891 +}
20892 +
20893 +/*
20894 + * initial procedure of adding a new entry.
20895 + * prepare writable branch and the parent dir, lock it,
20896 + * and lookup whiteout for the new entry.
20897 + */
20898 +static struct dentry*
20899 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20900 +                 struct dentry *src_dentry, struct au_pin *pin,
20901 +                 struct au_wr_dir_args *wr_dir_args)
20902 +{
20903 +       struct dentry *wh_dentry, *h_parent;
20904 +       struct super_block *sb;
20905 +       struct au_branch *br;
20906 +       int err;
20907 +       unsigned int udba;
20908 +       aufs_bindex_t bcpup;
20909 +
20910 +       AuDbg("%pd\n", dentry);
20911 +
20912 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20913 +       bcpup = err;
20914 +       wh_dentry = ERR_PTR(err);
20915 +       if (unlikely(err < 0))
20916 +               goto out;
20917 +
20918 +       sb = dentry->d_sb;
20919 +       udba = au_opt_udba(sb);
20920 +       err = au_pin(pin, dentry, bcpup, udba,
20921 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20922 +       wh_dentry = ERR_PTR(err);
20923 +       if (unlikely(err))
20924 +               goto out;
20925 +
20926 +       h_parent = au_pinned_h_parent(pin);
20927 +       if (udba != AuOpt_UDBA_NONE
20928 +           && au_dbtop(dentry) == bcpup)
20929 +               err = au_may_add(dentry, bcpup, h_parent,
20930 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20931 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20932 +               err = -ENAMETOOLONG;
20933 +       wh_dentry = ERR_PTR(err);
20934 +       if (unlikely(err))
20935 +               goto out_unpin;
20936 +
20937 +       br = au_sbr(sb, bcpup);
20938 +       if (dt) {
20939 +               struct path tmp = {
20940 +                       .dentry = h_parent,
20941 +                       .mnt    = au_br_mnt(br)
20942 +               };
20943 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20944 +       }
20945 +
20946 +       wh_dentry = NULL;
20947 +       if (bcpup != au_dbwh(dentry))
20948 +               goto out; /* success */
20949 +
20950 +       /*
20951 +        * ENAMETOOLONG here means that if we allowed create such name, then it
20952 +        * would not be able to removed in the future. So we don't allow such
20953 +        * name here and we don't handle ENAMETOOLONG differently here.
20954 +        */
20955 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20956 +
20957 +out_unpin:
20958 +       if (IS_ERR(wh_dentry))
20959 +               au_unpin(pin);
20960 +out:
20961 +       return wh_dentry;
20962 +}
20963 +
20964 +/* ---------------------------------------------------------------------- */
20965 +
20966 +enum { Mknod, Symlink, Creat };
20967 +struct simple_arg {
20968 +       int type;
20969 +       union {
20970 +               struct {
20971 +                       umode_t                 mode;
20972 +                       bool                    want_excl;
20973 +                       bool                    try_aopen;
20974 +                       struct vfsub_aopen_args *aopen;
20975 +               } c;
20976 +               struct {
20977 +                       const char *symname;
20978 +               } s;
20979 +               struct {
20980 +                       umode_t mode;
20981 +                       dev_t dev;
20982 +               } m;
20983 +       } u;
20984 +};
20985 +
20986 +static int add_simple(struct inode *dir, struct dentry *dentry,
20987 +                     struct simple_arg *arg)
20988 +{
20989 +       int err, rerr;
20990 +       aufs_bindex_t btop;
20991 +       unsigned char created;
20992 +       const unsigned char try_aopen
20993 +               = (arg->type == Creat && arg->u.c.try_aopen);
20994 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
20995 +       struct dentry *wh_dentry, *parent;
20996 +       struct inode *h_dir;
20997 +       struct super_block *sb;
20998 +       struct au_branch *br;
20999 +       /* to reduce stack size */
21000 +       struct {
21001 +               struct au_dtime dt;
21002 +               struct au_pin pin;
21003 +               struct path h_path;
21004 +               struct au_wr_dir_args wr_dir_args;
21005 +       } *a;
21006 +
21007 +       AuDbg("%pd\n", dentry);
21008 +       IMustLock(dir);
21009 +
21010 +       err = -ENOMEM;
21011 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21012 +       if (unlikely(!a))
21013 +               goto out;
21014 +       a->wr_dir_args.force_btgt = -1;
21015 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21016 +
21017 +       parent = dentry->d_parent; /* dir inode is locked */
21018 +       if (!try_aopen) {
21019 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21020 +               if (unlikely(err))
21021 +                       goto out_free;
21022 +       }
21023 +       err = au_d_may_add(dentry);
21024 +       if (unlikely(err))
21025 +               goto out_unlock;
21026 +       if (!try_aopen)
21027 +               di_write_lock_parent(parent);
21028 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21029 +                                     &a->pin, &a->wr_dir_args);
21030 +       err = PTR_ERR(wh_dentry);
21031 +       if (IS_ERR(wh_dentry))
21032 +               goto out_parent;
21033 +
21034 +       btop = au_dbtop(dentry);
21035 +       sb = dentry->d_sb;
21036 +       br = au_sbr(sb, btop);
21037 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21038 +       a->h_path.mnt = au_br_mnt(br);
21039 +       h_dir = au_pinned_h_dir(&a->pin);
21040 +       switch (arg->type) {
21041 +       case Creat:
21042 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
21043 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21044 +                                          arg->u.c.want_excl);
21045 +                       created = !err;
21046 +                       if (!err && try_aopen)
21047 +                               aopen->file->f_mode |= FMODE_CREATED;
21048 +               } else {
21049 +                       aopen->br = br;
21050 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21051 +                       AuDbg("err %d\n", err);
21052 +                       AuDbgFile(aopen->file);
21053 +                       created = err >= 0
21054 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
21055 +               }
21056 +               break;
21057 +       case Symlink:
21058 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
21059 +               created = !err;
21060 +               break;
21061 +       case Mknod:
21062 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21063 +                                 arg->u.m.dev);
21064 +               created = !err;
21065 +               break;
21066 +       default:
21067 +               BUG();
21068 +       }
21069 +       if (unlikely(err < 0))
21070 +               goto out_unpin;
21071 +
21072 +       err = epilog(dir, btop, wh_dentry, dentry);
21073 +       if (!err)
21074 +               goto out_unpin; /* success */
21075 +
21076 +       /* revert */
21077 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
21078 +               /* no delegation since it is just created */
21079 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21080 +                                   /*force*/0);
21081 +               if (rerr) {
21082 +                       AuIOErr("%pd revert failure(%d, %d)\n",
21083 +                               dentry, err, rerr);
21084 +                       err = -EIO;
21085 +               }
21086 +               au_dtime_revert(&a->dt);
21087 +       }
21088 +       if (try_aopen && h_dir->i_op->atomic_open
21089 +           && (aopen->file->f_mode & FMODE_OPENED))
21090 +               /* aopen->file is still opened */
21091 +               au_lcnt_dec(&aopen->br->br_nfiles);
21092 +
21093 +out_unpin:
21094 +       au_unpin(&a->pin);
21095 +       dput(wh_dentry);
21096 +out_parent:
21097 +       if (!try_aopen)
21098 +               di_write_unlock(parent);
21099 +out_unlock:
21100 +       if (unlikely(err)) {
21101 +               au_update_dbtop(dentry);
21102 +               d_drop(dentry);
21103 +       }
21104 +       if (!try_aopen)
21105 +               aufs_read_unlock(dentry, AuLock_DW);
21106 +out_free:
21107 +       au_kfree_rcu(a);
21108 +out:
21109 +       return err;
21110 +}
21111 +
21112 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
21113 +              struct dentry *dentry, umode_t mode, dev_t dev)
21114 +{
21115 +       struct simple_arg arg = {
21116 +               .type = Mknod,
21117 +               .u.m = {
21118 +                       .mode   = mode,
21119 +                       .dev    = dev
21120 +               }
21121 +       };
21122 +       return add_simple(dir, dentry, &arg);
21123 +}
21124 +
21125 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
21126 +                struct dentry *dentry, const char *symname)
21127 +{
21128 +       struct simple_arg arg = {
21129 +               .type = Symlink,
21130 +               .u.s.symname = symname
21131 +       };
21132 +       return add_simple(dir, dentry, &arg);
21133 +}
21134 +
21135 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
21136 +               struct dentry *dentry, umode_t mode, bool want_excl)
21137 +{
21138 +       struct simple_arg arg = {
21139 +               .type = Creat,
21140 +               .u.c = {
21141 +                       .mode           = mode,
21142 +                       .want_excl      = want_excl
21143 +               }
21144 +       };
21145 +       return add_simple(dir, dentry, &arg);
21146 +}
21147 +
21148 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21149 +                      struct vfsub_aopen_args *aopen_args)
21150 +{
21151 +       struct simple_arg arg = {
21152 +               .type = Creat,
21153 +               .u.c = {
21154 +                       .mode           = aopen_args->create_mode,
21155 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
21156 +                       .try_aopen      = true,
21157 +                       .aopen          = aopen_args
21158 +               }
21159 +       };
21160 +       return add_simple(dir, dentry, &arg);
21161 +}
21162 +
21163 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
21164 +                struct file *file, umode_t mode)
21165 +{
21166 +       int err;
21167 +       aufs_bindex_t bindex;
21168 +       struct path h_ppath;
21169 +       struct super_block *sb;
21170 +       struct au_branch *br;
21171 +       struct dentry *dentry, *parent, *h_parent, *h_dentry;
21172 +       struct inode *h_dir, *inode;
21173 +       struct vfsmount *h_mnt;
21174 +       struct mnt_idmap *h_idmap;
21175 +       struct file *h_file;
21176 +       struct au_wr_dir_args wr_dir_args = {
21177 +               .force_btgt     = -1,
21178 +               .flags          = AuWrDir_TMPFILE
21179 +       };
21180 +
21181 +       /* copy-up may happen */
21182 +       inode_lock(dir);
21183 +
21184 +       h_file = NULL;
21185 +       sb = dir->i_sb;
21186 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21187 +       if (unlikely(err))
21188 +               goto out;
21189 +
21190 +       dentry = file->f_path.dentry;
21191 +       err = au_di_init(dentry);
21192 +       if (unlikely(err))
21193 +               goto out_si;
21194 +
21195 +       err = -EBUSY;
21196 +       parent = d_find_any_alias(dir);
21197 +       AuDebugOn(!parent);
21198 +       di_write_lock_parent(parent);
21199 +       if (unlikely(d_inode(parent) != dir))
21200 +               goto out_parent;
21201 +
21202 +       err = au_digen_test(parent, au_sigen(sb));
21203 +       if (unlikely(err))
21204 +               goto out_parent;
21205 +
21206 +       bindex = au_dbtop(parent);
21207 +       au_set_dbtop(dentry, bindex);
21208 +       au_set_dbbot(dentry, bindex);
21209 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21210 +       bindex = err;
21211 +       if (unlikely(err < 0))
21212 +               goto out_parent;
21213 +
21214 +       err = -EOPNOTSUPP;
21215 +       h_dir = au_h_iptr(dir, bindex);
21216 +       if (unlikely(!h_dir->i_op->tmpfile))
21217 +               goto out_parent;
21218 +
21219 +       br = au_sbr(sb, bindex);
21220 +       h_mnt = au_br_mnt(br);
21221 +       err = vfsub_mnt_want_write(h_mnt);
21222 +       if (unlikely(err))
21223 +               goto out_parent;
21224 +
21225 +       h_idmap = mnt_idmap(h_mnt);
21226 +       h_parent = au_h_dptr(parent, bindex);
21227 +       h_ppath.mnt = h_mnt;
21228 +       h_ppath.dentry = h_parent;
21229 +       h_file = kernel_tmpfile_open(h_idmap, &h_ppath, mode, /*open_flag*/0,
21230 +                                    current_cred());
21231 +       if (IS_ERR(h_file)) {
21232 +               err = PTR_ERR(h_file);
21233 +               h_file = NULL;
21234 +               goto out_mnt;
21235 +       }
21236 +
21237 +       h_dentry = h_file->f_path.dentry;
21238 +       au_set_dbtop(dentry, bindex);
21239 +       au_set_dbbot(dentry, bindex);
21240 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
21241 +       inode = au_new_inode(dentry, /*must_new*/1);
21242 +       if (IS_ERR(inode)) {
21243 +               err = PTR_ERR(inode);
21244 +               au_set_h_dptr(dentry, bindex, NULL);
21245 +               au_set_dbtop(dentry, -1);
21246 +               au_set_dbbot(dentry, -1);
21247 +               goto out_h_file;
21248 +       }
21249 +
21250 +       if (!inode->i_nlink)
21251 +               set_nlink(inode, 1);
21252 +       d_tmpfile(file, inode);
21253 +       au_di(dentry)->di_tmpfile = 1;
21254 +       get_file(h_file);
21255 +       au_di(dentry)->di_htmpfile = h_file;
21256 +
21257 +       /* update without i_mutex */
21258 +       if (au_ibtop(dir) == au_dbtop(dentry))
21259 +               au_cpup_attr_timesizes(dir);
21260 +
21261 +out_h_file:
21262 +       fput(h_file);
21263 +out_mnt:
21264 +       vfsub_mnt_drop_write(h_mnt);
21265 +out_parent:
21266 +       di_write_unlock(parent);
21267 +       dput(parent);
21268 +       di_write_unlock(dentry);
21269 +       if (!err)
21270 +               goto out_si;
21271 +       if (h_file)
21272 +               fput(h_file);
21273 +       au_di(dentry)->di_htmpfile = NULL;
21274 +       au_di_fin(dentry);
21275 +       dentry->d_fsdata = NULL;
21276 +out_si:
21277 +       si_read_unlock(sb);
21278 +       if (!err && h_file) {
21279 +               /* finally... */
21280 +               err = finish_open_simple(file, err);
21281 +               if (!err)
21282 +                       au_lcnt_inc(&br->br_nfiles);
21283 +               else {
21284 +                       fput(h_file);
21285 +                       au_di(dentry)->di_htmpfile = NULL;
21286 +                       au_di_fin(dentry);
21287 +                       dentry->d_fsdata = NULL;
21288 +               }
21289 +       }
21290 +out:
21291 +       inode_unlock(dir);
21292 +       AuTraceErr(err);
21293 +       return err;
21294 +}
21295 +
21296 +/* ---------------------------------------------------------------------- */
21297 +
21298 +struct au_link_args {
21299 +       aufs_bindex_t bdst, bsrc;
21300 +       struct au_pin pin;
21301 +       struct path h_path;
21302 +       struct dentry *src_parent, *parent;
21303 +};
21304 +
21305 +static int au_cpup_before_link(struct dentry *src_dentry,
21306 +                              struct au_link_args *a)
21307 +{
21308 +       int err;
21309 +       struct dentry *h_src_dentry;
21310 +       struct au_cp_generic cpg = {
21311 +               .dentry = src_dentry,
21312 +               .bdst   = a->bdst,
21313 +               .bsrc   = a->bsrc,
21314 +               .len    = -1,
21315 +               .pin    = &a->pin,
21316 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21317 +       };
21318 +
21319 +       di_read_lock_parent(a->src_parent, AuLock_IR);
21320 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21321 +       if (unlikely(err))
21322 +               goto out;
21323 +
21324 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
21325 +       err = au_pin(&a->pin, src_dentry, a->bdst,
21326 +                    au_opt_udba(src_dentry->d_sb),
21327 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21328 +       if (unlikely(err))
21329 +               goto out;
21330 +
21331 +       err = au_sio_cpup_simple(&cpg);
21332 +       au_unpin(&a->pin);
21333 +
21334 +out:
21335 +       di_read_unlock(a->src_parent, AuLock_IR);
21336 +       return err;
21337 +}
21338 +
21339 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21340 +                          struct au_link_args *a)
21341 +{
21342 +       int err;
21343 +       unsigned char plink;
21344 +       aufs_bindex_t bbot;
21345 +       struct dentry *h_src_dentry;
21346 +       struct inode *h_inode, *inode, *delegated;
21347 +       struct super_block *sb;
21348 +       struct file *h_file;
21349 +
21350 +       plink = 0;
21351 +       h_inode = NULL;
21352 +       sb = src_dentry->d_sb;
21353 +       inode = d_inode(src_dentry);
21354 +       if (au_ibtop(inode) <= a->bdst)
21355 +               h_inode = au_h_iptr(inode, a->bdst);
21356 +       if (!h_inode || !h_inode->i_nlink) {
21357 +               /* copyup src_dentry as the name of dentry. */
21358 +               bbot = au_dbbot(dentry);
21359 +               if (bbot < a->bsrc)
21360 +                       au_set_dbbot(dentry, a->bsrc);
21361 +               au_set_h_dptr(dentry, a->bsrc,
21362 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
21363 +               dget(a->h_path.dentry);
21364 +               au_set_h_dptr(dentry, a->bdst, NULL);
21365 +               AuDbg("temporary d_inode...\n");
21366 +               spin_lock(&dentry->d_lock);
21367 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
21368 +               spin_unlock(&dentry->d_lock);
21369 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
21370 +               if (IS_ERR(h_file))
21371 +                       err = PTR_ERR(h_file);
21372 +               else {
21373 +                       struct au_cp_generic cpg = {
21374 +                               .dentry = dentry,
21375 +                               .bdst   = a->bdst,
21376 +                               .bsrc   = -1,
21377 +                               .len    = -1,
21378 +                               .pin    = &a->pin,
21379 +                               .flags  = AuCpup_KEEPLINO
21380 +                       };
21381 +                       err = au_sio_cpup_simple(&cpg);
21382 +                       au_h_open_post(dentry, a->bsrc, h_file);
21383 +                       if (!err) {
21384 +                               dput(a->h_path.dentry);
21385 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21386 +                       } else
21387 +                               au_set_h_dptr(dentry, a->bdst,
21388 +                                             a->h_path.dentry);
21389 +               }
21390 +               spin_lock(&dentry->d_lock);
21391 +               dentry->d_inode = NULL; /* restore */
21392 +               spin_unlock(&dentry->d_lock);
21393 +               AuDbg("temporary d_inode...done\n");
21394 +               au_set_h_dptr(dentry, a->bsrc, NULL);
21395 +               au_set_dbbot(dentry, bbot);
21396 +       } else {
21397 +               /* the inode of src_dentry already exists on a.bdst branch */
21398 +               h_src_dentry = d_find_alias(h_inode);
21399 +               if (!h_src_dentry && au_plink_test(inode)) {
21400 +                       plink = 1;
21401 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
21402 +                       err = PTR_ERR(h_src_dentry);
21403 +                       if (IS_ERR(h_src_dentry))
21404 +                               goto out;
21405 +
21406 +                       if (unlikely(d_is_negative(h_src_dentry))) {
21407 +                               dput(h_src_dentry);
21408 +                               h_src_dentry = NULL;
21409 +                       }
21410 +
21411 +               }
21412 +               if (h_src_dentry) {
21413 +                       delegated = NULL;
21414 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21415 +                                        &a->h_path, &delegated);
21416 +                       if (unlikely(err == -EWOULDBLOCK)) {
21417 +                               pr_warn("cannot retry for NFSv4 delegation"
21418 +                                       " for an internal link\n");
21419 +                               iput(delegated);
21420 +                       }
21421 +                       dput(h_src_dentry);
21422 +               } else {
21423 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
21424 +                               h_inode->i_ino, a->bdst);
21425 +                       err = -EIO;
21426 +               }
21427 +       }
21428 +
21429 +       if (!err && !plink)
21430 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
21431 +
21432 +out:
21433 +       AuTraceErr(err);
21434 +       return err;
21435 +}
21436 +
21437 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
21438 +             struct dentry *dentry)
21439 +{
21440 +       int err, rerr;
21441 +       struct au_dtime dt;
21442 +       struct au_link_args *a;
21443 +       struct dentry *wh_dentry, *h_src_dentry;
21444 +       struct inode *inode, *delegated;
21445 +       struct super_block *sb;
21446 +       struct au_wr_dir_args wr_dir_args = {
21447 +               /* .force_btgt  = -1, */
21448 +               .flags          = AuWrDir_ADD_ENTRY
21449 +       };
21450 +
21451 +       IMustLock(dir);
21452 +       inode = d_inode(src_dentry);
21453 +       IMustLock(inode);
21454 +
21455 +       err = -ENOMEM;
21456 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21457 +       if (unlikely(!a))
21458 +               goto out;
21459 +
21460 +       a->parent = dentry->d_parent; /* dir inode is locked */
21461 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
21462 +                                       AuLock_NOPLM | AuLock_GEN);
21463 +       if (unlikely(err))
21464 +               goto out_kfree;
21465 +       err = au_d_linkable(src_dentry);
21466 +       if (unlikely(err))
21467 +               goto out_unlock;
21468 +       err = au_d_may_add(dentry);
21469 +       if (unlikely(err))
21470 +               goto out_unlock;
21471 +
21472 +       a->src_parent = dget_parent(src_dentry);
21473 +       wr_dir_args.force_btgt = au_ibtop(inode);
21474 +
21475 +       di_write_lock_parent(a->parent);
21476 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21477 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21478 +                                     &wr_dir_args);
21479 +       err = PTR_ERR(wh_dentry);
21480 +       if (IS_ERR(wh_dentry))
21481 +               goto out_parent;
21482 +
21483 +       err = 0;
21484 +       sb = dentry->d_sb;
21485 +       a->bdst = au_dbtop(dentry);
21486 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21487 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21488 +       a->bsrc = au_ibtop(inode);
21489 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21490 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21491 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21492 +       if (!h_src_dentry) {
21493 +               a->bsrc = au_dbtop(src_dentry);
21494 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21495 +               AuDebugOn(!h_src_dentry);
21496 +       } else if (IS_ERR(h_src_dentry)) {
21497 +               err = PTR_ERR(h_src_dentry);
21498 +               goto out_parent;
21499 +       }
21500 +
21501 +       /*
21502 +        * aufs doesn't touch the credential so
21503 +        * security_dentry_create_files_as() is unnecessary.
21504 +        */
21505 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21506 +               if (a->bdst < a->bsrc
21507 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21508 +                       err = au_cpup_or_link(src_dentry, dentry, a);
21509 +               else {
21510 +                       delegated = NULL;
21511 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21512 +                                        &a->h_path, &delegated);
21513 +                       if (unlikely(err == -EWOULDBLOCK)) {
21514 +                               pr_warn("cannot retry for NFSv4 delegation"
21515 +                                       " for an internal link\n");
21516 +                               iput(delegated);
21517 +                       }
21518 +               }
21519 +               dput(h_src_dentry);
21520 +       } else {
21521 +               /*
21522 +                * copyup src_dentry to the branch we process,
21523 +                * and then link(2) to it.
21524 +                */
21525 +               dput(h_src_dentry);
21526 +               if (a->bdst < a->bsrc
21527 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21528 +                       au_unpin(&a->pin);
21529 +                       di_write_unlock(a->parent);
21530 +                       err = au_cpup_before_link(src_dentry, a);
21531 +                       di_write_lock_parent(a->parent);
21532 +                       if (!err)
21533 +                               err = au_pin(&a->pin, dentry, a->bdst,
21534 +                                            au_opt_udba(sb),
21535 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21536 +                       if (unlikely(err))
21537 +                               goto out_wh;
21538 +               }
21539 +               if (!err) {
21540 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21541 +                       err = -ENOENT;
21542 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
21543 +                               delegated = NULL;
21544 +                               err = vfsub_link(h_src_dentry,
21545 +                                                au_pinned_h_dir(&a->pin),
21546 +                                                &a->h_path, &delegated);
21547 +                               if (unlikely(err == -EWOULDBLOCK)) {
21548 +                                       pr_warn("cannot retry"
21549 +                                               " for NFSv4 delegation"
21550 +                                               " for an internal link\n");
21551 +                                       iput(delegated);
21552 +                               }
21553 +                       }
21554 +               }
21555 +       }
21556 +       if (unlikely(err))
21557 +               goto out_unpin;
21558 +
21559 +       if (wh_dentry) {
21560 +               a->h_path.dentry = wh_dentry;
21561 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21562 +                                         dentry);
21563 +               if (unlikely(err))
21564 +                       goto out_revert;
21565 +       }
21566 +
21567 +       au_dir_ts(dir, a->bdst);
21568 +       inode_inc_iversion(dir);
21569 +       inc_nlink(inode);
21570 +       inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
21571 +       d_instantiate(dentry, au_igrab(inode));
21572 +       if (d_unhashed(a->h_path.dentry))
21573 +               /* some filesystem calls d_drop() */
21574 +               d_drop(dentry);
21575 +       /* some filesystems consume an inode even hardlink */
21576 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
21577 +       goto out_unpin; /* success */
21578 +
21579 +out_revert:
21580 +       /* no delegation since it is just created */
21581 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21582 +                           /*delegated*/NULL, /*force*/0);
21583 +       if (unlikely(rerr)) {
21584 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21585 +               err = -EIO;
21586 +       }
21587 +       au_dtime_revert(&dt);
21588 +out_unpin:
21589 +       au_unpin(&a->pin);
21590 +out_wh:
21591 +       dput(wh_dentry);
21592 +out_parent:
21593 +       di_write_unlock(a->parent);
21594 +       dput(a->src_parent);
21595 +out_unlock:
21596 +       if (unlikely(err)) {
21597 +               au_update_dbtop(dentry);
21598 +               d_drop(dentry);
21599 +       }
21600 +       aufs_read_and_write_unlock2(dentry, src_dentry);
21601 +out_kfree:
21602 +       au_kfree_rcu(a);
21603 +out:
21604 +       AuTraceErr(err);
21605 +       return err;
21606 +}
21607 +
21608 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
21609 +              struct dentry *dentry, umode_t mode)
21610 +{
21611 +       int err, rerr;
21612 +       aufs_bindex_t bindex;
21613 +       unsigned char diropq;
21614 +       struct path h_path;
21615 +       struct dentry *wh_dentry, *parent, *opq_dentry;
21616 +       struct inode *h_inode;
21617 +       struct super_block *sb;
21618 +       struct {
21619 +               struct au_pin pin;
21620 +               struct au_dtime dt;
21621 +       } *a; /* reduce the stack usage */
21622 +       struct au_wr_dir_args wr_dir_args = {
21623 +               .force_btgt     = -1,
21624 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21625 +       };
21626 +
21627 +       IMustLock(dir);
21628 +
21629 +       err = -ENOMEM;
21630 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21631 +       if (unlikely(!a))
21632 +               goto out;
21633 +
21634 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21635 +       if (unlikely(err))
21636 +               goto out_free;
21637 +       err = au_d_may_add(dentry);
21638 +       if (unlikely(err))
21639 +               goto out_unlock;
21640 +
21641 +       parent = dentry->d_parent; /* dir inode is locked */
21642 +       di_write_lock_parent(parent);
21643 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21644 +                                     &a->pin, &wr_dir_args);
21645 +       err = PTR_ERR(wh_dentry);
21646 +       if (IS_ERR(wh_dentry))
21647 +               goto out_parent;
21648 +
21649 +       sb = dentry->d_sb;
21650 +       bindex = au_dbtop(dentry);
21651 +       h_path.dentry = au_h_dptr(dentry, bindex);
21652 +       h_path.mnt = au_sbr_mnt(sb, bindex);
21653 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21654 +       if (unlikely(err))
21655 +               goto out_unpin;
21656 +
21657 +       /* make the dir opaque */
21658 +       diropq = 0;
21659 +       h_inode = d_inode(h_path.dentry);
21660 +       if (wh_dentry
21661 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21662 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21663 +               opq_dentry = au_diropq_create(dentry, bindex);
21664 +               inode_unlock(h_inode);
21665 +               err = PTR_ERR(opq_dentry);
21666 +               if (IS_ERR(opq_dentry))
21667 +                       goto out_dir;
21668 +               dput(opq_dentry);
21669 +               diropq = 1;
21670 +       }
21671 +
21672 +       err = epilog(dir, bindex, wh_dentry, dentry);
21673 +       if (!err) {
21674 +               inc_nlink(dir);
21675 +               goto out_unpin; /* success */
21676 +       }
21677 +
21678 +       /* revert */
21679 +       if (diropq) {
21680 +               AuLabel(revert opq);
21681 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21682 +               rerr = au_diropq_remove(dentry, bindex);
21683 +               inode_unlock(h_inode);
21684 +               if (rerr) {
21685 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21686 +                               dentry, err, rerr);
21687 +                       err = -EIO;
21688 +               }
21689 +       }
21690 +
21691 +out_dir:
21692 +       AuLabel(revert dir);
21693 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21694 +       if (rerr) {
21695 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
21696 +                       dentry, err, rerr);
21697 +               err = -EIO;
21698 +       }
21699 +       au_dtime_revert(&a->dt);
21700 +out_unpin:
21701 +       au_unpin(&a->pin);
21702 +       dput(wh_dentry);
21703 +out_parent:
21704 +       di_write_unlock(parent);
21705 +out_unlock:
21706 +       if (unlikely(err)) {
21707 +               au_update_dbtop(dentry);
21708 +               d_drop(dentry);
21709 +       }
21710 +       aufs_read_unlock(dentry, AuLock_DW);
21711 +out_free:
21712 +       au_kfree_rcu(a);
21713 +out:
21714 +       return err;
21715 +}
21716 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
21717 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
21718 +++ linux/fs/aufs/i_op.c        2023-10-31 09:31:04.199880750 +0100
21719 @@ -0,0 +1,1516 @@
21720 +// SPDX-License-Identifier: GPL-2.0
21721 +/*
21722 + * Copyright (C) 2005-2022 Junjiro R. Okajima
21723 + *
21724 + * This program is free software; you can redistribute it and/or modify
21725 + * it under the terms of the GNU General Public License as published by
21726 + * the Free Software Foundation; either version 2 of the License, or
21727 + * (at your option) any later version.
21728 + *
21729 + * This program is distributed in the hope that it will be useful,
21730 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21731 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21732 + * GNU General Public License for more details.
21733 + *
21734 + * You should have received a copy of the GNU General Public License
21735 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21736 + */
21737 +
21738 +/*
21739 + * inode operations (except add/del/rename)
21740 + */
21741 +
21742 +#include <linux/device_cgroup.h>
21743 +#include <linux/filelock.h>
21744 +#include <linux/fs_stack.h>
21745 +#include <linux/iversion.h>
21746 +#include <linux/security.h>
21747 +#include "aufs.h"
21748 +
21749 +static int h_permission(struct inode *h_inode, int mask,
21750 +                       struct path *h_path, int brperm)
21751 +{
21752 +       int err;
21753 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21754 +       struct mnt_idmap *h_idmap;
21755 +
21756 +       err = -EPERM;
21757 +       if (write_mask && IS_IMMUTABLE(h_inode))
21758 +               goto out;
21759 +
21760 +       err = -EACCES;
21761 +       if (((mask & MAY_EXEC)
21762 +            && S_ISREG(h_inode->i_mode)
21763 +            && (path_noexec(h_path)
21764 +                || !(h_inode->i_mode & 0111))))
21765 +               goto out;
21766 +
21767 +       /*
21768 +        * - skip the lower fs test in the case of write to ro branch.
21769 +        * - nfs dir permission write check is optimized, but a policy for
21770 +        *   link/rename requires a real check.
21771 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
21772 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
21773 +        */
21774 +       h_idmap = mnt_idmap(h_path->mnt);
21775 +       if ((write_mask && !au_br_writable(brperm))
21776 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21777 +               && write_mask && !(mask & MAY_READ))
21778 +           || !h_inode->i_op->permission) {
21779 +               /* AuLabel(generic_permission); */
21780 +               /* AuDbg("get_inode_acl %ps\n",
21781 +                  h_inode->i_op->get_inode_acl); */
21782 +               err = generic_permission(h_idmap, h_inode, mask);
21783 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
21784 +                       err = h_inode->i_op->permission(h_idmap, h_inode,
21785 +                                                       mask);
21786 +               AuTraceErr(err);
21787 +       } else {
21788 +               /* AuLabel(h_inode->permission); */
21789 +               err = h_inode->i_op->permission(h_idmap, h_inode, mask);
21790 +               AuTraceErr(err);
21791 +       }
21792 +
21793 +       if (!err)
21794 +               err = devcgroup_inode_permission(h_inode, mask);
21795 +       if (!err)
21796 +               err = security_inode_permission(h_inode, mask);
21797 +
21798 +out:
21799 +       return err;
21800 +}
21801 +
21802 +static int aufs_permission(struct mnt_idmap *idmap, struct inode *inode,
21803 +                          int mask)
21804 +{
21805 +       int err;
21806 +       aufs_bindex_t bindex, bbot;
21807 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21808 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21809 +       struct inode *h_inode;
21810 +       struct super_block *sb;
21811 +       struct au_branch *br;
21812 +
21813 +       /* todo: support rcu-walk? */
21814 +       if (mask & MAY_NOT_BLOCK)
21815 +               return -ECHILD;
21816 +
21817 +       sb = inode->i_sb;
21818 +       si_read_lock(sb, AuLock_FLUSH);
21819 +       ii_read_lock_child(inode);
21820 +#if 0 /* reserved for future use */
21821 +       /*
21822 +        * This test may be rather 'too much' since the test is essentially done
21823 +        * in the aufs_lookup().  Theoretically it is possible that the inode
21824 +        * generation doesn't match to the superblock's here.  But it isn't a
21825 +        * big deal I suppose.
21826 +        */
21827 +       err = au_iigen_test(inode, au_sigen(sb));
21828 +       if (unlikely(err))
21829 +               goto out;
21830 +#endif
21831 +
21832 +       if (!isdir
21833 +           || write_mask
21834 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
21835 +               err = au_busy_or_stale();
21836 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
21837 +               if (unlikely(!h_inode
21838 +                            || (h_inode->i_mode & S_IFMT)
21839 +                            != (inode->i_mode & S_IFMT)))
21840 +                       goto out;
21841 +
21842 +               err = 0;
21843 +               bindex = au_ibtop(inode);
21844 +               br = au_sbr(sb, bindex);
21845 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
21846 +               if (write_mask
21847 +                   && !err
21848 +                   && !special_file(h_inode->i_mode)) {
21849 +                       /* test whether the upper writable branch exists */
21850 +                       err = -EROFS;
21851 +                       for (; bindex >= 0; bindex--)
21852 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
21853 +                                       err = 0;
21854 +                                       break;
21855 +                               }
21856 +               }
21857 +               goto out;
21858 +       }
21859 +
21860 +       /* non-write to dir */
21861 +       err = 0;
21862 +       bbot = au_ibbot(inode);
21863 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
21864 +               h_inode = au_h_iptr(inode, bindex);
21865 +               if (h_inode) {
21866 +                       err = au_busy_or_stale();
21867 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
21868 +                               break;
21869 +
21870 +                       br = au_sbr(sb, bindex);
21871 +                       err = h_permission(h_inode, mask, &br->br_path,
21872 +                                          br->br_perm);
21873 +               }
21874 +       }
21875 +
21876 +out:
21877 +       ii_read_unlock(inode);
21878 +       si_read_unlock(sb);
21879 +       return err;
21880 +}
21881 +
21882 +/* ---------------------------------------------------------------------- */
21883 +
21884 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
21885 +                                 unsigned int flags)
21886 +{
21887 +       struct dentry *ret, *parent;
21888 +       struct inode *inode;
21889 +       struct super_block *sb;
21890 +       int err, npositive;
21891 +
21892 +       IMustLock(dir);
21893 +
21894 +       /* todo: support rcu-walk? */
21895 +       ret = ERR_PTR(-ECHILD);
21896 +       if (flags & LOOKUP_RCU)
21897 +               goto out;
21898 +
21899 +       ret = ERR_PTR(-ENAMETOOLONG);
21900 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21901 +               goto out;
21902 +
21903 +       sb = dir->i_sb;
21904 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21905 +       ret = ERR_PTR(err);
21906 +       if (unlikely(err))
21907 +               goto out;
21908 +
21909 +       err = au_di_init(dentry);
21910 +       ret = ERR_PTR(err);
21911 +       if (unlikely(err))
21912 +               goto out_si;
21913 +
21914 +       inode = NULL;
21915 +       npositive = 0; /* suppress a warning */
21916 +       parent = dentry->d_parent; /* dir inode is locked */
21917 +       di_read_lock_parent(parent, AuLock_IR);
21918 +       err = au_alive_dir(parent);
21919 +       if (!err)
21920 +               err = au_digen_test(parent, au_sigen(sb));
21921 +       if (!err) {
21922 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21923 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21924 +                                          AuLkup_ALLOW_NEG);
21925 +               err = npositive;
21926 +       }
21927 +       di_read_unlock(parent, AuLock_IR);
21928 +       ret = ERR_PTR(err);
21929 +       if (unlikely(err < 0))
21930 +               goto out_unlock;
21931 +
21932 +       if (npositive) {
21933 +               inode = au_new_inode(dentry, /*must_new*/0);
21934 +               if (IS_ERR(inode)) {
21935 +                       ret = (void *)inode;
21936 +                       inode = NULL;
21937 +                       goto out_unlock;
21938 +               }
21939 +       }
21940 +
21941 +       if (inode)
21942 +               atomic_inc(&inode->i_count);
21943 +       ret = d_splice_alias(inode, dentry);
21944 +#if 0 /* reserved for future use */
21945 +       if (unlikely(d_need_lookup(dentry))) {
21946 +               spin_lock(&dentry->d_lock);
21947 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21948 +               spin_unlock(&dentry->d_lock);
21949 +       } else
21950 +#endif
21951 +       if (inode) {
21952 +               if (!IS_ERR(ret)) {
21953 +                       iput(inode);
21954 +                       if (ret && ret != dentry)
21955 +                               ii_write_unlock(inode);
21956 +               } else {
21957 +                       ii_write_unlock(inode);
21958 +                       iput(inode);
21959 +                       inode = NULL;
21960 +               }
21961 +       }
21962 +
21963 +out_unlock:
21964 +       di_write_unlock(dentry);
21965 +out_si:
21966 +       si_read_unlock(sb);
21967 +out:
21968 +       return ret;
21969 +}
21970 +
21971 +/* ---------------------------------------------------------------------- */
21972 +
21973 +/*
21974 + * very dirty and complicated aufs ->atomic_open().
21975 + * aufs_atomic_open()
21976 + * + au_aopen_or_create()
21977 + *   + add_simple()
21978 + *     + vfsub_atomic_open()
21979 + *       + branch fs ->atomic_open()
21980 + *        may call the actual 'open' for h_file
21981 + *       + inc br_nfiles only if opened
21982 + * + au_aopen_no_open() or au_aopen_do_open()
21983 + *
21984 + * au_aopen_do_open()
21985 + * + finish_open()
21986 + *   + au_do_aopen()
21987 + *     + au_do_open() the body of all 'open'
21988 + *       + au_do_open_nondir()
21989 + *        set the passed h_file
21990 + *
21991 + * au_aopen_no_open()
21992 + * + finish_no_open()
21993 + */
21994 +
21995 +struct aopen_node {
21996 +       struct hlist_bl_node hblist;
21997 +       struct file *file, *h_file;
21998 +};
21999 +
22000 +static int au_do_aopen(struct inode *inode, struct file *file)
22001 +{
22002 +       struct hlist_bl_head *aopen;
22003 +       struct hlist_bl_node *pos;
22004 +       struct aopen_node *node;
22005 +       struct au_do_open_args args = {
22006 +               .aopen  = 1,
22007 +               .open   = au_do_open_nondir
22008 +       };
22009 +
22010 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
22011 +       hlist_bl_lock(aopen);
22012 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
22013 +               if (node->file == file) {
22014 +                       args.h_file = node->h_file;
22015 +                       break;
22016 +               }
22017 +       hlist_bl_unlock(aopen);
22018 +       /* AuDebugOn(!args.h_file); */
22019 +
22020 +       return au_do_open(file, &args);
22021 +}
22022 +
22023 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
22024 +                           struct aopen_node *aopen_node)
22025 +{
22026 +       int err;
22027 +       struct hlist_bl_head *aopen;
22028 +
22029 +       AuLabel(here);
22030 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
22031 +       au_hbl_add(&aopen_node->hblist, aopen);
22032 +       err = finish_open(file, dentry, au_do_aopen);
22033 +       au_hbl_del(&aopen_node->hblist, aopen);
22034 +       /* AuDbgFile(file); */
22035 +       AuDbg("%pd%s%s\n", dentry,
22036 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
22037 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
22038 +
22039 +       AuTraceErr(err);
22040 +       return err;
22041 +}
22042 +
22043 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
22044 +{
22045 +       int err;
22046 +
22047 +       AuLabel(here);
22048 +       dget(dentry);
22049 +       err = finish_no_open(file, dentry);
22050 +
22051 +       AuTraceErr(err);
22052 +       return err;
22053 +}
22054 +
22055 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22056 +                           struct file *file, unsigned int open_flag,
22057 +                           umode_t create_mode)
22058 +{
22059 +       int err, did_open;
22060 +       unsigned int lkup_flags;
22061 +       aufs_bindex_t bindex;
22062 +       struct super_block *sb;
22063 +       struct dentry *parent, *d;
22064 +       struct vfsub_aopen_args args = {
22065 +               .open_flag      = open_flag,
22066 +               .create_mode    = create_mode
22067 +       };
22068 +       struct aopen_node aopen_node = {
22069 +               .file   = file
22070 +       };
22071 +
22072 +       IMustLock(dir);
22073 +       AuDbg("open_flag 0%o\n", open_flag);
22074 +       AuDbgDentry(dentry);
22075 +
22076 +       err = 0;
22077 +       if (!au_di(dentry)) {
22078 +               lkup_flags = LOOKUP_OPEN;
22079 +               if (open_flag & O_CREAT)
22080 +                       lkup_flags |= LOOKUP_CREATE;
22081 +               d = aufs_lookup(dir, dentry, lkup_flags);
22082 +               if (IS_ERR(d)) {
22083 +                       err = PTR_ERR(d);
22084 +                       AuTraceErr(err);
22085 +                       goto out;
22086 +               } else if (d) {
22087 +                       /*
22088 +                        * obsoleted dentry found.
22089 +                        * another error will be returned later.
22090 +                        */
22091 +                       d_drop(d);
22092 +                       AuDbgDentry(d);
22093 +                       dput(d);
22094 +               }
22095 +               AuDbgDentry(dentry);
22096 +       }
22097 +
22098 +       if (d_is_positive(dentry)
22099 +           || d_unhashed(dentry)
22100 +           || d_unlinked(dentry)
22101 +           || !(open_flag & O_CREAT)) {
22102 +               err = au_aopen_no_open(file, dentry);
22103 +               goto out; /* success */
22104 +       }
22105 +
22106 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22107 +       if (unlikely(err))
22108 +               goto out;
22109 +
22110 +       sb = dentry->d_sb;
22111 +       parent = dentry->d_parent;      /* dir is locked */
22112 +       di_write_lock_parent(parent);
22113 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
22114 +       if (unlikely(err < 0))
22115 +               goto out_parent;
22116 +
22117 +       AuDbgDentry(dentry);
22118 +       if (d_is_positive(dentry)) {
22119 +               err = au_aopen_no_open(file, dentry);
22120 +               goto out_parent; /* success */
22121 +       }
22122 +
22123 +       args.file = alloc_empty_file(file->f_flags, current_cred());
22124 +       err = PTR_ERR(args.file);
22125 +       if (IS_ERR(args.file))
22126 +               goto out_parent;
22127 +
22128 +       bindex = au_dbtop(dentry);
22129 +       err = au_aopen_or_create(dir, dentry, &args);
22130 +       AuTraceErr(err);
22131 +       AuDbgFile(args.file);
22132 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22133 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
22134 +       if (!did_open) {
22135 +               fput(args.file);
22136 +               args.file = NULL;
22137 +       }
22138 +       di_write_unlock(parent);
22139 +       di_write_unlock(dentry);
22140 +       if (unlikely(err < 0)) {
22141 +               if (args.file)
22142 +                       fput(args.file);
22143 +               goto out_sb;
22144 +       }
22145 +
22146 +       if (!did_open)
22147 +               err = au_aopen_no_open(file, dentry);
22148 +       else {
22149 +               aopen_node.h_file = args.file;
22150 +               err = au_aopen_do_open(file, dentry, &aopen_node);
22151 +       }
22152 +       if (unlikely(err < 0)) {
22153 +               if (args.file)
22154 +                       fput(args.file);
22155 +               if (did_open)
22156 +                       au_lcnt_dec(&args.br->br_nfiles);
22157 +       }
22158 +       goto out_sb; /* success */
22159 +
22160 +out_parent:
22161 +       di_write_unlock(parent);
22162 +       di_write_unlock(dentry);
22163 +out_sb:
22164 +       si_read_unlock(sb);
22165 +out:
22166 +       AuTraceErr(err);
22167 +       AuDbgFile(file);
22168 +       return err;
22169 +}
22170 +
22171 +
22172 +/* ---------------------------------------------------------------------- */
22173 +
22174 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22175 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
22176 +                         aufs_bindex_t btop)
22177 +{
22178 +       int err;
22179 +       struct dentry *h_parent;
22180 +       struct inode *h_dir;
22181 +
22182 +       if (add_entry)
22183 +               IMustLock(d_inode(parent));
22184 +       else
22185 +               di_write_lock_parent(parent);
22186 +
22187 +       err = 0;
22188 +       if (!au_h_dptr(parent, bcpup)) {
22189 +               if (btop > bcpup)
22190 +                       err = au_cpup_dirs(dentry, bcpup);
22191 +               else if (btop < bcpup)
22192 +                       err = au_cpdown_dirs(dentry, bcpup);
22193 +               else
22194 +                       BUG();
22195 +       }
22196 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
22197 +               h_parent = au_h_dptr(parent, bcpup);
22198 +               h_dir = d_inode(h_parent);
22199 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
22200 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
22201 +               /* todo: no unlock here */
22202 +               inode_unlock_shared(h_dir);
22203 +
22204 +               AuDbg("bcpup %d\n", bcpup);
22205 +               if (!err) {
22206 +                       if (d_really_is_negative(dentry))
22207 +                               au_set_h_dptr(dentry, btop, NULL);
22208 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
22209 +               }
22210 +       }
22211 +
22212 +       if (!add_entry)
22213 +               di_write_unlock(parent);
22214 +       if (!err)
22215 +               err = bcpup; /* success */
22216 +
22217 +       AuTraceErr(err);
22218 +       return err;
22219 +}
22220 +
22221 +/*
22222 + * decide the branch and the parent dir where we will create a new entry.
22223 + * returns new bindex or an error.
22224 + * copyup the parent dir if needed.
22225 + */
22226 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22227 +             struct au_wr_dir_args *args)
22228 +{
22229 +       int err;
22230 +       unsigned int flags;
22231 +       aufs_bindex_t bcpup, btop, src_btop;
22232 +       const unsigned char add_entry
22233 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
22234 +               | au_ftest_wrdir(args->flags, TMPFILE);
22235 +       struct super_block *sb;
22236 +       struct dentry *parent;
22237 +       struct au_sbinfo *sbinfo;
22238 +
22239 +       sb = dentry->d_sb;
22240 +       sbinfo = au_sbi(sb);
22241 +       parent = dget_parent(dentry);
22242 +       btop = au_dbtop(dentry);
22243 +       bcpup = btop;
22244 +       if (args->force_btgt < 0) {
22245 +               if (src_dentry) {
22246 +                       src_btop = au_dbtop(src_dentry);
22247 +                       if (src_btop < btop)
22248 +                               bcpup = src_btop;
22249 +               } else if (add_entry) {
22250 +                       flags = 0;
22251 +                       if (au_ftest_wrdir(args->flags, ISDIR))
22252 +                               au_fset_wbr(flags, DIR);
22253 +                       err = AuWbrCreate(sbinfo, dentry, flags);
22254 +                       bcpup = err;
22255 +               }
22256 +
22257 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
22258 +                       if (add_entry)
22259 +                               err = AuWbrCopyup(sbinfo, dentry);
22260 +                       else {
22261 +                               if (!IS_ROOT(dentry)) {
22262 +                                       di_read_lock_parent(parent, !AuLock_IR);
22263 +                                       err = AuWbrCopyup(sbinfo, dentry);
22264 +                                       di_read_unlock(parent, !AuLock_IR);
22265 +                               } else
22266 +                                       err = AuWbrCopyup(sbinfo, dentry);
22267 +                       }
22268 +                       bcpup = err;
22269 +                       if (unlikely(err < 0))
22270 +                               goto out;
22271 +               }
22272 +       } else {
22273 +               bcpup = args->force_btgt;
22274 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
22275 +       }
22276 +
22277 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
22278 +       err = bcpup;
22279 +       if (bcpup == btop)
22280 +               goto out; /* success */
22281 +
22282 +       /* copyup the new parent into the branch we process */
22283 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
22284 +       if (err >= 0) {
22285 +               if (d_really_is_negative(dentry)) {
22286 +                       au_set_h_dptr(dentry, btop, NULL);
22287 +                       au_set_dbtop(dentry, bcpup);
22288 +                       au_set_dbbot(dentry, bcpup);
22289 +               }
22290 +               AuDebugOn(add_entry
22291 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
22292 +                         && !au_h_dptr(dentry, bcpup));
22293 +       }
22294 +
22295 +out:
22296 +       dput(parent);
22297 +       return err;
22298 +}
22299 +
22300 +/* ---------------------------------------------------------------------- */
22301 +
22302 +void au_pin_hdir_unlock(struct au_pin *p)
22303 +{
22304 +       if (p->hdir)
22305 +               au_hn_inode_unlock(p->hdir);
22306 +}
22307 +
22308 +int au_pin_hdir_lock(struct au_pin *p)
22309 +{
22310 +       int err;
22311 +
22312 +       err = 0;
22313 +       if (!p->hdir)
22314 +               goto out;
22315 +
22316 +       /* even if an error happens later, keep this lock */
22317 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
22318 +
22319 +       err = -EBUSY;
22320 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
22321 +               goto out;
22322 +
22323 +       err = 0;
22324 +       if (p->h_dentry)
22325 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22326 +                                 p->h_parent, p->br);
22327 +
22328 +out:
22329 +       return err;
22330 +}
22331 +
22332 +int au_pin_hdir_relock(struct au_pin *p)
22333 +{
22334 +       int err, i;
22335 +       struct inode *h_i;
22336 +       struct dentry *h_d[] = {
22337 +               p->h_dentry,
22338 +               p->h_parent
22339 +       };
22340 +
22341 +       err = au_pin_hdir_lock(p);
22342 +       if (unlikely(err))
22343 +               goto out;
22344 +
22345 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22346 +               if (!h_d[i])
22347 +                       continue;
22348 +               if (d_is_positive(h_d[i])) {
22349 +                       h_i = d_inode(h_d[i]);
22350 +                       err = !h_i->i_nlink;
22351 +               }
22352 +       }
22353 +
22354 +out:
22355 +       return err;
22356 +}
22357 +
22358 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
22359 +{
22360 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
22361 +}
22362 +
22363 +void au_pin_hdir_acquire_nest(struct au_pin *p)
22364 +{
22365 +       if (p->hdir) {
22366 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
22367 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
22368 +               au_pin_hdir_set_owner(p, current);
22369 +       }
22370 +}
22371 +
22372 +void au_pin_hdir_release(struct au_pin *p)
22373 +{
22374 +       if (p->hdir) {
22375 +               au_pin_hdir_set_owner(p, p->task);
22376 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
22377 +       }
22378 +}
22379 +
22380 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
22381 +{
22382 +       if (pin && pin->parent)
22383 +               return au_h_dptr(pin->parent, pin->bindex);
22384 +       return NULL;
22385 +}
22386 +
22387 +void au_unpin(struct au_pin *p)
22388 +{
22389 +       if (p->hdir)
22390 +               au_pin_hdir_unlock(p);
22391 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
22392 +               vfsub_mnt_drop_write(p->h_mnt);
22393 +       if (!p->hdir)
22394 +               return;
22395 +
22396 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22397 +               di_read_unlock(p->parent, AuLock_IR);
22398 +       iput(p->hdir->hi_inode);
22399 +       dput(p->parent);
22400 +       p->parent = NULL;
22401 +       p->hdir = NULL;
22402 +       p->h_mnt = NULL;
22403 +       /* do not clear p->task */
22404 +}
22405 +
22406 +int au_do_pin(struct au_pin *p)
22407 +{
22408 +       int err;
22409 +       struct super_block *sb;
22410 +       struct inode *h_dir;
22411 +
22412 +       err = 0;
22413 +       sb = p->dentry->d_sb;
22414 +       p->br = au_sbr(sb, p->bindex);
22415 +       if (IS_ROOT(p->dentry)) {
22416 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
22417 +                       p->h_mnt = au_br_mnt(p->br);
22418 +                       err = vfsub_mnt_want_write(p->h_mnt);
22419 +                       if (unlikely(err)) {
22420 +                               au_fclr_pin(p->flags, MNT_WRITE);
22421 +                               goto out_err;
22422 +                       }
22423 +               }
22424 +               goto out;
22425 +       }
22426 +
22427 +       p->h_dentry = NULL;
22428 +       if (p->bindex <= au_dbbot(p->dentry))
22429 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
22430 +
22431 +       p->parent = dget_parent(p->dentry);
22432 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22433 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
22434 +
22435 +       h_dir = NULL;
22436 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
22437 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
22438 +       if (p->hdir)
22439 +               h_dir = p->hdir->hi_inode;
22440 +
22441 +       /*
22442 +        * udba case, or
22443 +        * if DI_LOCKED is not set, then p->parent may be different
22444 +        * and h_parent can be NULL.
22445 +        */
22446 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
22447 +               err = -EBUSY;
22448 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
22449 +                       di_read_unlock(p->parent, AuLock_IR);
22450 +               dput(p->parent);
22451 +               p->parent = NULL;
22452 +               goto out_err;
22453 +       }
22454 +
22455 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
22456 +               p->h_mnt = au_br_mnt(p->br);
22457 +               err = vfsub_mnt_want_write(p->h_mnt);
22458 +               if (unlikely(err)) {
22459 +                       au_fclr_pin(p->flags, MNT_WRITE);
22460 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
22461 +                               di_read_unlock(p->parent, AuLock_IR);
22462 +                       dput(p->parent);
22463 +                       p->parent = NULL;
22464 +                       goto out_err;
22465 +               }
22466 +       }
22467 +
22468 +       au_igrab(h_dir);
22469 +       err = au_pin_hdir_lock(p);
22470 +       if (!err)
22471 +               goto out; /* success */
22472 +
22473 +       au_unpin(p);
22474 +
22475 +out_err:
22476 +       pr_err("err %d\n", err);
22477 +       err = au_busy_or_stale();
22478 +out:
22479 +       return err;
22480 +}
22481 +
22482 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
22483 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22484 +                unsigned int udba, unsigned char flags)
22485 +{
22486 +       p->dentry = dentry;
22487 +       p->udba = udba;
22488 +       p->lsc_di = lsc_di;
22489 +       p->lsc_hi = lsc_hi;
22490 +       p->flags = flags;
22491 +       p->bindex = bindex;
22492 +
22493 +       p->parent = NULL;
22494 +       p->hdir = NULL;
22495 +       p->h_mnt = NULL;
22496 +
22497 +       p->h_dentry = NULL;
22498 +       p->h_parent = NULL;
22499 +       p->br = NULL;
22500 +       p->task = current;
22501 +}
22502 +
22503 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22504 +          unsigned int udba, unsigned char flags)
22505 +{
22506 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22507 +                   udba, flags);
22508 +       return au_do_pin(pin);
22509 +}
22510 +
22511 +/* ---------------------------------------------------------------------- */
22512 +
22513 +/*
22514 + * ->setattr() and ->getattr() are called in various cases.
22515 + * chmod, stat: dentry is revalidated.
22516 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22517 + *               unhashed.
22518 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
22519 + */
22520 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
22521 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
22522 +{
22523 +       int err;
22524 +       struct dentry *parent;
22525 +
22526 +       err = 0;
22527 +       if (au_digen_test(dentry, sigen)) {
22528 +               parent = dget_parent(dentry);
22529 +               di_read_lock_parent(parent, AuLock_IR);
22530 +               err = au_refresh_dentry(dentry, parent);
22531 +               di_read_unlock(parent, AuLock_IR);
22532 +               dput(parent);
22533 +       }
22534 +
22535 +       AuTraceErr(err);
22536 +       return err;
22537 +}
22538 +
22539 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22540 +                    struct au_icpup_args *a)
22541 +{
22542 +       int err;
22543 +       loff_t sz;
22544 +       aufs_bindex_t btop, ibtop;
22545 +       struct dentry *hi_wh, *parent;
22546 +       struct inode *inode;
22547 +       struct au_wr_dir_args wr_dir_args = {
22548 +               .force_btgt     = -1,
22549 +               .flags          = 0
22550 +       };
22551 +
22552 +       if (d_is_dir(dentry))
22553 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
22554 +       /* plink or hi_wh() case */
22555 +       btop = au_dbtop(dentry);
22556 +       inode = d_inode(dentry);
22557 +       ibtop = au_ibtop(inode);
22558 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22559 +               wr_dir_args.force_btgt = ibtop;
22560 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22561 +       if (unlikely(err < 0))
22562 +               goto out;
22563 +       a->btgt = err;
22564 +       if (err != btop)
22565 +               au_fset_icpup(a->flags, DID_CPUP);
22566 +
22567 +       err = 0;
22568 +       a->pin_flags = AuPin_MNT_WRITE;
22569 +       parent = NULL;
22570 +       if (!IS_ROOT(dentry)) {
22571 +               au_fset_pin(a->pin_flags, DI_LOCKED);
22572 +               parent = dget_parent(dentry);
22573 +               di_write_lock_parent(parent);
22574 +       }
22575 +
22576 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22577 +       if (unlikely(err))
22578 +               goto out_parent;
22579 +
22580 +       sz = -1;
22581 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22582 +       a->h_inode = d_inode(a->h_path.dentry);
22583 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
22584 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
22585 +               if (ia->ia_size < i_size_read(a->h_inode))
22586 +                       sz = ia->ia_size;
22587 +               inode_unlock_shared(a->h_inode);
22588 +       }
22589 +
22590 +       hi_wh = NULL;
22591 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
22592 +               hi_wh = au_hi_wh(inode, a->btgt);
22593 +               if (!hi_wh) {
22594 +                       struct au_cp_generic cpg = {
22595 +                               .dentry = dentry,
22596 +                               .bdst   = a->btgt,
22597 +                               .bsrc   = -1,
22598 +                               .len    = sz,
22599 +                               .pin    = &a->pin
22600 +                       };
22601 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
22602 +                       if (unlikely(err))
22603 +                               goto out_unlock;
22604 +                       hi_wh = au_hi_wh(inode, a->btgt);
22605 +                       /* todo: revalidate hi_wh? */
22606 +               }
22607 +       }
22608 +
22609 +       if (parent) {
22610 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22611 +               di_downgrade_lock(parent, AuLock_IR);
22612 +               dput(parent);
22613 +               parent = NULL;
22614 +       }
22615 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
22616 +               goto out; /* success */
22617 +
22618 +       if (!d_unhashed(dentry)) {
22619 +               struct au_cp_generic cpg = {
22620 +                       .dentry = dentry,
22621 +                       .bdst   = a->btgt,
22622 +                       .bsrc   = btop,
22623 +                       .len    = sz,
22624 +                       .pin    = &a->pin,
22625 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
22626 +               };
22627 +               err = au_sio_cpup_simple(&cpg);
22628 +               if (!err)
22629 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22630 +       } else if (!hi_wh)
22631 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22632 +       else
22633 +               a->h_path.dentry = hi_wh; /* do not dget here */
22634 +
22635 +out_unlock:
22636 +       a->h_inode = d_inode(a->h_path.dentry);
22637 +       if (!err)
22638 +               goto out; /* success */
22639 +       au_unpin(&a->pin);
22640 +out_parent:
22641 +       if (parent) {
22642 +               di_write_unlock(parent);
22643 +               dput(parent);
22644 +       }
22645 +out:
22646 +       if (!err)
22647 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22648 +       return err;
22649 +}
22650 +
22651 +static int aufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
22652 +                       struct iattr *ia)
22653 +{
22654 +       int err;
22655 +       struct inode *inode, *delegated;
22656 +       struct super_block *sb;
22657 +       struct file *file;
22658 +       struct au_icpup_args *a;
22659 +       struct mnt_idmap *h_idmap;
22660 +
22661 +       inode = d_inode(dentry);
22662 +       IMustLock(inode);
22663 +
22664 +       err = setattr_prepare(idmap, dentry, ia);
22665 +       if (unlikely(err))
22666 +               goto out;
22667 +
22668 +       err = -ENOMEM;
22669 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22670 +       if (unlikely(!a))
22671 +               goto out;
22672 +
22673 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22674 +               ia->ia_valid &= ~ATTR_MODE;
22675 +
22676 +       file = NULL;
22677 +       sb = dentry->d_sb;
22678 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22679 +       if (unlikely(err))
22680 +               goto out_kfree;
22681 +
22682 +       if (ia->ia_valid & ATTR_FILE) {
22683 +               /* currently ftruncate(2) only */
22684 +               AuDebugOn(!d_is_reg(dentry));
22685 +               file = ia->ia_file;
22686 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22687 +                                           /*fi_lsc*/0);
22688 +               if (unlikely(err))
22689 +                       goto out_si;
22690 +               ia->ia_file = au_hf_top(file);
22691 +               a->udba = AuOpt_UDBA_NONE;
22692 +       } else {
22693 +               /* fchmod() doesn't pass ia_file */
22694 +               a->udba = au_opt_udba(sb);
22695 +               di_write_lock_child(dentry);
22696 +               /* no d_unlinked(), to set UDBA_NONE for root */
22697 +               if (d_unhashed(dentry))
22698 +                       a->udba = AuOpt_UDBA_NONE;
22699 +               if (a->udba != AuOpt_UDBA_NONE) {
22700 +                       AuDebugOn(IS_ROOT(dentry));
22701 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
22702 +                       if (unlikely(err))
22703 +                               goto out_dentry;
22704 +               }
22705 +       }
22706 +
22707 +       err = au_pin_and_icpup(dentry, ia, a);
22708 +       if (unlikely(err < 0))
22709 +               goto out_dentry;
22710 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
22711 +               ia->ia_file = NULL;
22712 +               ia->ia_valid &= ~ATTR_FILE;
22713 +       }
22714 +
22715 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22716 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22717 +           == (ATTR_MODE | ATTR_CTIME)) {
22718 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
22719 +               if (unlikely(err))
22720 +                       goto out_unlock;
22721 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22722 +                  && (ia->ia_valid & ATTR_CTIME)) {
22723 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
22724 +               if (unlikely(err))
22725 +                       goto out_unlock;
22726 +       }
22727 +
22728 +       if (ia->ia_valid & ATTR_SIZE) {
22729 +               struct file *f;
22730 +
22731 +               if (ia->ia_size < i_size_read(inode))
22732 +                       /* unmap only */
22733 +                       truncate_setsize(inode, ia->ia_size);
22734 +
22735 +               f = NULL;
22736 +               if (ia->ia_valid & ATTR_FILE)
22737 +                       f = ia->ia_file;
22738 +               inode_unlock(a->h_inode);
22739 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
22740 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22741 +       } else {
22742 +               delegated = NULL;
22743 +               while (1) {
22744 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
22745 +                       if (delegated) {
22746 +                               err = break_deleg_wait(&delegated);
22747 +                               if (!err)
22748 +                                       continue;
22749 +                       }
22750 +                       break;
22751 +               }
22752 +       }
22753 +       /*
22754 +        * regardless aufs 'acl' option setting.
22755 +        * why don't all acl-aware fs call this func from their ->setattr()?
22756 +        */
22757 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
22758 +               h_idmap = mnt_idmap(a->h_path.mnt);
22759 +               err = vfsub_acl_chmod(h_idmap, a->h_path.dentry, ia->ia_mode);
22760 +       }
22761 +       if (!err)
22762 +               au_cpup_attr_changeable(inode);
22763 +
22764 +out_unlock:
22765 +       inode_unlock(a->h_inode);
22766 +       au_unpin(&a->pin);
22767 +       if (unlikely(err))
22768 +               au_update_dbtop(dentry);
22769 +out_dentry:
22770 +       di_write_unlock(dentry);
22771 +       if (file) {
22772 +               fi_write_unlock(file);
22773 +               ia->ia_file = file;
22774 +               ia->ia_valid |= ATTR_FILE;
22775 +       }
22776 +out_si:
22777 +       si_read_unlock(sb);
22778 +out_kfree:
22779 +       au_kfree_rcu(a);
22780 +out:
22781 +       AuTraceErr(err);
22782 +       return err;
22783 +}
22784 +
22785 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22786 +static int au_h_path_to_set_attr(struct dentry *dentry,
22787 +                                struct au_icpup_args *a, struct path *h_path)
22788 +{
22789 +       int err;
22790 +       struct super_block *sb;
22791 +
22792 +       sb = dentry->d_sb;
22793 +       a->udba = au_opt_udba(sb);
22794 +       /* no d_unlinked(), to set UDBA_NONE for root */
22795 +       if (d_unhashed(dentry))
22796 +               a->udba = AuOpt_UDBA_NONE;
22797 +       if (a->udba != AuOpt_UDBA_NONE) {
22798 +               AuDebugOn(IS_ROOT(dentry));
22799 +               err = au_reval_for_attr(dentry, au_sigen(sb));
22800 +               if (unlikely(err))
22801 +                       goto out;
22802 +       }
22803 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22804 +       if (unlikely(err < 0))
22805 +               goto out;
22806 +
22807 +       h_path->dentry = a->h_path.dentry;
22808 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
22809 +
22810 +out:
22811 +       return err;
22812 +}
22813 +
22814 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22815 +                 struct au_sxattr *arg)
22816 +{
22817 +       int err;
22818 +       struct path h_path;
22819 +       struct super_block *sb;
22820 +       struct au_icpup_args *a;
22821 +       struct inode *h_inode;
22822 +       struct mnt_idmap *h_idmap;
22823 +
22824 +       IMustLock(inode);
22825 +
22826 +       err = -ENOMEM;
22827 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22828 +       if (unlikely(!a))
22829 +               goto out;
22830 +
22831 +       sb = dentry->d_sb;
22832 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22833 +       if (unlikely(err))
22834 +               goto out_kfree;
22835 +
22836 +       h_path.dentry = NULL;   /* silence gcc */
22837 +       di_write_lock_child(dentry);
22838 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
22839 +       if (unlikely(err))
22840 +               goto out_di;
22841 +       h_idmap = mnt_idmap(h_path.mnt);
22842 +
22843 +       inode_unlock(a->h_inode);
22844 +       switch (arg->type) {
22845 +       case AU_XATTR_SET:
22846 +               AuDebugOn(d_is_negative(h_path.dentry));
22847 +               err = vfsub_setxattr(h_idmap, h_path.dentry,
22848 +                                    arg->u.set.name, arg->u.set.value,
22849 +                                    arg->u.set.size, arg->u.set.flags);
22850 +               break;
22851 +       case AU_ACL_SET:
22852 +               err = -EOPNOTSUPP;
22853 +               h_inode = d_inode(h_path.dentry);
22854 +               if (h_inode->i_op->set_acl) {
22855 +                       /* this will call posix_acl_update_mode */
22856 +                       err = h_inode->i_op->set_acl(h_idmap, h_path.dentry,
22857 +                                                    arg->u.acl_set.acl,
22858 +                                                    arg->u.acl_set.type);
22859 +               }
22860 +               break;
22861 +       }
22862 +       if (!err)
22863 +               au_cpup_attr_timesizes(inode);
22864 +
22865 +       au_unpin(&a->pin);
22866 +       if (unlikely(err))
22867 +               au_update_dbtop(dentry);
22868 +
22869 +out_di:
22870 +       di_write_unlock(dentry);
22871 +       si_read_unlock(sb);
22872 +out_kfree:
22873 +       au_kfree_rcu(a);
22874 +out:
22875 +       AuTraceErr(err);
22876 +       return err;
22877 +}
22878 +#endif
22879 +
22880 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22881 +                            unsigned int nlink)
22882 +{
22883 +       unsigned int n;
22884 +
22885 +       inode->i_mode = st->mode;
22886 +       /* don't i_[ug]id_write() here */
22887 +       inode->i_uid = st->uid;
22888 +       inode->i_gid = st->gid;
22889 +       inode->i_atime = st->atime;
22890 +       inode->i_mtime = st->mtime;
22891 +       inode_set_ctime_to_ts(inode, st->ctime);
22892 +
22893 +       au_cpup_attr_nlink(inode, /*force*/0);
22894 +       if (S_ISDIR(inode->i_mode)) {
22895 +               n = inode->i_nlink;
22896 +               n -= nlink;
22897 +               n += st->nlink;
22898 +               smp_mb(); /* for i_nlink */
22899 +               /* 0 can happen */
22900 +               set_nlink(inode, n);
22901 +       }
22902 +
22903 +       spin_lock(&inode->i_lock);
22904 +       inode->i_blocks = st->blocks;
22905 +       i_size_write(inode, st->size);
22906 +       spin_unlock(&inode->i_lock);
22907 +}
22908 +
22909 +/*
22910 + * common routine for aufs_getattr() and au_getxattr().
22911 + * returns zero or negative (an error).
22912 + * @dentry will be read-locked in success.
22913 + */
22914 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22915 +                     struct path *h_path, int locked)
22916 +{
22917 +       int err;
22918 +       unsigned int mnt_flags, sigen;
22919 +       unsigned char udba_none;
22920 +       aufs_bindex_t bindex;
22921 +       struct super_block *sb, *h_sb;
22922 +
22923 +       h_path->mnt = NULL;
22924 +       h_path->dentry = NULL;
22925 +
22926 +       err = 0;
22927 +       sb = dentry->d_sb;
22928 +       mnt_flags = au_mntflags(sb);
22929 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
22930 +
22931 +       if (unlikely(locked))
22932 +               goto body; /* skip locking dinfo */
22933 +
22934 +       /* support fstat(2) */
22935 +       if (!d_unlinked(dentry) && !udba_none) {
22936 +               sigen = au_sigen(sb);
22937 +               err = au_digen_test(dentry, sigen);
22938 +               if (!err) {
22939 +                       di_read_lock_child(dentry, AuLock_IR);
22940 +                       err = au_dbrange_test(dentry);
22941 +                       if (unlikely(err)) {
22942 +                               di_read_unlock(dentry, AuLock_IR);
22943 +                               goto out;
22944 +                       }
22945 +               } else {
22946 +                       AuDebugOn(IS_ROOT(dentry));
22947 +                       di_write_lock_child(dentry);
22948 +                       err = au_dbrange_test(dentry);
22949 +                       if (!err)
22950 +                               err = au_reval_for_attr(dentry, sigen);
22951 +                       if (!err)
22952 +                               di_downgrade_lock(dentry, AuLock_IR);
22953 +                       else {
22954 +                               di_write_unlock(dentry);
22955 +                               goto out;
22956 +                       }
22957 +               }
22958 +       } else
22959 +               di_read_lock_child(dentry, AuLock_IR);
22960 +
22961 +body:
22962 +       if (!inode) {
22963 +               inode = d_inode(dentry);
22964 +               if (unlikely(!inode))
22965 +                       goto out;
22966 +       }
22967 +       bindex = au_ibtop(inode);
22968 +       h_path->mnt = au_sbr_mnt(sb, bindex);
22969 +       h_sb = h_path->mnt->mnt_sb;
22970 +       if (!force
22971 +           && !au_test_fs_bad_iattr(h_sb)
22972 +           && udba_none)
22973 +               goto out; /* success */
22974 +
22975 +       if (au_dbtop(dentry) == bindex)
22976 +               h_path->dentry = au_h_dptr(dentry, bindex);
22977 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
22978 +               h_path->dentry = au_plink_lkup(inode, bindex);
22979 +               if (IS_ERR(h_path->dentry))
22980 +                       /* pretending success */
22981 +                       h_path->dentry = NULL;
22982 +               else
22983 +                       dput(h_path->dentry);
22984 +       }
22985 +
22986 +out:
22987 +       return err;
22988 +}
22989 +
22990 +static int aufs_getattr(struct mnt_idmap *idmap, const struct path *path,
22991 +                       struct kstat *st, u32 request, unsigned int query)
22992 +{
22993 +       int err;
22994 +       unsigned char positive;
22995 +       struct path h_path;
22996 +       struct dentry *dentry;
22997 +       struct inode *inode;
22998 +       struct super_block *sb;
22999 +
23000 +       dentry = path->dentry;
23001 +       inode = d_inode(dentry);
23002 +       sb = dentry->d_sb;
23003 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23004 +       if (unlikely(err))
23005 +               goto out;
23006 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
23007 +                               /*locked*/0);
23008 +       if (unlikely(err))
23009 +               goto out_si;
23010 +       if (unlikely(!h_path.dentry))
23011 +               /* illegally overlapped or something */
23012 +               goto out_fill; /* pretending success */
23013 +
23014 +       positive = d_is_positive(h_path.dentry);
23015 +       if (positive)
23016 +               /* no vfsub version */
23017 +               err = vfs_getattr(&h_path, st, request, query);
23018 +       if (!err) {
23019 +               if (positive)
23020 +                       au_refresh_iattr(inode, st,
23021 +                                        d_inode(h_path.dentry)->i_nlink);
23022 +               goto out_fill; /* success */
23023 +       }
23024 +       AuTraceErr(err);
23025 +       goto out_di;
23026 +
23027 +out_fill:
23028 +       generic_fillattr(idmap, request, inode, st);
23029 +out_di:
23030 +       di_read_unlock(dentry, AuLock_IR);
23031 +out_si:
23032 +       si_read_unlock(sb);
23033 +out:
23034 +       AuTraceErr(err);
23035 +       return err;
23036 +}
23037 +
23038 +/* ---------------------------------------------------------------------- */
23039 +
23040 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
23041 +                                struct delayed_call *done)
23042 +{
23043 +       const char *ret;
23044 +       struct dentry *h_dentry;
23045 +       struct inode *h_inode;
23046 +       int err;
23047 +       aufs_bindex_t bindex;
23048 +
23049 +       ret = NULL; /* suppress a warning */
23050 +       err = -ECHILD;
23051 +       if (!dentry)
23052 +               goto out;
23053 +
23054 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23055 +       if (unlikely(err))
23056 +               goto out;
23057 +
23058 +       err = au_d_hashed_positive(dentry);
23059 +       if (unlikely(err))
23060 +               goto out_unlock;
23061 +
23062 +       err = -EINVAL;
23063 +       inode = d_inode(dentry);
23064 +       bindex = au_ibtop(inode);
23065 +       h_inode = au_h_iptr(inode, bindex);
23066 +       if (unlikely(!h_inode->i_op->get_link))
23067 +               goto out_unlock;
23068 +
23069 +       err = -EBUSY;
23070 +       h_dentry = NULL;
23071 +       if (au_dbtop(dentry) <= bindex) {
23072 +               h_dentry = au_h_dptr(dentry, bindex);
23073 +               if (h_dentry)
23074 +                       dget(h_dentry);
23075 +       }
23076 +       if (!h_dentry) {
23077 +               h_dentry = d_find_any_alias(h_inode);
23078 +               if (IS_ERR(h_dentry)) {
23079 +                       err = PTR_ERR(h_dentry);
23080 +                       goto out_unlock;
23081 +               }
23082 +       }
23083 +       if (unlikely(!h_dentry))
23084 +               goto out_unlock;
23085 +
23086 +       err = 0;
23087 +       AuDbg("%ps\n", h_inode->i_op->get_link);
23088 +       AuDbgDentry(h_dentry);
23089 +       ret = vfs_get_link(h_dentry, done);
23090 +       dput(h_dentry);
23091 +       if (IS_ERR(ret))
23092 +               err = PTR_ERR(ret);
23093 +
23094 +out_unlock:
23095 +       aufs_read_unlock(dentry, AuLock_IR);
23096 +out:
23097 +       if (unlikely(err))
23098 +               ret = ERR_PTR(err);
23099 +       AuTraceErrPtr(ret);
23100 +       return ret;
23101 +}
23102 +
23103 +/* ---------------------------------------------------------------------- */
23104 +
23105 +static int au_is_special(struct inode *inode)
23106 +{
23107 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23108 +}
23109 +
23110 +static int aufs_update_time(struct inode *inode, int flags)
23111 +{
23112 +       int err;
23113 +       aufs_bindex_t bindex;
23114 +       struct super_block *sb;
23115 +       struct inode *h_inode;
23116 +       struct vfsmount *h_mnt;
23117 +
23118 +       sb = inode->i_sb;
23119 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23120 +                 "unexpected s_flags 0x%lx", sb->s_flags);
23121 +
23122 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
23123 +       lockdep_off();
23124 +       si_read_lock(sb, AuLock_FLUSH);
23125 +       ii_write_lock_child(inode);
23126 +
23127 +       err = 0;
23128 +       bindex = au_ibtop(inode);
23129 +       h_inode = au_h_iptr(inode, bindex);
23130 +       if (!au_test_ro(sb, bindex, inode)) {
23131 +               h_mnt = au_sbr_mnt(sb, bindex);
23132 +               err = vfsub_mnt_want_write(h_mnt);
23133 +               if (!err) {
23134 +                       err = vfsub_update_time(h_inode, flags);
23135 +                       vfsub_mnt_drop_write(h_mnt);
23136 +               }
23137 +       } else if (au_is_special(h_inode)) {
23138 +               /*
23139 +                * Never copy-up here.
23140 +                * These special files may already be opened and used for
23141 +                * communicating. If we copied it up, then the communication
23142 +                * would be corrupted.
23143 +                */
23144 +               AuWarn1("timestamps for i%lu are ignored "
23145 +                       "since it is on readonly branch (hi%lu).\n",
23146 +                       inode->i_ino, h_inode->i_ino);
23147 +       } else if (flags & ~S_ATIME) {
23148 +               err = -EIO;
23149 +               AuIOErr1("unexpected flags 0x%x\n", flags);
23150 +               AuDebugOn(1);
23151 +       }
23152 +
23153 +       if (!err)
23154 +               au_cpup_attr_timesizes(inode);
23155 +       ii_write_unlock(inode);
23156 +       si_read_unlock(sb);
23157 +       lockdep_on();
23158 +
23159 +       if (!err && (flags & S_VERSION))
23160 +               inode_inc_iversion(inode);
23161 +
23162 +       return err;
23163 +}
23164 +
23165 +/* ---------------------------------------------------------------------- */
23166 +
23167 +/* no getattr version will be set by module.c:aufs_init() */
23168 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23169 +       aufs_iop[] = {
23170 +       [AuIop_SYMLINK] = {
23171 +               .permission     = aufs_permission,
23172 +#ifdef CONFIG_FS_POSIX_ACL
23173 +               .get_inode_acl  = aufs_get_inode_acl,
23174 +               .get_acl        = aufs_get_acl,
23175 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
23176 +#endif
23177 +
23178 +               .setattr        = aufs_setattr,
23179 +               .getattr        = aufs_getattr,
23180 +
23181 +#ifdef CONFIG_AUFS_XATTR
23182 +               .listxattr      = aufs_listxattr,
23183 +#endif
23184 +
23185 +               .get_link       = aufs_get_link
23186 +
23187 +               /* .update_time = aufs_update_time */
23188 +       },
23189 +       [AuIop_DIR] = {
23190 +               .create         = aufs_create,
23191 +               .lookup         = aufs_lookup,
23192 +               .link           = aufs_link,
23193 +               .unlink         = aufs_unlink,
23194 +               .symlink        = aufs_symlink,
23195 +               .mkdir          = aufs_mkdir,
23196 +               .rmdir          = aufs_rmdir,
23197 +               .mknod          = aufs_mknod,
23198 +               .rename         = aufs_rename,
23199 +
23200 +               .permission     = aufs_permission,
23201 +#ifdef CONFIG_FS_POSIX_ACL
23202 +               .get_inode_acl  = aufs_get_inode_acl,
23203 +               .get_acl        = aufs_get_acl,
23204 +               .set_acl        = aufs_set_acl,
23205 +#endif
23206 +
23207 +               .setattr        = aufs_setattr,
23208 +               .getattr        = aufs_getattr,
23209 +
23210 +#ifdef CONFIG_AUFS_XATTR
23211 +               .listxattr      = aufs_listxattr,
23212 +#endif
23213 +
23214 +               .update_time    = aufs_update_time,
23215 +               .atomic_open    = aufs_atomic_open,
23216 +               .tmpfile        = aufs_tmpfile
23217 +       },
23218 +       [AuIop_OTHER] = {
23219 +               .permission     = aufs_permission,
23220 +#ifdef CONFIG_FS_POSIX_ACL
23221 +               .get_inode_acl  = aufs_get_inode_acl,
23222 +               .get_acl        = aufs_get_acl,
23223 +               .set_acl        = aufs_set_acl,
23224 +#endif
23225 +
23226 +               .setattr        = aufs_setattr,
23227 +               .getattr        = aufs_getattr,
23228 +
23229 +#ifdef CONFIG_AUFS_XATTR
23230 +               .listxattr      = aufs_listxattr,
23231 +#endif
23232 +
23233 +               .update_time    = aufs_update_time
23234 +       }
23235 +};
23236 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
23237 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
23238 +++ linux/fs/aufs/i_op_del.c    2023-10-31 09:31:04.199880750 +0100
23239 @@ -0,0 +1,523 @@
23240 +// SPDX-License-Identifier: GPL-2.0
23241 +/*
23242 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23243 + *
23244 + * This program is free software; you can redistribute it and/or modify
23245 + * it under the terms of the GNU General Public License as published by
23246 + * the Free Software Foundation; either version 2 of the License, or
23247 + * (at your option) any later version.
23248 + *
23249 + * This program is distributed in the hope that it will be useful,
23250 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23251 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23252 + * GNU General Public License for more details.
23253 + *
23254 + * You should have received a copy of the GNU General Public License
23255 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23256 + */
23257 +
23258 +/*
23259 + * inode operations (del entry)
23260 + */
23261 +
23262 +#include <linux/iversion.h>
23263 +#include "aufs.h"
23264 +
23265 +/*
23266 + * decide if a new whiteout for @dentry is necessary or not.
23267 + * when it is necessary, prepare the parent dir for the upper branch whose
23268 + * branch index is @bcpup for creation. the actual creation of the whiteout will
23269 + * be done by caller.
23270 + * return value:
23271 + * 0: wh is unnecessary
23272 + * plus: wh is necessary
23273 + * minus: error
23274 + */
23275 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
23276 +{
23277 +       int need_wh, err;
23278 +       aufs_bindex_t btop;
23279 +       struct super_block *sb;
23280 +
23281 +       sb = dentry->d_sb;
23282 +       btop = au_dbtop(dentry);
23283 +       if (*bcpup < 0) {
23284 +               *bcpup = btop;
23285 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
23286 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
23287 +                       *bcpup = err;
23288 +                       if (unlikely(err < 0))
23289 +                               goto out;
23290 +               }
23291 +       } else
23292 +               AuDebugOn(btop < *bcpup
23293 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
23294 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
23295 +
23296 +       if (*bcpup != btop) {
23297 +               err = au_cpup_dirs(dentry, *bcpup);
23298 +               if (unlikely(err))
23299 +                       goto out;
23300 +               need_wh = 1;
23301 +       } else {
23302 +               struct au_dinfo *dinfo, *tmp;
23303 +
23304 +               need_wh = -ENOMEM;
23305 +               dinfo = au_di(dentry);
23306 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23307 +               if (tmp) {
23308 +                       au_di_cp(tmp, dinfo);
23309 +                       au_di_swap(tmp, dinfo);
23310 +                       /* returns the number of positive dentries */
23311 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
23312 +                                                /* AuLkup_IGNORE_PERM */ 0);
23313 +                       au_di_swap(tmp, dinfo);
23314 +                       au_rw_write_unlock(&tmp->di_rwsem);
23315 +                       au_di_free(tmp);
23316 +               }
23317 +       }
23318 +       AuDbg("need_wh %d\n", need_wh);
23319 +       err = need_wh;
23320 +
23321 +out:
23322 +       return err;
23323 +}
23324 +
23325 +/*
23326 + * simple tests for the del-entry operations.
23327 + * following the checks in vfs, plus the parent-child relationship.
23328 + */
23329 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23330 +              struct dentry *h_parent, int isdir)
23331 +{
23332 +       int err;
23333 +       umode_t h_mode;
23334 +       struct dentry *h_dentry, *h_latest;
23335 +       struct inode *h_inode;
23336 +       struct path h_ppath;
23337 +       struct super_block *sb;
23338 +       struct au_branch *br;
23339 +       struct mnt_idmap *h_idmap;
23340 +
23341 +       h_dentry = au_h_dptr(dentry, bindex);
23342 +       if (d_really_is_positive(dentry)) {
23343 +               err = -ENOENT;
23344 +               if (unlikely(d_is_negative(h_dentry)))
23345 +                       goto out;
23346 +               h_inode = d_inode(h_dentry);
23347 +               if (unlikely(!h_inode->i_nlink))
23348 +                       goto out;
23349 +
23350 +               h_mode = h_inode->i_mode;
23351 +               if (!isdir) {
23352 +                       err = -EISDIR;
23353 +                       if (unlikely(S_ISDIR(h_mode)))
23354 +                               goto out;
23355 +               } else if (unlikely(!S_ISDIR(h_mode))) {
23356 +                       err = -ENOTDIR;
23357 +                       goto out;
23358 +               }
23359 +       } else {
23360 +               /* rename(2) case */
23361 +               err = -EIO;
23362 +               if (unlikely(d_is_positive(h_dentry)))
23363 +                       goto out;
23364 +       }
23365 +
23366 +       err = -ENOENT;
23367 +       /* expected parent dir is locked */
23368 +       if (unlikely(h_parent != h_dentry->d_parent))
23369 +               goto out;
23370 +       err = 0;
23371 +
23372 +       /*
23373 +        * rmdir a dir may break the consistency on some filesystem.
23374 +        * let's try heavy test.
23375 +        */
23376 +       err = -EACCES;
23377 +       sb = dentry->d_sb;
23378 +       br = au_sbr(sb, bindex);
23379 +       h_idmap = au_br_idmap(br);
23380 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
23381 +                    && au_test_h_perm(h_idmap, d_inode(h_parent),
23382 +                                      MAY_EXEC | MAY_WRITE)))
23383 +               goto out;
23384 +
23385 +       h_ppath.dentry = h_parent;
23386 +       h_ppath.mnt = au_br_mnt(br);
23387 +       h_latest = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
23388 +       err = -EIO;
23389 +       if (IS_ERR(h_latest))
23390 +               goto out;
23391 +       if (h_latest == h_dentry)
23392 +               err = 0;
23393 +       dput(h_latest);
23394 +
23395 +out:
23396 +       return err;
23397 +}
23398 +
23399 +/*
23400 + * decide the branch where we operate for @dentry. the branch index will be set
23401 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
23402 + * dir for reverting.
23403 + * when a new whiteout is necessary, create it.
23404 + */
23405 +static struct dentry*
23406 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23407 +                   struct au_dtime *dt, struct au_pin *pin)
23408 +{
23409 +       struct dentry *wh_dentry;
23410 +       struct super_block *sb;
23411 +       struct path h_path;
23412 +       int err, need_wh;
23413 +       unsigned int udba;
23414 +       aufs_bindex_t bcpup;
23415 +
23416 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23417 +       wh_dentry = ERR_PTR(need_wh);
23418 +       if (unlikely(need_wh < 0))
23419 +               goto out;
23420 +
23421 +       sb = dentry->d_sb;
23422 +       udba = au_opt_udba(sb);
23423 +       bcpup = *rbcpup;
23424 +       err = au_pin(pin, dentry, bcpup, udba,
23425 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23426 +       wh_dentry = ERR_PTR(err);
23427 +       if (unlikely(err))
23428 +               goto out;
23429 +
23430 +       h_path.dentry = au_pinned_h_parent(pin);
23431 +       if (udba != AuOpt_UDBA_NONE
23432 +           && au_dbtop(dentry) == bcpup) {
23433 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23434 +               wh_dentry = ERR_PTR(err);
23435 +               if (unlikely(err))
23436 +                       goto out_unpin;
23437 +       }
23438 +
23439 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
23440 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23441 +       wh_dentry = NULL;
23442 +       if (!need_wh)
23443 +               goto out; /* success, no need to create whiteout */
23444 +
23445 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23446 +       if (IS_ERR(wh_dentry))
23447 +               goto out_unpin;
23448 +
23449 +       /* returns with the parent is locked and wh_dentry is dget-ed */
23450 +       goto out; /* success */
23451 +
23452 +out_unpin:
23453 +       au_unpin(pin);
23454 +out:
23455 +       return wh_dentry;
23456 +}
23457 +
23458 +/*
23459 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
23460 + * in order to be revertible and save time for removing many child whiteouts
23461 + * under the dir.
23462 + * returns 1 when there are too many child whiteout and caller should remove
23463 + * them asynchronously. returns 0 when the number of children is enough small to
23464 + * remove now or the branch fs is a remote fs.
23465 + * otherwise return an error.
23466 + */
23467 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23468 +                          struct au_nhash *whlist, struct inode *dir)
23469 +{
23470 +       int rmdir_later, err, dirwh;
23471 +       struct dentry *h_dentry;
23472 +       struct super_block *sb;
23473 +       struct inode *inode;
23474 +
23475 +       sb = dentry->d_sb;
23476 +       SiMustAnyLock(sb);
23477 +       h_dentry = au_h_dptr(dentry, bindex);
23478 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23479 +       if (unlikely(err))
23480 +               goto out;
23481 +
23482 +       /* stop monitoring */
23483 +       inode = d_inode(dentry);
23484 +       au_hn_free(au_hi(inode, bindex));
23485 +
23486 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
23487 +               dirwh = au_sbi(sb)->si_dirwh;
23488 +               rmdir_later = (dirwh <= 1);
23489 +               if (!rmdir_later)
23490 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23491 +                                                             dirwh);
23492 +               if (rmdir_later)
23493 +                       return rmdir_later;
23494 +       }
23495 +
23496 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23497 +       if (unlikely(err)) {
23498 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23499 +                       h_dentry, bindex, err);
23500 +               err = 0;
23501 +       }
23502 +
23503 +out:
23504 +       AuTraceErr(err);
23505 +       return err;
23506 +}
23507 +
23508 +/*
23509 + * final procedure for deleting a entry.
23510 + * maintain dentry and iattr.
23511 + */
23512 +static void epilog(struct inode *dir, struct dentry *dentry,
23513 +                  aufs_bindex_t bindex)
23514 +{
23515 +       struct inode *inode;
23516 +
23517 +       inode = d_inode(dentry);
23518 +       d_drop(dentry);
23519 +       inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
23520 +
23521 +       au_dir_ts(dir, bindex);
23522 +       inode_inc_iversion(dir);
23523 +}
23524 +
23525 +/*
23526 + * when an error happened, remove the created whiteout and revert everything.
23527 + */
23528 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23529 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
23530 +                    struct dentry *dentry, struct au_dtime *dt)
23531 +{
23532 +       int rerr;
23533 +       struct path h_path = {
23534 +               .dentry = wh_dentry,
23535 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
23536 +       };
23537 +
23538 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
23539 +       if (!rerr) {
23540 +               au_set_dbwh(dentry, bwh);
23541 +               au_dtime_revert(dt);
23542 +               return 0;
23543 +       }
23544 +
23545 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
23546 +       return -EIO;
23547 +}
23548 +
23549 +/* ---------------------------------------------------------------------- */
23550 +
23551 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
23552 +{
23553 +       int err;
23554 +       aufs_bindex_t bwh, bindex, btop;
23555 +       struct inode *inode, *h_dir, *delegated, *h_inode;
23556 +       struct dentry *parent, *wh_dentry;
23557 +       /* to reduce stack size */
23558 +       struct {
23559 +               struct au_dtime dt;
23560 +               struct au_pin pin;
23561 +               struct path h_path;
23562 +       } *a;
23563 +
23564 +       IMustLock(dir);
23565 +
23566 +       err = -ENOMEM;
23567 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23568 +       if (unlikely(!a))
23569 +               goto out;
23570 +
23571 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23572 +       if (unlikely(err))
23573 +               goto out_free;
23574 +       err = au_d_hashed_positive(dentry);
23575 +       if (unlikely(err))
23576 +               goto out_unlock;
23577 +       inode = d_inode(dentry);
23578 +       IMustLock(inode);
23579 +       err = -EISDIR;
23580 +       if (unlikely(d_is_dir(dentry)))
23581 +               goto out_unlock; /* possible? */
23582 +
23583 +       btop = au_dbtop(dentry);
23584 +       bwh = au_dbwh(dentry);
23585 +       bindex = -1;
23586 +       parent = dentry->d_parent; /* dir inode is locked */
23587 +       di_write_lock_parent(parent);
23588 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23589 +                                       &a->pin);
23590 +       err = PTR_ERR(wh_dentry);
23591 +       if (IS_ERR(wh_dentry))
23592 +               goto out_parent;
23593 +
23594 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23595 +       a->h_path.dentry = au_h_dptr(dentry, btop);
23596 +       dget(a->h_path.dentry);
23597 +       if (bindex == btop) {
23598 +               h_dir = au_pinned_h_dir(&a->pin);
23599 +               delegated = NULL;
23600 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23601 +               if (unlikely(err == -EWOULDBLOCK)) {
23602 +                       pr_warn("cannot retry for NFSv4 delegation"
23603 +                               " for an internal unlink\n");
23604 +                       iput(delegated);
23605 +               }
23606 +       } else {
23607 +               /* dir inode is locked */
23608 +               h_dir = d_inode(wh_dentry->d_parent);
23609 +               IMustLock(h_dir);
23610 +               err = 0;
23611 +       }
23612 +
23613 +       if (!err) {
23614 +               vfsub_drop_nlink(inode);
23615 +               epilog(dir, dentry, bindex);
23616 +
23617 +               /* update target timestamps */
23618 +               if (bindex == btop) {
23619 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23620 +                       /*ignore*/
23621 +                       h_inode = d_inode(a->h_path.dentry);
23622 +                       inode_set_ctime_to_ts(inode, inode_get_ctime(h_inode));
23623 +               } else
23624 +                       /* todo: this timestamp may be reverted later */
23625 +                       inode_set_ctime_to_ts(inode, inode_get_ctime(h_dir));
23626 +               goto out_unpin; /* success */
23627 +       }
23628 +
23629 +       /* revert */
23630 +       if (wh_dentry) {
23631 +               int rerr;
23632 +
23633 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23634 +                                &a->dt);
23635 +               if (rerr)
23636 +                       err = rerr;
23637 +       }
23638 +
23639 +out_unpin:
23640 +       au_unpin(&a->pin);
23641 +       dput(wh_dentry);
23642 +       dput(a->h_path.dentry);
23643 +out_parent:
23644 +       di_write_unlock(parent);
23645 +out_unlock:
23646 +       aufs_read_unlock(dentry, AuLock_DW);
23647 +out_free:
23648 +       au_kfree_rcu(a);
23649 +out:
23650 +       return err;
23651 +}
23652 +
23653 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
23654 +{
23655 +       int err, rmdir_later;
23656 +       aufs_bindex_t bwh, bindex, btop;
23657 +       struct inode *inode;
23658 +       struct dentry *parent, *wh_dentry, *h_dentry;
23659 +       struct au_whtmp_rmdir *args;
23660 +       /* to reduce stack size */
23661 +       struct {
23662 +               struct au_dtime dt;
23663 +               struct au_pin pin;
23664 +       } *a;
23665 +
23666 +       IMustLock(dir);
23667 +
23668 +       err = -ENOMEM;
23669 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23670 +       if (unlikely(!a))
23671 +               goto out;
23672 +
23673 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23674 +       if (unlikely(err))
23675 +               goto out_free;
23676 +       err = au_alive_dir(dentry);
23677 +       if (unlikely(err))
23678 +               goto out_unlock;
23679 +       inode = d_inode(dentry);
23680 +       IMustLock(inode);
23681 +       err = -ENOTDIR;
23682 +       if (unlikely(!d_is_dir(dentry)))
23683 +               goto out_unlock; /* possible? */
23684 +
23685 +       err = -ENOMEM;
23686 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23687 +       if (unlikely(!args))
23688 +               goto out_unlock;
23689 +
23690 +       parent = dentry->d_parent; /* dir inode is locked */
23691 +       di_write_lock_parent(parent);
23692 +       err = au_test_empty(dentry, &args->whlist);
23693 +       if (unlikely(err))
23694 +               goto out_parent;
23695 +
23696 +       btop = au_dbtop(dentry);
23697 +       bwh = au_dbwh(dentry);
23698 +       bindex = -1;
23699 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23700 +                                       &a->pin);
23701 +       err = PTR_ERR(wh_dentry);
23702 +       if (IS_ERR(wh_dentry))
23703 +               goto out_parent;
23704 +
23705 +       h_dentry = au_h_dptr(dentry, btop);
23706 +       dget(h_dentry);
23707 +       rmdir_later = 0;
23708 +       if (bindex == btop) {
23709 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
23710 +               if (err > 0) {
23711 +                       rmdir_later = err;
23712 +                       err = 0;
23713 +               }
23714 +       } else {
23715 +               /* stop monitoring */
23716 +               au_hn_free(au_hi(inode, btop));
23717 +
23718 +               /* dir inode is locked */
23719 +               IMustLock(d_inode(wh_dentry->d_parent));
23720 +               err = 0;
23721 +       }
23722 +
23723 +       if (!err) {
23724 +               vfsub_dead_dir(inode);
23725 +               au_set_dbdiropq(dentry, -1);
23726 +               epilog(dir, dentry, bindex);
23727 +
23728 +               if (rmdir_later) {
23729 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
23730 +                       args = NULL;
23731 +               }
23732 +
23733 +               goto out_unpin; /* success */
23734 +       }
23735 +
23736 +       /* revert */
23737 +       AuLabel(revert);
23738 +       if (wh_dentry) {
23739 +               int rerr;
23740 +
23741 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23742 +                                &a->dt);
23743 +               if (rerr)
23744 +                       err = rerr;
23745 +       }
23746 +
23747 +out_unpin:
23748 +       au_unpin(&a->pin);
23749 +       dput(wh_dentry);
23750 +       dput(h_dentry);
23751 +out_parent:
23752 +       di_write_unlock(parent);
23753 +       if (args)
23754 +               au_whtmp_rmdir_free(args);
23755 +out_unlock:
23756 +       aufs_read_unlock(dentry, AuLock_DW);
23757 +out_free:
23758 +       au_kfree_rcu(a);
23759 +out:
23760 +       AuTraceErr(err);
23761 +       return err;
23762 +}
23763 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
23764 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
23765 +++ linux/fs/aufs/i_op_ren.c    2023-10-31 09:31:04.199880750 +0100
23766 @@ -0,0 +1,1260 @@
23767 +// SPDX-License-Identifier: GPL-2.0
23768 +/*
23769 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23770 + *
23771 + * This program is free software; you can redistribute it and/or modify
23772 + * it under the terms of the GNU General Public License as published by
23773 + * the Free Software Foundation; either version 2 of the License, or
23774 + * (at your option) any later version.
23775 + *
23776 + * This program is distributed in the hope that it will be useful,
23777 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23778 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23779 + * GNU General Public License for more details.
23780 + *
23781 + * You should have received a copy of the GNU General Public License
23782 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23783 + */
23784 +
23785 +/*
23786 + * inode operation (rename entry)
23787 + * todo: this is crazy monster
23788 + */
23789 +
23790 +#include <linux/iversion.h>
23791 +#include "aufs.h"
23792 +
23793 +enum { AuSRC, AuDST, AuSrcDst };
23794 +enum { AuPARENT, AuCHILD, AuParentChild };
23795 +
23796 +#define AuRen_ISDIR_SRC                1
23797 +#define AuRen_ISDIR_DST                (1 << 1)
23798 +#define AuRen_ISSAMEDIR                (1 << 2)
23799 +#define AuRen_WHSRC            (1 << 3)
23800 +#define AuRen_WHDST            (1 << 4)
23801 +#define AuRen_MNT_WRITE                (1 << 5)
23802 +#define AuRen_DT_DSTDIR                (1 << 6)
23803 +#define AuRen_DIROPQ_SRC       (1 << 7)
23804 +#define AuRen_DIROPQ_DST       (1 << 8)
23805 +#define AuRen_DIRREN           (1 << 9)
23806 +#define AuRen_DROPPED_SRC      (1 << 10)
23807 +#define AuRen_DROPPED_DST      (1 << 11)
23808 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
23809 +#define au_fset_ren(flags, name) \
23810 +       do { (flags) |= AuRen_##name; } while (0)
23811 +#define au_fclr_ren(flags, name) \
23812 +       do { (flags) &= ~AuRen_##name; } while (0)
23813 +
23814 +#ifndef CONFIG_AUFS_DIRREN
23815 +#undef AuRen_DIRREN
23816 +#define AuRen_DIRREN           0
23817 +#endif
23818 +
23819 +struct au_ren_args {
23820 +       struct {
23821 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
23822 +                       *wh_dentry;
23823 +               struct inode *dir, *inode;
23824 +               struct au_hinode *hdir, *hinode;
23825 +               struct au_dtime dt[AuParentChild];
23826 +               aufs_bindex_t btop, bdiropq;
23827 +       } sd[AuSrcDst];
23828 +
23829 +#define src_dentry     sd[AuSRC].dentry
23830 +#define src_dir                sd[AuSRC].dir
23831 +#define src_inode      sd[AuSRC].inode
23832 +#define src_h_dentry   sd[AuSRC].h_dentry
23833 +#define src_parent     sd[AuSRC].parent
23834 +#define src_h_parent   sd[AuSRC].h_parent
23835 +#define src_wh_dentry  sd[AuSRC].wh_dentry
23836 +#define src_hdir       sd[AuSRC].hdir
23837 +#define src_hinode     sd[AuSRC].hinode
23838 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
23839 +#define src_dt         sd[AuSRC].dt
23840 +#define src_btop       sd[AuSRC].btop
23841 +#define src_bdiropq    sd[AuSRC].bdiropq
23842 +
23843 +#define dst_dentry     sd[AuDST].dentry
23844 +#define dst_dir                sd[AuDST].dir
23845 +#define dst_inode      sd[AuDST].inode
23846 +#define dst_h_dentry   sd[AuDST].h_dentry
23847 +#define dst_parent     sd[AuDST].parent
23848 +#define dst_h_parent   sd[AuDST].h_parent
23849 +#define dst_wh_dentry  sd[AuDST].wh_dentry
23850 +#define dst_hdir       sd[AuDST].hdir
23851 +#define dst_hinode     sd[AuDST].hinode
23852 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
23853 +#define dst_dt         sd[AuDST].dt
23854 +#define dst_btop       sd[AuDST].btop
23855 +#define dst_bdiropq    sd[AuDST].bdiropq
23856 +
23857 +       struct dentry *h_trap;
23858 +       struct au_branch *br;
23859 +       struct path h_path;
23860 +       struct au_nhash whlist;
23861 +       aufs_bindex_t btgt, src_bwh;
23862 +
23863 +       struct {
23864 +               unsigned short auren_flags;
23865 +               unsigned char flags;    /* syscall parameter */
23866 +               unsigned char exchange;
23867 +       } __packed;
23868 +
23869 +       struct au_whtmp_rmdir *thargs;
23870 +       struct dentry *h_dst;
23871 +       struct au_hinode *h_root;
23872 +};
23873 +
23874 +/* ---------------------------------------------------------------------- */
23875 +
23876 +/*
23877 + * functions for reverting.
23878 + * when an error happened in a single rename systemcall, we should revert
23879 + * everything as if nothing happened.
23880 + * we don't need to revert the copied-up/down the parent dir since they are
23881 + * harmless.
23882 + */
23883 +
23884 +#define RevertFailure(fmt, ...) do { \
23885 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23886 +               ##__VA_ARGS__, err, rerr); \
23887 +       err = -EIO; \
23888 +} while (0)
23889 +
23890 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
23891 +{
23892 +       int rerr;
23893 +       struct dentry *d;
23894 +#define src_or_dst(member) a->sd[idx].member
23895 +
23896 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
23897 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23898 +       rerr = au_diropq_remove(d, a->btgt);
23899 +       au_hn_inode_unlock(src_or_dst(hinode));
23900 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
23901 +       if (rerr)
23902 +               RevertFailure("remove diropq %pd", d);
23903 +
23904 +#undef src_or_dst_
23905 +}
23906 +
23907 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23908 +{
23909 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23910 +               au_ren_do_rev_diropq(err, a, AuSRC);
23911 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23912 +               au_ren_do_rev_diropq(err, a, AuDST);
23913 +}
23914 +
23915 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
23916 +{
23917 +       int rerr;
23918 +       struct inode *delegated;
23919 +       struct path h_ppath = {
23920 +               .dentry = a->src_h_parent,
23921 +               .mnt    = a->h_path.mnt
23922 +       };
23923 +
23924 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
23925 +       rerr = PTR_ERR(a->h_path.dentry);
23926 +       if (IS_ERR(a->h_path.dentry)) {
23927 +               RevertFailure("lkup one %pd", a->src_dentry);
23928 +               return;
23929 +       }
23930 +
23931 +       delegated = NULL;
23932 +       rerr = vfsub_rename(a->dst_h_dir,
23933 +                           au_h_dptr(a->src_dentry, a->btgt),
23934 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
23935 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23936 +               pr_warn("cannot retry for NFSv4 delegation"
23937 +                       " for an internal rename\n");
23938 +               iput(delegated);
23939 +       }
23940 +       d_drop(a->h_path.dentry);
23941 +       dput(a->h_path.dentry);
23942 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23943 +       if (rerr)
23944 +               RevertFailure("rename %pd", a->src_dentry);
23945 +}
23946 +
23947 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
23948 +{
23949 +       int rerr;
23950 +       struct inode *delegated;
23951 +       struct path h_ppath = {
23952 +               .dentry = a->dst_h_parent,
23953 +               .mnt    = a->h_path.mnt
23954 +       };
23955 +
23956 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
23957 +       rerr = PTR_ERR(a->h_path.dentry);
23958 +       if (IS_ERR(a->h_path.dentry)) {
23959 +               RevertFailure("lkup one %pd", a->dst_dentry);
23960 +               return;
23961 +       }
23962 +       if (d_is_positive(a->h_path.dentry)) {
23963 +               d_drop(a->h_path.dentry);
23964 +               dput(a->h_path.dentry);
23965 +               return;
23966 +       }
23967 +
23968 +       delegated = NULL;
23969 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
23970 +                           &delegated, a->flags);
23971 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23972 +               pr_warn("cannot retry for NFSv4 delegation"
23973 +                       " for an internal rename\n");
23974 +               iput(delegated);
23975 +       }
23976 +       d_drop(a->h_path.dentry);
23977 +       dput(a->h_path.dentry);
23978 +       if (!rerr)
23979 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
23980 +       else
23981 +               RevertFailure("rename %pd", a->h_dst);
23982 +}
23983 +
23984 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
23985 +{
23986 +       int rerr;
23987 +
23988 +       a->h_path.dentry = a->src_wh_dentry;
23989 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
23990 +       au_set_dbwh(a->src_dentry, a->src_bwh);
23991 +       if (rerr)
23992 +               RevertFailure("unlink %pd", a->src_wh_dentry);
23993 +}
23994 +#undef RevertFailure
23995 +
23996 +/* ---------------------------------------------------------------------- */
23997 +
23998 +/*
23999 + * when we have to copyup the renaming entry, do it with the rename-target name
24000 + * in order to minimize the cost (the later actual rename is unnecessary).
24001 + * otherwise rename it on the target branch.
24002 + */
24003 +static int au_ren_or_cpup(struct au_ren_args *a)
24004 +{
24005 +       int err;
24006 +       struct dentry *d;
24007 +       struct inode *delegated;
24008 +
24009 +       d = a->src_dentry;
24010 +       if (au_dbtop(d) == a->btgt) {
24011 +               a->h_path.dentry = a->dst_h_dentry;
24012 +               AuDebugOn(au_dbtop(d) != a->btgt);
24013 +               delegated = NULL;
24014 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
24015 +                                  a->dst_h_dir, &a->h_path, &delegated,
24016 +                                  a->flags);
24017 +               if (unlikely(err == -EWOULDBLOCK)) {
24018 +                       pr_warn("cannot retry for NFSv4 delegation"
24019 +                               " for an internal rename\n");
24020 +                       iput(delegated);
24021 +               }
24022 +       } else
24023 +               BUG();
24024 +
24025 +       if (!err && a->h_dst)
24026 +               /* it will be set to dinfo later */
24027 +               dget(a->h_dst);
24028 +
24029 +       return err;
24030 +}
24031 +
24032 +/* cf. aufs_rmdir() */
24033 +static int au_ren_del_whtmp(struct au_ren_args *a)
24034 +{
24035 +       int err;
24036 +       struct inode *dir;
24037 +
24038 +       dir = a->dst_dir;
24039 +       SiMustAnyLock(dir->i_sb);
24040 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
24041 +                                    au_sbi(dir->i_sb)->si_dirwh)
24042 +           || au_test_fs_remote(a->h_dst->d_sb)) {
24043 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
24044 +               if (unlikely(err))
24045 +                       pr_warn("failed removing whtmp dir %pd (%d), "
24046 +                               "ignored.\n", a->h_dst, err);
24047 +       } else {
24048 +               au_nhash_wh_free(&a->thargs->whlist);
24049 +               a->thargs->whlist = a->whlist;
24050 +               a->whlist.nh_num = 0;
24051 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24052 +               dput(a->h_dst);
24053 +               a->thargs = NULL;
24054 +       }
24055 +
24056 +       return 0;
24057 +}
24058 +
24059 +/* make it 'opaque' dir. */
24060 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
24061 +{
24062 +       int err;
24063 +       struct dentry *d, *diropq;
24064 +#define src_or_dst(member) a->sd[idx].member
24065 +
24066 +       err = 0;
24067 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
24068 +       src_or_dst(bdiropq) = au_dbdiropq(d);
24069 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24070 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24071 +       diropq = au_diropq_create(d, a->btgt);
24072 +       au_hn_inode_unlock(src_or_dst(hinode));
24073 +       if (IS_ERR(diropq))
24074 +               err = PTR_ERR(diropq);
24075 +       else
24076 +               dput(diropq);
24077 +
24078 +#undef src_or_dst_
24079 +       return err;
24080 +}
24081 +
24082 +static int au_ren_diropq(struct au_ren_args *a)
24083 +{
24084 +       int err;
24085 +       unsigned char always;
24086 +       struct dentry *d;
24087 +
24088 +       err = 0;
24089 +       d = a->dst_dentry; /* already renamed on the branch */
24090 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24091 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24092 +           && !au_ftest_ren(a->auren_flags, DIRREN)
24093 +           && a->btgt != au_dbdiropq(a->src_dentry)
24094 +           && (a->dst_wh_dentry
24095 +               || a->btgt <= au_dbdiropq(d)
24096 +               /* hide the lower to keep xino */
24097 +               /* the lowers may not be a dir, but we hide them anyway */
24098 +               || a->btgt < au_dbbot(d)
24099 +               || always)) {
24100 +               AuDbg("here\n");
24101 +               err = au_ren_do_diropq(a, AuSRC);
24102 +               if (unlikely(err))
24103 +                       goto out;
24104 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
24105 +       }
24106 +       if (!a->exchange)
24107 +               goto out; /* success */
24108 +
24109 +       d = a->src_dentry; /* already renamed on the branch */
24110 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24111 +           && a->btgt != au_dbdiropq(a->dst_dentry)
24112 +           && (a->btgt < au_dbdiropq(d)
24113 +               || a->btgt < au_dbbot(d)
24114 +               || always)) {
24115 +               AuDbgDentry(a->src_dentry);
24116 +               AuDbgDentry(a->dst_dentry);
24117 +               err = au_ren_do_diropq(a, AuDST);
24118 +               if (unlikely(err))
24119 +                       goto out_rev_src;
24120 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
24121 +       }
24122 +       goto out; /* success */
24123 +
24124 +out_rev_src:
24125 +       AuDbg("err %d, reverting src\n", err);
24126 +       au_ren_rev_diropq(err, a);
24127 +out:
24128 +       return err;
24129 +}
24130 +
24131 +static int do_rename(struct au_ren_args *a)
24132 +{
24133 +       int err;
24134 +       struct dentry *d, *h_d;
24135 +       struct inode *h_inode;
24136 +
24137 +       if (!a->exchange) {
24138 +               /* prepare workqueue args for asynchronous rmdir */
24139 +               h_d = a->dst_h_dentry;
24140 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24141 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
24142 +                   && d_is_positive(h_d)) {
24143 +                       err = -ENOMEM;
24144 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24145 +                                                        GFP_NOFS);
24146 +                       if (unlikely(!a->thargs))
24147 +                               goto out;
24148 +                       a->h_dst = dget(h_d);
24149 +               }
24150 +
24151 +               /* create whiteout for src_dentry */
24152 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
24153 +                       a->src_bwh = au_dbwh(a->src_dentry);
24154 +                       AuDebugOn(a->src_bwh >= 0);
24155 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24156 +                                                       a->src_h_parent);
24157 +                       err = PTR_ERR(a->src_wh_dentry);
24158 +                       if (IS_ERR(a->src_wh_dentry))
24159 +                               goto out_thargs;
24160 +               }
24161 +
24162 +               /* lookup whiteout for dentry */
24163 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
24164 +                       h_d = au_wh_lkup(a->dst_h_parent,
24165 +                                        &a->dst_dentry->d_name, a->br);
24166 +                       err = PTR_ERR(h_d);
24167 +                       if (IS_ERR(h_d))
24168 +                               goto out_whsrc;
24169 +                       if (d_is_negative(h_d))
24170 +                               dput(h_d);
24171 +                       else
24172 +                               a->dst_wh_dentry = h_d;
24173 +               }
24174 +
24175 +               /* rename dentry to tmpwh */
24176 +               if (a->thargs) {
24177 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
24178 +                       if (unlikely(err))
24179 +                               goto out_whdst;
24180 +
24181 +                       d = a->dst_dentry;
24182 +                       au_set_h_dptr(d, a->btgt, NULL);
24183 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
24184 +                       if (unlikely(err))
24185 +                               goto out_whtmp;
24186 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
24187 +               }
24188 +       }
24189 +
24190 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
24191 +#if 0 /* debugging */
24192 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24193 +              && d_is_positive(a->dst_h_dentry)
24194 +              && a->src_btop != a->btgt);
24195 +#endif
24196 +
24197 +       /* rename by vfs_rename or cpup */
24198 +       err = au_ren_or_cpup(a);
24199 +       if (unlikely(err))
24200 +               /* leave the copied-up one */
24201 +               goto out_whtmp;
24202 +
24203 +       /* make dir opaque */
24204 +       err = au_ren_diropq(a);
24205 +       if (unlikely(err))
24206 +               goto out_rename;
24207 +
24208 +       /* update target timestamps */
24209 +       if (a->exchange) {
24210 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24211 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24212 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24213 +               h_inode = d_inode(a->h_path.dentry);
24214 +               inode_set_ctime_to_ts(a->dst_inode, inode_get_ctime(h_inode));
24215 +       }
24216 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
24217 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24218 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24219 +       h_inode = d_inode(a->h_path.dentry);
24220 +       inode_set_ctime_to_ts(a->src_inode, inode_get_ctime(h_inode));
24221 +
24222 +       if (!a->exchange) {
24223 +               /* remove whiteout for dentry */
24224 +               if (a->dst_wh_dentry) {
24225 +                       a->h_path.dentry = a->dst_wh_dentry;
24226 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24227 +                                                 a->dst_dentry);
24228 +                       if (unlikely(err))
24229 +                               goto out_diropq;
24230 +               }
24231 +
24232 +               /* remove whtmp */
24233 +               if (a->thargs)
24234 +                       au_ren_del_whtmp(a); /* ignore this error */
24235 +
24236 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24237 +       }
24238 +       err = 0;
24239 +       goto out_success;
24240 +
24241 +out_diropq:
24242 +       au_ren_rev_diropq(err, a);
24243 +out_rename:
24244 +       au_ren_rev_rename(err, a);
24245 +       dput(a->h_dst);
24246 +out_whtmp:
24247 +       if (a->thargs)
24248 +               au_ren_rev_whtmp(err, a);
24249 +out_whdst:
24250 +       dput(a->dst_wh_dentry);
24251 +       a->dst_wh_dentry = NULL;
24252 +out_whsrc:
24253 +       if (a->src_wh_dentry)
24254 +               au_ren_rev_whsrc(err, a);
24255 +out_success:
24256 +       dput(a->src_wh_dentry);
24257 +       dput(a->dst_wh_dentry);
24258 +out_thargs:
24259 +       if (a->thargs) {
24260 +               dput(a->h_dst);
24261 +               au_whtmp_rmdir_free(a->thargs);
24262 +               a->thargs = NULL;
24263 +       }
24264 +out:
24265 +       return err;
24266 +}
24267 +
24268 +/* ---------------------------------------------------------------------- */
24269 +
24270 +/*
24271 + * test if @dentry dir can be rename destination or not.
24272 + * success means, it is a logically empty dir.
24273 + */
24274 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
24275 +{
24276 +       return au_test_empty(dentry, whlist);
24277 +}
24278 +
24279 +/*
24280 + * test if @a->src_dentry dir can be rename source or not.
24281 + * if it can, return 0.
24282 + * success means,
24283 + * - it is a logically empty dir.
24284 + * - or, it exists on writable branch and has no children including whiteouts
24285 + *   on the lower branch unless DIRREN is on.
24286 + */
24287 +static int may_rename_srcdir(struct au_ren_args *a)
24288 +{
24289 +       int err;
24290 +       unsigned int rdhash;
24291 +       aufs_bindex_t btop, btgt;
24292 +       struct dentry *dentry;
24293 +       struct super_block *sb;
24294 +       struct au_sbinfo *sbinfo;
24295 +
24296 +       dentry = a->src_dentry;
24297 +       sb = dentry->d_sb;
24298 +       sbinfo = au_sbi(sb);
24299 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24300 +               au_fset_ren(a->auren_flags, DIRREN);
24301 +
24302 +       btgt = a->btgt;
24303 +       btop = au_dbtop(dentry);
24304 +       if (btop != btgt) {
24305 +               struct au_nhash whlist;
24306 +
24307 +               SiMustAnyLock(sb);
24308 +               rdhash = sbinfo->si_rdhash;
24309 +               if (!rdhash)
24310 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24311 +                                                          dentry));
24312 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24313 +               if (unlikely(err))
24314 +                       goto out;
24315 +               err = au_test_empty(dentry, &whlist);
24316 +               au_nhash_wh_free(&whlist);
24317 +               goto out;
24318 +       }
24319 +
24320 +       if (btop == au_dbtaildir(dentry))
24321 +               return 0; /* success */
24322 +
24323 +       err = au_test_empty_lower(dentry);
24324 +
24325 +out:
24326 +       if (err == -ENOTEMPTY) {
24327 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
24328 +                       err = 0;
24329 +               } else {
24330 +                       AuWarn1("renaming dir who has child(ren) on multiple "
24331 +                               "branches, is not supported\n");
24332 +                       err = -EXDEV;
24333 +               }
24334 +       }
24335 +       return err;
24336 +}
24337 +
24338 +/* side effect: sets whlist and h_dentry */
24339 +static int au_ren_may_dir(struct au_ren_args *a)
24340 +{
24341 +       int err;
24342 +       unsigned int rdhash;
24343 +       struct dentry *d;
24344 +
24345 +       d = a->dst_dentry;
24346 +       SiMustAnyLock(d->d_sb);
24347 +
24348 +       err = 0;
24349 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
24350 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
24351 +               if (!rdhash)
24352 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24353 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24354 +               if (unlikely(err))
24355 +                       goto out;
24356 +
24357 +               if (!a->exchange) {
24358 +                       au_set_dbtop(d, a->dst_btop);
24359 +                       err = may_rename_dstdir(d, &a->whlist);
24360 +                       au_set_dbtop(d, a->btgt);
24361 +               } else
24362 +                       err = may_rename_srcdir(a);
24363 +       }
24364 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
24365 +       if (unlikely(err))
24366 +               goto out;
24367 +
24368 +       d = a->src_dentry;
24369 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
24370 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24371 +               err = may_rename_srcdir(a);
24372 +               if (unlikely(err)) {
24373 +                       au_nhash_wh_free(&a->whlist);
24374 +                       a->whlist.nh_num = 0;
24375 +               }
24376 +       }
24377 +out:
24378 +       return err;
24379 +}
24380 +
24381 +/* ---------------------------------------------------------------------- */
24382 +
24383 +/*
24384 + * simple tests for rename.
24385 + * following the checks in vfs, plus the parent-child relationship.
24386 + */
24387 +static int au_may_ren(struct au_ren_args *a)
24388 +{
24389 +       int err, isdir;
24390 +       struct inode *h_inode;
24391 +
24392 +       if (a->src_btop == a->btgt) {
24393 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
24394 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
24395 +               if (unlikely(err))
24396 +                       goto out;
24397 +               err = -EINVAL;
24398 +               if (unlikely(a->src_h_dentry == a->h_trap))
24399 +                       goto out;
24400 +       }
24401 +
24402 +       err = 0;
24403 +       if (a->dst_btop != a->btgt)
24404 +               goto out;
24405 +
24406 +       err = -ENOTEMPTY;
24407 +       if (unlikely(a->dst_h_dentry == a->h_trap))
24408 +               goto out;
24409 +
24410 +       err = -EIO;
24411 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
24412 +       if (d_really_is_negative(a->dst_dentry)) {
24413 +               if (d_is_negative(a->dst_h_dentry))
24414 +                       err = au_may_add(a->dst_dentry, a->btgt,
24415 +                                        a->dst_h_parent, isdir);
24416 +       } else {
24417 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
24418 +                       goto out;
24419 +               h_inode = d_inode(a->dst_h_dentry);
24420 +               if (h_inode->i_nlink)
24421 +                       err = au_may_del(a->dst_dentry, a->btgt,
24422 +                                        a->dst_h_parent, isdir);
24423 +       }
24424 +
24425 +out:
24426 +       if (unlikely(err == -ENOENT || err == -EEXIST))
24427 +               err = -EIO;
24428 +       AuTraceErr(err);
24429 +       return err;
24430 +}
24431 +
24432 +/* ---------------------------------------------------------------------- */
24433 +
24434 +/*
24435 + * locking order
24436 + * (VFS)
24437 + * - src_dir and dir by lock_rename()
24438 + * - inode if exists
24439 + * (aufs)
24440 + * - lock all
24441 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24442 + *     + si_read_lock
24443 + *     + di_write_lock2_child()
24444 + *       + di_write_lock_child()
24445 + *        + ii_write_lock_child()
24446 + *       + di_write_lock_child2()
24447 + *        + ii_write_lock_child2()
24448 + *     + src_parent and parent
24449 + *       + di_write_lock_parent()
24450 + *        + ii_write_lock_parent()
24451 + *       + di_write_lock_parent2()
24452 + *        + ii_write_lock_parent2()
24453 + *   + lower src_dir and dir by vfsub_lock_rename()
24454 + *   + verify the every relationships between child and parent. if any
24455 + *     of them failed, unlock all and return -EBUSY.
24456 + */
24457 +static void au_ren_unlock(struct au_ren_args *a)
24458 +{
24459 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24460 +                           a->dst_h_parent, a->dst_hdir);
24461 +       if (au_ftest_ren(a->auren_flags, DIRREN)
24462 +           && a->h_root)
24463 +               au_hn_inode_unlock(a->h_root);
24464 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
24465 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
24466 +}
24467 +
24468 +static int au_ren_lock(struct au_ren_args *a)
24469 +{
24470 +       int err;
24471 +       unsigned int udba;
24472 +
24473 +       err = 0;
24474 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24475 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
24476 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24477 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
24478 +
24479 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
24480 +       if (unlikely(err))
24481 +               goto out;
24482 +       au_fset_ren(a->auren_flags, MNT_WRITE);
24483 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24484 +               struct dentry *root;
24485 +               struct inode *dir;
24486 +
24487 +               /*
24488 +                * sbinfo is already locked, so this ii_read_lock is
24489 +                * unnecessary. but our debugging feature checks it.
24490 +                */
24491 +               root = a->src_inode->i_sb->s_root;
24492 +               if (root != a->src_parent && root != a->dst_parent) {
24493 +                       dir = d_inode(root);
24494 +                       ii_read_lock_parent3(dir);
24495 +                       a->h_root = au_hi(dir, a->btgt);
24496 +                       ii_read_unlock(dir);
24497 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24498 +               }
24499 +       }
24500 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24501 +                                     a->dst_h_parent, a->dst_hdir);
24502 +       udba = au_opt_udba(a->src_dentry->d_sb);
24503 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24504 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
24505 +               err = au_busy_or_stale();
24506 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
24507 +               err = au_h_verify(a->src_h_dentry, udba,
24508 +                                 d_inode(a->src_h_parent), a->src_h_parent,
24509 +                                 a->br);
24510 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
24511 +               err = au_h_verify(a->dst_h_dentry, udba,
24512 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
24513 +                                 a->br);
24514 +       if (!err)
24515 +               goto out; /* success */
24516 +
24517 +       err = au_busy_or_stale();
24518 +       au_ren_unlock(a);
24519 +
24520 +out:
24521 +       return err;
24522 +}
24523 +
24524 +/* ---------------------------------------------------------------------- */
24525 +
24526 +static void au_ren_refresh_dir(struct au_ren_args *a)
24527 +{
24528 +       struct inode *dir;
24529 +
24530 +       dir = a->dst_dir;
24531 +       inode_inc_iversion(dir);
24532 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24533 +               /* is this updating defined in POSIX? */
24534 +               au_cpup_attr_timesizes(a->src_inode);
24535 +               au_cpup_attr_nlink(dir, /*force*/1);
24536 +       }
24537 +       au_dir_ts(dir, a->btgt);
24538 +
24539 +       if (a->exchange) {
24540 +               dir = a->src_dir;
24541 +               inode_inc_iversion(dir);
24542 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24543 +                       /* is this updating defined in POSIX? */
24544 +                       au_cpup_attr_timesizes(a->dst_inode);
24545 +                       au_cpup_attr_nlink(dir, /*force*/1);
24546 +               }
24547 +               au_dir_ts(dir, a->btgt);
24548 +       }
24549 +
24550 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24551 +               return;
24552 +
24553 +       dir = a->src_dir;
24554 +       inode_inc_iversion(dir);
24555 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
24556 +               au_cpup_attr_nlink(dir, /*force*/1);
24557 +       au_dir_ts(dir, a->btgt);
24558 +}
24559 +
24560 +static void au_ren_refresh(struct au_ren_args *a)
24561 +{
24562 +       aufs_bindex_t bbot, bindex;
24563 +       struct dentry *d, *h_d;
24564 +       struct inode *i, *h_i;
24565 +       struct super_block *sb;
24566 +
24567 +       d = a->dst_dentry;
24568 +       d_drop(d);
24569 +       if (a->h_dst)
24570 +               /* already dget-ed by au_ren_or_cpup() */
24571 +               au_set_h_dptr(d, a->btgt, a->h_dst);
24572 +
24573 +       i = a->dst_inode;
24574 +       if (i) {
24575 +               if (!a->exchange) {
24576 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24577 +                               vfsub_drop_nlink(i);
24578 +                       else {
24579 +                               vfsub_dead_dir(i);
24580 +                               au_cpup_attr_timesizes(i);
24581 +                       }
24582 +                       au_update_dbrange(d, /*do_put_zero*/1);
24583 +               } else
24584 +                       au_cpup_attr_nlink(i, /*force*/1);
24585 +       } else {
24586 +               bbot = a->btgt;
24587 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
24588 +                       au_set_h_dptr(d, bindex, NULL);
24589 +               bbot = au_dbbot(d);
24590 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
24591 +                       au_set_h_dptr(d, bindex, NULL);
24592 +               au_update_dbrange(d, /*do_put_zero*/0);
24593 +       }
24594 +
24595 +       if (a->exchange
24596 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
24597 +               d_drop(a->src_dentry);
24598 +               if (au_ftest_ren(a->auren_flags, DIRREN))
24599 +                       au_set_dbwh(a->src_dentry, -1);
24600 +               return;
24601 +       }
24602 +
24603 +       d = a->src_dentry;
24604 +       au_set_dbwh(d, -1);
24605 +       bbot = au_dbbot(d);
24606 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24607 +               h_d = au_h_dptr(d, bindex);
24608 +               if (h_d)
24609 +                       au_set_h_dptr(d, bindex, NULL);
24610 +       }
24611 +       au_set_dbbot(d, a->btgt);
24612 +
24613 +       sb = d->d_sb;
24614 +       i = a->src_inode;
24615 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24616 +               return; /* success */
24617 +
24618 +       bbot = au_ibbot(i);
24619 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24620 +               h_i = au_h_iptr(i, bindex);
24621 +               if (h_i) {
24622 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24623 +                       /* ignore this error */
24624 +                       au_set_h_iptr(i, bindex, NULL, 0);
24625 +               }
24626 +       }
24627 +       au_set_ibbot(i, a->btgt);
24628 +}
24629 +
24630 +/* ---------------------------------------------------------------------- */
24631 +
24632 +/* mainly for link(2) and rename(2) */
24633 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
24634 +{
24635 +       aufs_bindex_t bdiropq, bwh;
24636 +       struct dentry *parent;
24637 +       struct au_branch *br;
24638 +
24639 +       parent = dentry->d_parent;
24640 +       IMustLock(d_inode(parent)); /* dir is locked */
24641 +
24642 +       bdiropq = au_dbdiropq(parent);
24643 +       bwh = au_dbwh(dentry);
24644 +       br = au_sbr(dentry->d_sb, btgt);
24645 +       if (au_br_rdonly(br)
24646 +           || (0 <= bdiropq && bdiropq < btgt)
24647 +           || (0 <= bwh && bwh < btgt))
24648 +               btgt = -1;
24649 +
24650 +       AuDbg("btgt %d\n", btgt);
24651 +       return btgt;
24652 +}
24653 +
24654 +/* sets src_btop, dst_btop and btgt */
24655 +static int au_ren_wbr(struct au_ren_args *a)
24656 +{
24657 +       int err;
24658 +       struct au_wr_dir_args wr_dir_args = {
24659 +               /* .force_btgt  = -1, */
24660 +               .flags          = AuWrDir_ADD_ENTRY
24661 +       };
24662 +
24663 +       a->src_btop = au_dbtop(a->src_dentry);
24664 +       a->dst_btop = au_dbtop(a->dst_dentry);
24665 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24666 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
24667 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
24668 +       wr_dir_args.force_btgt = a->src_btop;
24669 +       if (a->dst_inode && a->dst_btop < a->src_btop)
24670 +               wr_dir_args.force_btgt = a->dst_btop;
24671 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24672 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24673 +       a->btgt = err;
24674 +       if (a->exchange)
24675 +               au_update_dbtop(a->dst_dentry);
24676 +
24677 +       return err;
24678 +}
24679 +
24680 +static void au_ren_dt(struct au_ren_args *a)
24681 +{
24682 +       a->h_path.dentry = a->src_h_parent;
24683 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
24684 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
24685 +               a->h_path.dentry = a->dst_h_parent;
24686 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24687 +       }
24688 +
24689 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
24690 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24691 +           && !a->exchange)
24692 +               return;
24693 +
24694 +       a->h_path.dentry = a->src_h_dentry;
24695 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
24696 +       if (d_is_positive(a->dst_h_dentry)) {
24697 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
24698 +               a->h_path.dentry = a->dst_h_dentry;
24699 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24700 +       }
24701 +}
24702 +
24703 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
24704 +{
24705 +       struct dentry *h_d;
24706 +       struct inode *h_inode;
24707 +
24708 +       au_dtime_revert(a->src_dt + AuPARENT);
24709 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
24710 +               au_dtime_revert(a->dst_dt + AuPARENT);
24711 +
24712 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
24713 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
24714 +               h_inode = d_inode(h_d);
24715 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
24716 +               au_dtime_revert(a->src_dt + AuCHILD);
24717 +               inode_unlock(h_inode);
24718 +
24719 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
24720 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
24721 +                       h_inode = d_inode(h_d);
24722 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
24723 +                       au_dtime_revert(a->dst_dt + AuCHILD);
24724 +                       inode_unlock(h_inode);
24725 +               }
24726 +       }
24727 +}
24728 +
24729 +/* ---------------------------------------------------------------------- */
24730 +
24731 +int aufs_rename(struct mnt_idmap *idmap,
24732 +               struct inode *_src_dir, struct dentry *_src_dentry,
24733 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
24734 +               unsigned int _flags)
24735 +{
24736 +       int err, lock_flags;
24737 +       void *rev;
24738 +       /* reduce stack space */
24739 +       struct au_ren_args *a;
24740 +       struct au_pin pin;
24741 +
24742 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
24743 +       IMustLock(_src_dir);
24744 +       IMustLock(_dst_dir);
24745 +
24746 +       err = -EINVAL;
24747 +       if (unlikely(_flags & RENAME_WHITEOUT))
24748 +               goto out;
24749 +
24750 +       err = -ENOMEM;
24751 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24752 +       a = kzalloc(sizeof(*a), GFP_NOFS);
24753 +       if (unlikely(!a))
24754 +               goto out;
24755 +
24756 +       a->flags = _flags;
24757 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24758 +                    && RENAME_EXCHANGE > U8_MAX);
24759 +       a->exchange = _flags & RENAME_EXCHANGE;
24760 +       a->src_dir = _src_dir;
24761 +       a->src_dentry = _src_dentry;
24762 +       a->src_inode = NULL;
24763 +       if (d_really_is_positive(a->src_dentry))
24764 +               a->src_inode = d_inode(a->src_dentry);
24765 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24766 +       a->dst_dir = _dst_dir;
24767 +       a->dst_dentry = _dst_dentry;
24768 +       a->dst_inode = NULL;
24769 +       if (d_really_is_positive(a->dst_dentry))
24770 +               a->dst_inode = d_inode(a->dst_dentry);
24771 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24772 +       if (a->dst_inode) {
24773 +               /*
24774 +                * if EXCHANGE && src is non-dir && dst is dir,
24775 +                * dst is not locked.
24776 +                */
24777 +               /* IMustLock(a->dst_inode); */
24778 +               au_igrab(a->dst_inode);
24779 +       }
24780 +
24781 +       err = -ENOTDIR;
24782 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
24783 +       if (d_is_dir(a->src_dentry)) {
24784 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
24785 +               if (unlikely(!a->exchange
24786 +                            && d_really_is_positive(a->dst_dentry)
24787 +                            && !d_is_dir(a->dst_dentry)))
24788 +                       goto out_free;
24789 +               lock_flags |= AuLock_DIRS;
24790 +       }
24791 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24792 +               au_fset_ren(a->auren_flags, ISDIR_DST);
24793 +               if (unlikely(!a->exchange
24794 +                            && d_really_is_positive(a->src_dentry)
24795 +                            && !d_is_dir(a->src_dentry)))
24796 +                       goto out_free;
24797 +               lock_flags |= AuLock_DIRS;
24798 +       }
24799 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24800 +                                       lock_flags);
24801 +       if (unlikely(err))
24802 +               goto out_free;
24803 +
24804 +       err = au_d_hashed_positive(a->src_dentry);
24805 +       if (unlikely(err))
24806 +               goto out_unlock;
24807 +       err = -ENOENT;
24808 +       if (a->dst_inode) {
24809 +               /*
24810 +                * If it is a dir, VFS unhash it before this
24811 +                * function. It means we cannot rely upon d_unhashed().
24812 +                */
24813 +               if (unlikely(!a->dst_inode->i_nlink))
24814 +                       goto out_unlock;
24815 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24816 +                       err = au_d_hashed_positive(a->dst_dentry);
24817 +                       if (unlikely(err && !a->exchange))
24818 +                               goto out_unlock;
24819 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24820 +                       goto out_unlock;
24821 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
24822 +               goto out_unlock;
24823 +
24824 +       /*
24825 +        * is it possible?
24826 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
24827 +        * there may exist a problem somewhere else.
24828 +        */
24829 +       err = -EINVAL;
24830 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
24831 +               goto out_unlock;
24832 +
24833 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
24834 +       di_write_lock_parent(a->dst_parent);
24835 +
24836 +       /* which branch we process */
24837 +       err = au_ren_wbr(a);
24838 +       if (unlikely(err < 0))
24839 +               goto out_parent;
24840 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
24841 +       a->h_path.mnt = au_br_mnt(a->br);
24842 +
24843 +       /* are they available to be renamed */
24844 +       err = au_ren_may_dir(a);
24845 +       if (unlikely(err))
24846 +               goto out_children;
24847 +
24848 +       /* prepare the writable parent dir on the same branch */
24849 +       if (a->dst_btop == a->btgt) {
24850 +               au_fset_ren(a->auren_flags, WHDST);
24851 +       } else {
24852 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
24853 +               if (unlikely(err))
24854 +                       goto out_children;
24855 +       }
24856 +
24857 +       err = 0;
24858 +       if (!a->exchange) {
24859 +               if (a->src_dir != a->dst_dir) {
24860 +                       /*
24861 +                        * this temporary unlock is safe,
24862 +                        * because both dir->i_mutex are locked.
24863 +                        */
24864 +                       di_write_unlock(a->dst_parent);
24865 +                       di_write_lock_parent(a->src_parent);
24866 +                       err = au_wr_dir_need_wh(a->src_dentry,
24867 +                                               au_ftest_ren(a->auren_flags,
24868 +                                                            ISDIR_SRC),
24869 +                                               &a->btgt);
24870 +                       di_write_unlock(a->src_parent);
24871 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
24872 +                                             /*isdir*/1);
24873 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
24874 +               } else
24875 +                       err = au_wr_dir_need_wh(a->src_dentry,
24876 +                                               au_ftest_ren(a->auren_flags,
24877 +                                                            ISDIR_SRC),
24878 +                                               &a->btgt);
24879 +       }
24880 +       if (unlikely(err < 0))
24881 +               goto out_children;
24882 +       if (err)
24883 +               au_fset_ren(a->auren_flags, WHSRC);
24884 +
24885 +       /* cpup src */
24886 +       if (a->src_btop != a->btgt) {
24887 +               err = au_pin(&pin, a->src_dentry, a->btgt,
24888 +                            au_opt_udba(a->src_dentry->d_sb),
24889 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24890 +               if (!err) {
24891 +                       struct au_cp_generic cpg = {
24892 +                               .dentry = a->src_dentry,
24893 +                               .bdst   = a->btgt,
24894 +                               .bsrc   = a->src_btop,
24895 +                               .len    = -1,
24896 +                               .pin    = &pin,
24897 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24898 +                       };
24899 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
24900 +                       err = au_sio_cpup_simple(&cpg);
24901 +                       au_unpin(&pin);
24902 +               }
24903 +               if (unlikely(err))
24904 +                       goto out_children;
24905 +               a->src_btop = a->btgt;
24906 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
24907 +               if (!a->exchange)
24908 +                       au_fset_ren(a->auren_flags, WHSRC);
24909 +       }
24910 +
24911 +       /* cpup dst */
24912 +       if (a->exchange && a->dst_inode
24913 +           && a->dst_btop != a->btgt) {
24914 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
24915 +                            au_opt_udba(a->dst_dentry->d_sb),
24916 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24917 +               if (!err) {
24918 +                       struct au_cp_generic cpg = {
24919 +                               .dentry = a->dst_dentry,
24920 +                               .bdst   = a->btgt,
24921 +                               .bsrc   = a->dst_btop,
24922 +                               .len    = -1,
24923 +                               .pin    = &pin,
24924 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24925 +                       };
24926 +                       err = au_sio_cpup_simple(&cpg);
24927 +                       au_unpin(&pin);
24928 +               }
24929 +               if (unlikely(err))
24930 +                       goto out_children;
24931 +               a->dst_btop = a->btgt;
24932 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
24933 +       }
24934 +
24935 +       /* lock them all */
24936 +       err = au_ren_lock(a);
24937 +       if (unlikely(err))
24938 +               /* leave the copied-up one */
24939 +               goto out_children;
24940 +
24941 +       if (!a->exchange) {
24942 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24943 +                       err = au_may_ren(a);
24944 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24945 +                       err = -ENAMETOOLONG;
24946 +               if (unlikely(err))
24947 +                       goto out_hdir;
24948 +       }
24949 +
24950 +       /* store timestamps to be revertible */
24951 +       au_ren_dt(a);
24952 +
24953 +       /* store dirren info */
24954 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24955 +               err = au_dr_rename(a->src_dentry, a->btgt,
24956 +                                  &a->dst_dentry->d_name, &rev);
24957 +               AuTraceErr(err);
24958 +               if (unlikely(err))
24959 +                       goto out_dt;
24960 +       }
24961 +
24962 +       /* here we go */
24963 +       err = do_rename(a);
24964 +       if (unlikely(err))
24965 +               goto out_dirren;
24966 +
24967 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24968 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
24969 +
24970 +       /* update dir attributes */
24971 +       au_ren_refresh_dir(a);
24972 +
24973 +       /* dput/iput all lower dentries */
24974 +       au_ren_refresh(a);
24975 +
24976 +       goto out_hdir; /* success */
24977 +
24978 +out_dirren:
24979 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24980 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
24981 +out_dt:
24982 +       au_ren_rev_dt(err, a);
24983 +out_hdir:
24984 +       au_ren_unlock(a);
24985 +out_children:
24986 +       au_nhash_wh_free(&a->whlist);
24987 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
24988 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
24989 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
24990 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
24991 +       }
24992 +out_parent:
24993 +       if (!err) {
24994 +               if (d_unhashed(a->src_dentry))
24995 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
24996 +               if (d_unhashed(a->dst_dentry))
24997 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
24998 +               if (!a->exchange)
24999 +                       d_move(a->src_dentry, a->dst_dentry);
25000 +               else {
25001 +                       d_exchange(a->src_dentry, a->dst_dentry);
25002 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
25003 +                               d_drop(a->dst_dentry);
25004 +               }
25005 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
25006 +                       d_drop(a->src_dentry);
25007 +       } else {
25008 +               au_update_dbtop(a->dst_dentry);
25009 +               if (!a->dst_inode)
25010 +                       d_drop(a->dst_dentry);
25011 +       }
25012 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
25013 +               di_write_unlock(a->dst_parent);
25014 +       else
25015 +               di_write_unlock2(a->src_parent, a->dst_parent);
25016 +out_unlock:
25017 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
25018 +out_free:
25019 +       iput(a->dst_inode);
25020 +       if (a->thargs)
25021 +               au_whtmp_rmdir_free(a->thargs);
25022 +       au_kfree_rcu(a);
25023 +out:
25024 +       AuTraceErr(err);
25025 +       return err;
25026 +}
25027 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
25028 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
25029 +++ linux/fs/aufs/Kconfig       2022-11-05 23:02:18.959222617 +0100
25030 @@ -0,0 +1,199 @@
25031 +# SPDX-License-Identifier: GPL-2.0
25032 +config AUFS_FS
25033 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
25034 +       help
25035 +       Aufs is a stackable unification filesystem such as Unionfs,
25036 +       which unifies several directories and provides a merged single
25037 +       directory.
25038 +       In the early days, aufs was entirely re-designed and
25039 +       re-implemented Unionfs Version 1.x series. Introducing many
25040 +       original ideas, approaches and improvements, it becomes totally
25041 +       different from Unionfs while keeping the basic features.
25042 +
25043 +if AUFS_FS
25044 +choice
25045 +       prompt "Maximum number of branches"
25046 +       default AUFS_BRANCH_MAX_127
25047 +       help
25048 +       Specifies the maximum number of branches (or member directories)
25049 +       in a single aufs. The larger value consumes more system
25050 +       resources and has a minor impact to performance.
25051 +config AUFS_BRANCH_MAX_127
25052 +       bool "127"
25053 +       help
25054 +       Specifies the maximum number of branches (or member directories)
25055 +       in a single aufs. The larger value consumes more system
25056 +       resources and has a minor impact to performance.
25057 +config AUFS_BRANCH_MAX_511
25058 +       bool "511"
25059 +       help
25060 +       Specifies the maximum number of branches (or member directories)
25061 +       in a single aufs. The larger value consumes more system
25062 +       resources and has a minor impact to performance.
25063 +config AUFS_BRANCH_MAX_1023
25064 +       bool "1023"
25065 +       help
25066 +       Specifies the maximum number of branches (or member directories)
25067 +       in a single aufs. The larger value consumes more system
25068 +       resources and has a minor impact to performance.
25069 +config AUFS_BRANCH_MAX_32767
25070 +       bool "32767"
25071 +       help
25072 +       Specifies the maximum number of branches (or member directories)
25073 +       in a single aufs. The larger value consumes more system
25074 +       resources and has a minor impact to performance.
25075 +endchoice
25076 +
25077 +config AUFS_SBILIST
25078 +       bool
25079 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
25080 +       default y
25081 +       help
25082 +       Automatic configuration for internal use.
25083 +       When aufs supports Magic SysRq or /proc, enabled automatically.
25084 +
25085 +config AUFS_HNOTIFY
25086 +       bool "Detect direct branch access (bypassing aufs)"
25087 +       help
25088 +       If you want to modify files on branches directly, eg. bypassing aufs,
25089 +       and want aufs to detect the changes of them fully, then enable this
25090 +       option and use 'udba=notify' mount option.
25091 +       Currently there is only one available configuration, "fsnotify".
25092 +       It will have a negative impact to the performance.
25093 +       See detail in aufs.5.
25094 +
25095 +choice
25096 +       prompt "method" if AUFS_HNOTIFY
25097 +       default AUFS_HFSNOTIFY
25098 +config AUFS_HFSNOTIFY
25099 +       bool "fsnotify"
25100 +       select FSNOTIFY
25101 +endchoice
25102 +
25103 +config AUFS_EXPORT
25104 +       bool "NFS-exportable aufs"
25105 +       depends on EXPORTFS
25106 +       help
25107 +       If you want to export your mounted aufs via NFS, then enable this
25108 +       option. There are several requirements for this configuration.
25109 +       See detail in aufs.5.
25110 +
25111 +config AUFS_INO_T_64
25112 +       bool
25113 +       depends on AUFS_EXPORT
25114 +       depends on 64BIT && !(ALPHA || S390)
25115 +       default y
25116 +       help
25117 +       Automatic configuration for internal use.
25118 +       /* typedef unsigned long/int __kernel_ino_t */
25119 +       /* alpha and s390x are int */
25120 +
25121 +config AUFS_XATTR
25122 +       bool "support for XATTR/EA (including Security Labels)"
25123 +       help
25124 +       If your branch fs supports XATTR/EA and you want to make them
25125 +       available in aufs too, then enable this opsion and specify the
25126 +       branch attributes for EA.
25127 +       See detail in aufs.5.
25128 +
25129 +config AUFS_FHSM
25130 +       bool "File-based Hierarchical Storage Management"
25131 +       help
25132 +       Hierarchical Storage Management (or HSM) is a well-known feature
25133 +       in the storage world. Aufs provides this feature as file-based.
25134 +       with multiple branches.
25135 +       These multiple branches are prioritized, ie. the topmost one
25136 +       should be the fastest drive and be used heavily.
25137 +
25138 +config AUFS_RDU
25139 +       bool "Readdir in userspace"
25140 +       help
25141 +       Aufs has two methods to provide a merged view for a directory,
25142 +       by a user-space library and by kernel-space natively. The latter
25143 +       is always enabled but sometimes large and slow.
25144 +       If you enable this option, install the library in aufs2-util
25145 +       package, and set some environment variables for your readdir(3),
25146 +       then the work will be handled in user-space which generally
25147 +       shows better performance in most cases.
25148 +       See detail in aufs.5.
25149 +
25150 +config AUFS_DIRREN
25151 +       bool "Workaround for rename(2)-ing a directory"
25152 +       help
25153 +       By default, aufs returns EXDEV error in renameing a dir who has
25154 +       his child on the lower branch, since it is a bad idea to issue
25155 +       rename(2) internally for every lower branch. But user may not
25156 +       accept this behaviour. So here is a workaround to allow such
25157 +       rename(2) and store some extra information on the writable
25158 +       branch. Obviously this costs high (and I don't like it).
25159 +       To use this feature, you need to enable this configuration AND
25160 +       to specify the mount option `dirren.'
25161 +       See details in aufs.5 and the design documents.
25162 +
25163 +config AUFS_SHWH
25164 +       bool "Show whiteouts"
25165 +       help
25166 +       If you want to make the whiteouts in aufs visible, then enable
25167 +       this option and specify 'shwh' mount option. Although it may
25168 +       sounds like philosophy or something, but in technically it
25169 +       simply shows the name of whiteout with keeping its behaviour.
25170 +
25171 +config AUFS_BR_RAMFS
25172 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
25173 +       help
25174 +       If you want to use ramfs as an aufs branch fs, then enable this
25175 +       option. Generally tmpfs is recommended.
25176 +       Aufs prohibited them to be a branch fs by default, because
25177 +       initramfs becomes unusable after switch_root or something
25178 +       generally. If you sets initramfs as an aufs branch and boot your
25179 +       system by switch_root, you will meet a problem easily since the
25180 +       files in initramfs may be inaccessible.
25181 +       Unless you are going to use ramfs as an aufs branch fs without
25182 +       switch_root or something, leave it N.
25183 +
25184 +config AUFS_BR_FUSE
25185 +       bool "Fuse fs as an aufs branch"
25186 +       depends on FUSE_FS
25187 +       select AUFS_POLL
25188 +       help
25189 +       If you want to use fuse-based userspace filesystem as an aufs
25190 +       branch fs, then enable this option.
25191 +       It implements the internal poll(2) operation which is
25192 +       implemented by fuse only (curretnly).
25193 +
25194 +config AUFS_POLL
25195 +       bool
25196 +       help
25197 +       Automatic configuration for internal use.
25198 +
25199 +config AUFS_BR_HFSPLUS
25200 +       bool "Hfsplus as an aufs branch"
25201 +       depends on HFSPLUS_FS
25202 +       default y
25203 +       help
25204 +       If you want to use hfsplus fs as an aufs branch fs, then enable
25205 +       this option. This option introduces a small overhead at
25206 +       copying-up a file on hfsplus.
25207 +
25208 +config AUFS_BDEV_LOOP
25209 +       bool
25210 +       depends on BLK_DEV_LOOP
25211 +       default y
25212 +       help
25213 +       Automatic configuration for internal use.
25214 +       Convert =[ym] into =y.
25215 +
25216 +config AUFS_DEBUG
25217 +       bool "Debug aufs"
25218 +       help
25219 +       Enable this to compile aufs internal debug code.
25220 +       It will have a negative impact to the performance.
25221 +
25222 +config AUFS_MAGIC_SYSRQ
25223 +       bool
25224 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
25225 +       default y
25226 +       help
25227 +       Automatic configuration for internal use.
25228 +       When aufs supports Magic SysRq, enabled automatically.
25229 +endif
25230 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
25231 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
25232 +++ linux/fs/aufs/lcnt.h        2022-11-05 23:02:18.965889284 +0100
25233 @@ -0,0 +1,186 @@
25234 +/* SPDX-License-Identifier: GPL-2.0 */
25235 +/*
25236 + * Copyright (C) 2018-2022 Junjiro R. Okajima
25237 + *
25238 + * This program is free software; you can redistribute it and/or modify
25239 + * it under the terms of the GNU General Public License as published by
25240 + * the Free Software Foundation; either version 2 of the License, or
25241 + * (at your option) any later version.
25242 + *
25243 + * This program is distributed in the hope that it will be useful,
25244 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25245 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25246 + * GNU General Public License for more details.
25247 + *
25248 + * You should have received a copy of the GNU General Public License
25249 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25250 + */
25251 +
25252 +/*
25253 + * simple long counter wrapper
25254 + */
25255 +
25256 +#ifndef __AUFS_LCNT_H__
25257 +#define __AUFS_LCNT_H__
25258 +
25259 +#ifdef __KERNEL__
25260 +
25261 +#include "debug.h"
25262 +
25263 +#define AuLCntATOMIC   1
25264 +#define AuLCntPCPUCNT  2
25265 +/*
25266 + * why does percpu_refcount require extra synchronize_rcu()s in
25267 + * au_br_do_free()
25268 + */
25269 +#define AuLCntPCPUREF  3
25270 +
25271 +/* #define AuLCntChosen        AuLCntATOMIC */
25272 +#define AuLCntChosen   AuLCntPCPUCNT
25273 +/* #define AuLCntChosen        AuLCntPCPUREF */
25274 +
25275 +#if AuLCntChosen == AuLCntATOMIC
25276 +#include <linux/atomic.h>
25277 +
25278 +typedef atomic_long_t au_lcnt_t;
25279 +
25280 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25281 +{
25282 +       atomic_long_set(cnt, 0);
25283 +       return 0;
25284 +}
25285 +
25286 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25287 +{
25288 +       /* empty */
25289 +}
25290 +
25291 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25292 +                              int do_sync __maybe_unused)
25293 +{
25294 +       /* empty */
25295 +}
25296 +
25297 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25298 +{
25299 +       atomic_long_inc(cnt);
25300 +}
25301 +
25302 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25303 +{
25304 +       atomic_long_dec(cnt);
25305 +}
25306 +
25307 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25308 +{
25309 +       return atomic_long_read(cnt);
25310 +}
25311 +#endif
25312 +
25313 +#if AuLCntChosen == AuLCntPCPUCNT
25314 +#include <linux/percpu_counter.h>
25315 +
25316 +typedef struct percpu_counter au_lcnt_t;
25317 +
25318 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25319 +{
25320 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
25321 +}
25322 +
25323 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25324 +{
25325 +       /* empty */
25326 +}
25327 +
25328 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25329 +{
25330 +       percpu_counter_destroy(cnt);
25331 +}
25332 +
25333 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25334 +{
25335 +       percpu_counter_inc(cnt);
25336 +}
25337 +
25338 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25339 +{
25340 +       percpu_counter_dec(cnt);
25341 +}
25342 +
25343 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25344 +{
25345 +       s64 n;
25346 +
25347 +       n = percpu_counter_sum(cnt);
25348 +       BUG_ON(n < 0);
25349 +       if (LONG_MAX != LLONG_MAX
25350 +           && n > LONG_MAX)
25351 +               AuWarn1("%s\n", "wrap-around");
25352 +
25353 +       return n;
25354 +}
25355 +#endif
25356 +
25357 +#if AuLCntChosen == AuLCntPCPUREF
25358 +#include <linux/percpu-refcount.h>
25359 +
25360 +typedef struct percpu_ref au_lcnt_t;
25361 +
25362 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25363 +{
25364 +       if (!release)
25365 +               release = percpu_ref_exit;
25366 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25367 +}
25368 +
25369 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25370 +{
25371 +       synchronize_rcu();
25372 +}
25373 +
25374 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25375 +{
25376 +       percpu_ref_kill(cnt);
25377 +       if (do_sync)
25378 +               au_lcnt_wait_for_fin(cnt);
25379 +}
25380 +
25381 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25382 +{
25383 +       percpu_ref_get(cnt);
25384 +}
25385 +
25386 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25387 +{
25388 +       percpu_ref_put(cnt);
25389 +}
25390 +
25391 +/*
25392 + * avoid calling this func as possible.
25393 + */
25394 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25395 +{
25396 +       long l;
25397 +
25398 +       percpu_ref_switch_to_atomic_sync(cnt);
25399 +       l = atomic_long_read(&cnt->count);
25400 +       if (do_rev)
25401 +               percpu_ref_switch_to_percpu(cnt);
25402 +
25403 +       /* percpu_ref is initialized by 1 instead of 0 */
25404 +       return l - 1;
25405 +}
25406 +#endif
25407 +
25408 +#ifdef CONFIG_AUFS_DEBUG
25409 +#define AuLCntZero(val) do {                   \
25410 +       long l = val;                           \
25411 +       if (l)                                  \
25412 +               AuDbg("%s = %ld\n", #val, l);   \
25413 +} while (0)
25414 +#else
25415 +#define AuLCntZero(val)                do {} while (0)
25416 +#endif
25417 +
25418 +#endif /* __KERNEL__ */
25419 +#endif /* __AUFS_LCNT_H__ */
25420 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
25421 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
25422 +++ linux/fs/aufs/loop.c        2022-11-05 23:02:18.965889284 +0100
25423 @@ -0,0 +1,148 @@
25424 +// SPDX-License-Identifier: GPL-2.0
25425 +/*
25426 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25427 + *
25428 + * This program is free software; you can redistribute it and/or modify
25429 + * it under the terms of the GNU General Public License as published by
25430 + * the Free Software Foundation; either version 2 of the License, or
25431 + * (at your option) any later version.
25432 + *
25433 + * This program is distributed in the hope that it will be useful,
25434 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25435 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25436 + * GNU General Public License for more details.
25437 + *
25438 + * You should have received a copy of the GNU General Public License
25439 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25440 + */
25441 +
25442 +/*
25443 + * support for loopback block device as a branch
25444 + */
25445 +
25446 +#include "aufs.h"
25447 +
25448 +/* added into drivers/block/loop.c */
25449 +static struct file *(*backing_file_func)(struct super_block *sb);
25450 +
25451 +/*
25452 + * test if two lower dentries have overlapping branches.
25453 + */
25454 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25455 +{
25456 +       struct super_block *h_sb;
25457 +       struct file *backing_file;
25458 +
25459 +       if (unlikely(!backing_file_func)) {
25460 +               /* don't load "loop" module here */
25461 +               backing_file_func = symbol_get(loop_backing_file);
25462 +               if (unlikely(!backing_file_func))
25463 +                       /* "loop" module is not loaded */
25464 +                       return 0;
25465 +       }
25466 +
25467 +       h_sb = h_adding->d_sb;
25468 +       backing_file = backing_file_func(h_sb);
25469 +       if (!backing_file)
25470 +               return 0;
25471 +
25472 +       h_adding = backing_file->f_path.dentry;
25473 +       /*
25474 +        * h_adding can be local NFS.
25475 +        * in this case aufs cannot detect the loop.
25476 +        */
25477 +       if (unlikely(h_adding->d_sb == sb))
25478 +               return 1;
25479 +       return !!au_test_subdir(h_adding, sb->s_root);
25480 +}
25481 +
25482 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25483 +int au_test_loopback_kthread(void)
25484 +{
25485 +       int ret;
25486 +       struct task_struct *tsk = current;
25487 +       char c, comm[sizeof(tsk->comm)];
25488 +
25489 +       ret = 0;
25490 +       if (tsk->flags & PF_KTHREAD) {
25491 +               get_task_comm(comm, tsk);
25492 +               c = comm[4];
25493 +               ret = ('0' <= c && c <= '9'
25494 +                      && !strncmp(comm, "loop", 4));
25495 +       }
25496 +
25497 +       return ret;
25498 +}
25499 +
25500 +/* ---------------------------------------------------------------------- */
25501 +
25502 +#define au_warn_loopback_step  16
25503 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25504 +static unsigned long *au_warn_loopback_array;
25505 +
25506 +void au_warn_loopback(struct super_block *h_sb)
25507 +{
25508 +       int i, new_nelem;
25509 +       unsigned long *a, magic;
25510 +       static DEFINE_SPINLOCK(spin);
25511 +
25512 +       magic = h_sb->s_magic;
25513 +       spin_lock(&spin);
25514 +       a = au_warn_loopback_array;
25515 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25516 +               if (a[i] == magic) {
25517 +                       spin_unlock(&spin);
25518 +                       return;
25519 +               }
25520 +
25521 +       /* h_sb is new to us, print it */
25522 +       if (i < au_warn_loopback_nelem) {
25523 +               a[i] = magic;
25524 +               goto pr;
25525 +       }
25526 +
25527 +       /* expand the array */
25528 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25529 +       a = au_kzrealloc(au_warn_loopback_array,
25530 +                        au_warn_loopback_nelem * sizeof(unsigned long),
25531 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25532 +                        /*may_shrink*/0);
25533 +       if (a) {
25534 +               au_warn_loopback_nelem = new_nelem;
25535 +               au_warn_loopback_array = a;
25536 +               a[i] = magic;
25537 +               goto pr;
25538 +       }
25539 +
25540 +       spin_unlock(&spin);
25541 +       AuWarn1("realloc failed, ignored\n");
25542 +       return;
25543 +
25544 +pr:
25545 +       spin_unlock(&spin);
25546 +       pr_warn("you may want to try another patch for loopback file "
25547 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25548 +}
25549 +
25550 +int au_loopback_init(void)
25551 +{
25552 +       int err;
25553 +       struct super_block *sb __maybe_unused;
25554 +
25555 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25556 +
25557 +       err = 0;
25558 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25559 +                                        sizeof(unsigned long), GFP_NOFS);
25560 +       if (unlikely(!au_warn_loopback_array))
25561 +               err = -ENOMEM;
25562 +
25563 +       return err;
25564 +}
25565 +
25566 +void au_loopback_fin(void)
25567 +{
25568 +       if (backing_file_func)
25569 +               symbol_put(loop_backing_file);
25570 +       au_kfree_try_rcu(au_warn_loopback_array);
25571 +}
25572 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
25573 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
25574 +++ linux/fs/aufs/loop.h        2022-11-05 23:02:18.965889284 +0100
25575 @@ -0,0 +1,55 @@
25576 +/* SPDX-License-Identifier: GPL-2.0 */
25577 +/*
25578 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25579 + *
25580 + * This program is free software; you can redistribute it and/or modify
25581 + * it under the terms of the GNU General Public License as published by
25582 + * the Free Software Foundation; either version 2 of the License, or
25583 + * (at your option) any later version.
25584 + *
25585 + * This program is distributed in the hope that it will be useful,
25586 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25587 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25588 + * GNU General Public License for more details.
25589 + *
25590 + * You should have received a copy of the GNU General Public License
25591 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25592 + */
25593 +
25594 +/*
25595 + * support for loopback mount as a branch
25596 + */
25597 +
25598 +#ifndef __AUFS_LOOP_H__
25599 +#define __AUFS_LOOP_H__
25600 +
25601 +#ifdef __KERNEL__
25602 +
25603 +struct dentry;
25604 +struct super_block;
25605 +
25606 +#ifdef CONFIG_AUFS_BDEV_LOOP
25607 +/* drivers/block/loop.c */
25608 +struct file *loop_backing_file(struct super_block *sb);
25609 +
25610 +/* loop.c */
25611 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25612 +int au_test_loopback_kthread(void);
25613 +void au_warn_loopback(struct super_block *h_sb);
25614 +
25615 +int au_loopback_init(void);
25616 +void au_loopback_fin(void);
25617 +#else
25618 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25619 +
25620 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25621 +          struct dentry *h_adding)
25622 +AuStubInt0(au_test_loopback_kthread, void)
25623 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25624 +
25625 +AuStubInt0(au_loopback_init, void)
25626 +AuStubVoid(au_loopback_fin, void)
25627 +#endif /* BLK_DEV_LOOP */
25628 +
25629 +#endif /* __KERNEL__ */
25630 +#endif /* __AUFS_LOOP_H__ */
25631 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
25632 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
25633 +++ linux/fs/aufs/magic.mk      2022-11-05 23:02:18.965889284 +0100
25634 @@ -0,0 +1,31 @@
25635 +# SPDX-License-Identifier: GPL-2.0
25636 +
25637 +# defined in ${srctree}/fs/fuse/inode.c
25638 +# tristate
25639 +ifdef CONFIG_FUSE_FS
25640 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25641 +endif
25642 +
25643 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25644 +# tristate
25645 +ifdef CONFIG_XFS_FS
25646 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25647 +endif
25648 +
25649 +# defined in ${srctree}/fs/configfs/mount.c
25650 +# tristate
25651 +ifdef CONFIG_CONFIGFS_FS
25652 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25653 +endif
25654 +
25655 +# defined in ${srctree}/fs/ubifs/ubifs.h
25656 +# tristate
25657 +ifdef CONFIG_UBIFS_FS
25658 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25659 +endif
25660 +
25661 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25662 +# tristate
25663 +ifdef CONFIG_HFSPLUS_FS
25664 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25665 +endif
25666 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
25667 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
25668 +++ linux/fs/aufs/Makefile      2022-11-05 23:02:18.959222617 +0100
25669 @@ -0,0 +1,46 @@
25670 +# SPDX-License-Identifier: GPL-2.0
25671 +
25672 +include ${src}/magic.mk
25673 +ifeq (${CONFIG_AUFS_FS},m)
25674 +include ${src}/conf.mk
25675 +endif
25676 +-include ${src}/priv_def.mk
25677 +
25678 +# cf. include/linux/kernel.h
25679 +# enable pr_debug
25680 +ccflags-y += -DDEBUG
25681 +# sparse requires the full pathname
25682 +ifdef M
25683 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
25684 +else
25685 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
25686 +endif
25687 +
25688 +obj-$(CONFIG_AUFS_FS) += aufs.o
25689 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
25690 +       wkq.o vfsub.o dcsub.o \
25691 +       cpup.o whout.o wbr_policy.o \
25692 +       dinfo.o dentry.o \
25693 +       dynop.o \
25694 +       finfo.o file.o f_op.o \
25695 +       dir.o vdir.o \
25696 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
25697 +       mvdown.o ioctl.o
25698 +
25699 +# all are boolean
25700 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
25701 +aufs-$(CONFIG_SYSFS) += sysfs.o
25702 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25703 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25704 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25705 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
25706 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
25707 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25708 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
25709 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
25710 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
25711 +aufs-$(CONFIG_AUFS_POLL) += poll.o
25712 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
25713 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25714 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25715 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
25716 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
25717 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
25718 +++ linux/fs/aufs/module.c      2022-11-05 23:02:18.965889284 +0100
25719 @@ -0,0 +1,273 @@
25720 +// SPDX-License-Identifier: GPL-2.0
25721 +/*
25722 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25723 + *
25724 + * This program is free software; you can redistribute it and/or modify
25725 + * it under the terms of the GNU General Public License as published by
25726 + * the Free Software Foundation; either version 2 of the License, or
25727 + * (at your option) any later version.
25728 + *
25729 + * This program is distributed in the hope that it will be useful,
25730 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25731 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25732 + * GNU General Public License for more details.
25733 + *
25734 + * You should have received a copy of the GNU General Public License
25735 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25736 + */
25737 +
25738 +/*
25739 + * module global variables and operations
25740 + */
25741 +
25742 +#include <linux/module.h>
25743 +#include <linux/seq_file.h>
25744 +#include "aufs.h"
25745 +
25746 +/* shrinkable realloc */
25747 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25748 +{
25749 +       size_t sz;
25750 +       int diff;
25751 +
25752 +       sz = 0;
25753 +       diff = -1;
25754 +       if (p) {
25755 +#if 0 /* unused */
25756 +               if (!new_sz) {
25757 +                       au_kfree_rcu(p);
25758 +                       p = NULL;
25759 +                       goto out;
25760 +               }
25761 +#else
25762 +               AuDebugOn(!new_sz);
25763 +#endif
25764 +               sz = ksize(p);
25765 +               diff = au_kmidx_sub(sz, new_sz);
25766 +       }
25767 +       if (sz && !diff)
25768 +               goto out;
25769 +
25770 +       if (sz < new_sz)
25771 +               /* expand or SLOB */
25772 +               p = krealloc(p, new_sz, gfp);
25773 +       else if (new_sz < sz && may_shrink) {
25774 +               /* shrink */
25775 +               void *q;
25776 +
25777 +               q = kmalloc(new_sz, gfp);
25778 +               if (q) {
25779 +                       if (p) {
25780 +                               memcpy(q, p, new_sz);
25781 +                               au_kfree_try_rcu(p);
25782 +                       }
25783 +                       p = q;
25784 +               } else
25785 +                       p = NULL;
25786 +       }
25787 +
25788 +out:
25789 +       return p;
25790 +}
25791 +
25792 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25793 +                  int may_shrink)
25794 +{
25795 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
25796 +       if (p && new_sz > nused)
25797 +               memset(p + nused, 0, new_sz - nused);
25798 +       return p;
25799 +}
25800 +
25801 +/* ---------------------------------------------------------------------- */
25802 +/*
25803 + * aufs caches
25804 + */
25805 +struct kmem_cache *au_cache[AuCache_Last];
25806 +
25807 +static void au_cache_fin(void)
25808 +{
25809 +       int i;
25810 +
25811 +       /*
25812 +        * Make sure all delayed rcu free inodes are flushed before we
25813 +        * destroy cache.
25814 +        */
25815 +       rcu_barrier();
25816 +
25817 +       /* excluding AuCache_HNOTIFY */
25818 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25819 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
25820 +               kmem_cache_destroy(au_cache[i]);
25821 +               au_cache[i] = NULL;
25822 +       }
25823 +}
25824 +
25825 +static int __init au_cache_init(void)
25826 +{
25827 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25828 +       if (au_cache[AuCache_DINFO])
25829 +               /* SLAB_DESTROY_BY_RCU */
25830 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25831 +                                                      au_icntnr_init_once);
25832 +       if (au_cache[AuCache_ICNTNR])
25833 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25834 +                                                     au_fi_init_once);
25835 +       if (au_cache[AuCache_FINFO])
25836 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
25837 +       if (au_cache[AuCache_VDIR])
25838 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25839 +       if (au_cache[AuCache_DEHSTR])
25840 +               return 0;
25841 +
25842 +       au_cache_fin();
25843 +       return -ENOMEM;
25844 +}
25845 +
25846 +/* ---------------------------------------------------------------------- */
25847 +
25848 +int au_dir_roflags;
25849 +
25850 +#ifdef CONFIG_AUFS_SBILIST
25851 +/*
25852 + * iterate_supers_type() doesn't protect us from
25853 + * remounting (branch management)
25854 + */
25855 +struct hlist_bl_head au_sbilist;
25856 +#endif
25857 +
25858 +/*
25859 + * functions for module interface.
25860 + */
25861 +MODULE_LICENSE("GPL");
25862 +/* MODULE_LICENSE("GPL v2"); */
25863 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25864 +MODULE_DESCRIPTION(AUFS_NAME
25865 +       " -- Advanced multi layered unification filesystem");
25866 +MODULE_VERSION(AUFS_VERSION);
25867 +MODULE_ALIAS_FS(AUFS_NAME);
25868 +
25869 +/* this module parameter has no meaning when SYSFS is disabled */
25870 +int sysaufs_brs = 1;
25871 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25872 +module_param_named(brs, sysaufs_brs, int, 0444);
25873 +
25874 +/* this module parameter has no meaning when USER_NS is disabled */
25875 +bool au_userns;
25876 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25877 +module_param_named(allow_userns, au_userns, bool, 0444);
25878 +
25879 +/* ---------------------------------------------------------------------- */
25880 +
25881 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25882 +
25883 +int au_seq_path(struct seq_file *seq, struct path *path)
25884 +{
25885 +       int err;
25886 +
25887 +       err = seq_path(seq, path, au_esc_chars);
25888 +       if (err >= 0)
25889 +               err = 0;
25890 +       else
25891 +               err = -ENOMEM;
25892 +
25893 +       return err;
25894 +}
25895 +
25896 +/* ---------------------------------------------------------------------- */
25897 +
25898 +static int __init aufs_init(void)
25899 +{
25900 +       int err, i;
25901 +       char *p;
25902 +
25903 +       p = au_esc_chars;
25904 +       for (i = 1; i <= ' '; i++)
25905 +               *p++ = i;
25906 +       *p++ = '\\';
25907 +       *p++ = '\x7f';
25908 +       *p = 0;
25909 +
25910 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25911 +
25912 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25913 +       for (i = 0; i < AuIop_Last; i++)
25914 +               aufs_iop_nogetattr[i].getattr = NULL;
25915 +
25916 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
25917 +
25918 +       au_sbilist_init();
25919 +       sysaufs_brs_init();
25920 +       au_debug_init();
25921 +       au_dy_init();
25922 +       err = sysaufs_init();
25923 +       if (unlikely(err))
25924 +               goto out;
25925 +       err = dbgaufs_init();
25926 +       if (unlikely(err))
25927 +               goto out_sysaufs;
25928 +       err = au_procfs_init();
25929 +       if (unlikely(err))
25930 +               goto out_dbgaufs;
25931 +       err = au_wkq_init();
25932 +       if (unlikely(err))
25933 +               goto out_procfs;
25934 +       err = au_loopback_init();
25935 +       if (unlikely(err))
25936 +               goto out_wkq;
25937 +       err = au_hnotify_init();
25938 +       if (unlikely(err))
25939 +               goto out_loopback;
25940 +       err = au_sysrq_init();
25941 +       if (unlikely(err))
25942 +               goto out_hin;
25943 +       err = au_cache_init();
25944 +       if (unlikely(err))
25945 +               goto out_sysrq;
25946 +
25947 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25948 +       err = register_filesystem(&aufs_fs_type);
25949 +       if (unlikely(err))
25950 +               goto out_cache;
25951 +
25952 +       /* since we define pr_fmt, call printk directly */
25953 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25954 +       goto out; /* success */
25955 +
25956 +out_cache:
25957 +       au_cache_fin();
25958 +out_sysrq:
25959 +       au_sysrq_fin();
25960 +out_hin:
25961 +       au_hnotify_fin();
25962 +out_loopback:
25963 +       au_loopback_fin();
25964 +out_wkq:
25965 +       au_wkq_fin();
25966 +out_procfs:
25967 +       au_procfs_fin();
25968 +out_dbgaufs:
25969 +       dbgaufs_fin();
25970 +out_sysaufs:
25971 +       sysaufs_fin();
25972 +       au_dy_fin();
25973 +out:
25974 +       return err;
25975 +}
25976 +
25977 +static void __exit aufs_exit(void)
25978 +{
25979 +       unregister_filesystem(&aufs_fs_type);
25980 +       au_cache_fin();
25981 +       au_sysrq_fin();
25982 +       au_hnotify_fin();
25983 +       au_loopback_fin();
25984 +       au_wkq_fin();
25985 +       au_procfs_fin();
25986 +       dbgaufs_fin();
25987 +       sysaufs_fin();
25988 +       au_dy_fin();
25989 +}
25990 +
25991 +module_init(aufs_init);
25992 +module_exit(aufs_exit);
25993 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
25994 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
25995 +++ linux/fs/aufs/module.h      2022-11-05 23:02:18.969222617 +0100
25996 @@ -0,0 +1,180 @@
25997 +/* SPDX-License-Identifier: GPL-2.0 */
25998 +/*
25999 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26000 + *
26001 + * This program is free software; you can redistribute it and/or modify
26002 + * it under the terms of the GNU General Public License as published by
26003 + * the Free Software Foundation; either version 2 of the License, or
26004 + * (at your option) any later version.
26005 + *
26006 + * This program is distributed in the hope that it will be useful,
26007 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26008 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26009 + * GNU General Public License for more details.
26010 + *
26011 + * You should have received a copy of the GNU General Public License
26012 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26013 + */
26014 +
26015 +/*
26016 + * module initialization and module-global
26017 + */
26018 +
26019 +#ifndef __AUFS_MODULE_H__
26020 +#define __AUFS_MODULE_H__
26021 +
26022 +#ifdef __KERNEL__
26023 +
26024 +#include <linux/slab.h>
26025 +#include "debug.h"
26026 +#include "dentry.h"
26027 +#include "dir.h"
26028 +#include "file.h"
26029 +#include "inode.h"
26030 +
26031 +struct path;
26032 +struct seq_file;
26033 +
26034 +/* module parameters */
26035 +extern int sysaufs_brs;
26036 +extern bool au_userns;
26037 +
26038 +/* ---------------------------------------------------------------------- */
26039 +
26040 +extern int au_dir_roflags;
26041 +
26042 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
26043 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
26044 +                  int may_shrink);
26045 +
26046 +/*
26047 + * Comparing the size of the object with sizeof(struct rcu_head)
26048 + * case 1: object is always larger
26049 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
26050 + * case 2: object is always smaller
26051 + *     --> au_kfree_small()
26052 + * case 3: object can be any size
26053 + *     --> au_kfree_try_rcu()
26054 + */
26055 +
26056 +static inline void au_kfree_do_rcu(const void *p)
26057 +{
26058 +       struct {
26059 +               struct rcu_head rcu;
26060 +       } *a = (void *)p;
26061 +
26062 +       kfree_rcu(a, rcu);
26063 +}
26064 +
26065 +#define au_kfree_rcu(_p) do {                                          \
26066 +               typeof(_p) p = (_p);                                    \
26067 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
26068 +               if (p)                                                  \
26069 +                       au_kfree_do_rcu(p);                             \
26070 +       } while (0)
26071 +
26072 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
26073 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
26074 +
26075 +static inline void au_kfree_try_rcu(const void *p)
26076 +{
26077 +       if (!p)
26078 +               return;
26079 +       if (au_kfree_sz_test(p))
26080 +               au_kfree_do_rcu(p);
26081 +       else
26082 +               kfree(p);
26083 +}
26084 +
26085 +static inline void au_kfree_small(const void *p)
26086 +{
26087 +       if (!p)
26088 +               return;
26089 +       AuDebugOn(au_kfree_sz_test(p));
26090 +       kfree(p);
26091 +}
26092 +
26093 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26094 +{
26095 +#ifndef CONFIG_SLOB
26096 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
26097 +#else
26098 +       return -1; /* SLOB is untested */
26099 +#endif
26100 +}
26101 +
26102 +int au_seq_path(struct seq_file *seq, struct path *path);
26103 +
26104 +#ifdef CONFIG_PROC_FS
26105 +/* procfs.c */
26106 +int __init au_procfs_init(void);
26107 +void au_procfs_fin(void);
26108 +#else
26109 +AuStubInt0(au_procfs_init, void);
26110 +AuStubVoid(au_procfs_fin, void);
26111 +#endif
26112 +
26113 +/* ---------------------------------------------------------------------- */
26114 +
26115 +/* kmem cache */
26116 +enum {
26117 +       AuCache_DINFO,
26118 +       AuCache_ICNTNR,
26119 +       AuCache_FINFO,
26120 +       AuCache_VDIR,
26121 +       AuCache_DEHSTR,
26122 +       AuCache_HNOTIFY, /* must be last */
26123 +       AuCache_Last
26124 +};
26125 +
26126 +extern struct kmem_cache *au_cache[AuCache_Last];
26127 +
26128 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26129 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
26130 +#define AuCacheCtor(type, ctor)        \
26131 +       kmem_cache_create(#type, sizeof(struct type), \
26132 +                         __alignof__(struct type), AuCacheFlags, ctor)
26133 +
26134 +#define AuCacheFuncAlloc(name, index)                                  \
26135 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
26136 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
26137 +
26138 +#define AuCacheFuncs(name, index)                                      \
26139 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26140 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
26141 +                                                                       \
26142 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26143 +       { void *p = rcu;                                                \
26144 +               p -= offsetof(struct au_##name, rcu);                   \
26145 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
26146 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26147 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26148 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
26149 +                                                                       \
26150 +       static inline void au_cache_free_##name(struct au_##name *p)    \
26151 +       { /* au_cache_free_##name##_norcu(p); */                        \
26152 +               au_cache_free_##name##_rcu(p); }
26153 +
26154 +AuCacheFuncs(dinfo, DINFO);
26155 +AuCacheFuncAlloc(dinfo, DINFO);
26156 +
26157 +AuCacheFuncs(icntnr, ICNTNR);
26158 +static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
26159 +{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
26160 +
26161 +AuCacheFuncs(finfo, FINFO);
26162 +AuCacheFuncAlloc(finfo, FINFO);
26163 +
26164 +AuCacheFuncs(vdir, VDIR);
26165 +AuCacheFuncAlloc(vdir, VDIR);
26166 +
26167 +AuCacheFuncs(vdir_dehstr, DEHSTR);
26168 +AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
26169 +
26170 +#ifdef CONFIG_AUFS_HNOTIFY
26171 +AuCacheFuncs(hnotify, HNOTIFY);
26172 +AuCacheFuncAlloc(hnotify, HNOTIFY);
26173 +#endif
26174 +
26175 +#endif /* __KERNEL__ */
26176 +#endif /* __AUFS_MODULE_H__ */
26177 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
26178 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
26179 +++ linux/fs/aufs/mvdown.c      2022-11-05 23:02:18.969222617 +0100
26180 @@ -0,0 +1,706 @@
26181 +// SPDX-License-Identifier: GPL-2.0
26182 +/*
26183 + * Copyright (C) 2011-2022 Junjiro R. Okajima
26184 + *
26185 + * This program is free software; you can redistribute it and/or modify
26186 + * it under the terms of the GNU General Public License as published by
26187 + * the Free Software Foundation; either version 2 of the License, or
26188 + * (at your option) any later version.
26189 + *
26190 + * This program is distributed in the hope that it will be useful,
26191 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26192 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26193 + * GNU General Public License for more details.
26194 + *
26195 + * You should have received a copy of the GNU General Public License
26196 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26197 + */
26198 +
26199 +/*
26200 + * move-down, opposite of copy-up
26201 + */
26202 +
26203 +#include "aufs.h"
26204 +
26205 +struct au_mvd_args {
26206 +       struct {
26207 +               struct super_block *h_sb;
26208 +               struct dentry *h_parent;
26209 +               struct au_hinode *hdir;
26210 +               struct inode *h_dir, *h_inode;
26211 +               struct au_pin pin;
26212 +       } info[AUFS_MVDOWN_NARRAY];
26213 +
26214 +       struct aufs_mvdown mvdown;
26215 +       struct dentry *dentry, *parent;
26216 +       struct inode *inode, *dir;
26217 +       struct super_block *sb;
26218 +       aufs_bindex_t bopq, bwh, bfound;
26219 +       unsigned char rename_lock;
26220 +};
26221 +
26222 +#define mvd_errno              mvdown.au_errno
26223 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26224 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26225 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26226 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
26227 +
26228 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
26229 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
26230 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
26231 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
26232 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
26233 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
26234 +
26235 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
26236 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
26237 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
26238 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
26239 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
26240 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
26241 +
26242 +#define AU_MVD_PR(flag, ...) do {                      \
26243 +               if (flag)                               \
26244 +                       pr_err(__VA_ARGS__);            \
26245 +       } while (0)
26246 +
26247 +static int find_lower_writable(struct au_mvd_args *a)
26248 +{
26249 +       struct super_block *sb;
26250 +       aufs_bindex_t bindex, bbot;
26251 +       struct au_branch *br;
26252 +
26253 +       sb = a->sb;
26254 +       bindex = a->mvd_bsrc;
26255 +       bbot = au_sbbot(sb);
26256 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
26257 +               for (bindex++; bindex <= bbot; bindex++) {
26258 +                       br = au_sbr(sb, bindex);
26259 +                       if (au_br_fhsm(br->br_perm)
26260 +                           && !sb_rdonly(au_br_sb(br)))
26261 +                               return bindex;
26262 +               }
26263 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
26264 +               for (bindex++; bindex <= bbot; bindex++) {
26265 +                       br = au_sbr(sb, bindex);
26266 +                       if (!au_br_rdonly(br))
26267 +                               return bindex;
26268 +               }
26269 +       else
26270 +               for (bindex++; bindex <= bbot; bindex++) {
26271 +                       br = au_sbr(sb, bindex);
26272 +                       if (!sb_rdonly(au_br_sb(br))) {
26273 +                               if (au_br_rdonly(br))
26274 +                                       a->mvdown.flags
26275 +                                               |= AUFS_MVDOWN_ROLOWER_R;
26276 +                               return bindex;
26277 +                       }
26278 +               }
26279 +
26280 +       return -1;
26281 +}
26282 +
26283 +/* make the parent dir on bdst */
26284 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
26285 +{
26286 +       int err;
26287 +
26288 +       err = 0;
26289 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26290 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26291 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26292 +       a->mvd_h_dst_parent = NULL;
26293 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
26294 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26295 +       if (!a->mvd_h_dst_parent) {
26296 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26297 +               if (unlikely(err)) {
26298 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
26299 +                       goto out;
26300 +               }
26301 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26302 +       }
26303 +
26304 +out:
26305 +       AuTraceErr(err);
26306 +       return err;
26307 +}
26308 +
26309 +/* lock them all */
26310 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
26311 +{
26312 +       int err;
26313 +       struct dentry *h_trap;
26314 +
26315 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26316 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
26317 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26318 +                    au_opt_udba(a->sb),
26319 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26320 +       AuTraceErr(err);
26321 +       if (unlikely(err)) {
26322 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
26323 +               goto out;
26324 +       }
26325 +
26326 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26327 +               a->rename_lock = 0;
26328 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26329 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26330 +                           au_opt_udba(a->sb),
26331 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26332 +               err = au_do_pin(&a->mvd_pin_src);
26333 +               AuTraceErr(err);
26334 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26335 +               if (unlikely(err)) {
26336 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
26337 +                       goto out_dst;
26338 +               }
26339 +               goto out; /* success */
26340 +       }
26341 +
26342 +       a->rename_lock = 1;
26343 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
26344 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26345 +                    au_opt_udba(a->sb),
26346 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26347 +       AuTraceErr(err);
26348 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26349 +       if (unlikely(err)) {
26350 +               AU_MVD_PR(dmsg, "pin_src failed\n");
26351 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26352 +               goto out_dst;
26353 +       }
26354 +       au_pin_hdir_unlock(&a->mvd_pin_src);
26355 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26356 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
26357 +       if (h_trap) {
26358 +               err = (h_trap != a->mvd_h_src_parent);
26359 +               if (err)
26360 +                       err = (h_trap != a->mvd_h_dst_parent);
26361 +       }
26362 +       BUG_ON(err); /* it should never happen */
26363 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26364 +               err = -EBUSY;
26365 +               AuTraceErr(err);
26366 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26367 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26368 +               au_pin_hdir_lock(&a->mvd_pin_src);
26369 +               au_unpin(&a->mvd_pin_src);
26370 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26371 +               goto out_dst;
26372 +       }
26373 +       goto out; /* success */
26374 +
26375 +out_dst:
26376 +       au_unpin(&a->mvd_pin_dst);
26377 +out:
26378 +       AuTraceErr(err);
26379 +       return err;
26380 +}
26381 +
26382 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
26383 +{
26384 +       if (!a->rename_lock)
26385 +               au_unpin(&a->mvd_pin_src);
26386 +       else {
26387 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26388 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26389 +               au_pin_hdir_lock(&a->mvd_pin_src);
26390 +               au_unpin(&a->mvd_pin_src);
26391 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26392 +       }
26393 +       au_unpin(&a->mvd_pin_dst);
26394 +}
26395 +
26396 +/* copy-down the file */
26397 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
26398 +{
26399 +       int err;
26400 +       struct au_cp_generic cpg = {
26401 +               .dentry = a->dentry,
26402 +               .bdst   = a->mvd_bdst,
26403 +               .bsrc   = a->mvd_bsrc,
26404 +               .len    = -1,
26405 +               .pin    = &a->mvd_pin_dst,
26406 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
26407 +       };
26408 +
26409 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
26410 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26411 +               au_fset_cpup(cpg.flags, OVERWRITE);
26412 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26413 +               au_fset_cpup(cpg.flags, RWDST);
26414 +       err = au_sio_cpdown_simple(&cpg);
26415 +       if (unlikely(err))
26416 +               AU_MVD_PR(dmsg, "cpdown failed\n");
26417 +
26418 +       AuTraceErr(err);
26419 +       return err;
26420 +}
26421 +
26422 +/*
26423 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
26424 + * were sleeping
26425 + */
26426 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
26427 +{
26428 +       int err;
26429 +       struct path h_path;
26430 +       struct au_branch *br;
26431 +       struct inode *delegated;
26432 +
26433 +       br = au_sbr(a->sb, a->mvd_bdst);
26434 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26435 +       err = PTR_ERR(h_path.dentry);
26436 +       if (IS_ERR(h_path.dentry)) {
26437 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
26438 +               goto out;
26439 +       }
26440 +
26441 +       err = 0;
26442 +       if (d_is_positive(h_path.dentry)) {
26443 +               h_path.mnt = au_br_mnt(br);
26444 +               delegated = NULL;
26445 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
26446 +                                  &delegated, /*force*/0);
26447 +               if (unlikely(err == -EWOULDBLOCK)) {
26448 +                       pr_warn("cannot retry for NFSv4 delegation"
26449 +                               " for an internal unlink\n");
26450 +                       iput(delegated);
26451 +               }
26452 +               if (unlikely(err))
26453 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
26454 +       }
26455 +       dput(h_path.dentry);
26456 +
26457 +out:
26458 +       AuTraceErr(err);
26459 +       return err;
26460 +}
26461 +
26462 +/*
26463 + * unlink the topmost h_dentry
26464 + */
26465 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
26466 +{
26467 +       int err;
26468 +       struct path h_path;
26469 +       struct inode *delegated;
26470 +
26471 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26472 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26473 +       delegated = NULL;
26474 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26475 +       if (unlikely(err == -EWOULDBLOCK)) {
26476 +               pr_warn("cannot retry for NFSv4 delegation"
26477 +                       " for an internal unlink\n");
26478 +               iput(delegated);
26479 +       }
26480 +       if (unlikely(err))
26481 +               AU_MVD_PR(dmsg, "unlink failed\n");
26482 +
26483 +       AuTraceErr(err);
26484 +       return err;
26485 +}
26486 +
26487 +/* Since mvdown succeeded, we ignore an error of this function */
26488 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26489 +{
26490 +       int err;
26491 +       struct au_branch *br;
26492 +
26493 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26494 +       br = au_sbr(a->sb, a->mvd_bsrc);
26495 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26496 +       if (!err) {
26497 +               br = au_sbr(a->sb, a->mvd_bdst);
26498 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26499 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26500 +       }
26501 +       if (!err)
26502 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26503 +       else
26504 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26505 +}
26506 +
26507 +/*
26508 + * copy-down the file and unlink the bsrc file.
26509 + * - unlink the bdst whout if exist
26510 + * - copy-down the file (with whtmp name and rename)
26511 + * - unlink the bsrc file
26512 + */
26513 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26514 +{
26515 +       int err;
26516 +
26517 +       err = au_do_mkdir(dmsg, a);
26518 +       if (!err)
26519 +               err = au_do_lock(dmsg, a);
26520 +       if (unlikely(err))
26521 +               goto out;
26522 +
26523 +       /*
26524 +        * do not revert the activities we made on bdst since they should be
26525 +        * harmless in aufs.
26526 +        */
26527 +
26528 +       err = au_do_cpdown(dmsg, a);
26529 +       if (!err)
26530 +               err = au_do_unlink_wh(dmsg, a);
26531 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26532 +               err = au_do_unlink(dmsg, a);
26533 +       if (unlikely(err))
26534 +               goto out_unlock;
26535 +
26536 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
26537 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26538 +       if (find_lower_writable(a) < 0)
26539 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26540 +
26541 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26542 +               au_do_stfs(dmsg, a);
26543 +
26544 +       /* maintain internal array */
26545 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26546 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26547 +               au_set_dbtop(a->dentry, a->mvd_bdst);
26548 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26549 +               au_set_ibtop(a->inode, a->mvd_bdst);
26550 +       } else {
26551 +               /* hide the lower */
26552 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26553 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
26554 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26555 +               au_set_ibbot(a->inode, a->mvd_bsrc);
26556 +       }
26557 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
26558 +               au_set_dbbot(a->dentry, a->mvd_bdst);
26559 +       if (au_ibbot(a->inode) < a->mvd_bdst)
26560 +               au_set_ibbot(a->inode, a->mvd_bdst);
26561 +
26562 +out_unlock:
26563 +       au_do_unlock(dmsg, a);
26564 +out:
26565 +       AuTraceErr(err);
26566 +       return err;
26567 +}
26568 +
26569 +/* ---------------------------------------------------------------------- */
26570 +
26571 +/* make sure the file is idle */
26572 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26573 +{
26574 +       int err, plinked;
26575 +
26576 +       err = 0;
26577 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26578 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
26579 +           && au_dcount(a->dentry) == 1
26580 +           && atomic_read(&a->inode->i_count) == 1
26581 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
26582 +           && (!plinked || !au_plink_test(a->inode))
26583 +           && a->inode->i_nlink == 1)
26584 +               goto out;
26585 +
26586 +       err = -EBUSY;
26587 +       AU_MVD_PR(dmsg,
26588 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26589 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26590 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
26591 +                 a->mvd_h_src_inode->i_nlink,
26592 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
26593 +
26594 +out:
26595 +       AuTraceErr(err);
26596 +       return err;
26597 +}
26598 +
26599 +/* make sure the parent dir is fine */
26600 +static int au_mvd_args_parent(const unsigned char dmsg,
26601 +                             struct au_mvd_args *a)
26602 +{
26603 +       int err;
26604 +       aufs_bindex_t bindex;
26605 +
26606 +       err = 0;
26607 +       if (unlikely(au_alive_dir(a->parent))) {
26608 +               err = -ENOENT;
26609 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
26610 +               goto out;
26611 +       }
26612 +
26613 +       a->bopq = au_dbdiropq(a->parent);
26614 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26615 +       AuDbg("b%d\n", bindex);
26616 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26617 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26618 +               err = -EINVAL;
26619 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
26620 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26621 +                         a->bopq, a->mvd_bdst);
26622 +       }
26623 +
26624 +out:
26625 +       AuTraceErr(err);
26626 +       return err;
26627 +}
26628 +
26629 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26630 +                                   struct au_mvd_args *a)
26631 +{
26632 +       int err;
26633 +       struct au_dinfo *dinfo, *tmp;
26634 +
26635 +       /* lookup the next lower positive entry */
26636 +       err = -ENOMEM;
26637 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26638 +       if (unlikely(!tmp))
26639 +               goto out;
26640 +
26641 +       a->bfound = -1;
26642 +       a->bwh = -1;
26643 +       dinfo = au_di(a->dentry);
26644 +       au_di_cp(tmp, dinfo);
26645 +       au_di_swap(tmp, dinfo);
26646 +
26647 +       /* returns the number of positive dentries */
26648 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26649 +                            /* AuLkup_IGNORE_PERM */ 0);
26650 +       if (!err)
26651 +               a->bwh = au_dbwh(a->dentry);
26652 +       else if (err > 0)
26653 +               a->bfound = au_dbtop(a->dentry);
26654 +
26655 +       au_di_swap(tmp, dinfo);
26656 +       au_rw_write_unlock(&tmp->di_rwsem);
26657 +       au_di_free(tmp);
26658 +       if (unlikely(err < 0))
26659 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
26660 +
26661 +       /*
26662 +        * here, we have these cases.
26663 +        * bfound == -1
26664 +        *      no positive dentry under bsrc. there are more sub-cases.
26665 +        *      bwh < 0
26666 +        *              there no whiteout, we can safely move-down.
26667 +        *      bwh <= bsrc
26668 +        *              impossible
26669 +        *      bsrc < bwh && bwh < bdst
26670 +        *              there is a whiteout on RO branch. cannot proceed.
26671 +        *      bwh == bdst
26672 +        *              there is a whiteout on the RW target branch. it should
26673 +        *              be removed.
26674 +        *      bdst < bwh
26675 +        *              there is a whiteout somewhere unrelated branch.
26676 +        * -1 < bfound && bfound <= bsrc
26677 +        *      impossible.
26678 +        * bfound < bdst
26679 +        *      found, but it is on RO branch between bsrc and bdst. cannot
26680 +        *      proceed.
26681 +        * bfound == bdst
26682 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26683 +        *      error.
26684 +        * bdst < bfound
26685 +        *      found, after we create the file on bdst, it will be hidden.
26686 +        */
26687 +
26688 +       AuDebugOn(a->bfound == -1
26689 +                 && a->bwh != -1
26690 +                 && a->bwh <= a->mvd_bsrc);
26691 +       AuDebugOn(-1 < a->bfound
26692 +                 && a->bfound <= a->mvd_bsrc);
26693 +
26694 +       err = -EINVAL;
26695 +       if (a->bfound == -1
26696 +           && a->mvd_bsrc < a->bwh
26697 +           && a->bwh != -1
26698 +           && a->bwh < a->mvd_bdst) {
26699 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26700 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26701 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26702 +               goto out;
26703 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26704 +               a->mvd_errno = EAU_MVDOWN_UPPER;
26705 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26706 +                         a->mvd_bdst, a->bfound);
26707 +               goto out;
26708 +       }
26709 +
26710 +       err = 0; /* success */
26711 +
26712 +out:
26713 +       AuTraceErr(err);
26714 +       return err;
26715 +}
26716 +
26717 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26718 +{
26719 +       int err;
26720 +
26721 +       err = 0;
26722 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26723 +           && a->bfound == a->mvd_bdst)
26724 +               err = -EEXIST;
26725 +       AuTraceErr(err);
26726 +       return err;
26727 +}
26728 +
26729 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26730 +{
26731 +       int err;
26732 +       struct au_branch *br;
26733 +
26734 +       err = -EISDIR;
26735 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
26736 +               goto out;
26737 +
26738 +       err = -EINVAL;
26739 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26740 +               a->mvd_bsrc = au_ibtop(a->inode);
26741 +       else {
26742 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26743 +               if (unlikely(a->mvd_bsrc < 0
26744 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
26745 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
26746 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
26747 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
26748 +                                || au_ibbot(a->inode) < a->mvd_bsrc
26749 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26750 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
26751 +                       AU_MVD_PR(dmsg, "no upper\n");
26752 +                       goto out;
26753 +               }
26754 +       }
26755 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26756 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
26757 +               AU_MVD_PR(dmsg, "on the bottom\n");
26758 +               goto out;
26759 +       }
26760 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26761 +       br = au_sbr(a->sb, a->mvd_bsrc);
26762 +       err = au_br_rdonly(br);
26763 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26764 +               if (unlikely(err))
26765 +                       goto out;
26766 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26767 +                    || IS_APPEND(a->mvd_h_src_inode))) {
26768 +               if (err)
26769 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26770 +               /* go on */
26771 +       } else
26772 +               goto out;
26773 +
26774 +       err = -EINVAL;
26775 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26776 +               a->mvd_bdst = find_lower_writable(a);
26777 +               if (unlikely(a->mvd_bdst < 0)) {
26778 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
26779 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
26780 +                       goto out;
26781 +               }
26782 +       } else {
26783 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26784 +               if (unlikely(a->mvd_bdst < 0
26785 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
26786 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26787 +                       AU_MVD_PR(dmsg, "no lower brid\n");
26788 +                       goto out;
26789 +               }
26790 +       }
26791 +
26792 +       err = au_mvd_args_busy(dmsg, a);
26793 +       if (!err)
26794 +               err = au_mvd_args_parent(dmsg, a);
26795 +       if (!err)
26796 +               err = au_mvd_args_intermediate(dmsg, a);
26797 +       if (!err)
26798 +               err = au_mvd_args_exist(dmsg, a);
26799 +       if (!err)
26800 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26801 +
26802 +out:
26803 +       AuTraceErr(err);
26804 +       return err;
26805 +}
26806 +
26807 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26808 +{
26809 +       int err, e;
26810 +       unsigned char dmsg;
26811 +       struct au_mvd_args *args;
26812 +       struct inode *inode;
26813 +
26814 +       inode = d_inode(dentry);
26815 +       err = -EPERM;
26816 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26817 +               goto out;
26818 +
26819 +       err = -ENOMEM;
26820 +       args = kmalloc(sizeof(*args), GFP_NOFS);
26821 +       if (unlikely(!args))
26822 +               goto out;
26823 +
26824 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26825 +       if (!err)
26826 +               /* VERIFY_WRITE */
26827 +               err = !access_ok(uarg, sizeof(*uarg));
26828 +       if (unlikely(err)) {
26829 +               err = -EFAULT;
26830 +               AuTraceErr(err);
26831 +               goto out_free;
26832 +       }
26833 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
26834 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26835 +       args->mvdown.au_errno = 0;
26836 +       args->dentry = dentry;
26837 +       args->inode = inode;
26838 +       args->sb = dentry->d_sb;
26839 +
26840 +       err = -ENOENT;
26841 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26842 +       args->parent = dget_parent(dentry);
26843 +       args->dir = d_inode(args->parent);
26844 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
26845 +       dput(args->parent);
26846 +       if (unlikely(args->parent != dentry->d_parent)) {
26847 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
26848 +               goto out_dir;
26849 +       }
26850 +
26851 +       inode_lock_nested(inode, I_MUTEX_CHILD);
26852 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26853 +       if (unlikely(err))
26854 +               goto out_inode;
26855 +
26856 +       di_write_lock_parent(args->parent);
26857 +       err = au_mvd_args(dmsg, args);
26858 +       if (unlikely(err))
26859 +               goto out_parent;
26860 +
26861 +       err = au_do_mvdown(dmsg, args);
26862 +       if (unlikely(err))
26863 +               goto out_parent;
26864 +
26865 +       au_cpup_attr_timesizes(args->dir);
26866 +       au_cpup_attr_timesizes(inode);
26867 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26868 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26869 +       /* au_digen_dec(dentry); */
26870 +
26871 +out_parent:
26872 +       di_write_unlock(args->parent);
26873 +       aufs_read_unlock(dentry, AuLock_DW);
26874 +out_inode:
26875 +       inode_unlock(inode);
26876 +out_dir:
26877 +       inode_unlock(args->dir);
26878 +out_free:
26879 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26880 +       if (unlikely(e))
26881 +               err = -EFAULT;
26882 +       au_kfree_rcu(args);
26883 +out:
26884 +       AuTraceErr(err);
26885 +       return err;
26886 +}
26887 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
26888 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
26889 +++ linux/fs/aufs/opts.c        2022-11-05 23:02:18.969222617 +0100
26890 @@ -0,0 +1,1032 @@
26891 +// SPDX-License-Identifier: GPL-2.0
26892 +/*
26893 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26894 + *
26895 + * This program is free software; you can redistribute it and/or modify
26896 + * it under the terms of the GNU General Public License as published by
26897 + * the Free Software Foundation; either version 2 of the License, or
26898 + * (at your option) any later version.
26899 + *
26900 + * This program is distributed in the hope that it will be useful,
26901 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26902 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26903 + * GNU General Public License for more details.
26904 + *
26905 + * You should have received a copy of the GNU General Public License
26906 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26907 + */
26908 +
26909 +/*
26910 + * mount options/flags
26911 + */
26912 +
26913 +#include <linux/types.h> /* a distribution requires */
26914 +#include <linux/parser.h>
26915 +#include "aufs.h"
26916 +
26917 +/* ---------------------------------------------------------------------- */
26918 +
26919 +static const char *au_parser_pattern(int val, match_table_t tbl)
26920 +{
26921 +       struct match_token *p;
26922 +
26923 +       p = tbl;
26924 +       while (p->pattern) {
26925 +               if (p->token == val)
26926 +                       return p->pattern;
26927 +               p++;
26928 +       }
26929 +       BUG();
26930 +       return "??";
26931 +}
26932 +
26933 +static const char *au_optstr(int *val, match_table_t tbl)
26934 +{
26935 +       struct match_token *p;
26936 +       int v;
26937 +
26938 +       v = *val;
26939 +       if (!v)
26940 +               goto out;
26941 +       p = tbl;
26942 +       while (p->pattern) {
26943 +               if (p->token
26944 +                   && (v & p->token) == p->token) {
26945 +                       *val &= ~p->token;
26946 +                       return p->pattern;
26947 +               }
26948 +               p++;
26949 +       }
26950 +
26951 +out:
26952 +       return NULL;
26953 +}
26954 +
26955 +/* ---------------------------------------------------------------------- */
26956 +
26957 +static match_table_t brperm = {
26958 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
26959 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
26960 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
26961 +       {0, NULL}
26962 +};
26963 +
26964 +static match_table_t brattr = {
26965 +       /* general */
26966 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26967 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
26968 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
26969 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
26970 +#ifdef CONFIG_AUFS_FHSM
26971 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
26972 +#endif
26973 +#ifdef CONFIG_AUFS_XATTR
26974 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26975 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26976 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26977 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26978 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26979 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
26980 +#endif
26981 +
26982 +       /* ro/rr branch */
26983 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
26984 +
26985 +       /* rw branch */
26986 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
26987 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
26988 +
26989 +       {0, NULL}
26990 +};
26991 +
26992 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
26993 +{
26994 +       int attr, v;
26995 +       char *p;
26996 +
26997 +       attr = 0;
26998 +       do {
26999 +               p = strchr(str, '+');
27000 +               if (p)
27001 +                       *p = 0;
27002 +               v = match_token(str, table, args);
27003 +               if (v) {
27004 +                       if (v & AuBrAttr_CMOO_Mask)
27005 +                               attr &= ~AuBrAttr_CMOO_Mask;
27006 +                       attr |= v;
27007 +               } else {
27008 +                       if (p)
27009 +                               *p = '+';
27010 +                       pr_warn("ignored branch attribute %s\n", str);
27011 +                       break;
27012 +               }
27013 +               if (p)
27014 +                       str = p + 1;
27015 +       } while (p);
27016 +
27017 +       return attr;
27018 +}
27019 +
27020 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
27021 +{
27022 +       int sz;
27023 +       const char *p;
27024 +       char *q;
27025 +
27026 +       q = str->a;
27027 +       *q = 0;
27028 +       p = au_optstr(&perm, brattr);
27029 +       if (p) {
27030 +               sz = strlen(p);
27031 +               memcpy(q, p, sz + 1);
27032 +               q += sz;
27033 +       } else
27034 +               goto out;
27035 +
27036 +       do {
27037 +               p = au_optstr(&perm, brattr);
27038 +               if (p) {
27039 +                       *q++ = '+';
27040 +                       sz = strlen(p);
27041 +                       memcpy(q, p, sz + 1);
27042 +                       q += sz;
27043 +               }
27044 +       } while (p);
27045 +
27046 +out:
27047 +       return q - str->a;
27048 +}
27049 +
27050 +int au_br_perm_val(char *perm)
27051 +{
27052 +       int val, bad, sz;
27053 +       char *p;
27054 +       substring_t args[MAX_OPT_ARGS];
27055 +       au_br_perm_str_t attr;
27056 +
27057 +       p = strchr(perm, '+');
27058 +       if (p)
27059 +               *p = 0;
27060 +       val = match_token(perm, brperm, args);
27061 +       if (!val) {
27062 +               if (p)
27063 +                       *p = '+';
27064 +               pr_warn("ignored branch permission %s\n", perm);
27065 +               val = AuBrPerm_RO;
27066 +               goto out;
27067 +       }
27068 +       if (!p)
27069 +               goto out;
27070 +
27071 +       val |= br_attr_val(p + 1, brattr, args);
27072 +
27073 +       bad = 0;
27074 +       switch (val & AuBrPerm_Mask) {
27075 +       case AuBrPerm_RO:
27076 +       case AuBrPerm_RR:
27077 +               bad = val & AuBrWAttr_Mask;
27078 +               val &= ~AuBrWAttr_Mask;
27079 +               break;
27080 +       case AuBrPerm_RW:
27081 +               bad = val & AuBrRAttr_Mask;
27082 +               val &= ~AuBrRAttr_Mask;
27083 +               break;
27084 +       }
27085 +
27086 +       /*
27087 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27088 +        * does not treat it as an error, just warning.
27089 +        * this is a tiny guard for the user operation.
27090 +        */
27091 +       if (val & AuBrAttr_UNPIN) {
27092 +               bad |= AuBrAttr_UNPIN;
27093 +               val &= ~AuBrAttr_UNPIN;
27094 +       }
27095 +
27096 +       if (unlikely(bad)) {
27097 +               sz = au_do_optstr_br_attr(&attr, bad);
27098 +               AuDebugOn(!sz);
27099 +               pr_warn("ignored branch attribute %s\n", attr.a);
27100 +       }
27101 +
27102 +out:
27103 +       return val;
27104 +}
27105 +
27106 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
27107 +{
27108 +       au_br_perm_str_t attr;
27109 +       const char *p;
27110 +       char *q;
27111 +       int sz;
27112 +
27113 +       q = str->a;
27114 +       p = au_optstr(&perm, brperm);
27115 +       AuDebugOn(!p || !*p);
27116 +       sz = strlen(p);
27117 +       memcpy(q, p, sz + 1);
27118 +       q += sz;
27119 +
27120 +       sz = au_do_optstr_br_attr(&attr, perm);
27121 +       if (sz) {
27122 +               *q++ = '+';
27123 +               memcpy(q, attr.a, sz + 1);
27124 +       }
27125 +
27126 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
27127 +}
27128 +
27129 +/* ---------------------------------------------------------------------- */
27130 +
27131 +static match_table_t udbalevel = {
27132 +       {AuOpt_UDBA_REVAL, "reval"},
27133 +       {AuOpt_UDBA_NONE, "none"},
27134 +#ifdef CONFIG_AUFS_HNOTIFY
27135 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27136 +#ifdef CONFIG_AUFS_HFSNOTIFY
27137 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
27138 +#endif
27139 +#endif
27140 +       {-1, NULL}
27141 +};
27142 +
27143 +int au_udba_val(char *str)
27144 +{
27145 +       substring_t args[MAX_OPT_ARGS];
27146 +
27147 +       return match_token(str, udbalevel, args);
27148 +}
27149 +
27150 +const char *au_optstr_udba(int udba)
27151 +{
27152 +       return au_parser_pattern(udba, udbalevel);
27153 +}
27154 +
27155 +/* ---------------------------------------------------------------------- */
27156 +
27157 +static match_table_t au_wbr_create_policy = {
27158 +       {AuWbrCreate_TDP, "tdp"},
27159 +       {AuWbrCreate_TDP, "top-down-parent"},
27160 +       {AuWbrCreate_RR, "rr"},
27161 +       {AuWbrCreate_RR, "round-robin"},
27162 +       {AuWbrCreate_MFS, "mfs"},
27163 +       {AuWbrCreate_MFS, "most-free-space"},
27164 +       {AuWbrCreate_MFSV, "mfs:%d"},
27165 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
27166 +
27167 +       /* top-down regardless the parent, and then mfs */
27168 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
27169 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27170 +
27171 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
27172 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27173 +       {AuWbrCreate_PMFS, "pmfs"},
27174 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
27175 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27176 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
27177 +
27178 +       {-1, NULL}
27179 +};
27180 +
27181 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27182 +                           struct au_opt_wbr_create *create)
27183 +{
27184 +       int err;
27185 +       unsigned long long ull;
27186 +
27187 +       err = 0;
27188 +       if (!match_u64(arg, &ull))
27189 +               create->mfsrr_watermark = ull;
27190 +       else {
27191 +               pr_err("bad integer in %s\n", str);
27192 +               err = -EINVAL;
27193 +       }
27194 +
27195 +       return err;
27196 +}
27197 +
27198 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
27199 +                         struct au_opt_wbr_create *create)
27200 +{
27201 +       int n, err;
27202 +
27203 +       err = 0;
27204 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
27205 +               create->mfs_second = n;
27206 +       else {
27207 +               pr_err("bad integer in %s\n", str);
27208 +               err = -EINVAL;
27209 +       }
27210 +
27211 +       return err;
27212 +}
27213 +
27214 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
27215 +{
27216 +       int err, e;
27217 +       substring_t args[MAX_OPT_ARGS];
27218 +
27219 +       err = match_token(str, au_wbr_create_policy, args);
27220 +       create->wbr_create = err;
27221 +       switch (err) {
27222 +       case AuWbrCreate_MFSRRV:
27223 +       case AuWbrCreate_TDMFSV:
27224 +       case AuWbrCreate_PMFSRRV:
27225 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27226 +               if (!e)
27227 +                       e = au_wbr_mfs_sec(&args[1], str, create);
27228 +               if (unlikely(e))
27229 +                       err = e;
27230 +               break;
27231 +       case AuWbrCreate_MFSRR:
27232 +       case AuWbrCreate_TDMFS:
27233 +       case AuWbrCreate_PMFSRR:
27234 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27235 +               if (unlikely(e)) {
27236 +                       err = e;
27237 +                       break;
27238 +               }
27239 +               fallthrough;
27240 +       case AuWbrCreate_MFS:
27241 +       case AuWbrCreate_PMFS:
27242 +               create->mfs_second = AUFS_MFS_DEF_SEC;
27243 +               break;
27244 +       case AuWbrCreate_MFSV:
27245 +       case AuWbrCreate_PMFSV:
27246 +               e = au_wbr_mfs_sec(&args[0], str, create);
27247 +               if (unlikely(e))
27248 +                       err = e;
27249 +               break;
27250 +       }
27251 +
27252 +       return err;
27253 +}
27254 +
27255 +const char *au_optstr_wbr_create(int wbr_create)
27256 +{
27257 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
27258 +}
27259 +
27260 +static match_table_t au_wbr_copyup_policy = {
27261 +       {AuWbrCopyup_TDP, "tdp"},
27262 +       {AuWbrCopyup_TDP, "top-down-parent"},
27263 +       {AuWbrCopyup_BUP, "bup"},
27264 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
27265 +       {AuWbrCopyup_BU, "bu"},
27266 +       {AuWbrCopyup_BU, "bottom-up"},
27267 +       {-1, NULL}
27268 +};
27269 +
27270 +int au_wbr_copyup_val(char *str)
27271 +{
27272 +       substring_t args[MAX_OPT_ARGS];
27273 +
27274 +       return match_token(str, au_wbr_copyup_policy, args);
27275 +}
27276 +
27277 +const char *au_optstr_wbr_copyup(int wbr_copyup)
27278 +{
27279 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
27280 +}
27281 +
27282 +/* ---------------------------------------------------------------------- */
27283 +
27284 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27285 +              aufs_bindex_t bindex)
27286 +{
27287 +       int err;
27288 +       struct au_opt_add *add = &opt->add;
27289 +       char *p;
27290 +
27291 +       add->bindex = bindex;
27292 +       add->perm = AuBrPerm_RO;
27293 +       add->pathname = opt_str;
27294 +       p = strchr(opt_str, '=');
27295 +       if (p) {
27296 +               *p++ = 0;
27297 +               if (*p)
27298 +                       add->perm = au_br_perm_val(p);
27299 +       }
27300 +
27301 +       err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
27302 +       if (!err) {
27303 +               if (!p) {
27304 +                       add->perm = AuBrPerm_RO;
27305 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
27306 +                               add->perm = AuBrPerm_RR;
27307 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
27308 +                               add->perm = AuBrPerm_RW;
27309 +               }
27310 +               opt->type = Opt_add;
27311 +               goto out;
27312 +       }
27313 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
27314 +       err = -EINVAL;
27315 +
27316 +out:
27317 +       return err;
27318 +}
27319 +
27320 +static int au_opt_wbr_create(struct super_block *sb,
27321 +                            struct au_opt_wbr_create *create)
27322 +{
27323 +       int err;
27324 +       struct au_sbinfo *sbinfo;
27325 +
27326 +       SiMustWriteLock(sb);
27327 +
27328 +       err = 1; /* handled */
27329 +       sbinfo = au_sbi(sb);
27330 +       if (sbinfo->si_wbr_create_ops->fin) {
27331 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27332 +               if (!err)
27333 +                       err = 1;
27334 +       }
27335 +
27336 +       sbinfo->si_wbr_create = create->wbr_create;
27337 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27338 +       switch (create->wbr_create) {
27339 +       case AuWbrCreate_MFSRRV:
27340 +       case AuWbrCreate_MFSRR:
27341 +       case AuWbrCreate_TDMFS:
27342 +       case AuWbrCreate_TDMFSV:
27343 +       case AuWbrCreate_PMFSRR:
27344 +       case AuWbrCreate_PMFSRRV:
27345 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27346 +               fallthrough;
27347 +       case AuWbrCreate_MFS:
27348 +       case AuWbrCreate_MFSV:
27349 +       case AuWbrCreate_PMFS:
27350 +       case AuWbrCreate_PMFSV:
27351 +               sbinfo->si_wbr_mfs.mfs_expire
27352 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27353 +               break;
27354 +       }
27355 +
27356 +       if (sbinfo->si_wbr_create_ops->init)
27357 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27358 +
27359 +       return err;
27360 +}
27361 +
27362 +/*
27363 + * returns,
27364 + * plus: processed without an error
27365 + * zero: unprocessed
27366 + */
27367 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27368 +                        struct au_opts *opts)
27369 +{
27370 +       int err;
27371 +       struct au_sbinfo *sbinfo;
27372 +
27373 +       SiMustWriteLock(sb);
27374 +
27375 +       err = 1; /* handled */
27376 +       sbinfo = au_sbi(sb);
27377 +       switch (opt->type) {
27378 +       case Opt_udba:
27379 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27380 +               sbinfo->si_mntflags |= opt->udba;
27381 +               opts->given_udba |= opt->udba;
27382 +               break;
27383 +
27384 +       case Opt_plink:
27385 +               if (opt->tf)
27386 +                       au_opt_set(sbinfo->si_mntflags, PLINK);
27387 +               else {
27388 +                       if (au_opt_test(sbinfo->si_mntflags, PLINK))
27389 +                               au_plink_put(sb, /*verbose*/1);
27390 +                       au_opt_clr(sbinfo->si_mntflags, PLINK);
27391 +               }
27392 +               break;
27393 +       case Opt_list_plink:
27394 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27395 +                       au_plink_list(sb);
27396 +               break;
27397 +
27398 +       case Opt_dio:
27399 +               if (opt->tf) {
27400 +                       au_opt_set(sbinfo->si_mntflags, DIO);
27401 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27402 +               } else {
27403 +                       au_opt_clr(sbinfo->si_mntflags, DIO);
27404 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27405 +               }
27406 +               break;
27407 +
27408 +       case Opt_fhsm_sec:
27409 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27410 +               break;
27411 +
27412 +       case Opt_diropq_a:
27413 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27414 +               break;
27415 +       case Opt_diropq_w:
27416 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27417 +               break;
27418 +
27419 +       case Opt_warn_perm:
27420 +               if (opt->tf)
27421 +                       au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27422 +               else
27423 +                       au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27424 +               break;
27425 +
27426 +       case Opt_verbose:
27427 +               if (opt->tf)
27428 +                       au_opt_set(sbinfo->si_mntflags, VERBOSE);
27429 +               else
27430 +                       au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27431 +               break;
27432 +
27433 +       case Opt_sum:
27434 +               if (opt->tf)
27435 +                       au_opt_set(sbinfo->si_mntflags, SUM);
27436 +               else {
27437 +                       au_opt_clr(sbinfo->si_mntflags, SUM);
27438 +                       au_opt_clr(sbinfo->si_mntflags, SUM_W);
27439 +               }
27440 +               break;
27441 +       case Opt_wsum:
27442 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27443 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27444 +               break;
27445 +
27446 +       case Opt_wbr_create:
27447 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27448 +               break;
27449 +       case Opt_wbr_copyup:
27450 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27451 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27452 +               break;
27453 +
27454 +       case Opt_dirwh:
27455 +               sbinfo->si_dirwh = opt->dirwh;
27456 +               break;
27457 +
27458 +       case Opt_rdcache:
27459 +               sbinfo->si_rdcache
27460 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27461 +               break;
27462 +       case Opt_rdblk:
27463 +               sbinfo->si_rdblk = opt->rdblk;
27464 +               break;
27465 +       case Opt_rdhash:
27466 +               sbinfo->si_rdhash = opt->rdhash;
27467 +               break;
27468 +
27469 +       case Opt_shwh:
27470 +               if (opt->tf)
27471 +                       au_opt_set(sbinfo->si_mntflags, SHWH);
27472 +               else
27473 +                       au_opt_clr(sbinfo->si_mntflags, SHWH);
27474 +               break;
27475 +
27476 +       case Opt_dirperm1:
27477 +               if (opt->tf)
27478 +                       au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27479 +               else
27480 +                       au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27481 +               break;
27482 +
27483 +       case Opt_trunc_xino:
27484 +               if (opt->tf)
27485 +                       au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27486 +               else
27487 +                       au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27488 +               break;
27489 +
27490 +       case Opt_trunc_xino_path:
27491 +       case Opt_itrunc_xino:
27492 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27493 +                                   /*idx_begin*/0);
27494 +               if (!err)
27495 +                       err = 1;
27496 +               break;
27497 +
27498 +       case Opt_trunc_xib:
27499 +               if (opt->tf)
27500 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27501 +               else
27502 +                       au_fclr_opts(opts->flags, TRUNC_XIB);
27503 +               break;
27504 +
27505 +       case Opt_dirren:
27506 +               err = 1;
27507 +               if (opt->tf) {
27508 +                       if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27509 +                               err = au_dr_opt_set(sb);
27510 +                               if (!err)
27511 +                                       err = 1;
27512 +                       }
27513 +                       if (err == 1)
27514 +                               au_opt_set(sbinfo->si_mntflags, DIRREN);
27515 +               } else {
27516 +                       if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27517 +                               err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27518 +                                                                     DR_FLUSHED));
27519 +                               if (!err)
27520 +                                       err = 1;
27521 +                       }
27522 +                       if (err == 1)
27523 +                               au_opt_clr(sbinfo->si_mntflags, DIRREN);
27524 +               }
27525 +               break;
27526 +
27527 +       case Opt_acl:
27528 +               if (opt->tf)
27529 +                       sb->s_flags |= SB_POSIXACL;
27530 +               else
27531 +                       sb->s_flags &= ~SB_POSIXACL;
27532 +               break;
27533 +
27534 +       default:
27535 +               err = 0;
27536 +               break;
27537 +       }
27538 +
27539 +       return err;
27540 +}
27541 +
27542 +/*
27543 + * returns tri-state.
27544 + * plus: processed without an error
27545 + * zero: unprocessed
27546 + * minus: error
27547 + */
27548 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27549 +                    struct au_opts *opts)
27550 +{
27551 +       int err, do_refresh;
27552 +
27553 +       err = 0;
27554 +       switch (opt->type) {
27555 +       case Opt_append:
27556 +               opt->add.bindex = au_sbbot(sb) + 1;
27557 +               if (opt->add.bindex < 0)
27558 +                       opt->add.bindex = 0;
27559 +               goto add;
27560 +               /* Always goto add, not fallthrough */
27561 +       case Opt_prepend:
27562 +               opt->add.bindex = 0;
27563 +               fallthrough;
27564 +       add: /* indented label */
27565 +       case Opt_add:
27566 +               err = au_br_add(sb, &opt->add,
27567 +                               au_ftest_opts(opts->flags, REMOUNT));
27568 +               if (!err) {
27569 +                       err = 1;
27570 +                       au_fset_opts(opts->flags, REFRESH);
27571 +               }
27572 +               break;
27573 +
27574 +       case Opt_del:
27575 +       case Opt_idel:
27576 +               err = au_br_del(sb, &opt->del,
27577 +                               au_ftest_opts(opts->flags, REMOUNT));
27578 +               if (!err) {
27579 +                       err = 1;
27580 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27581 +                       au_fset_opts(opts->flags, REFRESH);
27582 +               }
27583 +               break;
27584 +
27585 +       case Opt_mod:
27586 +       case Opt_imod:
27587 +               err = au_br_mod(sb, &opt->mod,
27588 +                               au_ftest_opts(opts->flags, REMOUNT),
27589 +                               &do_refresh);
27590 +               if (!err) {
27591 +                       err = 1;
27592 +                       if (do_refresh)
27593 +                               au_fset_opts(opts->flags, REFRESH);
27594 +               }
27595 +               break;
27596 +       }
27597 +       return err;
27598 +}
27599 +
27600 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27601 +                      struct au_opt_xino **opt_xino,
27602 +                      struct au_opts *opts)
27603 +{
27604 +       int err;
27605 +
27606 +       err = 0;
27607 +       switch (opt->type) {
27608 +       case Opt_xino:
27609 +               err = au_xino_set(sb, &opt->xino,
27610 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27611 +               if (!err)
27612 +                       *opt_xino = &opt->xino;
27613 +               break;
27614 +       case Opt_noxino:
27615 +               au_xino_clr(sb);
27616 +               *opt_xino = (void *)-1;
27617 +               break;
27618 +       }
27619 +
27620 +       return err;
27621 +}
27622 +
27623 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27624 +                  unsigned int pending)
27625 +{
27626 +       int err, fhsm;
27627 +       aufs_bindex_t bindex, bbot;
27628 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27629 +       struct au_branch *br;
27630 +       struct au_wbr *wbr;
27631 +       struct dentry *root, *dentry;
27632 +       struct inode *dir, *h_dir;
27633 +       struct au_sbinfo *sbinfo;
27634 +       struct au_hinode *hdir;
27635 +
27636 +       SiMustAnyLock(sb);
27637 +
27638 +       sbinfo = au_sbi(sb);
27639 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27640 +
27641 +       if (!(sb_flags & SB_RDONLY)) {
27642 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27643 +                       pr_warn("first branch should be rw\n");
27644 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27645 +                       pr_warn_once("shwh should be used with ro\n");
27646 +       }
27647 +
27648 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27649 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27650 +               pr_warn_once("udba=*notify requires xino\n");
27651 +
27652 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27653 +               pr_warn_once("dirperm1 breaks the protection"
27654 +                            " by the permission bits on the lower branch\n");
27655 +
27656 +       err = 0;
27657 +       fhsm = 0;
27658 +       root = sb->s_root;
27659 +       dir = d_inode(root);
27660 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27661 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27662 +                                     UDBA_NONE);
27663 +       bbot = au_sbbot(sb);
27664 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27665 +               skip = 0;
27666 +               h_dir = au_h_iptr(dir, bindex);
27667 +               br = au_sbr(sb, bindex);
27668 +
27669 +               if ((br->br_perm & AuBrAttr_ICEX)
27670 +                   && !h_dir->i_op->listxattr)
27671 +                       br->br_perm &= ~AuBrAttr_ICEX;
27672 +#if 0 /* untested */
27673 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27674 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27675 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27676 +#endif
27677 +
27678 +               do_free = 0;
27679 +               wbr = br->br_wbr;
27680 +               if (wbr)
27681 +                       wbr_wh_read_lock(wbr);
27682 +
27683 +               if (!au_br_writable(br->br_perm)) {
27684 +                       do_free = !!wbr;
27685 +                       skip = (!wbr
27686 +                               || (!wbr->wbr_whbase
27687 +                                   && !wbr->wbr_plink
27688 +                                   && !wbr->wbr_orph));
27689 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27690 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27691 +                       skip = (!wbr || !wbr->wbr_whbase);
27692 +                       if (skip && wbr) {
27693 +                               if (do_plink)
27694 +                                       skip = !!wbr->wbr_plink;
27695 +                               else
27696 +                                       skip = !wbr->wbr_plink;
27697 +                       }
27698 +               } else {
27699 +                       /* skip = (br->br_whbase && br->br_ohph); */
27700 +                       skip = (wbr && wbr->wbr_whbase);
27701 +                       if (skip) {
27702 +                               if (do_plink)
27703 +                                       skip = !!wbr->wbr_plink;
27704 +                               else
27705 +                                       skip = !wbr->wbr_plink;
27706 +                       }
27707 +               }
27708 +               if (wbr)
27709 +                       wbr_wh_read_unlock(wbr);
27710 +
27711 +               if (can_no_dreval) {
27712 +                       dentry = br->br_path.dentry;
27713 +                       spin_lock(&dentry->d_lock);
27714 +                       if (dentry->d_flags &
27715 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27716 +                               can_no_dreval = 0;
27717 +                       spin_unlock(&dentry->d_lock);
27718 +               }
27719 +
27720 +               if (au_br_fhsm(br->br_perm)) {
27721 +                       fhsm++;
27722 +                       AuDebugOn(!br->br_fhsm);
27723 +               }
27724 +
27725 +               if (skip)
27726 +                       continue;
27727 +
27728 +               hdir = au_hi(dir, bindex);
27729 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27730 +               if (wbr)
27731 +                       wbr_wh_write_lock(wbr);
27732 +               err = au_wh_init(br, sb);
27733 +               if (wbr)
27734 +                       wbr_wh_write_unlock(wbr);
27735 +               au_hn_inode_unlock(hdir);
27736 +
27737 +               if (!err && do_free) {
27738 +                       au_kfree_rcu(wbr);
27739 +                       br->br_wbr = NULL;
27740 +               }
27741 +       }
27742 +
27743 +       if (can_no_dreval)
27744 +               au_fset_si(sbinfo, NO_DREVAL);
27745 +       else
27746 +               au_fclr_si(sbinfo, NO_DREVAL);
27747 +
27748 +       if (fhsm >= 2) {
27749 +               au_fset_si(sbinfo, FHSM);
27750 +               for (bindex = bbot; bindex >= 0; bindex--) {
27751 +                       br = au_sbr(sb, bindex);
27752 +                       if (au_br_fhsm(br->br_perm)) {
27753 +                               au_fhsm_set_bottom(sb, bindex);
27754 +                               break;
27755 +                       }
27756 +               }
27757 +       } else {
27758 +               au_fclr_si(sbinfo, FHSM);
27759 +               au_fhsm_set_bottom(sb, -1);
27760 +       }
27761 +
27762 +       return err;
27763 +}
27764 +
27765 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27766 +{
27767 +       int err;
27768 +       unsigned int tmp;
27769 +       aufs_bindex_t bindex, bbot;
27770 +       struct au_opt *opt;
27771 +       struct au_opt_xino *opt_xino, xino;
27772 +       struct au_sbinfo *sbinfo;
27773 +       struct au_branch *br;
27774 +       struct inode *dir;
27775 +
27776 +       SiMustWriteLock(sb);
27777 +
27778 +       err = 0;
27779 +       opt_xino = NULL;
27780 +       opt = opts->opt;
27781 +       while (err >= 0 && opt->type != Opt_tail)
27782 +               err = au_opt_simple(sb, opt++, opts);
27783 +       if (err > 0)
27784 +               err = 0;
27785 +       else if (unlikely(err < 0))
27786 +               goto out;
27787 +
27788 +       /* disable xino and udba temporary */
27789 +       sbinfo = au_sbi(sb);
27790 +       tmp = sbinfo->si_mntflags;
27791 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27792 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27793 +
27794 +       opt = opts->opt;
27795 +       while (err >= 0 && opt->type != Opt_tail)
27796 +               err = au_opt_br(sb, opt++, opts);
27797 +       if (err > 0)
27798 +               err = 0;
27799 +       else if (unlikely(err < 0))
27800 +               goto out;
27801 +
27802 +       bbot = au_sbbot(sb);
27803 +       if (unlikely(bbot < 0)) {
27804 +               err = -EINVAL;
27805 +               pr_err("no branches\n");
27806 +               goto out;
27807 +       }
27808 +
27809 +       if (au_opt_test(tmp, XINO))
27810 +               au_opt_set(sbinfo->si_mntflags, XINO);
27811 +       opt = opts->opt;
27812 +       while (!err && opt->type != Opt_tail)
27813 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27814 +       if (unlikely(err))
27815 +               goto out;
27816 +
27817 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27818 +       if (unlikely(err))
27819 +               goto out;
27820 +
27821 +       /* restore xino */
27822 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27823 +               xino.file = au_xino_def(sb);
27824 +               err = PTR_ERR(xino.file);
27825 +               if (IS_ERR(xino.file))
27826 +                       goto out;
27827 +
27828 +               err = au_xino_set(sb, &xino, /*remount*/0);
27829 +               fput(xino.file);
27830 +               if (unlikely(err))
27831 +                       goto out;
27832 +       }
27833 +
27834 +       /* restore udba */
27835 +       tmp &= AuOptMask_UDBA;
27836 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27837 +       sbinfo->si_mntflags |= tmp;
27838 +       bbot = au_sbbot(sb);
27839 +       for (bindex = 0; bindex <= bbot; bindex++) {
27840 +               br = au_sbr(sb, bindex);
27841 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27842 +               if (unlikely(err))
27843 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27844 +                               bindex, err);
27845 +               /* go on even if err */
27846 +       }
27847 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27848 +               dir = d_inode(sb->s_root);
27849 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27850 +       }
27851 +
27852 +out:
27853 +       return err;
27854 +}
27855 +
27856 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27857 +{
27858 +       int err, rerr;
27859 +       unsigned char no_dreval;
27860 +       struct inode *dir;
27861 +       struct au_opt_xino *opt_xino;
27862 +       struct au_opt *opt;
27863 +       struct au_sbinfo *sbinfo;
27864 +
27865 +       SiMustWriteLock(sb);
27866 +
27867 +       err = au_dr_opt_flush(sb);
27868 +       if (unlikely(err))
27869 +               goto out;
27870 +       au_fset_opts(opts->flags, DR_FLUSHED);
27871 +
27872 +       dir = d_inode(sb->s_root);
27873 +       sbinfo = au_sbi(sb);
27874 +       opt_xino = NULL;
27875 +       opt = opts->opt;
27876 +       while (err >= 0 && opt->type != Opt_tail) {
27877 +               err = au_opt_simple(sb, opt, opts);
27878 +               if (!err)
27879 +                       err = au_opt_br(sb, opt, opts);
27880 +               if (!err)
27881 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27882 +               opt++;
27883 +       }
27884 +       if (err > 0)
27885 +               err = 0;
27886 +       AuTraceErr(err);
27887 +       /* go on even err */
27888 +
27889 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27890 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27891 +       if (unlikely(rerr && !err))
27892 +               err = rerr;
27893 +
27894 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27895 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27896 +
27897 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27898 +               rerr = au_xib_trunc(sb);
27899 +               if (unlikely(rerr && !err))
27900 +                       err = rerr;
27901 +       }
27902 +
27903 +       /* will be handled by the caller */
27904 +       if (!au_ftest_opts(opts->flags, REFRESH)
27905 +           && (opts->given_udba
27906 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27907 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27908 +                   ))
27909 +               au_fset_opts(opts->flags, REFRESH);
27910 +
27911 +       AuDbg("status 0x%x\n", opts->flags);
27912 +
27913 +out:
27914 +       return err;
27915 +}
27916 +
27917 +/* ---------------------------------------------------------------------- */
27918 +
27919 +unsigned int au_opt_udba(struct super_block *sb)
27920 +{
27921 +       return au_mntflags(sb) & AuOptMask_UDBA;
27922 +}
27923 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27924 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27925 +++ linux/fs/aufs/opts.h        2022-11-05 23:02:18.969222617 +0100
27926 @@ -0,0 +1,263 @@
27927 +/* SPDX-License-Identifier: GPL-2.0 */
27928 +/*
27929 + * Copyright (C) 2005-2022 Junjiro R. Okajima
27930 + *
27931 + * This program is free software; you can redistribute it and/or modify
27932 + * it under the terms of the GNU General Public License as published by
27933 + * the Free Software Foundation; either version 2 of the License, or
27934 + * (at your option) any later version.
27935 + *
27936 + * This program is distributed in the hope that it will be useful,
27937 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27938 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27939 + * GNU General Public License for more details.
27940 + *
27941 + * You should have received a copy of the GNU General Public License
27942 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27943 + */
27944 +
27945 +/*
27946 + * mount options/flags
27947 + */
27948 +
27949 +#ifndef __AUFS_OPTS_H__
27950 +#define __AUFS_OPTS_H__
27951 +
27952 +#ifdef __KERNEL__
27953 +
27954 +#include <linux/fs_parser.h>
27955 +#include <linux/namei.h>
27956 +#include <linux/path.h>
27957 +
27958 +enum {
27959 +       Opt_br,
27960 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27961 +       Opt_idel, Opt_imod,
27962 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27963 +       Opt_xino, Opt_noxino,
27964 +       Opt_trunc_xino, Opt_trunc_xino_v,
27965 +       Opt_trunc_xino_path, Opt_itrunc_xino,
27966 +       Opt_trunc_xib,
27967 +       Opt_shwh,
27968 +       Opt_plink, Opt_list_plink,
27969 +       Opt_udba,
27970 +       Opt_dio,
27971 +       Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27972 +       Opt_warn_perm,
27973 +       Opt_wbr_copyup, Opt_wbr_create,
27974 +       Opt_fhsm_sec,
27975 +       Opt_verbose, Opt_noverbose,
27976 +       Opt_sum, Opt_wsum,
27977 +       Opt_dirperm1,
27978 +       Opt_dirren,
27979 +       Opt_acl,
27980 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
27981 +};
27982 +
27983 +/* ---------------------------------------------------------------------- */
27984 +
27985 +/* mount flags */
27986 +#define AuOpt_XINO             1               /* external inode number bitmap
27987 +                                                  and translation table */
27988 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27989 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27990 +#define AuOpt_UDBA_REVAL       (1 << 3)
27991 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27992 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27993 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27994 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27995 +                                                  bits */
27996 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27997 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27998 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27999 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
28000 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
28001 +#define AuOpt_DIO              (1 << 14)       /* direct io */
28002 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
28003 +
28004 +#ifndef CONFIG_AUFS_HNOTIFY
28005 +#undef AuOpt_UDBA_HNOTIFY
28006 +#define AuOpt_UDBA_HNOTIFY     0
28007 +#endif
28008 +#ifndef CONFIG_AUFS_DIRREN
28009 +#undef AuOpt_DIRREN
28010 +#define AuOpt_DIRREN           0
28011 +#endif
28012 +#ifndef CONFIG_AUFS_SHWH
28013 +#undef AuOpt_SHWH
28014 +#define AuOpt_SHWH             0
28015 +#endif
28016 +
28017 +#define AuOpt_Def      (AuOpt_XINO \
28018 +                        | AuOpt_UDBA_REVAL \
28019 +                        | AuOpt_PLINK \
28020 +                        /* | AuOpt_DIRPERM1 */ \
28021 +                        | AuOpt_WARN_PERM)
28022 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
28023 +                        | AuOpt_UDBA_REVAL \
28024 +                        | AuOpt_UDBA_HNOTIFY)
28025 +
28026 +#define AuOpt_LkupDirFlags     (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
28027 +
28028 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
28029 +#define au_opt_set(flags, name) do { \
28030 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
28031 +       ((flags) |= AuOpt_##name); \
28032 +} while (0)
28033 +#define au_opt_set_udba(flags, name) do { \
28034 +       (flags) &= ~AuOptMask_UDBA; \
28035 +       ((flags) |= AuOpt_##name); \
28036 +} while (0)
28037 +#define au_opt_clr(flags, name) do { \
28038 +       ((flags) &= ~AuOpt_##name); \
28039 +} while (0)
28040 +
28041 +static inline unsigned int au_opts_plink(unsigned int mntflags)
28042 +{
28043 +#ifdef CONFIG_PROC_FS
28044 +       return mntflags;
28045 +#else
28046 +       return mntflags & ~AuOpt_PLINK;
28047 +#endif
28048 +}
28049 +
28050 +/* ---------------------------------------------------------------------- */
28051 +
28052 +/* policies to select one among multiple writable branches */
28053 +enum {
28054 +       AuWbrCreate_TDP,        /* top down parent */
28055 +       AuWbrCreate_RR,         /* round robin */
28056 +       AuWbrCreate_MFS,        /* most free space */
28057 +       AuWbrCreate_MFSV,       /* mfs with seconds */
28058 +       AuWbrCreate_MFSRR,      /* mfs then rr */
28059 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
28060 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
28061 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
28062 +       AuWbrCreate_PMFS,       /* parent and mfs */
28063 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
28064 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
28065 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
28066 +
28067 +       AuWbrCreate_Def = AuWbrCreate_TDP
28068 +};
28069 +
28070 +enum {
28071 +       AuWbrCopyup_TDP,        /* top down parent */
28072 +       AuWbrCopyup_BUP,        /* bottom up parent */
28073 +       AuWbrCopyup_BU,         /* bottom up */
28074 +
28075 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
28076 +};
28077 +
28078 +/* ---------------------------------------------------------------------- */
28079 +
28080 +struct file;
28081 +
28082 +struct au_opt_add {
28083 +       aufs_bindex_t   bindex;
28084 +       char            *pathname;
28085 +       int             perm;
28086 +       struct path     path;
28087 +};
28088 +
28089 +struct au_opt_del {
28090 +       char            *pathname;
28091 +       struct path     h_path;
28092 +};
28093 +
28094 +struct au_opt_mod {
28095 +       char            *path;
28096 +       int             perm;
28097 +       struct dentry   *h_root;
28098 +};
28099 +
28100 +struct au_opt_xino {
28101 +       char            *path;
28102 +       struct file     *file;
28103 +};
28104 +
28105 +struct au_opt_xino_itrunc {
28106 +       aufs_bindex_t   bindex;
28107 +};
28108 +
28109 +struct au_opt_wbr_create {
28110 +       int                     wbr_create;
28111 +       int                     mfs_second;
28112 +       unsigned long long      mfsrr_watermark;
28113 +};
28114 +
28115 +struct au_opt {
28116 +       int type;
28117 +       union {
28118 +               struct au_opt_xino      xino;
28119 +               struct au_opt_xino_itrunc xino_itrunc;
28120 +               struct au_opt_add       add;
28121 +               struct au_opt_del       del;
28122 +               struct au_opt_mod       mod;
28123 +               int                     dirwh;
28124 +               int                     rdcache;
28125 +               unsigned int            rdblk;
28126 +               unsigned int            rdhash;
28127 +               int                     udba;
28128 +               struct au_opt_wbr_create wbr_create;
28129 +               int                     wbr_copyup;
28130 +               unsigned int            fhsm_second;
28131 +               bool                    tf; /* generic flag, true or false */
28132 +       };
28133 +};
28134 +
28135 +/* opts flags */
28136 +#define AuOpts_REMOUNT         1
28137 +#define AuOpts_REFRESH         (1 << 1)
28138 +#define AuOpts_TRUNC_XIB       (1 << 2)
28139 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
28140 +#define AuOpts_REFRESH_IDOP    (1 << 4)
28141 +#define AuOpts_DR_FLUSHED      (1 << 5)
28142 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
28143 +#define au_fset_opts(flags, name) \
28144 +       do { (flags) |= AuOpts_##name; } while (0)
28145 +#define au_fclr_opts(flags, name) \
28146 +       do { (flags) &= ~AuOpts_##name; } while (0)
28147 +
28148 +#ifndef CONFIG_AUFS_DIRREN
28149 +#undef AuOpts_DR_FLUSHED
28150 +#define AuOpts_DR_FLUSHED      0
28151 +#endif
28152 +
28153 +struct au_opts {
28154 +       struct au_opt   *opt;
28155 +       int             max_opt;
28156 +
28157 +       unsigned int    given_udba;
28158 +       unsigned int    flags;
28159 +       unsigned long   sb_flags;
28160 +};
28161 +
28162 +/* ---------------------------------------------------------------------- */
28163 +
28164 +/* opts.c */
28165 +int au_br_perm_val(char *perm);
28166 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
28167 +int au_udba_val(char *str);
28168 +const char *au_optstr_udba(int udba);
28169 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
28170 +const char *au_optstr_wbr_create(int wbr_create);
28171 +int au_wbr_copyup_val(char *str);
28172 +const char *au_optstr_wbr_copyup(int wbr_copyup);
28173 +
28174 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28175 +              aufs_bindex_t bindex);
28176 +struct super_block;
28177 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28178 +                  unsigned int pending);
28179 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28180 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28181 +
28182 +unsigned int au_opt_udba(struct super_block *sb);
28183 +
28184 +/* fsctx.c */
28185 +int aufs_fsctx_init(struct fs_context *fc);
28186 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28187 +
28188 +#endif /* __KERNEL__ */
28189 +#endif /* __AUFS_OPTS_H__ */
28190 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
28191 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
28192 +++ linux/fs/aufs/plink.c       2022-11-05 23:02:18.969222617 +0100
28193 @@ -0,0 +1,516 @@
28194 +// SPDX-License-Identifier: GPL-2.0
28195 +/*
28196 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28197 + *
28198 + * This program is free software; you can redistribute it and/or modify
28199 + * it under the terms of the GNU General Public License as published by
28200 + * the Free Software Foundation; either version 2 of the License, or
28201 + * (at your option) any later version.
28202 + *
28203 + * This program is distributed in the hope that it will be useful,
28204 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28205 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28206 + * GNU General Public License for more details.
28207 + *
28208 + * You should have received a copy of the GNU General Public License
28209 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28210 + */
28211 +
28212 +/*
28213 + * pseudo-link
28214 + */
28215 +
28216 +#include "aufs.h"
28217 +
28218 +/*
28219 + * the pseudo-link maintenance mode.
28220 + * during a user process maintains the pseudo-links,
28221 + * prohibit adding a new plink and branch manipulation.
28222 + *
28223 + * Flags
28224 + * NOPLM:
28225 + *     For entry functions which will handle plink, and i_mutex is already held
28226 + *     in VFS.
28227 + *     They cannot wait and should return an error at once.
28228 + *     Callers has to check the error.
28229 + * NOPLMW:
28230 + *     For entry functions which will handle plink, but i_mutex is not held
28231 + *     in VFS.
28232 + *     They can wait the plink maintenance mode to finish.
28233 + *
28234 + * They behave like F_SETLK and F_SETLKW.
28235 + * If the caller never handle plink, then both flags are unnecessary.
28236 + */
28237 +
28238 +int au_plink_maint(struct super_block *sb, int flags)
28239 +{
28240 +       int err;
28241 +       pid_t pid, ppid;
28242 +       struct task_struct *parent, *prev;
28243 +       struct au_sbinfo *sbi;
28244 +
28245 +       SiMustAnyLock(sb);
28246 +
28247 +       err = 0;
28248 +       if (!au_opt_test(au_mntflags(sb), PLINK))
28249 +               goto out;
28250 +
28251 +       sbi = au_sbi(sb);
28252 +       pid = sbi->si_plink_maint_pid;
28253 +       if (!pid || pid == current->pid)
28254 +               goto out;
28255 +
28256 +       /* todo: it highly depends upon /sbin/mount.aufs */
28257 +       prev = NULL;
28258 +       parent = current;
28259 +       ppid = 0;
28260 +       rcu_read_lock();
28261 +       while (1) {
28262 +               parent = rcu_dereference(parent->real_parent);
28263 +               if (parent == prev)
28264 +                       break;
28265 +               ppid = task_pid_vnr(parent);
28266 +               if (pid == ppid) {
28267 +                       rcu_read_unlock();
28268 +                       goto out;
28269 +               }
28270 +               prev = parent;
28271 +       }
28272 +       rcu_read_unlock();
28273 +
28274 +       if (au_ftest_lock(flags, NOPLMW)) {
28275 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
28276 +               /* AuDebugOn(!lockdep_depth(current)); */
28277 +               while (sbi->si_plink_maint_pid) {
28278 +                       si_read_unlock(sb);
28279 +                       /* gave up wake_up_bit() */
28280 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28281 +
28282 +                       if (au_ftest_lock(flags, FLUSH))
28283 +                               au_nwt_flush(&sbi->si_nowait);
28284 +                       si_noflush_read_lock(sb);
28285 +               }
28286 +       } else if (au_ftest_lock(flags, NOPLM)) {
28287 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
28288 +               err = -EAGAIN;
28289 +       }
28290 +
28291 +out:
28292 +       return err;
28293 +}
28294 +
28295 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28296 +{
28297 +       spin_lock(&sbinfo->si_plink_maint_lock);
28298 +       sbinfo->si_plink_maint_pid = 0;
28299 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28300 +       wake_up_all(&sbinfo->si_plink_wq);
28301 +}
28302 +
28303 +int au_plink_maint_enter(struct super_block *sb)
28304 +{
28305 +       int err;
28306 +       struct au_sbinfo *sbinfo;
28307 +
28308 +       err = 0;
28309 +       sbinfo = au_sbi(sb);
28310 +       /* make sure i am the only one in this fs */
28311 +       si_write_lock(sb, AuLock_FLUSH);
28312 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28313 +               spin_lock(&sbinfo->si_plink_maint_lock);
28314 +               if (!sbinfo->si_plink_maint_pid)
28315 +                       sbinfo->si_plink_maint_pid = current->pid;
28316 +               else
28317 +                       err = -EBUSY;
28318 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28319 +       }
28320 +       si_write_unlock(sb);
28321 +
28322 +       return err;
28323 +}
28324 +
28325 +/* ---------------------------------------------------------------------- */
28326 +
28327 +#ifdef CONFIG_AUFS_DEBUG
28328 +void au_plink_list(struct super_block *sb)
28329 +{
28330 +       int i;
28331 +       struct au_sbinfo *sbinfo;
28332 +       struct hlist_bl_head *hbl;
28333 +       struct hlist_bl_node *pos;
28334 +       struct au_icntnr *icntnr;
28335 +
28336 +       SiMustAnyLock(sb);
28337 +
28338 +       sbinfo = au_sbi(sb);
28339 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28340 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28341 +
28342 +       for (i = 0; i < AuPlink_NHASH; i++) {
28343 +               hbl = sbinfo->si_plink + i;
28344 +               hlist_bl_lock(hbl);
28345 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28346 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28347 +               hlist_bl_unlock(hbl);
28348 +       }
28349 +}
28350 +#endif
28351 +
28352 +/* is the inode pseudo-linked? */
28353 +int au_plink_test(struct inode *inode)
28354 +{
28355 +       int found, i;
28356 +       struct au_sbinfo *sbinfo;
28357 +       struct hlist_bl_head *hbl;
28358 +       struct hlist_bl_node *pos;
28359 +       struct au_icntnr *icntnr;
28360 +
28361 +       sbinfo = au_sbi(inode->i_sb);
28362 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28363 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28364 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28365 +
28366 +       found = 0;
28367 +       i = au_plink_hash(inode->i_ino);
28368 +       hbl =  sbinfo->si_plink + i;
28369 +       hlist_bl_lock(hbl);
28370 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28371 +               if (&icntnr->vfs_inode == inode) {
28372 +                       found = 1;
28373 +                       break;
28374 +               }
28375 +       hlist_bl_unlock(hbl);
28376 +       return found;
28377 +}
28378 +
28379 +/* ---------------------------------------------------------------------- */
28380 +
28381 +/*
28382 + * generate a name for plink.
28383 + * the file will be stored under AUFS_WH_PLINKDIR.
28384 + */
28385 +/* 20 is max digits length of ulong 64 */
28386 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28387 +
28388 +static int plink_name(char *name, int len, struct inode *inode,
28389 +                     aufs_bindex_t bindex)
28390 +{
28391 +       int rlen;
28392 +       struct inode *h_inode;
28393 +
28394 +       h_inode = au_h_iptr(inode, bindex);
28395 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28396 +       return rlen;
28397 +}
28398 +
28399 +struct au_do_plink_lkup_args {
28400 +       struct dentry **errp;
28401 +       struct qstr *tgtname;
28402 +       struct path *h_ppath;
28403 +};
28404 +
28405 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28406 +                                      struct path *h_ppath)
28407 +{
28408 +       struct dentry *h_dentry;
28409 +       struct inode *h_inode;
28410 +
28411 +       h_inode = d_inode(h_ppath->dentry);
28412 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28413 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
28414 +       inode_unlock_shared(h_inode);
28415 +
28416 +       return h_dentry;
28417 +}
28418 +
28419 +static void au_call_do_plink_lkup(void *args)
28420 +{
28421 +       struct au_do_plink_lkup_args *a = args;
28422 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
28423 +}
28424 +
28425 +/* lookup the plink-ed @inode under the branch at @bindex */
28426 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28427 +{
28428 +       struct dentry *h_dentry;
28429 +       struct au_branch *br;
28430 +       struct path h_ppath;
28431 +       int wkq_err;
28432 +       char a[PLINK_NAME_LEN];
28433 +       struct qstr tgtname = QSTR_INIT(a, 0);
28434 +
28435 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28436 +
28437 +       br = au_sbr(inode->i_sb, bindex);
28438 +       h_ppath.dentry = br->br_wbr->wbr_plink;
28439 +       h_ppath.mnt = au_br_mnt(br);
28440 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28441 +
28442 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28443 +               struct au_do_plink_lkup_args args = {
28444 +                       .errp           = &h_dentry,
28445 +                       .tgtname        = &tgtname,
28446 +                       .h_ppath        = &h_ppath
28447 +               };
28448 +
28449 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28450 +               if (unlikely(wkq_err))
28451 +                       h_dentry = ERR_PTR(wkq_err);
28452 +       } else
28453 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28454 +
28455 +       return h_dentry;
28456 +}
28457 +
28458 +/* create a pseudo-link */
28459 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28460 +                     struct dentry *h_dentry)
28461 +{
28462 +       int err;
28463 +       struct path h_path;
28464 +       struct inode *h_dir, *delegated;
28465 +
28466 +       h_dir = d_inode(h_ppath->dentry);
28467 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28468 +       h_path.mnt = h_ppath->mnt;
28469 +again:
28470 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28471 +       err = PTR_ERR(h_path.dentry);
28472 +       if (IS_ERR(h_path.dentry))
28473 +               goto out;
28474 +
28475 +       err = 0;
28476 +       /* wh.plink dir is not monitored */
28477 +       /* todo: is it really safe? */
28478 +       if (d_is_positive(h_path.dentry)
28479 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28480 +               delegated = NULL;
28481 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28482 +               if (unlikely(err == -EWOULDBLOCK)) {
28483 +                       pr_warn("cannot retry for NFSv4 delegation"
28484 +                               " for an internal unlink\n");
28485 +                       iput(delegated);
28486 +               }
28487 +               dput(h_path.dentry);
28488 +               h_path.dentry = NULL;
28489 +               if (!err)
28490 +                       goto again;
28491 +       }
28492 +       if (!err && d_is_negative(h_path.dentry)) {
28493 +               delegated = NULL;
28494 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28495 +               if (unlikely(err == -EWOULDBLOCK)) {
28496 +                       pr_warn("cannot retry for NFSv4 delegation"
28497 +                               " for an internal link\n");
28498 +                       iput(delegated);
28499 +               }
28500 +       }
28501 +       dput(h_path.dentry);
28502 +
28503 +out:
28504 +       inode_unlock(h_dir);
28505 +       return err;
28506 +}
28507 +
28508 +struct do_whplink_args {
28509 +       int *errp;
28510 +       struct qstr *tgt;
28511 +       struct path *h_ppath;
28512 +       struct dentry *h_dentry;
28513 +};
28514 +
28515 +static void call_do_whplink(void *args)
28516 +{
28517 +       struct do_whplink_args *a = args;
28518 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28519 +}
28520 +
28521 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28522 +                  aufs_bindex_t bindex)
28523 +{
28524 +       int err, wkq_err;
28525 +       struct au_branch *br;
28526 +       struct au_wbr *wbr;
28527 +       struct path h_ppath;
28528 +       char a[PLINK_NAME_LEN];
28529 +       struct qstr tgtname = QSTR_INIT(a, 0);
28530 +
28531 +       br = au_sbr(inode->i_sb, bindex);
28532 +       wbr = br->br_wbr;
28533 +       h_ppath.dentry = wbr->wbr_plink;
28534 +       h_ppath.mnt = au_br_mnt(br);
28535 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28536 +
28537 +       /* always superio. */
28538 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28539 +               struct do_whplink_args args = {
28540 +                       .errp           = &err,
28541 +                       .tgt            = &tgtname,
28542 +                       .h_ppath        = &h_ppath,
28543 +                       .h_dentry       = h_dentry
28544 +               };
28545 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28546 +               if (unlikely(wkq_err))
28547 +                       err = wkq_err;
28548 +       } else
28549 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28550 +
28551 +       return err;
28552 +}
28553 +
28554 +/*
28555 + * create a new pseudo-link for @h_dentry on @bindex.
28556 + * the linked inode is held in aufs @inode.
28557 + */
28558 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28559 +                    struct dentry *h_dentry)
28560 +{
28561 +       struct super_block *sb;
28562 +       struct au_sbinfo *sbinfo;
28563 +       struct hlist_bl_head *hbl;
28564 +       struct hlist_bl_node *pos;
28565 +       struct au_icntnr *icntnr;
28566 +       int found, err, cnt, i;
28567 +
28568 +       sb = inode->i_sb;
28569 +       sbinfo = au_sbi(sb);
28570 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28571 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28572 +
28573 +       found = au_plink_test(inode);
28574 +       if (found)
28575 +               return;
28576 +
28577 +       i = au_plink_hash(inode->i_ino);
28578 +       hbl = sbinfo->si_plink + i;
28579 +       au_igrab(inode);
28580 +
28581 +       hlist_bl_lock(hbl);
28582 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28583 +               if (&icntnr->vfs_inode == inode) {
28584 +                       found = 1;
28585 +                       break;
28586 +               }
28587 +       }
28588 +       if (!found) {
28589 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28590 +               hlist_bl_add_head(&icntnr->plink, hbl);
28591 +       }
28592 +       hlist_bl_unlock(hbl);
28593 +       if (!found) {
28594 +               cnt = au_hbl_count(hbl);
28595 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28596 +               if (cnt > AUFS_PLINK_WARN)
28597 +                       AuWarn1(msg ", %d\n", cnt);
28598 +#undef msg
28599 +               err = whplink(h_dentry, inode, bindex);
28600 +               if (unlikely(err)) {
28601 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28602 +                       au_hbl_del(&icntnr->plink, hbl);
28603 +                       iput(&icntnr->vfs_inode);
28604 +               }
28605 +       } else
28606 +               iput(&icntnr->vfs_inode);
28607 +}
28608 +
28609 +/* free all plinks */
28610 +void au_plink_put(struct super_block *sb, int verbose)
28611 +{
28612 +       int i, warned;
28613 +       struct au_sbinfo *sbinfo;
28614 +       struct hlist_bl_head *hbl;
28615 +       struct hlist_bl_node *pos, *tmp;
28616 +       struct au_icntnr *icntnr;
28617 +
28618 +       SiMustWriteLock(sb);
28619 +
28620 +       sbinfo = au_sbi(sb);
28621 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28622 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28623 +
28624 +       /* no spin_lock since sbinfo is write-locked */
28625 +       warned = 0;
28626 +       for (i = 0; i < AuPlink_NHASH; i++) {
28627 +               hbl = sbinfo->si_plink + i;
28628 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28629 +                       pr_warn("pseudo-link is not flushed");
28630 +                       warned = 1;
28631 +               }
28632 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28633 +                       iput(&icntnr->vfs_inode);
28634 +               INIT_HLIST_BL_HEAD(hbl);
28635 +       }
28636 +}
28637 +
28638 +void au_plink_clean(struct super_block *sb, int verbose)
28639 +{
28640 +       struct dentry *root;
28641 +
28642 +       root = sb->s_root;
28643 +       aufs_write_lock(root);
28644 +       if (au_opt_test(au_mntflags(sb), PLINK))
28645 +               au_plink_put(sb, verbose);
28646 +       aufs_write_unlock(root);
28647 +}
28648 +
28649 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28650 +{
28651 +       int do_put;
28652 +       aufs_bindex_t btop, bbot, bindex;
28653 +
28654 +       do_put = 0;
28655 +       btop = au_ibtop(inode);
28656 +       bbot = au_ibbot(inode);
28657 +       if (btop >= 0) {
28658 +               for (bindex = btop; bindex <= bbot; bindex++) {
28659 +                       if (!au_h_iptr(inode, bindex)
28660 +                           || au_ii_br_id(inode, bindex) != br_id)
28661 +                               continue;
28662 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28663 +                       do_put = 1;
28664 +                       break;
28665 +               }
28666 +               if (do_put)
28667 +                       for (bindex = btop; bindex <= bbot; bindex++)
28668 +                               if (au_h_iptr(inode, bindex)) {
28669 +                                       do_put = 0;
28670 +                                       break;
28671 +                               }
28672 +       } else
28673 +               do_put = 1;
28674 +
28675 +       return do_put;
28676 +}
28677 +
28678 +/* free the plinks on a branch specified by @br_id */
28679 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28680 +{
28681 +       struct au_sbinfo *sbinfo;
28682 +       struct hlist_bl_head *hbl;
28683 +       struct hlist_bl_node *pos, *tmp;
28684 +       struct au_icntnr *icntnr;
28685 +       struct inode *inode;
28686 +       int i, do_put;
28687 +
28688 +       SiMustWriteLock(sb);
28689 +
28690 +       sbinfo = au_sbi(sb);
28691 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28692 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28693 +
28694 +       /* no bit_lock since sbinfo is write-locked */
28695 +       for (i = 0; i < AuPlink_NHASH; i++) {
28696 +               hbl = sbinfo->si_plink + i;
28697 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28698 +                       inode = au_igrab(&icntnr->vfs_inode);
28699 +                       ii_write_lock_child(inode);
28700 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28701 +                       if (do_put) {
28702 +                               hlist_bl_del(&icntnr->plink);
28703 +                               iput(inode);
28704 +                       }
28705 +                       ii_write_unlock(inode);
28706 +                       iput(inode);
28707 +               }
28708 +       }
28709 +}
28710 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28711 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28712 +++ linux/fs/aufs/poll.c        2022-11-05 23:02:18.969222617 +0100
28713 @@ -0,0 +1,51 @@
28714 +// SPDX-License-Identifier: GPL-2.0
28715 +/*
28716 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28717 + *
28718 + * This program is free software; you can redistribute it and/or modify
28719 + * it under the terms of the GNU General Public License as published by
28720 + * the Free Software Foundation; either version 2 of the License, or
28721 + * (at your option) any later version.
28722 + *
28723 + * This program is distributed in the hope that it will be useful,
28724 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28725 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28726 + * GNU General Public License for more details.
28727 + *
28728 + * You should have received a copy of the GNU General Public License
28729 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28730 + */
28731 +
28732 +/*
28733 + * poll operation
28734 + * There is only one filesystem which implements ->poll operation, currently.
28735 + */
28736 +
28737 +#include "aufs.h"
28738 +
28739 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28740 +{
28741 +       __poll_t mask;
28742 +       struct file *h_file;
28743 +       struct super_block *sb;
28744 +
28745 +       /* We should pretend an error happened. */
28746 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28747 +       sb = file->f_path.dentry->d_sb;
28748 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28749 +
28750 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28751 +       if (IS_ERR(h_file)) {
28752 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28753 +               goto out;
28754 +       }
28755 +
28756 +       mask = vfs_poll(h_file, pt);
28757 +       fput(h_file); /* instead of au_read_post() */
28758 +
28759 +out:
28760 +       si_read_unlock(sb);
28761 +       if (mask & EPOLLERR)
28762 +               AuDbg("mask 0x%x\n", mask);
28763 +       return mask;
28764 +}
28765 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28766 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28767 +++ linux/fs/aufs/posix_acl.c   2023-10-31 09:31:04.199880750 +0100
28768 @@ -0,0 +1,108 @@
28769 +// SPDX-License-Identifier: GPL-2.0
28770 +/*
28771 + * Copyright (C) 2014-2022 Junjiro R. Okajima
28772 + *
28773 + * This program is free software; you can redistribute it and/or modify
28774 + * it under the terms of the GNU General Public License as published by
28775 + * the Free Software Foundation; either version 2 of the License, or
28776 + * (at your option) any later version.
28777 + *
28778 + * This program is distributed in the hope that it will be useful,
28779 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28780 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28781 + * GNU General Public License for more details.
28782 + *
28783 + * You should have received a copy of the GNU General Public License
28784 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28785 + */
28786 +
28787 +/*
28788 + * posix acl operations
28789 + */
28790 +
28791 +#include <linux/fs.h>
28792 +#include "aufs.h"
28793 +
28794 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu)
28795 +{
28796 +       struct posix_acl *acl;
28797 +       int err;
28798 +       aufs_bindex_t bindex;
28799 +       struct inode *h_inode;
28800 +       struct super_block *sb;
28801 +
28802 +       acl = ERR_PTR(-ECHILD);
28803 +       if (rcu)
28804 +               goto out;
28805 +
28806 +       acl = NULL;
28807 +       sb = inode->i_sb;
28808 +       si_read_lock(sb, AuLock_FLUSH);
28809 +       ii_read_lock_child(inode);
28810 +       if (!(sb->s_flags & SB_POSIXACL))
28811 +               goto unlock;
28812 +
28813 +       bindex = au_ibtop(inode);
28814 +       h_inode = au_h_iptr(inode, bindex);
28815 +       if (unlikely(!h_inode
28816 +                    || ((h_inode->i_mode & S_IFMT)
28817 +                        != (inode->i_mode & S_IFMT)))) {
28818 +               err = au_busy_or_stale();
28819 +               acl = ERR_PTR(err);
28820 +               goto unlock;
28821 +       }
28822 +
28823 +       /* always topmost only */
28824 +       acl = get_inode_acl(h_inode, type);
28825 +       if (IS_ERR(acl))
28826 +               forget_cached_acl(inode, type);
28827 +       else
28828 +               set_cached_acl(inode, type, acl);
28829 +
28830 +unlock:
28831 +       ii_read_unlock(inode);
28832 +       si_read_unlock(sb);
28833 +
28834 +out:
28835 +       AuTraceErrPtr(acl);
28836 +       return acl;
28837 +}
28838 +
28839 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
28840 +                              struct dentry *dentry, int type)
28841 +{
28842 +       struct posix_acl *acl;
28843 +       struct inode *inode;
28844 +
28845 +       inode = d_inode(dentry);
28846 +       acl = aufs_get_inode_acl(inode, type, /*rcu*/false);
28847 +
28848 +       return acl;
28849 +}
28850 +
28851 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
28852 +                struct posix_acl *acl, int type)
28853 +{
28854 +       int err;
28855 +       ssize_t ssz;
28856 +       struct inode *inode;
28857 +       struct au_sxattr arg = {
28858 +               .type = AU_ACL_SET,
28859 +               .u.acl_set = {
28860 +                       .acl    = acl,
28861 +                       .type   = type
28862 +               },
28863 +       };
28864 +
28865 +       inode = d_inode(dentry);
28866 +       IMustLock(inode);
28867 +
28868 +       ssz = au_sxattr(dentry, inode, &arg);
28869 +       /* forget even it if succeeds since the branch might set differently */
28870 +       forget_cached_acl(inode, type);
28871 +       err = ssz;
28872 +       if (ssz >= 0)
28873 +               err = 0;
28874 +
28875 +       return err;
28876 +}
28877 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28878 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28879 +++ linux/fs/aufs/procfs.c      2022-11-05 23:02:18.969222617 +0100
28880 @@ -0,0 +1,170 @@
28881 +// SPDX-License-Identifier: GPL-2.0
28882 +/*
28883 + * Copyright (C) 2010-2022 Junjiro R. Okajima
28884 + *
28885 + * This program is free software; you can redistribute it and/or modify
28886 + * it under the terms of the GNU General Public License as published by
28887 + * the Free Software Foundation; either version 2 of the License, or
28888 + * (at your option) any later version.
28889 + *
28890 + * This program is distributed in the hope that it will be useful,
28891 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28892 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28893 + * GNU General Public License for more details.
28894 + *
28895 + * You should have received a copy of the GNU General Public License
28896 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28897 + */
28898 +
28899 +/*
28900 + * procfs interfaces
28901 + */
28902 +
28903 +#include <linux/proc_fs.h>
28904 +#include "aufs.h"
28905 +
28906 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28907 +{
28908 +       struct au_sbinfo *sbinfo;
28909 +
28910 +       sbinfo = file->private_data;
28911 +       if (sbinfo) {
28912 +               au_plink_maint_leave(sbinfo);
28913 +               kobject_put(&sbinfo->si_kobj);
28914 +       }
28915 +
28916 +       return 0;
28917 +}
28918 +
28919 +static void au_procfs_plm_write_clean(struct file *file)
28920 +{
28921 +       struct au_sbinfo *sbinfo;
28922 +
28923 +       sbinfo = file->private_data;
28924 +       if (sbinfo)
28925 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28926 +}
28927 +
28928 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28929 +{
28930 +       int err;
28931 +       struct super_block *sb;
28932 +       struct au_sbinfo *sbinfo;
28933 +       struct hlist_bl_node *pos;
28934 +
28935 +       err = -EBUSY;
28936 +       if (unlikely(file->private_data))
28937 +               goto out;
28938 +
28939 +       sb = NULL;
28940 +       /* don't use au_sbilist_lock() here */
28941 +       hlist_bl_lock(&au_sbilist);
28942 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28943 +               if (id == sysaufs_si_id(sbinfo)) {
28944 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28945 +                               sb = sbinfo->si_sb;
28946 +                       break;
28947 +               }
28948 +       hlist_bl_unlock(&au_sbilist);
28949 +
28950 +       err = -EINVAL;
28951 +       if (unlikely(!sb))
28952 +               goto out;
28953 +
28954 +       err = au_plink_maint_enter(sb);
28955 +       if (!err)
28956 +               /* keep kobject_get() */
28957 +               file->private_data = sbinfo;
28958 +       else
28959 +               kobject_put(&sbinfo->si_kobj);
28960 +out:
28961 +       return err;
28962 +}
28963 +
28964 +/*
28965 + * Accept a valid "si=xxxx" only.
28966 + * Once it is accepted successfully, accept "clean" too.
28967 + */
28968 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28969 +                                  size_t count, loff_t *ppos)
28970 +{
28971 +       ssize_t err;
28972 +       unsigned long id;
28973 +       /* last newline is allowed */
28974 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28975 +
28976 +       err = -EACCES;
28977 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28978 +               goto out;
28979 +
28980 +       err = -EINVAL;
28981 +       if (unlikely(count > sizeof(buf)))
28982 +               goto out;
28983 +
28984 +       err = copy_from_user(buf, ubuf, count);
28985 +       if (unlikely(err)) {
28986 +               err = -EFAULT;
28987 +               goto out;
28988 +       }
28989 +       buf[count] = 0;
28990 +
28991 +       err = -EINVAL;
28992 +       if (!strcmp("clean", buf)) {
28993 +               au_procfs_plm_write_clean(file);
28994 +               goto out_success;
28995 +       } else if (unlikely(strncmp("si=", buf, 3)))
28996 +               goto out;
28997 +
28998 +       err = kstrtoul(buf + 3, 16, &id);
28999 +       if (unlikely(err))
29000 +               goto out;
29001 +
29002 +       err = au_procfs_plm_write_si(file, id);
29003 +       if (unlikely(err))
29004 +               goto out;
29005 +
29006 +out_success:
29007 +       err = count; /* success */
29008 +out:
29009 +       return err;
29010 +}
29011 +
29012 +static const struct proc_ops au_procfs_plm_op = {
29013 +       .proc_write     = au_procfs_plm_write,
29014 +       .proc_release   = au_procfs_plm_release
29015 +};
29016 +
29017 +/* ---------------------------------------------------------------------- */
29018 +
29019 +static struct proc_dir_entry *au_procfs_dir;
29020 +
29021 +void au_procfs_fin(void)
29022 +{
29023 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
29024 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29025 +}
29026 +
29027 +int __init au_procfs_init(void)
29028 +{
29029 +       int err;
29030 +       struct proc_dir_entry *entry;
29031 +
29032 +       err = -ENOMEM;
29033 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
29034 +       if (unlikely(!au_procfs_dir))
29035 +               goto out;
29036 +
29037 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
29038 +                           au_procfs_dir, &au_procfs_plm_op);
29039 +       if (unlikely(!entry))
29040 +               goto out_dir;
29041 +
29042 +       err = 0;
29043 +       goto out; /* success */
29044 +
29045 +
29046 +out_dir:
29047 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29048 +out:
29049 +       return err;
29050 +}
29051 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
29052 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
29053 +++ linux/fs/aufs/rdu.c 2023-10-31 09:31:04.199880750 +0100
29054 @@ -0,0 +1,384 @@
29055 +// SPDX-License-Identifier: GPL-2.0
29056 +/*
29057 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29058 + *
29059 + * This program is free software; you can redistribute it and/or modify
29060 + * it under the terms of the GNU General Public License as published by
29061 + * the Free Software Foundation; either version 2 of the License, or
29062 + * (at your option) any later version.
29063 + *
29064 + * This program is distributed in the hope that it will be useful,
29065 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29066 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29067 + * GNU General Public License for more details.
29068 + *
29069 + * You should have received a copy of the GNU General Public License
29070 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29071 + */
29072 +
29073 +/*
29074 + * readdir in userspace.
29075 + */
29076 +
29077 +#include <linux/compat.h>
29078 +#include <linux/fs_stack.h>
29079 +#include <linux/security.h>
29080 +#include "aufs.h"
29081 +
29082 +/* bits for struct aufs_rdu.flags */
29083 +#define        AuRdu_CALLED    1
29084 +#define        AuRdu_CONT      (1 << 1)
29085 +#define        AuRdu_FULL      (1 << 2)
29086 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
29087 +#define au_fset_rdu(flags, name) \
29088 +       do { (flags) |= AuRdu_##name; } while (0)
29089 +#define au_fclr_rdu(flags, name) \
29090 +       do { (flags) &= ~AuRdu_##name; } while (0)
29091 +
29092 +struct au_rdu_arg {
29093 +       struct dir_context              ctx;
29094 +       struct aufs_rdu                 *rdu;
29095 +       union au_rdu_ent_ul             ent;
29096 +       unsigned long                   end;
29097 +
29098 +       struct super_block              *sb;
29099 +       int                             err;
29100 +};
29101 +
29102 +static bool au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29103 +                       loff_t offset, u64 h_ino, unsigned int d_type)
29104 +{
29105 +       int err, len;
29106 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
29107 +       struct aufs_rdu *rdu = arg->rdu;
29108 +       struct au_rdu_ent ent;
29109 +
29110 +       err = 0;
29111 +       arg->err = 0;
29112 +       au_fset_rdu(rdu->cookie.flags, CALLED);
29113 +       len = au_rdu_len(nlen);
29114 +       if (arg->ent.ul + len  < arg->end) {
29115 +               ent.ino = h_ino;
29116 +               ent.bindex = rdu->cookie.bindex;
29117 +               ent.type = d_type;
29118 +               ent.nlen = nlen;
29119 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
29120 +                       ent.type = DT_UNKNOWN;
29121 +
29122 +               /* unnecessary to support mmap_sem since this is a dir */
29123 +               err = -EFAULT;
29124 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29125 +                       goto out;
29126 +               if (copy_to_user(arg->ent.e->name, name, nlen))
29127 +                       goto out;
29128 +               /* the terminating NULL */
29129 +               if (__put_user(0, arg->ent.e->name + nlen))
29130 +                       goto out;
29131 +               err = 0;
29132 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29133 +               arg->ent.ul += len;
29134 +               rdu->rent++;
29135 +       } else {
29136 +               err = -EFAULT;
29137 +               au_fset_rdu(rdu->cookie.flags, FULL);
29138 +               rdu->full = 1;
29139 +               rdu->tail = arg->ent;
29140 +       }
29141 +
29142 +out:
29143 +       /* AuTraceErr(err); */
29144 +       return !err;
29145 +}
29146 +
29147 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29148 +{
29149 +       int err;
29150 +       loff_t offset;
29151 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29152 +
29153 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
29154 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29155 +       err = offset;
29156 +       if (unlikely(offset != cookie->h_pos))
29157 +               goto out;
29158 +
29159 +       err = 0;
29160 +       do {
29161 +               arg->err = 0;
29162 +               au_fclr_rdu(cookie->flags, CALLED);
29163 +               /* smp_mb(); */
29164 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
29165 +               if (err >= 0)
29166 +                       err = arg->err;
29167 +       } while (!err
29168 +                && au_ftest_rdu(cookie->flags, CALLED)
29169 +                && !au_ftest_rdu(cookie->flags, FULL));
29170 +       cookie->h_pos = h_file->f_pos;
29171 +
29172 +out:
29173 +       AuTraceErr(err);
29174 +       return err;
29175 +}
29176 +
29177 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29178 +{
29179 +       int err;
29180 +       aufs_bindex_t bbot;
29181 +       struct au_rdu_arg arg = {
29182 +               .ctx = {
29183 +                       .actor = au_rdu_fill
29184 +               }
29185 +       };
29186 +       struct dentry *dentry;
29187 +       struct inode *inode;
29188 +       struct file *h_file;
29189 +       struct au_rdu_cookie *cookie = &rdu->cookie;
29190 +
29191 +       /* VERIFY_WRITE */
29192 +       err = !access_ok(rdu->ent.e, rdu->sz);
29193 +       if (unlikely(err)) {
29194 +               err = -EFAULT;
29195 +               AuTraceErr(err);
29196 +               goto out;
29197 +       }
29198 +       rdu->rent = 0;
29199 +       rdu->tail = rdu->ent;
29200 +       rdu->full = 0;
29201 +       arg.rdu = rdu;
29202 +       arg.ent = rdu->ent;
29203 +       arg.end = arg.ent.ul;
29204 +       arg.end += rdu->sz;
29205 +
29206 +       err = -ENOTDIR;
29207 +       if (unlikely(!file->f_op->iterate_shared))
29208 +               goto out;
29209 +
29210 +       err = security_file_permission(file, MAY_READ);
29211 +       AuTraceErr(err);
29212 +       if (unlikely(err))
29213 +               goto out;
29214 +
29215 +       dentry = file->f_path.dentry;
29216 +       inode = d_inode(dentry);
29217 +       inode_lock_shared(inode);
29218 +
29219 +       arg.sb = inode->i_sb;
29220 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29221 +       if (unlikely(err))
29222 +               goto out_mtx;
29223 +       err = au_alive_dir(dentry);
29224 +       if (unlikely(err))
29225 +               goto out_si;
29226 +       /* todo: reval? */
29227 +       fi_read_lock(file);
29228 +
29229 +       err = -EAGAIN;
29230 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29231 +                    && cookie->generation != au_figen(file)))
29232 +               goto out_unlock;
29233 +
29234 +       err = 0;
29235 +       if (!rdu->blk) {
29236 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
29237 +               if (!rdu->blk)
29238 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
29239 +       }
29240 +       bbot = au_fbtop(file);
29241 +       if (cookie->bindex < bbot)
29242 +               cookie->bindex = bbot;
29243 +       bbot = au_fbbot_dir(file);
29244 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29245 +       for (; !err && cookie->bindex <= bbot;
29246 +            cookie->bindex++, cookie->h_pos = 0) {
29247 +               h_file = au_hf_dir(file, cookie->bindex);
29248 +               if (!h_file)
29249 +                       continue;
29250 +
29251 +               au_fclr_rdu(cookie->flags, FULL);
29252 +               err = au_rdu_do(h_file, &arg);
29253 +               AuTraceErr(err);
29254 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29255 +                       break;
29256 +       }
29257 +       AuDbg("rent %llu\n", rdu->rent);
29258 +
29259 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29260 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29261 +               au_fset_rdu(cookie->flags, CONT);
29262 +               cookie->generation = au_figen(file);
29263 +       }
29264 +
29265 +       ii_read_lock_child(inode);
29266 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
29267 +       ii_read_unlock(inode);
29268 +
29269 +out_unlock:
29270 +       fi_read_unlock(file);
29271 +out_si:
29272 +       si_read_unlock(arg.sb);
29273 +out_mtx:
29274 +       inode_unlock_shared(inode);
29275 +out:
29276 +       AuTraceErr(err);
29277 +       return err;
29278 +}
29279 +
29280 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29281 +{
29282 +       int err;
29283 +       ino_t ino;
29284 +       unsigned long long nent;
29285 +       union au_rdu_ent_ul *u;
29286 +       struct au_rdu_ent ent;
29287 +       struct super_block *sb;
29288 +
29289 +       err = 0;
29290 +       nent = rdu->nent;
29291 +       u = &rdu->ent;
29292 +       sb = file->f_path.dentry->d_sb;
29293 +       si_read_lock(sb, AuLock_FLUSH);
29294 +       while (nent-- > 0) {
29295 +               /* unnecessary to support mmap_sem since this is a dir */
29296 +               err = copy_from_user(&ent, u->e, sizeof(ent));
29297 +               if (!err)
29298 +                       /* VERIFY_WRITE */
29299 +                       err = !access_ok(&u->e->ino, sizeof(ino));
29300 +               if (unlikely(err)) {
29301 +                       err = -EFAULT;
29302 +                       AuTraceErr(err);
29303 +                       break;
29304 +               }
29305 +
29306 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29307 +               if (!ent.wh)
29308 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29309 +               else
29310 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29311 +                                       &ino);
29312 +               if (unlikely(err)) {
29313 +                       AuTraceErr(err);
29314 +                       break;
29315 +               }
29316 +
29317 +               err = __put_user(ino, &u->e->ino);
29318 +               if (unlikely(err)) {
29319 +                       err = -EFAULT;
29320 +                       AuTraceErr(err);
29321 +                       break;
29322 +               }
29323 +               u->ul += au_rdu_len(ent.nlen);
29324 +       }
29325 +       si_read_unlock(sb);
29326 +
29327 +       return err;
29328 +}
29329 +
29330 +/* ---------------------------------------------------------------------- */
29331 +
29332 +static int au_rdu_verify(struct aufs_rdu *rdu)
29333 +{
29334 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29335 +             "%llu, b%d, 0x%x, g%u}\n",
29336 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29337 +             rdu->blk,
29338 +             rdu->rent, rdu->shwh, rdu->full,
29339 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29340 +             rdu->cookie.generation);
29341 +
29342 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29343 +               return 0;
29344 +
29345 +       AuDbg("%u:%u\n",
29346 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29347 +       return -EINVAL;
29348 +}
29349 +
29350 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29351 +{
29352 +       long err, e;
29353 +       struct aufs_rdu rdu;
29354 +       void __user *p = (void __user *)arg;
29355 +
29356 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29357 +       if (unlikely(err)) {
29358 +               err = -EFAULT;
29359 +               AuTraceErr(err);
29360 +               goto out;
29361 +       }
29362 +       err = au_rdu_verify(&rdu);
29363 +       if (unlikely(err))
29364 +               goto out;
29365 +
29366 +       switch (cmd) {
29367 +       case AUFS_CTL_RDU:
29368 +               err = au_rdu(file, &rdu);
29369 +               if (unlikely(err))
29370 +                       break;
29371 +
29372 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29373 +               if (unlikely(e)) {
29374 +                       err = -EFAULT;
29375 +                       AuTraceErr(err);
29376 +               }
29377 +               break;
29378 +       case AUFS_CTL_RDU_INO:
29379 +               err = au_rdu_ino(file, &rdu);
29380 +               break;
29381 +
29382 +       default:
29383 +               /* err = -ENOTTY; */
29384 +               err = -EINVAL;
29385 +       }
29386 +
29387 +out:
29388 +       AuTraceErr(err);
29389 +       return err;
29390 +}
29391 +
29392 +#ifdef CONFIG_COMPAT
29393 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29394 +{
29395 +       long err, e;
29396 +       struct aufs_rdu rdu;
29397 +       void __user *p = compat_ptr(arg);
29398 +
29399 +       /* todo: get_user()? */
29400 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29401 +       if (unlikely(err)) {
29402 +               err = -EFAULT;
29403 +               AuTraceErr(err);
29404 +               goto out;
29405 +       }
29406 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
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 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29418 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29419 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29420 +               if (unlikely(e)) {
29421 +                       err = -EFAULT;
29422 +                       AuTraceErr(err);
29423 +               }
29424 +               break;
29425 +       case AUFS_CTL_RDU_INO:
29426 +               err = au_rdu_ino(file, &rdu);
29427 +               break;
29428 +
29429 +       default:
29430 +               /* err = -ENOTTY; */
29431 +               err = -EINVAL;
29432 +       }
29433 +
29434 +out:
29435 +       AuTraceErr(err);
29436 +       return err;
29437 +}
29438 +#endif
29439 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29440 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29441 +++ linux/fs/aufs/rwsem.h       2022-11-05 23:02:18.969222617 +0100
29442 @@ -0,0 +1,85 @@
29443 +/* SPDX-License-Identifier: GPL-2.0 */
29444 +/*
29445 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29446 + *
29447 + * This program is free software; you can redistribute it and/or modify
29448 + * it under the terms of the GNU General Public License as published by
29449 + * the Free Software Foundation; either version 2 of the License, or
29450 + * (at your option) any later version.
29451 + *
29452 + * This program is distributed in the hope that it will be useful,
29453 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29454 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29455 + * GNU General Public License for more details.
29456 + *
29457 + * You should have received a copy of the GNU General Public License
29458 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29459 + */
29460 +
29461 +/*
29462 + * simple read-write semaphore wrappers
29463 + */
29464 +
29465 +#ifndef __AUFS_RWSEM_H__
29466 +#define __AUFS_RWSEM_H__
29467 +
29468 +#ifdef __KERNEL__
29469 +
29470 +#include "debug.h"
29471 +
29472 +/* in the future, the name 'au_rwsem' will be totally gone */
29473 +#define au_rwsem       rw_semaphore
29474 +
29475 +/* to debug easier, do not make them inlined functions */
29476 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29477 +
29478 +#ifdef CONFIG_LOCKDEP
29479 +/* rwsem_is_locked() is unusable */
29480 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29481 +                                         && !lockdep_recursing(current) \
29482 +                                         && debug_locks                \
29483 +                                         && !lockdep_is_held_type(rw, 1))
29484 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29485 +                                         && !lockdep_recursing(current) \
29486 +                                         && debug_locks                \
29487 +                                         && !lockdep_is_held_type(rw, 0))
29488 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29489 +                                         && !lockdep_recursing(current) \
29490 +                                         && debug_locks                \
29491 +                                         && !lockdep_is_held(rw))
29492 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29493 +                                         && !lockdep_recursing(current) \
29494 +                                         && debug_locks                \
29495 +                                         && lockdep_is_held(rw))
29496 +#else
29497 +#define AuRwMustReadLock(rw)   do {} while (0)
29498 +#define AuRwMustWriteLock(rw)  do {} while (0)
29499 +#define AuRwMustAnyLock(rw)    do {} while (0)
29500 +#define AuRwDestroy(rw)                do {} while (0)
29501 +#endif
29502 +
29503 +#define au_rw_init(rw) init_rwsem(rw)
29504 +
29505 +#define au_rw_init_wlock(rw) do {              \
29506 +               au_rw_init(rw);                 \
29507 +               down_write(rw);                 \
29508 +       } while (0)
29509 +
29510 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29511 +               au_rw_init(rw);                 \
29512 +               down_write_nested(rw, lsc);     \
29513 +       } while (0)
29514 +
29515 +#define au_rw_read_lock(rw)            down_read(rw)
29516 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29517 +#define au_rw_read_unlock(rw)          up_read(rw)
29518 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29519 +#define au_rw_write_lock(rw)           down_write(rw)
29520 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29521 +#define au_rw_write_unlock(rw)         up_write(rw)
29522 +/* why is not _nested version defined? */
29523 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29524 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29525 +
29526 +#endif /* __KERNEL__ */
29527 +#endif /* __AUFS_RWSEM_H__ */
29528 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29529 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29530 +++ linux/fs/aufs/sbinfo.c      2022-11-05 23:02:18.969222617 +0100
29531 @@ -0,0 +1,316 @@
29532 +// SPDX-License-Identifier: GPL-2.0
29533 +/*
29534 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29535 + *
29536 + * This program is free software; you can redistribute it and/or modify
29537 + * it under the terms of the GNU General Public License as published by
29538 + * the Free Software Foundation; either version 2 of the License, or
29539 + * (at your option) any later version.
29540 + *
29541 + * This program is distributed in the hope that it will be useful,
29542 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29543 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29544 + * GNU General Public License for more details.
29545 + *
29546 + * You should have received a copy of the GNU General Public License
29547 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29548 + */
29549 +
29550 +/*
29551 + * superblock private data
29552 + */
29553 +
29554 +#include <linux/iversion.h>
29555 +#include "aufs.h"
29556 +
29557 +/*
29558 + * they are necessary regardless sysfs is disabled.
29559 + */
29560 +void au_si_free(struct kobject *kobj)
29561 +{
29562 +       int i;
29563 +       struct au_sbinfo *sbinfo;
29564 +       char *locked __maybe_unused; /* debug only */
29565 +
29566 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29567 +       for (i = 0; i < AuPlink_NHASH; i++)
29568 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29569 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29570 +
29571 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29572 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29573 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29574 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29575 +
29576 +       dbgaufs_si_fin(sbinfo);
29577 +       au_rw_write_lock(&sbinfo->si_rwsem);
29578 +       au_br_free(sbinfo);
29579 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29580 +
29581 +       au_kfree_try_rcu(sbinfo->si_branch);
29582 +       mutex_destroy(&sbinfo->si_xib_mtx);
29583 +       AuRwDestroy(&sbinfo->si_rwsem);
29584 +
29585 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29586 +       /* si_nfiles is waited too */
29587 +       au_kfree_rcu(sbinfo);
29588 +}
29589 +
29590 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29591 +{
29592 +       struct au_sbinfo *sbinfo;
29593 +       int err, i;
29594 +
29595 +       err = -ENOMEM;
29596 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29597 +       if (unlikely(!sbinfo))
29598 +               goto out;
29599 +
29600 +       /* will be reallocated separately */
29601 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29602 +       if (unlikely(!sbinfo->si_branch))
29603 +               goto out_sbinfo;
29604 +
29605 +       err = sysaufs_si_init(sbinfo);
29606 +       if (!err) {
29607 +               dbgaufs_si_null(sbinfo);
29608 +               err = dbgaufs_si_init(sbinfo);
29609 +               if (unlikely(err))
29610 +                       kobject_put(&sbinfo->si_kobj);
29611 +       }
29612 +       if (unlikely(err))
29613 +               goto out_br;
29614 +
29615 +       au_nwt_init(&sbinfo->si_nowait);
29616 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29617 +
29618 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29619 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29620 +
29621 +       sbinfo->si_bbot = -1;
29622 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29623 +
29624 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29625 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29626 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29627 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29628 +
29629 +       au_fhsm_init(sbinfo);
29630 +
29631 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29632 +
29633 +       sbinfo->si_xino_jiffy = jiffies;
29634 +       sbinfo->si_xino_expire
29635 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29636 +       mutex_init(&sbinfo->si_xib_mtx);
29637 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29638 +
29639 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29640 +
29641 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29642 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29643 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29644 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29645 +
29646 +       for (i = 0; i < AuPlink_NHASH; i++)
29647 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29648 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29649 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29650 +
29651 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29652 +
29653 +       /* with getattr by default */
29654 +       sbinfo->si_iop_array = aufs_iop;
29655 +
29656 +       /* leave other members for sysaufs and si_mnt. */
29657 +       sbinfo->si_sb = sb;
29658 +       if (sb) {
29659 +               sb->s_fs_info = sbinfo;
29660 +               si_pid_set(sb);
29661 +       }
29662 +       return sbinfo; /* success */
29663 +
29664 +out_br:
29665 +       au_kfree_try_rcu(sbinfo->si_branch);
29666 +out_sbinfo:
29667 +       au_kfree_rcu(sbinfo);
29668 +out:
29669 +       return ERR_PTR(err);
29670 +}
29671 +
29672 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29673 +{
29674 +       int err, sz;
29675 +       struct au_branch **brp;
29676 +
29677 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29678 +
29679 +       err = -ENOMEM;
29680 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29681 +       if (unlikely(!sz))
29682 +               sz = sizeof(*brp);
29683 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29684 +                          may_shrink);
29685 +       if (brp) {
29686 +               sbinfo->si_branch = brp;
29687 +               err = 0;
29688 +       }
29689 +
29690 +       return err;
29691 +}
29692 +
29693 +/* ---------------------------------------------------------------------- */
29694 +
29695 +unsigned int au_sigen_inc(struct super_block *sb)
29696 +{
29697 +       unsigned int gen;
29698 +       struct inode *inode;
29699 +
29700 +       SiMustWriteLock(sb);
29701 +
29702 +       gen = ++au_sbi(sb)->si_generation;
29703 +       au_update_digen(sb->s_root);
29704 +       inode = d_inode(sb->s_root);
29705 +       au_update_iigen(inode, /*half*/0);
29706 +       inode_inc_iversion(inode);
29707 +       return gen;
29708 +}
29709 +
29710 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29711 +{
29712 +       aufs_bindex_t br_id;
29713 +       int i;
29714 +       struct au_sbinfo *sbinfo;
29715 +
29716 +       SiMustWriteLock(sb);
29717 +
29718 +       sbinfo = au_sbi(sb);
29719 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29720 +               br_id = ++sbinfo->si_last_br_id;
29721 +               AuDebugOn(br_id < 0);
29722 +               if (br_id && au_br_index(sb, br_id) < 0)
29723 +                       return br_id;
29724 +       }
29725 +
29726 +       return -1;
29727 +}
29728 +
29729 +/* ---------------------------------------------------------------------- */
29730 +
29731 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29732 +int si_read_lock(struct super_block *sb, int flags)
29733 +{
29734 +       int err;
29735 +
29736 +       err = 0;
29737 +       if (au_ftest_lock(flags, FLUSH))
29738 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29739 +
29740 +       si_noflush_read_lock(sb);
29741 +       err = au_plink_maint(sb, flags);
29742 +       if (unlikely(err))
29743 +               si_read_unlock(sb);
29744 +
29745 +       return err;
29746 +}
29747 +
29748 +int si_write_lock(struct super_block *sb, int flags)
29749 +{
29750 +       int err;
29751 +
29752 +       if (au_ftest_lock(flags, FLUSH))
29753 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29754 +
29755 +       si_noflush_write_lock(sb);
29756 +       err = au_plink_maint(sb, flags);
29757 +       if (unlikely(err))
29758 +               si_write_unlock(sb);
29759 +
29760 +       return err;
29761 +}
29762 +
29763 +/* dentry and super_block lock. call at entry point */
29764 +int aufs_read_lock(struct dentry *dentry, int flags)
29765 +{
29766 +       int err;
29767 +       struct super_block *sb;
29768 +
29769 +       sb = dentry->d_sb;
29770 +       err = si_read_lock(sb, flags);
29771 +       if (unlikely(err))
29772 +               goto out;
29773 +
29774 +       if (au_ftest_lock(flags, DW))
29775 +               di_write_lock_child(dentry);
29776 +       else
29777 +               di_read_lock_child(dentry, flags);
29778 +
29779 +       if (au_ftest_lock(flags, GEN)) {
29780 +               err = au_digen_test(dentry, au_sigen(sb));
29781 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29782 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29783 +               else if (!err)
29784 +                       err = au_dbrange_test(dentry);
29785 +               if (unlikely(err))
29786 +                       aufs_read_unlock(dentry, flags);
29787 +       }
29788 +
29789 +out:
29790 +       return err;
29791 +}
29792 +
29793 +void aufs_read_unlock(struct dentry *dentry, int flags)
29794 +{
29795 +       if (au_ftest_lock(flags, DW))
29796 +               di_write_unlock(dentry);
29797 +       else
29798 +               di_read_unlock(dentry, flags);
29799 +       si_read_unlock(dentry->d_sb);
29800 +}
29801 +
29802 +void aufs_write_lock(struct dentry *dentry)
29803 +{
29804 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29805 +       di_write_lock_child(dentry);
29806 +}
29807 +
29808 +void aufs_write_unlock(struct dentry *dentry)
29809 +{
29810 +       di_write_unlock(dentry);
29811 +       si_write_unlock(dentry->d_sb);
29812 +}
29813 +
29814 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29815 +{
29816 +       int err;
29817 +       unsigned int sigen;
29818 +       struct super_block *sb;
29819 +
29820 +       sb = d1->d_sb;
29821 +       err = si_read_lock(sb, flags);
29822 +       if (unlikely(err))
29823 +               goto out;
29824 +
29825 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29826 +
29827 +       if (au_ftest_lock(flags, GEN)) {
29828 +               sigen = au_sigen(sb);
29829 +               err = au_digen_test(d1, sigen);
29830 +               AuDebugOn(!err && au_dbrange_test(d1));
29831 +               if (!err) {
29832 +                       err = au_digen_test(d2, sigen);
29833 +                       AuDebugOn(!err && au_dbrange_test(d2));
29834 +               }
29835 +               if (unlikely(err))
29836 +                       aufs_read_and_write_unlock2(d1, d2);
29837 +       }
29838 +
29839 +out:
29840 +       return err;
29841 +}
29842 +
29843 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29844 +{
29845 +       di_write_unlock2(d1, d2);
29846 +       si_read_unlock(d1->d_sb);
29847 +}
29848 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29849 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29850 +++ linux/fs/aufs/super.c       2023-10-10 22:51:18.033248030 +0200
29851 @@ -0,0 +1,871 @@
29852 +// SPDX-License-Identifier: GPL-2.0
29853 +/*
29854 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29855 + *
29856 + * This program is free software; you can redistribute it and/or modify
29857 + * it under the terms of the GNU General Public License as published by
29858 + * the Free Software Foundation; either version 2 of the License, or
29859 + * (at your option) any later version.
29860 + *
29861 + * This program is distributed in the hope that it will be useful,
29862 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29863 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29864 + * GNU General Public License for more details.
29865 + *
29866 + * You should have received a copy of the GNU General Public License
29867 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29868 + */
29869 +
29870 +/*
29871 + * mount and super_block operations
29872 + */
29873 +
29874 +#include <linux/iversion.h>
29875 +#include <linux/mm.h>
29876 +#include <linux/seq_file.h>
29877 +#include <linux/statfs.h>
29878 +#include <linux/vmalloc.h>
29879 +#include "aufs.h"
29880 +
29881 +/*
29882 + * super_operations
29883 + */
29884 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29885 +{
29886 +       struct au_icntnr *c;
29887 +
29888 +       c = au_cache_alloc_icntnr(sb);
29889 +       if (c) {
29890 +               au_icntnr_init(c);
29891 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29892 +               c->iinfo.ii_hinode = NULL;
29893 +               return &c->vfs_inode;
29894 +       }
29895 +       return NULL;
29896 +}
29897 +
29898 +static void aufs_destroy_inode(struct inode *inode)
29899 +{
29900 +       if (!au_is_bad_inode(inode))
29901 +               au_iinfo_fin(inode);
29902 +}
29903 +
29904 +static void aufs_free_inode(struct inode *inode)
29905 +{
29906 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29907 +}
29908 +
29909 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29910 +{
29911 +       struct inode *inode;
29912 +       int err;
29913 +
29914 +       inode = iget_locked(sb, ino);
29915 +       if (unlikely(!inode)) {
29916 +               inode = ERR_PTR(-ENOMEM);
29917 +               goto out;
29918 +       }
29919 +       if (!(inode->i_state & I_NEW))
29920 +               goto out;
29921 +
29922 +       err = au_xigen_new(inode);
29923 +       if (!err)
29924 +               err = au_iinfo_init(inode);
29925 +       if (!err)
29926 +               inode_inc_iversion(inode);
29927 +       else {
29928 +               iget_failed(inode);
29929 +               inode = ERR_PTR(err);
29930 +       }
29931 +
29932 +out:
29933 +       /* never return NULL */
29934 +       AuDebugOn(!inode);
29935 +       AuTraceErrPtr(inode);
29936 +       return inode;
29937 +}
29938 +
29939 +/* lock free root dinfo */
29940 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29941 +{
29942 +       int err;
29943 +       aufs_bindex_t bindex, bbot;
29944 +       struct path path;
29945 +       struct au_hdentry *hdp;
29946 +       struct au_branch *br;
29947 +       au_br_perm_str_t perm;
29948 +
29949 +       err = 0;
29950 +       bbot = au_sbbot(sb);
29951 +       bindex = 0;
29952 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29953 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29954 +               br = au_sbr(sb, bindex);
29955 +               path.mnt = au_br_mnt(br);
29956 +               path.dentry = hdp->hd_dentry;
29957 +               err = au_seq_path(seq, &path);
29958 +               if (!err) {
29959 +                       au_optstr_br_perm(&perm, br->br_perm);
29960 +                       seq_printf(seq, "=%s", perm.a);
29961 +                       if (bindex != bbot)
29962 +                               seq_putc(seq, ':');
29963 +               }
29964 +       }
29965 +       if (unlikely(err || seq_has_overflowed(seq)))
29966 +               err = -E2BIG;
29967 +
29968 +       return err;
29969 +}
29970 +
29971 +static void au_gen_fmt(char *fmt, int len, const char *pat,
29972 +                      const char *append)
29973 +{
29974 +       char *p;
29975 +
29976 +       p = fmt;
29977 +       while (*pat != ':')
29978 +               *p++ = *pat++;
29979 +       *p++ = *pat++;
29980 +       strscpy(p, append, len - (p - fmt));
29981 +       AuDebugOn(strlen(fmt) >= len);
29982 +}
29983 +
29984 +static void au_show_wbr_create(struct seq_file *m, int v,
29985 +                              struct au_sbinfo *sbinfo)
29986 +{
29987 +       const char *pat;
29988 +       char fmt[32];
29989 +       struct au_wbr_mfs *mfs;
29990 +
29991 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29992 +
29993 +       seq_puts(m, ",create=");
29994 +       pat = au_optstr_wbr_create(v);
29995 +       mfs = &sbinfo->si_wbr_mfs;
29996 +       switch (v) {
29997 +       case AuWbrCreate_TDP:
29998 +       case AuWbrCreate_RR:
29999 +       case AuWbrCreate_MFS:
30000 +       case AuWbrCreate_PMFS:
30001 +               seq_puts(m, pat);
30002 +               break;
30003 +       case AuWbrCreate_MFSRR:
30004 +       case AuWbrCreate_TDMFS:
30005 +       case AuWbrCreate_PMFSRR:
30006 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
30007 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
30008 +               break;
30009 +       case AuWbrCreate_MFSV:
30010 +       case AuWbrCreate_PMFSV:
30011 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
30012 +               seq_printf(m, fmt,
30013 +                          jiffies_to_msecs(mfs->mfs_expire)
30014 +                          / MSEC_PER_SEC);
30015 +               break;
30016 +       case AuWbrCreate_MFSRRV:
30017 +       case AuWbrCreate_TDMFSV:
30018 +       case AuWbrCreate_PMFSRRV:
30019 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
30020 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
30021 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
30022 +               break;
30023 +       default:
30024 +               BUG();
30025 +       }
30026 +}
30027 +
30028 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
30029 +{
30030 +#ifdef CONFIG_SYSFS
30031 +       return 0;
30032 +#else
30033 +       int err;
30034 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
30035 +       aufs_bindex_t bindex, brid;
30036 +       struct qstr *name;
30037 +       struct file *f;
30038 +       struct dentry *d, *h_root;
30039 +       struct au_branch *br;
30040 +
30041 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30042 +
30043 +       err = 0;
30044 +       f = au_sbi(sb)->si_xib;
30045 +       if (!f)
30046 +               goto out;
30047 +
30048 +       /* stop printing the default xino path on the first writable branch */
30049 +       h_root = NULL;
30050 +       bindex = au_xi_root(sb, f->f_path.dentry);
30051 +       if (bindex >= 0) {
30052 +               br = au_sbr_sb(sb, bindex);
30053 +               h_root = au_br_dentry(br);
30054 +       }
30055 +
30056 +       d = f->f_path.dentry;
30057 +       name = &d->d_name;
30058 +       /* safe ->d_parent because the file is unlinked */
30059 +       if (d->d_parent == h_root
30060 +           && name->len == len
30061 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
30062 +               goto out;
30063 +
30064 +       seq_puts(seq, ",xino=");
30065 +       err = au_xino_path(seq, f);
30066 +
30067 +out:
30068 +       return err;
30069 +#endif
30070 +}
30071 +
30072 +/* seq_file will re-call me in case of too long string */
30073 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
30074 +{
30075 +       int err;
30076 +       unsigned int mnt_flags, v;
30077 +       struct super_block *sb;
30078 +       struct au_sbinfo *sbinfo;
30079 +
30080 +#define AuBool(name, str) do { \
30081 +       v = au_opt_test(mnt_flags, name); \
30082 +       if (v != au_opt_test(AuOpt_Def, name)) \
30083 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
30084 +} while (0)
30085 +
30086 +#define AuStr(name, str) do { \
30087 +       v = mnt_flags & AuOptMask_##name; \
30088 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
30089 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30090 +} while (0)
30091 +
30092 +#define AuUInt(name, str, val) do { \
30093 +       if (val != AUFS_##name##_DEF) \
30094 +               seq_printf(m, "," #str "=%u", val); \
30095 +} while (0)
30096 +
30097 +       sb = dentry->d_sb;
30098 +       if (sb->s_flags & SB_POSIXACL)
30099 +               seq_puts(m, ",acl");
30100 +#if 0 /* reserved for future use */
30101 +       if (sb->s_flags & SB_I_VERSION)
30102 +               seq_puts(m, ",i_version");
30103 +#endif
30104 +
30105 +       /* lock free root dinfo */
30106 +       si_noflush_read_lock(sb);
30107 +       sbinfo = au_sbi(sb);
30108 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30109 +
30110 +       mnt_flags = au_mntflags(sb);
30111 +       if (au_opt_test(mnt_flags, XINO)) {
30112 +               err = au_show_xino(m, sb);
30113 +               if (unlikely(err))
30114 +                       goto out;
30115 +       } else
30116 +               seq_puts(m, ",noxino");
30117 +
30118 +       AuBool(TRUNC_XINO, trunc_xino);
30119 +       AuStr(UDBA, udba);
30120 +       AuBool(SHWH, shwh);
30121 +       AuBool(PLINK, plink);
30122 +       AuBool(DIO, dio);
30123 +       AuBool(DIRPERM1, dirperm1);
30124 +
30125 +       v = sbinfo->si_wbr_create;
30126 +       if (v != AuWbrCreate_Def)
30127 +               au_show_wbr_create(m, v, sbinfo);
30128 +
30129 +       v = sbinfo->si_wbr_copyup;
30130 +       if (v != AuWbrCopyup_Def)
30131 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30132 +
30133 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30134 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30135 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30136 +
30137 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30138 +
30139 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30140 +       AuUInt(RDCACHE, rdcache, v);
30141 +
30142 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30143 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30144 +
30145 +       au_fhsm_show(m, sbinfo);
30146 +
30147 +       AuBool(DIRREN, dirren);
30148 +       AuBool(SUM, sum);
30149 +       /* AuBool(SUM_W, wsum); */
30150 +       AuBool(WARN_PERM, warn_perm);
30151 +       AuBool(VERBOSE, verbose);
30152 +
30153 +out:
30154 +       /* be sure to print "br:" last */
30155 +       if (!sysaufs_brs) {
30156 +               seq_puts(m, ",br:");
30157 +               au_show_brs(m, sb);
30158 +       }
30159 +       si_read_unlock(sb);
30160 +       return 0;
30161 +
30162 +#undef AuBool
30163 +#undef AuStr
30164 +#undef AuUInt
30165 +}
30166 +
30167 +/* ---------------------------------------------------------------------- */
30168 +
30169 +/* sum mode which returns the summation for statfs(2) */
30170 +
30171 +static u64 au_add_till_max(u64 a, u64 b)
30172 +{
30173 +       u64 old;
30174 +
30175 +       old = a;
30176 +       a += b;
30177 +       if (old <= a)
30178 +               return a;
30179 +       return ULLONG_MAX;
30180 +}
30181 +
30182 +static u64 au_mul_till_max(u64 a, long mul)
30183 +{
30184 +       u64 old;
30185 +
30186 +       old = a;
30187 +       a *= mul;
30188 +       if (old <= a)
30189 +               return a;
30190 +       return ULLONG_MAX;
30191 +}
30192 +
30193 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30194 +{
30195 +       int err;
30196 +       long bsize, factor;
30197 +       u64 blocks, bfree, bavail, files, ffree;
30198 +       aufs_bindex_t bbot, bindex, i;
30199 +       unsigned char shared;
30200 +       struct path h_path;
30201 +       struct super_block *h_sb;
30202 +
30203 +       err = 0;
30204 +       bsize = LONG_MAX;
30205 +       files = 0;
30206 +       ffree = 0;
30207 +       blocks = 0;
30208 +       bfree = 0;
30209 +       bavail = 0;
30210 +       bbot = au_sbbot(sb);
30211 +       for (bindex = 0; bindex <= bbot; bindex++) {
30212 +               h_path.mnt = au_sbr_mnt(sb, bindex);
30213 +               h_sb = h_path.mnt->mnt_sb;
30214 +               shared = 0;
30215 +               for (i = 0; !shared && i < bindex; i++)
30216 +                       shared = (au_sbr_sb(sb, i) == h_sb);
30217 +               if (shared)
30218 +                       continue;
30219 +
30220 +               /* sb->s_root for NFS is unreliable */
30221 +               h_path.dentry = h_path.mnt->mnt_root;
30222 +               err = vfs_statfs(&h_path, buf);
30223 +               if (unlikely(err))
30224 +                       goto out;
30225 +
30226 +               if (bsize > buf->f_bsize) {
30227 +                       /*
30228 +                        * we will reduce bsize, so we have to expand blocks
30229 +                        * etc. to match them again
30230 +                        */
30231 +                       factor = (bsize / buf->f_bsize);
30232 +                       blocks = au_mul_till_max(blocks, factor);
30233 +                       bfree = au_mul_till_max(bfree, factor);
30234 +                       bavail = au_mul_till_max(bavail, factor);
30235 +                       bsize = buf->f_bsize;
30236 +               }
30237 +
30238 +               factor = (buf->f_bsize / bsize);
30239 +               blocks = au_add_till_max(blocks,
30240 +                               au_mul_till_max(buf->f_blocks, factor));
30241 +               bfree = au_add_till_max(bfree,
30242 +                               au_mul_till_max(buf->f_bfree, factor));
30243 +               bavail = au_add_till_max(bavail,
30244 +                               au_mul_till_max(buf->f_bavail, factor));
30245 +               files = au_add_till_max(files, buf->f_files);
30246 +               ffree = au_add_till_max(ffree, buf->f_ffree);
30247 +       }
30248 +
30249 +       buf->f_bsize = bsize;
30250 +       buf->f_blocks = blocks;
30251 +       buf->f_bfree = bfree;
30252 +       buf->f_bavail = bavail;
30253 +       buf->f_files = files;
30254 +       buf->f_ffree = ffree;
30255 +       buf->f_frsize = 0;
30256 +
30257 +out:
30258 +       return err;
30259 +}
30260 +
30261 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30262 +{
30263 +       int err;
30264 +       struct path h_path;
30265 +       struct super_block *sb;
30266 +
30267 +       /* lock free root dinfo */
30268 +       sb = dentry->d_sb;
30269 +       si_noflush_read_lock(sb);
30270 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
30271 +               /* sb->s_root for NFS is unreliable */
30272 +               h_path.mnt = au_sbr_mnt(sb, 0);
30273 +               h_path.dentry = h_path.mnt->mnt_root;
30274 +               err = vfs_statfs(&h_path, buf);
30275 +       } else
30276 +               err = au_statfs_sum(sb, buf);
30277 +       si_read_unlock(sb);
30278 +
30279 +       if (!err) {
30280 +               buf->f_type = AUFS_SUPER_MAGIC;
30281 +               buf->f_namelen = AUFS_MAX_NAMELEN;
30282 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30283 +       }
30284 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30285 +
30286 +       return err;
30287 +}
30288 +
30289 +/* ---------------------------------------------------------------------- */
30290 +
30291 +static int aufs_sync_fs(struct super_block *sb, int wait)
30292 +{
30293 +       int err, e;
30294 +       aufs_bindex_t bbot, bindex;
30295 +       struct au_branch *br;
30296 +       struct super_block *h_sb;
30297 +
30298 +       err = 0;
30299 +       si_noflush_read_lock(sb);
30300 +       bbot = au_sbbot(sb);
30301 +       for (bindex = 0; bindex <= bbot; bindex++) {
30302 +               br = au_sbr(sb, bindex);
30303 +               if (!au_br_writable(br->br_perm))
30304 +                       continue;
30305 +
30306 +               h_sb = au_sbr_sb(sb, bindex);
30307 +               e = vfsub_sync_filesystem(h_sb);
30308 +               if (unlikely(e && !err))
30309 +                       err = e;
30310 +               /* go on even if an error happens */
30311 +       }
30312 +       si_read_unlock(sb);
30313 +
30314 +       return err;
30315 +}
30316 +
30317 +/* ---------------------------------------------------------------------- */
30318 +
30319 +/* final actions when unmounting a file system */
30320 +static void aufs_put_super(struct super_block *sb)
30321 +{
30322 +       struct au_sbinfo *sbinfo;
30323 +
30324 +       sbinfo = au_sbi(sb);
30325 +       if (sbinfo)
30326 +               kobject_put(&sbinfo->si_kobj);
30327 +}
30328 +
30329 +/* ---------------------------------------------------------------------- */
30330 +
30331 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30332 +                    struct super_block *sb, void *arg)
30333 +{
30334 +       void *array;
30335 +       unsigned long long n, sz;
30336 +
30337 +       array = NULL;
30338 +       n = 0;
30339 +       if (!*hint)
30340 +               goto out;
30341 +
30342 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30343 +               array = ERR_PTR(-EMFILE);
30344 +               pr_err("hint %llu\n", *hint);
30345 +               goto out;
30346 +       }
30347 +
30348 +       sz = sizeof(array) * *hint;
30349 +       array = kzalloc(sz, GFP_NOFS);
30350 +       if (unlikely(!array))
30351 +               array = vzalloc(sz);
30352 +       if (unlikely(!array)) {
30353 +               array = ERR_PTR(-ENOMEM);
30354 +               goto out;
30355 +       }
30356 +
30357 +       n = cb(sb, array, *hint, arg);
30358 +       AuDebugOn(n > *hint);
30359 +
30360 +out:
30361 +       *hint = n;
30362 +       return array;
30363 +}
30364 +
30365 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30366 +                                      unsigned long long max __maybe_unused,
30367 +                                      void *arg)
30368 +{
30369 +       unsigned long long n;
30370 +       struct inode **p, *inode;
30371 +       struct list_head *head;
30372 +
30373 +       n = 0;
30374 +       p = a;
30375 +       head = arg;
30376 +       spin_lock(&sb->s_inode_list_lock);
30377 +       list_for_each_entry(inode, head, i_sb_list) {
30378 +               if (!au_is_bad_inode(inode)
30379 +                   && au_ii(inode)->ii_btop >= 0) {
30380 +                       spin_lock(&inode->i_lock);
30381 +                       if (atomic_read(&inode->i_count)) {
30382 +                               au_igrab(inode);
30383 +                               *p++ = inode;
30384 +                               n++;
30385 +                               AuDebugOn(n > max);
30386 +                       }
30387 +                       spin_unlock(&inode->i_lock);
30388 +               }
30389 +       }
30390 +       spin_unlock(&sb->s_inode_list_lock);
30391 +
30392 +       return n;
30393 +}
30394 +
30395 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30396 +{
30397 +       struct au_sbinfo *sbi;
30398 +
30399 +       sbi = au_sbi(sb);
30400 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30401 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30402 +}
30403 +
30404 +void au_iarray_free(struct inode **a, unsigned long long max)
30405 +{
30406 +       unsigned long long ull;
30407 +
30408 +       for (ull = 0; ull < max; ull++)
30409 +               iput(a[ull]);
30410 +       kvfree(a);
30411 +}
30412 +
30413 +/* ---------------------------------------------------------------------- */
30414 +
30415 +/*
30416 + * refresh dentry and inode at remount time.
30417 + */
30418 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30419 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30420 +                     struct dentry *parent)
30421 +{
30422 +       int err;
30423 +
30424 +       di_write_lock_child(dentry);
30425 +       di_read_lock_parent(parent, AuLock_IR);
30426 +       err = au_refresh_dentry(dentry, parent);
30427 +       if (!err && dir_flags)
30428 +               au_hn_reset(d_inode(dentry), dir_flags);
30429 +       di_read_unlock(parent, AuLock_IR);
30430 +       di_write_unlock(dentry);
30431 +
30432 +       return err;
30433 +}
30434 +
30435 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30436 +                          struct au_sbinfo *sbinfo,
30437 +                          const unsigned int dir_flags, unsigned int do_idop)
30438 +{
30439 +       int err;
30440 +       struct dentry *parent;
30441 +
30442 +       err = 0;
30443 +       parent = dget_parent(dentry);
30444 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30445 +               if (d_really_is_positive(dentry)) {
30446 +                       if (!d_is_dir(dentry))
30447 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30448 +                                                parent);
30449 +                       else {
30450 +                               err = au_do_refresh(dentry, dir_flags, parent);
30451 +                               if (unlikely(err))
30452 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30453 +                       }
30454 +               } else
30455 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30456 +               AuDbgDentry(dentry);
30457 +       }
30458 +       dput(parent);
30459 +
30460 +       if (!err) {
30461 +               if (do_idop)
30462 +                       au_refresh_dop(dentry, /*force_reval*/0);
30463 +       } else
30464 +               au_refresh_dop(dentry, /*force_reval*/1);
30465 +
30466 +       AuTraceErr(err);
30467 +       return err;
30468 +}
30469 +
30470 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30471 +{
30472 +       int err, i, j, ndentry, e;
30473 +       unsigned int sigen;
30474 +       struct au_dcsub_pages dpages;
30475 +       struct au_dpage *dpage;
30476 +       struct dentry **dentries, *d;
30477 +       struct au_sbinfo *sbinfo;
30478 +       struct dentry *root = sb->s_root;
30479 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30480 +
30481 +       if (do_idop)
30482 +               au_refresh_dop(root, /*force_reval*/0);
30483 +
30484 +       err = au_dpages_init(&dpages, GFP_NOFS);
30485 +       if (unlikely(err))
30486 +               goto out;
30487 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30488 +       if (unlikely(err))
30489 +               goto out_dpages;
30490 +
30491 +       sigen = au_sigen(sb);
30492 +       sbinfo = au_sbi(sb);
30493 +       for (i = 0; i < dpages.ndpage; i++) {
30494 +               dpage = dpages.dpages + i;
30495 +               dentries = dpage->dentries;
30496 +               ndentry = dpage->ndentry;
30497 +               for (j = 0; j < ndentry; j++) {
30498 +                       d = dentries[j];
30499 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30500 +                                           do_idop);
30501 +                       if (unlikely(e && !err))
30502 +                               err = e;
30503 +                       /* go on even err */
30504 +               }
30505 +       }
30506 +
30507 +out_dpages:
30508 +       au_dpages_free(&dpages);
30509 +out:
30510 +       return err;
30511 +}
30512 +
30513 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30514 +{
30515 +       int err, e;
30516 +       unsigned int sigen;
30517 +       unsigned long long max, ull;
30518 +       struct inode *inode, **array;
30519 +
30520 +       array = au_iarray_alloc(sb, &max);
30521 +       err = PTR_ERR(array);
30522 +       if (IS_ERR(array))
30523 +               goto out;
30524 +
30525 +       err = 0;
30526 +       sigen = au_sigen(sb);
30527 +       for (ull = 0; ull < max; ull++) {
30528 +               inode = array[ull];
30529 +               if (unlikely(!inode))
30530 +                       break;
30531 +
30532 +               e = 0;
30533 +               ii_write_lock_child(inode);
30534 +               if (au_iigen(inode, NULL) != sigen) {
30535 +                       e = au_refresh_hinode_self(inode);
30536 +                       if (unlikely(e)) {
30537 +                               au_refresh_iop(inode, /*force_getattr*/1);
30538 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30539 +                               if (!err)
30540 +                                       err = e;
30541 +                               /* go on even if err */
30542 +                       }
30543 +               }
30544 +               if (!e && do_idop)
30545 +                       au_refresh_iop(inode, /*force_getattr*/0);
30546 +               ii_write_unlock(inode);
30547 +       }
30548 +
30549 +       au_iarray_free(array, max);
30550 +
30551 +out:
30552 +       return err;
30553 +}
30554 +
30555 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30556 +{
30557 +       int err, e;
30558 +       unsigned int udba;
30559 +       aufs_bindex_t bindex, bbot;
30560 +       struct dentry *root;
30561 +       struct inode *inode;
30562 +       struct au_branch *br;
30563 +       struct au_sbinfo *sbi;
30564 +
30565 +       au_sigen_inc(sb);
30566 +       sbi = au_sbi(sb);
30567 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30568 +
30569 +       root = sb->s_root;
30570 +       DiMustNoWaiters(root);
30571 +       inode = d_inode(root);
30572 +       IiMustNoWaiters(inode);
30573 +
30574 +       udba = au_opt_udba(sb);
30575 +       bbot = au_sbbot(sb);
30576 +       for (bindex = 0; bindex <= bbot; bindex++) {
30577 +               br = au_sbr(sb, bindex);
30578 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30579 +               if (unlikely(err))
30580 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30581 +                               bindex, err);
30582 +               /* go on even if err */
30583 +       }
30584 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30585 +
30586 +       if (do_idop) {
30587 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30588 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30589 +                       sb->s_d_op = &aufs_dop_noreval;
30590 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30591 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30592 +               } else {
30593 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30594 +                       sb->s_d_op = &aufs_dop;
30595 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30596 +                       sbi->si_iop_array = aufs_iop;
30597 +               }
30598 +               pr_info("reset to %ps and %ps\n",
30599 +                       sb->s_d_op, sbi->si_iop_array);
30600 +       }
30601 +
30602 +       di_write_unlock(root);
30603 +       err = au_refresh_d(sb, do_idop);
30604 +       e = au_refresh_i(sb, do_idop);
30605 +       if (unlikely(e && !err))
30606 +               err = e;
30607 +       /* aufs_write_lock() calls ..._child() */
30608 +       di_write_lock_child(root);
30609 +
30610 +       au_cpup_attr_all(inode, /*force*/1);
30611 +
30612 +       if (unlikely(err))
30613 +               AuIOErr("refresh failed, ignored, %d\n", err);
30614 +}
30615 +
30616 +const struct super_operations aufs_sop = {
30617 +       .alloc_inode    = aufs_alloc_inode,
30618 +       .destroy_inode  = aufs_destroy_inode,
30619 +       .free_inode     = aufs_free_inode,
30620 +       /* always deleting, no clearing */
30621 +       .drop_inode     = generic_delete_inode,
30622 +       .show_options   = aufs_show_options,
30623 +       .statfs         = aufs_statfs,
30624 +       .put_super      = aufs_put_super,
30625 +       .sync_fs        = aufs_sync_fs
30626 +};
30627 +
30628 +/* ---------------------------------------------------------------------- */
30629 +
30630 +int au_alloc_root(struct super_block *sb)
30631 +{
30632 +       int err;
30633 +       struct inode *inode;
30634 +       struct dentry *root;
30635 +
30636 +       err = -ENOMEM;
30637 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30638 +       err = PTR_ERR(inode);
30639 +       if (IS_ERR(inode))
30640 +               goto out;
30641 +
30642 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30643 +       inode->i_fop = &aufs_dir_fop;
30644 +       inode->i_mode = S_IFDIR;
30645 +       set_nlink(inode, 2);
30646 +       unlock_new_inode(inode);
30647 +
30648 +       root = d_make_root(inode);
30649 +       if (unlikely(!root))
30650 +               goto out;
30651 +       err = PTR_ERR(root);
30652 +       if (IS_ERR(root))
30653 +               goto out;
30654 +
30655 +       err = au_di_init(root);
30656 +       if (!err) {
30657 +               sb->s_root = root;
30658 +               return 0; /* success */
30659 +       }
30660 +       dput(root);
30661 +
30662 +out:
30663 +       return err;
30664 +}
30665 +
30666 +/* ---------------------------------------------------------------------- */
30667 +
30668 +static void aufs_kill_sb(struct super_block *sb)
30669 +{
30670 +       struct au_sbinfo *sbinfo;
30671 +       struct dentry *root;
30672 +
30673 +       sbinfo = au_sbi(sb);
30674 +       if (!sbinfo)
30675 +               goto out;
30676 +
30677 +       au_sbilist_del(sb);
30678 +
30679 +       root = sb->s_root;
30680 +       if (root)
30681 +               aufs_write_lock(root);
30682 +       else
30683 +               __si_write_lock(sb);
30684 +
30685 +       au_fhsm_fin(sb);
30686 +       if (sbinfo->si_wbr_create_ops->fin)
30687 +               sbinfo->si_wbr_create_ops->fin(sb);
30688 +       if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30689 +               au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30690 +               au_remount_refresh(sb, /*do_idop*/0);
30691 +       }
30692 +       if (au_opt_test(sbinfo->si_mntflags, PLINK))
30693 +               au_plink_put(sb, /*verbose*/1);
30694 +       au_xino_clr(sb);
30695 +       if (root)
30696 +               au_dr_opt_flush(sb);
30697 +
30698 +       if (root)
30699 +               aufs_write_unlock(root);
30700 +       else
30701 +               __si_write_unlock(sb);
30702 +
30703 +       sbinfo->si_sb = NULL;
30704 +       au_nwt_flush(&sbinfo->si_nowait);
30705 +
30706 +out:
30707 +       kill_anon_super(sb);
30708 +}
30709 +
30710 +struct file_system_type aufs_fs_type = {
30711 +       .name           = AUFS_FSTYPE,
30712 +       /* a race between rename and others */
30713 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30714 +                               /* untested */
30715 +                               /*| FS_ALLOW_IDMAP*/
30716 +                               ,
30717 +       .init_fs_context = aufs_fsctx_init,
30718 +       .parameters     = aufs_fsctx_paramspec,
30719 +       .kill_sb        = aufs_kill_sb,
30720 +       /* no need to __module_get() and module_put(). */
30721 +       .owner          = THIS_MODULE,
30722 +};
30723 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30724 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30725 +++ linux/fs/aufs/super.h       2022-11-05 23:02:18.969222617 +0100
30726 @@ -0,0 +1,592 @@
30727 +/* SPDX-License-Identifier: GPL-2.0 */
30728 +/*
30729 + * Copyright (C) 2005-2022 Junjiro R. Okajima
30730 + *
30731 + * This program is free software; you can redistribute it and/or modify
30732 + * it under the terms of the GNU General Public License as published by
30733 + * the Free Software Foundation; either version 2 of the License, or
30734 + * (at your option) any later version.
30735 + *
30736 + * This program is distributed in the hope that it will be useful,
30737 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30738 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30739 + * GNU General Public License for more details.
30740 + *
30741 + * You should have received a copy of the GNU General Public License
30742 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30743 + */
30744 +
30745 +/*
30746 + * super_block operations
30747 + */
30748 +
30749 +#ifndef __AUFS_SUPER_H__
30750 +#define __AUFS_SUPER_H__
30751 +
30752 +#ifdef __KERNEL__
30753 +
30754 +#include <linux/fs.h>
30755 +#include <linux/kobject.h>
30756 +#include "hbl.h"
30757 +#include "lcnt.h"
30758 +#include "rwsem.h"
30759 +#include "wkq.h"
30760 +
30761 +/* policies to select one among multiple writable branches */
30762 +struct au_wbr_copyup_operations {
30763 +       int (*copyup)(struct dentry *dentry);
30764 +};
30765 +
30766 +#define AuWbr_DIR      1               /* target is a dir */
30767 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30768 +
30769 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30770 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30771 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30772 +
30773 +struct au_wbr_create_operations {
30774 +       int (*create)(struct dentry *dentry, unsigned int flags);
30775 +       int (*init)(struct super_block *sb);
30776 +       int (*fin)(struct super_block *sb);
30777 +};
30778 +
30779 +struct au_wbr_mfs {
30780 +       struct mutex    mfs_lock; /* protect this structure */
30781 +       unsigned long   mfs_jiffy;
30782 +       unsigned long   mfs_expire;
30783 +       aufs_bindex_t   mfs_bindex;
30784 +
30785 +       unsigned long long      mfsrr_bytes;
30786 +       unsigned long long      mfsrr_watermark;
30787 +};
30788 +
30789 +#define AuPlink_NHASH 100
30790 +static inline int au_plink_hash(ino_t ino)
30791 +{
30792 +       return ino % AuPlink_NHASH;
30793 +}
30794 +
30795 +/* File-based Hierarchical Storage Management */
30796 +struct au_fhsm {
30797 +#ifdef CONFIG_AUFS_FHSM
30798 +       /* allow only one process who can receive the notification */
30799 +       spinlock_t              fhsm_spin;
30800 +       pid_t                   fhsm_pid;
30801 +       wait_queue_head_t       fhsm_wqh;
30802 +       atomic_t                fhsm_readable;
30803 +
30804 +       /* these are protected by si_rwsem */
30805 +       unsigned long           fhsm_expire;
30806 +       aufs_bindex_t           fhsm_bottom;
30807 +#endif
30808 +};
30809 +
30810 +struct au_branch;
30811 +struct au_sbinfo {
30812 +       /* nowait tasks in the system-wide workqueue */
30813 +       struct au_nowait_tasks  si_nowait;
30814 +
30815 +       /*
30816 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30817 +        * rwsem for au_sbinfo is necessary.
30818 +        */
30819 +       struct au_rwsem         si_rwsem;
30820 +
30821 +       /*
30822 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30823 +        * remount.
30824 +        */
30825 +       au_lcnt_t               si_ninodes, si_nfiles;
30826 +
30827 +       /* branch management */
30828 +       unsigned int            si_generation;
30829 +
30830 +       /* see AuSi_ flags */
30831 +       unsigned char           au_si_status;
30832 +
30833 +       aufs_bindex_t           si_bbot;
30834 +
30835 +       /* dirty trick to keep br_id plus */
30836 +       unsigned int            si_last_br_id :
30837 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30838 +       struct au_branch        **si_branch;
30839 +
30840 +       /* policy to select a writable branch */
30841 +       unsigned char           si_wbr_copyup;
30842 +       unsigned char           si_wbr_create;
30843 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30844 +       struct au_wbr_create_operations *si_wbr_create_ops;
30845 +
30846 +       /* round robin */
30847 +       atomic_t                si_wbr_rr_next;
30848 +
30849 +       /* most free space */
30850 +       struct au_wbr_mfs       si_wbr_mfs;
30851 +
30852 +       /* File-based Hierarchical Storage Management */
30853 +       struct au_fhsm          si_fhsm;
30854 +
30855 +       /* mount flags */
30856 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30857 +       unsigned int            si_mntflags;
30858 +
30859 +       /* external inode number (bitmap and translation table) */
30860 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30861 +
30862 +       struct file             *si_xib;
30863 +       struct mutex            si_xib_mtx; /* protect xib members */
30864 +       unsigned long           *si_xib_buf;
30865 +       unsigned long           si_xib_last_pindex;
30866 +       int                     si_xib_next_bit;
30867 +
30868 +       unsigned long           si_xino_jiffy;
30869 +       unsigned long           si_xino_expire;
30870 +       /* reserved for future use */
30871 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30872 +
30873 +#ifdef CONFIG_AUFS_EXPORT
30874 +       /* i_generation */
30875 +       /* todo: make xigen file an array to support many inode numbers */
30876 +       struct file             *si_xigen;
30877 +       atomic_t                si_xigen_next;
30878 +#endif
30879 +
30880 +       /* dirty trick to support atomic_open */
30881 +       struct hlist_bl_head    si_aopen;
30882 +
30883 +       /* vdir parameters */
30884 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30885 +       unsigned int            si_rdblk;       /* deblk size */
30886 +       unsigned int            si_rdhash;      /* hash size */
30887 +
30888 +       /*
30889 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30890 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30891 +        * future fsck.aufs or kernel thread will remove them later.
30892 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30893 +        */
30894 +       unsigned int            si_dirwh;
30895 +
30896 +       /* pseudo_link list */
30897 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30898 +       wait_queue_head_t       si_plink_wq;
30899 +       spinlock_t              si_plink_maint_lock;
30900 +       pid_t                   si_plink_maint_pid;
30901 +
30902 +       /* file list */
30903 +       struct hlist_bl_head    si_files;
30904 +
30905 +       /* with/without getattr, brother of sb->s_d_op */
30906 +       const struct inode_operations *si_iop_array;
30907 +
30908 +       /*
30909 +        * sysfs and lifetime management.
30910 +        * this is not a small structure and it may be a waste of memory in case
30911 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30912 +        * but using sysfs is majority.
30913 +        */
30914 +       struct kobject          si_kobj;
30915 +#ifdef CONFIG_DEBUG_FS
30916 +       struct dentry            *si_dbgaufs;
30917 +       struct dentry            *si_dbgaufs_plink;
30918 +       struct dentry            *si_dbgaufs_xib;
30919 +#ifdef CONFIG_AUFS_EXPORT
30920 +       struct dentry            *si_dbgaufs_xigen;
30921 +#endif
30922 +#endif
30923 +
30924 +#ifdef CONFIG_AUFS_SBILIST
30925 +       struct hlist_bl_node    si_list;
30926 +#endif
30927 +
30928 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30929 +       struct super_block      *si_sb;
30930 +};
30931 +
30932 +/* sbinfo status flags */
30933 +/*
30934 + * set true when refresh_dirs() failed at remount time.
30935 + * then try refreshing dirs at access time again.
30936 + * if it is false, refreshing dirs at access time is unnecessary
30937 + */
30938 +#define AuSi_FAILED_REFRESH_DIR        1
30939 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30940 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30941 +
30942 +#ifndef CONFIG_AUFS_FHSM
30943 +#undef AuSi_FHSM
30944 +#define AuSi_FHSM              0
30945 +#endif
30946 +
30947 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30948 +                                          unsigned int flag)
30949 +{
30950 +       AuRwMustAnyLock(&sbi->si_rwsem);
30951 +       return sbi->au_si_status & flag;
30952 +}
30953 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30954 +#define au_fset_si(sbinfo, name) do { \
30955 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30956 +       (sbinfo)->au_si_status |= AuSi_##name; \
30957 +} while (0)
30958 +#define au_fclr_si(sbinfo, name) do { \
30959 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30960 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30961 +} while (0)
30962 +
30963 +/* ---------------------------------------------------------------------- */
30964 +
30965 +/* policy to select one among writable branches */
30966 +#define AuWbrCopyup(sbinfo, ...) \
30967 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30968 +#define AuWbrCreate(sbinfo, ...) \
30969 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30970 +
30971 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30972 +#define AuLock_DW              1               /* write-lock dentry */
30973 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30974 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30975 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30976 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30977 +                                               /* except RENAME_EXCHANGE */
30978 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30979 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30980 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30981 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30982 +#define au_fset_lock(flags, name) \
30983 +       do { (flags) |= AuLock_##name; } while (0)
30984 +#define au_fclr_lock(flags, name) \
30985 +       do { (flags) &= ~AuLock_##name; } while (0)
30986 +
30987 +/* ---------------------------------------------------------------------- */
30988 +
30989 +/* super.c */
30990 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30991 +
30992 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30993 +                                          unsigned long long max, void *arg);
30994 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30995 +                    struct super_block *sb, void *arg);
30996 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30997 +void au_iarray_free(struct inode **a, unsigned long long max);
30998 +
30999 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
31000 +extern const struct super_operations aufs_sop;
31001 +int au_alloc_root(struct super_block *sb);
31002 +extern struct file_system_type aufs_fs_type;
31003 +
31004 +/* sbinfo.c */
31005 +void au_si_free(struct kobject *kobj);
31006 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
31007 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
31008 +
31009 +unsigned int au_sigen_inc(struct super_block *sb);
31010 +aufs_bindex_t au_new_br_id(struct super_block *sb);
31011 +
31012 +int si_read_lock(struct super_block *sb, int flags);
31013 +int si_write_lock(struct super_block *sb, int flags);
31014 +int aufs_read_lock(struct dentry *dentry, int flags);
31015 +void aufs_read_unlock(struct dentry *dentry, int flags);
31016 +void aufs_write_lock(struct dentry *dentry);
31017 +void aufs_write_unlock(struct dentry *dentry);
31018 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
31019 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
31020 +
31021 +/* wbr_policy.c */
31022 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
31023 +extern struct au_wbr_create_operations au_wbr_create_ops[];
31024 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
31025 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
31026 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
31027 +
31028 +/* mvdown.c */
31029 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
31030 +
31031 +#ifdef CONFIG_AUFS_FHSM
31032 +/* fhsm.c */
31033 +
31034 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
31035 +{
31036 +       pid_t pid;
31037 +
31038 +       spin_lock(&fhsm->fhsm_spin);
31039 +       pid = fhsm->fhsm_pid;
31040 +       spin_unlock(&fhsm->fhsm_spin);
31041 +
31042 +       return pid;
31043 +}
31044 +
31045 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
31046 +void au_fhsm_wrote_all(struct super_block *sb, int force);
31047 +int au_fhsm_fd(struct super_block *sb, int oflags);
31048 +int au_fhsm_br_alloc(struct au_branch *br);
31049 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
31050 +void au_fhsm_fin(struct super_block *sb);
31051 +void au_fhsm_init(struct au_sbinfo *sbinfo);
31052 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
31053 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
31054 +#else
31055 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
31056 +          int force)
31057 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
31058 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
31059 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
31060 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
31061 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
31062 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
31063 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
31064 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
31065 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31066 +#endif
31067 +
31068 +/* ---------------------------------------------------------------------- */
31069 +
31070 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31071 +{
31072 +       return sb->s_fs_info;
31073 +}
31074 +
31075 +/* ---------------------------------------------------------------------- */
31076 +
31077 +#ifdef CONFIG_AUFS_EXPORT
31078 +int au_test_nfsd(void);
31079 +void au_export_init(struct super_block *sb);
31080 +void au_xigen_inc(struct inode *inode);
31081 +int au_xigen_new(struct inode *inode);
31082 +int au_xigen_set(struct super_block *sb, struct path *path);
31083 +void au_xigen_clr(struct super_block *sb);
31084 +
31085 +static inline int au_busy_or_stale(void)
31086 +{
31087 +       if (!au_test_nfsd())
31088 +               return -EBUSY;
31089 +       return -ESTALE;
31090 +}
31091 +#else
31092 +AuStubInt0(au_test_nfsd, void)
31093 +AuStubVoid(au_export_init, struct super_block *sb)
31094 +AuStubVoid(au_xigen_inc, struct inode *inode)
31095 +AuStubInt0(au_xigen_new, struct inode *inode)
31096 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
31097 +AuStubVoid(au_xigen_clr, struct super_block *sb)
31098 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
31099 +#endif /* CONFIG_AUFS_EXPORT */
31100 +
31101 +/* ---------------------------------------------------------------------- */
31102 +
31103 +#ifdef CONFIG_AUFS_SBILIST
31104 +/* module.c */
31105 +extern struct hlist_bl_head au_sbilist;
31106 +
31107 +static inline void au_sbilist_init(void)
31108 +{
31109 +       INIT_HLIST_BL_HEAD(&au_sbilist);
31110 +}
31111 +
31112 +static inline void au_sbilist_add(struct super_block *sb)
31113 +{
31114 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
31115 +}
31116 +
31117 +static inline void au_sbilist_del(struct super_block *sb)
31118 +{
31119 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
31120 +}
31121 +
31122 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31123 +static inline void au_sbilist_lock(void)
31124 +{
31125 +       hlist_bl_lock(&au_sbilist);
31126 +}
31127 +
31128 +static inline void au_sbilist_unlock(void)
31129 +{
31130 +       hlist_bl_unlock(&au_sbilist);
31131 +}
31132 +#define AuGFP_SBILIST  GFP_ATOMIC
31133 +#else
31134 +AuStubVoid(au_sbilist_lock, void)
31135 +AuStubVoid(au_sbilist_unlock, void)
31136 +#define AuGFP_SBILIST  GFP_NOFS
31137 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
31138 +#else
31139 +AuStubVoid(au_sbilist_init, void)
31140 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31141 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31142 +AuStubVoid(au_sbilist_lock, void)
31143 +AuStubVoid(au_sbilist_unlock, void)
31144 +#define AuGFP_SBILIST  GFP_NOFS
31145 +#endif
31146 +
31147 +/* ---------------------------------------------------------------------- */
31148 +
31149 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31150 +{
31151 +       /*
31152 +        * This function is a dynamic '__init' function actually,
31153 +        * so the tiny check for si_rwsem is unnecessary.
31154 +        */
31155 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31156 +#ifdef CONFIG_DEBUG_FS
31157 +       sbinfo->si_dbgaufs = NULL;
31158 +       sbinfo->si_dbgaufs_plink = NULL;
31159 +       sbinfo->si_dbgaufs_xib = NULL;
31160 +#ifdef CONFIG_AUFS_EXPORT
31161 +       sbinfo->si_dbgaufs_xigen = NULL;
31162 +#endif
31163 +#endif
31164 +}
31165 +
31166 +/* ---------------------------------------------------------------------- */
31167 +
31168 +/* current->atomic_flags */
31169 +/* this value should never corrupt the ones defined in linux/sched.h */
31170 +#define PFA_AUFS       0x10
31171 +
31172 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31173 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31174 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31175 +
31176 +static inline int si_pid_test(struct super_block *sb)
31177 +{
31178 +       return !!task_test_aufs(current);
31179 +}
31180 +
31181 +static inline void si_pid_clr(struct super_block *sb)
31182 +{
31183 +       AuDebugOn(!task_test_aufs(current));
31184 +       task_clear_aufs(current);
31185 +}
31186 +
31187 +static inline void si_pid_set(struct super_block *sb)
31188 +{
31189 +       AuDebugOn(task_test_aufs(current));
31190 +       task_set_aufs(current);
31191 +}
31192 +
31193 +/* ---------------------------------------------------------------------- */
31194 +
31195 +/* lock superblock. mainly for entry point functions */
31196 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31197 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31198 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31199 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31200 +/*
31201 +#define __si_read_trylock_nested(sb) \
31202 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31203 +#define __si_write_trylock_nested(sb) \
31204 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31205 +*/
31206 +
31207 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31208 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31209 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31210 +
31211 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31212 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31213 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31214 +
31215 +static inline void si_noflush_read_lock(struct super_block *sb)
31216 +{
31217 +       __si_read_lock(sb);
31218 +       si_pid_set(sb);
31219 +}
31220 +
31221 +static inline int si_noflush_read_trylock(struct super_block *sb)
31222 +{
31223 +       int locked;
31224 +
31225 +       locked = __si_read_trylock(sb);
31226 +       if (locked)
31227 +               si_pid_set(sb);
31228 +       return locked;
31229 +}
31230 +
31231 +static inline void si_noflush_write_lock(struct super_block *sb)
31232 +{
31233 +       __si_write_lock(sb);
31234 +       si_pid_set(sb);
31235 +}
31236 +
31237 +static inline int si_noflush_write_trylock(struct super_block *sb)
31238 +{
31239 +       int locked;
31240 +
31241 +       locked = __si_write_trylock(sb);
31242 +       if (locked)
31243 +               si_pid_set(sb);
31244 +       return locked;
31245 +}
31246 +
31247 +#if 0 /* reserved */
31248 +static inline int si_read_trylock(struct super_block *sb, int flags)
31249 +{
31250 +       if (au_ftest_lock(flags, FLUSH))
31251 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31252 +       return si_noflush_read_trylock(sb);
31253 +}
31254 +#endif
31255 +
31256 +static inline void si_read_unlock(struct super_block *sb)
31257 +{
31258 +       si_pid_clr(sb);
31259 +       __si_read_unlock(sb);
31260 +}
31261 +
31262 +#if 0 /* reserved */
31263 +static inline int si_write_trylock(struct super_block *sb, int flags)
31264 +{
31265 +       if (au_ftest_lock(flags, FLUSH))
31266 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31267 +       return si_noflush_write_trylock(sb);
31268 +}
31269 +#endif
31270 +
31271 +static inline void si_write_unlock(struct super_block *sb)
31272 +{
31273 +       si_pid_clr(sb);
31274 +       __si_write_unlock(sb);
31275 +}
31276 +
31277 +#if 0 /* reserved */
31278 +static inline void si_downgrade_lock(struct super_block *sb)
31279 +{
31280 +       __si_downgrade_lock(sb);
31281 +}
31282 +#endif
31283 +
31284 +/* ---------------------------------------------------------------------- */
31285 +
31286 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31287 +{
31288 +       SiMustAnyLock(sb);
31289 +       return au_sbi(sb)->si_bbot;
31290 +}
31291 +
31292 +static inline unsigned int au_mntflags(struct super_block *sb)
31293 +{
31294 +       SiMustAnyLock(sb);
31295 +       return au_sbi(sb)->si_mntflags;
31296 +}
31297 +
31298 +static inline unsigned int au_sigen(struct super_block *sb)
31299 +{
31300 +       SiMustAnyLock(sb);
31301 +       return au_sbi(sb)->si_generation;
31302 +}
31303 +
31304 +static inline struct au_branch *au_sbr(struct super_block *sb,
31305 +                                      aufs_bindex_t bindex)
31306 +{
31307 +       SiMustAnyLock(sb);
31308 +       return au_sbi(sb)->si_branch[0 + bindex];
31309 +}
31310 +
31311 +static inline loff_t au_xi_maxent(struct super_block *sb)
31312 +{
31313 +       SiMustAnyLock(sb);
31314 +       return au_sbi(sb)->si_ximaxent;
31315 +}
31316 +
31317 +#endif /* __KERNEL__ */
31318 +#endif /* __AUFS_SUPER_H__ */
31319 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31320 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31321 +++ linux/fs/aufs/sysaufs.c     2023-10-10 22:51:18.033248030 +0200
31322 @@ -0,0 +1,94 @@
31323 +// SPDX-License-Identifier: GPL-2.0
31324 +/*
31325 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31326 + *
31327 + * This program is free software; you can redistribute it and/or modify
31328 + * it under the terms of the GNU General Public License as published by
31329 + * the Free Software Foundation; either version 2 of the License, or
31330 + * (at your option) any later version.
31331 + *
31332 + * This program is distributed in the hope that it will be useful,
31333 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31334 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31335 + * GNU General Public License for more details.
31336 + *
31337 + * You should have received a copy of the GNU General Public License
31338 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31339 + */
31340 +
31341 +/*
31342 + * sysfs interface and lifetime management
31343 + * they are necessary regardless sysfs is disabled.
31344 + */
31345 +
31346 +#include <linux/random.h>
31347 +#include "aufs.h"
31348 +
31349 +unsigned long sysaufs_si_mask;
31350 +struct kset *sysaufs_kset;
31351 +
31352 +#define AuSiAttr(_name) { \
31353 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31354 +       .show   = sysaufs_si_##_name,                           \
31355 +}
31356 +
31357 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31358 +struct attribute *sysaufs_si_attrs[] = {
31359 +       &sysaufs_si_attr_xi_path.attr,
31360 +       NULL,
31361 +};
31362 +ATTRIBUTE_GROUPS(sysaufs_si);
31363 +
31364 +static const struct sysfs_ops au_sbi_ops = {
31365 +       .show   = sysaufs_si_show
31366 +};
31367 +
31368 +static const struct kobj_type au_sbi_ktype = {
31369 +       .release        = au_si_free,
31370 +       .sysfs_ops      = &au_sbi_ops,
31371 +       .default_groups = sysaufs_si_groups
31372 +};
31373 +
31374 +/* ---------------------------------------------------------------------- */
31375 +
31376 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31377 +{
31378 +       int err;
31379 +
31380 +       sbinfo->si_kobj.kset = sysaufs_kset;
31381 +       /* cf. sysaufs_name() */
31382 +       err = kobject_init_and_add
31383 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31384 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31385 +
31386 +       return err;
31387 +}
31388 +
31389 +void sysaufs_fin(void)
31390 +{
31391 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31392 +       kset_unregister(sysaufs_kset);
31393 +}
31394 +
31395 +int __init sysaufs_init(void)
31396 +{
31397 +       int err;
31398 +
31399 +       do {
31400 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31401 +       } while (!sysaufs_si_mask);
31402 +
31403 +       err = -EINVAL;
31404 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31405 +       if (unlikely(!sysaufs_kset))
31406 +               goto out;
31407 +       err = PTR_ERR(sysaufs_kset);
31408 +       if (IS_ERR(sysaufs_kset))
31409 +               goto out;
31410 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31411 +       if (unlikely(err))
31412 +               kset_unregister(sysaufs_kset);
31413 +
31414 +out:
31415 +       return err;
31416 +}
31417 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31418 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31419 +++ linux/fs/aufs/sysaufs.h     2022-11-05 23:02:18.969222617 +0100
31420 @@ -0,0 +1,102 @@
31421 +/* SPDX-License-Identifier: GPL-2.0 */
31422 +/*
31423 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31424 + *
31425 + * This program is free software; you can redistribute it and/or modify
31426 + * it under the terms of the GNU General Public License as published by
31427 + * the Free Software Foundation; either version 2 of the License, or
31428 + * (at your option) any later version.
31429 + *
31430 + * This program is distributed in the hope that it will be useful,
31431 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31432 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31433 + * GNU General Public License for more details.
31434 + *
31435 + * You should have received a copy of the GNU General Public License
31436 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31437 + */
31438 +
31439 +/*
31440 + * sysfs interface and mount lifetime management
31441 + */
31442 +
31443 +#ifndef __SYSAUFS_H__
31444 +#define __SYSAUFS_H__
31445 +
31446 +#ifdef __KERNEL__
31447 +
31448 +#include <linux/sysfs.h>
31449 +#include "module.h"
31450 +
31451 +struct super_block;
31452 +struct au_sbinfo;
31453 +
31454 +struct sysaufs_si_attr {
31455 +       struct attribute attr;
31456 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31457 +};
31458 +
31459 +/* ---------------------------------------------------------------------- */
31460 +
31461 +/* sysaufs.c */
31462 +extern unsigned long sysaufs_si_mask;
31463 +extern struct kset *sysaufs_kset;
31464 +extern struct attribute *sysaufs_si_attrs[];
31465 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31466 +int __init sysaufs_init(void);
31467 +void sysaufs_fin(void);
31468 +
31469 +/* ---------------------------------------------------------------------- */
31470 +
31471 +/* some people doesn't like to show a pointer in kernel */
31472 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31473 +{
31474 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31475 +}
31476 +
31477 +#define SysaufsSiNamePrefix    "si_"
31478 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31479 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31480 +{
31481 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31482 +                sysaufs_si_id(sbinfo));
31483 +}
31484 +
31485 +struct au_branch;
31486 +#ifdef CONFIG_SYSFS
31487 +/* sysfs.c */
31488 +extern struct attribute_group *sysaufs_attr_group;
31489 +
31490 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31491 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31492 +                        char *buf);
31493 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31494 +#ifdef CONFIG_COMPAT
31495 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31496 +#endif
31497 +
31498 +void sysaufs_br_init(struct au_branch *br);
31499 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31500 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31501 +
31502 +#define sysaufs_brs_init()     do {} while (0)
31503 +
31504 +#else
31505 +#define sysaufs_attr_group     NULL
31506 +
31507 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31508 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31509 +       struct attribute *attr, char *buf)
31510 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31511 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31512 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31513 +
31514 +static inline void sysaufs_brs_init(void)
31515 +{
31516 +       sysaufs_brs = 0;
31517 +}
31518 +
31519 +#endif /* CONFIG_SYSFS */
31520 +
31521 +#endif /* __KERNEL__ */
31522 +#endif /* __SYSAUFS_H__ */
31523 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31524 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31525 +++ linux/fs/aufs/sysfs.c       2022-11-05 23:02:18.969222617 +0100
31526 @@ -0,0 +1,374 @@
31527 +// SPDX-License-Identifier: GPL-2.0
31528 +/*
31529 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31530 + *
31531 + * This program is free software; you can redistribute it and/or modify
31532 + * it under the terms of the GNU General Public License as published by
31533 + * the Free Software Foundation; either version 2 of the License, or
31534 + * (at your option) any later version.
31535 + *
31536 + * This program is distributed in the hope that it will be useful,
31537 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31538 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31539 + * GNU General Public License for more details.
31540 + *
31541 + * You should have received a copy of the GNU General Public License
31542 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31543 + */
31544 +
31545 +/*
31546 + * sysfs interface
31547 + */
31548 +
31549 +#include <linux/compat.h>
31550 +#include <linux/seq_file.h>
31551 +#include "aufs.h"
31552 +
31553 +#ifdef CONFIG_AUFS_FS_MODULE
31554 +/* this entry violates the "one line per file" policy of sysfs */
31555 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31556 +                          char *buf)
31557 +{
31558 +       ssize_t err;
31559 +       static char *conf =
31560 +/* this file is generated at compiling */
31561 +#include "conf.str"
31562 +               ;
31563 +
31564 +       err = snprintf(buf, PAGE_SIZE, conf);
31565 +       if (unlikely(err >= PAGE_SIZE))
31566 +               err = -EFBIG;
31567 +       return err;
31568 +}
31569 +
31570 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31571 +#endif
31572 +
31573 +static struct attribute *au_attr[] = {
31574 +#ifdef CONFIG_AUFS_FS_MODULE
31575 +       &au_config_attr.attr,
31576 +#endif
31577 +       NULL,   /* need to NULL terminate the list of attributes */
31578 +};
31579 +
31580 +static struct attribute_group sysaufs_attr_group_body = {
31581 +       .attrs = au_attr
31582 +};
31583 +
31584 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31585 +
31586 +/* ---------------------------------------------------------------------- */
31587 +
31588 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31589 +{
31590 +       int err;
31591 +
31592 +       SiMustAnyLock(sb);
31593 +
31594 +       err = 0;
31595 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31596 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31597 +               seq_putc(seq, '\n');
31598 +       }
31599 +       return err;
31600 +}
31601 +
31602 +/*
31603 + * the lifetime of branch is independent from the entry under sysfs.
31604 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31605 + * unlinked.
31606 + */
31607 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31608 +                        aufs_bindex_t bindex, int idx)
31609 +{
31610 +       int err;
31611 +       struct path path;
31612 +       struct dentry *root;
31613 +       struct au_branch *br;
31614 +       au_br_perm_str_t perm;
31615 +
31616 +       AuDbg("b%d\n", bindex);
31617 +
31618 +       err = 0;
31619 +       root = sb->s_root;
31620 +       di_read_lock_parent(root, !AuLock_IR);
31621 +       br = au_sbr(sb, bindex);
31622 +
31623 +       switch (idx) {
31624 +       case AuBrSysfs_BR:
31625 +               path.mnt = au_br_mnt(br);
31626 +               path.dentry = au_h_dptr(root, bindex);
31627 +               err = au_seq_path(seq, &path);
31628 +               if (!err) {
31629 +                       au_optstr_br_perm(&perm, br->br_perm);
31630 +                       seq_printf(seq, "=%s\n", perm.a);
31631 +               }
31632 +               break;
31633 +       case AuBrSysfs_BRID:
31634 +               seq_printf(seq, "%d\n", br->br_id);
31635 +               break;
31636 +       }
31637 +       di_read_unlock(root, !AuLock_IR);
31638 +       if (unlikely(err || seq_has_overflowed(seq)))
31639 +               err = -E2BIG;
31640 +
31641 +       return err;
31642 +}
31643 +
31644 +/* ---------------------------------------------------------------------- */
31645 +
31646 +static struct seq_file *au_seq(char *p, ssize_t len)
31647 +{
31648 +       struct seq_file *seq;
31649 +
31650 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31651 +       if (seq) {
31652 +               /* mutex_init(&seq.lock); */
31653 +               seq->buf = p;
31654 +               seq->size = len;
31655 +               return seq; /* success */
31656 +       }
31657 +
31658 +       seq = ERR_PTR(-ENOMEM);
31659 +       return seq;
31660 +}
31661 +
31662 +#define SysaufsBr_PREFIX       "br"
31663 +#define SysaufsBrid_PREFIX     "brid"
31664 +
31665 +/* todo: file size may exceed PAGE_SIZE */
31666 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31667 +                       char *buf)
31668 +{
31669 +       ssize_t err;
31670 +       int idx;
31671 +       long l;
31672 +       aufs_bindex_t bbot;
31673 +       struct au_sbinfo *sbinfo;
31674 +       struct super_block *sb;
31675 +       struct seq_file *seq;
31676 +       char *name;
31677 +       struct attribute **cattr;
31678 +
31679 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31680 +       sb = sbinfo->si_sb;
31681 +
31682 +       /*
31683 +        * prevent a race condition between sysfs and aufs.
31684 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31685 +        * prohibits maintaining the sysfs entries.
31686 +        * hew we acquire read lock after sysfs_get_active_two().
31687 +        * on the other hand, the remount process may maintain the sysfs/aufs
31688 +        * entries after acquiring write lock.
31689 +        * it can cause a deadlock.
31690 +        * simply we gave up processing read here.
31691 +        */
31692 +       err = -EBUSY;
31693 +       if (unlikely(!si_noflush_read_trylock(sb)))
31694 +               goto out;
31695 +
31696 +       seq = au_seq(buf, PAGE_SIZE);
31697 +       err = PTR_ERR(seq);
31698 +       if (IS_ERR(seq))
31699 +               goto out_unlock;
31700 +
31701 +       name = (void *)attr->name;
31702 +       cattr = sysaufs_si_attrs;
31703 +       while (*cattr) {
31704 +               if (!strcmp(name, (*cattr)->name)) {
31705 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31706 +                               ->show(seq, sb);
31707 +                       goto out_seq;
31708 +               }
31709 +               cattr++;
31710 +       }
31711 +
31712 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31713 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31714 +               idx = AuBrSysfs_BRID;
31715 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31716 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31717 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31718 +               idx = AuBrSysfs_BR;
31719 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31720 +       } else
31721 +                 BUG();
31722 +
31723 +       err = kstrtol(name, 10, &l);
31724 +       if (!err) {
31725 +               bbot = au_sbbot(sb);
31726 +               if (l <= bbot)
31727 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31728 +               else
31729 +                       err = -ENOENT;
31730 +       }
31731 +
31732 +out_seq:
31733 +       if (!err) {
31734 +               err = seq->count;
31735 +               /* sysfs limit */
31736 +               if (unlikely(err == PAGE_SIZE))
31737 +                       err = -EFBIG;
31738 +       }
31739 +       au_kfree_rcu(seq);
31740 +out_unlock:
31741 +       si_read_unlock(sb);
31742 +out:
31743 +       return err;
31744 +}
31745 +
31746 +/* ---------------------------------------------------------------------- */
31747 +
31748 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31749 +{
31750 +       int err;
31751 +       int16_t brid;
31752 +       aufs_bindex_t bindex, bbot;
31753 +       size_t sz;
31754 +       char *buf;
31755 +       struct seq_file *seq;
31756 +       struct au_branch *br;
31757 +
31758 +       si_read_lock(sb, AuLock_FLUSH);
31759 +       bbot = au_sbbot(sb);
31760 +       err = bbot + 1;
31761 +       if (!arg)
31762 +               goto out;
31763 +
31764 +       err = -ENOMEM;
31765 +       buf = (void *)__get_free_page(GFP_NOFS);
31766 +       if (unlikely(!buf))
31767 +               goto out;
31768 +
31769 +       seq = au_seq(buf, PAGE_SIZE);
31770 +       err = PTR_ERR(seq);
31771 +       if (IS_ERR(seq))
31772 +               goto out_buf;
31773 +
31774 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31775 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31776 +               /* VERIFY_WRITE */
31777 +               err = !access_ok(arg, sizeof(*arg));
31778 +               if (unlikely(err))
31779 +                       break;
31780 +
31781 +               br = au_sbr(sb, bindex);
31782 +               brid = br->br_id;
31783 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31784 +               err = __put_user(brid, &arg->id);
31785 +               if (unlikely(err))
31786 +                       break;
31787 +
31788 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31789 +               err = __put_user(br->br_perm, &arg->perm);
31790 +               if (unlikely(err))
31791 +                       break;
31792 +
31793 +               err = au_seq_path(seq, &br->br_path);
31794 +               if (unlikely(err))
31795 +                       break;
31796 +               seq_putc(seq, '\0');
31797 +               if (!seq_has_overflowed(seq)) {
31798 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31799 +                       seq->count = 0;
31800 +                       if (unlikely(err))
31801 +                               break;
31802 +               } else {
31803 +                       err = -E2BIG;
31804 +                       goto out_seq;
31805 +               }
31806 +       }
31807 +       if (unlikely(err))
31808 +               err = -EFAULT;
31809 +
31810 +out_seq:
31811 +       au_kfree_rcu(seq);
31812 +out_buf:
31813 +       free_page((unsigned long)buf);
31814 +out:
31815 +       si_read_unlock(sb);
31816 +       return err;
31817 +}
31818 +
31819 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31820 +{
31821 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31822 +}
31823 +
31824 +#ifdef CONFIG_COMPAT
31825 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31826 +{
31827 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31828 +}
31829 +#endif
31830 +
31831 +/* ---------------------------------------------------------------------- */
31832 +
31833 +void sysaufs_br_init(struct au_branch *br)
31834 +{
31835 +       int i;
31836 +       struct au_brsysfs *br_sysfs;
31837 +       struct attribute *attr;
31838 +
31839 +       br_sysfs = br->br_sysfs;
31840 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31841 +               attr = &br_sysfs->attr;
31842 +               sysfs_attr_init(attr);
31843 +               attr->name = br_sysfs->name;
31844 +               attr->mode = 0444;
31845 +               br_sysfs++;
31846 +       }
31847 +}
31848 +
31849 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31850 +{
31851 +       struct au_branch *br;
31852 +       struct kobject *kobj;
31853 +       struct au_brsysfs *br_sysfs;
31854 +       int i;
31855 +       aufs_bindex_t bbot;
31856 +
31857 +       if (!sysaufs_brs)
31858 +               return;
31859 +
31860 +       kobj = &au_sbi(sb)->si_kobj;
31861 +       bbot = au_sbbot(sb);
31862 +       for (; bindex <= bbot; bindex++) {
31863 +               br = au_sbr(sb, bindex);
31864 +               br_sysfs = br->br_sysfs;
31865 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31866 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31867 +                       br_sysfs++;
31868 +               }
31869 +       }
31870 +}
31871 +
31872 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31873 +{
31874 +       int err, i;
31875 +       aufs_bindex_t bbot;
31876 +       struct kobject *kobj;
31877 +       struct au_branch *br;
31878 +       struct au_brsysfs *br_sysfs;
31879 +
31880 +       if (!sysaufs_brs)
31881 +               return;
31882 +
31883 +       kobj = &au_sbi(sb)->si_kobj;
31884 +       bbot = au_sbbot(sb);
31885 +       for (; bindex <= bbot; bindex++) {
31886 +               br = au_sbr(sb, bindex);
31887 +               br_sysfs = br->br_sysfs;
31888 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31889 +                        SysaufsBr_PREFIX "%d", bindex);
31890 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31891 +                        SysaufsBrid_PREFIX "%d", bindex);
31892 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31893 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31894 +                       if (unlikely(err))
31895 +                               pr_warn("failed %s under sysfs(%d)\n",
31896 +                                       br_sysfs->name, err);
31897 +                       br_sysfs++;
31898 +               }
31899 +       }
31900 +}
31901 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31902 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31903 +++ linux/fs/aufs/sysrq.c       2023-10-31 09:31:04.199880750 +0100
31904 @@ -0,0 +1,149 @@
31905 +// SPDX-License-Identifier: GPL-2.0
31906 +/*
31907 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31908 + *
31909 + * This program is free software; you can redistribute it and/or modify
31910 + * it under the terms of the GNU General Public License as published by
31911 + * the Free Software Foundation; either version 2 of the License, or
31912 + * (at your option) any later version.
31913 + *
31914 + * This program is distributed in the hope that it will be useful,
31915 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31916 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31917 + * GNU General Public License for more details.
31918 + *
31919 + * You should have received a copy of the GNU General Public License
31920 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31921 + */
31922 +
31923 +/*
31924 + * magic sysrq handler
31925 + */
31926 +
31927 +/* #include <linux/sysrq.h> */
31928 +#include <linux/writeback.h>
31929 +#include "aufs.h"
31930 +
31931 +/* ---------------------------------------------------------------------- */
31932 +
31933 +static void sysrq_sb(struct super_block *sb)
31934 +{
31935 +       char *plevel;
31936 +       struct au_sbinfo *sbinfo;
31937 +       struct file *file;
31938 +       struct hlist_bl_head *files;
31939 +       struct hlist_bl_node *pos;
31940 +       struct au_finfo *finfo;
31941 +       struct inode *i;
31942 +
31943 +       plevel = au_plevel;
31944 +       au_plevel = KERN_WARNING;
31945 +
31946 +       /* since we define pr_fmt, call printk directly */
31947 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31948 +
31949 +       sbinfo = au_sbi(sb);
31950 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31951 +       pr("superblock\n");
31952 +       au_dpri_sb(sb);
31953 +
31954 +#if 0 /* reserved */
31955 +       do {
31956 +               int err, i, j, ndentry;
31957 +               struct au_dcsub_pages dpages;
31958 +               struct au_dpage *dpage;
31959 +
31960 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31961 +               if (unlikely(err))
31962 +                       break;
31963 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31964 +               if (!err)
31965 +                       for (i = 0; i < dpages.ndpage; i++) {
31966 +                               dpage = dpages.dpages + i;
31967 +                               ndentry = dpage->ndentry;
31968 +                               for (j = 0; j < ndentry; j++)
31969 +                                       au_dpri_dentry(dpage->dentries[j]);
31970 +                       }
31971 +               au_dpages_free(&dpages);
31972 +       } while (0);
31973 +#endif
31974 +
31975 +       pr("isolated inode\n");
31976 +       spin_lock(&sb->s_inode_list_lock);
31977 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31978 +               spin_lock(&i->i_lock);
31979 +               if (hlist_empty(&i->i_dentry))
31980 +                       au_dpri_inode(i);
31981 +               spin_unlock(&i->i_lock);
31982 +       }
31983 +       spin_unlock(&sb->s_inode_list_lock);
31984 +
31985 +       pr("files\n");
31986 +       files = &au_sbi(sb)->si_files;
31987 +       hlist_bl_lock(files);
31988 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31989 +               umode_t mode;
31990 +
31991 +               file = finfo->fi_file;
31992 +               mode = file_inode(file)->i_mode;
31993 +               if (!special_file(mode))
31994 +                       au_dpri_file(file);
31995 +       }
31996 +       hlist_bl_unlock(files);
31997 +       pr("done\n");
31998 +
31999 +#undef pr
32000 +       au_plevel = plevel;
32001 +}
32002 +
32003 +/* ---------------------------------------------------------------------- */
32004 +
32005 +/* module parameter */
32006 +static char *aufs_sysrq_key = "a";
32007 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
32008 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
32009 +
32010 +static void au_sysrq(u8 key __maybe_unused)
32011 +{
32012 +       struct au_sbinfo *sbinfo;
32013 +       struct hlist_bl_node *pos;
32014 +
32015 +       lockdep_off();
32016 +       au_sbilist_lock();
32017 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
32018 +               sysrq_sb(sbinfo->si_sb);
32019 +       au_sbilist_unlock();
32020 +       lockdep_on();
32021 +}
32022 +
32023 +static struct sysrq_key_op au_sysrq_op = {
32024 +       .handler        = au_sysrq,
32025 +       .help_msg       = "Aufs",
32026 +       .action_msg     = "Aufs",
32027 +       .enable_mask    = SYSRQ_ENABLE_DUMP
32028 +};
32029 +
32030 +/* ---------------------------------------------------------------------- */
32031 +
32032 +int __init au_sysrq_init(void)
32033 +{
32034 +       int err;
32035 +       char key;
32036 +
32037 +       err = -1;
32038 +       key = *aufs_sysrq_key;
32039 +       if ('a' <= key && key <= 'z')
32040 +               err = register_sysrq_key(key, &au_sysrq_op);
32041 +       if (unlikely(err))
32042 +               pr_err("err %d, sysrq=%c\n", err, key);
32043 +       return err;
32044 +}
32045 +
32046 +void au_sysrq_fin(void)
32047 +{
32048 +       int err;
32049 +
32050 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
32051 +       if (unlikely(err))
32052 +               pr_err("err %d (ignored)\n", err);
32053 +}
32054 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
32055 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
32056 +++ linux/fs/aufs/vdir.c        2022-12-17 09:21:34.799855195 +0100
32057 @@ -0,0 +1,896 @@
32058 +// SPDX-License-Identifier: GPL-2.0
32059 +/*
32060 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32061 + *
32062 + * This program is free software; you can redistribute it and/or modify
32063 + * it under the terms of the GNU General Public License as published by
32064 + * the Free Software Foundation; either version 2 of the License, or
32065 + * (at your option) any later version.
32066 + *
32067 + * This program is distributed in the hope that it will be useful,
32068 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32069 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32070 + * GNU General Public License for more details.
32071 + *
32072 + * You should have received a copy of the GNU General Public License
32073 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32074 + */
32075 +
32076 +/*
32077 + * virtual or vertical directory
32078 + */
32079 +
32080 +#include <linux/iversion.h>
32081 +#include "aufs.h"
32082 +
32083 +static unsigned int calc_size(int nlen)
32084 +{
32085 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
32086 +}
32087 +
32088 +static int set_deblk_end(union au_vdir_deblk_p *p,
32089 +                        union au_vdir_deblk_p *deblk_end)
32090 +{
32091 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32092 +               p->de->de_str.len = 0;
32093 +               /* smp_mb(); */
32094 +               return 0;
32095 +       }
32096 +       return -1; /* error */
32097 +}
32098 +
32099 +/* returns true or false */
32100 +static int is_deblk_end(union au_vdir_deblk_p *p,
32101 +                       union au_vdir_deblk_p *deblk_end)
32102 +{
32103 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
32104 +               return !p->de->de_str.len;
32105 +       return 1;
32106 +}
32107 +
32108 +static unsigned char *last_deblk(struct au_vdir *vdir)
32109 +{
32110 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
32111 +}
32112 +
32113 +/* ---------------------------------------------------------------------- */
32114 +
32115 +/* estimate the appropriate size for name hash table */
32116 +unsigned int au_rdhash_est(loff_t sz)
32117 +{
32118 +       unsigned int n;
32119 +
32120 +       n = UINT_MAX;
32121 +       sz >>= 10;
32122 +       if (sz < n)
32123 +               n = sz;
32124 +       if (sz < AUFS_RDHASH_DEF)
32125 +               n = AUFS_RDHASH_DEF;
32126 +       /* pr_info("n %u\n", n); */
32127 +       return n;
32128 +}
32129 +
32130 +/*
32131 + * the allocated memory has to be freed by
32132 + * au_nhash_wh_free() or au_nhash_de_free().
32133 + */
32134 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32135 +{
32136 +       struct hlist_head *head;
32137 +       unsigned int u;
32138 +       size_t sz;
32139 +
32140 +       sz = sizeof(*nhash->nh_head) * num_hash;
32141 +       head = kmalloc(sz, gfp);
32142 +       if (head) {
32143 +               nhash->nh_num = num_hash;
32144 +               nhash->nh_head = head;
32145 +               for (u = 0; u < num_hash; u++)
32146 +                       INIT_HLIST_HEAD(head++);
32147 +               return 0; /* success */
32148 +       }
32149 +
32150 +       return -ENOMEM;
32151 +}
32152 +
32153 +static void nhash_count(struct hlist_head *head)
32154 +{
32155 +#if 0 /* debugging */
32156 +       unsigned long n;
32157 +       struct hlist_node *pos;
32158 +
32159 +       n = 0;
32160 +       hlist_for_each(pos, head)
32161 +               n++;
32162 +       pr_info("%lu\n", n);
32163 +#endif
32164 +}
32165 +
32166 +static void au_nhash_wh_do_free(struct hlist_head *head)
32167 +{
32168 +       struct au_vdir_wh *pos;
32169 +       struct hlist_node *node;
32170 +
32171 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32172 +               au_kfree_rcu(pos);
32173 +}
32174 +
32175 +static void au_nhash_de_do_free(struct hlist_head *head)
32176 +{
32177 +       struct au_vdir_dehstr *pos;
32178 +       struct hlist_node *node;
32179 +
32180 +       hlist_for_each_entry_safe(pos, node, head, hash)
32181 +               au_cache_free_vdir_dehstr(pos);
32182 +}
32183 +
32184 +static void au_nhash_do_free(struct au_nhash *nhash,
32185 +                            void (*free)(struct hlist_head *head))
32186 +{
32187 +       unsigned int n;
32188 +       struct hlist_head *head;
32189 +
32190 +       n = nhash->nh_num;
32191 +       if (!n)
32192 +               return;
32193 +
32194 +       head = nhash->nh_head;
32195 +       while (n-- > 0) {
32196 +               nhash_count(head);
32197 +               free(head++);
32198 +       }
32199 +       au_kfree_try_rcu(nhash->nh_head);
32200 +}
32201 +
32202 +void au_nhash_wh_free(struct au_nhash *whlist)
32203 +{
32204 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32205 +}
32206 +
32207 +static void au_nhash_de_free(struct au_nhash *delist)
32208 +{
32209 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32210 +}
32211 +
32212 +/* ---------------------------------------------------------------------- */
32213 +
32214 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32215 +                           int limit)
32216 +{
32217 +       int num;
32218 +       unsigned int u, n;
32219 +       struct hlist_head *head;
32220 +       struct au_vdir_wh *pos;
32221 +
32222 +       num = 0;
32223 +       n = whlist->nh_num;
32224 +       head = whlist->nh_head;
32225 +       for (u = 0; u < n; u++, head++)
32226 +               hlist_for_each_entry(pos, head, wh_hash)
32227 +                       if (pos->wh_bindex == btgt && ++num > limit)
32228 +                               return 1;
32229 +       return 0;
32230 +}
32231 +
32232 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32233 +                                      unsigned char *name,
32234 +                                      unsigned int len)
32235 +{
32236 +       unsigned int v;
32237 +       /* const unsigned int magic_bit = 12; */
32238 +
32239 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32240 +
32241 +       v = 0;
32242 +       if (len > 8)
32243 +               len = 8;
32244 +       while (len--)
32245 +               v += *name++;
32246 +       /* v = hash_long(v, magic_bit); */
32247 +       v %= nhash->nh_num;
32248 +       return nhash->nh_head + v;
32249 +}
32250 +
32251 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32252 +                             int nlen)
32253 +{
32254 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32255 +}
32256 +
32257 +/* returns found or not */
32258 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32259 +{
32260 +       struct hlist_head *head;
32261 +       struct au_vdir_wh *pos;
32262 +       struct au_vdir_destr *str;
32263 +
32264 +       head = au_name_hash(whlist, name, nlen);
32265 +       hlist_for_each_entry(pos, head, wh_hash) {
32266 +               str = &pos->wh_str;
32267 +               AuDbg("%.*s\n", str->len, str->name);
32268 +               if (au_nhash_test_name(str, name, nlen))
32269 +                       return 1;
32270 +       }
32271 +       return 0;
32272 +}
32273 +
32274 +/* returns found(true) or not */
32275 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32276 +{
32277 +       struct hlist_head *head;
32278 +       struct au_vdir_dehstr *pos;
32279 +       struct au_vdir_destr *str;
32280 +
32281 +       head = au_name_hash(delist, name, nlen);
32282 +       hlist_for_each_entry(pos, head, hash) {
32283 +               str = pos->str;
32284 +               AuDbg("%.*s\n", str->len, str->name);
32285 +               if (au_nhash_test_name(str, name, nlen))
32286 +                       return 1;
32287 +       }
32288 +       return 0;
32289 +}
32290 +
32291 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32292 +                           unsigned char d_type)
32293 +{
32294 +#ifdef CONFIG_AUFS_SHWH
32295 +       wh->wh_ino = ino;
32296 +       wh->wh_type = d_type;
32297 +#endif
32298 +}
32299 +
32300 +/* ---------------------------------------------------------------------- */
32301 +
32302 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32303 +                      unsigned int d_type, aufs_bindex_t bindex,
32304 +                      unsigned char shwh)
32305 +{
32306 +       int err;
32307 +       struct au_vdir_destr *str;
32308 +       struct au_vdir_wh *wh;
32309 +
32310 +       AuDbg("%.*s\n", nlen, name);
32311 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32312 +
32313 +       err = -ENOMEM;
32314 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32315 +       if (unlikely(!wh))
32316 +               goto out;
32317 +
32318 +       err = 0;
32319 +       wh->wh_bindex = bindex;
32320 +       if (shwh)
32321 +               au_shwh_init_wh(wh, ino, d_type);
32322 +       str = &wh->wh_str;
32323 +       str->len = nlen;
32324 +       memcpy(str->name, name, nlen);
32325 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32326 +       /* smp_mb(); */
32327 +
32328 +out:
32329 +       return err;
32330 +}
32331 +
32332 +static int append_deblk(struct au_vdir *vdir)
32333 +{
32334 +       int err;
32335 +       unsigned long ul;
32336 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32337 +       union au_vdir_deblk_p p, deblk_end;
32338 +       unsigned char **o;
32339 +
32340 +       err = -ENOMEM;
32341 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32342 +                       GFP_NOFS, /*may_shrink*/0);
32343 +       if (unlikely(!o))
32344 +               goto out;
32345 +
32346 +       vdir->vd_deblk = o;
32347 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32348 +       if (p.deblk) {
32349 +               ul = vdir->vd_nblk++;
32350 +               vdir->vd_deblk[ul] = p.deblk;
32351 +               vdir->vd_last.ul = ul;
32352 +               vdir->vd_last.p.deblk = p.deblk;
32353 +               deblk_end.deblk = p.deblk + deblk_sz;
32354 +               err = set_deblk_end(&p, &deblk_end);
32355 +       }
32356 +
32357 +out:
32358 +       return err;
32359 +}
32360 +
32361 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32362 +                    unsigned int d_type, struct au_nhash *delist)
32363 +{
32364 +       int err;
32365 +       unsigned int sz;
32366 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32367 +       union au_vdir_deblk_p p, *room, deblk_end;
32368 +       struct au_vdir_dehstr *dehstr;
32369 +
32370 +       p.deblk = last_deblk(vdir);
32371 +       deblk_end.deblk = p.deblk + deblk_sz;
32372 +       room = &vdir->vd_last.p;
32373 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32374 +                 || !is_deblk_end(room, &deblk_end));
32375 +
32376 +       sz = calc_size(nlen);
32377 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32378 +               err = append_deblk(vdir);
32379 +               if (unlikely(err))
32380 +                       goto out;
32381 +
32382 +               p.deblk = last_deblk(vdir);
32383 +               deblk_end.deblk = p.deblk + deblk_sz;
32384 +               /* smp_mb(); */
32385 +               AuDebugOn(room->deblk != p.deblk);
32386 +       }
32387 +
32388 +       err = -ENOMEM;
32389 +       dehstr = au_cache_alloc_vdir_dehstr();
32390 +       if (unlikely(!dehstr))
32391 +               goto out;
32392 +
32393 +       dehstr->str = &room->de->de_str;
32394 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32395 +       room->de->de_ino = ino;
32396 +       room->de->de_type = d_type;
32397 +       room->de->de_str.len = nlen;
32398 +       memcpy(room->de->de_str.name, name, nlen);
32399 +
32400 +       err = 0;
32401 +       room->deblk += sz;
32402 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32403 +               err = append_deblk(vdir);
32404 +       /* smp_mb(); */
32405 +
32406 +out:
32407 +       return err;
32408 +}
32409 +
32410 +/* ---------------------------------------------------------------------- */
32411 +
32412 +void au_vdir_free(struct au_vdir *vdir)
32413 +{
32414 +       unsigned char **deblk;
32415 +
32416 +       deblk = vdir->vd_deblk;
32417 +       while (vdir->vd_nblk--)
32418 +               au_kfree_try_rcu(*deblk++);
32419 +       au_kfree_try_rcu(vdir->vd_deblk);
32420 +       au_cache_free_vdir(vdir);
32421 +}
32422 +
32423 +static struct au_vdir *alloc_vdir(struct file *file)
32424 +{
32425 +       struct au_vdir *vdir;
32426 +       struct super_block *sb;
32427 +       int err;
32428 +
32429 +       sb = file->f_path.dentry->d_sb;
32430 +       SiMustAnyLock(sb);
32431 +
32432 +       err = -ENOMEM;
32433 +       vdir = au_cache_alloc_vdir();
32434 +       if (unlikely(!vdir))
32435 +               goto out;
32436 +
32437 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32438 +       if (unlikely(!vdir->vd_deblk))
32439 +               goto out_free;
32440 +
32441 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32442 +       if (!vdir->vd_deblk_sz) {
32443 +               /* estimate the appropriate size for deblk */
32444 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32445 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32446 +       }
32447 +       vdir->vd_nblk = 0;
32448 +       vdir->vd_version = 0;
32449 +       vdir->vd_jiffy = 0;
32450 +       err = append_deblk(vdir);
32451 +       if (!err)
32452 +               return vdir; /* success */
32453 +
32454 +       au_kfree_try_rcu(vdir->vd_deblk);
32455 +
32456 +out_free:
32457 +       au_cache_free_vdir(vdir);
32458 +out:
32459 +       vdir = ERR_PTR(err);
32460 +       return vdir;
32461 +}
32462 +
32463 +static int reinit_vdir(struct au_vdir *vdir)
32464 +{
32465 +       int err;
32466 +       union au_vdir_deblk_p p, deblk_end;
32467 +
32468 +       while (vdir->vd_nblk > 1) {
32469 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32470 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32471 +               vdir->vd_nblk--;
32472 +       }
32473 +       p.deblk = vdir->vd_deblk[0];
32474 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32475 +       err = set_deblk_end(&p, &deblk_end);
32476 +       /* keep vd_dblk_sz */
32477 +       vdir->vd_last.ul = 0;
32478 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32479 +       vdir->vd_version = 0;
32480 +       vdir->vd_jiffy = 0;
32481 +       /* smp_mb(); */
32482 +       return err;
32483 +}
32484 +
32485 +/* ---------------------------------------------------------------------- */
32486 +
32487 +#define AuFillVdir_CALLED      1
32488 +#define AuFillVdir_WHABLE      (1 << 1)
32489 +#define AuFillVdir_SHWH                (1 << 2)
32490 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32491 +#define au_fset_fillvdir(flags, name) \
32492 +       do { (flags) |= AuFillVdir_##name; } while (0)
32493 +#define au_fclr_fillvdir(flags, name) \
32494 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32495 +
32496 +#ifndef CONFIG_AUFS_SHWH
32497 +#undef AuFillVdir_SHWH
32498 +#define AuFillVdir_SHWH                0
32499 +#endif
32500 +
32501 +struct fillvdir_arg {
32502 +       struct dir_context      ctx;
32503 +       struct file             *file;
32504 +       struct au_vdir          *vdir;
32505 +       struct au_nhash         delist;
32506 +       struct au_nhash         whlist;
32507 +       aufs_bindex_t           bindex;
32508 +       unsigned int            flags;
32509 +       int                     err;
32510 +};
32511 +
32512 +static bool fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32513 +                   loff_t offset __maybe_unused, u64 h_ino,
32514 +                   unsigned int d_type)
32515 +{
32516 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32517 +       char *name = (void *)__name;
32518 +       struct super_block *sb;
32519 +       ino_t ino;
32520 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32521 +
32522 +       arg->err = 0;
32523 +       sb = arg->file->f_path.dentry->d_sb;
32524 +       au_fset_fillvdir(arg->flags, CALLED);
32525 +       /* smp_mb(); */
32526 +       if (nlen <= AUFS_WH_PFX_LEN
32527 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32528 +               if (test_known(&arg->delist, name, nlen)
32529 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32530 +                       goto out; /* already exists or whiteouted */
32531 +
32532 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32533 +               if (!arg->err) {
32534 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32535 +                               d_type = DT_UNKNOWN;
32536 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32537 +                                            d_type, &arg->delist);
32538 +               }
32539 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32540 +               name += AUFS_WH_PFX_LEN;
32541 +               nlen -= AUFS_WH_PFX_LEN;
32542 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32543 +                       goto out; /* already whiteouted */
32544 +
32545 +               ino = 0; /* just to suppress a warning */
32546 +               if (shwh)
32547 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32548 +                                            &ino);
32549 +               if (!arg->err) {
32550 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32551 +                               d_type = DT_UNKNOWN;
32552 +                       arg->err = au_nhash_append_wh
32553 +                               (&arg->whlist, name, nlen, ino, d_type,
32554 +                                arg->bindex, shwh);
32555 +               }
32556 +       }
32557 +
32558 +out:
32559 +       if (!arg->err)
32560 +               arg->vdir->vd_jiffy = jiffies;
32561 +       /* smp_mb(); */
32562 +       AuTraceErr(arg->err);
32563 +       return !arg->err;
32564 +}
32565 +
32566 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32567 +                         struct au_nhash *whlist, struct au_nhash *delist)
32568 +{
32569 +#ifdef CONFIG_AUFS_SHWH
32570 +       int err;
32571 +       unsigned int nh, u;
32572 +       struct hlist_head *head;
32573 +       struct au_vdir_wh *pos;
32574 +       struct hlist_node *n;
32575 +       char *p, *o;
32576 +       struct au_vdir_destr *destr;
32577 +
32578 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32579 +
32580 +       err = -ENOMEM;
32581 +       o = p = (void *)__get_free_page(GFP_NOFS);
32582 +       if (unlikely(!p))
32583 +               goto out;
32584 +
32585 +       err = 0;
32586 +       nh = whlist->nh_num;
32587 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32588 +       p += AUFS_WH_PFX_LEN;
32589 +       for (u = 0; u < nh; u++) {
32590 +               head = whlist->nh_head + u;
32591 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32592 +                       destr = &pos->wh_str;
32593 +                       memcpy(p, destr->name, destr->len);
32594 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32595 +                                       pos->wh_ino, pos->wh_type, delist);
32596 +                       if (unlikely(err))
32597 +                               break;
32598 +               }
32599 +       }
32600 +
32601 +       free_page((unsigned long)o);
32602 +
32603 +out:
32604 +       AuTraceErr(err);
32605 +       return err;
32606 +#else
32607 +       return 0;
32608 +#endif
32609 +}
32610 +
32611 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32612 +{
32613 +       int err;
32614 +       unsigned int rdhash;
32615 +       loff_t offset;
32616 +       aufs_bindex_t bbot, bindex, btop;
32617 +       unsigned char shwh;
32618 +       struct file *hf, *file;
32619 +       struct super_block *sb;
32620 +
32621 +       file = arg->file;
32622 +       sb = file->f_path.dentry->d_sb;
32623 +       SiMustAnyLock(sb);
32624 +
32625 +       rdhash = au_sbi(sb)->si_rdhash;
32626 +       if (!rdhash)
32627 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32628 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32629 +       if (unlikely(err))
32630 +               goto out;
32631 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32632 +       if (unlikely(err))
32633 +               goto out_delist;
32634 +
32635 +       err = 0;
32636 +       arg->flags = 0;
32637 +       shwh = 0;
32638 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32639 +               shwh = 1;
32640 +               au_fset_fillvdir(arg->flags, SHWH);
32641 +       }
32642 +       btop = au_fbtop(file);
32643 +       bbot = au_fbbot_dir(file);
32644 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32645 +               hf = au_hf_dir(file, bindex);
32646 +               if (!hf)
32647 +                       continue;
32648 +
32649 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32650 +               err = offset;
32651 +               if (unlikely(offset))
32652 +                       break;
32653 +
32654 +               arg->bindex = bindex;
32655 +               au_fclr_fillvdir(arg->flags, WHABLE);
32656 +               if (shwh
32657 +                   || (bindex != bbot
32658 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32659 +                       au_fset_fillvdir(arg->flags, WHABLE);
32660 +               do {
32661 +                       arg->err = 0;
32662 +                       au_fclr_fillvdir(arg->flags, CALLED);
32663 +                       /* smp_mb(); */
32664 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32665 +                       if (err >= 0)
32666 +                               err = arg->err;
32667 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32668 +
32669 +               /*
32670 +                * dir_relax() may be good for concurrency, but aufs should not
32671 +                * use it since it will cause a lockdep problem.
32672 +                */
32673 +       }
32674 +
32675 +       if (!err && shwh)
32676 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32677 +
32678 +       au_nhash_wh_free(&arg->whlist);
32679 +
32680 +out_delist:
32681 +       au_nhash_de_free(&arg->delist);
32682 +out:
32683 +       return err;
32684 +}
32685 +
32686 +static int read_vdir(struct file *file, int may_read)
32687 +{
32688 +       int err;
32689 +       unsigned long expire;
32690 +       unsigned char do_read;
32691 +       struct fillvdir_arg arg = {
32692 +               .ctx = {
32693 +                       .actor = fillvdir
32694 +               }
32695 +       };
32696 +       struct inode *inode;
32697 +       struct au_vdir *vdir, *allocated;
32698 +
32699 +       err = 0;
32700 +       inode = file_inode(file);
32701 +       IMustLock(inode);
32702 +       IiMustWriteLock(inode);
32703 +       SiMustAnyLock(inode->i_sb);
32704 +
32705 +       allocated = NULL;
32706 +       do_read = 0;
32707 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32708 +       vdir = au_ivdir(inode);
32709 +       if (!vdir) {
32710 +               do_read = 1;
32711 +               vdir = alloc_vdir(file);
32712 +               err = PTR_ERR(vdir);
32713 +               if (IS_ERR(vdir))
32714 +                       goto out;
32715 +               err = 0;
32716 +               allocated = vdir;
32717 +       } else if (may_read
32718 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32719 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32720 +               do_read = 1;
32721 +               err = reinit_vdir(vdir);
32722 +               if (unlikely(err))
32723 +                       goto out;
32724 +       }
32725 +
32726 +       if (!do_read)
32727 +               return 0; /* success */
32728 +
32729 +       arg.file = file;
32730 +       arg.vdir = vdir;
32731 +       err = au_do_read_vdir(&arg);
32732 +       if (!err) {
32733 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32734 +               vdir->vd_version = inode_query_iversion(inode);
32735 +               vdir->vd_last.ul = 0;
32736 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32737 +               if (allocated)
32738 +                       au_set_ivdir(inode, allocated);
32739 +       } else if (allocated)
32740 +               au_vdir_free(allocated);
32741 +
32742 +out:
32743 +       return err;
32744 +}
32745 +
32746 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32747 +{
32748 +       int err, rerr;
32749 +       unsigned long ul, n;
32750 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32751 +
32752 +       AuDebugOn(tgt->vd_nblk != 1);
32753 +
32754 +       err = -ENOMEM;
32755 +       if (tgt->vd_nblk < src->vd_nblk) {
32756 +               unsigned char **p;
32757 +
32758 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32759 +                               GFP_NOFS, /*may_shrink*/0);
32760 +               if (unlikely(!p))
32761 +                       goto out;
32762 +               tgt->vd_deblk = p;
32763 +       }
32764 +
32765 +       if (tgt->vd_deblk_sz != deblk_sz) {
32766 +               unsigned char *p;
32767 +
32768 +               tgt->vd_deblk_sz = deblk_sz;
32769 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32770 +                               /*may_shrink*/1);
32771 +               if (unlikely(!p))
32772 +                       goto out;
32773 +               tgt->vd_deblk[0] = p;
32774 +       }
32775 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32776 +       tgt->vd_version = src->vd_version;
32777 +       tgt->vd_jiffy = src->vd_jiffy;
32778 +
32779 +       n = src->vd_nblk;
32780 +       for (ul = 1; ul < n; ul++) {
32781 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32782 +                                           GFP_NOFS);
32783 +               if (unlikely(!tgt->vd_deblk[ul]))
32784 +                       goto out;
32785 +               tgt->vd_nblk++;
32786 +       }
32787 +       tgt->vd_nblk = n;
32788 +       tgt->vd_last.ul = tgt->vd_last.ul;
32789 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32790 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32791 +               - src->vd_deblk[src->vd_last.ul];
32792 +       /* smp_mb(); */
32793 +       return 0; /* success */
32794 +
32795 +out:
32796 +       rerr = reinit_vdir(tgt);
32797 +       BUG_ON(rerr);
32798 +       return err;
32799 +}
32800 +
32801 +int au_vdir_init(struct file *file)
32802 +{
32803 +       int err;
32804 +       struct inode *inode;
32805 +       struct au_vdir *vdir_cache, *allocated;
32806 +
32807 +       /* test file->f_pos here instead of ctx->pos */
32808 +       err = read_vdir(file, !file->f_pos);
32809 +       if (unlikely(err))
32810 +               goto out;
32811 +
32812 +       allocated = NULL;
32813 +       vdir_cache = au_fvdir_cache(file);
32814 +       if (!vdir_cache) {
32815 +               vdir_cache = alloc_vdir(file);
32816 +               err = PTR_ERR(vdir_cache);
32817 +               if (IS_ERR(vdir_cache))
32818 +                       goto out;
32819 +               allocated = vdir_cache;
32820 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32821 +               /* test file->f_pos here instead of ctx->pos */
32822 +               err = reinit_vdir(vdir_cache);
32823 +               if (unlikely(err))
32824 +                       goto out;
32825 +       } else
32826 +               return 0; /* success */
32827 +
32828 +       inode = file_inode(file);
32829 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32830 +       if (!err) {
32831 +               file->f_version = inode_query_iversion(inode);
32832 +               if (allocated)
32833 +                       au_set_fvdir_cache(file, allocated);
32834 +       } else if (allocated)
32835 +               au_vdir_free(allocated);
32836 +
32837 +out:
32838 +       return err;
32839 +}
32840 +
32841 +static loff_t calc_offset(struct au_vdir *vdir)
32842 +{
32843 +       loff_t offset;
32844 +       union au_vdir_deblk_p p;
32845 +
32846 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32847 +       offset = vdir->vd_last.p.deblk - p.deblk;
32848 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32849 +       return offset;
32850 +}
32851 +
32852 +/* returns true or false */
32853 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32854 +{
32855 +       int valid;
32856 +       unsigned int deblk_sz;
32857 +       unsigned long ul, n;
32858 +       loff_t offset;
32859 +       union au_vdir_deblk_p p, deblk_end;
32860 +       struct au_vdir *vdir_cache;
32861 +
32862 +       valid = 1;
32863 +       vdir_cache = au_fvdir_cache(file);
32864 +       offset = calc_offset(vdir_cache);
32865 +       AuDbg("offset %lld\n", offset);
32866 +       if (ctx->pos == offset)
32867 +               goto out;
32868 +
32869 +       vdir_cache->vd_last.ul = 0;
32870 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32871 +       if (!ctx->pos)
32872 +               goto out;
32873 +
32874 +       valid = 0;
32875 +       deblk_sz = vdir_cache->vd_deblk_sz;
32876 +       ul = div64_u64(ctx->pos, deblk_sz);
32877 +       AuDbg("ul %lu\n", ul);
32878 +       if (ul >= vdir_cache->vd_nblk)
32879 +               goto out;
32880 +
32881 +       n = vdir_cache->vd_nblk;
32882 +       for (; ul < n; ul++) {
32883 +               p.deblk = vdir_cache->vd_deblk[ul];
32884 +               deblk_end.deblk = p.deblk + deblk_sz;
32885 +               offset = ul;
32886 +               offset *= deblk_sz;
32887 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32888 +                       unsigned int l;
32889 +
32890 +                       l = calc_size(p.de->de_str.len);
32891 +                       offset += l;
32892 +                       p.deblk += l;
32893 +               }
32894 +               if (!is_deblk_end(&p, &deblk_end)) {
32895 +                       valid = 1;
32896 +                       vdir_cache->vd_last.ul = ul;
32897 +                       vdir_cache->vd_last.p = p;
32898 +                       break;
32899 +               }
32900 +       }
32901 +
32902 +out:
32903 +       /* smp_mb(); */
32904 +       if (!valid)
32905 +               AuDbg("valid %d\n", !valid);
32906 +       return valid;
32907 +}
32908 +
32909 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32910 +{
32911 +       unsigned int l, deblk_sz;
32912 +       union au_vdir_deblk_p deblk_end;
32913 +       struct au_vdir *vdir_cache;
32914 +       struct au_vdir_de *de;
32915 +
32916 +       if (!seek_vdir(file, ctx))
32917 +               return 0;
32918 +
32919 +       vdir_cache = au_fvdir_cache(file);
32920 +       deblk_sz = vdir_cache->vd_deblk_sz;
32921 +       while (1) {
32922 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32923 +               deblk_end.deblk += deblk_sz;
32924 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32925 +                       de = vdir_cache->vd_last.p.de;
32926 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32927 +                             de->de_str.len, de->de_str.name, ctx->pos,
32928 +                             (unsigned long)de->de_ino, de->de_type);
32929 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32930 +                                              de->de_str.len, de->de_ino,
32931 +                                              de->de_type))) {
32932 +                               /* todo: ignore the error caused by udba? */
32933 +                               /* return err; */
32934 +                               return 0;
32935 +                       }
32936 +
32937 +                       l = calc_size(de->de_str.len);
32938 +                       vdir_cache->vd_last.p.deblk += l;
32939 +                       ctx->pos += l;
32940 +               }
32941 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32942 +                       vdir_cache->vd_last.ul++;
32943 +                       vdir_cache->vd_last.p.deblk
32944 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32945 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32946 +                       continue;
32947 +               }
32948 +               break;
32949 +       }
32950 +
32951 +       /* smp_mb(); */
32952 +       return 0;
32953 +}
32954 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32955 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32956 +++ linux/fs/aufs/vfsub.c       2023-10-31 09:31:04.199880750 +0100
32957 @@ -0,0 +1,918 @@
32958 +// SPDX-License-Identifier: GPL-2.0
32959 +/*
32960 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32961 + *
32962 + * This program is free software; you can redistribute it and/or modify
32963 + * it under the terms of the GNU General Public License as published by
32964 + * the Free Software Foundation; either version 2 of the License, or
32965 + * (at your option) any later version.
32966 + *
32967 + * This program is distributed in the hope that it will be useful,
32968 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32969 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32970 + * GNU General Public License for more details.
32971 + *
32972 + * You should have received a copy of the GNU General Public License
32973 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32974 + */
32975 +
32976 +/*
32977 + * sub-routines for VFS
32978 + */
32979 +
32980 +#include <linux/mnt_namespace.h>
32981 +#include <linux/nsproxy.h>
32982 +#include <linux/security.h>
32983 +#include <linux/splice.h>
32984 +#include "aufs.h"
32985 +
32986 +#ifdef CONFIG_AUFS_BR_FUSE
32987 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32988 +{
32989 +       if (!au_test_fuse(h_sb) || !au_userns)
32990 +               return 0;
32991 +
32992 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32993 +}
32994 +#endif
32995 +
32996 +int vfsub_sync_filesystem(struct super_block *h_sb)
32997 +{
32998 +       int err;
32999 +
33000 +       lockdep_off();
33001 +       down_read(&h_sb->s_umount);
33002 +       err = sync_filesystem(h_sb);
33003 +       up_read(&h_sb->s_umount);
33004 +       lockdep_on();
33005 +
33006 +       return err;
33007 +}
33008 +
33009 +/* ---------------------------------------------------------------------- */
33010 +
33011 +int vfsub_update_h_iattr(struct path *h_path, int *did)
33012 +{
33013 +       int err;
33014 +       struct kstat st;
33015 +       struct super_block *h_sb;
33016 +
33017 +       /*
33018 +        * Always needs h_path->mnt for LSM or FUSE branch.
33019 +        */
33020 +       AuDebugOn(!h_path->mnt);
33021 +
33022 +       /* for remote fs, leave work for its getattr or d_revalidate */
33023 +       /* for bad i_attr fs, handle them in aufs_getattr() */
33024 +       /* still some fs may acquire i_mutex. we need to skip them */
33025 +       err = 0;
33026 +       if (!did)
33027 +               did = &err;
33028 +       h_sb = h_path->dentry->d_sb;
33029 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
33030 +       if (*did)
33031 +               err = vfsub_getattr(h_path, &st);
33032 +
33033 +       return err;
33034 +}
33035 +
33036 +/* ---------------------------------------------------------------------- */
33037 +
33038 +struct file *vfsub_dentry_open(struct path *path, int flags)
33039 +{
33040 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
33041 +                          current_cred());
33042 +}
33043 +
33044 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
33045 +{
33046 +       struct file *file;
33047 +
33048 +       lockdep_off();
33049 +       file = filp_open(path,
33050 +                        oflags /* | __FMODE_NONOTIFY */,
33051 +                        mode);
33052 +       lockdep_on();
33053 +       if (IS_ERR(file))
33054 +               goto out;
33055 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33056 +
33057 +out:
33058 +       return file;
33059 +}
33060 +
33061 +/*
33062 + * Ideally this function should call VFS:do_last() in order to keep all its
33063 + * checkings. But it is very hard for aufs to regenerate several VFS internal
33064 + * structure such as nameidata. This is a second (or third) best approach.
33065 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
33066 + */
33067 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33068 +                     struct vfsub_aopen_args *args)
33069 +{
33070 +       int err;
33071 +       struct au_branch *br = args->br;
33072 +       struct file *file = args->file;
33073 +       /* copied from linux/fs/namei.c:atomic_open() */
33074 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33075 +
33076 +       IMustLock(dir);
33077 +       AuDebugOn(!dir->i_op->atomic_open);
33078 +
33079 +       err = au_br_test_oflag(args->open_flag, br);
33080 +       if (unlikely(err))
33081 +               goto out;
33082 +
33083 +       au_lcnt_inc(&br->br_nfiles);
33084 +       file->f_path.dentry = DENTRY_NOT_SET;
33085 +       file->f_path.mnt = au_br_mnt(br);
33086 +       AuDbg("%ps\n", dir->i_op->atomic_open);
33087 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
33088 +                                    args->create_mode);
33089 +       if (unlikely(err < 0)) {
33090 +               au_lcnt_dec(&br->br_nfiles);
33091 +               goto out;
33092 +       }
33093 +
33094 +       /* temporary workaround for nfsv4 branch */
33095 +       if (au_test_nfs(dir->i_sb))
33096 +               nfs_mark_for_revalidate(dir);
33097 +
33098 +       if (file->f_mode & FMODE_CREATED)
33099 +               fsnotify_create(dir, dentry);
33100 +       if (!(file->f_mode & FMODE_OPENED)) {
33101 +               au_lcnt_dec(&br->br_nfiles);
33102 +               goto out;
33103 +       }
33104 +
33105 +       /* todo: call VFS:may_open() here */
33106 +       /* todo: ima_file_check() too? */
33107 +       if (!err && (args->open_flag & __FMODE_EXEC))
33108 +               err = deny_write_access(file);
33109 +       if (!err)
33110 +               fsnotify_open(file);
33111 +       else
33112 +               au_lcnt_dec(&br->br_nfiles);
33113 +       /* note that the file is created and still opened */
33114 +
33115 +out:
33116 +       return err;
33117 +}
33118 +
33119 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33120 +{
33121 +       int err;
33122 +
33123 +       err = kern_path(name, flags, path);
33124 +       if (!err && d_is_positive(path->dentry))
33125 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33126 +       return err;
33127 +}
33128 +
33129 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33130 +                                            struct path *ppath, int len)
33131 +{
33132 +       struct path path;
33133 +
33134 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
33135 +       if (IS_ERR(path.dentry))
33136 +               goto out;
33137 +       if (d_is_positive(path.dentry)) {
33138 +               path.mnt = ppath->mnt;
33139 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33140 +       }
33141 +
33142 +out:
33143 +       AuTraceErrPtr(path.dentry);
33144 +       return path.dentry;
33145 +}
33146 +
33147 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33148 +                                   int len)
33149 +{
33150 +       struct path path;
33151 +
33152 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33153 +       IMustLock(d_inode(ppath->dentry));
33154 +
33155 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
33156 +       if (IS_ERR(path.dentry))
33157 +               goto out;
33158 +       if (d_is_positive(path.dentry)) {
33159 +               path.mnt = ppath->mnt;
33160 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33161 +       }
33162 +
33163 +out:
33164 +       AuTraceErrPtr(path.dentry);
33165 +       return path.dentry;
33166 +}
33167 +
33168 +void vfsub_call_lkup_one(void *args)
33169 +{
33170 +       struct vfsub_lkup_one_args *a = args;
33171 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
33172 +}
33173 +
33174 +/* ---------------------------------------------------------------------- */
33175 +
33176 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33177 +                                struct dentry *d2, struct au_hinode *hdir2)
33178 +{
33179 +       struct dentry *d;
33180 +
33181 +       lockdep_off();
33182 +       d = lock_rename(d1, d2);
33183 +       lockdep_on();
33184 +       au_hn_suspend(hdir1);
33185 +       if (hdir1 != hdir2)
33186 +               au_hn_suspend(hdir2);
33187 +
33188 +       return d;
33189 +}
33190 +
33191 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33192 +                        struct dentry *d2, struct au_hinode *hdir2)
33193 +{
33194 +       au_hn_resume(hdir1);
33195 +       if (hdir1 != hdir2)
33196 +               au_hn_resume(hdir2);
33197 +       lockdep_off();
33198 +       unlock_rename(d1, d2);
33199 +       lockdep_on();
33200 +}
33201 +
33202 +/* ---------------------------------------------------------------------- */
33203 +
33204 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33205 +{
33206 +       int err;
33207 +       struct dentry *d;
33208 +       struct mnt_idmap *idmap;
33209 +
33210 +       IMustLock(dir);
33211 +
33212 +       d = path->dentry;
33213 +       path->dentry = d->d_parent;
33214 +       err = security_path_mknod(path, d, mode, 0);
33215 +       path->dentry = d;
33216 +       if (unlikely(err))
33217 +               goto out;
33218 +       idmap = mnt_idmap(path->mnt);
33219 +
33220 +       lockdep_off();
33221 +       err = vfs_create(idmap, dir, path->dentry, mode, want_excl);
33222 +       lockdep_on();
33223 +       if (!err) {
33224 +               struct path tmp = *path;
33225 +               int did;
33226 +
33227 +               vfsub_update_h_iattr(&tmp, &did);
33228 +               if (did) {
33229 +                       tmp.dentry = path->dentry->d_parent;
33230 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33231 +               }
33232 +               /*ignore*/
33233 +       }
33234 +
33235 +out:
33236 +       return err;
33237 +}
33238 +
33239 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33240 +{
33241 +       int err;
33242 +       struct dentry *d;
33243 +       struct mnt_idmap *idmap;
33244 +
33245 +       IMustLock(dir);
33246 +
33247 +       d = path->dentry;
33248 +       path->dentry = d->d_parent;
33249 +       err = security_path_symlink(path, d, symname);
33250 +       path->dentry = d;
33251 +       if (unlikely(err))
33252 +               goto out;
33253 +       idmap = mnt_idmap(path->mnt);
33254 +
33255 +       lockdep_off();
33256 +       err = vfs_symlink(idmap, dir, path->dentry, symname);
33257 +       lockdep_on();
33258 +       if (!err) {
33259 +               struct path tmp = *path;
33260 +               int did;
33261 +
33262 +               vfsub_update_h_iattr(&tmp, &did);
33263 +               if (did) {
33264 +                       tmp.dentry = path->dentry->d_parent;
33265 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33266 +               }
33267 +               /*ignore*/
33268 +       }
33269 +
33270 +out:
33271 +       return err;
33272 +}
33273 +
33274 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33275 +{
33276 +       int err;
33277 +       struct dentry *d;
33278 +       struct mnt_idmap *idmap;
33279 +
33280 +       IMustLock(dir);
33281 +
33282 +       d = path->dentry;
33283 +       path->dentry = d->d_parent;
33284 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33285 +       path->dentry = d;
33286 +       if (unlikely(err))
33287 +               goto out;
33288 +       idmap = mnt_idmap(path->mnt);
33289 +
33290 +       lockdep_off();
33291 +       err = vfs_mknod(idmap, dir, path->dentry, mode, dev);
33292 +       lockdep_on();
33293 +       if (!err) {
33294 +               struct path tmp = *path;
33295 +               int did;
33296 +
33297 +               vfsub_update_h_iattr(&tmp, &did);
33298 +               if (did) {
33299 +                       tmp.dentry = path->dentry->d_parent;
33300 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33301 +               }
33302 +               /*ignore*/
33303 +       }
33304 +
33305 +out:
33306 +       return err;
33307 +}
33308 +
33309 +static int au_test_nlink(struct inode *inode)
33310 +{
33311 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33312 +
33313 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33314 +           || inode->i_nlink < link_max)
33315 +               return 0;
33316 +       return -EMLINK;
33317 +}
33318 +
33319 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33320 +              struct inode **delegated_inode)
33321 +{
33322 +       int err;
33323 +       struct dentry *d;
33324 +       struct mnt_idmap *idmap;
33325 +
33326 +       IMustLock(dir);
33327 +
33328 +       err = au_test_nlink(d_inode(src_dentry));
33329 +       if (unlikely(err))
33330 +               return err;
33331 +
33332 +       /* we don't call may_linkat() */
33333 +       d = path->dentry;
33334 +       path->dentry = d->d_parent;
33335 +       err = security_path_link(src_dentry, path, d);
33336 +       path->dentry = d;
33337 +       if (unlikely(err))
33338 +               goto out;
33339 +       idmap = mnt_idmap(path->mnt);
33340 +
33341 +       lockdep_off();
33342 +       err = vfs_link(src_dentry, idmap, dir, path->dentry, delegated_inode);
33343 +       lockdep_on();
33344 +       if (!err) {
33345 +               struct path tmp = *path;
33346 +               int did;
33347 +
33348 +               /* fuse has different memory inode for the same inumber */
33349 +               vfsub_update_h_iattr(&tmp, &did);
33350 +               if (did) {
33351 +                       tmp.dentry = path->dentry->d_parent;
33352 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33353 +                       tmp.dentry = src_dentry;
33354 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33355 +               }
33356 +               /*ignore*/
33357 +       }
33358 +
33359 +out:
33360 +       return err;
33361 +}
33362 +
33363 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33364 +                struct inode *dir, struct path *path,
33365 +                struct inode **delegated_inode, unsigned int flags)
33366 +{
33367 +       int err;
33368 +       struct renamedata rd;
33369 +       struct path tmp = {
33370 +               .mnt    = path->mnt
33371 +       };
33372 +       struct dentry *d;
33373 +
33374 +       IMustLock(dir);
33375 +       IMustLock(src_dir);
33376 +
33377 +       d = path->dentry;
33378 +       path->dentry = d->d_parent;
33379 +       tmp.dentry = src_dentry->d_parent;
33380 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33381 +       path->dentry = d;
33382 +       if (unlikely(err))
33383 +               goto out;
33384 +
33385 +       rd.old_mnt_idmap = mnt_idmap(path->mnt);
33386 +       rd.old_dir = src_dir;
33387 +       rd.old_dentry = src_dentry;
33388 +       rd.new_mnt_idmap = rd.old_mnt_idmap;
33389 +       rd.new_dir = dir;
33390 +       rd.new_dentry = path->dentry;
33391 +       rd.delegated_inode = delegated_inode;
33392 +       rd.flags = flags;
33393 +       lockdep_off();
33394 +       err = vfs_rename(&rd);
33395 +       lockdep_on();
33396 +       if (!err) {
33397 +               int did;
33398 +
33399 +               tmp.dentry = d->d_parent;
33400 +               vfsub_update_h_iattr(&tmp, &did);
33401 +               if (did) {
33402 +                       tmp.dentry = src_dentry;
33403 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33404 +                       tmp.dentry = src_dentry->d_parent;
33405 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33406 +               }
33407 +               /*ignore*/
33408 +       }
33409 +
33410 +out:
33411 +       return err;
33412 +}
33413 +
33414 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33415 +{
33416 +       int err;
33417 +       struct dentry *d;
33418 +       struct mnt_idmap *idmap;
33419 +
33420 +       IMustLock(dir);
33421 +
33422 +       d = path->dentry;
33423 +       path->dentry = d->d_parent;
33424 +       err = security_path_mkdir(path, d, mode);
33425 +       path->dentry = d;
33426 +       if (unlikely(err))
33427 +               goto out;
33428 +       idmap = mnt_idmap(path->mnt);
33429 +
33430 +       lockdep_off();
33431 +       err = vfs_mkdir(idmap, dir, path->dentry, mode);
33432 +       lockdep_on();
33433 +       if (!err) {
33434 +               struct path tmp = *path;
33435 +               int did;
33436 +
33437 +               vfsub_update_h_iattr(&tmp, &did);
33438 +               if (did) {
33439 +                       tmp.dentry = path->dentry->d_parent;
33440 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33441 +               }
33442 +               /*ignore*/
33443 +       }
33444 +
33445 +out:
33446 +       return err;
33447 +}
33448 +
33449 +int vfsub_rmdir(struct inode *dir, struct path *path)
33450 +{
33451 +       int err;
33452 +       struct dentry *d;
33453 +       struct mnt_idmap *idmap;
33454 +
33455 +       IMustLock(dir);
33456 +
33457 +       d = path->dentry;
33458 +       path->dentry = d->d_parent;
33459 +       err = security_path_rmdir(path, d);
33460 +       path->dentry = d;
33461 +       if (unlikely(err))
33462 +               goto out;
33463 +       idmap = mnt_idmap(path->mnt);
33464 +
33465 +       lockdep_off();
33466 +       err = vfs_rmdir(idmap, dir, path->dentry);
33467 +       lockdep_on();
33468 +       if (!err) {
33469 +               struct path tmp = {
33470 +                       .dentry = path->dentry->d_parent,
33471 +                       .mnt    = path->mnt
33472 +               };
33473 +
33474 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33475 +       }
33476 +
33477 +out:
33478 +       return err;
33479 +}
33480 +
33481 +/* ---------------------------------------------------------------------- */
33482 +
33483 +/* todo: support mmap_sem? */
33484 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33485 +                    loff_t *ppos)
33486 +{
33487 +       ssize_t err;
33488 +
33489 +       lockdep_off();
33490 +       err = vfs_read(file, ubuf, count, ppos);
33491 +       lockdep_on();
33492 +       if (err >= 0)
33493 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33494 +       return err;
33495 +}
33496 +
33497 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33498 +                    loff_t *ppos)
33499 +{
33500 +       ssize_t err;
33501 +
33502 +       lockdep_off();
33503 +       err = kernel_read(file, kbuf, count, ppos);
33504 +       lockdep_on();
33505 +       AuTraceErr(err);
33506 +       if (err >= 0)
33507 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33508 +       return err;
33509 +}
33510 +
33511 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33512 +                     loff_t *ppos)
33513 +{
33514 +       ssize_t err;
33515 +
33516 +       lockdep_off();
33517 +       err = vfs_write(file, ubuf, count, ppos);
33518 +       lockdep_on();
33519 +       if (err >= 0)
33520 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33521 +       return err;
33522 +}
33523 +
33524 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33525 +{
33526 +       ssize_t err;
33527 +
33528 +       lockdep_off();
33529 +       err = kernel_write(file, kbuf, count, ppos);
33530 +       lockdep_on();
33531 +       if (err >= 0)
33532 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33533 +       return err;
33534 +}
33535 +
33536 +int vfsub_flush(struct file *file, fl_owner_t id)
33537 +{
33538 +       int err;
33539 +
33540 +       err = 0;
33541 +       if (file->f_op->flush) {
33542 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33543 +                       err = file->f_op->flush(file, id);
33544 +               else {
33545 +                       lockdep_off();
33546 +                       err = file->f_op->flush(file, id);
33547 +                       lockdep_on();
33548 +               }
33549 +               if (!err)
33550 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33551 +               /*ignore*/
33552 +       }
33553 +       return err;
33554 +}
33555 +
33556 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33557 +{
33558 +       int err;
33559 +
33560 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33561 +
33562 +       lockdep_off();
33563 +       err = iterate_dir(file, ctx);
33564 +       lockdep_on();
33565 +       if (err >= 0)
33566 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33567 +
33568 +       return err;
33569 +}
33570 +
33571 +long vfsub_splice_read(struct file *in, loff_t *ppos,
33572 +                      struct pipe_inode_info *pipe, size_t len,
33573 +                      unsigned int flags)
33574 +{
33575 +       long err;
33576 +
33577 +       lockdep_off();
33578 +       err = vfs_splice_read(in, ppos, pipe, len, flags);
33579 +       lockdep_on();
33580 +       file_accessed(in);
33581 +       if (err >= 0)
33582 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33583 +       return err;
33584 +}
33585 +
33586 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33587 +                      loff_t *ppos, size_t len, unsigned int flags)
33588 +{
33589 +       long err;
33590 +
33591 +       lockdep_off();
33592 +       err = do_splice_from(pipe, out, ppos, len, flags);
33593 +       lockdep_on();
33594 +       if (err >= 0)
33595 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33596 +       return err;
33597 +}
33598 +
33599 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33600 +{
33601 +       int err;
33602 +
33603 +       /* file can be NULL */
33604 +       lockdep_off();
33605 +       err = vfs_fsync(file, datasync);
33606 +       lockdep_on();
33607 +       if (!err) {
33608 +               if (!path) {
33609 +                       AuDebugOn(!file);
33610 +                       path = &file->f_path;
33611 +               }
33612 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33613 +       }
33614 +       return err;
33615 +}
33616 +
33617 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33618 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33619 +               struct file *h_file)
33620 +{
33621 +       int err;
33622 +       struct inode *h_inode;
33623 +       struct super_block *h_sb;
33624 +       struct mnt_idmap *h_idmap;
33625 +
33626 +       if (!h_file) {
33627 +               err = vfsub_truncate(h_path, length);
33628 +               goto out;
33629 +       }
33630 +
33631 +       h_inode = d_inode(h_path->dentry);
33632 +       h_sb = h_inode->i_sb;
33633 +       lockdep_off();
33634 +       sb_start_write(h_sb);
33635 +       lockdep_on();
33636 +       err = security_file_truncate(h_file);
33637 +       if (!err) {
33638 +               h_idmap = mnt_idmap(h_path->mnt);
33639 +               lockdep_off();
33640 +               err = do_truncate(h_idmap, h_path->dentry, length, attr,
33641 +                                 h_file);
33642 +               lockdep_on();
33643 +       }
33644 +       lockdep_off();
33645 +       sb_end_write(h_sb);
33646 +       lockdep_on();
33647 +
33648 +out:
33649 +       return err;
33650 +}
33651 +
33652 +/* ---------------------------------------------------------------------- */
33653 +
33654 +struct au_vfsub_mkdir_args {
33655 +       int *errp;
33656 +       struct inode *dir;
33657 +       struct path *path;
33658 +       int mode;
33659 +};
33660 +
33661 +static void au_call_vfsub_mkdir(void *args)
33662 +{
33663 +       struct au_vfsub_mkdir_args *a = args;
33664 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33665 +}
33666 +
33667 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33668 +{
33669 +       int err, do_sio, wkq_err;
33670 +       struct mnt_idmap *idmap;
33671 +
33672 +       idmap = mnt_idmap(path->mnt);
33673 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33674 +       if (!do_sio) {
33675 +               lockdep_off();
33676 +               err = vfsub_mkdir(dir, path, mode);
33677 +               lockdep_on();
33678 +       } else {
33679 +               struct au_vfsub_mkdir_args args = {
33680 +                       .errp   = &err,
33681 +                       .dir    = dir,
33682 +                       .path   = path,
33683 +                       .mode   = mode
33684 +               };
33685 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33686 +               if (unlikely(wkq_err))
33687 +                       err = wkq_err;
33688 +       }
33689 +
33690 +       return err;
33691 +}
33692 +
33693 +struct au_vfsub_rmdir_args {
33694 +       int *errp;
33695 +       struct inode *dir;
33696 +       struct path *path;
33697 +};
33698 +
33699 +static void au_call_vfsub_rmdir(void *args)
33700 +{
33701 +       struct au_vfsub_rmdir_args *a = args;
33702 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33703 +}
33704 +
33705 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33706 +{
33707 +       int err, do_sio, wkq_err;
33708 +       struct mnt_idmap *idmap;
33709 +
33710 +       idmap = mnt_idmap(path->mnt);
33711 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33712 +       if (!do_sio) {
33713 +               lockdep_off();
33714 +               err = vfsub_rmdir(dir, path);
33715 +               lockdep_on();
33716 +       } else {
33717 +               struct au_vfsub_rmdir_args args = {
33718 +                       .errp   = &err,
33719 +                       .dir    = dir,
33720 +                       .path   = path
33721 +               };
33722 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33723 +               if (unlikely(wkq_err))
33724 +                       err = wkq_err;
33725 +       }
33726 +
33727 +       return err;
33728 +}
33729 +
33730 +/* ---------------------------------------------------------------------- */
33731 +
33732 +struct notify_change_args {
33733 +       int *errp;
33734 +       struct path *path;
33735 +       struct iattr *ia;
33736 +       struct inode **delegated_inode;
33737 +};
33738 +
33739 +static void call_notify_change(void *args)
33740 +{
33741 +       struct notify_change_args *a = args;
33742 +       struct inode *h_inode;
33743 +       struct mnt_idmap *idmap;
33744 +
33745 +       h_inode = d_inode(a->path->dentry);
33746 +       IMustLock(h_inode);
33747 +
33748 +       *a->errp = -EPERM;
33749 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33750 +               idmap = mnt_idmap(a->path->mnt);
33751 +               lockdep_off();
33752 +               *a->errp = notify_change(idmap, a->path->dentry, a->ia,
33753 +                                        a->delegated_inode);
33754 +               lockdep_on();
33755 +               if (!*a->errp)
33756 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33757 +       }
33758 +       AuTraceErr(*a->errp);
33759 +}
33760 +
33761 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33762 +                       struct inode **delegated_inode)
33763 +{
33764 +       int err;
33765 +       struct notify_change_args args = {
33766 +               .errp                   = &err,
33767 +               .path                   = path,
33768 +               .ia                     = ia,
33769 +               .delegated_inode        = delegated_inode
33770 +       };
33771 +
33772 +       call_notify_change(&args);
33773 +
33774 +       return err;
33775 +}
33776 +
33777 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33778 +                           struct inode **delegated_inode)
33779 +{
33780 +       int err, wkq_err;
33781 +       struct notify_change_args args = {
33782 +               .errp                   = &err,
33783 +               .path                   = path,
33784 +               .ia                     = ia,
33785 +               .delegated_inode        = delegated_inode
33786 +       };
33787 +
33788 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33789 +       if (unlikely(wkq_err))
33790 +               err = wkq_err;
33791 +
33792 +       return err;
33793 +}
33794 +
33795 +/* ---------------------------------------------------------------------- */
33796 +
33797 +struct unlink_args {
33798 +       int *errp;
33799 +       struct inode *dir;
33800 +       struct path *path;
33801 +       struct inode **delegated_inode;
33802 +};
33803 +
33804 +static void call_unlink(void *args)
33805 +{
33806 +       struct unlink_args *a = args;
33807 +       struct dentry *d = a->path->dentry;
33808 +       struct inode *h_inode;
33809 +       struct mnt_idmap *idmap;
33810 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33811 +                                     && au_dcount(d) == 1);
33812 +
33813 +       IMustLock(a->dir);
33814 +
33815 +       a->path->dentry = d->d_parent;
33816 +       *a->errp = security_path_unlink(a->path, d);
33817 +       a->path->dentry = d;
33818 +       if (unlikely(*a->errp))
33819 +               return;
33820 +
33821 +       if (!stop_sillyrename)
33822 +               dget(d);
33823 +       h_inode = NULL;
33824 +       if (d_is_positive(d)) {
33825 +               h_inode = d_inode(d);
33826 +               ihold(h_inode);
33827 +       }
33828 +
33829 +       idmap = mnt_idmap(a->path->mnt);
33830 +       lockdep_off();
33831 +       *a->errp = vfs_unlink(idmap, a->dir, d, a->delegated_inode);
33832 +       lockdep_on();
33833 +       if (!*a->errp) {
33834 +               struct path tmp = {
33835 +                       .dentry = d->d_parent,
33836 +                       .mnt    = a->path->mnt
33837 +               };
33838 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33839 +       }
33840 +
33841 +       if (!stop_sillyrename)
33842 +               dput(d);
33843 +       if (h_inode)
33844 +               iput(h_inode);
33845 +
33846 +       AuTraceErr(*a->errp);
33847 +}
33848 +
33849 +/*
33850 + * @dir: must be locked.
33851 + * @dentry: target dentry.
33852 + */
33853 +int vfsub_unlink(struct inode *dir, struct path *path,
33854 +                struct inode **delegated_inode, int force)
33855 +{
33856 +       int err;
33857 +       struct unlink_args args = {
33858 +               .errp                   = &err,
33859 +               .dir                    = dir,
33860 +               .path                   = path,
33861 +               .delegated_inode        = delegated_inode
33862 +       };
33863 +
33864 +       if (!force)
33865 +               call_unlink(&args);
33866 +       else {
33867 +               int wkq_err;
33868 +
33869 +               wkq_err = au_wkq_wait(call_unlink, &args);
33870 +               if (unlikely(wkq_err))
33871 +                       err = wkq_err;
33872 +       }
33873 +
33874 +       return err;
33875 +}
33876 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33877 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33878 +++ linux/fs/aufs/vfsub.h       2023-10-31 09:31:04.199880750 +0100
33879 @@ -0,0 +1,403 @@
33880 +/* SPDX-License-Identifier: GPL-2.0 */
33881 +/*
33882 + * Copyright (C) 2005-2022 Junjiro R. Okajima
33883 + *
33884 + * This program is free software; you can redistribute it and/or modify
33885 + * it under the terms of the GNU General Public License as published by
33886 + * the Free Software Foundation; either version 2 of the License, or
33887 + * (at your option) any later version.
33888 + *
33889 + * This program is distributed in the hope that it will be useful,
33890 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33891 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33892 + * GNU General Public License for more details.
33893 + *
33894 + * You should have received a copy of the GNU General Public License
33895 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33896 + */
33897 +
33898 +/*
33899 + * sub-routines for VFS
33900 + */
33901 +
33902 +#ifndef __AUFS_VFSUB_H__
33903 +#define __AUFS_VFSUB_H__
33904 +
33905 +#ifdef __KERNEL__
33906 +
33907 +#include <linux/fs.h>
33908 +#include <linux/mount.h>
33909 +#include <linux/posix_acl.h>
33910 +#include <linux/xattr.h>
33911 +#include "debug.h"
33912 +
33913 +/* copied from linux/fs/internal.h */
33914 +/* todo: BAD approach!! */
33915 +extern void __mnt_drop_write(struct vfsmount *);
33916 +extern struct file *alloc_empty_file(int, const struct cred *);
33917 +
33918 +/* ---------------------------------------------------------------------- */
33919 +
33920 +/* lock subclass for lower inode */
33921 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33922 +/* reduce? gave up. */
33923 +enum {
33924 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33925 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33926 +       AuLsc_I_PARENT2,        /* copyup dirs */
33927 +       AuLsc_I_PARENT3,        /* copyup wh */
33928 +       AuLsc_I_CHILD,
33929 +       AuLsc_I_CHILD2,
33930 +       AuLsc_I_End
33931 +};
33932 +
33933 +/* to debug easier, do not make them inlined functions */
33934 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33935 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33936 +
33937 +/* ---------------------------------------------------------------------- */
33938 +
33939 +static inline void vfsub_drop_nlink(struct inode *inode)
33940 +{
33941 +       AuDebugOn(!inode->i_nlink);
33942 +       drop_nlink(inode);
33943 +}
33944 +
33945 +static inline void vfsub_dead_dir(struct inode *inode)
33946 +{
33947 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33948 +       inode->i_flags |= S_DEAD;
33949 +       clear_nlink(inode);
33950 +}
33951 +
33952 +static inline int vfsub_native_ro(struct inode *inode)
33953 +{
33954 +       return sb_rdonly(inode->i_sb)
33955 +               || IS_RDONLY(inode)
33956 +               /* || IS_APPEND(inode) */
33957 +               || IS_IMMUTABLE(inode);
33958 +}
33959 +
33960 +#ifdef CONFIG_AUFS_BR_FUSE
33961 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33962 +#else
33963 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33964 +#endif
33965 +
33966 +int vfsub_sync_filesystem(struct super_block *h_sb);
33967 +
33968 +/* ---------------------------------------------------------------------- */
33969 +
33970 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33971 +struct file *vfsub_dentry_open(struct path *path, int flags);
33972 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33973 +struct au_branch;
33974 +struct vfsub_aopen_args {
33975 +       struct file             *file;
33976 +       unsigned int            open_flag;
33977 +       umode_t                 create_mode;
33978 +       struct au_branch        *br;
33979 +};
33980 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33981 +                     struct vfsub_aopen_args *args);
33982 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33983 +
33984 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33985 +                                            struct path *ppath, int len);
33986 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33987 +                                   int len);
33988 +
33989 +struct vfsub_lkup_one_args {
33990 +       struct dentry **errp;
33991 +       struct qstr *name;
33992 +       struct path *ppath;
33993 +};
33994 +
33995 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33996 +                                           struct path *ppath)
33997 +{
33998 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
33999 +}
34000 +
34001 +void vfsub_call_lkup_one(void *args);
34002 +
34003 +/* ---------------------------------------------------------------------- */
34004 +
34005 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
34006 +{
34007 +       int err;
34008 +
34009 +       lockdep_off();
34010 +       err = mnt_want_write(mnt);
34011 +       lockdep_on();
34012 +       return err;
34013 +}
34014 +
34015 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
34016 +{
34017 +       lockdep_off();
34018 +       mnt_drop_write(mnt);
34019 +       lockdep_on();
34020 +}
34021 +
34022 +#if 0 /* reserved */
34023 +static inline void vfsub_mnt_drop_write_file(struct file *file)
34024 +{
34025 +       lockdep_off();
34026 +       mnt_drop_write_file(file);
34027 +       lockdep_on();
34028 +}
34029 +#endif
34030 +
34031 +static inline void vfsub_file_start_write(struct file *file)
34032 +{
34033 +       lockdep_off();
34034 +       file_start_write(file);
34035 +       lockdep_on();
34036 +}
34037 +
34038 +static inline void vfsub_file_end_write(struct file *file)
34039 +{
34040 +       lockdep_off();
34041 +       file_end_write(file);
34042 +       lockdep_on();
34043 +}
34044 +
34045 +/* ---------------------------------------------------------------------- */
34046 +
34047 +struct au_hinode;
34048 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
34049 +                                struct dentry *d2, struct au_hinode *hdir2);
34050 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
34051 +                        struct dentry *d2, struct au_hinode *hdir2);
34052 +
34053 +int vfsub_create(struct inode *dir, struct path *path, int mode,
34054 +                bool want_excl);
34055 +int vfsub_symlink(struct inode *dir, struct path *path,
34056 +                 const char *symname);
34057 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
34058 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
34059 +              struct path *path, struct inode **delegated_inode);
34060 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
34061 +                struct inode *hdir, struct path *path,
34062 +                struct inode **delegated_inode, unsigned int flags);
34063 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
34064 +int vfsub_rmdir(struct inode *dir, struct path *path);
34065 +
34066 +/* ---------------------------------------------------------------------- */
34067 +
34068 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
34069 +                    loff_t *ppos);
34070 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
34071 +                       loff_t *ppos);
34072 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
34073 +                     loff_t *ppos);
34074 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
34075 +                     loff_t *ppos);
34076 +int vfsub_flush(struct file *file, fl_owner_t id);
34077 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
34078 +
34079 +static inline loff_t vfsub_f_size_read(struct file *file)
34080 +{
34081 +       return i_size_read(file_inode(file));
34082 +}
34083 +
34084 +static inline unsigned int vfsub_file_flags(struct file *file)
34085 +{
34086 +       unsigned int flags;
34087 +
34088 +       spin_lock(&file->f_lock);
34089 +       flags = file->f_flags;
34090 +       spin_unlock(&file->f_lock);
34091 +
34092 +       return flags;
34093 +}
34094 +
34095 +static inline int vfsub_file_execed(struct file *file)
34096 +{
34097 +       /* todo: direct access f_flags */
34098 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34099 +}
34100 +
34101 +#if 0 /* reserved */
34102 +static inline void vfsub_file_accessed(struct file *h_file)
34103 +{
34104 +       file_accessed(h_file);
34105 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34106 +}
34107 +#endif
34108 +
34109 +#if 0 /* reserved */
34110 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34111 +                                    struct dentry *h_dentry)
34112 +{
34113 +       struct path h_path = {
34114 +               .dentry = h_dentry,
34115 +               .mnt    = h_mnt
34116 +       };
34117 +       touch_atime(&h_path);
34118 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34119 +}
34120 +#endif
34121 +
34122 +static inline int vfsub_update_time(struct inode *h_inode, int flags)
34123 +{
34124 +       return inode_update_time(h_inode, flags);
34125 +       /* no vfsub_update_h_iattr() since we don't have struct path */
34126 +}
34127 +
34128 +#ifdef CONFIG_FS_POSIX_ACL
34129 +static inline int vfsub_acl_chmod(struct mnt_idmap *h_idmap,
34130 +                                 struct dentry *h_dentry, umode_t h_mode)
34131 +{
34132 +       int err;
34133 +
34134 +       err = posix_acl_chmod(h_idmap, h_dentry, h_mode);
34135 +       if (err == -EOPNOTSUPP)
34136 +               err = 0;
34137 +       return err;
34138 +}
34139 +#else
34140 +AuStubInt0(vfsub_acl_chmod, struct mnt_idmap *h_idmap,
34141 +          struct dentry *h_dentry, umode_t h_mode);
34142 +#endif
34143 +
34144 +long vfsub_splice_read(struct file *in, loff_t *ppos,
34145 +                      struct pipe_inode_info *pipe, size_t len,
34146 +                      unsigned int flags);
34147 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34148 +                      loff_t *ppos, size_t len, unsigned int flags);
34149 +
34150 +static inline long vfsub_truncate(struct path *path, loff_t length)
34151 +{
34152 +       long err;
34153 +
34154 +       lockdep_off();
34155 +       err = vfs_truncate(path, length);
34156 +       lockdep_on();
34157 +       return err;
34158 +}
34159 +
34160 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34161 +               struct file *h_file);
34162 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34163 +
34164 +/*
34165 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34166 + * ioctl.
34167 + */
34168 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34169 +                                           loff_t len)
34170 +{
34171 +       loff_t err;
34172 +
34173 +       lockdep_off();
34174 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34175 +       lockdep_on();
34176 +
34177 +       return err;
34178 +}
34179 +
34180 +/* copy_file_range(2) is a systemcall */
34181 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34182 +                                           struct file *dst, loff_t dst_pos,
34183 +                                           size_t len, unsigned int flags)
34184 +{
34185 +       ssize_t ssz;
34186 +
34187 +       lockdep_off();
34188 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34189 +       lockdep_on();
34190 +
34191 +       return ssz;
34192 +}
34193 +
34194 +/* ---------------------------------------------------------------------- */
34195 +
34196 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34197 +{
34198 +       loff_t err;
34199 +
34200 +       lockdep_off();
34201 +       err = vfs_llseek(file, offset, origin);
34202 +       lockdep_on();
34203 +       return err;
34204 +}
34205 +
34206 +/* ---------------------------------------------------------------------- */
34207 +
34208 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34209 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34210 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34211 +                           struct inode **delegated_inode);
34212 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34213 +                       struct inode **delegated_inode);
34214 +int vfsub_unlink(struct inode *dir, struct path *path,
34215 +                struct inode **delegated_inode, int force);
34216 +
34217 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34218 +{
34219 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34220 +}
34221 +
34222 +/* ---------------------------------------------------------------------- */
34223 +
34224 +static inline int vfsub_setxattr(struct mnt_idmap *idmap,
34225 +                                struct dentry *dentry, const char *name,
34226 +                                const void *value, size_t size, int flags)
34227 +{
34228 +       int err;
34229 +
34230 +       lockdep_off();
34231 +       err = vfs_setxattr(idmap, dentry, name, value, size, flags);
34232 +       lockdep_on();
34233 +
34234 +       return err;
34235 +}
34236 +
34237 +static inline int vfsub_removexattr(struct mnt_idmap *idmap,
34238 +                                   struct dentry *dentry, const char *name)
34239 +{
34240 +       int err;
34241 +
34242 +       lockdep_off();
34243 +       err = vfs_removexattr(idmap, dentry, name);
34244 +       lockdep_on();
34245 +
34246 +       return err;
34247 +}
34248 +
34249 +#ifdef CONFIG_FS_POSIX_ACL
34250 +static inline int vfsub_set_acl(struct mnt_idmap *idmap,
34251 +                               struct dentry *dentry, const char *name,
34252 +                               struct posix_acl *acl)
34253 +{
34254 +       int err;
34255 +
34256 +       lockdep_off();
34257 +       err = vfs_set_acl(idmap, dentry, name, acl);
34258 +       lockdep_on();
34259 +
34260 +       return err;
34261 +}
34262 +
34263 +static inline int vfsub_remove_acl(struct mnt_idmap *idmap,
34264 +                                  struct dentry *dentry, const char *name)
34265 +{
34266 +       int err;
34267 +
34268 +       lockdep_off();
34269 +       err = vfs_remove_acl(idmap, dentry, name);
34270 +       lockdep_on();
34271 +
34272 +       return err;
34273 +}
34274 +#else
34275 +AuStubInt0(vfsub_set_acl, struct mnt_idmap *idmap, struct dentry *dentry,
34276 +          const char *name, struct posix_acl *acl);
34277 +AuStubInt0(vfsub_remove_acl, struct mnt_idmap *idmap,
34278 +          struct dentry *dentry, const char *name);
34279 +#endif
34280 +
34281 +#endif /* __KERNEL__ */
34282 +#endif /* __AUFS_VFSUB_H__ */
34283 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34284 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34285 +++ linux/fs/aufs/wbr_policy.c  2022-11-05 23:02:18.969222617 +0100
34286 @@ -0,0 +1,830 @@
34287 +// SPDX-License-Identifier: GPL-2.0
34288 +/*
34289 + * Copyright (C) 2005-2022 Junjiro R. Okajima
34290 + *
34291 + * This program is free software; you can redistribute it and/or modify
34292 + * it under the terms of the GNU General Public License as published by
34293 + * the Free Software Foundation; either version 2 of the License, or
34294 + * (at your option) any later version.
34295 + *
34296 + * This program is distributed in the hope that it will be useful,
34297 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34298 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34299 + * GNU General Public License for more details.
34300 + *
34301 + * You should have received a copy of the GNU General Public License
34302 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34303 + */
34304 +
34305 +/*
34306 + * policies for selecting one among multiple writable branches
34307 + */
34308 +
34309 +#include <linux/statfs.h>
34310 +#include "aufs.h"
34311 +
34312 +/* subset of cpup_attr() */
34313 +static noinline_for_stack
34314 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34315 +{
34316 +       int err, sbits;
34317 +       struct iattr ia;
34318 +       struct inode *h_isrc;
34319 +
34320 +       h_isrc = d_inode(h_src);
34321 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34322 +       ia.ia_mode = h_isrc->i_mode;
34323 +       ia.ia_uid = h_isrc->i_uid;
34324 +       ia.ia_gid = h_isrc->i_gid;
34325 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34326 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34327 +       /* no delegation since it is just created */
34328 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34329 +
34330 +       /* is this nfs only? */
34331 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34332 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34333 +               ia.ia_mode = h_isrc->i_mode;
34334 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34335 +       }
34336 +
34337 +       return err;
34338 +}
34339 +
34340 +#define AuCpdown_PARENT_OPQ    1
34341 +#define AuCpdown_WHED          (1 << 1)
34342 +#define AuCpdown_MADE_DIR      (1 << 2)
34343 +#define AuCpdown_DIROPQ                (1 << 3)
34344 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34345 +#define au_fset_cpdown(flags, name) \
34346 +       do { (flags) |= AuCpdown_##name; } while (0)
34347 +#define au_fclr_cpdown(flags, name) \
34348 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34349 +
34350 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34351 +                            unsigned int *flags)
34352 +{
34353 +       int err;
34354 +       struct dentry *opq_dentry;
34355 +
34356 +       opq_dentry = au_diropq_create(dentry, bdst);
34357 +       err = PTR_ERR(opq_dentry);
34358 +       if (IS_ERR(opq_dentry))
34359 +               goto out;
34360 +       dput(opq_dentry);
34361 +       au_fset_cpdown(*flags, DIROPQ);
34362 +
34363 +out:
34364 +       return err;
34365 +}
34366 +
34367 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34368 +                           struct inode *dir, aufs_bindex_t bdst)
34369 +{
34370 +       int err;
34371 +       struct path h_path;
34372 +       struct au_branch *br;
34373 +
34374 +       br = au_sbr(dentry->d_sb, bdst);
34375 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34376 +       err = PTR_ERR(h_path.dentry);
34377 +       if (IS_ERR(h_path.dentry))
34378 +               goto out;
34379 +
34380 +       err = 0;
34381 +       if (d_is_positive(h_path.dentry)) {
34382 +               h_path.mnt = au_br_mnt(br);
34383 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34384 +                                         dentry);
34385 +       }
34386 +       dput(h_path.dentry);
34387 +
34388 +out:
34389 +       return err;
34390 +}
34391 +
34392 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34393 +                        struct au_pin *pin,
34394 +                        struct dentry *h_parent, void *arg)
34395 +{
34396 +       int err, rerr;
34397 +       aufs_bindex_t bopq, btop;
34398 +       struct path h_path;
34399 +       struct dentry *parent;
34400 +       struct inode *h_dir, *h_inode, *inode, *dir;
34401 +       unsigned int *flags = arg;
34402 +
34403 +       btop = au_dbtop(dentry);
34404 +       /* dentry is di-locked */
34405 +       parent = dget_parent(dentry);
34406 +       dir = d_inode(parent);
34407 +       h_dir = d_inode(h_parent);
34408 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34409 +       IMustLock(h_dir);
34410 +
34411 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34412 +       if (unlikely(err < 0))
34413 +               goto out;
34414 +       h_path.dentry = au_h_dptr(dentry, bdst);
34415 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34416 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34417 +       if (unlikely(err))
34418 +               goto out_put;
34419 +       au_fset_cpdown(*flags, MADE_DIR);
34420 +
34421 +       bopq = au_dbdiropq(dentry);
34422 +       au_fclr_cpdown(*flags, WHED);
34423 +       au_fclr_cpdown(*flags, DIROPQ);
34424 +       if (au_dbwh(dentry) == bdst)
34425 +               au_fset_cpdown(*flags, WHED);
34426 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34427 +               au_fset_cpdown(*flags, PARENT_OPQ);
34428 +       h_inode = d_inode(h_path.dentry);
34429 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34430 +       if (au_ftest_cpdown(*flags, WHED)) {
34431 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34432 +               if (unlikely(err)) {
34433 +                       inode_unlock(h_inode);
34434 +                       goto out_dir;
34435 +               }
34436 +       }
34437 +
34438 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34439 +       inode_unlock(h_inode);
34440 +       if (unlikely(err))
34441 +               goto out_opq;
34442 +
34443 +       if (au_ftest_cpdown(*flags, WHED)) {
34444 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34445 +               if (unlikely(err))
34446 +                       goto out_opq;
34447 +       }
34448 +
34449 +       inode = d_inode(dentry);
34450 +       if (au_ibbot(inode) < bdst)
34451 +               au_set_ibbot(inode, bdst);
34452 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34453 +                     au_hi_flags(inode, /*isdir*/1));
34454 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34455 +       goto out; /* success */
34456 +
34457 +       /* revert */
34458 +out_opq:
34459 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34460 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34461 +               rerr = au_diropq_remove(dentry, bdst);
34462 +               inode_unlock(h_inode);
34463 +               if (unlikely(rerr)) {
34464 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34465 +                               dentry, bdst, rerr);
34466 +                       err = -EIO;
34467 +                       goto out;
34468 +               }
34469 +       }
34470 +out_dir:
34471 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34472 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34473 +               if (unlikely(rerr)) {
34474 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34475 +                               dentry, bdst, rerr);
34476 +                       err = -EIO;
34477 +               }
34478 +       }
34479 +out_put:
34480 +       au_set_h_dptr(dentry, bdst, NULL);
34481 +       if (au_dbbot(dentry) == bdst)
34482 +               au_update_dbbot(dentry);
34483 +out:
34484 +       dput(parent);
34485 +       return err;
34486 +}
34487 +
34488 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34489 +{
34490 +       int err;
34491 +       unsigned int flags;
34492 +
34493 +       flags = 0;
34494 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34495 +
34496 +       return err;
34497 +}
34498 +
34499 +/* ---------------------------------------------------------------------- */
34500 +
34501 +/* policies for create */
34502 +
34503 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34504 +{
34505 +       int err, i, j, ndentry;
34506 +       aufs_bindex_t bopq;
34507 +       struct au_dcsub_pages dpages;
34508 +       struct au_dpage *dpage;
34509 +       struct dentry **dentries, *parent, *d;
34510 +
34511 +       err = au_dpages_init(&dpages, GFP_NOFS);
34512 +       if (unlikely(err))
34513 +               goto out;
34514 +       parent = dget_parent(dentry);
34515 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34516 +       if (unlikely(err))
34517 +               goto out_free;
34518 +
34519 +       err = bindex;
34520 +       for (i = 0; i < dpages.ndpage; i++) {
34521 +               dpage = dpages.dpages + i;
34522 +               dentries = dpage->dentries;
34523 +               ndentry = dpage->ndentry;
34524 +               for (j = 0; j < ndentry; j++) {
34525 +                       d = dentries[j];
34526 +                       di_read_lock_parent2(d, !AuLock_IR);
34527 +                       bopq = au_dbdiropq(d);
34528 +                       di_read_unlock(d, !AuLock_IR);
34529 +                       if (bopq >= 0 && bopq < err)
34530 +                               err = bopq;
34531 +               }
34532 +       }
34533 +
34534 +out_free:
34535 +       dput(parent);
34536 +       au_dpages_free(&dpages);
34537 +out:
34538 +       return err;
34539 +}
34540 +
34541 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34542 +{
34543 +       for (; bindex >= 0; bindex--)
34544 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34545 +                       return bindex;
34546 +       return -EROFS;
34547 +}
34548 +
34549 +/* top down parent */
34550 +static int au_wbr_create_tdp(struct dentry *dentry,
34551 +                            unsigned int flags __maybe_unused)
34552 +{
34553 +       int err;
34554 +       aufs_bindex_t btop, bindex;
34555 +       struct super_block *sb;
34556 +       struct dentry *parent, *h_parent;
34557 +
34558 +       sb = dentry->d_sb;
34559 +       btop = au_dbtop(dentry);
34560 +       err = btop;
34561 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34562 +               goto out;
34563 +
34564 +       err = -EROFS;
34565 +       parent = dget_parent(dentry);
34566 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34567 +               h_parent = au_h_dptr(parent, bindex);
34568 +               if (!h_parent || d_is_negative(h_parent))
34569 +                       continue;
34570 +
34571 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34572 +                       err = bindex;
34573 +                       break;
34574 +               }
34575 +       }
34576 +       dput(parent);
34577 +
34578 +       /* bottom up here */
34579 +       if (unlikely(err < 0)) {
34580 +               err = au_wbr_bu(sb, btop - 1);
34581 +               if (err >= 0)
34582 +                       err = au_wbr_nonopq(dentry, err);
34583 +       }
34584 +
34585 +out:
34586 +       AuDbg("b%d\n", err);
34587 +       return err;
34588 +}
34589 +
34590 +/* ---------------------------------------------------------------------- */
34591 +
34592 +/* an exception for the policy other than tdp */
34593 +static int au_wbr_create_exp(struct dentry *dentry)
34594 +{
34595 +       int err;
34596 +       aufs_bindex_t bwh, bdiropq;
34597 +       struct dentry *parent;
34598 +
34599 +       err = -1;
34600 +       bwh = au_dbwh(dentry);
34601 +       parent = dget_parent(dentry);
34602 +       bdiropq = au_dbdiropq(parent);
34603 +       if (bwh >= 0) {
34604 +               if (bdiropq >= 0)
34605 +                       err = min(bdiropq, bwh);
34606 +               else
34607 +                       err = bwh;
34608 +               AuDbg("%d\n", err);
34609 +       } else if (bdiropq >= 0) {
34610 +               err = bdiropq;
34611 +               AuDbg("%d\n", err);
34612 +       }
34613 +       dput(parent);
34614 +
34615 +       if (err >= 0)
34616 +               err = au_wbr_nonopq(dentry, err);
34617 +
34618 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34619 +               err = -1;
34620 +
34621 +       AuDbg("%d\n", err);
34622 +       return err;
34623 +}
34624 +
34625 +/* ---------------------------------------------------------------------- */
34626 +
34627 +/* round robin */
34628 +static int au_wbr_create_init_rr(struct super_block *sb)
34629 +{
34630 +       int err;
34631 +
34632 +       err = au_wbr_bu(sb, au_sbbot(sb));
34633 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34634 +       /* smp_mb(); */
34635 +
34636 +       AuDbg("b%d\n", err);
34637 +       return err;
34638 +}
34639 +
34640 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34641 +{
34642 +       int err, nbr;
34643 +       unsigned int u;
34644 +       aufs_bindex_t bindex, bbot;
34645 +       struct super_block *sb;
34646 +       atomic_t *next;
34647 +
34648 +       err = au_wbr_create_exp(dentry);
34649 +       if (err >= 0)
34650 +               goto out;
34651 +
34652 +       sb = dentry->d_sb;
34653 +       next = &au_sbi(sb)->si_wbr_rr_next;
34654 +       bbot = au_sbbot(sb);
34655 +       nbr = bbot + 1;
34656 +       for (bindex = 0; bindex <= bbot; bindex++) {
34657 +               if (!au_ftest_wbr(flags, DIR)) {
34658 +                       err = atomic_dec_return(next) + 1;
34659 +                       /* modulo for 0 is meaningless */
34660 +                       if (unlikely(!err))
34661 +                               err = atomic_dec_return(next) + 1;
34662 +               } else
34663 +                       err = atomic_read(next);
34664 +               AuDbg("%d\n", err);
34665 +               u = err;
34666 +               err = u % nbr;
34667 +               AuDbg("%d\n", err);
34668 +               if (!au_br_rdonly(au_sbr(sb, err)))
34669 +                       break;
34670 +               err = -EROFS;
34671 +       }
34672 +
34673 +       if (err >= 0)
34674 +               err = au_wbr_nonopq(dentry, err);
34675 +
34676 +out:
34677 +       AuDbg("%d\n", err);
34678 +       return err;
34679 +}
34680 +
34681 +/* ---------------------------------------------------------------------- */
34682 +
34683 +/* most free space */
34684 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34685 +{
34686 +       struct super_block *sb;
34687 +       struct au_branch *br;
34688 +       struct au_wbr_mfs *mfs;
34689 +       struct dentry *h_parent;
34690 +       aufs_bindex_t bindex, bbot;
34691 +       int err;
34692 +       unsigned long long b, bavail;
34693 +       struct path h_path;
34694 +       /* reduce the stack usage */
34695 +       struct kstatfs *st;
34696 +
34697 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34698 +       if (unlikely(!st)) {
34699 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34700 +               return;
34701 +       }
34702 +
34703 +       bavail = 0;
34704 +       sb = dentry->d_sb;
34705 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34706 +       MtxMustLock(&mfs->mfs_lock);
34707 +       mfs->mfs_bindex = -EROFS;
34708 +       mfs->mfsrr_bytes = 0;
34709 +       if (!parent) {
34710 +               bindex = 0;
34711 +               bbot = au_sbbot(sb);
34712 +       } else {
34713 +               bindex = au_dbtop(parent);
34714 +               bbot = au_dbtaildir(parent);
34715 +       }
34716 +
34717 +       for (; bindex <= bbot; bindex++) {
34718 +               if (parent) {
34719 +                       h_parent = au_h_dptr(parent, bindex);
34720 +                       if (!h_parent || d_is_negative(h_parent))
34721 +                               continue;
34722 +               }
34723 +               br = au_sbr(sb, bindex);
34724 +               if (au_br_rdonly(br))
34725 +                       continue;
34726 +
34727 +               /* sb->s_root for NFS is unreliable */
34728 +               h_path.mnt = au_br_mnt(br);
34729 +               h_path.dentry = h_path.mnt->mnt_root;
34730 +               err = vfs_statfs(&h_path, st);
34731 +               if (unlikely(err)) {
34732 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34733 +                       continue;
34734 +               }
34735 +
34736 +               /* when the available size is equal, select the lower one */
34737 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34738 +                            || sizeof(b) < sizeof(st->f_bsize));
34739 +               b = st->f_bavail * st->f_bsize;
34740 +               br->br_wbr->wbr_bytes = b;
34741 +               if (b >= bavail) {
34742 +                       bavail = b;
34743 +                       mfs->mfs_bindex = bindex;
34744 +                       mfs->mfs_jiffy = jiffies;
34745 +               }
34746 +       }
34747 +
34748 +       mfs->mfsrr_bytes = bavail;
34749 +       AuDbg("b%d\n", mfs->mfs_bindex);
34750 +       au_kfree_rcu(st);
34751 +}
34752 +
34753 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34754 +{
34755 +       int err;
34756 +       struct dentry *parent;
34757 +       struct super_block *sb;
34758 +       struct au_wbr_mfs *mfs;
34759 +
34760 +       err = au_wbr_create_exp(dentry);
34761 +       if (err >= 0)
34762 +               goto out;
34763 +
34764 +       sb = dentry->d_sb;
34765 +       parent = NULL;
34766 +       if (au_ftest_wbr(flags, PARENT))
34767 +               parent = dget_parent(dentry);
34768 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34769 +       mutex_lock(&mfs->mfs_lock);
34770 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34771 +           || mfs->mfs_bindex < 0
34772 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34773 +               au_mfs(dentry, parent);
34774 +       mutex_unlock(&mfs->mfs_lock);
34775 +       err = mfs->mfs_bindex;
34776 +       dput(parent);
34777 +
34778 +       if (err >= 0)
34779 +               err = au_wbr_nonopq(dentry, err);
34780 +
34781 +out:
34782 +       AuDbg("b%d\n", err);
34783 +       return err;
34784 +}
34785 +
34786 +static int au_wbr_create_init_mfs(struct super_block *sb)
34787 +{
34788 +       struct au_wbr_mfs *mfs;
34789 +
34790 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34791 +       mutex_init(&mfs->mfs_lock);
34792 +       mfs->mfs_jiffy = 0;
34793 +       mfs->mfs_bindex = -EROFS;
34794 +
34795 +       return 0;
34796 +}
34797 +
34798 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34799 +{
34800 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34801 +       return 0;
34802 +}
34803 +
34804 +/* ---------------------------------------------------------------------- */
34805 +
34806 +/* top down regardless parent, and then mfs */
34807 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34808 +                              unsigned int flags __maybe_unused)
34809 +{
34810 +       int err;
34811 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34812 +       unsigned long long watermark;
34813 +       struct super_block *sb;
34814 +       struct au_wbr_mfs *mfs;
34815 +       struct au_branch *br;
34816 +       struct dentry *parent;
34817 +
34818 +       sb = dentry->d_sb;
34819 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34820 +       mutex_lock(&mfs->mfs_lock);
34821 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34822 +           || mfs->mfs_bindex < 0)
34823 +               au_mfs(dentry, /*parent*/NULL);
34824 +       watermark = mfs->mfsrr_watermark;
34825 +       bmfs = mfs->mfs_bindex;
34826 +       mutex_unlock(&mfs->mfs_lock);
34827 +
34828 +       /* another style of au_wbr_create_exp() */
34829 +       bwh = au_dbwh(dentry);
34830 +       parent = dget_parent(dentry);
34831 +       btail = au_dbtaildir(parent);
34832 +       if (bwh >= 0 && bwh < btail)
34833 +               btail = bwh;
34834 +
34835 +       err = au_wbr_nonopq(dentry, btail);
34836 +       if (unlikely(err < 0))
34837 +               goto out;
34838 +       btail = err;
34839 +       bfound = -1;
34840 +       for (bindex = 0; bindex <= btail; bindex++) {
34841 +               br = au_sbr(sb, bindex);
34842 +               if (au_br_rdonly(br))
34843 +                       continue;
34844 +               if (br->br_wbr->wbr_bytes > watermark) {
34845 +                       bfound = bindex;
34846 +                       break;
34847 +               }
34848 +       }
34849 +       err = bfound;
34850 +       if (err < 0)
34851 +               err = bmfs;
34852 +
34853 +out:
34854 +       dput(parent);
34855 +       AuDbg("b%d\n", err);
34856 +       return err;
34857 +}
34858 +
34859 +/* ---------------------------------------------------------------------- */
34860 +
34861 +/* most free space and then round robin */
34862 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34863 +{
34864 +       int err;
34865 +       struct au_wbr_mfs *mfs;
34866 +
34867 +       err = au_wbr_create_mfs(dentry, flags);
34868 +       if (err >= 0) {
34869 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34870 +               mutex_lock(&mfs->mfs_lock);
34871 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34872 +                       err = au_wbr_create_rr(dentry, flags);
34873 +               mutex_unlock(&mfs->mfs_lock);
34874 +       }
34875 +
34876 +       AuDbg("b%d\n", err);
34877 +       return err;
34878 +}
34879 +
34880 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34881 +{
34882 +       int err;
34883 +
34884 +       au_wbr_create_init_mfs(sb); /* ignore */
34885 +       err = au_wbr_create_init_rr(sb);
34886 +
34887 +       return err;
34888 +}
34889 +
34890 +/* ---------------------------------------------------------------------- */
34891 +
34892 +/* top down parent and most free space */
34893 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34894 +{
34895 +       int err, e2;
34896 +       unsigned long long b;
34897 +       aufs_bindex_t bindex, btop, bbot;
34898 +       struct super_block *sb;
34899 +       struct dentry *parent, *h_parent;
34900 +       struct au_branch *br;
34901 +
34902 +       err = au_wbr_create_tdp(dentry, flags);
34903 +       if (unlikely(err < 0))
34904 +               goto out;
34905 +       parent = dget_parent(dentry);
34906 +       btop = au_dbtop(parent);
34907 +       bbot = au_dbtaildir(parent);
34908 +       if (btop == bbot)
34909 +               goto out_parent; /* success */
34910 +
34911 +       e2 = au_wbr_create_mfs(dentry, flags);
34912 +       if (e2 < 0)
34913 +               goto out_parent; /* success */
34914 +
34915 +       /* when the available size is equal, select upper one */
34916 +       sb = dentry->d_sb;
34917 +       br = au_sbr(sb, err);
34918 +       b = br->br_wbr->wbr_bytes;
34919 +       AuDbg("b%d, %llu\n", err, b);
34920 +
34921 +       for (bindex = btop; bindex <= bbot; bindex++) {
34922 +               h_parent = au_h_dptr(parent, bindex);
34923 +               if (!h_parent || d_is_negative(h_parent))
34924 +                       continue;
34925 +
34926 +               br = au_sbr(sb, bindex);
34927 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34928 +                       b = br->br_wbr->wbr_bytes;
34929 +                       err = bindex;
34930 +                       AuDbg("b%d, %llu\n", err, b);
34931 +               }
34932 +       }
34933 +
34934 +       if (err >= 0)
34935 +               err = au_wbr_nonopq(dentry, err);
34936 +
34937 +out_parent:
34938 +       dput(parent);
34939 +out:
34940 +       AuDbg("b%d\n", err);
34941 +       return err;
34942 +}
34943 +
34944 +/* ---------------------------------------------------------------------- */
34945 +
34946 +/*
34947 + * - top down parent
34948 + * - most free space with parent
34949 + * - most free space round-robin regardless parent
34950 + */
34951 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34952 +{
34953 +       int err;
34954 +       unsigned long long watermark;
34955 +       struct super_block *sb;
34956 +       struct au_branch *br;
34957 +       struct au_wbr_mfs *mfs;
34958 +
34959 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34960 +       if (unlikely(err < 0))
34961 +               goto out;
34962 +
34963 +       sb = dentry->d_sb;
34964 +       br = au_sbr(sb, err);
34965 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34966 +       mutex_lock(&mfs->mfs_lock);
34967 +       watermark = mfs->mfsrr_watermark;
34968 +       mutex_unlock(&mfs->mfs_lock);
34969 +       if (br->br_wbr->wbr_bytes < watermark)
34970 +               /* regardless the parent dir */
34971 +               err = au_wbr_create_mfsrr(dentry, flags);
34972 +
34973 +out:
34974 +       AuDbg("b%d\n", err);
34975 +       return err;
34976 +}
34977 +
34978 +/* ---------------------------------------------------------------------- */
34979 +
34980 +/* policies for copyup */
34981 +
34982 +/* top down parent */
34983 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34984 +{
34985 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34986 +}
34987 +
34988 +/* bottom up parent */
34989 +static int au_wbr_copyup_bup(struct dentry *dentry)
34990 +{
34991 +       int err;
34992 +       aufs_bindex_t bindex, btop;
34993 +       struct dentry *parent, *h_parent;
34994 +       struct super_block *sb;
34995 +
34996 +       err = -EROFS;
34997 +       sb = dentry->d_sb;
34998 +       parent = dget_parent(dentry);
34999 +       btop = au_dbtop(parent);
35000 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
35001 +               h_parent = au_h_dptr(parent, bindex);
35002 +               if (!h_parent || d_is_negative(h_parent))
35003 +                       continue;
35004 +
35005 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
35006 +                       err = bindex;
35007 +                       break;
35008 +               }
35009 +       }
35010 +       dput(parent);
35011 +
35012 +       /* bottom up here */
35013 +       if (unlikely(err < 0))
35014 +               err = au_wbr_bu(sb, btop - 1);
35015 +
35016 +       AuDbg("b%d\n", err);
35017 +       return err;
35018 +}
35019 +
35020 +/* bottom up */
35021 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
35022 +{
35023 +       int err;
35024 +
35025 +       err = au_wbr_bu(dentry->d_sb, btop);
35026 +       AuDbg("b%d\n", err);
35027 +       if (err > btop)
35028 +               err = au_wbr_nonopq(dentry, err);
35029 +
35030 +       AuDbg("b%d\n", err);
35031 +       return err;
35032 +}
35033 +
35034 +static int au_wbr_copyup_bu(struct dentry *dentry)
35035 +{
35036 +       int err;
35037 +       aufs_bindex_t btop;
35038 +
35039 +       btop = au_dbtop(dentry);
35040 +       err = au_wbr_do_copyup_bu(dentry, btop);
35041 +       return err;
35042 +}
35043 +
35044 +/* ---------------------------------------------------------------------- */
35045 +
35046 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
35047 +       [AuWbrCopyup_TDP] = {
35048 +               .copyup = au_wbr_copyup_tdp
35049 +       },
35050 +       [AuWbrCopyup_BUP] = {
35051 +               .copyup = au_wbr_copyup_bup
35052 +       },
35053 +       [AuWbrCopyup_BU] = {
35054 +               .copyup = au_wbr_copyup_bu
35055 +       }
35056 +};
35057 +
35058 +struct au_wbr_create_operations au_wbr_create_ops[] = {
35059 +       [AuWbrCreate_TDP] = {
35060 +               .create = au_wbr_create_tdp
35061 +       },
35062 +       [AuWbrCreate_RR] = {
35063 +               .create = au_wbr_create_rr,
35064 +               .init   = au_wbr_create_init_rr
35065 +       },
35066 +       [AuWbrCreate_MFS] = {
35067 +               .create = au_wbr_create_mfs,
35068 +               .init   = au_wbr_create_init_mfs,
35069 +               .fin    = au_wbr_create_fin_mfs
35070 +       },
35071 +       [AuWbrCreate_MFSV] = {
35072 +               .create = au_wbr_create_mfs,
35073 +               .init   = au_wbr_create_init_mfs,
35074 +               .fin    = au_wbr_create_fin_mfs
35075 +       },
35076 +       [AuWbrCreate_MFSRR] = {
35077 +               .create = au_wbr_create_mfsrr,
35078 +               .init   = au_wbr_create_init_mfsrr,
35079 +               .fin    = au_wbr_create_fin_mfs
35080 +       },
35081 +       [AuWbrCreate_MFSRRV] = {
35082 +               .create = au_wbr_create_mfsrr,
35083 +               .init   = au_wbr_create_init_mfsrr,
35084 +               .fin    = au_wbr_create_fin_mfs
35085 +       },
35086 +       [AuWbrCreate_TDMFS] = {
35087 +               .create = au_wbr_create_tdmfs,
35088 +               .init   = au_wbr_create_init_mfs,
35089 +               .fin    = au_wbr_create_fin_mfs
35090 +       },
35091 +       [AuWbrCreate_TDMFSV] = {
35092 +               .create = au_wbr_create_tdmfs,
35093 +               .init   = au_wbr_create_init_mfs,
35094 +               .fin    = au_wbr_create_fin_mfs
35095 +       },
35096 +       [AuWbrCreate_PMFS] = {
35097 +               .create = au_wbr_create_pmfs,
35098 +               .init   = au_wbr_create_init_mfs,
35099 +               .fin    = au_wbr_create_fin_mfs
35100 +       },
35101 +       [AuWbrCreate_PMFSV] = {
35102 +               .create = au_wbr_create_pmfs,
35103 +               .init   = au_wbr_create_init_mfs,
35104 +               .fin    = au_wbr_create_fin_mfs
35105 +       },
35106 +       [AuWbrCreate_PMFSRR] = {
35107 +               .create = au_wbr_create_pmfsrr,
35108 +               .init   = au_wbr_create_init_mfsrr,
35109 +               .fin    = au_wbr_create_fin_mfs
35110 +       },
35111 +       [AuWbrCreate_PMFSRRV] = {
35112 +               .create = au_wbr_create_pmfsrr,
35113 +               .init   = au_wbr_create_init_mfsrr,
35114 +               .fin    = au_wbr_create_fin_mfs
35115 +       }
35116 +};
35117 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
35118 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
35119 +++ linux/fs/aufs/whout.c       2023-10-31 09:31:04.199880750 +0100
35120 @@ -0,0 +1,1072 @@
35121 +// SPDX-License-Identifier: GPL-2.0
35122 +/*
35123 + * Copyright (C) 2005-2022 Junjiro R. Okajima
35124 + *
35125 + * This program is free software; you can redistribute it and/or modify
35126 + * it under the terms of the GNU General Public License as published by
35127 + * the Free Software Foundation; either version 2 of the License, or
35128 + * (at your option) any later version.
35129 + *
35130 + * This program is distributed in the hope that it will be useful,
35131 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35132 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35133 + * GNU General Public License for more details.
35134 + *
35135 + * You should have received a copy of the GNU General Public License
35136 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35137 + */
35138 +
35139 +/*
35140 + * whiteout for logical deletion and opaque directory
35141 + */
35142 +
35143 +#include "aufs.h"
35144 +
35145 +#define WH_MASK                        0444
35146 +
35147 +/*
35148 + * If a directory contains this file, then it is opaque.  We start with the
35149 + * .wh. flag so that it is blocked by lookup.
35150 + */
35151 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35152 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
35153 +
35154 +/*
35155 + * generate whiteout name, which is NOT terminated by NULL.
35156 + * @name: original d_name.name
35157 + * @len: original d_name.len
35158 + * @wh: whiteout qstr
35159 + * returns zero when succeeds, otherwise error.
35160 + * succeeded value as wh->name should be freed by kfree().
35161 + */
35162 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35163 +{
35164 +       char *p;
35165 +
35166 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35167 +               return -ENAMETOOLONG;
35168 +
35169 +       wh->len = name->len + AUFS_WH_PFX_LEN;
35170 +       p = kmalloc(wh->len, GFP_NOFS);
35171 +       wh->name = p;
35172 +       if (p) {
35173 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35174 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35175 +               /* smp_mb(); */
35176 +               return 0;
35177 +       }
35178 +       return -ENOMEM;
35179 +}
35180 +
35181 +/* ---------------------------------------------------------------------- */
35182 +
35183 +/*
35184 + * test if the @wh_name exists under @h_ppath.
35185 + * @try_sio specifies the necessary of super-io.
35186 + */
35187 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
35188 +              struct qstr *wh_name, int try_sio)
35189 +{
35190 +       int err;
35191 +       struct dentry *wh_dentry;
35192 +
35193 +       if (!try_sio)
35194 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
35195 +       else
35196 +               wh_dentry = au_sio_lkup_one(h_idmap, wh_name, h_ppath);
35197 +       err = PTR_ERR(wh_dentry);
35198 +       if (IS_ERR(wh_dentry)) {
35199 +               if (err == -ENAMETOOLONG)
35200 +                       err = 0;
35201 +               goto out;
35202 +       }
35203 +
35204 +       err = 0;
35205 +       if (d_is_negative(wh_dentry))
35206 +               goto out_wh; /* success */
35207 +
35208 +       err = 1;
35209 +       if (d_is_reg(wh_dentry))
35210 +               goto out_wh; /* success */
35211 +
35212 +       err = -EIO;
35213 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35214 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35215 +
35216 +out_wh:
35217 +       dput(wh_dentry);
35218 +out:
35219 +       return err;
35220 +}
35221 +
35222 +/*
35223 + * test if the @h_path->dentry sets opaque or not.
35224 + */
35225 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path)
35226 +{
35227 +       int err;
35228 +       struct inode *h_dir;
35229 +
35230 +       h_dir = d_inode(h_path->dentry);
35231 +       err = au_wh_test(h_idmap, h_path, &diropq_name,
35232 +                        au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC));
35233 +       return err;
35234 +}
35235 +
35236 +/*
35237 + * returns a negative dentry whose name is unique and temporary.
35238 + */
35239 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35240 +                            struct qstr *prefix)
35241 +{
35242 +       struct dentry *dentry;
35243 +       int i;
35244 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35245 +               *name, *p;
35246 +       /* strict atomic_t is unnecessary here */
35247 +       static unsigned short cnt;
35248 +       struct qstr qs;
35249 +       struct path h_ppath;
35250 +       struct mnt_idmap *h_idmap;
35251 +
35252 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35253 +
35254 +       name = defname;
35255 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35256 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35257 +               dentry = ERR_PTR(-ENAMETOOLONG);
35258 +               if (unlikely(qs.len > NAME_MAX))
35259 +                       goto out;
35260 +               dentry = ERR_PTR(-ENOMEM);
35261 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35262 +               if (unlikely(!name))
35263 +                       goto out;
35264 +       }
35265 +
35266 +       /* doubly whiteout-ed */
35267 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35268 +       p = name + AUFS_WH_PFX_LEN * 2;
35269 +       memcpy(p, prefix->name, prefix->len);
35270 +       p += prefix->len;
35271 +       *p++ = '.';
35272 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35273 +
35274 +       h_ppath.dentry = h_parent;
35275 +       h_ppath.mnt = au_br_mnt(br);
35276 +       h_idmap = au_br_idmap(br);
35277 +       qs.name = name;
35278 +       for (i = 0; i < 3; i++) {
35279 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35280 +               dentry = au_sio_lkup_one(h_idmap, &qs, &h_ppath);
35281 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35282 +                       goto out_name;
35283 +               dput(dentry);
35284 +       }
35285 +       /* pr_warn("could not get random name\n"); */
35286 +       dentry = ERR_PTR(-EEXIST);
35287 +       AuDbg("%.*s\n", AuLNPair(&qs));
35288 +       BUG();
35289 +
35290 +out_name:
35291 +       if (name != defname)
35292 +               au_kfree_try_rcu(name);
35293 +out:
35294 +       AuTraceErrPtr(dentry);
35295 +       return dentry;
35296 +}
35297 +
35298 +/*
35299 + * rename the @h_dentry on @br to the whiteouted temporary name.
35300 + */
35301 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35302 +{
35303 +       int err;
35304 +       struct path h_path = {
35305 +               .mnt = au_br_mnt(br)
35306 +       };
35307 +       struct inode *h_dir, *delegated;
35308 +       struct dentry *h_parent;
35309 +
35310 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35311 +       h_dir = d_inode(h_parent);
35312 +       IMustLock(h_dir);
35313 +
35314 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35315 +       err = PTR_ERR(h_path.dentry);
35316 +       if (IS_ERR(h_path.dentry))
35317 +               goto out;
35318 +
35319 +       /* under the same dir, no need to lock_rename() */
35320 +       delegated = NULL;
35321 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35322 +                          /*flags*/0);
35323 +       AuTraceErr(err);
35324 +       if (unlikely(err == -EWOULDBLOCK)) {
35325 +               pr_warn("cannot retry for NFSv4 delegation"
35326 +                       " for an internal rename\n");
35327 +               iput(delegated);
35328 +       }
35329 +       dput(h_path.dentry);
35330 +
35331 +out:
35332 +       AuTraceErr(err);
35333 +       return err;
35334 +}
35335 +
35336 +/* ---------------------------------------------------------------------- */
35337 +/*
35338 + * functions for removing a whiteout
35339 + */
35340 +
35341 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35342 +{
35343 +       int err, force;
35344 +       struct inode *delegated;
35345 +
35346 +       /*
35347 +        * forces superio when the dir has a sticky bit.
35348 +        * this may be a violation of unix fs semantics.
35349 +        */
35350 +       force = (h_dir->i_mode & S_ISVTX)
35351 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35352 +       delegated = NULL;
35353 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35354 +       if (unlikely(err == -EWOULDBLOCK)) {
35355 +               pr_warn("cannot retry for NFSv4 delegation"
35356 +                       " for an internal unlink\n");
35357 +               iput(delegated);
35358 +       }
35359 +       return err;
35360 +}
35361 +
35362 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35363 +                       struct dentry *dentry)
35364 +{
35365 +       int err;
35366 +
35367 +       err = do_unlink_wh(h_dir, h_path);
35368 +       if (!err && dentry)
35369 +               au_set_dbwh(dentry, -1);
35370 +
35371 +       return err;
35372 +}
35373 +
35374 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35375 +{
35376 +       int err;
35377 +       struct path h_path;
35378 +
35379 +       err = 0;
35380 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35381 +       if (IS_ERR(h_path.dentry))
35382 +               err = PTR_ERR(h_path.dentry);
35383 +       else {
35384 +               if (d_is_reg(h_path.dentry)) {
35385 +                       h_path.mnt = h_ppath->mnt;
35386 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35387 +               }
35388 +               dput(h_path.dentry);
35389 +       }
35390 +
35391 +       return err;
35392 +}
35393 +
35394 +/* ---------------------------------------------------------------------- */
35395 +/*
35396 + * initialize/clean whiteout for a branch
35397 + */
35398 +
35399 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35400 +                       const int isdir)
35401 +{
35402 +       int err;
35403 +       struct inode *delegated;
35404 +
35405 +       if (d_is_negative(whpath->dentry))
35406 +               return;
35407 +
35408 +       if (isdir)
35409 +               err = vfsub_rmdir(h_dir, whpath);
35410 +       else {
35411 +               delegated = NULL;
35412 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35413 +               if (unlikely(err == -EWOULDBLOCK)) {
35414 +                       pr_warn("cannot retry for NFSv4 delegation"
35415 +                               " for an internal unlink\n");
35416 +                       iput(delegated);
35417 +               }
35418 +       }
35419 +       if (unlikely(err))
35420 +               pr_warn("failed removing %pd (%d), ignored.\n",
35421 +                       whpath->dentry, err);
35422 +}
35423 +
35424 +static int test_linkable(struct dentry *h_root)
35425 +{
35426 +       struct inode *h_dir = d_inode(h_root);
35427 +
35428 +       if (h_dir->i_op->link)
35429 +               return 0;
35430 +
35431 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35432 +              h_root, au_sbtype(h_root->d_sb));
35433 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35434 +}
35435 +
35436 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35437 +static int au_whdir(struct inode *h_dir, struct path *path)
35438 +{
35439 +       int err;
35440 +
35441 +       err = -EEXIST;
35442 +       if (d_is_negative(path->dentry)) {
35443 +               int mode = 0700;
35444 +
35445 +               if (au_test_nfs(path->dentry->d_sb))
35446 +                       mode |= 0111;
35447 +               err = vfsub_mkdir(h_dir, path, mode);
35448 +       } else if (d_is_dir(path->dentry))
35449 +               err = 0;
35450 +       else
35451 +               pr_err("unknown %pd exists\n", path->dentry);
35452 +
35453 +       return err;
35454 +}
35455 +
35456 +struct au_wh_base {
35457 +       const struct qstr *name;
35458 +       struct dentry *dentry;
35459 +};
35460 +
35461 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35462 +                         struct path *h_path)
35463 +{
35464 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35465 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35466 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35467 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35468 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35469 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35470 +}
35471 +
35472 +/*
35473 + * returns tri-state,
35474 + * minus: error, caller should print the message
35475 + * zero: success
35476 + * plus: error, caller should NOT print the message
35477 + */
35478 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35479 +                               int do_plink, struct au_wh_base base[],
35480 +                               struct path *h_path)
35481 +{
35482 +       int err;
35483 +       struct inode *h_dir;
35484 +
35485 +       h_dir = d_inode(h_root);
35486 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35487 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35488 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35489 +       if (do_plink) {
35490 +               err = test_linkable(h_root);
35491 +               if (unlikely(err)) {
35492 +                       err = 1;
35493 +                       goto out;
35494 +               }
35495 +
35496 +               err = au_whdir(h_dir, h_path);
35497 +               if (unlikely(err))
35498 +                       goto out;
35499 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35500 +       } else
35501 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35502 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35503 +       err = au_whdir(h_dir, h_path);
35504 +       if (unlikely(err))
35505 +               goto out;
35506 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35507 +
35508 +out:
35509 +       return err;
35510 +}
35511 +
35512 +/*
35513 + * for the moment, aufs supports the branch filesystem which does not support
35514 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35515 + * copyup failed. finally, such filesystem will not be used as the writable
35516 + * branch.
35517 + *
35518 + * returns tri-state, see above.
35519 + */
35520 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35521 +                        int do_plink, struct au_wh_base base[],
35522 +                        struct path *h_path)
35523 +{
35524 +       int err;
35525 +       struct inode *h_dir;
35526 +
35527 +       WbrWhMustWriteLock(wbr);
35528 +
35529 +       err = test_linkable(h_root);
35530 +       if (unlikely(err)) {
35531 +               err = 1;
35532 +               goto out;
35533 +       }
35534 +
35535 +       /*
35536 +        * todo: should this create be done in /sbin/mount.aufs helper?
35537 +        */
35538 +       err = -EEXIST;
35539 +       h_dir = d_inode(h_root);
35540 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35541 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35542 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35543 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35544 +               err = 0;
35545 +       else
35546 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35547 +       if (unlikely(err))
35548 +               goto out;
35549 +
35550 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35551 +       if (do_plink) {
35552 +               err = au_whdir(h_dir, h_path);
35553 +               if (unlikely(err))
35554 +                       goto out;
35555 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35556 +       } else
35557 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35558 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35559 +
35560 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35561 +       err = au_whdir(h_dir, h_path);
35562 +       if (unlikely(err))
35563 +               goto out;
35564 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35565 +
35566 +out:
35567 +       return err;
35568 +}
35569 +
35570 +/*
35571 + * initialize the whiteout base file/dir for @br.
35572 + */
35573 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35574 +{
35575 +       int err, i;
35576 +       const unsigned char do_plink
35577 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35578 +       struct inode *h_dir;
35579 +       struct path path = br->br_path;
35580 +       struct dentry *h_root = path.dentry;
35581 +       struct au_wbr *wbr = br->br_wbr;
35582 +       static const struct qstr base_name[] = {
35583 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35584 +                                         sizeof(AUFS_BASE_NAME) - 1),
35585 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35586 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35587 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35588 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35589 +       };
35590 +       struct au_wh_base base[] = {
35591 +               [AuBrWh_BASE] = {
35592 +                       .name   = base_name + AuBrWh_BASE,
35593 +                       .dentry = NULL
35594 +               },
35595 +               [AuBrWh_PLINK] = {
35596 +                       .name   = base_name + AuBrWh_PLINK,
35597 +                       .dentry = NULL
35598 +               },
35599 +               [AuBrWh_ORPH] = {
35600 +                       .name   = base_name + AuBrWh_ORPH,
35601 +                       .dentry = NULL
35602 +               }
35603 +       };
35604 +
35605 +       if (wbr)
35606 +               WbrWhMustWriteLock(wbr);
35607 +
35608 +       for (i = 0; i < AuBrWh_Last; i++) {
35609 +               /* doubly whiteouted */
35610 +               struct dentry *d;
35611 +
35612 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35613 +               err = PTR_ERR(d);
35614 +               if (IS_ERR(d))
35615 +                       goto out;
35616 +
35617 +               base[i].dentry = d;
35618 +               AuDebugOn(wbr
35619 +                         && wbr->wbr_wh[i]
35620 +                         && wbr->wbr_wh[i] != base[i].dentry);
35621 +       }
35622 +
35623 +       if (wbr)
35624 +               for (i = 0; i < AuBrWh_Last; i++) {
35625 +                       dput(wbr->wbr_wh[i]);
35626 +                       wbr->wbr_wh[i] = NULL;
35627 +               }
35628 +
35629 +       err = 0;
35630 +       if (!au_br_writable(br->br_perm)) {
35631 +               h_dir = d_inode(h_root);
35632 +               au_wh_init_ro(h_dir, base, &path);
35633 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35634 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35635 +               if (err > 0)
35636 +                       goto out;
35637 +               else if (err)
35638 +                       goto out_err;
35639 +       } else {
35640 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35641 +               if (err > 0)
35642 +                       goto out;
35643 +               else if (err)
35644 +                       goto out_err;
35645 +       }
35646 +       goto out; /* success */
35647 +
35648 +out_err:
35649 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35650 +              err, h_root, au_sbtype(h_root->d_sb));
35651 +out:
35652 +       for (i = 0; i < AuBrWh_Last; i++)
35653 +               dput(base[i].dentry);
35654 +       return err;
35655 +}
35656 +
35657 +/* ---------------------------------------------------------------------- */
35658 +/*
35659 + * whiteouts are all hard-linked usually.
35660 + * when its link count reaches a ceiling, we create a new whiteout base
35661 + * asynchronously.
35662 + */
35663 +
35664 +struct reinit_br_wh {
35665 +       struct super_block *sb;
35666 +       struct au_branch *br;
35667 +};
35668 +
35669 +static void reinit_br_wh(void *arg)
35670 +{
35671 +       int err;
35672 +       aufs_bindex_t bindex;
35673 +       struct path h_path;
35674 +       struct reinit_br_wh *a = arg;
35675 +       struct au_wbr *wbr;
35676 +       struct inode *dir, *delegated;
35677 +       struct dentry *h_root;
35678 +       struct au_hinode *hdir;
35679 +
35680 +       err = 0;
35681 +       wbr = a->br->br_wbr;
35682 +       /* big aufs lock */
35683 +       si_noflush_write_lock(a->sb);
35684 +       if (!au_br_writable(a->br->br_perm))
35685 +               goto out;
35686 +       bindex = au_br_index(a->sb, a->br->br_id);
35687 +       if (unlikely(bindex < 0))
35688 +               goto out;
35689 +
35690 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35691 +       dir = d_inode(a->sb->s_root);
35692 +       hdir = au_hi(dir, bindex);
35693 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35694 +       AuDebugOn(h_root != au_br_dentry(a->br));
35695 +
35696 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35697 +       wbr_wh_write_lock(wbr);
35698 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35699 +                         h_root, a->br);
35700 +       if (!err) {
35701 +               h_path.dentry = wbr->wbr_whbase;
35702 +               h_path.mnt = au_br_mnt(a->br);
35703 +               delegated = NULL;
35704 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35705 +                                  /*force*/0);
35706 +               if (unlikely(err == -EWOULDBLOCK)) {
35707 +                       pr_warn("cannot retry for NFSv4 delegation"
35708 +                               " for an internal unlink\n");
35709 +                       iput(delegated);
35710 +               }
35711 +       } else {
35712 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35713 +               err = 0;
35714 +       }
35715 +       dput(wbr->wbr_whbase);
35716 +       wbr->wbr_whbase = NULL;
35717 +       if (!err)
35718 +               err = au_wh_init(a->br, a->sb);
35719 +       wbr_wh_write_unlock(wbr);
35720 +       au_hn_inode_unlock(hdir);
35721 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35722 +       if (!err)
35723 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35724 +
35725 +out:
35726 +       if (wbr)
35727 +               atomic_dec(&wbr->wbr_wh_running);
35728 +       au_lcnt_dec(&a->br->br_count);
35729 +       si_write_unlock(a->sb);
35730 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35731 +       au_kfree_rcu(a);
35732 +       if (unlikely(err))
35733 +               AuIOErr("err %d\n", err);
35734 +}
35735 +
35736 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35737 +{
35738 +       int do_dec, wkq_err;
35739 +       struct reinit_br_wh *arg;
35740 +
35741 +       do_dec = 1;
35742 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35743 +               goto out;
35744 +
35745 +       /* ignore ENOMEM */
35746 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35747 +       if (arg) {
35748 +               /*
35749 +                * dec(wh_running), kfree(arg) and dec(br_count)
35750 +                * in reinit function
35751 +                */
35752 +               arg->sb = sb;
35753 +               arg->br = br;
35754 +               au_lcnt_inc(&br->br_count);
35755 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35756 +               if (unlikely(wkq_err)) {
35757 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35758 +                       au_lcnt_dec(&br->br_count);
35759 +                       au_kfree_rcu(arg);
35760 +               }
35761 +               do_dec = 0;
35762 +       }
35763 +
35764 +out:
35765 +       if (do_dec)
35766 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35767 +}
35768 +
35769 +/* ---------------------------------------------------------------------- */
35770 +
35771 +/*
35772 + * create the whiteout @wh.
35773 + */
35774 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35775 +                            struct dentry *wh)
35776 +{
35777 +       int err;
35778 +       struct path h_path = {
35779 +               .dentry = wh
35780 +       };
35781 +       struct au_branch *br;
35782 +       struct au_wbr *wbr;
35783 +       struct dentry *h_parent;
35784 +       struct inode *h_dir, *delegated;
35785 +
35786 +       h_parent = wh->d_parent; /* dir inode is locked */
35787 +       h_dir = d_inode(h_parent);
35788 +       IMustLock(h_dir);
35789 +
35790 +       br = au_sbr(sb, bindex);
35791 +       h_path.mnt = au_br_mnt(br);
35792 +       wbr = br->br_wbr;
35793 +       wbr_wh_read_lock(wbr);
35794 +       if (wbr->wbr_whbase) {
35795 +               delegated = NULL;
35796 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35797 +               if (unlikely(err == -EWOULDBLOCK)) {
35798 +                       pr_warn("cannot retry for NFSv4 delegation"
35799 +                               " for an internal link\n");
35800 +                       iput(delegated);
35801 +               }
35802 +               if (!err || err != -EMLINK)
35803 +                       goto out;
35804 +
35805 +               /* link count full. re-initialize br_whbase. */
35806 +               kick_reinit_br_wh(sb, br);
35807 +       }
35808 +
35809 +       /* return this error in this context */
35810 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35811 +       if (!err)
35812 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35813 +
35814 +out:
35815 +       wbr_wh_read_unlock(wbr);
35816 +       return err;
35817 +}
35818 +
35819 +/* ---------------------------------------------------------------------- */
35820 +
35821 +/*
35822 + * create or remove the diropq.
35823 + */
35824 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35825 +                               unsigned int flags)
35826 +{
35827 +       struct dentry *opq_dentry;
35828 +       struct super_block *sb;
35829 +       struct au_branch *br;
35830 +       struct path h_path;
35831 +       int err;
35832 +
35833 +       sb = dentry->d_sb;
35834 +       br = au_sbr(sb, bindex);
35835 +       h_path.dentry = au_h_dptr(dentry, bindex);
35836 +       h_path.mnt = au_br_mnt(br);
35837 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35838 +       if (IS_ERR(opq_dentry))
35839 +               goto out;
35840 +
35841 +       if (au_ftest_diropq(flags, CREATE)) {
35842 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35843 +               if (!err) {
35844 +                       au_set_dbdiropq(dentry, bindex);
35845 +                       goto out; /* success */
35846 +               }
35847 +       } else {
35848 +               h_path.dentry = opq_dentry;
35849 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35850 +               if (!err)
35851 +                       au_set_dbdiropq(dentry, -1);
35852 +       }
35853 +       dput(opq_dentry);
35854 +       opq_dentry = ERR_PTR(err);
35855 +
35856 +out:
35857 +       return opq_dentry;
35858 +}
35859 +
35860 +struct do_diropq_args {
35861 +       struct dentry **errp;
35862 +       struct dentry *dentry;
35863 +       aufs_bindex_t bindex;
35864 +       unsigned int flags;
35865 +};
35866 +
35867 +static void call_do_diropq(void *args)
35868 +{
35869 +       struct do_diropq_args *a = args;
35870 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35871 +}
35872 +
35873 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35874 +                            unsigned int flags)
35875 +{
35876 +       struct dentry *diropq, *h_dentry;
35877 +       struct mnt_idmap *h_idmap;
35878 +
35879 +       h_idmap = au_sbr_idmap(dentry->d_sb, bindex);
35880 +       h_dentry = au_h_dptr(dentry, bindex);
35881 +       if (!au_test_h_perm_sio(h_idmap, d_inode(h_dentry),
35882 +                               MAY_EXEC | MAY_WRITE))
35883 +               diropq = do_diropq(dentry, bindex, flags);
35884 +       else {
35885 +               int wkq_err;
35886 +               struct do_diropq_args args = {
35887 +                       .errp           = &diropq,
35888 +                       .dentry         = dentry,
35889 +                       .bindex         = bindex,
35890 +                       .flags          = flags
35891 +               };
35892 +
35893 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35894 +               if (unlikely(wkq_err))
35895 +                       diropq = ERR_PTR(wkq_err);
35896 +       }
35897 +
35898 +       return diropq;
35899 +}
35900 +
35901 +/* ---------------------------------------------------------------------- */
35902 +
35903 +/*
35904 + * lookup whiteout dentry.
35905 + * @h_parent: lower parent dentry which must exist and be locked
35906 + * @base_name: name of dentry which will be whiteouted
35907 + * returns dentry for whiteout.
35908 + */
35909 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35910 +                         struct au_branch *br)
35911 +{
35912 +       int err;
35913 +       struct qstr wh_name;
35914 +       struct dentry *wh_dentry;
35915 +       struct path h_path;
35916 +
35917 +       err = au_wh_name_alloc(&wh_name, base_name);
35918 +       wh_dentry = ERR_PTR(err);
35919 +       if (!err) {
35920 +               h_path.dentry = h_parent;
35921 +               h_path.mnt = au_br_mnt(br);
35922 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35923 +               au_kfree_try_rcu(wh_name.name);
35924 +       }
35925 +       return wh_dentry;
35926 +}
35927 +
35928 +/*
35929 + * link/create a whiteout for @dentry on @bindex.
35930 + */
35931 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35932 +                           struct dentry *h_parent)
35933 +{
35934 +       struct dentry *wh_dentry;
35935 +       struct super_block *sb;
35936 +       int err;
35937 +
35938 +       sb = dentry->d_sb;
35939 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35940 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35941 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35942 +               if (!err) {
35943 +                       au_set_dbwh(dentry, bindex);
35944 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35945 +               } else {
35946 +                       dput(wh_dentry);
35947 +                       wh_dentry = ERR_PTR(err);
35948 +               }
35949 +       }
35950 +
35951 +       return wh_dentry;
35952 +}
35953 +
35954 +/* ---------------------------------------------------------------------- */
35955 +
35956 +/* Delete all whiteouts in this directory on branch bindex. */
35957 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35958 +                          aufs_bindex_t bindex)
35959 +{
35960 +       int err;
35961 +       unsigned long ul, n;
35962 +       struct qstr wh_name;
35963 +       char *p;
35964 +       struct hlist_head *head;
35965 +       struct au_vdir_wh *pos;
35966 +       struct au_vdir_destr *str;
35967 +
35968 +       err = -ENOMEM;
35969 +       p = (void *)__get_free_page(GFP_NOFS);
35970 +       wh_name.name = p;
35971 +       if (unlikely(!wh_name.name))
35972 +               goto out;
35973 +
35974 +       err = 0;
35975 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35976 +       p += AUFS_WH_PFX_LEN;
35977 +       n = whlist->nh_num;
35978 +       head = whlist->nh_head;
35979 +       for (ul = 0; !err && ul < n; ul++, head++) {
35980 +               hlist_for_each_entry(pos, head, wh_hash) {
35981 +                       if (pos->wh_bindex != bindex)
35982 +                               continue;
35983 +
35984 +                       str = &pos->wh_str;
35985 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35986 +                               memcpy(p, str->name, str->len);
35987 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35988 +                               err = unlink_wh_name(h_path, &wh_name);
35989 +                               if (!err)
35990 +                                       continue;
35991 +                               break;
35992 +                       }
35993 +                       AuIOErr("whiteout name too long %.*s\n",
35994 +                               str->len, str->name);
35995 +                       err = -EIO;
35996 +                       break;
35997 +               }
35998 +       }
35999 +       free_page((unsigned long)wh_name.name);
36000 +
36001 +out:
36002 +       return err;
36003 +}
36004 +
36005 +struct del_wh_children_args {
36006 +       int *errp;
36007 +       struct path *h_path;
36008 +       struct au_nhash *whlist;
36009 +       aufs_bindex_t bindex;
36010 +};
36011 +
36012 +static void call_del_wh_children(void *args)
36013 +{
36014 +       struct del_wh_children_args *a = args;
36015 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
36016 +}
36017 +
36018 +/* ---------------------------------------------------------------------- */
36019 +
36020 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
36021 +{
36022 +       struct au_whtmp_rmdir *whtmp;
36023 +       int err;
36024 +       unsigned int rdhash;
36025 +
36026 +       SiMustAnyLock(sb);
36027 +
36028 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
36029 +       if (unlikely(!whtmp)) {
36030 +               whtmp = ERR_PTR(-ENOMEM);
36031 +               goto out;
36032 +       }
36033 +
36034 +       /* no estimation for dir size */
36035 +       rdhash = au_sbi(sb)->si_rdhash;
36036 +       if (!rdhash)
36037 +               rdhash = AUFS_RDHASH_DEF;
36038 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
36039 +       if (unlikely(err)) {
36040 +               au_kfree_rcu(whtmp);
36041 +               whtmp = ERR_PTR(err);
36042 +       }
36043 +
36044 +out:
36045 +       return whtmp;
36046 +}
36047 +
36048 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
36049 +{
36050 +       if (whtmp->br)
36051 +               au_lcnt_dec(&whtmp->br->br_count);
36052 +       dput(whtmp->wh_dentry);
36053 +       iput(whtmp->dir);
36054 +       au_nhash_wh_free(&whtmp->whlist);
36055 +       au_kfree_rcu(whtmp);
36056 +}
36057 +
36058 +/*
36059 + * rmdir the whiteouted temporary named dir @h_dentry.
36060 + * @whlist: whiteouted children.
36061 + */
36062 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36063 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
36064 +{
36065 +       int err;
36066 +       unsigned int h_nlink;
36067 +       struct path wh_path;
36068 +       struct inode *wh_inode, *h_dir;
36069 +       struct au_branch *br;
36070 +       struct mnt_idmap *h_idmap;
36071 +
36072 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
36073 +       IMustLock(h_dir);
36074 +
36075 +       br = au_sbr(dir->i_sb, bindex);
36076 +       wh_path.dentry = wh_dentry;
36077 +       wh_path.mnt = au_br_mnt(br);
36078 +       h_idmap = au_br_idmap(br);
36079 +       wh_inode = d_inode(wh_dentry);
36080 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
36081 +
36082 +       /*
36083 +        * someone else might change some whiteouts while we were sleeping.
36084 +        * it means this whlist may have an obsoleted entry.
36085 +        */
36086 +       if (!au_test_h_perm_sio(h_idmap, wh_inode, MAY_EXEC | MAY_WRITE))
36087 +               err = del_wh_children(&wh_path, whlist, bindex);
36088 +       else {
36089 +               int wkq_err;
36090 +               struct del_wh_children_args args = {
36091 +                       .errp           = &err,
36092 +                       .h_path         = &wh_path,
36093 +                       .whlist         = whlist,
36094 +                       .bindex         = bindex
36095 +               };
36096 +
36097 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
36098 +               if (unlikely(wkq_err))
36099 +                       err = wkq_err;
36100 +       }
36101 +       inode_unlock(wh_inode);
36102 +
36103 +       if (!err) {
36104 +               h_nlink = h_dir->i_nlink;
36105 +               err = vfsub_rmdir(h_dir, &wh_path);
36106 +               /* some fs doesn't change the parent nlink in some cases */
36107 +               h_nlink -= h_dir->i_nlink;
36108 +       }
36109 +
36110 +       if (!err) {
36111 +               if (au_ibtop(dir) == bindex) {
36112 +                       /* todo: dir->i_mutex is necessary */
36113 +                       au_cpup_attr_timesizes(dir);
36114 +                       if (h_nlink)
36115 +                               vfsub_drop_nlink(dir);
36116 +               }
36117 +               return 0; /* success */
36118 +       }
36119 +
36120 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
36121 +       return err;
36122 +}
36123 +
36124 +static void call_rmdir_whtmp(void *args)
36125 +{
36126 +       int err;
36127 +       aufs_bindex_t bindex;
36128 +       struct au_whtmp_rmdir *a = args;
36129 +       struct super_block *sb;
36130 +       struct dentry *h_parent;
36131 +       struct inode *h_dir;
36132 +       struct au_hinode *hdir;
36133 +
36134 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
36135 +       /* inode_lock(a->dir); */
36136 +       err = -EROFS;
36137 +       sb = a->dir->i_sb;
36138 +       si_read_lock(sb, !AuLock_FLUSH);
36139 +       if (!au_br_writable(a->br->br_perm))
36140 +               goto out;
36141 +       bindex = au_br_index(sb, a->br->br_id);
36142 +       if (unlikely(bindex < 0))
36143 +               goto out;
36144 +
36145 +       err = -EIO;
36146 +       ii_write_lock_parent(a->dir);
36147 +       h_parent = dget_parent(a->wh_dentry);
36148 +       h_dir = d_inode(h_parent);
36149 +       hdir = au_hi(a->dir, bindex);
36150 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
36151 +       if (unlikely(err))
36152 +               goto out_mnt;
36153 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
36154 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36155 +                         a->br);
36156 +       if (!err)
36157 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
36158 +       au_hn_inode_unlock(hdir);
36159 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
36160 +
36161 +out_mnt:
36162 +       dput(h_parent);
36163 +       ii_write_unlock(a->dir);
36164 +out:
36165 +       /* inode_unlock(a->dir); */
36166 +       au_whtmp_rmdir_free(a);
36167 +       si_read_unlock(sb);
36168 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36169 +       if (unlikely(err))
36170 +               AuIOErr("err %d\n", err);
36171 +}
36172 +
36173 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36174 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36175 +{
36176 +       int wkq_err;
36177 +       struct super_block *sb;
36178 +
36179 +       IMustLock(dir);
36180 +
36181 +       /* all post-process will be done in do_rmdir_whtmp(). */
36182 +       sb = dir->i_sb;
36183 +       args->dir = au_igrab(dir);
36184 +       args->br = au_sbr(sb, bindex);
36185 +       au_lcnt_inc(&args->br->br_count);
36186 +       args->wh_dentry = dget(wh_dentry);
36187 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
36188 +       if (unlikely(wkq_err)) {
36189 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
36190 +               au_whtmp_rmdir_free(args);
36191 +       }
36192 +}
36193 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
36194 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
36195 +++ linux/fs/aufs/whout.h       2023-10-31 09:31:04.199880750 +0100
36196 @@ -0,0 +1,87 @@
36197 +/* SPDX-License-Identifier: GPL-2.0 */
36198 +/*
36199 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36200 + *
36201 + * This program is free software; you can redistribute it and/or modify
36202 + * it under the terms of the GNU General Public License as published by
36203 + * the Free Software Foundation; either version 2 of the License, or
36204 + * (at your option) any later version.
36205 + *
36206 + * This program is distributed in the hope that it will be useful,
36207 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36208 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36209 + * GNU General Public License for more details.
36210 + *
36211 + * You should have received a copy of the GNU General Public License
36212 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36213 + */
36214 +
36215 +/*
36216 + * whiteout for logical deletion and opaque directory
36217 + */
36218 +
36219 +#ifndef __AUFS_WHOUT_H__
36220 +#define __AUFS_WHOUT_H__
36221 +
36222 +#ifdef __KERNEL__
36223 +
36224 +#include "dir.h"
36225 +
36226 +/* whout.c */
36227 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36228 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
36229 +              struct qstr *wh_name, int try_sio);
36230 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path);
36231 +struct au_branch;
36232 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36233 +                            struct qstr *prefix);
36234 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36235 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36236 +                       struct dentry *dentry);
36237 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36238 +
36239 +/* diropq flags */
36240 +#define AuDiropq_CREATE        1
36241 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36242 +#define au_fset_diropq(flags, name) \
36243 +       do { (flags) |= AuDiropq_##name; } while (0)
36244 +#define au_fclr_diropq(flags, name) \
36245 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36246 +
36247 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36248 +                            unsigned int flags);
36249 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36250 +                         struct au_branch *br);
36251 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36252 +                           struct dentry *h_parent);
36253 +
36254 +/* real rmdir for the whiteout-ed dir */
36255 +struct au_whtmp_rmdir {
36256 +       struct inode *dir;
36257 +       struct au_branch *br;
36258 +       struct dentry *wh_dentry;
36259 +       struct au_nhash whlist;
36260 +};
36261 +
36262 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36263 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36264 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36265 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36266 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36267 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36268 +
36269 +/* ---------------------------------------------------------------------- */
36270 +
36271 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36272 +                                             aufs_bindex_t bindex)
36273 +{
36274 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36275 +}
36276 +
36277 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36278 +{
36279 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36280 +}
36281 +
36282 +#endif /* __KERNEL__ */
36283 +#endif /* __AUFS_WHOUT_H__ */
36284 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36285 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36286 +++ linux/fs/aufs/wkq.c 2022-11-05 23:02:18.972555950 +0100
36287 @@ -0,0 +1,372 @@
36288 +// SPDX-License-Identifier: GPL-2.0
36289 +/*
36290 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36291 + *
36292 + * This program is free software; you can redistribute it and/or modify
36293 + * it under the terms of the GNU General Public License as published by
36294 + * the Free Software Foundation; either version 2 of the License, or
36295 + * (at your option) any later version.
36296 + *
36297 + * This program is distributed in the hope that it will be useful,
36298 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36299 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36300 + * GNU General Public License for more details.
36301 + *
36302 + * You should have received a copy of the GNU General Public License
36303 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36304 + */
36305 +
36306 +/*
36307 + * workqueue for asynchronous/super-io operations
36308 + * todo: try new credential scheme
36309 + */
36310 +
36311 +#include <linux/module.h>
36312 +#include "aufs.h"
36313 +
36314 +/* internal workqueue named AUFS_WKQ_NAME */
36315 +
36316 +static struct workqueue_struct *au_wkq;
36317 +
36318 +struct au_wkinfo {
36319 +       struct work_struct wk;
36320 +       struct kobject *kobj;
36321 +
36322 +       unsigned int flags; /* see wkq.h */
36323 +
36324 +       au_wkq_func_t func;
36325 +       void *args;
36326 +
36327 +#ifdef CONFIG_LOCKDEP
36328 +       int dont_check;
36329 +       struct held_lock **hlock;
36330 +#endif
36331 +
36332 +       struct completion *comp;
36333 +};
36334 +
36335 +/* ---------------------------------------------------------------------- */
36336 +/*
36337 + * Aufs passes some operations to the workqueue such as the internal copyup.
36338 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36339 + * job run by workqueue depends upon the locks acquired in the other task.
36340 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36341 + * information too. And the job in the workqueue restores the info in order to
36342 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36343 + * correctly and expectedly.
36344 + */
36345 +
36346 +#ifndef CONFIG_LOCKDEP
36347 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36348 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36349 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36350 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36351 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36352 +#else
36353 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36354 +{
36355 +       wkinfo->hlock = NULL;
36356 +       wkinfo->dont_check = 0;
36357 +}
36358 +
36359 +/*
36360 + * 1: matched
36361 + * 0: unmatched
36362 + */
36363 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36364 +{
36365 +       static DEFINE_SPINLOCK(spin);
36366 +       static struct {
36367 +               char *name;
36368 +               struct lock_class_key *key;
36369 +       } a[] = {
36370 +               { .name = "&sbinfo->si_rwsem" },
36371 +               { .name = "&finfo->fi_rwsem" },
36372 +               { .name = "&dinfo->di_rwsem" },
36373 +               { .name = "&iinfo->ii_rwsem" }
36374 +       };
36375 +       static int set;
36376 +       int i;
36377 +
36378 +       /* lockless read from 'set.' see below */
36379 +       if (set == ARRAY_SIZE(a)) {
36380 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36381 +                       if (a[i].key == key)
36382 +                               goto match;
36383 +               goto unmatch;
36384 +       }
36385 +
36386 +       spin_lock(&spin);
36387 +       if (set)
36388 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36389 +                       if (a[i].key == key) {
36390 +                               spin_unlock(&spin);
36391 +                               goto match;
36392 +                       }
36393 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36394 +               if (a[i].key) {
36395 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36396 +                               spin_unlock(&spin);
36397 +                               goto match;
36398 +                       } else
36399 +                               continue;
36400 +               }
36401 +               if (strstr(a[i].name, name)) {
36402 +                       /*
36403 +                        * the order of these three lines is important for the
36404 +                        * lockless read above.
36405 +                        */
36406 +                       a[i].key = key;
36407 +                       spin_unlock(&spin);
36408 +                       set++;
36409 +                       /* AuDbg("%d, %s\n", set, name); */
36410 +                       goto match;
36411 +               }
36412 +       }
36413 +       spin_unlock(&spin);
36414 +       goto unmatch;
36415 +
36416 +match:
36417 +       return 1;
36418 +unmatch:
36419 +       return 0;
36420 +}
36421 +
36422 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36423 +{
36424 +       int err, n;
36425 +       struct task_struct *curr;
36426 +       struct held_lock **hl, *held_locks, *p;
36427 +
36428 +       err = 0;
36429 +       curr = current;
36430 +       wkinfo->dont_check = lockdep_recursing(curr);
36431 +       if (wkinfo->dont_check)
36432 +               goto out;
36433 +       n = curr->lockdep_depth;
36434 +       if (!n)
36435 +               goto out;
36436 +
36437 +       err = -ENOMEM;
36438 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36439 +       if (unlikely(!wkinfo->hlock))
36440 +               goto out;
36441 +
36442 +       err = 0;
36443 +#if 0 /* left for debugging */
36444 +       if (0 && au_debug_test())
36445 +               lockdep_print_held_locks(curr);
36446 +#endif
36447 +       held_locks = curr->held_locks;
36448 +       hl = wkinfo->hlock;
36449 +       while (n--) {
36450 +               p = held_locks++;
36451 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36452 +                       *hl++ = p;
36453 +       }
36454 +       *hl = NULL;
36455 +
36456 +out:
36457 +       return err;
36458 +}
36459 +
36460 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36461 +{
36462 +       au_kfree_try_rcu(wkinfo->hlock);
36463 +}
36464 +
36465 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36466 +{
36467 +       struct held_lock *p, **hl = wkinfo->hlock;
36468 +       int subclass;
36469 +
36470 +       if (wkinfo->dont_check)
36471 +               lockdep_off();
36472 +       if (!hl)
36473 +               return;
36474 +       while ((p = *hl++)) { /* assignment */
36475 +               subclass = lockdep_hlock_class(p)->subclass;
36476 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36477 +               if (p->read)
36478 +                       rwsem_acquire_read(p->instance, subclass, 0,
36479 +                                          /*p->acquire_ip*/_RET_IP_);
36480 +               else
36481 +                       rwsem_acquire(p->instance, subclass, 0,
36482 +                                     /*p->acquire_ip*/_RET_IP_);
36483 +       }
36484 +}
36485 +
36486 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36487 +{
36488 +       struct held_lock *p, **hl = wkinfo->hlock;
36489 +
36490 +       if (wkinfo->dont_check)
36491 +               lockdep_on();
36492 +       if (!hl)
36493 +               return;
36494 +       while ((p = *hl++)) /* assignment */
36495 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36496 +}
36497 +#endif
36498 +
36499 +static void wkq_func(struct work_struct *wk)
36500 +{
36501 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36502 +
36503 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36504 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36505 +
36506 +       au_wkq_lockdep_pre(wkinfo);
36507 +       wkinfo->func(wkinfo->args);
36508 +       au_wkq_lockdep_post(wkinfo);
36509 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36510 +               complete(wkinfo->comp);
36511 +       else {
36512 +               kobject_put(wkinfo->kobj);
36513 +               module_put(THIS_MODULE); /* todo: ?? */
36514 +               au_kfree_rcu(wkinfo);
36515 +       }
36516 +}
36517 +
36518 +/*
36519 + * Since struct completion is large, try allocating it dynamically.
36520 + */
36521 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36522 +
36523 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36524 +{
36525 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36526 +       if (*comp) {
36527 +               init_completion(*comp);
36528 +               wkinfo->comp = *comp;
36529 +               return 0;
36530 +       }
36531 +       return -ENOMEM;
36532 +}
36533 +
36534 +static void au_wkq_comp_free(struct completion *comp)
36535 +{
36536 +       au_kfree_rcu(comp);
36537 +}
36538 +
36539 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36540 +{
36541 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36542 +               if (au_wkq_test()) {
36543 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36544 +                               " due to a dead dir by UDBA,"
36545 +                               " or async xino write?\n");
36546 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36547 +               }
36548 +       } else
36549 +               au_dbg_verify_kthread();
36550 +
36551 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36552 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36553 +               queue_work(au_wkq, &wkinfo->wk);
36554 +       } else {
36555 +               INIT_WORK(&wkinfo->wk, wkq_func);
36556 +               schedule_work(&wkinfo->wk);
36557 +       }
36558 +}
36559 +
36560 +/*
36561 + * Be careful. It is easy to make deadlock happen.
36562 + * processA: lock, wkq and wait
36563 + * processB: wkq and wait, lock in wkq
36564 + * --> deadlock
36565 + */
36566 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36567 +{
36568 +       int err;
36569 +       AuWkqCompDeclare(comp);
36570 +       struct au_wkinfo wkinfo = {
36571 +               .flags  = flags,
36572 +               .func   = func,
36573 +               .args   = args
36574 +       };
36575 +
36576 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36577 +       if (unlikely(err))
36578 +               goto out;
36579 +       err = au_wkq_lockdep_alloc(&wkinfo);
36580 +       if (unlikely(err))
36581 +               goto out_comp;
36582 +       if (!err) {
36583 +               au_wkq_run(&wkinfo);
36584 +               /* no timeout, no interrupt */
36585 +               wait_for_completion(wkinfo.comp);
36586 +       }
36587 +       au_wkq_lockdep_free(&wkinfo);
36588 +
36589 +out_comp:
36590 +       au_wkq_comp_free(comp);
36591 +out:
36592 +       destroy_work_on_stack(&wkinfo.wk);
36593 +       return err;
36594 +}
36595 +
36596 +/*
36597 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36598 + * problem in a concurrent umounting.
36599 + */
36600 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36601 +                 unsigned int flags)
36602 +{
36603 +       int err;
36604 +       struct au_wkinfo *wkinfo;
36605 +
36606 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36607 +
36608 +       /*
36609 +        * wkq_func() must free this wkinfo.
36610 +        * it highly depends upon the implementation of workqueue.
36611 +        */
36612 +       err = 0;
36613 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36614 +       if (wkinfo) {
36615 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36616 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36617 +               wkinfo->func = func;
36618 +               wkinfo->args = args;
36619 +               wkinfo->comp = NULL;
36620 +               au_wkq_lockdep_init(wkinfo);
36621 +               kobject_get(wkinfo->kobj);
36622 +               __module_get(THIS_MODULE); /* todo: ?? */
36623 +
36624 +               au_wkq_run(wkinfo);
36625 +       } else {
36626 +               err = -ENOMEM;
36627 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36628 +       }
36629 +
36630 +       return err;
36631 +}
36632 +
36633 +/* ---------------------------------------------------------------------- */
36634 +
36635 +void au_nwt_init(struct au_nowait_tasks *nwt)
36636 +{
36637 +       atomic_set(&nwt->nw_len, 0);
36638 +       /* smp_mb(); */ /* atomic_set */
36639 +       init_waitqueue_head(&nwt->nw_wq);
36640 +}
36641 +
36642 +void au_wkq_fin(void)
36643 +{
36644 +       destroy_workqueue(au_wkq);
36645 +}
36646 +
36647 +int __init au_wkq_init(void)
36648 +{
36649 +       int err;
36650 +
36651 +       err = 0;
36652 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36653 +       if (IS_ERR(au_wkq))
36654 +               err = PTR_ERR(au_wkq);
36655 +       else if (!au_wkq)
36656 +               err = -ENOMEM;
36657 +
36658 +       return err;
36659 +}
36660 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36661 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36662 +++ linux/fs/aufs/wkq.h 2022-11-05 23:02:18.972555950 +0100
36663 @@ -0,0 +1,89 @@
36664 +/* SPDX-License-Identifier: GPL-2.0 */
36665 +/*
36666 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36667 + *
36668 + * This program is free software; you can redistribute it and/or modify
36669 + * it under the terms of the GNU General Public License as published by
36670 + * the Free Software Foundation; either version 2 of the License, or
36671 + * (at your option) any later version.
36672 + *
36673 + * This program is distributed in the hope that it will be useful,
36674 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36675 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36676 + * GNU General Public License for more details.
36677 + *
36678 + * You should have received a copy of the GNU General Public License
36679 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36680 + */
36681 +
36682 +/*
36683 + * workqueue for asynchronous/super-io operations
36684 + * todo: try new credentials management scheme
36685 + */
36686 +
36687 +#ifndef __AUFS_WKQ_H__
36688 +#define __AUFS_WKQ_H__
36689 +
36690 +#ifdef __KERNEL__
36691 +
36692 +#include <linux/wait.h>
36693 +
36694 +struct super_block;
36695 +
36696 +/* ---------------------------------------------------------------------- */
36697 +
36698 +/*
36699 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36700 + */
36701 +struct au_nowait_tasks {
36702 +       atomic_t                nw_len;
36703 +       wait_queue_head_t       nw_wq;
36704 +};
36705 +
36706 +/* ---------------------------------------------------------------------- */
36707 +
36708 +typedef void (*au_wkq_func_t)(void *args);
36709 +
36710 +/* wkq flags */
36711 +#define AuWkq_WAIT     1
36712 +#define AuWkq_NEST     (1 << 1)
36713 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36714 +#define au_fset_wkq(flags, name) \
36715 +       do { (flags) |= AuWkq_##name; } while (0)
36716 +#define au_fclr_wkq(flags, name) \
36717 +       do { (flags) &= ~AuWkq_##name; } while (0)
36718 +
36719 +/* wkq.c */
36720 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36721 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36722 +                 unsigned int flags);
36723 +void au_nwt_init(struct au_nowait_tasks *nwt);
36724 +int __init au_wkq_init(void);
36725 +void au_wkq_fin(void);
36726 +
36727 +/* ---------------------------------------------------------------------- */
36728 +
36729 +static inline int au_wkq_test(void)
36730 +{
36731 +       return current->flags & PF_WQ_WORKER;
36732 +}
36733 +
36734 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36735 +{
36736 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36737 +}
36738 +
36739 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36740 +{
36741 +       if (atomic_dec_and_test(&nwt->nw_len))
36742 +               wake_up_all(&nwt->nw_wq);
36743 +}
36744 +
36745 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36746 +{
36747 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36748 +       return 0;
36749 +}
36750 +
36751 +#endif /* __KERNEL__ */
36752 +#endif /* __AUFS_WKQ_H__ */
36753 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36754 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36755 +++ linux/fs/aufs/xattr.c       2023-10-31 09:31:04.203214083 +0100
36756 @@ -0,0 +1,360 @@
36757 +// SPDX-License-Identifier: GPL-2.0
36758 +/*
36759 + * Copyright (C) 2014-2022 Junjiro R. Okajima
36760 + *
36761 + * This program is free software; you can redistribute it and/or modify
36762 + * it under the terms of the GNU General Public License as published by
36763 + * the Free Software Foundation; either version 2 of the License, or
36764 + * (at your option) any later version.
36765 + *
36766 + * This program is distributed in the hope that it will be useful,
36767 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36768 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36769 + * GNU General Public License for more details.
36770 + *
36771 + * You should have received a copy of the GNU General Public License
36772 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36773 + */
36774 +
36775 +/*
36776 + * handling xattr functions
36777 + */
36778 +
36779 +#include <linux/fs.h>
36780 +#include <linux/xattr.h>
36781 +#include "aufs.h"
36782 +
36783 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36784 +{
36785 +       if (!ignore_flags)
36786 +               goto out;
36787 +       switch (err) {
36788 +       case -ENOMEM:
36789 +       case -EDQUOT:
36790 +               goto out;
36791 +       }
36792 +
36793 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36794 +               err = 0;
36795 +               goto out;
36796 +       }
36797 +
36798 +#define cmp(brattr, prefix) do {                                       \
36799 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36800 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36801 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36802 +                               err = 0;                                \
36803 +                       goto out;                                       \
36804 +               }                                                       \
36805 +       } while (0)
36806 +
36807 +       cmp(SEC, SECURITY);
36808 +       cmp(SYS, SYSTEM);
36809 +       cmp(TR, TRUSTED);
36810 +       cmp(USR, USER);
36811 +#undef cmp
36812 +
36813 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36814 +               err = 0;
36815 +
36816 +out:
36817 +       return err;
36818 +}
36819 +
36820 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36821 +
36822 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36823 +                           char *name, char **buf, unsigned int ignore_flags,
36824 +                           unsigned int verbose)
36825 +{
36826 +       int err, is_acl;
36827 +       ssize_t ssz;
36828 +       struct inode *h_idst;
36829 +       struct dentry *h_dst_dentry, *h_src_dentry;
36830 +       struct mnt_idmap *h_dst_idmap, *h_src_idmap;
36831 +       struct posix_acl *acl;
36832 +
36833 +       is_acl = !!is_posix_acl_xattr(name);
36834 +       h_src_idmap = mnt_idmap(h_src->mnt);
36835 +       h_src_dentry = h_src->dentry;
36836 +       if (is_acl) {
36837 +               acl = vfs_get_acl(h_src_idmap, h_src_dentry, name);
36838 +               AuDebugOn(!acl);
36839 +               if (unlikely(IS_ERR(acl))) {
36840 +                       err = PTR_ERR(acl);
36841 +                       if (err == -ENODATA)
36842 +                               err = 0;
36843 +                       else if (err == -EOPNOTSUPP
36844 +                                && au_test_nfs_noacl(d_inode(h_src_dentry)))
36845 +                               err = 0;
36846 +                       else if (verbose || au_debug_test())
36847 +                               pr_err("%s, err %d\n", name, err);
36848 +                       goto out;
36849 +               }
36850 +       } else {
36851 +               ssz = vfs_getxattr_alloc(h_src_idmap, h_src_dentry, name, buf,
36852 +                                        0, GFP_NOFS);
36853 +               if (unlikely(ssz <= 0)) {
36854 +                       err = ssz;
36855 +                       if (err == -ENODATA)
36856 +                               err = 0;
36857 +                       else if (err == -EOPNOTSUPP
36858 +                                && (ignore_flags & au_xattr_out_of_list))
36859 +                                err = 0;
36860 +                       else if (err && (verbose || au_debug_test()))
36861 +                               pr_err("%s, err %d\n", name, err);
36862 +                       goto out;
36863 +               }
36864 +       }
36865 +
36866 +       /* unlock it temporary */
36867 +       h_dst_idmap = mnt_idmap(h_dst->mnt);
36868 +       h_dst_dentry = h_dst->dentry;
36869 +       h_idst = d_inode(h_dst_dentry);
36870 +       inode_unlock(h_idst);
36871 +       if (is_acl) {
36872 +               err = vfsub_set_acl(h_dst_idmap, h_dst_dentry, name, acl);
36873 +               posix_acl_release(acl);
36874 +       } else
36875 +               err = vfsub_setxattr(h_dst_idmap, h_dst_dentry, name, *buf,
36876 +                                    ssz, /*flags*/0);
36877 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36878 +       if (unlikely(err)) {
36879 +               if (verbose || au_debug_test())
36880 +                       pr_err("%s, err %d\n", name, err);
36881 +               err = au_xattr_ignore(err, name, ignore_flags);
36882 +       }
36883 +
36884 +out:
36885 +       return err;
36886 +}
36887 +
36888 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36889 +                 unsigned int verbose)
36890 +{
36891 +       int err, unlocked;
36892 +       ssize_t ssz;
36893 +       struct dentry *h_dst_dentry, *h_src_dentry;
36894 +       struct inode *h_isrc, *h_idst;
36895 +       char *value, *p, *o, *e;
36896 +
36897 +       /* try stopping to update the source inode while we are referencing */
36898 +       /* there should not be the parent-child relationship between them */
36899 +       h_dst_dentry = h_dst->dentry;
36900 +       h_idst = d_inode(h_dst_dentry);
36901 +       h_src_dentry = h_src->dentry;
36902 +       h_isrc = d_inode(h_src_dentry);
36903 +       inode_unlock(h_idst);
36904 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36905 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36906 +       unlocked = 0;
36907 +
36908 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36909 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36910 +       err = ssz;
36911 +       if (unlikely(err < 0)) {
36912 +               AuTraceErr(err);
36913 +               if (err == -ENODATA
36914 +                   || err == -EOPNOTSUPP)
36915 +                       err = 0;        /* ignore */
36916 +               goto out;
36917 +       }
36918 +
36919 +       err = 0;
36920 +       p = NULL;
36921 +       o = NULL;
36922 +       if (ssz) {
36923 +               err = -ENOMEM;
36924 +               p = kmalloc(ssz, GFP_NOFS);
36925 +               o = p;
36926 +               if (unlikely(!p))
36927 +                       goto out;
36928 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36929 +       }
36930 +       inode_unlock_shared(h_isrc);
36931 +       unlocked = 1;
36932 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36933 +       if (unlikely(err < 0))
36934 +               goto out_free;
36935 +
36936 +       err = 0;
36937 +       e = p + ssz;
36938 +       value = NULL;
36939 +       ignore_flags |= au_xattr_out_of_list;
36940 +       while (!err && p < e) {
36941 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36942 +                                      verbose);
36943 +               p += strlen(p) + 1;
36944 +       }
36945 +       au_kfree_try_rcu(value);
36946 +
36947 +out_free:
36948 +       au_kfree_try_rcu(o);
36949 +out:
36950 +       if (!unlocked)
36951 +               inode_unlock_shared(h_isrc);
36952 +       AuTraceErr(err);
36953 +       return err;
36954 +}
36955 +
36956 +/* ---------------------------------------------------------------------- */
36957 +
36958 +static int au_smack_reentering(struct super_block *sb)
36959 +{
36960 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36961 +       /*
36962 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36963 +        * i_op->getxattr(). ouch.
36964 +        */
36965 +       return si_pid_test(sb);
36966 +#else
36967 +       return 0;
36968 +#endif
36969 +}
36970 +
36971 +enum {
36972 +       AU_XATTR_LIST,
36973 +       AU_XATTR_GET
36974 +};
36975 +
36976 +struct au_lgxattr {
36977 +       int type;
36978 +       union {
36979 +               struct {
36980 +                       char    *list;
36981 +                       size_t  size;
36982 +               } list;
36983 +               struct {
36984 +                       const char      *name;
36985 +                       void            *value;
36986 +                       size_t          size;
36987 +               } get;
36988 +       } u;
36989 +};
36990 +
36991 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36992 +                         struct au_lgxattr *arg)
36993 +{
36994 +       ssize_t err;
36995 +       int reenter;
36996 +       struct path h_path;
36997 +       struct super_block *sb;
36998 +
36999 +       sb = dentry->d_sb;
37000 +       reenter = au_smack_reentering(sb);
37001 +       if (!reenter) {
37002 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
37003 +               if (unlikely(err))
37004 +                       goto out;
37005 +       }
37006 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
37007 +       if (unlikely(err))
37008 +               goto out_si;
37009 +       if (unlikely(!h_path.dentry))
37010 +               /* illegally overlapped or something */
37011 +               goto out_di; /* pretending success */
37012 +
37013 +       /* always topmost entry only */
37014 +       switch (arg->type) {
37015 +       case AU_XATTR_LIST:
37016 +               err = vfs_listxattr(h_path.dentry,
37017 +                                   arg->u.list.list, arg->u.list.size);
37018 +               break;
37019 +       case AU_XATTR_GET:
37020 +               AuDebugOn(d_is_negative(h_path.dentry));
37021 +               err = vfs_getxattr(mnt_idmap(h_path.mnt), h_path.dentry,
37022 +                                  arg->u.get.name, arg->u.get.value,
37023 +                                  arg->u.get.size);
37024 +               break;
37025 +       }
37026 +
37027 +out_di:
37028 +       if (!reenter)
37029 +               di_read_unlock(dentry, AuLock_IR);
37030 +out_si:
37031 +       if (!reenter)
37032 +               si_read_unlock(sb);
37033 +out:
37034 +       AuTraceErr(err);
37035 +       return err;
37036 +}
37037 +
37038 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
37039 +{
37040 +       struct au_lgxattr arg = {
37041 +               .type = AU_XATTR_LIST,
37042 +               .u.list = {
37043 +                       .list   = list,
37044 +                       .size   = size
37045 +               },
37046 +       };
37047 +
37048 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
37049 +}
37050 +
37051 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
37052 +                          const char *name, void *value, size_t size)
37053 +{
37054 +       struct au_lgxattr arg = {
37055 +               .type = AU_XATTR_GET,
37056 +               .u.get = {
37057 +                       .name   = name,
37058 +                       .value  = value,
37059 +                       .size   = size
37060 +               },
37061 +       };
37062 +
37063 +       return au_lgxattr(dentry, inode, &arg);
37064 +}
37065 +
37066 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
37067 +                      const char *name, const void *value, size_t size,
37068 +                      int flags)
37069 +{
37070 +       struct au_sxattr arg = {
37071 +               .type = AU_XATTR_SET,
37072 +               .u.set = {
37073 +                       .name   = name,
37074 +                       .value  = value,
37075 +                       .size   = size,
37076 +                       .flags  = flags
37077 +               },
37078 +       };
37079 +
37080 +       return au_sxattr(dentry, inode, &arg);
37081 +}
37082 +
37083 +/* ---------------------------------------------------------------------- */
37084 +
37085 +static int au_xattr_get(const struct xattr_handler *handler,
37086 +                       struct dentry *dentry, struct inode *inode,
37087 +                       const char *name, void *buffer, size_t size)
37088 +{
37089 +       return au_getxattr(dentry, inode, name, buffer, size);
37090 +}
37091 +
37092 +static int au_xattr_set(const struct xattr_handler *handler,
37093 +                       struct mnt_idmap *idmap,
37094 +                       struct dentry *dentry, struct inode *inode,
37095 +                       const char *name, const void *value, size_t size,
37096 +                       int flags)
37097 +{
37098 +       return au_setxattr(dentry, inode, name, value, size, flags);
37099 +}
37100 +
37101 +static const struct xattr_handler au_xattr_handler = {
37102 +       .name   = "",
37103 +       .prefix = "",
37104 +       .get    = au_xattr_get,
37105 +       .set    = au_xattr_set
37106 +};
37107 +
37108 +static const struct xattr_handler *au_xattr_handlers[] = {
37109 +       &au_xattr_handler, /* must be last */
37110 +       NULL
37111 +};
37112 +
37113 +void au_xattr_init(struct super_block *sb)
37114 +{
37115 +       sb->s_xattr = au_xattr_handlers;
37116 +}
37117 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
37118 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
37119 +++ linux/fs/aufs/xino.c        2023-10-31 09:31:04.203214083 +0100
37120 @@ -0,0 +1,1926 @@
37121 +// SPDX-License-Identifier: GPL-2.0
37122 +/*
37123 + * Copyright (C) 2005-2022 Junjiro R. Okajima
37124 + *
37125 + * This program is free software; you can redistribute it and/or modify
37126 + * it under the terms of the GNU General Public License as published by
37127 + * the Free Software Foundation; either version 2 of the License, or
37128 + * (at your option) any later version.
37129 + *
37130 + * This program is distributed in the hope that it will be useful,
37131 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37132 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37133 + * GNU General Public License for more details.
37134 + *
37135 + * You should have received a copy of the GNU General Public License
37136 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37137 + */
37138 +
37139 +/*
37140 + * external inode number translation table and bitmap
37141 + *
37142 + * things to consider
37143 + * - the lifetime
37144 + *   + au_xino object
37145 + *   + XINO files (xino, xib, xigen)
37146 + *   + dynamic debugfs entries (xiN)
37147 + *   + static debugfs entries (xib, xigen)
37148 + *   + static sysfs entry (xi_path)
37149 + * - several entry points to handle them.
37150 + *   + mount(2) without xino option (default)
37151 + *   + mount(2) with xino option
37152 + *   + mount(2) with noxino option
37153 + *   + umount(2)
37154 + *   + remount with add/del branches
37155 + *   + remount with xino/noxino options
37156 + */
37157 +
37158 +#include <linux/seq_file.h>
37159 +#include <linux/statfs.h>
37160 +#include "aufs.h"
37161 +
37162 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37163 +                                    aufs_bindex_t bbot,
37164 +                                    struct super_block *h_sb)
37165 +{
37166 +       /* todo: try binary-search if the branches are many */
37167 +       for (; btop <= bbot; btop++)
37168 +               if (h_sb == au_sbr_sb(sb, btop))
37169 +                       return btop;
37170 +       return -1;
37171 +}
37172 +
37173 +/*
37174 + * find another branch who is on the same filesystem of the specified
37175 + * branch{@btgt}. search until @bbot.
37176 + */
37177 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37178 +                                 aufs_bindex_t bbot)
37179 +{
37180 +       aufs_bindex_t bindex;
37181 +       struct super_block *tgt_sb;
37182 +
37183 +       tgt_sb = au_sbr_sb(sb, btgt);
37184 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37185 +       if (bindex < 0)
37186 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
37187 +
37188 +       return bindex;
37189 +}
37190 +
37191 +/* ---------------------------------------------------------------------- */
37192 +
37193 +/*
37194 + * stop unnecessary notify events at creating xino files
37195 + */
37196 +
37197 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37198 +{
37199 +       aufs_bindex_t bfound, bindex, bbot;
37200 +       struct dentry *parent;
37201 +       struct au_branch *br;
37202 +
37203 +       bfound = -1;
37204 +       parent = dentry->d_parent; /* safe d_parent access */
37205 +       bbot = au_sbbot(sb);
37206 +       for (bindex = 0; bindex <= bbot; bindex++) {
37207 +               br = au_sbr(sb, bindex);
37208 +               if (au_br_dentry(br) == parent) {
37209 +                       bfound = bindex;
37210 +                       break;
37211 +               }
37212 +       }
37213 +
37214 +       AuDbg("bfound b%d\n", bfound);
37215 +       return bfound;
37216 +}
37217 +
37218 +struct au_xino_lock_dir {
37219 +       struct au_hinode *hdir;
37220 +       struct dentry *parent;
37221 +       struct inode *dir;
37222 +};
37223 +
37224 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37225 +                                         unsigned int lsc)
37226 +{
37227 +       struct dentry *parent;
37228 +       struct inode *dir;
37229 +
37230 +       parent = dget_parent(dentry);
37231 +       dir = d_inode(parent);
37232 +       inode_lock_nested(dir, lsc);
37233 +#if 0 /* it should not happen */
37234 +       spin_lock(&dentry->d_lock);
37235 +       if (unlikely(dentry->d_parent != parent)) {
37236 +               spin_unlock(&dentry->d_lock);
37237 +               inode_unlock(dir);
37238 +               dput(parent);
37239 +               parent = NULL;
37240 +               goto out;
37241 +       }
37242 +       spin_unlock(&dentry->d_lock);
37243 +
37244 +out:
37245 +#endif
37246 +       return parent;
37247 +}
37248 +
37249 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37250 +                            struct au_xino_lock_dir *ldir)
37251 +{
37252 +       aufs_bindex_t bindex;
37253 +
37254 +       ldir->hdir = NULL;
37255 +       bindex = au_xi_root(sb, xipath->dentry);
37256 +       if (bindex >= 0) {
37257 +               /* rw branch root */
37258 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37259 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37260 +       } else {
37261 +               /* other */
37262 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37263 +                                                  AuLsc_I_PARENT);
37264 +               ldir->dir = d_inode(ldir->parent);
37265 +       }
37266 +}
37267 +
37268 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37269 +{
37270 +       if (ldir->hdir)
37271 +               au_hn_inode_unlock(ldir->hdir);
37272 +       else {
37273 +               inode_unlock(ldir->dir);
37274 +               dput(ldir->parent);
37275 +       }
37276 +}
37277 +
37278 +/* ---------------------------------------------------------------------- */
37279 +
37280 +/*
37281 + * create and set a new xino file
37282 + */
37283 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37284 +                           int wbrtop)
37285 +{
37286 +       struct file *file;
37287 +       struct dentry *h_parent, *d;
37288 +       struct inode *h_dir, *inode;
37289 +       int err;
37290 +       static DEFINE_MUTEX(mtx);
37291 +
37292 +       /*
37293 +        * at mount-time, and the xino file is the default path,
37294 +        * hnotify is disabled so we have no notify events to ignore.
37295 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37296 +        */
37297 +       if (!wbrtop)
37298 +               mutex_lock(&mtx);
37299 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37300 +                              /* | __FMODE_NONOTIFY */,
37301 +                              0666);
37302 +       if (IS_ERR(file)) {
37303 +               if (!wbrtop)
37304 +                       mutex_unlock(&mtx);
37305 +               if (!silent)
37306 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37307 +               return file;
37308 +       }
37309 +
37310 +       /* keep file count */
37311 +       err = 0;
37312 +       d = file->f_path.dentry;
37313 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37314 +       if (!wbrtop)
37315 +               mutex_unlock(&mtx);
37316 +       /* mnt_want_write() is unnecessary here */
37317 +       h_dir = d_inode(h_parent);
37318 +       inode = file_inode(file);
37319 +       /* no delegation since it is just created */
37320 +       if (inode->i_nlink)
37321 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37322 +                                  /*force*/0);
37323 +       inode_unlock(h_dir);
37324 +       dput(h_parent);
37325 +       if (unlikely(err)) {
37326 +               if (!silent)
37327 +                       pr_err("unlink %s(%d)\n", fpath, err);
37328 +               goto out;
37329 +       }
37330 +
37331 +       err = -EINVAL;
37332 +       if (unlikely(sb && sb == d->d_sb)) {
37333 +               if (!silent)
37334 +                       pr_err("%s must be outside\n", fpath);
37335 +               goto out;
37336 +       }
37337 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37338 +               if (!silent)
37339 +                       pr_err("xino doesn't support %s(%s)\n",
37340 +                              fpath, au_sbtype(d->d_sb));
37341 +               goto out;
37342 +       }
37343 +       return file; /* success */
37344 +
37345 +out:
37346 +       fput(file);
37347 +       file = ERR_PTR(err);
37348 +       return file;
37349 +}
37350 +
37351 +/*
37352 + * create a new xinofile at the same place/path as @base.
37353 + */
37354 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37355 +                            struct file *copy_src)
37356 +{
37357 +       struct file *file;
37358 +       struct dentry *dentry;
37359 +       struct inode *dir, *delegated;
37360 +       struct qstr *name;
37361 +       struct path ppath, path;
37362 +       int err, do_unlock;
37363 +       struct au_xino_lock_dir ldir;
37364 +
37365 +       do_unlock = 1;
37366 +       au_xino_lock_dir(sb, base, &ldir);
37367 +       dentry = base->dentry;
37368 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37369 +       ppath.mnt = base->mnt;
37370 +       dir = d_inode(ppath.dentry);
37371 +       IMustLock(dir);
37372 +
37373 +       name = &dentry->d_name;
37374 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37375 +       if (IS_ERR(path.dentry)) {
37376 +               file = (void *)path.dentry;
37377 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37378 +               goto out;
37379 +       }
37380 +
37381 +       /* no need to mnt_want_write() since we call dentry_open() later */
37382 +       err = vfs_create(mnt_idmap(base->mnt), dir, path.dentry, 0666, NULL);
37383 +       if (unlikely(err)) {
37384 +               file = ERR_PTR(err);
37385 +               pr_err("%pd create err %d\n", dentry, err);
37386 +               goto out_dput;
37387 +       }
37388 +
37389 +       path.mnt = base->mnt;
37390 +       file = vfsub_dentry_open(&path,
37391 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37392 +                                /* | __FMODE_NONOTIFY */);
37393 +       if (IS_ERR(file)) {
37394 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37395 +               goto out_dput;
37396 +       }
37397 +
37398 +       delegated = NULL;
37399 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37400 +       au_xino_unlock_dir(&ldir);
37401 +       do_unlock = 0;
37402 +       if (unlikely(err == -EWOULDBLOCK)) {
37403 +               pr_warn("cannot retry for NFSv4 delegation"
37404 +                       " for an internal unlink\n");
37405 +               iput(delegated);
37406 +       }
37407 +       if (unlikely(err)) {
37408 +               pr_err("%pd unlink err %d\n", dentry, err);
37409 +               goto out_fput;
37410 +       }
37411 +
37412 +       if (copy_src) {
37413 +               /* no one can touch copy_src xino */
37414 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37415 +               if (unlikely(err)) {
37416 +                       pr_err("%pd copy err %d\n", dentry, err);
37417 +                       goto out_fput;
37418 +               }
37419 +       }
37420 +       goto out_dput; /* success */
37421 +
37422 +out_fput:
37423 +       fput(file);
37424 +       file = ERR_PTR(err);
37425 +out_dput:
37426 +       dput(path.dentry);
37427 +out:
37428 +       if (do_unlock)
37429 +               au_xino_unlock_dir(&ldir);
37430 +       return file;
37431 +}
37432 +
37433 +struct file *au_xino_file1(struct au_xino *xi)
37434 +{
37435 +       struct file *file;
37436 +       unsigned int u, nfile;
37437 +
37438 +       file = NULL;
37439 +       nfile = xi->xi_nfile;
37440 +       for (u = 0; u < nfile; u++) {
37441 +               file = xi->xi_file[u];
37442 +               if (file)
37443 +                       break;
37444 +       }
37445 +
37446 +       return file;
37447 +}
37448 +
37449 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37450 +{
37451 +       int err;
37452 +       struct file *f;
37453 +       void *p;
37454 +
37455 +       if (file)
37456 +               get_file(file);
37457 +
37458 +       err = 0;
37459 +       f = NULL;
37460 +       if (idx < xi->xi_nfile) {
37461 +               f = xi->xi_file[idx];
37462 +               if (f)
37463 +                       fput(f);
37464 +       } else {
37465 +               p = au_kzrealloc(xi->xi_file,
37466 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37467 +                                sizeof(*xi->xi_file) * (idx + 1),
37468 +                                GFP_NOFS, /*may_shrink*/0);
37469 +               if (p) {
37470 +                       MtxMustLock(&xi->xi_mtx);
37471 +                       xi->xi_file = p;
37472 +                       xi->xi_nfile = idx + 1;
37473 +               } else {
37474 +                       err = -ENOMEM;
37475 +                       if (file)
37476 +                               fput(file);
37477 +                       goto out;
37478 +               }
37479 +       }
37480 +       xi->xi_file[idx] = file;
37481 +
37482 +out:
37483 +       return err;
37484 +}
37485 +
37486 +/*
37487 + * if @xinew->xi is not set, then create new xigen file.
37488 + */
37489 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37490 +{
37491 +       struct file *file;
37492 +       int err;
37493 +
37494 +       SiMustAnyLock(sb);
37495 +
37496 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37497 +       if (IS_ERR(file)) {
37498 +               err = PTR_ERR(file);
37499 +               pr_err("%s[%d], err %d\n",
37500 +                      xinew->xi ? "xino" : "xigen",
37501 +                      xinew->idx, err);
37502 +               goto out;
37503 +       }
37504 +
37505 +       if (xinew->xi)
37506 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37507 +       else {
37508 +               BUG();
37509 +               /* todo: make xigen file an array */
37510 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37511 +       }
37512 +       fput(file);
37513 +       if (unlikely(err))
37514 +               file = ERR_PTR(err);
37515 +
37516 +out:
37517 +       return file;
37518 +}
37519 +
37520 +/* ---------------------------------------------------------------------- */
37521 +
37522 +/*
37523 + * truncate xino files
37524 + */
37525 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37526 +                           int idx, struct kstatfs *st)
37527 +{
37528 +       int err;
37529 +       blkcnt_t blocks;
37530 +       struct file *file, *new_xino;
37531 +       struct au_xi_new xinew = {
37532 +               .idx = idx
37533 +       };
37534 +
37535 +       err = 0;
37536 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37537 +       file = au_xino_file(xinew.xi, idx);
37538 +       if (!file)
37539 +               goto out;
37540 +
37541 +       xinew.base = &file->f_path;
37542 +       err = vfs_statfs(xinew.base, st);
37543 +       if (unlikely(err)) {
37544 +               AuErr1("statfs err %d, ignored\n", err);
37545 +               err = 0;
37546 +               goto out;
37547 +       }
37548 +
37549 +       blocks = file_inode(file)->i_blocks;
37550 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37551 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37552 +
37553 +       xinew.copy_src = file;
37554 +       new_xino = au_xi_new(sb, &xinew);
37555 +       if (IS_ERR(new_xino)) {
37556 +               err = PTR_ERR(new_xino);
37557 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37558 +               goto out;
37559 +       }
37560 +
37561 +       err = vfs_statfs(&new_xino->f_path, st);
37562 +       if (!err)
37563 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37564 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37565 +                       st->f_bfree, st->f_blocks);
37566 +       else {
37567 +               AuErr1("statfs err %d, ignored\n", err);
37568 +               err = 0;
37569 +       }
37570 +
37571 +out:
37572 +       return err;
37573 +}
37574 +
37575 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37576 +{
37577 +       int err, i;
37578 +       unsigned long jiffy;
37579 +       aufs_bindex_t bbot;
37580 +       struct kstatfs *st;
37581 +       struct au_branch *br;
37582 +       struct au_xino *xi;
37583 +
37584 +       err = -ENOMEM;
37585 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37586 +       if (unlikely(!st))
37587 +               goto out;
37588 +
37589 +       err = -EINVAL;
37590 +       bbot = au_sbbot(sb);
37591 +       if (unlikely(bindex < 0 || bbot < bindex))
37592 +               goto out_st;
37593 +
37594 +       err = 0;
37595 +       jiffy = jiffies;
37596 +       br = au_sbr(sb, bindex);
37597 +       xi = br->br_xino;
37598 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37599 +               err = au_xino_do_trunc(sb, bindex, i, st);
37600 +       if (!err)
37601 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37602 +
37603 +out_st:
37604 +       au_kfree_rcu(st);
37605 +out:
37606 +       return err;
37607 +}
37608 +
37609 +struct xino_do_trunc_args {
37610 +       struct super_block *sb;
37611 +       struct au_branch *br;
37612 +       int idx;
37613 +};
37614 +
37615 +static void xino_do_trunc(void *_args)
37616 +{
37617 +       struct xino_do_trunc_args *args = _args;
37618 +       struct super_block *sb;
37619 +       struct au_branch *br;
37620 +       struct inode *dir;
37621 +       int err, idx;
37622 +       aufs_bindex_t bindex;
37623 +
37624 +       err = 0;
37625 +       sb = args->sb;
37626 +       dir = d_inode(sb->s_root);
37627 +       br = args->br;
37628 +       idx = args->idx;
37629 +
37630 +       si_noflush_write_lock(sb);
37631 +       ii_read_lock_parent(dir);
37632 +       bindex = au_br_index(sb, br->br_id);
37633 +       err = au_xino_trunc(sb, bindex, idx);
37634 +       ii_read_unlock(dir);
37635 +       if (unlikely(err))
37636 +               pr_warn("err b%d, (%d)\n", bindex, err);
37637 +       atomic_dec(&br->br_xino->xi_truncating);
37638 +       au_lcnt_dec(&br->br_count);
37639 +       si_write_unlock(sb);
37640 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37641 +       au_kfree_rcu(args);
37642 +}
37643 +
37644 +/*
37645 + * returns the index in the xi_file array whose corresponding file is necessary
37646 + * to truncate, or -1 which means no need to truncate.
37647 + */
37648 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37649 +{
37650 +       int err;
37651 +       unsigned int u;
37652 +       struct kstatfs st;
37653 +       struct au_sbinfo *sbinfo;
37654 +       struct au_xino *xi;
37655 +       struct file *file;
37656 +
37657 +       /* todo: si_xino_expire and the ratio should be customizable */
37658 +       sbinfo = au_sbi(sb);
37659 +       if (time_before(jiffies,
37660 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37661 +               return -1;
37662 +
37663 +       /* truncation border */
37664 +       xi = br->br_xino;
37665 +       for (u = 0; u < xi->xi_nfile; u++) {
37666 +               file = au_xino_file(xi, u);
37667 +               if (!file)
37668 +                       continue;
37669 +
37670 +               err = vfs_statfs(&file->f_path, &st);
37671 +               if (unlikely(err)) {
37672 +                       AuErr1("statfs err %d, ignored\n", err);
37673 +                       return -1;
37674 +               }
37675 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37676 +                   >= AUFS_XINO_DEF_TRUNC)
37677 +                       return u;
37678 +       }
37679 +
37680 +       return -1;
37681 +}
37682 +
37683 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37684 +{
37685 +       int idx;
37686 +       struct xino_do_trunc_args *args;
37687 +       int wkq_err;
37688 +
37689 +       idx = xino_trunc_test(sb, br);
37690 +       if (idx < 0)
37691 +               return;
37692 +
37693 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37694 +               goto out;
37695 +
37696 +       /* lock and kfree() will be called in trunc_xino() */
37697 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37698 +       if (unlikely(!args)) {
37699 +               AuErr1("no memory\n");
37700 +               goto out;
37701 +       }
37702 +
37703 +       au_lcnt_inc(&br->br_count);
37704 +       args->sb = sb;
37705 +       args->br = br;
37706 +       args->idx = idx;
37707 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37708 +       if (!wkq_err)
37709 +               return; /* success */
37710 +
37711 +       pr_err("wkq %d\n", wkq_err);
37712 +       au_lcnt_dec(&br->br_count);
37713 +       au_kfree_rcu(args);
37714 +
37715 +out:
37716 +       atomic_dec(&br->br_xino->xi_truncating);
37717 +}
37718 +
37719 +/* ---------------------------------------------------------------------- */
37720 +
37721 +struct au_xi_calc {
37722 +       int idx;
37723 +       loff_t pos;
37724 +};
37725 +
37726 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37727 +                      struct au_xi_calc *calc)
37728 +{
37729 +       loff_t maxent;
37730 +
37731 +       maxent = au_xi_maxent(sb);
37732 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37733 +       calc->pos *= sizeof(ino_t);
37734 +}
37735 +
37736 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37737 +                               struct au_xi_calc *calc)
37738 +{
37739 +       int err;
37740 +       struct file *file;
37741 +       struct au_xino *xi = br->br_xino;
37742 +       struct au_xi_new xinew = {
37743 +               .xi = xi
37744 +       };
37745 +
37746 +       SiMustAnyLock(sb);
37747 +
37748 +       err = 0;
37749 +       if (!xi)
37750 +               goto out;
37751 +
37752 +       mutex_lock(&xi->xi_mtx);
37753 +       file = au_xino_file(xi, calc->idx);
37754 +       if (file)
37755 +               goto out_mtx;
37756 +
37757 +       file = au_xino_file(xi, /*idx*/-1);
37758 +       AuDebugOn(!file);
37759 +       xinew.idx = calc->idx;
37760 +       xinew.base = &file->f_path;
37761 +       /* xinew.copy_src = NULL; */
37762 +       file = au_xi_new(sb, &xinew);
37763 +       if (IS_ERR(file))
37764 +               err = PTR_ERR(file);
37765 +
37766 +out_mtx:
37767 +       mutex_unlock(&xi->xi_mtx);
37768 +out:
37769 +       return err;
37770 +}
37771 +
37772 +struct au_xino_do_new_async_args {
37773 +       struct super_block *sb;
37774 +       struct au_branch *br;
37775 +       struct au_xi_calc calc;
37776 +       ino_t ino;
37777 +};
37778 +
37779 +struct au_xi_writing {
37780 +       struct hlist_bl_node node;
37781 +       ino_t h_ino, ino;
37782 +};
37783 +
37784 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37785 +                           ino_t ino);
37786 +
37787 +static void au_xino_call_do_new_async(void *args)
37788 +{
37789 +       struct au_xino_do_new_async_args *a = args;
37790 +       struct au_branch *br;
37791 +       struct super_block *sb;
37792 +       struct au_sbinfo *sbi;
37793 +       struct inode *root;
37794 +       struct file *file;
37795 +       struct au_xi_writing *del, *p;
37796 +       struct hlist_bl_head *hbl;
37797 +       struct hlist_bl_node *pos;
37798 +       int err;
37799 +
37800 +       br = a->br;
37801 +       sb = a->sb;
37802 +       sbi = au_sbi(sb);
37803 +       si_noflush_read_lock(sb);
37804 +       root = d_inode(sb->s_root);
37805 +       ii_read_lock_child(root);
37806 +       err = au_xino_do_new_async(sb, br, &a->calc);
37807 +       if (unlikely(err)) {
37808 +               AuIOErr("err %d\n", err);
37809 +               goto out;
37810 +       }
37811 +
37812 +       file = au_xino_file(br->br_xino, a->calc.idx);
37813 +       AuDebugOn(!file);
37814 +       err = au_xino_do_write(file, &a->calc, a->ino);
37815 +       if (unlikely(err)) {
37816 +               AuIOErr("err %d\n", err);
37817 +               goto out;
37818 +       }
37819 +
37820 +       del = NULL;
37821 +       hbl = &br->br_xino->xi_writing;
37822 +       hlist_bl_lock(hbl);
37823 +       au_hbl_for_each(pos, hbl) {
37824 +               p = container_of(pos, struct au_xi_writing, node);
37825 +               if (p->ino == a->ino) {
37826 +                       del = p;
37827 +                       hlist_bl_del(&p->node);
37828 +                       break;
37829 +               }
37830 +       }
37831 +       hlist_bl_unlock(hbl);
37832 +       au_kfree_rcu(del);
37833 +
37834 +out:
37835 +       au_lcnt_dec(&br->br_count);
37836 +       ii_read_unlock(root);
37837 +       si_read_unlock(sb);
37838 +       au_nwt_done(&sbi->si_nowait);
37839 +       au_kfree_rcu(a);
37840 +}
37841 +
37842 +/*
37843 + * create a new xino file asynchronously
37844 + */
37845 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37846 +                            struct au_xi_calc *calc, ino_t ino)
37847 +{
37848 +       int err;
37849 +       struct au_xino_do_new_async_args *arg;
37850 +
37851 +       err = -ENOMEM;
37852 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37853 +       if (unlikely(!arg))
37854 +               goto out;
37855 +
37856 +       arg->sb = sb;
37857 +       arg->br = br;
37858 +       arg->calc = *calc;
37859 +       arg->ino = ino;
37860 +       au_lcnt_inc(&br->br_count);
37861 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37862 +       if (unlikely(err)) {
37863 +               pr_err("wkq %d\n", err);
37864 +               au_lcnt_dec(&br->br_count);
37865 +               au_kfree_rcu(arg);
37866 +       }
37867 +
37868 +out:
37869 +       return err;
37870 +}
37871 +
37872 +/*
37873 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37874 + * at the position of @h_ino.
37875 + */
37876 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37877 +                ino_t *ino)
37878 +{
37879 +       int err;
37880 +       ssize_t sz;
37881 +       struct au_xi_calc calc;
37882 +       struct au_sbinfo *sbinfo;
37883 +       struct file *file;
37884 +       struct au_xino *xi;
37885 +       struct hlist_bl_head *hbl;
37886 +       struct hlist_bl_node *pos;
37887 +       struct au_xi_writing *p;
37888 +
37889 +       *ino = 0;
37890 +       if (!au_opt_test(au_mntflags(sb), XINO))
37891 +               return 0; /* no xino */
37892 +
37893 +       err = 0;
37894 +       au_xi_calc(sb, h_ino, &calc);
37895 +       xi = au_sbr(sb, bindex)->br_xino;
37896 +       file = au_xino_file(xi, calc.idx);
37897 +       if (!file) {
37898 +               hbl = &xi->xi_writing;
37899 +               hlist_bl_lock(hbl);
37900 +               au_hbl_for_each(pos, hbl) {
37901 +                       p = container_of(pos, struct au_xi_writing, node);
37902 +                       if (p->h_ino == h_ino) {
37903 +                               AuDbg("hi%llu, i%llu, found\n",
37904 +                                     (u64)p->h_ino, (u64)p->ino);
37905 +                               *ino = p->ino;
37906 +                               break;
37907 +                       }
37908 +               }
37909 +               hlist_bl_unlock(hbl);
37910 +               return 0;
37911 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37912 +               return 0; /* no xino */
37913 +
37914 +       sbinfo = au_sbi(sb);
37915 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37916 +       if (sz == sizeof(*ino))
37917 +               return 0; /* success */
37918 +
37919 +       err = sz;
37920 +       if (unlikely(sz >= 0)) {
37921 +               err = -EIO;
37922 +               AuIOErr("xino read error (%zd)\n", sz);
37923 +       }
37924 +       return err;
37925 +}
37926 +
37927 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37928 +                           ino_t ino)
37929 +{
37930 +       ssize_t sz;
37931 +
37932 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37933 +       if (sz == sizeof(ino))
37934 +               return 0; /* success */
37935 +
37936 +       AuIOErr("write failed (%zd)\n", sz);
37937 +       return -EIO;
37938 +}
37939 +
37940 +/*
37941 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37942 + * at the position of @h_ino.
37943 + * even if @ino is zero, it is written to the xinofile and means no entry.
37944 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37945 + * try truncating it.
37946 + */
37947 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37948 +                 ino_t ino)
37949 +{
37950 +       int err;
37951 +       unsigned int mnt_flags;
37952 +       struct au_xi_calc calc;
37953 +       struct file *file;
37954 +       struct au_branch *br;
37955 +       struct au_xino *xi;
37956 +       struct au_xi_writing *p;
37957 +
37958 +       SiMustAnyLock(sb);
37959 +
37960 +       mnt_flags = au_mntflags(sb);
37961 +       if (!au_opt_test(mnt_flags, XINO))
37962 +               return 0;
37963 +
37964 +       au_xi_calc(sb, h_ino, &calc);
37965 +       br = au_sbr(sb, bindex);
37966 +       xi = br->br_xino;
37967 +       file = au_xino_file(xi, calc.idx);
37968 +       if (!file) {
37969 +               /* store the inum pair into the list */
37970 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37971 +               p->h_ino = h_ino;
37972 +               p->ino = ino;
37973 +               au_hbl_add(&p->node, &xi->xi_writing);
37974 +
37975 +               /* create and write a new xino file asynchronously */
37976 +               err = au_xino_new_async(sb, br, &calc, ino);
37977 +               if (!err)
37978 +                       return 0; /* success */
37979 +               goto out;
37980 +       }
37981 +
37982 +       err = au_xino_do_write(file, &calc, ino);
37983 +       if (!err) {
37984 +               br = au_sbr(sb, bindex);
37985 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37986 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37987 +                       xino_try_trunc(sb, br);
37988 +               return 0; /* success */
37989 +       }
37990 +
37991 +out:
37992 +       AuIOErr("write failed (%d)\n", err);
37993 +       return -EIO;
37994 +}
37995 +
37996 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37997 +                             loff_t *pos);
37998 +
37999 +/* todo: unnecessary to support mmap_sem since kernel-space? */
38000 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
38001 +{
38002 +       ssize_t err;
38003 +       int i;
38004 +       const int prevent_endless = 10;
38005 +
38006 +       i = 0;
38007 +       do {
38008 +               err = vfsub_read_k(file, kbuf, size, pos);
38009 +               if (err == -EINTR
38010 +                   && !au_wkq_test()
38011 +                   && fatal_signal_pending(current)) {
38012 +                       err = xino_fread_wkq(file, kbuf, size, pos);
38013 +                       BUG_ON(err == -EINTR);
38014 +               }
38015 +       } while (i++ < prevent_endless
38016 +                && (err == -EAGAIN || err == -EINTR));
38017 +
38018 +#if 0 /* reserved for future use */
38019 +       if (err > 0)
38020 +               fsnotify_access(file->f_path.dentry);
38021 +#endif
38022 +
38023 +       return err;
38024 +}
38025 +
38026 +struct xino_fread_args {
38027 +       ssize_t *errp;
38028 +       struct file *file;
38029 +       void *buf;
38030 +       size_t size;
38031 +       loff_t *pos;
38032 +};
38033 +
38034 +static void call_xino_fread(void *args)
38035 +{
38036 +       struct xino_fread_args *a = args;
38037 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
38038 +}
38039 +
38040 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38041 +                             loff_t *pos)
38042 +{
38043 +       ssize_t err;
38044 +       int wkq_err;
38045 +       struct xino_fread_args args = {
38046 +               .errp   = &err,
38047 +               .file   = file,
38048 +               .buf    = buf,
38049 +               .size   = size,
38050 +               .pos    = pos
38051 +       };
38052 +
38053 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
38054 +       if (unlikely(wkq_err))
38055 +               err = wkq_err;
38056 +
38057 +       return err;
38058 +}
38059 +
38060 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38061 +                              loff_t *pos);
38062 +
38063 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
38064 +                             loff_t *pos)
38065 +{
38066 +       ssize_t err;
38067 +       int i;
38068 +       const int prevent_endless = 10;
38069 +
38070 +       i = 0;
38071 +       do {
38072 +               err = vfsub_write_k(file, kbuf, size, pos);
38073 +               if (err == -EINTR
38074 +                   && !au_wkq_test()
38075 +                   && fatal_signal_pending(current)) {
38076 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
38077 +                       BUG_ON(err == -EINTR);
38078 +               }
38079 +       } while (i++ < prevent_endless
38080 +                && (err == -EAGAIN || err == -EINTR));
38081 +
38082 +#if 0 /* reserved for future use */
38083 +       if (err > 0)
38084 +               fsnotify_modify(file->f_path.dentry);
38085 +#endif
38086 +
38087 +       return err;
38088 +}
38089 +
38090 +struct do_xino_fwrite_args {
38091 +       ssize_t *errp;
38092 +       struct file *file;
38093 +       void *buf;
38094 +       size_t size;
38095 +       loff_t *pos;
38096 +};
38097 +
38098 +static void call_do_xino_fwrite(void *args)
38099 +{
38100 +       struct do_xino_fwrite_args *a = args;
38101 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
38102 +}
38103 +
38104 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38105 +                              loff_t *pos)
38106 +{
38107 +       ssize_t err;
38108 +       int wkq_err;
38109 +       struct do_xino_fwrite_args args = {
38110 +               .errp   = &err,
38111 +               .file   = file,
38112 +               .buf    = buf,
38113 +               .size   = size,
38114 +               .pos    = pos
38115 +       };
38116 +
38117 +       /*
38118 +        * it breaks RLIMIT_FSIZE and normal user's limit,
38119 +        * users should care about quota and real 'filesystem full.'
38120 +        */
38121 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38122 +       if (unlikely(wkq_err))
38123 +               err = wkq_err;
38124 +
38125 +       return err;
38126 +}
38127 +
38128 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
38129 +{
38130 +       ssize_t err;
38131 +
38132 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38133 +               lockdep_off();
38134 +               err = do_xino_fwrite(file, buf, size, pos);
38135 +               lockdep_on();
38136 +       } else {
38137 +               lockdep_off();
38138 +               err = xino_fwrite_wkq(file, buf, size, pos);
38139 +               lockdep_on();
38140 +       }
38141 +
38142 +       return err;
38143 +}
38144 +
38145 +/* ---------------------------------------------------------------------- */
38146 +
38147 +/*
38148 + * inode number bitmap
38149 + */
38150 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38151 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
38152 +{
38153 +       ino_t ino;
38154 +
38155 +       AuDebugOn(bit < 0 || page_bits <= bit);
38156 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38157 +       return ino;
38158 +}
38159 +
38160 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38161 +{
38162 +       AuDebugOn(ino < AUFS_FIRST_INO);
38163 +       ino -= AUFS_FIRST_INO;
38164 +       *pindex = ino / page_bits;
38165 +       *bit = ino % page_bits;
38166 +}
38167 +
38168 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
38169 +{
38170 +       int err;
38171 +       loff_t pos;
38172 +       ssize_t sz;
38173 +       struct au_sbinfo *sbinfo;
38174 +       struct file *xib;
38175 +       unsigned long *p;
38176 +
38177 +       sbinfo = au_sbi(sb);
38178 +       MtxMustLock(&sbinfo->si_xib_mtx);
38179 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38180 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38181 +
38182 +       if (pindex == sbinfo->si_xib_last_pindex)
38183 +               return 0;
38184 +
38185 +       xib = sbinfo->si_xib;
38186 +       p = sbinfo->si_xib_buf;
38187 +       pos = sbinfo->si_xib_last_pindex;
38188 +       pos *= PAGE_SIZE;
38189 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38190 +       if (unlikely(sz != PAGE_SIZE))
38191 +               goto out;
38192 +
38193 +       pos = pindex;
38194 +       pos *= PAGE_SIZE;
38195 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38196 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
38197 +       else {
38198 +               memset(p, 0, PAGE_SIZE);
38199 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38200 +       }
38201 +       if (sz == PAGE_SIZE) {
38202 +               sbinfo->si_xib_last_pindex = pindex;
38203 +               return 0; /* success */
38204 +       }
38205 +
38206 +out:
38207 +       AuIOErr1("write failed (%zd)\n", sz);
38208 +       err = sz;
38209 +       if (sz >= 0)
38210 +               err = -EIO;
38211 +       return err;
38212 +}
38213 +
38214 +static void au_xib_clear_bit(struct inode *inode)
38215 +{
38216 +       int err, bit;
38217 +       unsigned long pindex;
38218 +       struct super_block *sb;
38219 +       struct au_sbinfo *sbinfo;
38220 +
38221 +       AuDebugOn(inode->i_nlink);
38222 +
38223 +       sb = inode->i_sb;
38224 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38225 +       AuDebugOn(page_bits <= bit);
38226 +       sbinfo = au_sbi(sb);
38227 +       mutex_lock(&sbinfo->si_xib_mtx);
38228 +       err = xib_pindex(sb, pindex);
38229 +       if (!err) {
38230 +               clear_bit(bit, sbinfo->si_xib_buf);
38231 +               sbinfo->si_xib_next_bit = bit;
38232 +       }
38233 +       mutex_unlock(&sbinfo->si_xib_mtx);
38234 +}
38235 +
38236 +/* ---------------------------------------------------------------------- */
38237 +
38238 +/*
38239 + * truncate a xino bitmap file
38240 + */
38241 +
38242 +/* todo: slow */
38243 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38244 +{
38245 +       int err, bit;
38246 +       ssize_t sz;
38247 +       unsigned long pindex;
38248 +       loff_t pos, pend;
38249 +       struct au_sbinfo *sbinfo;
38250 +       ino_t *ino;
38251 +       unsigned long *p;
38252 +
38253 +       err = 0;
38254 +       sbinfo = au_sbi(sb);
38255 +       MtxMustLock(&sbinfo->si_xib_mtx);
38256 +       p = sbinfo->si_xib_buf;
38257 +       pend = vfsub_f_size_read(file);
38258 +       pos = 0;
38259 +       while (pos < pend) {
38260 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
38261 +               err = sz;
38262 +               if (unlikely(sz <= 0))
38263 +                       goto out;
38264 +
38265 +               err = 0;
38266 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38267 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38268 +                               continue;
38269 +
38270 +                       xib_calc_bit(*ino, &pindex, &bit);
38271 +                       AuDebugOn(page_bits <= bit);
38272 +                       err = xib_pindex(sb, pindex);
38273 +                       if (!err)
38274 +                               set_bit(bit, p);
38275 +                       else
38276 +                               goto out;
38277 +               }
38278 +       }
38279 +
38280 +out:
38281 +       return err;
38282 +}
38283 +
38284 +static int xib_restore(struct super_block *sb)
38285 +{
38286 +       int err, i;
38287 +       unsigned int nfile;
38288 +       aufs_bindex_t bindex, bbot;
38289 +       void *page;
38290 +       struct au_branch *br;
38291 +       struct au_xino *xi;
38292 +       struct file *file;
38293 +
38294 +       err = -ENOMEM;
38295 +       page = (void *)__get_free_page(GFP_NOFS);
38296 +       if (unlikely(!page))
38297 +               goto out;
38298 +
38299 +       err = 0;
38300 +       bbot = au_sbbot(sb);
38301 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38302 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38303 +                       br = au_sbr(sb, bindex);
38304 +                       xi = br->br_xino;
38305 +                       nfile = xi->xi_nfile;
38306 +                       for (i = 0; i < nfile; i++) {
38307 +                               file = au_xino_file(xi, i);
38308 +                               if (file)
38309 +                                       err = do_xib_restore(sb, file, page);
38310 +                       }
38311 +               } else
38312 +                       AuDbg("skip shared b%d\n", bindex);
38313 +       free_page((unsigned long)page);
38314 +
38315 +out:
38316 +       return err;
38317 +}
38318 +
38319 +int au_xib_trunc(struct super_block *sb)
38320 +{
38321 +       int err;
38322 +       ssize_t sz;
38323 +       loff_t pos;
38324 +       struct au_sbinfo *sbinfo;
38325 +       unsigned long *p;
38326 +       struct file *file;
38327 +
38328 +       SiMustWriteLock(sb);
38329 +
38330 +       err = 0;
38331 +       sbinfo = au_sbi(sb);
38332 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38333 +               goto out;
38334 +
38335 +       file = sbinfo->si_xib;
38336 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38337 +               goto out;
38338 +
38339 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38340 +       err = PTR_ERR(file);
38341 +       if (IS_ERR(file))
38342 +               goto out;
38343 +       fput(sbinfo->si_xib);
38344 +       sbinfo->si_xib = file;
38345 +
38346 +       p = sbinfo->si_xib_buf;
38347 +       memset(p, 0, PAGE_SIZE);
38348 +       pos = 0;
38349 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38350 +       if (unlikely(sz != PAGE_SIZE)) {
38351 +               err = sz;
38352 +               AuIOErr("err %d\n", err);
38353 +               if (sz >= 0)
38354 +                       err = -EIO;
38355 +               goto out;
38356 +       }
38357 +
38358 +       mutex_lock(&sbinfo->si_xib_mtx);
38359 +       /* mnt_want_write() is unnecessary here */
38360 +       err = xib_restore(sb);
38361 +       mutex_unlock(&sbinfo->si_xib_mtx);
38362 +
38363 +out:
38364 +       return err;
38365 +}
38366 +
38367 +/* ---------------------------------------------------------------------- */
38368 +
38369 +struct au_xino *au_xino_alloc(unsigned int nfile)
38370 +{
38371 +       struct au_xino *xi;
38372 +
38373 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38374 +       if (unlikely(!xi))
38375 +               goto out;
38376 +       xi->xi_nfile = nfile;
38377 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38378 +       if (unlikely(!xi->xi_file))
38379 +               goto out_free;
38380 +
38381 +       xi->xi_nondir.total = 8; /* initial size */
38382 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38383 +                                     GFP_NOFS);
38384 +       if (unlikely(!xi->xi_nondir.array))
38385 +               goto out_file;
38386 +
38387 +       spin_lock_init(&xi->xi_nondir.spin);
38388 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38389 +       mutex_init(&xi->xi_mtx);
38390 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38391 +       atomic_set(&xi->xi_truncating, 0);
38392 +       kref_init(&xi->xi_kref);
38393 +       goto out; /* success */
38394 +
38395 +out_file:
38396 +       au_kfree_try_rcu(xi->xi_file);
38397 +out_free:
38398 +       au_kfree_rcu(xi);
38399 +       xi = NULL;
38400 +out:
38401 +       return xi;
38402 +}
38403 +
38404 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38405 +{
38406 +       int err;
38407 +       struct au_xino *xi;
38408 +
38409 +       err = 0;
38410 +       xi = au_xino_alloc(idx + 1);
38411 +       if (unlikely(!xi)) {
38412 +               err = -ENOMEM;
38413 +               goto out;
38414 +       }
38415 +
38416 +       if (file)
38417 +               get_file(file);
38418 +       xi->xi_file[idx] = file;
38419 +       AuDebugOn(br->br_xino);
38420 +       br->br_xino = xi;
38421 +
38422 +out:
38423 +       return err;
38424 +}
38425 +
38426 +static void au_xino_release(struct kref *kref)
38427 +{
38428 +       struct au_xino *xi;
38429 +       int i;
38430 +       unsigned long ul;
38431 +       struct hlist_bl_head *hbl;
38432 +       struct hlist_bl_node *pos, *n;
38433 +       struct au_xi_writing *p;
38434 +
38435 +       xi = container_of(kref, struct au_xino, xi_kref);
38436 +       for (i = 0; i < xi->xi_nfile; i++)
38437 +               if (xi->xi_file[i])
38438 +                       fput(xi->xi_file[i]);
38439 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38440 +               AuDebugOn(xi->xi_nondir.array[i]);
38441 +       mutex_destroy(&xi->xi_mtx);
38442 +       hbl = &xi->xi_writing;
38443 +       ul = au_hbl_count(hbl);
38444 +       if (unlikely(ul)) {
38445 +               pr_warn("xi_writing %lu\n", ul);
38446 +               hlist_bl_lock(hbl);
38447 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38448 +                       hlist_bl_del(&p->node);
38449 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38450 +                       kfree(p);
38451 +               }
38452 +               hlist_bl_unlock(hbl);
38453 +       }
38454 +       au_kfree_try_rcu(xi->xi_file);
38455 +       au_kfree_try_rcu(xi->xi_nondir.array);
38456 +       au_kfree_rcu(xi);
38457 +}
38458 +
38459 +int au_xino_put(struct au_branch *br)
38460 +{
38461 +       int ret;
38462 +       struct au_xino *xi;
38463 +
38464 +       ret = 0;
38465 +       xi = br->br_xino;
38466 +       if (xi) {
38467 +               br->br_xino = NULL;
38468 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38469 +       }
38470 +
38471 +       return ret;
38472 +}
38473 +
38474 +/* ---------------------------------------------------------------------- */
38475 +
38476 +/*
38477 + * xino mount option handlers
38478 + */
38479 +
38480 +/* xino bitmap */
38481 +static void xino_clear_xib(struct super_block *sb)
38482 +{
38483 +       struct au_sbinfo *sbinfo;
38484 +
38485 +       SiMustWriteLock(sb);
38486 +
38487 +       sbinfo = au_sbi(sb);
38488 +       if (sbinfo->si_xib)
38489 +               fput(sbinfo->si_xib);
38490 +       sbinfo->si_xib = NULL;
38491 +       if (sbinfo->si_xib_buf)
38492 +               free_page((unsigned long)sbinfo->si_xib_buf);
38493 +       sbinfo->si_xib_buf = NULL;
38494 +}
38495 +
38496 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38497 +{
38498 +       int err;
38499 +       loff_t pos;
38500 +       struct au_sbinfo *sbinfo;
38501 +       struct file *file;
38502 +       struct super_block *xi_sb;
38503 +
38504 +       SiMustWriteLock(sb);
38505 +
38506 +       sbinfo = au_sbi(sb);
38507 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38508 +       err = PTR_ERR(file);
38509 +       if (IS_ERR(file))
38510 +               goto out;
38511 +       if (sbinfo->si_xib)
38512 +               fput(sbinfo->si_xib);
38513 +       sbinfo->si_xib = file;
38514 +       xi_sb = file_inode(file)->i_sb;
38515 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38516 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38517 +               err = -EIO;
38518 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38519 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38520 +               goto out_unset;
38521 +       }
38522 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38523 +
38524 +       err = -ENOMEM;
38525 +       if (!sbinfo->si_xib_buf)
38526 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38527 +       if (unlikely(!sbinfo->si_xib_buf))
38528 +               goto out_unset;
38529 +
38530 +       sbinfo->si_xib_last_pindex = 0;
38531 +       sbinfo->si_xib_next_bit = 0;
38532 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38533 +               pos = 0;
38534 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38535 +               if (unlikely(err != PAGE_SIZE))
38536 +                       goto out_free;
38537 +       }
38538 +       err = 0;
38539 +       goto out; /* success */
38540 +
38541 +out_free:
38542 +       if (sbinfo->si_xib_buf)
38543 +               free_page((unsigned long)sbinfo->si_xib_buf);
38544 +       sbinfo->si_xib_buf = NULL;
38545 +       if (err >= 0)
38546 +               err = -EIO;
38547 +out_unset:
38548 +       fput(sbinfo->si_xib);
38549 +       sbinfo->si_xib = NULL;
38550 +out:
38551 +       AuTraceErr(err);
38552 +       return err;
38553 +}
38554 +
38555 +/* xino for each branch */
38556 +static void xino_clear_br(struct super_block *sb)
38557 +{
38558 +       aufs_bindex_t bindex, bbot;
38559 +       struct au_branch *br;
38560 +
38561 +       bbot = au_sbbot(sb);
38562 +       for (bindex = 0; bindex <= bbot; bindex++) {
38563 +               br = au_sbr(sb, bindex);
38564 +               AuDebugOn(!br);
38565 +               au_xino_put(br);
38566 +       }
38567 +}
38568 +
38569 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38570 +                                 aufs_bindex_t bshared)
38571 +{
38572 +       struct au_branch *brshared;
38573 +
38574 +       brshared = au_sbr(sb, bshared);
38575 +       AuDebugOn(!brshared->br_xino);
38576 +       AuDebugOn(!brshared->br_xino->xi_file);
38577 +       if (br->br_xino != brshared->br_xino) {
38578 +               au_xino_get(brshared);
38579 +               au_xino_put(br);
38580 +               br->br_xino = brshared->br_xino;
38581 +       }
38582 +}
38583 +
38584 +struct au_xino_do_set_br {
38585 +       struct au_branch *br;
38586 +       ino_t h_ino;
38587 +       aufs_bindex_t bshared;
38588 +};
38589 +
38590 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38591 +                            struct au_xino_do_set_br *args)
38592 +{
38593 +       int err;
38594 +       struct au_xi_calc calc;
38595 +       struct file *file;
38596 +       struct au_branch *br;
38597 +       struct au_xi_new xinew = {
38598 +               .base = path
38599 +       };
38600 +
38601 +       br = args->br;
38602 +       xinew.xi = br->br_xino;
38603 +       au_xi_calc(sb, args->h_ino, &calc);
38604 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38605 +       if (args->bshared >= 0)
38606 +               /* shared xino */
38607 +               au_xino_set_br_shared(sb, br, args->bshared);
38608 +       else if (!xinew.xi) {
38609 +               /* new xino */
38610 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38611 +               if (unlikely(err))
38612 +                       goto out;
38613 +       }
38614 +
38615 +       /* force re-creating */
38616 +       xinew.xi = br->br_xino;
38617 +       xinew.idx = calc.idx;
38618 +       mutex_lock(&xinew.xi->xi_mtx);
38619 +       file = au_xi_new(sb, &xinew);
38620 +       mutex_unlock(&xinew.xi->xi_mtx);
38621 +       err = PTR_ERR(file);
38622 +       if (IS_ERR(file))
38623 +               goto out;
38624 +       AuDebugOn(!file);
38625 +
38626 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38627 +       if (unlikely(err))
38628 +               au_xino_put(br);
38629 +
38630 +out:
38631 +       AuTraceErr(err);
38632 +       return err;
38633 +}
38634 +
38635 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38636 +{
38637 +       int err;
38638 +       aufs_bindex_t bindex, bbot;
38639 +       struct au_xino_do_set_br args;
38640 +       struct inode *inode;
38641 +
38642 +       SiMustWriteLock(sb);
38643 +
38644 +       bbot = au_sbbot(sb);
38645 +       inode = d_inode(sb->s_root);
38646 +       for (bindex = 0; bindex <= bbot; bindex++) {
38647 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38648 +               args.br = au_sbr(sb, bindex);
38649 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38650 +               err = au_xino_do_set_br(sb, path, &args);
38651 +               if (unlikely(err))
38652 +                       break;
38653 +       }
38654 +
38655 +       AuTraceErr(err);
38656 +       return err;
38657 +}
38658 +
38659 +void au_xino_clr(struct super_block *sb)
38660 +{
38661 +       struct au_sbinfo *sbinfo;
38662 +
38663 +       au_xigen_clr(sb);
38664 +       xino_clear_xib(sb);
38665 +       xino_clear_br(sb);
38666 +       dbgaufs_brs_del(sb, 0);
38667 +       sbinfo = au_sbi(sb);
38668 +       /* lvalue, do not call au_mntflags() */
38669 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38670 +}
38671 +
38672 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38673 +{
38674 +       int err, skip;
38675 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38676 +       struct qstr *dname, *cur_name;
38677 +       struct file *cur_xino;
38678 +       struct au_sbinfo *sbinfo;
38679 +       struct path *path, *cur_path;
38680 +
38681 +       SiMustWriteLock(sb);
38682 +
38683 +       err = 0;
38684 +       sbinfo = au_sbi(sb);
38685 +       path = &xiopt->file->f_path;
38686 +       dentry = path->dentry;
38687 +       parent = dget_parent(dentry);
38688 +       if (remount) {
38689 +               skip = 0;
38690 +               cur_xino = sbinfo->si_xib;
38691 +               if (cur_xino) {
38692 +                       cur_path = &cur_xino->f_path;
38693 +                       cur_dentry = cur_path->dentry;
38694 +                       cur_parent = dget_parent(cur_dentry);
38695 +                       cur_name = &cur_dentry->d_name;
38696 +                       dname = &dentry->d_name;
38697 +                       skip = (cur_parent == parent
38698 +                               && au_qstreq(dname, cur_name));
38699 +                       dput(cur_parent);
38700 +               }
38701 +               if (skip)
38702 +                       goto out;
38703 +       }
38704 +
38705 +       au_opt_set(sbinfo->si_mntflags, XINO);
38706 +       err = au_xino_set_xib(sb, path);
38707 +       /* si_x{read,write} are set */
38708 +       if (!err)
38709 +               err = au_xigen_set(sb, path);
38710 +       if (!err)
38711 +               err = au_xino_set_br(sb, path);
38712 +       if (!err) {
38713 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38714 +               goto out; /* success */
38715 +       }
38716 +
38717 +       /* reset all */
38718 +       AuIOErr("failed setting xino(%d).\n", err);
38719 +       au_xino_clr(sb);
38720 +
38721 +out:
38722 +       dput(parent);
38723 +       return err;
38724 +}
38725 +
38726 +/*
38727 + * create a xinofile at the default place/path.
38728 + */
38729 +struct file *au_xino_def(struct super_block *sb)
38730 +{
38731 +       struct file *file;
38732 +       char *page, *p;
38733 +       struct au_branch *br;
38734 +       struct super_block *h_sb;
38735 +       struct path path;
38736 +       aufs_bindex_t bbot, bindex, bwr;
38737 +
38738 +       br = NULL;
38739 +       bbot = au_sbbot(sb);
38740 +       bwr = -1;
38741 +       for (bindex = 0; bindex <= bbot; bindex++) {
38742 +               br = au_sbr(sb, bindex);
38743 +               if (au_br_writable(br->br_perm)
38744 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38745 +                       bwr = bindex;
38746 +                       break;
38747 +               }
38748 +       }
38749 +
38750 +       if (bwr >= 0) {
38751 +               file = ERR_PTR(-ENOMEM);
38752 +               page = (void *)__get_free_page(GFP_NOFS);
38753 +               if (unlikely(!page))
38754 +                       goto out;
38755 +               path.mnt = au_br_mnt(br);
38756 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38757 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38758 +               file = (void *)p;
38759 +               if (!IS_ERR(p)) {
38760 +                       strcat(p, "/" AUFS_XINO_FNAME);
38761 +                       AuDbg("%s\n", p);
38762 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38763 +               }
38764 +               free_page((unsigned long)page);
38765 +       } else {
38766 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38767 +                                     /*wbrtop*/0);
38768 +               if (IS_ERR(file))
38769 +                       goto out;
38770 +               h_sb = file->f_path.dentry->d_sb;
38771 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38772 +                       pr_err("xino doesn't support %s(%s)\n",
38773 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38774 +                       fput(file);
38775 +                       file = ERR_PTR(-EINVAL);
38776 +               }
38777 +       }
38778 +
38779 +out:
38780 +       return file;
38781 +}
38782 +
38783 +/* ---------------------------------------------------------------------- */
38784 +
38785 +/*
38786 + * initialize the xinofile for the specified branch @br
38787 + * at the place/path where @base_file indicates.
38788 + * test whether another branch is on the same filesystem or not,
38789 + * if found then share the xinofile with another branch.
38790 + */
38791 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38792 +                   struct path *base)
38793 +{
38794 +       int err;
38795 +       struct au_xino_do_set_br args = {
38796 +               .h_ino  = h_ino,
38797 +               .br     = br
38798 +       };
38799 +
38800 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38801 +                                      au_br_sb(br));
38802 +       err = au_xino_do_set_br(sb, base, &args);
38803 +       if (unlikely(err))
38804 +               au_xino_put(br);
38805 +
38806 +       return err;
38807 +}
38808 +
38809 +/* ---------------------------------------------------------------------- */
38810 +
38811 +/*
38812 + * get an unused inode number from bitmap
38813 + */
38814 +ino_t au_xino_new_ino(struct super_block *sb)
38815 +{
38816 +       ino_t ino;
38817 +       unsigned long *p, pindex, ul, pend;
38818 +       struct au_sbinfo *sbinfo;
38819 +       struct file *file;
38820 +       int free_bit, err;
38821 +
38822 +       if (!au_opt_test(au_mntflags(sb), XINO))
38823 +               return iunique(sb, AUFS_FIRST_INO);
38824 +
38825 +       sbinfo = au_sbi(sb);
38826 +       mutex_lock(&sbinfo->si_xib_mtx);
38827 +       p = sbinfo->si_xib_buf;
38828 +       free_bit = sbinfo->si_xib_next_bit;
38829 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38830 +               goto out; /* success */
38831 +       free_bit = find_first_zero_bit(p, page_bits);
38832 +       if (free_bit < page_bits)
38833 +               goto out; /* success */
38834 +
38835 +       pindex = sbinfo->si_xib_last_pindex;
38836 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38837 +               err = xib_pindex(sb, ul);
38838 +               if (unlikely(err))
38839 +                       goto out_err;
38840 +               free_bit = find_first_zero_bit(p, page_bits);
38841 +               if (free_bit < page_bits)
38842 +                       goto out; /* success */
38843 +       }
38844 +
38845 +       file = sbinfo->si_xib;
38846 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38847 +       for (ul = pindex + 1; ul <= pend; ul++) {
38848 +               err = xib_pindex(sb, ul);
38849 +               if (unlikely(err))
38850 +                       goto out_err;
38851 +               free_bit = find_first_zero_bit(p, page_bits);
38852 +               if (free_bit < page_bits)
38853 +                       goto out; /* success */
38854 +       }
38855 +       BUG();
38856 +
38857 +out:
38858 +       set_bit(free_bit, p);
38859 +       sbinfo->si_xib_next_bit = free_bit + 1;
38860 +       pindex = sbinfo->si_xib_last_pindex;
38861 +       mutex_unlock(&sbinfo->si_xib_mtx);
38862 +       ino = xib_calc_ino(pindex, free_bit);
38863 +       AuDbg("i%lu\n", (unsigned long)ino);
38864 +       return ino;
38865 +out_err:
38866 +       mutex_unlock(&sbinfo->si_xib_mtx);
38867 +       AuDbg("i0\n");
38868 +       return 0;
38869 +}
38870 +
38871 +/* for s_op->delete_inode() */
38872 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38873 +{
38874 +       int err;
38875 +       unsigned int mnt_flags;
38876 +       aufs_bindex_t bindex, bbot, bi;
38877 +       unsigned char try_trunc;
38878 +       struct au_iinfo *iinfo;
38879 +       struct super_block *sb;
38880 +       struct au_hinode *hi;
38881 +       struct inode *h_inode;
38882 +       struct au_branch *br;
38883 +       struct au_xi_calc calc;
38884 +       struct file *file;
38885 +
38886 +       AuDebugOn(au_is_bad_inode(inode));
38887 +
38888 +       sb = inode->i_sb;
38889 +       mnt_flags = au_mntflags(sb);
38890 +       if (!au_opt_test(mnt_flags, XINO)
38891 +           || inode->i_ino == AUFS_ROOT_INO)
38892 +               return;
38893 +
38894 +       if (unlinked) {
38895 +               au_xigen_inc(inode);
38896 +               au_xib_clear_bit(inode);
38897 +       }
38898 +
38899 +       iinfo = au_ii(inode);
38900 +       bindex = iinfo->ii_btop;
38901 +       if (bindex < 0)
38902 +               return;
38903 +
38904 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38905 +       hi = au_hinode(iinfo, bindex);
38906 +       bbot = iinfo->ii_bbot;
38907 +       for (; bindex <= bbot; bindex++, hi++) {
38908 +               h_inode = hi->hi_inode;
38909 +               if (!h_inode
38910 +                   || (!unlinked && h_inode->i_nlink))
38911 +                       continue;
38912 +
38913 +               /* inode may not be revalidated */
38914 +               bi = au_br_index(sb, hi->hi_id);
38915 +               if (bi < 0)
38916 +                       continue;
38917 +
38918 +               br = au_sbr(sb, bi);
38919 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38920 +               file = au_xino_file(br->br_xino, calc.idx);
38921 +               if (IS_ERR_OR_NULL(file))
38922 +                       continue;
38923 +
38924 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38925 +               if (!err && try_trunc
38926 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38927 +                       xino_try_trunc(sb, br);
38928 +       }
38929 +}
38930 +
38931 +/* ---------------------------------------------------------------------- */
38932 +
38933 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38934 +{
38935 +       int found, total, i;
38936 +
38937 +       found = -1;
38938 +       total = xi->xi_nondir.total;
38939 +       for (i = 0; i < total; i++) {
38940 +               if (xi->xi_nondir.array[i] != h_ino)
38941 +                       continue;
38942 +               found = i;
38943 +               break;
38944 +       }
38945 +
38946 +       return found;
38947 +}
38948 +
38949 +static int au_xinondir_expand(struct au_xino *xi)
38950 +{
38951 +       int err, sz;
38952 +       ino_t *p;
38953 +
38954 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38955 +
38956 +       err = -ENOMEM;
38957 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38958 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38959 +               goto out;
38960 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38961 +                        /*may_shrink*/0);
38962 +       if (p) {
38963 +               xi->xi_nondir.array = p;
38964 +               xi->xi_nondir.total <<= 1;
38965 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38966 +               err = 0;
38967 +       }
38968 +
38969 +out:
38970 +       return err;
38971 +}
38972 +
38973 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38974 +                      ino_t h_ino, int idx)
38975 +{
38976 +       struct au_xino *xi;
38977 +
38978 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38979 +       xi = au_sbr(sb, bindex)->br_xino;
38980 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38981 +
38982 +       spin_lock(&xi->xi_nondir.spin);
38983 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38984 +       xi->xi_nondir.array[idx] = 0;
38985 +       spin_unlock(&xi->xi_nondir.spin);
38986 +       wake_up_all(&xi->xi_nondir.wqh);
38987 +}
38988 +
38989 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38990 +                     int *idx)
38991 +{
38992 +       int err, found, empty;
38993 +       struct au_xino *xi;
38994 +
38995 +       err = 0;
38996 +       *idx = -1;
38997 +       if (!au_opt_test(au_mntflags(sb), XINO))
38998 +               goto out; /* no xino */
38999 +
39000 +       xi = au_sbr(sb, bindex)->br_xino;
39001 +
39002 +again:
39003 +       spin_lock(&xi->xi_nondir.spin);
39004 +       found = au_xinondir_find(xi, h_ino);
39005 +       if (found == -1) {
39006 +               empty = au_xinondir_find(xi, /*h_ino*/0);
39007 +               if (empty == -1) {
39008 +                       empty = xi->xi_nondir.total;
39009 +                       err = au_xinondir_expand(xi);
39010 +                       if (unlikely(err))
39011 +                               goto out_unlock;
39012 +               }
39013 +               xi->xi_nondir.array[empty] = h_ino;
39014 +               *idx = empty;
39015 +       } else {
39016 +               spin_unlock(&xi->xi_nondir.spin);
39017 +               wait_event(xi->xi_nondir.wqh,
39018 +                          xi->xi_nondir.array[found] != h_ino);
39019 +               goto again;
39020 +       }
39021 +
39022 +out_unlock:
39023 +       spin_unlock(&xi->xi_nondir.spin);
39024 +out:
39025 +       return err;
39026 +}
39027 +
39028 +/* ---------------------------------------------------------------------- */
39029 +
39030 +int au_xino_path(struct seq_file *seq, struct file *file)
39031 +{
39032 +       int err;
39033 +
39034 +       err = au_seq_path(seq, &file->f_path);
39035 +       if (unlikely(err))
39036 +               goto out;
39037 +
39038 +#define Deleted "\\040(deleted)"
39039 +       seq->count -= sizeof(Deleted) - 1;
39040 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
39041 +                        sizeof(Deleted) - 1));
39042 +#undef Deleted
39043 +
39044 +out:
39045 +       return err;
39046 +}
39047 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
39048 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
39049 +++ linux/include/uapi/linux/aufs_type.h        2023-10-31 09:31:04.203214083 +0100
39050 @@ -0,0 +1,452 @@
39051 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
39052 +/*
39053 + * Copyright (C) 2005-2022 Junjiro R. Okajima
39054 + *
39055 + * This program is free software; you can redistribute it and/or modify
39056 + * it under the terms of the GNU General Public License as published by
39057 + * the Free Software Foundation; either version 2 of the License, or
39058 + * (at your option) any later version.
39059 + *
39060 + * This program is distributed in the hope that it will be useful,
39061 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
39062 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39063 + * GNU General Public License for more details.
39064 + *
39065 + * You should have received a copy of the GNU General Public License
39066 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
39067 + */
39068 +
39069 +#ifndef __AUFS_TYPE_H__
39070 +#define __AUFS_TYPE_H__
39071 +
39072 +#define AUFS_NAME      "aufs"
39073 +
39074 +#ifdef __KERNEL__
39075 +/*
39076 + * define it before including all other headers.
39077 + * sched.h may use pr_* macros before defining "current", so define the
39078 + * no-current version first, and re-define later.
39079 + */
39080 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39081 +#include <linux/sched.h>
39082 +#undef pr_fmt
39083 +#define pr_fmt(fmt) \
39084 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39085 +               (int)sizeof(current->comm), current->comm, current->pid
39086 +#include <linux/limits.h>
39087 +#else
39088 +#include <stdint.h>
39089 +#include <sys/types.h>
39090 +#include <limits.h>
39091 +#endif /* __KERNEL__ */
39092 +
39093 +#define AUFS_VERSION   "6.x-rcN-20230925"
39094 +
39095 +/* todo? move this to linux-2.6.19/include/magic.h */
39096 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39097 +
39098 +/* ---------------------------------------------------------------------- */
39099 +
39100 +#ifdef __KERNEL__
39101 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
39102 +typedef int8_t aufs_bindex_t;
39103 +#define AUFS_BRANCH_MAX 127
39104 +#else
39105 +typedef int16_t aufs_bindex_t;
39106 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
39107 +#define AUFS_BRANCH_MAX 511
39108 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39109 +#define AUFS_BRANCH_MAX 1023
39110 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39111 +#define AUFS_BRANCH_MAX 32767
39112 +#endif
39113 +#endif
39114 +
39115 +#ifndef AUFS_BRANCH_MAX
39116 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39117 +#endif
39118 +#endif /* __KERNEL__ */
39119 +
39120 +/* ---------------------------------------------------------------------- */
39121 +
39122 +#define AUFS_FSTYPE            AUFS_NAME
39123 +
39124 +#define AUFS_ROOT_INO          2
39125 +#define AUFS_FIRST_INO         11
39126 +
39127 +#define AUFS_WH_PFX            ".wh."
39128 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
39129 +#define AUFS_WH_TMP_LEN                4
39130 +/* a limit for rmdir/rename a dir and copyup */
39131 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
39132 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
39133 +                               - 1                     /* dot */\
39134 +                               - AUFS_WH_TMP_LEN)      /* hex */
39135 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
39136 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
39137 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
39138 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
39139 +#define AUFS_DIRWH_DEF         3
39140 +#define AUFS_RDCACHE_DEF       10 /* seconds */
39141 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
39142 +#define AUFS_RDBLK_DEF         512 /* bytes */
39143 +#define AUFS_RDHASH_DEF                32
39144 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
39145 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
39146 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
39147 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
39148 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
39149 +
39150 +/* pseudo-link maintenace under /proc */
39151 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
39152 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
39153 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39154 +
39155 +/* dirren, renamed dir */
39156 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
39157 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
39158 +/* whiteouted doubly */
39159 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
39160 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39161 +
39162 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39163 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39164 +
39165 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39166 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39167 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39168 +
39169 +/* doubly whiteouted */
39170 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39171 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39172 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39173 +
39174 +/* branch permissions and attributes */
39175 +#define AUFS_BRPERM_RW         "rw"
39176 +#define AUFS_BRPERM_RO         "ro"
39177 +#define AUFS_BRPERM_RR         "rr"
39178 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39179 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39180 +#define AUFS_BRATTR_FHSM       "fhsm"
39181 +#define AUFS_BRATTR_UNPIN      "unpin"
39182 +#define AUFS_BRATTR_ICEX       "icex"
39183 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39184 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39185 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39186 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39187 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39188 +#define AUFS_BRRATTR_WH                "wh"
39189 +#define AUFS_BRWATTR_NLWH      "nolwh"
39190 +#define AUFS_BRWATTR_MOO       "moo"
39191 +
39192 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39193 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39194 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39195 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39196 +
39197 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39198 +#define AuBrAttr_COO_ALL       (1 << 4)
39199 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39200 +
39201 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39202 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39203 +                                                  branch. meaningless since
39204 +                                                  linux-3.18-rc1 */
39205 +
39206 +/* ignore error in copying XATTR */
39207 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39208 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39209 +#define AuBrAttr_ICEX_TR       (1 << 9)
39210 +#define AuBrAttr_ICEX_USR      (1 << 10)
39211 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39212 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39213 +                                | AuBrAttr_ICEX_SYS    \
39214 +                                | AuBrAttr_ICEX_TR     \
39215 +                                | AuBrAttr_ICEX_USR    \
39216 +                                | AuBrAttr_ICEX_OTH)
39217 +
39218 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39219 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39220 +
39221 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39222 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39223 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39224 +
39225 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39226 +
39227 +/* #warning test userspace */
39228 +#ifdef __KERNEL__
39229 +#ifndef CONFIG_AUFS_FHSM
39230 +#undef AuBrAttr_FHSM
39231 +#define AuBrAttr_FHSM          0
39232 +#endif
39233 +#ifndef CONFIG_AUFS_XATTR
39234 +#undef AuBrAttr_ICEX
39235 +#define AuBrAttr_ICEX          0
39236 +#undef AuBrAttr_ICEX_SEC
39237 +#define AuBrAttr_ICEX_SEC      0
39238 +#undef AuBrAttr_ICEX_SYS
39239 +#define AuBrAttr_ICEX_SYS      0
39240 +#undef AuBrAttr_ICEX_TR
39241 +#define AuBrAttr_ICEX_TR       0
39242 +#undef AuBrAttr_ICEX_USR
39243 +#define AuBrAttr_ICEX_USR      0
39244 +#undef AuBrAttr_ICEX_OTH
39245 +#define AuBrAttr_ICEX_OTH      0
39246 +#endif
39247 +#endif
39248 +
39249 +/* the longest combination */
39250 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39251 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39252 +                              "+" AUFS_BRATTR_COO_REG          \
39253 +                              "+" AUFS_BRATTR_FHSM             \
39254 +                              "+" AUFS_BRATTR_UNPIN            \
39255 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39256 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39257 +                              "+" AUFS_BRATTR_ICEX_USR         \
39258 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39259 +                              "+" AUFS_BRWATTR_NLWH)
39260 +
39261 +typedef struct {
39262 +       char a[AuBrPermStrSz];
39263 +} au_br_perm_str_t;
39264 +
39265 +static inline int au_br_writable(int brperm)
39266 +{
39267 +       return brperm & AuBrPerm_RW;
39268 +}
39269 +
39270 +static inline int au_br_whable(int brperm)
39271 +{
39272 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39273 +}
39274 +
39275 +static inline int au_br_wh_linkable(int brperm)
39276 +{
39277 +       return !(brperm & AuBrWAttr_NoLinkWH);
39278 +}
39279 +
39280 +static inline int au_br_cmoo(int brperm)
39281 +{
39282 +       return brperm & AuBrAttr_CMOO_Mask;
39283 +}
39284 +
39285 +static inline int au_br_fhsm(int brperm)
39286 +{
39287 +       return brperm & AuBrAttr_FHSM;
39288 +}
39289 +
39290 +/* ---------------------------------------------------------------------- */
39291 +
39292 +/* ioctl */
39293 +enum {
39294 +       /* readdir in userspace */
39295 +       AuCtl_RDU,
39296 +       AuCtl_RDU_INO,
39297 +
39298 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39299 +       AuCtl_IBUSY,    /* busy inode */
39300 +       AuCtl_MVDOWN,   /* move-down */
39301 +       AuCtl_BR,       /* info about branches */
39302 +       AuCtl_FHSM_FD   /* connection for fhsm */
39303 +};
39304 +
39305 +/* borrowed from linux/include/linux/kernel.h */
39306 +#ifndef ALIGN
39307 +#ifdef _GNU_SOURCE
39308 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39309 +#else
39310 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39311 +#endif
39312 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39313 +#endif
39314 +
39315 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39316 +#ifndef __aligned
39317 +#define __aligned(x)                   __attribute__((aligned(x)))
39318 +#endif
39319 +
39320 +#ifdef __KERNEL__
39321 +#ifndef __packed
39322 +#define __packed                       __attribute__((packed))
39323 +#endif
39324 +#endif
39325 +
39326 +struct au_rdu_cookie {
39327 +       uint64_t        h_pos;
39328 +       int16_t         bindex;
39329 +       uint8_t         flags;
39330 +       uint8_t         pad;
39331 +       uint32_t        generation;
39332 +} __aligned(8);
39333 +
39334 +struct au_rdu_ent {
39335 +       uint64_t        ino;
39336 +       int16_t         bindex;
39337 +       uint8_t         type;
39338 +       uint8_t         nlen;
39339 +       uint8_t         wh;
39340 +       char            name[];
39341 +} __aligned(8);
39342 +
39343 +static inline int au_rdu_len(int nlen)
39344 +{
39345 +       /* include the terminating NULL */
39346 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39347 +                    sizeof(uint64_t));
39348 +}
39349 +
39350 +union au_rdu_ent_ul {
39351 +       struct au_rdu_ent __user        *e;
39352 +       uint64_t                        ul;
39353 +};
39354 +
39355 +enum {
39356 +       AufsCtlRduV_SZ,
39357 +       AufsCtlRduV_End
39358 +};
39359 +
39360 +struct aufs_rdu {
39361 +       /* input */
39362 +       union {
39363 +               uint64_t        sz;     /* AuCtl_RDU */
39364 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39365 +       };
39366 +       union au_rdu_ent_ul     ent;
39367 +       uint16_t                verify[AufsCtlRduV_End];
39368 +
39369 +       /* input/output */
39370 +       uint32_t                blk;
39371 +
39372 +       /* output */
39373 +       union au_rdu_ent_ul     tail;
39374 +       /* number of entries which were added in a single call */
39375 +       uint64_t                rent;
39376 +       uint8_t                 full;
39377 +       uint8_t                 shwh;
39378 +
39379 +       struct au_rdu_cookie    cookie;
39380 +} __aligned(8);
39381 +
39382 +/* ---------------------------------------------------------------------- */
39383 +
39384 +/* dirren. the branch is identified by the filename who contains this */
39385 +struct au_drinfo {
39386 +       uint64_t ino;
39387 +       union {
39388 +               uint8_t oldnamelen;
39389 +               uint64_t _padding;
39390 +       };
39391 +       uint8_t oldname[];
39392 +} __aligned(8);
39393 +
39394 +struct au_drinfo_fdata {
39395 +       uint32_t magic;
39396 +       struct au_drinfo drinfo;
39397 +} __aligned(8);
39398 +
39399 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39400 +/* future */
39401 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39402 +
39403 +/* ---------------------------------------------------------------------- */
39404 +
39405 +struct aufs_wbr_fd {
39406 +       uint32_t        oflags;
39407 +       int16_t         brid;
39408 +} __aligned(8);
39409 +
39410 +/* ---------------------------------------------------------------------- */
39411 +
39412 +struct aufs_ibusy {
39413 +       uint64_t        ino, h_ino;
39414 +       int16_t         bindex;
39415 +} __aligned(8);
39416 +
39417 +/* ---------------------------------------------------------------------- */
39418 +
39419 +/* error code for move-down */
39420 +/* the actual message strings are implemented in aufs-util.git */
39421 +enum {
39422 +       EAU_MVDOWN_OPAQUE = 1,
39423 +       EAU_MVDOWN_WHITEOUT,
39424 +       EAU_MVDOWN_UPPER,
39425 +       EAU_MVDOWN_BOTTOM,
39426 +       EAU_MVDOWN_NOUPPER,
39427 +       EAU_MVDOWN_NOLOWERBR,
39428 +       EAU_Last
39429 +};
39430 +
39431 +/* flags for move-down */
39432 +#define AUFS_MVDOWN_DMSG       1
39433 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39434 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39435 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39436 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39437 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39438 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39439 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39440 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39441 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39442 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39443 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39444 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39445 +
39446 +/* index for move-down */
39447 +enum {
39448 +       AUFS_MVDOWN_UPPER,
39449 +       AUFS_MVDOWN_LOWER,
39450 +       AUFS_MVDOWN_NARRAY
39451 +};
39452 +
39453 +/*
39454 + * additional info of move-down
39455 + * number of free blocks and inodes.
39456 + * subset of struct kstatfs, but smaller and always 64bit.
39457 + */
39458 +struct aufs_stfs {
39459 +       uint64_t        f_blocks;
39460 +       uint64_t        f_bavail;
39461 +       uint64_t        f_files;
39462 +       uint64_t        f_ffree;
39463 +};
39464 +
39465 +struct aufs_stbr {
39466 +       int16_t                 brid;   /* optional input */
39467 +       int16_t                 bindex; /* output */
39468 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39469 +} __aligned(8);
39470 +
39471 +struct aufs_mvdown {
39472 +       uint32_t                flags;                  /* input/output */
39473 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39474 +       int8_t                  au_errno;               /* output */
39475 +} __aligned(8);
39476 +
39477 +/* ---------------------------------------------------------------------- */
39478 +
39479 +union aufs_brinfo {
39480 +       /* PATH_MAX may differ between kernel-space and user-space */
39481 +       char    _spacer[4096];
39482 +       struct {
39483 +               int16_t id;
39484 +               int     perm;
39485 +               char    path[];
39486 +       };
39487 +} __aligned(8);
39488 +
39489 +/* ---------------------------------------------------------------------- */
39490 +
39491 +#define AuCtlType              'A'
39492 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39493 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39494 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39495 +                                    struct aufs_wbr_fd)
39496 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39497 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39498 +                                     struct aufs_mvdown)
39499 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39500 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39501 +
39502 +#endif /* __AUFS_TYPE_H__ */
39503 SPDX-License-Identifier: GPL-2.0
39504 aufs6.x-rcN loopback patch
39505
39506 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39507 index 1fefc6a8d049..86f1f66305d2 100644
39508 --- a/drivers/block/loop.c
39509 +++ b/drivers/block/loop.c
39510 @@ -54,7 +54,7 @@ struct loop_device {
39511         int             lo_flags;
39512         char            lo_file_name[LO_NAME_SIZE];
39513  
39514 -       struct file *   lo_backing_file;
39515 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39516         struct block_device *lo_device;
39517  
39518         gfp_t           old_gfp_mask;
39519 @@ -510,6 +510,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39520                                 lo->use_dio);
39521  }
39522  
39523 +static struct file *loop_real_file(struct file *file)
39524 +{
39525 +       struct file *f = NULL;
39526 +
39527 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39528 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39529 +       return f;
39530 +}
39531 +
39532  static void loop_reread_partitions(struct loop_device *lo)
39533  {
39534         int rc;
39535 @@ -567,6 +576,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39536  {
39537         struct file *file = fget(arg);
39538         struct file *old_file;
39539 +       struct file *f, *virt_file = NULL, *old_virt_file;
39540         int error;
39541         bool partscan;
39542         bool is_loop;
39543 @@ -590,11 +600,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39544         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39545                 goto out_err;
39546  
39547 +       f = loop_real_file(file);
39548 +       if (f) {
39549 +               virt_file = file;
39550 +               file = f;
39551 +               get_file(file);
39552 +       }
39553 +
39554         error = loop_validate_file(file, bdev);
39555         if (error)
39556                 goto out_err;
39557  
39558         old_file = lo->lo_backing_file;
39559 +       old_virt_file = lo->lo_backing_virt_file;
39560  
39561         error = -EINVAL;
39562  
39563 @@ -607,6 +625,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39564         blk_mq_freeze_queue(lo->lo_queue);
39565         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39566         lo->lo_backing_file = file;
39567 +       lo->lo_backing_virt_file = virt_file;
39568         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39569         mapping_set_gfp_mask(file->f_mapping,
39570                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39571 @@ -629,6 +648,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39572          * dependency.
39573          */
39574         fput(old_file);
39575 +       if (old_virt_file)
39576 +               fput(old_virt_file);
39577         if (partscan)
39578                 loop_reread_partitions(lo);
39579  
39580 @@ -642,6 +663,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39581         loop_global_unlock(lo, is_loop);
39582  out_putf:
39583         fput(file);
39584 +       if (virt_file)
39585 +               fput(virt_file);
39586         goto done;
39587  }
39588  
39589 @@ -1013,6 +1036,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39590                           const struct loop_config *config)
39591  {
39592         struct file *file = fget(config->fd);
39593 +       struct file *f, *virt_file = NULL;
39594         struct inode *inode;
39595         struct address_space *mapping;
39596         int error;
39597 @@ -1028,6 +1052,13 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39598         /* This is safe, since we have a reference from open(). */
39599         __module_get(THIS_MODULE);
39600  
39601 +       f = loop_real_file(file);
39602 +       if (f) {
39603 +               virt_file = file;
39604 +               file = f;
39605 +               get_file(file);
39606 +       }
39607 +
39608         /*
39609          * If we don't hold exclusive handle for the device, upgrade to it
39610          * here to avoid changing device under exclusive owner.
39611 @@ -1091,6 +1122,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39612         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39613         lo->lo_device = bdev;
39614         lo->lo_backing_file = file;
39615 +       lo->lo_backing_virt_file = virt_file;
39616         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39617         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39618  
39619 @@ -1146,6 +1178,8 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39620                 bd_abort_claiming(bdev, loop_configure);
39621  out_putf:
39622         fput(file);
39623 +       if (virt_file)
39624 +               fput(virt_file);
39625         /* This is safe: open() is still holding a reference. */
39626         module_put(THIS_MODULE);
39627         return error;
39628 @@ -1154,6 +1188,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39629  static void __loop_clr_fd(struct loop_device *lo, bool release)
39630  {
39631         struct file *filp;
39632 +       struct file *virt_filp = lo->lo_backing_virt_file;
39633         gfp_t gfp = lo->old_gfp_mask;
39634  
39635         if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
39636 @@ -1170,6 +1205,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39637         spin_lock_irq(&lo->lo_lock);
39638         filp = lo->lo_backing_file;
39639         lo->lo_backing_file = NULL;
39640 +       lo->lo_backing_virt_file = NULL;
39641         spin_unlock_irq(&lo->lo_lock);
39642  
39643         lo->lo_device = NULL;
39644 @@ -1232,6 +1268,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39645          * fput can take open_mutex which is usually taken before lo_mutex.
39646          */
39647         fput(filp);
39648 +       if (virt_filp)
39649 +               fput(virt_filp);
39650  }
39651  
39652  static int loop_clr_fd(struct loop_device *lo)
39653 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39654 index fa8a517ffd0c..c18f7bcef81b 100644
39655 --- a/fs/aufs/f_op.c
39656 +++ b/fs/aufs/f_op.c
39657 @@ -311,7 +311,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39658         if (IS_ERR(h_file))
39659                 goto out;
39660  
39661 -       if (au_test_loopback_kthread()) {
39662 +       if (0 && au_test_loopback_kthread()) {
39663                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39664                 if (file->f_mapping != h_file->f_mapping) {
39665                         file->f_mapping = h_file->f_mapping;
39666 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39667 index 58043e31e5f3..e2bfae6f9d59 100644
39668 --- a/fs/aufs/loop.c
39669 +++ b/fs/aufs/loop.c
39670 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39671                 symbol_put(loop_backing_file);
39672         au_kfree_try_rcu(au_warn_loopback_array);
39673  }
39674 +
39675 +/* ---------------------------------------------------------------------- */
39676 +
39677 +/* support the loopback block device insude aufs */
39678 +
39679 +struct file *aufs_real_loop(struct file *file)
39680 +{
39681 +       struct file *f;
39682 +
39683 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39684 +       fi_read_lock(file);
39685 +       f = au_hf_top(file);
39686 +       fi_read_unlock(file);
39687 +       AuDebugOn(!f);
39688 +       return f;
39689 +}
39690 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39691 index 03d4908a6c03..34d356e181d5 100644
39692 --- a/fs/aufs/loop.h
39693 +++ b/fs/aufs/loop.h
39694 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39695  
39696  int au_loopback_init(void);
39697  void au_loopback_fin(void);
39698 +
39699 +struct file *aufs_real_loop(struct file *file);
39700  #else
39701  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39702  
39703 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39704  
39705  AuStubInt0(au_loopback_init, void)
39706  AuStubVoid(au_loopback_fin, void)
39707 +
39708 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39709  #endif /* BLK_DEV_LOOP */
39710  
39711  #endif /* __KERNEL__ */
39712 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39713 index 07d3412e950f..c4a00f620e57 100644
39714 --- a/fs/aufs/super.c
39715 +++ b/fs/aufs/super.c
39716 @@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39717         .show_options   = aufs_show_options,
39718         .statfs         = aufs_statfs,
39719         .put_super      = aufs_put_super,
39720 -       .sync_fs        = aufs_sync_fs
39721 +       .sync_fs        = aufs_sync_fs,
39722 +#ifdef CONFIG_AUFS_BDEV_LOOP
39723 +       .real_loop      = aufs_real_loop
39724 +#endif
39725  };
39726  
39727  /* ---------------------------------------------------------------------- */
39728 diff --git a/include/linux/fs.h b/include/linux/fs.h
39729 index dd5871d0c429..748091221f2d 100644
39730 --- a/include/linux/fs.h
39731 +++ b/include/linux/fs.h
39732 @@ -2056,6 +2056,11 @@ struct super_operations {
39733         long (*free_cached_objects)(struct super_block *,
39734                                     struct shrink_control *);
39735         void (*shutdown)(struct super_block *sb);
39736 +
39737 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39738 +       /* and aufs */
39739 +       struct file *(*real_loop)(struct file *);
39740 +#endif
39741  };
39742  
39743  /*
This page took 3.67831 seconds and 3 git commands to generate.