]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs.patch
9c8f2eb3a932275dc9c9bc02ef72e6951d07f3f1
[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 cc07a0cd3172..5ee4f1be48ff 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -316,6 +316,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 834f1c3dba46..b5c97dd026cd 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -137,3 +137,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 e0ad886d3163..a27659e1886b 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3380,6 +3380,19 @@ F:       include/uapi/linux/audit.h
33  F:     kernel/audit*
34  F:     lib/*audit.c
35  
36 +AUFS (advanced multi layered unification filesystem) FILESYSTEM
37 +M:     "J. R. Okajima" <hooanon05g@gmail.com>
38 +L:     aufs-users@lists.sourceforge.net (members only)
39 +L:     linux-unionfs@vger.kernel.org
40 +S:     Supported
41 +W:     http://aufs.sourceforge.net
42 +T:     git://github.com/sfjro/aufs4-linux.git
43 +F:     Documentation/ABI/testing/debugfs-aufs
44 +F:     Documentation/ABI/testing/sysfs-aufs
45 +F:     Documentation/filesystems/aufs/
46 +F:     fs/aufs/
47 +F:     include/uapi/linux/aufs_type.h
48 +
49  AUXILIARY DISPLAY DRIVERS
50  M:     Miguel Ojeda <ojeda@kernel.org>
51  S:     Maintained
52 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53 index bc31bb7072a2..131294601819 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 52e6d5fdab6b..519321f32f95 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 b622be119706..9ea58b7bb580 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 long arg)
103 +int setfl(int fd, struct file *filp, unsigned long arg)
104  {
105         struct inode * inode = file_inode(filp);
106         int error = 0;
107 @@ -64,6 +64,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
108  
109         if (filp->f_op->check_flags)
110                 error = filp->f_op->check_flags(arg);
111 +       if (!error && filp->f_op->setfl)
112 +               error = filp->f_op->setfl(filp, arg);
113         if (error)
114                 return error;
115  
116 diff --git a/fs/namespace.c b/fs/namespace.c
117 index 54847db5b819..ebb7b4b057ea 100644
118 --- a/fs/namespace.c
119 +++ b/fs/namespace.c
120 @@ -849,6 +849,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 3e06611d19ae..97a66df75d4c 100644
135 --- a/fs/splice.c
136 +++ b/fs/splice.c
137 @@ -865,8 +865,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 @@ -876,9 +876,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
149  /*
150   * Attempt to initiate a splice from a file to a pipe.
151   */
152 -static long do_splice_to(struct file *in, loff_t *ppos,
153 -                        struct pipe_inode_info *pipe, size_t len,
154 -                        unsigned int flags)
155 +long do_splice_to(struct file *in, loff_t *ppos,
156 +                 struct pipe_inode_info *pipe, size_t len,
157 +                 unsigned int flags)
158  {
159         unsigned int p_space;
160         int ret;
161 diff --git a/include/linux/fs.h b/include/linux/fs.h
162 index 21a981680856..1e3054e5367d 100644
163 --- a/include/linux/fs.h
164 +++ b/include/linux/fs.h
165 @@ -1066,6 +1066,7 @@ extern void fasync_free(struct fasync_struct *);
166  /* can be called from interrupts */
167  extern void kill_fasync(struct fasync_struct **, int, int);
168  
169 +extern int setfl(int fd, struct file *filp, unsigned long arg);
170  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
171  extern int f_setown(struct file *filp, unsigned long arg, int force);
172  extern void f_delown(struct file *filp);
173 @@ -1793,6 +1794,7 @@ struct file_operations {
174         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
175         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
176         int (*check_flags)(int);
177 +       int (*setfl)(struct file *, unsigned long);
178         int (*flock) (struct file *, int, struct file_lock *);
179         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
180         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
181 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
182 index b32256e9e944..16bd7230f44b 100644
183 --- a/include/linux/lockdep.h
184 +++ b/include/linux/lockdep.h
185 @@ -249,6 +249,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
186         return lock->key == key;
187  }
188  
189 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
190 +
191  /*
192   * Acquire a lock.
193   *
194 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
195 index 8f882f5881e8..6b9808f09843 100644
196 --- a/include/linux/mnt_namespace.h
197 +++ b/include/linux/mnt_namespace.h
198 @@ -7,12 +7,15 @@ struct mnt_namespace;
199  struct fs_struct;
200  struct user_namespace;
201  struct ns_common;
202 +struct vfsmount;
203  
204  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
205                 struct user_namespace *, struct fs_struct *);
206  extern void put_mnt_ns(struct mnt_namespace *ns);
207  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
208  
209 +extern int is_current_mnt_ns(struct vfsmount *mnt);
210 +
211  extern const struct file_operations proc_mounts_operations;
212  extern const struct file_operations proc_mountinfo_operations;
213  extern const struct file_operations proc_mountstats_operations;
214 diff --git a/include/linux/splice.h b/include/linux/splice.h
215 index a55179fd60fc..8e21c53cf883 100644
216 --- a/include/linux/splice.h
217 +++ b/include/linux/splice.h
218 @@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
219  
220  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
221  extern const struct pipe_buf_operations default_pipe_buf_ops;
222 +
223 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
224 +                          loff_t *ppos, size_t len, unsigned int flags);
225 +extern long do_splice_to(struct file *in, loff_t *ppos,
226 +                        struct pipe_inode_info *pipe, size_t len,
227 +                        unsigned int flags);
228  #endif
229 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
230 index dcd1d5bfc1e0..85d1f7e0bc07 100644
231 --- a/kernel/locking/lockdep.c
232 +++ b/kernel/locking/lockdep.c
233 @@ -218,7 +218,7 @@ unsigned long max_lock_class_idx;
234  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
235  DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
236  
237 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
238 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
239  {
240         unsigned int class_idx = hlock->class_idx;
241  
242 @@ -239,6 +239,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
243          */
244         return lock_classes + class_idx;
245  }
246 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
247  
248  #ifdef CONFIG_LOCK_STAT
249  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
250 SPDX-License-Identifier: GPL-2.0
251 aufs6.x-rcN mmap patch
252
253 diff --git a/fs/proc/base.c b/fs/proc/base.c
254 index 05452c3b9872..6b92a8430130 100644
255 --- a/fs/proc/base.c
256 +++ b/fs/proc/base.c
257 @@ -2218,7 +2218,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
258         rc = -ENOENT;
259         vma = find_exact_vma(mm, vm_start, vm_end);
260         if (vma && vma->vm_file) {
261 -               *path = vma->vm_file->f_path;
262 +               *path = vma_pr_or_file(vma)->f_path;
263                 path_get(path);
264                 rc = 0;
265         }
266 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
267 index 4d3493579458..42edd9a42c78 100644
268 --- a/fs/proc/nommu.c
269 +++ b/fs/proc/nommu.c
270 @@ -39,7 +39,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
271         file = region->vm_file;
272  
273         if (file) {
274 -               struct inode *inode = file_inode(region->vm_file);
275 +               struct inode *inode;
276 +
277 +               file = vmr_pr_or_file(region);
278 +               inode = file_inode(file);
279                 dev = inode->i_sb->s_dev;
280                 ino = inode->i_ino;
281         }
282 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
283 index 420510f6a545..edfc0941806a 100644
284 --- a/fs/proc/task_mmu.c
285 +++ b/fs/proc/task_mmu.c
286 @@ -285,7 +285,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
287         const char *name = NULL;
288  
289         if (file) {
290 -               struct inode *inode = file_inode(vma->vm_file);
291 +               struct inode *inode;
292 +
293 +               file = vma_pr_or_file(vma);
294 +               inode = file_inode(file);
295                 dev = inode->i_sb->s_dev;
296                 ino = inode->i_ino;
297                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
298 @@ -1943,7 +1946,7 @@ static int show_numa_map(struct seq_file *m, void *v)
299         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
300         struct vm_area_struct *vma = v;
301         struct numa_maps *md = &numa_priv->md;
302 -       struct file *file = vma->vm_file;
303 +       struct file *file = vma_pr_or_file(vma);
304         struct mm_struct *mm = vma->vm_mm;
305         struct mempolicy *pol;
306         char buffer[64];
307 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
308 index 0ec35072a8e5..f0f0a03eb2b5 100644
309 --- a/fs/proc/task_nommu.c
310 +++ b/fs/proc/task_nommu.c
311 @@ -150,7 +150,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
312         file = vma->vm_file;
313  
314         if (file) {
315 -               struct inode *inode = file_inode(vma->vm_file);
316 +               struct inode *inode;
317 +
318 +               file = vma_pr_or_file(vma);
319 +               inode = file_inode(file);
320                 dev = inode->i_sb->s_dev;
321                 ino = inode->i_ino;
322                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
323 diff --git a/include/linux/mm.h b/include/linux/mm.h
324 index 27ce77080c79..5f07b972242d 100644
325 --- a/include/linux/mm.h
326 +++ b/include/linux/mm.h
327 @@ -2353,6 +2353,43 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
328         unmap_mapping_range(mapping, holebegin, holelen, 0);
329  }
330  
331 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
332 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
333 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
334 +                                     int);
335 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
336 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
337 +
338 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
339 +                                                               __LINE__)
340 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
341 +                                                         __LINE__)
342 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
343 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
344 +
345 +#ifndef CONFIG_MMU
346 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
347 +extern void vmr_do_fput(struct vm_region *, const char[], int);
348 +
349 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
350 +                                                         __LINE__)
351 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
352 +#endif /* !CONFIG_MMU */
353 +
354 +#else
355 +
356 +#define vma_file_update_time(vma)      file_update_time((vma)->vm_file)
357 +#define vma_pr_or_file(vma)            (vma)->vm_file
358 +#define vma_get_file(vma)              get_file((vma)->vm_file)
359 +#define vma_fput(vma)                  fput((vma)->vm_file)
360 +
361 +#ifndef CONFIG_MMU
362 +#define vmr_pr_or_file(region)         (region)->vm_file
363 +#define vmr_fput(region)               fput((region)->vm_file)
364 +#endif /* !CONFIG_MMU */
365 +
366 +#endif /* CONFIG_AUFS_FS */
367 +
368  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
369                 void *buf, int len, unsigned int gup_flags);
370  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
371 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
372 index 306a3d1a0fa6..56374f0eb825 100644
373 --- a/include/linux/mm_types.h
374 +++ b/include/linux/mm_types.h
375 @@ -449,6 +449,9 @@ struct vm_region {
376         unsigned long   vm_top;         /* region allocated to here */
377         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
378         struct file     *vm_file;       /* the backing file or NULL */
379 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
380 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
381 +#endif
382  
383         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
384         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
385 @@ -548,6 +551,9 @@ struct vm_area_struct {
386         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
387                                            units */
388         struct file * vm_file;          /* File we map to (can be NULL). */
389 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
390 +       struct file *vm_prfile;         /* shadow of vm_file */
391 +#endif
392         void * vm_private_data;         /* was vm_pte (shared mem) */
393  
394  #ifdef CONFIG_ANON_VMA_NAME
395 diff --git a/kernel/fork.c b/kernel/fork.c
396 index ed4e01daccaa..e945a1639283 100644
397 --- a/kernel/fork.c
398 +++ b/kernel/fork.c
399 @@ -733,7 +733,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
400                 if (file) {
401                         struct address_space *mapping = file->f_mapping;
402  
403 -                       get_file(file);
404 +                       vma_get_file(tmp);
405                         i_mmap_lock_write(mapping);
406                         if (tmp->vm_flags & VM_SHARED)
407                                 mapping_allow_writable(mapping);
408 diff --git a/mm/Makefile b/mm/Makefile
409 index e29afc890cde..c467601299d4 100644
410 --- a/mm/Makefile
411 +++ b/mm/Makefile
412 @@ -137,3 +137,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o
413  obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
414  obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
415  obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
416 +obj-y += prfile.o
417 diff --git a/mm/filemap.c b/mm/filemap.c
418 index b4c9bd368b7e..ec4f4e5c1a92 100644
419 --- a/mm/filemap.c
420 +++ b/mm/filemap.c
421 @@ -3564,7 +3564,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
422         vm_fault_t ret = VM_FAULT_LOCKED;
423  
424         sb_start_pagefault(mapping->host->i_sb);
425 -       file_update_time(vmf->vma->vm_file);
426 +       vma_file_update_time(vmf->vma);
427         folio_lock(folio);
428         if (folio->mapping != mapping) {
429                 folio_unlock(folio);
430 diff --git a/mm/mmap.c b/mm/mmap.c
431 index 13678edaa22c..3b5dd8a8d627 100644
432 --- a/mm/mmap.c
433 +++ b/mm/mmap.c
434 @@ -140,7 +140,7 @@ static void remove_vma(struct vm_area_struct *vma, bool unreachable)
435         if (vma->vm_ops && vma->vm_ops->close)
436                 vma->vm_ops->close(vma);
437         if (vma->vm_file)
438 -               fput(vma->vm_file);
439 +               vma_fput(vma);
440         mpol_put(vma_policy(vma));
441         if (unreachable)
442                 __vm_area_free(vma);
443 @@ -607,7 +607,7 @@ static inline void vma_complete(struct vma_prepare *vp,
444                 if (vp->file) {
445                         uprobe_munmap(vp->remove, vp->remove->vm_start,
446                                       vp->remove->vm_end);
447 -                       fput(vp->file);
448 +                       vma_fput(vp->vma);
449                 }
450                 if (vp->remove->anon_vma)
451                         anon_vma_merge(vp->vma, vp->remove);
452 @@ -2269,7 +2269,7 @@ int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
453                 goto out_free_mpol;
454  
455         if (new->vm_file)
456 -               get_file(new->vm_file);
457 +               vma_get_file(new);
458  
459         if (new->vm_ops && new->vm_ops->open)
460                 new->vm_ops->open(new);
461 @@ -2679,7 +2679,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
462                                  * and cause general protection fault
463                                  * ultimately.
464                                  */
465 -                               fput(vma->vm_file);
466 +                               vma_fput(vma);
467                                 vm_area_free(vma);
468                                 vma = merge;
469                                 /* Update vm_flags to pick up the change. */
470 @@ -2774,7 +2774,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
471  
472         if (file || vma->vm_file) {
473  unmap_and_free_vma:
474 -               fput(vma->vm_file);
475 +               vma_fput(vma);
476                 vma->vm_file = NULL;
477  
478                 /* Undo any partial mapping done by a device driver. */
479 @@ -2843,6 +2843,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
480         unsigned long populate = 0;
481         unsigned long ret = -EINVAL;
482         struct file *file;
483 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
484 +       struct file *prfile;
485 +#endif
486  
487         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
488                      current->comm, current->pid);
489 @@ -2901,10 +2904,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
490         if (vma->vm_flags & VM_LOCKED)
491                 flags |= MAP_LOCKED;
492  
493 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
494 +       vma_get_file(vma);
495 +       file = vma->vm_file;
496 +       prfile = vma->vm_prfile;
497 +       ret = do_mmap(vma->vm_file, start, size,
498 +                       prot, flags, pgoff, &populate, NULL);
499 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
500 +               struct vm_area_struct *new_vma;
501 +
502 +               new_vma = find_vma(mm, ret);
503 +               if (!new_vma->vm_prfile)
504 +                       new_vma->vm_prfile = prfile;
505 +               if (prfile)
506 +                       get_file(prfile);
507 +       }
508 +       /*
509 +        * two fput()s instead of vma_fput(vma),
510 +        * coz vma may not be available anymore.
511 +        */
512 +       fput(file);
513 +       if (prfile)
514 +               fput(prfile);
515 +#else
516         file = get_file(vma->vm_file);
517         ret = do_mmap(vma->vm_file, start, size,
518                         prot, flags, pgoff, &populate, NULL);
519         fput(file);
520 +#endif /* CONFIG_AUFS_FS */
521  out:
522         mmap_write_unlock(mm);
523         if (populate)
524 @@ -3250,7 +3277,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
525                 if (anon_vma_clone(new_vma, vma))
526                         goto out_free_mempol;
527                 if (new_vma->vm_file)
528 -                       get_file(new_vma->vm_file);
529 +                       vma_get_file(new_vma);
530                 if (new_vma->vm_ops && new_vma->vm_ops->open)
531                         new_vma->vm_ops->open(new_vma);
532                 vma_start_write(new_vma);
533 @@ -3266,7 +3293,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
534                 new_vma->vm_ops->close(new_vma);
535  
536         if (new_vma->vm_file)
537 -               fput(new_vma->vm_file);
538 +               vma_fput(new_vma);
539  
540         unlink_anon_vmas(new_vma);
541  out_free_mempol:
542 diff --git a/mm/nommu.c b/mm/nommu.c
543 index f670d9979a26..94d41bc45915 100644
544 --- a/mm/nommu.c
545 +++ b/mm/nommu.c
546 @@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
547                 up_write(&nommu_region_sem);
548  
549                 if (region->vm_file)
550 -                       fput(region->vm_file);
551 +                       vmr_fput(region);
552  
553                 /* IO memory and memory shared directly out of the pagecache
554                  * from ramfs/tmpfs mustn't be released here */
555 @@ -602,7 +602,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
556         if (vma->vm_ops && vma->vm_ops->close)
557                 vma->vm_ops->close(vma);
558         if (vma->vm_file)
559 -               fput(vma->vm_file);
560 +               vma_fput(vma);
561         put_nommu_region(vma->vm_region);
562         vm_area_free(vma);
563  }
564 @@ -1124,7 +1124,7 @@ unsigned long do_mmap(struct file *file,
565                                         goto error_just_free;
566                                 }
567                         }
568 -                       fput(region->vm_file);
569 +                       vmr_fput(region);
570                         kmem_cache_free(vm_region_jar, region);
571                         region = pregion;
572                         result = start;
573 @@ -1206,10 +1206,10 @@ unsigned long do_mmap(struct file *file,
574  error:
575         vma_iter_free(&vmi);
576         if (region->vm_file)
577 -               fput(region->vm_file);
578 +               vmr_fput(region);
579         kmem_cache_free(vm_region_jar, region);
580         if (vma->vm_file)
581 -               fput(vma->vm_file);
582 +               vma_fput(vma);
583         vm_area_free(vma);
584         return ret;
585  
586 diff --git a/mm/prfile.c b/mm/prfile.c
587 new file mode 100644
588 index 000000000000..8f820a235364
589 --- /dev/null
590 +++ b/mm/prfile.c
591 @@ -0,0 +1,86 @@
592 +// SPDX-License-Identifier: GPL-2.0
593 +/*
594 + * Mainly for aufs which mmap(2) different file and wants to print different
595 + * path in /proc/PID/maps.
596 + * Call these functions via macros defined in linux/mm.h.
597 + *
598 + * See Documentation/filesystems/aufs/design/06mmap.txt
599 + *
600 + * Copyright (c) 2014-2022 Junjro R. Okajima
601 + * Copyright (c) 2014 Ian Campbell
602 + */
603 +
604 +#include <linux/mm.h>
605 +#include <linux/file.h>
606 +#include <linux/fs.h>
607 +
608 +/* #define PRFILE_TRACE */
609 +static inline void prfile_trace(struct file *f, struct file *pr,
610 +                             const char func[], int line, const char func2[])
611 +{
612 +#ifdef PRFILE_TRACE
613 +       if (pr)
614 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
615 +#endif
616 +}
617 +
618 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
619 +                            int line)
620 +{
621 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
622 +
623 +       prfile_trace(f, pr, func, line, __func__);
624 +       file_update_time(f);
625 +       if (f && pr)
626 +               file_update_time(pr);
627 +}
628 +
629 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
630 +                              int line)
631 +{
632 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
633 +
634 +       prfile_trace(f, pr, func, line, __func__);
635 +       return (f && pr) ? pr : f;
636 +}
637 +
638 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
639 +{
640 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
641 +
642 +       prfile_trace(f, pr, func, line, __func__);
643 +       get_file(f);
644 +       if (f && pr)
645 +               get_file(pr);
646 +}
647 +
648 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
649 +{
650 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
651 +
652 +       prfile_trace(f, pr, func, line, __func__);
653 +       fput(f);
654 +       if (f && pr)
655 +               fput(pr);
656 +}
657 +
658 +#ifndef CONFIG_MMU
659 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
660 +                              int line)
661 +{
662 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
663 +
664 +       prfile_trace(f, pr, func, line, __func__);
665 +       return (f && pr) ? pr : f;
666 +}
667 +
668 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
669 +{
670 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
671 +
672 +       prfile_trace(f, pr, func, line, __func__);
673 +       fput(f);
674 +       if (f && pr)
675 +               fput(pr);
676 +}
677 +#endif /* !CONFIG_MMU */
678 SPDX-License-Identifier: GPL-2.0
679 aufs6.x-rcN standalone patch
680
681 diff --git a/fs/dcache.c b/fs/dcache.c
682 index 519321f32f95..267e0c65914a 100644
683 --- a/fs/dcache.c
684 +++ b/fs/dcache.c
685 @@ -1450,6 +1450,7 @@ void d_walk(struct dentry *parent, void *data,
686         seq = 1;
687         goto again;
688  }
689 +EXPORT_SYMBOL_GPL(d_walk);
690  
691  struct check_mount {
692         struct vfsmount *mnt;
693 @@ -3052,6 +3053,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
694  
695         write_sequnlock(&rename_lock);
696  }
697 +EXPORT_SYMBOL_GPL(d_exchange);
698  
699  /**
700   * d_ancestor - search for an ancestor
701 diff --git a/fs/exec.c b/fs/exec.c
702 index a466e797c8e2..c60353b5214f 100644
703 --- a/fs/exec.c
704 +++ b/fs/exec.c
705 @@ -112,6 +112,7 @@ bool path_noexec(const struct path *path)
706         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
707                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
708  }
709 +EXPORT_SYMBOL_GPL(path_noexec);
710  
711  #ifdef CONFIG_USELIB
712  /*
713 diff --git a/fs/fcntl.c b/fs/fcntl.c
714 index 9ea58b7bb580..99fef189bcd6 100644
715 --- a/fs/fcntl.c
716 +++ b/fs/fcntl.c
717 @@ -87,6 +87,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
718   out:
719         return error;
720  }
721 +EXPORT_SYMBOL_GPL(setfl);
722  
723  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
724                       int force)
725 diff --git a/fs/file_table.c b/fs/file_table.c
726 index 372653b92617..69128fe39b6d 100644
727 --- a/fs/file_table.c
728 +++ b/fs/file_table.c
729 @@ -199,6 +199,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
730         }
731         return ERR_PTR(-ENFILE);
732  }
733 +EXPORT_SYMBOL_GPL(alloc_empty_file);
734  
735  /*
736   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
737 diff --git a/fs/namespace.c b/fs/namespace.c
738 index ebb7b4b057ea..6ee47677de2f 100644
739 --- a/fs/namespace.c
740 +++ b/fs/namespace.c
741 @@ -457,6 +457,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
742         mnt_dec_writers(real_mount(mnt));
743         preempt_enable();
744  }
745 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
746  
747  /**
748   * mnt_drop_write - give up write access to a mount
749 @@ -854,6 +855,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
750  {
751         return check_mnt(real_mount(mnt));
752  }
753 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
754  
755  /*
756   * vfsmount lock must be held for write
757 @@ -2062,6 +2064,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
758         }
759         return 0;
760  }
761 +EXPORT_SYMBOL_GPL(iterate_mounts);
762  
763  static void lock_mnt_tree(struct mount *mnt)
764  {
765 diff --git a/fs/notify/group.c b/fs/notify/group.c
766 index 1de6631a3925..3008eb37a18d 100644
767 --- a/fs/notify/group.c
768 +++ b/fs/notify/group.c
769 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
770  {
771         refcount_inc(&group->refcnt);
772  }
773 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
774  
775  /*
776   * Drop a reference to a group.  Free it if it's through.
777 diff --git a/fs/open.c b/fs/open.c
778 index 4478adcc4f3a..5a984ff90580 100644
779 --- a/fs/open.c
780 +++ b/fs/open.c
781 @@ -67,6 +67,7 @@ int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
782         inode_unlock(dentry->d_inode);
783         return ret;
784  }
785 +EXPORT_SYMBOL_GPL(do_truncate);
786  
787  long vfs_truncate(const struct path *path, loff_t length)
788  {
789 diff --git a/fs/read_write.c b/fs/read_write.c
790 index a21ba3be7dbe..ec40061176b3 100644
791 --- a/fs/read_write.c
792 +++ b/fs/read_write.c
793 @@ -477,6 +477,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
794         inc_syscr(current);
795         return ret;
796  }
797 +EXPORT_SYMBOL_GPL(vfs_read);
798  
799  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
800  {
801 @@ -592,6 +593,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
802         file_end_write(file);
803         return ret;
804  }
805 +EXPORT_SYMBOL_GPL(vfs_write);
806  
807  /* file_ppos returns &file->f_pos or NULL if file is stream */
808  static inline loff_t *file_ppos(struct file *file)
809 diff --git a/fs/splice.c b/fs/splice.c
810 index 97a66df75d4c..19c8d35f5ebd 100644
811 --- a/fs/splice.c
812 +++ b/fs/splice.c
813 @@ -872,6 +872,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
814                 return warn_unsupported(out, "write");
815         return out->f_op->splice_write(pipe, out, ppos, len, flags);
816  }
817 +EXPORT_SYMBOL_GPL(do_splice_from);
818  
819  /*
820   * Attempt to initiate a splice from a file to a pipe.
821 @@ -901,6 +902,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
822                 return warn_unsupported(in, "read");
823         return in->f_op->splice_read(in, ppos, pipe, len, flags);
824  }
825 +EXPORT_SYMBOL_GPL(do_splice_to);
826  
827  /**
828   * splice_direct_to_actor - splices data directly between two non-pipes
829 diff --git a/fs/xattr.c b/fs/xattr.c
830 index fcf67d80d7f9..174dfb9285fb 100644
831 --- a/fs/xattr.c
832 +++ b/fs/xattr.c
833 @@ -406,6 +406,7 @@ vfs_getxattr_alloc(struct mnt_idmap *idmap, struct dentry *dentry,
834         *xattr_value = value;
835         return error;
836  }
837 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
838  
839  ssize_t
840  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
841 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
842 index 85d1f7e0bc07..5e20504eed24 100644
843 --- a/kernel/locking/lockdep.c
844 +++ b/kernel/locking/lockdep.c
845 @@ -239,6 +239,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
846          */
847         return lock_classes + class_idx;
848  }
849 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
850  #define hlock_class(hlock) lockdep_hlock_class(hlock)
851  
852  #ifdef CONFIG_LOCK_STAT
853 diff --git a/kernel/task_work.c b/kernel/task_work.c
854 index 065e1ef8fc8d..c623c6f0c645 100644
855 --- a/kernel/task_work.c
856 +++ b/kernel/task_work.c
857 @@ -182,3 +182,4 @@ void task_work_run(void)
858                 } while (work);
859         }
860  }
861 +EXPORT_SYMBOL_GPL(task_work_run);
862 diff --git a/security/security.c b/security/security.c
863 index d5ff7ff45b77..5cca1f5774ef 100644
864 --- a/security/security.c
865 +++ b/security/security.c
866 @@ -1711,6 +1711,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
867                 return 0;
868         return call_int_hook(path_rmdir, 0, dir, dentry);
869  }
870 +EXPORT_SYMBOL_GPL(security_path_rmdir);
871  
872  /**
873   * security_path_unlink() - Check if removing a hard link is allowed
874 @@ -1746,6 +1747,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
875                 return 0;
876         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
877  }
878 +EXPORT_SYMBOL_GPL(security_path_symlink);
879  
880  /**
881   * security_path_link - Check if creating a hard link is allowed
882 @@ -1764,6 +1766,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
883                 return 0;
884         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
885  }
886 +EXPORT_SYMBOL_GPL(security_path_link);
887  
888  /**
889   * security_path_rename() - Check if renaming a file is allowed
890 @@ -1825,6 +1828,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
891                 return 0;
892         return call_int_hook(path_chmod, 0, path, mode);
893  }
894 +EXPORT_SYMBOL_GPL(security_path_chmod);
895  
896  /**
897   * security_path_chown() - Check if changing the file's owner/group is allowed
898 @@ -1842,6 +1846,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
899                 return 0;
900         return call_int_hook(path_chown, 0, path, uid, gid);
901  }
902 +EXPORT_SYMBOL_GPL(security_path_chown);
903  
904  /**
905   * security_path_chroot() - Check if changing the root directory is allowed
906 @@ -2071,6 +2076,7 @@ int security_inode_permission(struct inode *inode, int mask)
907                 return 0;
908         return call_int_hook(inode_permission, 0, inode, mask);
909  }
910 +EXPORT_SYMBOL_GPL(security_inode_permission);
911  
912  /**
913   * security_inode_setattr() - Check if setting file attributes is allowed
914 @@ -2549,6 +2555,7 @@ int security_file_permission(struct file *file, int mask)
915  
916         return fsnotify_perm(file, mask);
917  }
918 +EXPORT_SYMBOL_GPL(security_file_permission);
919  
920  /**
921   * security_file_alloc() - Allocate and init a file's LSM blob
922 @@ -2815,6 +2822,7 @@ int security_file_truncate(struct file *file)
923  {
924         return call_int_hook(file_truncate, 0, file);
925  }
926 +EXPORT_SYMBOL_GPL(security_file_truncate);
927  
928  /**
929   * security_task_alloc() - Allocate a task's LSM blob
930 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
931 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
932 +++ linux/Documentation/ABI/testing/debugfs-aufs        2022-11-05 23:02:18.955889283 +0100
933 @@ -0,0 +1,55 @@
934 +What:          /debug/aufs/si_<id>/
935 +Date:          March 2009
936 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
937 +Description:
938 +               Under /debug/aufs, a directory named si_<id> is created
939 +               per aufs mount, where <id> is a unique id generated
940 +               internally.
941 +
942 +What:          /debug/aufs/si_<id>/plink
943 +Date:          Apr 2013
944 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
945 +Description:
946 +               It has three lines and shows the information about the
947 +               pseudo-link. The first line is a single number
948 +               representing a number of buckets. The second line is a
949 +               number of pseudo-links per buckets (separated by a
950 +               blank). The last line is a single number representing a
951 +               total number of psedo-links.
952 +               When the aufs mount option 'noplink' is specified, it
953 +               will show "1\n0\n0\n".
954 +
955 +What:          /debug/aufs/si_<id>/xib
956 +Date:          March 2009
957 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
958 +Description:
959 +               It shows the consumed blocks by xib (External Inode Number
960 +               Bitmap), its block size and file size.
961 +               When the aufs mount option 'noxino' is specified, it
962 +               will be empty. About XINO files, see the aufs manual.
963 +
964 +What:          /debug/aufs/si_<id>/xi<branch-index>
965 +Date:          March 2009
966 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
967 +Description:
968 +               It shows the consumed blocks by xino (External Inode Number
969 +               Translation Table), its link count, block size and file
970 +               size.
971 +               Due to the file size limit, there may exist multiple
972 +               xino files per branch.  In this case, "-N" is added to
973 +               the filename and it corresponds to the index of the
974 +               internal xino array.  "-0" is omitted.
975 +               When the aufs mount option 'noxino' is specified, Those
976 +               entries won't exist.  About XINO files, see the aufs
977 +               manual.
978 +
979 +What:          /debug/aufs/si_<id>/xigen
980 +Date:          March 2009
981 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
982 +Description:
983 +               It shows the consumed blocks by xigen (External Inode
984 +               Generation Table), its block size and file size.
985 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
986 +               be created.
987 +               When the aufs mount option 'noxino' is specified, it
988 +               will be empty. About XINO files, see the aufs manual.
989 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
990 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
991 +++ linux/Documentation/ABI/testing/sysfs-aufs  2022-11-05 23:02:18.955889283 +0100
992 @@ -0,0 +1,31 @@
993 +What:          /sys/fs/aufs/si_<id>/
994 +Date:          March 2009
995 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
996 +Description:
997 +               Under /sys/fs/aufs, a directory named si_<id> is created
998 +               per aufs mount, where <id> is a unique id generated
999 +               internally.
1000 +
1001 +What:          /sys/fs/aufs/si_<id>/br<idx>
1002 +Date:          March 2009
1003 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1004 +Description:
1005 +               It shows the abolute path of a member directory (which
1006 +               is called branch) in aufs, and its permission.
1007 +
1008 +What:          /sys/fs/aufs/si_<id>/brid<idx>
1009 +Date:          July 2013
1010 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1011 +Description:
1012 +               It shows the id of a member directory (which is called
1013 +               branch) in aufs.
1014 +
1015 +What:          /sys/fs/aufs/si_<id>/xi_path
1016 +Date:          March 2009
1017 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1018 +Description:
1019 +               It shows the abolute path of XINO (External Inode Number
1020 +               Bitmap, Translation Table and Generation Table) file
1021 +               even if it is the default path.
1022 +               When the aufs mount option 'noxino' is specified, it
1023 +               will be empty. About XINO files, see the aufs manual.
1024 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1025 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1026 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2022-11-05 23:02:18.955889283 +0100
1027 @@ -0,0 +1,171 @@
1028 +
1029 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1030 +#
1031 +# This program is free software; you can redistribute it and/or modify
1032 +# it under the terms of the GNU General Public License as published by
1033 +# the Free Software Foundation; either version 2 of the License, or
1034 +# (at your option) any later version.
1035 +#
1036 +# This program is distributed in the hope that it will be useful,
1037 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1038 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1039 +# GNU General Public License for more details.
1040 +#
1041 +# You should have received a copy of the GNU General Public License
1042 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1043 +
1044 +Introduction
1045 +----------------------------------------
1046 +
1047 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1048 +1. abbrev. for "advanced multi-layered unification filesystem".
1049 +2. abbrev. for "another unionfs".
1050 +3. abbrev. for "auf das" in German which means "on the" in English.
1051 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1052 +   But "Filesystem aufs Filesystem" is hard to understand.
1053 +4. abbrev. for "African Urban Fashion Show".
1054 +
1055 +AUFS is a filesystem with features:
1056 +- multi layered stackable unification filesystem, the member directory
1057 +  is called as a branch.
1058 +- branch permission and attribute, 'readonly', 'real-readonly',
1059 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1060 +  combination.
1061 +- internal "file copy-on-write".
1062 +- logical deletion, whiteout.
1063 +- dynamic branch manipulation, adding, deleting and changing permission.
1064 +- allow bypassing aufs, user's direct branch access.
1065 +- external inode number translation table and bitmap which maintains the
1066 +  persistent aufs inode number.
1067 +- seekable directory, including NFS readdir.
1068 +- file mapping, mmap and sharing pages.
1069 +- pseudo-link, hardlink over branches.
1070 +- loopback mounted filesystem as a branch.
1071 +- several policies to select one among multiple writable branches.
1072 +- revert a single systemcall when an error occurs in aufs.
1073 +- and more...
1074 +
1075 +
1076 +Multi Layered Stackable Unification Filesystem
1077 +----------------------------------------------------------------------
1078 +Most people already knows what it is.
1079 +It is a filesystem which unifies several directories and provides a
1080 +merged single directory. When users access a file, the access will be
1081 +passed/re-directed/converted (sorry, I am not sure which English word is
1082 +correct) to the real file on the member filesystem. The member
1083 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1084 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1085 +readonly branch is handled by creating 'whiteout' on the upper writable
1086 +branch.
1087 +
1088 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1089 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1090 +different approaches to implement the merged-view.
1091 +The former tries putting it into VFS, and the latter implements as a
1092 +separate filesystem.
1093 +(If I misunderstand about these implementations, please let me know and
1094 +I shall correct it. Because it is a long time ago when I read their
1095 +source files last time).
1096 +
1097 +UnionMount's approach will be able to small, but may be hard to share
1098 +branches between several UnionMount since the whiteout in it is
1099 +implemented in the inode on branch filesystem and always
1100 +shared. According to Bharata's post, readdir does not seems to be
1101 +finished yet.
1102 +There are several missing features known in this implementations such as
1103 +- for users, the inode number may change silently. eg. copy-up.
1104 +- link(2) may break by copy-up.
1105 +- read(2) may get an obsoleted filedata (fstat(2) too).
1106 +- fcntl(F_SETLK) may be broken by copy-up.
1107 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1108 +  open(O_RDWR).
1109 +
1110 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1111 +merged into mainline. This is another implementation of UnionMount as a
1112 +separated filesystem. All the limitations and known problems which
1113 +UnionMount are equally inherited to "overlay" filesystem.
1114 +
1115 +Unionfs has a longer history. When I started implementing a stackable
1116 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1117 +inode, dentry and file objects and they have an array pointing lower
1118 +same kind objects. After contributing many patches for Unionfs, I
1119 +re-started my project AUFS (Jun 2006).
1120 +
1121 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1122 +implemented my own ideas, approaches and enhancements and it became
1123 +totally different one.
1124 +
1125 +Comparing DM snapshot and fs based implementation
1126 +- the number of bytes to be copied between devices is much smaller.
1127 +- the type of filesystem must be one and only.
1128 +- the fs must be writable, no readonly fs, even for the lower original
1129 +  device. so the compression fs will not be usable. but if we use
1130 +  loopback mount, we may address this issue.
1131 +  for instance,
1132 +       mount /cdrom/squashfs.img /sq
1133 +       losetup /sq/ext2.img
1134 +       losetup /somewhere/cow
1135 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1136 +- it will be difficult (or needs more operations) to extract the
1137 +  difference between the original device and COW.
1138 +- DM snapshot-merge may help a lot when users try merging. in the
1139 +  fs-layer union, users will use rsync(1).
1140 +
1141 +You may want to read my old paper "Filesystems in LiveCD"
1142 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1143 +
1144 +
1145 +Several characters/aspects/persona of aufs
1146 +----------------------------------------------------------------------
1147 +
1148 +Aufs has several characters, aspects or persona.
1149 +1. a filesystem, callee of VFS helper
1150 +2. sub-VFS, caller of VFS helper for branches
1151 +3. a virtual filesystem which maintains persistent inode number
1152 +4. reader/writer of files on branches such like an application
1153 +
1154 +1. Callee of VFS Helper
1155 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1156 +unlink(2) from an application reaches sys_unlink() kernel function and
1157 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1158 +calls filesystem specific unlink operation. Actually aufs implements the
1159 +unlink operation but it behaves like a redirector.
1160 +
1161 +2. Caller of VFS Helper for Branches
1162 +aufs_unlink() passes the unlink request to the branch filesystem as if
1163 +it were called from VFS. So the called unlink operation of the branch
1164 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1165 +every necessary pre/post operation for the branch filesystem.
1166 +- acquire the lock for the parent dir on a branch
1167 +- lookup in a branch
1168 +- revalidate dentry on a branch
1169 +- mnt_want_write() for a branch
1170 +- vfs_unlink() for a branch
1171 +- mnt_drop_write() for a branch
1172 +- release the lock on a branch
1173 +
1174 +3. Persistent Inode Number
1175 +One of the most important issue for a filesystem is to maintain inode
1176 +numbers. This is particularly important to support exporting a
1177 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1178 +backend block device for its own. But some storage is necessary to
1179 +keep and maintain the inode numbers. It may be a large space and may not
1180 +suit to keep in memory. Aufs rents some space from its first writable
1181 +branch filesystem (by default) and creates file(s) on it. These files
1182 +are created by aufs internally and removed soon (currently) keeping
1183 +opened.
1184 +Note: Because these files are removed, they are totally gone after
1185 +      unmounting aufs. It means the inode numbers are not persistent
1186 +      across unmount or reboot. I have a plan to make them really
1187 +      persistent which will be important for aufs on NFS server.
1188 +
1189 +4. Read/Write Files Internally (copy-on-write)
1190 +Because a branch can be readonly, when you write a file on it, aufs will
1191 +"copy-up" it to the upper writable branch internally. And then write the
1192 +originally requested thing to the file. Generally kernel doesn't
1193 +open/read/write file actively. In aufs, even a single write may cause a
1194 +internal "file copy". This behaviour is very similar to cp(1) command.
1195 +
1196 +Some people may think it is better to pass such work to user space
1197 +helper, instead of doing in kernel space. Actually I am still thinking
1198 +about it. But currently I have implemented it in kernel space.
1199 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1200 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1201 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2022-11-05 23:02:18.955889283 +0100
1202 @@ -0,0 +1,258 @@
1203 +
1204 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1205 +#
1206 +# This program is free software; you can redistribute it and/or modify
1207 +# it under the terms of the GNU General Public License as published by
1208 +# the Free Software Foundation; either version 2 of the License, or
1209 +# (at your option) any later version.
1210 +#
1211 +# This program is distributed in the hope that it will be useful,
1212 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1213 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1214 +# GNU General Public License for more details.
1215 +#
1216 +# You should have received a copy of the GNU General Public License
1217 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1218 +
1219 +Basic Aufs Internal Structure
1220 +
1221 +Superblock/Inode/Dentry/File Objects
1222 +----------------------------------------------------------------------
1223 +As like an ordinary filesystem, aufs has its own
1224 +superblock/inode/dentry/file objects. All these objects have a
1225 +dynamically allocated array and store the same kind of pointers to the
1226 +lower filesystem, branch.
1227 +For example, when you build a union with one readwrite branch and one
1228 +readonly, mounted /au, /rw and /ro respectively.
1229 +- /au = /rw + /ro
1230 +- /ro/fileA exists but /rw/fileA
1231 +
1232 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1233 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1234 +- [0] = NULL (because /rw/fileA doesn't exist)
1235 +- [1] = /ro/fileA
1236 +
1237 +This style of an array is essentially same to the aufs
1238 +superblock/inode/dentry/file objects.
1239 +
1240 +Because aufs supports manipulating branches, ie. add/delete/change
1241 +branches dynamically, these objects has its own generation. When
1242 +branches are changed, the generation in aufs superblock is
1243 +incremented. And a generation in other object are compared when it is
1244 +accessed. When a generation in other objects are obsoleted, aufs
1245 +refreshes the internal array.
1246 +
1247 +
1248 +Superblock
1249 +----------------------------------------------------------------------
1250 +Additionally aufs superblock has some data for policies to select one
1251 +among multiple writable branches, XIB files, pseudo-links and kobject.
1252 +See below in detail.
1253 +About the policies which supports copy-down a directory, see
1254 +wbr_policy.txt too.
1255 +
1256 +
1257 +Branch and XINO(External Inode Number Translation Table)
1258 +----------------------------------------------------------------------
1259 +Every branch has its own xino (external inode number translation table)
1260 +file. The xino file is created and unlinked by aufs internally. When two
1261 +members of a union exist on the same filesystem, they share the single
1262 +xino file.
1263 +The struct of a xino file is simple, just a sequence of aufs inode
1264 +numbers which is indexed by the lower inode number.
1265 +In the above sample, assume the inode number of /ro/fileA is i111 and
1266 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1267 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1268 +
1269 +When the inode numbers are not contiguous, the xino file will be sparse
1270 +which has a hole in it and doesn't consume as much disk space as it
1271 +might appear. If your branch filesystem consumes disk space for such
1272 +holes, then you should specify 'xino=' option at mounting aufs.
1273 +
1274 +Aufs has a mount option to free the disk blocks for such holes in XINO
1275 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1276 +meet a problem of disk shortage due to XINO files, then you should try
1277 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1278 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1279 +the holes in XINO files.
1280 +
1281 +Also a writable branch has three kinds of "whiteout bases". All these
1282 +are existed when the branch is joined to aufs, and their names are
1283 +whiteout-ed doubly, so that users will never see their names in aufs
1284 +hierarchy.
1285 +1. a regular file which will be hardlinked to all whiteouts.
1286 +2. a directory to store a pseudo-link.
1287 +3. a directory to store an "orphan"-ed file temporary.
1288 +
1289 +1. Whiteout Base
1290 +   When you remove a file on a readonly branch, aufs handles it as a
1291 +   logical deletion and creates a whiteout on the upper writable branch
1292 +   as a hardlink of this file in order not to consume inode on the
1293 +   writable branch.
1294 +2. Pseudo-link Dir
1295 +   See below, Pseudo-link.
1296 +3. Step-Parent Dir
1297 +   When "fileC" exists on the lower readonly branch only and it is
1298 +   opened and removed with its parent dir, and then user writes
1299 +   something into it, then aufs copies-up fileC to this
1300 +   directory. Because there is no other dir to store fileC. After
1301 +   creating a file under this dir, the file is unlinked.
1302 +
1303 +Because aufs supports manipulating branches, ie. add/delete/change
1304 +dynamically, a branch has its own id. When the branch order changes,
1305 +aufs finds the new index by searching the branch id.
1306 +
1307 +
1308 +Pseudo-link
1309 +----------------------------------------------------------------------
1310 +Assume "fileA" exists on the lower readonly branch only and it is
1311 +hardlinked to "fileB" on the branch. When you write something to fileA,
1312 +aufs copies-up it to the upper writable branch. Additionally aufs
1313 +creates a hardlink under the Pseudo-link Directory of the writable
1314 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1315 +simple list. If fileB is read after unlinking fileA, aufs returns
1316 +filedata from the pseudo-link instead of the lower readonly
1317 +branch. Because the pseudo-link is based upon the inode, to keep the
1318 +inode number by xino (see above) is essentially necessary.
1319 +
1320 +All the hardlinks under the Pseudo-link Directory of the writable branch
1321 +should be restored in a proper location later. Aufs provides a utility
1322 +to do this. The userspace helpers executed at remounting and unmounting
1323 +aufs by default.
1324 +During this utility is running, it puts aufs into the pseudo-link
1325 +maintenance mode. In this mode, only the process which began the
1326 +maintenance mode (and its child processes) is allowed to operate in
1327 +aufs. Some other processes which are not related to the pseudo-link will
1328 +be allowed to run too, but the rest have to return an error or wait
1329 +until the maintenance mode ends. If a process already acquires an inode
1330 +mutex (in VFS), it has to return an error.
1331 +
1332 +
1333 +XIB(external inode number bitmap)
1334 +----------------------------------------------------------------------
1335 +Addition to the xino file per a branch, aufs has an external inode number
1336 +bitmap in a superblock object. It is also an internal file such like a
1337 +xino file.
1338 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1339 +not.
1340 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1341 +
1342 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1343 +reduce the number of consumed disk blocks for these files.
1344 +
1345 +
1346 +Virtual or Vertical Dir, and Readdir in Userspace
1347 +----------------------------------------------------------------------
1348 +In order to support multiple layers (branches), aufs readdir operation
1349 +constructs a virtual dir block on memory. For readdir, aufs calls
1350 +vfs_readdir() internally for each dir on branches, merges their entries
1351 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1352 +object. So the file object has its entry list until it is closed. The
1353 +entry list will be updated when the file position is zero and becomes
1354 +obsoleted. This decision is made in aufs automatically.
1355 +
1356 +The dynamically allocated memory block for the name of entries has a
1357 +unit of 512 bytes (by default) and stores the names contiguously (no
1358 +padding). Another block for each entry is handled by kmem_cache too.
1359 +During building dir blocks, aufs creates hash list and judging whether
1360 +the entry is whiteouted by its upper branch or already listed.
1361 +The merged result is cached in the corresponding inode object and
1362 +maintained by a customizable life-time option.
1363 +
1364 +Some people may call it can be a security hole or invite DoS attack
1365 +since the opened and once readdir-ed dir (file object) holds its entry
1366 +list and becomes a pressure for system memory. But I'd say it is similar
1367 +to files under /proc or /sys. The virtual files in them also holds a
1368 +memory page (generally) while they are opened. When an idea to reduce
1369 +memory for them is introduced, it will be applied to aufs too.
1370 +For those who really hate this situation, I've developed readdir(3)
1371 +library which operates this merging in userspace. You just need to set
1372 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1373 +kernel space for readdir(3).
1374 +
1375 +
1376 +Workqueue
1377 +----------------------------------------------------------------------
1378 +Aufs sometimes requires privilege access to a branch. For instance,
1379 +in copy-up/down operation. When a user process is going to make changes
1380 +to a file which exists in the lower readonly branch only, and the mode
1381 +of one of ancestor directories may not be writable by a user
1382 +process. Here aufs copy-up the file with its ancestors and they may
1383 +require privilege to set its owner/group/mode/etc.
1384 +This is a typical case of a application character of aufs (see
1385 +Introduction).
1386 +
1387 +Aufs uses workqueue synchronously for this case. It creates its own
1388 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1389 +passes the request to call mkdir or write (for example), and wait for
1390 +its completion. This approach solves a problem of a signal handler
1391 +simply.
1392 +If aufs didn't adopt the workqueue and changed the privilege of the
1393 +process, then the process may receive the unexpected SIGXFSZ or other
1394 +signals.
1395 +
1396 +Also aufs uses the system global workqueue ("events" kernel thread) too
1397 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1398 +whiteout base and etc. This is unrelated to a privilege.
1399 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1400 +superblock at the beginning, at the same time waits for the completion
1401 +of all queued asynchronous tasks.
1402 +
1403 +
1404 +Whiteout
1405 +----------------------------------------------------------------------
1406 +The whiteout in aufs is very similar to Unionfs's. That is represented
1407 +by its filename. UnionMount takes an approach of a file mode, but I am
1408 +afraid several utilities (find(1) or something) will have to support it.
1409 +
1410 +Basically the whiteout represents "logical deletion" which stops aufs to
1411 +lookup further, but also it represents "dir is opaque" which also stop
1412 +further lookup.
1413 +
1414 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1415 +In order to make several functions in a single systemcall to be
1416 +revertible, aufs adopts an approach to rename a directory to a temporary
1417 +unique whiteouted name.
1418 +For example, in rename(2) dir where the target dir already existed, aufs
1419 +renames the target dir to a temporary unique whiteouted name before the
1420 +actual rename on a branch, and then handles other actions (make it opaque,
1421 +update the attributes, etc). If an error happens in these actions, aufs
1422 +simply renames the whiteouted name back and returns an error. If all are
1423 +succeeded, aufs registers a function to remove the whiteouted unique
1424 +temporary name completely and asynchronously to the system global
1425 +workqueue.
1426 +
1427 +
1428 +Copy-up
1429 +----------------------------------------------------------------------
1430 +It is a well-known feature or concept.
1431 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1432 +internally and makes change to the new file on the upper writable branch.
1433 +When the trigger systemcall does not update the timestamps of the parent
1434 +dir, aufs reverts it after copy-up.
1435 +
1436 +
1437 +Move-down (aufs3.9 and later)
1438 +----------------------------------------------------------------------
1439 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1440 +the lower readonly branch to the upper writable branch when a user
1441 +changes something about the file.
1442 +"Move-down" is an opposite action of copy-up. Basically this action is
1443 +ran manually instead of automatically and internally.
1444 +For desgin and implementation, aufs has to consider these issues.
1445 +- whiteout for the file may exist on the lower branch.
1446 +- ancestor directories may not exist on the lower branch.
1447 +- diropq for the ancestor directories may exist on the upper branch.
1448 +- free space on the lower branch will reduce.
1449 +- another access to the file may happen during moving-down, including
1450 +  UDBA (see "Revalidate Dentry and UDBA").
1451 +- the file should not be hard-linked nor pseudo-linked. they should be
1452 +  handled by auplink utility later.
1453 +
1454 +Sometimes users want to move-down a file from the upper writable branch
1455 +to the lower readonly or writable branch. For instance,
1456 +- the free space of the upper writable branch is going to run out.
1457 +- create a new intermediate branch between the upper and lower branch.
1458 +- etc.
1459 +
1460 +For this purpose, use "aumvdown" command in aufs-util.git.
1461 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1462 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1463 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2022-11-05 23:02:18.955889283 +0100
1464 @@ -0,0 +1,85 @@
1465 +
1466 +# Copyright (C) 2015-2022 Junjiro R. Okajima
1467 +#
1468 +# This program is free software; you can redistribute it and/or modify
1469 +# it under the terms of the GNU General Public License as published by
1470 +# the Free Software Foundation; either version 2 of the License, or
1471 +# (at your option) any later version.
1472 +#
1473 +# This program is distributed in the hope that it will be useful,
1474 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1475 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1476 +# GNU General Public License for more details.
1477 +#
1478 +# You should have received a copy of the GNU General Public License
1479 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1480 +
1481 +Support for a branch who has its ->atomic_open()
1482 +----------------------------------------------------------------------
1483 +The filesystems who implement its ->atomic_open() are not majority. For
1484 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1485 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1486 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1487 +sure whether all filesystems who have ->atomic_open() behave like this,
1488 +but NFSv4 surely returns the error.
1489 +
1490 +In order to support ->atomic_open() for aufs, there are a few
1491 +approaches.
1492 +
1493 +A. Introduce aufs_atomic_open()
1494 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1495 +     branch fs.
1496 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1497 +   an aufs user Pip Cet's approach
1498 +   - calls aufs_create(), VFS finish_open() and notify_change().
1499 +   - pass fake-mode to finish_open(), and then correct the mode by
1500 +     notify_change().
1501 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1502 +   - no aufs_atomic_open().
1503 +   - aufs_lookup() registers the TID to an aufs internal object.
1504 +   - aufs_create() does nothing when the matching TID is registered, but
1505 +     registers the mode.
1506 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1507 +     TID is registered.
1508 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1509 +   credential
1510 +   - no aufs_atomic_open().
1511 +   - aufs_create() registers the TID to an internal object. this info
1512 +     represents "this process created this file just now."
1513 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1514 +     registered TID and re-try open() with superuser's credential.
1515 +
1516 +Pros and cons for each approach.
1517 +
1518 +A.
1519 +   - straightforward but highly depends upon VFS internal.
1520 +   - the atomic behavaiour is kept.
1521 +   - some of parameters such as nameidata are hard to reproduce for
1522 +     branch fs.
1523 +   - large overhead.
1524 +B.
1525 +   - easy to implement.
1526 +   - the atomic behavaiour is lost.
1527 +C.
1528 +   - the atomic behavaiour is kept.
1529 +   - dirty and tricky.
1530 +   - VFS checks whether the file is created correctly after calling
1531 +     ->create(), which means this approach doesn't work.
1532 +D.
1533 +   - easy to implement.
1534 +   - the atomic behavaiour is lost.
1535 +   - to open a file with superuser's credential and give it to a user
1536 +     process is a bad idea, since the file object keeps the credential
1537 +     in it. It may affect LSM or something. This approach doesn't work
1538 +     either.
1539 +
1540 +The approach A is ideal, but it hard to implement. So here is a
1541 +variation of A, which is to be implemented.
1542 +
1543 +A-1. Introduce aufs_atomic_open()
1544 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1545 +       vfs_create() and finish_open().
1546 +     - the demerit is that the several checks after branch fs
1547 +       ->atomic_open() are lost. in the ordinary case, the checks are
1548 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1549 +       be implemented in aufs, but not all I am afraid.
1550 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1551 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1552 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2022-11-05 23:02:18.959222617 +0100
1553 @@ -0,0 +1,113 @@
1554 +
1555 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1556 +#
1557 +# This program is free software; you can redistribute it and/or modify
1558 +# it under the terms of the GNU General Public License as published by
1559 +# the Free Software Foundation; either version 2 of the License, or
1560 +# (at your option) any later version.
1561 +#
1562 +# This program is distributed in the hope that it will be useful,
1563 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1564 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1565 +# GNU General Public License for more details.
1566 +#
1567 +# You should have received a copy of the GNU General Public License
1568 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1569 +
1570 +Lookup in a Branch
1571 +----------------------------------------------------------------------
1572 +Since aufs has a character of sub-VFS (see Introduction), it operates
1573 +lookup for branches as VFS does. It may be a heavy work. But almost all
1574 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1575 +directly connected to its parent. Digging down the directory hierarchy
1576 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1577 +aufs calls it.
1578 +
1579 +When a branch is a remote filesystem, aufs basically relies upon its
1580 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1581 +them.
1582 +For d_revalidate, aufs implements three levels of revalidate tests. See
1583 +"Revalidate Dentry and UDBA" in detail.
1584 +
1585 +
1586 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1587 +----------------------------------------------------------------------
1588 +Let's try case study.
1589 +- aufs has two branches, upper readwrite and lower readonly.
1590 +  /au = /rw + /ro
1591 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1592 +- user invoked "chmod a+rx /au/dirA"
1593 +- the internal copy-up is activated and "/rw/dirA" is created and its
1594 +  permission bits are set to world readable.
1595 +- then "/au/dirA" becomes world readable?
1596 +
1597 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1598 +or it may be a natively readonly filesystem. If aufs respects the lower
1599 +branch, it should not respond readdir request from other users. But user
1600 +allowed it by chmod. Should really aufs rejects showing the entries
1601 +under /ro/dirA?
1602 +
1603 +To be honest, I don't have a good solution for this case. So aufs
1604 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1605 +users.
1606 +When dirperm1 is specified, aufs checks only the highest one for the
1607 +directory permission, and shows the entries. Otherwise, as usual, checks
1608 +every dir existing on all branches and rejects the request.
1609 +
1610 +As a side effect, dirperm1 option improves the performance of aufs
1611 +because the number of permission check is reduced when the number of
1612 +branch is many.
1613 +
1614 +
1615 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1616 +----------------------------------------------------------------------
1617 +Generally VFS helpers re-validate a dentry as a part of lookup.
1618 +0. digging down the directory hierarchy.
1619 +1. lock the parent dir by its i_mutex.
1620 +2. lookup the final (child) entry.
1621 +3. revalidate it.
1622 +4. call the actual operation (create, unlink, etc.)
1623 +5. unlock the parent dir
1624 +
1625 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1626 +called. Actually aufs implements it and checks the dentry on a branch is
1627 +still valid.
1628 +But it is not enough. Because aufs has to release the lock for the
1629 +parent dir on a branch at the end of ->lookup() (step 2) and
1630 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1631 +held by VFS.
1632 +If the file on a branch is changed directly, eg. bypassing aufs, after
1633 +aufs released the lock, then the subsequent operation may cause
1634 +something unpleasant result.
1635 +
1636 +This situation is a result of VFS architecture, ->lookup() and
1637 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1638 +design from VFS's point of view. It is just not suitable for sub-VFS
1639 +character in aufs.
1640 +
1641 +Aufs supports such case by three level of revalidation which is
1642 +selectable by user.
1643 +1. Simple Revalidate
1644 +   Addition to the native flow in VFS's, confirm the child-parent
1645 +   relationship on the branch just after locking the parent dir on the
1646 +   branch in the "actual operation" (step 4). When this validation
1647 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1648 +   checks the validation of the dentry on branches.
1649 +2. Monitor Changes Internally by Inotify/Fsnotify
1650 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1651 +   the dentry on the branch, and returns EBUSY if it finds different
1652 +   dentry.
1653 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1654 +   during it is in cache. When the event is notified, aufs registers a
1655 +   function to kernel 'events' thread by schedule_work(). And the
1656 +   function sets some special status to the cached aufs dentry and inode
1657 +   private data. If they are not cached, then aufs has nothing to
1658 +   do. When the same file is accessed through aufs (step 0-3) later,
1659 +   aufs will detect the status and refresh all necessary data.
1660 +   In this mode, aufs has to ignore the event which is fired by aufs
1661 +   itself.
1662 +3. No Extra Validation
1663 +   This is the simplest test and doesn't add any additional revalidation
1664 +   test, and skip the revalidation in step 4. It is useful and improves
1665 +   aufs performance when system surely hide the aufs branches from user,
1666 +   by over-mounting something (or another method).
1667 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1668 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1669 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2022-11-05 23:02:18.959222617 +0100
1670 @@ -0,0 +1,74 @@
1671 +
1672 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1673 +#
1674 +# This program is free software; you can redistribute it and/or modify
1675 +# it under the terms of the GNU General Public License as published by
1676 +# the Free Software Foundation; either version 2 of the License, or
1677 +# (at your option) any later version.
1678 +#
1679 +# This program is distributed in the hope that it will be useful,
1680 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1681 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1682 +# GNU General Public License for more details.
1683 +#
1684 +# You should have received a copy of the GNU General Public License
1685 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1686 +
1687 +Branch Manipulation
1688 +
1689 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1690 +and changing its permission/attribute, there are a lot of works to do.
1691 +
1692 +
1693 +Add a Branch
1694 +----------------------------------------------------------------------
1695 +o Confirm the adding dir exists outside of aufs, including loopback
1696 +  mount, and its various attributes.
1697 +o Initialize the xino file and whiteout bases if necessary.
1698 +  See struct.txt.
1699 +
1700 +o Check the owner/group/mode of the directory
1701 +  When the owner/group/mode of the adding directory differs from the
1702 +  existing branch, aufs issues a warning because it may impose a
1703 +  security risk.
1704 +  For example, when a upper writable branch has a world writable empty
1705 +  top directory, a malicious user can create any files on the writable
1706 +  branch directly, like copy-up and modify manually. If something like
1707 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1708 +  writable branch, and the writable branch is world-writable, then a
1709 +  malicious guy may create /etc/passwd on the writable branch directly
1710 +  and the infected file will be valid in aufs.
1711 +  I am afraid it can be a security issue, but aufs can do nothing except
1712 +  producing a warning.
1713 +
1714 +
1715 +Delete a Branch
1716 +----------------------------------------------------------------------
1717 +o Confirm the deleting branch is not busy
1718 +  To be general, there is one merit to adopt "remount" interface to
1719 +  manipulate branches. It is to discard caches. At deleting a branch,
1720 +  aufs checks the still cached (and connected) dentries and inodes. If
1721 +  there are any, then they are all in-use. An inode without its
1722 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1723 +
1724 +  For the cached one, aufs checks whether the same named entry exists on
1725 +  other branches.
1726 +  If the cached one is a directory, because aufs provides a merged view
1727 +  to users, as long as one dir is left on any branch aufs can show the
1728 +  dir to users. In this case, the branch can be removed from aufs.
1729 +  Otherwise aufs rejects deleting the branch.
1730 +
1731 +  If any file on the deleting branch is opened by aufs, then aufs
1732 +  rejects deleting.
1733 +
1734 +
1735 +Modify the Permission of a Branch
1736 +----------------------------------------------------------------------
1737 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1738 +  See struct.txt.
1739 +
1740 +o rw --> ro: Confirm the modifying branch is not busy
1741 +  Aufs rejects the request if any of these conditions are true.
1742 +  - a file on the branch is mmap-ed.
1743 +  - a regular file on the branch is opened for write and there is no
1744 +    same named entry on the upper branch.
1745 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1746 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1747 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2022-11-05 23:02:18.959222617 +0100
1748 @@ -0,0 +1,64 @@
1749 +
1750 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1751 +#
1752 +# This program is free software; you can redistribute it and/or modify
1753 +# it under the terms of the GNU General Public License as published by
1754 +# the Free Software Foundation; either version 2 of the License, or
1755 +# (at your option) any later version.
1756 +#
1757 +# This program is distributed in the hope that it will be useful,
1758 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1759 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1760 +# GNU General Public License for more details.
1761 +#
1762 +# You should have received a copy of the GNU General Public License
1763 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1764 +
1765 +Policies to Select One among Multiple Writable Branches
1766 +----------------------------------------------------------------------
1767 +When the number of writable branch is more than one, aufs has to decide
1768 +the target branch for file creation or copy-up. By default, the highest
1769 +writable branch which has the parent (or ancestor) dir of the target
1770 +file is chosen (top-down-parent policy).
1771 +By user's request, aufs implements some other policies to select the
1772 +writable branch, for file creation several policies, round-robin,
1773 +most-free-space, and other policies. For copy-up, top-down-parent,
1774 +bottom-up-parent, bottom-up and others.
1775 +
1776 +As expected, the round-robin policy selects the branch in circular. When
1777 +you have two writable branches and creates 10 new files, 5 files will be
1778 +created for each branch. mkdir(2) systemcall is an exception. When you
1779 +create 10 new directories, all will be created on the same branch.
1780 +And the most-free-space policy selects the one which has most free
1781 +space among the writable branches. The amount of free space will be
1782 +checked by aufs internally, and users can specify its time interval.
1783 +
1784 +The policies for copy-up is more simple,
1785 +top-down-parent is equivalent to the same named on in create policy,
1786 +bottom-up-parent selects the writable branch where the parent dir
1787 +exists and the nearest upper one from the copyup-source,
1788 +bottom-up selects the nearest upper writable branch from the
1789 +copyup-source, regardless the existence of the parent dir.
1790 +
1791 +There are some rules or exceptions to apply these policies.
1792 +- If there is a readonly branch above the policy-selected branch and
1793 +  the parent dir is marked as opaque (a variation of whiteout), or the
1794 +  target (creating) file is whiteout-ed on the upper readonly branch,
1795 +  then the result of the policy is ignored and the target file will be
1796 +  created on the nearest upper writable branch than the readonly branch.
1797 +- If there is a writable branch above the policy-selected branch and
1798 +  the parent dir is marked as opaque or the target file is whiteouted
1799 +  on the branch, then the result of the policy is ignored and the target
1800 +  file will be created on the highest one among the upper writable
1801 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1802 +  it as usual.
1803 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1804 +  They try selecting the branch where the source exists as possible
1805 +  since copyup a large file will take long time. If it can't be,
1806 +  ie. the branch where the source exists is readonly, then they will
1807 +  follow the copyup policy.
1808 +- There is an exception for rename(2) when the target exists.
1809 +  If the rename target exists, aufs compares the index of the branches
1810 +  where the source and the target exists and selects the higher
1811 +  one. If the selected branch is readonly, then aufs follows the
1812 +  copyup policy.
1813 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1814 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1815 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2022-11-05 23:02:18.959222617 +0100
1816 @@ -0,0 +1,44 @@
1817 +
1818 +// to view this graph, run dot(1) command in GRAPHVIZ.
1819 +//
1820 +// This program is free software; you can redistribute it and/or modify
1821 +// it under the terms of the GNU General Public License as published by
1822 +// the Free Software Foundation; either version 2 of the License, or
1823 +// (at your option) any later version.
1824 +//
1825 +// This program is distributed in the hope that it will be useful,
1826 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
1827 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1828 +// GNU General Public License for more details.
1829 +//
1830 +// You should have received a copy of the GNU General Public License
1831 +// along with this program.  If not, see <http://www.gnu.org/licenses/>.
1832 +
1833 +digraph G {
1834 +node [shape=box];
1835 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1836 +
1837 +node [shape=oval];
1838 +
1839 +aufs_rename -> whinfo [label="store/remove"];
1840 +
1841 +node [shape=oval];
1842 +inode_list [label="h_inum list in branch\ncache"];
1843 +
1844 +node [shape=box];
1845 +whinode [label="h_inum list file"];
1846 +
1847 +node [shape=oval];
1848 +brmgmt [label="br_add/del/mod/umount"];
1849 +
1850 +brmgmt -> inode_list [label="create/remove"];
1851 +brmgmt -> whinode [label="load/store"];
1852 +
1853 +inode_list -> whinode [style=dashed,dir=both];
1854 +
1855 +aufs_rename -> inode_list [label="add/del"];
1856 +
1857 +aufs_lookup -> inode_list [label="search"];
1858 +
1859 +aufs_lookup -> whinfo [label="load/remove"];
1860 +}
1861 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1862 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1863 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2022-11-05 23:02:18.959222617 +0100
1864 @@ -0,0 +1,102 @@
1865 +
1866 +# Copyright (C) 2017-2022 Junjiro R. Okajima
1867 +#
1868 +# This program is free software; you can redistribute it and/or modify
1869 +# it under the terms of the GNU General Public License as published by
1870 +# the Free Software Foundation; either version 2 of the License, or
1871 +# (at your option) any later version.
1872 +#
1873 +# This program is distributed in the hope that it will be useful,
1874 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1875 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1876 +# GNU General Public License for more details.
1877 +#
1878 +# You should have received a copy of the GNU General Public License
1879 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1880 +
1881 +Special handling for renaming a directory (DIRREN)
1882 +----------------------------------------------------------------------
1883 +First, let's assume we have a simple usecase.
1884 +
1885 +- /u = /rw + /ro
1886 +- /rw/dirA exists
1887 +- /ro/dirA and /ro/dirA/file exist too
1888 +- there is no dirB on both branches
1889 +- a user issues rename("dirA", "dirB")
1890 +
1891 +Now, what should aufs behave against this rename(2)?
1892 +There are a few possible cases.
1893 +
1894 +A. returns EROFS.
1895 +   since dirA exists on a readonly branch which cannot be renamed.
1896 +B. returns EXDEV.
1897 +   it is possible to copy-up dirA (only the dir itself), but the child
1898 +   entries ("file" in this case) should not be. it must be a bad
1899 +   approach to copy-up recursively.
1900 +C. returns a success.
1901 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1902 +   is a violation of aufs' policy.
1903 +D. construct an extra information which indicates that /ro/dirA should
1904 +   be handled as the name of dirB.
1905 +   overlayfs has a similar feature called REDIRECT.
1906 +
1907 +Until now, aufs implements the case B only which returns EXDEV, and
1908 +expects the userspace application behaves like mv(1) which tries
1909 +issueing rename(2) recursively.
1910 +
1911 +A new aufs feature called DIRREN is introduced which implements the case
1912 +D. There are several "extra information" added.
1913 +
1914 +1. detailed info per renamed directory
1915 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1916 +2. the inode-number list of directories on a branch
1917 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1918 +
1919 +The filename of "detailed info per directory" represents the lower
1920 +branch, and its format is
1921 +- a type of the branch id
1922 +  one of these.
1923 +  + uuid (not implemented yet)
1924 +  + fsid
1925 +  + dev
1926 +- the inode-number of the branch root dir
1927 +
1928 +And it contains these info in a single regular file.
1929 +- magic number
1930 +- branch's inode-number of the logically renamed dir
1931 +- the name of the before-renamed dir
1932 +
1933 +The "detailed info per directory" file is created in aufs rename(2), and
1934 +loaded in any lookup.
1935 +The info is considered in lookup for the matching case only. Here
1936 +"matching" means that the root of branch (in the info filename) is same
1937 +to the current looking-up branch. After looking-up the before-renamed
1938 +name, the inode-number is compared. And the matched dentry is used.
1939 +
1940 +The "inode-number list of directories" is a regular file which contains
1941 +simply the inode-numbers on the branch. The file is created or updated
1942 +in removing the branch, and loaded in adding the branch. Its lifetime is
1943 +equal to the branch.
1944 +The list is referred in lookup, and when the current target inode is
1945 +found in the list, the aufs tries loading the "detailed info per
1946 +directory" and get the changed and valid name of the dir.
1947 +
1948 +Theoretically these "extra informaiton" may be able to be put into XATTR
1949 +in the dir inode. But aufs doesn't choose this way because
1950 +1. XATTR may not be supported by the branch (or its configuration)
1951 +2. XATTR may have its size limit.
1952 +3. XATTR may be less easy to convert than a regular file, when the
1953 +   format of the info is changed in the future.
1954 +At the same time, I agree that the regular file approach is much slower
1955 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1956 +better approach.
1957 +
1958 +This DIRREN feature is enabled by aufs configuration, and is activated
1959 +by a new mount option.
1960 +
1961 +For the more complicated case, there is a work with UDBA option, which
1962 +is to dected the direct access to the branches (by-passing aufs) and to
1963 +maintain the cashes in aufs. Since a single cached aufs dentry may
1964 +contains two names, before- and after-rename, the name comparision in
1965 +UDBA handler may not work correctly. In this case, the behaviour will be
1966 +equivalen to udba=reval case.
1967 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1968 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1969 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2022-11-05 23:02:18.959222617 +0100
1970 @@ -0,0 +1,118 @@
1971 +
1972 +# Copyright (C) 2011-2022 Junjiro R. Okajima
1973 +#
1974 +# This program is free software; you can redistribute it and/or modify
1975 +# it under the terms of the GNU General Public License as published by
1976 +# the Free Software Foundation; either version 2 of the License, or
1977 +# (at your option) any later version.
1978 +#
1979 +# This program is distributed in the hope that it will be useful,
1980 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1981 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1982 +# GNU General Public License for more details.
1983 +#
1984 +# You should have received a copy of the GNU General Public License
1985 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1986 +
1987 +File-based Hierarchical Storage Management (FHSM)
1988 +----------------------------------------------------------------------
1989 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1990 +storage world. Aufs provides this feature as file-based with multiple
1991 +writable branches, based upon the principle of "Colder, the Lower".
1992 +Here the word "colder" means that the less used files, and "lower" means
1993 +that the position in the order of the stacked branches vertically.
1994 +These multiple writable branches are prioritized, ie. the topmost one
1995 +should be the fastest drive and be used heavily.
1996 +
1997 +o Characters in aufs FHSM story
1998 +- aufs itself and a new branch attribute.
1999 +- a new ioctl interface to move-down and to establish a connection with
2000 +  the daemon ("move-down" is a converse of "copy-up").
2001 +- userspace tool and daemon.
2002 +
2003 +The userspace daemon establishes a connection with aufs and waits for
2004 +the notification. The notified information is very similar to struct
2005 +statfs containing the number of consumed blocks and inodes.
2006 +When the consumed blocks/inodes of a branch exceeds the user-specified
2007 +upper watermark, the daemon activates its move-down process until the
2008 +consumed blocks/inodes reaches the user-specified lower watermark.
2009 +
2010 +The actual move-down is done by aufs based upon the request from
2011 +user-space since we need to maintain the inode number and the internal
2012 +pointer arrays in aufs.
2013 +
2014 +Currently aufs FHSM handles the regular files only. Additionally they
2015 +must not be hard-linked nor pseudo-linked.
2016 +
2017 +
2018 +o Cowork of aufs and the user-space daemon
2019 +  During the userspace daemon established the connection, aufs sends a
2020 +  small notification to it whenever aufs writes something into the
2021 +  writable branch. But it may cost high since aufs issues statfs(2)
2022 +  internally. So user can specify a new option to cache the
2023 +  info. Actually the notification is controlled by these factors.
2024 +  + the specified cache time.
2025 +  + classified as "force" by aufs internally.
2026 +  Until the specified time expires, aufs doesn't send the info
2027 +  except the forced cases. When aufs decide forcing, the info is always
2028 +  notified to userspace.
2029 +  For example, the number of free inodes is generally large enough and
2030 +  the shortage of it happens rarely. So aufs doesn't force the
2031 +  notification when creating a new file, directory and others. This is
2032 +  the typical case which aufs doesn't force.
2033 +  When aufs writes the actual filedata and the files consumes any of new
2034 +  blocks, the aufs forces notifying.
2035 +
2036 +
2037 +o Interfaces in aufs
2038 +- New branch attribute.
2039 +  + fhsm
2040 +    Specifies that the branch is managed by FHSM feature. In other word,
2041 +    participant in the FHSM.
2042 +    When nofhsm is set to the branch, it will not be the source/target
2043 +    branch of the move-down operation. This attribute is set
2044 +    independently from coo and moo attributes, and if you want full
2045 +    FHSM, you should specify them as well.
2046 +- New mount option.
2047 +  + fhsm_sec
2048 +    Specifies a second to suppress many less important info to be
2049 +    notified.
2050 +- New ioctl.
2051 +  + AUFS_CTL_FHSM_FD
2052 +    create a new file descriptor which userspace can read the notification
2053 +    (a subset of struct statfs) from aufs.
2054 +- Module parameter 'brs'
2055 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2056 +  be set.
2057 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2058 +  When there are two or more branches with fhsm attributes,
2059 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2060 +  terminates it. As a result of remounting and branch-manipulation, the
2061 +  number of branches with fhsm attribute can be one. In this case,
2062 +  /sbin/mount.aufs will terminate the user-space daemon.
2063 +
2064 +
2065 +Finally the operation is done as these steps in kernel-space.
2066 +- make sure that,
2067 +  + no one else is using the file.
2068 +  + the file is not hard-linked.
2069 +  + the file is not pseudo-linked.
2070 +  + the file is a regular file.
2071 +  + the parent dir is not opaqued.
2072 +- find the target writable branch.
2073 +- make sure the file is not whiteout-ed by the upper (than the target)
2074 +  branch.
2075 +- make the parent dir on the target branch.
2076 +- mutex lock the inode on the branch.
2077 +- unlink the whiteout on the target branch (if exists).
2078 +- lookup and create the whiteout-ed temporary name on the target branch.
2079 +- copy the file as the whiteout-ed temporary name on the target branch.
2080 +- rename the whiteout-ed temporary name to the original name.
2081 +- unlink the file on the source branch.
2082 +- maintain the internal pointer array and the external inode number
2083 +  table (XINO).
2084 +- maintain the timestamps and other attributes of the parent dir and the
2085 +  file.
2086 +
2087 +And of course, in every step, an error may happen. So the operation
2088 +should restore the original file state after an error happens.
2089 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2090 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2091 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2022-11-05 23:02:18.959222617 +0100
2092 @@ -0,0 +1,72 @@
2093 +
2094 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2095 +#
2096 +# This program is free software; you can redistribute it and/or modify
2097 +# it under the terms of the GNU General Public License as published by
2098 +# the Free Software Foundation; either version 2 of the License, or
2099 +# (at your option) any later version.
2100 +#
2101 +# This program is distributed in the hope that it will be useful,
2102 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2103 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2104 +# GNU General Public License for more details.
2105 +#
2106 +# You should have received a copy of the GNU General Public License
2107 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2108 +
2109 +mmap(2) -- File Memory Mapping
2110 +----------------------------------------------------------------------
2111 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2112 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2113 +->mmap().
2114 +This approach is simple and good, but there is one problem.
2115 +Under /proc, several entries show the mmapped files by its path (with
2116 +device and inode number), and the printed path will be the path on the
2117 +branch fs's instead of virtual aufs's.
2118 +This is not a problem in most cases, but some utilities lsof(1) (and its
2119 +user) may expect the path on aufs.
2120 +
2121 +To address this issue, aufs adds a new member called vm_prfile in struct
2122 +vm_area_struct (and struct vm_region). The original vm_file points to
2123 +the file on the branch fs in order to handle everything correctly as
2124 +usual. The new vm_prfile points to a virtual file in aufs, and the
2125 +show-functions in procfs refers to vm_prfile if it is set.
2126 +Also we need to maintain several other places where touching vm_file
2127 +such like
2128 +- fork()/clone() copies vma and the reference count of vm_file is
2129 +  incremented.
2130 +- merging vma maintains the ref count too.
2131 +
2132 +This is not a good approach. It just fakes the printed path. But it
2133 +leaves all behaviour around f_mapping unchanged. This is surely an
2134 +advantage.
2135 +Actually aufs had adopted another complicated approach which calls
2136 +generic_file_mmap() and handles struct vm_operations_struct. In this
2137 +approach, aufs met a hard problem and I could not solve it without
2138 +switching the approach.
2139 +
2140 +There may be one more another approach which is
2141 +- bind-mount the branch-root onto the aufs-root internally
2142 +- grab the new vfsmount (ie. struct mount)
2143 +- lazy-umount the branch-root internally
2144 +- in open(2) the aufs-file, open the branch-file with the hidden
2145 +  vfsmount (instead of the original branch's vfsmount)
2146 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2147 +  but it may be possible from userspace by the mount helper.
2148 +
2149 +Adding the internal hidden vfsmount and using it in opening a file, the
2150 +file path under /proc will be printed correctly. This approach looks
2151 +smarter, but is not possible I am afraid.
2152 +- aufs-root may be bind-mount later. when it happens, another hidden
2153 +  vfsmount will be required.
2154 +- it is hard to get the chance to bind-mount and lazy-umount
2155 +  + in kernel-space, FS can have vfsmount in open(2) via
2156 +    file->f_path, and aufs can know its vfsmount. But several locks are
2157 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2158 +    here, then it may cause a deadlock.
2159 +  + in user-space, bind-mount doesn't invoke the mount helper.
2160 +- since /proc shows dev and ino, aufs has to give vma these info. it
2161 +  means a new member vm_prinode will be necessary. this is essentially
2162 +  equivalent to vm_prfile described above.
2163 +
2164 +I have to give up this "looks-smater" approach.
2165 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2166 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2167 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2022-11-05 23:02:18.959222617 +0100
2168 @@ -0,0 +1,94 @@
2169 +
2170 +# Copyright (C) 2014-2022 Junjiro R. Okajima
2171 +#
2172 +# This program is free software; you can redistribute it and/or modify
2173 +# it under the terms of the GNU General Public License as published by
2174 +# the Free Software Foundation; either version 2 of the License, or
2175 +# (at your option) any later version.
2176 +#
2177 +# This program is distributed in the hope that it will be useful,
2178 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2179 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2180 +# GNU General Public License for more details.
2181 +#
2182 +# You should have received a copy of the GNU General Public License
2183 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2184 +
2185 +Listing XATTR/EA and getting the value
2186 +----------------------------------------------------------------------
2187 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2188 +shows the values from the topmost existing file. This behaviour is good
2189 +for the non-dir entries since the bahaviour exactly matches the shown
2190 +information. But for the directories, aufs considers all the same named
2191 +entries on the lower branches. Which means, if one of the lower entry
2192 +rejects readdir call, then aufs returns an error even if the topmost
2193 +entry allows it. This behaviour is necessary to respect the branch fs's
2194 +security, but can make users confused since the user-visible standard
2195 +attributes don't match the behaviour.
2196 +To address this issue, aufs has a mount option called dirperm1 which
2197 +checks the permission for the topmost entry only, and ignores the lower
2198 +entry's permission.
2199 +
2200 +A similar issue can happen around XATTR.
2201 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2202 +always set. Otherwise these very unpleasant situation would happen.
2203 +- listxattr(2) may return the duplicated entries.
2204 +- users may not be able to remove or reset the XATTR forever,
2205 +
2206 +
2207 +XATTR/EA support in the internal (copy,move)-(up,down)
2208 +----------------------------------------------------------------------
2209 +Generally the extended attributes of inode are categorized as these.
2210 +- "security" for LSM and capability.
2211 +- "system" for posix ACL, 'acl' mount option is required for the branch
2212 +  fs generally.
2213 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2214 +- "user" for userspace, 'user_xattr' mount option is required for the
2215 +  branch fs generally.
2216 +
2217 +Moreover there are some other categories. Aufs handles these rather
2218 +unpopular categories as the ordinary ones, ie. there is no special
2219 +condition nor exception.
2220 +
2221 +In copy-up, the support for XATTR on the dst branch may differ from the
2222 +src branch. In this case, the copy-up operation will get an error and
2223 +the original user operation which triggered the copy-up will fail. It
2224 +can happen that even all copy-up will fail.
2225 +When both of src and dst branches support XATTR and if an error occurs
2226 +during copying XATTR, then the copy-up should fail obviously. That is a
2227 +good reason and aufs should return an error to userspace. But when only
2228 +the src branch support that XATTR, aufs should not return an error.
2229 +For example, the src branch supports ACL but the dst branch doesn't
2230 +because the dst branch may natively un-support it or temporary
2231 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2232 +may NOT return an error even if the XATTR is not supported. It is
2233 +totally up to the branch fs.
2234 +
2235 +Anyway when the aufs internal copy-up gets an error from the dst branch
2236 +fs, then aufs tries removing the just copied entry and returns the error
2237 +to the userspace. The worst case of this situation will be all copy-up
2238 +will fail.
2239 +
2240 +For the copy-up operation, there two basic approaches.
2241 +- copy the specified XATTR only (by category above), and return the
2242 +  error unconditionally if it happens.
2243 +- copy all XATTR, and ignore the error on the specified category only.
2244 +
2245 +In order to support XATTR and to implement the correct behaviour, aufs
2246 +chooses the latter approach and introduces some new branch attributes,
2247 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2248 +They correspond to the XATTR namespaces (see above). Additionally, to be
2249 +convenient, "icex" is also provided which means all "icex*" attributes
2250 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2251 +
2252 +The meaning of these attributes is to ignore the error from setting
2253 +XATTR on that branch.
2254 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2255 +error from the dst branch according to the specified attributes.
2256 +
2257 +Some XATTR may have its default value. The default value may come from
2258 +the parent dir or the environment. If the default value is set at the
2259 +file creating-time, it will be overwritten by copy-up.
2260 +Some contradiction may happen I am afraid.
2261 +Do we need another attribute to stop copying XATTR? I am unsure. For
2262 +now, aufs implements the branch attributes to ignore the error.
2263 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2264 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2265 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2022-11-05 23:02:18.959222617 +0100
2266 @@ -0,0 +1,58 @@
2267 +
2268 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2269 +#
2270 +# This program is free software; you can redistribute it and/or modify
2271 +# it under the terms of the GNU General Public License as published by
2272 +# the Free Software Foundation; either version 2 of the License, or
2273 +# (at your option) any later version.
2274 +#
2275 +# This program is distributed in the hope that it will be useful,
2276 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2277 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2278 +# GNU General Public License for more details.
2279 +#
2280 +# You should have received a copy of the GNU General Public License
2281 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2282 +
2283 +Export Aufs via NFS
2284 +----------------------------------------------------------------------
2285 +Here is an approach.
2286 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2287 +  generation.
2288 +- iget_locked(): initialize aufs inode generation for a new inode, and
2289 +  store it in xigen file.
2290 +- destroy_inode(): increment aufs inode generation and store it in xigen
2291 +  file. it is necessary even if it is not unlinked, because any data of
2292 +  inode may be changed by UDBA.
2293 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2294 +  build file handle by
2295 +  + branch id (4 bytes)
2296 +  + superblock generation (4 bytes)
2297 +  + inode number (4 or 8 bytes)
2298 +  + parent dir inode number (4 or 8 bytes)
2299 +  + inode generation (4 bytes))
2300 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2301 +    bytes)
2302 +  + file handle for a branch (by exportfs_encode_fh())
2303 +- fh_to_dentry():
2304 +  + find the index of a branch from its id in handle, and check it is
2305 +    still exist in aufs.
2306 +  + 1st level: get the inode number from handle and search it in cache.
2307 +  + 2nd level: if not found in cache, get the parent inode number from
2308 +    the handle and search it in cache. and then open the found parent
2309 +    dir, find the matching inode number by vfs_readdir() and get its
2310 +    name, and call lookup_one_len() for the target dentry.
2311 +  + 3rd level: if the parent dir is not cached, call
2312 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2313 +    build a pathname of it, convert it a pathname in aufs, call
2314 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2315 +    the 2nd level.
2316 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2317 +    for every branch, but not itself. to get this, (currently) aufs
2318 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2319 +    idea, but I didn't get other approach.
2320 +  + test the generation of the gotten inode.
2321 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2322 +  convert it into ESTALE for NFSD.
2323 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2324 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2325 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2326 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2327 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2022-11-05 23:02:18.959222617 +0100
2328 @@ -0,0 +1,52 @@
2329 +
2330 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2331 +#
2332 +# This program is free software; you can redistribute it and/or modify
2333 +# it under the terms of the GNU General Public License as published by
2334 +# the Free Software Foundation; either version 2 of the License, or
2335 +# (at your option) any later version.
2336 +#
2337 +# This program is distributed in the hope that it will be useful,
2338 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2339 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2340 +# GNU General Public License for more details.
2341 +#
2342 +# You should have received a copy of the GNU General Public License
2343 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2344 +
2345 +Show Whiteout Mode (shwh)
2346 +----------------------------------------------------------------------
2347 +Generally aufs hides the name of whiteouts. But in some cases, to show
2348 +them is very useful for users. For instance, creating a new middle layer
2349 +(branch) by merging existing layers.
2350 +
2351 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2352 +When you have three branches,
2353 +- Bottom: 'system', squashfs (underlying base system), read-only
2354 +- Middle: 'mods', squashfs, read-only
2355 +- Top: 'overlay', ram (tmpfs), read-write
2356 +
2357 +The top layer is loaded at boot time and saved at shutdown, to preserve
2358 +the changes made to the system during the session.
2359 +When larger changes have been made, or smaller changes have accumulated,
2360 +the size of the saved top layer data grows. At this point, it would be
2361 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2362 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2363 +restoring save and load speed.
2364 +
2365 +This merging is simplified by the use of another aufs mount, of just the
2366 +two overlay branches using the 'shwh' option.
2367 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2368 +       aufs /livesys/merge_union
2369 +
2370 +A merged view of these two branches is then available at
2371 +/livesys/merge_union, and the new feature is that the whiteouts are
2372 +visible!
2373 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2374 +writing to all branches. Also the default mode for all branches is 'ro'.
2375 +It is now possible to save the combined contents of the two overlay
2376 +branches to a new squashfs, e.g.:
2377 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2378 +
2379 +This new squashfs archive can be stored on the boot device and the
2380 +initramfs will use it to replace the old one at the next boot.
2381 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2382 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2383 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2022-11-05 23:02:18.959222617 +0100
2384 @@ -0,0 +1,47 @@
2385 +
2386 +# Copyright (C) 2010-2022 Junjiro R. Okajima
2387 +#
2388 +# This program is free software; you can redistribute it and/or modify
2389 +# it under the terms of the GNU General Public License as published by
2390 +# the Free Software Foundation; either version 2 of the License, or
2391 +# (at your option) any later version.
2392 +#
2393 +# This program is distributed in the hope that it will be useful,
2394 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2395 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2396 +# GNU General Public License for more details.
2397 +#
2398 +# You should have received a copy of the GNU General Public License
2399 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2400 +
2401 +Dynamically customizable FS operations
2402 +----------------------------------------------------------------------
2403 +Generally FS operations (struct inode_operations, struct
2404 +address_space_operations, struct file_operations, etc.) are defined as
2405 +"static const", but it never means that FS have only one set of
2406 +operation. Some FS have multiple sets of them. For instance, ext2 has
2407 +three sets, one for XIP, for NOBH, and for normal.
2408 +Since aufs overrides and redirects these operations, sometimes aufs has
2409 +to change its behaviour according to the branch FS type. More importantly
2410 +VFS acts differently if a function (member in the struct) is set or
2411 +not. It means aufs should have several sets of operations and select one
2412 +among them according to the branch FS definition.
2413 +
2414 +In order to solve this problem and not to affect the behaviour of VFS,
2415 +aufs defines these operations dynamically. For instance, aufs defines
2416 +dummy direct_IO function for struct address_space_operations, but it may
2417 +not be set to the address_space_operations actually. When the branch FS
2418 +doesn't have it, aufs doesn't set it to its address_space_operations
2419 +while the function definition itself is still alive. So the behaviour
2420 +itself will not change, and it will return an error when direct_IO is
2421 +not set.
2422 +
2423 +The lifetime of these dynamically generated operation object is
2424 +maintained by aufs branch object. When the branch is removed from aufs,
2425 +the reference counter of the object is decremented. When it reaches
2426 +zero, the dynamically generated operation object will be freed.
2427 +
2428 +This approach is designed to support AIO (io_submit), Direct I/O and
2429 +XIP (DAX) mainly.
2430 +Currently this approach is applied to address_space_operations for
2431 +regular files only.
2432 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2433 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2434 +++ linux/Documentation/filesystems/aufs/README 2023-08-28 12:34:41.669969465 +0200
2435 @@ -0,0 +1,409 @@
2436 +
2437 +Aufs6 -- advanced multi layered unification filesystem version 6.x
2438 +http://aufs.sf.net
2439 +Junjiro R. Okajima
2440 +
2441 +
2442 +0. Introduction
2443 +----------------------------------------
2444 +In the early days, aufs was entirely re-designed and re-implemented
2445 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2446 +improvements and implementations, it became totally different from
2447 +Unionfs while keeping the basic features.
2448 +Later, Unionfs Version 2.x series began taking some of the same
2449 +approaches to aufs1's.
2450 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2451 +University and his team.
2452 +
2453 +Aufs6 supports linux-v6.0 and later, try aufs6.0 branch in
2454 +aufs-linux.git or aufs-standalone.git.
2455 +If you want older kernel version support,
2456 +- for linux-v5.x series, try aufs-linux.git or aufs-standalone.git
2457 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2458 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2459 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2460 +  or aufs1 from CVS on SourceForge.
2461 +
2462 +Note: the name of aufs5-linux.git and aufs5-standalone.git on github
2463 +      were changed. Now they are aufs-linux.git and
2464 +      aufs-standalone.git and they contain aufs5 and later branches.
2465 +
2466 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2467 +      According to Christoph Hellwig, linux rejects all union-type
2468 +      filesystems but UnionMount.
2469 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2470 +
2471 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2472 +    UnionMount, and he pointed out an issue around a directory mutex
2473 +    lock and aufs addressed it. But it is still unsure whether aufs will
2474 +    be merged (or any other union solution).
2475 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2476 +
2477 +
2478 +1. Features
2479 +----------------------------------------
2480 +- unite several directories into a single virtual filesystem. The member
2481 +  directory is called as a branch.
2482 +- you can specify the permission flags to the branch, which are 'readonly',
2483 +  'readwrite' and 'whiteout-able.'
2484 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2485 +  readonly branch are modifiable logically.
2486 +- dynamic branch manipulation, add, del.
2487 +- etc...
2488 +
2489 +Also there are many enhancements in aufs, such as:
2490 +- test only the highest one for the directory permission (dirperm1)
2491 +- copyup on open (coo=)
2492 +- 'move' policy for copy-up between two writable branches, after
2493 +  checking free space.
2494 +- xattr, acl
2495 +- readdir(3) in userspace.
2496 +- keep inode number by external inode number table
2497 +- keep the timestamps of file/dir in internal copyup operation
2498 +- seekable directory, supporting NFS readdir.
2499 +- whiteout is hardlinked in order to reduce the consumption of inodes
2500 +  on branch
2501 +- do not copyup, nor create a whiteout when it is unnecessary
2502 +- revert a single systemcall when an error occurs in aufs
2503 +- remount interface instead of ioctl
2504 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2505 +- loopback mounted filesystem as a branch
2506 +- kernel thread for removing the dir who has a plenty of whiteouts
2507 +- support copyup sparse file (a file which has a 'hole' in it)
2508 +- default permission flags for branches
2509 +- selectable permission flags for ro branch, whether whiteout can
2510 +  exist or not
2511 +- export via NFS.
2512 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2513 +- support multiple writable branches, some policies to select one
2514 +  among multiple writable branches.
2515 +- a new semantics for link(2) and rename(2) to support multiple
2516 +  writable branches.
2517 +- no glibc changes are required.
2518 +- pseudo hardlink (hardlink over branches)
2519 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2520 +  including NFS or remote filesystem branch.
2521 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2522 +- and more...
2523 +
2524 +Currently these features are dropped temporary from aufs6.
2525 +See design/08plan.txt in detail.
2526 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2527 +  (robr)
2528 +- statistics of aufs thread (/sys/fs/aufs/stat)
2529 +
2530 +Features or just an idea in the future (see also design/*.txt),
2531 +- reorder the branch index without del/re-add.
2532 +- permanent xino files for NFSD
2533 +- an option for refreshing the opened files after add/del branches
2534 +- light version, without branch manipulation. (unnecessary?)
2535 +- copyup in userspace
2536 +- inotify in userspace
2537 +- readv/writev
2538 +
2539 +
2540 +2. Download
2541 +----------------------------------------
2542 +There are three GIT trees for aufs6, aufs-linux.git,
2543 +aufs-standalone.git, and aufs-util.git.
2544 +While the aufs-util is always necessary, you need either of aufs-linux
2545 +or aufs-standalone.
2546 +
2547 +The aufs-linux tree includes the whole linux mainline GIT tree,
2548 +git://git.kernel.org/.../torvalds/linux.git.
2549 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2550 +build aufs6 as an external kernel module.
2551 +Several extra patches are not included in this tree. Only
2552 +aufs-standalone tree contains them. They are described in the later
2553 +section "Configuration and Compilation."
2554 +
2555 +On the other hand, the aufs-standalone tree has only aufs source files
2556 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2557 +But you need to apply all aufs patches manually.
2558 +
2559 +You will find GIT branches whose name is in form of "aufs6.x" where "x"
2560 +represents the linux kernel version, "linux-6.x". For instance,
2561 +"aufs6.0" is for linux-6.0. For latest "linux-6.x-rcN", use
2562 +"aufs6.x-rcN" branch.
2563 +
2564 +o aufs-linux tree
2565 +$ git clone --reference /your/linux/git/tree \
2566 +       git://github.com/sfjro/aufs-linux.git aufs-linux.git
2567 +- if you don't have linux GIT tree, then remove "--reference ..."
2568 +$ cd aufs-linux.git
2569 +$ git checkout origin/aufs6.0
2570 +
2571 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2572 +leave the patch-work to GIT.
2573 +$ cd /your/linux/git/tree
2574 +$ git remote add aufs git://github.com/sfjro/aufs-linux.git
2575 +$ git fetch aufs
2576 +$ git checkout -b my6.0 v6.0
2577 +$ (add your local change...)
2578 +$ git pull aufs aufs6.0
2579 +- now you have v6.0 + your_changes + aufs6.0 in you my6.0 branch.
2580 +- you may need to solve some conflicts between your_changes and
2581 +  aufs6.0. in this case, git-rerere is recommended so that you can
2582 +  solve the similar conflicts automatically when you upgrade to 6.1 or
2583 +  later in the future.
2584 +
2585 +o aufs-standalone tree
2586 +$ git clone git://github.com/sfjro/aufs-standalone.git aufs-standalone.git
2587 +$ cd aufs-standalone.git
2588 +$ git checkout origin/aufs6.0
2589 +
2590 +o aufs-util tree
2591 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2592 +- note that the public aufs-util.git is on SourceForge instead of
2593 +  GitHUB.
2594 +$ cd aufs-util.git
2595 +$ git checkout origin/aufs6.0
2596 +
2597 +Note: The 6.x-rcN branch is to be used with `rc' kernel versions ONLY.
2598 +The minor version number, 'x' in '6.x', of aufs may not always
2599 +follow the minor version number of the kernel.
2600 +Because changes in the kernel that cause the use of a new
2601 +minor version number do not always require changes to aufs-util.
2602 +
2603 +Since aufs-util has its own minor version number, you may not be
2604 +able to find a GIT branch in aufs-util for your kernel's
2605 +exact minor version number.
2606 +In this case, you should git-checkout the branch for the
2607 +nearest lower number.
2608 +
2609 +For (an unreleased) example:
2610 +If you are using "linux-6.10" and the "aufs6.10" branch
2611 +does not exist in aufs-util repository, then "aufs6.9", "aufs6.8"
2612 +or something numerically smaller is the branch for your kernel.
2613 +
2614 +Also you can view all branches by
2615 +       $ git branch -a
2616 +
2617 +
2618 +3. Configuration and Compilation
2619 +----------------------------------------
2620 +Make sure you have git-checkout'ed the correct branch.
2621 +
2622 +For aufs-linux tree,
2623 +- enable CONFIG_AUFS_FS.
2624 +- set other aufs configurations if necessary.
2625 +- for aufs5.13 and later
2626 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2627 +  also a caller of VFS functions for branch filesystems, subclassing of
2628 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2629 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2630 +  need to customize some LOCKDEP numbers. Here are what I use on my
2631 +  test environment.
2632 +       CONFIG_LOCKDEP_BITS=21
2633 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2634 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2635 +  Also you will need to expand some constant values in LOCKDEP. Refer
2636 +  to lockdep-debug.patch in aufs-standalone.git.
2637 +
2638 +For aufs-standalone tree,
2639 +There are several ways to build.
2640 +
2641 +1.
2642 +- apply ./aufs6-kbuild.patch to your kernel source files.
2643 +- apply ./aufs6-base.patch too.
2644 +- apply ./aufs6-mmap.patch too.
2645 +- apply ./aufs6-standalone.patch too, if you have a plan to set
2646 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs-standalone.patch.
2647 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2648 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2649 +- enable CONFIG_AUFS_FS, you can select either
2650 +  =m or =y.
2651 +- and build your kernel as usual.
2652 +- install the built kernel.
2653 +- install the header files too by "make headers_install" to the
2654 +  directory where you specify. By default, it is $PWD/usr.
2655 +  "make help" shows a brief note for headers_install.
2656 +- and reboot your system.
2657 +
2658 +2.
2659 +- module only (CONFIG_AUFS_FS=m).
2660 +- apply ./aufs6-base.patch to your kernel source files.
2661 +- apply ./aufs6-mmap.patch too.
2662 +- apply ./aufs6-standalone.patch too.
2663 +- build your kernel, don't forget "make headers_install", and reboot.
2664 +- edit ./config.mk and set other aufs configurations if necessary.
2665 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2666 +  every aufs configurations.
2667 +- build the module by simple "make".
2668 +- you can specify ${KDIR} make variable which points to your kernel
2669 +  source tree.
2670 +- install the files
2671 +  + run "make install" to install the aufs module, or copy the built
2672 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2673 +  + run "make install_headers" (instead of headers_install) to install
2674 +    the modified aufs header file (you can specify DESTDIR which is
2675 +    available in aufs standalone version's Makefile only), or copy
2676 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2677 +    you like manually. By default, the target directory is $PWD/usr.
2678 +- no need to apply aufs6-kbuild.patch, nor copying source files to your
2679 +  kernel source tree.
2680 +
2681 +Note: The header file aufs_type.h is necessary to build aufs-util
2682 +      as well as "make headers_install" in the kernel source tree.
2683 +      headers_install is subject to be forgotten, but it is essentially
2684 +      necessary, not only for building aufs-util.
2685 +      You may not meet problems without headers_install in some older
2686 +      version though.
2687 +
2688 +And then,
2689 +- read README in aufs-util, build and install it
2690 +- note that your distribution may contain an obsoleted version of
2691 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2692 +  utilities, make sure that your compiler refers the correct aufs header
2693 +  file which is built by "make headers_install."
2694 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2695 +  then run "make install_ulib" too. And refer to the aufs manual in
2696 +  detail.
2697 +
2698 +There several other patches in aufs-standalone.git. They are all
2699 +optional. When you meet some problems, they will help you.
2700 +- aufs6-loopback.patch
2701 +  Supports a nested loopback mount in a branch-fs. This patch is
2702 +  unnecessary until aufs produces a message like "you may want to try
2703 +  another patch for loopback file".
2704 +- vfs-ino.patch
2705 +  Modifies a system global kernel internal function get_next_ino() in
2706 +  order to stop assigning 0 for an inode-number. Not directly related to
2707 +  aufs, but recommended generally.
2708 +- tmpfs-idr.patch
2709 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2710 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2711 +  duplication of inode number, which is important for backup tools and
2712 +  other utilities. When you find aufs XINO files for tmpfs branch
2713 +  growing too much, try this patch.
2714 +- lockdep-debug.patch
2715 +  Similar to some kernel configurations for LOCKDEP (see the top of
2716 +  this section), you will need expand some constants in LOCKDEP for
2717 +  aufs if you enable CONFIG_LOCKDEP.
2718 +
2719 +
2720 +4. Usage
2721 +----------------------------------------
2722 +At first, make sure aufs-util are installed, and please read the aufs
2723 +manual, aufs.5 in aufs-util.git tree.
2724 +$ man -l aufs.5
2725 +
2726 +And then,
2727 +$ mkdir /tmp/rw /tmp/aufs
2728 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2729 +
2730 +Here is another example. The result is equivalent.
2731 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2732 +  Or
2733 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2734 +# mount -o remount,append:${HOME} /tmp/aufs
2735 +
2736 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2737 +you modify a file under /tmp/aufs, the one on your home directory is
2738 +not affected, instead the same named file will be newly created under
2739 +/tmp/rw. And all of your modification to a file will be applied to
2740 +the one under /tmp/rw. This is called the file based Copy on Write
2741 +(COW) method.
2742 +Aufs mount options are described in aufs.5.
2743 +If you run chroot or something and make your aufs as a root directory,
2744 +then you need to customize the shutdown script. See the aufs manual in
2745 +detail.
2746 +
2747 +Additionally, there are some sample usages of aufs which are a
2748 +diskless system with network booting, and LiveCD over NFS.
2749 +See sample dir in CVS tree on SourceForge.
2750 +
2751 +
2752 +5. Contact
2753 +----------------------------------------
2754 +When you have any problems or strange behaviour in aufs, please let me
2755 +know with:
2756 +- /proc/mounts (instead of the output of mount(8))
2757 +- /sys/module/aufs/*
2758 +- /sys/fs/aufs/* (if you have them)
2759 +- /debug/aufs/* (if you have them)
2760 +- linux kernel version
2761 +  if your kernel is not plain, for example modified by distributor,
2762 +  the url where i can download its source is necessary too.
2763 +- aufs version which was printed at loading the module or booting the
2764 +  system, instead of the date you downloaded.
2765 +- configuration (define/undefine CONFIG_AUFS_xxx)
2766 +- kernel configuration or /proc/config.gz (if you have it)
2767 +- LSM (linux security module, if you are using)
2768 +- behaviour which you think to be incorrect
2769 +- actual operation, reproducible one is better
2770 +- mailto: aufs-users at lists.sourceforge.net
2771 +
2772 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2773 +and Feature Requests) on SourceForge. Please join and write to
2774 +aufs-users ML.
2775 +
2776 +
2777 +6. Acknowledgements
2778 +----------------------------------------
2779 +Thanks to everyone who have tried and are using aufs, whoever
2780 +have reported a bug or any feedback.
2781 +
2782 +Especially donators:
2783 +Tomas Matejicek(slax.org) made a donation (much more than once).
2784 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2785 +       scripts) is making "doubling" donations.
2786 +       Unfortunately I cannot list all of the donators, but I really
2787 +       appreciate.
2788 +       It ends Aug 2010, but the ordinary donation URL is still available.
2789 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2790 +Dai Itasaka made a donation (2007/8).
2791 +Chuck Smith made a donation (2008/4, 10 and 12).
2792 +Henk Schoneveld made a donation (2008/9).
2793 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2794 +Francois Dupoux made a donation (2008/11).
2795 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2796 +       aufs2 GIT tree (2009/2).
2797 +William Grant made a donation (2009/3).
2798 +Patrick Lane made a donation (2009/4).
2799 +The Mail Archive (mail-archive.com) made donations (2009/5).
2800 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2801 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2802 +Pavel Pronskiy made a donation (2011/2).
2803 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2804 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2805 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2806 +11).
2807 +Sam Liddicott made a donation (2011/9).
2808 +Era Scarecrow made a donation (2013/4).
2809 +Bor Ratajc made a donation (2013/4).
2810 +Alessandro Gorreta made a donation (2013/4).
2811 +POIRETTE Marc made a donation (2013/4).
2812 +Alessandro Gorreta made a donation (2013/4).
2813 +lauri kasvandik made a donation (2013/5).
2814 +"pemasu from Finland" made a donation (2013/7).
2815 +The Parted Magic Project made a donation (2013/9 and 11).
2816 +Pavel Barta made a donation (2013/10).
2817 +Nikolay Pertsev made a donation (2014/5).
2818 +James B made a donation (2014/7, 2015/7, and 2021/12).
2819 +Stefano Di Biase made a donation (2014/8).
2820 +Daniel Epellei made a donation (2015/1).
2821 +OmegaPhil made a donation (2016/1, 2018/4).
2822 +Tomasz Szewczyk made a donation (2016/4).
2823 +James Burry made a donation (2016/12).
2824 +Carsten Rose made a donation (2018/9).
2825 +Porteus Kiosk made a donation (2018/10).
2826 +huronOS team: Enya Quetzalli made donations (2022/5, 2023/5 and 8).
2827 +Vasily Mikhaylichenko made a donation (2023/5).
2828 +
2829 +Thank you very much.
2830 +Donations are always, including future donations, very important and
2831 +helpful for me to keep on developing aufs.
2832 +
2833 +
2834 +7.
2835 +----------------------------------------
2836 +If you are an experienced user, no explanation is needed. Aufs is
2837 +just a linux filesystem.
2838 +
2839 +
2840 +Enjoy!
2841 +
2842 +# Local variables: ;
2843 +# mode: text;
2844 +# End: ;
2845 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2846 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2847 +++ linux/fs/aufs/aufs.h        2022-11-05 23:02:18.959222617 +0100
2848 @@ -0,0 +1,62 @@
2849 +/* SPDX-License-Identifier: GPL-2.0 */
2850 +/*
2851 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2852 + *
2853 + * This program is free software; you can redistribute it and/or modify
2854 + * it under the terms of the GNU General Public License as published by
2855 + * the Free Software Foundation; either version 2 of the License, or
2856 + * (at your option) any later version.
2857 + *
2858 + * This program is distributed in the hope that it will be useful,
2859 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2860 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2861 + * GNU General Public License for more details.
2862 + *
2863 + * You should have received a copy of the GNU General Public License
2864 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2865 + */
2866 +
2867 +/*
2868 + * all header files
2869 + */
2870 +
2871 +#ifndef __AUFS_H__
2872 +#define __AUFS_H__
2873 +
2874 +#ifdef __KERNEL__
2875 +
2876 +#define AuStub(type, name, body, ...) \
2877 +       static inline type name(__VA_ARGS__) { body; }
2878 +
2879 +#define AuStubVoid(name, ...) \
2880 +       AuStub(void, name, , __VA_ARGS__)
2881 +#define AuStubInt0(name, ...) \
2882 +       AuStub(int, name, return 0, __VA_ARGS__)
2883 +
2884 +#include "debug.h"
2885 +
2886 +#include "branch.h"
2887 +#include "cpup.h"
2888 +#include "dcsub.h"
2889 +#include "dbgaufs.h"
2890 +#include "dentry.h"
2891 +#include "dir.h"
2892 +#include "dirren.h"
2893 +#include "dynop.h"
2894 +#include "file.h"
2895 +#include "fstype.h"
2896 +#include "hbl.h"
2897 +#include "inode.h"
2898 +#include "lcnt.h"
2899 +#include "loop.h"
2900 +#include "module.h"
2901 +#include "opts.h"
2902 +#include "rwsem.h"
2903 +#include "super.h"
2904 +#include "sysaufs.h"
2905 +#include "vfsub.h"
2906 +#include "whout.h"
2907 +#include "wkq.h"
2908 +
2909 +#endif /* __KERNEL__ */
2910 +#endif /* __AUFS_H__ */
2911 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2912 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2913 +++ linux/fs/aufs/branch.c      2022-11-05 23:02:18.959222617 +0100
2914 @@ -0,0 +1,1427 @@
2915 +// SPDX-License-Identifier: GPL-2.0
2916 +/*
2917 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2918 + *
2919 + * This program is free software; you can redistribute it and/or modify
2920 + * it under the terms of the GNU General Public License as published by
2921 + * the Free Software Foundation; either version 2 of the License, or
2922 + * (at your option) any later version.
2923 + *
2924 + * This program is distributed in the hope that it will be useful,
2925 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2926 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2927 + * GNU General Public License for more details.
2928 + *
2929 + * You should have received a copy of the GNU General Public License
2930 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2931 + */
2932 +
2933 +/*
2934 + * branch management
2935 + */
2936 +
2937 +#include <linux/compat.h>
2938 +#include <linux/statfs.h>
2939 +#include "aufs.h"
2940 +
2941 +/*
2942 + * free a single branch
2943 + */
2944 +static void au_br_do_free(struct au_branch *br)
2945 +{
2946 +       int i;
2947 +       struct au_wbr *wbr;
2948 +       struct au_dykey **key;
2949 +
2950 +       au_hnotify_fin_br(br);
2951 +       /* always, regardless the mount option */
2952 +       au_dr_hino_free(&br->br_dirren);
2953 +       au_xino_put(br);
2954 +
2955 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2956 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2957 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2958 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2959 +
2960 +       wbr = br->br_wbr;
2961 +       if (wbr) {
2962 +               for (i = 0; i < AuBrWh_Last; i++)
2963 +                       dput(wbr->wbr_wh[i]);
2964 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2965 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2966 +       }
2967 +
2968 +       if (br->br_fhsm) {
2969 +               au_br_fhsm_fin(br->br_fhsm);
2970 +               au_kfree_try_rcu(br->br_fhsm);
2971 +       }
2972 +
2973 +       key = br->br_dykey;
2974 +       for (i = 0; i < AuBrDynOp; i++, key++)
2975 +               if (*key)
2976 +                       au_dy_put(*key);
2977 +               else
2978 +                       break;
2979 +
2980 +       /* recursive lock, s_umount of branch's */
2981 +       /* synchronize_rcu(); */ /* why? */
2982 +       lockdep_off();
2983 +       path_put(&br->br_path);
2984 +       lockdep_on();
2985 +       au_kfree_rcu(wbr);
2986 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2987 +       au_lcnt_wait_for_fin(&br->br_count);
2988 +       /* I don't know why, but percpu_refcount requires this */
2989 +       /* synchronize_rcu(); */
2990 +       au_kfree_rcu(br);
2991 +}
2992 +
2993 +/*
2994 + * frees all branches
2995 + */
2996 +void au_br_free(struct au_sbinfo *sbinfo)
2997 +{
2998 +       aufs_bindex_t bmax;
2999 +       struct au_branch **br;
3000 +
3001 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3002 +
3003 +       bmax = sbinfo->si_bbot + 1;
3004 +       br = sbinfo->si_branch;
3005 +       while (bmax--)
3006 +               au_br_do_free(*br++);
3007 +}
3008 +
3009 +/*
3010 + * find the index of a branch which is specified by @br_id.
3011 + */
3012 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3013 +{
3014 +       aufs_bindex_t bindex, bbot;
3015 +
3016 +       bbot = au_sbbot(sb);
3017 +       for (bindex = 0; bindex <= bbot; bindex++)
3018 +               if (au_sbr_id(sb, bindex) == br_id)
3019 +                       return bindex;
3020 +       return -1;
3021 +}
3022 +
3023 +/* ---------------------------------------------------------------------- */
3024 +
3025 +/*
3026 + * add a branch
3027 + */
3028 +
3029 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3030 +                       struct dentry *h_root)
3031 +{
3032 +       if (unlikely(h_adding == h_root
3033 +                    || au_test_loopback_overlap(sb, h_adding)))
3034 +               return 1;
3035 +       if (h_adding->d_sb != h_root->d_sb)
3036 +               return 0;
3037 +       return au_test_subdir(h_adding, h_root)
3038 +               || au_test_subdir(h_root, h_adding);
3039 +}
3040 +
3041 +/*
3042 + * returns a newly allocated branch. @new_nbranch is a number of branches
3043 + * after adding a branch.
3044 + */
3045 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3046 +                                    int perm)
3047 +{
3048 +       struct au_branch *add_branch;
3049 +       struct dentry *root;
3050 +       struct inode *inode;
3051 +       int err;
3052 +
3053 +       err = -ENOMEM;
3054 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3055 +       if (unlikely(!add_branch))
3056 +               goto out;
3057 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3058 +       if (unlikely(!add_branch->br_xino))
3059 +               goto out_br;
3060 +       err = au_hnotify_init_br(add_branch, perm);
3061 +       if (unlikely(err))
3062 +               goto out_xino;
3063 +
3064 +       if (au_br_writable(perm)) {
3065 +               /* may be freed separately at changing the branch permission */
3066 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3067 +                                            GFP_NOFS);
3068 +               if (unlikely(!add_branch->br_wbr))
3069 +                       goto out_hnotify;
3070 +       }
3071 +
3072 +       if (au_br_fhsm(perm)) {
3073 +               err = au_fhsm_br_alloc(add_branch);
3074 +               if (unlikely(err))
3075 +                       goto out_wbr;
3076 +       }
3077 +
3078 +       root = sb->s_root;
3079 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3080 +       if (!err)
3081 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3082 +       if (!err) {
3083 +               inode = d_inode(root);
3084 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3085 +                                       /*may_shrink*/0);
3086 +       }
3087 +       if (!err)
3088 +               return add_branch; /* success */
3089 +
3090 +out_wbr:
3091 +       au_kfree_rcu(add_branch->br_wbr);
3092 +out_hnotify:
3093 +       au_hnotify_fin_br(add_branch);
3094 +out_xino:
3095 +       au_xino_put(add_branch);
3096 +out_br:
3097 +       au_kfree_rcu(add_branch);
3098 +out:
3099 +       return ERR_PTR(err);
3100 +}
3101 +
3102 +/*
3103 + * test if the branch permission is legal or not.
3104 + */
3105 +static int test_br(struct inode *inode, int brperm, char *path)
3106 +{
3107 +       int err;
3108 +
3109 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3110 +       if (!err)
3111 +               goto out;
3112 +
3113 +       err = -EINVAL;
3114 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3115 +
3116 +out:
3117 +       return err;
3118 +}
3119 +
3120 +/*
3121 + * returns:
3122 + * 0: success, the caller will add it
3123 + * plus: success, it is already unified, the caller should ignore it
3124 + * minus: error
3125 + */
3126 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3127 +{
3128 +       int err;
3129 +       aufs_bindex_t bbot, bindex;
3130 +       struct dentry *root, *h_dentry;
3131 +       struct inode *inode, *h_inode;
3132 +
3133 +       root = sb->s_root;
3134 +       bbot = au_sbbot(sb);
3135 +       if (unlikely(bbot >= 0
3136 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3137 +               err = 1;
3138 +               if (!remount) {
3139 +                       err = -EINVAL;
3140 +                       pr_err("%s duplicated\n", add->pathname);
3141 +               }
3142 +               goto out;
3143 +       }
3144 +
3145 +       err = -ENOSPC; /* -E2BIG; */
3146 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3147 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3148 +               pr_err("number of branches exceeded %s\n", add->pathname);
3149 +               goto out;
3150 +       }
3151 +
3152 +       err = -EDOM;
3153 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3154 +               pr_err("bad index %d\n", add->bindex);
3155 +               goto out;
3156 +       }
3157 +
3158 +       inode = d_inode(add->path.dentry);
3159 +       err = -ENOENT;
3160 +       if (unlikely(!inode->i_nlink)) {
3161 +               pr_err("no existence %s\n", add->pathname);
3162 +               goto out;
3163 +       }
3164 +
3165 +       err = -EINVAL;
3166 +       if (unlikely(inode->i_sb == sb)) {
3167 +               pr_err("%s must be outside\n", add->pathname);
3168 +               goto out;
3169 +       }
3170 +
3171 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3172 +               pr_err("unsupported filesystem, %s (%s)\n",
3173 +                      add->pathname, au_sbtype(inode->i_sb));
3174 +               goto out;
3175 +       }
3176 +
3177 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3178 +               pr_err("already stacked, %s (%s)\n",
3179 +                      add->pathname, au_sbtype(inode->i_sb));
3180 +               goto out;
3181 +       }
3182 +
3183 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3184 +       if (unlikely(err))
3185 +               goto out;
3186 +
3187 +       if (bbot < 0)
3188 +               return 0; /* success */
3189 +
3190 +       err = -EINVAL;
3191 +       for (bindex = 0; bindex <= bbot; bindex++)
3192 +               if (unlikely(test_overlap(sb, add->path.dentry,
3193 +                                         au_h_dptr(root, bindex)))) {
3194 +                       pr_err("%s is overlapped\n", add->pathname);
3195 +                       goto out;
3196 +               }
3197 +
3198 +       err = 0;
3199 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3200 +               h_dentry = au_h_dptr(root, 0);
3201 +               h_inode = d_inode(h_dentry);
3202 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3203 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3204 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3205 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3206 +                               add->pathname,
3207 +                               i_uid_read(inode), i_gid_read(inode),
3208 +                               (inode->i_mode & S_IALLUGO),
3209 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3210 +                               (h_inode->i_mode & S_IALLUGO));
3211 +       }
3212 +
3213 +out:
3214 +       return err;
3215 +}
3216 +
3217 +/*
3218 + * initialize or clean the whiteouts for an adding branch
3219 + */
3220 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3221 +                        int new_perm)
3222 +{
3223 +       int err, old_perm;
3224 +       aufs_bindex_t bindex;
3225 +       struct inode *h_inode;
3226 +       struct au_wbr *wbr;
3227 +       struct au_hinode *hdir;
3228 +       struct dentry *h_dentry;
3229 +
3230 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3231 +       if (unlikely(err))
3232 +               goto out;
3233 +
3234 +       wbr = br->br_wbr;
3235 +       old_perm = br->br_perm;
3236 +       br->br_perm = new_perm;
3237 +       hdir = NULL;
3238 +       h_inode = NULL;
3239 +       bindex = au_br_index(sb, br->br_id);
3240 +       if (0 <= bindex) {
3241 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3242 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3243 +       } else {
3244 +               h_dentry = au_br_dentry(br);
3245 +               h_inode = d_inode(h_dentry);
3246 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3247 +       }
3248 +       if (!wbr)
3249 +               err = au_wh_init(br, sb);
3250 +       else {
3251 +               wbr_wh_write_lock(wbr);
3252 +               err = au_wh_init(br, sb);
3253 +               wbr_wh_write_unlock(wbr);
3254 +       }
3255 +       if (hdir)
3256 +               au_hn_inode_unlock(hdir);
3257 +       else
3258 +               inode_unlock(h_inode);
3259 +       vfsub_mnt_drop_write(au_br_mnt(br));
3260 +       br->br_perm = old_perm;
3261 +
3262 +       if (!err && wbr && !au_br_writable(new_perm)) {
3263 +               au_kfree_rcu(wbr);
3264 +               br->br_wbr = NULL;
3265 +       }
3266 +
3267 +out:
3268 +       return err;
3269 +}
3270 +
3271 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3272 +                      int perm)
3273 +{
3274 +       int err;
3275 +       struct kstatfs kst;
3276 +       struct au_wbr *wbr;
3277 +
3278 +       wbr = br->br_wbr;
3279 +       au_rw_init(&wbr->wbr_wh_rwsem);
3280 +       atomic_set(&wbr->wbr_wh_running, 0);
3281 +
3282 +       /*
3283 +        * a limit for rmdir/rename a dir
3284 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3285 +        */
3286 +       err = vfs_statfs(&br->br_path, &kst);
3287 +       if (unlikely(err))
3288 +               goto out;
3289 +       err = -EINVAL;
3290 +       if (kst.f_namelen >= NAME_MAX)
3291 +               err = au_br_init_wh(sb, br, perm);
3292 +       else
3293 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3294 +                      au_br_dentry(br),
3295 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3296 +
3297 +out:
3298 +       return err;
3299 +}
3300 +
3301 +/* initialize a new branch */
3302 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3303 +                     struct au_opt_add *add)
3304 +{
3305 +       int err;
3306 +       struct au_branch *brbase;
3307 +       struct file *xf;
3308 +       struct inode *h_inode;
3309 +
3310 +       err = 0;
3311 +       br->br_perm = add->perm;
3312 +       br->br_path = add->path; /* set first, path_get() later */
3313 +       spin_lock_init(&br->br_dykey_lock);
3314 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3315 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3316 +       br->br_id = au_new_br_id(sb);
3317 +       AuDebugOn(br->br_id < 0);
3318 +
3319 +       /* always, regardless the given option */
3320 +       err = au_dr_br_init(sb, br, &add->path);
3321 +       if (unlikely(err))
3322 +               goto out_err;
3323 +
3324 +       if (au_br_writable(add->perm)) {
3325 +               err = au_wbr_init(br, sb, add->perm);
3326 +               if (unlikely(err))
3327 +                       goto out_err;
3328 +       }
3329 +
3330 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3331 +               brbase = au_sbr(sb, 0);
3332 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3333 +               AuDebugOn(!xf);
3334 +               h_inode = d_inode(add->path.dentry);
3335 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3336 +               if (unlikely(err)) {
3337 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3338 +                       goto out_err;
3339 +               }
3340 +       }
3341 +
3342 +       sysaufs_br_init(br);
3343 +       path_get(&br->br_path);
3344 +       goto out; /* success */
3345 +
3346 +out_err:
3347 +       memset(&br->br_path, 0, sizeof(br->br_path));
3348 +out:
3349 +       return err;
3350 +}
3351 +
3352 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3353 +                            struct au_branch *br, aufs_bindex_t bbot,
3354 +                            aufs_bindex_t amount)
3355 +{
3356 +       struct au_branch **brp;
3357 +
3358 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3359 +
3360 +       brp = sbinfo->si_branch + bindex;
3361 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3362 +       *brp = br;
3363 +       sbinfo->si_bbot++;
3364 +       if (unlikely(bbot < 0))
3365 +               sbinfo->si_bbot = 0;
3366 +}
3367 +
3368 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3369 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3370 +{
3371 +       struct au_hdentry *hdp;
3372 +
3373 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3374 +
3375 +       hdp = au_hdentry(dinfo, bindex);
3376 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3377 +       au_h_dentry_init(hdp);
3378 +       dinfo->di_bbot++;
3379 +       if (unlikely(bbot < 0))
3380 +               dinfo->di_btop = 0;
3381 +}
3382 +
3383 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3384 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3385 +{
3386 +       struct au_hinode *hip;
3387 +
3388 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3389 +
3390 +       hip = au_hinode(iinfo, bindex);
3391 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3392 +       au_hinode_init(hip);
3393 +       iinfo->ii_bbot++;
3394 +       if (unlikely(bbot < 0))
3395 +               iinfo->ii_btop = 0;
3396 +}
3397 +
3398 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3399 +                        aufs_bindex_t bindex)
3400 +{
3401 +       struct dentry *root, *h_dentry;
3402 +       struct inode *root_inode, *h_inode;
3403 +       aufs_bindex_t bbot, amount;
3404 +
3405 +       root = sb->s_root;
3406 +       root_inode = d_inode(root);
3407 +       bbot = au_sbbot(sb);
3408 +       amount = bbot + 1 - bindex;
3409 +       h_dentry = au_br_dentry(br);
3410 +       au_sbilist_lock();
3411 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3412 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3413 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3414 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3415 +       h_inode = d_inode(h_dentry);
3416 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3417 +       au_sbilist_unlock();
3418 +}
3419 +
3420 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3421 +{
3422 +       int err;
3423 +       aufs_bindex_t bbot, add_bindex;
3424 +       struct dentry *root, *h_dentry;
3425 +       struct inode *root_inode;
3426 +       struct au_branch *add_branch;
3427 +
3428 +       root = sb->s_root;
3429 +       root_inode = d_inode(root);
3430 +       IMustLock(root_inode);
3431 +       IiMustWriteLock(root_inode);
3432 +       err = test_add(sb, add, remount);
3433 +       if (unlikely(err < 0))
3434 +               goto out;
3435 +       if (err) {
3436 +               err = 0;
3437 +               goto out; /* success */
3438 +       }
3439 +
3440 +       bbot = au_sbbot(sb);
3441 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3442 +       err = PTR_ERR(add_branch);
3443 +       if (IS_ERR(add_branch))
3444 +               goto out;
3445 +
3446 +       err = au_br_init(add_branch, sb, add);
3447 +       if (unlikely(err)) {
3448 +               au_br_do_free(add_branch);
3449 +               goto out;
3450 +       }
3451 +
3452 +       add_bindex = add->bindex;
3453 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3454 +       au_br_do_add(sb, add_branch, add_bindex);
3455 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3456 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3457 +
3458 +       h_dentry = add->path.dentry;
3459 +       if (!add_bindex) {
3460 +               au_cpup_attr_all(root_inode, /*force*/1);
3461 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3462 +       } else
3463 +               au_add_nlink(root_inode, d_inode(h_dentry));
3464 +
3465 +out:
3466 +       return err;
3467 +}
3468 +
3469 +/* ---------------------------------------------------------------------- */
3470 +
3471 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3472 +                                      unsigned long long max __maybe_unused,
3473 +                                      void *arg)
3474 +{
3475 +       unsigned long long n;
3476 +       struct file **p, *f;
3477 +       struct hlist_bl_head *files;
3478 +       struct hlist_bl_node *pos;
3479 +       struct au_finfo *finfo;
3480 +
3481 +       n = 0;
3482 +       p = a;
3483 +       files = &au_sbi(sb)->si_files;
3484 +       hlist_bl_lock(files);
3485 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3486 +               f = finfo->fi_file;
3487 +               if (file_count(f)
3488 +                   && !special_file(file_inode(f)->i_mode)) {
3489 +                       get_file(f);
3490 +                       *p++ = f;
3491 +                       n++;
3492 +                       AuDebugOn(n > max);
3493 +               }
3494 +       }
3495 +       hlist_bl_unlock(files);
3496 +
3497 +       return n;
3498 +}
3499 +
3500 +static struct file **au_farray_alloc(struct super_block *sb,
3501 +                                    unsigned long long *max)
3502 +{
3503 +       struct au_sbinfo *sbi;
3504 +
3505 +       sbi = au_sbi(sb);
3506 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3507 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3508 +}
3509 +
3510 +static void au_farray_free(struct file **a, unsigned long long max)
3511 +{
3512 +       unsigned long long ull;
3513 +
3514 +       for (ull = 0; ull < max; ull++)
3515 +               if (a[ull])
3516 +                       fput(a[ull]);
3517 +       kvfree(a);
3518 +}
3519 +
3520 +/* ---------------------------------------------------------------------- */
3521 +
3522 +/*
3523 + * delete a branch
3524 + */
3525 +
3526 +/* to show the line number, do not make it inlined function */
3527 +#define AuVerbose(do_info, fmt, ...) do { \
3528 +       if (do_info) \
3529 +               pr_info(fmt, ##__VA_ARGS__); \
3530 +} while (0)
3531 +
3532 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3533 +                        aufs_bindex_t bbot)
3534 +{
3535 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3536 +}
3537 +
3538 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3539 +                        aufs_bindex_t bbot)
3540 +{
3541 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3542 +}
3543 +
3544 +/*
3545 + * test if the branch is deletable or not.
3546 + */
3547 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3548 +                           unsigned int sigen, const unsigned int verbose)
3549 +{
3550 +       int err, i, j, ndentry;
3551 +       aufs_bindex_t btop, bbot;
3552 +       struct au_dcsub_pages dpages;
3553 +       struct au_dpage *dpage;
3554 +       struct dentry *d;
3555 +
3556 +       err = au_dpages_init(&dpages, GFP_NOFS);
3557 +       if (unlikely(err))
3558 +               goto out;
3559 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3560 +       if (unlikely(err))
3561 +               goto out_dpages;
3562 +
3563 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3564 +               dpage = dpages.dpages + i;
3565 +               ndentry = dpage->ndentry;
3566 +               for (j = 0; !err && j < ndentry; j++) {
3567 +                       d = dpage->dentries[j];
3568 +                       AuDebugOn(au_dcount(d) <= 0);
3569 +                       if (!au_digen_test(d, sigen)) {
3570 +                               di_read_lock_child(d, AuLock_IR);
3571 +                               if (unlikely(au_dbrange_test(d))) {
3572 +                                       di_read_unlock(d, AuLock_IR);
3573 +                                       continue;
3574 +                               }
3575 +                       } else {
3576 +                               di_write_lock_child(d);
3577 +                               if (unlikely(au_dbrange_test(d))) {
3578 +                                       di_write_unlock(d);
3579 +                                       continue;
3580 +                               }
3581 +                               err = au_reval_dpath(d, sigen);
3582 +                               if (!err)
3583 +                                       di_downgrade_lock(d, AuLock_IR);
3584 +                               else {
3585 +                                       di_write_unlock(d);
3586 +                                       break;
3587 +                               }
3588 +                       }
3589 +
3590 +                       /* AuDbgDentry(d); */
3591 +                       btop = au_dbtop(d);
3592 +                       bbot = au_dbbot(d);
3593 +                       if (btop <= bindex
3594 +                           && bindex <= bbot
3595 +                           && au_h_dptr(d, bindex)
3596 +                           && au_test_dbusy(d, btop, bbot)) {
3597 +                               err = -EBUSY;
3598 +                               AuVerbose(verbose, "busy %pd\n", d);
3599 +                               AuDbgDentry(d);
3600 +                       }
3601 +                       di_read_unlock(d, AuLock_IR);
3602 +               }
3603 +       }
3604 +
3605 +out_dpages:
3606 +       au_dpages_free(&dpages);
3607 +out:
3608 +       return err;
3609 +}
3610 +
3611 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3612 +                          unsigned int sigen, const unsigned int verbose)
3613 +{
3614 +       int err;
3615 +       unsigned long long max, ull;
3616 +       struct inode *i, **array;
3617 +       aufs_bindex_t btop, bbot;
3618 +
3619 +       array = au_iarray_alloc(sb, &max);
3620 +       err = PTR_ERR(array);
3621 +       if (IS_ERR(array))
3622 +               goto out;
3623 +
3624 +       err = 0;
3625 +       AuDbg("b%d\n", bindex);
3626 +       for (ull = 0; !err && ull < max; ull++) {
3627 +               i = array[ull];
3628 +               if (unlikely(!i))
3629 +                       break;
3630 +               if (i->i_ino == AUFS_ROOT_INO)
3631 +                       continue;
3632 +
3633 +               /* AuDbgInode(i); */
3634 +               if (au_iigen(i, NULL) == sigen)
3635 +                       ii_read_lock_child(i);
3636 +               else {
3637 +                       ii_write_lock_child(i);
3638 +                       err = au_refresh_hinode_self(i);
3639 +                       au_iigen_dec(i);
3640 +                       if (!err)
3641 +                               ii_downgrade_lock(i);
3642 +                       else {
3643 +                               ii_write_unlock(i);
3644 +                               break;
3645 +                       }
3646 +               }
3647 +
3648 +               btop = au_ibtop(i);
3649 +               bbot = au_ibbot(i);
3650 +               if (btop <= bindex
3651 +                   && bindex <= bbot
3652 +                   && au_h_iptr(i, bindex)
3653 +                   && au_test_ibusy(i, btop, bbot)) {
3654 +                       err = -EBUSY;
3655 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3656 +                       AuDbgInode(i);
3657 +               }
3658 +               ii_read_unlock(i);
3659 +       }
3660 +       au_iarray_free(array, max);
3661 +
3662 +out:
3663 +       return err;
3664 +}
3665 +
3666 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3667 +                             const unsigned int verbose)
3668 +{
3669 +       int err;
3670 +       unsigned int sigen;
3671 +
3672 +       sigen = au_sigen(root->d_sb);
3673 +       DiMustNoWaiters(root);
3674 +       IiMustNoWaiters(d_inode(root));
3675 +       di_write_unlock(root);
3676 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3677 +       if (!err)
3678 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3679 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3680 +
3681 +       return err;
3682 +}
3683 +
3684 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3685 +                        struct file **to_free, int *idx)
3686 +{
3687 +       int err;
3688 +       unsigned char matched, root;
3689 +       aufs_bindex_t bindex, bbot;
3690 +       struct au_fidir *fidir;
3691 +       struct au_hfile *hfile;
3692 +
3693 +       err = 0;
3694 +       root = IS_ROOT(file->f_path.dentry);
3695 +       if (root) {
3696 +               get_file(file);
3697 +               to_free[*idx] = file;
3698 +               (*idx)++;
3699 +               goto out;
3700 +       }
3701 +
3702 +       matched = 0;
3703 +       fidir = au_fi(file)->fi_hdir;
3704 +       AuDebugOn(!fidir);
3705 +       bbot = au_fbbot_dir(file);
3706 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3707 +               hfile = fidir->fd_hfile + bindex;
3708 +               if (!hfile->hf_file)
3709 +                       continue;
3710 +
3711 +               if (hfile->hf_br->br_id == br_id) {
3712 +                       matched = 1;
3713 +                       break;
3714 +               }
3715 +       }
3716 +       if (matched)
3717 +               err = -EBUSY;
3718 +
3719 +out:
3720 +       return err;
3721 +}
3722 +
3723 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3724 +                         struct file **to_free, int opened)
3725 +{
3726 +       int err, idx;
3727 +       unsigned long long ull, max;
3728 +       aufs_bindex_t btop;
3729 +       struct file *file, **array;
3730 +       struct dentry *root;
3731 +       struct au_hfile *hfile;
3732 +
3733 +       array = au_farray_alloc(sb, &max);
3734 +       err = PTR_ERR(array);
3735 +       if (IS_ERR(array))
3736 +               goto out;
3737 +
3738 +       err = 0;
3739 +       idx = 0;
3740 +       root = sb->s_root;
3741 +       di_write_unlock(root);
3742 +       for (ull = 0; ull < max; ull++) {
3743 +               file = array[ull];
3744 +               if (unlikely(!file))
3745 +                       break;
3746 +
3747 +               /* AuDbg("%pD\n", file); */
3748 +               fi_read_lock(file);
3749 +               btop = au_fbtop(file);
3750 +               if (!d_is_dir(file->f_path.dentry)) {
3751 +                       hfile = &au_fi(file)->fi_htop;
3752 +                       if (hfile->hf_br->br_id == br_id)
3753 +                               err = -EBUSY;
3754 +               } else
3755 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3756 +               fi_read_unlock(file);
3757 +               if (unlikely(err))
3758 +                       break;
3759 +       }
3760 +       di_write_lock_child(root);
3761 +       au_farray_free(array, max);
3762 +       AuDebugOn(idx > opened);
3763 +
3764 +out:
3765 +       return err;
3766 +}
3767 +
3768 +static void br_del_file(struct file **to_free, unsigned long long opened,
3769 +                       aufs_bindex_t br_id)
3770 +{
3771 +       unsigned long long ull;
3772 +       aufs_bindex_t bindex, btop, bbot, bfound;
3773 +       struct file *file;
3774 +       struct au_fidir *fidir;
3775 +       struct au_hfile *hfile;
3776 +
3777 +       for (ull = 0; ull < opened; ull++) {
3778 +               file = to_free[ull];
3779 +               if (unlikely(!file))
3780 +                       break;
3781 +
3782 +               /* AuDbg("%pD\n", file); */
3783 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3784 +               bfound = -1;
3785 +               fidir = au_fi(file)->fi_hdir;
3786 +               AuDebugOn(!fidir);
3787 +               fi_write_lock(file);
3788 +               btop = au_fbtop(file);
3789 +               bbot = au_fbbot_dir(file);
3790 +               for (bindex = btop; bindex <= bbot; bindex++) {
3791 +                       hfile = fidir->fd_hfile + bindex;
3792 +                       if (!hfile->hf_file)
3793 +                               continue;
3794 +
3795 +                       if (hfile->hf_br->br_id == br_id) {
3796 +                               bfound = bindex;
3797 +                               break;
3798 +                       }
3799 +               }
3800 +               AuDebugOn(bfound < 0);
3801 +               au_set_h_fptr(file, bfound, NULL);
3802 +               if (bfound == btop) {
3803 +                       for (btop++; btop <= bbot; btop++)
3804 +                               if (au_hf_dir(file, btop)) {
3805 +                                       au_set_fbtop(file, btop);
3806 +                                       break;
3807 +                               }
3808 +               }
3809 +               fi_write_unlock(file);
3810 +       }
3811 +}
3812 +
3813 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3814 +                            const aufs_bindex_t bindex,
3815 +                            const aufs_bindex_t bbot)
3816 +{
3817 +       struct au_branch **brp, **p;
3818 +
3819 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3820 +
3821 +       brp = sbinfo->si_branch + bindex;
3822 +       if (bindex < bbot)
3823 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3824 +       sbinfo->si_branch[0 + bbot] = NULL;
3825 +       sbinfo->si_bbot--;
3826 +
3827 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3828 +                       /*may_shrink*/1);
3829 +       if (p)
3830 +               sbinfo->si_branch = p;
3831 +       /* harmless error */
3832 +}
3833 +
3834 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3835 +                            const aufs_bindex_t bbot)
3836 +{
3837 +       struct au_hdentry *hdp, *p;
3838 +
3839 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3840 +
3841 +       hdp = au_hdentry(dinfo, bindex);
3842 +       if (bindex < bbot)
3843 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3844 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3845 +       dinfo->di_bbot--;
3846 +
3847 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3848 +                       /*may_shrink*/1);
3849 +       if (p)
3850 +               dinfo->di_hdentry = p;
3851 +       /* harmless error */
3852 +}
3853 +
3854 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3855 +                            const aufs_bindex_t bbot)
3856 +{
3857 +       struct au_hinode *hip, *p;
3858 +
3859 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3860 +
3861 +       hip = au_hinode(iinfo, bindex);
3862 +       if (bindex < bbot)
3863 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3864 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3865 +       iinfo->ii_bbot--;
3866 +
3867 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3868 +                       /*may_shrink*/1);
3869 +       if (p)
3870 +               iinfo->ii_hinode = p;
3871 +       /* harmless error */
3872 +}
3873 +
3874 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3875 +                        struct au_branch *br)
3876 +{
3877 +       aufs_bindex_t bbot;
3878 +       struct au_sbinfo *sbinfo;
3879 +       struct dentry *root, *h_root;
3880 +       struct inode *inode, *h_inode;
3881 +       struct au_hinode *hinode;
3882 +
3883 +       SiMustWriteLock(sb);
3884 +
3885 +       root = sb->s_root;
3886 +       inode = d_inode(root);
3887 +       sbinfo = au_sbi(sb);
3888 +       bbot = sbinfo->si_bbot;
3889 +
3890 +       h_root = au_h_dptr(root, bindex);
3891 +       hinode = au_hi(inode, bindex);
3892 +       h_inode = au_igrab(hinode->hi_inode);
3893 +       au_hiput(hinode);
3894 +
3895 +       au_sbilist_lock();
3896 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3897 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3898 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3899 +       au_sbilist_unlock();
3900 +
3901 +       /* ignore an error */
3902 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3903 +
3904 +       dput(h_root);
3905 +       iput(h_inode);
3906 +       au_br_do_free(br);
3907 +}
3908 +
3909 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3910 +                                  unsigned long long max, void *arg)
3911 +{
3912 +       return max;
3913 +}
3914 +
3915 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3916 +{
3917 +       int err, rerr, i;
3918 +       unsigned long long opened;
3919 +       unsigned int mnt_flags;
3920 +       aufs_bindex_t bindex, bbot, br_id;
3921 +       unsigned char do_wh, verbose;
3922 +       struct au_branch *br;
3923 +       struct au_wbr *wbr;
3924 +       struct dentry *root;
3925 +       struct file **to_free;
3926 +
3927 +       err = 0;
3928 +       opened = 0;
3929 +       to_free = NULL;
3930 +       root = sb->s_root;
3931 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3932 +       if (bindex < 0) {
3933 +               if (remount)
3934 +                       goto out; /* success */
3935 +               err = -ENOENT;
3936 +               pr_err("%s no such branch\n", del->pathname);
3937 +               goto out;
3938 +       }
3939 +       AuDbg("bindex b%d\n", bindex);
3940 +
3941 +       err = -EBUSY;
3942 +       mnt_flags = au_mntflags(sb);
3943 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3944 +       bbot = au_sbbot(sb);
3945 +       if (unlikely(!bbot)) {
3946 +               AuVerbose(verbose, "no more branches left\n");
3947 +               goto out;
3948 +       }
3949 +
3950 +       br = au_sbr(sb, bindex);
3951 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3952 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3953 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3954 +               goto out;
3955 +       }
3956 +
3957 +       br_id = br->br_id;
3958 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3959 +       if (unlikely(opened)) {
3960 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3961 +               err = PTR_ERR(to_free);
3962 +               if (IS_ERR(to_free))
3963 +                       goto out;
3964 +
3965 +               err = test_file_busy(sb, br_id, to_free, opened);
3966 +               if (unlikely(err)) {
3967 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3968 +                       goto out;
3969 +               }
3970 +       }
3971 +
3972 +       wbr = br->br_wbr;
3973 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3974 +       if (do_wh) {
3975 +               /* instead of WbrWhMustWriteLock(wbr) */
3976 +               SiMustWriteLock(sb);
3977 +               for (i = 0; i < AuBrWh_Last; i++) {
3978 +                       dput(wbr->wbr_wh[i]);
3979 +                       wbr->wbr_wh[i] = NULL;
3980 +               }
3981 +       }
3982 +
3983 +       err = test_children_busy(root, bindex, verbose);
3984 +       if (unlikely(err)) {
3985 +               if (do_wh)
3986 +                       goto out_wh;
3987 +               goto out;
3988 +       }
3989 +
3990 +       err = 0;
3991 +       if (to_free) {
3992 +               /*
3993 +                * now we confirmed the branch is deletable.
3994 +                * let's free the remaining opened dirs on the branch.
3995 +                */
3996 +               di_write_unlock(root);
3997 +               br_del_file(to_free, opened, br_id);
3998 +               di_write_lock_child(root);
3999 +       }
4000 +
4001 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
4002 +       dbgaufs_xino_del(br);           /* remove one */
4003 +       au_br_do_del(sb, bindex, br);
4004 +       sysaufs_brs_add(sb, bindex);    /* append successors */
4005 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4006 +
4007 +       if (!bindex) {
4008 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4009 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4010 +       } else
4011 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4012 +       if (au_opt_test(mnt_flags, PLINK))
4013 +               au_plink_half_refresh(sb, br_id);
4014 +
4015 +       goto out; /* success */
4016 +
4017 +out_wh:
4018 +       /* revert */
4019 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4020 +       if (rerr)
4021 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4022 +                       del->pathname, rerr);
4023 +out:
4024 +       if (to_free)
4025 +               au_farray_free(to_free, opened);
4026 +       return err;
4027 +}
4028 +
4029 +/* ---------------------------------------------------------------------- */
4030 +
4031 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4032 +{
4033 +       int err;
4034 +       aufs_bindex_t btop, bbot;
4035 +       struct aufs_ibusy ibusy;
4036 +       struct inode *inode, *h_inode;
4037 +
4038 +       err = -EPERM;
4039 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4040 +               goto out;
4041 +
4042 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4043 +       if (!err)
4044 +               /* VERIFY_WRITE */
4045 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4046 +       if (unlikely(err)) {
4047 +               err = -EFAULT;
4048 +               AuTraceErr(err);
4049 +               goto out;
4050 +       }
4051 +
4052 +       err = -EINVAL;
4053 +       si_read_lock(sb, AuLock_FLUSH);
4054 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4055 +               goto out_unlock;
4056 +
4057 +       err = 0;
4058 +       ibusy.h_ino = 0; /* invalid */
4059 +       inode = ilookup(sb, ibusy.ino);
4060 +       if (!inode
4061 +           || inode->i_ino == AUFS_ROOT_INO
4062 +           || au_is_bad_inode(inode))
4063 +               goto out_unlock;
4064 +
4065 +       ii_read_lock_child(inode);
4066 +       btop = au_ibtop(inode);
4067 +       bbot = au_ibbot(inode);
4068 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4069 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4070 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4071 +                       ibusy.h_ino = h_inode->i_ino;
4072 +       }
4073 +       ii_read_unlock(inode);
4074 +       iput(inode);
4075 +
4076 +out_unlock:
4077 +       si_read_unlock(sb);
4078 +       if (!err) {
4079 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4080 +               if (unlikely(err)) {
4081 +                       err = -EFAULT;
4082 +                       AuTraceErr(err);
4083 +               }
4084 +       }
4085 +out:
4086 +       return err;
4087 +}
4088 +
4089 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4090 +{
4091 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4092 +}
4093 +
4094 +#ifdef CONFIG_COMPAT
4095 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4096 +{
4097 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4098 +}
4099 +#endif
4100 +
4101 +/* ---------------------------------------------------------------------- */
4102 +
4103 +/*
4104 + * change a branch permission
4105 + */
4106 +
4107 +static void au_warn_ima(void)
4108 +{
4109 +#ifdef CONFIG_IMA
4110 +       /* since it doesn't support mark_files_ro() */
4111 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4112 +#endif
4113 +}
4114 +
4115 +static int do_need_sigen_inc(int a, int b)
4116 +{
4117 +       return au_br_whable(a) && !au_br_whable(b);
4118 +}
4119 +
4120 +static int need_sigen_inc(int old, int new)
4121 +{
4122 +       return do_need_sigen_inc(old, new)
4123 +               || do_need_sigen_inc(new, old);
4124 +}
4125 +
4126 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4127 +{
4128 +       int err, do_warn;
4129 +       unsigned int mnt_flags;
4130 +       unsigned long long ull, max;
4131 +       aufs_bindex_t br_id;
4132 +       unsigned char verbose, writer;
4133 +       struct file *file, *hf, **array;
4134 +       struct au_hfile *hfile;
4135 +       struct inode *h_inode;
4136 +
4137 +       mnt_flags = au_mntflags(sb);
4138 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4139 +
4140 +       array = au_farray_alloc(sb, &max);
4141 +       err = PTR_ERR(array);
4142 +       if (IS_ERR(array))
4143 +               goto out;
4144 +
4145 +       do_warn = 0;
4146 +       br_id = au_sbr_id(sb, bindex);
4147 +       for (ull = 0; ull < max; ull++) {
4148 +               file = array[ull];
4149 +               if (unlikely(!file))
4150 +                       break;
4151 +
4152 +               /* AuDbg("%pD\n", file); */
4153 +               fi_read_lock(file);
4154 +               if (unlikely(au_test_mmapped(file))) {
4155 +                       err = -EBUSY;
4156 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4157 +                       AuDbgFile(file);
4158 +                       FiMustNoWaiters(file);
4159 +                       fi_read_unlock(file);
4160 +                       goto out_array;
4161 +               }
4162 +
4163 +               hfile = &au_fi(file)->fi_htop;
4164 +               hf = hfile->hf_file;
4165 +               if (!d_is_reg(file->f_path.dentry)
4166 +                   || !(file->f_mode & FMODE_WRITE)
4167 +                   || hfile->hf_br->br_id != br_id
4168 +                   || !(hf->f_mode & FMODE_WRITE))
4169 +                       array[ull] = NULL;
4170 +               else {
4171 +                       do_warn = 1;
4172 +                       get_file(file);
4173 +               }
4174 +
4175 +               FiMustNoWaiters(file);
4176 +               fi_read_unlock(file);
4177 +               fput(file);
4178 +       }
4179 +
4180 +       err = 0;
4181 +       if (do_warn)
4182 +               au_warn_ima();
4183 +
4184 +       for (ull = 0; ull < max; ull++) {
4185 +               file = array[ull];
4186 +               if (!file)
4187 +                       continue;
4188 +
4189 +               /* todo: already flushed? */
4190 +               /*
4191 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4192 +                * approach which resets f_mode and calls mnt_drop_write() and
4193 +                * file_release_write() for each file, because the branch
4194 +                * attribute in aufs world is totally different from the native
4195 +                * fs rw/ro mode.
4196 +                */
4197 +               /* fi_read_lock(file); */
4198 +               hfile = &au_fi(file)->fi_htop;
4199 +               hf = hfile->hf_file;
4200 +               /* fi_read_unlock(file); */
4201 +               spin_lock(&hf->f_lock);
4202 +               writer = !!(hf->f_mode & FMODE_WRITER);
4203 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4204 +               spin_unlock(&hf->f_lock);
4205 +               if (writer) {
4206 +                       h_inode = file_inode(hf);
4207 +                       if (hf->f_mode & FMODE_READ)
4208 +                               i_readcount_inc(h_inode);
4209 +                       put_write_access(h_inode);
4210 +                       __mnt_drop_write(hf->f_path.mnt);
4211 +               }
4212 +       }
4213 +
4214 +out_array:
4215 +       au_farray_free(array, max);
4216 +out:
4217 +       AuTraceErr(err);
4218 +       return err;
4219 +}
4220 +
4221 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4222 +             int *do_refresh)
4223 +{
4224 +       int err, rerr;
4225 +       aufs_bindex_t bindex;
4226 +       struct dentry *root;
4227 +       struct au_branch *br;
4228 +       struct au_br_fhsm *bf;
4229 +
4230 +       root = sb->s_root;
4231 +       bindex = au_find_dbindex(root, mod->h_root);
4232 +       if (bindex < 0) {
4233 +               if (remount)
4234 +                       return 0; /* success */
4235 +               err = -ENOENT;
4236 +               pr_err("%s no such branch\n", mod->path);
4237 +               goto out;
4238 +       }
4239 +       AuDbg("bindex b%d\n", bindex);
4240 +
4241 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4242 +       if (unlikely(err))
4243 +               goto out;
4244 +
4245 +       br = au_sbr(sb, bindex);
4246 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4247 +       if (br->br_perm == mod->perm)
4248 +               return 0; /* success */
4249 +
4250 +       /* pre-allocate for non-fhsm --> fhsm */
4251 +       bf = NULL;
4252 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4253 +               err = au_fhsm_br_alloc(br);
4254 +               if (unlikely(err))
4255 +                       goto out;
4256 +               bf = br->br_fhsm;
4257 +               br->br_fhsm = NULL;
4258 +       }
4259 +
4260 +       if (au_br_writable(br->br_perm)) {
4261 +               /* remove whiteout base */
4262 +               err = au_br_init_wh(sb, br, mod->perm);
4263 +               if (unlikely(err))
4264 +                       goto out_bf;
4265 +
4266 +               if (!au_br_writable(mod->perm)) {
4267 +                       /* rw --> ro, file might be mmapped */
4268 +                       DiMustNoWaiters(root);
4269 +                       IiMustNoWaiters(d_inode(root));
4270 +                       di_write_unlock(root);
4271 +                       err = au_br_mod_files_ro(sb, bindex);
4272 +                       /* aufs_write_lock() calls ..._child() */
4273 +                       di_write_lock_child(root);
4274 +
4275 +                       if (unlikely(err)) {
4276 +                               rerr = -ENOMEM;
4277 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4278 +                                                    GFP_NOFS);
4279 +                               if (br->br_wbr)
4280 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4281 +                               if (unlikely(rerr)) {
4282 +                                       AuIOErr("nested error %d (%d)\n",
4283 +                                               rerr, err);
4284 +                                       br->br_perm = mod->perm;
4285 +                               }
4286 +                       }
4287 +               }
4288 +       } else if (au_br_writable(mod->perm)) {
4289 +               /* ro --> rw */
4290 +               err = -ENOMEM;
4291 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4292 +               if (br->br_wbr) {
4293 +                       err = au_wbr_init(br, sb, mod->perm);
4294 +                       if (unlikely(err)) {
4295 +                               au_kfree_rcu(br->br_wbr);
4296 +                               br->br_wbr = NULL;
4297 +                       }
4298 +               }
4299 +       }
4300 +       if (unlikely(err))
4301 +               goto out_bf;
4302 +
4303 +       if (au_br_fhsm(br->br_perm)) {
4304 +               if (!au_br_fhsm(mod->perm)) {
4305 +                       /* fhsm --> non-fhsm */
4306 +                       au_br_fhsm_fin(br->br_fhsm);
4307 +                       au_kfree_rcu(br->br_fhsm);
4308 +                       br->br_fhsm = NULL;
4309 +               }
4310 +       } else if (au_br_fhsm(mod->perm))
4311 +               /* non-fhsm --> fhsm */
4312 +               br->br_fhsm = bf;
4313 +
4314 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4315 +       br->br_perm = mod->perm;
4316 +       goto out; /* success */
4317 +
4318 +out_bf:
4319 +       au_kfree_try_rcu(bf);
4320 +out:
4321 +       AuTraceErr(err);
4322 +       return err;
4323 +}
4324 +
4325 +/* ---------------------------------------------------------------------- */
4326 +
4327 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4328 +{
4329 +       int err;
4330 +       struct kstatfs kstfs;
4331 +
4332 +       err = vfs_statfs(&br->br_path, &kstfs);
4333 +       if (!err) {
4334 +               stfs->f_blocks = kstfs.f_blocks;
4335 +               stfs->f_bavail = kstfs.f_bavail;
4336 +               stfs->f_files = kstfs.f_files;
4337 +               stfs->f_ffree = kstfs.f_ffree;
4338 +       }
4339 +
4340 +       return err;
4341 +}
4342 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4343 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4344 +++ linux/fs/aufs/branch.h      2023-08-28 12:34:39.956636132 +0200
4345 @@ -0,0 +1,375 @@
4346 +/* SPDX-License-Identifier: GPL-2.0 */
4347 +/*
4348 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4349 + *
4350 + * This program is free software; you can redistribute it and/or modify
4351 + * it under the terms of the GNU General Public License as published by
4352 + * the Free Software Foundation; either version 2 of the License, or
4353 + * (at your option) any later version.
4354 + *
4355 + * This program is distributed in the hope that it will be useful,
4356 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4357 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4358 + * GNU General Public License for more details.
4359 + *
4360 + * You should have received a copy of the GNU General Public License
4361 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4362 + */
4363 +
4364 +/*
4365 + * branch filesystems and xino for them
4366 + */
4367 +
4368 +#ifndef __AUFS_BRANCH_H__
4369 +#define __AUFS_BRANCH_H__
4370 +
4371 +#ifdef __KERNEL__
4372 +
4373 +#include <linux/mount.h>
4374 +#include "dirren.h"
4375 +#include "dynop.h"
4376 +#include "lcnt.h"
4377 +#include "rwsem.h"
4378 +#include "super.h"
4379 +
4380 +/* ---------------------------------------------------------------------- */
4381 +
4382 +/* a xino file */
4383 +struct au_xino {
4384 +       struct file             **xi_file;
4385 +       unsigned int            xi_nfile;
4386 +
4387 +       struct {
4388 +               spinlock_t              spin;
4389 +               ino_t                   *array;
4390 +               int                     total;
4391 +               /* reserved for future use */
4392 +               /* unsigned long        *bitmap; */
4393 +               wait_queue_head_t       wqh;
4394 +       } xi_nondir;
4395 +
4396 +       struct mutex            xi_mtx; /* protects xi_file array */
4397 +       struct hlist_bl_head    xi_writing;
4398 +
4399 +       atomic_t                xi_truncating;
4400 +
4401 +       struct kref             xi_kref;
4402 +};
4403 +
4404 +/* File-based Hierarchical Storage Management */
4405 +struct au_br_fhsm {
4406 +#ifdef CONFIG_AUFS_FHSM
4407 +       struct mutex            bf_lock;
4408 +       unsigned long           bf_jiffy;
4409 +       struct aufs_stfs        bf_stfs;
4410 +       int                     bf_readable;
4411 +#endif
4412 +};
4413 +
4414 +/* members for writable branch only */
4415 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4416 +struct au_wbr {
4417 +       struct au_rwsem         wbr_wh_rwsem;
4418 +       struct dentry           *wbr_wh[AuBrWh_Last];
4419 +       atomic_t                wbr_wh_running;
4420 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4421 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4422 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4423 +
4424 +       /* mfs mode */
4425 +       unsigned long long      wbr_bytes;
4426 +};
4427 +
4428 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4429 +#define AuBrDynOp (AuDyLast * 4)
4430 +
4431 +#ifdef CONFIG_AUFS_HFSNOTIFY
4432 +/* support for asynchronous destruction */
4433 +struct au_br_hfsnotify {
4434 +       struct fsnotify_group   *hfsn_group;
4435 +};
4436 +#endif
4437 +
4438 +/* sysfs entries */
4439 +struct au_brsysfs {
4440 +       char                    name[16];
4441 +       struct attribute        attr;
4442 +};
4443 +
4444 +enum {
4445 +       AuBrSysfs_BR,
4446 +       AuBrSysfs_BRID,
4447 +       AuBrSysfs_Last
4448 +};
4449 +
4450 +/* protected by superblock rwsem */
4451 +struct au_branch {
4452 +       struct au_xino          *br_xino;
4453 +
4454 +       aufs_bindex_t           br_id;
4455 +
4456 +       int                     br_perm;
4457 +       struct path             br_path;
4458 +       spinlock_t              br_dykey_lock;
4459 +       struct au_dykey         *br_dykey[AuBrDynOp];
4460 +       au_lcnt_t               br_nfiles;      /* opened files */
4461 +       au_lcnt_t               br_count;       /* in-use for other */
4462 +
4463 +       struct au_wbr           *br_wbr;
4464 +       struct au_br_fhsm       *br_fhsm;
4465 +
4466 +#ifdef CONFIG_AUFS_HFSNOTIFY
4467 +       struct au_br_hfsnotify  *br_hfsn;
4468 +#endif
4469 +
4470 +#ifdef CONFIG_SYSFS
4471 +       /* entries under sysfs per mount-point */
4472 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4473 +#endif
4474 +
4475 +#ifdef CONFIG_DEBUG_FS
4476 +       struct dentry            *br_dbgaufs; /* xino */
4477 +#endif
4478 +
4479 +       struct au_dr_br         br_dirren;
4480 +};
4481 +
4482 +/* ---------------------------------------------------------------------- */
4483 +
4484 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4485 +{
4486 +       return br->br_path.mnt;
4487 +}
4488 +
4489 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4490 +{
4491 +       return br->br_path.dentry;
4492 +}
4493 +
4494 +static inline struct mnt_idmap *au_br_idmap(struct au_branch *br)
4495 +{
4496 +       return mnt_idmap(br->br_path.mnt);
4497 +}
4498 +
4499 +static inline struct super_block *au_br_sb(struct au_branch *br)
4500 +{
4501 +       return au_br_mnt(br)->mnt_sb;
4502 +}
4503 +
4504 +static inline int au_br_rdonly(struct au_branch *br)
4505 +{
4506 +       return (sb_rdonly(au_br_sb(br))
4507 +               || !au_br_writable(br->br_perm))
4508 +               ? -EROFS : 0;
4509 +}
4510 +
4511 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4512 +{
4513 +#ifdef CONFIG_AUFS_HNOTIFY
4514 +       return !(brperm & AuBrPerm_RR);
4515 +#else
4516 +       return 0;
4517 +#endif
4518 +}
4519 +
4520 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4521 +{
4522 +       int err, exec_flag;
4523 +
4524 +       err = 0;
4525 +       exec_flag = oflag & __FMODE_EXEC;
4526 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4527 +               err = -EACCES;
4528 +
4529 +       return err;
4530 +}
4531 +
4532 +static inline void au_xino_get(struct au_branch *br)
4533 +{
4534 +       struct au_xino *xi;
4535 +
4536 +       xi = br->br_xino;
4537 +       if (xi)
4538 +               kref_get(&xi->xi_kref);
4539 +}
4540 +
4541 +static inline int au_xino_count(struct au_branch *br)
4542 +{
4543 +       int v;
4544 +       struct au_xino *xi;
4545 +
4546 +       v = 0;
4547 +       xi = br->br_xino;
4548 +       if (xi)
4549 +               v = kref_read(&xi->xi_kref);
4550 +
4551 +       return v;
4552 +}
4553 +
4554 +/* ---------------------------------------------------------------------- */
4555 +
4556 +/* branch.c */
4557 +struct au_sbinfo;
4558 +void au_br_free(struct au_sbinfo *sinfo);
4559 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4560 +struct au_opt_add;
4561 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4562 +struct au_opt_del;
4563 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4564 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4565 +#ifdef CONFIG_COMPAT
4566 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4567 +#endif
4568 +struct au_opt_mod;
4569 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4570 +             int *do_refresh);
4571 +struct aufs_stfs;
4572 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4573 +
4574 +/* xino.c */
4575 +static const loff_t au_loff_max = LLONG_MAX;
4576 +
4577 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4578 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4579 +                           int wbrtop);
4580 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4581 +                            struct file *copy_src);
4582 +struct au_xi_new {
4583 +       struct au_xino *xi;     /* switch between xino and xigen */
4584 +       int idx;
4585 +       struct path *base;
4586 +       struct file *copy_src;
4587 +};
4588 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4589 +
4590 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4591 +                ino_t *ino);
4592 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4593 +                 ino_t ino);
4594 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4595 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4596 +
4597 +int au_xib_trunc(struct super_block *sb);
4598 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4599 +
4600 +struct au_xino *au_xino_alloc(unsigned int nfile);
4601 +int au_xino_put(struct au_branch *br);
4602 +struct file *au_xino_file1(struct au_xino *xi);
4603 +
4604 +struct au_opt_xino;
4605 +void au_xino_clr(struct super_block *sb);
4606 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4607 +struct file *au_xino_def(struct super_block *sb);
4608 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4609 +                   struct path *base);
4610 +
4611 +ino_t au_xino_new_ino(struct super_block *sb);
4612 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4613 +
4614 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4615 +                      ino_t h_ino, int idx);
4616 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4617 +                     int *idx);
4618 +
4619 +int au_xino_path(struct seq_file *seq, struct file *file);
4620 +
4621 +/* ---------------------------------------------------------------------- */
4622 +
4623 +/* @idx is signed to accept -1 meaning the first file */
4624 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4625 +{
4626 +       struct file *file;
4627 +
4628 +       file = NULL;
4629 +       if (!xi)
4630 +               goto out;
4631 +
4632 +       if (idx >= 0) {
4633 +               if (idx < xi->xi_nfile)
4634 +                       file = xi->xi_file[idx];
4635 +       } else
4636 +               file = au_xino_file1(xi);
4637 +
4638 +out:
4639 +       return file;
4640 +}
4641 +
4642 +/* ---------------------------------------------------------------------- */
4643 +
4644 +/* Superblock to branch */
4645 +static inline
4646 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4647 +{
4648 +       return au_sbr(sb, bindex)->br_id;
4649 +}
4650 +
4651 +static inline
4652 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4653 +{
4654 +       return au_br_mnt(au_sbr(sb, bindex));
4655 +}
4656 +
4657 +static inline
4658 +struct mnt_idmap *au_sbr_idmap(struct super_block *sb, aufs_bindex_t bindex)
4659 +{
4660 +       return au_br_idmap(au_sbr(sb, bindex));
4661 +}
4662 +
4663 +static inline
4664 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4665 +{
4666 +       return au_br_sb(au_sbr(sb, bindex));
4667 +}
4668 +
4669 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4670 +{
4671 +       return au_sbr(sb, bindex)->br_perm;
4672 +}
4673 +
4674 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4675 +{
4676 +       return au_br_whable(au_sbr_perm(sb, bindex));
4677 +}
4678 +
4679 +/* ---------------------------------------------------------------------- */
4680 +
4681 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4682 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4683 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4684 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4685 +/*
4686 +#define wbr_wh_read_trylock_nested(wbr) \
4687 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4688 +#define wbr_wh_write_trylock_nested(wbr) \
4689 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4690 +*/
4691 +
4692 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4693 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4694 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4695 +
4696 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4697 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4698 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4699 +
4700 +/* ---------------------------------------------------------------------- */
4701 +
4702 +#ifdef CONFIG_AUFS_FHSM
4703 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4704 +{
4705 +       mutex_init(&brfhsm->bf_lock);
4706 +       brfhsm->bf_jiffy = 0;
4707 +       brfhsm->bf_readable = 0;
4708 +}
4709 +
4710 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4711 +{
4712 +       mutex_destroy(&brfhsm->bf_lock);
4713 +}
4714 +#else
4715 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4716 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4717 +#endif
4718 +
4719 +#endif /* __KERNEL__ */
4720 +#endif /* __AUFS_BRANCH_H__ */
4721 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4722 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4723 +++ linux/fs/aufs/conf.mk       2022-11-05 23:02:18.959222617 +0100
4724 @@ -0,0 +1,40 @@
4725 +# SPDX-License-Identifier: GPL-2.0
4726 +
4727 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4728 +
4729 +define AuConf
4730 +ifdef ${1}
4731 +AuConfStr += ${1}=${${1}}
4732 +endif
4733 +endef
4734 +
4735 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4736 +       SBILIST \
4737 +       HNOTIFY HFSNOTIFY \
4738 +       EXPORT INO_T_64 \
4739 +       XATTR \
4740 +       FHSM \
4741 +       RDU \
4742 +       DIRREN \
4743 +       SHWH \
4744 +       BR_RAMFS \
4745 +       BR_FUSE POLL \
4746 +       BR_HFSPLUS \
4747 +       BDEV_LOOP \
4748 +       DEBUG MAGIC_SYSRQ
4749 +$(foreach i, ${AuConfAll}, \
4750 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4751 +
4752 +AuConfName = ${obj}/conf.str
4753 +${AuConfName}.tmp: FORCE
4754 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4755 +${AuConfName}: ${AuConfName}.tmp
4756 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4757 +       echo '  GEN    ' $@; \
4758 +       cp -p $< $@; \
4759 +       }
4760 +FORCE:
4761 +clean-files += ${AuConfName} ${AuConfName}.tmp
4762 +${obj}/sysfs.o: ${AuConfName}
4763 +
4764 +-include ${srctree}/${src}/conf_priv.mk
4765 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4766 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4767 +++ linux/fs/aufs/cpup.c        2023-08-28 12:34:39.956636132 +0200
4768 @@ -0,0 +1,1459 @@
4769 +// SPDX-License-Identifier: GPL-2.0
4770 +/*
4771 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4772 + *
4773 + * This program is free software; you can redistribute it and/or modify
4774 + * it under the terms of the GNU General Public License as published by
4775 + * the Free Software Foundation; either version 2 of the License, or
4776 + * (at your option) any later version.
4777 + *
4778 + * This program is distributed in the hope that it will be useful,
4779 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4780 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4781 + * GNU General Public License for more details.
4782 + *
4783 + * You should have received a copy of the GNU General Public License
4784 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4785 + */
4786 +
4787 +/*
4788 + * copy-up functions, see wbr_policy.c for copy-down
4789 + */
4790 +
4791 +#include <linux/fs_stack.h>
4792 +#include <linux/mm.h>
4793 +#include <linux/task_work.h>
4794 +#include "aufs.h"
4795 +
4796 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4797 +{
4798 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4799 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4800 +
4801 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4802 +
4803 +       dst->i_flags |= iflags & ~mask;
4804 +       if (au_test_fs_notime(dst->i_sb))
4805 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4806 +}
4807 +
4808 +void au_cpup_attr_timesizes(struct inode *inode)
4809 +{
4810 +       struct inode *h_inode;
4811 +
4812 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4813 +       fsstack_copy_attr_times(inode, h_inode);
4814 +       fsstack_copy_inode_size(inode, h_inode);
4815 +}
4816 +
4817 +void au_cpup_attr_nlink(struct inode *inode, int force)
4818 +{
4819 +       struct inode *h_inode;
4820 +       struct super_block *sb;
4821 +       aufs_bindex_t bindex, bbot;
4822 +
4823 +       sb = inode->i_sb;
4824 +       bindex = au_ibtop(inode);
4825 +       h_inode = au_h_iptr(inode, bindex);
4826 +       if (!force
4827 +           && !S_ISDIR(h_inode->i_mode)
4828 +           && au_opt_test(au_mntflags(sb), PLINK)
4829 +           && au_plink_test(inode))
4830 +               return;
4831 +
4832 +       /*
4833 +        * 0 can happen in revalidating.
4834 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4835 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4836 +        * case.
4837 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4838 +        *       the incorrect link count.
4839 +        */
4840 +       set_nlink(inode, h_inode->i_nlink);
4841 +
4842 +       /*
4843 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4844 +        * it may includes whplink directory.
4845 +        */
4846 +       if (S_ISDIR(h_inode->i_mode)) {
4847 +               bbot = au_ibbot(inode);
4848 +               for (bindex++; bindex <= bbot; bindex++) {
4849 +                       h_inode = au_h_iptr(inode, bindex);
4850 +                       if (h_inode)
4851 +                               au_add_nlink(inode, h_inode);
4852 +               }
4853 +       }
4854 +}
4855 +
4856 +void au_cpup_attr_changeable(struct inode *inode)
4857 +{
4858 +       struct inode *h_inode;
4859 +
4860 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4861 +       inode->i_mode = h_inode->i_mode;
4862 +       inode->i_uid = h_inode->i_uid;
4863 +       inode->i_gid = h_inode->i_gid;
4864 +       au_cpup_attr_timesizes(inode);
4865 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4866 +}
4867 +
4868 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4869 +{
4870 +       struct au_iinfo *iinfo = au_ii(inode);
4871 +
4872 +       IiMustWriteLock(inode);
4873 +
4874 +       iinfo->ii_higen = h_inode->i_generation;
4875 +       iinfo->ii_hsb1 = h_inode->i_sb;
4876 +}
4877 +
4878 +void au_cpup_attr_all(struct inode *inode, int force)
4879 +{
4880 +       struct inode *h_inode;
4881 +
4882 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4883 +       au_cpup_attr_changeable(inode);
4884 +       if (inode->i_nlink > 0)
4885 +               au_cpup_attr_nlink(inode, force);
4886 +       inode->i_rdev = h_inode->i_rdev;
4887 +       inode->i_blkbits = h_inode->i_blkbits;
4888 +       au_cpup_igen(inode, h_inode);
4889 +}
4890 +
4891 +/* ---------------------------------------------------------------------- */
4892 +
4893 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4894 +
4895 +/* keep the timestamps of the parent dir when cpup */
4896 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4897 +                   struct path *h_path)
4898 +{
4899 +       struct inode *h_inode;
4900 +
4901 +       dt->dt_dentry = dentry;
4902 +       dt->dt_h_path = *h_path;
4903 +       h_inode = d_inode(h_path->dentry);
4904 +       dt->dt_atime = h_inode->i_atime;
4905 +       dt->dt_mtime = h_inode->i_mtime;
4906 +       /* smp_mb(); */
4907 +}
4908 +
4909 +void au_dtime_revert(struct au_dtime *dt)
4910 +{
4911 +       struct iattr attr;
4912 +       int err;
4913 +
4914 +       attr.ia_atime = dt->dt_atime;
4915 +       attr.ia_mtime = dt->dt_mtime;
4916 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4917 +               | ATTR_ATIME | ATTR_ATIME_SET;
4918 +
4919 +       /* no delegation since this is a directory */
4920 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4921 +       if (unlikely(err))
4922 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4923 +}
4924 +
4925 +/* ---------------------------------------------------------------------- */
4926 +
4927 +/* internal use only */
4928 +struct au_cpup_reg_attr {
4929 +       int             valid;
4930 +       struct kstat    st;
4931 +       unsigned int    iflags; /* inode->i_flags */
4932 +};
4933 +
4934 +static noinline_for_stack
4935 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4936 +              struct au_cpup_reg_attr *h_src_attr)
4937 +{
4938 +       int err, sbits, icex;
4939 +       unsigned int mnt_flags;
4940 +       unsigned char verbose;
4941 +       struct iattr ia;
4942 +       struct path h_path;
4943 +       struct inode *h_isrc, *h_idst;
4944 +       struct kstat *h_st;
4945 +       struct au_branch *br;
4946 +
4947 +       br = au_sbr(dst->d_sb, bindex);
4948 +       h_path.mnt = au_br_mnt(br);
4949 +       h_path.dentry = au_h_dptr(dst, bindex);
4950 +       h_idst = d_inode(h_path.dentry);
4951 +       h_isrc = d_inode(h_src->dentry);
4952 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4953 +               | ATTR_ATIME | ATTR_MTIME
4954 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4955 +       if (h_src_attr && h_src_attr->valid) {
4956 +               h_st = &h_src_attr->st;
4957 +               ia.ia_uid = h_st->uid;
4958 +               ia.ia_gid = h_st->gid;
4959 +               ia.ia_atime = h_st->atime;
4960 +               ia.ia_mtime = h_st->mtime;
4961 +               if (h_idst->i_mode != h_st->mode
4962 +                   && !S_ISLNK(h_idst->i_mode)) {
4963 +                       ia.ia_valid |= ATTR_MODE;
4964 +                       ia.ia_mode = h_st->mode;
4965 +               }
4966 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4967 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4968 +       } else {
4969 +               ia.ia_uid = h_isrc->i_uid;
4970 +               ia.ia_gid = h_isrc->i_gid;
4971 +               ia.ia_atime = h_isrc->i_atime;
4972 +               ia.ia_mtime = h_isrc->i_mtime;
4973 +               if (h_idst->i_mode != h_isrc->i_mode
4974 +                   && !S_ISLNK(h_idst->i_mode)) {
4975 +                       ia.ia_valid |= ATTR_MODE;
4976 +                       ia.ia_mode = h_isrc->i_mode;
4977 +               }
4978 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4979 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4980 +       }
4981 +       /* no delegation since it is just created */
4982 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4983 +
4984 +       /* is this nfs only? */
4985 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4986 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4987 +               ia.ia_mode = h_isrc->i_mode;
4988 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4989 +       }
4990 +
4991 +       icex = br->br_perm & AuBrAttr_ICEX;
4992 +       if (!err) {
4993 +               mnt_flags = au_mntflags(dst->d_sb);
4994 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4995 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4996 +       }
4997 +
4998 +       return err;
4999 +}
5000 +
5001 +/* ---------------------------------------------------------------------- */
5002 +
5003 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5004 +                          char *buf, unsigned long blksize)
5005 +{
5006 +       int err;
5007 +       size_t sz, rbytes, wbytes;
5008 +       unsigned char all_zero;
5009 +       char *p, *zp;
5010 +       struct inode *h_inode;
5011 +       /* reduce stack usage */
5012 +       struct iattr *ia;
5013 +
5014 +       zp = page_address(ZERO_PAGE(0));
5015 +       if (unlikely(!zp))
5016 +               return -ENOMEM; /* possible? */
5017 +
5018 +       err = 0;
5019 +       all_zero = 0;
5020 +       while (len) {
5021 +               AuDbg("len %lld\n", len);
5022 +               sz = blksize;
5023 +               if (len < blksize)
5024 +                       sz = len;
5025 +
5026 +               rbytes = 0;
5027 +               /* todo: signal_pending? */
5028 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5029 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5030 +                       err = rbytes;
5031 +               }
5032 +               if (unlikely(err < 0))
5033 +                       break;
5034 +
5035 +               all_zero = 0;
5036 +               if (len >= rbytes && rbytes == blksize)
5037 +                       all_zero = !memcmp(buf, zp, rbytes);
5038 +               if (!all_zero) {
5039 +                       wbytes = rbytes;
5040 +                       p = buf;
5041 +                       while (wbytes) {
5042 +                               size_t b;
5043 +
5044 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5045 +                               err = b;
5046 +                               /* todo: signal_pending? */
5047 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5048 +                                       continue;
5049 +                               if (unlikely(err < 0))
5050 +                                       break;
5051 +                               wbytes -= b;
5052 +                               p += b;
5053 +                       }
5054 +                       if (unlikely(err < 0))
5055 +                               break;
5056 +               } else {
5057 +                       loff_t res;
5058 +
5059 +                       AuLabel(hole);
5060 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5061 +                       err = res;
5062 +                       if (unlikely(res < 0))
5063 +                               break;
5064 +               }
5065 +               len -= rbytes;
5066 +               err = 0;
5067 +       }
5068 +
5069 +       /* the last block may be a hole */
5070 +       if (!err && all_zero) {
5071 +               AuLabel(last hole);
5072 +
5073 +               err = 1;
5074 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5075 +                       /* nfs requires this step to make last hole */
5076 +                       /* is this only nfs? */
5077 +                       do {
5078 +                               /* todo: signal_pending? */
5079 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5080 +                       } while (err == -EAGAIN || err == -EINTR);
5081 +                       if (err == 1)
5082 +                               dst->f_pos--;
5083 +               }
5084 +
5085 +               if (err == 1) {
5086 +                       ia = (void *)buf;
5087 +                       ia->ia_size = dst->f_pos;
5088 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5089 +                       ia->ia_file = dst;
5090 +                       h_inode = file_inode(dst);
5091 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5092 +                       /* no delegation since it is just created */
5093 +                       err = vfsub_notify_change(&dst->f_path, ia,
5094 +                                                 /*delegated*/NULL);
5095 +                       inode_unlock(h_inode);
5096 +               }
5097 +       }
5098 +
5099 +       return err;
5100 +}
5101 +
5102 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5103 +{
5104 +       int err;
5105 +       unsigned long blksize;
5106 +       unsigned char do_kfree;
5107 +       char *buf;
5108 +       struct super_block *h_sb;
5109 +
5110 +       err = -ENOMEM;
5111 +       h_sb = file_inode(dst)->i_sb;
5112 +       blksize = h_sb->s_blocksize;
5113 +       if (!blksize || PAGE_SIZE < blksize)
5114 +               blksize = PAGE_SIZE;
5115 +       AuDbg("blksize %lu\n", blksize);
5116 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5117 +       if (do_kfree)
5118 +               buf = kmalloc(blksize, GFP_NOFS);
5119 +       else
5120 +               buf = (void *)__get_free_page(GFP_NOFS);
5121 +       if (unlikely(!buf))
5122 +               goto out;
5123 +
5124 +       if (len > (1 << 22))
5125 +               AuDbg("copying a large file %lld\n", (long long)len);
5126 +
5127 +       src->f_pos = 0;
5128 +       dst->f_pos = 0;
5129 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5130 +       if (do_kfree) {
5131 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5132 +               au_kfree_do_rcu(buf);
5133 +       } else
5134 +               free_page((unsigned long)buf);
5135 +
5136 +out:
5137 +       return err;
5138 +}
5139 +
5140 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5141 +{
5142 +       int err;
5143 +       struct super_block *h_src_sb;
5144 +       struct inode *h_src_inode;
5145 +
5146 +       h_src_inode = file_inode(src);
5147 +       h_src_sb = h_src_inode->i_sb;
5148 +
5149 +       /* XFS acquires inode_lock */
5150 +       if (!au_test_xfs(h_src_sb))
5151 +               err = au_copy_file(dst, src, len);
5152 +       else {
5153 +               inode_unlock_shared(h_src_inode);
5154 +               err = au_copy_file(dst, src, len);
5155 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5156 +       }
5157 +
5158 +       return err;
5159 +}
5160 +
5161 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5162 +{
5163 +       int err;
5164 +       loff_t lo;
5165 +       struct super_block *h_src_sb;
5166 +       struct inode *h_src_inode;
5167 +
5168 +       h_src_inode = file_inode(src);
5169 +       h_src_sb = h_src_inode->i_sb;
5170 +       if (h_src_sb != file_inode(dst)->i_sb
5171 +           || !dst->f_op->remap_file_range) {
5172 +               err = au_do_copy(dst, src, len);
5173 +               goto out;
5174 +       }
5175 +
5176 +       if (!au_test_nfs(h_src_sb)) {
5177 +               inode_unlock_shared(h_src_inode);
5178 +               lo = vfsub_clone_file_range(src, dst, len);
5179 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5180 +       } else
5181 +               lo = vfsub_clone_file_range(src, dst, len);
5182 +       if (lo == len) {
5183 +               err = 0;
5184 +               goto out; /* success */
5185 +       } else if (lo >= 0)
5186 +               /* todo: possible? */
5187 +               /* paritially succeeded */
5188 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5189 +       else if (lo != -EOPNOTSUPP) {
5190 +               /* older XFS has a condition in cloning */
5191 +               err = lo;
5192 +               goto out;
5193 +       }
5194 +
5195 +       /* the backend fs on NFS may not support cloning */
5196 +       err = au_do_copy(dst, src, len);
5197 +
5198 +out:
5199 +       AuTraceErr(err);
5200 +       return err;
5201 +}
5202 +
5203 +/*
5204 + * to support a sparse file which is opened with O_APPEND,
5205 + * we need to close the file.
5206 + */
5207 +static int au_cp_regular(struct au_cp_generic *cpg)
5208 +{
5209 +       int err, i;
5210 +       enum { SRC, DST };
5211 +       struct {
5212 +               aufs_bindex_t bindex;
5213 +               unsigned int flags;
5214 +               struct dentry *dentry;
5215 +               int force_wr;
5216 +               struct file *file;
5217 +       } *f, file[] = {
5218 +               {
5219 +                       .bindex = cpg->bsrc,
5220 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5221 +               },
5222 +               {
5223 +                       .bindex = cpg->bdst,
5224 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5225 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5226 +               }
5227 +       };
5228 +       struct au_branch *br;
5229 +       struct super_block *sb, *h_src_sb;
5230 +       struct inode *h_src_inode;
5231 +       struct task_struct *tsk = current;
5232 +
5233 +       /* bsrc branch can be ro/rw. */
5234 +       sb = cpg->dentry->d_sb;
5235 +       f = file;
5236 +       for (i = 0; i < 2; i++, f++) {
5237 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5238 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5239 +                                   /*file*/NULL, f->force_wr);
5240 +               if (IS_ERR(f->file)) {
5241 +                       err = PTR_ERR(f->file);
5242 +                       if (i == SRC)
5243 +                               goto out;
5244 +                       else
5245 +                               goto out_src;
5246 +               }
5247 +       }
5248 +
5249 +       /* try stopping to update while we copyup */
5250 +       h_src_inode = d_inode(file[SRC].dentry);
5251 +       h_src_sb = h_src_inode->i_sb;
5252 +       if (!au_test_nfs(h_src_sb))
5253 +               IMustLock(h_src_inode);
5254 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5255 +
5256 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5257 +       if (tsk->flags & PF_KTHREAD)
5258 +               __fput_sync(file[DST].file);
5259 +       else {
5260 +               /* it happened actually */
5261 +               fput(file[DST].file);
5262 +               /*
5263 +                * too bad.
5264 +                * we have to call both since we don't know which place the file
5265 +                * was added to.
5266 +                */
5267 +               task_work_run();
5268 +               flush_delayed_fput();
5269 +       }
5270 +       br = au_sbr(sb, file[DST].bindex);
5271 +       au_lcnt_dec(&br->br_nfiles);
5272 +
5273 +out_src:
5274 +       fput(file[SRC].file);
5275 +       br = au_sbr(sb, file[SRC].bindex);
5276 +       au_lcnt_dec(&br->br_nfiles);
5277 +out:
5278 +       return err;
5279 +}
5280 +
5281 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5282 +                             struct au_cpup_reg_attr *h_src_attr)
5283 +{
5284 +       int err, rerr;
5285 +       loff_t l;
5286 +       struct path h_path;
5287 +       struct inode *h_src_inode, *h_dst_inode;
5288 +
5289 +       err = 0;
5290 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5291 +       l = i_size_read(h_src_inode);
5292 +       if (cpg->len == -1 || l < cpg->len)
5293 +               cpg->len = l;
5294 +       if (cpg->len) {
5295 +               /* try stopping to update while we are referencing */
5296 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5297 +               au_pin_hdir_unlock(cpg->pin);
5298 +
5299 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5300 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5301 +               h_src_attr->iflags = h_src_inode->i_flags;
5302 +               if (!au_test_nfs(h_src_inode->i_sb))
5303 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5304 +               else {
5305 +                       inode_unlock_shared(h_src_inode);
5306 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5307 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5308 +               }
5309 +               if (unlikely(err)) {
5310 +                       inode_unlock_shared(h_src_inode);
5311 +                       goto out;
5312 +               }
5313 +               h_src_attr->valid = 1;
5314 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5315 +                       err = au_cp_regular(cpg);
5316 +                       inode_unlock_shared(h_src_inode);
5317 +               } else {
5318 +                       inode_unlock_shared(h_src_inode);
5319 +                       err = au_cp_regular(cpg);
5320 +               }
5321 +               rerr = au_pin_hdir_relock(cpg->pin);
5322 +               if (!err && rerr)
5323 +                       err = rerr;
5324 +       }
5325 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5326 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5327 +               h_dst_inode = d_inode(h_path.dentry);
5328 +               spin_lock(&h_dst_inode->i_lock);
5329 +               h_dst_inode->i_state |= I_LINKABLE;
5330 +               spin_unlock(&h_dst_inode->i_lock);
5331 +       }
5332 +
5333 +out:
5334 +       return err;
5335 +}
5336 +
5337 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5338 +                             struct inode *h_dir)
5339 +{
5340 +       int err;
5341 +       DEFINE_DELAYED_CALL(done);
5342 +       const char *sym;
5343 +
5344 +       sym = vfs_get_link(h_src, &done);
5345 +       err = PTR_ERR(sym);
5346 +       if (IS_ERR(sym))
5347 +               goto out;
5348 +
5349 +       err = vfsub_symlink(h_dir, h_path, sym);
5350 +
5351 +out:
5352 +       do_delayed_call(&done);
5353 +       return err;
5354 +}
5355 +
5356 +/*
5357 + * regardless 'acl' option, reset all ACL.
5358 + * All ACL will be copied up later from the original entry on the lower branch.
5359 + */
5360 +static int au_reset_acl(struct path *h_path, umode_t mode)
5361 +{
5362 +       int err;
5363 +       struct dentry *h_dentry;
5364 +       /* struct inode *h_inode; */
5365 +       struct mnt_idmap *h_idmap;
5366 +
5367 +       h_idmap = mnt_idmap(h_path->mnt);
5368 +       h_dentry = h_path->dentry;
5369 +       /* h_inode = d_inode(h_dentry); */
5370 +       /* forget_all_cached_acls(h_inode)); */
5371 +       err = vfsub_remove_acl(h_idmap, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5372 +       AuTraceErr(err);
5373 +       if (err == -EOPNOTSUPP)
5374 +               err = 0;
5375 +       if (!err)
5376 +               err = vfsub_acl_chmod(h_idmap, h_dentry, mode);
5377 +
5378 +       AuTraceErr(err);
5379 +       return err;
5380 +}
5381 +
5382 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5383 +                         struct inode *h_dir, struct path *h_path)
5384 +{
5385 +       int err;
5386 +       struct inode *dir, *inode;
5387 +       struct mnt_idmap *h_idmap;
5388 +
5389 +       h_idmap = mnt_idmap(h_path->mnt);
5390 +       err = vfsub_remove_acl(h_idmap, h_path->dentry,
5391 +                              XATTR_NAME_POSIX_ACL_DEFAULT);
5392 +       AuTraceErr(err);
5393 +       if (err == -EOPNOTSUPP)
5394 +               err = 0;
5395 +       if (unlikely(err))
5396 +               goto out;
5397 +
5398 +       /*
5399 +        * strange behaviour from the users view,
5400 +        * particularly setattr case
5401 +        */
5402 +       dir = d_inode(dst_parent);
5403 +       if (au_ibtop(dir) == cpg->bdst)
5404 +               au_cpup_attr_nlink(dir, /*force*/1);
5405 +       inode = d_inode(cpg->dentry);
5406 +       au_cpup_attr_nlink(inode, /*force*/1);
5407 +
5408 +out:
5409 +       return err;
5410 +}
5411 +
5412 +static noinline_for_stack
5413 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5414 +              struct au_cpup_reg_attr *h_src_attr)
5415 +{
5416 +       int err;
5417 +       umode_t mode;
5418 +       unsigned int mnt_flags;
5419 +       unsigned char isdir, isreg, force;
5420 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5421 +       struct au_dtime dt;
5422 +       struct path h_path;
5423 +       struct dentry *h_src, *h_dst, *h_parent;
5424 +       struct inode *h_inode, *h_dir;
5425 +       struct super_block *sb;
5426 +
5427 +       /* bsrc branch can be ro/rw. */
5428 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5429 +       h_inode = d_inode(h_src);
5430 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5431 +
5432 +       /* try stopping to be referenced while we are creating */
5433 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5434 +       if (au_ftest_cpup(cpg->flags, RENAME))
5435 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5436 +                                 AUFS_WH_PFX_LEN));
5437 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5438 +       h_dir = d_inode(h_parent);
5439 +       IMustLock(h_dir);
5440 +       AuDebugOn(h_parent != h_dst->d_parent);
5441 +
5442 +       sb = cpg->dentry->d_sb;
5443 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5444 +       if (do_dt) {
5445 +               h_path.dentry = h_parent;
5446 +               au_dtime_store(&dt, dst_parent, &h_path);
5447 +       }
5448 +       h_path.dentry = h_dst;
5449 +
5450 +       isreg = 0;
5451 +       isdir = 0;
5452 +       mode = h_inode->i_mode;
5453 +       switch (mode & S_IFMT) {
5454 +       case S_IFREG:
5455 +               isreg = 1;
5456 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5457 +               if (!err)
5458 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5459 +               break;
5460 +       case S_IFDIR:
5461 +               isdir = 1;
5462 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5463 +               if (!err)
5464 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5465 +               break;
5466 +       case S_IFLNK:
5467 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5468 +               break;
5469 +       case S_IFCHR:
5470 +       case S_IFBLK:
5471 +               AuDebugOn(!capable(CAP_MKNOD));
5472 +               fallthrough;
5473 +       case S_IFIFO:
5474 +       case S_IFSOCK:
5475 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5476 +               break;
5477 +       default:
5478 +               AuIOErr("Unknown inode type 0%o\n", mode);
5479 +               err = -EIO;
5480 +       }
5481 +       if (!err)
5482 +               err = au_reset_acl(&h_path, mode);
5483 +
5484 +       mnt_flags = au_mntflags(sb);
5485 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5486 +           && !isdir
5487 +           && au_opt_test(mnt_flags, XINO)
5488 +           && (h_inode->i_nlink == 1
5489 +               || (h_inode->i_state & I_LINKABLE))
5490 +           /* todo: unnecessary? */
5491 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5492 +           && cpg->bdst < cpg->bsrc
5493 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5494 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5495 +               /* ignore this error */
5496 +
5497 +       if (!err) {
5498 +               force = 0;
5499 +               if (isreg) {
5500 +                       force = !!cpg->len;
5501 +                       if (cpg->len == -1)
5502 +                               force = !!i_size_read(h_inode);
5503 +               }
5504 +               au_fhsm_wrote(sb, cpg->bdst, force);
5505 +       }
5506 +
5507 +       if (do_dt)
5508 +               au_dtime_revert(&dt);
5509 +       return err;
5510 +}
5511 +
5512 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5513 +{
5514 +       int err;
5515 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5516 +       struct path h_ppath;
5517 +       struct inode *h_dir;
5518 +       aufs_bindex_t bdst;
5519 +
5520 +       dentry = cpg->dentry;
5521 +       bdst = cpg->bdst;
5522 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5523 +       h_dentry = au_h_dptr(dentry, bdst);
5524 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5525 +               dget(h_dentry);
5526 +               au_set_h_dptr(dentry, bdst, NULL);
5527 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5528 +               if (!err)
5529 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5530 +               au_set_h_dptr(dentry, bdst, h_dentry);
5531 +       } else {
5532 +               err = 0;
5533 +               parent = dget_parent(dentry);
5534 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5535 +               dput(parent);
5536 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5537 +               if (IS_ERR(h_path->dentry))
5538 +                       err = PTR_ERR(h_path->dentry);
5539 +       }
5540 +       if (unlikely(err))
5541 +               goto out;
5542 +
5543 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5544 +       h_dir = d_inode(h_parent);
5545 +       IMustLock(h_dir);
5546 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5547 +       /* no delegation since it is just created */
5548 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5549 +                          /*flags*/0);
5550 +       dput(h_path->dentry);
5551 +
5552 +out:
5553 +       return err;
5554 +}
5555 +
5556 +/*
5557 + * copyup the @dentry from @bsrc to @bdst.
5558 + * the caller must set the both of lower dentries.
5559 + * @len is for truncating when it is -1 copyup the entire file.
5560 + * in link/rename cases, @dst_parent may be different from the real one.
5561 + * basic->bsrc can be larger than basic->bdst.
5562 + * aufs doesn't touch the credential so
5563 + * security_inode_copy_up{,_xattr}() are unnecessary.
5564 + */
5565 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5566 +{
5567 +       int err, rerr;
5568 +       aufs_bindex_t old_ibtop;
5569 +       unsigned char isdir, plink;
5570 +       struct dentry *h_src, *h_dst, *h_parent;
5571 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5572 +       struct super_block *sb;
5573 +       struct au_branch *br;
5574 +       struct path h_src_path;
5575 +       /* to reduce stack size */
5576 +       struct {
5577 +               struct au_dtime dt;
5578 +               struct path h_path;
5579 +               struct au_cpup_reg_attr h_src_attr;
5580 +       } *a;
5581 +
5582 +       err = -ENOMEM;
5583 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5584 +       if (unlikely(!a))
5585 +               goto out;
5586 +       a->h_src_attr.valid = 0;
5587 +
5588 +       sb = cpg->dentry->d_sb;
5589 +       br = au_sbr(sb, cpg->bdst);
5590 +       a->h_path.mnt = au_br_mnt(br);
5591 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5592 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5593 +       h_dir = d_inode(h_parent);
5594 +       IMustLock(h_dir);
5595 +
5596 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5597 +       inode = d_inode(cpg->dentry);
5598 +
5599 +       if (!dst_parent)
5600 +               dst_parent = dget_parent(cpg->dentry);
5601 +       else
5602 +               dget(dst_parent);
5603 +
5604 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5605 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5606 +       if (dst_inode) {
5607 +               if (unlikely(!plink)) {
5608 +                       err = -EIO;
5609 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5610 +                               "but plink is disabled\n",
5611 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5612 +                       goto out_parent;
5613 +               }
5614 +
5615 +               if (dst_inode->i_nlink) {
5616 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5617 +
5618 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5619 +                       err = PTR_ERR(h_src);
5620 +                       if (IS_ERR(h_src))
5621 +                               goto out_parent;
5622 +                       if (unlikely(d_is_negative(h_src))) {
5623 +                               err = -EIO;
5624 +                               AuIOErr("i%lu exists on b%d "
5625 +                                       "but not pseudo-linked\n",
5626 +                                       inode->i_ino, cpg->bdst);
5627 +                               dput(h_src);
5628 +                               goto out_parent;
5629 +                       }
5630 +
5631 +                       if (do_dt) {
5632 +                               a->h_path.dentry = h_parent;
5633 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5634 +                       }
5635 +
5636 +                       a->h_path.dentry = h_dst;
5637 +                       delegated = NULL;
5638 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5639 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5640 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5641 +                       if (do_dt)
5642 +                               au_dtime_revert(&a->dt);
5643 +                       if (unlikely(err == -EWOULDBLOCK)) {
5644 +                               pr_warn("cannot retry for NFSv4 delegation"
5645 +                                       " for an internal link\n");
5646 +                               iput(delegated);
5647 +                       }
5648 +                       dput(h_src);
5649 +                       goto out_parent;
5650 +               } else
5651 +                       /* todo: cpup_wh_file? */
5652 +                       /* udba work */
5653 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5654 +       }
5655 +
5656 +       isdir = S_ISDIR(inode->i_mode);
5657 +       old_ibtop = au_ibtop(inode);
5658 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5659 +       if (unlikely(err))
5660 +               goto out_rev;
5661 +       dst_inode = d_inode(h_dst);
5662 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5663 +       /* todo: necessary? */
5664 +       /* au_pin_hdir_unlock(cpg->pin); */
5665 +
5666 +       h_src_path.dentry = h_src;
5667 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5668 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5669 +       if (unlikely(err)) {
5670 +               /* todo: necessary? */
5671 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5672 +               inode_unlock(dst_inode);
5673 +               goto out_rev;
5674 +       }
5675 +
5676 +       if (cpg->bdst < old_ibtop) {
5677 +               if (S_ISREG(inode->i_mode)) {
5678 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5679 +                       if (unlikely(err)) {
5680 +                               /* ignore an error */
5681 +                               /* au_pin_hdir_relock(cpg->pin); */
5682 +                               inode_unlock(dst_inode);
5683 +                               goto out_rev;
5684 +                       }
5685 +               }
5686 +               au_set_ibtop(inode, cpg->bdst);
5687 +       } else
5688 +               au_set_ibbot(inode, cpg->bdst);
5689 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5690 +                     au_hi_flags(inode, isdir));
5691 +
5692 +       /* todo: necessary? */
5693 +       /* err = au_pin_hdir_relock(cpg->pin); */
5694 +       inode_unlock(dst_inode);
5695 +       if (unlikely(err))
5696 +               goto out_rev;
5697 +
5698 +       src_inode = d_inode(h_src);
5699 +       if (!isdir
5700 +           && (src_inode->i_nlink > 1
5701 +               || src_inode->i_state & I_LINKABLE)
5702 +           && plink)
5703 +               au_plink_append(inode, cpg->bdst, h_dst);
5704 +
5705 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5706 +               a->h_path.dentry = h_dst;
5707 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5708 +       }
5709 +       if (!err)
5710 +               goto out_parent; /* success */
5711 +
5712 +       /* revert */
5713 +out_rev:
5714 +       a->h_path.dentry = h_parent;
5715 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5716 +       a->h_path.dentry = h_dst;
5717 +       rerr = 0;
5718 +       if (d_is_positive(h_dst)) {
5719 +               if (!isdir) {
5720 +                       /* no delegation since it is just created */
5721 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5722 +                                           /*delegated*/NULL, /*force*/0);
5723 +               } else
5724 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5725 +       }
5726 +       au_dtime_revert(&a->dt);
5727 +       if (rerr) {
5728 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5729 +               err = -EIO;
5730 +       }
5731 +out_parent:
5732 +       dput(dst_parent);
5733 +       au_kfree_rcu(a);
5734 +out:
5735 +       return err;
5736 +}
5737 +
5738 +#if 0 /* reserved */
5739 +struct au_cpup_single_args {
5740 +       int *errp;
5741 +       struct au_cp_generic *cpg;
5742 +       struct dentry *dst_parent;
5743 +};
5744 +
5745 +static void au_call_cpup_single(void *args)
5746 +{
5747 +       struct au_cpup_single_args *a = args;
5748 +
5749 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5750 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5751 +       au_pin_hdir_release(a->cpg->pin);
5752 +}
5753 +#endif
5754 +
5755 +/*
5756 + * prevent SIGXFSZ in copy-up.
5757 + * testing CAP_MKNOD is for generic fs,
5758 + * but CAP_FSETID is for xfs only, currently.
5759 + */
5760 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5761 +{
5762 +       int do_sio;
5763 +       struct super_block *sb;
5764 +       struct inode *h_dir;
5765 +
5766 +       do_sio = 0;
5767 +       sb = au_pinned_parent(pin)->d_sb;
5768 +       if (!au_wkq_test()
5769 +           && (!au_sbi(sb)->si_plink_maint_pid
5770 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5771 +               switch (mode & S_IFMT) {
5772 +               case S_IFREG:
5773 +                       /* no condition about RLIMIT_FSIZE and the file size */
5774 +                       do_sio = 1;
5775 +                       break;
5776 +               case S_IFCHR:
5777 +               case S_IFBLK:
5778 +                       do_sio = !capable(CAP_MKNOD);
5779 +                       break;
5780 +               }
5781 +               if (!do_sio)
5782 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5783 +                                 && !capable(CAP_FSETID));
5784 +               /* this workaround may be removed in the future */
5785 +               if (!do_sio) {
5786 +                       h_dir = au_pinned_h_dir(pin);
5787 +                       do_sio = h_dir->i_mode & S_ISVTX;
5788 +               }
5789 +       }
5790 +
5791 +       return do_sio;
5792 +}
5793 +
5794 +#if 0 /* reserved */
5795 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5796 +{
5797 +       int err, wkq_err;
5798 +       struct dentry *h_dentry;
5799 +
5800 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5801 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5802 +               err = au_cpup_single(cpg, dst_parent);
5803 +       else {
5804 +               struct au_cpup_single_args args = {
5805 +                       .errp           = &err,
5806 +                       .cpg            = cpg,
5807 +                       .dst_parent     = dst_parent
5808 +               };
5809 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5810 +               if (unlikely(wkq_err))
5811 +                       err = wkq_err;
5812 +       }
5813 +
5814 +       return err;
5815 +}
5816 +#endif
5817 +
5818 +/*
5819 + * copyup the @dentry from the first active lower branch to @bdst,
5820 + * using au_cpup_single().
5821 + */
5822 +static int au_cpup_simple(struct au_cp_generic *cpg)
5823 +{
5824 +       int err;
5825 +       unsigned int flags_orig;
5826 +       struct dentry *dentry;
5827 +
5828 +       AuDebugOn(cpg->bsrc < 0);
5829 +
5830 +       dentry = cpg->dentry;
5831 +       DiMustWriteLock(dentry);
5832 +
5833 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5834 +       if (!err) {
5835 +               flags_orig = cpg->flags;
5836 +               au_fset_cpup(cpg->flags, RENAME);
5837 +               err = au_cpup_single(cpg, NULL);
5838 +               cpg->flags = flags_orig;
5839 +               if (!err)
5840 +                       return 0; /* success */
5841 +
5842 +               /* revert */
5843 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5844 +               au_set_dbtop(dentry, cpg->bsrc);
5845 +       }
5846 +
5847 +       return err;
5848 +}
5849 +
5850 +struct au_cpup_simple_args {
5851 +       int *errp;
5852 +       struct au_cp_generic *cpg;
5853 +};
5854 +
5855 +static void au_call_cpup_simple(void *args)
5856 +{
5857 +       struct au_cpup_simple_args *a = args;
5858 +
5859 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5860 +       *a->errp = au_cpup_simple(a->cpg);
5861 +       au_pin_hdir_release(a->cpg->pin);
5862 +}
5863 +
5864 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5865 +{
5866 +       int err, wkq_err;
5867 +       struct dentry *dentry, *parent;
5868 +       struct file *h_file;
5869 +       struct inode *h_dir;
5870 +       struct mnt_idmap *h_idmap;
5871 +
5872 +       dentry = cpg->dentry;
5873 +       h_file = NULL;
5874 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5875 +               AuDebugOn(cpg->bsrc < 0);
5876 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5877 +               err = PTR_ERR(h_file);
5878 +               if (IS_ERR(h_file))
5879 +                       goto out;
5880 +       }
5881 +
5882 +       parent = dget_parent(dentry);
5883 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5884 +       h_idmap = au_sbr_idmap(dentry->d_sb, cpg->bdst);
5885 +       if (!au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC | MAY_WRITE)
5886 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5887 +               err = au_cpup_simple(cpg);
5888 +       else {
5889 +               struct au_cpup_simple_args args = {
5890 +                       .errp           = &err,
5891 +                       .cpg            = cpg
5892 +               };
5893 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5894 +               if (unlikely(wkq_err))
5895 +                       err = wkq_err;
5896 +       }
5897 +
5898 +       dput(parent);
5899 +       if (h_file)
5900 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5901 +
5902 +out:
5903 +       return err;
5904 +}
5905 +
5906 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5907 +{
5908 +       aufs_bindex_t bsrc, bbot;
5909 +       struct dentry *dentry, *h_dentry;
5910 +
5911 +       if (cpg->bsrc < 0) {
5912 +               dentry = cpg->dentry;
5913 +               bbot = au_dbbot(dentry);
5914 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5915 +                       h_dentry = au_h_dptr(dentry, bsrc);
5916 +                       if (h_dentry) {
5917 +                               AuDebugOn(d_is_negative(h_dentry));
5918 +                               break;
5919 +                       }
5920 +               }
5921 +               AuDebugOn(bsrc > bbot);
5922 +               cpg->bsrc = bsrc;
5923 +       }
5924 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5925 +       return au_do_sio_cpup_simple(cpg);
5926 +}
5927 +
5928 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5929 +{
5930 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5931 +       return au_do_sio_cpup_simple(cpg);
5932 +}
5933 +
5934 +/* ---------------------------------------------------------------------- */
5935 +
5936 +/*
5937 + * copyup the deleted file for writing.
5938 + */
5939 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5940 +                        struct file *file)
5941 +{
5942 +       int err;
5943 +       unsigned int flags_orig;
5944 +       aufs_bindex_t bsrc_orig;
5945 +       struct au_dinfo *dinfo;
5946 +       struct {
5947 +               struct au_hdentry *hd;
5948 +               struct dentry *h_dentry;
5949 +       } hdst, hsrc;
5950 +
5951 +       dinfo = au_di(cpg->dentry);
5952 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5953 +
5954 +       bsrc_orig = cpg->bsrc;
5955 +       cpg->bsrc = dinfo->di_btop;
5956 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5957 +       hdst.h_dentry = hdst.hd->hd_dentry;
5958 +       hdst.hd->hd_dentry = wh_dentry;
5959 +       dinfo->di_btop = cpg->bdst;
5960 +
5961 +       hsrc.h_dentry = NULL;
5962 +       if (file) {
5963 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5964 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5965 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5966 +       }
5967 +       flags_orig = cpg->flags;
5968 +       cpg->flags = !AuCpup_DTIME;
5969 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5970 +       cpg->flags = flags_orig;
5971 +       if (file) {
5972 +               if (!err)
5973 +                       err = au_reopen_nondir(file);
5974 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5975 +       }
5976 +       hdst.hd->hd_dentry = hdst.h_dentry;
5977 +       dinfo->di_btop = cpg->bsrc;
5978 +       cpg->bsrc = bsrc_orig;
5979 +
5980 +       return err;
5981 +}
5982 +
5983 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5984 +{
5985 +       int err;
5986 +       aufs_bindex_t bdst;
5987 +       struct au_dtime dt;
5988 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5989 +       struct au_branch *br;
5990 +       struct path h_path;
5991 +
5992 +       dentry = cpg->dentry;
5993 +       bdst = cpg->bdst;
5994 +       br = au_sbr(dentry->d_sb, bdst);
5995 +       parent = dget_parent(dentry);
5996 +       h_parent = au_h_dptr(parent, bdst);
5997 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5998 +       err = PTR_ERR(wh_dentry);
5999 +       if (IS_ERR(wh_dentry))
6000 +               goto out;
6001 +
6002 +       h_path.dentry = h_parent;
6003 +       h_path.mnt = au_br_mnt(br);
6004 +       au_dtime_store(&dt, parent, &h_path);
6005 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6006 +       if (unlikely(err))
6007 +               goto out_wh;
6008 +
6009 +       dget(wh_dentry);
6010 +       h_path.dentry = wh_dentry;
6011 +       if (!d_is_dir(wh_dentry)) {
6012 +               /* no delegation since it is just created */
6013 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6014 +                                  /*delegated*/NULL, /*force*/0);
6015 +       } else
6016 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6017 +       if (unlikely(err)) {
6018 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6019 +                       wh_dentry, err);
6020 +               err = -EIO;
6021 +       }
6022 +       au_dtime_revert(&dt);
6023 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6024 +
6025 +out_wh:
6026 +       dput(wh_dentry);
6027 +out:
6028 +       dput(parent);
6029 +       return err;
6030 +}
6031 +
6032 +struct au_cpup_wh_args {
6033 +       int *errp;
6034 +       struct au_cp_generic *cpg;
6035 +       struct file *file;
6036 +};
6037 +
6038 +static void au_call_cpup_wh(void *args)
6039 +{
6040 +       struct au_cpup_wh_args *a = args;
6041 +
6042 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6043 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6044 +       au_pin_hdir_release(a->cpg->pin);
6045 +}
6046 +
6047 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6048 +{
6049 +       int err, wkq_err;
6050 +       aufs_bindex_t bdst;
6051 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6052 +       struct inode *dir, *h_dir, *h_tmpdir;
6053 +       struct au_wbr *wbr;
6054 +       struct au_pin wh_pin, *pin_orig;
6055 +       struct mnt_idmap *h_idmap;
6056 +
6057 +       dentry = cpg->dentry;
6058 +       bdst = cpg->bdst;
6059 +       parent = dget_parent(dentry);
6060 +       dir = d_inode(parent);
6061 +       h_orph = NULL;
6062 +       h_parent = NULL;
6063 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6064 +       h_tmpdir = h_dir;
6065 +       pin_orig = NULL;
6066 +       if (!h_dir->i_nlink) {
6067 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6068 +               h_orph = wbr->wbr_orph;
6069 +
6070 +               h_parent = dget(au_h_dptr(parent, bdst));
6071 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6072 +               h_tmpdir = d_inode(h_orph);
6073 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6074 +
6075 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6076 +               /* todo: au_h_open_pre()? */
6077 +
6078 +               pin_orig = cpg->pin;
6079 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6080 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6081 +               cpg->pin = &wh_pin;
6082 +       }
6083 +
6084 +       h_idmap = au_sbr_idmap(dentry->d_sb, bdst);
6085 +       if (!au_test_h_perm_sio(h_idmap, h_tmpdir, MAY_EXEC | MAY_WRITE)
6086 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6087 +               err = au_cpup_wh(cpg, file);
6088 +       else {
6089 +               struct au_cpup_wh_args args = {
6090 +                       .errp   = &err,
6091 +                       .cpg    = cpg,
6092 +                       .file   = file
6093 +               };
6094 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6095 +               if (unlikely(wkq_err))
6096 +                       err = wkq_err;
6097 +       }
6098 +
6099 +       if (h_orph) {
6100 +               inode_unlock(h_tmpdir);
6101 +               /* todo: au_h_open_post()? */
6102 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6103 +               au_set_h_dptr(parent, bdst, h_parent);
6104 +               AuDebugOn(!pin_orig);
6105 +               cpg->pin = pin_orig;
6106 +       }
6107 +       iput(h_dir);
6108 +       dput(parent);
6109 +
6110 +       return err;
6111 +}
6112 +
6113 +/* ---------------------------------------------------------------------- */
6114 +
6115 +/*
6116 + * generic routine for both of copy-up and copy-down.
6117 + */
6118 +/* cf. revalidate function in file.c */
6119 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6120 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6121 +                        struct au_pin *pin,
6122 +                        struct dentry *h_parent, void *arg),
6123 +              void *arg)
6124 +{
6125 +       int err;
6126 +       struct au_pin pin;
6127 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6128 +
6129 +       err = 0;
6130 +       parent = dget_parent(dentry);
6131 +       if (IS_ROOT(parent))
6132 +               goto out;
6133 +
6134 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6135 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6136 +
6137 +       /* do not use au_dpage */
6138 +       real_parent = parent;
6139 +       while (1) {
6140 +               dput(parent);
6141 +               parent = dget_parent(dentry);
6142 +               h_parent = au_h_dptr(parent, bdst);
6143 +               if (h_parent)
6144 +                       goto out; /* success */
6145 +
6146 +               /* find top dir which is necessary to cpup */
6147 +               do {
6148 +                       d = parent;
6149 +                       dput(parent);
6150 +                       parent = dget_parent(d);
6151 +                       di_read_lock_parent3(parent, !AuLock_IR);
6152 +                       h_parent = au_h_dptr(parent, bdst);
6153 +                       di_read_unlock(parent, !AuLock_IR);
6154 +               } while (!h_parent);
6155 +
6156 +               if (d != real_parent)
6157 +                       di_write_lock_child3(d);
6158 +
6159 +               /* somebody else might create while we were sleeping */
6160 +               h_dentry = au_h_dptr(d, bdst);
6161 +               if (!h_dentry || d_is_negative(h_dentry)) {
6162 +                       if (h_dentry)
6163 +                               au_update_dbtop(d);
6164 +
6165 +                       au_pin_set_dentry(&pin, d);
6166 +                       err = au_do_pin(&pin);
6167 +                       if (!err) {
6168 +                               err = cp(d, bdst, &pin, h_parent, arg);
6169 +                               au_unpin(&pin);
6170 +                       }
6171 +               }
6172 +
6173 +               if (d != real_parent)
6174 +                       di_write_unlock(d);
6175 +               if (unlikely(err))
6176 +                       break;
6177 +       }
6178 +
6179 +out:
6180 +       dput(parent);
6181 +       return err;
6182 +}
6183 +
6184 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6185 +                      struct au_pin *pin,
6186 +                      struct dentry *h_parent __maybe_unused,
6187 +                      void *arg __maybe_unused)
6188 +{
6189 +       struct au_cp_generic cpg = {
6190 +               .dentry = dentry,
6191 +               .bdst   = bdst,
6192 +               .bsrc   = -1,
6193 +               .len    = 0,
6194 +               .pin    = pin,
6195 +               .flags  = AuCpup_DTIME
6196 +       };
6197 +       return au_sio_cpup_simple(&cpg);
6198 +}
6199 +
6200 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6201 +{
6202 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6203 +}
6204 +
6205 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6206 +{
6207 +       int err;
6208 +       struct dentry *parent;
6209 +       struct inode *dir;
6210 +
6211 +       parent = dget_parent(dentry);
6212 +       dir = d_inode(parent);
6213 +       err = 0;
6214 +       if (au_h_iptr(dir, bdst))
6215 +               goto out;
6216 +
6217 +       di_read_unlock(parent, AuLock_IR);
6218 +       di_write_lock_parent(parent);
6219 +       /* someone else might change our inode while we were sleeping */
6220 +       if (!au_h_iptr(dir, bdst))
6221 +               err = au_cpup_dirs(dentry, bdst);
6222 +       di_downgrade_lock(parent, AuLock_IR);
6223 +
6224 +out:
6225 +       dput(parent);
6226 +       return err;
6227 +}
6228 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6229 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6230 +++ linux/fs/aufs/cpup.h        2022-11-05 23:02:18.962555950 +0100
6231 @@ -0,0 +1,100 @@
6232 +/* SPDX-License-Identifier: GPL-2.0 */
6233 +/*
6234 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6235 + *
6236 + * This program is free software; you can redistribute it and/or modify
6237 + * it under the terms of the GNU General Public License as published by
6238 + * the Free Software Foundation; either version 2 of the License, or
6239 + * (at your option) any later version.
6240 + *
6241 + * This program is distributed in the hope that it will be useful,
6242 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6243 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6244 + * GNU General Public License for more details.
6245 + *
6246 + * You should have received a copy of the GNU General Public License
6247 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6248 + */
6249 +
6250 +/*
6251 + * copy-up/down functions
6252 + */
6253 +
6254 +#ifndef __AUFS_CPUP_H__
6255 +#define __AUFS_CPUP_H__
6256 +
6257 +#ifdef __KERNEL__
6258 +
6259 +#include <linux/path.h>
6260 +
6261 +struct inode;
6262 +struct file;
6263 +struct au_pin;
6264 +
6265 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6266 +void au_cpup_attr_timesizes(struct inode *inode);
6267 +void au_cpup_attr_nlink(struct inode *inode, int force);
6268 +void au_cpup_attr_changeable(struct inode *inode);
6269 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6270 +void au_cpup_attr_all(struct inode *inode, int force);
6271 +
6272 +/* ---------------------------------------------------------------------- */
6273 +
6274 +struct au_cp_generic {
6275 +       struct dentry   *dentry;
6276 +       aufs_bindex_t   bdst, bsrc;
6277 +       loff_t          len;
6278 +       struct au_pin   *pin;
6279 +       unsigned int    flags;
6280 +};
6281 +
6282 +/* cpup flags */
6283 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6284 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6285 +                                                  for link(2) */
6286 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6287 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6288 +                                                  cpup */
6289 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6290 +                                                  existing entry */
6291 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6292 +                                                  the branch is marked as RO */
6293 +
6294 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6295 +#undef AuCpup_HOPEN
6296 +#define AuCpup_HOPEN           0
6297 +#endif
6298 +
6299 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6300 +#define au_fset_cpup(flags, name) \
6301 +       do { (flags) |= AuCpup_##name; } while (0)
6302 +#define au_fclr_cpup(flags, name) \
6303 +       do { (flags) &= ~AuCpup_##name; } while (0)
6304 +
6305 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6306 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6307 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6308 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6309 +
6310 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6311 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6312 +                        struct au_pin *pin,
6313 +                        struct dentry *h_parent, void *arg),
6314 +              void *arg);
6315 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6316 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6317 +
6318 +/* ---------------------------------------------------------------------- */
6319 +
6320 +/* keep timestamps when copyup */
6321 +struct au_dtime {
6322 +       struct dentry *dt_dentry;
6323 +       struct path dt_h_path;
6324 +       struct timespec64 dt_atime, dt_mtime;
6325 +};
6326 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6327 +                   struct path *h_path);
6328 +void au_dtime_revert(struct au_dtime *dt);
6329 +
6330 +#endif /* __KERNEL__ */
6331 +#endif /* __AUFS_CPUP_H__ */
6332 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6333 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6334 +++ linux/fs/aufs/dbgaufs.c     2022-11-05 23:02:18.962555950 +0100
6335 @@ -0,0 +1,526 @@
6336 +// SPDX-License-Identifier: GPL-2.0
6337 +/*
6338 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6339 + *
6340 + * This program is free software; you can redistribute it and/or modify
6341 + * it under the terms of the GNU General Public License as published by
6342 + * the Free Software Foundation; either version 2 of the License, or
6343 + * (at your option) any later version.
6344 + *
6345 + * This program is distributed in the hope that it will be useful,
6346 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6347 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6348 + * GNU General Public License for more details.
6349 + *
6350 + * You should have received a copy of the GNU General Public License
6351 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6352 + */
6353 +
6354 +/*
6355 + * debugfs interface
6356 + */
6357 +
6358 +#include <linux/debugfs.h>
6359 +#include "aufs.h"
6360 +
6361 +#ifndef CONFIG_SYSFS
6362 +#error DEBUG_FS depends upon SYSFS
6363 +#endif
6364 +
6365 +static struct dentry *dbgaufs;
6366 +static const mode_t dbgaufs_mode = 0444;
6367 +
6368 +/* 20 is max digits length of ulong 64 */
6369 +struct dbgaufs_arg {
6370 +       int n;
6371 +       char a[20 * 4];
6372 +};
6373 +
6374 +/*
6375 + * common function for all XINO files
6376 + */
6377 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6378 +                             struct file *file)
6379 +{
6380 +       void *p;
6381 +
6382 +       p = file->private_data;
6383 +       if (p) {
6384 +               /* this is struct dbgaufs_arg */
6385 +               AuDebugOn(!au_kfree_sz_test(p));
6386 +               au_kfree_do_rcu(p);
6387 +       }
6388 +       return 0;
6389 +}
6390 +
6391 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6392 +                          int cnt)
6393 +{
6394 +       int err;
6395 +       struct kstat st;
6396 +       struct dbgaufs_arg *p;
6397 +
6398 +       err = -ENOMEM;
6399 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6400 +       if (unlikely(!p))
6401 +               goto out;
6402 +
6403 +       err = 0;
6404 +       p->n = 0;
6405 +       file->private_data = p;
6406 +       if (!xf)
6407 +               goto out;
6408 +
6409 +       err = vfsub_getattr(&xf->f_path, &st);
6410 +       if (!err) {
6411 +               if (do_fcnt)
6412 +                       p->n = snprintf
6413 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6414 +                                cnt, st.blocks, st.blksize,
6415 +                                (long long)st.size);
6416 +               else
6417 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6418 +                                       st.blocks, st.blksize,
6419 +                                       (long long)st.size);
6420 +               AuDebugOn(p->n >= sizeof(p->a));
6421 +       } else {
6422 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6423 +               err = 0;
6424 +       }
6425 +
6426 +out:
6427 +       return err;
6428 +}
6429 +
6430 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6431 +                              size_t count, loff_t *ppos)
6432 +{
6433 +       struct dbgaufs_arg *p;
6434 +
6435 +       p = file->private_data;
6436 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6437 +}
6438 +
6439 +/* ---------------------------------------------------------------------- */
6440 +
6441 +struct dbgaufs_plink_arg {
6442 +       int n;
6443 +       char a[];
6444 +};
6445 +
6446 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6447 +                                struct file *file)
6448 +{
6449 +       free_page((unsigned long)file->private_data);
6450 +       return 0;
6451 +}
6452 +
6453 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6454 +{
6455 +       int err, i, limit;
6456 +       unsigned long n, sum;
6457 +       struct dbgaufs_plink_arg *p;
6458 +       struct au_sbinfo *sbinfo;
6459 +       struct super_block *sb;
6460 +       struct hlist_bl_head *hbl;
6461 +
6462 +       err = -ENOMEM;
6463 +       p = (void *)get_zeroed_page(GFP_NOFS);
6464 +       if (unlikely(!p))
6465 +               goto out;
6466 +
6467 +       err = -EFBIG;
6468 +       sbinfo = inode->i_private;
6469 +       sb = sbinfo->si_sb;
6470 +       si_noflush_read_lock(sb);
6471 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6472 +               limit = PAGE_SIZE - sizeof(p->n);
6473 +
6474 +               /* the number of buckets */
6475 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6476 +               p->n += n;
6477 +               limit -= n;
6478 +
6479 +               sum = 0;
6480 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6481 +                    i++, hbl++) {
6482 +                       n = au_hbl_count(hbl);
6483 +                       sum += n;
6484 +
6485 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6486 +                       p->n += n;
6487 +                       limit -= n;
6488 +                       if (unlikely(limit <= 0))
6489 +                               goto out_free;
6490 +               }
6491 +               p->a[p->n - 1] = '\n';
6492 +
6493 +               /* the sum of plinks */
6494 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6495 +               p->n += n;
6496 +               limit -= n;
6497 +               if (unlikely(limit <= 0))
6498 +                       goto out_free;
6499 +       } else {
6500 +#define str "1\n0\n0\n"
6501 +               p->n = sizeof(str) - 1;
6502 +               strcpy(p->a, str);
6503 +#undef str
6504 +       }
6505 +       si_read_unlock(sb);
6506 +
6507 +       err = 0;
6508 +       file->private_data = p;
6509 +       goto out; /* success */
6510 +
6511 +out_free:
6512 +       free_page((unsigned long)p);
6513 +out:
6514 +       return err;
6515 +}
6516 +
6517 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6518 +                                 size_t count, loff_t *ppos)
6519 +{
6520 +       struct dbgaufs_plink_arg *p;
6521 +
6522 +       p = file->private_data;
6523 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6524 +}
6525 +
6526 +static const struct file_operations dbgaufs_plink_fop = {
6527 +       .owner          = THIS_MODULE,
6528 +       .open           = dbgaufs_plink_open,
6529 +       .release        = dbgaufs_plink_release,
6530 +       .read           = dbgaufs_plink_read
6531 +};
6532 +
6533 +/* ---------------------------------------------------------------------- */
6534 +
6535 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6536 +{
6537 +       int err;
6538 +       struct au_sbinfo *sbinfo;
6539 +       struct super_block *sb;
6540 +
6541 +       sbinfo = inode->i_private;
6542 +       sb = sbinfo->si_sb;
6543 +       si_noflush_read_lock(sb);
6544 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6545 +       si_read_unlock(sb);
6546 +       return err;
6547 +}
6548 +
6549 +static const struct file_operations dbgaufs_xib_fop = {
6550 +       .owner          = THIS_MODULE,
6551 +       .open           = dbgaufs_xib_open,
6552 +       .release        = dbgaufs_xi_release,
6553 +       .read           = dbgaufs_xi_read
6554 +};
6555 +
6556 +/* ---------------------------------------------------------------------- */
6557 +
6558 +#define DbgaufsXi_PREFIX "xi"
6559 +
6560 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6561 +{
6562 +       int err, idx;
6563 +       long l;
6564 +       aufs_bindex_t bindex;
6565 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6566 +       struct au_sbinfo *sbinfo;
6567 +       struct super_block *sb;
6568 +       struct au_xino *xi;
6569 +       struct file *xf;
6570 +       struct qstr *name;
6571 +       struct au_branch *br;
6572 +
6573 +       err = -ENOENT;
6574 +       name = &file->f_path.dentry->d_name;
6575 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6576 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6577 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6578 +               goto out;
6579 +
6580 +       AuDebugOn(name->len >= sizeof(a));
6581 +       memcpy(a, name->name, name->len);
6582 +       a[name->len] = '\0';
6583 +       p = strchr(a, '-');
6584 +       if (p)
6585 +               *p = '\0';
6586 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6587 +       if (unlikely(err))
6588 +               goto out;
6589 +       bindex = l;
6590 +       idx = 0;
6591 +       if (p) {
6592 +               err = kstrtol(p + 1, 10, &l);
6593 +               if (unlikely(err))
6594 +                       goto out;
6595 +               idx = l;
6596 +       }
6597 +
6598 +       err = -ENOENT;
6599 +       sbinfo = inode->i_private;
6600 +       sb = sbinfo->si_sb;
6601 +       si_noflush_read_lock(sb);
6602 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6603 +               goto out_si;
6604 +       br = au_sbr(sb, bindex);
6605 +       xi = br->br_xino;
6606 +       if (unlikely(idx >= xi->xi_nfile))
6607 +               goto out_si;
6608 +       xf = au_xino_file(xi, idx);
6609 +       if (xf)
6610 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6611 +                                     au_xino_count(br));
6612 +
6613 +out_si:
6614 +       si_read_unlock(sb);
6615 +out:
6616 +       AuTraceErr(err);
6617 +       return err;
6618 +}
6619 +
6620 +static const struct file_operations dbgaufs_xino_fop = {
6621 +       .owner          = THIS_MODULE,
6622 +       .open           = dbgaufs_xino_open,
6623 +       .release        = dbgaufs_xi_release,
6624 +       .read           = dbgaufs_xi_read
6625 +};
6626 +
6627 +void dbgaufs_xino_del(struct au_branch *br)
6628 +{
6629 +       struct dentry *dbgaufs;
6630 +
6631 +       dbgaufs = br->br_dbgaufs;
6632 +       if (!dbgaufs)
6633 +               return;
6634 +
6635 +       br->br_dbgaufs = NULL;
6636 +       /* debugfs acquires the parent i_mutex */
6637 +       lockdep_off();
6638 +       debugfs_remove(dbgaufs);
6639 +       lockdep_on();
6640 +}
6641 +
6642 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6643 +{
6644 +       aufs_bindex_t bbot;
6645 +       struct au_branch *br;
6646 +
6647 +       if (!au_sbi(sb)->si_dbgaufs)
6648 +               return;
6649 +
6650 +       bbot = au_sbbot(sb);
6651 +       for (; bindex <= bbot; bindex++) {
6652 +               br = au_sbr(sb, bindex);
6653 +               dbgaufs_xino_del(br);
6654 +       }
6655 +}
6656 +
6657 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6658 +                             unsigned int idx, struct dentry *parent,
6659 +                             struct au_sbinfo *sbinfo)
6660 +{
6661 +       struct au_branch *br;
6662 +       struct dentry *d;
6663 +       /* "xi" bindex(5) "-" idx(2) NULL */
6664 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6665 +
6666 +       if (!idx)
6667 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6668 +       else
6669 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6670 +                        bindex, idx);
6671 +       br = au_sbr(sb, bindex);
6672 +       if (br->br_dbgaufs) {
6673 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6674 +
6675 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6676 +                       /* debugfs acquires the parent i_mutex */
6677 +                       lockdep_off();
6678 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6679 +                                          name);
6680 +                       lockdep_on();
6681 +                       if (unlikely(!d))
6682 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6683 +                                       parent, name);
6684 +               }
6685 +       } else {
6686 +               lockdep_off();
6687 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6688 +                                                    sbinfo, &dbgaufs_xino_fop);
6689 +               lockdep_on();
6690 +               if (unlikely(!br->br_dbgaufs))
6691 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6692 +                               parent, name);
6693 +       }
6694 +}
6695 +
6696 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6697 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6698 +{
6699 +       struct au_branch *br;
6700 +       struct au_xino *xi;
6701 +       unsigned int u;
6702 +
6703 +       br = au_sbr(sb, bindex);
6704 +       xi = br->br_xino;
6705 +       for (u = 0; u < xi->xi_nfile; u++)
6706 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6707 +}
6708 +
6709 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6710 +{
6711 +       struct au_sbinfo *sbinfo;
6712 +       struct dentry *parent;
6713 +       aufs_bindex_t bbot;
6714 +
6715 +       if (!au_opt_test(au_mntflags(sb), XINO))
6716 +               return;
6717 +
6718 +       sbinfo = au_sbi(sb);
6719 +       parent = sbinfo->si_dbgaufs;
6720 +       if (!parent)
6721 +               return;
6722 +
6723 +       bbot = au_sbbot(sb);
6724 +       if (topdown)
6725 +               for (; bindex <= bbot; bindex++)
6726 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6727 +       else
6728 +               for (; bbot >= bindex; bbot--)
6729 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6730 +}
6731 +
6732 +/* ---------------------------------------------------------------------- */
6733 +
6734 +#ifdef CONFIG_AUFS_EXPORT
6735 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6736 +{
6737 +       int err;
6738 +       struct au_sbinfo *sbinfo;
6739 +       struct super_block *sb;
6740 +
6741 +       sbinfo = inode->i_private;
6742 +       sb = sbinfo->si_sb;
6743 +       si_noflush_read_lock(sb);
6744 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6745 +       si_read_unlock(sb);
6746 +       return err;
6747 +}
6748 +
6749 +static const struct file_operations dbgaufs_xigen_fop = {
6750 +       .owner          = THIS_MODULE,
6751 +       .open           = dbgaufs_xigen_open,
6752 +       .release        = dbgaufs_xi_release,
6753 +       .read           = dbgaufs_xi_read
6754 +};
6755 +
6756 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6757 +{
6758 +       int err;
6759 +
6760 +       /*
6761 +        * This function is a dynamic '__init' function actually,
6762 +        * so the tiny check for si_rwsem is unnecessary.
6763 +        */
6764 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6765 +
6766 +       err = -EIO;
6767 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6768 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6769 +                &dbgaufs_xigen_fop);
6770 +       if (sbinfo->si_dbgaufs_xigen)
6771 +               err = 0;
6772 +
6773 +       return err;
6774 +}
6775 +#else
6776 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6777 +{
6778 +       return 0;
6779 +}
6780 +#endif /* CONFIG_AUFS_EXPORT */
6781 +
6782 +/* ---------------------------------------------------------------------- */
6783 +
6784 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6785 +{
6786 +       /*
6787 +        * This function is a dynamic '__fin' function actually,
6788 +        * so the tiny check for si_rwsem is unnecessary.
6789 +        */
6790 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6791 +
6792 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6793 +       sbinfo->si_dbgaufs = NULL;
6794 +}
6795 +
6796 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6797 +{
6798 +       int err;
6799 +       char name[SysaufsSiNameLen];
6800 +
6801 +       /*
6802 +        * This function is a dynamic '__init' function actually,
6803 +        * so the tiny check for si_rwsem is unnecessary.
6804 +        */
6805 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6806 +
6807 +       err = -ENOENT;
6808 +       if (!dbgaufs) {
6809 +               AuErr1("/debug/aufs is uninitialized\n");
6810 +               goto out;
6811 +       }
6812 +
6813 +       err = -EIO;
6814 +       sysaufs_name(sbinfo, name);
6815 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6816 +       if (unlikely(!sbinfo->si_dbgaufs))
6817 +               goto out;
6818 +
6819 +       /* regardless plink/noplink option */
6820 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6821 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6822 +                &dbgaufs_plink_fop);
6823 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6824 +               goto out_dir;
6825 +
6826 +       /* regardless xino/noxino option */
6827 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6828 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6829 +                &dbgaufs_xib_fop);
6830 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6831 +               goto out_dir;
6832 +
6833 +       err = dbgaufs_xigen_init(sbinfo);
6834 +       if (!err)
6835 +               goto out; /* success */
6836 +
6837 +out_dir:
6838 +       dbgaufs_si_fin(sbinfo);
6839 +out:
6840 +       if (unlikely(err))
6841 +               pr_err("debugfs/aufs failed\n");
6842 +       return err;
6843 +}
6844 +
6845 +/* ---------------------------------------------------------------------- */
6846 +
6847 +void dbgaufs_fin(void)
6848 +{
6849 +       debugfs_remove(dbgaufs);
6850 +}
6851 +
6852 +int __init dbgaufs_init(void)
6853 +{
6854 +       int err;
6855 +
6856 +       err = -EIO;
6857 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6858 +       if (dbgaufs)
6859 +               err = 0;
6860 +       return err;
6861 +}
6862 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6863 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6864 +++ linux/fs/aufs/dbgaufs.h     2022-11-05 23:02:18.962555950 +0100
6865 @@ -0,0 +1,53 @@
6866 +/* SPDX-License-Identifier: GPL-2.0 */
6867 +/*
6868 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6869 + *
6870 + * This program is free software; you can redistribute it and/or modify
6871 + * it under the terms of the GNU General Public License as published by
6872 + * the Free Software Foundation; either version 2 of the License, or
6873 + * (at your option) any later version.
6874 + *
6875 + * This program is distributed in the hope that it will be useful,
6876 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6877 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6878 + * GNU General Public License for more details.
6879 + *
6880 + * You should have received a copy of the GNU General Public License
6881 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6882 + */
6883 +
6884 +/*
6885 + * debugfs interface
6886 + */
6887 +
6888 +#ifndef __DBGAUFS_H__
6889 +#define __DBGAUFS_H__
6890 +
6891 +#ifdef __KERNEL__
6892 +
6893 +struct super_block;
6894 +struct au_sbinfo;
6895 +struct au_branch;
6896 +
6897 +#ifdef CONFIG_DEBUG_FS
6898 +/* dbgaufs.c */
6899 +void dbgaufs_xino_del(struct au_branch *br);
6900 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6901 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6902 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6903 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6904 +void dbgaufs_fin(void);
6905 +int __init dbgaufs_init(void);
6906 +#else
6907 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6908 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6909 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6910 +          int topdown)
6911 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6912 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6913 +AuStubVoid(dbgaufs_fin, void)
6914 +AuStubInt0(__init dbgaufs_init, void)
6915 +#endif /* CONFIG_DEBUG_FS */
6916 +
6917 +#endif /* __KERNEL__ */
6918 +#endif /* __DBGAUFS_H__ */
6919 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6920 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6921 +++ linux/fs/aufs/dcsub.c       2022-11-05 23:02:18.962555950 +0100
6922 @@ -0,0 +1,225 @@
6923 +// SPDX-License-Identifier: GPL-2.0
6924 +/*
6925 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6926 + *
6927 + * This program is free software; you can redistribute it and/or modify
6928 + * it under the terms of the GNU General Public License as published by
6929 + * the Free Software Foundation; either version 2 of the License, or
6930 + * (at your option) any later version.
6931 + *
6932 + * This program is distributed in the hope that it will be useful,
6933 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6934 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6935 + * GNU General Public License for more details.
6936 + *
6937 + * You should have received a copy of the GNU General Public License
6938 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6939 + */
6940 +
6941 +/*
6942 + * sub-routines for dentry cache
6943 + */
6944 +
6945 +#include "aufs.h"
6946 +
6947 +static void au_dpage_free(struct au_dpage *dpage)
6948 +{
6949 +       int i;
6950 +       struct dentry **p;
6951 +
6952 +       p = dpage->dentries;
6953 +       for (i = 0; i < dpage->ndentry; i++)
6954 +               dput(*p++);
6955 +       free_page((unsigned long)dpage->dentries);
6956 +}
6957 +
6958 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6959 +{
6960 +       int err;
6961 +       void *p;
6962 +
6963 +       err = -ENOMEM;
6964 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6965 +       if (unlikely(!dpages->dpages))
6966 +               goto out;
6967 +
6968 +       p = (void *)__get_free_page(gfp);
6969 +       if (unlikely(!p))
6970 +               goto out_dpages;
6971 +
6972 +       dpages->dpages[0].ndentry = 0;
6973 +       dpages->dpages[0].dentries = p;
6974 +       dpages->ndpage = 1;
6975 +       return 0; /* success */
6976 +
6977 +out_dpages:
6978 +       au_kfree_try_rcu(dpages->dpages);
6979 +out:
6980 +       return err;
6981 +}
6982 +
6983 +void au_dpages_free(struct au_dcsub_pages *dpages)
6984 +{
6985 +       int i;
6986 +       struct au_dpage *p;
6987 +
6988 +       p = dpages->dpages;
6989 +       for (i = 0; i < dpages->ndpage; i++)
6990 +               au_dpage_free(p++);
6991 +       au_kfree_try_rcu(dpages->dpages);
6992 +}
6993 +
6994 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6995 +                           struct dentry *dentry, gfp_t gfp)
6996 +{
6997 +       int err, sz;
6998 +       struct au_dpage *dpage;
6999 +       void *p;
7000 +
7001 +       dpage = dpages->dpages + dpages->ndpage - 1;
7002 +       sz = PAGE_SIZE / sizeof(dentry);
7003 +       if (unlikely(dpage->ndentry >= sz)) {
7004 +               AuLabel(new dpage);
7005 +               err = -ENOMEM;
7006 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7007 +               p = au_kzrealloc(dpages->dpages, sz,
7008 +                                sz + sizeof(*dpages->dpages), gfp,
7009 +                                /*may_shrink*/0);
7010 +               if (unlikely(!p))
7011 +                       goto out;
7012 +
7013 +               dpages->dpages = p;
7014 +               dpage = dpages->dpages + dpages->ndpage;
7015 +               p = (void *)__get_free_page(gfp);
7016 +               if (unlikely(!p))
7017 +                       goto out;
7018 +
7019 +               dpage->ndentry = 0;
7020 +               dpage->dentries = p;
7021 +               dpages->ndpage++;
7022 +       }
7023 +
7024 +       AuDebugOn(au_dcount(dentry) <= 0);
7025 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7026 +       return 0; /* success */
7027 +
7028 +out:
7029 +       return err;
7030 +}
7031 +
7032 +/* todo: BAD approach */
7033 +/* copied from linux/fs/dcache.c */
7034 +enum d_walk_ret {
7035 +       D_WALK_CONTINUE,
7036 +       D_WALK_QUIT,
7037 +       D_WALK_NORETRY,
7038 +       D_WALK_SKIP,
7039 +};
7040 +
7041 +extern void d_walk(struct dentry *parent, void *data,
7042 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7043 +
7044 +struct ac_dpages_arg {
7045 +       int err;
7046 +       struct au_dcsub_pages *dpages;
7047 +       struct super_block *sb;
7048 +       au_dpages_test test;
7049 +       void *arg;
7050 +};
7051 +
7052 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7053 +{
7054 +       enum d_walk_ret ret;
7055 +       struct ac_dpages_arg *arg = _arg;
7056 +
7057 +       ret = D_WALK_CONTINUE;
7058 +       if (dentry->d_sb == arg->sb
7059 +           && !IS_ROOT(dentry)
7060 +           && au_dcount(dentry) > 0
7061 +           && au_di(dentry)
7062 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7063 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7064 +               if (unlikely(arg->err))
7065 +                       ret = D_WALK_QUIT;
7066 +       }
7067 +
7068 +       return ret;
7069 +}
7070 +
7071 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7072 +                  au_dpages_test test, void *arg)
7073 +{
7074 +       struct ac_dpages_arg args = {
7075 +               .err    = 0,
7076 +               .dpages = dpages,
7077 +               .sb     = root->d_sb,
7078 +               .test   = test,
7079 +               .arg    = arg
7080 +       };
7081 +
7082 +       d_walk(root, &args, au_call_dpages_append);
7083 +
7084 +       return args.err;
7085 +}
7086 +
7087 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7088 +                      int do_include, au_dpages_test test, void *arg)
7089 +{
7090 +       int err;
7091 +
7092 +       err = 0;
7093 +       write_seqlock(&rename_lock);
7094 +       spin_lock(&dentry->d_lock);
7095 +       if (do_include
7096 +           && au_dcount(dentry) > 0
7097 +           && (!test || test(dentry, arg)))
7098 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7099 +       spin_unlock(&dentry->d_lock);
7100 +       if (unlikely(err))
7101 +               goto out;
7102 +
7103 +       /*
7104 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7105 +        * mount
7106 +        */
7107 +       while (!IS_ROOT(dentry)) {
7108 +               dentry = dentry->d_parent; /* rename_lock is locked */
7109 +               spin_lock(&dentry->d_lock);
7110 +               if (au_dcount(dentry) > 0
7111 +                   && (!test || test(dentry, arg)))
7112 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7113 +               spin_unlock(&dentry->d_lock);
7114 +               if (unlikely(err))
7115 +                       break;
7116 +       }
7117 +
7118 +out:
7119 +       write_sequnlock(&rename_lock);
7120 +       return err;
7121 +}
7122 +
7123 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7124 +{
7125 +       return au_di(dentry) && dentry->d_sb == arg;
7126 +}
7127 +
7128 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7129 +                           struct dentry *dentry, int do_include)
7130 +{
7131 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7132 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7133 +}
7134 +
7135 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7136 +{
7137 +       struct path path[2] = {
7138 +               {
7139 +                       .dentry = d1
7140 +               },
7141 +               {
7142 +                       .dentry = d2
7143 +               }
7144 +       };
7145 +
7146 +       return path_is_under(path + 0, path + 1);
7147 +}
7148 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7149 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7150 +++ linux/fs/aufs/dcsub.h       2022-11-05 23:02:18.962555950 +0100
7151 @@ -0,0 +1,137 @@
7152 +/* SPDX-License-Identifier: GPL-2.0 */
7153 +/*
7154 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7155 + *
7156 + * This program is free software; you can redistribute it and/or modify
7157 + * it under the terms of the GNU General Public License as published by
7158 + * the Free Software Foundation; either version 2 of the License, or
7159 + * (at your option) any later version.
7160 + *
7161 + * This program is distributed in the hope that it will be useful,
7162 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7163 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7164 + * GNU General Public License for more details.
7165 + *
7166 + * You should have received a copy of the GNU General Public License
7167 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7168 + */
7169 +
7170 +/*
7171 + * sub-routines for dentry cache
7172 + */
7173 +
7174 +#ifndef __AUFS_DCSUB_H__
7175 +#define __AUFS_DCSUB_H__
7176 +
7177 +#ifdef __KERNEL__
7178 +
7179 +#include <linux/dcache.h>
7180 +#include <linux/fs.h>
7181 +
7182 +struct au_dpage {
7183 +       int ndentry;
7184 +       struct dentry **dentries;
7185 +};
7186 +
7187 +struct au_dcsub_pages {
7188 +       int ndpage;
7189 +       struct au_dpage *dpages;
7190 +};
7191 +
7192 +/* ---------------------------------------------------------------------- */
7193 +
7194 +/* dcsub.c */
7195 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7196 +void au_dpages_free(struct au_dcsub_pages *dpages);
7197 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7198 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7199 +                  au_dpages_test test, void *arg);
7200 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7201 +                      int do_include, au_dpages_test test, void *arg);
7202 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7203 +                           struct dentry *dentry, int do_include);
7204 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7205 +
7206 +/* ---------------------------------------------------------------------- */
7207 +
7208 +/*
7209 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7210 + * include/linux/dcache.h. Try them (in the future).
7211 + */
7212 +
7213 +static inline int au_d_hashed_positive(struct dentry *d)
7214 +{
7215 +       int err;
7216 +       struct inode *inode = d_inode(d);
7217 +
7218 +       err = 0;
7219 +       if (unlikely(d_unhashed(d)
7220 +                    || d_is_negative(d)
7221 +                    || !inode->i_nlink))
7222 +               err = -ENOENT;
7223 +       return err;
7224 +}
7225 +
7226 +static inline int au_d_linkable(struct dentry *d)
7227 +{
7228 +       int err;
7229 +       struct inode *inode = d_inode(d);
7230 +
7231 +       err = au_d_hashed_positive(d);
7232 +       if (err
7233 +           && d_is_positive(d)
7234 +           && (inode->i_state & I_LINKABLE))
7235 +               err = 0;
7236 +       return err;
7237 +}
7238 +
7239 +static inline int au_d_alive(struct dentry *d)
7240 +{
7241 +       int err;
7242 +       struct inode *inode;
7243 +
7244 +       err = 0;
7245 +       if (!IS_ROOT(d))
7246 +               err = au_d_hashed_positive(d);
7247 +       else {
7248 +               inode = d_inode(d);
7249 +               if (unlikely(d_unlinked(d)
7250 +                            || d_is_negative(d)
7251 +                            || !inode->i_nlink))
7252 +                       err = -ENOENT;
7253 +       }
7254 +       return err;
7255 +}
7256 +
7257 +static inline int au_alive_dir(struct dentry *d)
7258 +{
7259 +       int err;
7260 +
7261 +       err = au_d_alive(d);
7262 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7263 +               err = -ENOENT;
7264 +       return err;
7265 +}
7266 +
7267 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7268 +{
7269 +       return a->len == b->len
7270 +               && !memcmp(a->name, b->name, a->len);
7271 +}
7272 +
7273 +/*
7274 + * by the commit
7275 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7276 + *                     taking d_lock
7277 + * the type of d_lockref.count became int, but the inlined function d_count()
7278 + * still returns unsigned int.
7279 + * I don't know why. Maybe it is for every d_count() users?
7280 + * Anyway au_dcount() lives on.
7281 + */
7282 +static inline int au_dcount(struct dentry *d)
7283 +{
7284 +       return (int)d_count(d);
7285 +}
7286 +
7287 +#endif /* __KERNEL__ */
7288 +#endif /* __AUFS_DCSUB_H__ */
7289 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7290 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7291 +++ linux/fs/aufs/debug.c       2023-08-28 12:34:39.956636132 +0200
7292 @@ -0,0 +1,446 @@
7293 +// SPDX-License-Identifier: GPL-2.0
7294 +/*
7295 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7296 + *
7297 + * This program is free software; you can redistribute it and/or modify
7298 + * it under the terms of the GNU General Public License as published by
7299 + * the Free Software Foundation; either version 2 of the License, or
7300 + * (at your option) any later version.
7301 + *
7302 + * This program is distributed in the hope that it will be useful,
7303 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7304 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7305 + * GNU General Public License for more details.
7306 + *
7307 + * You should have received a copy of the GNU General Public License
7308 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7309 + */
7310 +
7311 +/*
7312 + * debug print functions
7313 + */
7314 +
7315 +#include <linux/iversion.h>
7316 +#include "aufs.h"
7317 +
7318 +/* Returns 0, or -errno.  arg is in kp->arg. */
7319 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7320 +{
7321 +       int err, n;
7322 +
7323 +       err = kstrtoint(val, 0, &n);
7324 +       if (!err) {
7325 +               if (n > 0)
7326 +                       au_debug_on();
7327 +               else
7328 +                       au_debug_off();
7329 +       }
7330 +       return err;
7331 +}
7332 +
7333 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7334 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7335 +{
7336 +       atomic_t *a;
7337 +
7338 +       a = kp->arg;
7339 +       return sprintf(buffer, "%d", atomic_read(a));
7340 +}
7341 +
7342 +static const struct kernel_param_ops param_ops_atomic_t = {
7343 +       .set = param_atomic_t_set,
7344 +       .get = param_atomic_t_get
7345 +       /* void (*free)(void *arg) */
7346 +};
7347 +
7348 +atomic_t aufs_debug = ATOMIC_INIT(0);
7349 +MODULE_PARM_DESC(debug, "debug print");
7350 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7351 +
7352 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7353 +char *au_plevel = KERN_DEBUG;
7354 +#define dpri(fmt, ...) do {                                    \
7355 +       if ((au_plevel                                          \
7356 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7357 +           || au_debug_test())                                 \
7358 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7359 +} while (0)
7360 +
7361 +/* ---------------------------------------------------------------------- */
7362 +
7363 +void au_dpri_whlist(struct au_nhash *whlist)
7364 +{
7365 +       unsigned long ul, n;
7366 +       struct hlist_head *head;
7367 +       struct au_vdir_wh *pos;
7368 +
7369 +       n = whlist->nh_num;
7370 +       head = whlist->nh_head;
7371 +       for (ul = 0; ul < n; ul++) {
7372 +               hlist_for_each_entry(pos, head, wh_hash)
7373 +                       dpri("b%d, %.*s, %d\n",
7374 +                            pos->wh_bindex,
7375 +                            pos->wh_str.len, pos->wh_str.name,
7376 +                            pos->wh_str.len);
7377 +               head++;
7378 +       }
7379 +}
7380 +
7381 +void au_dpri_vdir(struct au_vdir *vdir)
7382 +{
7383 +       unsigned long ul;
7384 +       union au_vdir_deblk_p p;
7385 +       unsigned char *o;
7386 +
7387 +       if (!vdir || IS_ERR(vdir)) {
7388 +               dpri("err %ld\n", PTR_ERR(vdir));
7389 +               return;
7390 +       }
7391 +
7392 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7393 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7394 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7395 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7396 +               p.deblk = vdir->vd_deblk[ul];
7397 +               o = p.deblk;
7398 +               dpri("[%lu]: %p\n", ul, o);
7399 +       }
7400 +}
7401 +
7402 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7403 +                       struct dentry *wh)
7404 +{
7405 +       char *n = NULL;
7406 +       int l = 0;
7407 +
7408 +       if (!inode || IS_ERR(inode)) {
7409 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7410 +               return -1;
7411 +       }
7412 +
7413 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7414 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7415 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7416 +       if (wh) {
7417 +               n = (void *)wh->d_name.name;
7418 +               l = wh->d_name.len;
7419 +       }
7420 +
7421 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7422 +            " acl %p, def_acl %p,"
7423 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7424 +            bindex, inode,
7425 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7426 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7427 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7428 +            inode->i_acl, inode->i_default_acl,
7429 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7430 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7431 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7432 +            inode->i_generation,
7433 +            l ? ", wh " : "", l, n);
7434 +       return 0;
7435 +}
7436 +
7437 +void au_dpri_inode(struct inode *inode)
7438 +{
7439 +       struct au_iinfo *iinfo;
7440 +       struct au_hinode *hi;
7441 +       aufs_bindex_t bindex;
7442 +       int err, hn;
7443 +
7444 +       err = do_pri_inode(-1, inode, -1, NULL);
7445 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7446 +               return;
7447 +
7448 +       iinfo = au_ii(inode);
7449 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7450 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7451 +       if (iinfo->ii_btop < 0)
7452 +               return;
7453 +       hn = 0;
7454 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7455 +               hi = au_hinode(iinfo, bindex);
7456 +               hn = !!au_hn(hi);
7457 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7458 +       }
7459 +}
7460 +
7461 +void au_dpri_dalias(struct inode *inode)
7462 +{
7463 +       struct dentry *d;
7464 +
7465 +       spin_lock(&inode->i_lock);
7466 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7467 +               au_dpri_dentry(d);
7468 +       spin_unlock(&inode->i_lock);
7469 +}
7470 +
7471 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7472 +{
7473 +       struct dentry *wh = NULL;
7474 +       int hn;
7475 +       struct inode *inode;
7476 +       struct au_iinfo *iinfo;
7477 +       struct au_hinode *hi;
7478 +
7479 +       if (!dentry || IS_ERR(dentry)) {
7480 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7481 +               return -1;
7482 +       }
7483 +       /* do not call dget_parent() here */
7484 +       /* note: access d_xxx without d_lock */
7485 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7486 +            bindex, dentry, dentry,
7487 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7488 +            au_dcount(dentry), dentry->d_flags,
7489 +            d_unhashed(dentry) ? "un" : "");
7490 +       hn = -1;
7491 +       inode = NULL;
7492 +       if (d_is_positive(dentry))
7493 +               inode = d_inode(dentry);
7494 +       if (inode
7495 +           && au_test_aufs(dentry->d_sb)
7496 +           && bindex >= 0
7497 +           && !au_is_bad_inode(inode)) {
7498 +               iinfo = au_ii(inode);
7499 +               hi = au_hinode(iinfo, bindex);
7500 +               hn = !!au_hn(hi);
7501 +               wh = hi->hi_whdentry;
7502 +       }
7503 +       do_pri_inode(bindex, inode, hn, wh);
7504 +       return 0;
7505 +}
7506 +
7507 +void au_dpri_dentry(struct dentry *dentry)
7508 +{
7509 +       struct au_dinfo *dinfo;
7510 +       aufs_bindex_t bindex;
7511 +       int err;
7512 +
7513 +       err = do_pri_dentry(-1, dentry);
7514 +       if (err || !au_test_aufs(dentry->d_sb))
7515 +               return;
7516 +
7517 +       dinfo = au_di(dentry);
7518 +       if (!dinfo)
7519 +               return;
7520 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7521 +            dinfo->di_btop, dinfo->di_bbot,
7522 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7523 +            dinfo->di_tmpfile);
7524 +       if (dinfo->di_btop < 0)
7525 +               return;
7526 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7527 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7528 +}
7529 +
7530 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7531 +{
7532 +       char a[32];
7533 +
7534 +       if (!file || IS_ERR(file)) {
7535 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7536 +               return -1;
7537 +       }
7538 +       a[0] = 0;
7539 +       if (bindex < 0
7540 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7541 +           && au_test_aufs(file->f_path.dentry->d_sb)
7542 +           && au_fi(file))
7543 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7544 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7545 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7546 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7547 +            file->f_version, file->f_pos, a);
7548 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7549 +               do_pri_dentry(bindex, file->f_path.dentry);
7550 +       return 0;
7551 +}
7552 +
7553 +void au_dpri_file(struct file *file)
7554 +{
7555 +       struct au_finfo *finfo;
7556 +       struct au_fidir *fidir;
7557 +       struct au_hfile *hfile;
7558 +       aufs_bindex_t bindex;
7559 +       int err;
7560 +
7561 +       err = do_pri_file(-1, file);
7562 +       if (err
7563 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7564 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7565 +               return;
7566 +
7567 +       finfo = au_fi(file);
7568 +       if (!finfo)
7569 +               return;
7570 +       if (finfo->fi_btop < 0)
7571 +               return;
7572 +       fidir = finfo->fi_hdir;
7573 +       if (!fidir)
7574 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7575 +       else
7576 +               for (bindex = finfo->fi_btop;
7577 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7578 +                    bindex++) {
7579 +                       hfile = fidir->fd_hfile + bindex;
7580 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7581 +               }
7582 +}
7583 +
7584 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7585 +{
7586 +       struct vfsmount *mnt;
7587 +       struct super_block *sb;
7588 +
7589 +       if (!br || IS_ERR(br))
7590 +               goto out;
7591 +       mnt = au_br_mnt(br);
7592 +       if (!mnt || IS_ERR(mnt))
7593 +               goto out;
7594 +       sb = mnt->mnt_sb;
7595 +       if (!sb || IS_ERR(sb))
7596 +               goto out;
7597 +
7598 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7599 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7600 +            "xino %d\n",
7601 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7602 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7603 +            sb->s_flags, sb->s_count,
7604 +            atomic_read(&sb->s_active),
7605 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7606 +       return 0;
7607 +
7608 +out:
7609 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7610 +       return -1;
7611 +}
7612 +
7613 +void au_dpri_sb(struct super_block *sb)
7614 +{
7615 +       struct au_sbinfo *sbinfo;
7616 +       aufs_bindex_t bindex;
7617 +       int err;
7618 +       /* to reduce stack size */
7619 +       struct {
7620 +               struct vfsmount mnt;
7621 +               struct au_branch fake;
7622 +       } *a;
7623 +
7624 +       /* this function can be called from magic sysrq */
7625 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7626 +       if (unlikely(!a)) {
7627 +               dpri("no memory\n");
7628 +               return;
7629 +       }
7630 +
7631 +       a->mnt.mnt_sb = sb;
7632 +       a->fake.br_path.mnt = &a->mnt;
7633 +       err = do_pri_br(-1, &a->fake);
7634 +       au_kfree_rcu(a);
7635 +       dpri("dev 0x%x\n", sb->s_dev);
7636 +       if (err || !au_test_aufs(sb))
7637 +               return;
7638 +
7639 +       sbinfo = au_sbi(sb);
7640 +       if (!sbinfo)
7641 +               return;
7642 +       dpri("nw %d, gen %u, kobj %d\n",
7643 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7644 +            kref_read(&sbinfo->si_kobj.kref));
7645 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7646 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7647 +}
7648 +
7649 +/* ---------------------------------------------------------------------- */
7650 +
7651 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7652 +{
7653 +       struct inode *h_inode, *inode = d_inode(dentry);
7654 +       struct dentry *h_dentry;
7655 +       aufs_bindex_t bindex, bbot, bi;
7656 +
7657 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7658 +               return;
7659 +
7660 +       bbot = au_dbbot(dentry);
7661 +       bi = au_ibbot(inode);
7662 +       if (bi < bbot)
7663 +               bbot = bi;
7664 +       bindex = au_dbtop(dentry);
7665 +       bi = au_ibtop(inode);
7666 +       if (bi > bindex)
7667 +               bindex = bi;
7668 +
7669 +       for (; bindex <= bbot; bindex++) {
7670 +               h_dentry = au_h_dptr(dentry, bindex);
7671 +               if (!h_dentry)
7672 +                       continue;
7673 +               h_inode = au_h_iptr(inode, bindex);
7674 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7675 +                       au_debug_on();
7676 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7677 +                       AuDbgDentry(dentry);
7678 +                       AuDbgInode(inode);
7679 +                       au_debug_off();
7680 +                       if (au_test_fuse(h_inode->i_sb))
7681 +                               WARN_ON_ONCE(1);
7682 +                       else
7683 +                               BUG();
7684 +               }
7685 +       }
7686 +}
7687 +
7688 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7689 +{
7690 +       int err, i, j;
7691 +       struct au_dcsub_pages dpages;
7692 +       struct au_dpage *dpage;
7693 +       struct dentry **dentries;
7694 +
7695 +       err = au_dpages_init(&dpages, GFP_NOFS);
7696 +       AuDebugOn(err);
7697 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7698 +       AuDebugOn(err);
7699 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7700 +               dpage = dpages.dpages + i;
7701 +               dentries = dpage->dentries;
7702 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7703 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7704 +       }
7705 +       au_dpages_free(&dpages);
7706 +}
7707 +
7708 +void au_dbg_verify_kthread(void)
7709 +{
7710 +       if (au_wkq_test()) {
7711 +               au_dbg_blocked();
7712 +               /*
7713 +                * It may be recursive, but udba=notify between two aufs mounts,
7714 +                * where a single ro branch is shared, is not a problem.
7715 +                */
7716 +               /* WARN_ON(1); */
7717 +       }
7718 +}
7719 +
7720 +/* ---------------------------------------------------------------------- */
7721 +
7722 +int __init au_debug_init(void)
7723 +{
7724 +       aufs_bindex_t bindex;
7725 +       struct au_vdir_destr destr;
7726 +
7727 +       bindex = -1;
7728 +       AuDebugOn(bindex >= 0);
7729 +
7730 +       destr.len = -1;
7731 +       AuDebugOn(destr.len < NAME_MAX);
7732 +
7733 +#ifdef CONFIG_4KSTACKS
7734 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7735 +#endif
7736 +
7737 +       return 0;
7738 +}
7739 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7740 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7741 +++ linux/fs/aufs/debug.h       2022-11-05 23:02:18.962555950 +0100
7742 @@ -0,0 +1,226 @@
7743 +/* SPDX-License-Identifier: GPL-2.0 */
7744 +/*
7745 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7746 + *
7747 + * This program is free software; you can redistribute it and/or modify
7748 + * it under the terms of the GNU General Public License as published by
7749 + * the Free Software Foundation; either version 2 of the License, or
7750 + * (at your option) any later version.
7751 + *
7752 + * This program is distributed in the hope that it will be useful,
7753 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7754 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7755 + * GNU General Public License for more details.
7756 + *
7757 + * You should have received a copy of the GNU General Public License
7758 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7759 + */
7760 +
7761 +/*
7762 + * debug print functions
7763 + */
7764 +
7765 +#ifndef __AUFS_DEBUG_H__
7766 +#define __AUFS_DEBUG_H__
7767 +
7768 +#ifdef __KERNEL__
7769 +
7770 +#include <linux/atomic.h>
7771 +#include <linux/module.h>
7772 +#include <linux/kallsyms.h>
7773 +#include <linux/sysrq.h>
7774 +
7775 +#ifdef CONFIG_AUFS_DEBUG
7776 +#define AuDebugOn(a)           BUG_ON(a)
7777 +
7778 +/* module parameter */
7779 +extern atomic_t aufs_debug;
7780 +static inline void au_debug_on(void)
7781 +{
7782 +       atomic_inc(&aufs_debug);
7783 +}
7784 +static inline void au_debug_off(void)
7785 +{
7786 +       atomic_dec_if_positive(&aufs_debug);
7787 +}
7788 +
7789 +static inline int au_debug_test(void)
7790 +{
7791 +       return atomic_read(&aufs_debug) > 0;
7792 +}
7793 +#else
7794 +#define AuDebugOn(a)           do {} while (0)
7795 +AuStubVoid(au_debug_on, void)
7796 +AuStubVoid(au_debug_off, void)
7797 +AuStubInt0(au_debug_test, void)
7798 +#endif /* CONFIG_AUFS_DEBUG */
7799 +
7800 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7801 +
7802 +/* ---------------------------------------------------------------------- */
7803 +
7804 +/* debug print */
7805 +
7806 +#define AuDbg(fmt, ...) do { \
7807 +       if (au_debug_test()) \
7808 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7809 +} while (0)
7810 +#define AuLabel(l)             AuDbg(#l "\n")
7811 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7812 +#define AuWarn1(fmt, ...) do { \
7813 +       static unsigned char _c; \
7814 +       if (!_c++) \
7815 +               pr_warn(fmt, ##__VA_ARGS__); \
7816 +} while (0)
7817 +
7818 +#define AuErr1(fmt, ...) do { \
7819 +       static unsigned char _c; \
7820 +       if (!_c++) \
7821 +               pr_err(fmt, ##__VA_ARGS__); \
7822 +} while (0)
7823 +
7824 +#define AuIOErr1(fmt, ...) do { \
7825 +       static unsigned char _c; \
7826 +       if (!_c++) \
7827 +               AuIOErr(fmt, ##__VA_ARGS__); \
7828 +} while (0)
7829 +
7830 +#define AuUnsupportMsg "This operation is not supported." \
7831 +                       " Please report this application to aufs-users ML."
7832 +#define AuUnsupport(fmt, ...) do { \
7833 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7834 +       dump_stack(); \
7835 +} while (0)
7836 +
7837 +#define AuTraceErr(e) do { \
7838 +       if (unlikely((e) < 0)) \
7839 +               AuDbg("err %d\n", (int)(e)); \
7840 +} while (0)
7841 +
7842 +#define AuTraceErrPtr(p) do { \
7843 +       if (IS_ERR(p)) \
7844 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7845 +} while (0)
7846 +
7847 +/* dirty macros for debug print, use with "%.*s" and caution */
7848 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7849 +
7850 +/* ---------------------------------------------------------------------- */
7851 +
7852 +struct dentry;
7853 +#ifdef CONFIG_AUFS_DEBUG
7854 +extern struct mutex au_dbg_mtx;
7855 +extern char *au_plevel;
7856 +struct au_nhash;
7857 +void au_dpri_whlist(struct au_nhash *whlist);
7858 +struct au_vdir;
7859 +void au_dpri_vdir(struct au_vdir *vdir);
7860 +struct inode;
7861 +void au_dpri_inode(struct inode *inode);
7862 +void au_dpri_dalias(struct inode *inode);
7863 +void au_dpri_dentry(struct dentry *dentry);
7864 +struct file;
7865 +void au_dpri_file(struct file *filp);
7866 +struct super_block;
7867 +void au_dpri_sb(struct super_block *sb);
7868 +
7869 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7870 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7871 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7872 +void au_dbg_verify_kthread(void);
7873 +
7874 +int __init au_debug_init(void);
7875 +
7876 +#define AuDbgWhlist(w) do { \
7877 +       mutex_lock(&au_dbg_mtx); \
7878 +       AuDbg(#w "\n"); \
7879 +       au_dpri_whlist(w); \
7880 +       mutex_unlock(&au_dbg_mtx); \
7881 +} while (0)
7882 +
7883 +#define AuDbgVdir(v) do { \
7884 +       mutex_lock(&au_dbg_mtx); \
7885 +       AuDbg(#v "\n"); \
7886 +       au_dpri_vdir(v); \
7887 +       mutex_unlock(&au_dbg_mtx); \
7888 +} while (0)
7889 +
7890 +#define AuDbgInode(i) do { \
7891 +       mutex_lock(&au_dbg_mtx); \
7892 +       AuDbg(#i "\n"); \
7893 +       au_dpri_inode(i); \
7894 +       mutex_unlock(&au_dbg_mtx); \
7895 +} while (0)
7896 +
7897 +#define AuDbgDAlias(i) do { \
7898 +       mutex_lock(&au_dbg_mtx); \
7899 +       AuDbg(#i "\n"); \
7900 +       au_dpri_dalias(i); \
7901 +       mutex_unlock(&au_dbg_mtx); \
7902 +} while (0)
7903 +
7904 +#define AuDbgDentry(d) do { \
7905 +       mutex_lock(&au_dbg_mtx); \
7906 +       AuDbg(#d "\n"); \
7907 +       au_dpri_dentry(d); \
7908 +       mutex_unlock(&au_dbg_mtx); \
7909 +} while (0)
7910 +
7911 +#define AuDbgFile(f) do { \
7912 +       mutex_lock(&au_dbg_mtx); \
7913 +       AuDbg(#f "\n"); \
7914 +       au_dpri_file(f); \
7915 +       mutex_unlock(&au_dbg_mtx); \
7916 +} while (0)
7917 +
7918 +#define AuDbgSb(sb) do { \
7919 +       mutex_lock(&au_dbg_mtx); \
7920 +       AuDbg(#sb "\n"); \
7921 +       au_dpri_sb(sb); \
7922 +       mutex_unlock(&au_dbg_mtx); \
7923 +} while (0)
7924 +
7925 +#define AuDbgSym(addr) do {                            \
7926 +       char sym[KSYM_SYMBOL_LEN];                      \
7927 +       sprint_symbol(sym, (unsigned long)addr);        \
7928 +       AuDbg("%s\n", sym);                             \
7929 +} while (0)
7930 +#else
7931 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7932 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7933 +AuStubVoid(au_dbg_verify_kthread, void)
7934 +AuStubInt0(__init au_debug_init, void)
7935 +
7936 +#define AuDbgWhlist(w)         do {} while (0)
7937 +#define AuDbgVdir(v)           do {} while (0)
7938 +#define AuDbgInode(i)          do {} while (0)
7939 +#define AuDbgDAlias(i)         do {} while (0)
7940 +#define AuDbgDentry(d)         do {} while (0)
7941 +#define AuDbgFile(f)           do {} while (0)
7942 +#define AuDbgSb(sb)            do {} while (0)
7943 +#define AuDbgSym(addr)         do {} while (0)
7944 +#endif /* CONFIG_AUFS_DEBUG */
7945 +
7946 +/* ---------------------------------------------------------------------- */
7947 +
7948 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7949 +int __init au_sysrq_init(void);
7950 +void au_sysrq_fin(void);
7951 +
7952 +#ifdef CONFIG_HW_CONSOLE
7953 +#define au_dbg_blocked() do { \
7954 +       WARN_ON(1); \
7955 +       handle_sysrq('w'); \
7956 +} while (0)
7957 +#else
7958 +AuStubVoid(au_dbg_blocked, void)
7959 +#endif
7960 +
7961 +#else
7962 +AuStubInt0(__init au_sysrq_init, void)
7963 +AuStubVoid(au_sysrq_fin, void)
7964 +AuStubVoid(au_dbg_blocked, void)
7965 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7966 +
7967 +#endif /* __KERNEL__ */
7968 +#endif /* __AUFS_DEBUG_H__ */
7969 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7970 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7971 +++ linux/fs/aufs/dentry.c      2023-08-28 12:34:39.956636132 +0200
7972 @@ -0,0 +1,1168 @@
7973 +// SPDX-License-Identifier: GPL-2.0
7974 +/*
7975 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7976 + *
7977 + * This program is free software; you can redistribute it and/or modify
7978 + * it under the terms of the GNU General Public License as published by
7979 + * the Free Software Foundation; either version 2 of the License, or
7980 + * (at your option) any later version.
7981 + *
7982 + * This program is distributed in the hope that it will be useful,
7983 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7984 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7985 + * GNU General Public License for more details.
7986 + *
7987 + * You should have received a copy of the GNU General Public License
7988 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7989 + */
7990 +
7991 +/*
7992 + * lookup and dentry operations
7993 + */
7994 +
7995 +#include <linux/iversion.h>
7996 +#include "aufs.h"
7997 +
7998 +/*
7999 + * returns positive/negative dentry, NULL or an error.
8000 + * NULL means whiteout-ed or not-found.
8001 + */
8002 +static struct dentry*
8003 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8004 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
8005 +{
8006 +       struct dentry *h_dentry;
8007 +       struct inode *h_inode;
8008 +       struct au_branch *br;
8009 +       struct mnt_idmap *h_idmap;
8010 +       struct path h_path;
8011 +       int wh_found, opq;
8012 +       unsigned char wh_able;
8013 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8014 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8015 +                                                         IGNORE_PERM);
8016 +
8017 +       wh_found = 0;
8018 +       br = au_sbr(dentry->d_sb, bindex);
8019 +       h_path.dentry = h_parent;
8020 +       h_path.mnt = au_br_mnt(br);
8021 +       h_idmap = au_br_idmap(br);
8022 +       wh_able = !!au_br_whable(br->br_perm);
8023 +       if (wh_able)
8024 +               wh_found = au_wh_test(h_idmap, &h_path, &args->whname,
8025 +                                     ignore_perm);
8026 +       h_dentry = ERR_PTR(wh_found);
8027 +       if (!wh_found)
8028 +               goto real_lookup;
8029 +       if (unlikely(wh_found < 0))
8030 +               goto out;
8031 +
8032 +       /* We found a whiteout */
8033 +       /* au_set_dbbot(dentry, bindex); */
8034 +       au_set_dbwh(dentry, bindex);
8035 +       if (!allow_neg)
8036 +               return NULL; /* success */
8037 +
8038 +real_lookup:
8039 +       if (!ignore_perm)
8040 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8041 +       else
8042 +               h_dentry = au_sio_lkup_one(h_idmap, args->name, &h_path);
8043 +       if (IS_ERR(h_dentry)) {
8044 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8045 +                   && !allow_neg)
8046 +                       h_dentry = NULL;
8047 +               goto out;
8048 +       }
8049 +
8050 +       h_inode = d_inode(h_dentry);
8051 +       if (d_is_negative(h_dentry)) {
8052 +               if (!allow_neg)
8053 +                       goto out_neg;
8054 +       } else if (wh_found
8055 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8056 +               goto out_neg;
8057 +       else if (au_ftest_lkup(args->flags, DIRREN)
8058 +                /* && h_inode */
8059 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8060 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8061 +                     (unsigned long long)h_inode->i_ino);
8062 +               goto out_neg;
8063 +       }
8064 +
8065 +       if (au_dbbot(dentry) <= bindex)
8066 +               au_set_dbbot(dentry, bindex);
8067 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8068 +               au_set_dbtop(dentry, bindex);
8069 +       au_set_h_dptr(dentry, bindex, h_dentry);
8070 +
8071 +       if (!d_is_dir(h_dentry)
8072 +           || !wh_able
8073 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8074 +               goto out; /* success */
8075 +
8076 +       h_path.dentry = h_dentry;
8077 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8078 +       opq = au_diropq_test(h_idmap, &h_path);
8079 +       inode_unlock_shared(h_inode);
8080 +       if (opq > 0)
8081 +               au_set_dbdiropq(dentry, bindex);
8082 +       else if (unlikely(opq < 0)) {
8083 +               au_set_h_dptr(dentry, bindex, NULL);
8084 +               h_dentry = ERR_PTR(opq);
8085 +       }
8086 +       goto out;
8087 +
8088 +out_neg:
8089 +       dput(h_dentry);
8090 +       h_dentry = NULL;
8091 +out:
8092 +       return h_dentry;
8093 +}
8094 +
8095 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8096 +{
8097 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8098 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8099 +               return -EPERM;
8100 +       return 0;
8101 +}
8102 +
8103 +/*
8104 + * returns the number of lower positive dentries,
8105 + * otherwise an error.
8106 + * can be called at unlinking with @type is zero.
8107 + */
8108 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8109 +                  unsigned int flags)
8110 +{
8111 +       int npositive, err;
8112 +       aufs_bindex_t bindex, btail, bdiropq;
8113 +       unsigned char isdir, dirperm1, dirren;
8114 +       struct au_do_lookup_args args = {
8115 +               .flags          = flags,
8116 +               .name           = &dentry->d_name
8117 +       };
8118 +       struct dentry *parent;
8119 +       struct super_block *sb;
8120 +
8121 +       sb = dentry->d_sb;
8122 +       err = au_test_shwh(sb, args.name);
8123 +       if (unlikely(err))
8124 +               goto out;
8125 +
8126 +       err = au_wh_name_alloc(&args.whname, args.name);
8127 +       if (unlikely(err))
8128 +               goto out;
8129 +
8130 +       isdir = !!d_is_dir(dentry);
8131 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8132 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8133 +       if (dirren)
8134 +               au_fset_lkup(args.flags, DIRREN);
8135 +
8136 +       npositive = 0;
8137 +       parent = dget_parent(dentry);
8138 +       btail = au_dbtaildir(parent);
8139 +       for (bindex = btop; bindex <= btail; bindex++) {
8140 +               struct dentry *h_parent, *h_dentry;
8141 +               struct inode *h_inode, *h_dir;
8142 +               struct au_branch *br;
8143 +
8144 +               h_dentry = au_h_dptr(dentry, bindex);
8145 +               if (h_dentry) {
8146 +                       if (d_is_positive(h_dentry))
8147 +                               npositive++;
8148 +                       break;
8149 +               }
8150 +               h_parent = au_h_dptr(parent, bindex);
8151 +               if (!h_parent || !d_is_dir(h_parent))
8152 +                       continue;
8153 +
8154 +               if (dirren) {
8155 +                       /* if the inum matches, then use the prepared name */
8156 +                       err = au_dr_lkup_name(&args, bindex);
8157 +                       if (unlikely(err))
8158 +                               goto out_parent;
8159 +               }
8160 +
8161 +               h_dir = d_inode(h_parent);
8162 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8163 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8164 +               inode_unlock_shared(h_dir);
8165 +               err = PTR_ERR(h_dentry);
8166 +               if (IS_ERR(h_dentry))
8167 +                       goto out_parent;
8168 +               if (h_dentry)
8169 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8170 +               if (dirperm1)
8171 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8172 +
8173 +               if (au_dbwh(dentry) == bindex)
8174 +                       break;
8175 +               if (!h_dentry)
8176 +                       continue;
8177 +               if (d_is_negative(h_dentry))
8178 +                       continue;
8179 +               h_inode = d_inode(h_dentry);
8180 +               npositive++;
8181 +               if (!args.type)
8182 +                       args.type = h_inode->i_mode & S_IFMT;
8183 +               if (args.type != S_IFDIR)
8184 +                       break;
8185 +               else if (isdir) {
8186 +                       /* the type of lower may be different */
8187 +                       bdiropq = au_dbdiropq(dentry);
8188 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8189 +                               break;
8190 +               }
8191 +               br = au_sbr(sb, bindex);
8192 +               if (dirren
8193 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8194 +                                          /*add_ent*/NULL)) {
8195 +                       /* prepare next name to lookup */
8196 +                       err = au_dr_lkup(&args, dentry, bindex);
8197 +                       if (unlikely(err))
8198 +                               goto out_parent;
8199 +               }
8200 +       }
8201 +
8202 +       if (npositive) {
8203 +               AuLabel(positive);
8204 +               au_update_dbtop(dentry);
8205 +       }
8206 +       err = npositive;
8207 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8208 +                    && au_dbtop(dentry) < 0)) {
8209 +               err = -EIO;
8210 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8211 +                       dentry, err);
8212 +       }
8213 +
8214 +out_parent:
8215 +       dput(parent);
8216 +       au_kfree_try_rcu(args.whname.name);
8217 +       if (dirren)
8218 +               au_dr_lkup_fin(&args);
8219 +out:
8220 +       return err;
8221 +}
8222 +
8223 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
8224 +                              struct path *ppath)
8225 +{
8226 +       struct dentry *dentry;
8227 +       int wkq_err;
8228 +
8229 +       if (!au_test_h_perm_sio(idmap, d_inode(ppath->dentry), MAY_EXEC))
8230 +               dentry = vfsub_lkup_one(name, ppath);
8231 +       else {
8232 +               struct vfsub_lkup_one_args args = {
8233 +                       .errp   = &dentry,
8234 +                       .name   = name,
8235 +                       .ppath  = ppath
8236 +               };
8237 +
8238 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8239 +               if (unlikely(wkq_err))
8240 +                       dentry = ERR_PTR(wkq_err);
8241 +       }
8242 +
8243 +       return dentry;
8244 +}
8245 +
8246 +/*
8247 + * lookup @dentry on @bindex which should be negative.
8248 + */
8249 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8250 +{
8251 +       int err;
8252 +       struct dentry *parent, *h_dentry;
8253 +       struct au_branch *br;
8254 +       struct mnt_idmap *h_idmap;
8255 +       struct path h_ppath;
8256 +
8257 +       parent = dget_parent(dentry);
8258 +       br = au_sbr(dentry->d_sb, bindex);
8259 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8260 +       h_ppath.mnt = au_br_mnt(br);
8261 +       h_idmap = au_br_idmap(br);
8262 +       if (wh)
8263 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8264 +       else
8265 +               h_dentry = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
8266 +       err = PTR_ERR(h_dentry);
8267 +       if (IS_ERR(h_dentry))
8268 +               goto out;
8269 +       if (unlikely(d_is_positive(h_dentry))) {
8270 +               err = -EIO;
8271 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8272 +               dput(h_dentry);
8273 +               goto out;
8274 +       }
8275 +
8276 +       err = 0;
8277 +       if (bindex < au_dbtop(dentry))
8278 +               au_set_dbtop(dentry, bindex);
8279 +       if (au_dbbot(dentry) < bindex)
8280 +               au_set_dbbot(dentry, bindex);
8281 +       au_set_h_dptr(dentry, bindex, h_dentry);
8282 +
8283 +out:
8284 +       dput(parent);
8285 +       return err;
8286 +}
8287 +
8288 +/* ---------------------------------------------------------------------- */
8289 +
8290 +/* subset of struct inode */
8291 +struct au_iattr {
8292 +       unsigned long           i_ino;
8293 +       /* unsigned int         i_nlink; */
8294 +       kuid_t                  i_uid;
8295 +       kgid_t                  i_gid;
8296 +       u64                     i_version;
8297 +/*
8298 +       loff_t                  i_size;
8299 +       blkcnt_t                i_blocks;
8300 +*/
8301 +       umode_t                 i_mode;
8302 +};
8303 +
8304 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8305 +{
8306 +       ia->i_ino = h_inode->i_ino;
8307 +       /* ia->i_nlink = h_inode->i_nlink; */
8308 +       ia->i_uid = h_inode->i_uid;
8309 +       ia->i_gid = h_inode->i_gid;
8310 +       ia->i_version = inode_query_iversion(h_inode);
8311 +/*
8312 +       ia->i_size = h_inode->i_size;
8313 +       ia->i_blocks = h_inode->i_blocks;
8314 +*/
8315 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8316 +}
8317 +
8318 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8319 +{
8320 +       return ia->i_ino != h_inode->i_ino
8321 +               /* || ia->i_nlink != h_inode->i_nlink */
8322 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8323 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8324 +               || !inode_eq_iversion(h_inode, ia->i_version)
8325 +/*
8326 +               || ia->i_size != h_inode->i_size
8327 +               || ia->i_blocks != h_inode->i_blocks
8328 +*/
8329 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8330 +}
8331 +
8332 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8333 +                             struct au_branch *br)
8334 +{
8335 +       int err;
8336 +       struct au_iattr ia;
8337 +       struct inode *h_inode;
8338 +       struct dentry *h_d;
8339 +       struct super_block *h_sb;
8340 +       struct path h_ppath;
8341 +
8342 +       err = 0;
8343 +       memset(&ia, -1, sizeof(ia));
8344 +       h_sb = h_dentry->d_sb;
8345 +       h_inode = NULL;
8346 +       if (d_is_positive(h_dentry)) {
8347 +               h_inode = d_inode(h_dentry);
8348 +               au_iattr_save(&ia, h_inode);
8349 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8350 +               /* nfs d_revalidate may return 0 for negative dentry */
8351 +               /* fuse d_revalidate always return 0 for negative dentry */
8352 +               goto out;
8353 +
8354 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8355 +       h_ppath.dentry = h_parent;
8356 +       h_ppath.mnt = au_br_mnt(br);
8357 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8358 +       err = PTR_ERR(h_d);
8359 +       if (IS_ERR(h_d))
8360 +               goto out;
8361 +
8362 +       err = 0;
8363 +       if (unlikely(h_d != h_dentry
8364 +                    || d_inode(h_d) != h_inode
8365 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8366 +               err = au_busy_or_stale();
8367 +       dput(h_d);
8368 +
8369 +out:
8370 +       AuTraceErr(err);
8371 +       return err;
8372 +}
8373 +
8374 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8375 +               struct dentry *h_parent, struct au_branch *br)
8376 +{
8377 +       int err;
8378 +
8379 +       err = 0;
8380 +       if (udba == AuOpt_UDBA_REVAL
8381 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8382 +               IMustLock(h_dir);
8383 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8384 +       } else if (udba != AuOpt_UDBA_NONE)
8385 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8386 +
8387 +       return err;
8388 +}
8389 +
8390 +/* ---------------------------------------------------------------------- */
8391 +
8392 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8393 +{
8394 +       int err;
8395 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8396 +       struct au_hdentry tmp, *p, *q;
8397 +       struct au_dinfo *dinfo;
8398 +       struct super_block *sb;
8399 +
8400 +       DiMustWriteLock(dentry);
8401 +
8402 +       sb = dentry->d_sb;
8403 +       dinfo = au_di(dentry);
8404 +       bbot = dinfo->di_bbot;
8405 +       bwh = dinfo->di_bwh;
8406 +       bdiropq = dinfo->di_bdiropq;
8407 +       bindex = dinfo->di_btop;
8408 +       p = au_hdentry(dinfo, bindex);
8409 +       for (; bindex <= bbot; bindex++, p++) {
8410 +               if (!p->hd_dentry)
8411 +                       continue;
8412 +
8413 +               new_bindex = au_br_index(sb, p->hd_id);
8414 +               if (new_bindex == bindex)
8415 +                       continue;
8416 +
8417 +               if (dinfo->di_bwh == bindex)
8418 +                       bwh = new_bindex;
8419 +               if (dinfo->di_bdiropq == bindex)
8420 +                       bdiropq = new_bindex;
8421 +               if (new_bindex < 0) {
8422 +                       au_hdput(p);
8423 +                       p->hd_dentry = NULL;
8424 +                       continue;
8425 +               }
8426 +
8427 +               /* swap two lower dentries, and loop again */
8428 +               q = au_hdentry(dinfo, new_bindex);
8429 +               tmp = *q;
8430 +               *q = *p;
8431 +               *p = tmp;
8432 +               if (tmp.hd_dentry) {
8433 +                       bindex--;
8434 +                       p--;
8435 +               }
8436 +       }
8437 +
8438 +       dinfo->di_bwh = -1;
8439 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8440 +               dinfo->di_bwh = bwh;
8441 +
8442 +       dinfo->di_bdiropq = -1;
8443 +       if (bdiropq >= 0
8444 +           && bdiropq <= au_sbbot(sb)
8445 +           && au_sbr_whable(sb, bdiropq))
8446 +               dinfo->di_bdiropq = bdiropq;
8447 +
8448 +       err = -EIO;
8449 +       dinfo->di_btop = -1;
8450 +       dinfo->di_bbot = -1;
8451 +       bbot = au_dbbot(parent);
8452 +       bindex = 0;
8453 +       p = au_hdentry(dinfo, bindex);
8454 +       for (; bindex <= bbot; bindex++, p++)
8455 +               if (p->hd_dentry) {
8456 +                       dinfo->di_btop = bindex;
8457 +                       break;
8458 +               }
8459 +
8460 +       if (dinfo->di_btop >= 0) {
8461 +               bindex = bbot;
8462 +               p = au_hdentry(dinfo, bindex);
8463 +               for (; bindex >= 0; bindex--, p--)
8464 +                       if (p->hd_dentry) {
8465 +                               dinfo->di_bbot = bindex;
8466 +                               err = 0;
8467 +                               break;
8468 +                       }
8469 +       }
8470 +
8471 +       return err;
8472 +}
8473 +
8474 +static void au_do_hide(struct dentry *dentry)
8475 +{
8476 +       struct inode *inode;
8477 +
8478 +       if (d_really_is_positive(dentry)) {
8479 +               inode = d_inode(dentry);
8480 +               if (!d_is_dir(dentry)) {
8481 +                       if (inode->i_nlink && !d_unhashed(dentry))
8482 +                               drop_nlink(inode);
8483 +               } else {
8484 +                       clear_nlink(inode);
8485 +                       /* stop next lookup */
8486 +                       inode->i_flags |= S_DEAD;
8487 +               }
8488 +               smp_mb(); /* necessary? */
8489 +       }
8490 +       d_drop(dentry);
8491 +}
8492 +
8493 +static int au_hide_children(struct dentry *parent)
8494 +{
8495 +       int err, i, j, ndentry;
8496 +       struct au_dcsub_pages dpages;
8497 +       struct au_dpage *dpage;
8498 +       struct dentry *dentry;
8499 +
8500 +       err = au_dpages_init(&dpages, GFP_NOFS);
8501 +       if (unlikely(err))
8502 +               goto out;
8503 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8504 +       if (unlikely(err))
8505 +               goto out_dpages;
8506 +
8507 +       /* in reverse order */
8508 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8509 +               dpage = dpages.dpages + i;
8510 +               ndentry = dpage->ndentry;
8511 +               for (j = ndentry - 1; j >= 0; j--) {
8512 +                       dentry = dpage->dentries[j];
8513 +                       if (dentry != parent)
8514 +                               au_do_hide(dentry);
8515 +               }
8516 +       }
8517 +
8518 +out_dpages:
8519 +       au_dpages_free(&dpages);
8520 +out:
8521 +       return err;
8522 +}
8523 +
8524 +static void au_hide(struct dentry *dentry)
8525 +{
8526 +       int err;
8527 +
8528 +       AuDbgDentry(dentry);
8529 +       if (d_is_dir(dentry)) {
8530 +               /* shrink_dcache_parent(dentry); */
8531 +               err = au_hide_children(dentry);
8532 +               if (unlikely(err))
8533 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8534 +                               dentry, err);
8535 +       }
8536 +       au_do_hide(dentry);
8537 +}
8538 +
8539 +/*
8540 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8541 + *
8542 + * a dirty branch is added
8543 + * - on the top of layers
8544 + * - in the middle of layers
8545 + * - to the bottom of layers
8546 + *
8547 + * on the added branch there exists
8548 + * - a whiteout
8549 + * - a diropq
8550 + * - a same named entry
8551 + *   + exist
8552 + *     * negative --> positive
8553 + *     * positive --> positive
8554 + *      - type is unchanged
8555 + *      - type is changed
8556 + *   + doesn't exist
8557 + *     * negative --> negative
8558 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8559 + * - none
8560 + */
8561 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8562 +                              struct au_dinfo *tmp)
8563 +{
8564 +       int err;
8565 +       aufs_bindex_t bindex, bbot;
8566 +       struct {
8567 +               struct dentry *dentry;
8568 +               struct inode *inode;
8569 +               mode_t mode;
8570 +       } orig_h, tmp_h = {
8571 +               .dentry = NULL
8572 +       };
8573 +       struct au_hdentry *hd;
8574 +       struct inode *inode, *h_inode;
8575 +       struct dentry *h_dentry;
8576 +
8577 +       err = 0;
8578 +       AuDebugOn(dinfo->di_btop < 0);
8579 +       orig_h.mode = 0;
8580 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8581 +       orig_h.inode = NULL;
8582 +       if (d_is_positive(orig_h.dentry)) {
8583 +               orig_h.inode = d_inode(orig_h.dentry);
8584 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8585 +       }
8586 +       if (tmp->di_btop >= 0) {
8587 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8588 +               if (d_is_positive(tmp_h.dentry)) {
8589 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8590 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8591 +               }
8592 +       }
8593 +
8594 +       inode = NULL;
8595 +       if (d_really_is_positive(dentry))
8596 +               inode = d_inode(dentry);
8597 +       if (!orig_h.inode) {
8598 +               AuDbg("negative originally\n");
8599 +               if (inode) {
8600 +                       au_hide(dentry);
8601 +                       goto out;
8602 +               }
8603 +               AuDebugOn(inode);
8604 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8605 +               AuDebugOn(dinfo->di_bdiropq != -1);
8606 +
8607 +               if (!tmp_h.inode) {
8608 +                       AuDbg("negative --> negative\n");
8609 +                       /* should have only one negative lower */
8610 +                       if (tmp->di_btop >= 0
8611 +                           && tmp->di_btop < dinfo->di_btop) {
8612 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8613 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8614 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8615 +                               au_di_cp(dinfo, tmp);
8616 +                               hd = au_hdentry(tmp, tmp->di_btop);
8617 +                               au_set_h_dptr(dentry, tmp->di_btop,
8618 +                                             dget(hd->hd_dentry));
8619 +                       }
8620 +                       au_dbg_verify_dinode(dentry);
8621 +               } else {
8622 +                       AuDbg("negative --> positive\n");
8623 +                       /*
8624 +                        * similar to the behaviour of creating with bypassing
8625 +                        * aufs.
8626 +                        * unhash it in order to force an error in the
8627 +                        * succeeding create operation.
8628 +                        * we should not set S_DEAD here.
8629 +                        */
8630 +                       d_drop(dentry);
8631 +                       /* au_di_swap(tmp, dinfo); */
8632 +                       au_dbg_verify_dinode(dentry);
8633 +               }
8634 +       } else {
8635 +               AuDbg("positive originally\n");
8636 +               /* inode may be NULL */
8637 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8638 +               if (!tmp_h.inode) {
8639 +                       AuDbg("positive --> negative\n");
8640 +                       /* or bypassing aufs */
8641 +                       au_hide(dentry);
8642 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8643 +                               dinfo->di_bwh = tmp->di_bwh;
8644 +                       if (inode)
8645 +                               err = au_refresh_hinode_self(inode);
8646 +                       au_dbg_verify_dinode(dentry);
8647 +               } else if (orig_h.mode == tmp_h.mode) {
8648 +                       AuDbg("positive --> positive, same type\n");
8649 +                       if (!S_ISDIR(orig_h.mode)
8650 +                           && dinfo->di_btop > tmp->di_btop) {
8651 +                               /*
8652 +                                * similar to the behaviour of removing and
8653 +                                * creating.
8654 +                                */
8655 +                               au_hide(dentry);
8656 +                               if (inode)
8657 +                                       err = au_refresh_hinode_self(inode);
8658 +                               au_dbg_verify_dinode(dentry);
8659 +                       } else {
8660 +                               /* fill empty slots */
8661 +                               if (dinfo->di_btop > tmp->di_btop)
8662 +                                       dinfo->di_btop = tmp->di_btop;
8663 +                               if (dinfo->di_bbot < tmp->di_bbot)
8664 +                                       dinfo->di_bbot = tmp->di_bbot;
8665 +                               dinfo->di_bwh = tmp->di_bwh;
8666 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8667 +                               bbot = dinfo->di_bbot;
8668 +                               bindex = tmp->di_btop;
8669 +                               hd = au_hdentry(tmp, bindex);
8670 +                               for (; bindex <= bbot; bindex++, hd++) {
8671 +                                       if (au_h_dptr(dentry, bindex))
8672 +                                               continue;
8673 +                                       h_dentry = hd->hd_dentry;
8674 +                                       if (!h_dentry)
8675 +                                               continue;
8676 +                                       AuDebugOn(d_is_negative(h_dentry));
8677 +                                       h_inode = d_inode(h_dentry);
8678 +                                       AuDebugOn(orig_h.mode
8679 +                                                 != (h_inode->i_mode
8680 +                                                     & S_IFMT));
8681 +                                       au_set_h_dptr(dentry, bindex,
8682 +                                                     dget(h_dentry));
8683 +                               }
8684 +                               if (inode)
8685 +                                       err = au_refresh_hinode(inode, dentry);
8686 +                               au_dbg_verify_dinode(dentry);
8687 +                       }
8688 +               } else {
8689 +                       AuDbg("positive --> positive, different type\n");
8690 +                       /* similar to the behaviour of removing and creating */
8691 +                       au_hide(dentry);
8692 +                       if (inode)
8693 +                               err = au_refresh_hinode_self(inode);
8694 +                       au_dbg_verify_dinode(dentry);
8695 +               }
8696 +       }
8697 +
8698 +out:
8699 +       return err;
8700 +}
8701 +
8702 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8703 +{
8704 +       const struct dentry_operations *dop
8705 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8706 +       static const unsigned int mask
8707 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8708 +
8709 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8710 +
8711 +       if (dentry->d_op == dop)
8712 +               return;
8713 +
8714 +       AuDbg("%pd\n", dentry);
8715 +       spin_lock(&dentry->d_lock);
8716 +       if (dop == &aufs_dop)
8717 +               dentry->d_flags |= mask;
8718 +       else
8719 +               dentry->d_flags &= ~mask;
8720 +       dentry->d_op = dop;
8721 +       spin_unlock(&dentry->d_lock);
8722 +}
8723 +
8724 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8725 +{
8726 +       int err, ebrange, nbr;
8727 +       unsigned int sigen;
8728 +       struct au_dinfo *dinfo, *tmp;
8729 +       struct super_block *sb;
8730 +       struct inode *inode;
8731 +
8732 +       DiMustWriteLock(dentry);
8733 +       AuDebugOn(IS_ROOT(dentry));
8734 +       AuDebugOn(d_really_is_negative(parent));
8735 +
8736 +       sb = dentry->d_sb;
8737 +       sigen = au_sigen(sb);
8738 +       err = au_digen_test(parent, sigen);
8739 +       if (unlikely(err))
8740 +               goto out;
8741 +
8742 +       nbr = au_sbbot(sb) + 1;
8743 +       dinfo = au_di(dentry);
8744 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8745 +       if (unlikely(err))
8746 +               goto out;
8747 +       ebrange = au_dbrange_test(dentry);
8748 +       if (!ebrange)
8749 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8750 +
8751 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8752 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8753 +               if (d_really_is_positive(dentry)) {
8754 +                       inode = d_inode(dentry);
8755 +                       err = au_refresh_hinode_self(inode);
8756 +               }
8757 +               au_dbg_verify_dinode(dentry);
8758 +               if (!err)
8759 +                       goto out_dgen; /* success */
8760 +               goto out;
8761 +       }
8762 +
8763 +       /* temporary dinfo */
8764 +       AuDbgDentry(dentry);
8765 +       err = -ENOMEM;
8766 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8767 +       if (unlikely(!tmp))
8768 +               goto out;
8769 +       au_di_swap(tmp, dinfo);
8770 +       /* returns the number of positive dentries */
8771 +       /*
8772 +        * if current working dir is removed, it returns an error.
8773 +        * but the dentry is legal.
8774 +        */
8775 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8776 +       AuDbgDentry(dentry);
8777 +       au_di_swap(tmp, dinfo);
8778 +       if (err == -ENOENT)
8779 +               err = 0;
8780 +       if (err >= 0) {
8781 +               /* compare/refresh by dinfo */
8782 +               AuDbgDentry(dentry);
8783 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8784 +               au_dbg_verify_dinode(dentry);
8785 +               AuTraceErr(err);
8786 +       }
8787 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8788 +       au_rw_write_unlock(&tmp->di_rwsem);
8789 +       au_di_free(tmp);
8790 +       if (unlikely(err))
8791 +               goto out;
8792 +
8793 +out_dgen:
8794 +       au_update_digen(dentry);
8795 +out:
8796 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8797 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8798 +               AuDbgDentry(dentry);
8799 +       }
8800 +       AuTraceErr(err);
8801 +       return err;
8802 +}
8803 +
8804 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8805 +                          struct dentry *dentry, aufs_bindex_t bindex)
8806 +{
8807 +       int err, valid;
8808 +
8809 +       err = 0;
8810 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8811 +               goto out;
8812 +
8813 +       AuDbg("b%d\n", bindex);
8814 +       /*
8815 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8816 +        * due to whiteout and branch permission.
8817 +        */
8818 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8819 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8820 +       /* it may return tri-state */
8821 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8822 +
8823 +       if (unlikely(valid < 0))
8824 +               err = valid;
8825 +       else if (!valid)
8826 +               err = -EINVAL;
8827 +
8828 +out:
8829 +       AuTraceErr(err);
8830 +       return err;
8831 +}
8832 +
8833 +/* todo: remove this */
8834 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8835 +                         unsigned int flags, int do_udba, int dirren)
8836 +{
8837 +       int err;
8838 +       umode_t mode, h_mode;
8839 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8840 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8841 +       struct inode *h_inode, *h_cached_inode;
8842 +       struct dentry *h_dentry;
8843 +       struct qstr *name, *h_name;
8844 +
8845 +       err = 0;
8846 +       plus = 0;
8847 +       mode = 0;
8848 +       ibs = -1;
8849 +       ibe = -1;
8850 +       unhashed = !!d_unhashed(dentry);
8851 +       is_root = !!IS_ROOT(dentry);
8852 +       name = &dentry->d_name;
8853 +       tmpfile = au_di(dentry)->di_tmpfile;
8854 +
8855 +       /*
8856 +        * Theoretically, REVAL test should be unnecessary in case of
8857 +        * {FS,I}NOTIFY.
8858 +        * But {fs,i}notify doesn't fire some necessary events,
8859 +        *      IN_ATTRIB for atime/nlink/pageio
8860 +        * Let's do REVAL test too.
8861 +        */
8862 +       if (do_udba && inode) {
8863 +               mode = (inode->i_mode & S_IFMT);
8864 +               plus = (inode->i_nlink > 0);
8865 +               ibs = au_ibtop(inode);
8866 +               ibe = au_ibbot(inode);
8867 +       }
8868 +
8869 +       btop = au_dbtop(dentry);
8870 +       btail = btop;
8871 +       if (inode && S_ISDIR(inode->i_mode))
8872 +               btail = au_dbtaildir(dentry);
8873 +       for (bindex = btop; bindex <= btail; bindex++) {
8874 +               h_dentry = au_h_dptr(dentry, bindex);
8875 +               if (!h_dentry)
8876 +                       continue;
8877 +
8878 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8879 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8880 +               spin_lock(&h_dentry->d_lock);
8881 +               h_name = &h_dentry->d_name;
8882 +               if (unlikely(do_udba
8883 +                            && !is_root
8884 +                            && ((!h_nfs
8885 +                                 && (unhashed != !!d_unhashed(h_dentry)
8886 +                                     || (!tmpfile && !dirren
8887 +                                         && !au_qstreq(name, h_name))
8888 +                                         ))
8889 +                                || (h_nfs
8890 +                                    && !(flags & LOOKUP_OPEN)
8891 +                                    && (h_dentry->d_flags
8892 +                                        & DCACHE_NFSFS_RENAMED)))
8893 +                           )) {
8894 +                       int h_unhashed;
8895 +
8896 +                       h_unhashed = d_unhashed(h_dentry);
8897 +                       spin_unlock(&h_dentry->d_lock);
8898 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8899 +                             unhashed, h_unhashed, dentry, h_dentry);
8900 +                       goto err;
8901 +               }
8902 +               spin_unlock(&h_dentry->d_lock);
8903 +
8904 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8905 +               if (unlikely(err))
8906 +                       /* do not goto err, to keep the errno */
8907 +                       break;
8908 +
8909 +               /* todo: plink too? */
8910 +               if (!do_udba)
8911 +                       continue;
8912 +
8913 +               /* UDBA tests */
8914 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8915 +                       goto err;
8916 +
8917 +               h_inode = NULL;
8918 +               if (d_is_positive(h_dentry))
8919 +                       h_inode = d_inode(h_dentry);
8920 +               h_plus = plus;
8921 +               h_mode = mode;
8922 +               h_cached_inode = h_inode;
8923 +               if (h_inode) {
8924 +                       h_mode = (h_inode->i_mode & S_IFMT);
8925 +                       h_plus = (h_inode->i_nlink > 0);
8926 +               }
8927 +               if (inode && ibs <= bindex && bindex <= ibe)
8928 +                       h_cached_inode = au_h_iptr(inode, bindex);
8929 +
8930 +               if (!h_nfs) {
8931 +                       if (unlikely(plus != h_plus && !tmpfile))
8932 +                               goto err;
8933 +               } else {
8934 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8935 +                                    && !is_root
8936 +                                    && !IS_ROOT(h_dentry)
8937 +                                    && unhashed != d_unhashed(h_dentry)))
8938 +                               goto err;
8939 +               }
8940 +               if (unlikely(mode != h_mode
8941 +                            || h_cached_inode != h_inode))
8942 +                       goto err;
8943 +               continue;
8944 +
8945 +err:
8946 +               err = -EINVAL;
8947 +               break;
8948 +       }
8949 +
8950 +       AuTraceErr(err);
8951 +       return err;
8952 +}
8953 +
8954 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8955 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8956 +{
8957 +       int err;
8958 +       struct dentry *parent;
8959 +
8960 +       if (!au_digen_test(dentry, sigen))
8961 +               return 0;
8962 +
8963 +       parent = dget_parent(dentry);
8964 +       di_read_lock_parent(parent, AuLock_IR);
8965 +       AuDebugOn(au_digen_test(parent, sigen));
8966 +       au_dbg_verify_gen(parent, sigen);
8967 +       err = au_refresh_dentry(dentry, parent);
8968 +       di_read_unlock(parent, AuLock_IR);
8969 +       dput(parent);
8970 +       AuTraceErr(err);
8971 +       return err;
8972 +}
8973 +
8974 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8975 +{
8976 +       int err;
8977 +       struct dentry *d, *parent;
8978 +
8979 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8980 +               return simple_reval_dpath(dentry, sigen);
8981 +
8982 +       /* slow loop, keep it simple and stupid */
8983 +       /* cf: au_cpup_dirs() */
8984 +       err = 0;
8985 +       parent = NULL;
8986 +       while (au_digen_test(dentry, sigen)) {
8987 +               d = dentry;
8988 +               while (1) {
8989 +                       dput(parent);
8990 +                       parent = dget_parent(d);
8991 +                       if (!au_digen_test(parent, sigen))
8992 +                               break;
8993 +                       d = parent;
8994 +               }
8995 +
8996 +               if (d != dentry)
8997 +                       di_write_lock_child2(d);
8998 +
8999 +               /* someone might update our dentry while we were sleeping */
9000 +               if (au_digen_test(d, sigen)) {
9001 +                       /*
9002 +                        * todo: consolidate with simple_reval_dpath(),
9003 +                        * do_refresh() and au_reval_for_attr().
9004 +                        */
9005 +                       di_read_lock_parent(parent, AuLock_IR);
9006 +                       err = au_refresh_dentry(d, parent);
9007 +                       di_read_unlock(parent, AuLock_IR);
9008 +               }
9009 +
9010 +               if (d != dentry)
9011 +                       di_write_unlock(d);
9012 +               dput(parent);
9013 +               if (unlikely(err))
9014 +                       break;
9015 +       }
9016 +
9017 +       return err;
9018 +}
9019 +
9020 +/*
9021 + * if valid returns 1, otherwise 0.
9022 + */
9023 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9024 +{
9025 +       int valid, err;
9026 +       unsigned int sigen;
9027 +       unsigned char do_udba, dirren;
9028 +       struct super_block *sb;
9029 +       struct inode *inode;
9030 +
9031 +       /* todo: support rcu-walk? */
9032 +       if (flags & LOOKUP_RCU)
9033 +               return -ECHILD;
9034 +
9035 +       valid = 0;
9036 +       if (unlikely(!au_di(dentry)))
9037 +               goto out;
9038 +
9039 +       valid = 1;
9040 +       sb = dentry->d_sb;
9041 +       /*
9042 +        * todo: very ugly
9043 +        * i_mutex of parent dir may be held,
9044 +        * but we should not return 'invalid' due to busy.
9045 +        */
9046 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9047 +       if (unlikely(err)) {
9048 +               valid = err;
9049 +               AuTraceErr(err);
9050 +               goto out;
9051 +       }
9052 +       inode = NULL;
9053 +       if (d_really_is_positive(dentry))
9054 +               inode = d_inode(dentry);
9055 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9056 +               err = -EINVAL;
9057 +               AuTraceErr(err);
9058 +               goto out_dgrade;
9059 +       }
9060 +       if (unlikely(au_dbrange_test(dentry))) {
9061 +               err = -EINVAL;
9062 +               AuTraceErr(err);
9063 +               goto out_dgrade;
9064 +       }
9065 +
9066 +       sigen = au_sigen(sb);
9067 +       if (au_digen_test(dentry, sigen)) {
9068 +               AuDebugOn(IS_ROOT(dentry));
9069 +               err = au_reval_dpath(dentry, sigen);
9070 +               if (unlikely(err)) {
9071 +                       AuTraceErr(err);
9072 +                       goto out_dgrade;
9073 +               }
9074 +       }
9075 +       di_downgrade_lock(dentry, AuLock_IR);
9076 +
9077 +       err = -EINVAL;
9078 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9079 +           && inode
9080 +           && !(inode->i_state && I_LINKABLE)
9081 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9082 +               AuTraceErr(err);
9083 +               goto out_inval;
9084 +       }
9085 +
9086 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9087 +       if (do_udba && inode) {
9088 +               aufs_bindex_t btop = au_ibtop(inode);
9089 +               struct inode *h_inode;
9090 +
9091 +               if (btop >= 0) {
9092 +                       h_inode = au_h_iptr(inode, btop);
9093 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9094 +                               AuTraceErr(err);
9095 +                               goto out_inval;
9096 +                       }
9097 +               }
9098 +       }
9099 +
9100 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9101 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9102 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9103 +               err = -EIO;
9104 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9105 +                     dentry, err);
9106 +       }
9107 +       goto out_inval;
9108 +
9109 +out_dgrade:
9110 +       di_downgrade_lock(dentry, AuLock_IR);
9111 +out_inval:
9112 +       aufs_read_unlock(dentry, AuLock_IR);
9113 +       AuTraceErr(err);
9114 +       valid = !err;
9115 +out:
9116 +       if (!valid) {
9117 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9118 +               d_drop(dentry);
9119 +       }
9120 +       return valid;
9121 +}
9122 +
9123 +static void aufs_d_release(struct dentry *dentry)
9124 +{
9125 +       if (au_di(dentry)) {
9126 +               au_di_fin(dentry);
9127 +               au_hn_di_reinit(dentry);
9128 +       }
9129 +}
9130 +
9131 +const struct dentry_operations aufs_dop = {
9132 +       .d_revalidate           = aufs_d_revalidate,
9133 +       .d_weak_revalidate      = aufs_d_revalidate,
9134 +       .d_release              = aufs_d_release
9135 +};
9136 +
9137 +/* aufs_dop without d_revalidate */
9138 +const struct dentry_operations aufs_dop_noreval = {
9139 +       .d_release              = aufs_d_release
9140 +};
9141 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9142 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9143 +++ linux/fs/aufs/dentry.h      2023-08-28 12:34:39.956636132 +0200
9144 @@ -0,0 +1,270 @@
9145 +/* SPDX-License-Identifier: GPL-2.0 */
9146 +/*
9147 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9148 + *
9149 + * This program is free software; you can redistribute it and/or modify
9150 + * it under the terms of the GNU General Public License as published by
9151 + * the Free Software Foundation; either version 2 of the License, or
9152 + * (at your option) any later version.
9153 + *
9154 + * This program is distributed in the hope that it will be useful,
9155 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9156 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9157 + * GNU General Public License for more details.
9158 + *
9159 + * You should have received a copy of the GNU General Public License
9160 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9161 + */
9162 +
9163 +/*
9164 + * lookup and dentry operations
9165 + */
9166 +
9167 +#ifndef __AUFS_DENTRY_H__
9168 +#define __AUFS_DENTRY_H__
9169 +
9170 +#ifdef __KERNEL__
9171 +
9172 +#include <linux/dcache.h>
9173 +#include "dirren.h"
9174 +#include "rwsem.h"
9175 +
9176 +struct au_hdentry {
9177 +       struct dentry           *hd_dentry;
9178 +       aufs_bindex_t           hd_id;
9179 +};
9180 +
9181 +struct au_dinfo {
9182 +       atomic_t                di_generation;
9183 +
9184 +       struct au_rwsem         di_rwsem;
9185 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9186 +       unsigned char           di_tmpfile; /* to allow the different name */
9187 +       struct au_hdentry       *di_hdentry;
9188 +       struct file             *di_htmpfile;
9189 +       struct rcu_head         rcu;
9190 +} ____cacheline_aligned_in_smp;
9191 +
9192 +/* ---------------------------------------------------------------------- */
9193 +
9194 +/* flags for au_lkup_dentry() */
9195 +#define AuLkup_ALLOW_NEG       1
9196 +#define AuLkup_IGNORE_PERM     (1 << 1)
9197 +#define AuLkup_DIRREN          (1 << 2)
9198 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9199 +#define au_fset_lkup(flags, name) \
9200 +       do { (flags) |= AuLkup_##name; } while (0)
9201 +#define au_fclr_lkup(flags, name) \
9202 +       do { (flags) &= ~AuLkup_##name; } while (0)
9203 +
9204 +#ifndef CONFIG_AUFS_DIRREN
9205 +#undef AuLkup_DIRREN
9206 +#define AuLkup_DIRREN 0
9207 +#endif
9208 +
9209 +struct au_do_lookup_args {
9210 +       unsigned int            flags;
9211 +       mode_t                  type;
9212 +       struct qstr             whname, *name;
9213 +       struct au_dr_lookup     dirren;
9214 +};
9215 +
9216 +/* ---------------------------------------------------------------------- */
9217 +
9218 +/* dentry.c */
9219 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9220 +struct au_branch;
9221 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
9222 +                              struct path *ppath);
9223 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9224 +               struct dentry *h_parent, struct au_branch *br);
9225 +
9226 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9227 +                  unsigned int flags);
9228 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9229 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9230 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9231 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9232 +
9233 +/* dinfo.c */
9234 +void au_di_init_once(void *_di);
9235 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9236 +void au_di_free(struct au_dinfo *dinfo);
9237 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9238 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9239 +int au_di_init(struct dentry *dentry);
9240 +void au_di_fin(struct dentry *dentry);
9241 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9242 +
9243 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9244 +void di_read_unlock(struct dentry *d, int flags);
9245 +void di_downgrade_lock(struct dentry *d, int flags);
9246 +void di_write_lock(struct dentry *d, unsigned int lsc);
9247 +void di_write_unlock(struct dentry *d);
9248 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9249 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9250 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9251 +
9252 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9253 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9254 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9255 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9256 +
9257 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9258 +                  struct dentry *h_dentry);
9259 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9260 +int au_dbrange_test(struct dentry *dentry);
9261 +void au_update_digen(struct dentry *dentry);
9262 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9263 +void au_update_dbtop(struct dentry *dentry);
9264 +void au_update_dbbot(struct dentry *dentry);
9265 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9266 +
9267 +/* ---------------------------------------------------------------------- */
9268 +
9269 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9270 +{
9271 +       return dentry->d_fsdata;
9272 +}
9273 +
9274 +/* ---------------------------------------------------------------------- */
9275 +
9276 +/* lock subclass for dinfo */
9277 +enum {
9278 +       AuLsc_DI_CHILD,         /* child first */
9279 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9280 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9281 +       AuLsc_DI_PARENT,
9282 +       AuLsc_DI_PARENT2,
9283 +       AuLsc_DI_PARENT3,
9284 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9285 +};
9286 +
9287 +/*
9288 + * di_read_lock_child, di_write_lock_child,
9289 + * di_read_lock_child2, di_write_lock_child2,
9290 + * di_read_lock_child3, di_write_lock_child3,
9291 + * di_read_lock_parent, di_write_lock_parent,
9292 + * di_read_lock_parent2, di_write_lock_parent2,
9293 + * di_read_lock_parent3, di_write_lock_parent3,
9294 + */
9295 +#define AuReadLockFunc(name, lsc) \
9296 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9297 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9298 +
9299 +#define AuWriteLockFunc(name, lsc) \
9300 +static inline void di_write_lock_##name(struct dentry *d) \
9301 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9302 +
9303 +#define AuRWLockFuncs(name, lsc) \
9304 +       AuReadLockFunc(name, lsc) \
9305 +       AuWriteLockFunc(name, lsc)
9306 +
9307 +AuRWLockFuncs(child, CHILD);
9308 +AuRWLockFuncs(child2, CHILD2);
9309 +AuRWLockFuncs(child3, CHILD3);
9310 +AuRWLockFuncs(parent, PARENT);
9311 +AuRWLockFuncs(parent2, PARENT2);
9312 +AuRWLockFuncs(parent3, PARENT3);
9313 +
9314 +#undef AuReadLockFunc
9315 +#undef AuWriteLockFunc
9316 +#undef AuRWLockFuncs
9317 +
9318 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9319 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9320 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9321 +
9322 +/* ---------------------------------------------------------------------- */
9323 +
9324 +/* todo: memory barrier? */
9325 +static inline unsigned int au_digen(struct dentry *d)
9326 +{
9327 +       return atomic_read(&au_di(d)->di_generation);
9328 +}
9329 +
9330 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9331 +{
9332 +       hdentry->hd_dentry = NULL;
9333 +}
9334 +
9335 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9336 +                                           aufs_bindex_t bindex)
9337 +{
9338 +       return di->di_hdentry + bindex;
9339 +}
9340 +
9341 +static inline void au_hdput(struct au_hdentry *hd)
9342 +{
9343 +       if (hd)
9344 +               dput(hd->hd_dentry);
9345 +}
9346 +
9347 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9348 +{
9349 +       DiMustAnyLock(dentry);
9350 +       return au_di(dentry)->di_btop;
9351 +}
9352 +
9353 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9354 +{
9355 +       DiMustAnyLock(dentry);
9356 +       return au_di(dentry)->di_bbot;
9357 +}
9358 +
9359 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9360 +{
9361 +       DiMustAnyLock(dentry);
9362 +       return au_di(dentry)->di_bwh;
9363 +}
9364 +
9365 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9366 +{
9367 +       DiMustAnyLock(dentry);
9368 +       return au_di(dentry)->di_bdiropq;
9369 +}
9370 +
9371 +/* todo: hard/soft set? */
9372 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9373 +{
9374 +       DiMustWriteLock(dentry);
9375 +       au_di(dentry)->di_btop = bindex;
9376 +}
9377 +
9378 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9379 +{
9380 +       DiMustWriteLock(dentry);
9381 +       au_di(dentry)->di_bbot = bindex;
9382 +}
9383 +
9384 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9385 +{
9386 +       DiMustWriteLock(dentry);
9387 +       /* dbwh can be outside of btop - bbot range */
9388 +       au_di(dentry)->di_bwh = bindex;
9389 +}
9390 +
9391 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9392 +{
9393 +       DiMustWriteLock(dentry);
9394 +       au_di(dentry)->di_bdiropq = bindex;
9395 +}
9396 +
9397 +/* ---------------------------------------------------------------------- */
9398 +
9399 +#ifdef CONFIG_AUFS_HNOTIFY
9400 +static inline void au_digen_dec(struct dentry *d)
9401 +{
9402 +       atomic_dec(&au_di(d)->di_generation);
9403 +}
9404 +
9405 +static inline void au_hn_di_reinit(struct dentry *dentry)
9406 +{
9407 +       dentry->d_fsdata = NULL;
9408 +}
9409 +#else
9410 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9411 +#endif /* CONFIG_AUFS_HNOTIFY */
9412 +
9413 +#endif /* __KERNEL__ */
9414 +#endif /* __AUFS_DENTRY_H__ */
9415 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9416 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9417 +++ linux/fs/aufs/dinfo.c       2022-12-17 09:21:34.796521861 +0100
9418 @@ -0,0 +1,555 @@
9419 +// SPDX-License-Identifier: GPL-2.0
9420 +/*
9421 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9422 + *
9423 + * This program is free software; you can redistribute it and/or modify
9424 + * it under the terms of the GNU General Public License as published by
9425 + * the Free Software Foundation; either version 2 of the License, or
9426 + * (at your option) any later version.
9427 + *
9428 + * This program is distributed in the hope that it will be useful,
9429 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9430 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9431 + * GNU General Public License for more details.
9432 + *
9433 + * You should have received a copy of the GNU General Public License
9434 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9435 + */
9436 +
9437 +/*
9438 + * dentry private data
9439 + */
9440 +
9441 +#include "aufs.h"
9442 +
9443 +void au_di_init_once(void *_dinfo)
9444 +{
9445 +       struct au_dinfo *dinfo = _dinfo;
9446 +
9447 +       au_rw_init(&dinfo->di_rwsem);
9448 +}
9449 +
9450 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9451 +{
9452 +       struct au_dinfo *dinfo;
9453 +       int nbr, i;
9454 +
9455 +       dinfo = au_cache_alloc_dinfo();
9456 +       if (unlikely(!dinfo))
9457 +               goto out;
9458 +
9459 +       nbr = au_sbbot(sb) + 1;
9460 +       if (nbr <= 0)
9461 +               nbr = 1;
9462 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9463 +       if (dinfo->di_hdentry) {
9464 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9465 +               dinfo->di_btop = -1;
9466 +               dinfo->di_bbot = -1;
9467 +               dinfo->di_bwh = -1;
9468 +               dinfo->di_bdiropq = -1;
9469 +               dinfo->di_tmpfile = 0;
9470 +               for (i = 0; i < nbr; i++)
9471 +                       dinfo->di_hdentry[i].hd_id = -1;
9472 +               dinfo->di_htmpfile = NULL;
9473 +               goto out;
9474 +       }
9475 +
9476 +       au_cache_free_dinfo(dinfo);
9477 +       dinfo = NULL;
9478 +
9479 +out:
9480 +       return dinfo;
9481 +}
9482 +
9483 +void au_di_free(struct au_dinfo *dinfo)
9484 +{
9485 +       struct au_hdentry *p;
9486 +       aufs_bindex_t bbot, bindex;
9487 +
9488 +       /* dentry may not be revalidated */
9489 +       bindex = dinfo->di_btop;
9490 +       if (bindex >= 0) {
9491 +               bbot = dinfo->di_bbot;
9492 +               p = au_hdentry(dinfo, bindex);
9493 +               while (bindex++ <= bbot)
9494 +                       au_hdput(p++);
9495 +       }
9496 +       au_kfree_try_rcu(dinfo->di_hdentry);
9497 +       au_cache_free_dinfo(dinfo);
9498 +}
9499 +
9500 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9501 +{
9502 +       struct au_hdentry *p;
9503 +       aufs_bindex_t bi;
9504 +
9505 +       AuRwMustWriteLock(&a->di_rwsem);
9506 +       AuRwMustWriteLock(&b->di_rwsem);
9507 +
9508 +#define DiSwap(v, name)                                \
9509 +       do {                                    \
9510 +               v = a->di_##name;               \
9511 +               a->di_##name = b->di_##name;    \
9512 +               b->di_##name = v;               \
9513 +       } while (0)
9514 +
9515 +       DiSwap(p, hdentry);
9516 +       DiSwap(bi, btop);
9517 +       DiSwap(bi, bbot);
9518 +       DiSwap(bi, bwh);
9519 +       DiSwap(bi, bdiropq);
9520 +       /* smp_mb(); */
9521 +
9522 +#undef DiSwap
9523 +}
9524 +
9525 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9526 +{
9527 +       AuRwMustWriteLock(&dst->di_rwsem);
9528 +       AuRwMustWriteLock(&src->di_rwsem);
9529 +
9530 +       dst->di_btop = src->di_btop;
9531 +       dst->di_bbot = src->di_bbot;
9532 +       dst->di_bwh = src->di_bwh;
9533 +       dst->di_bdiropq = src->di_bdiropq;
9534 +       /* smp_mb(); */
9535 +}
9536 +
9537 +int au_di_init(struct dentry *dentry)
9538 +{
9539 +       int err;
9540 +       struct super_block *sb;
9541 +       struct au_dinfo *dinfo;
9542 +
9543 +       err = 0;
9544 +       sb = dentry->d_sb;
9545 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9546 +       if (dinfo) {
9547 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9548 +               /* smp_mb(); */ /* atomic_set */
9549 +               dentry->d_fsdata = dinfo;
9550 +       } else
9551 +               err = -ENOMEM;
9552 +
9553 +       return err;
9554 +}
9555 +
9556 +void au_di_fin(struct dentry *dentry)
9557 +{
9558 +       struct au_dinfo *dinfo;
9559 +
9560 +       dinfo = au_di(dentry);
9561 +       AuRwDestroy(&dinfo->di_rwsem);
9562 +       au_di_free(dinfo);
9563 +}
9564 +
9565 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9566 +{
9567 +       int err, sz;
9568 +       struct au_hdentry *hdp;
9569 +
9570 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9571 +
9572 +       err = -ENOMEM;
9573 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9574 +       if (!sz)
9575 +               sz = sizeof(*hdp);
9576 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9577 +                          may_shrink);
9578 +       if (hdp) {
9579 +               dinfo->di_hdentry = hdp;
9580 +               err = 0;
9581 +       }
9582 +
9583 +       return err;
9584 +}
9585 +
9586 +/* ---------------------------------------------------------------------- */
9587 +
9588 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9589 +{
9590 +       switch (lsc) {
9591 +       case AuLsc_DI_CHILD:
9592 +               ii_write_lock_child(inode);
9593 +               break;
9594 +       case AuLsc_DI_CHILD2:
9595 +               ii_write_lock_child2(inode);
9596 +               break;
9597 +       case AuLsc_DI_CHILD3:
9598 +               ii_write_lock_child3(inode);
9599 +               break;
9600 +       case AuLsc_DI_PARENT:
9601 +               ii_write_lock_parent(inode);
9602 +               break;
9603 +       case AuLsc_DI_PARENT2:
9604 +               ii_write_lock_parent2(inode);
9605 +               break;
9606 +       case AuLsc_DI_PARENT3:
9607 +               ii_write_lock_parent3(inode);
9608 +               break;
9609 +       default:
9610 +               BUG();
9611 +       }
9612 +}
9613 +
9614 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9615 +{
9616 +       switch (lsc) {
9617 +       case AuLsc_DI_CHILD:
9618 +               ii_read_lock_child(inode);
9619 +               break;
9620 +       case AuLsc_DI_CHILD2:
9621 +               ii_read_lock_child2(inode);
9622 +               break;
9623 +       case AuLsc_DI_CHILD3:
9624 +               ii_read_lock_child3(inode);
9625 +               break;
9626 +       case AuLsc_DI_PARENT:
9627 +               ii_read_lock_parent(inode);
9628 +               break;
9629 +       case AuLsc_DI_PARENT2:
9630 +               ii_read_lock_parent2(inode);
9631 +               break;
9632 +       case AuLsc_DI_PARENT3:
9633 +               ii_read_lock_parent3(inode);
9634 +               break;
9635 +       default:
9636 +               BUG();
9637 +       }
9638 +}
9639 +
9640 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9641 +{
9642 +       struct inode *inode;
9643 +
9644 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9645 +       if (d_really_is_positive(d)) {
9646 +               inode = d_inode(d);
9647 +               if (au_ftest_lock(flags, IW))
9648 +                       do_ii_write_lock(inode, lsc);
9649 +               else if (au_ftest_lock(flags, IR))
9650 +                       do_ii_read_lock(inode, lsc);
9651 +       }
9652 +}
9653 +
9654 +void di_read_unlock(struct dentry *d, int flags)
9655 +{
9656 +       struct inode *inode;
9657 +
9658 +       if (d_really_is_positive(d)) {
9659 +               inode = d_inode(d);
9660 +               if (au_ftest_lock(flags, IW)) {
9661 +                       au_dbg_verify_dinode(d);
9662 +                       ii_write_unlock(inode);
9663 +               } else if (au_ftest_lock(flags, IR)) {
9664 +                       au_dbg_verify_dinode(d);
9665 +                       ii_read_unlock(inode);
9666 +               }
9667 +       }
9668 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9669 +}
9670 +
9671 +void di_downgrade_lock(struct dentry *d, int flags)
9672 +{
9673 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9674 +               ii_downgrade_lock(d_inode(d));
9675 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9676 +}
9677 +
9678 +void di_write_lock(struct dentry *d, unsigned int lsc)
9679 +{
9680 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9681 +       if (d_really_is_positive(d))
9682 +               do_ii_write_lock(d_inode(d), lsc);
9683 +}
9684 +
9685 +void di_write_unlock(struct dentry *d)
9686 +{
9687 +       au_dbg_verify_dinode(d);
9688 +       if (d_really_is_positive(d))
9689 +               ii_write_unlock(d_inode(d));
9690 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9691 +}
9692 +
9693 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9694 +{
9695 +       AuDebugOn(d1 == d2
9696 +                 || d_inode(d1) == d_inode(d2)
9697 +                 || d1->d_sb != d2->d_sb);
9698 +
9699 +       if ((isdir && au_test_subdir(d1, d2))
9700 +           || d1 < d2) {
9701 +               di_write_lock_child(d1);
9702 +               di_write_lock_child2(d2);
9703 +       } else {
9704 +               di_write_lock_child(d2);
9705 +               di_write_lock_child2(d1);
9706 +       }
9707 +}
9708 +
9709 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9710 +{
9711 +       AuDebugOn(d1 == d2
9712 +                 || d_inode(d1) == d_inode(d2)
9713 +                 || d1->d_sb != d2->d_sb);
9714 +
9715 +       if ((isdir && au_test_subdir(d1, d2))
9716 +           || d1 < d2) {
9717 +               di_write_lock_parent(d1);
9718 +               di_write_lock_parent2(d2);
9719 +       } else {
9720 +               di_write_lock_parent(d2);
9721 +               di_write_lock_parent2(d1);
9722 +       }
9723 +}
9724 +
9725 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9726 +{
9727 +       di_write_unlock(d1);
9728 +       if (d_inode(d1) == d_inode(d2))
9729 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9730 +       else
9731 +               di_write_unlock(d2);
9732 +}
9733 +
9734 +/* ---------------------------------------------------------------------- */
9735 +
9736 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9737 +{
9738 +       struct dentry *d;
9739 +
9740 +       DiMustAnyLock(dentry);
9741 +
9742 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9743 +               return NULL;
9744 +       AuDebugOn(bindex < 0);
9745 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9746 +       AuDebugOn(d && au_dcount(d) <= 0);
9747 +       return d;
9748 +}
9749 +
9750 +/*
9751 + * extended version of au_h_dptr().
9752 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9753 + * error.
9754 + */
9755 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9756 +{
9757 +       struct dentry *h_dentry;
9758 +       struct inode *inode, *h_inode;
9759 +
9760 +       AuDebugOn(d_really_is_negative(dentry));
9761 +
9762 +       h_dentry = NULL;
9763 +       if (au_dbtop(dentry) <= bindex
9764 +           && bindex <= au_dbbot(dentry))
9765 +               h_dentry = au_h_dptr(dentry, bindex);
9766 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9767 +               dget(h_dentry);
9768 +               goto out; /* success */
9769 +       }
9770 +
9771 +       inode = d_inode(dentry);
9772 +       AuDebugOn(bindex < au_ibtop(inode));
9773 +       AuDebugOn(au_ibbot(inode) < bindex);
9774 +       h_inode = au_h_iptr(inode, bindex);
9775 +       h_dentry = d_find_alias(h_inode);
9776 +       if (h_dentry) {
9777 +               if (!IS_ERR(h_dentry)) {
9778 +                       if (!au_d_linkable(h_dentry))
9779 +                               goto out; /* success */
9780 +                       dput(h_dentry);
9781 +               } else
9782 +                       goto out;
9783 +       }
9784 +
9785 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9786 +               h_dentry = au_plink_lkup(inode, bindex);
9787 +               AuDebugOn(!h_dentry);
9788 +               if (!IS_ERR(h_dentry)) {
9789 +                       if (!au_d_hashed_positive(h_dentry))
9790 +                               goto out; /* success */
9791 +                       dput(h_dentry);
9792 +                       h_dentry = NULL;
9793 +               }
9794 +       }
9795 +
9796 +out:
9797 +       AuDbgDentry(h_dentry);
9798 +       return h_dentry;
9799 +}
9800 +
9801 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9802 +{
9803 +       aufs_bindex_t bbot, bwh;
9804 +
9805 +       bbot = au_dbbot(dentry);
9806 +       if (0 <= bbot) {
9807 +               bwh = au_dbwh(dentry);
9808 +               if (!bwh)
9809 +                       return bwh;
9810 +               if (0 < bwh && bwh < bbot)
9811 +                       return bwh - 1;
9812 +       }
9813 +       return bbot;
9814 +}
9815 +
9816 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9817 +{
9818 +       aufs_bindex_t bbot, bopq;
9819 +
9820 +       bbot = au_dbtail(dentry);
9821 +       if (0 <= bbot) {
9822 +               bopq = au_dbdiropq(dentry);
9823 +               if (0 <= bopq && bopq < bbot)
9824 +                       bbot = bopq;
9825 +       }
9826 +       return bbot;
9827 +}
9828 +
9829 +/* ---------------------------------------------------------------------- */
9830 +
9831 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9832 +                  struct dentry *h_dentry)
9833 +{
9834 +       struct au_dinfo *dinfo;
9835 +       struct au_hdentry *hd;
9836 +       struct au_branch *br;
9837 +
9838 +       DiMustWriteLock(dentry);
9839 +
9840 +       dinfo = au_di(dentry);
9841 +       hd = au_hdentry(dinfo, bindex);
9842 +       au_hdput(hd);
9843 +       hd->hd_dentry = h_dentry;
9844 +       if (h_dentry) {
9845 +               br = au_sbr(dentry->d_sb, bindex);
9846 +               hd->hd_id = br->br_id;
9847 +       }
9848 +}
9849 +
9850 +int au_dbrange_test(struct dentry *dentry)
9851 +{
9852 +       int err;
9853 +       aufs_bindex_t btop, bbot;
9854 +
9855 +       err = 0;
9856 +       btop = au_dbtop(dentry);
9857 +       bbot = au_dbbot(dentry);
9858 +       if (btop >= 0)
9859 +               AuDebugOn(bbot < 0 && btop > bbot);
9860 +       else {
9861 +               err = -EIO;
9862 +               AuDebugOn(bbot >= 0);
9863 +       }
9864 +
9865 +       return err;
9866 +}
9867 +
9868 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9869 +{
9870 +       int err;
9871 +
9872 +       err = 0;
9873 +       if (unlikely(au_digen(dentry) != sigen
9874 +                    || au_iigen_test(d_inode(dentry), sigen)))
9875 +               err = -EIO;
9876 +
9877 +       return err;
9878 +}
9879 +
9880 +void au_update_digen(struct dentry *dentry)
9881 +{
9882 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9883 +       /* smp_mb(); */ /* atomic_set */
9884 +}
9885 +
9886 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9887 +{
9888 +       struct au_dinfo *dinfo;
9889 +       struct dentry *h_d;
9890 +       struct au_hdentry *hdp;
9891 +       aufs_bindex_t bindex, bbot;
9892 +
9893 +       DiMustWriteLock(dentry);
9894 +
9895 +       dinfo = au_di(dentry);
9896 +       if (!dinfo || dinfo->di_btop < 0)
9897 +               return;
9898 +
9899 +       if (do_put_zero) {
9900 +               bbot = dinfo->di_bbot;
9901 +               bindex = dinfo->di_btop;
9902 +               hdp = au_hdentry(dinfo, bindex);
9903 +               for (; bindex <= bbot; bindex++, hdp++) {
9904 +                       h_d = hdp->hd_dentry;
9905 +                       if (h_d && d_is_negative(h_d))
9906 +                               au_set_h_dptr(dentry, bindex, NULL);
9907 +               }
9908 +       }
9909 +
9910 +       dinfo->di_btop = 0;
9911 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9912 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9913 +               if (hdp->hd_dentry)
9914 +                       break;
9915 +       if (dinfo->di_btop > dinfo->di_bbot) {
9916 +               dinfo->di_btop = -1;
9917 +               dinfo->di_bbot = -1;
9918 +               return;
9919 +       }
9920 +
9921 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9922 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9923 +               if (hdp->hd_dentry)
9924 +                       break;
9925 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9926 +}
9927 +
9928 +void au_update_dbtop(struct dentry *dentry)
9929 +{
9930 +       aufs_bindex_t bindex, bbot;
9931 +       struct dentry *h_dentry;
9932 +
9933 +       bbot = au_dbbot(dentry);
9934 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9935 +               h_dentry = au_h_dptr(dentry, bindex);
9936 +               if (!h_dentry)
9937 +                       continue;
9938 +               if (d_is_positive(h_dentry)) {
9939 +                       au_set_dbtop(dentry, bindex);
9940 +                       return;
9941 +               }
9942 +               au_set_h_dptr(dentry, bindex, NULL);
9943 +       }
9944 +}
9945 +
9946 +void au_update_dbbot(struct dentry *dentry)
9947 +{
9948 +       aufs_bindex_t bindex, btop;
9949 +       struct dentry *h_dentry;
9950 +
9951 +       btop = au_dbtop(dentry);
9952 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9953 +               h_dentry = au_h_dptr(dentry, bindex);
9954 +               if (!h_dentry)
9955 +                       continue;
9956 +               if (d_is_positive(h_dentry)) {
9957 +                       au_set_dbbot(dentry, bindex);
9958 +                       return;
9959 +               }
9960 +               au_set_h_dptr(dentry, bindex, NULL);
9961 +       }
9962 +}
9963 +
9964 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9965 +{
9966 +       aufs_bindex_t bindex, bbot;
9967 +
9968 +       bbot = au_dbbot(dentry);
9969 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9970 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9971 +                       return bindex;
9972 +       return -1;
9973 +}
9974 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9975 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9976 +++ linux/fs/aufs/dir.c 2023-08-28 12:34:39.956636132 +0200
9977 @@ -0,0 +1,765 @@
9978 +// SPDX-License-Identifier: GPL-2.0
9979 +/*
9980 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9981 + *
9982 + * This program is free software; you can redistribute it and/or modify
9983 + * it under the terms of the GNU General Public License as published by
9984 + * the Free Software Foundation; either version 2 of the License, or
9985 + * (at your option) any later version.
9986 + *
9987 + * This program is distributed in the hope that it will be useful,
9988 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9989 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9990 + * GNU General Public License for more details.
9991 + *
9992 + * You should have received a copy of the GNU General Public License
9993 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9994 + */
9995 +
9996 +/*
9997 + * directory operations
9998 + */
9999 +
10000 +#include <linux/fs_stack.h>
10001 +#include <linux/iversion.h>
10002 +#include "aufs.h"
10003 +
10004 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
10005 +{
10006 +       unsigned int nlink;
10007 +
10008 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10009 +
10010 +       nlink = dir->i_nlink;
10011 +       nlink += h_dir->i_nlink - 2;
10012 +       if (h_dir->i_nlink < 2)
10013 +               nlink += 2;
10014 +       smp_mb(); /* for i_nlink */
10015 +       /* 0 can happen in revaliding */
10016 +       set_nlink(dir, nlink);
10017 +}
10018 +
10019 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10020 +{
10021 +       unsigned int nlink;
10022 +
10023 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10024 +
10025 +       nlink = dir->i_nlink;
10026 +       nlink -= h_dir->i_nlink - 2;
10027 +       if (h_dir->i_nlink < 2)
10028 +               nlink -= 2;
10029 +       smp_mb(); /* for i_nlink */
10030 +       /* nlink == 0 means the branch-fs is broken */
10031 +       set_nlink(dir, nlink);
10032 +}
10033 +
10034 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10035 +{
10036 +       loff_t sz;
10037 +       aufs_bindex_t bindex, bbot;
10038 +       struct file *h_file;
10039 +       struct dentry *h_dentry;
10040 +
10041 +       sz = 0;
10042 +       if (file) {
10043 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10044 +
10045 +               bbot = au_fbbot_dir(file);
10046 +               for (bindex = au_fbtop(file);
10047 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10048 +                    bindex++) {
10049 +                       h_file = au_hf_dir(file, bindex);
10050 +                       if (h_file && file_inode(h_file))
10051 +                               sz += vfsub_f_size_read(h_file);
10052 +               }
10053 +       } else {
10054 +               AuDebugOn(!dentry);
10055 +               AuDebugOn(!d_is_dir(dentry));
10056 +
10057 +               bbot = au_dbtaildir(dentry);
10058 +               for (bindex = au_dbtop(dentry);
10059 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10060 +                    bindex++) {
10061 +                       h_dentry = au_h_dptr(dentry, bindex);
10062 +                       if (h_dentry && d_is_positive(h_dentry))
10063 +                               sz += i_size_read(d_inode(h_dentry));
10064 +               }
10065 +       }
10066 +       if (sz < KMALLOC_MAX_SIZE)
10067 +               sz = roundup_pow_of_two(sz);
10068 +       if (sz > KMALLOC_MAX_SIZE)
10069 +               sz = KMALLOC_MAX_SIZE;
10070 +       else if (sz < NAME_MAX) {
10071 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10072 +               sz = AUFS_RDBLK_DEF;
10073 +       }
10074 +       return sz;
10075 +}
10076 +
10077 +struct au_dir_ts_arg {
10078 +       struct dentry *dentry;
10079 +       aufs_bindex_t brid;
10080 +};
10081 +
10082 +static void au_do_dir_ts(void *arg)
10083 +{
10084 +       struct au_dir_ts_arg *a = arg;
10085 +       struct au_dtime dt;
10086 +       struct path h_path;
10087 +       struct inode *dir, *h_dir;
10088 +       struct super_block *sb;
10089 +       struct au_branch *br;
10090 +       struct au_hinode *hdir;
10091 +       int err;
10092 +       aufs_bindex_t btop, bindex;
10093 +
10094 +       sb = a->dentry->d_sb;
10095 +       if (d_really_is_negative(a->dentry))
10096 +               goto out;
10097 +       /* no dir->i_mutex lock */
10098 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10099 +
10100 +       dir = d_inode(a->dentry);
10101 +       btop = au_ibtop(dir);
10102 +       bindex = au_br_index(sb, a->brid);
10103 +       if (bindex < btop)
10104 +               goto out_unlock;
10105 +
10106 +       br = au_sbr(sb, bindex);
10107 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10108 +       if (!h_path.dentry)
10109 +               goto out_unlock;
10110 +       h_path.mnt = au_br_mnt(br);
10111 +       au_dtime_store(&dt, a->dentry, &h_path);
10112 +
10113 +       br = au_sbr(sb, btop);
10114 +       if (!au_br_writable(br->br_perm))
10115 +               goto out_unlock;
10116 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10117 +       h_path.mnt = au_br_mnt(br);
10118 +       err = vfsub_mnt_want_write(h_path.mnt);
10119 +       if (err)
10120 +               goto out_unlock;
10121 +       hdir = au_hi(dir, btop);
10122 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10123 +       h_dir = au_h_iptr(dir, btop);
10124 +       if (h_dir->i_nlink
10125 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10126 +               dt.dt_h_path = h_path;
10127 +               au_dtime_revert(&dt);
10128 +       }
10129 +       au_hn_inode_unlock(hdir);
10130 +       vfsub_mnt_drop_write(h_path.mnt);
10131 +       au_cpup_attr_timesizes(dir);
10132 +
10133 +out_unlock:
10134 +       aufs_read_unlock(a->dentry, AuLock_DW);
10135 +out:
10136 +       dput(a->dentry);
10137 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10138 +       au_kfree_try_rcu(arg);
10139 +}
10140 +
10141 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10142 +{
10143 +       int perm, wkq_err;
10144 +       aufs_bindex_t btop;
10145 +       struct au_dir_ts_arg *arg;
10146 +       struct dentry *dentry;
10147 +       struct super_block *sb;
10148 +
10149 +       IMustLock(dir);
10150 +
10151 +       dentry = d_find_any_alias(dir);
10152 +       AuDebugOn(!dentry);
10153 +       sb = dentry->d_sb;
10154 +       btop = au_ibtop(dir);
10155 +       if (btop == bindex) {
10156 +               au_cpup_attr_timesizes(dir);
10157 +               goto out;
10158 +       }
10159 +
10160 +       perm = au_sbr_perm(sb, btop);
10161 +       if (!au_br_writable(perm))
10162 +               goto out;
10163 +
10164 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10165 +       if (!arg)
10166 +               goto out;
10167 +
10168 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10169 +       arg->brid = au_sbr_id(sb, bindex);
10170 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10171 +       if (unlikely(wkq_err)) {
10172 +               pr_err("wkq %d\n", wkq_err);
10173 +               dput(dentry);
10174 +               au_kfree_try_rcu(arg);
10175 +       }
10176 +
10177 +out:
10178 +       dput(dentry);
10179 +}
10180 +
10181 +/* ---------------------------------------------------------------------- */
10182 +
10183 +static int reopen_dir(struct file *file)
10184 +{
10185 +       int err;
10186 +       unsigned int flags;
10187 +       aufs_bindex_t bindex, btail, btop;
10188 +       struct dentry *dentry, *h_dentry;
10189 +       struct file *h_file;
10190 +
10191 +       /* open all lower dirs */
10192 +       dentry = file->f_path.dentry;
10193 +       btop = au_dbtop(dentry);
10194 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10195 +               au_set_h_fptr(file, bindex, NULL);
10196 +       au_set_fbtop(file, btop);
10197 +
10198 +       btail = au_dbtaildir(dentry);
10199 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10200 +               au_set_h_fptr(file, bindex, NULL);
10201 +       au_set_fbbot_dir(file, btail);
10202 +
10203 +       flags = vfsub_file_flags(file);
10204 +       for (bindex = btop; bindex <= btail; bindex++) {
10205 +               h_dentry = au_h_dptr(dentry, bindex);
10206 +               if (!h_dentry)
10207 +                       continue;
10208 +               h_file = au_hf_dir(file, bindex);
10209 +               if (h_file)
10210 +                       continue;
10211 +
10212 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10213 +               err = PTR_ERR(h_file);
10214 +               if (IS_ERR(h_file))
10215 +                       goto out; /* close all? */
10216 +               au_set_h_fptr(file, bindex, h_file);
10217 +       }
10218 +       au_update_figen(file);
10219 +       /* todo: necessary? */
10220 +       /* file->f_ra = h_file->f_ra; */
10221 +       err = 0;
10222 +
10223 +out:
10224 +       return err;
10225 +}
10226 +
10227 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10228 +{
10229 +       int err;
10230 +       aufs_bindex_t bindex, btail;
10231 +       struct dentry *dentry, *h_dentry;
10232 +       struct vfsmount *mnt;
10233 +
10234 +       FiMustWriteLock(file);
10235 +       AuDebugOn(h_file);
10236 +
10237 +       err = 0;
10238 +       mnt = file->f_path.mnt;
10239 +       dentry = file->f_path.dentry;
10240 +       file->f_version = inode_query_iversion(d_inode(dentry));
10241 +       bindex = au_dbtop(dentry);
10242 +       au_set_fbtop(file, bindex);
10243 +       btail = au_dbtaildir(dentry);
10244 +       au_set_fbbot_dir(file, btail);
10245 +       for (; !err && bindex <= btail; bindex++) {
10246 +               h_dentry = au_h_dptr(dentry, bindex);
10247 +               if (!h_dentry)
10248 +                       continue;
10249 +
10250 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10251 +               if (unlikely(err))
10252 +                       break;
10253 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10254 +               if (IS_ERR(h_file)) {
10255 +                       err = PTR_ERR(h_file);
10256 +                       break;
10257 +               }
10258 +               au_set_h_fptr(file, bindex, h_file);
10259 +       }
10260 +       au_update_figen(file);
10261 +       /* todo: necessary? */
10262 +       /* file->f_ra = h_file->f_ra; */
10263 +       if (!err)
10264 +               return 0; /* success */
10265 +
10266 +       /* close all */
10267 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10268 +               au_set_h_fptr(file, bindex, NULL);
10269 +       au_set_fbtop(file, -1);
10270 +       au_set_fbbot_dir(file, -1);
10271 +
10272 +       return err;
10273 +}
10274 +
10275 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10276 +                        struct file *file)
10277 +{
10278 +       int err;
10279 +       struct super_block *sb;
10280 +       struct au_fidir *fidir;
10281 +
10282 +       err = -ENOMEM;
10283 +       sb = file->f_path.dentry->d_sb;
10284 +       si_read_lock(sb, AuLock_FLUSH);
10285 +       fidir = au_fidir_alloc(sb);
10286 +       if (fidir) {
10287 +               struct au_do_open_args args = {
10288 +                       .open   = do_open_dir,
10289 +                       .fidir  = fidir
10290 +               };
10291 +               err = au_do_open(file, &args);
10292 +               if (unlikely(err))
10293 +                       au_kfree_rcu(fidir);
10294 +       }
10295 +       si_read_unlock(sb);
10296 +       return err;
10297 +}
10298 +
10299 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10300 +                           struct file *file)
10301 +{
10302 +       struct au_vdir *vdir_cache;
10303 +       struct au_finfo *finfo;
10304 +       struct au_fidir *fidir;
10305 +       struct au_hfile *hf;
10306 +       aufs_bindex_t bindex, bbot;
10307 +
10308 +       finfo = au_fi(file);
10309 +       fidir = finfo->fi_hdir;
10310 +       if (fidir) {
10311 +               au_hbl_del(&finfo->fi_hlist,
10312 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10313 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10314 +               if (vdir_cache)
10315 +                       au_vdir_free(vdir_cache);
10316 +
10317 +               bindex = finfo->fi_btop;
10318 +               if (bindex >= 0) {
10319 +                       hf = fidir->fd_hfile + bindex;
10320 +                       /*
10321 +                        * calls fput() instead of filp_close(),
10322 +                        * since no dnotify or lock for the lower file.
10323 +                        */
10324 +                       bbot = fidir->fd_bbot;
10325 +                       for (; bindex <= bbot; bindex++, hf++)
10326 +                               if (hf->hf_file)
10327 +                                       au_hfput(hf, /*execed*/0);
10328 +               }
10329 +               au_kfree_rcu(fidir);
10330 +               finfo->fi_hdir = NULL;
10331 +       }
10332 +       au_finfo_fin(file);
10333 +       return 0;
10334 +}
10335 +
10336 +/* ---------------------------------------------------------------------- */
10337 +
10338 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10339 +{
10340 +       int err;
10341 +       aufs_bindex_t bindex, bbot;
10342 +       struct file *h_file;
10343 +
10344 +       err = 0;
10345 +       bbot = au_fbbot_dir(file);
10346 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10347 +               h_file = au_hf_dir(file, bindex);
10348 +               if (h_file)
10349 +                       err = vfsub_flush(h_file, id);
10350 +       }
10351 +       return err;
10352 +}
10353 +
10354 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10355 +{
10356 +       return au_do_flush(file, id, au_do_flush_dir);
10357 +}
10358 +
10359 +/* ---------------------------------------------------------------------- */
10360 +
10361 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10362 +{
10363 +       int err;
10364 +       aufs_bindex_t bbot, bindex;
10365 +       struct inode *inode;
10366 +       struct super_block *sb;
10367 +
10368 +       err = 0;
10369 +       sb = dentry->d_sb;
10370 +       inode = d_inode(dentry);
10371 +       IMustLock(inode);
10372 +       bbot = au_dbbot(dentry);
10373 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10374 +               struct path h_path;
10375 +
10376 +               if (au_test_ro(sb, bindex, inode))
10377 +                       continue;
10378 +               h_path.dentry = au_h_dptr(dentry, bindex);
10379 +               if (!h_path.dentry)
10380 +                       continue;
10381 +
10382 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10383 +               err = vfsub_fsync(NULL, &h_path, datasync);
10384 +       }
10385 +
10386 +       return err;
10387 +}
10388 +
10389 +static int au_do_fsync_dir(struct file *file, int datasync)
10390 +{
10391 +       int err;
10392 +       aufs_bindex_t bbot, bindex;
10393 +       struct file *h_file;
10394 +       struct super_block *sb;
10395 +       struct inode *inode;
10396 +
10397 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10398 +       if (unlikely(err))
10399 +               goto out;
10400 +
10401 +       inode = file_inode(file);
10402 +       sb = inode->i_sb;
10403 +       bbot = au_fbbot_dir(file);
10404 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10405 +               h_file = au_hf_dir(file, bindex);
10406 +               if (!h_file || au_test_ro(sb, bindex, inode))
10407 +                       continue;
10408 +
10409 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10410 +       }
10411 +
10412 +out:
10413 +       return err;
10414 +}
10415 +
10416 +/*
10417 + * @file may be NULL
10418 + */
10419 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10420 +                         int datasync)
10421 +{
10422 +       int err;
10423 +       struct dentry *dentry;
10424 +       struct inode *inode;
10425 +       struct super_block *sb;
10426 +
10427 +       err = 0;
10428 +       dentry = file->f_path.dentry;
10429 +       inode = d_inode(dentry);
10430 +       inode_lock(inode);
10431 +       sb = dentry->d_sb;
10432 +       si_noflush_read_lock(sb);
10433 +       if (file)
10434 +               err = au_do_fsync_dir(file, datasync);
10435 +       else {
10436 +               di_write_lock_child(dentry);
10437 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10438 +       }
10439 +       au_cpup_attr_timesizes(inode);
10440 +       di_write_unlock(dentry);
10441 +       if (file)
10442 +               fi_write_unlock(file);
10443 +
10444 +       si_read_unlock(sb);
10445 +       inode_unlock(inode);
10446 +       return err;
10447 +}
10448 +
10449 +/* ---------------------------------------------------------------------- */
10450 +
10451 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10452 +{
10453 +       int err;
10454 +       struct dentry *dentry;
10455 +       struct inode *inode, *h_inode;
10456 +       struct super_block *sb;
10457 +
10458 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10459 +
10460 +       dentry = file->f_path.dentry;
10461 +       inode = d_inode(dentry);
10462 +       IMustLock(inode);
10463 +
10464 +       sb = dentry->d_sb;
10465 +       si_read_lock(sb, AuLock_FLUSH);
10466 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10467 +       if (unlikely(err))
10468 +               goto out;
10469 +       err = au_alive_dir(dentry);
10470 +       if (!err)
10471 +               err = au_vdir_init(file);
10472 +       di_downgrade_lock(dentry, AuLock_IR);
10473 +       if (unlikely(err))
10474 +               goto out_unlock;
10475 +
10476 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10477 +       if (!au_test_nfsd()) {
10478 +               err = au_vdir_fill_de(file, ctx);
10479 +               fsstack_copy_attr_atime(inode, h_inode);
10480 +       } else {
10481 +               /*
10482 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10483 +                * encode_fh() and others.
10484 +                */
10485 +               atomic_inc(&h_inode->i_count);
10486 +               di_read_unlock(dentry, AuLock_IR);
10487 +               si_read_unlock(sb);
10488 +               err = au_vdir_fill_de(file, ctx);
10489 +               fsstack_copy_attr_atime(inode, h_inode);
10490 +               fi_write_unlock(file);
10491 +               iput(h_inode);
10492 +
10493 +               AuTraceErr(err);
10494 +               return err;
10495 +       }
10496 +
10497 +out_unlock:
10498 +       di_read_unlock(dentry, AuLock_IR);
10499 +       fi_write_unlock(file);
10500 +out:
10501 +       si_read_unlock(sb);
10502 +       return err;
10503 +}
10504 +
10505 +/* ---------------------------------------------------------------------- */
10506 +
10507 +#define AuTestEmpty_WHONLY     1
10508 +#define AuTestEmpty_CALLED     (1 << 1)
10509 +#define AuTestEmpty_SHWH       (1 << 2)
10510 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10511 +#define au_fset_testempty(flags, name) \
10512 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10513 +#define au_fclr_testempty(flags, name) \
10514 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10515 +
10516 +#ifndef CONFIG_AUFS_SHWH
10517 +#undef AuTestEmpty_SHWH
10518 +#define AuTestEmpty_SHWH       0
10519 +#endif
10520 +
10521 +struct test_empty_arg {
10522 +       struct dir_context ctx;
10523 +       struct au_nhash *whlist;
10524 +       unsigned int flags;
10525 +       int err;
10526 +       aufs_bindex_t bindex;
10527 +};
10528 +
10529 +static bool test_empty_cb(struct dir_context *ctx, const char *__name,
10530 +                         int namelen, loff_t offset __maybe_unused, u64 ino,
10531 +                         unsigned int d_type)
10532 +{
10533 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10534 +                                                 ctx);
10535 +       char *name = (void *)__name;
10536 +
10537 +       arg->err = 0;
10538 +       au_fset_testempty(arg->flags, CALLED);
10539 +       /* smp_mb(); */
10540 +       if (name[0] == '.'
10541 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10542 +               goto out; /* success */
10543 +
10544 +       if (namelen <= AUFS_WH_PFX_LEN
10545 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10546 +               if (au_ftest_testempty(arg->flags, WHONLY)
10547 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10548 +                       arg->err = -ENOTEMPTY;
10549 +               goto out;
10550 +       }
10551 +
10552 +       name += AUFS_WH_PFX_LEN;
10553 +       namelen -= AUFS_WH_PFX_LEN;
10554 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10555 +               arg->err = au_nhash_append_wh
10556 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10557 +                        au_ftest_testempty(arg->flags, SHWH));
10558 +
10559 +out:
10560 +       /* smp_mb(); */
10561 +       AuTraceErr(arg->err);
10562 +       return !arg->err;
10563 +}
10564 +
10565 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10566 +{
10567 +       int err;
10568 +       struct file *h_file;
10569 +       struct au_branch *br;
10570 +
10571 +       h_file = au_h_open(dentry, arg->bindex,
10572 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10573 +                          /*file*/NULL, /*force_wr*/0);
10574 +       err = PTR_ERR(h_file);
10575 +       if (IS_ERR(h_file))
10576 +               goto out;
10577 +
10578 +       err = 0;
10579 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10580 +           && !file_inode(h_file)->i_nlink)
10581 +               goto out_put;
10582 +
10583 +       do {
10584 +               arg->err = 0;
10585 +               au_fclr_testempty(arg->flags, CALLED);
10586 +               /* smp_mb(); */
10587 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10588 +               if (err >= 0)
10589 +                       err = arg->err;
10590 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10591 +
10592 +out_put:
10593 +       fput(h_file);
10594 +       br = au_sbr(dentry->d_sb, arg->bindex);
10595 +       au_lcnt_dec(&br->br_nfiles);
10596 +out:
10597 +       return err;
10598 +}
10599 +
10600 +struct do_test_empty_args {
10601 +       int *errp;
10602 +       struct dentry *dentry;
10603 +       struct test_empty_arg *arg;
10604 +};
10605 +
10606 +static void call_do_test_empty(void *args)
10607 +{
10608 +       struct do_test_empty_args *a = args;
10609 +       *a->errp = do_test_empty(a->dentry, a->arg);
10610 +}
10611 +
10612 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10613 +{
10614 +       int err, wkq_err;
10615 +       struct dentry *h_dentry;
10616 +       struct inode *h_inode;
10617 +       struct mnt_idmap *h_idmap;
10618 +
10619 +       h_idmap = au_sbr_idmap(dentry->d_sb, arg->bindex);
10620 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10621 +       h_inode = d_inode(h_dentry);
10622 +       /* todo: i_mode changes anytime? */
10623 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10624 +       err = au_test_h_perm_sio(h_idmap, h_inode, MAY_EXEC | MAY_READ);
10625 +       inode_unlock_shared(h_inode);
10626 +       if (!err)
10627 +               err = do_test_empty(dentry, arg);
10628 +       else {
10629 +               struct do_test_empty_args args = {
10630 +                       .errp   = &err,
10631 +                       .dentry = dentry,
10632 +                       .arg    = arg
10633 +               };
10634 +               unsigned int flags = arg->flags;
10635 +
10636 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10637 +               if (unlikely(wkq_err))
10638 +                       err = wkq_err;
10639 +               arg->flags = flags;
10640 +       }
10641 +
10642 +       return err;
10643 +}
10644 +
10645 +int au_test_empty_lower(struct dentry *dentry)
10646 +{
10647 +       int err;
10648 +       unsigned int rdhash;
10649 +       aufs_bindex_t bindex, btop, btail;
10650 +       struct au_nhash whlist;
10651 +       struct test_empty_arg arg = {
10652 +               .ctx = {
10653 +                       .actor = test_empty_cb
10654 +               }
10655 +       };
10656 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10657 +
10658 +       SiMustAnyLock(dentry->d_sb);
10659 +
10660 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10661 +       if (!rdhash)
10662 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10663 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10664 +       if (unlikely(err))
10665 +               goto out;
10666 +
10667 +       arg.flags = 0;
10668 +       arg.whlist = &whlist;
10669 +       btop = au_dbtop(dentry);
10670 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10671 +               au_fset_testempty(arg.flags, SHWH);
10672 +       test_empty = do_test_empty;
10673 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10674 +               test_empty = sio_test_empty;
10675 +       arg.bindex = btop;
10676 +       err = test_empty(dentry, &arg);
10677 +       if (unlikely(err))
10678 +               goto out_whlist;
10679 +
10680 +       au_fset_testempty(arg.flags, WHONLY);
10681 +       btail = au_dbtaildir(dentry);
10682 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10683 +               struct dentry *h_dentry;
10684 +
10685 +               h_dentry = au_h_dptr(dentry, bindex);
10686 +               if (h_dentry && d_is_positive(h_dentry)) {
10687 +                       arg.bindex = bindex;
10688 +                       err = test_empty(dentry, &arg);
10689 +               }
10690 +       }
10691 +
10692 +out_whlist:
10693 +       au_nhash_wh_free(&whlist);
10694 +out:
10695 +       return err;
10696 +}
10697 +
10698 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10699 +{
10700 +       int err;
10701 +       struct test_empty_arg arg = {
10702 +               .ctx = {
10703 +                       .actor = test_empty_cb
10704 +               }
10705 +       };
10706 +       aufs_bindex_t bindex, btail;
10707 +
10708 +       err = 0;
10709 +       arg.whlist = whlist;
10710 +       arg.flags = AuTestEmpty_WHONLY;
10711 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10712 +               au_fset_testempty(arg.flags, SHWH);
10713 +       btail = au_dbtaildir(dentry);
10714 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10715 +               struct dentry *h_dentry;
10716 +
10717 +               h_dentry = au_h_dptr(dentry, bindex);
10718 +               if (h_dentry && d_is_positive(h_dentry)) {
10719 +                       arg.bindex = bindex;
10720 +                       err = sio_test_empty(dentry, &arg);
10721 +               }
10722 +       }
10723 +
10724 +       return err;
10725 +}
10726 +
10727 +/* ---------------------------------------------------------------------- */
10728 +
10729 +const struct file_operations aufs_dir_fop = {
10730 +       .owner          = THIS_MODULE,
10731 +       .llseek         = default_llseek,
10732 +       .read           = generic_read_dir,
10733 +       .iterate_shared = aufs_iterate_shared,
10734 +       .unlocked_ioctl = aufs_ioctl_dir,
10735 +#ifdef CONFIG_COMPAT
10736 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10737 +#endif
10738 +       .open           = aufs_open_dir,
10739 +       .release        = aufs_release_dir,
10740 +       .flush          = aufs_flush_dir,
10741 +       .fsync          = aufs_fsync_dir
10742 +};
10743 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10744 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10745 +++ linux/fs/aufs/dir.h 2022-11-05 23:02:18.962555950 +0100
10746 @@ -0,0 +1,134 @@
10747 +/* SPDX-License-Identifier: GPL-2.0 */
10748 +/*
10749 + * Copyright (C) 2005-2022 Junjiro R. Okajima
10750 + *
10751 + * This program is free software; you can redistribute it and/or modify
10752 + * it under the terms of the GNU General Public License as published by
10753 + * the Free Software Foundation; either version 2 of the License, or
10754 + * (at your option) any later version.
10755 + *
10756 + * This program is distributed in the hope that it will be useful,
10757 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10758 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10759 + * GNU General Public License for more details.
10760 + *
10761 + * You should have received a copy of the GNU General Public License
10762 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10763 + */
10764 +
10765 +/*
10766 + * directory operations
10767 + */
10768 +
10769 +#ifndef __AUFS_DIR_H__
10770 +#define __AUFS_DIR_H__
10771 +
10772 +#ifdef __KERNEL__
10773 +
10774 +#include <linux/fs.h>
10775 +
10776 +/* ---------------------------------------------------------------------- */
10777 +
10778 +/* need to be faster and smaller */
10779 +
10780 +struct au_nhash {
10781 +       unsigned int            nh_num;
10782 +       struct hlist_head       *nh_head;
10783 +};
10784 +
10785 +struct au_vdir_destr {
10786 +       unsigned char   len;
10787 +       unsigned char   name[];
10788 +} __packed;
10789 +
10790 +struct au_vdir_dehstr {
10791 +       struct hlist_node       hash;
10792 +       struct au_vdir_destr    *str;
10793 +       struct rcu_head         rcu;
10794 +} ____cacheline_aligned_in_smp;
10795 +
10796 +struct au_vdir_de {
10797 +       ino_t                   de_ino;
10798 +       unsigned char           de_type;
10799 +       /* caution: packed */
10800 +       struct au_vdir_destr    de_str;
10801 +} __packed;
10802 +
10803 +struct au_vdir_wh {
10804 +       struct hlist_node       wh_hash;
10805 +#ifdef CONFIG_AUFS_SHWH
10806 +       ino_t                   wh_ino;
10807 +       aufs_bindex_t           wh_bindex;
10808 +       unsigned char           wh_type;
10809 +#else
10810 +       aufs_bindex_t           wh_bindex;
10811 +#endif
10812 +       /* caution: packed */
10813 +       struct au_vdir_destr    wh_str;
10814 +} __packed;
10815 +
10816 +union au_vdir_deblk_p {
10817 +       unsigned char           *deblk;
10818 +       struct au_vdir_de       *de;
10819 +};
10820 +
10821 +struct au_vdir {
10822 +       unsigned char   **vd_deblk;
10823 +       unsigned long   vd_nblk;
10824 +       struct {
10825 +               unsigned long           ul;
10826 +               union au_vdir_deblk_p   p;
10827 +       } vd_last;
10828 +
10829 +       u64             vd_version;
10830 +       unsigned int    vd_deblk_sz;
10831 +       unsigned long   vd_jiffy;
10832 +       struct rcu_head rcu;
10833 +} ____cacheline_aligned_in_smp;
10834 +
10835 +/* ---------------------------------------------------------------------- */
10836 +
10837 +/* dir.c */
10838 +extern const struct file_operations aufs_dir_fop;
10839 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10840 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10841 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10842 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10843 +int au_test_empty_lower(struct dentry *dentry);
10844 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10845 +
10846 +/* vdir.c */
10847 +unsigned int au_rdhash_est(loff_t sz);
10848 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10849 +void au_nhash_wh_free(struct au_nhash *whlist);
10850 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10851 +                           int limit);
10852 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10853 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10854 +                      unsigned int d_type, aufs_bindex_t bindex,
10855 +                      unsigned char shwh);
10856 +void au_vdir_free(struct au_vdir *vdir);
10857 +int au_vdir_init(struct file *file);
10858 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10859 +
10860 +/* ioctl.c */
10861 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10862 +
10863 +#ifdef CONFIG_AUFS_RDU
10864 +/* rdu.c */
10865 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10866 +#ifdef CONFIG_COMPAT
10867 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10868 +                        unsigned long arg);
10869 +#endif
10870 +#else
10871 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10872 +       unsigned int cmd, unsigned long arg)
10873 +#ifdef CONFIG_COMPAT
10874 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10875 +       unsigned int cmd, unsigned long arg)
10876 +#endif
10877 +#endif
10878 +
10879 +#endif /* __KERNEL__ */
10880 +#endif /* __AUFS_DIR_H__ */
10881 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10882 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10883 +++ linux/fs/aufs/dirren.c      2022-11-05 23:02:18.962555950 +0100
10884 @@ -0,0 +1,1315 @@
10885 +// SPDX-License-Identifier: GPL-2.0
10886 +/*
10887 + * Copyright (C) 2017-2022 Junjiro R. Okajima
10888 + *
10889 + * This program is free software; you can redistribute it and/or modify
10890 + * it under the terms of the GNU General Public License as published by
10891 + * the Free Software Foundation; either version 2 of the License, or
10892 + * (at your option) any later version.
10893 + *
10894 + * This program is distributed in the hope that it will be useful,
10895 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10896 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10897 + * GNU General Public License for more details.
10898 + *
10899 + * You should have received a copy of the GNU General Public License
10900 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10901 + */
10902 +
10903 +/*
10904 + * special handling in renaming a directory
10905 + * in order to support looking-up the before-renamed name on the lower readonly
10906 + * branches
10907 + */
10908 +
10909 +#include <linux/byteorder/generic.h>
10910 +#include "aufs.h"
10911 +
10912 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10913 +{
10914 +       int idx;
10915 +
10916 +       idx = au_dr_ihash(ent->dr_h_ino);
10917 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10918 +}
10919 +
10920 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10921 +{
10922 +       int ret, i;
10923 +       struct hlist_bl_head *hbl;
10924 +
10925 +       ret = 1;
10926 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10927 +               hbl = dr->dr_h_ino + i;
10928 +               hlist_bl_lock(hbl);
10929 +               ret &= hlist_bl_empty(hbl);
10930 +               hlist_bl_unlock(hbl);
10931 +       }
10932 +
10933 +       return ret;
10934 +}
10935 +
10936 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10937 +{
10938 +       struct au_dr_hino *found, *ent;
10939 +       struct hlist_bl_head *hbl;
10940 +       struct hlist_bl_node *pos;
10941 +       int idx;
10942 +
10943 +       found = NULL;
10944 +       idx = au_dr_ihash(ino);
10945 +       hbl = dr->dr_h_ino + idx;
10946 +       hlist_bl_lock(hbl);
10947 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10948 +               if (ent->dr_h_ino == ino) {
10949 +                       found = ent;
10950 +                       break;
10951 +               }
10952 +       hlist_bl_unlock(hbl);
10953 +
10954 +       return found;
10955 +}
10956 +
10957 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10958 +                       struct au_dr_hino *add_ent)
10959 +{
10960 +       int found, idx;
10961 +       struct hlist_bl_head *hbl;
10962 +       struct hlist_bl_node *pos;
10963 +       struct au_dr_hino *ent;
10964 +
10965 +       found = 0;
10966 +       idx = au_dr_ihash(ino);
10967 +       hbl = dr->dr_h_ino + idx;
10968 +#if 0 /* debug print */
10969 +       {
10970 +               struct hlist_bl_node *tmp;
10971 +
10972 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10973 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10974 +       }
10975 +#endif
10976 +       hlist_bl_lock(hbl);
10977 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10978 +               if (ent->dr_h_ino == ino) {
10979 +                       found = 1;
10980 +                       break;
10981 +               }
10982 +       if (!found && add_ent)
10983 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10984 +       hlist_bl_unlock(hbl);
10985 +
10986 +       if (!found && add_ent)
10987 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10988 +
10989 +       return found;
10990 +}
10991 +
10992 +void au_dr_hino_free(struct au_dr_br *dr)
10993 +{
10994 +       int i;
10995 +       struct hlist_bl_head *hbl;
10996 +       struct hlist_bl_node *pos, *tmp;
10997 +       struct au_dr_hino *ent;
10998 +
10999 +       /* SiMustWriteLock(sb); */
11000 +
11001 +       for (i = 0; i < AuDirren_NHASH; i++) {
11002 +               hbl = dr->dr_h_ino + i;
11003 +               /* no spinlock since sbinfo must be write-locked */
11004 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11005 +                       au_kfree_rcu(ent);
11006 +               INIT_HLIST_BL_HEAD(hbl);
11007 +       }
11008 +}
11009 +
11010 +/* returns the number of inodes or an error */
11011 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11012 +                           struct file *hinofile)
11013 +{
11014 +       int err, i;
11015 +       ssize_t ssz;
11016 +       loff_t pos, oldsize;
11017 +       __be64 u64;
11018 +       struct inode *hinoinode;
11019 +       struct hlist_bl_head *hbl;
11020 +       struct hlist_bl_node *n1, *n2;
11021 +       struct au_dr_hino *ent;
11022 +
11023 +       SiMustWriteLock(sb);
11024 +       AuDebugOn(!au_br_writable(br->br_perm));
11025 +
11026 +       hinoinode = file_inode(hinofile);
11027 +       oldsize = i_size_read(hinoinode);
11028 +
11029 +       err = 0;
11030 +       pos = 0;
11031 +       hbl = br->br_dirren.dr_h_ino;
11032 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11033 +               /* no bit-lock since sbinfo must be write-locked */
11034 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11035 +                       AuDbg("hi%llu, %pD2\n",
11036 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11037 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11038 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11039 +                       if (ssz == sizeof(u64))
11040 +                               continue;
11041 +
11042 +                       /* write error */
11043 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11044 +                       err = -ENOSPC;
11045 +                       if (ssz < 0)
11046 +                               err = ssz;
11047 +                       break;
11048 +               }
11049 +       }
11050 +       /* regardless the error */
11051 +       if (pos < oldsize) {
11052 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11053 +               AuTraceErr(err);
11054 +       }
11055 +
11056 +       AuTraceErr(err);
11057 +       return err;
11058 +}
11059 +
11060 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11061 +{
11062 +       int err, hidx;
11063 +       ssize_t ssz;
11064 +       size_t sz, n;
11065 +       loff_t pos;
11066 +       uint64_t u64;
11067 +       struct au_dr_hino *ent;
11068 +       struct inode *hinoinode;
11069 +       struct hlist_bl_head *hbl;
11070 +
11071 +       err = 0;
11072 +       pos = 0;
11073 +       hbl = dr->dr_h_ino;
11074 +       hinoinode = file_inode(hinofile);
11075 +       sz = i_size_read(hinoinode);
11076 +       AuDebugOn(sz % sizeof(u64));
11077 +       n = sz / sizeof(u64);
11078 +       while (n--) {
11079 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11080 +               if (unlikely(ssz != sizeof(u64))) {
11081 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11082 +                       err = -EINVAL;
11083 +                       if (ssz < 0)
11084 +                               err = ssz;
11085 +                       goto out_free;
11086 +               }
11087 +
11088 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11089 +               if (!ent) {
11090 +                       err = -ENOMEM;
11091 +                       AuTraceErr(err);
11092 +                       goto out_free;
11093 +               }
11094 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11095 +               AuDbg("hi%llu, %pD2\n",
11096 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11097 +               hidx = au_dr_ihash(ent->dr_h_ino);
11098 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11099 +       }
11100 +       goto out; /* success */
11101 +
11102 +out_free:
11103 +       au_dr_hino_free(dr);
11104 +out:
11105 +       AuTraceErr(err);
11106 +       return err;
11107 +}
11108 +
11109 +/*
11110 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11111 + * @path is a switch to distinguish load and store.
11112 + */
11113 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11114 +                     struct au_branch *br, const struct path *path)
11115 +{
11116 +       int err, flags;
11117 +       unsigned char load, suspend;
11118 +       struct file *hinofile;
11119 +       struct au_hinode *hdir;
11120 +       struct inode *dir, *delegated;
11121 +       struct path hinopath;
11122 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11123 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11124 +
11125 +       AuDebugOn(bindex < 0 && !br);
11126 +       AuDebugOn(bindex >= 0 && br);
11127 +
11128 +       err = -EINVAL;
11129 +       suspend = !br;
11130 +       if (suspend)
11131 +               br = au_sbr(sb, bindex);
11132 +       load = !!path;
11133 +       if (!load) {
11134 +               path = &br->br_path;
11135 +               AuDebugOn(!au_br_writable(br->br_perm));
11136 +               if (unlikely(!au_br_writable(br->br_perm)))
11137 +                       goto out;
11138 +       }
11139 +
11140 +       hdir = NULL;
11141 +       if (suspend) {
11142 +               dir = d_inode(sb->s_root);
11143 +               hdir = au_hinode(au_ii(dir), bindex);
11144 +               dir = hdir->hi_inode;
11145 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11146 +       } else {
11147 +               dir = d_inode(path->dentry);
11148 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11149 +       }
11150 +       hinopath.mnt = path->mnt;
11151 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11152 +       err = PTR_ERR(hinopath.dentry);
11153 +       if (IS_ERR(hinopath.dentry))
11154 +               goto out_unlock;
11155 +
11156 +       err = 0;
11157 +       flags = O_RDONLY;
11158 +       if (load) {
11159 +               if (d_is_negative(hinopath.dentry))
11160 +                       goto out_dput; /* success */
11161 +       } else {
11162 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11163 +                       if (d_is_positive(hinopath.dentry)) {
11164 +                               delegated = NULL;
11165 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11166 +                                                  /*force*/0);
11167 +                               AuTraceErr(err);
11168 +                               if (unlikely(err))
11169 +                                       pr_err("ignored err %d, %pd2\n",
11170 +                                              err, hinopath.dentry);
11171 +                               if (unlikely(err == -EWOULDBLOCK))
11172 +                                       iput(delegated);
11173 +                               err = 0;
11174 +                       }
11175 +                       goto out_dput;
11176 +               } else if (!d_is_positive(hinopath.dentry)) {
11177 +                       err = vfsub_create(dir, &hinopath, 0600,
11178 +                                          /*want_excl*/false);
11179 +                       AuTraceErr(err);
11180 +                       if (unlikely(err))
11181 +                               goto out_dput;
11182 +               }
11183 +               flags = O_WRONLY;
11184 +       }
11185 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11186 +       if (suspend)
11187 +               au_hn_inode_unlock(hdir);
11188 +       else
11189 +               inode_unlock(dir);
11190 +       dput(hinopath.dentry);
11191 +       AuTraceErrPtr(hinofile);
11192 +       if (IS_ERR(hinofile)) {
11193 +               err = PTR_ERR(hinofile);
11194 +               goto out;
11195 +       }
11196 +
11197 +       if (load)
11198 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11199 +       else
11200 +               err = au_dr_hino_store(sb, br, hinofile);
11201 +       fput(hinofile);
11202 +       goto out;
11203 +
11204 +out_dput:
11205 +       dput(hinopath.dentry);
11206 +out_unlock:
11207 +       if (suspend)
11208 +               au_hn_inode_unlock(hdir);
11209 +       else
11210 +               inode_unlock(dir);
11211 +out:
11212 +       AuTraceErr(err);
11213 +       return err;
11214 +}
11215 +
11216 +/* ---------------------------------------------------------------------- */
11217 +
11218 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11219 +{
11220 +       int err;
11221 +       struct kstatfs kstfs;
11222 +       dev_t dev;
11223 +       struct dentry *dentry;
11224 +       struct super_block *sb;
11225 +
11226 +       err = vfs_statfs((void *)path, &kstfs);
11227 +       AuTraceErr(err);
11228 +       if (unlikely(err))
11229 +               goto out;
11230 +
11231 +       /* todo: support for UUID */
11232 +
11233 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11234 +               brid->type = AuBrid_FSID;
11235 +               brid->fsid = kstfs.f_fsid;
11236 +       } else {
11237 +               dentry = path->dentry;
11238 +               sb = dentry->d_sb;
11239 +               dev = sb->s_dev;
11240 +               if (dev) {
11241 +                       brid->type = AuBrid_DEV;
11242 +                       brid->dev = dev;
11243 +               }
11244 +       }
11245 +
11246 +out:
11247 +       return err;
11248 +}
11249 +
11250 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11251 +                 const struct path *path)
11252 +{
11253 +       int err, i;
11254 +       struct au_dr_br *dr;
11255 +       struct hlist_bl_head *hbl;
11256 +
11257 +       dr = &br->br_dirren;
11258 +       hbl = dr->dr_h_ino;
11259 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11260 +               INIT_HLIST_BL_HEAD(hbl);
11261 +
11262 +       err = au_dr_brid_init(&dr->dr_brid, path);
11263 +       if (unlikely(err))
11264 +               goto out;
11265 +
11266 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11267 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11268 +
11269 +out:
11270 +       AuTraceErr(err);
11271 +       return err;
11272 +}
11273 +
11274 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11275 +{
11276 +       int err;
11277 +
11278 +       err = 0;
11279 +       if (au_br_writable(br->br_perm))
11280 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11281 +       if (!err)
11282 +               au_dr_hino_free(&br->br_dirren);
11283 +
11284 +       return err;
11285 +}
11286 +
11287 +/* ---------------------------------------------------------------------- */
11288 +
11289 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11290 +                      char *buf, size_t sz)
11291 +{
11292 +       int err;
11293 +       unsigned int major, minor;
11294 +       char *p;
11295 +
11296 +       p = buf;
11297 +       err = snprintf(p, sz, "%d_", brid->type);
11298 +       AuDebugOn(err > sz);
11299 +       p += err;
11300 +       sz -= err;
11301 +       switch (brid->type) {
11302 +       case AuBrid_Unset:
11303 +               return -EINVAL;
11304 +       case AuBrid_UUID:
11305 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11306 +               break;
11307 +       case AuBrid_FSID:
11308 +               err = snprintf(p, sz, "%08x-%08x",
11309 +                              brid->fsid.val[0], brid->fsid.val[1]);
11310 +               break;
11311 +       case AuBrid_DEV:
11312 +               major = MAJOR(brid->dev);
11313 +               minor = MINOR(brid->dev);
11314 +               if (major <= 0xff && minor <= 0xff)
11315 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11316 +               else
11317 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11318 +               break;
11319 +       }
11320 +       AuDebugOn(err > sz);
11321 +       p += err;
11322 +       sz -= err;
11323 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11324 +       AuDebugOn(err > sz);
11325 +       p += err;
11326 +       sz -= err;
11327 +
11328 +       return p - buf;
11329 +}
11330 +
11331 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11332 +{
11333 +       int rlen;
11334 +       struct dentry *br_dentry;
11335 +       struct inode *br_inode;
11336 +
11337 +       br_dentry = au_br_dentry(br);
11338 +       br_inode = d_inode(br_dentry);
11339 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11340 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11341 +       AuDebugOn(rlen > len);
11342 +
11343 +       return rlen;
11344 +}
11345 +
11346 +/* ---------------------------------------------------------------------- */
11347 +
11348 +/*
11349 + * from the given @h_dentry, construct drinfo at @*fdata.
11350 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11351 + * @allocated.
11352 + */
11353 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11354 +                              struct dentry *h_dentry,
11355 +                              unsigned char *allocated)
11356 +{
11357 +       int err, v;
11358 +       struct au_drinfo_fdata *f, *p;
11359 +       struct au_drinfo *drinfo;
11360 +       struct inode *h_inode;
11361 +       struct qstr *qname;
11362 +
11363 +       err = 0;
11364 +       f = *fdata;
11365 +       h_inode = d_inode(h_dentry);
11366 +       qname = &h_dentry->d_name;
11367 +       drinfo = &f->drinfo;
11368 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11369 +       drinfo->oldnamelen = qname->len;
11370 +       if (*allocated < sizeof(*f) + qname->len) {
11371 +               v = roundup_pow_of_two(*allocated + qname->len);
11372 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11373 +               if (unlikely(!p)) {
11374 +                       err = -ENOMEM;
11375 +                       AuTraceErr(err);
11376 +                       goto out;
11377 +               }
11378 +               f = p;
11379 +               *fdata = f;
11380 +               *allocated = v;
11381 +               drinfo = &f->drinfo;
11382 +       }
11383 +       memcpy(drinfo->oldname, qname->name, qname->len);
11384 +       AuDbg("i%llu, %.*s\n",
11385 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11386 +             drinfo->oldname);
11387 +
11388 +out:
11389 +       AuTraceErr(err);
11390 +       return err;
11391 +}
11392 +
11393 +/* callers have to free the return value */
11394 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11395 +{
11396 +       struct au_drinfo *ret, *drinfo;
11397 +       struct au_drinfo_fdata fdata;
11398 +       int len;
11399 +       loff_t pos;
11400 +       ssize_t ssz;
11401 +
11402 +       ret = ERR_PTR(-EIO);
11403 +       pos = 0;
11404 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11405 +       if (unlikely(ssz != sizeof(fdata))) {
11406 +               AuIOErr("ssz %zd, %u, %pD2\n",
11407 +                       ssz, (unsigned int)sizeof(fdata), file);
11408 +               goto out;
11409 +       }
11410 +
11411 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11412 +       switch (fdata.magic) {
11413 +       case AUFS_DRINFO_MAGIC_V1:
11414 +               break;
11415 +       default:
11416 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11417 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11418 +               goto out;
11419 +       }
11420 +
11421 +       drinfo = &fdata.drinfo;
11422 +       len = drinfo->oldnamelen;
11423 +       if (!len) {
11424 +               AuIOErr("broken drinfo %pD2\n", file);
11425 +               goto out;
11426 +       }
11427 +
11428 +       ret = NULL;
11429 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11430 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11431 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11432 +                     (unsigned long long)drinfo->ino,
11433 +                     (unsigned long long)h_ino, file);
11434 +               goto out; /* success */
11435 +       }
11436 +
11437 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11438 +       if (unlikely(!ret)) {
11439 +               ret = ERR_PTR(-ENOMEM);
11440 +               AuTraceErrPtr(ret);
11441 +               goto out;
11442 +       }
11443 +
11444 +       *ret = *drinfo;
11445 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11446 +       if (unlikely(ssz != len)) {
11447 +               au_kfree_rcu(ret);
11448 +               ret = ERR_PTR(-EIO);
11449 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11450 +               goto out;
11451 +       }
11452 +
11453 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11454 +
11455 +out:
11456 +       return ret;
11457 +}
11458 +
11459 +/* ---------------------------------------------------------------------- */
11460 +
11461 +/* in order to be revertible */
11462 +struct au_drinfo_rev_elm {
11463 +       int                     created;
11464 +       struct dentry           *info_dentry;
11465 +       struct au_drinfo        *info_last;
11466 +};
11467 +
11468 +struct au_drinfo_rev {
11469 +       unsigned char                   already;
11470 +       aufs_bindex_t                   nelm;
11471 +       struct au_drinfo_rev_elm        elm[];
11472 +};
11473 +
11474 +/* todo: isn't it too large? */
11475 +struct au_drinfo_store {
11476 +       struct path h_ppath;
11477 +       struct dentry *h_dentry;
11478 +       struct au_drinfo_fdata *fdata;
11479 +       char *infoname;                 /* inside of whname, just after PFX */
11480 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11481 +       aufs_bindex_t btgt, btail;
11482 +       unsigned char no_sio,
11483 +               allocated,              /* current size of *fdata */
11484 +               infonamelen,            /* room size for p */
11485 +               whnamelen,              /* length of the generated name */
11486 +               renameback;             /* renamed back */
11487 +};
11488 +
11489 +/* on rename(2) error, the caller should revert it using @elm */
11490 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11491 +                             struct au_drinfo_rev_elm *elm)
11492 +{
11493 +       int err, len;
11494 +       ssize_t ssz;
11495 +       loff_t pos;
11496 +       struct path infopath = {
11497 +               .mnt = w->h_ppath.mnt
11498 +       };
11499 +       struct inode *h_dir, *h_inode, *delegated;
11500 +       struct file *infofile;
11501 +       struct qstr *qname;
11502 +
11503 +       AuDebugOn(elm
11504 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11505 +
11506 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11507 +                                              w->whnamelen);
11508 +       AuTraceErrPtr(infopath.dentry);
11509 +       if (IS_ERR(infopath.dentry)) {
11510 +               err = PTR_ERR(infopath.dentry);
11511 +               goto out;
11512 +       }
11513 +
11514 +       err = 0;
11515 +       h_dir = d_inode(w->h_ppath.dentry);
11516 +       if (elm && d_is_negative(infopath.dentry)) {
11517 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11518 +               AuTraceErr(err);
11519 +               if (unlikely(err))
11520 +                       goto out_dput;
11521 +               elm->created = 1;
11522 +               elm->info_dentry = dget(infopath.dentry);
11523 +       }
11524 +
11525 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11526 +       AuTraceErrPtr(infofile);
11527 +       if (IS_ERR(infofile)) {
11528 +               err = PTR_ERR(infofile);
11529 +               goto out_dput;
11530 +       }
11531 +
11532 +       h_inode = d_inode(infopath.dentry);
11533 +       if (elm && i_size_read(h_inode)) {
11534 +               h_inode = d_inode(w->h_dentry);
11535 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11536 +               AuTraceErrPtr(elm->info_last);
11537 +               if (IS_ERR(elm->info_last)) {
11538 +                       err = PTR_ERR(elm->info_last);
11539 +                       elm->info_last = NULL;
11540 +                       AuDebugOn(elm->info_dentry);
11541 +                       goto out_fput;
11542 +               }
11543 +       }
11544 +
11545 +       if (elm && w->renameback) {
11546 +               delegated = NULL;
11547 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11548 +               AuTraceErr(err);
11549 +               if (unlikely(err == -EWOULDBLOCK))
11550 +                       iput(delegated);
11551 +               goto out_fput;
11552 +       }
11553 +
11554 +       pos = 0;
11555 +       qname = &w->h_dentry->d_name;
11556 +       len = sizeof(*w->fdata) + qname->len;
11557 +       if (!elm)
11558 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11559 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11560 +       if (ssz == len) {
11561 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11562 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11563 +               goto out_fput; /* success */
11564 +       } else {
11565 +               err = -EIO;
11566 +               if (ssz < 0)
11567 +                       err = ssz;
11568 +               /* the caller should revert it using @elm */
11569 +       }
11570 +
11571 +out_fput:
11572 +       fput(infofile);
11573 +out_dput:
11574 +       dput(infopath.dentry);
11575 +out:
11576 +       AuTraceErr(err);
11577 +       return err;
11578 +}
11579 +
11580 +struct au_call_drinfo_do_store_args {
11581 +       int *errp;
11582 +       struct au_drinfo_store *w;
11583 +       struct au_drinfo_rev_elm *elm;
11584 +};
11585 +
11586 +static void au_call_drinfo_do_store(void *args)
11587 +{
11588 +       struct au_call_drinfo_do_store_args *a = args;
11589 +
11590 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11591 +}
11592 +
11593 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11594 +                              struct au_drinfo_rev_elm *elm)
11595 +{
11596 +       int err, wkq_err;
11597 +
11598 +       if (w->no_sio)
11599 +               err = au_drinfo_do_store(w, elm);
11600 +       else {
11601 +               struct au_call_drinfo_do_store_args a = {
11602 +                       .errp   = &err,
11603 +                       .w      = w,
11604 +                       .elm    = elm
11605 +               };
11606 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11607 +               if (unlikely(wkq_err))
11608 +                       err = wkq_err;
11609 +       }
11610 +       AuTraceErr(err);
11611 +
11612 +       return err;
11613 +}
11614 +
11615 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11616 +                                    aufs_bindex_t btgt)
11617 +{
11618 +       int err;
11619 +
11620 +       memset(w, 0, sizeof(*w));
11621 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11622 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11623 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11624 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11625 +       w->btgt = btgt;
11626 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11627 +
11628 +       err = -ENOMEM;
11629 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11630 +       if (unlikely(!w->fdata)) {
11631 +               AuTraceErr(err);
11632 +               goto out;
11633 +       }
11634 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11635 +       err = 0;
11636 +
11637 +out:
11638 +       return err;
11639 +}
11640 +
11641 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11642 +{
11643 +       au_kfree_rcu(w->fdata);
11644 +}
11645 +
11646 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11647 +                               struct au_drinfo_store *w)
11648 +{
11649 +       struct au_drinfo_rev_elm *elm;
11650 +       struct inode *h_dir, *delegated;
11651 +       int err, nelm;
11652 +       struct path infopath = {
11653 +               .mnt = w->h_ppath.mnt
11654 +       };
11655 +
11656 +       h_dir = d_inode(w->h_ppath.dentry);
11657 +       IMustLock(h_dir);
11658 +
11659 +       err = 0;
11660 +       elm = rev->elm;
11661 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11662 +               AuDebugOn(elm->created && elm->info_last);
11663 +               if (elm->created) {
11664 +                       AuDbg("here\n");
11665 +                       delegated = NULL;
11666 +                       infopath.dentry = elm->info_dentry;
11667 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11668 +                                          !w->no_sio);
11669 +                       AuTraceErr(err);
11670 +                       if (unlikely(err == -EWOULDBLOCK))
11671 +                               iput(delegated);
11672 +                       dput(elm->info_dentry);
11673 +               } else if (elm->info_last) {
11674 +                       AuDbg("here\n");
11675 +                       w->fdata->drinfo = *elm->info_last;
11676 +                       memcpy(w->fdata->drinfo.oldname,
11677 +                              elm->info_last->oldname,
11678 +                              elm->info_last->oldnamelen);
11679 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11680 +                       au_kfree_rcu(elm->info_last);
11681 +               }
11682 +               if (unlikely(err))
11683 +                       AuIOErr("%d, %s\n", err, w->whname);
11684 +               /* go on even if err */
11685 +       }
11686 +}
11687 +
11688 +/* caller has to call au_dr_rename_fin() later */
11689 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11690 +                          struct qstr *dst_name, void *_rev)
11691 +{
11692 +       int err, sz, nelm;
11693 +       aufs_bindex_t bindex, btail;
11694 +       struct au_drinfo_store work;
11695 +       struct au_drinfo_rev *rev, **p;
11696 +       struct au_drinfo_rev_elm *elm;
11697 +       struct super_block *sb;
11698 +       struct au_branch *br;
11699 +       struct au_hinode *hdir;
11700 +
11701 +       err = au_drinfo_store_work_init(&work, btgt);
11702 +       AuTraceErr(err);
11703 +       if (unlikely(err))
11704 +               goto out;
11705 +
11706 +       err = -ENOMEM;
11707 +       btail = au_dbtaildir(dentry);
11708 +       nelm = btail - btgt;
11709 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11710 +       rev = kcalloc(1, sz, GFP_NOFS);
11711 +       if (unlikely(!rev)) {
11712 +               AuTraceErr(err);
11713 +               goto out_args;
11714 +       }
11715 +       rev->nelm = nelm;
11716 +       elm = rev->elm;
11717 +       p = _rev;
11718 +       *p = rev;
11719 +
11720 +       err = 0;
11721 +       sb = dentry->d_sb;
11722 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11723 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11724 +       hdir = au_hi(d_inode(dentry), btgt);
11725 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11726 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11727 +               work.h_dentry = au_h_dptr(dentry, bindex);
11728 +               if (!work.h_dentry)
11729 +                       continue;
11730 +
11731 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11732 +                                         &work.allocated);
11733 +               AuTraceErr(err);
11734 +               if (unlikely(err))
11735 +                       break;
11736 +
11737 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11738 +               br = au_sbr(sb, bindex);
11739 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11740 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11741 +                                                work.infonamelen);
11742 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11743 +                     work.whnamelen, work.whname,
11744 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11745 +                     work.fdata->drinfo.oldnamelen,
11746 +                     work.fdata->drinfo.oldname);
11747 +
11748 +               err = au_drinfo_store_sio(&work, elm);
11749 +               AuTraceErr(err);
11750 +               if (unlikely(err))
11751 +                       break;
11752 +       }
11753 +       if (unlikely(err)) {
11754 +               /* revert all drinfo */
11755 +               au_drinfo_store_rev(rev, &work);
11756 +               au_kfree_try_rcu(rev);
11757 +               *p = NULL;
11758 +       }
11759 +       au_hn_inode_unlock(hdir);
11760 +
11761 +out_args:
11762 +       au_drinfo_store_work_fin(&work);
11763 +out:
11764 +       return err;
11765 +}
11766 +
11767 +/* ---------------------------------------------------------------------- */
11768 +
11769 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11770 +                struct qstr *dst_name, void *_rev)
11771 +{
11772 +       int err, already;
11773 +       ino_t ino;
11774 +       struct super_block *sb;
11775 +       struct au_branch *br;
11776 +       struct au_dr_br *dr;
11777 +       struct dentry *h_dentry;
11778 +       struct inode *h_inode;
11779 +       struct au_dr_hino *ent;
11780 +       struct au_drinfo_rev *rev, **p;
11781 +
11782 +       AuDbg("bindex %d\n", bindex);
11783 +
11784 +       err = -ENOMEM;
11785 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11786 +       if (unlikely(!ent))
11787 +               goto out;
11788 +
11789 +       sb = src->d_sb;
11790 +       br = au_sbr(sb, bindex);
11791 +       dr = &br->br_dirren;
11792 +       h_dentry = au_h_dptr(src, bindex);
11793 +       h_inode = d_inode(h_dentry);
11794 +       ino = h_inode->i_ino;
11795 +       ent->dr_h_ino = ino;
11796 +       already = au_dr_hino_test_add(dr, ino, ent);
11797 +       AuDbg("b%d, hi%llu, already %d\n",
11798 +             bindex, (unsigned long long)ino, already);
11799 +
11800 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11801 +       AuTraceErr(err);
11802 +       if (!err) {
11803 +               p = _rev;
11804 +               rev = *p;
11805 +               rev->already = already;
11806 +               goto out; /* success */
11807 +       }
11808 +
11809 +       /* revert */
11810 +       if (!already)
11811 +               au_dr_hino_del(dr, ent);
11812 +       au_kfree_rcu(ent);
11813 +
11814 +out:
11815 +       AuTraceErr(err);
11816 +       return err;
11817 +}
11818 +
11819 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11820 +{
11821 +       struct au_drinfo_rev *rev;
11822 +       struct au_drinfo_rev_elm *elm;
11823 +       int nelm;
11824 +
11825 +       rev = _rev;
11826 +       elm = rev->elm;
11827 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11828 +               dput(elm->info_dentry);
11829 +               au_kfree_rcu(elm->info_last);
11830 +       }
11831 +       au_kfree_try_rcu(rev);
11832 +}
11833 +
11834 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11835 +{
11836 +       int err;
11837 +       struct au_drinfo_store work;
11838 +       struct au_drinfo_rev *rev = _rev;
11839 +       struct super_block *sb;
11840 +       struct au_branch *br;
11841 +       struct inode *h_inode;
11842 +       struct au_dr_br *dr;
11843 +       struct au_dr_hino *ent;
11844 +
11845 +       err = au_drinfo_store_work_init(&work, btgt);
11846 +       if (unlikely(err))
11847 +               goto out;
11848 +
11849 +       sb = src->d_sb;
11850 +       br = au_sbr(sb, btgt);
11851 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11852 +       work.h_ppath.mnt = au_br_mnt(br);
11853 +       au_drinfo_store_rev(rev, &work);
11854 +       au_drinfo_store_work_fin(&work);
11855 +       if (rev->already)
11856 +               goto out;
11857 +
11858 +       dr = &br->br_dirren;
11859 +       h_inode = d_inode(work.h_ppath.dentry);
11860 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11861 +       BUG_ON(!ent);
11862 +       au_dr_hino_del(dr, ent);
11863 +       au_kfree_rcu(ent);
11864 +
11865 +out:
11866 +       au_kfree_try_rcu(rev);
11867 +       if (unlikely(err))
11868 +               pr_err("failed to remove dirren info\n");
11869 +}
11870 +
11871 +/* ---------------------------------------------------------------------- */
11872 +
11873 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11874 +                                          char *whname, int whnamelen,
11875 +                                          struct dentry **info_dentry)
11876 +{
11877 +       struct au_drinfo *drinfo;
11878 +       struct file *f;
11879 +       struct inode *h_dir;
11880 +       struct path infopath;
11881 +       int unlocked;
11882 +
11883 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11884 +
11885 +       *info_dentry = NULL;
11886 +       drinfo = NULL;
11887 +       unlocked = 0;
11888 +       h_dir = d_inode(h_ppath->dentry);
11889 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11890 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11891 +       if (IS_ERR(infopath.dentry)) {
11892 +               drinfo = (void *)infopath.dentry;
11893 +               goto out;
11894 +       }
11895 +
11896 +       if (d_is_negative(infopath.dentry))
11897 +               goto out_dput; /* success */
11898 +
11899 +       infopath.mnt = h_ppath->mnt;
11900 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11901 +       inode_unlock_shared(h_dir);
11902 +       unlocked = 1;
11903 +       if (IS_ERR(f)) {
11904 +               drinfo = (void *)f;
11905 +               goto out_dput;
11906 +       }
11907 +
11908 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11909 +       if (IS_ERR_OR_NULL(drinfo))
11910 +               goto out_fput;
11911 +
11912 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11913 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11914 +
11915 +out_fput:
11916 +       fput(f);
11917 +out_dput:
11918 +       dput(infopath.dentry);
11919 +out:
11920 +       if (!unlocked)
11921 +               inode_unlock_shared(h_dir);
11922 +       AuTraceErrPtr(drinfo);
11923 +       return drinfo;
11924 +}
11925 +
11926 +struct au_drinfo_do_load_args {
11927 +       struct au_drinfo **drinfop;
11928 +       struct path *h_ppath;
11929 +       char *whname;
11930 +       int whnamelen;
11931 +       struct dentry **info_dentry;
11932 +};
11933 +
11934 +static void au_call_drinfo_do_load(void *args)
11935 +{
11936 +       struct au_drinfo_do_load_args *a = args;
11937 +
11938 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11939 +                                       a->info_dentry);
11940 +}
11941 +
11942 +struct au_drinfo_load {
11943 +       struct path h_ppath;
11944 +       struct qstr *qname;
11945 +       unsigned char no_sio;
11946 +
11947 +       aufs_bindex_t ninfo;
11948 +       struct au_drinfo **drinfo;
11949 +};
11950 +
11951 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11952 +                         struct au_branch *br)
11953 +{
11954 +       int err, wkq_err, whnamelen, e;
11955 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11956 +               = AUFS_WH_DR_INFO_PFX;
11957 +       struct au_drinfo *drinfo;
11958 +       struct qstr oldname;
11959 +       struct inode *h_dir, *delegated;
11960 +       struct dentry *info_dentry;
11961 +       struct path infopath;
11962 +
11963 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11964 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11965 +                                   sizeof(whname) - whnamelen);
11966 +       if (w->no_sio)
11967 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11968 +                                          &info_dentry);
11969 +       else {
11970 +               struct au_drinfo_do_load_args args = {
11971 +                       .drinfop        = &drinfo,
11972 +                       .h_ppath        = &w->h_ppath,
11973 +                       .whname         = whname,
11974 +                       .whnamelen      = whnamelen,
11975 +                       .info_dentry    = &info_dentry
11976 +               };
11977 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11978 +               if (unlikely(wkq_err))
11979 +                       drinfo = ERR_PTR(wkq_err);
11980 +       }
11981 +       err = PTR_ERR(drinfo);
11982 +       if (IS_ERR_OR_NULL(drinfo))
11983 +               goto out;
11984 +
11985 +       err = 0;
11986 +       oldname.len = drinfo->oldnamelen;
11987 +       oldname.name = drinfo->oldname;
11988 +       if (au_qstreq(w->qname, &oldname)) {
11989 +               /* the name is renamed back */
11990 +               au_kfree_rcu(drinfo);
11991 +               drinfo = NULL;
11992 +
11993 +               infopath.dentry = info_dentry;
11994 +               infopath.mnt = w->h_ppath.mnt;
11995 +               h_dir = d_inode(w->h_ppath.dentry);
11996 +               delegated = NULL;
11997 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11998 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11999 +               inode_unlock(h_dir);
12000 +               if (unlikely(e))
12001 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12002 +               if (unlikely(e == -EWOULDBLOCK))
12003 +                       iput(delegated);
12004 +       }
12005 +       au_kfree_rcu(w->drinfo[bindex]);
12006 +       w->drinfo[bindex] = drinfo;
12007 +       dput(info_dentry);
12008 +
12009 +out:
12010 +       AuTraceErr(err);
12011 +       return err;
12012 +}
12013 +
12014 +/* ---------------------------------------------------------------------- */
12015 +
12016 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12017 +{
12018 +       struct au_drinfo **p = drinfo;
12019 +
12020 +       while (n-- > 0)
12021 +               au_kfree_rcu(*drinfo++);
12022 +       au_kfree_try_rcu(p);
12023 +}
12024 +
12025 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12026 +              aufs_bindex_t btgt)
12027 +{
12028 +       int err, ninfo;
12029 +       struct au_drinfo_load w;
12030 +       aufs_bindex_t bindex, bbot;
12031 +       struct au_branch *br;
12032 +       struct inode *h_dir;
12033 +       struct au_dr_hino *ent;
12034 +       struct super_block *sb;
12035 +
12036 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12037 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12038 +             AuLNPair(&lkup->whname), btgt);
12039 +
12040 +       sb = dentry->d_sb;
12041 +       bbot = au_sbbot(sb);
12042 +       w.ninfo = bbot + 1;
12043 +       if (!lkup->dirren.drinfo) {
12044 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12045 +                                             sizeof(*lkup->dirren.drinfo),
12046 +                                             GFP_NOFS);
12047 +               if (unlikely(!lkup->dirren.drinfo)) {
12048 +                       err = -ENOMEM;
12049 +                       goto out;
12050 +               }
12051 +               lkup->dirren.ninfo = w.ninfo;
12052 +       }
12053 +       w.drinfo = lkup->dirren.drinfo;
12054 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12055 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12056 +       AuDebugOn(!w.h_ppath.dentry);
12057 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12058 +       w.qname = &dentry->d_name;
12059 +
12060 +       ninfo = 0;
12061 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12062 +               br = au_sbr(sb, bindex);
12063 +               err = au_drinfo_load(&w, bindex, br);
12064 +               if (unlikely(err))
12065 +                       goto out_free;
12066 +               if (w.drinfo[bindex])
12067 +                       ninfo++;
12068 +       }
12069 +       if (!ninfo) {
12070 +               br = au_sbr(sb, btgt);
12071 +               h_dir = d_inode(w.h_ppath.dentry);
12072 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12073 +               AuDebugOn(!ent);
12074 +               au_dr_hino_del(&br->br_dirren, ent);
12075 +               au_kfree_rcu(ent);
12076 +       }
12077 +       goto out; /* success */
12078 +
12079 +out_free:
12080 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12081 +       lkup->dirren.ninfo = 0;
12082 +       lkup->dirren.drinfo = NULL;
12083 +out:
12084 +       AuTraceErr(err);
12085 +       return err;
12086 +}
12087 +
12088 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12089 +{
12090 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12091 +}
12092 +
12093 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12094 +{
12095 +       int err;
12096 +       struct au_drinfo *drinfo;
12097 +
12098 +       err = 0;
12099 +       if (!lkup->dirren.drinfo)
12100 +               goto out;
12101 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12102 +       drinfo = lkup->dirren.drinfo[btgt];
12103 +       if (!drinfo)
12104 +               goto out;
12105 +
12106 +       au_kfree_try_rcu(lkup->whname.name);
12107 +       lkup->whname.name = NULL;
12108 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12109 +       lkup->dirren.dr_name.name = drinfo->oldname;
12110 +       lkup->name = &lkup->dirren.dr_name;
12111 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12112 +       if (!err)
12113 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12114 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12115 +                     btgt);
12116 +
12117 +out:
12118 +       AuTraceErr(err);
12119 +       return err;
12120 +}
12121 +
12122 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12123 +                    ino_t h_ino)
12124 +{
12125 +       int match;
12126 +       struct au_drinfo *drinfo;
12127 +
12128 +       match = 1;
12129 +       if (!lkup->dirren.drinfo)
12130 +               goto out;
12131 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12132 +       drinfo = lkup->dirren.drinfo[bindex];
12133 +       if (!drinfo)
12134 +               goto out;
12135 +
12136 +       match = (drinfo->ino == h_ino);
12137 +       AuDbg("match %d\n", match);
12138 +
12139 +out:
12140 +       return match;
12141 +}
12142 +
12143 +/* ---------------------------------------------------------------------- */
12144 +
12145 +int au_dr_opt_set(struct super_block *sb)
12146 +{
12147 +       int err;
12148 +       aufs_bindex_t bindex, bbot;
12149 +       struct au_branch *br;
12150 +
12151 +       err = 0;
12152 +       bbot = au_sbbot(sb);
12153 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12154 +               br = au_sbr(sb, bindex);
12155 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12156 +       }
12157 +
12158 +       return err;
12159 +}
12160 +
12161 +int au_dr_opt_flush(struct super_block *sb)
12162 +{
12163 +       int err;
12164 +       aufs_bindex_t bindex, bbot;
12165 +       struct au_branch *br;
12166 +
12167 +       err = 0;
12168 +       bbot = au_sbbot(sb);
12169 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12170 +               br = au_sbr(sb, bindex);
12171 +               if (au_br_writable(br->br_perm))
12172 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12173 +       }
12174 +
12175 +       return err;
12176 +}
12177 +
12178 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12179 +{
12180 +       int err;
12181 +       aufs_bindex_t bindex, bbot;
12182 +       struct au_branch *br;
12183 +
12184 +       err = 0;
12185 +       if (!no_flush) {
12186 +               err = au_dr_opt_flush(sb);
12187 +               if (unlikely(err))
12188 +                       goto out;
12189 +       }
12190 +
12191 +       bbot = au_sbbot(sb);
12192 +       for (bindex = 0; bindex <= bbot; bindex++) {
12193 +               br = au_sbr(sb, bindex);
12194 +               au_dr_hino_free(&br->br_dirren);
12195 +       }
12196 +
12197 +out:
12198 +       return err;
12199 +}
12200 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12201 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12202 +++ linux/fs/aufs/dirren.h      2022-11-05 23:02:18.962555950 +0100
12203 @@ -0,0 +1,140 @@
12204 +/* SPDX-License-Identifier: GPL-2.0 */
12205 +/*
12206 + * Copyright (C) 2017-2022 Junjiro R. Okajima
12207 + *
12208 + * This program is free software; you can redistribute it and/or modify
12209 + * it under the terms of the GNU General Public License as published by
12210 + * the Free Software Foundation; either version 2 of the License, or
12211 + * (at your option) any later version.
12212 + *
12213 + * This program is distributed in the hope that it will be useful,
12214 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12215 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12216 + * GNU General Public License for more details.
12217 + *
12218 + * You should have received a copy of the GNU General Public License
12219 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12220 + */
12221 +
12222 +/*
12223 + * renamed dir info
12224 + */
12225 +
12226 +#ifndef __AUFS_DIRREN_H__
12227 +#define __AUFS_DIRREN_H__
12228 +
12229 +#ifdef __KERNEL__
12230 +
12231 +#include <linux/dcache.h>
12232 +#include <linux/statfs.h>
12233 +#include <linux/uuid.h>
12234 +#include "hbl.h"
12235 +
12236 +#define AuDirren_NHASH 100
12237 +
12238 +#ifdef CONFIG_AUFS_DIRREN
12239 +enum au_brid_type {
12240 +       AuBrid_Unset,
12241 +       AuBrid_UUID,
12242 +       AuBrid_FSID,
12243 +       AuBrid_DEV
12244 +};
12245 +
12246 +struct au_dr_brid {
12247 +       enum au_brid_type       type;
12248 +       union {
12249 +               uuid_t  uuid;   /* unimplemented yet */
12250 +               fsid_t  fsid;
12251 +               dev_t   dev;
12252 +       };
12253 +};
12254 +
12255 +/* 20 is the max digits length of ulong 64 */
12256 +/* brid-type "_" uuid "_" inum */
12257 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12258 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12259 +
12260 +struct au_dr_hino {
12261 +       struct hlist_bl_node    dr_hnode;
12262 +       ino_t                   dr_h_ino;
12263 +};
12264 +
12265 +struct au_dr_br {
12266 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12267 +       struct au_dr_brid       dr_brid;
12268 +};
12269 +
12270 +struct au_dr_lookup {
12271 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12272 +       struct qstr             dr_name; /* subset of dr_info */
12273 +       aufs_bindex_t           ninfo;
12274 +       struct au_drinfo        **drinfo;
12275 +};
12276 +#else
12277 +struct au_dr_hino;
12278 +/* empty */
12279 +struct au_dr_br { };
12280 +struct au_dr_lookup { };
12281 +#endif
12282 +
12283 +/* ---------------------------------------------------------------------- */
12284 +
12285 +struct au_branch;
12286 +struct au_do_lookup_args;
12287 +struct au_hinode;
12288 +#ifdef CONFIG_AUFS_DIRREN
12289 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12290 +                       struct au_dr_hino *add_ent);
12291 +void au_dr_hino_free(struct au_dr_br *dr);
12292 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12293 +                 const struct path *path);
12294 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12295 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12296 +                struct qstr *dst_name, void *_rev);
12297 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12298 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12299 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12300 +              aufs_bindex_t bindex);
12301 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12302 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12303 +                    ino_t h_ino);
12304 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12305 +int au_dr_opt_set(struct super_block *sb);
12306 +int au_dr_opt_flush(struct super_block *sb);
12307 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12308 +#else
12309 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12310 +          struct au_dr_hino *add_ent);
12311 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12312 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12313 +          const struct path *path);
12314 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12315 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12316 +          struct qstr *dst_name, void *_rev);
12317 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12318 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12319 +          void *rev);
12320 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12321 +          aufs_bindex_t bindex);
12322 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12323 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12324 +          aufs_bindex_t bindex, ino_t h_ino);
12325 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12326 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12327 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12328 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12329 +#endif
12330 +
12331 +/* ---------------------------------------------------------------------- */
12332 +
12333 +#ifdef CONFIG_AUFS_DIRREN
12334 +static inline int au_dr_ihash(ino_t h_ino)
12335 +{
12336 +       return h_ino % AuDirren_NHASH;
12337 +}
12338 +#else
12339 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12340 +#endif
12341 +
12342 +#endif /* __KERNEL__ */
12343 +#endif /* __AUFS_DIRREN_H__ */
12344 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12345 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12346 +++ linux/fs/aufs/dynop.c       2022-11-05 23:02:18.962555950 +0100
12347 @@ -0,0 +1,366 @@
12348 +// SPDX-License-Identifier: GPL-2.0
12349 +/*
12350 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12351 + *
12352 + * This program is free software; you can redistribute it and/or modify
12353 + * it under the terms of the GNU General Public License as published by
12354 + * the Free Software Foundation; either version 2 of the License, or
12355 + * (at your option) any later version.
12356 + *
12357 + * This program is distributed in the hope that it will be useful,
12358 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12359 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12360 + * GNU General Public License for more details.
12361 + *
12362 + * You should have received a copy of the GNU General Public License
12363 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12364 + */
12365 +
12366 +/*
12367 + * dynamically customizable operations for regular files
12368 + */
12369 +
12370 +#include "aufs.h"
12371 +
12372 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12373 +
12374 +/*
12375 + * How large will these lists be?
12376 + * Usually just a few elements, 20-30 at most for each, I guess.
12377 + */
12378 +static struct hlist_bl_head dynop[AuDyLast];
12379 +
12380 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12381 +                                    const void *h_op)
12382 +{
12383 +       struct au_dykey *key, *tmp;
12384 +       struct hlist_bl_node *pos;
12385 +
12386 +       key = NULL;
12387 +       hlist_bl_lock(hbl);
12388 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12389 +               if (tmp->dk_op.dy_hop == h_op) {
12390 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12391 +                               key = tmp;
12392 +                       break;
12393 +               }
12394 +       hlist_bl_unlock(hbl);
12395 +
12396 +       return key;
12397 +}
12398 +
12399 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12400 +{
12401 +       struct au_dykey **k, *found;
12402 +       const void *h_op = key->dk_op.dy_hop;
12403 +       int i;
12404 +
12405 +       found = NULL;
12406 +       k = br->br_dykey;
12407 +       for (i = 0; i < AuBrDynOp; i++)
12408 +               if (k[i]) {
12409 +                       if (k[i]->dk_op.dy_hop == h_op) {
12410 +                               found = k[i];
12411 +                               break;
12412 +                       }
12413 +               } else
12414 +                       break;
12415 +       if (!found) {
12416 +               spin_lock(&br->br_dykey_lock);
12417 +               for (; i < AuBrDynOp; i++)
12418 +                       if (k[i]) {
12419 +                               if (k[i]->dk_op.dy_hop == h_op) {
12420 +                                       found = k[i];
12421 +                                       break;
12422 +                               }
12423 +                       } else {
12424 +                               k[i] = key;
12425 +                               break;
12426 +                       }
12427 +               spin_unlock(&br->br_dykey_lock);
12428 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12429 +       }
12430 +
12431 +       return found;
12432 +}
12433 +
12434 +/* kref_get() if @key is already added */
12435 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12436 +{
12437 +       struct au_dykey *tmp, *found;
12438 +       struct hlist_bl_node *pos;
12439 +       const void *h_op = key->dk_op.dy_hop;
12440 +
12441 +       found = NULL;
12442 +       hlist_bl_lock(hbl);
12443 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12444 +               if (tmp->dk_op.dy_hop == h_op) {
12445 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12446 +                               found = tmp;
12447 +                       break;
12448 +               }
12449 +       if (!found)
12450 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12451 +       hlist_bl_unlock(hbl);
12452 +
12453 +       if (!found)
12454 +               DyPrSym(key);
12455 +       return found;
12456 +}
12457 +
12458 +static void dy_free_rcu(struct rcu_head *rcu)
12459 +{
12460 +       struct au_dykey *key;
12461 +
12462 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12463 +       DyPrSym(key);
12464 +       kfree(key);
12465 +}
12466 +
12467 +static void dy_free(struct kref *kref)
12468 +{
12469 +       struct au_dykey *key;
12470 +       struct hlist_bl_head *hbl;
12471 +
12472 +       key = container_of(kref, struct au_dykey, dk_kref);
12473 +       hbl = dynop + key->dk_op.dy_type;
12474 +       au_hbl_del(&key->dk_hnode, hbl);
12475 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12476 +}
12477 +
12478 +void au_dy_put(struct au_dykey *key)
12479 +{
12480 +       kref_put(&key->dk_kref, dy_free);
12481 +}
12482 +
12483 +/* ---------------------------------------------------------------------- */
12484 +
12485 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12486 +
12487 +#ifdef CONFIG_AUFS_DEBUG
12488 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12489 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12490 +#else
12491 +#define DyDbgDeclare(cnt)      do {} while (0)
12492 +#define DyDbgInc(cnt)          do {} while (0)
12493 +#endif
12494 +
12495 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12496 +       DyDbgInc(cnt);                                                  \
12497 +       if (h_op->func) {                                               \
12498 +               if (src.func)                                           \
12499 +                       dst.func = src.func;                            \
12500 +               else                                                    \
12501 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12502 +       }                                                               \
12503 +} while (0)
12504 +
12505 +#define DySetForce(func, dst, src) do {                \
12506 +       AuDebugOn(!src.func);                   \
12507 +       DyDbgInc(cnt);                          \
12508 +       dst.func = src.func;                    \
12509 +} while (0)
12510 +
12511 +#define DySetAop(func) \
12512 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12513 +#define DySetAopForce(func) \
12514 +       DySetForce(func, dyaop->da_op, aufs_aop)
12515 +
12516 +static void dy_aop(struct au_dykey *key, const void *h_op,
12517 +                  struct super_block *h_sb __maybe_unused)
12518 +{
12519 +       struct au_dyaop *dyaop = (void *)key;
12520 +       const struct address_space_operations *h_aop = h_op;
12521 +       DyDbgDeclare(cnt);
12522 +
12523 +       AuDbg("%s\n", au_sbtype(h_sb));
12524 +
12525 +       DySetAop(writepage);
12526 +       DySetAopForce(read_folio);      /* force */
12527 +       DySetAop(writepages);
12528 +       DySetAop(dirty_folio);
12529 +       DySetAop(invalidate_folio);
12530 +       DySetAop(readahead);
12531 +       DySetAop(write_begin);
12532 +       DySetAop(write_end);
12533 +       DySetAop(bmap);
12534 +       DySetAop(release_folio);
12535 +       DySetAop(free_folio);
12536 +       /* this one will be changed according to an aufs mount option */
12537 +       DySetAop(direct_IO);
12538 +       DySetAop(migrate_folio);
12539 +       DySetAop(launder_folio);
12540 +       DySetAop(is_partially_uptodate);
12541 +       DySetAop(is_dirty_writeback);
12542 +       DySetAop(error_remove_page);
12543 +       DySetAop(swap_activate);
12544 +       DySetAop(swap_deactivate);
12545 +       DySetAop(swap_rw);
12546 +
12547 +       DyDbgSize(cnt, *h_aop);
12548 +}
12549 +
12550 +/* ---------------------------------------------------------------------- */
12551 +
12552 +static void dy_bug(struct kref *kref)
12553 +{
12554 +       BUG();
12555 +}
12556 +
12557 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12558 +{
12559 +       struct au_dykey *key, *old;
12560 +       struct hlist_bl_head *hbl;
12561 +       struct op {
12562 +               unsigned int sz;
12563 +               void (*set)(struct au_dykey *key, const void *h_op,
12564 +                           struct super_block *h_sb __maybe_unused);
12565 +       };
12566 +       static const struct op a[] = {
12567 +               [AuDy_AOP] = {
12568 +                       .sz     = sizeof(struct au_dyaop),
12569 +                       .set    = dy_aop
12570 +               }
12571 +       };
12572 +       const struct op *p;
12573 +
12574 +       hbl = dynop + op->dy_type;
12575 +       key = dy_gfind_get(hbl, op->dy_hop);
12576 +       if (key)
12577 +               goto out_add; /* success */
12578 +
12579 +       p = a + op->dy_type;
12580 +       key = kzalloc(p->sz, GFP_NOFS);
12581 +       if (unlikely(!key)) {
12582 +               key = ERR_PTR(-ENOMEM);
12583 +               goto out;
12584 +       }
12585 +
12586 +       key->dk_op.dy_hop = op->dy_hop;
12587 +       kref_init(&key->dk_kref);
12588 +       p->set(key, op->dy_hop, au_br_sb(br));
12589 +       old = dy_gadd(hbl, key);
12590 +       if (old) {
12591 +               au_kfree_rcu(key);
12592 +               key = old;
12593 +       }
12594 +
12595 +out_add:
12596 +       old = dy_bradd(br, key);
12597 +       if (old)
12598 +               /* its ref-count should never be zero here */
12599 +               kref_put(&key->dk_kref, dy_bug);
12600 +out:
12601 +       return key;
12602 +}
12603 +
12604 +/* ---------------------------------------------------------------------- */
12605 +/*
12606 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12607 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12608 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12609 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12610 + * See the aufs manual in detail.
12611 + */
12612 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12613 +{
12614 +       if (!do_dx)
12615 +               dyaop->da_op.direct_IO = NULL;
12616 +       else
12617 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12618 +}
12619 +
12620 +static struct au_dyaop *dy_aget(struct au_branch *br,
12621 +                               const struct address_space_operations *h_aop,
12622 +                               int do_dx)
12623 +{
12624 +       struct au_dyaop *dyaop;
12625 +       struct au_dynop op;
12626 +
12627 +       op.dy_type = AuDy_AOP;
12628 +       op.dy_haop = h_aop;
12629 +       dyaop = (void *)dy_get(&op, br);
12630 +       if (IS_ERR(dyaop))
12631 +               goto out;
12632 +       dy_adx(dyaop, do_dx);
12633 +
12634 +out:
12635 +       return dyaop;
12636 +}
12637 +
12638 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12639 +               struct inode *h_inode)
12640 +{
12641 +       int err, do_dx;
12642 +       struct super_block *sb;
12643 +       struct au_branch *br;
12644 +       struct au_dyaop *dyaop;
12645 +
12646 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12647 +       IiMustWriteLock(inode);
12648 +
12649 +       sb = inode->i_sb;
12650 +       br = au_sbr(sb, bindex);
12651 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12652 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12653 +       err = PTR_ERR(dyaop);
12654 +       if (IS_ERR(dyaop))
12655 +               /* unnecessary to call dy_fput() */
12656 +               goto out;
12657 +
12658 +       err = 0;
12659 +       inode->i_mapping->a_ops = &dyaop->da_op;
12660 +
12661 +out:
12662 +       return err;
12663 +}
12664 +
12665 +/*
12666 + * Is it safe to replace a_ops during the inode/file is in operation?
12667 + * Yes, I hope so.
12668 + */
12669 +int au_dy_irefresh(struct inode *inode)
12670 +{
12671 +       int err;
12672 +       aufs_bindex_t btop;
12673 +       struct inode *h_inode;
12674 +
12675 +       err = 0;
12676 +       if (S_ISREG(inode->i_mode)) {
12677 +               btop = au_ibtop(inode);
12678 +               h_inode = au_h_iptr(inode, btop);
12679 +               err = au_dy_iaop(inode, btop, h_inode);
12680 +       }
12681 +       return err;
12682 +}
12683 +
12684 +void au_dy_arefresh(int do_dx)
12685 +{
12686 +       struct hlist_bl_head *hbl;
12687 +       struct hlist_bl_node *pos;
12688 +       struct au_dykey *key;
12689 +
12690 +       hbl = dynop + AuDy_AOP;
12691 +       hlist_bl_lock(hbl);
12692 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12693 +               dy_adx((void *)key, do_dx);
12694 +       hlist_bl_unlock(hbl);
12695 +}
12696 +
12697 +/* ---------------------------------------------------------------------- */
12698 +
12699 +void __init au_dy_init(void)
12700 +{
12701 +       int i;
12702 +
12703 +       for (i = 0; i < AuDyLast; i++)
12704 +               INIT_HLIST_BL_HEAD(dynop + i);
12705 +}
12706 +
12707 +void au_dy_fin(void)
12708 +{
12709 +       int i;
12710 +
12711 +       for (i = 0; i < AuDyLast; i++)
12712 +               WARN_ON(!hlist_bl_empty(dynop + i));
12713 +}
12714 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12715 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12716 +++ linux/fs/aufs/dynop.h       2022-11-05 23:02:18.962555950 +0100
12717 @@ -0,0 +1,77 @@
12718 +/* SPDX-License-Identifier: GPL-2.0 */
12719 +/*
12720 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12721 + *
12722 + * This program is free software; you can redistribute it and/or modify
12723 + * it under the terms of the GNU General Public License as published by
12724 + * the Free Software Foundation; either version 2 of the License, or
12725 + * (at your option) any later version.
12726 + *
12727 + * This program is distributed in the hope that it will be useful,
12728 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12729 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12730 + * GNU General Public License for more details.
12731 + *
12732 + * You should have received a copy of the GNU General Public License
12733 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12734 + */
12735 +
12736 +/*
12737 + * dynamically customizable operations (for regular files only)
12738 + */
12739 +
12740 +#ifndef __AUFS_DYNOP_H__
12741 +#define __AUFS_DYNOP_H__
12742 +
12743 +#ifdef __KERNEL__
12744 +
12745 +#include <linux/fs.h>
12746 +#include <linux/kref.h>
12747 +
12748 +enum {AuDy_AOP, AuDyLast};
12749 +
12750 +struct au_dynop {
12751 +       int                                             dy_type;
12752 +       union {
12753 +               const void                              *dy_hop;
12754 +               const struct address_space_operations   *dy_haop;
12755 +       };
12756 +};
12757 +
12758 +struct au_dykey {
12759 +       union {
12760 +               struct hlist_bl_node    dk_hnode;
12761 +               struct rcu_head         dk_rcu;
12762 +       };
12763 +       struct au_dynop         dk_op;
12764 +
12765 +       /*
12766 +        * during I am in the branch local array, kref is gotten. when the
12767 +        * branch is removed, kref is put.
12768 +        */
12769 +       struct kref             dk_kref;
12770 +};
12771 +
12772 +/* stop unioning since their sizes are very different from each other */
12773 +struct au_dyaop {
12774 +       struct au_dykey                 da_key;
12775 +       struct address_space_operations da_op; /* not const */
12776 +};
12777 +/* make sure that 'struct au_dykey *' can be any type */
12778 +static_assert(!offsetof(struct au_dyaop, da_key));
12779 +
12780 +/* ---------------------------------------------------------------------- */
12781 +
12782 +/* dynop.c */
12783 +struct au_branch;
12784 +void au_dy_put(struct au_dykey *key);
12785 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12786 +               struct inode *h_inode);
12787 +int au_dy_irefresh(struct inode *inode);
12788 +void au_dy_arefresh(int do_dio);
12789 +
12790 +void __init au_dy_init(void);
12791 +void au_dy_fin(void);
12792 +
12793 +#endif /* __KERNEL__ */
12794 +#endif /* __AUFS_DYNOP_H__ */
12795 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12796 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12797 +++ linux/fs/aufs/export.c      2022-12-17 09:21:34.796521861 +0100
12798 @@ -0,0 +1,830 @@
12799 +// SPDX-License-Identifier: GPL-2.0
12800 +/*
12801 + * Copyright (C) 2005-2022 Junjiro R. Okajima
12802 + *
12803 + * This program is free software; you can redistribute it and/or modify
12804 + * it under the terms of the GNU General Public License as published by
12805 + * the Free Software Foundation; either version 2 of the License, or
12806 + * (at your option) any later version.
12807 + *
12808 + * This program is distributed in the hope that it will be useful,
12809 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12810 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12811 + * GNU General Public License for more details.
12812 + *
12813 + * You should have received a copy of the GNU General Public License
12814 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12815 + */
12816 +
12817 +/*
12818 + * export via nfs
12819 + */
12820 +
12821 +#include <linux/exportfs.h>
12822 +#include <linux/fs_struct.h>
12823 +#include <linux/nsproxy.h>
12824 +#include <linux/random.h>
12825 +#include <linux/writeback.h>
12826 +#include "aufs.h"
12827 +
12828 +union conv {
12829 +#ifdef CONFIG_AUFS_INO_T_64
12830 +       __u32 a[2];
12831 +#else
12832 +       __u32 a[1];
12833 +#endif
12834 +       ino_t ino;
12835 +};
12836 +
12837 +static ino_t decode_ino(__u32 *a)
12838 +{
12839 +       union conv u;
12840 +
12841 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12842 +       u.a[0] = a[0];
12843 +#ifdef CONFIG_AUFS_INO_T_64
12844 +       u.a[1] = a[1];
12845 +#endif
12846 +       return u.ino;
12847 +}
12848 +
12849 +static void encode_ino(__u32 *a, ino_t ino)
12850 +{
12851 +       union conv u;
12852 +
12853 +       u.ino = ino;
12854 +       a[0] = u.a[0];
12855 +#ifdef CONFIG_AUFS_INO_T_64
12856 +       a[1] = u.a[1];
12857 +#endif
12858 +}
12859 +
12860 +/* NFS file handle */
12861 +enum {
12862 +       Fh_br_id,
12863 +       Fh_sigen,
12864 +#ifdef CONFIG_AUFS_INO_T_64
12865 +       /* support 64bit inode number */
12866 +       Fh_ino1,
12867 +       Fh_ino2,
12868 +       Fh_dir_ino1,
12869 +       Fh_dir_ino2,
12870 +#else
12871 +       Fh_ino1,
12872 +       Fh_dir_ino1,
12873 +#endif
12874 +       Fh_igen,
12875 +       Fh_h_type,
12876 +       Fh_tail,
12877 +
12878 +       Fh_ino = Fh_ino1,
12879 +       Fh_dir_ino = Fh_dir_ino1
12880 +};
12881 +
12882 +static int au_test_anon(struct dentry *dentry)
12883 +{
12884 +       /* note: read d_flags without d_lock */
12885 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12886 +}
12887 +
12888 +int au_test_nfsd(void)
12889 +{
12890 +       int ret;
12891 +       struct task_struct *tsk = current;
12892 +       char comm[sizeof(tsk->comm)];
12893 +
12894 +       ret = 0;
12895 +       if (tsk->flags & PF_KTHREAD) {
12896 +               get_task_comm(comm, tsk);
12897 +               ret = !strcmp(comm, "nfsd");
12898 +       }
12899 +
12900 +       return ret;
12901 +}
12902 +
12903 +/* ---------------------------------------------------------------------- */
12904 +/* inode generation external table */
12905 +
12906 +void au_xigen_inc(struct inode *inode)
12907 +{
12908 +       loff_t pos;
12909 +       ssize_t sz;
12910 +       __u32 igen;
12911 +       struct super_block *sb;
12912 +       struct au_sbinfo *sbinfo;
12913 +
12914 +       sb = inode->i_sb;
12915 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12916 +
12917 +       sbinfo = au_sbi(sb);
12918 +       pos = inode->i_ino;
12919 +       pos *= sizeof(igen);
12920 +       igen = inode->i_generation + 1;
12921 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12922 +       if (sz == sizeof(igen))
12923 +               return; /* success */
12924 +
12925 +       if (unlikely(sz >= 0))
12926 +               AuIOErr("xigen error (%zd)\n", sz);
12927 +}
12928 +
12929 +int au_xigen_new(struct inode *inode)
12930 +{
12931 +       int err;
12932 +       loff_t pos;
12933 +       ssize_t sz;
12934 +       struct super_block *sb;
12935 +       struct au_sbinfo *sbinfo;
12936 +       struct file *file;
12937 +
12938 +       err = 0;
12939 +       /* todo: dirty, at mount time */
12940 +       if (inode->i_ino == AUFS_ROOT_INO)
12941 +               goto out;
12942 +       sb = inode->i_sb;
12943 +       SiMustAnyLock(sb);
12944 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12945 +               goto out;
12946 +
12947 +       err = -EFBIG;
12948 +       pos = inode->i_ino;
12949 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12950 +               AuIOErr1("too large i%lld\n", pos);
12951 +               goto out;
12952 +       }
12953 +       pos *= sizeof(inode->i_generation);
12954 +
12955 +       err = 0;
12956 +       sbinfo = au_sbi(sb);
12957 +       file = sbinfo->si_xigen;
12958 +       BUG_ON(!file);
12959 +
12960 +       if (vfsub_f_size_read(file)
12961 +           < pos + sizeof(inode->i_generation)) {
12962 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12963 +               sz = xino_fwrite(file, &inode->i_generation,
12964 +                                sizeof(inode->i_generation), &pos);
12965 +       } else
12966 +               sz = xino_fread(file, &inode->i_generation,
12967 +                               sizeof(inode->i_generation), &pos);
12968 +       if (sz == sizeof(inode->i_generation))
12969 +               goto out; /* success */
12970 +
12971 +       err = sz;
12972 +       if (unlikely(sz >= 0)) {
12973 +               err = -EIO;
12974 +               AuIOErr("xigen error (%zd)\n", sz);
12975 +       }
12976 +
12977 +out:
12978 +       return err;
12979 +}
12980 +
12981 +int au_xigen_set(struct super_block *sb, struct path *path)
12982 +{
12983 +       int err;
12984 +       struct au_sbinfo *sbinfo;
12985 +       struct file *file;
12986 +
12987 +       SiMustWriteLock(sb);
12988 +
12989 +       sbinfo = au_sbi(sb);
12990 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12991 +       err = PTR_ERR(file);
12992 +       if (IS_ERR(file))
12993 +               goto out;
12994 +       err = 0;
12995 +       if (sbinfo->si_xigen)
12996 +               fput(sbinfo->si_xigen);
12997 +       sbinfo->si_xigen = file;
12998 +
12999 +out:
13000 +       AuTraceErr(err);
13001 +       return err;
13002 +}
13003 +
13004 +void au_xigen_clr(struct super_block *sb)
13005 +{
13006 +       struct au_sbinfo *sbinfo;
13007 +
13008 +       SiMustWriteLock(sb);
13009 +
13010 +       sbinfo = au_sbi(sb);
13011 +       if (sbinfo->si_xigen) {
13012 +               fput(sbinfo->si_xigen);
13013 +               sbinfo->si_xigen = NULL;
13014 +       }
13015 +}
13016 +
13017 +/* ---------------------------------------------------------------------- */
13018 +
13019 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13020 +                                   ino_t dir_ino)
13021 +{
13022 +       struct dentry *dentry, *d;
13023 +       struct inode *inode;
13024 +       unsigned int sigen;
13025 +
13026 +       dentry = NULL;
13027 +       inode = ilookup(sb, ino);
13028 +       if (!inode)
13029 +               goto out;
13030 +
13031 +       dentry = ERR_PTR(-ESTALE);
13032 +       sigen = au_sigen(sb);
13033 +       if (unlikely(au_is_bad_inode(inode)
13034 +                    || IS_DEADDIR(inode)
13035 +                    || sigen != au_iigen(inode, NULL)))
13036 +               goto out_iput;
13037 +
13038 +       dentry = NULL;
13039 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13040 +               dentry = d_find_alias(inode);
13041 +       else {
13042 +               spin_lock(&inode->i_lock);
13043 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13044 +                       spin_lock(&d->d_lock);
13045 +                       if (!au_test_anon(d)
13046 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13047 +                               dentry = dget_dlock(d);
13048 +                               spin_unlock(&d->d_lock);
13049 +                               break;
13050 +                       }
13051 +                       spin_unlock(&d->d_lock);
13052 +               }
13053 +               spin_unlock(&inode->i_lock);
13054 +       }
13055 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13056 +               /* need to refresh */
13057 +               dput(dentry);
13058 +               dentry = NULL;
13059 +       }
13060 +
13061 +out_iput:
13062 +       iput(inode);
13063 +out:
13064 +       AuTraceErrPtr(dentry);
13065 +       return dentry;
13066 +}
13067 +
13068 +/* ---------------------------------------------------------------------- */
13069 +
13070 +/* todo: dirty? */
13071 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13072 +
13073 +struct au_compare_mnt_args {
13074 +       /* input */
13075 +       struct super_block *sb;
13076 +
13077 +       /* output */
13078 +       struct vfsmount *mnt;
13079 +};
13080 +
13081 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13082 +{
13083 +       struct au_compare_mnt_args *a = arg;
13084 +
13085 +       if (mnt->mnt_sb != a->sb)
13086 +               return 0;
13087 +       a->mnt = mntget(mnt);
13088 +       return 1;
13089 +}
13090 +
13091 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13092 +{
13093 +       int err;
13094 +       struct path root;
13095 +       struct au_compare_mnt_args args = {
13096 +               .sb = sb
13097 +       };
13098 +
13099 +       get_fs_root(current->fs, &root);
13100 +       rcu_read_lock();
13101 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13102 +       rcu_read_unlock();
13103 +       path_put(&root);
13104 +       AuDebugOn(!err);
13105 +       AuDebugOn(!args.mnt);
13106 +       return args.mnt;
13107 +}
13108 +
13109 +struct au_nfsd_si_lock {
13110 +       unsigned int sigen;
13111 +       aufs_bindex_t bindex, br_id;
13112 +       unsigned char force_lock;
13113 +};
13114 +
13115 +static int si_nfsd_read_lock(struct super_block *sb,
13116 +                            struct au_nfsd_si_lock *nsi_lock)
13117 +{
13118 +       int err;
13119 +       aufs_bindex_t bindex;
13120 +
13121 +       si_read_lock(sb, AuLock_FLUSH);
13122 +
13123 +       /* branch id may be wrapped around */
13124 +       err = 0;
13125 +       bindex = au_br_index(sb, nsi_lock->br_id);
13126 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13127 +               goto out; /* success */
13128 +
13129 +       err = -ESTALE;
13130 +       bindex = -1;
13131 +       if (!nsi_lock->force_lock)
13132 +               si_read_unlock(sb);
13133 +
13134 +out:
13135 +       nsi_lock->bindex = bindex;
13136 +       return err;
13137 +}
13138 +
13139 +struct find_name_by_ino {
13140 +       struct dir_context ctx;
13141 +       int called, found;
13142 +       ino_t ino;
13143 +       char *name;
13144 +       int namelen;
13145 +};
13146 +
13147 +static bool
13148 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13149 +                loff_t offset, u64 ino, unsigned int d_type)
13150 +{
13151 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13152 +                                                 ctx);
13153 +
13154 +       a->called++;
13155 +       if (a->ino != ino)
13156 +               return true;
13157 +
13158 +       memcpy(a->name, name, namelen);
13159 +       a->namelen = namelen;
13160 +       a->found = 1;
13161 +       return false;
13162 +}
13163 +
13164 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13165 +                                    struct au_nfsd_si_lock *nsi_lock)
13166 +{
13167 +       struct dentry *dentry, *parent;
13168 +       struct file *file;
13169 +       struct inode *dir;
13170 +       struct find_name_by_ino arg = {
13171 +               .ctx = {
13172 +                       .actor = find_name_by_ino
13173 +               }
13174 +       };
13175 +       int err;
13176 +
13177 +       parent = path->dentry;
13178 +       if (nsi_lock)
13179 +               si_read_unlock(parent->d_sb);
13180 +       file = vfsub_dentry_open(path, au_dir_roflags);
13181 +       dentry = (void *)file;
13182 +       if (IS_ERR(file))
13183 +               goto out;
13184 +
13185 +       dentry = ERR_PTR(-ENOMEM);
13186 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13187 +       if (unlikely(!arg.name))
13188 +               goto out_file;
13189 +       arg.ino = ino;
13190 +       arg.found = 0;
13191 +       do {
13192 +               arg.called = 0;
13193 +               /* smp_mb(); */
13194 +               err = vfsub_iterate_dir(file, &arg.ctx);
13195 +       } while (!err && !arg.found && arg.called);
13196 +       dentry = ERR_PTR(err);
13197 +       if (unlikely(err))
13198 +               goto out_name;
13199 +       /* instead of ENOENT */
13200 +       dentry = ERR_PTR(-ESTALE);
13201 +       if (!arg.found)
13202 +               goto out_name;
13203 +
13204 +       /* do not call vfsub_lkup_one() */
13205 +       dir = d_inode(parent);
13206 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13207 +       AuTraceErrPtr(dentry);
13208 +       if (IS_ERR(dentry))
13209 +               goto out_name;
13210 +       AuDebugOn(au_test_anon(dentry));
13211 +       if (unlikely(d_really_is_negative(dentry))) {
13212 +               dput(dentry);
13213 +               dentry = ERR_PTR(-ENOENT);
13214 +       }
13215 +
13216 +out_name:
13217 +       free_page((unsigned long)arg.name);
13218 +out_file:
13219 +       fput(file);
13220 +out:
13221 +       if (unlikely(nsi_lock
13222 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13223 +               if (!IS_ERR(dentry)) {
13224 +                       dput(dentry);
13225 +                       dentry = ERR_PTR(-ESTALE);
13226 +               }
13227 +       AuTraceErrPtr(dentry);
13228 +       return dentry;
13229 +}
13230 +
13231 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13232 +                                       ino_t dir_ino,
13233 +                                       struct au_nfsd_si_lock *nsi_lock)
13234 +{
13235 +       struct dentry *dentry;
13236 +       struct path path;
13237 +
13238 +       if (dir_ino != AUFS_ROOT_INO) {
13239 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13240 +               dentry = path.dentry;
13241 +               if (!path.dentry || IS_ERR(path.dentry))
13242 +                       goto out;
13243 +               AuDebugOn(au_test_anon(path.dentry));
13244 +       } else
13245 +               path.dentry = dget(sb->s_root);
13246 +
13247 +       path.mnt = au_mnt_get(sb);
13248 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13249 +       path_put(&path);
13250 +
13251 +out:
13252 +       AuTraceErrPtr(dentry);
13253 +       return dentry;
13254 +}
13255 +
13256 +/* ---------------------------------------------------------------------- */
13257 +
13258 +static int h_acceptable(void *expv, struct dentry *dentry)
13259 +{
13260 +       return 1;
13261 +}
13262 +
13263 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13264 +                          char *buf, int len, struct super_block *sb)
13265 +{
13266 +       char *p;
13267 +       int n;
13268 +       struct path path;
13269 +
13270 +       p = d_path(h_rootpath, buf, len);
13271 +       if (IS_ERR(p))
13272 +               goto out;
13273 +       n = strlen(p);
13274 +
13275 +       path.mnt = h_rootpath->mnt;
13276 +       path.dentry = h_parent;
13277 +       p = d_path(&path, buf, len);
13278 +       if (IS_ERR(p))
13279 +               goto out;
13280 +       if (n != 1)
13281 +               p += n;
13282 +
13283 +       path.mnt = au_mnt_get(sb);
13284 +       path.dentry = sb->s_root;
13285 +       p = d_path(&path, buf, len - strlen(p));
13286 +       mntput(path.mnt);
13287 +       if (IS_ERR(p))
13288 +               goto out;
13289 +       if (n != 1)
13290 +               p[strlen(p)] = '/';
13291 +
13292 +out:
13293 +       AuTraceErrPtr(p);
13294 +       return p;
13295 +}
13296 +
13297 +static
13298 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13299 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13300 +{
13301 +       struct dentry *dentry, *h_parent, *root;
13302 +       struct super_block *h_sb;
13303 +       char *pathname, *p;
13304 +       struct vfsmount *h_mnt;
13305 +       struct au_branch *br;
13306 +       int err;
13307 +       struct path path;
13308 +
13309 +       br = au_sbr(sb, nsi_lock->bindex);
13310 +       h_mnt = au_br_mnt(br);
13311 +       h_sb = h_mnt->mnt_sb;
13312 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13313 +       lockdep_off();
13314 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13315 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13316 +                                     h_acceptable, /*context*/NULL);
13317 +       lockdep_on();
13318 +       dentry = h_parent;
13319 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13320 +               AuWarn1("%s decode_fh failed, %ld\n",
13321 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13322 +               goto out;
13323 +       }
13324 +       dentry = NULL;
13325 +       if (unlikely(au_test_anon(h_parent))) {
13326 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13327 +                       au_sbtype(h_sb));
13328 +               goto out_h_parent;
13329 +       }
13330 +
13331 +       dentry = ERR_PTR(-ENOMEM);
13332 +       pathname = (void *)__get_free_page(GFP_NOFS);
13333 +       if (unlikely(!pathname))
13334 +               goto out_h_parent;
13335 +
13336 +       root = sb->s_root;
13337 +       path.mnt = h_mnt;
13338 +       di_read_lock_parent(root, !AuLock_IR);
13339 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13340 +       di_read_unlock(root, !AuLock_IR);
13341 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13342 +       dentry = (void *)p;
13343 +       if (IS_ERR(p))
13344 +               goto out_pathname;
13345 +
13346 +       si_read_unlock(sb);
13347 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13348 +       dentry = ERR_PTR(err);
13349 +       if (unlikely(err))
13350 +               goto out_relock;
13351 +
13352 +       dentry = ERR_PTR(-ENOENT);
13353 +       AuDebugOn(au_test_anon(path.dentry));
13354 +       if (unlikely(d_really_is_negative(path.dentry)))
13355 +               goto out_path;
13356 +
13357 +       if (ino != d_inode(path.dentry)->i_ino)
13358 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13359 +       else
13360 +               dentry = dget(path.dentry);
13361 +
13362 +out_path:
13363 +       path_put(&path);
13364 +out_relock:
13365 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13366 +               if (!IS_ERR(dentry)) {
13367 +                       dput(dentry);
13368 +                       dentry = ERR_PTR(-ESTALE);
13369 +               }
13370 +out_pathname:
13371 +       free_page((unsigned long)pathname);
13372 +out_h_parent:
13373 +       dput(h_parent);
13374 +out:
13375 +       AuTraceErrPtr(dentry);
13376 +       return dentry;
13377 +}
13378 +
13379 +/* ---------------------------------------------------------------------- */
13380 +
13381 +static struct dentry *
13382 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13383 +                 int fh_type)
13384 +{
13385 +       struct dentry *dentry;
13386 +       __u32 *fh = fid->raw;
13387 +       struct au_branch *br;
13388 +       ino_t ino, dir_ino;
13389 +       struct au_nfsd_si_lock nsi_lock = {
13390 +               .force_lock     = 0
13391 +       };
13392 +
13393 +       dentry = ERR_PTR(-ESTALE);
13394 +       /* it should never happen, but the file handle is unreliable */
13395 +       if (unlikely(fh_len < Fh_tail))
13396 +               goto out;
13397 +       nsi_lock.sigen = fh[Fh_sigen];
13398 +       nsi_lock.br_id = fh[Fh_br_id];
13399 +
13400 +       /* branch id may be wrapped around */
13401 +       br = NULL;
13402 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13403 +               goto out;
13404 +       nsi_lock.force_lock = 1;
13405 +
13406 +       /* is this inode still cached? */
13407 +       ino = decode_ino(fh + Fh_ino);
13408 +       /* it should never happen */
13409 +       if (unlikely(ino == AUFS_ROOT_INO))
13410 +               goto out_unlock;
13411 +
13412 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13413 +       dentry = decode_by_ino(sb, ino, dir_ino);
13414 +       if (IS_ERR(dentry))
13415 +               goto out_unlock;
13416 +       if (dentry)
13417 +               goto accept;
13418 +
13419 +       /* is the parent dir cached? */
13420 +       br = au_sbr(sb, nsi_lock.bindex);
13421 +       au_lcnt_inc(&br->br_nfiles);
13422 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13423 +       if (IS_ERR(dentry))
13424 +               goto out_unlock;
13425 +       if (dentry)
13426 +               goto accept;
13427 +
13428 +       /* lookup path */
13429 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13430 +       if (IS_ERR(dentry))
13431 +               goto out_unlock;
13432 +       if (unlikely(!dentry))
13433 +               /* todo?: make it ESTALE */
13434 +               goto out_unlock;
13435 +
13436 +accept:
13437 +       if (!au_digen_test(dentry, au_sigen(sb))
13438 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13439 +               goto out_unlock; /* success */
13440 +
13441 +       dput(dentry);
13442 +       dentry = ERR_PTR(-ESTALE);
13443 +out_unlock:
13444 +       if (br)
13445 +               au_lcnt_dec(&br->br_nfiles);
13446 +       si_read_unlock(sb);
13447 +out:
13448 +       AuTraceErrPtr(dentry);
13449 +       return dentry;
13450 +}
13451 +
13452 +#if 0 /* reserved for future use */
13453 +/* support subtreecheck option */
13454 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13455 +                                       int fh_len, int fh_type)
13456 +{
13457 +       struct dentry *parent;
13458 +       __u32 *fh = fid->raw;
13459 +       ino_t dir_ino;
13460 +
13461 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13462 +       parent = decode_by_ino(sb, dir_ino, 0);
13463 +       if (IS_ERR(parent))
13464 +               goto out;
13465 +       if (!parent)
13466 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13467 +                                       dir_ino, fh, fh_len);
13468 +
13469 +out:
13470 +       AuTraceErrPtr(parent);
13471 +       return parent;
13472 +}
13473 +#endif
13474 +
13475 +/* ---------------------------------------------------------------------- */
13476 +
13477 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13478 +                         struct inode *dir)
13479 +{
13480 +       int err;
13481 +       aufs_bindex_t bindex;
13482 +       struct super_block *sb, *h_sb;
13483 +       struct dentry *dentry, *parent, *h_parent;
13484 +       struct inode *h_dir;
13485 +       struct au_branch *br;
13486 +
13487 +       err = -ENOSPC;
13488 +       if (unlikely(*max_len <= Fh_tail)) {
13489 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13490 +               goto out;
13491 +       }
13492 +
13493 +       err = FILEID_ROOT;
13494 +       if (inode->i_ino == AUFS_ROOT_INO) {
13495 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13496 +               goto out;
13497 +       }
13498 +
13499 +       h_parent = NULL;
13500 +       sb = inode->i_sb;
13501 +       err = si_read_lock(sb, AuLock_FLUSH);
13502 +       if (unlikely(err))
13503 +               goto out;
13504 +
13505 +#ifdef CONFIG_AUFS_DEBUG
13506 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13507 +               AuWarn1("NFS-exporting requires xino\n");
13508 +#endif
13509 +       err = -EIO;
13510 +       parent = NULL;
13511 +       ii_read_lock_child(inode);
13512 +       bindex = au_ibtop(inode);
13513 +       if (!dir) {
13514 +               dentry = d_find_any_alias(inode);
13515 +               if (unlikely(!dentry))
13516 +                       goto out_unlock;
13517 +               AuDebugOn(au_test_anon(dentry));
13518 +               parent = dget_parent(dentry);
13519 +               dput(dentry);
13520 +               if (unlikely(!parent))
13521 +                       goto out_unlock;
13522 +               if (d_really_is_positive(parent))
13523 +                       dir = d_inode(parent);
13524 +       }
13525 +
13526 +       ii_read_lock_parent(dir);
13527 +       h_dir = au_h_iptr(dir, bindex);
13528 +       ii_read_unlock(dir);
13529 +       if (unlikely(!h_dir))
13530 +               goto out_parent;
13531 +       h_parent = d_find_any_alias(h_dir);
13532 +       if (unlikely(!h_parent))
13533 +               goto out_hparent;
13534 +
13535 +       err = -EPERM;
13536 +       br = au_sbr(sb, bindex);
13537 +       h_sb = au_br_sb(br);
13538 +       if (unlikely(!h_sb->s_export_op)) {
13539 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13540 +               goto out_hparent;
13541 +       }
13542 +
13543 +       fh[Fh_br_id] = br->br_id;
13544 +       fh[Fh_sigen] = au_sigen(sb);
13545 +       encode_ino(fh + Fh_ino, inode->i_ino);
13546 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13547 +       fh[Fh_igen] = inode->i_generation;
13548 +
13549 +       *max_len -= Fh_tail;
13550 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13551 +                                          max_len,
13552 +                                          /*connectable or subtreecheck*/0);
13553 +       err = fh[Fh_h_type];
13554 +       *max_len += Fh_tail;
13555 +       /* todo: macros? */
13556 +       if (err != FILEID_INVALID)
13557 +               err = 99;
13558 +       else
13559 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13560 +
13561 +out_hparent:
13562 +       dput(h_parent);
13563 +out_parent:
13564 +       dput(parent);
13565 +out_unlock:
13566 +       ii_read_unlock(inode);
13567 +       si_read_unlock(sb);
13568 +out:
13569 +       if (unlikely(err < 0))
13570 +               err = FILEID_INVALID;
13571 +       return err;
13572 +}
13573 +
13574 +/* ---------------------------------------------------------------------- */
13575 +
13576 +static int aufs_commit_metadata(struct inode *inode)
13577 +{
13578 +       int err;
13579 +       aufs_bindex_t bindex;
13580 +       struct super_block *sb;
13581 +       struct inode *h_inode;
13582 +       int (*f)(struct inode *inode);
13583 +
13584 +       sb = inode->i_sb;
13585 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13586 +       ii_write_lock_child(inode);
13587 +       bindex = au_ibtop(inode);
13588 +       AuDebugOn(bindex < 0);
13589 +       h_inode = au_h_iptr(inode, bindex);
13590 +
13591 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13592 +       if (f)
13593 +               err = f(h_inode);
13594 +       else
13595 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13596 +
13597 +       au_cpup_attr_timesizes(inode);
13598 +       ii_write_unlock(inode);
13599 +       si_read_unlock(sb);
13600 +       return err;
13601 +}
13602 +
13603 +/* ---------------------------------------------------------------------- */
13604 +
13605 +static struct export_operations aufs_export_op = {
13606 +       .fh_to_dentry           = aufs_fh_to_dentry,
13607 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13608 +       .encode_fh              = aufs_encode_fh,
13609 +       .commit_metadata        = aufs_commit_metadata
13610 +};
13611 +
13612 +void au_export_init(struct super_block *sb)
13613 +{
13614 +       struct au_sbinfo *sbinfo;
13615 +       __u32 u;
13616 +
13617 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13618 +                        && IS_MODULE(CONFIG_EXPORTFS),
13619 +                        AUFS_NAME ": unsupported configuration "
13620 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13621 +
13622 +       sb->s_export_op = &aufs_export_op;
13623 +       sbinfo = au_sbi(sb);
13624 +       sbinfo->si_xigen = NULL;
13625 +       get_random_bytes(&u, sizeof(u));
13626 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13627 +       atomic_set(&sbinfo->si_xigen_next, u);
13628 +}
13629 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13630 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13631 +++ linux/fs/aufs/fhsm.c        2022-11-05 23:02:18.962555950 +0100
13632 @@ -0,0 +1,426 @@
13633 +// SPDX-License-Identifier: GPL-2.0
13634 +/*
13635 + * Copyright (C) 2011-2022 Junjiro R. Okajima
13636 + *
13637 + * This program is free software; you can redistribute it and/or modify
13638 + * it under the terms of the GNU General Public License as published by
13639 + * the Free Software Foundation; either version 2 of the License, or
13640 + * (at your option) any later version.
13641 + *
13642 + * This program is distributed in the hope that it will be useful,
13643 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13644 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13645 + * GNU General Public License for more details.
13646 + *
13647 + * You should have received a copy of the GNU General Public License
13648 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13649 + */
13650 +
13651 +/*
13652 + * File-based Hierarchy Storage Management
13653 + */
13654 +
13655 +#include <linux/anon_inodes.h>
13656 +#include <linux/poll.h>
13657 +#include <linux/seq_file.h>
13658 +#include <linux/statfs.h>
13659 +#include "aufs.h"
13660 +
13661 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13662 +{
13663 +       struct au_sbinfo *sbinfo;
13664 +       struct au_fhsm *fhsm;
13665 +
13666 +       SiMustAnyLock(sb);
13667 +
13668 +       sbinfo = au_sbi(sb);
13669 +       fhsm = &sbinfo->si_fhsm;
13670 +       AuDebugOn(!fhsm);
13671 +       return fhsm->fhsm_bottom;
13672 +}
13673 +
13674 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13675 +{
13676 +       struct au_sbinfo *sbinfo;
13677 +       struct au_fhsm *fhsm;
13678 +
13679 +       SiMustWriteLock(sb);
13680 +
13681 +       sbinfo = au_sbi(sb);
13682 +       fhsm = &sbinfo->si_fhsm;
13683 +       AuDebugOn(!fhsm);
13684 +       fhsm->fhsm_bottom = bindex;
13685 +}
13686 +
13687 +/* ---------------------------------------------------------------------- */
13688 +
13689 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13690 +{
13691 +       struct au_br_fhsm *bf;
13692 +
13693 +       bf = br->br_fhsm;
13694 +       MtxMustLock(&bf->bf_lock);
13695 +
13696 +       return !bf->bf_readable
13697 +               || time_after(jiffies,
13698 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13699 +}
13700 +
13701 +/* ---------------------------------------------------------------------- */
13702 +
13703 +static void au_fhsm_notify(struct super_block *sb, int val)
13704 +{
13705 +       struct au_sbinfo *sbinfo;
13706 +       struct au_fhsm *fhsm;
13707 +
13708 +       SiMustAnyLock(sb);
13709 +
13710 +       sbinfo = au_sbi(sb);
13711 +       fhsm = &sbinfo->si_fhsm;
13712 +       if (au_fhsm_pid(fhsm)
13713 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13714 +               atomic_set(&fhsm->fhsm_readable, val);
13715 +               if (val)
13716 +                       wake_up(&fhsm->fhsm_wqh);
13717 +       }
13718 +}
13719 +
13720 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13721 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13722 +{
13723 +       int err;
13724 +       struct au_branch *br;
13725 +       struct au_br_fhsm *bf;
13726 +
13727 +       br = au_sbr(sb, bindex);
13728 +       AuDebugOn(au_br_rdonly(br));
13729 +       bf = br->br_fhsm;
13730 +       AuDebugOn(!bf);
13731 +
13732 +       if (do_lock)
13733 +               mutex_lock(&bf->bf_lock);
13734 +       else
13735 +               MtxMustLock(&bf->bf_lock);
13736 +
13737 +       /* sb->s_root for NFS is unreliable */
13738 +       err = au_br_stfs(br, &bf->bf_stfs);
13739 +       if (unlikely(err)) {
13740 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13741 +               goto out;
13742 +       }
13743 +
13744 +       bf->bf_jiffy = jiffies;
13745 +       bf->bf_readable = 1;
13746 +       if (do_notify)
13747 +               au_fhsm_notify(sb, /*val*/1);
13748 +       if (rstfs)
13749 +               *rstfs = bf->bf_stfs;
13750 +
13751 +out:
13752 +       if (do_lock)
13753 +               mutex_unlock(&bf->bf_lock);
13754 +       au_fhsm_notify(sb, /*val*/1);
13755 +
13756 +       return err;
13757 +}
13758 +
13759 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13760 +{
13761 +       int err;
13762 +       struct au_sbinfo *sbinfo;
13763 +       struct au_fhsm *fhsm;
13764 +       struct au_branch *br;
13765 +       struct au_br_fhsm *bf;
13766 +
13767 +       AuDbg("b%d, force %d\n", bindex, force);
13768 +       SiMustAnyLock(sb);
13769 +
13770 +       sbinfo = au_sbi(sb);
13771 +       fhsm = &sbinfo->si_fhsm;
13772 +       if (!au_ftest_si(sbinfo, FHSM)
13773 +           || fhsm->fhsm_bottom == bindex)
13774 +               return;
13775 +
13776 +       br = au_sbr(sb, bindex);
13777 +       bf = br->br_fhsm;
13778 +       AuDebugOn(!bf);
13779 +       mutex_lock(&bf->bf_lock);
13780 +       if (force
13781 +           || au_fhsm_pid(fhsm)
13782 +           || au_fhsm_test_jiffy(sbinfo, br))
13783 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13784 +                                 /*do_notify*/1);
13785 +       mutex_unlock(&bf->bf_lock);
13786 +}
13787 +
13788 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13789 +{
13790 +       aufs_bindex_t bindex, bbot;
13791 +       struct au_branch *br;
13792 +
13793 +       /* exclude the bottom */
13794 +       bbot = au_fhsm_bottom(sb);
13795 +       for (bindex = 0; bindex < bbot; bindex++) {
13796 +               br = au_sbr(sb, bindex);
13797 +               if (au_br_fhsm(br->br_perm))
13798 +                       au_fhsm_wrote(sb, bindex, force);
13799 +       }
13800 +}
13801 +
13802 +/* ---------------------------------------------------------------------- */
13803 +
13804 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13805 +{
13806 +       __poll_t mask;
13807 +       struct au_sbinfo *sbinfo;
13808 +       struct au_fhsm *fhsm;
13809 +
13810 +       mask = 0;
13811 +       sbinfo = file->private_data;
13812 +       fhsm = &sbinfo->si_fhsm;
13813 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13814 +       if (atomic_read(&fhsm->fhsm_readable))
13815 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13816 +
13817 +       if (!mask)
13818 +               AuDbg("mask 0x%x\n", mask);
13819 +       return mask;
13820 +}
13821 +
13822 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13823 +                             struct aufs_stfs *stfs, __s16 brid)
13824 +{
13825 +       int err;
13826 +
13827 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13828 +       if (!err)
13829 +               err = __put_user(brid, &stbr->brid);
13830 +       if (unlikely(err))
13831 +               err = -EFAULT;
13832 +
13833 +       return err;
13834 +}
13835 +
13836 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13837 +                              struct aufs_stbr __user *stbr, size_t count)
13838 +{
13839 +       ssize_t err;
13840 +       int nstbr;
13841 +       aufs_bindex_t bindex, bbot;
13842 +       struct au_branch *br;
13843 +       struct au_br_fhsm *bf;
13844 +
13845 +       /* except the bottom branch */
13846 +       err = 0;
13847 +       nstbr = 0;
13848 +       bbot = au_fhsm_bottom(sb);
13849 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13850 +               br = au_sbr(sb, bindex);
13851 +               if (!au_br_fhsm(br->br_perm))
13852 +                       continue;
13853 +
13854 +               bf = br->br_fhsm;
13855 +               mutex_lock(&bf->bf_lock);
13856 +               if (bf->bf_readable) {
13857 +                       err = -EFAULT;
13858 +                       if (count >= sizeof(*stbr))
13859 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13860 +                                                         br->br_id);
13861 +                       if (!err) {
13862 +                               bf->bf_readable = 0;
13863 +                               count -= sizeof(*stbr);
13864 +                               nstbr++;
13865 +                       }
13866 +               }
13867 +               mutex_unlock(&bf->bf_lock);
13868 +       }
13869 +       if (!err)
13870 +               err = sizeof(*stbr) * nstbr;
13871 +
13872 +       return err;
13873 +}
13874 +
13875 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13876 +                          loff_t *pos)
13877 +{
13878 +       ssize_t err;
13879 +       int readable;
13880 +       aufs_bindex_t nfhsm, bindex, bbot;
13881 +       struct au_sbinfo *sbinfo;
13882 +       struct au_fhsm *fhsm;
13883 +       struct au_branch *br;
13884 +       struct super_block *sb;
13885 +
13886 +       err = 0;
13887 +       sbinfo = file->private_data;
13888 +       fhsm = &sbinfo->si_fhsm;
13889 +need_data:
13890 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13891 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13892 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13893 +                       err = -EAGAIN;
13894 +               else
13895 +                       err = wait_event_interruptible_locked_irq
13896 +                               (fhsm->fhsm_wqh,
13897 +                                atomic_read(&fhsm->fhsm_readable));
13898 +       }
13899 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13900 +       if (unlikely(err))
13901 +               goto out;
13902 +
13903 +       /* sb may already be dead */
13904 +       au_rw_read_lock(&sbinfo->si_rwsem);
13905 +       readable = atomic_read(&fhsm->fhsm_readable);
13906 +       if (readable > 0) {
13907 +               sb = sbinfo->si_sb;
13908 +               AuDebugOn(!sb);
13909 +               /* exclude the bottom branch */
13910 +               nfhsm = 0;
13911 +               bbot = au_fhsm_bottom(sb);
13912 +               for (bindex = 0; bindex < bbot; bindex++) {
13913 +                       br = au_sbr(sb, bindex);
13914 +                       if (au_br_fhsm(br->br_perm))
13915 +                               nfhsm++;
13916 +               }
13917 +               err = -EMSGSIZE;
13918 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13919 +                       atomic_set(&fhsm->fhsm_readable, 0);
13920 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13921 +                                            count);
13922 +               }
13923 +       }
13924 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13925 +       if (!readable)
13926 +               goto need_data;
13927 +
13928 +out:
13929 +       return err;
13930 +}
13931 +
13932 +static int au_fhsm_release(struct inode *inode, struct file *file)
13933 +{
13934 +       struct au_sbinfo *sbinfo;
13935 +       struct au_fhsm *fhsm;
13936 +
13937 +       /* sb may already be dead */
13938 +       sbinfo = file->private_data;
13939 +       fhsm = &sbinfo->si_fhsm;
13940 +       spin_lock(&fhsm->fhsm_spin);
13941 +       fhsm->fhsm_pid = 0;
13942 +       spin_unlock(&fhsm->fhsm_spin);
13943 +       kobject_put(&sbinfo->si_kobj);
13944 +
13945 +       return 0;
13946 +}
13947 +
13948 +static const struct file_operations au_fhsm_fops = {
13949 +       .owner          = THIS_MODULE,
13950 +       .llseek         = noop_llseek,
13951 +       .read           = au_fhsm_read,
13952 +       .poll           = au_fhsm_poll,
13953 +       .release        = au_fhsm_release
13954 +};
13955 +
13956 +int au_fhsm_fd(struct super_block *sb, int oflags)
13957 +{
13958 +       int err, fd;
13959 +       struct au_sbinfo *sbinfo;
13960 +       struct au_fhsm *fhsm;
13961 +
13962 +       err = -EPERM;
13963 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13964 +               goto out;
13965 +
13966 +       err = -EINVAL;
13967 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13968 +               goto out;
13969 +
13970 +       err = 0;
13971 +       sbinfo = au_sbi(sb);
13972 +       fhsm = &sbinfo->si_fhsm;
13973 +       spin_lock(&fhsm->fhsm_spin);
13974 +       if (!fhsm->fhsm_pid)
13975 +               fhsm->fhsm_pid = current->pid;
13976 +       else
13977 +               err = -EBUSY;
13978 +       spin_unlock(&fhsm->fhsm_spin);
13979 +       if (unlikely(err))
13980 +               goto out;
13981 +
13982 +       oflags |= O_RDONLY;
13983 +       /* oflags |= FMODE_NONOTIFY; */
13984 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13985 +       err = fd;
13986 +       if (unlikely(fd < 0))
13987 +               goto out_pid;
13988 +
13989 +       /* succeed regardless 'fhsm' status */
13990 +       kobject_get(&sbinfo->si_kobj);
13991 +       si_noflush_read_lock(sb);
13992 +       if (au_ftest_si(sbinfo, FHSM))
13993 +               au_fhsm_wrote_all(sb, /*force*/0);
13994 +       si_read_unlock(sb);
13995 +       goto out; /* success */
13996 +
13997 +out_pid:
13998 +       spin_lock(&fhsm->fhsm_spin);
13999 +       fhsm->fhsm_pid = 0;
14000 +       spin_unlock(&fhsm->fhsm_spin);
14001 +out:
14002 +       AuTraceErr(err);
14003 +       return err;
14004 +}
14005 +
14006 +/* ---------------------------------------------------------------------- */
14007 +
14008 +int au_fhsm_br_alloc(struct au_branch *br)
14009 +{
14010 +       int err;
14011 +
14012 +       err = 0;
14013 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14014 +       if (br->br_fhsm)
14015 +               au_br_fhsm_init(br->br_fhsm);
14016 +       else
14017 +               err = -ENOMEM;
14018 +
14019 +       return err;
14020 +}
14021 +
14022 +/* ---------------------------------------------------------------------- */
14023 +
14024 +void au_fhsm_fin(struct super_block *sb)
14025 +{
14026 +       au_fhsm_notify(sb, /*val*/-1);
14027 +}
14028 +
14029 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14030 +{
14031 +       struct au_fhsm *fhsm;
14032 +
14033 +       fhsm = &sbinfo->si_fhsm;
14034 +       spin_lock_init(&fhsm->fhsm_spin);
14035 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14036 +       atomic_set(&fhsm->fhsm_readable, 0);
14037 +       fhsm->fhsm_expire
14038 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14039 +       fhsm->fhsm_bottom = -1;
14040 +}
14041 +
14042 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14043 +{
14044 +       sbinfo->si_fhsm.fhsm_expire
14045 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14046 +}
14047 +
14048 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14049 +{
14050 +       unsigned int u;
14051 +
14052 +       if (!au_ftest_si(sbinfo, FHSM))
14053 +               return;
14054 +
14055 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14056 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14057 +               seq_printf(seq, ",fhsm_sec=%u", u);
14058 +}
14059 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14060 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14061 +++ linux/fs/aufs/file.c        2022-12-17 09:21:34.799855195 +0100
14062 @@ -0,0 +1,860 @@
14063 +// SPDX-License-Identifier: GPL-2.0
14064 +/*
14065 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14066 + *
14067 + * This program is free software; you can redistribute it and/or modify
14068 + * it under the terms of the GNU General Public License as published by
14069 + * the Free Software Foundation; either version 2 of the License, or
14070 + * (at your option) any later version.
14071 + *
14072 + * This program is distributed in the hope that it will be useful,
14073 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14074 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14075 + * GNU General Public License for more details.
14076 + *
14077 + * You should have received a copy of the GNU General Public License
14078 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14079 + */
14080 +
14081 +/*
14082 + * handling file/dir, and address_space operation
14083 + */
14084 +
14085 +#ifdef CONFIG_AUFS_DEBUG
14086 +#include <linux/migrate.h>
14087 +#endif
14088 +#include <linux/pagemap.h>
14089 +#include "aufs.h"
14090 +
14091 +/* drop flags for writing */
14092 +unsigned int au_file_roflags(unsigned int flags)
14093 +{
14094 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14095 +       flags |= O_RDONLY | O_NOATIME;
14096 +       return flags;
14097 +}
14098 +
14099 +/* common functions to regular file and dir */
14100 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14101 +                      struct file *file, int force_wr)
14102 +{
14103 +       struct file *h_file;
14104 +       struct dentry *h_dentry;
14105 +       struct inode *h_inode;
14106 +       struct super_block *sb;
14107 +       struct au_branch *br;
14108 +       struct path h_path;
14109 +       int err;
14110 +
14111 +       /* a race condition can happen between open and unlink/rmdir */
14112 +       h_file = ERR_PTR(-ENOENT);
14113 +       h_dentry = au_h_dptr(dentry, bindex);
14114 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14115 +               goto out;
14116 +       h_inode = d_inode(h_dentry);
14117 +       spin_lock(&h_dentry->d_lock);
14118 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14119 +               /* || !d_inode(dentry)->i_nlink */
14120 +               ;
14121 +       spin_unlock(&h_dentry->d_lock);
14122 +       if (unlikely(err))
14123 +               goto out;
14124 +
14125 +       sb = dentry->d_sb;
14126 +       br = au_sbr(sb, bindex);
14127 +       err = au_br_test_oflag(flags, br);
14128 +       h_file = ERR_PTR(err);
14129 +       if (unlikely(err))
14130 +               goto out;
14131 +
14132 +       /* drop flags for writing */
14133 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14134 +               if (force_wr && !(flags & O_WRONLY))
14135 +                       force_wr = 0;
14136 +               flags = au_file_roflags(flags);
14137 +               if (force_wr) {
14138 +                       h_file = ERR_PTR(-EROFS);
14139 +                       flags = au_file_roflags(flags);
14140 +                       if (unlikely(vfsub_native_ro(h_inode)
14141 +                                    || IS_APPEND(h_inode)))
14142 +                               goto out;
14143 +                       flags &= ~O_ACCMODE;
14144 +                       flags |= O_WRONLY;
14145 +               }
14146 +       }
14147 +       flags &= ~O_CREAT;
14148 +       au_lcnt_inc(&br->br_nfiles);
14149 +       h_path.dentry = h_dentry;
14150 +       h_path.mnt = au_br_mnt(br);
14151 +       h_file = vfsub_dentry_open(&h_path, flags);
14152 +       if (IS_ERR(h_file))
14153 +               goto out_br;
14154 +
14155 +       if (flags & __FMODE_EXEC) {
14156 +               err = deny_write_access(h_file);
14157 +               if (unlikely(err)) {
14158 +                       fput(h_file);
14159 +                       h_file = ERR_PTR(err);
14160 +                       goto out_br;
14161 +               }
14162 +       }
14163 +       fsnotify_open(h_file);
14164 +       goto out; /* success */
14165 +
14166 +out_br:
14167 +       au_lcnt_dec(&br->br_nfiles);
14168 +out:
14169 +       return h_file;
14170 +}
14171 +
14172 +static int au_cmoo(struct dentry *dentry)
14173 +{
14174 +       int err, cmoo, matched;
14175 +       unsigned int udba;
14176 +       struct path h_path;
14177 +       struct au_pin pin;
14178 +       struct au_cp_generic cpg = {
14179 +               .dentry = dentry,
14180 +               .bdst   = -1,
14181 +               .bsrc   = -1,
14182 +               .len    = -1,
14183 +               .pin    = &pin,
14184 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14185 +       };
14186 +       struct inode *delegated;
14187 +       struct super_block *sb;
14188 +       struct au_sbinfo *sbinfo;
14189 +       struct au_fhsm *fhsm;
14190 +       pid_t pid;
14191 +       struct au_branch *br;
14192 +       struct dentry *parent;
14193 +       struct au_hinode *hdir;
14194 +
14195 +       DiMustWriteLock(dentry);
14196 +       IiMustWriteLock(d_inode(dentry));
14197 +
14198 +       err = 0;
14199 +       if (IS_ROOT(dentry))
14200 +               goto out;
14201 +       cpg.bsrc = au_dbtop(dentry);
14202 +       if (!cpg.bsrc)
14203 +               goto out;
14204 +
14205 +       sb = dentry->d_sb;
14206 +       sbinfo = au_sbi(sb);
14207 +       fhsm = &sbinfo->si_fhsm;
14208 +       pid = au_fhsm_pid(fhsm);
14209 +       rcu_read_lock();
14210 +       matched = (pid
14211 +                  && (current->pid == pid
14212 +                      || rcu_dereference(current->real_parent)->pid == pid));
14213 +       rcu_read_unlock();
14214 +       if (matched)
14215 +               goto out;
14216 +
14217 +       br = au_sbr(sb, cpg.bsrc);
14218 +       cmoo = au_br_cmoo(br->br_perm);
14219 +       if (!cmoo)
14220 +               goto out;
14221 +       if (!d_is_reg(dentry))
14222 +               cmoo &= AuBrAttr_COO_ALL;
14223 +       if (!cmoo)
14224 +               goto out;
14225 +
14226 +       parent = dget_parent(dentry);
14227 +       di_write_lock_parent(parent);
14228 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14229 +       cpg.bdst = err;
14230 +       if (unlikely(err < 0)) {
14231 +               err = 0;        /* there is no upper writable branch */
14232 +               goto out_dgrade;
14233 +       }
14234 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14235 +
14236 +       /* do not respect the coo attrib for the target branch */
14237 +       err = au_cpup_dirs(dentry, cpg.bdst);
14238 +       if (unlikely(err))
14239 +               goto out_dgrade;
14240 +
14241 +       di_downgrade_lock(parent, AuLock_IR);
14242 +       udba = au_opt_udba(sb);
14243 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14244 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14245 +       if (unlikely(err))
14246 +               goto out_parent;
14247 +
14248 +       err = au_sio_cpup_simple(&cpg);
14249 +       au_unpin(&pin);
14250 +       if (unlikely(err))
14251 +               goto out_parent;
14252 +       if (!(cmoo & AuBrWAttr_MOO))
14253 +               goto out_parent; /* success */
14254 +
14255 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14256 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14257 +       if (unlikely(err))
14258 +               goto out_parent;
14259 +
14260 +       h_path.mnt = au_br_mnt(br);
14261 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14262 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14263 +       delegated = NULL;
14264 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14265 +       au_unpin(&pin);
14266 +       /* todo: keep h_dentry or not? */
14267 +       if (unlikely(err == -EWOULDBLOCK)) {
14268 +               pr_warn("cannot retry for NFSv4 delegation"
14269 +                       " for an internal unlink\n");
14270 +               iput(delegated);
14271 +       }
14272 +       if (unlikely(err)) {
14273 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14274 +                      dentry, err);
14275 +               err = 0;
14276 +       }
14277 +       goto out_parent; /* success */
14278 +
14279 +out_dgrade:
14280 +       di_downgrade_lock(parent, AuLock_IR);
14281 +out_parent:
14282 +       di_read_unlock(parent, AuLock_IR);
14283 +       dput(parent);
14284 +out:
14285 +       AuTraceErr(err);
14286 +       return err;
14287 +}
14288 +
14289 +int au_do_open(struct file *file, struct au_do_open_args *args)
14290 +{
14291 +       int err, aopen = args->aopen;
14292 +       struct dentry *dentry;
14293 +       struct au_finfo *finfo;
14294 +
14295 +       if (!aopen)
14296 +               err = au_finfo_init(file, args->fidir);
14297 +       else {
14298 +               lockdep_off();
14299 +               err = au_finfo_init(file, args->fidir);
14300 +               lockdep_on();
14301 +       }
14302 +       if (unlikely(err))
14303 +               goto out;
14304 +
14305 +       dentry = file->f_path.dentry;
14306 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14307 +       di_write_lock_child(dentry);
14308 +       err = au_cmoo(dentry);
14309 +       if (!err) {
14310 +               if (!aopen) {
14311 +                       err = args->open(file, vfsub_file_flags(file),
14312 +                                        au_di(dentry)->di_htmpfile);
14313 +                       di_write_unlock(dentry);
14314 +               } else {
14315 +                       di_downgrade_lock(dentry, AuLock_IR);
14316 +                       lockdep_off();
14317 +                       err = args->open(file, vfsub_file_flags(file),
14318 +                                        args->h_file);
14319 +                       lockdep_on();
14320 +                       di_read_unlock(dentry, AuLock_IR);
14321 +               }
14322 +       }
14323 +
14324 +       finfo = au_fi(file);
14325 +       if (!err) {
14326 +               finfo->fi_file = file;
14327 +               au_hbl_add(&finfo->fi_hlist,
14328 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14329 +       }
14330 +       if (!aopen)
14331 +               fi_write_unlock(file);
14332 +       else {
14333 +               lockdep_off();
14334 +               fi_write_unlock(file);
14335 +               lockdep_on();
14336 +       }
14337 +       if (unlikely(err)) {
14338 +               finfo->fi_hdir = NULL;
14339 +               au_finfo_fin(file);
14340 +       }
14341 +
14342 +out:
14343 +       AuTraceErr(err);
14344 +       return err;
14345 +}
14346 +
14347 +int au_reopen_nondir(struct file *file)
14348 +{
14349 +       int err;
14350 +       aufs_bindex_t btop;
14351 +       struct dentry *dentry;
14352 +       struct au_branch *br;
14353 +       struct file *h_file, *h_file_tmp;
14354 +
14355 +       dentry = file->f_path.dentry;
14356 +       btop = au_dbtop(dentry);
14357 +       br = au_sbr(dentry->d_sb, btop);
14358 +       h_file_tmp = NULL;
14359 +       if (au_fbtop(file) == btop) {
14360 +               h_file = au_hf_top(file);
14361 +               if (file->f_mode == h_file->f_mode)
14362 +                       return 0; /* success */
14363 +               h_file_tmp = h_file;
14364 +               get_file(h_file_tmp);
14365 +               au_lcnt_inc(&br->br_nfiles);
14366 +               au_set_h_fptr(file, btop, NULL);
14367 +       }
14368 +       AuDebugOn(au_fi(file)->fi_hdir);
14369 +       /*
14370 +        * it can happen
14371 +        * file exists on both of rw and ro
14372 +        * open --> dbtop and fbtop are both 0
14373 +        * prepend a branch as rw, "rw" become ro
14374 +        * remove rw/file
14375 +        * delete the top branch, "rw" becomes rw again
14376 +        *      --> dbtop is 1, fbtop is still 0
14377 +        * write --> fbtop is 0 but dbtop is 1
14378 +        */
14379 +       /* AuDebugOn(au_fbtop(file) < btop); */
14380 +
14381 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14382 +                          file, /*force_wr*/0);
14383 +       err = PTR_ERR(h_file);
14384 +       if (IS_ERR(h_file)) {
14385 +               if (h_file_tmp) {
14386 +                       /* revert */
14387 +                       au_set_h_fptr(file, btop, h_file_tmp);
14388 +                       h_file_tmp = NULL;
14389 +               }
14390 +               goto out; /* todo: close all? */
14391 +       }
14392 +
14393 +       err = 0;
14394 +       au_set_fbtop(file, btop);
14395 +       au_set_h_fptr(file, btop, h_file);
14396 +       au_update_figen(file);
14397 +       /* todo: necessary? */
14398 +       /* file->f_ra = h_file->f_ra; */
14399 +
14400 +out:
14401 +       if (h_file_tmp) {
14402 +               fput(h_file_tmp);
14403 +               au_lcnt_dec(&br->br_nfiles);
14404 +       }
14405 +       return err;
14406 +}
14407 +
14408 +/* ---------------------------------------------------------------------- */
14409 +
14410 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14411 +                       struct dentry *hi_wh)
14412 +{
14413 +       int err;
14414 +       aufs_bindex_t btop;
14415 +       struct au_dinfo *dinfo;
14416 +       struct dentry *h_dentry;
14417 +       struct au_hdentry *hdp;
14418 +
14419 +       dinfo = au_di(file->f_path.dentry);
14420 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14421 +
14422 +       btop = dinfo->di_btop;
14423 +       dinfo->di_btop = btgt;
14424 +       hdp = au_hdentry(dinfo, btgt);
14425 +       h_dentry = hdp->hd_dentry;
14426 +       hdp->hd_dentry = hi_wh;
14427 +       err = au_reopen_nondir(file);
14428 +       hdp->hd_dentry = h_dentry;
14429 +       dinfo->di_btop = btop;
14430 +
14431 +       return err;
14432 +}
14433 +
14434 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14435 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14436 +{
14437 +       int err;
14438 +       struct inode *inode, *h_inode;
14439 +       struct dentry *h_dentry, *hi_wh;
14440 +       struct au_cp_generic cpg = {
14441 +               .dentry = file->f_path.dentry,
14442 +               .bdst   = bcpup,
14443 +               .bsrc   = -1,
14444 +               .len    = len,
14445 +               .pin    = pin
14446 +       };
14447 +
14448 +       au_update_dbtop(cpg.dentry);
14449 +       inode = d_inode(cpg.dentry);
14450 +       h_inode = NULL;
14451 +       if (au_dbtop(cpg.dentry) <= bcpup
14452 +           && au_dbbot(cpg.dentry) >= bcpup) {
14453 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14454 +               if (h_dentry && d_is_positive(h_dentry))
14455 +                       h_inode = d_inode(h_dentry);
14456 +       }
14457 +       hi_wh = au_hi_wh(inode, bcpup);
14458 +       if (!hi_wh && !h_inode)
14459 +               err = au_sio_cpup_wh(&cpg, file);
14460 +       else
14461 +               /* already copied-up after unlink */
14462 +               err = au_reopen_wh(file, bcpup, hi_wh);
14463 +
14464 +       if (!err
14465 +           && (inode->i_nlink > 1
14466 +               || (inode->i_state & I_LINKABLE))
14467 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14468 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14469 +
14470 +       return err;
14471 +}
14472 +
14473 +/*
14474 + * prepare the @file for writing.
14475 + */
14476 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14477 +{
14478 +       int err;
14479 +       aufs_bindex_t dbtop;
14480 +       struct dentry *parent;
14481 +       struct inode *inode;
14482 +       struct super_block *sb;
14483 +       struct file *h_file;
14484 +       struct au_cp_generic cpg = {
14485 +               .dentry = file->f_path.dentry,
14486 +               .bdst   = -1,
14487 +               .bsrc   = -1,
14488 +               .len    = len,
14489 +               .pin    = pin,
14490 +               .flags  = AuCpup_DTIME
14491 +       };
14492 +
14493 +       sb = cpg.dentry->d_sb;
14494 +       inode = d_inode(cpg.dentry);
14495 +       cpg.bsrc = au_fbtop(file);
14496 +       err = au_test_ro(sb, cpg.bsrc, inode);
14497 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14498 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14499 +                            /*flags*/0);
14500 +               goto out;
14501 +       }
14502 +
14503 +       /* need to cpup or reopen */
14504 +       parent = dget_parent(cpg.dentry);
14505 +       di_write_lock_parent(parent);
14506 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14507 +       cpg.bdst = err;
14508 +       if (unlikely(err < 0))
14509 +               goto out_dgrade;
14510 +       err = 0;
14511 +
14512 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14513 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14514 +               if (unlikely(err))
14515 +                       goto out_dgrade;
14516 +       }
14517 +
14518 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14519 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14520 +       if (unlikely(err))
14521 +               goto out_dgrade;
14522 +
14523 +       dbtop = au_dbtop(cpg.dentry);
14524 +       if (dbtop <= cpg.bdst)
14525 +               cpg.bsrc = cpg.bdst;
14526 +
14527 +       if (dbtop <= cpg.bdst           /* just reopen */
14528 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14529 +               ) {
14530 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14531 +               if (IS_ERR(h_file))
14532 +                       err = PTR_ERR(h_file);
14533 +               else {
14534 +                       di_downgrade_lock(parent, AuLock_IR);
14535 +                       if (dbtop > cpg.bdst)
14536 +                               err = au_sio_cpup_simple(&cpg);
14537 +                       if (!err)
14538 +                               err = au_reopen_nondir(file);
14539 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14540 +               }
14541 +       } else {                        /* copyup as wh and reopen */
14542 +               /*
14543 +                * since writable hfsplus branch is not supported,
14544 +                * h_open_pre/post() are unnecessary.
14545 +                */
14546 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14547 +               di_downgrade_lock(parent, AuLock_IR);
14548 +       }
14549 +
14550 +       if (!err) {
14551 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14552 +               goto out_dput; /* success */
14553 +       }
14554 +       au_unpin(pin);
14555 +       goto out_unlock;
14556 +
14557 +out_dgrade:
14558 +       di_downgrade_lock(parent, AuLock_IR);
14559 +out_unlock:
14560 +       di_read_unlock(parent, AuLock_IR);
14561 +out_dput:
14562 +       dput(parent);
14563 +out:
14564 +       return err;
14565 +}
14566 +
14567 +/* ---------------------------------------------------------------------- */
14568 +
14569 +int au_do_flush(struct file *file, fl_owner_t id,
14570 +               int (*flush)(struct file *file, fl_owner_t id))
14571 +{
14572 +       int err;
14573 +       struct super_block *sb;
14574 +       struct inode *inode;
14575 +
14576 +       inode = file_inode(file);
14577 +       sb = inode->i_sb;
14578 +       si_noflush_read_lock(sb);
14579 +       fi_read_lock(file);
14580 +       ii_read_lock_child(inode);
14581 +
14582 +       err = flush(file, id);
14583 +       au_cpup_attr_timesizes(inode);
14584 +
14585 +       ii_read_unlock(inode);
14586 +       fi_read_unlock(file);
14587 +       si_read_unlock(sb);
14588 +       return err;
14589 +}
14590 +
14591 +/* ---------------------------------------------------------------------- */
14592 +
14593 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14594 +{
14595 +       int err;
14596 +       struct au_pin pin;
14597 +       struct au_finfo *finfo;
14598 +       struct dentry *parent, *hi_wh;
14599 +       struct inode *inode;
14600 +       struct super_block *sb;
14601 +       struct au_cp_generic cpg = {
14602 +               .dentry = file->f_path.dentry,
14603 +               .bdst   = -1,
14604 +               .bsrc   = -1,
14605 +               .len    = -1,
14606 +               .pin    = &pin,
14607 +               .flags  = AuCpup_DTIME
14608 +       };
14609 +
14610 +       FiMustWriteLock(file);
14611 +
14612 +       err = 0;
14613 +       finfo = au_fi(file);
14614 +       sb = cpg.dentry->d_sb;
14615 +       inode = d_inode(cpg.dentry);
14616 +       cpg.bdst = au_ibtop(inode);
14617 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14618 +               goto out;
14619 +
14620 +       parent = dget_parent(cpg.dentry);
14621 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14622 +               di_read_lock_parent(parent, !AuLock_IR);
14623 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14624 +               cpg.bdst = err;
14625 +               di_read_unlock(parent, !AuLock_IR);
14626 +               if (unlikely(err < 0))
14627 +                       goto out_parent;
14628 +               err = 0;
14629 +       }
14630 +
14631 +       di_read_lock_parent(parent, AuLock_IR);
14632 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14633 +       if (!S_ISDIR(inode->i_mode)
14634 +           && au_opt_test(au_mntflags(sb), PLINK)
14635 +           && au_plink_test(inode)
14636 +           && !d_unhashed(cpg.dentry)
14637 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14638 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14639 +               if (unlikely(err))
14640 +                       goto out_unlock;
14641 +
14642 +               /* always superio. */
14643 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14644 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14645 +               if (!err) {
14646 +                       err = au_sio_cpup_simple(&cpg);
14647 +                       au_unpin(&pin);
14648 +               }
14649 +       } else if (hi_wh) {
14650 +               /* already copied-up after unlink */
14651 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14652 +               *need_reopen = 0;
14653 +       }
14654 +
14655 +out_unlock:
14656 +       di_read_unlock(parent, AuLock_IR);
14657 +out_parent:
14658 +       dput(parent);
14659 +out:
14660 +       return err;
14661 +}
14662 +
14663 +static void au_do_refresh_dir(struct file *file)
14664 +{
14665 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14666 +       struct au_hfile *p, tmp, *q;
14667 +       struct au_finfo *finfo;
14668 +       struct super_block *sb;
14669 +       struct au_fidir *fidir;
14670 +
14671 +       FiMustWriteLock(file);
14672 +
14673 +       sb = file->f_path.dentry->d_sb;
14674 +       finfo = au_fi(file);
14675 +       fidir = finfo->fi_hdir;
14676 +       AuDebugOn(!fidir);
14677 +       p = fidir->fd_hfile + finfo->fi_btop;
14678 +       brid = p->hf_br->br_id;
14679 +       bbot = fidir->fd_bbot;
14680 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14681 +               if (!p->hf_file)
14682 +                       continue;
14683 +
14684 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14685 +               if (new_bindex == bindex)
14686 +                       continue;
14687 +               if (new_bindex < 0) {
14688 +                       au_set_h_fptr(file, bindex, NULL);
14689 +                       continue;
14690 +               }
14691 +
14692 +               /* swap two lower inode, and loop again */
14693 +               q = fidir->fd_hfile + new_bindex;
14694 +               tmp = *q;
14695 +               *q = *p;
14696 +               *p = tmp;
14697 +               if (tmp.hf_file) {
14698 +                       bindex--;
14699 +                       p--;
14700 +               }
14701 +       }
14702 +
14703 +       p = fidir->fd_hfile;
14704 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14705 +               bbot = au_sbbot(sb);
14706 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14707 +                    finfo->fi_btop++, p++)
14708 +                       if (p->hf_file) {
14709 +                               if (file_inode(p->hf_file))
14710 +                                       break;
14711 +                               au_hfput(p, /*execed*/0);
14712 +                       }
14713 +       } else {
14714 +               bbot = au_br_index(sb, brid);
14715 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14716 +                    finfo->fi_btop++, p++)
14717 +                       if (p->hf_file)
14718 +                               au_hfput(p, /*execed*/0);
14719 +               bbot = au_sbbot(sb);
14720 +       }
14721 +
14722 +       p = fidir->fd_hfile + bbot;
14723 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14724 +            fidir->fd_bbot--, p--)
14725 +               if (p->hf_file) {
14726 +                       if (file_inode(p->hf_file))
14727 +                               break;
14728 +                       au_hfput(p, /*execed*/0);
14729 +               }
14730 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14731 +}
14732 +
14733 +/*
14734 + * after branch manipulating, refresh the file.
14735 + */
14736 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14737 +{
14738 +       int err, need_reopen, nbr;
14739 +       aufs_bindex_t bbot, bindex;
14740 +       struct dentry *dentry;
14741 +       struct super_block *sb;
14742 +       struct au_finfo *finfo;
14743 +       struct au_hfile *hfile;
14744 +
14745 +       dentry = file->f_path.dentry;
14746 +       sb = dentry->d_sb;
14747 +       nbr = au_sbbot(sb) + 1;
14748 +       finfo = au_fi(file);
14749 +       if (!finfo->fi_hdir) {
14750 +               hfile = &finfo->fi_htop;
14751 +               AuDebugOn(!hfile->hf_file);
14752 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14753 +               AuDebugOn(bindex < 0);
14754 +               if (bindex != finfo->fi_btop)
14755 +                       au_set_fbtop(file, bindex);
14756 +       } else {
14757 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14758 +               if (unlikely(err))
14759 +                       goto out;
14760 +               au_do_refresh_dir(file);
14761 +       }
14762 +
14763 +       err = 0;
14764 +       need_reopen = 1;
14765 +       if (!au_test_mmapped(file))
14766 +               err = au_file_refresh_by_inode(file, &need_reopen);
14767 +       if (finfo->fi_hdir)
14768 +               /* harmless if err */
14769 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14770 +       if (!err && need_reopen && !d_unlinked(dentry))
14771 +               err = reopen(file);
14772 +       if (!err) {
14773 +               au_update_figen(file);
14774 +               goto out; /* success */
14775 +       }
14776 +
14777 +       /* error, close all lower files */
14778 +       if (finfo->fi_hdir) {
14779 +               bbot = au_fbbot_dir(file);
14780 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14781 +                       au_set_h_fptr(file, bindex, NULL);
14782 +       }
14783 +
14784 +out:
14785 +       return err;
14786 +}
14787 +
14788 +/* common function to regular file and dir */
14789 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14790 +                         int wlock, unsigned int fi_lsc)
14791 +{
14792 +       int err;
14793 +       unsigned int sigen, figen;
14794 +       aufs_bindex_t btop;
14795 +       unsigned char pseudo_link;
14796 +       struct dentry *dentry;
14797 +       struct inode *inode;
14798 +
14799 +       err = 0;
14800 +       dentry = file->f_path.dentry;
14801 +       inode = d_inode(dentry);
14802 +       sigen = au_sigen(dentry->d_sb);
14803 +       fi_write_lock_nested(file, fi_lsc);
14804 +       figen = au_figen(file);
14805 +       if (!fi_lsc)
14806 +               di_write_lock_child(dentry);
14807 +       else
14808 +               di_write_lock_child2(dentry);
14809 +       btop = au_dbtop(dentry);
14810 +       pseudo_link = (btop != au_ibtop(inode));
14811 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14812 +               if (!wlock) {
14813 +                       di_downgrade_lock(dentry, AuLock_IR);
14814 +                       fi_downgrade_lock(file);
14815 +               }
14816 +               goto out; /* success */
14817 +       }
14818 +
14819 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14820 +       if (au_digen_test(dentry, sigen)) {
14821 +               err = au_reval_dpath(dentry, sigen);
14822 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14823 +       }
14824 +
14825 +       if (!err)
14826 +               err = refresh_file(file, reopen);
14827 +       if (!err) {
14828 +               if (!wlock) {
14829 +                       di_downgrade_lock(dentry, AuLock_IR);
14830 +                       fi_downgrade_lock(file);
14831 +               }
14832 +       } else {
14833 +               di_write_unlock(dentry);
14834 +               fi_write_unlock(file);
14835 +       }
14836 +
14837 +out:
14838 +       return err;
14839 +}
14840 +
14841 +/* ---------------------------------------------------------------------- */
14842 +
14843 +/* cf. aufs_nopage() */
14844 +/* for madvise(2) */
14845 +static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
14846 +{
14847 +       folio_unlock(folio);
14848 +       return 0;
14849 +}
14850 +
14851 +/* it will never be called, but necessary to support O_DIRECT */
14852 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14853 +{ BUG(); return 0; }
14854 +
14855 +/* they will never be called. */
14856 +#ifdef CONFIG_AUFS_DEBUG
14857 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14858 +                           loff_t pos, unsigned len,
14859 +                           struct page **pagep, void **fsdata)
14860 +{ AuUnsupport(); return 0; }
14861 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14862 +                         loff_t pos, unsigned len, unsigned copied,
14863 +                         struct page *page, void *fsdata)
14864 +{ AuUnsupport(); return 0; }
14865 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14866 +{ AuUnsupport(); return 0; }
14867 +
14868 +static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
14869 +{ AuUnsupport(); return true; }
14870 +static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
14871 +{ AuUnsupport(); }
14872 +static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
14873 +{ AuUnsupport(); return true; }
14874 +#if 0 /* called by memory compaction regardless file */
14875 +static int aufs_migrate_folio(struct address_space *mapping, struct folio *dst,
14876 +                             struct folio *src, enum migrate_mode mode)
14877 +{ AuUnsupport(); return 0; }
14878 +#endif
14879 +static int aufs_launder_folio(struct folio *folio)
14880 +{ AuUnsupport(); return 0; }
14881 +static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
14882 +                                     size_t count)
14883 +{ AuUnsupport(); return true; }
14884 +static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
14885 +                                   bool *writeback)
14886 +{ AuUnsupport(); }
14887 +static int aufs_error_remove_page(struct address_space *mapping,
14888 +                                 struct page *page)
14889 +{ AuUnsupport(); return 0; }
14890 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14891 +                             sector_t *span)
14892 +{ AuUnsupport(); return 0; }
14893 +static void aufs_swap_deactivate(struct file *file)
14894 +{ AuUnsupport(); }
14895 +static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
14896 +{ AuUnsupport(); return 0; }
14897 +#endif /* CONFIG_AUFS_DEBUG */
14898 +
14899 +const struct address_space_operations aufs_aop = {
14900 +       .read_folio             = aufs_read_folio,
14901 +       .direct_IO              = aufs_direct_IO,
14902 +#ifdef CONFIG_AUFS_DEBUG
14903 +       .writepage              = aufs_writepage,
14904 +       /* no writepages, because of writepage */
14905 +       .dirty_folio            = aufs_dirty_folio,
14906 +       /* no readpages, because of readpage */
14907 +       .write_begin            = aufs_write_begin,
14908 +       .write_end              = aufs_write_end,
14909 +       /* no bmap, no block device */
14910 +       .invalidate_folio       = aufs_invalidate_folio,
14911 +       .release_folio          = aufs_release_folio,
14912 +       /* is fallback_migrate_page ok? */
14913 +       /* .migrate_folio       = aufs_migrate_folio, */
14914 +       .launder_folio          = aufs_launder_folio,
14915 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14916 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14917 +       .error_remove_page      = aufs_error_remove_page,
14918 +       .swap_activate          = aufs_swap_activate,
14919 +       .swap_deactivate        = aufs_swap_deactivate,
14920 +       .swap_rw                = aufs_swap_rw
14921 +#endif /* CONFIG_AUFS_DEBUG */
14922 +};
14923 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14924 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14925 +++ linux/fs/aufs/file.h        2022-11-05 23:02:18.965889284 +0100
14926 @@ -0,0 +1,342 @@
14927 +/* SPDX-License-Identifier: GPL-2.0 */
14928 +/*
14929 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14930 + *
14931 + * This program is free software; you can redistribute it and/or modify
14932 + * it under the terms of the GNU General Public License as published by
14933 + * the Free Software Foundation; either version 2 of the License, or
14934 + * (at your option) any later version.
14935 + *
14936 + * This program is distributed in the hope that it will be useful,
14937 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14938 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14939 + * GNU General Public License for more details.
14940 + *
14941 + * You should have received a copy of the GNU General Public License
14942 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14943 + */
14944 +
14945 +/*
14946 + * file operations
14947 + */
14948 +
14949 +#ifndef __AUFS_FILE_H__
14950 +#define __AUFS_FILE_H__
14951 +
14952 +#ifdef __KERNEL__
14953 +
14954 +#include <linux/file.h>
14955 +#include <linux/fs.h>
14956 +#include <linux/mm_types.h>
14957 +#include <linux/poll.h>
14958 +#include "rwsem.h"
14959 +
14960 +struct au_branch;
14961 +struct au_hfile {
14962 +       struct file             *hf_file;
14963 +       struct au_branch        *hf_br;
14964 +};
14965 +
14966 +struct au_vdir;
14967 +struct au_fidir {
14968 +       aufs_bindex_t           fd_bbot;
14969 +       aufs_bindex_t           fd_nent;
14970 +       struct au_vdir          *fd_vdir_cache;
14971 +       struct au_hfile         fd_hfile[];
14972 +};
14973 +
14974 +static inline int au_fidir_sz(int nent)
14975 +{
14976 +       AuDebugOn(nent < 0);
14977 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14978 +}
14979 +
14980 +struct au_finfo {
14981 +       atomic_t                fi_generation;
14982 +
14983 +       struct au_rwsem         fi_rwsem;
14984 +       aufs_bindex_t           fi_btop;
14985 +
14986 +       /* do not union them */
14987 +       struct {                                /* for non-dir */
14988 +               struct au_hfile                 fi_htop;
14989 +               atomic_t                        fi_mmapped;
14990 +       };
14991 +       struct au_fidir         *fi_hdir;       /* for dir only */
14992 +
14993 +       struct hlist_bl_node    fi_hlist;
14994 +       struct file             *fi_file;       /* very ugly */
14995 +       struct rcu_head         rcu;
14996 +} ____cacheline_aligned_in_smp;
14997 +
14998 +/* ---------------------------------------------------------------------- */
14999 +
15000 +/* file.c */
15001 +extern const struct address_space_operations aufs_aop;
15002 +unsigned int au_file_roflags(unsigned int flags);
15003 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15004 +                      struct file *file, int force_wr);
15005 +struct au_do_open_args {
15006 +       int             aopen;
15007 +       int             (*open)(struct file *file, int flags,
15008 +                               struct file *h_file);
15009 +       struct au_fidir *fidir;
15010 +       struct file     *h_file;
15011 +};
15012 +int au_do_open(struct file *file, struct au_do_open_args *args);
15013 +int au_reopen_nondir(struct file *file);
15014 +struct au_pin;
15015 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15016 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15017 +                         int wlock, unsigned int fi_lsc);
15018 +int au_do_flush(struct file *file, fl_owner_t id,
15019 +               int (*flush)(struct file *file, fl_owner_t id));
15020 +
15021 +/* poll.c */
15022 +#ifdef CONFIG_AUFS_POLL
15023 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15024 +#endif
15025 +
15026 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15027 +/* hfsplus.c */
15028 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15029 +                          int force_wr);
15030 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15031 +                   struct file *h_file);
15032 +#else
15033 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15034 +       aufs_bindex_t bindex, int force_wr)
15035 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15036 +          struct file *h_file);
15037 +#endif
15038 +
15039 +/* f_op.c */
15040 +extern const struct file_operations aufs_file_fop;
15041 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15042 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15043 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15044 +
15045 +/* finfo.c */
15046 +void au_hfput(struct au_hfile *hf, int execed);
15047 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15048 +                  struct file *h_file);
15049 +
15050 +void au_update_figen(struct file *file);
15051 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15052 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15053 +
15054 +void au_fi_init_once(void *_fi);
15055 +void au_finfo_fin(struct file *file);
15056 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15057 +
15058 +/* ioctl.c */
15059 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15060 +#ifdef CONFIG_COMPAT
15061 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15062 +                          unsigned long arg);
15063 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15064 +                             unsigned long arg);
15065 +#endif
15066 +
15067 +/* ---------------------------------------------------------------------- */
15068 +
15069 +static inline struct au_finfo *au_fi(struct file *file)
15070 +{
15071 +       return file->private_data;
15072 +}
15073 +
15074 +/* ---------------------------------------------------------------------- */
15075 +
15076 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15077 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15078 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15079 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15080 +/*
15081 +#define fi_read_trylock_nested(f) \
15082 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15083 +#define fi_write_trylock_nested(f) \
15084 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15085 +*/
15086 +
15087 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15088 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15089 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15090 +
15091 +/* lock subclass for finfo */
15092 +enum {
15093 +       AuLsc_FI_1,
15094 +       AuLsc_FI_2
15095 +};
15096 +
15097 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15098 +{
15099 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15100 +}
15101 +
15102 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15103 +{
15104 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15105 +}
15106 +
15107 +/*
15108 + * fi_read_lock_1, fi_write_lock_1,
15109 + * fi_read_lock_2, fi_write_lock_2
15110 + */
15111 +#define AuReadLockFunc(name) \
15112 +static inline void fi_read_lock_##name(struct file *f) \
15113 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15114 +
15115 +#define AuWriteLockFunc(name) \
15116 +static inline void fi_write_lock_##name(struct file *f) \
15117 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15118 +
15119 +#define AuRWLockFuncs(name) \
15120 +       AuReadLockFunc(name) \
15121 +       AuWriteLockFunc(name)
15122 +
15123 +AuRWLockFuncs(1);
15124 +AuRWLockFuncs(2);
15125 +
15126 +#undef AuReadLockFunc
15127 +#undef AuWriteLockFunc
15128 +#undef AuRWLockFuncs
15129 +
15130 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15131 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15132 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15133 +
15134 +/* ---------------------------------------------------------------------- */
15135 +
15136 +/* todo: hard/soft set? */
15137 +static inline aufs_bindex_t au_fbtop(struct file *file)
15138 +{
15139 +       FiMustAnyLock(file);
15140 +       return au_fi(file)->fi_btop;
15141 +}
15142 +
15143 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15144 +{
15145 +       FiMustAnyLock(file);
15146 +       AuDebugOn(!au_fi(file)->fi_hdir);
15147 +       return au_fi(file)->fi_hdir->fd_bbot;
15148 +}
15149 +
15150 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15151 +{
15152 +       FiMustAnyLock(file);
15153 +       AuDebugOn(!au_fi(file)->fi_hdir);
15154 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15155 +}
15156 +
15157 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15158 +{
15159 +       FiMustWriteLock(file);
15160 +       au_fi(file)->fi_btop = bindex;
15161 +}
15162 +
15163 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15164 +{
15165 +       FiMustWriteLock(file);
15166 +       AuDebugOn(!au_fi(file)->fi_hdir);
15167 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15168 +}
15169 +
15170 +static inline void au_set_fvdir_cache(struct file *file,
15171 +                                     struct au_vdir *vdir_cache)
15172 +{
15173 +       FiMustWriteLock(file);
15174 +       AuDebugOn(!au_fi(file)->fi_hdir);
15175 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15176 +}
15177 +
15178 +static inline struct file *au_hf_top(struct file *file)
15179 +{
15180 +       FiMustAnyLock(file);
15181 +       AuDebugOn(au_fi(file)->fi_hdir);
15182 +       return au_fi(file)->fi_htop.hf_file;
15183 +}
15184 +
15185 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15186 +{
15187 +       FiMustAnyLock(file);
15188 +       AuDebugOn(!au_fi(file)->fi_hdir);
15189 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15190 +}
15191 +
15192 +/* todo: memory barrier? */
15193 +static inline unsigned int au_figen(struct file *f)
15194 +{
15195 +       return atomic_read(&au_fi(f)->fi_generation);
15196 +}
15197 +
15198 +static inline void au_set_mmapped(struct file *f)
15199 +{
15200 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15201 +               return;
15202 +       pr_warn("fi_mmapped wrapped around\n");
15203 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15204 +               ;
15205 +}
15206 +
15207 +static inline void au_unset_mmapped(struct file *f)
15208 +{
15209 +       atomic_dec(&au_fi(f)->fi_mmapped);
15210 +}
15211 +
15212 +static inline int au_test_mmapped(struct file *f)
15213 +{
15214 +       return atomic_read(&au_fi(f)->fi_mmapped);
15215 +}
15216 +
15217 +/* customize vma->vm_file */
15218 +
15219 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15220 +                                      struct file *file)
15221 +{
15222 +       struct file *f;
15223 +
15224 +       f = vma->vm_file;
15225 +       get_file(file);
15226 +       vma->vm_file = file;
15227 +       fput(f);
15228 +}
15229 +
15230 +#ifdef CONFIG_MMU
15231 +#define AuDbgVmRegion(file, vma) do {} while (0)
15232 +
15233 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15234 +                                   struct file *file)
15235 +{
15236 +       au_do_vm_file_reset(vma, file);
15237 +}
15238 +#else
15239 +#define AuDbgVmRegion(file, vma) \
15240 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15241 +
15242 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15243 +                                   struct file *file)
15244 +{
15245 +       struct file *f;
15246 +
15247 +       au_do_vm_file_reset(vma, file);
15248 +       f = vma->vm_region->vm_file;
15249 +       get_file(file);
15250 +       vma->vm_region->vm_file = file;
15251 +       fput(f);
15252 +}
15253 +#endif /* CONFIG_MMU */
15254 +
15255 +/* handle vma->vm_prfile */
15256 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15257 +                                   struct file *file)
15258 +{
15259 +       get_file(file);
15260 +       vma->vm_prfile = file;
15261 +#ifndef CONFIG_MMU
15262 +       get_file(file);
15263 +       vma->vm_region->vm_prfile = file;
15264 +#endif
15265 +}
15266 +
15267 +#endif /* __KERNEL__ */
15268 +#endif /* __AUFS_FILE_H__ */
15269 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15270 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15271 +++ linux/fs/aufs/finfo.c       2022-11-05 23:02:18.965889284 +0100
15272 @@ -0,0 +1,149 @@
15273 +// SPDX-License-Identifier: GPL-2.0
15274 +/*
15275 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15276 + *
15277 + * This program is free software; you can redistribute it and/or modify
15278 + * it under the terms of the GNU General Public License as published by
15279 + * the Free Software Foundation; either version 2 of the License, or
15280 + * (at your option) any later version.
15281 + *
15282 + * This program is distributed in the hope that it will be useful,
15283 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15284 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15285 + * GNU General Public License for more details.
15286 + *
15287 + * You should have received a copy of the GNU General Public License
15288 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15289 + */
15290 +
15291 +/*
15292 + * file private data
15293 + */
15294 +
15295 +#include "aufs.h"
15296 +
15297 +void au_hfput(struct au_hfile *hf, int execed)
15298 +{
15299 +       if (execed)
15300 +               allow_write_access(hf->hf_file);
15301 +       fput(hf->hf_file);
15302 +       hf->hf_file = NULL;
15303 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15304 +       hf->hf_br = NULL;
15305 +}
15306 +
15307 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15308 +{
15309 +       struct au_finfo *finfo = au_fi(file);
15310 +       struct au_hfile *hf;
15311 +       struct au_fidir *fidir;
15312 +
15313 +       fidir = finfo->fi_hdir;
15314 +       if (!fidir) {
15315 +               AuDebugOn(finfo->fi_btop != bindex);
15316 +               hf = &finfo->fi_htop;
15317 +       } else
15318 +               hf = fidir->fd_hfile + bindex;
15319 +
15320 +       if (hf && hf->hf_file)
15321 +               au_hfput(hf, vfsub_file_execed(file));
15322 +       if (val) {
15323 +               FiMustWriteLock(file);
15324 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15325 +               hf->hf_file = val;
15326 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15327 +       }
15328 +}
15329 +
15330 +void au_update_figen(struct file *file)
15331 +{
15332 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15333 +       /* smp_mb(); */ /* atomic_set */
15334 +}
15335 +
15336 +/* ---------------------------------------------------------------------- */
15337 +
15338 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15339 +{
15340 +       struct au_fidir *fidir;
15341 +       int nbr;
15342 +
15343 +       nbr = au_sbbot(sb) + 1;
15344 +       if (nbr < 2)
15345 +               nbr = 2; /* initial allocate for 2 branches */
15346 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15347 +       if (fidir) {
15348 +               fidir->fd_bbot = -1;
15349 +               fidir->fd_nent = nbr;
15350 +       }
15351 +
15352 +       return fidir;
15353 +}
15354 +
15355 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15356 +{
15357 +       int err;
15358 +       struct au_fidir *fidir, *p;
15359 +
15360 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15361 +       fidir = finfo->fi_hdir;
15362 +       AuDebugOn(!fidir);
15363 +
15364 +       err = -ENOMEM;
15365 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15366 +                        GFP_NOFS, may_shrink);
15367 +       if (p) {
15368 +               p->fd_nent = nbr;
15369 +               finfo->fi_hdir = p;
15370 +               err = 0;
15371 +       }
15372 +
15373 +       return err;
15374 +}
15375 +
15376 +/* ---------------------------------------------------------------------- */
15377 +
15378 +void au_finfo_fin(struct file *file)
15379 +{
15380 +       struct au_finfo *finfo;
15381 +
15382 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15383 +
15384 +       finfo = au_fi(file);
15385 +       AuDebugOn(finfo->fi_hdir);
15386 +       AuRwDestroy(&finfo->fi_rwsem);
15387 +       au_cache_free_finfo(finfo);
15388 +}
15389 +
15390 +void au_fi_init_once(void *_finfo)
15391 +{
15392 +       struct au_finfo *finfo = _finfo;
15393 +
15394 +       au_rw_init(&finfo->fi_rwsem);
15395 +}
15396 +
15397 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15398 +{
15399 +       int err;
15400 +       struct au_finfo *finfo;
15401 +       struct dentry *dentry;
15402 +
15403 +       err = -ENOMEM;
15404 +       dentry = file->f_path.dentry;
15405 +       finfo = au_cache_alloc_finfo();
15406 +       if (unlikely(!finfo))
15407 +               goto out;
15408 +
15409 +       err = 0;
15410 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15411 +       au_rw_write_lock(&finfo->fi_rwsem);
15412 +       finfo->fi_btop = -1;
15413 +       finfo->fi_hdir = fidir;
15414 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15415 +       /* smp_mb(); */ /* atomic_set */
15416 +
15417 +       file->private_data = finfo;
15418 +
15419 +out:
15420 +       return err;
15421 +}
15422 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15423 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15424 +++ linux/fs/aufs/f_op.c        2023-02-20 21:05:51.959693785 +0100
15425 @@ -0,0 +1,780 @@
15426 +// SPDX-License-Identifier: GPL-2.0
15427 +/*
15428 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15429 + *
15430 + * This program is free software; you can redistribute it and/or modify
15431 + * it under the terms of the GNU General Public License as published by
15432 + * the Free Software Foundation; either version 2 of the License, or
15433 + * (at your option) any later version.
15434 + *
15435 + * This program is distributed in the hope that it will be useful,
15436 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15437 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15438 + * GNU General Public License for more details.
15439 + *
15440 + * You should have received a copy of the GNU General Public License
15441 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15442 + */
15443 +
15444 +/*
15445 + * file and vm operations
15446 + */
15447 +
15448 +#include <linux/aio.h>
15449 +#include <linux/fs_stack.h>
15450 +#include <linux/mman.h>
15451 +#include <linux/security.h>
15452 +#include "aufs.h"
15453 +
15454 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15455 +{
15456 +       int err;
15457 +       aufs_bindex_t bindex;
15458 +       struct dentry *dentry, *h_dentry;
15459 +       struct au_finfo *finfo;
15460 +       struct inode *h_inode;
15461 +
15462 +       FiMustWriteLock(file);
15463 +
15464 +       err = 0;
15465 +       dentry = file->f_path.dentry;
15466 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15467 +       finfo = au_fi(file);
15468 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15469 +       atomic_set(&finfo->fi_mmapped, 0);
15470 +       bindex = au_dbtop(dentry);
15471 +       if (!h_file) {
15472 +               h_dentry = au_h_dptr(dentry, bindex);
15473 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15474 +               if (unlikely(err))
15475 +                       goto out;
15476 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15477 +               if (IS_ERR(h_file)) {
15478 +                       err = PTR_ERR(h_file);
15479 +                       goto out;
15480 +               }
15481 +       } else {
15482 +               h_dentry = h_file->f_path.dentry;
15483 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15484 +               if (unlikely(err))
15485 +                       goto out;
15486 +               /* br ref is already inc-ed */
15487 +       }
15488 +
15489 +       if (flags & __O_TMPFILE) {
15490 +               AuDebugOn(!h_file);
15491 +               AuDebugOn(h_file != au_di(dentry)->di_htmpfile);
15492 +               au_di(dentry)->di_htmpfile = NULL;
15493 +
15494 +               if (!(flags & O_EXCL)) {
15495 +                       h_inode = file_inode(h_file);
15496 +                       spin_lock(&h_inode->i_lock);
15497 +                       h_inode->i_state |= I_LINKABLE;
15498 +                       spin_unlock(&h_inode->i_lock);
15499 +               }
15500 +       }
15501 +       au_set_fbtop(file, bindex);
15502 +       au_set_h_fptr(file, bindex, h_file);
15503 +       au_update_figen(file);
15504 +       /* todo: necessary? */
15505 +       /* file->f_ra = h_file->f_ra; */
15506 +
15507 +out:
15508 +       return err;
15509 +}
15510 +
15511 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15512 +                           struct file *file)
15513 +{
15514 +       int err;
15515 +       struct super_block *sb;
15516 +       struct au_do_open_args args = {
15517 +               .open   = au_do_open_nondir
15518 +       };
15519 +
15520 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15521 +             file, vfsub_file_flags(file), file->f_mode);
15522 +
15523 +       sb = file->f_path.dentry->d_sb;
15524 +       si_read_lock(sb, AuLock_FLUSH);
15525 +       err = au_do_open(file, &args);
15526 +       si_read_unlock(sb);
15527 +       return err;
15528 +}
15529 +
15530 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15531 +{
15532 +       struct au_finfo *finfo;
15533 +       aufs_bindex_t bindex;
15534 +
15535 +       finfo = au_fi(file);
15536 +       au_hbl_del(&finfo->fi_hlist,
15537 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15538 +       bindex = finfo->fi_btop;
15539 +       if (bindex >= 0)
15540 +               au_set_h_fptr(file, bindex, NULL);
15541 +
15542 +       au_finfo_fin(file);
15543 +       return 0;
15544 +}
15545 +
15546 +/* ---------------------------------------------------------------------- */
15547 +
15548 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15549 +{
15550 +       int err;
15551 +       struct file *h_file;
15552 +
15553 +       err = 0;
15554 +       h_file = au_hf_top(file);
15555 +       if (h_file)
15556 +               err = vfsub_flush(h_file, id);
15557 +       return err;
15558 +}
15559 +
15560 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15561 +{
15562 +       return au_do_flush(file, id, au_do_flush_nondir);
15563 +}
15564 +
15565 +/* ---------------------------------------------------------------------- */
15566 +/*
15567 + * read and write functions acquire [fdi]_rwsem once, but release before
15568 + * mmap_sem. This is because to stop a race condition between mmap(2).
15569 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15570 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15571 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15572 + */
15573 +
15574 +/* Callers should call au_read_post() or fput() in the end */
15575 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15576 +{
15577 +       struct file *h_file;
15578 +       int err;
15579 +
15580 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15581 +       if (!err) {
15582 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15583 +               h_file = au_hf_top(file);
15584 +               get_file(h_file);
15585 +               if (!keep_fi)
15586 +                       fi_read_unlock(file);
15587 +       } else
15588 +               h_file = ERR_PTR(err);
15589 +
15590 +       return h_file;
15591 +}
15592 +
15593 +static void au_read_post(struct inode *inode, struct file *h_file)
15594 +{
15595 +       /* update without lock, I don't think it a problem */
15596 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15597 +       fput(h_file);
15598 +}
15599 +
15600 +struct au_write_pre {
15601 +       /* input */
15602 +       unsigned int lsc;
15603 +
15604 +       /* output */
15605 +       blkcnt_t blks;
15606 +       aufs_bindex_t btop;
15607 +};
15608 +
15609 +/*
15610 + * return with iinfo is write-locked
15611 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15612 + * end
15613 + */
15614 +static struct file *au_write_pre(struct file *file, int do_ready,
15615 +                                struct au_write_pre *wpre)
15616 +{
15617 +       struct file *h_file;
15618 +       struct dentry *dentry;
15619 +       int err;
15620 +       unsigned int lsc;
15621 +       struct au_pin pin;
15622 +
15623 +       lsc = 0;
15624 +       if (wpre)
15625 +               lsc = wpre->lsc;
15626 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15627 +       h_file = ERR_PTR(err);
15628 +       if (unlikely(err))
15629 +               goto out;
15630 +
15631 +       dentry = file->f_path.dentry;
15632 +       if (do_ready) {
15633 +               err = au_ready_to_write(file, -1, &pin);
15634 +               if (unlikely(err)) {
15635 +                       h_file = ERR_PTR(err);
15636 +                       di_write_unlock(dentry);
15637 +                       goto out_fi;
15638 +               }
15639 +       }
15640 +
15641 +       di_downgrade_lock(dentry, /*flags*/0);
15642 +       if (wpre)
15643 +               wpre->btop = au_fbtop(file);
15644 +       h_file = au_hf_top(file);
15645 +       get_file(h_file);
15646 +       if (wpre)
15647 +               wpre->blks = file_inode(h_file)->i_blocks;
15648 +       if (do_ready)
15649 +               au_unpin(&pin);
15650 +       di_read_unlock(dentry, /*flags*/0);
15651 +
15652 +out_fi:
15653 +       fi_write_unlock(file);
15654 +out:
15655 +       return h_file;
15656 +}
15657 +
15658 +static void au_write_post(struct inode *inode, struct file *h_file,
15659 +                         struct au_write_pre *wpre, ssize_t written)
15660 +{
15661 +       struct inode *h_inode;
15662 +
15663 +       au_cpup_attr_timesizes(inode);
15664 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15665 +       h_inode = file_inode(h_file);
15666 +       inode->i_mode = h_inode->i_mode;
15667 +       ii_write_unlock(inode);
15668 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15669 +       if (written > 0)
15670 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15671 +                             /*force*/h_inode->i_blocks > wpre->blks);
15672 +       fput(h_file);
15673 +}
15674 +
15675 +/*
15676 + * todo: very ugly
15677 + * it locks both of i_mutex and si_rwsem for read in safe.
15678 + * if the plink maintenance mode continues forever (that is the problem),
15679 + * may loop forever.
15680 + */
15681 +static void au_mtx_and_read_lock(struct inode *inode)
15682 +{
15683 +       int err;
15684 +       struct super_block *sb = inode->i_sb;
15685 +
15686 +       while (1) {
15687 +               inode_lock(inode);
15688 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15689 +               if (!err)
15690 +                       break;
15691 +               inode_unlock(inode);
15692 +               si_read_lock(sb, AuLock_NOPLMW);
15693 +               si_read_unlock(sb);
15694 +       }
15695 +}
15696 +
15697 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15698 +                         struct iov_iter *iov_iter)
15699 +{
15700 +       ssize_t err;
15701 +       struct file *file;
15702 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15703 +
15704 +       err = security_file_permission(h_file, rw);
15705 +       if (unlikely(err))
15706 +               goto out;
15707 +
15708 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15709 +       iter = NULL;
15710 +       if (rw == MAY_READ)
15711 +               iter = h_file->f_op->read_iter;
15712 +       else if (rw == MAY_WRITE)
15713 +               iter = h_file->f_op->write_iter;
15714 +
15715 +       file = kio->ki_filp;
15716 +       kio->ki_filp = h_file;
15717 +       if (iter) {
15718 +               lockdep_off();
15719 +               err = iter(kio, iov_iter);
15720 +               lockdep_on();
15721 +       } else
15722 +               /* currently there is no such fs */
15723 +               WARN_ON_ONCE(1);
15724 +       kio->ki_filp = file;
15725 +
15726 +out:
15727 +       return err;
15728 +}
15729 +
15730 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15731 +{
15732 +       ssize_t err;
15733 +       struct file *file, *h_file;
15734 +       struct inode *inode;
15735 +       struct super_block *sb;
15736 +
15737 +       file = kio->ki_filp;
15738 +       inode = file_inode(file);
15739 +       sb = inode->i_sb;
15740 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15741 +
15742 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15743 +       err = PTR_ERR(h_file);
15744 +       if (IS_ERR(h_file))
15745 +               goto out;
15746 +
15747 +       if (au_test_loopback_kthread()) {
15748 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15749 +               if (file->f_mapping != h_file->f_mapping) {
15750 +                       file->f_mapping = h_file->f_mapping;
15751 +                       smp_mb(); /* unnecessary? */
15752 +               }
15753 +       }
15754 +       fi_read_unlock(file);
15755 +
15756 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15757 +       /* todo: necessary? */
15758 +       /* file->f_ra = h_file->f_ra; */
15759 +       au_read_post(inode, h_file);
15760 +
15761 +out:
15762 +       si_read_unlock(sb);
15763 +       return err;
15764 +}
15765 +
15766 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15767 +{
15768 +       ssize_t err;
15769 +       struct au_write_pre wpre;
15770 +       struct inode *inode;
15771 +       struct file *file, *h_file;
15772 +
15773 +       file = kio->ki_filp;
15774 +       inode = file_inode(file);
15775 +       au_mtx_and_read_lock(inode);
15776 +
15777 +       wpre.lsc = 0;
15778 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15779 +       err = PTR_ERR(h_file);
15780 +       if (IS_ERR(h_file))
15781 +               goto out;
15782 +
15783 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15784 +       au_write_post(inode, h_file, &wpre, err);
15785 +
15786 +out:
15787 +       si_read_unlock(inode->i_sb);
15788 +       inode_unlock(inode);
15789 +       return err;
15790 +}
15791 +
15792 +/*
15793 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15794 + * don't have their own .splice_{read,write} implimentations, and they use
15795 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15796 + * simple converters to f_op->iter_read() and ->iter_write().
15797 + * But we keep our own implementations because some non-mainlined FSes may have
15798 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15799 + * away an opportunity to co-work with aufs from them.
15800 + */
15801 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15802 +                               struct pipe_inode_info *pipe, size_t len,
15803 +                               unsigned int flags)
15804 +{
15805 +       ssize_t err;
15806 +       struct file *h_file;
15807 +       struct inode *inode;
15808 +       struct super_block *sb;
15809 +
15810 +       inode = file_inode(file);
15811 +       sb = inode->i_sb;
15812 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15813 +
15814 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15815 +       err = PTR_ERR(h_file);
15816 +       if (IS_ERR(h_file))
15817 +               goto out;
15818 +
15819 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15820 +       /* todo: necessary? */
15821 +       /* file->f_ra = h_file->f_ra; */
15822 +       au_read_post(inode, h_file);
15823 +
15824 +out:
15825 +       si_read_unlock(sb);
15826 +       return err;
15827 +}
15828 +
15829 +static ssize_t
15830 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15831 +                 size_t len, unsigned int flags)
15832 +{
15833 +       ssize_t err;
15834 +       struct au_write_pre wpre;
15835 +       struct inode *inode;
15836 +       struct file *h_file;
15837 +
15838 +       inode = file_inode(file);
15839 +       au_mtx_and_read_lock(inode);
15840 +
15841 +       wpre.lsc = 0;
15842 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15843 +       err = PTR_ERR(h_file);
15844 +       if (IS_ERR(h_file))
15845 +               goto out;
15846 +
15847 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15848 +       au_write_post(inode, h_file, &wpre, err);
15849 +
15850 +out:
15851 +       si_read_unlock(inode->i_sb);
15852 +       inode_unlock(inode);
15853 +       return err;
15854 +}
15855 +
15856 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15857 +                          loff_t len)
15858 +{
15859 +       long err;
15860 +       struct au_write_pre wpre;
15861 +       struct inode *inode;
15862 +       struct file *h_file;
15863 +
15864 +       inode = file_inode(file);
15865 +       au_mtx_and_read_lock(inode);
15866 +
15867 +       wpre.lsc = 0;
15868 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15869 +       err = PTR_ERR(h_file);
15870 +       if (IS_ERR(h_file))
15871 +               goto out;
15872 +
15873 +       lockdep_off();
15874 +       err = vfs_fallocate(h_file, mode, offset, len);
15875 +       lockdep_on();
15876 +       /*
15877 +        * we don't need to call file_modifed() here since au_write_post()
15878 +        * is equivalent and copies-up all timestamps and permission bits.
15879 +        */
15880 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15881 +
15882 +out:
15883 +       si_read_unlock(inode->i_sb);
15884 +       inode_unlock(inode);
15885 +       return err;
15886 +}
15887 +
15888 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15889 +                                   struct file *dst, loff_t dst_pos,
15890 +                                   size_t len, unsigned int flags)
15891 +{
15892 +       ssize_t err;
15893 +       struct au_write_pre wpre;
15894 +       enum { SRC, DST };
15895 +       struct {
15896 +               struct inode *inode;
15897 +               struct file *h_file;
15898 +               struct super_block *h_sb;
15899 +       } a[2];
15900 +#define a_src  a[SRC]
15901 +#define a_dst  a[DST]
15902 +
15903 +       err = -EINVAL;
15904 +       a_src.inode = file_inode(src);
15905 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15906 +               goto out;
15907 +       a_dst.inode = file_inode(dst);
15908 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15909 +               goto out;
15910 +
15911 +       au_mtx_and_read_lock(a_dst.inode);
15912 +       /*
15913 +        * in order to match the order in di_write_lock2_{child,parent}(),
15914 +        * use f_path.dentry for this comparison.
15915 +        */
15916 +       if (src->f_path.dentry < dst->f_path.dentry) {
15917 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15918 +               err = PTR_ERR(a_src.h_file);
15919 +               if (IS_ERR(a_src.h_file))
15920 +                       goto out_si;
15921 +
15922 +               wpre.lsc = AuLsc_FI_2;
15923 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15924 +               err = PTR_ERR(a_dst.h_file);
15925 +               if (IS_ERR(a_dst.h_file)) {
15926 +                       au_read_post(a_src.inode, a_src.h_file);
15927 +                       goto out_si;
15928 +               }
15929 +       } else {
15930 +               wpre.lsc = AuLsc_FI_1;
15931 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15932 +               err = PTR_ERR(a_dst.h_file);
15933 +               if (IS_ERR(a_dst.h_file))
15934 +                       goto out_si;
15935 +
15936 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15937 +               err = PTR_ERR(a_src.h_file);
15938 +               if (IS_ERR(a_src.h_file)) {
15939 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15940 +                                     /*written*/0);
15941 +                       goto out_si;
15942 +               }
15943 +       }
15944 +
15945 +       err = -EXDEV;
15946 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15947 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15948 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15949 +               AuDbgFile(src);
15950 +               AuDbgFile(dst);
15951 +               goto out_file;
15952 +       }
15953 +
15954 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15955 +                                   dst_pos, len, flags);
15956 +
15957 +out_file:
15958 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15959 +       fi_read_unlock(src);
15960 +       au_read_post(a_src.inode, a_src.h_file);
15961 +out_si:
15962 +       si_read_unlock(a_dst.inode->i_sb);
15963 +       inode_unlock(a_dst.inode);
15964 +out:
15965 +       return err;
15966 +#undef a_src
15967 +#undef a_dst
15968 +}
15969 +
15970 +/* ---------------------------------------------------------------------- */
15971 +
15972 +/*
15973 + * The locking order around current->mmap_sem.
15974 + * - in most and regular cases
15975 + *   file I/O syscall -- aufs_read() or something
15976 + *     -- si_rwsem for read -- mmap_sem
15977 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15978 + * - in mmap case
15979 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15980 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15981 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15982 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15983 + * It means that when aufs acquires si_rwsem for write, the process should never
15984 + * acquire mmap_sem.
15985 + *
15986 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15987 + * problem either since any directory is not able to be mmap-ed.
15988 + * The similar scenario is applied to aufs_readlink() too.
15989 + */
15990 +
15991 +#if 0 /* stop calling security_file_mmap() */
15992 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15993 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15994 +
15995 +static unsigned long au_arch_prot_conv(unsigned long flags)
15996 +{
15997 +       /* currently ppc64 only */
15998 +#ifdef CONFIG_PPC64
15999 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
16000 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16001 +       return AuConv_VM_PROT(flags, SAO);
16002 +#else
16003 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
16004 +       return 0;
16005 +#endif
16006 +}
16007 +
16008 +static unsigned long au_prot_conv(unsigned long flags)
16009 +{
16010 +       return AuConv_VM_PROT(flags, READ)
16011 +               | AuConv_VM_PROT(flags, WRITE)
16012 +               | AuConv_VM_PROT(flags, EXEC)
16013 +               | au_arch_prot_conv(flags);
16014 +}
16015 +
16016 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16017 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16018 +
16019 +static unsigned long au_flag_conv(unsigned long flags)
16020 +{
16021 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16022 +               | AuConv_VM_MAP(flags, DENYWRITE)
16023 +               | AuConv_VM_MAP(flags, LOCKED);
16024 +}
16025 +#endif
16026 +
16027 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16028 +{
16029 +       int err;
16030 +       const unsigned char wlock
16031 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16032 +       struct super_block *sb;
16033 +       struct file *h_file;
16034 +       struct inode *inode;
16035 +
16036 +       AuDbgVmRegion(file, vma);
16037 +
16038 +       inode = file_inode(file);
16039 +       sb = inode->i_sb;
16040 +       lockdep_off();
16041 +       si_read_lock(sb, AuLock_NOPLMW);
16042 +
16043 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16044 +       lockdep_on();
16045 +       err = PTR_ERR(h_file);
16046 +       if (IS_ERR(h_file))
16047 +               goto out;
16048 +
16049 +       err = 0;
16050 +       au_set_mmapped(file);
16051 +       au_vm_file_reset(vma, h_file);
16052 +       /*
16053 +        * we cannot call security_mmap_file() here since it may acquire
16054 +        * mmap_sem or i_mutex.
16055 +        *
16056 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16057 +        *                       au_flag_conv(vma->vm_flags));
16058 +        */
16059 +       if (!err)
16060 +               err = call_mmap(h_file, vma);
16061 +       if (!err) {
16062 +               au_vm_prfile_set(vma, file);
16063 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16064 +               goto out_fput; /* success */
16065 +       }
16066 +       au_unset_mmapped(file);
16067 +       au_vm_file_reset(vma, file);
16068 +
16069 +out_fput:
16070 +       lockdep_off();
16071 +       ii_write_unlock(inode);
16072 +       lockdep_on();
16073 +       fput(h_file);
16074 +out:
16075 +       lockdep_off();
16076 +       si_read_unlock(sb);
16077 +       lockdep_on();
16078 +       AuTraceErr(err);
16079 +       return err;
16080 +}
16081 +
16082 +/* ---------------------------------------------------------------------- */
16083 +
16084 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16085 +                            int datasync)
16086 +{
16087 +       int err;
16088 +       struct au_write_pre wpre;
16089 +       struct inode *inode;
16090 +       struct file *h_file;
16091 +
16092 +       err = 0; /* -EBADF; */ /* posix? */
16093 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16094 +               goto out;
16095 +
16096 +       inode = file_inode(file);
16097 +       au_mtx_and_read_lock(inode);
16098 +
16099 +       wpre.lsc = 0;
16100 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16101 +       err = PTR_ERR(h_file);
16102 +       if (IS_ERR(h_file))
16103 +               goto out_unlock;
16104 +
16105 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16106 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16107 +
16108 +out_unlock:
16109 +       si_read_unlock(inode->i_sb);
16110 +       inode_unlock(inode);
16111 +out:
16112 +       return err;
16113 +}
16114 +
16115 +static int aufs_fasync(int fd, struct file *file, int flag)
16116 +{
16117 +       int err;
16118 +       struct file *h_file;
16119 +       struct super_block *sb;
16120 +
16121 +       sb = file->f_path.dentry->d_sb;
16122 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16123 +
16124 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16125 +       err = PTR_ERR(h_file);
16126 +       if (IS_ERR(h_file))
16127 +               goto out;
16128 +
16129 +       if (h_file->f_op->fasync)
16130 +               err = h_file->f_op->fasync(fd, h_file, flag);
16131 +       fput(h_file); /* instead of au_read_post() */
16132 +
16133 +out:
16134 +       si_read_unlock(sb);
16135 +       return err;
16136 +}
16137 +
16138 +static int aufs_setfl(struct file *file, unsigned long arg)
16139 +{
16140 +       int err;
16141 +       struct file *h_file;
16142 +       struct super_block *sb;
16143 +
16144 +       sb = file->f_path.dentry->d_sb;
16145 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16146 +
16147 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16148 +       err = PTR_ERR(h_file);
16149 +       if (IS_ERR(h_file))
16150 +               goto out;
16151 +
16152 +       /* stop calling h_file->fasync */
16153 +       arg |= vfsub_file_flags(file) & FASYNC;
16154 +       err = setfl(/*unused fd*/-1, h_file, arg);
16155 +       fput(h_file); /* instead of au_read_post() */
16156 +
16157 +out:
16158 +       si_read_unlock(sb);
16159 +       return err;
16160 +}
16161 +
16162 +/* ---------------------------------------------------------------------- */
16163 +
16164 +/* no one supports this operation, currently */
16165 +#if 0 /* reserved for future use */
16166 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16167 +                            size_t len, loff_t *pos, int more)
16168 +{
16169 +}
16170 +#endif
16171 +
16172 +/* ---------------------------------------------------------------------- */
16173 +
16174 +const struct file_operations aufs_file_fop = {
16175 +       .owner          = THIS_MODULE,
16176 +
16177 +       .llseek         = default_llseek,
16178 +
16179 +       .read_iter      = aufs_read_iter,
16180 +       .write_iter     = aufs_write_iter,
16181 +
16182 +#ifdef CONFIG_AUFS_POLL
16183 +       .poll           = aufs_poll,
16184 +#endif
16185 +       .unlocked_ioctl = aufs_ioctl_nondir,
16186 +#ifdef CONFIG_COMPAT
16187 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16188 +#endif
16189 +       .mmap           = aufs_mmap,
16190 +       .open           = aufs_open_nondir,
16191 +       .flush          = aufs_flush_nondir,
16192 +       .release        = aufs_release_nondir,
16193 +       .fsync          = aufs_fsync_nondir,
16194 +       .fasync         = aufs_fasync,
16195 +       /* .sendpage    = aufs_sendpage, */
16196 +       .setfl          = aufs_setfl,
16197 +       .splice_write   = aufs_splice_write,
16198 +       .splice_read    = aufs_splice_read,
16199 +#if 0 /* reserved for future use */
16200 +       .aio_splice_write = aufs_aio_splice_write,
16201 +       .aio_splice_read  = aufs_aio_splice_read,
16202 +#endif
16203 +       .fallocate      = aufs_fallocate,
16204 +       .copy_file_range = aufs_copy_file_range
16205 +};
16206 diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16207 --- /usr/share/empty/fs/aufs/fsctx.c    1970-01-01 01:00:00.000000000 +0100
16208 +++ linux/fs/aufs/fsctx.c       2022-11-05 23:02:18.965889284 +0100
16209 @@ -0,0 +1,1242 @@
16210 +// SPDX-License-Identifier: GPL-2.0
16211 +/*
16212 + * Copyright (C) 2022 Junjiro R. Okajima
16213 + *
16214 + * This program is free software; you can redistribute it and/or modify
16215 + * it under the terms of the GNU General Public License as published by
16216 + * the Free Software Foundation; either version 2 of the License, or
16217 + * (at your option) any later version.
16218 + *
16219 + * This program is distributed in the hope that it will be useful,
16220 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16221 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16222 + * GNU General Public License for more details.
16223 + *
16224 + * You should have received a copy of the GNU General Public License
16225 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16226 + */
16227 +
16228 +/*
16229 + * fs context, aka new mount api
16230 + */
16231 +
16232 +#include <linux/fs_context.h>
16233 +#include "aufs.h"
16234 +
16235 +struct au_fsctx_opts {
16236 +       aufs_bindex_t bindex;
16237 +       unsigned char skipped;
16238 +       struct au_opt *opt, *opt_tail;
16239 +       struct super_block *sb;
16240 +       struct au_sbinfo *sbinfo;
16241 +       struct au_opts opts;
16242 +};
16243 +
16244 +/* stop extra interpretation of errno in mount(8), and strange error messages */
16245 +static int cvt_err(int err)
16246 +{
16247 +       AuTraceErr(err);
16248 +
16249 +       switch (err) {
16250 +       case -ENOENT:
16251 +       case -ENOTDIR:
16252 +       case -EEXIST:
16253 +       case -EIO:
16254 +               err = -EINVAL;
16255 +       }
16256 +       return err;
16257 +}
16258 +
16259 +static int au_fsctx_reconfigure(struct fs_context *fc)
16260 +{
16261 +       int err, do_dx;
16262 +       unsigned int mntflags;
16263 +       struct dentry *root;
16264 +       struct super_block *sb;
16265 +       struct inode *inode;
16266 +       struct au_fsctx_opts *a = fc->fs_private;
16267 +
16268 +       AuDbg("fc %p\n", fc);
16269 +
16270 +       root = fc->root;
16271 +       sb = root->d_sb;
16272 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16273 +       if (!err) {
16274 +               di_write_lock_child(root);
16275 +               err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16276 +               aufs_write_unlock(root);
16277 +       }
16278 +
16279 +       inode = d_inode(root);
16280 +       inode_lock(inode);
16281 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16282 +       if (unlikely(err))
16283 +               goto out;
16284 +       di_write_lock_child(root);
16285 +
16286 +       /* au_opts_remount() may return an error */
16287 +       err = au_opts_remount(sb, &a->opts);
16288 +
16289 +       if (au_ftest_opts(a->opts.flags, REFRESH))
16290 +               au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16291 +                                                    REFRESH_IDOP));
16292 +
16293 +       if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16294 +               mntflags = au_mntflags(sb);
16295 +               do_dx = !!au_opt_test(mntflags, DIO);
16296 +               au_dy_arefresh(do_dx);
16297 +       }
16298 +
16299 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16300 +       aufs_write_unlock(root);
16301 +
16302 +out:
16303 +       inode_unlock(inode);
16304 +       err = cvt_err(err);
16305 +       AuTraceErr(err);
16306 +
16307 +       return err;
16308 +}
16309 +
16310 +/* ---------------------------------------------------------------------- */
16311 +
16312 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16313 +{
16314 +       int err;
16315 +       struct au_fsctx_opts *a = fc->fs_private;
16316 +       struct au_sbinfo *sbinfo = a->sbinfo;
16317 +       struct dentry *root;
16318 +       struct inode *inode;
16319 +
16320 +       sbinfo->si_sb = sb;
16321 +       sb->s_fs_info = sbinfo;
16322 +       kobject_get(&sbinfo->si_kobj);
16323 +
16324 +       __si_write_lock(sb);
16325 +       si_pid_set(sb);
16326 +       au_sbilist_add(sb);
16327 +
16328 +       /* all timestamps always follow the ones on the branch */
16329 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16330 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16331 +       sb->s_op = &aufs_sop;
16332 +       sb->s_d_op = &aufs_dop;
16333 +       sb->s_magic = AUFS_SUPER_MAGIC;
16334 +       sb->s_maxbytes = 0;
16335 +       sb->s_stack_depth = 1;
16336 +       au_export_init(sb);
16337 +       au_xattr_init(sb);
16338 +
16339 +       err = au_alloc_root(sb);
16340 +       if (unlikely(err)) {
16341 +               si_write_unlock(sb);
16342 +               goto out;
16343 +       }
16344 +       root = sb->s_root;
16345 +       inode = d_inode(root);
16346 +       ii_write_lock_parent(inode);
16347 +       aufs_write_unlock(root);
16348 +
16349 +       /* lock vfs_inode first, then aufs. */
16350 +       inode_lock(inode);
16351 +       aufs_write_lock(root);
16352 +       err = au_opts_mount(sb, &a->opts);
16353 +       AuTraceErr(err);
16354 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16355 +               sb->s_d_op = &aufs_dop_noreval;
16356 +               /* infofc(fc, "%ps", sb->s_d_op); */
16357 +               pr_info("%ps\n", sb->s_d_op);
16358 +               au_refresh_dop(root, /*force_reval*/0);
16359 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
16360 +               au_refresh_iop(inode, /*force_getattr*/0);
16361 +       }
16362 +       aufs_write_unlock(root);
16363 +       inode_unlock(inode);
16364 +       if (!err)
16365 +               goto out; /* success */
16366 +
16367 +       dput(root);
16368 +       sb->s_root = NULL;
16369 +
16370 +out:
16371 +       if (unlikely(err))
16372 +               kobject_put(&sbinfo->si_kobj);
16373 +       AuTraceErr(err);
16374 +       err = cvt_err(err);
16375 +       AuTraceErr(err);
16376 +       return err;
16377 +}
16378 +
16379 +static int au_fsctx_get_tree(struct fs_context *fc)
16380 +{
16381 +       int err;
16382 +
16383 +       AuDbg("fc %p\n", fc);
16384 +       err = get_tree_nodev(fc, au_fsctx_fill_super);
16385 +
16386 +       AuTraceErr(err);
16387 +       return err;
16388 +}
16389 +
16390 +/* ---------------------------------------------------------------------- */
16391 +
16392 +static void au_fsctx_dump(struct au_opts *opts)
16393 +{
16394 +#ifdef CONFIG_AUFS_DEBUG
16395 +       /* reduce stack space */
16396 +       union {
16397 +               struct au_opt_add *add;
16398 +               struct au_opt_del *del;
16399 +               struct au_opt_mod *mod;
16400 +               struct au_opt_xino *xino;
16401 +               struct au_opt_xino_itrunc *xino_itrunc;
16402 +               struct au_opt_wbr_create *create;
16403 +       } u;
16404 +       struct au_opt *opt;
16405 +
16406 +       opt = opts->opt;
16407 +       while (opt->type != Opt_tail) {
16408 +               switch (opt->type) {
16409 +               case Opt_add:
16410 +                       u.add = &opt->add;
16411 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16412 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16413 +                                 u.add->path.dentry);
16414 +                       break;
16415 +               case Opt_del:
16416 +                       fallthrough;
16417 +               case Opt_idel:
16418 +                       u.del = &opt->del;
16419 +                       AuDbg("del {%s, %p}\n",
16420 +                             u.del->pathname, u.del->h_path.dentry);
16421 +                       break;
16422 +               case Opt_mod:
16423 +                       fallthrough;
16424 +               case Opt_imod:
16425 +                       u.mod = &opt->mod;
16426 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16427 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16428 +                       break;
16429 +               case Opt_append:
16430 +                       u.add = &opt->add;
16431 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16432 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16433 +                                 u.add->path.dentry);
16434 +                       break;
16435 +               case Opt_prepend:
16436 +                       u.add = &opt->add;
16437 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16438 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16439 +                                 u.add->path.dentry);
16440 +                       break;
16441 +
16442 +               case Opt_dirwh:
16443 +                       AuDbg("dirwh %d\n", opt->dirwh);
16444 +                       break;
16445 +               case Opt_rdcache:
16446 +                       AuDbg("rdcache %d\n", opt->rdcache);
16447 +                       break;
16448 +               case Opt_rdblk:
16449 +                       AuDbg("rdblk %d\n", opt->rdblk);
16450 +                       break;
16451 +               case Opt_rdhash:
16452 +                       AuDbg("rdhash %u\n", opt->rdhash);
16453 +                       break;
16454 +
16455 +               case Opt_xino:
16456 +                       u.xino = &opt->xino;
16457 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16458 +                       break;
16459 +
16460 +#define au_fsctx_TF(name)                                        \
16461 +                       case Opt_##name:                          \
16462 +                               if (opt->tf)                      \
16463 +                                       AuLabel(name);            \
16464 +                               else                              \
16465 +                                       AuLabel(no##name);        \
16466 +                               break;
16467 +
16468 +               /* simple true/false flag */
16469 +               au_fsctx_TF(trunc_xino);
16470 +               au_fsctx_TF(trunc_xib);
16471 +               au_fsctx_TF(dirperm1);
16472 +               au_fsctx_TF(plink);
16473 +               au_fsctx_TF(shwh);
16474 +               au_fsctx_TF(dio);
16475 +               au_fsctx_TF(warn_perm);
16476 +               au_fsctx_TF(verbose);
16477 +               au_fsctx_TF(sum);
16478 +               au_fsctx_TF(dirren);
16479 +               au_fsctx_TF(acl);
16480 +#undef au_fsctx_TF
16481 +
16482 +               case Opt_trunc_xino_path:
16483 +                       fallthrough;
16484 +               case Opt_itrunc_xino:
16485 +                       u.xino_itrunc = &opt->xino_itrunc;
16486 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16487 +                       break;
16488 +               case Opt_noxino:
16489 +                       AuLabel(noxino);
16490 +                       break;
16491 +
16492 +               case Opt_list_plink:
16493 +                       AuLabel(list_plink);
16494 +                       break;
16495 +               case Opt_udba:
16496 +                       AuDbg("udba %d, %s\n",
16497 +                                 opt->udba, au_optstr_udba(opt->udba));
16498 +                       break;
16499 +               case Opt_diropq_a:
16500 +                       AuLabel(diropq_a);
16501 +                       break;
16502 +               case Opt_diropq_w:
16503 +                       AuLabel(diropq_w);
16504 +                       break;
16505 +               case Opt_wsum:
16506 +                       AuLabel(wsum);
16507 +                       break;
16508 +               case Opt_wbr_create:
16509 +                       u.create = &opt->wbr_create;
16510 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
16511 +                                 au_optstr_wbr_create(u.create->wbr_create));
16512 +                       switch (u.create->wbr_create) {
16513 +                       case AuWbrCreate_MFSV:
16514 +                               fallthrough;
16515 +                       case AuWbrCreate_PMFSV:
16516 +                               AuDbg("%d sec\n", u.create->mfs_second);
16517 +                               break;
16518 +                       case AuWbrCreate_MFSRR:
16519 +                               fallthrough;
16520 +                       case AuWbrCreate_TDMFS:
16521 +                               AuDbg("%llu watermark\n",
16522 +                                         u.create->mfsrr_watermark);
16523 +                               break;
16524 +                       case AuWbrCreate_MFSRRV:
16525 +                               fallthrough;
16526 +                       case AuWbrCreate_TDMFSV:
16527 +                               fallthrough;
16528 +                       case AuWbrCreate_PMFSRRV:
16529 +                               AuDbg("%llu watermark, %d sec\n",
16530 +                                         u.create->mfsrr_watermark,
16531 +                                         u.create->mfs_second);
16532 +                               break;
16533 +                       }
16534 +                       break;
16535 +               case Opt_wbr_copyup:
16536 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16537 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
16538 +                       break;
16539 +               case Opt_fhsm_sec:
16540 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16541 +                       break;
16542 +
16543 +               default:
16544 +                       AuDbg("type %d\n", opt->type);
16545 +                       BUG();
16546 +               }
16547 +               opt++;
16548 +       }
16549 +#endif
16550 +}
16551 +
16552 +/* ---------------------------------------------------------------------- */
16553 +
16554 +/*
16555 + * For conditionally compiled mount options.
16556 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16557 + */
16558 +#define au_ignore_flag(name, action)           \
16559 +       fsparam_flag(name, action),             \
16560 +       fsparam_flag("no" name, Opt_ignore_silent)
16561 +
16562 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16563 +       fsparam_string("br", Opt_br),
16564 +
16565 +       /* "add=%d:%s" or "ins=%d:%s" */
16566 +       fsparam_string("add", Opt_add),
16567 +       fsparam_string("ins", Opt_add),
16568 +       fsparam_path("append", Opt_append),
16569 +       fsparam_path("prepend", Opt_prepend),
16570 +
16571 +       fsparam_path("del", Opt_del),
16572 +       /* fsparam_s32("idel", Opt_idel), */
16573 +       fsparam_path("mod", Opt_mod),
16574 +       /* fsparam_string("imod", Opt_imod), */
16575 +
16576 +       fsparam_s32("dirwh", Opt_dirwh),
16577 +
16578 +       fsparam_path("xino", Opt_xino),
16579 +       fsparam_flag("noxino", Opt_noxino),
16580 +       fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16581 +       /* "trunc_xino_v=%d:%d" */
16582 +       /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16583 +       fsparam_path("trunc_xino", Opt_trunc_xino_path),
16584 +       fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16585 +       /* fsparam_path("zxino", Opt_zxino), */
16586 +       fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16587 +
16588 +#ifdef CONFIG_PROC_FS
16589 +       fsparam_flag_no("plink", Opt_plink),
16590 +#else
16591 +       au_ignore_flag("plink", Opt_ignore),
16592 +#endif
16593 +
16594 +#ifdef CONFIG_AUFS_DEBUG
16595 +       fsparam_flag("list_plink", Opt_list_plink),
16596 +#endif
16597 +
16598 +       fsparam_string("udba", Opt_udba),
16599 +
16600 +       fsparam_flag_no("dio", Opt_dio),
16601 +
16602 +#ifdef CONFIG_AUFS_DIRREN
16603 +       fsparam_flag_no("dirren", Opt_dirren),
16604 +#else
16605 +       au_ignore_flag("dirren", Opt_ignore),
16606 +#endif
16607 +
16608 +#ifdef CONFIG_AUFS_FHSM
16609 +       fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16610 +#else
16611 +       fsparam_s32("fhsm_sec", Opt_ignore),
16612 +#endif
16613 +
16614 +       /* always | a | whiteouted | w */
16615 +       fsparam_string("diropq", Opt_diropq),
16616 +
16617 +       fsparam_flag_no("warn_perm", Opt_warn_perm),
16618 +
16619 +#ifdef CONFIG_AUFS_SHWH
16620 +       fsparam_flag_no("shwh", Opt_shwh),
16621 +#else
16622 +       au_ignore_flag("shwh", Opt_err),
16623 +#endif
16624 +
16625 +       fsparam_flag_no("dirperm1", Opt_dirperm1),
16626 +
16627 +       fsparam_flag_no("verbose", Opt_verbose),
16628 +       fsparam_flag("v", Opt_verbose),
16629 +       fsparam_flag("quiet", Opt_noverbose),
16630 +       fsparam_flag("q", Opt_noverbose),
16631 +       /* user-space may handle this */
16632 +       fsparam_flag("silent", Opt_noverbose),
16633 +
16634 +       fsparam_flag_no("sum", Opt_sum),
16635 +       fsparam_flag("wsum", Opt_wsum),
16636 +
16637 +       fsparam_s32("rdcache", Opt_rdcache),
16638 +       /* "def" or s32 */
16639 +       fsparam_string("rdblk", Opt_rdblk),
16640 +       /* "def" or s32 */
16641 +       fsparam_string("rdhash", Opt_rdhash),
16642 +
16643 +       fsparam_string("create", Opt_wbr_create),
16644 +       fsparam_string("create_policy", Opt_wbr_create),
16645 +       fsparam_string("cpup", Opt_wbr_copyup),
16646 +       fsparam_string("copyup", Opt_wbr_copyup),
16647 +       fsparam_string("copyup_policy", Opt_wbr_copyup),
16648 +
16649 +       /* generic VFS flag */
16650 +#ifdef CONFIG_FS_POSIX_ACL
16651 +       fsparam_flag_no("acl", Opt_acl),
16652 +#else
16653 +       au_ignore_flag("acl", Opt_ignore),
16654 +#endif
16655 +
16656 +       /* internal use for the scripts */
16657 +       fsparam_string("si", Opt_ignore_silent),
16658 +
16659 +       /* obsoleted, keep them temporary */
16660 +       fsparam_flag("nodlgt", Opt_ignore_silent),
16661 +       fsparam_flag("clean_plink", Opt_ignore),
16662 +       fsparam_string("dirs", Opt_br),
16663 +       fsparam_u32("debug", Opt_ignore),
16664 +       /* "whiteout" or "all" */
16665 +       fsparam_string("delete", Opt_ignore),
16666 +       fsparam_string("imap", Opt_ignore),
16667 +
16668 +       /* temporary workaround, due to old mount(8)? */
16669 +       fsparam_flag("relatime", Opt_ignore_silent),
16670 +
16671 +       {}
16672 +};
16673 +
16674 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16675 +                                char *brspec, size_t speclen,
16676 +                                aufs_bindex_t bindex)
16677 +{
16678 +       int err;
16679 +       char *p;
16680 +
16681 +       AuDbg("brspec %s\n", brspec);
16682 +
16683 +       err = -ENOMEM;
16684 +       if (!speclen)
16685 +               speclen = strlen(brspec);
16686 +       /* will be freed by au_fsctx_free() */
16687 +       p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16688 +       if (unlikely(!p)) {
16689 +               errorfc(fc, "failed in %s", brspec);
16690 +               goto out;
16691 +       }
16692 +       err = au_opt_add(opt, p, fc->sb_flags, bindex);
16693 +
16694 +out:
16695 +       AuTraceErr(err);
16696 +       return err;
16697 +}
16698 +
16699 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16700 +{
16701 +       int err;
16702 +       char *p;
16703 +       struct au_fsctx_opts *a = fc->fs_private;
16704 +       struct au_opt *opt = a->opt;
16705 +       aufs_bindex_t bindex = a->bindex;
16706 +
16707 +       AuDbg("brspec %s\n", brspec);
16708 +
16709 +       err = -EINVAL;
16710 +       while ((p = strsep(&brspec, ":")) && *p) {
16711 +               err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16712 +               AuTraceErr(err);
16713 +               if (unlikely(err))
16714 +                       break;
16715 +               bindex++;
16716 +               opt++;
16717 +               if (unlikely(opt > a->opt_tail)) {
16718 +                       err = -E2BIG;
16719 +                       bindex--;
16720 +                       opt--;
16721 +                       break;
16722 +               }
16723 +               opt->type = Opt_tail;
16724 +               a->skipped = 1;
16725 +       }
16726 +       a->bindex = bindex;
16727 +       a->opt = opt;
16728 +
16729 +       AuTraceErr(err);
16730 +       return err;
16731 +}
16732 +
16733 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16734 +{
16735 +       int err, n;
16736 +       char *p;
16737 +       struct au_fsctx_opts *a = fc->fs_private;
16738 +       struct au_opt *opt = a->opt;
16739 +
16740 +       err = -EINVAL;
16741 +       p = strchr(addspec, ':');
16742 +       if (unlikely(!p)) {
16743 +               errorfc(fc, "bad arg in %s", addspec);
16744 +               goto out;
16745 +       }
16746 +       *p++ = '\0';
16747 +       err = kstrtoint(addspec, 0, &n);
16748 +       if (unlikely(err)) {
16749 +               errorfc(fc, "bad integer in %s", addspec);
16750 +               goto out;
16751 +       }
16752 +       AuDbg("n %d\n", n);
16753 +       err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16754 +
16755 +out:
16756 +       AuTraceErr(err);
16757 +       return err;
16758 +}
16759 +
16760 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16761 +                             struct fs_parameter *param)
16762 +{
16763 +       int err;
16764 +
16765 +       err = -ENOMEM;
16766 +       /* will be freed by au_fsctx_free() */
16767 +       del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16768 +       if (unlikely(!del->pathname))
16769 +               goto out;
16770 +       AuDbg("del %s\n", del->pathname);
16771 +       err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16772 +       if (unlikely(err))
16773 +               errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16774 +
16775 +out:
16776 +       AuTraceErr(err);
16777 +       return err;
16778 +}
16779 +
16780 +#if 0 /* reserved for future use */
16781 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16782 +                              aufs_bindex_t bindex)
16783 +{
16784 +       int err;
16785 +       struct super_block *sb;
16786 +       struct dentry *root;
16787 +       struct au_fsctx_opts *a = fc->fs_private;
16788 +
16789 +       sb = a->sb;
16790 +       AuDebugOn(!sb);
16791 +
16792 +       err = -EINVAL;
16793 +       root = sb->s_root;
16794 +       aufs_read_lock(root, AuLock_FLUSH);
16795 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
16796 +               errorfc(fc, "out of bounds, %d", bindex);
16797 +               goto out;
16798 +       }
16799 +
16800 +       err = 0;
16801 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
16802 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16803 +
16804 +out:
16805 +       aufs_read_unlock(root, !AuLock_IR);
16806 +       AuTraceErr(err);
16807 +       return err;
16808 +}
16809 +#endif
16810 +
16811 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16812 +                             struct fs_parameter *param)
16813 +{
16814 +       int err;
16815 +       struct path path;
16816 +       char *p;
16817 +
16818 +       err = -ENOMEM;
16819 +       /* will be freed by au_fsctx_free() */
16820 +       mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16821 +       if (unlikely(!mod->path))
16822 +               goto out;
16823 +
16824 +       err = -EINVAL;
16825 +       p = strchr(mod->path, '=');
16826 +       if (unlikely(!p)) {
16827 +               errorfc(fc, "no permission %s", mod->path);
16828 +               goto out;
16829 +       }
16830 +
16831 +       *p++ = 0;
16832 +       err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16833 +       if (unlikely(err)) {
16834 +               errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16835 +               goto out;
16836 +       }
16837 +
16838 +       mod->perm = au_br_perm_val(p);
16839 +       AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16840 +       mod->h_root = dget(path.dentry);
16841 +       path_put(&path);
16842 +
16843 +out:
16844 +       AuTraceErr(err);
16845 +       return err;
16846 +}
16847 +
16848 +#if 0 /* reserved for future use */
16849 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16850 +                              char *ibrspec)
16851 +{
16852 +       int err, n;
16853 +       char *p;
16854 +       struct super_block *sb;
16855 +       struct dentry *root;
16856 +       struct au_fsctx_opts *a = fc->fs_private;
16857 +
16858 +       sb = a->sb;
16859 +       AuDebugOn(!sb);
16860 +
16861 +       err = -EINVAL;
16862 +       p = strchr(ibrspec, ':');
16863 +       if (unlikely(!p)) {
16864 +               errorfc(fc, "no index, %s", ibrspec);
16865 +               goto out;
16866 +       }
16867 +       *p++ = '\0';
16868 +       err = kstrtoint(ibrspec, 0, &n);
16869 +       if (unlikely(err)) {
16870 +               errorfc(fc, "bad integer in %s", ibrspec);
16871 +               goto out;
16872 +       }
16873 +       AuDbg("n %d\n", n);
16874 +
16875 +       root = sb->s_root;
16876 +       aufs_read_lock(root, AuLock_FLUSH);
16877 +       if (n < 0 || au_sbbot(sb) < n) {
16878 +               errorfc(fc, "out of bounds, %d", bindex);
16879 +               goto out_root;
16880 +       }
16881 +
16882 +       err = 0;
16883 +       mod->perm = au_br_perm_val(p);
16884 +       AuDbg("mod path %s, perm 0x%x, %s\n",
16885 +             mod->path, mod->perm, p);
16886 +       mod->h_root = dget(au_h_dptr(root, bindex));
16887 +
16888 +out_root:
16889 +       aufs_read_unlock(root, !AuLock_IR);
16890 +out:
16891 +       AuTraceErr(err);
16892 +       return err;
16893 +}
16894 +#endif
16895 +
16896 +static int au_fsctx_parse_xino(struct fs_context *fc,
16897 +                              struct au_opt_xino *xino,
16898 +                              struct fs_parameter *param)
16899 +{
16900 +       int err;
16901 +       struct au_fsctx_opts *a = fc->fs_private;
16902 +
16903 +       err = -ENOMEM;
16904 +       /* will be freed by au_opts_free() */
16905 +       xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16906 +       if (unlikely(!xino->path))
16907 +               goto out;
16908 +       AuDbg("path %s\n", xino->path);
16909 +
16910 +       xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16911 +                                   /*wbrtop*/0);
16912 +       err = PTR_ERR(xino->file);
16913 +       if (IS_ERR(xino->file)) {
16914 +               xino->file = NULL;
16915 +               goto out;
16916 +       }
16917 +
16918 +       err = 0;
16919 +       if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16920 +               err = -EINVAL;
16921 +               errorfc(fc, "%s must be outside", xino->path);
16922 +       }
16923 +
16924 +out:
16925 +       AuTraceErr(err);
16926 +       return err;
16927 +}
16928 +
16929 +static
16930 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16931 +                                   struct au_opt_xino_itrunc *xino_itrunc,
16932 +                                   char *pathname)
16933 +{
16934 +       int err;
16935 +       aufs_bindex_t bbot, bindex;
16936 +       struct path path;
16937 +       struct dentry *root;
16938 +       struct au_fsctx_opts *a = fc->fs_private;
16939 +
16940 +       AuDebugOn(!a->sb);
16941 +
16942 +       err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16943 +       if (unlikely(err)) {
16944 +               errorfc(fc, "lookup failed %s (%d)", pathname, err);
16945 +               goto out;
16946 +       }
16947 +
16948 +       xino_itrunc->bindex = -1;
16949 +       root = a->sb->s_root;
16950 +       aufs_read_lock(root, AuLock_FLUSH);
16951 +       bbot = au_sbbot(a->sb);
16952 +       for (bindex = 0; bindex <= bbot; bindex++) {
16953 +               if (au_h_dptr(root, bindex) == path.dentry) {
16954 +                       xino_itrunc->bindex = bindex;
16955 +                       break;
16956 +               }
16957 +       }
16958 +       aufs_read_unlock(root, !AuLock_IR);
16959 +       path_put(&path);
16960 +
16961 +       if (unlikely(xino_itrunc->bindex < 0)) {
16962 +               err = -EINVAL;
16963 +               errorfc(fc, "no such branch %s", pathname);
16964 +       }
16965 +
16966 +out:
16967 +       AuTraceErr(err);
16968 +       return err;
16969 +}
16970 +
16971 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16972 +                                     struct au_opt_xino_itrunc *xino_itrunc,
16973 +                                     unsigned int bindex)
16974 +{
16975 +       int err;
16976 +       aufs_bindex_t bbot;
16977 +       struct super_block *sb;
16978 +       struct au_fsctx_opts *a = fc->fs_private;
16979 +
16980 +       sb = a->sb;
16981 +       AuDebugOn(!sb);
16982 +
16983 +       err = 0;
16984 +       si_noflush_read_lock(sb);
16985 +       bbot = au_sbbot(sb);
16986 +       si_read_unlock(sb);
16987 +       if (bindex <= bbot)
16988 +               xino_itrunc->bindex = bindex;
16989 +       else {
16990 +               err = -EINVAL;
16991 +               errorfc(fc, "out of bounds, %u", bindex);
16992 +       }
16993 +
16994 +       AuTraceErr(err);
16995 +       return err;
16996 +}
16997 +
16998 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16999 +{
17000 +       int err, token;
17001 +       struct fs_parse_result result;
17002 +       struct au_fsctx_opts *a = fc->fs_private;
17003 +       struct au_opt *opt = a->opt;
17004 +
17005 +       AuDbg("fc %p, param {key %s, string %s}\n",
17006 +             fc, param->key, param->string);
17007 +       err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
17008 +       if (unlikely(err < 0))
17009 +               goto out;
17010 +       token = err;
17011 +       AuDbg("token %d, res{negated %d, uint64 %llu}\n",
17012 +             token, result.negated, result.uint_64);
17013 +
17014 +       err = -EINVAL;
17015 +       a->skipped = 0;
17016 +       switch (token) {
17017 +       case Opt_br:
17018 +               err = au_fsctx_parse_br(fc, param->string);
17019 +               break;
17020 +       case Opt_add:
17021 +               err = au_fsctx_parse_add(fc, param->string);
17022 +               break;
17023 +       case Opt_append:
17024 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17025 +                                           /*dummy bindex*/1);
17026 +               break;
17027 +       case Opt_prepend:
17028 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17029 +                                           /*bindex*/0);
17030 +               break;
17031 +
17032 +       case Opt_del:
17033 +               err = au_fsctx_parse_del(fc, &opt->del, param);
17034 +               break;
17035 +#if 0 /* reserved for future use */
17036 +       case Opt_idel:
17037 +               if (!a->sb) {
17038 +                       err = 0;
17039 +                       a->skipped = 1;
17040 +                       break;
17041 +               }
17042 +               del->pathname = "(indexed)";
17043 +               err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17044 +               break;
17045 +#endif
17046 +
17047 +       case Opt_mod:
17048 +               err = au_fsctx_parse_mod(fc, &opt->mod, param);
17049 +               break;
17050 +#ifdef IMOD /* reserved for future use */
17051 +       case Opt_imod:
17052 +               if (!a->sb) {
17053 +                       err = 0;
17054 +                       a->skipped = 1;
17055 +                       break;
17056 +               }
17057 +               u.mod->path = "(indexed)";
17058 +               err = au_opts_parse_imod(fc, &opt->mod, param->string);
17059 +               break;
17060 +#endif
17061 +
17062 +       case Opt_xino:
17063 +               err = au_fsctx_parse_xino(fc, &opt->xino, param);
17064 +               break;
17065 +       case Opt_trunc_xino_path:
17066 +               if (!a->sb) {
17067 +                       errorfc(fc, "no such branch %s", param->string);
17068 +                       break;
17069 +               }
17070 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17071 +                                                     param->string);
17072 +               break;
17073 +#if 0
17074 +       case Opt_trunc_xino_v:
17075 +               if (!a->sb) {
17076 +                       err = 0;
17077 +                       a->skipped = 1;
17078 +                       break;
17079 +               }
17080 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17081 +                                                     param->string);
17082 +               break;
17083 +#endif
17084 +       case Opt_itrunc_xino:
17085 +               if (!a->sb) {
17086 +                       errorfc(fc, "out of bounds %s", param->string);
17087 +                       break;
17088 +               }
17089 +               err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17090 +                                                result.int_32);
17091 +               break;
17092 +
17093 +       case Opt_dirwh:
17094 +               err = 0;
17095 +               opt->dirwh = result.int_32;
17096 +               break;
17097 +
17098 +       case Opt_rdcache:
17099 +               if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17100 +                       errorfc(fc, "rdcache must be smaller than %d",
17101 +                               AUFS_RDCACHE_MAX);
17102 +                       break;
17103 +               }
17104 +               err = 0;
17105 +               opt->rdcache = result.int_32;
17106 +               break;
17107 +
17108 +       case Opt_rdblk:
17109 +               err = 0;
17110 +               opt->rdblk = AUFS_RDBLK_DEF;
17111 +               if (!strcmp(param->string, "def"))
17112 +                       break;
17113 +
17114 +               err = kstrtoint(param->string, 0, &result.int_32);
17115 +               if (unlikely(err)) {
17116 +                       errorfc(fc, "bad value in %s", param->key);
17117 +                       break;
17118 +               }
17119 +               err = -EINVAL;
17120 +               if (unlikely(result.int_32 < 0
17121 +                            || result.int_32 > KMALLOC_MAX_SIZE)) {
17122 +                       errorfc(fc, "bad value in %s", param->key);
17123 +                       break;
17124 +               }
17125 +               if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17126 +                       errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17127 +                       break;
17128 +               }
17129 +               err = 0;
17130 +               opt->rdblk = result.int_32;
17131 +               break;
17132 +
17133 +       case Opt_rdhash:
17134 +               err = 0;
17135 +               opt->rdhash = AUFS_RDHASH_DEF;
17136 +               if (!strcmp(param->string, "def"))
17137 +                       break;
17138 +
17139 +               err = kstrtoint(param->string, 0, &result.int_32);
17140 +               if (unlikely(err)) {
17141 +                       errorfc(fc, "bad value in %s", param->key);
17142 +                       break;
17143 +               }
17144 +               /* how about zero? */
17145 +               if (result.int_32 < 0
17146 +                   || result.int_32 * sizeof(struct hlist_head)
17147 +                   > KMALLOC_MAX_SIZE) {
17148 +                       err = -EINVAL;
17149 +                       errorfc(fc, "bad integer in %s", param->key);
17150 +                       break;
17151 +               }
17152 +               opt->rdhash = result.int_32;
17153 +               break;
17154 +
17155 +       case Opt_diropq:
17156 +               /*
17157 +                * As other options, fs/aufs/opts.c can handle these strings by
17158 +                * match_token().  But "diropq=" is deprecated now and will
17159 +                * never have other value.  So simple strcmp() is enough here.
17160 +                */
17161 +               if (!strcmp(param->string, "a") ||
17162 +                   !strcmp(param->string, "always")) {
17163 +                       err = 0;
17164 +                       opt->type = Opt_diropq_a;
17165 +               } else if (!strcmp(param->string, "w") ||
17166 +                          !strcmp(param->string, "whiteouted")) {
17167 +                       err = 0;
17168 +                       opt->type = Opt_diropq_w;
17169 +               } else
17170 +                       errorfc(fc, "unknown value %s", param->string);
17171 +               break;
17172 +
17173 +       case Opt_udba:
17174 +               opt->udba = au_udba_val(param->string);
17175 +               if (opt->udba >= 0)
17176 +                       err = 0;
17177 +               else
17178 +                       errorf(fc, "wrong value, %s", param->string);
17179 +               break;
17180 +
17181 +       case Opt_wbr_create:
17182 +               opt->wbr_create.wbr_create
17183 +                       = au_wbr_create_val(param->string, &opt->wbr_create);
17184 +               if (opt->wbr_create.wbr_create >= 0)
17185 +                       err = 0;
17186 +               else
17187 +                       errorf(fc, "wrong value, %s", param->key);
17188 +               break;
17189 +
17190 +       case Opt_wbr_copyup:
17191 +               opt->wbr_copyup = au_wbr_copyup_val(param->string);
17192 +               if (opt->wbr_copyup >= 0)
17193 +                       err = 0;
17194 +               else
17195 +                       errorfc(fc, "wrong value, %s", param->key);
17196 +               break;
17197 +
17198 +       case Opt_fhsm_sec:
17199 +               if (unlikely(result.int_32 < 0)) {
17200 +                       errorfc(fc, "bad integer in %s\n", param->key);
17201 +                       break;
17202 +               }
17203 +               err = 0;
17204 +               if (sysaufs_brs)
17205 +                       opt->fhsm_second = result.int_32;
17206 +               else
17207 +                       warnfc(fc, "ignored %s %s", param->key, param->string);
17208 +               break;
17209 +
17210 +       /* simple true/false flag */
17211 +#define au_fsctx_TF(name)                              \
17212 +               case Opt_##name:                        \
17213 +                       err = 0;                        \
17214 +                       opt->tf = !result.negated;      \
17215 +                       break
17216 +       au_fsctx_TF(trunc_xino);
17217 +       au_fsctx_TF(trunc_xib);
17218 +       au_fsctx_TF(dirperm1);
17219 +       au_fsctx_TF(plink);
17220 +       au_fsctx_TF(shwh);
17221 +       au_fsctx_TF(dio);
17222 +       au_fsctx_TF(warn_perm);
17223 +       au_fsctx_TF(verbose);
17224 +       au_fsctx_TF(sum);
17225 +       au_fsctx_TF(dirren);
17226 +       au_fsctx_TF(acl);
17227 +#undef au_fsctx_TF
17228 +
17229 +       case Opt_noverbose:
17230 +               err = 0;
17231 +               opt->type = Opt_verbose;
17232 +               opt->tf = false;
17233 +               break;
17234 +
17235 +       case Opt_noxino:
17236 +               fallthrough;
17237 +       case Opt_list_plink:
17238 +               fallthrough;
17239 +       case Opt_wsum:
17240 +               err = 0;
17241 +               break;
17242 +
17243 +       case Opt_ignore:
17244 +               warnfc(fc, "ignored %s", param->key);
17245 +               fallthrough;
17246 +       case Opt_ignore_silent:
17247 +               a->skipped = 1;
17248 +               err = 0;
17249 +               break;
17250 +       default:
17251 +               a->skipped = 1;
17252 +               err = -ENOPARAM;
17253 +               break;
17254 +       }
17255 +       if (unlikely(err))
17256 +               goto out;
17257 +       if (a->skipped)
17258 +               goto out;
17259 +
17260 +       switch (token) {
17261 +       case Opt_br:
17262 +               fallthrough;
17263 +       case Opt_noverbose:
17264 +               fallthrough;
17265 +       case Opt_diropq:
17266 +               break;
17267 +       default:
17268 +               opt->type = token;
17269 +               break;
17270 +       }
17271 +       opt++;
17272 +       if (unlikely(opt > a->opt_tail)) {
17273 +               err = -E2BIG;
17274 +               opt--;
17275 +       }
17276 +       opt->type = Opt_tail;
17277 +       a->opt = opt;
17278 +
17279 +out:
17280 +       return err;
17281 +}
17282 +
17283 +/*
17284 + * these options accept both 'name=val' and 'name:val' form.
17285 + * some accept optional '=' in its value.
17286 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17287 + */
17288 +static inline unsigned int is_colonopt(char *str)
17289 +{
17290 +#define do_test(name)                                  \
17291 +       if (!strncmp(str, name ":", sizeof(name)))      \
17292 +               return sizeof(name) - 1
17293 +       do_test("br");
17294 +       do_test("add");
17295 +       do_test("ins");
17296 +       do_test("append");
17297 +       do_test("prepend");
17298 +       do_test("del");
17299 +       /* do_test("idel"); */
17300 +       do_test("mod");
17301 +       /* do_test("imod"); */
17302 +#undef do_test
17303 +
17304 +       return 0;
17305 +}
17306 +
17307 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17308 +{
17309 +       int err;
17310 +       unsigned int u;
17311 +       char *str;
17312 +       struct au_fsctx_opts *a = fc->fs_private;
17313 +
17314 +       str = data;
17315 +       AuDbg("str %s\n", str);
17316 +       while (str) {
17317 +               u = is_colonopt(str);
17318 +               if (u)
17319 +                       str[u] = '=';
17320 +               str = strchr(str, ',');
17321 +               if (!str)
17322 +                       break;
17323 +               str++;
17324 +       }
17325 +       str = data;
17326 +       AuDbg("str %s\n", str);
17327 +
17328 +       err = generic_parse_monolithic(fc, str);
17329 +       AuTraceErr(err);
17330 +       au_fsctx_dump(&a->opts);
17331 +
17332 +       return err;
17333 +}
17334 +
17335 +/* ---------------------------------------------------------------------- */
17336 +
17337 +static void au_fsctx_opts_free(struct au_opts *opts)
17338 +{
17339 +       struct au_opt *opt;
17340 +
17341 +       opt = opts->opt;
17342 +       while (opt->type != Opt_tail) {
17343 +               switch (opt->type) {
17344 +               case Opt_add:
17345 +                       fallthrough;
17346 +               case Opt_append:
17347 +                       fallthrough;
17348 +               case Opt_prepend:
17349 +                       kfree(opt->add.pathname);
17350 +                       path_put(&opt->add.path);
17351 +                       break;
17352 +               case Opt_del:
17353 +                       kfree(opt->del.pathname);
17354 +                       fallthrough;
17355 +               case Opt_idel:
17356 +                       path_put(&opt->del.h_path);
17357 +                       break;
17358 +               case Opt_mod:
17359 +                       kfree(opt->mod.path);
17360 +                       fallthrough;
17361 +               case Opt_imod:
17362 +                       dput(opt->mod.h_root);
17363 +                       break;
17364 +               case Opt_xino:
17365 +                       kfree(opt->xino.path);
17366 +                       fput(opt->xino.file);
17367 +                       break;
17368 +               }
17369 +               opt++;
17370 +       }
17371 +}
17372 +
17373 +static void au_fsctx_free(struct fs_context *fc)
17374 +{
17375 +       struct au_fsctx_opts *a = fc->fs_private;
17376 +
17377 +       /* fs_type=%p, root=%pD */
17378 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17379 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17380 +
17381 +       kobject_put(&a->sbinfo->si_kobj);
17382 +       au_fsctx_opts_free(&a->opts);
17383 +       free_page((unsigned long)a->opts.opt);
17384 +       au_kfree_rcu(a);
17385 +}
17386 +
17387 +static const struct fs_context_operations au_fsctx_ops = {
17388 +       .free                   = au_fsctx_free,
17389 +       .parse_param            = au_fsctx_parse_param,
17390 +       .parse_monolithic       = au_fsctx_parse_monolithic,
17391 +       .get_tree               = au_fsctx_get_tree,
17392 +       .reconfigure            = au_fsctx_reconfigure
17393 +       /*
17394 +        * nfs4 requires ->dup()? No.
17395 +        * I don't know what is this ->dup() for.
17396 +        */
17397 +};
17398 +
17399 +int aufs_fsctx_init(struct fs_context *fc)
17400 +{
17401 +       int err;
17402 +       struct au_fsctx_opts *a;
17403 +
17404 +       /* fs_type=%p, root=%pD */
17405 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17406 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17407 +
17408 +       /* they will be freed by au_fsctx_free() */
17409 +       err = -ENOMEM;
17410 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17411 +       if (unlikely(!a))
17412 +               goto out;
17413 +       a->bindex = 0;
17414 +       a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17415 +       if (unlikely(!a->opts.opt))
17416 +               goto out_a;
17417 +       a->opt = a->opts.opt;
17418 +       a->opt->type = Opt_tail;
17419 +       a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17420 +       a->opt_tail = a->opt + a->opts.max_opt - 1;
17421 +       a->opts.sb_flags = fc->sb_flags;
17422 +
17423 +       a->sb = NULL;
17424 +       if (fc->root) {
17425 +               AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17426 +               a->opts.flags = AuOpts_REMOUNT;
17427 +               a->sb = fc->root->d_sb;
17428 +               a->sbinfo = au_sbi(a->sb);
17429 +               kobject_get(&a->sbinfo->si_kobj);
17430 +       } else {
17431 +               a->sbinfo = au_si_alloc(a->sb);
17432 +               AuDebugOn(!a->sbinfo);
17433 +               err = PTR_ERR(a->sbinfo);
17434 +               if (IS_ERR(a->sbinfo))
17435 +                       goto out_opt;
17436 +               au_rw_write_unlock(&a->sbinfo->si_rwsem);
17437 +       }
17438 +
17439 +       err = 0;
17440 +       fc->fs_private = a;
17441 +       fc->ops = &au_fsctx_ops;
17442 +       goto out; /* success */
17443 +
17444 +out_opt:
17445 +       free_page((unsigned long)a->opts.opt);
17446 +out_a:
17447 +       au_kfree_rcu(a);
17448 +out:
17449 +       AuTraceErr(err);
17450 +       return err;
17451 +}
17452 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
17453 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
17454 +++ linux/fs/aufs/fstype.h      2022-11-05 23:02:18.965889284 +0100
17455 @@ -0,0 +1,401 @@
17456 +/* SPDX-License-Identifier: GPL-2.0 */
17457 +/*
17458 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17459 + *
17460 + * This program is free software; you can redistribute it and/or modify
17461 + * it under the terms of the GNU General Public License as published by
17462 + * the Free Software Foundation; either version 2 of the License, or
17463 + * (at your option) any later version.
17464 + *
17465 + * This program is distributed in the hope that it will be useful,
17466 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17467 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17468 + * GNU General Public License for more details.
17469 + *
17470 + * You should have received a copy of the GNU General Public License
17471 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17472 + */
17473 +
17474 +/*
17475 + * judging filesystem type
17476 + */
17477 +
17478 +#ifndef __AUFS_FSTYPE_H__
17479 +#define __AUFS_FSTYPE_H__
17480 +
17481 +#ifdef __KERNEL__
17482 +
17483 +#include <linux/fs.h>
17484 +#include <linux/magic.h>
17485 +#include <linux/nfs_fs.h>
17486 +#include <linux/romfs_fs.h>
17487 +
17488 +static inline int au_test_aufs(struct super_block *sb)
17489 +{
17490 +       return sb->s_magic == AUFS_SUPER_MAGIC;
17491 +}
17492 +
17493 +static inline const char *au_sbtype(struct super_block *sb)
17494 +{
17495 +       return sb->s_type->name;
17496 +}
17497 +
17498 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17499 +{
17500 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17501 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
17502 +#else
17503 +       return 0;
17504 +#endif
17505 +}
17506 +
17507 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17508 +{
17509 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17510 +       return sb->s_magic == ROMFS_MAGIC;
17511 +#else
17512 +       return 0;
17513 +#endif
17514 +}
17515 +
17516 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17517 +{
17518 +#if IS_ENABLED(CONFIG_CRAMFS)
17519 +       return sb->s_magic == CRAMFS_MAGIC;
17520 +#endif
17521 +       return 0;
17522 +}
17523 +
17524 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17525 +{
17526 +#if IS_ENABLED(CONFIG_NFS_FS)
17527 +       return sb->s_magic == NFS_SUPER_MAGIC;
17528 +#else
17529 +       return 0;
17530 +#endif
17531 +}
17532 +
17533 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17534 +{
17535 +#if IS_ENABLED(CONFIG_FUSE_FS)
17536 +       return sb->s_magic == FUSE_SUPER_MAGIC;
17537 +#else
17538 +       return 0;
17539 +#endif
17540 +}
17541 +
17542 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17543 +{
17544 +#if IS_ENABLED(CONFIG_XFS_FS)
17545 +       return sb->s_magic == XFS_SB_MAGIC;
17546 +#else
17547 +       return 0;
17548 +#endif
17549 +}
17550 +
17551 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17552 +{
17553 +#ifdef CONFIG_TMPFS
17554 +       return sb->s_magic == TMPFS_MAGIC;
17555 +#else
17556 +       return 0;
17557 +#endif
17558 +}
17559 +
17560 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17561 +{
17562 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17563 +       return !strcmp(au_sbtype(sb), "ecryptfs");
17564 +#else
17565 +       return 0;
17566 +#endif
17567 +}
17568 +
17569 +static inline int au_test_ramfs(struct super_block *sb)
17570 +{
17571 +       return sb->s_magic == RAMFS_MAGIC;
17572 +}
17573 +
17574 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17575 +{
17576 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17577 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
17578 +#else
17579 +       return 0;
17580 +#endif
17581 +}
17582 +
17583 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17584 +{
17585 +#ifdef CONFIG_PROC_FS
17586 +       return sb->s_magic == PROC_SUPER_MAGIC;
17587 +#else
17588 +       return 0;
17589 +#endif
17590 +}
17591 +
17592 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17593 +{
17594 +#ifdef CONFIG_SYSFS
17595 +       return sb->s_magic == SYSFS_MAGIC;
17596 +#else
17597 +       return 0;
17598 +#endif
17599 +}
17600 +
17601 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17602 +{
17603 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17604 +       return sb->s_magic == CONFIGFS_MAGIC;
17605 +#else
17606 +       return 0;
17607 +#endif
17608 +}
17609 +
17610 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17611 +{
17612 +#if IS_ENABLED(CONFIG_MINIX_FS)
17613 +       return sb->s_magic == MINIX3_SUPER_MAGIC
17614 +               || sb->s_magic == MINIX2_SUPER_MAGIC
17615 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
17616 +               || sb->s_magic == MINIX_SUPER_MAGIC
17617 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
17618 +#else
17619 +       return 0;
17620 +#endif
17621 +}
17622 +
17623 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17624 +{
17625 +#if IS_ENABLED(CONFIG_FAT_FS)
17626 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
17627 +#else
17628 +       return 0;
17629 +#endif
17630 +}
17631 +
17632 +static inline int au_test_msdos(struct super_block *sb)
17633 +{
17634 +       return au_test_fat(sb);
17635 +}
17636 +
17637 +static inline int au_test_vfat(struct super_block *sb)
17638 +{
17639 +       return au_test_fat(sb);
17640 +}
17641 +
17642 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17643 +{
17644 +#ifdef CONFIG_SECURITYFS
17645 +       return sb->s_magic == SECURITYFS_MAGIC;
17646 +#else
17647 +       return 0;
17648 +#endif
17649 +}
17650 +
17651 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17652 +{
17653 +#if IS_ENABLED(CONFIG_SQUASHFS)
17654 +       return sb->s_magic == SQUASHFS_MAGIC;
17655 +#else
17656 +       return 0;
17657 +#endif
17658 +}
17659 +
17660 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17661 +{
17662 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17663 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
17664 +#else
17665 +       return 0;
17666 +#endif
17667 +}
17668 +
17669 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17670 +{
17671 +#if IS_ENABLED(CONFIG_XENFS)
17672 +       return sb->s_magic == XENFS_SUPER_MAGIC;
17673 +#else
17674 +       return 0;
17675 +#endif
17676 +}
17677 +
17678 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17679 +{
17680 +#ifdef CONFIG_DEBUG_FS
17681 +       return sb->s_magic == DEBUGFS_MAGIC;
17682 +#else
17683 +       return 0;
17684 +#endif
17685 +}
17686 +
17687 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17688 +{
17689 +#if IS_ENABLED(CONFIG_NILFS)
17690 +       return sb->s_magic == NILFS_SUPER_MAGIC;
17691 +#else
17692 +       return 0;
17693 +#endif
17694 +}
17695 +
17696 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17697 +{
17698 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17699 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17700 +#else
17701 +       return 0;
17702 +#endif
17703 +}
17704 +
17705 +/* ---------------------------------------------------------------------- */
17706 +/*
17707 + * they can't be an aufs branch.
17708 + */
17709 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17710 +{
17711 +       return
17712 +#ifndef CONFIG_AUFS_BR_RAMFS
17713 +               au_test_ramfs(sb) ||
17714 +#endif
17715 +               au_test_procfs(sb)
17716 +               || au_test_sysfs(sb)
17717 +               || au_test_configfs(sb)
17718 +               || au_test_debugfs(sb)
17719 +               || au_test_securityfs(sb)
17720 +               || au_test_xenfs(sb)
17721 +               || au_test_ecryptfs(sb)
17722 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
17723 +               || au_test_aufs(sb); /* will be supported in next version */
17724 +}
17725 +
17726 +static inline int au_test_fs_remote(struct super_block *sb)
17727 +{
17728 +       return !au_test_tmpfs(sb)
17729 +#ifdef CONFIG_AUFS_BR_RAMFS
17730 +               && !au_test_ramfs(sb)
17731 +#endif
17732 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17733 +}
17734 +
17735 +/* ---------------------------------------------------------------------- */
17736 +
17737 +/*
17738 + * Note: these functions (below) are created after reading ->getattr() in all
17739 + * filesystems under linux/fs. it means we have to do so in every update...
17740 + */
17741 +
17742 +/*
17743 + * some filesystems require getattr to refresh the inode attributes before
17744 + * referencing.
17745 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17746 + * and leave the work for d_revalidate()
17747 + */
17748 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17749 +{
17750 +       return au_test_nfs(sb)
17751 +               || au_test_fuse(sb)
17752 +               /* || au_test_btrfs(sb) */      /* untested */
17753 +               ;
17754 +}
17755 +
17756 +/*
17757 + * filesystems which don't maintain i_size or i_blocks.
17758 + */
17759 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17760 +{
17761 +       return au_test_xfs(sb)
17762 +               || au_test_btrfs(sb)
17763 +               || au_test_ubifs(sb)
17764 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
17765 +               /* || au_test_minix(sb) */      /* untested */
17766 +               ;
17767 +}
17768 +
17769 +/*
17770 + * filesystems which don't store the correct value in some of their inode
17771 + * attributes.
17772 + */
17773 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17774 +{
17775 +       return au_test_fs_bad_iattr_size(sb)
17776 +               || au_test_fat(sb)
17777 +               || au_test_msdos(sb)
17778 +               || au_test_vfat(sb);
17779 +}
17780 +
17781 +/* they don't check i_nlink in link(2) */
17782 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17783 +{
17784 +       return au_test_tmpfs(sb)
17785 +#ifdef CONFIG_AUFS_BR_RAMFS
17786 +               || au_test_ramfs(sb)
17787 +#endif
17788 +               || au_test_ubifs(sb)
17789 +               || au_test_hfsplus(sb);
17790 +}
17791 +
17792 +/*
17793 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17794 + */
17795 +static inline int au_test_fs_notime(struct super_block *sb)
17796 +{
17797 +       return au_test_nfs(sb)
17798 +               || au_test_fuse(sb)
17799 +               || au_test_ubifs(sb)
17800 +               ;
17801 +}
17802 +
17803 +/* temporary support for i#1 in cramfs */
17804 +static inline int au_test_fs_unique_ino(struct inode *inode)
17805 +{
17806 +       if (au_test_cramfs(inode->i_sb))
17807 +               return inode->i_ino != 1;
17808 +       return 1;
17809 +}
17810 +
17811 +/* ---------------------------------------------------------------------- */
17812 +
17813 +/*
17814 + * the filesystem where the xino files placed must support i/o after unlink and
17815 + * maintain i_size and i_blocks.
17816 + */
17817 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17818 +{
17819 +       return au_test_fs_remote(sb)
17820 +               || au_test_fs_bad_iattr_size(sb)
17821 +               /* don't want unnecessary work for xino */
17822 +               || au_test_aufs(sb)
17823 +               || au_test_ecryptfs(sb)
17824 +               || au_test_nilfs(sb);
17825 +}
17826 +
17827 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17828 +{
17829 +       return au_test_tmpfs(sb)
17830 +               || au_test_ramfs(sb);
17831 +}
17832 +
17833 +/*
17834 + * test if the @sb is real-readonly.
17835 + */
17836 +static inline int au_test_fs_rr(struct super_block *sb)
17837 +{
17838 +       return au_test_squashfs(sb)
17839 +               || au_test_iso9660(sb)
17840 +               || au_test_cramfs(sb)
17841 +               || au_test_romfs(sb);
17842 +}
17843 +
17844 +/*
17845 + * test if the @inode is nfs with 'noacl' option
17846 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17847 + */
17848 +static inline int au_test_nfs_noacl(struct inode *inode)
17849 +{
17850 +       return au_test_nfs(inode->i_sb)
17851 +               /* && IS_POSIXACL(inode) */
17852 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
17853 +}
17854 +
17855 +#endif /* __KERNEL__ */
17856 +#endif /* __AUFS_FSTYPE_H__ */
17857 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
17858 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
17859 +++ linux/fs/aufs/hbl.h 2022-11-05 23:02:18.965889284 +0100
17860 @@ -0,0 +1,65 @@
17861 +/* SPDX-License-Identifier: GPL-2.0 */
17862 +/*
17863 + * Copyright (C) 2017-2022 Junjiro R. Okajima
17864 + *
17865 + * This program is free software; you can redistribute it and/or modify
17866 + * it under the terms of the GNU General Public License as published by
17867 + * the Free Software Foundation; either version 2 of the License, or
17868 + * (at your option) any later version.
17869 + *
17870 + * This program is distributed in the hope that it will be useful,
17871 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17872 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17873 + * GNU General Public License for more details.
17874 + *
17875 + * You should have received a copy of the GNU General Public License
17876 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17877 + */
17878 +
17879 +/*
17880 + * helpers for hlist_bl.h
17881 + */
17882 +
17883 +#ifndef __AUFS_HBL_H__
17884 +#define __AUFS_HBL_H__
17885 +
17886 +#ifdef __KERNEL__
17887 +
17888 +#include <linux/list_bl.h>
17889 +
17890 +static inline void au_hbl_add(struct hlist_bl_node *node,
17891 +                             struct hlist_bl_head *hbl)
17892 +{
17893 +       hlist_bl_lock(hbl);
17894 +       hlist_bl_add_head(node, hbl);
17895 +       hlist_bl_unlock(hbl);
17896 +}
17897 +
17898 +static inline void au_hbl_del(struct hlist_bl_node *node,
17899 +                             struct hlist_bl_head *hbl)
17900 +{
17901 +       hlist_bl_lock(hbl);
17902 +       hlist_bl_del(node);
17903 +       hlist_bl_unlock(hbl);
17904 +}
17905 +
17906 +#define au_hbl_for_each(pos, head)                                     \
17907 +       for (pos = hlist_bl_first(head);                                \
17908 +            pos;                                                       \
17909 +            pos = pos->next)
17910 +
17911 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17912 +{
17913 +       unsigned long cnt;
17914 +       struct hlist_bl_node *pos;
17915 +
17916 +       cnt = 0;
17917 +       hlist_bl_lock(hbl);
17918 +       au_hbl_for_each(pos, hbl)
17919 +               cnt++;
17920 +       hlist_bl_unlock(hbl);
17921 +       return cnt;
17922 +}
17923 +
17924 +#endif /* __KERNEL__ */
17925 +#endif /* __AUFS_HBL_H__ */
17926 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
17927 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
17928 +++ linux/fs/aufs/hfsnotify.c   2022-11-05 23:02:18.965889284 +0100
17929 @@ -0,0 +1,290 @@
17930 +// SPDX-License-Identifier: GPL-2.0
17931 +/*
17932 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17933 + *
17934 + * This program is free software; you can redistribute it and/or modify
17935 + * it under the terms of the GNU General Public License as published by
17936 + * the Free Software Foundation; either version 2 of the License, or
17937 + * (at your option) any later version.
17938 + *
17939 + * This program is distributed in the hope that it will be useful,
17940 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17941 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17942 + * GNU General Public License for more details.
17943 + *
17944 + * You should have received a copy of the GNU General Public License
17945 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17946 + */
17947 +
17948 +/*
17949 + * fsnotify for the lower directories
17950 + */
17951 +
17952 +#include "aufs.h"
17953 +
17954 +/* FS_IN_IGNORED is unnecessary */
17955 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17956 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
17957 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17958 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17959 +
17960 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17961 +{
17962 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17963 +                                            hn_mark);
17964 +       /* AuDbg("here\n"); */
17965 +       au_cache_free_hnotify(hn);
17966 +       smp_mb__before_atomic(); /* for atomic64_dec */
17967 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
17968 +               wake_up(&au_hfsn_wq);
17969 +}
17970 +
17971 +static int au_hfsn_alloc(struct au_hinode *hinode)
17972 +{
17973 +       int err;
17974 +       struct au_hnotify *hn;
17975 +       struct super_block *sb;
17976 +       struct au_branch *br;
17977 +       struct fsnotify_mark *mark;
17978 +       aufs_bindex_t bindex;
17979 +
17980 +       hn = hinode->hi_notify;
17981 +       sb = hn->hn_aufs_inode->i_sb;
17982 +       bindex = au_br_index(sb, hinode->hi_id);
17983 +       br = au_sbr(sb, bindex);
17984 +       AuDebugOn(!br->br_hfsn);
17985 +
17986 +       mark = &hn->hn_mark;
17987 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17988 +       mark->mask = AuHfsnMask;
17989 +       /*
17990 +        * by udba rename or rmdir, aufs assign a new inode to the known
17991 +        * h_inode, so specify 1 to allow dups.
17992 +        */
17993 +       lockdep_off();
17994 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
17995 +       lockdep_on();
17996 +
17997 +       return err;
17998 +}
17999 +
18000 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
18001 +{
18002 +       struct fsnotify_mark *mark;
18003 +       unsigned long long ull;
18004 +       struct fsnotify_group *group;
18005 +
18006 +       ull = atomic64_inc_return(&au_hfsn_ifree);
18007 +       BUG_ON(!ull);
18008 +
18009 +       mark = &hn->hn_mark;
18010 +       spin_lock(&mark->lock);
18011 +       group = mark->group;
18012 +       fsnotify_get_group(group);
18013 +       spin_unlock(&mark->lock);
18014 +       lockdep_off();
18015 +       fsnotify_destroy_mark(mark, group);
18016 +       fsnotify_put_mark(mark);
18017 +       fsnotify_put_group(group);
18018 +       lockdep_on();
18019 +
18020 +       /* free hn by myself */
18021 +       return 0;
18022 +}
18023 +
18024 +/* ---------------------------------------------------------------------- */
18025 +
18026 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
18027 +{
18028 +       struct fsnotify_mark *mark;
18029 +
18030 +       mark = &hinode->hi_notify->hn_mark;
18031 +       spin_lock(&mark->lock);
18032 +       if (do_set) {
18033 +               AuDebugOn(mark->mask & AuHfsnMask);
18034 +               mark->mask |= AuHfsnMask;
18035 +       } else {
18036 +               AuDebugOn(!(mark->mask & AuHfsnMask));
18037 +               mark->mask &= ~AuHfsnMask;
18038 +       }
18039 +       spin_unlock(&mark->lock);
18040 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
18041 +}
18042 +
18043 +/* ---------------------------------------------------------------------- */
18044 +
18045 +/* #define AuDbgHnotify */
18046 +#ifdef AuDbgHnotify
18047 +static char *au_hfsn_name(u32 mask)
18048 +{
18049 +#ifdef CONFIG_AUFS_DEBUG
18050 +#define test_ret(flag)                         \
18051 +       do {                                    \
18052 +               if (mask & flag)                \
18053 +                       return #flag;           \
18054 +       } while (0)
18055 +       test_ret(FS_ACCESS);
18056 +       test_ret(FS_MODIFY);
18057 +       test_ret(FS_ATTRIB);
18058 +       test_ret(FS_CLOSE_WRITE);
18059 +       test_ret(FS_CLOSE_NOWRITE);
18060 +       test_ret(FS_OPEN);
18061 +       test_ret(FS_MOVED_FROM);
18062 +       test_ret(FS_MOVED_TO);
18063 +       test_ret(FS_CREATE);
18064 +       test_ret(FS_DELETE);
18065 +       test_ret(FS_DELETE_SELF);
18066 +       test_ret(FS_MOVE_SELF);
18067 +       test_ret(FS_UNMOUNT);
18068 +       test_ret(FS_Q_OVERFLOW);
18069 +       test_ret(FS_IN_IGNORED);
18070 +       test_ret(FS_ISDIR);
18071 +       test_ret(FS_IN_ONESHOT);
18072 +       test_ret(FS_EVENT_ON_CHILD);
18073 +       return "";
18074 +#undef test_ret
18075 +#else
18076 +       return "??";
18077 +#endif
18078 +}
18079 +#endif
18080 +
18081 +/* ---------------------------------------------------------------------- */
18082 +
18083 +static void au_hfsn_free_group(struct fsnotify_group *group)
18084 +{
18085 +       struct au_br_hfsnotify *hfsn = group->private;
18086 +
18087 +       /* AuDbg("here\n"); */
18088 +       au_kfree_try_rcu(hfsn);
18089 +}
18090 +
18091 +static int au_hfsn_handle_event(struct fsnotify_group *group,
18092 +                               u32 mask, const void *data, int data_type,
18093 +                               struct inode *dir,
18094 +                               const struct qstr *file_name, u32 cookie,
18095 +                               struct fsnotify_iter_info *iter_info)
18096 +{
18097 +       int err;
18098 +       struct au_hnotify *hnotify;
18099 +       struct inode *h_dir, *h_inode;
18100 +       struct fsnotify_mark *inode_mark;
18101 +
18102 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18103 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
18104 +
18105 +       err = 0;
18106 +       /* if FS_UNMOUNT happens, there must be another bug */
18107 +       AuDebugOn(mask & FS_UNMOUNT);
18108 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
18109 +               goto out;
18110 +
18111 +       h_dir = dir;
18112 +       h_inode = NULL;
18113 +#ifdef AuDbgHnotify
18114 +       au_debug_on();
18115 +       if (1 || file_name.len != sizeof(AUFS_XINO_FNAME) - 1
18116 +           || strncmp(file_name.name, AUFS_XINO_FNAME, file_name.len)) {
18117 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18118 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
18119 +                     AuLNPair(file_name), h_inode ? h_inode->i_ino : 0);
18120 +               /* WARN_ON(1); */
18121 +       }
18122 +       au_debug_off();
18123 +#endif
18124 +
18125 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
18126 +       AuDebugOn(!inode_mark);
18127 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
18128 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
18129 +
18130 +out:
18131 +       return err;
18132 +}
18133 +
18134 +static struct fsnotify_ops au_hfsn_ops = {
18135 +       .handle_event           = au_hfsn_handle_event,
18136 +       .free_group_priv        = au_hfsn_free_group,
18137 +       .free_mark              = au_hfsn_free_mark
18138 +};
18139 +
18140 +/* ---------------------------------------------------------------------- */
18141 +
18142 +static void au_hfsn_fin_br(struct au_branch *br)
18143 +{
18144 +       struct au_br_hfsnotify *hfsn;
18145 +
18146 +       hfsn = br->br_hfsn;
18147 +       if (hfsn) {
18148 +               lockdep_off();
18149 +               fsnotify_put_group(hfsn->hfsn_group);
18150 +               lockdep_on();
18151 +       }
18152 +}
18153 +
18154 +static int au_hfsn_init_br(struct au_branch *br, int perm)
18155 +{
18156 +       int err;
18157 +       struct fsnotify_group *group;
18158 +       struct au_br_hfsnotify *hfsn;
18159 +
18160 +       err = 0;
18161 +       br->br_hfsn = NULL;
18162 +       if (!au_br_hnotifyable(perm))
18163 +               goto out;
18164 +
18165 +       err = -ENOMEM;
18166 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18167 +       if (unlikely(!hfsn))
18168 +               goto out;
18169 +
18170 +       err = 0;
18171 +       group = fsnotify_alloc_group(&au_hfsn_ops,
18172 +                                    /*flags - not for userspace*/0);
18173 +       if (IS_ERR(group)) {
18174 +               err = PTR_ERR(group);
18175 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
18176 +               goto out_hfsn;
18177 +       }
18178 +
18179 +       group->private = hfsn;
18180 +       hfsn->hfsn_group = group;
18181 +       br->br_hfsn = hfsn;
18182 +       goto out; /* success */
18183 +
18184 +out_hfsn:
18185 +       au_kfree_try_rcu(hfsn);
18186 +out:
18187 +       return err;
18188 +}
18189 +
18190 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18191 +{
18192 +       int err;
18193 +
18194 +       err = 0;
18195 +       if (!br->br_hfsn)
18196 +               err = au_hfsn_init_br(br, perm);
18197 +
18198 +       return err;
18199 +}
18200 +
18201 +/* ---------------------------------------------------------------------- */
18202 +
18203 +static void au_hfsn_fin(void)
18204 +{
18205 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18206 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18207 +}
18208 +
18209 +const struct au_hnotify_op au_hnotify_op = {
18210 +       .ctl            = au_hfsn_ctl,
18211 +       .alloc          = au_hfsn_alloc,
18212 +       .free           = au_hfsn_free,
18213 +
18214 +       .fin            = au_hfsn_fin,
18215 +
18216 +       .reset_br       = au_hfsn_reset_br,
18217 +       .fin_br         = au_hfsn_fin_br,
18218 +       .init_br        = au_hfsn_init_br
18219 +};
18220 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
18221 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
18222 +++ linux/fs/aufs/hfsplus.c     2022-11-05 23:02:18.965889284 +0100
18223 @@ -0,0 +1,60 @@
18224 +// SPDX-License-Identifier: GPL-2.0
18225 +/*
18226 + * Copyright (C) 2010-2022 Junjiro R. Okajima
18227 + *
18228 + * This program is free software; you can redistribute it and/or modify
18229 + * it under the terms of the GNU General Public License as published by
18230 + * the Free Software Foundation; either version 2 of the License, or
18231 + * (at your option) any later version.
18232 + *
18233 + * This program is distributed in the hope that it will be useful,
18234 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18235 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18236 + * GNU General Public License for more details.
18237 + *
18238 + * You should have received a copy of the GNU General Public License
18239 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18240 + */
18241 +
18242 +/*
18243 + * special support for filesystems which acquires an inode mutex
18244 + * at final closing a file, eg, hfsplus.
18245 + *
18246 + * This trick is very simple and stupid, just to open the file before really
18247 + * necessary open to tell hfsplus that this is not the final closing.
18248 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
18249 + * and au_h_open_post() after releasing it.
18250 + */
18251 +
18252 +#include "aufs.h"
18253 +
18254 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18255 +                          int force_wr)
18256 +{
18257 +       struct file *h_file;
18258 +       struct dentry *h_dentry;
18259 +
18260 +       h_dentry = au_h_dptr(dentry, bindex);
18261 +       AuDebugOn(!h_dentry);
18262 +       AuDebugOn(d_is_negative(h_dentry));
18263 +
18264 +       h_file = NULL;
18265 +       if (au_test_hfsplus(h_dentry->d_sb)
18266 +           && d_is_reg(h_dentry))
18267 +               h_file = au_h_open(dentry, bindex,
18268 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
18269 +                                  /*file*/NULL, force_wr);
18270 +       return h_file;
18271 +}
18272 +
18273 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18274 +                   struct file *h_file)
18275 +{
18276 +       struct au_branch *br;
18277 +
18278 +       if (h_file) {
18279 +               fput(h_file);
18280 +               br = au_sbr(dentry->d_sb, bindex);
18281 +               au_lcnt_dec(&br->br_nfiles);
18282 +       }
18283 +}
18284 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
18285 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
18286 +++ linux/fs/aufs/hnotify.c     2022-11-05 23:02:18.965889284 +0100
18287 @@ -0,0 +1,715 @@
18288 +// SPDX-License-Identifier: GPL-2.0
18289 +/*
18290 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18291 + *
18292 + * This program is free software; you can redistribute it and/or modify
18293 + * it under the terms of the GNU General Public License as published by
18294 + * the Free Software Foundation; either version 2 of the License, or
18295 + * (at your option) any later version.
18296 + *
18297 + * This program is distributed in the hope that it will be useful,
18298 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18299 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18300 + * GNU General Public License for more details.
18301 + *
18302 + * You should have received a copy of the GNU General Public License
18303 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18304 + */
18305 +
18306 +/*
18307 + * abstraction to notify the direct changes on lower directories
18308 + */
18309 +
18310 +/* #include <linux/iversion.h> */
18311 +#include "aufs.h"
18312 +
18313 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18314 +{
18315 +       int err;
18316 +       struct au_hnotify *hn;
18317 +
18318 +       err = -ENOMEM;
18319 +       hn = au_cache_alloc_hnotify();
18320 +       if (hn) {
18321 +               hn->hn_aufs_inode = inode;
18322 +               hinode->hi_notify = hn;
18323 +               err = au_hnotify_op.alloc(hinode);
18324 +               AuTraceErr(err);
18325 +               if (unlikely(err)) {
18326 +                       hinode->hi_notify = NULL;
18327 +                       au_cache_free_hnotify(hn);
18328 +                       /*
18329 +                        * The upper dir was removed by udba, but the same named
18330 +                        * dir left. In this case, aufs assigns a new inode
18331 +                        * number and set the monitor again.
18332 +                        * For the lower dir, the old monitor is still left.
18333 +                        */
18334 +                       if (err == -EEXIST)
18335 +                               err = 0;
18336 +               }
18337 +       }
18338 +
18339 +       AuTraceErr(err);
18340 +       return err;
18341 +}
18342 +
18343 +void au_hn_free(struct au_hinode *hinode)
18344 +{
18345 +       struct au_hnotify *hn;
18346 +
18347 +       hn = hinode->hi_notify;
18348 +       if (hn) {
18349 +               hinode->hi_notify = NULL;
18350 +               if (au_hnotify_op.free(hinode, hn))
18351 +                       au_cache_free_hnotify(hn);
18352 +       }
18353 +}
18354 +
18355 +/* ---------------------------------------------------------------------- */
18356 +
18357 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18358 +{
18359 +       if (hinode->hi_notify)
18360 +               au_hnotify_op.ctl(hinode, do_set);
18361 +}
18362 +
18363 +void au_hn_reset(struct inode *inode, unsigned int flags)
18364 +{
18365 +       aufs_bindex_t bindex, bbot;
18366 +       struct inode *hi;
18367 +       struct dentry *iwhdentry;
18368 +
18369 +       bbot = au_ibbot(inode);
18370 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18371 +               hi = au_h_iptr(inode, bindex);
18372 +               if (!hi)
18373 +                       continue;
18374 +
18375 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18376 +               iwhdentry = au_hi_wh(inode, bindex);
18377 +               if (iwhdentry)
18378 +                       dget(iwhdentry);
18379 +               au_igrab(hi);
18380 +               au_set_h_iptr(inode, bindex, NULL, 0);
18381 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
18382 +                             flags & ~AuHi_XINO);
18383 +               iput(hi);
18384 +               dput(iwhdentry);
18385 +               /* inode_unlock(hi); */
18386 +       }
18387 +}
18388 +
18389 +/* ---------------------------------------------------------------------- */
18390 +
18391 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18392 +{
18393 +       int err;
18394 +       aufs_bindex_t bindex, bbot, bfound, btop;
18395 +       struct inode *h_i;
18396 +
18397 +       err = 0;
18398 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18399 +               pr_warn("branch root dir was changed\n");
18400 +               goto out;
18401 +       }
18402 +
18403 +       bfound = -1;
18404 +       bbot = au_ibbot(inode);
18405 +       btop = au_ibtop(inode);
18406 +#if 0 /* reserved for future use */
18407 +       if (bindex == bbot) {
18408 +               /* keep this ino in rename case */
18409 +               goto out;
18410 +       }
18411 +#endif
18412 +       for (bindex = btop; bindex <= bbot; bindex++)
18413 +               if (au_h_iptr(inode, bindex) == h_inode) {
18414 +                       bfound = bindex;
18415 +                       break;
18416 +               }
18417 +       if (bfound < 0)
18418 +               goto out;
18419 +
18420 +       for (bindex = btop; bindex <= bbot; bindex++) {
18421 +               h_i = au_h_iptr(inode, bindex);
18422 +               if (!h_i)
18423 +                       continue;
18424 +
18425 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18426 +               /* ignore this error */
18427 +               /* bad action? */
18428 +       }
18429 +
18430 +       /* children inode number will be broken */
18431 +
18432 +out:
18433 +       AuTraceErr(err);
18434 +       return err;
18435 +}
18436 +
18437 +static int hn_gen_tree(struct dentry *dentry)
18438 +{
18439 +       int err, i, j, ndentry;
18440 +       struct au_dcsub_pages dpages;
18441 +       struct au_dpage *dpage;
18442 +       struct dentry **dentries;
18443 +
18444 +       err = au_dpages_init(&dpages, GFP_NOFS);
18445 +       if (unlikely(err))
18446 +               goto out;
18447 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18448 +       if (unlikely(err))
18449 +               goto out_dpages;
18450 +
18451 +       for (i = 0; i < dpages.ndpage; i++) {
18452 +               dpage = dpages.dpages + i;
18453 +               dentries = dpage->dentries;
18454 +               ndentry = dpage->ndentry;
18455 +               for (j = 0; j < ndentry; j++) {
18456 +                       struct dentry *d;
18457 +
18458 +                       d = dentries[j];
18459 +                       if (IS_ROOT(d))
18460 +                               continue;
18461 +
18462 +                       au_digen_dec(d);
18463 +                       if (d_really_is_positive(d))
18464 +                               /* todo: reset children xino?
18465 +                                  cached children only? */
18466 +                               au_iigen_dec(d_inode(d));
18467 +               }
18468 +       }
18469 +
18470 +out_dpages:
18471 +       au_dpages_free(&dpages);
18472 +out:
18473 +       return err;
18474 +}
18475 +
18476 +/*
18477 + * return 0 if processed.
18478 + */
18479 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18480 +                          const unsigned int isdir)
18481 +{
18482 +       int err;
18483 +       struct dentry *d;
18484 +       struct qstr *dname;
18485 +
18486 +       err = 1;
18487 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18488 +               pr_warn("branch root dir was changed\n");
18489 +               err = 0;
18490 +               goto out;
18491 +       }
18492 +
18493 +       if (!isdir) {
18494 +               AuDebugOn(!name);
18495 +               au_iigen_dec(inode);
18496 +               spin_lock(&inode->i_lock);
18497 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18498 +                       spin_lock(&d->d_lock);
18499 +                       dname = &d->d_name;
18500 +                       if (dname->len != nlen
18501 +                           && memcmp(dname->name, name, nlen)) {
18502 +                               spin_unlock(&d->d_lock);
18503 +                               continue;
18504 +                       }
18505 +                       err = 0;
18506 +                       au_digen_dec(d);
18507 +                       spin_unlock(&d->d_lock);
18508 +                       break;
18509 +               }
18510 +               spin_unlock(&inode->i_lock);
18511 +       } else {
18512 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18513 +               d = d_find_any_alias(inode);
18514 +               if (!d) {
18515 +                       au_iigen_dec(inode);
18516 +                       goto out;
18517 +               }
18518 +
18519 +               spin_lock(&d->d_lock);
18520 +               dname = &d->d_name;
18521 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18522 +                       spin_unlock(&d->d_lock);
18523 +                       err = hn_gen_tree(d);
18524 +                       spin_lock(&d->d_lock);
18525 +               }
18526 +               spin_unlock(&d->d_lock);
18527 +               dput(d);
18528 +       }
18529 +
18530 +out:
18531 +       AuTraceErr(err);
18532 +       return err;
18533 +}
18534 +
18535 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18536 +{
18537 +       int err;
18538 +
18539 +       if (IS_ROOT(dentry)) {
18540 +               pr_warn("branch root dir was changed\n");
18541 +               return 0;
18542 +       }
18543 +
18544 +       err = 0;
18545 +       if (!isdir) {
18546 +               au_digen_dec(dentry);
18547 +               if (d_really_is_positive(dentry))
18548 +                       au_iigen_dec(d_inode(dentry));
18549 +       } else {
18550 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18551 +               if (d_really_is_positive(dentry))
18552 +                       err = hn_gen_tree(dentry);
18553 +       }
18554 +
18555 +       AuTraceErr(err);
18556 +       return err;
18557 +}
18558 +
18559 +/* ---------------------------------------------------------------------- */
18560 +
18561 +/* hnotify job flags */
18562 +#define AuHnJob_XINO0          1
18563 +#define AuHnJob_GEN            (1 << 1)
18564 +#define AuHnJob_DIRENT         (1 << 2)
18565 +#define AuHnJob_ISDIR          (1 << 3)
18566 +#define AuHnJob_TRYXINO0       (1 << 4)
18567 +#define AuHnJob_MNTPNT         (1 << 5)
18568 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
18569 +#define au_fset_hnjob(flags, name) \
18570 +       do { (flags) |= AuHnJob_##name; } while (0)
18571 +#define au_fclr_hnjob(flags, name) \
18572 +       do { (flags) &= ~AuHnJob_##name; } while (0)
18573 +
18574 +enum {
18575 +       AuHn_CHILD,
18576 +       AuHn_PARENT,
18577 +       AuHnLast
18578 +};
18579 +
18580 +struct au_hnotify_args {
18581 +       struct inode *h_dir, *dir, *h_child_inode;
18582 +       u32 mask;
18583 +       unsigned int flags[AuHnLast];
18584 +       unsigned int h_child_nlen;
18585 +       char h_child_name[];
18586 +};
18587 +
18588 +struct hn_job_args {
18589 +       unsigned int flags;
18590 +       struct inode *inode, *h_inode, *dir, *h_dir;
18591 +       struct dentry *dentry;
18592 +       char *h_name;
18593 +       int h_nlen;
18594 +};
18595 +
18596 +static int hn_job(struct hn_job_args *a)
18597 +{
18598 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18599 +       int e;
18600 +
18601 +       /* reset xino */
18602 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18603 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
18604 +
18605 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
18606 +           && a->inode
18607 +           && a->h_inode) {
18608 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18609 +               if (!a->h_inode->i_nlink
18610 +                   && !(a->h_inode->i_state & I_LINKABLE))
18611 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
18612 +               inode_unlock_shared(a->h_inode);
18613 +       }
18614 +
18615 +       /* make the generation obsolete */
18616 +       if (au_ftest_hnjob(a->flags, GEN)) {
18617 +               e = -1;
18618 +               if (a->inode)
18619 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18620 +                                             isdir);
18621 +               if (e && a->dentry)
18622 +                       hn_gen_by_name(a->dentry, isdir);
18623 +               /* ignore this error */
18624 +       }
18625 +
18626 +       /* make dir entries obsolete */
18627 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18628 +               struct au_vdir *vdir;
18629 +
18630 +               vdir = au_ivdir(a->inode);
18631 +               if (vdir)
18632 +                       vdir->vd_jiffy = 0;
18633 +               /* IMustLock(a->inode); */
18634 +               /* inode_inc_iversion(a->inode); */
18635 +       }
18636 +
18637 +       /* can do nothing but warn */
18638 +       if (au_ftest_hnjob(a->flags, MNTPNT)
18639 +           && a->dentry
18640 +           && d_mountpoint(a->dentry))
18641 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18642 +
18643 +       return 0;
18644 +}
18645 +
18646 +/* ---------------------------------------------------------------------- */
18647 +
18648 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18649 +                                          struct inode *dir)
18650 +{
18651 +       struct dentry *dentry, *d, *parent;
18652 +       struct qstr *dname;
18653 +
18654 +       parent = d_find_any_alias(dir);
18655 +       if (!parent)
18656 +               return NULL;
18657 +
18658 +       dentry = NULL;
18659 +       spin_lock(&parent->d_lock);
18660 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
18661 +               /* AuDbg("%pd\n", d); */
18662 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18663 +               dname = &d->d_name;
18664 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
18665 +                       goto cont_unlock;
18666 +               if (au_di(d))
18667 +                       au_digen_dec(d);
18668 +               else
18669 +                       goto cont_unlock;
18670 +               if (au_dcount(d) > 0) {
18671 +                       dentry = dget_dlock(d);
18672 +                       spin_unlock(&d->d_lock);
18673 +                       break;
18674 +               }
18675 +
18676 +cont_unlock:
18677 +               spin_unlock(&d->d_lock);
18678 +       }
18679 +       spin_unlock(&parent->d_lock);
18680 +       dput(parent);
18681 +
18682 +       if (dentry)
18683 +               di_write_lock_child(dentry);
18684 +
18685 +       return dentry;
18686 +}
18687 +
18688 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18689 +                                        aufs_bindex_t bindex, ino_t h_ino)
18690 +{
18691 +       struct inode *inode;
18692 +       ino_t ino;
18693 +       int err;
18694 +
18695 +       inode = NULL;
18696 +       err = au_xino_read(sb, bindex, h_ino, &ino);
18697 +       if (!err && ino)
18698 +               inode = ilookup(sb, ino);
18699 +       if (!inode)
18700 +               goto out;
18701 +
18702 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18703 +               pr_warn("wrong root branch\n");
18704 +               iput(inode);
18705 +               inode = NULL;
18706 +               goto out;
18707 +       }
18708 +
18709 +       ii_write_lock_child(inode);
18710 +
18711 +out:
18712 +       return inode;
18713 +}
18714 +
18715 +static void au_hn_bh(void *_args)
18716 +{
18717 +       struct au_hnotify_args *a = _args;
18718 +       struct super_block *sb;
18719 +       aufs_bindex_t bindex, bbot, bfound;
18720 +       unsigned char xino, try_iput;
18721 +       int err;
18722 +       struct inode *inode;
18723 +       ino_t h_ino;
18724 +       struct hn_job_args args;
18725 +       struct dentry *dentry;
18726 +       struct au_sbinfo *sbinfo;
18727 +
18728 +       AuDebugOn(!_args);
18729 +       AuDebugOn(!a->h_dir);
18730 +       AuDebugOn(!a->dir);
18731 +       AuDebugOn(!a->mask);
18732 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18733 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
18734 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
18735 +
18736 +       inode = NULL;
18737 +       dentry = NULL;
18738 +       /*
18739 +        * do not lock a->dir->i_mutex here
18740 +        * because of d_revalidate() may cause a deadlock.
18741 +        */
18742 +       sb = a->dir->i_sb;
18743 +       AuDebugOn(!sb);
18744 +       sbinfo = au_sbi(sb);
18745 +       AuDebugOn(!sbinfo);
18746 +       si_write_lock(sb, AuLock_NOPLMW);
18747 +
18748 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18749 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18750 +               case FS_MOVED_FROM:
18751 +               case FS_MOVED_TO:
18752 +                       AuWarn1("DIRREN with UDBA may not work correctly "
18753 +                               "for the direct rename(2)\n");
18754 +               }
18755 +
18756 +       ii_read_lock_parent(a->dir);
18757 +       bfound = -1;
18758 +       bbot = au_ibbot(a->dir);
18759 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18760 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18761 +                       bfound = bindex;
18762 +                       break;
18763 +               }
18764 +       ii_read_unlock(a->dir);
18765 +       if (unlikely(bfound < 0))
18766 +               goto out;
18767 +
18768 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
18769 +       h_ino = 0;
18770 +       if (a->h_child_inode)
18771 +               h_ino = a->h_child_inode->i_ino;
18772 +
18773 +       if (a->h_child_nlen
18774 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18775 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18776 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18777 +                                             a->dir);
18778 +       try_iput = 0;
18779 +       if (dentry && d_really_is_positive(dentry))
18780 +               inode = d_inode(dentry);
18781 +       if (xino && !inode && h_ino
18782 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18783 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18784 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18785 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18786 +               try_iput = 1;
18787 +       }
18788 +
18789 +       args.flags = a->flags[AuHn_CHILD];
18790 +       args.dentry = dentry;
18791 +       args.inode = inode;
18792 +       args.h_inode = a->h_child_inode;
18793 +       args.dir = a->dir;
18794 +       args.h_dir = a->h_dir;
18795 +       args.h_name = a->h_child_name;
18796 +       args.h_nlen = a->h_child_nlen;
18797 +       err = hn_job(&args);
18798 +       if (dentry) {
18799 +               if (au_di(dentry))
18800 +                       di_write_unlock(dentry);
18801 +               dput(dentry);
18802 +       }
18803 +       if (inode && try_iput) {
18804 +               ii_write_unlock(inode);
18805 +               iput(inode);
18806 +       }
18807 +
18808 +       ii_write_lock_parent(a->dir);
18809 +       args.flags = a->flags[AuHn_PARENT];
18810 +       args.dentry = NULL;
18811 +       args.inode = a->dir;
18812 +       args.h_inode = a->h_dir;
18813 +       args.dir = NULL;
18814 +       args.h_dir = NULL;
18815 +       args.h_name = NULL;
18816 +       args.h_nlen = 0;
18817 +       err = hn_job(&args);
18818 +       ii_write_unlock(a->dir);
18819 +
18820 +out:
18821 +       iput(a->h_child_inode);
18822 +       iput(a->h_dir);
18823 +       iput(a->dir);
18824 +       si_write_unlock(sb);
18825 +       au_nwt_done(&sbinfo->si_nowait);
18826 +       au_kfree_rcu(a);
18827 +}
18828 +
18829 +/* ---------------------------------------------------------------------- */
18830 +
18831 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18832 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
18833 +{
18834 +       int err, len;
18835 +       unsigned int flags[AuHnLast], f;
18836 +       unsigned char isdir, isroot, wh;
18837 +       struct inode *dir;
18838 +       struct au_hnotify_args *args;
18839 +       char *p, *h_child_name;
18840 +
18841 +       err = 0;
18842 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18843 +       dir = igrab(hnotify->hn_aufs_inode);
18844 +       if (!dir)
18845 +               goto out;
18846 +
18847 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
18848 +       wh = 0;
18849 +       h_child_name = (void *)h_child_qstr->name;
18850 +       len = h_child_qstr->len;
18851 +       if (h_child_name) {
18852 +               if (len > AUFS_WH_PFX_LEN
18853 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18854 +                       h_child_name += AUFS_WH_PFX_LEN;
18855 +                       len -= AUFS_WH_PFX_LEN;
18856 +                       wh = 1;
18857 +               }
18858 +       }
18859 +
18860 +       isdir = 0;
18861 +       if (h_child_inode)
18862 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
18863 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
18864 +       flags[AuHn_CHILD] = 0;
18865 +       if (isdir)
18866 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
18867 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18868 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
18869 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18870 +       case FS_MOVED_FROM:
18871 +       case FS_MOVED_TO:
18872 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18873 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18874 +               fallthrough;
18875 +       case FS_CREATE:
18876 +               AuDebugOn(!h_child_name);
18877 +               break;
18878 +
18879 +       case FS_DELETE:
18880 +               /*
18881 +                * aufs never be able to get this child inode.
18882 +                * revalidation should be in d_revalidate()
18883 +                * by checking i_nlink, i_generation or d_unhashed().
18884 +                */
18885 +               AuDebugOn(!h_child_name);
18886 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18887 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18888 +               break;
18889 +
18890 +       default:
18891 +               AuDebugOn(1);
18892 +       }
18893 +
18894 +       if (wh)
18895 +               h_child_inode = NULL;
18896 +
18897 +       err = -ENOMEM;
18898 +       /* iput() and kfree() will be called in au_hnotify() */
18899 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18900 +       if (unlikely(!args)) {
18901 +               AuErr1("no memory\n");
18902 +               iput(dir);
18903 +               goto out;
18904 +       }
18905 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18906 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18907 +       args->mask = mask;
18908 +       args->dir = dir;
18909 +       args->h_dir = igrab(h_dir);
18910 +       if (h_child_inode)
18911 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
18912 +       args->h_child_inode = h_child_inode;
18913 +       args->h_child_nlen = len;
18914 +       if (len) {
18915 +               p = (void *)args;
18916 +               p += sizeof(*args);
18917 +               memcpy(p, h_child_name, len);
18918 +               p[len] = 0;
18919 +       }
18920 +
18921 +       /* NFS fires the event for silly-renamed one from kworker */
18922 +       f = 0;
18923 +       if (!dir->i_nlink
18924 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18925 +               f = AuWkq_NEST;
18926 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18927 +       if (unlikely(err)) {
18928 +               pr_err("wkq %d\n", err);
18929 +               iput(args->h_child_inode);
18930 +               iput(args->h_dir);
18931 +               iput(args->dir);
18932 +               au_kfree_rcu(args);
18933 +       }
18934 +
18935 +out:
18936 +       return err;
18937 +}
18938 +
18939 +/* ---------------------------------------------------------------------- */
18940 +
18941 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18942 +{
18943 +       int err;
18944 +
18945 +       AuDebugOn(!(udba & AuOptMask_UDBA));
18946 +
18947 +       err = 0;
18948 +       if (au_hnotify_op.reset_br)
18949 +               err = au_hnotify_op.reset_br(udba, br, perm);
18950 +
18951 +       return err;
18952 +}
18953 +
18954 +int au_hnotify_init_br(struct au_branch *br, int perm)
18955 +{
18956 +       int err;
18957 +
18958 +       err = 0;
18959 +       if (au_hnotify_op.init_br)
18960 +               err = au_hnotify_op.init_br(br, perm);
18961 +
18962 +       return err;
18963 +}
18964 +
18965 +void au_hnotify_fin_br(struct au_branch *br)
18966 +{
18967 +       if (au_hnotify_op.fin_br)
18968 +               au_hnotify_op.fin_br(br);
18969 +}
18970 +
18971 +static void au_hn_destroy_cache(void)
18972 +{
18973 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18974 +       au_cache[AuCache_HNOTIFY] = NULL;
18975 +}
18976 +
18977 +int __init au_hnotify_init(void)
18978 +{
18979 +       int err;
18980 +
18981 +       err = -ENOMEM;
18982 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18983 +       if (au_cache[AuCache_HNOTIFY]) {
18984 +               err = 0;
18985 +               if (au_hnotify_op.init)
18986 +                       err = au_hnotify_op.init();
18987 +               if (unlikely(err))
18988 +                       au_hn_destroy_cache();
18989 +       }
18990 +       AuTraceErr(err);
18991 +       return err;
18992 +}
18993 +
18994 +void au_hnotify_fin(void)
18995 +{
18996 +       if (au_hnotify_op.fin)
18997 +               au_hnotify_op.fin();
18998 +
18999 +       /* cf. au_cache_fin() */
19000 +       if (au_cache[AuCache_HNOTIFY])
19001 +               au_hn_destroy_cache();
19002 +}
19003 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
19004 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
19005 +++ linux/fs/aufs/iinfo.c       2022-11-05 23:02:18.965889284 +0100
19006 @@ -0,0 +1,286 @@
19007 +// SPDX-License-Identifier: GPL-2.0
19008 +/*
19009 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19010 + *
19011 + * This program is free software; you can redistribute it and/or modify
19012 + * it under the terms of the GNU General Public License as published by
19013 + * the Free Software Foundation; either version 2 of the License, or
19014 + * (at your option) any later version.
19015 + *
19016 + * This program is distributed in the hope that it will be useful,
19017 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19018 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19019 + * GNU General Public License for more details.
19020 + *
19021 + * You should have received a copy of the GNU General Public License
19022 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19023 + */
19024 +
19025 +/*
19026 + * inode private data
19027 + */
19028 +
19029 +#include "aufs.h"
19030 +
19031 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
19032 +{
19033 +       struct inode *h_inode;
19034 +       struct au_hinode *hinode;
19035 +
19036 +       IiMustAnyLock(inode);
19037 +
19038 +       hinode = au_hinode(au_ii(inode), bindex);
19039 +       h_inode = hinode->hi_inode;
19040 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19041 +       return h_inode;
19042 +}
19043 +
19044 +/* todo: hard/soft set? */
19045 +void au_hiput(struct au_hinode *hinode)
19046 +{
19047 +       au_hn_free(hinode);
19048 +       dput(hinode->hi_whdentry);
19049 +       iput(hinode->hi_inode);
19050 +}
19051 +
19052 +unsigned int au_hi_flags(struct inode *inode, int isdir)
19053 +{
19054 +       unsigned int flags;
19055 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
19056 +
19057 +       flags = 0;
19058 +       if (au_opt_test(mnt_flags, XINO))
19059 +               au_fset_hi(flags, XINO);
19060 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19061 +               au_fset_hi(flags, HNOTIFY);
19062 +       return flags;
19063 +}
19064 +
19065 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19066 +                  struct inode *h_inode, unsigned int flags)
19067 +{
19068 +       struct au_hinode *hinode;
19069 +       struct inode *hi;
19070 +       struct au_iinfo *iinfo = au_ii(inode);
19071 +
19072 +       IiMustWriteLock(inode);
19073 +
19074 +       hinode = au_hinode(iinfo, bindex);
19075 +       hi = hinode->hi_inode;
19076 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19077 +
19078 +       if (hi)
19079 +               au_hiput(hinode);
19080 +       hinode->hi_inode = h_inode;
19081 +       if (h_inode) {
19082 +               int err;
19083 +               struct super_block *sb = inode->i_sb;
19084 +               struct au_branch *br;
19085 +
19086 +               AuDebugOn(inode->i_mode
19087 +                         && (h_inode->i_mode & S_IFMT)
19088 +                         != (inode->i_mode & S_IFMT));
19089 +               if (bindex == iinfo->ii_btop)
19090 +                       au_cpup_igen(inode, h_inode);
19091 +               br = au_sbr(sb, bindex);
19092 +               hinode->hi_id = br->br_id;
19093 +               if (au_ftest_hi(flags, XINO)) {
19094 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
19095 +                                           inode->i_ino);
19096 +                       if (unlikely(err))
19097 +                               AuIOErr1("failed au_xino_write() %d\n", err);
19098 +               }
19099 +
19100 +               if (au_ftest_hi(flags, HNOTIFY)
19101 +                   && au_br_hnotifyable(br->br_perm)) {
19102 +                       err = au_hn_alloc(hinode, inode);
19103 +                       if (unlikely(err))
19104 +                               AuIOErr1("au_hn_alloc() %d\n", err);
19105 +               }
19106 +       }
19107 +}
19108 +
19109 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19110 +                 struct dentry *h_wh)
19111 +{
19112 +       struct au_hinode *hinode;
19113 +
19114 +       IiMustWriteLock(inode);
19115 +
19116 +       hinode = au_hinode(au_ii(inode), bindex);
19117 +       AuDebugOn(hinode->hi_whdentry);
19118 +       hinode->hi_whdentry = h_wh;
19119 +}
19120 +
19121 +void au_update_iigen(struct inode *inode, int half)
19122 +{
19123 +       struct au_iinfo *iinfo;
19124 +       struct au_iigen *iigen;
19125 +       unsigned int sigen;
19126 +
19127 +       sigen = au_sigen(inode->i_sb);
19128 +       iinfo = au_ii(inode);
19129 +       iigen = &iinfo->ii_generation;
19130 +       spin_lock(&iigen->ig_spin);
19131 +       iigen->ig_generation = sigen;
19132 +       if (half)
19133 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19134 +       else
19135 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
19136 +       spin_unlock(&iigen->ig_spin);
19137 +}
19138 +
19139 +/* it may be called at remount time, too */
19140 +void au_update_ibrange(struct inode *inode, int do_put_zero)
19141 +{
19142 +       struct au_iinfo *iinfo;
19143 +       aufs_bindex_t bindex, bbot;
19144 +
19145 +       AuDebugOn(au_is_bad_inode(inode));
19146 +       IiMustWriteLock(inode);
19147 +
19148 +       iinfo = au_ii(inode);
19149 +       if (do_put_zero && iinfo->ii_btop >= 0) {
19150 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19151 +                    bindex++) {
19152 +                       struct inode *h_i;
19153 +
19154 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
19155 +                       if (h_i
19156 +                           && !h_i->i_nlink
19157 +                           && !(h_i->i_state & I_LINKABLE))
19158 +                               au_set_h_iptr(inode, bindex, NULL, 0);
19159 +               }
19160 +       }
19161 +
19162 +       iinfo->ii_btop = -1;
19163 +       iinfo->ii_bbot = -1;
19164 +       bbot = au_sbbot(inode->i_sb);
19165 +       for (bindex = 0; bindex <= bbot; bindex++)
19166 +               if (au_hinode(iinfo, bindex)->hi_inode) {
19167 +                       iinfo->ii_btop = bindex;
19168 +                       break;
19169 +               }
19170 +       if (iinfo->ii_btop >= 0)
19171 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19172 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
19173 +                               iinfo->ii_bbot = bindex;
19174 +                               break;
19175 +                       }
19176 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
19177 +}
19178 +
19179 +/* ---------------------------------------------------------------------- */
19180 +
19181 +void au_icntnr_init_once(void *_c)
19182 +{
19183 +       struct au_icntnr *c = _c;
19184 +       struct au_iinfo *iinfo = &c->iinfo;
19185 +
19186 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
19187 +       au_rw_init(&iinfo->ii_rwsem);
19188 +       inode_init_once(&c->vfs_inode);
19189 +}
19190 +
19191 +void au_hinode_init(struct au_hinode *hinode)
19192 +{
19193 +       hinode->hi_inode = NULL;
19194 +       hinode->hi_id = -1;
19195 +       au_hn_init(hinode);
19196 +       hinode->hi_whdentry = NULL;
19197 +}
19198 +
19199 +int au_iinfo_init(struct inode *inode)
19200 +{
19201 +       struct au_iinfo *iinfo;
19202 +       struct super_block *sb;
19203 +       struct au_hinode *hi;
19204 +       int nbr, i;
19205 +
19206 +       sb = inode->i_sb;
19207 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19208 +       nbr = au_sbbot(sb) + 1;
19209 +       if (unlikely(nbr <= 0))
19210 +               nbr = 1;
19211 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19212 +       if (hi) {
19213 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
19214 +
19215 +               iinfo->ii_hinode = hi;
19216 +               for (i = 0; i < nbr; i++, hi++)
19217 +                       au_hinode_init(hi);
19218 +
19219 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
19220 +               iinfo->ii_btop = -1;
19221 +               iinfo->ii_bbot = -1;
19222 +               iinfo->ii_vdir = NULL;
19223 +               return 0;
19224 +       }
19225 +       return -ENOMEM;
19226 +}
19227 +
19228 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
19229 +{
19230 +       int err, i;
19231 +       struct au_hinode *hip;
19232 +
19233 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
19234 +
19235 +       err = -ENOMEM;
19236 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19237 +                         may_shrink);
19238 +       if (hip) {
19239 +               iinfo->ii_hinode = hip;
19240 +               i = iinfo->ii_bbot + 1;
19241 +               hip += i;
19242 +               for (; i < nbr; i++, hip++)
19243 +                       au_hinode_init(hip);
19244 +               err = 0;
19245 +       }
19246 +
19247 +       return err;
19248 +}
19249 +
19250 +void au_iinfo_fin(struct inode *inode)
19251 +{
19252 +       struct au_iinfo *iinfo;
19253 +       struct au_hinode *hi;
19254 +       struct super_block *sb;
19255 +       aufs_bindex_t bindex, bbot;
19256 +       const unsigned char unlinked = !inode->i_nlink;
19257 +
19258 +       AuDebugOn(au_is_bad_inode(inode));
19259 +
19260 +       sb = inode->i_sb;
19261 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
19262 +       if (si_pid_test(sb))
19263 +               au_xino_delete_inode(inode, unlinked);
19264 +       else {
19265 +               /*
19266 +                * it is safe to hide the dependency between sbinfo and
19267 +                * sb->s_umount.
19268 +                */
19269 +               lockdep_off();
19270 +               si_noflush_read_lock(sb);
19271 +               au_xino_delete_inode(inode, unlinked);
19272 +               si_read_unlock(sb);
19273 +               lockdep_on();
19274 +       }
19275 +
19276 +       iinfo = au_ii(inode);
19277 +       if (iinfo->ii_vdir)
19278 +               au_vdir_free(iinfo->ii_vdir);
19279 +
19280 +       bindex = iinfo->ii_btop;
19281 +       if (bindex >= 0) {
19282 +               hi = au_hinode(iinfo, bindex);
19283 +               bbot = iinfo->ii_bbot;
19284 +               while (bindex++ <= bbot) {
19285 +                       if (hi->hi_inode)
19286 +                               au_hiput(hi);
19287 +                       hi++;
19288 +               }
19289 +       }
19290 +       au_kfree_rcu(iinfo->ii_hinode);
19291 +       AuRwDestroy(&iinfo->ii_rwsem);
19292 +}
19293 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
19294 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
19295 +++ linux/fs/aufs/inode.c       2023-08-28 12:34:39.956636132 +0200
19296 @@ -0,0 +1,531 @@
19297 +// SPDX-License-Identifier: GPL-2.0
19298 +/*
19299 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19300 + *
19301 + * This program is free software; you can redistribute it and/or modify
19302 + * it under the terms of the GNU General Public License as published by
19303 + * the Free Software Foundation; either version 2 of the License, or
19304 + * (at your option) any later version.
19305 + *
19306 + * This program is distributed in the hope that it will be useful,
19307 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19308 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19309 + * GNU General Public License for more details.
19310 + *
19311 + * You should have received a copy of the GNU General Public License
19312 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19313 + */
19314 +
19315 +/*
19316 + * inode functions
19317 + */
19318 +
19319 +#include <linux/iversion.h>
19320 +#include "aufs.h"
19321 +
19322 +struct inode *au_igrab(struct inode *inode)
19323 +{
19324 +       if (inode) {
19325 +               AuDebugOn(!atomic_read(&inode->i_count));
19326 +               ihold(inode);
19327 +       }
19328 +       return inode;
19329 +}
19330 +
19331 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19332 +{
19333 +       au_cpup_attr_all(inode, /*force*/0);
19334 +       au_update_iigen(inode, /*half*/1);
19335 +       if (do_version)
19336 +               inode_inc_iversion(inode);
19337 +}
19338 +
19339 +static int au_ii_refresh(struct inode *inode, int *update)
19340 +{
19341 +       int err, e, nbr;
19342 +       umode_t type;
19343 +       aufs_bindex_t bindex, new_bindex;
19344 +       struct super_block *sb;
19345 +       struct au_iinfo *iinfo;
19346 +       struct au_hinode *p, *q, tmp;
19347 +
19348 +       AuDebugOn(au_is_bad_inode(inode));
19349 +       IiMustWriteLock(inode);
19350 +
19351 +       *update = 0;
19352 +       sb = inode->i_sb;
19353 +       nbr = au_sbbot(sb) + 1;
19354 +       type = inode->i_mode & S_IFMT;
19355 +       iinfo = au_ii(inode);
19356 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
19357 +       if (unlikely(err))
19358 +               goto out;
19359 +
19360 +       AuDebugOn(iinfo->ii_btop < 0);
19361 +       p = au_hinode(iinfo, iinfo->ii_btop);
19362 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19363 +            bindex++, p++) {
19364 +               if (!p->hi_inode)
19365 +                       continue;
19366 +
19367 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
19368 +               new_bindex = au_br_index(sb, p->hi_id);
19369 +               if (new_bindex == bindex)
19370 +                       continue;
19371 +
19372 +               if (new_bindex < 0) {
19373 +                       *update = 1;
19374 +                       au_hiput(p);
19375 +                       p->hi_inode = NULL;
19376 +                       continue;
19377 +               }
19378 +
19379 +               if (new_bindex < iinfo->ii_btop)
19380 +                       iinfo->ii_btop = new_bindex;
19381 +               if (iinfo->ii_bbot < new_bindex)
19382 +                       iinfo->ii_bbot = new_bindex;
19383 +               /* swap two lower inode, and loop again */
19384 +               q = au_hinode(iinfo, new_bindex);
19385 +               tmp = *q;
19386 +               *q = *p;
19387 +               *p = tmp;
19388 +               if (tmp.hi_inode) {
19389 +                       bindex--;
19390 +                       p--;
19391 +               }
19392 +       }
19393 +       au_update_ibrange(inode, /*do_put_zero*/0);
19394 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
19395 +       e = au_dy_irefresh(inode);
19396 +       if (unlikely(e && !err))
19397 +               err = e;
19398 +
19399 +out:
19400 +       AuTraceErr(err);
19401 +       return err;
19402 +}
19403 +
19404 +void au_refresh_iop(struct inode *inode, int force_getattr)
19405 +{
19406 +       int type;
19407 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19408 +       const struct inode_operations *iop
19409 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
19410 +
19411 +       if (inode->i_op == iop)
19412 +               return;
19413 +
19414 +       switch (inode->i_mode & S_IFMT) {
19415 +       case S_IFDIR:
19416 +               type = AuIop_DIR;
19417 +               break;
19418 +       case S_IFLNK:
19419 +               type = AuIop_SYMLINK;
19420 +               break;
19421 +       default:
19422 +               type = AuIop_OTHER;
19423 +               break;
19424 +       }
19425 +
19426 +       inode->i_op = iop + type;
19427 +       /* unnecessary smp_wmb() */
19428 +}
19429 +
19430 +int au_refresh_hinode_self(struct inode *inode)
19431 +{
19432 +       int err, update;
19433 +
19434 +       err = au_ii_refresh(inode, &update);
19435 +       if (!err)
19436 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19437 +
19438 +       AuTraceErr(err);
19439 +       return err;
19440 +}
19441 +
19442 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19443 +{
19444 +       int err, e, update;
19445 +       unsigned int flags;
19446 +       umode_t mode;
19447 +       aufs_bindex_t bindex, bbot;
19448 +       unsigned char isdir;
19449 +       struct au_hinode *p;
19450 +       struct au_iinfo *iinfo;
19451 +
19452 +       err = au_ii_refresh(inode, &update);
19453 +       if (unlikely(err))
19454 +               goto out;
19455 +
19456 +       update = 0;
19457 +       iinfo = au_ii(inode);
19458 +       p = au_hinode(iinfo, iinfo->ii_btop);
19459 +       mode = (inode->i_mode & S_IFMT);
19460 +       isdir = S_ISDIR(mode);
19461 +       flags = au_hi_flags(inode, isdir);
19462 +       bbot = au_dbbot(dentry);
19463 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
19464 +               struct inode *h_i, *h_inode;
19465 +               struct dentry *h_d;
19466 +
19467 +               h_d = au_h_dptr(dentry, bindex);
19468 +               if (!h_d || d_is_negative(h_d))
19469 +                       continue;
19470 +
19471 +               h_inode = d_inode(h_d);
19472 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
19473 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
19474 +                       h_i = au_h_iptr(inode, bindex);
19475 +                       if (h_i) {
19476 +                               if (h_i == h_inode)
19477 +                                       continue;
19478 +                               err = -EIO;
19479 +                               break;
19480 +                       }
19481 +               }
19482 +               if (bindex < iinfo->ii_btop)
19483 +                       iinfo->ii_btop = bindex;
19484 +               if (iinfo->ii_bbot < bindex)
19485 +                       iinfo->ii_bbot = bindex;
19486 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
19487 +               update = 1;
19488 +       }
19489 +       au_update_ibrange(inode, /*do_put_zero*/0);
19490 +       e = au_dy_irefresh(inode);
19491 +       if (unlikely(e && !err))
19492 +               err = e;
19493 +       if (!err)
19494 +               au_refresh_hinode_attr(inode, update && isdir);
19495 +
19496 +out:
19497 +       AuTraceErr(err);
19498 +       return err;
19499 +}
19500 +
19501 +static int set_inode(struct inode *inode, struct dentry *dentry)
19502 +{
19503 +       int err;
19504 +       unsigned int flags;
19505 +       umode_t mode;
19506 +       aufs_bindex_t bindex, btop, btail;
19507 +       unsigned char isdir;
19508 +       struct dentry *h_dentry;
19509 +       struct inode *h_inode;
19510 +       struct au_iinfo *iinfo;
19511 +       const struct inode_operations *iop;
19512 +
19513 +       IiMustWriteLock(inode);
19514 +
19515 +       err = 0;
19516 +       isdir = 0;
19517 +       iop = au_sbi(inode->i_sb)->si_iop_array;
19518 +       btop = au_dbtop(dentry);
19519 +       h_dentry = au_h_dptr(dentry, btop);
19520 +       h_inode = d_inode(h_dentry);
19521 +       mode = h_inode->i_mode;
19522 +       switch (mode & S_IFMT) {
19523 +       case S_IFREG:
19524 +               btail = au_dbtail(dentry);
19525 +               inode->i_op = iop + AuIop_OTHER;
19526 +               inode->i_fop = &aufs_file_fop;
19527 +               err = au_dy_iaop(inode, btop, h_inode);
19528 +               if (unlikely(err))
19529 +                       goto out;
19530 +               break;
19531 +       case S_IFDIR:
19532 +               isdir = 1;
19533 +               btail = au_dbtaildir(dentry);
19534 +               inode->i_op = iop + AuIop_DIR;
19535 +               inode->i_fop = &aufs_dir_fop;
19536 +               break;
19537 +       case S_IFLNK:
19538 +               btail = au_dbtail(dentry);
19539 +               inode->i_op = iop + AuIop_SYMLINK;
19540 +               break;
19541 +       case S_IFBLK:
19542 +       case S_IFCHR:
19543 +       case S_IFIFO:
19544 +       case S_IFSOCK:
19545 +               btail = au_dbtail(dentry);
19546 +               inode->i_op = iop + AuIop_OTHER;
19547 +               init_special_inode(inode, mode, h_inode->i_rdev);
19548 +               break;
19549 +       default:
19550 +               AuIOErr("Unknown file type 0%o\n", mode);
19551 +               err = -EIO;
19552 +               goto out;
19553 +       }
19554 +
19555 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
19556 +       flags = au_hi_flags(inode, isdir);
19557 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19558 +           && au_ftest_hi(flags, HNOTIFY)
19559 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
19560 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19561 +               au_fclr_hi(flags, HNOTIFY);
19562 +       iinfo = au_ii(inode);
19563 +       iinfo->ii_btop = btop;
19564 +       iinfo->ii_bbot = btail;
19565 +       for (bindex = btop; bindex <= btail; bindex++) {
19566 +               h_dentry = au_h_dptr(dentry, bindex);
19567 +               if (h_dentry)
19568 +                       au_set_h_iptr(inode, bindex,
19569 +                                     au_igrab(d_inode(h_dentry)), flags);
19570 +       }
19571 +       au_cpup_attr_all(inode, /*force*/1);
19572 +       /*
19573 +        * to force calling aufs_get_inode_acl() every time,
19574 +        * do not call cache_no_acl() for aufs inode.
19575 +        */
19576 +
19577 +out:
19578 +       return err;
19579 +}
19580 +
19581 +/*
19582 + * successful returns with iinfo write_locked
19583 + * minus: errno
19584 + * zero: success, matched
19585 + * plus: no error, but unmatched
19586 + */
19587 +static int reval_inode(struct inode *inode, struct dentry *dentry)
19588 +{
19589 +       int err;
19590 +       unsigned int gen, igflags;
19591 +       aufs_bindex_t bindex, bbot;
19592 +       struct inode *h_inode, *h_dinode;
19593 +       struct dentry *h_dentry;
19594 +
19595 +       /*
19596 +        * before this function, if aufs got any iinfo lock, it must be only
19597 +        * one, the parent dir.
19598 +        * it can happen by UDBA and the obsoleted inode number.
19599 +        */
19600 +       err = -EIO;
19601 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
19602 +               goto out;
19603 +
19604 +       err = 1;
19605 +       ii_write_lock_new_child(inode);
19606 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
19607 +       h_dinode = d_inode(h_dentry);
19608 +       bbot = au_ibbot(inode);
19609 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
19610 +               h_inode = au_h_iptr(inode, bindex);
19611 +               if (!h_inode || h_inode != h_dinode)
19612 +                       continue;
19613 +
19614 +               err = 0;
19615 +               gen = au_iigen(inode, &igflags);
19616 +               if (gen == au_digen(dentry)
19617 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
19618 +                       break;
19619 +
19620 +               /* fully refresh inode using dentry */
19621 +               err = au_refresh_hinode(inode, dentry);
19622 +               if (!err)
19623 +                       au_update_iigen(inode, /*half*/0);
19624 +               break;
19625 +       }
19626 +
19627 +       if (unlikely(err))
19628 +               ii_write_unlock(inode);
19629 +out:
19630 +       return err;
19631 +}
19632 +
19633 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19634 +          unsigned int d_type, ino_t *ino)
19635 +{
19636 +       int err, idx;
19637 +       const int isnondir = d_type != DT_DIR;
19638 +
19639 +       /* prevent hardlinked inode number from race condition */
19640 +       if (isnondir) {
19641 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19642 +               if (unlikely(err))
19643 +                       goto out;
19644 +       }
19645 +
19646 +       err = au_xino_read(sb, bindex, h_ino, ino);
19647 +       if (unlikely(err))
19648 +               goto out_xinondir;
19649 +
19650 +       if (!*ino) {
19651 +               err = -EIO;
19652 +               *ino = au_xino_new_ino(sb);
19653 +               if (unlikely(!*ino))
19654 +                       goto out_xinondir;
19655 +               err = au_xino_write(sb, bindex, h_ino, *ino);
19656 +               if (unlikely(err))
19657 +                       goto out_xinondir;
19658 +       }
19659 +
19660 +out_xinondir:
19661 +       if (isnondir && idx >= 0)
19662 +               au_xinondir_leave(sb, bindex, h_ino, idx);
19663 +out:
19664 +       return err;
19665 +}
19666 +
19667 +/* successful returns with iinfo write_locked */
19668 +/* todo: return with unlocked? */
19669 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
19670 +{
19671 +       struct inode *inode, *h_inode;
19672 +       struct dentry *h_dentry;
19673 +       struct super_block *sb;
19674 +       ino_t h_ino, ino;
19675 +       int err, idx, hlinked;
19676 +       aufs_bindex_t btop;
19677 +
19678 +       sb = dentry->d_sb;
19679 +       btop = au_dbtop(dentry);
19680 +       h_dentry = au_h_dptr(dentry, btop);
19681 +       h_inode = d_inode(h_dentry);
19682 +       h_ino = h_inode->i_ino;
19683 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
19684 +
19685 +new_ino:
19686 +       /*
19687 +        * stop 'race'-ing between hardlinks under different
19688 +        * parents.
19689 +        */
19690 +       if (hlinked) {
19691 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
19692 +               inode = ERR_PTR(err);
19693 +               if (unlikely(err))
19694 +                       goto out;
19695 +       }
19696 +
19697 +       err = au_xino_read(sb, btop, h_ino, &ino);
19698 +       inode = ERR_PTR(err);
19699 +       if (unlikely(err))
19700 +               goto out_xinondir;
19701 +
19702 +       if (!ino) {
19703 +               ino = au_xino_new_ino(sb);
19704 +               if (unlikely(!ino)) {
19705 +                       inode = ERR_PTR(-EIO);
19706 +                       goto out_xinondir;
19707 +               }
19708 +       }
19709 +
19710 +       AuDbg("i%lu\n", (unsigned long)ino);
19711 +       inode = au_iget_locked(sb, ino);
19712 +       err = PTR_ERR(inode);
19713 +       if (IS_ERR(inode))
19714 +               goto out_xinondir;
19715 +
19716 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19717 +       if (inode->i_state & I_NEW) {
19718 +               ii_write_lock_new_child(inode);
19719 +               err = set_inode(inode, dentry);
19720 +               if (!err) {
19721 +                       unlock_new_inode(inode);
19722 +                       goto out_xinondir; /* success */
19723 +               }
19724 +
19725 +               /*
19726 +                * iget_failed() calls iput(), but we need to call
19727 +                * ii_write_unlock() after iget_failed(). so dirty hack for
19728 +                * i_count.
19729 +                */
19730 +               atomic_inc(&inode->i_count);
19731 +               iget_failed(inode);
19732 +               ii_write_unlock(inode);
19733 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
19734 +               /* ignore this error */
19735 +               goto out_iput;
19736 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
19737 +               /*
19738 +                * horrible race condition between lookup, readdir and copyup
19739 +                * (or something).
19740 +                */
19741 +               if (hlinked && idx >= 0)
19742 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19743 +               err = reval_inode(inode, dentry);
19744 +               if (unlikely(err < 0)) {
19745 +                       hlinked = 0;
19746 +                       goto out_iput;
19747 +               }
19748 +               if (!err)
19749 +                       goto out; /* success */
19750 +               else if (hlinked && idx >= 0) {
19751 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
19752 +                       if (unlikely(err)) {
19753 +                               iput(inode);
19754 +                               inode = ERR_PTR(err);
19755 +                               goto out;
19756 +                       }
19757 +               }
19758 +       }
19759 +
19760 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
19761 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
19762 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
19763 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
19764 +                       (unsigned long)h_ino, (unsigned long)ino);
19765 +       ino = 0;
19766 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
19767 +       if (!err) {
19768 +               iput(inode);
19769 +               if (hlinked && idx >= 0)
19770 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19771 +               goto new_ino;
19772 +       }
19773 +
19774 +out_iput:
19775 +       iput(inode);
19776 +       inode = ERR_PTR(err);
19777 +out_xinondir:
19778 +       if (hlinked && idx >= 0)
19779 +               au_xinondir_leave(sb, btop, h_ino, idx);
19780 +out:
19781 +       return inode;
19782 +}
19783 +
19784 +/* ---------------------------------------------------------------------- */
19785 +
19786 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19787 +              struct inode *inode)
19788 +{
19789 +       int err;
19790 +       struct inode *hi;
19791 +
19792 +       err = au_br_rdonly(au_sbr(sb, bindex));
19793 +
19794 +       /* pseudo-link after flushed may happen out of bounds */
19795 +       if (!err
19796 +           && inode
19797 +           && au_ibtop(inode) <= bindex
19798 +           && bindex <= au_ibbot(inode)) {
19799 +               /*
19800 +                * permission check is unnecessary since vfsub routine
19801 +                * will be called later
19802 +                */
19803 +               hi = au_h_iptr(inode, bindex);
19804 +               if (hi)
19805 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
19806 +       }
19807 +
19808 +       return err;
19809 +}
19810 +
19811 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19812 +                  int mask)
19813 +{
19814 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
19815 +               return 0;
19816 +       return inode_permission(h_idmap, h_inode, mask);
19817 +}
19818 +
19819 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19820 +                      int mask)
19821 +{
19822 +       if (au_test_nfs(h_inode->i_sb)
19823 +           && (mask & MAY_WRITE)
19824 +           && S_ISDIR(h_inode->i_mode))
19825 +               mask |= MAY_READ; /* force permission check */
19826 +       return au_test_h_perm(h_idmap, h_inode, mask);
19827 +}
19828 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
19829 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
19830 +++ linux/fs/aufs/inode.h       2023-08-28 12:34:39.956636132 +0200
19831 @@ -0,0 +1,707 @@
19832 +/* SPDX-License-Identifier: GPL-2.0 */
19833 +/*
19834 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19835 + *
19836 + * This program is free software; you can redistribute it and/or modify
19837 + * it under the terms of the GNU General Public License as published by
19838 + * the Free Software Foundation; either version 2 of the License, or
19839 + * (at your option) any later version.
19840 + *
19841 + * This program is distributed in the hope that it will be useful,
19842 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19843 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19844 + * GNU General Public License for more details.
19845 + *
19846 + * You should have received a copy of the GNU General Public License
19847 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19848 + */
19849 +
19850 +/*
19851 + * inode operations
19852 + */
19853 +
19854 +#ifndef __AUFS_INODE_H__
19855 +#define __AUFS_INODE_H__
19856 +
19857 +#ifdef __KERNEL__
19858 +
19859 +#include <linux/fsnotify.h>
19860 +#include "rwsem.h"
19861 +
19862 +struct vfsmount;
19863 +
19864 +struct au_hnotify {
19865 +#ifdef CONFIG_AUFS_HNOTIFY
19866 +#ifdef CONFIG_AUFS_HFSNOTIFY
19867 +       /* never use fsnotify_add_vfsmount_mark() */
19868 +       struct fsnotify_mark            hn_mark;
19869 +#endif
19870 +       struct inode            *hn_aufs_inode; /* no get/put */
19871 +       struct rcu_head         rcu;
19872 +#endif
19873 +} ____cacheline_aligned_in_smp;
19874 +
19875 +struct au_hinode {
19876 +       struct inode            *hi_inode;
19877 +       aufs_bindex_t           hi_id;
19878 +#ifdef CONFIG_AUFS_HNOTIFY
19879 +       struct au_hnotify       *hi_notify;
19880 +#endif
19881 +
19882 +       /* reference to the copied-up whiteout with get/put */
19883 +       struct dentry           *hi_whdentry;
19884 +};
19885 +
19886 +/* ig_flags */
19887 +#define AuIG_HALF_REFRESHED            1
19888 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
19889 +#define au_ig_fset(flags, name) \
19890 +       do { (flags) |= AuIG_##name; } while (0)
19891 +#define au_ig_fclr(flags, name) \
19892 +       do { (flags) &= ~AuIG_##name; } while (0)
19893 +
19894 +struct au_iigen {
19895 +       spinlock_t      ig_spin;
19896 +       __u32           ig_generation, ig_flags;
19897 +};
19898 +
19899 +struct au_vdir;
19900 +struct au_iinfo {
19901 +       struct au_iigen         ii_generation;
19902 +       struct super_block      *ii_hsb1;       /* no get/put */
19903 +
19904 +       struct au_rwsem         ii_rwsem;
19905 +       aufs_bindex_t           ii_btop, ii_bbot;
19906 +       __u32                   ii_higen;
19907 +       struct au_hinode        *ii_hinode;
19908 +       struct au_vdir          *ii_vdir;
19909 +};
19910 +
19911 +struct au_icntnr {
19912 +       struct au_iinfo         iinfo;
19913 +       struct inode            vfs_inode;
19914 +       struct hlist_bl_node    plink;
19915 +       struct rcu_head         rcu;
19916 +} ____cacheline_aligned_in_smp;
19917 +
19918 +/* au_pin flags */
19919 +#define AuPin_DI_LOCKED                1
19920 +#define AuPin_MNT_WRITE                (1 << 1)
19921 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
19922 +#define au_fset_pin(flags, name) \
19923 +       do { (flags) |= AuPin_##name; } while (0)
19924 +#define au_fclr_pin(flags, name) \
19925 +       do { (flags) &= ~AuPin_##name; } while (0)
19926 +
19927 +struct au_pin {
19928 +       /* input */
19929 +       struct dentry *dentry;
19930 +       unsigned int udba;
19931 +       unsigned char lsc_di, lsc_hi, flags;
19932 +       aufs_bindex_t bindex;
19933 +
19934 +       /* output */
19935 +       struct dentry *parent;
19936 +       struct au_hinode *hdir;
19937 +       struct vfsmount *h_mnt;
19938 +
19939 +       /* temporary unlock/relock for copyup */
19940 +       struct dentry *h_dentry, *h_parent;
19941 +       struct au_branch *br;
19942 +       struct task_struct *task;
19943 +};
19944 +
19945 +void au_pin_hdir_unlock(struct au_pin *p);
19946 +int au_pin_hdir_lock(struct au_pin *p);
19947 +int au_pin_hdir_relock(struct au_pin *p);
19948 +void au_pin_hdir_acquire_nest(struct au_pin *p);
19949 +void au_pin_hdir_release(struct au_pin *p);
19950 +
19951 +/* ---------------------------------------------------------------------- */
19952 +
19953 +static inline struct au_iinfo *au_ii(struct inode *inode)
19954 +{
19955 +       BUG_ON(is_bad_inode(inode));
19956 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19957 +}
19958 +
19959 +/* ---------------------------------------------------------------------- */
19960 +
19961 +/* inode.c */
19962 +struct inode *au_igrab(struct inode *inode);
19963 +void au_refresh_iop(struct inode *inode, int force_getattr);
19964 +int au_refresh_hinode_self(struct inode *inode);
19965 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19966 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19967 +          unsigned int d_type, ino_t *ino);
19968 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
19969 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19970 +              struct inode *inode);
19971 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19972 +                  int mask);
19973 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19974 +                      int mask);
19975 +
19976 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19977 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
19978 +{
19979 +#ifdef CONFIG_AUFS_SHWH
19980 +       return au_ino(sb, bindex, h_ino, d_type, ino);
19981 +#else
19982 +       return 0;
19983 +#endif
19984 +}
19985 +
19986 +/* i_op.c */
19987 +enum {
19988 +       AuIop_SYMLINK,
19989 +       AuIop_DIR,
19990 +       AuIop_OTHER,
19991 +       AuIop_Last
19992 +};
19993 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
19994 +       aufs_iop_nogetattr[AuIop_Last];
19995 +
19996 +/* au_wr_dir flags */
19997 +#define AuWrDir_ADD_ENTRY      1
19998 +#define AuWrDir_ISDIR          (1 << 1)
19999 +#define AuWrDir_TMPFILE                (1 << 2)
20000 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
20001 +#define au_fset_wrdir(flags, name) \
20002 +       do { (flags) |= AuWrDir_##name; } while (0)
20003 +#define au_fclr_wrdir(flags, name) \
20004 +       do { (flags) &= ~AuWrDir_##name; } while (0)
20005 +
20006 +struct au_wr_dir_args {
20007 +       aufs_bindex_t force_btgt;
20008 +       unsigned char flags;
20009 +};
20010 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20011 +             struct au_wr_dir_args *args);
20012 +
20013 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
20014 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
20015 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
20016 +                unsigned int udba, unsigned char flags);
20017 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
20018 +          unsigned int udba, unsigned char flags) __must_check;
20019 +int au_do_pin(struct au_pin *pin) __must_check;
20020 +void au_unpin(struct au_pin *pin);
20021 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
20022 +
20023 +#define AuIcpup_DID_CPUP       1
20024 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
20025 +#define au_fset_icpup(flags, name) \
20026 +       do { (flags) |= AuIcpup_##name; } while (0)
20027 +#define au_fclr_icpup(flags, name) \
20028 +       do { (flags) &= ~AuIcpup_##name; } while (0)
20029 +
20030 +struct au_icpup_args {
20031 +       unsigned char flags;
20032 +       unsigned char pin_flags;
20033 +       aufs_bindex_t btgt;
20034 +       unsigned int udba;
20035 +       struct au_pin pin;
20036 +       struct path h_path;
20037 +       struct inode *h_inode;
20038 +};
20039 +
20040 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20041 +                    struct au_icpup_args *a);
20042 +
20043 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20044 +                     struct path *h_path, int locked);
20045 +
20046 +/* i_op_add.c */
20047 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20048 +              struct dentry *h_parent, int isdir);
20049 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
20050 +              struct dentry *dentry, umode_t mode, dev_t dev);
20051 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
20052 +                struct dentry *dentry, const char *symname);
20053 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
20054 +               struct dentry *dentry, umode_t mode, bool want_excl);
20055 +struct vfsub_aopen_args;
20056 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20057 +                      struct vfsub_aopen_args *args);
20058 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
20059 +                struct file *file, umode_t mode);
20060 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20061 +             struct dentry *dentry);
20062 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
20063 +              struct dentry *dentry, umode_t mode);
20064 +
20065 +/* i_op_del.c */
20066 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20067 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20068 +              struct dentry *h_parent, int isdir);
20069 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
20070 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
20071 +
20072 +/* i_op_ren.c */
20073 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
20074 +int aufs_rename(struct mnt_idmap *idmap,
20075 +               struct inode *_src_dir, struct dentry *_src_dentry,
20076 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
20077 +               unsigned int _flags);
20078 +
20079 +/* iinfo.c */
20080 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20081 +void au_hiput(struct au_hinode *hinode);
20082 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20083 +                 struct dentry *h_wh);
20084 +unsigned int au_hi_flags(struct inode *inode, int isdir);
20085 +
20086 +/* hinode flags */
20087 +#define AuHi_XINO      1
20088 +#define AuHi_HNOTIFY   (1 << 1)
20089 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
20090 +#define au_fset_hi(flags, name) \
20091 +       do { (flags) |= AuHi_##name; } while (0)
20092 +#define au_fclr_hi(flags, name) \
20093 +       do { (flags) &= ~AuHi_##name; } while (0)
20094 +
20095 +#ifndef CONFIG_AUFS_HNOTIFY
20096 +#undef AuHi_HNOTIFY
20097 +#define AuHi_HNOTIFY   0
20098 +#endif
20099 +
20100 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20101 +                  struct inode *h_inode, unsigned int flags);
20102 +
20103 +void au_update_iigen(struct inode *inode, int half);
20104 +void au_update_ibrange(struct inode *inode, int do_put_zero);
20105 +
20106 +void au_icntnr_init_once(void *_c);
20107 +void au_hinode_init(struct au_hinode *hinode);
20108 +int au_iinfo_init(struct inode *inode);
20109 +void au_iinfo_fin(struct inode *inode);
20110 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
20111 +
20112 +#ifdef CONFIG_PROC_FS
20113 +/* plink.c */
20114 +int au_plink_maint(struct super_block *sb, int flags);
20115 +struct au_sbinfo;
20116 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20117 +int au_plink_maint_enter(struct super_block *sb);
20118 +#ifdef CONFIG_AUFS_DEBUG
20119 +void au_plink_list(struct super_block *sb);
20120 +#else
20121 +AuStubVoid(au_plink_list, struct super_block *sb)
20122 +#endif
20123 +int au_plink_test(struct inode *inode);
20124 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20125 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20126 +                    struct dentry *h_dentry);
20127 +void au_plink_put(struct super_block *sb, int verbose);
20128 +void au_plink_clean(struct super_block *sb, int verbose);
20129 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
20130 +#else
20131 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20132 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20133 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20134 +AuStubVoid(au_plink_list, struct super_block *sb);
20135 +AuStubInt0(au_plink_test, struct inode *inode);
20136 +AuStub(struct dentry *, au_plink_lkup, return NULL,
20137 +       struct inode *inode, aufs_bindex_t bindex);
20138 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20139 +          struct dentry *h_dentry);
20140 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20141 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20142 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20143 +#endif /* CONFIG_PROC_FS */
20144 +
20145 +#ifdef CONFIG_AUFS_XATTR
20146 +/* xattr.c */
20147 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
20148 +                 unsigned int verbose);
20149 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
20150 +void au_xattr_init(struct super_block *sb);
20151 +#else
20152 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
20153 +          int ignore_flags, unsigned int verbose);
20154 +AuStubVoid(au_xattr_init, struct super_block *sb);
20155 +#endif
20156 +
20157 +#ifdef CONFIG_FS_POSIX_ACL
20158 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu);
20159 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
20160 +                              struct dentry *dentry, int type);
20161 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
20162 +                struct posix_acl *acl, int type);
20163 +#endif
20164 +
20165 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20166 +enum {
20167 +       AU_XATTR_SET,
20168 +       AU_ACL_SET
20169 +};
20170 +
20171 +struct au_sxattr {
20172 +       int type;
20173 +       union {
20174 +               struct {
20175 +                       const char      *name;
20176 +                       const void      *value;
20177 +                       size_t          size;
20178 +                       int             flags;
20179 +               } set;
20180 +               struct {
20181 +                       struct posix_acl *acl;
20182 +                       int             type;
20183 +               } acl_set;
20184 +       } u;
20185 +};
20186 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20187 +                 struct au_sxattr *arg);
20188 +#endif
20189 +
20190 +/* ---------------------------------------------------------------------- */
20191 +
20192 +/* lock subclass for iinfo */
20193 +enum {
20194 +       AuLsc_II_CHILD,         /* child first */
20195 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
20196 +       AuLsc_II_CHILD3,        /* copyup dirs */
20197 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
20198 +       AuLsc_II_PARENT2,
20199 +       AuLsc_II_PARENT3,       /* copyup dirs */
20200 +       AuLsc_II_NEW_CHILD
20201 +};
20202 +
20203 +/*
20204 + * ii_read_lock_child, ii_write_lock_child,
20205 + * ii_read_lock_child2, ii_write_lock_child2,
20206 + * ii_read_lock_child3, ii_write_lock_child3,
20207 + * ii_read_lock_parent, ii_write_lock_parent,
20208 + * ii_read_lock_parent2, ii_write_lock_parent2,
20209 + * ii_read_lock_parent3, ii_write_lock_parent3,
20210 + * ii_read_lock_new_child, ii_write_lock_new_child,
20211 + */
20212 +#define AuReadLockFunc(name, lsc) \
20213 +static inline void ii_read_lock_##name(struct inode *i) \
20214 +{ \
20215 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20216 +}
20217 +
20218 +#define AuWriteLockFunc(name, lsc) \
20219 +static inline void ii_write_lock_##name(struct inode *i) \
20220 +{ \
20221 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20222 +}
20223 +
20224 +#define AuRWLockFuncs(name, lsc) \
20225 +       AuReadLockFunc(name, lsc) \
20226 +       AuWriteLockFunc(name, lsc)
20227 +
20228 +AuRWLockFuncs(child, CHILD);
20229 +AuRWLockFuncs(child2, CHILD2);
20230 +AuRWLockFuncs(child3, CHILD3);
20231 +AuRWLockFuncs(parent, PARENT);
20232 +AuRWLockFuncs(parent2, PARENT2);
20233 +AuRWLockFuncs(parent3, PARENT3);
20234 +AuRWLockFuncs(new_child, NEW_CHILD);
20235 +
20236 +#undef AuReadLockFunc
20237 +#undef AuWriteLockFunc
20238 +#undef AuRWLockFuncs
20239 +
20240 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20241 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20242 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
20243 +
20244 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20245 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20246 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
20247 +
20248 +/* ---------------------------------------------------------------------- */
20249 +
20250 +static inline void au_icntnr_init(struct au_icntnr *c)
20251 +{
20252 +#ifdef CONFIG_AUFS_DEBUG
20253 +       c->vfs_inode.i_mode = 0;
20254 +#endif
20255 +}
20256 +
20257 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
20258 +{
20259 +       unsigned int gen;
20260 +       struct au_iinfo *iinfo;
20261 +       struct au_iigen *iigen;
20262 +
20263 +       iinfo = au_ii(inode);
20264 +       iigen = &iinfo->ii_generation;
20265 +       spin_lock(&iigen->ig_spin);
20266 +       if (igflags)
20267 +               *igflags = iigen->ig_flags;
20268 +       gen = iigen->ig_generation;
20269 +       spin_unlock(&iigen->ig_spin);
20270 +
20271 +       return gen;
20272 +}
20273 +
20274 +/* tiny test for inode number */
20275 +/* tmpfs generation is too rough */
20276 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20277 +{
20278 +       struct au_iinfo *iinfo;
20279 +
20280 +       iinfo = au_ii(inode);
20281 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
20282 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
20283 +                && iinfo->ii_higen == h_inode->i_generation);
20284 +}
20285 +
20286 +static inline void au_iigen_dec(struct inode *inode)
20287 +{
20288 +       struct au_iinfo *iinfo;
20289 +       struct au_iigen *iigen;
20290 +
20291 +       iinfo = au_ii(inode);
20292 +       iigen = &iinfo->ii_generation;
20293 +       spin_lock(&iigen->ig_spin);
20294 +       iigen->ig_generation--;
20295 +       spin_unlock(&iigen->ig_spin);
20296 +}
20297 +
20298 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20299 +{
20300 +       int err;
20301 +
20302 +       err = 0;
20303 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
20304 +               err = -EIO;
20305 +
20306 +       return err;
20307 +}
20308 +
20309 +/* ---------------------------------------------------------------------- */
20310 +
20311 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20312 +                                         aufs_bindex_t bindex)
20313 +{
20314 +       return iinfo->ii_hinode + bindex;
20315 +}
20316 +
20317 +static inline int au_is_bad_inode(struct inode *inode)
20318 +{
20319 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20320 +}
20321 +
20322 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20323 +                                       aufs_bindex_t bindex)
20324 +{
20325 +       IiMustAnyLock(inode);
20326 +       return au_hinode(au_ii(inode), bindex)->hi_id;
20327 +}
20328 +
20329 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
20330 +{
20331 +       IiMustAnyLock(inode);
20332 +       return au_ii(inode)->ii_btop;
20333 +}
20334 +
20335 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
20336 +{
20337 +       IiMustAnyLock(inode);
20338 +       return au_ii(inode)->ii_bbot;
20339 +}
20340 +
20341 +static inline struct au_vdir *au_ivdir(struct inode *inode)
20342 +{
20343 +       IiMustAnyLock(inode);
20344 +       return au_ii(inode)->ii_vdir;
20345 +}
20346 +
20347 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20348 +{
20349 +       IiMustAnyLock(inode);
20350 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
20351 +}
20352 +
20353 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
20354 +{
20355 +       IiMustWriteLock(inode);
20356 +       au_ii(inode)->ii_btop = bindex;
20357 +}
20358 +
20359 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
20360 +{
20361 +       IiMustWriteLock(inode);
20362 +       au_ii(inode)->ii_bbot = bindex;
20363 +}
20364 +
20365 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20366 +{
20367 +       IiMustWriteLock(inode);
20368 +       au_ii(inode)->ii_vdir = vdir;
20369 +}
20370 +
20371 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
20372 +{
20373 +       IiMustAnyLock(inode);
20374 +       return au_hinode(au_ii(inode), bindex);
20375 +}
20376 +
20377 +/* ---------------------------------------------------------------------- */
20378 +
20379 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20380 +{
20381 +       if (pin)
20382 +               return pin->parent;
20383 +       return NULL;
20384 +}
20385 +
20386 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
20387 +{
20388 +       if (pin && pin->hdir)
20389 +               return pin->hdir->hi_inode;
20390 +       return NULL;
20391 +}
20392 +
20393 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20394 +{
20395 +       if (pin)
20396 +               return pin->hdir;
20397 +       return NULL;
20398 +}
20399 +
20400 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
20401 +{
20402 +       if (pin)
20403 +               pin->dentry = dentry;
20404 +}
20405 +
20406 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20407 +                                          unsigned char lflag)
20408 +{
20409 +       if (pin) {
20410 +               if (lflag)
20411 +                       au_fset_pin(pin->flags, DI_LOCKED);
20412 +               else
20413 +                       au_fclr_pin(pin->flags, DI_LOCKED);
20414 +       }
20415 +}
20416 +
20417 +#if 0 /* reserved */
20418 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20419 +{
20420 +       if (pin) {
20421 +               dput(pin->parent);
20422 +               pin->parent = dget(parent);
20423 +       }
20424 +}
20425 +#endif
20426 +
20427 +/* ---------------------------------------------------------------------- */
20428 +
20429 +struct au_branch;
20430 +#ifdef CONFIG_AUFS_HNOTIFY
20431 +struct au_hnotify_op {
20432 +       void (*ctl)(struct au_hinode *hinode, int do_set);
20433 +       int (*alloc)(struct au_hinode *hinode);
20434 +
20435 +       /*
20436 +        * if it returns true, the caller should free hinode->hi_notify,
20437 +        * otherwise ->free() frees it.
20438 +        */
20439 +       int (*free)(struct au_hinode *hinode,
20440 +                   struct au_hnotify *hn) __must_check;
20441 +
20442 +       void (*fin)(void);
20443 +       int (*init)(void);
20444 +
20445 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20446 +       void (*fin_br)(struct au_branch *br);
20447 +       int (*init_br)(struct au_branch *br, int perm);
20448 +};
20449 +
20450 +/* hnotify.c */
20451 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
20452 +void au_hn_free(struct au_hinode *hinode);
20453 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
20454 +void au_hn_reset(struct inode *inode, unsigned int flags);
20455 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
20456 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
20457 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20458 +int au_hnotify_init_br(struct au_branch *br, int perm);
20459 +void au_hnotify_fin_br(struct au_branch *br);
20460 +int __init au_hnotify_init(void);
20461 +void au_hnotify_fin(void);
20462 +
20463 +/* hfsnotify.c */
20464 +extern const struct au_hnotify_op au_hnotify_op;
20465 +
20466 +static inline
20467 +void au_hn_init(struct au_hinode *hinode)
20468 +{
20469 +       hinode->hi_notify = NULL;
20470 +}
20471 +
20472 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20473 +{
20474 +       return hinode->hi_notify;
20475 +}
20476 +
20477 +#else
20478 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20479 +       struct au_hinode *hinode __maybe_unused,
20480 +       struct inode *inode __maybe_unused)
20481 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
20482 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20483 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20484 +          int do_set __maybe_unused)
20485 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20486 +          unsigned int flags __maybe_unused)
20487 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20488 +          struct au_branch *br __maybe_unused,
20489 +          int perm __maybe_unused)
20490 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20491 +          int perm __maybe_unused)
20492 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
20493 +AuStubInt0(__init au_hnotify_init, void)
20494 +AuStubVoid(au_hnotify_fin, void)
20495 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20496 +#endif /* CONFIG_AUFS_HNOTIFY */
20497 +
20498 +static inline void au_hn_suspend(struct au_hinode *hdir)
20499 +{
20500 +       au_hn_ctl(hdir, /*do_set*/0);
20501 +}
20502 +
20503 +static inline void au_hn_resume(struct au_hinode *hdir)
20504 +{
20505 +       au_hn_ctl(hdir, /*do_set*/1);
20506 +}
20507 +
20508 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
20509 +{
20510 +       inode_lock(hdir->hi_inode);
20511 +       au_hn_suspend(hdir);
20512 +}
20513 +
20514 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
20515 +                                         unsigned int sc __maybe_unused)
20516 +{
20517 +       inode_lock_nested(hdir->hi_inode, sc);
20518 +       au_hn_suspend(hdir);
20519 +}
20520 +
20521 +#if 0 /* unused */
20522 +#include "vfsub.h"
20523 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20524 +                                                 unsigned int sc)
20525 +{
20526 +       inode_lock_shared_nested(hdir->hi_inode, sc);
20527 +       au_hn_suspend(hdir);
20528 +}
20529 +#endif
20530 +
20531 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
20532 +{
20533 +       au_hn_resume(hdir);
20534 +       inode_unlock(hdir->hi_inode);
20535 +}
20536 +
20537 +#endif /* __KERNEL__ */
20538 +#endif /* __AUFS_INODE_H__ */
20539 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
20540 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
20541 +++ linux/fs/aufs/ioctl.c       2022-11-05 23:02:18.965889284 +0100
20542 @@ -0,0 +1,220 @@
20543 +// SPDX-License-Identifier: GPL-2.0
20544 +/*
20545 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20546 + *
20547 + * This program is free software; you can redistribute it and/or modify
20548 + * it under the terms of the GNU General Public License as published by
20549 + * the Free Software Foundation; either version 2 of the License, or
20550 + * (at your option) any later version.
20551 + *
20552 + * This program is distributed in the hope that it will be useful,
20553 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20554 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20555 + * GNU General Public License for more details.
20556 + *
20557 + * You should have received a copy of the GNU General Public License
20558 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20559 + */
20560 +
20561 +/*
20562 + * ioctl
20563 + * plink-management and readdir in userspace.
20564 + * assist the pathconf(3) wrapper library.
20565 + * move-down
20566 + * File-based Hierarchical Storage Management.
20567 + */
20568 +
20569 +#include <linux/compat.h>
20570 +#include <linux/file.h>
20571 +#include "aufs.h"
20572 +
20573 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
20574 +{
20575 +       int err, fd;
20576 +       aufs_bindex_t wbi, bindex, bbot;
20577 +       struct file *h_file;
20578 +       struct super_block *sb;
20579 +       struct dentry *root;
20580 +       struct au_branch *br;
20581 +       struct aufs_wbr_fd wbrfd = {
20582 +               .oflags = au_dir_roflags,
20583 +               .brid   = -1
20584 +       };
20585 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20586 +               | O_NOATIME | O_CLOEXEC;
20587 +
20588 +       AuDebugOn(wbrfd.oflags & ~valid);
20589 +
20590 +       if (arg) {
20591 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20592 +               if (unlikely(err)) {
20593 +                       err = -EFAULT;
20594 +                       goto out;
20595 +               }
20596 +
20597 +               err = -EINVAL;
20598 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20599 +               wbrfd.oflags |= au_dir_roflags;
20600 +               AuDbg("0%o\n", wbrfd.oflags);
20601 +               if (unlikely(wbrfd.oflags & ~valid))
20602 +                       goto out;
20603 +       }
20604 +
20605 +       fd = get_unused_fd_flags(0);
20606 +       err = fd;
20607 +       if (unlikely(fd < 0))
20608 +               goto out;
20609 +
20610 +       h_file = ERR_PTR(-EINVAL);
20611 +       wbi = 0;
20612 +       br = NULL;
20613 +       sb = path->dentry->d_sb;
20614 +       root = sb->s_root;
20615 +       aufs_read_lock(root, AuLock_IR);
20616 +       bbot = au_sbbot(sb);
20617 +       if (wbrfd.brid >= 0) {
20618 +               wbi = au_br_index(sb, wbrfd.brid);
20619 +               if (unlikely(wbi < 0 || wbi > bbot))
20620 +                       goto out_unlock;
20621 +       }
20622 +
20623 +       h_file = ERR_PTR(-ENOENT);
20624 +       br = au_sbr(sb, wbi);
20625 +       if (!au_br_writable(br->br_perm)) {
20626 +               if (arg)
20627 +                       goto out_unlock;
20628 +
20629 +               bindex = wbi + 1;
20630 +               wbi = -1;
20631 +               for (; bindex <= bbot; bindex++) {
20632 +                       br = au_sbr(sb, bindex);
20633 +                       if (au_br_writable(br->br_perm)) {
20634 +                               wbi = bindex;
20635 +                               br = au_sbr(sb, wbi);
20636 +                               break;
20637 +                       }
20638 +               }
20639 +       }
20640 +       AuDbg("wbi %d\n", wbi);
20641 +       if (wbi >= 0)
20642 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20643 +                                  /*force_wr*/0);
20644 +
20645 +out_unlock:
20646 +       aufs_read_unlock(root, AuLock_IR);
20647 +       err = PTR_ERR(h_file);
20648 +       if (IS_ERR(h_file))
20649 +               goto out_fd;
20650 +
20651 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
20652 +       fd_install(fd, h_file);
20653 +       err = fd;
20654 +       goto out; /* success */
20655 +
20656 +out_fd:
20657 +       put_unused_fd(fd);
20658 +out:
20659 +       AuTraceErr(err);
20660 +       return err;
20661 +}
20662 +
20663 +/* ---------------------------------------------------------------------- */
20664 +
20665 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20666 +{
20667 +       long err;
20668 +       struct dentry *dentry;
20669 +
20670 +       switch (cmd) {
20671 +       case AUFS_CTL_RDU:
20672 +       case AUFS_CTL_RDU_INO:
20673 +               err = au_rdu_ioctl(file, cmd, arg);
20674 +               break;
20675 +
20676 +       case AUFS_CTL_WBR_FD:
20677 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20678 +               break;
20679 +
20680 +       case AUFS_CTL_IBUSY:
20681 +               err = au_ibusy_ioctl(file, arg);
20682 +               break;
20683 +
20684 +       case AUFS_CTL_BRINFO:
20685 +               err = au_brinfo_ioctl(file, arg);
20686 +               break;
20687 +
20688 +       case AUFS_CTL_FHSM_FD:
20689 +               dentry = file->f_path.dentry;
20690 +               if (IS_ROOT(dentry))
20691 +                       err = au_fhsm_fd(dentry->d_sb, arg);
20692 +               else
20693 +                       err = -ENOTTY;
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 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20707 +{
20708 +       long err;
20709 +
20710 +       switch (cmd) {
20711 +       case AUFS_CTL_MVDOWN:
20712 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
20713 +               break;
20714 +
20715 +       case AUFS_CTL_WBR_FD:
20716 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20717 +               break;
20718 +
20719 +       default:
20720 +               /* do not call the lower */
20721 +               AuDbg("0x%x\n", cmd);
20722 +               err = -ENOTTY;
20723 +       }
20724 +
20725 +       AuTraceErr(err);
20726 +       return err;
20727 +}
20728 +
20729 +#ifdef CONFIG_COMPAT
20730 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20731 +                          unsigned long arg)
20732 +{
20733 +       long err;
20734 +
20735 +       switch (cmd) {
20736 +       case AUFS_CTL_RDU:
20737 +       case AUFS_CTL_RDU_INO:
20738 +               err = au_rdu_compat_ioctl(file, cmd, arg);
20739 +               break;
20740 +
20741 +       case AUFS_CTL_IBUSY:
20742 +               err = au_ibusy_compat_ioctl(file, arg);
20743 +               break;
20744 +
20745 +       case AUFS_CTL_BRINFO:
20746 +               err = au_brinfo_compat_ioctl(file, arg);
20747 +               break;
20748 +
20749 +       default:
20750 +               err = aufs_ioctl_dir(file, cmd, arg);
20751 +       }
20752 +
20753 +       AuTraceErr(err);
20754 +       return err;
20755 +}
20756 +
20757 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20758 +                             unsigned long arg)
20759 +{
20760 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20761 +}
20762 +#endif
20763 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
20764 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
20765 +++ linux/fs/aufs/i_op_add.c    2023-08-28 12:34:39.956636132 +0200
20766 @@ -0,0 +1,972 @@
20767 +// SPDX-License-Identifier: GPL-2.0
20768 +/*
20769 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20770 + *
20771 + * This program is free software; you can redistribute it and/or modify
20772 + * it under the terms of the GNU General Public License as published by
20773 + * the Free Software Foundation; either version 2 of the License, or
20774 + * (at your option) any later version.
20775 + *
20776 + * This program is distributed in the hope that it will be useful,
20777 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20778 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20779 + * GNU General Public License for more details.
20780 + *
20781 + * You should have received a copy of the GNU General Public License
20782 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20783 + */
20784 +
20785 +/*
20786 + * inode operations (add entry)
20787 + */
20788 +
20789 +#include <linux/iversion.h>
20790 +#include "aufs.h"
20791 +
20792 +/*
20793 + * final procedure of adding a new entry, except link(2).
20794 + * remove whiteout, instantiate, copyup the parent dir's times and size
20795 + * and update version.
20796 + * if it failed, re-create the removed whiteout.
20797 + */
20798 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20799 +                 struct dentry *wh_dentry, struct dentry *dentry)
20800 +{
20801 +       int err, rerr;
20802 +       aufs_bindex_t bwh;
20803 +       struct path h_path;
20804 +       struct super_block *sb;
20805 +       struct inode *inode, *h_dir;
20806 +       struct dentry *wh;
20807 +
20808 +       bwh = -1;
20809 +       sb = dir->i_sb;
20810 +       if (wh_dentry) {
20811 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20812 +               IMustLock(h_dir);
20813 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20814 +               bwh = au_dbwh(dentry);
20815 +               h_path.dentry = wh_dentry;
20816 +               h_path.mnt = au_sbr_mnt(sb, bindex);
20817 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20818 +                                         dentry);
20819 +               if (unlikely(err))
20820 +                       goto out;
20821 +       }
20822 +
20823 +       inode = au_new_inode(dentry, /*must_new*/1);
20824 +       if (!IS_ERR(inode)) {
20825 +               d_instantiate(dentry, inode);
20826 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
20827 +               IMustLock(dir);
20828 +               au_dir_ts(dir, bindex);
20829 +               inode_inc_iversion(dir);
20830 +               au_fhsm_wrote(sb, bindex, /*force*/0);
20831 +               return 0; /* success */
20832 +       }
20833 +
20834 +       err = PTR_ERR(inode);
20835 +       if (!wh_dentry)
20836 +               goto out;
20837 +
20838 +       /* revert */
20839 +       /* dir inode is locked */
20840 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20841 +       rerr = PTR_ERR(wh);
20842 +       if (IS_ERR(wh)) {
20843 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20844 +                       dentry, err, rerr);
20845 +               err = -EIO;
20846 +       } else
20847 +               dput(wh);
20848 +
20849 +out:
20850 +       return err;
20851 +}
20852 +
20853 +static int au_d_may_add(struct dentry *dentry)
20854 +{
20855 +       int err;
20856 +
20857 +       err = 0;
20858 +       if (unlikely(d_unhashed(dentry)))
20859 +               err = -ENOENT;
20860 +       if (unlikely(d_really_is_positive(dentry)))
20861 +               err = -EEXIST;
20862 +       return err;
20863 +}
20864 +
20865 +/*
20866 + * simple tests for the adding inode operations.
20867 + * following the checks in vfs, plus the parent-child relationship.
20868 + */
20869 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20870 +              struct dentry *h_parent, int isdir)
20871 +{
20872 +       int err;
20873 +       umode_t h_mode;
20874 +       struct dentry *h_dentry;
20875 +       struct inode *h_inode;
20876 +
20877 +       err = -ENAMETOOLONG;
20878 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20879 +               goto out;
20880 +
20881 +       h_dentry = au_h_dptr(dentry, bindex);
20882 +       if (d_really_is_negative(dentry)) {
20883 +               err = -EEXIST;
20884 +               if (unlikely(d_is_positive(h_dentry)))
20885 +                       goto out;
20886 +       } else {
20887 +               /* rename(2) case */
20888 +               err = -EIO;
20889 +               if (unlikely(d_is_negative(h_dentry)))
20890 +                       goto out;
20891 +               h_inode = d_inode(h_dentry);
20892 +               if (unlikely(!h_inode->i_nlink))
20893 +                       goto out;
20894 +
20895 +               h_mode = h_inode->i_mode;
20896 +               if (!isdir) {
20897 +                       err = -EISDIR;
20898 +                       if (unlikely(S_ISDIR(h_mode)))
20899 +                               goto out;
20900 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20901 +                       err = -ENOTDIR;
20902 +                       goto out;
20903 +               }
20904 +       }
20905 +
20906 +       err = 0;
20907 +       /* expected parent dir is locked */
20908 +       if (unlikely(h_parent != h_dentry->d_parent))
20909 +               err = -EIO;
20910 +
20911 +out:
20912 +       AuTraceErr(err);
20913 +       return err;
20914 +}
20915 +
20916 +/*
20917 + * initial procedure of adding a new entry.
20918 + * prepare writable branch and the parent dir, lock it,
20919 + * and lookup whiteout for the new entry.
20920 + */
20921 +static struct dentry*
20922 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20923 +                 struct dentry *src_dentry, struct au_pin *pin,
20924 +                 struct au_wr_dir_args *wr_dir_args)
20925 +{
20926 +       struct dentry *wh_dentry, *h_parent;
20927 +       struct super_block *sb;
20928 +       struct au_branch *br;
20929 +       int err;
20930 +       unsigned int udba;
20931 +       aufs_bindex_t bcpup;
20932 +
20933 +       AuDbg("%pd\n", dentry);
20934 +
20935 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20936 +       bcpup = err;
20937 +       wh_dentry = ERR_PTR(err);
20938 +       if (unlikely(err < 0))
20939 +               goto out;
20940 +
20941 +       sb = dentry->d_sb;
20942 +       udba = au_opt_udba(sb);
20943 +       err = au_pin(pin, dentry, bcpup, udba,
20944 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20945 +       wh_dentry = ERR_PTR(err);
20946 +       if (unlikely(err))
20947 +               goto out;
20948 +
20949 +       h_parent = au_pinned_h_parent(pin);
20950 +       if (udba != AuOpt_UDBA_NONE
20951 +           && au_dbtop(dentry) == bcpup)
20952 +               err = au_may_add(dentry, bcpup, h_parent,
20953 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20954 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20955 +               err = -ENAMETOOLONG;
20956 +       wh_dentry = ERR_PTR(err);
20957 +       if (unlikely(err))
20958 +               goto out_unpin;
20959 +
20960 +       br = au_sbr(sb, bcpup);
20961 +       if (dt) {
20962 +               struct path tmp = {
20963 +                       .dentry = h_parent,
20964 +                       .mnt    = au_br_mnt(br)
20965 +               };
20966 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20967 +       }
20968 +
20969 +       wh_dentry = NULL;
20970 +       if (bcpup != au_dbwh(dentry))
20971 +               goto out; /* success */
20972 +
20973 +       /*
20974 +        * ENAMETOOLONG here means that if we allowed create such name, then it
20975 +        * would not be able to removed in the future. So we don't allow such
20976 +        * name here and we don't handle ENAMETOOLONG differently here.
20977 +        */
20978 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20979 +
20980 +out_unpin:
20981 +       if (IS_ERR(wh_dentry))
20982 +               au_unpin(pin);
20983 +out:
20984 +       return wh_dentry;
20985 +}
20986 +
20987 +/* ---------------------------------------------------------------------- */
20988 +
20989 +enum { Mknod, Symlink, Creat };
20990 +struct simple_arg {
20991 +       int type;
20992 +       union {
20993 +               struct {
20994 +                       umode_t                 mode;
20995 +                       bool                    want_excl;
20996 +                       bool                    try_aopen;
20997 +                       struct vfsub_aopen_args *aopen;
20998 +               } c;
20999 +               struct {
21000 +                       const char *symname;
21001 +               } s;
21002 +               struct {
21003 +                       umode_t mode;
21004 +                       dev_t dev;
21005 +               } m;
21006 +       } u;
21007 +};
21008 +
21009 +static int add_simple(struct inode *dir, struct dentry *dentry,
21010 +                     struct simple_arg *arg)
21011 +{
21012 +       int err, rerr;
21013 +       aufs_bindex_t btop;
21014 +       unsigned char created;
21015 +       const unsigned char try_aopen
21016 +               = (arg->type == Creat && arg->u.c.try_aopen);
21017 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
21018 +       struct dentry *wh_dentry, *parent;
21019 +       struct inode *h_dir;
21020 +       struct super_block *sb;
21021 +       struct au_branch *br;
21022 +       /* to reduce stack size */
21023 +       struct {
21024 +               struct au_dtime dt;
21025 +               struct au_pin pin;
21026 +               struct path h_path;
21027 +               struct au_wr_dir_args wr_dir_args;
21028 +       } *a;
21029 +
21030 +       AuDbg("%pd\n", dentry);
21031 +       IMustLock(dir);
21032 +
21033 +       err = -ENOMEM;
21034 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21035 +       if (unlikely(!a))
21036 +               goto out;
21037 +       a->wr_dir_args.force_btgt = -1;
21038 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21039 +
21040 +       parent = dentry->d_parent; /* dir inode is locked */
21041 +       if (!try_aopen) {
21042 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21043 +               if (unlikely(err))
21044 +                       goto out_free;
21045 +       }
21046 +       err = au_d_may_add(dentry);
21047 +       if (unlikely(err))
21048 +               goto out_unlock;
21049 +       if (!try_aopen)
21050 +               di_write_lock_parent(parent);
21051 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21052 +                                     &a->pin, &a->wr_dir_args);
21053 +       err = PTR_ERR(wh_dentry);
21054 +       if (IS_ERR(wh_dentry))
21055 +               goto out_parent;
21056 +
21057 +       btop = au_dbtop(dentry);
21058 +       sb = dentry->d_sb;
21059 +       br = au_sbr(sb, btop);
21060 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21061 +       a->h_path.mnt = au_br_mnt(br);
21062 +       h_dir = au_pinned_h_dir(&a->pin);
21063 +       switch (arg->type) {
21064 +       case Creat:
21065 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
21066 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21067 +                                          arg->u.c.want_excl);
21068 +                       created = !err;
21069 +                       if (!err && try_aopen)
21070 +                               aopen->file->f_mode |= FMODE_CREATED;
21071 +               } else {
21072 +                       aopen->br = br;
21073 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21074 +                       AuDbg("err %d\n", err);
21075 +                       AuDbgFile(aopen->file);
21076 +                       created = err >= 0
21077 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
21078 +               }
21079 +               break;
21080 +       case Symlink:
21081 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
21082 +               created = !err;
21083 +               break;
21084 +       case Mknod:
21085 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21086 +                                 arg->u.m.dev);
21087 +               created = !err;
21088 +               break;
21089 +       default:
21090 +               BUG();
21091 +       }
21092 +       if (unlikely(err < 0))
21093 +               goto out_unpin;
21094 +
21095 +       err = epilog(dir, btop, wh_dentry, dentry);
21096 +       if (!err)
21097 +               goto out_unpin; /* success */
21098 +
21099 +       /* revert */
21100 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
21101 +               /* no delegation since it is just created */
21102 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21103 +                                   /*force*/0);
21104 +               if (rerr) {
21105 +                       AuIOErr("%pd revert failure(%d, %d)\n",
21106 +                               dentry, err, rerr);
21107 +                       err = -EIO;
21108 +               }
21109 +               au_dtime_revert(&a->dt);
21110 +       }
21111 +       if (try_aopen && h_dir->i_op->atomic_open
21112 +           && (aopen->file->f_mode & FMODE_OPENED))
21113 +               /* aopen->file is still opened */
21114 +               au_lcnt_dec(&aopen->br->br_nfiles);
21115 +
21116 +out_unpin:
21117 +       au_unpin(&a->pin);
21118 +       dput(wh_dentry);
21119 +out_parent:
21120 +       if (!try_aopen)
21121 +               di_write_unlock(parent);
21122 +out_unlock:
21123 +       if (unlikely(err)) {
21124 +               au_update_dbtop(dentry);
21125 +               d_drop(dentry);
21126 +       }
21127 +       if (!try_aopen)
21128 +               aufs_read_unlock(dentry, AuLock_DW);
21129 +out_free:
21130 +       au_kfree_rcu(a);
21131 +out:
21132 +       return err;
21133 +}
21134 +
21135 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
21136 +              struct dentry *dentry, umode_t mode, dev_t dev)
21137 +{
21138 +       struct simple_arg arg = {
21139 +               .type = Mknod,
21140 +               .u.m = {
21141 +                       .mode   = mode,
21142 +                       .dev    = dev
21143 +               }
21144 +       };
21145 +       return add_simple(dir, dentry, &arg);
21146 +}
21147 +
21148 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
21149 +                struct dentry *dentry, const char *symname)
21150 +{
21151 +       struct simple_arg arg = {
21152 +               .type = Symlink,
21153 +               .u.s.symname = symname
21154 +       };
21155 +       return add_simple(dir, dentry, &arg);
21156 +}
21157 +
21158 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
21159 +               struct dentry *dentry, umode_t mode, bool want_excl)
21160 +{
21161 +       struct simple_arg arg = {
21162 +               .type = Creat,
21163 +               .u.c = {
21164 +                       .mode           = mode,
21165 +                       .want_excl      = want_excl
21166 +               }
21167 +       };
21168 +       return add_simple(dir, dentry, &arg);
21169 +}
21170 +
21171 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21172 +                      struct vfsub_aopen_args *aopen_args)
21173 +{
21174 +       struct simple_arg arg = {
21175 +               .type = Creat,
21176 +               .u.c = {
21177 +                       .mode           = aopen_args->create_mode,
21178 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
21179 +                       .try_aopen      = true,
21180 +                       .aopen          = aopen_args
21181 +               }
21182 +       };
21183 +       return add_simple(dir, dentry, &arg);
21184 +}
21185 +
21186 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
21187 +                struct file *file, umode_t mode)
21188 +{
21189 +       int err;
21190 +       aufs_bindex_t bindex;
21191 +       struct path h_ppath;
21192 +       struct super_block *sb;
21193 +       struct au_branch *br;
21194 +       struct dentry *dentry, *parent, *h_parent, *h_dentry;
21195 +       struct inode *h_dir, *inode;
21196 +       struct vfsmount *h_mnt;
21197 +       struct mnt_idmap *h_idmap;
21198 +       struct file *h_file;
21199 +       struct au_wr_dir_args wr_dir_args = {
21200 +               .force_btgt     = -1,
21201 +               .flags          = AuWrDir_TMPFILE
21202 +       };
21203 +
21204 +       /* copy-up may happen */
21205 +       inode_lock(dir);
21206 +
21207 +       h_file = NULL;
21208 +       sb = dir->i_sb;
21209 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21210 +       if (unlikely(err))
21211 +               goto out;
21212 +
21213 +       dentry = file->f_path.dentry;
21214 +       err = au_di_init(dentry);
21215 +       if (unlikely(err))
21216 +               goto out_si;
21217 +
21218 +       err = -EBUSY;
21219 +       parent = d_find_any_alias(dir);
21220 +       AuDebugOn(!parent);
21221 +       di_write_lock_parent(parent);
21222 +       if (unlikely(d_inode(parent) != dir))
21223 +               goto out_parent;
21224 +
21225 +       err = au_digen_test(parent, au_sigen(sb));
21226 +       if (unlikely(err))
21227 +               goto out_parent;
21228 +
21229 +       bindex = au_dbtop(parent);
21230 +       au_set_dbtop(dentry, bindex);
21231 +       au_set_dbbot(dentry, bindex);
21232 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21233 +       bindex = err;
21234 +       if (unlikely(err < 0))
21235 +               goto out_parent;
21236 +
21237 +       err = -EOPNOTSUPP;
21238 +       h_dir = au_h_iptr(dir, bindex);
21239 +       if (unlikely(!h_dir->i_op->tmpfile))
21240 +               goto out_parent;
21241 +
21242 +       br = au_sbr(sb, bindex);
21243 +       h_mnt = au_br_mnt(br);
21244 +       err = vfsub_mnt_want_write(h_mnt);
21245 +       if (unlikely(err))
21246 +               goto out_parent;
21247 +
21248 +       h_idmap = mnt_idmap(h_mnt);
21249 +       h_parent = au_h_dptr(parent, bindex);
21250 +       h_ppath.mnt = h_mnt;
21251 +       h_ppath.dentry = h_parent;
21252 +       h_file = vfs_tmpfile_open(h_idmap, &h_ppath, mode, /*open_flag*/0,
21253 +                                 current_cred());
21254 +       if (IS_ERR(h_file)) {
21255 +               err = PTR_ERR(h_file);
21256 +               h_file = NULL;
21257 +               goto out_mnt;
21258 +       }
21259 +
21260 +       h_dentry = h_file->f_path.dentry;
21261 +       au_set_dbtop(dentry, bindex);
21262 +       au_set_dbbot(dentry, bindex);
21263 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
21264 +       inode = au_new_inode(dentry, /*must_new*/1);
21265 +       if (IS_ERR(inode)) {
21266 +               err = PTR_ERR(inode);
21267 +               au_set_h_dptr(dentry, bindex, NULL);
21268 +               au_set_dbtop(dentry, -1);
21269 +               au_set_dbbot(dentry, -1);
21270 +               goto out_h_file;
21271 +       }
21272 +
21273 +       if (!inode->i_nlink)
21274 +               set_nlink(inode, 1);
21275 +       d_tmpfile(file, inode);
21276 +       au_di(dentry)->di_tmpfile = 1;
21277 +       get_file(h_file);
21278 +       au_di(dentry)->di_htmpfile = h_file;
21279 +
21280 +       /* update without i_mutex */
21281 +       if (au_ibtop(dir) == au_dbtop(dentry))
21282 +               au_cpup_attr_timesizes(dir);
21283 +
21284 +out_h_file:
21285 +       fput(h_file);
21286 +out_mnt:
21287 +       vfsub_mnt_drop_write(h_mnt);
21288 +out_parent:
21289 +       di_write_unlock(parent);
21290 +       dput(parent);
21291 +       di_write_unlock(dentry);
21292 +       if (!err)
21293 +               goto out_si;
21294 +       if (h_file)
21295 +               fput(h_file);
21296 +       au_di(dentry)->di_htmpfile = NULL;
21297 +       au_di_fin(dentry);
21298 +       dentry->d_fsdata = NULL;
21299 +out_si:
21300 +       si_read_unlock(sb);
21301 +       if (!err && h_file) {
21302 +               /* finally... */
21303 +               err = finish_open_simple(file, err);
21304 +               if (!err)
21305 +                       au_lcnt_inc(&br->br_nfiles);
21306 +               else {
21307 +                       fput(h_file);
21308 +                       au_di(dentry)->di_htmpfile = NULL;
21309 +                       au_di_fin(dentry);
21310 +                       dentry->d_fsdata = NULL;
21311 +               }
21312 +       }
21313 +out:
21314 +       inode_unlock(dir);
21315 +       AuTraceErr(err);
21316 +       return err;
21317 +}
21318 +
21319 +/* ---------------------------------------------------------------------- */
21320 +
21321 +struct au_link_args {
21322 +       aufs_bindex_t bdst, bsrc;
21323 +       struct au_pin pin;
21324 +       struct path h_path;
21325 +       struct dentry *src_parent, *parent;
21326 +};
21327 +
21328 +static int au_cpup_before_link(struct dentry *src_dentry,
21329 +                              struct au_link_args *a)
21330 +{
21331 +       int err;
21332 +       struct dentry *h_src_dentry;
21333 +       struct au_cp_generic cpg = {
21334 +               .dentry = src_dentry,
21335 +               .bdst   = a->bdst,
21336 +               .bsrc   = a->bsrc,
21337 +               .len    = -1,
21338 +               .pin    = &a->pin,
21339 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21340 +       };
21341 +
21342 +       di_read_lock_parent(a->src_parent, AuLock_IR);
21343 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21344 +       if (unlikely(err))
21345 +               goto out;
21346 +
21347 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
21348 +       err = au_pin(&a->pin, src_dentry, a->bdst,
21349 +                    au_opt_udba(src_dentry->d_sb),
21350 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21351 +       if (unlikely(err))
21352 +               goto out;
21353 +
21354 +       err = au_sio_cpup_simple(&cpg);
21355 +       au_unpin(&a->pin);
21356 +
21357 +out:
21358 +       di_read_unlock(a->src_parent, AuLock_IR);
21359 +       return err;
21360 +}
21361 +
21362 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21363 +                          struct au_link_args *a)
21364 +{
21365 +       int err;
21366 +       unsigned char plink;
21367 +       aufs_bindex_t bbot;
21368 +       struct dentry *h_src_dentry;
21369 +       struct inode *h_inode, *inode, *delegated;
21370 +       struct super_block *sb;
21371 +       struct file *h_file;
21372 +
21373 +       plink = 0;
21374 +       h_inode = NULL;
21375 +       sb = src_dentry->d_sb;
21376 +       inode = d_inode(src_dentry);
21377 +       if (au_ibtop(inode) <= a->bdst)
21378 +               h_inode = au_h_iptr(inode, a->bdst);
21379 +       if (!h_inode || !h_inode->i_nlink) {
21380 +               /* copyup src_dentry as the name of dentry. */
21381 +               bbot = au_dbbot(dentry);
21382 +               if (bbot < a->bsrc)
21383 +                       au_set_dbbot(dentry, a->bsrc);
21384 +               au_set_h_dptr(dentry, a->bsrc,
21385 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
21386 +               dget(a->h_path.dentry);
21387 +               au_set_h_dptr(dentry, a->bdst, NULL);
21388 +               AuDbg("temporary d_inode...\n");
21389 +               spin_lock(&dentry->d_lock);
21390 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
21391 +               spin_unlock(&dentry->d_lock);
21392 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
21393 +               if (IS_ERR(h_file))
21394 +                       err = PTR_ERR(h_file);
21395 +               else {
21396 +                       struct au_cp_generic cpg = {
21397 +                               .dentry = dentry,
21398 +                               .bdst   = a->bdst,
21399 +                               .bsrc   = -1,
21400 +                               .len    = -1,
21401 +                               .pin    = &a->pin,
21402 +                               .flags  = AuCpup_KEEPLINO
21403 +                       };
21404 +                       err = au_sio_cpup_simple(&cpg);
21405 +                       au_h_open_post(dentry, a->bsrc, h_file);
21406 +                       if (!err) {
21407 +                               dput(a->h_path.dentry);
21408 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21409 +                       } else
21410 +                               au_set_h_dptr(dentry, a->bdst,
21411 +                                             a->h_path.dentry);
21412 +               }
21413 +               spin_lock(&dentry->d_lock);
21414 +               dentry->d_inode = NULL; /* restore */
21415 +               spin_unlock(&dentry->d_lock);
21416 +               AuDbg("temporary d_inode...done\n");
21417 +               au_set_h_dptr(dentry, a->bsrc, NULL);
21418 +               au_set_dbbot(dentry, bbot);
21419 +       } else {
21420 +               /* the inode of src_dentry already exists on a.bdst branch */
21421 +               h_src_dentry = d_find_alias(h_inode);
21422 +               if (!h_src_dentry && au_plink_test(inode)) {
21423 +                       plink = 1;
21424 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
21425 +                       err = PTR_ERR(h_src_dentry);
21426 +                       if (IS_ERR(h_src_dentry))
21427 +                               goto out;
21428 +
21429 +                       if (unlikely(d_is_negative(h_src_dentry))) {
21430 +                               dput(h_src_dentry);
21431 +                               h_src_dentry = NULL;
21432 +                       }
21433 +
21434 +               }
21435 +               if (h_src_dentry) {
21436 +                       delegated = NULL;
21437 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21438 +                                        &a->h_path, &delegated);
21439 +                       if (unlikely(err == -EWOULDBLOCK)) {
21440 +                               pr_warn("cannot retry for NFSv4 delegation"
21441 +                                       " for an internal link\n");
21442 +                               iput(delegated);
21443 +                       }
21444 +                       dput(h_src_dentry);
21445 +               } else {
21446 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
21447 +                               h_inode->i_ino, a->bdst);
21448 +                       err = -EIO;
21449 +               }
21450 +       }
21451 +
21452 +       if (!err && !plink)
21453 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
21454 +
21455 +out:
21456 +       AuTraceErr(err);
21457 +       return err;
21458 +}
21459 +
21460 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
21461 +             struct dentry *dentry)
21462 +{
21463 +       int err, rerr;
21464 +       struct au_dtime dt;
21465 +       struct au_link_args *a;
21466 +       struct dentry *wh_dentry, *h_src_dentry;
21467 +       struct inode *inode, *delegated;
21468 +       struct super_block *sb;
21469 +       struct au_wr_dir_args wr_dir_args = {
21470 +               /* .force_btgt  = -1, */
21471 +               .flags          = AuWrDir_ADD_ENTRY
21472 +       };
21473 +
21474 +       IMustLock(dir);
21475 +       inode = d_inode(src_dentry);
21476 +       IMustLock(inode);
21477 +
21478 +       err = -ENOMEM;
21479 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21480 +       if (unlikely(!a))
21481 +               goto out;
21482 +
21483 +       a->parent = dentry->d_parent; /* dir inode is locked */
21484 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
21485 +                                       AuLock_NOPLM | AuLock_GEN);
21486 +       if (unlikely(err))
21487 +               goto out_kfree;
21488 +       err = au_d_linkable(src_dentry);
21489 +       if (unlikely(err))
21490 +               goto out_unlock;
21491 +       err = au_d_may_add(dentry);
21492 +       if (unlikely(err))
21493 +               goto out_unlock;
21494 +
21495 +       a->src_parent = dget_parent(src_dentry);
21496 +       wr_dir_args.force_btgt = au_ibtop(inode);
21497 +
21498 +       di_write_lock_parent(a->parent);
21499 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21500 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21501 +                                     &wr_dir_args);
21502 +       err = PTR_ERR(wh_dentry);
21503 +       if (IS_ERR(wh_dentry))
21504 +               goto out_parent;
21505 +
21506 +       err = 0;
21507 +       sb = dentry->d_sb;
21508 +       a->bdst = au_dbtop(dentry);
21509 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21510 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21511 +       a->bsrc = au_ibtop(inode);
21512 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21513 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21514 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21515 +       if (!h_src_dentry) {
21516 +               a->bsrc = au_dbtop(src_dentry);
21517 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21518 +               AuDebugOn(!h_src_dentry);
21519 +       } else if (IS_ERR(h_src_dentry)) {
21520 +               err = PTR_ERR(h_src_dentry);
21521 +               goto out_parent;
21522 +       }
21523 +
21524 +       /*
21525 +        * aufs doesn't touch the credential so
21526 +        * security_dentry_create_files_as() is unnecessary.
21527 +        */
21528 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21529 +               if (a->bdst < a->bsrc
21530 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21531 +                       err = au_cpup_or_link(src_dentry, dentry, a);
21532 +               else {
21533 +                       delegated = NULL;
21534 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21535 +                                        &a->h_path, &delegated);
21536 +                       if (unlikely(err == -EWOULDBLOCK)) {
21537 +                               pr_warn("cannot retry for NFSv4 delegation"
21538 +                                       " for an internal link\n");
21539 +                               iput(delegated);
21540 +                       }
21541 +               }
21542 +               dput(h_src_dentry);
21543 +       } else {
21544 +               /*
21545 +                * copyup src_dentry to the branch we process,
21546 +                * and then link(2) to it.
21547 +                */
21548 +               dput(h_src_dentry);
21549 +               if (a->bdst < a->bsrc
21550 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21551 +                       au_unpin(&a->pin);
21552 +                       di_write_unlock(a->parent);
21553 +                       err = au_cpup_before_link(src_dentry, a);
21554 +                       di_write_lock_parent(a->parent);
21555 +                       if (!err)
21556 +                               err = au_pin(&a->pin, dentry, a->bdst,
21557 +                                            au_opt_udba(sb),
21558 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21559 +                       if (unlikely(err))
21560 +                               goto out_wh;
21561 +               }
21562 +               if (!err) {
21563 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21564 +                       err = -ENOENT;
21565 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
21566 +                               delegated = NULL;
21567 +                               err = vfsub_link(h_src_dentry,
21568 +                                                au_pinned_h_dir(&a->pin),
21569 +                                                &a->h_path, &delegated);
21570 +                               if (unlikely(err == -EWOULDBLOCK)) {
21571 +                                       pr_warn("cannot retry"
21572 +                                               " for NFSv4 delegation"
21573 +                                               " for an internal link\n");
21574 +                                       iput(delegated);
21575 +                               }
21576 +                       }
21577 +               }
21578 +       }
21579 +       if (unlikely(err))
21580 +               goto out_unpin;
21581 +
21582 +       if (wh_dentry) {
21583 +               a->h_path.dentry = wh_dentry;
21584 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21585 +                                         dentry);
21586 +               if (unlikely(err))
21587 +                       goto out_revert;
21588 +       }
21589 +
21590 +       au_dir_ts(dir, a->bdst);
21591 +       inode_inc_iversion(dir);
21592 +       inc_nlink(inode);
21593 +       inode->i_ctime = dir->i_ctime;
21594 +       d_instantiate(dentry, au_igrab(inode));
21595 +       if (d_unhashed(a->h_path.dentry))
21596 +               /* some filesystem calls d_drop() */
21597 +               d_drop(dentry);
21598 +       /* some filesystems consume an inode even hardlink */
21599 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
21600 +       goto out_unpin; /* success */
21601 +
21602 +out_revert:
21603 +       /* no delegation since it is just created */
21604 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21605 +                           /*delegated*/NULL, /*force*/0);
21606 +       if (unlikely(rerr)) {
21607 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21608 +               err = -EIO;
21609 +       }
21610 +       au_dtime_revert(&dt);
21611 +out_unpin:
21612 +       au_unpin(&a->pin);
21613 +out_wh:
21614 +       dput(wh_dentry);
21615 +out_parent:
21616 +       di_write_unlock(a->parent);
21617 +       dput(a->src_parent);
21618 +out_unlock:
21619 +       if (unlikely(err)) {
21620 +               au_update_dbtop(dentry);
21621 +               d_drop(dentry);
21622 +       }
21623 +       aufs_read_and_write_unlock2(dentry, src_dentry);
21624 +out_kfree:
21625 +       au_kfree_rcu(a);
21626 +out:
21627 +       AuTraceErr(err);
21628 +       return err;
21629 +}
21630 +
21631 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
21632 +              struct dentry *dentry, umode_t mode)
21633 +{
21634 +       int err, rerr;
21635 +       aufs_bindex_t bindex;
21636 +       unsigned char diropq;
21637 +       struct path h_path;
21638 +       struct dentry *wh_dentry, *parent, *opq_dentry;
21639 +       struct inode *h_inode;
21640 +       struct super_block *sb;
21641 +       struct {
21642 +               struct au_pin pin;
21643 +               struct au_dtime dt;
21644 +       } *a; /* reduce the stack usage */
21645 +       struct au_wr_dir_args wr_dir_args = {
21646 +               .force_btgt     = -1,
21647 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21648 +       };
21649 +
21650 +       IMustLock(dir);
21651 +
21652 +       err = -ENOMEM;
21653 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21654 +       if (unlikely(!a))
21655 +               goto out;
21656 +
21657 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21658 +       if (unlikely(err))
21659 +               goto out_free;
21660 +       err = au_d_may_add(dentry);
21661 +       if (unlikely(err))
21662 +               goto out_unlock;
21663 +
21664 +       parent = dentry->d_parent; /* dir inode is locked */
21665 +       di_write_lock_parent(parent);
21666 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21667 +                                     &a->pin, &wr_dir_args);
21668 +       err = PTR_ERR(wh_dentry);
21669 +       if (IS_ERR(wh_dentry))
21670 +               goto out_parent;
21671 +
21672 +       sb = dentry->d_sb;
21673 +       bindex = au_dbtop(dentry);
21674 +       h_path.dentry = au_h_dptr(dentry, bindex);
21675 +       h_path.mnt = au_sbr_mnt(sb, bindex);
21676 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21677 +       if (unlikely(err))
21678 +               goto out_unpin;
21679 +
21680 +       /* make the dir opaque */
21681 +       diropq = 0;
21682 +       h_inode = d_inode(h_path.dentry);
21683 +       if (wh_dentry
21684 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21685 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21686 +               opq_dentry = au_diropq_create(dentry, bindex);
21687 +               inode_unlock(h_inode);
21688 +               err = PTR_ERR(opq_dentry);
21689 +               if (IS_ERR(opq_dentry))
21690 +                       goto out_dir;
21691 +               dput(opq_dentry);
21692 +               diropq = 1;
21693 +       }
21694 +
21695 +       err = epilog(dir, bindex, wh_dentry, dentry);
21696 +       if (!err) {
21697 +               inc_nlink(dir);
21698 +               goto out_unpin; /* success */
21699 +       }
21700 +
21701 +       /* revert */
21702 +       if (diropq) {
21703 +               AuLabel(revert opq);
21704 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21705 +               rerr = au_diropq_remove(dentry, bindex);
21706 +               inode_unlock(h_inode);
21707 +               if (rerr) {
21708 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21709 +                               dentry, err, rerr);
21710 +                       err = -EIO;
21711 +               }
21712 +       }
21713 +
21714 +out_dir:
21715 +       AuLabel(revert dir);
21716 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21717 +       if (rerr) {
21718 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
21719 +                       dentry, err, rerr);
21720 +               err = -EIO;
21721 +       }
21722 +       au_dtime_revert(&a->dt);
21723 +out_unpin:
21724 +       au_unpin(&a->pin);
21725 +       dput(wh_dentry);
21726 +out_parent:
21727 +       di_write_unlock(parent);
21728 +out_unlock:
21729 +       if (unlikely(err)) {
21730 +               au_update_dbtop(dentry);
21731 +               d_drop(dentry);
21732 +       }
21733 +       aufs_read_unlock(dentry, AuLock_DW);
21734 +out_free:
21735 +       au_kfree_rcu(a);
21736 +out:
21737 +       return err;
21738 +}
21739 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
21740 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
21741 +++ linux/fs/aufs/i_op.c        2023-08-28 12:34:39.956636132 +0200
21742 @@ -0,0 +1,1517 @@
21743 +// SPDX-License-Identifier: GPL-2.0
21744 +/*
21745 + * Copyright (C) 2005-2022 Junjiro R. Okajima
21746 + *
21747 + * This program is free software; you can redistribute it and/or modify
21748 + * it under the terms of the GNU General Public License as published by
21749 + * the Free Software Foundation; either version 2 of the License, or
21750 + * (at your option) any later version.
21751 + *
21752 + * This program is distributed in the hope that it will be useful,
21753 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21754 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21755 + * GNU General Public License for more details.
21756 + *
21757 + * You should have received a copy of the GNU General Public License
21758 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21759 + */
21760 +
21761 +/*
21762 + * inode operations (except add/del/rename)
21763 + */
21764 +
21765 +#include <linux/device_cgroup.h>
21766 +#include <linux/filelock.h>
21767 +#include <linux/fs_stack.h>
21768 +#include <linux/iversion.h>
21769 +#include <linux/security.h>
21770 +#include "aufs.h"
21771 +
21772 +static int h_permission(struct inode *h_inode, int mask,
21773 +                       struct path *h_path, int brperm)
21774 +{
21775 +       int err;
21776 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21777 +       struct mnt_idmap *h_idmap;
21778 +
21779 +       err = -EPERM;
21780 +       if (write_mask && IS_IMMUTABLE(h_inode))
21781 +               goto out;
21782 +
21783 +       err = -EACCES;
21784 +       if (((mask & MAY_EXEC)
21785 +            && S_ISREG(h_inode->i_mode)
21786 +            && (path_noexec(h_path)
21787 +                || !(h_inode->i_mode & 0111))))
21788 +               goto out;
21789 +
21790 +       /*
21791 +        * - skip the lower fs test in the case of write to ro branch.
21792 +        * - nfs dir permission write check is optimized, but a policy for
21793 +        *   link/rename requires a real check.
21794 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
21795 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
21796 +        */
21797 +       h_idmap = mnt_idmap(h_path->mnt);
21798 +       if ((write_mask && !au_br_writable(brperm))
21799 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21800 +               && write_mask && !(mask & MAY_READ))
21801 +           || !h_inode->i_op->permission) {
21802 +               /* AuLabel(generic_permission); */
21803 +               /* AuDbg("get_inode_acl %ps\n",
21804 +                  h_inode->i_op->get_inode_acl); */
21805 +               err = generic_permission(h_idmap, h_inode, mask);
21806 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
21807 +                       err = h_inode->i_op->permission(h_idmap, h_inode,
21808 +                                                       mask);
21809 +               AuTraceErr(err);
21810 +       } else {
21811 +               /* AuLabel(h_inode->permission); */
21812 +               err = h_inode->i_op->permission(h_idmap, h_inode, mask);
21813 +               AuTraceErr(err);
21814 +       }
21815 +
21816 +       if (!err)
21817 +               err = devcgroup_inode_permission(h_inode, mask);
21818 +       if (!err)
21819 +               err = security_inode_permission(h_inode, mask);
21820 +
21821 +out:
21822 +       return err;
21823 +}
21824 +
21825 +static int aufs_permission(struct mnt_idmap *idmap, struct inode *inode,
21826 +                          int mask)
21827 +{
21828 +       int err;
21829 +       aufs_bindex_t bindex, bbot;
21830 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21831 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21832 +       struct inode *h_inode;
21833 +       struct super_block *sb;
21834 +       struct au_branch *br;
21835 +
21836 +       /* todo: support rcu-walk? */
21837 +       if (mask & MAY_NOT_BLOCK)
21838 +               return -ECHILD;
21839 +
21840 +       sb = inode->i_sb;
21841 +       si_read_lock(sb, AuLock_FLUSH);
21842 +       ii_read_lock_child(inode);
21843 +#if 0 /* reserved for future use */
21844 +       /*
21845 +        * This test may be rather 'too much' since the test is essentially done
21846 +        * in the aufs_lookup().  Theoretically it is possible that the inode
21847 +        * generation doesn't match to the superblock's here.  But it isn't a
21848 +        * big deal I suppose.
21849 +        */
21850 +       err = au_iigen_test(inode, au_sigen(sb));
21851 +       if (unlikely(err))
21852 +               goto out;
21853 +#endif
21854 +
21855 +       if (!isdir
21856 +           || write_mask
21857 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
21858 +               err = au_busy_or_stale();
21859 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
21860 +               if (unlikely(!h_inode
21861 +                            || (h_inode->i_mode & S_IFMT)
21862 +                            != (inode->i_mode & S_IFMT)))
21863 +                       goto out;
21864 +
21865 +               err = 0;
21866 +               bindex = au_ibtop(inode);
21867 +               br = au_sbr(sb, bindex);
21868 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
21869 +               if (write_mask
21870 +                   && !err
21871 +                   && !special_file(h_inode->i_mode)) {
21872 +                       /* test whether the upper writable branch exists */
21873 +                       err = -EROFS;
21874 +                       for (; bindex >= 0; bindex--)
21875 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
21876 +                                       err = 0;
21877 +                                       break;
21878 +                               }
21879 +               }
21880 +               goto out;
21881 +       }
21882 +
21883 +       /* non-write to dir */
21884 +       err = 0;
21885 +       bbot = au_ibbot(inode);
21886 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
21887 +               h_inode = au_h_iptr(inode, bindex);
21888 +               if (h_inode) {
21889 +                       err = au_busy_or_stale();
21890 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
21891 +                               break;
21892 +
21893 +                       br = au_sbr(sb, bindex);
21894 +                       err = h_permission(h_inode, mask, &br->br_path,
21895 +                                          br->br_perm);
21896 +               }
21897 +       }
21898 +
21899 +out:
21900 +       ii_read_unlock(inode);
21901 +       si_read_unlock(sb);
21902 +       return err;
21903 +}
21904 +
21905 +/* ---------------------------------------------------------------------- */
21906 +
21907 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
21908 +                                 unsigned int flags)
21909 +{
21910 +       struct dentry *ret, *parent;
21911 +       struct inode *inode;
21912 +       struct super_block *sb;
21913 +       int err, npositive;
21914 +
21915 +       IMustLock(dir);
21916 +
21917 +       /* todo: support rcu-walk? */
21918 +       ret = ERR_PTR(-ECHILD);
21919 +       if (flags & LOOKUP_RCU)
21920 +               goto out;
21921 +
21922 +       ret = ERR_PTR(-ENAMETOOLONG);
21923 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21924 +               goto out;
21925 +
21926 +       sb = dir->i_sb;
21927 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21928 +       ret = ERR_PTR(err);
21929 +       if (unlikely(err))
21930 +               goto out;
21931 +
21932 +       err = au_di_init(dentry);
21933 +       ret = ERR_PTR(err);
21934 +       if (unlikely(err))
21935 +               goto out_si;
21936 +
21937 +       inode = NULL;
21938 +       npositive = 0; /* suppress a warning */
21939 +       parent = dentry->d_parent; /* dir inode is locked */
21940 +       di_read_lock_parent(parent, AuLock_IR);
21941 +       err = au_alive_dir(parent);
21942 +       if (!err)
21943 +               err = au_digen_test(parent, au_sigen(sb));
21944 +       if (!err) {
21945 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21946 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21947 +                                          AuLkup_ALLOW_NEG);
21948 +               err = npositive;
21949 +       }
21950 +       di_read_unlock(parent, AuLock_IR);
21951 +       ret = ERR_PTR(err);
21952 +       if (unlikely(err < 0))
21953 +               goto out_unlock;
21954 +
21955 +       if (npositive) {
21956 +               inode = au_new_inode(dentry, /*must_new*/0);
21957 +               if (IS_ERR(inode)) {
21958 +                       ret = (void *)inode;
21959 +                       inode = NULL;
21960 +                       goto out_unlock;
21961 +               }
21962 +       }
21963 +
21964 +       if (inode)
21965 +               atomic_inc(&inode->i_count);
21966 +       ret = d_splice_alias(inode, dentry);
21967 +#if 0 /* reserved for future use */
21968 +       if (unlikely(d_need_lookup(dentry))) {
21969 +               spin_lock(&dentry->d_lock);
21970 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21971 +               spin_unlock(&dentry->d_lock);
21972 +       } else
21973 +#endif
21974 +       if (inode) {
21975 +               if (!IS_ERR(ret)) {
21976 +                       iput(inode);
21977 +                       if (ret && ret != dentry)
21978 +                               ii_write_unlock(inode);
21979 +               } else {
21980 +                       ii_write_unlock(inode);
21981 +                       iput(inode);
21982 +                       inode = NULL;
21983 +               }
21984 +       }
21985 +
21986 +out_unlock:
21987 +       di_write_unlock(dentry);
21988 +out_si:
21989 +       si_read_unlock(sb);
21990 +out:
21991 +       return ret;
21992 +}
21993 +
21994 +/* ---------------------------------------------------------------------- */
21995 +
21996 +/*
21997 + * very dirty and complicated aufs ->atomic_open().
21998 + * aufs_atomic_open()
21999 + * + au_aopen_or_create()
22000 + *   + add_simple()
22001 + *     + vfsub_atomic_open()
22002 + *       + branch fs ->atomic_open()
22003 + *        may call the actual 'open' for h_file
22004 + *       + inc br_nfiles only if opened
22005 + * + au_aopen_no_open() or au_aopen_do_open()
22006 + *
22007 + * au_aopen_do_open()
22008 + * + finish_open()
22009 + *   + au_do_aopen()
22010 + *     + au_do_open() the body of all 'open'
22011 + *       + au_do_open_nondir()
22012 + *        set the passed h_file
22013 + *
22014 + * au_aopen_no_open()
22015 + * + finish_no_open()
22016 + */
22017 +
22018 +struct aopen_node {
22019 +       struct hlist_bl_node hblist;
22020 +       struct file *file, *h_file;
22021 +};
22022 +
22023 +static int au_do_aopen(struct inode *inode, struct file *file)
22024 +{
22025 +       struct hlist_bl_head *aopen;
22026 +       struct hlist_bl_node *pos;
22027 +       struct aopen_node *node;
22028 +       struct au_do_open_args args = {
22029 +               .aopen  = 1,
22030 +               .open   = au_do_open_nondir
22031 +       };
22032 +
22033 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
22034 +       hlist_bl_lock(aopen);
22035 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
22036 +               if (node->file == file) {
22037 +                       args.h_file = node->h_file;
22038 +                       break;
22039 +               }
22040 +       hlist_bl_unlock(aopen);
22041 +       /* AuDebugOn(!args.h_file); */
22042 +
22043 +       return au_do_open(file, &args);
22044 +}
22045 +
22046 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
22047 +                           struct aopen_node *aopen_node)
22048 +{
22049 +       int err;
22050 +       struct hlist_bl_head *aopen;
22051 +
22052 +       AuLabel(here);
22053 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
22054 +       au_hbl_add(&aopen_node->hblist, aopen);
22055 +       err = finish_open(file, dentry, au_do_aopen);
22056 +       au_hbl_del(&aopen_node->hblist, aopen);
22057 +       /* AuDbgFile(file); */
22058 +       AuDbg("%pd%s%s\n", dentry,
22059 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
22060 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
22061 +
22062 +       AuTraceErr(err);
22063 +       return err;
22064 +}
22065 +
22066 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
22067 +{
22068 +       int err;
22069 +
22070 +       AuLabel(here);
22071 +       dget(dentry);
22072 +       err = finish_no_open(file, dentry);
22073 +
22074 +       AuTraceErr(err);
22075 +       return err;
22076 +}
22077 +
22078 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22079 +                           struct file *file, unsigned int open_flag,
22080 +                           umode_t create_mode)
22081 +{
22082 +       int err, did_open;
22083 +       unsigned int lkup_flags;
22084 +       aufs_bindex_t bindex;
22085 +       struct super_block *sb;
22086 +       struct dentry *parent, *d;
22087 +       struct vfsub_aopen_args args = {
22088 +               .open_flag      = open_flag,
22089 +               .create_mode    = create_mode
22090 +       };
22091 +       struct aopen_node aopen_node = {
22092 +               .file   = file
22093 +       };
22094 +
22095 +       IMustLock(dir);
22096 +       AuDbg("open_flag 0%o\n", open_flag);
22097 +       AuDbgDentry(dentry);
22098 +
22099 +       err = 0;
22100 +       if (!au_di(dentry)) {
22101 +               lkup_flags = LOOKUP_OPEN;
22102 +               if (open_flag & O_CREAT)
22103 +                       lkup_flags |= LOOKUP_CREATE;
22104 +               d = aufs_lookup(dir, dentry, lkup_flags);
22105 +               if (IS_ERR(d)) {
22106 +                       err = PTR_ERR(d);
22107 +                       AuTraceErr(err);
22108 +                       goto out;
22109 +               } else if (d) {
22110 +                       /*
22111 +                        * obsoleted dentry found.
22112 +                        * another error will be returned later.
22113 +                        */
22114 +                       d_drop(d);
22115 +                       AuDbgDentry(d);
22116 +                       dput(d);
22117 +               }
22118 +               AuDbgDentry(dentry);
22119 +       }
22120 +
22121 +       if (d_is_positive(dentry)
22122 +           || d_unhashed(dentry)
22123 +           || d_unlinked(dentry)
22124 +           || !(open_flag & O_CREAT)) {
22125 +               err = au_aopen_no_open(file, dentry);
22126 +               goto out; /* success */
22127 +       }
22128 +
22129 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22130 +       if (unlikely(err))
22131 +               goto out;
22132 +
22133 +       sb = dentry->d_sb;
22134 +       parent = dentry->d_parent;      /* dir is locked */
22135 +       di_write_lock_parent(parent);
22136 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
22137 +       if (unlikely(err < 0))
22138 +               goto out_parent;
22139 +
22140 +       AuDbgDentry(dentry);
22141 +       if (d_is_positive(dentry)) {
22142 +               err = au_aopen_no_open(file, dentry);
22143 +               goto out_parent; /* success */
22144 +       }
22145 +
22146 +       args.file = alloc_empty_file(file->f_flags, current_cred());
22147 +       err = PTR_ERR(args.file);
22148 +       if (IS_ERR(args.file))
22149 +               goto out_parent;
22150 +
22151 +       bindex = au_dbtop(dentry);
22152 +       err = au_aopen_or_create(dir, dentry, &args);
22153 +       AuTraceErr(err);
22154 +       AuDbgFile(args.file);
22155 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22156 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
22157 +       if (!did_open) {
22158 +               fput(args.file);
22159 +               args.file = NULL;
22160 +       }
22161 +       di_write_unlock(parent);
22162 +       di_write_unlock(dentry);
22163 +       if (unlikely(err < 0)) {
22164 +               if (args.file)
22165 +                       fput(args.file);
22166 +               goto out_sb;
22167 +       }
22168 +
22169 +       if (!did_open)
22170 +               err = au_aopen_no_open(file, dentry);
22171 +       else {
22172 +               aopen_node.h_file = args.file;
22173 +               err = au_aopen_do_open(file, dentry, &aopen_node);
22174 +       }
22175 +       if (unlikely(err < 0)) {
22176 +               if (args.file)
22177 +                       fput(args.file);
22178 +               if (did_open)
22179 +                       au_lcnt_dec(&args.br->br_nfiles);
22180 +       }
22181 +       goto out_sb; /* success */
22182 +
22183 +out_parent:
22184 +       di_write_unlock(parent);
22185 +       di_write_unlock(dentry);
22186 +out_sb:
22187 +       si_read_unlock(sb);
22188 +out:
22189 +       AuTraceErr(err);
22190 +       AuDbgFile(file);
22191 +       return err;
22192 +}
22193 +
22194 +
22195 +/* ---------------------------------------------------------------------- */
22196 +
22197 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22198 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
22199 +                         aufs_bindex_t btop)
22200 +{
22201 +       int err;
22202 +       struct dentry *h_parent;
22203 +       struct inode *h_dir;
22204 +
22205 +       if (add_entry)
22206 +               IMustLock(d_inode(parent));
22207 +       else
22208 +               di_write_lock_parent(parent);
22209 +
22210 +       err = 0;
22211 +       if (!au_h_dptr(parent, bcpup)) {
22212 +               if (btop > bcpup)
22213 +                       err = au_cpup_dirs(dentry, bcpup);
22214 +               else if (btop < bcpup)
22215 +                       err = au_cpdown_dirs(dentry, bcpup);
22216 +               else
22217 +                       BUG();
22218 +       }
22219 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
22220 +               h_parent = au_h_dptr(parent, bcpup);
22221 +               h_dir = d_inode(h_parent);
22222 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
22223 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
22224 +               /* todo: no unlock here */
22225 +               inode_unlock_shared(h_dir);
22226 +
22227 +               AuDbg("bcpup %d\n", bcpup);
22228 +               if (!err) {
22229 +                       if (d_really_is_negative(dentry))
22230 +                               au_set_h_dptr(dentry, btop, NULL);
22231 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
22232 +               }
22233 +       }
22234 +
22235 +       if (!add_entry)
22236 +               di_write_unlock(parent);
22237 +       if (!err)
22238 +               err = bcpup; /* success */
22239 +
22240 +       AuTraceErr(err);
22241 +       return err;
22242 +}
22243 +
22244 +/*
22245 + * decide the branch and the parent dir where we will create a new entry.
22246 + * returns new bindex or an error.
22247 + * copyup the parent dir if needed.
22248 + */
22249 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22250 +             struct au_wr_dir_args *args)
22251 +{
22252 +       int err;
22253 +       unsigned int flags;
22254 +       aufs_bindex_t bcpup, btop, src_btop;
22255 +       const unsigned char add_entry
22256 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
22257 +               | au_ftest_wrdir(args->flags, TMPFILE);
22258 +       struct super_block *sb;
22259 +       struct dentry *parent;
22260 +       struct au_sbinfo *sbinfo;
22261 +
22262 +       sb = dentry->d_sb;
22263 +       sbinfo = au_sbi(sb);
22264 +       parent = dget_parent(dentry);
22265 +       btop = au_dbtop(dentry);
22266 +       bcpup = btop;
22267 +       if (args->force_btgt < 0) {
22268 +               if (src_dentry) {
22269 +                       src_btop = au_dbtop(src_dentry);
22270 +                       if (src_btop < btop)
22271 +                               bcpup = src_btop;
22272 +               } else if (add_entry) {
22273 +                       flags = 0;
22274 +                       if (au_ftest_wrdir(args->flags, ISDIR))
22275 +                               au_fset_wbr(flags, DIR);
22276 +                       err = AuWbrCreate(sbinfo, dentry, flags);
22277 +                       bcpup = err;
22278 +               }
22279 +
22280 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
22281 +                       if (add_entry)
22282 +                               err = AuWbrCopyup(sbinfo, dentry);
22283 +                       else {
22284 +                               if (!IS_ROOT(dentry)) {
22285 +                                       di_read_lock_parent(parent, !AuLock_IR);
22286 +                                       err = AuWbrCopyup(sbinfo, dentry);
22287 +                                       di_read_unlock(parent, !AuLock_IR);
22288 +                               } else
22289 +                                       err = AuWbrCopyup(sbinfo, dentry);
22290 +                       }
22291 +                       bcpup = err;
22292 +                       if (unlikely(err < 0))
22293 +                               goto out;
22294 +               }
22295 +       } else {
22296 +               bcpup = args->force_btgt;
22297 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
22298 +       }
22299 +
22300 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
22301 +       err = bcpup;
22302 +       if (bcpup == btop)
22303 +               goto out; /* success */
22304 +
22305 +       /* copyup the new parent into the branch we process */
22306 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
22307 +       if (err >= 0) {
22308 +               if (d_really_is_negative(dentry)) {
22309 +                       au_set_h_dptr(dentry, btop, NULL);
22310 +                       au_set_dbtop(dentry, bcpup);
22311 +                       au_set_dbbot(dentry, bcpup);
22312 +               }
22313 +               AuDebugOn(add_entry
22314 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
22315 +                         && !au_h_dptr(dentry, bcpup));
22316 +       }
22317 +
22318 +out:
22319 +       dput(parent);
22320 +       return err;
22321 +}
22322 +
22323 +/* ---------------------------------------------------------------------- */
22324 +
22325 +void au_pin_hdir_unlock(struct au_pin *p)
22326 +{
22327 +       if (p->hdir)
22328 +               au_hn_inode_unlock(p->hdir);
22329 +}
22330 +
22331 +int au_pin_hdir_lock(struct au_pin *p)
22332 +{
22333 +       int err;
22334 +
22335 +       err = 0;
22336 +       if (!p->hdir)
22337 +               goto out;
22338 +
22339 +       /* even if an error happens later, keep this lock */
22340 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
22341 +
22342 +       err = -EBUSY;
22343 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
22344 +               goto out;
22345 +
22346 +       err = 0;
22347 +       if (p->h_dentry)
22348 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22349 +                                 p->h_parent, p->br);
22350 +
22351 +out:
22352 +       return err;
22353 +}
22354 +
22355 +int au_pin_hdir_relock(struct au_pin *p)
22356 +{
22357 +       int err, i;
22358 +       struct inode *h_i;
22359 +       struct dentry *h_d[] = {
22360 +               p->h_dentry,
22361 +               p->h_parent
22362 +       };
22363 +
22364 +       err = au_pin_hdir_lock(p);
22365 +       if (unlikely(err))
22366 +               goto out;
22367 +
22368 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22369 +               if (!h_d[i])
22370 +                       continue;
22371 +               if (d_is_positive(h_d[i])) {
22372 +                       h_i = d_inode(h_d[i]);
22373 +                       err = !h_i->i_nlink;
22374 +               }
22375 +       }
22376 +
22377 +out:
22378 +       return err;
22379 +}
22380 +
22381 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
22382 +{
22383 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
22384 +}
22385 +
22386 +void au_pin_hdir_acquire_nest(struct au_pin *p)
22387 +{
22388 +       if (p->hdir) {
22389 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
22390 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
22391 +               au_pin_hdir_set_owner(p, current);
22392 +       }
22393 +}
22394 +
22395 +void au_pin_hdir_release(struct au_pin *p)
22396 +{
22397 +       if (p->hdir) {
22398 +               au_pin_hdir_set_owner(p, p->task);
22399 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
22400 +       }
22401 +}
22402 +
22403 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
22404 +{
22405 +       if (pin && pin->parent)
22406 +               return au_h_dptr(pin->parent, pin->bindex);
22407 +       return NULL;
22408 +}
22409 +
22410 +void au_unpin(struct au_pin *p)
22411 +{
22412 +       if (p->hdir)
22413 +               au_pin_hdir_unlock(p);
22414 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
22415 +               vfsub_mnt_drop_write(p->h_mnt);
22416 +       if (!p->hdir)
22417 +               return;
22418 +
22419 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22420 +               di_read_unlock(p->parent, AuLock_IR);
22421 +       iput(p->hdir->hi_inode);
22422 +       dput(p->parent);
22423 +       p->parent = NULL;
22424 +       p->hdir = NULL;
22425 +       p->h_mnt = NULL;
22426 +       /* do not clear p->task */
22427 +}
22428 +
22429 +int au_do_pin(struct au_pin *p)
22430 +{
22431 +       int err;
22432 +       struct super_block *sb;
22433 +       struct inode *h_dir;
22434 +
22435 +       err = 0;
22436 +       sb = p->dentry->d_sb;
22437 +       p->br = au_sbr(sb, p->bindex);
22438 +       if (IS_ROOT(p->dentry)) {
22439 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
22440 +                       p->h_mnt = au_br_mnt(p->br);
22441 +                       err = vfsub_mnt_want_write(p->h_mnt);
22442 +                       if (unlikely(err)) {
22443 +                               au_fclr_pin(p->flags, MNT_WRITE);
22444 +                               goto out_err;
22445 +                       }
22446 +               }
22447 +               goto out;
22448 +       }
22449 +
22450 +       p->h_dentry = NULL;
22451 +       if (p->bindex <= au_dbbot(p->dentry))
22452 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
22453 +
22454 +       p->parent = dget_parent(p->dentry);
22455 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22456 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
22457 +
22458 +       h_dir = NULL;
22459 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
22460 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
22461 +       if (p->hdir)
22462 +               h_dir = p->hdir->hi_inode;
22463 +
22464 +       /*
22465 +        * udba case, or
22466 +        * if DI_LOCKED is not set, then p->parent may be different
22467 +        * and h_parent can be NULL.
22468 +        */
22469 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
22470 +               err = -EBUSY;
22471 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
22472 +                       di_read_unlock(p->parent, AuLock_IR);
22473 +               dput(p->parent);
22474 +               p->parent = NULL;
22475 +               goto out_err;
22476 +       }
22477 +
22478 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
22479 +               p->h_mnt = au_br_mnt(p->br);
22480 +               err = vfsub_mnt_want_write(p->h_mnt);
22481 +               if (unlikely(err)) {
22482 +                       au_fclr_pin(p->flags, MNT_WRITE);
22483 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
22484 +                               di_read_unlock(p->parent, AuLock_IR);
22485 +                       dput(p->parent);
22486 +                       p->parent = NULL;
22487 +                       goto out_err;
22488 +               }
22489 +       }
22490 +
22491 +       au_igrab(h_dir);
22492 +       err = au_pin_hdir_lock(p);
22493 +       if (!err)
22494 +               goto out; /* success */
22495 +
22496 +       au_unpin(p);
22497 +
22498 +out_err:
22499 +       pr_err("err %d\n", err);
22500 +       err = au_busy_or_stale();
22501 +out:
22502 +       return err;
22503 +}
22504 +
22505 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
22506 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22507 +                unsigned int udba, unsigned char flags)
22508 +{
22509 +       p->dentry = dentry;
22510 +       p->udba = udba;
22511 +       p->lsc_di = lsc_di;
22512 +       p->lsc_hi = lsc_hi;
22513 +       p->flags = flags;
22514 +       p->bindex = bindex;
22515 +
22516 +       p->parent = NULL;
22517 +       p->hdir = NULL;
22518 +       p->h_mnt = NULL;
22519 +
22520 +       p->h_dentry = NULL;
22521 +       p->h_parent = NULL;
22522 +       p->br = NULL;
22523 +       p->task = current;
22524 +}
22525 +
22526 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22527 +          unsigned int udba, unsigned char flags)
22528 +{
22529 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22530 +                   udba, flags);
22531 +       return au_do_pin(pin);
22532 +}
22533 +
22534 +/* ---------------------------------------------------------------------- */
22535 +
22536 +/*
22537 + * ->setattr() and ->getattr() are called in various cases.
22538 + * chmod, stat: dentry is revalidated.
22539 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22540 + *               unhashed.
22541 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
22542 + */
22543 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
22544 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
22545 +{
22546 +       int err;
22547 +       struct dentry *parent;
22548 +
22549 +       err = 0;
22550 +       if (au_digen_test(dentry, sigen)) {
22551 +               parent = dget_parent(dentry);
22552 +               di_read_lock_parent(parent, AuLock_IR);
22553 +               err = au_refresh_dentry(dentry, parent);
22554 +               di_read_unlock(parent, AuLock_IR);
22555 +               dput(parent);
22556 +       }
22557 +
22558 +       AuTraceErr(err);
22559 +       return err;
22560 +}
22561 +
22562 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22563 +                    struct au_icpup_args *a)
22564 +{
22565 +       int err;
22566 +       loff_t sz;
22567 +       aufs_bindex_t btop, ibtop;
22568 +       struct dentry *hi_wh, *parent;
22569 +       struct inode *inode;
22570 +       struct au_wr_dir_args wr_dir_args = {
22571 +               .force_btgt     = -1,
22572 +               .flags          = 0
22573 +       };
22574 +
22575 +       if (d_is_dir(dentry))
22576 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
22577 +       /* plink or hi_wh() case */
22578 +       btop = au_dbtop(dentry);
22579 +       inode = d_inode(dentry);
22580 +       ibtop = au_ibtop(inode);
22581 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22582 +               wr_dir_args.force_btgt = ibtop;
22583 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22584 +       if (unlikely(err < 0))
22585 +               goto out;
22586 +       a->btgt = err;
22587 +       if (err != btop)
22588 +               au_fset_icpup(a->flags, DID_CPUP);
22589 +
22590 +       err = 0;
22591 +       a->pin_flags = AuPin_MNT_WRITE;
22592 +       parent = NULL;
22593 +       if (!IS_ROOT(dentry)) {
22594 +               au_fset_pin(a->pin_flags, DI_LOCKED);
22595 +               parent = dget_parent(dentry);
22596 +               di_write_lock_parent(parent);
22597 +       }
22598 +
22599 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22600 +       if (unlikely(err))
22601 +               goto out_parent;
22602 +
22603 +       sz = -1;
22604 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22605 +       a->h_inode = d_inode(a->h_path.dentry);
22606 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
22607 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
22608 +               if (ia->ia_size < i_size_read(a->h_inode))
22609 +                       sz = ia->ia_size;
22610 +               inode_unlock_shared(a->h_inode);
22611 +       }
22612 +
22613 +       hi_wh = NULL;
22614 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
22615 +               hi_wh = au_hi_wh(inode, a->btgt);
22616 +               if (!hi_wh) {
22617 +                       struct au_cp_generic cpg = {
22618 +                               .dentry = dentry,
22619 +                               .bdst   = a->btgt,
22620 +                               .bsrc   = -1,
22621 +                               .len    = sz,
22622 +                               .pin    = &a->pin
22623 +                       };
22624 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
22625 +                       if (unlikely(err))
22626 +                               goto out_unlock;
22627 +                       hi_wh = au_hi_wh(inode, a->btgt);
22628 +                       /* todo: revalidate hi_wh? */
22629 +               }
22630 +       }
22631 +
22632 +       if (parent) {
22633 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22634 +               di_downgrade_lock(parent, AuLock_IR);
22635 +               dput(parent);
22636 +               parent = NULL;
22637 +       }
22638 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
22639 +               goto out; /* success */
22640 +
22641 +       if (!d_unhashed(dentry)) {
22642 +               struct au_cp_generic cpg = {
22643 +                       .dentry = dentry,
22644 +                       .bdst   = a->btgt,
22645 +                       .bsrc   = btop,
22646 +                       .len    = sz,
22647 +                       .pin    = &a->pin,
22648 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
22649 +               };
22650 +               err = au_sio_cpup_simple(&cpg);
22651 +               if (!err)
22652 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22653 +       } else if (!hi_wh)
22654 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22655 +       else
22656 +               a->h_path.dentry = hi_wh; /* do not dget here */
22657 +
22658 +out_unlock:
22659 +       a->h_inode = d_inode(a->h_path.dentry);
22660 +       if (!err)
22661 +               goto out; /* success */
22662 +       au_unpin(&a->pin);
22663 +out_parent:
22664 +       if (parent) {
22665 +               di_write_unlock(parent);
22666 +               dput(parent);
22667 +       }
22668 +out:
22669 +       if (!err)
22670 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22671 +       return err;
22672 +}
22673 +
22674 +static int aufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
22675 +                       struct iattr *ia)
22676 +{
22677 +       int err;
22678 +       struct inode *inode, *delegated;
22679 +       struct super_block *sb;
22680 +       struct file *file;
22681 +       struct au_icpup_args *a;
22682 +       struct mnt_idmap *h_idmap;
22683 +
22684 +       inode = d_inode(dentry);
22685 +       IMustLock(inode);
22686 +
22687 +       err = setattr_prepare(idmap, dentry, ia);
22688 +       if (unlikely(err))
22689 +               goto out;
22690 +
22691 +       err = -ENOMEM;
22692 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22693 +       if (unlikely(!a))
22694 +               goto out;
22695 +
22696 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22697 +               ia->ia_valid &= ~ATTR_MODE;
22698 +
22699 +       file = NULL;
22700 +       sb = dentry->d_sb;
22701 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22702 +       if (unlikely(err))
22703 +               goto out_kfree;
22704 +
22705 +       if (ia->ia_valid & ATTR_FILE) {
22706 +               /* currently ftruncate(2) only */
22707 +               AuDebugOn(!d_is_reg(dentry));
22708 +               file = ia->ia_file;
22709 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22710 +                                           /*fi_lsc*/0);
22711 +               if (unlikely(err))
22712 +                       goto out_si;
22713 +               ia->ia_file = au_hf_top(file);
22714 +               a->udba = AuOpt_UDBA_NONE;
22715 +       } else {
22716 +               /* fchmod() doesn't pass ia_file */
22717 +               a->udba = au_opt_udba(sb);
22718 +               di_write_lock_child(dentry);
22719 +               /* no d_unlinked(), to set UDBA_NONE for root */
22720 +               if (d_unhashed(dentry))
22721 +                       a->udba = AuOpt_UDBA_NONE;
22722 +               if (a->udba != AuOpt_UDBA_NONE) {
22723 +                       AuDebugOn(IS_ROOT(dentry));
22724 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
22725 +                       if (unlikely(err))
22726 +                               goto out_dentry;
22727 +               }
22728 +       }
22729 +
22730 +       err = au_pin_and_icpup(dentry, ia, a);
22731 +       if (unlikely(err < 0))
22732 +               goto out_dentry;
22733 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
22734 +               ia->ia_file = NULL;
22735 +               ia->ia_valid &= ~ATTR_FILE;
22736 +       }
22737 +
22738 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22739 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22740 +           == (ATTR_MODE | ATTR_CTIME)) {
22741 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
22742 +               if (unlikely(err))
22743 +                       goto out_unlock;
22744 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22745 +                  && (ia->ia_valid & ATTR_CTIME)) {
22746 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
22747 +               if (unlikely(err))
22748 +                       goto out_unlock;
22749 +       }
22750 +
22751 +       if (ia->ia_valid & ATTR_SIZE) {
22752 +               struct file *f;
22753 +
22754 +               if (ia->ia_size < i_size_read(inode))
22755 +                       /* unmap only */
22756 +                       truncate_setsize(inode, ia->ia_size);
22757 +
22758 +               f = NULL;
22759 +               if (ia->ia_valid & ATTR_FILE)
22760 +                       f = ia->ia_file;
22761 +               inode_unlock(a->h_inode);
22762 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
22763 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22764 +       } else {
22765 +               delegated = NULL;
22766 +               while (1) {
22767 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
22768 +                       if (delegated) {
22769 +                               err = break_deleg_wait(&delegated);
22770 +                               if (!err)
22771 +                                       continue;
22772 +                       }
22773 +                       break;
22774 +               }
22775 +       }
22776 +       /*
22777 +        * regardless aufs 'acl' option setting.
22778 +        * why don't all acl-aware fs call this func from their ->setattr()?
22779 +        */
22780 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
22781 +               h_idmap = mnt_idmap(a->h_path.mnt);
22782 +               err = vfsub_acl_chmod(h_idmap, a->h_path.dentry, ia->ia_mode);
22783 +       }
22784 +       if (!err)
22785 +               au_cpup_attr_changeable(inode);
22786 +
22787 +out_unlock:
22788 +       inode_unlock(a->h_inode);
22789 +       au_unpin(&a->pin);
22790 +       if (unlikely(err))
22791 +               au_update_dbtop(dentry);
22792 +out_dentry:
22793 +       di_write_unlock(dentry);
22794 +       if (file) {
22795 +               fi_write_unlock(file);
22796 +               ia->ia_file = file;
22797 +               ia->ia_valid |= ATTR_FILE;
22798 +       }
22799 +out_si:
22800 +       si_read_unlock(sb);
22801 +out_kfree:
22802 +       au_kfree_rcu(a);
22803 +out:
22804 +       AuTraceErr(err);
22805 +       return err;
22806 +}
22807 +
22808 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22809 +static int au_h_path_to_set_attr(struct dentry *dentry,
22810 +                                struct au_icpup_args *a, struct path *h_path)
22811 +{
22812 +       int err;
22813 +       struct super_block *sb;
22814 +
22815 +       sb = dentry->d_sb;
22816 +       a->udba = au_opt_udba(sb);
22817 +       /* no d_unlinked(), to set UDBA_NONE for root */
22818 +       if (d_unhashed(dentry))
22819 +               a->udba = AuOpt_UDBA_NONE;
22820 +       if (a->udba != AuOpt_UDBA_NONE) {
22821 +               AuDebugOn(IS_ROOT(dentry));
22822 +               err = au_reval_for_attr(dentry, au_sigen(sb));
22823 +               if (unlikely(err))
22824 +                       goto out;
22825 +       }
22826 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22827 +       if (unlikely(err < 0))
22828 +               goto out;
22829 +
22830 +       h_path->dentry = a->h_path.dentry;
22831 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
22832 +
22833 +out:
22834 +       return err;
22835 +}
22836 +
22837 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22838 +                 struct au_sxattr *arg)
22839 +{
22840 +       int err;
22841 +       struct path h_path;
22842 +       struct super_block *sb;
22843 +       struct au_icpup_args *a;
22844 +       struct inode *h_inode;
22845 +       struct mnt_idmap *h_idmap;
22846 +
22847 +       IMustLock(inode);
22848 +
22849 +       err = -ENOMEM;
22850 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22851 +       if (unlikely(!a))
22852 +               goto out;
22853 +
22854 +       sb = dentry->d_sb;
22855 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22856 +       if (unlikely(err))
22857 +               goto out_kfree;
22858 +
22859 +       h_path.dentry = NULL;   /* silence gcc */
22860 +       di_write_lock_child(dentry);
22861 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
22862 +       if (unlikely(err))
22863 +               goto out_di;
22864 +       h_idmap = mnt_idmap(h_path.mnt);
22865 +
22866 +       inode_unlock(a->h_inode);
22867 +       switch (arg->type) {
22868 +       case AU_XATTR_SET:
22869 +               AuDebugOn(d_is_negative(h_path.dentry));
22870 +               err = vfsub_setxattr(h_idmap, h_path.dentry,
22871 +                                    arg->u.set.name, arg->u.set.value,
22872 +                                    arg->u.set.size, arg->u.set.flags);
22873 +               break;
22874 +       case AU_ACL_SET:
22875 +               err = -EOPNOTSUPP;
22876 +               h_inode = d_inode(h_path.dentry);
22877 +               if (h_inode->i_op->set_acl) {
22878 +                       /* this will call posix_acl_update_mode */
22879 +                       err = h_inode->i_op->set_acl(h_idmap, h_path.dentry,
22880 +                                                    arg->u.acl_set.acl,
22881 +                                                    arg->u.acl_set.type);
22882 +               }
22883 +               break;
22884 +       }
22885 +       if (!err)
22886 +               au_cpup_attr_timesizes(inode);
22887 +
22888 +       au_unpin(&a->pin);
22889 +       if (unlikely(err))
22890 +               au_update_dbtop(dentry);
22891 +
22892 +out_di:
22893 +       di_write_unlock(dentry);
22894 +       si_read_unlock(sb);
22895 +out_kfree:
22896 +       au_kfree_rcu(a);
22897 +out:
22898 +       AuTraceErr(err);
22899 +       return err;
22900 +}
22901 +#endif
22902 +
22903 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22904 +                            unsigned int nlink)
22905 +{
22906 +       unsigned int n;
22907 +
22908 +       inode->i_mode = st->mode;
22909 +       /* don't i_[ug]id_write() here */
22910 +       inode->i_uid = st->uid;
22911 +       inode->i_gid = st->gid;
22912 +       inode->i_atime = st->atime;
22913 +       inode->i_mtime = st->mtime;
22914 +       inode->i_ctime = st->ctime;
22915 +
22916 +       au_cpup_attr_nlink(inode, /*force*/0);
22917 +       if (S_ISDIR(inode->i_mode)) {
22918 +               n = inode->i_nlink;
22919 +               n -= nlink;
22920 +               n += st->nlink;
22921 +               smp_mb(); /* for i_nlink */
22922 +               /* 0 can happen */
22923 +               set_nlink(inode, n);
22924 +       }
22925 +
22926 +       spin_lock(&inode->i_lock);
22927 +       inode->i_blocks = st->blocks;
22928 +       i_size_write(inode, st->size);
22929 +       spin_unlock(&inode->i_lock);
22930 +}
22931 +
22932 +/*
22933 + * common routine for aufs_getattr() and au_getxattr().
22934 + * returns zero or negative (an error).
22935 + * @dentry will be read-locked in success.
22936 + */
22937 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22938 +                     struct path *h_path, int locked)
22939 +{
22940 +       int err;
22941 +       unsigned int mnt_flags, sigen;
22942 +       unsigned char udba_none;
22943 +       aufs_bindex_t bindex;
22944 +       struct super_block *sb, *h_sb;
22945 +
22946 +       h_path->mnt = NULL;
22947 +       h_path->dentry = NULL;
22948 +
22949 +       err = 0;
22950 +       sb = dentry->d_sb;
22951 +       mnt_flags = au_mntflags(sb);
22952 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
22953 +
22954 +       if (unlikely(locked))
22955 +               goto body; /* skip locking dinfo */
22956 +
22957 +       /* support fstat(2) */
22958 +       if (!d_unlinked(dentry) && !udba_none) {
22959 +               sigen = au_sigen(sb);
22960 +               err = au_digen_test(dentry, sigen);
22961 +               if (!err) {
22962 +                       di_read_lock_child(dentry, AuLock_IR);
22963 +                       err = au_dbrange_test(dentry);
22964 +                       if (unlikely(err)) {
22965 +                               di_read_unlock(dentry, AuLock_IR);
22966 +                               goto out;
22967 +                       }
22968 +               } else {
22969 +                       AuDebugOn(IS_ROOT(dentry));
22970 +                       di_write_lock_child(dentry);
22971 +                       err = au_dbrange_test(dentry);
22972 +                       if (!err)
22973 +                               err = au_reval_for_attr(dentry, sigen);
22974 +                       if (!err)
22975 +                               di_downgrade_lock(dentry, AuLock_IR);
22976 +                       else {
22977 +                               di_write_unlock(dentry);
22978 +                               goto out;
22979 +                       }
22980 +               }
22981 +       } else
22982 +               di_read_lock_child(dentry, AuLock_IR);
22983 +
22984 +body:
22985 +       if (!inode) {
22986 +               inode = d_inode(dentry);
22987 +               if (unlikely(!inode))
22988 +                       goto out;
22989 +       }
22990 +       bindex = au_ibtop(inode);
22991 +       h_path->mnt = au_sbr_mnt(sb, bindex);
22992 +       h_sb = h_path->mnt->mnt_sb;
22993 +       if (!force
22994 +           && !au_test_fs_bad_iattr(h_sb)
22995 +           && udba_none)
22996 +               goto out; /* success */
22997 +
22998 +       if (au_dbtop(dentry) == bindex)
22999 +               h_path->dentry = au_h_dptr(dentry, bindex);
23000 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
23001 +               h_path->dentry = au_plink_lkup(inode, bindex);
23002 +               if (IS_ERR(h_path->dentry))
23003 +                       /* pretending success */
23004 +                       h_path->dentry = NULL;
23005 +               else
23006 +                       dput(h_path->dentry);
23007 +       }
23008 +
23009 +out:
23010 +       return err;
23011 +}
23012 +
23013 +static int aufs_getattr(struct mnt_idmap *idmap, const struct path *path,
23014 +                       struct kstat *st, u32 request, unsigned int query)
23015 +{
23016 +       int err;
23017 +       unsigned char positive;
23018 +       struct path h_path;
23019 +       struct dentry *dentry;
23020 +       struct inode *inode;
23021 +       struct super_block *sb;
23022 +
23023 +       dentry = path->dentry;
23024 +       inode = d_inode(dentry);
23025 +       sb = dentry->d_sb;
23026 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23027 +       if (unlikely(err))
23028 +               goto out;
23029 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
23030 +                               /*locked*/0);
23031 +       if (unlikely(err))
23032 +               goto out_si;
23033 +       if (unlikely(!h_path.dentry))
23034 +               /* illegally overlapped or something */
23035 +               goto out_fill; /* pretending success */
23036 +
23037 +       positive = d_is_positive(h_path.dentry);
23038 +       if (positive)
23039 +               /* no vfsub version */
23040 +               err = vfs_getattr(&h_path, st, request, query);
23041 +       if (!err) {
23042 +               if (positive)
23043 +                       au_refresh_iattr(inode, st,
23044 +                                        d_inode(h_path.dentry)->i_nlink);
23045 +               goto out_fill; /* success */
23046 +       }
23047 +       AuTraceErr(err);
23048 +       goto out_di;
23049 +
23050 +out_fill:
23051 +       generic_fillattr(idmap, inode, st);
23052 +out_di:
23053 +       di_read_unlock(dentry, AuLock_IR);
23054 +out_si:
23055 +       si_read_unlock(sb);
23056 +out:
23057 +       AuTraceErr(err);
23058 +       return err;
23059 +}
23060 +
23061 +/* ---------------------------------------------------------------------- */
23062 +
23063 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
23064 +                                struct delayed_call *done)
23065 +{
23066 +       const char *ret;
23067 +       struct dentry *h_dentry;
23068 +       struct inode *h_inode;
23069 +       int err;
23070 +       aufs_bindex_t bindex;
23071 +
23072 +       ret = NULL; /* suppress a warning */
23073 +       err = -ECHILD;
23074 +       if (!dentry)
23075 +               goto out;
23076 +
23077 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23078 +       if (unlikely(err))
23079 +               goto out;
23080 +
23081 +       err = au_d_hashed_positive(dentry);
23082 +       if (unlikely(err))
23083 +               goto out_unlock;
23084 +
23085 +       err = -EINVAL;
23086 +       inode = d_inode(dentry);
23087 +       bindex = au_ibtop(inode);
23088 +       h_inode = au_h_iptr(inode, bindex);
23089 +       if (unlikely(!h_inode->i_op->get_link))
23090 +               goto out_unlock;
23091 +
23092 +       err = -EBUSY;
23093 +       h_dentry = NULL;
23094 +       if (au_dbtop(dentry) <= bindex) {
23095 +               h_dentry = au_h_dptr(dentry, bindex);
23096 +               if (h_dentry)
23097 +                       dget(h_dentry);
23098 +       }
23099 +       if (!h_dentry) {
23100 +               h_dentry = d_find_any_alias(h_inode);
23101 +               if (IS_ERR(h_dentry)) {
23102 +                       err = PTR_ERR(h_dentry);
23103 +                       goto out_unlock;
23104 +               }
23105 +       }
23106 +       if (unlikely(!h_dentry))
23107 +               goto out_unlock;
23108 +
23109 +       err = 0;
23110 +       AuDbg("%ps\n", h_inode->i_op->get_link);
23111 +       AuDbgDentry(h_dentry);
23112 +       ret = vfs_get_link(h_dentry, done);
23113 +       dput(h_dentry);
23114 +       if (IS_ERR(ret))
23115 +               err = PTR_ERR(ret);
23116 +
23117 +out_unlock:
23118 +       aufs_read_unlock(dentry, AuLock_IR);
23119 +out:
23120 +       if (unlikely(err))
23121 +               ret = ERR_PTR(err);
23122 +       AuTraceErrPtr(ret);
23123 +       return ret;
23124 +}
23125 +
23126 +/* ---------------------------------------------------------------------- */
23127 +
23128 +static int au_is_special(struct inode *inode)
23129 +{
23130 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23131 +}
23132 +
23133 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
23134 +                           int flags)
23135 +{
23136 +       int err;
23137 +       aufs_bindex_t bindex;
23138 +       struct super_block *sb;
23139 +       struct inode *h_inode;
23140 +       struct vfsmount *h_mnt;
23141 +
23142 +       sb = inode->i_sb;
23143 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23144 +                 "unexpected s_flags 0x%lx", sb->s_flags);
23145 +
23146 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
23147 +       lockdep_off();
23148 +       si_read_lock(sb, AuLock_FLUSH);
23149 +       ii_write_lock_child(inode);
23150 +
23151 +       err = 0;
23152 +       bindex = au_ibtop(inode);
23153 +       h_inode = au_h_iptr(inode, bindex);
23154 +       if (!au_test_ro(sb, bindex, inode)) {
23155 +               h_mnt = au_sbr_mnt(sb, bindex);
23156 +               err = vfsub_mnt_want_write(h_mnt);
23157 +               if (!err) {
23158 +                       err = vfsub_update_time(h_inode, ts, flags);
23159 +                       vfsub_mnt_drop_write(h_mnt);
23160 +               }
23161 +       } else if (au_is_special(h_inode)) {
23162 +               /*
23163 +                * Never copy-up here.
23164 +                * These special files may already be opened and used for
23165 +                * communicating. If we copied it up, then the communication
23166 +                * would be corrupted.
23167 +                */
23168 +               AuWarn1("timestamps for i%lu are ignored "
23169 +                       "since it is on readonly branch (hi%lu).\n",
23170 +                       inode->i_ino, h_inode->i_ino);
23171 +       } else if (flags & ~S_ATIME) {
23172 +               err = -EIO;
23173 +               AuIOErr1("unexpected flags 0x%x\n", flags);
23174 +               AuDebugOn(1);
23175 +       }
23176 +
23177 +       if (!err)
23178 +               au_cpup_attr_timesizes(inode);
23179 +       ii_write_unlock(inode);
23180 +       si_read_unlock(sb);
23181 +       lockdep_on();
23182 +
23183 +       if (!err && (flags & S_VERSION))
23184 +               inode_inc_iversion(inode);
23185 +
23186 +       return err;
23187 +}
23188 +
23189 +/* ---------------------------------------------------------------------- */
23190 +
23191 +/* no getattr version will be set by module.c:aufs_init() */
23192 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23193 +       aufs_iop[] = {
23194 +       [AuIop_SYMLINK] = {
23195 +               .permission     = aufs_permission,
23196 +#ifdef CONFIG_FS_POSIX_ACL
23197 +               .get_inode_acl  = aufs_get_inode_acl,
23198 +               .get_acl        = aufs_get_acl,
23199 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
23200 +#endif
23201 +
23202 +               .setattr        = aufs_setattr,
23203 +               .getattr        = aufs_getattr,
23204 +
23205 +#ifdef CONFIG_AUFS_XATTR
23206 +               .listxattr      = aufs_listxattr,
23207 +#endif
23208 +
23209 +               .get_link       = aufs_get_link
23210 +
23211 +               /* .update_time = aufs_update_time */
23212 +       },
23213 +       [AuIop_DIR] = {
23214 +               .create         = aufs_create,
23215 +               .lookup         = aufs_lookup,
23216 +               .link           = aufs_link,
23217 +               .unlink         = aufs_unlink,
23218 +               .symlink        = aufs_symlink,
23219 +               .mkdir          = aufs_mkdir,
23220 +               .rmdir          = aufs_rmdir,
23221 +               .mknod          = aufs_mknod,
23222 +               .rename         = aufs_rename,
23223 +
23224 +               .permission     = aufs_permission,
23225 +#ifdef CONFIG_FS_POSIX_ACL
23226 +               .get_inode_acl  = aufs_get_inode_acl,
23227 +               .get_acl        = aufs_get_acl,
23228 +               .set_acl        = aufs_set_acl,
23229 +#endif
23230 +
23231 +               .setattr        = aufs_setattr,
23232 +               .getattr        = aufs_getattr,
23233 +
23234 +#ifdef CONFIG_AUFS_XATTR
23235 +               .listxattr      = aufs_listxattr,
23236 +#endif
23237 +
23238 +               .update_time    = aufs_update_time,
23239 +               .atomic_open    = aufs_atomic_open,
23240 +               .tmpfile        = aufs_tmpfile
23241 +       },
23242 +       [AuIop_OTHER] = {
23243 +               .permission     = aufs_permission,
23244 +#ifdef CONFIG_FS_POSIX_ACL
23245 +               .get_inode_acl  = aufs_get_inode_acl,
23246 +               .get_acl        = aufs_get_acl,
23247 +               .set_acl        = aufs_set_acl,
23248 +#endif
23249 +
23250 +               .setattr        = aufs_setattr,
23251 +               .getattr        = aufs_getattr,
23252 +
23253 +#ifdef CONFIG_AUFS_XATTR
23254 +               .listxattr      = aufs_listxattr,
23255 +#endif
23256 +
23257 +               .update_time    = aufs_update_time
23258 +       }
23259 +};
23260 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
23261 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
23262 +++ linux/fs/aufs/i_op_del.c    2023-08-28 12:34:39.956636132 +0200
23263 @@ -0,0 +1,522 @@
23264 +// SPDX-License-Identifier: GPL-2.0
23265 +/*
23266 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23267 + *
23268 + * This program is free software; you can redistribute it and/or modify
23269 + * it under the terms of the GNU General Public License as published by
23270 + * the Free Software Foundation; either version 2 of the License, or
23271 + * (at your option) any later version.
23272 + *
23273 + * This program is distributed in the hope that it will be useful,
23274 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23275 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23276 + * GNU General Public License for more details.
23277 + *
23278 + * You should have received a copy of the GNU General Public License
23279 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23280 + */
23281 +
23282 +/*
23283 + * inode operations (del entry)
23284 + */
23285 +
23286 +#include <linux/iversion.h>
23287 +#include "aufs.h"
23288 +
23289 +/*
23290 + * decide if a new whiteout for @dentry is necessary or not.
23291 + * when it is necessary, prepare the parent dir for the upper branch whose
23292 + * branch index is @bcpup for creation. the actual creation of the whiteout will
23293 + * be done by caller.
23294 + * return value:
23295 + * 0: wh is unnecessary
23296 + * plus: wh is necessary
23297 + * minus: error
23298 + */
23299 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
23300 +{
23301 +       int need_wh, err;
23302 +       aufs_bindex_t btop;
23303 +       struct super_block *sb;
23304 +
23305 +       sb = dentry->d_sb;
23306 +       btop = au_dbtop(dentry);
23307 +       if (*bcpup < 0) {
23308 +               *bcpup = btop;
23309 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
23310 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
23311 +                       *bcpup = err;
23312 +                       if (unlikely(err < 0))
23313 +                               goto out;
23314 +               }
23315 +       } else
23316 +               AuDebugOn(btop < *bcpup
23317 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
23318 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
23319 +
23320 +       if (*bcpup != btop) {
23321 +               err = au_cpup_dirs(dentry, *bcpup);
23322 +               if (unlikely(err))
23323 +                       goto out;
23324 +               need_wh = 1;
23325 +       } else {
23326 +               struct au_dinfo *dinfo, *tmp;
23327 +
23328 +               need_wh = -ENOMEM;
23329 +               dinfo = au_di(dentry);
23330 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23331 +               if (tmp) {
23332 +                       au_di_cp(tmp, dinfo);
23333 +                       au_di_swap(tmp, dinfo);
23334 +                       /* returns the number of positive dentries */
23335 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
23336 +                                                /* AuLkup_IGNORE_PERM */ 0);
23337 +                       au_di_swap(tmp, dinfo);
23338 +                       au_rw_write_unlock(&tmp->di_rwsem);
23339 +                       au_di_free(tmp);
23340 +               }
23341 +       }
23342 +       AuDbg("need_wh %d\n", need_wh);
23343 +       err = need_wh;
23344 +
23345 +out:
23346 +       return err;
23347 +}
23348 +
23349 +/*
23350 + * simple tests for the del-entry operations.
23351 + * following the checks in vfs, plus the parent-child relationship.
23352 + */
23353 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23354 +              struct dentry *h_parent, int isdir)
23355 +{
23356 +       int err;
23357 +       umode_t h_mode;
23358 +       struct dentry *h_dentry, *h_latest;
23359 +       struct inode *h_inode;
23360 +       struct path h_ppath;
23361 +       struct super_block *sb;
23362 +       struct au_branch *br;
23363 +       struct mnt_idmap *h_idmap;
23364 +
23365 +       h_dentry = au_h_dptr(dentry, bindex);
23366 +       if (d_really_is_positive(dentry)) {
23367 +               err = -ENOENT;
23368 +               if (unlikely(d_is_negative(h_dentry)))
23369 +                       goto out;
23370 +               h_inode = d_inode(h_dentry);
23371 +               if (unlikely(!h_inode->i_nlink))
23372 +                       goto out;
23373 +
23374 +               h_mode = h_inode->i_mode;
23375 +               if (!isdir) {
23376 +                       err = -EISDIR;
23377 +                       if (unlikely(S_ISDIR(h_mode)))
23378 +                               goto out;
23379 +               } else if (unlikely(!S_ISDIR(h_mode))) {
23380 +                       err = -ENOTDIR;
23381 +                       goto out;
23382 +               }
23383 +       } else {
23384 +               /* rename(2) case */
23385 +               err = -EIO;
23386 +               if (unlikely(d_is_positive(h_dentry)))
23387 +                       goto out;
23388 +       }
23389 +
23390 +       err = -ENOENT;
23391 +       /* expected parent dir is locked */
23392 +       if (unlikely(h_parent != h_dentry->d_parent))
23393 +               goto out;
23394 +       err = 0;
23395 +
23396 +       /*
23397 +        * rmdir a dir may break the consistency on some filesystem.
23398 +        * let's try heavy test.
23399 +        */
23400 +       err = -EACCES;
23401 +       sb = dentry->d_sb;
23402 +       br = au_sbr(sb, bindex);
23403 +       h_idmap = au_br_idmap(br);
23404 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
23405 +                    && au_test_h_perm(h_idmap, d_inode(h_parent),
23406 +                                      MAY_EXEC | MAY_WRITE)))
23407 +               goto out;
23408 +
23409 +       h_ppath.dentry = h_parent;
23410 +       h_ppath.mnt = au_br_mnt(br);
23411 +       h_latest = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
23412 +       err = -EIO;
23413 +       if (IS_ERR(h_latest))
23414 +               goto out;
23415 +       if (h_latest == h_dentry)
23416 +               err = 0;
23417 +       dput(h_latest);
23418 +
23419 +out:
23420 +       return err;
23421 +}
23422 +
23423 +/*
23424 + * decide the branch where we operate for @dentry. the branch index will be set
23425 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
23426 + * dir for reverting.
23427 + * when a new whiteout is necessary, create it.
23428 + */
23429 +static struct dentry*
23430 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23431 +                   struct au_dtime *dt, struct au_pin *pin)
23432 +{
23433 +       struct dentry *wh_dentry;
23434 +       struct super_block *sb;
23435 +       struct path h_path;
23436 +       int err, need_wh;
23437 +       unsigned int udba;
23438 +       aufs_bindex_t bcpup;
23439 +
23440 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23441 +       wh_dentry = ERR_PTR(need_wh);
23442 +       if (unlikely(need_wh < 0))
23443 +               goto out;
23444 +
23445 +       sb = dentry->d_sb;
23446 +       udba = au_opt_udba(sb);
23447 +       bcpup = *rbcpup;
23448 +       err = au_pin(pin, dentry, bcpup, udba,
23449 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23450 +       wh_dentry = ERR_PTR(err);
23451 +       if (unlikely(err))
23452 +               goto out;
23453 +
23454 +       h_path.dentry = au_pinned_h_parent(pin);
23455 +       if (udba != AuOpt_UDBA_NONE
23456 +           && au_dbtop(dentry) == bcpup) {
23457 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23458 +               wh_dentry = ERR_PTR(err);
23459 +               if (unlikely(err))
23460 +                       goto out_unpin;
23461 +       }
23462 +
23463 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
23464 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23465 +       wh_dentry = NULL;
23466 +       if (!need_wh)
23467 +               goto out; /* success, no need to create whiteout */
23468 +
23469 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23470 +       if (IS_ERR(wh_dentry))
23471 +               goto out_unpin;
23472 +
23473 +       /* returns with the parent is locked and wh_dentry is dget-ed */
23474 +       goto out; /* success */
23475 +
23476 +out_unpin:
23477 +       au_unpin(pin);
23478 +out:
23479 +       return wh_dentry;
23480 +}
23481 +
23482 +/*
23483 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
23484 + * in order to be revertible and save time for removing many child whiteouts
23485 + * under the dir.
23486 + * returns 1 when there are too many child whiteout and caller should remove
23487 + * them asynchronously. returns 0 when the number of children is enough small to
23488 + * remove now or the branch fs is a remote fs.
23489 + * otherwise return an error.
23490 + */
23491 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23492 +                          struct au_nhash *whlist, struct inode *dir)
23493 +{
23494 +       int rmdir_later, err, dirwh;
23495 +       struct dentry *h_dentry;
23496 +       struct super_block *sb;
23497 +       struct inode *inode;
23498 +
23499 +       sb = dentry->d_sb;
23500 +       SiMustAnyLock(sb);
23501 +       h_dentry = au_h_dptr(dentry, bindex);
23502 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23503 +       if (unlikely(err))
23504 +               goto out;
23505 +
23506 +       /* stop monitoring */
23507 +       inode = d_inode(dentry);
23508 +       au_hn_free(au_hi(inode, bindex));
23509 +
23510 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
23511 +               dirwh = au_sbi(sb)->si_dirwh;
23512 +               rmdir_later = (dirwh <= 1);
23513 +               if (!rmdir_later)
23514 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23515 +                                                             dirwh);
23516 +               if (rmdir_later)
23517 +                       return rmdir_later;
23518 +       }
23519 +
23520 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23521 +       if (unlikely(err)) {
23522 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23523 +                       h_dentry, bindex, err);
23524 +               err = 0;
23525 +       }
23526 +
23527 +out:
23528 +       AuTraceErr(err);
23529 +       return err;
23530 +}
23531 +
23532 +/*
23533 + * final procedure for deleting a entry.
23534 + * maintain dentry and iattr.
23535 + */
23536 +static void epilog(struct inode *dir, struct dentry *dentry,
23537 +                  aufs_bindex_t bindex)
23538 +{
23539 +       struct inode *inode;
23540 +
23541 +       inode = d_inode(dentry);
23542 +       d_drop(dentry);
23543 +       inode->i_ctime = dir->i_ctime;
23544 +
23545 +       au_dir_ts(dir, bindex);
23546 +       inode_inc_iversion(dir);
23547 +}
23548 +
23549 +/*
23550 + * when an error happened, remove the created whiteout and revert everything.
23551 + */
23552 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23553 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
23554 +                    struct dentry *dentry, struct au_dtime *dt)
23555 +{
23556 +       int rerr;
23557 +       struct path h_path = {
23558 +               .dentry = wh_dentry,
23559 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
23560 +       };
23561 +
23562 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
23563 +       if (!rerr) {
23564 +               au_set_dbwh(dentry, bwh);
23565 +               au_dtime_revert(dt);
23566 +               return 0;
23567 +       }
23568 +
23569 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
23570 +       return -EIO;
23571 +}
23572 +
23573 +/* ---------------------------------------------------------------------- */
23574 +
23575 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
23576 +{
23577 +       int err;
23578 +       aufs_bindex_t bwh, bindex, btop;
23579 +       struct inode *inode, *h_dir, *delegated;
23580 +       struct dentry *parent, *wh_dentry;
23581 +       /* to reduce stack size */
23582 +       struct {
23583 +               struct au_dtime dt;
23584 +               struct au_pin pin;
23585 +               struct path h_path;
23586 +       } *a;
23587 +
23588 +       IMustLock(dir);
23589 +
23590 +       err = -ENOMEM;
23591 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23592 +       if (unlikely(!a))
23593 +               goto out;
23594 +
23595 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23596 +       if (unlikely(err))
23597 +               goto out_free;
23598 +       err = au_d_hashed_positive(dentry);
23599 +       if (unlikely(err))
23600 +               goto out_unlock;
23601 +       inode = d_inode(dentry);
23602 +       IMustLock(inode);
23603 +       err = -EISDIR;
23604 +       if (unlikely(d_is_dir(dentry)))
23605 +               goto out_unlock; /* possible? */
23606 +
23607 +       btop = au_dbtop(dentry);
23608 +       bwh = au_dbwh(dentry);
23609 +       bindex = -1;
23610 +       parent = dentry->d_parent; /* dir inode is locked */
23611 +       di_write_lock_parent(parent);
23612 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23613 +                                       &a->pin);
23614 +       err = PTR_ERR(wh_dentry);
23615 +       if (IS_ERR(wh_dentry))
23616 +               goto out_parent;
23617 +
23618 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23619 +       a->h_path.dentry = au_h_dptr(dentry, btop);
23620 +       dget(a->h_path.dentry);
23621 +       if (bindex == btop) {
23622 +               h_dir = au_pinned_h_dir(&a->pin);
23623 +               delegated = NULL;
23624 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23625 +               if (unlikely(err == -EWOULDBLOCK)) {
23626 +                       pr_warn("cannot retry for NFSv4 delegation"
23627 +                               " for an internal unlink\n");
23628 +                       iput(delegated);
23629 +               }
23630 +       } else {
23631 +               /* dir inode is locked */
23632 +               h_dir = d_inode(wh_dentry->d_parent);
23633 +               IMustLock(h_dir);
23634 +               err = 0;
23635 +       }
23636 +
23637 +       if (!err) {
23638 +               vfsub_drop_nlink(inode);
23639 +               epilog(dir, dentry, bindex);
23640 +
23641 +               /* update target timestamps */
23642 +               if (bindex == btop) {
23643 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23644 +                       /*ignore*/
23645 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23646 +               } else
23647 +                       /* todo: this timestamp may be reverted later */
23648 +                       inode->i_ctime = h_dir->i_ctime;
23649 +               goto out_unpin; /* success */
23650 +       }
23651 +
23652 +       /* revert */
23653 +       if (wh_dentry) {
23654 +               int rerr;
23655 +
23656 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23657 +                                &a->dt);
23658 +               if (rerr)
23659 +                       err = rerr;
23660 +       }
23661 +
23662 +out_unpin:
23663 +       au_unpin(&a->pin);
23664 +       dput(wh_dentry);
23665 +       dput(a->h_path.dentry);
23666 +out_parent:
23667 +       di_write_unlock(parent);
23668 +out_unlock:
23669 +       aufs_read_unlock(dentry, AuLock_DW);
23670 +out_free:
23671 +       au_kfree_rcu(a);
23672 +out:
23673 +       return err;
23674 +}
23675 +
23676 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
23677 +{
23678 +       int err, rmdir_later;
23679 +       aufs_bindex_t bwh, bindex, btop;
23680 +       struct inode *inode;
23681 +       struct dentry *parent, *wh_dentry, *h_dentry;
23682 +       struct au_whtmp_rmdir *args;
23683 +       /* to reduce stack size */
23684 +       struct {
23685 +               struct au_dtime dt;
23686 +               struct au_pin pin;
23687 +       } *a;
23688 +
23689 +       IMustLock(dir);
23690 +
23691 +       err = -ENOMEM;
23692 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23693 +       if (unlikely(!a))
23694 +               goto out;
23695 +
23696 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23697 +       if (unlikely(err))
23698 +               goto out_free;
23699 +       err = au_alive_dir(dentry);
23700 +       if (unlikely(err))
23701 +               goto out_unlock;
23702 +       inode = d_inode(dentry);
23703 +       IMustLock(inode);
23704 +       err = -ENOTDIR;
23705 +       if (unlikely(!d_is_dir(dentry)))
23706 +               goto out_unlock; /* possible? */
23707 +
23708 +       err = -ENOMEM;
23709 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23710 +       if (unlikely(!args))
23711 +               goto out_unlock;
23712 +
23713 +       parent = dentry->d_parent; /* dir inode is locked */
23714 +       di_write_lock_parent(parent);
23715 +       err = au_test_empty(dentry, &args->whlist);
23716 +       if (unlikely(err))
23717 +               goto out_parent;
23718 +
23719 +       btop = au_dbtop(dentry);
23720 +       bwh = au_dbwh(dentry);
23721 +       bindex = -1;
23722 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23723 +                                       &a->pin);
23724 +       err = PTR_ERR(wh_dentry);
23725 +       if (IS_ERR(wh_dentry))
23726 +               goto out_parent;
23727 +
23728 +       h_dentry = au_h_dptr(dentry, btop);
23729 +       dget(h_dentry);
23730 +       rmdir_later = 0;
23731 +       if (bindex == btop) {
23732 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
23733 +               if (err > 0) {
23734 +                       rmdir_later = err;
23735 +                       err = 0;
23736 +               }
23737 +       } else {
23738 +               /* stop monitoring */
23739 +               au_hn_free(au_hi(inode, btop));
23740 +
23741 +               /* dir inode is locked */
23742 +               IMustLock(d_inode(wh_dentry->d_parent));
23743 +               err = 0;
23744 +       }
23745 +
23746 +       if (!err) {
23747 +               vfsub_dead_dir(inode);
23748 +               au_set_dbdiropq(dentry, -1);
23749 +               epilog(dir, dentry, bindex);
23750 +
23751 +               if (rmdir_later) {
23752 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
23753 +                       args = NULL;
23754 +               }
23755 +
23756 +               goto out_unpin; /* success */
23757 +       }
23758 +
23759 +       /* revert */
23760 +       AuLabel(revert);
23761 +       if (wh_dentry) {
23762 +               int rerr;
23763 +
23764 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23765 +                                &a->dt);
23766 +               if (rerr)
23767 +                       err = rerr;
23768 +       }
23769 +
23770 +out_unpin:
23771 +       au_unpin(&a->pin);
23772 +       dput(wh_dentry);
23773 +       dput(h_dentry);
23774 +out_parent:
23775 +       di_write_unlock(parent);
23776 +       if (args)
23777 +               au_whtmp_rmdir_free(args);
23778 +out_unlock:
23779 +       aufs_read_unlock(dentry, AuLock_DW);
23780 +out_free:
23781 +       au_kfree_rcu(a);
23782 +out:
23783 +       AuTraceErr(err);
23784 +       return err;
23785 +}
23786 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
23787 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
23788 +++ linux/fs/aufs/i_op_ren.c    2023-08-28 12:34:39.956636132 +0200
23789 @@ -0,0 +1,1257 @@
23790 +// SPDX-License-Identifier: GPL-2.0
23791 +/*
23792 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23793 + *
23794 + * This program is free software; you can redistribute it and/or modify
23795 + * it under the terms of the GNU General Public License as published by
23796 + * the Free Software Foundation; either version 2 of the License, or
23797 + * (at your option) any later version.
23798 + *
23799 + * This program is distributed in the hope that it will be useful,
23800 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23801 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23802 + * GNU General Public License for more details.
23803 + *
23804 + * You should have received a copy of the GNU General Public License
23805 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23806 + */
23807 +
23808 +/*
23809 + * inode operation (rename entry)
23810 + * todo: this is crazy monster
23811 + */
23812 +
23813 +#include <linux/iversion.h>
23814 +#include "aufs.h"
23815 +
23816 +enum { AuSRC, AuDST, AuSrcDst };
23817 +enum { AuPARENT, AuCHILD, AuParentChild };
23818 +
23819 +#define AuRen_ISDIR_SRC                1
23820 +#define AuRen_ISDIR_DST                (1 << 1)
23821 +#define AuRen_ISSAMEDIR                (1 << 2)
23822 +#define AuRen_WHSRC            (1 << 3)
23823 +#define AuRen_WHDST            (1 << 4)
23824 +#define AuRen_MNT_WRITE                (1 << 5)
23825 +#define AuRen_DT_DSTDIR                (1 << 6)
23826 +#define AuRen_DIROPQ_SRC       (1 << 7)
23827 +#define AuRen_DIROPQ_DST       (1 << 8)
23828 +#define AuRen_DIRREN           (1 << 9)
23829 +#define AuRen_DROPPED_SRC      (1 << 10)
23830 +#define AuRen_DROPPED_DST      (1 << 11)
23831 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
23832 +#define au_fset_ren(flags, name) \
23833 +       do { (flags) |= AuRen_##name; } while (0)
23834 +#define au_fclr_ren(flags, name) \
23835 +       do { (flags) &= ~AuRen_##name; } while (0)
23836 +
23837 +#ifndef CONFIG_AUFS_DIRREN
23838 +#undef AuRen_DIRREN
23839 +#define AuRen_DIRREN           0
23840 +#endif
23841 +
23842 +struct au_ren_args {
23843 +       struct {
23844 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
23845 +                       *wh_dentry;
23846 +               struct inode *dir, *inode;
23847 +               struct au_hinode *hdir, *hinode;
23848 +               struct au_dtime dt[AuParentChild];
23849 +               aufs_bindex_t btop, bdiropq;
23850 +       } sd[AuSrcDst];
23851 +
23852 +#define src_dentry     sd[AuSRC].dentry
23853 +#define src_dir                sd[AuSRC].dir
23854 +#define src_inode      sd[AuSRC].inode
23855 +#define src_h_dentry   sd[AuSRC].h_dentry
23856 +#define src_parent     sd[AuSRC].parent
23857 +#define src_h_parent   sd[AuSRC].h_parent
23858 +#define src_wh_dentry  sd[AuSRC].wh_dentry
23859 +#define src_hdir       sd[AuSRC].hdir
23860 +#define src_hinode     sd[AuSRC].hinode
23861 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
23862 +#define src_dt         sd[AuSRC].dt
23863 +#define src_btop       sd[AuSRC].btop
23864 +#define src_bdiropq    sd[AuSRC].bdiropq
23865 +
23866 +#define dst_dentry     sd[AuDST].dentry
23867 +#define dst_dir                sd[AuDST].dir
23868 +#define dst_inode      sd[AuDST].inode
23869 +#define dst_h_dentry   sd[AuDST].h_dentry
23870 +#define dst_parent     sd[AuDST].parent
23871 +#define dst_h_parent   sd[AuDST].h_parent
23872 +#define dst_wh_dentry  sd[AuDST].wh_dentry
23873 +#define dst_hdir       sd[AuDST].hdir
23874 +#define dst_hinode     sd[AuDST].hinode
23875 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
23876 +#define dst_dt         sd[AuDST].dt
23877 +#define dst_btop       sd[AuDST].btop
23878 +#define dst_bdiropq    sd[AuDST].bdiropq
23879 +
23880 +       struct dentry *h_trap;
23881 +       struct au_branch *br;
23882 +       struct path h_path;
23883 +       struct au_nhash whlist;
23884 +       aufs_bindex_t btgt, src_bwh;
23885 +
23886 +       struct {
23887 +               unsigned short auren_flags;
23888 +               unsigned char flags;    /* syscall parameter */
23889 +               unsigned char exchange;
23890 +       } __packed;
23891 +
23892 +       struct au_whtmp_rmdir *thargs;
23893 +       struct dentry *h_dst;
23894 +       struct au_hinode *h_root;
23895 +};
23896 +
23897 +/* ---------------------------------------------------------------------- */
23898 +
23899 +/*
23900 + * functions for reverting.
23901 + * when an error happened in a single rename systemcall, we should revert
23902 + * everything as if nothing happened.
23903 + * we don't need to revert the copied-up/down the parent dir since they are
23904 + * harmless.
23905 + */
23906 +
23907 +#define RevertFailure(fmt, ...) do { \
23908 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23909 +               ##__VA_ARGS__, err, rerr); \
23910 +       err = -EIO; \
23911 +} while (0)
23912 +
23913 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
23914 +{
23915 +       int rerr;
23916 +       struct dentry *d;
23917 +#define src_or_dst(member) a->sd[idx].member
23918 +
23919 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
23920 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23921 +       rerr = au_diropq_remove(d, a->btgt);
23922 +       au_hn_inode_unlock(src_or_dst(hinode));
23923 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
23924 +       if (rerr)
23925 +               RevertFailure("remove diropq %pd", d);
23926 +
23927 +#undef src_or_dst_
23928 +}
23929 +
23930 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23931 +{
23932 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23933 +               au_ren_do_rev_diropq(err, a, AuSRC);
23934 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23935 +               au_ren_do_rev_diropq(err, a, AuDST);
23936 +}
23937 +
23938 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
23939 +{
23940 +       int rerr;
23941 +       struct inode *delegated;
23942 +       struct path h_ppath = {
23943 +               .dentry = a->src_h_parent,
23944 +               .mnt    = a->h_path.mnt
23945 +       };
23946 +
23947 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
23948 +       rerr = PTR_ERR(a->h_path.dentry);
23949 +       if (IS_ERR(a->h_path.dentry)) {
23950 +               RevertFailure("lkup one %pd", a->src_dentry);
23951 +               return;
23952 +       }
23953 +
23954 +       delegated = NULL;
23955 +       rerr = vfsub_rename(a->dst_h_dir,
23956 +                           au_h_dptr(a->src_dentry, a->btgt),
23957 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
23958 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23959 +               pr_warn("cannot retry for NFSv4 delegation"
23960 +                       " for an internal rename\n");
23961 +               iput(delegated);
23962 +       }
23963 +       d_drop(a->h_path.dentry);
23964 +       dput(a->h_path.dentry);
23965 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23966 +       if (rerr)
23967 +               RevertFailure("rename %pd", a->src_dentry);
23968 +}
23969 +
23970 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
23971 +{
23972 +       int rerr;
23973 +       struct inode *delegated;
23974 +       struct path h_ppath = {
23975 +               .dentry = a->dst_h_parent,
23976 +               .mnt    = a->h_path.mnt
23977 +       };
23978 +
23979 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
23980 +       rerr = PTR_ERR(a->h_path.dentry);
23981 +       if (IS_ERR(a->h_path.dentry)) {
23982 +               RevertFailure("lkup one %pd", a->dst_dentry);
23983 +               return;
23984 +       }
23985 +       if (d_is_positive(a->h_path.dentry)) {
23986 +               d_drop(a->h_path.dentry);
23987 +               dput(a->h_path.dentry);
23988 +               return;
23989 +       }
23990 +
23991 +       delegated = NULL;
23992 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
23993 +                           &delegated, a->flags);
23994 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23995 +               pr_warn("cannot retry for NFSv4 delegation"
23996 +                       " for an internal rename\n");
23997 +               iput(delegated);
23998 +       }
23999 +       d_drop(a->h_path.dentry);
24000 +       dput(a->h_path.dentry);
24001 +       if (!rerr)
24002 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
24003 +       else
24004 +               RevertFailure("rename %pd", a->h_dst);
24005 +}
24006 +
24007 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
24008 +{
24009 +       int rerr;
24010 +
24011 +       a->h_path.dentry = a->src_wh_dentry;
24012 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
24013 +       au_set_dbwh(a->src_dentry, a->src_bwh);
24014 +       if (rerr)
24015 +               RevertFailure("unlink %pd", a->src_wh_dentry);
24016 +}
24017 +#undef RevertFailure
24018 +
24019 +/* ---------------------------------------------------------------------- */
24020 +
24021 +/*
24022 + * when we have to copyup the renaming entry, do it with the rename-target name
24023 + * in order to minimize the cost (the later actual rename is unnecessary).
24024 + * otherwise rename it on the target branch.
24025 + */
24026 +static int au_ren_or_cpup(struct au_ren_args *a)
24027 +{
24028 +       int err;
24029 +       struct dentry *d;
24030 +       struct inode *delegated;
24031 +
24032 +       d = a->src_dentry;
24033 +       if (au_dbtop(d) == a->btgt) {
24034 +               a->h_path.dentry = a->dst_h_dentry;
24035 +               AuDebugOn(au_dbtop(d) != a->btgt);
24036 +               delegated = NULL;
24037 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
24038 +                                  a->dst_h_dir, &a->h_path, &delegated,
24039 +                                  a->flags);
24040 +               if (unlikely(err == -EWOULDBLOCK)) {
24041 +                       pr_warn("cannot retry for NFSv4 delegation"
24042 +                               " for an internal rename\n");
24043 +                       iput(delegated);
24044 +               }
24045 +       } else
24046 +               BUG();
24047 +
24048 +       if (!err && a->h_dst)
24049 +               /* it will be set to dinfo later */
24050 +               dget(a->h_dst);
24051 +
24052 +       return err;
24053 +}
24054 +
24055 +/* cf. aufs_rmdir() */
24056 +static int au_ren_del_whtmp(struct au_ren_args *a)
24057 +{
24058 +       int err;
24059 +       struct inode *dir;
24060 +
24061 +       dir = a->dst_dir;
24062 +       SiMustAnyLock(dir->i_sb);
24063 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
24064 +                                    au_sbi(dir->i_sb)->si_dirwh)
24065 +           || au_test_fs_remote(a->h_dst->d_sb)) {
24066 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
24067 +               if (unlikely(err))
24068 +                       pr_warn("failed removing whtmp dir %pd (%d), "
24069 +                               "ignored.\n", a->h_dst, err);
24070 +       } else {
24071 +               au_nhash_wh_free(&a->thargs->whlist);
24072 +               a->thargs->whlist = a->whlist;
24073 +               a->whlist.nh_num = 0;
24074 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24075 +               dput(a->h_dst);
24076 +               a->thargs = NULL;
24077 +       }
24078 +
24079 +       return 0;
24080 +}
24081 +
24082 +/* make it 'opaque' dir. */
24083 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
24084 +{
24085 +       int err;
24086 +       struct dentry *d, *diropq;
24087 +#define src_or_dst(member) a->sd[idx].member
24088 +
24089 +       err = 0;
24090 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
24091 +       src_or_dst(bdiropq) = au_dbdiropq(d);
24092 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24093 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24094 +       diropq = au_diropq_create(d, a->btgt);
24095 +       au_hn_inode_unlock(src_or_dst(hinode));
24096 +       if (IS_ERR(diropq))
24097 +               err = PTR_ERR(diropq);
24098 +       else
24099 +               dput(diropq);
24100 +
24101 +#undef src_or_dst_
24102 +       return err;
24103 +}
24104 +
24105 +static int au_ren_diropq(struct au_ren_args *a)
24106 +{
24107 +       int err;
24108 +       unsigned char always;
24109 +       struct dentry *d;
24110 +
24111 +       err = 0;
24112 +       d = a->dst_dentry; /* already renamed on the branch */
24113 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24114 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24115 +           && !au_ftest_ren(a->auren_flags, DIRREN)
24116 +           && a->btgt != au_dbdiropq(a->src_dentry)
24117 +           && (a->dst_wh_dentry
24118 +               || a->btgt <= au_dbdiropq(d)
24119 +               /* hide the lower to keep xino */
24120 +               /* the lowers may not be a dir, but we hide them anyway */
24121 +               || a->btgt < au_dbbot(d)
24122 +               || always)) {
24123 +               AuDbg("here\n");
24124 +               err = au_ren_do_diropq(a, AuSRC);
24125 +               if (unlikely(err))
24126 +                       goto out;
24127 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
24128 +       }
24129 +       if (!a->exchange)
24130 +               goto out; /* success */
24131 +
24132 +       d = a->src_dentry; /* already renamed on the branch */
24133 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24134 +           && a->btgt != au_dbdiropq(a->dst_dentry)
24135 +           && (a->btgt < au_dbdiropq(d)
24136 +               || a->btgt < au_dbbot(d)
24137 +               || always)) {
24138 +               AuDbgDentry(a->src_dentry);
24139 +               AuDbgDentry(a->dst_dentry);
24140 +               err = au_ren_do_diropq(a, AuDST);
24141 +               if (unlikely(err))
24142 +                       goto out_rev_src;
24143 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
24144 +       }
24145 +       goto out; /* success */
24146 +
24147 +out_rev_src:
24148 +       AuDbg("err %d, reverting src\n", err);
24149 +       au_ren_rev_diropq(err, a);
24150 +out:
24151 +       return err;
24152 +}
24153 +
24154 +static int do_rename(struct au_ren_args *a)
24155 +{
24156 +       int err;
24157 +       struct dentry *d, *h_d;
24158 +
24159 +       if (!a->exchange) {
24160 +               /* prepare workqueue args for asynchronous rmdir */
24161 +               h_d = a->dst_h_dentry;
24162 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24163 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
24164 +                   && d_is_positive(h_d)) {
24165 +                       err = -ENOMEM;
24166 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24167 +                                                        GFP_NOFS);
24168 +                       if (unlikely(!a->thargs))
24169 +                               goto out;
24170 +                       a->h_dst = dget(h_d);
24171 +               }
24172 +
24173 +               /* create whiteout for src_dentry */
24174 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
24175 +                       a->src_bwh = au_dbwh(a->src_dentry);
24176 +                       AuDebugOn(a->src_bwh >= 0);
24177 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24178 +                                                       a->src_h_parent);
24179 +                       err = PTR_ERR(a->src_wh_dentry);
24180 +                       if (IS_ERR(a->src_wh_dentry))
24181 +                               goto out_thargs;
24182 +               }
24183 +
24184 +               /* lookup whiteout for dentry */
24185 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
24186 +                       h_d = au_wh_lkup(a->dst_h_parent,
24187 +                                        &a->dst_dentry->d_name, a->br);
24188 +                       err = PTR_ERR(h_d);
24189 +                       if (IS_ERR(h_d))
24190 +                               goto out_whsrc;
24191 +                       if (d_is_negative(h_d))
24192 +                               dput(h_d);
24193 +                       else
24194 +                               a->dst_wh_dentry = h_d;
24195 +               }
24196 +
24197 +               /* rename dentry to tmpwh */
24198 +               if (a->thargs) {
24199 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
24200 +                       if (unlikely(err))
24201 +                               goto out_whdst;
24202 +
24203 +                       d = a->dst_dentry;
24204 +                       au_set_h_dptr(d, a->btgt, NULL);
24205 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
24206 +                       if (unlikely(err))
24207 +                               goto out_whtmp;
24208 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
24209 +               }
24210 +       }
24211 +
24212 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
24213 +#if 0 /* debugging */
24214 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24215 +              && d_is_positive(a->dst_h_dentry)
24216 +              && a->src_btop != a->btgt);
24217 +#endif
24218 +
24219 +       /* rename by vfs_rename or cpup */
24220 +       err = au_ren_or_cpup(a);
24221 +       if (unlikely(err))
24222 +               /* leave the copied-up one */
24223 +               goto out_whtmp;
24224 +
24225 +       /* make dir opaque */
24226 +       err = au_ren_diropq(a);
24227 +       if (unlikely(err))
24228 +               goto out_rename;
24229 +
24230 +       /* update target timestamps */
24231 +       if (a->exchange) {
24232 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24233 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24234 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24235 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24236 +       }
24237 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
24238 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24239 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24240 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24241 +
24242 +       if (!a->exchange) {
24243 +               /* remove whiteout for dentry */
24244 +               if (a->dst_wh_dentry) {
24245 +                       a->h_path.dentry = a->dst_wh_dentry;
24246 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24247 +                                                 a->dst_dentry);
24248 +                       if (unlikely(err))
24249 +                               goto out_diropq;
24250 +               }
24251 +
24252 +               /* remove whtmp */
24253 +               if (a->thargs)
24254 +                       au_ren_del_whtmp(a); /* ignore this error */
24255 +
24256 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24257 +       }
24258 +       err = 0;
24259 +       goto out_success;
24260 +
24261 +out_diropq:
24262 +       au_ren_rev_diropq(err, a);
24263 +out_rename:
24264 +       au_ren_rev_rename(err, a);
24265 +       dput(a->h_dst);
24266 +out_whtmp:
24267 +       if (a->thargs)
24268 +               au_ren_rev_whtmp(err, a);
24269 +out_whdst:
24270 +       dput(a->dst_wh_dentry);
24271 +       a->dst_wh_dentry = NULL;
24272 +out_whsrc:
24273 +       if (a->src_wh_dentry)
24274 +               au_ren_rev_whsrc(err, a);
24275 +out_success:
24276 +       dput(a->src_wh_dentry);
24277 +       dput(a->dst_wh_dentry);
24278 +out_thargs:
24279 +       if (a->thargs) {
24280 +               dput(a->h_dst);
24281 +               au_whtmp_rmdir_free(a->thargs);
24282 +               a->thargs = NULL;
24283 +       }
24284 +out:
24285 +       return err;
24286 +}
24287 +
24288 +/* ---------------------------------------------------------------------- */
24289 +
24290 +/*
24291 + * test if @dentry dir can be rename destination or not.
24292 + * success means, it is a logically empty dir.
24293 + */
24294 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
24295 +{
24296 +       return au_test_empty(dentry, whlist);
24297 +}
24298 +
24299 +/*
24300 + * test if @a->src_dentry dir can be rename source or not.
24301 + * if it can, return 0.
24302 + * success means,
24303 + * - it is a logically empty dir.
24304 + * - or, it exists on writable branch and has no children including whiteouts
24305 + *   on the lower branch unless DIRREN is on.
24306 + */
24307 +static int may_rename_srcdir(struct au_ren_args *a)
24308 +{
24309 +       int err;
24310 +       unsigned int rdhash;
24311 +       aufs_bindex_t btop, btgt;
24312 +       struct dentry *dentry;
24313 +       struct super_block *sb;
24314 +       struct au_sbinfo *sbinfo;
24315 +
24316 +       dentry = a->src_dentry;
24317 +       sb = dentry->d_sb;
24318 +       sbinfo = au_sbi(sb);
24319 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24320 +               au_fset_ren(a->auren_flags, DIRREN);
24321 +
24322 +       btgt = a->btgt;
24323 +       btop = au_dbtop(dentry);
24324 +       if (btop != btgt) {
24325 +               struct au_nhash whlist;
24326 +
24327 +               SiMustAnyLock(sb);
24328 +               rdhash = sbinfo->si_rdhash;
24329 +               if (!rdhash)
24330 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24331 +                                                          dentry));
24332 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24333 +               if (unlikely(err))
24334 +                       goto out;
24335 +               err = au_test_empty(dentry, &whlist);
24336 +               au_nhash_wh_free(&whlist);
24337 +               goto out;
24338 +       }
24339 +
24340 +       if (btop == au_dbtaildir(dentry))
24341 +               return 0; /* success */
24342 +
24343 +       err = au_test_empty_lower(dentry);
24344 +
24345 +out:
24346 +       if (err == -ENOTEMPTY) {
24347 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
24348 +                       err = 0;
24349 +               } else {
24350 +                       AuWarn1("renaming dir who has child(ren) on multiple "
24351 +                               "branches, is not supported\n");
24352 +                       err = -EXDEV;
24353 +               }
24354 +       }
24355 +       return err;
24356 +}
24357 +
24358 +/* side effect: sets whlist and h_dentry */
24359 +static int au_ren_may_dir(struct au_ren_args *a)
24360 +{
24361 +       int err;
24362 +       unsigned int rdhash;
24363 +       struct dentry *d;
24364 +
24365 +       d = a->dst_dentry;
24366 +       SiMustAnyLock(d->d_sb);
24367 +
24368 +       err = 0;
24369 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
24370 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
24371 +               if (!rdhash)
24372 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24373 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24374 +               if (unlikely(err))
24375 +                       goto out;
24376 +
24377 +               if (!a->exchange) {
24378 +                       au_set_dbtop(d, a->dst_btop);
24379 +                       err = may_rename_dstdir(d, &a->whlist);
24380 +                       au_set_dbtop(d, a->btgt);
24381 +               } else
24382 +                       err = may_rename_srcdir(a);
24383 +       }
24384 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
24385 +       if (unlikely(err))
24386 +               goto out;
24387 +
24388 +       d = a->src_dentry;
24389 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
24390 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24391 +               err = may_rename_srcdir(a);
24392 +               if (unlikely(err)) {
24393 +                       au_nhash_wh_free(&a->whlist);
24394 +                       a->whlist.nh_num = 0;
24395 +               }
24396 +       }
24397 +out:
24398 +       return err;
24399 +}
24400 +
24401 +/* ---------------------------------------------------------------------- */
24402 +
24403 +/*
24404 + * simple tests for rename.
24405 + * following the checks in vfs, plus the parent-child relationship.
24406 + */
24407 +static int au_may_ren(struct au_ren_args *a)
24408 +{
24409 +       int err, isdir;
24410 +       struct inode *h_inode;
24411 +
24412 +       if (a->src_btop == a->btgt) {
24413 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
24414 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
24415 +               if (unlikely(err))
24416 +                       goto out;
24417 +               err = -EINVAL;
24418 +               if (unlikely(a->src_h_dentry == a->h_trap))
24419 +                       goto out;
24420 +       }
24421 +
24422 +       err = 0;
24423 +       if (a->dst_btop != a->btgt)
24424 +               goto out;
24425 +
24426 +       err = -ENOTEMPTY;
24427 +       if (unlikely(a->dst_h_dentry == a->h_trap))
24428 +               goto out;
24429 +
24430 +       err = -EIO;
24431 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
24432 +       if (d_really_is_negative(a->dst_dentry)) {
24433 +               if (d_is_negative(a->dst_h_dentry))
24434 +                       err = au_may_add(a->dst_dentry, a->btgt,
24435 +                                        a->dst_h_parent, isdir);
24436 +       } else {
24437 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
24438 +                       goto out;
24439 +               h_inode = d_inode(a->dst_h_dentry);
24440 +               if (h_inode->i_nlink)
24441 +                       err = au_may_del(a->dst_dentry, a->btgt,
24442 +                                        a->dst_h_parent, isdir);
24443 +       }
24444 +
24445 +out:
24446 +       if (unlikely(err == -ENOENT || err == -EEXIST))
24447 +               err = -EIO;
24448 +       AuTraceErr(err);
24449 +       return err;
24450 +}
24451 +
24452 +/* ---------------------------------------------------------------------- */
24453 +
24454 +/*
24455 + * locking order
24456 + * (VFS)
24457 + * - src_dir and dir by lock_rename()
24458 + * - inode if exists
24459 + * (aufs)
24460 + * - lock all
24461 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24462 + *     + si_read_lock
24463 + *     + di_write_lock2_child()
24464 + *       + di_write_lock_child()
24465 + *        + ii_write_lock_child()
24466 + *       + di_write_lock_child2()
24467 + *        + ii_write_lock_child2()
24468 + *     + src_parent and parent
24469 + *       + di_write_lock_parent()
24470 + *        + ii_write_lock_parent()
24471 + *       + di_write_lock_parent2()
24472 + *        + ii_write_lock_parent2()
24473 + *   + lower src_dir and dir by vfsub_lock_rename()
24474 + *   + verify the every relationships between child and parent. if any
24475 + *     of them failed, unlock all and return -EBUSY.
24476 + */
24477 +static void au_ren_unlock(struct au_ren_args *a)
24478 +{
24479 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24480 +                           a->dst_h_parent, a->dst_hdir);
24481 +       if (au_ftest_ren(a->auren_flags, DIRREN)
24482 +           && a->h_root)
24483 +               au_hn_inode_unlock(a->h_root);
24484 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
24485 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
24486 +}
24487 +
24488 +static int au_ren_lock(struct au_ren_args *a)
24489 +{
24490 +       int err;
24491 +       unsigned int udba;
24492 +
24493 +       err = 0;
24494 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24495 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
24496 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24497 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
24498 +
24499 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
24500 +       if (unlikely(err))
24501 +               goto out;
24502 +       au_fset_ren(a->auren_flags, MNT_WRITE);
24503 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24504 +               struct dentry *root;
24505 +               struct inode *dir;
24506 +
24507 +               /*
24508 +                * sbinfo is already locked, so this ii_read_lock is
24509 +                * unnecessary. but our debugging feature checks it.
24510 +                */
24511 +               root = a->src_inode->i_sb->s_root;
24512 +               if (root != a->src_parent && root != a->dst_parent) {
24513 +                       dir = d_inode(root);
24514 +                       ii_read_lock_parent3(dir);
24515 +                       a->h_root = au_hi(dir, a->btgt);
24516 +                       ii_read_unlock(dir);
24517 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24518 +               }
24519 +       }
24520 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24521 +                                     a->dst_h_parent, a->dst_hdir);
24522 +       udba = au_opt_udba(a->src_dentry->d_sb);
24523 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24524 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
24525 +               err = au_busy_or_stale();
24526 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
24527 +               err = au_h_verify(a->src_h_dentry, udba,
24528 +                                 d_inode(a->src_h_parent), a->src_h_parent,
24529 +                                 a->br);
24530 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
24531 +               err = au_h_verify(a->dst_h_dentry, udba,
24532 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
24533 +                                 a->br);
24534 +       if (!err)
24535 +               goto out; /* success */
24536 +
24537 +       err = au_busy_or_stale();
24538 +       au_ren_unlock(a);
24539 +
24540 +out:
24541 +       return err;
24542 +}
24543 +
24544 +/* ---------------------------------------------------------------------- */
24545 +
24546 +static void au_ren_refresh_dir(struct au_ren_args *a)
24547 +{
24548 +       struct inode *dir;
24549 +
24550 +       dir = a->dst_dir;
24551 +       inode_inc_iversion(dir);
24552 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24553 +               /* is this updating defined in POSIX? */
24554 +               au_cpup_attr_timesizes(a->src_inode);
24555 +               au_cpup_attr_nlink(dir, /*force*/1);
24556 +       }
24557 +       au_dir_ts(dir, a->btgt);
24558 +
24559 +       if (a->exchange) {
24560 +               dir = a->src_dir;
24561 +               inode_inc_iversion(dir);
24562 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24563 +                       /* is this updating defined in POSIX? */
24564 +                       au_cpup_attr_timesizes(a->dst_inode);
24565 +                       au_cpup_attr_nlink(dir, /*force*/1);
24566 +               }
24567 +               au_dir_ts(dir, a->btgt);
24568 +       }
24569 +
24570 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24571 +               return;
24572 +
24573 +       dir = a->src_dir;
24574 +       inode_inc_iversion(dir);
24575 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
24576 +               au_cpup_attr_nlink(dir, /*force*/1);
24577 +       au_dir_ts(dir, a->btgt);
24578 +}
24579 +
24580 +static void au_ren_refresh(struct au_ren_args *a)
24581 +{
24582 +       aufs_bindex_t bbot, bindex;
24583 +       struct dentry *d, *h_d;
24584 +       struct inode *i, *h_i;
24585 +       struct super_block *sb;
24586 +
24587 +       d = a->dst_dentry;
24588 +       d_drop(d);
24589 +       if (a->h_dst)
24590 +               /* already dget-ed by au_ren_or_cpup() */
24591 +               au_set_h_dptr(d, a->btgt, a->h_dst);
24592 +
24593 +       i = a->dst_inode;
24594 +       if (i) {
24595 +               if (!a->exchange) {
24596 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24597 +                               vfsub_drop_nlink(i);
24598 +                       else {
24599 +                               vfsub_dead_dir(i);
24600 +                               au_cpup_attr_timesizes(i);
24601 +                       }
24602 +                       au_update_dbrange(d, /*do_put_zero*/1);
24603 +               } else
24604 +                       au_cpup_attr_nlink(i, /*force*/1);
24605 +       } else {
24606 +               bbot = a->btgt;
24607 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
24608 +                       au_set_h_dptr(d, bindex, NULL);
24609 +               bbot = au_dbbot(d);
24610 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
24611 +                       au_set_h_dptr(d, bindex, NULL);
24612 +               au_update_dbrange(d, /*do_put_zero*/0);
24613 +       }
24614 +
24615 +       if (a->exchange
24616 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
24617 +               d_drop(a->src_dentry);
24618 +               if (au_ftest_ren(a->auren_flags, DIRREN))
24619 +                       au_set_dbwh(a->src_dentry, -1);
24620 +               return;
24621 +       }
24622 +
24623 +       d = a->src_dentry;
24624 +       au_set_dbwh(d, -1);
24625 +       bbot = au_dbbot(d);
24626 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24627 +               h_d = au_h_dptr(d, bindex);
24628 +               if (h_d)
24629 +                       au_set_h_dptr(d, bindex, NULL);
24630 +       }
24631 +       au_set_dbbot(d, a->btgt);
24632 +
24633 +       sb = d->d_sb;
24634 +       i = a->src_inode;
24635 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24636 +               return; /* success */
24637 +
24638 +       bbot = au_ibbot(i);
24639 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24640 +               h_i = au_h_iptr(i, bindex);
24641 +               if (h_i) {
24642 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24643 +                       /* ignore this error */
24644 +                       au_set_h_iptr(i, bindex, NULL, 0);
24645 +               }
24646 +       }
24647 +       au_set_ibbot(i, a->btgt);
24648 +}
24649 +
24650 +/* ---------------------------------------------------------------------- */
24651 +
24652 +/* mainly for link(2) and rename(2) */
24653 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
24654 +{
24655 +       aufs_bindex_t bdiropq, bwh;
24656 +       struct dentry *parent;
24657 +       struct au_branch *br;
24658 +
24659 +       parent = dentry->d_parent;
24660 +       IMustLock(d_inode(parent)); /* dir is locked */
24661 +
24662 +       bdiropq = au_dbdiropq(parent);
24663 +       bwh = au_dbwh(dentry);
24664 +       br = au_sbr(dentry->d_sb, btgt);
24665 +       if (au_br_rdonly(br)
24666 +           || (0 <= bdiropq && bdiropq < btgt)
24667 +           || (0 <= bwh && bwh < btgt))
24668 +               btgt = -1;
24669 +
24670 +       AuDbg("btgt %d\n", btgt);
24671 +       return btgt;
24672 +}
24673 +
24674 +/* sets src_btop, dst_btop and btgt */
24675 +static int au_ren_wbr(struct au_ren_args *a)
24676 +{
24677 +       int err;
24678 +       struct au_wr_dir_args wr_dir_args = {
24679 +               /* .force_btgt  = -1, */
24680 +               .flags          = AuWrDir_ADD_ENTRY
24681 +       };
24682 +
24683 +       a->src_btop = au_dbtop(a->src_dentry);
24684 +       a->dst_btop = au_dbtop(a->dst_dentry);
24685 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24686 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
24687 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
24688 +       wr_dir_args.force_btgt = a->src_btop;
24689 +       if (a->dst_inode && a->dst_btop < a->src_btop)
24690 +               wr_dir_args.force_btgt = a->dst_btop;
24691 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24692 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24693 +       a->btgt = err;
24694 +       if (a->exchange)
24695 +               au_update_dbtop(a->dst_dentry);
24696 +
24697 +       return err;
24698 +}
24699 +
24700 +static void au_ren_dt(struct au_ren_args *a)
24701 +{
24702 +       a->h_path.dentry = a->src_h_parent;
24703 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
24704 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
24705 +               a->h_path.dentry = a->dst_h_parent;
24706 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24707 +       }
24708 +
24709 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
24710 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24711 +           && !a->exchange)
24712 +               return;
24713 +
24714 +       a->h_path.dentry = a->src_h_dentry;
24715 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
24716 +       if (d_is_positive(a->dst_h_dentry)) {
24717 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
24718 +               a->h_path.dentry = a->dst_h_dentry;
24719 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24720 +       }
24721 +}
24722 +
24723 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
24724 +{
24725 +       struct dentry *h_d;
24726 +       struct inode *h_inode;
24727 +
24728 +       au_dtime_revert(a->src_dt + AuPARENT);
24729 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
24730 +               au_dtime_revert(a->dst_dt + AuPARENT);
24731 +
24732 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
24733 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
24734 +               h_inode = d_inode(h_d);
24735 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
24736 +               au_dtime_revert(a->src_dt + AuCHILD);
24737 +               inode_unlock(h_inode);
24738 +
24739 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
24740 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
24741 +                       h_inode = d_inode(h_d);
24742 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
24743 +                       au_dtime_revert(a->dst_dt + AuCHILD);
24744 +                       inode_unlock(h_inode);
24745 +               }
24746 +       }
24747 +}
24748 +
24749 +/* ---------------------------------------------------------------------- */
24750 +
24751 +int aufs_rename(struct mnt_idmap *idmap,
24752 +               struct inode *_src_dir, struct dentry *_src_dentry,
24753 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
24754 +               unsigned int _flags)
24755 +{
24756 +       int err, lock_flags;
24757 +       void *rev;
24758 +       /* reduce stack space */
24759 +       struct au_ren_args *a;
24760 +       struct au_pin pin;
24761 +
24762 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
24763 +       IMustLock(_src_dir);
24764 +       IMustLock(_dst_dir);
24765 +
24766 +       err = -EINVAL;
24767 +       if (unlikely(_flags & RENAME_WHITEOUT))
24768 +               goto out;
24769 +
24770 +       err = -ENOMEM;
24771 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24772 +       a = kzalloc(sizeof(*a), GFP_NOFS);
24773 +       if (unlikely(!a))
24774 +               goto out;
24775 +
24776 +       a->flags = _flags;
24777 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24778 +                    && RENAME_EXCHANGE > U8_MAX);
24779 +       a->exchange = _flags & RENAME_EXCHANGE;
24780 +       a->src_dir = _src_dir;
24781 +       a->src_dentry = _src_dentry;
24782 +       a->src_inode = NULL;
24783 +       if (d_really_is_positive(a->src_dentry))
24784 +               a->src_inode = d_inode(a->src_dentry);
24785 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24786 +       a->dst_dir = _dst_dir;
24787 +       a->dst_dentry = _dst_dentry;
24788 +       a->dst_inode = NULL;
24789 +       if (d_really_is_positive(a->dst_dentry))
24790 +               a->dst_inode = d_inode(a->dst_dentry);
24791 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24792 +       if (a->dst_inode) {
24793 +               /*
24794 +                * if EXCHANGE && src is non-dir && dst is dir,
24795 +                * dst is not locked.
24796 +                */
24797 +               /* IMustLock(a->dst_inode); */
24798 +               au_igrab(a->dst_inode);
24799 +       }
24800 +
24801 +       err = -ENOTDIR;
24802 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
24803 +       if (d_is_dir(a->src_dentry)) {
24804 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
24805 +               if (unlikely(!a->exchange
24806 +                            && d_really_is_positive(a->dst_dentry)
24807 +                            && !d_is_dir(a->dst_dentry)))
24808 +                       goto out_free;
24809 +               lock_flags |= AuLock_DIRS;
24810 +       }
24811 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24812 +               au_fset_ren(a->auren_flags, ISDIR_DST);
24813 +               if (unlikely(!a->exchange
24814 +                            && d_really_is_positive(a->src_dentry)
24815 +                            && !d_is_dir(a->src_dentry)))
24816 +                       goto out_free;
24817 +               lock_flags |= AuLock_DIRS;
24818 +       }
24819 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24820 +                                       lock_flags);
24821 +       if (unlikely(err))
24822 +               goto out_free;
24823 +
24824 +       err = au_d_hashed_positive(a->src_dentry);
24825 +       if (unlikely(err))
24826 +               goto out_unlock;
24827 +       err = -ENOENT;
24828 +       if (a->dst_inode) {
24829 +               /*
24830 +                * If it is a dir, VFS unhash it before this
24831 +                * function. It means we cannot rely upon d_unhashed().
24832 +                */
24833 +               if (unlikely(!a->dst_inode->i_nlink))
24834 +                       goto out_unlock;
24835 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24836 +                       err = au_d_hashed_positive(a->dst_dentry);
24837 +                       if (unlikely(err && !a->exchange))
24838 +                               goto out_unlock;
24839 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24840 +                       goto out_unlock;
24841 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
24842 +               goto out_unlock;
24843 +
24844 +       /*
24845 +        * is it possible?
24846 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
24847 +        * there may exist a problem somewhere else.
24848 +        */
24849 +       err = -EINVAL;
24850 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
24851 +               goto out_unlock;
24852 +
24853 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
24854 +       di_write_lock_parent(a->dst_parent);
24855 +
24856 +       /* which branch we process */
24857 +       err = au_ren_wbr(a);
24858 +       if (unlikely(err < 0))
24859 +               goto out_parent;
24860 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
24861 +       a->h_path.mnt = au_br_mnt(a->br);
24862 +
24863 +       /* are they available to be renamed */
24864 +       err = au_ren_may_dir(a);
24865 +       if (unlikely(err))
24866 +               goto out_children;
24867 +
24868 +       /* prepare the writable parent dir on the same branch */
24869 +       if (a->dst_btop == a->btgt) {
24870 +               au_fset_ren(a->auren_flags, WHDST);
24871 +       } else {
24872 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
24873 +               if (unlikely(err))
24874 +                       goto out_children;
24875 +       }
24876 +
24877 +       err = 0;
24878 +       if (!a->exchange) {
24879 +               if (a->src_dir != a->dst_dir) {
24880 +                       /*
24881 +                        * this temporary unlock is safe,
24882 +                        * because both dir->i_mutex are locked.
24883 +                        */
24884 +                       di_write_unlock(a->dst_parent);
24885 +                       di_write_lock_parent(a->src_parent);
24886 +                       err = au_wr_dir_need_wh(a->src_dentry,
24887 +                                               au_ftest_ren(a->auren_flags,
24888 +                                                            ISDIR_SRC),
24889 +                                               &a->btgt);
24890 +                       di_write_unlock(a->src_parent);
24891 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
24892 +                                             /*isdir*/1);
24893 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
24894 +               } else
24895 +                       err = au_wr_dir_need_wh(a->src_dentry,
24896 +                                               au_ftest_ren(a->auren_flags,
24897 +                                                            ISDIR_SRC),
24898 +                                               &a->btgt);
24899 +       }
24900 +       if (unlikely(err < 0))
24901 +               goto out_children;
24902 +       if (err)
24903 +               au_fset_ren(a->auren_flags, WHSRC);
24904 +
24905 +       /* cpup src */
24906 +       if (a->src_btop != a->btgt) {
24907 +               err = au_pin(&pin, a->src_dentry, a->btgt,
24908 +                            au_opt_udba(a->src_dentry->d_sb),
24909 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24910 +               if (!err) {
24911 +                       struct au_cp_generic cpg = {
24912 +                               .dentry = a->src_dentry,
24913 +                               .bdst   = a->btgt,
24914 +                               .bsrc   = a->src_btop,
24915 +                               .len    = -1,
24916 +                               .pin    = &pin,
24917 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24918 +                       };
24919 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
24920 +                       err = au_sio_cpup_simple(&cpg);
24921 +                       au_unpin(&pin);
24922 +               }
24923 +               if (unlikely(err))
24924 +                       goto out_children;
24925 +               a->src_btop = a->btgt;
24926 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
24927 +               if (!a->exchange)
24928 +                       au_fset_ren(a->auren_flags, WHSRC);
24929 +       }
24930 +
24931 +       /* cpup dst */
24932 +       if (a->exchange && a->dst_inode
24933 +           && a->dst_btop != a->btgt) {
24934 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
24935 +                            au_opt_udba(a->dst_dentry->d_sb),
24936 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24937 +               if (!err) {
24938 +                       struct au_cp_generic cpg = {
24939 +                               .dentry = a->dst_dentry,
24940 +                               .bdst   = a->btgt,
24941 +                               .bsrc   = a->dst_btop,
24942 +                               .len    = -1,
24943 +                               .pin    = &pin,
24944 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24945 +                       };
24946 +                       err = au_sio_cpup_simple(&cpg);
24947 +                       au_unpin(&pin);
24948 +               }
24949 +               if (unlikely(err))
24950 +                       goto out_children;
24951 +               a->dst_btop = a->btgt;
24952 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
24953 +       }
24954 +
24955 +       /* lock them all */
24956 +       err = au_ren_lock(a);
24957 +       if (unlikely(err))
24958 +               /* leave the copied-up one */
24959 +               goto out_children;
24960 +
24961 +       if (!a->exchange) {
24962 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24963 +                       err = au_may_ren(a);
24964 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24965 +                       err = -ENAMETOOLONG;
24966 +               if (unlikely(err))
24967 +                       goto out_hdir;
24968 +       }
24969 +
24970 +       /* store timestamps to be revertible */
24971 +       au_ren_dt(a);
24972 +
24973 +       /* store dirren info */
24974 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24975 +               err = au_dr_rename(a->src_dentry, a->btgt,
24976 +                                  &a->dst_dentry->d_name, &rev);
24977 +               AuTraceErr(err);
24978 +               if (unlikely(err))
24979 +                       goto out_dt;
24980 +       }
24981 +
24982 +       /* here we go */
24983 +       err = do_rename(a);
24984 +       if (unlikely(err))
24985 +               goto out_dirren;
24986 +
24987 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24988 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
24989 +
24990 +       /* update dir attributes */
24991 +       au_ren_refresh_dir(a);
24992 +
24993 +       /* dput/iput all lower dentries */
24994 +       au_ren_refresh(a);
24995 +
24996 +       goto out_hdir; /* success */
24997 +
24998 +out_dirren:
24999 +       if (au_ftest_ren(a->auren_flags, DIRREN))
25000 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
25001 +out_dt:
25002 +       au_ren_rev_dt(err, a);
25003 +out_hdir:
25004 +       au_ren_unlock(a);
25005 +out_children:
25006 +       au_nhash_wh_free(&a->whlist);
25007 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
25008 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
25009 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
25010 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
25011 +       }
25012 +out_parent:
25013 +       if (!err) {
25014 +               if (d_unhashed(a->src_dentry))
25015 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
25016 +               if (d_unhashed(a->dst_dentry))
25017 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
25018 +               if (!a->exchange)
25019 +                       d_move(a->src_dentry, a->dst_dentry);
25020 +               else {
25021 +                       d_exchange(a->src_dentry, a->dst_dentry);
25022 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
25023 +                               d_drop(a->dst_dentry);
25024 +               }
25025 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
25026 +                       d_drop(a->src_dentry);
25027 +       } else {
25028 +               au_update_dbtop(a->dst_dentry);
25029 +               if (!a->dst_inode)
25030 +                       d_drop(a->dst_dentry);
25031 +       }
25032 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
25033 +               di_write_unlock(a->dst_parent);
25034 +       else
25035 +               di_write_unlock2(a->src_parent, a->dst_parent);
25036 +out_unlock:
25037 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
25038 +out_free:
25039 +       iput(a->dst_inode);
25040 +       if (a->thargs)
25041 +               au_whtmp_rmdir_free(a->thargs);
25042 +       au_kfree_rcu(a);
25043 +out:
25044 +       AuTraceErr(err);
25045 +       return err;
25046 +}
25047 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
25048 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
25049 +++ linux/fs/aufs/Kconfig       2022-11-05 23:02:18.959222617 +0100
25050 @@ -0,0 +1,199 @@
25051 +# SPDX-License-Identifier: GPL-2.0
25052 +config AUFS_FS
25053 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
25054 +       help
25055 +       Aufs is a stackable unification filesystem such as Unionfs,
25056 +       which unifies several directories and provides a merged single
25057 +       directory.
25058 +       In the early days, aufs was entirely re-designed and
25059 +       re-implemented Unionfs Version 1.x series. Introducing many
25060 +       original ideas, approaches and improvements, it becomes totally
25061 +       different from Unionfs while keeping the basic features.
25062 +
25063 +if AUFS_FS
25064 +choice
25065 +       prompt "Maximum number of branches"
25066 +       default AUFS_BRANCH_MAX_127
25067 +       help
25068 +       Specifies the maximum number of branches (or member directories)
25069 +       in a single aufs. The larger value consumes more system
25070 +       resources and has a minor impact to performance.
25071 +config AUFS_BRANCH_MAX_127
25072 +       bool "127"
25073 +       help
25074 +       Specifies the maximum number of branches (or member directories)
25075 +       in a single aufs. The larger value consumes more system
25076 +       resources and has a minor impact to performance.
25077 +config AUFS_BRANCH_MAX_511
25078 +       bool "511"
25079 +       help
25080 +       Specifies the maximum number of branches (or member directories)
25081 +       in a single aufs. The larger value consumes more system
25082 +       resources and has a minor impact to performance.
25083 +config AUFS_BRANCH_MAX_1023
25084 +       bool "1023"
25085 +       help
25086 +       Specifies the maximum number of branches (or member directories)
25087 +       in a single aufs. The larger value consumes more system
25088 +       resources and has a minor impact to performance.
25089 +config AUFS_BRANCH_MAX_32767
25090 +       bool "32767"
25091 +       help
25092 +       Specifies the maximum number of branches (or member directories)
25093 +       in a single aufs. The larger value consumes more system
25094 +       resources and has a minor impact to performance.
25095 +endchoice
25096 +
25097 +config AUFS_SBILIST
25098 +       bool
25099 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
25100 +       default y
25101 +       help
25102 +       Automatic configuration for internal use.
25103 +       When aufs supports Magic SysRq or /proc, enabled automatically.
25104 +
25105 +config AUFS_HNOTIFY
25106 +       bool "Detect direct branch access (bypassing aufs)"
25107 +       help
25108 +       If you want to modify files on branches directly, eg. bypassing aufs,
25109 +       and want aufs to detect the changes of them fully, then enable this
25110 +       option and use 'udba=notify' mount option.
25111 +       Currently there is only one available configuration, "fsnotify".
25112 +       It will have a negative impact to the performance.
25113 +       See detail in aufs.5.
25114 +
25115 +choice
25116 +       prompt "method" if AUFS_HNOTIFY
25117 +       default AUFS_HFSNOTIFY
25118 +config AUFS_HFSNOTIFY
25119 +       bool "fsnotify"
25120 +       select FSNOTIFY
25121 +endchoice
25122 +
25123 +config AUFS_EXPORT
25124 +       bool "NFS-exportable aufs"
25125 +       depends on EXPORTFS
25126 +       help
25127 +       If you want to export your mounted aufs via NFS, then enable this
25128 +       option. There are several requirements for this configuration.
25129 +       See detail in aufs.5.
25130 +
25131 +config AUFS_INO_T_64
25132 +       bool
25133 +       depends on AUFS_EXPORT
25134 +       depends on 64BIT && !(ALPHA || S390)
25135 +       default y
25136 +       help
25137 +       Automatic configuration for internal use.
25138 +       /* typedef unsigned long/int __kernel_ino_t */
25139 +       /* alpha and s390x are int */
25140 +
25141 +config AUFS_XATTR
25142 +       bool "support for XATTR/EA (including Security Labels)"
25143 +       help
25144 +       If your branch fs supports XATTR/EA and you want to make them
25145 +       available in aufs too, then enable this opsion and specify the
25146 +       branch attributes for EA.
25147 +       See detail in aufs.5.
25148 +
25149 +config AUFS_FHSM
25150 +       bool "File-based Hierarchical Storage Management"
25151 +       help
25152 +       Hierarchical Storage Management (or HSM) is a well-known feature
25153 +       in the storage world. Aufs provides this feature as file-based.
25154 +       with multiple branches.
25155 +       These multiple branches are prioritized, ie. the topmost one
25156 +       should be the fastest drive and be used heavily.
25157 +
25158 +config AUFS_RDU
25159 +       bool "Readdir in userspace"
25160 +       help
25161 +       Aufs has two methods to provide a merged view for a directory,
25162 +       by a user-space library and by kernel-space natively. The latter
25163 +       is always enabled but sometimes large and slow.
25164 +       If you enable this option, install the library in aufs2-util
25165 +       package, and set some environment variables for your readdir(3),
25166 +       then the work will be handled in user-space which generally
25167 +       shows better performance in most cases.
25168 +       See detail in aufs.5.
25169 +
25170 +config AUFS_DIRREN
25171 +       bool "Workaround for rename(2)-ing a directory"
25172 +       help
25173 +       By default, aufs returns EXDEV error in renameing a dir who has
25174 +       his child on the lower branch, since it is a bad idea to issue
25175 +       rename(2) internally for every lower branch. But user may not
25176 +       accept this behaviour. So here is a workaround to allow such
25177 +       rename(2) and store some extra information on the writable
25178 +       branch. Obviously this costs high (and I don't like it).
25179 +       To use this feature, you need to enable this configuration AND
25180 +       to specify the mount option `dirren.'
25181 +       See details in aufs.5 and the design documents.
25182 +
25183 +config AUFS_SHWH
25184 +       bool "Show whiteouts"
25185 +       help
25186 +       If you want to make the whiteouts in aufs visible, then enable
25187 +       this option and specify 'shwh' mount option. Although it may
25188 +       sounds like philosophy or something, but in technically it
25189 +       simply shows the name of whiteout with keeping its behaviour.
25190 +
25191 +config AUFS_BR_RAMFS
25192 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
25193 +       help
25194 +       If you want to use ramfs as an aufs branch fs, then enable this
25195 +       option. Generally tmpfs is recommended.
25196 +       Aufs prohibited them to be a branch fs by default, because
25197 +       initramfs becomes unusable after switch_root or something
25198 +       generally. If you sets initramfs as an aufs branch and boot your
25199 +       system by switch_root, you will meet a problem easily since the
25200 +       files in initramfs may be inaccessible.
25201 +       Unless you are going to use ramfs as an aufs branch fs without
25202 +       switch_root or something, leave it N.
25203 +
25204 +config AUFS_BR_FUSE
25205 +       bool "Fuse fs as an aufs branch"
25206 +       depends on FUSE_FS
25207 +       select AUFS_POLL
25208 +       help
25209 +       If you want to use fuse-based userspace filesystem as an aufs
25210 +       branch fs, then enable this option.
25211 +       It implements the internal poll(2) operation which is
25212 +       implemented by fuse only (curretnly).
25213 +
25214 +config AUFS_POLL
25215 +       bool
25216 +       help
25217 +       Automatic configuration for internal use.
25218 +
25219 +config AUFS_BR_HFSPLUS
25220 +       bool "Hfsplus as an aufs branch"
25221 +       depends on HFSPLUS_FS
25222 +       default y
25223 +       help
25224 +       If you want to use hfsplus fs as an aufs branch fs, then enable
25225 +       this option. This option introduces a small overhead at
25226 +       copying-up a file on hfsplus.
25227 +
25228 +config AUFS_BDEV_LOOP
25229 +       bool
25230 +       depends on BLK_DEV_LOOP
25231 +       default y
25232 +       help
25233 +       Automatic configuration for internal use.
25234 +       Convert =[ym] into =y.
25235 +
25236 +config AUFS_DEBUG
25237 +       bool "Debug aufs"
25238 +       help
25239 +       Enable this to compile aufs internal debug code.
25240 +       It will have a negative impact to the performance.
25241 +
25242 +config AUFS_MAGIC_SYSRQ
25243 +       bool
25244 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
25245 +       default y
25246 +       help
25247 +       Automatic configuration for internal use.
25248 +       When aufs supports Magic SysRq, enabled automatically.
25249 +endif
25250 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
25251 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
25252 +++ linux/fs/aufs/lcnt.h        2022-11-05 23:02:18.965889284 +0100
25253 @@ -0,0 +1,186 @@
25254 +/* SPDX-License-Identifier: GPL-2.0 */
25255 +/*
25256 + * Copyright (C) 2018-2022 Junjiro R. Okajima
25257 + *
25258 + * This program is free software; you can redistribute it and/or modify
25259 + * it under the terms of the GNU General Public License as published by
25260 + * the Free Software Foundation; either version 2 of the License, or
25261 + * (at your option) any later version.
25262 + *
25263 + * This program is distributed in the hope that it will be useful,
25264 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25265 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25266 + * GNU General Public License for more details.
25267 + *
25268 + * You should have received a copy of the GNU General Public License
25269 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25270 + */
25271 +
25272 +/*
25273 + * simple long counter wrapper
25274 + */
25275 +
25276 +#ifndef __AUFS_LCNT_H__
25277 +#define __AUFS_LCNT_H__
25278 +
25279 +#ifdef __KERNEL__
25280 +
25281 +#include "debug.h"
25282 +
25283 +#define AuLCntATOMIC   1
25284 +#define AuLCntPCPUCNT  2
25285 +/*
25286 + * why does percpu_refcount require extra synchronize_rcu()s in
25287 + * au_br_do_free()
25288 + */
25289 +#define AuLCntPCPUREF  3
25290 +
25291 +/* #define AuLCntChosen        AuLCntATOMIC */
25292 +#define AuLCntChosen   AuLCntPCPUCNT
25293 +/* #define AuLCntChosen        AuLCntPCPUREF */
25294 +
25295 +#if AuLCntChosen == AuLCntATOMIC
25296 +#include <linux/atomic.h>
25297 +
25298 +typedef atomic_long_t au_lcnt_t;
25299 +
25300 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25301 +{
25302 +       atomic_long_set(cnt, 0);
25303 +       return 0;
25304 +}
25305 +
25306 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25307 +{
25308 +       /* empty */
25309 +}
25310 +
25311 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25312 +                              int do_sync __maybe_unused)
25313 +{
25314 +       /* empty */
25315 +}
25316 +
25317 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25318 +{
25319 +       atomic_long_inc(cnt);
25320 +}
25321 +
25322 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25323 +{
25324 +       atomic_long_dec(cnt);
25325 +}
25326 +
25327 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25328 +{
25329 +       return atomic_long_read(cnt);
25330 +}
25331 +#endif
25332 +
25333 +#if AuLCntChosen == AuLCntPCPUCNT
25334 +#include <linux/percpu_counter.h>
25335 +
25336 +typedef struct percpu_counter au_lcnt_t;
25337 +
25338 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25339 +{
25340 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
25341 +}
25342 +
25343 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25344 +{
25345 +       /* empty */
25346 +}
25347 +
25348 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25349 +{
25350 +       percpu_counter_destroy(cnt);
25351 +}
25352 +
25353 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25354 +{
25355 +       percpu_counter_inc(cnt);
25356 +}
25357 +
25358 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25359 +{
25360 +       percpu_counter_dec(cnt);
25361 +}
25362 +
25363 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25364 +{
25365 +       s64 n;
25366 +
25367 +       n = percpu_counter_sum(cnt);
25368 +       BUG_ON(n < 0);
25369 +       if (LONG_MAX != LLONG_MAX
25370 +           && n > LONG_MAX)
25371 +               AuWarn1("%s\n", "wrap-around");
25372 +
25373 +       return n;
25374 +}
25375 +#endif
25376 +
25377 +#if AuLCntChosen == AuLCntPCPUREF
25378 +#include <linux/percpu-refcount.h>
25379 +
25380 +typedef struct percpu_ref au_lcnt_t;
25381 +
25382 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25383 +{
25384 +       if (!release)
25385 +               release = percpu_ref_exit;
25386 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25387 +}
25388 +
25389 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25390 +{
25391 +       synchronize_rcu();
25392 +}
25393 +
25394 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25395 +{
25396 +       percpu_ref_kill(cnt);
25397 +       if (do_sync)
25398 +               au_lcnt_wait_for_fin(cnt);
25399 +}
25400 +
25401 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25402 +{
25403 +       percpu_ref_get(cnt);
25404 +}
25405 +
25406 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25407 +{
25408 +       percpu_ref_put(cnt);
25409 +}
25410 +
25411 +/*
25412 + * avoid calling this func as possible.
25413 + */
25414 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25415 +{
25416 +       long l;
25417 +
25418 +       percpu_ref_switch_to_atomic_sync(cnt);
25419 +       l = atomic_long_read(&cnt->count);
25420 +       if (do_rev)
25421 +               percpu_ref_switch_to_percpu(cnt);
25422 +
25423 +       /* percpu_ref is initialized by 1 instead of 0 */
25424 +       return l - 1;
25425 +}
25426 +#endif
25427 +
25428 +#ifdef CONFIG_AUFS_DEBUG
25429 +#define AuLCntZero(val) do {                   \
25430 +       long l = val;                           \
25431 +       if (l)                                  \
25432 +               AuDbg("%s = %ld\n", #val, l);   \
25433 +} while (0)
25434 +#else
25435 +#define AuLCntZero(val)                do {} while (0)
25436 +#endif
25437 +
25438 +#endif /* __KERNEL__ */
25439 +#endif /* __AUFS_LCNT_H__ */
25440 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
25441 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
25442 +++ linux/fs/aufs/loop.c        2022-11-05 23:02:18.965889284 +0100
25443 @@ -0,0 +1,148 @@
25444 +// SPDX-License-Identifier: GPL-2.0
25445 +/*
25446 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25447 + *
25448 + * This program is free software; you can redistribute it and/or modify
25449 + * it under the terms of the GNU General Public License as published by
25450 + * the Free Software Foundation; either version 2 of the License, or
25451 + * (at your option) any later version.
25452 + *
25453 + * This program is distributed in the hope that it will be useful,
25454 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25455 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25456 + * GNU General Public License for more details.
25457 + *
25458 + * You should have received a copy of the GNU General Public License
25459 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25460 + */
25461 +
25462 +/*
25463 + * support for loopback block device as a branch
25464 + */
25465 +
25466 +#include "aufs.h"
25467 +
25468 +/* added into drivers/block/loop.c */
25469 +static struct file *(*backing_file_func)(struct super_block *sb);
25470 +
25471 +/*
25472 + * test if two lower dentries have overlapping branches.
25473 + */
25474 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25475 +{
25476 +       struct super_block *h_sb;
25477 +       struct file *backing_file;
25478 +
25479 +       if (unlikely(!backing_file_func)) {
25480 +               /* don't load "loop" module here */
25481 +               backing_file_func = symbol_get(loop_backing_file);
25482 +               if (unlikely(!backing_file_func))
25483 +                       /* "loop" module is not loaded */
25484 +                       return 0;
25485 +       }
25486 +
25487 +       h_sb = h_adding->d_sb;
25488 +       backing_file = backing_file_func(h_sb);
25489 +       if (!backing_file)
25490 +               return 0;
25491 +
25492 +       h_adding = backing_file->f_path.dentry;
25493 +       /*
25494 +        * h_adding can be local NFS.
25495 +        * in this case aufs cannot detect the loop.
25496 +        */
25497 +       if (unlikely(h_adding->d_sb == sb))
25498 +               return 1;
25499 +       return !!au_test_subdir(h_adding, sb->s_root);
25500 +}
25501 +
25502 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25503 +int au_test_loopback_kthread(void)
25504 +{
25505 +       int ret;
25506 +       struct task_struct *tsk = current;
25507 +       char c, comm[sizeof(tsk->comm)];
25508 +
25509 +       ret = 0;
25510 +       if (tsk->flags & PF_KTHREAD) {
25511 +               get_task_comm(comm, tsk);
25512 +               c = comm[4];
25513 +               ret = ('0' <= c && c <= '9'
25514 +                      && !strncmp(comm, "loop", 4));
25515 +       }
25516 +
25517 +       return ret;
25518 +}
25519 +
25520 +/* ---------------------------------------------------------------------- */
25521 +
25522 +#define au_warn_loopback_step  16
25523 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25524 +static unsigned long *au_warn_loopback_array;
25525 +
25526 +void au_warn_loopback(struct super_block *h_sb)
25527 +{
25528 +       int i, new_nelem;
25529 +       unsigned long *a, magic;
25530 +       static DEFINE_SPINLOCK(spin);
25531 +
25532 +       magic = h_sb->s_magic;
25533 +       spin_lock(&spin);
25534 +       a = au_warn_loopback_array;
25535 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25536 +               if (a[i] == magic) {
25537 +                       spin_unlock(&spin);
25538 +                       return;
25539 +               }
25540 +
25541 +       /* h_sb is new to us, print it */
25542 +       if (i < au_warn_loopback_nelem) {
25543 +               a[i] = magic;
25544 +               goto pr;
25545 +       }
25546 +
25547 +       /* expand the array */
25548 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25549 +       a = au_kzrealloc(au_warn_loopback_array,
25550 +                        au_warn_loopback_nelem * sizeof(unsigned long),
25551 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25552 +                        /*may_shrink*/0);
25553 +       if (a) {
25554 +               au_warn_loopback_nelem = new_nelem;
25555 +               au_warn_loopback_array = a;
25556 +               a[i] = magic;
25557 +               goto pr;
25558 +       }
25559 +
25560 +       spin_unlock(&spin);
25561 +       AuWarn1("realloc failed, ignored\n");
25562 +       return;
25563 +
25564 +pr:
25565 +       spin_unlock(&spin);
25566 +       pr_warn("you may want to try another patch for loopback file "
25567 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25568 +}
25569 +
25570 +int au_loopback_init(void)
25571 +{
25572 +       int err;
25573 +       struct super_block *sb __maybe_unused;
25574 +
25575 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25576 +
25577 +       err = 0;
25578 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25579 +                                        sizeof(unsigned long), GFP_NOFS);
25580 +       if (unlikely(!au_warn_loopback_array))
25581 +               err = -ENOMEM;
25582 +
25583 +       return err;
25584 +}
25585 +
25586 +void au_loopback_fin(void)
25587 +{
25588 +       if (backing_file_func)
25589 +               symbol_put(loop_backing_file);
25590 +       au_kfree_try_rcu(au_warn_loopback_array);
25591 +}
25592 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
25593 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
25594 +++ linux/fs/aufs/loop.h        2022-11-05 23:02:18.965889284 +0100
25595 @@ -0,0 +1,55 @@
25596 +/* SPDX-License-Identifier: GPL-2.0 */
25597 +/*
25598 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25599 + *
25600 + * This program is free software; you can redistribute it and/or modify
25601 + * it under the terms of the GNU General Public License as published by
25602 + * the Free Software Foundation; either version 2 of the License, or
25603 + * (at your option) any later version.
25604 + *
25605 + * This program is distributed in the hope that it will be useful,
25606 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25607 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25608 + * GNU General Public License for more details.
25609 + *
25610 + * You should have received a copy of the GNU General Public License
25611 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25612 + */
25613 +
25614 +/*
25615 + * support for loopback mount as a branch
25616 + */
25617 +
25618 +#ifndef __AUFS_LOOP_H__
25619 +#define __AUFS_LOOP_H__
25620 +
25621 +#ifdef __KERNEL__
25622 +
25623 +struct dentry;
25624 +struct super_block;
25625 +
25626 +#ifdef CONFIG_AUFS_BDEV_LOOP
25627 +/* drivers/block/loop.c */
25628 +struct file *loop_backing_file(struct super_block *sb);
25629 +
25630 +/* loop.c */
25631 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25632 +int au_test_loopback_kthread(void);
25633 +void au_warn_loopback(struct super_block *h_sb);
25634 +
25635 +int au_loopback_init(void);
25636 +void au_loopback_fin(void);
25637 +#else
25638 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25639 +
25640 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25641 +          struct dentry *h_adding)
25642 +AuStubInt0(au_test_loopback_kthread, void)
25643 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25644 +
25645 +AuStubInt0(au_loopback_init, void)
25646 +AuStubVoid(au_loopback_fin, void)
25647 +#endif /* BLK_DEV_LOOP */
25648 +
25649 +#endif /* __KERNEL__ */
25650 +#endif /* __AUFS_LOOP_H__ */
25651 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
25652 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
25653 +++ linux/fs/aufs/magic.mk      2022-11-05 23:02:18.965889284 +0100
25654 @@ -0,0 +1,31 @@
25655 +# SPDX-License-Identifier: GPL-2.0
25656 +
25657 +# defined in ${srctree}/fs/fuse/inode.c
25658 +# tristate
25659 +ifdef CONFIG_FUSE_FS
25660 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25661 +endif
25662 +
25663 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25664 +# tristate
25665 +ifdef CONFIG_XFS_FS
25666 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25667 +endif
25668 +
25669 +# defined in ${srctree}/fs/configfs/mount.c
25670 +# tristate
25671 +ifdef CONFIG_CONFIGFS_FS
25672 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25673 +endif
25674 +
25675 +# defined in ${srctree}/fs/ubifs/ubifs.h
25676 +# tristate
25677 +ifdef CONFIG_UBIFS_FS
25678 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25679 +endif
25680 +
25681 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25682 +# tristate
25683 +ifdef CONFIG_HFSPLUS_FS
25684 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25685 +endif
25686 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
25687 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
25688 +++ linux/fs/aufs/Makefile      2022-11-05 23:02:18.959222617 +0100
25689 @@ -0,0 +1,46 @@
25690 +# SPDX-License-Identifier: GPL-2.0
25691 +
25692 +include ${src}/magic.mk
25693 +ifeq (${CONFIG_AUFS_FS},m)
25694 +include ${src}/conf.mk
25695 +endif
25696 +-include ${src}/priv_def.mk
25697 +
25698 +# cf. include/linux/kernel.h
25699 +# enable pr_debug
25700 +ccflags-y += -DDEBUG
25701 +# sparse requires the full pathname
25702 +ifdef M
25703 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
25704 +else
25705 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
25706 +endif
25707 +
25708 +obj-$(CONFIG_AUFS_FS) += aufs.o
25709 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
25710 +       wkq.o vfsub.o dcsub.o \
25711 +       cpup.o whout.o wbr_policy.o \
25712 +       dinfo.o dentry.o \
25713 +       dynop.o \
25714 +       finfo.o file.o f_op.o \
25715 +       dir.o vdir.o \
25716 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
25717 +       mvdown.o ioctl.o
25718 +
25719 +# all are boolean
25720 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
25721 +aufs-$(CONFIG_SYSFS) += sysfs.o
25722 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25723 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25724 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25725 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
25726 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
25727 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25728 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
25729 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
25730 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
25731 +aufs-$(CONFIG_AUFS_POLL) += poll.o
25732 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
25733 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25734 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25735 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
25736 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
25737 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
25738 +++ linux/fs/aufs/module.c      2022-11-05 23:02:18.965889284 +0100
25739 @@ -0,0 +1,273 @@
25740 +// SPDX-License-Identifier: GPL-2.0
25741 +/*
25742 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25743 + *
25744 + * This program is free software; you can redistribute it and/or modify
25745 + * it under the terms of the GNU General Public License as published by
25746 + * the Free Software Foundation; either version 2 of the License, or
25747 + * (at your option) any later version.
25748 + *
25749 + * This program is distributed in the hope that it will be useful,
25750 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25751 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25752 + * GNU General Public License for more details.
25753 + *
25754 + * You should have received a copy of the GNU General Public License
25755 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25756 + */
25757 +
25758 +/*
25759 + * module global variables and operations
25760 + */
25761 +
25762 +#include <linux/module.h>
25763 +#include <linux/seq_file.h>
25764 +#include "aufs.h"
25765 +
25766 +/* shrinkable realloc */
25767 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25768 +{
25769 +       size_t sz;
25770 +       int diff;
25771 +
25772 +       sz = 0;
25773 +       diff = -1;
25774 +       if (p) {
25775 +#if 0 /* unused */
25776 +               if (!new_sz) {
25777 +                       au_kfree_rcu(p);
25778 +                       p = NULL;
25779 +                       goto out;
25780 +               }
25781 +#else
25782 +               AuDebugOn(!new_sz);
25783 +#endif
25784 +               sz = ksize(p);
25785 +               diff = au_kmidx_sub(sz, new_sz);
25786 +       }
25787 +       if (sz && !diff)
25788 +               goto out;
25789 +
25790 +       if (sz < new_sz)
25791 +               /* expand or SLOB */
25792 +               p = krealloc(p, new_sz, gfp);
25793 +       else if (new_sz < sz && may_shrink) {
25794 +               /* shrink */
25795 +               void *q;
25796 +
25797 +               q = kmalloc(new_sz, gfp);
25798 +               if (q) {
25799 +                       if (p) {
25800 +                               memcpy(q, p, new_sz);
25801 +                               au_kfree_try_rcu(p);
25802 +                       }
25803 +                       p = q;
25804 +               } else
25805 +                       p = NULL;
25806 +       }
25807 +
25808 +out:
25809 +       return p;
25810 +}
25811 +
25812 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25813 +                  int may_shrink)
25814 +{
25815 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
25816 +       if (p && new_sz > nused)
25817 +               memset(p + nused, 0, new_sz - nused);
25818 +       return p;
25819 +}
25820 +
25821 +/* ---------------------------------------------------------------------- */
25822 +/*
25823 + * aufs caches
25824 + */
25825 +struct kmem_cache *au_cache[AuCache_Last];
25826 +
25827 +static void au_cache_fin(void)
25828 +{
25829 +       int i;
25830 +
25831 +       /*
25832 +        * Make sure all delayed rcu free inodes are flushed before we
25833 +        * destroy cache.
25834 +        */
25835 +       rcu_barrier();
25836 +
25837 +       /* excluding AuCache_HNOTIFY */
25838 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25839 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
25840 +               kmem_cache_destroy(au_cache[i]);
25841 +               au_cache[i] = NULL;
25842 +       }
25843 +}
25844 +
25845 +static int __init au_cache_init(void)
25846 +{
25847 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25848 +       if (au_cache[AuCache_DINFO])
25849 +               /* SLAB_DESTROY_BY_RCU */
25850 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25851 +                                                      au_icntnr_init_once);
25852 +       if (au_cache[AuCache_ICNTNR])
25853 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25854 +                                                     au_fi_init_once);
25855 +       if (au_cache[AuCache_FINFO])
25856 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
25857 +       if (au_cache[AuCache_VDIR])
25858 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25859 +       if (au_cache[AuCache_DEHSTR])
25860 +               return 0;
25861 +
25862 +       au_cache_fin();
25863 +       return -ENOMEM;
25864 +}
25865 +
25866 +/* ---------------------------------------------------------------------- */
25867 +
25868 +int au_dir_roflags;
25869 +
25870 +#ifdef CONFIG_AUFS_SBILIST
25871 +/*
25872 + * iterate_supers_type() doesn't protect us from
25873 + * remounting (branch management)
25874 + */
25875 +struct hlist_bl_head au_sbilist;
25876 +#endif
25877 +
25878 +/*
25879 + * functions for module interface.
25880 + */
25881 +MODULE_LICENSE("GPL");
25882 +/* MODULE_LICENSE("GPL v2"); */
25883 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25884 +MODULE_DESCRIPTION(AUFS_NAME
25885 +       " -- Advanced multi layered unification filesystem");
25886 +MODULE_VERSION(AUFS_VERSION);
25887 +MODULE_ALIAS_FS(AUFS_NAME);
25888 +
25889 +/* this module parameter has no meaning when SYSFS is disabled */
25890 +int sysaufs_brs = 1;
25891 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25892 +module_param_named(brs, sysaufs_brs, int, 0444);
25893 +
25894 +/* this module parameter has no meaning when USER_NS is disabled */
25895 +bool au_userns;
25896 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25897 +module_param_named(allow_userns, au_userns, bool, 0444);
25898 +
25899 +/* ---------------------------------------------------------------------- */
25900 +
25901 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25902 +
25903 +int au_seq_path(struct seq_file *seq, struct path *path)
25904 +{
25905 +       int err;
25906 +
25907 +       err = seq_path(seq, path, au_esc_chars);
25908 +       if (err >= 0)
25909 +               err = 0;
25910 +       else
25911 +               err = -ENOMEM;
25912 +
25913 +       return err;
25914 +}
25915 +
25916 +/* ---------------------------------------------------------------------- */
25917 +
25918 +static int __init aufs_init(void)
25919 +{
25920 +       int err, i;
25921 +       char *p;
25922 +
25923 +       p = au_esc_chars;
25924 +       for (i = 1; i <= ' '; i++)
25925 +               *p++ = i;
25926 +       *p++ = '\\';
25927 +       *p++ = '\x7f';
25928 +       *p = 0;
25929 +
25930 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25931 +
25932 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25933 +       for (i = 0; i < AuIop_Last; i++)
25934 +               aufs_iop_nogetattr[i].getattr = NULL;
25935 +
25936 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
25937 +
25938 +       au_sbilist_init();
25939 +       sysaufs_brs_init();
25940 +       au_debug_init();
25941 +       au_dy_init();
25942 +       err = sysaufs_init();
25943 +       if (unlikely(err))
25944 +               goto out;
25945 +       err = dbgaufs_init();
25946 +       if (unlikely(err))
25947 +               goto out_sysaufs;
25948 +       err = au_procfs_init();
25949 +       if (unlikely(err))
25950 +               goto out_dbgaufs;
25951 +       err = au_wkq_init();
25952 +       if (unlikely(err))
25953 +               goto out_procfs;
25954 +       err = au_loopback_init();
25955 +       if (unlikely(err))
25956 +               goto out_wkq;
25957 +       err = au_hnotify_init();
25958 +       if (unlikely(err))
25959 +               goto out_loopback;
25960 +       err = au_sysrq_init();
25961 +       if (unlikely(err))
25962 +               goto out_hin;
25963 +       err = au_cache_init();
25964 +       if (unlikely(err))
25965 +               goto out_sysrq;
25966 +
25967 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25968 +       err = register_filesystem(&aufs_fs_type);
25969 +       if (unlikely(err))
25970 +               goto out_cache;
25971 +
25972 +       /* since we define pr_fmt, call printk directly */
25973 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25974 +       goto out; /* success */
25975 +
25976 +out_cache:
25977 +       au_cache_fin();
25978 +out_sysrq:
25979 +       au_sysrq_fin();
25980 +out_hin:
25981 +       au_hnotify_fin();
25982 +out_loopback:
25983 +       au_loopback_fin();
25984 +out_wkq:
25985 +       au_wkq_fin();
25986 +out_procfs:
25987 +       au_procfs_fin();
25988 +out_dbgaufs:
25989 +       dbgaufs_fin();
25990 +out_sysaufs:
25991 +       sysaufs_fin();
25992 +       au_dy_fin();
25993 +out:
25994 +       return err;
25995 +}
25996 +
25997 +static void __exit aufs_exit(void)
25998 +{
25999 +       unregister_filesystem(&aufs_fs_type);
26000 +       au_cache_fin();
26001 +       au_sysrq_fin();
26002 +       au_hnotify_fin();
26003 +       au_loopback_fin();
26004 +       au_wkq_fin();
26005 +       au_procfs_fin();
26006 +       dbgaufs_fin();
26007 +       sysaufs_fin();
26008 +       au_dy_fin();
26009 +}
26010 +
26011 +module_init(aufs_init);
26012 +module_exit(aufs_exit);
26013 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
26014 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
26015 +++ linux/fs/aufs/module.h      2022-11-05 23:02:18.969222617 +0100
26016 @@ -0,0 +1,180 @@
26017 +/* SPDX-License-Identifier: GPL-2.0 */
26018 +/*
26019 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26020 + *
26021 + * This program is free software; you can redistribute it and/or modify
26022 + * it under the terms of the GNU General Public License as published by
26023 + * the Free Software Foundation; either version 2 of the License, or
26024 + * (at your option) any later version.
26025 + *
26026 + * This program is distributed in the hope that it will be useful,
26027 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26028 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26029 + * GNU General Public License for more details.
26030 + *
26031 + * You should have received a copy of the GNU General Public License
26032 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26033 + */
26034 +
26035 +/*
26036 + * module initialization and module-global
26037 + */
26038 +
26039 +#ifndef __AUFS_MODULE_H__
26040 +#define __AUFS_MODULE_H__
26041 +
26042 +#ifdef __KERNEL__
26043 +
26044 +#include <linux/slab.h>
26045 +#include "debug.h"
26046 +#include "dentry.h"
26047 +#include "dir.h"
26048 +#include "file.h"
26049 +#include "inode.h"
26050 +
26051 +struct path;
26052 +struct seq_file;
26053 +
26054 +/* module parameters */
26055 +extern int sysaufs_brs;
26056 +extern bool au_userns;
26057 +
26058 +/* ---------------------------------------------------------------------- */
26059 +
26060 +extern int au_dir_roflags;
26061 +
26062 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
26063 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
26064 +                  int may_shrink);
26065 +
26066 +/*
26067 + * Comparing the size of the object with sizeof(struct rcu_head)
26068 + * case 1: object is always larger
26069 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
26070 + * case 2: object is always smaller
26071 + *     --> au_kfree_small()
26072 + * case 3: object can be any size
26073 + *     --> au_kfree_try_rcu()
26074 + */
26075 +
26076 +static inline void au_kfree_do_rcu(const void *p)
26077 +{
26078 +       struct {
26079 +               struct rcu_head rcu;
26080 +       } *a = (void *)p;
26081 +
26082 +       kfree_rcu(a, rcu);
26083 +}
26084 +
26085 +#define au_kfree_rcu(_p) do {                                          \
26086 +               typeof(_p) p = (_p);                                    \
26087 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
26088 +               if (p)                                                  \
26089 +                       au_kfree_do_rcu(p);                             \
26090 +       } while (0)
26091 +
26092 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
26093 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
26094 +
26095 +static inline void au_kfree_try_rcu(const void *p)
26096 +{
26097 +       if (!p)
26098 +               return;
26099 +       if (au_kfree_sz_test(p))
26100 +               au_kfree_do_rcu(p);
26101 +       else
26102 +               kfree(p);
26103 +}
26104 +
26105 +static inline void au_kfree_small(const void *p)
26106 +{
26107 +       if (!p)
26108 +               return;
26109 +       AuDebugOn(au_kfree_sz_test(p));
26110 +       kfree(p);
26111 +}
26112 +
26113 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26114 +{
26115 +#ifndef CONFIG_SLOB
26116 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
26117 +#else
26118 +       return -1; /* SLOB is untested */
26119 +#endif
26120 +}
26121 +
26122 +int au_seq_path(struct seq_file *seq, struct path *path);
26123 +
26124 +#ifdef CONFIG_PROC_FS
26125 +/* procfs.c */
26126 +int __init au_procfs_init(void);
26127 +void au_procfs_fin(void);
26128 +#else
26129 +AuStubInt0(au_procfs_init, void);
26130 +AuStubVoid(au_procfs_fin, void);
26131 +#endif
26132 +
26133 +/* ---------------------------------------------------------------------- */
26134 +
26135 +/* kmem cache */
26136 +enum {
26137 +       AuCache_DINFO,
26138 +       AuCache_ICNTNR,
26139 +       AuCache_FINFO,
26140 +       AuCache_VDIR,
26141 +       AuCache_DEHSTR,
26142 +       AuCache_HNOTIFY, /* must be last */
26143 +       AuCache_Last
26144 +};
26145 +
26146 +extern struct kmem_cache *au_cache[AuCache_Last];
26147 +
26148 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26149 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
26150 +#define AuCacheCtor(type, ctor)        \
26151 +       kmem_cache_create(#type, sizeof(struct type), \
26152 +                         __alignof__(struct type), AuCacheFlags, ctor)
26153 +
26154 +#define AuCacheFuncAlloc(name, index)                                  \
26155 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
26156 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
26157 +
26158 +#define AuCacheFuncs(name, index)                                      \
26159 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26160 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
26161 +                                                                       \
26162 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26163 +       { void *p = rcu;                                                \
26164 +               p -= offsetof(struct au_##name, rcu);                   \
26165 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
26166 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26167 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26168 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
26169 +                                                                       \
26170 +       static inline void au_cache_free_##name(struct au_##name *p)    \
26171 +       { /* au_cache_free_##name##_norcu(p); */                        \
26172 +               au_cache_free_##name##_rcu(p); }
26173 +
26174 +AuCacheFuncs(dinfo, DINFO);
26175 +AuCacheFuncAlloc(dinfo, DINFO);
26176 +
26177 +AuCacheFuncs(icntnr, ICNTNR);
26178 +static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
26179 +{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
26180 +
26181 +AuCacheFuncs(finfo, FINFO);
26182 +AuCacheFuncAlloc(finfo, FINFO);
26183 +
26184 +AuCacheFuncs(vdir, VDIR);
26185 +AuCacheFuncAlloc(vdir, VDIR);
26186 +
26187 +AuCacheFuncs(vdir_dehstr, DEHSTR);
26188 +AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
26189 +
26190 +#ifdef CONFIG_AUFS_HNOTIFY
26191 +AuCacheFuncs(hnotify, HNOTIFY);
26192 +AuCacheFuncAlloc(hnotify, HNOTIFY);
26193 +#endif
26194 +
26195 +#endif /* __KERNEL__ */
26196 +#endif /* __AUFS_MODULE_H__ */
26197 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
26198 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
26199 +++ linux/fs/aufs/mvdown.c      2022-11-05 23:02:18.969222617 +0100
26200 @@ -0,0 +1,706 @@
26201 +// SPDX-License-Identifier: GPL-2.0
26202 +/*
26203 + * Copyright (C) 2011-2022 Junjiro R. Okajima
26204 + *
26205 + * This program is free software; you can redistribute it and/or modify
26206 + * it under the terms of the GNU General Public License as published by
26207 + * the Free Software Foundation; either version 2 of the License, or
26208 + * (at your option) any later version.
26209 + *
26210 + * This program is distributed in the hope that it will be useful,
26211 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26212 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26213 + * GNU General Public License for more details.
26214 + *
26215 + * You should have received a copy of the GNU General Public License
26216 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26217 + */
26218 +
26219 +/*
26220 + * move-down, opposite of copy-up
26221 + */
26222 +
26223 +#include "aufs.h"
26224 +
26225 +struct au_mvd_args {
26226 +       struct {
26227 +               struct super_block *h_sb;
26228 +               struct dentry *h_parent;
26229 +               struct au_hinode *hdir;
26230 +               struct inode *h_dir, *h_inode;
26231 +               struct au_pin pin;
26232 +       } info[AUFS_MVDOWN_NARRAY];
26233 +
26234 +       struct aufs_mvdown mvdown;
26235 +       struct dentry *dentry, *parent;
26236 +       struct inode *inode, *dir;
26237 +       struct super_block *sb;
26238 +       aufs_bindex_t bopq, bwh, bfound;
26239 +       unsigned char rename_lock;
26240 +};
26241 +
26242 +#define mvd_errno              mvdown.au_errno
26243 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26244 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26245 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26246 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
26247 +
26248 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
26249 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
26250 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
26251 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
26252 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
26253 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
26254 +
26255 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
26256 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
26257 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
26258 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
26259 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
26260 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
26261 +
26262 +#define AU_MVD_PR(flag, ...) do {                      \
26263 +               if (flag)                               \
26264 +                       pr_err(__VA_ARGS__);            \
26265 +       } while (0)
26266 +
26267 +static int find_lower_writable(struct au_mvd_args *a)
26268 +{
26269 +       struct super_block *sb;
26270 +       aufs_bindex_t bindex, bbot;
26271 +       struct au_branch *br;
26272 +
26273 +       sb = a->sb;
26274 +       bindex = a->mvd_bsrc;
26275 +       bbot = au_sbbot(sb);
26276 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
26277 +               for (bindex++; bindex <= bbot; bindex++) {
26278 +                       br = au_sbr(sb, bindex);
26279 +                       if (au_br_fhsm(br->br_perm)
26280 +                           && !sb_rdonly(au_br_sb(br)))
26281 +                               return bindex;
26282 +               }
26283 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
26284 +               for (bindex++; bindex <= bbot; bindex++) {
26285 +                       br = au_sbr(sb, bindex);
26286 +                       if (!au_br_rdonly(br))
26287 +                               return bindex;
26288 +               }
26289 +       else
26290 +               for (bindex++; bindex <= bbot; bindex++) {
26291 +                       br = au_sbr(sb, bindex);
26292 +                       if (!sb_rdonly(au_br_sb(br))) {
26293 +                               if (au_br_rdonly(br))
26294 +                                       a->mvdown.flags
26295 +                                               |= AUFS_MVDOWN_ROLOWER_R;
26296 +                               return bindex;
26297 +                       }
26298 +               }
26299 +
26300 +       return -1;
26301 +}
26302 +
26303 +/* make the parent dir on bdst */
26304 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
26305 +{
26306 +       int err;
26307 +
26308 +       err = 0;
26309 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26310 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26311 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26312 +       a->mvd_h_dst_parent = NULL;
26313 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
26314 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26315 +       if (!a->mvd_h_dst_parent) {
26316 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26317 +               if (unlikely(err)) {
26318 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
26319 +                       goto out;
26320 +               }
26321 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26322 +       }
26323 +
26324 +out:
26325 +       AuTraceErr(err);
26326 +       return err;
26327 +}
26328 +
26329 +/* lock them all */
26330 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
26331 +{
26332 +       int err;
26333 +       struct dentry *h_trap;
26334 +
26335 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26336 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
26337 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26338 +                    au_opt_udba(a->sb),
26339 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26340 +       AuTraceErr(err);
26341 +       if (unlikely(err)) {
26342 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
26343 +               goto out;
26344 +       }
26345 +
26346 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26347 +               a->rename_lock = 0;
26348 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26349 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26350 +                           au_opt_udba(a->sb),
26351 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26352 +               err = au_do_pin(&a->mvd_pin_src);
26353 +               AuTraceErr(err);
26354 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26355 +               if (unlikely(err)) {
26356 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
26357 +                       goto out_dst;
26358 +               }
26359 +               goto out; /* success */
26360 +       }
26361 +
26362 +       a->rename_lock = 1;
26363 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
26364 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26365 +                    au_opt_udba(a->sb),
26366 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26367 +       AuTraceErr(err);
26368 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26369 +       if (unlikely(err)) {
26370 +               AU_MVD_PR(dmsg, "pin_src failed\n");
26371 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26372 +               goto out_dst;
26373 +       }
26374 +       au_pin_hdir_unlock(&a->mvd_pin_src);
26375 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26376 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
26377 +       if (h_trap) {
26378 +               err = (h_trap != a->mvd_h_src_parent);
26379 +               if (err)
26380 +                       err = (h_trap != a->mvd_h_dst_parent);
26381 +       }
26382 +       BUG_ON(err); /* it should never happen */
26383 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26384 +               err = -EBUSY;
26385 +               AuTraceErr(err);
26386 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26387 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26388 +               au_pin_hdir_lock(&a->mvd_pin_src);
26389 +               au_unpin(&a->mvd_pin_src);
26390 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26391 +               goto out_dst;
26392 +       }
26393 +       goto out; /* success */
26394 +
26395 +out_dst:
26396 +       au_unpin(&a->mvd_pin_dst);
26397 +out:
26398 +       AuTraceErr(err);
26399 +       return err;
26400 +}
26401 +
26402 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
26403 +{
26404 +       if (!a->rename_lock)
26405 +               au_unpin(&a->mvd_pin_src);
26406 +       else {
26407 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26408 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26409 +               au_pin_hdir_lock(&a->mvd_pin_src);
26410 +               au_unpin(&a->mvd_pin_src);
26411 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26412 +       }
26413 +       au_unpin(&a->mvd_pin_dst);
26414 +}
26415 +
26416 +/* copy-down the file */
26417 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
26418 +{
26419 +       int err;
26420 +       struct au_cp_generic cpg = {
26421 +               .dentry = a->dentry,
26422 +               .bdst   = a->mvd_bdst,
26423 +               .bsrc   = a->mvd_bsrc,
26424 +               .len    = -1,
26425 +               .pin    = &a->mvd_pin_dst,
26426 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
26427 +       };
26428 +
26429 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
26430 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26431 +               au_fset_cpup(cpg.flags, OVERWRITE);
26432 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26433 +               au_fset_cpup(cpg.flags, RWDST);
26434 +       err = au_sio_cpdown_simple(&cpg);
26435 +       if (unlikely(err))
26436 +               AU_MVD_PR(dmsg, "cpdown failed\n");
26437 +
26438 +       AuTraceErr(err);
26439 +       return err;
26440 +}
26441 +
26442 +/*
26443 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
26444 + * were sleeping
26445 + */
26446 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
26447 +{
26448 +       int err;
26449 +       struct path h_path;
26450 +       struct au_branch *br;
26451 +       struct inode *delegated;
26452 +
26453 +       br = au_sbr(a->sb, a->mvd_bdst);
26454 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26455 +       err = PTR_ERR(h_path.dentry);
26456 +       if (IS_ERR(h_path.dentry)) {
26457 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
26458 +               goto out;
26459 +       }
26460 +
26461 +       err = 0;
26462 +       if (d_is_positive(h_path.dentry)) {
26463 +               h_path.mnt = au_br_mnt(br);
26464 +               delegated = NULL;
26465 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
26466 +                                  &delegated, /*force*/0);
26467 +               if (unlikely(err == -EWOULDBLOCK)) {
26468 +                       pr_warn("cannot retry for NFSv4 delegation"
26469 +                               " for an internal unlink\n");
26470 +                       iput(delegated);
26471 +               }
26472 +               if (unlikely(err))
26473 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
26474 +       }
26475 +       dput(h_path.dentry);
26476 +
26477 +out:
26478 +       AuTraceErr(err);
26479 +       return err;
26480 +}
26481 +
26482 +/*
26483 + * unlink the topmost h_dentry
26484 + */
26485 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
26486 +{
26487 +       int err;
26488 +       struct path h_path;
26489 +       struct inode *delegated;
26490 +
26491 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26492 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26493 +       delegated = NULL;
26494 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26495 +       if (unlikely(err == -EWOULDBLOCK)) {
26496 +               pr_warn("cannot retry for NFSv4 delegation"
26497 +                       " for an internal unlink\n");
26498 +               iput(delegated);
26499 +       }
26500 +       if (unlikely(err))
26501 +               AU_MVD_PR(dmsg, "unlink failed\n");
26502 +
26503 +       AuTraceErr(err);
26504 +       return err;
26505 +}
26506 +
26507 +/* Since mvdown succeeded, we ignore an error of this function */
26508 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26509 +{
26510 +       int err;
26511 +       struct au_branch *br;
26512 +
26513 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26514 +       br = au_sbr(a->sb, a->mvd_bsrc);
26515 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26516 +       if (!err) {
26517 +               br = au_sbr(a->sb, a->mvd_bdst);
26518 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26519 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26520 +       }
26521 +       if (!err)
26522 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26523 +       else
26524 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26525 +}
26526 +
26527 +/*
26528 + * copy-down the file and unlink the bsrc file.
26529 + * - unlink the bdst whout if exist
26530 + * - copy-down the file (with whtmp name and rename)
26531 + * - unlink the bsrc file
26532 + */
26533 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26534 +{
26535 +       int err;
26536 +
26537 +       err = au_do_mkdir(dmsg, a);
26538 +       if (!err)
26539 +               err = au_do_lock(dmsg, a);
26540 +       if (unlikely(err))
26541 +               goto out;
26542 +
26543 +       /*
26544 +        * do not revert the activities we made on bdst since they should be
26545 +        * harmless in aufs.
26546 +        */
26547 +
26548 +       err = au_do_cpdown(dmsg, a);
26549 +       if (!err)
26550 +               err = au_do_unlink_wh(dmsg, a);
26551 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26552 +               err = au_do_unlink(dmsg, a);
26553 +       if (unlikely(err))
26554 +               goto out_unlock;
26555 +
26556 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
26557 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26558 +       if (find_lower_writable(a) < 0)
26559 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26560 +
26561 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26562 +               au_do_stfs(dmsg, a);
26563 +
26564 +       /* maintain internal array */
26565 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26566 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26567 +               au_set_dbtop(a->dentry, a->mvd_bdst);
26568 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26569 +               au_set_ibtop(a->inode, a->mvd_bdst);
26570 +       } else {
26571 +               /* hide the lower */
26572 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26573 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
26574 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26575 +               au_set_ibbot(a->inode, a->mvd_bsrc);
26576 +       }
26577 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
26578 +               au_set_dbbot(a->dentry, a->mvd_bdst);
26579 +       if (au_ibbot(a->inode) < a->mvd_bdst)
26580 +               au_set_ibbot(a->inode, a->mvd_bdst);
26581 +
26582 +out_unlock:
26583 +       au_do_unlock(dmsg, a);
26584 +out:
26585 +       AuTraceErr(err);
26586 +       return err;
26587 +}
26588 +
26589 +/* ---------------------------------------------------------------------- */
26590 +
26591 +/* make sure the file is idle */
26592 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26593 +{
26594 +       int err, plinked;
26595 +
26596 +       err = 0;
26597 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26598 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
26599 +           && au_dcount(a->dentry) == 1
26600 +           && atomic_read(&a->inode->i_count) == 1
26601 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
26602 +           && (!plinked || !au_plink_test(a->inode))
26603 +           && a->inode->i_nlink == 1)
26604 +               goto out;
26605 +
26606 +       err = -EBUSY;
26607 +       AU_MVD_PR(dmsg,
26608 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26609 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26610 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
26611 +                 a->mvd_h_src_inode->i_nlink,
26612 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
26613 +
26614 +out:
26615 +       AuTraceErr(err);
26616 +       return err;
26617 +}
26618 +
26619 +/* make sure the parent dir is fine */
26620 +static int au_mvd_args_parent(const unsigned char dmsg,
26621 +                             struct au_mvd_args *a)
26622 +{
26623 +       int err;
26624 +       aufs_bindex_t bindex;
26625 +
26626 +       err = 0;
26627 +       if (unlikely(au_alive_dir(a->parent))) {
26628 +               err = -ENOENT;
26629 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
26630 +               goto out;
26631 +       }
26632 +
26633 +       a->bopq = au_dbdiropq(a->parent);
26634 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26635 +       AuDbg("b%d\n", bindex);
26636 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26637 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26638 +               err = -EINVAL;
26639 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
26640 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26641 +                         a->bopq, a->mvd_bdst);
26642 +       }
26643 +
26644 +out:
26645 +       AuTraceErr(err);
26646 +       return err;
26647 +}
26648 +
26649 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26650 +                                   struct au_mvd_args *a)
26651 +{
26652 +       int err;
26653 +       struct au_dinfo *dinfo, *tmp;
26654 +
26655 +       /* lookup the next lower positive entry */
26656 +       err = -ENOMEM;
26657 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26658 +       if (unlikely(!tmp))
26659 +               goto out;
26660 +
26661 +       a->bfound = -1;
26662 +       a->bwh = -1;
26663 +       dinfo = au_di(a->dentry);
26664 +       au_di_cp(tmp, dinfo);
26665 +       au_di_swap(tmp, dinfo);
26666 +
26667 +       /* returns the number of positive dentries */
26668 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26669 +                            /* AuLkup_IGNORE_PERM */ 0);
26670 +       if (!err)
26671 +               a->bwh = au_dbwh(a->dentry);
26672 +       else if (err > 0)
26673 +               a->bfound = au_dbtop(a->dentry);
26674 +
26675 +       au_di_swap(tmp, dinfo);
26676 +       au_rw_write_unlock(&tmp->di_rwsem);
26677 +       au_di_free(tmp);
26678 +       if (unlikely(err < 0))
26679 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
26680 +
26681 +       /*
26682 +        * here, we have these cases.
26683 +        * bfound == -1
26684 +        *      no positive dentry under bsrc. there are more sub-cases.
26685 +        *      bwh < 0
26686 +        *              there no whiteout, we can safely move-down.
26687 +        *      bwh <= bsrc
26688 +        *              impossible
26689 +        *      bsrc < bwh && bwh < bdst
26690 +        *              there is a whiteout on RO branch. cannot proceed.
26691 +        *      bwh == bdst
26692 +        *              there is a whiteout on the RW target branch. it should
26693 +        *              be removed.
26694 +        *      bdst < bwh
26695 +        *              there is a whiteout somewhere unrelated branch.
26696 +        * -1 < bfound && bfound <= bsrc
26697 +        *      impossible.
26698 +        * bfound < bdst
26699 +        *      found, but it is on RO branch between bsrc and bdst. cannot
26700 +        *      proceed.
26701 +        * bfound == bdst
26702 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26703 +        *      error.
26704 +        * bdst < bfound
26705 +        *      found, after we create the file on bdst, it will be hidden.
26706 +        */
26707 +
26708 +       AuDebugOn(a->bfound == -1
26709 +                 && a->bwh != -1
26710 +                 && a->bwh <= a->mvd_bsrc);
26711 +       AuDebugOn(-1 < a->bfound
26712 +                 && a->bfound <= a->mvd_bsrc);
26713 +
26714 +       err = -EINVAL;
26715 +       if (a->bfound == -1
26716 +           && a->mvd_bsrc < a->bwh
26717 +           && a->bwh != -1
26718 +           && a->bwh < a->mvd_bdst) {
26719 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26720 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26721 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26722 +               goto out;
26723 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26724 +               a->mvd_errno = EAU_MVDOWN_UPPER;
26725 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26726 +                         a->mvd_bdst, a->bfound);
26727 +               goto out;
26728 +       }
26729 +
26730 +       err = 0; /* success */
26731 +
26732 +out:
26733 +       AuTraceErr(err);
26734 +       return err;
26735 +}
26736 +
26737 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26738 +{
26739 +       int err;
26740 +
26741 +       err = 0;
26742 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26743 +           && a->bfound == a->mvd_bdst)
26744 +               err = -EEXIST;
26745 +       AuTraceErr(err);
26746 +       return err;
26747 +}
26748 +
26749 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26750 +{
26751 +       int err;
26752 +       struct au_branch *br;
26753 +
26754 +       err = -EISDIR;
26755 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
26756 +               goto out;
26757 +
26758 +       err = -EINVAL;
26759 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26760 +               a->mvd_bsrc = au_ibtop(a->inode);
26761 +       else {
26762 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26763 +               if (unlikely(a->mvd_bsrc < 0
26764 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
26765 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
26766 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
26767 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
26768 +                                || au_ibbot(a->inode) < a->mvd_bsrc
26769 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26770 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
26771 +                       AU_MVD_PR(dmsg, "no upper\n");
26772 +                       goto out;
26773 +               }
26774 +       }
26775 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26776 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
26777 +               AU_MVD_PR(dmsg, "on the bottom\n");
26778 +               goto out;
26779 +       }
26780 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26781 +       br = au_sbr(a->sb, a->mvd_bsrc);
26782 +       err = au_br_rdonly(br);
26783 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26784 +               if (unlikely(err))
26785 +                       goto out;
26786 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26787 +                    || IS_APPEND(a->mvd_h_src_inode))) {
26788 +               if (err)
26789 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26790 +               /* go on */
26791 +       } else
26792 +               goto out;
26793 +
26794 +       err = -EINVAL;
26795 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26796 +               a->mvd_bdst = find_lower_writable(a);
26797 +               if (unlikely(a->mvd_bdst < 0)) {
26798 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
26799 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
26800 +                       goto out;
26801 +               }
26802 +       } else {
26803 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26804 +               if (unlikely(a->mvd_bdst < 0
26805 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
26806 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26807 +                       AU_MVD_PR(dmsg, "no lower brid\n");
26808 +                       goto out;
26809 +               }
26810 +       }
26811 +
26812 +       err = au_mvd_args_busy(dmsg, a);
26813 +       if (!err)
26814 +               err = au_mvd_args_parent(dmsg, a);
26815 +       if (!err)
26816 +               err = au_mvd_args_intermediate(dmsg, a);
26817 +       if (!err)
26818 +               err = au_mvd_args_exist(dmsg, a);
26819 +       if (!err)
26820 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26821 +
26822 +out:
26823 +       AuTraceErr(err);
26824 +       return err;
26825 +}
26826 +
26827 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26828 +{
26829 +       int err, e;
26830 +       unsigned char dmsg;
26831 +       struct au_mvd_args *args;
26832 +       struct inode *inode;
26833 +
26834 +       inode = d_inode(dentry);
26835 +       err = -EPERM;
26836 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26837 +               goto out;
26838 +
26839 +       err = -ENOMEM;
26840 +       args = kmalloc(sizeof(*args), GFP_NOFS);
26841 +       if (unlikely(!args))
26842 +               goto out;
26843 +
26844 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26845 +       if (!err)
26846 +               /* VERIFY_WRITE */
26847 +               err = !access_ok(uarg, sizeof(*uarg));
26848 +       if (unlikely(err)) {
26849 +               err = -EFAULT;
26850 +               AuTraceErr(err);
26851 +               goto out_free;
26852 +       }
26853 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
26854 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26855 +       args->mvdown.au_errno = 0;
26856 +       args->dentry = dentry;
26857 +       args->inode = inode;
26858 +       args->sb = dentry->d_sb;
26859 +
26860 +       err = -ENOENT;
26861 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26862 +       args->parent = dget_parent(dentry);
26863 +       args->dir = d_inode(args->parent);
26864 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
26865 +       dput(args->parent);
26866 +       if (unlikely(args->parent != dentry->d_parent)) {
26867 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
26868 +               goto out_dir;
26869 +       }
26870 +
26871 +       inode_lock_nested(inode, I_MUTEX_CHILD);
26872 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26873 +       if (unlikely(err))
26874 +               goto out_inode;
26875 +
26876 +       di_write_lock_parent(args->parent);
26877 +       err = au_mvd_args(dmsg, args);
26878 +       if (unlikely(err))
26879 +               goto out_parent;
26880 +
26881 +       err = au_do_mvdown(dmsg, args);
26882 +       if (unlikely(err))
26883 +               goto out_parent;
26884 +
26885 +       au_cpup_attr_timesizes(args->dir);
26886 +       au_cpup_attr_timesizes(inode);
26887 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26888 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26889 +       /* au_digen_dec(dentry); */
26890 +
26891 +out_parent:
26892 +       di_write_unlock(args->parent);
26893 +       aufs_read_unlock(dentry, AuLock_DW);
26894 +out_inode:
26895 +       inode_unlock(inode);
26896 +out_dir:
26897 +       inode_unlock(args->dir);
26898 +out_free:
26899 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26900 +       if (unlikely(e))
26901 +               err = -EFAULT;
26902 +       au_kfree_rcu(args);
26903 +out:
26904 +       AuTraceErr(err);
26905 +       return err;
26906 +}
26907 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
26908 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
26909 +++ linux/fs/aufs/opts.c        2022-11-05 23:02:18.969222617 +0100
26910 @@ -0,0 +1,1032 @@
26911 +// SPDX-License-Identifier: GPL-2.0
26912 +/*
26913 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26914 + *
26915 + * This program is free software; you can redistribute it and/or modify
26916 + * it under the terms of the GNU General Public License as published by
26917 + * the Free Software Foundation; either version 2 of the License, or
26918 + * (at your option) any later version.
26919 + *
26920 + * This program is distributed in the hope that it will be useful,
26921 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26922 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26923 + * GNU General Public License for more details.
26924 + *
26925 + * You should have received a copy of the GNU General Public License
26926 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26927 + */
26928 +
26929 +/*
26930 + * mount options/flags
26931 + */
26932 +
26933 +#include <linux/types.h> /* a distribution requires */
26934 +#include <linux/parser.h>
26935 +#include "aufs.h"
26936 +
26937 +/* ---------------------------------------------------------------------- */
26938 +
26939 +static const char *au_parser_pattern(int val, match_table_t tbl)
26940 +{
26941 +       struct match_token *p;
26942 +
26943 +       p = tbl;
26944 +       while (p->pattern) {
26945 +               if (p->token == val)
26946 +                       return p->pattern;
26947 +               p++;
26948 +       }
26949 +       BUG();
26950 +       return "??";
26951 +}
26952 +
26953 +static const char *au_optstr(int *val, match_table_t tbl)
26954 +{
26955 +       struct match_token *p;
26956 +       int v;
26957 +
26958 +       v = *val;
26959 +       if (!v)
26960 +               goto out;
26961 +       p = tbl;
26962 +       while (p->pattern) {
26963 +               if (p->token
26964 +                   && (v & p->token) == p->token) {
26965 +                       *val &= ~p->token;
26966 +                       return p->pattern;
26967 +               }
26968 +               p++;
26969 +       }
26970 +
26971 +out:
26972 +       return NULL;
26973 +}
26974 +
26975 +/* ---------------------------------------------------------------------- */
26976 +
26977 +static match_table_t brperm = {
26978 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
26979 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
26980 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
26981 +       {0, NULL}
26982 +};
26983 +
26984 +static match_table_t brattr = {
26985 +       /* general */
26986 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26987 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
26988 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
26989 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
26990 +#ifdef CONFIG_AUFS_FHSM
26991 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
26992 +#endif
26993 +#ifdef CONFIG_AUFS_XATTR
26994 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26995 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26996 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26997 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26998 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26999 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
27000 +#endif
27001 +
27002 +       /* ro/rr branch */
27003 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
27004 +
27005 +       /* rw branch */
27006 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
27007 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
27008 +
27009 +       {0, NULL}
27010 +};
27011 +
27012 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
27013 +{
27014 +       int attr, v;
27015 +       char *p;
27016 +
27017 +       attr = 0;
27018 +       do {
27019 +               p = strchr(str, '+');
27020 +               if (p)
27021 +                       *p = 0;
27022 +               v = match_token(str, table, args);
27023 +               if (v) {
27024 +                       if (v & AuBrAttr_CMOO_Mask)
27025 +                               attr &= ~AuBrAttr_CMOO_Mask;
27026 +                       attr |= v;
27027 +               } else {
27028 +                       if (p)
27029 +                               *p = '+';
27030 +                       pr_warn("ignored branch attribute %s\n", str);
27031 +                       break;
27032 +               }
27033 +               if (p)
27034 +                       str = p + 1;
27035 +       } while (p);
27036 +
27037 +       return attr;
27038 +}
27039 +
27040 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
27041 +{
27042 +       int sz;
27043 +       const char *p;
27044 +       char *q;
27045 +
27046 +       q = str->a;
27047 +       *q = 0;
27048 +       p = au_optstr(&perm, brattr);
27049 +       if (p) {
27050 +               sz = strlen(p);
27051 +               memcpy(q, p, sz + 1);
27052 +               q += sz;
27053 +       } else
27054 +               goto out;
27055 +
27056 +       do {
27057 +               p = au_optstr(&perm, brattr);
27058 +               if (p) {
27059 +                       *q++ = '+';
27060 +                       sz = strlen(p);
27061 +                       memcpy(q, p, sz + 1);
27062 +                       q += sz;
27063 +               }
27064 +       } while (p);
27065 +
27066 +out:
27067 +       return q - str->a;
27068 +}
27069 +
27070 +int au_br_perm_val(char *perm)
27071 +{
27072 +       int val, bad, sz;
27073 +       char *p;
27074 +       substring_t args[MAX_OPT_ARGS];
27075 +       au_br_perm_str_t attr;
27076 +
27077 +       p = strchr(perm, '+');
27078 +       if (p)
27079 +               *p = 0;
27080 +       val = match_token(perm, brperm, args);
27081 +       if (!val) {
27082 +               if (p)
27083 +                       *p = '+';
27084 +               pr_warn("ignored branch permission %s\n", perm);
27085 +               val = AuBrPerm_RO;
27086 +               goto out;
27087 +       }
27088 +       if (!p)
27089 +               goto out;
27090 +
27091 +       val |= br_attr_val(p + 1, brattr, args);
27092 +
27093 +       bad = 0;
27094 +       switch (val & AuBrPerm_Mask) {
27095 +       case AuBrPerm_RO:
27096 +       case AuBrPerm_RR:
27097 +               bad = val & AuBrWAttr_Mask;
27098 +               val &= ~AuBrWAttr_Mask;
27099 +               break;
27100 +       case AuBrPerm_RW:
27101 +               bad = val & AuBrRAttr_Mask;
27102 +               val &= ~AuBrRAttr_Mask;
27103 +               break;
27104 +       }
27105 +
27106 +       /*
27107 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27108 +        * does not treat it as an error, just warning.
27109 +        * this is a tiny guard for the user operation.
27110 +        */
27111 +       if (val & AuBrAttr_UNPIN) {
27112 +               bad |= AuBrAttr_UNPIN;
27113 +               val &= ~AuBrAttr_UNPIN;
27114 +       }
27115 +
27116 +       if (unlikely(bad)) {
27117 +               sz = au_do_optstr_br_attr(&attr, bad);
27118 +               AuDebugOn(!sz);
27119 +               pr_warn("ignored branch attribute %s\n", attr.a);
27120 +       }
27121 +
27122 +out:
27123 +       return val;
27124 +}
27125 +
27126 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
27127 +{
27128 +       au_br_perm_str_t attr;
27129 +       const char *p;
27130 +       char *q;
27131 +       int sz;
27132 +
27133 +       q = str->a;
27134 +       p = au_optstr(&perm, brperm);
27135 +       AuDebugOn(!p || !*p);
27136 +       sz = strlen(p);
27137 +       memcpy(q, p, sz + 1);
27138 +       q += sz;
27139 +
27140 +       sz = au_do_optstr_br_attr(&attr, perm);
27141 +       if (sz) {
27142 +               *q++ = '+';
27143 +               memcpy(q, attr.a, sz + 1);
27144 +       }
27145 +
27146 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
27147 +}
27148 +
27149 +/* ---------------------------------------------------------------------- */
27150 +
27151 +static match_table_t udbalevel = {
27152 +       {AuOpt_UDBA_REVAL, "reval"},
27153 +       {AuOpt_UDBA_NONE, "none"},
27154 +#ifdef CONFIG_AUFS_HNOTIFY
27155 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27156 +#ifdef CONFIG_AUFS_HFSNOTIFY
27157 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
27158 +#endif
27159 +#endif
27160 +       {-1, NULL}
27161 +};
27162 +
27163 +int au_udba_val(char *str)
27164 +{
27165 +       substring_t args[MAX_OPT_ARGS];
27166 +
27167 +       return match_token(str, udbalevel, args);
27168 +}
27169 +
27170 +const char *au_optstr_udba(int udba)
27171 +{
27172 +       return au_parser_pattern(udba, udbalevel);
27173 +}
27174 +
27175 +/* ---------------------------------------------------------------------- */
27176 +
27177 +static match_table_t au_wbr_create_policy = {
27178 +       {AuWbrCreate_TDP, "tdp"},
27179 +       {AuWbrCreate_TDP, "top-down-parent"},
27180 +       {AuWbrCreate_RR, "rr"},
27181 +       {AuWbrCreate_RR, "round-robin"},
27182 +       {AuWbrCreate_MFS, "mfs"},
27183 +       {AuWbrCreate_MFS, "most-free-space"},
27184 +       {AuWbrCreate_MFSV, "mfs:%d"},
27185 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
27186 +
27187 +       /* top-down regardless the parent, and then mfs */
27188 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
27189 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27190 +
27191 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
27192 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27193 +       {AuWbrCreate_PMFS, "pmfs"},
27194 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
27195 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27196 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
27197 +
27198 +       {-1, NULL}
27199 +};
27200 +
27201 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27202 +                           struct au_opt_wbr_create *create)
27203 +{
27204 +       int err;
27205 +       unsigned long long ull;
27206 +
27207 +       err = 0;
27208 +       if (!match_u64(arg, &ull))
27209 +               create->mfsrr_watermark = ull;
27210 +       else {
27211 +               pr_err("bad integer in %s\n", str);
27212 +               err = -EINVAL;
27213 +       }
27214 +
27215 +       return err;
27216 +}
27217 +
27218 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
27219 +                         struct au_opt_wbr_create *create)
27220 +{
27221 +       int n, err;
27222 +
27223 +       err = 0;
27224 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
27225 +               create->mfs_second = n;
27226 +       else {
27227 +               pr_err("bad integer in %s\n", str);
27228 +               err = -EINVAL;
27229 +       }
27230 +
27231 +       return err;
27232 +}
27233 +
27234 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
27235 +{
27236 +       int err, e;
27237 +       substring_t args[MAX_OPT_ARGS];
27238 +
27239 +       err = match_token(str, au_wbr_create_policy, args);
27240 +       create->wbr_create = err;
27241 +       switch (err) {
27242 +       case AuWbrCreate_MFSRRV:
27243 +       case AuWbrCreate_TDMFSV:
27244 +       case AuWbrCreate_PMFSRRV:
27245 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27246 +               if (!e)
27247 +                       e = au_wbr_mfs_sec(&args[1], str, create);
27248 +               if (unlikely(e))
27249 +                       err = e;
27250 +               break;
27251 +       case AuWbrCreate_MFSRR:
27252 +       case AuWbrCreate_TDMFS:
27253 +       case AuWbrCreate_PMFSRR:
27254 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27255 +               if (unlikely(e)) {
27256 +                       err = e;
27257 +                       break;
27258 +               }
27259 +               fallthrough;
27260 +       case AuWbrCreate_MFS:
27261 +       case AuWbrCreate_PMFS:
27262 +               create->mfs_second = AUFS_MFS_DEF_SEC;
27263 +               break;
27264 +       case AuWbrCreate_MFSV:
27265 +       case AuWbrCreate_PMFSV:
27266 +               e = au_wbr_mfs_sec(&args[0], str, create);
27267 +               if (unlikely(e))
27268 +                       err = e;
27269 +               break;
27270 +       }
27271 +
27272 +       return err;
27273 +}
27274 +
27275 +const char *au_optstr_wbr_create(int wbr_create)
27276 +{
27277 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
27278 +}
27279 +
27280 +static match_table_t au_wbr_copyup_policy = {
27281 +       {AuWbrCopyup_TDP, "tdp"},
27282 +       {AuWbrCopyup_TDP, "top-down-parent"},
27283 +       {AuWbrCopyup_BUP, "bup"},
27284 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
27285 +       {AuWbrCopyup_BU, "bu"},
27286 +       {AuWbrCopyup_BU, "bottom-up"},
27287 +       {-1, NULL}
27288 +};
27289 +
27290 +int au_wbr_copyup_val(char *str)
27291 +{
27292 +       substring_t args[MAX_OPT_ARGS];
27293 +
27294 +       return match_token(str, au_wbr_copyup_policy, args);
27295 +}
27296 +
27297 +const char *au_optstr_wbr_copyup(int wbr_copyup)
27298 +{
27299 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
27300 +}
27301 +
27302 +/* ---------------------------------------------------------------------- */
27303 +
27304 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27305 +              aufs_bindex_t bindex)
27306 +{
27307 +       int err;
27308 +       struct au_opt_add *add = &opt->add;
27309 +       char *p;
27310 +
27311 +       add->bindex = bindex;
27312 +       add->perm = AuBrPerm_RO;
27313 +       add->pathname = opt_str;
27314 +       p = strchr(opt_str, '=');
27315 +       if (p) {
27316 +               *p++ = 0;
27317 +               if (*p)
27318 +                       add->perm = au_br_perm_val(p);
27319 +       }
27320 +
27321 +       err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
27322 +       if (!err) {
27323 +               if (!p) {
27324 +                       add->perm = AuBrPerm_RO;
27325 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
27326 +                               add->perm = AuBrPerm_RR;
27327 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
27328 +                               add->perm = AuBrPerm_RW;
27329 +               }
27330 +               opt->type = Opt_add;
27331 +               goto out;
27332 +       }
27333 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
27334 +       err = -EINVAL;
27335 +
27336 +out:
27337 +       return err;
27338 +}
27339 +
27340 +static int au_opt_wbr_create(struct super_block *sb,
27341 +                            struct au_opt_wbr_create *create)
27342 +{
27343 +       int err;
27344 +       struct au_sbinfo *sbinfo;
27345 +
27346 +       SiMustWriteLock(sb);
27347 +
27348 +       err = 1; /* handled */
27349 +       sbinfo = au_sbi(sb);
27350 +       if (sbinfo->si_wbr_create_ops->fin) {
27351 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27352 +               if (!err)
27353 +                       err = 1;
27354 +       }
27355 +
27356 +       sbinfo->si_wbr_create = create->wbr_create;
27357 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27358 +       switch (create->wbr_create) {
27359 +       case AuWbrCreate_MFSRRV:
27360 +       case AuWbrCreate_MFSRR:
27361 +       case AuWbrCreate_TDMFS:
27362 +       case AuWbrCreate_TDMFSV:
27363 +       case AuWbrCreate_PMFSRR:
27364 +       case AuWbrCreate_PMFSRRV:
27365 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27366 +               fallthrough;
27367 +       case AuWbrCreate_MFS:
27368 +       case AuWbrCreate_MFSV:
27369 +       case AuWbrCreate_PMFS:
27370 +       case AuWbrCreate_PMFSV:
27371 +               sbinfo->si_wbr_mfs.mfs_expire
27372 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27373 +               break;
27374 +       }
27375 +
27376 +       if (sbinfo->si_wbr_create_ops->init)
27377 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27378 +
27379 +       return err;
27380 +}
27381 +
27382 +/*
27383 + * returns,
27384 + * plus: processed without an error
27385 + * zero: unprocessed
27386 + */
27387 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27388 +                        struct au_opts *opts)
27389 +{
27390 +       int err;
27391 +       struct au_sbinfo *sbinfo;
27392 +
27393 +       SiMustWriteLock(sb);
27394 +
27395 +       err = 1; /* handled */
27396 +       sbinfo = au_sbi(sb);
27397 +       switch (opt->type) {
27398 +       case Opt_udba:
27399 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27400 +               sbinfo->si_mntflags |= opt->udba;
27401 +               opts->given_udba |= opt->udba;
27402 +               break;
27403 +
27404 +       case Opt_plink:
27405 +               if (opt->tf)
27406 +                       au_opt_set(sbinfo->si_mntflags, PLINK);
27407 +               else {
27408 +                       if (au_opt_test(sbinfo->si_mntflags, PLINK))
27409 +                               au_plink_put(sb, /*verbose*/1);
27410 +                       au_opt_clr(sbinfo->si_mntflags, PLINK);
27411 +               }
27412 +               break;
27413 +       case Opt_list_plink:
27414 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27415 +                       au_plink_list(sb);
27416 +               break;
27417 +
27418 +       case Opt_dio:
27419 +               if (opt->tf) {
27420 +                       au_opt_set(sbinfo->si_mntflags, DIO);
27421 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27422 +               } else {
27423 +                       au_opt_clr(sbinfo->si_mntflags, DIO);
27424 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27425 +               }
27426 +               break;
27427 +
27428 +       case Opt_fhsm_sec:
27429 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27430 +               break;
27431 +
27432 +       case Opt_diropq_a:
27433 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27434 +               break;
27435 +       case Opt_diropq_w:
27436 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27437 +               break;
27438 +
27439 +       case Opt_warn_perm:
27440 +               if (opt->tf)
27441 +                       au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27442 +               else
27443 +                       au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27444 +               break;
27445 +
27446 +       case Opt_verbose:
27447 +               if (opt->tf)
27448 +                       au_opt_set(sbinfo->si_mntflags, VERBOSE);
27449 +               else
27450 +                       au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27451 +               break;
27452 +
27453 +       case Opt_sum:
27454 +               if (opt->tf)
27455 +                       au_opt_set(sbinfo->si_mntflags, SUM);
27456 +               else {
27457 +                       au_opt_clr(sbinfo->si_mntflags, SUM);
27458 +                       au_opt_clr(sbinfo->si_mntflags, SUM_W);
27459 +               }
27460 +               break;
27461 +       case Opt_wsum:
27462 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27463 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27464 +               break;
27465 +
27466 +       case Opt_wbr_create:
27467 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27468 +               break;
27469 +       case Opt_wbr_copyup:
27470 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27471 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27472 +               break;
27473 +
27474 +       case Opt_dirwh:
27475 +               sbinfo->si_dirwh = opt->dirwh;
27476 +               break;
27477 +
27478 +       case Opt_rdcache:
27479 +               sbinfo->si_rdcache
27480 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27481 +               break;
27482 +       case Opt_rdblk:
27483 +               sbinfo->si_rdblk = opt->rdblk;
27484 +               break;
27485 +       case Opt_rdhash:
27486 +               sbinfo->si_rdhash = opt->rdhash;
27487 +               break;
27488 +
27489 +       case Opt_shwh:
27490 +               if (opt->tf)
27491 +                       au_opt_set(sbinfo->si_mntflags, SHWH);
27492 +               else
27493 +                       au_opt_clr(sbinfo->si_mntflags, SHWH);
27494 +               break;
27495 +
27496 +       case Opt_dirperm1:
27497 +               if (opt->tf)
27498 +                       au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27499 +               else
27500 +                       au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27501 +               break;
27502 +
27503 +       case Opt_trunc_xino:
27504 +               if (opt->tf)
27505 +                       au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27506 +               else
27507 +                       au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27508 +               break;
27509 +
27510 +       case Opt_trunc_xino_path:
27511 +       case Opt_itrunc_xino:
27512 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27513 +                                   /*idx_begin*/0);
27514 +               if (!err)
27515 +                       err = 1;
27516 +               break;
27517 +
27518 +       case Opt_trunc_xib:
27519 +               if (opt->tf)
27520 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27521 +               else
27522 +                       au_fclr_opts(opts->flags, TRUNC_XIB);
27523 +               break;
27524 +
27525 +       case Opt_dirren:
27526 +               err = 1;
27527 +               if (opt->tf) {
27528 +                       if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27529 +                               err = au_dr_opt_set(sb);
27530 +                               if (!err)
27531 +                                       err = 1;
27532 +                       }
27533 +                       if (err == 1)
27534 +                               au_opt_set(sbinfo->si_mntflags, DIRREN);
27535 +               } else {
27536 +                       if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27537 +                               err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27538 +                                                                     DR_FLUSHED));
27539 +                               if (!err)
27540 +                                       err = 1;
27541 +                       }
27542 +                       if (err == 1)
27543 +                               au_opt_clr(sbinfo->si_mntflags, DIRREN);
27544 +               }
27545 +               break;
27546 +
27547 +       case Opt_acl:
27548 +               if (opt->tf)
27549 +                       sb->s_flags |= SB_POSIXACL;
27550 +               else
27551 +                       sb->s_flags &= ~SB_POSIXACL;
27552 +               break;
27553 +
27554 +       default:
27555 +               err = 0;
27556 +               break;
27557 +       }
27558 +
27559 +       return err;
27560 +}
27561 +
27562 +/*
27563 + * returns tri-state.
27564 + * plus: processed without an error
27565 + * zero: unprocessed
27566 + * minus: error
27567 + */
27568 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27569 +                    struct au_opts *opts)
27570 +{
27571 +       int err, do_refresh;
27572 +
27573 +       err = 0;
27574 +       switch (opt->type) {
27575 +       case Opt_append:
27576 +               opt->add.bindex = au_sbbot(sb) + 1;
27577 +               if (opt->add.bindex < 0)
27578 +                       opt->add.bindex = 0;
27579 +               goto add;
27580 +               /* Always goto add, not fallthrough */
27581 +       case Opt_prepend:
27582 +               opt->add.bindex = 0;
27583 +               fallthrough;
27584 +       add: /* indented label */
27585 +       case Opt_add:
27586 +               err = au_br_add(sb, &opt->add,
27587 +                               au_ftest_opts(opts->flags, REMOUNT));
27588 +               if (!err) {
27589 +                       err = 1;
27590 +                       au_fset_opts(opts->flags, REFRESH);
27591 +               }
27592 +               break;
27593 +
27594 +       case Opt_del:
27595 +       case Opt_idel:
27596 +               err = au_br_del(sb, &opt->del,
27597 +                               au_ftest_opts(opts->flags, REMOUNT));
27598 +               if (!err) {
27599 +                       err = 1;
27600 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27601 +                       au_fset_opts(opts->flags, REFRESH);
27602 +               }
27603 +               break;
27604 +
27605 +       case Opt_mod:
27606 +       case Opt_imod:
27607 +               err = au_br_mod(sb, &opt->mod,
27608 +                               au_ftest_opts(opts->flags, REMOUNT),
27609 +                               &do_refresh);
27610 +               if (!err) {
27611 +                       err = 1;
27612 +                       if (do_refresh)
27613 +                               au_fset_opts(opts->flags, REFRESH);
27614 +               }
27615 +               break;
27616 +       }
27617 +       return err;
27618 +}
27619 +
27620 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27621 +                      struct au_opt_xino **opt_xino,
27622 +                      struct au_opts *opts)
27623 +{
27624 +       int err;
27625 +
27626 +       err = 0;
27627 +       switch (opt->type) {
27628 +       case Opt_xino:
27629 +               err = au_xino_set(sb, &opt->xino,
27630 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27631 +               if (!err)
27632 +                       *opt_xino = &opt->xino;
27633 +               break;
27634 +       case Opt_noxino:
27635 +               au_xino_clr(sb);
27636 +               *opt_xino = (void *)-1;
27637 +               break;
27638 +       }
27639 +
27640 +       return err;
27641 +}
27642 +
27643 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27644 +                  unsigned int pending)
27645 +{
27646 +       int err, fhsm;
27647 +       aufs_bindex_t bindex, bbot;
27648 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27649 +       struct au_branch *br;
27650 +       struct au_wbr *wbr;
27651 +       struct dentry *root, *dentry;
27652 +       struct inode *dir, *h_dir;
27653 +       struct au_sbinfo *sbinfo;
27654 +       struct au_hinode *hdir;
27655 +
27656 +       SiMustAnyLock(sb);
27657 +
27658 +       sbinfo = au_sbi(sb);
27659 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27660 +
27661 +       if (!(sb_flags & SB_RDONLY)) {
27662 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27663 +                       pr_warn("first branch should be rw\n");
27664 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27665 +                       pr_warn_once("shwh should be used with ro\n");
27666 +       }
27667 +
27668 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27669 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27670 +               pr_warn_once("udba=*notify requires xino\n");
27671 +
27672 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27673 +               pr_warn_once("dirperm1 breaks the protection"
27674 +                            " by the permission bits on the lower branch\n");
27675 +
27676 +       err = 0;
27677 +       fhsm = 0;
27678 +       root = sb->s_root;
27679 +       dir = d_inode(root);
27680 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27681 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27682 +                                     UDBA_NONE);
27683 +       bbot = au_sbbot(sb);
27684 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27685 +               skip = 0;
27686 +               h_dir = au_h_iptr(dir, bindex);
27687 +               br = au_sbr(sb, bindex);
27688 +
27689 +               if ((br->br_perm & AuBrAttr_ICEX)
27690 +                   && !h_dir->i_op->listxattr)
27691 +                       br->br_perm &= ~AuBrAttr_ICEX;
27692 +#if 0 /* untested */
27693 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27694 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27695 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27696 +#endif
27697 +
27698 +               do_free = 0;
27699 +               wbr = br->br_wbr;
27700 +               if (wbr)
27701 +                       wbr_wh_read_lock(wbr);
27702 +
27703 +               if (!au_br_writable(br->br_perm)) {
27704 +                       do_free = !!wbr;
27705 +                       skip = (!wbr
27706 +                               || (!wbr->wbr_whbase
27707 +                                   && !wbr->wbr_plink
27708 +                                   && !wbr->wbr_orph));
27709 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27710 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27711 +                       skip = (!wbr || !wbr->wbr_whbase);
27712 +                       if (skip && wbr) {
27713 +                               if (do_plink)
27714 +                                       skip = !!wbr->wbr_plink;
27715 +                               else
27716 +                                       skip = !wbr->wbr_plink;
27717 +                       }
27718 +               } else {
27719 +                       /* skip = (br->br_whbase && br->br_ohph); */
27720 +                       skip = (wbr && wbr->wbr_whbase);
27721 +                       if (skip) {
27722 +                               if (do_plink)
27723 +                                       skip = !!wbr->wbr_plink;
27724 +                               else
27725 +                                       skip = !wbr->wbr_plink;
27726 +                       }
27727 +               }
27728 +               if (wbr)
27729 +                       wbr_wh_read_unlock(wbr);
27730 +
27731 +               if (can_no_dreval) {
27732 +                       dentry = br->br_path.dentry;
27733 +                       spin_lock(&dentry->d_lock);
27734 +                       if (dentry->d_flags &
27735 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27736 +                               can_no_dreval = 0;
27737 +                       spin_unlock(&dentry->d_lock);
27738 +               }
27739 +
27740 +               if (au_br_fhsm(br->br_perm)) {
27741 +                       fhsm++;
27742 +                       AuDebugOn(!br->br_fhsm);
27743 +               }
27744 +
27745 +               if (skip)
27746 +                       continue;
27747 +
27748 +               hdir = au_hi(dir, bindex);
27749 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27750 +               if (wbr)
27751 +                       wbr_wh_write_lock(wbr);
27752 +               err = au_wh_init(br, sb);
27753 +               if (wbr)
27754 +                       wbr_wh_write_unlock(wbr);
27755 +               au_hn_inode_unlock(hdir);
27756 +
27757 +               if (!err && do_free) {
27758 +                       au_kfree_rcu(wbr);
27759 +                       br->br_wbr = NULL;
27760 +               }
27761 +       }
27762 +
27763 +       if (can_no_dreval)
27764 +               au_fset_si(sbinfo, NO_DREVAL);
27765 +       else
27766 +               au_fclr_si(sbinfo, NO_DREVAL);
27767 +
27768 +       if (fhsm >= 2) {
27769 +               au_fset_si(sbinfo, FHSM);
27770 +               for (bindex = bbot; bindex >= 0; bindex--) {
27771 +                       br = au_sbr(sb, bindex);
27772 +                       if (au_br_fhsm(br->br_perm)) {
27773 +                               au_fhsm_set_bottom(sb, bindex);
27774 +                               break;
27775 +                       }
27776 +               }
27777 +       } else {
27778 +               au_fclr_si(sbinfo, FHSM);
27779 +               au_fhsm_set_bottom(sb, -1);
27780 +       }
27781 +
27782 +       return err;
27783 +}
27784 +
27785 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27786 +{
27787 +       int err;
27788 +       unsigned int tmp;
27789 +       aufs_bindex_t bindex, bbot;
27790 +       struct au_opt *opt;
27791 +       struct au_opt_xino *opt_xino, xino;
27792 +       struct au_sbinfo *sbinfo;
27793 +       struct au_branch *br;
27794 +       struct inode *dir;
27795 +
27796 +       SiMustWriteLock(sb);
27797 +
27798 +       err = 0;
27799 +       opt_xino = NULL;
27800 +       opt = opts->opt;
27801 +       while (err >= 0 && opt->type != Opt_tail)
27802 +               err = au_opt_simple(sb, opt++, opts);
27803 +       if (err > 0)
27804 +               err = 0;
27805 +       else if (unlikely(err < 0))
27806 +               goto out;
27807 +
27808 +       /* disable xino and udba temporary */
27809 +       sbinfo = au_sbi(sb);
27810 +       tmp = sbinfo->si_mntflags;
27811 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27812 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27813 +
27814 +       opt = opts->opt;
27815 +       while (err >= 0 && opt->type != Opt_tail)
27816 +               err = au_opt_br(sb, opt++, opts);
27817 +       if (err > 0)
27818 +               err = 0;
27819 +       else if (unlikely(err < 0))
27820 +               goto out;
27821 +
27822 +       bbot = au_sbbot(sb);
27823 +       if (unlikely(bbot < 0)) {
27824 +               err = -EINVAL;
27825 +               pr_err("no branches\n");
27826 +               goto out;
27827 +       }
27828 +
27829 +       if (au_opt_test(tmp, XINO))
27830 +               au_opt_set(sbinfo->si_mntflags, XINO);
27831 +       opt = opts->opt;
27832 +       while (!err && opt->type != Opt_tail)
27833 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27834 +       if (unlikely(err))
27835 +               goto out;
27836 +
27837 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27838 +       if (unlikely(err))
27839 +               goto out;
27840 +
27841 +       /* restore xino */
27842 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27843 +               xino.file = au_xino_def(sb);
27844 +               err = PTR_ERR(xino.file);
27845 +               if (IS_ERR(xino.file))
27846 +                       goto out;
27847 +
27848 +               err = au_xino_set(sb, &xino, /*remount*/0);
27849 +               fput(xino.file);
27850 +               if (unlikely(err))
27851 +                       goto out;
27852 +       }
27853 +
27854 +       /* restore udba */
27855 +       tmp &= AuOptMask_UDBA;
27856 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27857 +       sbinfo->si_mntflags |= tmp;
27858 +       bbot = au_sbbot(sb);
27859 +       for (bindex = 0; bindex <= bbot; bindex++) {
27860 +               br = au_sbr(sb, bindex);
27861 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27862 +               if (unlikely(err))
27863 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27864 +                               bindex, err);
27865 +               /* go on even if err */
27866 +       }
27867 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27868 +               dir = d_inode(sb->s_root);
27869 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27870 +       }
27871 +
27872 +out:
27873 +       return err;
27874 +}
27875 +
27876 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27877 +{
27878 +       int err, rerr;
27879 +       unsigned char no_dreval;
27880 +       struct inode *dir;
27881 +       struct au_opt_xino *opt_xino;
27882 +       struct au_opt *opt;
27883 +       struct au_sbinfo *sbinfo;
27884 +
27885 +       SiMustWriteLock(sb);
27886 +
27887 +       err = au_dr_opt_flush(sb);
27888 +       if (unlikely(err))
27889 +               goto out;
27890 +       au_fset_opts(opts->flags, DR_FLUSHED);
27891 +
27892 +       dir = d_inode(sb->s_root);
27893 +       sbinfo = au_sbi(sb);
27894 +       opt_xino = NULL;
27895 +       opt = opts->opt;
27896 +       while (err >= 0 && opt->type != Opt_tail) {
27897 +               err = au_opt_simple(sb, opt, opts);
27898 +               if (!err)
27899 +                       err = au_opt_br(sb, opt, opts);
27900 +               if (!err)
27901 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27902 +               opt++;
27903 +       }
27904 +       if (err > 0)
27905 +               err = 0;
27906 +       AuTraceErr(err);
27907 +       /* go on even err */
27908 +
27909 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27910 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27911 +       if (unlikely(rerr && !err))
27912 +               err = rerr;
27913 +
27914 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27915 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27916 +
27917 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27918 +               rerr = au_xib_trunc(sb);
27919 +               if (unlikely(rerr && !err))
27920 +                       err = rerr;
27921 +       }
27922 +
27923 +       /* will be handled by the caller */
27924 +       if (!au_ftest_opts(opts->flags, REFRESH)
27925 +           && (opts->given_udba
27926 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27927 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27928 +                   ))
27929 +               au_fset_opts(opts->flags, REFRESH);
27930 +
27931 +       AuDbg("status 0x%x\n", opts->flags);
27932 +
27933 +out:
27934 +       return err;
27935 +}
27936 +
27937 +/* ---------------------------------------------------------------------- */
27938 +
27939 +unsigned int au_opt_udba(struct super_block *sb)
27940 +{
27941 +       return au_mntflags(sb) & AuOptMask_UDBA;
27942 +}
27943 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27944 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27945 +++ linux/fs/aufs/opts.h        2022-11-05 23:02:18.969222617 +0100
27946 @@ -0,0 +1,263 @@
27947 +/* SPDX-License-Identifier: GPL-2.0 */
27948 +/*
27949 + * Copyright (C) 2005-2022 Junjiro R. Okajima
27950 + *
27951 + * This program is free software; you can redistribute it and/or modify
27952 + * it under the terms of the GNU General Public License as published by
27953 + * the Free Software Foundation; either version 2 of the License, or
27954 + * (at your option) any later version.
27955 + *
27956 + * This program is distributed in the hope that it will be useful,
27957 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27958 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27959 + * GNU General Public License for more details.
27960 + *
27961 + * You should have received a copy of the GNU General Public License
27962 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27963 + */
27964 +
27965 +/*
27966 + * mount options/flags
27967 + */
27968 +
27969 +#ifndef __AUFS_OPTS_H__
27970 +#define __AUFS_OPTS_H__
27971 +
27972 +#ifdef __KERNEL__
27973 +
27974 +#include <linux/fs_parser.h>
27975 +#include <linux/namei.h>
27976 +#include <linux/path.h>
27977 +
27978 +enum {
27979 +       Opt_br,
27980 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27981 +       Opt_idel, Opt_imod,
27982 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27983 +       Opt_xino, Opt_noxino,
27984 +       Opt_trunc_xino, Opt_trunc_xino_v,
27985 +       Opt_trunc_xino_path, Opt_itrunc_xino,
27986 +       Opt_trunc_xib,
27987 +       Opt_shwh,
27988 +       Opt_plink, Opt_list_plink,
27989 +       Opt_udba,
27990 +       Opt_dio,
27991 +       Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27992 +       Opt_warn_perm,
27993 +       Opt_wbr_copyup, Opt_wbr_create,
27994 +       Opt_fhsm_sec,
27995 +       Opt_verbose, Opt_noverbose,
27996 +       Opt_sum, Opt_wsum,
27997 +       Opt_dirperm1,
27998 +       Opt_dirren,
27999 +       Opt_acl,
28000 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
28001 +};
28002 +
28003 +/* ---------------------------------------------------------------------- */
28004 +
28005 +/* mount flags */
28006 +#define AuOpt_XINO             1               /* external inode number bitmap
28007 +                                                  and translation table */
28008 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
28009 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
28010 +#define AuOpt_UDBA_REVAL       (1 << 3)
28011 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
28012 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
28013 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
28014 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
28015 +                                                  bits */
28016 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
28017 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
28018 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
28019 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
28020 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
28021 +#define AuOpt_DIO              (1 << 14)       /* direct io */
28022 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
28023 +
28024 +#ifndef CONFIG_AUFS_HNOTIFY
28025 +#undef AuOpt_UDBA_HNOTIFY
28026 +#define AuOpt_UDBA_HNOTIFY     0
28027 +#endif
28028 +#ifndef CONFIG_AUFS_DIRREN
28029 +#undef AuOpt_DIRREN
28030 +#define AuOpt_DIRREN           0
28031 +#endif
28032 +#ifndef CONFIG_AUFS_SHWH
28033 +#undef AuOpt_SHWH
28034 +#define AuOpt_SHWH             0
28035 +#endif
28036 +
28037 +#define AuOpt_Def      (AuOpt_XINO \
28038 +                        | AuOpt_UDBA_REVAL \
28039 +                        | AuOpt_PLINK \
28040 +                        /* | AuOpt_DIRPERM1 */ \
28041 +                        | AuOpt_WARN_PERM)
28042 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
28043 +                        | AuOpt_UDBA_REVAL \
28044 +                        | AuOpt_UDBA_HNOTIFY)
28045 +
28046 +#define AuOpt_LkupDirFlags     (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
28047 +
28048 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
28049 +#define au_opt_set(flags, name) do { \
28050 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
28051 +       ((flags) |= AuOpt_##name); \
28052 +} while (0)
28053 +#define au_opt_set_udba(flags, name) do { \
28054 +       (flags) &= ~AuOptMask_UDBA; \
28055 +       ((flags) |= AuOpt_##name); \
28056 +} while (0)
28057 +#define au_opt_clr(flags, name) do { \
28058 +       ((flags) &= ~AuOpt_##name); \
28059 +} while (0)
28060 +
28061 +static inline unsigned int au_opts_plink(unsigned int mntflags)
28062 +{
28063 +#ifdef CONFIG_PROC_FS
28064 +       return mntflags;
28065 +#else
28066 +       return mntflags & ~AuOpt_PLINK;
28067 +#endif
28068 +}
28069 +
28070 +/* ---------------------------------------------------------------------- */
28071 +
28072 +/* policies to select one among multiple writable branches */
28073 +enum {
28074 +       AuWbrCreate_TDP,        /* top down parent */
28075 +       AuWbrCreate_RR,         /* round robin */
28076 +       AuWbrCreate_MFS,        /* most free space */
28077 +       AuWbrCreate_MFSV,       /* mfs with seconds */
28078 +       AuWbrCreate_MFSRR,      /* mfs then rr */
28079 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
28080 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
28081 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
28082 +       AuWbrCreate_PMFS,       /* parent and mfs */
28083 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
28084 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
28085 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
28086 +
28087 +       AuWbrCreate_Def = AuWbrCreate_TDP
28088 +};
28089 +
28090 +enum {
28091 +       AuWbrCopyup_TDP,        /* top down parent */
28092 +       AuWbrCopyup_BUP,        /* bottom up parent */
28093 +       AuWbrCopyup_BU,         /* bottom up */
28094 +
28095 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
28096 +};
28097 +
28098 +/* ---------------------------------------------------------------------- */
28099 +
28100 +struct file;
28101 +
28102 +struct au_opt_add {
28103 +       aufs_bindex_t   bindex;
28104 +       char            *pathname;
28105 +       int             perm;
28106 +       struct path     path;
28107 +};
28108 +
28109 +struct au_opt_del {
28110 +       char            *pathname;
28111 +       struct path     h_path;
28112 +};
28113 +
28114 +struct au_opt_mod {
28115 +       char            *path;
28116 +       int             perm;
28117 +       struct dentry   *h_root;
28118 +};
28119 +
28120 +struct au_opt_xino {
28121 +       char            *path;
28122 +       struct file     *file;
28123 +};
28124 +
28125 +struct au_opt_xino_itrunc {
28126 +       aufs_bindex_t   bindex;
28127 +};
28128 +
28129 +struct au_opt_wbr_create {
28130 +       int                     wbr_create;
28131 +       int                     mfs_second;
28132 +       unsigned long long      mfsrr_watermark;
28133 +};
28134 +
28135 +struct au_opt {
28136 +       int type;
28137 +       union {
28138 +               struct au_opt_xino      xino;
28139 +               struct au_opt_xino_itrunc xino_itrunc;
28140 +               struct au_opt_add       add;
28141 +               struct au_opt_del       del;
28142 +               struct au_opt_mod       mod;
28143 +               int                     dirwh;
28144 +               int                     rdcache;
28145 +               unsigned int            rdblk;
28146 +               unsigned int            rdhash;
28147 +               int                     udba;
28148 +               struct au_opt_wbr_create wbr_create;
28149 +               int                     wbr_copyup;
28150 +               unsigned int            fhsm_second;
28151 +               bool                    tf; /* generic flag, true or false */
28152 +       };
28153 +};
28154 +
28155 +/* opts flags */
28156 +#define AuOpts_REMOUNT         1
28157 +#define AuOpts_REFRESH         (1 << 1)
28158 +#define AuOpts_TRUNC_XIB       (1 << 2)
28159 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
28160 +#define AuOpts_REFRESH_IDOP    (1 << 4)
28161 +#define AuOpts_DR_FLUSHED      (1 << 5)
28162 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
28163 +#define au_fset_opts(flags, name) \
28164 +       do { (flags) |= AuOpts_##name; } while (0)
28165 +#define au_fclr_opts(flags, name) \
28166 +       do { (flags) &= ~AuOpts_##name; } while (0)
28167 +
28168 +#ifndef CONFIG_AUFS_DIRREN
28169 +#undef AuOpts_DR_FLUSHED
28170 +#define AuOpts_DR_FLUSHED      0
28171 +#endif
28172 +
28173 +struct au_opts {
28174 +       struct au_opt   *opt;
28175 +       int             max_opt;
28176 +
28177 +       unsigned int    given_udba;
28178 +       unsigned int    flags;
28179 +       unsigned long   sb_flags;
28180 +};
28181 +
28182 +/* ---------------------------------------------------------------------- */
28183 +
28184 +/* opts.c */
28185 +int au_br_perm_val(char *perm);
28186 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
28187 +int au_udba_val(char *str);
28188 +const char *au_optstr_udba(int udba);
28189 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
28190 +const char *au_optstr_wbr_create(int wbr_create);
28191 +int au_wbr_copyup_val(char *str);
28192 +const char *au_optstr_wbr_copyup(int wbr_copyup);
28193 +
28194 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28195 +              aufs_bindex_t bindex);
28196 +struct super_block;
28197 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28198 +                  unsigned int pending);
28199 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28200 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28201 +
28202 +unsigned int au_opt_udba(struct super_block *sb);
28203 +
28204 +/* fsctx.c */
28205 +int aufs_fsctx_init(struct fs_context *fc);
28206 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28207 +
28208 +#endif /* __KERNEL__ */
28209 +#endif /* __AUFS_OPTS_H__ */
28210 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
28211 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
28212 +++ linux/fs/aufs/plink.c       2022-11-05 23:02:18.969222617 +0100
28213 @@ -0,0 +1,516 @@
28214 +// SPDX-License-Identifier: GPL-2.0
28215 +/*
28216 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28217 + *
28218 + * This program is free software; you can redistribute it and/or modify
28219 + * it under the terms of the GNU General Public License as published by
28220 + * the Free Software Foundation; either version 2 of the License, or
28221 + * (at your option) any later version.
28222 + *
28223 + * This program is distributed in the hope that it will be useful,
28224 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28225 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28226 + * GNU General Public License for more details.
28227 + *
28228 + * You should have received a copy of the GNU General Public License
28229 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28230 + */
28231 +
28232 +/*
28233 + * pseudo-link
28234 + */
28235 +
28236 +#include "aufs.h"
28237 +
28238 +/*
28239 + * the pseudo-link maintenance mode.
28240 + * during a user process maintains the pseudo-links,
28241 + * prohibit adding a new plink and branch manipulation.
28242 + *
28243 + * Flags
28244 + * NOPLM:
28245 + *     For entry functions which will handle plink, and i_mutex is already held
28246 + *     in VFS.
28247 + *     They cannot wait and should return an error at once.
28248 + *     Callers has to check the error.
28249 + * NOPLMW:
28250 + *     For entry functions which will handle plink, but i_mutex is not held
28251 + *     in VFS.
28252 + *     They can wait the plink maintenance mode to finish.
28253 + *
28254 + * They behave like F_SETLK and F_SETLKW.
28255 + * If the caller never handle plink, then both flags are unnecessary.
28256 + */
28257 +
28258 +int au_plink_maint(struct super_block *sb, int flags)
28259 +{
28260 +       int err;
28261 +       pid_t pid, ppid;
28262 +       struct task_struct *parent, *prev;
28263 +       struct au_sbinfo *sbi;
28264 +
28265 +       SiMustAnyLock(sb);
28266 +
28267 +       err = 0;
28268 +       if (!au_opt_test(au_mntflags(sb), PLINK))
28269 +               goto out;
28270 +
28271 +       sbi = au_sbi(sb);
28272 +       pid = sbi->si_plink_maint_pid;
28273 +       if (!pid || pid == current->pid)
28274 +               goto out;
28275 +
28276 +       /* todo: it highly depends upon /sbin/mount.aufs */
28277 +       prev = NULL;
28278 +       parent = current;
28279 +       ppid = 0;
28280 +       rcu_read_lock();
28281 +       while (1) {
28282 +               parent = rcu_dereference(parent->real_parent);
28283 +               if (parent == prev)
28284 +                       break;
28285 +               ppid = task_pid_vnr(parent);
28286 +               if (pid == ppid) {
28287 +                       rcu_read_unlock();
28288 +                       goto out;
28289 +               }
28290 +               prev = parent;
28291 +       }
28292 +       rcu_read_unlock();
28293 +
28294 +       if (au_ftest_lock(flags, NOPLMW)) {
28295 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
28296 +               /* AuDebugOn(!lockdep_depth(current)); */
28297 +               while (sbi->si_plink_maint_pid) {
28298 +                       si_read_unlock(sb);
28299 +                       /* gave up wake_up_bit() */
28300 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28301 +
28302 +                       if (au_ftest_lock(flags, FLUSH))
28303 +                               au_nwt_flush(&sbi->si_nowait);
28304 +                       si_noflush_read_lock(sb);
28305 +               }
28306 +       } else if (au_ftest_lock(flags, NOPLM)) {
28307 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
28308 +               err = -EAGAIN;
28309 +       }
28310 +
28311 +out:
28312 +       return err;
28313 +}
28314 +
28315 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28316 +{
28317 +       spin_lock(&sbinfo->si_plink_maint_lock);
28318 +       sbinfo->si_plink_maint_pid = 0;
28319 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28320 +       wake_up_all(&sbinfo->si_plink_wq);
28321 +}
28322 +
28323 +int au_plink_maint_enter(struct super_block *sb)
28324 +{
28325 +       int err;
28326 +       struct au_sbinfo *sbinfo;
28327 +
28328 +       err = 0;
28329 +       sbinfo = au_sbi(sb);
28330 +       /* make sure i am the only one in this fs */
28331 +       si_write_lock(sb, AuLock_FLUSH);
28332 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28333 +               spin_lock(&sbinfo->si_plink_maint_lock);
28334 +               if (!sbinfo->si_plink_maint_pid)
28335 +                       sbinfo->si_plink_maint_pid = current->pid;
28336 +               else
28337 +                       err = -EBUSY;
28338 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28339 +       }
28340 +       si_write_unlock(sb);
28341 +
28342 +       return err;
28343 +}
28344 +
28345 +/* ---------------------------------------------------------------------- */
28346 +
28347 +#ifdef CONFIG_AUFS_DEBUG
28348 +void au_plink_list(struct super_block *sb)
28349 +{
28350 +       int i;
28351 +       struct au_sbinfo *sbinfo;
28352 +       struct hlist_bl_head *hbl;
28353 +       struct hlist_bl_node *pos;
28354 +       struct au_icntnr *icntnr;
28355 +
28356 +       SiMustAnyLock(sb);
28357 +
28358 +       sbinfo = au_sbi(sb);
28359 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28360 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28361 +
28362 +       for (i = 0; i < AuPlink_NHASH; i++) {
28363 +               hbl = sbinfo->si_plink + i;
28364 +               hlist_bl_lock(hbl);
28365 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28366 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28367 +               hlist_bl_unlock(hbl);
28368 +       }
28369 +}
28370 +#endif
28371 +
28372 +/* is the inode pseudo-linked? */
28373 +int au_plink_test(struct inode *inode)
28374 +{
28375 +       int found, i;
28376 +       struct au_sbinfo *sbinfo;
28377 +       struct hlist_bl_head *hbl;
28378 +       struct hlist_bl_node *pos;
28379 +       struct au_icntnr *icntnr;
28380 +
28381 +       sbinfo = au_sbi(inode->i_sb);
28382 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28383 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28384 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28385 +
28386 +       found = 0;
28387 +       i = au_plink_hash(inode->i_ino);
28388 +       hbl =  sbinfo->si_plink + i;
28389 +       hlist_bl_lock(hbl);
28390 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28391 +               if (&icntnr->vfs_inode == inode) {
28392 +                       found = 1;
28393 +                       break;
28394 +               }
28395 +       hlist_bl_unlock(hbl);
28396 +       return found;
28397 +}
28398 +
28399 +/* ---------------------------------------------------------------------- */
28400 +
28401 +/*
28402 + * generate a name for plink.
28403 + * the file will be stored under AUFS_WH_PLINKDIR.
28404 + */
28405 +/* 20 is max digits length of ulong 64 */
28406 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28407 +
28408 +static int plink_name(char *name, int len, struct inode *inode,
28409 +                     aufs_bindex_t bindex)
28410 +{
28411 +       int rlen;
28412 +       struct inode *h_inode;
28413 +
28414 +       h_inode = au_h_iptr(inode, bindex);
28415 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28416 +       return rlen;
28417 +}
28418 +
28419 +struct au_do_plink_lkup_args {
28420 +       struct dentry **errp;
28421 +       struct qstr *tgtname;
28422 +       struct path *h_ppath;
28423 +};
28424 +
28425 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28426 +                                      struct path *h_ppath)
28427 +{
28428 +       struct dentry *h_dentry;
28429 +       struct inode *h_inode;
28430 +
28431 +       h_inode = d_inode(h_ppath->dentry);
28432 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28433 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
28434 +       inode_unlock_shared(h_inode);
28435 +
28436 +       return h_dentry;
28437 +}
28438 +
28439 +static void au_call_do_plink_lkup(void *args)
28440 +{
28441 +       struct au_do_plink_lkup_args *a = args;
28442 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
28443 +}
28444 +
28445 +/* lookup the plink-ed @inode under the branch at @bindex */
28446 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28447 +{
28448 +       struct dentry *h_dentry;
28449 +       struct au_branch *br;
28450 +       struct path h_ppath;
28451 +       int wkq_err;
28452 +       char a[PLINK_NAME_LEN];
28453 +       struct qstr tgtname = QSTR_INIT(a, 0);
28454 +
28455 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28456 +
28457 +       br = au_sbr(inode->i_sb, bindex);
28458 +       h_ppath.dentry = br->br_wbr->wbr_plink;
28459 +       h_ppath.mnt = au_br_mnt(br);
28460 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28461 +
28462 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28463 +               struct au_do_plink_lkup_args args = {
28464 +                       .errp           = &h_dentry,
28465 +                       .tgtname        = &tgtname,
28466 +                       .h_ppath        = &h_ppath
28467 +               };
28468 +
28469 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28470 +               if (unlikely(wkq_err))
28471 +                       h_dentry = ERR_PTR(wkq_err);
28472 +       } else
28473 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28474 +
28475 +       return h_dentry;
28476 +}
28477 +
28478 +/* create a pseudo-link */
28479 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28480 +                     struct dentry *h_dentry)
28481 +{
28482 +       int err;
28483 +       struct path h_path;
28484 +       struct inode *h_dir, *delegated;
28485 +
28486 +       h_dir = d_inode(h_ppath->dentry);
28487 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28488 +       h_path.mnt = h_ppath->mnt;
28489 +again:
28490 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28491 +       err = PTR_ERR(h_path.dentry);
28492 +       if (IS_ERR(h_path.dentry))
28493 +               goto out;
28494 +
28495 +       err = 0;
28496 +       /* wh.plink dir is not monitored */
28497 +       /* todo: is it really safe? */
28498 +       if (d_is_positive(h_path.dentry)
28499 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28500 +               delegated = NULL;
28501 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28502 +               if (unlikely(err == -EWOULDBLOCK)) {
28503 +                       pr_warn("cannot retry for NFSv4 delegation"
28504 +                               " for an internal unlink\n");
28505 +                       iput(delegated);
28506 +               }
28507 +               dput(h_path.dentry);
28508 +               h_path.dentry = NULL;
28509 +               if (!err)
28510 +                       goto again;
28511 +       }
28512 +       if (!err && d_is_negative(h_path.dentry)) {
28513 +               delegated = NULL;
28514 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28515 +               if (unlikely(err == -EWOULDBLOCK)) {
28516 +                       pr_warn("cannot retry for NFSv4 delegation"
28517 +                               " for an internal link\n");
28518 +                       iput(delegated);
28519 +               }
28520 +       }
28521 +       dput(h_path.dentry);
28522 +
28523 +out:
28524 +       inode_unlock(h_dir);
28525 +       return err;
28526 +}
28527 +
28528 +struct do_whplink_args {
28529 +       int *errp;
28530 +       struct qstr *tgt;
28531 +       struct path *h_ppath;
28532 +       struct dentry *h_dentry;
28533 +};
28534 +
28535 +static void call_do_whplink(void *args)
28536 +{
28537 +       struct do_whplink_args *a = args;
28538 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28539 +}
28540 +
28541 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28542 +                  aufs_bindex_t bindex)
28543 +{
28544 +       int err, wkq_err;
28545 +       struct au_branch *br;
28546 +       struct au_wbr *wbr;
28547 +       struct path h_ppath;
28548 +       char a[PLINK_NAME_LEN];
28549 +       struct qstr tgtname = QSTR_INIT(a, 0);
28550 +
28551 +       br = au_sbr(inode->i_sb, bindex);
28552 +       wbr = br->br_wbr;
28553 +       h_ppath.dentry = wbr->wbr_plink;
28554 +       h_ppath.mnt = au_br_mnt(br);
28555 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28556 +
28557 +       /* always superio. */
28558 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28559 +               struct do_whplink_args args = {
28560 +                       .errp           = &err,
28561 +                       .tgt            = &tgtname,
28562 +                       .h_ppath        = &h_ppath,
28563 +                       .h_dentry       = h_dentry
28564 +               };
28565 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28566 +               if (unlikely(wkq_err))
28567 +                       err = wkq_err;
28568 +       } else
28569 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28570 +
28571 +       return err;
28572 +}
28573 +
28574 +/*
28575 + * create a new pseudo-link for @h_dentry on @bindex.
28576 + * the linked inode is held in aufs @inode.
28577 + */
28578 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28579 +                    struct dentry *h_dentry)
28580 +{
28581 +       struct super_block *sb;
28582 +       struct au_sbinfo *sbinfo;
28583 +       struct hlist_bl_head *hbl;
28584 +       struct hlist_bl_node *pos;
28585 +       struct au_icntnr *icntnr;
28586 +       int found, err, cnt, i;
28587 +
28588 +       sb = inode->i_sb;
28589 +       sbinfo = au_sbi(sb);
28590 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28591 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28592 +
28593 +       found = au_plink_test(inode);
28594 +       if (found)
28595 +               return;
28596 +
28597 +       i = au_plink_hash(inode->i_ino);
28598 +       hbl = sbinfo->si_plink + i;
28599 +       au_igrab(inode);
28600 +
28601 +       hlist_bl_lock(hbl);
28602 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28603 +               if (&icntnr->vfs_inode == inode) {
28604 +                       found = 1;
28605 +                       break;
28606 +               }
28607 +       }
28608 +       if (!found) {
28609 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28610 +               hlist_bl_add_head(&icntnr->plink, hbl);
28611 +       }
28612 +       hlist_bl_unlock(hbl);
28613 +       if (!found) {
28614 +               cnt = au_hbl_count(hbl);
28615 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28616 +               if (cnt > AUFS_PLINK_WARN)
28617 +                       AuWarn1(msg ", %d\n", cnt);
28618 +#undef msg
28619 +               err = whplink(h_dentry, inode, bindex);
28620 +               if (unlikely(err)) {
28621 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28622 +                       au_hbl_del(&icntnr->plink, hbl);
28623 +                       iput(&icntnr->vfs_inode);
28624 +               }
28625 +       } else
28626 +               iput(&icntnr->vfs_inode);
28627 +}
28628 +
28629 +/* free all plinks */
28630 +void au_plink_put(struct super_block *sb, int verbose)
28631 +{
28632 +       int i, warned;
28633 +       struct au_sbinfo *sbinfo;
28634 +       struct hlist_bl_head *hbl;
28635 +       struct hlist_bl_node *pos, *tmp;
28636 +       struct au_icntnr *icntnr;
28637 +
28638 +       SiMustWriteLock(sb);
28639 +
28640 +       sbinfo = au_sbi(sb);
28641 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28642 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28643 +
28644 +       /* no spin_lock since sbinfo is write-locked */
28645 +       warned = 0;
28646 +       for (i = 0; i < AuPlink_NHASH; i++) {
28647 +               hbl = sbinfo->si_plink + i;
28648 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28649 +                       pr_warn("pseudo-link is not flushed");
28650 +                       warned = 1;
28651 +               }
28652 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28653 +                       iput(&icntnr->vfs_inode);
28654 +               INIT_HLIST_BL_HEAD(hbl);
28655 +       }
28656 +}
28657 +
28658 +void au_plink_clean(struct super_block *sb, int verbose)
28659 +{
28660 +       struct dentry *root;
28661 +
28662 +       root = sb->s_root;
28663 +       aufs_write_lock(root);
28664 +       if (au_opt_test(au_mntflags(sb), PLINK))
28665 +               au_plink_put(sb, verbose);
28666 +       aufs_write_unlock(root);
28667 +}
28668 +
28669 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28670 +{
28671 +       int do_put;
28672 +       aufs_bindex_t btop, bbot, bindex;
28673 +
28674 +       do_put = 0;
28675 +       btop = au_ibtop(inode);
28676 +       bbot = au_ibbot(inode);
28677 +       if (btop >= 0) {
28678 +               for (bindex = btop; bindex <= bbot; bindex++) {
28679 +                       if (!au_h_iptr(inode, bindex)
28680 +                           || au_ii_br_id(inode, bindex) != br_id)
28681 +                               continue;
28682 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28683 +                       do_put = 1;
28684 +                       break;
28685 +               }
28686 +               if (do_put)
28687 +                       for (bindex = btop; bindex <= bbot; bindex++)
28688 +                               if (au_h_iptr(inode, bindex)) {
28689 +                                       do_put = 0;
28690 +                                       break;
28691 +                               }
28692 +       } else
28693 +               do_put = 1;
28694 +
28695 +       return do_put;
28696 +}
28697 +
28698 +/* free the plinks on a branch specified by @br_id */
28699 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28700 +{
28701 +       struct au_sbinfo *sbinfo;
28702 +       struct hlist_bl_head *hbl;
28703 +       struct hlist_bl_node *pos, *tmp;
28704 +       struct au_icntnr *icntnr;
28705 +       struct inode *inode;
28706 +       int i, do_put;
28707 +
28708 +       SiMustWriteLock(sb);
28709 +
28710 +       sbinfo = au_sbi(sb);
28711 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28712 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28713 +
28714 +       /* no bit_lock since sbinfo is write-locked */
28715 +       for (i = 0; i < AuPlink_NHASH; i++) {
28716 +               hbl = sbinfo->si_plink + i;
28717 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28718 +                       inode = au_igrab(&icntnr->vfs_inode);
28719 +                       ii_write_lock_child(inode);
28720 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28721 +                       if (do_put) {
28722 +                               hlist_bl_del(&icntnr->plink);
28723 +                               iput(inode);
28724 +                       }
28725 +                       ii_write_unlock(inode);
28726 +                       iput(inode);
28727 +               }
28728 +       }
28729 +}
28730 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28731 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28732 +++ linux/fs/aufs/poll.c        2022-11-05 23:02:18.969222617 +0100
28733 @@ -0,0 +1,51 @@
28734 +// SPDX-License-Identifier: GPL-2.0
28735 +/*
28736 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28737 + *
28738 + * This program is free software; you can redistribute it and/or modify
28739 + * it under the terms of the GNU General Public License as published by
28740 + * the Free Software Foundation; either version 2 of the License, or
28741 + * (at your option) any later version.
28742 + *
28743 + * This program is distributed in the hope that it will be useful,
28744 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28745 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28746 + * GNU General Public License for more details.
28747 + *
28748 + * You should have received a copy of the GNU General Public License
28749 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28750 + */
28751 +
28752 +/*
28753 + * poll operation
28754 + * There is only one filesystem which implements ->poll operation, currently.
28755 + */
28756 +
28757 +#include "aufs.h"
28758 +
28759 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28760 +{
28761 +       __poll_t mask;
28762 +       struct file *h_file;
28763 +       struct super_block *sb;
28764 +
28765 +       /* We should pretend an error happened. */
28766 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28767 +       sb = file->f_path.dentry->d_sb;
28768 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28769 +
28770 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28771 +       if (IS_ERR(h_file)) {
28772 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28773 +               goto out;
28774 +       }
28775 +
28776 +       mask = vfs_poll(h_file, pt);
28777 +       fput(h_file); /* instead of au_read_post() */
28778 +
28779 +out:
28780 +       si_read_unlock(sb);
28781 +       if (mask & EPOLLERR)
28782 +               AuDbg("mask 0x%x\n", mask);
28783 +       return mask;
28784 +}
28785 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28786 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28787 +++ linux/fs/aufs/posix_acl.c   2023-08-28 12:34:39.959969465 +0200
28788 @@ -0,0 +1,108 @@
28789 +// SPDX-License-Identifier: GPL-2.0
28790 +/*
28791 + * Copyright (C) 2014-2022 Junjiro R. Okajima
28792 + *
28793 + * This program is free software; you can redistribute it and/or modify
28794 + * it under the terms of the GNU General Public License as published by
28795 + * the Free Software Foundation; either version 2 of the License, or
28796 + * (at your option) any later version.
28797 + *
28798 + * This program is distributed in the hope that it will be useful,
28799 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28800 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28801 + * GNU General Public License for more details.
28802 + *
28803 + * You should have received a copy of the GNU General Public License
28804 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28805 + */
28806 +
28807 +/*
28808 + * posix acl operations
28809 + */
28810 +
28811 +#include <linux/fs.h>
28812 +#include "aufs.h"
28813 +
28814 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu)
28815 +{
28816 +       struct posix_acl *acl;
28817 +       int err;
28818 +       aufs_bindex_t bindex;
28819 +       struct inode *h_inode;
28820 +       struct super_block *sb;
28821 +
28822 +       acl = ERR_PTR(-ECHILD);
28823 +       if (rcu)
28824 +               goto out;
28825 +
28826 +       acl = NULL;
28827 +       sb = inode->i_sb;
28828 +       si_read_lock(sb, AuLock_FLUSH);
28829 +       ii_read_lock_child(inode);
28830 +       if (!(sb->s_flags & SB_POSIXACL))
28831 +               goto unlock;
28832 +
28833 +       bindex = au_ibtop(inode);
28834 +       h_inode = au_h_iptr(inode, bindex);
28835 +       if (unlikely(!h_inode
28836 +                    || ((h_inode->i_mode & S_IFMT)
28837 +                        != (inode->i_mode & S_IFMT)))) {
28838 +               err = au_busy_or_stale();
28839 +               acl = ERR_PTR(err);
28840 +               goto unlock;
28841 +       }
28842 +
28843 +       /* always topmost only */
28844 +       acl = get_inode_acl(h_inode, type);
28845 +       if (IS_ERR(acl))
28846 +               forget_cached_acl(inode, type);
28847 +       else
28848 +               set_cached_acl(inode, type, acl);
28849 +
28850 +unlock:
28851 +       ii_read_unlock(inode);
28852 +       si_read_unlock(sb);
28853 +
28854 +out:
28855 +       AuTraceErrPtr(acl);
28856 +       return acl;
28857 +}
28858 +
28859 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
28860 +                              struct dentry *dentry, int type)
28861 +{
28862 +       struct posix_acl *acl;
28863 +       struct inode *inode;
28864 +
28865 +       inode = d_inode(dentry);
28866 +       acl = aufs_get_inode_acl(inode, type, /*rcu*/false);
28867 +
28868 +       return acl;
28869 +}
28870 +
28871 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
28872 +                struct posix_acl *acl, int type)
28873 +{
28874 +       int err;
28875 +       ssize_t ssz;
28876 +       struct inode *inode;
28877 +       struct au_sxattr arg = {
28878 +               .type = AU_ACL_SET,
28879 +               .u.acl_set = {
28880 +                       .acl    = acl,
28881 +                       .type   = type
28882 +               },
28883 +       };
28884 +
28885 +       inode = d_inode(dentry);
28886 +       IMustLock(inode);
28887 +
28888 +       ssz = au_sxattr(dentry, inode, &arg);
28889 +       /* forget even it if succeeds since the branch might set differently */
28890 +       forget_cached_acl(inode, type);
28891 +       err = ssz;
28892 +       if (ssz >= 0)
28893 +               err = 0;
28894 +
28895 +       return err;
28896 +}
28897 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28898 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28899 +++ linux/fs/aufs/procfs.c      2022-11-05 23:02:18.969222617 +0100
28900 @@ -0,0 +1,170 @@
28901 +// SPDX-License-Identifier: GPL-2.0
28902 +/*
28903 + * Copyright (C) 2010-2022 Junjiro R. Okajima
28904 + *
28905 + * This program is free software; you can redistribute it and/or modify
28906 + * it under the terms of the GNU General Public License as published by
28907 + * the Free Software Foundation; either version 2 of the License, or
28908 + * (at your option) any later version.
28909 + *
28910 + * This program is distributed in the hope that it will be useful,
28911 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28912 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28913 + * GNU General Public License for more details.
28914 + *
28915 + * You should have received a copy of the GNU General Public License
28916 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28917 + */
28918 +
28919 +/*
28920 + * procfs interfaces
28921 + */
28922 +
28923 +#include <linux/proc_fs.h>
28924 +#include "aufs.h"
28925 +
28926 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28927 +{
28928 +       struct au_sbinfo *sbinfo;
28929 +
28930 +       sbinfo = file->private_data;
28931 +       if (sbinfo) {
28932 +               au_plink_maint_leave(sbinfo);
28933 +               kobject_put(&sbinfo->si_kobj);
28934 +       }
28935 +
28936 +       return 0;
28937 +}
28938 +
28939 +static void au_procfs_plm_write_clean(struct file *file)
28940 +{
28941 +       struct au_sbinfo *sbinfo;
28942 +
28943 +       sbinfo = file->private_data;
28944 +       if (sbinfo)
28945 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28946 +}
28947 +
28948 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28949 +{
28950 +       int err;
28951 +       struct super_block *sb;
28952 +       struct au_sbinfo *sbinfo;
28953 +       struct hlist_bl_node *pos;
28954 +
28955 +       err = -EBUSY;
28956 +       if (unlikely(file->private_data))
28957 +               goto out;
28958 +
28959 +       sb = NULL;
28960 +       /* don't use au_sbilist_lock() here */
28961 +       hlist_bl_lock(&au_sbilist);
28962 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28963 +               if (id == sysaufs_si_id(sbinfo)) {
28964 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28965 +                               sb = sbinfo->si_sb;
28966 +                       break;
28967 +               }
28968 +       hlist_bl_unlock(&au_sbilist);
28969 +
28970 +       err = -EINVAL;
28971 +       if (unlikely(!sb))
28972 +               goto out;
28973 +
28974 +       err = au_plink_maint_enter(sb);
28975 +       if (!err)
28976 +               /* keep kobject_get() */
28977 +               file->private_data = sbinfo;
28978 +       else
28979 +               kobject_put(&sbinfo->si_kobj);
28980 +out:
28981 +       return err;
28982 +}
28983 +
28984 +/*
28985 + * Accept a valid "si=xxxx" only.
28986 + * Once it is accepted successfully, accept "clean" too.
28987 + */
28988 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28989 +                                  size_t count, loff_t *ppos)
28990 +{
28991 +       ssize_t err;
28992 +       unsigned long id;
28993 +       /* last newline is allowed */
28994 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28995 +
28996 +       err = -EACCES;
28997 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28998 +               goto out;
28999 +
29000 +       err = -EINVAL;
29001 +       if (unlikely(count > sizeof(buf)))
29002 +               goto out;
29003 +
29004 +       err = copy_from_user(buf, ubuf, count);
29005 +       if (unlikely(err)) {
29006 +               err = -EFAULT;
29007 +               goto out;
29008 +       }
29009 +       buf[count] = 0;
29010 +
29011 +       err = -EINVAL;
29012 +       if (!strcmp("clean", buf)) {
29013 +               au_procfs_plm_write_clean(file);
29014 +               goto out_success;
29015 +       } else if (unlikely(strncmp("si=", buf, 3)))
29016 +               goto out;
29017 +
29018 +       err = kstrtoul(buf + 3, 16, &id);
29019 +       if (unlikely(err))
29020 +               goto out;
29021 +
29022 +       err = au_procfs_plm_write_si(file, id);
29023 +       if (unlikely(err))
29024 +               goto out;
29025 +
29026 +out_success:
29027 +       err = count; /* success */
29028 +out:
29029 +       return err;
29030 +}
29031 +
29032 +static const struct proc_ops au_procfs_plm_op = {
29033 +       .proc_write     = au_procfs_plm_write,
29034 +       .proc_release   = au_procfs_plm_release
29035 +};
29036 +
29037 +/* ---------------------------------------------------------------------- */
29038 +
29039 +static struct proc_dir_entry *au_procfs_dir;
29040 +
29041 +void au_procfs_fin(void)
29042 +{
29043 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
29044 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29045 +}
29046 +
29047 +int __init au_procfs_init(void)
29048 +{
29049 +       int err;
29050 +       struct proc_dir_entry *entry;
29051 +
29052 +       err = -ENOMEM;
29053 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
29054 +       if (unlikely(!au_procfs_dir))
29055 +               goto out;
29056 +
29057 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
29058 +                           au_procfs_dir, &au_procfs_plm_op);
29059 +       if (unlikely(!entry))
29060 +               goto out_dir;
29061 +
29062 +       err = 0;
29063 +       goto out; /* success */
29064 +
29065 +
29066 +out_dir:
29067 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29068 +out:
29069 +       return err;
29070 +}
29071 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
29072 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
29073 +++ linux/fs/aufs/rdu.c 2022-12-17 09:21:34.799855195 +0100
29074 @@ -0,0 +1,384 @@
29075 +// SPDX-License-Identifier: GPL-2.0
29076 +/*
29077 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29078 + *
29079 + * This program is free software; you can redistribute it and/or modify
29080 + * it under the terms of the GNU General Public License as published by
29081 + * the Free Software Foundation; either version 2 of the License, or
29082 + * (at your option) any later version.
29083 + *
29084 + * This program is distributed in the hope that it will be useful,
29085 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29086 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29087 + * GNU General Public License for more details.
29088 + *
29089 + * You should have received a copy of the GNU General Public License
29090 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29091 + */
29092 +
29093 +/*
29094 + * readdir in userspace.
29095 + */
29096 +
29097 +#include <linux/compat.h>
29098 +#include <linux/fs_stack.h>
29099 +#include <linux/security.h>
29100 +#include "aufs.h"
29101 +
29102 +/* bits for struct aufs_rdu.flags */
29103 +#define        AuRdu_CALLED    1
29104 +#define        AuRdu_CONT      (1 << 1)
29105 +#define        AuRdu_FULL      (1 << 2)
29106 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
29107 +#define au_fset_rdu(flags, name) \
29108 +       do { (flags) |= AuRdu_##name; } while (0)
29109 +#define au_fclr_rdu(flags, name) \
29110 +       do { (flags) &= ~AuRdu_##name; } while (0)
29111 +
29112 +struct au_rdu_arg {
29113 +       struct dir_context              ctx;
29114 +       struct aufs_rdu                 *rdu;
29115 +       union au_rdu_ent_ul             ent;
29116 +       unsigned long                   end;
29117 +
29118 +       struct super_block              *sb;
29119 +       int                             err;
29120 +};
29121 +
29122 +static bool au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29123 +                       loff_t offset, u64 h_ino, unsigned int d_type)
29124 +{
29125 +       int err, len;
29126 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
29127 +       struct aufs_rdu *rdu = arg->rdu;
29128 +       struct au_rdu_ent ent;
29129 +
29130 +       err = 0;
29131 +       arg->err = 0;
29132 +       au_fset_rdu(rdu->cookie.flags, CALLED);
29133 +       len = au_rdu_len(nlen);
29134 +       if (arg->ent.ul + len  < arg->end) {
29135 +               ent.ino = h_ino;
29136 +               ent.bindex = rdu->cookie.bindex;
29137 +               ent.type = d_type;
29138 +               ent.nlen = nlen;
29139 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
29140 +                       ent.type = DT_UNKNOWN;
29141 +
29142 +               /* unnecessary to support mmap_sem since this is a dir */
29143 +               err = -EFAULT;
29144 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29145 +                       goto out;
29146 +               if (copy_to_user(arg->ent.e->name, name, nlen))
29147 +                       goto out;
29148 +               /* the terminating NULL */
29149 +               if (__put_user(0, arg->ent.e->name + nlen))
29150 +                       goto out;
29151 +               err = 0;
29152 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29153 +               arg->ent.ul += len;
29154 +               rdu->rent++;
29155 +       } else {
29156 +               err = -EFAULT;
29157 +               au_fset_rdu(rdu->cookie.flags, FULL);
29158 +               rdu->full = 1;
29159 +               rdu->tail = arg->ent;
29160 +       }
29161 +
29162 +out:
29163 +       /* AuTraceErr(err); */
29164 +       return !err;
29165 +}
29166 +
29167 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29168 +{
29169 +       int err;
29170 +       loff_t offset;
29171 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29172 +
29173 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
29174 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29175 +       err = offset;
29176 +       if (unlikely(offset != cookie->h_pos))
29177 +               goto out;
29178 +
29179 +       err = 0;
29180 +       do {
29181 +               arg->err = 0;
29182 +               au_fclr_rdu(cookie->flags, CALLED);
29183 +               /* smp_mb(); */
29184 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
29185 +               if (err >= 0)
29186 +                       err = arg->err;
29187 +       } while (!err
29188 +                && au_ftest_rdu(cookie->flags, CALLED)
29189 +                && !au_ftest_rdu(cookie->flags, FULL));
29190 +       cookie->h_pos = h_file->f_pos;
29191 +
29192 +out:
29193 +       AuTraceErr(err);
29194 +       return err;
29195 +}
29196 +
29197 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29198 +{
29199 +       int err;
29200 +       aufs_bindex_t bbot;
29201 +       struct au_rdu_arg arg = {
29202 +               .ctx = {
29203 +                       .actor = au_rdu_fill
29204 +               }
29205 +       };
29206 +       struct dentry *dentry;
29207 +       struct inode *inode;
29208 +       struct file *h_file;
29209 +       struct au_rdu_cookie *cookie = &rdu->cookie;
29210 +
29211 +       /* VERIFY_WRITE */
29212 +       err = !access_ok(rdu->ent.e, rdu->sz);
29213 +       if (unlikely(err)) {
29214 +               err = -EFAULT;
29215 +               AuTraceErr(err);
29216 +               goto out;
29217 +       }
29218 +       rdu->rent = 0;
29219 +       rdu->tail = rdu->ent;
29220 +       rdu->full = 0;
29221 +       arg.rdu = rdu;
29222 +       arg.ent = rdu->ent;
29223 +       arg.end = arg.ent.ul;
29224 +       arg.end += rdu->sz;
29225 +
29226 +       err = -ENOTDIR;
29227 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
29228 +               goto out;
29229 +
29230 +       err = security_file_permission(file, MAY_READ);
29231 +       AuTraceErr(err);
29232 +       if (unlikely(err))
29233 +               goto out;
29234 +
29235 +       dentry = file->f_path.dentry;
29236 +       inode = d_inode(dentry);
29237 +       inode_lock_shared(inode);
29238 +
29239 +       arg.sb = inode->i_sb;
29240 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29241 +       if (unlikely(err))
29242 +               goto out_mtx;
29243 +       err = au_alive_dir(dentry);
29244 +       if (unlikely(err))
29245 +               goto out_si;
29246 +       /* todo: reval? */
29247 +       fi_read_lock(file);
29248 +
29249 +       err = -EAGAIN;
29250 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29251 +                    && cookie->generation != au_figen(file)))
29252 +               goto out_unlock;
29253 +
29254 +       err = 0;
29255 +       if (!rdu->blk) {
29256 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
29257 +               if (!rdu->blk)
29258 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
29259 +       }
29260 +       bbot = au_fbtop(file);
29261 +       if (cookie->bindex < bbot)
29262 +               cookie->bindex = bbot;
29263 +       bbot = au_fbbot_dir(file);
29264 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29265 +       for (; !err && cookie->bindex <= bbot;
29266 +            cookie->bindex++, cookie->h_pos = 0) {
29267 +               h_file = au_hf_dir(file, cookie->bindex);
29268 +               if (!h_file)
29269 +                       continue;
29270 +
29271 +               au_fclr_rdu(cookie->flags, FULL);
29272 +               err = au_rdu_do(h_file, &arg);
29273 +               AuTraceErr(err);
29274 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29275 +                       break;
29276 +       }
29277 +       AuDbg("rent %llu\n", rdu->rent);
29278 +
29279 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29280 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29281 +               au_fset_rdu(cookie->flags, CONT);
29282 +               cookie->generation = au_figen(file);
29283 +       }
29284 +
29285 +       ii_read_lock_child(inode);
29286 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
29287 +       ii_read_unlock(inode);
29288 +
29289 +out_unlock:
29290 +       fi_read_unlock(file);
29291 +out_si:
29292 +       si_read_unlock(arg.sb);
29293 +out_mtx:
29294 +       inode_unlock_shared(inode);
29295 +out:
29296 +       AuTraceErr(err);
29297 +       return err;
29298 +}
29299 +
29300 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29301 +{
29302 +       int err;
29303 +       ino_t ino;
29304 +       unsigned long long nent;
29305 +       union au_rdu_ent_ul *u;
29306 +       struct au_rdu_ent ent;
29307 +       struct super_block *sb;
29308 +
29309 +       err = 0;
29310 +       nent = rdu->nent;
29311 +       u = &rdu->ent;
29312 +       sb = file->f_path.dentry->d_sb;
29313 +       si_read_lock(sb, AuLock_FLUSH);
29314 +       while (nent-- > 0) {
29315 +               /* unnecessary to support mmap_sem since this is a dir */
29316 +               err = copy_from_user(&ent, u->e, sizeof(ent));
29317 +               if (!err)
29318 +                       /* VERIFY_WRITE */
29319 +                       err = !access_ok(&u->e->ino, sizeof(ino));
29320 +               if (unlikely(err)) {
29321 +                       err = -EFAULT;
29322 +                       AuTraceErr(err);
29323 +                       break;
29324 +               }
29325 +
29326 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29327 +               if (!ent.wh)
29328 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29329 +               else
29330 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29331 +                                       &ino);
29332 +               if (unlikely(err)) {
29333 +                       AuTraceErr(err);
29334 +                       break;
29335 +               }
29336 +
29337 +               err = __put_user(ino, &u->e->ino);
29338 +               if (unlikely(err)) {
29339 +                       err = -EFAULT;
29340 +                       AuTraceErr(err);
29341 +                       break;
29342 +               }
29343 +               u->ul += au_rdu_len(ent.nlen);
29344 +       }
29345 +       si_read_unlock(sb);
29346 +
29347 +       return err;
29348 +}
29349 +
29350 +/* ---------------------------------------------------------------------- */
29351 +
29352 +static int au_rdu_verify(struct aufs_rdu *rdu)
29353 +{
29354 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29355 +             "%llu, b%d, 0x%x, g%u}\n",
29356 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29357 +             rdu->blk,
29358 +             rdu->rent, rdu->shwh, rdu->full,
29359 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29360 +             rdu->cookie.generation);
29361 +
29362 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29363 +               return 0;
29364 +
29365 +       AuDbg("%u:%u\n",
29366 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29367 +       return -EINVAL;
29368 +}
29369 +
29370 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29371 +{
29372 +       long err, e;
29373 +       struct aufs_rdu rdu;
29374 +       void __user *p = (void __user *)arg;
29375 +
29376 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29377 +       if (unlikely(err)) {
29378 +               err = -EFAULT;
29379 +               AuTraceErr(err);
29380 +               goto out;
29381 +       }
29382 +       err = au_rdu_verify(&rdu);
29383 +       if (unlikely(err))
29384 +               goto out;
29385 +
29386 +       switch (cmd) {
29387 +       case AUFS_CTL_RDU:
29388 +               err = au_rdu(file, &rdu);
29389 +               if (unlikely(err))
29390 +                       break;
29391 +
29392 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29393 +               if (unlikely(e)) {
29394 +                       err = -EFAULT;
29395 +                       AuTraceErr(err);
29396 +               }
29397 +               break;
29398 +       case AUFS_CTL_RDU_INO:
29399 +               err = au_rdu_ino(file, &rdu);
29400 +               break;
29401 +
29402 +       default:
29403 +               /* err = -ENOTTY; */
29404 +               err = -EINVAL;
29405 +       }
29406 +
29407 +out:
29408 +       AuTraceErr(err);
29409 +       return err;
29410 +}
29411 +
29412 +#ifdef CONFIG_COMPAT
29413 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29414 +{
29415 +       long err, e;
29416 +       struct aufs_rdu rdu;
29417 +       void __user *p = compat_ptr(arg);
29418 +
29419 +       /* todo: get_user()? */
29420 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29421 +       if (unlikely(err)) {
29422 +               err = -EFAULT;
29423 +               AuTraceErr(err);
29424 +               goto out;
29425 +       }
29426 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29427 +       err = au_rdu_verify(&rdu);
29428 +       if (unlikely(err))
29429 +               goto out;
29430 +
29431 +       switch (cmd) {
29432 +       case AUFS_CTL_RDU:
29433 +               err = au_rdu(file, &rdu);
29434 +               if (unlikely(err))
29435 +                       break;
29436 +
29437 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29438 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29439 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29440 +               if (unlikely(e)) {
29441 +                       err = -EFAULT;
29442 +                       AuTraceErr(err);
29443 +               }
29444 +               break;
29445 +       case AUFS_CTL_RDU_INO:
29446 +               err = au_rdu_ino(file, &rdu);
29447 +               break;
29448 +
29449 +       default:
29450 +               /* err = -ENOTTY; */
29451 +               err = -EINVAL;
29452 +       }
29453 +
29454 +out:
29455 +       AuTraceErr(err);
29456 +       return err;
29457 +}
29458 +#endif
29459 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29460 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29461 +++ linux/fs/aufs/rwsem.h       2022-11-05 23:02:18.969222617 +0100
29462 @@ -0,0 +1,85 @@
29463 +/* SPDX-License-Identifier: GPL-2.0 */
29464 +/*
29465 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29466 + *
29467 + * This program is free software; you can redistribute it and/or modify
29468 + * it under the terms of the GNU General Public License as published by
29469 + * the Free Software Foundation; either version 2 of the License, or
29470 + * (at your option) any later version.
29471 + *
29472 + * This program is distributed in the hope that it will be useful,
29473 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29474 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29475 + * GNU General Public License for more details.
29476 + *
29477 + * You should have received a copy of the GNU General Public License
29478 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29479 + */
29480 +
29481 +/*
29482 + * simple read-write semaphore wrappers
29483 + */
29484 +
29485 +#ifndef __AUFS_RWSEM_H__
29486 +#define __AUFS_RWSEM_H__
29487 +
29488 +#ifdef __KERNEL__
29489 +
29490 +#include "debug.h"
29491 +
29492 +/* in the future, the name 'au_rwsem' will be totally gone */
29493 +#define au_rwsem       rw_semaphore
29494 +
29495 +/* to debug easier, do not make them inlined functions */
29496 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29497 +
29498 +#ifdef CONFIG_LOCKDEP
29499 +/* rwsem_is_locked() is unusable */
29500 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29501 +                                         && !lockdep_recursing(current) \
29502 +                                         && debug_locks                \
29503 +                                         && !lockdep_is_held_type(rw, 1))
29504 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29505 +                                         && !lockdep_recursing(current) \
29506 +                                         && debug_locks                \
29507 +                                         && !lockdep_is_held_type(rw, 0))
29508 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29509 +                                         && !lockdep_recursing(current) \
29510 +                                         && debug_locks                \
29511 +                                         && !lockdep_is_held(rw))
29512 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29513 +                                         && !lockdep_recursing(current) \
29514 +                                         && debug_locks                \
29515 +                                         && lockdep_is_held(rw))
29516 +#else
29517 +#define AuRwMustReadLock(rw)   do {} while (0)
29518 +#define AuRwMustWriteLock(rw)  do {} while (0)
29519 +#define AuRwMustAnyLock(rw)    do {} while (0)
29520 +#define AuRwDestroy(rw)                do {} while (0)
29521 +#endif
29522 +
29523 +#define au_rw_init(rw) init_rwsem(rw)
29524 +
29525 +#define au_rw_init_wlock(rw) do {              \
29526 +               au_rw_init(rw);                 \
29527 +               down_write(rw);                 \
29528 +       } while (0)
29529 +
29530 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29531 +               au_rw_init(rw);                 \
29532 +               down_write_nested(rw, lsc);     \
29533 +       } while (0)
29534 +
29535 +#define au_rw_read_lock(rw)            down_read(rw)
29536 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29537 +#define au_rw_read_unlock(rw)          up_read(rw)
29538 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29539 +#define au_rw_write_lock(rw)           down_write(rw)
29540 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29541 +#define au_rw_write_unlock(rw)         up_write(rw)
29542 +/* why is not _nested version defined? */
29543 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29544 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29545 +
29546 +#endif /* __KERNEL__ */
29547 +#endif /* __AUFS_RWSEM_H__ */
29548 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29549 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29550 +++ linux/fs/aufs/sbinfo.c      2022-11-05 23:02:18.969222617 +0100
29551 @@ -0,0 +1,316 @@
29552 +// SPDX-License-Identifier: GPL-2.0
29553 +/*
29554 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29555 + *
29556 + * This program is free software; you can redistribute it and/or modify
29557 + * it under the terms of the GNU General Public License as published by
29558 + * the Free Software Foundation; either version 2 of the License, or
29559 + * (at your option) any later version.
29560 + *
29561 + * This program is distributed in the hope that it will be useful,
29562 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29563 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29564 + * GNU General Public License for more details.
29565 + *
29566 + * You should have received a copy of the GNU General Public License
29567 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29568 + */
29569 +
29570 +/*
29571 + * superblock private data
29572 + */
29573 +
29574 +#include <linux/iversion.h>
29575 +#include "aufs.h"
29576 +
29577 +/*
29578 + * they are necessary regardless sysfs is disabled.
29579 + */
29580 +void au_si_free(struct kobject *kobj)
29581 +{
29582 +       int i;
29583 +       struct au_sbinfo *sbinfo;
29584 +       char *locked __maybe_unused; /* debug only */
29585 +
29586 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29587 +       for (i = 0; i < AuPlink_NHASH; i++)
29588 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29589 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29590 +
29591 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29592 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29593 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29594 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29595 +
29596 +       dbgaufs_si_fin(sbinfo);
29597 +       au_rw_write_lock(&sbinfo->si_rwsem);
29598 +       au_br_free(sbinfo);
29599 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29600 +
29601 +       au_kfree_try_rcu(sbinfo->si_branch);
29602 +       mutex_destroy(&sbinfo->si_xib_mtx);
29603 +       AuRwDestroy(&sbinfo->si_rwsem);
29604 +
29605 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29606 +       /* si_nfiles is waited too */
29607 +       au_kfree_rcu(sbinfo);
29608 +}
29609 +
29610 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29611 +{
29612 +       struct au_sbinfo *sbinfo;
29613 +       int err, i;
29614 +
29615 +       err = -ENOMEM;
29616 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29617 +       if (unlikely(!sbinfo))
29618 +               goto out;
29619 +
29620 +       /* will be reallocated separately */
29621 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29622 +       if (unlikely(!sbinfo->si_branch))
29623 +               goto out_sbinfo;
29624 +
29625 +       err = sysaufs_si_init(sbinfo);
29626 +       if (!err) {
29627 +               dbgaufs_si_null(sbinfo);
29628 +               err = dbgaufs_si_init(sbinfo);
29629 +               if (unlikely(err))
29630 +                       kobject_put(&sbinfo->si_kobj);
29631 +       }
29632 +       if (unlikely(err))
29633 +               goto out_br;
29634 +
29635 +       au_nwt_init(&sbinfo->si_nowait);
29636 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29637 +
29638 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29639 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29640 +
29641 +       sbinfo->si_bbot = -1;
29642 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29643 +
29644 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29645 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29646 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29647 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29648 +
29649 +       au_fhsm_init(sbinfo);
29650 +
29651 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29652 +
29653 +       sbinfo->si_xino_jiffy = jiffies;
29654 +       sbinfo->si_xino_expire
29655 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29656 +       mutex_init(&sbinfo->si_xib_mtx);
29657 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29658 +
29659 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29660 +
29661 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29662 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29663 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29664 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29665 +
29666 +       for (i = 0; i < AuPlink_NHASH; i++)
29667 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29668 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29669 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29670 +
29671 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29672 +
29673 +       /* with getattr by default */
29674 +       sbinfo->si_iop_array = aufs_iop;
29675 +
29676 +       /* leave other members for sysaufs and si_mnt. */
29677 +       sbinfo->si_sb = sb;
29678 +       if (sb) {
29679 +               sb->s_fs_info = sbinfo;
29680 +               si_pid_set(sb);
29681 +       }
29682 +       return sbinfo; /* success */
29683 +
29684 +out_br:
29685 +       au_kfree_try_rcu(sbinfo->si_branch);
29686 +out_sbinfo:
29687 +       au_kfree_rcu(sbinfo);
29688 +out:
29689 +       return ERR_PTR(err);
29690 +}
29691 +
29692 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29693 +{
29694 +       int err, sz;
29695 +       struct au_branch **brp;
29696 +
29697 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29698 +
29699 +       err = -ENOMEM;
29700 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29701 +       if (unlikely(!sz))
29702 +               sz = sizeof(*brp);
29703 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29704 +                          may_shrink);
29705 +       if (brp) {
29706 +               sbinfo->si_branch = brp;
29707 +               err = 0;
29708 +       }
29709 +
29710 +       return err;
29711 +}
29712 +
29713 +/* ---------------------------------------------------------------------- */
29714 +
29715 +unsigned int au_sigen_inc(struct super_block *sb)
29716 +{
29717 +       unsigned int gen;
29718 +       struct inode *inode;
29719 +
29720 +       SiMustWriteLock(sb);
29721 +
29722 +       gen = ++au_sbi(sb)->si_generation;
29723 +       au_update_digen(sb->s_root);
29724 +       inode = d_inode(sb->s_root);
29725 +       au_update_iigen(inode, /*half*/0);
29726 +       inode_inc_iversion(inode);
29727 +       return gen;
29728 +}
29729 +
29730 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29731 +{
29732 +       aufs_bindex_t br_id;
29733 +       int i;
29734 +       struct au_sbinfo *sbinfo;
29735 +
29736 +       SiMustWriteLock(sb);
29737 +
29738 +       sbinfo = au_sbi(sb);
29739 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29740 +               br_id = ++sbinfo->si_last_br_id;
29741 +               AuDebugOn(br_id < 0);
29742 +               if (br_id && au_br_index(sb, br_id) < 0)
29743 +                       return br_id;
29744 +       }
29745 +
29746 +       return -1;
29747 +}
29748 +
29749 +/* ---------------------------------------------------------------------- */
29750 +
29751 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29752 +int si_read_lock(struct super_block *sb, int flags)
29753 +{
29754 +       int err;
29755 +
29756 +       err = 0;
29757 +       if (au_ftest_lock(flags, FLUSH))
29758 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29759 +
29760 +       si_noflush_read_lock(sb);
29761 +       err = au_plink_maint(sb, flags);
29762 +       if (unlikely(err))
29763 +               si_read_unlock(sb);
29764 +
29765 +       return err;
29766 +}
29767 +
29768 +int si_write_lock(struct super_block *sb, int flags)
29769 +{
29770 +       int err;
29771 +
29772 +       if (au_ftest_lock(flags, FLUSH))
29773 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29774 +
29775 +       si_noflush_write_lock(sb);
29776 +       err = au_plink_maint(sb, flags);
29777 +       if (unlikely(err))
29778 +               si_write_unlock(sb);
29779 +
29780 +       return err;
29781 +}
29782 +
29783 +/* dentry and super_block lock. call at entry point */
29784 +int aufs_read_lock(struct dentry *dentry, int flags)
29785 +{
29786 +       int err;
29787 +       struct super_block *sb;
29788 +
29789 +       sb = dentry->d_sb;
29790 +       err = si_read_lock(sb, flags);
29791 +       if (unlikely(err))
29792 +               goto out;
29793 +
29794 +       if (au_ftest_lock(flags, DW))
29795 +               di_write_lock_child(dentry);
29796 +       else
29797 +               di_read_lock_child(dentry, flags);
29798 +
29799 +       if (au_ftest_lock(flags, GEN)) {
29800 +               err = au_digen_test(dentry, au_sigen(sb));
29801 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29802 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29803 +               else if (!err)
29804 +                       err = au_dbrange_test(dentry);
29805 +               if (unlikely(err))
29806 +                       aufs_read_unlock(dentry, flags);
29807 +       }
29808 +
29809 +out:
29810 +       return err;
29811 +}
29812 +
29813 +void aufs_read_unlock(struct dentry *dentry, int flags)
29814 +{
29815 +       if (au_ftest_lock(flags, DW))
29816 +               di_write_unlock(dentry);
29817 +       else
29818 +               di_read_unlock(dentry, flags);
29819 +       si_read_unlock(dentry->d_sb);
29820 +}
29821 +
29822 +void aufs_write_lock(struct dentry *dentry)
29823 +{
29824 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29825 +       di_write_lock_child(dentry);
29826 +}
29827 +
29828 +void aufs_write_unlock(struct dentry *dentry)
29829 +{
29830 +       di_write_unlock(dentry);
29831 +       si_write_unlock(dentry->d_sb);
29832 +}
29833 +
29834 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29835 +{
29836 +       int err;
29837 +       unsigned int sigen;
29838 +       struct super_block *sb;
29839 +
29840 +       sb = d1->d_sb;
29841 +       err = si_read_lock(sb, flags);
29842 +       if (unlikely(err))
29843 +               goto out;
29844 +
29845 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29846 +
29847 +       if (au_ftest_lock(flags, GEN)) {
29848 +               sigen = au_sigen(sb);
29849 +               err = au_digen_test(d1, sigen);
29850 +               AuDebugOn(!err && au_dbrange_test(d1));
29851 +               if (!err) {
29852 +                       err = au_digen_test(d2, sigen);
29853 +                       AuDebugOn(!err && au_dbrange_test(d2));
29854 +               }
29855 +               if (unlikely(err))
29856 +                       aufs_read_and_write_unlock2(d1, d2);
29857 +       }
29858 +
29859 +out:
29860 +       return err;
29861 +}
29862 +
29863 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29864 +{
29865 +       di_write_unlock2(d1, d2);
29866 +       si_read_unlock(d1->d_sb);
29867 +}
29868 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29869 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29870 +++ linux/fs/aufs/super.c       2022-11-05 23:02:18.969222617 +0100
29871 @@ -0,0 +1,871 @@
29872 +// SPDX-License-Identifier: GPL-2.0
29873 +/*
29874 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29875 + *
29876 + * This program is free software; you can redistribute it and/or modify
29877 + * it under the terms of the GNU General Public License as published by
29878 + * the Free Software Foundation; either version 2 of the License, or
29879 + * (at your option) any later version.
29880 + *
29881 + * This program is distributed in the hope that it will be useful,
29882 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29883 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29884 + * GNU General Public License for more details.
29885 + *
29886 + * You should have received a copy of the GNU General Public License
29887 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29888 + */
29889 +
29890 +/*
29891 + * mount and super_block operations
29892 + */
29893 +
29894 +#include <linux/iversion.h>
29895 +#include <linux/mm.h>
29896 +#include <linux/seq_file.h>
29897 +#include <linux/statfs.h>
29898 +#include <linux/vmalloc.h>
29899 +#include "aufs.h"
29900 +
29901 +/*
29902 + * super_operations
29903 + */
29904 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29905 +{
29906 +       struct au_icntnr *c;
29907 +
29908 +       c = au_cache_alloc_icntnr(sb);
29909 +       if (c) {
29910 +               au_icntnr_init(c);
29911 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29912 +               c->iinfo.ii_hinode = NULL;
29913 +               return &c->vfs_inode;
29914 +       }
29915 +       return NULL;
29916 +}
29917 +
29918 +static void aufs_destroy_inode(struct inode *inode)
29919 +{
29920 +       if (!au_is_bad_inode(inode))
29921 +               au_iinfo_fin(inode);
29922 +}
29923 +
29924 +static void aufs_free_inode(struct inode *inode)
29925 +{
29926 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29927 +}
29928 +
29929 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29930 +{
29931 +       struct inode *inode;
29932 +       int err;
29933 +
29934 +       inode = iget_locked(sb, ino);
29935 +       if (unlikely(!inode)) {
29936 +               inode = ERR_PTR(-ENOMEM);
29937 +               goto out;
29938 +       }
29939 +       if (!(inode->i_state & I_NEW))
29940 +               goto out;
29941 +
29942 +       err = au_xigen_new(inode);
29943 +       if (!err)
29944 +               err = au_iinfo_init(inode);
29945 +       if (!err)
29946 +               inode_inc_iversion(inode);
29947 +       else {
29948 +               iget_failed(inode);
29949 +               inode = ERR_PTR(err);
29950 +       }
29951 +
29952 +out:
29953 +       /* never return NULL */
29954 +       AuDebugOn(!inode);
29955 +       AuTraceErrPtr(inode);
29956 +       return inode;
29957 +}
29958 +
29959 +/* lock free root dinfo */
29960 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29961 +{
29962 +       int err;
29963 +       aufs_bindex_t bindex, bbot;
29964 +       struct path path;
29965 +       struct au_hdentry *hdp;
29966 +       struct au_branch *br;
29967 +       au_br_perm_str_t perm;
29968 +
29969 +       err = 0;
29970 +       bbot = au_sbbot(sb);
29971 +       bindex = 0;
29972 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29973 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29974 +               br = au_sbr(sb, bindex);
29975 +               path.mnt = au_br_mnt(br);
29976 +               path.dentry = hdp->hd_dentry;
29977 +               err = au_seq_path(seq, &path);
29978 +               if (!err) {
29979 +                       au_optstr_br_perm(&perm, br->br_perm);
29980 +                       seq_printf(seq, "=%s", perm.a);
29981 +                       if (bindex != bbot)
29982 +                               seq_putc(seq, ':');
29983 +               }
29984 +       }
29985 +       if (unlikely(err || seq_has_overflowed(seq)))
29986 +               err = -E2BIG;
29987 +
29988 +       return err;
29989 +}
29990 +
29991 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29992 +                      const char *append)
29993 +{
29994 +       char *p;
29995 +
29996 +       p = fmt;
29997 +       while (*pat != ':')
29998 +               *p++ = *pat++;
29999 +       *p++ = *pat++;
30000 +       strcpy(p, append);
30001 +       AuDebugOn(strlen(fmt) >= len);
30002 +}
30003 +
30004 +static void au_show_wbr_create(struct seq_file *m, int v,
30005 +                              struct au_sbinfo *sbinfo)
30006 +{
30007 +       const char *pat;
30008 +       char fmt[32];
30009 +       struct au_wbr_mfs *mfs;
30010 +
30011 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30012 +
30013 +       seq_puts(m, ",create=");
30014 +       pat = au_optstr_wbr_create(v);
30015 +       mfs = &sbinfo->si_wbr_mfs;
30016 +       switch (v) {
30017 +       case AuWbrCreate_TDP:
30018 +       case AuWbrCreate_RR:
30019 +       case AuWbrCreate_MFS:
30020 +       case AuWbrCreate_PMFS:
30021 +               seq_puts(m, pat);
30022 +               break;
30023 +       case AuWbrCreate_MFSRR:
30024 +       case AuWbrCreate_TDMFS:
30025 +       case AuWbrCreate_PMFSRR:
30026 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
30027 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
30028 +               break;
30029 +       case AuWbrCreate_MFSV:
30030 +       case AuWbrCreate_PMFSV:
30031 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
30032 +               seq_printf(m, fmt,
30033 +                          jiffies_to_msecs(mfs->mfs_expire)
30034 +                          / MSEC_PER_SEC);
30035 +               break;
30036 +       case AuWbrCreate_MFSRRV:
30037 +       case AuWbrCreate_TDMFSV:
30038 +       case AuWbrCreate_PMFSRRV:
30039 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
30040 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
30041 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
30042 +               break;
30043 +       default:
30044 +               BUG();
30045 +       }
30046 +}
30047 +
30048 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
30049 +{
30050 +#ifdef CONFIG_SYSFS
30051 +       return 0;
30052 +#else
30053 +       int err;
30054 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
30055 +       aufs_bindex_t bindex, brid;
30056 +       struct qstr *name;
30057 +       struct file *f;
30058 +       struct dentry *d, *h_root;
30059 +       struct au_branch *br;
30060 +
30061 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30062 +
30063 +       err = 0;
30064 +       f = au_sbi(sb)->si_xib;
30065 +       if (!f)
30066 +               goto out;
30067 +
30068 +       /* stop printing the default xino path on the first writable branch */
30069 +       h_root = NULL;
30070 +       bindex = au_xi_root(sb, f->f_path.dentry);
30071 +       if (bindex >= 0) {
30072 +               br = au_sbr_sb(sb, bindex);
30073 +               h_root = au_br_dentry(br);
30074 +       }
30075 +
30076 +       d = f->f_path.dentry;
30077 +       name = &d->d_name;
30078 +       /* safe ->d_parent because the file is unlinked */
30079 +       if (d->d_parent == h_root
30080 +           && name->len == len
30081 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
30082 +               goto out;
30083 +
30084 +       seq_puts(seq, ",xino=");
30085 +       err = au_xino_path(seq, f);
30086 +
30087 +out:
30088 +       return err;
30089 +#endif
30090 +}
30091 +
30092 +/* seq_file will re-call me in case of too long string */
30093 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
30094 +{
30095 +       int err;
30096 +       unsigned int mnt_flags, v;
30097 +       struct super_block *sb;
30098 +       struct au_sbinfo *sbinfo;
30099 +
30100 +#define AuBool(name, str) do { \
30101 +       v = au_opt_test(mnt_flags, name); \
30102 +       if (v != au_opt_test(AuOpt_Def, name)) \
30103 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
30104 +} while (0)
30105 +
30106 +#define AuStr(name, str) do { \
30107 +       v = mnt_flags & AuOptMask_##name; \
30108 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
30109 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30110 +} while (0)
30111 +
30112 +#define AuUInt(name, str, val) do { \
30113 +       if (val != AUFS_##name##_DEF) \
30114 +               seq_printf(m, "," #str "=%u", val); \
30115 +} while (0)
30116 +
30117 +       sb = dentry->d_sb;
30118 +       if (sb->s_flags & SB_POSIXACL)
30119 +               seq_puts(m, ",acl");
30120 +#if 0 /* reserved for future use */
30121 +       if (sb->s_flags & SB_I_VERSION)
30122 +               seq_puts(m, ",i_version");
30123 +#endif
30124 +
30125 +       /* lock free root dinfo */
30126 +       si_noflush_read_lock(sb);
30127 +       sbinfo = au_sbi(sb);
30128 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30129 +
30130 +       mnt_flags = au_mntflags(sb);
30131 +       if (au_opt_test(mnt_flags, XINO)) {
30132 +               err = au_show_xino(m, sb);
30133 +               if (unlikely(err))
30134 +                       goto out;
30135 +       } else
30136 +               seq_puts(m, ",noxino");
30137 +
30138 +       AuBool(TRUNC_XINO, trunc_xino);
30139 +       AuStr(UDBA, udba);
30140 +       AuBool(SHWH, shwh);
30141 +       AuBool(PLINK, plink);
30142 +       AuBool(DIO, dio);
30143 +       AuBool(DIRPERM1, dirperm1);
30144 +
30145 +       v = sbinfo->si_wbr_create;
30146 +       if (v != AuWbrCreate_Def)
30147 +               au_show_wbr_create(m, v, sbinfo);
30148 +
30149 +       v = sbinfo->si_wbr_copyup;
30150 +       if (v != AuWbrCopyup_Def)
30151 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30152 +
30153 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30154 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30155 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30156 +
30157 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30158 +
30159 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30160 +       AuUInt(RDCACHE, rdcache, v);
30161 +
30162 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30163 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30164 +
30165 +       au_fhsm_show(m, sbinfo);
30166 +
30167 +       AuBool(DIRREN, dirren);
30168 +       AuBool(SUM, sum);
30169 +       /* AuBool(SUM_W, wsum); */
30170 +       AuBool(WARN_PERM, warn_perm);
30171 +       AuBool(VERBOSE, verbose);
30172 +
30173 +out:
30174 +       /* be sure to print "br:" last */
30175 +       if (!sysaufs_brs) {
30176 +               seq_puts(m, ",br:");
30177 +               au_show_brs(m, sb);
30178 +       }
30179 +       si_read_unlock(sb);
30180 +       return 0;
30181 +
30182 +#undef AuBool
30183 +#undef AuStr
30184 +#undef AuUInt
30185 +}
30186 +
30187 +/* ---------------------------------------------------------------------- */
30188 +
30189 +/* sum mode which returns the summation for statfs(2) */
30190 +
30191 +static u64 au_add_till_max(u64 a, u64 b)
30192 +{
30193 +       u64 old;
30194 +
30195 +       old = a;
30196 +       a += b;
30197 +       if (old <= a)
30198 +               return a;
30199 +       return ULLONG_MAX;
30200 +}
30201 +
30202 +static u64 au_mul_till_max(u64 a, long mul)
30203 +{
30204 +       u64 old;
30205 +
30206 +       old = a;
30207 +       a *= mul;
30208 +       if (old <= a)
30209 +               return a;
30210 +       return ULLONG_MAX;
30211 +}
30212 +
30213 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30214 +{
30215 +       int err;
30216 +       long bsize, factor;
30217 +       u64 blocks, bfree, bavail, files, ffree;
30218 +       aufs_bindex_t bbot, bindex, i;
30219 +       unsigned char shared;
30220 +       struct path h_path;
30221 +       struct super_block *h_sb;
30222 +
30223 +       err = 0;
30224 +       bsize = LONG_MAX;
30225 +       files = 0;
30226 +       ffree = 0;
30227 +       blocks = 0;
30228 +       bfree = 0;
30229 +       bavail = 0;
30230 +       bbot = au_sbbot(sb);
30231 +       for (bindex = 0; bindex <= bbot; bindex++) {
30232 +               h_path.mnt = au_sbr_mnt(sb, bindex);
30233 +               h_sb = h_path.mnt->mnt_sb;
30234 +               shared = 0;
30235 +               for (i = 0; !shared && i < bindex; i++)
30236 +                       shared = (au_sbr_sb(sb, i) == h_sb);
30237 +               if (shared)
30238 +                       continue;
30239 +
30240 +               /* sb->s_root for NFS is unreliable */
30241 +               h_path.dentry = h_path.mnt->mnt_root;
30242 +               err = vfs_statfs(&h_path, buf);
30243 +               if (unlikely(err))
30244 +                       goto out;
30245 +
30246 +               if (bsize > buf->f_bsize) {
30247 +                       /*
30248 +                        * we will reduce bsize, so we have to expand blocks
30249 +                        * etc. to match them again
30250 +                        */
30251 +                       factor = (bsize / buf->f_bsize);
30252 +                       blocks = au_mul_till_max(blocks, factor);
30253 +                       bfree = au_mul_till_max(bfree, factor);
30254 +                       bavail = au_mul_till_max(bavail, factor);
30255 +                       bsize = buf->f_bsize;
30256 +               }
30257 +
30258 +               factor = (buf->f_bsize / bsize);
30259 +               blocks = au_add_till_max(blocks,
30260 +                               au_mul_till_max(buf->f_blocks, factor));
30261 +               bfree = au_add_till_max(bfree,
30262 +                               au_mul_till_max(buf->f_bfree, factor));
30263 +               bavail = au_add_till_max(bavail,
30264 +                               au_mul_till_max(buf->f_bavail, factor));
30265 +               files = au_add_till_max(files, buf->f_files);
30266 +               ffree = au_add_till_max(ffree, buf->f_ffree);
30267 +       }
30268 +
30269 +       buf->f_bsize = bsize;
30270 +       buf->f_blocks = blocks;
30271 +       buf->f_bfree = bfree;
30272 +       buf->f_bavail = bavail;
30273 +       buf->f_files = files;
30274 +       buf->f_ffree = ffree;
30275 +       buf->f_frsize = 0;
30276 +
30277 +out:
30278 +       return err;
30279 +}
30280 +
30281 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30282 +{
30283 +       int err;
30284 +       struct path h_path;
30285 +       struct super_block *sb;
30286 +
30287 +       /* lock free root dinfo */
30288 +       sb = dentry->d_sb;
30289 +       si_noflush_read_lock(sb);
30290 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
30291 +               /* sb->s_root for NFS is unreliable */
30292 +               h_path.mnt = au_sbr_mnt(sb, 0);
30293 +               h_path.dentry = h_path.mnt->mnt_root;
30294 +               err = vfs_statfs(&h_path, buf);
30295 +       } else
30296 +               err = au_statfs_sum(sb, buf);
30297 +       si_read_unlock(sb);
30298 +
30299 +       if (!err) {
30300 +               buf->f_type = AUFS_SUPER_MAGIC;
30301 +               buf->f_namelen = AUFS_MAX_NAMELEN;
30302 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30303 +       }
30304 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30305 +
30306 +       return err;
30307 +}
30308 +
30309 +/* ---------------------------------------------------------------------- */
30310 +
30311 +static int aufs_sync_fs(struct super_block *sb, int wait)
30312 +{
30313 +       int err, e;
30314 +       aufs_bindex_t bbot, bindex;
30315 +       struct au_branch *br;
30316 +       struct super_block *h_sb;
30317 +
30318 +       err = 0;
30319 +       si_noflush_read_lock(sb);
30320 +       bbot = au_sbbot(sb);
30321 +       for (bindex = 0; bindex <= bbot; bindex++) {
30322 +               br = au_sbr(sb, bindex);
30323 +               if (!au_br_writable(br->br_perm))
30324 +                       continue;
30325 +
30326 +               h_sb = au_sbr_sb(sb, bindex);
30327 +               e = vfsub_sync_filesystem(h_sb);
30328 +               if (unlikely(e && !err))
30329 +                       err = e;
30330 +               /* go on even if an error happens */
30331 +       }
30332 +       si_read_unlock(sb);
30333 +
30334 +       return err;
30335 +}
30336 +
30337 +/* ---------------------------------------------------------------------- */
30338 +
30339 +/* final actions when unmounting a file system */
30340 +static void aufs_put_super(struct super_block *sb)
30341 +{
30342 +       struct au_sbinfo *sbinfo;
30343 +
30344 +       sbinfo = au_sbi(sb);
30345 +       if (sbinfo)
30346 +               kobject_put(&sbinfo->si_kobj);
30347 +}
30348 +
30349 +/* ---------------------------------------------------------------------- */
30350 +
30351 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30352 +                    struct super_block *sb, void *arg)
30353 +{
30354 +       void *array;
30355 +       unsigned long long n, sz;
30356 +
30357 +       array = NULL;
30358 +       n = 0;
30359 +       if (!*hint)
30360 +               goto out;
30361 +
30362 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30363 +               array = ERR_PTR(-EMFILE);
30364 +               pr_err("hint %llu\n", *hint);
30365 +               goto out;
30366 +       }
30367 +
30368 +       sz = sizeof(array) * *hint;
30369 +       array = kzalloc(sz, GFP_NOFS);
30370 +       if (unlikely(!array))
30371 +               array = vzalloc(sz);
30372 +       if (unlikely(!array)) {
30373 +               array = ERR_PTR(-ENOMEM);
30374 +               goto out;
30375 +       }
30376 +
30377 +       n = cb(sb, array, *hint, arg);
30378 +       AuDebugOn(n > *hint);
30379 +
30380 +out:
30381 +       *hint = n;
30382 +       return array;
30383 +}
30384 +
30385 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30386 +                                      unsigned long long max __maybe_unused,
30387 +                                      void *arg)
30388 +{
30389 +       unsigned long long n;
30390 +       struct inode **p, *inode;
30391 +       struct list_head *head;
30392 +
30393 +       n = 0;
30394 +       p = a;
30395 +       head = arg;
30396 +       spin_lock(&sb->s_inode_list_lock);
30397 +       list_for_each_entry(inode, head, i_sb_list) {
30398 +               if (!au_is_bad_inode(inode)
30399 +                   && au_ii(inode)->ii_btop >= 0) {
30400 +                       spin_lock(&inode->i_lock);
30401 +                       if (atomic_read(&inode->i_count)) {
30402 +                               au_igrab(inode);
30403 +                               *p++ = inode;
30404 +                               n++;
30405 +                               AuDebugOn(n > max);
30406 +                       }
30407 +                       spin_unlock(&inode->i_lock);
30408 +               }
30409 +       }
30410 +       spin_unlock(&sb->s_inode_list_lock);
30411 +
30412 +       return n;
30413 +}
30414 +
30415 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30416 +{
30417 +       struct au_sbinfo *sbi;
30418 +
30419 +       sbi = au_sbi(sb);
30420 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30421 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30422 +}
30423 +
30424 +void au_iarray_free(struct inode **a, unsigned long long max)
30425 +{
30426 +       unsigned long long ull;
30427 +
30428 +       for (ull = 0; ull < max; ull++)
30429 +               iput(a[ull]);
30430 +       kvfree(a);
30431 +}
30432 +
30433 +/* ---------------------------------------------------------------------- */
30434 +
30435 +/*
30436 + * refresh dentry and inode at remount time.
30437 + */
30438 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30439 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30440 +                     struct dentry *parent)
30441 +{
30442 +       int err;
30443 +
30444 +       di_write_lock_child(dentry);
30445 +       di_read_lock_parent(parent, AuLock_IR);
30446 +       err = au_refresh_dentry(dentry, parent);
30447 +       if (!err && dir_flags)
30448 +               au_hn_reset(d_inode(dentry), dir_flags);
30449 +       di_read_unlock(parent, AuLock_IR);
30450 +       di_write_unlock(dentry);
30451 +
30452 +       return err;
30453 +}
30454 +
30455 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30456 +                          struct au_sbinfo *sbinfo,
30457 +                          const unsigned int dir_flags, unsigned int do_idop)
30458 +{
30459 +       int err;
30460 +       struct dentry *parent;
30461 +
30462 +       err = 0;
30463 +       parent = dget_parent(dentry);
30464 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30465 +               if (d_really_is_positive(dentry)) {
30466 +                       if (!d_is_dir(dentry))
30467 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30468 +                                                parent);
30469 +                       else {
30470 +                               err = au_do_refresh(dentry, dir_flags, parent);
30471 +                               if (unlikely(err))
30472 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30473 +                       }
30474 +               } else
30475 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30476 +               AuDbgDentry(dentry);
30477 +       }
30478 +       dput(parent);
30479 +
30480 +       if (!err) {
30481 +               if (do_idop)
30482 +                       au_refresh_dop(dentry, /*force_reval*/0);
30483 +       } else
30484 +               au_refresh_dop(dentry, /*force_reval*/1);
30485 +
30486 +       AuTraceErr(err);
30487 +       return err;
30488 +}
30489 +
30490 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30491 +{
30492 +       int err, i, j, ndentry, e;
30493 +       unsigned int sigen;
30494 +       struct au_dcsub_pages dpages;
30495 +       struct au_dpage *dpage;
30496 +       struct dentry **dentries, *d;
30497 +       struct au_sbinfo *sbinfo;
30498 +       struct dentry *root = sb->s_root;
30499 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30500 +
30501 +       if (do_idop)
30502 +               au_refresh_dop(root, /*force_reval*/0);
30503 +
30504 +       err = au_dpages_init(&dpages, GFP_NOFS);
30505 +       if (unlikely(err))
30506 +               goto out;
30507 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30508 +       if (unlikely(err))
30509 +               goto out_dpages;
30510 +
30511 +       sigen = au_sigen(sb);
30512 +       sbinfo = au_sbi(sb);
30513 +       for (i = 0; i < dpages.ndpage; i++) {
30514 +               dpage = dpages.dpages + i;
30515 +               dentries = dpage->dentries;
30516 +               ndentry = dpage->ndentry;
30517 +               for (j = 0; j < ndentry; j++) {
30518 +                       d = dentries[j];
30519 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30520 +                                           do_idop);
30521 +                       if (unlikely(e && !err))
30522 +                               err = e;
30523 +                       /* go on even err */
30524 +               }
30525 +       }
30526 +
30527 +out_dpages:
30528 +       au_dpages_free(&dpages);
30529 +out:
30530 +       return err;
30531 +}
30532 +
30533 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30534 +{
30535 +       int err, e;
30536 +       unsigned int sigen;
30537 +       unsigned long long max, ull;
30538 +       struct inode *inode, **array;
30539 +
30540 +       array = au_iarray_alloc(sb, &max);
30541 +       err = PTR_ERR(array);
30542 +       if (IS_ERR(array))
30543 +               goto out;
30544 +
30545 +       err = 0;
30546 +       sigen = au_sigen(sb);
30547 +       for (ull = 0; ull < max; ull++) {
30548 +               inode = array[ull];
30549 +               if (unlikely(!inode))
30550 +                       break;
30551 +
30552 +               e = 0;
30553 +               ii_write_lock_child(inode);
30554 +               if (au_iigen(inode, NULL) != sigen) {
30555 +                       e = au_refresh_hinode_self(inode);
30556 +                       if (unlikely(e)) {
30557 +                               au_refresh_iop(inode, /*force_getattr*/1);
30558 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30559 +                               if (!err)
30560 +                                       err = e;
30561 +                               /* go on even if err */
30562 +                       }
30563 +               }
30564 +               if (!e && do_idop)
30565 +                       au_refresh_iop(inode, /*force_getattr*/0);
30566 +               ii_write_unlock(inode);
30567 +       }
30568 +
30569 +       au_iarray_free(array, max);
30570 +
30571 +out:
30572 +       return err;
30573 +}
30574 +
30575 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30576 +{
30577 +       int err, e;
30578 +       unsigned int udba;
30579 +       aufs_bindex_t bindex, bbot;
30580 +       struct dentry *root;
30581 +       struct inode *inode;
30582 +       struct au_branch *br;
30583 +       struct au_sbinfo *sbi;
30584 +
30585 +       au_sigen_inc(sb);
30586 +       sbi = au_sbi(sb);
30587 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30588 +
30589 +       root = sb->s_root;
30590 +       DiMustNoWaiters(root);
30591 +       inode = d_inode(root);
30592 +       IiMustNoWaiters(inode);
30593 +
30594 +       udba = au_opt_udba(sb);
30595 +       bbot = au_sbbot(sb);
30596 +       for (bindex = 0; bindex <= bbot; bindex++) {
30597 +               br = au_sbr(sb, bindex);
30598 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30599 +               if (unlikely(err))
30600 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30601 +                               bindex, err);
30602 +               /* go on even if err */
30603 +       }
30604 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30605 +
30606 +       if (do_idop) {
30607 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30608 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30609 +                       sb->s_d_op = &aufs_dop_noreval;
30610 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30611 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30612 +               } else {
30613 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30614 +                       sb->s_d_op = &aufs_dop;
30615 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30616 +                       sbi->si_iop_array = aufs_iop;
30617 +               }
30618 +               pr_info("reset to %ps and %ps\n",
30619 +                       sb->s_d_op, sbi->si_iop_array);
30620 +       }
30621 +
30622 +       di_write_unlock(root);
30623 +       err = au_refresh_d(sb, do_idop);
30624 +       e = au_refresh_i(sb, do_idop);
30625 +       if (unlikely(e && !err))
30626 +               err = e;
30627 +       /* aufs_write_lock() calls ..._child() */
30628 +       di_write_lock_child(root);
30629 +
30630 +       au_cpup_attr_all(inode, /*force*/1);
30631 +
30632 +       if (unlikely(err))
30633 +               AuIOErr("refresh failed, ignored, %d\n", err);
30634 +}
30635 +
30636 +const struct super_operations aufs_sop = {
30637 +       .alloc_inode    = aufs_alloc_inode,
30638 +       .destroy_inode  = aufs_destroy_inode,
30639 +       .free_inode     = aufs_free_inode,
30640 +       /* always deleting, no clearing */
30641 +       .drop_inode     = generic_delete_inode,
30642 +       .show_options   = aufs_show_options,
30643 +       .statfs         = aufs_statfs,
30644 +       .put_super      = aufs_put_super,
30645 +       .sync_fs        = aufs_sync_fs
30646 +};
30647 +
30648 +/* ---------------------------------------------------------------------- */
30649 +
30650 +int au_alloc_root(struct super_block *sb)
30651 +{
30652 +       int err;
30653 +       struct inode *inode;
30654 +       struct dentry *root;
30655 +
30656 +       err = -ENOMEM;
30657 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30658 +       err = PTR_ERR(inode);
30659 +       if (IS_ERR(inode))
30660 +               goto out;
30661 +
30662 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30663 +       inode->i_fop = &aufs_dir_fop;
30664 +       inode->i_mode = S_IFDIR;
30665 +       set_nlink(inode, 2);
30666 +       unlock_new_inode(inode);
30667 +
30668 +       root = d_make_root(inode);
30669 +       if (unlikely(!root))
30670 +               goto out;
30671 +       err = PTR_ERR(root);
30672 +       if (IS_ERR(root))
30673 +               goto out;
30674 +
30675 +       err = au_di_init(root);
30676 +       if (!err) {
30677 +               sb->s_root = root;
30678 +               return 0; /* success */
30679 +       }
30680 +       dput(root);
30681 +
30682 +out:
30683 +       return err;
30684 +}
30685 +
30686 +/* ---------------------------------------------------------------------- */
30687 +
30688 +static void aufs_kill_sb(struct super_block *sb)
30689 +{
30690 +       struct au_sbinfo *sbinfo;
30691 +       struct dentry *root;
30692 +
30693 +       sbinfo = au_sbi(sb);
30694 +       if (!sbinfo)
30695 +               goto out;
30696 +
30697 +       au_sbilist_del(sb);
30698 +
30699 +       root = sb->s_root;
30700 +       if (root)
30701 +               aufs_write_lock(root);
30702 +       else
30703 +               __si_write_lock(sb);
30704 +
30705 +       au_fhsm_fin(sb);
30706 +       if (sbinfo->si_wbr_create_ops->fin)
30707 +               sbinfo->si_wbr_create_ops->fin(sb);
30708 +       if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30709 +               au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30710 +               au_remount_refresh(sb, /*do_idop*/0);
30711 +       }
30712 +       if (au_opt_test(sbinfo->si_mntflags, PLINK))
30713 +               au_plink_put(sb, /*verbose*/1);
30714 +       au_xino_clr(sb);
30715 +       if (root)
30716 +               au_dr_opt_flush(sb);
30717 +
30718 +       if (root)
30719 +               aufs_write_unlock(root);
30720 +       else
30721 +               __si_write_unlock(sb);
30722 +
30723 +       sbinfo->si_sb = NULL;
30724 +       au_nwt_flush(&sbinfo->si_nowait);
30725 +
30726 +out:
30727 +       kill_anon_super(sb);
30728 +}
30729 +
30730 +struct file_system_type aufs_fs_type = {
30731 +       .name           = AUFS_FSTYPE,
30732 +       /* a race between rename and others */
30733 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30734 +                               /* untested */
30735 +                               /*| FS_ALLOW_IDMAP*/
30736 +                               ,
30737 +       .init_fs_context = aufs_fsctx_init,
30738 +       .parameters     = aufs_fsctx_paramspec,
30739 +       .kill_sb        = aufs_kill_sb,
30740 +       /* no need to __module_get() and module_put(). */
30741 +       .owner          = THIS_MODULE,
30742 +};
30743 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30744 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30745 +++ linux/fs/aufs/super.h       2022-11-05 23:02:18.969222617 +0100
30746 @@ -0,0 +1,592 @@
30747 +/* SPDX-License-Identifier: GPL-2.0 */
30748 +/*
30749 + * Copyright (C) 2005-2022 Junjiro R. Okajima
30750 + *
30751 + * This program is free software; you can redistribute it and/or modify
30752 + * it under the terms of the GNU General Public License as published by
30753 + * the Free Software Foundation; either version 2 of the License, or
30754 + * (at your option) any later version.
30755 + *
30756 + * This program is distributed in the hope that it will be useful,
30757 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30758 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30759 + * GNU General Public License for more details.
30760 + *
30761 + * You should have received a copy of the GNU General Public License
30762 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30763 + */
30764 +
30765 +/*
30766 + * super_block operations
30767 + */
30768 +
30769 +#ifndef __AUFS_SUPER_H__
30770 +#define __AUFS_SUPER_H__
30771 +
30772 +#ifdef __KERNEL__
30773 +
30774 +#include <linux/fs.h>
30775 +#include <linux/kobject.h>
30776 +#include "hbl.h"
30777 +#include "lcnt.h"
30778 +#include "rwsem.h"
30779 +#include "wkq.h"
30780 +
30781 +/* policies to select one among multiple writable branches */
30782 +struct au_wbr_copyup_operations {
30783 +       int (*copyup)(struct dentry *dentry);
30784 +};
30785 +
30786 +#define AuWbr_DIR      1               /* target is a dir */
30787 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30788 +
30789 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30790 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30791 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30792 +
30793 +struct au_wbr_create_operations {
30794 +       int (*create)(struct dentry *dentry, unsigned int flags);
30795 +       int (*init)(struct super_block *sb);
30796 +       int (*fin)(struct super_block *sb);
30797 +};
30798 +
30799 +struct au_wbr_mfs {
30800 +       struct mutex    mfs_lock; /* protect this structure */
30801 +       unsigned long   mfs_jiffy;
30802 +       unsigned long   mfs_expire;
30803 +       aufs_bindex_t   mfs_bindex;
30804 +
30805 +       unsigned long long      mfsrr_bytes;
30806 +       unsigned long long      mfsrr_watermark;
30807 +};
30808 +
30809 +#define AuPlink_NHASH 100
30810 +static inline int au_plink_hash(ino_t ino)
30811 +{
30812 +       return ino % AuPlink_NHASH;
30813 +}
30814 +
30815 +/* File-based Hierarchical Storage Management */
30816 +struct au_fhsm {
30817 +#ifdef CONFIG_AUFS_FHSM
30818 +       /* allow only one process who can receive the notification */
30819 +       spinlock_t              fhsm_spin;
30820 +       pid_t                   fhsm_pid;
30821 +       wait_queue_head_t       fhsm_wqh;
30822 +       atomic_t                fhsm_readable;
30823 +
30824 +       /* these are protected by si_rwsem */
30825 +       unsigned long           fhsm_expire;
30826 +       aufs_bindex_t           fhsm_bottom;
30827 +#endif
30828 +};
30829 +
30830 +struct au_branch;
30831 +struct au_sbinfo {
30832 +       /* nowait tasks in the system-wide workqueue */
30833 +       struct au_nowait_tasks  si_nowait;
30834 +
30835 +       /*
30836 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30837 +        * rwsem for au_sbinfo is necessary.
30838 +        */
30839 +       struct au_rwsem         si_rwsem;
30840 +
30841 +       /*
30842 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30843 +        * remount.
30844 +        */
30845 +       au_lcnt_t               si_ninodes, si_nfiles;
30846 +
30847 +       /* branch management */
30848 +       unsigned int            si_generation;
30849 +
30850 +       /* see AuSi_ flags */
30851 +       unsigned char           au_si_status;
30852 +
30853 +       aufs_bindex_t           si_bbot;
30854 +
30855 +       /* dirty trick to keep br_id plus */
30856 +       unsigned int            si_last_br_id :
30857 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30858 +       struct au_branch        **si_branch;
30859 +
30860 +       /* policy to select a writable branch */
30861 +       unsigned char           si_wbr_copyup;
30862 +       unsigned char           si_wbr_create;
30863 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30864 +       struct au_wbr_create_operations *si_wbr_create_ops;
30865 +
30866 +       /* round robin */
30867 +       atomic_t                si_wbr_rr_next;
30868 +
30869 +       /* most free space */
30870 +       struct au_wbr_mfs       si_wbr_mfs;
30871 +
30872 +       /* File-based Hierarchical Storage Management */
30873 +       struct au_fhsm          si_fhsm;
30874 +
30875 +       /* mount flags */
30876 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30877 +       unsigned int            si_mntflags;
30878 +
30879 +       /* external inode number (bitmap and translation table) */
30880 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30881 +
30882 +       struct file             *si_xib;
30883 +       struct mutex            si_xib_mtx; /* protect xib members */
30884 +       unsigned long           *si_xib_buf;
30885 +       unsigned long           si_xib_last_pindex;
30886 +       int                     si_xib_next_bit;
30887 +
30888 +       unsigned long           si_xino_jiffy;
30889 +       unsigned long           si_xino_expire;
30890 +       /* reserved for future use */
30891 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30892 +
30893 +#ifdef CONFIG_AUFS_EXPORT
30894 +       /* i_generation */
30895 +       /* todo: make xigen file an array to support many inode numbers */
30896 +       struct file             *si_xigen;
30897 +       atomic_t                si_xigen_next;
30898 +#endif
30899 +
30900 +       /* dirty trick to support atomic_open */
30901 +       struct hlist_bl_head    si_aopen;
30902 +
30903 +       /* vdir parameters */
30904 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30905 +       unsigned int            si_rdblk;       /* deblk size */
30906 +       unsigned int            si_rdhash;      /* hash size */
30907 +
30908 +       /*
30909 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30910 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30911 +        * future fsck.aufs or kernel thread will remove them later.
30912 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30913 +        */
30914 +       unsigned int            si_dirwh;
30915 +
30916 +       /* pseudo_link list */
30917 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30918 +       wait_queue_head_t       si_plink_wq;
30919 +       spinlock_t              si_plink_maint_lock;
30920 +       pid_t                   si_plink_maint_pid;
30921 +
30922 +       /* file list */
30923 +       struct hlist_bl_head    si_files;
30924 +
30925 +       /* with/without getattr, brother of sb->s_d_op */
30926 +       const struct inode_operations *si_iop_array;
30927 +
30928 +       /*
30929 +        * sysfs and lifetime management.
30930 +        * this is not a small structure and it may be a waste of memory in case
30931 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30932 +        * but using sysfs is majority.
30933 +        */
30934 +       struct kobject          si_kobj;
30935 +#ifdef CONFIG_DEBUG_FS
30936 +       struct dentry            *si_dbgaufs;
30937 +       struct dentry            *si_dbgaufs_plink;
30938 +       struct dentry            *si_dbgaufs_xib;
30939 +#ifdef CONFIG_AUFS_EXPORT
30940 +       struct dentry            *si_dbgaufs_xigen;
30941 +#endif
30942 +#endif
30943 +
30944 +#ifdef CONFIG_AUFS_SBILIST
30945 +       struct hlist_bl_node    si_list;
30946 +#endif
30947 +
30948 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30949 +       struct super_block      *si_sb;
30950 +};
30951 +
30952 +/* sbinfo status flags */
30953 +/*
30954 + * set true when refresh_dirs() failed at remount time.
30955 + * then try refreshing dirs at access time again.
30956 + * if it is false, refreshing dirs at access time is unnecessary
30957 + */
30958 +#define AuSi_FAILED_REFRESH_DIR        1
30959 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30960 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30961 +
30962 +#ifndef CONFIG_AUFS_FHSM
30963 +#undef AuSi_FHSM
30964 +#define AuSi_FHSM              0
30965 +#endif
30966 +
30967 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30968 +                                          unsigned int flag)
30969 +{
30970 +       AuRwMustAnyLock(&sbi->si_rwsem);
30971 +       return sbi->au_si_status & flag;
30972 +}
30973 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30974 +#define au_fset_si(sbinfo, name) do { \
30975 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30976 +       (sbinfo)->au_si_status |= AuSi_##name; \
30977 +} while (0)
30978 +#define au_fclr_si(sbinfo, name) do { \
30979 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30980 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30981 +} while (0)
30982 +
30983 +/* ---------------------------------------------------------------------- */
30984 +
30985 +/* policy to select one among writable branches */
30986 +#define AuWbrCopyup(sbinfo, ...) \
30987 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30988 +#define AuWbrCreate(sbinfo, ...) \
30989 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30990 +
30991 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30992 +#define AuLock_DW              1               /* write-lock dentry */
30993 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30994 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30995 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30996 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30997 +                                               /* except RENAME_EXCHANGE */
30998 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30999 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
31000 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
31001 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
31002 +#define au_fset_lock(flags, name) \
31003 +       do { (flags) |= AuLock_##name; } while (0)
31004 +#define au_fclr_lock(flags, name) \
31005 +       do { (flags) &= ~AuLock_##name; } while (0)
31006 +
31007 +/* ---------------------------------------------------------------------- */
31008 +
31009 +/* super.c */
31010 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
31011 +
31012 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
31013 +                                          unsigned long long max, void *arg);
31014 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
31015 +                    struct super_block *sb, void *arg);
31016 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
31017 +void au_iarray_free(struct inode **a, unsigned long long max);
31018 +
31019 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
31020 +extern const struct super_operations aufs_sop;
31021 +int au_alloc_root(struct super_block *sb);
31022 +extern struct file_system_type aufs_fs_type;
31023 +
31024 +/* sbinfo.c */
31025 +void au_si_free(struct kobject *kobj);
31026 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
31027 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
31028 +
31029 +unsigned int au_sigen_inc(struct super_block *sb);
31030 +aufs_bindex_t au_new_br_id(struct super_block *sb);
31031 +
31032 +int si_read_lock(struct super_block *sb, int flags);
31033 +int si_write_lock(struct super_block *sb, int flags);
31034 +int aufs_read_lock(struct dentry *dentry, int flags);
31035 +void aufs_read_unlock(struct dentry *dentry, int flags);
31036 +void aufs_write_lock(struct dentry *dentry);
31037 +void aufs_write_unlock(struct dentry *dentry);
31038 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
31039 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
31040 +
31041 +/* wbr_policy.c */
31042 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
31043 +extern struct au_wbr_create_operations au_wbr_create_ops[];
31044 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
31045 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
31046 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
31047 +
31048 +/* mvdown.c */
31049 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
31050 +
31051 +#ifdef CONFIG_AUFS_FHSM
31052 +/* fhsm.c */
31053 +
31054 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
31055 +{
31056 +       pid_t pid;
31057 +
31058 +       spin_lock(&fhsm->fhsm_spin);
31059 +       pid = fhsm->fhsm_pid;
31060 +       spin_unlock(&fhsm->fhsm_spin);
31061 +
31062 +       return pid;
31063 +}
31064 +
31065 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
31066 +void au_fhsm_wrote_all(struct super_block *sb, int force);
31067 +int au_fhsm_fd(struct super_block *sb, int oflags);
31068 +int au_fhsm_br_alloc(struct au_branch *br);
31069 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
31070 +void au_fhsm_fin(struct super_block *sb);
31071 +void au_fhsm_init(struct au_sbinfo *sbinfo);
31072 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
31073 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
31074 +#else
31075 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
31076 +          int force)
31077 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
31078 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
31079 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
31080 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
31081 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
31082 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
31083 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
31084 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
31085 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31086 +#endif
31087 +
31088 +/* ---------------------------------------------------------------------- */
31089 +
31090 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31091 +{
31092 +       return sb->s_fs_info;
31093 +}
31094 +
31095 +/* ---------------------------------------------------------------------- */
31096 +
31097 +#ifdef CONFIG_AUFS_EXPORT
31098 +int au_test_nfsd(void);
31099 +void au_export_init(struct super_block *sb);
31100 +void au_xigen_inc(struct inode *inode);
31101 +int au_xigen_new(struct inode *inode);
31102 +int au_xigen_set(struct super_block *sb, struct path *path);
31103 +void au_xigen_clr(struct super_block *sb);
31104 +
31105 +static inline int au_busy_or_stale(void)
31106 +{
31107 +       if (!au_test_nfsd())
31108 +               return -EBUSY;
31109 +       return -ESTALE;
31110 +}
31111 +#else
31112 +AuStubInt0(au_test_nfsd, void)
31113 +AuStubVoid(au_export_init, struct super_block *sb)
31114 +AuStubVoid(au_xigen_inc, struct inode *inode)
31115 +AuStubInt0(au_xigen_new, struct inode *inode)
31116 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
31117 +AuStubVoid(au_xigen_clr, struct super_block *sb)
31118 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
31119 +#endif /* CONFIG_AUFS_EXPORT */
31120 +
31121 +/* ---------------------------------------------------------------------- */
31122 +
31123 +#ifdef CONFIG_AUFS_SBILIST
31124 +/* module.c */
31125 +extern struct hlist_bl_head au_sbilist;
31126 +
31127 +static inline void au_sbilist_init(void)
31128 +{
31129 +       INIT_HLIST_BL_HEAD(&au_sbilist);
31130 +}
31131 +
31132 +static inline void au_sbilist_add(struct super_block *sb)
31133 +{
31134 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
31135 +}
31136 +
31137 +static inline void au_sbilist_del(struct super_block *sb)
31138 +{
31139 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
31140 +}
31141 +
31142 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31143 +static inline void au_sbilist_lock(void)
31144 +{
31145 +       hlist_bl_lock(&au_sbilist);
31146 +}
31147 +
31148 +static inline void au_sbilist_unlock(void)
31149 +{
31150 +       hlist_bl_unlock(&au_sbilist);
31151 +}
31152 +#define AuGFP_SBILIST  GFP_ATOMIC
31153 +#else
31154 +AuStubVoid(au_sbilist_lock, void)
31155 +AuStubVoid(au_sbilist_unlock, void)
31156 +#define AuGFP_SBILIST  GFP_NOFS
31157 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
31158 +#else
31159 +AuStubVoid(au_sbilist_init, void)
31160 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31161 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31162 +AuStubVoid(au_sbilist_lock, void)
31163 +AuStubVoid(au_sbilist_unlock, void)
31164 +#define AuGFP_SBILIST  GFP_NOFS
31165 +#endif
31166 +
31167 +/* ---------------------------------------------------------------------- */
31168 +
31169 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31170 +{
31171 +       /*
31172 +        * This function is a dynamic '__init' function actually,
31173 +        * so the tiny check for si_rwsem is unnecessary.
31174 +        */
31175 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31176 +#ifdef CONFIG_DEBUG_FS
31177 +       sbinfo->si_dbgaufs = NULL;
31178 +       sbinfo->si_dbgaufs_plink = NULL;
31179 +       sbinfo->si_dbgaufs_xib = NULL;
31180 +#ifdef CONFIG_AUFS_EXPORT
31181 +       sbinfo->si_dbgaufs_xigen = NULL;
31182 +#endif
31183 +#endif
31184 +}
31185 +
31186 +/* ---------------------------------------------------------------------- */
31187 +
31188 +/* current->atomic_flags */
31189 +/* this value should never corrupt the ones defined in linux/sched.h */
31190 +#define PFA_AUFS       0x10
31191 +
31192 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31193 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31194 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31195 +
31196 +static inline int si_pid_test(struct super_block *sb)
31197 +{
31198 +       return !!task_test_aufs(current);
31199 +}
31200 +
31201 +static inline void si_pid_clr(struct super_block *sb)
31202 +{
31203 +       AuDebugOn(!task_test_aufs(current));
31204 +       task_clear_aufs(current);
31205 +}
31206 +
31207 +static inline void si_pid_set(struct super_block *sb)
31208 +{
31209 +       AuDebugOn(task_test_aufs(current));
31210 +       task_set_aufs(current);
31211 +}
31212 +
31213 +/* ---------------------------------------------------------------------- */
31214 +
31215 +/* lock superblock. mainly for entry point functions */
31216 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31217 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31218 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31219 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31220 +/*
31221 +#define __si_read_trylock_nested(sb) \
31222 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31223 +#define __si_write_trylock_nested(sb) \
31224 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31225 +*/
31226 +
31227 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31228 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31229 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31230 +
31231 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31232 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31233 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31234 +
31235 +static inline void si_noflush_read_lock(struct super_block *sb)
31236 +{
31237 +       __si_read_lock(sb);
31238 +       si_pid_set(sb);
31239 +}
31240 +
31241 +static inline int si_noflush_read_trylock(struct super_block *sb)
31242 +{
31243 +       int locked;
31244 +
31245 +       locked = __si_read_trylock(sb);
31246 +       if (locked)
31247 +               si_pid_set(sb);
31248 +       return locked;
31249 +}
31250 +
31251 +static inline void si_noflush_write_lock(struct super_block *sb)
31252 +{
31253 +       __si_write_lock(sb);
31254 +       si_pid_set(sb);
31255 +}
31256 +
31257 +static inline int si_noflush_write_trylock(struct super_block *sb)
31258 +{
31259 +       int locked;
31260 +
31261 +       locked = __si_write_trylock(sb);
31262 +       if (locked)
31263 +               si_pid_set(sb);
31264 +       return locked;
31265 +}
31266 +
31267 +#if 0 /* reserved */
31268 +static inline int si_read_trylock(struct super_block *sb, int flags)
31269 +{
31270 +       if (au_ftest_lock(flags, FLUSH))
31271 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31272 +       return si_noflush_read_trylock(sb);
31273 +}
31274 +#endif
31275 +
31276 +static inline void si_read_unlock(struct super_block *sb)
31277 +{
31278 +       si_pid_clr(sb);
31279 +       __si_read_unlock(sb);
31280 +}
31281 +
31282 +#if 0 /* reserved */
31283 +static inline int si_write_trylock(struct super_block *sb, int flags)
31284 +{
31285 +       if (au_ftest_lock(flags, FLUSH))
31286 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31287 +       return si_noflush_write_trylock(sb);
31288 +}
31289 +#endif
31290 +
31291 +static inline void si_write_unlock(struct super_block *sb)
31292 +{
31293 +       si_pid_clr(sb);
31294 +       __si_write_unlock(sb);
31295 +}
31296 +
31297 +#if 0 /* reserved */
31298 +static inline void si_downgrade_lock(struct super_block *sb)
31299 +{
31300 +       __si_downgrade_lock(sb);
31301 +}
31302 +#endif
31303 +
31304 +/* ---------------------------------------------------------------------- */
31305 +
31306 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31307 +{
31308 +       SiMustAnyLock(sb);
31309 +       return au_sbi(sb)->si_bbot;
31310 +}
31311 +
31312 +static inline unsigned int au_mntflags(struct super_block *sb)
31313 +{
31314 +       SiMustAnyLock(sb);
31315 +       return au_sbi(sb)->si_mntflags;
31316 +}
31317 +
31318 +static inline unsigned int au_sigen(struct super_block *sb)
31319 +{
31320 +       SiMustAnyLock(sb);
31321 +       return au_sbi(sb)->si_generation;
31322 +}
31323 +
31324 +static inline struct au_branch *au_sbr(struct super_block *sb,
31325 +                                      aufs_bindex_t bindex)
31326 +{
31327 +       SiMustAnyLock(sb);
31328 +       return au_sbi(sb)->si_branch[0 + bindex];
31329 +}
31330 +
31331 +static inline loff_t au_xi_maxent(struct super_block *sb)
31332 +{
31333 +       SiMustAnyLock(sb);
31334 +       return au_sbi(sb)->si_ximaxent;
31335 +}
31336 +
31337 +#endif /* __KERNEL__ */
31338 +#endif /* __AUFS_SUPER_H__ */
31339 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31340 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31341 +++ linux/fs/aufs/sysaufs.c     2022-11-05 23:02:18.969222617 +0100
31342 @@ -0,0 +1,94 @@
31343 +// SPDX-License-Identifier: GPL-2.0
31344 +/*
31345 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31346 + *
31347 + * This program is free software; you can redistribute it and/or modify
31348 + * it under the terms of the GNU General Public License as published by
31349 + * the Free Software Foundation; either version 2 of the License, or
31350 + * (at your option) any later version.
31351 + *
31352 + * This program is distributed in the hope that it will be useful,
31353 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31354 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31355 + * GNU General Public License for more details.
31356 + *
31357 + * You should have received a copy of the GNU General Public License
31358 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31359 + */
31360 +
31361 +/*
31362 + * sysfs interface and lifetime management
31363 + * they are necessary regardless sysfs is disabled.
31364 + */
31365 +
31366 +#include <linux/random.h>
31367 +#include "aufs.h"
31368 +
31369 +unsigned long sysaufs_si_mask;
31370 +struct kset *sysaufs_kset;
31371 +
31372 +#define AuSiAttr(_name) { \
31373 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31374 +       .show   = sysaufs_si_##_name,                           \
31375 +}
31376 +
31377 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31378 +struct attribute *sysaufs_si_attrs[] = {
31379 +       &sysaufs_si_attr_xi_path.attr,
31380 +       NULL,
31381 +};
31382 +ATTRIBUTE_GROUPS(sysaufs_si);
31383 +
31384 +static const struct sysfs_ops au_sbi_ops = {
31385 +       .show   = sysaufs_si_show
31386 +};
31387 +
31388 +static struct kobj_type au_sbi_ktype = {
31389 +       .release        = au_si_free,
31390 +       .sysfs_ops      = &au_sbi_ops,
31391 +       .default_groups = sysaufs_si_groups
31392 +};
31393 +
31394 +/* ---------------------------------------------------------------------- */
31395 +
31396 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31397 +{
31398 +       int err;
31399 +
31400 +       sbinfo->si_kobj.kset = sysaufs_kset;
31401 +       /* cf. sysaufs_name() */
31402 +       err = kobject_init_and_add
31403 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31404 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31405 +
31406 +       return err;
31407 +}
31408 +
31409 +void sysaufs_fin(void)
31410 +{
31411 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31412 +       kset_unregister(sysaufs_kset);
31413 +}
31414 +
31415 +int __init sysaufs_init(void)
31416 +{
31417 +       int err;
31418 +
31419 +       do {
31420 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31421 +       } while (!sysaufs_si_mask);
31422 +
31423 +       err = -EINVAL;
31424 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31425 +       if (unlikely(!sysaufs_kset))
31426 +               goto out;
31427 +       err = PTR_ERR(sysaufs_kset);
31428 +       if (IS_ERR(sysaufs_kset))
31429 +               goto out;
31430 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31431 +       if (unlikely(err))
31432 +               kset_unregister(sysaufs_kset);
31433 +
31434 +out:
31435 +       return err;
31436 +}
31437 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31438 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31439 +++ linux/fs/aufs/sysaufs.h     2022-11-05 23:02:18.969222617 +0100
31440 @@ -0,0 +1,102 @@
31441 +/* SPDX-License-Identifier: GPL-2.0 */
31442 +/*
31443 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31444 + *
31445 + * This program is free software; you can redistribute it and/or modify
31446 + * it under the terms of the GNU General Public License as published by
31447 + * the Free Software Foundation; either version 2 of the License, or
31448 + * (at your option) any later version.
31449 + *
31450 + * This program is distributed in the hope that it will be useful,
31451 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31452 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31453 + * GNU General Public License for more details.
31454 + *
31455 + * You should have received a copy of the GNU General Public License
31456 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31457 + */
31458 +
31459 +/*
31460 + * sysfs interface and mount lifetime management
31461 + */
31462 +
31463 +#ifndef __SYSAUFS_H__
31464 +#define __SYSAUFS_H__
31465 +
31466 +#ifdef __KERNEL__
31467 +
31468 +#include <linux/sysfs.h>
31469 +#include "module.h"
31470 +
31471 +struct super_block;
31472 +struct au_sbinfo;
31473 +
31474 +struct sysaufs_si_attr {
31475 +       struct attribute attr;
31476 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31477 +};
31478 +
31479 +/* ---------------------------------------------------------------------- */
31480 +
31481 +/* sysaufs.c */
31482 +extern unsigned long sysaufs_si_mask;
31483 +extern struct kset *sysaufs_kset;
31484 +extern struct attribute *sysaufs_si_attrs[];
31485 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31486 +int __init sysaufs_init(void);
31487 +void sysaufs_fin(void);
31488 +
31489 +/* ---------------------------------------------------------------------- */
31490 +
31491 +/* some people doesn't like to show a pointer in kernel */
31492 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31493 +{
31494 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31495 +}
31496 +
31497 +#define SysaufsSiNamePrefix    "si_"
31498 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31499 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31500 +{
31501 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31502 +                sysaufs_si_id(sbinfo));
31503 +}
31504 +
31505 +struct au_branch;
31506 +#ifdef CONFIG_SYSFS
31507 +/* sysfs.c */
31508 +extern struct attribute_group *sysaufs_attr_group;
31509 +
31510 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31511 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31512 +                        char *buf);
31513 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31514 +#ifdef CONFIG_COMPAT
31515 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31516 +#endif
31517 +
31518 +void sysaufs_br_init(struct au_branch *br);
31519 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31520 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31521 +
31522 +#define sysaufs_brs_init()     do {} while (0)
31523 +
31524 +#else
31525 +#define sysaufs_attr_group     NULL
31526 +
31527 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31528 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31529 +       struct attribute *attr, char *buf)
31530 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31531 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31532 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31533 +
31534 +static inline void sysaufs_brs_init(void)
31535 +{
31536 +       sysaufs_brs = 0;
31537 +}
31538 +
31539 +#endif /* CONFIG_SYSFS */
31540 +
31541 +#endif /* __KERNEL__ */
31542 +#endif /* __SYSAUFS_H__ */
31543 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31544 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31545 +++ linux/fs/aufs/sysfs.c       2022-11-05 23:02:18.969222617 +0100
31546 @@ -0,0 +1,374 @@
31547 +// SPDX-License-Identifier: GPL-2.0
31548 +/*
31549 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31550 + *
31551 + * This program is free software; you can redistribute it and/or modify
31552 + * it under the terms of the GNU General Public License as published by
31553 + * the Free Software Foundation; either version 2 of the License, or
31554 + * (at your option) any later version.
31555 + *
31556 + * This program is distributed in the hope that it will be useful,
31557 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31558 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31559 + * GNU General Public License for more details.
31560 + *
31561 + * You should have received a copy of the GNU General Public License
31562 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31563 + */
31564 +
31565 +/*
31566 + * sysfs interface
31567 + */
31568 +
31569 +#include <linux/compat.h>
31570 +#include <linux/seq_file.h>
31571 +#include "aufs.h"
31572 +
31573 +#ifdef CONFIG_AUFS_FS_MODULE
31574 +/* this entry violates the "one line per file" policy of sysfs */
31575 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31576 +                          char *buf)
31577 +{
31578 +       ssize_t err;
31579 +       static char *conf =
31580 +/* this file is generated at compiling */
31581 +#include "conf.str"
31582 +               ;
31583 +
31584 +       err = snprintf(buf, PAGE_SIZE, conf);
31585 +       if (unlikely(err >= PAGE_SIZE))
31586 +               err = -EFBIG;
31587 +       return err;
31588 +}
31589 +
31590 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31591 +#endif
31592 +
31593 +static struct attribute *au_attr[] = {
31594 +#ifdef CONFIG_AUFS_FS_MODULE
31595 +       &au_config_attr.attr,
31596 +#endif
31597 +       NULL,   /* need to NULL terminate the list of attributes */
31598 +};
31599 +
31600 +static struct attribute_group sysaufs_attr_group_body = {
31601 +       .attrs = au_attr
31602 +};
31603 +
31604 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31605 +
31606 +/* ---------------------------------------------------------------------- */
31607 +
31608 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31609 +{
31610 +       int err;
31611 +
31612 +       SiMustAnyLock(sb);
31613 +
31614 +       err = 0;
31615 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31616 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31617 +               seq_putc(seq, '\n');
31618 +       }
31619 +       return err;
31620 +}
31621 +
31622 +/*
31623 + * the lifetime of branch is independent from the entry under sysfs.
31624 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31625 + * unlinked.
31626 + */
31627 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31628 +                        aufs_bindex_t bindex, int idx)
31629 +{
31630 +       int err;
31631 +       struct path path;
31632 +       struct dentry *root;
31633 +       struct au_branch *br;
31634 +       au_br_perm_str_t perm;
31635 +
31636 +       AuDbg("b%d\n", bindex);
31637 +
31638 +       err = 0;
31639 +       root = sb->s_root;
31640 +       di_read_lock_parent(root, !AuLock_IR);
31641 +       br = au_sbr(sb, bindex);
31642 +
31643 +       switch (idx) {
31644 +       case AuBrSysfs_BR:
31645 +               path.mnt = au_br_mnt(br);
31646 +               path.dentry = au_h_dptr(root, bindex);
31647 +               err = au_seq_path(seq, &path);
31648 +               if (!err) {
31649 +                       au_optstr_br_perm(&perm, br->br_perm);
31650 +                       seq_printf(seq, "=%s\n", perm.a);
31651 +               }
31652 +               break;
31653 +       case AuBrSysfs_BRID:
31654 +               seq_printf(seq, "%d\n", br->br_id);
31655 +               break;
31656 +       }
31657 +       di_read_unlock(root, !AuLock_IR);
31658 +       if (unlikely(err || seq_has_overflowed(seq)))
31659 +               err = -E2BIG;
31660 +
31661 +       return err;
31662 +}
31663 +
31664 +/* ---------------------------------------------------------------------- */
31665 +
31666 +static struct seq_file *au_seq(char *p, ssize_t len)
31667 +{
31668 +       struct seq_file *seq;
31669 +
31670 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31671 +       if (seq) {
31672 +               /* mutex_init(&seq.lock); */
31673 +               seq->buf = p;
31674 +               seq->size = len;
31675 +               return seq; /* success */
31676 +       }
31677 +
31678 +       seq = ERR_PTR(-ENOMEM);
31679 +       return seq;
31680 +}
31681 +
31682 +#define SysaufsBr_PREFIX       "br"
31683 +#define SysaufsBrid_PREFIX     "brid"
31684 +
31685 +/* todo: file size may exceed PAGE_SIZE */
31686 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31687 +                       char *buf)
31688 +{
31689 +       ssize_t err;
31690 +       int idx;
31691 +       long l;
31692 +       aufs_bindex_t bbot;
31693 +       struct au_sbinfo *sbinfo;
31694 +       struct super_block *sb;
31695 +       struct seq_file *seq;
31696 +       char *name;
31697 +       struct attribute **cattr;
31698 +
31699 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31700 +       sb = sbinfo->si_sb;
31701 +
31702 +       /*
31703 +        * prevent a race condition between sysfs and aufs.
31704 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31705 +        * prohibits maintaining the sysfs entries.
31706 +        * hew we acquire read lock after sysfs_get_active_two().
31707 +        * on the other hand, the remount process may maintain the sysfs/aufs
31708 +        * entries after acquiring write lock.
31709 +        * it can cause a deadlock.
31710 +        * simply we gave up processing read here.
31711 +        */
31712 +       err = -EBUSY;
31713 +       if (unlikely(!si_noflush_read_trylock(sb)))
31714 +               goto out;
31715 +
31716 +       seq = au_seq(buf, PAGE_SIZE);
31717 +       err = PTR_ERR(seq);
31718 +       if (IS_ERR(seq))
31719 +               goto out_unlock;
31720 +
31721 +       name = (void *)attr->name;
31722 +       cattr = sysaufs_si_attrs;
31723 +       while (*cattr) {
31724 +               if (!strcmp(name, (*cattr)->name)) {
31725 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31726 +                               ->show(seq, sb);
31727 +                       goto out_seq;
31728 +               }
31729 +               cattr++;
31730 +       }
31731 +
31732 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31733 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31734 +               idx = AuBrSysfs_BRID;
31735 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31736 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31737 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31738 +               idx = AuBrSysfs_BR;
31739 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31740 +       } else
31741 +                 BUG();
31742 +
31743 +       err = kstrtol(name, 10, &l);
31744 +       if (!err) {
31745 +               bbot = au_sbbot(sb);
31746 +               if (l <= bbot)
31747 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31748 +               else
31749 +                       err = -ENOENT;
31750 +       }
31751 +
31752 +out_seq:
31753 +       if (!err) {
31754 +               err = seq->count;
31755 +               /* sysfs limit */
31756 +               if (unlikely(err == PAGE_SIZE))
31757 +                       err = -EFBIG;
31758 +       }
31759 +       au_kfree_rcu(seq);
31760 +out_unlock:
31761 +       si_read_unlock(sb);
31762 +out:
31763 +       return err;
31764 +}
31765 +
31766 +/* ---------------------------------------------------------------------- */
31767 +
31768 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31769 +{
31770 +       int err;
31771 +       int16_t brid;
31772 +       aufs_bindex_t bindex, bbot;
31773 +       size_t sz;
31774 +       char *buf;
31775 +       struct seq_file *seq;
31776 +       struct au_branch *br;
31777 +
31778 +       si_read_lock(sb, AuLock_FLUSH);
31779 +       bbot = au_sbbot(sb);
31780 +       err = bbot + 1;
31781 +       if (!arg)
31782 +               goto out;
31783 +
31784 +       err = -ENOMEM;
31785 +       buf = (void *)__get_free_page(GFP_NOFS);
31786 +       if (unlikely(!buf))
31787 +               goto out;
31788 +
31789 +       seq = au_seq(buf, PAGE_SIZE);
31790 +       err = PTR_ERR(seq);
31791 +       if (IS_ERR(seq))
31792 +               goto out_buf;
31793 +
31794 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31795 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31796 +               /* VERIFY_WRITE */
31797 +               err = !access_ok(arg, sizeof(*arg));
31798 +               if (unlikely(err))
31799 +                       break;
31800 +
31801 +               br = au_sbr(sb, bindex);
31802 +               brid = br->br_id;
31803 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31804 +               err = __put_user(brid, &arg->id);
31805 +               if (unlikely(err))
31806 +                       break;
31807 +
31808 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31809 +               err = __put_user(br->br_perm, &arg->perm);
31810 +               if (unlikely(err))
31811 +                       break;
31812 +
31813 +               err = au_seq_path(seq, &br->br_path);
31814 +               if (unlikely(err))
31815 +                       break;
31816 +               seq_putc(seq, '\0');
31817 +               if (!seq_has_overflowed(seq)) {
31818 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31819 +                       seq->count = 0;
31820 +                       if (unlikely(err))
31821 +                               break;
31822 +               } else {
31823 +                       err = -E2BIG;
31824 +                       goto out_seq;
31825 +               }
31826 +       }
31827 +       if (unlikely(err))
31828 +               err = -EFAULT;
31829 +
31830 +out_seq:
31831 +       au_kfree_rcu(seq);
31832 +out_buf:
31833 +       free_page((unsigned long)buf);
31834 +out:
31835 +       si_read_unlock(sb);
31836 +       return err;
31837 +}
31838 +
31839 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31840 +{
31841 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31842 +}
31843 +
31844 +#ifdef CONFIG_COMPAT
31845 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31846 +{
31847 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31848 +}
31849 +#endif
31850 +
31851 +/* ---------------------------------------------------------------------- */
31852 +
31853 +void sysaufs_br_init(struct au_branch *br)
31854 +{
31855 +       int i;
31856 +       struct au_brsysfs *br_sysfs;
31857 +       struct attribute *attr;
31858 +
31859 +       br_sysfs = br->br_sysfs;
31860 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31861 +               attr = &br_sysfs->attr;
31862 +               sysfs_attr_init(attr);
31863 +               attr->name = br_sysfs->name;
31864 +               attr->mode = 0444;
31865 +               br_sysfs++;
31866 +       }
31867 +}
31868 +
31869 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31870 +{
31871 +       struct au_branch *br;
31872 +       struct kobject *kobj;
31873 +       struct au_brsysfs *br_sysfs;
31874 +       int i;
31875 +       aufs_bindex_t bbot;
31876 +
31877 +       if (!sysaufs_brs)
31878 +               return;
31879 +
31880 +       kobj = &au_sbi(sb)->si_kobj;
31881 +       bbot = au_sbbot(sb);
31882 +       for (; bindex <= bbot; bindex++) {
31883 +               br = au_sbr(sb, bindex);
31884 +               br_sysfs = br->br_sysfs;
31885 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31886 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31887 +                       br_sysfs++;
31888 +               }
31889 +       }
31890 +}
31891 +
31892 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31893 +{
31894 +       int err, i;
31895 +       aufs_bindex_t bbot;
31896 +       struct kobject *kobj;
31897 +       struct au_branch *br;
31898 +       struct au_brsysfs *br_sysfs;
31899 +
31900 +       if (!sysaufs_brs)
31901 +               return;
31902 +
31903 +       kobj = &au_sbi(sb)->si_kobj;
31904 +       bbot = au_sbbot(sb);
31905 +       for (; bindex <= bbot; bindex++) {
31906 +               br = au_sbr(sb, bindex);
31907 +               br_sysfs = br->br_sysfs;
31908 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31909 +                        SysaufsBr_PREFIX "%d", bindex);
31910 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31911 +                        SysaufsBrid_PREFIX "%d", bindex);
31912 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31913 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31914 +                       if (unlikely(err))
31915 +                               pr_warn("failed %s under sysfs(%d)\n",
31916 +                                       br_sysfs->name, err);
31917 +                       br_sysfs++;
31918 +               }
31919 +       }
31920 +}
31921 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31922 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31923 +++ linux/fs/aufs/sysrq.c       2022-11-05 23:02:18.969222617 +0100
31924 @@ -0,0 +1,149 @@
31925 +// SPDX-License-Identifier: GPL-2.0
31926 +/*
31927 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31928 + *
31929 + * This program is free software; you can redistribute it and/or modify
31930 + * it under the terms of the GNU General Public License as published by
31931 + * the Free Software Foundation; either version 2 of the License, or
31932 + * (at your option) any later version.
31933 + *
31934 + * This program is distributed in the hope that it will be useful,
31935 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31936 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31937 + * GNU General Public License for more details.
31938 + *
31939 + * You should have received a copy of the GNU General Public License
31940 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31941 + */
31942 +
31943 +/*
31944 + * magic sysrq handler
31945 + */
31946 +
31947 +/* #include <linux/sysrq.h> */
31948 +#include <linux/writeback.h>
31949 +#include "aufs.h"
31950 +
31951 +/* ---------------------------------------------------------------------- */
31952 +
31953 +static void sysrq_sb(struct super_block *sb)
31954 +{
31955 +       char *plevel;
31956 +       struct au_sbinfo *sbinfo;
31957 +       struct file *file;
31958 +       struct hlist_bl_head *files;
31959 +       struct hlist_bl_node *pos;
31960 +       struct au_finfo *finfo;
31961 +       struct inode *i;
31962 +
31963 +       plevel = au_plevel;
31964 +       au_plevel = KERN_WARNING;
31965 +
31966 +       /* since we define pr_fmt, call printk directly */
31967 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31968 +
31969 +       sbinfo = au_sbi(sb);
31970 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31971 +       pr("superblock\n");
31972 +       au_dpri_sb(sb);
31973 +
31974 +#if 0 /* reserved */
31975 +       do {
31976 +               int err, i, j, ndentry;
31977 +               struct au_dcsub_pages dpages;
31978 +               struct au_dpage *dpage;
31979 +
31980 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31981 +               if (unlikely(err))
31982 +                       break;
31983 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31984 +               if (!err)
31985 +                       for (i = 0; i < dpages.ndpage; i++) {
31986 +                               dpage = dpages.dpages + i;
31987 +                               ndentry = dpage->ndentry;
31988 +                               for (j = 0; j < ndentry; j++)
31989 +                                       au_dpri_dentry(dpage->dentries[j]);
31990 +                       }
31991 +               au_dpages_free(&dpages);
31992 +       } while (0);
31993 +#endif
31994 +
31995 +       pr("isolated inode\n");
31996 +       spin_lock(&sb->s_inode_list_lock);
31997 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31998 +               spin_lock(&i->i_lock);
31999 +               if (hlist_empty(&i->i_dentry))
32000 +                       au_dpri_inode(i);
32001 +               spin_unlock(&i->i_lock);
32002 +       }
32003 +       spin_unlock(&sb->s_inode_list_lock);
32004 +
32005 +       pr("files\n");
32006 +       files = &au_sbi(sb)->si_files;
32007 +       hlist_bl_lock(files);
32008 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
32009 +               umode_t mode;
32010 +
32011 +               file = finfo->fi_file;
32012 +               mode = file_inode(file)->i_mode;
32013 +               if (!special_file(mode))
32014 +                       au_dpri_file(file);
32015 +       }
32016 +       hlist_bl_unlock(files);
32017 +       pr("done\n");
32018 +
32019 +#undef pr
32020 +       au_plevel = plevel;
32021 +}
32022 +
32023 +/* ---------------------------------------------------------------------- */
32024 +
32025 +/* module parameter */
32026 +static char *aufs_sysrq_key = "a";
32027 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
32028 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
32029 +
32030 +static void au_sysrq(int key __maybe_unused)
32031 +{
32032 +       struct au_sbinfo *sbinfo;
32033 +       struct hlist_bl_node *pos;
32034 +
32035 +       lockdep_off();
32036 +       au_sbilist_lock();
32037 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
32038 +               sysrq_sb(sbinfo->si_sb);
32039 +       au_sbilist_unlock();
32040 +       lockdep_on();
32041 +}
32042 +
32043 +static struct sysrq_key_op au_sysrq_op = {
32044 +       .handler        = au_sysrq,
32045 +       .help_msg       = "Aufs",
32046 +       .action_msg     = "Aufs",
32047 +       .enable_mask    = SYSRQ_ENABLE_DUMP
32048 +};
32049 +
32050 +/* ---------------------------------------------------------------------- */
32051 +
32052 +int __init au_sysrq_init(void)
32053 +{
32054 +       int err;
32055 +       char key;
32056 +
32057 +       err = -1;
32058 +       key = *aufs_sysrq_key;
32059 +       if ('a' <= key && key <= 'z')
32060 +               err = register_sysrq_key(key, &au_sysrq_op);
32061 +       if (unlikely(err))
32062 +               pr_err("err %d, sysrq=%c\n", err, key);
32063 +       return err;
32064 +}
32065 +
32066 +void au_sysrq_fin(void)
32067 +{
32068 +       int err;
32069 +
32070 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
32071 +       if (unlikely(err))
32072 +               pr_err("err %d (ignored)\n", err);
32073 +}
32074 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
32075 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
32076 +++ linux/fs/aufs/vdir.c        2022-12-17 09:21:34.799855195 +0100
32077 @@ -0,0 +1,896 @@
32078 +// SPDX-License-Identifier: GPL-2.0
32079 +/*
32080 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32081 + *
32082 + * This program is free software; you can redistribute it and/or modify
32083 + * it under the terms of the GNU General Public License as published by
32084 + * the Free Software Foundation; either version 2 of the License, or
32085 + * (at your option) any later version.
32086 + *
32087 + * This program is distributed in the hope that it will be useful,
32088 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32089 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32090 + * GNU General Public License for more details.
32091 + *
32092 + * You should have received a copy of the GNU General Public License
32093 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32094 + */
32095 +
32096 +/*
32097 + * virtual or vertical directory
32098 + */
32099 +
32100 +#include <linux/iversion.h>
32101 +#include "aufs.h"
32102 +
32103 +static unsigned int calc_size(int nlen)
32104 +{
32105 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
32106 +}
32107 +
32108 +static int set_deblk_end(union au_vdir_deblk_p *p,
32109 +                        union au_vdir_deblk_p *deblk_end)
32110 +{
32111 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32112 +               p->de->de_str.len = 0;
32113 +               /* smp_mb(); */
32114 +               return 0;
32115 +       }
32116 +       return -1; /* error */
32117 +}
32118 +
32119 +/* returns true or false */
32120 +static int is_deblk_end(union au_vdir_deblk_p *p,
32121 +                       union au_vdir_deblk_p *deblk_end)
32122 +{
32123 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
32124 +               return !p->de->de_str.len;
32125 +       return 1;
32126 +}
32127 +
32128 +static unsigned char *last_deblk(struct au_vdir *vdir)
32129 +{
32130 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
32131 +}
32132 +
32133 +/* ---------------------------------------------------------------------- */
32134 +
32135 +/* estimate the appropriate size for name hash table */
32136 +unsigned int au_rdhash_est(loff_t sz)
32137 +{
32138 +       unsigned int n;
32139 +
32140 +       n = UINT_MAX;
32141 +       sz >>= 10;
32142 +       if (sz < n)
32143 +               n = sz;
32144 +       if (sz < AUFS_RDHASH_DEF)
32145 +               n = AUFS_RDHASH_DEF;
32146 +       /* pr_info("n %u\n", n); */
32147 +       return n;
32148 +}
32149 +
32150 +/*
32151 + * the allocated memory has to be freed by
32152 + * au_nhash_wh_free() or au_nhash_de_free().
32153 + */
32154 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32155 +{
32156 +       struct hlist_head *head;
32157 +       unsigned int u;
32158 +       size_t sz;
32159 +
32160 +       sz = sizeof(*nhash->nh_head) * num_hash;
32161 +       head = kmalloc(sz, gfp);
32162 +       if (head) {
32163 +               nhash->nh_num = num_hash;
32164 +               nhash->nh_head = head;
32165 +               for (u = 0; u < num_hash; u++)
32166 +                       INIT_HLIST_HEAD(head++);
32167 +               return 0; /* success */
32168 +       }
32169 +
32170 +       return -ENOMEM;
32171 +}
32172 +
32173 +static void nhash_count(struct hlist_head *head)
32174 +{
32175 +#if 0 /* debugging */
32176 +       unsigned long n;
32177 +       struct hlist_node *pos;
32178 +
32179 +       n = 0;
32180 +       hlist_for_each(pos, head)
32181 +               n++;
32182 +       pr_info("%lu\n", n);
32183 +#endif
32184 +}
32185 +
32186 +static void au_nhash_wh_do_free(struct hlist_head *head)
32187 +{
32188 +       struct au_vdir_wh *pos;
32189 +       struct hlist_node *node;
32190 +
32191 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32192 +               au_kfree_rcu(pos);
32193 +}
32194 +
32195 +static void au_nhash_de_do_free(struct hlist_head *head)
32196 +{
32197 +       struct au_vdir_dehstr *pos;
32198 +       struct hlist_node *node;
32199 +
32200 +       hlist_for_each_entry_safe(pos, node, head, hash)
32201 +               au_cache_free_vdir_dehstr(pos);
32202 +}
32203 +
32204 +static void au_nhash_do_free(struct au_nhash *nhash,
32205 +                            void (*free)(struct hlist_head *head))
32206 +{
32207 +       unsigned int n;
32208 +       struct hlist_head *head;
32209 +
32210 +       n = nhash->nh_num;
32211 +       if (!n)
32212 +               return;
32213 +
32214 +       head = nhash->nh_head;
32215 +       while (n-- > 0) {
32216 +               nhash_count(head);
32217 +               free(head++);
32218 +       }
32219 +       au_kfree_try_rcu(nhash->nh_head);
32220 +}
32221 +
32222 +void au_nhash_wh_free(struct au_nhash *whlist)
32223 +{
32224 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32225 +}
32226 +
32227 +static void au_nhash_de_free(struct au_nhash *delist)
32228 +{
32229 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32230 +}
32231 +
32232 +/* ---------------------------------------------------------------------- */
32233 +
32234 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32235 +                           int limit)
32236 +{
32237 +       int num;
32238 +       unsigned int u, n;
32239 +       struct hlist_head *head;
32240 +       struct au_vdir_wh *pos;
32241 +
32242 +       num = 0;
32243 +       n = whlist->nh_num;
32244 +       head = whlist->nh_head;
32245 +       for (u = 0; u < n; u++, head++)
32246 +               hlist_for_each_entry(pos, head, wh_hash)
32247 +                       if (pos->wh_bindex == btgt && ++num > limit)
32248 +                               return 1;
32249 +       return 0;
32250 +}
32251 +
32252 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32253 +                                      unsigned char *name,
32254 +                                      unsigned int len)
32255 +{
32256 +       unsigned int v;
32257 +       /* const unsigned int magic_bit = 12; */
32258 +
32259 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32260 +
32261 +       v = 0;
32262 +       if (len > 8)
32263 +               len = 8;
32264 +       while (len--)
32265 +               v += *name++;
32266 +       /* v = hash_long(v, magic_bit); */
32267 +       v %= nhash->nh_num;
32268 +       return nhash->nh_head + v;
32269 +}
32270 +
32271 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32272 +                             int nlen)
32273 +{
32274 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32275 +}
32276 +
32277 +/* returns found or not */
32278 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32279 +{
32280 +       struct hlist_head *head;
32281 +       struct au_vdir_wh *pos;
32282 +       struct au_vdir_destr *str;
32283 +
32284 +       head = au_name_hash(whlist, name, nlen);
32285 +       hlist_for_each_entry(pos, head, wh_hash) {
32286 +               str = &pos->wh_str;
32287 +               AuDbg("%.*s\n", str->len, str->name);
32288 +               if (au_nhash_test_name(str, name, nlen))
32289 +                       return 1;
32290 +       }
32291 +       return 0;
32292 +}
32293 +
32294 +/* returns found(true) or not */
32295 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32296 +{
32297 +       struct hlist_head *head;
32298 +       struct au_vdir_dehstr *pos;
32299 +       struct au_vdir_destr *str;
32300 +
32301 +       head = au_name_hash(delist, name, nlen);
32302 +       hlist_for_each_entry(pos, head, hash) {
32303 +               str = pos->str;
32304 +               AuDbg("%.*s\n", str->len, str->name);
32305 +               if (au_nhash_test_name(str, name, nlen))
32306 +                       return 1;
32307 +       }
32308 +       return 0;
32309 +}
32310 +
32311 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32312 +                           unsigned char d_type)
32313 +{
32314 +#ifdef CONFIG_AUFS_SHWH
32315 +       wh->wh_ino = ino;
32316 +       wh->wh_type = d_type;
32317 +#endif
32318 +}
32319 +
32320 +/* ---------------------------------------------------------------------- */
32321 +
32322 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32323 +                      unsigned int d_type, aufs_bindex_t bindex,
32324 +                      unsigned char shwh)
32325 +{
32326 +       int err;
32327 +       struct au_vdir_destr *str;
32328 +       struct au_vdir_wh *wh;
32329 +
32330 +       AuDbg("%.*s\n", nlen, name);
32331 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32332 +
32333 +       err = -ENOMEM;
32334 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32335 +       if (unlikely(!wh))
32336 +               goto out;
32337 +
32338 +       err = 0;
32339 +       wh->wh_bindex = bindex;
32340 +       if (shwh)
32341 +               au_shwh_init_wh(wh, ino, d_type);
32342 +       str = &wh->wh_str;
32343 +       str->len = nlen;
32344 +       memcpy(str->name, name, nlen);
32345 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32346 +       /* smp_mb(); */
32347 +
32348 +out:
32349 +       return err;
32350 +}
32351 +
32352 +static int append_deblk(struct au_vdir *vdir)
32353 +{
32354 +       int err;
32355 +       unsigned long ul;
32356 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32357 +       union au_vdir_deblk_p p, deblk_end;
32358 +       unsigned char **o;
32359 +
32360 +       err = -ENOMEM;
32361 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32362 +                       GFP_NOFS, /*may_shrink*/0);
32363 +       if (unlikely(!o))
32364 +               goto out;
32365 +
32366 +       vdir->vd_deblk = o;
32367 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32368 +       if (p.deblk) {
32369 +               ul = vdir->vd_nblk++;
32370 +               vdir->vd_deblk[ul] = p.deblk;
32371 +               vdir->vd_last.ul = ul;
32372 +               vdir->vd_last.p.deblk = p.deblk;
32373 +               deblk_end.deblk = p.deblk + deblk_sz;
32374 +               err = set_deblk_end(&p, &deblk_end);
32375 +       }
32376 +
32377 +out:
32378 +       return err;
32379 +}
32380 +
32381 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32382 +                    unsigned int d_type, struct au_nhash *delist)
32383 +{
32384 +       int err;
32385 +       unsigned int sz;
32386 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32387 +       union au_vdir_deblk_p p, *room, deblk_end;
32388 +       struct au_vdir_dehstr *dehstr;
32389 +
32390 +       p.deblk = last_deblk(vdir);
32391 +       deblk_end.deblk = p.deblk + deblk_sz;
32392 +       room = &vdir->vd_last.p;
32393 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32394 +                 || !is_deblk_end(room, &deblk_end));
32395 +
32396 +       sz = calc_size(nlen);
32397 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32398 +               err = append_deblk(vdir);
32399 +               if (unlikely(err))
32400 +                       goto out;
32401 +
32402 +               p.deblk = last_deblk(vdir);
32403 +               deblk_end.deblk = p.deblk + deblk_sz;
32404 +               /* smp_mb(); */
32405 +               AuDebugOn(room->deblk != p.deblk);
32406 +       }
32407 +
32408 +       err = -ENOMEM;
32409 +       dehstr = au_cache_alloc_vdir_dehstr();
32410 +       if (unlikely(!dehstr))
32411 +               goto out;
32412 +
32413 +       dehstr->str = &room->de->de_str;
32414 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32415 +       room->de->de_ino = ino;
32416 +       room->de->de_type = d_type;
32417 +       room->de->de_str.len = nlen;
32418 +       memcpy(room->de->de_str.name, name, nlen);
32419 +
32420 +       err = 0;
32421 +       room->deblk += sz;
32422 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32423 +               err = append_deblk(vdir);
32424 +       /* smp_mb(); */
32425 +
32426 +out:
32427 +       return err;
32428 +}
32429 +
32430 +/* ---------------------------------------------------------------------- */
32431 +
32432 +void au_vdir_free(struct au_vdir *vdir)
32433 +{
32434 +       unsigned char **deblk;
32435 +
32436 +       deblk = vdir->vd_deblk;
32437 +       while (vdir->vd_nblk--)
32438 +               au_kfree_try_rcu(*deblk++);
32439 +       au_kfree_try_rcu(vdir->vd_deblk);
32440 +       au_cache_free_vdir(vdir);
32441 +}
32442 +
32443 +static struct au_vdir *alloc_vdir(struct file *file)
32444 +{
32445 +       struct au_vdir *vdir;
32446 +       struct super_block *sb;
32447 +       int err;
32448 +
32449 +       sb = file->f_path.dentry->d_sb;
32450 +       SiMustAnyLock(sb);
32451 +
32452 +       err = -ENOMEM;
32453 +       vdir = au_cache_alloc_vdir();
32454 +       if (unlikely(!vdir))
32455 +               goto out;
32456 +
32457 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32458 +       if (unlikely(!vdir->vd_deblk))
32459 +               goto out_free;
32460 +
32461 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32462 +       if (!vdir->vd_deblk_sz) {
32463 +               /* estimate the appropriate size for deblk */
32464 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32465 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32466 +       }
32467 +       vdir->vd_nblk = 0;
32468 +       vdir->vd_version = 0;
32469 +       vdir->vd_jiffy = 0;
32470 +       err = append_deblk(vdir);
32471 +       if (!err)
32472 +               return vdir; /* success */
32473 +
32474 +       au_kfree_try_rcu(vdir->vd_deblk);
32475 +
32476 +out_free:
32477 +       au_cache_free_vdir(vdir);
32478 +out:
32479 +       vdir = ERR_PTR(err);
32480 +       return vdir;
32481 +}
32482 +
32483 +static int reinit_vdir(struct au_vdir *vdir)
32484 +{
32485 +       int err;
32486 +       union au_vdir_deblk_p p, deblk_end;
32487 +
32488 +       while (vdir->vd_nblk > 1) {
32489 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32490 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32491 +               vdir->vd_nblk--;
32492 +       }
32493 +       p.deblk = vdir->vd_deblk[0];
32494 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32495 +       err = set_deblk_end(&p, &deblk_end);
32496 +       /* keep vd_dblk_sz */
32497 +       vdir->vd_last.ul = 0;
32498 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32499 +       vdir->vd_version = 0;
32500 +       vdir->vd_jiffy = 0;
32501 +       /* smp_mb(); */
32502 +       return err;
32503 +}
32504 +
32505 +/* ---------------------------------------------------------------------- */
32506 +
32507 +#define AuFillVdir_CALLED      1
32508 +#define AuFillVdir_WHABLE      (1 << 1)
32509 +#define AuFillVdir_SHWH                (1 << 2)
32510 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32511 +#define au_fset_fillvdir(flags, name) \
32512 +       do { (flags) |= AuFillVdir_##name; } while (0)
32513 +#define au_fclr_fillvdir(flags, name) \
32514 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32515 +
32516 +#ifndef CONFIG_AUFS_SHWH
32517 +#undef AuFillVdir_SHWH
32518 +#define AuFillVdir_SHWH                0
32519 +#endif
32520 +
32521 +struct fillvdir_arg {
32522 +       struct dir_context      ctx;
32523 +       struct file             *file;
32524 +       struct au_vdir          *vdir;
32525 +       struct au_nhash         delist;
32526 +       struct au_nhash         whlist;
32527 +       aufs_bindex_t           bindex;
32528 +       unsigned int            flags;
32529 +       int                     err;
32530 +};
32531 +
32532 +static bool fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32533 +                   loff_t offset __maybe_unused, u64 h_ino,
32534 +                   unsigned int d_type)
32535 +{
32536 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32537 +       char *name = (void *)__name;
32538 +       struct super_block *sb;
32539 +       ino_t ino;
32540 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32541 +
32542 +       arg->err = 0;
32543 +       sb = arg->file->f_path.dentry->d_sb;
32544 +       au_fset_fillvdir(arg->flags, CALLED);
32545 +       /* smp_mb(); */
32546 +       if (nlen <= AUFS_WH_PFX_LEN
32547 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32548 +               if (test_known(&arg->delist, name, nlen)
32549 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32550 +                       goto out; /* already exists or whiteouted */
32551 +
32552 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32553 +               if (!arg->err) {
32554 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32555 +                               d_type = DT_UNKNOWN;
32556 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32557 +                                            d_type, &arg->delist);
32558 +               }
32559 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32560 +               name += AUFS_WH_PFX_LEN;
32561 +               nlen -= AUFS_WH_PFX_LEN;
32562 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32563 +                       goto out; /* already whiteouted */
32564 +
32565 +               ino = 0; /* just to suppress a warning */
32566 +               if (shwh)
32567 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32568 +                                            &ino);
32569 +               if (!arg->err) {
32570 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32571 +                               d_type = DT_UNKNOWN;
32572 +                       arg->err = au_nhash_append_wh
32573 +                               (&arg->whlist, name, nlen, ino, d_type,
32574 +                                arg->bindex, shwh);
32575 +               }
32576 +       }
32577 +
32578 +out:
32579 +       if (!arg->err)
32580 +               arg->vdir->vd_jiffy = jiffies;
32581 +       /* smp_mb(); */
32582 +       AuTraceErr(arg->err);
32583 +       return !arg->err;
32584 +}
32585 +
32586 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32587 +                         struct au_nhash *whlist, struct au_nhash *delist)
32588 +{
32589 +#ifdef CONFIG_AUFS_SHWH
32590 +       int err;
32591 +       unsigned int nh, u;
32592 +       struct hlist_head *head;
32593 +       struct au_vdir_wh *pos;
32594 +       struct hlist_node *n;
32595 +       char *p, *o;
32596 +       struct au_vdir_destr *destr;
32597 +
32598 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32599 +
32600 +       err = -ENOMEM;
32601 +       o = p = (void *)__get_free_page(GFP_NOFS);
32602 +       if (unlikely(!p))
32603 +               goto out;
32604 +
32605 +       err = 0;
32606 +       nh = whlist->nh_num;
32607 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32608 +       p += AUFS_WH_PFX_LEN;
32609 +       for (u = 0; u < nh; u++) {
32610 +               head = whlist->nh_head + u;
32611 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32612 +                       destr = &pos->wh_str;
32613 +                       memcpy(p, destr->name, destr->len);
32614 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32615 +                                       pos->wh_ino, pos->wh_type, delist);
32616 +                       if (unlikely(err))
32617 +                               break;
32618 +               }
32619 +       }
32620 +
32621 +       free_page((unsigned long)o);
32622 +
32623 +out:
32624 +       AuTraceErr(err);
32625 +       return err;
32626 +#else
32627 +       return 0;
32628 +#endif
32629 +}
32630 +
32631 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32632 +{
32633 +       int err;
32634 +       unsigned int rdhash;
32635 +       loff_t offset;
32636 +       aufs_bindex_t bbot, bindex, btop;
32637 +       unsigned char shwh;
32638 +       struct file *hf, *file;
32639 +       struct super_block *sb;
32640 +
32641 +       file = arg->file;
32642 +       sb = file->f_path.dentry->d_sb;
32643 +       SiMustAnyLock(sb);
32644 +
32645 +       rdhash = au_sbi(sb)->si_rdhash;
32646 +       if (!rdhash)
32647 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32648 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32649 +       if (unlikely(err))
32650 +               goto out;
32651 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32652 +       if (unlikely(err))
32653 +               goto out_delist;
32654 +
32655 +       err = 0;
32656 +       arg->flags = 0;
32657 +       shwh = 0;
32658 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32659 +               shwh = 1;
32660 +               au_fset_fillvdir(arg->flags, SHWH);
32661 +       }
32662 +       btop = au_fbtop(file);
32663 +       bbot = au_fbbot_dir(file);
32664 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32665 +               hf = au_hf_dir(file, bindex);
32666 +               if (!hf)
32667 +                       continue;
32668 +
32669 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32670 +               err = offset;
32671 +               if (unlikely(offset))
32672 +                       break;
32673 +
32674 +               arg->bindex = bindex;
32675 +               au_fclr_fillvdir(arg->flags, WHABLE);
32676 +               if (shwh
32677 +                   || (bindex != bbot
32678 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32679 +                       au_fset_fillvdir(arg->flags, WHABLE);
32680 +               do {
32681 +                       arg->err = 0;
32682 +                       au_fclr_fillvdir(arg->flags, CALLED);
32683 +                       /* smp_mb(); */
32684 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32685 +                       if (err >= 0)
32686 +                               err = arg->err;
32687 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32688 +
32689 +               /*
32690 +                * dir_relax() may be good for concurrency, but aufs should not
32691 +                * use it since it will cause a lockdep problem.
32692 +                */
32693 +       }
32694 +
32695 +       if (!err && shwh)
32696 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32697 +
32698 +       au_nhash_wh_free(&arg->whlist);
32699 +
32700 +out_delist:
32701 +       au_nhash_de_free(&arg->delist);
32702 +out:
32703 +       return err;
32704 +}
32705 +
32706 +static int read_vdir(struct file *file, int may_read)
32707 +{
32708 +       int err;
32709 +       unsigned long expire;
32710 +       unsigned char do_read;
32711 +       struct fillvdir_arg arg = {
32712 +               .ctx = {
32713 +                       .actor = fillvdir
32714 +               }
32715 +       };
32716 +       struct inode *inode;
32717 +       struct au_vdir *vdir, *allocated;
32718 +
32719 +       err = 0;
32720 +       inode = file_inode(file);
32721 +       IMustLock(inode);
32722 +       IiMustWriteLock(inode);
32723 +       SiMustAnyLock(inode->i_sb);
32724 +
32725 +       allocated = NULL;
32726 +       do_read = 0;
32727 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32728 +       vdir = au_ivdir(inode);
32729 +       if (!vdir) {
32730 +               do_read = 1;
32731 +               vdir = alloc_vdir(file);
32732 +               err = PTR_ERR(vdir);
32733 +               if (IS_ERR(vdir))
32734 +                       goto out;
32735 +               err = 0;
32736 +               allocated = vdir;
32737 +       } else if (may_read
32738 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32739 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32740 +               do_read = 1;
32741 +               err = reinit_vdir(vdir);
32742 +               if (unlikely(err))
32743 +                       goto out;
32744 +       }
32745 +
32746 +       if (!do_read)
32747 +               return 0; /* success */
32748 +
32749 +       arg.file = file;
32750 +       arg.vdir = vdir;
32751 +       err = au_do_read_vdir(&arg);
32752 +       if (!err) {
32753 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32754 +               vdir->vd_version = inode_query_iversion(inode);
32755 +               vdir->vd_last.ul = 0;
32756 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32757 +               if (allocated)
32758 +                       au_set_ivdir(inode, allocated);
32759 +       } else if (allocated)
32760 +               au_vdir_free(allocated);
32761 +
32762 +out:
32763 +       return err;
32764 +}
32765 +
32766 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32767 +{
32768 +       int err, rerr;
32769 +       unsigned long ul, n;
32770 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32771 +
32772 +       AuDebugOn(tgt->vd_nblk != 1);
32773 +
32774 +       err = -ENOMEM;
32775 +       if (tgt->vd_nblk < src->vd_nblk) {
32776 +               unsigned char **p;
32777 +
32778 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32779 +                               GFP_NOFS, /*may_shrink*/0);
32780 +               if (unlikely(!p))
32781 +                       goto out;
32782 +               tgt->vd_deblk = p;
32783 +       }
32784 +
32785 +       if (tgt->vd_deblk_sz != deblk_sz) {
32786 +               unsigned char *p;
32787 +
32788 +               tgt->vd_deblk_sz = deblk_sz;
32789 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32790 +                               /*may_shrink*/1);
32791 +               if (unlikely(!p))
32792 +                       goto out;
32793 +               tgt->vd_deblk[0] = p;
32794 +       }
32795 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32796 +       tgt->vd_version = src->vd_version;
32797 +       tgt->vd_jiffy = src->vd_jiffy;
32798 +
32799 +       n = src->vd_nblk;
32800 +       for (ul = 1; ul < n; ul++) {
32801 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32802 +                                           GFP_NOFS);
32803 +               if (unlikely(!tgt->vd_deblk[ul]))
32804 +                       goto out;
32805 +               tgt->vd_nblk++;
32806 +       }
32807 +       tgt->vd_nblk = n;
32808 +       tgt->vd_last.ul = tgt->vd_last.ul;
32809 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32810 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32811 +               - src->vd_deblk[src->vd_last.ul];
32812 +       /* smp_mb(); */
32813 +       return 0; /* success */
32814 +
32815 +out:
32816 +       rerr = reinit_vdir(tgt);
32817 +       BUG_ON(rerr);
32818 +       return err;
32819 +}
32820 +
32821 +int au_vdir_init(struct file *file)
32822 +{
32823 +       int err;
32824 +       struct inode *inode;
32825 +       struct au_vdir *vdir_cache, *allocated;
32826 +
32827 +       /* test file->f_pos here instead of ctx->pos */
32828 +       err = read_vdir(file, !file->f_pos);
32829 +       if (unlikely(err))
32830 +               goto out;
32831 +
32832 +       allocated = NULL;
32833 +       vdir_cache = au_fvdir_cache(file);
32834 +       if (!vdir_cache) {
32835 +               vdir_cache = alloc_vdir(file);
32836 +               err = PTR_ERR(vdir_cache);
32837 +               if (IS_ERR(vdir_cache))
32838 +                       goto out;
32839 +               allocated = vdir_cache;
32840 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32841 +               /* test file->f_pos here instead of ctx->pos */
32842 +               err = reinit_vdir(vdir_cache);
32843 +               if (unlikely(err))
32844 +                       goto out;
32845 +       } else
32846 +               return 0; /* success */
32847 +
32848 +       inode = file_inode(file);
32849 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32850 +       if (!err) {
32851 +               file->f_version = inode_query_iversion(inode);
32852 +               if (allocated)
32853 +                       au_set_fvdir_cache(file, allocated);
32854 +       } else if (allocated)
32855 +               au_vdir_free(allocated);
32856 +
32857 +out:
32858 +       return err;
32859 +}
32860 +
32861 +static loff_t calc_offset(struct au_vdir *vdir)
32862 +{
32863 +       loff_t offset;
32864 +       union au_vdir_deblk_p p;
32865 +
32866 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32867 +       offset = vdir->vd_last.p.deblk - p.deblk;
32868 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32869 +       return offset;
32870 +}
32871 +
32872 +/* returns true or false */
32873 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32874 +{
32875 +       int valid;
32876 +       unsigned int deblk_sz;
32877 +       unsigned long ul, n;
32878 +       loff_t offset;
32879 +       union au_vdir_deblk_p p, deblk_end;
32880 +       struct au_vdir *vdir_cache;
32881 +
32882 +       valid = 1;
32883 +       vdir_cache = au_fvdir_cache(file);
32884 +       offset = calc_offset(vdir_cache);
32885 +       AuDbg("offset %lld\n", offset);
32886 +       if (ctx->pos == offset)
32887 +               goto out;
32888 +
32889 +       vdir_cache->vd_last.ul = 0;
32890 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32891 +       if (!ctx->pos)
32892 +               goto out;
32893 +
32894 +       valid = 0;
32895 +       deblk_sz = vdir_cache->vd_deblk_sz;
32896 +       ul = div64_u64(ctx->pos, deblk_sz);
32897 +       AuDbg("ul %lu\n", ul);
32898 +       if (ul >= vdir_cache->vd_nblk)
32899 +               goto out;
32900 +
32901 +       n = vdir_cache->vd_nblk;
32902 +       for (; ul < n; ul++) {
32903 +               p.deblk = vdir_cache->vd_deblk[ul];
32904 +               deblk_end.deblk = p.deblk + deblk_sz;
32905 +               offset = ul;
32906 +               offset *= deblk_sz;
32907 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32908 +                       unsigned int l;
32909 +
32910 +                       l = calc_size(p.de->de_str.len);
32911 +                       offset += l;
32912 +                       p.deblk += l;
32913 +               }
32914 +               if (!is_deblk_end(&p, &deblk_end)) {
32915 +                       valid = 1;
32916 +                       vdir_cache->vd_last.ul = ul;
32917 +                       vdir_cache->vd_last.p = p;
32918 +                       break;
32919 +               }
32920 +       }
32921 +
32922 +out:
32923 +       /* smp_mb(); */
32924 +       if (!valid)
32925 +               AuDbg("valid %d\n", !valid);
32926 +       return valid;
32927 +}
32928 +
32929 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32930 +{
32931 +       unsigned int l, deblk_sz;
32932 +       union au_vdir_deblk_p deblk_end;
32933 +       struct au_vdir *vdir_cache;
32934 +       struct au_vdir_de *de;
32935 +
32936 +       if (!seek_vdir(file, ctx))
32937 +               return 0;
32938 +
32939 +       vdir_cache = au_fvdir_cache(file);
32940 +       deblk_sz = vdir_cache->vd_deblk_sz;
32941 +       while (1) {
32942 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32943 +               deblk_end.deblk += deblk_sz;
32944 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32945 +                       de = vdir_cache->vd_last.p.de;
32946 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32947 +                             de->de_str.len, de->de_str.name, ctx->pos,
32948 +                             (unsigned long)de->de_ino, de->de_type);
32949 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32950 +                                              de->de_str.len, de->de_ino,
32951 +                                              de->de_type))) {
32952 +                               /* todo: ignore the error caused by udba? */
32953 +                               /* return err; */
32954 +                               return 0;
32955 +                       }
32956 +
32957 +                       l = calc_size(de->de_str.len);
32958 +                       vdir_cache->vd_last.p.deblk += l;
32959 +                       ctx->pos += l;
32960 +               }
32961 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32962 +                       vdir_cache->vd_last.ul++;
32963 +                       vdir_cache->vd_last.p.deblk
32964 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32965 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32966 +                       continue;
32967 +               }
32968 +               break;
32969 +       }
32970 +
32971 +       /* smp_mb(); */
32972 +       return 0;
32973 +}
32974 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32975 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32976 +++ linux/fs/aufs/vfsub.c       2023-08-28 12:34:39.999969465 +0200
32977 @@ -0,0 +1,918 @@
32978 +// SPDX-License-Identifier: GPL-2.0
32979 +/*
32980 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32981 + *
32982 + * This program is free software; you can redistribute it and/or modify
32983 + * it under the terms of the GNU General Public License as published by
32984 + * the Free Software Foundation; either version 2 of the License, or
32985 + * (at your option) any later version.
32986 + *
32987 + * This program is distributed in the hope that it will be useful,
32988 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32989 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32990 + * GNU General Public License for more details.
32991 + *
32992 + * You should have received a copy of the GNU General Public License
32993 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32994 + */
32995 +
32996 +/*
32997 + * sub-routines for VFS
32998 + */
32999 +
33000 +#include <linux/mnt_namespace.h>
33001 +#include <linux/nsproxy.h>
33002 +#include <linux/security.h>
33003 +#include <linux/splice.h>
33004 +#include "aufs.h"
33005 +
33006 +#ifdef CONFIG_AUFS_BR_FUSE
33007 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
33008 +{
33009 +       if (!au_test_fuse(h_sb) || !au_userns)
33010 +               return 0;
33011 +
33012 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
33013 +}
33014 +#endif
33015 +
33016 +int vfsub_sync_filesystem(struct super_block *h_sb)
33017 +{
33018 +       int err;
33019 +
33020 +       lockdep_off();
33021 +       down_read(&h_sb->s_umount);
33022 +       err = sync_filesystem(h_sb);
33023 +       up_read(&h_sb->s_umount);
33024 +       lockdep_on();
33025 +
33026 +       return err;
33027 +}
33028 +
33029 +/* ---------------------------------------------------------------------- */
33030 +
33031 +int vfsub_update_h_iattr(struct path *h_path, int *did)
33032 +{
33033 +       int err;
33034 +       struct kstat st;
33035 +       struct super_block *h_sb;
33036 +
33037 +       /*
33038 +        * Always needs h_path->mnt for LSM or FUSE branch.
33039 +        */
33040 +       AuDebugOn(!h_path->mnt);
33041 +
33042 +       /* for remote fs, leave work for its getattr or d_revalidate */
33043 +       /* for bad i_attr fs, handle them in aufs_getattr() */
33044 +       /* still some fs may acquire i_mutex. we need to skip them */
33045 +       err = 0;
33046 +       if (!did)
33047 +               did = &err;
33048 +       h_sb = h_path->dentry->d_sb;
33049 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
33050 +       if (*did)
33051 +               err = vfsub_getattr(h_path, &st);
33052 +
33053 +       return err;
33054 +}
33055 +
33056 +/* ---------------------------------------------------------------------- */
33057 +
33058 +struct file *vfsub_dentry_open(struct path *path, int flags)
33059 +{
33060 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
33061 +                          current_cred());
33062 +}
33063 +
33064 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
33065 +{
33066 +       struct file *file;
33067 +
33068 +       lockdep_off();
33069 +       file = filp_open(path,
33070 +                        oflags /* | __FMODE_NONOTIFY */,
33071 +                        mode);
33072 +       lockdep_on();
33073 +       if (IS_ERR(file))
33074 +               goto out;
33075 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33076 +
33077 +out:
33078 +       return file;
33079 +}
33080 +
33081 +/*
33082 + * Ideally this function should call VFS:do_last() in order to keep all its
33083 + * checkings. But it is very hard for aufs to regenerate several VFS internal
33084 + * structure such as nameidata. This is a second (or third) best approach.
33085 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
33086 + */
33087 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33088 +                     struct vfsub_aopen_args *args)
33089 +{
33090 +       int err;
33091 +       struct au_branch *br = args->br;
33092 +       struct file *file = args->file;
33093 +       /* copied from linux/fs/namei.c:atomic_open() */
33094 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33095 +
33096 +       IMustLock(dir);
33097 +       AuDebugOn(!dir->i_op->atomic_open);
33098 +
33099 +       err = au_br_test_oflag(args->open_flag, br);
33100 +       if (unlikely(err))
33101 +               goto out;
33102 +
33103 +       au_lcnt_inc(&br->br_nfiles);
33104 +       file->f_path.dentry = DENTRY_NOT_SET;
33105 +       file->f_path.mnt = au_br_mnt(br);
33106 +       AuDbg("%ps\n", dir->i_op->atomic_open);
33107 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
33108 +                                    args->create_mode);
33109 +       if (unlikely(err < 0)) {
33110 +               au_lcnt_dec(&br->br_nfiles);
33111 +               goto out;
33112 +       }
33113 +
33114 +       /* temporary workaround for nfsv4 branch */
33115 +       if (au_test_nfs(dir->i_sb))
33116 +               nfs_mark_for_revalidate(dir);
33117 +
33118 +       if (file->f_mode & FMODE_CREATED)
33119 +               fsnotify_create(dir, dentry);
33120 +       if (!(file->f_mode & FMODE_OPENED)) {
33121 +               au_lcnt_dec(&br->br_nfiles);
33122 +               goto out;
33123 +       }
33124 +
33125 +       /* todo: call VFS:may_open() here */
33126 +       /* todo: ima_file_check() too? */
33127 +       if (!err && (args->open_flag & __FMODE_EXEC))
33128 +               err = deny_write_access(file);
33129 +       if (!err)
33130 +               fsnotify_open(file);
33131 +       else
33132 +               au_lcnt_dec(&br->br_nfiles);
33133 +       /* note that the file is created and still opened */
33134 +
33135 +out:
33136 +       return err;
33137 +}
33138 +
33139 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33140 +{
33141 +       int err;
33142 +
33143 +       err = kern_path(name, flags, path);
33144 +       if (!err && d_is_positive(path->dentry))
33145 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33146 +       return err;
33147 +}
33148 +
33149 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33150 +                                            struct path *ppath, int len)
33151 +{
33152 +       struct path path;
33153 +
33154 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
33155 +       if (IS_ERR(path.dentry))
33156 +               goto out;
33157 +       if (d_is_positive(path.dentry)) {
33158 +               path.mnt = ppath->mnt;
33159 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33160 +       }
33161 +
33162 +out:
33163 +       AuTraceErrPtr(path.dentry);
33164 +       return path.dentry;
33165 +}
33166 +
33167 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33168 +                                   int len)
33169 +{
33170 +       struct path path;
33171 +
33172 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33173 +       IMustLock(d_inode(ppath->dentry));
33174 +
33175 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
33176 +       if (IS_ERR(path.dentry))
33177 +               goto out;
33178 +       if (d_is_positive(path.dentry)) {
33179 +               path.mnt = ppath->mnt;
33180 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33181 +       }
33182 +
33183 +out:
33184 +       AuTraceErrPtr(path.dentry);
33185 +       return path.dentry;
33186 +}
33187 +
33188 +void vfsub_call_lkup_one(void *args)
33189 +{
33190 +       struct vfsub_lkup_one_args *a = args;
33191 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
33192 +}
33193 +
33194 +/* ---------------------------------------------------------------------- */
33195 +
33196 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33197 +                                struct dentry *d2, struct au_hinode *hdir2)
33198 +{
33199 +       struct dentry *d;
33200 +
33201 +       lockdep_off();
33202 +       d = lock_rename(d1, d2);
33203 +       lockdep_on();
33204 +       au_hn_suspend(hdir1);
33205 +       if (hdir1 != hdir2)
33206 +               au_hn_suspend(hdir2);
33207 +
33208 +       return d;
33209 +}
33210 +
33211 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33212 +                        struct dentry *d2, struct au_hinode *hdir2)
33213 +{
33214 +       au_hn_resume(hdir1);
33215 +       if (hdir1 != hdir2)
33216 +               au_hn_resume(hdir2);
33217 +       lockdep_off();
33218 +       unlock_rename(d1, d2);
33219 +       lockdep_on();
33220 +}
33221 +
33222 +/* ---------------------------------------------------------------------- */
33223 +
33224 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33225 +{
33226 +       int err;
33227 +       struct dentry *d;
33228 +       struct mnt_idmap *idmap;
33229 +
33230 +       IMustLock(dir);
33231 +
33232 +       d = path->dentry;
33233 +       path->dentry = d->d_parent;
33234 +       err = security_path_mknod(path, d, mode, 0);
33235 +       path->dentry = d;
33236 +       if (unlikely(err))
33237 +               goto out;
33238 +       idmap = mnt_idmap(path->mnt);
33239 +
33240 +       lockdep_off();
33241 +       err = vfs_create(idmap, dir, path->dentry, mode, want_excl);
33242 +       lockdep_on();
33243 +       if (!err) {
33244 +               struct path tmp = *path;
33245 +               int did;
33246 +
33247 +               vfsub_update_h_iattr(&tmp, &did);
33248 +               if (did) {
33249 +                       tmp.dentry = path->dentry->d_parent;
33250 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33251 +               }
33252 +               /*ignore*/
33253 +       }
33254 +
33255 +out:
33256 +       return err;
33257 +}
33258 +
33259 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33260 +{
33261 +       int err;
33262 +       struct dentry *d;
33263 +       struct mnt_idmap *idmap;
33264 +
33265 +       IMustLock(dir);
33266 +
33267 +       d = path->dentry;
33268 +       path->dentry = d->d_parent;
33269 +       err = security_path_symlink(path, d, symname);
33270 +       path->dentry = d;
33271 +       if (unlikely(err))
33272 +               goto out;
33273 +       idmap = mnt_idmap(path->mnt);
33274 +
33275 +       lockdep_off();
33276 +       err = vfs_symlink(idmap, dir, path->dentry, symname);
33277 +       lockdep_on();
33278 +       if (!err) {
33279 +               struct path tmp = *path;
33280 +               int did;
33281 +
33282 +               vfsub_update_h_iattr(&tmp, &did);
33283 +               if (did) {
33284 +                       tmp.dentry = path->dentry->d_parent;
33285 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33286 +               }
33287 +               /*ignore*/
33288 +       }
33289 +
33290 +out:
33291 +       return err;
33292 +}
33293 +
33294 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33295 +{
33296 +       int err;
33297 +       struct dentry *d;
33298 +       struct mnt_idmap *idmap;
33299 +
33300 +       IMustLock(dir);
33301 +
33302 +       d = path->dentry;
33303 +       path->dentry = d->d_parent;
33304 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33305 +       path->dentry = d;
33306 +       if (unlikely(err))
33307 +               goto out;
33308 +       idmap = mnt_idmap(path->mnt);
33309 +
33310 +       lockdep_off();
33311 +       err = vfs_mknod(idmap, dir, path->dentry, mode, dev);
33312 +       lockdep_on();
33313 +       if (!err) {
33314 +               struct path tmp = *path;
33315 +               int did;
33316 +
33317 +               vfsub_update_h_iattr(&tmp, &did);
33318 +               if (did) {
33319 +                       tmp.dentry = path->dentry->d_parent;
33320 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33321 +               }
33322 +               /*ignore*/
33323 +       }
33324 +
33325 +out:
33326 +       return err;
33327 +}
33328 +
33329 +static int au_test_nlink(struct inode *inode)
33330 +{
33331 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33332 +
33333 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33334 +           || inode->i_nlink < link_max)
33335 +               return 0;
33336 +       return -EMLINK;
33337 +}
33338 +
33339 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33340 +              struct inode **delegated_inode)
33341 +{
33342 +       int err;
33343 +       struct dentry *d;
33344 +       struct mnt_idmap *idmap;
33345 +
33346 +       IMustLock(dir);
33347 +
33348 +       err = au_test_nlink(d_inode(src_dentry));
33349 +       if (unlikely(err))
33350 +               return err;
33351 +
33352 +       /* we don't call may_linkat() */
33353 +       d = path->dentry;
33354 +       path->dentry = d->d_parent;
33355 +       err = security_path_link(src_dentry, path, d);
33356 +       path->dentry = d;
33357 +       if (unlikely(err))
33358 +               goto out;
33359 +       idmap = mnt_idmap(path->mnt);
33360 +
33361 +       lockdep_off();
33362 +       err = vfs_link(src_dentry, idmap, dir, path->dentry, delegated_inode);
33363 +       lockdep_on();
33364 +       if (!err) {
33365 +               struct path tmp = *path;
33366 +               int did;
33367 +
33368 +               /* fuse has different memory inode for the same inumber */
33369 +               vfsub_update_h_iattr(&tmp, &did);
33370 +               if (did) {
33371 +                       tmp.dentry = path->dentry->d_parent;
33372 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33373 +                       tmp.dentry = src_dentry;
33374 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33375 +               }
33376 +               /*ignore*/
33377 +       }
33378 +
33379 +out:
33380 +       return err;
33381 +}
33382 +
33383 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33384 +                struct inode *dir, struct path *path,
33385 +                struct inode **delegated_inode, unsigned int flags)
33386 +{
33387 +       int err;
33388 +       struct renamedata rd;
33389 +       struct path tmp = {
33390 +               .mnt    = path->mnt
33391 +       };
33392 +       struct dentry *d;
33393 +
33394 +       IMustLock(dir);
33395 +       IMustLock(src_dir);
33396 +
33397 +       d = path->dentry;
33398 +       path->dentry = d->d_parent;
33399 +       tmp.dentry = src_dentry->d_parent;
33400 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33401 +       path->dentry = d;
33402 +       if (unlikely(err))
33403 +               goto out;
33404 +
33405 +       rd.old_mnt_idmap = mnt_idmap(path->mnt);
33406 +       rd.old_dir = src_dir;
33407 +       rd.old_dentry = src_dentry;
33408 +       rd.new_mnt_idmap = rd.old_mnt_idmap;
33409 +       rd.new_dir = dir;
33410 +       rd.new_dentry = path->dentry;
33411 +       rd.delegated_inode = delegated_inode;
33412 +       rd.flags = flags;
33413 +       lockdep_off();
33414 +       err = vfs_rename(&rd);
33415 +       lockdep_on();
33416 +       if (!err) {
33417 +               int did;
33418 +
33419 +               tmp.dentry = d->d_parent;
33420 +               vfsub_update_h_iattr(&tmp, &did);
33421 +               if (did) {
33422 +                       tmp.dentry = src_dentry;
33423 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33424 +                       tmp.dentry = src_dentry->d_parent;
33425 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33426 +               }
33427 +               /*ignore*/
33428 +       }
33429 +
33430 +out:
33431 +       return err;
33432 +}
33433 +
33434 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33435 +{
33436 +       int err;
33437 +       struct dentry *d;
33438 +       struct mnt_idmap *idmap;
33439 +
33440 +       IMustLock(dir);
33441 +
33442 +       d = path->dentry;
33443 +       path->dentry = d->d_parent;
33444 +       err = security_path_mkdir(path, d, mode);
33445 +       path->dentry = d;
33446 +       if (unlikely(err))
33447 +               goto out;
33448 +       idmap = mnt_idmap(path->mnt);
33449 +
33450 +       lockdep_off();
33451 +       err = vfs_mkdir(idmap, dir, path->dentry, mode);
33452 +       lockdep_on();
33453 +       if (!err) {
33454 +               struct path tmp = *path;
33455 +               int did;
33456 +
33457 +               vfsub_update_h_iattr(&tmp, &did);
33458 +               if (did) {
33459 +                       tmp.dentry = path->dentry->d_parent;
33460 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33461 +               }
33462 +               /*ignore*/
33463 +       }
33464 +
33465 +out:
33466 +       return err;
33467 +}
33468 +
33469 +int vfsub_rmdir(struct inode *dir, struct path *path)
33470 +{
33471 +       int err;
33472 +       struct dentry *d;
33473 +       struct mnt_idmap *idmap;
33474 +
33475 +       IMustLock(dir);
33476 +
33477 +       d = path->dentry;
33478 +       path->dentry = d->d_parent;
33479 +       err = security_path_rmdir(path, d);
33480 +       path->dentry = d;
33481 +       if (unlikely(err))
33482 +               goto out;
33483 +       idmap = mnt_idmap(path->mnt);
33484 +
33485 +       lockdep_off();
33486 +       err = vfs_rmdir(idmap, dir, path->dentry);
33487 +       lockdep_on();
33488 +       if (!err) {
33489 +               struct path tmp = {
33490 +                       .dentry = path->dentry->d_parent,
33491 +                       .mnt    = path->mnt
33492 +               };
33493 +
33494 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33495 +       }
33496 +
33497 +out:
33498 +       return err;
33499 +}
33500 +
33501 +/* ---------------------------------------------------------------------- */
33502 +
33503 +/* todo: support mmap_sem? */
33504 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33505 +                    loff_t *ppos)
33506 +{
33507 +       ssize_t err;
33508 +
33509 +       lockdep_off();
33510 +       err = vfs_read(file, ubuf, count, ppos);
33511 +       lockdep_on();
33512 +       if (err >= 0)
33513 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33514 +       return err;
33515 +}
33516 +
33517 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33518 +                    loff_t *ppos)
33519 +{
33520 +       ssize_t err;
33521 +
33522 +       lockdep_off();
33523 +       err = kernel_read(file, kbuf, count, ppos);
33524 +       lockdep_on();
33525 +       AuTraceErr(err);
33526 +       if (err >= 0)
33527 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33528 +       return err;
33529 +}
33530 +
33531 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33532 +                     loff_t *ppos)
33533 +{
33534 +       ssize_t err;
33535 +
33536 +       lockdep_off();
33537 +       err = vfs_write(file, ubuf, count, ppos);
33538 +       lockdep_on();
33539 +       if (err >= 0)
33540 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33541 +       return err;
33542 +}
33543 +
33544 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33545 +{
33546 +       ssize_t err;
33547 +
33548 +       lockdep_off();
33549 +       err = kernel_write(file, kbuf, count, ppos);
33550 +       lockdep_on();
33551 +       if (err >= 0)
33552 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33553 +       return err;
33554 +}
33555 +
33556 +int vfsub_flush(struct file *file, fl_owner_t id)
33557 +{
33558 +       int err;
33559 +
33560 +       err = 0;
33561 +       if (file->f_op->flush) {
33562 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33563 +                       err = file->f_op->flush(file, id);
33564 +               else {
33565 +                       lockdep_off();
33566 +                       err = file->f_op->flush(file, id);
33567 +                       lockdep_on();
33568 +               }
33569 +               if (!err)
33570 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33571 +               /*ignore*/
33572 +       }
33573 +       return err;
33574 +}
33575 +
33576 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33577 +{
33578 +       int err;
33579 +
33580 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33581 +
33582 +       lockdep_off();
33583 +       err = iterate_dir(file, ctx);
33584 +       lockdep_on();
33585 +       if (err >= 0)
33586 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33587 +
33588 +       return err;
33589 +}
33590 +
33591 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33592 +                    struct pipe_inode_info *pipe, size_t len,
33593 +                    unsigned int flags)
33594 +{
33595 +       long err;
33596 +
33597 +       lockdep_off();
33598 +       err = do_splice_to(in, ppos, pipe, len, flags);
33599 +       lockdep_on();
33600 +       file_accessed(in);
33601 +       if (err >= 0)
33602 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33603 +       return err;
33604 +}
33605 +
33606 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33607 +                      loff_t *ppos, size_t len, unsigned int flags)
33608 +{
33609 +       long err;
33610 +
33611 +       lockdep_off();
33612 +       err = do_splice_from(pipe, out, ppos, len, flags);
33613 +       lockdep_on();
33614 +       if (err >= 0)
33615 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33616 +       return err;
33617 +}
33618 +
33619 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33620 +{
33621 +       int err;
33622 +
33623 +       /* file can be NULL */
33624 +       lockdep_off();
33625 +       err = vfs_fsync(file, datasync);
33626 +       lockdep_on();
33627 +       if (!err) {
33628 +               if (!path) {
33629 +                       AuDebugOn(!file);
33630 +                       path = &file->f_path;
33631 +               }
33632 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33633 +       }
33634 +       return err;
33635 +}
33636 +
33637 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33638 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33639 +               struct file *h_file)
33640 +{
33641 +       int err;
33642 +       struct inode *h_inode;
33643 +       struct super_block *h_sb;
33644 +       struct mnt_idmap *h_idmap;
33645 +
33646 +       if (!h_file) {
33647 +               err = vfsub_truncate(h_path, length);
33648 +               goto out;
33649 +       }
33650 +
33651 +       h_inode = d_inode(h_path->dentry);
33652 +       h_sb = h_inode->i_sb;
33653 +       lockdep_off();
33654 +       sb_start_write(h_sb);
33655 +       lockdep_on();
33656 +       err = security_file_truncate(h_file);
33657 +       if (!err) {
33658 +               h_idmap = mnt_idmap(h_path->mnt);
33659 +               lockdep_off();
33660 +               err = do_truncate(h_idmap, h_path->dentry, length, attr,
33661 +                                 h_file);
33662 +               lockdep_on();
33663 +       }
33664 +       lockdep_off();
33665 +       sb_end_write(h_sb);
33666 +       lockdep_on();
33667 +
33668 +out:
33669 +       return err;
33670 +}
33671 +
33672 +/* ---------------------------------------------------------------------- */
33673 +
33674 +struct au_vfsub_mkdir_args {
33675 +       int *errp;
33676 +       struct inode *dir;
33677 +       struct path *path;
33678 +       int mode;
33679 +};
33680 +
33681 +static void au_call_vfsub_mkdir(void *args)
33682 +{
33683 +       struct au_vfsub_mkdir_args *a = args;
33684 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33685 +}
33686 +
33687 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33688 +{
33689 +       int err, do_sio, wkq_err;
33690 +       struct mnt_idmap *idmap;
33691 +
33692 +       idmap = mnt_idmap(path->mnt);
33693 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33694 +       if (!do_sio) {
33695 +               lockdep_off();
33696 +               err = vfsub_mkdir(dir, path, mode);
33697 +               lockdep_on();
33698 +       } else {
33699 +               struct au_vfsub_mkdir_args args = {
33700 +                       .errp   = &err,
33701 +                       .dir    = dir,
33702 +                       .path   = path,
33703 +                       .mode   = mode
33704 +               };
33705 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33706 +               if (unlikely(wkq_err))
33707 +                       err = wkq_err;
33708 +       }
33709 +
33710 +       return err;
33711 +}
33712 +
33713 +struct au_vfsub_rmdir_args {
33714 +       int *errp;
33715 +       struct inode *dir;
33716 +       struct path *path;
33717 +};
33718 +
33719 +static void au_call_vfsub_rmdir(void *args)
33720 +{
33721 +       struct au_vfsub_rmdir_args *a = args;
33722 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33723 +}
33724 +
33725 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33726 +{
33727 +       int err, do_sio, wkq_err;
33728 +       struct mnt_idmap *idmap;
33729 +
33730 +       idmap = mnt_idmap(path->mnt);
33731 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33732 +       if (!do_sio) {
33733 +               lockdep_off();
33734 +               err = vfsub_rmdir(dir, path);
33735 +               lockdep_on();
33736 +       } else {
33737 +               struct au_vfsub_rmdir_args args = {
33738 +                       .errp   = &err,
33739 +                       .dir    = dir,
33740 +                       .path   = path
33741 +               };
33742 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33743 +               if (unlikely(wkq_err))
33744 +                       err = wkq_err;
33745 +       }
33746 +
33747 +       return err;
33748 +}
33749 +
33750 +/* ---------------------------------------------------------------------- */
33751 +
33752 +struct notify_change_args {
33753 +       int *errp;
33754 +       struct path *path;
33755 +       struct iattr *ia;
33756 +       struct inode **delegated_inode;
33757 +};
33758 +
33759 +static void call_notify_change(void *args)
33760 +{
33761 +       struct notify_change_args *a = args;
33762 +       struct inode *h_inode;
33763 +       struct mnt_idmap *idmap;
33764 +
33765 +       h_inode = d_inode(a->path->dentry);
33766 +       IMustLock(h_inode);
33767 +
33768 +       *a->errp = -EPERM;
33769 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33770 +               idmap = mnt_idmap(a->path->mnt);
33771 +               lockdep_off();
33772 +               *a->errp = notify_change(idmap, a->path->dentry, a->ia,
33773 +                                        a->delegated_inode);
33774 +               lockdep_on();
33775 +               if (!*a->errp)
33776 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33777 +       }
33778 +       AuTraceErr(*a->errp);
33779 +}
33780 +
33781 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33782 +                       struct inode **delegated_inode)
33783 +{
33784 +       int err;
33785 +       struct notify_change_args args = {
33786 +               .errp                   = &err,
33787 +               .path                   = path,
33788 +               .ia                     = ia,
33789 +               .delegated_inode        = delegated_inode
33790 +       };
33791 +
33792 +       call_notify_change(&args);
33793 +
33794 +       return err;
33795 +}
33796 +
33797 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33798 +                           struct inode **delegated_inode)
33799 +{
33800 +       int err, wkq_err;
33801 +       struct notify_change_args args = {
33802 +               .errp                   = &err,
33803 +               .path                   = path,
33804 +               .ia                     = ia,
33805 +               .delegated_inode        = delegated_inode
33806 +       };
33807 +
33808 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33809 +       if (unlikely(wkq_err))
33810 +               err = wkq_err;
33811 +
33812 +       return err;
33813 +}
33814 +
33815 +/* ---------------------------------------------------------------------- */
33816 +
33817 +struct unlink_args {
33818 +       int *errp;
33819 +       struct inode *dir;
33820 +       struct path *path;
33821 +       struct inode **delegated_inode;
33822 +};
33823 +
33824 +static void call_unlink(void *args)
33825 +{
33826 +       struct unlink_args *a = args;
33827 +       struct dentry *d = a->path->dentry;
33828 +       struct inode *h_inode;
33829 +       struct mnt_idmap *idmap;
33830 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33831 +                                     && au_dcount(d) == 1);
33832 +
33833 +       IMustLock(a->dir);
33834 +
33835 +       a->path->dentry = d->d_parent;
33836 +       *a->errp = security_path_unlink(a->path, d);
33837 +       a->path->dentry = d;
33838 +       if (unlikely(*a->errp))
33839 +               return;
33840 +
33841 +       if (!stop_sillyrename)
33842 +               dget(d);
33843 +       h_inode = NULL;
33844 +       if (d_is_positive(d)) {
33845 +               h_inode = d_inode(d);
33846 +               ihold(h_inode);
33847 +       }
33848 +
33849 +       idmap = mnt_idmap(a->path->mnt);
33850 +       lockdep_off();
33851 +       *a->errp = vfs_unlink(idmap, a->dir, d, a->delegated_inode);
33852 +       lockdep_on();
33853 +       if (!*a->errp) {
33854 +               struct path tmp = {
33855 +                       .dentry = d->d_parent,
33856 +                       .mnt    = a->path->mnt
33857 +               };
33858 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33859 +       }
33860 +
33861 +       if (!stop_sillyrename)
33862 +               dput(d);
33863 +       if (h_inode)
33864 +               iput(h_inode);
33865 +
33866 +       AuTraceErr(*a->errp);
33867 +}
33868 +
33869 +/*
33870 + * @dir: must be locked.
33871 + * @dentry: target dentry.
33872 + */
33873 +int vfsub_unlink(struct inode *dir, struct path *path,
33874 +                struct inode **delegated_inode, int force)
33875 +{
33876 +       int err;
33877 +       struct unlink_args args = {
33878 +               .errp                   = &err,
33879 +               .dir                    = dir,
33880 +               .path                   = path,
33881 +               .delegated_inode        = delegated_inode
33882 +       };
33883 +
33884 +       if (!force)
33885 +               call_unlink(&args);
33886 +       else {
33887 +               int wkq_err;
33888 +
33889 +               wkq_err = au_wkq_wait(call_unlink, &args);
33890 +               if (unlikely(wkq_err))
33891 +                       err = wkq_err;
33892 +       }
33893 +
33894 +       return err;
33895 +}
33896 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33897 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33898 +++ linux/fs/aufs/vfsub.h       2023-08-28 12:34:39.999969465 +0200
33899 @@ -0,0 +1,390 @@
33900 +/* SPDX-License-Identifier: GPL-2.0 */
33901 +/*
33902 + * Copyright (C) 2005-2022 Junjiro R. Okajima
33903 + *
33904 + * This program is free software; you can redistribute it and/or modify
33905 + * it under the terms of the GNU General Public License as published by
33906 + * the Free Software Foundation; either version 2 of the License, or
33907 + * (at your option) any later version.
33908 + *
33909 + * This program is distributed in the hope that it will be useful,
33910 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33911 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33912 + * GNU General Public License for more details.
33913 + *
33914 + * You should have received a copy of the GNU General Public License
33915 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33916 + */
33917 +
33918 +/*
33919 + * sub-routines for VFS
33920 + */
33921 +
33922 +#ifndef __AUFS_VFSUB_H__
33923 +#define __AUFS_VFSUB_H__
33924 +
33925 +#ifdef __KERNEL__
33926 +
33927 +#include <linux/fs.h>
33928 +#include <linux/mount.h>
33929 +#include <linux/posix_acl.h>
33930 +#include <linux/xattr.h>
33931 +#include "debug.h"
33932 +
33933 +/* copied from linux/fs/internal.h */
33934 +/* todo: BAD approach!! */
33935 +extern void __mnt_drop_write(struct vfsmount *);
33936 +extern struct file *alloc_empty_file(int, const struct cred *);
33937 +
33938 +/* ---------------------------------------------------------------------- */
33939 +
33940 +/* lock subclass for lower inode */
33941 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33942 +/* reduce? gave up. */
33943 +enum {
33944 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33945 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33946 +       AuLsc_I_PARENT2,        /* copyup dirs */
33947 +       AuLsc_I_PARENT3,        /* copyup wh */
33948 +       AuLsc_I_CHILD,
33949 +       AuLsc_I_CHILD2,
33950 +       AuLsc_I_End
33951 +};
33952 +
33953 +/* to debug easier, do not make them inlined functions */
33954 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33955 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33956 +
33957 +/* ---------------------------------------------------------------------- */
33958 +
33959 +static inline void vfsub_drop_nlink(struct inode *inode)
33960 +{
33961 +       AuDebugOn(!inode->i_nlink);
33962 +       drop_nlink(inode);
33963 +}
33964 +
33965 +static inline void vfsub_dead_dir(struct inode *inode)
33966 +{
33967 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33968 +       inode->i_flags |= S_DEAD;
33969 +       clear_nlink(inode);
33970 +}
33971 +
33972 +static inline int vfsub_native_ro(struct inode *inode)
33973 +{
33974 +       return sb_rdonly(inode->i_sb)
33975 +               || IS_RDONLY(inode)
33976 +               /* || IS_APPEND(inode) */
33977 +               || IS_IMMUTABLE(inode);
33978 +}
33979 +
33980 +#ifdef CONFIG_AUFS_BR_FUSE
33981 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33982 +#else
33983 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33984 +#endif
33985 +
33986 +int vfsub_sync_filesystem(struct super_block *h_sb);
33987 +
33988 +/* ---------------------------------------------------------------------- */
33989 +
33990 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33991 +struct file *vfsub_dentry_open(struct path *path, int flags);
33992 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33993 +struct au_branch;
33994 +struct vfsub_aopen_args {
33995 +       struct file             *file;
33996 +       unsigned int            open_flag;
33997 +       umode_t                 create_mode;
33998 +       struct au_branch        *br;
33999 +};
34000 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
34001 +                     struct vfsub_aopen_args *args);
34002 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
34003 +
34004 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
34005 +                                            struct path *ppath, int len);
34006 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
34007 +                                   int len);
34008 +
34009 +struct vfsub_lkup_one_args {
34010 +       struct dentry **errp;
34011 +       struct qstr *name;
34012 +       struct path *ppath;
34013 +};
34014 +
34015 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
34016 +                                           struct path *ppath)
34017 +{
34018 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
34019 +}
34020 +
34021 +void vfsub_call_lkup_one(void *args);
34022 +
34023 +/* ---------------------------------------------------------------------- */
34024 +
34025 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
34026 +{
34027 +       int err;
34028 +
34029 +       lockdep_off();
34030 +       err = mnt_want_write(mnt);
34031 +       lockdep_on();
34032 +       return err;
34033 +}
34034 +
34035 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
34036 +{
34037 +       lockdep_off();
34038 +       mnt_drop_write(mnt);
34039 +       lockdep_on();
34040 +}
34041 +
34042 +#if 0 /* reserved */
34043 +static inline void vfsub_mnt_drop_write_file(struct file *file)
34044 +{
34045 +       lockdep_off();
34046 +       mnt_drop_write_file(file);
34047 +       lockdep_on();
34048 +}
34049 +#endif
34050 +
34051 +/* ---------------------------------------------------------------------- */
34052 +
34053 +struct au_hinode;
34054 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
34055 +                                struct dentry *d2, struct au_hinode *hdir2);
34056 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
34057 +                        struct dentry *d2, struct au_hinode *hdir2);
34058 +
34059 +int vfsub_create(struct inode *dir, struct path *path, int mode,
34060 +                bool want_excl);
34061 +int vfsub_symlink(struct inode *dir, struct path *path,
34062 +                 const char *symname);
34063 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
34064 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
34065 +              struct path *path, struct inode **delegated_inode);
34066 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
34067 +                struct inode *hdir, struct path *path,
34068 +                struct inode **delegated_inode, unsigned int flags);
34069 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
34070 +int vfsub_rmdir(struct inode *dir, struct path *path);
34071 +
34072 +/* ---------------------------------------------------------------------- */
34073 +
34074 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
34075 +                    loff_t *ppos);
34076 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
34077 +                       loff_t *ppos);
34078 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
34079 +                     loff_t *ppos);
34080 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
34081 +                     loff_t *ppos);
34082 +int vfsub_flush(struct file *file, fl_owner_t id);
34083 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
34084 +
34085 +static inline loff_t vfsub_f_size_read(struct file *file)
34086 +{
34087 +       return i_size_read(file_inode(file));
34088 +}
34089 +
34090 +static inline unsigned int vfsub_file_flags(struct file *file)
34091 +{
34092 +       unsigned int flags;
34093 +
34094 +       spin_lock(&file->f_lock);
34095 +       flags = file->f_flags;
34096 +       spin_unlock(&file->f_lock);
34097 +
34098 +       return flags;
34099 +}
34100 +
34101 +static inline int vfsub_file_execed(struct file *file)
34102 +{
34103 +       /* todo: direct access f_flags */
34104 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34105 +}
34106 +
34107 +#if 0 /* reserved */
34108 +static inline void vfsub_file_accessed(struct file *h_file)
34109 +{
34110 +       file_accessed(h_file);
34111 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34112 +}
34113 +#endif
34114 +
34115 +#if 0 /* reserved */
34116 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34117 +                                    struct dentry *h_dentry)
34118 +{
34119 +       struct path h_path = {
34120 +               .dentry = h_dentry,
34121 +               .mnt    = h_mnt
34122 +       };
34123 +       touch_atime(&h_path);
34124 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34125 +}
34126 +#endif
34127 +
34128 +static inline int vfsub_update_time(struct inode *h_inode,
34129 +                                   struct timespec64 *ts, int flags)
34130 +{
34131 +       return inode_update_time(h_inode, ts, flags);
34132 +       /* no vfsub_update_h_iattr() since we don't have struct path */
34133 +}
34134 +
34135 +#ifdef CONFIG_FS_POSIX_ACL
34136 +static inline int vfsub_acl_chmod(struct mnt_idmap *h_idmap,
34137 +                                 struct dentry *h_dentry, umode_t h_mode)
34138 +{
34139 +       int err;
34140 +
34141 +       err = posix_acl_chmod(h_idmap, h_dentry, h_mode);
34142 +       if (err == -EOPNOTSUPP)
34143 +               err = 0;
34144 +       return err;
34145 +}
34146 +#else
34147 +AuStubInt0(vfsub_acl_chmod, struct mnt_idmap *h_idmap,
34148 +          struct dentry *h_dentry, umode_t h_mode);
34149 +#endif
34150 +
34151 +long vfsub_splice_to(struct file *in, loff_t *ppos,
34152 +                    struct pipe_inode_info *pipe, size_t len,
34153 +                    unsigned int flags);
34154 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34155 +                      loff_t *ppos, size_t len, unsigned int flags);
34156 +
34157 +static inline long vfsub_truncate(struct path *path, loff_t length)
34158 +{
34159 +       long err;
34160 +
34161 +       lockdep_off();
34162 +       err = vfs_truncate(path, length);
34163 +       lockdep_on();
34164 +       return err;
34165 +}
34166 +
34167 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34168 +               struct file *h_file);
34169 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34170 +
34171 +/*
34172 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34173 + * ioctl.
34174 + */
34175 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34176 +                                           loff_t len)
34177 +{
34178 +       loff_t err;
34179 +
34180 +       lockdep_off();
34181 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34182 +       lockdep_on();
34183 +
34184 +       return err;
34185 +}
34186 +
34187 +/* copy_file_range(2) is a systemcall */
34188 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34189 +                                           struct file *dst, loff_t dst_pos,
34190 +                                           size_t len, unsigned int flags)
34191 +{
34192 +       ssize_t ssz;
34193 +
34194 +       lockdep_off();
34195 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34196 +       lockdep_on();
34197 +
34198 +       return ssz;
34199 +}
34200 +
34201 +/* ---------------------------------------------------------------------- */
34202 +
34203 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34204 +{
34205 +       loff_t err;
34206 +
34207 +       lockdep_off();
34208 +       err = vfs_llseek(file, offset, origin);
34209 +       lockdep_on();
34210 +       return err;
34211 +}
34212 +
34213 +/* ---------------------------------------------------------------------- */
34214 +
34215 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34216 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34217 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34218 +                           struct inode **delegated_inode);
34219 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34220 +                       struct inode **delegated_inode);
34221 +int vfsub_unlink(struct inode *dir, struct path *path,
34222 +                struct inode **delegated_inode, int force);
34223 +
34224 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34225 +{
34226 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34227 +}
34228 +
34229 +/* ---------------------------------------------------------------------- */
34230 +
34231 +static inline int vfsub_setxattr(struct mnt_idmap *idmap,
34232 +                                struct dentry *dentry, const char *name,
34233 +                                const void *value, size_t size, int flags)
34234 +{
34235 +       int err;
34236 +
34237 +       lockdep_off();
34238 +       err = vfs_setxattr(idmap, dentry, name, value, size, flags);
34239 +       lockdep_on();
34240 +
34241 +       return err;
34242 +}
34243 +
34244 +static inline int vfsub_removexattr(struct mnt_idmap *idmap,
34245 +                                   struct dentry *dentry, const char *name)
34246 +{
34247 +       int err;
34248 +
34249 +       lockdep_off();
34250 +       err = vfs_removexattr(idmap, dentry, name);
34251 +       lockdep_on();
34252 +
34253 +       return err;
34254 +}
34255 +
34256 +#ifdef CONFIG_FS_POSIX_ACL
34257 +static inline int vfsub_set_acl(struct mnt_idmap *idmap,
34258 +                               struct dentry *dentry, const char *name,
34259 +                               struct posix_acl *acl)
34260 +{
34261 +       int err;
34262 +
34263 +       lockdep_off();
34264 +       err = vfs_set_acl(idmap, dentry, name, acl);
34265 +       lockdep_on();
34266 +
34267 +       return err;
34268 +}
34269 +
34270 +static inline int vfsub_remove_acl(struct mnt_idmap *idmap,
34271 +                                  struct dentry *dentry, const char *name)
34272 +{
34273 +       int err;
34274 +
34275 +       lockdep_off();
34276 +       err = vfs_remove_acl(idmap, dentry, name);
34277 +       lockdep_on();
34278 +
34279 +       return err;
34280 +}
34281 +#else
34282 +AuStubInt0(vfsub_set_acl, struct mnt_idmap *idmap, struct dentry *dentry,
34283 +          const char *name, struct posix_acl *acl);
34284 +AuStubInt0(vfsub_remove_acl, struct mnt_idmap *idmap,
34285 +          struct dentry *dentry, const char *name);
34286 +#endif
34287 +
34288 +#endif /* __KERNEL__ */
34289 +#endif /* __AUFS_VFSUB_H__ */
34290 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34291 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34292 +++ linux/fs/aufs/wbr_policy.c  2022-11-05 23:02:18.969222617 +0100
34293 @@ -0,0 +1,830 @@
34294 +// SPDX-License-Identifier: GPL-2.0
34295 +/*
34296 + * Copyright (C) 2005-2022 Junjiro R. Okajima
34297 + *
34298 + * This program is free software; you can redistribute it and/or modify
34299 + * it under the terms of the GNU General Public License as published by
34300 + * the Free Software Foundation; either version 2 of the License, or
34301 + * (at your option) any later version.
34302 + *
34303 + * This program is distributed in the hope that it will be useful,
34304 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34305 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34306 + * GNU General Public License for more details.
34307 + *
34308 + * You should have received a copy of the GNU General Public License
34309 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34310 + */
34311 +
34312 +/*
34313 + * policies for selecting one among multiple writable branches
34314 + */
34315 +
34316 +#include <linux/statfs.h>
34317 +#include "aufs.h"
34318 +
34319 +/* subset of cpup_attr() */
34320 +static noinline_for_stack
34321 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34322 +{
34323 +       int err, sbits;
34324 +       struct iattr ia;
34325 +       struct inode *h_isrc;
34326 +
34327 +       h_isrc = d_inode(h_src);
34328 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34329 +       ia.ia_mode = h_isrc->i_mode;
34330 +       ia.ia_uid = h_isrc->i_uid;
34331 +       ia.ia_gid = h_isrc->i_gid;
34332 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34333 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34334 +       /* no delegation since it is just created */
34335 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34336 +
34337 +       /* is this nfs only? */
34338 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34339 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34340 +               ia.ia_mode = h_isrc->i_mode;
34341 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34342 +       }
34343 +
34344 +       return err;
34345 +}
34346 +
34347 +#define AuCpdown_PARENT_OPQ    1
34348 +#define AuCpdown_WHED          (1 << 1)
34349 +#define AuCpdown_MADE_DIR      (1 << 2)
34350 +#define AuCpdown_DIROPQ                (1 << 3)
34351 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34352 +#define au_fset_cpdown(flags, name) \
34353 +       do { (flags) |= AuCpdown_##name; } while (0)
34354 +#define au_fclr_cpdown(flags, name) \
34355 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34356 +
34357 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34358 +                            unsigned int *flags)
34359 +{
34360 +       int err;
34361 +       struct dentry *opq_dentry;
34362 +
34363 +       opq_dentry = au_diropq_create(dentry, bdst);
34364 +       err = PTR_ERR(opq_dentry);
34365 +       if (IS_ERR(opq_dentry))
34366 +               goto out;
34367 +       dput(opq_dentry);
34368 +       au_fset_cpdown(*flags, DIROPQ);
34369 +
34370 +out:
34371 +       return err;
34372 +}
34373 +
34374 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34375 +                           struct inode *dir, aufs_bindex_t bdst)
34376 +{
34377 +       int err;
34378 +       struct path h_path;
34379 +       struct au_branch *br;
34380 +
34381 +       br = au_sbr(dentry->d_sb, bdst);
34382 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34383 +       err = PTR_ERR(h_path.dentry);
34384 +       if (IS_ERR(h_path.dentry))
34385 +               goto out;
34386 +
34387 +       err = 0;
34388 +       if (d_is_positive(h_path.dentry)) {
34389 +               h_path.mnt = au_br_mnt(br);
34390 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34391 +                                         dentry);
34392 +       }
34393 +       dput(h_path.dentry);
34394 +
34395 +out:
34396 +       return err;
34397 +}
34398 +
34399 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34400 +                        struct au_pin *pin,
34401 +                        struct dentry *h_parent, void *arg)
34402 +{
34403 +       int err, rerr;
34404 +       aufs_bindex_t bopq, btop;
34405 +       struct path h_path;
34406 +       struct dentry *parent;
34407 +       struct inode *h_dir, *h_inode, *inode, *dir;
34408 +       unsigned int *flags = arg;
34409 +
34410 +       btop = au_dbtop(dentry);
34411 +       /* dentry is di-locked */
34412 +       parent = dget_parent(dentry);
34413 +       dir = d_inode(parent);
34414 +       h_dir = d_inode(h_parent);
34415 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34416 +       IMustLock(h_dir);
34417 +
34418 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34419 +       if (unlikely(err < 0))
34420 +               goto out;
34421 +       h_path.dentry = au_h_dptr(dentry, bdst);
34422 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34423 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34424 +       if (unlikely(err))
34425 +               goto out_put;
34426 +       au_fset_cpdown(*flags, MADE_DIR);
34427 +
34428 +       bopq = au_dbdiropq(dentry);
34429 +       au_fclr_cpdown(*flags, WHED);
34430 +       au_fclr_cpdown(*flags, DIROPQ);
34431 +       if (au_dbwh(dentry) == bdst)
34432 +               au_fset_cpdown(*flags, WHED);
34433 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34434 +               au_fset_cpdown(*flags, PARENT_OPQ);
34435 +       h_inode = d_inode(h_path.dentry);
34436 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34437 +       if (au_ftest_cpdown(*flags, WHED)) {
34438 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34439 +               if (unlikely(err)) {
34440 +                       inode_unlock(h_inode);
34441 +                       goto out_dir;
34442 +               }
34443 +       }
34444 +
34445 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34446 +       inode_unlock(h_inode);
34447 +       if (unlikely(err))
34448 +               goto out_opq;
34449 +
34450 +       if (au_ftest_cpdown(*flags, WHED)) {
34451 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34452 +               if (unlikely(err))
34453 +                       goto out_opq;
34454 +       }
34455 +
34456 +       inode = d_inode(dentry);
34457 +       if (au_ibbot(inode) < bdst)
34458 +               au_set_ibbot(inode, bdst);
34459 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34460 +                     au_hi_flags(inode, /*isdir*/1));
34461 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34462 +       goto out; /* success */
34463 +
34464 +       /* revert */
34465 +out_opq:
34466 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34467 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34468 +               rerr = au_diropq_remove(dentry, bdst);
34469 +               inode_unlock(h_inode);
34470 +               if (unlikely(rerr)) {
34471 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34472 +                               dentry, bdst, rerr);
34473 +                       err = -EIO;
34474 +                       goto out;
34475 +               }
34476 +       }
34477 +out_dir:
34478 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34479 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34480 +               if (unlikely(rerr)) {
34481 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34482 +                               dentry, bdst, rerr);
34483 +                       err = -EIO;
34484 +               }
34485 +       }
34486 +out_put:
34487 +       au_set_h_dptr(dentry, bdst, NULL);
34488 +       if (au_dbbot(dentry) == bdst)
34489 +               au_update_dbbot(dentry);
34490 +out:
34491 +       dput(parent);
34492 +       return err;
34493 +}
34494 +
34495 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34496 +{
34497 +       int err;
34498 +       unsigned int flags;
34499 +
34500 +       flags = 0;
34501 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34502 +
34503 +       return err;
34504 +}
34505 +
34506 +/* ---------------------------------------------------------------------- */
34507 +
34508 +/* policies for create */
34509 +
34510 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34511 +{
34512 +       int err, i, j, ndentry;
34513 +       aufs_bindex_t bopq;
34514 +       struct au_dcsub_pages dpages;
34515 +       struct au_dpage *dpage;
34516 +       struct dentry **dentries, *parent, *d;
34517 +
34518 +       err = au_dpages_init(&dpages, GFP_NOFS);
34519 +       if (unlikely(err))
34520 +               goto out;
34521 +       parent = dget_parent(dentry);
34522 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34523 +       if (unlikely(err))
34524 +               goto out_free;
34525 +
34526 +       err = bindex;
34527 +       for (i = 0; i < dpages.ndpage; i++) {
34528 +               dpage = dpages.dpages + i;
34529 +               dentries = dpage->dentries;
34530 +               ndentry = dpage->ndentry;
34531 +               for (j = 0; j < ndentry; j++) {
34532 +                       d = dentries[j];
34533 +                       di_read_lock_parent2(d, !AuLock_IR);
34534 +                       bopq = au_dbdiropq(d);
34535 +                       di_read_unlock(d, !AuLock_IR);
34536 +                       if (bopq >= 0 && bopq < err)
34537 +                               err = bopq;
34538 +               }
34539 +       }
34540 +
34541 +out_free:
34542 +       dput(parent);
34543 +       au_dpages_free(&dpages);
34544 +out:
34545 +       return err;
34546 +}
34547 +
34548 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34549 +{
34550 +       for (; bindex >= 0; bindex--)
34551 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34552 +                       return bindex;
34553 +       return -EROFS;
34554 +}
34555 +
34556 +/* top down parent */
34557 +static int au_wbr_create_tdp(struct dentry *dentry,
34558 +                            unsigned int flags __maybe_unused)
34559 +{
34560 +       int err;
34561 +       aufs_bindex_t btop, bindex;
34562 +       struct super_block *sb;
34563 +       struct dentry *parent, *h_parent;
34564 +
34565 +       sb = dentry->d_sb;
34566 +       btop = au_dbtop(dentry);
34567 +       err = btop;
34568 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34569 +               goto out;
34570 +
34571 +       err = -EROFS;
34572 +       parent = dget_parent(dentry);
34573 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34574 +               h_parent = au_h_dptr(parent, bindex);
34575 +               if (!h_parent || d_is_negative(h_parent))
34576 +                       continue;
34577 +
34578 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34579 +                       err = bindex;
34580 +                       break;
34581 +               }
34582 +       }
34583 +       dput(parent);
34584 +
34585 +       /* bottom up here */
34586 +       if (unlikely(err < 0)) {
34587 +               err = au_wbr_bu(sb, btop - 1);
34588 +               if (err >= 0)
34589 +                       err = au_wbr_nonopq(dentry, err);
34590 +       }
34591 +
34592 +out:
34593 +       AuDbg("b%d\n", err);
34594 +       return err;
34595 +}
34596 +
34597 +/* ---------------------------------------------------------------------- */
34598 +
34599 +/* an exception for the policy other than tdp */
34600 +static int au_wbr_create_exp(struct dentry *dentry)
34601 +{
34602 +       int err;
34603 +       aufs_bindex_t bwh, bdiropq;
34604 +       struct dentry *parent;
34605 +
34606 +       err = -1;
34607 +       bwh = au_dbwh(dentry);
34608 +       parent = dget_parent(dentry);
34609 +       bdiropq = au_dbdiropq(parent);
34610 +       if (bwh >= 0) {
34611 +               if (bdiropq >= 0)
34612 +                       err = min(bdiropq, bwh);
34613 +               else
34614 +                       err = bwh;
34615 +               AuDbg("%d\n", err);
34616 +       } else if (bdiropq >= 0) {
34617 +               err = bdiropq;
34618 +               AuDbg("%d\n", err);
34619 +       }
34620 +       dput(parent);
34621 +
34622 +       if (err >= 0)
34623 +               err = au_wbr_nonopq(dentry, err);
34624 +
34625 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34626 +               err = -1;
34627 +
34628 +       AuDbg("%d\n", err);
34629 +       return err;
34630 +}
34631 +
34632 +/* ---------------------------------------------------------------------- */
34633 +
34634 +/* round robin */
34635 +static int au_wbr_create_init_rr(struct super_block *sb)
34636 +{
34637 +       int err;
34638 +
34639 +       err = au_wbr_bu(sb, au_sbbot(sb));
34640 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34641 +       /* smp_mb(); */
34642 +
34643 +       AuDbg("b%d\n", err);
34644 +       return err;
34645 +}
34646 +
34647 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34648 +{
34649 +       int err, nbr;
34650 +       unsigned int u;
34651 +       aufs_bindex_t bindex, bbot;
34652 +       struct super_block *sb;
34653 +       atomic_t *next;
34654 +
34655 +       err = au_wbr_create_exp(dentry);
34656 +       if (err >= 0)
34657 +               goto out;
34658 +
34659 +       sb = dentry->d_sb;
34660 +       next = &au_sbi(sb)->si_wbr_rr_next;
34661 +       bbot = au_sbbot(sb);
34662 +       nbr = bbot + 1;
34663 +       for (bindex = 0; bindex <= bbot; bindex++) {
34664 +               if (!au_ftest_wbr(flags, DIR)) {
34665 +                       err = atomic_dec_return(next) + 1;
34666 +                       /* modulo for 0 is meaningless */
34667 +                       if (unlikely(!err))
34668 +                               err = atomic_dec_return(next) + 1;
34669 +               } else
34670 +                       err = atomic_read(next);
34671 +               AuDbg("%d\n", err);
34672 +               u = err;
34673 +               err = u % nbr;
34674 +               AuDbg("%d\n", err);
34675 +               if (!au_br_rdonly(au_sbr(sb, err)))
34676 +                       break;
34677 +               err = -EROFS;
34678 +       }
34679 +
34680 +       if (err >= 0)
34681 +               err = au_wbr_nonopq(dentry, err);
34682 +
34683 +out:
34684 +       AuDbg("%d\n", err);
34685 +       return err;
34686 +}
34687 +
34688 +/* ---------------------------------------------------------------------- */
34689 +
34690 +/* most free space */
34691 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34692 +{
34693 +       struct super_block *sb;
34694 +       struct au_branch *br;
34695 +       struct au_wbr_mfs *mfs;
34696 +       struct dentry *h_parent;
34697 +       aufs_bindex_t bindex, bbot;
34698 +       int err;
34699 +       unsigned long long b, bavail;
34700 +       struct path h_path;
34701 +       /* reduce the stack usage */
34702 +       struct kstatfs *st;
34703 +
34704 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34705 +       if (unlikely(!st)) {
34706 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34707 +               return;
34708 +       }
34709 +
34710 +       bavail = 0;
34711 +       sb = dentry->d_sb;
34712 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34713 +       MtxMustLock(&mfs->mfs_lock);
34714 +       mfs->mfs_bindex = -EROFS;
34715 +       mfs->mfsrr_bytes = 0;
34716 +       if (!parent) {
34717 +               bindex = 0;
34718 +               bbot = au_sbbot(sb);
34719 +       } else {
34720 +               bindex = au_dbtop(parent);
34721 +               bbot = au_dbtaildir(parent);
34722 +       }
34723 +
34724 +       for (; bindex <= bbot; bindex++) {
34725 +               if (parent) {
34726 +                       h_parent = au_h_dptr(parent, bindex);
34727 +                       if (!h_parent || d_is_negative(h_parent))
34728 +                               continue;
34729 +               }
34730 +               br = au_sbr(sb, bindex);
34731 +               if (au_br_rdonly(br))
34732 +                       continue;
34733 +
34734 +               /* sb->s_root for NFS is unreliable */
34735 +               h_path.mnt = au_br_mnt(br);
34736 +               h_path.dentry = h_path.mnt->mnt_root;
34737 +               err = vfs_statfs(&h_path, st);
34738 +               if (unlikely(err)) {
34739 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34740 +                       continue;
34741 +               }
34742 +
34743 +               /* when the available size is equal, select the lower one */
34744 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34745 +                            || sizeof(b) < sizeof(st->f_bsize));
34746 +               b = st->f_bavail * st->f_bsize;
34747 +               br->br_wbr->wbr_bytes = b;
34748 +               if (b >= bavail) {
34749 +                       bavail = b;
34750 +                       mfs->mfs_bindex = bindex;
34751 +                       mfs->mfs_jiffy = jiffies;
34752 +               }
34753 +       }
34754 +
34755 +       mfs->mfsrr_bytes = bavail;
34756 +       AuDbg("b%d\n", mfs->mfs_bindex);
34757 +       au_kfree_rcu(st);
34758 +}
34759 +
34760 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34761 +{
34762 +       int err;
34763 +       struct dentry *parent;
34764 +       struct super_block *sb;
34765 +       struct au_wbr_mfs *mfs;
34766 +
34767 +       err = au_wbr_create_exp(dentry);
34768 +       if (err >= 0)
34769 +               goto out;
34770 +
34771 +       sb = dentry->d_sb;
34772 +       parent = NULL;
34773 +       if (au_ftest_wbr(flags, PARENT))
34774 +               parent = dget_parent(dentry);
34775 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34776 +       mutex_lock(&mfs->mfs_lock);
34777 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34778 +           || mfs->mfs_bindex < 0
34779 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34780 +               au_mfs(dentry, parent);
34781 +       mutex_unlock(&mfs->mfs_lock);
34782 +       err = mfs->mfs_bindex;
34783 +       dput(parent);
34784 +
34785 +       if (err >= 0)
34786 +               err = au_wbr_nonopq(dentry, err);
34787 +
34788 +out:
34789 +       AuDbg("b%d\n", err);
34790 +       return err;
34791 +}
34792 +
34793 +static int au_wbr_create_init_mfs(struct super_block *sb)
34794 +{
34795 +       struct au_wbr_mfs *mfs;
34796 +
34797 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34798 +       mutex_init(&mfs->mfs_lock);
34799 +       mfs->mfs_jiffy = 0;
34800 +       mfs->mfs_bindex = -EROFS;
34801 +
34802 +       return 0;
34803 +}
34804 +
34805 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34806 +{
34807 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34808 +       return 0;
34809 +}
34810 +
34811 +/* ---------------------------------------------------------------------- */
34812 +
34813 +/* top down regardless parent, and then mfs */
34814 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34815 +                              unsigned int flags __maybe_unused)
34816 +{
34817 +       int err;
34818 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34819 +       unsigned long long watermark;
34820 +       struct super_block *sb;
34821 +       struct au_wbr_mfs *mfs;
34822 +       struct au_branch *br;
34823 +       struct dentry *parent;
34824 +
34825 +       sb = dentry->d_sb;
34826 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34827 +       mutex_lock(&mfs->mfs_lock);
34828 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34829 +           || mfs->mfs_bindex < 0)
34830 +               au_mfs(dentry, /*parent*/NULL);
34831 +       watermark = mfs->mfsrr_watermark;
34832 +       bmfs = mfs->mfs_bindex;
34833 +       mutex_unlock(&mfs->mfs_lock);
34834 +
34835 +       /* another style of au_wbr_create_exp() */
34836 +       bwh = au_dbwh(dentry);
34837 +       parent = dget_parent(dentry);
34838 +       btail = au_dbtaildir(parent);
34839 +       if (bwh >= 0 && bwh < btail)
34840 +               btail = bwh;
34841 +
34842 +       err = au_wbr_nonopq(dentry, btail);
34843 +       if (unlikely(err < 0))
34844 +               goto out;
34845 +       btail = err;
34846 +       bfound = -1;
34847 +       for (bindex = 0; bindex <= btail; bindex++) {
34848 +               br = au_sbr(sb, bindex);
34849 +               if (au_br_rdonly(br))
34850 +                       continue;
34851 +               if (br->br_wbr->wbr_bytes > watermark) {
34852 +                       bfound = bindex;
34853 +                       break;
34854 +               }
34855 +       }
34856 +       err = bfound;
34857 +       if (err < 0)
34858 +               err = bmfs;
34859 +
34860 +out:
34861 +       dput(parent);
34862 +       AuDbg("b%d\n", err);
34863 +       return err;
34864 +}
34865 +
34866 +/* ---------------------------------------------------------------------- */
34867 +
34868 +/* most free space and then round robin */
34869 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34870 +{
34871 +       int err;
34872 +       struct au_wbr_mfs *mfs;
34873 +
34874 +       err = au_wbr_create_mfs(dentry, flags);
34875 +       if (err >= 0) {
34876 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34877 +               mutex_lock(&mfs->mfs_lock);
34878 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34879 +                       err = au_wbr_create_rr(dentry, flags);
34880 +               mutex_unlock(&mfs->mfs_lock);
34881 +       }
34882 +
34883 +       AuDbg("b%d\n", err);
34884 +       return err;
34885 +}
34886 +
34887 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34888 +{
34889 +       int err;
34890 +
34891 +       au_wbr_create_init_mfs(sb); /* ignore */
34892 +       err = au_wbr_create_init_rr(sb);
34893 +
34894 +       return err;
34895 +}
34896 +
34897 +/* ---------------------------------------------------------------------- */
34898 +
34899 +/* top down parent and most free space */
34900 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34901 +{
34902 +       int err, e2;
34903 +       unsigned long long b;
34904 +       aufs_bindex_t bindex, btop, bbot;
34905 +       struct super_block *sb;
34906 +       struct dentry *parent, *h_parent;
34907 +       struct au_branch *br;
34908 +
34909 +       err = au_wbr_create_tdp(dentry, flags);
34910 +       if (unlikely(err < 0))
34911 +               goto out;
34912 +       parent = dget_parent(dentry);
34913 +       btop = au_dbtop(parent);
34914 +       bbot = au_dbtaildir(parent);
34915 +       if (btop == bbot)
34916 +               goto out_parent; /* success */
34917 +
34918 +       e2 = au_wbr_create_mfs(dentry, flags);
34919 +       if (e2 < 0)
34920 +               goto out_parent; /* success */
34921 +
34922 +       /* when the available size is equal, select upper one */
34923 +       sb = dentry->d_sb;
34924 +       br = au_sbr(sb, err);
34925 +       b = br->br_wbr->wbr_bytes;
34926 +       AuDbg("b%d, %llu\n", err, b);
34927 +
34928 +       for (bindex = btop; bindex <= bbot; bindex++) {
34929 +               h_parent = au_h_dptr(parent, bindex);
34930 +               if (!h_parent || d_is_negative(h_parent))
34931 +                       continue;
34932 +
34933 +               br = au_sbr(sb, bindex);
34934 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34935 +                       b = br->br_wbr->wbr_bytes;
34936 +                       err = bindex;
34937 +                       AuDbg("b%d, %llu\n", err, b);
34938 +               }
34939 +       }
34940 +
34941 +       if (err >= 0)
34942 +               err = au_wbr_nonopq(dentry, err);
34943 +
34944 +out_parent:
34945 +       dput(parent);
34946 +out:
34947 +       AuDbg("b%d\n", err);
34948 +       return err;
34949 +}
34950 +
34951 +/* ---------------------------------------------------------------------- */
34952 +
34953 +/*
34954 + * - top down parent
34955 + * - most free space with parent
34956 + * - most free space round-robin regardless parent
34957 + */
34958 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34959 +{
34960 +       int err;
34961 +       unsigned long long watermark;
34962 +       struct super_block *sb;
34963 +       struct au_branch *br;
34964 +       struct au_wbr_mfs *mfs;
34965 +
34966 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34967 +       if (unlikely(err < 0))
34968 +               goto out;
34969 +
34970 +       sb = dentry->d_sb;
34971 +       br = au_sbr(sb, err);
34972 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34973 +       mutex_lock(&mfs->mfs_lock);
34974 +       watermark = mfs->mfsrr_watermark;
34975 +       mutex_unlock(&mfs->mfs_lock);
34976 +       if (br->br_wbr->wbr_bytes < watermark)
34977 +               /* regardless the parent dir */
34978 +               err = au_wbr_create_mfsrr(dentry, flags);
34979 +
34980 +out:
34981 +       AuDbg("b%d\n", err);
34982 +       return err;
34983 +}
34984 +
34985 +/* ---------------------------------------------------------------------- */
34986 +
34987 +/* policies for copyup */
34988 +
34989 +/* top down parent */
34990 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34991 +{
34992 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34993 +}
34994 +
34995 +/* bottom up parent */
34996 +static int au_wbr_copyup_bup(struct dentry *dentry)
34997 +{
34998 +       int err;
34999 +       aufs_bindex_t bindex, btop;
35000 +       struct dentry *parent, *h_parent;
35001 +       struct super_block *sb;
35002 +
35003 +       err = -EROFS;
35004 +       sb = dentry->d_sb;
35005 +       parent = dget_parent(dentry);
35006 +       btop = au_dbtop(parent);
35007 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
35008 +               h_parent = au_h_dptr(parent, bindex);
35009 +               if (!h_parent || d_is_negative(h_parent))
35010 +                       continue;
35011 +
35012 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
35013 +                       err = bindex;
35014 +                       break;
35015 +               }
35016 +       }
35017 +       dput(parent);
35018 +
35019 +       /* bottom up here */
35020 +       if (unlikely(err < 0))
35021 +               err = au_wbr_bu(sb, btop - 1);
35022 +
35023 +       AuDbg("b%d\n", err);
35024 +       return err;
35025 +}
35026 +
35027 +/* bottom up */
35028 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
35029 +{
35030 +       int err;
35031 +
35032 +       err = au_wbr_bu(dentry->d_sb, btop);
35033 +       AuDbg("b%d\n", err);
35034 +       if (err > btop)
35035 +               err = au_wbr_nonopq(dentry, err);
35036 +
35037 +       AuDbg("b%d\n", err);
35038 +       return err;
35039 +}
35040 +
35041 +static int au_wbr_copyup_bu(struct dentry *dentry)
35042 +{
35043 +       int err;
35044 +       aufs_bindex_t btop;
35045 +
35046 +       btop = au_dbtop(dentry);
35047 +       err = au_wbr_do_copyup_bu(dentry, btop);
35048 +       return err;
35049 +}
35050 +
35051 +/* ---------------------------------------------------------------------- */
35052 +
35053 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
35054 +       [AuWbrCopyup_TDP] = {
35055 +               .copyup = au_wbr_copyup_tdp
35056 +       },
35057 +       [AuWbrCopyup_BUP] = {
35058 +               .copyup = au_wbr_copyup_bup
35059 +       },
35060 +       [AuWbrCopyup_BU] = {
35061 +               .copyup = au_wbr_copyup_bu
35062 +       }
35063 +};
35064 +
35065 +struct au_wbr_create_operations au_wbr_create_ops[] = {
35066 +       [AuWbrCreate_TDP] = {
35067 +               .create = au_wbr_create_tdp
35068 +       },
35069 +       [AuWbrCreate_RR] = {
35070 +               .create = au_wbr_create_rr,
35071 +               .init   = au_wbr_create_init_rr
35072 +       },
35073 +       [AuWbrCreate_MFS] = {
35074 +               .create = au_wbr_create_mfs,
35075 +               .init   = au_wbr_create_init_mfs,
35076 +               .fin    = au_wbr_create_fin_mfs
35077 +       },
35078 +       [AuWbrCreate_MFSV] = {
35079 +               .create = au_wbr_create_mfs,
35080 +               .init   = au_wbr_create_init_mfs,
35081 +               .fin    = au_wbr_create_fin_mfs
35082 +       },
35083 +       [AuWbrCreate_MFSRR] = {
35084 +               .create = au_wbr_create_mfsrr,
35085 +               .init   = au_wbr_create_init_mfsrr,
35086 +               .fin    = au_wbr_create_fin_mfs
35087 +       },
35088 +       [AuWbrCreate_MFSRRV] = {
35089 +               .create = au_wbr_create_mfsrr,
35090 +               .init   = au_wbr_create_init_mfsrr,
35091 +               .fin    = au_wbr_create_fin_mfs
35092 +       },
35093 +       [AuWbrCreate_TDMFS] = {
35094 +               .create = au_wbr_create_tdmfs,
35095 +               .init   = au_wbr_create_init_mfs,
35096 +               .fin    = au_wbr_create_fin_mfs
35097 +       },
35098 +       [AuWbrCreate_TDMFSV] = {
35099 +               .create = au_wbr_create_tdmfs,
35100 +               .init   = au_wbr_create_init_mfs,
35101 +               .fin    = au_wbr_create_fin_mfs
35102 +       },
35103 +       [AuWbrCreate_PMFS] = {
35104 +               .create = au_wbr_create_pmfs,
35105 +               .init   = au_wbr_create_init_mfs,
35106 +               .fin    = au_wbr_create_fin_mfs
35107 +       },
35108 +       [AuWbrCreate_PMFSV] = {
35109 +               .create = au_wbr_create_pmfs,
35110 +               .init   = au_wbr_create_init_mfs,
35111 +               .fin    = au_wbr_create_fin_mfs
35112 +       },
35113 +       [AuWbrCreate_PMFSRR] = {
35114 +               .create = au_wbr_create_pmfsrr,
35115 +               .init   = au_wbr_create_init_mfsrr,
35116 +               .fin    = au_wbr_create_fin_mfs
35117 +       },
35118 +       [AuWbrCreate_PMFSRRV] = {
35119 +               .create = au_wbr_create_pmfsrr,
35120 +               .init   = au_wbr_create_init_mfsrr,
35121 +               .fin    = au_wbr_create_fin_mfs
35122 +       }
35123 +};
35124 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
35125 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
35126 +++ linux/fs/aufs/whout.c       2023-08-28 12:34:39.999969465 +0200
35127 @@ -0,0 +1,1072 @@
35128 +// SPDX-License-Identifier: GPL-2.0
35129 +/*
35130 + * Copyright (C) 2005-2022 Junjiro R. Okajima
35131 + *
35132 + * This program is free software; you can redistribute it and/or modify
35133 + * it under the terms of the GNU General Public License as published by
35134 + * the Free Software Foundation; either version 2 of the License, or
35135 + * (at your option) any later version.
35136 + *
35137 + * This program is distributed in the hope that it will be useful,
35138 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35139 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35140 + * GNU General Public License for more details.
35141 + *
35142 + * You should have received a copy of the GNU General Public License
35143 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35144 + */
35145 +
35146 +/*
35147 + * whiteout for logical deletion and opaque directory
35148 + */
35149 +
35150 +#include "aufs.h"
35151 +
35152 +#define WH_MASK                        0444
35153 +
35154 +/*
35155 + * If a directory contains this file, then it is opaque.  We start with the
35156 + * .wh. flag so that it is blocked by lookup.
35157 + */
35158 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35159 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
35160 +
35161 +/*
35162 + * generate whiteout name, which is NOT terminated by NULL.
35163 + * @name: original d_name.name
35164 + * @len: original d_name.len
35165 + * @wh: whiteout qstr
35166 + * returns zero when succeeds, otherwise error.
35167 + * succeeded value as wh->name should be freed by kfree().
35168 + */
35169 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35170 +{
35171 +       char *p;
35172 +
35173 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35174 +               return -ENAMETOOLONG;
35175 +
35176 +       wh->len = name->len + AUFS_WH_PFX_LEN;
35177 +       p = kmalloc(wh->len, GFP_NOFS);
35178 +       wh->name = p;
35179 +       if (p) {
35180 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35181 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35182 +               /* smp_mb(); */
35183 +               return 0;
35184 +       }
35185 +       return -ENOMEM;
35186 +}
35187 +
35188 +/* ---------------------------------------------------------------------- */
35189 +
35190 +/*
35191 + * test if the @wh_name exists under @h_ppath.
35192 + * @try_sio specifies the necessary of super-io.
35193 + */
35194 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
35195 +              struct qstr *wh_name, int try_sio)
35196 +{
35197 +       int err;
35198 +       struct dentry *wh_dentry;
35199 +
35200 +       if (!try_sio)
35201 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
35202 +       else
35203 +               wh_dentry = au_sio_lkup_one(h_idmap, wh_name, h_ppath);
35204 +       err = PTR_ERR(wh_dentry);
35205 +       if (IS_ERR(wh_dentry)) {
35206 +               if (err == -ENAMETOOLONG)
35207 +                       err = 0;
35208 +               goto out;
35209 +       }
35210 +
35211 +       err = 0;
35212 +       if (d_is_negative(wh_dentry))
35213 +               goto out_wh; /* success */
35214 +
35215 +       err = 1;
35216 +       if (d_is_reg(wh_dentry))
35217 +               goto out_wh; /* success */
35218 +
35219 +       err = -EIO;
35220 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35221 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35222 +
35223 +out_wh:
35224 +       dput(wh_dentry);
35225 +out:
35226 +       return err;
35227 +}
35228 +
35229 +/*
35230 + * test if the @h_path->dentry sets opaque or not.
35231 + */
35232 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path)
35233 +{
35234 +       int err;
35235 +       struct inode *h_dir;
35236 +
35237 +       h_dir = d_inode(h_path->dentry);
35238 +       err = au_wh_test(h_idmap, h_path, &diropq_name,
35239 +                        au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC));
35240 +       return err;
35241 +}
35242 +
35243 +/*
35244 + * returns a negative dentry whose name is unique and temporary.
35245 + */
35246 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35247 +                            struct qstr *prefix)
35248 +{
35249 +       struct dentry *dentry;
35250 +       int i;
35251 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35252 +               *name, *p;
35253 +       /* strict atomic_t is unnecessary here */
35254 +       static unsigned short cnt;
35255 +       struct qstr qs;
35256 +       struct path h_ppath;
35257 +       struct mnt_idmap *h_idmap;
35258 +
35259 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35260 +
35261 +       name = defname;
35262 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35263 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35264 +               dentry = ERR_PTR(-ENAMETOOLONG);
35265 +               if (unlikely(qs.len > NAME_MAX))
35266 +                       goto out;
35267 +               dentry = ERR_PTR(-ENOMEM);
35268 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35269 +               if (unlikely(!name))
35270 +                       goto out;
35271 +       }
35272 +
35273 +       /* doubly whiteout-ed */
35274 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35275 +       p = name + AUFS_WH_PFX_LEN * 2;
35276 +       memcpy(p, prefix->name, prefix->len);
35277 +       p += prefix->len;
35278 +       *p++ = '.';
35279 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35280 +
35281 +       h_ppath.dentry = h_parent;
35282 +       h_ppath.mnt = au_br_mnt(br);
35283 +       h_idmap = au_br_idmap(br);
35284 +       qs.name = name;
35285 +       for (i = 0; i < 3; i++) {
35286 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35287 +               dentry = au_sio_lkup_one(h_idmap, &qs, &h_ppath);
35288 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35289 +                       goto out_name;
35290 +               dput(dentry);
35291 +       }
35292 +       /* pr_warn("could not get random name\n"); */
35293 +       dentry = ERR_PTR(-EEXIST);
35294 +       AuDbg("%.*s\n", AuLNPair(&qs));
35295 +       BUG();
35296 +
35297 +out_name:
35298 +       if (name != defname)
35299 +               au_kfree_try_rcu(name);
35300 +out:
35301 +       AuTraceErrPtr(dentry);
35302 +       return dentry;
35303 +}
35304 +
35305 +/*
35306 + * rename the @h_dentry on @br to the whiteouted temporary name.
35307 + */
35308 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35309 +{
35310 +       int err;
35311 +       struct path h_path = {
35312 +               .mnt = au_br_mnt(br)
35313 +       };
35314 +       struct inode *h_dir, *delegated;
35315 +       struct dentry *h_parent;
35316 +
35317 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35318 +       h_dir = d_inode(h_parent);
35319 +       IMustLock(h_dir);
35320 +
35321 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35322 +       err = PTR_ERR(h_path.dentry);
35323 +       if (IS_ERR(h_path.dentry))
35324 +               goto out;
35325 +
35326 +       /* under the same dir, no need to lock_rename() */
35327 +       delegated = NULL;
35328 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35329 +                          /*flags*/0);
35330 +       AuTraceErr(err);
35331 +       if (unlikely(err == -EWOULDBLOCK)) {
35332 +               pr_warn("cannot retry for NFSv4 delegation"
35333 +                       " for an internal rename\n");
35334 +               iput(delegated);
35335 +       }
35336 +       dput(h_path.dentry);
35337 +
35338 +out:
35339 +       AuTraceErr(err);
35340 +       return err;
35341 +}
35342 +
35343 +/* ---------------------------------------------------------------------- */
35344 +/*
35345 + * functions for removing a whiteout
35346 + */
35347 +
35348 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35349 +{
35350 +       int err, force;
35351 +       struct inode *delegated;
35352 +
35353 +       /*
35354 +        * forces superio when the dir has a sticky bit.
35355 +        * this may be a violation of unix fs semantics.
35356 +        */
35357 +       force = (h_dir->i_mode & S_ISVTX)
35358 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35359 +       delegated = NULL;
35360 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35361 +       if (unlikely(err == -EWOULDBLOCK)) {
35362 +               pr_warn("cannot retry for NFSv4 delegation"
35363 +                       " for an internal unlink\n");
35364 +               iput(delegated);
35365 +       }
35366 +       return err;
35367 +}
35368 +
35369 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35370 +                       struct dentry *dentry)
35371 +{
35372 +       int err;
35373 +
35374 +       err = do_unlink_wh(h_dir, h_path);
35375 +       if (!err && dentry)
35376 +               au_set_dbwh(dentry, -1);
35377 +
35378 +       return err;
35379 +}
35380 +
35381 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35382 +{
35383 +       int err;
35384 +       struct path h_path;
35385 +
35386 +       err = 0;
35387 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35388 +       if (IS_ERR(h_path.dentry))
35389 +               err = PTR_ERR(h_path.dentry);
35390 +       else {
35391 +               if (d_is_reg(h_path.dentry)) {
35392 +                       h_path.mnt = h_ppath->mnt;
35393 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35394 +               }
35395 +               dput(h_path.dentry);
35396 +       }
35397 +
35398 +       return err;
35399 +}
35400 +
35401 +/* ---------------------------------------------------------------------- */
35402 +/*
35403 + * initialize/clean whiteout for a branch
35404 + */
35405 +
35406 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35407 +                       const int isdir)
35408 +{
35409 +       int err;
35410 +       struct inode *delegated;
35411 +
35412 +       if (d_is_negative(whpath->dentry))
35413 +               return;
35414 +
35415 +       if (isdir)
35416 +               err = vfsub_rmdir(h_dir, whpath);
35417 +       else {
35418 +               delegated = NULL;
35419 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35420 +               if (unlikely(err == -EWOULDBLOCK)) {
35421 +                       pr_warn("cannot retry for NFSv4 delegation"
35422 +                               " for an internal unlink\n");
35423 +                       iput(delegated);
35424 +               }
35425 +       }
35426 +       if (unlikely(err))
35427 +               pr_warn("failed removing %pd (%d), ignored.\n",
35428 +                       whpath->dentry, err);
35429 +}
35430 +
35431 +static int test_linkable(struct dentry *h_root)
35432 +{
35433 +       struct inode *h_dir = d_inode(h_root);
35434 +
35435 +       if (h_dir->i_op->link)
35436 +               return 0;
35437 +
35438 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35439 +              h_root, au_sbtype(h_root->d_sb));
35440 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35441 +}
35442 +
35443 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35444 +static int au_whdir(struct inode *h_dir, struct path *path)
35445 +{
35446 +       int err;
35447 +
35448 +       err = -EEXIST;
35449 +       if (d_is_negative(path->dentry)) {
35450 +               int mode = 0700;
35451 +
35452 +               if (au_test_nfs(path->dentry->d_sb))
35453 +                       mode |= 0111;
35454 +               err = vfsub_mkdir(h_dir, path, mode);
35455 +       } else if (d_is_dir(path->dentry))
35456 +               err = 0;
35457 +       else
35458 +               pr_err("unknown %pd exists\n", path->dentry);
35459 +
35460 +       return err;
35461 +}
35462 +
35463 +struct au_wh_base {
35464 +       const struct qstr *name;
35465 +       struct dentry *dentry;
35466 +};
35467 +
35468 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35469 +                         struct path *h_path)
35470 +{
35471 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35472 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35473 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35474 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35475 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35476 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35477 +}
35478 +
35479 +/*
35480 + * returns tri-state,
35481 + * minus: error, caller should print the message
35482 + * zero: success
35483 + * plus: error, caller should NOT print the message
35484 + */
35485 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35486 +                               int do_plink, struct au_wh_base base[],
35487 +                               struct path *h_path)
35488 +{
35489 +       int err;
35490 +       struct inode *h_dir;
35491 +
35492 +       h_dir = d_inode(h_root);
35493 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35494 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35495 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35496 +       if (do_plink) {
35497 +               err = test_linkable(h_root);
35498 +               if (unlikely(err)) {
35499 +                       err = 1;
35500 +                       goto out;
35501 +               }
35502 +
35503 +               err = au_whdir(h_dir, h_path);
35504 +               if (unlikely(err))
35505 +                       goto out;
35506 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35507 +       } else
35508 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35509 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35510 +       err = au_whdir(h_dir, h_path);
35511 +       if (unlikely(err))
35512 +               goto out;
35513 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35514 +
35515 +out:
35516 +       return err;
35517 +}
35518 +
35519 +/*
35520 + * for the moment, aufs supports the branch filesystem which does not support
35521 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35522 + * copyup failed. finally, such filesystem will not be used as the writable
35523 + * branch.
35524 + *
35525 + * returns tri-state, see above.
35526 + */
35527 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35528 +                        int do_plink, struct au_wh_base base[],
35529 +                        struct path *h_path)
35530 +{
35531 +       int err;
35532 +       struct inode *h_dir;
35533 +
35534 +       WbrWhMustWriteLock(wbr);
35535 +
35536 +       err = test_linkable(h_root);
35537 +       if (unlikely(err)) {
35538 +               err = 1;
35539 +               goto out;
35540 +       }
35541 +
35542 +       /*
35543 +        * todo: should this create be done in /sbin/mount.aufs helper?
35544 +        */
35545 +       err = -EEXIST;
35546 +       h_dir = d_inode(h_root);
35547 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35548 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35549 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35550 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35551 +               err = 0;
35552 +       else
35553 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35554 +       if (unlikely(err))
35555 +               goto out;
35556 +
35557 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35558 +       if (do_plink) {
35559 +               err = au_whdir(h_dir, h_path);
35560 +               if (unlikely(err))
35561 +                       goto out;
35562 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35563 +       } else
35564 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35565 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35566 +
35567 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35568 +       err = au_whdir(h_dir, h_path);
35569 +       if (unlikely(err))
35570 +               goto out;
35571 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35572 +
35573 +out:
35574 +       return err;
35575 +}
35576 +
35577 +/*
35578 + * initialize the whiteout base file/dir for @br.
35579 + */
35580 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35581 +{
35582 +       int err, i;
35583 +       const unsigned char do_plink
35584 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35585 +       struct inode *h_dir;
35586 +       struct path path = br->br_path;
35587 +       struct dentry *h_root = path.dentry;
35588 +       struct au_wbr *wbr = br->br_wbr;
35589 +       static const struct qstr base_name[] = {
35590 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35591 +                                         sizeof(AUFS_BASE_NAME) - 1),
35592 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35593 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35594 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35595 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35596 +       };
35597 +       struct au_wh_base base[] = {
35598 +               [AuBrWh_BASE] = {
35599 +                       .name   = base_name + AuBrWh_BASE,
35600 +                       .dentry = NULL
35601 +               },
35602 +               [AuBrWh_PLINK] = {
35603 +                       .name   = base_name + AuBrWh_PLINK,
35604 +                       .dentry = NULL
35605 +               },
35606 +               [AuBrWh_ORPH] = {
35607 +                       .name   = base_name + AuBrWh_ORPH,
35608 +                       .dentry = NULL
35609 +               }
35610 +       };
35611 +
35612 +       if (wbr)
35613 +               WbrWhMustWriteLock(wbr);
35614 +
35615 +       for (i = 0; i < AuBrWh_Last; i++) {
35616 +               /* doubly whiteouted */
35617 +               struct dentry *d;
35618 +
35619 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35620 +               err = PTR_ERR(d);
35621 +               if (IS_ERR(d))
35622 +                       goto out;
35623 +
35624 +               base[i].dentry = d;
35625 +               AuDebugOn(wbr
35626 +                         && wbr->wbr_wh[i]
35627 +                         && wbr->wbr_wh[i] != base[i].dentry);
35628 +       }
35629 +
35630 +       if (wbr)
35631 +               for (i = 0; i < AuBrWh_Last; i++) {
35632 +                       dput(wbr->wbr_wh[i]);
35633 +                       wbr->wbr_wh[i] = NULL;
35634 +               }
35635 +
35636 +       err = 0;
35637 +       if (!au_br_writable(br->br_perm)) {
35638 +               h_dir = d_inode(h_root);
35639 +               au_wh_init_ro(h_dir, base, &path);
35640 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35641 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35642 +               if (err > 0)
35643 +                       goto out;
35644 +               else if (err)
35645 +                       goto out_err;
35646 +       } else {
35647 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35648 +               if (err > 0)
35649 +                       goto out;
35650 +               else if (err)
35651 +                       goto out_err;
35652 +       }
35653 +       goto out; /* success */
35654 +
35655 +out_err:
35656 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35657 +              err, h_root, au_sbtype(h_root->d_sb));
35658 +out:
35659 +       for (i = 0; i < AuBrWh_Last; i++)
35660 +               dput(base[i].dentry);
35661 +       return err;
35662 +}
35663 +
35664 +/* ---------------------------------------------------------------------- */
35665 +/*
35666 + * whiteouts are all hard-linked usually.
35667 + * when its link count reaches a ceiling, we create a new whiteout base
35668 + * asynchronously.
35669 + */
35670 +
35671 +struct reinit_br_wh {
35672 +       struct super_block *sb;
35673 +       struct au_branch *br;
35674 +};
35675 +
35676 +static void reinit_br_wh(void *arg)
35677 +{
35678 +       int err;
35679 +       aufs_bindex_t bindex;
35680 +       struct path h_path;
35681 +       struct reinit_br_wh *a = arg;
35682 +       struct au_wbr *wbr;
35683 +       struct inode *dir, *delegated;
35684 +       struct dentry *h_root;
35685 +       struct au_hinode *hdir;
35686 +
35687 +       err = 0;
35688 +       wbr = a->br->br_wbr;
35689 +       /* big aufs lock */
35690 +       si_noflush_write_lock(a->sb);
35691 +       if (!au_br_writable(a->br->br_perm))
35692 +               goto out;
35693 +       bindex = au_br_index(a->sb, a->br->br_id);
35694 +       if (unlikely(bindex < 0))
35695 +               goto out;
35696 +
35697 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35698 +       dir = d_inode(a->sb->s_root);
35699 +       hdir = au_hi(dir, bindex);
35700 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35701 +       AuDebugOn(h_root != au_br_dentry(a->br));
35702 +
35703 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35704 +       wbr_wh_write_lock(wbr);
35705 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35706 +                         h_root, a->br);
35707 +       if (!err) {
35708 +               h_path.dentry = wbr->wbr_whbase;
35709 +               h_path.mnt = au_br_mnt(a->br);
35710 +               delegated = NULL;
35711 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35712 +                                  /*force*/0);
35713 +               if (unlikely(err == -EWOULDBLOCK)) {
35714 +                       pr_warn("cannot retry for NFSv4 delegation"
35715 +                               " for an internal unlink\n");
35716 +                       iput(delegated);
35717 +               }
35718 +       } else {
35719 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35720 +               err = 0;
35721 +       }
35722 +       dput(wbr->wbr_whbase);
35723 +       wbr->wbr_whbase = NULL;
35724 +       if (!err)
35725 +               err = au_wh_init(a->br, a->sb);
35726 +       wbr_wh_write_unlock(wbr);
35727 +       au_hn_inode_unlock(hdir);
35728 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35729 +       if (!err)
35730 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35731 +
35732 +out:
35733 +       if (wbr)
35734 +               atomic_dec(&wbr->wbr_wh_running);
35735 +       au_lcnt_dec(&a->br->br_count);
35736 +       si_write_unlock(a->sb);
35737 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35738 +       au_kfree_rcu(a);
35739 +       if (unlikely(err))
35740 +               AuIOErr("err %d\n", err);
35741 +}
35742 +
35743 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35744 +{
35745 +       int do_dec, wkq_err;
35746 +       struct reinit_br_wh *arg;
35747 +
35748 +       do_dec = 1;
35749 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35750 +               goto out;
35751 +
35752 +       /* ignore ENOMEM */
35753 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35754 +       if (arg) {
35755 +               /*
35756 +                * dec(wh_running), kfree(arg) and dec(br_count)
35757 +                * in reinit function
35758 +                */
35759 +               arg->sb = sb;
35760 +               arg->br = br;
35761 +               au_lcnt_inc(&br->br_count);
35762 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35763 +               if (unlikely(wkq_err)) {
35764 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35765 +                       au_lcnt_dec(&br->br_count);
35766 +                       au_kfree_rcu(arg);
35767 +               }
35768 +               do_dec = 0;
35769 +       }
35770 +
35771 +out:
35772 +       if (do_dec)
35773 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35774 +}
35775 +
35776 +/* ---------------------------------------------------------------------- */
35777 +
35778 +/*
35779 + * create the whiteout @wh.
35780 + */
35781 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35782 +                            struct dentry *wh)
35783 +{
35784 +       int err;
35785 +       struct path h_path = {
35786 +               .dentry = wh
35787 +       };
35788 +       struct au_branch *br;
35789 +       struct au_wbr *wbr;
35790 +       struct dentry *h_parent;
35791 +       struct inode *h_dir, *delegated;
35792 +
35793 +       h_parent = wh->d_parent; /* dir inode is locked */
35794 +       h_dir = d_inode(h_parent);
35795 +       IMustLock(h_dir);
35796 +
35797 +       br = au_sbr(sb, bindex);
35798 +       h_path.mnt = au_br_mnt(br);
35799 +       wbr = br->br_wbr;
35800 +       wbr_wh_read_lock(wbr);
35801 +       if (wbr->wbr_whbase) {
35802 +               delegated = NULL;
35803 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35804 +               if (unlikely(err == -EWOULDBLOCK)) {
35805 +                       pr_warn("cannot retry for NFSv4 delegation"
35806 +                               " for an internal link\n");
35807 +                       iput(delegated);
35808 +               }
35809 +               if (!err || err != -EMLINK)
35810 +                       goto out;
35811 +
35812 +               /* link count full. re-initialize br_whbase. */
35813 +               kick_reinit_br_wh(sb, br);
35814 +       }
35815 +
35816 +       /* return this error in this context */
35817 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35818 +       if (!err)
35819 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35820 +
35821 +out:
35822 +       wbr_wh_read_unlock(wbr);
35823 +       return err;
35824 +}
35825 +
35826 +/* ---------------------------------------------------------------------- */
35827 +
35828 +/*
35829 + * create or remove the diropq.
35830 + */
35831 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35832 +                               unsigned int flags)
35833 +{
35834 +       struct dentry *opq_dentry;
35835 +       struct super_block *sb;
35836 +       struct au_branch *br;
35837 +       struct path h_path;
35838 +       int err;
35839 +
35840 +       sb = dentry->d_sb;
35841 +       br = au_sbr(sb, bindex);
35842 +       h_path.dentry = au_h_dptr(dentry, bindex);
35843 +       h_path.mnt = au_br_mnt(br);
35844 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35845 +       if (IS_ERR(opq_dentry))
35846 +               goto out;
35847 +
35848 +       if (au_ftest_diropq(flags, CREATE)) {
35849 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35850 +               if (!err) {
35851 +                       au_set_dbdiropq(dentry, bindex);
35852 +                       goto out; /* success */
35853 +               }
35854 +       } else {
35855 +               h_path.dentry = opq_dentry;
35856 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35857 +               if (!err)
35858 +                       au_set_dbdiropq(dentry, -1);
35859 +       }
35860 +       dput(opq_dentry);
35861 +       opq_dentry = ERR_PTR(err);
35862 +
35863 +out:
35864 +       return opq_dentry;
35865 +}
35866 +
35867 +struct do_diropq_args {
35868 +       struct dentry **errp;
35869 +       struct dentry *dentry;
35870 +       aufs_bindex_t bindex;
35871 +       unsigned int flags;
35872 +};
35873 +
35874 +static void call_do_diropq(void *args)
35875 +{
35876 +       struct do_diropq_args *a = args;
35877 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35878 +}
35879 +
35880 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35881 +                            unsigned int flags)
35882 +{
35883 +       struct dentry *diropq, *h_dentry;
35884 +       struct mnt_idmap *h_idmap;
35885 +
35886 +       h_idmap = au_sbr_idmap(dentry->d_sb, bindex);
35887 +       h_dentry = au_h_dptr(dentry, bindex);
35888 +       if (!au_test_h_perm_sio(h_idmap, d_inode(h_dentry),
35889 +                               MAY_EXEC | MAY_WRITE))
35890 +               diropq = do_diropq(dentry, bindex, flags);
35891 +       else {
35892 +               int wkq_err;
35893 +               struct do_diropq_args args = {
35894 +                       .errp           = &diropq,
35895 +                       .dentry         = dentry,
35896 +                       .bindex         = bindex,
35897 +                       .flags          = flags
35898 +               };
35899 +
35900 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35901 +               if (unlikely(wkq_err))
35902 +                       diropq = ERR_PTR(wkq_err);
35903 +       }
35904 +
35905 +       return diropq;
35906 +}
35907 +
35908 +/* ---------------------------------------------------------------------- */
35909 +
35910 +/*
35911 + * lookup whiteout dentry.
35912 + * @h_parent: lower parent dentry which must exist and be locked
35913 + * @base_name: name of dentry which will be whiteouted
35914 + * returns dentry for whiteout.
35915 + */
35916 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35917 +                         struct au_branch *br)
35918 +{
35919 +       int err;
35920 +       struct qstr wh_name;
35921 +       struct dentry *wh_dentry;
35922 +       struct path h_path;
35923 +
35924 +       err = au_wh_name_alloc(&wh_name, base_name);
35925 +       wh_dentry = ERR_PTR(err);
35926 +       if (!err) {
35927 +               h_path.dentry = h_parent;
35928 +               h_path.mnt = au_br_mnt(br);
35929 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35930 +               au_kfree_try_rcu(wh_name.name);
35931 +       }
35932 +       return wh_dentry;
35933 +}
35934 +
35935 +/*
35936 + * link/create a whiteout for @dentry on @bindex.
35937 + */
35938 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35939 +                           struct dentry *h_parent)
35940 +{
35941 +       struct dentry *wh_dentry;
35942 +       struct super_block *sb;
35943 +       int err;
35944 +
35945 +       sb = dentry->d_sb;
35946 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35947 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35948 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35949 +               if (!err) {
35950 +                       au_set_dbwh(dentry, bindex);
35951 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35952 +               } else {
35953 +                       dput(wh_dentry);
35954 +                       wh_dentry = ERR_PTR(err);
35955 +               }
35956 +       }
35957 +
35958 +       return wh_dentry;
35959 +}
35960 +
35961 +/* ---------------------------------------------------------------------- */
35962 +
35963 +/* Delete all whiteouts in this directory on branch bindex. */
35964 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35965 +                          aufs_bindex_t bindex)
35966 +{
35967 +       int err;
35968 +       unsigned long ul, n;
35969 +       struct qstr wh_name;
35970 +       char *p;
35971 +       struct hlist_head *head;
35972 +       struct au_vdir_wh *pos;
35973 +       struct au_vdir_destr *str;
35974 +
35975 +       err = -ENOMEM;
35976 +       p = (void *)__get_free_page(GFP_NOFS);
35977 +       wh_name.name = p;
35978 +       if (unlikely(!wh_name.name))
35979 +               goto out;
35980 +
35981 +       err = 0;
35982 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35983 +       p += AUFS_WH_PFX_LEN;
35984 +       n = whlist->nh_num;
35985 +       head = whlist->nh_head;
35986 +       for (ul = 0; !err && ul < n; ul++, head++) {
35987 +               hlist_for_each_entry(pos, head, wh_hash) {
35988 +                       if (pos->wh_bindex != bindex)
35989 +                               continue;
35990 +
35991 +                       str = &pos->wh_str;
35992 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35993 +                               memcpy(p, str->name, str->len);
35994 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35995 +                               err = unlink_wh_name(h_path, &wh_name);
35996 +                               if (!err)
35997 +                                       continue;
35998 +                               break;
35999 +                       }
36000 +                       AuIOErr("whiteout name too long %.*s\n",
36001 +                               str->len, str->name);
36002 +                       err = -EIO;
36003 +                       break;
36004 +               }
36005 +       }
36006 +       free_page((unsigned long)wh_name.name);
36007 +
36008 +out:
36009 +       return err;
36010 +}
36011 +
36012 +struct del_wh_children_args {
36013 +       int *errp;
36014 +       struct path *h_path;
36015 +       struct au_nhash *whlist;
36016 +       aufs_bindex_t bindex;
36017 +};
36018 +
36019 +static void call_del_wh_children(void *args)
36020 +{
36021 +       struct del_wh_children_args *a = args;
36022 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
36023 +}
36024 +
36025 +/* ---------------------------------------------------------------------- */
36026 +
36027 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
36028 +{
36029 +       struct au_whtmp_rmdir *whtmp;
36030 +       int err;
36031 +       unsigned int rdhash;
36032 +
36033 +       SiMustAnyLock(sb);
36034 +
36035 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
36036 +       if (unlikely(!whtmp)) {
36037 +               whtmp = ERR_PTR(-ENOMEM);
36038 +               goto out;
36039 +       }
36040 +
36041 +       /* no estimation for dir size */
36042 +       rdhash = au_sbi(sb)->si_rdhash;
36043 +       if (!rdhash)
36044 +               rdhash = AUFS_RDHASH_DEF;
36045 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
36046 +       if (unlikely(err)) {
36047 +               au_kfree_rcu(whtmp);
36048 +               whtmp = ERR_PTR(err);
36049 +       }
36050 +
36051 +out:
36052 +       return whtmp;
36053 +}
36054 +
36055 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
36056 +{
36057 +       if (whtmp->br)
36058 +               au_lcnt_dec(&whtmp->br->br_count);
36059 +       dput(whtmp->wh_dentry);
36060 +       iput(whtmp->dir);
36061 +       au_nhash_wh_free(&whtmp->whlist);
36062 +       au_kfree_rcu(whtmp);
36063 +}
36064 +
36065 +/*
36066 + * rmdir the whiteouted temporary named dir @h_dentry.
36067 + * @whlist: whiteouted children.
36068 + */
36069 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36070 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
36071 +{
36072 +       int err;
36073 +       unsigned int h_nlink;
36074 +       struct path wh_path;
36075 +       struct inode *wh_inode, *h_dir;
36076 +       struct au_branch *br;
36077 +       struct mnt_idmap *h_idmap;
36078 +
36079 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
36080 +       IMustLock(h_dir);
36081 +
36082 +       br = au_sbr(dir->i_sb, bindex);
36083 +       wh_path.dentry = wh_dentry;
36084 +       wh_path.mnt = au_br_mnt(br);
36085 +       h_idmap = au_br_idmap(br);
36086 +       wh_inode = d_inode(wh_dentry);
36087 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
36088 +
36089 +       /*
36090 +        * someone else might change some whiteouts while we were sleeping.
36091 +        * it means this whlist may have an obsoleted entry.
36092 +        */
36093 +       if (!au_test_h_perm_sio(h_idmap, wh_inode, MAY_EXEC | MAY_WRITE))
36094 +               err = del_wh_children(&wh_path, whlist, bindex);
36095 +       else {
36096 +               int wkq_err;
36097 +               struct del_wh_children_args args = {
36098 +                       .errp           = &err,
36099 +                       .h_path         = &wh_path,
36100 +                       .whlist         = whlist,
36101 +                       .bindex         = bindex
36102 +               };
36103 +
36104 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
36105 +               if (unlikely(wkq_err))
36106 +                       err = wkq_err;
36107 +       }
36108 +       inode_unlock(wh_inode);
36109 +
36110 +       if (!err) {
36111 +               h_nlink = h_dir->i_nlink;
36112 +               err = vfsub_rmdir(h_dir, &wh_path);
36113 +               /* some fs doesn't change the parent nlink in some cases */
36114 +               h_nlink -= h_dir->i_nlink;
36115 +       }
36116 +
36117 +       if (!err) {
36118 +               if (au_ibtop(dir) == bindex) {
36119 +                       /* todo: dir->i_mutex is necessary */
36120 +                       au_cpup_attr_timesizes(dir);
36121 +                       if (h_nlink)
36122 +                               vfsub_drop_nlink(dir);
36123 +               }
36124 +               return 0; /* success */
36125 +       }
36126 +
36127 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
36128 +       return err;
36129 +}
36130 +
36131 +static void call_rmdir_whtmp(void *args)
36132 +{
36133 +       int err;
36134 +       aufs_bindex_t bindex;
36135 +       struct au_whtmp_rmdir *a = args;
36136 +       struct super_block *sb;
36137 +       struct dentry *h_parent;
36138 +       struct inode *h_dir;
36139 +       struct au_hinode *hdir;
36140 +
36141 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
36142 +       /* inode_lock(a->dir); */
36143 +       err = -EROFS;
36144 +       sb = a->dir->i_sb;
36145 +       si_read_lock(sb, !AuLock_FLUSH);
36146 +       if (!au_br_writable(a->br->br_perm))
36147 +               goto out;
36148 +       bindex = au_br_index(sb, a->br->br_id);
36149 +       if (unlikely(bindex < 0))
36150 +               goto out;
36151 +
36152 +       err = -EIO;
36153 +       ii_write_lock_parent(a->dir);
36154 +       h_parent = dget_parent(a->wh_dentry);
36155 +       h_dir = d_inode(h_parent);
36156 +       hdir = au_hi(a->dir, bindex);
36157 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
36158 +       if (unlikely(err))
36159 +               goto out_mnt;
36160 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
36161 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36162 +                         a->br);
36163 +       if (!err)
36164 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
36165 +       au_hn_inode_unlock(hdir);
36166 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
36167 +
36168 +out_mnt:
36169 +       dput(h_parent);
36170 +       ii_write_unlock(a->dir);
36171 +out:
36172 +       /* inode_unlock(a->dir); */
36173 +       au_whtmp_rmdir_free(a);
36174 +       si_read_unlock(sb);
36175 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36176 +       if (unlikely(err))
36177 +               AuIOErr("err %d\n", err);
36178 +}
36179 +
36180 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36181 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36182 +{
36183 +       int wkq_err;
36184 +       struct super_block *sb;
36185 +
36186 +       IMustLock(dir);
36187 +
36188 +       /* all post-process will be done in do_rmdir_whtmp(). */
36189 +       sb = dir->i_sb;
36190 +       args->dir = au_igrab(dir);
36191 +       args->br = au_sbr(sb, bindex);
36192 +       au_lcnt_inc(&args->br->br_count);
36193 +       args->wh_dentry = dget(wh_dentry);
36194 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
36195 +       if (unlikely(wkq_err)) {
36196 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
36197 +               au_whtmp_rmdir_free(args);
36198 +       }
36199 +}
36200 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
36201 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
36202 +++ linux/fs/aufs/whout.h       2023-08-28 12:34:39.999969465 +0200
36203 @@ -0,0 +1,87 @@
36204 +/* SPDX-License-Identifier: GPL-2.0 */
36205 +/*
36206 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36207 + *
36208 + * This program is free software; you can redistribute it and/or modify
36209 + * it under the terms of the GNU General Public License as published by
36210 + * the Free Software Foundation; either version 2 of the License, or
36211 + * (at your option) any later version.
36212 + *
36213 + * This program is distributed in the hope that it will be useful,
36214 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36215 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36216 + * GNU General Public License for more details.
36217 + *
36218 + * You should have received a copy of the GNU General Public License
36219 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36220 + */
36221 +
36222 +/*
36223 + * whiteout for logical deletion and opaque directory
36224 + */
36225 +
36226 +#ifndef __AUFS_WHOUT_H__
36227 +#define __AUFS_WHOUT_H__
36228 +
36229 +#ifdef __KERNEL__
36230 +
36231 +#include "dir.h"
36232 +
36233 +/* whout.c */
36234 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36235 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
36236 +              struct qstr *wh_name, int try_sio);
36237 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path);
36238 +struct au_branch;
36239 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36240 +                            struct qstr *prefix);
36241 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36242 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36243 +                       struct dentry *dentry);
36244 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36245 +
36246 +/* diropq flags */
36247 +#define AuDiropq_CREATE        1
36248 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36249 +#define au_fset_diropq(flags, name) \
36250 +       do { (flags) |= AuDiropq_##name; } while (0)
36251 +#define au_fclr_diropq(flags, name) \
36252 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36253 +
36254 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36255 +                            unsigned int flags);
36256 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36257 +                         struct au_branch *br);
36258 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36259 +                           struct dentry *h_parent);
36260 +
36261 +/* real rmdir for the whiteout-ed dir */
36262 +struct au_whtmp_rmdir {
36263 +       struct inode *dir;
36264 +       struct au_branch *br;
36265 +       struct dentry *wh_dentry;
36266 +       struct au_nhash whlist;
36267 +};
36268 +
36269 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36270 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36271 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36272 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36273 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36274 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36275 +
36276 +/* ---------------------------------------------------------------------- */
36277 +
36278 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36279 +                                             aufs_bindex_t bindex)
36280 +{
36281 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36282 +}
36283 +
36284 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36285 +{
36286 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36287 +}
36288 +
36289 +#endif /* __KERNEL__ */
36290 +#endif /* __AUFS_WHOUT_H__ */
36291 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36292 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36293 +++ linux/fs/aufs/wkq.c 2022-11-05 23:02:18.972555950 +0100
36294 @@ -0,0 +1,372 @@
36295 +// SPDX-License-Identifier: GPL-2.0
36296 +/*
36297 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36298 + *
36299 + * This program is free software; you can redistribute it and/or modify
36300 + * it under the terms of the GNU General Public License as published by
36301 + * the Free Software Foundation; either version 2 of the License, or
36302 + * (at your option) any later version.
36303 + *
36304 + * This program is distributed in the hope that it will be useful,
36305 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36306 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36307 + * GNU General Public License for more details.
36308 + *
36309 + * You should have received a copy of the GNU General Public License
36310 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36311 + */
36312 +
36313 +/*
36314 + * workqueue for asynchronous/super-io operations
36315 + * todo: try new credential scheme
36316 + */
36317 +
36318 +#include <linux/module.h>
36319 +#include "aufs.h"
36320 +
36321 +/* internal workqueue named AUFS_WKQ_NAME */
36322 +
36323 +static struct workqueue_struct *au_wkq;
36324 +
36325 +struct au_wkinfo {
36326 +       struct work_struct wk;
36327 +       struct kobject *kobj;
36328 +
36329 +       unsigned int flags; /* see wkq.h */
36330 +
36331 +       au_wkq_func_t func;
36332 +       void *args;
36333 +
36334 +#ifdef CONFIG_LOCKDEP
36335 +       int dont_check;
36336 +       struct held_lock **hlock;
36337 +#endif
36338 +
36339 +       struct completion *comp;
36340 +};
36341 +
36342 +/* ---------------------------------------------------------------------- */
36343 +/*
36344 + * Aufs passes some operations to the workqueue such as the internal copyup.
36345 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36346 + * job run by workqueue depends upon the locks acquired in the other task.
36347 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36348 + * information too. And the job in the workqueue restores the info in order to
36349 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36350 + * correctly and expectedly.
36351 + */
36352 +
36353 +#ifndef CONFIG_LOCKDEP
36354 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36355 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36356 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36357 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36358 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36359 +#else
36360 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36361 +{
36362 +       wkinfo->hlock = NULL;
36363 +       wkinfo->dont_check = 0;
36364 +}
36365 +
36366 +/*
36367 + * 1: matched
36368 + * 0: unmatched
36369 + */
36370 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36371 +{
36372 +       static DEFINE_SPINLOCK(spin);
36373 +       static struct {
36374 +               char *name;
36375 +               struct lock_class_key *key;
36376 +       } a[] = {
36377 +               { .name = "&sbinfo->si_rwsem" },
36378 +               { .name = "&finfo->fi_rwsem" },
36379 +               { .name = "&dinfo->di_rwsem" },
36380 +               { .name = "&iinfo->ii_rwsem" }
36381 +       };
36382 +       static int set;
36383 +       int i;
36384 +
36385 +       /* lockless read from 'set.' see below */
36386 +       if (set == ARRAY_SIZE(a)) {
36387 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36388 +                       if (a[i].key == key)
36389 +                               goto match;
36390 +               goto unmatch;
36391 +       }
36392 +
36393 +       spin_lock(&spin);
36394 +       if (set)
36395 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36396 +                       if (a[i].key == key) {
36397 +                               spin_unlock(&spin);
36398 +                               goto match;
36399 +                       }
36400 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36401 +               if (a[i].key) {
36402 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36403 +                               spin_unlock(&spin);
36404 +                               goto match;
36405 +                       } else
36406 +                               continue;
36407 +               }
36408 +               if (strstr(a[i].name, name)) {
36409 +                       /*
36410 +                        * the order of these three lines is important for the
36411 +                        * lockless read above.
36412 +                        */
36413 +                       a[i].key = key;
36414 +                       spin_unlock(&spin);
36415 +                       set++;
36416 +                       /* AuDbg("%d, %s\n", set, name); */
36417 +                       goto match;
36418 +               }
36419 +       }
36420 +       spin_unlock(&spin);
36421 +       goto unmatch;
36422 +
36423 +match:
36424 +       return 1;
36425 +unmatch:
36426 +       return 0;
36427 +}
36428 +
36429 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36430 +{
36431 +       int err, n;
36432 +       struct task_struct *curr;
36433 +       struct held_lock **hl, *held_locks, *p;
36434 +
36435 +       err = 0;
36436 +       curr = current;
36437 +       wkinfo->dont_check = lockdep_recursing(curr);
36438 +       if (wkinfo->dont_check)
36439 +               goto out;
36440 +       n = curr->lockdep_depth;
36441 +       if (!n)
36442 +               goto out;
36443 +
36444 +       err = -ENOMEM;
36445 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36446 +       if (unlikely(!wkinfo->hlock))
36447 +               goto out;
36448 +
36449 +       err = 0;
36450 +#if 0 /* left for debugging */
36451 +       if (0 && au_debug_test())
36452 +               lockdep_print_held_locks(curr);
36453 +#endif
36454 +       held_locks = curr->held_locks;
36455 +       hl = wkinfo->hlock;
36456 +       while (n--) {
36457 +               p = held_locks++;
36458 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36459 +                       *hl++ = p;
36460 +       }
36461 +       *hl = NULL;
36462 +
36463 +out:
36464 +       return err;
36465 +}
36466 +
36467 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36468 +{
36469 +       au_kfree_try_rcu(wkinfo->hlock);
36470 +}
36471 +
36472 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36473 +{
36474 +       struct held_lock *p, **hl = wkinfo->hlock;
36475 +       int subclass;
36476 +
36477 +       if (wkinfo->dont_check)
36478 +               lockdep_off();
36479 +       if (!hl)
36480 +               return;
36481 +       while ((p = *hl++)) { /* assignment */
36482 +               subclass = lockdep_hlock_class(p)->subclass;
36483 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36484 +               if (p->read)
36485 +                       rwsem_acquire_read(p->instance, subclass, 0,
36486 +                                          /*p->acquire_ip*/_RET_IP_);
36487 +               else
36488 +                       rwsem_acquire(p->instance, subclass, 0,
36489 +                                     /*p->acquire_ip*/_RET_IP_);
36490 +       }
36491 +}
36492 +
36493 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36494 +{
36495 +       struct held_lock *p, **hl = wkinfo->hlock;
36496 +
36497 +       if (wkinfo->dont_check)
36498 +               lockdep_on();
36499 +       if (!hl)
36500 +               return;
36501 +       while ((p = *hl++)) /* assignment */
36502 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36503 +}
36504 +#endif
36505 +
36506 +static void wkq_func(struct work_struct *wk)
36507 +{
36508 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36509 +
36510 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36511 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36512 +
36513 +       au_wkq_lockdep_pre(wkinfo);
36514 +       wkinfo->func(wkinfo->args);
36515 +       au_wkq_lockdep_post(wkinfo);
36516 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36517 +               complete(wkinfo->comp);
36518 +       else {
36519 +               kobject_put(wkinfo->kobj);
36520 +               module_put(THIS_MODULE); /* todo: ?? */
36521 +               au_kfree_rcu(wkinfo);
36522 +       }
36523 +}
36524 +
36525 +/*
36526 + * Since struct completion is large, try allocating it dynamically.
36527 + */
36528 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36529 +
36530 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36531 +{
36532 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36533 +       if (*comp) {
36534 +               init_completion(*comp);
36535 +               wkinfo->comp = *comp;
36536 +               return 0;
36537 +       }
36538 +       return -ENOMEM;
36539 +}
36540 +
36541 +static void au_wkq_comp_free(struct completion *comp)
36542 +{
36543 +       au_kfree_rcu(comp);
36544 +}
36545 +
36546 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36547 +{
36548 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36549 +               if (au_wkq_test()) {
36550 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36551 +                               " due to a dead dir by UDBA,"
36552 +                               " or async xino write?\n");
36553 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36554 +               }
36555 +       } else
36556 +               au_dbg_verify_kthread();
36557 +
36558 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36559 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36560 +               queue_work(au_wkq, &wkinfo->wk);
36561 +       } else {
36562 +               INIT_WORK(&wkinfo->wk, wkq_func);
36563 +               schedule_work(&wkinfo->wk);
36564 +       }
36565 +}
36566 +
36567 +/*
36568 + * Be careful. It is easy to make deadlock happen.
36569 + * processA: lock, wkq and wait
36570 + * processB: wkq and wait, lock in wkq
36571 + * --> deadlock
36572 + */
36573 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36574 +{
36575 +       int err;
36576 +       AuWkqCompDeclare(comp);
36577 +       struct au_wkinfo wkinfo = {
36578 +               .flags  = flags,
36579 +               .func   = func,
36580 +               .args   = args
36581 +       };
36582 +
36583 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36584 +       if (unlikely(err))
36585 +               goto out;
36586 +       err = au_wkq_lockdep_alloc(&wkinfo);
36587 +       if (unlikely(err))
36588 +               goto out_comp;
36589 +       if (!err) {
36590 +               au_wkq_run(&wkinfo);
36591 +               /* no timeout, no interrupt */
36592 +               wait_for_completion(wkinfo.comp);
36593 +       }
36594 +       au_wkq_lockdep_free(&wkinfo);
36595 +
36596 +out_comp:
36597 +       au_wkq_comp_free(comp);
36598 +out:
36599 +       destroy_work_on_stack(&wkinfo.wk);
36600 +       return err;
36601 +}
36602 +
36603 +/*
36604 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36605 + * problem in a concurrent umounting.
36606 + */
36607 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36608 +                 unsigned int flags)
36609 +{
36610 +       int err;
36611 +       struct au_wkinfo *wkinfo;
36612 +
36613 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36614 +
36615 +       /*
36616 +        * wkq_func() must free this wkinfo.
36617 +        * it highly depends upon the implementation of workqueue.
36618 +        */
36619 +       err = 0;
36620 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36621 +       if (wkinfo) {
36622 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36623 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36624 +               wkinfo->func = func;
36625 +               wkinfo->args = args;
36626 +               wkinfo->comp = NULL;
36627 +               au_wkq_lockdep_init(wkinfo);
36628 +               kobject_get(wkinfo->kobj);
36629 +               __module_get(THIS_MODULE); /* todo: ?? */
36630 +
36631 +               au_wkq_run(wkinfo);
36632 +       } else {
36633 +               err = -ENOMEM;
36634 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36635 +       }
36636 +
36637 +       return err;
36638 +}
36639 +
36640 +/* ---------------------------------------------------------------------- */
36641 +
36642 +void au_nwt_init(struct au_nowait_tasks *nwt)
36643 +{
36644 +       atomic_set(&nwt->nw_len, 0);
36645 +       /* smp_mb(); */ /* atomic_set */
36646 +       init_waitqueue_head(&nwt->nw_wq);
36647 +}
36648 +
36649 +void au_wkq_fin(void)
36650 +{
36651 +       destroy_workqueue(au_wkq);
36652 +}
36653 +
36654 +int __init au_wkq_init(void)
36655 +{
36656 +       int err;
36657 +
36658 +       err = 0;
36659 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36660 +       if (IS_ERR(au_wkq))
36661 +               err = PTR_ERR(au_wkq);
36662 +       else if (!au_wkq)
36663 +               err = -ENOMEM;
36664 +
36665 +       return err;
36666 +}
36667 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36668 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36669 +++ linux/fs/aufs/wkq.h 2022-11-05 23:02:18.972555950 +0100
36670 @@ -0,0 +1,89 @@
36671 +/* SPDX-License-Identifier: GPL-2.0 */
36672 +/*
36673 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36674 + *
36675 + * This program is free software; you can redistribute it and/or modify
36676 + * it under the terms of the GNU General Public License as published by
36677 + * the Free Software Foundation; either version 2 of the License, or
36678 + * (at your option) any later version.
36679 + *
36680 + * This program is distributed in the hope that it will be useful,
36681 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36682 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36683 + * GNU General Public License for more details.
36684 + *
36685 + * You should have received a copy of the GNU General Public License
36686 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36687 + */
36688 +
36689 +/*
36690 + * workqueue for asynchronous/super-io operations
36691 + * todo: try new credentials management scheme
36692 + */
36693 +
36694 +#ifndef __AUFS_WKQ_H__
36695 +#define __AUFS_WKQ_H__
36696 +
36697 +#ifdef __KERNEL__
36698 +
36699 +#include <linux/wait.h>
36700 +
36701 +struct super_block;
36702 +
36703 +/* ---------------------------------------------------------------------- */
36704 +
36705 +/*
36706 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36707 + */
36708 +struct au_nowait_tasks {
36709 +       atomic_t                nw_len;
36710 +       wait_queue_head_t       nw_wq;
36711 +};
36712 +
36713 +/* ---------------------------------------------------------------------- */
36714 +
36715 +typedef void (*au_wkq_func_t)(void *args);
36716 +
36717 +/* wkq flags */
36718 +#define AuWkq_WAIT     1
36719 +#define AuWkq_NEST     (1 << 1)
36720 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36721 +#define au_fset_wkq(flags, name) \
36722 +       do { (flags) |= AuWkq_##name; } while (0)
36723 +#define au_fclr_wkq(flags, name) \
36724 +       do { (flags) &= ~AuWkq_##name; } while (0)
36725 +
36726 +/* wkq.c */
36727 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36728 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36729 +                 unsigned int flags);
36730 +void au_nwt_init(struct au_nowait_tasks *nwt);
36731 +int __init au_wkq_init(void);
36732 +void au_wkq_fin(void);
36733 +
36734 +/* ---------------------------------------------------------------------- */
36735 +
36736 +static inline int au_wkq_test(void)
36737 +{
36738 +       return current->flags & PF_WQ_WORKER;
36739 +}
36740 +
36741 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36742 +{
36743 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36744 +}
36745 +
36746 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36747 +{
36748 +       if (atomic_dec_and_test(&nwt->nw_len))
36749 +               wake_up_all(&nwt->nw_wq);
36750 +}
36751 +
36752 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36753 +{
36754 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36755 +       return 0;
36756 +}
36757 +
36758 +#endif /* __KERNEL__ */
36759 +#endif /* __AUFS_WKQ_H__ */
36760 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36761 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36762 +++ linux/fs/aufs/xattr.c       2023-08-28 12:34:39.999969465 +0200
36763 @@ -0,0 +1,360 @@
36764 +// SPDX-License-Identifier: GPL-2.0
36765 +/*
36766 + * Copyright (C) 2014-2022 Junjiro R. Okajima
36767 + *
36768 + * This program is free software; you can redistribute it and/or modify
36769 + * it under the terms of the GNU General Public License as published by
36770 + * the Free Software Foundation; either version 2 of the License, or
36771 + * (at your option) any later version.
36772 + *
36773 + * This program is distributed in the hope that it will be useful,
36774 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36775 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36776 + * GNU General Public License for more details.
36777 + *
36778 + * You should have received a copy of the GNU General Public License
36779 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36780 + */
36781 +
36782 +/*
36783 + * handling xattr functions
36784 + */
36785 +
36786 +#include <linux/fs.h>
36787 +#include <linux/xattr.h>
36788 +#include "aufs.h"
36789 +
36790 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36791 +{
36792 +       if (!ignore_flags)
36793 +               goto out;
36794 +       switch (err) {
36795 +       case -ENOMEM:
36796 +       case -EDQUOT:
36797 +               goto out;
36798 +       }
36799 +
36800 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36801 +               err = 0;
36802 +               goto out;
36803 +       }
36804 +
36805 +#define cmp(brattr, prefix) do {                                       \
36806 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36807 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36808 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36809 +                               err = 0;                                \
36810 +                       goto out;                                       \
36811 +               }                                                       \
36812 +       } while (0)
36813 +
36814 +       cmp(SEC, SECURITY);
36815 +       cmp(SYS, SYSTEM);
36816 +       cmp(TR, TRUSTED);
36817 +       cmp(USR, USER);
36818 +#undef cmp
36819 +
36820 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36821 +               err = 0;
36822 +
36823 +out:
36824 +       return err;
36825 +}
36826 +
36827 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36828 +
36829 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36830 +                           char *name, char **buf, unsigned int ignore_flags,
36831 +                           unsigned int verbose)
36832 +{
36833 +       int err, is_acl;
36834 +       ssize_t ssz;
36835 +       struct inode *h_idst;
36836 +       struct dentry *h_dst_dentry, *h_src_dentry;
36837 +       struct mnt_idmap *h_dst_idmap, *h_src_idmap;
36838 +       struct posix_acl *acl;
36839 +
36840 +       is_acl = !!is_posix_acl_xattr(name);
36841 +       h_src_idmap = mnt_idmap(h_src->mnt);
36842 +       h_src_dentry = h_src->dentry;
36843 +       if (is_acl) {
36844 +               acl = vfs_get_acl(h_src_idmap, h_src_dentry, name);
36845 +               AuDebugOn(!acl);
36846 +               if (unlikely(IS_ERR(acl))) {
36847 +                       err = PTR_ERR(acl);
36848 +                       if (err == -ENODATA)
36849 +                               err = 0;
36850 +                       else if (err == -EOPNOTSUPP
36851 +                                && au_test_nfs_noacl(d_inode(h_src_dentry)))
36852 +                               err = 0;
36853 +                       else if (verbose || au_debug_test())
36854 +                               pr_err("%s, err %d\n", name, err);
36855 +                       goto out;
36856 +               }
36857 +       } else {
36858 +               ssz = vfs_getxattr_alloc(h_src_idmap, h_src_dentry, name, buf,
36859 +                                        0, GFP_NOFS);
36860 +               if (unlikely(ssz <= 0)) {
36861 +                       err = ssz;
36862 +                       if (err == -ENODATA)
36863 +                               err = 0;
36864 +                       else if (err == -EOPNOTSUPP
36865 +                                && (ignore_flags & au_xattr_out_of_list))
36866 +                                err = 0;
36867 +                       else if (err && (verbose || au_debug_test()))
36868 +                               pr_err("%s, err %d\n", name, err);
36869 +                       goto out;
36870 +               }
36871 +       }
36872 +
36873 +       /* unlock it temporary */
36874 +       h_dst_idmap = mnt_idmap(h_dst->mnt);
36875 +       h_dst_dentry = h_dst->dentry;
36876 +       h_idst = d_inode(h_dst_dentry);
36877 +       inode_unlock(h_idst);
36878 +       if (is_acl) {
36879 +               err = vfsub_set_acl(h_dst_idmap, h_dst_dentry, name, acl);
36880 +               posix_acl_release(acl);
36881 +       } else
36882 +               err = vfsub_setxattr(h_dst_idmap, h_dst_dentry, name, *buf,
36883 +                                    ssz, /*flags*/0);
36884 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36885 +       if (unlikely(err)) {
36886 +               if (verbose || au_debug_test())
36887 +                       pr_err("%s, err %d\n", name, err);
36888 +               err = au_xattr_ignore(err, name, ignore_flags);
36889 +       }
36890 +
36891 +out:
36892 +       return err;
36893 +}
36894 +
36895 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36896 +                 unsigned int verbose)
36897 +{
36898 +       int err, unlocked;
36899 +       ssize_t ssz;
36900 +       struct dentry *h_dst_dentry, *h_src_dentry;
36901 +       struct inode *h_isrc, *h_idst;
36902 +       char *value, *p, *o, *e;
36903 +
36904 +       /* try stopping to update the source inode while we are referencing */
36905 +       /* there should not be the parent-child relationship between them */
36906 +       h_dst_dentry = h_dst->dentry;
36907 +       h_idst = d_inode(h_dst_dentry);
36908 +       h_src_dentry = h_src->dentry;
36909 +       h_isrc = d_inode(h_src_dentry);
36910 +       inode_unlock(h_idst);
36911 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36912 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36913 +       unlocked = 0;
36914 +
36915 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36916 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36917 +       err = ssz;
36918 +       if (unlikely(err < 0)) {
36919 +               AuTraceErr(err);
36920 +               if (err == -ENODATA
36921 +                   || err == -EOPNOTSUPP)
36922 +                       err = 0;        /* ignore */
36923 +               goto out;
36924 +       }
36925 +
36926 +       err = 0;
36927 +       p = NULL;
36928 +       o = NULL;
36929 +       if (ssz) {
36930 +               err = -ENOMEM;
36931 +               p = kmalloc(ssz, GFP_NOFS);
36932 +               o = p;
36933 +               if (unlikely(!p))
36934 +                       goto out;
36935 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36936 +       }
36937 +       inode_unlock_shared(h_isrc);
36938 +       unlocked = 1;
36939 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36940 +       if (unlikely(err < 0))
36941 +               goto out_free;
36942 +
36943 +       err = 0;
36944 +       e = p + ssz;
36945 +       value = NULL;
36946 +       ignore_flags |= au_xattr_out_of_list;
36947 +       while (!err && p < e) {
36948 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36949 +                                      verbose);
36950 +               p += strlen(p) + 1;
36951 +       }
36952 +       au_kfree_try_rcu(value);
36953 +
36954 +out_free:
36955 +       au_kfree_try_rcu(o);
36956 +out:
36957 +       if (!unlocked)
36958 +               inode_unlock_shared(h_isrc);
36959 +       AuTraceErr(err);
36960 +       return err;
36961 +}
36962 +
36963 +/* ---------------------------------------------------------------------- */
36964 +
36965 +static int au_smack_reentering(struct super_block *sb)
36966 +{
36967 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36968 +       /*
36969 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36970 +        * i_op->getxattr(). ouch.
36971 +        */
36972 +       return si_pid_test(sb);
36973 +#else
36974 +       return 0;
36975 +#endif
36976 +}
36977 +
36978 +enum {
36979 +       AU_XATTR_LIST,
36980 +       AU_XATTR_GET
36981 +};
36982 +
36983 +struct au_lgxattr {
36984 +       int type;
36985 +       union {
36986 +               struct {
36987 +                       char    *list;
36988 +                       size_t  size;
36989 +               } list;
36990 +               struct {
36991 +                       const char      *name;
36992 +                       void            *value;
36993 +                       size_t          size;
36994 +               } get;
36995 +       } u;
36996 +};
36997 +
36998 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36999 +                         struct au_lgxattr *arg)
37000 +{
37001 +       ssize_t err;
37002 +       int reenter;
37003 +       struct path h_path;
37004 +       struct super_block *sb;
37005 +
37006 +       sb = dentry->d_sb;
37007 +       reenter = au_smack_reentering(sb);
37008 +       if (!reenter) {
37009 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
37010 +               if (unlikely(err))
37011 +                       goto out;
37012 +       }
37013 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
37014 +       if (unlikely(err))
37015 +               goto out_si;
37016 +       if (unlikely(!h_path.dentry))
37017 +               /* illegally overlapped or something */
37018 +               goto out_di; /* pretending success */
37019 +
37020 +       /* always topmost entry only */
37021 +       switch (arg->type) {
37022 +       case AU_XATTR_LIST:
37023 +               err = vfs_listxattr(h_path.dentry,
37024 +                                   arg->u.list.list, arg->u.list.size);
37025 +               break;
37026 +       case AU_XATTR_GET:
37027 +               AuDebugOn(d_is_negative(h_path.dentry));
37028 +               err = vfs_getxattr(mnt_idmap(h_path.mnt), h_path.dentry,
37029 +                                  arg->u.get.name, arg->u.get.value,
37030 +                                  arg->u.get.size);
37031 +               break;
37032 +       }
37033 +
37034 +out_di:
37035 +       if (!reenter)
37036 +               di_read_unlock(dentry, AuLock_IR);
37037 +out_si:
37038 +       if (!reenter)
37039 +               si_read_unlock(sb);
37040 +out:
37041 +       AuTraceErr(err);
37042 +       return err;
37043 +}
37044 +
37045 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
37046 +{
37047 +       struct au_lgxattr arg = {
37048 +               .type = AU_XATTR_LIST,
37049 +               .u.list = {
37050 +                       .list   = list,
37051 +                       .size   = size
37052 +               },
37053 +       };
37054 +
37055 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
37056 +}
37057 +
37058 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
37059 +                          const char *name, void *value, size_t size)
37060 +{
37061 +       struct au_lgxattr arg = {
37062 +               .type = AU_XATTR_GET,
37063 +               .u.get = {
37064 +                       .name   = name,
37065 +                       .value  = value,
37066 +                       .size   = size
37067 +               },
37068 +       };
37069 +
37070 +       return au_lgxattr(dentry, inode, &arg);
37071 +}
37072 +
37073 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
37074 +                      const char *name, const void *value, size_t size,
37075 +                      int flags)
37076 +{
37077 +       struct au_sxattr arg = {
37078 +               .type = AU_XATTR_SET,
37079 +               .u.set = {
37080 +                       .name   = name,
37081 +                       .value  = value,
37082 +                       .size   = size,
37083 +                       .flags  = flags
37084 +               },
37085 +       };
37086 +
37087 +       return au_sxattr(dentry, inode, &arg);
37088 +}
37089 +
37090 +/* ---------------------------------------------------------------------- */
37091 +
37092 +static int au_xattr_get(const struct xattr_handler *handler,
37093 +                       struct dentry *dentry, struct inode *inode,
37094 +                       const char *name, void *buffer, size_t size)
37095 +{
37096 +       return au_getxattr(dentry, inode, name, buffer, size);
37097 +}
37098 +
37099 +static int au_xattr_set(const struct xattr_handler *handler,
37100 +                       struct mnt_idmap *idmap,
37101 +                       struct dentry *dentry, struct inode *inode,
37102 +                       const char *name, const void *value, size_t size,
37103 +                       int flags)
37104 +{
37105 +       return au_setxattr(dentry, inode, name, value, size, flags);
37106 +}
37107 +
37108 +static const struct xattr_handler au_xattr_handler = {
37109 +       .name   = "",
37110 +       .prefix = "",
37111 +       .get    = au_xattr_get,
37112 +       .set    = au_xattr_set
37113 +};
37114 +
37115 +static const struct xattr_handler *au_xattr_handlers[] = {
37116 +       &au_xattr_handler, /* must be last */
37117 +       NULL
37118 +};
37119 +
37120 +void au_xattr_init(struct super_block *sb)
37121 +{
37122 +       sb->s_xattr = au_xattr_handlers;
37123 +}
37124 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
37125 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
37126 +++ linux/fs/aufs/xino.c        2023-08-28 12:34:39.999969465 +0200
37127 @@ -0,0 +1,1926 @@
37128 +// SPDX-License-Identifier: GPL-2.0
37129 +/*
37130 + * Copyright (C) 2005-2022 Junjiro R. Okajima
37131 + *
37132 + * This program is free software; you can redistribute it and/or modify
37133 + * it under the terms of the GNU General Public License as published by
37134 + * the Free Software Foundation; either version 2 of the License, or
37135 + * (at your option) any later version.
37136 + *
37137 + * This program is distributed in the hope that it will be useful,
37138 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37139 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37140 + * GNU General Public License for more details.
37141 + *
37142 + * You should have received a copy of the GNU General Public License
37143 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37144 + */
37145 +
37146 +/*
37147 + * external inode number translation table and bitmap
37148 + *
37149 + * things to consider
37150 + * - the lifetime
37151 + *   + au_xino object
37152 + *   + XINO files (xino, xib, xigen)
37153 + *   + dynamic debugfs entries (xiN)
37154 + *   + static debugfs entries (xib, xigen)
37155 + *   + static sysfs entry (xi_path)
37156 + * - several entry points to handle them.
37157 + *   + mount(2) without xino option (default)
37158 + *   + mount(2) with xino option
37159 + *   + mount(2) with noxino option
37160 + *   + umount(2)
37161 + *   + remount with add/del branches
37162 + *   + remount with xino/noxino options
37163 + */
37164 +
37165 +#include <linux/seq_file.h>
37166 +#include <linux/statfs.h>
37167 +#include "aufs.h"
37168 +
37169 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37170 +                                    aufs_bindex_t bbot,
37171 +                                    struct super_block *h_sb)
37172 +{
37173 +       /* todo: try binary-search if the branches are many */
37174 +       for (; btop <= bbot; btop++)
37175 +               if (h_sb == au_sbr_sb(sb, btop))
37176 +                       return btop;
37177 +       return -1;
37178 +}
37179 +
37180 +/*
37181 + * find another branch who is on the same filesystem of the specified
37182 + * branch{@btgt}. search until @bbot.
37183 + */
37184 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37185 +                                 aufs_bindex_t bbot)
37186 +{
37187 +       aufs_bindex_t bindex;
37188 +       struct super_block *tgt_sb;
37189 +
37190 +       tgt_sb = au_sbr_sb(sb, btgt);
37191 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37192 +       if (bindex < 0)
37193 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
37194 +
37195 +       return bindex;
37196 +}
37197 +
37198 +/* ---------------------------------------------------------------------- */
37199 +
37200 +/*
37201 + * stop unnecessary notify events at creating xino files
37202 + */
37203 +
37204 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37205 +{
37206 +       aufs_bindex_t bfound, bindex, bbot;
37207 +       struct dentry *parent;
37208 +       struct au_branch *br;
37209 +
37210 +       bfound = -1;
37211 +       parent = dentry->d_parent; /* safe d_parent access */
37212 +       bbot = au_sbbot(sb);
37213 +       for (bindex = 0; bindex <= bbot; bindex++) {
37214 +               br = au_sbr(sb, bindex);
37215 +               if (au_br_dentry(br) == parent) {
37216 +                       bfound = bindex;
37217 +                       break;
37218 +               }
37219 +       }
37220 +
37221 +       AuDbg("bfound b%d\n", bfound);
37222 +       return bfound;
37223 +}
37224 +
37225 +struct au_xino_lock_dir {
37226 +       struct au_hinode *hdir;
37227 +       struct dentry *parent;
37228 +       struct inode *dir;
37229 +};
37230 +
37231 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37232 +                                         unsigned int lsc)
37233 +{
37234 +       struct dentry *parent;
37235 +       struct inode *dir;
37236 +
37237 +       parent = dget_parent(dentry);
37238 +       dir = d_inode(parent);
37239 +       inode_lock_nested(dir, lsc);
37240 +#if 0 /* it should not happen */
37241 +       spin_lock(&dentry->d_lock);
37242 +       if (unlikely(dentry->d_parent != parent)) {
37243 +               spin_unlock(&dentry->d_lock);
37244 +               inode_unlock(dir);
37245 +               dput(parent);
37246 +               parent = NULL;
37247 +               goto out;
37248 +       }
37249 +       spin_unlock(&dentry->d_lock);
37250 +
37251 +out:
37252 +#endif
37253 +       return parent;
37254 +}
37255 +
37256 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37257 +                            struct au_xino_lock_dir *ldir)
37258 +{
37259 +       aufs_bindex_t bindex;
37260 +
37261 +       ldir->hdir = NULL;
37262 +       bindex = au_xi_root(sb, xipath->dentry);
37263 +       if (bindex >= 0) {
37264 +               /* rw branch root */
37265 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37266 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37267 +       } else {
37268 +               /* other */
37269 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37270 +                                                  AuLsc_I_PARENT);
37271 +               ldir->dir = d_inode(ldir->parent);
37272 +       }
37273 +}
37274 +
37275 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37276 +{
37277 +       if (ldir->hdir)
37278 +               au_hn_inode_unlock(ldir->hdir);
37279 +       else {
37280 +               inode_unlock(ldir->dir);
37281 +               dput(ldir->parent);
37282 +       }
37283 +}
37284 +
37285 +/* ---------------------------------------------------------------------- */
37286 +
37287 +/*
37288 + * create and set a new xino file
37289 + */
37290 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37291 +                           int wbrtop)
37292 +{
37293 +       struct file *file;
37294 +       struct dentry *h_parent, *d;
37295 +       struct inode *h_dir, *inode;
37296 +       int err;
37297 +       static DEFINE_MUTEX(mtx);
37298 +
37299 +       /*
37300 +        * at mount-time, and the xino file is the default path,
37301 +        * hnotify is disabled so we have no notify events to ignore.
37302 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37303 +        */
37304 +       if (!wbrtop)
37305 +               mutex_lock(&mtx);
37306 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37307 +                              /* | __FMODE_NONOTIFY */,
37308 +                              0666);
37309 +       if (IS_ERR(file)) {
37310 +               if (!wbrtop)
37311 +                       mutex_unlock(&mtx);
37312 +               if (!silent)
37313 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37314 +               return file;
37315 +       }
37316 +
37317 +       /* keep file count */
37318 +       err = 0;
37319 +       d = file->f_path.dentry;
37320 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37321 +       if (!wbrtop)
37322 +               mutex_unlock(&mtx);
37323 +       /* mnt_want_write() is unnecessary here */
37324 +       h_dir = d_inode(h_parent);
37325 +       inode = file_inode(file);
37326 +       /* no delegation since it is just created */
37327 +       if (inode->i_nlink)
37328 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37329 +                                  /*force*/0);
37330 +       inode_unlock(h_dir);
37331 +       dput(h_parent);
37332 +       if (unlikely(err)) {
37333 +               if (!silent)
37334 +                       pr_err("unlink %s(%d)\n", fpath, err);
37335 +               goto out;
37336 +       }
37337 +
37338 +       err = -EINVAL;
37339 +       if (unlikely(sb && sb == d->d_sb)) {
37340 +               if (!silent)
37341 +                       pr_err("%s must be outside\n", fpath);
37342 +               goto out;
37343 +       }
37344 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37345 +               if (!silent)
37346 +                       pr_err("xino doesn't support %s(%s)\n",
37347 +                              fpath, au_sbtype(d->d_sb));
37348 +               goto out;
37349 +       }
37350 +       return file; /* success */
37351 +
37352 +out:
37353 +       fput(file);
37354 +       file = ERR_PTR(err);
37355 +       return file;
37356 +}
37357 +
37358 +/*
37359 + * create a new xinofile at the same place/path as @base.
37360 + */
37361 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37362 +                            struct file *copy_src)
37363 +{
37364 +       struct file *file;
37365 +       struct dentry *dentry;
37366 +       struct inode *dir, *delegated;
37367 +       struct qstr *name;
37368 +       struct path ppath, path;
37369 +       int err, do_unlock;
37370 +       struct au_xino_lock_dir ldir;
37371 +
37372 +       do_unlock = 1;
37373 +       au_xino_lock_dir(sb, base, &ldir);
37374 +       dentry = base->dentry;
37375 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37376 +       ppath.mnt = base->mnt;
37377 +       dir = d_inode(ppath.dentry);
37378 +       IMustLock(dir);
37379 +
37380 +       name = &dentry->d_name;
37381 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37382 +       if (IS_ERR(path.dentry)) {
37383 +               file = (void *)path.dentry;
37384 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37385 +               goto out;
37386 +       }
37387 +
37388 +       /* no need to mnt_want_write() since we call dentry_open() later */
37389 +       err = vfs_create(mnt_idmap(base->mnt), dir, path.dentry, 0666, NULL);
37390 +       if (unlikely(err)) {
37391 +               file = ERR_PTR(err);
37392 +               pr_err("%pd create err %d\n", dentry, err);
37393 +               goto out_dput;
37394 +       }
37395 +
37396 +       path.mnt = base->mnt;
37397 +       file = vfsub_dentry_open(&path,
37398 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37399 +                                /* | __FMODE_NONOTIFY */);
37400 +       if (IS_ERR(file)) {
37401 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37402 +               goto out_dput;
37403 +       }
37404 +
37405 +       delegated = NULL;
37406 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37407 +       au_xino_unlock_dir(&ldir);
37408 +       do_unlock = 0;
37409 +       if (unlikely(err == -EWOULDBLOCK)) {
37410 +               pr_warn("cannot retry for NFSv4 delegation"
37411 +                       " for an internal unlink\n");
37412 +               iput(delegated);
37413 +       }
37414 +       if (unlikely(err)) {
37415 +               pr_err("%pd unlink err %d\n", dentry, err);
37416 +               goto out_fput;
37417 +       }
37418 +
37419 +       if (copy_src) {
37420 +               /* no one can touch copy_src xino */
37421 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37422 +               if (unlikely(err)) {
37423 +                       pr_err("%pd copy err %d\n", dentry, err);
37424 +                       goto out_fput;
37425 +               }
37426 +       }
37427 +       goto out_dput; /* success */
37428 +
37429 +out_fput:
37430 +       fput(file);
37431 +       file = ERR_PTR(err);
37432 +out_dput:
37433 +       dput(path.dentry);
37434 +out:
37435 +       if (do_unlock)
37436 +               au_xino_unlock_dir(&ldir);
37437 +       return file;
37438 +}
37439 +
37440 +struct file *au_xino_file1(struct au_xino *xi)
37441 +{
37442 +       struct file *file;
37443 +       unsigned int u, nfile;
37444 +
37445 +       file = NULL;
37446 +       nfile = xi->xi_nfile;
37447 +       for (u = 0; u < nfile; u++) {
37448 +               file = xi->xi_file[u];
37449 +               if (file)
37450 +                       break;
37451 +       }
37452 +
37453 +       return file;
37454 +}
37455 +
37456 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37457 +{
37458 +       int err;
37459 +       struct file *f;
37460 +       void *p;
37461 +
37462 +       if (file)
37463 +               get_file(file);
37464 +
37465 +       err = 0;
37466 +       f = NULL;
37467 +       if (idx < xi->xi_nfile) {
37468 +               f = xi->xi_file[idx];
37469 +               if (f)
37470 +                       fput(f);
37471 +       } else {
37472 +               p = au_kzrealloc(xi->xi_file,
37473 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37474 +                                sizeof(*xi->xi_file) * (idx + 1),
37475 +                                GFP_NOFS, /*may_shrink*/0);
37476 +               if (p) {
37477 +                       MtxMustLock(&xi->xi_mtx);
37478 +                       xi->xi_file = p;
37479 +                       xi->xi_nfile = idx + 1;
37480 +               } else {
37481 +                       err = -ENOMEM;
37482 +                       if (file)
37483 +                               fput(file);
37484 +                       goto out;
37485 +               }
37486 +       }
37487 +       xi->xi_file[idx] = file;
37488 +
37489 +out:
37490 +       return err;
37491 +}
37492 +
37493 +/*
37494 + * if @xinew->xi is not set, then create new xigen file.
37495 + */
37496 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37497 +{
37498 +       struct file *file;
37499 +       int err;
37500 +
37501 +       SiMustAnyLock(sb);
37502 +
37503 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37504 +       if (IS_ERR(file)) {
37505 +               err = PTR_ERR(file);
37506 +               pr_err("%s[%d], err %d\n",
37507 +                      xinew->xi ? "xino" : "xigen",
37508 +                      xinew->idx, err);
37509 +               goto out;
37510 +       }
37511 +
37512 +       if (xinew->xi)
37513 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37514 +       else {
37515 +               BUG();
37516 +               /* todo: make xigen file an array */
37517 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37518 +       }
37519 +       fput(file);
37520 +       if (unlikely(err))
37521 +               file = ERR_PTR(err);
37522 +
37523 +out:
37524 +       return file;
37525 +}
37526 +
37527 +/* ---------------------------------------------------------------------- */
37528 +
37529 +/*
37530 + * truncate xino files
37531 + */
37532 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37533 +                           int idx, struct kstatfs *st)
37534 +{
37535 +       int err;
37536 +       blkcnt_t blocks;
37537 +       struct file *file, *new_xino;
37538 +       struct au_xi_new xinew = {
37539 +               .idx = idx
37540 +       };
37541 +
37542 +       err = 0;
37543 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37544 +       file = au_xino_file(xinew.xi, idx);
37545 +       if (!file)
37546 +               goto out;
37547 +
37548 +       xinew.base = &file->f_path;
37549 +       err = vfs_statfs(xinew.base, st);
37550 +       if (unlikely(err)) {
37551 +               AuErr1("statfs err %d, ignored\n", err);
37552 +               err = 0;
37553 +               goto out;
37554 +       }
37555 +
37556 +       blocks = file_inode(file)->i_blocks;
37557 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37558 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37559 +
37560 +       xinew.copy_src = file;
37561 +       new_xino = au_xi_new(sb, &xinew);
37562 +       if (IS_ERR(new_xino)) {
37563 +               err = PTR_ERR(new_xino);
37564 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37565 +               goto out;
37566 +       }
37567 +
37568 +       err = vfs_statfs(&new_xino->f_path, st);
37569 +       if (!err)
37570 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37571 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37572 +                       st->f_bfree, st->f_blocks);
37573 +       else {
37574 +               AuErr1("statfs err %d, ignored\n", err);
37575 +               err = 0;
37576 +       }
37577 +
37578 +out:
37579 +       return err;
37580 +}
37581 +
37582 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37583 +{
37584 +       int err, i;
37585 +       unsigned long jiffy;
37586 +       aufs_bindex_t bbot;
37587 +       struct kstatfs *st;
37588 +       struct au_branch *br;
37589 +       struct au_xino *xi;
37590 +
37591 +       err = -ENOMEM;
37592 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37593 +       if (unlikely(!st))
37594 +               goto out;
37595 +
37596 +       err = -EINVAL;
37597 +       bbot = au_sbbot(sb);
37598 +       if (unlikely(bindex < 0 || bbot < bindex))
37599 +               goto out_st;
37600 +
37601 +       err = 0;
37602 +       jiffy = jiffies;
37603 +       br = au_sbr(sb, bindex);
37604 +       xi = br->br_xino;
37605 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37606 +               err = au_xino_do_trunc(sb, bindex, i, st);
37607 +       if (!err)
37608 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37609 +
37610 +out_st:
37611 +       au_kfree_rcu(st);
37612 +out:
37613 +       return err;
37614 +}
37615 +
37616 +struct xino_do_trunc_args {
37617 +       struct super_block *sb;
37618 +       struct au_branch *br;
37619 +       int idx;
37620 +};
37621 +
37622 +static void xino_do_trunc(void *_args)
37623 +{
37624 +       struct xino_do_trunc_args *args = _args;
37625 +       struct super_block *sb;
37626 +       struct au_branch *br;
37627 +       struct inode *dir;
37628 +       int err, idx;
37629 +       aufs_bindex_t bindex;
37630 +
37631 +       err = 0;
37632 +       sb = args->sb;
37633 +       dir = d_inode(sb->s_root);
37634 +       br = args->br;
37635 +       idx = args->idx;
37636 +
37637 +       si_noflush_write_lock(sb);
37638 +       ii_read_lock_parent(dir);
37639 +       bindex = au_br_index(sb, br->br_id);
37640 +       err = au_xino_trunc(sb, bindex, idx);
37641 +       ii_read_unlock(dir);
37642 +       if (unlikely(err))
37643 +               pr_warn("err b%d, (%d)\n", bindex, err);
37644 +       atomic_dec(&br->br_xino->xi_truncating);
37645 +       au_lcnt_dec(&br->br_count);
37646 +       si_write_unlock(sb);
37647 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37648 +       au_kfree_rcu(args);
37649 +}
37650 +
37651 +/*
37652 + * returns the index in the xi_file array whose corresponding file is necessary
37653 + * to truncate, or -1 which means no need to truncate.
37654 + */
37655 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37656 +{
37657 +       int err;
37658 +       unsigned int u;
37659 +       struct kstatfs st;
37660 +       struct au_sbinfo *sbinfo;
37661 +       struct au_xino *xi;
37662 +       struct file *file;
37663 +
37664 +       /* todo: si_xino_expire and the ratio should be customizable */
37665 +       sbinfo = au_sbi(sb);
37666 +       if (time_before(jiffies,
37667 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37668 +               return -1;
37669 +
37670 +       /* truncation border */
37671 +       xi = br->br_xino;
37672 +       for (u = 0; u < xi->xi_nfile; u++) {
37673 +               file = au_xino_file(xi, u);
37674 +               if (!file)
37675 +                       continue;
37676 +
37677 +               err = vfs_statfs(&file->f_path, &st);
37678 +               if (unlikely(err)) {
37679 +                       AuErr1("statfs err %d, ignored\n", err);
37680 +                       return -1;
37681 +               }
37682 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37683 +                   >= AUFS_XINO_DEF_TRUNC)
37684 +                       return u;
37685 +       }
37686 +
37687 +       return -1;
37688 +}
37689 +
37690 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37691 +{
37692 +       int idx;
37693 +       struct xino_do_trunc_args *args;
37694 +       int wkq_err;
37695 +
37696 +       idx = xino_trunc_test(sb, br);
37697 +       if (idx < 0)
37698 +               return;
37699 +
37700 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37701 +               goto out;
37702 +
37703 +       /* lock and kfree() will be called in trunc_xino() */
37704 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37705 +       if (unlikely(!args)) {
37706 +               AuErr1("no memory\n");
37707 +               goto out;
37708 +       }
37709 +
37710 +       au_lcnt_inc(&br->br_count);
37711 +       args->sb = sb;
37712 +       args->br = br;
37713 +       args->idx = idx;
37714 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37715 +       if (!wkq_err)
37716 +               return; /* success */
37717 +
37718 +       pr_err("wkq %d\n", wkq_err);
37719 +       au_lcnt_dec(&br->br_count);
37720 +       au_kfree_rcu(args);
37721 +
37722 +out:
37723 +       atomic_dec(&br->br_xino->xi_truncating);
37724 +}
37725 +
37726 +/* ---------------------------------------------------------------------- */
37727 +
37728 +struct au_xi_calc {
37729 +       int idx;
37730 +       loff_t pos;
37731 +};
37732 +
37733 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37734 +                      struct au_xi_calc *calc)
37735 +{
37736 +       loff_t maxent;
37737 +
37738 +       maxent = au_xi_maxent(sb);
37739 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37740 +       calc->pos *= sizeof(ino_t);
37741 +}
37742 +
37743 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37744 +                               struct au_xi_calc *calc)
37745 +{
37746 +       int err;
37747 +       struct file *file;
37748 +       struct au_xino *xi = br->br_xino;
37749 +       struct au_xi_new xinew = {
37750 +               .xi = xi
37751 +       };
37752 +
37753 +       SiMustAnyLock(sb);
37754 +
37755 +       err = 0;
37756 +       if (!xi)
37757 +               goto out;
37758 +
37759 +       mutex_lock(&xi->xi_mtx);
37760 +       file = au_xino_file(xi, calc->idx);
37761 +       if (file)
37762 +               goto out_mtx;
37763 +
37764 +       file = au_xino_file(xi, /*idx*/-1);
37765 +       AuDebugOn(!file);
37766 +       xinew.idx = calc->idx;
37767 +       xinew.base = &file->f_path;
37768 +       /* xinew.copy_src = NULL; */
37769 +       file = au_xi_new(sb, &xinew);
37770 +       if (IS_ERR(file))
37771 +               err = PTR_ERR(file);
37772 +
37773 +out_mtx:
37774 +       mutex_unlock(&xi->xi_mtx);
37775 +out:
37776 +       return err;
37777 +}
37778 +
37779 +struct au_xino_do_new_async_args {
37780 +       struct super_block *sb;
37781 +       struct au_branch *br;
37782 +       struct au_xi_calc calc;
37783 +       ino_t ino;
37784 +};
37785 +
37786 +struct au_xi_writing {
37787 +       struct hlist_bl_node node;
37788 +       ino_t h_ino, ino;
37789 +};
37790 +
37791 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37792 +                           ino_t ino);
37793 +
37794 +static void au_xino_call_do_new_async(void *args)
37795 +{
37796 +       struct au_xino_do_new_async_args *a = args;
37797 +       struct au_branch *br;
37798 +       struct super_block *sb;
37799 +       struct au_sbinfo *sbi;
37800 +       struct inode *root;
37801 +       struct file *file;
37802 +       struct au_xi_writing *del, *p;
37803 +       struct hlist_bl_head *hbl;
37804 +       struct hlist_bl_node *pos;
37805 +       int err;
37806 +
37807 +       br = a->br;
37808 +       sb = a->sb;
37809 +       sbi = au_sbi(sb);
37810 +       si_noflush_read_lock(sb);
37811 +       root = d_inode(sb->s_root);
37812 +       ii_read_lock_child(root);
37813 +       err = au_xino_do_new_async(sb, br, &a->calc);
37814 +       if (unlikely(err)) {
37815 +               AuIOErr("err %d\n", err);
37816 +               goto out;
37817 +       }
37818 +
37819 +       file = au_xino_file(br->br_xino, a->calc.idx);
37820 +       AuDebugOn(!file);
37821 +       err = au_xino_do_write(file, &a->calc, a->ino);
37822 +       if (unlikely(err)) {
37823 +               AuIOErr("err %d\n", err);
37824 +               goto out;
37825 +       }
37826 +
37827 +       del = NULL;
37828 +       hbl = &br->br_xino->xi_writing;
37829 +       hlist_bl_lock(hbl);
37830 +       au_hbl_for_each(pos, hbl) {
37831 +               p = container_of(pos, struct au_xi_writing, node);
37832 +               if (p->ino == a->ino) {
37833 +                       del = p;
37834 +                       hlist_bl_del(&p->node);
37835 +                       break;
37836 +               }
37837 +       }
37838 +       hlist_bl_unlock(hbl);
37839 +       au_kfree_rcu(del);
37840 +
37841 +out:
37842 +       au_lcnt_dec(&br->br_count);
37843 +       ii_read_unlock(root);
37844 +       si_read_unlock(sb);
37845 +       au_nwt_done(&sbi->si_nowait);
37846 +       au_kfree_rcu(a);
37847 +}
37848 +
37849 +/*
37850 + * create a new xino file asynchronously
37851 + */
37852 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37853 +                            struct au_xi_calc *calc, ino_t ino)
37854 +{
37855 +       int err;
37856 +       struct au_xino_do_new_async_args *arg;
37857 +
37858 +       err = -ENOMEM;
37859 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37860 +       if (unlikely(!arg))
37861 +               goto out;
37862 +
37863 +       arg->sb = sb;
37864 +       arg->br = br;
37865 +       arg->calc = *calc;
37866 +       arg->ino = ino;
37867 +       au_lcnt_inc(&br->br_count);
37868 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37869 +       if (unlikely(err)) {
37870 +               pr_err("wkq %d\n", err);
37871 +               au_lcnt_dec(&br->br_count);
37872 +               au_kfree_rcu(arg);
37873 +       }
37874 +
37875 +out:
37876 +       return err;
37877 +}
37878 +
37879 +/*
37880 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37881 + * at the position of @h_ino.
37882 + */
37883 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37884 +                ino_t *ino)
37885 +{
37886 +       int err;
37887 +       ssize_t sz;
37888 +       struct au_xi_calc calc;
37889 +       struct au_sbinfo *sbinfo;
37890 +       struct file *file;
37891 +       struct au_xino *xi;
37892 +       struct hlist_bl_head *hbl;
37893 +       struct hlist_bl_node *pos;
37894 +       struct au_xi_writing *p;
37895 +
37896 +       *ino = 0;
37897 +       if (!au_opt_test(au_mntflags(sb), XINO))
37898 +               return 0; /* no xino */
37899 +
37900 +       err = 0;
37901 +       au_xi_calc(sb, h_ino, &calc);
37902 +       xi = au_sbr(sb, bindex)->br_xino;
37903 +       file = au_xino_file(xi, calc.idx);
37904 +       if (!file) {
37905 +               hbl = &xi->xi_writing;
37906 +               hlist_bl_lock(hbl);
37907 +               au_hbl_for_each(pos, hbl) {
37908 +                       p = container_of(pos, struct au_xi_writing, node);
37909 +                       if (p->h_ino == h_ino) {
37910 +                               AuDbg("hi%llu, i%llu, found\n",
37911 +                                     (u64)p->h_ino, (u64)p->ino);
37912 +                               *ino = p->ino;
37913 +                               break;
37914 +                       }
37915 +               }
37916 +               hlist_bl_unlock(hbl);
37917 +               return 0;
37918 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37919 +               return 0; /* no xino */
37920 +
37921 +       sbinfo = au_sbi(sb);
37922 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37923 +       if (sz == sizeof(*ino))
37924 +               return 0; /* success */
37925 +
37926 +       err = sz;
37927 +       if (unlikely(sz >= 0)) {
37928 +               err = -EIO;
37929 +               AuIOErr("xino read error (%zd)\n", sz);
37930 +       }
37931 +       return err;
37932 +}
37933 +
37934 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37935 +                           ino_t ino)
37936 +{
37937 +       ssize_t sz;
37938 +
37939 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37940 +       if (sz == sizeof(ino))
37941 +               return 0; /* success */
37942 +
37943 +       AuIOErr("write failed (%zd)\n", sz);
37944 +       return -EIO;
37945 +}
37946 +
37947 +/*
37948 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37949 + * at the position of @h_ino.
37950 + * even if @ino is zero, it is written to the xinofile and means no entry.
37951 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37952 + * try truncating it.
37953 + */
37954 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37955 +                 ino_t ino)
37956 +{
37957 +       int err;
37958 +       unsigned int mnt_flags;
37959 +       struct au_xi_calc calc;
37960 +       struct file *file;
37961 +       struct au_branch *br;
37962 +       struct au_xino *xi;
37963 +       struct au_xi_writing *p;
37964 +
37965 +       SiMustAnyLock(sb);
37966 +
37967 +       mnt_flags = au_mntflags(sb);
37968 +       if (!au_opt_test(mnt_flags, XINO))
37969 +               return 0;
37970 +
37971 +       au_xi_calc(sb, h_ino, &calc);
37972 +       br = au_sbr(sb, bindex);
37973 +       xi = br->br_xino;
37974 +       file = au_xino_file(xi, calc.idx);
37975 +       if (!file) {
37976 +               /* store the inum pair into the list */
37977 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37978 +               p->h_ino = h_ino;
37979 +               p->ino = ino;
37980 +               au_hbl_add(&p->node, &xi->xi_writing);
37981 +
37982 +               /* create and write a new xino file asynchronously */
37983 +               err = au_xino_new_async(sb, br, &calc, ino);
37984 +               if (!err)
37985 +                       return 0; /* success */
37986 +               goto out;
37987 +       }
37988 +
37989 +       err = au_xino_do_write(file, &calc, ino);
37990 +       if (!err) {
37991 +               br = au_sbr(sb, bindex);
37992 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37993 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37994 +                       xino_try_trunc(sb, br);
37995 +               return 0; /* success */
37996 +       }
37997 +
37998 +out:
37999 +       AuIOErr("write failed (%d)\n", err);
38000 +       return -EIO;
38001 +}
38002 +
38003 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38004 +                             loff_t *pos);
38005 +
38006 +/* todo: unnecessary to support mmap_sem since kernel-space? */
38007 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
38008 +{
38009 +       ssize_t err;
38010 +       int i;
38011 +       const int prevent_endless = 10;
38012 +
38013 +       i = 0;
38014 +       do {
38015 +               err = vfsub_read_k(file, kbuf, size, pos);
38016 +               if (err == -EINTR
38017 +                   && !au_wkq_test()
38018 +                   && fatal_signal_pending(current)) {
38019 +                       err = xino_fread_wkq(file, kbuf, size, pos);
38020 +                       BUG_ON(err == -EINTR);
38021 +               }
38022 +       } while (i++ < prevent_endless
38023 +                && (err == -EAGAIN || err == -EINTR));
38024 +
38025 +#if 0 /* reserved for future use */
38026 +       if (err > 0)
38027 +               fsnotify_access(file->f_path.dentry);
38028 +#endif
38029 +
38030 +       return err;
38031 +}
38032 +
38033 +struct xino_fread_args {
38034 +       ssize_t *errp;
38035 +       struct file *file;
38036 +       void *buf;
38037 +       size_t size;
38038 +       loff_t *pos;
38039 +};
38040 +
38041 +static void call_xino_fread(void *args)
38042 +{
38043 +       struct xino_fread_args *a = args;
38044 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
38045 +}
38046 +
38047 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38048 +                             loff_t *pos)
38049 +{
38050 +       ssize_t err;
38051 +       int wkq_err;
38052 +       struct xino_fread_args args = {
38053 +               .errp   = &err,
38054 +               .file   = file,
38055 +               .buf    = buf,
38056 +               .size   = size,
38057 +               .pos    = pos
38058 +       };
38059 +
38060 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
38061 +       if (unlikely(wkq_err))
38062 +               err = wkq_err;
38063 +
38064 +       return err;
38065 +}
38066 +
38067 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38068 +                              loff_t *pos);
38069 +
38070 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
38071 +                             loff_t *pos)
38072 +{
38073 +       ssize_t err;
38074 +       int i;
38075 +       const int prevent_endless = 10;
38076 +
38077 +       i = 0;
38078 +       do {
38079 +               err = vfsub_write_k(file, kbuf, size, pos);
38080 +               if (err == -EINTR
38081 +                   && !au_wkq_test()
38082 +                   && fatal_signal_pending(current)) {
38083 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
38084 +                       BUG_ON(err == -EINTR);
38085 +               }
38086 +       } while (i++ < prevent_endless
38087 +                && (err == -EAGAIN || err == -EINTR));
38088 +
38089 +#if 0 /* reserved for future use */
38090 +       if (err > 0)
38091 +               fsnotify_modify(file->f_path.dentry);
38092 +#endif
38093 +
38094 +       return err;
38095 +}
38096 +
38097 +struct do_xino_fwrite_args {
38098 +       ssize_t *errp;
38099 +       struct file *file;
38100 +       void *buf;
38101 +       size_t size;
38102 +       loff_t *pos;
38103 +};
38104 +
38105 +static void call_do_xino_fwrite(void *args)
38106 +{
38107 +       struct do_xino_fwrite_args *a = args;
38108 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
38109 +}
38110 +
38111 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38112 +                              loff_t *pos)
38113 +{
38114 +       ssize_t err;
38115 +       int wkq_err;
38116 +       struct do_xino_fwrite_args args = {
38117 +               .errp   = &err,
38118 +               .file   = file,
38119 +               .buf    = buf,
38120 +               .size   = size,
38121 +               .pos    = pos
38122 +       };
38123 +
38124 +       /*
38125 +        * it breaks RLIMIT_FSIZE and normal user's limit,
38126 +        * users should care about quota and real 'filesystem full.'
38127 +        */
38128 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38129 +       if (unlikely(wkq_err))
38130 +               err = wkq_err;
38131 +
38132 +       return err;
38133 +}
38134 +
38135 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
38136 +{
38137 +       ssize_t err;
38138 +
38139 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38140 +               lockdep_off();
38141 +               err = do_xino_fwrite(file, buf, size, pos);
38142 +               lockdep_on();
38143 +       } else {
38144 +               lockdep_off();
38145 +               err = xino_fwrite_wkq(file, buf, size, pos);
38146 +               lockdep_on();
38147 +       }
38148 +
38149 +       return err;
38150 +}
38151 +
38152 +/* ---------------------------------------------------------------------- */
38153 +
38154 +/*
38155 + * inode number bitmap
38156 + */
38157 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38158 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
38159 +{
38160 +       ino_t ino;
38161 +
38162 +       AuDebugOn(bit < 0 || page_bits <= bit);
38163 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38164 +       return ino;
38165 +}
38166 +
38167 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38168 +{
38169 +       AuDebugOn(ino < AUFS_FIRST_INO);
38170 +       ino -= AUFS_FIRST_INO;
38171 +       *pindex = ino / page_bits;
38172 +       *bit = ino % page_bits;
38173 +}
38174 +
38175 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
38176 +{
38177 +       int err;
38178 +       loff_t pos;
38179 +       ssize_t sz;
38180 +       struct au_sbinfo *sbinfo;
38181 +       struct file *xib;
38182 +       unsigned long *p;
38183 +
38184 +       sbinfo = au_sbi(sb);
38185 +       MtxMustLock(&sbinfo->si_xib_mtx);
38186 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38187 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38188 +
38189 +       if (pindex == sbinfo->si_xib_last_pindex)
38190 +               return 0;
38191 +
38192 +       xib = sbinfo->si_xib;
38193 +       p = sbinfo->si_xib_buf;
38194 +       pos = sbinfo->si_xib_last_pindex;
38195 +       pos *= PAGE_SIZE;
38196 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38197 +       if (unlikely(sz != PAGE_SIZE))
38198 +               goto out;
38199 +
38200 +       pos = pindex;
38201 +       pos *= PAGE_SIZE;
38202 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38203 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
38204 +       else {
38205 +               memset(p, 0, PAGE_SIZE);
38206 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38207 +       }
38208 +       if (sz == PAGE_SIZE) {
38209 +               sbinfo->si_xib_last_pindex = pindex;
38210 +               return 0; /* success */
38211 +       }
38212 +
38213 +out:
38214 +       AuIOErr1("write failed (%zd)\n", sz);
38215 +       err = sz;
38216 +       if (sz >= 0)
38217 +               err = -EIO;
38218 +       return err;
38219 +}
38220 +
38221 +static void au_xib_clear_bit(struct inode *inode)
38222 +{
38223 +       int err, bit;
38224 +       unsigned long pindex;
38225 +       struct super_block *sb;
38226 +       struct au_sbinfo *sbinfo;
38227 +
38228 +       AuDebugOn(inode->i_nlink);
38229 +
38230 +       sb = inode->i_sb;
38231 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38232 +       AuDebugOn(page_bits <= bit);
38233 +       sbinfo = au_sbi(sb);
38234 +       mutex_lock(&sbinfo->si_xib_mtx);
38235 +       err = xib_pindex(sb, pindex);
38236 +       if (!err) {
38237 +               clear_bit(bit, sbinfo->si_xib_buf);
38238 +               sbinfo->si_xib_next_bit = bit;
38239 +       }
38240 +       mutex_unlock(&sbinfo->si_xib_mtx);
38241 +}
38242 +
38243 +/* ---------------------------------------------------------------------- */
38244 +
38245 +/*
38246 + * truncate a xino bitmap file
38247 + */
38248 +
38249 +/* todo: slow */
38250 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38251 +{
38252 +       int err, bit;
38253 +       ssize_t sz;
38254 +       unsigned long pindex;
38255 +       loff_t pos, pend;
38256 +       struct au_sbinfo *sbinfo;
38257 +       ino_t *ino;
38258 +       unsigned long *p;
38259 +
38260 +       err = 0;
38261 +       sbinfo = au_sbi(sb);
38262 +       MtxMustLock(&sbinfo->si_xib_mtx);
38263 +       p = sbinfo->si_xib_buf;
38264 +       pend = vfsub_f_size_read(file);
38265 +       pos = 0;
38266 +       while (pos < pend) {
38267 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
38268 +               err = sz;
38269 +               if (unlikely(sz <= 0))
38270 +                       goto out;
38271 +
38272 +               err = 0;
38273 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38274 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38275 +                               continue;
38276 +
38277 +                       xib_calc_bit(*ino, &pindex, &bit);
38278 +                       AuDebugOn(page_bits <= bit);
38279 +                       err = xib_pindex(sb, pindex);
38280 +                       if (!err)
38281 +                               set_bit(bit, p);
38282 +                       else
38283 +                               goto out;
38284 +               }
38285 +       }
38286 +
38287 +out:
38288 +       return err;
38289 +}
38290 +
38291 +static int xib_restore(struct super_block *sb)
38292 +{
38293 +       int err, i;
38294 +       unsigned int nfile;
38295 +       aufs_bindex_t bindex, bbot;
38296 +       void *page;
38297 +       struct au_branch *br;
38298 +       struct au_xino *xi;
38299 +       struct file *file;
38300 +
38301 +       err = -ENOMEM;
38302 +       page = (void *)__get_free_page(GFP_NOFS);
38303 +       if (unlikely(!page))
38304 +               goto out;
38305 +
38306 +       err = 0;
38307 +       bbot = au_sbbot(sb);
38308 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38309 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38310 +                       br = au_sbr(sb, bindex);
38311 +                       xi = br->br_xino;
38312 +                       nfile = xi->xi_nfile;
38313 +                       for (i = 0; i < nfile; i++) {
38314 +                               file = au_xino_file(xi, i);
38315 +                               if (file)
38316 +                                       err = do_xib_restore(sb, file, page);
38317 +                       }
38318 +               } else
38319 +                       AuDbg("skip shared b%d\n", bindex);
38320 +       free_page((unsigned long)page);
38321 +
38322 +out:
38323 +       return err;
38324 +}
38325 +
38326 +int au_xib_trunc(struct super_block *sb)
38327 +{
38328 +       int err;
38329 +       ssize_t sz;
38330 +       loff_t pos;
38331 +       struct au_sbinfo *sbinfo;
38332 +       unsigned long *p;
38333 +       struct file *file;
38334 +
38335 +       SiMustWriteLock(sb);
38336 +
38337 +       err = 0;
38338 +       sbinfo = au_sbi(sb);
38339 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38340 +               goto out;
38341 +
38342 +       file = sbinfo->si_xib;
38343 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38344 +               goto out;
38345 +
38346 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38347 +       err = PTR_ERR(file);
38348 +       if (IS_ERR(file))
38349 +               goto out;
38350 +       fput(sbinfo->si_xib);
38351 +       sbinfo->si_xib = file;
38352 +
38353 +       p = sbinfo->si_xib_buf;
38354 +       memset(p, 0, PAGE_SIZE);
38355 +       pos = 0;
38356 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38357 +       if (unlikely(sz != PAGE_SIZE)) {
38358 +               err = sz;
38359 +               AuIOErr("err %d\n", err);
38360 +               if (sz >= 0)
38361 +                       err = -EIO;
38362 +               goto out;
38363 +       }
38364 +
38365 +       mutex_lock(&sbinfo->si_xib_mtx);
38366 +       /* mnt_want_write() is unnecessary here */
38367 +       err = xib_restore(sb);
38368 +       mutex_unlock(&sbinfo->si_xib_mtx);
38369 +
38370 +out:
38371 +       return err;
38372 +}
38373 +
38374 +/* ---------------------------------------------------------------------- */
38375 +
38376 +struct au_xino *au_xino_alloc(unsigned int nfile)
38377 +{
38378 +       struct au_xino *xi;
38379 +
38380 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38381 +       if (unlikely(!xi))
38382 +               goto out;
38383 +       xi->xi_nfile = nfile;
38384 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38385 +       if (unlikely(!xi->xi_file))
38386 +               goto out_free;
38387 +
38388 +       xi->xi_nondir.total = 8; /* initial size */
38389 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38390 +                                     GFP_NOFS);
38391 +       if (unlikely(!xi->xi_nondir.array))
38392 +               goto out_file;
38393 +
38394 +       spin_lock_init(&xi->xi_nondir.spin);
38395 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38396 +       mutex_init(&xi->xi_mtx);
38397 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38398 +       atomic_set(&xi->xi_truncating, 0);
38399 +       kref_init(&xi->xi_kref);
38400 +       goto out; /* success */
38401 +
38402 +out_file:
38403 +       au_kfree_try_rcu(xi->xi_file);
38404 +out_free:
38405 +       au_kfree_rcu(xi);
38406 +       xi = NULL;
38407 +out:
38408 +       return xi;
38409 +}
38410 +
38411 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38412 +{
38413 +       int err;
38414 +       struct au_xino *xi;
38415 +
38416 +       err = 0;
38417 +       xi = au_xino_alloc(idx + 1);
38418 +       if (unlikely(!xi)) {
38419 +               err = -ENOMEM;
38420 +               goto out;
38421 +       }
38422 +
38423 +       if (file)
38424 +               get_file(file);
38425 +       xi->xi_file[idx] = file;
38426 +       AuDebugOn(br->br_xino);
38427 +       br->br_xino = xi;
38428 +
38429 +out:
38430 +       return err;
38431 +}
38432 +
38433 +static void au_xino_release(struct kref *kref)
38434 +{
38435 +       struct au_xino *xi;
38436 +       int i;
38437 +       unsigned long ul;
38438 +       struct hlist_bl_head *hbl;
38439 +       struct hlist_bl_node *pos, *n;
38440 +       struct au_xi_writing *p;
38441 +
38442 +       xi = container_of(kref, struct au_xino, xi_kref);
38443 +       for (i = 0; i < xi->xi_nfile; i++)
38444 +               if (xi->xi_file[i])
38445 +                       fput(xi->xi_file[i]);
38446 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38447 +               AuDebugOn(xi->xi_nondir.array[i]);
38448 +       mutex_destroy(&xi->xi_mtx);
38449 +       hbl = &xi->xi_writing;
38450 +       ul = au_hbl_count(hbl);
38451 +       if (unlikely(ul)) {
38452 +               pr_warn("xi_writing %lu\n", ul);
38453 +               hlist_bl_lock(hbl);
38454 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38455 +                       hlist_bl_del(&p->node);
38456 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38457 +                       kfree(p);
38458 +               }
38459 +               hlist_bl_unlock(hbl);
38460 +       }
38461 +       au_kfree_try_rcu(xi->xi_file);
38462 +       au_kfree_try_rcu(xi->xi_nondir.array);
38463 +       au_kfree_rcu(xi);
38464 +}
38465 +
38466 +int au_xino_put(struct au_branch *br)
38467 +{
38468 +       int ret;
38469 +       struct au_xino *xi;
38470 +
38471 +       ret = 0;
38472 +       xi = br->br_xino;
38473 +       if (xi) {
38474 +               br->br_xino = NULL;
38475 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38476 +       }
38477 +
38478 +       return ret;
38479 +}
38480 +
38481 +/* ---------------------------------------------------------------------- */
38482 +
38483 +/*
38484 + * xino mount option handlers
38485 + */
38486 +
38487 +/* xino bitmap */
38488 +static void xino_clear_xib(struct super_block *sb)
38489 +{
38490 +       struct au_sbinfo *sbinfo;
38491 +
38492 +       SiMustWriteLock(sb);
38493 +
38494 +       sbinfo = au_sbi(sb);
38495 +       if (sbinfo->si_xib)
38496 +               fput(sbinfo->si_xib);
38497 +       sbinfo->si_xib = NULL;
38498 +       if (sbinfo->si_xib_buf)
38499 +               free_page((unsigned long)sbinfo->si_xib_buf);
38500 +       sbinfo->si_xib_buf = NULL;
38501 +}
38502 +
38503 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38504 +{
38505 +       int err;
38506 +       loff_t pos;
38507 +       struct au_sbinfo *sbinfo;
38508 +       struct file *file;
38509 +       struct super_block *xi_sb;
38510 +
38511 +       SiMustWriteLock(sb);
38512 +
38513 +       sbinfo = au_sbi(sb);
38514 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38515 +       err = PTR_ERR(file);
38516 +       if (IS_ERR(file))
38517 +               goto out;
38518 +       if (sbinfo->si_xib)
38519 +               fput(sbinfo->si_xib);
38520 +       sbinfo->si_xib = file;
38521 +       xi_sb = file_inode(file)->i_sb;
38522 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38523 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38524 +               err = -EIO;
38525 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38526 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38527 +               goto out_unset;
38528 +       }
38529 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38530 +
38531 +       err = -ENOMEM;
38532 +       if (!sbinfo->si_xib_buf)
38533 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38534 +       if (unlikely(!sbinfo->si_xib_buf))
38535 +               goto out_unset;
38536 +
38537 +       sbinfo->si_xib_last_pindex = 0;
38538 +       sbinfo->si_xib_next_bit = 0;
38539 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38540 +               pos = 0;
38541 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38542 +               if (unlikely(err != PAGE_SIZE))
38543 +                       goto out_free;
38544 +       }
38545 +       err = 0;
38546 +       goto out; /* success */
38547 +
38548 +out_free:
38549 +       if (sbinfo->si_xib_buf)
38550 +               free_page((unsigned long)sbinfo->si_xib_buf);
38551 +       sbinfo->si_xib_buf = NULL;
38552 +       if (err >= 0)
38553 +               err = -EIO;
38554 +out_unset:
38555 +       fput(sbinfo->si_xib);
38556 +       sbinfo->si_xib = NULL;
38557 +out:
38558 +       AuTraceErr(err);
38559 +       return err;
38560 +}
38561 +
38562 +/* xino for each branch */
38563 +static void xino_clear_br(struct super_block *sb)
38564 +{
38565 +       aufs_bindex_t bindex, bbot;
38566 +       struct au_branch *br;
38567 +
38568 +       bbot = au_sbbot(sb);
38569 +       for (bindex = 0; bindex <= bbot; bindex++) {
38570 +               br = au_sbr(sb, bindex);
38571 +               AuDebugOn(!br);
38572 +               au_xino_put(br);
38573 +       }
38574 +}
38575 +
38576 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38577 +                                 aufs_bindex_t bshared)
38578 +{
38579 +       struct au_branch *brshared;
38580 +
38581 +       brshared = au_sbr(sb, bshared);
38582 +       AuDebugOn(!brshared->br_xino);
38583 +       AuDebugOn(!brshared->br_xino->xi_file);
38584 +       if (br->br_xino != brshared->br_xino) {
38585 +               au_xino_get(brshared);
38586 +               au_xino_put(br);
38587 +               br->br_xino = brshared->br_xino;
38588 +       }
38589 +}
38590 +
38591 +struct au_xino_do_set_br {
38592 +       struct au_branch *br;
38593 +       ino_t h_ino;
38594 +       aufs_bindex_t bshared;
38595 +};
38596 +
38597 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38598 +                            struct au_xino_do_set_br *args)
38599 +{
38600 +       int err;
38601 +       struct au_xi_calc calc;
38602 +       struct file *file;
38603 +       struct au_branch *br;
38604 +       struct au_xi_new xinew = {
38605 +               .base = path
38606 +       };
38607 +
38608 +       br = args->br;
38609 +       xinew.xi = br->br_xino;
38610 +       au_xi_calc(sb, args->h_ino, &calc);
38611 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38612 +       if (args->bshared >= 0)
38613 +               /* shared xino */
38614 +               au_xino_set_br_shared(sb, br, args->bshared);
38615 +       else if (!xinew.xi) {
38616 +               /* new xino */
38617 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38618 +               if (unlikely(err))
38619 +                       goto out;
38620 +       }
38621 +
38622 +       /* force re-creating */
38623 +       xinew.xi = br->br_xino;
38624 +       xinew.idx = calc.idx;
38625 +       mutex_lock(&xinew.xi->xi_mtx);
38626 +       file = au_xi_new(sb, &xinew);
38627 +       mutex_unlock(&xinew.xi->xi_mtx);
38628 +       err = PTR_ERR(file);
38629 +       if (IS_ERR(file))
38630 +               goto out;
38631 +       AuDebugOn(!file);
38632 +
38633 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38634 +       if (unlikely(err))
38635 +               au_xino_put(br);
38636 +
38637 +out:
38638 +       AuTraceErr(err);
38639 +       return err;
38640 +}
38641 +
38642 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38643 +{
38644 +       int err;
38645 +       aufs_bindex_t bindex, bbot;
38646 +       struct au_xino_do_set_br args;
38647 +       struct inode *inode;
38648 +
38649 +       SiMustWriteLock(sb);
38650 +
38651 +       bbot = au_sbbot(sb);
38652 +       inode = d_inode(sb->s_root);
38653 +       for (bindex = 0; bindex <= bbot; bindex++) {
38654 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38655 +               args.br = au_sbr(sb, bindex);
38656 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38657 +               err = au_xino_do_set_br(sb, path, &args);
38658 +               if (unlikely(err))
38659 +                       break;
38660 +       }
38661 +
38662 +       AuTraceErr(err);
38663 +       return err;
38664 +}
38665 +
38666 +void au_xino_clr(struct super_block *sb)
38667 +{
38668 +       struct au_sbinfo *sbinfo;
38669 +
38670 +       au_xigen_clr(sb);
38671 +       xino_clear_xib(sb);
38672 +       xino_clear_br(sb);
38673 +       dbgaufs_brs_del(sb, 0);
38674 +       sbinfo = au_sbi(sb);
38675 +       /* lvalue, do not call au_mntflags() */
38676 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38677 +}
38678 +
38679 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38680 +{
38681 +       int err, skip;
38682 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38683 +       struct qstr *dname, *cur_name;
38684 +       struct file *cur_xino;
38685 +       struct au_sbinfo *sbinfo;
38686 +       struct path *path, *cur_path;
38687 +
38688 +       SiMustWriteLock(sb);
38689 +
38690 +       err = 0;
38691 +       sbinfo = au_sbi(sb);
38692 +       path = &xiopt->file->f_path;
38693 +       dentry = path->dentry;
38694 +       parent = dget_parent(dentry);
38695 +       if (remount) {
38696 +               skip = 0;
38697 +               cur_xino = sbinfo->si_xib;
38698 +               if (cur_xino) {
38699 +                       cur_path = &cur_xino->f_path;
38700 +                       cur_dentry = cur_path->dentry;
38701 +                       cur_parent = dget_parent(cur_dentry);
38702 +                       cur_name = &cur_dentry->d_name;
38703 +                       dname = &dentry->d_name;
38704 +                       skip = (cur_parent == parent
38705 +                               && au_qstreq(dname, cur_name));
38706 +                       dput(cur_parent);
38707 +               }
38708 +               if (skip)
38709 +                       goto out;
38710 +       }
38711 +
38712 +       au_opt_set(sbinfo->si_mntflags, XINO);
38713 +       err = au_xino_set_xib(sb, path);
38714 +       /* si_x{read,write} are set */
38715 +       if (!err)
38716 +               err = au_xigen_set(sb, path);
38717 +       if (!err)
38718 +               err = au_xino_set_br(sb, path);
38719 +       if (!err) {
38720 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38721 +               goto out; /* success */
38722 +       }
38723 +
38724 +       /* reset all */
38725 +       AuIOErr("failed setting xino(%d).\n", err);
38726 +       au_xino_clr(sb);
38727 +
38728 +out:
38729 +       dput(parent);
38730 +       return err;
38731 +}
38732 +
38733 +/*
38734 + * create a xinofile at the default place/path.
38735 + */
38736 +struct file *au_xino_def(struct super_block *sb)
38737 +{
38738 +       struct file *file;
38739 +       char *page, *p;
38740 +       struct au_branch *br;
38741 +       struct super_block *h_sb;
38742 +       struct path path;
38743 +       aufs_bindex_t bbot, bindex, bwr;
38744 +
38745 +       br = NULL;
38746 +       bbot = au_sbbot(sb);
38747 +       bwr = -1;
38748 +       for (bindex = 0; bindex <= bbot; bindex++) {
38749 +               br = au_sbr(sb, bindex);
38750 +               if (au_br_writable(br->br_perm)
38751 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38752 +                       bwr = bindex;
38753 +                       break;
38754 +               }
38755 +       }
38756 +
38757 +       if (bwr >= 0) {
38758 +               file = ERR_PTR(-ENOMEM);
38759 +               page = (void *)__get_free_page(GFP_NOFS);
38760 +               if (unlikely(!page))
38761 +                       goto out;
38762 +               path.mnt = au_br_mnt(br);
38763 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38764 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38765 +               file = (void *)p;
38766 +               if (!IS_ERR(p)) {
38767 +                       strcat(p, "/" AUFS_XINO_FNAME);
38768 +                       AuDbg("%s\n", p);
38769 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38770 +               }
38771 +               free_page((unsigned long)page);
38772 +       } else {
38773 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38774 +                                     /*wbrtop*/0);
38775 +               if (IS_ERR(file))
38776 +                       goto out;
38777 +               h_sb = file->f_path.dentry->d_sb;
38778 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38779 +                       pr_err("xino doesn't support %s(%s)\n",
38780 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38781 +                       fput(file);
38782 +                       file = ERR_PTR(-EINVAL);
38783 +               }
38784 +       }
38785 +
38786 +out:
38787 +       return file;
38788 +}
38789 +
38790 +/* ---------------------------------------------------------------------- */
38791 +
38792 +/*
38793 + * initialize the xinofile for the specified branch @br
38794 + * at the place/path where @base_file indicates.
38795 + * test whether another branch is on the same filesystem or not,
38796 + * if found then share the xinofile with another branch.
38797 + */
38798 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38799 +                   struct path *base)
38800 +{
38801 +       int err;
38802 +       struct au_xino_do_set_br args = {
38803 +               .h_ino  = h_ino,
38804 +               .br     = br
38805 +       };
38806 +
38807 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38808 +                                      au_br_sb(br));
38809 +       err = au_xino_do_set_br(sb, base, &args);
38810 +       if (unlikely(err))
38811 +               au_xino_put(br);
38812 +
38813 +       return err;
38814 +}
38815 +
38816 +/* ---------------------------------------------------------------------- */
38817 +
38818 +/*
38819 + * get an unused inode number from bitmap
38820 + */
38821 +ino_t au_xino_new_ino(struct super_block *sb)
38822 +{
38823 +       ino_t ino;
38824 +       unsigned long *p, pindex, ul, pend;
38825 +       struct au_sbinfo *sbinfo;
38826 +       struct file *file;
38827 +       int free_bit, err;
38828 +
38829 +       if (!au_opt_test(au_mntflags(sb), XINO))
38830 +               return iunique(sb, AUFS_FIRST_INO);
38831 +
38832 +       sbinfo = au_sbi(sb);
38833 +       mutex_lock(&sbinfo->si_xib_mtx);
38834 +       p = sbinfo->si_xib_buf;
38835 +       free_bit = sbinfo->si_xib_next_bit;
38836 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38837 +               goto out; /* success */
38838 +       free_bit = find_first_zero_bit(p, page_bits);
38839 +       if (free_bit < page_bits)
38840 +               goto out; /* success */
38841 +
38842 +       pindex = sbinfo->si_xib_last_pindex;
38843 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38844 +               err = xib_pindex(sb, ul);
38845 +               if (unlikely(err))
38846 +                       goto out_err;
38847 +               free_bit = find_first_zero_bit(p, page_bits);
38848 +               if (free_bit < page_bits)
38849 +                       goto out; /* success */
38850 +       }
38851 +
38852 +       file = sbinfo->si_xib;
38853 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38854 +       for (ul = pindex + 1; ul <= pend; ul++) {
38855 +               err = xib_pindex(sb, ul);
38856 +               if (unlikely(err))
38857 +                       goto out_err;
38858 +               free_bit = find_first_zero_bit(p, page_bits);
38859 +               if (free_bit < page_bits)
38860 +                       goto out; /* success */
38861 +       }
38862 +       BUG();
38863 +
38864 +out:
38865 +       set_bit(free_bit, p);
38866 +       sbinfo->si_xib_next_bit = free_bit + 1;
38867 +       pindex = sbinfo->si_xib_last_pindex;
38868 +       mutex_unlock(&sbinfo->si_xib_mtx);
38869 +       ino = xib_calc_ino(pindex, free_bit);
38870 +       AuDbg("i%lu\n", (unsigned long)ino);
38871 +       return ino;
38872 +out_err:
38873 +       mutex_unlock(&sbinfo->si_xib_mtx);
38874 +       AuDbg("i0\n");
38875 +       return 0;
38876 +}
38877 +
38878 +/* for s_op->delete_inode() */
38879 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38880 +{
38881 +       int err;
38882 +       unsigned int mnt_flags;
38883 +       aufs_bindex_t bindex, bbot, bi;
38884 +       unsigned char try_trunc;
38885 +       struct au_iinfo *iinfo;
38886 +       struct super_block *sb;
38887 +       struct au_hinode *hi;
38888 +       struct inode *h_inode;
38889 +       struct au_branch *br;
38890 +       struct au_xi_calc calc;
38891 +       struct file *file;
38892 +
38893 +       AuDebugOn(au_is_bad_inode(inode));
38894 +
38895 +       sb = inode->i_sb;
38896 +       mnt_flags = au_mntflags(sb);
38897 +       if (!au_opt_test(mnt_flags, XINO)
38898 +           || inode->i_ino == AUFS_ROOT_INO)
38899 +               return;
38900 +
38901 +       if (unlinked) {
38902 +               au_xigen_inc(inode);
38903 +               au_xib_clear_bit(inode);
38904 +       }
38905 +
38906 +       iinfo = au_ii(inode);
38907 +       bindex = iinfo->ii_btop;
38908 +       if (bindex < 0)
38909 +               return;
38910 +
38911 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38912 +       hi = au_hinode(iinfo, bindex);
38913 +       bbot = iinfo->ii_bbot;
38914 +       for (; bindex <= bbot; bindex++, hi++) {
38915 +               h_inode = hi->hi_inode;
38916 +               if (!h_inode
38917 +                   || (!unlinked && h_inode->i_nlink))
38918 +                       continue;
38919 +
38920 +               /* inode may not be revalidated */
38921 +               bi = au_br_index(sb, hi->hi_id);
38922 +               if (bi < 0)
38923 +                       continue;
38924 +
38925 +               br = au_sbr(sb, bi);
38926 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38927 +               file = au_xino_file(br->br_xino, calc.idx);
38928 +               if (IS_ERR_OR_NULL(file))
38929 +                       continue;
38930 +
38931 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38932 +               if (!err && try_trunc
38933 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38934 +                       xino_try_trunc(sb, br);
38935 +       }
38936 +}
38937 +
38938 +/* ---------------------------------------------------------------------- */
38939 +
38940 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38941 +{
38942 +       int found, total, i;
38943 +
38944 +       found = -1;
38945 +       total = xi->xi_nondir.total;
38946 +       for (i = 0; i < total; i++) {
38947 +               if (xi->xi_nondir.array[i] != h_ino)
38948 +                       continue;
38949 +               found = i;
38950 +               break;
38951 +       }
38952 +
38953 +       return found;
38954 +}
38955 +
38956 +static int au_xinondir_expand(struct au_xino *xi)
38957 +{
38958 +       int err, sz;
38959 +       ino_t *p;
38960 +
38961 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38962 +
38963 +       err = -ENOMEM;
38964 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38965 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38966 +               goto out;
38967 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38968 +                        /*may_shrink*/0);
38969 +       if (p) {
38970 +               xi->xi_nondir.array = p;
38971 +               xi->xi_nondir.total <<= 1;
38972 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38973 +               err = 0;
38974 +       }
38975 +
38976 +out:
38977 +       return err;
38978 +}
38979 +
38980 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38981 +                      ino_t h_ino, int idx)
38982 +{
38983 +       struct au_xino *xi;
38984 +
38985 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38986 +       xi = au_sbr(sb, bindex)->br_xino;
38987 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38988 +
38989 +       spin_lock(&xi->xi_nondir.spin);
38990 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38991 +       xi->xi_nondir.array[idx] = 0;
38992 +       spin_unlock(&xi->xi_nondir.spin);
38993 +       wake_up_all(&xi->xi_nondir.wqh);
38994 +}
38995 +
38996 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38997 +                     int *idx)
38998 +{
38999 +       int err, found, empty;
39000 +       struct au_xino *xi;
39001 +
39002 +       err = 0;
39003 +       *idx = -1;
39004 +       if (!au_opt_test(au_mntflags(sb), XINO))
39005 +               goto out; /* no xino */
39006 +
39007 +       xi = au_sbr(sb, bindex)->br_xino;
39008 +
39009 +again:
39010 +       spin_lock(&xi->xi_nondir.spin);
39011 +       found = au_xinondir_find(xi, h_ino);
39012 +       if (found == -1) {
39013 +               empty = au_xinondir_find(xi, /*h_ino*/0);
39014 +               if (empty == -1) {
39015 +                       empty = xi->xi_nondir.total;
39016 +                       err = au_xinondir_expand(xi);
39017 +                       if (unlikely(err))
39018 +                               goto out_unlock;
39019 +               }
39020 +               xi->xi_nondir.array[empty] = h_ino;
39021 +               *idx = empty;
39022 +       } else {
39023 +               spin_unlock(&xi->xi_nondir.spin);
39024 +               wait_event(xi->xi_nondir.wqh,
39025 +                          xi->xi_nondir.array[found] != h_ino);
39026 +               goto again;
39027 +       }
39028 +
39029 +out_unlock:
39030 +       spin_unlock(&xi->xi_nondir.spin);
39031 +out:
39032 +       return err;
39033 +}
39034 +
39035 +/* ---------------------------------------------------------------------- */
39036 +
39037 +int au_xino_path(struct seq_file *seq, struct file *file)
39038 +{
39039 +       int err;
39040 +
39041 +       err = au_seq_path(seq, &file->f_path);
39042 +       if (unlikely(err))
39043 +               goto out;
39044 +
39045 +#define Deleted "\\040(deleted)"
39046 +       seq->count -= sizeof(Deleted) - 1;
39047 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
39048 +                        sizeof(Deleted) - 1));
39049 +#undef Deleted
39050 +
39051 +out:
39052 +       return err;
39053 +}
39054 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
39055 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
39056 +++ linux/include/uapi/linux/aufs_type.h        2023-08-28 12:34:41.669969465 +0200
39057 @@ -0,0 +1,452 @@
39058 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
39059 +/*
39060 + * Copyright (C) 2005-2022 Junjiro R. Okajima
39061 + *
39062 + * This program is free software; you can redistribute it and/or modify
39063 + * it under the terms of the GNU General Public License as published by
39064 + * the Free Software Foundation; either version 2 of the License, or
39065 + * (at your option) any later version.
39066 + *
39067 + * This program is distributed in the hope that it will be useful,
39068 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
39069 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39070 + * GNU General Public License for more details.
39071 + *
39072 + * You should have received a copy of the GNU General Public License
39073 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
39074 + */
39075 +
39076 +#ifndef __AUFS_TYPE_H__
39077 +#define __AUFS_TYPE_H__
39078 +
39079 +#define AUFS_NAME      "aufs"
39080 +
39081 +#ifdef __KERNEL__
39082 +/*
39083 + * define it before including all other headers.
39084 + * sched.h may use pr_* macros before defining "current", so define the
39085 + * no-current version first, and re-define later.
39086 + */
39087 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39088 +#include <linux/sched.h>
39089 +#undef pr_fmt
39090 +#define pr_fmt(fmt) \
39091 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39092 +               (int)sizeof(current->comm), current->comm, current->pid
39093 +#include <linux/limits.h>
39094 +#else
39095 +#include <stdint.h>
39096 +#include <sys/types.h>
39097 +#include <limits.h>
39098 +#endif /* __KERNEL__ */
39099 +
39100 +#define AUFS_VERSION   "6.x-rcN-20230828"
39101 +
39102 +/* todo? move this to linux-2.6.19/include/magic.h */
39103 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39104 +
39105 +/* ---------------------------------------------------------------------- */
39106 +
39107 +#ifdef __KERNEL__
39108 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
39109 +typedef int8_t aufs_bindex_t;
39110 +#define AUFS_BRANCH_MAX 127
39111 +#else
39112 +typedef int16_t aufs_bindex_t;
39113 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
39114 +#define AUFS_BRANCH_MAX 511
39115 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39116 +#define AUFS_BRANCH_MAX 1023
39117 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39118 +#define AUFS_BRANCH_MAX 32767
39119 +#endif
39120 +#endif
39121 +
39122 +#ifndef AUFS_BRANCH_MAX
39123 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39124 +#endif
39125 +#endif /* __KERNEL__ */
39126 +
39127 +/* ---------------------------------------------------------------------- */
39128 +
39129 +#define AUFS_FSTYPE            AUFS_NAME
39130 +
39131 +#define AUFS_ROOT_INO          2
39132 +#define AUFS_FIRST_INO         11
39133 +
39134 +#define AUFS_WH_PFX            ".wh."
39135 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
39136 +#define AUFS_WH_TMP_LEN                4
39137 +/* a limit for rmdir/rename a dir and copyup */
39138 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
39139 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
39140 +                               - 1                     /* dot */\
39141 +                               - AUFS_WH_TMP_LEN)      /* hex */
39142 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
39143 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
39144 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
39145 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
39146 +#define AUFS_DIRWH_DEF         3
39147 +#define AUFS_RDCACHE_DEF       10 /* seconds */
39148 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
39149 +#define AUFS_RDBLK_DEF         512 /* bytes */
39150 +#define AUFS_RDHASH_DEF                32
39151 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
39152 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
39153 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
39154 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
39155 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
39156 +
39157 +/* pseudo-link maintenace under /proc */
39158 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
39159 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
39160 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39161 +
39162 +/* dirren, renamed dir */
39163 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
39164 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
39165 +/* whiteouted doubly */
39166 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
39167 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39168 +
39169 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39170 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39171 +
39172 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39173 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39174 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39175 +
39176 +/* doubly whiteouted */
39177 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39178 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39179 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39180 +
39181 +/* branch permissions and attributes */
39182 +#define AUFS_BRPERM_RW         "rw"
39183 +#define AUFS_BRPERM_RO         "ro"
39184 +#define AUFS_BRPERM_RR         "rr"
39185 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39186 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39187 +#define AUFS_BRATTR_FHSM       "fhsm"
39188 +#define AUFS_BRATTR_UNPIN      "unpin"
39189 +#define AUFS_BRATTR_ICEX       "icex"
39190 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39191 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39192 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39193 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39194 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39195 +#define AUFS_BRRATTR_WH                "wh"
39196 +#define AUFS_BRWATTR_NLWH      "nolwh"
39197 +#define AUFS_BRWATTR_MOO       "moo"
39198 +
39199 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39200 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39201 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39202 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39203 +
39204 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39205 +#define AuBrAttr_COO_ALL       (1 << 4)
39206 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39207 +
39208 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39209 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39210 +                                                  branch. meaningless since
39211 +                                                  linux-3.18-rc1 */
39212 +
39213 +/* ignore error in copying XATTR */
39214 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39215 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39216 +#define AuBrAttr_ICEX_TR       (1 << 9)
39217 +#define AuBrAttr_ICEX_USR      (1 << 10)
39218 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39219 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39220 +                                | AuBrAttr_ICEX_SYS    \
39221 +                                | AuBrAttr_ICEX_TR     \
39222 +                                | AuBrAttr_ICEX_USR    \
39223 +                                | AuBrAttr_ICEX_OTH)
39224 +
39225 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39226 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39227 +
39228 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39229 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39230 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39231 +
39232 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39233 +
39234 +/* #warning test userspace */
39235 +#ifdef __KERNEL__
39236 +#ifndef CONFIG_AUFS_FHSM
39237 +#undef AuBrAttr_FHSM
39238 +#define AuBrAttr_FHSM          0
39239 +#endif
39240 +#ifndef CONFIG_AUFS_XATTR
39241 +#undef AuBrAttr_ICEX
39242 +#define AuBrAttr_ICEX          0
39243 +#undef AuBrAttr_ICEX_SEC
39244 +#define AuBrAttr_ICEX_SEC      0
39245 +#undef AuBrAttr_ICEX_SYS
39246 +#define AuBrAttr_ICEX_SYS      0
39247 +#undef AuBrAttr_ICEX_TR
39248 +#define AuBrAttr_ICEX_TR       0
39249 +#undef AuBrAttr_ICEX_USR
39250 +#define AuBrAttr_ICEX_USR      0
39251 +#undef AuBrAttr_ICEX_OTH
39252 +#define AuBrAttr_ICEX_OTH      0
39253 +#endif
39254 +#endif
39255 +
39256 +/* the longest combination */
39257 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39258 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39259 +                              "+" AUFS_BRATTR_COO_REG          \
39260 +                              "+" AUFS_BRATTR_FHSM             \
39261 +                              "+" AUFS_BRATTR_UNPIN            \
39262 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39263 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39264 +                              "+" AUFS_BRATTR_ICEX_USR         \
39265 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39266 +                              "+" AUFS_BRWATTR_NLWH)
39267 +
39268 +typedef struct {
39269 +       char a[AuBrPermStrSz];
39270 +} au_br_perm_str_t;
39271 +
39272 +static inline int au_br_writable(int brperm)
39273 +{
39274 +       return brperm & AuBrPerm_RW;
39275 +}
39276 +
39277 +static inline int au_br_whable(int brperm)
39278 +{
39279 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39280 +}
39281 +
39282 +static inline int au_br_wh_linkable(int brperm)
39283 +{
39284 +       return !(brperm & AuBrWAttr_NoLinkWH);
39285 +}
39286 +
39287 +static inline int au_br_cmoo(int brperm)
39288 +{
39289 +       return brperm & AuBrAttr_CMOO_Mask;
39290 +}
39291 +
39292 +static inline int au_br_fhsm(int brperm)
39293 +{
39294 +       return brperm & AuBrAttr_FHSM;
39295 +}
39296 +
39297 +/* ---------------------------------------------------------------------- */
39298 +
39299 +/* ioctl */
39300 +enum {
39301 +       /* readdir in userspace */
39302 +       AuCtl_RDU,
39303 +       AuCtl_RDU_INO,
39304 +
39305 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39306 +       AuCtl_IBUSY,    /* busy inode */
39307 +       AuCtl_MVDOWN,   /* move-down */
39308 +       AuCtl_BR,       /* info about branches */
39309 +       AuCtl_FHSM_FD   /* connection for fhsm */
39310 +};
39311 +
39312 +/* borrowed from linux/include/linux/kernel.h */
39313 +#ifndef ALIGN
39314 +#ifdef _GNU_SOURCE
39315 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39316 +#else
39317 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39318 +#endif
39319 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39320 +#endif
39321 +
39322 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39323 +#ifndef __aligned
39324 +#define __aligned(x)                   __attribute__((aligned(x)))
39325 +#endif
39326 +
39327 +#ifdef __KERNEL__
39328 +#ifndef __packed
39329 +#define __packed                       __attribute__((packed))
39330 +#endif
39331 +#endif
39332 +
39333 +struct au_rdu_cookie {
39334 +       uint64_t        h_pos;
39335 +       int16_t         bindex;
39336 +       uint8_t         flags;
39337 +       uint8_t         pad;
39338 +       uint32_t        generation;
39339 +} __aligned(8);
39340 +
39341 +struct au_rdu_ent {
39342 +       uint64_t        ino;
39343 +       int16_t         bindex;
39344 +       uint8_t         type;
39345 +       uint8_t         nlen;
39346 +       uint8_t         wh;
39347 +       char            name[];
39348 +} __aligned(8);
39349 +
39350 +static inline int au_rdu_len(int nlen)
39351 +{
39352 +       /* include the terminating NULL */
39353 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39354 +                    sizeof(uint64_t));
39355 +}
39356 +
39357 +union au_rdu_ent_ul {
39358 +       struct au_rdu_ent __user        *e;
39359 +       uint64_t                        ul;
39360 +};
39361 +
39362 +enum {
39363 +       AufsCtlRduV_SZ,
39364 +       AufsCtlRduV_End
39365 +};
39366 +
39367 +struct aufs_rdu {
39368 +       /* input */
39369 +       union {
39370 +               uint64_t        sz;     /* AuCtl_RDU */
39371 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39372 +       };
39373 +       union au_rdu_ent_ul     ent;
39374 +       uint16_t                verify[AufsCtlRduV_End];
39375 +
39376 +       /* input/output */
39377 +       uint32_t                blk;
39378 +
39379 +       /* output */
39380 +       union au_rdu_ent_ul     tail;
39381 +       /* number of entries which were added in a single call */
39382 +       uint64_t                rent;
39383 +       uint8_t                 full;
39384 +       uint8_t                 shwh;
39385 +
39386 +       struct au_rdu_cookie    cookie;
39387 +} __aligned(8);
39388 +
39389 +/* ---------------------------------------------------------------------- */
39390 +
39391 +/* dirren. the branch is identified by the filename who contains this */
39392 +struct au_drinfo {
39393 +       uint64_t ino;
39394 +       union {
39395 +               uint8_t oldnamelen;
39396 +               uint64_t _padding;
39397 +       };
39398 +       uint8_t oldname[];
39399 +} __aligned(8);
39400 +
39401 +struct au_drinfo_fdata {
39402 +       uint32_t magic;
39403 +       struct au_drinfo drinfo;
39404 +} __aligned(8);
39405 +
39406 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39407 +/* future */
39408 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39409 +
39410 +/* ---------------------------------------------------------------------- */
39411 +
39412 +struct aufs_wbr_fd {
39413 +       uint32_t        oflags;
39414 +       int16_t         brid;
39415 +} __aligned(8);
39416 +
39417 +/* ---------------------------------------------------------------------- */
39418 +
39419 +struct aufs_ibusy {
39420 +       uint64_t        ino, h_ino;
39421 +       int16_t         bindex;
39422 +} __aligned(8);
39423 +
39424 +/* ---------------------------------------------------------------------- */
39425 +
39426 +/* error code for move-down */
39427 +/* the actual message strings are implemented in aufs-util.git */
39428 +enum {
39429 +       EAU_MVDOWN_OPAQUE = 1,
39430 +       EAU_MVDOWN_WHITEOUT,
39431 +       EAU_MVDOWN_UPPER,
39432 +       EAU_MVDOWN_BOTTOM,
39433 +       EAU_MVDOWN_NOUPPER,
39434 +       EAU_MVDOWN_NOLOWERBR,
39435 +       EAU_Last
39436 +};
39437 +
39438 +/* flags for move-down */
39439 +#define AUFS_MVDOWN_DMSG       1
39440 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39441 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39442 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39443 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39444 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39445 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39446 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39447 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39448 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39449 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39450 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39451 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39452 +
39453 +/* index for move-down */
39454 +enum {
39455 +       AUFS_MVDOWN_UPPER,
39456 +       AUFS_MVDOWN_LOWER,
39457 +       AUFS_MVDOWN_NARRAY
39458 +};
39459 +
39460 +/*
39461 + * additional info of move-down
39462 + * number of free blocks and inodes.
39463 + * subset of struct kstatfs, but smaller and always 64bit.
39464 + */
39465 +struct aufs_stfs {
39466 +       uint64_t        f_blocks;
39467 +       uint64_t        f_bavail;
39468 +       uint64_t        f_files;
39469 +       uint64_t        f_ffree;
39470 +};
39471 +
39472 +struct aufs_stbr {
39473 +       int16_t                 brid;   /* optional input */
39474 +       int16_t                 bindex; /* output */
39475 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39476 +} __aligned(8);
39477 +
39478 +struct aufs_mvdown {
39479 +       uint32_t                flags;                  /* input/output */
39480 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39481 +       int8_t                  au_errno;               /* output */
39482 +} __aligned(8);
39483 +
39484 +/* ---------------------------------------------------------------------- */
39485 +
39486 +union aufs_brinfo {
39487 +       /* PATH_MAX may differ between kernel-space and user-space */
39488 +       char    _spacer[4096];
39489 +       struct {
39490 +               int16_t id;
39491 +               int     perm;
39492 +               char    path[];
39493 +       };
39494 +} __aligned(8);
39495 +
39496 +/* ---------------------------------------------------------------------- */
39497 +
39498 +#define AuCtlType              'A'
39499 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39500 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39501 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39502 +                                    struct aufs_wbr_fd)
39503 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39504 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39505 +                                     struct aufs_mvdown)
39506 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39507 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39508 +
39509 +#endif /* __AUFS_TYPE_H__ */
39510 SPDX-License-Identifier: GPL-2.0
39511 aufs6.x-rcN loopback patch
39512
39513 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39514 index 131294601819..56016fc9c9d8 100644
39515 --- a/drivers/block/loop.c
39516 +++ b/drivers/block/loop.c
39517 @@ -54,7 +54,7 @@ struct loop_device {
39518         int             lo_flags;
39519         char            lo_file_name[LO_NAME_SIZE];
39520  
39521 -       struct file *   lo_backing_file;
39522 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39523         struct block_device *lo_device;
39524  
39525         gfp_t           old_gfp_mask;
39526 @@ -510,6 +510,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39527                                 lo->use_dio);
39528  }
39529  
39530 +static struct file *loop_real_file(struct file *file)
39531 +{
39532 +       struct file *f = NULL;
39533 +
39534 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39535 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39536 +       return f;
39537 +}
39538 +
39539  static void loop_reread_partitions(struct loop_device *lo)
39540  {
39541         int rc;
39542 @@ -567,6 +576,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39543  {
39544         struct file *file = fget(arg);
39545         struct file *old_file;
39546 +       struct file *f, *virt_file = NULL, *old_virt_file;
39547         int error;
39548         bool partscan;
39549         bool is_loop;
39550 @@ -590,11 +600,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39551         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39552                 goto out_err;
39553  
39554 +       f = loop_real_file(file);
39555 +       if (f) {
39556 +               virt_file = file;
39557 +               file = f;
39558 +               get_file(file);
39559 +       }
39560 +
39561         error = loop_validate_file(file, bdev);
39562         if (error)
39563                 goto out_err;
39564  
39565         old_file = lo->lo_backing_file;
39566 +       old_virt_file = lo->lo_backing_virt_file;
39567  
39568         error = -EINVAL;
39569  
39570 @@ -607,6 +625,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39571         blk_mq_freeze_queue(lo->lo_queue);
39572         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39573         lo->lo_backing_file = file;
39574 +       lo->lo_backing_virt_file = virt_file;
39575         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39576         mapping_set_gfp_mask(file->f_mapping,
39577                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39578 @@ -629,6 +648,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39579          * dependency.
39580          */
39581         fput(old_file);
39582 +       if (old_virt_file)
39583 +               fput(old_virt_file);
39584         if (partscan)
39585                 loop_reread_partitions(lo);
39586  
39587 @@ -642,6 +663,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39588         loop_global_unlock(lo, is_loop);
39589  out_putf:
39590         fput(file);
39591 +       if (virt_file)
39592 +               fput(virt_file);
39593         goto done;
39594  }
39595  
39596 @@ -1013,6 +1036,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39597                           const struct loop_config *config)
39598  {
39599         struct file *file = fget(config->fd);
39600 +       struct file *f, *virt_file = NULL;
39601         struct inode *inode;
39602         struct address_space *mapping;
39603         int error;
39604 @@ -1028,6 +1052,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39605         /* This is safe, since we have a reference from open(). */
39606         __module_get(THIS_MODULE);
39607  
39608 +       f = loop_real_file(file);
39609 +       if (f) {
39610 +               virt_file = file;
39611 +               file = f;
39612 +               get_file(file);
39613 +       }
39614 +
39615         /*
39616          * If we don't hold exclusive handle for the device, upgrade to it
39617          * here to avoid changing device under exclusive owner.
39618 @@ -1091,6 +1122,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39619         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39620         lo->lo_device = bdev;
39621         lo->lo_backing_file = file;
39622 +       lo->lo_backing_virt_file = virt_file;
39623         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39624         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39625  
39626 @@ -1146,6 +1178,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39627                 bd_abort_claiming(bdev, loop_configure);
39628  out_putf:
39629         fput(file);
39630 +       if (virt_file)
39631 +               fput(virt_file);
39632         /* This is safe: open() is still holding a reference. */
39633         module_put(THIS_MODULE);
39634         return error;
39635 @@ -1154,6 +1188,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39636  static void __loop_clr_fd(struct loop_device *lo, bool release)
39637  {
39638         struct file *filp;
39639 +       struct file *virt_filp = lo->lo_backing_virt_file;
39640         gfp_t gfp = lo->old_gfp_mask;
39641  
39642         if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
39643 @@ -1170,6 +1205,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39644         spin_lock_irq(&lo->lo_lock);
39645         filp = lo->lo_backing_file;
39646         lo->lo_backing_file = NULL;
39647 +       lo->lo_backing_virt_file = NULL;
39648         spin_unlock_irq(&lo->lo_lock);
39649  
39650         lo->lo_device = NULL;
39651 @@ -1232,6 +1268,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39652          * fput can take open_mutex which is usually taken before lo_mutex.
39653          */
39654         fput(filp);
39655 +       if (virt_filp)
39656 +               fput(virt_filp);
39657  }
39658  
39659  static int loop_clr_fd(struct loop_device *lo)
39660 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39661 index 1ed300047a41..ea63191d6473 100644
39662 --- a/fs/aufs/f_op.c
39663 +++ b/fs/aufs/f_op.c
39664 @@ -309,7 +309,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39665         if (IS_ERR(h_file))
39666                 goto out;
39667  
39668 -       if (au_test_loopback_kthread()) {
39669 +       if (0 && au_test_loopback_kthread()) {
39670                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39671                 if (file->f_mapping != h_file->f_mapping) {
39672                         file->f_mapping = h_file->f_mapping;
39673 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39674 index 58043e31e5f3..e2bfae6f9d59 100644
39675 --- a/fs/aufs/loop.c
39676 +++ b/fs/aufs/loop.c
39677 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39678                 symbol_put(loop_backing_file);
39679         au_kfree_try_rcu(au_warn_loopback_array);
39680  }
39681 +
39682 +/* ---------------------------------------------------------------------- */
39683 +
39684 +/* support the loopback block device insude aufs */
39685 +
39686 +struct file *aufs_real_loop(struct file *file)
39687 +{
39688 +       struct file *f;
39689 +
39690 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39691 +       fi_read_lock(file);
39692 +       f = au_hf_top(file);
39693 +       fi_read_unlock(file);
39694 +       AuDebugOn(!f);
39695 +       return f;
39696 +}
39697 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39698 index 03d4908a6c03..34d356e181d5 100644
39699 --- a/fs/aufs/loop.h
39700 +++ b/fs/aufs/loop.h
39701 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39702  
39703  int au_loopback_init(void);
39704  void au_loopback_fin(void);
39705 +
39706 +struct file *aufs_real_loop(struct file *file);
39707  #else
39708  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39709  
39710 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39711  
39712  AuStubInt0(au_loopback_init, void)
39713  AuStubVoid(au_loopback_fin, void)
39714 +
39715 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39716  #endif /* BLK_DEV_LOOP */
39717  
39718  #endif /* __KERNEL__ */
39719 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39720 index 81922d4faf54..c8a62c267d72 100644
39721 --- a/fs/aufs/super.c
39722 +++ b/fs/aufs/super.c
39723 @@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39724         .show_options   = aufs_show_options,
39725         .statfs         = aufs_statfs,
39726         .put_super      = aufs_put_super,
39727 -       .sync_fs        = aufs_sync_fs
39728 +       .sync_fs        = aufs_sync_fs,
39729 +#ifdef CONFIG_AUFS_BDEV_LOOP
39730 +       .real_loop      = aufs_real_loop
39731 +#endif
39732  };
39733  
39734  /* ---------------------------------------------------------------------- */
39735 diff --git a/include/linux/fs.h b/include/linux/fs.h
39736 index 1e3054e5367d..2a780fcbdc75 100644
39737 --- a/include/linux/fs.h
39738 +++ b/include/linux/fs.h
39739 @@ -1934,6 +1934,10 @@ struct super_operations {
39740                                   struct shrink_control *);
39741         long (*free_cached_objects)(struct super_block *,
39742                                     struct shrink_control *);
39743 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39744 +       /* and aufs */
39745 +       struct file *(*real_loop)(struct file *);
39746 +#endif
39747  };
39748  
39749  /*
This page took 2.904845 seconds and 2 git commands to generate.