]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs.patch
Up to 6.4.1
[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 7e0b87d5aa2e..ce2984ca2f06 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3387,6 +3387,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..4de00f38822c 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 @@ -2269,7 +2269,7 @@ int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
444                 goto out_free_mpol;
445  
446         if (new->vm_file)
447 -               get_file(new->vm_file);
448 +               vma_get_file(new);
449  
450         if (new->vm_ops && new->vm_ops->open)
451                 new->vm_ops->open(new);
452 @@ -2774,7 +2774,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
453  
454         if (file || vma->vm_file) {
455  unmap_and_free_vma:
456 -               fput(vma->vm_file);
457 +               vma_fput(vma);
458                 vma->vm_file = NULL;
459  
460                 /* Undo any partial mapping done by a device driver. */
461 @@ -2843,6 +2843,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
462         unsigned long populate = 0;
463         unsigned long ret = -EINVAL;
464         struct file *file;
465 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
466 +       struct file *prfile;
467 +#endif
468  
469         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
470                      current->comm, current->pid);
471 @@ -2901,10 +2904,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
472         if (vma->vm_flags & VM_LOCKED)
473                 flags |= MAP_LOCKED;
474  
475 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
476 +       vma_get_file(vma);
477 +       file = vma->vm_file;
478 +       prfile = vma->vm_prfile;
479 +       ret = do_mmap(vma->vm_file, start, size,
480 +                       prot, flags, pgoff, &populate, NULL);
481 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
482 +               struct vm_area_struct *new_vma;
483 +
484 +               new_vma = find_vma(mm, ret);
485 +               if (!new_vma->vm_prfile)
486 +                       new_vma->vm_prfile = prfile;
487 +               if (new_vma != vma)
488 +                       get_file(prfile);
489 +       }
490 +       /*
491 +        * two fput()s instead of vma_fput(vma),
492 +        * coz vma may not be available anymore.
493 +        */
494 +       fput(file);
495 +       if (prfile)
496 +               fput(prfile);
497 +#else
498         file = get_file(vma->vm_file);
499         ret = do_mmap(vma->vm_file, start, size,
500                         prot, flags, pgoff, &populate, NULL);
501         fput(file);
502 +#endif /* CONFIG_AUFS_FS */
503  out:
504         mmap_write_unlock(mm);
505         if (populate)
506 @@ -3250,7 +3277,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
507                 if (anon_vma_clone(new_vma, vma))
508                         goto out_free_mempol;
509                 if (new_vma->vm_file)
510 -                       get_file(new_vma->vm_file);
511 +                       vma_get_file(new_vma);
512                 if (new_vma->vm_ops && new_vma->vm_ops->open)
513                         new_vma->vm_ops->open(new_vma);
514                 vma_start_write(new_vma);
515 @@ -3266,7 +3293,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
516                 new_vma->vm_ops->close(new_vma);
517  
518         if (new_vma->vm_file)
519 -               fput(new_vma->vm_file);
520 +               vma_fput(new_vma);
521  
522         unlink_anon_vmas(new_vma);
523  out_free_mempol:
524 diff --git a/mm/nommu.c b/mm/nommu.c
525 index f670d9979a26..94d41bc45915 100644
526 --- a/mm/nommu.c
527 +++ b/mm/nommu.c
528 @@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
529                 up_write(&nommu_region_sem);
530  
531                 if (region->vm_file)
532 -                       fput(region->vm_file);
533 +                       vmr_fput(region);
534  
535                 /* IO memory and memory shared directly out of the pagecache
536                  * from ramfs/tmpfs mustn't be released here */
537 @@ -602,7 +602,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
538         if (vma->vm_ops && vma->vm_ops->close)
539                 vma->vm_ops->close(vma);
540         if (vma->vm_file)
541 -               fput(vma->vm_file);
542 +               vma_fput(vma);
543         put_nommu_region(vma->vm_region);
544         vm_area_free(vma);
545  }
546 @@ -1124,7 +1124,7 @@ unsigned long do_mmap(struct file *file,
547                                         goto error_just_free;
548                                 }
549                         }
550 -                       fput(region->vm_file);
551 +                       vmr_fput(region);
552                         kmem_cache_free(vm_region_jar, region);
553                         region = pregion;
554                         result = start;
555 @@ -1206,10 +1206,10 @@ unsigned long do_mmap(struct file *file,
556  error:
557         vma_iter_free(&vmi);
558         if (region->vm_file)
559 -               fput(region->vm_file);
560 +               vmr_fput(region);
561         kmem_cache_free(vm_region_jar, region);
562         if (vma->vm_file)
563 -               fput(vma->vm_file);
564 +               vma_fput(vma);
565         vm_area_free(vma);
566         return ret;
567  
568 diff --git a/mm/prfile.c b/mm/prfile.c
569 new file mode 100644
570 index 000000000000..8f820a235364
571 --- /dev/null
572 +++ b/mm/prfile.c
573 @@ -0,0 +1,86 @@
574 +// SPDX-License-Identifier: GPL-2.0
575 +/*
576 + * Mainly for aufs which mmap(2) different file and wants to print different
577 + * path in /proc/PID/maps.
578 + * Call these functions via macros defined in linux/mm.h.
579 + *
580 + * See Documentation/filesystems/aufs/design/06mmap.txt
581 + *
582 + * Copyright (c) 2014-2022 Junjro R. Okajima
583 + * Copyright (c) 2014 Ian Campbell
584 + */
585 +
586 +#include <linux/mm.h>
587 +#include <linux/file.h>
588 +#include <linux/fs.h>
589 +
590 +/* #define PRFILE_TRACE */
591 +static inline void prfile_trace(struct file *f, struct file *pr,
592 +                             const char func[], int line, const char func2[])
593 +{
594 +#ifdef PRFILE_TRACE
595 +       if (pr)
596 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
597 +#endif
598 +}
599 +
600 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
601 +                            int line)
602 +{
603 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
604 +
605 +       prfile_trace(f, pr, func, line, __func__);
606 +       file_update_time(f);
607 +       if (f && pr)
608 +               file_update_time(pr);
609 +}
610 +
611 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
612 +                              int line)
613 +{
614 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
615 +
616 +       prfile_trace(f, pr, func, line, __func__);
617 +       return (f && pr) ? pr : f;
618 +}
619 +
620 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
621 +{
622 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
623 +
624 +       prfile_trace(f, pr, func, line, __func__);
625 +       get_file(f);
626 +       if (f && pr)
627 +               get_file(pr);
628 +}
629 +
630 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
631 +{
632 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
633 +
634 +       prfile_trace(f, pr, func, line, __func__);
635 +       fput(f);
636 +       if (f && pr)
637 +               fput(pr);
638 +}
639 +
640 +#ifndef CONFIG_MMU
641 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
642 +                              int line)
643 +{
644 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
645 +
646 +       prfile_trace(f, pr, func, line, __func__);
647 +       return (f && pr) ? pr : f;
648 +}
649 +
650 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
651 +{
652 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
653 +
654 +       prfile_trace(f, pr, func, line, __func__);
655 +       fput(f);
656 +       if (f && pr)
657 +               fput(pr);
658 +}
659 +#endif /* !CONFIG_MMU */
660 SPDX-License-Identifier: GPL-2.0
661 aufs6.x-rcN standalone patch
662
663 diff --git a/fs/dcache.c b/fs/dcache.c
664 index 519321f32f95..267e0c65914a 100644
665 --- a/fs/dcache.c
666 +++ b/fs/dcache.c
667 @@ -1450,6 +1450,7 @@ void d_walk(struct dentry *parent, void *data,
668         seq = 1;
669         goto again;
670  }
671 +EXPORT_SYMBOL_GPL(d_walk);
672  
673  struct check_mount {
674         struct vfsmount *mnt;
675 @@ -3052,6 +3053,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
676  
677         write_sequnlock(&rename_lock);
678  }
679 +EXPORT_SYMBOL_GPL(d_exchange);
680  
681  /**
682   * d_ancestor - search for an ancestor
683 diff --git a/fs/exec.c b/fs/exec.c
684 index a466e797c8e2..c60353b5214f 100644
685 --- a/fs/exec.c
686 +++ b/fs/exec.c
687 @@ -112,6 +112,7 @@ bool path_noexec(const struct path *path)
688         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
689                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
690  }
691 +EXPORT_SYMBOL_GPL(path_noexec);
692  
693  #ifdef CONFIG_USELIB
694  /*
695 diff --git a/fs/fcntl.c b/fs/fcntl.c
696 index 9ea58b7bb580..99fef189bcd6 100644
697 --- a/fs/fcntl.c
698 +++ b/fs/fcntl.c
699 @@ -87,6 +87,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
700   out:
701         return error;
702  }
703 +EXPORT_SYMBOL_GPL(setfl);
704  
705  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
706                       int force)
707 diff --git a/fs/file_table.c b/fs/file_table.c
708 index 372653b92617..69128fe39b6d 100644
709 --- a/fs/file_table.c
710 +++ b/fs/file_table.c
711 @@ -199,6 +199,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
712         }
713         return ERR_PTR(-ENFILE);
714  }
715 +EXPORT_SYMBOL_GPL(alloc_empty_file);
716  
717  /*
718   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
719 diff --git a/fs/namespace.c b/fs/namespace.c
720 index ebb7b4b057ea..6ee47677de2f 100644
721 --- a/fs/namespace.c
722 +++ b/fs/namespace.c
723 @@ -457,6 +457,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
724         mnt_dec_writers(real_mount(mnt));
725         preempt_enable();
726  }
727 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
728  
729  /**
730   * mnt_drop_write - give up write access to a mount
731 @@ -854,6 +855,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
732  {
733         return check_mnt(real_mount(mnt));
734  }
735 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
736  
737  /*
738   * vfsmount lock must be held for write
739 @@ -2062,6 +2064,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
740         }
741         return 0;
742  }
743 +EXPORT_SYMBOL_GPL(iterate_mounts);
744  
745  static void lock_mnt_tree(struct mount *mnt)
746  {
747 diff --git a/fs/notify/group.c b/fs/notify/group.c
748 index 1de6631a3925..3008eb37a18d 100644
749 --- a/fs/notify/group.c
750 +++ b/fs/notify/group.c
751 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
752  {
753         refcount_inc(&group->refcnt);
754  }
755 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
756  
757  /*
758   * Drop a reference to a group.  Free it if it's through.
759 diff --git a/fs/open.c b/fs/open.c
760 index 4478adcc4f3a..5a984ff90580 100644
761 --- a/fs/open.c
762 +++ b/fs/open.c
763 @@ -67,6 +67,7 @@ int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
764         inode_unlock(dentry->d_inode);
765         return ret;
766  }
767 +EXPORT_SYMBOL_GPL(do_truncate);
768  
769  long vfs_truncate(const struct path *path, loff_t length)
770  {
771 diff --git a/fs/read_write.c b/fs/read_write.c
772 index a21ba3be7dbe..ec40061176b3 100644
773 --- a/fs/read_write.c
774 +++ b/fs/read_write.c
775 @@ -477,6 +477,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
776         inc_syscr(current);
777         return ret;
778  }
779 +EXPORT_SYMBOL_GPL(vfs_read);
780  
781  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
782  {
783 @@ -592,6 +593,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
784         file_end_write(file);
785         return ret;
786  }
787 +EXPORT_SYMBOL_GPL(vfs_write);
788  
789  /* file_ppos returns &file->f_pos or NULL if file is stream */
790  static inline loff_t *file_ppos(struct file *file)
791 diff --git a/fs/splice.c b/fs/splice.c
792 index 97a66df75d4c..19c8d35f5ebd 100644
793 --- a/fs/splice.c
794 +++ b/fs/splice.c
795 @@ -872,6 +872,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
796                 return warn_unsupported(out, "write");
797         return out->f_op->splice_write(pipe, out, ppos, len, flags);
798  }
799 +EXPORT_SYMBOL_GPL(do_splice_from);
800  
801  /*
802   * Attempt to initiate a splice from a file to a pipe.
803 @@ -901,6 +902,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
804                 return warn_unsupported(in, "read");
805         return in->f_op->splice_read(in, ppos, pipe, len, flags);
806  }
807 +EXPORT_SYMBOL_GPL(do_splice_to);
808  
809  /**
810   * splice_direct_to_actor - splices data directly between two non-pipes
811 diff --git a/fs/xattr.c b/fs/xattr.c
812 index fcf67d80d7f9..174dfb9285fb 100644
813 --- a/fs/xattr.c
814 +++ b/fs/xattr.c
815 @@ -406,6 +406,7 @@ vfs_getxattr_alloc(struct mnt_idmap *idmap, struct dentry *dentry,
816         *xattr_value = value;
817         return error;
818  }
819 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
820  
821  ssize_t
822  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
823 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
824 index 85d1f7e0bc07..5e20504eed24 100644
825 --- a/kernel/locking/lockdep.c
826 +++ b/kernel/locking/lockdep.c
827 @@ -239,6 +239,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
828          */
829         return lock_classes + class_idx;
830  }
831 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
832  #define hlock_class(hlock) lockdep_hlock_class(hlock)
833  
834  #ifdef CONFIG_LOCK_STAT
835 diff --git a/kernel/task_work.c b/kernel/task_work.c
836 index 065e1ef8fc8d..c623c6f0c645 100644
837 --- a/kernel/task_work.c
838 +++ b/kernel/task_work.c
839 @@ -182,3 +182,4 @@ void task_work_run(void)
840                 } while (work);
841         }
842  }
843 +EXPORT_SYMBOL_GPL(task_work_run);
844 diff --git a/security/security.c b/security/security.c
845 index d5ff7ff45b77..5cca1f5774ef 100644
846 --- a/security/security.c
847 +++ b/security/security.c
848 @@ -1711,6 +1711,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
849                 return 0;
850         return call_int_hook(path_rmdir, 0, dir, dentry);
851  }
852 +EXPORT_SYMBOL_GPL(security_path_rmdir);
853  
854  /**
855   * security_path_unlink() - Check if removing a hard link is allowed
856 @@ -1746,6 +1747,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
857                 return 0;
858         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
859  }
860 +EXPORT_SYMBOL_GPL(security_path_symlink);
861  
862  /**
863   * security_path_link - Check if creating a hard link is allowed
864 @@ -1764,6 +1766,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
865                 return 0;
866         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
867  }
868 +EXPORT_SYMBOL_GPL(security_path_link);
869  
870  /**
871   * security_path_rename() - Check if renaming a file is allowed
872 @@ -1825,6 +1828,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
873                 return 0;
874         return call_int_hook(path_chmod, 0, path, mode);
875  }
876 +EXPORT_SYMBOL_GPL(security_path_chmod);
877  
878  /**
879   * security_path_chown() - Check if changing the file's owner/group is allowed
880 @@ -1842,6 +1846,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
881                 return 0;
882         return call_int_hook(path_chown, 0, path, uid, gid);
883  }
884 +EXPORT_SYMBOL_GPL(security_path_chown);
885  
886  /**
887   * security_path_chroot() - Check if changing the root directory is allowed
888 @@ -2071,6 +2076,7 @@ int security_inode_permission(struct inode *inode, int mask)
889                 return 0;
890         return call_int_hook(inode_permission, 0, inode, mask);
891  }
892 +EXPORT_SYMBOL_GPL(security_inode_permission);
893  
894  /**
895   * security_inode_setattr() - Check if setting file attributes is allowed
896 @@ -2549,6 +2555,7 @@ int security_file_permission(struct file *file, int mask)
897  
898         return fsnotify_perm(file, mask);
899  }
900 +EXPORT_SYMBOL_GPL(security_file_permission);
901  
902  /**
903   * security_file_alloc() - Allocate and init a file's LSM blob
904 @@ -2815,6 +2822,7 @@ int security_file_truncate(struct file *file)
905  {
906         return call_int_hook(file_truncate, 0, file);
907  }
908 +EXPORT_SYMBOL_GPL(security_file_truncate);
909  
910  /**
911   * security_task_alloc() - Allocate a task's LSM blob
912 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
913 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
914 +++ linux/Documentation/ABI/testing/debugfs-aufs        2022-11-05 23:02:18.955889283 +0100
915 @@ -0,0 +1,55 @@
916 +What:          /debug/aufs/si_<id>/
917 +Date:          March 2009
918 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
919 +Description:
920 +               Under /debug/aufs, a directory named si_<id> is created
921 +               per aufs mount, where <id> is a unique id generated
922 +               internally.
923 +
924 +What:          /debug/aufs/si_<id>/plink
925 +Date:          Apr 2013
926 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
927 +Description:
928 +               It has three lines and shows the information about the
929 +               pseudo-link. The first line is a single number
930 +               representing a number of buckets. The second line is a
931 +               number of pseudo-links per buckets (separated by a
932 +               blank). The last line is a single number representing a
933 +               total number of psedo-links.
934 +               When the aufs mount option 'noplink' is specified, it
935 +               will show "1\n0\n0\n".
936 +
937 +What:          /debug/aufs/si_<id>/xib
938 +Date:          March 2009
939 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
940 +Description:
941 +               It shows the consumed blocks by xib (External Inode Number
942 +               Bitmap), its block size and file size.
943 +               When the aufs mount option 'noxino' is specified, it
944 +               will be empty. About XINO files, see the aufs manual.
945 +
946 +What:          /debug/aufs/si_<id>/xi<branch-index>
947 +Date:          March 2009
948 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
949 +Description:
950 +               It shows the consumed blocks by xino (External Inode Number
951 +               Translation Table), its link count, block size and file
952 +               size.
953 +               Due to the file size limit, there may exist multiple
954 +               xino files per branch.  In this case, "-N" is added to
955 +               the filename and it corresponds to the index of the
956 +               internal xino array.  "-0" is omitted.
957 +               When the aufs mount option 'noxino' is specified, Those
958 +               entries won't exist.  About XINO files, see the aufs
959 +               manual.
960 +
961 +What:          /debug/aufs/si_<id>/xigen
962 +Date:          March 2009
963 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
964 +Description:
965 +               It shows the consumed blocks by xigen (External Inode
966 +               Generation Table), its block size and file size.
967 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
968 +               be created.
969 +               When the aufs mount option 'noxino' is specified, it
970 +               will be empty. About XINO files, see the aufs manual.
971 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
972 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
973 +++ linux/Documentation/ABI/testing/sysfs-aufs  2022-11-05 23:02:18.955889283 +0100
974 @@ -0,0 +1,31 @@
975 +What:          /sys/fs/aufs/si_<id>/
976 +Date:          March 2009
977 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
978 +Description:
979 +               Under /sys/fs/aufs, a directory named si_<id> is created
980 +               per aufs mount, where <id> is a unique id generated
981 +               internally.
982 +
983 +What:          /sys/fs/aufs/si_<id>/br<idx>
984 +Date:          March 2009
985 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
986 +Description:
987 +               It shows the abolute path of a member directory (which
988 +               is called branch) in aufs, and its permission.
989 +
990 +What:          /sys/fs/aufs/si_<id>/brid<idx>
991 +Date:          July 2013
992 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
993 +Description:
994 +               It shows the id of a member directory (which is called
995 +               branch) in aufs.
996 +
997 +What:          /sys/fs/aufs/si_<id>/xi_path
998 +Date:          March 2009
999 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1000 +Description:
1001 +               It shows the abolute path of XINO (External Inode Number
1002 +               Bitmap, Translation Table and Generation Table) file
1003 +               even if it is the default path.
1004 +               When the aufs mount option 'noxino' is specified, it
1005 +               will be empty. About XINO files, see the aufs manual.
1006 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1007 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1008 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2022-11-05 23:02:18.955889283 +0100
1009 @@ -0,0 +1,171 @@
1010 +
1011 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1012 +#
1013 +# This program is free software; you can redistribute it and/or modify
1014 +# it under the terms of the GNU General Public License as published by
1015 +# the Free Software Foundation; either version 2 of the License, or
1016 +# (at your option) any later version.
1017 +#
1018 +# This program is distributed in the hope that it will be useful,
1019 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1020 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1021 +# GNU General Public License for more details.
1022 +#
1023 +# You should have received a copy of the GNU General Public License
1024 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1025 +
1026 +Introduction
1027 +----------------------------------------
1028 +
1029 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1030 +1. abbrev. for "advanced multi-layered unification filesystem".
1031 +2. abbrev. for "another unionfs".
1032 +3. abbrev. for "auf das" in German which means "on the" in English.
1033 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1034 +   But "Filesystem aufs Filesystem" is hard to understand.
1035 +4. abbrev. for "African Urban Fashion Show".
1036 +
1037 +AUFS is a filesystem with features:
1038 +- multi layered stackable unification filesystem, the member directory
1039 +  is called as a branch.
1040 +- branch permission and attribute, 'readonly', 'real-readonly',
1041 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1042 +  combination.
1043 +- internal "file copy-on-write".
1044 +- logical deletion, whiteout.
1045 +- dynamic branch manipulation, adding, deleting and changing permission.
1046 +- allow bypassing aufs, user's direct branch access.
1047 +- external inode number translation table and bitmap which maintains the
1048 +  persistent aufs inode number.
1049 +- seekable directory, including NFS readdir.
1050 +- file mapping, mmap and sharing pages.
1051 +- pseudo-link, hardlink over branches.
1052 +- loopback mounted filesystem as a branch.
1053 +- several policies to select one among multiple writable branches.
1054 +- revert a single systemcall when an error occurs in aufs.
1055 +- and more...
1056 +
1057 +
1058 +Multi Layered Stackable Unification Filesystem
1059 +----------------------------------------------------------------------
1060 +Most people already knows what it is.
1061 +It is a filesystem which unifies several directories and provides a
1062 +merged single directory. When users access a file, the access will be
1063 +passed/re-directed/converted (sorry, I am not sure which English word is
1064 +correct) to the real file on the member filesystem. The member
1065 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1066 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1067 +readonly branch is handled by creating 'whiteout' on the upper writable
1068 +branch.
1069 +
1070 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1071 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1072 +different approaches to implement the merged-view.
1073 +The former tries putting it into VFS, and the latter implements as a
1074 +separate filesystem.
1075 +(If I misunderstand about these implementations, please let me know and
1076 +I shall correct it. Because it is a long time ago when I read their
1077 +source files last time).
1078 +
1079 +UnionMount's approach will be able to small, but may be hard to share
1080 +branches between several UnionMount since the whiteout in it is
1081 +implemented in the inode on branch filesystem and always
1082 +shared. According to Bharata's post, readdir does not seems to be
1083 +finished yet.
1084 +There are several missing features known in this implementations such as
1085 +- for users, the inode number may change silently. eg. copy-up.
1086 +- link(2) may break by copy-up.
1087 +- read(2) may get an obsoleted filedata (fstat(2) too).
1088 +- fcntl(F_SETLK) may be broken by copy-up.
1089 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1090 +  open(O_RDWR).
1091 +
1092 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1093 +merged into mainline. This is another implementation of UnionMount as a
1094 +separated filesystem. All the limitations and known problems which
1095 +UnionMount are equally inherited to "overlay" filesystem.
1096 +
1097 +Unionfs has a longer history. When I started implementing a stackable
1098 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1099 +inode, dentry and file objects and they have an array pointing lower
1100 +same kind objects. After contributing many patches for Unionfs, I
1101 +re-started my project AUFS (Jun 2006).
1102 +
1103 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1104 +implemented my own ideas, approaches and enhancements and it became
1105 +totally different one.
1106 +
1107 +Comparing DM snapshot and fs based implementation
1108 +- the number of bytes to be copied between devices is much smaller.
1109 +- the type of filesystem must be one and only.
1110 +- the fs must be writable, no readonly fs, even for the lower original
1111 +  device. so the compression fs will not be usable. but if we use
1112 +  loopback mount, we may address this issue.
1113 +  for instance,
1114 +       mount /cdrom/squashfs.img /sq
1115 +       losetup /sq/ext2.img
1116 +       losetup /somewhere/cow
1117 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1118 +- it will be difficult (or needs more operations) to extract the
1119 +  difference between the original device and COW.
1120 +- DM snapshot-merge may help a lot when users try merging. in the
1121 +  fs-layer union, users will use rsync(1).
1122 +
1123 +You may want to read my old paper "Filesystems in LiveCD"
1124 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1125 +
1126 +
1127 +Several characters/aspects/persona of aufs
1128 +----------------------------------------------------------------------
1129 +
1130 +Aufs has several characters, aspects or persona.
1131 +1. a filesystem, callee of VFS helper
1132 +2. sub-VFS, caller of VFS helper for branches
1133 +3. a virtual filesystem which maintains persistent inode number
1134 +4. reader/writer of files on branches such like an application
1135 +
1136 +1. Callee of VFS Helper
1137 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1138 +unlink(2) from an application reaches sys_unlink() kernel function and
1139 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1140 +calls filesystem specific unlink operation. Actually aufs implements the
1141 +unlink operation but it behaves like a redirector.
1142 +
1143 +2. Caller of VFS Helper for Branches
1144 +aufs_unlink() passes the unlink request to the branch filesystem as if
1145 +it were called from VFS. So the called unlink operation of the branch
1146 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1147 +every necessary pre/post operation for the branch filesystem.
1148 +- acquire the lock for the parent dir on a branch
1149 +- lookup in a branch
1150 +- revalidate dentry on a branch
1151 +- mnt_want_write() for a branch
1152 +- vfs_unlink() for a branch
1153 +- mnt_drop_write() for a branch
1154 +- release the lock on a branch
1155 +
1156 +3. Persistent Inode Number
1157 +One of the most important issue for a filesystem is to maintain inode
1158 +numbers. This is particularly important to support exporting a
1159 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1160 +backend block device for its own. But some storage is necessary to
1161 +keep and maintain the inode numbers. It may be a large space and may not
1162 +suit to keep in memory. Aufs rents some space from its first writable
1163 +branch filesystem (by default) and creates file(s) on it. These files
1164 +are created by aufs internally and removed soon (currently) keeping
1165 +opened.
1166 +Note: Because these files are removed, they are totally gone after
1167 +      unmounting aufs. It means the inode numbers are not persistent
1168 +      across unmount or reboot. I have a plan to make them really
1169 +      persistent which will be important for aufs on NFS server.
1170 +
1171 +4. Read/Write Files Internally (copy-on-write)
1172 +Because a branch can be readonly, when you write a file on it, aufs will
1173 +"copy-up" it to the upper writable branch internally. And then write the
1174 +originally requested thing to the file. Generally kernel doesn't
1175 +open/read/write file actively. In aufs, even a single write may cause a
1176 +internal "file copy". This behaviour is very similar to cp(1) command.
1177 +
1178 +Some people may think it is better to pass such work to user space
1179 +helper, instead of doing in kernel space. Actually I am still thinking
1180 +about it. But currently I have implemented it in kernel space.
1181 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1182 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1183 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2022-11-05 23:02:18.955889283 +0100
1184 @@ -0,0 +1,258 @@
1185 +
1186 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1187 +#
1188 +# This program is free software; you can redistribute it and/or modify
1189 +# it under the terms of the GNU General Public License as published by
1190 +# the Free Software Foundation; either version 2 of the License, or
1191 +# (at your option) any later version.
1192 +#
1193 +# This program is distributed in the hope that it will be useful,
1194 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1195 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1196 +# GNU General Public License for more details.
1197 +#
1198 +# You should have received a copy of the GNU General Public License
1199 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1200 +
1201 +Basic Aufs Internal Structure
1202 +
1203 +Superblock/Inode/Dentry/File Objects
1204 +----------------------------------------------------------------------
1205 +As like an ordinary filesystem, aufs has its own
1206 +superblock/inode/dentry/file objects. All these objects have a
1207 +dynamically allocated array and store the same kind of pointers to the
1208 +lower filesystem, branch.
1209 +For example, when you build a union with one readwrite branch and one
1210 +readonly, mounted /au, /rw and /ro respectively.
1211 +- /au = /rw + /ro
1212 +- /ro/fileA exists but /rw/fileA
1213 +
1214 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1215 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1216 +- [0] = NULL (because /rw/fileA doesn't exist)
1217 +- [1] = /ro/fileA
1218 +
1219 +This style of an array is essentially same to the aufs
1220 +superblock/inode/dentry/file objects.
1221 +
1222 +Because aufs supports manipulating branches, ie. add/delete/change
1223 +branches dynamically, these objects has its own generation. When
1224 +branches are changed, the generation in aufs superblock is
1225 +incremented. And a generation in other object are compared when it is
1226 +accessed. When a generation in other objects are obsoleted, aufs
1227 +refreshes the internal array.
1228 +
1229 +
1230 +Superblock
1231 +----------------------------------------------------------------------
1232 +Additionally aufs superblock has some data for policies to select one
1233 +among multiple writable branches, XIB files, pseudo-links and kobject.
1234 +See below in detail.
1235 +About the policies which supports copy-down a directory, see
1236 +wbr_policy.txt too.
1237 +
1238 +
1239 +Branch and XINO(External Inode Number Translation Table)
1240 +----------------------------------------------------------------------
1241 +Every branch has its own xino (external inode number translation table)
1242 +file. The xino file is created and unlinked by aufs internally. When two
1243 +members of a union exist on the same filesystem, they share the single
1244 +xino file.
1245 +The struct of a xino file is simple, just a sequence of aufs inode
1246 +numbers which is indexed by the lower inode number.
1247 +In the above sample, assume the inode number of /ro/fileA is i111 and
1248 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1249 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1250 +
1251 +When the inode numbers are not contiguous, the xino file will be sparse
1252 +which has a hole in it and doesn't consume as much disk space as it
1253 +might appear. If your branch filesystem consumes disk space for such
1254 +holes, then you should specify 'xino=' option at mounting aufs.
1255 +
1256 +Aufs has a mount option to free the disk blocks for such holes in XINO
1257 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1258 +meet a problem of disk shortage due to XINO files, then you should try
1259 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1260 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1261 +the holes in XINO files.
1262 +
1263 +Also a writable branch has three kinds of "whiteout bases". All these
1264 +are existed when the branch is joined to aufs, and their names are
1265 +whiteout-ed doubly, so that users will never see their names in aufs
1266 +hierarchy.
1267 +1. a regular file which will be hardlinked to all whiteouts.
1268 +2. a directory to store a pseudo-link.
1269 +3. a directory to store an "orphan"-ed file temporary.
1270 +
1271 +1. Whiteout Base
1272 +   When you remove a file on a readonly branch, aufs handles it as a
1273 +   logical deletion and creates a whiteout on the upper writable branch
1274 +   as a hardlink of this file in order not to consume inode on the
1275 +   writable branch.
1276 +2. Pseudo-link Dir
1277 +   See below, Pseudo-link.
1278 +3. Step-Parent Dir
1279 +   When "fileC" exists on the lower readonly branch only and it is
1280 +   opened and removed with its parent dir, and then user writes
1281 +   something into it, then aufs copies-up fileC to this
1282 +   directory. Because there is no other dir to store fileC. After
1283 +   creating a file under this dir, the file is unlinked.
1284 +
1285 +Because aufs supports manipulating branches, ie. add/delete/change
1286 +dynamically, a branch has its own id. When the branch order changes,
1287 +aufs finds the new index by searching the branch id.
1288 +
1289 +
1290 +Pseudo-link
1291 +----------------------------------------------------------------------
1292 +Assume "fileA" exists on the lower readonly branch only and it is
1293 +hardlinked to "fileB" on the branch. When you write something to fileA,
1294 +aufs copies-up it to the upper writable branch. Additionally aufs
1295 +creates a hardlink under the Pseudo-link Directory of the writable
1296 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1297 +simple list. If fileB is read after unlinking fileA, aufs returns
1298 +filedata from the pseudo-link instead of the lower readonly
1299 +branch. Because the pseudo-link is based upon the inode, to keep the
1300 +inode number by xino (see above) is essentially necessary.
1301 +
1302 +All the hardlinks under the Pseudo-link Directory of the writable branch
1303 +should be restored in a proper location later. Aufs provides a utility
1304 +to do this. The userspace helpers executed at remounting and unmounting
1305 +aufs by default.
1306 +During this utility is running, it puts aufs into the pseudo-link
1307 +maintenance mode. In this mode, only the process which began the
1308 +maintenance mode (and its child processes) is allowed to operate in
1309 +aufs. Some other processes which are not related to the pseudo-link will
1310 +be allowed to run too, but the rest have to return an error or wait
1311 +until the maintenance mode ends. If a process already acquires an inode
1312 +mutex (in VFS), it has to return an error.
1313 +
1314 +
1315 +XIB(external inode number bitmap)
1316 +----------------------------------------------------------------------
1317 +Addition to the xino file per a branch, aufs has an external inode number
1318 +bitmap in a superblock object. It is also an internal file such like a
1319 +xino file.
1320 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1321 +not.
1322 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1323 +
1324 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1325 +reduce the number of consumed disk blocks for these files.
1326 +
1327 +
1328 +Virtual or Vertical Dir, and Readdir in Userspace
1329 +----------------------------------------------------------------------
1330 +In order to support multiple layers (branches), aufs readdir operation
1331 +constructs a virtual dir block on memory. For readdir, aufs calls
1332 +vfs_readdir() internally for each dir on branches, merges their entries
1333 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1334 +object. So the file object has its entry list until it is closed. The
1335 +entry list will be updated when the file position is zero and becomes
1336 +obsoleted. This decision is made in aufs automatically.
1337 +
1338 +The dynamically allocated memory block for the name of entries has a
1339 +unit of 512 bytes (by default) and stores the names contiguously (no
1340 +padding). Another block for each entry is handled by kmem_cache too.
1341 +During building dir blocks, aufs creates hash list and judging whether
1342 +the entry is whiteouted by its upper branch or already listed.
1343 +The merged result is cached in the corresponding inode object and
1344 +maintained by a customizable life-time option.
1345 +
1346 +Some people may call it can be a security hole or invite DoS attack
1347 +since the opened and once readdir-ed dir (file object) holds its entry
1348 +list and becomes a pressure for system memory. But I'd say it is similar
1349 +to files under /proc or /sys. The virtual files in them also holds a
1350 +memory page (generally) while they are opened. When an idea to reduce
1351 +memory for them is introduced, it will be applied to aufs too.
1352 +For those who really hate this situation, I've developed readdir(3)
1353 +library which operates this merging in userspace. You just need to set
1354 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1355 +kernel space for readdir(3).
1356 +
1357 +
1358 +Workqueue
1359 +----------------------------------------------------------------------
1360 +Aufs sometimes requires privilege access to a branch. For instance,
1361 +in copy-up/down operation. When a user process is going to make changes
1362 +to a file which exists in the lower readonly branch only, and the mode
1363 +of one of ancestor directories may not be writable by a user
1364 +process. Here aufs copy-up the file with its ancestors and they may
1365 +require privilege to set its owner/group/mode/etc.
1366 +This is a typical case of a application character of aufs (see
1367 +Introduction).
1368 +
1369 +Aufs uses workqueue synchronously for this case. It creates its own
1370 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1371 +passes the request to call mkdir or write (for example), and wait for
1372 +its completion. This approach solves a problem of a signal handler
1373 +simply.
1374 +If aufs didn't adopt the workqueue and changed the privilege of the
1375 +process, then the process may receive the unexpected SIGXFSZ or other
1376 +signals.
1377 +
1378 +Also aufs uses the system global workqueue ("events" kernel thread) too
1379 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1380 +whiteout base and etc. This is unrelated to a privilege.
1381 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1382 +superblock at the beginning, at the same time waits for the completion
1383 +of all queued asynchronous tasks.
1384 +
1385 +
1386 +Whiteout
1387 +----------------------------------------------------------------------
1388 +The whiteout in aufs is very similar to Unionfs's. That is represented
1389 +by its filename. UnionMount takes an approach of a file mode, but I am
1390 +afraid several utilities (find(1) or something) will have to support it.
1391 +
1392 +Basically the whiteout represents "logical deletion" which stops aufs to
1393 +lookup further, but also it represents "dir is opaque" which also stop
1394 +further lookup.
1395 +
1396 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1397 +In order to make several functions in a single systemcall to be
1398 +revertible, aufs adopts an approach to rename a directory to a temporary
1399 +unique whiteouted name.
1400 +For example, in rename(2) dir where the target dir already existed, aufs
1401 +renames the target dir to a temporary unique whiteouted name before the
1402 +actual rename on a branch, and then handles other actions (make it opaque,
1403 +update the attributes, etc). If an error happens in these actions, aufs
1404 +simply renames the whiteouted name back and returns an error. If all are
1405 +succeeded, aufs registers a function to remove the whiteouted unique
1406 +temporary name completely and asynchronously to the system global
1407 +workqueue.
1408 +
1409 +
1410 +Copy-up
1411 +----------------------------------------------------------------------
1412 +It is a well-known feature or concept.
1413 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1414 +internally and makes change to the new file on the upper writable branch.
1415 +When the trigger systemcall does not update the timestamps of the parent
1416 +dir, aufs reverts it after copy-up.
1417 +
1418 +
1419 +Move-down (aufs3.9 and later)
1420 +----------------------------------------------------------------------
1421 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1422 +the lower readonly branch to the upper writable branch when a user
1423 +changes something about the file.
1424 +"Move-down" is an opposite action of copy-up. Basically this action is
1425 +ran manually instead of automatically and internally.
1426 +For desgin and implementation, aufs has to consider these issues.
1427 +- whiteout for the file may exist on the lower branch.
1428 +- ancestor directories may not exist on the lower branch.
1429 +- diropq for the ancestor directories may exist on the upper branch.
1430 +- free space on the lower branch will reduce.
1431 +- another access to the file may happen during moving-down, including
1432 +  UDBA (see "Revalidate Dentry and UDBA").
1433 +- the file should not be hard-linked nor pseudo-linked. they should be
1434 +  handled by auplink utility later.
1435 +
1436 +Sometimes users want to move-down a file from the upper writable branch
1437 +to the lower readonly or writable branch. For instance,
1438 +- the free space of the upper writable branch is going to run out.
1439 +- create a new intermediate branch between the upper and lower branch.
1440 +- etc.
1441 +
1442 +For this purpose, use "aumvdown" command in aufs-util.git.
1443 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1444 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1445 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2022-11-05 23:02:18.955889283 +0100
1446 @@ -0,0 +1,85 @@
1447 +
1448 +# Copyright (C) 2015-2022 Junjiro R. Okajima
1449 +#
1450 +# This program is free software; you can redistribute it and/or modify
1451 +# it under the terms of the GNU General Public License as published by
1452 +# the Free Software Foundation; either version 2 of the License, or
1453 +# (at your option) any later version.
1454 +#
1455 +# This program is distributed in the hope that it will be useful,
1456 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1457 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1458 +# GNU General Public License for more details.
1459 +#
1460 +# You should have received a copy of the GNU General Public License
1461 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1462 +
1463 +Support for a branch who has its ->atomic_open()
1464 +----------------------------------------------------------------------
1465 +The filesystems who implement its ->atomic_open() are not majority. For
1466 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1467 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1468 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1469 +sure whether all filesystems who have ->atomic_open() behave like this,
1470 +but NFSv4 surely returns the error.
1471 +
1472 +In order to support ->atomic_open() for aufs, there are a few
1473 +approaches.
1474 +
1475 +A. Introduce aufs_atomic_open()
1476 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1477 +     branch fs.
1478 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1479 +   an aufs user Pip Cet's approach
1480 +   - calls aufs_create(), VFS finish_open() and notify_change().
1481 +   - pass fake-mode to finish_open(), and then correct the mode by
1482 +     notify_change().
1483 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1484 +   - no aufs_atomic_open().
1485 +   - aufs_lookup() registers the TID to an aufs internal object.
1486 +   - aufs_create() does nothing when the matching TID is registered, but
1487 +     registers the mode.
1488 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1489 +     TID is registered.
1490 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1491 +   credential
1492 +   - no aufs_atomic_open().
1493 +   - aufs_create() registers the TID to an internal object. this info
1494 +     represents "this process created this file just now."
1495 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1496 +     registered TID and re-try open() with superuser's credential.
1497 +
1498 +Pros and cons for each approach.
1499 +
1500 +A.
1501 +   - straightforward but highly depends upon VFS internal.
1502 +   - the atomic behavaiour is kept.
1503 +   - some of parameters such as nameidata are hard to reproduce for
1504 +     branch fs.
1505 +   - large overhead.
1506 +B.
1507 +   - easy to implement.
1508 +   - the atomic behavaiour is lost.
1509 +C.
1510 +   - the atomic behavaiour is kept.
1511 +   - dirty and tricky.
1512 +   - VFS checks whether the file is created correctly after calling
1513 +     ->create(), which means this approach doesn't work.
1514 +D.
1515 +   - easy to implement.
1516 +   - the atomic behavaiour is lost.
1517 +   - to open a file with superuser's credential and give it to a user
1518 +     process is a bad idea, since the file object keeps the credential
1519 +     in it. It may affect LSM or something. This approach doesn't work
1520 +     either.
1521 +
1522 +The approach A is ideal, but it hard to implement. So here is a
1523 +variation of A, which is to be implemented.
1524 +
1525 +A-1. Introduce aufs_atomic_open()
1526 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1527 +       vfs_create() and finish_open().
1528 +     - the demerit is that the several checks after branch fs
1529 +       ->atomic_open() are lost. in the ordinary case, the checks are
1530 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1531 +       be implemented in aufs, but not all I am afraid.
1532 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1533 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1534 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2022-11-05 23:02:18.959222617 +0100
1535 @@ -0,0 +1,113 @@
1536 +
1537 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1538 +#
1539 +# This program is free software; you can redistribute it and/or modify
1540 +# it under the terms of the GNU General Public License as published by
1541 +# the Free Software Foundation; either version 2 of the License, or
1542 +# (at your option) any later version.
1543 +#
1544 +# This program is distributed in the hope that it will be useful,
1545 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1546 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1547 +# GNU General Public License for more details.
1548 +#
1549 +# You should have received a copy of the GNU General Public License
1550 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1551 +
1552 +Lookup in a Branch
1553 +----------------------------------------------------------------------
1554 +Since aufs has a character of sub-VFS (see Introduction), it operates
1555 +lookup for branches as VFS does. It may be a heavy work. But almost all
1556 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1557 +directly connected to its parent. Digging down the directory hierarchy
1558 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1559 +aufs calls it.
1560 +
1561 +When a branch is a remote filesystem, aufs basically relies upon its
1562 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1563 +them.
1564 +For d_revalidate, aufs implements three levels of revalidate tests. See
1565 +"Revalidate Dentry and UDBA" in detail.
1566 +
1567 +
1568 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1569 +----------------------------------------------------------------------
1570 +Let's try case study.
1571 +- aufs has two branches, upper readwrite and lower readonly.
1572 +  /au = /rw + /ro
1573 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1574 +- user invoked "chmod a+rx /au/dirA"
1575 +- the internal copy-up is activated and "/rw/dirA" is created and its
1576 +  permission bits are set to world readable.
1577 +- then "/au/dirA" becomes world readable?
1578 +
1579 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1580 +or it may be a natively readonly filesystem. If aufs respects the lower
1581 +branch, it should not respond readdir request from other users. But user
1582 +allowed it by chmod. Should really aufs rejects showing the entries
1583 +under /ro/dirA?
1584 +
1585 +To be honest, I don't have a good solution for this case. So aufs
1586 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1587 +users.
1588 +When dirperm1 is specified, aufs checks only the highest one for the
1589 +directory permission, and shows the entries. Otherwise, as usual, checks
1590 +every dir existing on all branches and rejects the request.
1591 +
1592 +As a side effect, dirperm1 option improves the performance of aufs
1593 +because the number of permission check is reduced when the number of
1594 +branch is many.
1595 +
1596 +
1597 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1598 +----------------------------------------------------------------------
1599 +Generally VFS helpers re-validate a dentry as a part of lookup.
1600 +0. digging down the directory hierarchy.
1601 +1. lock the parent dir by its i_mutex.
1602 +2. lookup the final (child) entry.
1603 +3. revalidate it.
1604 +4. call the actual operation (create, unlink, etc.)
1605 +5. unlock the parent dir
1606 +
1607 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1608 +called. Actually aufs implements it and checks the dentry on a branch is
1609 +still valid.
1610 +But it is not enough. Because aufs has to release the lock for the
1611 +parent dir on a branch at the end of ->lookup() (step 2) and
1612 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1613 +held by VFS.
1614 +If the file on a branch is changed directly, eg. bypassing aufs, after
1615 +aufs released the lock, then the subsequent operation may cause
1616 +something unpleasant result.
1617 +
1618 +This situation is a result of VFS architecture, ->lookup() and
1619 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1620 +design from VFS's point of view. It is just not suitable for sub-VFS
1621 +character in aufs.
1622 +
1623 +Aufs supports such case by three level of revalidation which is
1624 +selectable by user.
1625 +1. Simple Revalidate
1626 +   Addition to the native flow in VFS's, confirm the child-parent
1627 +   relationship on the branch just after locking the parent dir on the
1628 +   branch in the "actual operation" (step 4). When this validation
1629 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1630 +   checks the validation of the dentry on branches.
1631 +2. Monitor Changes Internally by Inotify/Fsnotify
1632 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1633 +   the dentry on the branch, and returns EBUSY if it finds different
1634 +   dentry.
1635 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1636 +   during it is in cache. When the event is notified, aufs registers a
1637 +   function to kernel 'events' thread by schedule_work(). And the
1638 +   function sets some special status to the cached aufs dentry and inode
1639 +   private data. If they are not cached, then aufs has nothing to
1640 +   do. When the same file is accessed through aufs (step 0-3) later,
1641 +   aufs will detect the status and refresh all necessary data.
1642 +   In this mode, aufs has to ignore the event which is fired by aufs
1643 +   itself.
1644 +3. No Extra Validation
1645 +   This is the simplest test and doesn't add any additional revalidation
1646 +   test, and skip the revalidation in step 4. It is useful and improves
1647 +   aufs performance when system surely hide the aufs branches from user,
1648 +   by over-mounting something (or another method).
1649 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1650 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1651 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2022-11-05 23:02:18.959222617 +0100
1652 @@ -0,0 +1,74 @@
1653 +
1654 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1655 +#
1656 +# This program is free software; you can redistribute it and/or modify
1657 +# it under the terms of the GNU General Public License as published by
1658 +# the Free Software Foundation; either version 2 of the License, or
1659 +# (at your option) any later version.
1660 +#
1661 +# This program is distributed in the hope that it will be useful,
1662 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1663 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1664 +# GNU General Public License for more details.
1665 +#
1666 +# You should have received a copy of the GNU General Public License
1667 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1668 +
1669 +Branch Manipulation
1670 +
1671 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1672 +and changing its permission/attribute, there are a lot of works to do.
1673 +
1674 +
1675 +Add a Branch
1676 +----------------------------------------------------------------------
1677 +o Confirm the adding dir exists outside of aufs, including loopback
1678 +  mount, and its various attributes.
1679 +o Initialize the xino file and whiteout bases if necessary.
1680 +  See struct.txt.
1681 +
1682 +o Check the owner/group/mode of the directory
1683 +  When the owner/group/mode of the adding directory differs from the
1684 +  existing branch, aufs issues a warning because it may impose a
1685 +  security risk.
1686 +  For example, when a upper writable branch has a world writable empty
1687 +  top directory, a malicious user can create any files on the writable
1688 +  branch directly, like copy-up and modify manually. If something like
1689 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1690 +  writable branch, and the writable branch is world-writable, then a
1691 +  malicious guy may create /etc/passwd on the writable branch directly
1692 +  and the infected file will be valid in aufs.
1693 +  I am afraid it can be a security issue, but aufs can do nothing except
1694 +  producing a warning.
1695 +
1696 +
1697 +Delete a Branch
1698 +----------------------------------------------------------------------
1699 +o Confirm the deleting branch is not busy
1700 +  To be general, there is one merit to adopt "remount" interface to
1701 +  manipulate branches. It is to discard caches. At deleting a branch,
1702 +  aufs checks the still cached (and connected) dentries and inodes. If
1703 +  there are any, then they are all in-use. An inode without its
1704 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1705 +
1706 +  For the cached one, aufs checks whether the same named entry exists on
1707 +  other branches.
1708 +  If the cached one is a directory, because aufs provides a merged view
1709 +  to users, as long as one dir is left on any branch aufs can show the
1710 +  dir to users. In this case, the branch can be removed from aufs.
1711 +  Otherwise aufs rejects deleting the branch.
1712 +
1713 +  If any file on the deleting branch is opened by aufs, then aufs
1714 +  rejects deleting.
1715 +
1716 +
1717 +Modify the Permission of a Branch
1718 +----------------------------------------------------------------------
1719 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1720 +  See struct.txt.
1721 +
1722 +o rw --> ro: Confirm the modifying branch is not busy
1723 +  Aufs rejects the request if any of these conditions are true.
1724 +  - a file on the branch is mmap-ed.
1725 +  - a regular file on the branch is opened for write and there is no
1726 +    same named entry on the upper branch.
1727 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1728 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1729 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2022-11-05 23:02:18.959222617 +0100
1730 @@ -0,0 +1,64 @@
1731 +
1732 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1733 +#
1734 +# This program is free software; you can redistribute it and/or modify
1735 +# it under the terms of the GNU General Public License as published by
1736 +# the Free Software Foundation; either version 2 of the License, or
1737 +# (at your option) any later version.
1738 +#
1739 +# This program is distributed in the hope that it will be useful,
1740 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1741 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1742 +# GNU General Public License for more details.
1743 +#
1744 +# You should have received a copy of the GNU General Public License
1745 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1746 +
1747 +Policies to Select One among Multiple Writable Branches
1748 +----------------------------------------------------------------------
1749 +When the number of writable branch is more than one, aufs has to decide
1750 +the target branch for file creation or copy-up. By default, the highest
1751 +writable branch which has the parent (or ancestor) dir of the target
1752 +file is chosen (top-down-parent policy).
1753 +By user's request, aufs implements some other policies to select the
1754 +writable branch, for file creation several policies, round-robin,
1755 +most-free-space, and other policies. For copy-up, top-down-parent,
1756 +bottom-up-parent, bottom-up and others.
1757 +
1758 +As expected, the round-robin policy selects the branch in circular. When
1759 +you have two writable branches and creates 10 new files, 5 files will be
1760 +created for each branch. mkdir(2) systemcall is an exception. When you
1761 +create 10 new directories, all will be created on the same branch.
1762 +And the most-free-space policy selects the one which has most free
1763 +space among the writable branches. The amount of free space will be
1764 +checked by aufs internally, and users can specify its time interval.
1765 +
1766 +The policies for copy-up is more simple,
1767 +top-down-parent is equivalent to the same named on in create policy,
1768 +bottom-up-parent selects the writable branch where the parent dir
1769 +exists and the nearest upper one from the copyup-source,
1770 +bottom-up selects the nearest upper writable branch from the
1771 +copyup-source, regardless the existence of the parent dir.
1772 +
1773 +There are some rules or exceptions to apply these policies.
1774 +- If there is a readonly branch above the policy-selected branch and
1775 +  the parent dir is marked as opaque (a variation of whiteout), or the
1776 +  target (creating) file is whiteout-ed on the upper readonly branch,
1777 +  then the result of the policy is ignored and the target file will be
1778 +  created on the nearest upper writable branch than the readonly branch.
1779 +- If there is a writable branch above the policy-selected branch and
1780 +  the parent dir is marked as opaque or the target file is whiteouted
1781 +  on the branch, then the result of the policy is ignored and the target
1782 +  file will be created on the highest one among the upper writable
1783 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1784 +  it as usual.
1785 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1786 +  They try selecting the branch where the source exists as possible
1787 +  since copyup a large file will take long time. If it can't be,
1788 +  ie. the branch where the source exists is readonly, then they will
1789 +  follow the copyup policy.
1790 +- There is an exception for rename(2) when the target exists.
1791 +  If the rename target exists, aufs compares the index of the branches
1792 +  where the source and the target exists and selects the higher
1793 +  one. If the selected branch is readonly, then aufs follows the
1794 +  copyup policy.
1795 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1796 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1797 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2022-11-05 23:02:18.959222617 +0100
1798 @@ -0,0 +1,44 @@
1799 +
1800 +// to view this graph, run dot(1) command in GRAPHVIZ.
1801 +//
1802 +// This program is free software; you can redistribute it and/or modify
1803 +// it under the terms of the GNU General Public License as published by
1804 +// the Free Software Foundation; either version 2 of the License, or
1805 +// (at your option) any later version.
1806 +//
1807 +// This program is distributed in the hope that it will be useful,
1808 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
1809 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1810 +// GNU General Public License for more details.
1811 +//
1812 +// You should have received a copy of the GNU General Public License
1813 +// along with this program.  If not, see <http://www.gnu.org/licenses/>.
1814 +
1815 +digraph G {
1816 +node [shape=box];
1817 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1818 +
1819 +node [shape=oval];
1820 +
1821 +aufs_rename -> whinfo [label="store/remove"];
1822 +
1823 +node [shape=oval];
1824 +inode_list [label="h_inum list in branch\ncache"];
1825 +
1826 +node [shape=box];
1827 +whinode [label="h_inum list file"];
1828 +
1829 +node [shape=oval];
1830 +brmgmt [label="br_add/del/mod/umount"];
1831 +
1832 +brmgmt -> inode_list [label="create/remove"];
1833 +brmgmt -> whinode [label="load/store"];
1834 +
1835 +inode_list -> whinode [style=dashed,dir=both];
1836 +
1837 +aufs_rename -> inode_list [label="add/del"];
1838 +
1839 +aufs_lookup -> inode_list [label="search"];
1840 +
1841 +aufs_lookup -> whinfo [label="load/remove"];
1842 +}
1843 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1844 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1845 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2022-11-05 23:02:18.959222617 +0100
1846 @@ -0,0 +1,102 @@
1847 +
1848 +# Copyright (C) 2017-2022 Junjiro R. Okajima
1849 +#
1850 +# This program is free software; you can redistribute it and/or modify
1851 +# it under the terms of the GNU General Public License as published by
1852 +# the Free Software Foundation; either version 2 of the License, or
1853 +# (at your option) any later version.
1854 +#
1855 +# This program is distributed in the hope that it will be useful,
1856 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1857 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1858 +# GNU General Public License for more details.
1859 +#
1860 +# You should have received a copy of the GNU General Public License
1861 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1862 +
1863 +Special handling for renaming a directory (DIRREN)
1864 +----------------------------------------------------------------------
1865 +First, let's assume we have a simple usecase.
1866 +
1867 +- /u = /rw + /ro
1868 +- /rw/dirA exists
1869 +- /ro/dirA and /ro/dirA/file exist too
1870 +- there is no dirB on both branches
1871 +- a user issues rename("dirA", "dirB")
1872 +
1873 +Now, what should aufs behave against this rename(2)?
1874 +There are a few possible cases.
1875 +
1876 +A. returns EROFS.
1877 +   since dirA exists on a readonly branch which cannot be renamed.
1878 +B. returns EXDEV.
1879 +   it is possible to copy-up dirA (only the dir itself), but the child
1880 +   entries ("file" in this case) should not be. it must be a bad
1881 +   approach to copy-up recursively.
1882 +C. returns a success.
1883 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1884 +   is a violation of aufs' policy.
1885 +D. construct an extra information which indicates that /ro/dirA should
1886 +   be handled as the name of dirB.
1887 +   overlayfs has a similar feature called REDIRECT.
1888 +
1889 +Until now, aufs implements the case B only which returns EXDEV, and
1890 +expects the userspace application behaves like mv(1) which tries
1891 +issueing rename(2) recursively.
1892 +
1893 +A new aufs feature called DIRREN is introduced which implements the case
1894 +D. There are several "extra information" added.
1895 +
1896 +1. detailed info per renamed directory
1897 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1898 +2. the inode-number list of directories on a branch
1899 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1900 +
1901 +The filename of "detailed info per directory" represents the lower
1902 +branch, and its format is
1903 +- a type of the branch id
1904 +  one of these.
1905 +  + uuid (not implemented yet)
1906 +  + fsid
1907 +  + dev
1908 +- the inode-number of the branch root dir
1909 +
1910 +And it contains these info in a single regular file.
1911 +- magic number
1912 +- branch's inode-number of the logically renamed dir
1913 +- the name of the before-renamed dir
1914 +
1915 +The "detailed info per directory" file is created in aufs rename(2), and
1916 +loaded in any lookup.
1917 +The info is considered in lookup for the matching case only. Here
1918 +"matching" means that the root of branch (in the info filename) is same
1919 +to the current looking-up branch. After looking-up the before-renamed
1920 +name, the inode-number is compared. And the matched dentry is used.
1921 +
1922 +The "inode-number list of directories" is a regular file which contains
1923 +simply the inode-numbers on the branch. The file is created or updated
1924 +in removing the branch, and loaded in adding the branch. Its lifetime is
1925 +equal to the branch.
1926 +The list is referred in lookup, and when the current target inode is
1927 +found in the list, the aufs tries loading the "detailed info per
1928 +directory" and get the changed and valid name of the dir.
1929 +
1930 +Theoretically these "extra informaiton" may be able to be put into XATTR
1931 +in the dir inode. But aufs doesn't choose this way because
1932 +1. XATTR may not be supported by the branch (or its configuration)
1933 +2. XATTR may have its size limit.
1934 +3. XATTR may be less easy to convert than a regular file, when the
1935 +   format of the info is changed in the future.
1936 +At the same time, I agree that the regular file approach is much slower
1937 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1938 +better approach.
1939 +
1940 +This DIRREN feature is enabled by aufs configuration, and is activated
1941 +by a new mount option.
1942 +
1943 +For the more complicated case, there is a work with UDBA option, which
1944 +is to dected the direct access to the branches (by-passing aufs) and to
1945 +maintain the cashes in aufs. Since a single cached aufs dentry may
1946 +contains two names, before- and after-rename, the name comparision in
1947 +UDBA handler may not work correctly. In this case, the behaviour will be
1948 +equivalen to udba=reval case.
1949 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1950 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1951 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2022-11-05 23:02:18.959222617 +0100
1952 @@ -0,0 +1,118 @@
1953 +
1954 +# Copyright (C) 2011-2022 Junjiro R. Okajima
1955 +#
1956 +# This program is free software; you can redistribute it and/or modify
1957 +# it under the terms of the GNU General Public License as published by
1958 +# the Free Software Foundation; either version 2 of the License, or
1959 +# (at your option) any later version.
1960 +#
1961 +# This program is distributed in the hope that it will be useful,
1962 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1963 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1964 +# GNU General Public License for more details.
1965 +#
1966 +# You should have received a copy of the GNU General Public License
1967 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1968 +
1969 +File-based Hierarchical Storage Management (FHSM)
1970 +----------------------------------------------------------------------
1971 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1972 +storage world. Aufs provides this feature as file-based with multiple
1973 +writable branches, based upon the principle of "Colder, the Lower".
1974 +Here the word "colder" means that the less used files, and "lower" means
1975 +that the position in the order of the stacked branches vertically.
1976 +These multiple writable branches are prioritized, ie. the topmost one
1977 +should be the fastest drive and be used heavily.
1978 +
1979 +o Characters in aufs FHSM story
1980 +- aufs itself and a new branch attribute.
1981 +- a new ioctl interface to move-down and to establish a connection with
1982 +  the daemon ("move-down" is a converse of "copy-up").
1983 +- userspace tool and daemon.
1984 +
1985 +The userspace daemon establishes a connection with aufs and waits for
1986 +the notification. The notified information is very similar to struct
1987 +statfs containing the number of consumed blocks and inodes.
1988 +When the consumed blocks/inodes of a branch exceeds the user-specified
1989 +upper watermark, the daemon activates its move-down process until the
1990 +consumed blocks/inodes reaches the user-specified lower watermark.
1991 +
1992 +The actual move-down is done by aufs based upon the request from
1993 +user-space since we need to maintain the inode number and the internal
1994 +pointer arrays in aufs.
1995 +
1996 +Currently aufs FHSM handles the regular files only. Additionally they
1997 +must not be hard-linked nor pseudo-linked.
1998 +
1999 +
2000 +o Cowork of aufs and the user-space daemon
2001 +  During the userspace daemon established the connection, aufs sends a
2002 +  small notification to it whenever aufs writes something into the
2003 +  writable branch. But it may cost high since aufs issues statfs(2)
2004 +  internally. So user can specify a new option to cache the
2005 +  info. Actually the notification is controlled by these factors.
2006 +  + the specified cache time.
2007 +  + classified as "force" by aufs internally.
2008 +  Until the specified time expires, aufs doesn't send the info
2009 +  except the forced cases. When aufs decide forcing, the info is always
2010 +  notified to userspace.
2011 +  For example, the number of free inodes is generally large enough and
2012 +  the shortage of it happens rarely. So aufs doesn't force the
2013 +  notification when creating a new file, directory and others. This is
2014 +  the typical case which aufs doesn't force.
2015 +  When aufs writes the actual filedata and the files consumes any of new
2016 +  blocks, the aufs forces notifying.
2017 +
2018 +
2019 +o Interfaces in aufs
2020 +- New branch attribute.
2021 +  + fhsm
2022 +    Specifies that the branch is managed by FHSM feature. In other word,
2023 +    participant in the FHSM.
2024 +    When nofhsm is set to the branch, it will not be the source/target
2025 +    branch of the move-down operation. This attribute is set
2026 +    independently from coo and moo attributes, and if you want full
2027 +    FHSM, you should specify them as well.
2028 +- New mount option.
2029 +  + fhsm_sec
2030 +    Specifies a second to suppress many less important info to be
2031 +    notified.
2032 +- New ioctl.
2033 +  + AUFS_CTL_FHSM_FD
2034 +    create a new file descriptor which userspace can read the notification
2035 +    (a subset of struct statfs) from aufs.
2036 +- Module parameter 'brs'
2037 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2038 +  be set.
2039 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2040 +  When there are two or more branches with fhsm attributes,
2041 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2042 +  terminates it. As a result of remounting and branch-manipulation, the
2043 +  number of branches with fhsm attribute can be one. In this case,
2044 +  /sbin/mount.aufs will terminate the user-space daemon.
2045 +
2046 +
2047 +Finally the operation is done as these steps in kernel-space.
2048 +- make sure that,
2049 +  + no one else is using the file.
2050 +  + the file is not hard-linked.
2051 +  + the file is not pseudo-linked.
2052 +  + the file is a regular file.
2053 +  + the parent dir is not opaqued.
2054 +- find the target writable branch.
2055 +- make sure the file is not whiteout-ed by the upper (than the target)
2056 +  branch.
2057 +- make the parent dir on the target branch.
2058 +- mutex lock the inode on the branch.
2059 +- unlink the whiteout on the target branch (if exists).
2060 +- lookup and create the whiteout-ed temporary name on the target branch.
2061 +- copy the file as the whiteout-ed temporary name on the target branch.
2062 +- rename the whiteout-ed temporary name to the original name.
2063 +- unlink the file on the source branch.
2064 +- maintain the internal pointer array and the external inode number
2065 +  table (XINO).
2066 +- maintain the timestamps and other attributes of the parent dir and the
2067 +  file.
2068 +
2069 +And of course, in every step, an error may happen. So the operation
2070 +should restore the original file state after an error happens.
2071 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2072 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2073 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2022-11-05 23:02:18.959222617 +0100
2074 @@ -0,0 +1,72 @@
2075 +
2076 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2077 +#
2078 +# This program is free software; you can redistribute it and/or modify
2079 +# it under the terms of the GNU General Public License as published by
2080 +# the Free Software Foundation; either version 2 of the License, or
2081 +# (at your option) any later version.
2082 +#
2083 +# This program is distributed in the hope that it will be useful,
2084 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2085 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2086 +# GNU General Public License for more details.
2087 +#
2088 +# You should have received a copy of the GNU General Public License
2089 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2090 +
2091 +mmap(2) -- File Memory Mapping
2092 +----------------------------------------------------------------------
2093 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2094 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2095 +->mmap().
2096 +This approach is simple and good, but there is one problem.
2097 +Under /proc, several entries show the mmapped files by its path (with
2098 +device and inode number), and the printed path will be the path on the
2099 +branch fs's instead of virtual aufs's.
2100 +This is not a problem in most cases, but some utilities lsof(1) (and its
2101 +user) may expect the path on aufs.
2102 +
2103 +To address this issue, aufs adds a new member called vm_prfile in struct
2104 +vm_area_struct (and struct vm_region). The original vm_file points to
2105 +the file on the branch fs in order to handle everything correctly as
2106 +usual. The new vm_prfile points to a virtual file in aufs, and the
2107 +show-functions in procfs refers to vm_prfile if it is set.
2108 +Also we need to maintain several other places where touching vm_file
2109 +such like
2110 +- fork()/clone() copies vma and the reference count of vm_file is
2111 +  incremented.
2112 +- merging vma maintains the ref count too.
2113 +
2114 +This is not a good approach. It just fakes the printed path. But it
2115 +leaves all behaviour around f_mapping unchanged. This is surely an
2116 +advantage.
2117 +Actually aufs had adopted another complicated approach which calls
2118 +generic_file_mmap() and handles struct vm_operations_struct. In this
2119 +approach, aufs met a hard problem and I could not solve it without
2120 +switching the approach.
2121 +
2122 +There may be one more another approach which is
2123 +- bind-mount the branch-root onto the aufs-root internally
2124 +- grab the new vfsmount (ie. struct mount)
2125 +- lazy-umount the branch-root internally
2126 +- in open(2) the aufs-file, open the branch-file with the hidden
2127 +  vfsmount (instead of the original branch's vfsmount)
2128 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2129 +  but it may be possible from userspace by the mount helper.
2130 +
2131 +Adding the internal hidden vfsmount and using it in opening a file, the
2132 +file path under /proc will be printed correctly. This approach looks
2133 +smarter, but is not possible I am afraid.
2134 +- aufs-root may be bind-mount later. when it happens, another hidden
2135 +  vfsmount will be required.
2136 +- it is hard to get the chance to bind-mount and lazy-umount
2137 +  + in kernel-space, FS can have vfsmount in open(2) via
2138 +    file->f_path, and aufs can know its vfsmount. But several locks are
2139 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2140 +    here, then it may cause a deadlock.
2141 +  + in user-space, bind-mount doesn't invoke the mount helper.
2142 +- since /proc shows dev and ino, aufs has to give vma these info. it
2143 +  means a new member vm_prinode will be necessary. this is essentially
2144 +  equivalent to vm_prfile described above.
2145 +
2146 +I have to give up this "looks-smater" approach.
2147 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2148 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2149 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2022-11-05 23:02:18.959222617 +0100
2150 @@ -0,0 +1,94 @@
2151 +
2152 +# Copyright (C) 2014-2022 Junjiro R. Okajima
2153 +#
2154 +# This program is free software; you can redistribute it and/or modify
2155 +# it under the terms of the GNU General Public License as published by
2156 +# the Free Software Foundation; either version 2 of the License, or
2157 +# (at your option) any later version.
2158 +#
2159 +# This program is distributed in the hope that it will be useful,
2160 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2161 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2162 +# GNU General Public License for more details.
2163 +#
2164 +# You should have received a copy of the GNU General Public License
2165 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2166 +
2167 +Listing XATTR/EA and getting the value
2168 +----------------------------------------------------------------------
2169 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2170 +shows the values from the topmost existing file. This behaviour is good
2171 +for the non-dir entries since the bahaviour exactly matches the shown
2172 +information. But for the directories, aufs considers all the same named
2173 +entries on the lower branches. Which means, if one of the lower entry
2174 +rejects readdir call, then aufs returns an error even if the topmost
2175 +entry allows it. This behaviour is necessary to respect the branch fs's
2176 +security, but can make users confused since the user-visible standard
2177 +attributes don't match the behaviour.
2178 +To address this issue, aufs has a mount option called dirperm1 which
2179 +checks the permission for the topmost entry only, and ignores the lower
2180 +entry's permission.
2181 +
2182 +A similar issue can happen around XATTR.
2183 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2184 +always set. Otherwise these very unpleasant situation would happen.
2185 +- listxattr(2) may return the duplicated entries.
2186 +- users may not be able to remove or reset the XATTR forever,
2187 +
2188 +
2189 +XATTR/EA support in the internal (copy,move)-(up,down)
2190 +----------------------------------------------------------------------
2191 +Generally the extended attributes of inode are categorized as these.
2192 +- "security" for LSM and capability.
2193 +- "system" for posix ACL, 'acl' mount option is required for the branch
2194 +  fs generally.
2195 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2196 +- "user" for userspace, 'user_xattr' mount option is required for the
2197 +  branch fs generally.
2198 +
2199 +Moreover there are some other categories. Aufs handles these rather
2200 +unpopular categories as the ordinary ones, ie. there is no special
2201 +condition nor exception.
2202 +
2203 +In copy-up, the support for XATTR on the dst branch may differ from the
2204 +src branch. In this case, the copy-up operation will get an error and
2205 +the original user operation which triggered the copy-up will fail. It
2206 +can happen that even all copy-up will fail.
2207 +When both of src and dst branches support XATTR and if an error occurs
2208 +during copying XATTR, then the copy-up should fail obviously. That is a
2209 +good reason and aufs should return an error to userspace. But when only
2210 +the src branch support that XATTR, aufs should not return an error.
2211 +For example, the src branch supports ACL but the dst branch doesn't
2212 +because the dst branch may natively un-support it or temporary
2213 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2214 +may NOT return an error even if the XATTR is not supported. It is
2215 +totally up to the branch fs.
2216 +
2217 +Anyway when the aufs internal copy-up gets an error from the dst branch
2218 +fs, then aufs tries removing the just copied entry and returns the error
2219 +to the userspace. The worst case of this situation will be all copy-up
2220 +will fail.
2221 +
2222 +For the copy-up operation, there two basic approaches.
2223 +- copy the specified XATTR only (by category above), and return the
2224 +  error unconditionally if it happens.
2225 +- copy all XATTR, and ignore the error on the specified category only.
2226 +
2227 +In order to support XATTR and to implement the correct behaviour, aufs
2228 +chooses the latter approach and introduces some new branch attributes,
2229 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2230 +They correspond to the XATTR namespaces (see above). Additionally, to be
2231 +convenient, "icex" is also provided which means all "icex*" attributes
2232 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2233 +
2234 +The meaning of these attributes is to ignore the error from setting
2235 +XATTR on that branch.
2236 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2237 +error from the dst branch according to the specified attributes.
2238 +
2239 +Some XATTR may have its default value. The default value may come from
2240 +the parent dir or the environment. If the default value is set at the
2241 +file creating-time, it will be overwritten by copy-up.
2242 +Some contradiction may happen I am afraid.
2243 +Do we need another attribute to stop copying XATTR? I am unsure. For
2244 +now, aufs implements the branch attributes to ignore the error.
2245 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2246 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2247 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2022-11-05 23:02:18.959222617 +0100
2248 @@ -0,0 +1,58 @@
2249 +
2250 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2251 +#
2252 +# This program is free software; you can redistribute it and/or modify
2253 +# it under the terms of the GNU General Public License as published by
2254 +# the Free Software Foundation; either version 2 of the License, or
2255 +# (at your option) any later version.
2256 +#
2257 +# This program is distributed in the hope that it will be useful,
2258 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2259 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2260 +# GNU General Public License for more details.
2261 +#
2262 +# You should have received a copy of the GNU General Public License
2263 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2264 +
2265 +Export Aufs via NFS
2266 +----------------------------------------------------------------------
2267 +Here is an approach.
2268 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2269 +  generation.
2270 +- iget_locked(): initialize aufs inode generation for a new inode, and
2271 +  store it in xigen file.
2272 +- destroy_inode(): increment aufs inode generation and store it in xigen
2273 +  file. it is necessary even if it is not unlinked, because any data of
2274 +  inode may be changed by UDBA.
2275 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2276 +  build file handle by
2277 +  + branch id (4 bytes)
2278 +  + superblock generation (4 bytes)
2279 +  + inode number (4 or 8 bytes)
2280 +  + parent dir inode number (4 or 8 bytes)
2281 +  + inode generation (4 bytes))
2282 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2283 +    bytes)
2284 +  + file handle for a branch (by exportfs_encode_fh())
2285 +- fh_to_dentry():
2286 +  + find the index of a branch from its id in handle, and check it is
2287 +    still exist in aufs.
2288 +  + 1st level: get the inode number from handle and search it in cache.
2289 +  + 2nd level: if not found in cache, get the parent inode number from
2290 +    the handle and search it in cache. and then open the found parent
2291 +    dir, find the matching inode number by vfs_readdir() and get its
2292 +    name, and call lookup_one_len() for the target dentry.
2293 +  + 3rd level: if the parent dir is not cached, call
2294 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2295 +    build a pathname of it, convert it a pathname in aufs, call
2296 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2297 +    the 2nd level.
2298 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2299 +    for every branch, but not itself. to get this, (currently) aufs
2300 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2301 +    idea, but I didn't get other approach.
2302 +  + test the generation of the gotten inode.
2303 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2304 +  convert it into ESTALE for NFSD.
2305 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2306 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2307 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2308 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2309 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2022-11-05 23:02:18.959222617 +0100
2310 @@ -0,0 +1,52 @@
2311 +
2312 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2313 +#
2314 +# This program is free software; you can redistribute it and/or modify
2315 +# it under the terms of the GNU General Public License as published by
2316 +# the Free Software Foundation; either version 2 of the License, or
2317 +# (at your option) any later version.
2318 +#
2319 +# This program is distributed in the hope that it will be useful,
2320 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2321 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2322 +# GNU General Public License for more details.
2323 +#
2324 +# You should have received a copy of the GNU General Public License
2325 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2326 +
2327 +Show Whiteout Mode (shwh)
2328 +----------------------------------------------------------------------
2329 +Generally aufs hides the name of whiteouts. But in some cases, to show
2330 +them is very useful for users. For instance, creating a new middle layer
2331 +(branch) by merging existing layers.
2332 +
2333 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2334 +When you have three branches,
2335 +- Bottom: 'system', squashfs (underlying base system), read-only
2336 +- Middle: 'mods', squashfs, read-only
2337 +- Top: 'overlay', ram (tmpfs), read-write
2338 +
2339 +The top layer is loaded at boot time and saved at shutdown, to preserve
2340 +the changes made to the system during the session.
2341 +When larger changes have been made, or smaller changes have accumulated,
2342 +the size of the saved top layer data grows. At this point, it would be
2343 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2344 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2345 +restoring save and load speed.
2346 +
2347 +This merging is simplified by the use of another aufs mount, of just the
2348 +two overlay branches using the 'shwh' option.
2349 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2350 +       aufs /livesys/merge_union
2351 +
2352 +A merged view of these two branches is then available at
2353 +/livesys/merge_union, and the new feature is that the whiteouts are
2354 +visible!
2355 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2356 +writing to all branches. Also the default mode for all branches is 'ro'.
2357 +It is now possible to save the combined contents of the two overlay
2358 +branches to a new squashfs, e.g.:
2359 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2360 +
2361 +This new squashfs archive can be stored on the boot device and the
2362 +initramfs will use it to replace the old one at the next boot.
2363 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2364 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2365 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2022-11-05 23:02:18.959222617 +0100
2366 @@ -0,0 +1,47 @@
2367 +
2368 +# Copyright (C) 2010-2022 Junjiro R. Okajima
2369 +#
2370 +# This program is free software; you can redistribute it and/or modify
2371 +# it under the terms of the GNU General Public License as published by
2372 +# the Free Software Foundation; either version 2 of the License, or
2373 +# (at your option) any later version.
2374 +#
2375 +# This program is distributed in the hope that it will be useful,
2376 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2377 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2378 +# GNU General Public License for more details.
2379 +#
2380 +# You should have received a copy of the GNU General Public License
2381 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2382 +
2383 +Dynamically customizable FS operations
2384 +----------------------------------------------------------------------
2385 +Generally FS operations (struct inode_operations, struct
2386 +address_space_operations, struct file_operations, etc.) are defined as
2387 +"static const", but it never means that FS have only one set of
2388 +operation. Some FS have multiple sets of them. For instance, ext2 has
2389 +three sets, one for XIP, for NOBH, and for normal.
2390 +Since aufs overrides and redirects these operations, sometimes aufs has
2391 +to change its behaviour according to the branch FS type. More importantly
2392 +VFS acts differently if a function (member in the struct) is set or
2393 +not. It means aufs should have several sets of operations and select one
2394 +among them according to the branch FS definition.
2395 +
2396 +In order to solve this problem and not to affect the behaviour of VFS,
2397 +aufs defines these operations dynamically. For instance, aufs defines
2398 +dummy direct_IO function for struct address_space_operations, but it may
2399 +not be set to the address_space_operations actually. When the branch FS
2400 +doesn't have it, aufs doesn't set it to its address_space_operations
2401 +while the function definition itself is still alive. So the behaviour
2402 +itself will not change, and it will return an error when direct_IO is
2403 +not set.
2404 +
2405 +The lifetime of these dynamically generated operation object is
2406 +maintained by aufs branch object. When the branch is removed from aufs,
2407 +the reference counter of the object is decremented. When it reaches
2408 +zero, the dynamically generated operation object will be freed.
2409 +
2410 +This approach is designed to support AIO (io_submit), Direct I/O and
2411 +XIP (DAX) mainly.
2412 +Currently this approach is applied to address_space_operations for
2413 +regular files only.
2414 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2415 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2416 +++ linux/Documentation/filesystems/aufs/README 2023-07-04 23:43:55.846316200 +0200
2417 @@ -0,0 +1,408 @@
2418 +
2419 +Aufs6 -- advanced multi layered unification filesystem version 6.x
2420 +http://aufs.sf.net
2421 +Junjiro R. Okajima
2422 +
2423 +
2424 +0. Introduction
2425 +----------------------------------------
2426 +In the early days, aufs was entirely re-designed and re-implemented
2427 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2428 +improvements and implementations, it became totally different from
2429 +Unionfs while keeping the basic features.
2430 +Later, Unionfs Version 2.x series began taking some of the same
2431 +approaches to aufs1's.
2432 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2433 +University and his team.
2434 +
2435 +Aufs6 supports linux-v6.0 and later, try aufs6.0 branch in
2436 +aufs-linux.git or aufs-standalone.git.
2437 +If you want older kernel version support,
2438 +- for linux-v5.x series, try aufs-linux.git or aufs-standalone.git
2439 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2440 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2441 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2442 +  or aufs1 from CVS on SourceForge.
2443 +
2444 +Note: the name of aufs5-linux.git and aufs5-standalone.git on github
2445 +      were changed. Now they are aufs-linux.git and
2446 +      aufs-standalone.git and they contain aufs5 and later branches.
2447 +
2448 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2449 +      According to Christoph Hellwig, linux rejects all union-type
2450 +      filesystems but UnionMount.
2451 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2452 +
2453 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2454 +    UnionMount, and he pointed out an issue around a directory mutex
2455 +    lock and aufs addressed it. But it is still unsure whether aufs will
2456 +    be merged (or any other union solution).
2457 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2458 +
2459 +
2460 +1. Features
2461 +----------------------------------------
2462 +- unite several directories into a single virtual filesystem. The member
2463 +  directory is called as a branch.
2464 +- you can specify the permission flags to the branch, which are 'readonly',
2465 +  'readwrite' and 'whiteout-able.'
2466 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2467 +  readonly branch are modifiable logically.
2468 +- dynamic branch manipulation, add, del.
2469 +- etc...
2470 +
2471 +Also there are many enhancements in aufs, such as:
2472 +- test only the highest one for the directory permission (dirperm1)
2473 +- copyup on open (coo=)
2474 +- 'move' policy for copy-up between two writable branches, after
2475 +  checking free space.
2476 +- xattr, acl
2477 +- readdir(3) in userspace.
2478 +- keep inode number by external inode number table
2479 +- keep the timestamps of file/dir in internal copyup operation
2480 +- seekable directory, supporting NFS readdir.
2481 +- whiteout is hardlinked in order to reduce the consumption of inodes
2482 +  on branch
2483 +- do not copyup, nor create a whiteout when it is unnecessary
2484 +- revert a single systemcall when an error occurs in aufs
2485 +- remount interface instead of ioctl
2486 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2487 +- loopback mounted filesystem as a branch
2488 +- kernel thread for removing the dir who has a plenty of whiteouts
2489 +- support copyup sparse file (a file which has a 'hole' in it)
2490 +- default permission flags for branches
2491 +- selectable permission flags for ro branch, whether whiteout can
2492 +  exist or not
2493 +- export via NFS.
2494 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2495 +- support multiple writable branches, some policies to select one
2496 +  among multiple writable branches.
2497 +- a new semantics for link(2) and rename(2) to support multiple
2498 +  writable branches.
2499 +- no glibc changes are required.
2500 +- pseudo hardlink (hardlink over branches)
2501 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2502 +  including NFS or remote filesystem branch.
2503 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2504 +- and more...
2505 +
2506 +Currently these features are dropped temporary from aufs6.
2507 +See design/08plan.txt in detail.
2508 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2509 +  (robr)
2510 +- statistics of aufs thread (/sys/fs/aufs/stat)
2511 +
2512 +Features or just an idea in the future (see also design/*.txt),
2513 +- reorder the branch index without del/re-add.
2514 +- permanent xino files for NFSD
2515 +- an option for refreshing the opened files after add/del branches
2516 +- light version, without branch manipulation. (unnecessary?)
2517 +- copyup in userspace
2518 +- inotify in userspace
2519 +- readv/writev
2520 +
2521 +
2522 +2. Download
2523 +----------------------------------------
2524 +There are three GIT trees for aufs6, aufs-linux.git,
2525 +aufs-standalone.git, and aufs-util.git.
2526 +While the aufs-util is always necessary, you need either of aufs-linux
2527 +or aufs-standalone.
2528 +
2529 +The aufs-linux tree includes the whole linux mainline GIT tree,
2530 +git://git.kernel.org/.../torvalds/linux.git.
2531 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2532 +build aufs6 as an external kernel module.
2533 +Several extra patches are not included in this tree. Only
2534 +aufs-standalone tree contains them. They are described in the later
2535 +section "Configuration and Compilation."
2536 +
2537 +On the other hand, the aufs-standalone tree has only aufs source files
2538 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2539 +But you need to apply all aufs patches manually.
2540 +
2541 +You will find GIT branches whose name is in form of "aufs6.x" where "x"
2542 +represents the linux kernel version, "linux-6.x". For instance,
2543 +"aufs6.0" is for linux-6.0. For latest "linux-6.x-rcN", use
2544 +"aufs6.x-rcN" branch.
2545 +
2546 +o aufs-linux tree
2547 +$ git clone --reference /your/linux/git/tree \
2548 +       git://github.com/sfjro/aufs-linux.git aufs-linux.git
2549 +- if you don't have linux GIT tree, then remove "--reference ..."
2550 +$ cd aufs-linux.git
2551 +$ git checkout origin/aufs6.0
2552 +
2553 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2554 +leave the patch-work to GIT.
2555 +$ cd /your/linux/git/tree
2556 +$ git remote add aufs git://github.com/sfjro/aufs-linux.git
2557 +$ git fetch aufs
2558 +$ git checkout -b my6.0 v6.0
2559 +$ (add your local change...)
2560 +$ git pull aufs aufs6.0
2561 +- now you have v6.0 + your_changes + aufs6.0 in you my6.0 branch.
2562 +- you may need to solve some conflicts between your_changes and
2563 +  aufs6.0. in this case, git-rerere is recommended so that you can
2564 +  solve the similar conflicts automatically when you upgrade to 6.1 or
2565 +  later in the future.
2566 +
2567 +o aufs-standalone tree
2568 +$ git clone git://github.com/sfjro/aufs-standalone.git aufs-standalone.git
2569 +$ cd aufs-standalone.git
2570 +$ git checkout origin/aufs6.0
2571 +
2572 +o aufs-util tree
2573 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2574 +- note that the public aufs-util.git is on SourceForge instead of
2575 +  GitHUB.
2576 +$ cd aufs-util.git
2577 +$ git checkout origin/aufs6.0
2578 +
2579 +Note: The 6.x-rcN branch is to be used with `rc' kernel versions ONLY.
2580 +The minor version number, 'x' in '6.x', of aufs may not always
2581 +follow the minor version number of the kernel.
2582 +Because changes in the kernel that cause the use of a new
2583 +minor version number do not always require changes to aufs-util.
2584 +
2585 +Since aufs-util has its own minor version number, you may not be
2586 +able to find a GIT branch in aufs-util for your kernel's
2587 +exact minor version number.
2588 +In this case, you should git-checkout the branch for the
2589 +nearest lower number.
2590 +
2591 +For (an unreleased) example:
2592 +If you are using "linux-6.10" and the "aufs6.10" branch
2593 +does not exist in aufs-util repository, then "aufs6.9", "aufs6.8"
2594 +or something numerically smaller is the branch for your kernel.
2595 +
2596 +Also you can view all branches by
2597 +       $ git branch -a
2598 +
2599 +
2600 +3. Configuration and Compilation
2601 +----------------------------------------
2602 +Make sure you have git-checkout'ed the correct branch.
2603 +
2604 +For aufs-linux tree,
2605 +- enable CONFIG_AUFS_FS.
2606 +- set other aufs configurations if necessary.
2607 +- for aufs5.13 and later
2608 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2609 +  also a caller of VFS functions for branch filesystems, subclassing of
2610 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2611 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2612 +  need to customize some LOCKDEP numbers. Here are what I use on my
2613 +  test environment.
2614 +       CONFIG_LOCKDEP_BITS=21
2615 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2616 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2617 +  Also you will need to expand some constant values in LOCKDEP. Refer
2618 +  to lockdep-debug.patch in aufs-standalone.git.
2619 +
2620 +For aufs-standalone tree,
2621 +There are several ways to build.
2622 +
2623 +1.
2624 +- apply ./aufs6-kbuild.patch to your kernel source files.
2625 +- apply ./aufs6-base.patch too.
2626 +- apply ./aufs6-mmap.patch too.
2627 +- apply ./aufs6-standalone.patch too, if you have a plan to set
2628 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs-standalone.patch.
2629 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2630 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2631 +- enable CONFIG_AUFS_FS, you can select either
2632 +  =m or =y.
2633 +- and build your kernel as usual.
2634 +- install the built kernel.
2635 +- install the header files too by "make headers_install" to the
2636 +  directory where you specify. By default, it is $PWD/usr.
2637 +  "make help" shows a brief note for headers_install.
2638 +- and reboot your system.
2639 +
2640 +2.
2641 +- module only (CONFIG_AUFS_FS=m).
2642 +- apply ./aufs6-base.patch to your kernel source files.
2643 +- apply ./aufs6-mmap.patch too.
2644 +- apply ./aufs6-standalone.patch too.
2645 +- build your kernel, don't forget "make headers_install", and reboot.
2646 +- edit ./config.mk and set other aufs configurations if necessary.
2647 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2648 +  every aufs configurations.
2649 +- build the module by simple "make".
2650 +- you can specify ${KDIR} make variable which points to your kernel
2651 +  source tree.
2652 +- install the files
2653 +  + run "make install" to install the aufs module, or copy the built
2654 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2655 +  + run "make install_headers" (instead of headers_install) to install
2656 +    the modified aufs header file (you can specify DESTDIR which is
2657 +    available in aufs standalone version's Makefile only), or copy
2658 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2659 +    you like manually. By default, the target directory is $PWD/usr.
2660 +- no need to apply aufs6-kbuild.patch, nor copying source files to your
2661 +  kernel source tree.
2662 +
2663 +Note: The header file aufs_type.h is necessary to build aufs-util
2664 +      as well as "make headers_install" in the kernel source tree.
2665 +      headers_install is subject to be forgotten, but it is essentially
2666 +      necessary, not only for building aufs-util.
2667 +      You may not meet problems without headers_install in some older
2668 +      version though.
2669 +
2670 +And then,
2671 +- read README in aufs-util, build and install it
2672 +- note that your distribution may contain an obsoleted version of
2673 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2674 +  utilities, make sure that your compiler refers the correct aufs header
2675 +  file which is built by "make headers_install."
2676 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2677 +  then run "make install_ulib" too. And refer to the aufs manual in
2678 +  detail.
2679 +
2680 +There several other patches in aufs-standalone.git. They are all
2681 +optional. When you meet some problems, they will help you.
2682 +- aufs6-loopback.patch
2683 +  Supports a nested loopback mount in a branch-fs. This patch is
2684 +  unnecessary until aufs produces a message like "you may want to try
2685 +  another patch for loopback file".
2686 +- vfs-ino.patch
2687 +  Modifies a system global kernel internal function get_next_ino() in
2688 +  order to stop assigning 0 for an inode-number. Not directly related to
2689 +  aufs, but recommended generally.
2690 +- tmpfs-idr.patch
2691 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2692 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2693 +  duplication of inode number, which is important for backup tools and
2694 +  other utilities. When you find aufs XINO files for tmpfs branch
2695 +  growing too much, try this patch.
2696 +- lockdep-debug.patch
2697 +  Similar to some kernel configurations for LOCKDEP (see the top of
2698 +  this section), you will need expand some constants in LOCKDEP for
2699 +  aufs if you enable CONFIG_LOCKDEP.
2700 +
2701 +
2702 +4. Usage
2703 +----------------------------------------
2704 +At first, make sure aufs-util are installed, and please read the aufs
2705 +manual, aufs.5 in aufs-util.git tree.
2706 +$ man -l aufs.5
2707 +
2708 +And then,
2709 +$ mkdir /tmp/rw /tmp/aufs
2710 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2711 +
2712 +Here is another example. The result is equivalent.
2713 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2714 +  Or
2715 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2716 +# mount -o remount,append:${HOME} /tmp/aufs
2717 +
2718 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2719 +you modify a file under /tmp/aufs, the one on your home directory is
2720 +not affected, instead the same named file will be newly created under
2721 +/tmp/rw. And all of your modification to a file will be applied to
2722 +the one under /tmp/rw. This is called the file based Copy on Write
2723 +(COW) method.
2724 +Aufs mount options are described in aufs.5.
2725 +If you run chroot or something and make your aufs as a root directory,
2726 +then you need to customize the shutdown script. See the aufs manual in
2727 +detail.
2728 +
2729 +Additionally, there are some sample usages of aufs which are a
2730 +diskless system with network booting, and LiveCD over NFS.
2731 +See sample dir in CVS tree on SourceForge.
2732 +
2733 +
2734 +5. Contact
2735 +----------------------------------------
2736 +When you have any problems or strange behaviour in aufs, please let me
2737 +know with:
2738 +- /proc/mounts (instead of the output of mount(8))
2739 +- /sys/module/aufs/*
2740 +- /sys/fs/aufs/* (if you have them)
2741 +- /debug/aufs/* (if you have them)
2742 +- linux kernel version
2743 +  if your kernel is not plain, for example modified by distributor,
2744 +  the url where i can download its source is necessary too.
2745 +- aufs version which was printed at loading the module or booting the
2746 +  system, instead of the date you downloaded.
2747 +- configuration (define/undefine CONFIG_AUFS_xxx)
2748 +- kernel configuration or /proc/config.gz (if you have it)
2749 +- LSM (linux security module, if you are using)
2750 +- behaviour which you think to be incorrect
2751 +- actual operation, reproducible one is better
2752 +- mailto: aufs-users at lists.sourceforge.net
2753 +
2754 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2755 +and Feature Requests) on SourceForge. Please join and write to
2756 +aufs-users ML.
2757 +
2758 +
2759 +6. Acknowledgements
2760 +----------------------------------------
2761 +Thanks to everyone who have tried and are using aufs, whoever
2762 +have reported a bug or any feedback.
2763 +
2764 +Especially donators:
2765 +Tomas Matejicek(slax.org) made a donation (much more than once).
2766 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2767 +       scripts) is making "doubling" donations.
2768 +       Unfortunately I cannot list all of the donators, but I really
2769 +       appreciate.
2770 +       It ends Aug 2010, but the ordinary donation URL is still available.
2771 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2772 +Dai Itasaka made a donation (2007/8).
2773 +Chuck Smith made a donation (2008/4, 10 and 12).
2774 +Henk Schoneveld made a donation (2008/9).
2775 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2776 +Francois Dupoux made a donation (2008/11).
2777 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2778 +       aufs2 GIT tree (2009/2).
2779 +William Grant made a donation (2009/3).
2780 +Patrick Lane made a donation (2009/4).
2781 +The Mail Archive (mail-archive.com) made donations (2009/5).
2782 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2783 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2784 +Pavel Pronskiy made a donation (2011/2).
2785 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2786 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2787 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2788 +11).
2789 +Sam Liddicott made a donation (2011/9).
2790 +Era Scarecrow made a donation (2013/4).
2791 +Bor Ratajc made a donation (2013/4).
2792 +Alessandro Gorreta made a donation (2013/4).
2793 +POIRETTE Marc made a donation (2013/4).
2794 +Alessandro Gorreta made a donation (2013/4).
2795 +lauri kasvandik made a donation (2013/5).
2796 +"pemasu from Finland" made a donation (2013/7).
2797 +The Parted Magic Project made a donation (2013/9 and 11).
2798 +Pavel Barta made a donation (2013/10).
2799 +Nikolay Pertsev made a donation (2014/5).
2800 +James B made a donation (2014/7, 2015/7, and 2021/12).
2801 +Stefano Di Biase made a donation (2014/8).
2802 +Daniel Epellei made a donation (2015/1).
2803 +OmegaPhil made a donation (2016/1, 2018/4).
2804 +Tomasz Szewczyk made a donation (2016/4).
2805 +James Burry made a donation (2016/12).
2806 +Carsten Rose made a donation (2018/9).
2807 +Porteus Kiosk made a donation (2018/10).
2808 +huronOS team: Enya Quetzalli made donations (2022/5 and 2023/5).
2809 +
2810 +Thank you very much.
2811 +Donations are always, including future donations, very important and
2812 +helpful for me to keep on developing aufs.
2813 +
2814 +
2815 +7.
2816 +----------------------------------------
2817 +If you are an experienced user, no explanation is needed. Aufs is
2818 +just a linux filesystem.
2819 +
2820 +
2821 +Enjoy!
2822 +
2823 +# Local variables: ;
2824 +# mode: text;
2825 +# End: ;
2826 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2827 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2828 +++ linux/fs/aufs/aufs.h        2022-11-05 23:02:18.959222617 +0100
2829 @@ -0,0 +1,62 @@
2830 +/* SPDX-License-Identifier: GPL-2.0 */
2831 +/*
2832 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2833 + *
2834 + * This program is free software; you can redistribute it and/or modify
2835 + * it under the terms of the GNU General Public License as published by
2836 + * the Free Software Foundation; either version 2 of the License, or
2837 + * (at your option) any later version.
2838 + *
2839 + * This program is distributed in the hope that it will be useful,
2840 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2841 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2842 + * GNU General Public License for more details.
2843 + *
2844 + * You should have received a copy of the GNU General Public License
2845 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2846 + */
2847 +
2848 +/*
2849 + * all header files
2850 + */
2851 +
2852 +#ifndef __AUFS_H__
2853 +#define __AUFS_H__
2854 +
2855 +#ifdef __KERNEL__
2856 +
2857 +#define AuStub(type, name, body, ...) \
2858 +       static inline type name(__VA_ARGS__) { body; }
2859 +
2860 +#define AuStubVoid(name, ...) \
2861 +       AuStub(void, name, , __VA_ARGS__)
2862 +#define AuStubInt0(name, ...) \
2863 +       AuStub(int, name, return 0, __VA_ARGS__)
2864 +
2865 +#include "debug.h"
2866 +
2867 +#include "branch.h"
2868 +#include "cpup.h"
2869 +#include "dcsub.h"
2870 +#include "dbgaufs.h"
2871 +#include "dentry.h"
2872 +#include "dir.h"
2873 +#include "dirren.h"
2874 +#include "dynop.h"
2875 +#include "file.h"
2876 +#include "fstype.h"
2877 +#include "hbl.h"
2878 +#include "inode.h"
2879 +#include "lcnt.h"
2880 +#include "loop.h"
2881 +#include "module.h"
2882 +#include "opts.h"
2883 +#include "rwsem.h"
2884 +#include "super.h"
2885 +#include "sysaufs.h"
2886 +#include "vfsub.h"
2887 +#include "whout.h"
2888 +#include "wkq.h"
2889 +
2890 +#endif /* __KERNEL__ */
2891 +#endif /* __AUFS_H__ */
2892 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2893 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2894 +++ linux/fs/aufs/branch.c      2022-11-05 23:02:18.959222617 +0100
2895 @@ -0,0 +1,1427 @@
2896 +// SPDX-License-Identifier: GPL-2.0
2897 +/*
2898 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2899 + *
2900 + * This program is free software; you can redistribute it and/or modify
2901 + * it under the terms of the GNU General Public License as published by
2902 + * the Free Software Foundation; either version 2 of the License, or
2903 + * (at your option) any later version.
2904 + *
2905 + * This program is distributed in the hope that it will be useful,
2906 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2907 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2908 + * GNU General Public License for more details.
2909 + *
2910 + * You should have received a copy of the GNU General Public License
2911 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2912 + */
2913 +
2914 +/*
2915 + * branch management
2916 + */
2917 +
2918 +#include <linux/compat.h>
2919 +#include <linux/statfs.h>
2920 +#include "aufs.h"
2921 +
2922 +/*
2923 + * free a single branch
2924 + */
2925 +static void au_br_do_free(struct au_branch *br)
2926 +{
2927 +       int i;
2928 +       struct au_wbr *wbr;
2929 +       struct au_dykey **key;
2930 +
2931 +       au_hnotify_fin_br(br);
2932 +       /* always, regardless the mount option */
2933 +       au_dr_hino_free(&br->br_dirren);
2934 +       au_xino_put(br);
2935 +
2936 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2937 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2938 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2939 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2940 +
2941 +       wbr = br->br_wbr;
2942 +       if (wbr) {
2943 +               for (i = 0; i < AuBrWh_Last; i++)
2944 +                       dput(wbr->wbr_wh[i]);
2945 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2946 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2947 +       }
2948 +
2949 +       if (br->br_fhsm) {
2950 +               au_br_fhsm_fin(br->br_fhsm);
2951 +               au_kfree_try_rcu(br->br_fhsm);
2952 +       }
2953 +
2954 +       key = br->br_dykey;
2955 +       for (i = 0; i < AuBrDynOp; i++, key++)
2956 +               if (*key)
2957 +                       au_dy_put(*key);
2958 +               else
2959 +                       break;
2960 +
2961 +       /* recursive lock, s_umount of branch's */
2962 +       /* synchronize_rcu(); */ /* why? */
2963 +       lockdep_off();
2964 +       path_put(&br->br_path);
2965 +       lockdep_on();
2966 +       au_kfree_rcu(wbr);
2967 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2968 +       au_lcnt_wait_for_fin(&br->br_count);
2969 +       /* I don't know why, but percpu_refcount requires this */
2970 +       /* synchronize_rcu(); */
2971 +       au_kfree_rcu(br);
2972 +}
2973 +
2974 +/*
2975 + * frees all branches
2976 + */
2977 +void au_br_free(struct au_sbinfo *sbinfo)
2978 +{
2979 +       aufs_bindex_t bmax;
2980 +       struct au_branch **br;
2981 +
2982 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2983 +
2984 +       bmax = sbinfo->si_bbot + 1;
2985 +       br = sbinfo->si_branch;
2986 +       while (bmax--)
2987 +               au_br_do_free(*br++);
2988 +}
2989 +
2990 +/*
2991 + * find the index of a branch which is specified by @br_id.
2992 + */
2993 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2994 +{
2995 +       aufs_bindex_t bindex, bbot;
2996 +
2997 +       bbot = au_sbbot(sb);
2998 +       for (bindex = 0; bindex <= bbot; bindex++)
2999 +               if (au_sbr_id(sb, bindex) == br_id)
3000 +                       return bindex;
3001 +       return -1;
3002 +}
3003 +
3004 +/* ---------------------------------------------------------------------- */
3005 +
3006 +/*
3007 + * add a branch
3008 + */
3009 +
3010 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3011 +                       struct dentry *h_root)
3012 +{
3013 +       if (unlikely(h_adding == h_root
3014 +                    || au_test_loopback_overlap(sb, h_adding)))
3015 +               return 1;
3016 +       if (h_adding->d_sb != h_root->d_sb)
3017 +               return 0;
3018 +       return au_test_subdir(h_adding, h_root)
3019 +               || au_test_subdir(h_root, h_adding);
3020 +}
3021 +
3022 +/*
3023 + * returns a newly allocated branch. @new_nbranch is a number of branches
3024 + * after adding a branch.
3025 + */
3026 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3027 +                                    int perm)
3028 +{
3029 +       struct au_branch *add_branch;
3030 +       struct dentry *root;
3031 +       struct inode *inode;
3032 +       int err;
3033 +
3034 +       err = -ENOMEM;
3035 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3036 +       if (unlikely(!add_branch))
3037 +               goto out;
3038 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3039 +       if (unlikely(!add_branch->br_xino))
3040 +               goto out_br;
3041 +       err = au_hnotify_init_br(add_branch, perm);
3042 +       if (unlikely(err))
3043 +               goto out_xino;
3044 +
3045 +       if (au_br_writable(perm)) {
3046 +               /* may be freed separately at changing the branch permission */
3047 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3048 +                                            GFP_NOFS);
3049 +               if (unlikely(!add_branch->br_wbr))
3050 +                       goto out_hnotify;
3051 +       }
3052 +
3053 +       if (au_br_fhsm(perm)) {
3054 +               err = au_fhsm_br_alloc(add_branch);
3055 +               if (unlikely(err))
3056 +                       goto out_wbr;
3057 +       }
3058 +
3059 +       root = sb->s_root;
3060 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3061 +       if (!err)
3062 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3063 +       if (!err) {
3064 +               inode = d_inode(root);
3065 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3066 +                                       /*may_shrink*/0);
3067 +       }
3068 +       if (!err)
3069 +               return add_branch; /* success */
3070 +
3071 +out_wbr:
3072 +       au_kfree_rcu(add_branch->br_wbr);
3073 +out_hnotify:
3074 +       au_hnotify_fin_br(add_branch);
3075 +out_xino:
3076 +       au_xino_put(add_branch);
3077 +out_br:
3078 +       au_kfree_rcu(add_branch);
3079 +out:
3080 +       return ERR_PTR(err);
3081 +}
3082 +
3083 +/*
3084 + * test if the branch permission is legal or not.
3085 + */
3086 +static int test_br(struct inode *inode, int brperm, char *path)
3087 +{
3088 +       int err;
3089 +
3090 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3091 +       if (!err)
3092 +               goto out;
3093 +
3094 +       err = -EINVAL;
3095 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3096 +
3097 +out:
3098 +       return err;
3099 +}
3100 +
3101 +/*
3102 + * returns:
3103 + * 0: success, the caller will add it
3104 + * plus: success, it is already unified, the caller should ignore it
3105 + * minus: error
3106 + */
3107 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3108 +{
3109 +       int err;
3110 +       aufs_bindex_t bbot, bindex;
3111 +       struct dentry *root, *h_dentry;
3112 +       struct inode *inode, *h_inode;
3113 +
3114 +       root = sb->s_root;
3115 +       bbot = au_sbbot(sb);
3116 +       if (unlikely(bbot >= 0
3117 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3118 +               err = 1;
3119 +               if (!remount) {
3120 +                       err = -EINVAL;
3121 +                       pr_err("%s duplicated\n", add->pathname);
3122 +               }
3123 +               goto out;
3124 +       }
3125 +
3126 +       err = -ENOSPC; /* -E2BIG; */
3127 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3128 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3129 +               pr_err("number of branches exceeded %s\n", add->pathname);
3130 +               goto out;
3131 +       }
3132 +
3133 +       err = -EDOM;
3134 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3135 +               pr_err("bad index %d\n", add->bindex);
3136 +               goto out;
3137 +       }
3138 +
3139 +       inode = d_inode(add->path.dentry);
3140 +       err = -ENOENT;
3141 +       if (unlikely(!inode->i_nlink)) {
3142 +               pr_err("no existence %s\n", add->pathname);
3143 +               goto out;
3144 +       }
3145 +
3146 +       err = -EINVAL;
3147 +       if (unlikely(inode->i_sb == sb)) {
3148 +               pr_err("%s must be outside\n", add->pathname);
3149 +               goto out;
3150 +       }
3151 +
3152 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3153 +               pr_err("unsupported filesystem, %s (%s)\n",
3154 +                      add->pathname, au_sbtype(inode->i_sb));
3155 +               goto out;
3156 +       }
3157 +
3158 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3159 +               pr_err("already stacked, %s (%s)\n",
3160 +                      add->pathname, au_sbtype(inode->i_sb));
3161 +               goto out;
3162 +       }
3163 +
3164 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3165 +       if (unlikely(err))
3166 +               goto out;
3167 +
3168 +       if (bbot < 0)
3169 +               return 0; /* success */
3170 +
3171 +       err = -EINVAL;
3172 +       for (bindex = 0; bindex <= bbot; bindex++)
3173 +               if (unlikely(test_overlap(sb, add->path.dentry,
3174 +                                         au_h_dptr(root, bindex)))) {
3175 +                       pr_err("%s is overlapped\n", add->pathname);
3176 +                       goto out;
3177 +               }
3178 +
3179 +       err = 0;
3180 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3181 +               h_dentry = au_h_dptr(root, 0);
3182 +               h_inode = d_inode(h_dentry);
3183 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3184 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3185 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3186 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3187 +                               add->pathname,
3188 +                               i_uid_read(inode), i_gid_read(inode),
3189 +                               (inode->i_mode & S_IALLUGO),
3190 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3191 +                               (h_inode->i_mode & S_IALLUGO));
3192 +       }
3193 +
3194 +out:
3195 +       return err;
3196 +}
3197 +
3198 +/*
3199 + * initialize or clean the whiteouts for an adding branch
3200 + */
3201 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3202 +                        int new_perm)
3203 +{
3204 +       int err, old_perm;
3205 +       aufs_bindex_t bindex;
3206 +       struct inode *h_inode;
3207 +       struct au_wbr *wbr;
3208 +       struct au_hinode *hdir;
3209 +       struct dentry *h_dentry;
3210 +
3211 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3212 +       if (unlikely(err))
3213 +               goto out;
3214 +
3215 +       wbr = br->br_wbr;
3216 +       old_perm = br->br_perm;
3217 +       br->br_perm = new_perm;
3218 +       hdir = NULL;
3219 +       h_inode = NULL;
3220 +       bindex = au_br_index(sb, br->br_id);
3221 +       if (0 <= bindex) {
3222 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3223 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3224 +       } else {
3225 +               h_dentry = au_br_dentry(br);
3226 +               h_inode = d_inode(h_dentry);
3227 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3228 +       }
3229 +       if (!wbr)
3230 +               err = au_wh_init(br, sb);
3231 +       else {
3232 +               wbr_wh_write_lock(wbr);
3233 +               err = au_wh_init(br, sb);
3234 +               wbr_wh_write_unlock(wbr);
3235 +       }
3236 +       if (hdir)
3237 +               au_hn_inode_unlock(hdir);
3238 +       else
3239 +               inode_unlock(h_inode);
3240 +       vfsub_mnt_drop_write(au_br_mnt(br));
3241 +       br->br_perm = old_perm;
3242 +
3243 +       if (!err && wbr && !au_br_writable(new_perm)) {
3244 +               au_kfree_rcu(wbr);
3245 +               br->br_wbr = NULL;
3246 +       }
3247 +
3248 +out:
3249 +       return err;
3250 +}
3251 +
3252 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3253 +                      int perm)
3254 +{
3255 +       int err;
3256 +       struct kstatfs kst;
3257 +       struct au_wbr *wbr;
3258 +
3259 +       wbr = br->br_wbr;
3260 +       au_rw_init(&wbr->wbr_wh_rwsem);
3261 +       atomic_set(&wbr->wbr_wh_running, 0);
3262 +
3263 +       /*
3264 +        * a limit for rmdir/rename a dir
3265 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3266 +        */
3267 +       err = vfs_statfs(&br->br_path, &kst);
3268 +       if (unlikely(err))
3269 +               goto out;
3270 +       err = -EINVAL;
3271 +       if (kst.f_namelen >= NAME_MAX)
3272 +               err = au_br_init_wh(sb, br, perm);
3273 +       else
3274 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3275 +                      au_br_dentry(br),
3276 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3277 +
3278 +out:
3279 +       return err;
3280 +}
3281 +
3282 +/* initialize a new branch */
3283 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3284 +                     struct au_opt_add *add)
3285 +{
3286 +       int err;
3287 +       struct au_branch *brbase;
3288 +       struct file *xf;
3289 +       struct inode *h_inode;
3290 +
3291 +       err = 0;
3292 +       br->br_perm = add->perm;
3293 +       br->br_path = add->path; /* set first, path_get() later */
3294 +       spin_lock_init(&br->br_dykey_lock);
3295 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3296 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3297 +       br->br_id = au_new_br_id(sb);
3298 +       AuDebugOn(br->br_id < 0);
3299 +
3300 +       /* always, regardless the given option */
3301 +       err = au_dr_br_init(sb, br, &add->path);
3302 +       if (unlikely(err))
3303 +               goto out_err;
3304 +
3305 +       if (au_br_writable(add->perm)) {
3306 +               err = au_wbr_init(br, sb, add->perm);
3307 +               if (unlikely(err))
3308 +                       goto out_err;
3309 +       }
3310 +
3311 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3312 +               brbase = au_sbr(sb, 0);
3313 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3314 +               AuDebugOn(!xf);
3315 +               h_inode = d_inode(add->path.dentry);
3316 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3317 +               if (unlikely(err)) {
3318 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3319 +                       goto out_err;
3320 +               }
3321 +       }
3322 +
3323 +       sysaufs_br_init(br);
3324 +       path_get(&br->br_path);
3325 +       goto out; /* success */
3326 +
3327 +out_err:
3328 +       memset(&br->br_path, 0, sizeof(br->br_path));
3329 +out:
3330 +       return err;
3331 +}
3332 +
3333 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3334 +                            struct au_branch *br, aufs_bindex_t bbot,
3335 +                            aufs_bindex_t amount)
3336 +{
3337 +       struct au_branch **brp;
3338 +
3339 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3340 +
3341 +       brp = sbinfo->si_branch + bindex;
3342 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3343 +       *brp = br;
3344 +       sbinfo->si_bbot++;
3345 +       if (unlikely(bbot < 0))
3346 +               sbinfo->si_bbot = 0;
3347 +}
3348 +
3349 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3350 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3351 +{
3352 +       struct au_hdentry *hdp;
3353 +
3354 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3355 +
3356 +       hdp = au_hdentry(dinfo, bindex);
3357 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3358 +       au_h_dentry_init(hdp);
3359 +       dinfo->di_bbot++;
3360 +       if (unlikely(bbot < 0))
3361 +               dinfo->di_btop = 0;
3362 +}
3363 +
3364 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3365 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3366 +{
3367 +       struct au_hinode *hip;
3368 +
3369 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3370 +
3371 +       hip = au_hinode(iinfo, bindex);
3372 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3373 +       au_hinode_init(hip);
3374 +       iinfo->ii_bbot++;
3375 +       if (unlikely(bbot < 0))
3376 +               iinfo->ii_btop = 0;
3377 +}
3378 +
3379 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3380 +                        aufs_bindex_t bindex)
3381 +{
3382 +       struct dentry *root, *h_dentry;
3383 +       struct inode *root_inode, *h_inode;
3384 +       aufs_bindex_t bbot, amount;
3385 +
3386 +       root = sb->s_root;
3387 +       root_inode = d_inode(root);
3388 +       bbot = au_sbbot(sb);
3389 +       amount = bbot + 1 - bindex;
3390 +       h_dentry = au_br_dentry(br);
3391 +       au_sbilist_lock();
3392 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3393 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3394 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3395 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3396 +       h_inode = d_inode(h_dentry);
3397 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3398 +       au_sbilist_unlock();
3399 +}
3400 +
3401 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3402 +{
3403 +       int err;
3404 +       aufs_bindex_t bbot, add_bindex;
3405 +       struct dentry *root, *h_dentry;
3406 +       struct inode *root_inode;
3407 +       struct au_branch *add_branch;
3408 +
3409 +       root = sb->s_root;
3410 +       root_inode = d_inode(root);
3411 +       IMustLock(root_inode);
3412 +       IiMustWriteLock(root_inode);
3413 +       err = test_add(sb, add, remount);
3414 +       if (unlikely(err < 0))
3415 +               goto out;
3416 +       if (err) {
3417 +               err = 0;
3418 +               goto out; /* success */
3419 +       }
3420 +
3421 +       bbot = au_sbbot(sb);
3422 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3423 +       err = PTR_ERR(add_branch);
3424 +       if (IS_ERR(add_branch))
3425 +               goto out;
3426 +
3427 +       err = au_br_init(add_branch, sb, add);
3428 +       if (unlikely(err)) {
3429 +               au_br_do_free(add_branch);
3430 +               goto out;
3431 +       }
3432 +
3433 +       add_bindex = add->bindex;
3434 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3435 +       au_br_do_add(sb, add_branch, add_bindex);
3436 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3437 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3438 +
3439 +       h_dentry = add->path.dentry;
3440 +       if (!add_bindex) {
3441 +               au_cpup_attr_all(root_inode, /*force*/1);
3442 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3443 +       } else
3444 +               au_add_nlink(root_inode, d_inode(h_dentry));
3445 +
3446 +out:
3447 +       return err;
3448 +}
3449 +
3450 +/* ---------------------------------------------------------------------- */
3451 +
3452 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3453 +                                      unsigned long long max __maybe_unused,
3454 +                                      void *arg)
3455 +{
3456 +       unsigned long long n;
3457 +       struct file **p, *f;
3458 +       struct hlist_bl_head *files;
3459 +       struct hlist_bl_node *pos;
3460 +       struct au_finfo *finfo;
3461 +
3462 +       n = 0;
3463 +       p = a;
3464 +       files = &au_sbi(sb)->si_files;
3465 +       hlist_bl_lock(files);
3466 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3467 +               f = finfo->fi_file;
3468 +               if (file_count(f)
3469 +                   && !special_file(file_inode(f)->i_mode)) {
3470 +                       get_file(f);
3471 +                       *p++ = f;
3472 +                       n++;
3473 +                       AuDebugOn(n > max);
3474 +               }
3475 +       }
3476 +       hlist_bl_unlock(files);
3477 +
3478 +       return n;
3479 +}
3480 +
3481 +static struct file **au_farray_alloc(struct super_block *sb,
3482 +                                    unsigned long long *max)
3483 +{
3484 +       struct au_sbinfo *sbi;
3485 +
3486 +       sbi = au_sbi(sb);
3487 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3488 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3489 +}
3490 +
3491 +static void au_farray_free(struct file **a, unsigned long long max)
3492 +{
3493 +       unsigned long long ull;
3494 +
3495 +       for (ull = 0; ull < max; ull++)
3496 +               if (a[ull])
3497 +                       fput(a[ull]);
3498 +       kvfree(a);
3499 +}
3500 +
3501 +/* ---------------------------------------------------------------------- */
3502 +
3503 +/*
3504 + * delete a branch
3505 + */
3506 +
3507 +/* to show the line number, do not make it inlined function */
3508 +#define AuVerbose(do_info, fmt, ...) do { \
3509 +       if (do_info) \
3510 +               pr_info(fmt, ##__VA_ARGS__); \
3511 +} while (0)
3512 +
3513 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3514 +                        aufs_bindex_t bbot)
3515 +{
3516 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3517 +}
3518 +
3519 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3520 +                        aufs_bindex_t bbot)
3521 +{
3522 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3523 +}
3524 +
3525 +/*
3526 + * test if the branch is deletable or not.
3527 + */
3528 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3529 +                           unsigned int sigen, const unsigned int verbose)
3530 +{
3531 +       int err, i, j, ndentry;
3532 +       aufs_bindex_t btop, bbot;
3533 +       struct au_dcsub_pages dpages;
3534 +       struct au_dpage *dpage;
3535 +       struct dentry *d;
3536 +
3537 +       err = au_dpages_init(&dpages, GFP_NOFS);
3538 +       if (unlikely(err))
3539 +               goto out;
3540 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3541 +       if (unlikely(err))
3542 +               goto out_dpages;
3543 +
3544 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3545 +               dpage = dpages.dpages + i;
3546 +               ndentry = dpage->ndentry;
3547 +               for (j = 0; !err && j < ndentry; j++) {
3548 +                       d = dpage->dentries[j];
3549 +                       AuDebugOn(au_dcount(d) <= 0);
3550 +                       if (!au_digen_test(d, sigen)) {
3551 +                               di_read_lock_child(d, AuLock_IR);
3552 +                               if (unlikely(au_dbrange_test(d))) {
3553 +                                       di_read_unlock(d, AuLock_IR);
3554 +                                       continue;
3555 +                               }
3556 +                       } else {
3557 +                               di_write_lock_child(d);
3558 +                               if (unlikely(au_dbrange_test(d))) {
3559 +                                       di_write_unlock(d);
3560 +                                       continue;
3561 +                               }
3562 +                               err = au_reval_dpath(d, sigen);
3563 +                               if (!err)
3564 +                                       di_downgrade_lock(d, AuLock_IR);
3565 +                               else {
3566 +                                       di_write_unlock(d);
3567 +                                       break;
3568 +                               }
3569 +                       }
3570 +
3571 +                       /* AuDbgDentry(d); */
3572 +                       btop = au_dbtop(d);
3573 +                       bbot = au_dbbot(d);
3574 +                       if (btop <= bindex
3575 +                           && bindex <= bbot
3576 +                           && au_h_dptr(d, bindex)
3577 +                           && au_test_dbusy(d, btop, bbot)) {
3578 +                               err = -EBUSY;
3579 +                               AuVerbose(verbose, "busy %pd\n", d);
3580 +                               AuDbgDentry(d);
3581 +                       }
3582 +                       di_read_unlock(d, AuLock_IR);
3583 +               }
3584 +       }
3585 +
3586 +out_dpages:
3587 +       au_dpages_free(&dpages);
3588 +out:
3589 +       return err;
3590 +}
3591 +
3592 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3593 +                          unsigned int sigen, const unsigned int verbose)
3594 +{
3595 +       int err;
3596 +       unsigned long long max, ull;
3597 +       struct inode *i, **array;
3598 +       aufs_bindex_t btop, bbot;
3599 +
3600 +       array = au_iarray_alloc(sb, &max);
3601 +       err = PTR_ERR(array);
3602 +       if (IS_ERR(array))
3603 +               goto out;
3604 +
3605 +       err = 0;
3606 +       AuDbg("b%d\n", bindex);
3607 +       for (ull = 0; !err && ull < max; ull++) {
3608 +               i = array[ull];
3609 +               if (unlikely(!i))
3610 +                       break;
3611 +               if (i->i_ino == AUFS_ROOT_INO)
3612 +                       continue;
3613 +
3614 +               /* AuDbgInode(i); */
3615 +               if (au_iigen(i, NULL) == sigen)
3616 +                       ii_read_lock_child(i);
3617 +               else {
3618 +                       ii_write_lock_child(i);
3619 +                       err = au_refresh_hinode_self(i);
3620 +                       au_iigen_dec(i);
3621 +                       if (!err)
3622 +                               ii_downgrade_lock(i);
3623 +                       else {
3624 +                               ii_write_unlock(i);
3625 +                               break;
3626 +                       }
3627 +               }
3628 +
3629 +               btop = au_ibtop(i);
3630 +               bbot = au_ibbot(i);
3631 +               if (btop <= bindex
3632 +                   && bindex <= bbot
3633 +                   && au_h_iptr(i, bindex)
3634 +                   && au_test_ibusy(i, btop, bbot)) {
3635 +                       err = -EBUSY;
3636 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3637 +                       AuDbgInode(i);
3638 +               }
3639 +               ii_read_unlock(i);
3640 +       }
3641 +       au_iarray_free(array, max);
3642 +
3643 +out:
3644 +       return err;
3645 +}
3646 +
3647 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3648 +                             const unsigned int verbose)
3649 +{
3650 +       int err;
3651 +       unsigned int sigen;
3652 +
3653 +       sigen = au_sigen(root->d_sb);
3654 +       DiMustNoWaiters(root);
3655 +       IiMustNoWaiters(d_inode(root));
3656 +       di_write_unlock(root);
3657 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3658 +       if (!err)
3659 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3660 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3661 +
3662 +       return err;
3663 +}
3664 +
3665 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3666 +                        struct file **to_free, int *idx)
3667 +{
3668 +       int err;
3669 +       unsigned char matched, root;
3670 +       aufs_bindex_t bindex, bbot;
3671 +       struct au_fidir *fidir;
3672 +       struct au_hfile *hfile;
3673 +
3674 +       err = 0;
3675 +       root = IS_ROOT(file->f_path.dentry);
3676 +       if (root) {
3677 +               get_file(file);
3678 +               to_free[*idx] = file;
3679 +               (*idx)++;
3680 +               goto out;
3681 +       }
3682 +
3683 +       matched = 0;
3684 +       fidir = au_fi(file)->fi_hdir;
3685 +       AuDebugOn(!fidir);
3686 +       bbot = au_fbbot_dir(file);
3687 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3688 +               hfile = fidir->fd_hfile + bindex;
3689 +               if (!hfile->hf_file)
3690 +                       continue;
3691 +
3692 +               if (hfile->hf_br->br_id == br_id) {
3693 +                       matched = 1;
3694 +                       break;
3695 +               }
3696 +       }
3697 +       if (matched)
3698 +               err = -EBUSY;
3699 +
3700 +out:
3701 +       return err;
3702 +}
3703 +
3704 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3705 +                         struct file **to_free, int opened)
3706 +{
3707 +       int err, idx;
3708 +       unsigned long long ull, max;
3709 +       aufs_bindex_t btop;
3710 +       struct file *file, **array;
3711 +       struct dentry *root;
3712 +       struct au_hfile *hfile;
3713 +
3714 +       array = au_farray_alloc(sb, &max);
3715 +       err = PTR_ERR(array);
3716 +       if (IS_ERR(array))
3717 +               goto out;
3718 +
3719 +       err = 0;
3720 +       idx = 0;
3721 +       root = sb->s_root;
3722 +       di_write_unlock(root);
3723 +       for (ull = 0; ull < max; ull++) {
3724 +               file = array[ull];
3725 +               if (unlikely(!file))
3726 +                       break;
3727 +
3728 +               /* AuDbg("%pD\n", file); */
3729 +               fi_read_lock(file);
3730 +               btop = au_fbtop(file);
3731 +               if (!d_is_dir(file->f_path.dentry)) {
3732 +                       hfile = &au_fi(file)->fi_htop;
3733 +                       if (hfile->hf_br->br_id == br_id)
3734 +                               err = -EBUSY;
3735 +               } else
3736 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3737 +               fi_read_unlock(file);
3738 +               if (unlikely(err))
3739 +                       break;
3740 +       }
3741 +       di_write_lock_child(root);
3742 +       au_farray_free(array, max);
3743 +       AuDebugOn(idx > opened);
3744 +
3745 +out:
3746 +       return err;
3747 +}
3748 +
3749 +static void br_del_file(struct file **to_free, unsigned long long opened,
3750 +                       aufs_bindex_t br_id)
3751 +{
3752 +       unsigned long long ull;
3753 +       aufs_bindex_t bindex, btop, bbot, bfound;
3754 +       struct file *file;
3755 +       struct au_fidir *fidir;
3756 +       struct au_hfile *hfile;
3757 +
3758 +       for (ull = 0; ull < opened; ull++) {
3759 +               file = to_free[ull];
3760 +               if (unlikely(!file))
3761 +                       break;
3762 +
3763 +               /* AuDbg("%pD\n", file); */
3764 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3765 +               bfound = -1;
3766 +               fidir = au_fi(file)->fi_hdir;
3767 +               AuDebugOn(!fidir);
3768 +               fi_write_lock(file);
3769 +               btop = au_fbtop(file);
3770 +               bbot = au_fbbot_dir(file);
3771 +               for (bindex = btop; bindex <= bbot; bindex++) {
3772 +                       hfile = fidir->fd_hfile + bindex;
3773 +                       if (!hfile->hf_file)
3774 +                               continue;
3775 +
3776 +                       if (hfile->hf_br->br_id == br_id) {
3777 +                               bfound = bindex;
3778 +                               break;
3779 +                       }
3780 +               }
3781 +               AuDebugOn(bfound < 0);
3782 +               au_set_h_fptr(file, bfound, NULL);
3783 +               if (bfound == btop) {
3784 +                       for (btop++; btop <= bbot; btop++)
3785 +                               if (au_hf_dir(file, btop)) {
3786 +                                       au_set_fbtop(file, btop);
3787 +                                       break;
3788 +                               }
3789 +               }
3790 +               fi_write_unlock(file);
3791 +       }
3792 +}
3793 +
3794 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3795 +                            const aufs_bindex_t bindex,
3796 +                            const aufs_bindex_t bbot)
3797 +{
3798 +       struct au_branch **brp, **p;
3799 +
3800 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3801 +
3802 +       brp = sbinfo->si_branch + bindex;
3803 +       if (bindex < bbot)
3804 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3805 +       sbinfo->si_branch[0 + bbot] = NULL;
3806 +       sbinfo->si_bbot--;
3807 +
3808 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3809 +                       /*may_shrink*/1);
3810 +       if (p)
3811 +               sbinfo->si_branch = p;
3812 +       /* harmless error */
3813 +}
3814 +
3815 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3816 +                            const aufs_bindex_t bbot)
3817 +{
3818 +       struct au_hdentry *hdp, *p;
3819 +
3820 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3821 +
3822 +       hdp = au_hdentry(dinfo, bindex);
3823 +       if (bindex < bbot)
3824 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3825 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3826 +       dinfo->di_bbot--;
3827 +
3828 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3829 +                       /*may_shrink*/1);
3830 +       if (p)
3831 +               dinfo->di_hdentry = p;
3832 +       /* harmless error */
3833 +}
3834 +
3835 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3836 +                            const aufs_bindex_t bbot)
3837 +{
3838 +       struct au_hinode *hip, *p;
3839 +
3840 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3841 +
3842 +       hip = au_hinode(iinfo, bindex);
3843 +       if (bindex < bbot)
3844 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3845 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3846 +       iinfo->ii_bbot--;
3847 +
3848 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3849 +                       /*may_shrink*/1);
3850 +       if (p)
3851 +               iinfo->ii_hinode = p;
3852 +       /* harmless error */
3853 +}
3854 +
3855 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3856 +                        struct au_branch *br)
3857 +{
3858 +       aufs_bindex_t bbot;
3859 +       struct au_sbinfo *sbinfo;
3860 +       struct dentry *root, *h_root;
3861 +       struct inode *inode, *h_inode;
3862 +       struct au_hinode *hinode;
3863 +
3864 +       SiMustWriteLock(sb);
3865 +
3866 +       root = sb->s_root;
3867 +       inode = d_inode(root);
3868 +       sbinfo = au_sbi(sb);
3869 +       bbot = sbinfo->si_bbot;
3870 +
3871 +       h_root = au_h_dptr(root, bindex);
3872 +       hinode = au_hi(inode, bindex);
3873 +       h_inode = au_igrab(hinode->hi_inode);
3874 +       au_hiput(hinode);
3875 +
3876 +       au_sbilist_lock();
3877 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3878 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3879 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3880 +       au_sbilist_unlock();
3881 +
3882 +       /* ignore an error */
3883 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3884 +
3885 +       dput(h_root);
3886 +       iput(h_inode);
3887 +       au_br_do_free(br);
3888 +}
3889 +
3890 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3891 +                                  unsigned long long max, void *arg)
3892 +{
3893 +       return max;
3894 +}
3895 +
3896 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3897 +{
3898 +       int err, rerr, i;
3899 +       unsigned long long opened;
3900 +       unsigned int mnt_flags;
3901 +       aufs_bindex_t bindex, bbot, br_id;
3902 +       unsigned char do_wh, verbose;
3903 +       struct au_branch *br;
3904 +       struct au_wbr *wbr;
3905 +       struct dentry *root;
3906 +       struct file **to_free;
3907 +
3908 +       err = 0;
3909 +       opened = 0;
3910 +       to_free = NULL;
3911 +       root = sb->s_root;
3912 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3913 +       if (bindex < 0) {
3914 +               if (remount)
3915 +                       goto out; /* success */
3916 +               err = -ENOENT;
3917 +               pr_err("%s no such branch\n", del->pathname);
3918 +               goto out;
3919 +       }
3920 +       AuDbg("bindex b%d\n", bindex);
3921 +
3922 +       err = -EBUSY;
3923 +       mnt_flags = au_mntflags(sb);
3924 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3925 +       bbot = au_sbbot(sb);
3926 +       if (unlikely(!bbot)) {
3927 +               AuVerbose(verbose, "no more branches left\n");
3928 +               goto out;
3929 +       }
3930 +
3931 +       br = au_sbr(sb, bindex);
3932 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3933 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3934 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3935 +               goto out;
3936 +       }
3937 +
3938 +       br_id = br->br_id;
3939 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3940 +       if (unlikely(opened)) {
3941 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3942 +               err = PTR_ERR(to_free);
3943 +               if (IS_ERR(to_free))
3944 +                       goto out;
3945 +
3946 +               err = test_file_busy(sb, br_id, to_free, opened);
3947 +               if (unlikely(err)) {
3948 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3949 +                       goto out;
3950 +               }
3951 +       }
3952 +
3953 +       wbr = br->br_wbr;
3954 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3955 +       if (do_wh) {
3956 +               /* instead of WbrWhMustWriteLock(wbr) */
3957 +               SiMustWriteLock(sb);
3958 +               for (i = 0; i < AuBrWh_Last; i++) {
3959 +                       dput(wbr->wbr_wh[i]);
3960 +                       wbr->wbr_wh[i] = NULL;
3961 +               }
3962 +       }
3963 +
3964 +       err = test_children_busy(root, bindex, verbose);
3965 +       if (unlikely(err)) {
3966 +               if (do_wh)
3967 +                       goto out_wh;
3968 +               goto out;
3969 +       }
3970 +
3971 +       err = 0;
3972 +       if (to_free) {
3973 +               /*
3974 +                * now we confirmed the branch is deletable.
3975 +                * let's free the remaining opened dirs on the branch.
3976 +                */
3977 +               di_write_unlock(root);
3978 +               br_del_file(to_free, opened, br_id);
3979 +               di_write_lock_child(root);
3980 +       }
3981 +
3982 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3983 +       dbgaufs_xino_del(br);           /* remove one */
3984 +       au_br_do_del(sb, bindex, br);
3985 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3986 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
3987 +
3988 +       if (!bindex) {
3989 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3990 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3991 +       } else
3992 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3993 +       if (au_opt_test(mnt_flags, PLINK))
3994 +               au_plink_half_refresh(sb, br_id);
3995 +
3996 +       goto out; /* success */
3997 +
3998 +out_wh:
3999 +       /* revert */
4000 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4001 +       if (rerr)
4002 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4003 +                       del->pathname, rerr);
4004 +out:
4005 +       if (to_free)
4006 +               au_farray_free(to_free, opened);
4007 +       return err;
4008 +}
4009 +
4010 +/* ---------------------------------------------------------------------- */
4011 +
4012 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4013 +{
4014 +       int err;
4015 +       aufs_bindex_t btop, bbot;
4016 +       struct aufs_ibusy ibusy;
4017 +       struct inode *inode, *h_inode;
4018 +
4019 +       err = -EPERM;
4020 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4021 +               goto out;
4022 +
4023 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4024 +       if (!err)
4025 +               /* VERIFY_WRITE */
4026 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4027 +       if (unlikely(err)) {
4028 +               err = -EFAULT;
4029 +               AuTraceErr(err);
4030 +               goto out;
4031 +       }
4032 +
4033 +       err = -EINVAL;
4034 +       si_read_lock(sb, AuLock_FLUSH);
4035 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4036 +               goto out_unlock;
4037 +
4038 +       err = 0;
4039 +       ibusy.h_ino = 0; /* invalid */
4040 +       inode = ilookup(sb, ibusy.ino);
4041 +       if (!inode
4042 +           || inode->i_ino == AUFS_ROOT_INO
4043 +           || au_is_bad_inode(inode))
4044 +               goto out_unlock;
4045 +
4046 +       ii_read_lock_child(inode);
4047 +       btop = au_ibtop(inode);
4048 +       bbot = au_ibbot(inode);
4049 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4050 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4051 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4052 +                       ibusy.h_ino = h_inode->i_ino;
4053 +       }
4054 +       ii_read_unlock(inode);
4055 +       iput(inode);
4056 +
4057 +out_unlock:
4058 +       si_read_unlock(sb);
4059 +       if (!err) {
4060 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4061 +               if (unlikely(err)) {
4062 +                       err = -EFAULT;
4063 +                       AuTraceErr(err);
4064 +               }
4065 +       }
4066 +out:
4067 +       return err;
4068 +}
4069 +
4070 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4071 +{
4072 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4073 +}
4074 +
4075 +#ifdef CONFIG_COMPAT
4076 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4077 +{
4078 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4079 +}
4080 +#endif
4081 +
4082 +/* ---------------------------------------------------------------------- */
4083 +
4084 +/*
4085 + * change a branch permission
4086 + */
4087 +
4088 +static void au_warn_ima(void)
4089 +{
4090 +#ifdef CONFIG_IMA
4091 +       /* since it doesn't support mark_files_ro() */
4092 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4093 +#endif
4094 +}
4095 +
4096 +static int do_need_sigen_inc(int a, int b)
4097 +{
4098 +       return au_br_whable(a) && !au_br_whable(b);
4099 +}
4100 +
4101 +static int need_sigen_inc(int old, int new)
4102 +{
4103 +       return do_need_sigen_inc(old, new)
4104 +               || do_need_sigen_inc(new, old);
4105 +}
4106 +
4107 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4108 +{
4109 +       int err, do_warn;
4110 +       unsigned int mnt_flags;
4111 +       unsigned long long ull, max;
4112 +       aufs_bindex_t br_id;
4113 +       unsigned char verbose, writer;
4114 +       struct file *file, *hf, **array;
4115 +       struct au_hfile *hfile;
4116 +       struct inode *h_inode;
4117 +
4118 +       mnt_flags = au_mntflags(sb);
4119 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4120 +
4121 +       array = au_farray_alloc(sb, &max);
4122 +       err = PTR_ERR(array);
4123 +       if (IS_ERR(array))
4124 +               goto out;
4125 +
4126 +       do_warn = 0;
4127 +       br_id = au_sbr_id(sb, bindex);
4128 +       for (ull = 0; ull < max; ull++) {
4129 +               file = array[ull];
4130 +               if (unlikely(!file))
4131 +                       break;
4132 +
4133 +               /* AuDbg("%pD\n", file); */
4134 +               fi_read_lock(file);
4135 +               if (unlikely(au_test_mmapped(file))) {
4136 +                       err = -EBUSY;
4137 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4138 +                       AuDbgFile(file);
4139 +                       FiMustNoWaiters(file);
4140 +                       fi_read_unlock(file);
4141 +                       goto out_array;
4142 +               }
4143 +
4144 +               hfile = &au_fi(file)->fi_htop;
4145 +               hf = hfile->hf_file;
4146 +               if (!d_is_reg(file->f_path.dentry)
4147 +                   || !(file->f_mode & FMODE_WRITE)
4148 +                   || hfile->hf_br->br_id != br_id
4149 +                   || !(hf->f_mode & FMODE_WRITE))
4150 +                       array[ull] = NULL;
4151 +               else {
4152 +                       do_warn = 1;
4153 +                       get_file(file);
4154 +               }
4155 +
4156 +               FiMustNoWaiters(file);
4157 +               fi_read_unlock(file);
4158 +               fput(file);
4159 +       }
4160 +
4161 +       err = 0;
4162 +       if (do_warn)
4163 +               au_warn_ima();
4164 +
4165 +       for (ull = 0; ull < max; ull++) {
4166 +               file = array[ull];
4167 +               if (!file)
4168 +                       continue;
4169 +
4170 +               /* todo: already flushed? */
4171 +               /*
4172 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4173 +                * approach which resets f_mode and calls mnt_drop_write() and
4174 +                * file_release_write() for each file, because the branch
4175 +                * attribute in aufs world is totally different from the native
4176 +                * fs rw/ro mode.
4177 +                */
4178 +               /* fi_read_lock(file); */
4179 +               hfile = &au_fi(file)->fi_htop;
4180 +               hf = hfile->hf_file;
4181 +               /* fi_read_unlock(file); */
4182 +               spin_lock(&hf->f_lock);
4183 +               writer = !!(hf->f_mode & FMODE_WRITER);
4184 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4185 +               spin_unlock(&hf->f_lock);
4186 +               if (writer) {
4187 +                       h_inode = file_inode(hf);
4188 +                       if (hf->f_mode & FMODE_READ)
4189 +                               i_readcount_inc(h_inode);
4190 +                       put_write_access(h_inode);
4191 +                       __mnt_drop_write(hf->f_path.mnt);
4192 +               }
4193 +       }
4194 +
4195 +out_array:
4196 +       au_farray_free(array, max);
4197 +out:
4198 +       AuTraceErr(err);
4199 +       return err;
4200 +}
4201 +
4202 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4203 +             int *do_refresh)
4204 +{
4205 +       int err, rerr;
4206 +       aufs_bindex_t bindex;
4207 +       struct dentry *root;
4208 +       struct au_branch *br;
4209 +       struct au_br_fhsm *bf;
4210 +
4211 +       root = sb->s_root;
4212 +       bindex = au_find_dbindex(root, mod->h_root);
4213 +       if (bindex < 0) {
4214 +               if (remount)
4215 +                       return 0; /* success */
4216 +               err = -ENOENT;
4217 +               pr_err("%s no such branch\n", mod->path);
4218 +               goto out;
4219 +       }
4220 +       AuDbg("bindex b%d\n", bindex);
4221 +
4222 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4223 +       if (unlikely(err))
4224 +               goto out;
4225 +
4226 +       br = au_sbr(sb, bindex);
4227 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4228 +       if (br->br_perm == mod->perm)
4229 +               return 0; /* success */
4230 +
4231 +       /* pre-allocate for non-fhsm --> fhsm */
4232 +       bf = NULL;
4233 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4234 +               err = au_fhsm_br_alloc(br);
4235 +               if (unlikely(err))
4236 +                       goto out;
4237 +               bf = br->br_fhsm;
4238 +               br->br_fhsm = NULL;
4239 +       }
4240 +
4241 +       if (au_br_writable(br->br_perm)) {
4242 +               /* remove whiteout base */
4243 +               err = au_br_init_wh(sb, br, mod->perm);
4244 +               if (unlikely(err))
4245 +                       goto out_bf;
4246 +
4247 +               if (!au_br_writable(mod->perm)) {
4248 +                       /* rw --> ro, file might be mmapped */
4249 +                       DiMustNoWaiters(root);
4250 +                       IiMustNoWaiters(d_inode(root));
4251 +                       di_write_unlock(root);
4252 +                       err = au_br_mod_files_ro(sb, bindex);
4253 +                       /* aufs_write_lock() calls ..._child() */
4254 +                       di_write_lock_child(root);
4255 +
4256 +                       if (unlikely(err)) {
4257 +                               rerr = -ENOMEM;
4258 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4259 +                                                    GFP_NOFS);
4260 +                               if (br->br_wbr)
4261 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4262 +                               if (unlikely(rerr)) {
4263 +                                       AuIOErr("nested error %d (%d)\n",
4264 +                                               rerr, err);
4265 +                                       br->br_perm = mod->perm;
4266 +                               }
4267 +                       }
4268 +               }
4269 +       } else if (au_br_writable(mod->perm)) {
4270 +               /* ro --> rw */
4271 +               err = -ENOMEM;
4272 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4273 +               if (br->br_wbr) {
4274 +                       err = au_wbr_init(br, sb, mod->perm);
4275 +                       if (unlikely(err)) {
4276 +                               au_kfree_rcu(br->br_wbr);
4277 +                               br->br_wbr = NULL;
4278 +                       }
4279 +               }
4280 +       }
4281 +       if (unlikely(err))
4282 +               goto out_bf;
4283 +
4284 +       if (au_br_fhsm(br->br_perm)) {
4285 +               if (!au_br_fhsm(mod->perm)) {
4286 +                       /* fhsm --> non-fhsm */
4287 +                       au_br_fhsm_fin(br->br_fhsm);
4288 +                       au_kfree_rcu(br->br_fhsm);
4289 +                       br->br_fhsm = NULL;
4290 +               }
4291 +       } else if (au_br_fhsm(mod->perm))
4292 +               /* non-fhsm --> fhsm */
4293 +               br->br_fhsm = bf;
4294 +
4295 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4296 +       br->br_perm = mod->perm;
4297 +       goto out; /* success */
4298 +
4299 +out_bf:
4300 +       au_kfree_try_rcu(bf);
4301 +out:
4302 +       AuTraceErr(err);
4303 +       return err;
4304 +}
4305 +
4306 +/* ---------------------------------------------------------------------- */
4307 +
4308 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4309 +{
4310 +       int err;
4311 +       struct kstatfs kstfs;
4312 +
4313 +       err = vfs_statfs(&br->br_path, &kstfs);
4314 +       if (!err) {
4315 +               stfs->f_blocks = kstfs.f_blocks;
4316 +               stfs->f_bavail = kstfs.f_bavail;
4317 +               stfs->f_files = kstfs.f_files;
4318 +               stfs->f_ffree = kstfs.f_ffree;
4319 +       }
4320 +
4321 +       return err;
4322 +}
4323 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4324 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4325 +++ linux/fs/aufs/branch.h      2023-04-24 08:13:40.969576115 +0200
4326 @@ -0,0 +1,375 @@
4327 +/* SPDX-License-Identifier: GPL-2.0 */
4328 +/*
4329 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4330 + *
4331 + * This program is free software; you can redistribute it and/or modify
4332 + * it under the terms of the GNU General Public License as published by
4333 + * the Free Software Foundation; either version 2 of the License, or
4334 + * (at your option) any later version.
4335 + *
4336 + * This program is distributed in the hope that it will be useful,
4337 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4338 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4339 + * GNU General Public License for more details.
4340 + *
4341 + * You should have received a copy of the GNU General Public License
4342 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4343 + */
4344 +
4345 +/*
4346 + * branch filesystems and xino for them
4347 + */
4348 +
4349 +#ifndef __AUFS_BRANCH_H__
4350 +#define __AUFS_BRANCH_H__
4351 +
4352 +#ifdef __KERNEL__
4353 +
4354 +#include <linux/mount.h>
4355 +#include "dirren.h"
4356 +#include "dynop.h"
4357 +#include "lcnt.h"
4358 +#include "rwsem.h"
4359 +#include "super.h"
4360 +
4361 +/* ---------------------------------------------------------------------- */
4362 +
4363 +/* a xino file */
4364 +struct au_xino {
4365 +       struct file             **xi_file;
4366 +       unsigned int            xi_nfile;
4367 +
4368 +       struct {
4369 +               spinlock_t              spin;
4370 +               ino_t                   *array;
4371 +               int                     total;
4372 +               /* reserved for future use */
4373 +               /* unsigned long        *bitmap; */
4374 +               wait_queue_head_t       wqh;
4375 +       } xi_nondir;
4376 +
4377 +       struct mutex            xi_mtx; /* protects xi_file array */
4378 +       struct hlist_bl_head    xi_writing;
4379 +
4380 +       atomic_t                xi_truncating;
4381 +
4382 +       struct kref             xi_kref;
4383 +};
4384 +
4385 +/* File-based Hierarchical Storage Management */
4386 +struct au_br_fhsm {
4387 +#ifdef CONFIG_AUFS_FHSM
4388 +       struct mutex            bf_lock;
4389 +       unsigned long           bf_jiffy;
4390 +       struct aufs_stfs        bf_stfs;
4391 +       int                     bf_readable;
4392 +#endif
4393 +};
4394 +
4395 +/* members for writable branch only */
4396 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4397 +struct au_wbr {
4398 +       struct au_rwsem         wbr_wh_rwsem;
4399 +       struct dentry           *wbr_wh[AuBrWh_Last];
4400 +       atomic_t                wbr_wh_running;
4401 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4402 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4403 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4404 +
4405 +       /* mfs mode */
4406 +       unsigned long long      wbr_bytes;
4407 +};
4408 +
4409 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4410 +#define AuBrDynOp (AuDyLast * 4)
4411 +
4412 +#ifdef CONFIG_AUFS_HFSNOTIFY
4413 +/* support for asynchronous destruction */
4414 +struct au_br_hfsnotify {
4415 +       struct fsnotify_group   *hfsn_group;
4416 +};
4417 +#endif
4418 +
4419 +/* sysfs entries */
4420 +struct au_brsysfs {
4421 +       char                    name[16];
4422 +       struct attribute        attr;
4423 +};
4424 +
4425 +enum {
4426 +       AuBrSysfs_BR,
4427 +       AuBrSysfs_BRID,
4428 +       AuBrSysfs_Last
4429 +};
4430 +
4431 +/* protected by superblock rwsem */
4432 +struct au_branch {
4433 +       struct au_xino          *br_xino;
4434 +
4435 +       aufs_bindex_t           br_id;
4436 +
4437 +       int                     br_perm;
4438 +       struct path             br_path;
4439 +       spinlock_t              br_dykey_lock;
4440 +       struct au_dykey         *br_dykey[AuBrDynOp];
4441 +       au_lcnt_t               br_nfiles;      /* opened files */
4442 +       au_lcnt_t               br_count;       /* in-use for other */
4443 +
4444 +       struct au_wbr           *br_wbr;
4445 +       struct au_br_fhsm       *br_fhsm;
4446 +
4447 +#ifdef CONFIG_AUFS_HFSNOTIFY
4448 +       struct au_br_hfsnotify  *br_hfsn;
4449 +#endif
4450 +
4451 +#ifdef CONFIG_SYSFS
4452 +       /* entries under sysfs per mount-point */
4453 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4454 +#endif
4455 +
4456 +#ifdef CONFIG_DEBUG_FS
4457 +       struct dentry            *br_dbgaufs; /* xino */
4458 +#endif
4459 +
4460 +       struct au_dr_br         br_dirren;
4461 +};
4462 +
4463 +/* ---------------------------------------------------------------------- */
4464 +
4465 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4466 +{
4467 +       return br->br_path.mnt;
4468 +}
4469 +
4470 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4471 +{
4472 +       return br->br_path.dentry;
4473 +}
4474 +
4475 +static inline struct mnt_idmap *au_br_idmap(struct au_branch *br)
4476 +{
4477 +       return mnt_idmap(br->br_path.mnt);
4478 +}
4479 +
4480 +static inline struct super_block *au_br_sb(struct au_branch *br)
4481 +{
4482 +       return au_br_mnt(br)->mnt_sb;
4483 +}
4484 +
4485 +static inline int au_br_rdonly(struct au_branch *br)
4486 +{
4487 +       return (sb_rdonly(au_br_sb(br))
4488 +               || !au_br_writable(br->br_perm))
4489 +               ? -EROFS : 0;
4490 +}
4491 +
4492 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4493 +{
4494 +#ifdef CONFIG_AUFS_HNOTIFY
4495 +       return !(brperm & AuBrPerm_RR);
4496 +#else
4497 +       return 0;
4498 +#endif
4499 +}
4500 +
4501 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4502 +{
4503 +       int err, exec_flag;
4504 +
4505 +       err = 0;
4506 +       exec_flag = oflag & __FMODE_EXEC;
4507 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4508 +               err = -EACCES;
4509 +
4510 +       return err;
4511 +}
4512 +
4513 +static inline void au_xino_get(struct au_branch *br)
4514 +{
4515 +       struct au_xino *xi;
4516 +
4517 +       xi = br->br_xino;
4518 +       if (xi)
4519 +               kref_get(&xi->xi_kref);
4520 +}
4521 +
4522 +static inline int au_xino_count(struct au_branch *br)
4523 +{
4524 +       int v;
4525 +       struct au_xino *xi;
4526 +
4527 +       v = 0;
4528 +       xi = br->br_xino;
4529 +       if (xi)
4530 +               v = kref_read(&xi->xi_kref);
4531 +
4532 +       return v;
4533 +}
4534 +
4535 +/* ---------------------------------------------------------------------- */
4536 +
4537 +/* branch.c */
4538 +struct au_sbinfo;
4539 +void au_br_free(struct au_sbinfo *sinfo);
4540 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4541 +struct au_opt_add;
4542 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4543 +struct au_opt_del;
4544 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4545 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4546 +#ifdef CONFIG_COMPAT
4547 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4548 +#endif
4549 +struct au_opt_mod;
4550 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4551 +             int *do_refresh);
4552 +struct aufs_stfs;
4553 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4554 +
4555 +/* xino.c */
4556 +static const loff_t au_loff_max = LLONG_MAX;
4557 +
4558 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4559 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4560 +                           int wbrtop);
4561 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4562 +                            struct file *copy_src);
4563 +struct au_xi_new {
4564 +       struct au_xino *xi;     /* switch between xino and xigen */
4565 +       int idx;
4566 +       struct path *base;
4567 +       struct file *copy_src;
4568 +};
4569 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4570 +
4571 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4572 +                ino_t *ino);
4573 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4574 +                 ino_t ino);
4575 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4576 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4577 +
4578 +int au_xib_trunc(struct super_block *sb);
4579 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4580 +
4581 +struct au_xino *au_xino_alloc(unsigned int nfile);
4582 +int au_xino_put(struct au_branch *br);
4583 +struct file *au_xino_file1(struct au_xino *xi);
4584 +
4585 +struct au_opt_xino;
4586 +void au_xino_clr(struct super_block *sb);
4587 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4588 +struct file *au_xino_def(struct super_block *sb);
4589 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4590 +                   struct path *base);
4591 +
4592 +ino_t au_xino_new_ino(struct super_block *sb);
4593 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4594 +
4595 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4596 +                      ino_t h_ino, int idx);
4597 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4598 +                     int *idx);
4599 +
4600 +int au_xino_path(struct seq_file *seq, struct file *file);
4601 +
4602 +/* ---------------------------------------------------------------------- */
4603 +
4604 +/* @idx is signed to accept -1 meaning the first file */
4605 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4606 +{
4607 +       struct file *file;
4608 +
4609 +       file = NULL;
4610 +       if (!xi)
4611 +               goto out;
4612 +
4613 +       if (idx >= 0) {
4614 +               if (idx < xi->xi_nfile)
4615 +                       file = xi->xi_file[idx];
4616 +       } else
4617 +               file = au_xino_file1(xi);
4618 +
4619 +out:
4620 +       return file;
4621 +}
4622 +
4623 +/* ---------------------------------------------------------------------- */
4624 +
4625 +/* Superblock to branch */
4626 +static inline
4627 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4628 +{
4629 +       return au_sbr(sb, bindex)->br_id;
4630 +}
4631 +
4632 +static inline
4633 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4634 +{
4635 +       return au_br_mnt(au_sbr(sb, bindex));
4636 +}
4637 +
4638 +static inline
4639 +struct mnt_idmap *au_sbr_idmap(struct super_block *sb, aufs_bindex_t bindex)
4640 +{
4641 +       return au_br_idmap(au_sbr(sb, bindex));
4642 +}
4643 +
4644 +static inline
4645 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4646 +{
4647 +       return au_br_sb(au_sbr(sb, bindex));
4648 +}
4649 +
4650 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4651 +{
4652 +       return au_sbr(sb, bindex)->br_perm;
4653 +}
4654 +
4655 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4656 +{
4657 +       return au_br_whable(au_sbr_perm(sb, bindex));
4658 +}
4659 +
4660 +/* ---------------------------------------------------------------------- */
4661 +
4662 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4663 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4664 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4665 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4666 +/*
4667 +#define wbr_wh_read_trylock_nested(wbr) \
4668 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4669 +#define wbr_wh_write_trylock_nested(wbr) \
4670 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4671 +*/
4672 +
4673 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4674 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4675 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4676 +
4677 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4678 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4679 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4680 +
4681 +/* ---------------------------------------------------------------------- */
4682 +
4683 +#ifdef CONFIG_AUFS_FHSM
4684 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4685 +{
4686 +       mutex_init(&brfhsm->bf_lock);
4687 +       brfhsm->bf_jiffy = 0;
4688 +       brfhsm->bf_readable = 0;
4689 +}
4690 +
4691 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4692 +{
4693 +       mutex_destroy(&brfhsm->bf_lock);
4694 +}
4695 +#else
4696 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4697 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4698 +#endif
4699 +
4700 +#endif /* __KERNEL__ */
4701 +#endif /* __AUFS_BRANCH_H__ */
4702 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4703 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4704 +++ linux/fs/aufs/conf.mk       2022-11-05 23:02:18.959222617 +0100
4705 @@ -0,0 +1,40 @@
4706 +# SPDX-License-Identifier: GPL-2.0
4707 +
4708 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4709 +
4710 +define AuConf
4711 +ifdef ${1}
4712 +AuConfStr += ${1}=${${1}}
4713 +endif
4714 +endef
4715 +
4716 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4717 +       SBILIST \
4718 +       HNOTIFY HFSNOTIFY \
4719 +       EXPORT INO_T_64 \
4720 +       XATTR \
4721 +       FHSM \
4722 +       RDU \
4723 +       DIRREN \
4724 +       SHWH \
4725 +       BR_RAMFS \
4726 +       BR_FUSE POLL \
4727 +       BR_HFSPLUS \
4728 +       BDEV_LOOP \
4729 +       DEBUG MAGIC_SYSRQ
4730 +$(foreach i, ${AuConfAll}, \
4731 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4732 +
4733 +AuConfName = ${obj}/conf.str
4734 +${AuConfName}.tmp: FORCE
4735 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4736 +${AuConfName}: ${AuConfName}.tmp
4737 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4738 +       echo '  GEN    ' $@; \
4739 +       cp -p $< $@; \
4740 +       }
4741 +FORCE:
4742 +clean-files += ${AuConfName} ${AuConfName}.tmp
4743 +${obj}/sysfs.o: ${AuConfName}
4744 +
4745 +-include ${srctree}/${src}/conf_priv.mk
4746 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4747 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4748 +++ linux/fs/aufs/cpup.c        2023-04-24 08:13:40.969576115 +0200
4749 @@ -0,0 +1,1459 @@
4750 +// SPDX-License-Identifier: GPL-2.0
4751 +/*
4752 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4753 + *
4754 + * This program is free software; you can redistribute it and/or modify
4755 + * it under the terms of the GNU General Public License as published by
4756 + * the Free Software Foundation; either version 2 of the License, or
4757 + * (at your option) any later version.
4758 + *
4759 + * This program is distributed in the hope that it will be useful,
4760 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4761 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4762 + * GNU General Public License for more details.
4763 + *
4764 + * You should have received a copy of the GNU General Public License
4765 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4766 + */
4767 +
4768 +/*
4769 + * copy-up functions, see wbr_policy.c for copy-down
4770 + */
4771 +
4772 +#include <linux/fs_stack.h>
4773 +#include <linux/mm.h>
4774 +#include <linux/task_work.h>
4775 +#include "aufs.h"
4776 +
4777 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4778 +{
4779 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4780 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4781 +
4782 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4783 +
4784 +       dst->i_flags |= iflags & ~mask;
4785 +       if (au_test_fs_notime(dst->i_sb))
4786 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4787 +}
4788 +
4789 +void au_cpup_attr_timesizes(struct inode *inode)
4790 +{
4791 +       struct inode *h_inode;
4792 +
4793 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4794 +       fsstack_copy_attr_times(inode, h_inode);
4795 +       fsstack_copy_inode_size(inode, h_inode);
4796 +}
4797 +
4798 +void au_cpup_attr_nlink(struct inode *inode, int force)
4799 +{
4800 +       struct inode *h_inode;
4801 +       struct super_block *sb;
4802 +       aufs_bindex_t bindex, bbot;
4803 +
4804 +       sb = inode->i_sb;
4805 +       bindex = au_ibtop(inode);
4806 +       h_inode = au_h_iptr(inode, bindex);
4807 +       if (!force
4808 +           && !S_ISDIR(h_inode->i_mode)
4809 +           && au_opt_test(au_mntflags(sb), PLINK)
4810 +           && au_plink_test(inode))
4811 +               return;
4812 +
4813 +       /*
4814 +        * 0 can happen in revalidating.
4815 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4816 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4817 +        * case.
4818 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4819 +        *       the incorrect link count.
4820 +        */
4821 +       set_nlink(inode, h_inode->i_nlink);
4822 +
4823 +       /*
4824 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4825 +        * it may includes whplink directory.
4826 +        */
4827 +       if (S_ISDIR(h_inode->i_mode)) {
4828 +               bbot = au_ibbot(inode);
4829 +               for (bindex++; bindex <= bbot; bindex++) {
4830 +                       h_inode = au_h_iptr(inode, bindex);
4831 +                       if (h_inode)
4832 +                               au_add_nlink(inode, h_inode);
4833 +               }
4834 +       }
4835 +}
4836 +
4837 +void au_cpup_attr_changeable(struct inode *inode)
4838 +{
4839 +       struct inode *h_inode;
4840 +
4841 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4842 +       inode->i_mode = h_inode->i_mode;
4843 +       inode->i_uid = h_inode->i_uid;
4844 +       inode->i_gid = h_inode->i_gid;
4845 +       au_cpup_attr_timesizes(inode);
4846 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4847 +}
4848 +
4849 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4850 +{
4851 +       struct au_iinfo *iinfo = au_ii(inode);
4852 +
4853 +       IiMustWriteLock(inode);
4854 +
4855 +       iinfo->ii_higen = h_inode->i_generation;
4856 +       iinfo->ii_hsb1 = h_inode->i_sb;
4857 +}
4858 +
4859 +void au_cpup_attr_all(struct inode *inode, int force)
4860 +{
4861 +       struct inode *h_inode;
4862 +
4863 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4864 +       au_cpup_attr_changeable(inode);
4865 +       if (inode->i_nlink > 0)
4866 +               au_cpup_attr_nlink(inode, force);
4867 +       inode->i_rdev = h_inode->i_rdev;
4868 +       inode->i_blkbits = h_inode->i_blkbits;
4869 +       au_cpup_igen(inode, h_inode);
4870 +}
4871 +
4872 +/* ---------------------------------------------------------------------- */
4873 +
4874 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4875 +
4876 +/* keep the timestamps of the parent dir when cpup */
4877 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4878 +                   struct path *h_path)
4879 +{
4880 +       struct inode *h_inode;
4881 +
4882 +       dt->dt_dentry = dentry;
4883 +       dt->dt_h_path = *h_path;
4884 +       h_inode = d_inode(h_path->dentry);
4885 +       dt->dt_atime = h_inode->i_atime;
4886 +       dt->dt_mtime = h_inode->i_mtime;
4887 +       /* smp_mb(); */
4888 +}
4889 +
4890 +void au_dtime_revert(struct au_dtime *dt)
4891 +{
4892 +       struct iattr attr;
4893 +       int err;
4894 +
4895 +       attr.ia_atime = dt->dt_atime;
4896 +       attr.ia_mtime = dt->dt_mtime;
4897 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4898 +               | ATTR_ATIME | ATTR_ATIME_SET;
4899 +
4900 +       /* no delegation since this is a directory */
4901 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4902 +       if (unlikely(err))
4903 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4904 +}
4905 +
4906 +/* ---------------------------------------------------------------------- */
4907 +
4908 +/* internal use only */
4909 +struct au_cpup_reg_attr {
4910 +       int             valid;
4911 +       struct kstat    st;
4912 +       unsigned int    iflags; /* inode->i_flags */
4913 +};
4914 +
4915 +static noinline_for_stack
4916 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4917 +              struct au_cpup_reg_attr *h_src_attr)
4918 +{
4919 +       int err, sbits, icex;
4920 +       unsigned int mnt_flags;
4921 +       unsigned char verbose;
4922 +       struct iattr ia;
4923 +       struct path h_path;
4924 +       struct inode *h_isrc, *h_idst;
4925 +       struct kstat *h_st;
4926 +       struct au_branch *br;
4927 +
4928 +       br = au_sbr(dst->d_sb, bindex);
4929 +       h_path.mnt = au_br_mnt(br);
4930 +       h_path.dentry = au_h_dptr(dst, bindex);
4931 +       h_idst = d_inode(h_path.dentry);
4932 +       h_isrc = d_inode(h_src->dentry);
4933 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4934 +               | ATTR_ATIME | ATTR_MTIME
4935 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4936 +       if (h_src_attr && h_src_attr->valid) {
4937 +               h_st = &h_src_attr->st;
4938 +               ia.ia_uid = h_st->uid;
4939 +               ia.ia_gid = h_st->gid;
4940 +               ia.ia_atime = h_st->atime;
4941 +               ia.ia_mtime = h_st->mtime;
4942 +               if (h_idst->i_mode != h_st->mode
4943 +                   && !S_ISLNK(h_idst->i_mode)) {
4944 +                       ia.ia_valid |= ATTR_MODE;
4945 +                       ia.ia_mode = h_st->mode;
4946 +               }
4947 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4948 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4949 +       } else {
4950 +               ia.ia_uid = h_isrc->i_uid;
4951 +               ia.ia_gid = h_isrc->i_gid;
4952 +               ia.ia_atime = h_isrc->i_atime;
4953 +               ia.ia_mtime = h_isrc->i_mtime;
4954 +               if (h_idst->i_mode != h_isrc->i_mode
4955 +                   && !S_ISLNK(h_idst->i_mode)) {
4956 +                       ia.ia_valid |= ATTR_MODE;
4957 +                       ia.ia_mode = h_isrc->i_mode;
4958 +               }
4959 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4960 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4961 +       }
4962 +       /* no delegation since it is just created */
4963 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4964 +
4965 +       /* is this nfs only? */
4966 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4967 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4968 +               ia.ia_mode = h_isrc->i_mode;
4969 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4970 +       }
4971 +
4972 +       icex = br->br_perm & AuBrAttr_ICEX;
4973 +       if (!err) {
4974 +               mnt_flags = au_mntflags(dst->d_sb);
4975 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4976 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4977 +       }
4978 +
4979 +       return err;
4980 +}
4981 +
4982 +/* ---------------------------------------------------------------------- */
4983 +
4984 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4985 +                          char *buf, unsigned long blksize)
4986 +{
4987 +       int err;
4988 +       size_t sz, rbytes, wbytes;
4989 +       unsigned char all_zero;
4990 +       char *p, *zp;
4991 +       struct inode *h_inode;
4992 +       /* reduce stack usage */
4993 +       struct iattr *ia;
4994 +
4995 +       zp = page_address(ZERO_PAGE(0));
4996 +       if (unlikely(!zp))
4997 +               return -ENOMEM; /* possible? */
4998 +
4999 +       err = 0;
5000 +       all_zero = 0;
5001 +       while (len) {
5002 +               AuDbg("len %lld\n", len);
5003 +               sz = blksize;
5004 +               if (len < blksize)
5005 +                       sz = len;
5006 +
5007 +               rbytes = 0;
5008 +               /* todo: signal_pending? */
5009 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5010 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5011 +                       err = rbytes;
5012 +               }
5013 +               if (unlikely(err < 0))
5014 +                       break;
5015 +
5016 +               all_zero = 0;
5017 +               if (len >= rbytes && rbytes == blksize)
5018 +                       all_zero = !memcmp(buf, zp, rbytes);
5019 +               if (!all_zero) {
5020 +                       wbytes = rbytes;
5021 +                       p = buf;
5022 +                       while (wbytes) {
5023 +                               size_t b;
5024 +
5025 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5026 +                               err = b;
5027 +                               /* todo: signal_pending? */
5028 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5029 +                                       continue;
5030 +                               if (unlikely(err < 0))
5031 +                                       break;
5032 +                               wbytes -= b;
5033 +                               p += b;
5034 +                       }
5035 +                       if (unlikely(err < 0))
5036 +                               break;
5037 +               } else {
5038 +                       loff_t res;
5039 +
5040 +                       AuLabel(hole);
5041 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5042 +                       err = res;
5043 +                       if (unlikely(res < 0))
5044 +                               break;
5045 +               }
5046 +               len -= rbytes;
5047 +               err = 0;
5048 +       }
5049 +
5050 +       /* the last block may be a hole */
5051 +       if (!err && all_zero) {
5052 +               AuLabel(last hole);
5053 +
5054 +               err = 1;
5055 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5056 +                       /* nfs requires this step to make last hole */
5057 +                       /* is this only nfs? */
5058 +                       do {
5059 +                               /* todo: signal_pending? */
5060 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5061 +                       } while (err == -EAGAIN || err == -EINTR);
5062 +                       if (err == 1)
5063 +                               dst->f_pos--;
5064 +               }
5065 +
5066 +               if (err == 1) {
5067 +                       ia = (void *)buf;
5068 +                       ia->ia_size = dst->f_pos;
5069 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5070 +                       ia->ia_file = dst;
5071 +                       h_inode = file_inode(dst);
5072 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5073 +                       /* no delegation since it is just created */
5074 +                       err = vfsub_notify_change(&dst->f_path, ia,
5075 +                                                 /*delegated*/NULL);
5076 +                       inode_unlock(h_inode);
5077 +               }
5078 +       }
5079 +
5080 +       return err;
5081 +}
5082 +
5083 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5084 +{
5085 +       int err;
5086 +       unsigned long blksize;
5087 +       unsigned char do_kfree;
5088 +       char *buf;
5089 +       struct super_block *h_sb;
5090 +
5091 +       err = -ENOMEM;
5092 +       h_sb = file_inode(dst)->i_sb;
5093 +       blksize = h_sb->s_blocksize;
5094 +       if (!blksize || PAGE_SIZE < blksize)
5095 +               blksize = PAGE_SIZE;
5096 +       AuDbg("blksize %lu\n", blksize);
5097 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5098 +       if (do_kfree)
5099 +               buf = kmalloc(blksize, GFP_NOFS);
5100 +       else
5101 +               buf = (void *)__get_free_page(GFP_NOFS);
5102 +       if (unlikely(!buf))
5103 +               goto out;
5104 +
5105 +       if (len > (1 << 22))
5106 +               AuDbg("copying a large file %lld\n", (long long)len);
5107 +
5108 +       src->f_pos = 0;
5109 +       dst->f_pos = 0;
5110 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5111 +       if (do_kfree) {
5112 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5113 +               au_kfree_do_rcu(buf);
5114 +       } else
5115 +               free_page((unsigned long)buf);
5116 +
5117 +out:
5118 +       return err;
5119 +}
5120 +
5121 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5122 +{
5123 +       int err;
5124 +       struct super_block *h_src_sb;
5125 +       struct inode *h_src_inode;
5126 +
5127 +       h_src_inode = file_inode(src);
5128 +       h_src_sb = h_src_inode->i_sb;
5129 +
5130 +       /* XFS acquires inode_lock */
5131 +       if (!au_test_xfs(h_src_sb))
5132 +               err = au_copy_file(dst, src, len);
5133 +       else {
5134 +               inode_unlock_shared(h_src_inode);
5135 +               err = au_copy_file(dst, src, len);
5136 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5137 +       }
5138 +
5139 +       return err;
5140 +}
5141 +
5142 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5143 +{
5144 +       int err;
5145 +       loff_t lo;
5146 +       struct super_block *h_src_sb;
5147 +       struct inode *h_src_inode;
5148 +
5149 +       h_src_inode = file_inode(src);
5150 +       h_src_sb = h_src_inode->i_sb;
5151 +       if (h_src_sb != file_inode(dst)->i_sb
5152 +           || !dst->f_op->remap_file_range) {
5153 +               err = au_do_copy(dst, src, len);
5154 +               goto out;
5155 +       }
5156 +
5157 +       if (!au_test_nfs(h_src_sb)) {
5158 +               inode_unlock_shared(h_src_inode);
5159 +               lo = vfsub_clone_file_range(src, dst, len);
5160 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5161 +       } else
5162 +               lo = vfsub_clone_file_range(src, dst, len);
5163 +       if (lo == len) {
5164 +               err = 0;
5165 +               goto out; /* success */
5166 +       } else if (lo >= 0)
5167 +               /* todo: possible? */
5168 +               /* paritially succeeded */
5169 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5170 +       else if (lo != -EOPNOTSUPP) {
5171 +               /* older XFS has a condition in cloning */
5172 +               err = lo;
5173 +               goto out;
5174 +       }
5175 +
5176 +       /* the backend fs on NFS may not support cloning */
5177 +       err = au_do_copy(dst, src, len);
5178 +
5179 +out:
5180 +       AuTraceErr(err);
5181 +       return err;
5182 +}
5183 +
5184 +/*
5185 + * to support a sparse file which is opened with O_APPEND,
5186 + * we need to close the file.
5187 + */
5188 +static int au_cp_regular(struct au_cp_generic *cpg)
5189 +{
5190 +       int err, i;
5191 +       enum { SRC, DST };
5192 +       struct {
5193 +               aufs_bindex_t bindex;
5194 +               unsigned int flags;
5195 +               struct dentry *dentry;
5196 +               int force_wr;
5197 +               struct file *file;
5198 +       } *f, file[] = {
5199 +               {
5200 +                       .bindex = cpg->bsrc,
5201 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5202 +               },
5203 +               {
5204 +                       .bindex = cpg->bdst,
5205 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5206 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5207 +               }
5208 +       };
5209 +       struct au_branch *br;
5210 +       struct super_block *sb, *h_src_sb;
5211 +       struct inode *h_src_inode;
5212 +       struct task_struct *tsk = current;
5213 +
5214 +       /* bsrc branch can be ro/rw. */
5215 +       sb = cpg->dentry->d_sb;
5216 +       f = file;
5217 +       for (i = 0; i < 2; i++, f++) {
5218 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5219 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5220 +                                   /*file*/NULL, f->force_wr);
5221 +               if (IS_ERR(f->file)) {
5222 +                       err = PTR_ERR(f->file);
5223 +                       if (i == SRC)
5224 +                               goto out;
5225 +                       else
5226 +                               goto out_src;
5227 +               }
5228 +       }
5229 +
5230 +       /* try stopping to update while we copyup */
5231 +       h_src_inode = d_inode(file[SRC].dentry);
5232 +       h_src_sb = h_src_inode->i_sb;
5233 +       if (!au_test_nfs(h_src_sb))
5234 +               IMustLock(h_src_inode);
5235 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5236 +
5237 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5238 +       if (tsk->flags & PF_KTHREAD)
5239 +               __fput_sync(file[DST].file);
5240 +       else {
5241 +               /* it happened actually */
5242 +               fput(file[DST].file);
5243 +               /*
5244 +                * too bad.
5245 +                * we have to call both since we don't know which place the file
5246 +                * was added to.
5247 +                */
5248 +               task_work_run();
5249 +               flush_delayed_fput();
5250 +       }
5251 +       br = au_sbr(sb, file[DST].bindex);
5252 +       au_lcnt_dec(&br->br_nfiles);
5253 +
5254 +out_src:
5255 +       fput(file[SRC].file);
5256 +       br = au_sbr(sb, file[SRC].bindex);
5257 +       au_lcnt_dec(&br->br_nfiles);
5258 +out:
5259 +       return err;
5260 +}
5261 +
5262 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5263 +                             struct au_cpup_reg_attr *h_src_attr)
5264 +{
5265 +       int err, rerr;
5266 +       loff_t l;
5267 +       struct path h_path;
5268 +       struct inode *h_src_inode, *h_dst_inode;
5269 +
5270 +       err = 0;
5271 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5272 +       l = i_size_read(h_src_inode);
5273 +       if (cpg->len == -1 || l < cpg->len)
5274 +               cpg->len = l;
5275 +       if (cpg->len) {
5276 +               /* try stopping to update while we are referencing */
5277 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5278 +               au_pin_hdir_unlock(cpg->pin);
5279 +
5280 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5281 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5282 +               h_src_attr->iflags = h_src_inode->i_flags;
5283 +               if (!au_test_nfs(h_src_inode->i_sb))
5284 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5285 +               else {
5286 +                       inode_unlock_shared(h_src_inode);
5287 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5288 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5289 +               }
5290 +               if (unlikely(err)) {
5291 +                       inode_unlock_shared(h_src_inode);
5292 +                       goto out;
5293 +               }
5294 +               h_src_attr->valid = 1;
5295 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5296 +                       err = au_cp_regular(cpg);
5297 +                       inode_unlock_shared(h_src_inode);
5298 +               } else {
5299 +                       inode_unlock_shared(h_src_inode);
5300 +                       err = au_cp_regular(cpg);
5301 +               }
5302 +               rerr = au_pin_hdir_relock(cpg->pin);
5303 +               if (!err && rerr)
5304 +                       err = rerr;
5305 +       }
5306 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5307 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5308 +               h_dst_inode = d_inode(h_path.dentry);
5309 +               spin_lock(&h_dst_inode->i_lock);
5310 +               h_dst_inode->i_state |= I_LINKABLE;
5311 +               spin_unlock(&h_dst_inode->i_lock);
5312 +       }
5313 +
5314 +out:
5315 +       return err;
5316 +}
5317 +
5318 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5319 +                             struct inode *h_dir)
5320 +{
5321 +       int err;
5322 +       DEFINE_DELAYED_CALL(done);
5323 +       const char *sym;
5324 +
5325 +       sym = vfs_get_link(h_src, &done);
5326 +       err = PTR_ERR(sym);
5327 +       if (IS_ERR(sym))
5328 +               goto out;
5329 +
5330 +       err = vfsub_symlink(h_dir, h_path, sym);
5331 +
5332 +out:
5333 +       do_delayed_call(&done);
5334 +       return err;
5335 +}
5336 +
5337 +/*
5338 + * regardless 'acl' option, reset all ACL.
5339 + * All ACL will be copied up later from the original entry on the lower branch.
5340 + */
5341 +static int au_reset_acl(struct path *h_path, umode_t mode)
5342 +{
5343 +       int err;
5344 +       struct dentry *h_dentry;
5345 +       /* struct inode *h_inode; */
5346 +       struct mnt_idmap *h_idmap;
5347 +
5348 +       h_idmap = mnt_idmap(h_path->mnt);
5349 +       h_dentry = h_path->dentry;
5350 +       /* h_inode = d_inode(h_dentry); */
5351 +       /* forget_all_cached_acls(h_inode)); */
5352 +       err = vfsub_remove_acl(h_idmap, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5353 +       AuTraceErr(err);
5354 +       if (err == -EOPNOTSUPP)
5355 +               err = 0;
5356 +       if (!err)
5357 +               err = vfsub_acl_chmod(h_idmap, h_dentry, mode);
5358 +
5359 +       AuTraceErr(err);
5360 +       return err;
5361 +}
5362 +
5363 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5364 +                         struct inode *h_dir, struct path *h_path)
5365 +{
5366 +       int err;
5367 +       struct inode *dir, *inode;
5368 +       struct mnt_idmap *h_idmap;
5369 +
5370 +       h_idmap = mnt_idmap(h_path->mnt);
5371 +       err = vfsub_remove_acl(h_idmap, h_path->dentry,
5372 +                              XATTR_NAME_POSIX_ACL_DEFAULT);
5373 +       AuTraceErr(err);
5374 +       if (err == -EOPNOTSUPP)
5375 +               err = 0;
5376 +       if (unlikely(err))
5377 +               goto out;
5378 +
5379 +       /*
5380 +        * strange behaviour from the users view,
5381 +        * particularly setattr case
5382 +        */
5383 +       dir = d_inode(dst_parent);
5384 +       if (au_ibtop(dir) == cpg->bdst)
5385 +               au_cpup_attr_nlink(dir, /*force*/1);
5386 +       inode = d_inode(cpg->dentry);
5387 +       au_cpup_attr_nlink(inode, /*force*/1);
5388 +
5389 +out:
5390 +       return err;
5391 +}
5392 +
5393 +static noinline_for_stack
5394 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5395 +              struct au_cpup_reg_attr *h_src_attr)
5396 +{
5397 +       int err;
5398 +       umode_t mode;
5399 +       unsigned int mnt_flags;
5400 +       unsigned char isdir, isreg, force;
5401 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5402 +       struct au_dtime dt;
5403 +       struct path h_path;
5404 +       struct dentry *h_src, *h_dst, *h_parent;
5405 +       struct inode *h_inode, *h_dir;
5406 +       struct super_block *sb;
5407 +
5408 +       /* bsrc branch can be ro/rw. */
5409 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5410 +       h_inode = d_inode(h_src);
5411 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5412 +
5413 +       /* try stopping to be referenced while we are creating */
5414 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5415 +       if (au_ftest_cpup(cpg->flags, RENAME))
5416 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5417 +                                 AUFS_WH_PFX_LEN));
5418 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5419 +       h_dir = d_inode(h_parent);
5420 +       IMustLock(h_dir);
5421 +       AuDebugOn(h_parent != h_dst->d_parent);
5422 +
5423 +       sb = cpg->dentry->d_sb;
5424 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5425 +       if (do_dt) {
5426 +               h_path.dentry = h_parent;
5427 +               au_dtime_store(&dt, dst_parent, &h_path);
5428 +       }
5429 +       h_path.dentry = h_dst;
5430 +
5431 +       isreg = 0;
5432 +       isdir = 0;
5433 +       mode = h_inode->i_mode;
5434 +       switch (mode & S_IFMT) {
5435 +       case S_IFREG:
5436 +               isreg = 1;
5437 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5438 +               if (!err)
5439 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5440 +               break;
5441 +       case S_IFDIR:
5442 +               isdir = 1;
5443 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5444 +               if (!err)
5445 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5446 +               break;
5447 +       case S_IFLNK:
5448 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5449 +               break;
5450 +       case S_IFCHR:
5451 +       case S_IFBLK:
5452 +               AuDebugOn(!capable(CAP_MKNOD));
5453 +               fallthrough;
5454 +       case S_IFIFO:
5455 +       case S_IFSOCK:
5456 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5457 +               break;
5458 +       default:
5459 +               AuIOErr("Unknown inode type 0%o\n", mode);
5460 +               err = -EIO;
5461 +       }
5462 +       if (!err)
5463 +               err = au_reset_acl(&h_path, mode);
5464 +
5465 +       mnt_flags = au_mntflags(sb);
5466 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5467 +           && !isdir
5468 +           && au_opt_test(mnt_flags, XINO)
5469 +           && (h_inode->i_nlink == 1
5470 +               || (h_inode->i_state & I_LINKABLE))
5471 +           /* todo: unnecessary? */
5472 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5473 +           && cpg->bdst < cpg->bsrc
5474 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5475 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5476 +               /* ignore this error */
5477 +
5478 +       if (!err) {
5479 +               force = 0;
5480 +               if (isreg) {
5481 +                       force = !!cpg->len;
5482 +                       if (cpg->len == -1)
5483 +                               force = !!i_size_read(h_inode);
5484 +               }
5485 +               au_fhsm_wrote(sb, cpg->bdst, force);
5486 +       }
5487 +
5488 +       if (do_dt)
5489 +               au_dtime_revert(&dt);
5490 +       return err;
5491 +}
5492 +
5493 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5494 +{
5495 +       int err;
5496 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5497 +       struct path h_ppath;
5498 +       struct inode *h_dir;
5499 +       aufs_bindex_t bdst;
5500 +
5501 +       dentry = cpg->dentry;
5502 +       bdst = cpg->bdst;
5503 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5504 +       h_dentry = au_h_dptr(dentry, bdst);
5505 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5506 +               dget(h_dentry);
5507 +               au_set_h_dptr(dentry, bdst, NULL);
5508 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5509 +               if (!err)
5510 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5511 +               au_set_h_dptr(dentry, bdst, h_dentry);
5512 +       } else {
5513 +               err = 0;
5514 +               parent = dget_parent(dentry);
5515 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5516 +               dput(parent);
5517 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5518 +               if (IS_ERR(h_path->dentry))
5519 +                       err = PTR_ERR(h_path->dentry);
5520 +       }
5521 +       if (unlikely(err))
5522 +               goto out;
5523 +
5524 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5525 +       h_dir = d_inode(h_parent);
5526 +       IMustLock(h_dir);
5527 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5528 +       /* no delegation since it is just created */
5529 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5530 +                          /*flags*/0);
5531 +       dput(h_path->dentry);
5532 +
5533 +out:
5534 +       return err;
5535 +}
5536 +
5537 +/*
5538 + * copyup the @dentry from @bsrc to @bdst.
5539 + * the caller must set the both of lower dentries.
5540 + * @len is for truncating when it is -1 copyup the entire file.
5541 + * in link/rename cases, @dst_parent may be different from the real one.
5542 + * basic->bsrc can be larger than basic->bdst.
5543 + * aufs doesn't touch the credential so
5544 + * security_inode_copy_up{,_xattr}() are unnecessary.
5545 + */
5546 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5547 +{
5548 +       int err, rerr;
5549 +       aufs_bindex_t old_ibtop;
5550 +       unsigned char isdir, plink;
5551 +       struct dentry *h_src, *h_dst, *h_parent;
5552 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5553 +       struct super_block *sb;
5554 +       struct au_branch *br;
5555 +       struct path h_src_path;
5556 +       /* to reduce stack size */
5557 +       struct {
5558 +               struct au_dtime dt;
5559 +               struct path h_path;
5560 +               struct au_cpup_reg_attr h_src_attr;
5561 +       } *a;
5562 +
5563 +       err = -ENOMEM;
5564 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5565 +       if (unlikely(!a))
5566 +               goto out;
5567 +       a->h_src_attr.valid = 0;
5568 +
5569 +       sb = cpg->dentry->d_sb;
5570 +       br = au_sbr(sb, cpg->bdst);
5571 +       a->h_path.mnt = au_br_mnt(br);
5572 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5573 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5574 +       h_dir = d_inode(h_parent);
5575 +       IMustLock(h_dir);
5576 +
5577 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5578 +       inode = d_inode(cpg->dentry);
5579 +
5580 +       if (!dst_parent)
5581 +               dst_parent = dget_parent(cpg->dentry);
5582 +       else
5583 +               dget(dst_parent);
5584 +
5585 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5586 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5587 +       if (dst_inode) {
5588 +               if (unlikely(!plink)) {
5589 +                       err = -EIO;
5590 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5591 +                               "but plink is disabled\n",
5592 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5593 +                       goto out_parent;
5594 +               }
5595 +
5596 +               if (dst_inode->i_nlink) {
5597 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5598 +
5599 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5600 +                       err = PTR_ERR(h_src);
5601 +                       if (IS_ERR(h_src))
5602 +                               goto out_parent;
5603 +                       if (unlikely(d_is_negative(h_src))) {
5604 +                               err = -EIO;
5605 +                               AuIOErr("i%lu exists on b%d "
5606 +                                       "but not pseudo-linked\n",
5607 +                                       inode->i_ino, cpg->bdst);
5608 +                               dput(h_src);
5609 +                               goto out_parent;
5610 +                       }
5611 +
5612 +                       if (do_dt) {
5613 +                               a->h_path.dentry = h_parent;
5614 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5615 +                       }
5616 +
5617 +                       a->h_path.dentry = h_dst;
5618 +                       delegated = NULL;
5619 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5620 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5621 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5622 +                       if (do_dt)
5623 +                               au_dtime_revert(&a->dt);
5624 +                       if (unlikely(err == -EWOULDBLOCK)) {
5625 +                               pr_warn("cannot retry for NFSv4 delegation"
5626 +                                       " for an internal link\n");
5627 +                               iput(delegated);
5628 +                       }
5629 +                       dput(h_src);
5630 +                       goto out_parent;
5631 +               } else
5632 +                       /* todo: cpup_wh_file? */
5633 +                       /* udba work */
5634 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5635 +       }
5636 +
5637 +       isdir = S_ISDIR(inode->i_mode);
5638 +       old_ibtop = au_ibtop(inode);
5639 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5640 +       if (unlikely(err))
5641 +               goto out_rev;
5642 +       dst_inode = d_inode(h_dst);
5643 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5644 +       /* todo: necessary? */
5645 +       /* au_pin_hdir_unlock(cpg->pin); */
5646 +
5647 +       h_src_path.dentry = h_src;
5648 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5649 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5650 +       if (unlikely(err)) {
5651 +               /* todo: necessary? */
5652 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5653 +               inode_unlock(dst_inode);
5654 +               goto out_rev;
5655 +       }
5656 +
5657 +       if (cpg->bdst < old_ibtop) {
5658 +               if (S_ISREG(inode->i_mode)) {
5659 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5660 +                       if (unlikely(err)) {
5661 +                               /* ignore an error */
5662 +                               /* au_pin_hdir_relock(cpg->pin); */
5663 +                               inode_unlock(dst_inode);
5664 +                               goto out_rev;
5665 +                       }
5666 +               }
5667 +               au_set_ibtop(inode, cpg->bdst);
5668 +       } else
5669 +               au_set_ibbot(inode, cpg->bdst);
5670 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5671 +                     au_hi_flags(inode, isdir));
5672 +
5673 +       /* todo: necessary? */
5674 +       /* err = au_pin_hdir_relock(cpg->pin); */
5675 +       inode_unlock(dst_inode);
5676 +       if (unlikely(err))
5677 +               goto out_rev;
5678 +
5679 +       src_inode = d_inode(h_src);
5680 +       if (!isdir
5681 +           && (src_inode->i_nlink > 1
5682 +               || src_inode->i_state & I_LINKABLE)
5683 +           && plink)
5684 +               au_plink_append(inode, cpg->bdst, h_dst);
5685 +
5686 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5687 +               a->h_path.dentry = h_dst;
5688 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5689 +       }
5690 +       if (!err)
5691 +               goto out_parent; /* success */
5692 +
5693 +       /* revert */
5694 +out_rev:
5695 +       a->h_path.dentry = h_parent;
5696 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5697 +       a->h_path.dentry = h_dst;
5698 +       rerr = 0;
5699 +       if (d_is_positive(h_dst)) {
5700 +               if (!isdir) {
5701 +                       /* no delegation since it is just created */
5702 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5703 +                                           /*delegated*/NULL, /*force*/0);
5704 +               } else
5705 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5706 +       }
5707 +       au_dtime_revert(&a->dt);
5708 +       if (rerr) {
5709 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5710 +               err = -EIO;
5711 +       }
5712 +out_parent:
5713 +       dput(dst_parent);
5714 +       au_kfree_rcu(a);
5715 +out:
5716 +       return err;
5717 +}
5718 +
5719 +#if 0 /* reserved */
5720 +struct au_cpup_single_args {
5721 +       int *errp;
5722 +       struct au_cp_generic *cpg;
5723 +       struct dentry *dst_parent;
5724 +};
5725 +
5726 +static void au_call_cpup_single(void *args)
5727 +{
5728 +       struct au_cpup_single_args *a = args;
5729 +
5730 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5731 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5732 +       au_pin_hdir_release(a->cpg->pin);
5733 +}
5734 +#endif
5735 +
5736 +/*
5737 + * prevent SIGXFSZ in copy-up.
5738 + * testing CAP_MKNOD is for generic fs,
5739 + * but CAP_FSETID is for xfs only, currently.
5740 + */
5741 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5742 +{
5743 +       int do_sio;
5744 +       struct super_block *sb;
5745 +       struct inode *h_dir;
5746 +
5747 +       do_sio = 0;
5748 +       sb = au_pinned_parent(pin)->d_sb;
5749 +       if (!au_wkq_test()
5750 +           && (!au_sbi(sb)->si_plink_maint_pid
5751 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5752 +               switch (mode & S_IFMT) {
5753 +               case S_IFREG:
5754 +                       /* no condition about RLIMIT_FSIZE and the file size */
5755 +                       do_sio = 1;
5756 +                       break;
5757 +               case S_IFCHR:
5758 +               case S_IFBLK:
5759 +                       do_sio = !capable(CAP_MKNOD);
5760 +                       break;
5761 +               }
5762 +               if (!do_sio)
5763 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5764 +                                 && !capable(CAP_FSETID));
5765 +               /* this workaround may be removed in the future */
5766 +               if (!do_sio) {
5767 +                       h_dir = au_pinned_h_dir(pin);
5768 +                       do_sio = h_dir->i_mode & S_ISVTX;
5769 +               }
5770 +       }
5771 +
5772 +       return do_sio;
5773 +}
5774 +
5775 +#if 0 /* reserved */
5776 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5777 +{
5778 +       int err, wkq_err;
5779 +       struct dentry *h_dentry;
5780 +
5781 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5782 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5783 +               err = au_cpup_single(cpg, dst_parent);
5784 +       else {
5785 +               struct au_cpup_single_args args = {
5786 +                       .errp           = &err,
5787 +                       .cpg            = cpg,
5788 +                       .dst_parent     = dst_parent
5789 +               };
5790 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5791 +               if (unlikely(wkq_err))
5792 +                       err = wkq_err;
5793 +       }
5794 +
5795 +       return err;
5796 +}
5797 +#endif
5798 +
5799 +/*
5800 + * copyup the @dentry from the first active lower branch to @bdst,
5801 + * using au_cpup_single().
5802 + */
5803 +static int au_cpup_simple(struct au_cp_generic *cpg)
5804 +{
5805 +       int err;
5806 +       unsigned int flags_orig;
5807 +       struct dentry *dentry;
5808 +
5809 +       AuDebugOn(cpg->bsrc < 0);
5810 +
5811 +       dentry = cpg->dentry;
5812 +       DiMustWriteLock(dentry);
5813 +
5814 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5815 +       if (!err) {
5816 +               flags_orig = cpg->flags;
5817 +               au_fset_cpup(cpg->flags, RENAME);
5818 +               err = au_cpup_single(cpg, NULL);
5819 +               cpg->flags = flags_orig;
5820 +               if (!err)
5821 +                       return 0; /* success */
5822 +
5823 +               /* revert */
5824 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5825 +               au_set_dbtop(dentry, cpg->bsrc);
5826 +       }
5827 +
5828 +       return err;
5829 +}
5830 +
5831 +struct au_cpup_simple_args {
5832 +       int *errp;
5833 +       struct au_cp_generic *cpg;
5834 +};
5835 +
5836 +static void au_call_cpup_simple(void *args)
5837 +{
5838 +       struct au_cpup_simple_args *a = args;
5839 +
5840 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5841 +       *a->errp = au_cpup_simple(a->cpg);
5842 +       au_pin_hdir_release(a->cpg->pin);
5843 +}
5844 +
5845 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5846 +{
5847 +       int err, wkq_err;
5848 +       struct dentry *dentry, *parent;
5849 +       struct file *h_file;
5850 +       struct inode *h_dir;
5851 +       struct mnt_idmap *h_idmap;
5852 +
5853 +       dentry = cpg->dentry;
5854 +       h_file = NULL;
5855 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5856 +               AuDebugOn(cpg->bsrc < 0);
5857 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5858 +               err = PTR_ERR(h_file);
5859 +               if (IS_ERR(h_file))
5860 +                       goto out;
5861 +       }
5862 +
5863 +       parent = dget_parent(dentry);
5864 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5865 +       h_idmap = au_sbr_idmap(dentry->d_sb, cpg->bdst);
5866 +       if (!au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC | MAY_WRITE)
5867 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5868 +               err = au_cpup_simple(cpg);
5869 +       else {
5870 +               struct au_cpup_simple_args args = {
5871 +                       .errp           = &err,
5872 +                       .cpg            = cpg
5873 +               };
5874 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5875 +               if (unlikely(wkq_err))
5876 +                       err = wkq_err;
5877 +       }
5878 +
5879 +       dput(parent);
5880 +       if (h_file)
5881 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5882 +
5883 +out:
5884 +       return err;
5885 +}
5886 +
5887 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5888 +{
5889 +       aufs_bindex_t bsrc, bbot;
5890 +       struct dentry *dentry, *h_dentry;
5891 +
5892 +       if (cpg->bsrc < 0) {
5893 +               dentry = cpg->dentry;
5894 +               bbot = au_dbbot(dentry);
5895 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5896 +                       h_dentry = au_h_dptr(dentry, bsrc);
5897 +                       if (h_dentry) {
5898 +                               AuDebugOn(d_is_negative(h_dentry));
5899 +                               break;
5900 +                       }
5901 +               }
5902 +               AuDebugOn(bsrc > bbot);
5903 +               cpg->bsrc = bsrc;
5904 +       }
5905 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5906 +       return au_do_sio_cpup_simple(cpg);
5907 +}
5908 +
5909 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5910 +{
5911 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5912 +       return au_do_sio_cpup_simple(cpg);
5913 +}
5914 +
5915 +/* ---------------------------------------------------------------------- */
5916 +
5917 +/*
5918 + * copyup the deleted file for writing.
5919 + */
5920 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5921 +                        struct file *file)
5922 +{
5923 +       int err;
5924 +       unsigned int flags_orig;
5925 +       aufs_bindex_t bsrc_orig;
5926 +       struct au_dinfo *dinfo;
5927 +       struct {
5928 +               struct au_hdentry *hd;
5929 +               struct dentry *h_dentry;
5930 +       } hdst, hsrc;
5931 +
5932 +       dinfo = au_di(cpg->dentry);
5933 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5934 +
5935 +       bsrc_orig = cpg->bsrc;
5936 +       cpg->bsrc = dinfo->di_btop;
5937 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5938 +       hdst.h_dentry = hdst.hd->hd_dentry;
5939 +       hdst.hd->hd_dentry = wh_dentry;
5940 +       dinfo->di_btop = cpg->bdst;
5941 +
5942 +       hsrc.h_dentry = NULL;
5943 +       if (file) {
5944 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5945 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5946 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5947 +       }
5948 +       flags_orig = cpg->flags;
5949 +       cpg->flags = !AuCpup_DTIME;
5950 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5951 +       cpg->flags = flags_orig;
5952 +       if (file) {
5953 +               if (!err)
5954 +                       err = au_reopen_nondir(file);
5955 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5956 +       }
5957 +       hdst.hd->hd_dentry = hdst.h_dentry;
5958 +       dinfo->di_btop = cpg->bsrc;
5959 +       cpg->bsrc = bsrc_orig;
5960 +
5961 +       return err;
5962 +}
5963 +
5964 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5965 +{
5966 +       int err;
5967 +       aufs_bindex_t bdst;
5968 +       struct au_dtime dt;
5969 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5970 +       struct au_branch *br;
5971 +       struct path h_path;
5972 +
5973 +       dentry = cpg->dentry;
5974 +       bdst = cpg->bdst;
5975 +       br = au_sbr(dentry->d_sb, bdst);
5976 +       parent = dget_parent(dentry);
5977 +       h_parent = au_h_dptr(parent, bdst);
5978 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5979 +       err = PTR_ERR(wh_dentry);
5980 +       if (IS_ERR(wh_dentry))
5981 +               goto out;
5982 +
5983 +       h_path.dentry = h_parent;
5984 +       h_path.mnt = au_br_mnt(br);
5985 +       au_dtime_store(&dt, parent, &h_path);
5986 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5987 +       if (unlikely(err))
5988 +               goto out_wh;
5989 +
5990 +       dget(wh_dentry);
5991 +       h_path.dentry = wh_dentry;
5992 +       if (!d_is_dir(wh_dentry)) {
5993 +               /* no delegation since it is just created */
5994 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5995 +                                  /*delegated*/NULL, /*force*/0);
5996 +       } else
5997 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5998 +       if (unlikely(err)) {
5999 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6000 +                       wh_dentry, err);
6001 +               err = -EIO;
6002 +       }
6003 +       au_dtime_revert(&dt);
6004 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6005 +
6006 +out_wh:
6007 +       dput(wh_dentry);
6008 +out:
6009 +       dput(parent);
6010 +       return err;
6011 +}
6012 +
6013 +struct au_cpup_wh_args {
6014 +       int *errp;
6015 +       struct au_cp_generic *cpg;
6016 +       struct file *file;
6017 +};
6018 +
6019 +static void au_call_cpup_wh(void *args)
6020 +{
6021 +       struct au_cpup_wh_args *a = args;
6022 +
6023 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6024 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6025 +       au_pin_hdir_release(a->cpg->pin);
6026 +}
6027 +
6028 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6029 +{
6030 +       int err, wkq_err;
6031 +       aufs_bindex_t bdst;
6032 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6033 +       struct inode *dir, *h_dir, *h_tmpdir;
6034 +       struct au_wbr *wbr;
6035 +       struct au_pin wh_pin, *pin_orig;
6036 +       struct mnt_idmap *h_idmap;
6037 +
6038 +       dentry = cpg->dentry;
6039 +       bdst = cpg->bdst;
6040 +       parent = dget_parent(dentry);
6041 +       dir = d_inode(parent);
6042 +       h_orph = NULL;
6043 +       h_parent = NULL;
6044 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6045 +       h_tmpdir = h_dir;
6046 +       pin_orig = NULL;
6047 +       if (!h_dir->i_nlink) {
6048 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6049 +               h_orph = wbr->wbr_orph;
6050 +
6051 +               h_parent = dget(au_h_dptr(parent, bdst));
6052 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6053 +               h_tmpdir = d_inode(h_orph);
6054 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6055 +
6056 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6057 +               /* todo: au_h_open_pre()? */
6058 +
6059 +               pin_orig = cpg->pin;
6060 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6061 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6062 +               cpg->pin = &wh_pin;
6063 +       }
6064 +
6065 +       h_idmap = au_sbr_idmap(dentry->d_sb, bdst);
6066 +       if (!au_test_h_perm_sio(h_idmap, h_tmpdir, MAY_EXEC | MAY_WRITE)
6067 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6068 +               err = au_cpup_wh(cpg, file);
6069 +       else {
6070 +               struct au_cpup_wh_args args = {
6071 +                       .errp   = &err,
6072 +                       .cpg    = cpg,
6073 +                       .file   = file
6074 +               };
6075 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6076 +               if (unlikely(wkq_err))
6077 +                       err = wkq_err;
6078 +       }
6079 +
6080 +       if (h_orph) {
6081 +               inode_unlock(h_tmpdir);
6082 +               /* todo: au_h_open_post()? */
6083 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6084 +               au_set_h_dptr(parent, bdst, h_parent);
6085 +               AuDebugOn(!pin_orig);
6086 +               cpg->pin = pin_orig;
6087 +       }
6088 +       iput(h_dir);
6089 +       dput(parent);
6090 +
6091 +       return err;
6092 +}
6093 +
6094 +/* ---------------------------------------------------------------------- */
6095 +
6096 +/*
6097 + * generic routine for both of copy-up and copy-down.
6098 + */
6099 +/* cf. revalidate function in file.c */
6100 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6101 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6102 +                        struct au_pin *pin,
6103 +                        struct dentry *h_parent, void *arg),
6104 +              void *arg)
6105 +{
6106 +       int err;
6107 +       struct au_pin pin;
6108 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6109 +
6110 +       err = 0;
6111 +       parent = dget_parent(dentry);
6112 +       if (IS_ROOT(parent))
6113 +               goto out;
6114 +
6115 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6116 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6117 +
6118 +       /* do not use au_dpage */
6119 +       real_parent = parent;
6120 +       while (1) {
6121 +               dput(parent);
6122 +               parent = dget_parent(dentry);
6123 +               h_parent = au_h_dptr(parent, bdst);
6124 +               if (h_parent)
6125 +                       goto out; /* success */
6126 +
6127 +               /* find top dir which is necessary to cpup */
6128 +               do {
6129 +                       d = parent;
6130 +                       dput(parent);
6131 +                       parent = dget_parent(d);
6132 +                       di_read_lock_parent3(parent, !AuLock_IR);
6133 +                       h_parent = au_h_dptr(parent, bdst);
6134 +                       di_read_unlock(parent, !AuLock_IR);
6135 +               } while (!h_parent);
6136 +
6137 +               if (d != real_parent)
6138 +                       di_write_lock_child3(d);
6139 +
6140 +               /* somebody else might create while we were sleeping */
6141 +               h_dentry = au_h_dptr(d, bdst);
6142 +               if (!h_dentry || d_is_negative(h_dentry)) {
6143 +                       if (h_dentry)
6144 +                               au_update_dbtop(d);
6145 +
6146 +                       au_pin_set_dentry(&pin, d);
6147 +                       err = au_do_pin(&pin);
6148 +                       if (!err) {
6149 +                               err = cp(d, bdst, &pin, h_parent, arg);
6150 +                               au_unpin(&pin);
6151 +                       }
6152 +               }
6153 +
6154 +               if (d != real_parent)
6155 +                       di_write_unlock(d);
6156 +               if (unlikely(err))
6157 +                       break;
6158 +       }
6159 +
6160 +out:
6161 +       dput(parent);
6162 +       return err;
6163 +}
6164 +
6165 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6166 +                      struct au_pin *pin,
6167 +                      struct dentry *h_parent __maybe_unused,
6168 +                      void *arg __maybe_unused)
6169 +{
6170 +       struct au_cp_generic cpg = {
6171 +               .dentry = dentry,
6172 +               .bdst   = bdst,
6173 +               .bsrc   = -1,
6174 +               .len    = 0,
6175 +               .pin    = pin,
6176 +               .flags  = AuCpup_DTIME
6177 +       };
6178 +       return au_sio_cpup_simple(&cpg);
6179 +}
6180 +
6181 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6182 +{
6183 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6184 +}
6185 +
6186 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6187 +{
6188 +       int err;
6189 +       struct dentry *parent;
6190 +       struct inode *dir;
6191 +
6192 +       parent = dget_parent(dentry);
6193 +       dir = d_inode(parent);
6194 +       err = 0;
6195 +       if (au_h_iptr(dir, bdst))
6196 +               goto out;
6197 +
6198 +       di_read_unlock(parent, AuLock_IR);
6199 +       di_write_lock_parent(parent);
6200 +       /* someone else might change our inode while we were sleeping */
6201 +       if (!au_h_iptr(dir, bdst))
6202 +               err = au_cpup_dirs(dentry, bdst);
6203 +       di_downgrade_lock(parent, AuLock_IR);
6204 +
6205 +out:
6206 +       dput(parent);
6207 +       return err;
6208 +}
6209 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6210 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6211 +++ linux/fs/aufs/cpup.h        2022-11-05 23:02:18.962555950 +0100
6212 @@ -0,0 +1,100 @@
6213 +/* SPDX-License-Identifier: GPL-2.0 */
6214 +/*
6215 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6216 + *
6217 + * This program is free software; you can redistribute it and/or modify
6218 + * it under the terms of the GNU General Public License as published by
6219 + * the Free Software Foundation; either version 2 of the License, or
6220 + * (at your option) any later version.
6221 + *
6222 + * This program is distributed in the hope that it will be useful,
6223 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6224 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6225 + * GNU General Public License for more details.
6226 + *
6227 + * You should have received a copy of the GNU General Public License
6228 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6229 + */
6230 +
6231 +/*
6232 + * copy-up/down functions
6233 + */
6234 +
6235 +#ifndef __AUFS_CPUP_H__
6236 +#define __AUFS_CPUP_H__
6237 +
6238 +#ifdef __KERNEL__
6239 +
6240 +#include <linux/path.h>
6241 +
6242 +struct inode;
6243 +struct file;
6244 +struct au_pin;
6245 +
6246 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6247 +void au_cpup_attr_timesizes(struct inode *inode);
6248 +void au_cpup_attr_nlink(struct inode *inode, int force);
6249 +void au_cpup_attr_changeable(struct inode *inode);
6250 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6251 +void au_cpup_attr_all(struct inode *inode, int force);
6252 +
6253 +/* ---------------------------------------------------------------------- */
6254 +
6255 +struct au_cp_generic {
6256 +       struct dentry   *dentry;
6257 +       aufs_bindex_t   bdst, bsrc;
6258 +       loff_t          len;
6259 +       struct au_pin   *pin;
6260 +       unsigned int    flags;
6261 +};
6262 +
6263 +/* cpup flags */
6264 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6265 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6266 +                                                  for link(2) */
6267 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6268 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6269 +                                                  cpup */
6270 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6271 +                                                  existing entry */
6272 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6273 +                                                  the branch is marked as RO */
6274 +
6275 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6276 +#undef AuCpup_HOPEN
6277 +#define AuCpup_HOPEN           0
6278 +#endif
6279 +
6280 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6281 +#define au_fset_cpup(flags, name) \
6282 +       do { (flags) |= AuCpup_##name; } while (0)
6283 +#define au_fclr_cpup(flags, name) \
6284 +       do { (flags) &= ~AuCpup_##name; } while (0)
6285 +
6286 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6287 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6288 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6289 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6290 +
6291 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6292 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6293 +                        struct au_pin *pin,
6294 +                        struct dentry *h_parent, void *arg),
6295 +              void *arg);
6296 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6297 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6298 +
6299 +/* ---------------------------------------------------------------------- */
6300 +
6301 +/* keep timestamps when copyup */
6302 +struct au_dtime {
6303 +       struct dentry *dt_dentry;
6304 +       struct path dt_h_path;
6305 +       struct timespec64 dt_atime, dt_mtime;
6306 +};
6307 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6308 +                   struct path *h_path);
6309 +void au_dtime_revert(struct au_dtime *dt);
6310 +
6311 +#endif /* __KERNEL__ */
6312 +#endif /* __AUFS_CPUP_H__ */
6313 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6314 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6315 +++ linux/fs/aufs/dbgaufs.c     2022-11-05 23:02:18.962555950 +0100
6316 @@ -0,0 +1,526 @@
6317 +// SPDX-License-Identifier: GPL-2.0
6318 +/*
6319 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6320 + *
6321 + * This program is free software; you can redistribute it and/or modify
6322 + * it under the terms of the GNU General Public License as published by
6323 + * the Free Software Foundation; either version 2 of the License, or
6324 + * (at your option) any later version.
6325 + *
6326 + * This program is distributed in the hope that it will be useful,
6327 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6328 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6329 + * GNU General Public License for more details.
6330 + *
6331 + * You should have received a copy of the GNU General Public License
6332 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6333 + */
6334 +
6335 +/*
6336 + * debugfs interface
6337 + */
6338 +
6339 +#include <linux/debugfs.h>
6340 +#include "aufs.h"
6341 +
6342 +#ifndef CONFIG_SYSFS
6343 +#error DEBUG_FS depends upon SYSFS
6344 +#endif
6345 +
6346 +static struct dentry *dbgaufs;
6347 +static const mode_t dbgaufs_mode = 0444;
6348 +
6349 +/* 20 is max digits length of ulong 64 */
6350 +struct dbgaufs_arg {
6351 +       int n;
6352 +       char a[20 * 4];
6353 +};
6354 +
6355 +/*
6356 + * common function for all XINO files
6357 + */
6358 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6359 +                             struct file *file)
6360 +{
6361 +       void *p;
6362 +
6363 +       p = file->private_data;
6364 +       if (p) {
6365 +               /* this is struct dbgaufs_arg */
6366 +               AuDebugOn(!au_kfree_sz_test(p));
6367 +               au_kfree_do_rcu(p);
6368 +       }
6369 +       return 0;
6370 +}
6371 +
6372 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6373 +                          int cnt)
6374 +{
6375 +       int err;
6376 +       struct kstat st;
6377 +       struct dbgaufs_arg *p;
6378 +
6379 +       err = -ENOMEM;
6380 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6381 +       if (unlikely(!p))
6382 +               goto out;
6383 +
6384 +       err = 0;
6385 +       p->n = 0;
6386 +       file->private_data = p;
6387 +       if (!xf)
6388 +               goto out;
6389 +
6390 +       err = vfsub_getattr(&xf->f_path, &st);
6391 +       if (!err) {
6392 +               if (do_fcnt)
6393 +                       p->n = snprintf
6394 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6395 +                                cnt, st.blocks, st.blksize,
6396 +                                (long long)st.size);
6397 +               else
6398 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6399 +                                       st.blocks, st.blksize,
6400 +                                       (long long)st.size);
6401 +               AuDebugOn(p->n >= sizeof(p->a));
6402 +       } else {
6403 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6404 +               err = 0;
6405 +       }
6406 +
6407 +out:
6408 +       return err;
6409 +}
6410 +
6411 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6412 +                              size_t count, loff_t *ppos)
6413 +{
6414 +       struct dbgaufs_arg *p;
6415 +
6416 +       p = file->private_data;
6417 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6418 +}
6419 +
6420 +/* ---------------------------------------------------------------------- */
6421 +
6422 +struct dbgaufs_plink_arg {
6423 +       int n;
6424 +       char a[];
6425 +};
6426 +
6427 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6428 +                                struct file *file)
6429 +{
6430 +       free_page((unsigned long)file->private_data);
6431 +       return 0;
6432 +}
6433 +
6434 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6435 +{
6436 +       int err, i, limit;
6437 +       unsigned long n, sum;
6438 +       struct dbgaufs_plink_arg *p;
6439 +       struct au_sbinfo *sbinfo;
6440 +       struct super_block *sb;
6441 +       struct hlist_bl_head *hbl;
6442 +
6443 +       err = -ENOMEM;
6444 +       p = (void *)get_zeroed_page(GFP_NOFS);
6445 +       if (unlikely(!p))
6446 +               goto out;
6447 +
6448 +       err = -EFBIG;
6449 +       sbinfo = inode->i_private;
6450 +       sb = sbinfo->si_sb;
6451 +       si_noflush_read_lock(sb);
6452 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6453 +               limit = PAGE_SIZE - sizeof(p->n);
6454 +
6455 +               /* the number of buckets */
6456 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6457 +               p->n += n;
6458 +               limit -= n;
6459 +
6460 +               sum = 0;
6461 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6462 +                    i++, hbl++) {
6463 +                       n = au_hbl_count(hbl);
6464 +                       sum += n;
6465 +
6466 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6467 +                       p->n += n;
6468 +                       limit -= n;
6469 +                       if (unlikely(limit <= 0))
6470 +                               goto out_free;
6471 +               }
6472 +               p->a[p->n - 1] = '\n';
6473 +
6474 +               /* the sum of plinks */
6475 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6476 +               p->n += n;
6477 +               limit -= n;
6478 +               if (unlikely(limit <= 0))
6479 +                       goto out_free;
6480 +       } else {
6481 +#define str "1\n0\n0\n"
6482 +               p->n = sizeof(str) - 1;
6483 +               strcpy(p->a, str);
6484 +#undef str
6485 +       }
6486 +       si_read_unlock(sb);
6487 +
6488 +       err = 0;
6489 +       file->private_data = p;
6490 +       goto out; /* success */
6491 +
6492 +out_free:
6493 +       free_page((unsigned long)p);
6494 +out:
6495 +       return err;
6496 +}
6497 +
6498 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6499 +                                 size_t count, loff_t *ppos)
6500 +{
6501 +       struct dbgaufs_plink_arg *p;
6502 +
6503 +       p = file->private_data;
6504 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6505 +}
6506 +
6507 +static const struct file_operations dbgaufs_plink_fop = {
6508 +       .owner          = THIS_MODULE,
6509 +       .open           = dbgaufs_plink_open,
6510 +       .release        = dbgaufs_plink_release,
6511 +       .read           = dbgaufs_plink_read
6512 +};
6513 +
6514 +/* ---------------------------------------------------------------------- */
6515 +
6516 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6517 +{
6518 +       int err;
6519 +       struct au_sbinfo *sbinfo;
6520 +       struct super_block *sb;
6521 +
6522 +       sbinfo = inode->i_private;
6523 +       sb = sbinfo->si_sb;
6524 +       si_noflush_read_lock(sb);
6525 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6526 +       si_read_unlock(sb);
6527 +       return err;
6528 +}
6529 +
6530 +static const struct file_operations dbgaufs_xib_fop = {
6531 +       .owner          = THIS_MODULE,
6532 +       .open           = dbgaufs_xib_open,
6533 +       .release        = dbgaufs_xi_release,
6534 +       .read           = dbgaufs_xi_read
6535 +};
6536 +
6537 +/* ---------------------------------------------------------------------- */
6538 +
6539 +#define DbgaufsXi_PREFIX "xi"
6540 +
6541 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6542 +{
6543 +       int err, idx;
6544 +       long l;
6545 +       aufs_bindex_t bindex;
6546 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6547 +       struct au_sbinfo *sbinfo;
6548 +       struct super_block *sb;
6549 +       struct au_xino *xi;
6550 +       struct file *xf;
6551 +       struct qstr *name;
6552 +       struct au_branch *br;
6553 +
6554 +       err = -ENOENT;
6555 +       name = &file->f_path.dentry->d_name;
6556 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6557 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6558 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6559 +               goto out;
6560 +
6561 +       AuDebugOn(name->len >= sizeof(a));
6562 +       memcpy(a, name->name, name->len);
6563 +       a[name->len] = '\0';
6564 +       p = strchr(a, '-');
6565 +       if (p)
6566 +               *p = '\0';
6567 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6568 +       if (unlikely(err))
6569 +               goto out;
6570 +       bindex = l;
6571 +       idx = 0;
6572 +       if (p) {
6573 +               err = kstrtol(p + 1, 10, &l);
6574 +               if (unlikely(err))
6575 +                       goto out;
6576 +               idx = l;
6577 +       }
6578 +
6579 +       err = -ENOENT;
6580 +       sbinfo = inode->i_private;
6581 +       sb = sbinfo->si_sb;
6582 +       si_noflush_read_lock(sb);
6583 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6584 +               goto out_si;
6585 +       br = au_sbr(sb, bindex);
6586 +       xi = br->br_xino;
6587 +       if (unlikely(idx >= xi->xi_nfile))
6588 +               goto out_si;
6589 +       xf = au_xino_file(xi, idx);
6590 +       if (xf)
6591 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6592 +                                     au_xino_count(br));
6593 +
6594 +out_si:
6595 +       si_read_unlock(sb);
6596 +out:
6597 +       AuTraceErr(err);
6598 +       return err;
6599 +}
6600 +
6601 +static const struct file_operations dbgaufs_xino_fop = {
6602 +       .owner          = THIS_MODULE,
6603 +       .open           = dbgaufs_xino_open,
6604 +       .release        = dbgaufs_xi_release,
6605 +       .read           = dbgaufs_xi_read
6606 +};
6607 +
6608 +void dbgaufs_xino_del(struct au_branch *br)
6609 +{
6610 +       struct dentry *dbgaufs;
6611 +
6612 +       dbgaufs = br->br_dbgaufs;
6613 +       if (!dbgaufs)
6614 +               return;
6615 +
6616 +       br->br_dbgaufs = NULL;
6617 +       /* debugfs acquires the parent i_mutex */
6618 +       lockdep_off();
6619 +       debugfs_remove(dbgaufs);
6620 +       lockdep_on();
6621 +}
6622 +
6623 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6624 +{
6625 +       aufs_bindex_t bbot;
6626 +       struct au_branch *br;
6627 +
6628 +       if (!au_sbi(sb)->si_dbgaufs)
6629 +               return;
6630 +
6631 +       bbot = au_sbbot(sb);
6632 +       for (; bindex <= bbot; bindex++) {
6633 +               br = au_sbr(sb, bindex);
6634 +               dbgaufs_xino_del(br);
6635 +       }
6636 +}
6637 +
6638 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6639 +                             unsigned int idx, struct dentry *parent,
6640 +                             struct au_sbinfo *sbinfo)
6641 +{
6642 +       struct au_branch *br;
6643 +       struct dentry *d;
6644 +       /* "xi" bindex(5) "-" idx(2) NULL */
6645 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6646 +
6647 +       if (!idx)
6648 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6649 +       else
6650 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6651 +                        bindex, idx);
6652 +       br = au_sbr(sb, bindex);
6653 +       if (br->br_dbgaufs) {
6654 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6655 +
6656 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6657 +                       /* debugfs acquires the parent i_mutex */
6658 +                       lockdep_off();
6659 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6660 +                                          name);
6661 +                       lockdep_on();
6662 +                       if (unlikely(!d))
6663 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6664 +                                       parent, name);
6665 +               }
6666 +       } else {
6667 +               lockdep_off();
6668 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6669 +                                                    sbinfo, &dbgaufs_xino_fop);
6670 +               lockdep_on();
6671 +               if (unlikely(!br->br_dbgaufs))
6672 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6673 +                               parent, name);
6674 +       }
6675 +}
6676 +
6677 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6678 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6679 +{
6680 +       struct au_branch *br;
6681 +       struct au_xino *xi;
6682 +       unsigned int u;
6683 +
6684 +       br = au_sbr(sb, bindex);
6685 +       xi = br->br_xino;
6686 +       for (u = 0; u < xi->xi_nfile; u++)
6687 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6688 +}
6689 +
6690 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6691 +{
6692 +       struct au_sbinfo *sbinfo;
6693 +       struct dentry *parent;
6694 +       aufs_bindex_t bbot;
6695 +
6696 +       if (!au_opt_test(au_mntflags(sb), XINO))
6697 +               return;
6698 +
6699 +       sbinfo = au_sbi(sb);
6700 +       parent = sbinfo->si_dbgaufs;
6701 +       if (!parent)
6702 +               return;
6703 +
6704 +       bbot = au_sbbot(sb);
6705 +       if (topdown)
6706 +               for (; bindex <= bbot; bindex++)
6707 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6708 +       else
6709 +               for (; bbot >= bindex; bbot--)
6710 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6711 +}
6712 +
6713 +/* ---------------------------------------------------------------------- */
6714 +
6715 +#ifdef CONFIG_AUFS_EXPORT
6716 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6717 +{
6718 +       int err;
6719 +       struct au_sbinfo *sbinfo;
6720 +       struct super_block *sb;
6721 +
6722 +       sbinfo = inode->i_private;
6723 +       sb = sbinfo->si_sb;
6724 +       si_noflush_read_lock(sb);
6725 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6726 +       si_read_unlock(sb);
6727 +       return err;
6728 +}
6729 +
6730 +static const struct file_operations dbgaufs_xigen_fop = {
6731 +       .owner          = THIS_MODULE,
6732 +       .open           = dbgaufs_xigen_open,
6733 +       .release        = dbgaufs_xi_release,
6734 +       .read           = dbgaufs_xi_read
6735 +};
6736 +
6737 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6738 +{
6739 +       int err;
6740 +
6741 +       /*
6742 +        * This function is a dynamic '__init' function actually,
6743 +        * so the tiny check for si_rwsem is unnecessary.
6744 +        */
6745 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6746 +
6747 +       err = -EIO;
6748 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6749 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6750 +                &dbgaufs_xigen_fop);
6751 +       if (sbinfo->si_dbgaufs_xigen)
6752 +               err = 0;
6753 +
6754 +       return err;
6755 +}
6756 +#else
6757 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6758 +{
6759 +       return 0;
6760 +}
6761 +#endif /* CONFIG_AUFS_EXPORT */
6762 +
6763 +/* ---------------------------------------------------------------------- */
6764 +
6765 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6766 +{
6767 +       /*
6768 +        * This function is a dynamic '__fin' function actually,
6769 +        * so the tiny check for si_rwsem is unnecessary.
6770 +        */
6771 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6772 +
6773 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6774 +       sbinfo->si_dbgaufs = NULL;
6775 +}
6776 +
6777 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6778 +{
6779 +       int err;
6780 +       char name[SysaufsSiNameLen];
6781 +
6782 +       /*
6783 +        * This function is a dynamic '__init' function actually,
6784 +        * so the tiny check for si_rwsem is unnecessary.
6785 +        */
6786 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6787 +
6788 +       err = -ENOENT;
6789 +       if (!dbgaufs) {
6790 +               AuErr1("/debug/aufs is uninitialized\n");
6791 +               goto out;
6792 +       }
6793 +
6794 +       err = -EIO;
6795 +       sysaufs_name(sbinfo, name);
6796 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6797 +       if (unlikely(!sbinfo->si_dbgaufs))
6798 +               goto out;
6799 +
6800 +       /* regardless plink/noplink option */
6801 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6802 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6803 +                &dbgaufs_plink_fop);
6804 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6805 +               goto out_dir;
6806 +
6807 +       /* regardless xino/noxino option */
6808 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6809 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6810 +                &dbgaufs_xib_fop);
6811 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6812 +               goto out_dir;
6813 +
6814 +       err = dbgaufs_xigen_init(sbinfo);
6815 +       if (!err)
6816 +               goto out; /* success */
6817 +
6818 +out_dir:
6819 +       dbgaufs_si_fin(sbinfo);
6820 +out:
6821 +       if (unlikely(err))
6822 +               pr_err("debugfs/aufs failed\n");
6823 +       return err;
6824 +}
6825 +
6826 +/* ---------------------------------------------------------------------- */
6827 +
6828 +void dbgaufs_fin(void)
6829 +{
6830 +       debugfs_remove(dbgaufs);
6831 +}
6832 +
6833 +int __init dbgaufs_init(void)
6834 +{
6835 +       int err;
6836 +
6837 +       err = -EIO;
6838 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6839 +       if (dbgaufs)
6840 +               err = 0;
6841 +       return err;
6842 +}
6843 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6844 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6845 +++ linux/fs/aufs/dbgaufs.h     2022-11-05 23:02:18.962555950 +0100
6846 @@ -0,0 +1,53 @@
6847 +/* SPDX-License-Identifier: GPL-2.0 */
6848 +/*
6849 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6850 + *
6851 + * This program is free software; you can redistribute it and/or modify
6852 + * it under the terms of the GNU General Public License as published by
6853 + * the Free Software Foundation; either version 2 of the License, or
6854 + * (at your option) any later version.
6855 + *
6856 + * This program is distributed in the hope that it will be useful,
6857 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6858 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6859 + * GNU General Public License for more details.
6860 + *
6861 + * You should have received a copy of the GNU General Public License
6862 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6863 + */
6864 +
6865 +/*
6866 + * debugfs interface
6867 + */
6868 +
6869 +#ifndef __DBGAUFS_H__
6870 +#define __DBGAUFS_H__
6871 +
6872 +#ifdef __KERNEL__
6873 +
6874 +struct super_block;
6875 +struct au_sbinfo;
6876 +struct au_branch;
6877 +
6878 +#ifdef CONFIG_DEBUG_FS
6879 +/* dbgaufs.c */
6880 +void dbgaufs_xino_del(struct au_branch *br);
6881 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6882 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6883 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6884 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6885 +void dbgaufs_fin(void);
6886 +int __init dbgaufs_init(void);
6887 +#else
6888 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6889 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6890 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6891 +          int topdown)
6892 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6893 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6894 +AuStubVoid(dbgaufs_fin, void)
6895 +AuStubInt0(__init dbgaufs_init, void)
6896 +#endif /* CONFIG_DEBUG_FS */
6897 +
6898 +#endif /* __KERNEL__ */
6899 +#endif /* __DBGAUFS_H__ */
6900 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6901 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6902 +++ linux/fs/aufs/dcsub.c       2022-11-05 23:02:18.962555950 +0100
6903 @@ -0,0 +1,225 @@
6904 +// SPDX-License-Identifier: GPL-2.0
6905 +/*
6906 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6907 + *
6908 + * This program is free software; you can redistribute it and/or modify
6909 + * it under the terms of the GNU General Public License as published by
6910 + * the Free Software Foundation; either version 2 of the License, or
6911 + * (at your option) any later version.
6912 + *
6913 + * This program is distributed in the hope that it will be useful,
6914 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6915 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6916 + * GNU General Public License for more details.
6917 + *
6918 + * You should have received a copy of the GNU General Public License
6919 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6920 + */
6921 +
6922 +/*
6923 + * sub-routines for dentry cache
6924 + */
6925 +
6926 +#include "aufs.h"
6927 +
6928 +static void au_dpage_free(struct au_dpage *dpage)
6929 +{
6930 +       int i;
6931 +       struct dentry **p;
6932 +
6933 +       p = dpage->dentries;
6934 +       for (i = 0; i < dpage->ndentry; i++)
6935 +               dput(*p++);
6936 +       free_page((unsigned long)dpage->dentries);
6937 +}
6938 +
6939 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6940 +{
6941 +       int err;
6942 +       void *p;
6943 +
6944 +       err = -ENOMEM;
6945 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6946 +       if (unlikely(!dpages->dpages))
6947 +               goto out;
6948 +
6949 +       p = (void *)__get_free_page(gfp);
6950 +       if (unlikely(!p))
6951 +               goto out_dpages;
6952 +
6953 +       dpages->dpages[0].ndentry = 0;
6954 +       dpages->dpages[0].dentries = p;
6955 +       dpages->ndpage = 1;
6956 +       return 0; /* success */
6957 +
6958 +out_dpages:
6959 +       au_kfree_try_rcu(dpages->dpages);
6960 +out:
6961 +       return err;
6962 +}
6963 +
6964 +void au_dpages_free(struct au_dcsub_pages *dpages)
6965 +{
6966 +       int i;
6967 +       struct au_dpage *p;
6968 +
6969 +       p = dpages->dpages;
6970 +       for (i = 0; i < dpages->ndpage; i++)
6971 +               au_dpage_free(p++);
6972 +       au_kfree_try_rcu(dpages->dpages);
6973 +}
6974 +
6975 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6976 +                           struct dentry *dentry, gfp_t gfp)
6977 +{
6978 +       int err, sz;
6979 +       struct au_dpage *dpage;
6980 +       void *p;
6981 +
6982 +       dpage = dpages->dpages + dpages->ndpage - 1;
6983 +       sz = PAGE_SIZE / sizeof(dentry);
6984 +       if (unlikely(dpage->ndentry >= sz)) {
6985 +               AuLabel(new dpage);
6986 +               err = -ENOMEM;
6987 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6988 +               p = au_kzrealloc(dpages->dpages, sz,
6989 +                                sz + sizeof(*dpages->dpages), gfp,
6990 +                                /*may_shrink*/0);
6991 +               if (unlikely(!p))
6992 +                       goto out;
6993 +
6994 +               dpages->dpages = p;
6995 +               dpage = dpages->dpages + dpages->ndpage;
6996 +               p = (void *)__get_free_page(gfp);
6997 +               if (unlikely(!p))
6998 +                       goto out;
6999 +
7000 +               dpage->ndentry = 0;
7001 +               dpage->dentries = p;
7002 +               dpages->ndpage++;
7003 +       }
7004 +
7005 +       AuDebugOn(au_dcount(dentry) <= 0);
7006 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7007 +       return 0; /* success */
7008 +
7009 +out:
7010 +       return err;
7011 +}
7012 +
7013 +/* todo: BAD approach */
7014 +/* copied from linux/fs/dcache.c */
7015 +enum d_walk_ret {
7016 +       D_WALK_CONTINUE,
7017 +       D_WALK_QUIT,
7018 +       D_WALK_NORETRY,
7019 +       D_WALK_SKIP,
7020 +};
7021 +
7022 +extern void d_walk(struct dentry *parent, void *data,
7023 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7024 +
7025 +struct ac_dpages_arg {
7026 +       int err;
7027 +       struct au_dcsub_pages *dpages;
7028 +       struct super_block *sb;
7029 +       au_dpages_test test;
7030 +       void *arg;
7031 +};
7032 +
7033 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7034 +{
7035 +       enum d_walk_ret ret;
7036 +       struct ac_dpages_arg *arg = _arg;
7037 +
7038 +       ret = D_WALK_CONTINUE;
7039 +       if (dentry->d_sb == arg->sb
7040 +           && !IS_ROOT(dentry)
7041 +           && au_dcount(dentry) > 0
7042 +           && au_di(dentry)
7043 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7044 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7045 +               if (unlikely(arg->err))
7046 +                       ret = D_WALK_QUIT;
7047 +       }
7048 +
7049 +       return ret;
7050 +}
7051 +
7052 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7053 +                  au_dpages_test test, void *arg)
7054 +{
7055 +       struct ac_dpages_arg args = {
7056 +               .err    = 0,
7057 +               .dpages = dpages,
7058 +               .sb     = root->d_sb,
7059 +               .test   = test,
7060 +               .arg    = arg
7061 +       };
7062 +
7063 +       d_walk(root, &args, au_call_dpages_append);
7064 +
7065 +       return args.err;
7066 +}
7067 +
7068 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7069 +                      int do_include, au_dpages_test test, void *arg)
7070 +{
7071 +       int err;
7072 +
7073 +       err = 0;
7074 +       write_seqlock(&rename_lock);
7075 +       spin_lock(&dentry->d_lock);
7076 +       if (do_include
7077 +           && au_dcount(dentry) > 0
7078 +           && (!test || test(dentry, arg)))
7079 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7080 +       spin_unlock(&dentry->d_lock);
7081 +       if (unlikely(err))
7082 +               goto out;
7083 +
7084 +       /*
7085 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7086 +        * mount
7087 +        */
7088 +       while (!IS_ROOT(dentry)) {
7089 +               dentry = dentry->d_parent; /* rename_lock is locked */
7090 +               spin_lock(&dentry->d_lock);
7091 +               if (au_dcount(dentry) > 0
7092 +                   && (!test || test(dentry, arg)))
7093 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7094 +               spin_unlock(&dentry->d_lock);
7095 +               if (unlikely(err))
7096 +                       break;
7097 +       }
7098 +
7099 +out:
7100 +       write_sequnlock(&rename_lock);
7101 +       return err;
7102 +}
7103 +
7104 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7105 +{
7106 +       return au_di(dentry) && dentry->d_sb == arg;
7107 +}
7108 +
7109 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7110 +                           struct dentry *dentry, int do_include)
7111 +{
7112 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7113 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7114 +}
7115 +
7116 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7117 +{
7118 +       struct path path[2] = {
7119 +               {
7120 +                       .dentry = d1
7121 +               },
7122 +               {
7123 +                       .dentry = d2
7124 +               }
7125 +       };
7126 +
7127 +       return path_is_under(path + 0, path + 1);
7128 +}
7129 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7130 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7131 +++ linux/fs/aufs/dcsub.h       2022-11-05 23:02:18.962555950 +0100
7132 @@ -0,0 +1,137 @@
7133 +/* SPDX-License-Identifier: GPL-2.0 */
7134 +/*
7135 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7136 + *
7137 + * This program is free software; you can redistribute it and/or modify
7138 + * it under the terms of the GNU General Public License as published by
7139 + * the Free Software Foundation; either version 2 of the License, or
7140 + * (at your option) any later version.
7141 + *
7142 + * This program is distributed in the hope that it will be useful,
7143 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7144 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7145 + * GNU General Public License for more details.
7146 + *
7147 + * You should have received a copy of the GNU General Public License
7148 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7149 + */
7150 +
7151 +/*
7152 + * sub-routines for dentry cache
7153 + */
7154 +
7155 +#ifndef __AUFS_DCSUB_H__
7156 +#define __AUFS_DCSUB_H__
7157 +
7158 +#ifdef __KERNEL__
7159 +
7160 +#include <linux/dcache.h>
7161 +#include <linux/fs.h>
7162 +
7163 +struct au_dpage {
7164 +       int ndentry;
7165 +       struct dentry **dentries;
7166 +};
7167 +
7168 +struct au_dcsub_pages {
7169 +       int ndpage;
7170 +       struct au_dpage *dpages;
7171 +};
7172 +
7173 +/* ---------------------------------------------------------------------- */
7174 +
7175 +/* dcsub.c */
7176 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7177 +void au_dpages_free(struct au_dcsub_pages *dpages);
7178 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7179 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7180 +                  au_dpages_test test, void *arg);
7181 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7182 +                      int do_include, au_dpages_test test, void *arg);
7183 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7184 +                           struct dentry *dentry, int do_include);
7185 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7186 +
7187 +/* ---------------------------------------------------------------------- */
7188 +
7189 +/*
7190 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7191 + * include/linux/dcache.h. Try them (in the future).
7192 + */
7193 +
7194 +static inline int au_d_hashed_positive(struct dentry *d)
7195 +{
7196 +       int err;
7197 +       struct inode *inode = d_inode(d);
7198 +
7199 +       err = 0;
7200 +       if (unlikely(d_unhashed(d)
7201 +                    || d_is_negative(d)
7202 +                    || !inode->i_nlink))
7203 +               err = -ENOENT;
7204 +       return err;
7205 +}
7206 +
7207 +static inline int au_d_linkable(struct dentry *d)
7208 +{
7209 +       int err;
7210 +       struct inode *inode = d_inode(d);
7211 +
7212 +       err = au_d_hashed_positive(d);
7213 +       if (err
7214 +           && d_is_positive(d)
7215 +           && (inode->i_state & I_LINKABLE))
7216 +               err = 0;
7217 +       return err;
7218 +}
7219 +
7220 +static inline int au_d_alive(struct dentry *d)
7221 +{
7222 +       int err;
7223 +       struct inode *inode;
7224 +
7225 +       err = 0;
7226 +       if (!IS_ROOT(d))
7227 +               err = au_d_hashed_positive(d);
7228 +       else {
7229 +               inode = d_inode(d);
7230 +               if (unlikely(d_unlinked(d)
7231 +                            || d_is_negative(d)
7232 +                            || !inode->i_nlink))
7233 +                       err = -ENOENT;
7234 +       }
7235 +       return err;
7236 +}
7237 +
7238 +static inline int au_alive_dir(struct dentry *d)
7239 +{
7240 +       int err;
7241 +
7242 +       err = au_d_alive(d);
7243 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7244 +               err = -ENOENT;
7245 +       return err;
7246 +}
7247 +
7248 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7249 +{
7250 +       return a->len == b->len
7251 +               && !memcmp(a->name, b->name, a->len);
7252 +}
7253 +
7254 +/*
7255 + * by the commit
7256 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7257 + *                     taking d_lock
7258 + * the type of d_lockref.count became int, but the inlined function d_count()
7259 + * still returns unsigned int.
7260 + * I don't know why. Maybe it is for every d_count() users?
7261 + * Anyway au_dcount() lives on.
7262 + */
7263 +static inline int au_dcount(struct dentry *d)
7264 +{
7265 +       return (int)d_count(d);
7266 +}
7267 +
7268 +#endif /* __KERNEL__ */
7269 +#endif /* __AUFS_DCSUB_H__ */
7270 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7271 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7272 +++ linux/fs/aufs/debug.c       2023-02-20 21:05:51.959693785 +0100
7273 @@ -0,0 +1,446 @@
7274 +// SPDX-License-Identifier: GPL-2.0
7275 +/*
7276 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7277 + *
7278 + * This program is free software; you can redistribute it and/or modify
7279 + * it under the terms of the GNU General Public License as published by
7280 + * the Free Software Foundation; either version 2 of the License, or
7281 + * (at your option) any later version.
7282 + *
7283 + * This program is distributed in the hope that it will be useful,
7284 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7285 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7286 + * GNU General Public License for more details.
7287 + *
7288 + * You should have received a copy of the GNU General Public License
7289 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7290 + */
7291 +
7292 +/*
7293 + * debug print functions
7294 + */
7295 +
7296 +#include <linux/iversion.h>
7297 +#include "aufs.h"
7298 +
7299 +/* Returns 0, or -errno.  arg is in kp->arg. */
7300 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7301 +{
7302 +       int err, n;
7303 +
7304 +       err = kstrtoint(val, 0, &n);
7305 +       if (!err) {
7306 +               if (n > 0)
7307 +                       au_debug_on();
7308 +               else
7309 +                       au_debug_off();
7310 +       }
7311 +       return err;
7312 +}
7313 +
7314 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7315 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7316 +{
7317 +       atomic_t *a;
7318 +
7319 +       a = kp->arg;
7320 +       return sprintf(buffer, "%d", atomic_read(a));
7321 +}
7322 +
7323 +static const struct kernel_param_ops param_ops_atomic_t = {
7324 +       .set = param_atomic_t_set,
7325 +       .get = param_atomic_t_get
7326 +       /* void (*free)(void *arg) */
7327 +};
7328 +
7329 +atomic_t aufs_debug = ATOMIC_INIT(0);
7330 +MODULE_PARM_DESC(debug, "debug print");
7331 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7332 +
7333 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7334 +char *au_plevel = KERN_DEBUG;
7335 +#define dpri(fmt, ...) do {                                    \
7336 +       if ((au_plevel                                          \
7337 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7338 +           || au_debug_test())                                 \
7339 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7340 +} while (0)
7341 +
7342 +/* ---------------------------------------------------------------------- */
7343 +
7344 +void au_dpri_whlist(struct au_nhash *whlist)
7345 +{
7346 +       unsigned long ul, n;
7347 +       struct hlist_head *head;
7348 +       struct au_vdir_wh *pos;
7349 +
7350 +       n = whlist->nh_num;
7351 +       head = whlist->nh_head;
7352 +       for (ul = 0; ul < n; ul++) {
7353 +               hlist_for_each_entry(pos, head, wh_hash)
7354 +                       dpri("b%d, %.*s, %d\n",
7355 +                            pos->wh_bindex,
7356 +                            pos->wh_str.len, pos->wh_str.name,
7357 +                            pos->wh_str.len);
7358 +               head++;
7359 +       }
7360 +}
7361 +
7362 +void au_dpri_vdir(struct au_vdir *vdir)
7363 +{
7364 +       unsigned long ul;
7365 +       union au_vdir_deblk_p p;
7366 +       unsigned char *o;
7367 +
7368 +       if (!vdir || IS_ERR(vdir)) {
7369 +               dpri("err %ld\n", PTR_ERR(vdir));
7370 +               return;
7371 +       }
7372 +
7373 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7374 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7375 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7376 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7377 +               p.deblk = vdir->vd_deblk[ul];
7378 +               o = p.deblk;
7379 +               dpri("[%lu]: %p\n", ul, o);
7380 +       }
7381 +}
7382 +
7383 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7384 +                       struct dentry *wh)
7385 +{
7386 +       char *n = NULL;
7387 +       int l = 0;
7388 +
7389 +       if (!inode || IS_ERR(inode)) {
7390 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7391 +               return -1;
7392 +       }
7393 +
7394 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7395 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7396 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7397 +       if (wh) {
7398 +               n = (void *)wh->d_name.name;
7399 +               l = wh->d_name.len;
7400 +       }
7401 +
7402 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7403 +            " acl %p, def_acl %p,"
7404 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7405 +            bindex, inode,
7406 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7407 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7408 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7409 +            inode->i_acl, inode->i_default_acl,
7410 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7411 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7412 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7413 +            inode->i_generation,
7414 +            l ? ", wh " : "", l, n);
7415 +       return 0;
7416 +}
7417 +
7418 +void au_dpri_inode(struct inode *inode)
7419 +{
7420 +       struct au_iinfo *iinfo;
7421 +       struct au_hinode *hi;
7422 +       aufs_bindex_t bindex;
7423 +       int err, hn;
7424 +
7425 +       err = do_pri_inode(-1, inode, -1, NULL);
7426 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7427 +               return;
7428 +
7429 +       iinfo = au_ii(inode);
7430 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7431 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7432 +       if (iinfo->ii_btop < 0)
7433 +               return;
7434 +       hn = 0;
7435 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7436 +               hi = au_hinode(iinfo, bindex);
7437 +               hn = !!au_hn(hi);
7438 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7439 +       }
7440 +}
7441 +
7442 +void au_dpri_dalias(struct inode *inode)
7443 +{
7444 +       struct dentry *d;
7445 +
7446 +       spin_lock(&inode->i_lock);
7447 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7448 +               au_dpri_dentry(d);
7449 +       spin_unlock(&inode->i_lock);
7450 +}
7451 +
7452 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7453 +{
7454 +       struct dentry *wh = NULL;
7455 +       int hn;
7456 +       struct inode *inode;
7457 +       struct au_iinfo *iinfo;
7458 +       struct au_hinode *hi;
7459 +
7460 +       if (!dentry || IS_ERR(dentry)) {
7461 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7462 +               return -1;
7463 +       }
7464 +       /* do not call dget_parent() here */
7465 +       /* note: access d_xxx without d_lock */
7466 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7467 +            bindex, dentry, dentry,
7468 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7469 +            au_dcount(dentry), dentry->d_flags,
7470 +            d_unhashed(dentry) ? "un" : "");
7471 +       hn = -1;
7472 +       inode = NULL;
7473 +       if (d_is_positive(dentry))
7474 +               inode = d_inode(dentry);
7475 +       if (inode
7476 +           && au_test_aufs(dentry->d_sb)
7477 +           && bindex >= 0
7478 +           && !au_is_bad_inode(inode)) {
7479 +               iinfo = au_ii(inode);
7480 +               hi = au_hinode(iinfo, bindex);
7481 +               hn = !!au_hn(hi);
7482 +               wh = hi->hi_whdentry;
7483 +       }
7484 +       do_pri_inode(bindex, inode, hn, wh);
7485 +       return 0;
7486 +}
7487 +
7488 +void au_dpri_dentry(struct dentry *dentry)
7489 +{
7490 +       struct au_dinfo *dinfo;
7491 +       aufs_bindex_t bindex;
7492 +       int err;
7493 +
7494 +       err = do_pri_dentry(-1, dentry);
7495 +       if (err || !au_test_aufs(dentry->d_sb))
7496 +               return;
7497 +
7498 +       dinfo = au_di(dentry);
7499 +       if (!dinfo)
7500 +               return;
7501 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7502 +            dinfo->di_btop, dinfo->di_bbot,
7503 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7504 +            dinfo->di_tmpfile);
7505 +       if (dinfo->di_btop < 0)
7506 +               return;
7507 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7508 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7509 +}
7510 +
7511 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7512 +{
7513 +       char a[32];
7514 +
7515 +       if (!file || IS_ERR(file)) {
7516 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7517 +               return -1;
7518 +       }
7519 +       a[0] = 0;
7520 +       if (bindex < 0
7521 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7522 +           && au_test_aufs(file->f_path.dentry->d_sb)
7523 +           && au_fi(file))
7524 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7525 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7526 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7527 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7528 +            file->f_version, file->f_pos, a);
7529 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7530 +               do_pri_dentry(bindex, file->f_path.dentry);
7531 +       return 0;
7532 +}
7533 +
7534 +void au_dpri_file(struct file *file)
7535 +{
7536 +       struct au_finfo *finfo;
7537 +       struct au_fidir *fidir;
7538 +       struct au_hfile *hfile;
7539 +       aufs_bindex_t bindex;
7540 +       int err;
7541 +
7542 +       err = do_pri_file(-1, file);
7543 +       if (err
7544 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7545 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7546 +               return;
7547 +
7548 +       finfo = au_fi(file);
7549 +       if (!finfo)
7550 +               return;
7551 +       if (finfo->fi_btop < 0)
7552 +               return;
7553 +       fidir = finfo->fi_hdir;
7554 +       if (!fidir)
7555 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7556 +       else
7557 +               for (bindex = finfo->fi_btop;
7558 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7559 +                    bindex++) {
7560 +                       hfile = fidir->fd_hfile + bindex;
7561 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7562 +               }
7563 +}
7564 +
7565 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7566 +{
7567 +       struct vfsmount *mnt;
7568 +       struct super_block *sb;
7569 +
7570 +       if (!br || IS_ERR(br))
7571 +               goto out;
7572 +       mnt = au_br_mnt(br);
7573 +       if (!mnt || IS_ERR(mnt))
7574 +               goto out;
7575 +       sb = mnt->mnt_sb;
7576 +       if (!sb || IS_ERR(sb))
7577 +               goto out;
7578 +
7579 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7580 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7581 +            "xino %d\n",
7582 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7583 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7584 +            sb->s_flags, sb->s_count,
7585 +            atomic_read(&sb->s_active),
7586 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7587 +       return 0;
7588 +
7589 +out:
7590 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7591 +       return -1;
7592 +}
7593 +
7594 +void au_dpri_sb(struct super_block *sb)
7595 +{
7596 +       struct au_sbinfo *sbinfo;
7597 +       aufs_bindex_t bindex;
7598 +       int err;
7599 +       /* to reduce stack size */
7600 +       struct {
7601 +               struct vfsmount mnt;
7602 +               struct au_branch fake;
7603 +       } *a;
7604 +
7605 +       /* this function can be called from magic sysrq */
7606 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7607 +       if (unlikely(!a)) {
7608 +               dpri("no memory\n");
7609 +               return;
7610 +       }
7611 +
7612 +       a->mnt.mnt_sb = sb;
7613 +       a->fake.br_path.mnt = &a->mnt;
7614 +       err = do_pri_br(-1, &a->fake);
7615 +       au_kfree_rcu(a);
7616 +       dpri("dev 0x%x\n", sb->s_dev);
7617 +       if (err || !au_test_aufs(sb))
7618 +               return;
7619 +
7620 +       sbinfo = au_sbi(sb);
7621 +       if (!sbinfo)
7622 +               return;
7623 +       dpri("nw %d, gen %u, kobj %d\n",
7624 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7625 +            kref_read(&sbinfo->si_kobj.kref));
7626 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7627 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7628 +}
7629 +
7630 +/* ---------------------------------------------------------------------- */
7631 +
7632 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7633 +{
7634 +       struct inode *h_inode, *inode = d_inode(dentry);
7635 +       struct dentry *h_dentry;
7636 +       aufs_bindex_t bindex, bbot, bi;
7637 +
7638 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7639 +               return;
7640 +
7641 +       bbot = au_dbbot(dentry);
7642 +       bi = au_ibbot(inode);
7643 +       if (bi < bbot)
7644 +               bbot = bi;
7645 +       bindex = au_dbtop(dentry);
7646 +       bi = au_ibtop(inode);
7647 +       if (bi > bindex)
7648 +               bindex = bi;
7649 +
7650 +       for (; bindex <= bbot; bindex++) {
7651 +               h_dentry = au_h_dptr(dentry, bindex);
7652 +               if (!h_dentry)
7653 +                       continue;
7654 +               h_inode = au_h_iptr(inode, bindex);
7655 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7656 +                       au_debug_on();
7657 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7658 +                       AuDbgDentry(dentry);
7659 +                       AuDbgInode(inode);
7660 +                       au_debug_off();
7661 +                       if (au_test_fuse(h_inode->i_sb))
7662 +                               WARN_ON_ONCE(1);
7663 +                       else
7664 +                               BUG();
7665 +               }
7666 +       }
7667 +}
7668 +
7669 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7670 +{
7671 +       int err, i, j;
7672 +       struct au_dcsub_pages dpages;
7673 +       struct au_dpage *dpage;
7674 +       struct dentry **dentries;
7675 +
7676 +       err = au_dpages_init(&dpages, GFP_NOFS);
7677 +       AuDebugOn(err);
7678 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7679 +       AuDebugOn(err);
7680 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7681 +               dpage = dpages.dpages + i;
7682 +               dentries = dpage->dentries;
7683 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7684 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7685 +       }
7686 +       au_dpages_free(&dpages);
7687 +}
7688 +
7689 +void au_dbg_verify_kthread(void)
7690 +{
7691 +       if (au_wkq_test()) {
7692 +               au_dbg_blocked();
7693 +               /*
7694 +                * It may be recursive, but udba=notify between two aufs mounts,
7695 +                * where a single ro branch is shared, is not a problem.
7696 +                */
7697 +               /* WARN_ON(1); */
7698 +       }
7699 +}
7700 +
7701 +/* ---------------------------------------------------------------------- */
7702 +
7703 +int __init au_debug_init(void)
7704 +{
7705 +       aufs_bindex_t bindex;
7706 +       struct au_vdir_destr destr;
7707 +
7708 +       bindex = -1;
7709 +       AuDebugOn(bindex >= 0);
7710 +
7711 +       destr.len = -1;
7712 +       AuDebugOn(destr.len < NAME_MAX);
7713 +
7714 +#ifdef CONFIG_4KSTACKS
7715 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7716 +#endif
7717 +
7718 +       return 0;
7719 +}
7720 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7721 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7722 +++ linux/fs/aufs/debug.h       2022-11-05 23:02:18.962555950 +0100
7723 @@ -0,0 +1,226 @@
7724 +/* SPDX-License-Identifier: GPL-2.0 */
7725 +/*
7726 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7727 + *
7728 + * This program is free software; you can redistribute it and/or modify
7729 + * it under the terms of the GNU General Public License as published by
7730 + * the Free Software Foundation; either version 2 of the License, or
7731 + * (at your option) any later version.
7732 + *
7733 + * This program is distributed in the hope that it will be useful,
7734 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7735 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7736 + * GNU General Public License for more details.
7737 + *
7738 + * You should have received a copy of the GNU General Public License
7739 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7740 + */
7741 +
7742 +/*
7743 + * debug print functions
7744 + */
7745 +
7746 +#ifndef __AUFS_DEBUG_H__
7747 +#define __AUFS_DEBUG_H__
7748 +
7749 +#ifdef __KERNEL__
7750 +
7751 +#include <linux/atomic.h>
7752 +#include <linux/module.h>
7753 +#include <linux/kallsyms.h>
7754 +#include <linux/sysrq.h>
7755 +
7756 +#ifdef CONFIG_AUFS_DEBUG
7757 +#define AuDebugOn(a)           BUG_ON(a)
7758 +
7759 +/* module parameter */
7760 +extern atomic_t aufs_debug;
7761 +static inline void au_debug_on(void)
7762 +{
7763 +       atomic_inc(&aufs_debug);
7764 +}
7765 +static inline void au_debug_off(void)
7766 +{
7767 +       atomic_dec_if_positive(&aufs_debug);
7768 +}
7769 +
7770 +static inline int au_debug_test(void)
7771 +{
7772 +       return atomic_read(&aufs_debug) > 0;
7773 +}
7774 +#else
7775 +#define AuDebugOn(a)           do {} while (0)
7776 +AuStubVoid(au_debug_on, void)
7777 +AuStubVoid(au_debug_off, void)
7778 +AuStubInt0(au_debug_test, void)
7779 +#endif /* CONFIG_AUFS_DEBUG */
7780 +
7781 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7782 +
7783 +/* ---------------------------------------------------------------------- */
7784 +
7785 +/* debug print */
7786 +
7787 +#define AuDbg(fmt, ...) do { \
7788 +       if (au_debug_test()) \
7789 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7790 +} while (0)
7791 +#define AuLabel(l)             AuDbg(#l "\n")
7792 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7793 +#define AuWarn1(fmt, ...) do { \
7794 +       static unsigned char _c; \
7795 +       if (!_c++) \
7796 +               pr_warn(fmt, ##__VA_ARGS__); \
7797 +} while (0)
7798 +
7799 +#define AuErr1(fmt, ...) do { \
7800 +       static unsigned char _c; \
7801 +       if (!_c++) \
7802 +               pr_err(fmt, ##__VA_ARGS__); \
7803 +} while (0)
7804 +
7805 +#define AuIOErr1(fmt, ...) do { \
7806 +       static unsigned char _c; \
7807 +       if (!_c++) \
7808 +               AuIOErr(fmt, ##__VA_ARGS__); \
7809 +} while (0)
7810 +
7811 +#define AuUnsupportMsg "This operation is not supported." \
7812 +                       " Please report this application to aufs-users ML."
7813 +#define AuUnsupport(fmt, ...) do { \
7814 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7815 +       dump_stack(); \
7816 +} while (0)
7817 +
7818 +#define AuTraceErr(e) do { \
7819 +       if (unlikely((e) < 0)) \
7820 +               AuDbg("err %d\n", (int)(e)); \
7821 +} while (0)
7822 +
7823 +#define AuTraceErrPtr(p) do { \
7824 +       if (IS_ERR(p)) \
7825 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7826 +} while (0)
7827 +
7828 +/* dirty macros for debug print, use with "%.*s" and caution */
7829 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7830 +
7831 +/* ---------------------------------------------------------------------- */
7832 +
7833 +struct dentry;
7834 +#ifdef CONFIG_AUFS_DEBUG
7835 +extern struct mutex au_dbg_mtx;
7836 +extern char *au_plevel;
7837 +struct au_nhash;
7838 +void au_dpri_whlist(struct au_nhash *whlist);
7839 +struct au_vdir;
7840 +void au_dpri_vdir(struct au_vdir *vdir);
7841 +struct inode;
7842 +void au_dpri_inode(struct inode *inode);
7843 +void au_dpri_dalias(struct inode *inode);
7844 +void au_dpri_dentry(struct dentry *dentry);
7845 +struct file;
7846 +void au_dpri_file(struct file *filp);
7847 +struct super_block;
7848 +void au_dpri_sb(struct super_block *sb);
7849 +
7850 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7851 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7852 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7853 +void au_dbg_verify_kthread(void);
7854 +
7855 +int __init au_debug_init(void);
7856 +
7857 +#define AuDbgWhlist(w) do { \
7858 +       mutex_lock(&au_dbg_mtx); \
7859 +       AuDbg(#w "\n"); \
7860 +       au_dpri_whlist(w); \
7861 +       mutex_unlock(&au_dbg_mtx); \
7862 +} while (0)
7863 +
7864 +#define AuDbgVdir(v) do { \
7865 +       mutex_lock(&au_dbg_mtx); \
7866 +       AuDbg(#v "\n"); \
7867 +       au_dpri_vdir(v); \
7868 +       mutex_unlock(&au_dbg_mtx); \
7869 +} while (0)
7870 +
7871 +#define AuDbgInode(i) do { \
7872 +       mutex_lock(&au_dbg_mtx); \
7873 +       AuDbg(#i "\n"); \
7874 +       au_dpri_inode(i); \
7875 +       mutex_unlock(&au_dbg_mtx); \
7876 +} while (0)
7877 +
7878 +#define AuDbgDAlias(i) do { \
7879 +       mutex_lock(&au_dbg_mtx); \
7880 +       AuDbg(#i "\n"); \
7881 +       au_dpri_dalias(i); \
7882 +       mutex_unlock(&au_dbg_mtx); \
7883 +} while (0)
7884 +
7885 +#define AuDbgDentry(d) do { \
7886 +       mutex_lock(&au_dbg_mtx); \
7887 +       AuDbg(#d "\n"); \
7888 +       au_dpri_dentry(d); \
7889 +       mutex_unlock(&au_dbg_mtx); \
7890 +} while (0)
7891 +
7892 +#define AuDbgFile(f) do { \
7893 +       mutex_lock(&au_dbg_mtx); \
7894 +       AuDbg(#f "\n"); \
7895 +       au_dpri_file(f); \
7896 +       mutex_unlock(&au_dbg_mtx); \
7897 +} while (0)
7898 +
7899 +#define AuDbgSb(sb) do { \
7900 +       mutex_lock(&au_dbg_mtx); \
7901 +       AuDbg(#sb "\n"); \
7902 +       au_dpri_sb(sb); \
7903 +       mutex_unlock(&au_dbg_mtx); \
7904 +} while (0)
7905 +
7906 +#define AuDbgSym(addr) do {                            \
7907 +       char sym[KSYM_SYMBOL_LEN];                      \
7908 +       sprint_symbol(sym, (unsigned long)addr);        \
7909 +       AuDbg("%s\n", sym);                             \
7910 +} while (0)
7911 +#else
7912 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7913 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7914 +AuStubVoid(au_dbg_verify_kthread, void)
7915 +AuStubInt0(__init au_debug_init, void)
7916 +
7917 +#define AuDbgWhlist(w)         do {} while (0)
7918 +#define AuDbgVdir(v)           do {} while (0)
7919 +#define AuDbgInode(i)          do {} while (0)
7920 +#define AuDbgDAlias(i)         do {} while (0)
7921 +#define AuDbgDentry(d)         do {} while (0)
7922 +#define AuDbgFile(f)           do {} while (0)
7923 +#define AuDbgSb(sb)            do {} while (0)
7924 +#define AuDbgSym(addr)         do {} while (0)
7925 +#endif /* CONFIG_AUFS_DEBUG */
7926 +
7927 +/* ---------------------------------------------------------------------- */
7928 +
7929 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7930 +int __init au_sysrq_init(void);
7931 +void au_sysrq_fin(void);
7932 +
7933 +#ifdef CONFIG_HW_CONSOLE
7934 +#define au_dbg_blocked() do { \
7935 +       WARN_ON(1); \
7936 +       handle_sysrq('w'); \
7937 +} while (0)
7938 +#else
7939 +AuStubVoid(au_dbg_blocked, void)
7940 +#endif
7941 +
7942 +#else
7943 +AuStubInt0(__init au_sysrq_init, void)
7944 +AuStubVoid(au_sysrq_fin, void)
7945 +AuStubVoid(au_dbg_blocked, void)
7946 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7947 +
7948 +#endif /* __KERNEL__ */
7949 +#endif /* __AUFS_DEBUG_H__ */
7950 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7951 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7952 +++ linux/fs/aufs/dentry.c      2023-04-24 08:13:40.969576115 +0200
7953 @@ -0,0 +1,1168 @@
7954 +// SPDX-License-Identifier: GPL-2.0
7955 +/*
7956 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7957 + *
7958 + * This program is free software; you can redistribute it and/or modify
7959 + * it under the terms of the GNU General Public License as published by
7960 + * the Free Software Foundation; either version 2 of the License, or
7961 + * (at your option) any later version.
7962 + *
7963 + * This program is distributed in the hope that it will be useful,
7964 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7965 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7966 + * GNU General Public License for more details.
7967 + *
7968 + * You should have received a copy of the GNU General Public License
7969 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7970 + */
7971 +
7972 +/*
7973 + * lookup and dentry operations
7974 + */
7975 +
7976 +#include <linux/iversion.h>
7977 +#include "aufs.h"
7978 +
7979 +/*
7980 + * returns positive/negative dentry, NULL or an error.
7981 + * NULL means whiteout-ed or not-found.
7982 + */
7983 +static struct dentry*
7984 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7985 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7986 +{
7987 +       struct dentry *h_dentry;
7988 +       struct inode *h_inode;
7989 +       struct au_branch *br;
7990 +       struct mnt_idmap *h_idmap;
7991 +       struct path h_path;
7992 +       int wh_found, opq;
7993 +       unsigned char wh_able;
7994 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7995 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7996 +                                                         IGNORE_PERM);
7997 +
7998 +       wh_found = 0;
7999 +       br = au_sbr(dentry->d_sb, bindex);
8000 +       h_path.dentry = h_parent;
8001 +       h_path.mnt = au_br_mnt(br);
8002 +       h_idmap = au_br_idmap(br);
8003 +       wh_able = !!au_br_whable(br->br_perm);
8004 +       if (wh_able)
8005 +               wh_found = au_wh_test(h_idmap, &h_path, &args->whname,
8006 +                                     ignore_perm);
8007 +       h_dentry = ERR_PTR(wh_found);
8008 +       if (!wh_found)
8009 +               goto real_lookup;
8010 +       if (unlikely(wh_found < 0))
8011 +               goto out;
8012 +
8013 +       /* We found a whiteout */
8014 +       /* au_set_dbbot(dentry, bindex); */
8015 +       au_set_dbwh(dentry, bindex);
8016 +       if (!allow_neg)
8017 +               return NULL; /* success */
8018 +
8019 +real_lookup:
8020 +       if (!ignore_perm)
8021 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8022 +       else
8023 +               h_dentry = au_sio_lkup_one(h_idmap, args->name, &h_path);
8024 +       if (IS_ERR(h_dentry)) {
8025 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8026 +                   && !allow_neg)
8027 +                       h_dentry = NULL;
8028 +               goto out;
8029 +       }
8030 +
8031 +       h_inode = d_inode(h_dentry);
8032 +       if (d_is_negative(h_dentry)) {
8033 +               if (!allow_neg)
8034 +                       goto out_neg;
8035 +       } else if (wh_found
8036 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8037 +               goto out_neg;
8038 +       else if (au_ftest_lkup(args->flags, DIRREN)
8039 +                /* && h_inode */
8040 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8041 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8042 +                     (unsigned long long)h_inode->i_ino);
8043 +               goto out_neg;
8044 +       }
8045 +
8046 +       if (au_dbbot(dentry) <= bindex)
8047 +               au_set_dbbot(dentry, bindex);
8048 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8049 +               au_set_dbtop(dentry, bindex);
8050 +       au_set_h_dptr(dentry, bindex, h_dentry);
8051 +
8052 +       if (!d_is_dir(h_dentry)
8053 +           || !wh_able
8054 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8055 +               goto out; /* success */
8056 +
8057 +       h_path.dentry = h_dentry;
8058 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8059 +       opq = au_diropq_test(h_idmap, &h_path);
8060 +       inode_unlock_shared(h_inode);
8061 +       if (opq > 0)
8062 +               au_set_dbdiropq(dentry, bindex);
8063 +       else if (unlikely(opq < 0)) {
8064 +               au_set_h_dptr(dentry, bindex, NULL);
8065 +               h_dentry = ERR_PTR(opq);
8066 +       }
8067 +       goto out;
8068 +
8069 +out_neg:
8070 +       dput(h_dentry);
8071 +       h_dentry = NULL;
8072 +out:
8073 +       return h_dentry;
8074 +}
8075 +
8076 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8077 +{
8078 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8079 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8080 +               return -EPERM;
8081 +       return 0;
8082 +}
8083 +
8084 +/*
8085 + * returns the number of lower positive dentries,
8086 + * otherwise an error.
8087 + * can be called at unlinking with @type is zero.
8088 + */
8089 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8090 +                  unsigned int flags)
8091 +{
8092 +       int npositive, err;
8093 +       aufs_bindex_t bindex, btail, bdiropq;
8094 +       unsigned char isdir, dirperm1, dirren;
8095 +       struct au_do_lookup_args args = {
8096 +               .flags          = flags,
8097 +               .name           = &dentry->d_name
8098 +       };
8099 +       struct dentry *parent;
8100 +       struct super_block *sb;
8101 +
8102 +       sb = dentry->d_sb;
8103 +       err = au_test_shwh(sb, args.name);
8104 +       if (unlikely(err))
8105 +               goto out;
8106 +
8107 +       err = au_wh_name_alloc(&args.whname, args.name);
8108 +       if (unlikely(err))
8109 +               goto out;
8110 +
8111 +       isdir = !!d_is_dir(dentry);
8112 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8113 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8114 +       if (dirren)
8115 +               au_fset_lkup(args.flags, DIRREN);
8116 +
8117 +       npositive = 0;
8118 +       parent = dget_parent(dentry);
8119 +       btail = au_dbtaildir(parent);
8120 +       for (bindex = btop; bindex <= btail; bindex++) {
8121 +               struct dentry *h_parent, *h_dentry;
8122 +               struct inode *h_inode, *h_dir;
8123 +               struct au_branch *br;
8124 +
8125 +               h_dentry = au_h_dptr(dentry, bindex);
8126 +               if (h_dentry) {
8127 +                       if (d_is_positive(h_dentry))
8128 +                               npositive++;
8129 +                       break;
8130 +               }
8131 +               h_parent = au_h_dptr(parent, bindex);
8132 +               if (!h_parent || !d_is_dir(h_parent))
8133 +                       continue;
8134 +
8135 +               if (dirren) {
8136 +                       /* if the inum matches, then use the prepared name */
8137 +                       err = au_dr_lkup_name(&args, bindex);
8138 +                       if (unlikely(err))
8139 +                               goto out_parent;
8140 +               }
8141 +
8142 +               h_dir = d_inode(h_parent);
8143 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8144 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8145 +               inode_unlock_shared(h_dir);
8146 +               err = PTR_ERR(h_dentry);
8147 +               if (IS_ERR(h_dentry))
8148 +                       goto out_parent;
8149 +               if (h_dentry)
8150 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8151 +               if (dirperm1)
8152 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8153 +
8154 +               if (au_dbwh(dentry) == bindex)
8155 +                       break;
8156 +               if (!h_dentry)
8157 +                       continue;
8158 +               if (d_is_negative(h_dentry))
8159 +                       continue;
8160 +               h_inode = d_inode(h_dentry);
8161 +               npositive++;
8162 +               if (!args.type)
8163 +                       args.type = h_inode->i_mode & S_IFMT;
8164 +               if (args.type != S_IFDIR)
8165 +                       break;
8166 +               else if (isdir) {
8167 +                       /* the type of lower may be different */
8168 +                       bdiropq = au_dbdiropq(dentry);
8169 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8170 +                               break;
8171 +               }
8172 +               br = au_sbr(sb, bindex);
8173 +               if (dirren
8174 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8175 +                                          /*add_ent*/NULL)) {
8176 +                       /* prepare next name to lookup */
8177 +                       err = au_dr_lkup(&args, dentry, bindex);
8178 +                       if (unlikely(err))
8179 +                               goto out_parent;
8180 +               }
8181 +       }
8182 +
8183 +       if (npositive) {
8184 +               AuLabel(positive);
8185 +               au_update_dbtop(dentry);
8186 +       }
8187 +       err = npositive;
8188 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8189 +                    && au_dbtop(dentry) < 0)) {
8190 +               err = -EIO;
8191 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8192 +                       dentry, err);
8193 +       }
8194 +
8195 +out_parent:
8196 +       dput(parent);
8197 +       au_kfree_try_rcu(args.whname.name);
8198 +       if (dirren)
8199 +               au_dr_lkup_fin(&args);
8200 +out:
8201 +       return err;
8202 +}
8203 +
8204 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
8205 +                              struct path *ppath)
8206 +{
8207 +       struct dentry *dentry;
8208 +       int wkq_err;
8209 +
8210 +       if (!au_test_h_perm_sio(idmap, d_inode(ppath->dentry), MAY_EXEC))
8211 +               dentry = vfsub_lkup_one(name, ppath);
8212 +       else {
8213 +               struct vfsub_lkup_one_args args = {
8214 +                       .errp   = &dentry,
8215 +                       .name   = name,
8216 +                       .ppath  = ppath
8217 +               };
8218 +
8219 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8220 +               if (unlikely(wkq_err))
8221 +                       dentry = ERR_PTR(wkq_err);
8222 +       }
8223 +
8224 +       return dentry;
8225 +}
8226 +
8227 +/*
8228 + * lookup @dentry on @bindex which should be negative.
8229 + */
8230 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8231 +{
8232 +       int err;
8233 +       struct dentry *parent, *h_dentry;
8234 +       struct au_branch *br;
8235 +       struct mnt_idmap *h_idmap;
8236 +       struct path h_ppath;
8237 +
8238 +       parent = dget_parent(dentry);
8239 +       br = au_sbr(dentry->d_sb, bindex);
8240 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8241 +       h_ppath.mnt = au_br_mnt(br);
8242 +       h_idmap = au_br_idmap(br);
8243 +       if (wh)
8244 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8245 +       else
8246 +               h_dentry = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
8247 +       err = PTR_ERR(h_dentry);
8248 +       if (IS_ERR(h_dentry))
8249 +               goto out;
8250 +       if (unlikely(d_is_positive(h_dentry))) {
8251 +               err = -EIO;
8252 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8253 +               dput(h_dentry);
8254 +               goto out;
8255 +       }
8256 +
8257 +       err = 0;
8258 +       if (bindex < au_dbtop(dentry))
8259 +               au_set_dbtop(dentry, bindex);
8260 +       if (au_dbbot(dentry) < bindex)
8261 +               au_set_dbbot(dentry, bindex);
8262 +       au_set_h_dptr(dentry, bindex, h_dentry);
8263 +
8264 +out:
8265 +       dput(parent);
8266 +       return err;
8267 +}
8268 +
8269 +/* ---------------------------------------------------------------------- */
8270 +
8271 +/* subset of struct inode */
8272 +struct au_iattr {
8273 +       unsigned long           i_ino;
8274 +       /* unsigned int         i_nlink; */
8275 +       kuid_t                  i_uid;
8276 +       kgid_t                  i_gid;
8277 +       u64                     i_version;
8278 +/*
8279 +       loff_t                  i_size;
8280 +       blkcnt_t                i_blocks;
8281 +*/
8282 +       umode_t                 i_mode;
8283 +};
8284 +
8285 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8286 +{
8287 +       ia->i_ino = h_inode->i_ino;
8288 +       /* ia->i_nlink = h_inode->i_nlink; */
8289 +       ia->i_uid = h_inode->i_uid;
8290 +       ia->i_gid = h_inode->i_gid;
8291 +       ia->i_version = inode_query_iversion(h_inode);
8292 +/*
8293 +       ia->i_size = h_inode->i_size;
8294 +       ia->i_blocks = h_inode->i_blocks;
8295 +*/
8296 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8297 +}
8298 +
8299 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8300 +{
8301 +       return ia->i_ino != h_inode->i_ino
8302 +               /* || ia->i_nlink != h_inode->i_nlink */
8303 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8304 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8305 +               || !inode_eq_iversion(h_inode, ia->i_version)
8306 +/*
8307 +               || ia->i_size != h_inode->i_size
8308 +               || ia->i_blocks != h_inode->i_blocks
8309 +*/
8310 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8311 +}
8312 +
8313 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8314 +                             struct au_branch *br)
8315 +{
8316 +       int err;
8317 +       struct au_iattr ia;
8318 +       struct inode *h_inode;
8319 +       struct dentry *h_d;
8320 +       struct super_block *h_sb;
8321 +       struct path h_ppath;
8322 +
8323 +       err = 0;
8324 +       memset(&ia, -1, sizeof(ia));
8325 +       h_sb = h_dentry->d_sb;
8326 +       h_inode = NULL;
8327 +       if (d_is_positive(h_dentry)) {
8328 +               h_inode = d_inode(h_dentry);
8329 +               au_iattr_save(&ia, h_inode);
8330 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8331 +               /* nfs d_revalidate may return 0 for negative dentry */
8332 +               /* fuse d_revalidate always return 0 for negative dentry */
8333 +               goto out;
8334 +
8335 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8336 +       h_ppath.dentry = h_parent;
8337 +       h_ppath.mnt = au_br_mnt(br);
8338 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8339 +       err = PTR_ERR(h_d);
8340 +       if (IS_ERR(h_d))
8341 +               goto out;
8342 +
8343 +       err = 0;
8344 +       if (unlikely(h_d != h_dentry
8345 +                    || d_inode(h_d) != h_inode
8346 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8347 +               err = au_busy_or_stale();
8348 +       dput(h_d);
8349 +
8350 +out:
8351 +       AuTraceErr(err);
8352 +       return err;
8353 +}
8354 +
8355 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8356 +               struct dentry *h_parent, struct au_branch *br)
8357 +{
8358 +       int err;
8359 +
8360 +       err = 0;
8361 +       if (udba == AuOpt_UDBA_REVAL
8362 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8363 +               IMustLock(h_dir);
8364 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8365 +       } else if (udba != AuOpt_UDBA_NONE)
8366 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8367 +
8368 +       return err;
8369 +}
8370 +
8371 +/* ---------------------------------------------------------------------- */
8372 +
8373 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8374 +{
8375 +       int err;
8376 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8377 +       struct au_hdentry tmp, *p, *q;
8378 +       struct au_dinfo *dinfo;
8379 +       struct super_block *sb;
8380 +
8381 +       DiMustWriteLock(dentry);
8382 +
8383 +       sb = dentry->d_sb;
8384 +       dinfo = au_di(dentry);
8385 +       bbot = dinfo->di_bbot;
8386 +       bwh = dinfo->di_bwh;
8387 +       bdiropq = dinfo->di_bdiropq;
8388 +       bindex = dinfo->di_btop;
8389 +       p = au_hdentry(dinfo, bindex);
8390 +       for (; bindex <= bbot; bindex++, p++) {
8391 +               if (!p->hd_dentry)
8392 +                       continue;
8393 +
8394 +               new_bindex = au_br_index(sb, p->hd_id);
8395 +               if (new_bindex == bindex)
8396 +                       continue;
8397 +
8398 +               if (dinfo->di_bwh == bindex)
8399 +                       bwh = new_bindex;
8400 +               if (dinfo->di_bdiropq == bindex)
8401 +                       bdiropq = new_bindex;
8402 +               if (new_bindex < 0) {
8403 +                       au_hdput(p);
8404 +                       p->hd_dentry = NULL;
8405 +                       continue;
8406 +               }
8407 +
8408 +               /* swap two lower dentries, and loop again */
8409 +               q = au_hdentry(dinfo, new_bindex);
8410 +               tmp = *q;
8411 +               *q = *p;
8412 +               *p = tmp;
8413 +               if (tmp.hd_dentry) {
8414 +                       bindex--;
8415 +                       p--;
8416 +               }
8417 +       }
8418 +
8419 +       dinfo->di_bwh = -1;
8420 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8421 +               dinfo->di_bwh = bwh;
8422 +
8423 +       dinfo->di_bdiropq = -1;
8424 +       if (bdiropq >= 0
8425 +           && bdiropq <= au_sbbot(sb)
8426 +           && au_sbr_whable(sb, bdiropq))
8427 +               dinfo->di_bdiropq = bdiropq;
8428 +
8429 +       err = -EIO;
8430 +       dinfo->di_btop = -1;
8431 +       dinfo->di_bbot = -1;
8432 +       bbot = au_dbbot(parent);
8433 +       bindex = 0;
8434 +       p = au_hdentry(dinfo, bindex);
8435 +       for (; bindex <= bbot; bindex++, p++)
8436 +               if (p->hd_dentry) {
8437 +                       dinfo->di_btop = bindex;
8438 +                       break;
8439 +               }
8440 +
8441 +       if (dinfo->di_btop >= 0) {
8442 +               bindex = bbot;
8443 +               p = au_hdentry(dinfo, bindex);
8444 +               for (; bindex >= 0; bindex--, p--)
8445 +                       if (p->hd_dentry) {
8446 +                               dinfo->di_bbot = bindex;
8447 +                               err = 0;
8448 +                               break;
8449 +                       }
8450 +       }
8451 +
8452 +       return err;
8453 +}
8454 +
8455 +static void au_do_hide(struct dentry *dentry)
8456 +{
8457 +       struct inode *inode;
8458 +
8459 +       if (d_really_is_positive(dentry)) {
8460 +               inode = d_inode(dentry);
8461 +               if (!d_is_dir(dentry)) {
8462 +                       if (inode->i_nlink && !d_unhashed(dentry))
8463 +                               drop_nlink(inode);
8464 +               } else {
8465 +                       clear_nlink(inode);
8466 +                       /* stop next lookup */
8467 +                       inode->i_flags |= S_DEAD;
8468 +               }
8469 +               smp_mb(); /* necessary? */
8470 +       }
8471 +       d_drop(dentry);
8472 +}
8473 +
8474 +static int au_hide_children(struct dentry *parent)
8475 +{
8476 +       int err, i, j, ndentry;
8477 +       struct au_dcsub_pages dpages;
8478 +       struct au_dpage *dpage;
8479 +       struct dentry *dentry;
8480 +
8481 +       err = au_dpages_init(&dpages, GFP_NOFS);
8482 +       if (unlikely(err))
8483 +               goto out;
8484 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8485 +       if (unlikely(err))
8486 +               goto out_dpages;
8487 +
8488 +       /* in reverse order */
8489 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8490 +               dpage = dpages.dpages + i;
8491 +               ndentry = dpage->ndentry;
8492 +               for (j = ndentry - 1; j >= 0; j--) {
8493 +                       dentry = dpage->dentries[j];
8494 +                       if (dentry != parent)
8495 +                               au_do_hide(dentry);
8496 +               }
8497 +       }
8498 +
8499 +out_dpages:
8500 +       au_dpages_free(&dpages);
8501 +out:
8502 +       return err;
8503 +}
8504 +
8505 +static void au_hide(struct dentry *dentry)
8506 +{
8507 +       int err;
8508 +
8509 +       AuDbgDentry(dentry);
8510 +       if (d_is_dir(dentry)) {
8511 +               /* shrink_dcache_parent(dentry); */
8512 +               err = au_hide_children(dentry);
8513 +               if (unlikely(err))
8514 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8515 +                               dentry, err);
8516 +       }
8517 +       au_do_hide(dentry);
8518 +}
8519 +
8520 +/*
8521 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8522 + *
8523 + * a dirty branch is added
8524 + * - on the top of layers
8525 + * - in the middle of layers
8526 + * - to the bottom of layers
8527 + *
8528 + * on the added branch there exists
8529 + * - a whiteout
8530 + * - a diropq
8531 + * - a same named entry
8532 + *   + exist
8533 + *     * negative --> positive
8534 + *     * positive --> positive
8535 + *      - type is unchanged
8536 + *      - type is changed
8537 + *   + doesn't exist
8538 + *     * negative --> negative
8539 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8540 + * - none
8541 + */
8542 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8543 +                              struct au_dinfo *tmp)
8544 +{
8545 +       int err;
8546 +       aufs_bindex_t bindex, bbot;
8547 +       struct {
8548 +               struct dentry *dentry;
8549 +               struct inode *inode;
8550 +               mode_t mode;
8551 +       } orig_h, tmp_h = {
8552 +               .dentry = NULL
8553 +       };
8554 +       struct au_hdentry *hd;
8555 +       struct inode *inode, *h_inode;
8556 +       struct dentry *h_dentry;
8557 +
8558 +       err = 0;
8559 +       AuDebugOn(dinfo->di_btop < 0);
8560 +       orig_h.mode = 0;
8561 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8562 +       orig_h.inode = NULL;
8563 +       if (d_is_positive(orig_h.dentry)) {
8564 +               orig_h.inode = d_inode(orig_h.dentry);
8565 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8566 +       }
8567 +       if (tmp->di_btop >= 0) {
8568 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8569 +               if (d_is_positive(tmp_h.dentry)) {
8570 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8571 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8572 +               }
8573 +       }
8574 +
8575 +       inode = NULL;
8576 +       if (d_really_is_positive(dentry))
8577 +               inode = d_inode(dentry);
8578 +       if (!orig_h.inode) {
8579 +               AuDbg("negative originally\n");
8580 +               if (inode) {
8581 +                       au_hide(dentry);
8582 +                       goto out;
8583 +               }
8584 +               AuDebugOn(inode);
8585 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8586 +               AuDebugOn(dinfo->di_bdiropq != -1);
8587 +
8588 +               if (!tmp_h.inode) {
8589 +                       AuDbg("negative --> negative\n");
8590 +                       /* should have only one negative lower */
8591 +                       if (tmp->di_btop >= 0
8592 +                           && tmp->di_btop < dinfo->di_btop) {
8593 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8594 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8595 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8596 +                               au_di_cp(dinfo, tmp);
8597 +                               hd = au_hdentry(tmp, tmp->di_btop);
8598 +                               au_set_h_dptr(dentry, tmp->di_btop,
8599 +                                             dget(hd->hd_dentry));
8600 +                       }
8601 +                       au_dbg_verify_dinode(dentry);
8602 +               } else {
8603 +                       AuDbg("negative --> positive\n");
8604 +                       /*
8605 +                        * similar to the behaviour of creating with bypassing
8606 +                        * aufs.
8607 +                        * unhash it in order to force an error in the
8608 +                        * succeeding create operation.
8609 +                        * we should not set S_DEAD here.
8610 +                        */
8611 +                       d_drop(dentry);
8612 +                       /* au_di_swap(tmp, dinfo); */
8613 +                       au_dbg_verify_dinode(dentry);
8614 +               }
8615 +       } else {
8616 +               AuDbg("positive originally\n");
8617 +               /* inode may be NULL */
8618 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8619 +               if (!tmp_h.inode) {
8620 +                       AuDbg("positive --> negative\n");
8621 +                       /* or bypassing aufs */
8622 +                       au_hide(dentry);
8623 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8624 +                               dinfo->di_bwh = tmp->di_bwh;
8625 +                       if (inode)
8626 +                               err = au_refresh_hinode_self(inode);
8627 +                       au_dbg_verify_dinode(dentry);
8628 +               } else if (orig_h.mode == tmp_h.mode) {
8629 +                       AuDbg("positive --> positive, same type\n");
8630 +                       if (!S_ISDIR(orig_h.mode)
8631 +                           && dinfo->di_btop > tmp->di_btop) {
8632 +                               /*
8633 +                                * similar to the behaviour of removing and
8634 +                                * creating.
8635 +                                */
8636 +                               au_hide(dentry);
8637 +                               if (inode)
8638 +                                       err = au_refresh_hinode_self(inode);
8639 +                               au_dbg_verify_dinode(dentry);
8640 +                       } else {
8641 +                               /* fill empty slots */
8642 +                               if (dinfo->di_btop > tmp->di_btop)
8643 +                                       dinfo->di_btop = tmp->di_btop;
8644 +                               if (dinfo->di_bbot < tmp->di_bbot)
8645 +                                       dinfo->di_bbot = tmp->di_bbot;
8646 +                               dinfo->di_bwh = tmp->di_bwh;
8647 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8648 +                               bbot = dinfo->di_bbot;
8649 +                               bindex = tmp->di_btop;
8650 +                               hd = au_hdentry(tmp, bindex);
8651 +                               for (; bindex <= bbot; bindex++, hd++) {
8652 +                                       if (au_h_dptr(dentry, bindex))
8653 +                                               continue;
8654 +                                       h_dentry = hd->hd_dentry;
8655 +                                       if (!h_dentry)
8656 +                                               continue;
8657 +                                       AuDebugOn(d_is_negative(h_dentry));
8658 +                                       h_inode = d_inode(h_dentry);
8659 +                                       AuDebugOn(orig_h.mode
8660 +                                                 != (h_inode->i_mode
8661 +                                                     & S_IFMT));
8662 +                                       au_set_h_dptr(dentry, bindex,
8663 +                                                     dget(h_dentry));
8664 +                               }
8665 +                               if (inode)
8666 +                                       err = au_refresh_hinode(inode, dentry);
8667 +                               au_dbg_verify_dinode(dentry);
8668 +                       }
8669 +               } else {
8670 +                       AuDbg("positive --> positive, different type\n");
8671 +                       /* similar to the behaviour of removing and creating */
8672 +                       au_hide(dentry);
8673 +                       if (inode)
8674 +                               err = au_refresh_hinode_self(inode);
8675 +                       au_dbg_verify_dinode(dentry);
8676 +               }
8677 +       }
8678 +
8679 +out:
8680 +       return err;
8681 +}
8682 +
8683 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8684 +{
8685 +       const struct dentry_operations *dop
8686 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8687 +       static const unsigned int mask
8688 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8689 +
8690 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8691 +
8692 +       if (dentry->d_op == dop)
8693 +               return;
8694 +
8695 +       AuDbg("%pd\n", dentry);
8696 +       spin_lock(&dentry->d_lock);
8697 +       if (dop == &aufs_dop)
8698 +               dentry->d_flags |= mask;
8699 +       else
8700 +               dentry->d_flags &= ~mask;
8701 +       dentry->d_op = dop;
8702 +       spin_unlock(&dentry->d_lock);
8703 +}
8704 +
8705 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8706 +{
8707 +       int err, ebrange, nbr;
8708 +       unsigned int sigen;
8709 +       struct au_dinfo *dinfo, *tmp;
8710 +       struct super_block *sb;
8711 +       struct inode *inode;
8712 +
8713 +       DiMustWriteLock(dentry);
8714 +       AuDebugOn(IS_ROOT(dentry));
8715 +       AuDebugOn(d_really_is_negative(parent));
8716 +
8717 +       sb = dentry->d_sb;
8718 +       sigen = au_sigen(sb);
8719 +       err = au_digen_test(parent, sigen);
8720 +       if (unlikely(err))
8721 +               goto out;
8722 +
8723 +       nbr = au_sbbot(sb) + 1;
8724 +       dinfo = au_di(dentry);
8725 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8726 +       if (unlikely(err))
8727 +               goto out;
8728 +       ebrange = au_dbrange_test(dentry);
8729 +       if (!ebrange)
8730 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8731 +
8732 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8733 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8734 +               if (d_really_is_positive(dentry)) {
8735 +                       inode = d_inode(dentry);
8736 +                       err = au_refresh_hinode_self(inode);
8737 +               }
8738 +               au_dbg_verify_dinode(dentry);
8739 +               if (!err)
8740 +                       goto out_dgen; /* success */
8741 +               goto out;
8742 +       }
8743 +
8744 +       /* temporary dinfo */
8745 +       AuDbgDentry(dentry);
8746 +       err = -ENOMEM;
8747 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8748 +       if (unlikely(!tmp))
8749 +               goto out;
8750 +       au_di_swap(tmp, dinfo);
8751 +       /* returns the number of positive dentries */
8752 +       /*
8753 +        * if current working dir is removed, it returns an error.
8754 +        * but the dentry is legal.
8755 +        */
8756 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8757 +       AuDbgDentry(dentry);
8758 +       au_di_swap(tmp, dinfo);
8759 +       if (err == -ENOENT)
8760 +               err = 0;
8761 +       if (err >= 0) {
8762 +               /* compare/refresh by dinfo */
8763 +               AuDbgDentry(dentry);
8764 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8765 +               au_dbg_verify_dinode(dentry);
8766 +               AuTraceErr(err);
8767 +       }
8768 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8769 +       au_rw_write_unlock(&tmp->di_rwsem);
8770 +       au_di_free(tmp);
8771 +       if (unlikely(err))
8772 +               goto out;
8773 +
8774 +out_dgen:
8775 +       au_update_digen(dentry);
8776 +out:
8777 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8778 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8779 +               AuDbgDentry(dentry);
8780 +       }
8781 +       AuTraceErr(err);
8782 +       return err;
8783 +}
8784 +
8785 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8786 +                          struct dentry *dentry, aufs_bindex_t bindex)
8787 +{
8788 +       int err, valid;
8789 +
8790 +       err = 0;
8791 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8792 +               goto out;
8793 +
8794 +       AuDbg("b%d\n", bindex);
8795 +       /*
8796 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8797 +        * due to whiteout and branch permission.
8798 +        */
8799 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8800 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8801 +       /* it may return tri-state */
8802 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8803 +
8804 +       if (unlikely(valid < 0))
8805 +               err = valid;
8806 +       else if (!valid)
8807 +               err = -EINVAL;
8808 +
8809 +out:
8810 +       AuTraceErr(err);
8811 +       return err;
8812 +}
8813 +
8814 +/* todo: remove this */
8815 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8816 +                         unsigned int flags, int do_udba, int dirren)
8817 +{
8818 +       int err;
8819 +       umode_t mode, h_mode;
8820 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8821 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8822 +       struct inode *h_inode, *h_cached_inode;
8823 +       struct dentry *h_dentry;
8824 +       struct qstr *name, *h_name;
8825 +
8826 +       err = 0;
8827 +       plus = 0;
8828 +       mode = 0;
8829 +       ibs = -1;
8830 +       ibe = -1;
8831 +       unhashed = !!d_unhashed(dentry);
8832 +       is_root = !!IS_ROOT(dentry);
8833 +       name = &dentry->d_name;
8834 +       tmpfile = au_di(dentry)->di_tmpfile;
8835 +
8836 +       /*
8837 +        * Theoretically, REVAL test should be unnecessary in case of
8838 +        * {FS,I}NOTIFY.
8839 +        * But {fs,i}notify doesn't fire some necessary events,
8840 +        *      IN_ATTRIB for atime/nlink/pageio
8841 +        * Let's do REVAL test too.
8842 +        */
8843 +       if (do_udba && inode) {
8844 +               mode = (inode->i_mode & S_IFMT);
8845 +               plus = (inode->i_nlink > 0);
8846 +               ibs = au_ibtop(inode);
8847 +               ibe = au_ibbot(inode);
8848 +       }
8849 +
8850 +       btop = au_dbtop(dentry);
8851 +       btail = btop;
8852 +       if (inode && S_ISDIR(inode->i_mode))
8853 +               btail = au_dbtaildir(dentry);
8854 +       for (bindex = btop; bindex <= btail; bindex++) {
8855 +               h_dentry = au_h_dptr(dentry, bindex);
8856 +               if (!h_dentry)
8857 +                       continue;
8858 +
8859 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8860 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8861 +               spin_lock(&h_dentry->d_lock);
8862 +               h_name = &h_dentry->d_name;
8863 +               if (unlikely(do_udba
8864 +                            && !is_root
8865 +                            && ((!h_nfs
8866 +                                 && (unhashed != !!d_unhashed(h_dentry)
8867 +                                     || (!tmpfile && !dirren
8868 +                                         && !au_qstreq(name, h_name))
8869 +                                         ))
8870 +                                || (h_nfs
8871 +                                    && !(flags & LOOKUP_OPEN)
8872 +                                    && (h_dentry->d_flags
8873 +                                        & DCACHE_NFSFS_RENAMED)))
8874 +                           )) {
8875 +                       int h_unhashed;
8876 +
8877 +                       h_unhashed = d_unhashed(h_dentry);
8878 +                       spin_unlock(&h_dentry->d_lock);
8879 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8880 +                             unhashed, h_unhashed, dentry, h_dentry);
8881 +                       goto err;
8882 +               }
8883 +               spin_unlock(&h_dentry->d_lock);
8884 +
8885 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8886 +               if (unlikely(err))
8887 +                       /* do not goto err, to keep the errno */
8888 +                       break;
8889 +
8890 +               /* todo: plink too? */
8891 +               if (!do_udba)
8892 +                       continue;
8893 +
8894 +               /* UDBA tests */
8895 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8896 +                       goto err;
8897 +
8898 +               h_inode = NULL;
8899 +               if (d_is_positive(h_dentry))
8900 +                       h_inode = d_inode(h_dentry);
8901 +               h_plus = plus;
8902 +               h_mode = mode;
8903 +               h_cached_inode = h_inode;
8904 +               if (h_inode) {
8905 +                       h_mode = (h_inode->i_mode & S_IFMT);
8906 +                       h_plus = (h_inode->i_nlink > 0);
8907 +               }
8908 +               if (inode && ibs <= bindex && bindex <= ibe)
8909 +                       h_cached_inode = au_h_iptr(inode, bindex);
8910 +
8911 +               if (!h_nfs) {
8912 +                       if (unlikely(plus != h_plus && !tmpfile))
8913 +                               goto err;
8914 +               } else {
8915 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8916 +                                    && !is_root
8917 +                                    && !IS_ROOT(h_dentry)
8918 +                                    && unhashed != d_unhashed(h_dentry)))
8919 +                               goto err;
8920 +               }
8921 +               if (unlikely(mode != h_mode
8922 +                            || h_cached_inode != h_inode))
8923 +                       goto err;
8924 +               continue;
8925 +
8926 +err:
8927 +               err = -EINVAL;
8928 +               break;
8929 +       }
8930 +
8931 +       AuTraceErr(err);
8932 +       return err;
8933 +}
8934 +
8935 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8936 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8937 +{
8938 +       int err;
8939 +       struct dentry *parent;
8940 +
8941 +       if (!au_digen_test(dentry, sigen))
8942 +               return 0;
8943 +
8944 +       parent = dget_parent(dentry);
8945 +       di_read_lock_parent(parent, AuLock_IR);
8946 +       AuDebugOn(au_digen_test(parent, sigen));
8947 +       au_dbg_verify_gen(parent, sigen);
8948 +       err = au_refresh_dentry(dentry, parent);
8949 +       di_read_unlock(parent, AuLock_IR);
8950 +       dput(parent);
8951 +       AuTraceErr(err);
8952 +       return err;
8953 +}
8954 +
8955 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8956 +{
8957 +       int err;
8958 +       struct dentry *d, *parent;
8959 +
8960 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8961 +               return simple_reval_dpath(dentry, sigen);
8962 +
8963 +       /* slow loop, keep it simple and stupid */
8964 +       /* cf: au_cpup_dirs() */
8965 +       err = 0;
8966 +       parent = NULL;
8967 +       while (au_digen_test(dentry, sigen)) {
8968 +               d = dentry;
8969 +               while (1) {
8970 +                       dput(parent);
8971 +                       parent = dget_parent(d);
8972 +                       if (!au_digen_test(parent, sigen))
8973 +                               break;
8974 +                       d = parent;
8975 +               }
8976 +
8977 +               if (d != dentry)
8978 +                       di_write_lock_child2(d);
8979 +
8980 +               /* someone might update our dentry while we were sleeping */
8981 +               if (au_digen_test(d, sigen)) {
8982 +                       /*
8983 +                        * todo: consolidate with simple_reval_dpath(),
8984 +                        * do_refresh() and au_reval_for_attr().
8985 +                        */
8986 +                       di_read_lock_parent(parent, AuLock_IR);
8987 +                       err = au_refresh_dentry(d, parent);
8988 +                       di_read_unlock(parent, AuLock_IR);
8989 +               }
8990 +
8991 +               if (d != dentry)
8992 +                       di_write_unlock(d);
8993 +               dput(parent);
8994 +               if (unlikely(err))
8995 +                       break;
8996 +       }
8997 +
8998 +       return err;
8999 +}
9000 +
9001 +/*
9002 + * if valid returns 1, otherwise 0.
9003 + */
9004 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9005 +{
9006 +       int valid, err;
9007 +       unsigned int sigen;
9008 +       unsigned char do_udba, dirren;
9009 +       struct super_block *sb;
9010 +       struct inode *inode;
9011 +
9012 +       /* todo: support rcu-walk? */
9013 +       if (flags & LOOKUP_RCU)
9014 +               return -ECHILD;
9015 +
9016 +       valid = 0;
9017 +       if (unlikely(!au_di(dentry)))
9018 +               goto out;
9019 +
9020 +       valid = 1;
9021 +       sb = dentry->d_sb;
9022 +       /*
9023 +        * todo: very ugly
9024 +        * i_mutex of parent dir may be held,
9025 +        * but we should not return 'invalid' due to busy.
9026 +        */
9027 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9028 +       if (unlikely(err)) {
9029 +               valid = err;
9030 +               AuTraceErr(err);
9031 +               goto out;
9032 +       }
9033 +       inode = NULL;
9034 +       if (d_really_is_positive(dentry))
9035 +               inode = d_inode(dentry);
9036 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9037 +               err = -EINVAL;
9038 +               AuTraceErr(err);
9039 +               goto out_dgrade;
9040 +       }
9041 +       if (unlikely(au_dbrange_test(dentry))) {
9042 +               err = -EINVAL;
9043 +               AuTraceErr(err);
9044 +               goto out_dgrade;
9045 +       }
9046 +
9047 +       sigen = au_sigen(sb);
9048 +       if (au_digen_test(dentry, sigen)) {
9049 +               AuDebugOn(IS_ROOT(dentry));
9050 +               err = au_reval_dpath(dentry, sigen);
9051 +               if (unlikely(err)) {
9052 +                       AuTraceErr(err);
9053 +                       goto out_dgrade;
9054 +               }
9055 +       }
9056 +       di_downgrade_lock(dentry, AuLock_IR);
9057 +
9058 +       err = -EINVAL;
9059 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9060 +           && inode
9061 +           && !(inode->i_state && I_LINKABLE)
9062 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9063 +               AuTraceErr(err);
9064 +               goto out_inval;
9065 +       }
9066 +
9067 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9068 +       if (do_udba && inode) {
9069 +               aufs_bindex_t btop = au_ibtop(inode);
9070 +               struct inode *h_inode;
9071 +
9072 +               if (btop >= 0) {
9073 +                       h_inode = au_h_iptr(inode, btop);
9074 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9075 +                               AuTraceErr(err);
9076 +                               goto out_inval;
9077 +                       }
9078 +               }
9079 +       }
9080 +
9081 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9082 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9083 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9084 +               err = -EIO;
9085 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9086 +                     dentry, err);
9087 +       }
9088 +       goto out_inval;
9089 +
9090 +out_dgrade:
9091 +       di_downgrade_lock(dentry, AuLock_IR);
9092 +out_inval:
9093 +       aufs_read_unlock(dentry, AuLock_IR);
9094 +       AuTraceErr(err);
9095 +       valid = !err;
9096 +out:
9097 +       if (!valid) {
9098 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9099 +               d_drop(dentry);
9100 +       }
9101 +       return valid;
9102 +}
9103 +
9104 +static void aufs_d_release(struct dentry *dentry)
9105 +{
9106 +       if (au_di(dentry)) {
9107 +               au_di_fin(dentry);
9108 +               au_hn_di_reinit(dentry);
9109 +       }
9110 +}
9111 +
9112 +const struct dentry_operations aufs_dop = {
9113 +       .d_revalidate           = aufs_d_revalidate,
9114 +       .d_weak_revalidate      = aufs_d_revalidate,
9115 +       .d_release              = aufs_d_release
9116 +};
9117 +
9118 +/* aufs_dop without d_revalidate */
9119 +const struct dentry_operations aufs_dop_noreval = {
9120 +       .d_release              = aufs_d_release
9121 +};
9122 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9123 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9124 +++ linux/fs/aufs/dentry.h      2023-04-24 08:13:40.969576115 +0200
9125 @@ -0,0 +1,270 @@
9126 +/* SPDX-License-Identifier: GPL-2.0 */
9127 +/*
9128 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9129 + *
9130 + * This program is free software; you can redistribute it and/or modify
9131 + * it under the terms of the GNU General Public License as published by
9132 + * the Free Software Foundation; either version 2 of the License, or
9133 + * (at your option) any later version.
9134 + *
9135 + * This program is distributed in the hope that it will be useful,
9136 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9137 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9138 + * GNU General Public License for more details.
9139 + *
9140 + * You should have received a copy of the GNU General Public License
9141 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9142 + */
9143 +
9144 +/*
9145 + * lookup and dentry operations
9146 + */
9147 +
9148 +#ifndef __AUFS_DENTRY_H__
9149 +#define __AUFS_DENTRY_H__
9150 +
9151 +#ifdef __KERNEL__
9152 +
9153 +#include <linux/dcache.h>
9154 +#include "dirren.h"
9155 +#include "rwsem.h"
9156 +
9157 +struct au_hdentry {
9158 +       struct dentry           *hd_dentry;
9159 +       aufs_bindex_t           hd_id;
9160 +};
9161 +
9162 +struct au_dinfo {
9163 +       atomic_t                di_generation;
9164 +
9165 +       struct au_rwsem         di_rwsem;
9166 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9167 +       unsigned char           di_tmpfile; /* to allow the different name */
9168 +       struct au_hdentry       *di_hdentry;
9169 +       struct file             *di_htmpfile;
9170 +       struct rcu_head         rcu;
9171 +} ____cacheline_aligned_in_smp;
9172 +
9173 +/* ---------------------------------------------------------------------- */
9174 +
9175 +/* flags for au_lkup_dentry() */
9176 +#define AuLkup_ALLOW_NEG       1
9177 +#define AuLkup_IGNORE_PERM     (1 << 1)
9178 +#define AuLkup_DIRREN          (1 << 2)
9179 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9180 +#define au_fset_lkup(flags, name) \
9181 +       do { (flags) |= AuLkup_##name; } while (0)
9182 +#define au_fclr_lkup(flags, name) \
9183 +       do { (flags) &= ~AuLkup_##name; } while (0)
9184 +
9185 +#ifndef CONFIG_AUFS_DIRREN
9186 +#undef AuLkup_DIRREN
9187 +#define AuLkup_DIRREN 0
9188 +#endif
9189 +
9190 +struct au_do_lookup_args {
9191 +       unsigned int            flags;
9192 +       mode_t                  type;
9193 +       struct qstr             whname, *name;
9194 +       struct au_dr_lookup     dirren;
9195 +};
9196 +
9197 +/* ---------------------------------------------------------------------- */
9198 +
9199 +/* dentry.c */
9200 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9201 +struct au_branch;
9202 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
9203 +                              struct path *ppath);
9204 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9205 +               struct dentry *h_parent, struct au_branch *br);
9206 +
9207 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9208 +                  unsigned int flags);
9209 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9210 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9211 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9212 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9213 +
9214 +/* dinfo.c */
9215 +void au_di_init_once(void *_di);
9216 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9217 +void au_di_free(struct au_dinfo *dinfo);
9218 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9219 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9220 +int au_di_init(struct dentry *dentry);
9221 +void au_di_fin(struct dentry *dentry);
9222 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9223 +
9224 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9225 +void di_read_unlock(struct dentry *d, int flags);
9226 +void di_downgrade_lock(struct dentry *d, int flags);
9227 +void di_write_lock(struct dentry *d, unsigned int lsc);
9228 +void di_write_unlock(struct dentry *d);
9229 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9230 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9231 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9232 +
9233 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9234 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9235 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9236 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9237 +
9238 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9239 +                  struct dentry *h_dentry);
9240 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9241 +int au_dbrange_test(struct dentry *dentry);
9242 +void au_update_digen(struct dentry *dentry);
9243 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9244 +void au_update_dbtop(struct dentry *dentry);
9245 +void au_update_dbbot(struct dentry *dentry);
9246 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9247 +
9248 +/* ---------------------------------------------------------------------- */
9249 +
9250 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9251 +{
9252 +       return dentry->d_fsdata;
9253 +}
9254 +
9255 +/* ---------------------------------------------------------------------- */
9256 +
9257 +/* lock subclass for dinfo */
9258 +enum {
9259 +       AuLsc_DI_CHILD,         /* child first */
9260 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9261 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9262 +       AuLsc_DI_PARENT,
9263 +       AuLsc_DI_PARENT2,
9264 +       AuLsc_DI_PARENT3,
9265 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9266 +};
9267 +
9268 +/*
9269 + * di_read_lock_child, di_write_lock_child,
9270 + * di_read_lock_child2, di_write_lock_child2,
9271 + * di_read_lock_child3, di_write_lock_child3,
9272 + * di_read_lock_parent, di_write_lock_parent,
9273 + * di_read_lock_parent2, di_write_lock_parent2,
9274 + * di_read_lock_parent3, di_write_lock_parent3,
9275 + */
9276 +#define AuReadLockFunc(name, lsc) \
9277 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9278 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9279 +
9280 +#define AuWriteLockFunc(name, lsc) \
9281 +static inline void di_write_lock_##name(struct dentry *d) \
9282 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9283 +
9284 +#define AuRWLockFuncs(name, lsc) \
9285 +       AuReadLockFunc(name, lsc) \
9286 +       AuWriteLockFunc(name, lsc)
9287 +
9288 +AuRWLockFuncs(child, CHILD);
9289 +AuRWLockFuncs(child2, CHILD2);
9290 +AuRWLockFuncs(child3, CHILD3);
9291 +AuRWLockFuncs(parent, PARENT);
9292 +AuRWLockFuncs(parent2, PARENT2);
9293 +AuRWLockFuncs(parent3, PARENT3);
9294 +
9295 +#undef AuReadLockFunc
9296 +#undef AuWriteLockFunc
9297 +#undef AuRWLockFuncs
9298 +
9299 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9300 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9301 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9302 +
9303 +/* ---------------------------------------------------------------------- */
9304 +
9305 +/* todo: memory barrier? */
9306 +static inline unsigned int au_digen(struct dentry *d)
9307 +{
9308 +       return atomic_read(&au_di(d)->di_generation);
9309 +}
9310 +
9311 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9312 +{
9313 +       hdentry->hd_dentry = NULL;
9314 +}
9315 +
9316 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9317 +                                           aufs_bindex_t bindex)
9318 +{
9319 +       return di->di_hdentry + bindex;
9320 +}
9321 +
9322 +static inline void au_hdput(struct au_hdentry *hd)
9323 +{
9324 +       if (hd)
9325 +               dput(hd->hd_dentry);
9326 +}
9327 +
9328 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9329 +{
9330 +       DiMustAnyLock(dentry);
9331 +       return au_di(dentry)->di_btop;
9332 +}
9333 +
9334 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9335 +{
9336 +       DiMustAnyLock(dentry);
9337 +       return au_di(dentry)->di_bbot;
9338 +}
9339 +
9340 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9341 +{
9342 +       DiMustAnyLock(dentry);
9343 +       return au_di(dentry)->di_bwh;
9344 +}
9345 +
9346 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9347 +{
9348 +       DiMustAnyLock(dentry);
9349 +       return au_di(dentry)->di_bdiropq;
9350 +}
9351 +
9352 +/* todo: hard/soft set? */
9353 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9354 +{
9355 +       DiMustWriteLock(dentry);
9356 +       au_di(dentry)->di_btop = bindex;
9357 +}
9358 +
9359 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9360 +{
9361 +       DiMustWriteLock(dentry);
9362 +       au_di(dentry)->di_bbot = bindex;
9363 +}
9364 +
9365 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9366 +{
9367 +       DiMustWriteLock(dentry);
9368 +       /* dbwh can be outside of btop - bbot range */
9369 +       au_di(dentry)->di_bwh = bindex;
9370 +}
9371 +
9372 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9373 +{
9374 +       DiMustWriteLock(dentry);
9375 +       au_di(dentry)->di_bdiropq = bindex;
9376 +}
9377 +
9378 +/* ---------------------------------------------------------------------- */
9379 +
9380 +#ifdef CONFIG_AUFS_HNOTIFY
9381 +static inline void au_digen_dec(struct dentry *d)
9382 +{
9383 +       atomic_dec(&au_di(d)->di_generation);
9384 +}
9385 +
9386 +static inline void au_hn_di_reinit(struct dentry *dentry)
9387 +{
9388 +       dentry->d_fsdata = NULL;
9389 +}
9390 +#else
9391 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9392 +#endif /* CONFIG_AUFS_HNOTIFY */
9393 +
9394 +#endif /* __KERNEL__ */
9395 +#endif /* __AUFS_DENTRY_H__ */
9396 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9397 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9398 +++ linux/fs/aufs/dinfo.c       2022-12-17 09:21:34.796521861 +0100
9399 @@ -0,0 +1,555 @@
9400 +// SPDX-License-Identifier: GPL-2.0
9401 +/*
9402 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9403 + *
9404 + * This program is free software; you can redistribute it and/or modify
9405 + * it under the terms of the GNU General Public License as published by
9406 + * the Free Software Foundation; either version 2 of the License, or
9407 + * (at your option) any later version.
9408 + *
9409 + * This program is distributed in the hope that it will be useful,
9410 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9411 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9412 + * GNU General Public License for more details.
9413 + *
9414 + * You should have received a copy of the GNU General Public License
9415 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9416 + */
9417 +
9418 +/*
9419 + * dentry private data
9420 + */
9421 +
9422 +#include "aufs.h"
9423 +
9424 +void au_di_init_once(void *_dinfo)
9425 +{
9426 +       struct au_dinfo *dinfo = _dinfo;
9427 +
9428 +       au_rw_init(&dinfo->di_rwsem);
9429 +}
9430 +
9431 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9432 +{
9433 +       struct au_dinfo *dinfo;
9434 +       int nbr, i;
9435 +
9436 +       dinfo = au_cache_alloc_dinfo();
9437 +       if (unlikely(!dinfo))
9438 +               goto out;
9439 +
9440 +       nbr = au_sbbot(sb) + 1;
9441 +       if (nbr <= 0)
9442 +               nbr = 1;
9443 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9444 +       if (dinfo->di_hdentry) {
9445 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9446 +               dinfo->di_btop = -1;
9447 +               dinfo->di_bbot = -1;
9448 +               dinfo->di_bwh = -1;
9449 +               dinfo->di_bdiropq = -1;
9450 +               dinfo->di_tmpfile = 0;
9451 +               for (i = 0; i < nbr; i++)
9452 +                       dinfo->di_hdentry[i].hd_id = -1;
9453 +               dinfo->di_htmpfile = NULL;
9454 +               goto out;
9455 +       }
9456 +
9457 +       au_cache_free_dinfo(dinfo);
9458 +       dinfo = NULL;
9459 +
9460 +out:
9461 +       return dinfo;
9462 +}
9463 +
9464 +void au_di_free(struct au_dinfo *dinfo)
9465 +{
9466 +       struct au_hdentry *p;
9467 +       aufs_bindex_t bbot, bindex;
9468 +
9469 +       /* dentry may not be revalidated */
9470 +       bindex = dinfo->di_btop;
9471 +       if (bindex >= 0) {
9472 +               bbot = dinfo->di_bbot;
9473 +               p = au_hdentry(dinfo, bindex);
9474 +               while (bindex++ <= bbot)
9475 +                       au_hdput(p++);
9476 +       }
9477 +       au_kfree_try_rcu(dinfo->di_hdentry);
9478 +       au_cache_free_dinfo(dinfo);
9479 +}
9480 +
9481 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9482 +{
9483 +       struct au_hdentry *p;
9484 +       aufs_bindex_t bi;
9485 +
9486 +       AuRwMustWriteLock(&a->di_rwsem);
9487 +       AuRwMustWriteLock(&b->di_rwsem);
9488 +
9489 +#define DiSwap(v, name)                                \
9490 +       do {                                    \
9491 +               v = a->di_##name;               \
9492 +               a->di_##name = b->di_##name;    \
9493 +               b->di_##name = v;               \
9494 +       } while (0)
9495 +
9496 +       DiSwap(p, hdentry);
9497 +       DiSwap(bi, btop);
9498 +       DiSwap(bi, bbot);
9499 +       DiSwap(bi, bwh);
9500 +       DiSwap(bi, bdiropq);
9501 +       /* smp_mb(); */
9502 +
9503 +#undef DiSwap
9504 +}
9505 +
9506 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9507 +{
9508 +       AuRwMustWriteLock(&dst->di_rwsem);
9509 +       AuRwMustWriteLock(&src->di_rwsem);
9510 +
9511 +       dst->di_btop = src->di_btop;
9512 +       dst->di_bbot = src->di_bbot;
9513 +       dst->di_bwh = src->di_bwh;
9514 +       dst->di_bdiropq = src->di_bdiropq;
9515 +       /* smp_mb(); */
9516 +}
9517 +
9518 +int au_di_init(struct dentry *dentry)
9519 +{
9520 +       int err;
9521 +       struct super_block *sb;
9522 +       struct au_dinfo *dinfo;
9523 +
9524 +       err = 0;
9525 +       sb = dentry->d_sb;
9526 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9527 +       if (dinfo) {
9528 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9529 +               /* smp_mb(); */ /* atomic_set */
9530 +               dentry->d_fsdata = dinfo;
9531 +       } else
9532 +               err = -ENOMEM;
9533 +
9534 +       return err;
9535 +}
9536 +
9537 +void au_di_fin(struct dentry *dentry)
9538 +{
9539 +       struct au_dinfo *dinfo;
9540 +
9541 +       dinfo = au_di(dentry);
9542 +       AuRwDestroy(&dinfo->di_rwsem);
9543 +       au_di_free(dinfo);
9544 +}
9545 +
9546 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9547 +{
9548 +       int err, sz;
9549 +       struct au_hdentry *hdp;
9550 +
9551 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9552 +
9553 +       err = -ENOMEM;
9554 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9555 +       if (!sz)
9556 +               sz = sizeof(*hdp);
9557 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9558 +                          may_shrink);
9559 +       if (hdp) {
9560 +               dinfo->di_hdentry = hdp;
9561 +               err = 0;
9562 +       }
9563 +
9564 +       return err;
9565 +}
9566 +
9567 +/* ---------------------------------------------------------------------- */
9568 +
9569 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9570 +{
9571 +       switch (lsc) {
9572 +       case AuLsc_DI_CHILD:
9573 +               ii_write_lock_child(inode);
9574 +               break;
9575 +       case AuLsc_DI_CHILD2:
9576 +               ii_write_lock_child2(inode);
9577 +               break;
9578 +       case AuLsc_DI_CHILD3:
9579 +               ii_write_lock_child3(inode);
9580 +               break;
9581 +       case AuLsc_DI_PARENT:
9582 +               ii_write_lock_parent(inode);
9583 +               break;
9584 +       case AuLsc_DI_PARENT2:
9585 +               ii_write_lock_parent2(inode);
9586 +               break;
9587 +       case AuLsc_DI_PARENT3:
9588 +               ii_write_lock_parent3(inode);
9589 +               break;
9590 +       default:
9591 +               BUG();
9592 +       }
9593 +}
9594 +
9595 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9596 +{
9597 +       switch (lsc) {
9598 +       case AuLsc_DI_CHILD:
9599 +               ii_read_lock_child(inode);
9600 +               break;
9601 +       case AuLsc_DI_CHILD2:
9602 +               ii_read_lock_child2(inode);
9603 +               break;
9604 +       case AuLsc_DI_CHILD3:
9605 +               ii_read_lock_child3(inode);
9606 +               break;
9607 +       case AuLsc_DI_PARENT:
9608 +               ii_read_lock_parent(inode);
9609 +               break;
9610 +       case AuLsc_DI_PARENT2:
9611 +               ii_read_lock_parent2(inode);
9612 +               break;
9613 +       case AuLsc_DI_PARENT3:
9614 +               ii_read_lock_parent3(inode);
9615 +               break;
9616 +       default:
9617 +               BUG();
9618 +       }
9619 +}
9620 +
9621 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9622 +{
9623 +       struct inode *inode;
9624 +
9625 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9626 +       if (d_really_is_positive(d)) {
9627 +               inode = d_inode(d);
9628 +               if (au_ftest_lock(flags, IW))
9629 +                       do_ii_write_lock(inode, lsc);
9630 +               else if (au_ftest_lock(flags, IR))
9631 +                       do_ii_read_lock(inode, lsc);
9632 +       }
9633 +}
9634 +
9635 +void di_read_unlock(struct dentry *d, int flags)
9636 +{
9637 +       struct inode *inode;
9638 +
9639 +       if (d_really_is_positive(d)) {
9640 +               inode = d_inode(d);
9641 +               if (au_ftest_lock(flags, IW)) {
9642 +                       au_dbg_verify_dinode(d);
9643 +                       ii_write_unlock(inode);
9644 +               } else if (au_ftest_lock(flags, IR)) {
9645 +                       au_dbg_verify_dinode(d);
9646 +                       ii_read_unlock(inode);
9647 +               }
9648 +       }
9649 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9650 +}
9651 +
9652 +void di_downgrade_lock(struct dentry *d, int flags)
9653 +{
9654 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9655 +               ii_downgrade_lock(d_inode(d));
9656 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9657 +}
9658 +
9659 +void di_write_lock(struct dentry *d, unsigned int lsc)
9660 +{
9661 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9662 +       if (d_really_is_positive(d))
9663 +               do_ii_write_lock(d_inode(d), lsc);
9664 +}
9665 +
9666 +void di_write_unlock(struct dentry *d)
9667 +{
9668 +       au_dbg_verify_dinode(d);
9669 +       if (d_really_is_positive(d))
9670 +               ii_write_unlock(d_inode(d));
9671 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9672 +}
9673 +
9674 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9675 +{
9676 +       AuDebugOn(d1 == d2
9677 +                 || d_inode(d1) == d_inode(d2)
9678 +                 || d1->d_sb != d2->d_sb);
9679 +
9680 +       if ((isdir && au_test_subdir(d1, d2))
9681 +           || d1 < d2) {
9682 +               di_write_lock_child(d1);
9683 +               di_write_lock_child2(d2);
9684 +       } else {
9685 +               di_write_lock_child(d2);
9686 +               di_write_lock_child2(d1);
9687 +       }
9688 +}
9689 +
9690 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9691 +{
9692 +       AuDebugOn(d1 == d2
9693 +                 || d_inode(d1) == d_inode(d2)
9694 +                 || d1->d_sb != d2->d_sb);
9695 +
9696 +       if ((isdir && au_test_subdir(d1, d2))
9697 +           || d1 < d2) {
9698 +               di_write_lock_parent(d1);
9699 +               di_write_lock_parent2(d2);
9700 +       } else {
9701 +               di_write_lock_parent(d2);
9702 +               di_write_lock_parent2(d1);
9703 +       }
9704 +}
9705 +
9706 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9707 +{
9708 +       di_write_unlock(d1);
9709 +       if (d_inode(d1) == d_inode(d2))
9710 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9711 +       else
9712 +               di_write_unlock(d2);
9713 +}
9714 +
9715 +/* ---------------------------------------------------------------------- */
9716 +
9717 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9718 +{
9719 +       struct dentry *d;
9720 +
9721 +       DiMustAnyLock(dentry);
9722 +
9723 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9724 +               return NULL;
9725 +       AuDebugOn(bindex < 0);
9726 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9727 +       AuDebugOn(d && au_dcount(d) <= 0);
9728 +       return d;
9729 +}
9730 +
9731 +/*
9732 + * extended version of au_h_dptr().
9733 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9734 + * error.
9735 + */
9736 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9737 +{
9738 +       struct dentry *h_dentry;
9739 +       struct inode *inode, *h_inode;
9740 +
9741 +       AuDebugOn(d_really_is_negative(dentry));
9742 +
9743 +       h_dentry = NULL;
9744 +       if (au_dbtop(dentry) <= bindex
9745 +           && bindex <= au_dbbot(dentry))
9746 +               h_dentry = au_h_dptr(dentry, bindex);
9747 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9748 +               dget(h_dentry);
9749 +               goto out; /* success */
9750 +       }
9751 +
9752 +       inode = d_inode(dentry);
9753 +       AuDebugOn(bindex < au_ibtop(inode));
9754 +       AuDebugOn(au_ibbot(inode) < bindex);
9755 +       h_inode = au_h_iptr(inode, bindex);
9756 +       h_dentry = d_find_alias(h_inode);
9757 +       if (h_dentry) {
9758 +               if (!IS_ERR(h_dentry)) {
9759 +                       if (!au_d_linkable(h_dentry))
9760 +                               goto out; /* success */
9761 +                       dput(h_dentry);
9762 +               } else
9763 +                       goto out;
9764 +       }
9765 +
9766 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9767 +               h_dentry = au_plink_lkup(inode, bindex);
9768 +               AuDebugOn(!h_dentry);
9769 +               if (!IS_ERR(h_dentry)) {
9770 +                       if (!au_d_hashed_positive(h_dentry))
9771 +                               goto out; /* success */
9772 +                       dput(h_dentry);
9773 +                       h_dentry = NULL;
9774 +               }
9775 +       }
9776 +
9777 +out:
9778 +       AuDbgDentry(h_dentry);
9779 +       return h_dentry;
9780 +}
9781 +
9782 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9783 +{
9784 +       aufs_bindex_t bbot, bwh;
9785 +
9786 +       bbot = au_dbbot(dentry);
9787 +       if (0 <= bbot) {
9788 +               bwh = au_dbwh(dentry);
9789 +               if (!bwh)
9790 +                       return bwh;
9791 +               if (0 < bwh && bwh < bbot)
9792 +                       return bwh - 1;
9793 +       }
9794 +       return bbot;
9795 +}
9796 +
9797 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9798 +{
9799 +       aufs_bindex_t bbot, bopq;
9800 +
9801 +       bbot = au_dbtail(dentry);
9802 +       if (0 <= bbot) {
9803 +               bopq = au_dbdiropq(dentry);
9804 +               if (0 <= bopq && bopq < bbot)
9805 +                       bbot = bopq;
9806 +       }
9807 +       return bbot;
9808 +}
9809 +
9810 +/* ---------------------------------------------------------------------- */
9811 +
9812 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9813 +                  struct dentry *h_dentry)
9814 +{
9815 +       struct au_dinfo *dinfo;
9816 +       struct au_hdentry *hd;
9817 +       struct au_branch *br;
9818 +
9819 +       DiMustWriteLock(dentry);
9820 +
9821 +       dinfo = au_di(dentry);
9822 +       hd = au_hdentry(dinfo, bindex);
9823 +       au_hdput(hd);
9824 +       hd->hd_dentry = h_dentry;
9825 +       if (h_dentry) {
9826 +               br = au_sbr(dentry->d_sb, bindex);
9827 +               hd->hd_id = br->br_id;
9828 +       }
9829 +}
9830 +
9831 +int au_dbrange_test(struct dentry *dentry)
9832 +{
9833 +       int err;
9834 +       aufs_bindex_t btop, bbot;
9835 +
9836 +       err = 0;
9837 +       btop = au_dbtop(dentry);
9838 +       bbot = au_dbbot(dentry);
9839 +       if (btop >= 0)
9840 +               AuDebugOn(bbot < 0 && btop > bbot);
9841 +       else {
9842 +               err = -EIO;
9843 +               AuDebugOn(bbot >= 0);
9844 +       }
9845 +
9846 +       return err;
9847 +}
9848 +
9849 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9850 +{
9851 +       int err;
9852 +
9853 +       err = 0;
9854 +       if (unlikely(au_digen(dentry) != sigen
9855 +                    || au_iigen_test(d_inode(dentry), sigen)))
9856 +               err = -EIO;
9857 +
9858 +       return err;
9859 +}
9860 +
9861 +void au_update_digen(struct dentry *dentry)
9862 +{
9863 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9864 +       /* smp_mb(); */ /* atomic_set */
9865 +}
9866 +
9867 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9868 +{
9869 +       struct au_dinfo *dinfo;
9870 +       struct dentry *h_d;
9871 +       struct au_hdentry *hdp;
9872 +       aufs_bindex_t bindex, bbot;
9873 +
9874 +       DiMustWriteLock(dentry);
9875 +
9876 +       dinfo = au_di(dentry);
9877 +       if (!dinfo || dinfo->di_btop < 0)
9878 +               return;
9879 +
9880 +       if (do_put_zero) {
9881 +               bbot = dinfo->di_bbot;
9882 +               bindex = dinfo->di_btop;
9883 +               hdp = au_hdentry(dinfo, bindex);
9884 +               for (; bindex <= bbot; bindex++, hdp++) {
9885 +                       h_d = hdp->hd_dentry;
9886 +                       if (h_d && d_is_negative(h_d))
9887 +                               au_set_h_dptr(dentry, bindex, NULL);
9888 +               }
9889 +       }
9890 +
9891 +       dinfo->di_btop = 0;
9892 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9893 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9894 +               if (hdp->hd_dentry)
9895 +                       break;
9896 +       if (dinfo->di_btop > dinfo->di_bbot) {
9897 +               dinfo->di_btop = -1;
9898 +               dinfo->di_bbot = -1;
9899 +               return;
9900 +       }
9901 +
9902 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9903 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9904 +               if (hdp->hd_dentry)
9905 +                       break;
9906 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9907 +}
9908 +
9909 +void au_update_dbtop(struct dentry *dentry)
9910 +{
9911 +       aufs_bindex_t bindex, bbot;
9912 +       struct dentry *h_dentry;
9913 +
9914 +       bbot = au_dbbot(dentry);
9915 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9916 +               h_dentry = au_h_dptr(dentry, bindex);
9917 +               if (!h_dentry)
9918 +                       continue;
9919 +               if (d_is_positive(h_dentry)) {
9920 +                       au_set_dbtop(dentry, bindex);
9921 +                       return;
9922 +               }
9923 +               au_set_h_dptr(dentry, bindex, NULL);
9924 +       }
9925 +}
9926 +
9927 +void au_update_dbbot(struct dentry *dentry)
9928 +{
9929 +       aufs_bindex_t bindex, btop;
9930 +       struct dentry *h_dentry;
9931 +
9932 +       btop = au_dbtop(dentry);
9933 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9934 +               h_dentry = au_h_dptr(dentry, bindex);
9935 +               if (!h_dentry)
9936 +                       continue;
9937 +               if (d_is_positive(h_dentry)) {
9938 +                       au_set_dbbot(dentry, bindex);
9939 +                       return;
9940 +               }
9941 +               au_set_h_dptr(dentry, bindex, NULL);
9942 +       }
9943 +}
9944 +
9945 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9946 +{
9947 +       aufs_bindex_t bindex, bbot;
9948 +
9949 +       bbot = au_dbbot(dentry);
9950 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9951 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9952 +                       return bindex;
9953 +       return -1;
9954 +}
9955 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9956 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9957 +++ linux/fs/aufs/dir.c 2023-04-24 08:13:40.969576115 +0200
9958 @@ -0,0 +1,765 @@
9959 +// SPDX-License-Identifier: GPL-2.0
9960 +/*
9961 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9962 + *
9963 + * This program is free software; you can redistribute it and/or modify
9964 + * it under the terms of the GNU General Public License as published by
9965 + * the Free Software Foundation; either version 2 of the License, or
9966 + * (at your option) any later version.
9967 + *
9968 + * This program is distributed in the hope that it will be useful,
9969 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9970 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9971 + * GNU General Public License for more details.
9972 + *
9973 + * You should have received a copy of the GNU General Public License
9974 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9975 + */
9976 +
9977 +/*
9978 + * directory operations
9979 + */
9980 +
9981 +#include <linux/fs_stack.h>
9982 +#include <linux/iversion.h>
9983 +#include "aufs.h"
9984 +
9985 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9986 +{
9987 +       unsigned int nlink;
9988 +
9989 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9990 +
9991 +       nlink = dir->i_nlink;
9992 +       nlink += h_dir->i_nlink - 2;
9993 +       if (h_dir->i_nlink < 2)
9994 +               nlink += 2;
9995 +       smp_mb(); /* for i_nlink */
9996 +       /* 0 can happen in revaliding */
9997 +       set_nlink(dir, nlink);
9998 +}
9999 +
10000 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10001 +{
10002 +       unsigned int nlink;
10003 +
10004 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10005 +
10006 +       nlink = dir->i_nlink;
10007 +       nlink -= h_dir->i_nlink - 2;
10008 +       if (h_dir->i_nlink < 2)
10009 +               nlink -= 2;
10010 +       smp_mb(); /* for i_nlink */
10011 +       /* nlink == 0 means the branch-fs is broken */
10012 +       set_nlink(dir, nlink);
10013 +}
10014 +
10015 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10016 +{
10017 +       loff_t sz;
10018 +       aufs_bindex_t bindex, bbot;
10019 +       struct file *h_file;
10020 +       struct dentry *h_dentry;
10021 +
10022 +       sz = 0;
10023 +       if (file) {
10024 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10025 +
10026 +               bbot = au_fbbot_dir(file);
10027 +               for (bindex = au_fbtop(file);
10028 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10029 +                    bindex++) {
10030 +                       h_file = au_hf_dir(file, bindex);
10031 +                       if (h_file && file_inode(h_file))
10032 +                               sz += vfsub_f_size_read(h_file);
10033 +               }
10034 +       } else {
10035 +               AuDebugOn(!dentry);
10036 +               AuDebugOn(!d_is_dir(dentry));
10037 +
10038 +               bbot = au_dbtaildir(dentry);
10039 +               for (bindex = au_dbtop(dentry);
10040 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10041 +                    bindex++) {
10042 +                       h_dentry = au_h_dptr(dentry, bindex);
10043 +                       if (h_dentry && d_is_positive(h_dentry))
10044 +                               sz += i_size_read(d_inode(h_dentry));
10045 +               }
10046 +       }
10047 +       if (sz < KMALLOC_MAX_SIZE)
10048 +               sz = roundup_pow_of_two(sz);
10049 +       if (sz > KMALLOC_MAX_SIZE)
10050 +               sz = KMALLOC_MAX_SIZE;
10051 +       else if (sz < NAME_MAX) {
10052 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10053 +               sz = AUFS_RDBLK_DEF;
10054 +       }
10055 +       return sz;
10056 +}
10057 +
10058 +struct au_dir_ts_arg {
10059 +       struct dentry *dentry;
10060 +       aufs_bindex_t brid;
10061 +};
10062 +
10063 +static void au_do_dir_ts(void *arg)
10064 +{
10065 +       struct au_dir_ts_arg *a = arg;
10066 +       struct au_dtime dt;
10067 +       struct path h_path;
10068 +       struct inode *dir, *h_dir;
10069 +       struct super_block *sb;
10070 +       struct au_branch *br;
10071 +       struct au_hinode *hdir;
10072 +       int err;
10073 +       aufs_bindex_t btop, bindex;
10074 +
10075 +       sb = a->dentry->d_sb;
10076 +       if (d_really_is_negative(a->dentry))
10077 +               goto out;
10078 +       /* no dir->i_mutex lock */
10079 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10080 +
10081 +       dir = d_inode(a->dentry);
10082 +       btop = au_ibtop(dir);
10083 +       bindex = au_br_index(sb, a->brid);
10084 +       if (bindex < btop)
10085 +               goto out_unlock;
10086 +
10087 +       br = au_sbr(sb, bindex);
10088 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10089 +       if (!h_path.dentry)
10090 +               goto out_unlock;
10091 +       h_path.mnt = au_br_mnt(br);
10092 +       au_dtime_store(&dt, a->dentry, &h_path);
10093 +
10094 +       br = au_sbr(sb, btop);
10095 +       if (!au_br_writable(br->br_perm))
10096 +               goto out_unlock;
10097 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10098 +       h_path.mnt = au_br_mnt(br);
10099 +       err = vfsub_mnt_want_write(h_path.mnt);
10100 +       if (err)
10101 +               goto out_unlock;
10102 +       hdir = au_hi(dir, btop);
10103 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10104 +       h_dir = au_h_iptr(dir, btop);
10105 +       if (h_dir->i_nlink
10106 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10107 +               dt.dt_h_path = h_path;
10108 +               au_dtime_revert(&dt);
10109 +       }
10110 +       au_hn_inode_unlock(hdir);
10111 +       vfsub_mnt_drop_write(h_path.mnt);
10112 +       au_cpup_attr_timesizes(dir);
10113 +
10114 +out_unlock:
10115 +       aufs_read_unlock(a->dentry, AuLock_DW);
10116 +out:
10117 +       dput(a->dentry);
10118 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10119 +       au_kfree_try_rcu(arg);
10120 +}
10121 +
10122 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10123 +{
10124 +       int perm, wkq_err;
10125 +       aufs_bindex_t btop;
10126 +       struct au_dir_ts_arg *arg;
10127 +       struct dentry *dentry;
10128 +       struct super_block *sb;
10129 +
10130 +       IMustLock(dir);
10131 +
10132 +       dentry = d_find_any_alias(dir);
10133 +       AuDebugOn(!dentry);
10134 +       sb = dentry->d_sb;
10135 +       btop = au_ibtop(dir);
10136 +       if (btop == bindex) {
10137 +               au_cpup_attr_timesizes(dir);
10138 +               goto out;
10139 +       }
10140 +
10141 +       perm = au_sbr_perm(sb, btop);
10142 +       if (!au_br_writable(perm))
10143 +               goto out;
10144 +
10145 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10146 +       if (!arg)
10147 +               goto out;
10148 +
10149 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10150 +       arg->brid = au_sbr_id(sb, bindex);
10151 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10152 +       if (unlikely(wkq_err)) {
10153 +               pr_err("wkq %d\n", wkq_err);
10154 +               dput(dentry);
10155 +               au_kfree_try_rcu(arg);
10156 +       }
10157 +
10158 +out:
10159 +       dput(dentry);
10160 +}
10161 +
10162 +/* ---------------------------------------------------------------------- */
10163 +
10164 +static int reopen_dir(struct file *file)
10165 +{
10166 +       int err;
10167 +       unsigned int flags;
10168 +       aufs_bindex_t bindex, btail, btop;
10169 +       struct dentry *dentry, *h_dentry;
10170 +       struct file *h_file;
10171 +
10172 +       /* open all lower dirs */
10173 +       dentry = file->f_path.dentry;
10174 +       btop = au_dbtop(dentry);
10175 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10176 +               au_set_h_fptr(file, bindex, NULL);
10177 +       au_set_fbtop(file, btop);
10178 +
10179 +       btail = au_dbtaildir(dentry);
10180 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10181 +               au_set_h_fptr(file, bindex, NULL);
10182 +       au_set_fbbot_dir(file, btail);
10183 +
10184 +       flags = vfsub_file_flags(file);
10185 +       for (bindex = btop; bindex <= btail; bindex++) {
10186 +               h_dentry = au_h_dptr(dentry, bindex);
10187 +               if (!h_dentry)
10188 +                       continue;
10189 +               h_file = au_hf_dir(file, bindex);
10190 +               if (h_file)
10191 +                       continue;
10192 +
10193 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10194 +               err = PTR_ERR(h_file);
10195 +               if (IS_ERR(h_file))
10196 +                       goto out; /* close all? */
10197 +               au_set_h_fptr(file, bindex, h_file);
10198 +       }
10199 +       au_update_figen(file);
10200 +       /* todo: necessary? */
10201 +       /* file->f_ra = h_file->f_ra; */
10202 +       err = 0;
10203 +
10204 +out:
10205 +       return err;
10206 +}
10207 +
10208 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10209 +{
10210 +       int err;
10211 +       aufs_bindex_t bindex, btail;
10212 +       struct dentry *dentry, *h_dentry;
10213 +       struct vfsmount *mnt;
10214 +
10215 +       FiMustWriteLock(file);
10216 +       AuDebugOn(h_file);
10217 +
10218 +       err = 0;
10219 +       mnt = file->f_path.mnt;
10220 +       dentry = file->f_path.dentry;
10221 +       file->f_version = inode_query_iversion(d_inode(dentry));
10222 +       bindex = au_dbtop(dentry);
10223 +       au_set_fbtop(file, bindex);
10224 +       btail = au_dbtaildir(dentry);
10225 +       au_set_fbbot_dir(file, btail);
10226 +       for (; !err && bindex <= btail; bindex++) {
10227 +               h_dentry = au_h_dptr(dentry, bindex);
10228 +               if (!h_dentry)
10229 +                       continue;
10230 +
10231 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10232 +               if (unlikely(err))
10233 +                       break;
10234 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10235 +               if (IS_ERR(h_file)) {
10236 +                       err = PTR_ERR(h_file);
10237 +                       break;
10238 +               }
10239 +               au_set_h_fptr(file, bindex, h_file);
10240 +       }
10241 +       au_update_figen(file);
10242 +       /* todo: necessary? */
10243 +       /* file->f_ra = h_file->f_ra; */
10244 +       if (!err)
10245 +               return 0; /* success */
10246 +
10247 +       /* close all */
10248 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10249 +               au_set_h_fptr(file, bindex, NULL);
10250 +       au_set_fbtop(file, -1);
10251 +       au_set_fbbot_dir(file, -1);
10252 +
10253 +       return err;
10254 +}
10255 +
10256 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10257 +                        struct file *file)
10258 +{
10259 +       int err;
10260 +       struct super_block *sb;
10261 +       struct au_fidir *fidir;
10262 +
10263 +       err = -ENOMEM;
10264 +       sb = file->f_path.dentry->d_sb;
10265 +       si_read_lock(sb, AuLock_FLUSH);
10266 +       fidir = au_fidir_alloc(sb);
10267 +       if (fidir) {
10268 +               struct au_do_open_args args = {
10269 +                       .open   = do_open_dir,
10270 +                       .fidir  = fidir
10271 +               };
10272 +               err = au_do_open(file, &args);
10273 +               if (unlikely(err))
10274 +                       au_kfree_rcu(fidir);
10275 +       }
10276 +       si_read_unlock(sb);
10277 +       return err;
10278 +}
10279 +
10280 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10281 +                           struct file *file)
10282 +{
10283 +       struct au_vdir *vdir_cache;
10284 +       struct au_finfo *finfo;
10285 +       struct au_fidir *fidir;
10286 +       struct au_hfile *hf;
10287 +       aufs_bindex_t bindex, bbot;
10288 +
10289 +       finfo = au_fi(file);
10290 +       fidir = finfo->fi_hdir;
10291 +       if (fidir) {
10292 +               au_hbl_del(&finfo->fi_hlist,
10293 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10294 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10295 +               if (vdir_cache)
10296 +                       au_vdir_free(vdir_cache);
10297 +
10298 +               bindex = finfo->fi_btop;
10299 +               if (bindex >= 0) {
10300 +                       hf = fidir->fd_hfile + bindex;
10301 +                       /*
10302 +                        * calls fput() instead of filp_close(),
10303 +                        * since no dnotify or lock for the lower file.
10304 +                        */
10305 +                       bbot = fidir->fd_bbot;
10306 +                       for (; bindex <= bbot; bindex++, hf++)
10307 +                               if (hf->hf_file)
10308 +                                       au_hfput(hf, /*execed*/0);
10309 +               }
10310 +               au_kfree_rcu(fidir);
10311 +               finfo->fi_hdir = NULL;
10312 +       }
10313 +       au_finfo_fin(file);
10314 +       return 0;
10315 +}
10316 +
10317 +/* ---------------------------------------------------------------------- */
10318 +
10319 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10320 +{
10321 +       int err;
10322 +       aufs_bindex_t bindex, bbot;
10323 +       struct file *h_file;
10324 +
10325 +       err = 0;
10326 +       bbot = au_fbbot_dir(file);
10327 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10328 +               h_file = au_hf_dir(file, bindex);
10329 +               if (h_file)
10330 +                       err = vfsub_flush(h_file, id);
10331 +       }
10332 +       return err;
10333 +}
10334 +
10335 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10336 +{
10337 +       return au_do_flush(file, id, au_do_flush_dir);
10338 +}
10339 +
10340 +/* ---------------------------------------------------------------------- */
10341 +
10342 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10343 +{
10344 +       int err;
10345 +       aufs_bindex_t bbot, bindex;
10346 +       struct inode *inode;
10347 +       struct super_block *sb;
10348 +
10349 +       err = 0;
10350 +       sb = dentry->d_sb;
10351 +       inode = d_inode(dentry);
10352 +       IMustLock(inode);
10353 +       bbot = au_dbbot(dentry);
10354 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10355 +               struct path h_path;
10356 +
10357 +               if (au_test_ro(sb, bindex, inode))
10358 +                       continue;
10359 +               h_path.dentry = au_h_dptr(dentry, bindex);
10360 +               if (!h_path.dentry)
10361 +                       continue;
10362 +
10363 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10364 +               err = vfsub_fsync(NULL, &h_path, datasync);
10365 +       }
10366 +
10367 +       return err;
10368 +}
10369 +
10370 +static int au_do_fsync_dir(struct file *file, int datasync)
10371 +{
10372 +       int err;
10373 +       aufs_bindex_t bbot, bindex;
10374 +       struct file *h_file;
10375 +       struct super_block *sb;
10376 +       struct inode *inode;
10377 +
10378 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10379 +       if (unlikely(err))
10380 +               goto out;
10381 +
10382 +       inode = file_inode(file);
10383 +       sb = inode->i_sb;
10384 +       bbot = au_fbbot_dir(file);
10385 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10386 +               h_file = au_hf_dir(file, bindex);
10387 +               if (!h_file || au_test_ro(sb, bindex, inode))
10388 +                       continue;
10389 +
10390 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10391 +       }
10392 +
10393 +out:
10394 +       return err;
10395 +}
10396 +
10397 +/*
10398 + * @file may be NULL
10399 + */
10400 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10401 +                         int datasync)
10402 +{
10403 +       int err;
10404 +       struct dentry *dentry;
10405 +       struct inode *inode;
10406 +       struct super_block *sb;
10407 +
10408 +       err = 0;
10409 +       dentry = file->f_path.dentry;
10410 +       inode = d_inode(dentry);
10411 +       inode_lock(inode);
10412 +       sb = dentry->d_sb;
10413 +       si_noflush_read_lock(sb);
10414 +       if (file)
10415 +               err = au_do_fsync_dir(file, datasync);
10416 +       else {
10417 +               di_write_lock_child(dentry);
10418 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10419 +       }
10420 +       au_cpup_attr_timesizes(inode);
10421 +       di_write_unlock(dentry);
10422 +       if (file)
10423 +               fi_write_unlock(file);
10424 +
10425 +       si_read_unlock(sb);
10426 +       inode_unlock(inode);
10427 +       return err;
10428 +}
10429 +
10430 +/* ---------------------------------------------------------------------- */
10431 +
10432 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10433 +{
10434 +       int err;
10435 +       struct dentry *dentry;
10436 +       struct inode *inode, *h_inode;
10437 +       struct super_block *sb;
10438 +
10439 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10440 +
10441 +       dentry = file->f_path.dentry;
10442 +       inode = d_inode(dentry);
10443 +       IMustLock(inode);
10444 +
10445 +       sb = dentry->d_sb;
10446 +       si_read_lock(sb, AuLock_FLUSH);
10447 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10448 +       if (unlikely(err))
10449 +               goto out;
10450 +       err = au_alive_dir(dentry);
10451 +       if (!err)
10452 +               err = au_vdir_init(file);
10453 +       di_downgrade_lock(dentry, AuLock_IR);
10454 +       if (unlikely(err))
10455 +               goto out_unlock;
10456 +
10457 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10458 +       if (!au_test_nfsd()) {
10459 +               err = au_vdir_fill_de(file, ctx);
10460 +               fsstack_copy_attr_atime(inode, h_inode);
10461 +       } else {
10462 +               /*
10463 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10464 +                * encode_fh() and others.
10465 +                */
10466 +               atomic_inc(&h_inode->i_count);
10467 +               di_read_unlock(dentry, AuLock_IR);
10468 +               si_read_unlock(sb);
10469 +               err = au_vdir_fill_de(file, ctx);
10470 +               fsstack_copy_attr_atime(inode, h_inode);
10471 +               fi_write_unlock(file);
10472 +               iput(h_inode);
10473 +
10474 +               AuTraceErr(err);
10475 +               return err;
10476 +       }
10477 +
10478 +out_unlock:
10479 +       di_read_unlock(dentry, AuLock_IR);
10480 +       fi_write_unlock(file);
10481 +out:
10482 +       si_read_unlock(sb);
10483 +       return err;
10484 +}
10485 +
10486 +/* ---------------------------------------------------------------------- */
10487 +
10488 +#define AuTestEmpty_WHONLY     1
10489 +#define AuTestEmpty_CALLED     (1 << 1)
10490 +#define AuTestEmpty_SHWH       (1 << 2)
10491 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10492 +#define au_fset_testempty(flags, name) \
10493 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10494 +#define au_fclr_testempty(flags, name) \
10495 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10496 +
10497 +#ifndef CONFIG_AUFS_SHWH
10498 +#undef AuTestEmpty_SHWH
10499 +#define AuTestEmpty_SHWH       0
10500 +#endif
10501 +
10502 +struct test_empty_arg {
10503 +       struct dir_context ctx;
10504 +       struct au_nhash *whlist;
10505 +       unsigned int flags;
10506 +       int err;
10507 +       aufs_bindex_t bindex;
10508 +};
10509 +
10510 +static bool test_empty_cb(struct dir_context *ctx, const char *__name,
10511 +                         int namelen, loff_t offset __maybe_unused, u64 ino,
10512 +                         unsigned int d_type)
10513 +{
10514 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10515 +                                                 ctx);
10516 +       char *name = (void *)__name;
10517 +
10518 +       arg->err = 0;
10519 +       au_fset_testempty(arg->flags, CALLED);
10520 +       /* smp_mb(); */
10521 +       if (name[0] == '.'
10522 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10523 +               goto out; /* success */
10524 +
10525 +       if (namelen <= AUFS_WH_PFX_LEN
10526 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10527 +               if (au_ftest_testempty(arg->flags, WHONLY)
10528 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10529 +                       arg->err = -ENOTEMPTY;
10530 +               goto out;
10531 +       }
10532 +
10533 +       name += AUFS_WH_PFX_LEN;
10534 +       namelen -= AUFS_WH_PFX_LEN;
10535 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10536 +               arg->err = au_nhash_append_wh
10537 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10538 +                        au_ftest_testempty(arg->flags, SHWH));
10539 +
10540 +out:
10541 +       /* smp_mb(); */
10542 +       AuTraceErr(arg->err);
10543 +       return !arg->err;
10544 +}
10545 +
10546 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10547 +{
10548 +       int err;
10549 +       struct file *h_file;
10550 +       struct au_branch *br;
10551 +
10552 +       h_file = au_h_open(dentry, arg->bindex,
10553 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10554 +                          /*file*/NULL, /*force_wr*/0);
10555 +       err = PTR_ERR(h_file);
10556 +       if (IS_ERR(h_file))
10557 +               goto out;
10558 +
10559 +       err = 0;
10560 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10561 +           && !file_inode(h_file)->i_nlink)
10562 +               goto out_put;
10563 +
10564 +       do {
10565 +               arg->err = 0;
10566 +               au_fclr_testempty(arg->flags, CALLED);
10567 +               /* smp_mb(); */
10568 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10569 +               if (err >= 0)
10570 +                       err = arg->err;
10571 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10572 +
10573 +out_put:
10574 +       fput(h_file);
10575 +       br = au_sbr(dentry->d_sb, arg->bindex);
10576 +       au_lcnt_dec(&br->br_nfiles);
10577 +out:
10578 +       return err;
10579 +}
10580 +
10581 +struct do_test_empty_args {
10582 +       int *errp;
10583 +       struct dentry *dentry;
10584 +       struct test_empty_arg *arg;
10585 +};
10586 +
10587 +static void call_do_test_empty(void *args)
10588 +{
10589 +       struct do_test_empty_args *a = args;
10590 +       *a->errp = do_test_empty(a->dentry, a->arg);
10591 +}
10592 +
10593 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10594 +{
10595 +       int err, wkq_err;
10596 +       struct dentry *h_dentry;
10597 +       struct inode *h_inode;
10598 +       struct mnt_idmap *h_idmap;
10599 +
10600 +       h_idmap = au_sbr_idmap(dentry->d_sb, arg->bindex);
10601 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10602 +       h_inode = d_inode(h_dentry);
10603 +       /* todo: i_mode changes anytime? */
10604 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10605 +       err = au_test_h_perm_sio(h_idmap, h_inode, MAY_EXEC | MAY_READ);
10606 +       inode_unlock_shared(h_inode);
10607 +       if (!err)
10608 +               err = do_test_empty(dentry, arg);
10609 +       else {
10610 +               struct do_test_empty_args args = {
10611 +                       .errp   = &err,
10612 +                       .dentry = dentry,
10613 +                       .arg    = arg
10614 +               };
10615 +               unsigned int flags = arg->flags;
10616 +
10617 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10618 +               if (unlikely(wkq_err))
10619 +                       err = wkq_err;
10620 +               arg->flags = flags;
10621 +       }
10622 +
10623 +       return err;
10624 +}
10625 +
10626 +int au_test_empty_lower(struct dentry *dentry)
10627 +{
10628 +       int err;
10629 +       unsigned int rdhash;
10630 +       aufs_bindex_t bindex, btop, btail;
10631 +       struct au_nhash whlist;
10632 +       struct test_empty_arg arg = {
10633 +               .ctx = {
10634 +                       .actor = test_empty_cb
10635 +               }
10636 +       };
10637 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10638 +
10639 +       SiMustAnyLock(dentry->d_sb);
10640 +
10641 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10642 +       if (!rdhash)
10643 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10644 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10645 +       if (unlikely(err))
10646 +               goto out;
10647 +
10648 +       arg.flags = 0;
10649 +       arg.whlist = &whlist;
10650 +       btop = au_dbtop(dentry);
10651 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10652 +               au_fset_testempty(arg.flags, SHWH);
10653 +       test_empty = do_test_empty;
10654 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10655 +               test_empty = sio_test_empty;
10656 +       arg.bindex = btop;
10657 +       err = test_empty(dentry, &arg);
10658 +       if (unlikely(err))
10659 +               goto out_whlist;
10660 +
10661 +       au_fset_testempty(arg.flags, WHONLY);
10662 +       btail = au_dbtaildir(dentry);
10663 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10664 +               struct dentry *h_dentry;
10665 +
10666 +               h_dentry = au_h_dptr(dentry, bindex);
10667 +               if (h_dentry && d_is_positive(h_dentry)) {
10668 +                       arg.bindex = bindex;
10669 +                       err = test_empty(dentry, &arg);
10670 +               }
10671 +       }
10672 +
10673 +out_whlist:
10674 +       au_nhash_wh_free(&whlist);
10675 +out:
10676 +       return err;
10677 +}
10678 +
10679 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10680 +{
10681 +       int err;
10682 +       struct test_empty_arg arg = {
10683 +               .ctx = {
10684 +                       .actor = test_empty_cb
10685 +               }
10686 +       };
10687 +       aufs_bindex_t bindex, btail;
10688 +
10689 +       err = 0;
10690 +       arg.whlist = whlist;
10691 +       arg.flags = AuTestEmpty_WHONLY;
10692 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10693 +               au_fset_testempty(arg.flags, SHWH);
10694 +       btail = au_dbtaildir(dentry);
10695 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10696 +               struct dentry *h_dentry;
10697 +
10698 +               h_dentry = au_h_dptr(dentry, bindex);
10699 +               if (h_dentry && d_is_positive(h_dentry)) {
10700 +                       arg.bindex = bindex;
10701 +                       err = sio_test_empty(dentry, &arg);
10702 +               }
10703 +       }
10704 +
10705 +       return err;
10706 +}
10707 +
10708 +/* ---------------------------------------------------------------------- */
10709 +
10710 +const struct file_operations aufs_dir_fop = {
10711 +       .owner          = THIS_MODULE,
10712 +       .llseek         = default_llseek,
10713 +       .read           = generic_read_dir,
10714 +       .iterate_shared = aufs_iterate_shared,
10715 +       .unlocked_ioctl = aufs_ioctl_dir,
10716 +#ifdef CONFIG_COMPAT
10717 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10718 +#endif
10719 +       .open           = aufs_open_dir,
10720 +       .release        = aufs_release_dir,
10721 +       .flush          = aufs_flush_dir,
10722 +       .fsync          = aufs_fsync_dir
10723 +};
10724 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10725 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10726 +++ linux/fs/aufs/dir.h 2022-11-05 23:02:18.962555950 +0100
10727 @@ -0,0 +1,134 @@
10728 +/* SPDX-License-Identifier: GPL-2.0 */
10729 +/*
10730 + * Copyright (C) 2005-2022 Junjiro R. Okajima
10731 + *
10732 + * This program is free software; you can redistribute it and/or modify
10733 + * it under the terms of the GNU General Public License as published by
10734 + * the Free Software Foundation; either version 2 of the License, or
10735 + * (at your option) any later version.
10736 + *
10737 + * This program is distributed in the hope that it will be useful,
10738 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10739 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10740 + * GNU General Public License for more details.
10741 + *
10742 + * You should have received a copy of the GNU General Public License
10743 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10744 + */
10745 +
10746 +/*
10747 + * directory operations
10748 + */
10749 +
10750 +#ifndef __AUFS_DIR_H__
10751 +#define __AUFS_DIR_H__
10752 +
10753 +#ifdef __KERNEL__
10754 +
10755 +#include <linux/fs.h>
10756 +
10757 +/* ---------------------------------------------------------------------- */
10758 +
10759 +/* need to be faster and smaller */
10760 +
10761 +struct au_nhash {
10762 +       unsigned int            nh_num;
10763 +       struct hlist_head       *nh_head;
10764 +};
10765 +
10766 +struct au_vdir_destr {
10767 +       unsigned char   len;
10768 +       unsigned char   name[];
10769 +} __packed;
10770 +
10771 +struct au_vdir_dehstr {
10772 +       struct hlist_node       hash;
10773 +       struct au_vdir_destr    *str;
10774 +       struct rcu_head         rcu;
10775 +} ____cacheline_aligned_in_smp;
10776 +
10777 +struct au_vdir_de {
10778 +       ino_t                   de_ino;
10779 +       unsigned char           de_type;
10780 +       /* caution: packed */
10781 +       struct au_vdir_destr    de_str;
10782 +} __packed;
10783 +
10784 +struct au_vdir_wh {
10785 +       struct hlist_node       wh_hash;
10786 +#ifdef CONFIG_AUFS_SHWH
10787 +       ino_t                   wh_ino;
10788 +       aufs_bindex_t           wh_bindex;
10789 +       unsigned char           wh_type;
10790 +#else
10791 +       aufs_bindex_t           wh_bindex;
10792 +#endif
10793 +       /* caution: packed */
10794 +       struct au_vdir_destr    wh_str;
10795 +} __packed;
10796 +
10797 +union au_vdir_deblk_p {
10798 +       unsigned char           *deblk;
10799 +       struct au_vdir_de       *de;
10800 +};
10801 +
10802 +struct au_vdir {
10803 +       unsigned char   **vd_deblk;
10804 +       unsigned long   vd_nblk;
10805 +       struct {
10806 +               unsigned long           ul;
10807 +               union au_vdir_deblk_p   p;
10808 +       } vd_last;
10809 +
10810 +       u64             vd_version;
10811 +       unsigned int    vd_deblk_sz;
10812 +       unsigned long   vd_jiffy;
10813 +       struct rcu_head rcu;
10814 +} ____cacheline_aligned_in_smp;
10815 +
10816 +/* ---------------------------------------------------------------------- */
10817 +
10818 +/* dir.c */
10819 +extern const struct file_operations aufs_dir_fop;
10820 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10821 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10822 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10823 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10824 +int au_test_empty_lower(struct dentry *dentry);
10825 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10826 +
10827 +/* vdir.c */
10828 +unsigned int au_rdhash_est(loff_t sz);
10829 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10830 +void au_nhash_wh_free(struct au_nhash *whlist);
10831 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10832 +                           int limit);
10833 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10834 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10835 +                      unsigned int d_type, aufs_bindex_t bindex,
10836 +                      unsigned char shwh);
10837 +void au_vdir_free(struct au_vdir *vdir);
10838 +int au_vdir_init(struct file *file);
10839 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10840 +
10841 +/* ioctl.c */
10842 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10843 +
10844 +#ifdef CONFIG_AUFS_RDU
10845 +/* rdu.c */
10846 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10847 +#ifdef CONFIG_COMPAT
10848 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10849 +                        unsigned long arg);
10850 +#endif
10851 +#else
10852 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10853 +       unsigned int cmd, unsigned long arg)
10854 +#ifdef CONFIG_COMPAT
10855 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10856 +       unsigned int cmd, unsigned long arg)
10857 +#endif
10858 +#endif
10859 +
10860 +#endif /* __KERNEL__ */
10861 +#endif /* __AUFS_DIR_H__ */
10862 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10863 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10864 +++ linux/fs/aufs/dirren.c      2022-11-05 23:02:18.962555950 +0100
10865 @@ -0,0 +1,1315 @@
10866 +// SPDX-License-Identifier: GPL-2.0
10867 +/*
10868 + * Copyright (C) 2017-2022 Junjiro R. Okajima
10869 + *
10870 + * This program is free software; you can redistribute it and/or modify
10871 + * it under the terms of the GNU General Public License as published by
10872 + * the Free Software Foundation; either version 2 of the License, or
10873 + * (at your option) any later version.
10874 + *
10875 + * This program is distributed in the hope that it will be useful,
10876 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10877 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10878 + * GNU General Public License for more details.
10879 + *
10880 + * You should have received a copy of the GNU General Public License
10881 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10882 + */
10883 +
10884 +/*
10885 + * special handling in renaming a directory
10886 + * in order to support looking-up the before-renamed name on the lower readonly
10887 + * branches
10888 + */
10889 +
10890 +#include <linux/byteorder/generic.h>
10891 +#include "aufs.h"
10892 +
10893 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10894 +{
10895 +       int idx;
10896 +
10897 +       idx = au_dr_ihash(ent->dr_h_ino);
10898 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10899 +}
10900 +
10901 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10902 +{
10903 +       int ret, i;
10904 +       struct hlist_bl_head *hbl;
10905 +
10906 +       ret = 1;
10907 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10908 +               hbl = dr->dr_h_ino + i;
10909 +               hlist_bl_lock(hbl);
10910 +               ret &= hlist_bl_empty(hbl);
10911 +               hlist_bl_unlock(hbl);
10912 +       }
10913 +
10914 +       return ret;
10915 +}
10916 +
10917 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10918 +{
10919 +       struct au_dr_hino *found, *ent;
10920 +       struct hlist_bl_head *hbl;
10921 +       struct hlist_bl_node *pos;
10922 +       int idx;
10923 +
10924 +       found = NULL;
10925 +       idx = au_dr_ihash(ino);
10926 +       hbl = dr->dr_h_ino + idx;
10927 +       hlist_bl_lock(hbl);
10928 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10929 +               if (ent->dr_h_ino == ino) {
10930 +                       found = ent;
10931 +                       break;
10932 +               }
10933 +       hlist_bl_unlock(hbl);
10934 +
10935 +       return found;
10936 +}
10937 +
10938 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10939 +                       struct au_dr_hino *add_ent)
10940 +{
10941 +       int found, idx;
10942 +       struct hlist_bl_head *hbl;
10943 +       struct hlist_bl_node *pos;
10944 +       struct au_dr_hino *ent;
10945 +
10946 +       found = 0;
10947 +       idx = au_dr_ihash(ino);
10948 +       hbl = dr->dr_h_ino + idx;
10949 +#if 0 /* debug print */
10950 +       {
10951 +               struct hlist_bl_node *tmp;
10952 +
10953 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10954 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10955 +       }
10956 +#endif
10957 +       hlist_bl_lock(hbl);
10958 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10959 +               if (ent->dr_h_ino == ino) {
10960 +                       found = 1;
10961 +                       break;
10962 +               }
10963 +       if (!found && add_ent)
10964 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10965 +       hlist_bl_unlock(hbl);
10966 +
10967 +       if (!found && add_ent)
10968 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10969 +
10970 +       return found;
10971 +}
10972 +
10973 +void au_dr_hino_free(struct au_dr_br *dr)
10974 +{
10975 +       int i;
10976 +       struct hlist_bl_head *hbl;
10977 +       struct hlist_bl_node *pos, *tmp;
10978 +       struct au_dr_hino *ent;
10979 +
10980 +       /* SiMustWriteLock(sb); */
10981 +
10982 +       for (i = 0; i < AuDirren_NHASH; i++) {
10983 +               hbl = dr->dr_h_ino + i;
10984 +               /* no spinlock since sbinfo must be write-locked */
10985 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10986 +                       au_kfree_rcu(ent);
10987 +               INIT_HLIST_BL_HEAD(hbl);
10988 +       }
10989 +}
10990 +
10991 +/* returns the number of inodes or an error */
10992 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10993 +                           struct file *hinofile)
10994 +{
10995 +       int err, i;
10996 +       ssize_t ssz;
10997 +       loff_t pos, oldsize;
10998 +       __be64 u64;
10999 +       struct inode *hinoinode;
11000 +       struct hlist_bl_head *hbl;
11001 +       struct hlist_bl_node *n1, *n2;
11002 +       struct au_dr_hino *ent;
11003 +
11004 +       SiMustWriteLock(sb);
11005 +       AuDebugOn(!au_br_writable(br->br_perm));
11006 +
11007 +       hinoinode = file_inode(hinofile);
11008 +       oldsize = i_size_read(hinoinode);
11009 +
11010 +       err = 0;
11011 +       pos = 0;
11012 +       hbl = br->br_dirren.dr_h_ino;
11013 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11014 +               /* no bit-lock since sbinfo must be write-locked */
11015 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11016 +                       AuDbg("hi%llu, %pD2\n",
11017 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11018 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11019 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11020 +                       if (ssz == sizeof(u64))
11021 +                               continue;
11022 +
11023 +                       /* write error */
11024 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11025 +                       err = -ENOSPC;
11026 +                       if (ssz < 0)
11027 +                               err = ssz;
11028 +                       break;
11029 +               }
11030 +       }
11031 +       /* regardless the error */
11032 +       if (pos < oldsize) {
11033 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11034 +               AuTraceErr(err);
11035 +       }
11036 +
11037 +       AuTraceErr(err);
11038 +       return err;
11039 +}
11040 +
11041 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11042 +{
11043 +       int err, hidx;
11044 +       ssize_t ssz;
11045 +       size_t sz, n;
11046 +       loff_t pos;
11047 +       uint64_t u64;
11048 +       struct au_dr_hino *ent;
11049 +       struct inode *hinoinode;
11050 +       struct hlist_bl_head *hbl;
11051 +
11052 +       err = 0;
11053 +       pos = 0;
11054 +       hbl = dr->dr_h_ino;
11055 +       hinoinode = file_inode(hinofile);
11056 +       sz = i_size_read(hinoinode);
11057 +       AuDebugOn(sz % sizeof(u64));
11058 +       n = sz / sizeof(u64);
11059 +       while (n--) {
11060 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11061 +               if (unlikely(ssz != sizeof(u64))) {
11062 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11063 +                       err = -EINVAL;
11064 +                       if (ssz < 0)
11065 +                               err = ssz;
11066 +                       goto out_free;
11067 +               }
11068 +
11069 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11070 +               if (!ent) {
11071 +                       err = -ENOMEM;
11072 +                       AuTraceErr(err);
11073 +                       goto out_free;
11074 +               }
11075 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11076 +               AuDbg("hi%llu, %pD2\n",
11077 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11078 +               hidx = au_dr_ihash(ent->dr_h_ino);
11079 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11080 +       }
11081 +       goto out; /* success */
11082 +
11083 +out_free:
11084 +       au_dr_hino_free(dr);
11085 +out:
11086 +       AuTraceErr(err);
11087 +       return err;
11088 +}
11089 +
11090 +/*
11091 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11092 + * @path is a switch to distinguish load and store.
11093 + */
11094 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11095 +                     struct au_branch *br, const struct path *path)
11096 +{
11097 +       int err, flags;
11098 +       unsigned char load, suspend;
11099 +       struct file *hinofile;
11100 +       struct au_hinode *hdir;
11101 +       struct inode *dir, *delegated;
11102 +       struct path hinopath;
11103 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11104 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11105 +
11106 +       AuDebugOn(bindex < 0 && !br);
11107 +       AuDebugOn(bindex >= 0 && br);
11108 +
11109 +       err = -EINVAL;
11110 +       suspend = !br;
11111 +       if (suspend)
11112 +               br = au_sbr(sb, bindex);
11113 +       load = !!path;
11114 +       if (!load) {
11115 +               path = &br->br_path;
11116 +               AuDebugOn(!au_br_writable(br->br_perm));
11117 +               if (unlikely(!au_br_writable(br->br_perm)))
11118 +                       goto out;
11119 +       }
11120 +
11121 +       hdir = NULL;
11122 +       if (suspend) {
11123 +               dir = d_inode(sb->s_root);
11124 +               hdir = au_hinode(au_ii(dir), bindex);
11125 +               dir = hdir->hi_inode;
11126 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11127 +       } else {
11128 +               dir = d_inode(path->dentry);
11129 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11130 +       }
11131 +       hinopath.mnt = path->mnt;
11132 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11133 +       err = PTR_ERR(hinopath.dentry);
11134 +       if (IS_ERR(hinopath.dentry))
11135 +               goto out_unlock;
11136 +
11137 +       err = 0;
11138 +       flags = O_RDONLY;
11139 +       if (load) {
11140 +               if (d_is_negative(hinopath.dentry))
11141 +                       goto out_dput; /* success */
11142 +       } else {
11143 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11144 +                       if (d_is_positive(hinopath.dentry)) {
11145 +                               delegated = NULL;
11146 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11147 +                                                  /*force*/0);
11148 +                               AuTraceErr(err);
11149 +                               if (unlikely(err))
11150 +                                       pr_err("ignored err %d, %pd2\n",
11151 +                                              err, hinopath.dentry);
11152 +                               if (unlikely(err == -EWOULDBLOCK))
11153 +                                       iput(delegated);
11154 +                               err = 0;
11155 +                       }
11156 +                       goto out_dput;
11157 +               } else if (!d_is_positive(hinopath.dentry)) {
11158 +                       err = vfsub_create(dir, &hinopath, 0600,
11159 +                                          /*want_excl*/false);
11160 +                       AuTraceErr(err);
11161 +                       if (unlikely(err))
11162 +                               goto out_dput;
11163 +               }
11164 +               flags = O_WRONLY;
11165 +       }
11166 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11167 +       if (suspend)
11168 +               au_hn_inode_unlock(hdir);
11169 +       else
11170 +               inode_unlock(dir);
11171 +       dput(hinopath.dentry);
11172 +       AuTraceErrPtr(hinofile);
11173 +       if (IS_ERR(hinofile)) {
11174 +               err = PTR_ERR(hinofile);
11175 +               goto out;
11176 +       }
11177 +
11178 +       if (load)
11179 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11180 +       else
11181 +               err = au_dr_hino_store(sb, br, hinofile);
11182 +       fput(hinofile);
11183 +       goto out;
11184 +
11185 +out_dput:
11186 +       dput(hinopath.dentry);
11187 +out_unlock:
11188 +       if (suspend)
11189 +               au_hn_inode_unlock(hdir);
11190 +       else
11191 +               inode_unlock(dir);
11192 +out:
11193 +       AuTraceErr(err);
11194 +       return err;
11195 +}
11196 +
11197 +/* ---------------------------------------------------------------------- */
11198 +
11199 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11200 +{
11201 +       int err;
11202 +       struct kstatfs kstfs;
11203 +       dev_t dev;
11204 +       struct dentry *dentry;
11205 +       struct super_block *sb;
11206 +
11207 +       err = vfs_statfs((void *)path, &kstfs);
11208 +       AuTraceErr(err);
11209 +       if (unlikely(err))
11210 +               goto out;
11211 +
11212 +       /* todo: support for UUID */
11213 +
11214 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11215 +               brid->type = AuBrid_FSID;
11216 +               brid->fsid = kstfs.f_fsid;
11217 +       } else {
11218 +               dentry = path->dentry;
11219 +               sb = dentry->d_sb;
11220 +               dev = sb->s_dev;
11221 +               if (dev) {
11222 +                       brid->type = AuBrid_DEV;
11223 +                       brid->dev = dev;
11224 +               }
11225 +       }
11226 +
11227 +out:
11228 +       return err;
11229 +}
11230 +
11231 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11232 +                 const struct path *path)
11233 +{
11234 +       int err, i;
11235 +       struct au_dr_br *dr;
11236 +       struct hlist_bl_head *hbl;
11237 +
11238 +       dr = &br->br_dirren;
11239 +       hbl = dr->dr_h_ino;
11240 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11241 +               INIT_HLIST_BL_HEAD(hbl);
11242 +
11243 +       err = au_dr_brid_init(&dr->dr_brid, path);
11244 +       if (unlikely(err))
11245 +               goto out;
11246 +
11247 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11248 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11249 +
11250 +out:
11251 +       AuTraceErr(err);
11252 +       return err;
11253 +}
11254 +
11255 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11256 +{
11257 +       int err;
11258 +
11259 +       err = 0;
11260 +       if (au_br_writable(br->br_perm))
11261 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11262 +       if (!err)
11263 +               au_dr_hino_free(&br->br_dirren);
11264 +
11265 +       return err;
11266 +}
11267 +
11268 +/* ---------------------------------------------------------------------- */
11269 +
11270 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11271 +                      char *buf, size_t sz)
11272 +{
11273 +       int err;
11274 +       unsigned int major, minor;
11275 +       char *p;
11276 +
11277 +       p = buf;
11278 +       err = snprintf(p, sz, "%d_", brid->type);
11279 +       AuDebugOn(err > sz);
11280 +       p += err;
11281 +       sz -= err;
11282 +       switch (brid->type) {
11283 +       case AuBrid_Unset:
11284 +               return -EINVAL;
11285 +       case AuBrid_UUID:
11286 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11287 +               break;
11288 +       case AuBrid_FSID:
11289 +               err = snprintf(p, sz, "%08x-%08x",
11290 +                              brid->fsid.val[0], brid->fsid.val[1]);
11291 +               break;
11292 +       case AuBrid_DEV:
11293 +               major = MAJOR(brid->dev);
11294 +               minor = MINOR(brid->dev);
11295 +               if (major <= 0xff && minor <= 0xff)
11296 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11297 +               else
11298 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11299 +               break;
11300 +       }
11301 +       AuDebugOn(err > sz);
11302 +       p += err;
11303 +       sz -= err;
11304 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11305 +       AuDebugOn(err > sz);
11306 +       p += err;
11307 +       sz -= err;
11308 +
11309 +       return p - buf;
11310 +}
11311 +
11312 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11313 +{
11314 +       int rlen;
11315 +       struct dentry *br_dentry;
11316 +       struct inode *br_inode;
11317 +
11318 +       br_dentry = au_br_dentry(br);
11319 +       br_inode = d_inode(br_dentry);
11320 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11321 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11322 +       AuDebugOn(rlen > len);
11323 +
11324 +       return rlen;
11325 +}
11326 +
11327 +/* ---------------------------------------------------------------------- */
11328 +
11329 +/*
11330 + * from the given @h_dentry, construct drinfo at @*fdata.
11331 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11332 + * @allocated.
11333 + */
11334 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11335 +                              struct dentry *h_dentry,
11336 +                              unsigned char *allocated)
11337 +{
11338 +       int err, v;
11339 +       struct au_drinfo_fdata *f, *p;
11340 +       struct au_drinfo *drinfo;
11341 +       struct inode *h_inode;
11342 +       struct qstr *qname;
11343 +
11344 +       err = 0;
11345 +       f = *fdata;
11346 +       h_inode = d_inode(h_dentry);
11347 +       qname = &h_dentry->d_name;
11348 +       drinfo = &f->drinfo;
11349 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11350 +       drinfo->oldnamelen = qname->len;
11351 +       if (*allocated < sizeof(*f) + qname->len) {
11352 +               v = roundup_pow_of_two(*allocated + qname->len);
11353 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11354 +               if (unlikely(!p)) {
11355 +                       err = -ENOMEM;
11356 +                       AuTraceErr(err);
11357 +                       goto out;
11358 +               }
11359 +               f = p;
11360 +               *fdata = f;
11361 +               *allocated = v;
11362 +               drinfo = &f->drinfo;
11363 +       }
11364 +       memcpy(drinfo->oldname, qname->name, qname->len);
11365 +       AuDbg("i%llu, %.*s\n",
11366 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11367 +             drinfo->oldname);
11368 +
11369 +out:
11370 +       AuTraceErr(err);
11371 +       return err;
11372 +}
11373 +
11374 +/* callers have to free the return value */
11375 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11376 +{
11377 +       struct au_drinfo *ret, *drinfo;
11378 +       struct au_drinfo_fdata fdata;
11379 +       int len;
11380 +       loff_t pos;
11381 +       ssize_t ssz;
11382 +
11383 +       ret = ERR_PTR(-EIO);
11384 +       pos = 0;
11385 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11386 +       if (unlikely(ssz != sizeof(fdata))) {
11387 +               AuIOErr("ssz %zd, %u, %pD2\n",
11388 +                       ssz, (unsigned int)sizeof(fdata), file);
11389 +               goto out;
11390 +       }
11391 +
11392 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11393 +       switch (fdata.magic) {
11394 +       case AUFS_DRINFO_MAGIC_V1:
11395 +               break;
11396 +       default:
11397 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11398 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11399 +               goto out;
11400 +       }
11401 +
11402 +       drinfo = &fdata.drinfo;
11403 +       len = drinfo->oldnamelen;
11404 +       if (!len) {
11405 +               AuIOErr("broken drinfo %pD2\n", file);
11406 +               goto out;
11407 +       }
11408 +
11409 +       ret = NULL;
11410 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11411 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11412 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11413 +                     (unsigned long long)drinfo->ino,
11414 +                     (unsigned long long)h_ino, file);
11415 +               goto out; /* success */
11416 +       }
11417 +
11418 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11419 +       if (unlikely(!ret)) {
11420 +               ret = ERR_PTR(-ENOMEM);
11421 +               AuTraceErrPtr(ret);
11422 +               goto out;
11423 +       }
11424 +
11425 +       *ret = *drinfo;
11426 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11427 +       if (unlikely(ssz != len)) {
11428 +               au_kfree_rcu(ret);
11429 +               ret = ERR_PTR(-EIO);
11430 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11431 +               goto out;
11432 +       }
11433 +
11434 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11435 +
11436 +out:
11437 +       return ret;
11438 +}
11439 +
11440 +/* ---------------------------------------------------------------------- */
11441 +
11442 +/* in order to be revertible */
11443 +struct au_drinfo_rev_elm {
11444 +       int                     created;
11445 +       struct dentry           *info_dentry;
11446 +       struct au_drinfo        *info_last;
11447 +};
11448 +
11449 +struct au_drinfo_rev {
11450 +       unsigned char                   already;
11451 +       aufs_bindex_t                   nelm;
11452 +       struct au_drinfo_rev_elm        elm[];
11453 +};
11454 +
11455 +/* todo: isn't it too large? */
11456 +struct au_drinfo_store {
11457 +       struct path h_ppath;
11458 +       struct dentry *h_dentry;
11459 +       struct au_drinfo_fdata *fdata;
11460 +       char *infoname;                 /* inside of whname, just after PFX */
11461 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11462 +       aufs_bindex_t btgt, btail;
11463 +       unsigned char no_sio,
11464 +               allocated,              /* current size of *fdata */
11465 +               infonamelen,            /* room size for p */
11466 +               whnamelen,              /* length of the generated name */
11467 +               renameback;             /* renamed back */
11468 +};
11469 +
11470 +/* on rename(2) error, the caller should revert it using @elm */
11471 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11472 +                             struct au_drinfo_rev_elm *elm)
11473 +{
11474 +       int err, len;
11475 +       ssize_t ssz;
11476 +       loff_t pos;
11477 +       struct path infopath = {
11478 +               .mnt = w->h_ppath.mnt
11479 +       };
11480 +       struct inode *h_dir, *h_inode, *delegated;
11481 +       struct file *infofile;
11482 +       struct qstr *qname;
11483 +
11484 +       AuDebugOn(elm
11485 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11486 +
11487 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11488 +                                              w->whnamelen);
11489 +       AuTraceErrPtr(infopath.dentry);
11490 +       if (IS_ERR(infopath.dentry)) {
11491 +               err = PTR_ERR(infopath.dentry);
11492 +               goto out;
11493 +       }
11494 +
11495 +       err = 0;
11496 +       h_dir = d_inode(w->h_ppath.dentry);
11497 +       if (elm && d_is_negative(infopath.dentry)) {
11498 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11499 +               AuTraceErr(err);
11500 +               if (unlikely(err))
11501 +                       goto out_dput;
11502 +               elm->created = 1;
11503 +               elm->info_dentry = dget(infopath.dentry);
11504 +       }
11505 +
11506 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11507 +       AuTraceErrPtr(infofile);
11508 +       if (IS_ERR(infofile)) {
11509 +               err = PTR_ERR(infofile);
11510 +               goto out_dput;
11511 +       }
11512 +
11513 +       h_inode = d_inode(infopath.dentry);
11514 +       if (elm && i_size_read(h_inode)) {
11515 +               h_inode = d_inode(w->h_dentry);
11516 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11517 +               AuTraceErrPtr(elm->info_last);
11518 +               if (IS_ERR(elm->info_last)) {
11519 +                       err = PTR_ERR(elm->info_last);
11520 +                       elm->info_last = NULL;
11521 +                       AuDebugOn(elm->info_dentry);
11522 +                       goto out_fput;
11523 +               }
11524 +       }
11525 +
11526 +       if (elm && w->renameback) {
11527 +               delegated = NULL;
11528 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11529 +               AuTraceErr(err);
11530 +               if (unlikely(err == -EWOULDBLOCK))
11531 +                       iput(delegated);
11532 +               goto out_fput;
11533 +       }
11534 +
11535 +       pos = 0;
11536 +       qname = &w->h_dentry->d_name;
11537 +       len = sizeof(*w->fdata) + qname->len;
11538 +       if (!elm)
11539 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11540 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11541 +       if (ssz == len) {
11542 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11543 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11544 +               goto out_fput; /* success */
11545 +       } else {
11546 +               err = -EIO;
11547 +               if (ssz < 0)
11548 +                       err = ssz;
11549 +               /* the caller should revert it using @elm */
11550 +       }
11551 +
11552 +out_fput:
11553 +       fput(infofile);
11554 +out_dput:
11555 +       dput(infopath.dentry);
11556 +out:
11557 +       AuTraceErr(err);
11558 +       return err;
11559 +}
11560 +
11561 +struct au_call_drinfo_do_store_args {
11562 +       int *errp;
11563 +       struct au_drinfo_store *w;
11564 +       struct au_drinfo_rev_elm *elm;
11565 +};
11566 +
11567 +static void au_call_drinfo_do_store(void *args)
11568 +{
11569 +       struct au_call_drinfo_do_store_args *a = args;
11570 +
11571 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11572 +}
11573 +
11574 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11575 +                              struct au_drinfo_rev_elm *elm)
11576 +{
11577 +       int err, wkq_err;
11578 +
11579 +       if (w->no_sio)
11580 +               err = au_drinfo_do_store(w, elm);
11581 +       else {
11582 +               struct au_call_drinfo_do_store_args a = {
11583 +                       .errp   = &err,
11584 +                       .w      = w,
11585 +                       .elm    = elm
11586 +               };
11587 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11588 +               if (unlikely(wkq_err))
11589 +                       err = wkq_err;
11590 +       }
11591 +       AuTraceErr(err);
11592 +
11593 +       return err;
11594 +}
11595 +
11596 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11597 +                                    aufs_bindex_t btgt)
11598 +{
11599 +       int err;
11600 +
11601 +       memset(w, 0, sizeof(*w));
11602 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11603 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11604 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11605 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11606 +       w->btgt = btgt;
11607 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11608 +
11609 +       err = -ENOMEM;
11610 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11611 +       if (unlikely(!w->fdata)) {
11612 +               AuTraceErr(err);
11613 +               goto out;
11614 +       }
11615 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11616 +       err = 0;
11617 +
11618 +out:
11619 +       return err;
11620 +}
11621 +
11622 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11623 +{
11624 +       au_kfree_rcu(w->fdata);
11625 +}
11626 +
11627 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11628 +                               struct au_drinfo_store *w)
11629 +{
11630 +       struct au_drinfo_rev_elm *elm;
11631 +       struct inode *h_dir, *delegated;
11632 +       int err, nelm;
11633 +       struct path infopath = {
11634 +               .mnt = w->h_ppath.mnt
11635 +       };
11636 +
11637 +       h_dir = d_inode(w->h_ppath.dentry);
11638 +       IMustLock(h_dir);
11639 +
11640 +       err = 0;
11641 +       elm = rev->elm;
11642 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11643 +               AuDebugOn(elm->created && elm->info_last);
11644 +               if (elm->created) {
11645 +                       AuDbg("here\n");
11646 +                       delegated = NULL;
11647 +                       infopath.dentry = elm->info_dentry;
11648 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11649 +                                          !w->no_sio);
11650 +                       AuTraceErr(err);
11651 +                       if (unlikely(err == -EWOULDBLOCK))
11652 +                               iput(delegated);
11653 +                       dput(elm->info_dentry);
11654 +               } else if (elm->info_last) {
11655 +                       AuDbg("here\n");
11656 +                       w->fdata->drinfo = *elm->info_last;
11657 +                       memcpy(w->fdata->drinfo.oldname,
11658 +                              elm->info_last->oldname,
11659 +                              elm->info_last->oldnamelen);
11660 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11661 +                       au_kfree_rcu(elm->info_last);
11662 +               }
11663 +               if (unlikely(err))
11664 +                       AuIOErr("%d, %s\n", err, w->whname);
11665 +               /* go on even if err */
11666 +       }
11667 +}
11668 +
11669 +/* caller has to call au_dr_rename_fin() later */
11670 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11671 +                          struct qstr *dst_name, void *_rev)
11672 +{
11673 +       int err, sz, nelm;
11674 +       aufs_bindex_t bindex, btail;
11675 +       struct au_drinfo_store work;
11676 +       struct au_drinfo_rev *rev, **p;
11677 +       struct au_drinfo_rev_elm *elm;
11678 +       struct super_block *sb;
11679 +       struct au_branch *br;
11680 +       struct au_hinode *hdir;
11681 +
11682 +       err = au_drinfo_store_work_init(&work, btgt);
11683 +       AuTraceErr(err);
11684 +       if (unlikely(err))
11685 +               goto out;
11686 +
11687 +       err = -ENOMEM;
11688 +       btail = au_dbtaildir(dentry);
11689 +       nelm = btail - btgt;
11690 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11691 +       rev = kcalloc(1, sz, GFP_NOFS);
11692 +       if (unlikely(!rev)) {
11693 +               AuTraceErr(err);
11694 +               goto out_args;
11695 +       }
11696 +       rev->nelm = nelm;
11697 +       elm = rev->elm;
11698 +       p = _rev;
11699 +       *p = rev;
11700 +
11701 +       err = 0;
11702 +       sb = dentry->d_sb;
11703 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11704 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11705 +       hdir = au_hi(d_inode(dentry), btgt);
11706 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11707 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11708 +               work.h_dentry = au_h_dptr(dentry, bindex);
11709 +               if (!work.h_dentry)
11710 +                       continue;
11711 +
11712 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11713 +                                         &work.allocated);
11714 +               AuTraceErr(err);
11715 +               if (unlikely(err))
11716 +                       break;
11717 +
11718 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11719 +               br = au_sbr(sb, bindex);
11720 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11721 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11722 +                                                work.infonamelen);
11723 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11724 +                     work.whnamelen, work.whname,
11725 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11726 +                     work.fdata->drinfo.oldnamelen,
11727 +                     work.fdata->drinfo.oldname);
11728 +
11729 +               err = au_drinfo_store_sio(&work, elm);
11730 +               AuTraceErr(err);
11731 +               if (unlikely(err))
11732 +                       break;
11733 +       }
11734 +       if (unlikely(err)) {
11735 +               /* revert all drinfo */
11736 +               au_drinfo_store_rev(rev, &work);
11737 +               au_kfree_try_rcu(rev);
11738 +               *p = NULL;
11739 +       }
11740 +       au_hn_inode_unlock(hdir);
11741 +
11742 +out_args:
11743 +       au_drinfo_store_work_fin(&work);
11744 +out:
11745 +       return err;
11746 +}
11747 +
11748 +/* ---------------------------------------------------------------------- */
11749 +
11750 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11751 +                struct qstr *dst_name, void *_rev)
11752 +{
11753 +       int err, already;
11754 +       ino_t ino;
11755 +       struct super_block *sb;
11756 +       struct au_branch *br;
11757 +       struct au_dr_br *dr;
11758 +       struct dentry *h_dentry;
11759 +       struct inode *h_inode;
11760 +       struct au_dr_hino *ent;
11761 +       struct au_drinfo_rev *rev, **p;
11762 +
11763 +       AuDbg("bindex %d\n", bindex);
11764 +
11765 +       err = -ENOMEM;
11766 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11767 +       if (unlikely(!ent))
11768 +               goto out;
11769 +
11770 +       sb = src->d_sb;
11771 +       br = au_sbr(sb, bindex);
11772 +       dr = &br->br_dirren;
11773 +       h_dentry = au_h_dptr(src, bindex);
11774 +       h_inode = d_inode(h_dentry);
11775 +       ino = h_inode->i_ino;
11776 +       ent->dr_h_ino = ino;
11777 +       already = au_dr_hino_test_add(dr, ino, ent);
11778 +       AuDbg("b%d, hi%llu, already %d\n",
11779 +             bindex, (unsigned long long)ino, already);
11780 +
11781 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11782 +       AuTraceErr(err);
11783 +       if (!err) {
11784 +               p = _rev;
11785 +               rev = *p;
11786 +               rev->already = already;
11787 +               goto out; /* success */
11788 +       }
11789 +
11790 +       /* revert */
11791 +       if (!already)
11792 +               au_dr_hino_del(dr, ent);
11793 +       au_kfree_rcu(ent);
11794 +
11795 +out:
11796 +       AuTraceErr(err);
11797 +       return err;
11798 +}
11799 +
11800 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11801 +{
11802 +       struct au_drinfo_rev *rev;
11803 +       struct au_drinfo_rev_elm *elm;
11804 +       int nelm;
11805 +
11806 +       rev = _rev;
11807 +       elm = rev->elm;
11808 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11809 +               dput(elm->info_dentry);
11810 +               au_kfree_rcu(elm->info_last);
11811 +       }
11812 +       au_kfree_try_rcu(rev);
11813 +}
11814 +
11815 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11816 +{
11817 +       int err;
11818 +       struct au_drinfo_store work;
11819 +       struct au_drinfo_rev *rev = _rev;
11820 +       struct super_block *sb;
11821 +       struct au_branch *br;
11822 +       struct inode *h_inode;
11823 +       struct au_dr_br *dr;
11824 +       struct au_dr_hino *ent;
11825 +
11826 +       err = au_drinfo_store_work_init(&work, btgt);
11827 +       if (unlikely(err))
11828 +               goto out;
11829 +
11830 +       sb = src->d_sb;
11831 +       br = au_sbr(sb, btgt);
11832 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11833 +       work.h_ppath.mnt = au_br_mnt(br);
11834 +       au_drinfo_store_rev(rev, &work);
11835 +       au_drinfo_store_work_fin(&work);
11836 +       if (rev->already)
11837 +               goto out;
11838 +
11839 +       dr = &br->br_dirren;
11840 +       h_inode = d_inode(work.h_ppath.dentry);
11841 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11842 +       BUG_ON(!ent);
11843 +       au_dr_hino_del(dr, ent);
11844 +       au_kfree_rcu(ent);
11845 +
11846 +out:
11847 +       au_kfree_try_rcu(rev);
11848 +       if (unlikely(err))
11849 +               pr_err("failed to remove dirren info\n");
11850 +}
11851 +
11852 +/* ---------------------------------------------------------------------- */
11853 +
11854 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11855 +                                          char *whname, int whnamelen,
11856 +                                          struct dentry **info_dentry)
11857 +{
11858 +       struct au_drinfo *drinfo;
11859 +       struct file *f;
11860 +       struct inode *h_dir;
11861 +       struct path infopath;
11862 +       int unlocked;
11863 +
11864 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11865 +
11866 +       *info_dentry = NULL;
11867 +       drinfo = NULL;
11868 +       unlocked = 0;
11869 +       h_dir = d_inode(h_ppath->dentry);
11870 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11871 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11872 +       if (IS_ERR(infopath.dentry)) {
11873 +               drinfo = (void *)infopath.dentry;
11874 +               goto out;
11875 +       }
11876 +
11877 +       if (d_is_negative(infopath.dentry))
11878 +               goto out_dput; /* success */
11879 +
11880 +       infopath.mnt = h_ppath->mnt;
11881 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11882 +       inode_unlock_shared(h_dir);
11883 +       unlocked = 1;
11884 +       if (IS_ERR(f)) {
11885 +               drinfo = (void *)f;
11886 +               goto out_dput;
11887 +       }
11888 +
11889 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11890 +       if (IS_ERR_OR_NULL(drinfo))
11891 +               goto out_fput;
11892 +
11893 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11894 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11895 +
11896 +out_fput:
11897 +       fput(f);
11898 +out_dput:
11899 +       dput(infopath.dentry);
11900 +out:
11901 +       if (!unlocked)
11902 +               inode_unlock_shared(h_dir);
11903 +       AuTraceErrPtr(drinfo);
11904 +       return drinfo;
11905 +}
11906 +
11907 +struct au_drinfo_do_load_args {
11908 +       struct au_drinfo **drinfop;
11909 +       struct path *h_ppath;
11910 +       char *whname;
11911 +       int whnamelen;
11912 +       struct dentry **info_dentry;
11913 +};
11914 +
11915 +static void au_call_drinfo_do_load(void *args)
11916 +{
11917 +       struct au_drinfo_do_load_args *a = args;
11918 +
11919 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11920 +                                       a->info_dentry);
11921 +}
11922 +
11923 +struct au_drinfo_load {
11924 +       struct path h_ppath;
11925 +       struct qstr *qname;
11926 +       unsigned char no_sio;
11927 +
11928 +       aufs_bindex_t ninfo;
11929 +       struct au_drinfo **drinfo;
11930 +};
11931 +
11932 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11933 +                         struct au_branch *br)
11934 +{
11935 +       int err, wkq_err, whnamelen, e;
11936 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11937 +               = AUFS_WH_DR_INFO_PFX;
11938 +       struct au_drinfo *drinfo;
11939 +       struct qstr oldname;
11940 +       struct inode *h_dir, *delegated;
11941 +       struct dentry *info_dentry;
11942 +       struct path infopath;
11943 +
11944 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11945 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11946 +                                   sizeof(whname) - whnamelen);
11947 +       if (w->no_sio)
11948 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11949 +                                          &info_dentry);
11950 +       else {
11951 +               struct au_drinfo_do_load_args args = {
11952 +                       .drinfop        = &drinfo,
11953 +                       .h_ppath        = &w->h_ppath,
11954 +                       .whname         = whname,
11955 +                       .whnamelen      = whnamelen,
11956 +                       .info_dentry    = &info_dentry
11957 +               };
11958 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11959 +               if (unlikely(wkq_err))
11960 +                       drinfo = ERR_PTR(wkq_err);
11961 +       }
11962 +       err = PTR_ERR(drinfo);
11963 +       if (IS_ERR_OR_NULL(drinfo))
11964 +               goto out;
11965 +
11966 +       err = 0;
11967 +       oldname.len = drinfo->oldnamelen;
11968 +       oldname.name = drinfo->oldname;
11969 +       if (au_qstreq(w->qname, &oldname)) {
11970 +               /* the name is renamed back */
11971 +               au_kfree_rcu(drinfo);
11972 +               drinfo = NULL;
11973 +
11974 +               infopath.dentry = info_dentry;
11975 +               infopath.mnt = w->h_ppath.mnt;
11976 +               h_dir = d_inode(w->h_ppath.dentry);
11977 +               delegated = NULL;
11978 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11979 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11980 +               inode_unlock(h_dir);
11981 +               if (unlikely(e))
11982 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11983 +               if (unlikely(e == -EWOULDBLOCK))
11984 +                       iput(delegated);
11985 +       }
11986 +       au_kfree_rcu(w->drinfo[bindex]);
11987 +       w->drinfo[bindex] = drinfo;
11988 +       dput(info_dentry);
11989 +
11990 +out:
11991 +       AuTraceErr(err);
11992 +       return err;
11993 +}
11994 +
11995 +/* ---------------------------------------------------------------------- */
11996 +
11997 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
11998 +{
11999 +       struct au_drinfo **p = drinfo;
12000 +
12001 +       while (n-- > 0)
12002 +               au_kfree_rcu(*drinfo++);
12003 +       au_kfree_try_rcu(p);
12004 +}
12005 +
12006 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12007 +              aufs_bindex_t btgt)
12008 +{
12009 +       int err, ninfo;
12010 +       struct au_drinfo_load w;
12011 +       aufs_bindex_t bindex, bbot;
12012 +       struct au_branch *br;
12013 +       struct inode *h_dir;
12014 +       struct au_dr_hino *ent;
12015 +       struct super_block *sb;
12016 +
12017 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12018 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12019 +             AuLNPair(&lkup->whname), btgt);
12020 +
12021 +       sb = dentry->d_sb;
12022 +       bbot = au_sbbot(sb);
12023 +       w.ninfo = bbot + 1;
12024 +       if (!lkup->dirren.drinfo) {
12025 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12026 +                                             sizeof(*lkup->dirren.drinfo),
12027 +                                             GFP_NOFS);
12028 +               if (unlikely(!lkup->dirren.drinfo)) {
12029 +                       err = -ENOMEM;
12030 +                       goto out;
12031 +               }
12032 +               lkup->dirren.ninfo = w.ninfo;
12033 +       }
12034 +       w.drinfo = lkup->dirren.drinfo;
12035 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12036 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12037 +       AuDebugOn(!w.h_ppath.dentry);
12038 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12039 +       w.qname = &dentry->d_name;
12040 +
12041 +       ninfo = 0;
12042 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12043 +               br = au_sbr(sb, bindex);
12044 +               err = au_drinfo_load(&w, bindex, br);
12045 +               if (unlikely(err))
12046 +                       goto out_free;
12047 +               if (w.drinfo[bindex])
12048 +                       ninfo++;
12049 +       }
12050 +       if (!ninfo) {
12051 +               br = au_sbr(sb, btgt);
12052 +               h_dir = d_inode(w.h_ppath.dentry);
12053 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12054 +               AuDebugOn(!ent);
12055 +               au_dr_hino_del(&br->br_dirren, ent);
12056 +               au_kfree_rcu(ent);
12057 +       }
12058 +       goto out; /* success */
12059 +
12060 +out_free:
12061 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12062 +       lkup->dirren.ninfo = 0;
12063 +       lkup->dirren.drinfo = NULL;
12064 +out:
12065 +       AuTraceErr(err);
12066 +       return err;
12067 +}
12068 +
12069 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12070 +{
12071 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12072 +}
12073 +
12074 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12075 +{
12076 +       int err;
12077 +       struct au_drinfo *drinfo;
12078 +
12079 +       err = 0;
12080 +       if (!lkup->dirren.drinfo)
12081 +               goto out;
12082 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12083 +       drinfo = lkup->dirren.drinfo[btgt];
12084 +       if (!drinfo)
12085 +               goto out;
12086 +
12087 +       au_kfree_try_rcu(lkup->whname.name);
12088 +       lkup->whname.name = NULL;
12089 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12090 +       lkup->dirren.dr_name.name = drinfo->oldname;
12091 +       lkup->name = &lkup->dirren.dr_name;
12092 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12093 +       if (!err)
12094 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12095 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12096 +                     btgt);
12097 +
12098 +out:
12099 +       AuTraceErr(err);
12100 +       return err;
12101 +}
12102 +
12103 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12104 +                    ino_t h_ino)
12105 +{
12106 +       int match;
12107 +       struct au_drinfo *drinfo;
12108 +
12109 +       match = 1;
12110 +       if (!lkup->dirren.drinfo)
12111 +               goto out;
12112 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12113 +       drinfo = lkup->dirren.drinfo[bindex];
12114 +       if (!drinfo)
12115 +               goto out;
12116 +
12117 +       match = (drinfo->ino == h_ino);
12118 +       AuDbg("match %d\n", match);
12119 +
12120 +out:
12121 +       return match;
12122 +}
12123 +
12124 +/* ---------------------------------------------------------------------- */
12125 +
12126 +int au_dr_opt_set(struct super_block *sb)
12127 +{
12128 +       int err;
12129 +       aufs_bindex_t bindex, bbot;
12130 +       struct au_branch *br;
12131 +
12132 +       err = 0;
12133 +       bbot = au_sbbot(sb);
12134 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12135 +               br = au_sbr(sb, bindex);
12136 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12137 +       }
12138 +
12139 +       return err;
12140 +}
12141 +
12142 +int au_dr_opt_flush(struct super_block *sb)
12143 +{
12144 +       int err;
12145 +       aufs_bindex_t bindex, bbot;
12146 +       struct au_branch *br;
12147 +
12148 +       err = 0;
12149 +       bbot = au_sbbot(sb);
12150 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12151 +               br = au_sbr(sb, bindex);
12152 +               if (au_br_writable(br->br_perm))
12153 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12154 +       }
12155 +
12156 +       return err;
12157 +}
12158 +
12159 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12160 +{
12161 +       int err;
12162 +       aufs_bindex_t bindex, bbot;
12163 +       struct au_branch *br;
12164 +
12165 +       err = 0;
12166 +       if (!no_flush) {
12167 +               err = au_dr_opt_flush(sb);
12168 +               if (unlikely(err))
12169 +                       goto out;
12170 +       }
12171 +
12172 +       bbot = au_sbbot(sb);
12173 +       for (bindex = 0; bindex <= bbot; bindex++) {
12174 +               br = au_sbr(sb, bindex);
12175 +               au_dr_hino_free(&br->br_dirren);
12176 +       }
12177 +
12178 +out:
12179 +       return err;
12180 +}
12181 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12182 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12183 +++ linux/fs/aufs/dirren.h      2022-11-05 23:02:18.962555950 +0100
12184 @@ -0,0 +1,140 @@
12185 +/* SPDX-License-Identifier: GPL-2.0 */
12186 +/*
12187 + * Copyright (C) 2017-2022 Junjiro R. Okajima
12188 + *
12189 + * This program is free software; you can redistribute it and/or modify
12190 + * it under the terms of the GNU General Public License as published by
12191 + * the Free Software Foundation; either version 2 of the License, or
12192 + * (at your option) any later version.
12193 + *
12194 + * This program is distributed in the hope that it will be useful,
12195 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12196 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12197 + * GNU General Public License for more details.
12198 + *
12199 + * You should have received a copy of the GNU General Public License
12200 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12201 + */
12202 +
12203 +/*
12204 + * renamed dir info
12205 + */
12206 +
12207 +#ifndef __AUFS_DIRREN_H__
12208 +#define __AUFS_DIRREN_H__
12209 +
12210 +#ifdef __KERNEL__
12211 +
12212 +#include <linux/dcache.h>
12213 +#include <linux/statfs.h>
12214 +#include <linux/uuid.h>
12215 +#include "hbl.h"
12216 +
12217 +#define AuDirren_NHASH 100
12218 +
12219 +#ifdef CONFIG_AUFS_DIRREN
12220 +enum au_brid_type {
12221 +       AuBrid_Unset,
12222 +       AuBrid_UUID,
12223 +       AuBrid_FSID,
12224 +       AuBrid_DEV
12225 +};
12226 +
12227 +struct au_dr_brid {
12228 +       enum au_brid_type       type;
12229 +       union {
12230 +               uuid_t  uuid;   /* unimplemented yet */
12231 +               fsid_t  fsid;
12232 +               dev_t   dev;
12233 +       };
12234 +};
12235 +
12236 +/* 20 is the max digits length of ulong 64 */
12237 +/* brid-type "_" uuid "_" inum */
12238 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12239 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12240 +
12241 +struct au_dr_hino {
12242 +       struct hlist_bl_node    dr_hnode;
12243 +       ino_t                   dr_h_ino;
12244 +};
12245 +
12246 +struct au_dr_br {
12247 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12248 +       struct au_dr_brid       dr_brid;
12249 +};
12250 +
12251 +struct au_dr_lookup {
12252 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12253 +       struct qstr             dr_name; /* subset of dr_info */
12254 +       aufs_bindex_t           ninfo;
12255 +       struct au_drinfo        **drinfo;
12256 +};
12257 +#else
12258 +struct au_dr_hino;
12259 +/* empty */
12260 +struct au_dr_br { };
12261 +struct au_dr_lookup { };
12262 +#endif
12263 +
12264 +/* ---------------------------------------------------------------------- */
12265 +
12266 +struct au_branch;
12267 +struct au_do_lookup_args;
12268 +struct au_hinode;
12269 +#ifdef CONFIG_AUFS_DIRREN
12270 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12271 +                       struct au_dr_hino *add_ent);
12272 +void au_dr_hino_free(struct au_dr_br *dr);
12273 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12274 +                 const struct path *path);
12275 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12276 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12277 +                struct qstr *dst_name, void *_rev);
12278 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12279 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12280 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12281 +              aufs_bindex_t bindex);
12282 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12283 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12284 +                    ino_t h_ino);
12285 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12286 +int au_dr_opt_set(struct super_block *sb);
12287 +int au_dr_opt_flush(struct super_block *sb);
12288 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12289 +#else
12290 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12291 +          struct au_dr_hino *add_ent);
12292 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12293 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12294 +          const struct path *path);
12295 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12296 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12297 +          struct qstr *dst_name, void *_rev);
12298 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12299 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12300 +          void *rev);
12301 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12302 +          aufs_bindex_t bindex);
12303 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12304 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12305 +          aufs_bindex_t bindex, ino_t h_ino);
12306 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12307 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12308 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12309 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12310 +#endif
12311 +
12312 +/* ---------------------------------------------------------------------- */
12313 +
12314 +#ifdef CONFIG_AUFS_DIRREN
12315 +static inline int au_dr_ihash(ino_t h_ino)
12316 +{
12317 +       return h_ino % AuDirren_NHASH;
12318 +}
12319 +#else
12320 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12321 +#endif
12322 +
12323 +#endif /* __KERNEL__ */
12324 +#endif /* __AUFS_DIRREN_H__ */
12325 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12326 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12327 +++ linux/fs/aufs/dynop.c       2022-11-05 23:02:18.962555950 +0100
12328 @@ -0,0 +1,366 @@
12329 +// SPDX-License-Identifier: GPL-2.0
12330 +/*
12331 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12332 + *
12333 + * This program is free software; you can redistribute it and/or modify
12334 + * it under the terms of the GNU General Public License as published by
12335 + * the Free Software Foundation; either version 2 of the License, or
12336 + * (at your option) any later version.
12337 + *
12338 + * This program is distributed in the hope that it will be useful,
12339 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12340 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12341 + * GNU General Public License for more details.
12342 + *
12343 + * You should have received a copy of the GNU General Public License
12344 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12345 + */
12346 +
12347 +/*
12348 + * dynamically customizable operations for regular files
12349 + */
12350 +
12351 +#include "aufs.h"
12352 +
12353 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12354 +
12355 +/*
12356 + * How large will these lists be?
12357 + * Usually just a few elements, 20-30 at most for each, I guess.
12358 + */
12359 +static struct hlist_bl_head dynop[AuDyLast];
12360 +
12361 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12362 +                                    const void *h_op)
12363 +{
12364 +       struct au_dykey *key, *tmp;
12365 +       struct hlist_bl_node *pos;
12366 +
12367 +       key = NULL;
12368 +       hlist_bl_lock(hbl);
12369 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12370 +               if (tmp->dk_op.dy_hop == h_op) {
12371 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12372 +                               key = tmp;
12373 +                       break;
12374 +               }
12375 +       hlist_bl_unlock(hbl);
12376 +
12377 +       return key;
12378 +}
12379 +
12380 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12381 +{
12382 +       struct au_dykey **k, *found;
12383 +       const void *h_op = key->dk_op.dy_hop;
12384 +       int i;
12385 +
12386 +       found = NULL;
12387 +       k = br->br_dykey;
12388 +       for (i = 0; i < AuBrDynOp; i++)
12389 +               if (k[i]) {
12390 +                       if (k[i]->dk_op.dy_hop == h_op) {
12391 +                               found = k[i];
12392 +                               break;
12393 +                       }
12394 +               } else
12395 +                       break;
12396 +       if (!found) {
12397 +               spin_lock(&br->br_dykey_lock);
12398 +               for (; i < AuBrDynOp; i++)
12399 +                       if (k[i]) {
12400 +                               if (k[i]->dk_op.dy_hop == h_op) {
12401 +                                       found = k[i];
12402 +                                       break;
12403 +                               }
12404 +                       } else {
12405 +                               k[i] = key;
12406 +                               break;
12407 +                       }
12408 +               spin_unlock(&br->br_dykey_lock);
12409 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12410 +       }
12411 +
12412 +       return found;
12413 +}
12414 +
12415 +/* kref_get() if @key is already added */
12416 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12417 +{
12418 +       struct au_dykey *tmp, *found;
12419 +       struct hlist_bl_node *pos;
12420 +       const void *h_op = key->dk_op.dy_hop;
12421 +
12422 +       found = NULL;
12423 +       hlist_bl_lock(hbl);
12424 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12425 +               if (tmp->dk_op.dy_hop == h_op) {
12426 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12427 +                               found = tmp;
12428 +                       break;
12429 +               }
12430 +       if (!found)
12431 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12432 +       hlist_bl_unlock(hbl);
12433 +
12434 +       if (!found)
12435 +               DyPrSym(key);
12436 +       return found;
12437 +}
12438 +
12439 +static void dy_free_rcu(struct rcu_head *rcu)
12440 +{
12441 +       struct au_dykey *key;
12442 +
12443 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12444 +       DyPrSym(key);
12445 +       kfree(key);
12446 +}
12447 +
12448 +static void dy_free(struct kref *kref)
12449 +{
12450 +       struct au_dykey *key;
12451 +       struct hlist_bl_head *hbl;
12452 +
12453 +       key = container_of(kref, struct au_dykey, dk_kref);
12454 +       hbl = dynop + key->dk_op.dy_type;
12455 +       au_hbl_del(&key->dk_hnode, hbl);
12456 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12457 +}
12458 +
12459 +void au_dy_put(struct au_dykey *key)
12460 +{
12461 +       kref_put(&key->dk_kref, dy_free);
12462 +}
12463 +
12464 +/* ---------------------------------------------------------------------- */
12465 +
12466 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12467 +
12468 +#ifdef CONFIG_AUFS_DEBUG
12469 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12470 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12471 +#else
12472 +#define DyDbgDeclare(cnt)      do {} while (0)
12473 +#define DyDbgInc(cnt)          do {} while (0)
12474 +#endif
12475 +
12476 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12477 +       DyDbgInc(cnt);                                                  \
12478 +       if (h_op->func) {                                               \
12479 +               if (src.func)                                           \
12480 +                       dst.func = src.func;                            \
12481 +               else                                                    \
12482 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12483 +       }                                                               \
12484 +} while (0)
12485 +
12486 +#define DySetForce(func, dst, src) do {                \
12487 +       AuDebugOn(!src.func);                   \
12488 +       DyDbgInc(cnt);                          \
12489 +       dst.func = src.func;                    \
12490 +} while (0)
12491 +
12492 +#define DySetAop(func) \
12493 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12494 +#define DySetAopForce(func) \
12495 +       DySetForce(func, dyaop->da_op, aufs_aop)
12496 +
12497 +static void dy_aop(struct au_dykey *key, const void *h_op,
12498 +                  struct super_block *h_sb __maybe_unused)
12499 +{
12500 +       struct au_dyaop *dyaop = (void *)key;
12501 +       const struct address_space_operations *h_aop = h_op;
12502 +       DyDbgDeclare(cnt);
12503 +
12504 +       AuDbg("%s\n", au_sbtype(h_sb));
12505 +
12506 +       DySetAop(writepage);
12507 +       DySetAopForce(read_folio);      /* force */
12508 +       DySetAop(writepages);
12509 +       DySetAop(dirty_folio);
12510 +       DySetAop(invalidate_folio);
12511 +       DySetAop(readahead);
12512 +       DySetAop(write_begin);
12513 +       DySetAop(write_end);
12514 +       DySetAop(bmap);
12515 +       DySetAop(release_folio);
12516 +       DySetAop(free_folio);
12517 +       /* this one will be changed according to an aufs mount option */
12518 +       DySetAop(direct_IO);
12519 +       DySetAop(migrate_folio);
12520 +       DySetAop(launder_folio);
12521 +       DySetAop(is_partially_uptodate);
12522 +       DySetAop(is_dirty_writeback);
12523 +       DySetAop(error_remove_page);
12524 +       DySetAop(swap_activate);
12525 +       DySetAop(swap_deactivate);
12526 +       DySetAop(swap_rw);
12527 +
12528 +       DyDbgSize(cnt, *h_aop);
12529 +}
12530 +
12531 +/* ---------------------------------------------------------------------- */
12532 +
12533 +static void dy_bug(struct kref *kref)
12534 +{
12535 +       BUG();
12536 +}
12537 +
12538 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12539 +{
12540 +       struct au_dykey *key, *old;
12541 +       struct hlist_bl_head *hbl;
12542 +       struct op {
12543 +               unsigned int sz;
12544 +               void (*set)(struct au_dykey *key, const void *h_op,
12545 +                           struct super_block *h_sb __maybe_unused);
12546 +       };
12547 +       static const struct op a[] = {
12548 +               [AuDy_AOP] = {
12549 +                       .sz     = sizeof(struct au_dyaop),
12550 +                       .set    = dy_aop
12551 +               }
12552 +       };
12553 +       const struct op *p;
12554 +
12555 +       hbl = dynop + op->dy_type;
12556 +       key = dy_gfind_get(hbl, op->dy_hop);
12557 +       if (key)
12558 +               goto out_add; /* success */
12559 +
12560 +       p = a + op->dy_type;
12561 +       key = kzalloc(p->sz, GFP_NOFS);
12562 +       if (unlikely(!key)) {
12563 +               key = ERR_PTR(-ENOMEM);
12564 +               goto out;
12565 +       }
12566 +
12567 +       key->dk_op.dy_hop = op->dy_hop;
12568 +       kref_init(&key->dk_kref);
12569 +       p->set(key, op->dy_hop, au_br_sb(br));
12570 +       old = dy_gadd(hbl, key);
12571 +       if (old) {
12572 +               au_kfree_rcu(key);
12573 +               key = old;
12574 +       }
12575 +
12576 +out_add:
12577 +       old = dy_bradd(br, key);
12578 +       if (old)
12579 +               /* its ref-count should never be zero here */
12580 +               kref_put(&key->dk_kref, dy_bug);
12581 +out:
12582 +       return key;
12583 +}
12584 +
12585 +/* ---------------------------------------------------------------------- */
12586 +/*
12587 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12588 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12589 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12590 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12591 + * See the aufs manual in detail.
12592 + */
12593 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12594 +{
12595 +       if (!do_dx)
12596 +               dyaop->da_op.direct_IO = NULL;
12597 +       else
12598 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12599 +}
12600 +
12601 +static struct au_dyaop *dy_aget(struct au_branch *br,
12602 +                               const struct address_space_operations *h_aop,
12603 +                               int do_dx)
12604 +{
12605 +       struct au_dyaop *dyaop;
12606 +       struct au_dynop op;
12607 +
12608 +       op.dy_type = AuDy_AOP;
12609 +       op.dy_haop = h_aop;
12610 +       dyaop = (void *)dy_get(&op, br);
12611 +       if (IS_ERR(dyaop))
12612 +               goto out;
12613 +       dy_adx(dyaop, do_dx);
12614 +
12615 +out:
12616 +       return dyaop;
12617 +}
12618 +
12619 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12620 +               struct inode *h_inode)
12621 +{
12622 +       int err, do_dx;
12623 +       struct super_block *sb;
12624 +       struct au_branch *br;
12625 +       struct au_dyaop *dyaop;
12626 +
12627 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12628 +       IiMustWriteLock(inode);
12629 +
12630 +       sb = inode->i_sb;
12631 +       br = au_sbr(sb, bindex);
12632 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12633 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12634 +       err = PTR_ERR(dyaop);
12635 +       if (IS_ERR(dyaop))
12636 +               /* unnecessary to call dy_fput() */
12637 +               goto out;
12638 +
12639 +       err = 0;
12640 +       inode->i_mapping->a_ops = &dyaop->da_op;
12641 +
12642 +out:
12643 +       return err;
12644 +}
12645 +
12646 +/*
12647 + * Is it safe to replace a_ops during the inode/file is in operation?
12648 + * Yes, I hope so.
12649 + */
12650 +int au_dy_irefresh(struct inode *inode)
12651 +{
12652 +       int err;
12653 +       aufs_bindex_t btop;
12654 +       struct inode *h_inode;
12655 +
12656 +       err = 0;
12657 +       if (S_ISREG(inode->i_mode)) {
12658 +               btop = au_ibtop(inode);
12659 +               h_inode = au_h_iptr(inode, btop);
12660 +               err = au_dy_iaop(inode, btop, h_inode);
12661 +       }
12662 +       return err;
12663 +}
12664 +
12665 +void au_dy_arefresh(int do_dx)
12666 +{
12667 +       struct hlist_bl_head *hbl;
12668 +       struct hlist_bl_node *pos;
12669 +       struct au_dykey *key;
12670 +
12671 +       hbl = dynop + AuDy_AOP;
12672 +       hlist_bl_lock(hbl);
12673 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12674 +               dy_adx((void *)key, do_dx);
12675 +       hlist_bl_unlock(hbl);
12676 +}
12677 +
12678 +/* ---------------------------------------------------------------------- */
12679 +
12680 +void __init au_dy_init(void)
12681 +{
12682 +       int i;
12683 +
12684 +       for (i = 0; i < AuDyLast; i++)
12685 +               INIT_HLIST_BL_HEAD(dynop + i);
12686 +}
12687 +
12688 +void au_dy_fin(void)
12689 +{
12690 +       int i;
12691 +
12692 +       for (i = 0; i < AuDyLast; i++)
12693 +               WARN_ON(!hlist_bl_empty(dynop + i));
12694 +}
12695 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12696 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12697 +++ linux/fs/aufs/dynop.h       2022-11-05 23:02:18.962555950 +0100
12698 @@ -0,0 +1,77 @@
12699 +/* SPDX-License-Identifier: GPL-2.0 */
12700 +/*
12701 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12702 + *
12703 + * This program is free software; you can redistribute it and/or modify
12704 + * it under the terms of the GNU General Public License as published by
12705 + * the Free Software Foundation; either version 2 of the License, or
12706 + * (at your option) any later version.
12707 + *
12708 + * This program is distributed in the hope that it will be useful,
12709 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12710 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12711 + * GNU General Public License for more details.
12712 + *
12713 + * You should have received a copy of the GNU General Public License
12714 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12715 + */
12716 +
12717 +/*
12718 + * dynamically customizable operations (for regular files only)
12719 + */
12720 +
12721 +#ifndef __AUFS_DYNOP_H__
12722 +#define __AUFS_DYNOP_H__
12723 +
12724 +#ifdef __KERNEL__
12725 +
12726 +#include <linux/fs.h>
12727 +#include <linux/kref.h>
12728 +
12729 +enum {AuDy_AOP, AuDyLast};
12730 +
12731 +struct au_dynop {
12732 +       int                                             dy_type;
12733 +       union {
12734 +               const void                              *dy_hop;
12735 +               const struct address_space_operations   *dy_haop;
12736 +       };
12737 +};
12738 +
12739 +struct au_dykey {
12740 +       union {
12741 +               struct hlist_bl_node    dk_hnode;
12742 +               struct rcu_head         dk_rcu;
12743 +       };
12744 +       struct au_dynop         dk_op;
12745 +
12746 +       /*
12747 +        * during I am in the branch local array, kref is gotten. when the
12748 +        * branch is removed, kref is put.
12749 +        */
12750 +       struct kref             dk_kref;
12751 +};
12752 +
12753 +/* stop unioning since their sizes are very different from each other */
12754 +struct au_dyaop {
12755 +       struct au_dykey                 da_key;
12756 +       struct address_space_operations da_op; /* not const */
12757 +};
12758 +/* make sure that 'struct au_dykey *' can be any type */
12759 +static_assert(!offsetof(struct au_dyaop, da_key));
12760 +
12761 +/* ---------------------------------------------------------------------- */
12762 +
12763 +/* dynop.c */
12764 +struct au_branch;
12765 +void au_dy_put(struct au_dykey *key);
12766 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12767 +               struct inode *h_inode);
12768 +int au_dy_irefresh(struct inode *inode);
12769 +void au_dy_arefresh(int do_dio);
12770 +
12771 +void __init au_dy_init(void);
12772 +void au_dy_fin(void);
12773 +
12774 +#endif /* __KERNEL__ */
12775 +#endif /* __AUFS_DYNOP_H__ */
12776 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12777 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12778 +++ linux/fs/aufs/export.c      2022-12-17 09:21:34.796521861 +0100
12779 @@ -0,0 +1,830 @@
12780 +// SPDX-License-Identifier: GPL-2.0
12781 +/*
12782 + * Copyright (C) 2005-2022 Junjiro R. Okajima
12783 + *
12784 + * This program is free software; you can redistribute it and/or modify
12785 + * it under the terms of the GNU General Public License as published by
12786 + * the Free Software Foundation; either version 2 of the License, or
12787 + * (at your option) any later version.
12788 + *
12789 + * This program is distributed in the hope that it will be useful,
12790 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12791 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12792 + * GNU General Public License for more details.
12793 + *
12794 + * You should have received a copy of the GNU General Public License
12795 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12796 + */
12797 +
12798 +/*
12799 + * export via nfs
12800 + */
12801 +
12802 +#include <linux/exportfs.h>
12803 +#include <linux/fs_struct.h>
12804 +#include <linux/nsproxy.h>
12805 +#include <linux/random.h>
12806 +#include <linux/writeback.h>
12807 +#include "aufs.h"
12808 +
12809 +union conv {
12810 +#ifdef CONFIG_AUFS_INO_T_64
12811 +       __u32 a[2];
12812 +#else
12813 +       __u32 a[1];
12814 +#endif
12815 +       ino_t ino;
12816 +};
12817 +
12818 +static ino_t decode_ino(__u32 *a)
12819 +{
12820 +       union conv u;
12821 +
12822 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12823 +       u.a[0] = a[0];
12824 +#ifdef CONFIG_AUFS_INO_T_64
12825 +       u.a[1] = a[1];
12826 +#endif
12827 +       return u.ino;
12828 +}
12829 +
12830 +static void encode_ino(__u32 *a, ino_t ino)
12831 +{
12832 +       union conv u;
12833 +
12834 +       u.ino = ino;
12835 +       a[0] = u.a[0];
12836 +#ifdef CONFIG_AUFS_INO_T_64
12837 +       a[1] = u.a[1];
12838 +#endif
12839 +}
12840 +
12841 +/* NFS file handle */
12842 +enum {
12843 +       Fh_br_id,
12844 +       Fh_sigen,
12845 +#ifdef CONFIG_AUFS_INO_T_64
12846 +       /* support 64bit inode number */
12847 +       Fh_ino1,
12848 +       Fh_ino2,
12849 +       Fh_dir_ino1,
12850 +       Fh_dir_ino2,
12851 +#else
12852 +       Fh_ino1,
12853 +       Fh_dir_ino1,
12854 +#endif
12855 +       Fh_igen,
12856 +       Fh_h_type,
12857 +       Fh_tail,
12858 +
12859 +       Fh_ino = Fh_ino1,
12860 +       Fh_dir_ino = Fh_dir_ino1
12861 +};
12862 +
12863 +static int au_test_anon(struct dentry *dentry)
12864 +{
12865 +       /* note: read d_flags without d_lock */
12866 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12867 +}
12868 +
12869 +int au_test_nfsd(void)
12870 +{
12871 +       int ret;
12872 +       struct task_struct *tsk = current;
12873 +       char comm[sizeof(tsk->comm)];
12874 +
12875 +       ret = 0;
12876 +       if (tsk->flags & PF_KTHREAD) {
12877 +               get_task_comm(comm, tsk);
12878 +               ret = !strcmp(comm, "nfsd");
12879 +       }
12880 +
12881 +       return ret;
12882 +}
12883 +
12884 +/* ---------------------------------------------------------------------- */
12885 +/* inode generation external table */
12886 +
12887 +void au_xigen_inc(struct inode *inode)
12888 +{
12889 +       loff_t pos;
12890 +       ssize_t sz;
12891 +       __u32 igen;
12892 +       struct super_block *sb;
12893 +       struct au_sbinfo *sbinfo;
12894 +
12895 +       sb = inode->i_sb;
12896 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12897 +
12898 +       sbinfo = au_sbi(sb);
12899 +       pos = inode->i_ino;
12900 +       pos *= sizeof(igen);
12901 +       igen = inode->i_generation + 1;
12902 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12903 +       if (sz == sizeof(igen))
12904 +               return; /* success */
12905 +
12906 +       if (unlikely(sz >= 0))
12907 +               AuIOErr("xigen error (%zd)\n", sz);
12908 +}
12909 +
12910 +int au_xigen_new(struct inode *inode)
12911 +{
12912 +       int err;
12913 +       loff_t pos;
12914 +       ssize_t sz;
12915 +       struct super_block *sb;
12916 +       struct au_sbinfo *sbinfo;
12917 +       struct file *file;
12918 +
12919 +       err = 0;
12920 +       /* todo: dirty, at mount time */
12921 +       if (inode->i_ino == AUFS_ROOT_INO)
12922 +               goto out;
12923 +       sb = inode->i_sb;
12924 +       SiMustAnyLock(sb);
12925 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12926 +               goto out;
12927 +
12928 +       err = -EFBIG;
12929 +       pos = inode->i_ino;
12930 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12931 +               AuIOErr1("too large i%lld\n", pos);
12932 +               goto out;
12933 +       }
12934 +       pos *= sizeof(inode->i_generation);
12935 +
12936 +       err = 0;
12937 +       sbinfo = au_sbi(sb);
12938 +       file = sbinfo->si_xigen;
12939 +       BUG_ON(!file);
12940 +
12941 +       if (vfsub_f_size_read(file)
12942 +           < pos + sizeof(inode->i_generation)) {
12943 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12944 +               sz = xino_fwrite(file, &inode->i_generation,
12945 +                                sizeof(inode->i_generation), &pos);
12946 +       } else
12947 +               sz = xino_fread(file, &inode->i_generation,
12948 +                               sizeof(inode->i_generation), &pos);
12949 +       if (sz == sizeof(inode->i_generation))
12950 +               goto out; /* success */
12951 +
12952 +       err = sz;
12953 +       if (unlikely(sz >= 0)) {
12954 +               err = -EIO;
12955 +               AuIOErr("xigen error (%zd)\n", sz);
12956 +       }
12957 +
12958 +out:
12959 +       return err;
12960 +}
12961 +
12962 +int au_xigen_set(struct super_block *sb, struct path *path)
12963 +{
12964 +       int err;
12965 +       struct au_sbinfo *sbinfo;
12966 +       struct file *file;
12967 +
12968 +       SiMustWriteLock(sb);
12969 +
12970 +       sbinfo = au_sbi(sb);
12971 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12972 +       err = PTR_ERR(file);
12973 +       if (IS_ERR(file))
12974 +               goto out;
12975 +       err = 0;
12976 +       if (sbinfo->si_xigen)
12977 +               fput(sbinfo->si_xigen);
12978 +       sbinfo->si_xigen = file;
12979 +
12980 +out:
12981 +       AuTraceErr(err);
12982 +       return err;
12983 +}
12984 +
12985 +void au_xigen_clr(struct super_block *sb)
12986 +{
12987 +       struct au_sbinfo *sbinfo;
12988 +
12989 +       SiMustWriteLock(sb);
12990 +
12991 +       sbinfo = au_sbi(sb);
12992 +       if (sbinfo->si_xigen) {
12993 +               fput(sbinfo->si_xigen);
12994 +               sbinfo->si_xigen = NULL;
12995 +       }
12996 +}
12997 +
12998 +/* ---------------------------------------------------------------------- */
12999 +
13000 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13001 +                                   ino_t dir_ino)
13002 +{
13003 +       struct dentry *dentry, *d;
13004 +       struct inode *inode;
13005 +       unsigned int sigen;
13006 +
13007 +       dentry = NULL;
13008 +       inode = ilookup(sb, ino);
13009 +       if (!inode)
13010 +               goto out;
13011 +
13012 +       dentry = ERR_PTR(-ESTALE);
13013 +       sigen = au_sigen(sb);
13014 +       if (unlikely(au_is_bad_inode(inode)
13015 +                    || IS_DEADDIR(inode)
13016 +                    || sigen != au_iigen(inode, NULL)))
13017 +               goto out_iput;
13018 +
13019 +       dentry = NULL;
13020 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13021 +               dentry = d_find_alias(inode);
13022 +       else {
13023 +               spin_lock(&inode->i_lock);
13024 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13025 +                       spin_lock(&d->d_lock);
13026 +                       if (!au_test_anon(d)
13027 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13028 +                               dentry = dget_dlock(d);
13029 +                               spin_unlock(&d->d_lock);
13030 +                               break;
13031 +                       }
13032 +                       spin_unlock(&d->d_lock);
13033 +               }
13034 +               spin_unlock(&inode->i_lock);
13035 +       }
13036 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13037 +               /* need to refresh */
13038 +               dput(dentry);
13039 +               dentry = NULL;
13040 +       }
13041 +
13042 +out_iput:
13043 +       iput(inode);
13044 +out:
13045 +       AuTraceErrPtr(dentry);
13046 +       return dentry;
13047 +}
13048 +
13049 +/* ---------------------------------------------------------------------- */
13050 +
13051 +/* todo: dirty? */
13052 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13053 +
13054 +struct au_compare_mnt_args {
13055 +       /* input */
13056 +       struct super_block *sb;
13057 +
13058 +       /* output */
13059 +       struct vfsmount *mnt;
13060 +};
13061 +
13062 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13063 +{
13064 +       struct au_compare_mnt_args *a = arg;
13065 +
13066 +       if (mnt->mnt_sb != a->sb)
13067 +               return 0;
13068 +       a->mnt = mntget(mnt);
13069 +       return 1;
13070 +}
13071 +
13072 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13073 +{
13074 +       int err;
13075 +       struct path root;
13076 +       struct au_compare_mnt_args args = {
13077 +               .sb = sb
13078 +       };
13079 +
13080 +       get_fs_root(current->fs, &root);
13081 +       rcu_read_lock();
13082 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13083 +       rcu_read_unlock();
13084 +       path_put(&root);
13085 +       AuDebugOn(!err);
13086 +       AuDebugOn(!args.mnt);
13087 +       return args.mnt;
13088 +}
13089 +
13090 +struct au_nfsd_si_lock {
13091 +       unsigned int sigen;
13092 +       aufs_bindex_t bindex, br_id;
13093 +       unsigned char force_lock;
13094 +};
13095 +
13096 +static int si_nfsd_read_lock(struct super_block *sb,
13097 +                            struct au_nfsd_si_lock *nsi_lock)
13098 +{
13099 +       int err;
13100 +       aufs_bindex_t bindex;
13101 +
13102 +       si_read_lock(sb, AuLock_FLUSH);
13103 +
13104 +       /* branch id may be wrapped around */
13105 +       err = 0;
13106 +       bindex = au_br_index(sb, nsi_lock->br_id);
13107 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13108 +               goto out; /* success */
13109 +
13110 +       err = -ESTALE;
13111 +       bindex = -1;
13112 +       if (!nsi_lock->force_lock)
13113 +               si_read_unlock(sb);
13114 +
13115 +out:
13116 +       nsi_lock->bindex = bindex;
13117 +       return err;
13118 +}
13119 +
13120 +struct find_name_by_ino {
13121 +       struct dir_context ctx;
13122 +       int called, found;
13123 +       ino_t ino;
13124 +       char *name;
13125 +       int namelen;
13126 +};
13127 +
13128 +static bool
13129 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13130 +                loff_t offset, u64 ino, unsigned int d_type)
13131 +{
13132 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13133 +                                                 ctx);
13134 +
13135 +       a->called++;
13136 +       if (a->ino != ino)
13137 +               return true;
13138 +
13139 +       memcpy(a->name, name, namelen);
13140 +       a->namelen = namelen;
13141 +       a->found = 1;
13142 +       return false;
13143 +}
13144 +
13145 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13146 +                                    struct au_nfsd_si_lock *nsi_lock)
13147 +{
13148 +       struct dentry *dentry, *parent;
13149 +       struct file *file;
13150 +       struct inode *dir;
13151 +       struct find_name_by_ino arg = {
13152 +               .ctx = {
13153 +                       .actor = find_name_by_ino
13154 +               }
13155 +       };
13156 +       int err;
13157 +
13158 +       parent = path->dentry;
13159 +       if (nsi_lock)
13160 +               si_read_unlock(parent->d_sb);
13161 +       file = vfsub_dentry_open(path, au_dir_roflags);
13162 +       dentry = (void *)file;
13163 +       if (IS_ERR(file))
13164 +               goto out;
13165 +
13166 +       dentry = ERR_PTR(-ENOMEM);
13167 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13168 +       if (unlikely(!arg.name))
13169 +               goto out_file;
13170 +       arg.ino = ino;
13171 +       arg.found = 0;
13172 +       do {
13173 +               arg.called = 0;
13174 +               /* smp_mb(); */
13175 +               err = vfsub_iterate_dir(file, &arg.ctx);
13176 +       } while (!err && !arg.found && arg.called);
13177 +       dentry = ERR_PTR(err);
13178 +       if (unlikely(err))
13179 +               goto out_name;
13180 +       /* instead of ENOENT */
13181 +       dentry = ERR_PTR(-ESTALE);
13182 +       if (!arg.found)
13183 +               goto out_name;
13184 +
13185 +       /* do not call vfsub_lkup_one() */
13186 +       dir = d_inode(parent);
13187 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13188 +       AuTraceErrPtr(dentry);
13189 +       if (IS_ERR(dentry))
13190 +               goto out_name;
13191 +       AuDebugOn(au_test_anon(dentry));
13192 +       if (unlikely(d_really_is_negative(dentry))) {
13193 +               dput(dentry);
13194 +               dentry = ERR_PTR(-ENOENT);
13195 +       }
13196 +
13197 +out_name:
13198 +       free_page((unsigned long)arg.name);
13199 +out_file:
13200 +       fput(file);
13201 +out:
13202 +       if (unlikely(nsi_lock
13203 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13204 +               if (!IS_ERR(dentry)) {
13205 +                       dput(dentry);
13206 +                       dentry = ERR_PTR(-ESTALE);
13207 +               }
13208 +       AuTraceErrPtr(dentry);
13209 +       return dentry;
13210 +}
13211 +
13212 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13213 +                                       ino_t dir_ino,
13214 +                                       struct au_nfsd_si_lock *nsi_lock)
13215 +{
13216 +       struct dentry *dentry;
13217 +       struct path path;
13218 +
13219 +       if (dir_ino != AUFS_ROOT_INO) {
13220 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13221 +               dentry = path.dentry;
13222 +               if (!path.dentry || IS_ERR(path.dentry))
13223 +                       goto out;
13224 +               AuDebugOn(au_test_anon(path.dentry));
13225 +       } else
13226 +               path.dentry = dget(sb->s_root);
13227 +
13228 +       path.mnt = au_mnt_get(sb);
13229 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13230 +       path_put(&path);
13231 +
13232 +out:
13233 +       AuTraceErrPtr(dentry);
13234 +       return dentry;
13235 +}
13236 +
13237 +/* ---------------------------------------------------------------------- */
13238 +
13239 +static int h_acceptable(void *expv, struct dentry *dentry)
13240 +{
13241 +       return 1;
13242 +}
13243 +
13244 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13245 +                          char *buf, int len, struct super_block *sb)
13246 +{
13247 +       char *p;
13248 +       int n;
13249 +       struct path path;
13250 +
13251 +       p = d_path(h_rootpath, buf, len);
13252 +       if (IS_ERR(p))
13253 +               goto out;
13254 +       n = strlen(p);
13255 +
13256 +       path.mnt = h_rootpath->mnt;
13257 +       path.dentry = h_parent;
13258 +       p = d_path(&path, buf, len);
13259 +       if (IS_ERR(p))
13260 +               goto out;
13261 +       if (n != 1)
13262 +               p += n;
13263 +
13264 +       path.mnt = au_mnt_get(sb);
13265 +       path.dentry = sb->s_root;
13266 +       p = d_path(&path, buf, len - strlen(p));
13267 +       mntput(path.mnt);
13268 +       if (IS_ERR(p))
13269 +               goto out;
13270 +       if (n != 1)
13271 +               p[strlen(p)] = '/';
13272 +
13273 +out:
13274 +       AuTraceErrPtr(p);
13275 +       return p;
13276 +}
13277 +
13278 +static
13279 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13280 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13281 +{
13282 +       struct dentry *dentry, *h_parent, *root;
13283 +       struct super_block *h_sb;
13284 +       char *pathname, *p;
13285 +       struct vfsmount *h_mnt;
13286 +       struct au_branch *br;
13287 +       int err;
13288 +       struct path path;
13289 +
13290 +       br = au_sbr(sb, nsi_lock->bindex);
13291 +       h_mnt = au_br_mnt(br);
13292 +       h_sb = h_mnt->mnt_sb;
13293 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13294 +       lockdep_off();
13295 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13296 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13297 +                                     h_acceptable, /*context*/NULL);
13298 +       lockdep_on();
13299 +       dentry = h_parent;
13300 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13301 +               AuWarn1("%s decode_fh failed, %ld\n",
13302 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13303 +               goto out;
13304 +       }
13305 +       dentry = NULL;
13306 +       if (unlikely(au_test_anon(h_parent))) {
13307 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13308 +                       au_sbtype(h_sb));
13309 +               goto out_h_parent;
13310 +       }
13311 +
13312 +       dentry = ERR_PTR(-ENOMEM);
13313 +       pathname = (void *)__get_free_page(GFP_NOFS);
13314 +       if (unlikely(!pathname))
13315 +               goto out_h_parent;
13316 +
13317 +       root = sb->s_root;
13318 +       path.mnt = h_mnt;
13319 +       di_read_lock_parent(root, !AuLock_IR);
13320 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13321 +       di_read_unlock(root, !AuLock_IR);
13322 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13323 +       dentry = (void *)p;
13324 +       if (IS_ERR(p))
13325 +               goto out_pathname;
13326 +
13327 +       si_read_unlock(sb);
13328 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13329 +       dentry = ERR_PTR(err);
13330 +       if (unlikely(err))
13331 +               goto out_relock;
13332 +
13333 +       dentry = ERR_PTR(-ENOENT);
13334 +       AuDebugOn(au_test_anon(path.dentry));
13335 +       if (unlikely(d_really_is_negative(path.dentry)))
13336 +               goto out_path;
13337 +
13338 +       if (ino != d_inode(path.dentry)->i_ino)
13339 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13340 +       else
13341 +               dentry = dget(path.dentry);
13342 +
13343 +out_path:
13344 +       path_put(&path);
13345 +out_relock:
13346 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13347 +               if (!IS_ERR(dentry)) {
13348 +                       dput(dentry);
13349 +                       dentry = ERR_PTR(-ESTALE);
13350 +               }
13351 +out_pathname:
13352 +       free_page((unsigned long)pathname);
13353 +out_h_parent:
13354 +       dput(h_parent);
13355 +out:
13356 +       AuTraceErrPtr(dentry);
13357 +       return dentry;
13358 +}
13359 +
13360 +/* ---------------------------------------------------------------------- */
13361 +
13362 +static struct dentry *
13363 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13364 +                 int fh_type)
13365 +{
13366 +       struct dentry *dentry;
13367 +       __u32 *fh = fid->raw;
13368 +       struct au_branch *br;
13369 +       ino_t ino, dir_ino;
13370 +       struct au_nfsd_si_lock nsi_lock = {
13371 +               .force_lock     = 0
13372 +       };
13373 +
13374 +       dentry = ERR_PTR(-ESTALE);
13375 +       /* it should never happen, but the file handle is unreliable */
13376 +       if (unlikely(fh_len < Fh_tail))
13377 +               goto out;
13378 +       nsi_lock.sigen = fh[Fh_sigen];
13379 +       nsi_lock.br_id = fh[Fh_br_id];
13380 +
13381 +       /* branch id may be wrapped around */
13382 +       br = NULL;
13383 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13384 +               goto out;
13385 +       nsi_lock.force_lock = 1;
13386 +
13387 +       /* is this inode still cached? */
13388 +       ino = decode_ino(fh + Fh_ino);
13389 +       /* it should never happen */
13390 +       if (unlikely(ino == AUFS_ROOT_INO))
13391 +               goto out_unlock;
13392 +
13393 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13394 +       dentry = decode_by_ino(sb, ino, dir_ino);
13395 +       if (IS_ERR(dentry))
13396 +               goto out_unlock;
13397 +       if (dentry)
13398 +               goto accept;
13399 +
13400 +       /* is the parent dir cached? */
13401 +       br = au_sbr(sb, nsi_lock.bindex);
13402 +       au_lcnt_inc(&br->br_nfiles);
13403 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13404 +       if (IS_ERR(dentry))
13405 +               goto out_unlock;
13406 +       if (dentry)
13407 +               goto accept;
13408 +
13409 +       /* lookup path */
13410 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13411 +       if (IS_ERR(dentry))
13412 +               goto out_unlock;
13413 +       if (unlikely(!dentry))
13414 +               /* todo?: make it ESTALE */
13415 +               goto out_unlock;
13416 +
13417 +accept:
13418 +       if (!au_digen_test(dentry, au_sigen(sb))
13419 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13420 +               goto out_unlock; /* success */
13421 +
13422 +       dput(dentry);
13423 +       dentry = ERR_PTR(-ESTALE);
13424 +out_unlock:
13425 +       if (br)
13426 +               au_lcnt_dec(&br->br_nfiles);
13427 +       si_read_unlock(sb);
13428 +out:
13429 +       AuTraceErrPtr(dentry);
13430 +       return dentry;
13431 +}
13432 +
13433 +#if 0 /* reserved for future use */
13434 +/* support subtreecheck option */
13435 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13436 +                                       int fh_len, int fh_type)
13437 +{
13438 +       struct dentry *parent;
13439 +       __u32 *fh = fid->raw;
13440 +       ino_t dir_ino;
13441 +
13442 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13443 +       parent = decode_by_ino(sb, dir_ino, 0);
13444 +       if (IS_ERR(parent))
13445 +               goto out;
13446 +       if (!parent)
13447 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13448 +                                       dir_ino, fh, fh_len);
13449 +
13450 +out:
13451 +       AuTraceErrPtr(parent);
13452 +       return parent;
13453 +}
13454 +#endif
13455 +
13456 +/* ---------------------------------------------------------------------- */
13457 +
13458 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13459 +                         struct inode *dir)
13460 +{
13461 +       int err;
13462 +       aufs_bindex_t bindex;
13463 +       struct super_block *sb, *h_sb;
13464 +       struct dentry *dentry, *parent, *h_parent;
13465 +       struct inode *h_dir;
13466 +       struct au_branch *br;
13467 +
13468 +       err = -ENOSPC;
13469 +       if (unlikely(*max_len <= Fh_tail)) {
13470 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13471 +               goto out;
13472 +       }
13473 +
13474 +       err = FILEID_ROOT;
13475 +       if (inode->i_ino == AUFS_ROOT_INO) {
13476 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13477 +               goto out;
13478 +       }
13479 +
13480 +       h_parent = NULL;
13481 +       sb = inode->i_sb;
13482 +       err = si_read_lock(sb, AuLock_FLUSH);
13483 +       if (unlikely(err))
13484 +               goto out;
13485 +
13486 +#ifdef CONFIG_AUFS_DEBUG
13487 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13488 +               AuWarn1("NFS-exporting requires xino\n");
13489 +#endif
13490 +       err = -EIO;
13491 +       parent = NULL;
13492 +       ii_read_lock_child(inode);
13493 +       bindex = au_ibtop(inode);
13494 +       if (!dir) {
13495 +               dentry = d_find_any_alias(inode);
13496 +               if (unlikely(!dentry))
13497 +                       goto out_unlock;
13498 +               AuDebugOn(au_test_anon(dentry));
13499 +               parent = dget_parent(dentry);
13500 +               dput(dentry);
13501 +               if (unlikely(!parent))
13502 +                       goto out_unlock;
13503 +               if (d_really_is_positive(parent))
13504 +                       dir = d_inode(parent);
13505 +       }
13506 +
13507 +       ii_read_lock_parent(dir);
13508 +       h_dir = au_h_iptr(dir, bindex);
13509 +       ii_read_unlock(dir);
13510 +       if (unlikely(!h_dir))
13511 +               goto out_parent;
13512 +       h_parent = d_find_any_alias(h_dir);
13513 +       if (unlikely(!h_parent))
13514 +               goto out_hparent;
13515 +
13516 +       err = -EPERM;
13517 +       br = au_sbr(sb, bindex);
13518 +       h_sb = au_br_sb(br);
13519 +       if (unlikely(!h_sb->s_export_op)) {
13520 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13521 +               goto out_hparent;
13522 +       }
13523 +
13524 +       fh[Fh_br_id] = br->br_id;
13525 +       fh[Fh_sigen] = au_sigen(sb);
13526 +       encode_ino(fh + Fh_ino, inode->i_ino);
13527 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13528 +       fh[Fh_igen] = inode->i_generation;
13529 +
13530 +       *max_len -= Fh_tail;
13531 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13532 +                                          max_len,
13533 +                                          /*connectable or subtreecheck*/0);
13534 +       err = fh[Fh_h_type];
13535 +       *max_len += Fh_tail;
13536 +       /* todo: macros? */
13537 +       if (err != FILEID_INVALID)
13538 +               err = 99;
13539 +       else
13540 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13541 +
13542 +out_hparent:
13543 +       dput(h_parent);
13544 +out_parent:
13545 +       dput(parent);
13546 +out_unlock:
13547 +       ii_read_unlock(inode);
13548 +       si_read_unlock(sb);
13549 +out:
13550 +       if (unlikely(err < 0))
13551 +               err = FILEID_INVALID;
13552 +       return err;
13553 +}
13554 +
13555 +/* ---------------------------------------------------------------------- */
13556 +
13557 +static int aufs_commit_metadata(struct inode *inode)
13558 +{
13559 +       int err;
13560 +       aufs_bindex_t bindex;
13561 +       struct super_block *sb;
13562 +       struct inode *h_inode;
13563 +       int (*f)(struct inode *inode);
13564 +
13565 +       sb = inode->i_sb;
13566 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13567 +       ii_write_lock_child(inode);
13568 +       bindex = au_ibtop(inode);
13569 +       AuDebugOn(bindex < 0);
13570 +       h_inode = au_h_iptr(inode, bindex);
13571 +
13572 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13573 +       if (f)
13574 +               err = f(h_inode);
13575 +       else
13576 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13577 +
13578 +       au_cpup_attr_timesizes(inode);
13579 +       ii_write_unlock(inode);
13580 +       si_read_unlock(sb);
13581 +       return err;
13582 +}
13583 +
13584 +/* ---------------------------------------------------------------------- */
13585 +
13586 +static struct export_operations aufs_export_op = {
13587 +       .fh_to_dentry           = aufs_fh_to_dentry,
13588 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13589 +       .encode_fh              = aufs_encode_fh,
13590 +       .commit_metadata        = aufs_commit_metadata
13591 +};
13592 +
13593 +void au_export_init(struct super_block *sb)
13594 +{
13595 +       struct au_sbinfo *sbinfo;
13596 +       __u32 u;
13597 +
13598 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13599 +                        && IS_MODULE(CONFIG_EXPORTFS),
13600 +                        AUFS_NAME ": unsupported configuration "
13601 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13602 +
13603 +       sb->s_export_op = &aufs_export_op;
13604 +       sbinfo = au_sbi(sb);
13605 +       sbinfo->si_xigen = NULL;
13606 +       get_random_bytes(&u, sizeof(u));
13607 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13608 +       atomic_set(&sbinfo->si_xigen_next, u);
13609 +}
13610 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13611 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13612 +++ linux/fs/aufs/fhsm.c        2022-11-05 23:02:18.962555950 +0100
13613 @@ -0,0 +1,426 @@
13614 +// SPDX-License-Identifier: GPL-2.0
13615 +/*
13616 + * Copyright (C) 2011-2022 Junjiro R. Okajima
13617 + *
13618 + * This program is free software; you can redistribute it and/or modify
13619 + * it under the terms of the GNU General Public License as published by
13620 + * the Free Software Foundation; either version 2 of the License, or
13621 + * (at your option) any later version.
13622 + *
13623 + * This program is distributed in the hope that it will be useful,
13624 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13625 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13626 + * GNU General Public License for more details.
13627 + *
13628 + * You should have received a copy of the GNU General Public License
13629 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13630 + */
13631 +
13632 +/*
13633 + * File-based Hierarchy Storage Management
13634 + */
13635 +
13636 +#include <linux/anon_inodes.h>
13637 +#include <linux/poll.h>
13638 +#include <linux/seq_file.h>
13639 +#include <linux/statfs.h>
13640 +#include "aufs.h"
13641 +
13642 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13643 +{
13644 +       struct au_sbinfo *sbinfo;
13645 +       struct au_fhsm *fhsm;
13646 +
13647 +       SiMustAnyLock(sb);
13648 +
13649 +       sbinfo = au_sbi(sb);
13650 +       fhsm = &sbinfo->si_fhsm;
13651 +       AuDebugOn(!fhsm);
13652 +       return fhsm->fhsm_bottom;
13653 +}
13654 +
13655 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13656 +{
13657 +       struct au_sbinfo *sbinfo;
13658 +       struct au_fhsm *fhsm;
13659 +
13660 +       SiMustWriteLock(sb);
13661 +
13662 +       sbinfo = au_sbi(sb);
13663 +       fhsm = &sbinfo->si_fhsm;
13664 +       AuDebugOn(!fhsm);
13665 +       fhsm->fhsm_bottom = bindex;
13666 +}
13667 +
13668 +/* ---------------------------------------------------------------------- */
13669 +
13670 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13671 +{
13672 +       struct au_br_fhsm *bf;
13673 +
13674 +       bf = br->br_fhsm;
13675 +       MtxMustLock(&bf->bf_lock);
13676 +
13677 +       return !bf->bf_readable
13678 +               || time_after(jiffies,
13679 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13680 +}
13681 +
13682 +/* ---------------------------------------------------------------------- */
13683 +
13684 +static void au_fhsm_notify(struct super_block *sb, int val)
13685 +{
13686 +       struct au_sbinfo *sbinfo;
13687 +       struct au_fhsm *fhsm;
13688 +
13689 +       SiMustAnyLock(sb);
13690 +
13691 +       sbinfo = au_sbi(sb);
13692 +       fhsm = &sbinfo->si_fhsm;
13693 +       if (au_fhsm_pid(fhsm)
13694 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13695 +               atomic_set(&fhsm->fhsm_readable, val);
13696 +               if (val)
13697 +                       wake_up(&fhsm->fhsm_wqh);
13698 +       }
13699 +}
13700 +
13701 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13702 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13703 +{
13704 +       int err;
13705 +       struct au_branch *br;
13706 +       struct au_br_fhsm *bf;
13707 +
13708 +       br = au_sbr(sb, bindex);
13709 +       AuDebugOn(au_br_rdonly(br));
13710 +       bf = br->br_fhsm;
13711 +       AuDebugOn(!bf);
13712 +
13713 +       if (do_lock)
13714 +               mutex_lock(&bf->bf_lock);
13715 +       else
13716 +               MtxMustLock(&bf->bf_lock);
13717 +
13718 +       /* sb->s_root for NFS is unreliable */
13719 +       err = au_br_stfs(br, &bf->bf_stfs);
13720 +       if (unlikely(err)) {
13721 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13722 +               goto out;
13723 +       }
13724 +
13725 +       bf->bf_jiffy = jiffies;
13726 +       bf->bf_readable = 1;
13727 +       if (do_notify)
13728 +               au_fhsm_notify(sb, /*val*/1);
13729 +       if (rstfs)
13730 +               *rstfs = bf->bf_stfs;
13731 +
13732 +out:
13733 +       if (do_lock)
13734 +               mutex_unlock(&bf->bf_lock);
13735 +       au_fhsm_notify(sb, /*val*/1);
13736 +
13737 +       return err;
13738 +}
13739 +
13740 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13741 +{
13742 +       int err;
13743 +       struct au_sbinfo *sbinfo;
13744 +       struct au_fhsm *fhsm;
13745 +       struct au_branch *br;
13746 +       struct au_br_fhsm *bf;
13747 +
13748 +       AuDbg("b%d, force %d\n", bindex, force);
13749 +       SiMustAnyLock(sb);
13750 +
13751 +       sbinfo = au_sbi(sb);
13752 +       fhsm = &sbinfo->si_fhsm;
13753 +       if (!au_ftest_si(sbinfo, FHSM)
13754 +           || fhsm->fhsm_bottom == bindex)
13755 +               return;
13756 +
13757 +       br = au_sbr(sb, bindex);
13758 +       bf = br->br_fhsm;
13759 +       AuDebugOn(!bf);
13760 +       mutex_lock(&bf->bf_lock);
13761 +       if (force
13762 +           || au_fhsm_pid(fhsm)
13763 +           || au_fhsm_test_jiffy(sbinfo, br))
13764 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13765 +                                 /*do_notify*/1);
13766 +       mutex_unlock(&bf->bf_lock);
13767 +}
13768 +
13769 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13770 +{
13771 +       aufs_bindex_t bindex, bbot;
13772 +       struct au_branch *br;
13773 +
13774 +       /* exclude the bottom */
13775 +       bbot = au_fhsm_bottom(sb);
13776 +       for (bindex = 0; bindex < bbot; bindex++) {
13777 +               br = au_sbr(sb, bindex);
13778 +               if (au_br_fhsm(br->br_perm))
13779 +                       au_fhsm_wrote(sb, bindex, force);
13780 +       }
13781 +}
13782 +
13783 +/* ---------------------------------------------------------------------- */
13784 +
13785 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13786 +{
13787 +       __poll_t mask;
13788 +       struct au_sbinfo *sbinfo;
13789 +       struct au_fhsm *fhsm;
13790 +
13791 +       mask = 0;
13792 +       sbinfo = file->private_data;
13793 +       fhsm = &sbinfo->si_fhsm;
13794 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13795 +       if (atomic_read(&fhsm->fhsm_readable))
13796 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13797 +
13798 +       if (!mask)
13799 +               AuDbg("mask 0x%x\n", mask);
13800 +       return mask;
13801 +}
13802 +
13803 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13804 +                             struct aufs_stfs *stfs, __s16 brid)
13805 +{
13806 +       int err;
13807 +
13808 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13809 +       if (!err)
13810 +               err = __put_user(brid, &stbr->brid);
13811 +       if (unlikely(err))
13812 +               err = -EFAULT;
13813 +
13814 +       return err;
13815 +}
13816 +
13817 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13818 +                              struct aufs_stbr __user *stbr, size_t count)
13819 +{
13820 +       ssize_t err;
13821 +       int nstbr;
13822 +       aufs_bindex_t bindex, bbot;
13823 +       struct au_branch *br;
13824 +       struct au_br_fhsm *bf;
13825 +
13826 +       /* except the bottom branch */
13827 +       err = 0;
13828 +       nstbr = 0;
13829 +       bbot = au_fhsm_bottom(sb);
13830 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13831 +               br = au_sbr(sb, bindex);
13832 +               if (!au_br_fhsm(br->br_perm))
13833 +                       continue;
13834 +
13835 +               bf = br->br_fhsm;
13836 +               mutex_lock(&bf->bf_lock);
13837 +               if (bf->bf_readable) {
13838 +                       err = -EFAULT;
13839 +                       if (count >= sizeof(*stbr))
13840 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13841 +                                                         br->br_id);
13842 +                       if (!err) {
13843 +                               bf->bf_readable = 0;
13844 +                               count -= sizeof(*stbr);
13845 +                               nstbr++;
13846 +                       }
13847 +               }
13848 +               mutex_unlock(&bf->bf_lock);
13849 +       }
13850 +       if (!err)
13851 +               err = sizeof(*stbr) * nstbr;
13852 +
13853 +       return err;
13854 +}
13855 +
13856 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13857 +                          loff_t *pos)
13858 +{
13859 +       ssize_t err;
13860 +       int readable;
13861 +       aufs_bindex_t nfhsm, bindex, bbot;
13862 +       struct au_sbinfo *sbinfo;
13863 +       struct au_fhsm *fhsm;
13864 +       struct au_branch *br;
13865 +       struct super_block *sb;
13866 +
13867 +       err = 0;
13868 +       sbinfo = file->private_data;
13869 +       fhsm = &sbinfo->si_fhsm;
13870 +need_data:
13871 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13872 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13873 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13874 +                       err = -EAGAIN;
13875 +               else
13876 +                       err = wait_event_interruptible_locked_irq
13877 +                               (fhsm->fhsm_wqh,
13878 +                                atomic_read(&fhsm->fhsm_readable));
13879 +       }
13880 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13881 +       if (unlikely(err))
13882 +               goto out;
13883 +
13884 +       /* sb may already be dead */
13885 +       au_rw_read_lock(&sbinfo->si_rwsem);
13886 +       readable = atomic_read(&fhsm->fhsm_readable);
13887 +       if (readable > 0) {
13888 +               sb = sbinfo->si_sb;
13889 +               AuDebugOn(!sb);
13890 +               /* exclude the bottom branch */
13891 +               nfhsm = 0;
13892 +               bbot = au_fhsm_bottom(sb);
13893 +               for (bindex = 0; bindex < bbot; bindex++) {
13894 +                       br = au_sbr(sb, bindex);
13895 +                       if (au_br_fhsm(br->br_perm))
13896 +                               nfhsm++;
13897 +               }
13898 +               err = -EMSGSIZE;
13899 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13900 +                       atomic_set(&fhsm->fhsm_readable, 0);
13901 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13902 +                                            count);
13903 +               }
13904 +       }
13905 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13906 +       if (!readable)
13907 +               goto need_data;
13908 +
13909 +out:
13910 +       return err;
13911 +}
13912 +
13913 +static int au_fhsm_release(struct inode *inode, struct file *file)
13914 +{
13915 +       struct au_sbinfo *sbinfo;
13916 +       struct au_fhsm *fhsm;
13917 +
13918 +       /* sb may already be dead */
13919 +       sbinfo = file->private_data;
13920 +       fhsm = &sbinfo->si_fhsm;
13921 +       spin_lock(&fhsm->fhsm_spin);
13922 +       fhsm->fhsm_pid = 0;
13923 +       spin_unlock(&fhsm->fhsm_spin);
13924 +       kobject_put(&sbinfo->si_kobj);
13925 +
13926 +       return 0;
13927 +}
13928 +
13929 +static const struct file_operations au_fhsm_fops = {
13930 +       .owner          = THIS_MODULE,
13931 +       .llseek         = noop_llseek,
13932 +       .read           = au_fhsm_read,
13933 +       .poll           = au_fhsm_poll,
13934 +       .release        = au_fhsm_release
13935 +};
13936 +
13937 +int au_fhsm_fd(struct super_block *sb, int oflags)
13938 +{
13939 +       int err, fd;
13940 +       struct au_sbinfo *sbinfo;
13941 +       struct au_fhsm *fhsm;
13942 +
13943 +       err = -EPERM;
13944 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13945 +               goto out;
13946 +
13947 +       err = -EINVAL;
13948 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13949 +               goto out;
13950 +
13951 +       err = 0;
13952 +       sbinfo = au_sbi(sb);
13953 +       fhsm = &sbinfo->si_fhsm;
13954 +       spin_lock(&fhsm->fhsm_spin);
13955 +       if (!fhsm->fhsm_pid)
13956 +               fhsm->fhsm_pid = current->pid;
13957 +       else
13958 +               err = -EBUSY;
13959 +       spin_unlock(&fhsm->fhsm_spin);
13960 +       if (unlikely(err))
13961 +               goto out;
13962 +
13963 +       oflags |= O_RDONLY;
13964 +       /* oflags |= FMODE_NONOTIFY; */
13965 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13966 +       err = fd;
13967 +       if (unlikely(fd < 0))
13968 +               goto out_pid;
13969 +
13970 +       /* succeed regardless 'fhsm' status */
13971 +       kobject_get(&sbinfo->si_kobj);
13972 +       si_noflush_read_lock(sb);
13973 +       if (au_ftest_si(sbinfo, FHSM))
13974 +               au_fhsm_wrote_all(sb, /*force*/0);
13975 +       si_read_unlock(sb);
13976 +       goto out; /* success */
13977 +
13978 +out_pid:
13979 +       spin_lock(&fhsm->fhsm_spin);
13980 +       fhsm->fhsm_pid = 0;
13981 +       spin_unlock(&fhsm->fhsm_spin);
13982 +out:
13983 +       AuTraceErr(err);
13984 +       return err;
13985 +}
13986 +
13987 +/* ---------------------------------------------------------------------- */
13988 +
13989 +int au_fhsm_br_alloc(struct au_branch *br)
13990 +{
13991 +       int err;
13992 +
13993 +       err = 0;
13994 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
13995 +       if (br->br_fhsm)
13996 +               au_br_fhsm_init(br->br_fhsm);
13997 +       else
13998 +               err = -ENOMEM;
13999 +
14000 +       return err;
14001 +}
14002 +
14003 +/* ---------------------------------------------------------------------- */
14004 +
14005 +void au_fhsm_fin(struct super_block *sb)
14006 +{
14007 +       au_fhsm_notify(sb, /*val*/-1);
14008 +}
14009 +
14010 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14011 +{
14012 +       struct au_fhsm *fhsm;
14013 +
14014 +       fhsm = &sbinfo->si_fhsm;
14015 +       spin_lock_init(&fhsm->fhsm_spin);
14016 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14017 +       atomic_set(&fhsm->fhsm_readable, 0);
14018 +       fhsm->fhsm_expire
14019 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14020 +       fhsm->fhsm_bottom = -1;
14021 +}
14022 +
14023 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14024 +{
14025 +       sbinfo->si_fhsm.fhsm_expire
14026 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14027 +}
14028 +
14029 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14030 +{
14031 +       unsigned int u;
14032 +
14033 +       if (!au_ftest_si(sbinfo, FHSM))
14034 +               return;
14035 +
14036 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14037 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14038 +               seq_printf(seq, ",fhsm_sec=%u", u);
14039 +}
14040 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14041 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14042 +++ linux/fs/aufs/file.c        2022-12-17 09:21:34.799855195 +0100
14043 @@ -0,0 +1,860 @@
14044 +// SPDX-License-Identifier: GPL-2.0
14045 +/*
14046 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14047 + *
14048 + * This program is free software; you can redistribute it and/or modify
14049 + * it under the terms of the GNU General Public License as published by
14050 + * the Free Software Foundation; either version 2 of the License, or
14051 + * (at your option) any later version.
14052 + *
14053 + * This program is distributed in the hope that it will be useful,
14054 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14055 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14056 + * GNU General Public License for more details.
14057 + *
14058 + * You should have received a copy of the GNU General Public License
14059 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14060 + */
14061 +
14062 +/*
14063 + * handling file/dir, and address_space operation
14064 + */
14065 +
14066 +#ifdef CONFIG_AUFS_DEBUG
14067 +#include <linux/migrate.h>
14068 +#endif
14069 +#include <linux/pagemap.h>
14070 +#include "aufs.h"
14071 +
14072 +/* drop flags for writing */
14073 +unsigned int au_file_roflags(unsigned int flags)
14074 +{
14075 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14076 +       flags |= O_RDONLY | O_NOATIME;
14077 +       return flags;
14078 +}
14079 +
14080 +/* common functions to regular file and dir */
14081 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14082 +                      struct file *file, int force_wr)
14083 +{
14084 +       struct file *h_file;
14085 +       struct dentry *h_dentry;
14086 +       struct inode *h_inode;
14087 +       struct super_block *sb;
14088 +       struct au_branch *br;
14089 +       struct path h_path;
14090 +       int err;
14091 +
14092 +       /* a race condition can happen between open and unlink/rmdir */
14093 +       h_file = ERR_PTR(-ENOENT);
14094 +       h_dentry = au_h_dptr(dentry, bindex);
14095 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14096 +               goto out;
14097 +       h_inode = d_inode(h_dentry);
14098 +       spin_lock(&h_dentry->d_lock);
14099 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14100 +               /* || !d_inode(dentry)->i_nlink */
14101 +               ;
14102 +       spin_unlock(&h_dentry->d_lock);
14103 +       if (unlikely(err))
14104 +               goto out;
14105 +
14106 +       sb = dentry->d_sb;
14107 +       br = au_sbr(sb, bindex);
14108 +       err = au_br_test_oflag(flags, br);
14109 +       h_file = ERR_PTR(err);
14110 +       if (unlikely(err))
14111 +               goto out;
14112 +
14113 +       /* drop flags for writing */
14114 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14115 +               if (force_wr && !(flags & O_WRONLY))
14116 +                       force_wr = 0;
14117 +               flags = au_file_roflags(flags);
14118 +               if (force_wr) {
14119 +                       h_file = ERR_PTR(-EROFS);
14120 +                       flags = au_file_roflags(flags);
14121 +                       if (unlikely(vfsub_native_ro(h_inode)
14122 +                                    || IS_APPEND(h_inode)))
14123 +                               goto out;
14124 +                       flags &= ~O_ACCMODE;
14125 +                       flags |= O_WRONLY;
14126 +               }
14127 +       }
14128 +       flags &= ~O_CREAT;
14129 +       au_lcnt_inc(&br->br_nfiles);
14130 +       h_path.dentry = h_dentry;
14131 +       h_path.mnt = au_br_mnt(br);
14132 +       h_file = vfsub_dentry_open(&h_path, flags);
14133 +       if (IS_ERR(h_file))
14134 +               goto out_br;
14135 +
14136 +       if (flags & __FMODE_EXEC) {
14137 +               err = deny_write_access(h_file);
14138 +               if (unlikely(err)) {
14139 +                       fput(h_file);
14140 +                       h_file = ERR_PTR(err);
14141 +                       goto out_br;
14142 +               }
14143 +       }
14144 +       fsnotify_open(h_file);
14145 +       goto out; /* success */
14146 +
14147 +out_br:
14148 +       au_lcnt_dec(&br->br_nfiles);
14149 +out:
14150 +       return h_file;
14151 +}
14152 +
14153 +static int au_cmoo(struct dentry *dentry)
14154 +{
14155 +       int err, cmoo, matched;
14156 +       unsigned int udba;
14157 +       struct path h_path;
14158 +       struct au_pin pin;
14159 +       struct au_cp_generic cpg = {
14160 +               .dentry = dentry,
14161 +               .bdst   = -1,
14162 +               .bsrc   = -1,
14163 +               .len    = -1,
14164 +               .pin    = &pin,
14165 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14166 +       };
14167 +       struct inode *delegated;
14168 +       struct super_block *sb;
14169 +       struct au_sbinfo *sbinfo;
14170 +       struct au_fhsm *fhsm;
14171 +       pid_t pid;
14172 +       struct au_branch *br;
14173 +       struct dentry *parent;
14174 +       struct au_hinode *hdir;
14175 +
14176 +       DiMustWriteLock(dentry);
14177 +       IiMustWriteLock(d_inode(dentry));
14178 +
14179 +       err = 0;
14180 +       if (IS_ROOT(dentry))
14181 +               goto out;
14182 +       cpg.bsrc = au_dbtop(dentry);
14183 +       if (!cpg.bsrc)
14184 +               goto out;
14185 +
14186 +       sb = dentry->d_sb;
14187 +       sbinfo = au_sbi(sb);
14188 +       fhsm = &sbinfo->si_fhsm;
14189 +       pid = au_fhsm_pid(fhsm);
14190 +       rcu_read_lock();
14191 +       matched = (pid
14192 +                  && (current->pid == pid
14193 +                      || rcu_dereference(current->real_parent)->pid == pid));
14194 +       rcu_read_unlock();
14195 +       if (matched)
14196 +               goto out;
14197 +
14198 +       br = au_sbr(sb, cpg.bsrc);
14199 +       cmoo = au_br_cmoo(br->br_perm);
14200 +       if (!cmoo)
14201 +               goto out;
14202 +       if (!d_is_reg(dentry))
14203 +               cmoo &= AuBrAttr_COO_ALL;
14204 +       if (!cmoo)
14205 +               goto out;
14206 +
14207 +       parent = dget_parent(dentry);
14208 +       di_write_lock_parent(parent);
14209 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14210 +       cpg.bdst = err;
14211 +       if (unlikely(err < 0)) {
14212 +               err = 0;        /* there is no upper writable branch */
14213 +               goto out_dgrade;
14214 +       }
14215 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14216 +
14217 +       /* do not respect the coo attrib for the target branch */
14218 +       err = au_cpup_dirs(dentry, cpg.bdst);
14219 +       if (unlikely(err))
14220 +               goto out_dgrade;
14221 +
14222 +       di_downgrade_lock(parent, AuLock_IR);
14223 +       udba = au_opt_udba(sb);
14224 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14225 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14226 +       if (unlikely(err))
14227 +               goto out_parent;
14228 +
14229 +       err = au_sio_cpup_simple(&cpg);
14230 +       au_unpin(&pin);
14231 +       if (unlikely(err))
14232 +               goto out_parent;
14233 +       if (!(cmoo & AuBrWAttr_MOO))
14234 +               goto out_parent; /* success */
14235 +
14236 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14237 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14238 +       if (unlikely(err))
14239 +               goto out_parent;
14240 +
14241 +       h_path.mnt = au_br_mnt(br);
14242 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14243 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14244 +       delegated = NULL;
14245 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14246 +       au_unpin(&pin);
14247 +       /* todo: keep h_dentry or not? */
14248 +       if (unlikely(err == -EWOULDBLOCK)) {
14249 +               pr_warn("cannot retry for NFSv4 delegation"
14250 +                       " for an internal unlink\n");
14251 +               iput(delegated);
14252 +       }
14253 +       if (unlikely(err)) {
14254 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14255 +                      dentry, err);
14256 +               err = 0;
14257 +       }
14258 +       goto out_parent; /* success */
14259 +
14260 +out_dgrade:
14261 +       di_downgrade_lock(parent, AuLock_IR);
14262 +out_parent:
14263 +       di_read_unlock(parent, AuLock_IR);
14264 +       dput(parent);
14265 +out:
14266 +       AuTraceErr(err);
14267 +       return err;
14268 +}
14269 +
14270 +int au_do_open(struct file *file, struct au_do_open_args *args)
14271 +{
14272 +       int err, aopen = args->aopen;
14273 +       struct dentry *dentry;
14274 +       struct au_finfo *finfo;
14275 +
14276 +       if (!aopen)
14277 +               err = au_finfo_init(file, args->fidir);
14278 +       else {
14279 +               lockdep_off();
14280 +               err = au_finfo_init(file, args->fidir);
14281 +               lockdep_on();
14282 +       }
14283 +       if (unlikely(err))
14284 +               goto out;
14285 +
14286 +       dentry = file->f_path.dentry;
14287 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14288 +       di_write_lock_child(dentry);
14289 +       err = au_cmoo(dentry);
14290 +       if (!err) {
14291 +               if (!aopen) {
14292 +                       err = args->open(file, vfsub_file_flags(file),
14293 +                                        au_di(dentry)->di_htmpfile);
14294 +                       di_write_unlock(dentry);
14295 +               } else {
14296 +                       di_downgrade_lock(dentry, AuLock_IR);
14297 +                       lockdep_off();
14298 +                       err = args->open(file, vfsub_file_flags(file),
14299 +                                        args->h_file);
14300 +                       lockdep_on();
14301 +                       di_read_unlock(dentry, AuLock_IR);
14302 +               }
14303 +       }
14304 +
14305 +       finfo = au_fi(file);
14306 +       if (!err) {
14307 +               finfo->fi_file = file;
14308 +               au_hbl_add(&finfo->fi_hlist,
14309 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14310 +       }
14311 +       if (!aopen)
14312 +               fi_write_unlock(file);
14313 +       else {
14314 +               lockdep_off();
14315 +               fi_write_unlock(file);
14316 +               lockdep_on();
14317 +       }
14318 +       if (unlikely(err)) {
14319 +               finfo->fi_hdir = NULL;
14320 +               au_finfo_fin(file);
14321 +       }
14322 +
14323 +out:
14324 +       AuTraceErr(err);
14325 +       return err;
14326 +}
14327 +
14328 +int au_reopen_nondir(struct file *file)
14329 +{
14330 +       int err;
14331 +       aufs_bindex_t btop;
14332 +       struct dentry *dentry;
14333 +       struct au_branch *br;
14334 +       struct file *h_file, *h_file_tmp;
14335 +
14336 +       dentry = file->f_path.dentry;
14337 +       btop = au_dbtop(dentry);
14338 +       br = au_sbr(dentry->d_sb, btop);
14339 +       h_file_tmp = NULL;
14340 +       if (au_fbtop(file) == btop) {
14341 +               h_file = au_hf_top(file);
14342 +               if (file->f_mode == h_file->f_mode)
14343 +                       return 0; /* success */
14344 +               h_file_tmp = h_file;
14345 +               get_file(h_file_tmp);
14346 +               au_lcnt_inc(&br->br_nfiles);
14347 +               au_set_h_fptr(file, btop, NULL);
14348 +       }
14349 +       AuDebugOn(au_fi(file)->fi_hdir);
14350 +       /*
14351 +        * it can happen
14352 +        * file exists on both of rw and ro
14353 +        * open --> dbtop and fbtop are both 0
14354 +        * prepend a branch as rw, "rw" become ro
14355 +        * remove rw/file
14356 +        * delete the top branch, "rw" becomes rw again
14357 +        *      --> dbtop is 1, fbtop is still 0
14358 +        * write --> fbtop is 0 but dbtop is 1
14359 +        */
14360 +       /* AuDebugOn(au_fbtop(file) < btop); */
14361 +
14362 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14363 +                          file, /*force_wr*/0);
14364 +       err = PTR_ERR(h_file);
14365 +       if (IS_ERR(h_file)) {
14366 +               if (h_file_tmp) {
14367 +                       /* revert */
14368 +                       au_set_h_fptr(file, btop, h_file_tmp);
14369 +                       h_file_tmp = NULL;
14370 +               }
14371 +               goto out; /* todo: close all? */
14372 +       }
14373 +
14374 +       err = 0;
14375 +       au_set_fbtop(file, btop);
14376 +       au_set_h_fptr(file, btop, h_file);
14377 +       au_update_figen(file);
14378 +       /* todo: necessary? */
14379 +       /* file->f_ra = h_file->f_ra; */
14380 +
14381 +out:
14382 +       if (h_file_tmp) {
14383 +               fput(h_file_tmp);
14384 +               au_lcnt_dec(&br->br_nfiles);
14385 +       }
14386 +       return err;
14387 +}
14388 +
14389 +/* ---------------------------------------------------------------------- */
14390 +
14391 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14392 +                       struct dentry *hi_wh)
14393 +{
14394 +       int err;
14395 +       aufs_bindex_t btop;
14396 +       struct au_dinfo *dinfo;
14397 +       struct dentry *h_dentry;
14398 +       struct au_hdentry *hdp;
14399 +
14400 +       dinfo = au_di(file->f_path.dentry);
14401 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14402 +
14403 +       btop = dinfo->di_btop;
14404 +       dinfo->di_btop = btgt;
14405 +       hdp = au_hdentry(dinfo, btgt);
14406 +       h_dentry = hdp->hd_dentry;
14407 +       hdp->hd_dentry = hi_wh;
14408 +       err = au_reopen_nondir(file);
14409 +       hdp->hd_dentry = h_dentry;
14410 +       dinfo->di_btop = btop;
14411 +
14412 +       return err;
14413 +}
14414 +
14415 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14416 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14417 +{
14418 +       int err;
14419 +       struct inode *inode, *h_inode;
14420 +       struct dentry *h_dentry, *hi_wh;
14421 +       struct au_cp_generic cpg = {
14422 +               .dentry = file->f_path.dentry,
14423 +               .bdst   = bcpup,
14424 +               .bsrc   = -1,
14425 +               .len    = len,
14426 +               .pin    = pin
14427 +       };
14428 +
14429 +       au_update_dbtop(cpg.dentry);
14430 +       inode = d_inode(cpg.dentry);
14431 +       h_inode = NULL;
14432 +       if (au_dbtop(cpg.dentry) <= bcpup
14433 +           && au_dbbot(cpg.dentry) >= bcpup) {
14434 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14435 +               if (h_dentry && d_is_positive(h_dentry))
14436 +                       h_inode = d_inode(h_dentry);
14437 +       }
14438 +       hi_wh = au_hi_wh(inode, bcpup);
14439 +       if (!hi_wh && !h_inode)
14440 +               err = au_sio_cpup_wh(&cpg, file);
14441 +       else
14442 +               /* already copied-up after unlink */
14443 +               err = au_reopen_wh(file, bcpup, hi_wh);
14444 +
14445 +       if (!err
14446 +           && (inode->i_nlink > 1
14447 +               || (inode->i_state & I_LINKABLE))
14448 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14449 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14450 +
14451 +       return err;
14452 +}
14453 +
14454 +/*
14455 + * prepare the @file for writing.
14456 + */
14457 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14458 +{
14459 +       int err;
14460 +       aufs_bindex_t dbtop;
14461 +       struct dentry *parent;
14462 +       struct inode *inode;
14463 +       struct super_block *sb;
14464 +       struct file *h_file;
14465 +       struct au_cp_generic cpg = {
14466 +               .dentry = file->f_path.dentry,
14467 +               .bdst   = -1,
14468 +               .bsrc   = -1,
14469 +               .len    = len,
14470 +               .pin    = pin,
14471 +               .flags  = AuCpup_DTIME
14472 +       };
14473 +
14474 +       sb = cpg.dentry->d_sb;
14475 +       inode = d_inode(cpg.dentry);
14476 +       cpg.bsrc = au_fbtop(file);
14477 +       err = au_test_ro(sb, cpg.bsrc, inode);
14478 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14479 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14480 +                            /*flags*/0);
14481 +               goto out;
14482 +       }
14483 +
14484 +       /* need to cpup or reopen */
14485 +       parent = dget_parent(cpg.dentry);
14486 +       di_write_lock_parent(parent);
14487 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14488 +       cpg.bdst = err;
14489 +       if (unlikely(err < 0))
14490 +               goto out_dgrade;
14491 +       err = 0;
14492 +
14493 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14494 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14495 +               if (unlikely(err))
14496 +                       goto out_dgrade;
14497 +       }
14498 +
14499 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14500 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14501 +       if (unlikely(err))
14502 +               goto out_dgrade;
14503 +
14504 +       dbtop = au_dbtop(cpg.dentry);
14505 +       if (dbtop <= cpg.bdst)
14506 +               cpg.bsrc = cpg.bdst;
14507 +
14508 +       if (dbtop <= cpg.bdst           /* just reopen */
14509 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14510 +               ) {
14511 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14512 +               if (IS_ERR(h_file))
14513 +                       err = PTR_ERR(h_file);
14514 +               else {
14515 +                       di_downgrade_lock(parent, AuLock_IR);
14516 +                       if (dbtop > cpg.bdst)
14517 +                               err = au_sio_cpup_simple(&cpg);
14518 +                       if (!err)
14519 +                               err = au_reopen_nondir(file);
14520 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14521 +               }
14522 +       } else {                        /* copyup as wh and reopen */
14523 +               /*
14524 +                * since writable hfsplus branch is not supported,
14525 +                * h_open_pre/post() are unnecessary.
14526 +                */
14527 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14528 +               di_downgrade_lock(parent, AuLock_IR);
14529 +       }
14530 +
14531 +       if (!err) {
14532 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14533 +               goto out_dput; /* success */
14534 +       }
14535 +       au_unpin(pin);
14536 +       goto out_unlock;
14537 +
14538 +out_dgrade:
14539 +       di_downgrade_lock(parent, AuLock_IR);
14540 +out_unlock:
14541 +       di_read_unlock(parent, AuLock_IR);
14542 +out_dput:
14543 +       dput(parent);
14544 +out:
14545 +       return err;
14546 +}
14547 +
14548 +/* ---------------------------------------------------------------------- */
14549 +
14550 +int au_do_flush(struct file *file, fl_owner_t id,
14551 +               int (*flush)(struct file *file, fl_owner_t id))
14552 +{
14553 +       int err;
14554 +       struct super_block *sb;
14555 +       struct inode *inode;
14556 +
14557 +       inode = file_inode(file);
14558 +       sb = inode->i_sb;
14559 +       si_noflush_read_lock(sb);
14560 +       fi_read_lock(file);
14561 +       ii_read_lock_child(inode);
14562 +
14563 +       err = flush(file, id);
14564 +       au_cpup_attr_timesizes(inode);
14565 +
14566 +       ii_read_unlock(inode);
14567 +       fi_read_unlock(file);
14568 +       si_read_unlock(sb);
14569 +       return err;
14570 +}
14571 +
14572 +/* ---------------------------------------------------------------------- */
14573 +
14574 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14575 +{
14576 +       int err;
14577 +       struct au_pin pin;
14578 +       struct au_finfo *finfo;
14579 +       struct dentry *parent, *hi_wh;
14580 +       struct inode *inode;
14581 +       struct super_block *sb;
14582 +       struct au_cp_generic cpg = {
14583 +               .dentry = file->f_path.dentry,
14584 +               .bdst   = -1,
14585 +               .bsrc   = -1,
14586 +               .len    = -1,
14587 +               .pin    = &pin,
14588 +               .flags  = AuCpup_DTIME
14589 +       };
14590 +
14591 +       FiMustWriteLock(file);
14592 +
14593 +       err = 0;
14594 +       finfo = au_fi(file);
14595 +       sb = cpg.dentry->d_sb;
14596 +       inode = d_inode(cpg.dentry);
14597 +       cpg.bdst = au_ibtop(inode);
14598 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14599 +               goto out;
14600 +
14601 +       parent = dget_parent(cpg.dentry);
14602 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14603 +               di_read_lock_parent(parent, !AuLock_IR);
14604 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14605 +               cpg.bdst = err;
14606 +               di_read_unlock(parent, !AuLock_IR);
14607 +               if (unlikely(err < 0))
14608 +                       goto out_parent;
14609 +               err = 0;
14610 +       }
14611 +
14612 +       di_read_lock_parent(parent, AuLock_IR);
14613 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14614 +       if (!S_ISDIR(inode->i_mode)
14615 +           && au_opt_test(au_mntflags(sb), PLINK)
14616 +           && au_plink_test(inode)
14617 +           && !d_unhashed(cpg.dentry)
14618 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14619 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14620 +               if (unlikely(err))
14621 +                       goto out_unlock;
14622 +
14623 +               /* always superio. */
14624 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14625 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14626 +               if (!err) {
14627 +                       err = au_sio_cpup_simple(&cpg);
14628 +                       au_unpin(&pin);
14629 +               }
14630 +       } else if (hi_wh) {
14631 +               /* already copied-up after unlink */
14632 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14633 +               *need_reopen = 0;
14634 +       }
14635 +
14636 +out_unlock:
14637 +       di_read_unlock(parent, AuLock_IR);
14638 +out_parent:
14639 +       dput(parent);
14640 +out:
14641 +       return err;
14642 +}
14643 +
14644 +static void au_do_refresh_dir(struct file *file)
14645 +{
14646 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14647 +       struct au_hfile *p, tmp, *q;
14648 +       struct au_finfo *finfo;
14649 +       struct super_block *sb;
14650 +       struct au_fidir *fidir;
14651 +
14652 +       FiMustWriteLock(file);
14653 +
14654 +       sb = file->f_path.dentry->d_sb;
14655 +       finfo = au_fi(file);
14656 +       fidir = finfo->fi_hdir;
14657 +       AuDebugOn(!fidir);
14658 +       p = fidir->fd_hfile + finfo->fi_btop;
14659 +       brid = p->hf_br->br_id;
14660 +       bbot = fidir->fd_bbot;
14661 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14662 +               if (!p->hf_file)
14663 +                       continue;
14664 +
14665 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14666 +               if (new_bindex == bindex)
14667 +                       continue;
14668 +               if (new_bindex < 0) {
14669 +                       au_set_h_fptr(file, bindex, NULL);
14670 +                       continue;
14671 +               }
14672 +
14673 +               /* swap two lower inode, and loop again */
14674 +               q = fidir->fd_hfile + new_bindex;
14675 +               tmp = *q;
14676 +               *q = *p;
14677 +               *p = tmp;
14678 +               if (tmp.hf_file) {
14679 +                       bindex--;
14680 +                       p--;
14681 +               }
14682 +       }
14683 +
14684 +       p = fidir->fd_hfile;
14685 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14686 +               bbot = au_sbbot(sb);
14687 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14688 +                    finfo->fi_btop++, p++)
14689 +                       if (p->hf_file) {
14690 +                               if (file_inode(p->hf_file))
14691 +                                       break;
14692 +                               au_hfput(p, /*execed*/0);
14693 +                       }
14694 +       } else {
14695 +               bbot = au_br_index(sb, brid);
14696 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14697 +                    finfo->fi_btop++, p++)
14698 +                       if (p->hf_file)
14699 +                               au_hfput(p, /*execed*/0);
14700 +               bbot = au_sbbot(sb);
14701 +       }
14702 +
14703 +       p = fidir->fd_hfile + bbot;
14704 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14705 +            fidir->fd_bbot--, p--)
14706 +               if (p->hf_file) {
14707 +                       if (file_inode(p->hf_file))
14708 +                               break;
14709 +                       au_hfput(p, /*execed*/0);
14710 +               }
14711 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14712 +}
14713 +
14714 +/*
14715 + * after branch manipulating, refresh the file.
14716 + */
14717 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14718 +{
14719 +       int err, need_reopen, nbr;
14720 +       aufs_bindex_t bbot, bindex;
14721 +       struct dentry *dentry;
14722 +       struct super_block *sb;
14723 +       struct au_finfo *finfo;
14724 +       struct au_hfile *hfile;
14725 +
14726 +       dentry = file->f_path.dentry;
14727 +       sb = dentry->d_sb;
14728 +       nbr = au_sbbot(sb) + 1;
14729 +       finfo = au_fi(file);
14730 +       if (!finfo->fi_hdir) {
14731 +               hfile = &finfo->fi_htop;
14732 +               AuDebugOn(!hfile->hf_file);
14733 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14734 +               AuDebugOn(bindex < 0);
14735 +               if (bindex != finfo->fi_btop)
14736 +                       au_set_fbtop(file, bindex);
14737 +       } else {
14738 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14739 +               if (unlikely(err))
14740 +                       goto out;
14741 +               au_do_refresh_dir(file);
14742 +       }
14743 +
14744 +       err = 0;
14745 +       need_reopen = 1;
14746 +       if (!au_test_mmapped(file))
14747 +               err = au_file_refresh_by_inode(file, &need_reopen);
14748 +       if (finfo->fi_hdir)
14749 +               /* harmless if err */
14750 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14751 +       if (!err && need_reopen && !d_unlinked(dentry))
14752 +               err = reopen(file);
14753 +       if (!err) {
14754 +               au_update_figen(file);
14755 +               goto out; /* success */
14756 +       }
14757 +
14758 +       /* error, close all lower files */
14759 +       if (finfo->fi_hdir) {
14760 +               bbot = au_fbbot_dir(file);
14761 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14762 +                       au_set_h_fptr(file, bindex, NULL);
14763 +       }
14764 +
14765 +out:
14766 +       return err;
14767 +}
14768 +
14769 +/* common function to regular file and dir */
14770 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14771 +                         int wlock, unsigned int fi_lsc)
14772 +{
14773 +       int err;
14774 +       unsigned int sigen, figen;
14775 +       aufs_bindex_t btop;
14776 +       unsigned char pseudo_link;
14777 +       struct dentry *dentry;
14778 +       struct inode *inode;
14779 +
14780 +       err = 0;
14781 +       dentry = file->f_path.dentry;
14782 +       inode = d_inode(dentry);
14783 +       sigen = au_sigen(dentry->d_sb);
14784 +       fi_write_lock_nested(file, fi_lsc);
14785 +       figen = au_figen(file);
14786 +       if (!fi_lsc)
14787 +               di_write_lock_child(dentry);
14788 +       else
14789 +               di_write_lock_child2(dentry);
14790 +       btop = au_dbtop(dentry);
14791 +       pseudo_link = (btop != au_ibtop(inode));
14792 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14793 +               if (!wlock) {
14794 +                       di_downgrade_lock(dentry, AuLock_IR);
14795 +                       fi_downgrade_lock(file);
14796 +               }
14797 +               goto out; /* success */
14798 +       }
14799 +
14800 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14801 +       if (au_digen_test(dentry, sigen)) {
14802 +               err = au_reval_dpath(dentry, sigen);
14803 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14804 +       }
14805 +
14806 +       if (!err)
14807 +               err = refresh_file(file, reopen);
14808 +       if (!err) {
14809 +               if (!wlock) {
14810 +                       di_downgrade_lock(dentry, AuLock_IR);
14811 +                       fi_downgrade_lock(file);
14812 +               }
14813 +       } else {
14814 +               di_write_unlock(dentry);
14815 +               fi_write_unlock(file);
14816 +       }
14817 +
14818 +out:
14819 +       return err;
14820 +}
14821 +
14822 +/* ---------------------------------------------------------------------- */
14823 +
14824 +/* cf. aufs_nopage() */
14825 +/* for madvise(2) */
14826 +static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
14827 +{
14828 +       folio_unlock(folio);
14829 +       return 0;
14830 +}
14831 +
14832 +/* it will never be called, but necessary to support O_DIRECT */
14833 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14834 +{ BUG(); return 0; }
14835 +
14836 +/* they will never be called. */
14837 +#ifdef CONFIG_AUFS_DEBUG
14838 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14839 +                           loff_t pos, unsigned len,
14840 +                           struct page **pagep, void **fsdata)
14841 +{ AuUnsupport(); return 0; }
14842 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14843 +                         loff_t pos, unsigned len, unsigned copied,
14844 +                         struct page *page, void *fsdata)
14845 +{ AuUnsupport(); return 0; }
14846 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14847 +{ AuUnsupport(); return 0; }
14848 +
14849 +static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
14850 +{ AuUnsupport(); return true; }
14851 +static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
14852 +{ AuUnsupport(); }
14853 +static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
14854 +{ AuUnsupport(); return true; }
14855 +#if 0 /* called by memory compaction regardless file */
14856 +static int aufs_migrate_folio(struct address_space *mapping, struct folio *dst,
14857 +                             struct folio *src, enum migrate_mode mode)
14858 +{ AuUnsupport(); return 0; }
14859 +#endif
14860 +static int aufs_launder_folio(struct folio *folio)
14861 +{ AuUnsupport(); return 0; }
14862 +static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
14863 +                                     size_t count)
14864 +{ AuUnsupport(); return true; }
14865 +static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
14866 +                                   bool *writeback)
14867 +{ AuUnsupport(); }
14868 +static int aufs_error_remove_page(struct address_space *mapping,
14869 +                                 struct page *page)
14870 +{ AuUnsupport(); return 0; }
14871 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14872 +                             sector_t *span)
14873 +{ AuUnsupport(); return 0; }
14874 +static void aufs_swap_deactivate(struct file *file)
14875 +{ AuUnsupport(); }
14876 +static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
14877 +{ AuUnsupport(); return 0; }
14878 +#endif /* CONFIG_AUFS_DEBUG */
14879 +
14880 +const struct address_space_operations aufs_aop = {
14881 +       .read_folio             = aufs_read_folio,
14882 +       .direct_IO              = aufs_direct_IO,
14883 +#ifdef CONFIG_AUFS_DEBUG
14884 +       .writepage              = aufs_writepage,
14885 +       /* no writepages, because of writepage */
14886 +       .dirty_folio            = aufs_dirty_folio,
14887 +       /* no readpages, because of readpage */
14888 +       .write_begin            = aufs_write_begin,
14889 +       .write_end              = aufs_write_end,
14890 +       /* no bmap, no block device */
14891 +       .invalidate_folio       = aufs_invalidate_folio,
14892 +       .release_folio          = aufs_release_folio,
14893 +       /* is fallback_migrate_page ok? */
14894 +       /* .migrate_folio       = aufs_migrate_folio, */
14895 +       .launder_folio          = aufs_launder_folio,
14896 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14897 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14898 +       .error_remove_page      = aufs_error_remove_page,
14899 +       .swap_activate          = aufs_swap_activate,
14900 +       .swap_deactivate        = aufs_swap_deactivate,
14901 +       .swap_rw                = aufs_swap_rw
14902 +#endif /* CONFIG_AUFS_DEBUG */
14903 +};
14904 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14905 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14906 +++ linux/fs/aufs/file.h        2022-11-05 23:02:18.965889284 +0100
14907 @@ -0,0 +1,342 @@
14908 +/* SPDX-License-Identifier: GPL-2.0 */
14909 +/*
14910 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14911 + *
14912 + * This program is free software; you can redistribute it and/or modify
14913 + * it under the terms of the GNU General Public License as published by
14914 + * the Free Software Foundation; either version 2 of the License, or
14915 + * (at your option) any later version.
14916 + *
14917 + * This program is distributed in the hope that it will be useful,
14918 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14919 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14920 + * GNU General Public License for more details.
14921 + *
14922 + * You should have received a copy of the GNU General Public License
14923 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14924 + */
14925 +
14926 +/*
14927 + * file operations
14928 + */
14929 +
14930 +#ifndef __AUFS_FILE_H__
14931 +#define __AUFS_FILE_H__
14932 +
14933 +#ifdef __KERNEL__
14934 +
14935 +#include <linux/file.h>
14936 +#include <linux/fs.h>
14937 +#include <linux/mm_types.h>
14938 +#include <linux/poll.h>
14939 +#include "rwsem.h"
14940 +
14941 +struct au_branch;
14942 +struct au_hfile {
14943 +       struct file             *hf_file;
14944 +       struct au_branch        *hf_br;
14945 +};
14946 +
14947 +struct au_vdir;
14948 +struct au_fidir {
14949 +       aufs_bindex_t           fd_bbot;
14950 +       aufs_bindex_t           fd_nent;
14951 +       struct au_vdir          *fd_vdir_cache;
14952 +       struct au_hfile         fd_hfile[];
14953 +};
14954 +
14955 +static inline int au_fidir_sz(int nent)
14956 +{
14957 +       AuDebugOn(nent < 0);
14958 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14959 +}
14960 +
14961 +struct au_finfo {
14962 +       atomic_t                fi_generation;
14963 +
14964 +       struct au_rwsem         fi_rwsem;
14965 +       aufs_bindex_t           fi_btop;
14966 +
14967 +       /* do not union them */
14968 +       struct {                                /* for non-dir */
14969 +               struct au_hfile                 fi_htop;
14970 +               atomic_t                        fi_mmapped;
14971 +       };
14972 +       struct au_fidir         *fi_hdir;       /* for dir only */
14973 +
14974 +       struct hlist_bl_node    fi_hlist;
14975 +       struct file             *fi_file;       /* very ugly */
14976 +       struct rcu_head         rcu;
14977 +} ____cacheline_aligned_in_smp;
14978 +
14979 +/* ---------------------------------------------------------------------- */
14980 +
14981 +/* file.c */
14982 +extern const struct address_space_operations aufs_aop;
14983 +unsigned int au_file_roflags(unsigned int flags);
14984 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14985 +                      struct file *file, int force_wr);
14986 +struct au_do_open_args {
14987 +       int             aopen;
14988 +       int             (*open)(struct file *file, int flags,
14989 +                               struct file *h_file);
14990 +       struct au_fidir *fidir;
14991 +       struct file     *h_file;
14992 +};
14993 +int au_do_open(struct file *file, struct au_do_open_args *args);
14994 +int au_reopen_nondir(struct file *file);
14995 +struct au_pin;
14996 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
14997 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14998 +                         int wlock, unsigned int fi_lsc);
14999 +int au_do_flush(struct file *file, fl_owner_t id,
15000 +               int (*flush)(struct file *file, fl_owner_t id));
15001 +
15002 +/* poll.c */
15003 +#ifdef CONFIG_AUFS_POLL
15004 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15005 +#endif
15006 +
15007 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15008 +/* hfsplus.c */
15009 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15010 +                          int force_wr);
15011 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15012 +                   struct file *h_file);
15013 +#else
15014 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15015 +       aufs_bindex_t bindex, int force_wr)
15016 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15017 +          struct file *h_file);
15018 +#endif
15019 +
15020 +/* f_op.c */
15021 +extern const struct file_operations aufs_file_fop;
15022 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15023 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15024 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15025 +
15026 +/* finfo.c */
15027 +void au_hfput(struct au_hfile *hf, int execed);
15028 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15029 +                  struct file *h_file);
15030 +
15031 +void au_update_figen(struct file *file);
15032 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15033 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15034 +
15035 +void au_fi_init_once(void *_fi);
15036 +void au_finfo_fin(struct file *file);
15037 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15038 +
15039 +/* ioctl.c */
15040 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15041 +#ifdef CONFIG_COMPAT
15042 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15043 +                          unsigned long arg);
15044 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15045 +                             unsigned long arg);
15046 +#endif
15047 +
15048 +/* ---------------------------------------------------------------------- */
15049 +
15050 +static inline struct au_finfo *au_fi(struct file *file)
15051 +{
15052 +       return file->private_data;
15053 +}
15054 +
15055 +/* ---------------------------------------------------------------------- */
15056 +
15057 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15058 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15059 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15060 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15061 +/*
15062 +#define fi_read_trylock_nested(f) \
15063 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15064 +#define fi_write_trylock_nested(f) \
15065 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15066 +*/
15067 +
15068 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15069 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15070 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15071 +
15072 +/* lock subclass for finfo */
15073 +enum {
15074 +       AuLsc_FI_1,
15075 +       AuLsc_FI_2
15076 +};
15077 +
15078 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15079 +{
15080 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15081 +}
15082 +
15083 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15084 +{
15085 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15086 +}
15087 +
15088 +/*
15089 + * fi_read_lock_1, fi_write_lock_1,
15090 + * fi_read_lock_2, fi_write_lock_2
15091 + */
15092 +#define AuReadLockFunc(name) \
15093 +static inline void fi_read_lock_##name(struct file *f) \
15094 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15095 +
15096 +#define AuWriteLockFunc(name) \
15097 +static inline void fi_write_lock_##name(struct file *f) \
15098 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15099 +
15100 +#define AuRWLockFuncs(name) \
15101 +       AuReadLockFunc(name) \
15102 +       AuWriteLockFunc(name)
15103 +
15104 +AuRWLockFuncs(1);
15105 +AuRWLockFuncs(2);
15106 +
15107 +#undef AuReadLockFunc
15108 +#undef AuWriteLockFunc
15109 +#undef AuRWLockFuncs
15110 +
15111 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15112 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15113 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15114 +
15115 +/* ---------------------------------------------------------------------- */
15116 +
15117 +/* todo: hard/soft set? */
15118 +static inline aufs_bindex_t au_fbtop(struct file *file)
15119 +{
15120 +       FiMustAnyLock(file);
15121 +       return au_fi(file)->fi_btop;
15122 +}
15123 +
15124 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15125 +{
15126 +       FiMustAnyLock(file);
15127 +       AuDebugOn(!au_fi(file)->fi_hdir);
15128 +       return au_fi(file)->fi_hdir->fd_bbot;
15129 +}
15130 +
15131 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15132 +{
15133 +       FiMustAnyLock(file);
15134 +       AuDebugOn(!au_fi(file)->fi_hdir);
15135 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15136 +}
15137 +
15138 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15139 +{
15140 +       FiMustWriteLock(file);
15141 +       au_fi(file)->fi_btop = bindex;
15142 +}
15143 +
15144 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15145 +{
15146 +       FiMustWriteLock(file);
15147 +       AuDebugOn(!au_fi(file)->fi_hdir);
15148 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15149 +}
15150 +
15151 +static inline void au_set_fvdir_cache(struct file *file,
15152 +                                     struct au_vdir *vdir_cache)
15153 +{
15154 +       FiMustWriteLock(file);
15155 +       AuDebugOn(!au_fi(file)->fi_hdir);
15156 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15157 +}
15158 +
15159 +static inline struct file *au_hf_top(struct file *file)
15160 +{
15161 +       FiMustAnyLock(file);
15162 +       AuDebugOn(au_fi(file)->fi_hdir);
15163 +       return au_fi(file)->fi_htop.hf_file;
15164 +}
15165 +
15166 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15167 +{
15168 +       FiMustAnyLock(file);
15169 +       AuDebugOn(!au_fi(file)->fi_hdir);
15170 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15171 +}
15172 +
15173 +/* todo: memory barrier? */
15174 +static inline unsigned int au_figen(struct file *f)
15175 +{
15176 +       return atomic_read(&au_fi(f)->fi_generation);
15177 +}
15178 +
15179 +static inline void au_set_mmapped(struct file *f)
15180 +{
15181 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15182 +               return;
15183 +       pr_warn("fi_mmapped wrapped around\n");
15184 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15185 +               ;
15186 +}
15187 +
15188 +static inline void au_unset_mmapped(struct file *f)
15189 +{
15190 +       atomic_dec(&au_fi(f)->fi_mmapped);
15191 +}
15192 +
15193 +static inline int au_test_mmapped(struct file *f)
15194 +{
15195 +       return atomic_read(&au_fi(f)->fi_mmapped);
15196 +}
15197 +
15198 +/* customize vma->vm_file */
15199 +
15200 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15201 +                                      struct file *file)
15202 +{
15203 +       struct file *f;
15204 +
15205 +       f = vma->vm_file;
15206 +       get_file(file);
15207 +       vma->vm_file = file;
15208 +       fput(f);
15209 +}
15210 +
15211 +#ifdef CONFIG_MMU
15212 +#define AuDbgVmRegion(file, vma) do {} while (0)
15213 +
15214 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15215 +                                   struct file *file)
15216 +{
15217 +       au_do_vm_file_reset(vma, file);
15218 +}
15219 +#else
15220 +#define AuDbgVmRegion(file, vma) \
15221 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15222 +
15223 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15224 +                                   struct file *file)
15225 +{
15226 +       struct file *f;
15227 +
15228 +       au_do_vm_file_reset(vma, file);
15229 +       f = vma->vm_region->vm_file;
15230 +       get_file(file);
15231 +       vma->vm_region->vm_file = file;
15232 +       fput(f);
15233 +}
15234 +#endif /* CONFIG_MMU */
15235 +
15236 +/* handle vma->vm_prfile */
15237 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15238 +                                   struct file *file)
15239 +{
15240 +       get_file(file);
15241 +       vma->vm_prfile = file;
15242 +#ifndef CONFIG_MMU
15243 +       get_file(file);
15244 +       vma->vm_region->vm_prfile = file;
15245 +#endif
15246 +}
15247 +
15248 +#endif /* __KERNEL__ */
15249 +#endif /* __AUFS_FILE_H__ */
15250 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15251 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15252 +++ linux/fs/aufs/finfo.c       2022-11-05 23:02:18.965889284 +0100
15253 @@ -0,0 +1,149 @@
15254 +// SPDX-License-Identifier: GPL-2.0
15255 +/*
15256 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15257 + *
15258 + * This program is free software; you can redistribute it and/or modify
15259 + * it under the terms of the GNU General Public License as published by
15260 + * the Free Software Foundation; either version 2 of the License, or
15261 + * (at your option) any later version.
15262 + *
15263 + * This program is distributed in the hope that it will be useful,
15264 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15265 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15266 + * GNU General Public License for more details.
15267 + *
15268 + * You should have received a copy of the GNU General Public License
15269 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15270 + */
15271 +
15272 +/*
15273 + * file private data
15274 + */
15275 +
15276 +#include "aufs.h"
15277 +
15278 +void au_hfput(struct au_hfile *hf, int execed)
15279 +{
15280 +       if (execed)
15281 +               allow_write_access(hf->hf_file);
15282 +       fput(hf->hf_file);
15283 +       hf->hf_file = NULL;
15284 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15285 +       hf->hf_br = NULL;
15286 +}
15287 +
15288 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15289 +{
15290 +       struct au_finfo *finfo = au_fi(file);
15291 +       struct au_hfile *hf;
15292 +       struct au_fidir *fidir;
15293 +
15294 +       fidir = finfo->fi_hdir;
15295 +       if (!fidir) {
15296 +               AuDebugOn(finfo->fi_btop != bindex);
15297 +               hf = &finfo->fi_htop;
15298 +       } else
15299 +               hf = fidir->fd_hfile + bindex;
15300 +
15301 +       if (hf && hf->hf_file)
15302 +               au_hfput(hf, vfsub_file_execed(file));
15303 +       if (val) {
15304 +               FiMustWriteLock(file);
15305 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15306 +               hf->hf_file = val;
15307 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15308 +       }
15309 +}
15310 +
15311 +void au_update_figen(struct file *file)
15312 +{
15313 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15314 +       /* smp_mb(); */ /* atomic_set */
15315 +}
15316 +
15317 +/* ---------------------------------------------------------------------- */
15318 +
15319 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15320 +{
15321 +       struct au_fidir *fidir;
15322 +       int nbr;
15323 +
15324 +       nbr = au_sbbot(sb) + 1;
15325 +       if (nbr < 2)
15326 +               nbr = 2; /* initial allocate for 2 branches */
15327 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15328 +       if (fidir) {
15329 +               fidir->fd_bbot = -1;
15330 +               fidir->fd_nent = nbr;
15331 +       }
15332 +
15333 +       return fidir;
15334 +}
15335 +
15336 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15337 +{
15338 +       int err;
15339 +       struct au_fidir *fidir, *p;
15340 +
15341 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15342 +       fidir = finfo->fi_hdir;
15343 +       AuDebugOn(!fidir);
15344 +
15345 +       err = -ENOMEM;
15346 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15347 +                        GFP_NOFS, may_shrink);
15348 +       if (p) {
15349 +               p->fd_nent = nbr;
15350 +               finfo->fi_hdir = p;
15351 +               err = 0;
15352 +       }
15353 +
15354 +       return err;
15355 +}
15356 +
15357 +/* ---------------------------------------------------------------------- */
15358 +
15359 +void au_finfo_fin(struct file *file)
15360 +{
15361 +       struct au_finfo *finfo;
15362 +
15363 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15364 +
15365 +       finfo = au_fi(file);
15366 +       AuDebugOn(finfo->fi_hdir);
15367 +       AuRwDestroy(&finfo->fi_rwsem);
15368 +       au_cache_free_finfo(finfo);
15369 +}
15370 +
15371 +void au_fi_init_once(void *_finfo)
15372 +{
15373 +       struct au_finfo *finfo = _finfo;
15374 +
15375 +       au_rw_init(&finfo->fi_rwsem);
15376 +}
15377 +
15378 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15379 +{
15380 +       int err;
15381 +       struct au_finfo *finfo;
15382 +       struct dentry *dentry;
15383 +
15384 +       err = -ENOMEM;
15385 +       dentry = file->f_path.dentry;
15386 +       finfo = au_cache_alloc_finfo();
15387 +       if (unlikely(!finfo))
15388 +               goto out;
15389 +
15390 +       err = 0;
15391 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15392 +       au_rw_write_lock(&finfo->fi_rwsem);
15393 +       finfo->fi_btop = -1;
15394 +       finfo->fi_hdir = fidir;
15395 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15396 +       /* smp_mb(); */ /* atomic_set */
15397 +
15398 +       file->private_data = finfo;
15399 +
15400 +out:
15401 +       return err;
15402 +}
15403 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15404 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15405 +++ linux/fs/aufs/f_op.c        2023-02-20 21:05:51.959693785 +0100
15406 @@ -0,0 +1,780 @@
15407 +// SPDX-License-Identifier: GPL-2.0
15408 +/*
15409 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15410 + *
15411 + * This program is free software; you can redistribute it and/or modify
15412 + * it under the terms of the GNU General Public License as published by
15413 + * the Free Software Foundation; either version 2 of the License, or
15414 + * (at your option) any later version.
15415 + *
15416 + * This program is distributed in the hope that it will be useful,
15417 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15418 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15419 + * GNU General Public License for more details.
15420 + *
15421 + * You should have received a copy of the GNU General Public License
15422 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15423 + */
15424 +
15425 +/*
15426 + * file and vm operations
15427 + */
15428 +
15429 +#include <linux/aio.h>
15430 +#include <linux/fs_stack.h>
15431 +#include <linux/mman.h>
15432 +#include <linux/security.h>
15433 +#include "aufs.h"
15434 +
15435 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15436 +{
15437 +       int err;
15438 +       aufs_bindex_t bindex;
15439 +       struct dentry *dentry, *h_dentry;
15440 +       struct au_finfo *finfo;
15441 +       struct inode *h_inode;
15442 +
15443 +       FiMustWriteLock(file);
15444 +
15445 +       err = 0;
15446 +       dentry = file->f_path.dentry;
15447 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15448 +       finfo = au_fi(file);
15449 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15450 +       atomic_set(&finfo->fi_mmapped, 0);
15451 +       bindex = au_dbtop(dentry);
15452 +       if (!h_file) {
15453 +               h_dentry = au_h_dptr(dentry, bindex);
15454 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15455 +               if (unlikely(err))
15456 +                       goto out;
15457 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15458 +               if (IS_ERR(h_file)) {
15459 +                       err = PTR_ERR(h_file);
15460 +                       goto out;
15461 +               }
15462 +       } else {
15463 +               h_dentry = h_file->f_path.dentry;
15464 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15465 +               if (unlikely(err))
15466 +                       goto out;
15467 +               /* br ref is already inc-ed */
15468 +       }
15469 +
15470 +       if (flags & __O_TMPFILE) {
15471 +               AuDebugOn(!h_file);
15472 +               AuDebugOn(h_file != au_di(dentry)->di_htmpfile);
15473 +               au_di(dentry)->di_htmpfile = NULL;
15474 +
15475 +               if (!(flags & O_EXCL)) {
15476 +                       h_inode = file_inode(h_file);
15477 +                       spin_lock(&h_inode->i_lock);
15478 +                       h_inode->i_state |= I_LINKABLE;
15479 +                       spin_unlock(&h_inode->i_lock);
15480 +               }
15481 +       }
15482 +       au_set_fbtop(file, bindex);
15483 +       au_set_h_fptr(file, bindex, h_file);
15484 +       au_update_figen(file);
15485 +       /* todo: necessary? */
15486 +       /* file->f_ra = h_file->f_ra; */
15487 +
15488 +out:
15489 +       return err;
15490 +}
15491 +
15492 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15493 +                           struct file *file)
15494 +{
15495 +       int err;
15496 +       struct super_block *sb;
15497 +       struct au_do_open_args args = {
15498 +               .open   = au_do_open_nondir
15499 +       };
15500 +
15501 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15502 +             file, vfsub_file_flags(file), file->f_mode);
15503 +
15504 +       sb = file->f_path.dentry->d_sb;
15505 +       si_read_lock(sb, AuLock_FLUSH);
15506 +       err = au_do_open(file, &args);
15507 +       si_read_unlock(sb);
15508 +       return err;
15509 +}
15510 +
15511 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15512 +{
15513 +       struct au_finfo *finfo;
15514 +       aufs_bindex_t bindex;
15515 +
15516 +       finfo = au_fi(file);
15517 +       au_hbl_del(&finfo->fi_hlist,
15518 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15519 +       bindex = finfo->fi_btop;
15520 +       if (bindex >= 0)
15521 +               au_set_h_fptr(file, bindex, NULL);
15522 +
15523 +       au_finfo_fin(file);
15524 +       return 0;
15525 +}
15526 +
15527 +/* ---------------------------------------------------------------------- */
15528 +
15529 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15530 +{
15531 +       int err;
15532 +       struct file *h_file;
15533 +
15534 +       err = 0;
15535 +       h_file = au_hf_top(file);
15536 +       if (h_file)
15537 +               err = vfsub_flush(h_file, id);
15538 +       return err;
15539 +}
15540 +
15541 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15542 +{
15543 +       return au_do_flush(file, id, au_do_flush_nondir);
15544 +}
15545 +
15546 +/* ---------------------------------------------------------------------- */
15547 +/*
15548 + * read and write functions acquire [fdi]_rwsem once, but release before
15549 + * mmap_sem. This is because to stop a race condition between mmap(2).
15550 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15551 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15552 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15553 + */
15554 +
15555 +/* Callers should call au_read_post() or fput() in the end */
15556 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15557 +{
15558 +       struct file *h_file;
15559 +       int err;
15560 +
15561 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15562 +       if (!err) {
15563 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15564 +               h_file = au_hf_top(file);
15565 +               get_file(h_file);
15566 +               if (!keep_fi)
15567 +                       fi_read_unlock(file);
15568 +       } else
15569 +               h_file = ERR_PTR(err);
15570 +
15571 +       return h_file;
15572 +}
15573 +
15574 +static void au_read_post(struct inode *inode, struct file *h_file)
15575 +{
15576 +       /* update without lock, I don't think it a problem */
15577 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15578 +       fput(h_file);
15579 +}
15580 +
15581 +struct au_write_pre {
15582 +       /* input */
15583 +       unsigned int lsc;
15584 +
15585 +       /* output */
15586 +       blkcnt_t blks;
15587 +       aufs_bindex_t btop;
15588 +};
15589 +
15590 +/*
15591 + * return with iinfo is write-locked
15592 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15593 + * end
15594 + */
15595 +static struct file *au_write_pre(struct file *file, int do_ready,
15596 +                                struct au_write_pre *wpre)
15597 +{
15598 +       struct file *h_file;
15599 +       struct dentry *dentry;
15600 +       int err;
15601 +       unsigned int lsc;
15602 +       struct au_pin pin;
15603 +
15604 +       lsc = 0;
15605 +       if (wpre)
15606 +               lsc = wpre->lsc;
15607 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15608 +       h_file = ERR_PTR(err);
15609 +       if (unlikely(err))
15610 +               goto out;
15611 +
15612 +       dentry = file->f_path.dentry;
15613 +       if (do_ready) {
15614 +               err = au_ready_to_write(file, -1, &pin);
15615 +               if (unlikely(err)) {
15616 +                       h_file = ERR_PTR(err);
15617 +                       di_write_unlock(dentry);
15618 +                       goto out_fi;
15619 +               }
15620 +       }
15621 +
15622 +       di_downgrade_lock(dentry, /*flags*/0);
15623 +       if (wpre)
15624 +               wpre->btop = au_fbtop(file);
15625 +       h_file = au_hf_top(file);
15626 +       get_file(h_file);
15627 +       if (wpre)
15628 +               wpre->blks = file_inode(h_file)->i_blocks;
15629 +       if (do_ready)
15630 +               au_unpin(&pin);
15631 +       di_read_unlock(dentry, /*flags*/0);
15632 +
15633 +out_fi:
15634 +       fi_write_unlock(file);
15635 +out:
15636 +       return h_file;
15637 +}
15638 +
15639 +static void au_write_post(struct inode *inode, struct file *h_file,
15640 +                         struct au_write_pre *wpre, ssize_t written)
15641 +{
15642 +       struct inode *h_inode;
15643 +
15644 +       au_cpup_attr_timesizes(inode);
15645 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15646 +       h_inode = file_inode(h_file);
15647 +       inode->i_mode = h_inode->i_mode;
15648 +       ii_write_unlock(inode);
15649 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15650 +       if (written > 0)
15651 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15652 +                             /*force*/h_inode->i_blocks > wpre->blks);
15653 +       fput(h_file);
15654 +}
15655 +
15656 +/*
15657 + * todo: very ugly
15658 + * it locks both of i_mutex and si_rwsem for read in safe.
15659 + * if the plink maintenance mode continues forever (that is the problem),
15660 + * may loop forever.
15661 + */
15662 +static void au_mtx_and_read_lock(struct inode *inode)
15663 +{
15664 +       int err;
15665 +       struct super_block *sb = inode->i_sb;
15666 +
15667 +       while (1) {
15668 +               inode_lock(inode);
15669 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15670 +               if (!err)
15671 +                       break;
15672 +               inode_unlock(inode);
15673 +               si_read_lock(sb, AuLock_NOPLMW);
15674 +               si_read_unlock(sb);
15675 +       }
15676 +}
15677 +
15678 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15679 +                         struct iov_iter *iov_iter)
15680 +{
15681 +       ssize_t err;
15682 +       struct file *file;
15683 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15684 +
15685 +       err = security_file_permission(h_file, rw);
15686 +       if (unlikely(err))
15687 +               goto out;
15688 +
15689 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15690 +       iter = NULL;
15691 +       if (rw == MAY_READ)
15692 +               iter = h_file->f_op->read_iter;
15693 +       else if (rw == MAY_WRITE)
15694 +               iter = h_file->f_op->write_iter;
15695 +
15696 +       file = kio->ki_filp;
15697 +       kio->ki_filp = h_file;
15698 +       if (iter) {
15699 +               lockdep_off();
15700 +               err = iter(kio, iov_iter);
15701 +               lockdep_on();
15702 +       } else
15703 +               /* currently there is no such fs */
15704 +               WARN_ON_ONCE(1);
15705 +       kio->ki_filp = file;
15706 +
15707 +out:
15708 +       return err;
15709 +}
15710 +
15711 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15712 +{
15713 +       ssize_t err;
15714 +       struct file *file, *h_file;
15715 +       struct inode *inode;
15716 +       struct super_block *sb;
15717 +
15718 +       file = kio->ki_filp;
15719 +       inode = file_inode(file);
15720 +       sb = inode->i_sb;
15721 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15722 +
15723 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15724 +       err = PTR_ERR(h_file);
15725 +       if (IS_ERR(h_file))
15726 +               goto out;
15727 +
15728 +       if (au_test_loopback_kthread()) {
15729 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15730 +               if (file->f_mapping != h_file->f_mapping) {
15731 +                       file->f_mapping = h_file->f_mapping;
15732 +                       smp_mb(); /* unnecessary? */
15733 +               }
15734 +       }
15735 +       fi_read_unlock(file);
15736 +
15737 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15738 +       /* todo: necessary? */
15739 +       /* file->f_ra = h_file->f_ra; */
15740 +       au_read_post(inode, h_file);
15741 +
15742 +out:
15743 +       si_read_unlock(sb);
15744 +       return err;
15745 +}
15746 +
15747 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15748 +{
15749 +       ssize_t err;
15750 +       struct au_write_pre wpre;
15751 +       struct inode *inode;
15752 +       struct file *file, *h_file;
15753 +
15754 +       file = kio->ki_filp;
15755 +       inode = file_inode(file);
15756 +       au_mtx_and_read_lock(inode);
15757 +
15758 +       wpre.lsc = 0;
15759 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15760 +       err = PTR_ERR(h_file);
15761 +       if (IS_ERR(h_file))
15762 +               goto out;
15763 +
15764 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15765 +       au_write_post(inode, h_file, &wpre, err);
15766 +
15767 +out:
15768 +       si_read_unlock(inode->i_sb);
15769 +       inode_unlock(inode);
15770 +       return err;
15771 +}
15772 +
15773 +/*
15774 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15775 + * don't have their own .splice_{read,write} implimentations, and they use
15776 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15777 + * simple converters to f_op->iter_read() and ->iter_write().
15778 + * But we keep our own implementations because some non-mainlined FSes may have
15779 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15780 + * away an opportunity to co-work with aufs from them.
15781 + */
15782 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15783 +                               struct pipe_inode_info *pipe, size_t len,
15784 +                               unsigned int flags)
15785 +{
15786 +       ssize_t err;
15787 +       struct file *h_file;
15788 +       struct inode *inode;
15789 +       struct super_block *sb;
15790 +
15791 +       inode = file_inode(file);
15792 +       sb = inode->i_sb;
15793 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15794 +
15795 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15796 +       err = PTR_ERR(h_file);
15797 +       if (IS_ERR(h_file))
15798 +               goto out;
15799 +
15800 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15801 +       /* todo: necessary? */
15802 +       /* file->f_ra = h_file->f_ra; */
15803 +       au_read_post(inode, h_file);
15804 +
15805 +out:
15806 +       si_read_unlock(sb);
15807 +       return err;
15808 +}
15809 +
15810 +static ssize_t
15811 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15812 +                 size_t len, unsigned int flags)
15813 +{
15814 +       ssize_t err;
15815 +       struct au_write_pre wpre;
15816 +       struct inode *inode;
15817 +       struct file *h_file;
15818 +
15819 +       inode = file_inode(file);
15820 +       au_mtx_and_read_lock(inode);
15821 +
15822 +       wpre.lsc = 0;
15823 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15824 +       err = PTR_ERR(h_file);
15825 +       if (IS_ERR(h_file))
15826 +               goto out;
15827 +
15828 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15829 +       au_write_post(inode, h_file, &wpre, err);
15830 +
15831 +out:
15832 +       si_read_unlock(inode->i_sb);
15833 +       inode_unlock(inode);
15834 +       return err;
15835 +}
15836 +
15837 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15838 +                          loff_t len)
15839 +{
15840 +       long err;
15841 +       struct au_write_pre wpre;
15842 +       struct inode *inode;
15843 +       struct file *h_file;
15844 +
15845 +       inode = file_inode(file);
15846 +       au_mtx_and_read_lock(inode);
15847 +
15848 +       wpre.lsc = 0;
15849 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15850 +       err = PTR_ERR(h_file);
15851 +       if (IS_ERR(h_file))
15852 +               goto out;
15853 +
15854 +       lockdep_off();
15855 +       err = vfs_fallocate(h_file, mode, offset, len);
15856 +       lockdep_on();
15857 +       /*
15858 +        * we don't need to call file_modifed() here since au_write_post()
15859 +        * is equivalent and copies-up all timestamps and permission bits.
15860 +        */
15861 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15862 +
15863 +out:
15864 +       si_read_unlock(inode->i_sb);
15865 +       inode_unlock(inode);
15866 +       return err;
15867 +}
15868 +
15869 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15870 +                                   struct file *dst, loff_t dst_pos,
15871 +                                   size_t len, unsigned int flags)
15872 +{
15873 +       ssize_t err;
15874 +       struct au_write_pre wpre;
15875 +       enum { SRC, DST };
15876 +       struct {
15877 +               struct inode *inode;
15878 +               struct file *h_file;
15879 +               struct super_block *h_sb;
15880 +       } a[2];
15881 +#define a_src  a[SRC]
15882 +#define a_dst  a[DST]
15883 +
15884 +       err = -EINVAL;
15885 +       a_src.inode = file_inode(src);
15886 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15887 +               goto out;
15888 +       a_dst.inode = file_inode(dst);
15889 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15890 +               goto out;
15891 +
15892 +       au_mtx_and_read_lock(a_dst.inode);
15893 +       /*
15894 +        * in order to match the order in di_write_lock2_{child,parent}(),
15895 +        * use f_path.dentry for this comparison.
15896 +        */
15897 +       if (src->f_path.dentry < dst->f_path.dentry) {
15898 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15899 +               err = PTR_ERR(a_src.h_file);
15900 +               if (IS_ERR(a_src.h_file))
15901 +                       goto out_si;
15902 +
15903 +               wpre.lsc = AuLsc_FI_2;
15904 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15905 +               err = PTR_ERR(a_dst.h_file);
15906 +               if (IS_ERR(a_dst.h_file)) {
15907 +                       au_read_post(a_src.inode, a_src.h_file);
15908 +                       goto out_si;
15909 +               }
15910 +       } else {
15911 +               wpre.lsc = AuLsc_FI_1;
15912 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15913 +               err = PTR_ERR(a_dst.h_file);
15914 +               if (IS_ERR(a_dst.h_file))
15915 +                       goto out_si;
15916 +
15917 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15918 +               err = PTR_ERR(a_src.h_file);
15919 +               if (IS_ERR(a_src.h_file)) {
15920 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15921 +                                     /*written*/0);
15922 +                       goto out_si;
15923 +               }
15924 +       }
15925 +
15926 +       err = -EXDEV;
15927 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15928 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15929 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15930 +               AuDbgFile(src);
15931 +               AuDbgFile(dst);
15932 +               goto out_file;
15933 +       }
15934 +
15935 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15936 +                                   dst_pos, len, flags);
15937 +
15938 +out_file:
15939 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15940 +       fi_read_unlock(src);
15941 +       au_read_post(a_src.inode, a_src.h_file);
15942 +out_si:
15943 +       si_read_unlock(a_dst.inode->i_sb);
15944 +       inode_unlock(a_dst.inode);
15945 +out:
15946 +       return err;
15947 +#undef a_src
15948 +#undef a_dst
15949 +}
15950 +
15951 +/* ---------------------------------------------------------------------- */
15952 +
15953 +/*
15954 + * The locking order around current->mmap_sem.
15955 + * - in most and regular cases
15956 + *   file I/O syscall -- aufs_read() or something
15957 + *     -- si_rwsem for read -- mmap_sem
15958 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15959 + * - in mmap case
15960 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15961 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15962 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15963 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15964 + * It means that when aufs acquires si_rwsem for write, the process should never
15965 + * acquire mmap_sem.
15966 + *
15967 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15968 + * problem either since any directory is not able to be mmap-ed.
15969 + * The similar scenario is applied to aufs_readlink() too.
15970 + */
15971 +
15972 +#if 0 /* stop calling security_file_mmap() */
15973 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15974 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15975 +
15976 +static unsigned long au_arch_prot_conv(unsigned long flags)
15977 +{
15978 +       /* currently ppc64 only */
15979 +#ifdef CONFIG_PPC64
15980 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15981 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15982 +       return AuConv_VM_PROT(flags, SAO);
15983 +#else
15984 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15985 +       return 0;
15986 +#endif
15987 +}
15988 +
15989 +static unsigned long au_prot_conv(unsigned long flags)
15990 +{
15991 +       return AuConv_VM_PROT(flags, READ)
15992 +               | AuConv_VM_PROT(flags, WRITE)
15993 +               | AuConv_VM_PROT(flags, EXEC)
15994 +               | au_arch_prot_conv(flags);
15995 +}
15996 +
15997 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
15998 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
15999 +
16000 +static unsigned long au_flag_conv(unsigned long flags)
16001 +{
16002 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16003 +               | AuConv_VM_MAP(flags, DENYWRITE)
16004 +               | AuConv_VM_MAP(flags, LOCKED);
16005 +}
16006 +#endif
16007 +
16008 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16009 +{
16010 +       int err;
16011 +       const unsigned char wlock
16012 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16013 +       struct super_block *sb;
16014 +       struct file *h_file;
16015 +       struct inode *inode;
16016 +
16017 +       AuDbgVmRegion(file, vma);
16018 +
16019 +       inode = file_inode(file);
16020 +       sb = inode->i_sb;
16021 +       lockdep_off();
16022 +       si_read_lock(sb, AuLock_NOPLMW);
16023 +
16024 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16025 +       lockdep_on();
16026 +       err = PTR_ERR(h_file);
16027 +       if (IS_ERR(h_file))
16028 +               goto out;
16029 +
16030 +       err = 0;
16031 +       au_set_mmapped(file);
16032 +       au_vm_file_reset(vma, h_file);
16033 +       /*
16034 +        * we cannot call security_mmap_file() here since it may acquire
16035 +        * mmap_sem or i_mutex.
16036 +        *
16037 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16038 +        *                       au_flag_conv(vma->vm_flags));
16039 +        */
16040 +       if (!err)
16041 +               err = call_mmap(h_file, vma);
16042 +       if (!err) {
16043 +               au_vm_prfile_set(vma, file);
16044 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16045 +               goto out_fput; /* success */
16046 +       }
16047 +       au_unset_mmapped(file);
16048 +       au_vm_file_reset(vma, file);
16049 +
16050 +out_fput:
16051 +       lockdep_off();
16052 +       ii_write_unlock(inode);
16053 +       lockdep_on();
16054 +       fput(h_file);
16055 +out:
16056 +       lockdep_off();
16057 +       si_read_unlock(sb);
16058 +       lockdep_on();
16059 +       AuTraceErr(err);
16060 +       return err;
16061 +}
16062 +
16063 +/* ---------------------------------------------------------------------- */
16064 +
16065 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16066 +                            int datasync)
16067 +{
16068 +       int err;
16069 +       struct au_write_pre wpre;
16070 +       struct inode *inode;
16071 +       struct file *h_file;
16072 +
16073 +       err = 0; /* -EBADF; */ /* posix? */
16074 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16075 +               goto out;
16076 +
16077 +       inode = file_inode(file);
16078 +       au_mtx_and_read_lock(inode);
16079 +
16080 +       wpre.lsc = 0;
16081 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16082 +       err = PTR_ERR(h_file);
16083 +       if (IS_ERR(h_file))
16084 +               goto out_unlock;
16085 +
16086 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16087 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16088 +
16089 +out_unlock:
16090 +       si_read_unlock(inode->i_sb);
16091 +       inode_unlock(inode);
16092 +out:
16093 +       return err;
16094 +}
16095 +
16096 +static int aufs_fasync(int fd, struct file *file, int flag)
16097 +{
16098 +       int err;
16099 +       struct file *h_file;
16100 +       struct super_block *sb;
16101 +
16102 +       sb = file->f_path.dentry->d_sb;
16103 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16104 +
16105 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16106 +       err = PTR_ERR(h_file);
16107 +       if (IS_ERR(h_file))
16108 +               goto out;
16109 +
16110 +       if (h_file->f_op->fasync)
16111 +               err = h_file->f_op->fasync(fd, h_file, flag);
16112 +       fput(h_file); /* instead of au_read_post() */
16113 +
16114 +out:
16115 +       si_read_unlock(sb);
16116 +       return err;
16117 +}
16118 +
16119 +static int aufs_setfl(struct file *file, unsigned long arg)
16120 +{
16121 +       int err;
16122 +       struct file *h_file;
16123 +       struct super_block *sb;
16124 +
16125 +       sb = file->f_path.dentry->d_sb;
16126 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16127 +
16128 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16129 +       err = PTR_ERR(h_file);
16130 +       if (IS_ERR(h_file))
16131 +               goto out;
16132 +
16133 +       /* stop calling h_file->fasync */
16134 +       arg |= vfsub_file_flags(file) & FASYNC;
16135 +       err = setfl(/*unused fd*/-1, h_file, arg);
16136 +       fput(h_file); /* instead of au_read_post() */
16137 +
16138 +out:
16139 +       si_read_unlock(sb);
16140 +       return err;
16141 +}
16142 +
16143 +/* ---------------------------------------------------------------------- */
16144 +
16145 +/* no one supports this operation, currently */
16146 +#if 0 /* reserved for future use */
16147 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16148 +                            size_t len, loff_t *pos, int more)
16149 +{
16150 +}
16151 +#endif
16152 +
16153 +/* ---------------------------------------------------------------------- */
16154 +
16155 +const struct file_operations aufs_file_fop = {
16156 +       .owner          = THIS_MODULE,
16157 +
16158 +       .llseek         = default_llseek,
16159 +
16160 +       .read_iter      = aufs_read_iter,
16161 +       .write_iter     = aufs_write_iter,
16162 +
16163 +#ifdef CONFIG_AUFS_POLL
16164 +       .poll           = aufs_poll,
16165 +#endif
16166 +       .unlocked_ioctl = aufs_ioctl_nondir,
16167 +#ifdef CONFIG_COMPAT
16168 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16169 +#endif
16170 +       .mmap           = aufs_mmap,
16171 +       .open           = aufs_open_nondir,
16172 +       .flush          = aufs_flush_nondir,
16173 +       .release        = aufs_release_nondir,
16174 +       .fsync          = aufs_fsync_nondir,
16175 +       .fasync         = aufs_fasync,
16176 +       /* .sendpage    = aufs_sendpage, */
16177 +       .setfl          = aufs_setfl,
16178 +       .splice_write   = aufs_splice_write,
16179 +       .splice_read    = aufs_splice_read,
16180 +#if 0 /* reserved for future use */
16181 +       .aio_splice_write = aufs_aio_splice_write,
16182 +       .aio_splice_read  = aufs_aio_splice_read,
16183 +#endif
16184 +       .fallocate      = aufs_fallocate,
16185 +       .copy_file_range = aufs_copy_file_range
16186 +};
16187 diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16188 --- /usr/share/empty/fs/aufs/fsctx.c    1970-01-01 01:00:00.000000000 +0100
16189 +++ linux/fs/aufs/fsctx.c       2022-11-05 23:02:18.965889284 +0100
16190 @@ -0,0 +1,1242 @@
16191 +// SPDX-License-Identifier: GPL-2.0
16192 +/*
16193 + * Copyright (C) 2022 Junjiro R. Okajima
16194 + *
16195 + * This program is free software; you can redistribute it and/or modify
16196 + * it under the terms of the GNU General Public License as published by
16197 + * the Free Software Foundation; either version 2 of the License, or
16198 + * (at your option) any later version.
16199 + *
16200 + * This program is distributed in the hope that it will be useful,
16201 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16202 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16203 + * GNU General Public License for more details.
16204 + *
16205 + * You should have received a copy of the GNU General Public License
16206 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16207 + */
16208 +
16209 +/*
16210 + * fs context, aka new mount api
16211 + */
16212 +
16213 +#include <linux/fs_context.h>
16214 +#include "aufs.h"
16215 +
16216 +struct au_fsctx_opts {
16217 +       aufs_bindex_t bindex;
16218 +       unsigned char skipped;
16219 +       struct au_opt *opt, *opt_tail;
16220 +       struct super_block *sb;
16221 +       struct au_sbinfo *sbinfo;
16222 +       struct au_opts opts;
16223 +};
16224 +
16225 +/* stop extra interpretation of errno in mount(8), and strange error messages */
16226 +static int cvt_err(int err)
16227 +{
16228 +       AuTraceErr(err);
16229 +
16230 +       switch (err) {
16231 +       case -ENOENT:
16232 +       case -ENOTDIR:
16233 +       case -EEXIST:
16234 +       case -EIO:
16235 +               err = -EINVAL;
16236 +       }
16237 +       return err;
16238 +}
16239 +
16240 +static int au_fsctx_reconfigure(struct fs_context *fc)
16241 +{
16242 +       int err, do_dx;
16243 +       unsigned int mntflags;
16244 +       struct dentry *root;
16245 +       struct super_block *sb;
16246 +       struct inode *inode;
16247 +       struct au_fsctx_opts *a = fc->fs_private;
16248 +
16249 +       AuDbg("fc %p\n", fc);
16250 +
16251 +       root = fc->root;
16252 +       sb = root->d_sb;
16253 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16254 +       if (!err) {
16255 +               di_write_lock_child(root);
16256 +               err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16257 +               aufs_write_unlock(root);
16258 +       }
16259 +
16260 +       inode = d_inode(root);
16261 +       inode_lock(inode);
16262 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16263 +       if (unlikely(err))
16264 +               goto out;
16265 +       di_write_lock_child(root);
16266 +
16267 +       /* au_opts_remount() may return an error */
16268 +       err = au_opts_remount(sb, &a->opts);
16269 +
16270 +       if (au_ftest_opts(a->opts.flags, REFRESH))
16271 +               au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16272 +                                                    REFRESH_IDOP));
16273 +
16274 +       if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16275 +               mntflags = au_mntflags(sb);
16276 +               do_dx = !!au_opt_test(mntflags, DIO);
16277 +               au_dy_arefresh(do_dx);
16278 +       }
16279 +
16280 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16281 +       aufs_write_unlock(root);
16282 +
16283 +out:
16284 +       inode_unlock(inode);
16285 +       err = cvt_err(err);
16286 +       AuTraceErr(err);
16287 +
16288 +       return err;
16289 +}
16290 +
16291 +/* ---------------------------------------------------------------------- */
16292 +
16293 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16294 +{
16295 +       int err;
16296 +       struct au_fsctx_opts *a = fc->fs_private;
16297 +       struct au_sbinfo *sbinfo = a->sbinfo;
16298 +       struct dentry *root;
16299 +       struct inode *inode;
16300 +
16301 +       sbinfo->si_sb = sb;
16302 +       sb->s_fs_info = sbinfo;
16303 +       kobject_get(&sbinfo->si_kobj);
16304 +
16305 +       __si_write_lock(sb);
16306 +       si_pid_set(sb);
16307 +       au_sbilist_add(sb);
16308 +
16309 +       /* all timestamps always follow the ones on the branch */
16310 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16311 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16312 +       sb->s_op = &aufs_sop;
16313 +       sb->s_d_op = &aufs_dop;
16314 +       sb->s_magic = AUFS_SUPER_MAGIC;
16315 +       sb->s_maxbytes = 0;
16316 +       sb->s_stack_depth = 1;
16317 +       au_export_init(sb);
16318 +       au_xattr_init(sb);
16319 +
16320 +       err = au_alloc_root(sb);
16321 +       if (unlikely(err)) {
16322 +               si_write_unlock(sb);
16323 +               goto out;
16324 +       }
16325 +       root = sb->s_root;
16326 +       inode = d_inode(root);
16327 +       ii_write_lock_parent(inode);
16328 +       aufs_write_unlock(root);
16329 +
16330 +       /* lock vfs_inode first, then aufs. */
16331 +       inode_lock(inode);
16332 +       aufs_write_lock(root);
16333 +       err = au_opts_mount(sb, &a->opts);
16334 +       AuTraceErr(err);
16335 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16336 +               sb->s_d_op = &aufs_dop_noreval;
16337 +               /* infofc(fc, "%ps", sb->s_d_op); */
16338 +               pr_info("%ps\n", sb->s_d_op);
16339 +               au_refresh_dop(root, /*force_reval*/0);
16340 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
16341 +               au_refresh_iop(inode, /*force_getattr*/0);
16342 +       }
16343 +       aufs_write_unlock(root);
16344 +       inode_unlock(inode);
16345 +       if (!err)
16346 +               goto out; /* success */
16347 +
16348 +       dput(root);
16349 +       sb->s_root = NULL;
16350 +
16351 +out:
16352 +       if (unlikely(err))
16353 +               kobject_put(&sbinfo->si_kobj);
16354 +       AuTraceErr(err);
16355 +       err = cvt_err(err);
16356 +       AuTraceErr(err);
16357 +       return err;
16358 +}
16359 +
16360 +static int au_fsctx_get_tree(struct fs_context *fc)
16361 +{
16362 +       int err;
16363 +
16364 +       AuDbg("fc %p\n", fc);
16365 +       err = get_tree_nodev(fc, au_fsctx_fill_super);
16366 +
16367 +       AuTraceErr(err);
16368 +       return err;
16369 +}
16370 +
16371 +/* ---------------------------------------------------------------------- */
16372 +
16373 +static void au_fsctx_dump(struct au_opts *opts)
16374 +{
16375 +#ifdef CONFIG_AUFS_DEBUG
16376 +       /* reduce stack space */
16377 +       union {
16378 +               struct au_opt_add *add;
16379 +               struct au_opt_del *del;
16380 +               struct au_opt_mod *mod;
16381 +               struct au_opt_xino *xino;
16382 +               struct au_opt_xino_itrunc *xino_itrunc;
16383 +               struct au_opt_wbr_create *create;
16384 +       } u;
16385 +       struct au_opt *opt;
16386 +
16387 +       opt = opts->opt;
16388 +       while (opt->type != Opt_tail) {
16389 +               switch (opt->type) {
16390 +               case Opt_add:
16391 +                       u.add = &opt->add;
16392 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16393 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16394 +                                 u.add->path.dentry);
16395 +                       break;
16396 +               case Opt_del:
16397 +                       fallthrough;
16398 +               case Opt_idel:
16399 +                       u.del = &opt->del;
16400 +                       AuDbg("del {%s, %p}\n",
16401 +                             u.del->pathname, u.del->h_path.dentry);
16402 +                       break;
16403 +               case Opt_mod:
16404 +                       fallthrough;
16405 +               case Opt_imod:
16406 +                       u.mod = &opt->mod;
16407 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16408 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16409 +                       break;
16410 +               case Opt_append:
16411 +                       u.add = &opt->add;
16412 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16413 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16414 +                                 u.add->path.dentry);
16415 +                       break;
16416 +               case Opt_prepend:
16417 +                       u.add = &opt->add;
16418 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16419 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16420 +                                 u.add->path.dentry);
16421 +                       break;
16422 +
16423 +               case Opt_dirwh:
16424 +                       AuDbg("dirwh %d\n", opt->dirwh);
16425 +                       break;
16426 +               case Opt_rdcache:
16427 +                       AuDbg("rdcache %d\n", opt->rdcache);
16428 +                       break;
16429 +               case Opt_rdblk:
16430 +                       AuDbg("rdblk %d\n", opt->rdblk);
16431 +                       break;
16432 +               case Opt_rdhash:
16433 +                       AuDbg("rdhash %u\n", opt->rdhash);
16434 +                       break;
16435 +
16436 +               case Opt_xino:
16437 +                       u.xino = &opt->xino;
16438 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16439 +                       break;
16440 +
16441 +#define au_fsctx_TF(name)                                        \
16442 +                       case Opt_##name:                          \
16443 +                               if (opt->tf)                      \
16444 +                                       AuLabel(name);            \
16445 +                               else                              \
16446 +                                       AuLabel(no##name);        \
16447 +                               break;
16448 +
16449 +               /* simple true/false flag */
16450 +               au_fsctx_TF(trunc_xino);
16451 +               au_fsctx_TF(trunc_xib);
16452 +               au_fsctx_TF(dirperm1);
16453 +               au_fsctx_TF(plink);
16454 +               au_fsctx_TF(shwh);
16455 +               au_fsctx_TF(dio);
16456 +               au_fsctx_TF(warn_perm);
16457 +               au_fsctx_TF(verbose);
16458 +               au_fsctx_TF(sum);
16459 +               au_fsctx_TF(dirren);
16460 +               au_fsctx_TF(acl);
16461 +#undef au_fsctx_TF
16462 +
16463 +               case Opt_trunc_xino_path:
16464 +                       fallthrough;
16465 +               case Opt_itrunc_xino:
16466 +                       u.xino_itrunc = &opt->xino_itrunc;
16467 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16468 +                       break;
16469 +               case Opt_noxino:
16470 +                       AuLabel(noxino);
16471 +                       break;
16472 +
16473 +               case Opt_list_plink:
16474 +                       AuLabel(list_plink);
16475 +                       break;
16476 +               case Opt_udba:
16477 +                       AuDbg("udba %d, %s\n",
16478 +                                 opt->udba, au_optstr_udba(opt->udba));
16479 +                       break;
16480 +               case Opt_diropq_a:
16481 +                       AuLabel(diropq_a);
16482 +                       break;
16483 +               case Opt_diropq_w:
16484 +                       AuLabel(diropq_w);
16485 +                       break;
16486 +               case Opt_wsum:
16487 +                       AuLabel(wsum);
16488 +                       break;
16489 +               case Opt_wbr_create:
16490 +                       u.create = &opt->wbr_create;
16491 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
16492 +                                 au_optstr_wbr_create(u.create->wbr_create));
16493 +                       switch (u.create->wbr_create) {
16494 +                       case AuWbrCreate_MFSV:
16495 +                               fallthrough;
16496 +                       case AuWbrCreate_PMFSV:
16497 +                               AuDbg("%d sec\n", u.create->mfs_second);
16498 +                               break;
16499 +                       case AuWbrCreate_MFSRR:
16500 +                               fallthrough;
16501 +                       case AuWbrCreate_TDMFS:
16502 +                               AuDbg("%llu watermark\n",
16503 +                                         u.create->mfsrr_watermark);
16504 +                               break;
16505 +                       case AuWbrCreate_MFSRRV:
16506 +                               fallthrough;
16507 +                       case AuWbrCreate_TDMFSV:
16508 +                               fallthrough;
16509 +                       case AuWbrCreate_PMFSRRV:
16510 +                               AuDbg("%llu watermark, %d sec\n",
16511 +                                         u.create->mfsrr_watermark,
16512 +                                         u.create->mfs_second);
16513 +                               break;
16514 +                       }
16515 +                       break;
16516 +               case Opt_wbr_copyup:
16517 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16518 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
16519 +                       break;
16520 +               case Opt_fhsm_sec:
16521 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16522 +                       break;
16523 +
16524 +               default:
16525 +                       AuDbg("type %d\n", opt->type);
16526 +                       BUG();
16527 +               }
16528 +               opt++;
16529 +       }
16530 +#endif
16531 +}
16532 +
16533 +/* ---------------------------------------------------------------------- */
16534 +
16535 +/*
16536 + * For conditionally compiled mount options.
16537 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16538 + */
16539 +#define au_ignore_flag(name, action)           \
16540 +       fsparam_flag(name, action),             \
16541 +       fsparam_flag("no" name, Opt_ignore_silent)
16542 +
16543 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16544 +       fsparam_string("br", Opt_br),
16545 +
16546 +       /* "add=%d:%s" or "ins=%d:%s" */
16547 +       fsparam_string("add", Opt_add),
16548 +       fsparam_string("ins", Opt_add),
16549 +       fsparam_path("append", Opt_append),
16550 +       fsparam_path("prepend", Opt_prepend),
16551 +
16552 +       fsparam_path("del", Opt_del),
16553 +       /* fsparam_s32("idel", Opt_idel), */
16554 +       fsparam_path("mod", Opt_mod),
16555 +       /* fsparam_string("imod", Opt_imod), */
16556 +
16557 +       fsparam_s32("dirwh", Opt_dirwh),
16558 +
16559 +       fsparam_path("xino", Opt_xino),
16560 +       fsparam_flag("noxino", Opt_noxino),
16561 +       fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16562 +       /* "trunc_xino_v=%d:%d" */
16563 +       /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16564 +       fsparam_path("trunc_xino", Opt_trunc_xino_path),
16565 +       fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16566 +       /* fsparam_path("zxino", Opt_zxino), */
16567 +       fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16568 +
16569 +#ifdef CONFIG_PROC_FS
16570 +       fsparam_flag_no("plink", Opt_plink),
16571 +#else
16572 +       au_ignore_flag("plink", Opt_ignore),
16573 +#endif
16574 +
16575 +#ifdef CONFIG_AUFS_DEBUG
16576 +       fsparam_flag("list_plink", Opt_list_plink),
16577 +#endif
16578 +
16579 +       fsparam_string("udba", Opt_udba),
16580 +
16581 +       fsparam_flag_no("dio", Opt_dio),
16582 +
16583 +#ifdef CONFIG_AUFS_DIRREN
16584 +       fsparam_flag_no("dirren", Opt_dirren),
16585 +#else
16586 +       au_ignore_flag("dirren", Opt_ignore),
16587 +#endif
16588 +
16589 +#ifdef CONFIG_AUFS_FHSM
16590 +       fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16591 +#else
16592 +       fsparam_s32("fhsm_sec", Opt_ignore),
16593 +#endif
16594 +
16595 +       /* always | a | whiteouted | w */
16596 +       fsparam_string("diropq", Opt_diropq),
16597 +
16598 +       fsparam_flag_no("warn_perm", Opt_warn_perm),
16599 +
16600 +#ifdef CONFIG_AUFS_SHWH
16601 +       fsparam_flag_no("shwh", Opt_shwh),
16602 +#else
16603 +       au_ignore_flag("shwh", Opt_err),
16604 +#endif
16605 +
16606 +       fsparam_flag_no("dirperm1", Opt_dirperm1),
16607 +
16608 +       fsparam_flag_no("verbose", Opt_verbose),
16609 +       fsparam_flag("v", Opt_verbose),
16610 +       fsparam_flag("quiet", Opt_noverbose),
16611 +       fsparam_flag("q", Opt_noverbose),
16612 +       /* user-space may handle this */
16613 +       fsparam_flag("silent", Opt_noverbose),
16614 +
16615 +       fsparam_flag_no("sum", Opt_sum),
16616 +       fsparam_flag("wsum", Opt_wsum),
16617 +
16618 +       fsparam_s32("rdcache", Opt_rdcache),
16619 +       /* "def" or s32 */
16620 +       fsparam_string("rdblk", Opt_rdblk),
16621 +       /* "def" or s32 */
16622 +       fsparam_string("rdhash", Opt_rdhash),
16623 +
16624 +       fsparam_string("create", Opt_wbr_create),
16625 +       fsparam_string("create_policy", Opt_wbr_create),
16626 +       fsparam_string("cpup", Opt_wbr_copyup),
16627 +       fsparam_string("copyup", Opt_wbr_copyup),
16628 +       fsparam_string("copyup_policy", Opt_wbr_copyup),
16629 +
16630 +       /* generic VFS flag */
16631 +#ifdef CONFIG_FS_POSIX_ACL
16632 +       fsparam_flag_no("acl", Opt_acl),
16633 +#else
16634 +       au_ignore_flag("acl", Opt_ignore),
16635 +#endif
16636 +
16637 +       /* internal use for the scripts */
16638 +       fsparam_string("si", Opt_ignore_silent),
16639 +
16640 +       /* obsoleted, keep them temporary */
16641 +       fsparam_flag("nodlgt", Opt_ignore_silent),
16642 +       fsparam_flag("clean_plink", Opt_ignore),
16643 +       fsparam_string("dirs", Opt_br),
16644 +       fsparam_u32("debug", Opt_ignore),
16645 +       /* "whiteout" or "all" */
16646 +       fsparam_string("delete", Opt_ignore),
16647 +       fsparam_string("imap", Opt_ignore),
16648 +
16649 +       /* temporary workaround, due to old mount(8)? */
16650 +       fsparam_flag("relatime", Opt_ignore_silent),
16651 +
16652 +       {}
16653 +};
16654 +
16655 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16656 +                                char *brspec, size_t speclen,
16657 +                                aufs_bindex_t bindex)
16658 +{
16659 +       int err;
16660 +       char *p;
16661 +
16662 +       AuDbg("brspec %s\n", brspec);
16663 +
16664 +       err = -ENOMEM;
16665 +       if (!speclen)
16666 +               speclen = strlen(brspec);
16667 +       /* will be freed by au_fsctx_free() */
16668 +       p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16669 +       if (unlikely(!p)) {
16670 +               errorfc(fc, "failed in %s", brspec);
16671 +               goto out;
16672 +       }
16673 +       err = au_opt_add(opt, p, fc->sb_flags, bindex);
16674 +
16675 +out:
16676 +       AuTraceErr(err);
16677 +       return err;
16678 +}
16679 +
16680 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16681 +{
16682 +       int err;
16683 +       char *p;
16684 +       struct au_fsctx_opts *a = fc->fs_private;
16685 +       struct au_opt *opt = a->opt;
16686 +       aufs_bindex_t bindex = a->bindex;
16687 +
16688 +       AuDbg("brspec %s\n", brspec);
16689 +
16690 +       err = -EINVAL;
16691 +       while ((p = strsep(&brspec, ":")) && *p) {
16692 +               err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16693 +               AuTraceErr(err);
16694 +               if (unlikely(err))
16695 +                       break;
16696 +               bindex++;
16697 +               opt++;
16698 +               if (unlikely(opt > a->opt_tail)) {
16699 +                       err = -E2BIG;
16700 +                       bindex--;
16701 +                       opt--;
16702 +                       break;
16703 +               }
16704 +               opt->type = Opt_tail;
16705 +               a->skipped = 1;
16706 +       }
16707 +       a->bindex = bindex;
16708 +       a->opt = opt;
16709 +
16710 +       AuTraceErr(err);
16711 +       return err;
16712 +}
16713 +
16714 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16715 +{
16716 +       int err, n;
16717 +       char *p;
16718 +       struct au_fsctx_opts *a = fc->fs_private;
16719 +       struct au_opt *opt = a->opt;
16720 +
16721 +       err = -EINVAL;
16722 +       p = strchr(addspec, ':');
16723 +       if (unlikely(!p)) {
16724 +               errorfc(fc, "bad arg in %s", addspec);
16725 +               goto out;
16726 +       }
16727 +       *p++ = '\0';
16728 +       err = kstrtoint(addspec, 0, &n);
16729 +       if (unlikely(err)) {
16730 +               errorfc(fc, "bad integer in %s", addspec);
16731 +               goto out;
16732 +       }
16733 +       AuDbg("n %d\n", n);
16734 +       err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16735 +
16736 +out:
16737 +       AuTraceErr(err);
16738 +       return err;
16739 +}
16740 +
16741 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16742 +                             struct fs_parameter *param)
16743 +{
16744 +       int err;
16745 +
16746 +       err = -ENOMEM;
16747 +       /* will be freed by au_fsctx_free() */
16748 +       del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16749 +       if (unlikely(!del->pathname))
16750 +               goto out;
16751 +       AuDbg("del %s\n", del->pathname);
16752 +       err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16753 +       if (unlikely(err))
16754 +               errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16755 +
16756 +out:
16757 +       AuTraceErr(err);
16758 +       return err;
16759 +}
16760 +
16761 +#if 0 /* reserved for future use */
16762 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16763 +                              aufs_bindex_t bindex)
16764 +{
16765 +       int err;
16766 +       struct super_block *sb;
16767 +       struct dentry *root;
16768 +       struct au_fsctx_opts *a = fc->fs_private;
16769 +
16770 +       sb = a->sb;
16771 +       AuDebugOn(!sb);
16772 +
16773 +       err = -EINVAL;
16774 +       root = sb->s_root;
16775 +       aufs_read_lock(root, AuLock_FLUSH);
16776 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
16777 +               errorfc(fc, "out of bounds, %d", bindex);
16778 +               goto out;
16779 +       }
16780 +
16781 +       err = 0;
16782 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
16783 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16784 +
16785 +out:
16786 +       aufs_read_unlock(root, !AuLock_IR);
16787 +       AuTraceErr(err);
16788 +       return err;
16789 +}
16790 +#endif
16791 +
16792 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16793 +                             struct fs_parameter *param)
16794 +{
16795 +       int err;
16796 +       struct path path;
16797 +       char *p;
16798 +
16799 +       err = -ENOMEM;
16800 +       /* will be freed by au_fsctx_free() */
16801 +       mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16802 +       if (unlikely(!mod->path))
16803 +               goto out;
16804 +
16805 +       err = -EINVAL;
16806 +       p = strchr(mod->path, '=');
16807 +       if (unlikely(!p)) {
16808 +               errorfc(fc, "no permission %s", mod->path);
16809 +               goto out;
16810 +       }
16811 +
16812 +       *p++ = 0;
16813 +       err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16814 +       if (unlikely(err)) {
16815 +               errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16816 +               goto out;
16817 +       }
16818 +
16819 +       mod->perm = au_br_perm_val(p);
16820 +       AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16821 +       mod->h_root = dget(path.dentry);
16822 +       path_put(&path);
16823 +
16824 +out:
16825 +       AuTraceErr(err);
16826 +       return err;
16827 +}
16828 +
16829 +#if 0 /* reserved for future use */
16830 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16831 +                              char *ibrspec)
16832 +{
16833 +       int err, n;
16834 +       char *p;
16835 +       struct super_block *sb;
16836 +       struct dentry *root;
16837 +       struct au_fsctx_opts *a = fc->fs_private;
16838 +
16839 +       sb = a->sb;
16840 +       AuDebugOn(!sb);
16841 +
16842 +       err = -EINVAL;
16843 +       p = strchr(ibrspec, ':');
16844 +       if (unlikely(!p)) {
16845 +               errorfc(fc, "no index, %s", ibrspec);
16846 +               goto out;
16847 +       }
16848 +       *p++ = '\0';
16849 +       err = kstrtoint(ibrspec, 0, &n);
16850 +       if (unlikely(err)) {
16851 +               errorfc(fc, "bad integer in %s", ibrspec);
16852 +               goto out;
16853 +       }
16854 +       AuDbg("n %d\n", n);
16855 +
16856 +       root = sb->s_root;
16857 +       aufs_read_lock(root, AuLock_FLUSH);
16858 +       if (n < 0 || au_sbbot(sb) < n) {
16859 +               errorfc(fc, "out of bounds, %d", bindex);
16860 +               goto out_root;
16861 +       }
16862 +
16863 +       err = 0;
16864 +       mod->perm = au_br_perm_val(p);
16865 +       AuDbg("mod path %s, perm 0x%x, %s\n",
16866 +             mod->path, mod->perm, p);
16867 +       mod->h_root = dget(au_h_dptr(root, bindex));
16868 +
16869 +out_root:
16870 +       aufs_read_unlock(root, !AuLock_IR);
16871 +out:
16872 +       AuTraceErr(err);
16873 +       return err;
16874 +}
16875 +#endif
16876 +
16877 +static int au_fsctx_parse_xino(struct fs_context *fc,
16878 +                              struct au_opt_xino *xino,
16879 +                              struct fs_parameter *param)
16880 +{
16881 +       int err;
16882 +       struct au_fsctx_opts *a = fc->fs_private;
16883 +
16884 +       err = -ENOMEM;
16885 +       /* will be freed by au_opts_free() */
16886 +       xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16887 +       if (unlikely(!xino->path))
16888 +               goto out;
16889 +       AuDbg("path %s\n", xino->path);
16890 +
16891 +       xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16892 +                                   /*wbrtop*/0);
16893 +       err = PTR_ERR(xino->file);
16894 +       if (IS_ERR(xino->file)) {
16895 +               xino->file = NULL;
16896 +               goto out;
16897 +       }
16898 +
16899 +       err = 0;
16900 +       if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16901 +               err = -EINVAL;
16902 +               errorfc(fc, "%s must be outside", xino->path);
16903 +       }
16904 +
16905 +out:
16906 +       AuTraceErr(err);
16907 +       return err;
16908 +}
16909 +
16910 +static
16911 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16912 +                                   struct au_opt_xino_itrunc *xino_itrunc,
16913 +                                   char *pathname)
16914 +{
16915 +       int err;
16916 +       aufs_bindex_t bbot, bindex;
16917 +       struct path path;
16918 +       struct dentry *root;
16919 +       struct au_fsctx_opts *a = fc->fs_private;
16920 +
16921 +       AuDebugOn(!a->sb);
16922 +
16923 +       err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16924 +       if (unlikely(err)) {
16925 +               errorfc(fc, "lookup failed %s (%d)", pathname, err);
16926 +               goto out;
16927 +       }
16928 +
16929 +       xino_itrunc->bindex = -1;
16930 +       root = a->sb->s_root;
16931 +       aufs_read_lock(root, AuLock_FLUSH);
16932 +       bbot = au_sbbot(a->sb);
16933 +       for (bindex = 0; bindex <= bbot; bindex++) {
16934 +               if (au_h_dptr(root, bindex) == path.dentry) {
16935 +                       xino_itrunc->bindex = bindex;
16936 +                       break;
16937 +               }
16938 +       }
16939 +       aufs_read_unlock(root, !AuLock_IR);
16940 +       path_put(&path);
16941 +
16942 +       if (unlikely(xino_itrunc->bindex < 0)) {
16943 +               err = -EINVAL;
16944 +               errorfc(fc, "no such branch %s", pathname);
16945 +       }
16946 +
16947 +out:
16948 +       AuTraceErr(err);
16949 +       return err;
16950 +}
16951 +
16952 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16953 +                                     struct au_opt_xino_itrunc *xino_itrunc,
16954 +                                     unsigned int bindex)
16955 +{
16956 +       int err;
16957 +       aufs_bindex_t bbot;
16958 +       struct super_block *sb;
16959 +       struct au_fsctx_opts *a = fc->fs_private;
16960 +
16961 +       sb = a->sb;
16962 +       AuDebugOn(!sb);
16963 +
16964 +       err = 0;
16965 +       si_noflush_read_lock(sb);
16966 +       bbot = au_sbbot(sb);
16967 +       si_read_unlock(sb);
16968 +       if (bindex <= bbot)
16969 +               xino_itrunc->bindex = bindex;
16970 +       else {
16971 +               err = -EINVAL;
16972 +               errorfc(fc, "out of bounds, %u", bindex);
16973 +       }
16974 +
16975 +       AuTraceErr(err);
16976 +       return err;
16977 +}
16978 +
16979 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16980 +{
16981 +       int err, token;
16982 +       struct fs_parse_result result;
16983 +       struct au_fsctx_opts *a = fc->fs_private;
16984 +       struct au_opt *opt = a->opt;
16985 +
16986 +       AuDbg("fc %p, param {key %s, string %s}\n",
16987 +             fc, param->key, param->string);
16988 +       err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
16989 +       if (unlikely(err < 0))
16990 +               goto out;
16991 +       token = err;
16992 +       AuDbg("token %d, res{negated %d, uint64 %llu}\n",
16993 +             token, result.negated, result.uint_64);
16994 +
16995 +       err = -EINVAL;
16996 +       a->skipped = 0;
16997 +       switch (token) {
16998 +       case Opt_br:
16999 +               err = au_fsctx_parse_br(fc, param->string);
17000 +               break;
17001 +       case Opt_add:
17002 +               err = au_fsctx_parse_add(fc, param->string);
17003 +               break;
17004 +       case Opt_append:
17005 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17006 +                                           /*dummy bindex*/1);
17007 +               break;
17008 +       case Opt_prepend:
17009 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17010 +                                           /*bindex*/0);
17011 +               break;
17012 +
17013 +       case Opt_del:
17014 +               err = au_fsctx_parse_del(fc, &opt->del, param);
17015 +               break;
17016 +#if 0 /* reserved for future use */
17017 +       case Opt_idel:
17018 +               if (!a->sb) {
17019 +                       err = 0;
17020 +                       a->skipped = 1;
17021 +                       break;
17022 +               }
17023 +               del->pathname = "(indexed)";
17024 +               err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17025 +               break;
17026 +#endif
17027 +
17028 +       case Opt_mod:
17029 +               err = au_fsctx_parse_mod(fc, &opt->mod, param);
17030 +               break;
17031 +#ifdef IMOD /* reserved for future use */
17032 +       case Opt_imod:
17033 +               if (!a->sb) {
17034 +                       err = 0;
17035 +                       a->skipped = 1;
17036 +                       break;
17037 +               }
17038 +               u.mod->path = "(indexed)";
17039 +               err = au_opts_parse_imod(fc, &opt->mod, param->string);
17040 +               break;
17041 +#endif
17042 +
17043 +       case Opt_xino:
17044 +               err = au_fsctx_parse_xino(fc, &opt->xino, param);
17045 +               break;
17046 +       case Opt_trunc_xino_path:
17047 +               if (!a->sb) {
17048 +                       errorfc(fc, "no such branch %s", param->string);
17049 +                       break;
17050 +               }
17051 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17052 +                                                     param->string);
17053 +               break;
17054 +#if 0
17055 +       case Opt_trunc_xino_v:
17056 +               if (!a->sb) {
17057 +                       err = 0;
17058 +                       a->skipped = 1;
17059 +                       break;
17060 +               }
17061 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17062 +                                                     param->string);
17063 +               break;
17064 +#endif
17065 +       case Opt_itrunc_xino:
17066 +               if (!a->sb) {
17067 +                       errorfc(fc, "out of bounds %s", param->string);
17068 +                       break;
17069 +               }
17070 +               err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17071 +                                                result.int_32);
17072 +               break;
17073 +
17074 +       case Opt_dirwh:
17075 +               err = 0;
17076 +               opt->dirwh = result.int_32;
17077 +               break;
17078 +
17079 +       case Opt_rdcache:
17080 +               if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17081 +                       errorfc(fc, "rdcache must be smaller than %d",
17082 +                               AUFS_RDCACHE_MAX);
17083 +                       break;
17084 +               }
17085 +               err = 0;
17086 +               opt->rdcache = result.int_32;
17087 +               break;
17088 +
17089 +       case Opt_rdblk:
17090 +               err = 0;
17091 +               opt->rdblk = AUFS_RDBLK_DEF;
17092 +               if (!strcmp(param->string, "def"))
17093 +                       break;
17094 +
17095 +               err = kstrtoint(param->string, 0, &result.int_32);
17096 +               if (unlikely(err)) {
17097 +                       errorfc(fc, "bad value in %s", param->key);
17098 +                       break;
17099 +               }
17100 +               err = -EINVAL;
17101 +               if (unlikely(result.int_32 < 0
17102 +                            || result.int_32 > KMALLOC_MAX_SIZE)) {
17103 +                       errorfc(fc, "bad value in %s", param->key);
17104 +                       break;
17105 +               }
17106 +               if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17107 +                       errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17108 +                       break;
17109 +               }
17110 +               err = 0;
17111 +               opt->rdblk = result.int_32;
17112 +               break;
17113 +
17114 +       case Opt_rdhash:
17115 +               err = 0;
17116 +               opt->rdhash = AUFS_RDHASH_DEF;
17117 +               if (!strcmp(param->string, "def"))
17118 +                       break;
17119 +
17120 +               err = kstrtoint(param->string, 0, &result.int_32);
17121 +               if (unlikely(err)) {
17122 +                       errorfc(fc, "bad value in %s", param->key);
17123 +                       break;
17124 +               }
17125 +               /* how about zero? */
17126 +               if (result.int_32 < 0
17127 +                   || result.int_32 * sizeof(struct hlist_head)
17128 +                   > KMALLOC_MAX_SIZE) {
17129 +                       err = -EINVAL;
17130 +                       errorfc(fc, "bad integer in %s", param->key);
17131 +                       break;
17132 +               }
17133 +               opt->rdhash = result.int_32;
17134 +               break;
17135 +
17136 +       case Opt_diropq:
17137 +               /*
17138 +                * As other options, fs/aufs/opts.c can handle these strings by
17139 +                * match_token().  But "diropq=" is deprecated now and will
17140 +                * never have other value.  So simple strcmp() is enough here.
17141 +                */
17142 +               if (!strcmp(param->string, "a") ||
17143 +                   !strcmp(param->string, "always")) {
17144 +                       err = 0;
17145 +                       opt->type = Opt_diropq_a;
17146 +               } else if (!strcmp(param->string, "w") ||
17147 +                          !strcmp(param->string, "whiteouted")) {
17148 +                       err = 0;
17149 +                       opt->type = Opt_diropq_w;
17150 +               } else
17151 +                       errorfc(fc, "unknown value %s", param->string);
17152 +               break;
17153 +
17154 +       case Opt_udba:
17155 +               opt->udba = au_udba_val(param->string);
17156 +               if (opt->udba >= 0)
17157 +                       err = 0;
17158 +               else
17159 +                       errorf(fc, "wrong value, %s", param->string);
17160 +               break;
17161 +
17162 +       case Opt_wbr_create:
17163 +               opt->wbr_create.wbr_create
17164 +                       = au_wbr_create_val(param->string, &opt->wbr_create);
17165 +               if (opt->wbr_create.wbr_create >= 0)
17166 +                       err = 0;
17167 +               else
17168 +                       errorf(fc, "wrong value, %s", param->key);
17169 +               break;
17170 +
17171 +       case Opt_wbr_copyup:
17172 +               opt->wbr_copyup = au_wbr_copyup_val(param->string);
17173 +               if (opt->wbr_copyup >= 0)
17174 +                       err = 0;
17175 +               else
17176 +                       errorfc(fc, "wrong value, %s", param->key);
17177 +               break;
17178 +
17179 +       case Opt_fhsm_sec:
17180 +               if (unlikely(result.int_32 < 0)) {
17181 +                       errorfc(fc, "bad integer in %s\n", param->key);
17182 +                       break;
17183 +               }
17184 +               err = 0;
17185 +               if (sysaufs_brs)
17186 +                       opt->fhsm_second = result.int_32;
17187 +               else
17188 +                       warnfc(fc, "ignored %s %s", param->key, param->string);
17189 +               break;
17190 +
17191 +       /* simple true/false flag */
17192 +#define au_fsctx_TF(name)                              \
17193 +               case Opt_##name:                        \
17194 +                       err = 0;                        \
17195 +                       opt->tf = !result.negated;      \
17196 +                       break
17197 +       au_fsctx_TF(trunc_xino);
17198 +       au_fsctx_TF(trunc_xib);
17199 +       au_fsctx_TF(dirperm1);
17200 +       au_fsctx_TF(plink);
17201 +       au_fsctx_TF(shwh);
17202 +       au_fsctx_TF(dio);
17203 +       au_fsctx_TF(warn_perm);
17204 +       au_fsctx_TF(verbose);
17205 +       au_fsctx_TF(sum);
17206 +       au_fsctx_TF(dirren);
17207 +       au_fsctx_TF(acl);
17208 +#undef au_fsctx_TF
17209 +
17210 +       case Opt_noverbose:
17211 +               err = 0;
17212 +               opt->type = Opt_verbose;
17213 +               opt->tf = false;
17214 +               break;
17215 +
17216 +       case Opt_noxino:
17217 +               fallthrough;
17218 +       case Opt_list_plink:
17219 +               fallthrough;
17220 +       case Opt_wsum:
17221 +               err = 0;
17222 +               break;
17223 +
17224 +       case Opt_ignore:
17225 +               warnfc(fc, "ignored %s", param->key);
17226 +               fallthrough;
17227 +       case Opt_ignore_silent:
17228 +               a->skipped = 1;
17229 +               err = 0;
17230 +               break;
17231 +       default:
17232 +               a->skipped = 1;
17233 +               err = -ENOPARAM;
17234 +               break;
17235 +       }
17236 +       if (unlikely(err))
17237 +               goto out;
17238 +       if (a->skipped)
17239 +               goto out;
17240 +
17241 +       switch (token) {
17242 +       case Opt_br:
17243 +               fallthrough;
17244 +       case Opt_noverbose:
17245 +               fallthrough;
17246 +       case Opt_diropq:
17247 +               break;
17248 +       default:
17249 +               opt->type = token;
17250 +               break;
17251 +       }
17252 +       opt++;
17253 +       if (unlikely(opt > a->opt_tail)) {
17254 +               err = -E2BIG;
17255 +               opt--;
17256 +       }
17257 +       opt->type = Opt_tail;
17258 +       a->opt = opt;
17259 +
17260 +out:
17261 +       return err;
17262 +}
17263 +
17264 +/*
17265 + * these options accept both 'name=val' and 'name:val' form.
17266 + * some accept optional '=' in its value.
17267 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17268 + */
17269 +static inline unsigned int is_colonopt(char *str)
17270 +{
17271 +#define do_test(name)                                  \
17272 +       if (!strncmp(str, name ":", sizeof(name)))      \
17273 +               return sizeof(name) - 1
17274 +       do_test("br");
17275 +       do_test("add");
17276 +       do_test("ins");
17277 +       do_test("append");
17278 +       do_test("prepend");
17279 +       do_test("del");
17280 +       /* do_test("idel"); */
17281 +       do_test("mod");
17282 +       /* do_test("imod"); */
17283 +#undef do_test
17284 +
17285 +       return 0;
17286 +}
17287 +
17288 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17289 +{
17290 +       int err;
17291 +       unsigned int u;
17292 +       char *str;
17293 +       struct au_fsctx_opts *a = fc->fs_private;
17294 +
17295 +       str = data;
17296 +       AuDbg("str %s\n", str);
17297 +       while (str) {
17298 +               u = is_colonopt(str);
17299 +               if (u)
17300 +                       str[u] = '=';
17301 +               str = strchr(str, ',');
17302 +               if (!str)
17303 +                       break;
17304 +               str++;
17305 +       }
17306 +       str = data;
17307 +       AuDbg("str %s\n", str);
17308 +
17309 +       err = generic_parse_monolithic(fc, str);
17310 +       AuTraceErr(err);
17311 +       au_fsctx_dump(&a->opts);
17312 +
17313 +       return err;
17314 +}
17315 +
17316 +/* ---------------------------------------------------------------------- */
17317 +
17318 +static void au_fsctx_opts_free(struct au_opts *opts)
17319 +{
17320 +       struct au_opt *opt;
17321 +
17322 +       opt = opts->opt;
17323 +       while (opt->type != Opt_tail) {
17324 +               switch (opt->type) {
17325 +               case Opt_add:
17326 +                       fallthrough;
17327 +               case Opt_append:
17328 +                       fallthrough;
17329 +               case Opt_prepend:
17330 +                       kfree(opt->add.pathname);
17331 +                       path_put(&opt->add.path);
17332 +                       break;
17333 +               case Opt_del:
17334 +                       kfree(opt->del.pathname);
17335 +                       fallthrough;
17336 +               case Opt_idel:
17337 +                       path_put(&opt->del.h_path);
17338 +                       break;
17339 +               case Opt_mod:
17340 +                       kfree(opt->mod.path);
17341 +                       fallthrough;
17342 +               case Opt_imod:
17343 +                       dput(opt->mod.h_root);
17344 +                       break;
17345 +               case Opt_xino:
17346 +                       kfree(opt->xino.path);
17347 +                       fput(opt->xino.file);
17348 +                       break;
17349 +               }
17350 +               opt++;
17351 +       }
17352 +}
17353 +
17354 +static void au_fsctx_free(struct fs_context *fc)
17355 +{
17356 +       struct au_fsctx_opts *a = fc->fs_private;
17357 +
17358 +       /* fs_type=%p, root=%pD */
17359 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17360 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17361 +
17362 +       kobject_put(&a->sbinfo->si_kobj);
17363 +       au_fsctx_opts_free(&a->opts);
17364 +       free_page((unsigned long)a->opts.opt);
17365 +       au_kfree_rcu(a);
17366 +}
17367 +
17368 +static const struct fs_context_operations au_fsctx_ops = {
17369 +       .free                   = au_fsctx_free,
17370 +       .parse_param            = au_fsctx_parse_param,
17371 +       .parse_monolithic       = au_fsctx_parse_monolithic,
17372 +       .get_tree               = au_fsctx_get_tree,
17373 +       .reconfigure            = au_fsctx_reconfigure
17374 +       /*
17375 +        * nfs4 requires ->dup()? No.
17376 +        * I don't know what is this ->dup() for.
17377 +        */
17378 +};
17379 +
17380 +int aufs_fsctx_init(struct fs_context *fc)
17381 +{
17382 +       int err;
17383 +       struct au_fsctx_opts *a;
17384 +
17385 +       /* fs_type=%p, root=%pD */
17386 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17387 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17388 +
17389 +       /* they will be freed by au_fsctx_free() */
17390 +       err = -ENOMEM;
17391 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17392 +       if (unlikely(!a))
17393 +               goto out;
17394 +       a->bindex = 0;
17395 +       a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17396 +       if (unlikely(!a->opts.opt))
17397 +               goto out_a;
17398 +       a->opt = a->opts.opt;
17399 +       a->opt->type = Opt_tail;
17400 +       a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17401 +       a->opt_tail = a->opt + a->opts.max_opt - 1;
17402 +       a->opts.sb_flags = fc->sb_flags;
17403 +
17404 +       a->sb = NULL;
17405 +       if (fc->root) {
17406 +               AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17407 +               a->opts.flags = AuOpts_REMOUNT;
17408 +               a->sb = fc->root->d_sb;
17409 +               a->sbinfo = au_sbi(a->sb);
17410 +               kobject_get(&a->sbinfo->si_kobj);
17411 +       } else {
17412 +               a->sbinfo = au_si_alloc(a->sb);
17413 +               AuDebugOn(!a->sbinfo);
17414 +               err = PTR_ERR(a->sbinfo);
17415 +               if (IS_ERR(a->sbinfo))
17416 +                       goto out_opt;
17417 +               au_rw_write_unlock(&a->sbinfo->si_rwsem);
17418 +       }
17419 +
17420 +       err = 0;
17421 +       fc->fs_private = a;
17422 +       fc->ops = &au_fsctx_ops;
17423 +       goto out; /* success */
17424 +
17425 +out_opt:
17426 +       free_page((unsigned long)a->opts.opt);
17427 +out_a:
17428 +       au_kfree_rcu(a);
17429 +out:
17430 +       AuTraceErr(err);
17431 +       return err;
17432 +}
17433 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
17434 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
17435 +++ linux/fs/aufs/fstype.h      2022-11-05 23:02:18.965889284 +0100
17436 @@ -0,0 +1,401 @@
17437 +/* SPDX-License-Identifier: GPL-2.0 */
17438 +/*
17439 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17440 + *
17441 + * This program is free software; you can redistribute it and/or modify
17442 + * it under the terms of the GNU General Public License as published by
17443 + * the Free Software Foundation; either version 2 of the License, or
17444 + * (at your option) any later version.
17445 + *
17446 + * This program is distributed in the hope that it will be useful,
17447 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17448 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17449 + * GNU General Public License for more details.
17450 + *
17451 + * You should have received a copy of the GNU General Public License
17452 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17453 + */
17454 +
17455 +/*
17456 + * judging filesystem type
17457 + */
17458 +
17459 +#ifndef __AUFS_FSTYPE_H__
17460 +#define __AUFS_FSTYPE_H__
17461 +
17462 +#ifdef __KERNEL__
17463 +
17464 +#include <linux/fs.h>
17465 +#include <linux/magic.h>
17466 +#include <linux/nfs_fs.h>
17467 +#include <linux/romfs_fs.h>
17468 +
17469 +static inline int au_test_aufs(struct super_block *sb)
17470 +{
17471 +       return sb->s_magic == AUFS_SUPER_MAGIC;
17472 +}
17473 +
17474 +static inline const char *au_sbtype(struct super_block *sb)
17475 +{
17476 +       return sb->s_type->name;
17477 +}
17478 +
17479 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17480 +{
17481 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17482 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
17483 +#else
17484 +       return 0;
17485 +#endif
17486 +}
17487 +
17488 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17489 +{
17490 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17491 +       return sb->s_magic == ROMFS_MAGIC;
17492 +#else
17493 +       return 0;
17494 +#endif
17495 +}
17496 +
17497 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17498 +{
17499 +#if IS_ENABLED(CONFIG_CRAMFS)
17500 +       return sb->s_magic == CRAMFS_MAGIC;
17501 +#endif
17502 +       return 0;
17503 +}
17504 +
17505 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17506 +{
17507 +#if IS_ENABLED(CONFIG_NFS_FS)
17508 +       return sb->s_magic == NFS_SUPER_MAGIC;
17509 +#else
17510 +       return 0;
17511 +#endif
17512 +}
17513 +
17514 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17515 +{
17516 +#if IS_ENABLED(CONFIG_FUSE_FS)
17517 +       return sb->s_magic == FUSE_SUPER_MAGIC;
17518 +#else
17519 +       return 0;
17520 +#endif
17521 +}
17522 +
17523 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17524 +{
17525 +#if IS_ENABLED(CONFIG_XFS_FS)
17526 +       return sb->s_magic == XFS_SB_MAGIC;
17527 +#else
17528 +       return 0;
17529 +#endif
17530 +}
17531 +
17532 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17533 +{
17534 +#ifdef CONFIG_TMPFS
17535 +       return sb->s_magic == TMPFS_MAGIC;
17536 +#else
17537 +       return 0;
17538 +#endif
17539 +}
17540 +
17541 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17542 +{
17543 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17544 +       return !strcmp(au_sbtype(sb), "ecryptfs");
17545 +#else
17546 +       return 0;
17547 +#endif
17548 +}
17549 +
17550 +static inline int au_test_ramfs(struct super_block *sb)
17551 +{
17552 +       return sb->s_magic == RAMFS_MAGIC;
17553 +}
17554 +
17555 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17556 +{
17557 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17558 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
17559 +#else
17560 +       return 0;
17561 +#endif
17562 +}
17563 +
17564 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17565 +{
17566 +#ifdef CONFIG_PROC_FS
17567 +       return sb->s_magic == PROC_SUPER_MAGIC;
17568 +#else
17569 +       return 0;
17570 +#endif
17571 +}
17572 +
17573 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17574 +{
17575 +#ifdef CONFIG_SYSFS
17576 +       return sb->s_magic == SYSFS_MAGIC;
17577 +#else
17578 +       return 0;
17579 +#endif
17580 +}
17581 +
17582 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17583 +{
17584 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17585 +       return sb->s_magic == CONFIGFS_MAGIC;
17586 +#else
17587 +       return 0;
17588 +#endif
17589 +}
17590 +
17591 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17592 +{
17593 +#if IS_ENABLED(CONFIG_MINIX_FS)
17594 +       return sb->s_magic == MINIX3_SUPER_MAGIC
17595 +               || sb->s_magic == MINIX2_SUPER_MAGIC
17596 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
17597 +               || sb->s_magic == MINIX_SUPER_MAGIC
17598 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
17599 +#else
17600 +       return 0;
17601 +#endif
17602 +}
17603 +
17604 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17605 +{
17606 +#if IS_ENABLED(CONFIG_FAT_FS)
17607 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
17608 +#else
17609 +       return 0;
17610 +#endif
17611 +}
17612 +
17613 +static inline int au_test_msdos(struct super_block *sb)
17614 +{
17615 +       return au_test_fat(sb);
17616 +}
17617 +
17618 +static inline int au_test_vfat(struct super_block *sb)
17619 +{
17620 +       return au_test_fat(sb);
17621 +}
17622 +
17623 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17624 +{
17625 +#ifdef CONFIG_SECURITYFS
17626 +       return sb->s_magic == SECURITYFS_MAGIC;
17627 +#else
17628 +       return 0;
17629 +#endif
17630 +}
17631 +
17632 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17633 +{
17634 +#if IS_ENABLED(CONFIG_SQUASHFS)
17635 +       return sb->s_magic == SQUASHFS_MAGIC;
17636 +#else
17637 +       return 0;
17638 +#endif
17639 +}
17640 +
17641 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17642 +{
17643 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17644 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
17645 +#else
17646 +       return 0;
17647 +#endif
17648 +}
17649 +
17650 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17651 +{
17652 +#if IS_ENABLED(CONFIG_XENFS)
17653 +       return sb->s_magic == XENFS_SUPER_MAGIC;
17654 +#else
17655 +       return 0;
17656 +#endif
17657 +}
17658 +
17659 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17660 +{
17661 +#ifdef CONFIG_DEBUG_FS
17662 +       return sb->s_magic == DEBUGFS_MAGIC;
17663 +#else
17664 +       return 0;
17665 +#endif
17666 +}
17667 +
17668 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17669 +{
17670 +#if IS_ENABLED(CONFIG_NILFS)
17671 +       return sb->s_magic == NILFS_SUPER_MAGIC;
17672 +#else
17673 +       return 0;
17674 +#endif
17675 +}
17676 +
17677 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17678 +{
17679 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17680 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17681 +#else
17682 +       return 0;
17683 +#endif
17684 +}
17685 +
17686 +/* ---------------------------------------------------------------------- */
17687 +/*
17688 + * they can't be an aufs branch.
17689 + */
17690 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17691 +{
17692 +       return
17693 +#ifndef CONFIG_AUFS_BR_RAMFS
17694 +               au_test_ramfs(sb) ||
17695 +#endif
17696 +               au_test_procfs(sb)
17697 +               || au_test_sysfs(sb)
17698 +               || au_test_configfs(sb)
17699 +               || au_test_debugfs(sb)
17700 +               || au_test_securityfs(sb)
17701 +               || au_test_xenfs(sb)
17702 +               || au_test_ecryptfs(sb)
17703 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
17704 +               || au_test_aufs(sb); /* will be supported in next version */
17705 +}
17706 +
17707 +static inline int au_test_fs_remote(struct super_block *sb)
17708 +{
17709 +       return !au_test_tmpfs(sb)
17710 +#ifdef CONFIG_AUFS_BR_RAMFS
17711 +               && !au_test_ramfs(sb)
17712 +#endif
17713 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17714 +}
17715 +
17716 +/* ---------------------------------------------------------------------- */
17717 +
17718 +/*
17719 + * Note: these functions (below) are created after reading ->getattr() in all
17720 + * filesystems under linux/fs. it means we have to do so in every update...
17721 + */
17722 +
17723 +/*
17724 + * some filesystems require getattr to refresh the inode attributes before
17725 + * referencing.
17726 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17727 + * and leave the work for d_revalidate()
17728 + */
17729 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17730 +{
17731 +       return au_test_nfs(sb)
17732 +               || au_test_fuse(sb)
17733 +               /* || au_test_btrfs(sb) */      /* untested */
17734 +               ;
17735 +}
17736 +
17737 +/*
17738 + * filesystems which don't maintain i_size or i_blocks.
17739 + */
17740 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17741 +{
17742 +       return au_test_xfs(sb)
17743 +               || au_test_btrfs(sb)
17744 +               || au_test_ubifs(sb)
17745 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
17746 +               /* || au_test_minix(sb) */      /* untested */
17747 +               ;
17748 +}
17749 +
17750 +/*
17751 + * filesystems which don't store the correct value in some of their inode
17752 + * attributes.
17753 + */
17754 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17755 +{
17756 +       return au_test_fs_bad_iattr_size(sb)
17757 +               || au_test_fat(sb)
17758 +               || au_test_msdos(sb)
17759 +               || au_test_vfat(sb);
17760 +}
17761 +
17762 +/* they don't check i_nlink in link(2) */
17763 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17764 +{
17765 +       return au_test_tmpfs(sb)
17766 +#ifdef CONFIG_AUFS_BR_RAMFS
17767 +               || au_test_ramfs(sb)
17768 +#endif
17769 +               || au_test_ubifs(sb)
17770 +               || au_test_hfsplus(sb);
17771 +}
17772 +
17773 +/*
17774 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17775 + */
17776 +static inline int au_test_fs_notime(struct super_block *sb)
17777 +{
17778 +       return au_test_nfs(sb)
17779 +               || au_test_fuse(sb)
17780 +               || au_test_ubifs(sb)
17781 +               ;
17782 +}
17783 +
17784 +/* temporary support for i#1 in cramfs */
17785 +static inline int au_test_fs_unique_ino(struct inode *inode)
17786 +{
17787 +       if (au_test_cramfs(inode->i_sb))
17788 +               return inode->i_ino != 1;
17789 +       return 1;
17790 +}
17791 +
17792 +/* ---------------------------------------------------------------------- */
17793 +
17794 +/*
17795 + * the filesystem where the xino files placed must support i/o after unlink and
17796 + * maintain i_size and i_blocks.
17797 + */
17798 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17799 +{
17800 +       return au_test_fs_remote(sb)
17801 +               || au_test_fs_bad_iattr_size(sb)
17802 +               /* don't want unnecessary work for xino */
17803 +               || au_test_aufs(sb)
17804 +               || au_test_ecryptfs(sb)
17805 +               || au_test_nilfs(sb);
17806 +}
17807 +
17808 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17809 +{
17810 +       return au_test_tmpfs(sb)
17811 +               || au_test_ramfs(sb);
17812 +}
17813 +
17814 +/*
17815 + * test if the @sb is real-readonly.
17816 + */
17817 +static inline int au_test_fs_rr(struct super_block *sb)
17818 +{
17819 +       return au_test_squashfs(sb)
17820 +               || au_test_iso9660(sb)
17821 +               || au_test_cramfs(sb)
17822 +               || au_test_romfs(sb);
17823 +}
17824 +
17825 +/*
17826 + * test if the @inode is nfs with 'noacl' option
17827 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17828 + */
17829 +static inline int au_test_nfs_noacl(struct inode *inode)
17830 +{
17831 +       return au_test_nfs(inode->i_sb)
17832 +               /* && IS_POSIXACL(inode) */
17833 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
17834 +}
17835 +
17836 +#endif /* __KERNEL__ */
17837 +#endif /* __AUFS_FSTYPE_H__ */
17838 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
17839 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
17840 +++ linux/fs/aufs/hbl.h 2022-11-05 23:02:18.965889284 +0100
17841 @@ -0,0 +1,65 @@
17842 +/* SPDX-License-Identifier: GPL-2.0 */
17843 +/*
17844 + * Copyright (C) 2017-2022 Junjiro R. Okajima
17845 + *
17846 + * This program is free software; you can redistribute it and/or modify
17847 + * it under the terms of the GNU General Public License as published by
17848 + * the Free Software Foundation; either version 2 of the License, or
17849 + * (at your option) any later version.
17850 + *
17851 + * This program is distributed in the hope that it will be useful,
17852 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17853 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17854 + * GNU General Public License for more details.
17855 + *
17856 + * You should have received a copy of the GNU General Public License
17857 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17858 + */
17859 +
17860 +/*
17861 + * helpers for hlist_bl.h
17862 + */
17863 +
17864 +#ifndef __AUFS_HBL_H__
17865 +#define __AUFS_HBL_H__
17866 +
17867 +#ifdef __KERNEL__
17868 +
17869 +#include <linux/list_bl.h>
17870 +
17871 +static inline void au_hbl_add(struct hlist_bl_node *node,
17872 +                             struct hlist_bl_head *hbl)
17873 +{
17874 +       hlist_bl_lock(hbl);
17875 +       hlist_bl_add_head(node, hbl);
17876 +       hlist_bl_unlock(hbl);
17877 +}
17878 +
17879 +static inline void au_hbl_del(struct hlist_bl_node *node,
17880 +                             struct hlist_bl_head *hbl)
17881 +{
17882 +       hlist_bl_lock(hbl);
17883 +       hlist_bl_del(node);
17884 +       hlist_bl_unlock(hbl);
17885 +}
17886 +
17887 +#define au_hbl_for_each(pos, head)                                     \
17888 +       for (pos = hlist_bl_first(head);                                \
17889 +            pos;                                                       \
17890 +            pos = pos->next)
17891 +
17892 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17893 +{
17894 +       unsigned long cnt;
17895 +       struct hlist_bl_node *pos;
17896 +
17897 +       cnt = 0;
17898 +       hlist_bl_lock(hbl);
17899 +       au_hbl_for_each(pos, hbl)
17900 +               cnt++;
17901 +       hlist_bl_unlock(hbl);
17902 +       return cnt;
17903 +}
17904 +
17905 +#endif /* __KERNEL__ */
17906 +#endif /* __AUFS_HBL_H__ */
17907 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
17908 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
17909 +++ linux/fs/aufs/hfsnotify.c   2022-11-05 23:02:18.965889284 +0100
17910 @@ -0,0 +1,290 @@
17911 +// SPDX-License-Identifier: GPL-2.0
17912 +/*
17913 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17914 + *
17915 + * This program is free software; you can redistribute it and/or modify
17916 + * it under the terms of the GNU General Public License as published by
17917 + * the Free Software Foundation; either version 2 of the License, or
17918 + * (at your option) any later version.
17919 + *
17920 + * This program is distributed in the hope that it will be useful,
17921 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17922 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17923 + * GNU General Public License for more details.
17924 + *
17925 + * You should have received a copy of the GNU General Public License
17926 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17927 + */
17928 +
17929 +/*
17930 + * fsnotify for the lower directories
17931 + */
17932 +
17933 +#include "aufs.h"
17934 +
17935 +/* FS_IN_IGNORED is unnecessary */
17936 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17937 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
17938 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17939 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17940 +
17941 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17942 +{
17943 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17944 +                                            hn_mark);
17945 +       /* AuDbg("here\n"); */
17946 +       au_cache_free_hnotify(hn);
17947 +       smp_mb__before_atomic(); /* for atomic64_dec */
17948 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
17949 +               wake_up(&au_hfsn_wq);
17950 +}
17951 +
17952 +static int au_hfsn_alloc(struct au_hinode *hinode)
17953 +{
17954 +       int err;
17955 +       struct au_hnotify *hn;
17956 +       struct super_block *sb;
17957 +       struct au_branch *br;
17958 +       struct fsnotify_mark *mark;
17959 +       aufs_bindex_t bindex;
17960 +
17961 +       hn = hinode->hi_notify;
17962 +       sb = hn->hn_aufs_inode->i_sb;
17963 +       bindex = au_br_index(sb, hinode->hi_id);
17964 +       br = au_sbr(sb, bindex);
17965 +       AuDebugOn(!br->br_hfsn);
17966 +
17967 +       mark = &hn->hn_mark;
17968 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17969 +       mark->mask = AuHfsnMask;
17970 +       /*
17971 +        * by udba rename or rmdir, aufs assign a new inode to the known
17972 +        * h_inode, so specify 1 to allow dups.
17973 +        */
17974 +       lockdep_off();
17975 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
17976 +       lockdep_on();
17977 +
17978 +       return err;
17979 +}
17980 +
17981 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
17982 +{
17983 +       struct fsnotify_mark *mark;
17984 +       unsigned long long ull;
17985 +       struct fsnotify_group *group;
17986 +
17987 +       ull = atomic64_inc_return(&au_hfsn_ifree);
17988 +       BUG_ON(!ull);
17989 +
17990 +       mark = &hn->hn_mark;
17991 +       spin_lock(&mark->lock);
17992 +       group = mark->group;
17993 +       fsnotify_get_group(group);
17994 +       spin_unlock(&mark->lock);
17995 +       lockdep_off();
17996 +       fsnotify_destroy_mark(mark, group);
17997 +       fsnotify_put_mark(mark);
17998 +       fsnotify_put_group(group);
17999 +       lockdep_on();
18000 +
18001 +       /* free hn by myself */
18002 +       return 0;
18003 +}
18004 +
18005 +/* ---------------------------------------------------------------------- */
18006 +
18007 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
18008 +{
18009 +       struct fsnotify_mark *mark;
18010 +
18011 +       mark = &hinode->hi_notify->hn_mark;
18012 +       spin_lock(&mark->lock);
18013 +       if (do_set) {
18014 +               AuDebugOn(mark->mask & AuHfsnMask);
18015 +               mark->mask |= AuHfsnMask;
18016 +       } else {
18017 +               AuDebugOn(!(mark->mask & AuHfsnMask));
18018 +               mark->mask &= ~AuHfsnMask;
18019 +       }
18020 +       spin_unlock(&mark->lock);
18021 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
18022 +}
18023 +
18024 +/* ---------------------------------------------------------------------- */
18025 +
18026 +/* #define AuDbgHnotify */
18027 +#ifdef AuDbgHnotify
18028 +static char *au_hfsn_name(u32 mask)
18029 +{
18030 +#ifdef CONFIG_AUFS_DEBUG
18031 +#define test_ret(flag)                         \
18032 +       do {                                    \
18033 +               if (mask & flag)                \
18034 +                       return #flag;           \
18035 +       } while (0)
18036 +       test_ret(FS_ACCESS);
18037 +       test_ret(FS_MODIFY);
18038 +       test_ret(FS_ATTRIB);
18039 +       test_ret(FS_CLOSE_WRITE);
18040 +       test_ret(FS_CLOSE_NOWRITE);
18041 +       test_ret(FS_OPEN);
18042 +       test_ret(FS_MOVED_FROM);
18043 +       test_ret(FS_MOVED_TO);
18044 +       test_ret(FS_CREATE);
18045 +       test_ret(FS_DELETE);
18046 +       test_ret(FS_DELETE_SELF);
18047 +       test_ret(FS_MOVE_SELF);
18048 +       test_ret(FS_UNMOUNT);
18049 +       test_ret(FS_Q_OVERFLOW);
18050 +       test_ret(FS_IN_IGNORED);
18051 +       test_ret(FS_ISDIR);
18052 +       test_ret(FS_IN_ONESHOT);
18053 +       test_ret(FS_EVENT_ON_CHILD);
18054 +       return "";
18055 +#undef test_ret
18056 +#else
18057 +       return "??";
18058 +#endif
18059 +}
18060 +#endif
18061 +
18062 +/* ---------------------------------------------------------------------- */
18063 +
18064 +static void au_hfsn_free_group(struct fsnotify_group *group)
18065 +{
18066 +       struct au_br_hfsnotify *hfsn = group->private;
18067 +
18068 +       /* AuDbg("here\n"); */
18069 +       au_kfree_try_rcu(hfsn);
18070 +}
18071 +
18072 +static int au_hfsn_handle_event(struct fsnotify_group *group,
18073 +                               u32 mask, const void *data, int data_type,
18074 +                               struct inode *dir,
18075 +                               const struct qstr *file_name, u32 cookie,
18076 +                               struct fsnotify_iter_info *iter_info)
18077 +{
18078 +       int err;
18079 +       struct au_hnotify *hnotify;
18080 +       struct inode *h_dir, *h_inode;
18081 +       struct fsnotify_mark *inode_mark;
18082 +
18083 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18084 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
18085 +
18086 +       err = 0;
18087 +       /* if FS_UNMOUNT happens, there must be another bug */
18088 +       AuDebugOn(mask & FS_UNMOUNT);
18089 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
18090 +               goto out;
18091 +
18092 +       h_dir = dir;
18093 +       h_inode = NULL;
18094 +#ifdef AuDbgHnotify
18095 +       au_debug_on();
18096 +       if (1 || file_name.len != sizeof(AUFS_XINO_FNAME) - 1
18097 +           || strncmp(file_name.name, AUFS_XINO_FNAME, file_name.len)) {
18098 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18099 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
18100 +                     AuLNPair(file_name), h_inode ? h_inode->i_ino : 0);
18101 +               /* WARN_ON(1); */
18102 +       }
18103 +       au_debug_off();
18104 +#endif
18105 +
18106 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
18107 +       AuDebugOn(!inode_mark);
18108 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
18109 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
18110 +
18111 +out:
18112 +       return err;
18113 +}
18114 +
18115 +static struct fsnotify_ops au_hfsn_ops = {
18116 +       .handle_event           = au_hfsn_handle_event,
18117 +       .free_group_priv        = au_hfsn_free_group,
18118 +       .free_mark              = au_hfsn_free_mark
18119 +};
18120 +
18121 +/* ---------------------------------------------------------------------- */
18122 +
18123 +static void au_hfsn_fin_br(struct au_branch *br)
18124 +{
18125 +       struct au_br_hfsnotify *hfsn;
18126 +
18127 +       hfsn = br->br_hfsn;
18128 +       if (hfsn) {
18129 +               lockdep_off();
18130 +               fsnotify_put_group(hfsn->hfsn_group);
18131 +               lockdep_on();
18132 +       }
18133 +}
18134 +
18135 +static int au_hfsn_init_br(struct au_branch *br, int perm)
18136 +{
18137 +       int err;
18138 +       struct fsnotify_group *group;
18139 +       struct au_br_hfsnotify *hfsn;
18140 +
18141 +       err = 0;
18142 +       br->br_hfsn = NULL;
18143 +       if (!au_br_hnotifyable(perm))
18144 +               goto out;
18145 +
18146 +       err = -ENOMEM;
18147 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18148 +       if (unlikely(!hfsn))
18149 +               goto out;
18150 +
18151 +       err = 0;
18152 +       group = fsnotify_alloc_group(&au_hfsn_ops,
18153 +                                    /*flags - not for userspace*/0);
18154 +       if (IS_ERR(group)) {
18155 +               err = PTR_ERR(group);
18156 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
18157 +               goto out_hfsn;
18158 +       }
18159 +
18160 +       group->private = hfsn;
18161 +       hfsn->hfsn_group = group;
18162 +       br->br_hfsn = hfsn;
18163 +       goto out; /* success */
18164 +
18165 +out_hfsn:
18166 +       au_kfree_try_rcu(hfsn);
18167 +out:
18168 +       return err;
18169 +}
18170 +
18171 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18172 +{
18173 +       int err;
18174 +
18175 +       err = 0;
18176 +       if (!br->br_hfsn)
18177 +               err = au_hfsn_init_br(br, perm);
18178 +
18179 +       return err;
18180 +}
18181 +
18182 +/* ---------------------------------------------------------------------- */
18183 +
18184 +static void au_hfsn_fin(void)
18185 +{
18186 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18187 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18188 +}
18189 +
18190 +const struct au_hnotify_op au_hnotify_op = {
18191 +       .ctl            = au_hfsn_ctl,
18192 +       .alloc          = au_hfsn_alloc,
18193 +       .free           = au_hfsn_free,
18194 +
18195 +       .fin            = au_hfsn_fin,
18196 +
18197 +       .reset_br       = au_hfsn_reset_br,
18198 +       .fin_br         = au_hfsn_fin_br,
18199 +       .init_br        = au_hfsn_init_br
18200 +};
18201 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
18202 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
18203 +++ linux/fs/aufs/hfsplus.c     2022-11-05 23:02:18.965889284 +0100
18204 @@ -0,0 +1,60 @@
18205 +// SPDX-License-Identifier: GPL-2.0
18206 +/*
18207 + * Copyright (C) 2010-2022 Junjiro R. Okajima
18208 + *
18209 + * This program is free software; you can redistribute it and/or modify
18210 + * it under the terms of the GNU General Public License as published by
18211 + * the Free Software Foundation; either version 2 of the License, or
18212 + * (at your option) any later version.
18213 + *
18214 + * This program is distributed in the hope that it will be useful,
18215 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18216 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18217 + * GNU General Public License for more details.
18218 + *
18219 + * You should have received a copy of the GNU General Public License
18220 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18221 + */
18222 +
18223 +/*
18224 + * special support for filesystems which acquires an inode mutex
18225 + * at final closing a file, eg, hfsplus.
18226 + *
18227 + * This trick is very simple and stupid, just to open the file before really
18228 + * necessary open to tell hfsplus that this is not the final closing.
18229 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
18230 + * and au_h_open_post() after releasing it.
18231 + */
18232 +
18233 +#include "aufs.h"
18234 +
18235 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18236 +                          int force_wr)
18237 +{
18238 +       struct file *h_file;
18239 +       struct dentry *h_dentry;
18240 +
18241 +       h_dentry = au_h_dptr(dentry, bindex);
18242 +       AuDebugOn(!h_dentry);
18243 +       AuDebugOn(d_is_negative(h_dentry));
18244 +
18245 +       h_file = NULL;
18246 +       if (au_test_hfsplus(h_dentry->d_sb)
18247 +           && d_is_reg(h_dentry))
18248 +               h_file = au_h_open(dentry, bindex,
18249 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
18250 +                                  /*file*/NULL, force_wr);
18251 +       return h_file;
18252 +}
18253 +
18254 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18255 +                   struct file *h_file)
18256 +{
18257 +       struct au_branch *br;
18258 +
18259 +       if (h_file) {
18260 +               fput(h_file);
18261 +               br = au_sbr(dentry->d_sb, bindex);
18262 +               au_lcnt_dec(&br->br_nfiles);
18263 +       }
18264 +}
18265 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
18266 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
18267 +++ linux/fs/aufs/hnotify.c     2022-11-05 23:02:18.965889284 +0100
18268 @@ -0,0 +1,715 @@
18269 +// SPDX-License-Identifier: GPL-2.0
18270 +/*
18271 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18272 + *
18273 + * This program is free software; you can redistribute it and/or modify
18274 + * it under the terms of the GNU General Public License as published by
18275 + * the Free Software Foundation; either version 2 of the License, or
18276 + * (at your option) any later version.
18277 + *
18278 + * This program is distributed in the hope that it will be useful,
18279 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18280 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18281 + * GNU General Public License for more details.
18282 + *
18283 + * You should have received a copy of the GNU General Public License
18284 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18285 + */
18286 +
18287 +/*
18288 + * abstraction to notify the direct changes on lower directories
18289 + */
18290 +
18291 +/* #include <linux/iversion.h> */
18292 +#include "aufs.h"
18293 +
18294 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18295 +{
18296 +       int err;
18297 +       struct au_hnotify *hn;
18298 +
18299 +       err = -ENOMEM;
18300 +       hn = au_cache_alloc_hnotify();
18301 +       if (hn) {
18302 +               hn->hn_aufs_inode = inode;
18303 +               hinode->hi_notify = hn;
18304 +               err = au_hnotify_op.alloc(hinode);
18305 +               AuTraceErr(err);
18306 +               if (unlikely(err)) {
18307 +                       hinode->hi_notify = NULL;
18308 +                       au_cache_free_hnotify(hn);
18309 +                       /*
18310 +                        * The upper dir was removed by udba, but the same named
18311 +                        * dir left. In this case, aufs assigns a new inode
18312 +                        * number and set the monitor again.
18313 +                        * For the lower dir, the old monitor is still left.
18314 +                        */
18315 +                       if (err == -EEXIST)
18316 +                               err = 0;
18317 +               }
18318 +       }
18319 +
18320 +       AuTraceErr(err);
18321 +       return err;
18322 +}
18323 +
18324 +void au_hn_free(struct au_hinode *hinode)
18325 +{
18326 +       struct au_hnotify *hn;
18327 +
18328 +       hn = hinode->hi_notify;
18329 +       if (hn) {
18330 +               hinode->hi_notify = NULL;
18331 +               if (au_hnotify_op.free(hinode, hn))
18332 +                       au_cache_free_hnotify(hn);
18333 +       }
18334 +}
18335 +
18336 +/* ---------------------------------------------------------------------- */
18337 +
18338 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18339 +{
18340 +       if (hinode->hi_notify)
18341 +               au_hnotify_op.ctl(hinode, do_set);
18342 +}
18343 +
18344 +void au_hn_reset(struct inode *inode, unsigned int flags)
18345 +{
18346 +       aufs_bindex_t bindex, bbot;
18347 +       struct inode *hi;
18348 +       struct dentry *iwhdentry;
18349 +
18350 +       bbot = au_ibbot(inode);
18351 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18352 +               hi = au_h_iptr(inode, bindex);
18353 +               if (!hi)
18354 +                       continue;
18355 +
18356 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18357 +               iwhdentry = au_hi_wh(inode, bindex);
18358 +               if (iwhdentry)
18359 +                       dget(iwhdentry);
18360 +               au_igrab(hi);
18361 +               au_set_h_iptr(inode, bindex, NULL, 0);
18362 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
18363 +                             flags & ~AuHi_XINO);
18364 +               iput(hi);
18365 +               dput(iwhdentry);
18366 +               /* inode_unlock(hi); */
18367 +       }
18368 +}
18369 +
18370 +/* ---------------------------------------------------------------------- */
18371 +
18372 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18373 +{
18374 +       int err;
18375 +       aufs_bindex_t bindex, bbot, bfound, btop;
18376 +       struct inode *h_i;
18377 +
18378 +       err = 0;
18379 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18380 +               pr_warn("branch root dir was changed\n");
18381 +               goto out;
18382 +       }
18383 +
18384 +       bfound = -1;
18385 +       bbot = au_ibbot(inode);
18386 +       btop = au_ibtop(inode);
18387 +#if 0 /* reserved for future use */
18388 +       if (bindex == bbot) {
18389 +               /* keep this ino in rename case */
18390 +               goto out;
18391 +       }
18392 +#endif
18393 +       for (bindex = btop; bindex <= bbot; bindex++)
18394 +               if (au_h_iptr(inode, bindex) == h_inode) {
18395 +                       bfound = bindex;
18396 +                       break;
18397 +               }
18398 +       if (bfound < 0)
18399 +               goto out;
18400 +
18401 +       for (bindex = btop; bindex <= bbot; bindex++) {
18402 +               h_i = au_h_iptr(inode, bindex);
18403 +               if (!h_i)
18404 +                       continue;
18405 +
18406 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18407 +               /* ignore this error */
18408 +               /* bad action? */
18409 +       }
18410 +
18411 +       /* children inode number will be broken */
18412 +
18413 +out:
18414 +       AuTraceErr(err);
18415 +       return err;
18416 +}
18417 +
18418 +static int hn_gen_tree(struct dentry *dentry)
18419 +{
18420 +       int err, i, j, ndentry;
18421 +       struct au_dcsub_pages dpages;
18422 +       struct au_dpage *dpage;
18423 +       struct dentry **dentries;
18424 +
18425 +       err = au_dpages_init(&dpages, GFP_NOFS);
18426 +       if (unlikely(err))
18427 +               goto out;
18428 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18429 +       if (unlikely(err))
18430 +               goto out_dpages;
18431 +
18432 +       for (i = 0; i < dpages.ndpage; i++) {
18433 +               dpage = dpages.dpages + i;
18434 +               dentries = dpage->dentries;
18435 +               ndentry = dpage->ndentry;
18436 +               for (j = 0; j < ndentry; j++) {
18437 +                       struct dentry *d;
18438 +
18439 +                       d = dentries[j];
18440 +                       if (IS_ROOT(d))
18441 +                               continue;
18442 +
18443 +                       au_digen_dec(d);
18444 +                       if (d_really_is_positive(d))
18445 +                               /* todo: reset children xino?
18446 +                                  cached children only? */
18447 +                               au_iigen_dec(d_inode(d));
18448 +               }
18449 +       }
18450 +
18451 +out_dpages:
18452 +       au_dpages_free(&dpages);
18453 +out:
18454 +       return err;
18455 +}
18456 +
18457 +/*
18458 + * return 0 if processed.
18459 + */
18460 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18461 +                          const unsigned int isdir)
18462 +{
18463 +       int err;
18464 +       struct dentry *d;
18465 +       struct qstr *dname;
18466 +
18467 +       err = 1;
18468 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18469 +               pr_warn("branch root dir was changed\n");
18470 +               err = 0;
18471 +               goto out;
18472 +       }
18473 +
18474 +       if (!isdir) {
18475 +               AuDebugOn(!name);
18476 +               au_iigen_dec(inode);
18477 +               spin_lock(&inode->i_lock);
18478 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18479 +                       spin_lock(&d->d_lock);
18480 +                       dname = &d->d_name;
18481 +                       if (dname->len != nlen
18482 +                           && memcmp(dname->name, name, nlen)) {
18483 +                               spin_unlock(&d->d_lock);
18484 +                               continue;
18485 +                       }
18486 +                       err = 0;
18487 +                       au_digen_dec(d);
18488 +                       spin_unlock(&d->d_lock);
18489 +                       break;
18490 +               }
18491 +               spin_unlock(&inode->i_lock);
18492 +       } else {
18493 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18494 +               d = d_find_any_alias(inode);
18495 +               if (!d) {
18496 +                       au_iigen_dec(inode);
18497 +                       goto out;
18498 +               }
18499 +
18500 +               spin_lock(&d->d_lock);
18501 +               dname = &d->d_name;
18502 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18503 +                       spin_unlock(&d->d_lock);
18504 +                       err = hn_gen_tree(d);
18505 +                       spin_lock(&d->d_lock);
18506 +               }
18507 +               spin_unlock(&d->d_lock);
18508 +               dput(d);
18509 +       }
18510 +
18511 +out:
18512 +       AuTraceErr(err);
18513 +       return err;
18514 +}
18515 +
18516 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18517 +{
18518 +       int err;
18519 +
18520 +       if (IS_ROOT(dentry)) {
18521 +               pr_warn("branch root dir was changed\n");
18522 +               return 0;
18523 +       }
18524 +
18525 +       err = 0;
18526 +       if (!isdir) {
18527 +               au_digen_dec(dentry);
18528 +               if (d_really_is_positive(dentry))
18529 +                       au_iigen_dec(d_inode(dentry));
18530 +       } else {
18531 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18532 +               if (d_really_is_positive(dentry))
18533 +                       err = hn_gen_tree(dentry);
18534 +       }
18535 +
18536 +       AuTraceErr(err);
18537 +       return err;
18538 +}
18539 +
18540 +/* ---------------------------------------------------------------------- */
18541 +
18542 +/* hnotify job flags */
18543 +#define AuHnJob_XINO0          1
18544 +#define AuHnJob_GEN            (1 << 1)
18545 +#define AuHnJob_DIRENT         (1 << 2)
18546 +#define AuHnJob_ISDIR          (1 << 3)
18547 +#define AuHnJob_TRYXINO0       (1 << 4)
18548 +#define AuHnJob_MNTPNT         (1 << 5)
18549 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
18550 +#define au_fset_hnjob(flags, name) \
18551 +       do { (flags) |= AuHnJob_##name; } while (0)
18552 +#define au_fclr_hnjob(flags, name) \
18553 +       do { (flags) &= ~AuHnJob_##name; } while (0)
18554 +
18555 +enum {
18556 +       AuHn_CHILD,
18557 +       AuHn_PARENT,
18558 +       AuHnLast
18559 +};
18560 +
18561 +struct au_hnotify_args {
18562 +       struct inode *h_dir, *dir, *h_child_inode;
18563 +       u32 mask;
18564 +       unsigned int flags[AuHnLast];
18565 +       unsigned int h_child_nlen;
18566 +       char h_child_name[];
18567 +};
18568 +
18569 +struct hn_job_args {
18570 +       unsigned int flags;
18571 +       struct inode *inode, *h_inode, *dir, *h_dir;
18572 +       struct dentry *dentry;
18573 +       char *h_name;
18574 +       int h_nlen;
18575 +};
18576 +
18577 +static int hn_job(struct hn_job_args *a)
18578 +{
18579 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18580 +       int e;
18581 +
18582 +       /* reset xino */
18583 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18584 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
18585 +
18586 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
18587 +           && a->inode
18588 +           && a->h_inode) {
18589 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18590 +               if (!a->h_inode->i_nlink
18591 +                   && !(a->h_inode->i_state & I_LINKABLE))
18592 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
18593 +               inode_unlock_shared(a->h_inode);
18594 +       }
18595 +
18596 +       /* make the generation obsolete */
18597 +       if (au_ftest_hnjob(a->flags, GEN)) {
18598 +               e = -1;
18599 +               if (a->inode)
18600 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18601 +                                             isdir);
18602 +               if (e && a->dentry)
18603 +                       hn_gen_by_name(a->dentry, isdir);
18604 +               /* ignore this error */
18605 +       }
18606 +
18607 +       /* make dir entries obsolete */
18608 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18609 +               struct au_vdir *vdir;
18610 +
18611 +               vdir = au_ivdir(a->inode);
18612 +               if (vdir)
18613 +                       vdir->vd_jiffy = 0;
18614 +               /* IMustLock(a->inode); */
18615 +               /* inode_inc_iversion(a->inode); */
18616 +       }
18617 +
18618 +       /* can do nothing but warn */
18619 +       if (au_ftest_hnjob(a->flags, MNTPNT)
18620 +           && a->dentry
18621 +           && d_mountpoint(a->dentry))
18622 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18623 +
18624 +       return 0;
18625 +}
18626 +
18627 +/* ---------------------------------------------------------------------- */
18628 +
18629 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18630 +                                          struct inode *dir)
18631 +{
18632 +       struct dentry *dentry, *d, *parent;
18633 +       struct qstr *dname;
18634 +
18635 +       parent = d_find_any_alias(dir);
18636 +       if (!parent)
18637 +               return NULL;
18638 +
18639 +       dentry = NULL;
18640 +       spin_lock(&parent->d_lock);
18641 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
18642 +               /* AuDbg("%pd\n", d); */
18643 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18644 +               dname = &d->d_name;
18645 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
18646 +                       goto cont_unlock;
18647 +               if (au_di(d))
18648 +                       au_digen_dec(d);
18649 +               else
18650 +                       goto cont_unlock;
18651 +               if (au_dcount(d) > 0) {
18652 +                       dentry = dget_dlock(d);
18653 +                       spin_unlock(&d->d_lock);
18654 +                       break;
18655 +               }
18656 +
18657 +cont_unlock:
18658 +               spin_unlock(&d->d_lock);
18659 +       }
18660 +       spin_unlock(&parent->d_lock);
18661 +       dput(parent);
18662 +
18663 +       if (dentry)
18664 +               di_write_lock_child(dentry);
18665 +
18666 +       return dentry;
18667 +}
18668 +
18669 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18670 +                                        aufs_bindex_t bindex, ino_t h_ino)
18671 +{
18672 +       struct inode *inode;
18673 +       ino_t ino;
18674 +       int err;
18675 +
18676 +       inode = NULL;
18677 +       err = au_xino_read(sb, bindex, h_ino, &ino);
18678 +       if (!err && ino)
18679 +               inode = ilookup(sb, ino);
18680 +       if (!inode)
18681 +               goto out;
18682 +
18683 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18684 +               pr_warn("wrong root branch\n");
18685 +               iput(inode);
18686 +               inode = NULL;
18687 +               goto out;
18688 +       }
18689 +
18690 +       ii_write_lock_child(inode);
18691 +
18692 +out:
18693 +       return inode;
18694 +}
18695 +
18696 +static void au_hn_bh(void *_args)
18697 +{
18698 +       struct au_hnotify_args *a = _args;
18699 +       struct super_block *sb;
18700 +       aufs_bindex_t bindex, bbot, bfound;
18701 +       unsigned char xino, try_iput;
18702 +       int err;
18703 +       struct inode *inode;
18704 +       ino_t h_ino;
18705 +       struct hn_job_args args;
18706 +       struct dentry *dentry;
18707 +       struct au_sbinfo *sbinfo;
18708 +
18709 +       AuDebugOn(!_args);
18710 +       AuDebugOn(!a->h_dir);
18711 +       AuDebugOn(!a->dir);
18712 +       AuDebugOn(!a->mask);
18713 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18714 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
18715 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
18716 +
18717 +       inode = NULL;
18718 +       dentry = NULL;
18719 +       /*
18720 +        * do not lock a->dir->i_mutex here
18721 +        * because of d_revalidate() may cause a deadlock.
18722 +        */
18723 +       sb = a->dir->i_sb;
18724 +       AuDebugOn(!sb);
18725 +       sbinfo = au_sbi(sb);
18726 +       AuDebugOn(!sbinfo);
18727 +       si_write_lock(sb, AuLock_NOPLMW);
18728 +
18729 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18730 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18731 +               case FS_MOVED_FROM:
18732 +               case FS_MOVED_TO:
18733 +                       AuWarn1("DIRREN with UDBA may not work correctly "
18734 +                               "for the direct rename(2)\n");
18735 +               }
18736 +
18737 +       ii_read_lock_parent(a->dir);
18738 +       bfound = -1;
18739 +       bbot = au_ibbot(a->dir);
18740 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18741 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18742 +                       bfound = bindex;
18743 +                       break;
18744 +               }
18745 +       ii_read_unlock(a->dir);
18746 +       if (unlikely(bfound < 0))
18747 +               goto out;
18748 +
18749 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
18750 +       h_ino = 0;
18751 +       if (a->h_child_inode)
18752 +               h_ino = a->h_child_inode->i_ino;
18753 +
18754 +       if (a->h_child_nlen
18755 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18756 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18757 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18758 +                                             a->dir);
18759 +       try_iput = 0;
18760 +       if (dentry && d_really_is_positive(dentry))
18761 +               inode = d_inode(dentry);
18762 +       if (xino && !inode && h_ino
18763 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18764 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18765 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18766 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18767 +               try_iput = 1;
18768 +       }
18769 +
18770 +       args.flags = a->flags[AuHn_CHILD];
18771 +       args.dentry = dentry;
18772 +       args.inode = inode;
18773 +       args.h_inode = a->h_child_inode;
18774 +       args.dir = a->dir;
18775 +       args.h_dir = a->h_dir;
18776 +       args.h_name = a->h_child_name;
18777 +       args.h_nlen = a->h_child_nlen;
18778 +       err = hn_job(&args);
18779 +       if (dentry) {
18780 +               if (au_di(dentry))
18781 +                       di_write_unlock(dentry);
18782 +               dput(dentry);
18783 +       }
18784 +       if (inode && try_iput) {
18785 +               ii_write_unlock(inode);
18786 +               iput(inode);
18787 +       }
18788 +
18789 +       ii_write_lock_parent(a->dir);
18790 +       args.flags = a->flags[AuHn_PARENT];
18791 +       args.dentry = NULL;
18792 +       args.inode = a->dir;
18793 +       args.h_inode = a->h_dir;
18794 +       args.dir = NULL;
18795 +       args.h_dir = NULL;
18796 +       args.h_name = NULL;
18797 +       args.h_nlen = 0;
18798 +       err = hn_job(&args);
18799 +       ii_write_unlock(a->dir);
18800 +
18801 +out:
18802 +       iput(a->h_child_inode);
18803 +       iput(a->h_dir);
18804 +       iput(a->dir);
18805 +       si_write_unlock(sb);
18806 +       au_nwt_done(&sbinfo->si_nowait);
18807 +       au_kfree_rcu(a);
18808 +}
18809 +
18810 +/* ---------------------------------------------------------------------- */
18811 +
18812 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18813 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
18814 +{
18815 +       int err, len;
18816 +       unsigned int flags[AuHnLast], f;
18817 +       unsigned char isdir, isroot, wh;
18818 +       struct inode *dir;
18819 +       struct au_hnotify_args *args;
18820 +       char *p, *h_child_name;
18821 +
18822 +       err = 0;
18823 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18824 +       dir = igrab(hnotify->hn_aufs_inode);
18825 +       if (!dir)
18826 +               goto out;
18827 +
18828 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
18829 +       wh = 0;
18830 +       h_child_name = (void *)h_child_qstr->name;
18831 +       len = h_child_qstr->len;
18832 +       if (h_child_name) {
18833 +               if (len > AUFS_WH_PFX_LEN
18834 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18835 +                       h_child_name += AUFS_WH_PFX_LEN;
18836 +                       len -= AUFS_WH_PFX_LEN;
18837 +                       wh = 1;
18838 +               }
18839 +       }
18840 +
18841 +       isdir = 0;
18842 +       if (h_child_inode)
18843 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
18844 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
18845 +       flags[AuHn_CHILD] = 0;
18846 +       if (isdir)
18847 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
18848 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18849 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
18850 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18851 +       case FS_MOVED_FROM:
18852 +       case FS_MOVED_TO:
18853 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18854 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18855 +               fallthrough;
18856 +       case FS_CREATE:
18857 +               AuDebugOn(!h_child_name);
18858 +               break;
18859 +
18860 +       case FS_DELETE:
18861 +               /*
18862 +                * aufs never be able to get this child inode.
18863 +                * revalidation should be in d_revalidate()
18864 +                * by checking i_nlink, i_generation or d_unhashed().
18865 +                */
18866 +               AuDebugOn(!h_child_name);
18867 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18868 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18869 +               break;
18870 +
18871 +       default:
18872 +               AuDebugOn(1);
18873 +       }
18874 +
18875 +       if (wh)
18876 +               h_child_inode = NULL;
18877 +
18878 +       err = -ENOMEM;
18879 +       /* iput() and kfree() will be called in au_hnotify() */
18880 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18881 +       if (unlikely(!args)) {
18882 +               AuErr1("no memory\n");
18883 +               iput(dir);
18884 +               goto out;
18885 +       }
18886 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18887 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18888 +       args->mask = mask;
18889 +       args->dir = dir;
18890 +       args->h_dir = igrab(h_dir);
18891 +       if (h_child_inode)
18892 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
18893 +       args->h_child_inode = h_child_inode;
18894 +       args->h_child_nlen = len;
18895 +       if (len) {
18896 +               p = (void *)args;
18897 +               p += sizeof(*args);
18898 +               memcpy(p, h_child_name, len);
18899 +               p[len] = 0;
18900 +       }
18901 +
18902 +       /* NFS fires the event for silly-renamed one from kworker */
18903 +       f = 0;
18904 +       if (!dir->i_nlink
18905 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18906 +               f = AuWkq_NEST;
18907 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18908 +       if (unlikely(err)) {
18909 +               pr_err("wkq %d\n", err);
18910 +               iput(args->h_child_inode);
18911 +               iput(args->h_dir);
18912 +               iput(args->dir);
18913 +               au_kfree_rcu(args);
18914 +       }
18915 +
18916 +out:
18917 +       return err;
18918 +}
18919 +
18920 +/* ---------------------------------------------------------------------- */
18921 +
18922 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18923 +{
18924 +       int err;
18925 +
18926 +       AuDebugOn(!(udba & AuOptMask_UDBA));
18927 +
18928 +       err = 0;
18929 +       if (au_hnotify_op.reset_br)
18930 +               err = au_hnotify_op.reset_br(udba, br, perm);
18931 +
18932 +       return err;
18933 +}
18934 +
18935 +int au_hnotify_init_br(struct au_branch *br, int perm)
18936 +{
18937 +       int err;
18938 +
18939 +       err = 0;
18940 +       if (au_hnotify_op.init_br)
18941 +               err = au_hnotify_op.init_br(br, perm);
18942 +
18943 +       return err;
18944 +}
18945 +
18946 +void au_hnotify_fin_br(struct au_branch *br)
18947 +{
18948 +       if (au_hnotify_op.fin_br)
18949 +               au_hnotify_op.fin_br(br);
18950 +}
18951 +
18952 +static void au_hn_destroy_cache(void)
18953 +{
18954 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18955 +       au_cache[AuCache_HNOTIFY] = NULL;
18956 +}
18957 +
18958 +int __init au_hnotify_init(void)
18959 +{
18960 +       int err;
18961 +
18962 +       err = -ENOMEM;
18963 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18964 +       if (au_cache[AuCache_HNOTIFY]) {
18965 +               err = 0;
18966 +               if (au_hnotify_op.init)
18967 +                       err = au_hnotify_op.init();
18968 +               if (unlikely(err))
18969 +                       au_hn_destroy_cache();
18970 +       }
18971 +       AuTraceErr(err);
18972 +       return err;
18973 +}
18974 +
18975 +void au_hnotify_fin(void)
18976 +{
18977 +       if (au_hnotify_op.fin)
18978 +               au_hnotify_op.fin();
18979 +
18980 +       /* cf. au_cache_fin() */
18981 +       if (au_cache[AuCache_HNOTIFY])
18982 +               au_hn_destroy_cache();
18983 +}
18984 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
18985 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
18986 +++ linux/fs/aufs/iinfo.c       2022-11-05 23:02:18.965889284 +0100
18987 @@ -0,0 +1,286 @@
18988 +// SPDX-License-Identifier: GPL-2.0
18989 +/*
18990 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18991 + *
18992 + * This program is free software; you can redistribute it and/or modify
18993 + * it under the terms of the GNU General Public License as published by
18994 + * the Free Software Foundation; either version 2 of the License, or
18995 + * (at your option) any later version.
18996 + *
18997 + * This program is distributed in the hope that it will be useful,
18998 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18999 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19000 + * GNU General Public License for more details.
19001 + *
19002 + * You should have received a copy of the GNU General Public License
19003 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19004 + */
19005 +
19006 +/*
19007 + * inode private data
19008 + */
19009 +
19010 +#include "aufs.h"
19011 +
19012 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
19013 +{
19014 +       struct inode *h_inode;
19015 +       struct au_hinode *hinode;
19016 +
19017 +       IiMustAnyLock(inode);
19018 +
19019 +       hinode = au_hinode(au_ii(inode), bindex);
19020 +       h_inode = hinode->hi_inode;
19021 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19022 +       return h_inode;
19023 +}
19024 +
19025 +/* todo: hard/soft set? */
19026 +void au_hiput(struct au_hinode *hinode)
19027 +{
19028 +       au_hn_free(hinode);
19029 +       dput(hinode->hi_whdentry);
19030 +       iput(hinode->hi_inode);
19031 +}
19032 +
19033 +unsigned int au_hi_flags(struct inode *inode, int isdir)
19034 +{
19035 +       unsigned int flags;
19036 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
19037 +
19038 +       flags = 0;
19039 +       if (au_opt_test(mnt_flags, XINO))
19040 +               au_fset_hi(flags, XINO);
19041 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19042 +               au_fset_hi(flags, HNOTIFY);
19043 +       return flags;
19044 +}
19045 +
19046 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19047 +                  struct inode *h_inode, unsigned int flags)
19048 +{
19049 +       struct au_hinode *hinode;
19050 +       struct inode *hi;
19051 +       struct au_iinfo *iinfo = au_ii(inode);
19052 +
19053 +       IiMustWriteLock(inode);
19054 +
19055 +       hinode = au_hinode(iinfo, bindex);
19056 +       hi = hinode->hi_inode;
19057 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19058 +
19059 +       if (hi)
19060 +               au_hiput(hinode);
19061 +       hinode->hi_inode = h_inode;
19062 +       if (h_inode) {
19063 +               int err;
19064 +               struct super_block *sb = inode->i_sb;
19065 +               struct au_branch *br;
19066 +
19067 +               AuDebugOn(inode->i_mode
19068 +                         && (h_inode->i_mode & S_IFMT)
19069 +                         != (inode->i_mode & S_IFMT));
19070 +               if (bindex == iinfo->ii_btop)
19071 +                       au_cpup_igen(inode, h_inode);
19072 +               br = au_sbr(sb, bindex);
19073 +               hinode->hi_id = br->br_id;
19074 +               if (au_ftest_hi(flags, XINO)) {
19075 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
19076 +                                           inode->i_ino);
19077 +                       if (unlikely(err))
19078 +                               AuIOErr1("failed au_xino_write() %d\n", err);
19079 +               }
19080 +
19081 +               if (au_ftest_hi(flags, HNOTIFY)
19082 +                   && au_br_hnotifyable(br->br_perm)) {
19083 +                       err = au_hn_alloc(hinode, inode);
19084 +                       if (unlikely(err))
19085 +                               AuIOErr1("au_hn_alloc() %d\n", err);
19086 +               }
19087 +       }
19088 +}
19089 +
19090 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19091 +                 struct dentry *h_wh)
19092 +{
19093 +       struct au_hinode *hinode;
19094 +
19095 +       IiMustWriteLock(inode);
19096 +
19097 +       hinode = au_hinode(au_ii(inode), bindex);
19098 +       AuDebugOn(hinode->hi_whdentry);
19099 +       hinode->hi_whdentry = h_wh;
19100 +}
19101 +
19102 +void au_update_iigen(struct inode *inode, int half)
19103 +{
19104 +       struct au_iinfo *iinfo;
19105 +       struct au_iigen *iigen;
19106 +       unsigned int sigen;
19107 +
19108 +       sigen = au_sigen(inode->i_sb);
19109 +       iinfo = au_ii(inode);
19110 +       iigen = &iinfo->ii_generation;
19111 +       spin_lock(&iigen->ig_spin);
19112 +       iigen->ig_generation = sigen;
19113 +       if (half)
19114 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19115 +       else
19116 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
19117 +       spin_unlock(&iigen->ig_spin);
19118 +}
19119 +
19120 +/* it may be called at remount time, too */
19121 +void au_update_ibrange(struct inode *inode, int do_put_zero)
19122 +{
19123 +       struct au_iinfo *iinfo;
19124 +       aufs_bindex_t bindex, bbot;
19125 +
19126 +       AuDebugOn(au_is_bad_inode(inode));
19127 +       IiMustWriteLock(inode);
19128 +
19129 +       iinfo = au_ii(inode);
19130 +       if (do_put_zero && iinfo->ii_btop >= 0) {
19131 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19132 +                    bindex++) {
19133 +                       struct inode *h_i;
19134 +
19135 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
19136 +                       if (h_i
19137 +                           && !h_i->i_nlink
19138 +                           && !(h_i->i_state & I_LINKABLE))
19139 +                               au_set_h_iptr(inode, bindex, NULL, 0);
19140 +               }
19141 +       }
19142 +
19143 +       iinfo->ii_btop = -1;
19144 +       iinfo->ii_bbot = -1;
19145 +       bbot = au_sbbot(inode->i_sb);
19146 +       for (bindex = 0; bindex <= bbot; bindex++)
19147 +               if (au_hinode(iinfo, bindex)->hi_inode) {
19148 +                       iinfo->ii_btop = bindex;
19149 +                       break;
19150 +               }
19151 +       if (iinfo->ii_btop >= 0)
19152 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19153 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
19154 +                               iinfo->ii_bbot = bindex;
19155 +                               break;
19156 +                       }
19157 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
19158 +}
19159 +
19160 +/* ---------------------------------------------------------------------- */
19161 +
19162 +void au_icntnr_init_once(void *_c)
19163 +{
19164 +       struct au_icntnr *c = _c;
19165 +       struct au_iinfo *iinfo = &c->iinfo;
19166 +
19167 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
19168 +       au_rw_init(&iinfo->ii_rwsem);
19169 +       inode_init_once(&c->vfs_inode);
19170 +}
19171 +
19172 +void au_hinode_init(struct au_hinode *hinode)
19173 +{
19174 +       hinode->hi_inode = NULL;
19175 +       hinode->hi_id = -1;
19176 +       au_hn_init(hinode);
19177 +       hinode->hi_whdentry = NULL;
19178 +}
19179 +
19180 +int au_iinfo_init(struct inode *inode)
19181 +{
19182 +       struct au_iinfo *iinfo;
19183 +       struct super_block *sb;
19184 +       struct au_hinode *hi;
19185 +       int nbr, i;
19186 +
19187 +       sb = inode->i_sb;
19188 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19189 +       nbr = au_sbbot(sb) + 1;
19190 +       if (unlikely(nbr <= 0))
19191 +               nbr = 1;
19192 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19193 +       if (hi) {
19194 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
19195 +
19196 +               iinfo->ii_hinode = hi;
19197 +               for (i = 0; i < nbr; i++, hi++)
19198 +                       au_hinode_init(hi);
19199 +
19200 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
19201 +               iinfo->ii_btop = -1;
19202 +               iinfo->ii_bbot = -1;
19203 +               iinfo->ii_vdir = NULL;
19204 +               return 0;
19205 +       }
19206 +       return -ENOMEM;
19207 +}
19208 +
19209 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
19210 +{
19211 +       int err, i;
19212 +       struct au_hinode *hip;
19213 +
19214 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
19215 +
19216 +       err = -ENOMEM;
19217 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19218 +                         may_shrink);
19219 +       if (hip) {
19220 +               iinfo->ii_hinode = hip;
19221 +               i = iinfo->ii_bbot + 1;
19222 +               hip += i;
19223 +               for (; i < nbr; i++, hip++)
19224 +                       au_hinode_init(hip);
19225 +               err = 0;
19226 +       }
19227 +
19228 +       return err;
19229 +}
19230 +
19231 +void au_iinfo_fin(struct inode *inode)
19232 +{
19233 +       struct au_iinfo *iinfo;
19234 +       struct au_hinode *hi;
19235 +       struct super_block *sb;
19236 +       aufs_bindex_t bindex, bbot;
19237 +       const unsigned char unlinked = !inode->i_nlink;
19238 +
19239 +       AuDebugOn(au_is_bad_inode(inode));
19240 +
19241 +       sb = inode->i_sb;
19242 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
19243 +       if (si_pid_test(sb))
19244 +               au_xino_delete_inode(inode, unlinked);
19245 +       else {
19246 +               /*
19247 +                * it is safe to hide the dependency between sbinfo and
19248 +                * sb->s_umount.
19249 +                */
19250 +               lockdep_off();
19251 +               si_noflush_read_lock(sb);
19252 +               au_xino_delete_inode(inode, unlinked);
19253 +               si_read_unlock(sb);
19254 +               lockdep_on();
19255 +       }
19256 +
19257 +       iinfo = au_ii(inode);
19258 +       if (iinfo->ii_vdir)
19259 +               au_vdir_free(iinfo->ii_vdir);
19260 +
19261 +       bindex = iinfo->ii_btop;
19262 +       if (bindex >= 0) {
19263 +               hi = au_hinode(iinfo, bindex);
19264 +               bbot = iinfo->ii_bbot;
19265 +               while (bindex++ <= bbot) {
19266 +                       if (hi->hi_inode)
19267 +                               au_hiput(hi);
19268 +                       hi++;
19269 +               }
19270 +       }
19271 +       au_kfree_rcu(iinfo->ii_hinode);
19272 +       AuRwDestroy(&iinfo->ii_rwsem);
19273 +}
19274 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
19275 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
19276 +++ linux/fs/aufs/inode.c       2023-04-24 08:13:40.969576115 +0200
19277 @@ -0,0 +1,531 @@
19278 +// SPDX-License-Identifier: GPL-2.0
19279 +/*
19280 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19281 + *
19282 + * This program is free software; you can redistribute it and/or modify
19283 + * it under the terms of the GNU General Public License as published by
19284 + * the Free Software Foundation; either version 2 of the License, or
19285 + * (at your option) any later version.
19286 + *
19287 + * This program is distributed in the hope that it will be useful,
19288 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19289 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19290 + * GNU General Public License for more details.
19291 + *
19292 + * You should have received a copy of the GNU General Public License
19293 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19294 + */
19295 +
19296 +/*
19297 + * inode functions
19298 + */
19299 +
19300 +#include <linux/iversion.h>
19301 +#include "aufs.h"
19302 +
19303 +struct inode *au_igrab(struct inode *inode)
19304 +{
19305 +       if (inode) {
19306 +               AuDebugOn(!atomic_read(&inode->i_count));
19307 +               ihold(inode);
19308 +       }
19309 +       return inode;
19310 +}
19311 +
19312 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19313 +{
19314 +       au_cpup_attr_all(inode, /*force*/0);
19315 +       au_update_iigen(inode, /*half*/1);
19316 +       if (do_version)
19317 +               inode_inc_iversion(inode);
19318 +}
19319 +
19320 +static int au_ii_refresh(struct inode *inode, int *update)
19321 +{
19322 +       int err, e, nbr;
19323 +       umode_t type;
19324 +       aufs_bindex_t bindex, new_bindex;
19325 +       struct super_block *sb;
19326 +       struct au_iinfo *iinfo;
19327 +       struct au_hinode *p, *q, tmp;
19328 +
19329 +       AuDebugOn(au_is_bad_inode(inode));
19330 +       IiMustWriteLock(inode);
19331 +
19332 +       *update = 0;
19333 +       sb = inode->i_sb;
19334 +       nbr = au_sbbot(sb) + 1;
19335 +       type = inode->i_mode & S_IFMT;
19336 +       iinfo = au_ii(inode);
19337 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
19338 +       if (unlikely(err))
19339 +               goto out;
19340 +
19341 +       AuDebugOn(iinfo->ii_btop < 0);
19342 +       p = au_hinode(iinfo, iinfo->ii_btop);
19343 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19344 +            bindex++, p++) {
19345 +               if (!p->hi_inode)
19346 +                       continue;
19347 +
19348 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
19349 +               new_bindex = au_br_index(sb, p->hi_id);
19350 +               if (new_bindex == bindex)
19351 +                       continue;
19352 +
19353 +               if (new_bindex < 0) {
19354 +                       *update = 1;
19355 +                       au_hiput(p);
19356 +                       p->hi_inode = NULL;
19357 +                       continue;
19358 +               }
19359 +
19360 +               if (new_bindex < iinfo->ii_btop)
19361 +                       iinfo->ii_btop = new_bindex;
19362 +               if (iinfo->ii_bbot < new_bindex)
19363 +                       iinfo->ii_bbot = new_bindex;
19364 +               /* swap two lower inode, and loop again */
19365 +               q = au_hinode(iinfo, new_bindex);
19366 +               tmp = *q;
19367 +               *q = *p;
19368 +               *p = tmp;
19369 +               if (tmp.hi_inode) {
19370 +                       bindex--;
19371 +                       p--;
19372 +               }
19373 +       }
19374 +       au_update_ibrange(inode, /*do_put_zero*/0);
19375 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
19376 +       e = au_dy_irefresh(inode);
19377 +       if (unlikely(e && !err))
19378 +               err = e;
19379 +
19380 +out:
19381 +       AuTraceErr(err);
19382 +       return err;
19383 +}
19384 +
19385 +void au_refresh_iop(struct inode *inode, int force_getattr)
19386 +{
19387 +       int type;
19388 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19389 +       const struct inode_operations *iop
19390 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
19391 +
19392 +       if (inode->i_op == iop)
19393 +               return;
19394 +
19395 +       switch (inode->i_mode & S_IFMT) {
19396 +       case S_IFDIR:
19397 +               type = AuIop_DIR;
19398 +               break;
19399 +       case S_IFLNK:
19400 +               type = AuIop_SYMLINK;
19401 +               break;
19402 +       default:
19403 +               type = AuIop_OTHER;
19404 +               break;
19405 +       }
19406 +
19407 +       inode->i_op = iop + type;
19408 +       /* unnecessary smp_wmb() */
19409 +}
19410 +
19411 +int au_refresh_hinode_self(struct inode *inode)
19412 +{
19413 +       int err, update;
19414 +
19415 +       err = au_ii_refresh(inode, &update);
19416 +       if (!err)
19417 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19418 +
19419 +       AuTraceErr(err);
19420 +       return err;
19421 +}
19422 +
19423 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19424 +{
19425 +       int err, e, update;
19426 +       unsigned int flags;
19427 +       umode_t mode;
19428 +       aufs_bindex_t bindex, bbot;
19429 +       unsigned char isdir;
19430 +       struct au_hinode *p;
19431 +       struct au_iinfo *iinfo;
19432 +
19433 +       err = au_ii_refresh(inode, &update);
19434 +       if (unlikely(err))
19435 +               goto out;
19436 +
19437 +       update = 0;
19438 +       iinfo = au_ii(inode);
19439 +       p = au_hinode(iinfo, iinfo->ii_btop);
19440 +       mode = (inode->i_mode & S_IFMT);
19441 +       isdir = S_ISDIR(mode);
19442 +       flags = au_hi_flags(inode, isdir);
19443 +       bbot = au_dbbot(dentry);
19444 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
19445 +               struct inode *h_i, *h_inode;
19446 +               struct dentry *h_d;
19447 +
19448 +               h_d = au_h_dptr(dentry, bindex);
19449 +               if (!h_d || d_is_negative(h_d))
19450 +                       continue;
19451 +
19452 +               h_inode = d_inode(h_d);
19453 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
19454 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
19455 +                       h_i = au_h_iptr(inode, bindex);
19456 +                       if (h_i) {
19457 +                               if (h_i == h_inode)
19458 +                                       continue;
19459 +                               err = -EIO;
19460 +                               break;
19461 +                       }
19462 +               }
19463 +               if (bindex < iinfo->ii_btop)
19464 +                       iinfo->ii_btop = bindex;
19465 +               if (iinfo->ii_bbot < bindex)
19466 +                       iinfo->ii_bbot = bindex;
19467 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
19468 +               update = 1;
19469 +       }
19470 +       au_update_ibrange(inode, /*do_put_zero*/0);
19471 +       e = au_dy_irefresh(inode);
19472 +       if (unlikely(e && !err))
19473 +               err = e;
19474 +       if (!err)
19475 +               au_refresh_hinode_attr(inode, update && isdir);
19476 +
19477 +out:
19478 +       AuTraceErr(err);
19479 +       return err;
19480 +}
19481 +
19482 +static int set_inode(struct inode *inode, struct dentry *dentry)
19483 +{
19484 +       int err;
19485 +       unsigned int flags;
19486 +       umode_t mode;
19487 +       aufs_bindex_t bindex, btop, btail;
19488 +       unsigned char isdir;
19489 +       struct dentry *h_dentry;
19490 +       struct inode *h_inode;
19491 +       struct au_iinfo *iinfo;
19492 +       const struct inode_operations *iop;
19493 +
19494 +       IiMustWriteLock(inode);
19495 +
19496 +       err = 0;
19497 +       isdir = 0;
19498 +       iop = au_sbi(inode->i_sb)->si_iop_array;
19499 +       btop = au_dbtop(dentry);
19500 +       h_dentry = au_h_dptr(dentry, btop);
19501 +       h_inode = d_inode(h_dentry);
19502 +       mode = h_inode->i_mode;
19503 +       switch (mode & S_IFMT) {
19504 +       case S_IFREG:
19505 +               btail = au_dbtail(dentry);
19506 +               inode->i_op = iop + AuIop_OTHER;
19507 +               inode->i_fop = &aufs_file_fop;
19508 +               err = au_dy_iaop(inode, btop, h_inode);
19509 +               if (unlikely(err))
19510 +                       goto out;
19511 +               break;
19512 +       case S_IFDIR:
19513 +               isdir = 1;
19514 +               btail = au_dbtaildir(dentry);
19515 +               inode->i_op = iop + AuIop_DIR;
19516 +               inode->i_fop = &aufs_dir_fop;
19517 +               break;
19518 +       case S_IFLNK:
19519 +               btail = au_dbtail(dentry);
19520 +               inode->i_op = iop + AuIop_SYMLINK;
19521 +               break;
19522 +       case S_IFBLK:
19523 +       case S_IFCHR:
19524 +       case S_IFIFO:
19525 +       case S_IFSOCK:
19526 +               btail = au_dbtail(dentry);
19527 +               inode->i_op = iop + AuIop_OTHER;
19528 +               init_special_inode(inode, mode, h_inode->i_rdev);
19529 +               break;
19530 +       default:
19531 +               AuIOErr("Unknown file type 0%o\n", mode);
19532 +               err = -EIO;
19533 +               goto out;
19534 +       }
19535 +
19536 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
19537 +       flags = au_hi_flags(inode, isdir);
19538 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19539 +           && au_ftest_hi(flags, HNOTIFY)
19540 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
19541 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19542 +               au_fclr_hi(flags, HNOTIFY);
19543 +       iinfo = au_ii(inode);
19544 +       iinfo->ii_btop = btop;
19545 +       iinfo->ii_bbot = btail;
19546 +       for (bindex = btop; bindex <= btail; bindex++) {
19547 +               h_dentry = au_h_dptr(dentry, bindex);
19548 +               if (h_dentry)
19549 +                       au_set_h_iptr(inode, bindex,
19550 +                                     au_igrab(d_inode(h_dentry)), flags);
19551 +       }
19552 +       au_cpup_attr_all(inode, /*force*/1);
19553 +       /*
19554 +        * to force calling aufs_get_inode_acl() every time,
19555 +        * do not call cache_no_acl() for aufs inode.
19556 +        */
19557 +
19558 +out:
19559 +       return err;
19560 +}
19561 +
19562 +/*
19563 + * successful returns with iinfo write_locked
19564 + * minus: errno
19565 + * zero: success, matched
19566 + * plus: no error, but unmatched
19567 + */
19568 +static int reval_inode(struct inode *inode, struct dentry *dentry)
19569 +{
19570 +       int err;
19571 +       unsigned int gen, igflags;
19572 +       aufs_bindex_t bindex, bbot;
19573 +       struct inode *h_inode, *h_dinode;
19574 +       struct dentry *h_dentry;
19575 +
19576 +       /*
19577 +        * before this function, if aufs got any iinfo lock, it must be only
19578 +        * one, the parent dir.
19579 +        * it can happen by UDBA and the obsoleted inode number.
19580 +        */
19581 +       err = -EIO;
19582 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
19583 +               goto out;
19584 +
19585 +       err = 1;
19586 +       ii_write_lock_new_child(inode);
19587 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
19588 +       h_dinode = d_inode(h_dentry);
19589 +       bbot = au_ibbot(inode);
19590 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
19591 +               h_inode = au_h_iptr(inode, bindex);
19592 +               if (!h_inode || h_inode != h_dinode)
19593 +                       continue;
19594 +
19595 +               err = 0;
19596 +               gen = au_iigen(inode, &igflags);
19597 +               if (gen == au_digen(dentry)
19598 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
19599 +                       break;
19600 +
19601 +               /* fully refresh inode using dentry */
19602 +               err = au_refresh_hinode(inode, dentry);
19603 +               if (!err)
19604 +                       au_update_iigen(inode, /*half*/0);
19605 +               break;
19606 +       }
19607 +
19608 +       if (unlikely(err))
19609 +               ii_write_unlock(inode);
19610 +out:
19611 +       return err;
19612 +}
19613 +
19614 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19615 +          unsigned int d_type, ino_t *ino)
19616 +{
19617 +       int err, idx;
19618 +       const int isnondir = d_type != DT_DIR;
19619 +
19620 +       /* prevent hardlinked inode number from race condition */
19621 +       if (isnondir) {
19622 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19623 +               if (unlikely(err))
19624 +                       goto out;
19625 +       }
19626 +
19627 +       err = au_xino_read(sb, bindex, h_ino, ino);
19628 +       if (unlikely(err))
19629 +               goto out_xinondir;
19630 +
19631 +       if (!*ino) {
19632 +               err = -EIO;
19633 +               *ino = au_xino_new_ino(sb);
19634 +               if (unlikely(!*ino))
19635 +                       goto out_xinondir;
19636 +               err = au_xino_write(sb, bindex, h_ino, *ino);
19637 +               if (unlikely(err))
19638 +                       goto out_xinondir;
19639 +       }
19640 +
19641 +out_xinondir:
19642 +       if (isnondir && idx >= 0)
19643 +               au_xinondir_leave(sb, bindex, h_ino, idx);
19644 +out:
19645 +       return err;
19646 +}
19647 +
19648 +/* successful returns with iinfo write_locked */
19649 +/* todo: return with unlocked? */
19650 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
19651 +{
19652 +       struct inode *inode, *h_inode;
19653 +       struct dentry *h_dentry;
19654 +       struct super_block *sb;
19655 +       ino_t h_ino, ino;
19656 +       int err, idx, hlinked;
19657 +       aufs_bindex_t btop;
19658 +
19659 +       sb = dentry->d_sb;
19660 +       btop = au_dbtop(dentry);
19661 +       h_dentry = au_h_dptr(dentry, btop);
19662 +       h_inode = d_inode(h_dentry);
19663 +       h_ino = h_inode->i_ino;
19664 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
19665 +
19666 +new_ino:
19667 +       /*
19668 +        * stop 'race'-ing between hardlinks under different
19669 +        * parents.
19670 +        */
19671 +       if (hlinked) {
19672 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
19673 +               inode = ERR_PTR(err);
19674 +               if (unlikely(err))
19675 +                       goto out;
19676 +       }
19677 +
19678 +       err = au_xino_read(sb, btop, h_ino, &ino);
19679 +       inode = ERR_PTR(err);
19680 +       if (unlikely(err))
19681 +               goto out_xinondir;
19682 +
19683 +       if (!ino) {
19684 +               ino = au_xino_new_ino(sb);
19685 +               if (unlikely(!ino)) {
19686 +                       inode = ERR_PTR(-EIO);
19687 +                       goto out_xinondir;
19688 +               }
19689 +       }
19690 +
19691 +       AuDbg("i%lu\n", (unsigned long)ino);
19692 +       inode = au_iget_locked(sb, ino);
19693 +       err = PTR_ERR(inode);
19694 +       if (IS_ERR(inode))
19695 +               goto out_xinondir;
19696 +
19697 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19698 +       if (inode->i_state & I_NEW) {
19699 +               ii_write_lock_new_child(inode);
19700 +               err = set_inode(inode, dentry);
19701 +               if (!err) {
19702 +                       unlock_new_inode(inode);
19703 +                       goto out_xinondir; /* success */
19704 +               }
19705 +
19706 +               /*
19707 +                * iget_failed() calls iput(), but we need to call
19708 +                * ii_write_unlock() after iget_failed(). so dirty hack for
19709 +                * i_count.
19710 +                */
19711 +               atomic_inc(&inode->i_count);
19712 +               iget_failed(inode);
19713 +               ii_write_unlock(inode);
19714 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
19715 +               /* ignore this error */
19716 +               goto out_iput;
19717 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
19718 +               /*
19719 +                * horrible race condition between lookup, readdir and copyup
19720 +                * (or something).
19721 +                */
19722 +               if (hlinked && idx >= 0)
19723 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19724 +               err = reval_inode(inode, dentry);
19725 +               if (unlikely(err < 0)) {
19726 +                       hlinked = 0;
19727 +                       goto out_iput;
19728 +               }
19729 +               if (!err)
19730 +                       goto out; /* success */
19731 +               else if (hlinked && idx >= 0) {
19732 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
19733 +                       if (unlikely(err)) {
19734 +                               iput(inode);
19735 +                               inode = ERR_PTR(err);
19736 +                               goto out;
19737 +                       }
19738 +               }
19739 +       }
19740 +
19741 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
19742 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
19743 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
19744 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
19745 +                       (unsigned long)h_ino, (unsigned long)ino);
19746 +       ino = 0;
19747 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
19748 +       if (!err) {
19749 +               iput(inode);
19750 +               if (hlinked && idx >= 0)
19751 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19752 +               goto new_ino;
19753 +       }
19754 +
19755 +out_iput:
19756 +       iput(inode);
19757 +       inode = ERR_PTR(err);
19758 +out_xinondir:
19759 +       if (hlinked && idx >= 0)
19760 +               au_xinondir_leave(sb, btop, h_ino, idx);
19761 +out:
19762 +       return inode;
19763 +}
19764 +
19765 +/* ---------------------------------------------------------------------- */
19766 +
19767 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19768 +              struct inode *inode)
19769 +{
19770 +       int err;
19771 +       struct inode *hi;
19772 +
19773 +       err = au_br_rdonly(au_sbr(sb, bindex));
19774 +
19775 +       /* pseudo-link after flushed may happen out of bounds */
19776 +       if (!err
19777 +           && inode
19778 +           && au_ibtop(inode) <= bindex
19779 +           && bindex <= au_ibbot(inode)) {
19780 +               /*
19781 +                * permission check is unnecessary since vfsub routine
19782 +                * will be called later
19783 +                */
19784 +               hi = au_h_iptr(inode, bindex);
19785 +               if (hi)
19786 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
19787 +       }
19788 +
19789 +       return err;
19790 +}
19791 +
19792 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19793 +                  int mask)
19794 +{
19795 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
19796 +               return 0;
19797 +       return inode_permission(h_idmap, h_inode, mask);
19798 +}
19799 +
19800 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19801 +                      int mask)
19802 +{
19803 +       if (au_test_nfs(h_inode->i_sb)
19804 +           && (mask & MAY_WRITE)
19805 +           && S_ISDIR(h_inode->i_mode))
19806 +               mask |= MAY_READ; /* force permission check */
19807 +       return au_test_h_perm(h_idmap, h_inode, mask);
19808 +}
19809 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
19810 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
19811 +++ linux/fs/aufs/inode.h       2023-04-24 08:13:40.969576115 +0200
19812 @@ -0,0 +1,707 @@
19813 +/* SPDX-License-Identifier: GPL-2.0 */
19814 +/*
19815 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19816 + *
19817 + * This program is free software; you can redistribute it and/or modify
19818 + * it under the terms of the GNU General Public License as published by
19819 + * the Free Software Foundation; either version 2 of the License, or
19820 + * (at your option) any later version.
19821 + *
19822 + * This program is distributed in the hope that it will be useful,
19823 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19824 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19825 + * GNU General Public License for more details.
19826 + *
19827 + * You should have received a copy of the GNU General Public License
19828 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19829 + */
19830 +
19831 +/*
19832 + * inode operations
19833 + */
19834 +
19835 +#ifndef __AUFS_INODE_H__
19836 +#define __AUFS_INODE_H__
19837 +
19838 +#ifdef __KERNEL__
19839 +
19840 +#include <linux/fsnotify.h>
19841 +#include "rwsem.h"
19842 +
19843 +struct vfsmount;
19844 +
19845 +struct au_hnotify {
19846 +#ifdef CONFIG_AUFS_HNOTIFY
19847 +#ifdef CONFIG_AUFS_HFSNOTIFY
19848 +       /* never use fsnotify_add_vfsmount_mark() */
19849 +       struct fsnotify_mark            hn_mark;
19850 +#endif
19851 +       struct inode            *hn_aufs_inode; /* no get/put */
19852 +       struct rcu_head         rcu;
19853 +#endif
19854 +} ____cacheline_aligned_in_smp;
19855 +
19856 +struct au_hinode {
19857 +       struct inode            *hi_inode;
19858 +       aufs_bindex_t           hi_id;
19859 +#ifdef CONFIG_AUFS_HNOTIFY
19860 +       struct au_hnotify       *hi_notify;
19861 +#endif
19862 +
19863 +       /* reference to the copied-up whiteout with get/put */
19864 +       struct dentry           *hi_whdentry;
19865 +};
19866 +
19867 +/* ig_flags */
19868 +#define AuIG_HALF_REFRESHED            1
19869 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
19870 +#define au_ig_fset(flags, name) \
19871 +       do { (flags) |= AuIG_##name; } while (0)
19872 +#define au_ig_fclr(flags, name) \
19873 +       do { (flags) &= ~AuIG_##name; } while (0)
19874 +
19875 +struct au_iigen {
19876 +       spinlock_t      ig_spin;
19877 +       __u32           ig_generation, ig_flags;
19878 +};
19879 +
19880 +struct au_vdir;
19881 +struct au_iinfo {
19882 +       struct au_iigen         ii_generation;
19883 +       struct super_block      *ii_hsb1;       /* no get/put */
19884 +
19885 +       struct au_rwsem         ii_rwsem;
19886 +       aufs_bindex_t           ii_btop, ii_bbot;
19887 +       __u32                   ii_higen;
19888 +       struct au_hinode        *ii_hinode;
19889 +       struct au_vdir          *ii_vdir;
19890 +};
19891 +
19892 +struct au_icntnr {
19893 +       struct au_iinfo         iinfo;
19894 +       struct inode            vfs_inode;
19895 +       struct hlist_bl_node    plink;
19896 +       struct rcu_head         rcu;
19897 +} ____cacheline_aligned_in_smp;
19898 +
19899 +/* au_pin flags */
19900 +#define AuPin_DI_LOCKED                1
19901 +#define AuPin_MNT_WRITE                (1 << 1)
19902 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
19903 +#define au_fset_pin(flags, name) \
19904 +       do { (flags) |= AuPin_##name; } while (0)
19905 +#define au_fclr_pin(flags, name) \
19906 +       do { (flags) &= ~AuPin_##name; } while (0)
19907 +
19908 +struct au_pin {
19909 +       /* input */
19910 +       struct dentry *dentry;
19911 +       unsigned int udba;
19912 +       unsigned char lsc_di, lsc_hi, flags;
19913 +       aufs_bindex_t bindex;
19914 +
19915 +       /* output */
19916 +       struct dentry *parent;
19917 +       struct au_hinode *hdir;
19918 +       struct vfsmount *h_mnt;
19919 +
19920 +       /* temporary unlock/relock for copyup */
19921 +       struct dentry *h_dentry, *h_parent;
19922 +       struct au_branch *br;
19923 +       struct task_struct *task;
19924 +};
19925 +
19926 +void au_pin_hdir_unlock(struct au_pin *p);
19927 +int au_pin_hdir_lock(struct au_pin *p);
19928 +int au_pin_hdir_relock(struct au_pin *p);
19929 +void au_pin_hdir_acquire_nest(struct au_pin *p);
19930 +void au_pin_hdir_release(struct au_pin *p);
19931 +
19932 +/* ---------------------------------------------------------------------- */
19933 +
19934 +static inline struct au_iinfo *au_ii(struct inode *inode)
19935 +{
19936 +       BUG_ON(is_bad_inode(inode));
19937 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19938 +}
19939 +
19940 +/* ---------------------------------------------------------------------- */
19941 +
19942 +/* inode.c */
19943 +struct inode *au_igrab(struct inode *inode);
19944 +void au_refresh_iop(struct inode *inode, int force_getattr);
19945 +int au_refresh_hinode_self(struct inode *inode);
19946 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19947 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19948 +          unsigned int d_type, ino_t *ino);
19949 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
19950 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19951 +              struct inode *inode);
19952 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19953 +                  int mask);
19954 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19955 +                      int mask);
19956 +
19957 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19958 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
19959 +{
19960 +#ifdef CONFIG_AUFS_SHWH
19961 +       return au_ino(sb, bindex, h_ino, d_type, ino);
19962 +#else
19963 +       return 0;
19964 +#endif
19965 +}
19966 +
19967 +/* i_op.c */
19968 +enum {
19969 +       AuIop_SYMLINK,
19970 +       AuIop_DIR,
19971 +       AuIop_OTHER,
19972 +       AuIop_Last
19973 +};
19974 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
19975 +       aufs_iop_nogetattr[AuIop_Last];
19976 +
19977 +/* au_wr_dir flags */
19978 +#define AuWrDir_ADD_ENTRY      1
19979 +#define AuWrDir_ISDIR          (1 << 1)
19980 +#define AuWrDir_TMPFILE                (1 << 2)
19981 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
19982 +#define au_fset_wrdir(flags, name) \
19983 +       do { (flags) |= AuWrDir_##name; } while (0)
19984 +#define au_fclr_wrdir(flags, name) \
19985 +       do { (flags) &= ~AuWrDir_##name; } while (0)
19986 +
19987 +struct au_wr_dir_args {
19988 +       aufs_bindex_t force_btgt;
19989 +       unsigned char flags;
19990 +};
19991 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19992 +             struct au_wr_dir_args *args);
19993 +
19994 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
19995 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
19996 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19997 +                unsigned int udba, unsigned char flags);
19998 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19999 +          unsigned int udba, unsigned char flags) __must_check;
20000 +int au_do_pin(struct au_pin *pin) __must_check;
20001 +void au_unpin(struct au_pin *pin);
20002 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
20003 +
20004 +#define AuIcpup_DID_CPUP       1
20005 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
20006 +#define au_fset_icpup(flags, name) \
20007 +       do { (flags) |= AuIcpup_##name; } while (0)
20008 +#define au_fclr_icpup(flags, name) \
20009 +       do { (flags) &= ~AuIcpup_##name; } while (0)
20010 +
20011 +struct au_icpup_args {
20012 +       unsigned char flags;
20013 +       unsigned char pin_flags;
20014 +       aufs_bindex_t btgt;
20015 +       unsigned int udba;
20016 +       struct au_pin pin;
20017 +       struct path h_path;
20018 +       struct inode *h_inode;
20019 +};
20020 +
20021 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20022 +                    struct au_icpup_args *a);
20023 +
20024 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20025 +                     struct path *h_path, int locked);
20026 +
20027 +/* i_op_add.c */
20028 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20029 +              struct dentry *h_parent, int isdir);
20030 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
20031 +              struct dentry *dentry, umode_t mode, dev_t dev);
20032 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
20033 +                struct dentry *dentry, const char *symname);
20034 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
20035 +               struct dentry *dentry, umode_t mode, bool want_excl);
20036 +struct vfsub_aopen_args;
20037 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20038 +                      struct vfsub_aopen_args *args);
20039 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
20040 +                struct file *file, umode_t mode);
20041 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20042 +             struct dentry *dentry);
20043 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
20044 +              struct dentry *dentry, umode_t mode);
20045 +
20046 +/* i_op_del.c */
20047 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20048 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20049 +              struct dentry *h_parent, int isdir);
20050 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
20051 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
20052 +
20053 +/* i_op_ren.c */
20054 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
20055 +int aufs_rename(struct mnt_idmap *idmap,
20056 +               struct inode *_src_dir, struct dentry *_src_dentry,
20057 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
20058 +               unsigned int _flags);
20059 +
20060 +/* iinfo.c */
20061 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20062 +void au_hiput(struct au_hinode *hinode);
20063 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20064 +                 struct dentry *h_wh);
20065 +unsigned int au_hi_flags(struct inode *inode, int isdir);
20066 +
20067 +/* hinode flags */
20068 +#define AuHi_XINO      1
20069 +#define AuHi_HNOTIFY   (1 << 1)
20070 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
20071 +#define au_fset_hi(flags, name) \
20072 +       do { (flags) |= AuHi_##name; } while (0)
20073 +#define au_fclr_hi(flags, name) \
20074 +       do { (flags) &= ~AuHi_##name; } while (0)
20075 +
20076 +#ifndef CONFIG_AUFS_HNOTIFY
20077 +#undef AuHi_HNOTIFY
20078 +#define AuHi_HNOTIFY   0
20079 +#endif
20080 +
20081 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20082 +                  struct inode *h_inode, unsigned int flags);
20083 +
20084 +void au_update_iigen(struct inode *inode, int half);
20085 +void au_update_ibrange(struct inode *inode, int do_put_zero);
20086 +
20087 +void au_icntnr_init_once(void *_c);
20088 +void au_hinode_init(struct au_hinode *hinode);
20089 +int au_iinfo_init(struct inode *inode);
20090 +void au_iinfo_fin(struct inode *inode);
20091 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
20092 +
20093 +#ifdef CONFIG_PROC_FS
20094 +/* plink.c */
20095 +int au_plink_maint(struct super_block *sb, int flags);
20096 +struct au_sbinfo;
20097 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20098 +int au_plink_maint_enter(struct super_block *sb);
20099 +#ifdef CONFIG_AUFS_DEBUG
20100 +void au_plink_list(struct super_block *sb);
20101 +#else
20102 +AuStubVoid(au_plink_list, struct super_block *sb)
20103 +#endif
20104 +int au_plink_test(struct inode *inode);
20105 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20106 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20107 +                    struct dentry *h_dentry);
20108 +void au_plink_put(struct super_block *sb, int verbose);
20109 +void au_plink_clean(struct super_block *sb, int verbose);
20110 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
20111 +#else
20112 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20113 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20114 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20115 +AuStubVoid(au_plink_list, struct super_block *sb);
20116 +AuStubInt0(au_plink_test, struct inode *inode);
20117 +AuStub(struct dentry *, au_plink_lkup, return NULL,
20118 +       struct inode *inode, aufs_bindex_t bindex);
20119 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20120 +          struct dentry *h_dentry);
20121 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20122 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20123 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20124 +#endif /* CONFIG_PROC_FS */
20125 +
20126 +#ifdef CONFIG_AUFS_XATTR
20127 +/* xattr.c */
20128 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
20129 +                 unsigned int verbose);
20130 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
20131 +void au_xattr_init(struct super_block *sb);
20132 +#else
20133 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
20134 +          int ignore_flags, unsigned int verbose);
20135 +AuStubVoid(au_xattr_init, struct super_block *sb);
20136 +#endif
20137 +
20138 +#ifdef CONFIG_FS_POSIX_ACL
20139 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu);
20140 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
20141 +                              struct dentry *dentry, int type);
20142 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
20143 +                struct posix_acl *acl, int type);
20144 +#endif
20145 +
20146 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20147 +enum {
20148 +       AU_XATTR_SET,
20149 +       AU_ACL_SET
20150 +};
20151 +
20152 +struct au_sxattr {
20153 +       int type;
20154 +       union {
20155 +               struct {
20156 +                       const char      *name;
20157 +                       const void      *value;
20158 +                       size_t          size;
20159 +                       int             flags;
20160 +               } set;
20161 +               struct {
20162 +                       struct posix_acl *acl;
20163 +                       int             type;
20164 +               } acl_set;
20165 +       } u;
20166 +};
20167 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20168 +                 struct au_sxattr *arg);
20169 +#endif
20170 +
20171 +/* ---------------------------------------------------------------------- */
20172 +
20173 +/* lock subclass for iinfo */
20174 +enum {
20175 +       AuLsc_II_CHILD,         /* child first */
20176 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
20177 +       AuLsc_II_CHILD3,        /* copyup dirs */
20178 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
20179 +       AuLsc_II_PARENT2,
20180 +       AuLsc_II_PARENT3,       /* copyup dirs */
20181 +       AuLsc_II_NEW_CHILD
20182 +};
20183 +
20184 +/*
20185 + * ii_read_lock_child, ii_write_lock_child,
20186 + * ii_read_lock_child2, ii_write_lock_child2,
20187 + * ii_read_lock_child3, ii_write_lock_child3,
20188 + * ii_read_lock_parent, ii_write_lock_parent,
20189 + * ii_read_lock_parent2, ii_write_lock_parent2,
20190 + * ii_read_lock_parent3, ii_write_lock_parent3,
20191 + * ii_read_lock_new_child, ii_write_lock_new_child,
20192 + */
20193 +#define AuReadLockFunc(name, lsc) \
20194 +static inline void ii_read_lock_##name(struct inode *i) \
20195 +{ \
20196 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20197 +}
20198 +
20199 +#define AuWriteLockFunc(name, lsc) \
20200 +static inline void ii_write_lock_##name(struct inode *i) \
20201 +{ \
20202 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20203 +}
20204 +
20205 +#define AuRWLockFuncs(name, lsc) \
20206 +       AuReadLockFunc(name, lsc) \
20207 +       AuWriteLockFunc(name, lsc)
20208 +
20209 +AuRWLockFuncs(child, CHILD);
20210 +AuRWLockFuncs(child2, CHILD2);
20211 +AuRWLockFuncs(child3, CHILD3);
20212 +AuRWLockFuncs(parent, PARENT);
20213 +AuRWLockFuncs(parent2, PARENT2);
20214 +AuRWLockFuncs(parent3, PARENT3);
20215 +AuRWLockFuncs(new_child, NEW_CHILD);
20216 +
20217 +#undef AuReadLockFunc
20218 +#undef AuWriteLockFunc
20219 +#undef AuRWLockFuncs
20220 +
20221 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20222 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20223 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
20224 +
20225 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20226 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20227 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
20228 +
20229 +/* ---------------------------------------------------------------------- */
20230 +
20231 +static inline void au_icntnr_init(struct au_icntnr *c)
20232 +{
20233 +#ifdef CONFIG_AUFS_DEBUG
20234 +       c->vfs_inode.i_mode = 0;
20235 +#endif
20236 +}
20237 +
20238 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
20239 +{
20240 +       unsigned int gen;
20241 +       struct au_iinfo *iinfo;
20242 +       struct au_iigen *iigen;
20243 +
20244 +       iinfo = au_ii(inode);
20245 +       iigen = &iinfo->ii_generation;
20246 +       spin_lock(&iigen->ig_spin);
20247 +       if (igflags)
20248 +               *igflags = iigen->ig_flags;
20249 +       gen = iigen->ig_generation;
20250 +       spin_unlock(&iigen->ig_spin);
20251 +
20252 +       return gen;
20253 +}
20254 +
20255 +/* tiny test for inode number */
20256 +/* tmpfs generation is too rough */
20257 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20258 +{
20259 +       struct au_iinfo *iinfo;
20260 +
20261 +       iinfo = au_ii(inode);
20262 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
20263 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
20264 +                && iinfo->ii_higen == h_inode->i_generation);
20265 +}
20266 +
20267 +static inline void au_iigen_dec(struct inode *inode)
20268 +{
20269 +       struct au_iinfo *iinfo;
20270 +       struct au_iigen *iigen;
20271 +
20272 +       iinfo = au_ii(inode);
20273 +       iigen = &iinfo->ii_generation;
20274 +       spin_lock(&iigen->ig_spin);
20275 +       iigen->ig_generation--;
20276 +       spin_unlock(&iigen->ig_spin);
20277 +}
20278 +
20279 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20280 +{
20281 +       int err;
20282 +
20283 +       err = 0;
20284 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
20285 +               err = -EIO;
20286 +
20287 +       return err;
20288 +}
20289 +
20290 +/* ---------------------------------------------------------------------- */
20291 +
20292 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20293 +                                         aufs_bindex_t bindex)
20294 +{
20295 +       return iinfo->ii_hinode + bindex;
20296 +}
20297 +
20298 +static inline int au_is_bad_inode(struct inode *inode)
20299 +{
20300 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20301 +}
20302 +
20303 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20304 +                                       aufs_bindex_t bindex)
20305 +{
20306 +       IiMustAnyLock(inode);
20307 +       return au_hinode(au_ii(inode), bindex)->hi_id;
20308 +}
20309 +
20310 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
20311 +{
20312 +       IiMustAnyLock(inode);
20313 +       return au_ii(inode)->ii_btop;
20314 +}
20315 +
20316 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
20317 +{
20318 +       IiMustAnyLock(inode);
20319 +       return au_ii(inode)->ii_bbot;
20320 +}
20321 +
20322 +static inline struct au_vdir *au_ivdir(struct inode *inode)
20323 +{
20324 +       IiMustAnyLock(inode);
20325 +       return au_ii(inode)->ii_vdir;
20326 +}
20327 +
20328 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20329 +{
20330 +       IiMustAnyLock(inode);
20331 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
20332 +}
20333 +
20334 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
20335 +{
20336 +       IiMustWriteLock(inode);
20337 +       au_ii(inode)->ii_btop = bindex;
20338 +}
20339 +
20340 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
20341 +{
20342 +       IiMustWriteLock(inode);
20343 +       au_ii(inode)->ii_bbot = bindex;
20344 +}
20345 +
20346 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20347 +{
20348 +       IiMustWriteLock(inode);
20349 +       au_ii(inode)->ii_vdir = vdir;
20350 +}
20351 +
20352 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
20353 +{
20354 +       IiMustAnyLock(inode);
20355 +       return au_hinode(au_ii(inode), bindex);
20356 +}
20357 +
20358 +/* ---------------------------------------------------------------------- */
20359 +
20360 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20361 +{
20362 +       if (pin)
20363 +               return pin->parent;
20364 +       return NULL;
20365 +}
20366 +
20367 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
20368 +{
20369 +       if (pin && pin->hdir)
20370 +               return pin->hdir->hi_inode;
20371 +       return NULL;
20372 +}
20373 +
20374 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20375 +{
20376 +       if (pin)
20377 +               return pin->hdir;
20378 +       return NULL;
20379 +}
20380 +
20381 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
20382 +{
20383 +       if (pin)
20384 +               pin->dentry = dentry;
20385 +}
20386 +
20387 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20388 +                                          unsigned char lflag)
20389 +{
20390 +       if (pin) {
20391 +               if (lflag)
20392 +                       au_fset_pin(pin->flags, DI_LOCKED);
20393 +               else
20394 +                       au_fclr_pin(pin->flags, DI_LOCKED);
20395 +       }
20396 +}
20397 +
20398 +#if 0 /* reserved */
20399 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20400 +{
20401 +       if (pin) {
20402 +               dput(pin->parent);
20403 +               pin->parent = dget(parent);
20404 +       }
20405 +}
20406 +#endif
20407 +
20408 +/* ---------------------------------------------------------------------- */
20409 +
20410 +struct au_branch;
20411 +#ifdef CONFIG_AUFS_HNOTIFY
20412 +struct au_hnotify_op {
20413 +       void (*ctl)(struct au_hinode *hinode, int do_set);
20414 +       int (*alloc)(struct au_hinode *hinode);
20415 +
20416 +       /*
20417 +        * if it returns true, the caller should free hinode->hi_notify,
20418 +        * otherwise ->free() frees it.
20419 +        */
20420 +       int (*free)(struct au_hinode *hinode,
20421 +                   struct au_hnotify *hn) __must_check;
20422 +
20423 +       void (*fin)(void);
20424 +       int (*init)(void);
20425 +
20426 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20427 +       void (*fin_br)(struct au_branch *br);
20428 +       int (*init_br)(struct au_branch *br, int perm);
20429 +};
20430 +
20431 +/* hnotify.c */
20432 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
20433 +void au_hn_free(struct au_hinode *hinode);
20434 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
20435 +void au_hn_reset(struct inode *inode, unsigned int flags);
20436 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
20437 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
20438 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20439 +int au_hnotify_init_br(struct au_branch *br, int perm);
20440 +void au_hnotify_fin_br(struct au_branch *br);
20441 +int __init au_hnotify_init(void);
20442 +void au_hnotify_fin(void);
20443 +
20444 +/* hfsnotify.c */
20445 +extern const struct au_hnotify_op au_hnotify_op;
20446 +
20447 +static inline
20448 +void au_hn_init(struct au_hinode *hinode)
20449 +{
20450 +       hinode->hi_notify = NULL;
20451 +}
20452 +
20453 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20454 +{
20455 +       return hinode->hi_notify;
20456 +}
20457 +
20458 +#else
20459 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20460 +       struct au_hinode *hinode __maybe_unused,
20461 +       struct inode *inode __maybe_unused)
20462 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
20463 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20464 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20465 +          int do_set __maybe_unused)
20466 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20467 +          unsigned int flags __maybe_unused)
20468 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20469 +          struct au_branch *br __maybe_unused,
20470 +          int perm __maybe_unused)
20471 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20472 +          int perm __maybe_unused)
20473 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
20474 +AuStubInt0(__init au_hnotify_init, void)
20475 +AuStubVoid(au_hnotify_fin, void)
20476 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20477 +#endif /* CONFIG_AUFS_HNOTIFY */
20478 +
20479 +static inline void au_hn_suspend(struct au_hinode *hdir)
20480 +{
20481 +       au_hn_ctl(hdir, /*do_set*/0);
20482 +}
20483 +
20484 +static inline void au_hn_resume(struct au_hinode *hdir)
20485 +{
20486 +       au_hn_ctl(hdir, /*do_set*/1);
20487 +}
20488 +
20489 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
20490 +{
20491 +       inode_lock(hdir->hi_inode);
20492 +       au_hn_suspend(hdir);
20493 +}
20494 +
20495 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
20496 +                                         unsigned int sc __maybe_unused)
20497 +{
20498 +       inode_lock_nested(hdir->hi_inode, sc);
20499 +       au_hn_suspend(hdir);
20500 +}
20501 +
20502 +#if 0 /* unused */
20503 +#include "vfsub.h"
20504 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20505 +                                                 unsigned int sc)
20506 +{
20507 +       inode_lock_shared_nested(hdir->hi_inode, sc);
20508 +       au_hn_suspend(hdir);
20509 +}
20510 +#endif
20511 +
20512 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
20513 +{
20514 +       au_hn_resume(hdir);
20515 +       inode_unlock(hdir->hi_inode);
20516 +}
20517 +
20518 +#endif /* __KERNEL__ */
20519 +#endif /* __AUFS_INODE_H__ */
20520 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
20521 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
20522 +++ linux/fs/aufs/ioctl.c       2022-11-05 23:02:18.965889284 +0100
20523 @@ -0,0 +1,220 @@
20524 +// SPDX-License-Identifier: GPL-2.0
20525 +/*
20526 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20527 + *
20528 + * This program is free software; you can redistribute it and/or modify
20529 + * it under the terms of the GNU General Public License as published by
20530 + * the Free Software Foundation; either version 2 of the License, or
20531 + * (at your option) any later version.
20532 + *
20533 + * This program is distributed in the hope that it will be useful,
20534 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20535 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20536 + * GNU General Public License for more details.
20537 + *
20538 + * You should have received a copy of the GNU General Public License
20539 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20540 + */
20541 +
20542 +/*
20543 + * ioctl
20544 + * plink-management and readdir in userspace.
20545 + * assist the pathconf(3) wrapper library.
20546 + * move-down
20547 + * File-based Hierarchical Storage Management.
20548 + */
20549 +
20550 +#include <linux/compat.h>
20551 +#include <linux/file.h>
20552 +#include "aufs.h"
20553 +
20554 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
20555 +{
20556 +       int err, fd;
20557 +       aufs_bindex_t wbi, bindex, bbot;
20558 +       struct file *h_file;
20559 +       struct super_block *sb;
20560 +       struct dentry *root;
20561 +       struct au_branch *br;
20562 +       struct aufs_wbr_fd wbrfd = {
20563 +               .oflags = au_dir_roflags,
20564 +               .brid   = -1
20565 +       };
20566 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20567 +               | O_NOATIME | O_CLOEXEC;
20568 +
20569 +       AuDebugOn(wbrfd.oflags & ~valid);
20570 +
20571 +       if (arg) {
20572 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20573 +               if (unlikely(err)) {
20574 +                       err = -EFAULT;
20575 +                       goto out;
20576 +               }
20577 +
20578 +               err = -EINVAL;
20579 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20580 +               wbrfd.oflags |= au_dir_roflags;
20581 +               AuDbg("0%o\n", wbrfd.oflags);
20582 +               if (unlikely(wbrfd.oflags & ~valid))
20583 +                       goto out;
20584 +       }
20585 +
20586 +       fd = get_unused_fd_flags(0);
20587 +       err = fd;
20588 +       if (unlikely(fd < 0))
20589 +               goto out;
20590 +
20591 +       h_file = ERR_PTR(-EINVAL);
20592 +       wbi = 0;
20593 +       br = NULL;
20594 +       sb = path->dentry->d_sb;
20595 +       root = sb->s_root;
20596 +       aufs_read_lock(root, AuLock_IR);
20597 +       bbot = au_sbbot(sb);
20598 +       if (wbrfd.brid >= 0) {
20599 +               wbi = au_br_index(sb, wbrfd.brid);
20600 +               if (unlikely(wbi < 0 || wbi > bbot))
20601 +                       goto out_unlock;
20602 +       }
20603 +
20604 +       h_file = ERR_PTR(-ENOENT);
20605 +       br = au_sbr(sb, wbi);
20606 +       if (!au_br_writable(br->br_perm)) {
20607 +               if (arg)
20608 +                       goto out_unlock;
20609 +
20610 +               bindex = wbi + 1;
20611 +               wbi = -1;
20612 +               for (; bindex <= bbot; bindex++) {
20613 +                       br = au_sbr(sb, bindex);
20614 +                       if (au_br_writable(br->br_perm)) {
20615 +                               wbi = bindex;
20616 +                               br = au_sbr(sb, wbi);
20617 +                               break;
20618 +                       }
20619 +               }
20620 +       }
20621 +       AuDbg("wbi %d\n", wbi);
20622 +       if (wbi >= 0)
20623 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20624 +                                  /*force_wr*/0);
20625 +
20626 +out_unlock:
20627 +       aufs_read_unlock(root, AuLock_IR);
20628 +       err = PTR_ERR(h_file);
20629 +       if (IS_ERR(h_file))
20630 +               goto out_fd;
20631 +
20632 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
20633 +       fd_install(fd, h_file);
20634 +       err = fd;
20635 +       goto out; /* success */
20636 +
20637 +out_fd:
20638 +       put_unused_fd(fd);
20639 +out:
20640 +       AuTraceErr(err);
20641 +       return err;
20642 +}
20643 +
20644 +/* ---------------------------------------------------------------------- */
20645 +
20646 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20647 +{
20648 +       long err;
20649 +       struct dentry *dentry;
20650 +
20651 +       switch (cmd) {
20652 +       case AUFS_CTL_RDU:
20653 +       case AUFS_CTL_RDU_INO:
20654 +               err = au_rdu_ioctl(file, cmd, arg);
20655 +               break;
20656 +
20657 +       case AUFS_CTL_WBR_FD:
20658 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20659 +               break;
20660 +
20661 +       case AUFS_CTL_IBUSY:
20662 +               err = au_ibusy_ioctl(file, arg);
20663 +               break;
20664 +
20665 +       case AUFS_CTL_BRINFO:
20666 +               err = au_brinfo_ioctl(file, arg);
20667 +               break;
20668 +
20669 +       case AUFS_CTL_FHSM_FD:
20670 +               dentry = file->f_path.dentry;
20671 +               if (IS_ROOT(dentry))
20672 +                       err = au_fhsm_fd(dentry->d_sb, arg);
20673 +               else
20674 +                       err = -ENOTTY;
20675 +               break;
20676 +
20677 +       default:
20678 +               /* do not call the lower */
20679 +               AuDbg("0x%x\n", cmd);
20680 +               err = -ENOTTY;
20681 +       }
20682 +
20683 +       AuTraceErr(err);
20684 +       return err;
20685 +}
20686 +
20687 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20688 +{
20689 +       long err;
20690 +
20691 +       switch (cmd) {
20692 +       case AUFS_CTL_MVDOWN:
20693 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
20694 +               break;
20695 +
20696 +       case AUFS_CTL_WBR_FD:
20697 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20698 +               break;
20699 +
20700 +       default:
20701 +               /* do not call the lower */
20702 +               AuDbg("0x%x\n", cmd);
20703 +               err = -ENOTTY;
20704 +       }
20705 +
20706 +       AuTraceErr(err);
20707 +       return err;
20708 +}
20709 +
20710 +#ifdef CONFIG_COMPAT
20711 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20712 +                          unsigned long arg)
20713 +{
20714 +       long err;
20715 +
20716 +       switch (cmd) {
20717 +       case AUFS_CTL_RDU:
20718 +       case AUFS_CTL_RDU_INO:
20719 +               err = au_rdu_compat_ioctl(file, cmd, arg);
20720 +               break;
20721 +
20722 +       case AUFS_CTL_IBUSY:
20723 +               err = au_ibusy_compat_ioctl(file, arg);
20724 +               break;
20725 +
20726 +       case AUFS_CTL_BRINFO:
20727 +               err = au_brinfo_compat_ioctl(file, arg);
20728 +               break;
20729 +
20730 +       default:
20731 +               err = aufs_ioctl_dir(file, cmd, arg);
20732 +       }
20733 +
20734 +       AuTraceErr(err);
20735 +       return err;
20736 +}
20737 +
20738 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20739 +                             unsigned long arg)
20740 +{
20741 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20742 +}
20743 +#endif
20744 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
20745 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
20746 +++ linux/fs/aufs/i_op_add.c    2023-04-24 08:13:40.969576115 +0200
20747 @@ -0,0 +1,972 @@
20748 +// SPDX-License-Identifier: GPL-2.0
20749 +/*
20750 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20751 + *
20752 + * This program is free software; you can redistribute it and/or modify
20753 + * it under the terms of the GNU General Public License as published by
20754 + * the Free Software Foundation; either version 2 of the License, or
20755 + * (at your option) any later version.
20756 + *
20757 + * This program is distributed in the hope that it will be useful,
20758 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20759 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20760 + * GNU General Public License for more details.
20761 + *
20762 + * You should have received a copy of the GNU General Public License
20763 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20764 + */
20765 +
20766 +/*
20767 + * inode operations (add entry)
20768 + */
20769 +
20770 +#include <linux/iversion.h>
20771 +#include "aufs.h"
20772 +
20773 +/*
20774 + * final procedure of adding a new entry, except link(2).
20775 + * remove whiteout, instantiate, copyup the parent dir's times and size
20776 + * and update version.
20777 + * if it failed, re-create the removed whiteout.
20778 + */
20779 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20780 +                 struct dentry *wh_dentry, struct dentry *dentry)
20781 +{
20782 +       int err, rerr;
20783 +       aufs_bindex_t bwh;
20784 +       struct path h_path;
20785 +       struct super_block *sb;
20786 +       struct inode *inode, *h_dir;
20787 +       struct dentry *wh;
20788 +
20789 +       bwh = -1;
20790 +       sb = dir->i_sb;
20791 +       if (wh_dentry) {
20792 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20793 +               IMustLock(h_dir);
20794 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20795 +               bwh = au_dbwh(dentry);
20796 +               h_path.dentry = wh_dentry;
20797 +               h_path.mnt = au_sbr_mnt(sb, bindex);
20798 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20799 +                                         dentry);
20800 +               if (unlikely(err))
20801 +                       goto out;
20802 +       }
20803 +
20804 +       inode = au_new_inode(dentry, /*must_new*/1);
20805 +       if (!IS_ERR(inode)) {
20806 +               d_instantiate(dentry, inode);
20807 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
20808 +               IMustLock(dir);
20809 +               au_dir_ts(dir, bindex);
20810 +               inode_inc_iversion(dir);
20811 +               au_fhsm_wrote(sb, bindex, /*force*/0);
20812 +               return 0; /* success */
20813 +       }
20814 +
20815 +       err = PTR_ERR(inode);
20816 +       if (!wh_dentry)
20817 +               goto out;
20818 +
20819 +       /* revert */
20820 +       /* dir inode is locked */
20821 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20822 +       rerr = PTR_ERR(wh);
20823 +       if (IS_ERR(wh)) {
20824 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20825 +                       dentry, err, rerr);
20826 +               err = -EIO;
20827 +       } else
20828 +               dput(wh);
20829 +
20830 +out:
20831 +       return err;
20832 +}
20833 +
20834 +static int au_d_may_add(struct dentry *dentry)
20835 +{
20836 +       int err;
20837 +
20838 +       err = 0;
20839 +       if (unlikely(d_unhashed(dentry)))
20840 +               err = -ENOENT;
20841 +       if (unlikely(d_really_is_positive(dentry)))
20842 +               err = -EEXIST;
20843 +       return err;
20844 +}
20845 +
20846 +/*
20847 + * simple tests for the adding inode operations.
20848 + * following the checks in vfs, plus the parent-child relationship.
20849 + */
20850 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20851 +              struct dentry *h_parent, int isdir)
20852 +{
20853 +       int err;
20854 +       umode_t h_mode;
20855 +       struct dentry *h_dentry;
20856 +       struct inode *h_inode;
20857 +
20858 +       err = -ENAMETOOLONG;
20859 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20860 +               goto out;
20861 +
20862 +       h_dentry = au_h_dptr(dentry, bindex);
20863 +       if (d_really_is_negative(dentry)) {
20864 +               err = -EEXIST;
20865 +               if (unlikely(d_is_positive(h_dentry)))
20866 +                       goto out;
20867 +       } else {
20868 +               /* rename(2) case */
20869 +               err = -EIO;
20870 +               if (unlikely(d_is_negative(h_dentry)))
20871 +                       goto out;
20872 +               h_inode = d_inode(h_dentry);
20873 +               if (unlikely(!h_inode->i_nlink))
20874 +                       goto out;
20875 +
20876 +               h_mode = h_inode->i_mode;
20877 +               if (!isdir) {
20878 +                       err = -EISDIR;
20879 +                       if (unlikely(S_ISDIR(h_mode)))
20880 +                               goto out;
20881 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20882 +                       err = -ENOTDIR;
20883 +                       goto out;
20884 +               }
20885 +       }
20886 +
20887 +       err = 0;
20888 +       /* expected parent dir is locked */
20889 +       if (unlikely(h_parent != h_dentry->d_parent))
20890 +               err = -EIO;
20891 +
20892 +out:
20893 +       AuTraceErr(err);
20894 +       return err;
20895 +}
20896 +
20897 +/*
20898 + * initial procedure of adding a new entry.
20899 + * prepare writable branch and the parent dir, lock it,
20900 + * and lookup whiteout for the new entry.
20901 + */
20902 +static struct dentry*
20903 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20904 +                 struct dentry *src_dentry, struct au_pin *pin,
20905 +                 struct au_wr_dir_args *wr_dir_args)
20906 +{
20907 +       struct dentry *wh_dentry, *h_parent;
20908 +       struct super_block *sb;
20909 +       struct au_branch *br;
20910 +       int err;
20911 +       unsigned int udba;
20912 +       aufs_bindex_t bcpup;
20913 +
20914 +       AuDbg("%pd\n", dentry);
20915 +
20916 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20917 +       bcpup = err;
20918 +       wh_dentry = ERR_PTR(err);
20919 +       if (unlikely(err < 0))
20920 +               goto out;
20921 +
20922 +       sb = dentry->d_sb;
20923 +       udba = au_opt_udba(sb);
20924 +       err = au_pin(pin, dentry, bcpup, udba,
20925 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20926 +       wh_dentry = ERR_PTR(err);
20927 +       if (unlikely(err))
20928 +               goto out;
20929 +
20930 +       h_parent = au_pinned_h_parent(pin);
20931 +       if (udba != AuOpt_UDBA_NONE
20932 +           && au_dbtop(dentry) == bcpup)
20933 +               err = au_may_add(dentry, bcpup, h_parent,
20934 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20935 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20936 +               err = -ENAMETOOLONG;
20937 +       wh_dentry = ERR_PTR(err);
20938 +       if (unlikely(err))
20939 +               goto out_unpin;
20940 +
20941 +       br = au_sbr(sb, bcpup);
20942 +       if (dt) {
20943 +               struct path tmp = {
20944 +                       .dentry = h_parent,
20945 +                       .mnt    = au_br_mnt(br)
20946 +               };
20947 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20948 +       }
20949 +
20950 +       wh_dentry = NULL;
20951 +       if (bcpup != au_dbwh(dentry))
20952 +               goto out; /* success */
20953 +
20954 +       /*
20955 +        * ENAMETOOLONG here means that if we allowed create such name, then it
20956 +        * would not be able to removed in the future. So we don't allow such
20957 +        * name here and we don't handle ENAMETOOLONG differently here.
20958 +        */
20959 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20960 +
20961 +out_unpin:
20962 +       if (IS_ERR(wh_dentry))
20963 +               au_unpin(pin);
20964 +out:
20965 +       return wh_dentry;
20966 +}
20967 +
20968 +/* ---------------------------------------------------------------------- */
20969 +
20970 +enum { Mknod, Symlink, Creat };
20971 +struct simple_arg {
20972 +       int type;
20973 +       union {
20974 +               struct {
20975 +                       umode_t                 mode;
20976 +                       bool                    want_excl;
20977 +                       bool                    try_aopen;
20978 +                       struct vfsub_aopen_args *aopen;
20979 +               } c;
20980 +               struct {
20981 +                       const char *symname;
20982 +               } s;
20983 +               struct {
20984 +                       umode_t mode;
20985 +                       dev_t dev;
20986 +               } m;
20987 +       } u;
20988 +};
20989 +
20990 +static int add_simple(struct inode *dir, struct dentry *dentry,
20991 +                     struct simple_arg *arg)
20992 +{
20993 +       int err, rerr;
20994 +       aufs_bindex_t btop;
20995 +       unsigned char created;
20996 +       const unsigned char try_aopen
20997 +               = (arg->type == Creat && arg->u.c.try_aopen);
20998 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
20999 +       struct dentry *wh_dentry, *parent;
21000 +       struct inode *h_dir;
21001 +       struct super_block *sb;
21002 +       struct au_branch *br;
21003 +       /* to reduce stack size */
21004 +       struct {
21005 +               struct au_dtime dt;
21006 +               struct au_pin pin;
21007 +               struct path h_path;
21008 +               struct au_wr_dir_args wr_dir_args;
21009 +       } *a;
21010 +
21011 +       AuDbg("%pd\n", dentry);
21012 +       IMustLock(dir);
21013 +
21014 +       err = -ENOMEM;
21015 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21016 +       if (unlikely(!a))
21017 +               goto out;
21018 +       a->wr_dir_args.force_btgt = -1;
21019 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21020 +
21021 +       parent = dentry->d_parent; /* dir inode is locked */
21022 +       if (!try_aopen) {
21023 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21024 +               if (unlikely(err))
21025 +                       goto out_free;
21026 +       }
21027 +       err = au_d_may_add(dentry);
21028 +       if (unlikely(err))
21029 +               goto out_unlock;
21030 +       if (!try_aopen)
21031 +               di_write_lock_parent(parent);
21032 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21033 +                                     &a->pin, &a->wr_dir_args);
21034 +       err = PTR_ERR(wh_dentry);
21035 +       if (IS_ERR(wh_dentry))
21036 +               goto out_parent;
21037 +
21038 +       btop = au_dbtop(dentry);
21039 +       sb = dentry->d_sb;
21040 +       br = au_sbr(sb, btop);
21041 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21042 +       a->h_path.mnt = au_br_mnt(br);
21043 +       h_dir = au_pinned_h_dir(&a->pin);
21044 +       switch (arg->type) {
21045 +       case Creat:
21046 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
21047 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21048 +                                          arg->u.c.want_excl);
21049 +                       created = !err;
21050 +                       if (!err && try_aopen)
21051 +                               aopen->file->f_mode |= FMODE_CREATED;
21052 +               } else {
21053 +                       aopen->br = br;
21054 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21055 +                       AuDbg("err %d\n", err);
21056 +                       AuDbgFile(aopen->file);
21057 +                       created = err >= 0
21058 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
21059 +               }
21060 +               break;
21061 +       case Symlink:
21062 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
21063 +               created = !err;
21064 +               break;
21065 +       case Mknod:
21066 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21067 +                                 arg->u.m.dev);
21068 +               created = !err;
21069 +               break;
21070 +       default:
21071 +               BUG();
21072 +       }
21073 +       if (unlikely(err < 0))
21074 +               goto out_unpin;
21075 +
21076 +       err = epilog(dir, btop, wh_dentry, dentry);
21077 +       if (!err)
21078 +               goto out_unpin; /* success */
21079 +
21080 +       /* revert */
21081 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
21082 +               /* no delegation since it is just created */
21083 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21084 +                                   /*force*/0);
21085 +               if (rerr) {
21086 +                       AuIOErr("%pd revert failure(%d, %d)\n",
21087 +                               dentry, err, rerr);
21088 +                       err = -EIO;
21089 +               }
21090 +               au_dtime_revert(&a->dt);
21091 +       }
21092 +       if (try_aopen && h_dir->i_op->atomic_open
21093 +           && (aopen->file->f_mode & FMODE_OPENED))
21094 +               /* aopen->file is still opened */
21095 +               au_lcnt_dec(&aopen->br->br_nfiles);
21096 +
21097 +out_unpin:
21098 +       au_unpin(&a->pin);
21099 +       dput(wh_dentry);
21100 +out_parent:
21101 +       if (!try_aopen)
21102 +               di_write_unlock(parent);
21103 +out_unlock:
21104 +       if (unlikely(err)) {
21105 +               au_update_dbtop(dentry);
21106 +               d_drop(dentry);
21107 +       }
21108 +       if (!try_aopen)
21109 +               aufs_read_unlock(dentry, AuLock_DW);
21110 +out_free:
21111 +       au_kfree_rcu(a);
21112 +out:
21113 +       return err;
21114 +}
21115 +
21116 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
21117 +              struct dentry *dentry, umode_t mode, dev_t dev)
21118 +{
21119 +       struct simple_arg arg = {
21120 +               .type = Mknod,
21121 +               .u.m = {
21122 +                       .mode   = mode,
21123 +                       .dev    = dev
21124 +               }
21125 +       };
21126 +       return add_simple(dir, dentry, &arg);
21127 +}
21128 +
21129 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
21130 +                struct dentry *dentry, const char *symname)
21131 +{
21132 +       struct simple_arg arg = {
21133 +               .type = Symlink,
21134 +               .u.s.symname = symname
21135 +       };
21136 +       return add_simple(dir, dentry, &arg);
21137 +}
21138 +
21139 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
21140 +               struct dentry *dentry, umode_t mode, bool want_excl)
21141 +{
21142 +       struct simple_arg arg = {
21143 +               .type = Creat,
21144 +               .u.c = {
21145 +                       .mode           = mode,
21146 +                       .want_excl      = want_excl
21147 +               }
21148 +       };
21149 +       return add_simple(dir, dentry, &arg);
21150 +}
21151 +
21152 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21153 +                      struct vfsub_aopen_args *aopen_args)
21154 +{
21155 +       struct simple_arg arg = {
21156 +               .type = Creat,
21157 +               .u.c = {
21158 +                       .mode           = aopen_args->create_mode,
21159 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
21160 +                       .try_aopen      = true,
21161 +                       .aopen          = aopen_args
21162 +               }
21163 +       };
21164 +       return add_simple(dir, dentry, &arg);
21165 +}
21166 +
21167 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
21168 +                struct file *file, umode_t mode)
21169 +{
21170 +       int err;
21171 +       aufs_bindex_t bindex;
21172 +       struct path h_ppath;
21173 +       struct super_block *sb;
21174 +       struct au_branch *br;
21175 +       struct dentry *dentry, *parent, *h_parent, *h_dentry;
21176 +       struct inode *h_dir, *inode;
21177 +       struct vfsmount *h_mnt;
21178 +       struct mnt_idmap *h_idmap;
21179 +       struct file *h_file;
21180 +       struct au_wr_dir_args wr_dir_args = {
21181 +               .force_btgt     = -1,
21182 +               .flags          = AuWrDir_TMPFILE
21183 +       };
21184 +
21185 +       /* copy-up may happen */
21186 +       inode_lock(dir);
21187 +
21188 +       h_file = NULL;
21189 +       sb = dir->i_sb;
21190 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21191 +       if (unlikely(err))
21192 +               goto out;
21193 +
21194 +       dentry = file->f_path.dentry;
21195 +       err = au_di_init(dentry);
21196 +       if (unlikely(err))
21197 +               goto out_si;
21198 +
21199 +       err = -EBUSY;
21200 +       parent = d_find_any_alias(dir);
21201 +       AuDebugOn(!parent);
21202 +       di_write_lock_parent(parent);
21203 +       if (unlikely(d_inode(parent) != dir))
21204 +               goto out_parent;
21205 +
21206 +       err = au_digen_test(parent, au_sigen(sb));
21207 +       if (unlikely(err))
21208 +               goto out_parent;
21209 +
21210 +       bindex = au_dbtop(parent);
21211 +       au_set_dbtop(dentry, bindex);
21212 +       au_set_dbbot(dentry, bindex);
21213 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21214 +       bindex = err;
21215 +       if (unlikely(err < 0))
21216 +               goto out_parent;
21217 +
21218 +       err = -EOPNOTSUPP;
21219 +       h_dir = au_h_iptr(dir, bindex);
21220 +       if (unlikely(!h_dir->i_op->tmpfile))
21221 +               goto out_parent;
21222 +
21223 +       br = au_sbr(sb, bindex);
21224 +       h_mnt = au_br_mnt(br);
21225 +       err = vfsub_mnt_want_write(h_mnt);
21226 +       if (unlikely(err))
21227 +               goto out_parent;
21228 +
21229 +       h_idmap = mnt_idmap(h_mnt);
21230 +       h_parent = au_h_dptr(parent, bindex);
21231 +       h_ppath.mnt = h_mnt;
21232 +       h_ppath.dentry = h_parent;
21233 +       h_file = vfs_tmpfile_open(h_idmap, &h_ppath, mode, /*open_flag*/0,
21234 +                                 current_cred());
21235 +       if (IS_ERR(h_file)) {
21236 +               err = PTR_ERR(h_file);
21237 +               h_file = NULL;
21238 +               goto out_mnt;
21239 +       }
21240 +
21241 +       h_dentry = h_file->f_path.dentry;
21242 +       au_set_dbtop(dentry, bindex);
21243 +       au_set_dbbot(dentry, bindex);
21244 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
21245 +       inode = au_new_inode(dentry, /*must_new*/1);
21246 +       if (IS_ERR(inode)) {
21247 +               err = PTR_ERR(inode);
21248 +               au_set_h_dptr(dentry, bindex, NULL);
21249 +               au_set_dbtop(dentry, -1);
21250 +               au_set_dbbot(dentry, -1);
21251 +               goto out_h_file;
21252 +       }
21253 +
21254 +       if (!inode->i_nlink)
21255 +               set_nlink(inode, 1);
21256 +       d_tmpfile(file, inode);
21257 +       au_di(dentry)->di_tmpfile = 1;
21258 +       get_file(h_file);
21259 +       au_di(dentry)->di_htmpfile = h_file;
21260 +
21261 +       /* update without i_mutex */
21262 +       if (au_ibtop(dir) == au_dbtop(dentry))
21263 +               au_cpup_attr_timesizes(dir);
21264 +
21265 +out_h_file:
21266 +       fput(h_file);
21267 +out_mnt:
21268 +       vfsub_mnt_drop_write(h_mnt);
21269 +out_parent:
21270 +       di_write_unlock(parent);
21271 +       dput(parent);
21272 +       di_write_unlock(dentry);
21273 +       if (!err)
21274 +               goto out_si;
21275 +       if (h_file)
21276 +               fput(h_file);
21277 +       au_di(dentry)->di_htmpfile = NULL;
21278 +       au_di_fin(dentry);
21279 +       dentry->d_fsdata = NULL;
21280 +out_si:
21281 +       si_read_unlock(sb);
21282 +       if (!err && h_file) {
21283 +               /* finally... */
21284 +               err = finish_open_simple(file, err);
21285 +               if (!err)
21286 +                       au_lcnt_inc(&br->br_nfiles);
21287 +               else {
21288 +                       fput(h_file);
21289 +                       au_di(dentry)->di_htmpfile = NULL;
21290 +                       au_di_fin(dentry);
21291 +                       dentry->d_fsdata = NULL;
21292 +               }
21293 +       }
21294 +out:
21295 +       inode_unlock(dir);
21296 +       AuTraceErr(err);
21297 +       return err;
21298 +}
21299 +
21300 +/* ---------------------------------------------------------------------- */
21301 +
21302 +struct au_link_args {
21303 +       aufs_bindex_t bdst, bsrc;
21304 +       struct au_pin pin;
21305 +       struct path h_path;
21306 +       struct dentry *src_parent, *parent;
21307 +};
21308 +
21309 +static int au_cpup_before_link(struct dentry *src_dentry,
21310 +                              struct au_link_args *a)
21311 +{
21312 +       int err;
21313 +       struct dentry *h_src_dentry;
21314 +       struct au_cp_generic cpg = {
21315 +               .dentry = src_dentry,
21316 +               .bdst   = a->bdst,
21317 +               .bsrc   = a->bsrc,
21318 +               .len    = -1,
21319 +               .pin    = &a->pin,
21320 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21321 +       };
21322 +
21323 +       di_read_lock_parent(a->src_parent, AuLock_IR);
21324 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21325 +       if (unlikely(err))
21326 +               goto out;
21327 +
21328 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
21329 +       err = au_pin(&a->pin, src_dentry, a->bdst,
21330 +                    au_opt_udba(src_dentry->d_sb),
21331 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21332 +       if (unlikely(err))
21333 +               goto out;
21334 +
21335 +       err = au_sio_cpup_simple(&cpg);
21336 +       au_unpin(&a->pin);
21337 +
21338 +out:
21339 +       di_read_unlock(a->src_parent, AuLock_IR);
21340 +       return err;
21341 +}
21342 +
21343 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21344 +                          struct au_link_args *a)
21345 +{
21346 +       int err;
21347 +       unsigned char plink;
21348 +       aufs_bindex_t bbot;
21349 +       struct dentry *h_src_dentry;
21350 +       struct inode *h_inode, *inode, *delegated;
21351 +       struct super_block *sb;
21352 +       struct file *h_file;
21353 +
21354 +       plink = 0;
21355 +       h_inode = NULL;
21356 +       sb = src_dentry->d_sb;
21357 +       inode = d_inode(src_dentry);
21358 +       if (au_ibtop(inode) <= a->bdst)
21359 +               h_inode = au_h_iptr(inode, a->bdst);
21360 +       if (!h_inode || !h_inode->i_nlink) {
21361 +               /* copyup src_dentry as the name of dentry. */
21362 +               bbot = au_dbbot(dentry);
21363 +               if (bbot < a->bsrc)
21364 +                       au_set_dbbot(dentry, a->bsrc);
21365 +               au_set_h_dptr(dentry, a->bsrc,
21366 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
21367 +               dget(a->h_path.dentry);
21368 +               au_set_h_dptr(dentry, a->bdst, NULL);
21369 +               AuDbg("temporary d_inode...\n");
21370 +               spin_lock(&dentry->d_lock);
21371 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
21372 +               spin_unlock(&dentry->d_lock);
21373 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
21374 +               if (IS_ERR(h_file))
21375 +                       err = PTR_ERR(h_file);
21376 +               else {
21377 +                       struct au_cp_generic cpg = {
21378 +                               .dentry = dentry,
21379 +                               .bdst   = a->bdst,
21380 +                               .bsrc   = -1,
21381 +                               .len    = -1,
21382 +                               .pin    = &a->pin,
21383 +                               .flags  = AuCpup_KEEPLINO
21384 +                       };
21385 +                       err = au_sio_cpup_simple(&cpg);
21386 +                       au_h_open_post(dentry, a->bsrc, h_file);
21387 +                       if (!err) {
21388 +                               dput(a->h_path.dentry);
21389 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21390 +                       } else
21391 +                               au_set_h_dptr(dentry, a->bdst,
21392 +                                             a->h_path.dentry);
21393 +               }
21394 +               spin_lock(&dentry->d_lock);
21395 +               dentry->d_inode = NULL; /* restore */
21396 +               spin_unlock(&dentry->d_lock);
21397 +               AuDbg("temporary d_inode...done\n");
21398 +               au_set_h_dptr(dentry, a->bsrc, NULL);
21399 +               au_set_dbbot(dentry, bbot);
21400 +       } else {
21401 +               /* the inode of src_dentry already exists on a.bdst branch */
21402 +               h_src_dentry = d_find_alias(h_inode);
21403 +               if (!h_src_dentry && au_plink_test(inode)) {
21404 +                       plink = 1;
21405 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
21406 +                       err = PTR_ERR(h_src_dentry);
21407 +                       if (IS_ERR(h_src_dentry))
21408 +                               goto out;
21409 +
21410 +                       if (unlikely(d_is_negative(h_src_dentry))) {
21411 +                               dput(h_src_dentry);
21412 +                               h_src_dentry = NULL;
21413 +                       }
21414 +
21415 +               }
21416 +               if (h_src_dentry) {
21417 +                       delegated = NULL;
21418 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21419 +                                        &a->h_path, &delegated);
21420 +                       if (unlikely(err == -EWOULDBLOCK)) {
21421 +                               pr_warn("cannot retry for NFSv4 delegation"
21422 +                                       " for an internal link\n");
21423 +                               iput(delegated);
21424 +                       }
21425 +                       dput(h_src_dentry);
21426 +               } else {
21427 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
21428 +                               h_inode->i_ino, a->bdst);
21429 +                       err = -EIO;
21430 +               }
21431 +       }
21432 +
21433 +       if (!err && !plink)
21434 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
21435 +
21436 +out:
21437 +       AuTraceErr(err);
21438 +       return err;
21439 +}
21440 +
21441 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
21442 +             struct dentry *dentry)
21443 +{
21444 +       int err, rerr;
21445 +       struct au_dtime dt;
21446 +       struct au_link_args *a;
21447 +       struct dentry *wh_dentry, *h_src_dentry;
21448 +       struct inode *inode, *delegated;
21449 +       struct super_block *sb;
21450 +       struct au_wr_dir_args wr_dir_args = {
21451 +               /* .force_btgt  = -1, */
21452 +               .flags          = AuWrDir_ADD_ENTRY
21453 +       };
21454 +
21455 +       IMustLock(dir);
21456 +       inode = d_inode(src_dentry);
21457 +       IMustLock(inode);
21458 +
21459 +       err = -ENOMEM;
21460 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21461 +       if (unlikely(!a))
21462 +               goto out;
21463 +
21464 +       a->parent = dentry->d_parent; /* dir inode is locked */
21465 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
21466 +                                       AuLock_NOPLM | AuLock_GEN);
21467 +       if (unlikely(err))
21468 +               goto out_kfree;
21469 +       err = au_d_linkable(src_dentry);
21470 +       if (unlikely(err))
21471 +               goto out_unlock;
21472 +       err = au_d_may_add(dentry);
21473 +       if (unlikely(err))
21474 +               goto out_unlock;
21475 +
21476 +       a->src_parent = dget_parent(src_dentry);
21477 +       wr_dir_args.force_btgt = au_ibtop(inode);
21478 +
21479 +       di_write_lock_parent(a->parent);
21480 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21481 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21482 +                                     &wr_dir_args);
21483 +       err = PTR_ERR(wh_dentry);
21484 +       if (IS_ERR(wh_dentry))
21485 +               goto out_parent;
21486 +
21487 +       err = 0;
21488 +       sb = dentry->d_sb;
21489 +       a->bdst = au_dbtop(dentry);
21490 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21491 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21492 +       a->bsrc = au_ibtop(inode);
21493 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21494 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21495 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21496 +       if (!h_src_dentry) {
21497 +               a->bsrc = au_dbtop(src_dentry);
21498 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21499 +               AuDebugOn(!h_src_dentry);
21500 +       } else if (IS_ERR(h_src_dentry)) {
21501 +               err = PTR_ERR(h_src_dentry);
21502 +               goto out_parent;
21503 +       }
21504 +
21505 +       /*
21506 +        * aufs doesn't touch the credential so
21507 +        * security_dentry_create_files_as() is unnecessary.
21508 +        */
21509 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21510 +               if (a->bdst < a->bsrc
21511 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21512 +                       err = au_cpup_or_link(src_dentry, dentry, a);
21513 +               else {
21514 +                       delegated = NULL;
21515 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21516 +                                        &a->h_path, &delegated);
21517 +                       if (unlikely(err == -EWOULDBLOCK)) {
21518 +                               pr_warn("cannot retry for NFSv4 delegation"
21519 +                                       " for an internal link\n");
21520 +                               iput(delegated);
21521 +                       }
21522 +               }
21523 +               dput(h_src_dentry);
21524 +       } else {
21525 +               /*
21526 +                * copyup src_dentry to the branch we process,
21527 +                * and then link(2) to it.
21528 +                */
21529 +               dput(h_src_dentry);
21530 +               if (a->bdst < a->bsrc
21531 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21532 +                       au_unpin(&a->pin);
21533 +                       di_write_unlock(a->parent);
21534 +                       err = au_cpup_before_link(src_dentry, a);
21535 +                       di_write_lock_parent(a->parent);
21536 +                       if (!err)
21537 +                               err = au_pin(&a->pin, dentry, a->bdst,
21538 +                                            au_opt_udba(sb),
21539 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21540 +                       if (unlikely(err))
21541 +                               goto out_wh;
21542 +               }
21543 +               if (!err) {
21544 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21545 +                       err = -ENOENT;
21546 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
21547 +                               delegated = NULL;
21548 +                               err = vfsub_link(h_src_dentry,
21549 +                                                au_pinned_h_dir(&a->pin),
21550 +                                                &a->h_path, &delegated);
21551 +                               if (unlikely(err == -EWOULDBLOCK)) {
21552 +                                       pr_warn("cannot retry"
21553 +                                               " for NFSv4 delegation"
21554 +                                               " for an internal link\n");
21555 +                                       iput(delegated);
21556 +                               }
21557 +                       }
21558 +               }
21559 +       }
21560 +       if (unlikely(err))
21561 +               goto out_unpin;
21562 +
21563 +       if (wh_dentry) {
21564 +               a->h_path.dentry = wh_dentry;
21565 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21566 +                                         dentry);
21567 +               if (unlikely(err))
21568 +                       goto out_revert;
21569 +       }
21570 +
21571 +       au_dir_ts(dir, a->bdst);
21572 +       inode_inc_iversion(dir);
21573 +       inc_nlink(inode);
21574 +       inode->i_ctime = dir->i_ctime;
21575 +       d_instantiate(dentry, au_igrab(inode));
21576 +       if (d_unhashed(a->h_path.dentry))
21577 +               /* some filesystem calls d_drop() */
21578 +               d_drop(dentry);
21579 +       /* some filesystems consume an inode even hardlink */
21580 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
21581 +       goto out_unpin; /* success */
21582 +
21583 +out_revert:
21584 +       /* no delegation since it is just created */
21585 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21586 +                           /*delegated*/NULL, /*force*/0);
21587 +       if (unlikely(rerr)) {
21588 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21589 +               err = -EIO;
21590 +       }
21591 +       au_dtime_revert(&dt);
21592 +out_unpin:
21593 +       au_unpin(&a->pin);
21594 +out_wh:
21595 +       dput(wh_dentry);
21596 +out_parent:
21597 +       di_write_unlock(a->parent);
21598 +       dput(a->src_parent);
21599 +out_unlock:
21600 +       if (unlikely(err)) {
21601 +               au_update_dbtop(dentry);
21602 +               d_drop(dentry);
21603 +       }
21604 +       aufs_read_and_write_unlock2(dentry, src_dentry);
21605 +out_kfree:
21606 +       au_kfree_rcu(a);
21607 +out:
21608 +       AuTraceErr(err);
21609 +       return err;
21610 +}
21611 +
21612 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
21613 +              struct dentry *dentry, umode_t mode)
21614 +{
21615 +       int err, rerr;
21616 +       aufs_bindex_t bindex;
21617 +       unsigned char diropq;
21618 +       struct path h_path;
21619 +       struct dentry *wh_dentry, *parent, *opq_dentry;
21620 +       struct inode *h_inode;
21621 +       struct super_block *sb;
21622 +       struct {
21623 +               struct au_pin pin;
21624 +               struct au_dtime dt;
21625 +       } *a; /* reduce the stack usage */
21626 +       struct au_wr_dir_args wr_dir_args = {
21627 +               .force_btgt     = -1,
21628 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21629 +       };
21630 +
21631 +       IMustLock(dir);
21632 +
21633 +       err = -ENOMEM;
21634 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21635 +       if (unlikely(!a))
21636 +               goto out;
21637 +
21638 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21639 +       if (unlikely(err))
21640 +               goto out_free;
21641 +       err = au_d_may_add(dentry);
21642 +       if (unlikely(err))
21643 +               goto out_unlock;
21644 +
21645 +       parent = dentry->d_parent; /* dir inode is locked */
21646 +       di_write_lock_parent(parent);
21647 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21648 +                                     &a->pin, &wr_dir_args);
21649 +       err = PTR_ERR(wh_dentry);
21650 +       if (IS_ERR(wh_dentry))
21651 +               goto out_parent;
21652 +
21653 +       sb = dentry->d_sb;
21654 +       bindex = au_dbtop(dentry);
21655 +       h_path.dentry = au_h_dptr(dentry, bindex);
21656 +       h_path.mnt = au_sbr_mnt(sb, bindex);
21657 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21658 +       if (unlikely(err))
21659 +               goto out_unpin;
21660 +
21661 +       /* make the dir opaque */
21662 +       diropq = 0;
21663 +       h_inode = d_inode(h_path.dentry);
21664 +       if (wh_dentry
21665 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21666 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21667 +               opq_dentry = au_diropq_create(dentry, bindex);
21668 +               inode_unlock(h_inode);
21669 +               err = PTR_ERR(opq_dentry);
21670 +               if (IS_ERR(opq_dentry))
21671 +                       goto out_dir;
21672 +               dput(opq_dentry);
21673 +               diropq = 1;
21674 +       }
21675 +
21676 +       err = epilog(dir, bindex, wh_dentry, dentry);
21677 +       if (!err) {
21678 +               inc_nlink(dir);
21679 +               goto out_unpin; /* success */
21680 +       }
21681 +
21682 +       /* revert */
21683 +       if (diropq) {
21684 +               AuLabel(revert opq);
21685 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21686 +               rerr = au_diropq_remove(dentry, bindex);
21687 +               inode_unlock(h_inode);
21688 +               if (rerr) {
21689 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21690 +                               dentry, err, rerr);
21691 +                       err = -EIO;
21692 +               }
21693 +       }
21694 +
21695 +out_dir:
21696 +       AuLabel(revert dir);
21697 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21698 +       if (rerr) {
21699 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
21700 +                       dentry, err, rerr);
21701 +               err = -EIO;
21702 +       }
21703 +       au_dtime_revert(&a->dt);
21704 +out_unpin:
21705 +       au_unpin(&a->pin);
21706 +       dput(wh_dentry);
21707 +out_parent:
21708 +       di_write_unlock(parent);
21709 +out_unlock:
21710 +       if (unlikely(err)) {
21711 +               au_update_dbtop(dentry);
21712 +               d_drop(dentry);
21713 +       }
21714 +       aufs_read_unlock(dentry, AuLock_DW);
21715 +out_free:
21716 +       au_kfree_rcu(a);
21717 +out:
21718 +       return err;
21719 +}
21720 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
21721 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
21722 +++ linux/fs/aufs/i_op.c        2023-04-24 08:13:40.969576115 +0200
21723 @@ -0,0 +1,1517 @@
21724 +// SPDX-License-Identifier: GPL-2.0
21725 +/*
21726 + * Copyright (C) 2005-2022 Junjiro R. Okajima
21727 + *
21728 + * This program is free software; you can redistribute it and/or modify
21729 + * it under the terms of the GNU General Public License as published by
21730 + * the Free Software Foundation; either version 2 of the License, or
21731 + * (at your option) any later version.
21732 + *
21733 + * This program is distributed in the hope that it will be useful,
21734 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21735 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21736 + * GNU General Public License for more details.
21737 + *
21738 + * You should have received a copy of the GNU General Public License
21739 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21740 + */
21741 +
21742 +/*
21743 + * inode operations (except add/del/rename)
21744 + */
21745 +
21746 +#include <linux/device_cgroup.h>
21747 +#include <linux/filelock.h>
21748 +#include <linux/fs_stack.h>
21749 +#include <linux/iversion.h>
21750 +#include <linux/security.h>
21751 +#include "aufs.h"
21752 +
21753 +static int h_permission(struct inode *h_inode, int mask,
21754 +                       struct path *h_path, int brperm)
21755 +{
21756 +       int err;
21757 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21758 +       struct mnt_idmap *h_idmap;
21759 +
21760 +       err = -EPERM;
21761 +       if (write_mask && IS_IMMUTABLE(h_inode))
21762 +               goto out;
21763 +
21764 +       err = -EACCES;
21765 +       if (((mask & MAY_EXEC)
21766 +            && S_ISREG(h_inode->i_mode)
21767 +            && (path_noexec(h_path)
21768 +                || !(h_inode->i_mode & 0111))))
21769 +               goto out;
21770 +
21771 +       /*
21772 +        * - skip the lower fs test in the case of write to ro branch.
21773 +        * - nfs dir permission write check is optimized, but a policy for
21774 +        *   link/rename requires a real check.
21775 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
21776 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
21777 +        */
21778 +       h_idmap = mnt_idmap(h_path->mnt);
21779 +       if ((write_mask && !au_br_writable(brperm))
21780 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21781 +               && write_mask && !(mask & MAY_READ))
21782 +           || !h_inode->i_op->permission) {
21783 +               /* AuLabel(generic_permission); */
21784 +               /* AuDbg("get_inode_acl %ps\n",
21785 +                  h_inode->i_op->get_inode_acl); */
21786 +               err = generic_permission(h_idmap, h_inode, mask);
21787 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
21788 +                       err = h_inode->i_op->permission(h_idmap, h_inode,
21789 +                                                       mask);
21790 +               AuTraceErr(err);
21791 +       } else {
21792 +               /* AuLabel(h_inode->permission); */
21793 +               err = h_inode->i_op->permission(h_idmap, h_inode, mask);
21794 +               AuTraceErr(err);
21795 +       }
21796 +
21797 +       if (!err)
21798 +               err = devcgroup_inode_permission(h_inode, mask);
21799 +       if (!err)
21800 +               err = security_inode_permission(h_inode, mask);
21801 +
21802 +out:
21803 +       return err;
21804 +}
21805 +
21806 +static int aufs_permission(struct mnt_idmap *idmap, struct inode *inode,
21807 +                          int mask)
21808 +{
21809 +       int err;
21810 +       aufs_bindex_t bindex, bbot;
21811 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21812 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21813 +       struct inode *h_inode;
21814 +       struct super_block *sb;
21815 +       struct au_branch *br;
21816 +
21817 +       /* todo: support rcu-walk? */
21818 +       if (mask & MAY_NOT_BLOCK)
21819 +               return -ECHILD;
21820 +
21821 +       sb = inode->i_sb;
21822 +       si_read_lock(sb, AuLock_FLUSH);
21823 +       ii_read_lock_child(inode);
21824 +#if 0 /* reserved for future use */
21825 +       /*
21826 +        * This test may be rather 'too much' since the test is essentially done
21827 +        * in the aufs_lookup().  Theoretically it is possible that the inode
21828 +        * generation doesn't match to the superblock's here.  But it isn't a
21829 +        * big deal I suppose.
21830 +        */
21831 +       err = au_iigen_test(inode, au_sigen(sb));
21832 +       if (unlikely(err))
21833 +               goto out;
21834 +#endif
21835 +
21836 +       if (!isdir
21837 +           || write_mask
21838 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
21839 +               err = au_busy_or_stale();
21840 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
21841 +               if (unlikely(!h_inode
21842 +                            || (h_inode->i_mode & S_IFMT)
21843 +                            != (inode->i_mode & S_IFMT)))
21844 +                       goto out;
21845 +
21846 +               err = 0;
21847 +               bindex = au_ibtop(inode);
21848 +               br = au_sbr(sb, bindex);
21849 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
21850 +               if (write_mask
21851 +                   && !err
21852 +                   && !special_file(h_inode->i_mode)) {
21853 +                       /* test whether the upper writable branch exists */
21854 +                       err = -EROFS;
21855 +                       for (; bindex >= 0; bindex--)
21856 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
21857 +                                       err = 0;
21858 +                                       break;
21859 +                               }
21860 +               }
21861 +               goto out;
21862 +       }
21863 +
21864 +       /* non-write to dir */
21865 +       err = 0;
21866 +       bbot = au_ibbot(inode);
21867 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
21868 +               h_inode = au_h_iptr(inode, bindex);
21869 +               if (h_inode) {
21870 +                       err = au_busy_or_stale();
21871 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
21872 +                               break;
21873 +
21874 +                       br = au_sbr(sb, bindex);
21875 +                       err = h_permission(h_inode, mask, &br->br_path,
21876 +                                          br->br_perm);
21877 +               }
21878 +       }
21879 +
21880 +out:
21881 +       ii_read_unlock(inode);
21882 +       si_read_unlock(sb);
21883 +       return err;
21884 +}
21885 +
21886 +/* ---------------------------------------------------------------------- */
21887 +
21888 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
21889 +                                 unsigned int flags)
21890 +{
21891 +       struct dentry *ret, *parent;
21892 +       struct inode *inode;
21893 +       struct super_block *sb;
21894 +       int err, npositive;
21895 +
21896 +       IMustLock(dir);
21897 +
21898 +       /* todo: support rcu-walk? */
21899 +       ret = ERR_PTR(-ECHILD);
21900 +       if (flags & LOOKUP_RCU)
21901 +               goto out;
21902 +
21903 +       ret = ERR_PTR(-ENAMETOOLONG);
21904 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21905 +               goto out;
21906 +
21907 +       sb = dir->i_sb;
21908 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21909 +       ret = ERR_PTR(err);
21910 +       if (unlikely(err))
21911 +               goto out;
21912 +
21913 +       err = au_di_init(dentry);
21914 +       ret = ERR_PTR(err);
21915 +       if (unlikely(err))
21916 +               goto out_si;
21917 +
21918 +       inode = NULL;
21919 +       npositive = 0; /* suppress a warning */
21920 +       parent = dentry->d_parent; /* dir inode is locked */
21921 +       di_read_lock_parent(parent, AuLock_IR);
21922 +       err = au_alive_dir(parent);
21923 +       if (!err)
21924 +               err = au_digen_test(parent, au_sigen(sb));
21925 +       if (!err) {
21926 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21927 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21928 +                                          AuLkup_ALLOW_NEG);
21929 +               err = npositive;
21930 +       }
21931 +       di_read_unlock(parent, AuLock_IR);
21932 +       ret = ERR_PTR(err);
21933 +       if (unlikely(err < 0))
21934 +               goto out_unlock;
21935 +
21936 +       if (npositive) {
21937 +               inode = au_new_inode(dentry, /*must_new*/0);
21938 +               if (IS_ERR(inode)) {
21939 +                       ret = (void *)inode;
21940 +                       inode = NULL;
21941 +                       goto out_unlock;
21942 +               }
21943 +       }
21944 +
21945 +       if (inode)
21946 +               atomic_inc(&inode->i_count);
21947 +       ret = d_splice_alias(inode, dentry);
21948 +#if 0 /* reserved for future use */
21949 +       if (unlikely(d_need_lookup(dentry))) {
21950 +               spin_lock(&dentry->d_lock);
21951 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21952 +               spin_unlock(&dentry->d_lock);
21953 +       } else
21954 +#endif
21955 +       if (inode) {
21956 +               if (!IS_ERR(ret)) {
21957 +                       iput(inode);
21958 +                       if (ret && ret != dentry)
21959 +                               ii_write_unlock(inode);
21960 +               } else {
21961 +                       ii_write_unlock(inode);
21962 +                       iput(inode);
21963 +                       inode = NULL;
21964 +               }
21965 +       }
21966 +
21967 +out_unlock:
21968 +       di_write_unlock(dentry);
21969 +out_si:
21970 +       si_read_unlock(sb);
21971 +out:
21972 +       return ret;
21973 +}
21974 +
21975 +/* ---------------------------------------------------------------------- */
21976 +
21977 +/*
21978 + * very dirty and complicated aufs ->atomic_open().
21979 + * aufs_atomic_open()
21980 + * + au_aopen_or_create()
21981 + *   + add_simple()
21982 + *     + vfsub_atomic_open()
21983 + *       + branch fs ->atomic_open()
21984 + *        may call the actual 'open' for h_file
21985 + *       + inc br_nfiles only if opened
21986 + * + au_aopen_no_open() or au_aopen_do_open()
21987 + *
21988 + * au_aopen_do_open()
21989 + * + finish_open()
21990 + *   + au_do_aopen()
21991 + *     + au_do_open() the body of all 'open'
21992 + *       + au_do_open_nondir()
21993 + *        set the passed h_file
21994 + *
21995 + * au_aopen_no_open()
21996 + * + finish_no_open()
21997 + */
21998 +
21999 +struct aopen_node {
22000 +       struct hlist_bl_node hblist;
22001 +       struct file *file, *h_file;
22002 +};
22003 +
22004 +static int au_do_aopen(struct inode *inode, struct file *file)
22005 +{
22006 +       struct hlist_bl_head *aopen;
22007 +       struct hlist_bl_node *pos;
22008 +       struct aopen_node *node;
22009 +       struct au_do_open_args args = {
22010 +               .aopen  = 1,
22011 +               .open   = au_do_open_nondir
22012 +       };
22013 +
22014 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
22015 +       hlist_bl_lock(aopen);
22016 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
22017 +               if (node->file == file) {
22018 +                       args.h_file = node->h_file;
22019 +                       break;
22020 +               }
22021 +       hlist_bl_unlock(aopen);
22022 +       /* AuDebugOn(!args.h_file); */
22023 +
22024 +       return au_do_open(file, &args);
22025 +}
22026 +
22027 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
22028 +                           struct aopen_node *aopen_node)
22029 +{
22030 +       int err;
22031 +       struct hlist_bl_head *aopen;
22032 +
22033 +       AuLabel(here);
22034 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
22035 +       au_hbl_add(&aopen_node->hblist, aopen);
22036 +       err = finish_open(file, dentry, au_do_aopen);
22037 +       au_hbl_del(&aopen_node->hblist, aopen);
22038 +       /* AuDbgFile(file); */
22039 +       AuDbg("%pd%s%s\n", dentry,
22040 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
22041 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
22042 +
22043 +       AuTraceErr(err);
22044 +       return err;
22045 +}
22046 +
22047 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
22048 +{
22049 +       int err;
22050 +
22051 +       AuLabel(here);
22052 +       dget(dentry);
22053 +       err = finish_no_open(file, dentry);
22054 +
22055 +       AuTraceErr(err);
22056 +       return err;
22057 +}
22058 +
22059 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22060 +                           struct file *file, unsigned int open_flag,
22061 +                           umode_t create_mode)
22062 +{
22063 +       int err, did_open;
22064 +       unsigned int lkup_flags;
22065 +       aufs_bindex_t bindex;
22066 +       struct super_block *sb;
22067 +       struct dentry *parent, *d;
22068 +       struct vfsub_aopen_args args = {
22069 +               .open_flag      = open_flag,
22070 +               .create_mode    = create_mode
22071 +       };
22072 +       struct aopen_node aopen_node = {
22073 +               .file   = file
22074 +       };
22075 +
22076 +       IMustLock(dir);
22077 +       AuDbg("open_flag 0%o\n", open_flag);
22078 +       AuDbgDentry(dentry);
22079 +
22080 +       err = 0;
22081 +       if (!au_di(dentry)) {
22082 +               lkup_flags = LOOKUP_OPEN;
22083 +               if (open_flag & O_CREAT)
22084 +                       lkup_flags |= LOOKUP_CREATE;
22085 +               d = aufs_lookup(dir, dentry, lkup_flags);
22086 +               if (IS_ERR(d)) {
22087 +                       err = PTR_ERR(d);
22088 +                       AuTraceErr(err);
22089 +                       goto out;
22090 +               } else if (d) {
22091 +                       /*
22092 +                        * obsoleted dentry found.
22093 +                        * another error will be returned later.
22094 +                        */
22095 +                       d_drop(d);
22096 +                       AuDbgDentry(d);
22097 +                       dput(d);
22098 +               }
22099 +               AuDbgDentry(dentry);
22100 +       }
22101 +
22102 +       if (d_is_positive(dentry)
22103 +           || d_unhashed(dentry)
22104 +           || d_unlinked(dentry)
22105 +           || !(open_flag & O_CREAT)) {
22106 +               err = au_aopen_no_open(file, dentry);
22107 +               goto out; /* success */
22108 +       }
22109 +
22110 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22111 +       if (unlikely(err))
22112 +               goto out;
22113 +
22114 +       sb = dentry->d_sb;
22115 +       parent = dentry->d_parent;      /* dir is locked */
22116 +       di_write_lock_parent(parent);
22117 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
22118 +       if (unlikely(err < 0))
22119 +               goto out_parent;
22120 +
22121 +       AuDbgDentry(dentry);
22122 +       if (d_is_positive(dentry)) {
22123 +               err = au_aopen_no_open(file, dentry);
22124 +               goto out_parent; /* success */
22125 +       }
22126 +
22127 +       args.file = alloc_empty_file(file->f_flags, current_cred());
22128 +       err = PTR_ERR(args.file);
22129 +       if (IS_ERR(args.file))
22130 +               goto out_parent;
22131 +
22132 +       bindex = au_dbtop(dentry);
22133 +       err = au_aopen_or_create(dir, dentry, &args);
22134 +       AuTraceErr(err);
22135 +       AuDbgFile(args.file);
22136 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22137 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
22138 +       if (!did_open) {
22139 +               fput(args.file);
22140 +               args.file = NULL;
22141 +       }
22142 +       di_write_unlock(parent);
22143 +       di_write_unlock(dentry);
22144 +       if (unlikely(err < 0)) {
22145 +               if (args.file)
22146 +                       fput(args.file);
22147 +               goto out_sb;
22148 +       }
22149 +
22150 +       if (!did_open)
22151 +               err = au_aopen_no_open(file, dentry);
22152 +       else {
22153 +               aopen_node.h_file = args.file;
22154 +               err = au_aopen_do_open(file, dentry, &aopen_node);
22155 +       }
22156 +       if (unlikely(err < 0)) {
22157 +               if (args.file)
22158 +                       fput(args.file);
22159 +               if (did_open)
22160 +                       au_lcnt_dec(&args.br->br_nfiles);
22161 +       }
22162 +       goto out_sb; /* success */
22163 +
22164 +out_parent:
22165 +       di_write_unlock(parent);
22166 +       di_write_unlock(dentry);
22167 +out_sb:
22168 +       si_read_unlock(sb);
22169 +out:
22170 +       AuTraceErr(err);
22171 +       AuDbgFile(file);
22172 +       return err;
22173 +}
22174 +
22175 +
22176 +/* ---------------------------------------------------------------------- */
22177 +
22178 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22179 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
22180 +                         aufs_bindex_t btop)
22181 +{
22182 +       int err;
22183 +       struct dentry *h_parent;
22184 +       struct inode *h_dir;
22185 +
22186 +       if (add_entry)
22187 +               IMustLock(d_inode(parent));
22188 +       else
22189 +               di_write_lock_parent(parent);
22190 +
22191 +       err = 0;
22192 +       if (!au_h_dptr(parent, bcpup)) {
22193 +               if (btop > bcpup)
22194 +                       err = au_cpup_dirs(dentry, bcpup);
22195 +               else if (btop < bcpup)
22196 +                       err = au_cpdown_dirs(dentry, bcpup);
22197 +               else
22198 +                       BUG();
22199 +       }
22200 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
22201 +               h_parent = au_h_dptr(parent, bcpup);
22202 +               h_dir = d_inode(h_parent);
22203 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
22204 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
22205 +               /* todo: no unlock here */
22206 +               inode_unlock_shared(h_dir);
22207 +
22208 +               AuDbg("bcpup %d\n", bcpup);
22209 +               if (!err) {
22210 +                       if (d_really_is_negative(dentry))
22211 +                               au_set_h_dptr(dentry, btop, NULL);
22212 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
22213 +               }
22214 +       }
22215 +
22216 +       if (!add_entry)
22217 +               di_write_unlock(parent);
22218 +       if (!err)
22219 +               err = bcpup; /* success */
22220 +
22221 +       AuTraceErr(err);
22222 +       return err;
22223 +}
22224 +
22225 +/*
22226 + * decide the branch and the parent dir where we will create a new entry.
22227 + * returns new bindex or an error.
22228 + * copyup the parent dir if needed.
22229 + */
22230 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22231 +             struct au_wr_dir_args *args)
22232 +{
22233 +       int err;
22234 +       unsigned int flags;
22235 +       aufs_bindex_t bcpup, btop, src_btop;
22236 +       const unsigned char add_entry
22237 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
22238 +               | au_ftest_wrdir(args->flags, TMPFILE);
22239 +       struct super_block *sb;
22240 +       struct dentry *parent;
22241 +       struct au_sbinfo *sbinfo;
22242 +
22243 +       sb = dentry->d_sb;
22244 +       sbinfo = au_sbi(sb);
22245 +       parent = dget_parent(dentry);
22246 +       btop = au_dbtop(dentry);
22247 +       bcpup = btop;
22248 +       if (args->force_btgt < 0) {
22249 +               if (src_dentry) {
22250 +                       src_btop = au_dbtop(src_dentry);
22251 +                       if (src_btop < btop)
22252 +                               bcpup = src_btop;
22253 +               } else if (add_entry) {
22254 +                       flags = 0;
22255 +                       if (au_ftest_wrdir(args->flags, ISDIR))
22256 +                               au_fset_wbr(flags, DIR);
22257 +                       err = AuWbrCreate(sbinfo, dentry, flags);
22258 +                       bcpup = err;
22259 +               }
22260 +
22261 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
22262 +                       if (add_entry)
22263 +                               err = AuWbrCopyup(sbinfo, dentry);
22264 +                       else {
22265 +                               if (!IS_ROOT(dentry)) {
22266 +                                       di_read_lock_parent(parent, !AuLock_IR);
22267 +                                       err = AuWbrCopyup(sbinfo, dentry);
22268 +                                       di_read_unlock(parent, !AuLock_IR);
22269 +                               } else
22270 +                                       err = AuWbrCopyup(sbinfo, dentry);
22271 +                       }
22272 +                       bcpup = err;
22273 +                       if (unlikely(err < 0))
22274 +                               goto out;
22275 +               }
22276 +       } else {
22277 +               bcpup = args->force_btgt;
22278 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
22279 +       }
22280 +
22281 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
22282 +       err = bcpup;
22283 +       if (bcpup == btop)
22284 +               goto out; /* success */
22285 +
22286 +       /* copyup the new parent into the branch we process */
22287 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
22288 +       if (err >= 0) {
22289 +               if (d_really_is_negative(dentry)) {
22290 +                       au_set_h_dptr(dentry, btop, NULL);
22291 +                       au_set_dbtop(dentry, bcpup);
22292 +                       au_set_dbbot(dentry, bcpup);
22293 +               }
22294 +               AuDebugOn(add_entry
22295 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
22296 +                         && !au_h_dptr(dentry, bcpup));
22297 +       }
22298 +
22299 +out:
22300 +       dput(parent);
22301 +       return err;
22302 +}
22303 +
22304 +/* ---------------------------------------------------------------------- */
22305 +
22306 +void au_pin_hdir_unlock(struct au_pin *p)
22307 +{
22308 +       if (p->hdir)
22309 +               au_hn_inode_unlock(p->hdir);
22310 +}
22311 +
22312 +int au_pin_hdir_lock(struct au_pin *p)
22313 +{
22314 +       int err;
22315 +
22316 +       err = 0;
22317 +       if (!p->hdir)
22318 +               goto out;
22319 +
22320 +       /* even if an error happens later, keep this lock */
22321 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
22322 +
22323 +       err = -EBUSY;
22324 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
22325 +               goto out;
22326 +
22327 +       err = 0;
22328 +       if (p->h_dentry)
22329 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22330 +                                 p->h_parent, p->br);
22331 +
22332 +out:
22333 +       return err;
22334 +}
22335 +
22336 +int au_pin_hdir_relock(struct au_pin *p)
22337 +{
22338 +       int err, i;
22339 +       struct inode *h_i;
22340 +       struct dentry *h_d[] = {
22341 +               p->h_dentry,
22342 +               p->h_parent
22343 +       };
22344 +
22345 +       err = au_pin_hdir_lock(p);
22346 +       if (unlikely(err))
22347 +               goto out;
22348 +
22349 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22350 +               if (!h_d[i])
22351 +                       continue;
22352 +               if (d_is_positive(h_d[i])) {
22353 +                       h_i = d_inode(h_d[i]);
22354 +                       err = !h_i->i_nlink;
22355 +               }
22356 +       }
22357 +
22358 +out:
22359 +       return err;
22360 +}
22361 +
22362 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
22363 +{
22364 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
22365 +}
22366 +
22367 +void au_pin_hdir_acquire_nest(struct au_pin *p)
22368 +{
22369 +       if (p->hdir) {
22370 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
22371 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
22372 +               au_pin_hdir_set_owner(p, current);
22373 +       }
22374 +}
22375 +
22376 +void au_pin_hdir_release(struct au_pin *p)
22377 +{
22378 +       if (p->hdir) {
22379 +               au_pin_hdir_set_owner(p, p->task);
22380 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
22381 +       }
22382 +}
22383 +
22384 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
22385 +{
22386 +       if (pin && pin->parent)
22387 +               return au_h_dptr(pin->parent, pin->bindex);
22388 +       return NULL;
22389 +}
22390 +
22391 +void au_unpin(struct au_pin *p)
22392 +{
22393 +       if (p->hdir)
22394 +               au_pin_hdir_unlock(p);
22395 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
22396 +               vfsub_mnt_drop_write(p->h_mnt);
22397 +       if (!p->hdir)
22398 +               return;
22399 +
22400 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22401 +               di_read_unlock(p->parent, AuLock_IR);
22402 +       iput(p->hdir->hi_inode);
22403 +       dput(p->parent);
22404 +       p->parent = NULL;
22405 +       p->hdir = NULL;
22406 +       p->h_mnt = NULL;
22407 +       /* do not clear p->task */
22408 +}
22409 +
22410 +int au_do_pin(struct au_pin *p)
22411 +{
22412 +       int err;
22413 +       struct super_block *sb;
22414 +       struct inode *h_dir;
22415 +
22416 +       err = 0;
22417 +       sb = p->dentry->d_sb;
22418 +       p->br = au_sbr(sb, p->bindex);
22419 +       if (IS_ROOT(p->dentry)) {
22420 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
22421 +                       p->h_mnt = au_br_mnt(p->br);
22422 +                       err = vfsub_mnt_want_write(p->h_mnt);
22423 +                       if (unlikely(err)) {
22424 +                               au_fclr_pin(p->flags, MNT_WRITE);
22425 +                               goto out_err;
22426 +                       }
22427 +               }
22428 +               goto out;
22429 +       }
22430 +
22431 +       p->h_dentry = NULL;
22432 +       if (p->bindex <= au_dbbot(p->dentry))
22433 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
22434 +
22435 +       p->parent = dget_parent(p->dentry);
22436 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22437 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
22438 +
22439 +       h_dir = NULL;
22440 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
22441 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
22442 +       if (p->hdir)
22443 +               h_dir = p->hdir->hi_inode;
22444 +
22445 +       /*
22446 +        * udba case, or
22447 +        * if DI_LOCKED is not set, then p->parent may be different
22448 +        * and h_parent can be NULL.
22449 +        */
22450 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
22451 +               err = -EBUSY;
22452 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
22453 +                       di_read_unlock(p->parent, AuLock_IR);
22454 +               dput(p->parent);
22455 +               p->parent = NULL;
22456 +               goto out_err;
22457 +       }
22458 +
22459 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
22460 +               p->h_mnt = au_br_mnt(p->br);
22461 +               err = vfsub_mnt_want_write(p->h_mnt);
22462 +               if (unlikely(err)) {
22463 +                       au_fclr_pin(p->flags, MNT_WRITE);
22464 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
22465 +                               di_read_unlock(p->parent, AuLock_IR);
22466 +                       dput(p->parent);
22467 +                       p->parent = NULL;
22468 +                       goto out_err;
22469 +               }
22470 +       }
22471 +
22472 +       au_igrab(h_dir);
22473 +       err = au_pin_hdir_lock(p);
22474 +       if (!err)
22475 +               goto out; /* success */
22476 +
22477 +       au_unpin(p);
22478 +
22479 +out_err:
22480 +       pr_err("err %d\n", err);
22481 +       err = au_busy_or_stale();
22482 +out:
22483 +       return err;
22484 +}
22485 +
22486 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
22487 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22488 +                unsigned int udba, unsigned char flags)
22489 +{
22490 +       p->dentry = dentry;
22491 +       p->udba = udba;
22492 +       p->lsc_di = lsc_di;
22493 +       p->lsc_hi = lsc_hi;
22494 +       p->flags = flags;
22495 +       p->bindex = bindex;
22496 +
22497 +       p->parent = NULL;
22498 +       p->hdir = NULL;
22499 +       p->h_mnt = NULL;
22500 +
22501 +       p->h_dentry = NULL;
22502 +       p->h_parent = NULL;
22503 +       p->br = NULL;
22504 +       p->task = current;
22505 +}
22506 +
22507 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22508 +          unsigned int udba, unsigned char flags)
22509 +{
22510 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22511 +                   udba, flags);
22512 +       return au_do_pin(pin);
22513 +}
22514 +
22515 +/* ---------------------------------------------------------------------- */
22516 +
22517 +/*
22518 + * ->setattr() and ->getattr() are called in various cases.
22519 + * chmod, stat: dentry is revalidated.
22520 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22521 + *               unhashed.
22522 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
22523 + */
22524 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
22525 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
22526 +{
22527 +       int err;
22528 +       struct dentry *parent;
22529 +
22530 +       err = 0;
22531 +       if (au_digen_test(dentry, sigen)) {
22532 +               parent = dget_parent(dentry);
22533 +               di_read_lock_parent(parent, AuLock_IR);
22534 +               err = au_refresh_dentry(dentry, parent);
22535 +               di_read_unlock(parent, AuLock_IR);
22536 +               dput(parent);
22537 +       }
22538 +
22539 +       AuTraceErr(err);
22540 +       return err;
22541 +}
22542 +
22543 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22544 +                    struct au_icpup_args *a)
22545 +{
22546 +       int err;
22547 +       loff_t sz;
22548 +       aufs_bindex_t btop, ibtop;
22549 +       struct dentry *hi_wh, *parent;
22550 +       struct inode *inode;
22551 +       struct au_wr_dir_args wr_dir_args = {
22552 +               .force_btgt     = -1,
22553 +               .flags          = 0
22554 +       };
22555 +
22556 +       if (d_is_dir(dentry))
22557 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
22558 +       /* plink or hi_wh() case */
22559 +       btop = au_dbtop(dentry);
22560 +       inode = d_inode(dentry);
22561 +       ibtop = au_ibtop(inode);
22562 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22563 +               wr_dir_args.force_btgt = ibtop;
22564 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22565 +       if (unlikely(err < 0))
22566 +               goto out;
22567 +       a->btgt = err;
22568 +       if (err != btop)
22569 +               au_fset_icpup(a->flags, DID_CPUP);
22570 +
22571 +       err = 0;
22572 +       a->pin_flags = AuPin_MNT_WRITE;
22573 +       parent = NULL;
22574 +       if (!IS_ROOT(dentry)) {
22575 +               au_fset_pin(a->pin_flags, DI_LOCKED);
22576 +               parent = dget_parent(dentry);
22577 +               di_write_lock_parent(parent);
22578 +       }
22579 +
22580 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22581 +       if (unlikely(err))
22582 +               goto out_parent;
22583 +
22584 +       sz = -1;
22585 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22586 +       a->h_inode = d_inode(a->h_path.dentry);
22587 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
22588 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
22589 +               if (ia->ia_size < i_size_read(a->h_inode))
22590 +                       sz = ia->ia_size;
22591 +               inode_unlock_shared(a->h_inode);
22592 +       }
22593 +
22594 +       hi_wh = NULL;
22595 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
22596 +               hi_wh = au_hi_wh(inode, a->btgt);
22597 +               if (!hi_wh) {
22598 +                       struct au_cp_generic cpg = {
22599 +                               .dentry = dentry,
22600 +                               .bdst   = a->btgt,
22601 +                               .bsrc   = -1,
22602 +                               .len    = sz,
22603 +                               .pin    = &a->pin
22604 +                       };
22605 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
22606 +                       if (unlikely(err))
22607 +                               goto out_unlock;
22608 +                       hi_wh = au_hi_wh(inode, a->btgt);
22609 +                       /* todo: revalidate hi_wh? */
22610 +               }
22611 +       }
22612 +
22613 +       if (parent) {
22614 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22615 +               di_downgrade_lock(parent, AuLock_IR);
22616 +               dput(parent);
22617 +               parent = NULL;
22618 +       }
22619 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
22620 +               goto out; /* success */
22621 +
22622 +       if (!d_unhashed(dentry)) {
22623 +               struct au_cp_generic cpg = {
22624 +                       .dentry = dentry,
22625 +                       .bdst   = a->btgt,
22626 +                       .bsrc   = btop,
22627 +                       .len    = sz,
22628 +                       .pin    = &a->pin,
22629 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
22630 +               };
22631 +               err = au_sio_cpup_simple(&cpg);
22632 +               if (!err)
22633 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22634 +       } else if (!hi_wh)
22635 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22636 +       else
22637 +               a->h_path.dentry = hi_wh; /* do not dget here */
22638 +
22639 +out_unlock:
22640 +       a->h_inode = d_inode(a->h_path.dentry);
22641 +       if (!err)
22642 +               goto out; /* success */
22643 +       au_unpin(&a->pin);
22644 +out_parent:
22645 +       if (parent) {
22646 +               di_write_unlock(parent);
22647 +               dput(parent);
22648 +       }
22649 +out:
22650 +       if (!err)
22651 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22652 +       return err;
22653 +}
22654 +
22655 +static int aufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
22656 +                       struct iattr *ia)
22657 +{
22658 +       int err;
22659 +       struct inode *inode, *delegated;
22660 +       struct super_block *sb;
22661 +       struct file *file;
22662 +       struct au_icpup_args *a;
22663 +       struct mnt_idmap *h_idmap;
22664 +
22665 +       inode = d_inode(dentry);
22666 +       IMustLock(inode);
22667 +
22668 +       err = setattr_prepare(idmap, dentry, ia);
22669 +       if (unlikely(err))
22670 +               goto out;
22671 +
22672 +       err = -ENOMEM;
22673 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22674 +       if (unlikely(!a))
22675 +               goto out;
22676 +
22677 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22678 +               ia->ia_valid &= ~ATTR_MODE;
22679 +
22680 +       file = NULL;
22681 +       sb = dentry->d_sb;
22682 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22683 +       if (unlikely(err))
22684 +               goto out_kfree;
22685 +
22686 +       if (ia->ia_valid & ATTR_FILE) {
22687 +               /* currently ftruncate(2) only */
22688 +               AuDebugOn(!d_is_reg(dentry));
22689 +               file = ia->ia_file;
22690 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22691 +                                           /*fi_lsc*/0);
22692 +               if (unlikely(err))
22693 +                       goto out_si;
22694 +               ia->ia_file = au_hf_top(file);
22695 +               a->udba = AuOpt_UDBA_NONE;
22696 +       } else {
22697 +               /* fchmod() doesn't pass ia_file */
22698 +               a->udba = au_opt_udba(sb);
22699 +               di_write_lock_child(dentry);
22700 +               /* no d_unlinked(), to set UDBA_NONE for root */
22701 +               if (d_unhashed(dentry))
22702 +                       a->udba = AuOpt_UDBA_NONE;
22703 +               if (a->udba != AuOpt_UDBA_NONE) {
22704 +                       AuDebugOn(IS_ROOT(dentry));
22705 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
22706 +                       if (unlikely(err))
22707 +                               goto out_dentry;
22708 +               }
22709 +       }
22710 +
22711 +       err = au_pin_and_icpup(dentry, ia, a);
22712 +       if (unlikely(err < 0))
22713 +               goto out_dentry;
22714 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
22715 +               ia->ia_file = NULL;
22716 +               ia->ia_valid &= ~ATTR_FILE;
22717 +       }
22718 +
22719 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22720 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22721 +           == (ATTR_MODE | ATTR_CTIME)) {
22722 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
22723 +               if (unlikely(err))
22724 +                       goto out_unlock;
22725 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22726 +                  && (ia->ia_valid & ATTR_CTIME)) {
22727 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
22728 +               if (unlikely(err))
22729 +                       goto out_unlock;
22730 +       }
22731 +
22732 +       if (ia->ia_valid & ATTR_SIZE) {
22733 +               struct file *f;
22734 +
22735 +               if (ia->ia_size < i_size_read(inode))
22736 +                       /* unmap only */
22737 +                       truncate_setsize(inode, ia->ia_size);
22738 +
22739 +               f = NULL;
22740 +               if (ia->ia_valid & ATTR_FILE)
22741 +                       f = ia->ia_file;
22742 +               inode_unlock(a->h_inode);
22743 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
22744 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22745 +       } else {
22746 +               delegated = NULL;
22747 +               while (1) {
22748 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
22749 +                       if (delegated) {
22750 +                               err = break_deleg_wait(&delegated);
22751 +                               if (!err)
22752 +                                       continue;
22753 +                       }
22754 +                       break;
22755 +               }
22756 +       }
22757 +       /*
22758 +        * regardless aufs 'acl' option setting.
22759 +        * why don't all acl-aware fs call this func from their ->setattr()?
22760 +        */
22761 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
22762 +               h_idmap = mnt_idmap(a->h_path.mnt);
22763 +               err = vfsub_acl_chmod(h_idmap, a->h_path.dentry, ia->ia_mode);
22764 +       }
22765 +       if (!err)
22766 +               au_cpup_attr_changeable(inode);
22767 +
22768 +out_unlock:
22769 +       inode_unlock(a->h_inode);
22770 +       au_unpin(&a->pin);
22771 +       if (unlikely(err))
22772 +               au_update_dbtop(dentry);
22773 +out_dentry:
22774 +       di_write_unlock(dentry);
22775 +       if (file) {
22776 +               fi_write_unlock(file);
22777 +               ia->ia_file = file;
22778 +               ia->ia_valid |= ATTR_FILE;
22779 +       }
22780 +out_si:
22781 +       si_read_unlock(sb);
22782 +out_kfree:
22783 +       au_kfree_rcu(a);
22784 +out:
22785 +       AuTraceErr(err);
22786 +       return err;
22787 +}
22788 +
22789 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22790 +static int au_h_path_to_set_attr(struct dentry *dentry,
22791 +                                struct au_icpup_args *a, struct path *h_path)
22792 +{
22793 +       int err;
22794 +       struct super_block *sb;
22795 +
22796 +       sb = dentry->d_sb;
22797 +       a->udba = au_opt_udba(sb);
22798 +       /* no d_unlinked(), to set UDBA_NONE for root */
22799 +       if (d_unhashed(dentry))
22800 +               a->udba = AuOpt_UDBA_NONE;
22801 +       if (a->udba != AuOpt_UDBA_NONE) {
22802 +               AuDebugOn(IS_ROOT(dentry));
22803 +               err = au_reval_for_attr(dentry, au_sigen(sb));
22804 +               if (unlikely(err))
22805 +                       goto out;
22806 +       }
22807 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22808 +       if (unlikely(err < 0))
22809 +               goto out;
22810 +
22811 +       h_path->dentry = a->h_path.dentry;
22812 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
22813 +
22814 +out:
22815 +       return err;
22816 +}
22817 +
22818 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22819 +                 struct au_sxattr *arg)
22820 +{
22821 +       int err;
22822 +       struct path h_path;
22823 +       struct super_block *sb;
22824 +       struct au_icpup_args *a;
22825 +       struct inode *h_inode;
22826 +       struct mnt_idmap *h_idmap;
22827 +
22828 +       IMustLock(inode);
22829 +
22830 +       err = -ENOMEM;
22831 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22832 +       if (unlikely(!a))
22833 +               goto out;
22834 +
22835 +       sb = dentry->d_sb;
22836 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22837 +       if (unlikely(err))
22838 +               goto out_kfree;
22839 +
22840 +       h_path.dentry = NULL;   /* silence gcc */
22841 +       di_write_lock_child(dentry);
22842 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
22843 +       if (unlikely(err))
22844 +               goto out_di;
22845 +       h_idmap = mnt_idmap(h_path.mnt);
22846 +
22847 +       inode_unlock(a->h_inode);
22848 +       switch (arg->type) {
22849 +       case AU_XATTR_SET:
22850 +               AuDebugOn(d_is_negative(h_path.dentry));
22851 +               err = vfsub_setxattr(h_idmap, h_path.dentry,
22852 +                                    arg->u.set.name, arg->u.set.value,
22853 +                                    arg->u.set.size, arg->u.set.flags);
22854 +               break;
22855 +       case AU_ACL_SET:
22856 +               err = -EOPNOTSUPP;
22857 +               h_inode = d_inode(h_path.dentry);
22858 +               if (h_inode->i_op->set_acl) {
22859 +                       /* this will call posix_acl_update_mode */
22860 +                       err = h_inode->i_op->set_acl(h_idmap, h_path.dentry,
22861 +                                                    arg->u.acl_set.acl,
22862 +                                                    arg->u.acl_set.type);
22863 +               }
22864 +               break;
22865 +       }
22866 +       if (!err)
22867 +               au_cpup_attr_timesizes(inode);
22868 +
22869 +       au_unpin(&a->pin);
22870 +       if (unlikely(err))
22871 +               au_update_dbtop(dentry);
22872 +
22873 +out_di:
22874 +       di_write_unlock(dentry);
22875 +       si_read_unlock(sb);
22876 +out_kfree:
22877 +       au_kfree_rcu(a);
22878 +out:
22879 +       AuTraceErr(err);
22880 +       return err;
22881 +}
22882 +#endif
22883 +
22884 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22885 +                            unsigned int nlink)
22886 +{
22887 +       unsigned int n;
22888 +
22889 +       inode->i_mode = st->mode;
22890 +       /* don't i_[ug]id_write() here */
22891 +       inode->i_uid = st->uid;
22892 +       inode->i_gid = st->gid;
22893 +       inode->i_atime = st->atime;
22894 +       inode->i_mtime = st->mtime;
22895 +       inode->i_ctime = st->ctime;
22896 +
22897 +       au_cpup_attr_nlink(inode, /*force*/0);
22898 +       if (S_ISDIR(inode->i_mode)) {
22899 +               n = inode->i_nlink;
22900 +               n -= nlink;
22901 +               n += st->nlink;
22902 +               smp_mb(); /* for i_nlink */
22903 +               /* 0 can happen */
22904 +               set_nlink(inode, n);
22905 +       }
22906 +
22907 +       spin_lock(&inode->i_lock);
22908 +       inode->i_blocks = st->blocks;
22909 +       i_size_write(inode, st->size);
22910 +       spin_unlock(&inode->i_lock);
22911 +}
22912 +
22913 +/*
22914 + * common routine for aufs_getattr() and au_getxattr().
22915 + * returns zero or negative (an error).
22916 + * @dentry will be read-locked in success.
22917 + */
22918 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22919 +                     struct path *h_path, int locked)
22920 +{
22921 +       int err;
22922 +       unsigned int mnt_flags, sigen;
22923 +       unsigned char udba_none;
22924 +       aufs_bindex_t bindex;
22925 +       struct super_block *sb, *h_sb;
22926 +
22927 +       h_path->mnt = NULL;
22928 +       h_path->dentry = NULL;
22929 +
22930 +       err = 0;
22931 +       sb = dentry->d_sb;
22932 +       mnt_flags = au_mntflags(sb);
22933 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
22934 +
22935 +       if (unlikely(locked))
22936 +               goto body; /* skip locking dinfo */
22937 +
22938 +       /* support fstat(2) */
22939 +       if (!d_unlinked(dentry) && !udba_none) {
22940 +               sigen = au_sigen(sb);
22941 +               err = au_digen_test(dentry, sigen);
22942 +               if (!err) {
22943 +                       di_read_lock_child(dentry, AuLock_IR);
22944 +                       err = au_dbrange_test(dentry);
22945 +                       if (unlikely(err)) {
22946 +                               di_read_unlock(dentry, AuLock_IR);
22947 +                               goto out;
22948 +                       }
22949 +               } else {
22950 +                       AuDebugOn(IS_ROOT(dentry));
22951 +                       di_write_lock_child(dentry);
22952 +                       err = au_dbrange_test(dentry);
22953 +                       if (!err)
22954 +                               err = au_reval_for_attr(dentry, sigen);
22955 +                       if (!err)
22956 +                               di_downgrade_lock(dentry, AuLock_IR);
22957 +                       else {
22958 +                               di_write_unlock(dentry);
22959 +                               goto out;
22960 +                       }
22961 +               }
22962 +       } else
22963 +               di_read_lock_child(dentry, AuLock_IR);
22964 +
22965 +body:
22966 +       if (!inode) {
22967 +               inode = d_inode(dentry);
22968 +               if (unlikely(!inode))
22969 +                       goto out;
22970 +       }
22971 +       bindex = au_ibtop(inode);
22972 +       h_path->mnt = au_sbr_mnt(sb, bindex);
22973 +       h_sb = h_path->mnt->mnt_sb;
22974 +       if (!force
22975 +           && !au_test_fs_bad_iattr(h_sb)
22976 +           && udba_none)
22977 +               goto out; /* success */
22978 +
22979 +       if (au_dbtop(dentry) == bindex)
22980 +               h_path->dentry = au_h_dptr(dentry, bindex);
22981 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
22982 +               h_path->dentry = au_plink_lkup(inode, bindex);
22983 +               if (IS_ERR(h_path->dentry))
22984 +                       /* pretending success */
22985 +                       h_path->dentry = NULL;
22986 +               else
22987 +                       dput(h_path->dentry);
22988 +       }
22989 +
22990 +out:
22991 +       return err;
22992 +}
22993 +
22994 +static int aufs_getattr(struct mnt_idmap *idmap, const struct path *path,
22995 +                       struct kstat *st, u32 request, unsigned int query)
22996 +{
22997 +       int err;
22998 +       unsigned char positive;
22999 +       struct path h_path;
23000 +       struct dentry *dentry;
23001 +       struct inode *inode;
23002 +       struct super_block *sb;
23003 +
23004 +       dentry = path->dentry;
23005 +       inode = d_inode(dentry);
23006 +       sb = dentry->d_sb;
23007 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23008 +       if (unlikely(err))
23009 +               goto out;
23010 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
23011 +                               /*locked*/0);
23012 +       if (unlikely(err))
23013 +               goto out_si;
23014 +       if (unlikely(!h_path.dentry))
23015 +               /* illegally overlapped or something */
23016 +               goto out_fill; /* pretending success */
23017 +
23018 +       positive = d_is_positive(h_path.dentry);
23019 +       if (positive)
23020 +               /* no vfsub version */
23021 +               err = vfs_getattr(&h_path, st, request, query);
23022 +       if (!err) {
23023 +               if (positive)
23024 +                       au_refresh_iattr(inode, st,
23025 +                                        d_inode(h_path.dentry)->i_nlink);
23026 +               goto out_fill; /* success */
23027 +       }
23028 +       AuTraceErr(err);
23029 +       goto out_di;
23030 +
23031 +out_fill:
23032 +       generic_fillattr(idmap, inode, st);
23033 +out_di:
23034 +       di_read_unlock(dentry, AuLock_IR);
23035 +out_si:
23036 +       si_read_unlock(sb);
23037 +out:
23038 +       AuTraceErr(err);
23039 +       return err;
23040 +}
23041 +
23042 +/* ---------------------------------------------------------------------- */
23043 +
23044 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
23045 +                                struct delayed_call *done)
23046 +{
23047 +       const char *ret;
23048 +       struct dentry *h_dentry;
23049 +       struct inode *h_inode;
23050 +       int err;
23051 +       aufs_bindex_t bindex;
23052 +
23053 +       ret = NULL; /* suppress a warning */
23054 +       err = -ECHILD;
23055 +       if (!dentry)
23056 +               goto out;
23057 +
23058 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23059 +       if (unlikely(err))
23060 +               goto out;
23061 +
23062 +       err = au_d_hashed_positive(dentry);
23063 +       if (unlikely(err))
23064 +               goto out_unlock;
23065 +
23066 +       err = -EINVAL;
23067 +       inode = d_inode(dentry);
23068 +       bindex = au_ibtop(inode);
23069 +       h_inode = au_h_iptr(inode, bindex);
23070 +       if (unlikely(!h_inode->i_op->get_link))
23071 +               goto out_unlock;
23072 +
23073 +       err = -EBUSY;
23074 +       h_dentry = NULL;
23075 +       if (au_dbtop(dentry) <= bindex) {
23076 +               h_dentry = au_h_dptr(dentry, bindex);
23077 +               if (h_dentry)
23078 +                       dget(h_dentry);
23079 +       }
23080 +       if (!h_dentry) {
23081 +               h_dentry = d_find_any_alias(h_inode);
23082 +               if (IS_ERR(h_dentry)) {
23083 +                       err = PTR_ERR(h_dentry);
23084 +                       goto out_unlock;
23085 +               }
23086 +       }
23087 +       if (unlikely(!h_dentry))
23088 +               goto out_unlock;
23089 +
23090 +       err = 0;
23091 +       AuDbg("%ps\n", h_inode->i_op->get_link);
23092 +       AuDbgDentry(h_dentry);
23093 +       ret = vfs_get_link(h_dentry, done);
23094 +       dput(h_dentry);
23095 +       if (IS_ERR(ret))
23096 +               err = PTR_ERR(ret);
23097 +
23098 +out_unlock:
23099 +       aufs_read_unlock(dentry, AuLock_IR);
23100 +out:
23101 +       if (unlikely(err))
23102 +               ret = ERR_PTR(err);
23103 +       AuTraceErrPtr(ret);
23104 +       return ret;
23105 +}
23106 +
23107 +/* ---------------------------------------------------------------------- */
23108 +
23109 +static int au_is_special(struct inode *inode)
23110 +{
23111 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23112 +}
23113 +
23114 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
23115 +                           int flags)
23116 +{
23117 +       int err;
23118 +       aufs_bindex_t bindex;
23119 +       struct super_block *sb;
23120 +       struct inode *h_inode;
23121 +       struct vfsmount *h_mnt;
23122 +
23123 +       sb = inode->i_sb;
23124 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23125 +                 "unexpected s_flags 0x%lx", sb->s_flags);
23126 +
23127 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
23128 +       lockdep_off();
23129 +       si_read_lock(sb, AuLock_FLUSH);
23130 +       ii_write_lock_child(inode);
23131 +
23132 +       err = 0;
23133 +       bindex = au_ibtop(inode);
23134 +       h_inode = au_h_iptr(inode, bindex);
23135 +       if (!au_test_ro(sb, bindex, inode)) {
23136 +               h_mnt = au_sbr_mnt(sb, bindex);
23137 +               err = vfsub_mnt_want_write(h_mnt);
23138 +               if (!err) {
23139 +                       err = vfsub_update_time(h_inode, ts, flags);
23140 +                       vfsub_mnt_drop_write(h_mnt);
23141 +               }
23142 +       } else if (au_is_special(h_inode)) {
23143 +               /*
23144 +                * Never copy-up here.
23145 +                * These special files may already be opened and used for
23146 +                * communicating. If we copied it up, then the communication
23147 +                * would be corrupted.
23148 +                */
23149 +               AuWarn1("timestamps for i%lu are ignored "
23150 +                       "since it is on readonly branch (hi%lu).\n",
23151 +                       inode->i_ino, h_inode->i_ino);
23152 +       } else if (flags & ~S_ATIME) {
23153 +               err = -EIO;
23154 +               AuIOErr1("unexpected flags 0x%x\n", flags);
23155 +               AuDebugOn(1);
23156 +       }
23157 +
23158 +       if (!err)
23159 +               au_cpup_attr_timesizes(inode);
23160 +       ii_write_unlock(inode);
23161 +       si_read_unlock(sb);
23162 +       lockdep_on();
23163 +
23164 +       if (!err && (flags & S_VERSION))
23165 +               inode_inc_iversion(inode);
23166 +
23167 +       return err;
23168 +}
23169 +
23170 +/* ---------------------------------------------------------------------- */
23171 +
23172 +/* no getattr version will be set by module.c:aufs_init() */
23173 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23174 +       aufs_iop[] = {
23175 +       [AuIop_SYMLINK] = {
23176 +               .permission     = aufs_permission,
23177 +#ifdef CONFIG_FS_POSIX_ACL
23178 +               .get_inode_acl  = aufs_get_inode_acl,
23179 +               .get_acl        = aufs_get_acl,
23180 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
23181 +#endif
23182 +
23183 +               .setattr        = aufs_setattr,
23184 +               .getattr        = aufs_getattr,
23185 +
23186 +#ifdef CONFIG_AUFS_XATTR
23187 +               .listxattr      = aufs_listxattr,
23188 +#endif
23189 +
23190 +               .get_link       = aufs_get_link
23191 +
23192 +               /* .update_time = aufs_update_time */
23193 +       },
23194 +       [AuIop_DIR] = {
23195 +               .create         = aufs_create,
23196 +               .lookup         = aufs_lookup,
23197 +               .link           = aufs_link,
23198 +               .unlink         = aufs_unlink,
23199 +               .symlink        = aufs_symlink,
23200 +               .mkdir          = aufs_mkdir,
23201 +               .rmdir          = aufs_rmdir,
23202 +               .mknod          = aufs_mknod,
23203 +               .rename         = aufs_rename,
23204 +
23205 +               .permission     = aufs_permission,
23206 +#ifdef CONFIG_FS_POSIX_ACL
23207 +               .get_inode_acl  = aufs_get_inode_acl,
23208 +               .get_acl        = aufs_get_acl,
23209 +               .set_acl        = aufs_set_acl,
23210 +#endif
23211 +
23212 +               .setattr        = aufs_setattr,
23213 +               .getattr        = aufs_getattr,
23214 +
23215 +#ifdef CONFIG_AUFS_XATTR
23216 +               .listxattr      = aufs_listxattr,
23217 +#endif
23218 +
23219 +               .update_time    = aufs_update_time,
23220 +               .atomic_open    = aufs_atomic_open,
23221 +               .tmpfile        = aufs_tmpfile
23222 +       },
23223 +       [AuIop_OTHER] = {
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 +       }
23240 +};
23241 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
23242 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
23243 +++ linux/fs/aufs/i_op_del.c    2023-04-24 08:13:40.969576115 +0200
23244 @@ -0,0 +1,522 @@
23245 +// SPDX-License-Identifier: GPL-2.0
23246 +/*
23247 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23248 + *
23249 + * This program is free software; you can redistribute it and/or modify
23250 + * it under the terms of the GNU General Public License as published by
23251 + * the Free Software Foundation; either version 2 of the License, or
23252 + * (at your option) any later version.
23253 + *
23254 + * This program is distributed in the hope that it will be useful,
23255 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23256 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23257 + * GNU General Public License for more details.
23258 + *
23259 + * You should have received a copy of the GNU General Public License
23260 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23261 + */
23262 +
23263 +/*
23264 + * inode operations (del entry)
23265 + */
23266 +
23267 +#include <linux/iversion.h>
23268 +#include "aufs.h"
23269 +
23270 +/*
23271 + * decide if a new whiteout for @dentry is necessary or not.
23272 + * when it is necessary, prepare the parent dir for the upper branch whose
23273 + * branch index is @bcpup for creation. the actual creation of the whiteout will
23274 + * be done by caller.
23275 + * return value:
23276 + * 0: wh is unnecessary
23277 + * plus: wh is necessary
23278 + * minus: error
23279 + */
23280 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
23281 +{
23282 +       int need_wh, err;
23283 +       aufs_bindex_t btop;
23284 +       struct super_block *sb;
23285 +
23286 +       sb = dentry->d_sb;
23287 +       btop = au_dbtop(dentry);
23288 +       if (*bcpup < 0) {
23289 +               *bcpup = btop;
23290 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
23291 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
23292 +                       *bcpup = err;
23293 +                       if (unlikely(err < 0))
23294 +                               goto out;
23295 +               }
23296 +       } else
23297 +               AuDebugOn(btop < *bcpup
23298 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
23299 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
23300 +
23301 +       if (*bcpup != btop) {
23302 +               err = au_cpup_dirs(dentry, *bcpup);
23303 +               if (unlikely(err))
23304 +                       goto out;
23305 +               need_wh = 1;
23306 +       } else {
23307 +               struct au_dinfo *dinfo, *tmp;
23308 +
23309 +               need_wh = -ENOMEM;
23310 +               dinfo = au_di(dentry);
23311 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23312 +               if (tmp) {
23313 +                       au_di_cp(tmp, dinfo);
23314 +                       au_di_swap(tmp, dinfo);
23315 +                       /* returns the number of positive dentries */
23316 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
23317 +                                                /* AuLkup_IGNORE_PERM */ 0);
23318 +                       au_di_swap(tmp, dinfo);
23319 +                       au_rw_write_unlock(&tmp->di_rwsem);
23320 +                       au_di_free(tmp);
23321 +               }
23322 +       }
23323 +       AuDbg("need_wh %d\n", need_wh);
23324 +       err = need_wh;
23325 +
23326 +out:
23327 +       return err;
23328 +}
23329 +
23330 +/*
23331 + * simple tests for the del-entry operations.
23332 + * following the checks in vfs, plus the parent-child relationship.
23333 + */
23334 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23335 +              struct dentry *h_parent, int isdir)
23336 +{
23337 +       int err;
23338 +       umode_t h_mode;
23339 +       struct dentry *h_dentry, *h_latest;
23340 +       struct inode *h_inode;
23341 +       struct path h_ppath;
23342 +       struct super_block *sb;
23343 +       struct au_branch *br;
23344 +       struct mnt_idmap *h_idmap;
23345 +
23346 +       h_dentry = au_h_dptr(dentry, bindex);
23347 +       if (d_really_is_positive(dentry)) {
23348 +               err = -ENOENT;
23349 +               if (unlikely(d_is_negative(h_dentry)))
23350 +                       goto out;
23351 +               h_inode = d_inode(h_dentry);
23352 +               if (unlikely(!h_inode->i_nlink))
23353 +                       goto out;
23354 +
23355 +               h_mode = h_inode->i_mode;
23356 +               if (!isdir) {
23357 +                       err = -EISDIR;
23358 +                       if (unlikely(S_ISDIR(h_mode)))
23359 +                               goto out;
23360 +               } else if (unlikely(!S_ISDIR(h_mode))) {
23361 +                       err = -ENOTDIR;
23362 +                       goto out;
23363 +               }
23364 +       } else {
23365 +               /* rename(2) case */
23366 +               err = -EIO;
23367 +               if (unlikely(d_is_positive(h_dentry)))
23368 +                       goto out;
23369 +       }
23370 +
23371 +       err = -ENOENT;
23372 +       /* expected parent dir is locked */
23373 +       if (unlikely(h_parent != h_dentry->d_parent))
23374 +               goto out;
23375 +       err = 0;
23376 +
23377 +       /*
23378 +        * rmdir a dir may break the consistency on some filesystem.
23379 +        * let's try heavy test.
23380 +        */
23381 +       err = -EACCES;
23382 +       sb = dentry->d_sb;
23383 +       br = au_sbr(sb, bindex);
23384 +       h_idmap = au_br_idmap(br);
23385 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
23386 +                    && au_test_h_perm(h_idmap, d_inode(h_parent),
23387 +                                      MAY_EXEC | MAY_WRITE)))
23388 +               goto out;
23389 +
23390 +       h_ppath.dentry = h_parent;
23391 +       h_ppath.mnt = au_br_mnt(br);
23392 +       h_latest = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
23393 +       err = -EIO;
23394 +       if (IS_ERR(h_latest))
23395 +               goto out;
23396 +       if (h_latest == h_dentry)
23397 +               err = 0;
23398 +       dput(h_latest);
23399 +
23400 +out:
23401 +       return err;
23402 +}
23403 +
23404 +/*
23405 + * decide the branch where we operate for @dentry. the branch index will be set
23406 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
23407 + * dir for reverting.
23408 + * when a new whiteout is necessary, create it.
23409 + */
23410 +static struct dentry*
23411 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23412 +                   struct au_dtime *dt, struct au_pin *pin)
23413 +{
23414 +       struct dentry *wh_dentry;
23415 +       struct super_block *sb;
23416 +       struct path h_path;
23417 +       int err, need_wh;
23418 +       unsigned int udba;
23419 +       aufs_bindex_t bcpup;
23420 +
23421 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23422 +       wh_dentry = ERR_PTR(need_wh);
23423 +       if (unlikely(need_wh < 0))
23424 +               goto out;
23425 +
23426 +       sb = dentry->d_sb;
23427 +       udba = au_opt_udba(sb);
23428 +       bcpup = *rbcpup;
23429 +       err = au_pin(pin, dentry, bcpup, udba,
23430 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23431 +       wh_dentry = ERR_PTR(err);
23432 +       if (unlikely(err))
23433 +               goto out;
23434 +
23435 +       h_path.dentry = au_pinned_h_parent(pin);
23436 +       if (udba != AuOpt_UDBA_NONE
23437 +           && au_dbtop(dentry) == bcpup) {
23438 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23439 +               wh_dentry = ERR_PTR(err);
23440 +               if (unlikely(err))
23441 +                       goto out_unpin;
23442 +       }
23443 +
23444 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
23445 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23446 +       wh_dentry = NULL;
23447 +       if (!need_wh)
23448 +               goto out; /* success, no need to create whiteout */
23449 +
23450 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23451 +       if (IS_ERR(wh_dentry))
23452 +               goto out_unpin;
23453 +
23454 +       /* returns with the parent is locked and wh_dentry is dget-ed */
23455 +       goto out; /* success */
23456 +
23457 +out_unpin:
23458 +       au_unpin(pin);
23459 +out:
23460 +       return wh_dentry;
23461 +}
23462 +
23463 +/*
23464 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
23465 + * in order to be revertible and save time for removing many child whiteouts
23466 + * under the dir.
23467 + * returns 1 when there are too many child whiteout and caller should remove
23468 + * them asynchronously. returns 0 when the number of children is enough small to
23469 + * remove now or the branch fs is a remote fs.
23470 + * otherwise return an error.
23471 + */
23472 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23473 +                          struct au_nhash *whlist, struct inode *dir)
23474 +{
23475 +       int rmdir_later, err, dirwh;
23476 +       struct dentry *h_dentry;
23477 +       struct super_block *sb;
23478 +       struct inode *inode;
23479 +
23480 +       sb = dentry->d_sb;
23481 +       SiMustAnyLock(sb);
23482 +       h_dentry = au_h_dptr(dentry, bindex);
23483 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23484 +       if (unlikely(err))
23485 +               goto out;
23486 +
23487 +       /* stop monitoring */
23488 +       inode = d_inode(dentry);
23489 +       au_hn_free(au_hi(inode, bindex));
23490 +
23491 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
23492 +               dirwh = au_sbi(sb)->si_dirwh;
23493 +               rmdir_later = (dirwh <= 1);
23494 +               if (!rmdir_later)
23495 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23496 +                                                             dirwh);
23497 +               if (rmdir_later)
23498 +                       return rmdir_later;
23499 +       }
23500 +
23501 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23502 +       if (unlikely(err)) {
23503 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23504 +                       h_dentry, bindex, err);
23505 +               err = 0;
23506 +       }
23507 +
23508 +out:
23509 +       AuTraceErr(err);
23510 +       return err;
23511 +}
23512 +
23513 +/*
23514 + * final procedure for deleting a entry.
23515 + * maintain dentry and iattr.
23516 + */
23517 +static void epilog(struct inode *dir, struct dentry *dentry,
23518 +                  aufs_bindex_t bindex)
23519 +{
23520 +       struct inode *inode;
23521 +
23522 +       inode = d_inode(dentry);
23523 +       d_drop(dentry);
23524 +       inode->i_ctime = dir->i_ctime;
23525 +
23526 +       au_dir_ts(dir, bindex);
23527 +       inode_inc_iversion(dir);
23528 +}
23529 +
23530 +/*
23531 + * when an error happened, remove the created whiteout and revert everything.
23532 + */
23533 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23534 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
23535 +                    struct dentry *dentry, struct au_dtime *dt)
23536 +{
23537 +       int rerr;
23538 +       struct path h_path = {
23539 +               .dentry = wh_dentry,
23540 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
23541 +       };
23542 +
23543 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
23544 +       if (!rerr) {
23545 +               au_set_dbwh(dentry, bwh);
23546 +               au_dtime_revert(dt);
23547 +               return 0;
23548 +       }
23549 +
23550 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
23551 +       return -EIO;
23552 +}
23553 +
23554 +/* ---------------------------------------------------------------------- */
23555 +
23556 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
23557 +{
23558 +       int err;
23559 +       aufs_bindex_t bwh, bindex, btop;
23560 +       struct inode *inode, *h_dir, *delegated;
23561 +       struct dentry *parent, *wh_dentry;
23562 +       /* to reduce stack size */
23563 +       struct {
23564 +               struct au_dtime dt;
23565 +               struct au_pin pin;
23566 +               struct path h_path;
23567 +       } *a;
23568 +
23569 +       IMustLock(dir);
23570 +
23571 +       err = -ENOMEM;
23572 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23573 +       if (unlikely(!a))
23574 +               goto out;
23575 +
23576 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23577 +       if (unlikely(err))
23578 +               goto out_free;
23579 +       err = au_d_hashed_positive(dentry);
23580 +       if (unlikely(err))
23581 +               goto out_unlock;
23582 +       inode = d_inode(dentry);
23583 +       IMustLock(inode);
23584 +       err = -EISDIR;
23585 +       if (unlikely(d_is_dir(dentry)))
23586 +               goto out_unlock; /* possible? */
23587 +
23588 +       btop = au_dbtop(dentry);
23589 +       bwh = au_dbwh(dentry);
23590 +       bindex = -1;
23591 +       parent = dentry->d_parent; /* dir inode is locked */
23592 +       di_write_lock_parent(parent);
23593 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23594 +                                       &a->pin);
23595 +       err = PTR_ERR(wh_dentry);
23596 +       if (IS_ERR(wh_dentry))
23597 +               goto out_parent;
23598 +
23599 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23600 +       a->h_path.dentry = au_h_dptr(dentry, btop);
23601 +       dget(a->h_path.dentry);
23602 +       if (bindex == btop) {
23603 +               h_dir = au_pinned_h_dir(&a->pin);
23604 +               delegated = NULL;
23605 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23606 +               if (unlikely(err == -EWOULDBLOCK)) {
23607 +                       pr_warn("cannot retry for NFSv4 delegation"
23608 +                               " for an internal unlink\n");
23609 +                       iput(delegated);
23610 +               }
23611 +       } else {
23612 +               /* dir inode is locked */
23613 +               h_dir = d_inode(wh_dentry->d_parent);
23614 +               IMustLock(h_dir);
23615 +               err = 0;
23616 +       }
23617 +
23618 +       if (!err) {
23619 +               vfsub_drop_nlink(inode);
23620 +               epilog(dir, dentry, bindex);
23621 +
23622 +               /* update target timestamps */
23623 +               if (bindex == btop) {
23624 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23625 +                       /*ignore*/
23626 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23627 +               } else
23628 +                       /* todo: this timestamp may be reverted later */
23629 +                       inode->i_ctime = h_dir->i_ctime;
23630 +               goto out_unpin; /* success */
23631 +       }
23632 +
23633 +       /* revert */
23634 +       if (wh_dentry) {
23635 +               int rerr;
23636 +
23637 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23638 +                                &a->dt);
23639 +               if (rerr)
23640 +                       err = rerr;
23641 +       }
23642 +
23643 +out_unpin:
23644 +       au_unpin(&a->pin);
23645 +       dput(wh_dentry);
23646 +       dput(a->h_path.dentry);
23647 +out_parent:
23648 +       di_write_unlock(parent);
23649 +out_unlock:
23650 +       aufs_read_unlock(dentry, AuLock_DW);
23651 +out_free:
23652 +       au_kfree_rcu(a);
23653 +out:
23654 +       return err;
23655 +}
23656 +
23657 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
23658 +{
23659 +       int err, rmdir_later;
23660 +       aufs_bindex_t bwh, bindex, btop;
23661 +       struct inode *inode;
23662 +       struct dentry *parent, *wh_dentry, *h_dentry;
23663 +       struct au_whtmp_rmdir *args;
23664 +       /* to reduce stack size */
23665 +       struct {
23666 +               struct au_dtime dt;
23667 +               struct au_pin pin;
23668 +       } *a;
23669 +
23670 +       IMustLock(dir);
23671 +
23672 +       err = -ENOMEM;
23673 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23674 +       if (unlikely(!a))
23675 +               goto out;
23676 +
23677 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23678 +       if (unlikely(err))
23679 +               goto out_free;
23680 +       err = au_alive_dir(dentry);
23681 +       if (unlikely(err))
23682 +               goto out_unlock;
23683 +       inode = d_inode(dentry);
23684 +       IMustLock(inode);
23685 +       err = -ENOTDIR;
23686 +       if (unlikely(!d_is_dir(dentry)))
23687 +               goto out_unlock; /* possible? */
23688 +
23689 +       err = -ENOMEM;
23690 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23691 +       if (unlikely(!args))
23692 +               goto out_unlock;
23693 +
23694 +       parent = dentry->d_parent; /* dir inode is locked */
23695 +       di_write_lock_parent(parent);
23696 +       err = au_test_empty(dentry, &args->whlist);
23697 +       if (unlikely(err))
23698 +               goto out_parent;
23699 +
23700 +       btop = au_dbtop(dentry);
23701 +       bwh = au_dbwh(dentry);
23702 +       bindex = -1;
23703 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23704 +                                       &a->pin);
23705 +       err = PTR_ERR(wh_dentry);
23706 +       if (IS_ERR(wh_dentry))
23707 +               goto out_parent;
23708 +
23709 +       h_dentry = au_h_dptr(dentry, btop);
23710 +       dget(h_dentry);
23711 +       rmdir_later = 0;
23712 +       if (bindex == btop) {
23713 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
23714 +               if (err > 0) {
23715 +                       rmdir_later = err;
23716 +                       err = 0;
23717 +               }
23718 +       } else {
23719 +               /* stop monitoring */
23720 +               au_hn_free(au_hi(inode, btop));
23721 +
23722 +               /* dir inode is locked */
23723 +               IMustLock(d_inode(wh_dentry->d_parent));
23724 +               err = 0;
23725 +       }
23726 +
23727 +       if (!err) {
23728 +               vfsub_dead_dir(inode);
23729 +               au_set_dbdiropq(dentry, -1);
23730 +               epilog(dir, dentry, bindex);
23731 +
23732 +               if (rmdir_later) {
23733 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
23734 +                       args = NULL;
23735 +               }
23736 +
23737 +               goto out_unpin; /* success */
23738 +       }
23739 +
23740 +       /* revert */
23741 +       AuLabel(revert);
23742 +       if (wh_dentry) {
23743 +               int rerr;
23744 +
23745 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23746 +                                &a->dt);
23747 +               if (rerr)
23748 +                       err = rerr;
23749 +       }
23750 +
23751 +out_unpin:
23752 +       au_unpin(&a->pin);
23753 +       dput(wh_dentry);
23754 +       dput(h_dentry);
23755 +out_parent:
23756 +       di_write_unlock(parent);
23757 +       if (args)
23758 +               au_whtmp_rmdir_free(args);
23759 +out_unlock:
23760 +       aufs_read_unlock(dentry, AuLock_DW);
23761 +out_free:
23762 +       au_kfree_rcu(a);
23763 +out:
23764 +       AuTraceErr(err);
23765 +       return err;
23766 +}
23767 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
23768 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
23769 +++ linux/fs/aufs/i_op_ren.c    2023-04-24 08:13:40.969576115 +0200
23770 @@ -0,0 +1,1257 @@
23771 +// SPDX-License-Identifier: GPL-2.0
23772 +/*
23773 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23774 + *
23775 + * This program is free software; you can redistribute it and/or modify
23776 + * it under the terms of the GNU General Public License as published by
23777 + * the Free Software Foundation; either version 2 of the License, or
23778 + * (at your option) any later version.
23779 + *
23780 + * This program is distributed in the hope that it will be useful,
23781 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23782 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23783 + * GNU General Public License for more details.
23784 + *
23785 + * You should have received a copy of the GNU General Public License
23786 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23787 + */
23788 +
23789 +/*
23790 + * inode operation (rename entry)
23791 + * todo: this is crazy monster
23792 + */
23793 +
23794 +#include <linux/iversion.h>
23795 +#include "aufs.h"
23796 +
23797 +enum { AuSRC, AuDST, AuSrcDst };
23798 +enum { AuPARENT, AuCHILD, AuParentChild };
23799 +
23800 +#define AuRen_ISDIR_SRC                1
23801 +#define AuRen_ISDIR_DST                (1 << 1)
23802 +#define AuRen_ISSAMEDIR                (1 << 2)
23803 +#define AuRen_WHSRC            (1 << 3)
23804 +#define AuRen_WHDST            (1 << 4)
23805 +#define AuRen_MNT_WRITE                (1 << 5)
23806 +#define AuRen_DT_DSTDIR                (1 << 6)
23807 +#define AuRen_DIROPQ_SRC       (1 << 7)
23808 +#define AuRen_DIROPQ_DST       (1 << 8)
23809 +#define AuRen_DIRREN           (1 << 9)
23810 +#define AuRen_DROPPED_SRC      (1 << 10)
23811 +#define AuRen_DROPPED_DST      (1 << 11)
23812 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
23813 +#define au_fset_ren(flags, name) \
23814 +       do { (flags) |= AuRen_##name; } while (0)
23815 +#define au_fclr_ren(flags, name) \
23816 +       do { (flags) &= ~AuRen_##name; } while (0)
23817 +
23818 +#ifndef CONFIG_AUFS_DIRREN
23819 +#undef AuRen_DIRREN
23820 +#define AuRen_DIRREN           0
23821 +#endif
23822 +
23823 +struct au_ren_args {
23824 +       struct {
23825 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
23826 +                       *wh_dentry;
23827 +               struct inode *dir, *inode;
23828 +               struct au_hinode *hdir, *hinode;
23829 +               struct au_dtime dt[AuParentChild];
23830 +               aufs_bindex_t btop, bdiropq;
23831 +       } sd[AuSrcDst];
23832 +
23833 +#define src_dentry     sd[AuSRC].dentry
23834 +#define src_dir                sd[AuSRC].dir
23835 +#define src_inode      sd[AuSRC].inode
23836 +#define src_h_dentry   sd[AuSRC].h_dentry
23837 +#define src_parent     sd[AuSRC].parent
23838 +#define src_h_parent   sd[AuSRC].h_parent
23839 +#define src_wh_dentry  sd[AuSRC].wh_dentry
23840 +#define src_hdir       sd[AuSRC].hdir
23841 +#define src_hinode     sd[AuSRC].hinode
23842 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
23843 +#define src_dt         sd[AuSRC].dt
23844 +#define src_btop       sd[AuSRC].btop
23845 +#define src_bdiropq    sd[AuSRC].bdiropq
23846 +
23847 +#define dst_dentry     sd[AuDST].dentry
23848 +#define dst_dir                sd[AuDST].dir
23849 +#define dst_inode      sd[AuDST].inode
23850 +#define dst_h_dentry   sd[AuDST].h_dentry
23851 +#define dst_parent     sd[AuDST].parent
23852 +#define dst_h_parent   sd[AuDST].h_parent
23853 +#define dst_wh_dentry  sd[AuDST].wh_dentry
23854 +#define dst_hdir       sd[AuDST].hdir
23855 +#define dst_hinode     sd[AuDST].hinode
23856 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
23857 +#define dst_dt         sd[AuDST].dt
23858 +#define dst_btop       sd[AuDST].btop
23859 +#define dst_bdiropq    sd[AuDST].bdiropq
23860 +
23861 +       struct dentry *h_trap;
23862 +       struct au_branch *br;
23863 +       struct path h_path;
23864 +       struct au_nhash whlist;
23865 +       aufs_bindex_t btgt, src_bwh;
23866 +
23867 +       struct {
23868 +               unsigned short auren_flags;
23869 +               unsigned char flags;    /* syscall parameter */
23870 +               unsigned char exchange;
23871 +       } __packed;
23872 +
23873 +       struct au_whtmp_rmdir *thargs;
23874 +       struct dentry *h_dst;
23875 +       struct au_hinode *h_root;
23876 +};
23877 +
23878 +/* ---------------------------------------------------------------------- */
23879 +
23880 +/*
23881 + * functions for reverting.
23882 + * when an error happened in a single rename systemcall, we should revert
23883 + * everything as if nothing happened.
23884 + * we don't need to revert the copied-up/down the parent dir since they are
23885 + * harmless.
23886 + */
23887 +
23888 +#define RevertFailure(fmt, ...) do { \
23889 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23890 +               ##__VA_ARGS__, err, rerr); \
23891 +       err = -EIO; \
23892 +} while (0)
23893 +
23894 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
23895 +{
23896 +       int rerr;
23897 +       struct dentry *d;
23898 +#define src_or_dst(member) a->sd[idx].member
23899 +
23900 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
23901 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23902 +       rerr = au_diropq_remove(d, a->btgt);
23903 +       au_hn_inode_unlock(src_or_dst(hinode));
23904 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
23905 +       if (rerr)
23906 +               RevertFailure("remove diropq %pd", d);
23907 +
23908 +#undef src_or_dst_
23909 +}
23910 +
23911 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23912 +{
23913 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23914 +               au_ren_do_rev_diropq(err, a, AuSRC);
23915 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23916 +               au_ren_do_rev_diropq(err, a, AuDST);
23917 +}
23918 +
23919 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
23920 +{
23921 +       int rerr;
23922 +       struct inode *delegated;
23923 +       struct path h_ppath = {
23924 +               .dentry = a->src_h_parent,
23925 +               .mnt    = a->h_path.mnt
23926 +       };
23927 +
23928 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
23929 +       rerr = PTR_ERR(a->h_path.dentry);
23930 +       if (IS_ERR(a->h_path.dentry)) {
23931 +               RevertFailure("lkup one %pd", a->src_dentry);
23932 +               return;
23933 +       }
23934 +
23935 +       delegated = NULL;
23936 +       rerr = vfsub_rename(a->dst_h_dir,
23937 +                           au_h_dptr(a->src_dentry, a->btgt),
23938 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
23939 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23940 +               pr_warn("cannot retry for NFSv4 delegation"
23941 +                       " for an internal rename\n");
23942 +               iput(delegated);
23943 +       }
23944 +       d_drop(a->h_path.dentry);
23945 +       dput(a->h_path.dentry);
23946 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23947 +       if (rerr)
23948 +               RevertFailure("rename %pd", a->src_dentry);
23949 +}
23950 +
23951 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
23952 +{
23953 +       int rerr;
23954 +       struct inode *delegated;
23955 +       struct path h_ppath = {
23956 +               .dentry = a->dst_h_parent,
23957 +               .mnt    = a->h_path.mnt
23958 +       };
23959 +
23960 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
23961 +       rerr = PTR_ERR(a->h_path.dentry);
23962 +       if (IS_ERR(a->h_path.dentry)) {
23963 +               RevertFailure("lkup one %pd", a->dst_dentry);
23964 +               return;
23965 +       }
23966 +       if (d_is_positive(a->h_path.dentry)) {
23967 +               d_drop(a->h_path.dentry);
23968 +               dput(a->h_path.dentry);
23969 +               return;
23970 +       }
23971 +
23972 +       delegated = NULL;
23973 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
23974 +                           &delegated, a->flags);
23975 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23976 +               pr_warn("cannot retry for NFSv4 delegation"
23977 +                       " for an internal rename\n");
23978 +               iput(delegated);
23979 +       }
23980 +       d_drop(a->h_path.dentry);
23981 +       dput(a->h_path.dentry);
23982 +       if (!rerr)
23983 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
23984 +       else
23985 +               RevertFailure("rename %pd", a->h_dst);
23986 +}
23987 +
23988 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
23989 +{
23990 +       int rerr;
23991 +
23992 +       a->h_path.dentry = a->src_wh_dentry;
23993 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
23994 +       au_set_dbwh(a->src_dentry, a->src_bwh);
23995 +       if (rerr)
23996 +               RevertFailure("unlink %pd", a->src_wh_dentry);
23997 +}
23998 +#undef RevertFailure
23999 +
24000 +/* ---------------------------------------------------------------------- */
24001 +
24002 +/*
24003 + * when we have to copyup the renaming entry, do it with the rename-target name
24004 + * in order to minimize the cost (the later actual rename is unnecessary).
24005 + * otherwise rename it on the target branch.
24006 + */
24007 +static int au_ren_or_cpup(struct au_ren_args *a)
24008 +{
24009 +       int err;
24010 +       struct dentry *d;
24011 +       struct inode *delegated;
24012 +
24013 +       d = a->src_dentry;
24014 +       if (au_dbtop(d) == a->btgt) {
24015 +               a->h_path.dentry = a->dst_h_dentry;
24016 +               AuDebugOn(au_dbtop(d) != a->btgt);
24017 +               delegated = NULL;
24018 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
24019 +                                  a->dst_h_dir, &a->h_path, &delegated,
24020 +                                  a->flags);
24021 +               if (unlikely(err == -EWOULDBLOCK)) {
24022 +                       pr_warn("cannot retry for NFSv4 delegation"
24023 +                               " for an internal rename\n");
24024 +                       iput(delegated);
24025 +               }
24026 +       } else
24027 +               BUG();
24028 +
24029 +       if (!err && a->h_dst)
24030 +               /* it will be set to dinfo later */
24031 +               dget(a->h_dst);
24032 +
24033 +       return err;
24034 +}
24035 +
24036 +/* cf. aufs_rmdir() */
24037 +static int au_ren_del_whtmp(struct au_ren_args *a)
24038 +{
24039 +       int err;
24040 +       struct inode *dir;
24041 +
24042 +       dir = a->dst_dir;
24043 +       SiMustAnyLock(dir->i_sb);
24044 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
24045 +                                    au_sbi(dir->i_sb)->si_dirwh)
24046 +           || au_test_fs_remote(a->h_dst->d_sb)) {
24047 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
24048 +               if (unlikely(err))
24049 +                       pr_warn("failed removing whtmp dir %pd (%d), "
24050 +                               "ignored.\n", a->h_dst, err);
24051 +       } else {
24052 +               au_nhash_wh_free(&a->thargs->whlist);
24053 +               a->thargs->whlist = a->whlist;
24054 +               a->whlist.nh_num = 0;
24055 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24056 +               dput(a->h_dst);
24057 +               a->thargs = NULL;
24058 +       }
24059 +
24060 +       return 0;
24061 +}
24062 +
24063 +/* make it 'opaque' dir. */
24064 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
24065 +{
24066 +       int err;
24067 +       struct dentry *d, *diropq;
24068 +#define src_or_dst(member) a->sd[idx].member
24069 +
24070 +       err = 0;
24071 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
24072 +       src_or_dst(bdiropq) = au_dbdiropq(d);
24073 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24074 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24075 +       diropq = au_diropq_create(d, a->btgt);
24076 +       au_hn_inode_unlock(src_or_dst(hinode));
24077 +       if (IS_ERR(diropq))
24078 +               err = PTR_ERR(diropq);
24079 +       else
24080 +               dput(diropq);
24081 +
24082 +#undef src_or_dst_
24083 +       return err;
24084 +}
24085 +
24086 +static int au_ren_diropq(struct au_ren_args *a)
24087 +{
24088 +       int err;
24089 +       unsigned char always;
24090 +       struct dentry *d;
24091 +
24092 +       err = 0;
24093 +       d = a->dst_dentry; /* already renamed on the branch */
24094 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24095 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24096 +           && !au_ftest_ren(a->auren_flags, DIRREN)
24097 +           && a->btgt != au_dbdiropq(a->src_dentry)
24098 +           && (a->dst_wh_dentry
24099 +               || a->btgt <= au_dbdiropq(d)
24100 +               /* hide the lower to keep xino */
24101 +               /* the lowers may not be a dir, but we hide them anyway */
24102 +               || a->btgt < au_dbbot(d)
24103 +               || always)) {
24104 +               AuDbg("here\n");
24105 +               err = au_ren_do_diropq(a, AuSRC);
24106 +               if (unlikely(err))
24107 +                       goto out;
24108 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
24109 +       }
24110 +       if (!a->exchange)
24111 +               goto out; /* success */
24112 +
24113 +       d = a->src_dentry; /* already renamed on the branch */
24114 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24115 +           && a->btgt != au_dbdiropq(a->dst_dentry)
24116 +           && (a->btgt < au_dbdiropq(d)
24117 +               || a->btgt < au_dbbot(d)
24118 +               || always)) {
24119 +               AuDbgDentry(a->src_dentry);
24120 +               AuDbgDentry(a->dst_dentry);
24121 +               err = au_ren_do_diropq(a, AuDST);
24122 +               if (unlikely(err))
24123 +                       goto out_rev_src;
24124 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
24125 +       }
24126 +       goto out; /* success */
24127 +
24128 +out_rev_src:
24129 +       AuDbg("err %d, reverting src\n", err);
24130 +       au_ren_rev_diropq(err, a);
24131 +out:
24132 +       return err;
24133 +}
24134 +
24135 +static int do_rename(struct au_ren_args *a)
24136 +{
24137 +       int err;
24138 +       struct dentry *d, *h_d;
24139 +
24140 +       if (!a->exchange) {
24141 +               /* prepare workqueue args for asynchronous rmdir */
24142 +               h_d = a->dst_h_dentry;
24143 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24144 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
24145 +                   && d_is_positive(h_d)) {
24146 +                       err = -ENOMEM;
24147 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24148 +                                                        GFP_NOFS);
24149 +                       if (unlikely(!a->thargs))
24150 +                               goto out;
24151 +                       a->h_dst = dget(h_d);
24152 +               }
24153 +
24154 +               /* create whiteout for src_dentry */
24155 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
24156 +                       a->src_bwh = au_dbwh(a->src_dentry);
24157 +                       AuDebugOn(a->src_bwh >= 0);
24158 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24159 +                                                       a->src_h_parent);
24160 +                       err = PTR_ERR(a->src_wh_dentry);
24161 +                       if (IS_ERR(a->src_wh_dentry))
24162 +                               goto out_thargs;
24163 +               }
24164 +
24165 +               /* lookup whiteout for dentry */
24166 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
24167 +                       h_d = au_wh_lkup(a->dst_h_parent,
24168 +                                        &a->dst_dentry->d_name, a->br);
24169 +                       err = PTR_ERR(h_d);
24170 +                       if (IS_ERR(h_d))
24171 +                               goto out_whsrc;
24172 +                       if (d_is_negative(h_d))
24173 +                               dput(h_d);
24174 +                       else
24175 +                               a->dst_wh_dentry = h_d;
24176 +               }
24177 +
24178 +               /* rename dentry to tmpwh */
24179 +               if (a->thargs) {
24180 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
24181 +                       if (unlikely(err))
24182 +                               goto out_whdst;
24183 +
24184 +                       d = a->dst_dentry;
24185 +                       au_set_h_dptr(d, a->btgt, NULL);
24186 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
24187 +                       if (unlikely(err))
24188 +                               goto out_whtmp;
24189 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
24190 +               }
24191 +       }
24192 +
24193 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
24194 +#if 0 /* debugging */
24195 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24196 +              && d_is_positive(a->dst_h_dentry)
24197 +              && a->src_btop != a->btgt);
24198 +#endif
24199 +
24200 +       /* rename by vfs_rename or cpup */
24201 +       err = au_ren_or_cpup(a);
24202 +       if (unlikely(err))
24203 +               /* leave the copied-up one */
24204 +               goto out_whtmp;
24205 +
24206 +       /* make dir opaque */
24207 +       err = au_ren_diropq(a);
24208 +       if (unlikely(err))
24209 +               goto out_rename;
24210 +
24211 +       /* update target timestamps */
24212 +       if (a->exchange) {
24213 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24214 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24215 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24216 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24217 +       }
24218 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
24219 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24220 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24221 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24222 +
24223 +       if (!a->exchange) {
24224 +               /* remove whiteout for dentry */
24225 +               if (a->dst_wh_dentry) {
24226 +                       a->h_path.dentry = a->dst_wh_dentry;
24227 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24228 +                                                 a->dst_dentry);
24229 +                       if (unlikely(err))
24230 +                               goto out_diropq;
24231 +               }
24232 +
24233 +               /* remove whtmp */
24234 +               if (a->thargs)
24235 +                       au_ren_del_whtmp(a); /* ignore this error */
24236 +
24237 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24238 +       }
24239 +       err = 0;
24240 +       goto out_success;
24241 +
24242 +out_diropq:
24243 +       au_ren_rev_diropq(err, a);
24244 +out_rename:
24245 +       au_ren_rev_rename(err, a);
24246 +       dput(a->h_dst);
24247 +out_whtmp:
24248 +       if (a->thargs)
24249 +               au_ren_rev_whtmp(err, a);
24250 +out_whdst:
24251 +       dput(a->dst_wh_dentry);
24252 +       a->dst_wh_dentry = NULL;
24253 +out_whsrc:
24254 +       if (a->src_wh_dentry)
24255 +               au_ren_rev_whsrc(err, a);
24256 +out_success:
24257 +       dput(a->src_wh_dentry);
24258 +       dput(a->dst_wh_dentry);
24259 +out_thargs:
24260 +       if (a->thargs) {
24261 +               dput(a->h_dst);
24262 +               au_whtmp_rmdir_free(a->thargs);
24263 +               a->thargs = NULL;
24264 +       }
24265 +out:
24266 +       return err;
24267 +}
24268 +
24269 +/* ---------------------------------------------------------------------- */
24270 +
24271 +/*
24272 + * test if @dentry dir can be rename destination or not.
24273 + * success means, it is a logically empty dir.
24274 + */
24275 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
24276 +{
24277 +       return au_test_empty(dentry, whlist);
24278 +}
24279 +
24280 +/*
24281 + * test if @a->src_dentry dir can be rename source or not.
24282 + * if it can, return 0.
24283 + * success means,
24284 + * - it is a logically empty dir.
24285 + * - or, it exists on writable branch and has no children including whiteouts
24286 + *   on the lower branch unless DIRREN is on.
24287 + */
24288 +static int may_rename_srcdir(struct au_ren_args *a)
24289 +{
24290 +       int err;
24291 +       unsigned int rdhash;
24292 +       aufs_bindex_t btop, btgt;
24293 +       struct dentry *dentry;
24294 +       struct super_block *sb;
24295 +       struct au_sbinfo *sbinfo;
24296 +
24297 +       dentry = a->src_dentry;
24298 +       sb = dentry->d_sb;
24299 +       sbinfo = au_sbi(sb);
24300 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24301 +               au_fset_ren(a->auren_flags, DIRREN);
24302 +
24303 +       btgt = a->btgt;
24304 +       btop = au_dbtop(dentry);
24305 +       if (btop != btgt) {
24306 +               struct au_nhash whlist;
24307 +
24308 +               SiMustAnyLock(sb);
24309 +               rdhash = sbinfo->si_rdhash;
24310 +               if (!rdhash)
24311 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24312 +                                                          dentry));
24313 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24314 +               if (unlikely(err))
24315 +                       goto out;
24316 +               err = au_test_empty(dentry, &whlist);
24317 +               au_nhash_wh_free(&whlist);
24318 +               goto out;
24319 +       }
24320 +
24321 +       if (btop == au_dbtaildir(dentry))
24322 +               return 0; /* success */
24323 +
24324 +       err = au_test_empty_lower(dentry);
24325 +
24326 +out:
24327 +       if (err == -ENOTEMPTY) {
24328 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
24329 +                       err = 0;
24330 +               } else {
24331 +                       AuWarn1("renaming dir who has child(ren) on multiple "
24332 +                               "branches, is not supported\n");
24333 +                       err = -EXDEV;
24334 +               }
24335 +       }
24336 +       return err;
24337 +}
24338 +
24339 +/* side effect: sets whlist and h_dentry */
24340 +static int au_ren_may_dir(struct au_ren_args *a)
24341 +{
24342 +       int err;
24343 +       unsigned int rdhash;
24344 +       struct dentry *d;
24345 +
24346 +       d = a->dst_dentry;
24347 +       SiMustAnyLock(d->d_sb);
24348 +
24349 +       err = 0;
24350 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
24351 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
24352 +               if (!rdhash)
24353 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24354 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24355 +               if (unlikely(err))
24356 +                       goto out;
24357 +
24358 +               if (!a->exchange) {
24359 +                       au_set_dbtop(d, a->dst_btop);
24360 +                       err = may_rename_dstdir(d, &a->whlist);
24361 +                       au_set_dbtop(d, a->btgt);
24362 +               } else
24363 +                       err = may_rename_srcdir(a);
24364 +       }
24365 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
24366 +       if (unlikely(err))
24367 +               goto out;
24368 +
24369 +       d = a->src_dentry;
24370 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
24371 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24372 +               err = may_rename_srcdir(a);
24373 +               if (unlikely(err)) {
24374 +                       au_nhash_wh_free(&a->whlist);
24375 +                       a->whlist.nh_num = 0;
24376 +               }
24377 +       }
24378 +out:
24379 +       return err;
24380 +}
24381 +
24382 +/* ---------------------------------------------------------------------- */
24383 +
24384 +/*
24385 + * simple tests for rename.
24386 + * following the checks in vfs, plus the parent-child relationship.
24387 + */
24388 +static int au_may_ren(struct au_ren_args *a)
24389 +{
24390 +       int err, isdir;
24391 +       struct inode *h_inode;
24392 +
24393 +       if (a->src_btop == a->btgt) {
24394 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
24395 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
24396 +               if (unlikely(err))
24397 +                       goto out;
24398 +               err = -EINVAL;
24399 +               if (unlikely(a->src_h_dentry == a->h_trap))
24400 +                       goto out;
24401 +       }
24402 +
24403 +       err = 0;
24404 +       if (a->dst_btop != a->btgt)
24405 +               goto out;
24406 +
24407 +       err = -ENOTEMPTY;
24408 +       if (unlikely(a->dst_h_dentry == a->h_trap))
24409 +               goto out;
24410 +
24411 +       err = -EIO;
24412 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
24413 +       if (d_really_is_negative(a->dst_dentry)) {
24414 +               if (d_is_negative(a->dst_h_dentry))
24415 +                       err = au_may_add(a->dst_dentry, a->btgt,
24416 +                                        a->dst_h_parent, isdir);
24417 +       } else {
24418 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
24419 +                       goto out;
24420 +               h_inode = d_inode(a->dst_h_dentry);
24421 +               if (h_inode->i_nlink)
24422 +                       err = au_may_del(a->dst_dentry, a->btgt,
24423 +                                        a->dst_h_parent, isdir);
24424 +       }
24425 +
24426 +out:
24427 +       if (unlikely(err == -ENOENT || err == -EEXIST))
24428 +               err = -EIO;
24429 +       AuTraceErr(err);
24430 +       return err;
24431 +}
24432 +
24433 +/* ---------------------------------------------------------------------- */
24434 +
24435 +/*
24436 + * locking order
24437 + * (VFS)
24438 + * - src_dir and dir by lock_rename()
24439 + * - inode if exists
24440 + * (aufs)
24441 + * - lock all
24442 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24443 + *     + si_read_lock
24444 + *     + di_write_lock2_child()
24445 + *       + di_write_lock_child()
24446 + *        + ii_write_lock_child()
24447 + *       + di_write_lock_child2()
24448 + *        + ii_write_lock_child2()
24449 + *     + src_parent and parent
24450 + *       + di_write_lock_parent()
24451 + *        + ii_write_lock_parent()
24452 + *       + di_write_lock_parent2()
24453 + *        + ii_write_lock_parent2()
24454 + *   + lower src_dir and dir by vfsub_lock_rename()
24455 + *   + verify the every relationships between child and parent. if any
24456 + *     of them failed, unlock all and return -EBUSY.
24457 + */
24458 +static void au_ren_unlock(struct au_ren_args *a)
24459 +{
24460 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24461 +                           a->dst_h_parent, a->dst_hdir);
24462 +       if (au_ftest_ren(a->auren_flags, DIRREN)
24463 +           && a->h_root)
24464 +               au_hn_inode_unlock(a->h_root);
24465 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
24466 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
24467 +}
24468 +
24469 +static int au_ren_lock(struct au_ren_args *a)
24470 +{
24471 +       int err;
24472 +       unsigned int udba;
24473 +
24474 +       err = 0;
24475 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24476 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
24477 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24478 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
24479 +
24480 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
24481 +       if (unlikely(err))
24482 +               goto out;
24483 +       au_fset_ren(a->auren_flags, MNT_WRITE);
24484 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24485 +               struct dentry *root;
24486 +               struct inode *dir;
24487 +
24488 +               /*
24489 +                * sbinfo is already locked, so this ii_read_lock is
24490 +                * unnecessary. but our debugging feature checks it.
24491 +                */
24492 +               root = a->src_inode->i_sb->s_root;
24493 +               if (root != a->src_parent && root != a->dst_parent) {
24494 +                       dir = d_inode(root);
24495 +                       ii_read_lock_parent3(dir);
24496 +                       a->h_root = au_hi(dir, a->btgt);
24497 +                       ii_read_unlock(dir);
24498 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24499 +               }
24500 +       }
24501 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24502 +                                     a->dst_h_parent, a->dst_hdir);
24503 +       udba = au_opt_udba(a->src_dentry->d_sb);
24504 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24505 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
24506 +               err = au_busy_or_stale();
24507 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
24508 +               err = au_h_verify(a->src_h_dentry, udba,
24509 +                                 d_inode(a->src_h_parent), a->src_h_parent,
24510 +                                 a->br);
24511 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
24512 +               err = au_h_verify(a->dst_h_dentry, udba,
24513 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
24514 +                                 a->br);
24515 +       if (!err)
24516 +               goto out; /* success */
24517 +
24518 +       err = au_busy_or_stale();
24519 +       au_ren_unlock(a);
24520 +
24521 +out:
24522 +       return err;
24523 +}
24524 +
24525 +/* ---------------------------------------------------------------------- */
24526 +
24527 +static void au_ren_refresh_dir(struct au_ren_args *a)
24528 +{
24529 +       struct inode *dir;
24530 +
24531 +       dir = a->dst_dir;
24532 +       inode_inc_iversion(dir);
24533 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24534 +               /* is this updating defined in POSIX? */
24535 +               au_cpup_attr_timesizes(a->src_inode);
24536 +               au_cpup_attr_nlink(dir, /*force*/1);
24537 +       }
24538 +       au_dir_ts(dir, a->btgt);
24539 +
24540 +       if (a->exchange) {
24541 +               dir = a->src_dir;
24542 +               inode_inc_iversion(dir);
24543 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24544 +                       /* is this updating defined in POSIX? */
24545 +                       au_cpup_attr_timesizes(a->dst_inode);
24546 +                       au_cpup_attr_nlink(dir, /*force*/1);
24547 +               }
24548 +               au_dir_ts(dir, a->btgt);
24549 +       }
24550 +
24551 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24552 +               return;
24553 +
24554 +       dir = a->src_dir;
24555 +       inode_inc_iversion(dir);
24556 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
24557 +               au_cpup_attr_nlink(dir, /*force*/1);
24558 +       au_dir_ts(dir, a->btgt);
24559 +}
24560 +
24561 +static void au_ren_refresh(struct au_ren_args *a)
24562 +{
24563 +       aufs_bindex_t bbot, bindex;
24564 +       struct dentry *d, *h_d;
24565 +       struct inode *i, *h_i;
24566 +       struct super_block *sb;
24567 +
24568 +       d = a->dst_dentry;
24569 +       d_drop(d);
24570 +       if (a->h_dst)
24571 +               /* already dget-ed by au_ren_or_cpup() */
24572 +               au_set_h_dptr(d, a->btgt, a->h_dst);
24573 +
24574 +       i = a->dst_inode;
24575 +       if (i) {
24576 +               if (!a->exchange) {
24577 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24578 +                               vfsub_drop_nlink(i);
24579 +                       else {
24580 +                               vfsub_dead_dir(i);
24581 +                               au_cpup_attr_timesizes(i);
24582 +                       }
24583 +                       au_update_dbrange(d, /*do_put_zero*/1);
24584 +               } else
24585 +                       au_cpup_attr_nlink(i, /*force*/1);
24586 +       } else {
24587 +               bbot = a->btgt;
24588 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
24589 +                       au_set_h_dptr(d, bindex, NULL);
24590 +               bbot = au_dbbot(d);
24591 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
24592 +                       au_set_h_dptr(d, bindex, NULL);
24593 +               au_update_dbrange(d, /*do_put_zero*/0);
24594 +       }
24595 +
24596 +       if (a->exchange
24597 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
24598 +               d_drop(a->src_dentry);
24599 +               if (au_ftest_ren(a->auren_flags, DIRREN))
24600 +                       au_set_dbwh(a->src_dentry, -1);
24601 +               return;
24602 +       }
24603 +
24604 +       d = a->src_dentry;
24605 +       au_set_dbwh(d, -1);
24606 +       bbot = au_dbbot(d);
24607 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24608 +               h_d = au_h_dptr(d, bindex);
24609 +               if (h_d)
24610 +                       au_set_h_dptr(d, bindex, NULL);
24611 +       }
24612 +       au_set_dbbot(d, a->btgt);
24613 +
24614 +       sb = d->d_sb;
24615 +       i = a->src_inode;
24616 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24617 +               return; /* success */
24618 +
24619 +       bbot = au_ibbot(i);
24620 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24621 +               h_i = au_h_iptr(i, bindex);
24622 +               if (h_i) {
24623 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24624 +                       /* ignore this error */
24625 +                       au_set_h_iptr(i, bindex, NULL, 0);
24626 +               }
24627 +       }
24628 +       au_set_ibbot(i, a->btgt);
24629 +}
24630 +
24631 +/* ---------------------------------------------------------------------- */
24632 +
24633 +/* mainly for link(2) and rename(2) */
24634 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
24635 +{
24636 +       aufs_bindex_t bdiropq, bwh;
24637 +       struct dentry *parent;
24638 +       struct au_branch *br;
24639 +
24640 +       parent = dentry->d_parent;
24641 +       IMustLock(d_inode(parent)); /* dir is locked */
24642 +
24643 +       bdiropq = au_dbdiropq(parent);
24644 +       bwh = au_dbwh(dentry);
24645 +       br = au_sbr(dentry->d_sb, btgt);
24646 +       if (au_br_rdonly(br)
24647 +           || (0 <= bdiropq && bdiropq < btgt)
24648 +           || (0 <= bwh && bwh < btgt))
24649 +               btgt = -1;
24650 +
24651 +       AuDbg("btgt %d\n", btgt);
24652 +       return btgt;
24653 +}
24654 +
24655 +/* sets src_btop, dst_btop and btgt */
24656 +static int au_ren_wbr(struct au_ren_args *a)
24657 +{
24658 +       int err;
24659 +       struct au_wr_dir_args wr_dir_args = {
24660 +               /* .force_btgt  = -1, */
24661 +               .flags          = AuWrDir_ADD_ENTRY
24662 +       };
24663 +
24664 +       a->src_btop = au_dbtop(a->src_dentry);
24665 +       a->dst_btop = au_dbtop(a->dst_dentry);
24666 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24667 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
24668 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
24669 +       wr_dir_args.force_btgt = a->src_btop;
24670 +       if (a->dst_inode && a->dst_btop < a->src_btop)
24671 +               wr_dir_args.force_btgt = a->dst_btop;
24672 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24673 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24674 +       a->btgt = err;
24675 +       if (a->exchange)
24676 +               au_update_dbtop(a->dst_dentry);
24677 +
24678 +       return err;
24679 +}
24680 +
24681 +static void au_ren_dt(struct au_ren_args *a)
24682 +{
24683 +       a->h_path.dentry = a->src_h_parent;
24684 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
24685 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
24686 +               a->h_path.dentry = a->dst_h_parent;
24687 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24688 +       }
24689 +
24690 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
24691 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24692 +           && !a->exchange)
24693 +               return;
24694 +
24695 +       a->h_path.dentry = a->src_h_dentry;
24696 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
24697 +       if (d_is_positive(a->dst_h_dentry)) {
24698 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
24699 +               a->h_path.dentry = a->dst_h_dentry;
24700 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24701 +       }
24702 +}
24703 +
24704 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
24705 +{
24706 +       struct dentry *h_d;
24707 +       struct inode *h_inode;
24708 +
24709 +       au_dtime_revert(a->src_dt + AuPARENT);
24710 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
24711 +               au_dtime_revert(a->dst_dt + AuPARENT);
24712 +
24713 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
24714 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
24715 +               h_inode = d_inode(h_d);
24716 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
24717 +               au_dtime_revert(a->src_dt + AuCHILD);
24718 +               inode_unlock(h_inode);
24719 +
24720 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
24721 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
24722 +                       h_inode = d_inode(h_d);
24723 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
24724 +                       au_dtime_revert(a->dst_dt + AuCHILD);
24725 +                       inode_unlock(h_inode);
24726 +               }
24727 +       }
24728 +}
24729 +
24730 +/* ---------------------------------------------------------------------- */
24731 +
24732 +int aufs_rename(struct mnt_idmap *idmap,
24733 +               struct inode *_src_dir, struct dentry *_src_dentry,
24734 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
24735 +               unsigned int _flags)
24736 +{
24737 +       int err, lock_flags;
24738 +       void *rev;
24739 +       /* reduce stack space */
24740 +       struct au_ren_args *a;
24741 +       struct au_pin pin;
24742 +
24743 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
24744 +       IMustLock(_src_dir);
24745 +       IMustLock(_dst_dir);
24746 +
24747 +       err = -EINVAL;
24748 +       if (unlikely(_flags & RENAME_WHITEOUT))
24749 +               goto out;
24750 +
24751 +       err = -ENOMEM;
24752 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24753 +       a = kzalloc(sizeof(*a), GFP_NOFS);
24754 +       if (unlikely(!a))
24755 +               goto out;
24756 +
24757 +       a->flags = _flags;
24758 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24759 +                    && RENAME_EXCHANGE > U8_MAX);
24760 +       a->exchange = _flags & RENAME_EXCHANGE;
24761 +       a->src_dir = _src_dir;
24762 +       a->src_dentry = _src_dentry;
24763 +       a->src_inode = NULL;
24764 +       if (d_really_is_positive(a->src_dentry))
24765 +               a->src_inode = d_inode(a->src_dentry);
24766 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24767 +       a->dst_dir = _dst_dir;
24768 +       a->dst_dentry = _dst_dentry;
24769 +       a->dst_inode = NULL;
24770 +       if (d_really_is_positive(a->dst_dentry))
24771 +               a->dst_inode = d_inode(a->dst_dentry);
24772 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24773 +       if (a->dst_inode) {
24774 +               /*
24775 +                * if EXCHANGE && src is non-dir && dst is dir,
24776 +                * dst is not locked.
24777 +                */
24778 +               /* IMustLock(a->dst_inode); */
24779 +               au_igrab(a->dst_inode);
24780 +       }
24781 +
24782 +       err = -ENOTDIR;
24783 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
24784 +       if (d_is_dir(a->src_dentry)) {
24785 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
24786 +               if (unlikely(!a->exchange
24787 +                            && d_really_is_positive(a->dst_dentry)
24788 +                            && !d_is_dir(a->dst_dentry)))
24789 +                       goto out_free;
24790 +               lock_flags |= AuLock_DIRS;
24791 +       }
24792 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24793 +               au_fset_ren(a->auren_flags, ISDIR_DST);
24794 +               if (unlikely(!a->exchange
24795 +                            && d_really_is_positive(a->src_dentry)
24796 +                            && !d_is_dir(a->src_dentry)))
24797 +                       goto out_free;
24798 +               lock_flags |= AuLock_DIRS;
24799 +       }
24800 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24801 +                                       lock_flags);
24802 +       if (unlikely(err))
24803 +               goto out_free;
24804 +
24805 +       err = au_d_hashed_positive(a->src_dentry);
24806 +       if (unlikely(err))
24807 +               goto out_unlock;
24808 +       err = -ENOENT;
24809 +       if (a->dst_inode) {
24810 +               /*
24811 +                * If it is a dir, VFS unhash it before this
24812 +                * function. It means we cannot rely upon d_unhashed().
24813 +                */
24814 +               if (unlikely(!a->dst_inode->i_nlink))
24815 +                       goto out_unlock;
24816 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24817 +                       err = au_d_hashed_positive(a->dst_dentry);
24818 +                       if (unlikely(err && !a->exchange))
24819 +                               goto out_unlock;
24820 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24821 +                       goto out_unlock;
24822 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
24823 +               goto out_unlock;
24824 +
24825 +       /*
24826 +        * is it possible?
24827 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
24828 +        * there may exist a problem somewhere else.
24829 +        */
24830 +       err = -EINVAL;
24831 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
24832 +               goto out_unlock;
24833 +
24834 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
24835 +       di_write_lock_parent(a->dst_parent);
24836 +
24837 +       /* which branch we process */
24838 +       err = au_ren_wbr(a);
24839 +       if (unlikely(err < 0))
24840 +               goto out_parent;
24841 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
24842 +       a->h_path.mnt = au_br_mnt(a->br);
24843 +
24844 +       /* are they available to be renamed */
24845 +       err = au_ren_may_dir(a);
24846 +       if (unlikely(err))
24847 +               goto out_children;
24848 +
24849 +       /* prepare the writable parent dir on the same branch */
24850 +       if (a->dst_btop == a->btgt) {
24851 +               au_fset_ren(a->auren_flags, WHDST);
24852 +       } else {
24853 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
24854 +               if (unlikely(err))
24855 +                       goto out_children;
24856 +       }
24857 +
24858 +       err = 0;
24859 +       if (!a->exchange) {
24860 +               if (a->src_dir != a->dst_dir) {
24861 +                       /*
24862 +                        * this temporary unlock is safe,
24863 +                        * because both dir->i_mutex are locked.
24864 +                        */
24865 +                       di_write_unlock(a->dst_parent);
24866 +                       di_write_lock_parent(a->src_parent);
24867 +                       err = au_wr_dir_need_wh(a->src_dentry,
24868 +                                               au_ftest_ren(a->auren_flags,
24869 +                                                            ISDIR_SRC),
24870 +                                               &a->btgt);
24871 +                       di_write_unlock(a->src_parent);
24872 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
24873 +                                             /*isdir*/1);
24874 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
24875 +               } else
24876 +                       err = au_wr_dir_need_wh(a->src_dentry,
24877 +                                               au_ftest_ren(a->auren_flags,
24878 +                                                            ISDIR_SRC),
24879 +                                               &a->btgt);
24880 +       }
24881 +       if (unlikely(err < 0))
24882 +               goto out_children;
24883 +       if (err)
24884 +               au_fset_ren(a->auren_flags, WHSRC);
24885 +
24886 +       /* cpup src */
24887 +       if (a->src_btop != a->btgt) {
24888 +               err = au_pin(&pin, a->src_dentry, a->btgt,
24889 +                            au_opt_udba(a->src_dentry->d_sb),
24890 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24891 +               if (!err) {
24892 +                       struct au_cp_generic cpg = {
24893 +                               .dentry = a->src_dentry,
24894 +                               .bdst   = a->btgt,
24895 +                               .bsrc   = a->src_btop,
24896 +                               .len    = -1,
24897 +                               .pin    = &pin,
24898 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24899 +                       };
24900 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
24901 +                       err = au_sio_cpup_simple(&cpg);
24902 +                       au_unpin(&pin);
24903 +               }
24904 +               if (unlikely(err))
24905 +                       goto out_children;
24906 +               a->src_btop = a->btgt;
24907 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
24908 +               if (!a->exchange)
24909 +                       au_fset_ren(a->auren_flags, WHSRC);
24910 +       }
24911 +
24912 +       /* cpup dst */
24913 +       if (a->exchange && a->dst_inode
24914 +           && a->dst_btop != a->btgt) {
24915 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
24916 +                            au_opt_udba(a->dst_dentry->d_sb),
24917 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24918 +               if (!err) {
24919 +                       struct au_cp_generic cpg = {
24920 +                               .dentry = a->dst_dentry,
24921 +                               .bdst   = a->btgt,
24922 +                               .bsrc   = a->dst_btop,
24923 +                               .len    = -1,
24924 +                               .pin    = &pin,
24925 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24926 +                       };
24927 +                       err = au_sio_cpup_simple(&cpg);
24928 +                       au_unpin(&pin);
24929 +               }
24930 +               if (unlikely(err))
24931 +                       goto out_children;
24932 +               a->dst_btop = a->btgt;
24933 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
24934 +       }
24935 +
24936 +       /* lock them all */
24937 +       err = au_ren_lock(a);
24938 +       if (unlikely(err))
24939 +               /* leave the copied-up one */
24940 +               goto out_children;
24941 +
24942 +       if (!a->exchange) {
24943 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24944 +                       err = au_may_ren(a);
24945 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24946 +                       err = -ENAMETOOLONG;
24947 +               if (unlikely(err))
24948 +                       goto out_hdir;
24949 +       }
24950 +
24951 +       /* store timestamps to be revertible */
24952 +       au_ren_dt(a);
24953 +
24954 +       /* store dirren info */
24955 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24956 +               err = au_dr_rename(a->src_dentry, a->btgt,
24957 +                                  &a->dst_dentry->d_name, &rev);
24958 +               AuTraceErr(err);
24959 +               if (unlikely(err))
24960 +                       goto out_dt;
24961 +       }
24962 +
24963 +       /* here we go */
24964 +       err = do_rename(a);
24965 +       if (unlikely(err))
24966 +               goto out_dirren;
24967 +
24968 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24969 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
24970 +
24971 +       /* update dir attributes */
24972 +       au_ren_refresh_dir(a);
24973 +
24974 +       /* dput/iput all lower dentries */
24975 +       au_ren_refresh(a);
24976 +
24977 +       goto out_hdir; /* success */
24978 +
24979 +out_dirren:
24980 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24981 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
24982 +out_dt:
24983 +       au_ren_rev_dt(err, a);
24984 +out_hdir:
24985 +       au_ren_unlock(a);
24986 +out_children:
24987 +       au_nhash_wh_free(&a->whlist);
24988 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
24989 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
24990 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
24991 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
24992 +       }
24993 +out_parent:
24994 +       if (!err) {
24995 +               if (d_unhashed(a->src_dentry))
24996 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
24997 +               if (d_unhashed(a->dst_dentry))
24998 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
24999 +               if (!a->exchange)
25000 +                       d_move(a->src_dentry, a->dst_dentry);
25001 +               else {
25002 +                       d_exchange(a->src_dentry, a->dst_dentry);
25003 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
25004 +                               d_drop(a->dst_dentry);
25005 +               }
25006 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
25007 +                       d_drop(a->src_dentry);
25008 +       } else {
25009 +               au_update_dbtop(a->dst_dentry);
25010 +               if (!a->dst_inode)
25011 +                       d_drop(a->dst_dentry);
25012 +       }
25013 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
25014 +               di_write_unlock(a->dst_parent);
25015 +       else
25016 +               di_write_unlock2(a->src_parent, a->dst_parent);
25017 +out_unlock:
25018 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
25019 +out_free:
25020 +       iput(a->dst_inode);
25021 +       if (a->thargs)
25022 +               au_whtmp_rmdir_free(a->thargs);
25023 +       au_kfree_rcu(a);
25024 +out:
25025 +       AuTraceErr(err);
25026 +       return err;
25027 +}
25028 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
25029 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
25030 +++ linux/fs/aufs/Kconfig       2022-11-05 23:02:18.959222617 +0100
25031 @@ -0,0 +1,199 @@
25032 +# SPDX-License-Identifier: GPL-2.0
25033 +config AUFS_FS
25034 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
25035 +       help
25036 +       Aufs is a stackable unification filesystem such as Unionfs,
25037 +       which unifies several directories and provides a merged single
25038 +       directory.
25039 +       In the early days, aufs was entirely re-designed and
25040 +       re-implemented Unionfs Version 1.x series. Introducing many
25041 +       original ideas, approaches and improvements, it becomes totally
25042 +       different from Unionfs while keeping the basic features.
25043 +
25044 +if AUFS_FS
25045 +choice
25046 +       prompt "Maximum number of branches"
25047 +       default AUFS_BRANCH_MAX_127
25048 +       help
25049 +       Specifies the maximum number of branches (or member directories)
25050 +       in a single aufs. The larger value consumes more system
25051 +       resources and has a minor impact to performance.
25052 +config AUFS_BRANCH_MAX_127
25053 +       bool "127"
25054 +       help
25055 +       Specifies the maximum number of branches (or member directories)
25056 +       in a single aufs. The larger value consumes more system
25057 +       resources and has a minor impact to performance.
25058 +config AUFS_BRANCH_MAX_511
25059 +       bool "511"
25060 +       help
25061 +       Specifies the maximum number of branches (or member directories)
25062 +       in a single aufs. The larger value consumes more system
25063 +       resources and has a minor impact to performance.
25064 +config AUFS_BRANCH_MAX_1023
25065 +       bool "1023"
25066 +       help
25067 +       Specifies the maximum number of branches (or member directories)
25068 +       in a single aufs. The larger value consumes more system
25069 +       resources and has a minor impact to performance.
25070 +config AUFS_BRANCH_MAX_32767
25071 +       bool "32767"
25072 +       help
25073 +       Specifies the maximum number of branches (or member directories)
25074 +       in a single aufs. The larger value consumes more system
25075 +       resources and has a minor impact to performance.
25076 +endchoice
25077 +
25078 +config AUFS_SBILIST
25079 +       bool
25080 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
25081 +       default y
25082 +       help
25083 +       Automatic configuration for internal use.
25084 +       When aufs supports Magic SysRq or /proc, enabled automatically.
25085 +
25086 +config AUFS_HNOTIFY
25087 +       bool "Detect direct branch access (bypassing aufs)"
25088 +       help
25089 +       If you want to modify files on branches directly, eg. bypassing aufs,
25090 +       and want aufs to detect the changes of them fully, then enable this
25091 +       option and use 'udba=notify' mount option.
25092 +       Currently there is only one available configuration, "fsnotify".
25093 +       It will have a negative impact to the performance.
25094 +       See detail in aufs.5.
25095 +
25096 +choice
25097 +       prompt "method" if AUFS_HNOTIFY
25098 +       default AUFS_HFSNOTIFY
25099 +config AUFS_HFSNOTIFY
25100 +       bool "fsnotify"
25101 +       select FSNOTIFY
25102 +endchoice
25103 +
25104 +config AUFS_EXPORT
25105 +       bool "NFS-exportable aufs"
25106 +       depends on EXPORTFS
25107 +       help
25108 +       If you want to export your mounted aufs via NFS, then enable this
25109 +       option. There are several requirements for this configuration.
25110 +       See detail in aufs.5.
25111 +
25112 +config AUFS_INO_T_64
25113 +       bool
25114 +       depends on AUFS_EXPORT
25115 +       depends on 64BIT && !(ALPHA || S390)
25116 +       default y
25117 +       help
25118 +       Automatic configuration for internal use.
25119 +       /* typedef unsigned long/int __kernel_ino_t */
25120 +       /* alpha and s390x are int */
25121 +
25122 +config AUFS_XATTR
25123 +       bool "support for XATTR/EA (including Security Labels)"
25124 +       help
25125 +       If your branch fs supports XATTR/EA and you want to make them
25126 +       available in aufs too, then enable this opsion and specify the
25127 +       branch attributes for EA.
25128 +       See detail in aufs.5.
25129 +
25130 +config AUFS_FHSM
25131 +       bool "File-based Hierarchical Storage Management"
25132 +       help
25133 +       Hierarchical Storage Management (or HSM) is a well-known feature
25134 +       in the storage world. Aufs provides this feature as file-based.
25135 +       with multiple branches.
25136 +       These multiple branches are prioritized, ie. the topmost one
25137 +       should be the fastest drive and be used heavily.
25138 +
25139 +config AUFS_RDU
25140 +       bool "Readdir in userspace"
25141 +       help
25142 +       Aufs has two methods to provide a merged view for a directory,
25143 +       by a user-space library and by kernel-space natively. The latter
25144 +       is always enabled but sometimes large and slow.
25145 +       If you enable this option, install the library in aufs2-util
25146 +       package, and set some environment variables for your readdir(3),
25147 +       then the work will be handled in user-space which generally
25148 +       shows better performance in most cases.
25149 +       See detail in aufs.5.
25150 +
25151 +config AUFS_DIRREN
25152 +       bool "Workaround for rename(2)-ing a directory"
25153 +       help
25154 +       By default, aufs returns EXDEV error in renameing a dir who has
25155 +       his child on the lower branch, since it is a bad idea to issue
25156 +       rename(2) internally for every lower branch. But user may not
25157 +       accept this behaviour. So here is a workaround to allow such
25158 +       rename(2) and store some extra information on the writable
25159 +       branch. Obviously this costs high (and I don't like it).
25160 +       To use this feature, you need to enable this configuration AND
25161 +       to specify the mount option `dirren.'
25162 +       See details in aufs.5 and the design documents.
25163 +
25164 +config AUFS_SHWH
25165 +       bool "Show whiteouts"
25166 +       help
25167 +       If you want to make the whiteouts in aufs visible, then enable
25168 +       this option and specify 'shwh' mount option. Although it may
25169 +       sounds like philosophy or something, but in technically it
25170 +       simply shows the name of whiteout with keeping its behaviour.
25171 +
25172 +config AUFS_BR_RAMFS
25173 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
25174 +       help
25175 +       If you want to use ramfs as an aufs branch fs, then enable this
25176 +       option. Generally tmpfs is recommended.
25177 +       Aufs prohibited them to be a branch fs by default, because
25178 +       initramfs becomes unusable after switch_root or something
25179 +       generally. If you sets initramfs as an aufs branch and boot your
25180 +       system by switch_root, you will meet a problem easily since the
25181 +       files in initramfs may be inaccessible.
25182 +       Unless you are going to use ramfs as an aufs branch fs without
25183 +       switch_root or something, leave it N.
25184 +
25185 +config AUFS_BR_FUSE
25186 +       bool "Fuse fs as an aufs branch"
25187 +       depends on FUSE_FS
25188 +       select AUFS_POLL
25189 +       help
25190 +       If you want to use fuse-based userspace filesystem as an aufs
25191 +       branch fs, then enable this option.
25192 +       It implements the internal poll(2) operation which is
25193 +       implemented by fuse only (curretnly).
25194 +
25195 +config AUFS_POLL
25196 +       bool
25197 +       help
25198 +       Automatic configuration for internal use.
25199 +
25200 +config AUFS_BR_HFSPLUS
25201 +       bool "Hfsplus as an aufs branch"
25202 +       depends on HFSPLUS_FS
25203 +       default y
25204 +       help
25205 +       If you want to use hfsplus fs as an aufs branch fs, then enable
25206 +       this option. This option introduces a small overhead at
25207 +       copying-up a file on hfsplus.
25208 +
25209 +config AUFS_BDEV_LOOP
25210 +       bool
25211 +       depends on BLK_DEV_LOOP
25212 +       default y
25213 +       help
25214 +       Automatic configuration for internal use.
25215 +       Convert =[ym] into =y.
25216 +
25217 +config AUFS_DEBUG
25218 +       bool "Debug aufs"
25219 +       help
25220 +       Enable this to compile aufs internal debug code.
25221 +       It will have a negative impact to the performance.
25222 +
25223 +config AUFS_MAGIC_SYSRQ
25224 +       bool
25225 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
25226 +       default y
25227 +       help
25228 +       Automatic configuration for internal use.
25229 +       When aufs supports Magic SysRq, enabled automatically.
25230 +endif
25231 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
25232 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
25233 +++ linux/fs/aufs/lcnt.h        2022-11-05 23:02:18.965889284 +0100
25234 @@ -0,0 +1,186 @@
25235 +/* SPDX-License-Identifier: GPL-2.0 */
25236 +/*
25237 + * Copyright (C) 2018-2022 Junjiro R. Okajima
25238 + *
25239 + * This program is free software; you can redistribute it and/or modify
25240 + * it under the terms of the GNU General Public License as published by
25241 + * the Free Software Foundation; either version 2 of the License, or
25242 + * (at your option) any later version.
25243 + *
25244 + * This program is distributed in the hope that it will be useful,
25245 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25246 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25247 + * GNU General Public License for more details.
25248 + *
25249 + * You should have received a copy of the GNU General Public License
25250 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25251 + */
25252 +
25253 +/*
25254 + * simple long counter wrapper
25255 + */
25256 +
25257 +#ifndef __AUFS_LCNT_H__
25258 +#define __AUFS_LCNT_H__
25259 +
25260 +#ifdef __KERNEL__
25261 +
25262 +#include "debug.h"
25263 +
25264 +#define AuLCntATOMIC   1
25265 +#define AuLCntPCPUCNT  2
25266 +/*
25267 + * why does percpu_refcount require extra synchronize_rcu()s in
25268 + * au_br_do_free()
25269 + */
25270 +#define AuLCntPCPUREF  3
25271 +
25272 +/* #define AuLCntChosen        AuLCntATOMIC */
25273 +#define AuLCntChosen   AuLCntPCPUCNT
25274 +/* #define AuLCntChosen        AuLCntPCPUREF */
25275 +
25276 +#if AuLCntChosen == AuLCntATOMIC
25277 +#include <linux/atomic.h>
25278 +
25279 +typedef atomic_long_t au_lcnt_t;
25280 +
25281 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25282 +{
25283 +       atomic_long_set(cnt, 0);
25284 +       return 0;
25285 +}
25286 +
25287 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25288 +{
25289 +       /* empty */
25290 +}
25291 +
25292 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25293 +                              int do_sync __maybe_unused)
25294 +{
25295 +       /* empty */
25296 +}
25297 +
25298 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25299 +{
25300 +       atomic_long_inc(cnt);
25301 +}
25302 +
25303 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25304 +{
25305 +       atomic_long_dec(cnt);
25306 +}
25307 +
25308 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25309 +{
25310 +       return atomic_long_read(cnt);
25311 +}
25312 +#endif
25313 +
25314 +#if AuLCntChosen == AuLCntPCPUCNT
25315 +#include <linux/percpu_counter.h>
25316 +
25317 +typedef struct percpu_counter au_lcnt_t;
25318 +
25319 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25320 +{
25321 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
25322 +}
25323 +
25324 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25325 +{
25326 +       /* empty */
25327 +}
25328 +
25329 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25330 +{
25331 +       percpu_counter_destroy(cnt);
25332 +}
25333 +
25334 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25335 +{
25336 +       percpu_counter_inc(cnt);
25337 +}
25338 +
25339 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25340 +{
25341 +       percpu_counter_dec(cnt);
25342 +}
25343 +
25344 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25345 +{
25346 +       s64 n;
25347 +
25348 +       n = percpu_counter_sum(cnt);
25349 +       BUG_ON(n < 0);
25350 +       if (LONG_MAX != LLONG_MAX
25351 +           && n > LONG_MAX)
25352 +               AuWarn1("%s\n", "wrap-around");
25353 +
25354 +       return n;
25355 +}
25356 +#endif
25357 +
25358 +#if AuLCntChosen == AuLCntPCPUREF
25359 +#include <linux/percpu-refcount.h>
25360 +
25361 +typedef struct percpu_ref au_lcnt_t;
25362 +
25363 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25364 +{
25365 +       if (!release)
25366 +               release = percpu_ref_exit;
25367 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25368 +}
25369 +
25370 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25371 +{
25372 +       synchronize_rcu();
25373 +}
25374 +
25375 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25376 +{
25377 +       percpu_ref_kill(cnt);
25378 +       if (do_sync)
25379 +               au_lcnt_wait_for_fin(cnt);
25380 +}
25381 +
25382 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25383 +{
25384 +       percpu_ref_get(cnt);
25385 +}
25386 +
25387 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25388 +{
25389 +       percpu_ref_put(cnt);
25390 +}
25391 +
25392 +/*
25393 + * avoid calling this func as possible.
25394 + */
25395 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25396 +{
25397 +       long l;
25398 +
25399 +       percpu_ref_switch_to_atomic_sync(cnt);
25400 +       l = atomic_long_read(&cnt->count);
25401 +       if (do_rev)
25402 +               percpu_ref_switch_to_percpu(cnt);
25403 +
25404 +       /* percpu_ref is initialized by 1 instead of 0 */
25405 +       return l - 1;
25406 +}
25407 +#endif
25408 +
25409 +#ifdef CONFIG_AUFS_DEBUG
25410 +#define AuLCntZero(val) do {                   \
25411 +       long l = val;                           \
25412 +       if (l)                                  \
25413 +               AuDbg("%s = %ld\n", #val, l);   \
25414 +} while (0)
25415 +#else
25416 +#define AuLCntZero(val)                do {} while (0)
25417 +#endif
25418 +
25419 +#endif /* __KERNEL__ */
25420 +#endif /* __AUFS_LCNT_H__ */
25421 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
25422 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
25423 +++ linux/fs/aufs/loop.c        2022-11-05 23:02:18.965889284 +0100
25424 @@ -0,0 +1,148 @@
25425 +// SPDX-License-Identifier: GPL-2.0
25426 +/*
25427 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25428 + *
25429 + * This program is free software; you can redistribute it and/or modify
25430 + * it under the terms of the GNU General Public License as published by
25431 + * the Free Software Foundation; either version 2 of the License, or
25432 + * (at your option) any later version.
25433 + *
25434 + * This program is distributed in the hope that it will be useful,
25435 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25436 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25437 + * GNU General Public License for more details.
25438 + *
25439 + * You should have received a copy of the GNU General Public License
25440 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25441 + */
25442 +
25443 +/*
25444 + * support for loopback block device as a branch
25445 + */
25446 +
25447 +#include "aufs.h"
25448 +
25449 +/* added into drivers/block/loop.c */
25450 +static struct file *(*backing_file_func)(struct super_block *sb);
25451 +
25452 +/*
25453 + * test if two lower dentries have overlapping branches.
25454 + */
25455 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25456 +{
25457 +       struct super_block *h_sb;
25458 +       struct file *backing_file;
25459 +
25460 +       if (unlikely(!backing_file_func)) {
25461 +               /* don't load "loop" module here */
25462 +               backing_file_func = symbol_get(loop_backing_file);
25463 +               if (unlikely(!backing_file_func))
25464 +                       /* "loop" module is not loaded */
25465 +                       return 0;
25466 +       }
25467 +
25468 +       h_sb = h_adding->d_sb;
25469 +       backing_file = backing_file_func(h_sb);
25470 +       if (!backing_file)
25471 +               return 0;
25472 +
25473 +       h_adding = backing_file->f_path.dentry;
25474 +       /*
25475 +        * h_adding can be local NFS.
25476 +        * in this case aufs cannot detect the loop.
25477 +        */
25478 +       if (unlikely(h_adding->d_sb == sb))
25479 +               return 1;
25480 +       return !!au_test_subdir(h_adding, sb->s_root);
25481 +}
25482 +
25483 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25484 +int au_test_loopback_kthread(void)
25485 +{
25486 +       int ret;
25487 +       struct task_struct *tsk = current;
25488 +       char c, comm[sizeof(tsk->comm)];
25489 +
25490 +       ret = 0;
25491 +       if (tsk->flags & PF_KTHREAD) {
25492 +               get_task_comm(comm, tsk);
25493 +               c = comm[4];
25494 +               ret = ('0' <= c && c <= '9'
25495 +                      && !strncmp(comm, "loop", 4));
25496 +       }
25497 +
25498 +       return ret;
25499 +}
25500 +
25501 +/* ---------------------------------------------------------------------- */
25502 +
25503 +#define au_warn_loopback_step  16
25504 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25505 +static unsigned long *au_warn_loopback_array;
25506 +
25507 +void au_warn_loopback(struct super_block *h_sb)
25508 +{
25509 +       int i, new_nelem;
25510 +       unsigned long *a, magic;
25511 +       static DEFINE_SPINLOCK(spin);
25512 +
25513 +       magic = h_sb->s_magic;
25514 +       spin_lock(&spin);
25515 +       a = au_warn_loopback_array;
25516 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25517 +               if (a[i] == magic) {
25518 +                       spin_unlock(&spin);
25519 +                       return;
25520 +               }
25521 +
25522 +       /* h_sb is new to us, print it */
25523 +       if (i < au_warn_loopback_nelem) {
25524 +               a[i] = magic;
25525 +               goto pr;
25526 +       }
25527 +
25528 +       /* expand the array */
25529 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25530 +       a = au_kzrealloc(au_warn_loopback_array,
25531 +                        au_warn_loopback_nelem * sizeof(unsigned long),
25532 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25533 +                        /*may_shrink*/0);
25534 +       if (a) {
25535 +               au_warn_loopback_nelem = new_nelem;
25536 +               au_warn_loopback_array = a;
25537 +               a[i] = magic;
25538 +               goto pr;
25539 +       }
25540 +
25541 +       spin_unlock(&spin);
25542 +       AuWarn1("realloc failed, ignored\n");
25543 +       return;
25544 +
25545 +pr:
25546 +       spin_unlock(&spin);
25547 +       pr_warn("you may want to try another patch for loopback file "
25548 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25549 +}
25550 +
25551 +int au_loopback_init(void)
25552 +{
25553 +       int err;
25554 +       struct super_block *sb __maybe_unused;
25555 +
25556 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25557 +
25558 +       err = 0;
25559 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25560 +                                        sizeof(unsigned long), GFP_NOFS);
25561 +       if (unlikely(!au_warn_loopback_array))
25562 +               err = -ENOMEM;
25563 +
25564 +       return err;
25565 +}
25566 +
25567 +void au_loopback_fin(void)
25568 +{
25569 +       if (backing_file_func)
25570 +               symbol_put(loop_backing_file);
25571 +       au_kfree_try_rcu(au_warn_loopback_array);
25572 +}
25573 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
25574 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
25575 +++ linux/fs/aufs/loop.h        2022-11-05 23:02:18.965889284 +0100
25576 @@ -0,0 +1,55 @@
25577 +/* SPDX-License-Identifier: GPL-2.0 */
25578 +/*
25579 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25580 + *
25581 + * This program is free software; you can redistribute it and/or modify
25582 + * it under the terms of the GNU General Public License as published by
25583 + * the Free Software Foundation; either version 2 of the License, or
25584 + * (at your option) any later version.
25585 + *
25586 + * This program is distributed in the hope that it will be useful,
25587 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25588 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25589 + * GNU General Public License for more details.
25590 + *
25591 + * You should have received a copy of the GNU General Public License
25592 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25593 + */
25594 +
25595 +/*
25596 + * support for loopback mount as a branch
25597 + */
25598 +
25599 +#ifndef __AUFS_LOOP_H__
25600 +#define __AUFS_LOOP_H__
25601 +
25602 +#ifdef __KERNEL__
25603 +
25604 +struct dentry;
25605 +struct super_block;
25606 +
25607 +#ifdef CONFIG_AUFS_BDEV_LOOP
25608 +/* drivers/block/loop.c */
25609 +struct file *loop_backing_file(struct super_block *sb);
25610 +
25611 +/* loop.c */
25612 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25613 +int au_test_loopback_kthread(void);
25614 +void au_warn_loopback(struct super_block *h_sb);
25615 +
25616 +int au_loopback_init(void);
25617 +void au_loopback_fin(void);
25618 +#else
25619 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25620 +
25621 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25622 +          struct dentry *h_adding)
25623 +AuStubInt0(au_test_loopback_kthread, void)
25624 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25625 +
25626 +AuStubInt0(au_loopback_init, void)
25627 +AuStubVoid(au_loopback_fin, void)
25628 +#endif /* BLK_DEV_LOOP */
25629 +
25630 +#endif /* __KERNEL__ */
25631 +#endif /* __AUFS_LOOP_H__ */
25632 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
25633 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
25634 +++ linux/fs/aufs/magic.mk      2022-11-05 23:02:18.965889284 +0100
25635 @@ -0,0 +1,31 @@
25636 +# SPDX-License-Identifier: GPL-2.0
25637 +
25638 +# defined in ${srctree}/fs/fuse/inode.c
25639 +# tristate
25640 +ifdef CONFIG_FUSE_FS
25641 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25642 +endif
25643 +
25644 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25645 +# tristate
25646 +ifdef CONFIG_XFS_FS
25647 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25648 +endif
25649 +
25650 +# defined in ${srctree}/fs/configfs/mount.c
25651 +# tristate
25652 +ifdef CONFIG_CONFIGFS_FS
25653 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25654 +endif
25655 +
25656 +# defined in ${srctree}/fs/ubifs/ubifs.h
25657 +# tristate
25658 +ifdef CONFIG_UBIFS_FS
25659 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25660 +endif
25661 +
25662 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25663 +# tristate
25664 +ifdef CONFIG_HFSPLUS_FS
25665 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25666 +endif
25667 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
25668 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
25669 +++ linux/fs/aufs/Makefile      2022-11-05 23:02:18.959222617 +0100
25670 @@ -0,0 +1,46 @@
25671 +# SPDX-License-Identifier: GPL-2.0
25672 +
25673 +include ${src}/magic.mk
25674 +ifeq (${CONFIG_AUFS_FS},m)
25675 +include ${src}/conf.mk
25676 +endif
25677 +-include ${src}/priv_def.mk
25678 +
25679 +# cf. include/linux/kernel.h
25680 +# enable pr_debug
25681 +ccflags-y += -DDEBUG
25682 +# sparse requires the full pathname
25683 +ifdef M
25684 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
25685 +else
25686 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
25687 +endif
25688 +
25689 +obj-$(CONFIG_AUFS_FS) += aufs.o
25690 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
25691 +       wkq.o vfsub.o dcsub.o \
25692 +       cpup.o whout.o wbr_policy.o \
25693 +       dinfo.o dentry.o \
25694 +       dynop.o \
25695 +       finfo.o file.o f_op.o \
25696 +       dir.o vdir.o \
25697 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
25698 +       mvdown.o ioctl.o
25699 +
25700 +# all are boolean
25701 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
25702 +aufs-$(CONFIG_SYSFS) += sysfs.o
25703 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25704 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25705 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25706 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
25707 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
25708 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25709 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
25710 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
25711 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
25712 +aufs-$(CONFIG_AUFS_POLL) += poll.o
25713 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
25714 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25715 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25716 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
25717 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
25718 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
25719 +++ linux/fs/aufs/module.c      2022-11-05 23:02:18.965889284 +0100
25720 @@ -0,0 +1,273 @@
25721 +// SPDX-License-Identifier: GPL-2.0
25722 +/*
25723 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25724 + *
25725 + * This program is free software; you can redistribute it and/or modify
25726 + * it under the terms of the GNU General Public License as published by
25727 + * the Free Software Foundation; either version 2 of the License, or
25728 + * (at your option) any later version.
25729 + *
25730 + * This program is distributed in the hope that it will be useful,
25731 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25732 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25733 + * GNU General Public License for more details.
25734 + *
25735 + * You should have received a copy of the GNU General Public License
25736 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25737 + */
25738 +
25739 +/*
25740 + * module global variables and operations
25741 + */
25742 +
25743 +#include <linux/module.h>
25744 +#include <linux/seq_file.h>
25745 +#include "aufs.h"
25746 +
25747 +/* shrinkable realloc */
25748 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25749 +{
25750 +       size_t sz;
25751 +       int diff;
25752 +
25753 +       sz = 0;
25754 +       diff = -1;
25755 +       if (p) {
25756 +#if 0 /* unused */
25757 +               if (!new_sz) {
25758 +                       au_kfree_rcu(p);
25759 +                       p = NULL;
25760 +                       goto out;
25761 +               }
25762 +#else
25763 +               AuDebugOn(!new_sz);
25764 +#endif
25765 +               sz = ksize(p);
25766 +               diff = au_kmidx_sub(sz, new_sz);
25767 +       }
25768 +       if (sz && !diff)
25769 +               goto out;
25770 +
25771 +       if (sz < new_sz)
25772 +               /* expand or SLOB */
25773 +               p = krealloc(p, new_sz, gfp);
25774 +       else if (new_sz < sz && may_shrink) {
25775 +               /* shrink */
25776 +               void *q;
25777 +
25778 +               q = kmalloc(new_sz, gfp);
25779 +               if (q) {
25780 +                       if (p) {
25781 +                               memcpy(q, p, new_sz);
25782 +                               au_kfree_try_rcu(p);
25783 +                       }
25784 +                       p = q;
25785 +               } else
25786 +                       p = NULL;
25787 +       }
25788 +
25789 +out:
25790 +       return p;
25791 +}
25792 +
25793 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25794 +                  int may_shrink)
25795 +{
25796 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
25797 +       if (p && new_sz > nused)
25798 +               memset(p + nused, 0, new_sz - nused);
25799 +       return p;
25800 +}
25801 +
25802 +/* ---------------------------------------------------------------------- */
25803 +/*
25804 + * aufs caches
25805 + */
25806 +struct kmem_cache *au_cache[AuCache_Last];
25807 +
25808 +static void au_cache_fin(void)
25809 +{
25810 +       int i;
25811 +
25812 +       /*
25813 +        * Make sure all delayed rcu free inodes are flushed before we
25814 +        * destroy cache.
25815 +        */
25816 +       rcu_barrier();
25817 +
25818 +       /* excluding AuCache_HNOTIFY */
25819 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25820 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
25821 +               kmem_cache_destroy(au_cache[i]);
25822 +               au_cache[i] = NULL;
25823 +       }
25824 +}
25825 +
25826 +static int __init au_cache_init(void)
25827 +{
25828 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25829 +       if (au_cache[AuCache_DINFO])
25830 +               /* SLAB_DESTROY_BY_RCU */
25831 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25832 +                                                      au_icntnr_init_once);
25833 +       if (au_cache[AuCache_ICNTNR])
25834 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25835 +                                                     au_fi_init_once);
25836 +       if (au_cache[AuCache_FINFO])
25837 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
25838 +       if (au_cache[AuCache_VDIR])
25839 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25840 +       if (au_cache[AuCache_DEHSTR])
25841 +               return 0;
25842 +
25843 +       au_cache_fin();
25844 +       return -ENOMEM;
25845 +}
25846 +
25847 +/* ---------------------------------------------------------------------- */
25848 +
25849 +int au_dir_roflags;
25850 +
25851 +#ifdef CONFIG_AUFS_SBILIST
25852 +/*
25853 + * iterate_supers_type() doesn't protect us from
25854 + * remounting (branch management)
25855 + */
25856 +struct hlist_bl_head au_sbilist;
25857 +#endif
25858 +
25859 +/*
25860 + * functions for module interface.
25861 + */
25862 +MODULE_LICENSE("GPL");
25863 +/* MODULE_LICENSE("GPL v2"); */
25864 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25865 +MODULE_DESCRIPTION(AUFS_NAME
25866 +       " -- Advanced multi layered unification filesystem");
25867 +MODULE_VERSION(AUFS_VERSION);
25868 +MODULE_ALIAS_FS(AUFS_NAME);
25869 +
25870 +/* this module parameter has no meaning when SYSFS is disabled */
25871 +int sysaufs_brs = 1;
25872 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25873 +module_param_named(brs, sysaufs_brs, int, 0444);
25874 +
25875 +/* this module parameter has no meaning when USER_NS is disabled */
25876 +bool au_userns;
25877 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25878 +module_param_named(allow_userns, au_userns, bool, 0444);
25879 +
25880 +/* ---------------------------------------------------------------------- */
25881 +
25882 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25883 +
25884 +int au_seq_path(struct seq_file *seq, struct path *path)
25885 +{
25886 +       int err;
25887 +
25888 +       err = seq_path(seq, path, au_esc_chars);
25889 +       if (err >= 0)
25890 +               err = 0;
25891 +       else
25892 +               err = -ENOMEM;
25893 +
25894 +       return err;
25895 +}
25896 +
25897 +/* ---------------------------------------------------------------------- */
25898 +
25899 +static int __init aufs_init(void)
25900 +{
25901 +       int err, i;
25902 +       char *p;
25903 +
25904 +       p = au_esc_chars;
25905 +       for (i = 1; i <= ' '; i++)
25906 +               *p++ = i;
25907 +       *p++ = '\\';
25908 +       *p++ = '\x7f';
25909 +       *p = 0;
25910 +
25911 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25912 +
25913 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25914 +       for (i = 0; i < AuIop_Last; i++)
25915 +               aufs_iop_nogetattr[i].getattr = NULL;
25916 +
25917 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
25918 +
25919 +       au_sbilist_init();
25920 +       sysaufs_brs_init();
25921 +       au_debug_init();
25922 +       au_dy_init();
25923 +       err = sysaufs_init();
25924 +       if (unlikely(err))
25925 +               goto out;
25926 +       err = dbgaufs_init();
25927 +       if (unlikely(err))
25928 +               goto out_sysaufs;
25929 +       err = au_procfs_init();
25930 +       if (unlikely(err))
25931 +               goto out_dbgaufs;
25932 +       err = au_wkq_init();
25933 +       if (unlikely(err))
25934 +               goto out_procfs;
25935 +       err = au_loopback_init();
25936 +       if (unlikely(err))
25937 +               goto out_wkq;
25938 +       err = au_hnotify_init();
25939 +       if (unlikely(err))
25940 +               goto out_loopback;
25941 +       err = au_sysrq_init();
25942 +       if (unlikely(err))
25943 +               goto out_hin;
25944 +       err = au_cache_init();
25945 +       if (unlikely(err))
25946 +               goto out_sysrq;
25947 +
25948 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25949 +       err = register_filesystem(&aufs_fs_type);
25950 +       if (unlikely(err))
25951 +               goto out_cache;
25952 +
25953 +       /* since we define pr_fmt, call printk directly */
25954 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25955 +       goto out; /* success */
25956 +
25957 +out_cache:
25958 +       au_cache_fin();
25959 +out_sysrq:
25960 +       au_sysrq_fin();
25961 +out_hin:
25962 +       au_hnotify_fin();
25963 +out_loopback:
25964 +       au_loopback_fin();
25965 +out_wkq:
25966 +       au_wkq_fin();
25967 +out_procfs:
25968 +       au_procfs_fin();
25969 +out_dbgaufs:
25970 +       dbgaufs_fin();
25971 +out_sysaufs:
25972 +       sysaufs_fin();
25973 +       au_dy_fin();
25974 +out:
25975 +       return err;
25976 +}
25977 +
25978 +static void __exit aufs_exit(void)
25979 +{
25980 +       unregister_filesystem(&aufs_fs_type);
25981 +       au_cache_fin();
25982 +       au_sysrq_fin();
25983 +       au_hnotify_fin();
25984 +       au_loopback_fin();
25985 +       au_wkq_fin();
25986 +       au_procfs_fin();
25987 +       dbgaufs_fin();
25988 +       sysaufs_fin();
25989 +       au_dy_fin();
25990 +}
25991 +
25992 +module_init(aufs_init);
25993 +module_exit(aufs_exit);
25994 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
25995 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
25996 +++ linux/fs/aufs/module.h      2022-11-05 23:02:18.969222617 +0100
25997 @@ -0,0 +1,180 @@
25998 +/* SPDX-License-Identifier: GPL-2.0 */
25999 +/*
26000 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26001 + *
26002 + * This program is free software; you can redistribute it and/or modify
26003 + * it under the terms of the GNU General Public License as published by
26004 + * the Free Software Foundation; either version 2 of the License, or
26005 + * (at your option) any later version.
26006 + *
26007 + * This program is distributed in the hope that it will be useful,
26008 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26009 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26010 + * GNU General Public License for more details.
26011 + *
26012 + * You should have received a copy of the GNU General Public License
26013 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26014 + */
26015 +
26016 +/*
26017 + * module initialization and module-global
26018 + */
26019 +
26020 +#ifndef __AUFS_MODULE_H__
26021 +#define __AUFS_MODULE_H__
26022 +
26023 +#ifdef __KERNEL__
26024 +
26025 +#include <linux/slab.h>
26026 +#include "debug.h"
26027 +#include "dentry.h"
26028 +#include "dir.h"
26029 +#include "file.h"
26030 +#include "inode.h"
26031 +
26032 +struct path;
26033 +struct seq_file;
26034 +
26035 +/* module parameters */
26036 +extern int sysaufs_brs;
26037 +extern bool au_userns;
26038 +
26039 +/* ---------------------------------------------------------------------- */
26040 +
26041 +extern int au_dir_roflags;
26042 +
26043 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
26044 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
26045 +                  int may_shrink);
26046 +
26047 +/*
26048 + * Comparing the size of the object with sizeof(struct rcu_head)
26049 + * case 1: object is always larger
26050 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
26051 + * case 2: object is always smaller
26052 + *     --> au_kfree_small()
26053 + * case 3: object can be any size
26054 + *     --> au_kfree_try_rcu()
26055 + */
26056 +
26057 +static inline void au_kfree_do_rcu(const void *p)
26058 +{
26059 +       struct {
26060 +               struct rcu_head rcu;
26061 +       } *a = (void *)p;
26062 +
26063 +       kfree_rcu(a, rcu);
26064 +}
26065 +
26066 +#define au_kfree_rcu(_p) do {                                          \
26067 +               typeof(_p) p = (_p);                                    \
26068 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
26069 +               if (p)                                                  \
26070 +                       au_kfree_do_rcu(p);                             \
26071 +       } while (0)
26072 +
26073 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
26074 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
26075 +
26076 +static inline void au_kfree_try_rcu(const void *p)
26077 +{
26078 +       if (!p)
26079 +               return;
26080 +       if (au_kfree_sz_test(p))
26081 +               au_kfree_do_rcu(p);
26082 +       else
26083 +               kfree(p);
26084 +}
26085 +
26086 +static inline void au_kfree_small(const void *p)
26087 +{
26088 +       if (!p)
26089 +               return;
26090 +       AuDebugOn(au_kfree_sz_test(p));
26091 +       kfree(p);
26092 +}
26093 +
26094 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26095 +{
26096 +#ifndef CONFIG_SLOB
26097 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
26098 +#else
26099 +       return -1; /* SLOB is untested */
26100 +#endif
26101 +}
26102 +
26103 +int au_seq_path(struct seq_file *seq, struct path *path);
26104 +
26105 +#ifdef CONFIG_PROC_FS
26106 +/* procfs.c */
26107 +int __init au_procfs_init(void);
26108 +void au_procfs_fin(void);
26109 +#else
26110 +AuStubInt0(au_procfs_init, void);
26111 +AuStubVoid(au_procfs_fin, void);
26112 +#endif
26113 +
26114 +/* ---------------------------------------------------------------------- */
26115 +
26116 +/* kmem cache */
26117 +enum {
26118 +       AuCache_DINFO,
26119 +       AuCache_ICNTNR,
26120 +       AuCache_FINFO,
26121 +       AuCache_VDIR,
26122 +       AuCache_DEHSTR,
26123 +       AuCache_HNOTIFY, /* must be last */
26124 +       AuCache_Last
26125 +};
26126 +
26127 +extern struct kmem_cache *au_cache[AuCache_Last];
26128 +
26129 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26130 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
26131 +#define AuCacheCtor(type, ctor)        \
26132 +       kmem_cache_create(#type, sizeof(struct type), \
26133 +                         __alignof__(struct type), AuCacheFlags, ctor)
26134 +
26135 +#define AuCacheFuncAlloc(name, index)                                  \
26136 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
26137 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
26138 +
26139 +#define AuCacheFuncs(name, index)                                      \
26140 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26141 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
26142 +                                                                       \
26143 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26144 +       { void *p = rcu;                                                \
26145 +               p -= offsetof(struct au_##name, rcu);                   \
26146 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
26147 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26148 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26149 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
26150 +                                                                       \
26151 +       static inline void au_cache_free_##name(struct au_##name *p)    \
26152 +       { /* au_cache_free_##name##_norcu(p); */                        \
26153 +               au_cache_free_##name##_rcu(p); }
26154 +
26155 +AuCacheFuncs(dinfo, DINFO);
26156 +AuCacheFuncAlloc(dinfo, DINFO);
26157 +
26158 +AuCacheFuncs(icntnr, ICNTNR);
26159 +static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
26160 +{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
26161 +
26162 +AuCacheFuncs(finfo, FINFO);
26163 +AuCacheFuncAlloc(finfo, FINFO);
26164 +
26165 +AuCacheFuncs(vdir, VDIR);
26166 +AuCacheFuncAlloc(vdir, VDIR);
26167 +
26168 +AuCacheFuncs(vdir_dehstr, DEHSTR);
26169 +AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
26170 +
26171 +#ifdef CONFIG_AUFS_HNOTIFY
26172 +AuCacheFuncs(hnotify, HNOTIFY);
26173 +AuCacheFuncAlloc(hnotify, HNOTIFY);
26174 +#endif
26175 +
26176 +#endif /* __KERNEL__ */
26177 +#endif /* __AUFS_MODULE_H__ */
26178 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
26179 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
26180 +++ linux/fs/aufs/mvdown.c      2022-11-05 23:02:18.969222617 +0100
26181 @@ -0,0 +1,706 @@
26182 +// SPDX-License-Identifier: GPL-2.0
26183 +/*
26184 + * Copyright (C) 2011-2022 Junjiro R. Okajima
26185 + *
26186 + * This program is free software; you can redistribute it and/or modify
26187 + * it under the terms of the GNU General Public License as published by
26188 + * the Free Software Foundation; either version 2 of the License, or
26189 + * (at your option) any later version.
26190 + *
26191 + * This program is distributed in the hope that it will be useful,
26192 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26193 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26194 + * GNU General Public License for more details.
26195 + *
26196 + * You should have received a copy of the GNU General Public License
26197 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26198 + */
26199 +
26200 +/*
26201 + * move-down, opposite of copy-up
26202 + */
26203 +
26204 +#include "aufs.h"
26205 +
26206 +struct au_mvd_args {
26207 +       struct {
26208 +               struct super_block *h_sb;
26209 +               struct dentry *h_parent;
26210 +               struct au_hinode *hdir;
26211 +               struct inode *h_dir, *h_inode;
26212 +               struct au_pin pin;
26213 +       } info[AUFS_MVDOWN_NARRAY];
26214 +
26215 +       struct aufs_mvdown mvdown;
26216 +       struct dentry *dentry, *parent;
26217 +       struct inode *inode, *dir;
26218 +       struct super_block *sb;
26219 +       aufs_bindex_t bopq, bwh, bfound;
26220 +       unsigned char rename_lock;
26221 +};
26222 +
26223 +#define mvd_errno              mvdown.au_errno
26224 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26225 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26226 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26227 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
26228 +
26229 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
26230 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
26231 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
26232 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
26233 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
26234 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
26235 +
26236 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
26237 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
26238 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
26239 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
26240 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
26241 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
26242 +
26243 +#define AU_MVD_PR(flag, ...) do {                      \
26244 +               if (flag)                               \
26245 +                       pr_err(__VA_ARGS__);            \
26246 +       } while (0)
26247 +
26248 +static int find_lower_writable(struct au_mvd_args *a)
26249 +{
26250 +       struct super_block *sb;
26251 +       aufs_bindex_t bindex, bbot;
26252 +       struct au_branch *br;
26253 +
26254 +       sb = a->sb;
26255 +       bindex = a->mvd_bsrc;
26256 +       bbot = au_sbbot(sb);
26257 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
26258 +               for (bindex++; bindex <= bbot; bindex++) {
26259 +                       br = au_sbr(sb, bindex);
26260 +                       if (au_br_fhsm(br->br_perm)
26261 +                           && !sb_rdonly(au_br_sb(br)))
26262 +                               return bindex;
26263 +               }
26264 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
26265 +               for (bindex++; bindex <= bbot; bindex++) {
26266 +                       br = au_sbr(sb, bindex);
26267 +                       if (!au_br_rdonly(br))
26268 +                               return bindex;
26269 +               }
26270 +       else
26271 +               for (bindex++; bindex <= bbot; bindex++) {
26272 +                       br = au_sbr(sb, bindex);
26273 +                       if (!sb_rdonly(au_br_sb(br))) {
26274 +                               if (au_br_rdonly(br))
26275 +                                       a->mvdown.flags
26276 +                                               |= AUFS_MVDOWN_ROLOWER_R;
26277 +                               return bindex;
26278 +                       }
26279 +               }
26280 +
26281 +       return -1;
26282 +}
26283 +
26284 +/* make the parent dir on bdst */
26285 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
26286 +{
26287 +       int err;
26288 +
26289 +       err = 0;
26290 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26291 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26292 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26293 +       a->mvd_h_dst_parent = NULL;
26294 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
26295 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26296 +       if (!a->mvd_h_dst_parent) {
26297 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26298 +               if (unlikely(err)) {
26299 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
26300 +                       goto out;
26301 +               }
26302 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26303 +       }
26304 +
26305 +out:
26306 +       AuTraceErr(err);
26307 +       return err;
26308 +}
26309 +
26310 +/* lock them all */
26311 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
26312 +{
26313 +       int err;
26314 +       struct dentry *h_trap;
26315 +
26316 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26317 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
26318 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26319 +                    au_opt_udba(a->sb),
26320 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26321 +       AuTraceErr(err);
26322 +       if (unlikely(err)) {
26323 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
26324 +               goto out;
26325 +       }
26326 +
26327 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26328 +               a->rename_lock = 0;
26329 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26330 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26331 +                           au_opt_udba(a->sb),
26332 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26333 +               err = au_do_pin(&a->mvd_pin_src);
26334 +               AuTraceErr(err);
26335 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26336 +               if (unlikely(err)) {
26337 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
26338 +                       goto out_dst;
26339 +               }
26340 +               goto out; /* success */
26341 +       }
26342 +
26343 +       a->rename_lock = 1;
26344 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
26345 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26346 +                    au_opt_udba(a->sb),
26347 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26348 +       AuTraceErr(err);
26349 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26350 +       if (unlikely(err)) {
26351 +               AU_MVD_PR(dmsg, "pin_src failed\n");
26352 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26353 +               goto out_dst;
26354 +       }
26355 +       au_pin_hdir_unlock(&a->mvd_pin_src);
26356 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26357 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
26358 +       if (h_trap) {
26359 +               err = (h_trap != a->mvd_h_src_parent);
26360 +               if (err)
26361 +                       err = (h_trap != a->mvd_h_dst_parent);
26362 +       }
26363 +       BUG_ON(err); /* it should never happen */
26364 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26365 +               err = -EBUSY;
26366 +               AuTraceErr(err);
26367 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26368 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26369 +               au_pin_hdir_lock(&a->mvd_pin_src);
26370 +               au_unpin(&a->mvd_pin_src);
26371 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26372 +               goto out_dst;
26373 +       }
26374 +       goto out; /* success */
26375 +
26376 +out_dst:
26377 +       au_unpin(&a->mvd_pin_dst);
26378 +out:
26379 +       AuTraceErr(err);
26380 +       return err;
26381 +}
26382 +
26383 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
26384 +{
26385 +       if (!a->rename_lock)
26386 +               au_unpin(&a->mvd_pin_src);
26387 +       else {
26388 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26389 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26390 +               au_pin_hdir_lock(&a->mvd_pin_src);
26391 +               au_unpin(&a->mvd_pin_src);
26392 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26393 +       }
26394 +       au_unpin(&a->mvd_pin_dst);
26395 +}
26396 +
26397 +/* copy-down the file */
26398 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
26399 +{
26400 +       int err;
26401 +       struct au_cp_generic cpg = {
26402 +               .dentry = a->dentry,
26403 +               .bdst   = a->mvd_bdst,
26404 +               .bsrc   = a->mvd_bsrc,
26405 +               .len    = -1,
26406 +               .pin    = &a->mvd_pin_dst,
26407 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
26408 +       };
26409 +
26410 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
26411 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26412 +               au_fset_cpup(cpg.flags, OVERWRITE);
26413 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26414 +               au_fset_cpup(cpg.flags, RWDST);
26415 +       err = au_sio_cpdown_simple(&cpg);
26416 +       if (unlikely(err))
26417 +               AU_MVD_PR(dmsg, "cpdown failed\n");
26418 +
26419 +       AuTraceErr(err);
26420 +       return err;
26421 +}
26422 +
26423 +/*
26424 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
26425 + * were sleeping
26426 + */
26427 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
26428 +{
26429 +       int err;
26430 +       struct path h_path;
26431 +       struct au_branch *br;
26432 +       struct inode *delegated;
26433 +
26434 +       br = au_sbr(a->sb, a->mvd_bdst);
26435 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26436 +       err = PTR_ERR(h_path.dentry);
26437 +       if (IS_ERR(h_path.dentry)) {
26438 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
26439 +               goto out;
26440 +       }
26441 +
26442 +       err = 0;
26443 +       if (d_is_positive(h_path.dentry)) {
26444 +               h_path.mnt = au_br_mnt(br);
26445 +               delegated = NULL;
26446 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
26447 +                                  &delegated, /*force*/0);
26448 +               if (unlikely(err == -EWOULDBLOCK)) {
26449 +                       pr_warn("cannot retry for NFSv4 delegation"
26450 +                               " for an internal unlink\n");
26451 +                       iput(delegated);
26452 +               }
26453 +               if (unlikely(err))
26454 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
26455 +       }
26456 +       dput(h_path.dentry);
26457 +
26458 +out:
26459 +       AuTraceErr(err);
26460 +       return err;
26461 +}
26462 +
26463 +/*
26464 + * unlink the topmost h_dentry
26465 + */
26466 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
26467 +{
26468 +       int err;
26469 +       struct path h_path;
26470 +       struct inode *delegated;
26471 +
26472 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26473 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26474 +       delegated = NULL;
26475 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26476 +       if (unlikely(err == -EWOULDBLOCK)) {
26477 +               pr_warn("cannot retry for NFSv4 delegation"
26478 +                       " for an internal unlink\n");
26479 +               iput(delegated);
26480 +       }
26481 +       if (unlikely(err))
26482 +               AU_MVD_PR(dmsg, "unlink failed\n");
26483 +
26484 +       AuTraceErr(err);
26485 +       return err;
26486 +}
26487 +
26488 +/* Since mvdown succeeded, we ignore an error of this function */
26489 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26490 +{
26491 +       int err;
26492 +       struct au_branch *br;
26493 +
26494 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26495 +       br = au_sbr(a->sb, a->mvd_bsrc);
26496 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26497 +       if (!err) {
26498 +               br = au_sbr(a->sb, a->mvd_bdst);
26499 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26500 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26501 +       }
26502 +       if (!err)
26503 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26504 +       else
26505 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26506 +}
26507 +
26508 +/*
26509 + * copy-down the file and unlink the bsrc file.
26510 + * - unlink the bdst whout if exist
26511 + * - copy-down the file (with whtmp name and rename)
26512 + * - unlink the bsrc file
26513 + */
26514 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26515 +{
26516 +       int err;
26517 +
26518 +       err = au_do_mkdir(dmsg, a);
26519 +       if (!err)
26520 +               err = au_do_lock(dmsg, a);
26521 +       if (unlikely(err))
26522 +               goto out;
26523 +
26524 +       /*
26525 +        * do not revert the activities we made on bdst since they should be
26526 +        * harmless in aufs.
26527 +        */
26528 +
26529 +       err = au_do_cpdown(dmsg, a);
26530 +       if (!err)
26531 +               err = au_do_unlink_wh(dmsg, a);
26532 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26533 +               err = au_do_unlink(dmsg, a);
26534 +       if (unlikely(err))
26535 +               goto out_unlock;
26536 +
26537 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
26538 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26539 +       if (find_lower_writable(a) < 0)
26540 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26541 +
26542 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26543 +               au_do_stfs(dmsg, a);
26544 +
26545 +       /* maintain internal array */
26546 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26547 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26548 +               au_set_dbtop(a->dentry, a->mvd_bdst);
26549 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26550 +               au_set_ibtop(a->inode, a->mvd_bdst);
26551 +       } else {
26552 +               /* hide the lower */
26553 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26554 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
26555 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26556 +               au_set_ibbot(a->inode, a->mvd_bsrc);
26557 +       }
26558 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
26559 +               au_set_dbbot(a->dentry, a->mvd_bdst);
26560 +       if (au_ibbot(a->inode) < a->mvd_bdst)
26561 +               au_set_ibbot(a->inode, a->mvd_bdst);
26562 +
26563 +out_unlock:
26564 +       au_do_unlock(dmsg, a);
26565 +out:
26566 +       AuTraceErr(err);
26567 +       return err;
26568 +}
26569 +
26570 +/* ---------------------------------------------------------------------- */
26571 +
26572 +/* make sure the file is idle */
26573 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26574 +{
26575 +       int err, plinked;
26576 +
26577 +       err = 0;
26578 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26579 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
26580 +           && au_dcount(a->dentry) == 1
26581 +           && atomic_read(&a->inode->i_count) == 1
26582 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
26583 +           && (!plinked || !au_plink_test(a->inode))
26584 +           && a->inode->i_nlink == 1)
26585 +               goto out;
26586 +
26587 +       err = -EBUSY;
26588 +       AU_MVD_PR(dmsg,
26589 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26590 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26591 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
26592 +                 a->mvd_h_src_inode->i_nlink,
26593 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
26594 +
26595 +out:
26596 +       AuTraceErr(err);
26597 +       return err;
26598 +}
26599 +
26600 +/* make sure the parent dir is fine */
26601 +static int au_mvd_args_parent(const unsigned char dmsg,
26602 +                             struct au_mvd_args *a)
26603 +{
26604 +       int err;
26605 +       aufs_bindex_t bindex;
26606 +
26607 +       err = 0;
26608 +       if (unlikely(au_alive_dir(a->parent))) {
26609 +               err = -ENOENT;
26610 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
26611 +               goto out;
26612 +       }
26613 +
26614 +       a->bopq = au_dbdiropq(a->parent);
26615 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26616 +       AuDbg("b%d\n", bindex);
26617 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26618 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26619 +               err = -EINVAL;
26620 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
26621 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26622 +                         a->bopq, a->mvd_bdst);
26623 +       }
26624 +
26625 +out:
26626 +       AuTraceErr(err);
26627 +       return err;
26628 +}
26629 +
26630 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26631 +                                   struct au_mvd_args *a)
26632 +{
26633 +       int err;
26634 +       struct au_dinfo *dinfo, *tmp;
26635 +
26636 +       /* lookup the next lower positive entry */
26637 +       err = -ENOMEM;
26638 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26639 +       if (unlikely(!tmp))
26640 +               goto out;
26641 +
26642 +       a->bfound = -1;
26643 +       a->bwh = -1;
26644 +       dinfo = au_di(a->dentry);
26645 +       au_di_cp(tmp, dinfo);
26646 +       au_di_swap(tmp, dinfo);
26647 +
26648 +       /* returns the number of positive dentries */
26649 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26650 +                            /* AuLkup_IGNORE_PERM */ 0);
26651 +       if (!err)
26652 +               a->bwh = au_dbwh(a->dentry);
26653 +       else if (err > 0)
26654 +               a->bfound = au_dbtop(a->dentry);
26655 +
26656 +       au_di_swap(tmp, dinfo);
26657 +       au_rw_write_unlock(&tmp->di_rwsem);
26658 +       au_di_free(tmp);
26659 +       if (unlikely(err < 0))
26660 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
26661 +
26662 +       /*
26663 +        * here, we have these cases.
26664 +        * bfound == -1
26665 +        *      no positive dentry under bsrc. there are more sub-cases.
26666 +        *      bwh < 0
26667 +        *              there no whiteout, we can safely move-down.
26668 +        *      bwh <= bsrc
26669 +        *              impossible
26670 +        *      bsrc < bwh && bwh < bdst
26671 +        *              there is a whiteout on RO branch. cannot proceed.
26672 +        *      bwh == bdst
26673 +        *              there is a whiteout on the RW target branch. it should
26674 +        *              be removed.
26675 +        *      bdst < bwh
26676 +        *              there is a whiteout somewhere unrelated branch.
26677 +        * -1 < bfound && bfound <= bsrc
26678 +        *      impossible.
26679 +        * bfound < bdst
26680 +        *      found, but it is on RO branch between bsrc and bdst. cannot
26681 +        *      proceed.
26682 +        * bfound == bdst
26683 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26684 +        *      error.
26685 +        * bdst < bfound
26686 +        *      found, after we create the file on bdst, it will be hidden.
26687 +        */
26688 +
26689 +       AuDebugOn(a->bfound == -1
26690 +                 && a->bwh != -1
26691 +                 && a->bwh <= a->mvd_bsrc);
26692 +       AuDebugOn(-1 < a->bfound
26693 +                 && a->bfound <= a->mvd_bsrc);
26694 +
26695 +       err = -EINVAL;
26696 +       if (a->bfound == -1
26697 +           && a->mvd_bsrc < a->bwh
26698 +           && a->bwh != -1
26699 +           && a->bwh < a->mvd_bdst) {
26700 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26701 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26702 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26703 +               goto out;
26704 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26705 +               a->mvd_errno = EAU_MVDOWN_UPPER;
26706 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26707 +                         a->mvd_bdst, a->bfound);
26708 +               goto out;
26709 +       }
26710 +
26711 +       err = 0; /* success */
26712 +
26713 +out:
26714 +       AuTraceErr(err);
26715 +       return err;
26716 +}
26717 +
26718 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26719 +{
26720 +       int err;
26721 +
26722 +       err = 0;
26723 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26724 +           && a->bfound == a->mvd_bdst)
26725 +               err = -EEXIST;
26726 +       AuTraceErr(err);
26727 +       return err;
26728 +}
26729 +
26730 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26731 +{
26732 +       int err;
26733 +       struct au_branch *br;
26734 +
26735 +       err = -EISDIR;
26736 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
26737 +               goto out;
26738 +
26739 +       err = -EINVAL;
26740 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26741 +               a->mvd_bsrc = au_ibtop(a->inode);
26742 +       else {
26743 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26744 +               if (unlikely(a->mvd_bsrc < 0
26745 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
26746 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
26747 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
26748 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
26749 +                                || au_ibbot(a->inode) < a->mvd_bsrc
26750 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26751 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
26752 +                       AU_MVD_PR(dmsg, "no upper\n");
26753 +                       goto out;
26754 +               }
26755 +       }
26756 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26757 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
26758 +               AU_MVD_PR(dmsg, "on the bottom\n");
26759 +               goto out;
26760 +       }
26761 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26762 +       br = au_sbr(a->sb, a->mvd_bsrc);
26763 +       err = au_br_rdonly(br);
26764 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26765 +               if (unlikely(err))
26766 +                       goto out;
26767 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26768 +                    || IS_APPEND(a->mvd_h_src_inode))) {
26769 +               if (err)
26770 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26771 +               /* go on */
26772 +       } else
26773 +               goto out;
26774 +
26775 +       err = -EINVAL;
26776 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26777 +               a->mvd_bdst = find_lower_writable(a);
26778 +               if (unlikely(a->mvd_bdst < 0)) {
26779 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
26780 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
26781 +                       goto out;
26782 +               }
26783 +       } else {
26784 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26785 +               if (unlikely(a->mvd_bdst < 0
26786 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
26787 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26788 +                       AU_MVD_PR(dmsg, "no lower brid\n");
26789 +                       goto out;
26790 +               }
26791 +       }
26792 +
26793 +       err = au_mvd_args_busy(dmsg, a);
26794 +       if (!err)
26795 +               err = au_mvd_args_parent(dmsg, a);
26796 +       if (!err)
26797 +               err = au_mvd_args_intermediate(dmsg, a);
26798 +       if (!err)
26799 +               err = au_mvd_args_exist(dmsg, a);
26800 +       if (!err)
26801 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26802 +
26803 +out:
26804 +       AuTraceErr(err);
26805 +       return err;
26806 +}
26807 +
26808 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26809 +{
26810 +       int err, e;
26811 +       unsigned char dmsg;
26812 +       struct au_mvd_args *args;
26813 +       struct inode *inode;
26814 +
26815 +       inode = d_inode(dentry);
26816 +       err = -EPERM;
26817 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26818 +               goto out;
26819 +
26820 +       err = -ENOMEM;
26821 +       args = kmalloc(sizeof(*args), GFP_NOFS);
26822 +       if (unlikely(!args))
26823 +               goto out;
26824 +
26825 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26826 +       if (!err)
26827 +               /* VERIFY_WRITE */
26828 +               err = !access_ok(uarg, sizeof(*uarg));
26829 +       if (unlikely(err)) {
26830 +               err = -EFAULT;
26831 +               AuTraceErr(err);
26832 +               goto out_free;
26833 +       }
26834 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
26835 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26836 +       args->mvdown.au_errno = 0;
26837 +       args->dentry = dentry;
26838 +       args->inode = inode;
26839 +       args->sb = dentry->d_sb;
26840 +
26841 +       err = -ENOENT;
26842 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26843 +       args->parent = dget_parent(dentry);
26844 +       args->dir = d_inode(args->parent);
26845 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
26846 +       dput(args->parent);
26847 +       if (unlikely(args->parent != dentry->d_parent)) {
26848 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
26849 +               goto out_dir;
26850 +       }
26851 +
26852 +       inode_lock_nested(inode, I_MUTEX_CHILD);
26853 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26854 +       if (unlikely(err))
26855 +               goto out_inode;
26856 +
26857 +       di_write_lock_parent(args->parent);
26858 +       err = au_mvd_args(dmsg, args);
26859 +       if (unlikely(err))
26860 +               goto out_parent;
26861 +
26862 +       err = au_do_mvdown(dmsg, args);
26863 +       if (unlikely(err))
26864 +               goto out_parent;
26865 +
26866 +       au_cpup_attr_timesizes(args->dir);
26867 +       au_cpup_attr_timesizes(inode);
26868 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26869 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26870 +       /* au_digen_dec(dentry); */
26871 +
26872 +out_parent:
26873 +       di_write_unlock(args->parent);
26874 +       aufs_read_unlock(dentry, AuLock_DW);
26875 +out_inode:
26876 +       inode_unlock(inode);
26877 +out_dir:
26878 +       inode_unlock(args->dir);
26879 +out_free:
26880 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26881 +       if (unlikely(e))
26882 +               err = -EFAULT;
26883 +       au_kfree_rcu(args);
26884 +out:
26885 +       AuTraceErr(err);
26886 +       return err;
26887 +}
26888 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
26889 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
26890 +++ linux/fs/aufs/opts.c        2022-11-05 23:02:18.969222617 +0100
26891 @@ -0,0 +1,1032 @@
26892 +// SPDX-License-Identifier: GPL-2.0
26893 +/*
26894 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26895 + *
26896 + * This program is free software; you can redistribute it and/or modify
26897 + * it under the terms of the GNU General Public License as published by
26898 + * the Free Software Foundation; either version 2 of the License, or
26899 + * (at your option) any later version.
26900 + *
26901 + * This program is distributed in the hope that it will be useful,
26902 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26903 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26904 + * GNU General Public License for more details.
26905 + *
26906 + * You should have received a copy of the GNU General Public License
26907 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26908 + */
26909 +
26910 +/*
26911 + * mount options/flags
26912 + */
26913 +
26914 +#include <linux/types.h> /* a distribution requires */
26915 +#include <linux/parser.h>
26916 +#include "aufs.h"
26917 +
26918 +/* ---------------------------------------------------------------------- */
26919 +
26920 +static const char *au_parser_pattern(int val, match_table_t tbl)
26921 +{
26922 +       struct match_token *p;
26923 +
26924 +       p = tbl;
26925 +       while (p->pattern) {
26926 +               if (p->token == val)
26927 +                       return p->pattern;
26928 +               p++;
26929 +       }
26930 +       BUG();
26931 +       return "??";
26932 +}
26933 +
26934 +static const char *au_optstr(int *val, match_table_t tbl)
26935 +{
26936 +       struct match_token *p;
26937 +       int v;
26938 +
26939 +       v = *val;
26940 +       if (!v)
26941 +               goto out;
26942 +       p = tbl;
26943 +       while (p->pattern) {
26944 +               if (p->token
26945 +                   && (v & p->token) == p->token) {
26946 +                       *val &= ~p->token;
26947 +                       return p->pattern;
26948 +               }
26949 +               p++;
26950 +       }
26951 +
26952 +out:
26953 +       return NULL;
26954 +}
26955 +
26956 +/* ---------------------------------------------------------------------- */
26957 +
26958 +static match_table_t brperm = {
26959 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
26960 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
26961 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
26962 +       {0, NULL}
26963 +};
26964 +
26965 +static match_table_t brattr = {
26966 +       /* general */
26967 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26968 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
26969 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
26970 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
26971 +#ifdef CONFIG_AUFS_FHSM
26972 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
26973 +#endif
26974 +#ifdef CONFIG_AUFS_XATTR
26975 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26976 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26977 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26978 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26979 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26980 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
26981 +#endif
26982 +
26983 +       /* ro/rr branch */
26984 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
26985 +
26986 +       /* rw branch */
26987 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
26988 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
26989 +
26990 +       {0, NULL}
26991 +};
26992 +
26993 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
26994 +{
26995 +       int attr, v;
26996 +       char *p;
26997 +
26998 +       attr = 0;
26999 +       do {
27000 +               p = strchr(str, '+');
27001 +               if (p)
27002 +                       *p = 0;
27003 +               v = match_token(str, table, args);
27004 +               if (v) {
27005 +                       if (v & AuBrAttr_CMOO_Mask)
27006 +                               attr &= ~AuBrAttr_CMOO_Mask;
27007 +                       attr |= v;
27008 +               } else {
27009 +                       if (p)
27010 +                               *p = '+';
27011 +                       pr_warn("ignored branch attribute %s\n", str);
27012 +                       break;
27013 +               }
27014 +               if (p)
27015 +                       str = p + 1;
27016 +       } while (p);
27017 +
27018 +       return attr;
27019 +}
27020 +
27021 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
27022 +{
27023 +       int sz;
27024 +       const char *p;
27025 +       char *q;
27026 +
27027 +       q = str->a;
27028 +       *q = 0;
27029 +       p = au_optstr(&perm, brattr);
27030 +       if (p) {
27031 +               sz = strlen(p);
27032 +               memcpy(q, p, sz + 1);
27033 +               q += sz;
27034 +       } else
27035 +               goto out;
27036 +
27037 +       do {
27038 +               p = au_optstr(&perm, brattr);
27039 +               if (p) {
27040 +                       *q++ = '+';
27041 +                       sz = strlen(p);
27042 +                       memcpy(q, p, sz + 1);
27043 +                       q += sz;
27044 +               }
27045 +       } while (p);
27046 +
27047 +out:
27048 +       return q - str->a;
27049 +}
27050 +
27051 +int au_br_perm_val(char *perm)
27052 +{
27053 +       int val, bad, sz;
27054 +       char *p;
27055 +       substring_t args[MAX_OPT_ARGS];
27056 +       au_br_perm_str_t attr;
27057 +
27058 +       p = strchr(perm, '+');
27059 +       if (p)
27060 +               *p = 0;
27061 +       val = match_token(perm, brperm, args);
27062 +       if (!val) {
27063 +               if (p)
27064 +                       *p = '+';
27065 +               pr_warn("ignored branch permission %s\n", perm);
27066 +               val = AuBrPerm_RO;
27067 +               goto out;
27068 +       }
27069 +       if (!p)
27070 +               goto out;
27071 +
27072 +       val |= br_attr_val(p + 1, brattr, args);
27073 +
27074 +       bad = 0;
27075 +       switch (val & AuBrPerm_Mask) {
27076 +       case AuBrPerm_RO:
27077 +       case AuBrPerm_RR:
27078 +               bad = val & AuBrWAttr_Mask;
27079 +               val &= ~AuBrWAttr_Mask;
27080 +               break;
27081 +       case AuBrPerm_RW:
27082 +               bad = val & AuBrRAttr_Mask;
27083 +               val &= ~AuBrRAttr_Mask;
27084 +               break;
27085 +       }
27086 +
27087 +       /*
27088 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27089 +        * does not treat it as an error, just warning.
27090 +        * this is a tiny guard for the user operation.
27091 +        */
27092 +       if (val & AuBrAttr_UNPIN) {
27093 +               bad |= AuBrAttr_UNPIN;
27094 +               val &= ~AuBrAttr_UNPIN;
27095 +       }
27096 +
27097 +       if (unlikely(bad)) {
27098 +               sz = au_do_optstr_br_attr(&attr, bad);
27099 +               AuDebugOn(!sz);
27100 +               pr_warn("ignored branch attribute %s\n", attr.a);
27101 +       }
27102 +
27103 +out:
27104 +       return val;
27105 +}
27106 +
27107 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
27108 +{
27109 +       au_br_perm_str_t attr;
27110 +       const char *p;
27111 +       char *q;
27112 +       int sz;
27113 +
27114 +       q = str->a;
27115 +       p = au_optstr(&perm, brperm);
27116 +       AuDebugOn(!p || !*p);
27117 +       sz = strlen(p);
27118 +       memcpy(q, p, sz + 1);
27119 +       q += sz;
27120 +
27121 +       sz = au_do_optstr_br_attr(&attr, perm);
27122 +       if (sz) {
27123 +               *q++ = '+';
27124 +               memcpy(q, attr.a, sz + 1);
27125 +       }
27126 +
27127 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
27128 +}
27129 +
27130 +/* ---------------------------------------------------------------------- */
27131 +
27132 +static match_table_t udbalevel = {
27133 +       {AuOpt_UDBA_REVAL, "reval"},
27134 +       {AuOpt_UDBA_NONE, "none"},
27135 +#ifdef CONFIG_AUFS_HNOTIFY
27136 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27137 +#ifdef CONFIG_AUFS_HFSNOTIFY
27138 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
27139 +#endif
27140 +#endif
27141 +       {-1, NULL}
27142 +};
27143 +
27144 +int au_udba_val(char *str)
27145 +{
27146 +       substring_t args[MAX_OPT_ARGS];
27147 +
27148 +       return match_token(str, udbalevel, args);
27149 +}
27150 +
27151 +const char *au_optstr_udba(int udba)
27152 +{
27153 +       return au_parser_pattern(udba, udbalevel);
27154 +}
27155 +
27156 +/* ---------------------------------------------------------------------- */
27157 +
27158 +static match_table_t au_wbr_create_policy = {
27159 +       {AuWbrCreate_TDP, "tdp"},
27160 +       {AuWbrCreate_TDP, "top-down-parent"},
27161 +       {AuWbrCreate_RR, "rr"},
27162 +       {AuWbrCreate_RR, "round-robin"},
27163 +       {AuWbrCreate_MFS, "mfs"},
27164 +       {AuWbrCreate_MFS, "most-free-space"},
27165 +       {AuWbrCreate_MFSV, "mfs:%d"},
27166 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
27167 +
27168 +       /* top-down regardless the parent, and then mfs */
27169 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
27170 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27171 +
27172 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
27173 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27174 +       {AuWbrCreate_PMFS, "pmfs"},
27175 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
27176 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27177 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
27178 +
27179 +       {-1, NULL}
27180 +};
27181 +
27182 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27183 +                           struct au_opt_wbr_create *create)
27184 +{
27185 +       int err;
27186 +       unsigned long long ull;
27187 +
27188 +       err = 0;
27189 +       if (!match_u64(arg, &ull))
27190 +               create->mfsrr_watermark = ull;
27191 +       else {
27192 +               pr_err("bad integer in %s\n", str);
27193 +               err = -EINVAL;
27194 +       }
27195 +
27196 +       return err;
27197 +}
27198 +
27199 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
27200 +                         struct au_opt_wbr_create *create)
27201 +{
27202 +       int n, err;
27203 +
27204 +       err = 0;
27205 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
27206 +               create->mfs_second = n;
27207 +       else {
27208 +               pr_err("bad integer in %s\n", str);
27209 +               err = -EINVAL;
27210 +       }
27211 +
27212 +       return err;
27213 +}
27214 +
27215 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
27216 +{
27217 +       int err, e;
27218 +       substring_t args[MAX_OPT_ARGS];
27219 +
27220 +       err = match_token(str, au_wbr_create_policy, args);
27221 +       create->wbr_create = err;
27222 +       switch (err) {
27223 +       case AuWbrCreate_MFSRRV:
27224 +       case AuWbrCreate_TDMFSV:
27225 +       case AuWbrCreate_PMFSRRV:
27226 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27227 +               if (!e)
27228 +                       e = au_wbr_mfs_sec(&args[1], str, create);
27229 +               if (unlikely(e))
27230 +                       err = e;
27231 +               break;
27232 +       case AuWbrCreate_MFSRR:
27233 +       case AuWbrCreate_TDMFS:
27234 +       case AuWbrCreate_PMFSRR:
27235 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27236 +               if (unlikely(e)) {
27237 +                       err = e;
27238 +                       break;
27239 +               }
27240 +               fallthrough;
27241 +       case AuWbrCreate_MFS:
27242 +       case AuWbrCreate_PMFS:
27243 +               create->mfs_second = AUFS_MFS_DEF_SEC;
27244 +               break;
27245 +       case AuWbrCreate_MFSV:
27246 +       case AuWbrCreate_PMFSV:
27247 +               e = au_wbr_mfs_sec(&args[0], str, create);
27248 +               if (unlikely(e))
27249 +                       err = e;
27250 +               break;
27251 +       }
27252 +
27253 +       return err;
27254 +}
27255 +
27256 +const char *au_optstr_wbr_create(int wbr_create)
27257 +{
27258 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
27259 +}
27260 +
27261 +static match_table_t au_wbr_copyup_policy = {
27262 +       {AuWbrCopyup_TDP, "tdp"},
27263 +       {AuWbrCopyup_TDP, "top-down-parent"},
27264 +       {AuWbrCopyup_BUP, "bup"},
27265 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
27266 +       {AuWbrCopyup_BU, "bu"},
27267 +       {AuWbrCopyup_BU, "bottom-up"},
27268 +       {-1, NULL}
27269 +};
27270 +
27271 +int au_wbr_copyup_val(char *str)
27272 +{
27273 +       substring_t args[MAX_OPT_ARGS];
27274 +
27275 +       return match_token(str, au_wbr_copyup_policy, args);
27276 +}
27277 +
27278 +const char *au_optstr_wbr_copyup(int wbr_copyup)
27279 +{
27280 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
27281 +}
27282 +
27283 +/* ---------------------------------------------------------------------- */
27284 +
27285 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27286 +              aufs_bindex_t bindex)
27287 +{
27288 +       int err;
27289 +       struct au_opt_add *add = &opt->add;
27290 +       char *p;
27291 +
27292 +       add->bindex = bindex;
27293 +       add->perm = AuBrPerm_RO;
27294 +       add->pathname = opt_str;
27295 +       p = strchr(opt_str, '=');
27296 +       if (p) {
27297 +               *p++ = 0;
27298 +               if (*p)
27299 +                       add->perm = au_br_perm_val(p);
27300 +       }
27301 +
27302 +       err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
27303 +       if (!err) {
27304 +               if (!p) {
27305 +                       add->perm = AuBrPerm_RO;
27306 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
27307 +                               add->perm = AuBrPerm_RR;
27308 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
27309 +                               add->perm = AuBrPerm_RW;
27310 +               }
27311 +               opt->type = Opt_add;
27312 +               goto out;
27313 +       }
27314 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
27315 +       err = -EINVAL;
27316 +
27317 +out:
27318 +       return err;
27319 +}
27320 +
27321 +static int au_opt_wbr_create(struct super_block *sb,
27322 +                            struct au_opt_wbr_create *create)
27323 +{
27324 +       int err;
27325 +       struct au_sbinfo *sbinfo;
27326 +
27327 +       SiMustWriteLock(sb);
27328 +
27329 +       err = 1; /* handled */
27330 +       sbinfo = au_sbi(sb);
27331 +       if (sbinfo->si_wbr_create_ops->fin) {
27332 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27333 +               if (!err)
27334 +                       err = 1;
27335 +       }
27336 +
27337 +       sbinfo->si_wbr_create = create->wbr_create;
27338 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27339 +       switch (create->wbr_create) {
27340 +       case AuWbrCreate_MFSRRV:
27341 +       case AuWbrCreate_MFSRR:
27342 +       case AuWbrCreate_TDMFS:
27343 +       case AuWbrCreate_TDMFSV:
27344 +       case AuWbrCreate_PMFSRR:
27345 +       case AuWbrCreate_PMFSRRV:
27346 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27347 +               fallthrough;
27348 +       case AuWbrCreate_MFS:
27349 +       case AuWbrCreate_MFSV:
27350 +       case AuWbrCreate_PMFS:
27351 +       case AuWbrCreate_PMFSV:
27352 +               sbinfo->si_wbr_mfs.mfs_expire
27353 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27354 +               break;
27355 +       }
27356 +
27357 +       if (sbinfo->si_wbr_create_ops->init)
27358 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27359 +
27360 +       return err;
27361 +}
27362 +
27363 +/*
27364 + * returns,
27365 + * plus: processed without an error
27366 + * zero: unprocessed
27367 + */
27368 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27369 +                        struct au_opts *opts)
27370 +{
27371 +       int err;
27372 +       struct au_sbinfo *sbinfo;
27373 +
27374 +       SiMustWriteLock(sb);
27375 +
27376 +       err = 1; /* handled */
27377 +       sbinfo = au_sbi(sb);
27378 +       switch (opt->type) {
27379 +       case Opt_udba:
27380 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27381 +               sbinfo->si_mntflags |= opt->udba;
27382 +               opts->given_udba |= opt->udba;
27383 +               break;
27384 +
27385 +       case Opt_plink:
27386 +               if (opt->tf)
27387 +                       au_opt_set(sbinfo->si_mntflags, PLINK);
27388 +               else {
27389 +                       if (au_opt_test(sbinfo->si_mntflags, PLINK))
27390 +                               au_plink_put(sb, /*verbose*/1);
27391 +                       au_opt_clr(sbinfo->si_mntflags, PLINK);
27392 +               }
27393 +               break;
27394 +       case Opt_list_plink:
27395 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27396 +                       au_plink_list(sb);
27397 +               break;
27398 +
27399 +       case Opt_dio:
27400 +               if (opt->tf) {
27401 +                       au_opt_set(sbinfo->si_mntflags, DIO);
27402 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27403 +               } else {
27404 +                       au_opt_clr(sbinfo->si_mntflags, DIO);
27405 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27406 +               }
27407 +               break;
27408 +
27409 +       case Opt_fhsm_sec:
27410 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27411 +               break;
27412 +
27413 +       case Opt_diropq_a:
27414 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27415 +               break;
27416 +       case Opt_diropq_w:
27417 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27418 +               break;
27419 +
27420 +       case Opt_warn_perm:
27421 +               if (opt->tf)
27422 +                       au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27423 +               else
27424 +                       au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27425 +               break;
27426 +
27427 +       case Opt_verbose:
27428 +               if (opt->tf)
27429 +                       au_opt_set(sbinfo->si_mntflags, VERBOSE);
27430 +               else
27431 +                       au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27432 +               break;
27433 +
27434 +       case Opt_sum:
27435 +               if (opt->tf)
27436 +                       au_opt_set(sbinfo->si_mntflags, SUM);
27437 +               else {
27438 +                       au_opt_clr(sbinfo->si_mntflags, SUM);
27439 +                       au_opt_clr(sbinfo->si_mntflags, SUM_W);
27440 +               }
27441 +               break;
27442 +       case Opt_wsum:
27443 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27444 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27445 +               break;
27446 +
27447 +       case Opt_wbr_create:
27448 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27449 +               break;
27450 +       case Opt_wbr_copyup:
27451 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27452 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27453 +               break;
27454 +
27455 +       case Opt_dirwh:
27456 +               sbinfo->si_dirwh = opt->dirwh;
27457 +               break;
27458 +
27459 +       case Opt_rdcache:
27460 +               sbinfo->si_rdcache
27461 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27462 +               break;
27463 +       case Opt_rdblk:
27464 +               sbinfo->si_rdblk = opt->rdblk;
27465 +               break;
27466 +       case Opt_rdhash:
27467 +               sbinfo->si_rdhash = opt->rdhash;
27468 +               break;
27469 +
27470 +       case Opt_shwh:
27471 +               if (opt->tf)
27472 +                       au_opt_set(sbinfo->si_mntflags, SHWH);
27473 +               else
27474 +                       au_opt_clr(sbinfo->si_mntflags, SHWH);
27475 +               break;
27476 +
27477 +       case Opt_dirperm1:
27478 +               if (opt->tf)
27479 +                       au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27480 +               else
27481 +                       au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27482 +               break;
27483 +
27484 +       case Opt_trunc_xino:
27485 +               if (opt->tf)
27486 +                       au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27487 +               else
27488 +                       au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27489 +               break;
27490 +
27491 +       case Opt_trunc_xino_path:
27492 +       case Opt_itrunc_xino:
27493 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27494 +                                   /*idx_begin*/0);
27495 +               if (!err)
27496 +                       err = 1;
27497 +               break;
27498 +
27499 +       case Opt_trunc_xib:
27500 +               if (opt->tf)
27501 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27502 +               else
27503 +                       au_fclr_opts(opts->flags, TRUNC_XIB);
27504 +               break;
27505 +
27506 +       case Opt_dirren:
27507 +               err = 1;
27508 +               if (opt->tf) {
27509 +                       if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27510 +                               err = au_dr_opt_set(sb);
27511 +                               if (!err)
27512 +                                       err = 1;
27513 +                       }
27514 +                       if (err == 1)
27515 +                               au_opt_set(sbinfo->si_mntflags, DIRREN);
27516 +               } else {
27517 +                       if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27518 +                               err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27519 +                                                                     DR_FLUSHED));
27520 +                               if (!err)
27521 +                                       err = 1;
27522 +                       }
27523 +                       if (err == 1)
27524 +                               au_opt_clr(sbinfo->si_mntflags, DIRREN);
27525 +               }
27526 +               break;
27527 +
27528 +       case Opt_acl:
27529 +               if (opt->tf)
27530 +                       sb->s_flags |= SB_POSIXACL;
27531 +               else
27532 +                       sb->s_flags &= ~SB_POSIXACL;
27533 +               break;
27534 +
27535 +       default:
27536 +               err = 0;
27537 +               break;
27538 +       }
27539 +
27540 +       return err;
27541 +}
27542 +
27543 +/*
27544 + * returns tri-state.
27545 + * plus: processed without an error
27546 + * zero: unprocessed
27547 + * minus: error
27548 + */
27549 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27550 +                    struct au_opts *opts)
27551 +{
27552 +       int err, do_refresh;
27553 +
27554 +       err = 0;
27555 +       switch (opt->type) {
27556 +       case Opt_append:
27557 +               opt->add.bindex = au_sbbot(sb) + 1;
27558 +               if (opt->add.bindex < 0)
27559 +                       opt->add.bindex = 0;
27560 +               goto add;
27561 +               /* Always goto add, not fallthrough */
27562 +       case Opt_prepend:
27563 +               opt->add.bindex = 0;
27564 +               fallthrough;
27565 +       add: /* indented label */
27566 +       case Opt_add:
27567 +               err = au_br_add(sb, &opt->add,
27568 +                               au_ftest_opts(opts->flags, REMOUNT));
27569 +               if (!err) {
27570 +                       err = 1;
27571 +                       au_fset_opts(opts->flags, REFRESH);
27572 +               }
27573 +               break;
27574 +
27575 +       case Opt_del:
27576 +       case Opt_idel:
27577 +               err = au_br_del(sb, &opt->del,
27578 +                               au_ftest_opts(opts->flags, REMOUNT));
27579 +               if (!err) {
27580 +                       err = 1;
27581 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27582 +                       au_fset_opts(opts->flags, REFRESH);
27583 +               }
27584 +               break;
27585 +
27586 +       case Opt_mod:
27587 +       case Opt_imod:
27588 +               err = au_br_mod(sb, &opt->mod,
27589 +                               au_ftest_opts(opts->flags, REMOUNT),
27590 +                               &do_refresh);
27591 +               if (!err) {
27592 +                       err = 1;
27593 +                       if (do_refresh)
27594 +                               au_fset_opts(opts->flags, REFRESH);
27595 +               }
27596 +               break;
27597 +       }
27598 +       return err;
27599 +}
27600 +
27601 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27602 +                      struct au_opt_xino **opt_xino,
27603 +                      struct au_opts *opts)
27604 +{
27605 +       int err;
27606 +
27607 +       err = 0;
27608 +       switch (opt->type) {
27609 +       case Opt_xino:
27610 +               err = au_xino_set(sb, &opt->xino,
27611 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27612 +               if (!err)
27613 +                       *opt_xino = &opt->xino;
27614 +               break;
27615 +       case Opt_noxino:
27616 +               au_xino_clr(sb);
27617 +               *opt_xino = (void *)-1;
27618 +               break;
27619 +       }
27620 +
27621 +       return err;
27622 +}
27623 +
27624 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27625 +                  unsigned int pending)
27626 +{
27627 +       int err, fhsm;
27628 +       aufs_bindex_t bindex, bbot;
27629 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27630 +       struct au_branch *br;
27631 +       struct au_wbr *wbr;
27632 +       struct dentry *root, *dentry;
27633 +       struct inode *dir, *h_dir;
27634 +       struct au_sbinfo *sbinfo;
27635 +       struct au_hinode *hdir;
27636 +
27637 +       SiMustAnyLock(sb);
27638 +
27639 +       sbinfo = au_sbi(sb);
27640 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27641 +
27642 +       if (!(sb_flags & SB_RDONLY)) {
27643 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27644 +                       pr_warn("first branch should be rw\n");
27645 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27646 +                       pr_warn_once("shwh should be used with ro\n");
27647 +       }
27648 +
27649 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27650 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27651 +               pr_warn_once("udba=*notify requires xino\n");
27652 +
27653 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27654 +               pr_warn_once("dirperm1 breaks the protection"
27655 +                            " by the permission bits on the lower branch\n");
27656 +
27657 +       err = 0;
27658 +       fhsm = 0;
27659 +       root = sb->s_root;
27660 +       dir = d_inode(root);
27661 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27662 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27663 +                                     UDBA_NONE);
27664 +       bbot = au_sbbot(sb);
27665 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27666 +               skip = 0;
27667 +               h_dir = au_h_iptr(dir, bindex);
27668 +               br = au_sbr(sb, bindex);
27669 +
27670 +               if ((br->br_perm & AuBrAttr_ICEX)
27671 +                   && !h_dir->i_op->listxattr)
27672 +                       br->br_perm &= ~AuBrAttr_ICEX;
27673 +#if 0 /* untested */
27674 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27675 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27676 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27677 +#endif
27678 +
27679 +               do_free = 0;
27680 +               wbr = br->br_wbr;
27681 +               if (wbr)
27682 +                       wbr_wh_read_lock(wbr);
27683 +
27684 +               if (!au_br_writable(br->br_perm)) {
27685 +                       do_free = !!wbr;
27686 +                       skip = (!wbr
27687 +                               || (!wbr->wbr_whbase
27688 +                                   && !wbr->wbr_plink
27689 +                                   && !wbr->wbr_orph));
27690 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27691 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27692 +                       skip = (!wbr || !wbr->wbr_whbase);
27693 +                       if (skip && wbr) {
27694 +                               if (do_plink)
27695 +                                       skip = !!wbr->wbr_plink;
27696 +                               else
27697 +                                       skip = !wbr->wbr_plink;
27698 +                       }
27699 +               } else {
27700 +                       /* skip = (br->br_whbase && br->br_ohph); */
27701 +                       skip = (wbr && wbr->wbr_whbase);
27702 +                       if (skip) {
27703 +                               if (do_plink)
27704 +                                       skip = !!wbr->wbr_plink;
27705 +                               else
27706 +                                       skip = !wbr->wbr_plink;
27707 +                       }
27708 +               }
27709 +               if (wbr)
27710 +                       wbr_wh_read_unlock(wbr);
27711 +
27712 +               if (can_no_dreval) {
27713 +                       dentry = br->br_path.dentry;
27714 +                       spin_lock(&dentry->d_lock);
27715 +                       if (dentry->d_flags &
27716 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27717 +                               can_no_dreval = 0;
27718 +                       spin_unlock(&dentry->d_lock);
27719 +               }
27720 +
27721 +               if (au_br_fhsm(br->br_perm)) {
27722 +                       fhsm++;
27723 +                       AuDebugOn(!br->br_fhsm);
27724 +               }
27725 +
27726 +               if (skip)
27727 +                       continue;
27728 +
27729 +               hdir = au_hi(dir, bindex);
27730 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27731 +               if (wbr)
27732 +                       wbr_wh_write_lock(wbr);
27733 +               err = au_wh_init(br, sb);
27734 +               if (wbr)
27735 +                       wbr_wh_write_unlock(wbr);
27736 +               au_hn_inode_unlock(hdir);
27737 +
27738 +               if (!err && do_free) {
27739 +                       au_kfree_rcu(wbr);
27740 +                       br->br_wbr = NULL;
27741 +               }
27742 +       }
27743 +
27744 +       if (can_no_dreval)
27745 +               au_fset_si(sbinfo, NO_DREVAL);
27746 +       else
27747 +               au_fclr_si(sbinfo, NO_DREVAL);
27748 +
27749 +       if (fhsm >= 2) {
27750 +               au_fset_si(sbinfo, FHSM);
27751 +               for (bindex = bbot; bindex >= 0; bindex--) {
27752 +                       br = au_sbr(sb, bindex);
27753 +                       if (au_br_fhsm(br->br_perm)) {
27754 +                               au_fhsm_set_bottom(sb, bindex);
27755 +                               break;
27756 +                       }
27757 +               }
27758 +       } else {
27759 +               au_fclr_si(sbinfo, FHSM);
27760 +               au_fhsm_set_bottom(sb, -1);
27761 +       }
27762 +
27763 +       return err;
27764 +}
27765 +
27766 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27767 +{
27768 +       int err;
27769 +       unsigned int tmp;
27770 +       aufs_bindex_t bindex, bbot;
27771 +       struct au_opt *opt;
27772 +       struct au_opt_xino *opt_xino, xino;
27773 +       struct au_sbinfo *sbinfo;
27774 +       struct au_branch *br;
27775 +       struct inode *dir;
27776 +
27777 +       SiMustWriteLock(sb);
27778 +
27779 +       err = 0;
27780 +       opt_xino = NULL;
27781 +       opt = opts->opt;
27782 +       while (err >= 0 && opt->type != Opt_tail)
27783 +               err = au_opt_simple(sb, opt++, opts);
27784 +       if (err > 0)
27785 +               err = 0;
27786 +       else if (unlikely(err < 0))
27787 +               goto out;
27788 +
27789 +       /* disable xino and udba temporary */
27790 +       sbinfo = au_sbi(sb);
27791 +       tmp = sbinfo->si_mntflags;
27792 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27793 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27794 +
27795 +       opt = opts->opt;
27796 +       while (err >= 0 && opt->type != Opt_tail)
27797 +               err = au_opt_br(sb, opt++, opts);
27798 +       if (err > 0)
27799 +               err = 0;
27800 +       else if (unlikely(err < 0))
27801 +               goto out;
27802 +
27803 +       bbot = au_sbbot(sb);
27804 +       if (unlikely(bbot < 0)) {
27805 +               err = -EINVAL;
27806 +               pr_err("no branches\n");
27807 +               goto out;
27808 +       }
27809 +
27810 +       if (au_opt_test(tmp, XINO))
27811 +               au_opt_set(sbinfo->si_mntflags, XINO);
27812 +       opt = opts->opt;
27813 +       while (!err && opt->type != Opt_tail)
27814 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27815 +       if (unlikely(err))
27816 +               goto out;
27817 +
27818 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27819 +       if (unlikely(err))
27820 +               goto out;
27821 +
27822 +       /* restore xino */
27823 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27824 +               xino.file = au_xino_def(sb);
27825 +               err = PTR_ERR(xino.file);
27826 +               if (IS_ERR(xino.file))
27827 +                       goto out;
27828 +
27829 +               err = au_xino_set(sb, &xino, /*remount*/0);
27830 +               fput(xino.file);
27831 +               if (unlikely(err))
27832 +                       goto out;
27833 +       }
27834 +
27835 +       /* restore udba */
27836 +       tmp &= AuOptMask_UDBA;
27837 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27838 +       sbinfo->si_mntflags |= tmp;
27839 +       bbot = au_sbbot(sb);
27840 +       for (bindex = 0; bindex <= bbot; bindex++) {
27841 +               br = au_sbr(sb, bindex);
27842 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27843 +               if (unlikely(err))
27844 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27845 +                               bindex, err);
27846 +               /* go on even if err */
27847 +       }
27848 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27849 +               dir = d_inode(sb->s_root);
27850 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27851 +       }
27852 +
27853 +out:
27854 +       return err;
27855 +}
27856 +
27857 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27858 +{
27859 +       int err, rerr;
27860 +       unsigned char no_dreval;
27861 +       struct inode *dir;
27862 +       struct au_opt_xino *opt_xino;
27863 +       struct au_opt *opt;
27864 +       struct au_sbinfo *sbinfo;
27865 +
27866 +       SiMustWriteLock(sb);
27867 +
27868 +       err = au_dr_opt_flush(sb);
27869 +       if (unlikely(err))
27870 +               goto out;
27871 +       au_fset_opts(opts->flags, DR_FLUSHED);
27872 +
27873 +       dir = d_inode(sb->s_root);
27874 +       sbinfo = au_sbi(sb);
27875 +       opt_xino = NULL;
27876 +       opt = opts->opt;
27877 +       while (err >= 0 && opt->type != Opt_tail) {
27878 +               err = au_opt_simple(sb, opt, opts);
27879 +               if (!err)
27880 +                       err = au_opt_br(sb, opt, opts);
27881 +               if (!err)
27882 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27883 +               opt++;
27884 +       }
27885 +       if (err > 0)
27886 +               err = 0;
27887 +       AuTraceErr(err);
27888 +       /* go on even err */
27889 +
27890 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27891 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27892 +       if (unlikely(rerr && !err))
27893 +               err = rerr;
27894 +
27895 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27896 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27897 +
27898 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27899 +               rerr = au_xib_trunc(sb);
27900 +               if (unlikely(rerr && !err))
27901 +                       err = rerr;
27902 +       }
27903 +
27904 +       /* will be handled by the caller */
27905 +       if (!au_ftest_opts(opts->flags, REFRESH)
27906 +           && (opts->given_udba
27907 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27908 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27909 +                   ))
27910 +               au_fset_opts(opts->flags, REFRESH);
27911 +
27912 +       AuDbg("status 0x%x\n", opts->flags);
27913 +
27914 +out:
27915 +       return err;
27916 +}
27917 +
27918 +/* ---------------------------------------------------------------------- */
27919 +
27920 +unsigned int au_opt_udba(struct super_block *sb)
27921 +{
27922 +       return au_mntflags(sb) & AuOptMask_UDBA;
27923 +}
27924 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27925 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27926 +++ linux/fs/aufs/opts.h        2022-11-05 23:02:18.969222617 +0100
27927 @@ -0,0 +1,263 @@
27928 +/* SPDX-License-Identifier: GPL-2.0 */
27929 +/*
27930 + * Copyright (C) 2005-2022 Junjiro R. Okajima
27931 + *
27932 + * This program is free software; you can redistribute it and/or modify
27933 + * it under the terms of the GNU General Public License as published by
27934 + * the Free Software Foundation; either version 2 of the License, or
27935 + * (at your option) any later version.
27936 + *
27937 + * This program is distributed in the hope that it will be useful,
27938 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27939 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27940 + * GNU General Public License for more details.
27941 + *
27942 + * You should have received a copy of the GNU General Public License
27943 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27944 + */
27945 +
27946 +/*
27947 + * mount options/flags
27948 + */
27949 +
27950 +#ifndef __AUFS_OPTS_H__
27951 +#define __AUFS_OPTS_H__
27952 +
27953 +#ifdef __KERNEL__
27954 +
27955 +#include <linux/fs_parser.h>
27956 +#include <linux/namei.h>
27957 +#include <linux/path.h>
27958 +
27959 +enum {
27960 +       Opt_br,
27961 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27962 +       Opt_idel, Opt_imod,
27963 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27964 +       Opt_xino, Opt_noxino,
27965 +       Opt_trunc_xino, Opt_trunc_xino_v,
27966 +       Opt_trunc_xino_path, Opt_itrunc_xino,
27967 +       Opt_trunc_xib,
27968 +       Opt_shwh,
27969 +       Opt_plink, Opt_list_plink,
27970 +       Opt_udba,
27971 +       Opt_dio,
27972 +       Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27973 +       Opt_warn_perm,
27974 +       Opt_wbr_copyup, Opt_wbr_create,
27975 +       Opt_fhsm_sec,
27976 +       Opt_verbose, Opt_noverbose,
27977 +       Opt_sum, Opt_wsum,
27978 +       Opt_dirperm1,
27979 +       Opt_dirren,
27980 +       Opt_acl,
27981 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
27982 +};
27983 +
27984 +/* ---------------------------------------------------------------------- */
27985 +
27986 +/* mount flags */
27987 +#define AuOpt_XINO             1               /* external inode number bitmap
27988 +                                                  and translation table */
27989 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27990 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27991 +#define AuOpt_UDBA_REVAL       (1 << 3)
27992 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27993 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27994 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27995 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27996 +                                                  bits */
27997 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27998 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27999 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
28000 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
28001 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
28002 +#define AuOpt_DIO              (1 << 14)       /* direct io */
28003 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
28004 +
28005 +#ifndef CONFIG_AUFS_HNOTIFY
28006 +#undef AuOpt_UDBA_HNOTIFY
28007 +#define AuOpt_UDBA_HNOTIFY     0
28008 +#endif
28009 +#ifndef CONFIG_AUFS_DIRREN
28010 +#undef AuOpt_DIRREN
28011 +#define AuOpt_DIRREN           0
28012 +#endif
28013 +#ifndef CONFIG_AUFS_SHWH
28014 +#undef AuOpt_SHWH
28015 +#define AuOpt_SHWH             0
28016 +#endif
28017 +
28018 +#define AuOpt_Def      (AuOpt_XINO \
28019 +                        | AuOpt_UDBA_REVAL \
28020 +                        | AuOpt_PLINK \
28021 +                        /* | AuOpt_DIRPERM1 */ \
28022 +                        | AuOpt_WARN_PERM)
28023 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
28024 +                        | AuOpt_UDBA_REVAL \
28025 +                        | AuOpt_UDBA_HNOTIFY)
28026 +
28027 +#define AuOpt_LkupDirFlags     (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
28028 +
28029 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
28030 +#define au_opt_set(flags, name) do { \
28031 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
28032 +       ((flags) |= AuOpt_##name); \
28033 +} while (0)
28034 +#define au_opt_set_udba(flags, name) do { \
28035 +       (flags) &= ~AuOptMask_UDBA; \
28036 +       ((flags) |= AuOpt_##name); \
28037 +} while (0)
28038 +#define au_opt_clr(flags, name) do { \
28039 +       ((flags) &= ~AuOpt_##name); \
28040 +} while (0)
28041 +
28042 +static inline unsigned int au_opts_plink(unsigned int mntflags)
28043 +{
28044 +#ifdef CONFIG_PROC_FS
28045 +       return mntflags;
28046 +#else
28047 +       return mntflags & ~AuOpt_PLINK;
28048 +#endif
28049 +}
28050 +
28051 +/* ---------------------------------------------------------------------- */
28052 +
28053 +/* policies to select one among multiple writable branches */
28054 +enum {
28055 +       AuWbrCreate_TDP,        /* top down parent */
28056 +       AuWbrCreate_RR,         /* round robin */
28057 +       AuWbrCreate_MFS,        /* most free space */
28058 +       AuWbrCreate_MFSV,       /* mfs with seconds */
28059 +       AuWbrCreate_MFSRR,      /* mfs then rr */
28060 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
28061 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
28062 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
28063 +       AuWbrCreate_PMFS,       /* parent and mfs */
28064 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
28065 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
28066 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
28067 +
28068 +       AuWbrCreate_Def = AuWbrCreate_TDP
28069 +};
28070 +
28071 +enum {
28072 +       AuWbrCopyup_TDP,        /* top down parent */
28073 +       AuWbrCopyup_BUP,        /* bottom up parent */
28074 +       AuWbrCopyup_BU,         /* bottom up */
28075 +
28076 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
28077 +};
28078 +
28079 +/* ---------------------------------------------------------------------- */
28080 +
28081 +struct file;
28082 +
28083 +struct au_opt_add {
28084 +       aufs_bindex_t   bindex;
28085 +       char            *pathname;
28086 +       int             perm;
28087 +       struct path     path;
28088 +};
28089 +
28090 +struct au_opt_del {
28091 +       char            *pathname;
28092 +       struct path     h_path;
28093 +};
28094 +
28095 +struct au_opt_mod {
28096 +       char            *path;
28097 +       int             perm;
28098 +       struct dentry   *h_root;
28099 +};
28100 +
28101 +struct au_opt_xino {
28102 +       char            *path;
28103 +       struct file     *file;
28104 +};
28105 +
28106 +struct au_opt_xino_itrunc {
28107 +       aufs_bindex_t   bindex;
28108 +};
28109 +
28110 +struct au_opt_wbr_create {
28111 +       int                     wbr_create;
28112 +       int                     mfs_second;
28113 +       unsigned long long      mfsrr_watermark;
28114 +};
28115 +
28116 +struct au_opt {
28117 +       int type;
28118 +       union {
28119 +               struct au_opt_xino      xino;
28120 +               struct au_opt_xino_itrunc xino_itrunc;
28121 +               struct au_opt_add       add;
28122 +               struct au_opt_del       del;
28123 +               struct au_opt_mod       mod;
28124 +               int                     dirwh;
28125 +               int                     rdcache;
28126 +               unsigned int            rdblk;
28127 +               unsigned int            rdhash;
28128 +               int                     udba;
28129 +               struct au_opt_wbr_create wbr_create;
28130 +               int                     wbr_copyup;
28131 +               unsigned int            fhsm_second;
28132 +               bool                    tf; /* generic flag, true or false */
28133 +       };
28134 +};
28135 +
28136 +/* opts flags */
28137 +#define AuOpts_REMOUNT         1
28138 +#define AuOpts_REFRESH         (1 << 1)
28139 +#define AuOpts_TRUNC_XIB       (1 << 2)
28140 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
28141 +#define AuOpts_REFRESH_IDOP    (1 << 4)
28142 +#define AuOpts_DR_FLUSHED      (1 << 5)
28143 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
28144 +#define au_fset_opts(flags, name) \
28145 +       do { (flags) |= AuOpts_##name; } while (0)
28146 +#define au_fclr_opts(flags, name) \
28147 +       do { (flags) &= ~AuOpts_##name; } while (0)
28148 +
28149 +#ifndef CONFIG_AUFS_DIRREN
28150 +#undef AuOpts_DR_FLUSHED
28151 +#define AuOpts_DR_FLUSHED      0
28152 +#endif
28153 +
28154 +struct au_opts {
28155 +       struct au_opt   *opt;
28156 +       int             max_opt;
28157 +
28158 +       unsigned int    given_udba;
28159 +       unsigned int    flags;
28160 +       unsigned long   sb_flags;
28161 +};
28162 +
28163 +/* ---------------------------------------------------------------------- */
28164 +
28165 +/* opts.c */
28166 +int au_br_perm_val(char *perm);
28167 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
28168 +int au_udba_val(char *str);
28169 +const char *au_optstr_udba(int udba);
28170 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
28171 +const char *au_optstr_wbr_create(int wbr_create);
28172 +int au_wbr_copyup_val(char *str);
28173 +const char *au_optstr_wbr_copyup(int wbr_copyup);
28174 +
28175 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28176 +              aufs_bindex_t bindex);
28177 +struct super_block;
28178 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28179 +                  unsigned int pending);
28180 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28181 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28182 +
28183 +unsigned int au_opt_udba(struct super_block *sb);
28184 +
28185 +/* fsctx.c */
28186 +int aufs_fsctx_init(struct fs_context *fc);
28187 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28188 +
28189 +#endif /* __KERNEL__ */
28190 +#endif /* __AUFS_OPTS_H__ */
28191 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
28192 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
28193 +++ linux/fs/aufs/plink.c       2022-11-05 23:02:18.969222617 +0100
28194 @@ -0,0 +1,516 @@
28195 +// SPDX-License-Identifier: GPL-2.0
28196 +/*
28197 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28198 + *
28199 + * This program is free software; you can redistribute it and/or modify
28200 + * it under the terms of the GNU General Public License as published by
28201 + * the Free Software Foundation; either version 2 of the License, or
28202 + * (at your option) any later version.
28203 + *
28204 + * This program is distributed in the hope that it will be useful,
28205 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28206 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28207 + * GNU General Public License for more details.
28208 + *
28209 + * You should have received a copy of the GNU General Public License
28210 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28211 + */
28212 +
28213 +/*
28214 + * pseudo-link
28215 + */
28216 +
28217 +#include "aufs.h"
28218 +
28219 +/*
28220 + * the pseudo-link maintenance mode.
28221 + * during a user process maintains the pseudo-links,
28222 + * prohibit adding a new plink and branch manipulation.
28223 + *
28224 + * Flags
28225 + * NOPLM:
28226 + *     For entry functions which will handle plink, and i_mutex is already held
28227 + *     in VFS.
28228 + *     They cannot wait and should return an error at once.
28229 + *     Callers has to check the error.
28230 + * NOPLMW:
28231 + *     For entry functions which will handle plink, but i_mutex is not held
28232 + *     in VFS.
28233 + *     They can wait the plink maintenance mode to finish.
28234 + *
28235 + * They behave like F_SETLK and F_SETLKW.
28236 + * If the caller never handle plink, then both flags are unnecessary.
28237 + */
28238 +
28239 +int au_plink_maint(struct super_block *sb, int flags)
28240 +{
28241 +       int err;
28242 +       pid_t pid, ppid;
28243 +       struct task_struct *parent, *prev;
28244 +       struct au_sbinfo *sbi;
28245 +
28246 +       SiMustAnyLock(sb);
28247 +
28248 +       err = 0;
28249 +       if (!au_opt_test(au_mntflags(sb), PLINK))
28250 +               goto out;
28251 +
28252 +       sbi = au_sbi(sb);
28253 +       pid = sbi->si_plink_maint_pid;
28254 +       if (!pid || pid == current->pid)
28255 +               goto out;
28256 +
28257 +       /* todo: it highly depends upon /sbin/mount.aufs */
28258 +       prev = NULL;
28259 +       parent = current;
28260 +       ppid = 0;
28261 +       rcu_read_lock();
28262 +       while (1) {
28263 +               parent = rcu_dereference(parent->real_parent);
28264 +               if (parent == prev)
28265 +                       break;
28266 +               ppid = task_pid_vnr(parent);
28267 +               if (pid == ppid) {
28268 +                       rcu_read_unlock();
28269 +                       goto out;
28270 +               }
28271 +               prev = parent;
28272 +       }
28273 +       rcu_read_unlock();
28274 +
28275 +       if (au_ftest_lock(flags, NOPLMW)) {
28276 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
28277 +               /* AuDebugOn(!lockdep_depth(current)); */
28278 +               while (sbi->si_plink_maint_pid) {
28279 +                       si_read_unlock(sb);
28280 +                       /* gave up wake_up_bit() */
28281 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28282 +
28283 +                       if (au_ftest_lock(flags, FLUSH))
28284 +                               au_nwt_flush(&sbi->si_nowait);
28285 +                       si_noflush_read_lock(sb);
28286 +               }
28287 +       } else if (au_ftest_lock(flags, NOPLM)) {
28288 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
28289 +               err = -EAGAIN;
28290 +       }
28291 +
28292 +out:
28293 +       return err;
28294 +}
28295 +
28296 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28297 +{
28298 +       spin_lock(&sbinfo->si_plink_maint_lock);
28299 +       sbinfo->si_plink_maint_pid = 0;
28300 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28301 +       wake_up_all(&sbinfo->si_plink_wq);
28302 +}
28303 +
28304 +int au_plink_maint_enter(struct super_block *sb)
28305 +{
28306 +       int err;
28307 +       struct au_sbinfo *sbinfo;
28308 +
28309 +       err = 0;
28310 +       sbinfo = au_sbi(sb);
28311 +       /* make sure i am the only one in this fs */
28312 +       si_write_lock(sb, AuLock_FLUSH);
28313 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28314 +               spin_lock(&sbinfo->si_plink_maint_lock);
28315 +               if (!sbinfo->si_plink_maint_pid)
28316 +                       sbinfo->si_plink_maint_pid = current->pid;
28317 +               else
28318 +                       err = -EBUSY;
28319 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28320 +       }
28321 +       si_write_unlock(sb);
28322 +
28323 +       return err;
28324 +}
28325 +
28326 +/* ---------------------------------------------------------------------- */
28327 +
28328 +#ifdef CONFIG_AUFS_DEBUG
28329 +void au_plink_list(struct super_block *sb)
28330 +{
28331 +       int i;
28332 +       struct au_sbinfo *sbinfo;
28333 +       struct hlist_bl_head *hbl;
28334 +       struct hlist_bl_node *pos;
28335 +       struct au_icntnr *icntnr;
28336 +
28337 +       SiMustAnyLock(sb);
28338 +
28339 +       sbinfo = au_sbi(sb);
28340 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28341 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28342 +
28343 +       for (i = 0; i < AuPlink_NHASH; i++) {
28344 +               hbl = sbinfo->si_plink + i;
28345 +               hlist_bl_lock(hbl);
28346 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28347 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28348 +               hlist_bl_unlock(hbl);
28349 +       }
28350 +}
28351 +#endif
28352 +
28353 +/* is the inode pseudo-linked? */
28354 +int au_plink_test(struct inode *inode)
28355 +{
28356 +       int found, i;
28357 +       struct au_sbinfo *sbinfo;
28358 +       struct hlist_bl_head *hbl;
28359 +       struct hlist_bl_node *pos;
28360 +       struct au_icntnr *icntnr;
28361 +
28362 +       sbinfo = au_sbi(inode->i_sb);
28363 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28364 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28365 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28366 +
28367 +       found = 0;
28368 +       i = au_plink_hash(inode->i_ino);
28369 +       hbl =  sbinfo->si_plink + i;
28370 +       hlist_bl_lock(hbl);
28371 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28372 +               if (&icntnr->vfs_inode == inode) {
28373 +                       found = 1;
28374 +                       break;
28375 +               }
28376 +       hlist_bl_unlock(hbl);
28377 +       return found;
28378 +}
28379 +
28380 +/* ---------------------------------------------------------------------- */
28381 +
28382 +/*
28383 + * generate a name for plink.
28384 + * the file will be stored under AUFS_WH_PLINKDIR.
28385 + */
28386 +/* 20 is max digits length of ulong 64 */
28387 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28388 +
28389 +static int plink_name(char *name, int len, struct inode *inode,
28390 +                     aufs_bindex_t bindex)
28391 +{
28392 +       int rlen;
28393 +       struct inode *h_inode;
28394 +
28395 +       h_inode = au_h_iptr(inode, bindex);
28396 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28397 +       return rlen;
28398 +}
28399 +
28400 +struct au_do_plink_lkup_args {
28401 +       struct dentry **errp;
28402 +       struct qstr *tgtname;
28403 +       struct path *h_ppath;
28404 +};
28405 +
28406 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28407 +                                      struct path *h_ppath)
28408 +{
28409 +       struct dentry *h_dentry;
28410 +       struct inode *h_inode;
28411 +
28412 +       h_inode = d_inode(h_ppath->dentry);
28413 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28414 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
28415 +       inode_unlock_shared(h_inode);
28416 +
28417 +       return h_dentry;
28418 +}
28419 +
28420 +static void au_call_do_plink_lkup(void *args)
28421 +{
28422 +       struct au_do_plink_lkup_args *a = args;
28423 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
28424 +}
28425 +
28426 +/* lookup the plink-ed @inode under the branch at @bindex */
28427 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28428 +{
28429 +       struct dentry *h_dentry;
28430 +       struct au_branch *br;
28431 +       struct path h_ppath;
28432 +       int wkq_err;
28433 +       char a[PLINK_NAME_LEN];
28434 +       struct qstr tgtname = QSTR_INIT(a, 0);
28435 +
28436 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28437 +
28438 +       br = au_sbr(inode->i_sb, bindex);
28439 +       h_ppath.dentry = br->br_wbr->wbr_plink;
28440 +       h_ppath.mnt = au_br_mnt(br);
28441 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28442 +
28443 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28444 +               struct au_do_plink_lkup_args args = {
28445 +                       .errp           = &h_dentry,
28446 +                       .tgtname        = &tgtname,
28447 +                       .h_ppath        = &h_ppath
28448 +               };
28449 +
28450 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28451 +               if (unlikely(wkq_err))
28452 +                       h_dentry = ERR_PTR(wkq_err);
28453 +       } else
28454 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28455 +
28456 +       return h_dentry;
28457 +}
28458 +
28459 +/* create a pseudo-link */
28460 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28461 +                     struct dentry *h_dentry)
28462 +{
28463 +       int err;
28464 +       struct path h_path;
28465 +       struct inode *h_dir, *delegated;
28466 +
28467 +       h_dir = d_inode(h_ppath->dentry);
28468 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28469 +       h_path.mnt = h_ppath->mnt;
28470 +again:
28471 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28472 +       err = PTR_ERR(h_path.dentry);
28473 +       if (IS_ERR(h_path.dentry))
28474 +               goto out;
28475 +
28476 +       err = 0;
28477 +       /* wh.plink dir is not monitored */
28478 +       /* todo: is it really safe? */
28479 +       if (d_is_positive(h_path.dentry)
28480 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28481 +               delegated = NULL;
28482 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28483 +               if (unlikely(err == -EWOULDBLOCK)) {
28484 +                       pr_warn("cannot retry for NFSv4 delegation"
28485 +                               " for an internal unlink\n");
28486 +                       iput(delegated);
28487 +               }
28488 +               dput(h_path.dentry);
28489 +               h_path.dentry = NULL;
28490 +               if (!err)
28491 +                       goto again;
28492 +       }
28493 +       if (!err && d_is_negative(h_path.dentry)) {
28494 +               delegated = NULL;
28495 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28496 +               if (unlikely(err == -EWOULDBLOCK)) {
28497 +                       pr_warn("cannot retry for NFSv4 delegation"
28498 +                               " for an internal link\n");
28499 +                       iput(delegated);
28500 +               }
28501 +       }
28502 +       dput(h_path.dentry);
28503 +
28504 +out:
28505 +       inode_unlock(h_dir);
28506 +       return err;
28507 +}
28508 +
28509 +struct do_whplink_args {
28510 +       int *errp;
28511 +       struct qstr *tgt;
28512 +       struct path *h_ppath;
28513 +       struct dentry *h_dentry;
28514 +};
28515 +
28516 +static void call_do_whplink(void *args)
28517 +{
28518 +       struct do_whplink_args *a = args;
28519 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28520 +}
28521 +
28522 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28523 +                  aufs_bindex_t bindex)
28524 +{
28525 +       int err, wkq_err;
28526 +       struct au_branch *br;
28527 +       struct au_wbr *wbr;
28528 +       struct path h_ppath;
28529 +       char a[PLINK_NAME_LEN];
28530 +       struct qstr tgtname = QSTR_INIT(a, 0);
28531 +
28532 +       br = au_sbr(inode->i_sb, bindex);
28533 +       wbr = br->br_wbr;
28534 +       h_ppath.dentry = wbr->wbr_plink;
28535 +       h_ppath.mnt = au_br_mnt(br);
28536 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28537 +
28538 +       /* always superio. */
28539 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28540 +               struct do_whplink_args args = {
28541 +                       .errp           = &err,
28542 +                       .tgt            = &tgtname,
28543 +                       .h_ppath        = &h_ppath,
28544 +                       .h_dentry       = h_dentry
28545 +               };
28546 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28547 +               if (unlikely(wkq_err))
28548 +                       err = wkq_err;
28549 +       } else
28550 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28551 +
28552 +       return err;
28553 +}
28554 +
28555 +/*
28556 + * create a new pseudo-link for @h_dentry on @bindex.
28557 + * the linked inode is held in aufs @inode.
28558 + */
28559 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28560 +                    struct dentry *h_dentry)
28561 +{
28562 +       struct super_block *sb;
28563 +       struct au_sbinfo *sbinfo;
28564 +       struct hlist_bl_head *hbl;
28565 +       struct hlist_bl_node *pos;
28566 +       struct au_icntnr *icntnr;
28567 +       int found, err, cnt, i;
28568 +
28569 +       sb = inode->i_sb;
28570 +       sbinfo = au_sbi(sb);
28571 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28572 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28573 +
28574 +       found = au_plink_test(inode);
28575 +       if (found)
28576 +               return;
28577 +
28578 +       i = au_plink_hash(inode->i_ino);
28579 +       hbl = sbinfo->si_plink + i;
28580 +       au_igrab(inode);
28581 +
28582 +       hlist_bl_lock(hbl);
28583 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28584 +               if (&icntnr->vfs_inode == inode) {
28585 +                       found = 1;
28586 +                       break;
28587 +               }
28588 +       }
28589 +       if (!found) {
28590 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28591 +               hlist_bl_add_head(&icntnr->plink, hbl);
28592 +       }
28593 +       hlist_bl_unlock(hbl);
28594 +       if (!found) {
28595 +               cnt = au_hbl_count(hbl);
28596 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28597 +               if (cnt > AUFS_PLINK_WARN)
28598 +                       AuWarn1(msg ", %d\n", cnt);
28599 +#undef msg
28600 +               err = whplink(h_dentry, inode, bindex);
28601 +               if (unlikely(err)) {
28602 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28603 +                       au_hbl_del(&icntnr->plink, hbl);
28604 +                       iput(&icntnr->vfs_inode);
28605 +               }
28606 +       } else
28607 +               iput(&icntnr->vfs_inode);
28608 +}
28609 +
28610 +/* free all plinks */
28611 +void au_plink_put(struct super_block *sb, int verbose)
28612 +{
28613 +       int i, warned;
28614 +       struct au_sbinfo *sbinfo;
28615 +       struct hlist_bl_head *hbl;
28616 +       struct hlist_bl_node *pos, *tmp;
28617 +       struct au_icntnr *icntnr;
28618 +
28619 +       SiMustWriteLock(sb);
28620 +
28621 +       sbinfo = au_sbi(sb);
28622 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28623 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28624 +
28625 +       /* no spin_lock since sbinfo is write-locked */
28626 +       warned = 0;
28627 +       for (i = 0; i < AuPlink_NHASH; i++) {
28628 +               hbl = sbinfo->si_plink + i;
28629 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28630 +                       pr_warn("pseudo-link is not flushed");
28631 +                       warned = 1;
28632 +               }
28633 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28634 +                       iput(&icntnr->vfs_inode);
28635 +               INIT_HLIST_BL_HEAD(hbl);
28636 +       }
28637 +}
28638 +
28639 +void au_plink_clean(struct super_block *sb, int verbose)
28640 +{
28641 +       struct dentry *root;
28642 +
28643 +       root = sb->s_root;
28644 +       aufs_write_lock(root);
28645 +       if (au_opt_test(au_mntflags(sb), PLINK))
28646 +               au_plink_put(sb, verbose);
28647 +       aufs_write_unlock(root);
28648 +}
28649 +
28650 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28651 +{
28652 +       int do_put;
28653 +       aufs_bindex_t btop, bbot, bindex;
28654 +
28655 +       do_put = 0;
28656 +       btop = au_ibtop(inode);
28657 +       bbot = au_ibbot(inode);
28658 +       if (btop >= 0) {
28659 +               for (bindex = btop; bindex <= bbot; bindex++) {
28660 +                       if (!au_h_iptr(inode, bindex)
28661 +                           || au_ii_br_id(inode, bindex) != br_id)
28662 +                               continue;
28663 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28664 +                       do_put = 1;
28665 +                       break;
28666 +               }
28667 +               if (do_put)
28668 +                       for (bindex = btop; bindex <= bbot; bindex++)
28669 +                               if (au_h_iptr(inode, bindex)) {
28670 +                                       do_put = 0;
28671 +                                       break;
28672 +                               }
28673 +       } else
28674 +               do_put = 1;
28675 +
28676 +       return do_put;
28677 +}
28678 +
28679 +/* free the plinks on a branch specified by @br_id */
28680 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28681 +{
28682 +       struct au_sbinfo *sbinfo;
28683 +       struct hlist_bl_head *hbl;
28684 +       struct hlist_bl_node *pos, *tmp;
28685 +       struct au_icntnr *icntnr;
28686 +       struct inode *inode;
28687 +       int i, do_put;
28688 +
28689 +       SiMustWriteLock(sb);
28690 +
28691 +       sbinfo = au_sbi(sb);
28692 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28693 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28694 +
28695 +       /* no bit_lock since sbinfo is write-locked */
28696 +       for (i = 0; i < AuPlink_NHASH; i++) {
28697 +               hbl = sbinfo->si_plink + i;
28698 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28699 +                       inode = au_igrab(&icntnr->vfs_inode);
28700 +                       ii_write_lock_child(inode);
28701 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28702 +                       if (do_put) {
28703 +                               hlist_bl_del(&icntnr->plink);
28704 +                               iput(inode);
28705 +                       }
28706 +                       ii_write_unlock(inode);
28707 +                       iput(inode);
28708 +               }
28709 +       }
28710 +}
28711 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28712 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28713 +++ linux/fs/aufs/poll.c        2022-11-05 23:02:18.969222617 +0100
28714 @@ -0,0 +1,51 @@
28715 +// SPDX-License-Identifier: GPL-2.0
28716 +/*
28717 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28718 + *
28719 + * This program is free software; you can redistribute it and/or modify
28720 + * it under the terms of the GNU General Public License as published by
28721 + * the Free Software Foundation; either version 2 of the License, or
28722 + * (at your option) any later version.
28723 + *
28724 + * This program is distributed in the hope that it will be useful,
28725 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28726 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28727 + * GNU General Public License for more details.
28728 + *
28729 + * You should have received a copy of the GNU General Public License
28730 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28731 + */
28732 +
28733 +/*
28734 + * poll operation
28735 + * There is only one filesystem which implements ->poll operation, currently.
28736 + */
28737 +
28738 +#include "aufs.h"
28739 +
28740 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28741 +{
28742 +       __poll_t mask;
28743 +       struct file *h_file;
28744 +       struct super_block *sb;
28745 +
28746 +       /* We should pretend an error happened. */
28747 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28748 +       sb = file->f_path.dentry->d_sb;
28749 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28750 +
28751 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28752 +       if (IS_ERR(h_file)) {
28753 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28754 +               goto out;
28755 +       }
28756 +
28757 +       mask = vfs_poll(h_file, pt);
28758 +       fput(h_file); /* instead of au_read_post() */
28759 +
28760 +out:
28761 +       si_read_unlock(sb);
28762 +       if (mask & EPOLLERR)
28763 +               AuDbg("mask 0x%x\n", mask);
28764 +       return mask;
28765 +}
28766 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28767 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28768 +++ linux/fs/aufs/posix_acl.c   2023-04-24 08:13:40.969576115 +0200
28769 @@ -0,0 +1,108 @@
28770 +// SPDX-License-Identifier: GPL-2.0
28771 +/*
28772 + * Copyright (C) 2014-2022 Junjiro R. Okajima
28773 + *
28774 + * This program is free software; you can redistribute it and/or modify
28775 + * it under the terms of the GNU General Public License as published by
28776 + * the Free Software Foundation; either version 2 of the License, or
28777 + * (at your option) any later version.
28778 + *
28779 + * This program is distributed in the hope that it will be useful,
28780 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28781 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28782 + * GNU General Public License for more details.
28783 + *
28784 + * You should have received a copy of the GNU General Public License
28785 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28786 + */
28787 +
28788 +/*
28789 + * posix acl operations
28790 + */
28791 +
28792 +#include <linux/fs.h>
28793 +#include "aufs.h"
28794 +
28795 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu)
28796 +{
28797 +       struct posix_acl *acl;
28798 +       int err;
28799 +       aufs_bindex_t bindex;
28800 +       struct inode *h_inode;
28801 +       struct super_block *sb;
28802 +
28803 +       acl = ERR_PTR(-ECHILD);
28804 +       if (rcu)
28805 +               goto out;
28806 +
28807 +       acl = NULL;
28808 +       sb = inode->i_sb;
28809 +       si_read_lock(sb, AuLock_FLUSH);
28810 +       ii_read_lock_child(inode);
28811 +       if (!(sb->s_flags & SB_POSIXACL))
28812 +               goto unlock;
28813 +
28814 +       bindex = au_ibtop(inode);
28815 +       h_inode = au_h_iptr(inode, bindex);
28816 +       if (unlikely(!h_inode
28817 +                    || ((h_inode->i_mode & S_IFMT)
28818 +                        != (inode->i_mode & S_IFMT)))) {
28819 +               err = au_busy_or_stale();
28820 +               acl = ERR_PTR(err);
28821 +               goto unlock;
28822 +       }
28823 +
28824 +       /* always topmost only */
28825 +       acl = get_inode_acl(h_inode, type);
28826 +       if (IS_ERR(acl))
28827 +               forget_cached_acl(inode, type);
28828 +       else
28829 +               set_cached_acl(inode, type, acl);
28830 +
28831 +unlock:
28832 +       ii_read_unlock(inode);
28833 +       si_read_unlock(sb);
28834 +
28835 +out:
28836 +       AuTraceErrPtr(acl);
28837 +       return acl;
28838 +}
28839 +
28840 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
28841 +                              struct dentry *dentry, int type)
28842 +{
28843 +       struct posix_acl *acl;
28844 +       struct inode *inode;
28845 +
28846 +       inode = d_inode(dentry);
28847 +       acl = aufs_get_inode_acl(inode, type, /*rcu*/false);
28848 +
28849 +       return acl;
28850 +}
28851 +
28852 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
28853 +                struct posix_acl *acl, int type)
28854 +{
28855 +       int err;
28856 +       ssize_t ssz;
28857 +       struct inode *inode;
28858 +       struct au_sxattr arg = {
28859 +               .type = AU_ACL_SET,
28860 +               .u.acl_set = {
28861 +                       .acl    = acl,
28862 +                       .type   = type
28863 +               },
28864 +       };
28865 +
28866 +       inode = d_inode(dentry);
28867 +       IMustLock(inode);
28868 +
28869 +       ssz = au_sxattr(dentry, inode, &arg);
28870 +       /* forget even it if succeeds since the branch might set differently */
28871 +       forget_cached_acl(inode, type);
28872 +       err = ssz;
28873 +       if (ssz >= 0)
28874 +               err = 0;
28875 +
28876 +       return err;
28877 +}
28878 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28879 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28880 +++ linux/fs/aufs/procfs.c      2022-11-05 23:02:18.969222617 +0100
28881 @@ -0,0 +1,170 @@
28882 +// SPDX-License-Identifier: GPL-2.0
28883 +/*
28884 + * Copyright (C) 2010-2022 Junjiro R. Okajima
28885 + *
28886 + * This program is free software; you can redistribute it and/or modify
28887 + * it under the terms of the GNU General Public License as published by
28888 + * the Free Software Foundation; either version 2 of the License, or
28889 + * (at your option) any later version.
28890 + *
28891 + * This program is distributed in the hope that it will be useful,
28892 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28893 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28894 + * GNU General Public License for more details.
28895 + *
28896 + * You should have received a copy of the GNU General Public License
28897 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28898 + */
28899 +
28900 +/*
28901 + * procfs interfaces
28902 + */
28903 +
28904 +#include <linux/proc_fs.h>
28905 +#include "aufs.h"
28906 +
28907 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28908 +{
28909 +       struct au_sbinfo *sbinfo;
28910 +
28911 +       sbinfo = file->private_data;
28912 +       if (sbinfo) {
28913 +               au_plink_maint_leave(sbinfo);
28914 +               kobject_put(&sbinfo->si_kobj);
28915 +       }
28916 +
28917 +       return 0;
28918 +}
28919 +
28920 +static void au_procfs_plm_write_clean(struct file *file)
28921 +{
28922 +       struct au_sbinfo *sbinfo;
28923 +
28924 +       sbinfo = file->private_data;
28925 +       if (sbinfo)
28926 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28927 +}
28928 +
28929 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28930 +{
28931 +       int err;
28932 +       struct super_block *sb;
28933 +       struct au_sbinfo *sbinfo;
28934 +       struct hlist_bl_node *pos;
28935 +
28936 +       err = -EBUSY;
28937 +       if (unlikely(file->private_data))
28938 +               goto out;
28939 +
28940 +       sb = NULL;
28941 +       /* don't use au_sbilist_lock() here */
28942 +       hlist_bl_lock(&au_sbilist);
28943 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28944 +               if (id == sysaufs_si_id(sbinfo)) {
28945 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28946 +                               sb = sbinfo->si_sb;
28947 +                       break;
28948 +               }
28949 +       hlist_bl_unlock(&au_sbilist);
28950 +
28951 +       err = -EINVAL;
28952 +       if (unlikely(!sb))
28953 +               goto out;
28954 +
28955 +       err = au_plink_maint_enter(sb);
28956 +       if (!err)
28957 +               /* keep kobject_get() */
28958 +               file->private_data = sbinfo;
28959 +       else
28960 +               kobject_put(&sbinfo->si_kobj);
28961 +out:
28962 +       return err;
28963 +}
28964 +
28965 +/*
28966 + * Accept a valid "si=xxxx" only.
28967 + * Once it is accepted successfully, accept "clean" too.
28968 + */
28969 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28970 +                                  size_t count, loff_t *ppos)
28971 +{
28972 +       ssize_t err;
28973 +       unsigned long id;
28974 +       /* last newline is allowed */
28975 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28976 +
28977 +       err = -EACCES;
28978 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28979 +               goto out;
28980 +
28981 +       err = -EINVAL;
28982 +       if (unlikely(count > sizeof(buf)))
28983 +               goto out;
28984 +
28985 +       err = copy_from_user(buf, ubuf, count);
28986 +       if (unlikely(err)) {
28987 +               err = -EFAULT;
28988 +               goto out;
28989 +       }
28990 +       buf[count] = 0;
28991 +
28992 +       err = -EINVAL;
28993 +       if (!strcmp("clean", buf)) {
28994 +               au_procfs_plm_write_clean(file);
28995 +               goto out_success;
28996 +       } else if (unlikely(strncmp("si=", buf, 3)))
28997 +               goto out;
28998 +
28999 +       err = kstrtoul(buf + 3, 16, &id);
29000 +       if (unlikely(err))
29001 +               goto out;
29002 +
29003 +       err = au_procfs_plm_write_si(file, id);
29004 +       if (unlikely(err))
29005 +               goto out;
29006 +
29007 +out_success:
29008 +       err = count; /* success */
29009 +out:
29010 +       return err;
29011 +}
29012 +
29013 +static const struct proc_ops au_procfs_plm_op = {
29014 +       .proc_write     = au_procfs_plm_write,
29015 +       .proc_release   = au_procfs_plm_release
29016 +};
29017 +
29018 +/* ---------------------------------------------------------------------- */
29019 +
29020 +static struct proc_dir_entry *au_procfs_dir;
29021 +
29022 +void au_procfs_fin(void)
29023 +{
29024 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
29025 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29026 +}
29027 +
29028 +int __init au_procfs_init(void)
29029 +{
29030 +       int err;
29031 +       struct proc_dir_entry *entry;
29032 +
29033 +       err = -ENOMEM;
29034 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
29035 +       if (unlikely(!au_procfs_dir))
29036 +               goto out;
29037 +
29038 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
29039 +                           au_procfs_dir, &au_procfs_plm_op);
29040 +       if (unlikely(!entry))
29041 +               goto out_dir;
29042 +
29043 +       err = 0;
29044 +       goto out; /* success */
29045 +
29046 +
29047 +out_dir:
29048 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29049 +out:
29050 +       return err;
29051 +}
29052 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
29053 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
29054 +++ linux/fs/aufs/rdu.c 2022-12-17 09:21:34.799855195 +0100
29055 @@ -0,0 +1,384 @@
29056 +// SPDX-License-Identifier: GPL-2.0
29057 +/*
29058 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29059 + *
29060 + * This program is free software; you can redistribute it and/or modify
29061 + * it under the terms of the GNU General Public License as published by
29062 + * the Free Software Foundation; either version 2 of the License, or
29063 + * (at your option) any later version.
29064 + *
29065 + * This program is distributed in the hope that it will be useful,
29066 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29067 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29068 + * GNU General Public License for more details.
29069 + *
29070 + * You should have received a copy of the GNU General Public License
29071 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29072 + */
29073 +
29074 +/*
29075 + * readdir in userspace.
29076 + */
29077 +
29078 +#include <linux/compat.h>
29079 +#include <linux/fs_stack.h>
29080 +#include <linux/security.h>
29081 +#include "aufs.h"
29082 +
29083 +/* bits for struct aufs_rdu.flags */
29084 +#define        AuRdu_CALLED    1
29085 +#define        AuRdu_CONT      (1 << 1)
29086 +#define        AuRdu_FULL      (1 << 2)
29087 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
29088 +#define au_fset_rdu(flags, name) \
29089 +       do { (flags) |= AuRdu_##name; } while (0)
29090 +#define au_fclr_rdu(flags, name) \
29091 +       do { (flags) &= ~AuRdu_##name; } while (0)
29092 +
29093 +struct au_rdu_arg {
29094 +       struct dir_context              ctx;
29095 +       struct aufs_rdu                 *rdu;
29096 +       union au_rdu_ent_ul             ent;
29097 +       unsigned long                   end;
29098 +
29099 +       struct super_block              *sb;
29100 +       int                             err;
29101 +};
29102 +
29103 +static bool au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29104 +                       loff_t offset, u64 h_ino, unsigned int d_type)
29105 +{
29106 +       int err, len;
29107 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
29108 +       struct aufs_rdu *rdu = arg->rdu;
29109 +       struct au_rdu_ent ent;
29110 +
29111 +       err = 0;
29112 +       arg->err = 0;
29113 +       au_fset_rdu(rdu->cookie.flags, CALLED);
29114 +       len = au_rdu_len(nlen);
29115 +       if (arg->ent.ul + len  < arg->end) {
29116 +               ent.ino = h_ino;
29117 +               ent.bindex = rdu->cookie.bindex;
29118 +               ent.type = d_type;
29119 +               ent.nlen = nlen;
29120 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
29121 +                       ent.type = DT_UNKNOWN;
29122 +
29123 +               /* unnecessary to support mmap_sem since this is a dir */
29124 +               err = -EFAULT;
29125 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29126 +                       goto out;
29127 +               if (copy_to_user(arg->ent.e->name, name, nlen))
29128 +                       goto out;
29129 +               /* the terminating NULL */
29130 +               if (__put_user(0, arg->ent.e->name + nlen))
29131 +                       goto out;
29132 +               err = 0;
29133 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29134 +               arg->ent.ul += len;
29135 +               rdu->rent++;
29136 +       } else {
29137 +               err = -EFAULT;
29138 +               au_fset_rdu(rdu->cookie.flags, FULL);
29139 +               rdu->full = 1;
29140 +               rdu->tail = arg->ent;
29141 +       }
29142 +
29143 +out:
29144 +       /* AuTraceErr(err); */
29145 +       return !err;
29146 +}
29147 +
29148 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29149 +{
29150 +       int err;
29151 +       loff_t offset;
29152 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29153 +
29154 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
29155 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29156 +       err = offset;
29157 +       if (unlikely(offset != cookie->h_pos))
29158 +               goto out;
29159 +
29160 +       err = 0;
29161 +       do {
29162 +               arg->err = 0;
29163 +               au_fclr_rdu(cookie->flags, CALLED);
29164 +               /* smp_mb(); */
29165 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
29166 +               if (err >= 0)
29167 +                       err = arg->err;
29168 +       } while (!err
29169 +                && au_ftest_rdu(cookie->flags, CALLED)
29170 +                && !au_ftest_rdu(cookie->flags, FULL));
29171 +       cookie->h_pos = h_file->f_pos;
29172 +
29173 +out:
29174 +       AuTraceErr(err);
29175 +       return err;
29176 +}
29177 +
29178 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29179 +{
29180 +       int err;
29181 +       aufs_bindex_t bbot;
29182 +       struct au_rdu_arg arg = {
29183 +               .ctx = {
29184 +                       .actor = au_rdu_fill
29185 +               }
29186 +       };
29187 +       struct dentry *dentry;
29188 +       struct inode *inode;
29189 +       struct file *h_file;
29190 +       struct au_rdu_cookie *cookie = &rdu->cookie;
29191 +
29192 +       /* VERIFY_WRITE */
29193 +       err = !access_ok(rdu->ent.e, rdu->sz);
29194 +       if (unlikely(err)) {
29195 +               err = -EFAULT;
29196 +               AuTraceErr(err);
29197 +               goto out;
29198 +       }
29199 +       rdu->rent = 0;
29200 +       rdu->tail = rdu->ent;
29201 +       rdu->full = 0;
29202 +       arg.rdu = rdu;
29203 +       arg.ent = rdu->ent;
29204 +       arg.end = arg.ent.ul;
29205 +       arg.end += rdu->sz;
29206 +
29207 +       err = -ENOTDIR;
29208 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
29209 +               goto out;
29210 +
29211 +       err = security_file_permission(file, MAY_READ);
29212 +       AuTraceErr(err);
29213 +       if (unlikely(err))
29214 +               goto out;
29215 +
29216 +       dentry = file->f_path.dentry;
29217 +       inode = d_inode(dentry);
29218 +       inode_lock_shared(inode);
29219 +
29220 +       arg.sb = inode->i_sb;
29221 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29222 +       if (unlikely(err))
29223 +               goto out_mtx;
29224 +       err = au_alive_dir(dentry);
29225 +       if (unlikely(err))
29226 +               goto out_si;
29227 +       /* todo: reval? */
29228 +       fi_read_lock(file);
29229 +
29230 +       err = -EAGAIN;
29231 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29232 +                    && cookie->generation != au_figen(file)))
29233 +               goto out_unlock;
29234 +
29235 +       err = 0;
29236 +       if (!rdu->blk) {
29237 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
29238 +               if (!rdu->blk)
29239 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
29240 +       }
29241 +       bbot = au_fbtop(file);
29242 +       if (cookie->bindex < bbot)
29243 +               cookie->bindex = bbot;
29244 +       bbot = au_fbbot_dir(file);
29245 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29246 +       for (; !err && cookie->bindex <= bbot;
29247 +            cookie->bindex++, cookie->h_pos = 0) {
29248 +               h_file = au_hf_dir(file, cookie->bindex);
29249 +               if (!h_file)
29250 +                       continue;
29251 +
29252 +               au_fclr_rdu(cookie->flags, FULL);
29253 +               err = au_rdu_do(h_file, &arg);
29254 +               AuTraceErr(err);
29255 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29256 +                       break;
29257 +       }
29258 +       AuDbg("rent %llu\n", rdu->rent);
29259 +
29260 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29261 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29262 +               au_fset_rdu(cookie->flags, CONT);
29263 +               cookie->generation = au_figen(file);
29264 +       }
29265 +
29266 +       ii_read_lock_child(inode);
29267 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
29268 +       ii_read_unlock(inode);
29269 +
29270 +out_unlock:
29271 +       fi_read_unlock(file);
29272 +out_si:
29273 +       si_read_unlock(arg.sb);
29274 +out_mtx:
29275 +       inode_unlock_shared(inode);
29276 +out:
29277 +       AuTraceErr(err);
29278 +       return err;
29279 +}
29280 +
29281 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29282 +{
29283 +       int err;
29284 +       ino_t ino;
29285 +       unsigned long long nent;
29286 +       union au_rdu_ent_ul *u;
29287 +       struct au_rdu_ent ent;
29288 +       struct super_block *sb;
29289 +
29290 +       err = 0;
29291 +       nent = rdu->nent;
29292 +       u = &rdu->ent;
29293 +       sb = file->f_path.dentry->d_sb;
29294 +       si_read_lock(sb, AuLock_FLUSH);
29295 +       while (nent-- > 0) {
29296 +               /* unnecessary to support mmap_sem since this is a dir */
29297 +               err = copy_from_user(&ent, u->e, sizeof(ent));
29298 +               if (!err)
29299 +                       /* VERIFY_WRITE */
29300 +                       err = !access_ok(&u->e->ino, sizeof(ino));
29301 +               if (unlikely(err)) {
29302 +                       err = -EFAULT;
29303 +                       AuTraceErr(err);
29304 +                       break;
29305 +               }
29306 +
29307 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29308 +               if (!ent.wh)
29309 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29310 +               else
29311 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29312 +                                       &ino);
29313 +               if (unlikely(err)) {
29314 +                       AuTraceErr(err);
29315 +                       break;
29316 +               }
29317 +
29318 +               err = __put_user(ino, &u->e->ino);
29319 +               if (unlikely(err)) {
29320 +                       err = -EFAULT;
29321 +                       AuTraceErr(err);
29322 +                       break;
29323 +               }
29324 +               u->ul += au_rdu_len(ent.nlen);
29325 +       }
29326 +       si_read_unlock(sb);
29327 +
29328 +       return err;
29329 +}
29330 +
29331 +/* ---------------------------------------------------------------------- */
29332 +
29333 +static int au_rdu_verify(struct aufs_rdu *rdu)
29334 +{
29335 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29336 +             "%llu, b%d, 0x%x, g%u}\n",
29337 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29338 +             rdu->blk,
29339 +             rdu->rent, rdu->shwh, rdu->full,
29340 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29341 +             rdu->cookie.generation);
29342 +
29343 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29344 +               return 0;
29345 +
29346 +       AuDbg("%u:%u\n",
29347 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29348 +       return -EINVAL;
29349 +}
29350 +
29351 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29352 +{
29353 +       long err, e;
29354 +       struct aufs_rdu rdu;
29355 +       void __user *p = (void __user *)arg;
29356 +
29357 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29358 +       if (unlikely(err)) {
29359 +               err = -EFAULT;
29360 +               AuTraceErr(err);
29361 +               goto out;
29362 +       }
29363 +       err = au_rdu_verify(&rdu);
29364 +       if (unlikely(err))
29365 +               goto out;
29366 +
29367 +       switch (cmd) {
29368 +       case AUFS_CTL_RDU:
29369 +               err = au_rdu(file, &rdu);
29370 +               if (unlikely(err))
29371 +                       break;
29372 +
29373 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29374 +               if (unlikely(e)) {
29375 +                       err = -EFAULT;
29376 +                       AuTraceErr(err);
29377 +               }
29378 +               break;
29379 +       case AUFS_CTL_RDU_INO:
29380 +               err = au_rdu_ino(file, &rdu);
29381 +               break;
29382 +
29383 +       default:
29384 +               /* err = -ENOTTY; */
29385 +               err = -EINVAL;
29386 +       }
29387 +
29388 +out:
29389 +       AuTraceErr(err);
29390 +       return err;
29391 +}
29392 +
29393 +#ifdef CONFIG_COMPAT
29394 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29395 +{
29396 +       long err, e;
29397 +       struct aufs_rdu rdu;
29398 +       void __user *p = compat_ptr(arg);
29399 +
29400 +       /* todo: get_user()? */
29401 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29402 +       if (unlikely(err)) {
29403 +               err = -EFAULT;
29404 +               AuTraceErr(err);
29405 +               goto out;
29406 +       }
29407 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29408 +       err = au_rdu_verify(&rdu);
29409 +       if (unlikely(err))
29410 +               goto out;
29411 +
29412 +       switch (cmd) {
29413 +       case AUFS_CTL_RDU:
29414 +               err = au_rdu(file, &rdu);
29415 +               if (unlikely(err))
29416 +                       break;
29417 +
29418 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29419 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29420 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29421 +               if (unlikely(e)) {
29422 +                       err = -EFAULT;
29423 +                       AuTraceErr(err);
29424 +               }
29425 +               break;
29426 +       case AUFS_CTL_RDU_INO:
29427 +               err = au_rdu_ino(file, &rdu);
29428 +               break;
29429 +
29430 +       default:
29431 +               /* err = -ENOTTY; */
29432 +               err = -EINVAL;
29433 +       }
29434 +
29435 +out:
29436 +       AuTraceErr(err);
29437 +       return err;
29438 +}
29439 +#endif
29440 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29441 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29442 +++ linux/fs/aufs/rwsem.h       2022-11-05 23:02:18.969222617 +0100
29443 @@ -0,0 +1,85 @@
29444 +/* SPDX-License-Identifier: GPL-2.0 */
29445 +/*
29446 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29447 + *
29448 + * This program is free software; you can redistribute it and/or modify
29449 + * it under the terms of the GNU General Public License as published by
29450 + * the Free Software Foundation; either version 2 of the License, or
29451 + * (at your option) any later version.
29452 + *
29453 + * This program is distributed in the hope that it will be useful,
29454 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29455 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29456 + * GNU General Public License for more details.
29457 + *
29458 + * You should have received a copy of the GNU General Public License
29459 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29460 + */
29461 +
29462 +/*
29463 + * simple read-write semaphore wrappers
29464 + */
29465 +
29466 +#ifndef __AUFS_RWSEM_H__
29467 +#define __AUFS_RWSEM_H__
29468 +
29469 +#ifdef __KERNEL__
29470 +
29471 +#include "debug.h"
29472 +
29473 +/* in the future, the name 'au_rwsem' will be totally gone */
29474 +#define au_rwsem       rw_semaphore
29475 +
29476 +/* to debug easier, do not make them inlined functions */
29477 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29478 +
29479 +#ifdef CONFIG_LOCKDEP
29480 +/* rwsem_is_locked() is unusable */
29481 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29482 +                                         && !lockdep_recursing(current) \
29483 +                                         && debug_locks                \
29484 +                                         && !lockdep_is_held_type(rw, 1))
29485 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29486 +                                         && !lockdep_recursing(current) \
29487 +                                         && debug_locks                \
29488 +                                         && !lockdep_is_held_type(rw, 0))
29489 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29490 +                                         && !lockdep_recursing(current) \
29491 +                                         && debug_locks                \
29492 +                                         && !lockdep_is_held(rw))
29493 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29494 +                                         && !lockdep_recursing(current) \
29495 +                                         && debug_locks                \
29496 +                                         && lockdep_is_held(rw))
29497 +#else
29498 +#define AuRwMustReadLock(rw)   do {} while (0)
29499 +#define AuRwMustWriteLock(rw)  do {} while (0)
29500 +#define AuRwMustAnyLock(rw)    do {} while (0)
29501 +#define AuRwDestroy(rw)                do {} while (0)
29502 +#endif
29503 +
29504 +#define au_rw_init(rw) init_rwsem(rw)
29505 +
29506 +#define au_rw_init_wlock(rw) do {              \
29507 +               au_rw_init(rw);                 \
29508 +               down_write(rw);                 \
29509 +       } while (0)
29510 +
29511 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29512 +               au_rw_init(rw);                 \
29513 +               down_write_nested(rw, lsc);     \
29514 +       } while (0)
29515 +
29516 +#define au_rw_read_lock(rw)            down_read(rw)
29517 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29518 +#define au_rw_read_unlock(rw)          up_read(rw)
29519 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29520 +#define au_rw_write_lock(rw)           down_write(rw)
29521 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29522 +#define au_rw_write_unlock(rw)         up_write(rw)
29523 +/* why is not _nested version defined? */
29524 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29525 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29526 +
29527 +#endif /* __KERNEL__ */
29528 +#endif /* __AUFS_RWSEM_H__ */
29529 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29530 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29531 +++ linux/fs/aufs/sbinfo.c      2022-11-05 23:02:18.969222617 +0100
29532 @@ -0,0 +1,316 @@
29533 +// SPDX-License-Identifier: GPL-2.0
29534 +/*
29535 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29536 + *
29537 + * This program is free software; you can redistribute it and/or modify
29538 + * it under the terms of the GNU General Public License as published by
29539 + * the Free Software Foundation; either version 2 of the License, or
29540 + * (at your option) any later version.
29541 + *
29542 + * This program is distributed in the hope that it will be useful,
29543 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29544 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29545 + * GNU General Public License for more details.
29546 + *
29547 + * You should have received a copy of the GNU General Public License
29548 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29549 + */
29550 +
29551 +/*
29552 + * superblock private data
29553 + */
29554 +
29555 +#include <linux/iversion.h>
29556 +#include "aufs.h"
29557 +
29558 +/*
29559 + * they are necessary regardless sysfs is disabled.
29560 + */
29561 +void au_si_free(struct kobject *kobj)
29562 +{
29563 +       int i;
29564 +       struct au_sbinfo *sbinfo;
29565 +       char *locked __maybe_unused; /* debug only */
29566 +
29567 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29568 +       for (i = 0; i < AuPlink_NHASH; i++)
29569 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29570 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29571 +
29572 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29573 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29574 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29575 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29576 +
29577 +       dbgaufs_si_fin(sbinfo);
29578 +       au_rw_write_lock(&sbinfo->si_rwsem);
29579 +       au_br_free(sbinfo);
29580 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29581 +
29582 +       au_kfree_try_rcu(sbinfo->si_branch);
29583 +       mutex_destroy(&sbinfo->si_xib_mtx);
29584 +       AuRwDestroy(&sbinfo->si_rwsem);
29585 +
29586 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29587 +       /* si_nfiles is waited too */
29588 +       au_kfree_rcu(sbinfo);
29589 +}
29590 +
29591 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29592 +{
29593 +       struct au_sbinfo *sbinfo;
29594 +       int err, i;
29595 +
29596 +       err = -ENOMEM;
29597 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29598 +       if (unlikely(!sbinfo))
29599 +               goto out;
29600 +
29601 +       /* will be reallocated separately */
29602 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29603 +       if (unlikely(!sbinfo->si_branch))
29604 +               goto out_sbinfo;
29605 +
29606 +       err = sysaufs_si_init(sbinfo);
29607 +       if (!err) {
29608 +               dbgaufs_si_null(sbinfo);
29609 +               err = dbgaufs_si_init(sbinfo);
29610 +               if (unlikely(err))
29611 +                       kobject_put(&sbinfo->si_kobj);
29612 +       }
29613 +       if (unlikely(err))
29614 +               goto out_br;
29615 +
29616 +       au_nwt_init(&sbinfo->si_nowait);
29617 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29618 +
29619 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29620 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29621 +
29622 +       sbinfo->si_bbot = -1;
29623 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29624 +
29625 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29626 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29627 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29628 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29629 +
29630 +       au_fhsm_init(sbinfo);
29631 +
29632 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29633 +
29634 +       sbinfo->si_xino_jiffy = jiffies;
29635 +       sbinfo->si_xino_expire
29636 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29637 +       mutex_init(&sbinfo->si_xib_mtx);
29638 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29639 +
29640 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29641 +
29642 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29643 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29644 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29645 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29646 +
29647 +       for (i = 0; i < AuPlink_NHASH; i++)
29648 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29649 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29650 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29651 +
29652 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29653 +
29654 +       /* with getattr by default */
29655 +       sbinfo->si_iop_array = aufs_iop;
29656 +
29657 +       /* leave other members for sysaufs and si_mnt. */
29658 +       sbinfo->si_sb = sb;
29659 +       if (sb) {
29660 +               sb->s_fs_info = sbinfo;
29661 +               si_pid_set(sb);
29662 +       }
29663 +       return sbinfo; /* success */
29664 +
29665 +out_br:
29666 +       au_kfree_try_rcu(sbinfo->si_branch);
29667 +out_sbinfo:
29668 +       au_kfree_rcu(sbinfo);
29669 +out:
29670 +       return ERR_PTR(err);
29671 +}
29672 +
29673 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29674 +{
29675 +       int err, sz;
29676 +       struct au_branch **brp;
29677 +
29678 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29679 +
29680 +       err = -ENOMEM;
29681 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29682 +       if (unlikely(!sz))
29683 +               sz = sizeof(*brp);
29684 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29685 +                          may_shrink);
29686 +       if (brp) {
29687 +               sbinfo->si_branch = brp;
29688 +               err = 0;
29689 +       }
29690 +
29691 +       return err;
29692 +}
29693 +
29694 +/* ---------------------------------------------------------------------- */
29695 +
29696 +unsigned int au_sigen_inc(struct super_block *sb)
29697 +{
29698 +       unsigned int gen;
29699 +       struct inode *inode;
29700 +
29701 +       SiMustWriteLock(sb);
29702 +
29703 +       gen = ++au_sbi(sb)->si_generation;
29704 +       au_update_digen(sb->s_root);
29705 +       inode = d_inode(sb->s_root);
29706 +       au_update_iigen(inode, /*half*/0);
29707 +       inode_inc_iversion(inode);
29708 +       return gen;
29709 +}
29710 +
29711 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29712 +{
29713 +       aufs_bindex_t br_id;
29714 +       int i;
29715 +       struct au_sbinfo *sbinfo;
29716 +
29717 +       SiMustWriteLock(sb);
29718 +
29719 +       sbinfo = au_sbi(sb);
29720 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29721 +               br_id = ++sbinfo->si_last_br_id;
29722 +               AuDebugOn(br_id < 0);
29723 +               if (br_id && au_br_index(sb, br_id) < 0)
29724 +                       return br_id;
29725 +       }
29726 +
29727 +       return -1;
29728 +}
29729 +
29730 +/* ---------------------------------------------------------------------- */
29731 +
29732 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29733 +int si_read_lock(struct super_block *sb, int flags)
29734 +{
29735 +       int err;
29736 +
29737 +       err = 0;
29738 +       if (au_ftest_lock(flags, FLUSH))
29739 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29740 +
29741 +       si_noflush_read_lock(sb);
29742 +       err = au_plink_maint(sb, flags);
29743 +       if (unlikely(err))
29744 +               si_read_unlock(sb);
29745 +
29746 +       return err;
29747 +}
29748 +
29749 +int si_write_lock(struct super_block *sb, int flags)
29750 +{
29751 +       int err;
29752 +
29753 +       if (au_ftest_lock(flags, FLUSH))
29754 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29755 +
29756 +       si_noflush_write_lock(sb);
29757 +       err = au_plink_maint(sb, flags);
29758 +       if (unlikely(err))
29759 +               si_write_unlock(sb);
29760 +
29761 +       return err;
29762 +}
29763 +
29764 +/* dentry and super_block lock. call at entry point */
29765 +int aufs_read_lock(struct dentry *dentry, int flags)
29766 +{
29767 +       int err;
29768 +       struct super_block *sb;
29769 +
29770 +       sb = dentry->d_sb;
29771 +       err = si_read_lock(sb, flags);
29772 +       if (unlikely(err))
29773 +               goto out;
29774 +
29775 +       if (au_ftest_lock(flags, DW))
29776 +               di_write_lock_child(dentry);
29777 +       else
29778 +               di_read_lock_child(dentry, flags);
29779 +
29780 +       if (au_ftest_lock(flags, GEN)) {
29781 +               err = au_digen_test(dentry, au_sigen(sb));
29782 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29783 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29784 +               else if (!err)
29785 +                       err = au_dbrange_test(dentry);
29786 +               if (unlikely(err))
29787 +                       aufs_read_unlock(dentry, flags);
29788 +       }
29789 +
29790 +out:
29791 +       return err;
29792 +}
29793 +
29794 +void aufs_read_unlock(struct dentry *dentry, int flags)
29795 +{
29796 +       if (au_ftest_lock(flags, DW))
29797 +               di_write_unlock(dentry);
29798 +       else
29799 +               di_read_unlock(dentry, flags);
29800 +       si_read_unlock(dentry->d_sb);
29801 +}
29802 +
29803 +void aufs_write_lock(struct dentry *dentry)
29804 +{
29805 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29806 +       di_write_lock_child(dentry);
29807 +}
29808 +
29809 +void aufs_write_unlock(struct dentry *dentry)
29810 +{
29811 +       di_write_unlock(dentry);
29812 +       si_write_unlock(dentry->d_sb);
29813 +}
29814 +
29815 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29816 +{
29817 +       int err;
29818 +       unsigned int sigen;
29819 +       struct super_block *sb;
29820 +
29821 +       sb = d1->d_sb;
29822 +       err = si_read_lock(sb, flags);
29823 +       if (unlikely(err))
29824 +               goto out;
29825 +
29826 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29827 +
29828 +       if (au_ftest_lock(flags, GEN)) {
29829 +               sigen = au_sigen(sb);
29830 +               err = au_digen_test(d1, sigen);
29831 +               AuDebugOn(!err && au_dbrange_test(d1));
29832 +               if (!err) {
29833 +                       err = au_digen_test(d2, sigen);
29834 +                       AuDebugOn(!err && au_dbrange_test(d2));
29835 +               }
29836 +               if (unlikely(err))
29837 +                       aufs_read_and_write_unlock2(d1, d2);
29838 +       }
29839 +
29840 +out:
29841 +       return err;
29842 +}
29843 +
29844 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29845 +{
29846 +       di_write_unlock2(d1, d2);
29847 +       si_read_unlock(d1->d_sb);
29848 +}
29849 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29850 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29851 +++ linux/fs/aufs/super.c       2022-11-05 23:02:18.969222617 +0100
29852 @@ -0,0 +1,871 @@
29853 +// SPDX-License-Identifier: GPL-2.0
29854 +/*
29855 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29856 + *
29857 + * This program is free software; you can redistribute it and/or modify
29858 + * it under the terms of the GNU General Public License as published by
29859 + * the Free Software Foundation; either version 2 of the License, or
29860 + * (at your option) any later version.
29861 + *
29862 + * This program is distributed in the hope that it will be useful,
29863 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29864 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29865 + * GNU General Public License for more details.
29866 + *
29867 + * You should have received a copy of the GNU General Public License
29868 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29869 + */
29870 +
29871 +/*
29872 + * mount and super_block operations
29873 + */
29874 +
29875 +#include <linux/iversion.h>
29876 +#include <linux/mm.h>
29877 +#include <linux/seq_file.h>
29878 +#include <linux/statfs.h>
29879 +#include <linux/vmalloc.h>
29880 +#include "aufs.h"
29881 +
29882 +/*
29883 + * super_operations
29884 + */
29885 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29886 +{
29887 +       struct au_icntnr *c;
29888 +
29889 +       c = au_cache_alloc_icntnr(sb);
29890 +       if (c) {
29891 +               au_icntnr_init(c);
29892 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29893 +               c->iinfo.ii_hinode = NULL;
29894 +               return &c->vfs_inode;
29895 +       }
29896 +       return NULL;
29897 +}
29898 +
29899 +static void aufs_destroy_inode(struct inode *inode)
29900 +{
29901 +       if (!au_is_bad_inode(inode))
29902 +               au_iinfo_fin(inode);
29903 +}
29904 +
29905 +static void aufs_free_inode(struct inode *inode)
29906 +{
29907 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29908 +}
29909 +
29910 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29911 +{
29912 +       struct inode *inode;
29913 +       int err;
29914 +
29915 +       inode = iget_locked(sb, ino);
29916 +       if (unlikely(!inode)) {
29917 +               inode = ERR_PTR(-ENOMEM);
29918 +               goto out;
29919 +       }
29920 +       if (!(inode->i_state & I_NEW))
29921 +               goto out;
29922 +
29923 +       err = au_xigen_new(inode);
29924 +       if (!err)
29925 +               err = au_iinfo_init(inode);
29926 +       if (!err)
29927 +               inode_inc_iversion(inode);
29928 +       else {
29929 +               iget_failed(inode);
29930 +               inode = ERR_PTR(err);
29931 +       }
29932 +
29933 +out:
29934 +       /* never return NULL */
29935 +       AuDebugOn(!inode);
29936 +       AuTraceErrPtr(inode);
29937 +       return inode;
29938 +}
29939 +
29940 +/* lock free root dinfo */
29941 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29942 +{
29943 +       int err;
29944 +       aufs_bindex_t bindex, bbot;
29945 +       struct path path;
29946 +       struct au_hdentry *hdp;
29947 +       struct au_branch *br;
29948 +       au_br_perm_str_t perm;
29949 +
29950 +       err = 0;
29951 +       bbot = au_sbbot(sb);
29952 +       bindex = 0;
29953 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29954 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29955 +               br = au_sbr(sb, bindex);
29956 +               path.mnt = au_br_mnt(br);
29957 +               path.dentry = hdp->hd_dentry;
29958 +               err = au_seq_path(seq, &path);
29959 +               if (!err) {
29960 +                       au_optstr_br_perm(&perm, br->br_perm);
29961 +                       seq_printf(seq, "=%s", perm.a);
29962 +                       if (bindex != bbot)
29963 +                               seq_putc(seq, ':');
29964 +               }
29965 +       }
29966 +       if (unlikely(err || seq_has_overflowed(seq)))
29967 +               err = -E2BIG;
29968 +
29969 +       return err;
29970 +}
29971 +
29972 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29973 +                      const char *append)
29974 +{
29975 +       char *p;
29976 +
29977 +       p = fmt;
29978 +       while (*pat != ':')
29979 +               *p++ = *pat++;
29980 +       *p++ = *pat++;
29981 +       strcpy(p, append);
29982 +       AuDebugOn(strlen(fmt) >= len);
29983 +}
29984 +
29985 +static void au_show_wbr_create(struct seq_file *m, int v,
29986 +                              struct au_sbinfo *sbinfo)
29987 +{
29988 +       const char *pat;
29989 +       char fmt[32];
29990 +       struct au_wbr_mfs *mfs;
29991 +
29992 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29993 +
29994 +       seq_puts(m, ",create=");
29995 +       pat = au_optstr_wbr_create(v);
29996 +       mfs = &sbinfo->si_wbr_mfs;
29997 +       switch (v) {
29998 +       case AuWbrCreate_TDP:
29999 +       case AuWbrCreate_RR:
30000 +       case AuWbrCreate_MFS:
30001 +       case AuWbrCreate_PMFS:
30002 +               seq_puts(m, pat);
30003 +               break;
30004 +       case AuWbrCreate_MFSRR:
30005 +       case AuWbrCreate_TDMFS:
30006 +       case AuWbrCreate_PMFSRR:
30007 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
30008 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
30009 +               break;
30010 +       case AuWbrCreate_MFSV:
30011 +       case AuWbrCreate_PMFSV:
30012 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
30013 +               seq_printf(m, fmt,
30014 +                          jiffies_to_msecs(mfs->mfs_expire)
30015 +                          / MSEC_PER_SEC);
30016 +               break;
30017 +       case AuWbrCreate_MFSRRV:
30018 +       case AuWbrCreate_TDMFSV:
30019 +       case AuWbrCreate_PMFSRRV:
30020 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
30021 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
30022 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
30023 +               break;
30024 +       default:
30025 +               BUG();
30026 +       }
30027 +}
30028 +
30029 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
30030 +{
30031 +#ifdef CONFIG_SYSFS
30032 +       return 0;
30033 +#else
30034 +       int err;
30035 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
30036 +       aufs_bindex_t bindex, brid;
30037 +       struct qstr *name;
30038 +       struct file *f;
30039 +       struct dentry *d, *h_root;
30040 +       struct au_branch *br;
30041 +
30042 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30043 +
30044 +       err = 0;
30045 +       f = au_sbi(sb)->si_xib;
30046 +       if (!f)
30047 +               goto out;
30048 +
30049 +       /* stop printing the default xino path on the first writable branch */
30050 +       h_root = NULL;
30051 +       bindex = au_xi_root(sb, f->f_path.dentry);
30052 +       if (bindex >= 0) {
30053 +               br = au_sbr_sb(sb, bindex);
30054 +               h_root = au_br_dentry(br);
30055 +       }
30056 +
30057 +       d = f->f_path.dentry;
30058 +       name = &d->d_name;
30059 +       /* safe ->d_parent because the file is unlinked */
30060 +       if (d->d_parent == h_root
30061 +           && name->len == len
30062 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
30063 +               goto out;
30064 +
30065 +       seq_puts(seq, ",xino=");
30066 +       err = au_xino_path(seq, f);
30067 +
30068 +out:
30069 +       return err;
30070 +#endif
30071 +}
30072 +
30073 +/* seq_file will re-call me in case of too long string */
30074 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
30075 +{
30076 +       int err;
30077 +       unsigned int mnt_flags, v;
30078 +       struct super_block *sb;
30079 +       struct au_sbinfo *sbinfo;
30080 +
30081 +#define AuBool(name, str) do { \
30082 +       v = au_opt_test(mnt_flags, name); \
30083 +       if (v != au_opt_test(AuOpt_Def, name)) \
30084 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
30085 +} while (0)
30086 +
30087 +#define AuStr(name, str) do { \
30088 +       v = mnt_flags & AuOptMask_##name; \
30089 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
30090 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30091 +} while (0)
30092 +
30093 +#define AuUInt(name, str, val) do { \
30094 +       if (val != AUFS_##name##_DEF) \
30095 +               seq_printf(m, "," #str "=%u", val); \
30096 +} while (0)
30097 +
30098 +       sb = dentry->d_sb;
30099 +       if (sb->s_flags & SB_POSIXACL)
30100 +               seq_puts(m, ",acl");
30101 +#if 0 /* reserved for future use */
30102 +       if (sb->s_flags & SB_I_VERSION)
30103 +               seq_puts(m, ",i_version");
30104 +#endif
30105 +
30106 +       /* lock free root dinfo */
30107 +       si_noflush_read_lock(sb);
30108 +       sbinfo = au_sbi(sb);
30109 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30110 +
30111 +       mnt_flags = au_mntflags(sb);
30112 +       if (au_opt_test(mnt_flags, XINO)) {
30113 +               err = au_show_xino(m, sb);
30114 +               if (unlikely(err))
30115 +                       goto out;
30116 +       } else
30117 +               seq_puts(m, ",noxino");
30118 +
30119 +       AuBool(TRUNC_XINO, trunc_xino);
30120 +       AuStr(UDBA, udba);
30121 +       AuBool(SHWH, shwh);
30122 +       AuBool(PLINK, plink);
30123 +       AuBool(DIO, dio);
30124 +       AuBool(DIRPERM1, dirperm1);
30125 +
30126 +       v = sbinfo->si_wbr_create;
30127 +       if (v != AuWbrCreate_Def)
30128 +               au_show_wbr_create(m, v, sbinfo);
30129 +
30130 +       v = sbinfo->si_wbr_copyup;
30131 +       if (v != AuWbrCopyup_Def)
30132 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30133 +
30134 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30135 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30136 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30137 +
30138 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30139 +
30140 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30141 +       AuUInt(RDCACHE, rdcache, v);
30142 +
30143 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30144 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30145 +
30146 +       au_fhsm_show(m, sbinfo);
30147 +
30148 +       AuBool(DIRREN, dirren);
30149 +       AuBool(SUM, sum);
30150 +       /* AuBool(SUM_W, wsum); */
30151 +       AuBool(WARN_PERM, warn_perm);
30152 +       AuBool(VERBOSE, verbose);
30153 +
30154 +out:
30155 +       /* be sure to print "br:" last */
30156 +       if (!sysaufs_brs) {
30157 +               seq_puts(m, ",br:");
30158 +               au_show_brs(m, sb);
30159 +       }
30160 +       si_read_unlock(sb);
30161 +       return 0;
30162 +
30163 +#undef AuBool
30164 +#undef AuStr
30165 +#undef AuUInt
30166 +}
30167 +
30168 +/* ---------------------------------------------------------------------- */
30169 +
30170 +/* sum mode which returns the summation for statfs(2) */
30171 +
30172 +static u64 au_add_till_max(u64 a, u64 b)
30173 +{
30174 +       u64 old;
30175 +
30176 +       old = a;
30177 +       a += b;
30178 +       if (old <= a)
30179 +               return a;
30180 +       return ULLONG_MAX;
30181 +}
30182 +
30183 +static u64 au_mul_till_max(u64 a, long mul)
30184 +{
30185 +       u64 old;
30186 +
30187 +       old = a;
30188 +       a *= mul;
30189 +       if (old <= a)
30190 +               return a;
30191 +       return ULLONG_MAX;
30192 +}
30193 +
30194 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30195 +{
30196 +       int err;
30197 +       long bsize, factor;
30198 +       u64 blocks, bfree, bavail, files, ffree;
30199 +       aufs_bindex_t bbot, bindex, i;
30200 +       unsigned char shared;
30201 +       struct path h_path;
30202 +       struct super_block *h_sb;
30203 +
30204 +       err = 0;
30205 +       bsize = LONG_MAX;
30206 +       files = 0;
30207 +       ffree = 0;
30208 +       blocks = 0;
30209 +       bfree = 0;
30210 +       bavail = 0;
30211 +       bbot = au_sbbot(sb);
30212 +       for (bindex = 0; bindex <= bbot; bindex++) {
30213 +               h_path.mnt = au_sbr_mnt(sb, bindex);
30214 +               h_sb = h_path.mnt->mnt_sb;
30215 +               shared = 0;
30216 +               for (i = 0; !shared && i < bindex; i++)
30217 +                       shared = (au_sbr_sb(sb, i) == h_sb);
30218 +               if (shared)
30219 +                       continue;
30220 +
30221 +               /* sb->s_root for NFS is unreliable */
30222 +               h_path.dentry = h_path.mnt->mnt_root;
30223 +               err = vfs_statfs(&h_path, buf);
30224 +               if (unlikely(err))
30225 +                       goto out;
30226 +
30227 +               if (bsize > buf->f_bsize) {
30228 +                       /*
30229 +                        * we will reduce bsize, so we have to expand blocks
30230 +                        * etc. to match them again
30231 +                        */
30232 +                       factor = (bsize / buf->f_bsize);
30233 +                       blocks = au_mul_till_max(blocks, factor);
30234 +                       bfree = au_mul_till_max(bfree, factor);
30235 +                       bavail = au_mul_till_max(bavail, factor);
30236 +                       bsize = buf->f_bsize;
30237 +               }
30238 +
30239 +               factor = (buf->f_bsize / bsize);
30240 +               blocks = au_add_till_max(blocks,
30241 +                               au_mul_till_max(buf->f_blocks, factor));
30242 +               bfree = au_add_till_max(bfree,
30243 +                               au_mul_till_max(buf->f_bfree, factor));
30244 +               bavail = au_add_till_max(bavail,
30245 +                               au_mul_till_max(buf->f_bavail, factor));
30246 +               files = au_add_till_max(files, buf->f_files);
30247 +               ffree = au_add_till_max(ffree, buf->f_ffree);
30248 +       }
30249 +
30250 +       buf->f_bsize = bsize;
30251 +       buf->f_blocks = blocks;
30252 +       buf->f_bfree = bfree;
30253 +       buf->f_bavail = bavail;
30254 +       buf->f_files = files;
30255 +       buf->f_ffree = ffree;
30256 +       buf->f_frsize = 0;
30257 +
30258 +out:
30259 +       return err;
30260 +}
30261 +
30262 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30263 +{
30264 +       int err;
30265 +       struct path h_path;
30266 +       struct super_block *sb;
30267 +
30268 +       /* lock free root dinfo */
30269 +       sb = dentry->d_sb;
30270 +       si_noflush_read_lock(sb);
30271 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
30272 +               /* sb->s_root for NFS is unreliable */
30273 +               h_path.mnt = au_sbr_mnt(sb, 0);
30274 +               h_path.dentry = h_path.mnt->mnt_root;
30275 +               err = vfs_statfs(&h_path, buf);
30276 +       } else
30277 +               err = au_statfs_sum(sb, buf);
30278 +       si_read_unlock(sb);
30279 +
30280 +       if (!err) {
30281 +               buf->f_type = AUFS_SUPER_MAGIC;
30282 +               buf->f_namelen = AUFS_MAX_NAMELEN;
30283 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30284 +       }
30285 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30286 +
30287 +       return err;
30288 +}
30289 +
30290 +/* ---------------------------------------------------------------------- */
30291 +
30292 +static int aufs_sync_fs(struct super_block *sb, int wait)
30293 +{
30294 +       int err, e;
30295 +       aufs_bindex_t bbot, bindex;
30296 +       struct au_branch *br;
30297 +       struct super_block *h_sb;
30298 +
30299 +       err = 0;
30300 +       si_noflush_read_lock(sb);
30301 +       bbot = au_sbbot(sb);
30302 +       for (bindex = 0; bindex <= bbot; bindex++) {
30303 +               br = au_sbr(sb, bindex);
30304 +               if (!au_br_writable(br->br_perm))
30305 +                       continue;
30306 +
30307 +               h_sb = au_sbr_sb(sb, bindex);
30308 +               e = vfsub_sync_filesystem(h_sb);
30309 +               if (unlikely(e && !err))
30310 +                       err = e;
30311 +               /* go on even if an error happens */
30312 +       }
30313 +       si_read_unlock(sb);
30314 +
30315 +       return err;
30316 +}
30317 +
30318 +/* ---------------------------------------------------------------------- */
30319 +
30320 +/* final actions when unmounting a file system */
30321 +static void aufs_put_super(struct super_block *sb)
30322 +{
30323 +       struct au_sbinfo *sbinfo;
30324 +
30325 +       sbinfo = au_sbi(sb);
30326 +       if (sbinfo)
30327 +               kobject_put(&sbinfo->si_kobj);
30328 +}
30329 +
30330 +/* ---------------------------------------------------------------------- */
30331 +
30332 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30333 +                    struct super_block *sb, void *arg)
30334 +{
30335 +       void *array;
30336 +       unsigned long long n, sz;
30337 +
30338 +       array = NULL;
30339 +       n = 0;
30340 +       if (!*hint)
30341 +               goto out;
30342 +
30343 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30344 +               array = ERR_PTR(-EMFILE);
30345 +               pr_err("hint %llu\n", *hint);
30346 +               goto out;
30347 +       }
30348 +
30349 +       sz = sizeof(array) * *hint;
30350 +       array = kzalloc(sz, GFP_NOFS);
30351 +       if (unlikely(!array))
30352 +               array = vzalloc(sz);
30353 +       if (unlikely(!array)) {
30354 +               array = ERR_PTR(-ENOMEM);
30355 +               goto out;
30356 +       }
30357 +
30358 +       n = cb(sb, array, *hint, arg);
30359 +       AuDebugOn(n > *hint);
30360 +
30361 +out:
30362 +       *hint = n;
30363 +       return array;
30364 +}
30365 +
30366 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30367 +                                      unsigned long long max __maybe_unused,
30368 +                                      void *arg)
30369 +{
30370 +       unsigned long long n;
30371 +       struct inode **p, *inode;
30372 +       struct list_head *head;
30373 +
30374 +       n = 0;
30375 +       p = a;
30376 +       head = arg;
30377 +       spin_lock(&sb->s_inode_list_lock);
30378 +       list_for_each_entry(inode, head, i_sb_list) {
30379 +               if (!au_is_bad_inode(inode)
30380 +                   && au_ii(inode)->ii_btop >= 0) {
30381 +                       spin_lock(&inode->i_lock);
30382 +                       if (atomic_read(&inode->i_count)) {
30383 +                               au_igrab(inode);
30384 +                               *p++ = inode;
30385 +                               n++;
30386 +                               AuDebugOn(n > max);
30387 +                       }
30388 +                       spin_unlock(&inode->i_lock);
30389 +               }
30390 +       }
30391 +       spin_unlock(&sb->s_inode_list_lock);
30392 +
30393 +       return n;
30394 +}
30395 +
30396 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30397 +{
30398 +       struct au_sbinfo *sbi;
30399 +
30400 +       sbi = au_sbi(sb);
30401 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30402 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30403 +}
30404 +
30405 +void au_iarray_free(struct inode **a, unsigned long long max)
30406 +{
30407 +       unsigned long long ull;
30408 +
30409 +       for (ull = 0; ull < max; ull++)
30410 +               iput(a[ull]);
30411 +       kvfree(a);
30412 +}
30413 +
30414 +/* ---------------------------------------------------------------------- */
30415 +
30416 +/*
30417 + * refresh dentry and inode at remount time.
30418 + */
30419 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30420 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30421 +                     struct dentry *parent)
30422 +{
30423 +       int err;
30424 +
30425 +       di_write_lock_child(dentry);
30426 +       di_read_lock_parent(parent, AuLock_IR);
30427 +       err = au_refresh_dentry(dentry, parent);
30428 +       if (!err && dir_flags)
30429 +               au_hn_reset(d_inode(dentry), dir_flags);
30430 +       di_read_unlock(parent, AuLock_IR);
30431 +       di_write_unlock(dentry);
30432 +
30433 +       return err;
30434 +}
30435 +
30436 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30437 +                          struct au_sbinfo *sbinfo,
30438 +                          const unsigned int dir_flags, unsigned int do_idop)
30439 +{
30440 +       int err;
30441 +       struct dentry *parent;
30442 +
30443 +       err = 0;
30444 +       parent = dget_parent(dentry);
30445 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30446 +               if (d_really_is_positive(dentry)) {
30447 +                       if (!d_is_dir(dentry))
30448 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30449 +                                                parent);
30450 +                       else {
30451 +                               err = au_do_refresh(dentry, dir_flags, parent);
30452 +                               if (unlikely(err))
30453 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30454 +                       }
30455 +               } else
30456 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30457 +               AuDbgDentry(dentry);
30458 +       }
30459 +       dput(parent);
30460 +
30461 +       if (!err) {
30462 +               if (do_idop)
30463 +                       au_refresh_dop(dentry, /*force_reval*/0);
30464 +       } else
30465 +               au_refresh_dop(dentry, /*force_reval*/1);
30466 +
30467 +       AuTraceErr(err);
30468 +       return err;
30469 +}
30470 +
30471 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30472 +{
30473 +       int err, i, j, ndentry, e;
30474 +       unsigned int sigen;
30475 +       struct au_dcsub_pages dpages;
30476 +       struct au_dpage *dpage;
30477 +       struct dentry **dentries, *d;
30478 +       struct au_sbinfo *sbinfo;
30479 +       struct dentry *root = sb->s_root;
30480 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30481 +
30482 +       if (do_idop)
30483 +               au_refresh_dop(root, /*force_reval*/0);
30484 +
30485 +       err = au_dpages_init(&dpages, GFP_NOFS);
30486 +       if (unlikely(err))
30487 +               goto out;
30488 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30489 +       if (unlikely(err))
30490 +               goto out_dpages;
30491 +
30492 +       sigen = au_sigen(sb);
30493 +       sbinfo = au_sbi(sb);
30494 +       for (i = 0; i < dpages.ndpage; i++) {
30495 +               dpage = dpages.dpages + i;
30496 +               dentries = dpage->dentries;
30497 +               ndentry = dpage->ndentry;
30498 +               for (j = 0; j < ndentry; j++) {
30499 +                       d = dentries[j];
30500 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30501 +                                           do_idop);
30502 +                       if (unlikely(e && !err))
30503 +                               err = e;
30504 +                       /* go on even err */
30505 +               }
30506 +       }
30507 +
30508 +out_dpages:
30509 +       au_dpages_free(&dpages);
30510 +out:
30511 +       return err;
30512 +}
30513 +
30514 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30515 +{
30516 +       int err, e;
30517 +       unsigned int sigen;
30518 +       unsigned long long max, ull;
30519 +       struct inode *inode, **array;
30520 +
30521 +       array = au_iarray_alloc(sb, &max);
30522 +       err = PTR_ERR(array);
30523 +       if (IS_ERR(array))
30524 +               goto out;
30525 +
30526 +       err = 0;
30527 +       sigen = au_sigen(sb);
30528 +       for (ull = 0; ull < max; ull++) {
30529 +               inode = array[ull];
30530 +               if (unlikely(!inode))
30531 +                       break;
30532 +
30533 +               e = 0;
30534 +               ii_write_lock_child(inode);
30535 +               if (au_iigen(inode, NULL) != sigen) {
30536 +                       e = au_refresh_hinode_self(inode);
30537 +                       if (unlikely(e)) {
30538 +                               au_refresh_iop(inode, /*force_getattr*/1);
30539 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30540 +                               if (!err)
30541 +                                       err = e;
30542 +                               /* go on even if err */
30543 +                       }
30544 +               }
30545 +               if (!e && do_idop)
30546 +                       au_refresh_iop(inode, /*force_getattr*/0);
30547 +               ii_write_unlock(inode);
30548 +       }
30549 +
30550 +       au_iarray_free(array, max);
30551 +
30552 +out:
30553 +       return err;
30554 +}
30555 +
30556 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30557 +{
30558 +       int err, e;
30559 +       unsigned int udba;
30560 +       aufs_bindex_t bindex, bbot;
30561 +       struct dentry *root;
30562 +       struct inode *inode;
30563 +       struct au_branch *br;
30564 +       struct au_sbinfo *sbi;
30565 +
30566 +       au_sigen_inc(sb);
30567 +       sbi = au_sbi(sb);
30568 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30569 +
30570 +       root = sb->s_root;
30571 +       DiMustNoWaiters(root);
30572 +       inode = d_inode(root);
30573 +       IiMustNoWaiters(inode);
30574 +
30575 +       udba = au_opt_udba(sb);
30576 +       bbot = au_sbbot(sb);
30577 +       for (bindex = 0; bindex <= bbot; bindex++) {
30578 +               br = au_sbr(sb, bindex);
30579 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30580 +               if (unlikely(err))
30581 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30582 +                               bindex, err);
30583 +               /* go on even if err */
30584 +       }
30585 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30586 +
30587 +       if (do_idop) {
30588 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30589 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30590 +                       sb->s_d_op = &aufs_dop_noreval;
30591 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30592 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30593 +               } else {
30594 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30595 +                       sb->s_d_op = &aufs_dop;
30596 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30597 +                       sbi->si_iop_array = aufs_iop;
30598 +               }
30599 +               pr_info("reset to %ps and %ps\n",
30600 +                       sb->s_d_op, sbi->si_iop_array);
30601 +       }
30602 +
30603 +       di_write_unlock(root);
30604 +       err = au_refresh_d(sb, do_idop);
30605 +       e = au_refresh_i(sb, do_idop);
30606 +       if (unlikely(e && !err))
30607 +               err = e;
30608 +       /* aufs_write_lock() calls ..._child() */
30609 +       di_write_lock_child(root);
30610 +
30611 +       au_cpup_attr_all(inode, /*force*/1);
30612 +
30613 +       if (unlikely(err))
30614 +               AuIOErr("refresh failed, ignored, %d\n", err);
30615 +}
30616 +
30617 +const struct super_operations aufs_sop = {
30618 +       .alloc_inode    = aufs_alloc_inode,
30619 +       .destroy_inode  = aufs_destroy_inode,
30620 +       .free_inode     = aufs_free_inode,
30621 +       /* always deleting, no clearing */
30622 +       .drop_inode     = generic_delete_inode,
30623 +       .show_options   = aufs_show_options,
30624 +       .statfs         = aufs_statfs,
30625 +       .put_super      = aufs_put_super,
30626 +       .sync_fs        = aufs_sync_fs
30627 +};
30628 +
30629 +/* ---------------------------------------------------------------------- */
30630 +
30631 +int au_alloc_root(struct super_block *sb)
30632 +{
30633 +       int err;
30634 +       struct inode *inode;
30635 +       struct dentry *root;
30636 +
30637 +       err = -ENOMEM;
30638 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30639 +       err = PTR_ERR(inode);
30640 +       if (IS_ERR(inode))
30641 +               goto out;
30642 +
30643 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30644 +       inode->i_fop = &aufs_dir_fop;
30645 +       inode->i_mode = S_IFDIR;
30646 +       set_nlink(inode, 2);
30647 +       unlock_new_inode(inode);
30648 +
30649 +       root = d_make_root(inode);
30650 +       if (unlikely(!root))
30651 +               goto out;
30652 +       err = PTR_ERR(root);
30653 +       if (IS_ERR(root))
30654 +               goto out;
30655 +
30656 +       err = au_di_init(root);
30657 +       if (!err) {
30658 +               sb->s_root = root;
30659 +               return 0; /* success */
30660 +       }
30661 +       dput(root);
30662 +
30663 +out:
30664 +       return err;
30665 +}
30666 +
30667 +/* ---------------------------------------------------------------------- */
30668 +
30669 +static void aufs_kill_sb(struct super_block *sb)
30670 +{
30671 +       struct au_sbinfo *sbinfo;
30672 +       struct dentry *root;
30673 +
30674 +       sbinfo = au_sbi(sb);
30675 +       if (!sbinfo)
30676 +               goto out;
30677 +
30678 +       au_sbilist_del(sb);
30679 +
30680 +       root = sb->s_root;
30681 +       if (root)
30682 +               aufs_write_lock(root);
30683 +       else
30684 +               __si_write_lock(sb);
30685 +
30686 +       au_fhsm_fin(sb);
30687 +       if (sbinfo->si_wbr_create_ops->fin)
30688 +               sbinfo->si_wbr_create_ops->fin(sb);
30689 +       if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30690 +               au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30691 +               au_remount_refresh(sb, /*do_idop*/0);
30692 +       }
30693 +       if (au_opt_test(sbinfo->si_mntflags, PLINK))
30694 +               au_plink_put(sb, /*verbose*/1);
30695 +       au_xino_clr(sb);
30696 +       if (root)
30697 +               au_dr_opt_flush(sb);
30698 +
30699 +       if (root)
30700 +               aufs_write_unlock(root);
30701 +       else
30702 +               __si_write_unlock(sb);
30703 +
30704 +       sbinfo->si_sb = NULL;
30705 +       au_nwt_flush(&sbinfo->si_nowait);
30706 +
30707 +out:
30708 +       kill_anon_super(sb);
30709 +}
30710 +
30711 +struct file_system_type aufs_fs_type = {
30712 +       .name           = AUFS_FSTYPE,
30713 +       /* a race between rename and others */
30714 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30715 +                               /* untested */
30716 +                               /*| FS_ALLOW_IDMAP*/
30717 +                               ,
30718 +       .init_fs_context = aufs_fsctx_init,
30719 +       .parameters     = aufs_fsctx_paramspec,
30720 +       .kill_sb        = aufs_kill_sb,
30721 +       /* no need to __module_get() and module_put(). */
30722 +       .owner          = THIS_MODULE,
30723 +};
30724 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30725 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30726 +++ linux/fs/aufs/super.h       2022-11-05 23:02:18.969222617 +0100
30727 @@ -0,0 +1,592 @@
30728 +/* SPDX-License-Identifier: GPL-2.0 */
30729 +/*
30730 + * Copyright (C) 2005-2022 Junjiro R. Okajima
30731 + *
30732 + * This program is free software; you can redistribute it and/or modify
30733 + * it under the terms of the GNU General Public License as published by
30734 + * the Free Software Foundation; either version 2 of the License, or
30735 + * (at your option) any later version.
30736 + *
30737 + * This program is distributed in the hope that it will be useful,
30738 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30739 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30740 + * GNU General Public License for more details.
30741 + *
30742 + * You should have received a copy of the GNU General Public License
30743 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30744 + */
30745 +
30746 +/*
30747 + * super_block operations
30748 + */
30749 +
30750 +#ifndef __AUFS_SUPER_H__
30751 +#define __AUFS_SUPER_H__
30752 +
30753 +#ifdef __KERNEL__
30754 +
30755 +#include <linux/fs.h>
30756 +#include <linux/kobject.h>
30757 +#include "hbl.h"
30758 +#include "lcnt.h"
30759 +#include "rwsem.h"
30760 +#include "wkq.h"
30761 +
30762 +/* policies to select one among multiple writable branches */
30763 +struct au_wbr_copyup_operations {
30764 +       int (*copyup)(struct dentry *dentry);
30765 +};
30766 +
30767 +#define AuWbr_DIR      1               /* target is a dir */
30768 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30769 +
30770 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30771 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30772 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30773 +
30774 +struct au_wbr_create_operations {
30775 +       int (*create)(struct dentry *dentry, unsigned int flags);
30776 +       int (*init)(struct super_block *sb);
30777 +       int (*fin)(struct super_block *sb);
30778 +};
30779 +
30780 +struct au_wbr_mfs {
30781 +       struct mutex    mfs_lock; /* protect this structure */
30782 +       unsigned long   mfs_jiffy;
30783 +       unsigned long   mfs_expire;
30784 +       aufs_bindex_t   mfs_bindex;
30785 +
30786 +       unsigned long long      mfsrr_bytes;
30787 +       unsigned long long      mfsrr_watermark;
30788 +};
30789 +
30790 +#define AuPlink_NHASH 100
30791 +static inline int au_plink_hash(ino_t ino)
30792 +{
30793 +       return ino % AuPlink_NHASH;
30794 +}
30795 +
30796 +/* File-based Hierarchical Storage Management */
30797 +struct au_fhsm {
30798 +#ifdef CONFIG_AUFS_FHSM
30799 +       /* allow only one process who can receive the notification */
30800 +       spinlock_t              fhsm_spin;
30801 +       pid_t                   fhsm_pid;
30802 +       wait_queue_head_t       fhsm_wqh;
30803 +       atomic_t                fhsm_readable;
30804 +
30805 +       /* these are protected by si_rwsem */
30806 +       unsigned long           fhsm_expire;
30807 +       aufs_bindex_t           fhsm_bottom;
30808 +#endif
30809 +};
30810 +
30811 +struct au_branch;
30812 +struct au_sbinfo {
30813 +       /* nowait tasks in the system-wide workqueue */
30814 +       struct au_nowait_tasks  si_nowait;
30815 +
30816 +       /*
30817 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30818 +        * rwsem for au_sbinfo is necessary.
30819 +        */
30820 +       struct au_rwsem         si_rwsem;
30821 +
30822 +       /*
30823 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30824 +        * remount.
30825 +        */
30826 +       au_lcnt_t               si_ninodes, si_nfiles;
30827 +
30828 +       /* branch management */
30829 +       unsigned int            si_generation;
30830 +
30831 +       /* see AuSi_ flags */
30832 +       unsigned char           au_si_status;
30833 +
30834 +       aufs_bindex_t           si_bbot;
30835 +
30836 +       /* dirty trick to keep br_id plus */
30837 +       unsigned int            si_last_br_id :
30838 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30839 +       struct au_branch        **si_branch;
30840 +
30841 +       /* policy to select a writable branch */
30842 +       unsigned char           si_wbr_copyup;
30843 +       unsigned char           si_wbr_create;
30844 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30845 +       struct au_wbr_create_operations *si_wbr_create_ops;
30846 +
30847 +       /* round robin */
30848 +       atomic_t                si_wbr_rr_next;
30849 +
30850 +       /* most free space */
30851 +       struct au_wbr_mfs       si_wbr_mfs;
30852 +
30853 +       /* File-based Hierarchical Storage Management */
30854 +       struct au_fhsm          si_fhsm;
30855 +
30856 +       /* mount flags */
30857 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30858 +       unsigned int            si_mntflags;
30859 +
30860 +       /* external inode number (bitmap and translation table) */
30861 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30862 +
30863 +       struct file             *si_xib;
30864 +       struct mutex            si_xib_mtx; /* protect xib members */
30865 +       unsigned long           *si_xib_buf;
30866 +       unsigned long           si_xib_last_pindex;
30867 +       int                     si_xib_next_bit;
30868 +
30869 +       unsigned long           si_xino_jiffy;
30870 +       unsigned long           si_xino_expire;
30871 +       /* reserved for future use */
30872 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30873 +
30874 +#ifdef CONFIG_AUFS_EXPORT
30875 +       /* i_generation */
30876 +       /* todo: make xigen file an array to support many inode numbers */
30877 +       struct file             *si_xigen;
30878 +       atomic_t                si_xigen_next;
30879 +#endif
30880 +
30881 +       /* dirty trick to support atomic_open */
30882 +       struct hlist_bl_head    si_aopen;
30883 +
30884 +       /* vdir parameters */
30885 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30886 +       unsigned int            si_rdblk;       /* deblk size */
30887 +       unsigned int            si_rdhash;      /* hash size */
30888 +
30889 +       /*
30890 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30891 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30892 +        * future fsck.aufs or kernel thread will remove them later.
30893 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30894 +        */
30895 +       unsigned int            si_dirwh;
30896 +
30897 +       /* pseudo_link list */
30898 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30899 +       wait_queue_head_t       si_plink_wq;
30900 +       spinlock_t              si_plink_maint_lock;
30901 +       pid_t                   si_plink_maint_pid;
30902 +
30903 +       /* file list */
30904 +       struct hlist_bl_head    si_files;
30905 +
30906 +       /* with/without getattr, brother of sb->s_d_op */
30907 +       const struct inode_operations *si_iop_array;
30908 +
30909 +       /*
30910 +        * sysfs and lifetime management.
30911 +        * this is not a small structure and it may be a waste of memory in case
30912 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30913 +        * but using sysfs is majority.
30914 +        */
30915 +       struct kobject          si_kobj;
30916 +#ifdef CONFIG_DEBUG_FS
30917 +       struct dentry            *si_dbgaufs;
30918 +       struct dentry            *si_dbgaufs_plink;
30919 +       struct dentry            *si_dbgaufs_xib;
30920 +#ifdef CONFIG_AUFS_EXPORT
30921 +       struct dentry            *si_dbgaufs_xigen;
30922 +#endif
30923 +#endif
30924 +
30925 +#ifdef CONFIG_AUFS_SBILIST
30926 +       struct hlist_bl_node    si_list;
30927 +#endif
30928 +
30929 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30930 +       struct super_block      *si_sb;
30931 +};
30932 +
30933 +/* sbinfo status flags */
30934 +/*
30935 + * set true when refresh_dirs() failed at remount time.
30936 + * then try refreshing dirs at access time again.
30937 + * if it is false, refreshing dirs at access time is unnecessary
30938 + */
30939 +#define AuSi_FAILED_REFRESH_DIR        1
30940 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30941 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30942 +
30943 +#ifndef CONFIG_AUFS_FHSM
30944 +#undef AuSi_FHSM
30945 +#define AuSi_FHSM              0
30946 +#endif
30947 +
30948 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30949 +                                          unsigned int flag)
30950 +{
30951 +       AuRwMustAnyLock(&sbi->si_rwsem);
30952 +       return sbi->au_si_status & flag;
30953 +}
30954 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30955 +#define au_fset_si(sbinfo, name) do { \
30956 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30957 +       (sbinfo)->au_si_status |= AuSi_##name; \
30958 +} while (0)
30959 +#define au_fclr_si(sbinfo, name) do { \
30960 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30961 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30962 +} while (0)
30963 +
30964 +/* ---------------------------------------------------------------------- */
30965 +
30966 +/* policy to select one among writable branches */
30967 +#define AuWbrCopyup(sbinfo, ...) \
30968 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30969 +#define AuWbrCreate(sbinfo, ...) \
30970 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30971 +
30972 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30973 +#define AuLock_DW              1               /* write-lock dentry */
30974 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30975 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30976 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30977 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30978 +                                               /* except RENAME_EXCHANGE */
30979 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30980 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30981 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30982 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30983 +#define au_fset_lock(flags, name) \
30984 +       do { (flags) |= AuLock_##name; } while (0)
30985 +#define au_fclr_lock(flags, name) \
30986 +       do { (flags) &= ~AuLock_##name; } while (0)
30987 +
30988 +/* ---------------------------------------------------------------------- */
30989 +
30990 +/* super.c */
30991 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30992 +
30993 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30994 +                                          unsigned long long max, void *arg);
30995 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30996 +                    struct super_block *sb, void *arg);
30997 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30998 +void au_iarray_free(struct inode **a, unsigned long long max);
30999 +
31000 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
31001 +extern const struct super_operations aufs_sop;
31002 +int au_alloc_root(struct super_block *sb);
31003 +extern struct file_system_type aufs_fs_type;
31004 +
31005 +/* sbinfo.c */
31006 +void au_si_free(struct kobject *kobj);
31007 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
31008 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
31009 +
31010 +unsigned int au_sigen_inc(struct super_block *sb);
31011 +aufs_bindex_t au_new_br_id(struct super_block *sb);
31012 +
31013 +int si_read_lock(struct super_block *sb, int flags);
31014 +int si_write_lock(struct super_block *sb, int flags);
31015 +int aufs_read_lock(struct dentry *dentry, int flags);
31016 +void aufs_read_unlock(struct dentry *dentry, int flags);
31017 +void aufs_write_lock(struct dentry *dentry);
31018 +void aufs_write_unlock(struct dentry *dentry);
31019 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
31020 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
31021 +
31022 +/* wbr_policy.c */
31023 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
31024 +extern struct au_wbr_create_operations au_wbr_create_ops[];
31025 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
31026 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
31027 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
31028 +
31029 +/* mvdown.c */
31030 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
31031 +
31032 +#ifdef CONFIG_AUFS_FHSM
31033 +/* fhsm.c */
31034 +
31035 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
31036 +{
31037 +       pid_t pid;
31038 +
31039 +       spin_lock(&fhsm->fhsm_spin);
31040 +       pid = fhsm->fhsm_pid;
31041 +       spin_unlock(&fhsm->fhsm_spin);
31042 +
31043 +       return pid;
31044 +}
31045 +
31046 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
31047 +void au_fhsm_wrote_all(struct super_block *sb, int force);
31048 +int au_fhsm_fd(struct super_block *sb, int oflags);
31049 +int au_fhsm_br_alloc(struct au_branch *br);
31050 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
31051 +void au_fhsm_fin(struct super_block *sb);
31052 +void au_fhsm_init(struct au_sbinfo *sbinfo);
31053 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
31054 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
31055 +#else
31056 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
31057 +          int force)
31058 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
31059 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
31060 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
31061 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
31062 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
31063 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
31064 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
31065 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
31066 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31067 +#endif
31068 +
31069 +/* ---------------------------------------------------------------------- */
31070 +
31071 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31072 +{
31073 +       return sb->s_fs_info;
31074 +}
31075 +
31076 +/* ---------------------------------------------------------------------- */
31077 +
31078 +#ifdef CONFIG_AUFS_EXPORT
31079 +int au_test_nfsd(void);
31080 +void au_export_init(struct super_block *sb);
31081 +void au_xigen_inc(struct inode *inode);
31082 +int au_xigen_new(struct inode *inode);
31083 +int au_xigen_set(struct super_block *sb, struct path *path);
31084 +void au_xigen_clr(struct super_block *sb);
31085 +
31086 +static inline int au_busy_or_stale(void)
31087 +{
31088 +       if (!au_test_nfsd())
31089 +               return -EBUSY;
31090 +       return -ESTALE;
31091 +}
31092 +#else
31093 +AuStubInt0(au_test_nfsd, void)
31094 +AuStubVoid(au_export_init, struct super_block *sb)
31095 +AuStubVoid(au_xigen_inc, struct inode *inode)
31096 +AuStubInt0(au_xigen_new, struct inode *inode)
31097 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
31098 +AuStubVoid(au_xigen_clr, struct super_block *sb)
31099 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
31100 +#endif /* CONFIG_AUFS_EXPORT */
31101 +
31102 +/* ---------------------------------------------------------------------- */
31103 +
31104 +#ifdef CONFIG_AUFS_SBILIST
31105 +/* module.c */
31106 +extern struct hlist_bl_head au_sbilist;
31107 +
31108 +static inline void au_sbilist_init(void)
31109 +{
31110 +       INIT_HLIST_BL_HEAD(&au_sbilist);
31111 +}
31112 +
31113 +static inline void au_sbilist_add(struct super_block *sb)
31114 +{
31115 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
31116 +}
31117 +
31118 +static inline void au_sbilist_del(struct super_block *sb)
31119 +{
31120 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
31121 +}
31122 +
31123 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31124 +static inline void au_sbilist_lock(void)
31125 +{
31126 +       hlist_bl_lock(&au_sbilist);
31127 +}
31128 +
31129 +static inline void au_sbilist_unlock(void)
31130 +{
31131 +       hlist_bl_unlock(&au_sbilist);
31132 +}
31133 +#define AuGFP_SBILIST  GFP_ATOMIC
31134 +#else
31135 +AuStubVoid(au_sbilist_lock, void)
31136 +AuStubVoid(au_sbilist_unlock, void)
31137 +#define AuGFP_SBILIST  GFP_NOFS
31138 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
31139 +#else
31140 +AuStubVoid(au_sbilist_init, void)
31141 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31142 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31143 +AuStubVoid(au_sbilist_lock, void)
31144 +AuStubVoid(au_sbilist_unlock, void)
31145 +#define AuGFP_SBILIST  GFP_NOFS
31146 +#endif
31147 +
31148 +/* ---------------------------------------------------------------------- */
31149 +
31150 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31151 +{
31152 +       /*
31153 +        * This function is a dynamic '__init' function actually,
31154 +        * so the tiny check for si_rwsem is unnecessary.
31155 +        */
31156 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31157 +#ifdef CONFIG_DEBUG_FS
31158 +       sbinfo->si_dbgaufs = NULL;
31159 +       sbinfo->si_dbgaufs_plink = NULL;
31160 +       sbinfo->si_dbgaufs_xib = NULL;
31161 +#ifdef CONFIG_AUFS_EXPORT
31162 +       sbinfo->si_dbgaufs_xigen = NULL;
31163 +#endif
31164 +#endif
31165 +}
31166 +
31167 +/* ---------------------------------------------------------------------- */
31168 +
31169 +/* current->atomic_flags */
31170 +/* this value should never corrupt the ones defined in linux/sched.h */
31171 +#define PFA_AUFS       0x10
31172 +
31173 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31174 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31175 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31176 +
31177 +static inline int si_pid_test(struct super_block *sb)
31178 +{
31179 +       return !!task_test_aufs(current);
31180 +}
31181 +
31182 +static inline void si_pid_clr(struct super_block *sb)
31183 +{
31184 +       AuDebugOn(!task_test_aufs(current));
31185 +       task_clear_aufs(current);
31186 +}
31187 +
31188 +static inline void si_pid_set(struct super_block *sb)
31189 +{
31190 +       AuDebugOn(task_test_aufs(current));
31191 +       task_set_aufs(current);
31192 +}
31193 +
31194 +/* ---------------------------------------------------------------------- */
31195 +
31196 +/* lock superblock. mainly for entry point functions */
31197 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31198 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31199 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31200 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31201 +/*
31202 +#define __si_read_trylock_nested(sb) \
31203 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31204 +#define __si_write_trylock_nested(sb) \
31205 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31206 +*/
31207 +
31208 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31209 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31210 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31211 +
31212 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31213 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31214 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31215 +
31216 +static inline void si_noflush_read_lock(struct super_block *sb)
31217 +{
31218 +       __si_read_lock(sb);
31219 +       si_pid_set(sb);
31220 +}
31221 +
31222 +static inline int si_noflush_read_trylock(struct super_block *sb)
31223 +{
31224 +       int locked;
31225 +
31226 +       locked = __si_read_trylock(sb);
31227 +       if (locked)
31228 +               si_pid_set(sb);
31229 +       return locked;
31230 +}
31231 +
31232 +static inline void si_noflush_write_lock(struct super_block *sb)
31233 +{
31234 +       __si_write_lock(sb);
31235 +       si_pid_set(sb);
31236 +}
31237 +
31238 +static inline int si_noflush_write_trylock(struct super_block *sb)
31239 +{
31240 +       int locked;
31241 +
31242 +       locked = __si_write_trylock(sb);
31243 +       if (locked)
31244 +               si_pid_set(sb);
31245 +       return locked;
31246 +}
31247 +
31248 +#if 0 /* reserved */
31249 +static inline int si_read_trylock(struct super_block *sb, int flags)
31250 +{
31251 +       if (au_ftest_lock(flags, FLUSH))
31252 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31253 +       return si_noflush_read_trylock(sb);
31254 +}
31255 +#endif
31256 +
31257 +static inline void si_read_unlock(struct super_block *sb)
31258 +{
31259 +       si_pid_clr(sb);
31260 +       __si_read_unlock(sb);
31261 +}
31262 +
31263 +#if 0 /* reserved */
31264 +static inline int si_write_trylock(struct super_block *sb, int flags)
31265 +{
31266 +       if (au_ftest_lock(flags, FLUSH))
31267 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31268 +       return si_noflush_write_trylock(sb);
31269 +}
31270 +#endif
31271 +
31272 +static inline void si_write_unlock(struct super_block *sb)
31273 +{
31274 +       si_pid_clr(sb);
31275 +       __si_write_unlock(sb);
31276 +}
31277 +
31278 +#if 0 /* reserved */
31279 +static inline void si_downgrade_lock(struct super_block *sb)
31280 +{
31281 +       __si_downgrade_lock(sb);
31282 +}
31283 +#endif
31284 +
31285 +/* ---------------------------------------------------------------------- */
31286 +
31287 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31288 +{
31289 +       SiMustAnyLock(sb);
31290 +       return au_sbi(sb)->si_bbot;
31291 +}
31292 +
31293 +static inline unsigned int au_mntflags(struct super_block *sb)
31294 +{
31295 +       SiMustAnyLock(sb);
31296 +       return au_sbi(sb)->si_mntflags;
31297 +}
31298 +
31299 +static inline unsigned int au_sigen(struct super_block *sb)
31300 +{
31301 +       SiMustAnyLock(sb);
31302 +       return au_sbi(sb)->si_generation;
31303 +}
31304 +
31305 +static inline struct au_branch *au_sbr(struct super_block *sb,
31306 +                                      aufs_bindex_t bindex)
31307 +{
31308 +       SiMustAnyLock(sb);
31309 +       return au_sbi(sb)->si_branch[0 + bindex];
31310 +}
31311 +
31312 +static inline loff_t au_xi_maxent(struct super_block *sb)
31313 +{
31314 +       SiMustAnyLock(sb);
31315 +       return au_sbi(sb)->si_ximaxent;
31316 +}
31317 +
31318 +#endif /* __KERNEL__ */
31319 +#endif /* __AUFS_SUPER_H__ */
31320 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31321 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31322 +++ linux/fs/aufs/sysaufs.c     2022-11-05 23:02:18.969222617 +0100
31323 @@ -0,0 +1,94 @@
31324 +// SPDX-License-Identifier: GPL-2.0
31325 +/*
31326 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31327 + *
31328 + * This program is free software; you can redistribute it and/or modify
31329 + * it under the terms of the GNU General Public License as published by
31330 + * the Free Software Foundation; either version 2 of the License, or
31331 + * (at your option) any later version.
31332 + *
31333 + * This program is distributed in the hope that it will be useful,
31334 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31335 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31336 + * GNU General Public License for more details.
31337 + *
31338 + * You should have received a copy of the GNU General Public License
31339 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31340 + */
31341 +
31342 +/*
31343 + * sysfs interface and lifetime management
31344 + * they are necessary regardless sysfs is disabled.
31345 + */
31346 +
31347 +#include <linux/random.h>
31348 +#include "aufs.h"
31349 +
31350 +unsigned long sysaufs_si_mask;
31351 +struct kset *sysaufs_kset;
31352 +
31353 +#define AuSiAttr(_name) { \
31354 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31355 +       .show   = sysaufs_si_##_name,                           \
31356 +}
31357 +
31358 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31359 +struct attribute *sysaufs_si_attrs[] = {
31360 +       &sysaufs_si_attr_xi_path.attr,
31361 +       NULL,
31362 +};
31363 +ATTRIBUTE_GROUPS(sysaufs_si);
31364 +
31365 +static const struct sysfs_ops au_sbi_ops = {
31366 +       .show   = sysaufs_si_show
31367 +};
31368 +
31369 +static struct kobj_type au_sbi_ktype = {
31370 +       .release        = au_si_free,
31371 +       .sysfs_ops      = &au_sbi_ops,
31372 +       .default_groups = sysaufs_si_groups
31373 +};
31374 +
31375 +/* ---------------------------------------------------------------------- */
31376 +
31377 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31378 +{
31379 +       int err;
31380 +
31381 +       sbinfo->si_kobj.kset = sysaufs_kset;
31382 +       /* cf. sysaufs_name() */
31383 +       err = kobject_init_and_add
31384 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31385 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31386 +
31387 +       return err;
31388 +}
31389 +
31390 +void sysaufs_fin(void)
31391 +{
31392 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31393 +       kset_unregister(sysaufs_kset);
31394 +}
31395 +
31396 +int __init sysaufs_init(void)
31397 +{
31398 +       int err;
31399 +
31400 +       do {
31401 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31402 +       } while (!sysaufs_si_mask);
31403 +
31404 +       err = -EINVAL;
31405 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31406 +       if (unlikely(!sysaufs_kset))
31407 +               goto out;
31408 +       err = PTR_ERR(sysaufs_kset);
31409 +       if (IS_ERR(sysaufs_kset))
31410 +               goto out;
31411 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31412 +       if (unlikely(err))
31413 +               kset_unregister(sysaufs_kset);
31414 +
31415 +out:
31416 +       return err;
31417 +}
31418 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31419 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31420 +++ linux/fs/aufs/sysaufs.h     2022-11-05 23:02:18.969222617 +0100
31421 @@ -0,0 +1,102 @@
31422 +/* SPDX-License-Identifier: GPL-2.0 */
31423 +/*
31424 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31425 + *
31426 + * This program is free software; you can redistribute it and/or modify
31427 + * it under the terms of the GNU General Public License as published by
31428 + * the Free Software Foundation; either version 2 of the License, or
31429 + * (at your option) any later version.
31430 + *
31431 + * This program is distributed in the hope that it will be useful,
31432 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31433 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31434 + * GNU General Public License for more details.
31435 + *
31436 + * You should have received a copy of the GNU General Public License
31437 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31438 + */
31439 +
31440 +/*
31441 + * sysfs interface and mount lifetime management
31442 + */
31443 +
31444 +#ifndef __SYSAUFS_H__
31445 +#define __SYSAUFS_H__
31446 +
31447 +#ifdef __KERNEL__
31448 +
31449 +#include <linux/sysfs.h>
31450 +#include "module.h"
31451 +
31452 +struct super_block;
31453 +struct au_sbinfo;
31454 +
31455 +struct sysaufs_si_attr {
31456 +       struct attribute attr;
31457 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31458 +};
31459 +
31460 +/* ---------------------------------------------------------------------- */
31461 +
31462 +/* sysaufs.c */
31463 +extern unsigned long sysaufs_si_mask;
31464 +extern struct kset *sysaufs_kset;
31465 +extern struct attribute *sysaufs_si_attrs[];
31466 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31467 +int __init sysaufs_init(void);
31468 +void sysaufs_fin(void);
31469 +
31470 +/* ---------------------------------------------------------------------- */
31471 +
31472 +/* some people doesn't like to show a pointer in kernel */
31473 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31474 +{
31475 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31476 +}
31477 +
31478 +#define SysaufsSiNamePrefix    "si_"
31479 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31480 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31481 +{
31482 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31483 +                sysaufs_si_id(sbinfo));
31484 +}
31485 +
31486 +struct au_branch;
31487 +#ifdef CONFIG_SYSFS
31488 +/* sysfs.c */
31489 +extern struct attribute_group *sysaufs_attr_group;
31490 +
31491 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31492 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31493 +                        char *buf);
31494 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31495 +#ifdef CONFIG_COMPAT
31496 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31497 +#endif
31498 +
31499 +void sysaufs_br_init(struct au_branch *br);
31500 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31501 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31502 +
31503 +#define sysaufs_brs_init()     do {} while (0)
31504 +
31505 +#else
31506 +#define sysaufs_attr_group     NULL
31507 +
31508 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31509 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31510 +       struct attribute *attr, char *buf)
31511 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31512 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31513 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31514 +
31515 +static inline void sysaufs_brs_init(void)
31516 +{
31517 +       sysaufs_brs = 0;
31518 +}
31519 +
31520 +#endif /* CONFIG_SYSFS */
31521 +
31522 +#endif /* __KERNEL__ */
31523 +#endif /* __SYSAUFS_H__ */
31524 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31525 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31526 +++ linux/fs/aufs/sysfs.c       2022-11-05 23:02:18.969222617 +0100
31527 @@ -0,0 +1,374 @@
31528 +// SPDX-License-Identifier: GPL-2.0
31529 +/*
31530 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31531 + *
31532 + * This program is free software; you can redistribute it and/or modify
31533 + * it under the terms of the GNU General Public License as published by
31534 + * the Free Software Foundation; either version 2 of the License, or
31535 + * (at your option) any later version.
31536 + *
31537 + * This program is distributed in the hope that it will be useful,
31538 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31539 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31540 + * GNU General Public License for more details.
31541 + *
31542 + * You should have received a copy of the GNU General Public License
31543 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31544 + */
31545 +
31546 +/*
31547 + * sysfs interface
31548 + */
31549 +
31550 +#include <linux/compat.h>
31551 +#include <linux/seq_file.h>
31552 +#include "aufs.h"
31553 +
31554 +#ifdef CONFIG_AUFS_FS_MODULE
31555 +/* this entry violates the "one line per file" policy of sysfs */
31556 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31557 +                          char *buf)
31558 +{
31559 +       ssize_t err;
31560 +       static char *conf =
31561 +/* this file is generated at compiling */
31562 +#include "conf.str"
31563 +               ;
31564 +
31565 +       err = snprintf(buf, PAGE_SIZE, conf);
31566 +       if (unlikely(err >= PAGE_SIZE))
31567 +               err = -EFBIG;
31568 +       return err;
31569 +}
31570 +
31571 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31572 +#endif
31573 +
31574 +static struct attribute *au_attr[] = {
31575 +#ifdef CONFIG_AUFS_FS_MODULE
31576 +       &au_config_attr.attr,
31577 +#endif
31578 +       NULL,   /* need to NULL terminate the list of attributes */
31579 +};
31580 +
31581 +static struct attribute_group sysaufs_attr_group_body = {
31582 +       .attrs = au_attr
31583 +};
31584 +
31585 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31586 +
31587 +/* ---------------------------------------------------------------------- */
31588 +
31589 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31590 +{
31591 +       int err;
31592 +
31593 +       SiMustAnyLock(sb);
31594 +
31595 +       err = 0;
31596 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31597 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31598 +               seq_putc(seq, '\n');
31599 +       }
31600 +       return err;
31601 +}
31602 +
31603 +/*
31604 + * the lifetime of branch is independent from the entry under sysfs.
31605 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31606 + * unlinked.
31607 + */
31608 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31609 +                        aufs_bindex_t bindex, int idx)
31610 +{
31611 +       int err;
31612 +       struct path path;
31613 +       struct dentry *root;
31614 +       struct au_branch *br;
31615 +       au_br_perm_str_t perm;
31616 +
31617 +       AuDbg("b%d\n", bindex);
31618 +
31619 +       err = 0;
31620 +       root = sb->s_root;
31621 +       di_read_lock_parent(root, !AuLock_IR);
31622 +       br = au_sbr(sb, bindex);
31623 +
31624 +       switch (idx) {
31625 +       case AuBrSysfs_BR:
31626 +               path.mnt = au_br_mnt(br);
31627 +               path.dentry = au_h_dptr(root, bindex);
31628 +               err = au_seq_path(seq, &path);
31629 +               if (!err) {
31630 +                       au_optstr_br_perm(&perm, br->br_perm);
31631 +                       seq_printf(seq, "=%s\n", perm.a);
31632 +               }
31633 +               break;
31634 +       case AuBrSysfs_BRID:
31635 +               seq_printf(seq, "%d\n", br->br_id);
31636 +               break;
31637 +       }
31638 +       di_read_unlock(root, !AuLock_IR);
31639 +       if (unlikely(err || seq_has_overflowed(seq)))
31640 +               err = -E2BIG;
31641 +
31642 +       return err;
31643 +}
31644 +
31645 +/* ---------------------------------------------------------------------- */
31646 +
31647 +static struct seq_file *au_seq(char *p, ssize_t len)
31648 +{
31649 +       struct seq_file *seq;
31650 +
31651 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31652 +       if (seq) {
31653 +               /* mutex_init(&seq.lock); */
31654 +               seq->buf = p;
31655 +               seq->size = len;
31656 +               return seq; /* success */
31657 +       }
31658 +
31659 +       seq = ERR_PTR(-ENOMEM);
31660 +       return seq;
31661 +}
31662 +
31663 +#define SysaufsBr_PREFIX       "br"
31664 +#define SysaufsBrid_PREFIX     "brid"
31665 +
31666 +/* todo: file size may exceed PAGE_SIZE */
31667 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31668 +                       char *buf)
31669 +{
31670 +       ssize_t err;
31671 +       int idx;
31672 +       long l;
31673 +       aufs_bindex_t bbot;
31674 +       struct au_sbinfo *sbinfo;
31675 +       struct super_block *sb;
31676 +       struct seq_file *seq;
31677 +       char *name;
31678 +       struct attribute **cattr;
31679 +
31680 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31681 +       sb = sbinfo->si_sb;
31682 +
31683 +       /*
31684 +        * prevent a race condition between sysfs and aufs.
31685 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31686 +        * prohibits maintaining the sysfs entries.
31687 +        * hew we acquire read lock after sysfs_get_active_two().
31688 +        * on the other hand, the remount process may maintain the sysfs/aufs
31689 +        * entries after acquiring write lock.
31690 +        * it can cause a deadlock.
31691 +        * simply we gave up processing read here.
31692 +        */
31693 +       err = -EBUSY;
31694 +       if (unlikely(!si_noflush_read_trylock(sb)))
31695 +               goto out;
31696 +
31697 +       seq = au_seq(buf, PAGE_SIZE);
31698 +       err = PTR_ERR(seq);
31699 +       if (IS_ERR(seq))
31700 +               goto out_unlock;
31701 +
31702 +       name = (void *)attr->name;
31703 +       cattr = sysaufs_si_attrs;
31704 +       while (*cattr) {
31705 +               if (!strcmp(name, (*cattr)->name)) {
31706 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31707 +                               ->show(seq, sb);
31708 +                       goto out_seq;
31709 +               }
31710 +               cattr++;
31711 +       }
31712 +
31713 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31714 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31715 +               idx = AuBrSysfs_BRID;
31716 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31717 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31718 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31719 +               idx = AuBrSysfs_BR;
31720 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31721 +       } else
31722 +                 BUG();
31723 +
31724 +       err = kstrtol(name, 10, &l);
31725 +       if (!err) {
31726 +               bbot = au_sbbot(sb);
31727 +               if (l <= bbot)
31728 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31729 +               else
31730 +                       err = -ENOENT;
31731 +       }
31732 +
31733 +out_seq:
31734 +       if (!err) {
31735 +               err = seq->count;
31736 +               /* sysfs limit */
31737 +               if (unlikely(err == PAGE_SIZE))
31738 +                       err = -EFBIG;
31739 +       }
31740 +       au_kfree_rcu(seq);
31741 +out_unlock:
31742 +       si_read_unlock(sb);
31743 +out:
31744 +       return err;
31745 +}
31746 +
31747 +/* ---------------------------------------------------------------------- */
31748 +
31749 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31750 +{
31751 +       int err;
31752 +       int16_t brid;
31753 +       aufs_bindex_t bindex, bbot;
31754 +       size_t sz;
31755 +       char *buf;
31756 +       struct seq_file *seq;
31757 +       struct au_branch *br;
31758 +
31759 +       si_read_lock(sb, AuLock_FLUSH);
31760 +       bbot = au_sbbot(sb);
31761 +       err = bbot + 1;
31762 +       if (!arg)
31763 +               goto out;
31764 +
31765 +       err = -ENOMEM;
31766 +       buf = (void *)__get_free_page(GFP_NOFS);
31767 +       if (unlikely(!buf))
31768 +               goto out;
31769 +
31770 +       seq = au_seq(buf, PAGE_SIZE);
31771 +       err = PTR_ERR(seq);
31772 +       if (IS_ERR(seq))
31773 +               goto out_buf;
31774 +
31775 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31776 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31777 +               /* VERIFY_WRITE */
31778 +               err = !access_ok(arg, sizeof(*arg));
31779 +               if (unlikely(err))
31780 +                       break;
31781 +
31782 +               br = au_sbr(sb, bindex);
31783 +               brid = br->br_id;
31784 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31785 +               err = __put_user(brid, &arg->id);
31786 +               if (unlikely(err))
31787 +                       break;
31788 +
31789 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31790 +               err = __put_user(br->br_perm, &arg->perm);
31791 +               if (unlikely(err))
31792 +                       break;
31793 +
31794 +               err = au_seq_path(seq, &br->br_path);
31795 +               if (unlikely(err))
31796 +                       break;
31797 +               seq_putc(seq, '\0');
31798 +               if (!seq_has_overflowed(seq)) {
31799 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31800 +                       seq->count = 0;
31801 +                       if (unlikely(err))
31802 +                               break;
31803 +               } else {
31804 +                       err = -E2BIG;
31805 +                       goto out_seq;
31806 +               }
31807 +       }
31808 +       if (unlikely(err))
31809 +               err = -EFAULT;
31810 +
31811 +out_seq:
31812 +       au_kfree_rcu(seq);
31813 +out_buf:
31814 +       free_page((unsigned long)buf);
31815 +out:
31816 +       si_read_unlock(sb);
31817 +       return err;
31818 +}
31819 +
31820 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31821 +{
31822 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31823 +}
31824 +
31825 +#ifdef CONFIG_COMPAT
31826 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31827 +{
31828 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31829 +}
31830 +#endif
31831 +
31832 +/* ---------------------------------------------------------------------- */
31833 +
31834 +void sysaufs_br_init(struct au_branch *br)
31835 +{
31836 +       int i;
31837 +       struct au_brsysfs *br_sysfs;
31838 +       struct attribute *attr;
31839 +
31840 +       br_sysfs = br->br_sysfs;
31841 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31842 +               attr = &br_sysfs->attr;
31843 +               sysfs_attr_init(attr);
31844 +               attr->name = br_sysfs->name;
31845 +               attr->mode = 0444;
31846 +               br_sysfs++;
31847 +       }
31848 +}
31849 +
31850 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31851 +{
31852 +       struct au_branch *br;
31853 +       struct kobject *kobj;
31854 +       struct au_brsysfs *br_sysfs;
31855 +       int i;
31856 +       aufs_bindex_t bbot;
31857 +
31858 +       if (!sysaufs_brs)
31859 +               return;
31860 +
31861 +       kobj = &au_sbi(sb)->si_kobj;
31862 +       bbot = au_sbbot(sb);
31863 +       for (; bindex <= bbot; bindex++) {
31864 +               br = au_sbr(sb, bindex);
31865 +               br_sysfs = br->br_sysfs;
31866 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31867 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31868 +                       br_sysfs++;
31869 +               }
31870 +       }
31871 +}
31872 +
31873 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31874 +{
31875 +       int err, i;
31876 +       aufs_bindex_t bbot;
31877 +       struct kobject *kobj;
31878 +       struct au_branch *br;
31879 +       struct au_brsysfs *br_sysfs;
31880 +
31881 +       if (!sysaufs_brs)
31882 +               return;
31883 +
31884 +       kobj = &au_sbi(sb)->si_kobj;
31885 +       bbot = au_sbbot(sb);
31886 +       for (; bindex <= bbot; bindex++) {
31887 +               br = au_sbr(sb, bindex);
31888 +               br_sysfs = br->br_sysfs;
31889 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31890 +                        SysaufsBr_PREFIX "%d", bindex);
31891 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31892 +                        SysaufsBrid_PREFIX "%d", bindex);
31893 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31894 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31895 +                       if (unlikely(err))
31896 +                               pr_warn("failed %s under sysfs(%d)\n",
31897 +                                       br_sysfs->name, err);
31898 +                       br_sysfs++;
31899 +               }
31900 +       }
31901 +}
31902 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31903 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31904 +++ linux/fs/aufs/sysrq.c       2022-11-05 23:02:18.969222617 +0100
31905 @@ -0,0 +1,149 @@
31906 +// SPDX-License-Identifier: GPL-2.0
31907 +/*
31908 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31909 + *
31910 + * This program is free software; you can redistribute it and/or modify
31911 + * it under the terms of the GNU General Public License as published by
31912 + * the Free Software Foundation; either version 2 of the License, or
31913 + * (at your option) any later version.
31914 + *
31915 + * This program is distributed in the hope that it will be useful,
31916 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31917 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31918 + * GNU General Public License for more details.
31919 + *
31920 + * You should have received a copy of the GNU General Public License
31921 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31922 + */
31923 +
31924 +/*
31925 + * magic sysrq handler
31926 + */
31927 +
31928 +/* #include <linux/sysrq.h> */
31929 +#include <linux/writeback.h>
31930 +#include "aufs.h"
31931 +
31932 +/* ---------------------------------------------------------------------- */
31933 +
31934 +static void sysrq_sb(struct super_block *sb)
31935 +{
31936 +       char *plevel;
31937 +       struct au_sbinfo *sbinfo;
31938 +       struct file *file;
31939 +       struct hlist_bl_head *files;
31940 +       struct hlist_bl_node *pos;
31941 +       struct au_finfo *finfo;
31942 +       struct inode *i;
31943 +
31944 +       plevel = au_plevel;
31945 +       au_plevel = KERN_WARNING;
31946 +
31947 +       /* since we define pr_fmt, call printk directly */
31948 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31949 +
31950 +       sbinfo = au_sbi(sb);
31951 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31952 +       pr("superblock\n");
31953 +       au_dpri_sb(sb);
31954 +
31955 +#if 0 /* reserved */
31956 +       do {
31957 +               int err, i, j, ndentry;
31958 +               struct au_dcsub_pages dpages;
31959 +               struct au_dpage *dpage;
31960 +
31961 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31962 +               if (unlikely(err))
31963 +                       break;
31964 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31965 +               if (!err)
31966 +                       for (i = 0; i < dpages.ndpage; i++) {
31967 +                               dpage = dpages.dpages + i;
31968 +                               ndentry = dpage->ndentry;
31969 +                               for (j = 0; j < ndentry; j++)
31970 +                                       au_dpri_dentry(dpage->dentries[j]);
31971 +                       }
31972 +               au_dpages_free(&dpages);
31973 +       } while (0);
31974 +#endif
31975 +
31976 +       pr("isolated inode\n");
31977 +       spin_lock(&sb->s_inode_list_lock);
31978 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31979 +               spin_lock(&i->i_lock);
31980 +               if (hlist_empty(&i->i_dentry))
31981 +                       au_dpri_inode(i);
31982 +               spin_unlock(&i->i_lock);
31983 +       }
31984 +       spin_unlock(&sb->s_inode_list_lock);
31985 +
31986 +       pr("files\n");
31987 +       files = &au_sbi(sb)->si_files;
31988 +       hlist_bl_lock(files);
31989 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31990 +               umode_t mode;
31991 +
31992 +               file = finfo->fi_file;
31993 +               mode = file_inode(file)->i_mode;
31994 +               if (!special_file(mode))
31995 +                       au_dpri_file(file);
31996 +       }
31997 +       hlist_bl_unlock(files);
31998 +       pr("done\n");
31999 +
32000 +#undef pr
32001 +       au_plevel = plevel;
32002 +}
32003 +
32004 +/* ---------------------------------------------------------------------- */
32005 +
32006 +/* module parameter */
32007 +static char *aufs_sysrq_key = "a";
32008 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
32009 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
32010 +
32011 +static void au_sysrq(int key __maybe_unused)
32012 +{
32013 +       struct au_sbinfo *sbinfo;
32014 +       struct hlist_bl_node *pos;
32015 +
32016 +       lockdep_off();
32017 +       au_sbilist_lock();
32018 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
32019 +               sysrq_sb(sbinfo->si_sb);
32020 +       au_sbilist_unlock();
32021 +       lockdep_on();
32022 +}
32023 +
32024 +static struct sysrq_key_op au_sysrq_op = {
32025 +       .handler        = au_sysrq,
32026 +       .help_msg       = "Aufs",
32027 +       .action_msg     = "Aufs",
32028 +       .enable_mask    = SYSRQ_ENABLE_DUMP
32029 +};
32030 +
32031 +/* ---------------------------------------------------------------------- */
32032 +
32033 +int __init au_sysrq_init(void)
32034 +{
32035 +       int err;
32036 +       char key;
32037 +
32038 +       err = -1;
32039 +       key = *aufs_sysrq_key;
32040 +       if ('a' <= key && key <= 'z')
32041 +               err = register_sysrq_key(key, &au_sysrq_op);
32042 +       if (unlikely(err))
32043 +               pr_err("err %d, sysrq=%c\n", err, key);
32044 +       return err;
32045 +}
32046 +
32047 +void au_sysrq_fin(void)
32048 +{
32049 +       int err;
32050 +
32051 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
32052 +       if (unlikely(err))
32053 +               pr_err("err %d (ignored)\n", err);
32054 +}
32055 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
32056 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
32057 +++ linux/fs/aufs/vdir.c        2022-12-17 09:21:34.799855195 +0100
32058 @@ -0,0 +1,896 @@
32059 +// SPDX-License-Identifier: GPL-2.0
32060 +/*
32061 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32062 + *
32063 + * This program is free software; you can redistribute it and/or modify
32064 + * it under the terms of the GNU General Public License as published by
32065 + * the Free Software Foundation; either version 2 of the License, or
32066 + * (at your option) any later version.
32067 + *
32068 + * This program is distributed in the hope that it will be useful,
32069 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32070 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32071 + * GNU General Public License for more details.
32072 + *
32073 + * You should have received a copy of the GNU General Public License
32074 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32075 + */
32076 +
32077 +/*
32078 + * virtual or vertical directory
32079 + */
32080 +
32081 +#include <linux/iversion.h>
32082 +#include "aufs.h"
32083 +
32084 +static unsigned int calc_size(int nlen)
32085 +{
32086 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
32087 +}
32088 +
32089 +static int set_deblk_end(union au_vdir_deblk_p *p,
32090 +                        union au_vdir_deblk_p *deblk_end)
32091 +{
32092 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32093 +               p->de->de_str.len = 0;
32094 +               /* smp_mb(); */
32095 +               return 0;
32096 +       }
32097 +       return -1; /* error */
32098 +}
32099 +
32100 +/* returns true or false */
32101 +static int is_deblk_end(union au_vdir_deblk_p *p,
32102 +                       union au_vdir_deblk_p *deblk_end)
32103 +{
32104 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
32105 +               return !p->de->de_str.len;
32106 +       return 1;
32107 +}
32108 +
32109 +static unsigned char *last_deblk(struct au_vdir *vdir)
32110 +{
32111 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
32112 +}
32113 +
32114 +/* ---------------------------------------------------------------------- */
32115 +
32116 +/* estimate the appropriate size for name hash table */
32117 +unsigned int au_rdhash_est(loff_t sz)
32118 +{
32119 +       unsigned int n;
32120 +
32121 +       n = UINT_MAX;
32122 +       sz >>= 10;
32123 +       if (sz < n)
32124 +               n = sz;
32125 +       if (sz < AUFS_RDHASH_DEF)
32126 +               n = AUFS_RDHASH_DEF;
32127 +       /* pr_info("n %u\n", n); */
32128 +       return n;
32129 +}
32130 +
32131 +/*
32132 + * the allocated memory has to be freed by
32133 + * au_nhash_wh_free() or au_nhash_de_free().
32134 + */
32135 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32136 +{
32137 +       struct hlist_head *head;
32138 +       unsigned int u;
32139 +       size_t sz;
32140 +
32141 +       sz = sizeof(*nhash->nh_head) * num_hash;
32142 +       head = kmalloc(sz, gfp);
32143 +       if (head) {
32144 +               nhash->nh_num = num_hash;
32145 +               nhash->nh_head = head;
32146 +               for (u = 0; u < num_hash; u++)
32147 +                       INIT_HLIST_HEAD(head++);
32148 +               return 0; /* success */
32149 +       }
32150 +
32151 +       return -ENOMEM;
32152 +}
32153 +
32154 +static void nhash_count(struct hlist_head *head)
32155 +{
32156 +#if 0 /* debugging */
32157 +       unsigned long n;
32158 +       struct hlist_node *pos;
32159 +
32160 +       n = 0;
32161 +       hlist_for_each(pos, head)
32162 +               n++;
32163 +       pr_info("%lu\n", n);
32164 +#endif
32165 +}
32166 +
32167 +static void au_nhash_wh_do_free(struct hlist_head *head)
32168 +{
32169 +       struct au_vdir_wh *pos;
32170 +       struct hlist_node *node;
32171 +
32172 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32173 +               au_kfree_rcu(pos);
32174 +}
32175 +
32176 +static void au_nhash_de_do_free(struct hlist_head *head)
32177 +{
32178 +       struct au_vdir_dehstr *pos;
32179 +       struct hlist_node *node;
32180 +
32181 +       hlist_for_each_entry_safe(pos, node, head, hash)
32182 +               au_cache_free_vdir_dehstr(pos);
32183 +}
32184 +
32185 +static void au_nhash_do_free(struct au_nhash *nhash,
32186 +                            void (*free)(struct hlist_head *head))
32187 +{
32188 +       unsigned int n;
32189 +       struct hlist_head *head;
32190 +
32191 +       n = nhash->nh_num;
32192 +       if (!n)
32193 +               return;
32194 +
32195 +       head = nhash->nh_head;
32196 +       while (n-- > 0) {
32197 +               nhash_count(head);
32198 +               free(head++);
32199 +       }
32200 +       au_kfree_try_rcu(nhash->nh_head);
32201 +}
32202 +
32203 +void au_nhash_wh_free(struct au_nhash *whlist)
32204 +{
32205 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32206 +}
32207 +
32208 +static void au_nhash_de_free(struct au_nhash *delist)
32209 +{
32210 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32211 +}
32212 +
32213 +/* ---------------------------------------------------------------------- */
32214 +
32215 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32216 +                           int limit)
32217 +{
32218 +       int num;
32219 +       unsigned int u, n;
32220 +       struct hlist_head *head;
32221 +       struct au_vdir_wh *pos;
32222 +
32223 +       num = 0;
32224 +       n = whlist->nh_num;
32225 +       head = whlist->nh_head;
32226 +       for (u = 0; u < n; u++, head++)
32227 +               hlist_for_each_entry(pos, head, wh_hash)
32228 +                       if (pos->wh_bindex == btgt && ++num > limit)
32229 +                               return 1;
32230 +       return 0;
32231 +}
32232 +
32233 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32234 +                                      unsigned char *name,
32235 +                                      unsigned int len)
32236 +{
32237 +       unsigned int v;
32238 +       /* const unsigned int magic_bit = 12; */
32239 +
32240 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32241 +
32242 +       v = 0;
32243 +       if (len > 8)
32244 +               len = 8;
32245 +       while (len--)
32246 +               v += *name++;
32247 +       /* v = hash_long(v, magic_bit); */
32248 +       v %= nhash->nh_num;
32249 +       return nhash->nh_head + v;
32250 +}
32251 +
32252 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32253 +                             int nlen)
32254 +{
32255 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32256 +}
32257 +
32258 +/* returns found or not */
32259 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32260 +{
32261 +       struct hlist_head *head;
32262 +       struct au_vdir_wh *pos;
32263 +       struct au_vdir_destr *str;
32264 +
32265 +       head = au_name_hash(whlist, name, nlen);
32266 +       hlist_for_each_entry(pos, head, wh_hash) {
32267 +               str = &pos->wh_str;
32268 +               AuDbg("%.*s\n", str->len, str->name);
32269 +               if (au_nhash_test_name(str, name, nlen))
32270 +                       return 1;
32271 +       }
32272 +       return 0;
32273 +}
32274 +
32275 +/* returns found(true) or not */
32276 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32277 +{
32278 +       struct hlist_head *head;
32279 +       struct au_vdir_dehstr *pos;
32280 +       struct au_vdir_destr *str;
32281 +
32282 +       head = au_name_hash(delist, name, nlen);
32283 +       hlist_for_each_entry(pos, head, hash) {
32284 +               str = pos->str;
32285 +               AuDbg("%.*s\n", str->len, str->name);
32286 +               if (au_nhash_test_name(str, name, nlen))
32287 +                       return 1;
32288 +       }
32289 +       return 0;
32290 +}
32291 +
32292 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32293 +                           unsigned char d_type)
32294 +{
32295 +#ifdef CONFIG_AUFS_SHWH
32296 +       wh->wh_ino = ino;
32297 +       wh->wh_type = d_type;
32298 +#endif
32299 +}
32300 +
32301 +/* ---------------------------------------------------------------------- */
32302 +
32303 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32304 +                      unsigned int d_type, aufs_bindex_t bindex,
32305 +                      unsigned char shwh)
32306 +{
32307 +       int err;
32308 +       struct au_vdir_destr *str;
32309 +       struct au_vdir_wh *wh;
32310 +
32311 +       AuDbg("%.*s\n", nlen, name);
32312 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32313 +
32314 +       err = -ENOMEM;
32315 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32316 +       if (unlikely(!wh))
32317 +               goto out;
32318 +
32319 +       err = 0;
32320 +       wh->wh_bindex = bindex;
32321 +       if (shwh)
32322 +               au_shwh_init_wh(wh, ino, d_type);
32323 +       str = &wh->wh_str;
32324 +       str->len = nlen;
32325 +       memcpy(str->name, name, nlen);
32326 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32327 +       /* smp_mb(); */
32328 +
32329 +out:
32330 +       return err;
32331 +}
32332 +
32333 +static int append_deblk(struct au_vdir *vdir)
32334 +{
32335 +       int err;
32336 +       unsigned long ul;
32337 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32338 +       union au_vdir_deblk_p p, deblk_end;
32339 +       unsigned char **o;
32340 +
32341 +       err = -ENOMEM;
32342 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32343 +                       GFP_NOFS, /*may_shrink*/0);
32344 +       if (unlikely(!o))
32345 +               goto out;
32346 +
32347 +       vdir->vd_deblk = o;
32348 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32349 +       if (p.deblk) {
32350 +               ul = vdir->vd_nblk++;
32351 +               vdir->vd_deblk[ul] = p.deblk;
32352 +               vdir->vd_last.ul = ul;
32353 +               vdir->vd_last.p.deblk = p.deblk;
32354 +               deblk_end.deblk = p.deblk + deblk_sz;
32355 +               err = set_deblk_end(&p, &deblk_end);
32356 +       }
32357 +
32358 +out:
32359 +       return err;
32360 +}
32361 +
32362 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32363 +                    unsigned int d_type, struct au_nhash *delist)
32364 +{
32365 +       int err;
32366 +       unsigned int sz;
32367 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32368 +       union au_vdir_deblk_p p, *room, deblk_end;
32369 +       struct au_vdir_dehstr *dehstr;
32370 +
32371 +       p.deblk = last_deblk(vdir);
32372 +       deblk_end.deblk = p.deblk + deblk_sz;
32373 +       room = &vdir->vd_last.p;
32374 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32375 +                 || !is_deblk_end(room, &deblk_end));
32376 +
32377 +       sz = calc_size(nlen);
32378 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32379 +               err = append_deblk(vdir);
32380 +               if (unlikely(err))
32381 +                       goto out;
32382 +
32383 +               p.deblk = last_deblk(vdir);
32384 +               deblk_end.deblk = p.deblk + deblk_sz;
32385 +               /* smp_mb(); */
32386 +               AuDebugOn(room->deblk != p.deblk);
32387 +       }
32388 +
32389 +       err = -ENOMEM;
32390 +       dehstr = au_cache_alloc_vdir_dehstr();
32391 +       if (unlikely(!dehstr))
32392 +               goto out;
32393 +
32394 +       dehstr->str = &room->de->de_str;
32395 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32396 +       room->de->de_ino = ino;
32397 +       room->de->de_type = d_type;
32398 +       room->de->de_str.len = nlen;
32399 +       memcpy(room->de->de_str.name, name, nlen);
32400 +
32401 +       err = 0;
32402 +       room->deblk += sz;
32403 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32404 +               err = append_deblk(vdir);
32405 +       /* smp_mb(); */
32406 +
32407 +out:
32408 +       return err;
32409 +}
32410 +
32411 +/* ---------------------------------------------------------------------- */
32412 +
32413 +void au_vdir_free(struct au_vdir *vdir)
32414 +{
32415 +       unsigned char **deblk;
32416 +
32417 +       deblk = vdir->vd_deblk;
32418 +       while (vdir->vd_nblk--)
32419 +               au_kfree_try_rcu(*deblk++);
32420 +       au_kfree_try_rcu(vdir->vd_deblk);
32421 +       au_cache_free_vdir(vdir);
32422 +}
32423 +
32424 +static struct au_vdir *alloc_vdir(struct file *file)
32425 +{
32426 +       struct au_vdir *vdir;
32427 +       struct super_block *sb;
32428 +       int err;
32429 +
32430 +       sb = file->f_path.dentry->d_sb;
32431 +       SiMustAnyLock(sb);
32432 +
32433 +       err = -ENOMEM;
32434 +       vdir = au_cache_alloc_vdir();
32435 +       if (unlikely(!vdir))
32436 +               goto out;
32437 +
32438 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32439 +       if (unlikely(!vdir->vd_deblk))
32440 +               goto out_free;
32441 +
32442 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32443 +       if (!vdir->vd_deblk_sz) {
32444 +               /* estimate the appropriate size for deblk */
32445 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32446 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32447 +       }
32448 +       vdir->vd_nblk = 0;
32449 +       vdir->vd_version = 0;
32450 +       vdir->vd_jiffy = 0;
32451 +       err = append_deblk(vdir);
32452 +       if (!err)
32453 +               return vdir; /* success */
32454 +
32455 +       au_kfree_try_rcu(vdir->vd_deblk);
32456 +
32457 +out_free:
32458 +       au_cache_free_vdir(vdir);
32459 +out:
32460 +       vdir = ERR_PTR(err);
32461 +       return vdir;
32462 +}
32463 +
32464 +static int reinit_vdir(struct au_vdir *vdir)
32465 +{
32466 +       int err;
32467 +       union au_vdir_deblk_p p, deblk_end;
32468 +
32469 +       while (vdir->vd_nblk > 1) {
32470 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32471 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32472 +               vdir->vd_nblk--;
32473 +       }
32474 +       p.deblk = vdir->vd_deblk[0];
32475 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32476 +       err = set_deblk_end(&p, &deblk_end);
32477 +       /* keep vd_dblk_sz */
32478 +       vdir->vd_last.ul = 0;
32479 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32480 +       vdir->vd_version = 0;
32481 +       vdir->vd_jiffy = 0;
32482 +       /* smp_mb(); */
32483 +       return err;
32484 +}
32485 +
32486 +/* ---------------------------------------------------------------------- */
32487 +
32488 +#define AuFillVdir_CALLED      1
32489 +#define AuFillVdir_WHABLE      (1 << 1)
32490 +#define AuFillVdir_SHWH                (1 << 2)
32491 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32492 +#define au_fset_fillvdir(flags, name) \
32493 +       do { (flags) |= AuFillVdir_##name; } while (0)
32494 +#define au_fclr_fillvdir(flags, name) \
32495 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32496 +
32497 +#ifndef CONFIG_AUFS_SHWH
32498 +#undef AuFillVdir_SHWH
32499 +#define AuFillVdir_SHWH                0
32500 +#endif
32501 +
32502 +struct fillvdir_arg {
32503 +       struct dir_context      ctx;
32504 +       struct file             *file;
32505 +       struct au_vdir          *vdir;
32506 +       struct au_nhash         delist;
32507 +       struct au_nhash         whlist;
32508 +       aufs_bindex_t           bindex;
32509 +       unsigned int            flags;
32510 +       int                     err;
32511 +};
32512 +
32513 +static bool fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32514 +                   loff_t offset __maybe_unused, u64 h_ino,
32515 +                   unsigned int d_type)
32516 +{
32517 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32518 +       char *name = (void *)__name;
32519 +       struct super_block *sb;
32520 +       ino_t ino;
32521 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32522 +
32523 +       arg->err = 0;
32524 +       sb = arg->file->f_path.dentry->d_sb;
32525 +       au_fset_fillvdir(arg->flags, CALLED);
32526 +       /* smp_mb(); */
32527 +       if (nlen <= AUFS_WH_PFX_LEN
32528 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32529 +               if (test_known(&arg->delist, name, nlen)
32530 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32531 +                       goto out; /* already exists or whiteouted */
32532 +
32533 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32534 +               if (!arg->err) {
32535 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32536 +                               d_type = DT_UNKNOWN;
32537 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32538 +                                            d_type, &arg->delist);
32539 +               }
32540 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32541 +               name += AUFS_WH_PFX_LEN;
32542 +               nlen -= AUFS_WH_PFX_LEN;
32543 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32544 +                       goto out; /* already whiteouted */
32545 +
32546 +               ino = 0; /* just to suppress a warning */
32547 +               if (shwh)
32548 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32549 +                                            &ino);
32550 +               if (!arg->err) {
32551 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32552 +                               d_type = DT_UNKNOWN;
32553 +                       arg->err = au_nhash_append_wh
32554 +                               (&arg->whlist, name, nlen, ino, d_type,
32555 +                                arg->bindex, shwh);
32556 +               }
32557 +       }
32558 +
32559 +out:
32560 +       if (!arg->err)
32561 +               arg->vdir->vd_jiffy = jiffies;
32562 +       /* smp_mb(); */
32563 +       AuTraceErr(arg->err);
32564 +       return !arg->err;
32565 +}
32566 +
32567 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32568 +                         struct au_nhash *whlist, struct au_nhash *delist)
32569 +{
32570 +#ifdef CONFIG_AUFS_SHWH
32571 +       int err;
32572 +       unsigned int nh, u;
32573 +       struct hlist_head *head;
32574 +       struct au_vdir_wh *pos;
32575 +       struct hlist_node *n;
32576 +       char *p, *o;
32577 +       struct au_vdir_destr *destr;
32578 +
32579 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32580 +
32581 +       err = -ENOMEM;
32582 +       o = p = (void *)__get_free_page(GFP_NOFS);
32583 +       if (unlikely(!p))
32584 +               goto out;
32585 +
32586 +       err = 0;
32587 +       nh = whlist->nh_num;
32588 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32589 +       p += AUFS_WH_PFX_LEN;
32590 +       for (u = 0; u < nh; u++) {
32591 +               head = whlist->nh_head + u;
32592 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32593 +                       destr = &pos->wh_str;
32594 +                       memcpy(p, destr->name, destr->len);
32595 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32596 +                                       pos->wh_ino, pos->wh_type, delist);
32597 +                       if (unlikely(err))
32598 +                               break;
32599 +               }
32600 +       }
32601 +
32602 +       free_page((unsigned long)o);
32603 +
32604 +out:
32605 +       AuTraceErr(err);
32606 +       return err;
32607 +#else
32608 +       return 0;
32609 +#endif
32610 +}
32611 +
32612 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32613 +{
32614 +       int err;
32615 +       unsigned int rdhash;
32616 +       loff_t offset;
32617 +       aufs_bindex_t bbot, bindex, btop;
32618 +       unsigned char shwh;
32619 +       struct file *hf, *file;
32620 +       struct super_block *sb;
32621 +
32622 +       file = arg->file;
32623 +       sb = file->f_path.dentry->d_sb;
32624 +       SiMustAnyLock(sb);
32625 +
32626 +       rdhash = au_sbi(sb)->si_rdhash;
32627 +       if (!rdhash)
32628 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32629 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32630 +       if (unlikely(err))
32631 +               goto out;
32632 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32633 +       if (unlikely(err))
32634 +               goto out_delist;
32635 +
32636 +       err = 0;
32637 +       arg->flags = 0;
32638 +       shwh = 0;
32639 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32640 +               shwh = 1;
32641 +               au_fset_fillvdir(arg->flags, SHWH);
32642 +       }
32643 +       btop = au_fbtop(file);
32644 +       bbot = au_fbbot_dir(file);
32645 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32646 +               hf = au_hf_dir(file, bindex);
32647 +               if (!hf)
32648 +                       continue;
32649 +
32650 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32651 +               err = offset;
32652 +               if (unlikely(offset))
32653 +                       break;
32654 +
32655 +               arg->bindex = bindex;
32656 +               au_fclr_fillvdir(arg->flags, WHABLE);
32657 +               if (shwh
32658 +                   || (bindex != bbot
32659 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32660 +                       au_fset_fillvdir(arg->flags, WHABLE);
32661 +               do {
32662 +                       arg->err = 0;
32663 +                       au_fclr_fillvdir(arg->flags, CALLED);
32664 +                       /* smp_mb(); */
32665 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32666 +                       if (err >= 0)
32667 +                               err = arg->err;
32668 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32669 +
32670 +               /*
32671 +                * dir_relax() may be good for concurrency, but aufs should not
32672 +                * use it since it will cause a lockdep problem.
32673 +                */
32674 +       }
32675 +
32676 +       if (!err && shwh)
32677 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32678 +
32679 +       au_nhash_wh_free(&arg->whlist);
32680 +
32681 +out_delist:
32682 +       au_nhash_de_free(&arg->delist);
32683 +out:
32684 +       return err;
32685 +}
32686 +
32687 +static int read_vdir(struct file *file, int may_read)
32688 +{
32689 +       int err;
32690 +       unsigned long expire;
32691 +       unsigned char do_read;
32692 +       struct fillvdir_arg arg = {
32693 +               .ctx = {
32694 +                       .actor = fillvdir
32695 +               }
32696 +       };
32697 +       struct inode *inode;
32698 +       struct au_vdir *vdir, *allocated;
32699 +
32700 +       err = 0;
32701 +       inode = file_inode(file);
32702 +       IMustLock(inode);
32703 +       IiMustWriteLock(inode);
32704 +       SiMustAnyLock(inode->i_sb);
32705 +
32706 +       allocated = NULL;
32707 +       do_read = 0;
32708 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32709 +       vdir = au_ivdir(inode);
32710 +       if (!vdir) {
32711 +               do_read = 1;
32712 +               vdir = alloc_vdir(file);
32713 +               err = PTR_ERR(vdir);
32714 +               if (IS_ERR(vdir))
32715 +                       goto out;
32716 +               err = 0;
32717 +               allocated = vdir;
32718 +       } else if (may_read
32719 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32720 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32721 +               do_read = 1;
32722 +               err = reinit_vdir(vdir);
32723 +               if (unlikely(err))
32724 +                       goto out;
32725 +       }
32726 +
32727 +       if (!do_read)
32728 +               return 0; /* success */
32729 +
32730 +       arg.file = file;
32731 +       arg.vdir = vdir;
32732 +       err = au_do_read_vdir(&arg);
32733 +       if (!err) {
32734 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32735 +               vdir->vd_version = inode_query_iversion(inode);
32736 +               vdir->vd_last.ul = 0;
32737 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32738 +               if (allocated)
32739 +                       au_set_ivdir(inode, allocated);
32740 +       } else if (allocated)
32741 +               au_vdir_free(allocated);
32742 +
32743 +out:
32744 +       return err;
32745 +}
32746 +
32747 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32748 +{
32749 +       int err, rerr;
32750 +       unsigned long ul, n;
32751 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32752 +
32753 +       AuDebugOn(tgt->vd_nblk != 1);
32754 +
32755 +       err = -ENOMEM;
32756 +       if (tgt->vd_nblk < src->vd_nblk) {
32757 +               unsigned char **p;
32758 +
32759 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32760 +                               GFP_NOFS, /*may_shrink*/0);
32761 +               if (unlikely(!p))
32762 +                       goto out;
32763 +               tgt->vd_deblk = p;
32764 +       }
32765 +
32766 +       if (tgt->vd_deblk_sz != deblk_sz) {
32767 +               unsigned char *p;
32768 +
32769 +               tgt->vd_deblk_sz = deblk_sz;
32770 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32771 +                               /*may_shrink*/1);
32772 +               if (unlikely(!p))
32773 +                       goto out;
32774 +               tgt->vd_deblk[0] = p;
32775 +       }
32776 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32777 +       tgt->vd_version = src->vd_version;
32778 +       tgt->vd_jiffy = src->vd_jiffy;
32779 +
32780 +       n = src->vd_nblk;
32781 +       for (ul = 1; ul < n; ul++) {
32782 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32783 +                                           GFP_NOFS);
32784 +               if (unlikely(!tgt->vd_deblk[ul]))
32785 +                       goto out;
32786 +               tgt->vd_nblk++;
32787 +       }
32788 +       tgt->vd_nblk = n;
32789 +       tgt->vd_last.ul = tgt->vd_last.ul;
32790 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32791 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32792 +               - src->vd_deblk[src->vd_last.ul];
32793 +       /* smp_mb(); */
32794 +       return 0; /* success */
32795 +
32796 +out:
32797 +       rerr = reinit_vdir(tgt);
32798 +       BUG_ON(rerr);
32799 +       return err;
32800 +}
32801 +
32802 +int au_vdir_init(struct file *file)
32803 +{
32804 +       int err;
32805 +       struct inode *inode;
32806 +       struct au_vdir *vdir_cache, *allocated;
32807 +
32808 +       /* test file->f_pos here instead of ctx->pos */
32809 +       err = read_vdir(file, !file->f_pos);
32810 +       if (unlikely(err))
32811 +               goto out;
32812 +
32813 +       allocated = NULL;
32814 +       vdir_cache = au_fvdir_cache(file);
32815 +       if (!vdir_cache) {
32816 +               vdir_cache = alloc_vdir(file);
32817 +               err = PTR_ERR(vdir_cache);
32818 +               if (IS_ERR(vdir_cache))
32819 +                       goto out;
32820 +               allocated = vdir_cache;
32821 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32822 +               /* test file->f_pos here instead of ctx->pos */
32823 +               err = reinit_vdir(vdir_cache);
32824 +               if (unlikely(err))
32825 +                       goto out;
32826 +       } else
32827 +               return 0; /* success */
32828 +
32829 +       inode = file_inode(file);
32830 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32831 +       if (!err) {
32832 +               file->f_version = inode_query_iversion(inode);
32833 +               if (allocated)
32834 +                       au_set_fvdir_cache(file, allocated);
32835 +       } else if (allocated)
32836 +               au_vdir_free(allocated);
32837 +
32838 +out:
32839 +       return err;
32840 +}
32841 +
32842 +static loff_t calc_offset(struct au_vdir *vdir)
32843 +{
32844 +       loff_t offset;
32845 +       union au_vdir_deblk_p p;
32846 +
32847 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32848 +       offset = vdir->vd_last.p.deblk - p.deblk;
32849 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32850 +       return offset;
32851 +}
32852 +
32853 +/* returns true or false */
32854 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32855 +{
32856 +       int valid;
32857 +       unsigned int deblk_sz;
32858 +       unsigned long ul, n;
32859 +       loff_t offset;
32860 +       union au_vdir_deblk_p p, deblk_end;
32861 +       struct au_vdir *vdir_cache;
32862 +
32863 +       valid = 1;
32864 +       vdir_cache = au_fvdir_cache(file);
32865 +       offset = calc_offset(vdir_cache);
32866 +       AuDbg("offset %lld\n", offset);
32867 +       if (ctx->pos == offset)
32868 +               goto out;
32869 +
32870 +       vdir_cache->vd_last.ul = 0;
32871 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32872 +       if (!ctx->pos)
32873 +               goto out;
32874 +
32875 +       valid = 0;
32876 +       deblk_sz = vdir_cache->vd_deblk_sz;
32877 +       ul = div64_u64(ctx->pos, deblk_sz);
32878 +       AuDbg("ul %lu\n", ul);
32879 +       if (ul >= vdir_cache->vd_nblk)
32880 +               goto out;
32881 +
32882 +       n = vdir_cache->vd_nblk;
32883 +       for (; ul < n; ul++) {
32884 +               p.deblk = vdir_cache->vd_deblk[ul];
32885 +               deblk_end.deblk = p.deblk + deblk_sz;
32886 +               offset = ul;
32887 +               offset *= deblk_sz;
32888 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32889 +                       unsigned int l;
32890 +
32891 +                       l = calc_size(p.de->de_str.len);
32892 +                       offset += l;
32893 +                       p.deblk += l;
32894 +               }
32895 +               if (!is_deblk_end(&p, &deblk_end)) {
32896 +                       valid = 1;
32897 +                       vdir_cache->vd_last.ul = ul;
32898 +                       vdir_cache->vd_last.p = p;
32899 +                       break;
32900 +               }
32901 +       }
32902 +
32903 +out:
32904 +       /* smp_mb(); */
32905 +       if (!valid)
32906 +               AuDbg("valid %d\n", !valid);
32907 +       return valid;
32908 +}
32909 +
32910 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32911 +{
32912 +       unsigned int l, deblk_sz;
32913 +       union au_vdir_deblk_p deblk_end;
32914 +       struct au_vdir *vdir_cache;
32915 +       struct au_vdir_de *de;
32916 +
32917 +       if (!seek_vdir(file, ctx))
32918 +               return 0;
32919 +
32920 +       vdir_cache = au_fvdir_cache(file);
32921 +       deblk_sz = vdir_cache->vd_deblk_sz;
32922 +       while (1) {
32923 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32924 +               deblk_end.deblk += deblk_sz;
32925 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32926 +                       de = vdir_cache->vd_last.p.de;
32927 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32928 +                             de->de_str.len, de->de_str.name, ctx->pos,
32929 +                             (unsigned long)de->de_ino, de->de_type);
32930 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32931 +                                              de->de_str.len, de->de_ino,
32932 +                                              de->de_type))) {
32933 +                               /* todo: ignore the error caused by udba? */
32934 +                               /* return err; */
32935 +                               return 0;
32936 +                       }
32937 +
32938 +                       l = calc_size(de->de_str.len);
32939 +                       vdir_cache->vd_last.p.deblk += l;
32940 +                       ctx->pos += l;
32941 +               }
32942 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32943 +                       vdir_cache->vd_last.ul++;
32944 +                       vdir_cache->vd_last.p.deblk
32945 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32946 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32947 +                       continue;
32948 +               }
32949 +               break;
32950 +       }
32951 +
32952 +       /* smp_mb(); */
32953 +       return 0;
32954 +}
32955 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32956 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32957 +++ linux/fs/aufs/vfsub.c       2023-04-24 08:13:40.969576115 +0200
32958 @@ -0,0 +1,918 @@
32959 +// SPDX-License-Identifier: GPL-2.0
32960 +/*
32961 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32962 + *
32963 + * This program is free software; you can redistribute it and/or modify
32964 + * it under the terms of the GNU General Public License as published by
32965 + * the Free Software Foundation; either version 2 of the License, or
32966 + * (at your option) any later version.
32967 + *
32968 + * This program is distributed in the hope that it will be useful,
32969 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32970 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32971 + * GNU General Public License for more details.
32972 + *
32973 + * You should have received a copy of the GNU General Public License
32974 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32975 + */
32976 +
32977 +/*
32978 + * sub-routines for VFS
32979 + */
32980 +
32981 +#include <linux/mnt_namespace.h>
32982 +#include <linux/nsproxy.h>
32983 +#include <linux/security.h>
32984 +#include <linux/splice.h>
32985 +#include "aufs.h"
32986 +
32987 +#ifdef CONFIG_AUFS_BR_FUSE
32988 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32989 +{
32990 +       if (!au_test_fuse(h_sb) || !au_userns)
32991 +               return 0;
32992 +
32993 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32994 +}
32995 +#endif
32996 +
32997 +int vfsub_sync_filesystem(struct super_block *h_sb)
32998 +{
32999 +       int err;
33000 +
33001 +       lockdep_off();
33002 +       down_read(&h_sb->s_umount);
33003 +       err = sync_filesystem(h_sb);
33004 +       up_read(&h_sb->s_umount);
33005 +       lockdep_on();
33006 +
33007 +       return err;
33008 +}
33009 +
33010 +/* ---------------------------------------------------------------------- */
33011 +
33012 +int vfsub_update_h_iattr(struct path *h_path, int *did)
33013 +{
33014 +       int err;
33015 +       struct kstat st;
33016 +       struct super_block *h_sb;
33017 +
33018 +       /*
33019 +        * Always needs h_path->mnt for LSM or FUSE branch.
33020 +        */
33021 +       AuDebugOn(!h_path->mnt);
33022 +
33023 +       /* for remote fs, leave work for its getattr or d_revalidate */
33024 +       /* for bad i_attr fs, handle them in aufs_getattr() */
33025 +       /* still some fs may acquire i_mutex. we need to skip them */
33026 +       err = 0;
33027 +       if (!did)
33028 +               did = &err;
33029 +       h_sb = h_path->dentry->d_sb;
33030 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
33031 +       if (*did)
33032 +               err = vfsub_getattr(h_path, &st);
33033 +
33034 +       return err;
33035 +}
33036 +
33037 +/* ---------------------------------------------------------------------- */
33038 +
33039 +struct file *vfsub_dentry_open(struct path *path, int flags)
33040 +{
33041 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
33042 +                          current_cred());
33043 +}
33044 +
33045 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
33046 +{
33047 +       struct file *file;
33048 +
33049 +       lockdep_off();
33050 +       file = filp_open(path,
33051 +                        oflags /* | __FMODE_NONOTIFY */,
33052 +                        mode);
33053 +       lockdep_on();
33054 +       if (IS_ERR(file))
33055 +               goto out;
33056 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33057 +
33058 +out:
33059 +       return file;
33060 +}
33061 +
33062 +/*
33063 + * Ideally this function should call VFS:do_last() in order to keep all its
33064 + * checkings. But it is very hard for aufs to regenerate several VFS internal
33065 + * structure such as nameidata. This is a second (or third) best approach.
33066 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
33067 + */
33068 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33069 +                     struct vfsub_aopen_args *args)
33070 +{
33071 +       int err;
33072 +       struct au_branch *br = args->br;
33073 +       struct file *file = args->file;
33074 +       /* copied from linux/fs/namei.c:atomic_open() */
33075 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33076 +
33077 +       IMustLock(dir);
33078 +       AuDebugOn(!dir->i_op->atomic_open);
33079 +
33080 +       err = au_br_test_oflag(args->open_flag, br);
33081 +       if (unlikely(err))
33082 +               goto out;
33083 +
33084 +       au_lcnt_inc(&br->br_nfiles);
33085 +       file->f_path.dentry = DENTRY_NOT_SET;
33086 +       file->f_path.mnt = au_br_mnt(br);
33087 +       AuDbg("%ps\n", dir->i_op->atomic_open);
33088 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
33089 +                                    args->create_mode);
33090 +       if (unlikely(err < 0)) {
33091 +               au_lcnt_dec(&br->br_nfiles);
33092 +               goto out;
33093 +       }
33094 +
33095 +       /* temporary workaround for nfsv4 branch */
33096 +       if (au_test_nfs(dir->i_sb))
33097 +               nfs_mark_for_revalidate(dir);
33098 +
33099 +       if (file->f_mode & FMODE_CREATED)
33100 +               fsnotify_create(dir, dentry);
33101 +       if (!(file->f_mode & FMODE_OPENED)) {
33102 +               au_lcnt_dec(&br->br_nfiles);
33103 +               goto out;
33104 +       }
33105 +
33106 +       /* todo: call VFS:may_open() here */
33107 +       /* todo: ima_file_check() too? */
33108 +       if (!err && (args->open_flag & __FMODE_EXEC))
33109 +               err = deny_write_access(file);
33110 +       if (!err)
33111 +               fsnotify_open(file);
33112 +       else
33113 +               au_lcnt_dec(&br->br_nfiles);
33114 +       /* note that the file is created and still opened */
33115 +
33116 +out:
33117 +       return err;
33118 +}
33119 +
33120 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33121 +{
33122 +       int err;
33123 +
33124 +       err = kern_path(name, flags, path);
33125 +       if (!err && d_is_positive(path->dentry))
33126 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33127 +       return err;
33128 +}
33129 +
33130 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33131 +                                            struct path *ppath, int len)
33132 +{
33133 +       struct path path;
33134 +
33135 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
33136 +       if (IS_ERR(path.dentry))
33137 +               goto out;
33138 +       if (d_is_positive(path.dentry)) {
33139 +               path.mnt = ppath->mnt;
33140 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33141 +       }
33142 +
33143 +out:
33144 +       AuTraceErrPtr(path.dentry);
33145 +       return path.dentry;
33146 +}
33147 +
33148 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33149 +                                   int len)
33150 +{
33151 +       struct path path;
33152 +
33153 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33154 +       IMustLock(d_inode(ppath->dentry));
33155 +
33156 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
33157 +       if (IS_ERR(path.dentry))
33158 +               goto out;
33159 +       if (d_is_positive(path.dentry)) {
33160 +               path.mnt = ppath->mnt;
33161 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33162 +       }
33163 +
33164 +out:
33165 +       AuTraceErrPtr(path.dentry);
33166 +       return path.dentry;
33167 +}
33168 +
33169 +void vfsub_call_lkup_one(void *args)
33170 +{
33171 +       struct vfsub_lkup_one_args *a = args;
33172 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
33173 +}
33174 +
33175 +/* ---------------------------------------------------------------------- */
33176 +
33177 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33178 +                                struct dentry *d2, struct au_hinode *hdir2)
33179 +{
33180 +       struct dentry *d;
33181 +
33182 +       lockdep_off();
33183 +       d = lock_rename(d1, d2);
33184 +       lockdep_on();
33185 +       au_hn_suspend(hdir1);
33186 +       if (hdir1 != hdir2)
33187 +               au_hn_suspend(hdir2);
33188 +
33189 +       return d;
33190 +}
33191 +
33192 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33193 +                        struct dentry *d2, struct au_hinode *hdir2)
33194 +{
33195 +       au_hn_resume(hdir1);
33196 +       if (hdir1 != hdir2)
33197 +               au_hn_resume(hdir2);
33198 +       lockdep_off();
33199 +       unlock_rename(d1, d2);
33200 +       lockdep_on();
33201 +}
33202 +
33203 +/* ---------------------------------------------------------------------- */
33204 +
33205 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33206 +{
33207 +       int err;
33208 +       struct dentry *d;
33209 +       struct mnt_idmap *idmap;
33210 +
33211 +       IMustLock(dir);
33212 +
33213 +       d = path->dentry;
33214 +       path->dentry = d->d_parent;
33215 +       err = security_path_mknod(path, d, mode, 0);
33216 +       path->dentry = d;
33217 +       if (unlikely(err))
33218 +               goto out;
33219 +       idmap = mnt_idmap(path->mnt);
33220 +
33221 +       lockdep_off();
33222 +       err = vfs_create(idmap, dir, path->dentry, mode, want_excl);
33223 +       lockdep_on();
33224 +       if (!err) {
33225 +               struct path tmp = *path;
33226 +               int did;
33227 +
33228 +               vfsub_update_h_iattr(&tmp, &did);
33229 +               if (did) {
33230 +                       tmp.dentry = path->dentry->d_parent;
33231 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33232 +               }
33233 +               /*ignore*/
33234 +       }
33235 +
33236 +out:
33237 +       return err;
33238 +}
33239 +
33240 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33241 +{
33242 +       int err;
33243 +       struct dentry *d;
33244 +       struct mnt_idmap *idmap;
33245 +
33246 +       IMustLock(dir);
33247 +
33248 +       d = path->dentry;
33249 +       path->dentry = d->d_parent;
33250 +       err = security_path_symlink(path, d, symname);
33251 +       path->dentry = d;
33252 +       if (unlikely(err))
33253 +               goto out;
33254 +       idmap = mnt_idmap(path->mnt);
33255 +
33256 +       lockdep_off();
33257 +       err = vfs_symlink(idmap, dir, path->dentry, symname);
33258 +       lockdep_on();
33259 +       if (!err) {
33260 +               struct path tmp = *path;
33261 +               int did;
33262 +
33263 +               vfsub_update_h_iattr(&tmp, &did);
33264 +               if (did) {
33265 +                       tmp.dentry = path->dentry->d_parent;
33266 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33267 +               }
33268 +               /*ignore*/
33269 +       }
33270 +
33271 +out:
33272 +       return err;
33273 +}
33274 +
33275 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33276 +{
33277 +       int err;
33278 +       struct dentry *d;
33279 +       struct mnt_idmap *idmap;
33280 +
33281 +       IMustLock(dir);
33282 +
33283 +       d = path->dentry;
33284 +       path->dentry = d->d_parent;
33285 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33286 +       path->dentry = d;
33287 +       if (unlikely(err))
33288 +               goto out;
33289 +       idmap = mnt_idmap(path->mnt);
33290 +
33291 +       lockdep_off();
33292 +       err = vfs_mknod(idmap, dir, path->dentry, mode, dev);
33293 +       lockdep_on();
33294 +       if (!err) {
33295 +               struct path tmp = *path;
33296 +               int did;
33297 +
33298 +               vfsub_update_h_iattr(&tmp, &did);
33299 +               if (did) {
33300 +                       tmp.dentry = path->dentry->d_parent;
33301 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33302 +               }
33303 +               /*ignore*/
33304 +       }
33305 +
33306 +out:
33307 +       return err;
33308 +}
33309 +
33310 +static int au_test_nlink(struct inode *inode)
33311 +{
33312 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33313 +
33314 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33315 +           || inode->i_nlink < link_max)
33316 +               return 0;
33317 +       return -EMLINK;
33318 +}
33319 +
33320 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33321 +              struct inode **delegated_inode)
33322 +{
33323 +       int err;
33324 +       struct dentry *d;
33325 +       struct mnt_idmap *idmap;
33326 +
33327 +       IMustLock(dir);
33328 +
33329 +       err = au_test_nlink(d_inode(src_dentry));
33330 +       if (unlikely(err))
33331 +               return err;
33332 +
33333 +       /* we don't call may_linkat() */
33334 +       d = path->dentry;
33335 +       path->dentry = d->d_parent;
33336 +       err = security_path_link(src_dentry, path, d);
33337 +       path->dentry = d;
33338 +       if (unlikely(err))
33339 +               goto out;
33340 +       idmap = mnt_idmap(path->mnt);
33341 +
33342 +       lockdep_off();
33343 +       err = vfs_link(src_dentry, idmap, dir, path->dentry, delegated_inode);
33344 +       lockdep_on();
33345 +       if (!err) {
33346 +               struct path tmp = *path;
33347 +               int did;
33348 +
33349 +               /* fuse has different memory inode for the same inumber */
33350 +               vfsub_update_h_iattr(&tmp, &did);
33351 +               if (did) {
33352 +                       tmp.dentry = path->dentry->d_parent;
33353 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33354 +                       tmp.dentry = src_dentry;
33355 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33356 +               }
33357 +               /*ignore*/
33358 +       }
33359 +
33360 +out:
33361 +       return err;
33362 +}
33363 +
33364 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33365 +                struct inode *dir, struct path *path,
33366 +                struct inode **delegated_inode, unsigned int flags)
33367 +{
33368 +       int err;
33369 +       struct renamedata rd;
33370 +       struct path tmp = {
33371 +               .mnt    = path->mnt
33372 +       };
33373 +       struct dentry *d;
33374 +
33375 +       IMustLock(dir);
33376 +       IMustLock(src_dir);
33377 +
33378 +       d = path->dentry;
33379 +       path->dentry = d->d_parent;
33380 +       tmp.dentry = src_dentry->d_parent;
33381 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33382 +       path->dentry = d;
33383 +       if (unlikely(err))
33384 +               goto out;
33385 +
33386 +       rd.old_mnt_idmap = mnt_idmap(path->mnt);
33387 +       rd.old_dir = src_dir;
33388 +       rd.old_dentry = src_dentry;
33389 +       rd.new_mnt_idmap = rd.old_mnt_idmap;
33390 +       rd.new_dir = dir;
33391 +       rd.new_dentry = path->dentry;
33392 +       rd.delegated_inode = delegated_inode;
33393 +       rd.flags = flags;
33394 +       lockdep_off();
33395 +       err = vfs_rename(&rd);
33396 +       lockdep_on();
33397 +       if (!err) {
33398 +               int did;
33399 +
33400 +               tmp.dentry = d->d_parent;
33401 +               vfsub_update_h_iattr(&tmp, &did);
33402 +               if (did) {
33403 +                       tmp.dentry = src_dentry;
33404 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33405 +                       tmp.dentry = src_dentry->d_parent;
33406 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33407 +               }
33408 +               /*ignore*/
33409 +       }
33410 +
33411 +out:
33412 +       return err;
33413 +}
33414 +
33415 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33416 +{
33417 +       int err;
33418 +       struct dentry *d;
33419 +       struct mnt_idmap *idmap;
33420 +
33421 +       IMustLock(dir);
33422 +
33423 +       d = path->dentry;
33424 +       path->dentry = d->d_parent;
33425 +       err = security_path_mkdir(path, d, mode);
33426 +       path->dentry = d;
33427 +       if (unlikely(err))
33428 +               goto out;
33429 +       idmap = mnt_idmap(path->mnt);
33430 +
33431 +       lockdep_off();
33432 +       err = vfs_mkdir(idmap, dir, path->dentry, mode);
33433 +       lockdep_on();
33434 +       if (!err) {
33435 +               struct path tmp = *path;
33436 +               int did;
33437 +
33438 +               vfsub_update_h_iattr(&tmp, &did);
33439 +               if (did) {
33440 +                       tmp.dentry = path->dentry->d_parent;
33441 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33442 +               }
33443 +               /*ignore*/
33444 +       }
33445 +
33446 +out:
33447 +       return err;
33448 +}
33449 +
33450 +int vfsub_rmdir(struct inode *dir, struct path *path)
33451 +{
33452 +       int err;
33453 +       struct dentry *d;
33454 +       struct mnt_idmap *idmap;
33455 +
33456 +       IMustLock(dir);
33457 +
33458 +       d = path->dentry;
33459 +       path->dentry = d->d_parent;
33460 +       err = security_path_rmdir(path, d);
33461 +       path->dentry = d;
33462 +       if (unlikely(err))
33463 +               goto out;
33464 +       idmap = mnt_idmap(path->mnt);
33465 +
33466 +       lockdep_off();
33467 +       err = vfs_rmdir(idmap, dir, path->dentry);
33468 +       lockdep_on();
33469 +       if (!err) {
33470 +               struct path tmp = {
33471 +                       .dentry = path->dentry->d_parent,
33472 +                       .mnt    = path->mnt
33473 +               };
33474 +
33475 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33476 +       }
33477 +
33478 +out:
33479 +       return err;
33480 +}
33481 +
33482 +/* ---------------------------------------------------------------------- */
33483 +
33484 +/* todo: support mmap_sem? */
33485 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33486 +                    loff_t *ppos)
33487 +{
33488 +       ssize_t err;
33489 +
33490 +       lockdep_off();
33491 +       err = vfs_read(file, ubuf, count, ppos);
33492 +       lockdep_on();
33493 +       if (err >= 0)
33494 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33495 +       return err;
33496 +}
33497 +
33498 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33499 +                    loff_t *ppos)
33500 +{
33501 +       ssize_t err;
33502 +
33503 +       lockdep_off();
33504 +       err = kernel_read(file, kbuf, count, ppos);
33505 +       lockdep_on();
33506 +       AuTraceErr(err);
33507 +       if (err >= 0)
33508 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33509 +       return err;
33510 +}
33511 +
33512 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33513 +                     loff_t *ppos)
33514 +{
33515 +       ssize_t err;
33516 +
33517 +       lockdep_off();
33518 +       err = vfs_write(file, ubuf, count, ppos);
33519 +       lockdep_on();
33520 +       if (err >= 0)
33521 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33522 +       return err;
33523 +}
33524 +
33525 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33526 +{
33527 +       ssize_t err;
33528 +
33529 +       lockdep_off();
33530 +       err = kernel_write(file, kbuf, count, ppos);
33531 +       lockdep_on();
33532 +       if (err >= 0)
33533 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33534 +       return err;
33535 +}
33536 +
33537 +int vfsub_flush(struct file *file, fl_owner_t id)
33538 +{
33539 +       int err;
33540 +
33541 +       err = 0;
33542 +       if (file->f_op->flush) {
33543 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33544 +                       err = file->f_op->flush(file, id);
33545 +               else {
33546 +                       lockdep_off();
33547 +                       err = file->f_op->flush(file, id);
33548 +                       lockdep_on();
33549 +               }
33550 +               if (!err)
33551 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33552 +               /*ignore*/
33553 +       }
33554 +       return err;
33555 +}
33556 +
33557 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33558 +{
33559 +       int err;
33560 +
33561 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33562 +
33563 +       lockdep_off();
33564 +       err = iterate_dir(file, ctx);
33565 +       lockdep_on();
33566 +       if (err >= 0)
33567 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33568 +
33569 +       return err;
33570 +}
33571 +
33572 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33573 +                    struct pipe_inode_info *pipe, size_t len,
33574 +                    unsigned int flags)
33575 +{
33576 +       long err;
33577 +
33578 +       lockdep_off();
33579 +       err = do_splice_to(in, ppos, pipe, len, flags);
33580 +       lockdep_on();
33581 +       file_accessed(in);
33582 +       if (err >= 0)
33583 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33584 +       return err;
33585 +}
33586 +
33587 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33588 +                      loff_t *ppos, size_t len, unsigned int flags)
33589 +{
33590 +       long err;
33591 +
33592 +       lockdep_off();
33593 +       err = do_splice_from(pipe, out, ppos, len, flags);
33594 +       lockdep_on();
33595 +       if (err >= 0)
33596 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33597 +       return err;
33598 +}
33599 +
33600 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33601 +{
33602 +       int err;
33603 +
33604 +       /* file can be NULL */
33605 +       lockdep_off();
33606 +       err = vfs_fsync(file, datasync);
33607 +       lockdep_on();
33608 +       if (!err) {
33609 +               if (!path) {
33610 +                       AuDebugOn(!file);
33611 +                       path = &file->f_path;
33612 +               }
33613 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33614 +       }
33615 +       return err;
33616 +}
33617 +
33618 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33619 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33620 +               struct file *h_file)
33621 +{
33622 +       int err;
33623 +       struct inode *h_inode;
33624 +       struct super_block *h_sb;
33625 +       struct mnt_idmap *h_idmap;
33626 +
33627 +       if (!h_file) {
33628 +               err = vfsub_truncate(h_path, length);
33629 +               goto out;
33630 +       }
33631 +
33632 +       h_inode = d_inode(h_path->dentry);
33633 +       h_sb = h_inode->i_sb;
33634 +       lockdep_off();
33635 +       sb_start_write(h_sb);
33636 +       lockdep_on();
33637 +       err = security_file_truncate(h_file);
33638 +       if (!err) {
33639 +               h_idmap = mnt_idmap(h_path->mnt);
33640 +               lockdep_off();
33641 +               err = do_truncate(h_idmap, h_path->dentry, length, attr,
33642 +                                 h_file);
33643 +               lockdep_on();
33644 +       }
33645 +       lockdep_off();
33646 +       sb_end_write(h_sb);
33647 +       lockdep_on();
33648 +
33649 +out:
33650 +       return err;
33651 +}
33652 +
33653 +/* ---------------------------------------------------------------------- */
33654 +
33655 +struct au_vfsub_mkdir_args {
33656 +       int *errp;
33657 +       struct inode *dir;
33658 +       struct path *path;
33659 +       int mode;
33660 +};
33661 +
33662 +static void au_call_vfsub_mkdir(void *args)
33663 +{
33664 +       struct au_vfsub_mkdir_args *a = args;
33665 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33666 +}
33667 +
33668 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33669 +{
33670 +       int err, do_sio, wkq_err;
33671 +       struct mnt_idmap *idmap;
33672 +
33673 +       idmap = mnt_idmap(path->mnt);
33674 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33675 +       if (!do_sio) {
33676 +               lockdep_off();
33677 +               err = vfsub_mkdir(dir, path, mode);
33678 +               lockdep_on();
33679 +       } else {
33680 +               struct au_vfsub_mkdir_args args = {
33681 +                       .errp   = &err,
33682 +                       .dir    = dir,
33683 +                       .path   = path,
33684 +                       .mode   = mode
33685 +               };
33686 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33687 +               if (unlikely(wkq_err))
33688 +                       err = wkq_err;
33689 +       }
33690 +
33691 +       return err;
33692 +}
33693 +
33694 +struct au_vfsub_rmdir_args {
33695 +       int *errp;
33696 +       struct inode *dir;
33697 +       struct path *path;
33698 +};
33699 +
33700 +static void au_call_vfsub_rmdir(void *args)
33701 +{
33702 +       struct au_vfsub_rmdir_args *a = args;
33703 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33704 +}
33705 +
33706 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33707 +{
33708 +       int err, do_sio, wkq_err;
33709 +       struct mnt_idmap *idmap;
33710 +
33711 +       idmap = mnt_idmap(path->mnt);
33712 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33713 +       if (!do_sio) {
33714 +               lockdep_off();
33715 +               err = vfsub_rmdir(dir, path);
33716 +               lockdep_on();
33717 +       } else {
33718 +               struct au_vfsub_rmdir_args args = {
33719 +                       .errp   = &err,
33720 +                       .dir    = dir,
33721 +                       .path   = path
33722 +               };
33723 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33724 +               if (unlikely(wkq_err))
33725 +                       err = wkq_err;
33726 +       }
33727 +
33728 +       return err;
33729 +}
33730 +
33731 +/* ---------------------------------------------------------------------- */
33732 +
33733 +struct notify_change_args {
33734 +       int *errp;
33735 +       struct path *path;
33736 +       struct iattr *ia;
33737 +       struct inode **delegated_inode;
33738 +};
33739 +
33740 +static void call_notify_change(void *args)
33741 +{
33742 +       struct notify_change_args *a = args;
33743 +       struct inode *h_inode;
33744 +       struct mnt_idmap *idmap;
33745 +
33746 +       h_inode = d_inode(a->path->dentry);
33747 +       IMustLock(h_inode);
33748 +
33749 +       *a->errp = -EPERM;
33750 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33751 +               idmap = mnt_idmap(a->path->mnt);
33752 +               lockdep_off();
33753 +               *a->errp = notify_change(idmap, a->path->dentry, a->ia,
33754 +                                        a->delegated_inode);
33755 +               lockdep_on();
33756 +               if (!*a->errp)
33757 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33758 +       }
33759 +       AuTraceErr(*a->errp);
33760 +}
33761 +
33762 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33763 +                       struct inode **delegated_inode)
33764 +{
33765 +       int err;
33766 +       struct notify_change_args args = {
33767 +               .errp                   = &err,
33768 +               .path                   = path,
33769 +               .ia                     = ia,
33770 +               .delegated_inode        = delegated_inode
33771 +       };
33772 +
33773 +       call_notify_change(&args);
33774 +
33775 +       return err;
33776 +}
33777 +
33778 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33779 +                           struct inode **delegated_inode)
33780 +{
33781 +       int err, wkq_err;
33782 +       struct notify_change_args args = {
33783 +               .errp                   = &err,
33784 +               .path                   = path,
33785 +               .ia                     = ia,
33786 +               .delegated_inode        = delegated_inode
33787 +       };
33788 +
33789 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33790 +       if (unlikely(wkq_err))
33791 +               err = wkq_err;
33792 +
33793 +       return err;
33794 +}
33795 +
33796 +/* ---------------------------------------------------------------------- */
33797 +
33798 +struct unlink_args {
33799 +       int *errp;
33800 +       struct inode *dir;
33801 +       struct path *path;
33802 +       struct inode **delegated_inode;
33803 +};
33804 +
33805 +static void call_unlink(void *args)
33806 +{
33807 +       struct unlink_args *a = args;
33808 +       struct dentry *d = a->path->dentry;
33809 +       struct inode *h_inode;
33810 +       struct mnt_idmap *idmap;
33811 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33812 +                                     && au_dcount(d) == 1);
33813 +
33814 +       IMustLock(a->dir);
33815 +
33816 +       a->path->dentry = d->d_parent;
33817 +       *a->errp = security_path_unlink(a->path, d);
33818 +       a->path->dentry = d;
33819 +       if (unlikely(*a->errp))
33820 +               return;
33821 +
33822 +       if (!stop_sillyrename)
33823 +               dget(d);
33824 +       h_inode = NULL;
33825 +       if (d_is_positive(d)) {
33826 +               h_inode = d_inode(d);
33827 +               ihold(h_inode);
33828 +       }
33829 +
33830 +       idmap = mnt_idmap(a->path->mnt);
33831 +       lockdep_off();
33832 +       *a->errp = vfs_unlink(idmap, a->dir, d, a->delegated_inode);
33833 +       lockdep_on();
33834 +       if (!*a->errp) {
33835 +               struct path tmp = {
33836 +                       .dentry = d->d_parent,
33837 +                       .mnt    = a->path->mnt
33838 +               };
33839 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33840 +       }
33841 +
33842 +       if (!stop_sillyrename)
33843 +               dput(d);
33844 +       if (h_inode)
33845 +               iput(h_inode);
33846 +
33847 +       AuTraceErr(*a->errp);
33848 +}
33849 +
33850 +/*
33851 + * @dir: must be locked.
33852 + * @dentry: target dentry.
33853 + */
33854 +int vfsub_unlink(struct inode *dir, struct path *path,
33855 +                struct inode **delegated_inode, int force)
33856 +{
33857 +       int err;
33858 +       struct unlink_args args = {
33859 +               .errp                   = &err,
33860 +               .dir                    = dir,
33861 +               .path                   = path,
33862 +               .delegated_inode        = delegated_inode
33863 +       };
33864 +
33865 +       if (!force)
33866 +               call_unlink(&args);
33867 +       else {
33868 +               int wkq_err;
33869 +
33870 +               wkq_err = au_wkq_wait(call_unlink, &args);
33871 +               if (unlikely(wkq_err))
33872 +                       err = wkq_err;
33873 +       }
33874 +
33875 +       return err;
33876 +}
33877 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33878 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33879 +++ linux/fs/aufs/vfsub.h       2023-04-24 08:13:40.969576115 +0200
33880 @@ -0,0 +1,390 @@
33881 +/* SPDX-License-Identifier: GPL-2.0 */
33882 +/*
33883 + * Copyright (C) 2005-2022 Junjiro R. Okajima
33884 + *
33885 + * This program is free software; you can redistribute it and/or modify
33886 + * it under the terms of the GNU General Public License as published by
33887 + * the Free Software Foundation; either version 2 of the License, or
33888 + * (at your option) any later version.
33889 + *
33890 + * This program is distributed in the hope that it will be useful,
33891 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33892 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33893 + * GNU General Public License for more details.
33894 + *
33895 + * You should have received a copy of the GNU General Public License
33896 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33897 + */
33898 +
33899 +/*
33900 + * sub-routines for VFS
33901 + */
33902 +
33903 +#ifndef __AUFS_VFSUB_H__
33904 +#define __AUFS_VFSUB_H__
33905 +
33906 +#ifdef __KERNEL__
33907 +
33908 +#include <linux/fs.h>
33909 +#include <linux/mount.h>
33910 +#include <linux/posix_acl.h>
33911 +#include <linux/xattr.h>
33912 +#include "debug.h"
33913 +
33914 +/* copied from linux/fs/internal.h */
33915 +/* todo: BAD approach!! */
33916 +extern void __mnt_drop_write(struct vfsmount *);
33917 +extern struct file *alloc_empty_file(int, const struct cred *);
33918 +
33919 +/* ---------------------------------------------------------------------- */
33920 +
33921 +/* lock subclass for lower inode */
33922 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33923 +/* reduce? gave up. */
33924 +enum {
33925 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33926 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33927 +       AuLsc_I_PARENT2,        /* copyup dirs */
33928 +       AuLsc_I_PARENT3,        /* copyup wh */
33929 +       AuLsc_I_CHILD,
33930 +       AuLsc_I_CHILD2,
33931 +       AuLsc_I_End
33932 +};
33933 +
33934 +/* to debug easier, do not make them inlined functions */
33935 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33936 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33937 +
33938 +/* ---------------------------------------------------------------------- */
33939 +
33940 +static inline void vfsub_drop_nlink(struct inode *inode)
33941 +{
33942 +       AuDebugOn(!inode->i_nlink);
33943 +       drop_nlink(inode);
33944 +}
33945 +
33946 +static inline void vfsub_dead_dir(struct inode *inode)
33947 +{
33948 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33949 +       inode->i_flags |= S_DEAD;
33950 +       clear_nlink(inode);
33951 +}
33952 +
33953 +static inline int vfsub_native_ro(struct inode *inode)
33954 +{
33955 +       return sb_rdonly(inode->i_sb)
33956 +               || IS_RDONLY(inode)
33957 +               /* || IS_APPEND(inode) */
33958 +               || IS_IMMUTABLE(inode);
33959 +}
33960 +
33961 +#ifdef CONFIG_AUFS_BR_FUSE
33962 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33963 +#else
33964 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33965 +#endif
33966 +
33967 +int vfsub_sync_filesystem(struct super_block *h_sb);
33968 +
33969 +/* ---------------------------------------------------------------------- */
33970 +
33971 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33972 +struct file *vfsub_dentry_open(struct path *path, int flags);
33973 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33974 +struct au_branch;
33975 +struct vfsub_aopen_args {
33976 +       struct file             *file;
33977 +       unsigned int            open_flag;
33978 +       umode_t                 create_mode;
33979 +       struct au_branch        *br;
33980 +};
33981 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33982 +                     struct vfsub_aopen_args *args);
33983 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33984 +
33985 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33986 +                                            struct path *ppath, int len);
33987 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33988 +                                   int len);
33989 +
33990 +struct vfsub_lkup_one_args {
33991 +       struct dentry **errp;
33992 +       struct qstr *name;
33993 +       struct path *ppath;
33994 +};
33995 +
33996 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33997 +                                           struct path *ppath)
33998 +{
33999 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
34000 +}
34001 +
34002 +void vfsub_call_lkup_one(void *args);
34003 +
34004 +/* ---------------------------------------------------------------------- */
34005 +
34006 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
34007 +{
34008 +       int err;
34009 +
34010 +       lockdep_off();
34011 +       err = mnt_want_write(mnt);
34012 +       lockdep_on();
34013 +       return err;
34014 +}
34015 +
34016 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
34017 +{
34018 +       lockdep_off();
34019 +       mnt_drop_write(mnt);
34020 +       lockdep_on();
34021 +}
34022 +
34023 +#if 0 /* reserved */
34024 +static inline void vfsub_mnt_drop_write_file(struct file *file)
34025 +{
34026 +       lockdep_off();
34027 +       mnt_drop_write_file(file);
34028 +       lockdep_on();
34029 +}
34030 +#endif
34031 +
34032 +/* ---------------------------------------------------------------------- */
34033 +
34034 +struct au_hinode;
34035 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
34036 +                                struct dentry *d2, struct au_hinode *hdir2);
34037 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
34038 +                        struct dentry *d2, struct au_hinode *hdir2);
34039 +
34040 +int vfsub_create(struct inode *dir, struct path *path, int mode,
34041 +                bool want_excl);
34042 +int vfsub_symlink(struct inode *dir, struct path *path,
34043 +                 const char *symname);
34044 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
34045 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
34046 +              struct path *path, struct inode **delegated_inode);
34047 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
34048 +                struct inode *hdir, struct path *path,
34049 +                struct inode **delegated_inode, unsigned int flags);
34050 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
34051 +int vfsub_rmdir(struct inode *dir, struct path *path);
34052 +
34053 +/* ---------------------------------------------------------------------- */
34054 +
34055 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
34056 +                    loff_t *ppos);
34057 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
34058 +                       loff_t *ppos);
34059 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
34060 +                     loff_t *ppos);
34061 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
34062 +                     loff_t *ppos);
34063 +int vfsub_flush(struct file *file, fl_owner_t id);
34064 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
34065 +
34066 +static inline loff_t vfsub_f_size_read(struct file *file)
34067 +{
34068 +       return i_size_read(file_inode(file));
34069 +}
34070 +
34071 +static inline unsigned int vfsub_file_flags(struct file *file)
34072 +{
34073 +       unsigned int flags;
34074 +
34075 +       spin_lock(&file->f_lock);
34076 +       flags = file->f_flags;
34077 +       spin_unlock(&file->f_lock);
34078 +
34079 +       return flags;
34080 +}
34081 +
34082 +static inline int vfsub_file_execed(struct file *file)
34083 +{
34084 +       /* todo: direct access f_flags */
34085 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34086 +}
34087 +
34088 +#if 0 /* reserved */
34089 +static inline void vfsub_file_accessed(struct file *h_file)
34090 +{
34091 +       file_accessed(h_file);
34092 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34093 +}
34094 +#endif
34095 +
34096 +#if 0 /* reserved */
34097 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34098 +                                    struct dentry *h_dentry)
34099 +{
34100 +       struct path h_path = {
34101 +               .dentry = h_dentry,
34102 +               .mnt    = h_mnt
34103 +       };
34104 +       touch_atime(&h_path);
34105 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34106 +}
34107 +#endif
34108 +
34109 +static inline int vfsub_update_time(struct inode *h_inode,
34110 +                                   struct timespec64 *ts, int flags)
34111 +{
34112 +       return inode_update_time(h_inode, ts, flags);
34113 +       /* no vfsub_update_h_iattr() since we don't have struct path */
34114 +}
34115 +
34116 +#ifdef CONFIG_FS_POSIX_ACL
34117 +static inline int vfsub_acl_chmod(struct mnt_idmap *h_idmap,
34118 +                                 struct dentry *h_dentry, umode_t h_mode)
34119 +{
34120 +       int err;
34121 +
34122 +       err = posix_acl_chmod(h_idmap, h_dentry, h_mode);
34123 +       if (err == -EOPNOTSUPP)
34124 +               err = 0;
34125 +       return err;
34126 +}
34127 +#else
34128 +AuStubInt0(vfsub_acl_chmod, struct mnt_idmap *h_idmap,
34129 +          struct dentry *h_dentry, umode_t h_mode);
34130 +#endif
34131 +
34132 +long vfsub_splice_to(struct file *in, loff_t *ppos,
34133 +                    struct pipe_inode_info *pipe, size_t len,
34134 +                    unsigned int flags);
34135 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34136 +                      loff_t *ppos, size_t len, unsigned int flags);
34137 +
34138 +static inline long vfsub_truncate(struct path *path, loff_t length)
34139 +{
34140 +       long err;
34141 +
34142 +       lockdep_off();
34143 +       err = vfs_truncate(path, length);
34144 +       lockdep_on();
34145 +       return err;
34146 +}
34147 +
34148 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34149 +               struct file *h_file);
34150 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34151 +
34152 +/*
34153 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34154 + * ioctl.
34155 + */
34156 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34157 +                                           loff_t len)
34158 +{
34159 +       loff_t err;
34160 +
34161 +       lockdep_off();
34162 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34163 +       lockdep_on();
34164 +
34165 +       return err;
34166 +}
34167 +
34168 +/* copy_file_range(2) is a systemcall */
34169 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34170 +                                           struct file *dst, loff_t dst_pos,
34171 +                                           size_t len, unsigned int flags)
34172 +{
34173 +       ssize_t ssz;
34174 +
34175 +       lockdep_off();
34176 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34177 +       lockdep_on();
34178 +
34179 +       return ssz;
34180 +}
34181 +
34182 +/* ---------------------------------------------------------------------- */
34183 +
34184 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34185 +{
34186 +       loff_t err;
34187 +
34188 +       lockdep_off();
34189 +       err = vfs_llseek(file, offset, origin);
34190 +       lockdep_on();
34191 +       return err;
34192 +}
34193 +
34194 +/* ---------------------------------------------------------------------- */
34195 +
34196 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34197 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34198 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34199 +                           struct inode **delegated_inode);
34200 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34201 +                       struct inode **delegated_inode);
34202 +int vfsub_unlink(struct inode *dir, struct path *path,
34203 +                struct inode **delegated_inode, int force);
34204 +
34205 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34206 +{
34207 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34208 +}
34209 +
34210 +/* ---------------------------------------------------------------------- */
34211 +
34212 +static inline int vfsub_setxattr(struct mnt_idmap *idmap,
34213 +                                struct dentry *dentry, const char *name,
34214 +                                const void *value, size_t size, int flags)
34215 +{
34216 +       int err;
34217 +
34218 +       lockdep_off();
34219 +       err = vfs_setxattr(idmap, dentry, name, value, size, flags);
34220 +       lockdep_on();
34221 +
34222 +       return err;
34223 +}
34224 +
34225 +static inline int vfsub_removexattr(struct mnt_idmap *idmap,
34226 +                                   struct dentry *dentry, const char *name)
34227 +{
34228 +       int err;
34229 +
34230 +       lockdep_off();
34231 +       err = vfs_removexattr(idmap, dentry, name);
34232 +       lockdep_on();
34233 +
34234 +       return err;
34235 +}
34236 +
34237 +#ifdef CONFIG_FS_POSIX_ACL
34238 +static inline int vfsub_set_acl(struct mnt_idmap *idmap,
34239 +                               struct dentry *dentry, const char *name,
34240 +                               struct posix_acl *acl)
34241 +{
34242 +       int err;
34243 +
34244 +       lockdep_off();
34245 +       err = vfs_set_acl(idmap, dentry, name, acl);
34246 +       lockdep_on();
34247 +
34248 +       return err;
34249 +}
34250 +
34251 +static inline int vfsub_remove_acl(struct mnt_idmap *idmap,
34252 +                                  struct dentry *dentry, const char *name)
34253 +{
34254 +       int err;
34255 +
34256 +       lockdep_off();
34257 +       err = vfs_remove_acl(idmap, dentry, name);
34258 +       lockdep_on();
34259 +
34260 +       return err;
34261 +}
34262 +#else
34263 +AuStubInt0(vfsub_set_acl, struct mnt_idmap *idmap, struct dentry *dentry,
34264 +          const char *name, struct posix_acl *acl);
34265 +AuStubInt0(vfsub_remove_acl, struct mnt_idmap *idmap,
34266 +          struct dentry *dentry, const char *name);
34267 +#endif
34268 +
34269 +#endif /* __KERNEL__ */
34270 +#endif /* __AUFS_VFSUB_H__ */
34271 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34272 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34273 +++ linux/fs/aufs/wbr_policy.c  2022-11-05 23:02:18.969222617 +0100
34274 @@ -0,0 +1,830 @@
34275 +// SPDX-License-Identifier: GPL-2.0
34276 +/*
34277 + * Copyright (C) 2005-2022 Junjiro R. Okajima
34278 + *
34279 + * This program is free software; you can redistribute it and/or modify
34280 + * it under the terms of the GNU General Public License as published by
34281 + * the Free Software Foundation; either version 2 of the License, or
34282 + * (at your option) any later version.
34283 + *
34284 + * This program is distributed in the hope that it will be useful,
34285 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34286 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34287 + * GNU General Public License for more details.
34288 + *
34289 + * You should have received a copy of the GNU General Public License
34290 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34291 + */
34292 +
34293 +/*
34294 + * policies for selecting one among multiple writable branches
34295 + */
34296 +
34297 +#include <linux/statfs.h>
34298 +#include "aufs.h"
34299 +
34300 +/* subset of cpup_attr() */
34301 +static noinline_for_stack
34302 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34303 +{
34304 +       int err, sbits;
34305 +       struct iattr ia;
34306 +       struct inode *h_isrc;
34307 +
34308 +       h_isrc = d_inode(h_src);
34309 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34310 +       ia.ia_mode = h_isrc->i_mode;
34311 +       ia.ia_uid = h_isrc->i_uid;
34312 +       ia.ia_gid = h_isrc->i_gid;
34313 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34314 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34315 +       /* no delegation since it is just created */
34316 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34317 +
34318 +       /* is this nfs only? */
34319 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34320 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34321 +               ia.ia_mode = h_isrc->i_mode;
34322 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34323 +       }
34324 +
34325 +       return err;
34326 +}
34327 +
34328 +#define AuCpdown_PARENT_OPQ    1
34329 +#define AuCpdown_WHED          (1 << 1)
34330 +#define AuCpdown_MADE_DIR      (1 << 2)
34331 +#define AuCpdown_DIROPQ                (1 << 3)
34332 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34333 +#define au_fset_cpdown(flags, name) \
34334 +       do { (flags) |= AuCpdown_##name; } while (0)
34335 +#define au_fclr_cpdown(flags, name) \
34336 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34337 +
34338 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34339 +                            unsigned int *flags)
34340 +{
34341 +       int err;
34342 +       struct dentry *opq_dentry;
34343 +
34344 +       opq_dentry = au_diropq_create(dentry, bdst);
34345 +       err = PTR_ERR(opq_dentry);
34346 +       if (IS_ERR(opq_dentry))
34347 +               goto out;
34348 +       dput(opq_dentry);
34349 +       au_fset_cpdown(*flags, DIROPQ);
34350 +
34351 +out:
34352 +       return err;
34353 +}
34354 +
34355 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34356 +                           struct inode *dir, aufs_bindex_t bdst)
34357 +{
34358 +       int err;
34359 +       struct path h_path;
34360 +       struct au_branch *br;
34361 +
34362 +       br = au_sbr(dentry->d_sb, bdst);
34363 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34364 +       err = PTR_ERR(h_path.dentry);
34365 +       if (IS_ERR(h_path.dentry))
34366 +               goto out;
34367 +
34368 +       err = 0;
34369 +       if (d_is_positive(h_path.dentry)) {
34370 +               h_path.mnt = au_br_mnt(br);
34371 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34372 +                                         dentry);
34373 +       }
34374 +       dput(h_path.dentry);
34375 +
34376 +out:
34377 +       return err;
34378 +}
34379 +
34380 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34381 +                        struct au_pin *pin,
34382 +                        struct dentry *h_parent, void *arg)
34383 +{
34384 +       int err, rerr;
34385 +       aufs_bindex_t bopq, btop;
34386 +       struct path h_path;
34387 +       struct dentry *parent;
34388 +       struct inode *h_dir, *h_inode, *inode, *dir;
34389 +       unsigned int *flags = arg;
34390 +
34391 +       btop = au_dbtop(dentry);
34392 +       /* dentry is di-locked */
34393 +       parent = dget_parent(dentry);
34394 +       dir = d_inode(parent);
34395 +       h_dir = d_inode(h_parent);
34396 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34397 +       IMustLock(h_dir);
34398 +
34399 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34400 +       if (unlikely(err < 0))
34401 +               goto out;
34402 +       h_path.dentry = au_h_dptr(dentry, bdst);
34403 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34404 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34405 +       if (unlikely(err))
34406 +               goto out_put;
34407 +       au_fset_cpdown(*flags, MADE_DIR);
34408 +
34409 +       bopq = au_dbdiropq(dentry);
34410 +       au_fclr_cpdown(*flags, WHED);
34411 +       au_fclr_cpdown(*flags, DIROPQ);
34412 +       if (au_dbwh(dentry) == bdst)
34413 +               au_fset_cpdown(*flags, WHED);
34414 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34415 +               au_fset_cpdown(*flags, PARENT_OPQ);
34416 +       h_inode = d_inode(h_path.dentry);
34417 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34418 +       if (au_ftest_cpdown(*flags, WHED)) {
34419 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34420 +               if (unlikely(err)) {
34421 +                       inode_unlock(h_inode);
34422 +                       goto out_dir;
34423 +               }
34424 +       }
34425 +
34426 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34427 +       inode_unlock(h_inode);
34428 +       if (unlikely(err))
34429 +               goto out_opq;
34430 +
34431 +       if (au_ftest_cpdown(*flags, WHED)) {
34432 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34433 +               if (unlikely(err))
34434 +                       goto out_opq;
34435 +       }
34436 +
34437 +       inode = d_inode(dentry);
34438 +       if (au_ibbot(inode) < bdst)
34439 +               au_set_ibbot(inode, bdst);
34440 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34441 +                     au_hi_flags(inode, /*isdir*/1));
34442 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34443 +       goto out; /* success */
34444 +
34445 +       /* revert */
34446 +out_opq:
34447 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34448 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34449 +               rerr = au_diropq_remove(dentry, bdst);
34450 +               inode_unlock(h_inode);
34451 +               if (unlikely(rerr)) {
34452 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34453 +                               dentry, bdst, rerr);
34454 +                       err = -EIO;
34455 +                       goto out;
34456 +               }
34457 +       }
34458 +out_dir:
34459 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34460 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34461 +               if (unlikely(rerr)) {
34462 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34463 +                               dentry, bdst, rerr);
34464 +                       err = -EIO;
34465 +               }
34466 +       }
34467 +out_put:
34468 +       au_set_h_dptr(dentry, bdst, NULL);
34469 +       if (au_dbbot(dentry) == bdst)
34470 +               au_update_dbbot(dentry);
34471 +out:
34472 +       dput(parent);
34473 +       return err;
34474 +}
34475 +
34476 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34477 +{
34478 +       int err;
34479 +       unsigned int flags;
34480 +
34481 +       flags = 0;
34482 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34483 +
34484 +       return err;
34485 +}
34486 +
34487 +/* ---------------------------------------------------------------------- */
34488 +
34489 +/* policies for create */
34490 +
34491 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34492 +{
34493 +       int err, i, j, ndentry;
34494 +       aufs_bindex_t bopq;
34495 +       struct au_dcsub_pages dpages;
34496 +       struct au_dpage *dpage;
34497 +       struct dentry **dentries, *parent, *d;
34498 +
34499 +       err = au_dpages_init(&dpages, GFP_NOFS);
34500 +       if (unlikely(err))
34501 +               goto out;
34502 +       parent = dget_parent(dentry);
34503 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34504 +       if (unlikely(err))
34505 +               goto out_free;
34506 +
34507 +       err = bindex;
34508 +       for (i = 0; i < dpages.ndpage; i++) {
34509 +               dpage = dpages.dpages + i;
34510 +               dentries = dpage->dentries;
34511 +               ndentry = dpage->ndentry;
34512 +               for (j = 0; j < ndentry; j++) {
34513 +                       d = dentries[j];
34514 +                       di_read_lock_parent2(d, !AuLock_IR);
34515 +                       bopq = au_dbdiropq(d);
34516 +                       di_read_unlock(d, !AuLock_IR);
34517 +                       if (bopq >= 0 && bopq < err)
34518 +                               err = bopq;
34519 +               }
34520 +       }
34521 +
34522 +out_free:
34523 +       dput(parent);
34524 +       au_dpages_free(&dpages);
34525 +out:
34526 +       return err;
34527 +}
34528 +
34529 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34530 +{
34531 +       for (; bindex >= 0; bindex--)
34532 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34533 +                       return bindex;
34534 +       return -EROFS;
34535 +}
34536 +
34537 +/* top down parent */
34538 +static int au_wbr_create_tdp(struct dentry *dentry,
34539 +                            unsigned int flags __maybe_unused)
34540 +{
34541 +       int err;
34542 +       aufs_bindex_t btop, bindex;
34543 +       struct super_block *sb;
34544 +       struct dentry *parent, *h_parent;
34545 +
34546 +       sb = dentry->d_sb;
34547 +       btop = au_dbtop(dentry);
34548 +       err = btop;
34549 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34550 +               goto out;
34551 +
34552 +       err = -EROFS;
34553 +       parent = dget_parent(dentry);
34554 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34555 +               h_parent = au_h_dptr(parent, bindex);
34556 +               if (!h_parent || d_is_negative(h_parent))
34557 +                       continue;
34558 +
34559 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34560 +                       err = bindex;
34561 +                       break;
34562 +               }
34563 +       }
34564 +       dput(parent);
34565 +
34566 +       /* bottom up here */
34567 +       if (unlikely(err < 0)) {
34568 +               err = au_wbr_bu(sb, btop - 1);
34569 +               if (err >= 0)
34570 +                       err = au_wbr_nonopq(dentry, err);
34571 +       }
34572 +
34573 +out:
34574 +       AuDbg("b%d\n", err);
34575 +       return err;
34576 +}
34577 +
34578 +/* ---------------------------------------------------------------------- */
34579 +
34580 +/* an exception for the policy other than tdp */
34581 +static int au_wbr_create_exp(struct dentry *dentry)
34582 +{
34583 +       int err;
34584 +       aufs_bindex_t bwh, bdiropq;
34585 +       struct dentry *parent;
34586 +
34587 +       err = -1;
34588 +       bwh = au_dbwh(dentry);
34589 +       parent = dget_parent(dentry);
34590 +       bdiropq = au_dbdiropq(parent);
34591 +       if (bwh >= 0) {
34592 +               if (bdiropq >= 0)
34593 +                       err = min(bdiropq, bwh);
34594 +               else
34595 +                       err = bwh;
34596 +               AuDbg("%d\n", err);
34597 +       } else if (bdiropq >= 0) {
34598 +               err = bdiropq;
34599 +               AuDbg("%d\n", err);
34600 +       }
34601 +       dput(parent);
34602 +
34603 +       if (err >= 0)
34604 +               err = au_wbr_nonopq(dentry, err);
34605 +
34606 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34607 +               err = -1;
34608 +
34609 +       AuDbg("%d\n", err);
34610 +       return err;
34611 +}
34612 +
34613 +/* ---------------------------------------------------------------------- */
34614 +
34615 +/* round robin */
34616 +static int au_wbr_create_init_rr(struct super_block *sb)
34617 +{
34618 +       int err;
34619 +
34620 +       err = au_wbr_bu(sb, au_sbbot(sb));
34621 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34622 +       /* smp_mb(); */
34623 +
34624 +       AuDbg("b%d\n", err);
34625 +       return err;
34626 +}
34627 +
34628 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34629 +{
34630 +       int err, nbr;
34631 +       unsigned int u;
34632 +       aufs_bindex_t bindex, bbot;
34633 +       struct super_block *sb;
34634 +       atomic_t *next;
34635 +
34636 +       err = au_wbr_create_exp(dentry);
34637 +       if (err >= 0)
34638 +               goto out;
34639 +
34640 +       sb = dentry->d_sb;
34641 +       next = &au_sbi(sb)->si_wbr_rr_next;
34642 +       bbot = au_sbbot(sb);
34643 +       nbr = bbot + 1;
34644 +       for (bindex = 0; bindex <= bbot; bindex++) {
34645 +               if (!au_ftest_wbr(flags, DIR)) {
34646 +                       err = atomic_dec_return(next) + 1;
34647 +                       /* modulo for 0 is meaningless */
34648 +                       if (unlikely(!err))
34649 +                               err = atomic_dec_return(next) + 1;
34650 +               } else
34651 +                       err = atomic_read(next);
34652 +               AuDbg("%d\n", err);
34653 +               u = err;
34654 +               err = u % nbr;
34655 +               AuDbg("%d\n", err);
34656 +               if (!au_br_rdonly(au_sbr(sb, err)))
34657 +                       break;
34658 +               err = -EROFS;
34659 +       }
34660 +
34661 +       if (err >= 0)
34662 +               err = au_wbr_nonopq(dentry, err);
34663 +
34664 +out:
34665 +       AuDbg("%d\n", err);
34666 +       return err;
34667 +}
34668 +
34669 +/* ---------------------------------------------------------------------- */
34670 +
34671 +/* most free space */
34672 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34673 +{
34674 +       struct super_block *sb;
34675 +       struct au_branch *br;
34676 +       struct au_wbr_mfs *mfs;
34677 +       struct dentry *h_parent;
34678 +       aufs_bindex_t bindex, bbot;
34679 +       int err;
34680 +       unsigned long long b, bavail;
34681 +       struct path h_path;
34682 +       /* reduce the stack usage */
34683 +       struct kstatfs *st;
34684 +
34685 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34686 +       if (unlikely(!st)) {
34687 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34688 +               return;
34689 +       }
34690 +
34691 +       bavail = 0;
34692 +       sb = dentry->d_sb;
34693 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34694 +       MtxMustLock(&mfs->mfs_lock);
34695 +       mfs->mfs_bindex = -EROFS;
34696 +       mfs->mfsrr_bytes = 0;
34697 +       if (!parent) {
34698 +               bindex = 0;
34699 +               bbot = au_sbbot(sb);
34700 +       } else {
34701 +               bindex = au_dbtop(parent);
34702 +               bbot = au_dbtaildir(parent);
34703 +       }
34704 +
34705 +       for (; bindex <= bbot; bindex++) {
34706 +               if (parent) {
34707 +                       h_parent = au_h_dptr(parent, bindex);
34708 +                       if (!h_parent || d_is_negative(h_parent))
34709 +                               continue;
34710 +               }
34711 +               br = au_sbr(sb, bindex);
34712 +               if (au_br_rdonly(br))
34713 +                       continue;
34714 +
34715 +               /* sb->s_root for NFS is unreliable */
34716 +               h_path.mnt = au_br_mnt(br);
34717 +               h_path.dentry = h_path.mnt->mnt_root;
34718 +               err = vfs_statfs(&h_path, st);
34719 +               if (unlikely(err)) {
34720 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34721 +                       continue;
34722 +               }
34723 +
34724 +               /* when the available size is equal, select the lower one */
34725 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34726 +                            || sizeof(b) < sizeof(st->f_bsize));
34727 +               b = st->f_bavail * st->f_bsize;
34728 +               br->br_wbr->wbr_bytes = b;
34729 +               if (b >= bavail) {
34730 +                       bavail = b;
34731 +                       mfs->mfs_bindex = bindex;
34732 +                       mfs->mfs_jiffy = jiffies;
34733 +               }
34734 +       }
34735 +
34736 +       mfs->mfsrr_bytes = bavail;
34737 +       AuDbg("b%d\n", mfs->mfs_bindex);
34738 +       au_kfree_rcu(st);
34739 +}
34740 +
34741 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34742 +{
34743 +       int err;
34744 +       struct dentry *parent;
34745 +       struct super_block *sb;
34746 +       struct au_wbr_mfs *mfs;
34747 +
34748 +       err = au_wbr_create_exp(dentry);
34749 +       if (err >= 0)
34750 +               goto out;
34751 +
34752 +       sb = dentry->d_sb;
34753 +       parent = NULL;
34754 +       if (au_ftest_wbr(flags, PARENT))
34755 +               parent = dget_parent(dentry);
34756 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34757 +       mutex_lock(&mfs->mfs_lock);
34758 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34759 +           || mfs->mfs_bindex < 0
34760 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34761 +               au_mfs(dentry, parent);
34762 +       mutex_unlock(&mfs->mfs_lock);
34763 +       err = mfs->mfs_bindex;
34764 +       dput(parent);
34765 +
34766 +       if (err >= 0)
34767 +               err = au_wbr_nonopq(dentry, err);
34768 +
34769 +out:
34770 +       AuDbg("b%d\n", err);
34771 +       return err;
34772 +}
34773 +
34774 +static int au_wbr_create_init_mfs(struct super_block *sb)
34775 +{
34776 +       struct au_wbr_mfs *mfs;
34777 +
34778 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34779 +       mutex_init(&mfs->mfs_lock);
34780 +       mfs->mfs_jiffy = 0;
34781 +       mfs->mfs_bindex = -EROFS;
34782 +
34783 +       return 0;
34784 +}
34785 +
34786 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34787 +{
34788 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34789 +       return 0;
34790 +}
34791 +
34792 +/* ---------------------------------------------------------------------- */
34793 +
34794 +/* top down regardless parent, and then mfs */
34795 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34796 +                              unsigned int flags __maybe_unused)
34797 +{
34798 +       int err;
34799 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34800 +       unsigned long long watermark;
34801 +       struct super_block *sb;
34802 +       struct au_wbr_mfs *mfs;
34803 +       struct au_branch *br;
34804 +       struct dentry *parent;
34805 +
34806 +       sb = dentry->d_sb;
34807 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34808 +       mutex_lock(&mfs->mfs_lock);
34809 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34810 +           || mfs->mfs_bindex < 0)
34811 +               au_mfs(dentry, /*parent*/NULL);
34812 +       watermark = mfs->mfsrr_watermark;
34813 +       bmfs = mfs->mfs_bindex;
34814 +       mutex_unlock(&mfs->mfs_lock);
34815 +
34816 +       /* another style of au_wbr_create_exp() */
34817 +       bwh = au_dbwh(dentry);
34818 +       parent = dget_parent(dentry);
34819 +       btail = au_dbtaildir(parent);
34820 +       if (bwh >= 0 && bwh < btail)
34821 +               btail = bwh;
34822 +
34823 +       err = au_wbr_nonopq(dentry, btail);
34824 +       if (unlikely(err < 0))
34825 +               goto out;
34826 +       btail = err;
34827 +       bfound = -1;
34828 +       for (bindex = 0; bindex <= btail; bindex++) {
34829 +               br = au_sbr(sb, bindex);
34830 +               if (au_br_rdonly(br))
34831 +                       continue;
34832 +               if (br->br_wbr->wbr_bytes > watermark) {
34833 +                       bfound = bindex;
34834 +                       break;
34835 +               }
34836 +       }
34837 +       err = bfound;
34838 +       if (err < 0)
34839 +               err = bmfs;
34840 +
34841 +out:
34842 +       dput(parent);
34843 +       AuDbg("b%d\n", err);
34844 +       return err;
34845 +}
34846 +
34847 +/* ---------------------------------------------------------------------- */
34848 +
34849 +/* most free space and then round robin */
34850 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34851 +{
34852 +       int err;
34853 +       struct au_wbr_mfs *mfs;
34854 +
34855 +       err = au_wbr_create_mfs(dentry, flags);
34856 +       if (err >= 0) {
34857 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34858 +               mutex_lock(&mfs->mfs_lock);
34859 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34860 +                       err = au_wbr_create_rr(dentry, flags);
34861 +               mutex_unlock(&mfs->mfs_lock);
34862 +       }
34863 +
34864 +       AuDbg("b%d\n", err);
34865 +       return err;
34866 +}
34867 +
34868 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34869 +{
34870 +       int err;
34871 +
34872 +       au_wbr_create_init_mfs(sb); /* ignore */
34873 +       err = au_wbr_create_init_rr(sb);
34874 +
34875 +       return err;
34876 +}
34877 +
34878 +/* ---------------------------------------------------------------------- */
34879 +
34880 +/* top down parent and most free space */
34881 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34882 +{
34883 +       int err, e2;
34884 +       unsigned long long b;
34885 +       aufs_bindex_t bindex, btop, bbot;
34886 +       struct super_block *sb;
34887 +       struct dentry *parent, *h_parent;
34888 +       struct au_branch *br;
34889 +
34890 +       err = au_wbr_create_tdp(dentry, flags);
34891 +       if (unlikely(err < 0))
34892 +               goto out;
34893 +       parent = dget_parent(dentry);
34894 +       btop = au_dbtop(parent);
34895 +       bbot = au_dbtaildir(parent);
34896 +       if (btop == bbot)
34897 +               goto out_parent; /* success */
34898 +
34899 +       e2 = au_wbr_create_mfs(dentry, flags);
34900 +       if (e2 < 0)
34901 +               goto out_parent; /* success */
34902 +
34903 +       /* when the available size is equal, select upper one */
34904 +       sb = dentry->d_sb;
34905 +       br = au_sbr(sb, err);
34906 +       b = br->br_wbr->wbr_bytes;
34907 +       AuDbg("b%d, %llu\n", err, b);
34908 +
34909 +       for (bindex = btop; bindex <= bbot; bindex++) {
34910 +               h_parent = au_h_dptr(parent, bindex);
34911 +               if (!h_parent || d_is_negative(h_parent))
34912 +                       continue;
34913 +
34914 +               br = au_sbr(sb, bindex);
34915 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34916 +                       b = br->br_wbr->wbr_bytes;
34917 +                       err = bindex;
34918 +                       AuDbg("b%d, %llu\n", err, b);
34919 +               }
34920 +       }
34921 +
34922 +       if (err >= 0)
34923 +               err = au_wbr_nonopq(dentry, err);
34924 +
34925 +out_parent:
34926 +       dput(parent);
34927 +out:
34928 +       AuDbg("b%d\n", err);
34929 +       return err;
34930 +}
34931 +
34932 +/* ---------------------------------------------------------------------- */
34933 +
34934 +/*
34935 + * - top down parent
34936 + * - most free space with parent
34937 + * - most free space round-robin regardless parent
34938 + */
34939 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34940 +{
34941 +       int err;
34942 +       unsigned long long watermark;
34943 +       struct super_block *sb;
34944 +       struct au_branch *br;
34945 +       struct au_wbr_mfs *mfs;
34946 +
34947 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34948 +       if (unlikely(err < 0))
34949 +               goto out;
34950 +
34951 +       sb = dentry->d_sb;
34952 +       br = au_sbr(sb, err);
34953 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34954 +       mutex_lock(&mfs->mfs_lock);
34955 +       watermark = mfs->mfsrr_watermark;
34956 +       mutex_unlock(&mfs->mfs_lock);
34957 +       if (br->br_wbr->wbr_bytes < watermark)
34958 +               /* regardless the parent dir */
34959 +               err = au_wbr_create_mfsrr(dentry, flags);
34960 +
34961 +out:
34962 +       AuDbg("b%d\n", err);
34963 +       return err;
34964 +}
34965 +
34966 +/* ---------------------------------------------------------------------- */
34967 +
34968 +/* policies for copyup */
34969 +
34970 +/* top down parent */
34971 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34972 +{
34973 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34974 +}
34975 +
34976 +/* bottom up parent */
34977 +static int au_wbr_copyup_bup(struct dentry *dentry)
34978 +{
34979 +       int err;
34980 +       aufs_bindex_t bindex, btop;
34981 +       struct dentry *parent, *h_parent;
34982 +       struct super_block *sb;
34983 +
34984 +       err = -EROFS;
34985 +       sb = dentry->d_sb;
34986 +       parent = dget_parent(dentry);
34987 +       btop = au_dbtop(parent);
34988 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34989 +               h_parent = au_h_dptr(parent, bindex);
34990 +               if (!h_parent || d_is_negative(h_parent))
34991 +                       continue;
34992 +
34993 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34994 +                       err = bindex;
34995 +                       break;
34996 +               }
34997 +       }
34998 +       dput(parent);
34999 +
35000 +       /* bottom up here */
35001 +       if (unlikely(err < 0))
35002 +               err = au_wbr_bu(sb, btop - 1);
35003 +
35004 +       AuDbg("b%d\n", err);
35005 +       return err;
35006 +}
35007 +
35008 +/* bottom up */
35009 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
35010 +{
35011 +       int err;
35012 +
35013 +       err = au_wbr_bu(dentry->d_sb, btop);
35014 +       AuDbg("b%d\n", err);
35015 +       if (err > btop)
35016 +               err = au_wbr_nonopq(dentry, err);
35017 +
35018 +       AuDbg("b%d\n", err);
35019 +       return err;
35020 +}
35021 +
35022 +static int au_wbr_copyup_bu(struct dentry *dentry)
35023 +{
35024 +       int err;
35025 +       aufs_bindex_t btop;
35026 +
35027 +       btop = au_dbtop(dentry);
35028 +       err = au_wbr_do_copyup_bu(dentry, btop);
35029 +       return err;
35030 +}
35031 +
35032 +/* ---------------------------------------------------------------------- */
35033 +
35034 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
35035 +       [AuWbrCopyup_TDP] = {
35036 +               .copyup = au_wbr_copyup_tdp
35037 +       },
35038 +       [AuWbrCopyup_BUP] = {
35039 +               .copyup = au_wbr_copyup_bup
35040 +       },
35041 +       [AuWbrCopyup_BU] = {
35042 +               .copyup = au_wbr_copyup_bu
35043 +       }
35044 +};
35045 +
35046 +struct au_wbr_create_operations au_wbr_create_ops[] = {
35047 +       [AuWbrCreate_TDP] = {
35048 +               .create = au_wbr_create_tdp
35049 +       },
35050 +       [AuWbrCreate_RR] = {
35051 +               .create = au_wbr_create_rr,
35052 +               .init   = au_wbr_create_init_rr
35053 +       },
35054 +       [AuWbrCreate_MFS] = {
35055 +               .create = au_wbr_create_mfs,
35056 +               .init   = au_wbr_create_init_mfs,
35057 +               .fin    = au_wbr_create_fin_mfs
35058 +       },
35059 +       [AuWbrCreate_MFSV] = {
35060 +               .create = au_wbr_create_mfs,
35061 +               .init   = au_wbr_create_init_mfs,
35062 +               .fin    = au_wbr_create_fin_mfs
35063 +       },
35064 +       [AuWbrCreate_MFSRR] = {
35065 +               .create = au_wbr_create_mfsrr,
35066 +               .init   = au_wbr_create_init_mfsrr,
35067 +               .fin    = au_wbr_create_fin_mfs
35068 +       },
35069 +       [AuWbrCreate_MFSRRV] = {
35070 +               .create = au_wbr_create_mfsrr,
35071 +               .init   = au_wbr_create_init_mfsrr,
35072 +               .fin    = au_wbr_create_fin_mfs
35073 +       },
35074 +       [AuWbrCreate_TDMFS] = {
35075 +               .create = au_wbr_create_tdmfs,
35076 +               .init   = au_wbr_create_init_mfs,
35077 +               .fin    = au_wbr_create_fin_mfs
35078 +       },
35079 +       [AuWbrCreate_TDMFSV] = {
35080 +               .create = au_wbr_create_tdmfs,
35081 +               .init   = au_wbr_create_init_mfs,
35082 +               .fin    = au_wbr_create_fin_mfs
35083 +       },
35084 +       [AuWbrCreate_PMFS] = {
35085 +               .create = au_wbr_create_pmfs,
35086 +               .init   = au_wbr_create_init_mfs,
35087 +               .fin    = au_wbr_create_fin_mfs
35088 +       },
35089 +       [AuWbrCreate_PMFSV] = {
35090 +               .create = au_wbr_create_pmfs,
35091 +               .init   = au_wbr_create_init_mfs,
35092 +               .fin    = au_wbr_create_fin_mfs
35093 +       },
35094 +       [AuWbrCreate_PMFSRR] = {
35095 +               .create = au_wbr_create_pmfsrr,
35096 +               .init   = au_wbr_create_init_mfsrr,
35097 +               .fin    = au_wbr_create_fin_mfs
35098 +       },
35099 +       [AuWbrCreate_PMFSRRV] = {
35100 +               .create = au_wbr_create_pmfsrr,
35101 +               .init   = au_wbr_create_init_mfsrr,
35102 +               .fin    = au_wbr_create_fin_mfs
35103 +       }
35104 +};
35105 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
35106 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
35107 +++ linux/fs/aufs/whout.c       2023-04-24 08:13:40.972909448 +0200
35108 @@ -0,0 +1,1072 @@
35109 +// SPDX-License-Identifier: GPL-2.0
35110 +/*
35111 + * Copyright (C) 2005-2022 Junjiro R. Okajima
35112 + *
35113 + * This program is free software; you can redistribute it and/or modify
35114 + * it under the terms of the GNU General Public License as published by
35115 + * the Free Software Foundation; either version 2 of the License, or
35116 + * (at your option) any later version.
35117 + *
35118 + * This program is distributed in the hope that it will be useful,
35119 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35120 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35121 + * GNU General Public License for more details.
35122 + *
35123 + * You should have received a copy of the GNU General Public License
35124 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35125 + */
35126 +
35127 +/*
35128 + * whiteout for logical deletion and opaque directory
35129 + */
35130 +
35131 +#include "aufs.h"
35132 +
35133 +#define WH_MASK                        0444
35134 +
35135 +/*
35136 + * If a directory contains this file, then it is opaque.  We start with the
35137 + * .wh. flag so that it is blocked by lookup.
35138 + */
35139 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35140 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
35141 +
35142 +/*
35143 + * generate whiteout name, which is NOT terminated by NULL.
35144 + * @name: original d_name.name
35145 + * @len: original d_name.len
35146 + * @wh: whiteout qstr
35147 + * returns zero when succeeds, otherwise error.
35148 + * succeeded value as wh->name should be freed by kfree().
35149 + */
35150 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35151 +{
35152 +       char *p;
35153 +
35154 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35155 +               return -ENAMETOOLONG;
35156 +
35157 +       wh->len = name->len + AUFS_WH_PFX_LEN;
35158 +       p = kmalloc(wh->len, GFP_NOFS);
35159 +       wh->name = p;
35160 +       if (p) {
35161 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35162 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35163 +               /* smp_mb(); */
35164 +               return 0;
35165 +       }
35166 +       return -ENOMEM;
35167 +}
35168 +
35169 +/* ---------------------------------------------------------------------- */
35170 +
35171 +/*
35172 + * test if the @wh_name exists under @h_ppath.
35173 + * @try_sio specifies the necessary of super-io.
35174 + */
35175 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
35176 +              struct qstr *wh_name, int try_sio)
35177 +{
35178 +       int err;
35179 +       struct dentry *wh_dentry;
35180 +
35181 +       if (!try_sio)
35182 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
35183 +       else
35184 +               wh_dentry = au_sio_lkup_one(h_idmap, wh_name, h_ppath);
35185 +       err = PTR_ERR(wh_dentry);
35186 +       if (IS_ERR(wh_dentry)) {
35187 +               if (err == -ENAMETOOLONG)
35188 +                       err = 0;
35189 +               goto out;
35190 +       }
35191 +
35192 +       err = 0;
35193 +       if (d_is_negative(wh_dentry))
35194 +               goto out_wh; /* success */
35195 +
35196 +       err = 1;
35197 +       if (d_is_reg(wh_dentry))
35198 +               goto out_wh; /* success */
35199 +
35200 +       err = -EIO;
35201 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35202 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35203 +
35204 +out_wh:
35205 +       dput(wh_dentry);
35206 +out:
35207 +       return err;
35208 +}
35209 +
35210 +/*
35211 + * test if the @h_path->dentry sets opaque or not.
35212 + */
35213 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path)
35214 +{
35215 +       int err;
35216 +       struct inode *h_dir;
35217 +
35218 +       h_dir = d_inode(h_path->dentry);
35219 +       err = au_wh_test(h_idmap, h_path, &diropq_name,
35220 +                        au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC));
35221 +       return err;
35222 +}
35223 +
35224 +/*
35225 + * returns a negative dentry whose name is unique and temporary.
35226 + */
35227 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35228 +                            struct qstr *prefix)
35229 +{
35230 +       struct dentry *dentry;
35231 +       int i;
35232 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35233 +               *name, *p;
35234 +       /* strict atomic_t is unnecessary here */
35235 +       static unsigned short cnt;
35236 +       struct qstr qs;
35237 +       struct path h_ppath;
35238 +       struct mnt_idmap *h_idmap;
35239 +
35240 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35241 +
35242 +       name = defname;
35243 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35244 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35245 +               dentry = ERR_PTR(-ENAMETOOLONG);
35246 +               if (unlikely(qs.len > NAME_MAX))
35247 +                       goto out;
35248 +               dentry = ERR_PTR(-ENOMEM);
35249 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35250 +               if (unlikely(!name))
35251 +                       goto out;
35252 +       }
35253 +
35254 +       /* doubly whiteout-ed */
35255 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35256 +       p = name + AUFS_WH_PFX_LEN * 2;
35257 +       memcpy(p, prefix->name, prefix->len);
35258 +       p += prefix->len;
35259 +       *p++ = '.';
35260 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35261 +
35262 +       h_ppath.dentry = h_parent;
35263 +       h_ppath.mnt = au_br_mnt(br);
35264 +       h_idmap = au_br_idmap(br);
35265 +       qs.name = name;
35266 +       for (i = 0; i < 3; i++) {
35267 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35268 +               dentry = au_sio_lkup_one(h_idmap, &qs, &h_ppath);
35269 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35270 +                       goto out_name;
35271 +               dput(dentry);
35272 +       }
35273 +       /* pr_warn("could not get random name\n"); */
35274 +       dentry = ERR_PTR(-EEXIST);
35275 +       AuDbg("%.*s\n", AuLNPair(&qs));
35276 +       BUG();
35277 +
35278 +out_name:
35279 +       if (name != defname)
35280 +               au_kfree_try_rcu(name);
35281 +out:
35282 +       AuTraceErrPtr(dentry);
35283 +       return dentry;
35284 +}
35285 +
35286 +/*
35287 + * rename the @h_dentry on @br to the whiteouted temporary name.
35288 + */
35289 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35290 +{
35291 +       int err;
35292 +       struct path h_path = {
35293 +               .mnt = au_br_mnt(br)
35294 +       };
35295 +       struct inode *h_dir, *delegated;
35296 +       struct dentry *h_parent;
35297 +
35298 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35299 +       h_dir = d_inode(h_parent);
35300 +       IMustLock(h_dir);
35301 +
35302 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35303 +       err = PTR_ERR(h_path.dentry);
35304 +       if (IS_ERR(h_path.dentry))
35305 +               goto out;
35306 +
35307 +       /* under the same dir, no need to lock_rename() */
35308 +       delegated = NULL;
35309 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35310 +                          /*flags*/0);
35311 +       AuTraceErr(err);
35312 +       if (unlikely(err == -EWOULDBLOCK)) {
35313 +               pr_warn("cannot retry for NFSv4 delegation"
35314 +                       " for an internal rename\n");
35315 +               iput(delegated);
35316 +       }
35317 +       dput(h_path.dentry);
35318 +
35319 +out:
35320 +       AuTraceErr(err);
35321 +       return err;
35322 +}
35323 +
35324 +/* ---------------------------------------------------------------------- */
35325 +/*
35326 + * functions for removing a whiteout
35327 + */
35328 +
35329 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35330 +{
35331 +       int err, force;
35332 +       struct inode *delegated;
35333 +
35334 +       /*
35335 +        * forces superio when the dir has a sticky bit.
35336 +        * this may be a violation of unix fs semantics.
35337 +        */
35338 +       force = (h_dir->i_mode & S_ISVTX)
35339 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35340 +       delegated = NULL;
35341 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35342 +       if (unlikely(err == -EWOULDBLOCK)) {
35343 +               pr_warn("cannot retry for NFSv4 delegation"
35344 +                       " for an internal unlink\n");
35345 +               iput(delegated);
35346 +       }
35347 +       return err;
35348 +}
35349 +
35350 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35351 +                       struct dentry *dentry)
35352 +{
35353 +       int err;
35354 +
35355 +       err = do_unlink_wh(h_dir, h_path);
35356 +       if (!err && dentry)
35357 +               au_set_dbwh(dentry, -1);
35358 +
35359 +       return err;
35360 +}
35361 +
35362 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35363 +{
35364 +       int err;
35365 +       struct path h_path;
35366 +
35367 +       err = 0;
35368 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35369 +       if (IS_ERR(h_path.dentry))
35370 +               err = PTR_ERR(h_path.dentry);
35371 +       else {
35372 +               if (d_is_reg(h_path.dentry)) {
35373 +                       h_path.mnt = h_ppath->mnt;
35374 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35375 +               }
35376 +               dput(h_path.dentry);
35377 +       }
35378 +
35379 +       return err;
35380 +}
35381 +
35382 +/* ---------------------------------------------------------------------- */
35383 +/*
35384 + * initialize/clean whiteout for a branch
35385 + */
35386 +
35387 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35388 +                       const int isdir)
35389 +{
35390 +       int err;
35391 +       struct inode *delegated;
35392 +
35393 +       if (d_is_negative(whpath->dentry))
35394 +               return;
35395 +
35396 +       if (isdir)
35397 +               err = vfsub_rmdir(h_dir, whpath);
35398 +       else {
35399 +               delegated = NULL;
35400 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35401 +               if (unlikely(err == -EWOULDBLOCK)) {
35402 +                       pr_warn("cannot retry for NFSv4 delegation"
35403 +                               " for an internal unlink\n");
35404 +                       iput(delegated);
35405 +               }
35406 +       }
35407 +       if (unlikely(err))
35408 +               pr_warn("failed removing %pd (%d), ignored.\n",
35409 +                       whpath->dentry, err);
35410 +}
35411 +
35412 +static int test_linkable(struct dentry *h_root)
35413 +{
35414 +       struct inode *h_dir = d_inode(h_root);
35415 +
35416 +       if (h_dir->i_op->link)
35417 +               return 0;
35418 +
35419 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35420 +              h_root, au_sbtype(h_root->d_sb));
35421 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35422 +}
35423 +
35424 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35425 +static int au_whdir(struct inode *h_dir, struct path *path)
35426 +{
35427 +       int err;
35428 +
35429 +       err = -EEXIST;
35430 +       if (d_is_negative(path->dentry)) {
35431 +               int mode = 0700;
35432 +
35433 +               if (au_test_nfs(path->dentry->d_sb))
35434 +                       mode |= 0111;
35435 +               err = vfsub_mkdir(h_dir, path, mode);
35436 +       } else if (d_is_dir(path->dentry))
35437 +               err = 0;
35438 +       else
35439 +               pr_err("unknown %pd exists\n", path->dentry);
35440 +
35441 +       return err;
35442 +}
35443 +
35444 +struct au_wh_base {
35445 +       const struct qstr *name;
35446 +       struct dentry *dentry;
35447 +};
35448 +
35449 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35450 +                         struct path *h_path)
35451 +{
35452 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35453 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35454 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35455 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35456 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35457 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35458 +}
35459 +
35460 +/*
35461 + * returns tri-state,
35462 + * minus: error, caller should print the message
35463 + * zero: success
35464 + * plus: error, caller should NOT print the message
35465 + */
35466 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35467 +                               int do_plink, struct au_wh_base base[],
35468 +                               struct path *h_path)
35469 +{
35470 +       int err;
35471 +       struct inode *h_dir;
35472 +
35473 +       h_dir = d_inode(h_root);
35474 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35475 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35476 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35477 +       if (do_plink) {
35478 +               err = test_linkable(h_root);
35479 +               if (unlikely(err)) {
35480 +                       err = 1;
35481 +                       goto out;
35482 +               }
35483 +
35484 +               err = au_whdir(h_dir, h_path);
35485 +               if (unlikely(err))
35486 +                       goto out;
35487 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35488 +       } else
35489 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35490 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35491 +       err = au_whdir(h_dir, h_path);
35492 +       if (unlikely(err))
35493 +               goto out;
35494 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35495 +
35496 +out:
35497 +       return err;
35498 +}
35499 +
35500 +/*
35501 + * for the moment, aufs supports the branch filesystem which does not support
35502 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35503 + * copyup failed. finally, such filesystem will not be used as the writable
35504 + * branch.
35505 + *
35506 + * returns tri-state, see above.
35507 + */
35508 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35509 +                        int do_plink, struct au_wh_base base[],
35510 +                        struct path *h_path)
35511 +{
35512 +       int err;
35513 +       struct inode *h_dir;
35514 +
35515 +       WbrWhMustWriteLock(wbr);
35516 +
35517 +       err = test_linkable(h_root);
35518 +       if (unlikely(err)) {
35519 +               err = 1;
35520 +               goto out;
35521 +       }
35522 +
35523 +       /*
35524 +        * todo: should this create be done in /sbin/mount.aufs helper?
35525 +        */
35526 +       err = -EEXIST;
35527 +       h_dir = d_inode(h_root);
35528 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35529 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35530 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35531 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35532 +               err = 0;
35533 +       else
35534 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35535 +       if (unlikely(err))
35536 +               goto out;
35537 +
35538 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35539 +       if (do_plink) {
35540 +               err = au_whdir(h_dir, h_path);
35541 +               if (unlikely(err))
35542 +                       goto out;
35543 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35544 +       } else
35545 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35546 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35547 +
35548 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35549 +       err = au_whdir(h_dir, h_path);
35550 +       if (unlikely(err))
35551 +               goto out;
35552 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35553 +
35554 +out:
35555 +       return err;
35556 +}
35557 +
35558 +/*
35559 + * initialize the whiteout base file/dir for @br.
35560 + */
35561 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35562 +{
35563 +       int err, i;
35564 +       const unsigned char do_plink
35565 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35566 +       struct inode *h_dir;
35567 +       struct path path = br->br_path;
35568 +       struct dentry *h_root = path.dentry;
35569 +       struct au_wbr *wbr = br->br_wbr;
35570 +       static const struct qstr base_name[] = {
35571 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35572 +                                         sizeof(AUFS_BASE_NAME) - 1),
35573 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35574 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35575 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35576 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35577 +       };
35578 +       struct au_wh_base base[] = {
35579 +               [AuBrWh_BASE] = {
35580 +                       .name   = base_name + AuBrWh_BASE,
35581 +                       .dentry = NULL
35582 +               },
35583 +               [AuBrWh_PLINK] = {
35584 +                       .name   = base_name + AuBrWh_PLINK,
35585 +                       .dentry = NULL
35586 +               },
35587 +               [AuBrWh_ORPH] = {
35588 +                       .name   = base_name + AuBrWh_ORPH,
35589 +                       .dentry = NULL
35590 +               }
35591 +       };
35592 +
35593 +       if (wbr)
35594 +               WbrWhMustWriteLock(wbr);
35595 +
35596 +       for (i = 0; i < AuBrWh_Last; i++) {
35597 +               /* doubly whiteouted */
35598 +               struct dentry *d;
35599 +
35600 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35601 +               err = PTR_ERR(d);
35602 +               if (IS_ERR(d))
35603 +                       goto out;
35604 +
35605 +               base[i].dentry = d;
35606 +               AuDebugOn(wbr
35607 +                         && wbr->wbr_wh[i]
35608 +                         && wbr->wbr_wh[i] != base[i].dentry);
35609 +       }
35610 +
35611 +       if (wbr)
35612 +               for (i = 0; i < AuBrWh_Last; i++) {
35613 +                       dput(wbr->wbr_wh[i]);
35614 +                       wbr->wbr_wh[i] = NULL;
35615 +               }
35616 +
35617 +       err = 0;
35618 +       if (!au_br_writable(br->br_perm)) {
35619 +               h_dir = d_inode(h_root);
35620 +               au_wh_init_ro(h_dir, base, &path);
35621 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35622 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35623 +               if (err > 0)
35624 +                       goto out;
35625 +               else if (err)
35626 +                       goto out_err;
35627 +       } else {
35628 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35629 +               if (err > 0)
35630 +                       goto out;
35631 +               else if (err)
35632 +                       goto out_err;
35633 +       }
35634 +       goto out; /* success */
35635 +
35636 +out_err:
35637 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35638 +              err, h_root, au_sbtype(h_root->d_sb));
35639 +out:
35640 +       for (i = 0; i < AuBrWh_Last; i++)
35641 +               dput(base[i].dentry);
35642 +       return err;
35643 +}
35644 +
35645 +/* ---------------------------------------------------------------------- */
35646 +/*
35647 + * whiteouts are all hard-linked usually.
35648 + * when its link count reaches a ceiling, we create a new whiteout base
35649 + * asynchronously.
35650 + */
35651 +
35652 +struct reinit_br_wh {
35653 +       struct super_block *sb;
35654 +       struct au_branch *br;
35655 +};
35656 +
35657 +static void reinit_br_wh(void *arg)
35658 +{
35659 +       int err;
35660 +       aufs_bindex_t bindex;
35661 +       struct path h_path;
35662 +       struct reinit_br_wh *a = arg;
35663 +       struct au_wbr *wbr;
35664 +       struct inode *dir, *delegated;
35665 +       struct dentry *h_root;
35666 +       struct au_hinode *hdir;
35667 +
35668 +       err = 0;
35669 +       wbr = a->br->br_wbr;
35670 +       /* big aufs lock */
35671 +       si_noflush_write_lock(a->sb);
35672 +       if (!au_br_writable(a->br->br_perm))
35673 +               goto out;
35674 +       bindex = au_br_index(a->sb, a->br->br_id);
35675 +       if (unlikely(bindex < 0))
35676 +               goto out;
35677 +
35678 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35679 +       dir = d_inode(a->sb->s_root);
35680 +       hdir = au_hi(dir, bindex);
35681 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35682 +       AuDebugOn(h_root != au_br_dentry(a->br));
35683 +
35684 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35685 +       wbr_wh_write_lock(wbr);
35686 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35687 +                         h_root, a->br);
35688 +       if (!err) {
35689 +               h_path.dentry = wbr->wbr_whbase;
35690 +               h_path.mnt = au_br_mnt(a->br);
35691 +               delegated = NULL;
35692 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35693 +                                  /*force*/0);
35694 +               if (unlikely(err == -EWOULDBLOCK)) {
35695 +                       pr_warn("cannot retry for NFSv4 delegation"
35696 +                               " for an internal unlink\n");
35697 +                       iput(delegated);
35698 +               }
35699 +       } else {
35700 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35701 +               err = 0;
35702 +       }
35703 +       dput(wbr->wbr_whbase);
35704 +       wbr->wbr_whbase = NULL;
35705 +       if (!err)
35706 +               err = au_wh_init(a->br, a->sb);
35707 +       wbr_wh_write_unlock(wbr);
35708 +       au_hn_inode_unlock(hdir);
35709 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35710 +       if (!err)
35711 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35712 +
35713 +out:
35714 +       if (wbr)
35715 +               atomic_dec(&wbr->wbr_wh_running);
35716 +       au_lcnt_dec(&a->br->br_count);
35717 +       si_write_unlock(a->sb);
35718 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35719 +       au_kfree_rcu(a);
35720 +       if (unlikely(err))
35721 +               AuIOErr("err %d\n", err);
35722 +}
35723 +
35724 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35725 +{
35726 +       int do_dec, wkq_err;
35727 +       struct reinit_br_wh *arg;
35728 +
35729 +       do_dec = 1;
35730 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35731 +               goto out;
35732 +
35733 +       /* ignore ENOMEM */
35734 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35735 +       if (arg) {
35736 +               /*
35737 +                * dec(wh_running), kfree(arg) and dec(br_count)
35738 +                * in reinit function
35739 +                */
35740 +               arg->sb = sb;
35741 +               arg->br = br;
35742 +               au_lcnt_inc(&br->br_count);
35743 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35744 +               if (unlikely(wkq_err)) {
35745 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35746 +                       au_lcnt_dec(&br->br_count);
35747 +                       au_kfree_rcu(arg);
35748 +               }
35749 +               do_dec = 0;
35750 +       }
35751 +
35752 +out:
35753 +       if (do_dec)
35754 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35755 +}
35756 +
35757 +/* ---------------------------------------------------------------------- */
35758 +
35759 +/*
35760 + * create the whiteout @wh.
35761 + */
35762 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35763 +                            struct dentry *wh)
35764 +{
35765 +       int err;
35766 +       struct path h_path = {
35767 +               .dentry = wh
35768 +       };
35769 +       struct au_branch *br;
35770 +       struct au_wbr *wbr;
35771 +       struct dentry *h_parent;
35772 +       struct inode *h_dir, *delegated;
35773 +
35774 +       h_parent = wh->d_parent; /* dir inode is locked */
35775 +       h_dir = d_inode(h_parent);
35776 +       IMustLock(h_dir);
35777 +
35778 +       br = au_sbr(sb, bindex);
35779 +       h_path.mnt = au_br_mnt(br);
35780 +       wbr = br->br_wbr;
35781 +       wbr_wh_read_lock(wbr);
35782 +       if (wbr->wbr_whbase) {
35783 +               delegated = NULL;
35784 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35785 +               if (unlikely(err == -EWOULDBLOCK)) {
35786 +                       pr_warn("cannot retry for NFSv4 delegation"
35787 +                               " for an internal link\n");
35788 +                       iput(delegated);
35789 +               }
35790 +               if (!err || err != -EMLINK)
35791 +                       goto out;
35792 +
35793 +               /* link count full. re-initialize br_whbase. */
35794 +               kick_reinit_br_wh(sb, br);
35795 +       }
35796 +
35797 +       /* return this error in this context */
35798 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35799 +       if (!err)
35800 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35801 +
35802 +out:
35803 +       wbr_wh_read_unlock(wbr);
35804 +       return err;
35805 +}
35806 +
35807 +/* ---------------------------------------------------------------------- */
35808 +
35809 +/*
35810 + * create or remove the diropq.
35811 + */
35812 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35813 +                               unsigned int flags)
35814 +{
35815 +       struct dentry *opq_dentry;
35816 +       struct super_block *sb;
35817 +       struct au_branch *br;
35818 +       struct path h_path;
35819 +       int err;
35820 +
35821 +       sb = dentry->d_sb;
35822 +       br = au_sbr(sb, bindex);
35823 +       h_path.dentry = au_h_dptr(dentry, bindex);
35824 +       h_path.mnt = au_br_mnt(br);
35825 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35826 +       if (IS_ERR(opq_dentry))
35827 +               goto out;
35828 +
35829 +       if (au_ftest_diropq(flags, CREATE)) {
35830 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35831 +               if (!err) {
35832 +                       au_set_dbdiropq(dentry, bindex);
35833 +                       goto out; /* success */
35834 +               }
35835 +       } else {
35836 +               h_path.dentry = opq_dentry;
35837 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35838 +               if (!err)
35839 +                       au_set_dbdiropq(dentry, -1);
35840 +       }
35841 +       dput(opq_dentry);
35842 +       opq_dentry = ERR_PTR(err);
35843 +
35844 +out:
35845 +       return opq_dentry;
35846 +}
35847 +
35848 +struct do_diropq_args {
35849 +       struct dentry **errp;
35850 +       struct dentry *dentry;
35851 +       aufs_bindex_t bindex;
35852 +       unsigned int flags;
35853 +};
35854 +
35855 +static void call_do_diropq(void *args)
35856 +{
35857 +       struct do_diropq_args *a = args;
35858 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35859 +}
35860 +
35861 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35862 +                            unsigned int flags)
35863 +{
35864 +       struct dentry *diropq, *h_dentry;
35865 +       struct mnt_idmap *h_idmap;
35866 +
35867 +       h_idmap = au_sbr_idmap(dentry->d_sb, bindex);
35868 +       h_dentry = au_h_dptr(dentry, bindex);
35869 +       if (!au_test_h_perm_sio(h_idmap, d_inode(h_dentry),
35870 +                               MAY_EXEC | MAY_WRITE))
35871 +               diropq = do_diropq(dentry, bindex, flags);
35872 +       else {
35873 +               int wkq_err;
35874 +               struct do_diropq_args args = {
35875 +                       .errp           = &diropq,
35876 +                       .dentry         = dentry,
35877 +                       .bindex         = bindex,
35878 +                       .flags          = flags
35879 +               };
35880 +
35881 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35882 +               if (unlikely(wkq_err))
35883 +                       diropq = ERR_PTR(wkq_err);
35884 +       }
35885 +
35886 +       return diropq;
35887 +}
35888 +
35889 +/* ---------------------------------------------------------------------- */
35890 +
35891 +/*
35892 + * lookup whiteout dentry.
35893 + * @h_parent: lower parent dentry which must exist and be locked
35894 + * @base_name: name of dentry which will be whiteouted
35895 + * returns dentry for whiteout.
35896 + */
35897 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35898 +                         struct au_branch *br)
35899 +{
35900 +       int err;
35901 +       struct qstr wh_name;
35902 +       struct dentry *wh_dentry;
35903 +       struct path h_path;
35904 +
35905 +       err = au_wh_name_alloc(&wh_name, base_name);
35906 +       wh_dentry = ERR_PTR(err);
35907 +       if (!err) {
35908 +               h_path.dentry = h_parent;
35909 +               h_path.mnt = au_br_mnt(br);
35910 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35911 +               au_kfree_try_rcu(wh_name.name);
35912 +       }
35913 +       return wh_dentry;
35914 +}
35915 +
35916 +/*
35917 + * link/create a whiteout for @dentry on @bindex.
35918 + */
35919 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35920 +                           struct dentry *h_parent)
35921 +{
35922 +       struct dentry *wh_dentry;
35923 +       struct super_block *sb;
35924 +       int err;
35925 +
35926 +       sb = dentry->d_sb;
35927 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35928 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35929 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35930 +               if (!err) {
35931 +                       au_set_dbwh(dentry, bindex);
35932 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35933 +               } else {
35934 +                       dput(wh_dentry);
35935 +                       wh_dentry = ERR_PTR(err);
35936 +               }
35937 +       }
35938 +
35939 +       return wh_dentry;
35940 +}
35941 +
35942 +/* ---------------------------------------------------------------------- */
35943 +
35944 +/* Delete all whiteouts in this directory on branch bindex. */
35945 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35946 +                          aufs_bindex_t bindex)
35947 +{
35948 +       int err;
35949 +       unsigned long ul, n;
35950 +       struct qstr wh_name;
35951 +       char *p;
35952 +       struct hlist_head *head;
35953 +       struct au_vdir_wh *pos;
35954 +       struct au_vdir_destr *str;
35955 +
35956 +       err = -ENOMEM;
35957 +       p = (void *)__get_free_page(GFP_NOFS);
35958 +       wh_name.name = p;
35959 +       if (unlikely(!wh_name.name))
35960 +               goto out;
35961 +
35962 +       err = 0;
35963 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35964 +       p += AUFS_WH_PFX_LEN;
35965 +       n = whlist->nh_num;
35966 +       head = whlist->nh_head;
35967 +       for (ul = 0; !err && ul < n; ul++, head++) {
35968 +               hlist_for_each_entry(pos, head, wh_hash) {
35969 +                       if (pos->wh_bindex != bindex)
35970 +                               continue;
35971 +
35972 +                       str = &pos->wh_str;
35973 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35974 +                               memcpy(p, str->name, str->len);
35975 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35976 +                               err = unlink_wh_name(h_path, &wh_name);
35977 +                               if (!err)
35978 +                                       continue;
35979 +                               break;
35980 +                       }
35981 +                       AuIOErr("whiteout name too long %.*s\n",
35982 +                               str->len, str->name);
35983 +                       err = -EIO;
35984 +                       break;
35985 +               }
35986 +       }
35987 +       free_page((unsigned long)wh_name.name);
35988 +
35989 +out:
35990 +       return err;
35991 +}
35992 +
35993 +struct del_wh_children_args {
35994 +       int *errp;
35995 +       struct path *h_path;
35996 +       struct au_nhash *whlist;
35997 +       aufs_bindex_t bindex;
35998 +};
35999 +
36000 +static void call_del_wh_children(void *args)
36001 +{
36002 +       struct del_wh_children_args *a = args;
36003 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
36004 +}
36005 +
36006 +/* ---------------------------------------------------------------------- */
36007 +
36008 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
36009 +{
36010 +       struct au_whtmp_rmdir *whtmp;
36011 +       int err;
36012 +       unsigned int rdhash;
36013 +
36014 +       SiMustAnyLock(sb);
36015 +
36016 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
36017 +       if (unlikely(!whtmp)) {
36018 +               whtmp = ERR_PTR(-ENOMEM);
36019 +               goto out;
36020 +       }
36021 +
36022 +       /* no estimation for dir size */
36023 +       rdhash = au_sbi(sb)->si_rdhash;
36024 +       if (!rdhash)
36025 +               rdhash = AUFS_RDHASH_DEF;
36026 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
36027 +       if (unlikely(err)) {
36028 +               au_kfree_rcu(whtmp);
36029 +               whtmp = ERR_PTR(err);
36030 +       }
36031 +
36032 +out:
36033 +       return whtmp;
36034 +}
36035 +
36036 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
36037 +{
36038 +       if (whtmp->br)
36039 +               au_lcnt_dec(&whtmp->br->br_count);
36040 +       dput(whtmp->wh_dentry);
36041 +       iput(whtmp->dir);
36042 +       au_nhash_wh_free(&whtmp->whlist);
36043 +       au_kfree_rcu(whtmp);
36044 +}
36045 +
36046 +/*
36047 + * rmdir the whiteouted temporary named dir @h_dentry.
36048 + * @whlist: whiteouted children.
36049 + */
36050 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36051 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
36052 +{
36053 +       int err;
36054 +       unsigned int h_nlink;
36055 +       struct path wh_path;
36056 +       struct inode *wh_inode, *h_dir;
36057 +       struct au_branch *br;
36058 +       struct mnt_idmap *h_idmap;
36059 +
36060 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
36061 +       IMustLock(h_dir);
36062 +
36063 +       br = au_sbr(dir->i_sb, bindex);
36064 +       wh_path.dentry = wh_dentry;
36065 +       wh_path.mnt = au_br_mnt(br);
36066 +       h_idmap = au_br_idmap(br);
36067 +       wh_inode = d_inode(wh_dentry);
36068 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
36069 +
36070 +       /*
36071 +        * someone else might change some whiteouts while we were sleeping.
36072 +        * it means this whlist may have an obsoleted entry.
36073 +        */
36074 +       if (!au_test_h_perm_sio(h_idmap, wh_inode, MAY_EXEC | MAY_WRITE))
36075 +               err = del_wh_children(&wh_path, whlist, bindex);
36076 +       else {
36077 +               int wkq_err;
36078 +               struct del_wh_children_args args = {
36079 +                       .errp           = &err,
36080 +                       .h_path         = &wh_path,
36081 +                       .whlist         = whlist,
36082 +                       .bindex         = bindex
36083 +               };
36084 +
36085 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
36086 +               if (unlikely(wkq_err))
36087 +                       err = wkq_err;
36088 +       }
36089 +       inode_unlock(wh_inode);
36090 +
36091 +       if (!err) {
36092 +               h_nlink = h_dir->i_nlink;
36093 +               err = vfsub_rmdir(h_dir, &wh_path);
36094 +               /* some fs doesn't change the parent nlink in some cases */
36095 +               h_nlink -= h_dir->i_nlink;
36096 +       }
36097 +
36098 +       if (!err) {
36099 +               if (au_ibtop(dir) == bindex) {
36100 +                       /* todo: dir->i_mutex is necessary */
36101 +                       au_cpup_attr_timesizes(dir);
36102 +                       if (h_nlink)
36103 +                               vfsub_drop_nlink(dir);
36104 +               }
36105 +               return 0; /* success */
36106 +       }
36107 +
36108 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
36109 +       return err;
36110 +}
36111 +
36112 +static void call_rmdir_whtmp(void *args)
36113 +{
36114 +       int err;
36115 +       aufs_bindex_t bindex;
36116 +       struct au_whtmp_rmdir *a = args;
36117 +       struct super_block *sb;
36118 +       struct dentry *h_parent;
36119 +       struct inode *h_dir;
36120 +       struct au_hinode *hdir;
36121 +
36122 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
36123 +       /* inode_lock(a->dir); */
36124 +       err = -EROFS;
36125 +       sb = a->dir->i_sb;
36126 +       si_read_lock(sb, !AuLock_FLUSH);
36127 +       if (!au_br_writable(a->br->br_perm))
36128 +               goto out;
36129 +       bindex = au_br_index(sb, a->br->br_id);
36130 +       if (unlikely(bindex < 0))
36131 +               goto out;
36132 +
36133 +       err = -EIO;
36134 +       ii_write_lock_parent(a->dir);
36135 +       h_parent = dget_parent(a->wh_dentry);
36136 +       h_dir = d_inode(h_parent);
36137 +       hdir = au_hi(a->dir, bindex);
36138 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
36139 +       if (unlikely(err))
36140 +               goto out_mnt;
36141 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
36142 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36143 +                         a->br);
36144 +       if (!err)
36145 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
36146 +       au_hn_inode_unlock(hdir);
36147 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
36148 +
36149 +out_mnt:
36150 +       dput(h_parent);
36151 +       ii_write_unlock(a->dir);
36152 +out:
36153 +       /* inode_unlock(a->dir); */
36154 +       au_whtmp_rmdir_free(a);
36155 +       si_read_unlock(sb);
36156 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36157 +       if (unlikely(err))
36158 +               AuIOErr("err %d\n", err);
36159 +}
36160 +
36161 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36162 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36163 +{
36164 +       int wkq_err;
36165 +       struct super_block *sb;
36166 +
36167 +       IMustLock(dir);
36168 +
36169 +       /* all post-process will be done in do_rmdir_whtmp(). */
36170 +       sb = dir->i_sb;
36171 +       args->dir = au_igrab(dir);
36172 +       args->br = au_sbr(sb, bindex);
36173 +       au_lcnt_inc(&args->br->br_count);
36174 +       args->wh_dentry = dget(wh_dentry);
36175 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
36176 +       if (unlikely(wkq_err)) {
36177 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
36178 +               au_whtmp_rmdir_free(args);
36179 +       }
36180 +}
36181 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
36182 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
36183 +++ linux/fs/aufs/whout.h       2023-04-24 08:13:40.972909448 +0200
36184 @@ -0,0 +1,87 @@
36185 +/* SPDX-License-Identifier: GPL-2.0 */
36186 +/*
36187 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36188 + *
36189 + * This program is free software; you can redistribute it and/or modify
36190 + * it under the terms of the GNU General Public License as published by
36191 + * the Free Software Foundation; either version 2 of the License, or
36192 + * (at your option) any later version.
36193 + *
36194 + * This program is distributed in the hope that it will be useful,
36195 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36196 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36197 + * GNU General Public License for more details.
36198 + *
36199 + * You should have received a copy of the GNU General Public License
36200 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36201 + */
36202 +
36203 +/*
36204 + * whiteout for logical deletion and opaque directory
36205 + */
36206 +
36207 +#ifndef __AUFS_WHOUT_H__
36208 +#define __AUFS_WHOUT_H__
36209 +
36210 +#ifdef __KERNEL__
36211 +
36212 +#include "dir.h"
36213 +
36214 +/* whout.c */
36215 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36216 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
36217 +              struct qstr *wh_name, int try_sio);
36218 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path);
36219 +struct au_branch;
36220 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36221 +                            struct qstr *prefix);
36222 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36223 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36224 +                       struct dentry *dentry);
36225 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36226 +
36227 +/* diropq flags */
36228 +#define AuDiropq_CREATE        1
36229 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36230 +#define au_fset_diropq(flags, name) \
36231 +       do { (flags) |= AuDiropq_##name; } while (0)
36232 +#define au_fclr_diropq(flags, name) \
36233 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36234 +
36235 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36236 +                            unsigned int flags);
36237 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36238 +                         struct au_branch *br);
36239 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36240 +                           struct dentry *h_parent);
36241 +
36242 +/* real rmdir for the whiteout-ed dir */
36243 +struct au_whtmp_rmdir {
36244 +       struct inode *dir;
36245 +       struct au_branch *br;
36246 +       struct dentry *wh_dentry;
36247 +       struct au_nhash whlist;
36248 +};
36249 +
36250 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36251 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36252 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36253 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36254 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36255 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36256 +
36257 +/* ---------------------------------------------------------------------- */
36258 +
36259 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36260 +                                             aufs_bindex_t bindex)
36261 +{
36262 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36263 +}
36264 +
36265 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36266 +{
36267 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36268 +}
36269 +
36270 +#endif /* __KERNEL__ */
36271 +#endif /* __AUFS_WHOUT_H__ */
36272 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36273 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36274 +++ linux/fs/aufs/wkq.c 2022-11-05 23:02:18.972555950 +0100
36275 @@ -0,0 +1,372 @@
36276 +// SPDX-License-Identifier: GPL-2.0
36277 +/*
36278 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36279 + *
36280 + * This program is free software; you can redistribute it and/or modify
36281 + * it under the terms of the GNU General Public License as published by
36282 + * the Free Software Foundation; either version 2 of the License, or
36283 + * (at your option) any later version.
36284 + *
36285 + * This program is distributed in the hope that it will be useful,
36286 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36287 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36288 + * GNU General Public License for more details.
36289 + *
36290 + * You should have received a copy of the GNU General Public License
36291 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36292 + */
36293 +
36294 +/*
36295 + * workqueue for asynchronous/super-io operations
36296 + * todo: try new credential scheme
36297 + */
36298 +
36299 +#include <linux/module.h>
36300 +#include "aufs.h"
36301 +
36302 +/* internal workqueue named AUFS_WKQ_NAME */
36303 +
36304 +static struct workqueue_struct *au_wkq;
36305 +
36306 +struct au_wkinfo {
36307 +       struct work_struct wk;
36308 +       struct kobject *kobj;
36309 +
36310 +       unsigned int flags; /* see wkq.h */
36311 +
36312 +       au_wkq_func_t func;
36313 +       void *args;
36314 +
36315 +#ifdef CONFIG_LOCKDEP
36316 +       int dont_check;
36317 +       struct held_lock **hlock;
36318 +#endif
36319 +
36320 +       struct completion *comp;
36321 +};
36322 +
36323 +/* ---------------------------------------------------------------------- */
36324 +/*
36325 + * Aufs passes some operations to the workqueue such as the internal copyup.
36326 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36327 + * job run by workqueue depends upon the locks acquired in the other task.
36328 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36329 + * information too. And the job in the workqueue restores the info in order to
36330 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36331 + * correctly and expectedly.
36332 + */
36333 +
36334 +#ifndef CONFIG_LOCKDEP
36335 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36336 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36337 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36338 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36339 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36340 +#else
36341 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36342 +{
36343 +       wkinfo->hlock = NULL;
36344 +       wkinfo->dont_check = 0;
36345 +}
36346 +
36347 +/*
36348 + * 1: matched
36349 + * 0: unmatched
36350 + */
36351 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36352 +{
36353 +       static DEFINE_SPINLOCK(spin);
36354 +       static struct {
36355 +               char *name;
36356 +               struct lock_class_key *key;
36357 +       } a[] = {
36358 +               { .name = "&sbinfo->si_rwsem" },
36359 +               { .name = "&finfo->fi_rwsem" },
36360 +               { .name = "&dinfo->di_rwsem" },
36361 +               { .name = "&iinfo->ii_rwsem" }
36362 +       };
36363 +       static int set;
36364 +       int i;
36365 +
36366 +       /* lockless read from 'set.' see below */
36367 +       if (set == ARRAY_SIZE(a)) {
36368 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36369 +                       if (a[i].key == key)
36370 +                               goto match;
36371 +               goto unmatch;
36372 +       }
36373 +
36374 +       spin_lock(&spin);
36375 +       if (set)
36376 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36377 +                       if (a[i].key == key) {
36378 +                               spin_unlock(&spin);
36379 +                               goto match;
36380 +                       }
36381 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36382 +               if (a[i].key) {
36383 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36384 +                               spin_unlock(&spin);
36385 +                               goto match;
36386 +                       } else
36387 +                               continue;
36388 +               }
36389 +               if (strstr(a[i].name, name)) {
36390 +                       /*
36391 +                        * the order of these three lines is important for the
36392 +                        * lockless read above.
36393 +                        */
36394 +                       a[i].key = key;
36395 +                       spin_unlock(&spin);
36396 +                       set++;
36397 +                       /* AuDbg("%d, %s\n", set, name); */
36398 +                       goto match;
36399 +               }
36400 +       }
36401 +       spin_unlock(&spin);
36402 +       goto unmatch;
36403 +
36404 +match:
36405 +       return 1;
36406 +unmatch:
36407 +       return 0;
36408 +}
36409 +
36410 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36411 +{
36412 +       int err, n;
36413 +       struct task_struct *curr;
36414 +       struct held_lock **hl, *held_locks, *p;
36415 +
36416 +       err = 0;
36417 +       curr = current;
36418 +       wkinfo->dont_check = lockdep_recursing(curr);
36419 +       if (wkinfo->dont_check)
36420 +               goto out;
36421 +       n = curr->lockdep_depth;
36422 +       if (!n)
36423 +               goto out;
36424 +
36425 +       err = -ENOMEM;
36426 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36427 +       if (unlikely(!wkinfo->hlock))
36428 +               goto out;
36429 +
36430 +       err = 0;
36431 +#if 0 /* left for debugging */
36432 +       if (0 && au_debug_test())
36433 +               lockdep_print_held_locks(curr);
36434 +#endif
36435 +       held_locks = curr->held_locks;
36436 +       hl = wkinfo->hlock;
36437 +       while (n--) {
36438 +               p = held_locks++;
36439 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36440 +                       *hl++ = p;
36441 +       }
36442 +       *hl = NULL;
36443 +
36444 +out:
36445 +       return err;
36446 +}
36447 +
36448 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36449 +{
36450 +       au_kfree_try_rcu(wkinfo->hlock);
36451 +}
36452 +
36453 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36454 +{
36455 +       struct held_lock *p, **hl = wkinfo->hlock;
36456 +       int subclass;
36457 +
36458 +       if (wkinfo->dont_check)
36459 +               lockdep_off();
36460 +       if (!hl)
36461 +               return;
36462 +       while ((p = *hl++)) { /* assignment */
36463 +               subclass = lockdep_hlock_class(p)->subclass;
36464 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36465 +               if (p->read)
36466 +                       rwsem_acquire_read(p->instance, subclass, 0,
36467 +                                          /*p->acquire_ip*/_RET_IP_);
36468 +               else
36469 +                       rwsem_acquire(p->instance, subclass, 0,
36470 +                                     /*p->acquire_ip*/_RET_IP_);
36471 +       }
36472 +}
36473 +
36474 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36475 +{
36476 +       struct held_lock *p, **hl = wkinfo->hlock;
36477 +
36478 +       if (wkinfo->dont_check)
36479 +               lockdep_on();
36480 +       if (!hl)
36481 +               return;
36482 +       while ((p = *hl++)) /* assignment */
36483 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36484 +}
36485 +#endif
36486 +
36487 +static void wkq_func(struct work_struct *wk)
36488 +{
36489 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36490 +
36491 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36492 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36493 +
36494 +       au_wkq_lockdep_pre(wkinfo);
36495 +       wkinfo->func(wkinfo->args);
36496 +       au_wkq_lockdep_post(wkinfo);
36497 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36498 +               complete(wkinfo->comp);
36499 +       else {
36500 +               kobject_put(wkinfo->kobj);
36501 +               module_put(THIS_MODULE); /* todo: ?? */
36502 +               au_kfree_rcu(wkinfo);
36503 +       }
36504 +}
36505 +
36506 +/*
36507 + * Since struct completion is large, try allocating it dynamically.
36508 + */
36509 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36510 +
36511 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36512 +{
36513 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36514 +       if (*comp) {
36515 +               init_completion(*comp);
36516 +               wkinfo->comp = *comp;
36517 +               return 0;
36518 +       }
36519 +       return -ENOMEM;
36520 +}
36521 +
36522 +static void au_wkq_comp_free(struct completion *comp)
36523 +{
36524 +       au_kfree_rcu(comp);
36525 +}
36526 +
36527 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36528 +{
36529 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36530 +               if (au_wkq_test()) {
36531 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36532 +                               " due to a dead dir by UDBA,"
36533 +                               " or async xino write?\n");
36534 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36535 +               }
36536 +       } else
36537 +               au_dbg_verify_kthread();
36538 +
36539 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36540 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36541 +               queue_work(au_wkq, &wkinfo->wk);
36542 +       } else {
36543 +               INIT_WORK(&wkinfo->wk, wkq_func);
36544 +               schedule_work(&wkinfo->wk);
36545 +       }
36546 +}
36547 +
36548 +/*
36549 + * Be careful. It is easy to make deadlock happen.
36550 + * processA: lock, wkq and wait
36551 + * processB: wkq and wait, lock in wkq
36552 + * --> deadlock
36553 + */
36554 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36555 +{
36556 +       int err;
36557 +       AuWkqCompDeclare(comp);
36558 +       struct au_wkinfo wkinfo = {
36559 +               .flags  = flags,
36560 +               .func   = func,
36561 +               .args   = args
36562 +       };
36563 +
36564 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36565 +       if (unlikely(err))
36566 +               goto out;
36567 +       err = au_wkq_lockdep_alloc(&wkinfo);
36568 +       if (unlikely(err))
36569 +               goto out_comp;
36570 +       if (!err) {
36571 +               au_wkq_run(&wkinfo);
36572 +               /* no timeout, no interrupt */
36573 +               wait_for_completion(wkinfo.comp);
36574 +       }
36575 +       au_wkq_lockdep_free(&wkinfo);
36576 +
36577 +out_comp:
36578 +       au_wkq_comp_free(comp);
36579 +out:
36580 +       destroy_work_on_stack(&wkinfo.wk);
36581 +       return err;
36582 +}
36583 +
36584 +/*
36585 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36586 + * problem in a concurrent umounting.
36587 + */
36588 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36589 +                 unsigned int flags)
36590 +{
36591 +       int err;
36592 +       struct au_wkinfo *wkinfo;
36593 +
36594 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36595 +
36596 +       /*
36597 +        * wkq_func() must free this wkinfo.
36598 +        * it highly depends upon the implementation of workqueue.
36599 +        */
36600 +       err = 0;
36601 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36602 +       if (wkinfo) {
36603 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36604 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36605 +               wkinfo->func = func;
36606 +               wkinfo->args = args;
36607 +               wkinfo->comp = NULL;
36608 +               au_wkq_lockdep_init(wkinfo);
36609 +               kobject_get(wkinfo->kobj);
36610 +               __module_get(THIS_MODULE); /* todo: ?? */
36611 +
36612 +               au_wkq_run(wkinfo);
36613 +       } else {
36614 +               err = -ENOMEM;
36615 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36616 +       }
36617 +
36618 +       return err;
36619 +}
36620 +
36621 +/* ---------------------------------------------------------------------- */
36622 +
36623 +void au_nwt_init(struct au_nowait_tasks *nwt)
36624 +{
36625 +       atomic_set(&nwt->nw_len, 0);
36626 +       /* smp_mb(); */ /* atomic_set */
36627 +       init_waitqueue_head(&nwt->nw_wq);
36628 +}
36629 +
36630 +void au_wkq_fin(void)
36631 +{
36632 +       destroy_workqueue(au_wkq);
36633 +}
36634 +
36635 +int __init au_wkq_init(void)
36636 +{
36637 +       int err;
36638 +
36639 +       err = 0;
36640 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36641 +       if (IS_ERR(au_wkq))
36642 +               err = PTR_ERR(au_wkq);
36643 +       else if (!au_wkq)
36644 +               err = -ENOMEM;
36645 +
36646 +       return err;
36647 +}
36648 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36649 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36650 +++ linux/fs/aufs/wkq.h 2022-11-05 23:02:18.972555950 +0100
36651 @@ -0,0 +1,89 @@
36652 +/* SPDX-License-Identifier: GPL-2.0 */
36653 +/*
36654 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36655 + *
36656 + * This program is free software; you can redistribute it and/or modify
36657 + * it under the terms of the GNU General Public License as published by
36658 + * the Free Software Foundation; either version 2 of the License, or
36659 + * (at your option) any later version.
36660 + *
36661 + * This program is distributed in the hope that it will be useful,
36662 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36663 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36664 + * GNU General Public License for more details.
36665 + *
36666 + * You should have received a copy of the GNU General Public License
36667 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36668 + */
36669 +
36670 +/*
36671 + * workqueue for asynchronous/super-io operations
36672 + * todo: try new credentials management scheme
36673 + */
36674 +
36675 +#ifndef __AUFS_WKQ_H__
36676 +#define __AUFS_WKQ_H__
36677 +
36678 +#ifdef __KERNEL__
36679 +
36680 +#include <linux/wait.h>
36681 +
36682 +struct super_block;
36683 +
36684 +/* ---------------------------------------------------------------------- */
36685 +
36686 +/*
36687 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36688 + */
36689 +struct au_nowait_tasks {
36690 +       atomic_t                nw_len;
36691 +       wait_queue_head_t       nw_wq;
36692 +};
36693 +
36694 +/* ---------------------------------------------------------------------- */
36695 +
36696 +typedef void (*au_wkq_func_t)(void *args);
36697 +
36698 +/* wkq flags */
36699 +#define AuWkq_WAIT     1
36700 +#define AuWkq_NEST     (1 << 1)
36701 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36702 +#define au_fset_wkq(flags, name) \
36703 +       do { (flags) |= AuWkq_##name; } while (0)
36704 +#define au_fclr_wkq(flags, name) \
36705 +       do { (flags) &= ~AuWkq_##name; } while (0)
36706 +
36707 +/* wkq.c */
36708 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36709 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36710 +                 unsigned int flags);
36711 +void au_nwt_init(struct au_nowait_tasks *nwt);
36712 +int __init au_wkq_init(void);
36713 +void au_wkq_fin(void);
36714 +
36715 +/* ---------------------------------------------------------------------- */
36716 +
36717 +static inline int au_wkq_test(void)
36718 +{
36719 +       return current->flags & PF_WQ_WORKER;
36720 +}
36721 +
36722 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36723 +{
36724 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36725 +}
36726 +
36727 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36728 +{
36729 +       if (atomic_dec_and_test(&nwt->nw_len))
36730 +               wake_up_all(&nwt->nw_wq);
36731 +}
36732 +
36733 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36734 +{
36735 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36736 +       return 0;
36737 +}
36738 +
36739 +#endif /* __KERNEL__ */
36740 +#endif /* __AUFS_WKQ_H__ */
36741 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36742 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36743 +++ linux/fs/aufs/xattr.c       2023-07-04 23:43:55.849649534 +0200
36744 @@ -0,0 +1,360 @@
36745 +// SPDX-License-Identifier: GPL-2.0
36746 +/*
36747 + * Copyright (C) 2014-2022 Junjiro R. Okajima
36748 + *
36749 + * This program is free software; you can redistribute it and/or modify
36750 + * it under the terms of the GNU General Public License as published by
36751 + * the Free Software Foundation; either version 2 of the License, or
36752 + * (at your option) any later version.
36753 + *
36754 + * This program is distributed in the hope that it will be useful,
36755 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36756 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36757 + * GNU General Public License for more details.
36758 + *
36759 + * You should have received a copy of the GNU General Public License
36760 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36761 + */
36762 +
36763 +/*
36764 + * handling xattr functions
36765 + */
36766 +
36767 +#include <linux/fs.h>
36768 +#include <linux/xattr.h>
36769 +#include "aufs.h"
36770 +
36771 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36772 +{
36773 +       if (!ignore_flags)
36774 +               goto out;
36775 +       switch (err) {
36776 +       case -ENOMEM:
36777 +       case -EDQUOT:
36778 +               goto out;
36779 +       }
36780 +
36781 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36782 +               err = 0;
36783 +               goto out;
36784 +       }
36785 +
36786 +#define cmp(brattr, prefix) do {                                       \
36787 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36788 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36789 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36790 +                               err = 0;                                \
36791 +                       goto out;                                       \
36792 +               }                                                       \
36793 +       } while (0)
36794 +
36795 +       cmp(SEC, SECURITY);
36796 +       cmp(SYS, SYSTEM);
36797 +       cmp(TR, TRUSTED);
36798 +       cmp(USR, USER);
36799 +#undef cmp
36800 +
36801 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36802 +               err = 0;
36803 +
36804 +out:
36805 +       return err;
36806 +}
36807 +
36808 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36809 +
36810 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36811 +                           char *name, char **buf, unsigned int ignore_flags,
36812 +                           unsigned int verbose)
36813 +{
36814 +       int err, is_acl;
36815 +       ssize_t ssz;
36816 +       struct inode *h_idst;
36817 +       struct dentry *h_dst_dentry, *h_src_dentry;
36818 +       struct mnt_idmap *h_dst_idmap, *h_src_idmap;
36819 +       struct posix_acl *acl;
36820 +
36821 +       is_acl = !!is_posix_acl_xattr(name);
36822 +       h_src_idmap = mnt_idmap(h_src->mnt);
36823 +       h_src_dentry = h_src->dentry;
36824 +       if (is_acl) {
36825 +               acl = vfs_get_acl(h_src_idmap, h_src_dentry, name);
36826 +               AuDebugOn(!acl);
36827 +               if (unlikely(IS_ERR(acl))) {
36828 +                       err = PTR_ERR(acl);
36829 +                       if (err == -ENODATA)
36830 +                               err = 0;
36831 +                       else if (err == -EOPNOTSUPP
36832 +                                && au_test_nfs_noacl(d_inode(h_src_dentry)))
36833 +                               err = 0;
36834 +                       else if (verbose || au_debug_test())
36835 +                               pr_err("%s, err %d\n", name, err);
36836 +                       goto out;
36837 +               }
36838 +       } else {
36839 +               ssz = vfs_getxattr_alloc(h_src_idmap, h_src_dentry, name, buf,
36840 +                                        0, GFP_NOFS);
36841 +               if (unlikely(ssz <= 0)) {
36842 +                       err = ssz;
36843 +                       if (err == -ENODATA)
36844 +                               err = 0;
36845 +                       else if (err == -EOPNOTSUPP
36846 +                                && (ignore_flags & au_xattr_out_of_list))
36847 +                                err = 0;
36848 +                       else if (err && (verbose || au_debug_test()))
36849 +                               pr_err("%s, err %d\n", name, err);
36850 +                       goto out;
36851 +               }
36852 +       }
36853 +
36854 +       /* unlock it temporary */
36855 +       h_dst_idmap = mnt_idmap(h_dst->mnt);
36856 +       h_dst_dentry = h_dst->dentry;
36857 +       h_idst = d_inode(h_dst_dentry);
36858 +       inode_unlock(h_idst);
36859 +       if (is_acl) {
36860 +               err = vfsub_set_acl(h_dst_idmap, h_dst_dentry, name, acl);
36861 +               posix_acl_release(acl);
36862 +       } else
36863 +               err = vfsub_setxattr(h_dst_idmap, h_dst_dentry, name, *buf,
36864 +                                    ssz, /*flags*/0);
36865 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36866 +       if (unlikely(err)) {
36867 +               if (verbose || au_debug_test())
36868 +                       pr_err("%s, err %d\n", name, err);
36869 +               err = au_xattr_ignore(err, name, ignore_flags);
36870 +       }
36871 +
36872 +out:
36873 +       return err;
36874 +}
36875 +
36876 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36877 +                 unsigned int verbose)
36878 +{
36879 +       int err, unlocked;
36880 +       ssize_t ssz;
36881 +       struct dentry *h_dst_dentry, *h_src_dentry;
36882 +       struct inode *h_isrc, *h_idst;
36883 +       char *value, *p, *o, *e;
36884 +
36885 +       /* try stopping to update the source inode while we are referencing */
36886 +       /* there should not be the parent-child relationship between them */
36887 +       h_dst_dentry = h_dst->dentry;
36888 +       h_idst = d_inode(h_dst_dentry);
36889 +       h_src_dentry = h_src->dentry;
36890 +       h_isrc = d_inode(h_src_dentry);
36891 +       inode_unlock(h_idst);
36892 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36893 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36894 +       unlocked = 0;
36895 +
36896 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36897 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36898 +       err = ssz;
36899 +       if (unlikely(err < 0)) {
36900 +               AuTraceErr(err);
36901 +               if (err == -ENODATA
36902 +                   || err == -EOPNOTSUPP)
36903 +                       err = 0;        /* ignore */
36904 +               goto out;
36905 +       }
36906 +
36907 +       err = 0;
36908 +       p = NULL;
36909 +       o = NULL;
36910 +       if (ssz) {
36911 +               err = -ENOMEM;
36912 +               p = kmalloc(ssz, GFP_NOFS);
36913 +               o = p;
36914 +               if (unlikely(!p))
36915 +                       goto out;
36916 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36917 +       }
36918 +       inode_unlock_shared(h_isrc);
36919 +       unlocked = 1;
36920 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36921 +       if (unlikely(err < 0))
36922 +               goto out_free;
36923 +
36924 +       err = 0;
36925 +       e = p + ssz;
36926 +       value = NULL;
36927 +       ignore_flags |= au_xattr_out_of_list;
36928 +       while (!err && p < e) {
36929 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36930 +                                      verbose);
36931 +               p += strlen(p) + 1;
36932 +       }
36933 +       au_kfree_try_rcu(value);
36934 +
36935 +out_free:
36936 +       au_kfree_try_rcu(o);
36937 +out:
36938 +       if (!unlocked)
36939 +               inode_unlock_shared(h_isrc);
36940 +       AuTraceErr(err);
36941 +       return err;
36942 +}
36943 +
36944 +/* ---------------------------------------------------------------------- */
36945 +
36946 +static int au_smack_reentering(struct super_block *sb)
36947 +{
36948 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36949 +       /*
36950 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36951 +        * i_op->getxattr(). ouch.
36952 +        */
36953 +       return si_pid_test(sb);
36954 +#else
36955 +       return 0;
36956 +#endif
36957 +}
36958 +
36959 +enum {
36960 +       AU_XATTR_LIST,
36961 +       AU_XATTR_GET
36962 +};
36963 +
36964 +struct au_lgxattr {
36965 +       int type;
36966 +       union {
36967 +               struct {
36968 +                       char    *list;
36969 +                       size_t  size;
36970 +               } list;
36971 +               struct {
36972 +                       const char      *name;
36973 +                       void            *value;
36974 +                       size_t          size;
36975 +               } get;
36976 +       } u;
36977 +};
36978 +
36979 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36980 +                         struct au_lgxattr *arg)
36981 +{
36982 +       ssize_t err;
36983 +       int reenter;
36984 +       struct path h_path;
36985 +       struct super_block *sb;
36986 +
36987 +       sb = dentry->d_sb;
36988 +       reenter = au_smack_reentering(sb);
36989 +       if (!reenter) {
36990 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36991 +               if (unlikely(err))
36992 +                       goto out;
36993 +       }
36994 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36995 +       if (unlikely(err))
36996 +               goto out_si;
36997 +       if (unlikely(!h_path.dentry))
36998 +               /* illegally overlapped or something */
36999 +               goto out_di; /* pretending success */
37000 +
37001 +       /* always topmost entry only */
37002 +       switch (arg->type) {
37003 +       case AU_XATTR_LIST:
37004 +               err = vfs_listxattr(h_path.dentry,
37005 +                                   arg->u.list.list, arg->u.list.size);
37006 +               break;
37007 +       case AU_XATTR_GET:
37008 +               AuDebugOn(d_is_negative(h_path.dentry));
37009 +               err = vfs_getxattr(mnt_idmap(h_path.mnt), h_path.dentry,
37010 +                                  arg->u.get.name, arg->u.get.value,
37011 +                                  arg->u.get.size);
37012 +               break;
37013 +       }
37014 +
37015 +out_di:
37016 +       if (!reenter)
37017 +               di_read_unlock(dentry, AuLock_IR);
37018 +out_si:
37019 +       if (!reenter)
37020 +               si_read_unlock(sb);
37021 +out:
37022 +       AuTraceErr(err);
37023 +       return err;
37024 +}
37025 +
37026 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
37027 +{
37028 +       struct au_lgxattr arg = {
37029 +               .type = AU_XATTR_LIST,
37030 +               .u.list = {
37031 +                       .list   = list,
37032 +                       .size   = size
37033 +               },
37034 +       };
37035 +
37036 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
37037 +}
37038 +
37039 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
37040 +                          const char *name, void *value, size_t size)
37041 +{
37042 +       struct au_lgxattr arg = {
37043 +               .type = AU_XATTR_GET,
37044 +               .u.get = {
37045 +                       .name   = name,
37046 +                       .value  = value,
37047 +                       .size   = size
37048 +               },
37049 +       };
37050 +
37051 +       return au_lgxattr(dentry, inode, &arg);
37052 +}
37053 +
37054 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
37055 +                      const char *name, const void *value, size_t size,
37056 +                      int flags)
37057 +{
37058 +       struct au_sxattr arg = {
37059 +               .type = AU_XATTR_SET,
37060 +               .u.set = {
37061 +                       .name   = name,
37062 +                       .value  = value,
37063 +                       .size   = size,
37064 +                       .flags  = flags
37065 +               },
37066 +       };
37067 +
37068 +       return au_sxattr(dentry, inode, &arg);
37069 +}
37070 +
37071 +/* ---------------------------------------------------------------------- */
37072 +
37073 +static int au_xattr_get(const struct xattr_handler *handler,
37074 +                       struct dentry *dentry, struct inode *inode,
37075 +                       const char *name, void *buffer, size_t size)
37076 +{
37077 +       return au_getxattr(dentry, inode, name, buffer, size);
37078 +}
37079 +
37080 +static int au_xattr_set(const struct xattr_handler *handler,
37081 +                       struct mnt_idmap *idmap,
37082 +                       struct dentry *dentry, struct inode *inode,
37083 +                       const char *name, const void *value, size_t size,
37084 +                       int flags)
37085 +{
37086 +       return au_setxattr(dentry, inode, name, value, size, flags);
37087 +}
37088 +
37089 +static const struct xattr_handler au_xattr_handler = {
37090 +       .name   = "",
37091 +       .prefix = "",
37092 +       .get    = au_xattr_get,
37093 +       .set    = au_xattr_set
37094 +};
37095 +
37096 +static const struct xattr_handler *au_xattr_handlers[] = {
37097 +       &au_xattr_handler, /* must be last */
37098 +       NULL
37099 +};
37100 +
37101 +void au_xattr_init(struct super_block *sb)
37102 +{
37103 +       sb->s_xattr = au_xattr_handlers;
37104 +}
37105 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
37106 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
37107 +++ linux/fs/aufs/xino.c        2023-04-24 08:13:40.972909448 +0200
37108 @@ -0,0 +1,1926 @@
37109 +// SPDX-License-Identifier: GPL-2.0
37110 +/*
37111 + * Copyright (C) 2005-2022 Junjiro R. Okajima
37112 + *
37113 + * This program is free software; you can redistribute it and/or modify
37114 + * it under the terms of the GNU General Public License as published by
37115 + * the Free Software Foundation; either version 2 of the License, or
37116 + * (at your option) any later version.
37117 + *
37118 + * This program is distributed in the hope that it will be useful,
37119 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37120 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37121 + * GNU General Public License for more details.
37122 + *
37123 + * You should have received a copy of the GNU General Public License
37124 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37125 + */
37126 +
37127 +/*
37128 + * external inode number translation table and bitmap
37129 + *
37130 + * things to consider
37131 + * - the lifetime
37132 + *   + au_xino object
37133 + *   + XINO files (xino, xib, xigen)
37134 + *   + dynamic debugfs entries (xiN)
37135 + *   + static debugfs entries (xib, xigen)
37136 + *   + static sysfs entry (xi_path)
37137 + * - several entry points to handle them.
37138 + *   + mount(2) without xino option (default)
37139 + *   + mount(2) with xino option
37140 + *   + mount(2) with noxino option
37141 + *   + umount(2)
37142 + *   + remount with add/del branches
37143 + *   + remount with xino/noxino options
37144 + */
37145 +
37146 +#include <linux/seq_file.h>
37147 +#include <linux/statfs.h>
37148 +#include "aufs.h"
37149 +
37150 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37151 +                                    aufs_bindex_t bbot,
37152 +                                    struct super_block *h_sb)
37153 +{
37154 +       /* todo: try binary-search if the branches are many */
37155 +       for (; btop <= bbot; btop++)
37156 +               if (h_sb == au_sbr_sb(sb, btop))
37157 +                       return btop;
37158 +       return -1;
37159 +}
37160 +
37161 +/*
37162 + * find another branch who is on the same filesystem of the specified
37163 + * branch{@btgt}. search until @bbot.
37164 + */
37165 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37166 +                                 aufs_bindex_t bbot)
37167 +{
37168 +       aufs_bindex_t bindex;
37169 +       struct super_block *tgt_sb;
37170 +
37171 +       tgt_sb = au_sbr_sb(sb, btgt);
37172 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37173 +       if (bindex < 0)
37174 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
37175 +
37176 +       return bindex;
37177 +}
37178 +
37179 +/* ---------------------------------------------------------------------- */
37180 +
37181 +/*
37182 + * stop unnecessary notify events at creating xino files
37183 + */
37184 +
37185 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37186 +{
37187 +       aufs_bindex_t bfound, bindex, bbot;
37188 +       struct dentry *parent;
37189 +       struct au_branch *br;
37190 +
37191 +       bfound = -1;
37192 +       parent = dentry->d_parent; /* safe d_parent access */
37193 +       bbot = au_sbbot(sb);
37194 +       for (bindex = 0; bindex <= bbot; bindex++) {
37195 +               br = au_sbr(sb, bindex);
37196 +               if (au_br_dentry(br) == parent) {
37197 +                       bfound = bindex;
37198 +                       break;
37199 +               }
37200 +       }
37201 +
37202 +       AuDbg("bfound b%d\n", bfound);
37203 +       return bfound;
37204 +}
37205 +
37206 +struct au_xino_lock_dir {
37207 +       struct au_hinode *hdir;
37208 +       struct dentry *parent;
37209 +       struct inode *dir;
37210 +};
37211 +
37212 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37213 +                                         unsigned int lsc)
37214 +{
37215 +       struct dentry *parent;
37216 +       struct inode *dir;
37217 +
37218 +       parent = dget_parent(dentry);
37219 +       dir = d_inode(parent);
37220 +       inode_lock_nested(dir, lsc);
37221 +#if 0 /* it should not happen */
37222 +       spin_lock(&dentry->d_lock);
37223 +       if (unlikely(dentry->d_parent != parent)) {
37224 +               spin_unlock(&dentry->d_lock);
37225 +               inode_unlock(dir);
37226 +               dput(parent);
37227 +               parent = NULL;
37228 +               goto out;
37229 +       }
37230 +       spin_unlock(&dentry->d_lock);
37231 +
37232 +out:
37233 +#endif
37234 +       return parent;
37235 +}
37236 +
37237 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37238 +                            struct au_xino_lock_dir *ldir)
37239 +{
37240 +       aufs_bindex_t bindex;
37241 +
37242 +       ldir->hdir = NULL;
37243 +       bindex = au_xi_root(sb, xipath->dentry);
37244 +       if (bindex >= 0) {
37245 +               /* rw branch root */
37246 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37247 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37248 +       } else {
37249 +               /* other */
37250 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37251 +                                                  AuLsc_I_PARENT);
37252 +               ldir->dir = d_inode(ldir->parent);
37253 +       }
37254 +}
37255 +
37256 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37257 +{
37258 +       if (ldir->hdir)
37259 +               au_hn_inode_unlock(ldir->hdir);
37260 +       else {
37261 +               inode_unlock(ldir->dir);
37262 +               dput(ldir->parent);
37263 +       }
37264 +}
37265 +
37266 +/* ---------------------------------------------------------------------- */
37267 +
37268 +/*
37269 + * create and set a new xino file
37270 + */
37271 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37272 +                           int wbrtop)
37273 +{
37274 +       struct file *file;
37275 +       struct dentry *h_parent, *d;
37276 +       struct inode *h_dir, *inode;
37277 +       int err;
37278 +       static DEFINE_MUTEX(mtx);
37279 +
37280 +       /*
37281 +        * at mount-time, and the xino file is the default path,
37282 +        * hnotify is disabled so we have no notify events to ignore.
37283 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37284 +        */
37285 +       if (!wbrtop)
37286 +               mutex_lock(&mtx);
37287 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37288 +                              /* | __FMODE_NONOTIFY */,
37289 +                              0666);
37290 +       if (IS_ERR(file)) {
37291 +               if (!wbrtop)
37292 +                       mutex_unlock(&mtx);
37293 +               if (!silent)
37294 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37295 +               return file;
37296 +       }
37297 +
37298 +       /* keep file count */
37299 +       err = 0;
37300 +       d = file->f_path.dentry;
37301 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37302 +       if (!wbrtop)
37303 +               mutex_unlock(&mtx);
37304 +       /* mnt_want_write() is unnecessary here */
37305 +       h_dir = d_inode(h_parent);
37306 +       inode = file_inode(file);
37307 +       /* no delegation since it is just created */
37308 +       if (inode->i_nlink)
37309 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37310 +                                  /*force*/0);
37311 +       inode_unlock(h_dir);
37312 +       dput(h_parent);
37313 +       if (unlikely(err)) {
37314 +               if (!silent)
37315 +                       pr_err("unlink %s(%d)\n", fpath, err);
37316 +               goto out;
37317 +       }
37318 +
37319 +       err = -EINVAL;
37320 +       if (unlikely(sb && sb == d->d_sb)) {
37321 +               if (!silent)
37322 +                       pr_err("%s must be outside\n", fpath);
37323 +               goto out;
37324 +       }
37325 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37326 +               if (!silent)
37327 +                       pr_err("xino doesn't support %s(%s)\n",
37328 +                              fpath, au_sbtype(d->d_sb));
37329 +               goto out;
37330 +       }
37331 +       return file; /* success */
37332 +
37333 +out:
37334 +       fput(file);
37335 +       file = ERR_PTR(err);
37336 +       return file;
37337 +}
37338 +
37339 +/*
37340 + * create a new xinofile at the same place/path as @base.
37341 + */
37342 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37343 +                            struct file *copy_src)
37344 +{
37345 +       struct file *file;
37346 +       struct dentry *dentry;
37347 +       struct inode *dir, *delegated;
37348 +       struct qstr *name;
37349 +       struct path ppath, path;
37350 +       int err, do_unlock;
37351 +       struct au_xino_lock_dir ldir;
37352 +
37353 +       do_unlock = 1;
37354 +       au_xino_lock_dir(sb, base, &ldir);
37355 +       dentry = base->dentry;
37356 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37357 +       ppath.mnt = base->mnt;
37358 +       dir = d_inode(ppath.dentry);
37359 +       IMustLock(dir);
37360 +
37361 +       name = &dentry->d_name;
37362 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37363 +       if (IS_ERR(path.dentry)) {
37364 +               file = (void *)path.dentry;
37365 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37366 +               goto out;
37367 +       }
37368 +
37369 +       /* no need to mnt_want_write() since we call dentry_open() later */
37370 +       err = vfs_create(mnt_idmap(base->mnt), dir, path.dentry, 0666, NULL);
37371 +       if (unlikely(err)) {
37372 +               file = ERR_PTR(err);
37373 +               pr_err("%pd create err %d\n", dentry, err);
37374 +               goto out_dput;
37375 +       }
37376 +
37377 +       path.mnt = base->mnt;
37378 +       file = vfsub_dentry_open(&path,
37379 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37380 +                                /* | __FMODE_NONOTIFY */);
37381 +       if (IS_ERR(file)) {
37382 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37383 +               goto out_dput;
37384 +       }
37385 +
37386 +       delegated = NULL;
37387 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37388 +       au_xino_unlock_dir(&ldir);
37389 +       do_unlock = 0;
37390 +       if (unlikely(err == -EWOULDBLOCK)) {
37391 +               pr_warn("cannot retry for NFSv4 delegation"
37392 +                       " for an internal unlink\n");
37393 +               iput(delegated);
37394 +       }
37395 +       if (unlikely(err)) {
37396 +               pr_err("%pd unlink err %d\n", dentry, err);
37397 +               goto out_fput;
37398 +       }
37399 +
37400 +       if (copy_src) {
37401 +               /* no one can touch copy_src xino */
37402 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37403 +               if (unlikely(err)) {
37404 +                       pr_err("%pd copy err %d\n", dentry, err);
37405 +                       goto out_fput;
37406 +               }
37407 +       }
37408 +       goto out_dput; /* success */
37409 +
37410 +out_fput:
37411 +       fput(file);
37412 +       file = ERR_PTR(err);
37413 +out_dput:
37414 +       dput(path.dentry);
37415 +out:
37416 +       if (do_unlock)
37417 +               au_xino_unlock_dir(&ldir);
37418 +       return file;
37419 +}
37420 +
37421 +struct file *au_xino_file1(struct au_xino *xi)
37422 +{
37423 +       struct file *file;
37424 +       unsigned int u, nfile;
37425 +
37426 +       file = NULL;
37427 +       nfile = xi->xi_nfile;
37428 +       for (u = 0; u < nfile; u++) {
37429 +               file = xi->xi_file[u];
37430 +               if (file)
37431 +                       break;
37432 +       }
37433 +
37434 +       return file;
37435 +}
37436 +
37437 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37438 +{
37439 +       int err;
37440 +       struct file *f;
37441 +       void *p;
37442 +
37443 +       if (file)
37444 +               get_file(file);
37445 +
37446 +       err = 0;
37447 +       f = NULL;
37448 +       if (idx < xi->xi_nfile) {
37449 +               f = xi->xi_file[idx];
37450 +               if (f)
37451 +                       fput(f);
37452 +       } else {
37453 +               p = au_kzrealloc(xi->xi_file,
37454 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37455 +                                sizeof(*xi->xi_file) * (idx + 1),
37456 +                                GFP_NOFS, /*may_shrink*/0);
37457 +               if (p) {
37458 +                       MtxMustLock(&xi->xi_mtx);
37459 +                       xi->xi_file = p;
37460 +                       xi->xi_nfile = idx + 1;
37461 +               } else {
37462 +                       err = -ENOMEM;
37463 +                       if (file)
37464 +                               fput(file);
37465 +                       goto out;
37466 +               }
37467 +       }
37468 +       xi->xi_file[idx] = file;
37469 +
37470 +out:
37471 +       return err;
37472 +}
37473 +
37474 +/*
37475 + * if @xinew->xi is not set, then create new xigen file.
37476 + */
37477 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37478 +{
37479 +       struct file *file;
37480 +       int err;
37481 +
37482 +       SiMustAnyLock(sb);
37483 +
37484 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37485 +       if (IS_ERR(file)) {
37486 +               err = PTR_ERR(file);
37487 +               pr_err("%s[%d], err %d\n",
37488 +                      xinew->xi ? "xino" : "xigen",
37489 +                      xinew->idx, err);
37490 +               goto out;
37491 +       }
37492 +
37493 +       if (xinew->xi)
37494 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37495 +       else {
37496 +               BUG();
37497 +               /* todo: make xigen file an array */
37498 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37499 +       }
37500 +       fput(file);
37501 +       if (unlikely(err))
37502 +               file = ERR_PTR(err);
37503 +
37504 +out:
37505 +       return file;
37506 +}
37507 +
37508 +/* ---------------------------------------------------------------------- */
37509 +
37510 +/*
37511 + * truncate xino files
37512 + */
37513 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37514 +                           int idx, struct kstatfs *st)
37515 +{
37516 +       int err;
37517 +       blkcnt_t blocks;
37518 +       struct file *file, *new_xino;
37519 +       struct au_xi_new xinew = {
37520 +               .idx = idx
37521 +       };
37522 +
37523 +       err = 0;
37524 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37525 +       file = au_xino_file(xinew.xi, idx);
37526 +       if (!file)
37527 +               goto out;
37528 +
37529 +       xinew.base = &file->f_path;
37530 +       err = vfs_statfs(xinew.base, st);
37531 +       if (unlikely(err)) {
37532 +               AuErr1("statfs err %d, ignored\n", err);
37533 +               err = 0;
37534 +               goto out;
37535 +       }
37536 +
37537 +       blocks = file_inode(file)->i_blocks;
37538 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37539 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37540 +
37541 +       xinew.copy_src = file;
37542 +       new_xino = au_xi_new(sb, &xinew);
37543 +       if (IS_ERR(new_xino)) {
37544 +               err = PTR_ERR(new_xino);
37545 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37546 +               goto out;
37547 +       }
37548 +
37549 +       err = vfs_statfs(&new_xino->f_path, st);
37550 +       if (!err)
37551 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37552 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37553 +                       st->f_bfree, st->f_blocks);
37554 +       else {
37555 +               AuErr1("statfs err %d, ignored\n", err);
37556 +               err = 0;
37557 +       }
37558 +
37559 +out:
37560 +       return err;
37561 +}
37562 +
37563 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37564 +{
37565 +       int err, i;
37566 +       unsigned long jiffy;
37567 +       aufs_bindex_t bbot;
37568 +       struct kstatfs *st;
37569 +       struct au_branch *br;
37570 +       struct au_xino *xi;
37571 +
37572 +       err = -ENOMEM;
37573 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37574 +       if (unlikely(!st))
37575 +               goto out;
37576 +
37577 +       err = -EINVAL;
37578 +       bbot = au_sbbot(sb);
37579 +       if (unlikely(bindex < 0 || bbot < bindex))
37580 +               goto out_st;
37581 +
37582 +       err = 0;
37583 +       jiffy = jiffies;
37584 +       br = au_sbr(sb, bindex);
37585 +       xi = br->br_xino;
37586 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37587 +               err = au_xino_do_trunc(sb, bindex, i, st);
37588 +       if (!err)
37589 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37590 +
37591 +out_st:
37592 +       au_kfree_rcu(st);
37593 +out:
37594 +       return err;
37595 +}
37596 +
37597 +struct xino_do_trunc_args {
37598 +       struct super_block *sb;
37599 +       struct au_branch *br;
37600 +       int idx;
37601 +};
37602 +
37603 +static void xino_do_trunc(void *_args)
37604 +{
37605 +       struct xino_do_trunc_args *args = _args;
37606 +       struct super_block *sb;
37607 +       struct au_branch *br;
37608 +       struct inode *dir;
37609 +       int err, idx;
37610 +       aufs_bindex_t bindex;
37611 +
37612 +       err = 0;
37613 +       sb = args->sb;
37614 +       dir = d_inode(sb->s_root);
37615 +       br = args->br;
37616 +       idx = args->idx;
37617 +
37618 +       si_noflush_write_lock(sb);
37619 +       ii_read_lock_parent(dir);
37620 +       bindex = au_br_index(sb, br->br_id);
37621 +       err = au_xino_trunc(sb, bindex, idx);
37622 +       ii_read_unlock(dir);
37623 +       if (unlikely(err))
37624 +               pr_warn("err b%d, (%d)\n", bindex, err);
37625 +       atomic_dec(&br->br_xino->xi_truncating);
37626 +       au_lcnt_dec(&br->br_count);
37627 +       si_write_unlock(sb);
37628 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37629 +       au_kfree_rcu(args);
37630 +}
37631 +
37632 +/*
37633 + * returns the index in the xi_file array whose corresponding file is necessary
37634 + * to truncate, or -1 which means no need to truncate.
37635 + */
37636 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37637 +{
37638 +       int err;
37639 +       unsigned int u;
37640 +       struct kstatfs st;
37641 +       struct au_sbinfo *sbinfo;
37642 +       struct au_xino *xi;
37643 +       struct file *file;
37644 +
37645 +       /* todo: si_xino_expire and the ratio should be customizable */
37646 +       sbinfo = au_sbi(sb);
37647 +       if (time_before(jiffies,
37648 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37649 +               return -1;
37650 +
37651 +       /* truncation border */
37652 +       xi = br->br_xino;
37653 +       for (u = 0; u < xi->xi_nfile; u++) {
37654 +               file = au_xino_file(xi, u);
37655 +               if (!file)
37656 +                       continue;
37657 +
37658 +               err = vfs_statfs(&file->f_path, &st);
37659 +               if (unlikely(err)) {
37660 +                       AuErr1("statfs err %d, ignored\n", err);
37661 +                       return -1;
37662 +               }
37663 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37664 +                   >= AUFS_XINO_DEF_TRUNC)
37665 +                       return u;
37666 +       }
37667 +
37668 +       return -1;
37669 +}
37670 +
37671 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37672 +{
37673 +       int idx;
37674 +       struct xino_do_trunc_args *args;
37675 +       int wkq_err;
37676 +
37677 +       idx = xino_trunc_test(sb, br);
37678 +       if (idx < 0)
37679 +               return;
37680 +
37681 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37682 +               goto out;
37683 +
37684 +       /* lock and kfree() will be called in trunc_xino() */
37685 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37686 +       if (unlikely(!args)) {
37687 +               AuErr1("no memory\n");
37688 +               goto out;
37689 +       }
37690 +
37691 +       au_lcnt_inc(&br->br_count);
37692 +       args->sb = sb;
37693 +       args->br = br;
37694 +       args->idx = idx;
37695 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37696 +       if (!wkq_err)
37697 +               return; /* success */
37698 +
37699 +       pr_err("wkq %d\n", wkq_err);
37700 +       au_lcnt_dec(&br->br_count);
37701 +       au_kfree_rcu(args);
37702 +
37703 +out:
37704 +       atomic_dec(&br->br_xino->xi_truncating);
37705 +}
37706 +
37707 +/* ---------------------------------------------------------------------- */
37708 +
37709 +struct au_xi_calc {
37710 +       int idx;
37711 +       loff_t pos;
37712 +};
37713 +
37714 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37715 +                      struct au_xi_calc *calc)
37716 +{
37717 +       loff_t maxent;
37718 +
37719 +       maxent = au_xi_maxent(sb);
37720 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37721 +       calc->pos *= sizeof(ino_t);
37722 +}
37723 +
37724 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37725 +                               struct au_xi_calc *calc)
37726 +{
37727 +       int err;
37728 +       struct file *file;
37729 +       struct au_xino *xi = br->br_xino;
37730 +       struct au_xi_new xinew = {
37731 +               .xi = xi
37732 +       };
37733 +
37734 +       SiMustAnyLock(sb);
37735 +
37736 +       err = 0;
37737 +       if (!xi)
37738 +               goto out;
37739 +
37740 +       mutex_lock(&xi->xi_mtx);
37741 +       file = au_xino_file(xi, calc->idx);
37742 +       if (file)
37743 +               goto out_mtx;
37744 +
37745 +       file = au_xino_file(xi, /*idx*/-1);
37746 +       AuDebugOn(!file);
37747 +       xinew.idx = calc->idx;
37748 +       xinew.base = &file->f_path;
37749 +       /* xinew.copy_src = NULL; */
37750 +       file = au_xi_new(sb, &xinew);
37751 +       if (IS_ERR(file))
37752 +               err = PTR_ERR(file);
37753 +
37754 +out_mtx:
37755 +       mutex_unlock(&xi->xi_mtx);
37756 +out:
37757 +       return err;
37758 +}
37759 +
37760 +struct au_xino_do_new_async_args {
37761 +       struct super_block *sb;
37762 +       struct au_branch *br;
37763 +       struct au_xi_calc calc;
37764 +       ino_t ino;
37765 +};
37766 +
37767 +struct au_xi_writing {
37768 +       struct hlist_bl_node node;
37769 +       ino_t h_ino, ino;
37770 +};
37771 +
37772 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37773 +                           ino_t ino);
37774 +
37775 +static void au_xino_call_do_new_async(void *args)
37776 +{
37777 +       struct au_xino_do_new_async_args *a = args;
37778 +       struct au_branch *br;
37779 +       struct super_block *sb;
37780 +       struct au_sbinfo *sbi;
37781 +       struct inode *root;
37782 +       struct file *file;
37783 +       struct au_xi_writing *del, *p;
37784 +       struct hlist_bl_head *hbl;
37785 +       struct hlist_bl_node *pos;
37786 +       int err;
37787 +
37788 +       br = a->br;
37789 +       sb = a->sb;
37790 +       sbi = au_sbi(sb);
37791 +       si_noflush_read_lock(sb);
37792 +       root = d_inode(sb->s_root);
37793 +       ii_read_lock_child(root);
37794 +       err = au_xino_do_new_async(sb, br, &a->calc);
37795 +       if (unlikely(err)) {
37796 +               AuIOErr("err %d\n", err);
37797 +               goto out;
37798 +       }
37799 +
37800 +       file = au_xino_file(br->br_xino, a->calc.idx);
37801 +       AuDebugOn(!file);
37802 +       err = au_xino_do_write(file, &a->calc, a->ino);
37803 +       if (unlikely(err)) {
37804 +               AuIOErr("err %d\n", err);
37805 +               goto out;
37806 +       }
37807 +
37808 +       del = NULL;
37809 +       hbl = &br->br_xino->xi_writing;
37810 +       hlist_bl_lock(hbl);
37811 +       au_hbl_for_each(pos, hbl) {
37812 +               p = container_of(pos, struct au_xi_writing, node);
37813 +               if (p->ino == a->ino) {
37814 +                       del = p;
37815 +                       hlist_bl_del(&p->node);
37816 +                       break;
37817 +               }
37818 +       }
37819 +       hlist_bl_unlock(hbl);
37820 +       au_kfree_rcu(del);
37821 +
37822 +out:
37823 +       au_lcnt_dec(&br->br_count);
37824 +       ii_read_unlock(root);
37825 +       si_read_unlock(sb);
37826 +       au_nwt_done(&sbi->si_nowait);
37827 +       au_kfree_rcu(a);
37828 +}
37829 +
37830 +/*
37831 + * create a new xino file asynchronously
37832 + */
37833 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37834 +                            struct au_xi_calc *calc, ino_t ino)
37835 +{
37836 +       int err;
37837 +       struct au_xino_do_new_async_args *arg;
37838 +
37839 +       err = -ENOMEM;
37840 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37841 +       if (unlikely(!arg))
37842 +               goto out;
37843 +
37844 +       arg->sb = sb;
37845 +       arg->br = br;
37846 +       arg->calc = *calc;
37847 +       arg->ino = ino;
37848 +       au_lcnt_inc(&br->br_count);
37849 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37850 +       if (unlikely(err)) {
37851 +               pr_err("wkq %d\n", err);
37852 +               au_lcnt_dec(&br->br_count);
37853 +               au_kfree_rcu(arg);
37854 +       }
37855 +
37856 +out:
37857 +       return err;
37858 +}
37859 +
37860 +/*
37861 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37862 + * at the position of @h_ino.
37863 + */
37864 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37865 +                ino_t *ino)
37866 +{
37867 +       int err;
37868 +       ssize_t sz;
37869 +       struct au_xi_calc calc;
37870 +       struct au_sbinfo *sbinfo;
37871 +       struct file *file;
37872 +       struct au_xino *xi;
37873 +       struct hlist_bl_head *hbl;
37874 +       struct hlist_bl_node *pos;
37875 +       struct au_xi_writing *p;
37876 +
37877 +       *ino = 0;
37878 +       if (!au_opt_test(au_mntflags(sb), XINO))
37879 +               return 0; /* no xino */
37880 +
37881 +       err = 0;
37882 +       au_xi_calc(sb, h_ino, &calc);
37883 +       xi = au_sbr(sb, bindex)->br_xino;
37884 +       file = au_xino_file(xi, calc.idx);
37885 +       if (!file) {
37886 +               hbl = &xi->xi_writing;
37887 +               hlist_bl_lock(hbl);
37888 +               au_hbl_for_each(pos, hbl) {
37889 +                       p = container_of(pos, struct au_xi_writing, node);
37890 +                       if (p->h_ino == h_ino) {
37891 +                               AuDbg("hi%llu, i%llu, found\n",
37892 +                                     (u64)p->h_ino, (u64)p->ino);
37893 +                               *ino = p->ino;
37894 +                               break;
37895 +                       }
37896 +               }
37897 +               hlist_bl_unlock(hbl);
37898 +               return 0;
37899 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37900 +               return 0; /* no xino */
37901 +
37902 +       sbinfo = au_sbi(sb);
37903 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37904 +       if (sz == sizeof(*ino))
37905 +               return 0; /* success */
37906 +
37907 +       err = sz;
37908 +       if (unlikely(sz >= 0)) {
37909 +               err = -EIO;
37910 +               AuIOErr("xino read error (%zd)\n", sz);
37911 +       }
37912 +       return err;
37913 +}
37914 +
37915 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37916 +                           ino_t ino)
37917 +{
37918 +       ssize_t sz;
37919 +
37920 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37921 +       if (sz == sizeof(ino))
37922 +               return 0; /* success */
37923 +
37924 +       AuIOErr("write failed (%zd)\n", sz);
37925 +       return -EIO;
37926 +}
37927 +
37928 +/*
37929 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37930 + * at the position of @h_ino.
37931 + * even if @ino is zero, it is written to the xinofile and means no entry.
37932 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37933 + * try truncating it.
37934 + */
37935 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37936 +                 ino_t ino)
37937 +{
37938 +       int err;
37939 +       unsigned int mnt_flags;
37940 +       struct au_xi_calc calc;
37941 +       struct file *file;
37942 +       struct au_branch *br;
37943 +       struct au_xino *xi;
37944 +       struct au_xi_writing *p;
37945 +
37946 +       SiMustAnyLock(sb);
37947 +
37948 +       mnt_flags = au_mntflags(sb);
37949 +       if (!au_opt_test(mnt_flags, XINO))
37950 +               return 0;
37951 +
37952 +       au_xi_calc(sb, h_ino, &calc);
37953 +       br = au_sbr(sb, bindex);
37954 +       xi = br->br_xino;
37955 +       file = au_xino_file(xi, calc.idx);
37956 +       if (!file) {
37957 +               /* store the inum pair into the list */
37958 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37959 +               p->h_ino = h_ino;
37960 +               p->ino = ino;
37961 +               au_hbl_add(&p->node, &xi->xi_writing);
37962 +
37963 +               /* create and write a new xino file asynchronously */
37964 +               err = au_xino_new_async(sb, br, &calc, ino);
37965 +               if (!err)
37966 +                       return 0; /* success */
37967 +               goto out;
37968 +       }
37969 +
37970 +       err = au_xino_do_write(file, &calc, ino);
37971 +       if (!err) {
37972 +               br = au_sbr(sb, bindex);
37973 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37974 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37975 +                       xino_try_trunc(sb, br);
37976 +               return 0; /* success */
37977 +       }
37978 +
37979 +out:
37980 +       AuIOErr("write failed (%d)\n", err);
37981 +       return -EIO;
37982 +}
37983 +
37984 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37985 +                             loff_t *pos);
37986 +
37987 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37988 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37989 +{
37990 +       ssize_t err;
37991 +       int i;
37992 +       const int prevent_endless = 10;
37993 +
37994 +       i = 0;
37995 +       do {
37996 +               err = vfsub_read_k(file, kbuf, size, pos);
37997 +               if (err == -EINTR
37998 +                   && !au_wkq_test()
37999 +                   && fatal_signal_pending(current)) {
38000 +                       err = xino_fread_wkq(file, kbuf, size, pos);
38001 +                       BUG_ON(err == -EINTR);
38002 +               }
38003 +       } while (i++ < prevent_endless
38004 +                && (err == -EAGAIN || err == -EINTR));
38005 +
38006 +#if 0 /* reserved for future use */
38007 +       if (err > 0)
38008 +               fsnotify_access(file->f_path.dentry);
38009 +#endif
38010 +
38011 +       return err;
38012 +}
38013 +
38014 +struct xino_fread_args {
38015 +       ssize_t *errp;
38016 +       struct file *file;
38017 +       void *buf;
38018 +       size_t size;
38019 +       loff_t *pos;
38020 +};
38021 +
38022 +static void call_xino_fread(void *args)
38023 +{
38024 +       struct xino_fread_args *a = args;
38025 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
38026 +}
38027 +
38028 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38029 +                             loff_t *pos)
38030 +{
38031 +       ssize_t err;
38032 +       int wkq_err;
38033 +       struct xino_fread_args args = {
38034 +               .errp   = &err,
38035 +               .file   = file,
38036 +               .buf    = buf,
38037 +               .size   = size,
38038 +               .pos    = pos
38039 +       };
38040 +
38041 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
38042 +       if (unlikely(wkq_err))
38043 +               err = wkq_err;
38044 +
38045 +       return err;
38046 +}
38047 +
38048 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38049 +                              loff_t *pos);
38050 +
38051 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
38052 +                             loff_t *pos)
38053 +{
38054 +       ssize_t err;
38055 +       int i;
38056 +       const int prevent_endless = 10;
38057 +
38058 +       i = 0;
38059 +       do {
38060 +               err = vfsub_write_k(file, kbuf, size, pos);
38061 +               if (err == -EINTR
38062 +                   && !au_wkq_test()
38063 +                   && fatal_signal_pending(current)) {
38064 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
38065 +                       BUG_ON(err == -EINTR);
38066 +               }
38067 +       } while (i++ < prevent_endless
38068 +                && (err == -EAGAIN || err == -EINTR));
38069 +
38070 +#if 0 /* reserved for future use */
38071 +       if (err > 0)
38072 +               fsnotify_modify(file->f_path.dentry);
38073 +#endif
38074 +
38075 +       return err;
38076 +}
38077 +
38078 +struct do_xino_fwrite_args {
38079 +       ssize_t *errp;
38080 +       struct file *file;
38081 +       void *buf;
38082 +       size_t size;
38083 +       loff_t *pos;
38084 +};
38085 +
38086 +static void call_do_xino_fwrite(void *args)
38087 +{
38088 +       struct do_xino_fwrite_args *a = args;
38089 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
38090 +}
38091 +
38092 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38093 +                              loff_t *pos)
38094 +{
38095 +       ssize_t err;
38096 +       int wkq_err;
38097 +       struct do_xino_fwrite_args args = {
38098 +               .errp   = &err,
38099 +               .file   = file,
38100 +               .buf    = buf,
38101 +               .size   = size,
38102 +               .pos    = pos
38103 +       };
38104 +
38105 +       /*
38106 +        * it breaks RLIMIT_FSIZE and normal user's limit,
38107 +        * users should care about quota and real 'filesystem full.'
38108 +        */
38109 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38110 +       if (unlikely(wkq_err))
38111 +               err = wkq_err;
38112 +
38113 +       return err;
38114 +}
38115 +
38116 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
38117 +{
38118 +       ssize_t err;
38119 +
38120 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38121 +               lockdep_off();
38122 +               err = do_xino_fwrite(file, buf, size, pos);
38123 +               lockdep_on();
38124 +       } else {
38125 +               lockdep_off();
38126 +               err = xino_fwrite_wkq(file, buf, size, pos);
38127 +               lockdep_on();
38128 +       }
38129 +
38130 +       return err;
38131 +}
38132 +
38133 +/* ---------------------------------------------------------------------- */
38134 +
38135 +/*
38136 + * inode number bitmap
38137 + */
38138 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38139 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
38140 +{
38141 +       ino_t ino;
38142 +
38143 +       AuDebugOn(bit < 0 || page_bits <= bit);
38144 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38145 +       return ino;
38146 +}
38147 +
38148 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38149 +{
38150 +       AuDebugOn(ino < AUFS_FIRST_INO);
38151 +       ino -= AUFS_FIRST_INO;
38152 +       *pindex = ino / page_bits;
38153 +       *bit = ino % page_bits;
38154 +}
38155 +
38156 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
38157 +{
38158 +       int err;
38159 +       loff_t pos;
38160 +       ssize_t sz;
38161 +       struct au_sbinfo *sbinfo;
38162 +       struct file *xib;
38163 +       unsigned long *p;
38164 +
38165 +       sbinfo = au_sbi(sb);
38166 +       MtxMustLock(&sbinfo->si_xib_mtx);
38167 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38168 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38169 +
38170 +       if (pindex == sbinfo->si_xib_last_pindex)
38171 +               return 0;
38172 +
38173 +       xib = sbinfo->si_xib;
38174 +       p = sbinfo->si_xib_buf;
38175 +       pos = sbinfo->si_xib_last_pindex;
38176 +       pos *= PAGE_SIZE;
38177 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38178 +       if (unlikely(sz != PAGE_SIZE))
38179 +               goto out;
38180 +
38181 +       pos = pindex;
38182 +       pos *= PAGE_SIZE;
38183 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38184 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
38185 +       else {
38186 +               memset(p, 0, PAGE_SIZE);
38187 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38188 +       }
38189 +       if (sz == PAGE_SIZE) {
38190 +               sbinfo->si_xib_last_pindex = pindex;
38191 +               return 0; /* success */
38192 +       }
38193 +
38194 +out:
38195 +       AuIOErr1("write failed (%zd)\n", sz);
38196 +       err = sz;
38197 +       if (sz >= 0)
38198 +               err = -EIO;
38199 +       return err;
38200 +}
38201 +
38202 +static void au_xib_clear_bit(struct inode *inode)
38203 +{
38204 +       int err, bit;
38205 +       unsigned long pindex;
38206 +       struct super_block *sb;
38207 +       struct au_sbinfo *sbinfo;
38208 +
38209 +       AuDebugOn(inode->i_nlink);
38210 +
38211 +       sb = inode->i_sb;
38212 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38213 +       AuDebugOn(page_bits <= bit);
38214 +       sbinfo = au_sbi(sb);
38215 +       mutex_lock(&sbinfo->si_xib_mtx);
38216 +       err = xib_pindex(sb, pindex);
38217 +       if (!err) {
38218 +               clear_bit(bit, sbinfo->si_xib_buf);
38219 +               sbinfo->si_xib_next_bit = bit;
38220 +       }
38221 +       mutex_unlock(&sbinfo->si_xib_mtx);
38222 +}
38223 +
38224 +/* ---------------------------------------------------------------------- */
38225 +
38226 +/*
38227 + * truncate a xino bitmap file
38228 + */
38229 +
38230 +/* todo: slow */
38231 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38232 +{
38233 +       int err, bit;
38234 +       ssize_t sz;
38235 +       unsigned long pindex;
38236 +       loff_t pos, pend;
38237 +       struct au_sbinfo *sbinfo;
38238 +       ino_t *ino;
38239 +       unsigned long *p;
38240 +
38241 +       err = 0;
38242 +       sbinfo = au_sbi(sb);
38243 +       MtxMustLock(&sbinfo->si_xib_mtx);
38244 +       p = sbinfo->si_xib_buf;
38245 +       pend = vfsub_f_size_read(file);
38246 +       pos = 0;
38247 +       while (pos < pend) {
38248 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
38249 +               err = sz;
38250 +               if (unlikely(sz <= 0))
38251 +                       goto out;
38252 +
38253 +               err = 0;
38254 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38255 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38256 +                               continue;
38257 +
38258 +                       xib_calc_bit(*ino, &pindex, &bit);
38259 +                       AuDebugOn(page_bits <= bit);
38260 +                       err = xib_pindex(sb, pindex);
38261 +                       if (!err)
38262 +                               set_bit(bit, p);
38263 +                       else
38264 +                               goto out;
38265 +               }
38266 +       }
38267 +
38268 +out:
38269 +       return err;
38270 +}
38271 +
38272 +static int xib_restore(struct super_block *sb)
38273 +{
38274 +       int err, i;
38275 +       unsigned int nfile;
38276 +       aufs_bindex_t bindex, bbot;
38277 +       void *page;
38278 +       struct au_branch *br;
38279 +       struct au_xino *xi;
38280 +       struct file *file;
38281 +
38282 +       err = -ENOMEM;
38283 +       page = (void *)__get_free_page(GFP_NOFS);
38284 +       if (unlikely(!page))
38285 +               goto out;
38286 +
38287 +       err = 0;
38288 +       bbot = au_sbbot(sb);
38289 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38290 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38291 +                       br = au_sbr(sb, bindex);
38292 +                       xi = br->br_xino;
38293 +                       nfile = xi->xi_nfile;
38294 +                       for (i = 0; i < nfile; i++) {
38295 +                               file = au_xino_file(xi, i);
38296 +                               if (file)
38297 +                                       err = do_xib_restore(sb, file, page);
38298 +                       }
38299 +               } else
38300 +                       AuDbg("skip shared b%d\n", bindex);
38301 +       free_page((unsigned long)page);
38302 +
38303 +out:
38304 +       return err;
38305 +}
38306 +
38307 +int au_xib_trunc(struct super_block *sb)
38308 +{
38309 +       int err;
38310 +       ssize_t sz;
38311 +       loff_t pos;
38312 +       struct au_sbinfo *sbinfo;
38313 +       unsigned long *p;
38314 +       struct file *file;
38315 +
38316 +       SiMustWriteLock(sb);
38317 +
38318 +       err = 0;
38319 +       sbinfo = au_sbi(sb);
38320 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38321 +               goto out;
38322 +
38323 +       file = sbinfo->si_xib;
38324 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38325 +               goto out;
38326 +
38327 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38328 +       err = PTR_ERR(file);
38329 +       if (IS_ERR(file))
38330 +               goto out;
38331 +       fput(sbinfo->si_xib);
38332 +       sbinfo->si_xib = file;
38333 +
38334 +       p = sbinfo->si_xib_buf;
38335 +       memset(p, 0, PAGE_SIZE);
38336 +       pos = 0;
38337 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38338 +       if (unlikely(sz != PAGE_SIZE)) {
38339 +               err = sz;
38340 +               AuIOErr("err %d\n", err);
38341 +               if (sz >= 0)
38342 +                       err = -EIO;
38343 +               goto out;
38344 +       }
38345 +
38346 +       mutex_lock(&sbinfo->si_xib_mtx);
38347 +       /* mnt_want_write() is unnecessary here */
38348 +       err = xib_restore(sb);
38349 +       mutex_unlock(&sbinfo->si_xib_mtx);
38350 +
38351 +out:
38352 +       return err;
38353 +}
38354 +
38355 +/* ---------------------------------------------------------------------- */
38356 +
38357 +struct au_xino *au_xino_alloc(unsigned int nfile)
38358 +{
38359 +       struct au_xino *xi;
38360 +
38361 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38362 +       if (unlikely(!xi))
38363 +               goto out;
38364 +       xi->xi_nfile = nfile;
38365 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38366 +       if (unlikely(!xi->xi_file))
38367 +               goto out_free;
38368 +
38369 +       xi->xi_nondir.total = 8; /* initial size */
38370 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38371 +                                     GFP_NOFS);
38372 +       if (unlikely(!xi->xi_nondir.array))
38373 +               goto out_file;
38374 +
38375 +       spin_lock_init(&xi->xi_nondir.spin);
38376 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38377 +       mutex_init(&xi->xi_mtx);
38378 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38379 +       atomic_set(&xi->xi_truncating, 0);
38380 +       kref_init(&xi->xi_kref);
38381 +       goto out; /* success */
38382 +
38383 +out_file:
38384 +       au_kfree_try_rcu(xi->xi_file);
38385 +out_free:
38386 +       au_kfree_rcu(xi);
38387 +       xi = NULL;
38388 +out:
38389 +       return xi;
38390 +}
38391 +
38392 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38393 +{
38394 +       int err;
38395 +       struct au_xino *xi;
38396 +
38397 +       err = 0;
38398 +       xi = au_xino_alloc(idx + 1);
38399 +       if (unlikely(!xi)) {
38400 +               err = -ENOMEM;
38401 +               goto out;
38402 +       }
38403 +
38404 +       if (file)
38405 +               get_file(file);
38406 +       xi->xi_file[idx] = file;
38407 +       AuDebugOn(br->br_xino);
38408 +       br->br_xino = xi;
38409 +
38410 +out:
38411 +       return err;
38412 +}
38413 +
38414 +static void au_xino_release(struct kref *kref)
38415 +{
38416 +       struct au_xino *xi;
38417 +       int i;
38418 +       unsigned long ul;
38419 +       struct hlist_bl_head *hbl;
38420 +       struct hlist_bl_node *pos, *n;
38421 +       struct au_xi_writing *p;
38422 +
38423 +       xi = container_of(kref, struct au_xino, xi_kref);
38424 +       for (i = 0; i < xi->xi_nfile; i++)
38425 +               if (xi->xi_file[i])
38426 +                       fput(xi->xi_file[i]);
38427 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38428 +               AuDebugOn(xi->xi_nondir.array[i]);
38429 +       mutex_destroy(&xi->xi_mtx);
38430 +       hbl = &xi->xi_writing;
38431 +       ul = au_hbl_count(hbl);
38432 +       if (unlikely(ul)) {
38433 +               pr_warn("xi_writing %lu\n", ul);
38434 +               hlist_bl_lock(hbl);
38435 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38436 +                       hlist_bl_del(&p->node);
38437 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38438 +                       kfree(p);
38439 +               }
38440 +               hlist_bl_unlock(hbl);
38441 +       }
38442 +       au_kfree_try_rcu(xi->xi_file);
38443 +       au_kfree_try_rcu(xi->xi_nondir.array);
38444 +       au_kfree_rcu(xi);
38445 +}
38446 +
38447 +int au_xino_put(struct au_branch *br)
38448 +{
38449 +       int ret;
38450 +       struct au_xino *xi;
38451 +
38452 +       ret = 0;
38453 +       xi = br->br_xino;
38454 +       if (xi) {
38455 +               br->br_xino = NULL;
38456 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38457 +       }
38458 +
38459 +       return ret;
38460 +}
38461 +
38462 +/* ---------------------------------------------------------------------- */
38463 +
38464 +/*
38465 + * xino mount option handlers
38466 + */
38467 +
38468 +/* xino bitmap */
38469 +static void xino_clear_xib(struct super_block *sb)
38470 +{
38471 +       struct au_sbinfo *sbinfo;
38472 +
38473 +       SiMustWriteLock(sb);
38474 +
38475 +       sbinfo = au_sbi(sb);
38476 +       if (sbinfo->si_xib)
38477 +               fput(sbinfo->si_xib);
38478 +       sbinfo->si_xib = NULL;
38479 +       if (sbinfo->si_xib_buf)
38480 +               free_page((unsigned long)sbinfo->si_xib_buf);
38481 +       sbinfo->si_xib_buf = NULL;
38482 +}
38483 +
38484 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38485 +{
38486 +       int err;
38487 +       loff_t pos;
38488 +       struct au_sbinfo *sbinfo;
38489 +       struct file *file;
38490 +       struct super_block *xi_sb;
38491 +
38492 +       SiMustWriteLock(sb);
38493 +
38494 +       sbinfo = au_sbi(sb);
38495 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38496 +       err = PTR_ERR(file);
38497 +       if (IS_ERR(file))
38498 +               goto out;
38499 +       if (sbinfo->si_xib)
38500 +               fput(sbinfo->si_xib);
38501 +       sbinfo->si_xib = file;
38502 +       xi_sb = file_inode(file)->i_sb;
38503 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38504 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38505 +               err = -EIO;
38506 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38507 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38508 +               goto out_unset;
38509 +       }
38510 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38511 +
38512 +       err = -ENOMEM;
38513 +       if (!sbinfo->si_xib_buf)
38514 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38515 +       if (unlikely(!sbinfo->si_xib_buf))
38516 +               goto out_unset;
38517 +
38518 +       sbinfo->si_xib_last_pindex = 0;
38519 +       sbinfo->si_xib_next_bit = 0;
38520 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38521 +               pos = 0;
38522 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38523 +               if (unlikely(err != PAGE_SIZE))
38524 +                       goto out_free;
38525 +       }
38526 +       err = 0;
38527 +       goto out; /* success */
38528 +
38529 +out_free:
38530 +       if (sbinfo->si_xib_buf)
38531 +               free_page((unsigned long)sbinfo->si_xib_buf);
38532 +       sbinfo->si_xib_buf = NULL;
38533 +       if (err >= 0)
38534 +               err = -EIO;
38535 +out_unset:
38536 +       fput(sbinfo->si_xib);
38537 +       sbinfo->si_xib = NULL;
38538 +out:
38539 +       AuTraceErr(err);
38540 +       return err;
38541 +}
38542 +
38543 +/* xino for each branch */
38544 +static void xino_clear_br(struct super_block *sb)
38545 +{
38546 +       aufs_bindex_t bindex, bbot;
38547 +       struct au_branch *br;
38548 +
38549 +       bbot = au_sbbot(sb);
38550 +       for (bindex = 0; bindex <= bbot; bindex++) {
38551 +               br = au_sbr(sb, bindex);
38552 +               AuDebugOn(!br);
38553 +               au_xino_put(br);
38554 +       }
38555 +}
38556 +
38557 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38558 +                                 aufs_bindex_t bshared)
38559 +{
38560 +       struct au_branch *brshared;
38561 +
38562 +       brshared = au_sbr(sb, bshared);
38563 +       AuDebugOn(!brshared->br_xino);
38564 +       AuDebugOn(!brshared->br_xino->xi_file);
38565 +       if (br->br_xino != brshared->br_xino) {
38566 +               au_xino_get(brshared);
38567 +               au_xino_put(br);
38568 +               br->br_xino = brshared->br_xino;
38569 +       }
38570 +}
38571 +
38572 +struct au_xino_do_set_br {
38573 +       struct au_branch *br;
38574 +       ino_t h_ino;
38575 +       aufs_bindex_t bshared;
38576 +};
38577 +
38578 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38579 +                            struct au_xino_do_set_br *args)
38580 +{
38581 +       int err;
38582 +       struct au_xi_calc calc;
38583 +       struct file *file;
38584 +       struct au_branch *br;
38585 +       struct au_xi_new xinew = {
38586 +               .base = path
38587 +       };
38588 +
38589 +       br = args->br;
38590 +       xinew.xi = br->br_xino;
38591 +       au_xi_calc(sb, args->h_ino, &calc);
38592 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38593 +       if (args->bshared >= 0)
38594 +               /* shared xino */
38595 +               au_xino_set_br_shared(sb, br, args->bshared);
38596 +       else if (!xinew.xi) {
38597 +               /* new xino */
38598 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38599 +               if (unlikely(err))
38600 +                       goto out;
38601 +       }
38602 +
38603 +       /* force re-creating */
38604 +       xinew.xi = br->br_xino;
38605 +       xinew.idx = calc.idx;
38606 +       mutex_lock(&xinew.xi->xi_mtx);
38607 +       file = au_xi_new(sb, &xinew);
38608 +       mutex_unlock(&xinew.xi->xi_mtx);
38609 +       err = PTR_ERR(file);
38610 +       if (IS_ERR(file))
38611 +               goto out;
38612 +       AuDebugOn(!file);
38613 +
38614 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38615 +       if (unlikely(err))
38616 +               au_xino_put(br);
38617 +
38618 +out:
38619 +       AuTraceErr(err);
38620 +       return err;
38621 +}
38622 +
38623 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38624 +{
38625 +       int err;
38626 +       aufs_bindex_t bindex, bbot;
38627 +       struct au_xino_do_set_br args;
38628 +       struct inode *inode;
38629 +
38630 +       SiMustWriteLock(sb);
38631 +
38632 +       bbot = au_sbbot(sb);
38633 +       inode = d_inode(sb->s_root);
38634 +       for (bindex = 0; bindex <= bbot; bindex++) {
38635 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38636 +               args.br = au_sbr(sb, bindex);
38637 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38638 +               err = au_xino_do_set_br(sb, path, &args);
38639 +               if (unlikely(err))
38640 +                       break;
38641 +       }
38642 +
38643 +       AuTraceErr(err);
38644 +       return err;
38645 +}
38646 +
38647 +void au_xino_clr(struct super_block *sb)
38648 +{
38649 +       struct au_sbinfo *sbinfo;
38650 +
38651 +       au_xigen_clr(sb);
38652 +       xino_clear_xib(sb);
38653 +       xino_clear_br(sb);
38654 +       dbgaufs_brs_del(sb, 0);
38655 +       sbinfo = au_sbi(sb);
38656 +       /* lvalue, do not call au_mntflags() */
38657 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38658 +}
38659 +
38660 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38661 +{
38662 +       int err, skip;
38663 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38664 +       struct qstr *dname, *cur_name;
38665 +       struct file *cur_xino;
38666 +       struct au_sbinfo *sbinfo;
38667 +       struct path *path, *cur_path;
38668 +
38669 +       SiMustWriteLock(sb);
38670 +
38671 +       err = 0;
38672 +       sbinfo = au_sbi(sb);
38673 +       path = &xiopt->file->f_path;
38674 +       dentry = path->dentry;
38675 +       parent = dget_parent(dentry);
38676 +       if (remount) {
38677 +               skip = 0;
38678 +               cur_xino = sbinfo->si_xib;
38679 +               if (cur_xino) {
38680 +                       cur_path = &cur_xino->f_path;
38681 +                       cur_dentry = cur_path->dentry;
38682 +                       cur_parent = dget_parent(cur_dentry);
38683 +                       cur_name = &cur_dentry->d_name;
38684 +                       dname = &dentry->d_name;
38685 +                       skip = (cur_parent == parent
38686 +                               && au_qstreq(dname, cur_name));
38687 +                       dput(cur_parent);
38688 +               }
38689 +               if (skip)
38690 +                       goto out;
38691 +       }
38692 +
38693 +       au_opt_set(sbinfo->si_mntflags, XINO);
38694 +       err = au_xino_set_xib(sb, path);
38695 +       /* si_x{read,write} are set */
38696 +       if (!err)
38697 +               err = au_xigen_set(sb, path);
38698 +       if (!err)
38699 +               err = au_xino_set_br(sb, path);
38700 +       if (!err) {
38701 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38702 +               goto out; /* success */
38703 +       }
38704 +
38705 +       /* reset all */
38706 +       AuIOErr("failed setting xino(%d).\n", err);
38707 +       au_xino_clr(sb);
38708 +
38709 +out:
38710 +       dput(parent);
38711 +       return err;
38712 +}
38713 +
38714 +/*
38715 + * create a xinofile at the default place/path.
38716 + */
38717 +struct file *au_xino_def(struct super_block *sb)
38718 +{
38719 +       struct file *file;
38720 +       char *page, *p;
38721 +       struct au_branch *br;
38722 +       struct super_block *h_sb;
38723 +       struct path path;
38724 +       aufs_bindex_t bbot, bindex, bwr;
38725 +
38726 +       br = NULL;
38727 +       bbot = au_sbbot(sb);
38728 +       bwr = -1;
38729 +       for (bindex = 0; bindex <= bbot; bindex++) {
38730 +               br = au_sbr(sb, bindex);
38731 +               if (au_br_writable(br->br_perm)
38732 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38733 +                       bwr = bindex;
38734 +                       break;
38735 +               }
38736 +       }
38737 +
38738 +       if (bwr >= 0) {
38739 +               file = ERR_PTR(-ENOMEM);
38740 +               page = (void *)__get_free_page(GFP_NOFS);
38741 +               if (unlikely(!page))
38742 +                       goto out;
38743 +               path.mnt = au_br_mnt(br);
38744 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38745 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38746 +               file = (void *)p;
38747 +               if (!IS_ERR(p)) {
38748 +                       strcat(p, "/" AUFS_XINO_FNAME);
38749 +                       AuDbg("%s\n", p);
38750 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38751 +               }
38752 +               free_page((unsigned long)page);
38753 +       } else {
38754 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38755 +                                     /*wbrtop*/0);
38756 +               if (IS_ERR(file))
38757 +                       goto out;
38758 +               h_sb = file->f_path.dentry->d_sb;
38759 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38760 +                       pr_err("xino doesn't support %s(%s)\n",
38761 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38762 +                       fput(file);
38763 +                       file = ERR_PTR(-EINVAL);
38764 +               }
38765 +       }
38766 +
38767 +out:
38768 +       return file;
38769 +}
38770 +
38771 +/* ---------------------------------------------------------------------- */
38772 +
38773 +/*
38774 + * initialize the xinofile for the specified branch @br
38775 + * at the place/path where @base_file indicates.
38776 + * test whether another branch is on the same filesystem or not,
38777 + * if found then share the xinofile with another branch.
38778 + */
38779 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38780 +                   struct path *base)
38781 +{
38782 +       int err;
38783 +       struct au_xino_do_set_br args = {
38784 +               .h_ino  = h_ino,
38785 +               .br     = br
38786 +       };
38787 +
38788 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38789 +                                      au_br_sb(br));
38790 +       err = au_xino_do_set_br(sb, base, &args);
38791 +       if (unlikely(err))
38792 +               au_xino_put(br);
38793 +
38794 +       return err;
38795 +}
38796 +
38797 +/* ---------------------------------------------------------------------- */
38798 +
38799 +/*
38800 + * get an unused inode number from bitmap
38801 + */
38802 +ino_t au_xino_new_ino(struct super_block *sb)
38803 +{
38804 +       ino_t ino;
38805 +       unsigned long *p, pindex, ul, pend;
38806 +       struct au_sbinfo *sbinfo;
38807 +       struct file *file;
38808 +       int free_bit, err;
38809 +
38810 +       if (!au_opt_test(au_mntflags(sb), XINO))
38811 +               return iunique(sb, AUFS_FIRST_INO);
38812 +
38813 +       sbinfo = au_sbi(sb);
38814 +       mutex_lock(&sbinfo->si_xib_mtx);
38815 +       p = sbinfo->si_xib_buf;
38816 +       free_bit = sbinfo->si_xib_next_bit;
38817 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38818 +               goto out; /* success */
38819 +       free_bit = find_first_zero_bit(p, page_bits);
38820 +       if (free_bit < page_bits)
38821 +               goto out; /* success */
38822 +
38823 +       pindex = sbinfo->si_xib_last_pindex;
38824 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38825 +               err = xib_pindex(sb, ul);
38826 +               if (unlikely(err))
38827 +                       goto out_err;
38828 +               free_bit = find_first_zero_bit(p, page_bits);
38829 +               if (free_bit < page_bits)
38830 +                       goto out; /* success */
38831 +       }
38832 +
38833 +       file = sbinfo->si_xib;
38834 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38835 +       for (ul = pindex + 1; ul <= pend; ul++) {
38836 +               err = xib_pindex(sb, ul);
38837 +               if (unlikely(err))
38838 +                       goto out_err;
38839 +               free_bit = find_first_zero_bit(p, page_bits);
38840 +               if (free_bit < page_bits)
38841 +                       goto out; /* success */
38842 +       }
38843 +       BUG();
38844 +
38845 +out:
38846 +       set_bit(free_bit, p);
38847 +       sbinfo->si_xib_next_bit = free_bit + 1;
38848 +       pindex = sbinfo->si_xib_last_pindex;
38849 +       mutex_unlock(&sbinfo->si_xib_mtx);
38850 +       ino = xib_calc_ino(pindex, free_bit);
38851 +       AuDbg("i%lu\n", (unsigned long)ino);
38852 +       return ino;
38853 +out_err:
38854 +       mutex_unlock(&sbinfo->si_xib_mtx);
38855 +       AuDbg("i0\n");
38856 +       return 0;
38857 +}
38858 +
38859 +/* for s_op->delete_inode() */
38860 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38861 +{
38862 +       int err;
38863 +       unsigned int mnt_flags;
38864 +       aufs_bindex_t bindex, bbot, bi;
38865 +       unsigned char try_trunc;
38866 +       struct au_iinfo *iinfo;
38867 +       struct super_block *sb;
38868 +       struct au_hinode *hi;
38869 +       struct inode *h_inode;
38870 +       struct au_branch *br;
38871 +       struct au_xi_calc calc;
38872 +       struct file *file;
38873 +
38874 +       AuDebugOn(au_is_bad_inode(inode));
38875 +
38876 +       sb = inode->i_sb;
38877 +       mnt_flags = au_mntflags(sb);
38878 +       if (!au_opt_test(mnt_flags, XINO)
38879 +           || inode->i_ino == AUFS_ROOT_INO)
38880 +               return;
38881 +
38882 +       if (unlinked) {
38883 +               au_xigen_inc(inode);
38884 +               au_xib_clear_bit(inode);
38885 +       }
38886 +
38887 +       iinfo = au_ii(inode);
38888 +       bindex = iinfo->ii_btop;
38889 +       if (bindex < 0)
38890 +               return;
38891 +
38892 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38893 +       hi = au_hinode(iinfo, bindex);
38894 +       bbot = iinfo->ii_bbot;
38895 +       for (; bindex <= bbot; bindex++, hi++) {
38896 +               h_inode = hi->hi_inode;
38897 +               if (!h_inode
38898 +                   || (!unlinked && h_inode->i_nlink))
38899 +                       continue;
38900 +
38901 +               /* inode may not be revalidated */
38902 +               bi = au_br_index(sb, hi->hi_id);
38903 +               if (bi < 0)
38904 +                       continue;
38905 +
38906 +               br = au_sbr(sb, bi);
38907 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38908 +               file = au_xino_file(br->br_xino, calc.idx);
38909 +               if (IS_ERR_OR_NULL(file))
38910 +                       continue;
38911 +
38912 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38913 +               if (!err && try_trunc
38914 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38915 +                       xino_try_trunc(sb, br);
38916 +       }
38917 +}
38918 +
38919 +/* ---------------------------------------------------------------------- */
38920 +
38921 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38922 +{
38923 +       int found, total, i;
38924 +
38925 +       found = -1;
38926 +       total = xi->xi_nondir.total;
38927 +       for (i = 0; i < total; i++) {
38928 +               if (xi->xi_nondir.array[i] != h_ino)
38929 +                       continue;
38930 +               found = i;
38931 +               break;
38932 +       }
38933 +
38934 +       return found;
38935 +}
38936 +
38937 +static int au_xinondir_expand(struct au_xino *xi)
38938 +{
38939 +       int err, sz;
38940 +       ino_t *p;
38941 +
38942 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38943 +
38944 +       err = -ENOMEM;
38945 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38946 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38947 +               goto out;
38948 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38949 +                        /*may_shrink*/0);
38950 +       if (p) {
38951 +               xi->xi_nondir.array = p;
38952 +               xi->xi_nondir.total <<= 1;
38953 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38954 +               err = 0;
38955 +       }
38956 +
38957 +out:
38958 +       return err;
38959 +}
38960 +
38961 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38962 +                      ino_t h_ino, int idx)
38963 +{
38964 +       struct au_xino *xi;
38965 +
38966 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38967 +       xi = au_sbr(sb, bindex)->br_xino;
38968 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38969 +
38970 +       spin_lock(&xi->xi_nondir.spin);
38971 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38972 +       xi->xi_nondir.array[idx] = 0;
38973 +       spin_unlock(&xi->xi_nondir.spin);
38974 +       wake_up_all(&xi->xi_nondir.wqh);
38975 +}
38976 +
38977 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38978 +                     int *idx)
38979 +{
38980 +       int err, found, empty;
38981 +       struct au_xino *xi;
38982 +
38983 +       err = 0;
38984 +       *idx = -1;
38985 +       if (!au_opt_test(au_mntflags(sb), XINO))
38986 +               goto out; /* no xino */
38987 +
38988 +       xi = au_sbr(sb, bindex)->br_xino;
38989 +
38990 +again:
38991 +       spin_lock(&xi->xi_nondir.spin);
38992 +       found = au_xinondir_find(xi, h_ino);
38993 +       if (found == -1) {
38994 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38995 +               if (empty == -1) {
38996 +                       empty = xi->xi_nondir.total;
38997 +                       err = au_xinondir_expand(xi);
38998 +                       if (unlikely(err))
38999 +                               goto out_unlock;
39000 +               }
39001 +               xi->xi_nondir.array[empty] = h_ino;
39002 +               *idx = empty;
39003 +       } else {
39004 +               spin_unlock(&xi->xi_nondir.spin);
39005 +               wait_event(xi->xi_nondir.wqh,
39006 +                          xi->xi_nondir.array[found] != h_ino);
39007 +               goto again;
39008 +       }
39009 +
39010 +out_unlock:
39011 +       spin_unlock(&xi->xi_nondir.spin);
39012 +out:
39013 +       return err;
39014 +}
39015 +
39016 +/* ---------------------------------------------------------------------- */
39017 +
39018 +int au_xino_path(struct seq_file *seq, struct file *file)
39019 +{
39020 +       int err;
39021 +
39022 +       err = au_seq_path(seq, &file->f_path);
39023 +       if (unlikely(err))
39024 +               goto out;
39025 +
39026 +#define Deleted "\\040(deleted)"
39027 +       seq->count -= sizeof(Deleted) - 1;
39028 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
39029 +                        sizeof(Deleted) - 1));
39030 +#undef Deleted
39031 +
39032 +out:
39033 +       return err;
39034 +}
39035 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
39036 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
39037 +++ linux/include/uapi/linux/aufs_type.h        2023-07-04 23:43:55.849649534 +0200
39038 @@ -0,0 +1,452 @@
39039 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
39040 +/*
39041 + * Copyright (C) 2005-2022 Junjiro R. Okajima
39042 + *
39043 + * This program is free software; you can redistribute it and/or modify
39044 + * it under the terms of the GNU General Public License as published by
39045 + * the Free Software Foundation; either version 2 of the License, or
39046 + * (at your option) any later version.
39047 + *
39048 + * This program is distributed in the hope that it will be useful,
39049 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
39050 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39051 + * GNU General Public License for more details.
39052 + *
39053 + * You should have received a copy of the GNU General Public License
39054 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
39055 + */
39056 +
39057 +#ifndef __AUFS_TYPE_H__
39058 +#define __AUFS_TYPE_H__
39059 +
39060 +#define AUFS_NAME      "aufs"
39061 +
39062 +#ifdef __KERNEL__
39063 +/*
39064 + * define it before including all other headers.
39065 + * sched.h may use pr_* macros before defining "current", so define the
39066 + * no-current version first, and re-define later.
39067 + */
39068 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39069 +#include <linux/sched.h>
39070 +#undef pr_fmt
39071 +#define pr_fmt(fmt) \
39072 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39073 +               (int)sizeof(current->comm), current->comm, current->pid
39074 +#include <linux/limits.h>
39075 +#else
39076 +#include <stdint.h>
39077 +#include <sys/types.h>
39078 +#include <limits.h>
39079 +#endif /* __KERNEL__ */
39080 +
39081 +#define AUFS_VERSION   "6.x-rcN-20230515"
39082 +
39083 +/* todo? move this to linux-2.6.19/include/magic.h */
39084 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39085 +
39086 +/* ---------------------------------------------------------------------- */
39087 +
39088 +#ifdef __KERNEL__
39089 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
39090 +typedef int8_t aufs_bindex_t;
39091 +#define AUFS_BRANCH_MAX 127
39092 +#else
39093 +typedef int16_t aufs_bindex_t;
39094 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
39095 +#define AUFS_BRANCH_MAX 511
39096 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39097 +#define AUFS_BRANCH_MAX 1023
39098 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39099 +#define AUFS_BRANCH_MAX 32767
39100 +#endif
39101 +#endif
39102 +
39103 +#ifndef AUFS_BRANCH_MAX
39104 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39105 +#endif
39106 +#endif /* __KERNEL__ */
39107 +
39108 +/* ---------------------------------------------------------------------- */
39109 +
39110 +#define AUFS_FSTYPE            AUFS_NAME
39111 +
39112 +#define AUFS_ROOT_INO          2
39113 +#define AUFS_FIRST_INO         11
39114 +
39115 +#define AUFS_WH_PFX            ".wh."
39116 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
39117 +#define AUFS_WH_TMP_LEN                4
39118 +/* a limit for rmdir/rename a dir and copyup */
39119 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
39120 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
39121 +                               - 1                     /* dot */\
39122 +                               - AUFS_WH_TMP_LEN)      /* hex */
39123 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
39124 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
39125 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
39126 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
39127 +#define AUFS_DIRWH_DEF         3
39128 +#define AUFS_RDCACHE_DEF       10 /* seconds */
39129 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
39130 +#define AUFS_RDBLK_DEF         512 /* bytes */
39131 +#define AUFS_RDHASH_DEF                32
39132 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
39133 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
39134 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
39135 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
39136 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
39137 +
39138 +/* pseudo-link maintenace under /proc */
39139 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
39140 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
39141 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39142 +
39143 +/* dirren, renamed dir */
39144 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
39145 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
39146 +/* whiteouted doubly */
39147 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
39148 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39149 +
39150 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39151 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39152 +
39153 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39154 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39155 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39156 +
39157 +/* doubly whiteouted */
39158 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39159 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39160 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39161 +
39162 +/* branch permissions and attributes */
39163 +#define AUFS_BRPERM_RW         "rw"
39164 +#define AUFS_BRPERM_RO         "ro"
39165 +#define AUFS_BRPERM_RR         "rr"
39166 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39167 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39168 +#define AUFS_BRATTR_FHSM       "fhsm"
39169 +#define AUFS_BRATTR_UNPIN      "unpin"
39170 +#define AUFS_BRATTR_ICEX       "icex"
39171 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39172 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39173 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39174 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39175 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39176 +#define AUFS_BRRATTR_WH                "wh"
39177 +#define AUFS_BRWATTR_NLWH      "nolwh"
39178 +#define AUFS_BRWATTR_MOO       "moo"
39179 +
39180 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39181 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39182 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39183 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39184 +
39185 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39186 +#define AuBrAttr_COO_ALL       (1 << 4)
39187 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39188 +
39189 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39190 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39191 +                                                  branch. meaningless since
39192 +                                                  linux-3.18-rc1 */
39193 +
39194 +/* ignore error in copying XATTR */
39195 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39196 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39197 +#define AuBrAttr_ICEX_TR       (1 << 9)
39198 +#define AuBrAttr_ICEX_USR      (1 << 10)
39199 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39200 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39201 +                                | AuBrAttr_ICEX_SYS    \
39202 +                                | AuBrAttr_ICEX_TR     \
39203 +                                | AuBrAttr_ICEX_USR    \
39204 +                                | AuBrAttr_ICEX_OTH)
39205 +
39206 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39207 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39208 +
39209 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39210 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39211 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39212 +
39213 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39214 +
39215 +/* #warning test userspace */
39216 +#ifdef __KERNEL__
39217 +#ifndef CONFIG_AUFS_FHSM
39218 +#undef AuBrAttr_FHSM
39219 +#define AuBrAttr_FHSM          0
39220 +#endif
39221 +#ifndef CONFIG_AUFS_XATTR
39222 +#undef AuBrAttr_ICEX
39223 +#define AuBrAttr_ICEX          0
39224 +#undef AuBrAttr_ICEX_SEC
39225 +#define AuBrAttr_ICEX_SEC      0
39226 +#undef AuBrAttr_ICEX_SYS
39227 +#define AuBrAttr_ICEX_SYS      0
39228 +#undef AuBrAttr_ICEX_TR
39229 +#define AuBrAttr_ICEX_TR       0
39230 +#undef AuBrAttr_ICEX_USR
39231 +#define AuBrAttr_ICEX_USR      0
39232 +#undef AuBrAttr_ICEX_OTH
39233 +#define AuBrAttr_ICEX_OTH      0
39234 +#endif
39235 +#endif
39236 +
39237 +/* the longest combination */
39238 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39239 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39240 +                              "+" AUFS_BRATTR_COO_REG          \
39241 +                              "+" AUFS_BRATTR_FHSM             \
39242 +                              "+" AUFS_BRATTR_UNPIN            \
39243 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39244 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39245 +                              "+" AUFS_BRATTR_ICEX_USR         \
39246 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39247 +                              "+" AUFS_BRWATTR_NLWH)
39248 +
39249 +typedef struct {
39250 +       char a[AuBrPermStrSz];
39251 +} au_br_perm_str_t;
39252 +
39253 +static inline int au_br_writable(int brperm)
39254 +{
39255 +       return brperm & AuBrPerm_RW;
39256 +}
39257 +
39258 +static inline int au_br_whable(int brperm)
39259 +{
39260 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39261 +}
39262 +
39263 +static inline int au_br_wh_linkable(int brperm)
39264 +{
39265 +       return !(brperm & AuBrWAttr_NoLinkWH);
39266 +}
39267 +
39268 +static inline int au_br_cmoo(int brperm)
39269 +{
39270 +       return brperm & AuBrAttr_CMOO_Mask;
39271 +}
39272 +
39273 +static inline int au_br_fhsm(int brperm)
39274 +{
39275 +       return brperm & AuBrAttr_FHSM;
39276 +}
39277 +
39278 +/* ---------------------------------------------------------------------- */
39279 +
39280 +/* ioctl */
39281 +enum {
39282 +       /* readdir in userspace */
39283 +       AuCtl_RDU,
39284 +       AuCtl_RDU_INO,
39285 +
39286 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39287 +       AuCtl_IBUSY,    /* busy inode */
39288 +       AuCtl_MVDOWN,   /* move-down */
39289 +       AuCtl_BR,       /* info about branches */
39290 +       AuCtl_FHSM_FD   /* connection for fhsm */
39291 +};
39292 +
39293 +/* borrowed from linux/include/linux/kernel.h */
39294 +#ifndef ALIGN
39295 +#ifdef _GNU_SOURCE
39296 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39297 +#else
39298 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39299 +#endif
39300 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39301 +#endif
39302 +
39303 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39304 +#ifndef __aligned
39305 +#define __aligned(x)                   __attribute__((aligned(x)))
39306 +#endif
39307 +
39308 +#ifdef __KERNEL__
39309 +#ifndef __packed
39310 +#define __packed                       __attribute__((packed))
39311 +#endif
39312 +#endif
39313 +
39314 +struct au_rdu_cookie {
39315 +       uint64_t        h_pos;
39316 +       int16_t         bindex;
39317 +       uint8_t         flags;
39318 +       uint8_t         pad;
39319 +       uint32_t        generation;
39320 +} __aligned(8);
39321 +
39322 +struct au_rdu_ent {
39323 +       uint64_t        ino;
39324 +       int16_t         bindex;
39325 +       uint8_t         type;
39326 +       uint8_t         nlen;
39327 +       uint8_t         wh;
39328 +       char            name[];
39329 +} __aligned(8);
39330 +
39331 +static inline int au_rdu_len(int nlen)
39332 +{
39333 +       /* include the terminating NULL */
39334 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39335 +                    sizeof(uint64_t));
39336 +}
39337 +
39338 +union au_rdu_ent_ul {
39339 +       struct au_rdu_ent __user        *e;
39340 +       uint64_t                        ul;
39341 +};
39342 +
39343 +enum {
39344 +       AufsCtlRduV_SZ,
39345 +       AufsCtlRduV_End
39346 +};
39347 +
39348 +struct aufs_rdu {
39349 +       /* input */
39350 +       union {
39351 +               uint64_t        sz;     /* AuCtl_RDU */
39352 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39353 +       };
39354 +       union au_rdu_ent_ul     ent;
39355 +       uint16_t                verify[AufsCtlRduV_End];
39356 +
39357 +       /* input/output */
39358 +       uint32_t                blk;
39359 +
39360 +       /* output */
39361 +       union au_rdu_ent_ul     tail;
39362 +       /* number of entries which were added in a single call */
39363 +       uint64_t                rent;
39364 +       uint8_t                 full;
39365 +       uint8_t                 shwh;
39366 +
39367 +       struct au_rdu_cookie    cookie;
39368 +} __aligned(8);
39369 +
39370 +/* ---------------------------------------------------------------------- */
39371 +
39372 +/* dirren. the branch is identified by the filename who contains this */
39373 +struct au_drinfo {
39374 +       uint64_t ino;
39375 +       union {
39376 +               uint8_t oldnamelen;
39377 +               uint64_t _padding;
39378 +       };
39379 +       uint8_t oldname[];
39380 +} __aligned(8);
39381 +
39382 +struct au_drinfo_fdata {
39383 +       uint32_t magic;
39384 +       struct au_drinfo drinfo;
39385 +} __aligned(8);
39386 +
39387 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39388 +/* future */
39389 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39390 +
39391 +/* ---------------------------------------------------------------------- */
39392 +
39393 +struct aufs_wbr_fd {
39394 +       uint32_t        oflags;
39395 +       int16_t         brid;
39396 +} __aligned(8);
39397 +
39398 +/* ---------------------------------------------------------------------- */
39399 +
39400 +struct aufs_ibusy {
39401 +       uint64_t        ino, h_ino;
39402 +       int16_t         bindex;
39403 +} __aligned(8);
39404 +
39405 +/* ---------------------------------------------------------------------- */
39406 +
39407 +/* error code for move-down */
39408 +/* the actual message strings are implemented in aufs-util.git */
39409 +enum {
39410 +       EAU_MVDOWN_OPAQUE = 1,
39411 +       EAU_MVDOWN_WHITEOUT,
39412 +       EAU_MVDOWN_UPPER,
39413 +       EAU_MVDOWN_BOTTOM,
39414 +       EAU_MVDOWN_NOUPPER,
39415 +       EAU_MVDOWN_NOLOWERBR,
39416 +       EAU_Last
39417 +};
39418 +
39419 +/* flags for move-down */
39420 +#define AUFS_MVDOWN_DMSG       1
39421 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39422 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39423 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39424 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39425 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39426 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39427 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39428 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39429 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39430 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39431 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39432 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39433 +
39434 +/* index for move-down */
39435 +enum {
39436 +       AUFS_MVDOWN_UPPER,
39437 +       AUFS_MVDOWN_LOWER,
39438 +       AUFS_MVDOWN_NARRAY
39439 +};
39440 +
39441 +/*
39442 + * additional info of move-down
39443 + * number of free blocks and inodes.
39444 + * subset of struct kstatfs, but smaller and always 64bit.
39445 + */
39446 +struct aufs_stfs {
39447 +       uint64_t        f_blocks;
39448 +       uint64_t        f_bavail;
39449 +       uint64_t        f_files;
39450 +       uint64_t        f_ffree;
39451 +};
39452 +
39453 +struct aufs_stbr {
39454 +       int16_t                 brid;   /* optional input */
39455 +       int16_t                 bindex; /* output */
39456 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39457 +} __aligned(8);
39458 +
39459 +struct aufs_mvdown {
39460 +       uint32_t                flags;                  /* input/output */
39461 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39462 +       int8_t                  au_errno;               /* output */
39463 +} __aligned(8);
39464 +
39465 +/* ---------------------------------------------------------------------- */
39466 +
39467 +union aufs_brinfo {
39468 +       /* PATH_MAX may differ between kernel-space and user-space */
39469 +       char    _spacer[4096];
39470 +       struct {
39471 +               int16_t id;
39472 +               int     perm;
39473 +               char    path[];
39474 +       };
39475 +} __aligned(8);
39476 +
39477 +/* ---------------------------------------------------------------------- */
39478 +
39479 +#define AuCtlType              'A'
39480 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39481 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39482 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39483 +                                    struct aufs_wbr_fd)
39484 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39485 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39486 +                                     struct aufs_mvdown)
39487 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39488 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39489 +
39490 +#endif /* __AUFS_TYPE_H__ */
39491 SPDX-License-Identifier: GPL-2.0
39492 aufs6.x-rcN loopback patch
39493
39494 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39495 index 131294601819..56016fc9c9d8 100644
39496 --- a/drivers/block/loop.c
39497 +++ b/drivers/block/loop.c
39498 @@ -54,7 +54,7 @@ struct loop_device {
39499         int             lo_flags;
39500         char            lo_file_name[LO_NAME_SIZE];
39501  
39502 -       struct file *   lo_backing_file;
39503 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39504         struct block_device *lo_device;
39505  
39506         gfp_t           old_gfp_mask;
39507 @@ -510,6 +510,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39508                                 lo->use_dio);
39509  }
39510  
39511 +static struct file *loop_real_file(struct file *file)
39512 +{
39513 +       struct file *f = NULL;
39514 +
39515 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39516 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39517 +       return f;
39518 +}
39519 +
39520  static void loop_reread_partitions(struct loop_device *lo)
39521  {
39522         int rc;
39523 @@ -567,6 +576,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39524  {
39525         struct file *file = fget(arg);
39526         struct file *old_file;
39527 +       struct file *f, *virt_file = NULL, *old_virt_file;
39528         int error;
39529         bool partscan;
39530         bool is_loop;
39531 @@ -590,11 +600,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39532         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39533                 goto out_err;
39534  
39535 +       f = loop_real_file(file);
39536 +       if (f) {
39537 +               virt_file = file;
39538 +               file = f;
39539 +               get_file(file);
39540 +       }
39541 +
39542         error = loop_validate_file(file, bdev);
39543         if (error)
39544                 goto out_err;
39545  
39546         old_file = lo->lo_backing_file;
39547 +       old_virt_file = lo->lo_backing_virt_file;
39548  
39549         error = -EINVAL;
39550  
39551 @@ -607,6 +625,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39552         blk_mq_freeze_queue(lo->lo_queue);
39553         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39554         lo->lo_backing_file = file;
39555 +       lo->lo_backing_virt_file = virt_file;
39556         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39557         mapping_set_gfp_mask(file->f_mapping,
39558                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39559 @@ -629,6 +648,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39560          * dependency.
39561          */
39562         fput(old_file);
39563 +       if (old_virt_file)
39564 +               fput(old_virt_file);
39565         if (partscan)
39566                 loop_reread_partitions(lo);
39567  
39568 @@ -642,6 +663,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39569         loop_global_unlock(lo, is_loop);
39570  out_putf:
39571         fput(file);
39572 +       if (virt_file)
39573 +               fput(virt_file);
39574         goto done;
39575  }
39576  
39577 @@ -1013,6 +1036,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39578                           const struct loop_config *config)
39579  {
39580         struct file *file = fget(config->fd);
39581 +       struct file *f, *virt_file = NULL;
39582         struct inode *inode;
39583         struct address_space *mapping;
39584         int error;
39585 @@ -1028,6 +1052,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39586         /* This is safe, since we have a reference from open(). */
39587         __module_get(THIS_MODULE);
39588  
39589 +       f = loop_real_file(file);
39590 +       if (f) {
39591 +               virt_file = file;
39592 +               file = f;
39593 +               get_file(file);
39594 +       }
39595 +
39596         /*
39597          * If we don't hold exclusive handle for the device, upgrade to it
39598          * here to avoid changing device under exclusive owner.
39599 @@ -1091,6 +1122,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39600         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39601         lo->lo_device = bdev;
39602         lo->lo_backing_file = file;
39603 +       lo->lo_backing_virt_file = virt_file;
39604         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39605         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39606  
39607 @@ -1146,6 +1178,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39608                 bd_abort_claiming(bdev, loop_configure);
39609  out_putf:
39610         fput(file);
39611 +       if (virt_file)
39612 +               fput(virt_file);
39613         /* This is safe: open() is still holding a reference. */
39614         module_put(THIS_MODULE);
39615         return error;
39616 @@ -1154,6 +1188,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39617  static void __loop_clr_fd(struct loop_device *lo, bool release)
39618  {
39619         struct file *filp;
39620 +       struct file *virt_filp = lo->lo_backing_virt_file;
39621         gfp_t gfp = lo->old_gfp_mask;
39622  
39623         if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
39624 @@ -1170,6 +1205,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39625         spin_lock_irq(&lo->lo_lock);
39626         filp = lo->lo_backing_file;
39627         lo->lo_backing_file = NULL;
39628 +       lo->lo_backing_virt_file = NULL;
39629         spin_unlock_irq(&lo->lo_lock);
39630  
39631         lo->lo_device = NULL;
39632 @@ -1232,6 +1268,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39633          * fput can take open_mutex which is usually taken before lo_mutex.
39634          */
39635         fput(filp);
39636 +       if (virt_filp)
39637 +               fput(virt_filp);
39638  }
39639  
39640  static int loop_clr_fd(struct loop_device *lo)
39641 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39642 index 1ed300047a41..ea63191d6473 100644
39643 --- a/fs/aufs/f_op.c
39644 +++ b/fs/aufs/f_op.c
39645 @@ -309,7 +309,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39646         if (IS_ERR(h_file))
39647                 goto out;
39648  
39649 -       if (au_test_loopback_kthread()) {
39650 +       if (0 && au_test_loopback_kthread()) {
39651                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39652                 if (file->f_mapping != h_file->f_mapping) {
39653                         file->f_mapping = h_file->f_mapping;
39654 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39655 index 58043e31e5f3..e2bfae6f9d59 100644
39656 --- a/fs/aufs/loop.c
39657 +++ b/fs/aufs/loop.c
39658 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39659                 symbol_put(loop_backing_file);
39660         au_kfree_try_rcu(au_warn_loopback_array);
39661  }
39662 +
39663 +/* ---------------------------------------------------------------------- */
39664 +
39665 +/* support the loopback block device insude aufs */
39666 +
39667 +struct file *aufs_real_loop(struct file *file)
39668 +{
39669 +       struct file *f;
39670 +
39671 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39672 +       fi_read_lock(file);
39673 +       f = au_hf_top(file);
39674 +       fi_read_unlock(file);
39675 +       AuDebugOn(!f);
39676 +       return f;
39677 +}
39678 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39679 index 03d4908a6c03..34d356e181d5 100644
39680 --- a/fs/aufs/loop.h
39681 +++ b/fs/aufs/loop.h
39682 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39683  
39684  int au_loopback_init(void);
39685  void au_loopback_fin(void);
39686 +
39687 +struct file *aufs_real_loop(struct file *file);
39688  #else
39689  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39690  
39691 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39692  
39693  AuStubInt0(au_loopback_init, void)
39694  AuStubVoid(au_loopback_fin, void)
39695 +
39696 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39697  #endif /* BLK_DEV_LOOP */
39698  
39699  #endif /* __KERNEL__ */
39700 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39701 index 81922d4faf54..c8a62c267d72 100644
39702 --- a/fs/aufs/super.c
39703 +++ b/fs/aufs/super.c
39704 @@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39705         .show_options   = aufs_show_options,
39706         .statfs         = aufs_statfs,
39707         .put_super      = aufs_put_super,
39708 -       .sync_fs        = aufs_sync_fs
39709 +       .sync_fs        = aufs_sync_fs,
39710 +#ifdef CONFIG_AUFS_BDEV_LOOP
39711 +       .real_loop      = aufs_real_loop
39712 +#endif
39713  };
39714  
39715  /* ---------------------------------------------------------------------- */
39716 diff --git a/include/linux/fs.h b/include/linux/fs.h
39717 index 1e3054e5367d..2a780fcbdc75 100644
39718 --- a/include/linux/fs.h
39719 +++ b/include/linux/fs.h
39720 @@ -1934,6 +1934,10 @@ struct super_operations {
39721                                   struct shrink_control *);
39722         long (*free_cached_objects)(struct super_block *,
39723                                     struct shrink_control *);
39724 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39725 +       /* and aufs */
39726 +       struct file *(*real_loop)(struct file *);
39727 +#endif
39728  };
39729  
39730  /*
This page took 3.060406 seconds and 3 git commands to generate.