]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs.patch
up to 6.5.5
[packages/kernel.git] / kernel-aufs.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs6.5 kbuild patch
3
4 diff --git a/fs/Kconfig b/fs/Kconfig
5 index 18d034ec7953..3159bcb16918 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 e513aaee0603..acdea5771285 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -129,3 +129,4 @@ obj-$(CONFIG_EFIVAR_FS)             += efivarfs/
21  obj-$(CONFIG_EROFS_FS)         += erofs/
22  obj-$(CONFIG_VBOXSF_FS)                += vboxsf/
23  obj-$(CONFIG_ZONEFS_FS)                += zonefs/
24 +obj-$(CONFIG_AUFS_FS)           += aufs/
25 SPDX-License-Identifier: GPL-2.0
26 aufs6.5 base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index 4cc6bf79fdd8..5578e44ad7e2 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3310,6 +3310,19 @@ F:       include/uapi/linux/audit.h
33  F:     kernel/audit*
34  F:     lib/*audit.c
35  
36 +AUFS (advanced multi layered unification filesystem) FILESYSTEM
37 +M:     "J. R. Okajima" <hooanon05g@gmail.com>
38 +L:     aufs-users@lists.sourceforge.net (members only)
39 +L:     linux-unionfs@vger.kernel.org
40 +S:     Supported
41 +W:     http://aufs.sourceforge.net
42 +T:     git://github.com/sfjro/aufs4-linux.git
43 +F:     Documentation/ABI/testing/debugfs-aufs
44 +F:     Documentation/ABI/testing/sysfs-aufs
45 +F:     Documentation/filesystems/aufs/
46 +F:     fs/aufs/
47 +F:     include/uapi/linux/aufs_type.h
48 +
49  AUXILIARY BUS DRIVER
50  M:     Greg Kroah-Hartman <gregkh@linuxfoundation.org>
51  R:     Dave Ertman <david.m.ertman@intel.com>
52 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53 index 637c5bda2387..cbefd23beb44 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 e157efc54023..6c57487f126b 100644
118 --- a/fs/namespace.c
119 +++ b/fs/namespace.c
120 @@ -872,6 +872,12 @@ static inline int check_mnt(struct mount *mnt)
121         return mnt->mnt_ns == current->nsproxy->mnt_ns;
122  }
123  
124 +/* for aufs, CONFIG_AUFS_BR_FUSE */
125 +int is_current_mnt_ns(struct vfsmount *mnt)
126 +{
127 +       return check_mnt(real_mount(mnt));
128 +}
129 +
130  /*
131   * vfsmount lock must be held for write
132   */
133 diff --git a/fs/splice.c b/fs/splice.c
134 index 3e2a31e1ce6a..a724fe9ccb80 100644
135 --- a/fs/splice.c
136 +++ b/fs/splice.c
137 @@ -928,8 +928,8 @@ static int warn_unsupported(struct file *file, const char *op)
138  /*
139   * Attempt to initiate a splice from pipe to file.
140   */
141 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
142 -                          loff_t *ppos, size_t len, unsigned int flags)
143 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
144 +                   loff_t *ppos, size_t len, unsigned int flags)
145  {
146         if (unlikely(!out->f_op->splice_write))
147                 return warn_unsupported(out, "write");
148 diff --git a/include/linux/fs.h b/include/linux/fs.h
149 index 562f2623c9c9..5a2db7b8eca5 100644
150 --- a/include/linux/fs.h
151 +++ b/include/linux/fs.h
152 @@ -1068,6 +1068,7 @@ extern void fasync_free(struct fasync_struct *);
153  /* can be called from interrupts */
154  extern void kill_fasync(struct fasync_struct **, int, int);
155  
156 +extern int setfl(int fd, struct file *filp, unsigned long arg);
157  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
158  extern int f_setown(struct file *filp, unsigned long arg, int force);
159  extern void f_delown(struct file *filp);
160 @@ -1794,6 +1795,7 @@ struct file_operations {
161         int (*lock) (struct file *, int, struct file_lock *);
162         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
163         int (*check_flags)(int);
164 +       int (*setfl)(struct file *, unsigned long);
165         int (*flock) (struct file *, int, struct file_lock *);
166         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
167         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
168 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
169 index 310f85903c91..46ff52c1a522 100644
170 --- a/include/linux/lockdep.h
171 +++ b/include/linux/lockdep.h
172 @@ -249,6 +249,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
173         return lock->key == key;
174  }
175  
176 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
177 +
178  /*
179   * Acquire a lock.
180   *
181 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
182 index 8f882f5881e8..6b9808f09843 100644
183 --- a/include/linux/mnt_namespace.h
184 +++ b/include/linux/mnt_namespace.h
185 @@ -7,12 +7,15 @@ struct mnt_namespace;
186  struct fs_struct;
187  struct user_namespace;
188  struct ns_common;
189 +struct vfsmount;
190  
191  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
192                 struct user_namespace *, struct fs_struct *);
193  extern void put_mnt_ns(struct mnt_namespace *ns);
194  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
195  
196 +extern int is_current_mnt_ns(struct vfsmount *mnt);
197 +
198  extern const struct file_operations proc_mounts_operations;
199  extern const struct file_operations proc_mountinfo_operations;
200  extern const struct file_operations proc_mountstats_operations;
201 diff --git a/include/linux/splice.h b/include/linux/splice.h
202 index 6c461573434d..7416cf375ad1 100644
203 --- a/include/linux/splice.h
204 +++ b/include/linux/splice.h
205 @@ -99,4 +99,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
206  
207  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
208  extern const struct pipe_buf_operations default_pipe_buf_ops;
209 +
210 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
211 +                          loff_t *ppos, size_t len, unsigned int flags);
212 +extern long do_splice_to(struct file *in, loff_t *ppos,
213 +                        struct pipe_inode_info *pipe, size_t len,
214 +                        unsigned int flags);
215  #endif
216 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
217 index 111607d91489..22af30abc781 100644
218 --- a/kernel/locking/lockdep.c
219 +++ b/kernel/locking/lockdep.c
220 @@ -218,7 +218,7 @@ unsigned long max_lock_class_idx;
221  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
222  DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
223  
224 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
225 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
226  {
227         unsigned int class_idx = hlock->class_idx;
228  
229 @@ -239,6 +239,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
230          */
231         return lock_classes + class_idx;
232  }
233 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
234  
235  #ifdef CONFIG_LOCK_STAT
236  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
237 SPDX-License-Identifier: GPL-2.0
238 aufs6.5 mmap patch
239
240 diff --git a/fs/proc/base.c b/fs/proc/base.c
241 index 9df3f4839662..102a56aebeef 100644
242 --- a/fs/proc/base.c
243 +++ b/fs/proc/base.c
244 @@ -2218,7 +2218,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
245         rc = -ENOENT;
246         vma = find_exact_vma(mm, vm_start, vm_end);
247         if (vma && vma->vm_file) {
248 -               *path = vma->vm_file->f_path;
249 +               *path = vma_pr_or_file(vma)->f_path;
250                 path_get(path);
251                 rc = 0;
252         }
253 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
254 index 4d3493579458..42edd9a42c78 100644
255 --- a/fs/proc/nommu.c
256 +++ b/fs/proc/nommu.c
257 @@ -39,7 +39,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
258         file = region->vm_file;
259  
260         if (file) {
261 -               struct inode *inode = file_inode(region->vm_file);
262 +               struct inode *inode;
263 +
264 +               file = vmr_pr_or_file(region);
265 +               inode = file_inode(file);
266                 dev = inode->i_sb->s_dev;
267                 ino = inode->i_ino;
268         }
269 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
270 index fafff1bd34cd..4387f43addcd 100644
271 --- a/fs/proc/task_mmu.c
272 +++ b/fs/proc/task_mmu.c
273 @@ -285,7 +285,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
274         const char *name = NULL;
275  
276         if (file) {
277 -               struct inode *inode = file_inode(vma->vm_file);
278 +               struct inode *inode;
279 +
280 +               file = vma_pr_or_file(vma);
281 +               inode = file_inode(file);
282                 dev = inode->i_sb->s_dev;
283                 ino = inode->i_ino;
284                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
285 @@ -1950,7 +1953,7 @@ static int show_numa_map(struct seq_file *m, void *v)
286         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
287         struct vm_area_struct *vma = v;
288         struct numa_maps *md = &numa_priv->md;
289 -       struct file *file = vma->vm_file;
290 +       struct file *file = vma_pr_or_file(vma);
291         struct mm_struct *mm = vma->vm_mm;
292         struct mempolicy *pol;
293         char buffer[64];
294 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
295 index 2c8b62265981..9f65138246a8 100644
296 --- a/fs/proc/task_nommu.c
297 +++ b/fs/proc/task_nommu.c
298 @@ -150,7 +150,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
299         file = vma->vm_file;
300  
301         if (file) {
302 -               struct inode *inode = file_inode(vma->vm_file);
303 +               struct inode *inode;
304 +
305 +               file = vma_pr_or_file(vma);
306 +               inode = file_inode(file);
307                 dev = inode->i_sb->s_dev;
308                 ino = inode->i_ino;
309                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
310 diff --git a/include/linux/mm.h b/include/linux/mm.h
311 index 34f9dba17c1a..0f01f02a3112 100644
312 --- a/include/linux/mm.h
313 +++ b/include/linux/mm.h
314 @@ -2387,6 +2387,43 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
315  static inline struct vm_area_struct *vma_lookup(struct mm_struct *mm,
316                                                 unsigned long addr);
317  
318 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
319 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
320 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
321 +                                     int);
322 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
323 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
324 +
325 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
326 +                                                               __LINE__)
327 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
328 +                                                         __LINE__)
329 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
330 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
331 +
332 +#ifndef CONFIG_MMU
333 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
334 +extern void vmr_do_fput(struct vm_region *, const char[], int);
335 +
336 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
337 +                                                         __LINE__)
338 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
339 +#endif /* !CONFIG_MMU */
340 +
341 +#else
342 +
343 +#define vma_file_update_time(vma)      file_update_time((vma)->vm_file)
344 +#define vma_pr_or_file(vma)            (vma)->vm_file
345 +#define vma_get_file(vma)              get_file((vma)->vm_file)
346 +#define vma_fput(vma)                  fput((vma)->vm_file)
347 +
348 +#ifndef CONFIG_MMU
349 +#define vmr_pr_or_file(region)         (region)->vm_file
350 +#define vmr_fput(region)               fput((region)->vm_file)
351 +#endif /* !CONFIG_MMU */
352 +
353 +#endif /* CONFIG_AUFS_FS */
354 +
355  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
356                 void *buf, int len, unsigned int gup_flags);
357  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
358 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
359 index 7d30dc4ff0ff..5a712268b2d7 100644
360 --- a/include/linux/mm_types.h
361 +++ b/include/linux/mm_types.h
362 @@ -449,6 +449,9 @@ struct vm_region {
363         unsigned long   vm_top;         /* region allocated to here */
364         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
365         struct file     *vm_file;       /* the backing file or NULL */
366 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
367 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
368 +#endif
369  
370         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
371         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
372 @@ -562,6 +565,9 @@ struct vm_area_struct {
373         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
374                                            units */
375         struct file * vm_file;          /* File we map to (can be NULL). */
376 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
377 +       struct file *vm_prfile;         /* shadow of vm_file */
378 +#endif
379         void * vm_private_data;         /* was vm_pte (shared mem) */
380  
381  #ifdef CONFIG_ANON_VMA_NAME
382 diff --git a/kernel/fork.c b/kernel/fork.c
383 index d2e12b6d2b18..39da83472c1d 100644
384 --- a/kernel/fork.c
385 +++ b/kernel/fork.c
386 @@ -731,7 +731,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
387                 if (file) {
388                         struct address_space *mapping = file->f_mapping;
389  
390 -                       get_file(file);
391 +                       vma_get_file(tmp);
392                         i_mmap_lock_write(mapping);
393                         if (tmp->vm_flags & VM_SHARED)
394                                 mapping_allow_writable(mapping);
395 diff --git a/mm/Makefile b/mm/Makefile
396 index 678530a07326..ce3cbfef4a61 100644
397 --- a/mm/Makefile
398 +++ b/mm/Makefile
399 @@ -139,3 +139,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o
400  obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
401  obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
402  obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
403 +obj-y += prfile.o
404 diff --git a/mm/filemap.c b/mm/filemap.c
405 index 9e44a49bbd74..334425b2903b 100644
406 --- a/mm/filemap.c
407 +++ b/mm/filemap.c
408 @@ -3580,7 +3580,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
409         vm_fault_t ret = VM_FAULT_LOCKED;
410  
411         sb_start_pagefault(mapping->host->i_sb);
412 -       file_update_time(vmf->vma->vm_file);
413 +       vma_file_update_time(vmf->vma);
414         folio_lock(folio);
415         if (folio->mapping != mapping) {
416                 folio_unlock(folio);
417 diff --git a/mm/mmap.c b/mm/mmap.c
418 index 3937479d0e07..db30d8a8cea3 100644
419 --- a/mm/mmap.c
420 +++ b/mm/mmap.c
421 @@ -140,7 +140,7 @@ static void remove_vma(struct vm_area_struct *vma, bool unreachable)
422         if (vma->vm_ops && vma->vm_ops->close)
423                 vma->vm_ops->close(vma);
424         if (vma->vm_file)
425 -               fput(vma->vm_file);
426 +               vma_fput(vma);
427         mpol_put(vma_policy(vma));
428         if (unreachable)
429                 __vm_area_free(vma);
430 @@ -575,7 +575,7 @@ static inline void vma_complete(struct vma_prepare *vp,
431                 if (vp->file) {
432                         uprobe_munmap(vp->remove, vp->remove->vm_start,
433                                       vp->remove->vm_end);
434 -                       fput(vp->file);
435 +                       vma_fput(vp->vma);
436                 }
437                 if (vp->remove->anon_vma)
438                         anon_vma_merge(vp->vma, vp->remove);
439 @@ -2365,7 +2365,7 @@ int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
440                 goto out_free_mpol;
441  
442         if (new->vm_file)
443 -               get_file(new->vm_file);
444 +               vma_get_file(new);
445  
446         if (new->vm_ops && new->vm_ops->open)
447                 new->vm_ops->open(new);
448 @@ -2778,7 +2778,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
449                                  * and cause general protection fault
450                                  * ultimately.
451                                  */
452 -                               fput(vma->vm_file);
453 +                               vma_fput(vma);
454                                 vm_area_free(vma);
455                                 vma = merge;
456                                 /* Update vm_flags to pick up the change. */
457 @@ -2875,7 +2875,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
458  
459         if (file || vma->vm_file) {
460  unmap_and_free_vma:
461 -               fput(vma->vm_file);
462 +               vma_fput(vma);
463                 vma->vm_file = NULL;
464  
465                 /* Undo any partial mapping done by a device driver. */
466 @@ -2936,6 +2936,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
467         unsigned long populate = 0;
468         unsigned long ret = -EINVAL;
469         struct file *file;
470 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
471 +       struct file *prfile;
472 +#endif
473  
474         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
475                      current->comm, current->pid);
476 @@ -2994,10 +2997,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
477         if (vma->vm_flags & VM_LOCKED)
478                 flags |= MAP_LOCKED;
479  
480 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
481 +       vma_get_file(vma);
482 +       file = vma->vm_file;
483 +       prfile = vma->vm_prfile;
484 +       ret = do_mmap(vma->vm_file, start, size,
485 +                       prot, flags, pgoff, &populate, NULL);
486 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
487 +               struct vm_area_struct *new_vma;
488 +
489 +               new_vma = find_vma(mm, ret);
490 +               if (!new_vma->vm_prfile)
491 +                       new_vma->vm_prfile = prfile;
492 +               if (prfile)
493 +                       get_file(prfile);
494 +       }
495 +       /*
496 +        * two fput()s instead of vma_fput(vma),
497 +        * coz vma may not be available anymore.
498 +        */
499 +       fput(file);
500 +       if (prfile)
501 +               fput(prfile);
502 +#else
503         file = get_file(vma->vm_file);
504         ret = do_mmap(vma->vm_file, start, size,
505                         prot, flags, pgoff, &populate, NULL);
506         fput(file);
507 +#endif /* CONFIG_AUFS_FS */
508  out:
509         mmap_write_unlock(mm);
510         if (populate)
511 @@ -3342,7 +3369,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
512                 if (anon_vma_clone(new_vma, vma))
513                         goto out_free_mempol;
514                 if (new_vma->vm_file)
515 -                       get_file(new_vma->vm_file);
516 +                       vma_get_file(new_vma);
517                 if (new_vma->vm_ops && new_vma->vm_ops->open)
518                         new_vma->vm_ops->open(new_vma);
519                 vma_start_write(new_vma);
520 @@ -3358,7 +3385,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
521                 new_vma->vm_ops->close(new_vma);
522  
523         if (new_vma->vm_file)
524 -               fput(new_vma->vm_file);
525 +               vma_fput(new_vma);
526  
527         unlink_anon_vmas(new_vma);
528  out_free_mempol:
529 diff --git a/mm/nommu.c b/mm/nommu.c
530 index c072a660ec2c..62e67267f79d 100644
531 --- a/mm/nommu.c
532 +++ b/mm/nommu.c
533 @@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
534                 up_write(&nommu_region_sem);
535  
536                 if (region->vm_file)
537 -                       fput(region->vm_file);
538 +                       vmr_fput(region);
539  
540                 /* IO memory and memory shared directly out of the pagecache
541                  * from ramfs/tmpfs mustn't be released here */
542 @@ -602,7 +602,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
543         if (vma->vm_ops && vma->vm_ops->close)
544                 vma->vm_ops->close(vma);
545         if (vma->vm_file)
546 -               fput(vma->vm_file);
547 +               vma_fput(vma);
548         put_nommu_region(vma->vm_region);
549         vm_area_free(vma);
550  }
551 @@ -1137,7 +1137,7 @@ unsigned long do_mmap(struct file *file,
552                                         goto error_just_free;
553                                 }
554                         }
555 -                       fput(region->vm_file);
556 +                       vmr_fput(region);
557                         kmem_cache_free(vm_region_jar, region);
558                         region = pregion;
559                         result = start;
560 @@ -1219,10 +1219,10 @@ unsigned long do_mmap(struct file *file,
561  error:
562         vma_iter_free(&vmi);
563         if (region->vm_file)
564 -               fput(region->vm_file);
565 +               vmr_fput(region);
566         kmem_cache_free(vm_region_jar, region);
567         if (vma->vm_file)
568 -               fput(vma->vm_file);
569 +               vma_fput(vma);
570         vm_area_free(vma);
571         return ret;
572  
573 diff --git a/mm/prfile.c b/mm/prfile.c
574 new file mode 100644
575 index 000000000000..8f820a235364
576 --- /dev/null
577 +++ b/mm/prfile.c
578 @@ -0,0 +1,86 @@
579 +// SPDX-License-Identifier: GPL-2.0
580 +/*
581 + * Mainly for aufs which mmap(2) different file and wants to print different
582 + * path in /proc/PID/maps.
583 + * Call these functions via macros defined in linux/mm.h.
584 + *
585 + * See Documentation/filesystems/aufs/design/06mmap.txt
586 + *
587 + * Copyright (c) 2014-2022 Junjro R. Okajima
588 + * Copyright (c) 2014 Ian Campbell
589 + */
590 +
591 +#include <linux/mm.h>
592 +#include <linux/file.h>
593 +#include <linux/fs.h>
594 +
595 +/* #define PRFILE_TRACE */
596 +static inline void prfile_trace(struct file *f, struct file *pr,
597 +                             const char func[], int line, const char func2[])
598 +{
599 +#ifdef PRFILE_TRACE
600 +       if (pr)
601 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
602 +#endif
603 +}
604 +
605 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
606 +                            int line)
607 +{
608 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
609 +
610 +       prfile_trace(f, pr, func, line, __func__);
611 +       file_update_time(f);
612 +       if (f && pr)
613 +               file_update_time(pr);
614 +}
615 +
616 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
617 +                              int line)
618 +{
619 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
620 +
621 +       prfile_trace(f, pr, func, line, __func__);
622 +       return (f && pr) ? pr : f;
623 +}
624 +
625 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
626 +{
627 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
628 +
629 +       prfile_trace(f, pr, func, line, __func__);
630 +       get_file(f);
631 +       if (f && pr)
632 +               get_file(pr);
633 +}
634 +
635 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
636 +{
637 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
638 +
639 +       prfile_trace(f, pr, func, line, __func__);
640 +       fput(f);
641 +       if (f && pr)
642 +               fput(pr);
643 +}
644 +
645 +#ifndef CONFIG_MMU
646 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
647 +                              int line)
648 +{
649 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
650 +
651 +       prfile_trace(f, pr, func, line, __func__);
652 +       return (f && pr) ? pr : f;
653 +}
654 +
655 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
656 +{
657 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
658 +
659 +       prfile_trace(f, pr, func, line, __func__);
660 +       fput(f);
661 +       if (f && pr)
662 +               fput(pr);
663 +}
664 +#endif /* !CONFIG_MMU */
665 SPDX-License-Identifier: GPL-2.0
666 aufs6.5 standalone patch
667
668 diff --git a/fs/dcache.c b/fs/dcache.c
669 index 519321f32f95..267e0c65914a 100644
670 --- a/fs/dcache.c
671 +++ b/fs/dcache.c
672 @@ -1450,6 +1450,7 @@ void d_walk(struct dentry *parent, void *data,
673         seq = 1;
674         goto again;
675  }
676 +EXPORT_SYMBOL_GPL(d_walk);
677  
678  struct check_mount {
679         struct vfsmount *mnt;
680 @@ -3052,6 +3053,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
681  
682         write_sequnlock(&rename_lock);
683  }
684 +EXPORT_SYMBOL_GPL(d_exchange);
685  
686  /**
687   * d_ancestor - search for an ancestor
688 diff --git a/fs/exec.c b/fs/exec.c
689 index 1a827d55ba94..d7b12f1e7af2 100644
690 --- a/fs/exec.c
691 +++ b/fs/exec.c
692 @@ -112,6 +112,7 @@ bool path_noexec(const struct path *path)
693         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
694                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
695  }
696 +EXPORT_SYMBOL_GPL(path_noexec);
697  
698  #ifdef CONFIG_USELIB
699  /*
700 diff --git a/fs/fcntl.c b/fs/fcntl.c
701 index 9ea58b7bb580..99fef189bcd6 100644
702 --- a/fs/fcntl.c
703 +++ b/fs/fcntl.c
704 @@ -87,6 +87,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
705   out:
706         return error;
707  }
708 +EXPORT_SYMBOL_GPL(setfl);
709  
710  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
711                       int force)
712 diff --git a/fs/file_table.c b/fs/file_table.c
713 index fc7d677ff5ad..a2fbedb58c0a 100644
714 --- a/fs/file_table.c
715 +++ b/fs/file_table.c
716 @@ -225,6 +225,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
717         }
718         return ERR_PTR(-ENFILE);
719  }
720 +EXPORT_SYMBOL_GPL(alloc_empty_file);
721  
722  /*
723   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
724 diff --git a/fs/namespace.c b/fs/namespace.c
725 index 6c57487f126b..16be9ac1c734 100644
726 --- a/fs/namespace.c
727 +++ b/fs/namespace.c
728 @@ -466,6 +466,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
729         mnt_dec_writers(real_mount(mnt));
730         preempt_enable();
731  }
732 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
733  
734  /**
735   * mnt_drop_write - give up write access to a mount
736 @@ -877,6 +878,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
737  {
738         return check_mnt(real_mount(mnt));
739  }
740 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
741  
742  /*
743   * vfsmount lock must be held for write
744 @@ -2152,6 +2154,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
745         }
746         return 0;
747  }
748 +EXPORT_SYMBOL_GPL(iterate_mounts);
749  
750  static void lock_mnt_tree(struct mount *mnt)
751  {
752 diff --git a/fs/notify/group.c b/fs/notify/group.c
753 index 1de6631a3925..3008eb37a18d 100644
754 --- a/fs/notify/group.c
755 +++ b/fs/notify/group.c
756 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
757  {
758         refcount_inc(&group->refcnt);
759  }
760 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
761  
762  /*
763   * Drop a reference to a group.  Free it if it's through.
764 diff --git a/fs/open.c b/fs/open.c
765 index e6ead0f19964..1f4deda436ec 100644
766 --- a/fs/open.c
767 +++ b/fs/open.c
768 @@ -67,6 +67,7 @@ int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
769         inode_unlock(dentry->d_inode);
770         return ret;
771  }
772 +EXPORT_SYMBOL_GPL(do_truncate);
773  
774  long vfs_truncate(const struct path *path, loff_t length)
775  {
776 diff --git a/fs/read_write.c b/fs/read_write.c
777 index b07de77ef126..e07fadb4afe2 100644
778 --- a/fs/read_write.c
779 +++ b/fs/read_write.c
780 @@ -477,6 +477,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
781         inc_syscr(current);
782         return ret;
783  }
784 +EXPORT_SYMBOL_GPL(vfs_read);
785  
786  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
787  {
788 @@ -592,6 +593,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
789         file_end_write(file);
790         return ret;
791  }
792 +EXPORT_SYMBOL_GPL(vfs_write);
793  
794  /* file_ppos returns &file->f_pos or NULL if file is stream */
795  static inline loff_t *file_ppos(struct file *file)
796 diff --git a/fs/splice.c b/fs/splice.c
797 index a724fe9ccb80..ebb2c4ceb598 100644
798 --- a/fs/splice.c
799 +++ b/fs/splice.c
800 @@ -935,6 +935,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
801                 return warn_unsupported(out, "write");
802         return out->f_op->splice_write(pipe, out, ppos, len, flags);
803  }
804 +EXPORT_SYMBOL_GPL(do_splice_from);
805  
806  /*
807   * Indicate to the caller that there was a premature EOF when reading from the
808 diff --git a/fs/xattr.c b/fs/xattr.c
809 index e7bbb7f57557..edb0190da4d5 100644
810 --- a/fs/xattr.c
811 +++ b/fs/xattr.c
812 @@ -406,6 +406,7 @@ vfs_getxattr_alloc(struct mnt_idmap *idmap, struct dentry *dentry,
813         *xattr_value = value;
814         return error;
815  }
816 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
817  
818  ssize_t
819  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
820 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
821 index 22af30abc781..eaf4e02bcb4d 100644
822 --- a/kernel/locking/lockdep.c
823 +++ b/kernel/locking/lockdep.c
824 @@ -239,6 +239,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
825          */
826         return lock_classes + class_idx;
827  }
828 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
829  #define hlock_class(hlock) lockdep_hlock_class(hlock)
830  
831  #ifdef CONFIG_LOCK_STAT
832 diff --git a/kernel/task_work.c b/kernel/task_work.c
833 index 065e1ef8fc8d..c623c6f0c645 100644
834 --- a/kernel/task_work.c
835 +++ b/kernel/task_work.c
836 @@ -182,3 +182,4 @@ void task_work_run(void)
837                 } while (work);
838         }
839  }
840 +EXPORT_SYMBOL_GPL(task_work_run);
841 diff --git a/security/security.c b/security/security.c
842 index b720424ca37d..635ee0582e05 100644
843 --- a/security/security.c
844 +++ b/security/security.c
845 @@ -1711,6 +1711,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
846                 return 0;
847         return call_int_hook(path_rmdir, 0, dir, dentry);
848  }
849 +EXPORT_SYMBOL_GPL(security_path_rmdir);
850  
851  /**
852   * security_path_unlink() - Check if removing a hard link is allowed
853 @@ -1746,6 +1747,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
854                 return 0;
855         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
856  }
857 +EXPORT_SYMBOL_GPL(security_path_symlink);
858  
859  /**
860   * security_path_link - Check if creating a hard link is allowed
861 @@ -1764,6 +1766,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
862                 return 0;
863         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
864  }
865 +EXPORT_SYMBOL_GPL(security_path_link);
866  
867  /**
868   * security_path_rename() - Check if renaming a file is allowed
869 @@ -1825,6 +1828,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
870                 return 0;
871         return call_int_hook(path_chmod, 0, path, mode);
872  }
873 +EXPORT_SYMBOL_GPL(security_path_chmod);
874  
875  /**
876   * security_path_chown() - Check if changing the file's owner/group is allowed
877 @@ -1842,6 +1846,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
878                 return 0;
879         return call_int_hook(path_chown, 0, path, uid, gid);
880  }
881 +EXPORT_SYMBOL_GPL(security_path_chown);
882  
883  /**
884   * security_path_chroot() - Check if changing the root directory is allowed
885 @@ -2071,6 +2076,7 @@ int security_inode_permission(struct inode *inode, int mask)
886                 return 0;
887         return call_int_hook(inode_permission, 0, inode, mask);
888  }
889 +EXPORT_SYMBOL_GPL(security_inode_permission);
890  
891  /**
892   * security_inode_setattr() - Check if setting file attributes is allowed
893 @@ -2549,6 +2555,7 @@ int security_file_permission(struct file *file, int mask)
894  
895         return fsnotify_perm(file, mask);
896  }
897 +EXPORT_SYMBOL_GPL(security_file_permission);
898  
899  /**
900   * security_file_alloc() - Allocate and init a file's LSM blob
901 @@ -2815,6 +2822,7 @@ int security_file_truncate(struct file *file)
902  {
903         return call_int_hook(file_truncate, 0, file);
904  }
905 +EXPORT_SYMBOL_GPL(security_file_truncate);
906  
907  /**
908   * security_task_alloc() - Allocate a task's LSM blob
909 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
910 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
911 +++ linux/Documentation/ABI/testing/debugfs-aufs        2022-11-05 23:02:18.955889283 +0100
912 @@ -0,0 +1,55 @@
913 +What:          /debug/aufs/si_<id>/
914 +Date:          March 2009
915 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
916 +Description:
917 +               Under /debug/aufs, a directory named si_<id> is created
918 +               per aufs mount, where <id> is a unique id generated
919 +               internally.
920 +
921 +What:          /debug/aufs/si_<id>/plink
922 +Date:          Apr 2013
923 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
924 +Description:
925 +               It has three lines and shows the information about the
926 +               pseudo-link. The first line is a single number
927 +               representing a number of buckets. The second line is a
928 +               number of pseudo-links per buckets (separated by a
929 +               blank). The last line is a single number representing a
930 +               total number of psedo-links.
931 +               When the aufs mount option 'noplink' is specified, it
932 +               will show "1\n0\n0\n".
933 +
934 +What:          /debug/aufs/si_<id>/xib
935 +Date:          March 2009
936 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
937 +Description:
938 +               It shows the consumed blocks by xib (External Inode Number
939 +               Bitmap), its block size and file size.
940 +               When the aufs mount option 'noxino' is specified, it
941 +               will be empty. About XINO files, see the aufs manual.
942 +
943 +What:          /debug/aufs/si_<id>/xi<branch-index>
944 +Date:          March 2009
945 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
946 +Description:
947 +               It shows the consumed blocks by xino (External Inode Number
948 +               Translation Table), its link count, block size and file
949 +               size.
950 +               Due to the file size limit, there may exist multiple
951 +               xino files per branch.  In this case, "-N" is added to
952 +               the filename and it corresponds to the index of the
953 +               internal xino array.  "-0" is omitted.
954 +               When the aufs mount option 'noxino' is specified, Those
955 +               entries won't exist.  About XINO files, see the aufs
956 +               manual.
957 +
958 +What:          /debug/aufs/si_<id>/xigen
959 +Date:          March 2009
960 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
961 +Description:
962 +               It shows the consumed blocks by xigen (External Inode
963 +               Generation Table), its block size and file size.
964 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
965 +               be created.
966 +               When the aufs mount option 'noxino' is specified, it
967 +               will be empty. About XINO files, see the aufs manual.
968 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
969 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
970 +++ linux/Documentation/ABI/testing/sysfs-aufs  2022-11-05 23:02:18.955889283 +0100
971 @@ -0,0 +1,31 @@
972 +What:          /sys/fs/aufs/si_<id>/
973 +Date:          March 2009
974 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
975 +Description:
976 +               Under /sys/fs/aufs, a directory named si_<id> is created
977 +               per aufs mount, where <id> is a unique id generated
978 +               internally.
979 +
980 +What:          /sys/fs/aufs/si_<id>/br<idx>
981 +Date:          March 2009
982 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
983 +Description:
984 +               It shows the abolute path of a member directory (which
985 +               is called branch) in aufs, and its permission.
986 +
987 +What:          /sys/fs/aufs/si_<id>/brid<idx>
988 +Date:          July 2013
989 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
990 +Description:
991 +               It shows the id of a member directory (which is called
992 +               branch) in aufs.
993 +
994 +What:          /sys/fs/aufs/si_<id>/xi_path
995 +Date:          March 2009
996 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
997 +Description:
998 +               It shows the abolute path of XINO (External Inode Number
999 +               Bitmap, Translation Table and Generation Table) file
1000 +               even if it is the default path.
1001 +               When the aufs mount option 'noxino' is specified, it
1002 +               will be empty. About XINO files, see the aufs manual.
1003 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1004 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1005 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2022-11-05 23:02:18.955889283 +0100
1006 @@ -0,0 +1,171 @@
1007 +
1008 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1009 +#
1010 +# This program is free software; you can redistribute it and/or modify
1011 +# it under the terms of the GNU General Public License as published by
1012 +# the Free Software Foundation; either version 2 of the License, or
1013 +# (at your option) any later version.
1014 +#
1015 +# This program is distributed in the hope that it will be useful,
1016 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1017 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1018 +# GNU General Public License for more details.
1019 +#
1020 +# You should have received a copy of the GNU General Public License
1021 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1022 +
1023 +Introduction
1024 +----------------------------------------
1025 +
1026 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1027 +1. abbrev. for "advanced multi-layered unification filesystem".
1028 +2. abbrev. for "another unionfs".
1029 +3. abbrev. for "auf das" in German which means "on the" in English.
1030 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1031 +   But "Filesystem aufs Filesystem" is hard to understand.
1032 +4. abbrev. for "African Urban Fashion Show".
1033 +
1034 +AUFS is a filesystem with features:
1035 +- multi layered stackable unification filesystem, the member directory
1036 +  is called as a branch.
1037 +- branch permission and attribute, 'readonly', 'real-readonly',
1038 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1039 +  combination.
1040 +- internal "file copy-on-write".
1041 +- logical deletion, whiteout.
1042 +- dynamic branch manipulation, adding, deleting and changing permission.
1043 +- allow bypassing aufs, user's direct branch access.
1044 +- external inode number translation table and bitmap which maintains the
1045 +  persistent aufs inode number.
1046 +- seekable directory, including NFS readdir.
1047 +- file mapping, mmap and sharing pages.
1048 +- pseudo-link, hardlink over branches.
1049 +- loopback mounted filesystem as a branch.
1050 +- several policies to select one among multiple writable branches.
1051 +- revert a single systemcall when an error occurs in aufs.
1052 +- and more...
1053 +
1054 +
1055 +Multi Layered Stackable Unification Filesystem
1056 +----------------------------------------------------------------------
1057 +Most people already knows what it is.
1058 +It is a filesystem which unifies several directories and provides a
1059 +merged single directory. When users access a file, the access will be
1060 +passed/re-directed/converted (sorry, I am not sure which English word is
1061 +correct) to the real file on the member filesystem. The member
1062 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1063 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1064 +readonly branch is handled by creating 'whiteout' on the upper writable
1065 +branch.
1066 +
1067 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1068 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1069 +different approaches to implement the merged-view.
1070 +The former tries putting it into VFS, and the latter implements as a
1071 +separate filesystem.
1072 +(If I misunderstand about these implementations, please let me know and
1073 +I shall correct it. Because it is a long time ago when I read their
1074 +source files last time).
1075 +
1076 +UnionMount's approach will be able to small, but may be hard to share
1077 +branches between several UnionMount since the whiteout in it is
1078 +implemented in the inode on branch filesystem and always
1079 +shared. According to Bharata's post, readdir does not seems to be
1080 +finished yet.
1081 +There are several missing features known in this implementations such as
1082 +- for users, the inode number may change silently. eg. copy-up.
1083 +- link(2) may break by copy-up.
1084 +- read(2) may get an obsoleted filedata (fstat(2) too).
1085 +- fcntl(F_SETLK) may be broken by copy-up.
1086 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1087 +  open(O_RDWR).
1088 +
1089 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1090 +merged into mainline. This is another implementation of UnionMount as a
1091 +separated filesystem. All the limitations and known problems which
1092 +UnionMount are equally inherited to "overlay" filesystem.
1093 +
1094 +Unionfs has a longer history. When I started implementing a stackable
1095 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1096 +inode, dentry and file objects and they have an array pointing lower
1097 +same kind objects. After contributing many patches for Unionfs, I
1098 +re-started my project AUFS (Jun 2006).
1099 +
1100 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1101 +implemented my own ideas, approaches and enhancements and it became
1102 +totally different one.
1103 +
1104 +Comparing DM snapshot and fs based implementation
1105 +- the number of bytes to be copied between devices is much smaller.
1106 +- the type of filesystem must be one and only.
1107 +- the fs must be writable, no readonly fs, even for the lower original
1108 +  device. so the compression fs will not be usable. but if we use
1109 +  loopback mount, we may address this issue.
1110 +  for instance,
1111 +       mount /cdrom/squashfs.img /sq
1112 +       losetup /sq/ext2.img
1113 +       losetup /somewhere/cow
1114 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1115 +- it will be difficult (or needs more operations) to extract the
1116 +  difference between the original device and COW.
1117 +- DM snapshot-merge may help a lot when users try merging. in the
1118 +  fs-layer union, users will use rsync(1).
1119 +
1120 +You may want to read my old paper "Filesystems in LiveCD"
1121 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1122 +
1123 +
1124 +Several characters/aspects/persona of aufs
1125 +----------------------------------------------------------------------
1126 +
1127 +Aufs has several characters, aspects or persona.
1128 +1. a filesystem, callee of VFS helper
1129 +2. sub-VFS, caller of VFS helper for branches
1130 +3. a virtual filesystem which maintains persistent inode number
1131 +4. reader/writer of files on branches such like an application
1132 +
1133 +1. Callee of VFS Helper
1134 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1135 +unlink(2) from an application reaches sys_unlink() kernel function and
1136 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1137 +calls filesystem specific unlink operation. Actually aufs implements the
1138 +unlink operation but it behaves like a redirector.
1139 +
1140 +2. Caller of VFS Helper for Branches
1141 +aufs_unlink() passes the unlink request to the branch filesystem as if
1142 +it were called from VFS. So the called unlink operation of the branch
1143 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1144 +every necessary pre/post operation for the branch filesystem.
1145 +- acquire the lock for the parent dir on a branch
1146 +- lookup in a branch
1147 +- revalidate dentry on a branch
1148 +- mnt_want_write() for a branch
1149 +- vfs_unlink() for a branch
1150 +- mnt_drop_write() for a branch
1151 +- release the lock on a branch
1152 +
1153 +3. Persistent Inode Number
1154 +One of the most important issue for a filesystem is to maintain inode
1155 +numbers. This is particularly important to support exporting a
1156 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1157 +backend block device for its own. But some storage is necessary to
1158 +keep and maintain the inode numbers. It may be a large space and may not
1159 +suit to keep in memory. Aufs rents some space from its first writable
1160 +branch filesystem (by default) and creates file(s) on it. These files
1161 +are created by aufs internally and removed soon (currently) keeping
1162 +opened.
1163 +Note: Because these files are removed, they are totally gone after
1164 +      unmounting aufs. It means the inode numbers are not persistent
1165 +      across unmount or reboot. I have a plan to make them really
1166 +      persistent which will be important for aufs on NFS server.
1167 +
1168 +4. Read/Write Files Internally (copy-on-write)
1169 +Because a branch can be readonly, when you write a file on it, aufs will
1170 +"copy-up" it to the upper writable branch internally. And then write the
1171 +originally requested thing to the file. Generally kernel doesn't
1172 +open/read/write file actively. In aufs, even a single write may cause a
1173 +internal "file copy". This behaviour is very similar to cp(1) command.
1174 +
1175 +Some people may think it is better to pass such work to user space
1176 +helper, instead of doing in kernel space. Actually I am still thinking
1177 +about it. But currently I have implemented it in kernel space.
1178 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1179 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1180 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2022-11-05 23:02:18.955889283 +0100
1181 @@ -0,0 +1,258 @@
1182 +
1183 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1184 +#
1185 +# This program is free software; you can redistribute it and/or modify
1186 +# it under the terms of the GNU General Public License as published by
1187 +# the Free Software Foundation; either version 2 of the License, or
1188 +# (at your option) any later version.
1189 +#
1190 +# This program is distributed in the hope that it will be useful,
1191 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1192 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1193 +# GNU General Public License for more details.
1194 +#
1195 +# You should have received a copy of the GNU General Public License
1196 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1197 +
1198 +Basic Aufs Internal Structure
1199 +
1200 +Superblock/Inode/Dentry/File Objects
1201 +----------------------------------------------------------------------
1202 +As like an ordinary filesystem, aufs has its own
1203 +superblock/inode/dentry/file objects. All these objects have a
1204 +dynamically allocated array and store the same kind of pointers to the
1205 +lower filesystem, branch.
1206 +For example, when you build a union with one readwrite branch and one
1207 +readonly, mounted /au, /rw and /ro respectively.
1208 +- /au = /rw + /ro
1209 +- /ro/fileA exists but /rw/fileA
1210 +
1211 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1212 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1213 +- [0] = NULL (because /rw/fileA doesn't exist)
1214 +- [1] = /ro/fileA
1215 +
1216 +This style of an array is essentially same to the aufs
1217 +superblock/inode/dentry/file objects.
1218 +
1219 +Because aufs supports manipulating branches, ie. add/delete/change
1220 +branches dynamically, these objects has its own generation. When
1221 +branches are changed, the generation in aufs superblock is
1222 +incremented. And a generation in other object are compared when it is
1223 +accessed. When a generation in other objects are obsoleted, aufs
1224 +refreshes the internal array.
1225 +
1226 +
1227 +Superblock
1228 +----------------------------------------------------------------------
1229 +Additionally aufs superblock has some data for policies to select one
1230 +among multiple writable branches, XIB files, pseudo-links and kobject.
1231 +See below in detail.
1232 +About the policies which supports copy-down a directory, see
1233 +wbr_policy.txt too.
1234 +
1235 +
1236 +Branch and XINO(External Inode Number Translation Table)
1237 +----------------------------------------------------------------------
1238 +Every branch has its own xino (external inode number translation table)
1239 +file. The xino file is created and unlinked by aufs internally. When two
1240 +members of a union exist on the same filesystem, they share the single
1241 +xino file.
1242 +The struct of a xino file is simple, just a sequence of aufs inode
1243 +numbers which is indexed by the lower inode number.
1244 +In the above sample, assume the inode number of /ro/fileA is i111 and
1245 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1246 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1247 +
1248 +When the inode numbers are not contiguous, the xino file will be sparse
1249 +which has a hole in it and doesn't consume as much disk space as it
1250 +might appear. If your branch filesystem consumes disk space for such
1251 +holes, then you should specify 'xino=' option at mounting aufs.
1252 +
1253 +Aufs has a mount option to free the disk blocks for such holes in XINO
1254 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1255 +meet a problem of disk shortage due to XINO files, then you should try
1256 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1257 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1258 +the holes in XINO files.
1259 +
1260 +Also a writable branch has three kinds of "whiteout bases". All these
1261 +are existed when the branch is joined to aufs, and their names are
1262 +whiteout-ed doubly, so that users will never see their names in aufs
1263 +hierarchy.
1264 +1. a regular file which will be hardlinked to all whiteouts.
1265 +2. a directory to store a pseudo-link.
1266 +3. a directory to store an "orphan"-ed file temporary.
1267 +
1268 +1. Whiteout Base
1269 +   When you remove a file on a readonly branch, aufs handles it as a
1270 +   logical deletion and creates a whiteout on the upper writable branch
1271 +   as a hardlink of this file in order not to consume inode on the
1272 +   writable branch.
1273 +2. Pseudo-link Dir
1274 +   See below, Pseudo-link.
1275 +3. Step-Parent Dir
1276 +   When "fileC" exists on the lower readonly branch only and it is
1277 +   opened and removed with its parent dir, and then user writes
1278 +   something into it, then aufs copies-up fileC to this
1279 +   directory. Because there is no other dir to store fileC. After
1280 +   creating a file under this dir, the file is unlinked.
1281 +
1282 +Because aufs supports manipulating branches, ie. add/delete/change
1283 +dynamically, a branch has its own id. When the branch order changes,
1284 +aufs finds the new index by searching the branch id.
1285 +
1286 +
1287 +Pseudo-link
1288 +----------------------------------------------------------------------
1289 +Assume "fileA" exists on the lower readonly branch only and it is
1290 +hardlinked to "fileB" on the branch. When you write something to fileA,
1291 +aufs copies-up it to the upper writable branch. Additionally aufs
1292 +creates a hardlink under the Pseudo-link Directory of the writable
1293 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1294 +simple list. If fileB is read after unlinking fileA, aufs returns
1295 +filedata from the pseudo-link instead of the lower readonly
1296 +branch. Because the pseudo-link is based upon the inode, to keep the
1297 +inode number by xino (see above) is essentially necessary.
1298 +
1299 +All the hardlinks under the Pseudo-link Directory of the writable branch
1300 +should be restored in a proper location later. Aufs provides a utility
1301 +to do this. The userspace helpers executed at remounting and unmounting
1302 +aufs by default.
1303 +During this utility is running, it puts aufs into the pseudo-link
1304 +maintenance mode. In this mode, only the process which began the
1305 +maintenance mode (and its child processes) is allowed to operate in
1306 +aufs. Some other processes which are not related to the pseudo-link will
1307 +be allowed to run too, but the rest have to return an error or wait
1308 +until the maintenance mode ends. If a process already acquires an inode
1309 +mutex (in VFS), it has to return an error.
1310 +
1311 +
1312 +XIB(external inode number bitmap)
1313 +----------------------------------------------------------------------
1314 +Addition to the xino file per a branch, aufs has an external inode number
1315 +bitmap in a superblock object. It is also an internal file such like a
1316 +xino file.
1317 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1318 +not.
1319 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1320 +
1321 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1322 +reduce the number of consumed disk blocks for these files.
1323 +
1324 +
1325 +Virtual or Vertical Dir, and Readdir in Userspace
1326 +----------------------------------------------------------------------
1327 +In order to support multiple layers (branches), aufs readdir operation
1328 +constructs a virtual dir block on memory. For readdir, aufs calls
1329 +vfs_readdir() internally for each dir on branches, merges their entries
1330 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1331 +object. So the file object has its entry list until it is closed. The
1332 +entry list will be updated when the file position is zero and becomes
1333 +obsoleted. This decision is made in aufs automatically.
1334 +
1335 +The dynamically allocated memory block for the name of entries has a
1336 +unit of 512 bytes (by default) and stores the names contiguously (no
1337 +padding). Another block for each entry is handled by kmem_cache too.
1338 +During building dir blocks, aufs creates hash list and judging whether
1339 +the entry is whiteouted by its upper branch or already listed.
1340 +The merged result is cached in the corresponding inode object and
1341 +maintained by a customizable life-time option.
1342 +
1343 +Some people may call it can be a security hole or invite DoS attack
1344 +since the opened and once readdir-ed dir (file object) holds its entry
1345 +list and becomes a pressure for system memory. But I'd say it is similar
1346 +to files under /proc or /sys. The virtual files in them also holds a
1347 +memory page (generally) while they are opened. When an idea to reduce
1348 +memory for them is introduced, it will be applied to aufs too.
1349 +For those who really hate this situation, I've developed readdir(3)
1350 +library which operates this merging in userspace. You just need to set
1351 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1352 +kernel space for readdir(3).
1353 +
1354 +
1355 +Workqueue
1356 +----------------------------------------------------------------------
1357 +Aufs sometimes requires privilege access to a branch. For instance,
1358 +in copy-up/down operation. When a user process is going to make changes
1359 +to a file which exists in the lower readonly branch only, and the mode
1360 +of one of ancestor directories may not be writable by a user
1361 +process. Here aufs copy-up the file with its ancestors and they may
1362 +require privilege to set its owner/group/mode/etc.
1363 +This is a typical case of a application character of aufs (see
1364 +Introduction).
1365 +
1366 +Aufs uses workqueue synchronously for this case. It creates its own
1367 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1368 +passes the request to call mkdir or write (for example), and wait for
1369 +its completion. This approach solves a problem of a signal handler
1370 +simply.
1371 +If aufs didn't adopt the workqueue and changed the privilege of the
1372 +process, then the process may receive the unexpected SIGXFSZ or other
1373 +signals.
1374 +
1375 +Also aufs uses the system global workqueue ("events" kernel thread) too
1376 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1377 +whiteout base and etc. This is unrelated to a privilege.
1378 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1379 +superblock at the beginning, at the same time waits for the completion
1380 +of all queued asynchronous tasks.
1381 +
1382 +
1383 +Whiteout
1384 +----------------------------------------------------------------------
1385 +The whiteout in aufs is very similar to Unionfs's. That is represented
1386 +by its filename. UnionMount takes an approach of a file mode, but I am
1387 +afraid several utilities (find(1) or something) will have to support it.
1388 +
1389 +Basically the whiteout represents "logical deletion" which stops aufs to
1390 +lookup further, but also it represents "dir is opaque" which also stop
1391 +further lookup.
1392 +
1393 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1394 +In order to make several functions in a single systemcall to be
1395 +revertible, aufs adopts an approach to rename a directory to a temporary
1396 +unique whiteouted name.
1397 +For example, in rename(2) dir where the target dir already existed, aufs
1398 +renames the target dir to a temporary unique whiteouted name before the
1399 +actual rename on a branch, and then handles other actions (make it opaque,
1400 +update the attributes, etc). If an error happens in these actions, aufs
1401 +simply renames the whiteouted name back and returns an error. If all are
1402 +succeeded, aufs registers a function to remove the whiteouted unique
1403 +temporary name completely and asynchronously to the system global
1404 +workqueue.
1405 +
1406 +
1407 +Copy-up
1408 +----------------------------------------------------------------------
1409 +It is a well-known feature or concept.
1410 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1411 +internally and makes change to the new file on the upper writable branch.
1412 +When the trigger systemcall does not update the timestamps of the parent
1413 +dir, aufs reverts it after copy-up.
1414 +
1415 +
1416 +Move-down (aufs3.9 and later)
1417 +----------------------------------------------------------------------
1418 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1419 +the lower readonly branch to the upper writable branch when a user
1420 +changes something about the file.
1421 +"Move-down" is an opposite action of copy-up. Basically this action is
1422 +ran manually instead of automatically and internally.
1423 +For desgin and implementation, aufs has to consider these issues.
1424 +- whiteout for the file may exist on the lower branch.
1425 +- ancestor directories may not exist on the lower branch.
1426 +- diropq for the ancestor directories may exist on the upper branch.
1427 +- free space on the lower branch will reduce.
1428 +- another access to the file may happen during moving-down, including
1429 +  UDBA (see "Revalidate Dentry and UDBA").
1430 +- the file should not be hard-linked nor pseudo-linked. they should be
1431 +  handled by auplink utility later.
1432 +
1433 +Sometimes users want to move-down a file from the upper writable branch
1434 +to the lower readonly or writable branch. For instance,
1435 +- the free space of the upper writable branch is going to run out.
1436 +- create a new intermediate branch between the upper and lower branch.
1437 +- etc.
1438 +
1439 +For this purpose, use "aumvdown" command in aufs-util.git.
1440 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1441 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1442 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2022-11-05 23:02:18.955889283 +0100
1443 @@ -0,0 +1,85 @@
1444 +
1445 +# Copyright (C) 2015-2022 Junjiro R. Okajima
1446 +#
1447 +# This program is free software; you can redistribute it and/or modify
1448 +# it under the terms of the GNU General Public License as published by
1449 +# the Free Software Foundation; either version 2 of the License, or
1450 +# (at your option) any later version.
1451 +#
1452 +# This program is distributed in the hope that it will be useful,
1453 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1454 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1455 +# GNU General Public License for more details.
1456 +#
1457 +# You should have received a copy of the GNU General Public License
1458 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1459 +
1460 +Support for a branch who has its ->atomic_open()
1461 +----------------------------------------------------------------------
1462 +The filesystems who implement its ->atomic_open() are not majority. For
1463 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1464 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1465 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1466 +sure whether all filesystems who have ->atomic_open() behave like this,
1467 +but NFSv4 surely returns the error.
1468 +
1469 +In order to support ->atomic_open() for aufs, there are a few
1470 +approaches.
1471 +
1472 +A. Introduce aufs_atomic_open()
1473 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1474 +     branch fs.
1475 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1476 +   an aufs user Pip Cet's approach
1477 +   - calls aufs_create(), VFS finish_open() and notify_change().
1478 +   - pass fake-mode to finish_open(), and then correct the mode by
1479 +     notify_change().
1480 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1481 +   - no aufs_atomic_open().
1482 +   - aufs_lookup() registers the TID to an aufs internal object.
1483 +   - aufs_create() does nothing when the matching TID is registered, but
1484 +     registers the mode.
1485 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1486 +     TID is registered.
1487 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1488 +   credential
1489 +   - no aufs_atomic_open().
1490 +   - aufs_create() registers the TID to an internal object. this info
1491 +     represents "this process created this file just now."
1492 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1493 +     registered TID and re-try open() with superuser's credential.
1494 +
1495 +Pros and cons for each approach.
1496 +
1497 +A.
1498 +   - straightforward but highly depends upon VFS internal.
1499 +   - the atomic behavaiour is kept.
1500 +   - some of parameters such as nameidata are hard to reproduce for
1501 +     branch fs.
1502 +   - large overhead.
1503 +B.
1504 +   - easy to implement.
1505 +   - the atomic behavaiour is lost.
1506 +C.
1507 +   - the atomic behavaiour is kept.
1508 +   - dirty and tricky.
1509 +   - VFS checks whether the file is created correctly after calling
1510 +     ->create(), which means this approach doesn't work.
1511 +D.
1512 +   - easy to implement.
1513 +   - the atomic behavaiour is lost.
1514 +   - to open a file with superuser's credential and give it to a user
1515 +     process is a bad idea, since the file object keeps the credential
1516 +     in it. It may affect LSM or something. This approach doesn't work
1517 +     either.
1518 +
1519 +The approach A is ideal, but it hard to implement. So here is a
1520 +variation of A, which is to be implemented.
1521 +
1522 +A-1. Introduce aufs_atomic_open()
1523 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1524 +       vfs_create() and finish_open().
1525 +     - the demerit is that the several checks after branch fs
1526 +       ->atomic_open() are lost. in the ordinary case, the checks are
1527 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1528 +       be implemented in aufs, but not all I am afraid.
1529 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1530 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1531 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2022-11-05 23:02:18.959222617 +0100
1532 @@ -0,0 +1,113 @@
1533 +
1534 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1535 +#
1536 +# This program is free software; you can redistribute it and/or modify
1537 +# it under the terms of the GNU General Public License as published by
1538 +# the Free Software Foundation; either version 2 of the License, or
1539 +# (at your option) any later version.
1540 +#
1541 +# This program is distributed in the hope that it will be useful,
1542 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1543 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1544 +# GNU General Public License for more details.
1545 +#
1546 +# You should have received a copy of the GNU General Public License
1547 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1548 +
1549 +Lookup in a Branch
1550 +----------------------------------------------------------------------
1551 +Since aufs has a character of sub-VFS (see Introduction), it operates
1552 +lookup for branches as VFS does. It may be a heavy work. But almost all
1553 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1554 +directly connected to its parent. Digging down the directory hierarchy
1555 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1556 +aufs calls it.
1557 +
1558 +When a branch is a remote filesystem, aufs basically relies upon its
1559 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1560 +them.
1561 +For d_revalidate, aufs implements three levels of revalidate tests. See
1562 +"Revalidate Dentry and UDBA" in detail.
1563 +
1564 +
1565 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1566 +----------------------------------------------------------------------
1567 +Let's try case study.
1568 +- aufs has two branches, upper readwrite and lower readonly.
1569 +  /au = /rw + /ro
1570 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1571 +- user invoked "chmod a+rx /au/dirA"
1572 +- the internal copy-up is activated and "/rw/dirA" is created and its
1573 +  permission bits are set to world readable.
1574 +- then "/au/dirA" becomes world readable?
1575 +
1576 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1577 +or it may be a natively readonly filesystem. If aufs respects the lower
1578 +branch, it should not respond readdir request from other users. But user
1579 +allowed it by chmod. Should really aufs rejects showing the entries
1580 +under /ro/dirA?
1581 +
1582 +To be honest, I don't have a good solution for this case. So aufs
1583 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1584 +users.
1585 +When dirperm1 is specified, aufs checks only the highest one for the
1586 +directory permission, and shows the entries. Otherwise, as usual, checks
1587 +every dir existing on all branches and rejects the request.
1588 +
1589 +As a side effect, dirperm1 option improves the performance of aufs
1590 +because the number of permission check is reduced when the number of
1591 +branch is many.
1592 +
1593 +
1594 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1595 +----------------------------------------------------------------------
1596 +Generally VFS helpers re-validate a dentry as a part of lookup.
1597 +0. digging down the directory hierarchy.
1598 +1. lock the parent dir by its i_mutex.
1599 +2. lookup the final (child) entry.
1600 +3. revalidate it.
1601 +4. call the actual operation (create, unlink, etc.)
1602 +5. unlock the parent dir
1603 +
1604 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1605 +called. Actually aufs implements it and checks the dentry on a branch is
1606 +still valid.
1607 +But it is not enough. Because aufs has to release the lock for the
1608 +parent dir on a branch at the end of ->lookup() (step 2) and
1609 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1610 +held by VFS.
1611 +If the file on a branch is changed directly, eg. bypassing aufs, after
1612 +aufs released the lock, then the subsequent operation may cause
1613 +something unpleasant result.
1614 +
1615 +This situation is a result of VFS architecture, ->lookup() and
1616 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1617 +design from VFS's point of view. It is just not suitable for sub-VFS
1618 +character in aufs.
1619 +
1620 +Aufs supports such case by three level of revalidation which is
1621 +selectable by user.
1622 +1. Simple Revalidate
1623 +   Addition to the native flow in VFS's, confirm the child-parent
1624 +   relationship on the branch just after locking the parent dir on the
1625 +   branch in the "actual operation" (step 4). When this validation
1626 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1627 +   checks the validation of the dentry on branches.
1628 +2. Monitor Changes Internally by Inotify/Fsnotify
1629 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1630 +   the dentry on the branch, and returns EBUSY if it finds different
1631 +   dentry.
1632 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1633 +   during it is in cache. When the event is notified, aufs registers a
1634 +   function to kernel 'events' thread by schedule_work(). And the
1635 +   function sets some special status to the cached aufs dentry and inode
1636 +   private data. If they are not cached, then aufs has nothing to
1637 +   do. When the same file is accessed through aufs (step 0-3) later,
1638 +   aufs will detect the status and refresh all necessary data.
1639 +   In this mode, aufs has to ignore the event which is fired by aufs
1640 +   itself.
1641 +3. No Extra Validation
1642 +   This is the simplest test and doesn't add any additional revalidation
1643 +   test, and skip the revalidation in step 4. It is useful and improves
1644 +   aufs performance when system surely hide the aufs branches from user,
1645 +   by over-mounting something (or another method).
1646 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1647 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1648 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2022-11-05 23:02:18.959222617 +0100
1649 @@ -0,0 +1,74 @@
1650 +
1651 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1652 +#
1653 +# This program is free software; you can redistribute it and/or modify
1654 +# it under the terms of the GNU General Public License as published by
1655 +# the Free Software Foundation; either version 2 of the License, or
1656 +# (at your option) any later version.
1657 +#
1658 +# This program is distributed in the hope that it will be useful,
1659 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1660 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1661 +# GNU General Public License for more details.
1662 +#
1663 +# You should have received a copy of the GNU General Public License
1664 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1665 +
1666 +Branch Manipulation
1667 +
1668 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1669 +and changing its permission/attribute, there are a lot of works to do.
1670 +
1671 +
1672 +Add a Branch
1673 +----------------------------------------------------------------------
1674 +o Confirm the adding dir exists outside of aufs, including loopback
1675 +  mount, and its various attributes.
1676 +o Initialize the xino file and whiteout bases if necessary.
1677 +  See struct.txt.
1678 +
1679 +o Check the owner/group/mode of the directory
1680 +  When the owner/group/mode of the adding directory differs from the
1681 +  existing branch, aufs issues a warning because it may impose a
1682 +  security risk.
1683 +  For example, when a upper writable branch has a world writable empty
1684 +  top directory, a malicious user can create any files on the writable
1685 +  branch directly, like copy-up and modify manually. If something like
1686 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1687 +  writable branch, and the writable branch is world-writable, then a
1688 +  malicious guy may create /etc/passwd on the writable branch directly
1689 +  and the infected file will be valid in aufs.
1690 +  I am afraid it can be a security issue, but aufs can do nothing except
1691 +  producing a warning.
1692 +
1693 +
1694 +Delete a Branch
1695 +----------------------------------------------------------------------
1696 +o Confirm the deleting branch is not busy
1697 +  To be general, there is one merit to adopt "remount" interface to
1698 +  manipulate branches. It is to discard caches. At deleting a branch,
1699 +  aufs checks the still cached (and connected) dentries and inodes. If
1700 +  there are any, then they are all in-use. An inode without its
1701 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1702 +
1703 +  For the cached one, aufs checks whether the same named entry exists on
1704 +  other branches.
1705 +  If the cached one is a directory, because aufs provides a merged view
1706 +  to users, as long as one dir is left on any branch aufs can show the
1707 +  dir to users. In this case, the branch can be removed from aufs.
1708 +  Otherwise aufs rejects deleting the branch.
1709 +
1710 +  If any file on the deleting branch is opened by aufs, then aufs
1711 +  rejects deleting.
1712 +
1713 +
1714 +Modify the Permission of a Branch
1715 +----------------------------------------------------------------------
1716 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1717 +  See struct.txt.
1718 +
1719 +o rw --> ro: Confirm the modifying branch is not busy
1720 +  Aufs rejects the request if any of these conditions are true.
1721 +  - a file on the branch is mmap-ed.
1722 +  - a regular file on the branch is opened for write and there is no
1723 +    same named entry on the upper branch.
1724 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1725 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1726 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2022-11-05 23:02:18.959222617 +0100
1727 @@ -0,0 +1,64 @@
1728 +
1729 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1730 +#
1731 +# This program is free software; you can redistribute it and/or modify
1732 +# it under the terms of the GNU General Public License as published by
1733 +# the Free Software Foundation; either version 2 of the License, or
1734 +# (at your option) any later version.
1735 +#
1736 +# This program is distributed in the hope that it will be useful,
1737 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1738 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1739 +# GNU General Public License for more details.
1740 +#
1741 +# You should have received a copy of the GNU General Public License
1742 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1743 +
1744 +Policies to Select One among Multiple Writable Branches
1745 +----------------------------------------------------------------------
1746 +When the number of writable branch is more than one, aufs has to decide
1747 +the target branch for file creation or copy-up. By default, the highest
1748 +writable branch which has the parent (or ancestor) dir of the target
1749 +file is chosen (top-down-parent policy).
1750 +By user's request, aufs implements some other policies to select the
1751 +writable branch, for file creation several policies, round-robin,
1752 +most-free-space, and other policies. For copy-up, top-down-parent,
1753 +bottom-up-parent, bottom-up and others.
1754 +
1755 +As expected, the round-robin policy selects the branch in circular. When
1756 +you have two writable branches and creates 10 new files, 5 files will be
1757 +created for each branch. mkdir(2) systemcall is an exception. When you
1758 +create 10 new directories, all will be created on the same branch.
1759 +And the most-free-space policy selects the one which has most free
1760 +space among the writable branches. The amount of free space will be
1761 +checked by aufs internally, and users can specify its time interval.
1762 +
1763 +The policies for copy-up is more simple,
1764 +top-down-parent is equivalent to the same named on in create policy,
1765 +bottom-up-parent selects the writable branch where the parent dir
1766 +exists and the nearest upper one from the copyup-source,
1767 +bottom-up selects the nearest upper writable branch from the
1768 +copyup-source, regardless the existence of the parent dir.
1769 +
1770 +There are some rules or exceptions to apply these policies.
1771 +- If there is a readonly branch above the policy-selected branch and
1772 +  the parent dir is marked as opaque (a variation of whiteout), or the
1773 +  target (creating) file is whiteout-ed on the upper readonly branch,
1774 +  then the result of the policy is ignored and the target file will be
1775 +  created on the nearest upper writable branch than the readonly branch.
1776 +- If there is a writable branch above the policy-selected branch and
1777 +  the parent dir is marked as opaque or the target file is whiteouted
1778 +  on the branch, then the result of the policy is ignored and the target
1779 +  file will be created on the highest one among the upper writable
1780 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1781 +  it as usual.
1782 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1783 +  They try selecting the branch where the source exists as possible
1784 +  since copyup a large file will take long time. If it can't be,
1785 +  ie. the branch where the source exists is readonly, then they will
1786 +  follow the copyup policy.
1787 +- There is an exception for rename(2) when the target exists.
1788 +  If the rename target exists, aufs compares the index of the branches
1789 +  where the source and the target exists and selects the higher
1790 +  one. If the selected branch is readonly, then aufs follows the
1791 +  copyup policy.
1792 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1793 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1794 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2022-11-05 23:02:18.959222617 +0100
1795 @@ -0,0 +1,44 @@
1796 +
1797 +// to view this graph, run dot(1) command in GRAPHVIZ.
1798 +//
1799 +// This program is free software; you can redistribute it and/or modify
1800 +// it under the terms of the GNU General Public License as published by
1801 +// the Free Software Foundation; either version 2 of the License, or
1802 +// (at your option) any later version.
1803 +//
1804 +// This program is distributed in the hope that it will be useful,
1805 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
1806 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1807 +// GNU General Public License for more details.
1808 +//
1809 +// You should have received a copy of the GNU General Public License
1810 +// along with this program.  If not, see <http://www.gnu.org/licenses/>.
1811 +
1812 +digraph G {
1813 +node [shape=box];
1814 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1815 +
1816 +node [shape=oval];
1817 +
1818 +aufs_rename -> whinfo [label="store/remove"];
1819 +
1820 +node [shape=oval];
1821 +inode_list [label="h_inum list in branch\ncache"];
1822 +
1823 +node [shape=box];
1824 +whinode [label="h_inum list file"];
1825 +
1826 +node [shape=oval];
1827 +brmgmt [label="br_add/del/mod/umount"];
1828 +
1829 +brmgmt -> inode_list [label="create/remove"];
1830 +brmgmt -> whinode [label="load/store"];
1831 +
1832 +inode_list -> whinode [style=dashed,dir=both];
1833 +
1834 +aufs_rename -> inode_list [label="add/del"];
1835 +
1836 +aufs_lookup -> inode_list [label="search"];
1837 +
1838 +aufs_lookup -> whinfo [label="load/remove"];
1839 +}
1840 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1841 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1842 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2022-11-05 23:02:18.959222617 +0100
1843 @@ -0,0 +1,102 @@
1844 +
1845 +# Copyright (C) 2017-2022 Junjiro R. Okajima
1846 +#
1847 +# This program is free software; you can redistribute it and/or modify
1848 +# it under the terms of the GNU General Public License as published by
1849 +# the Free Software Foundation; either version 2 of the License, or
1850 +# (at your option) any later version.
1851 +#
1852 +# This program is distributed in the hope that it will be useful,
1853 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1854 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1855 +# GNU General Public License for more details.
1856 +#
1857 +# You should have received a copy of the GNU General Public License
1858 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1859 +
1860 +Special handling for renaming a directory (DIRREN)
1861 +----------------------------------------------------------------------
1862 +First, let's assume we have a simple usecase.
1863 +
1864 +- /u = /rw + /ro
1865 +- /rw/dirA exists
1866 +- /ro/dirA and /ro/dirA/file exist too
1867 +- there is no dirB on both branches
1868 +- a user issues rename("dirA", "dirB")
1869 +
1870 +Now, what should aufs behave against this rename(2)?
1871 +There are a few possible cases.
1872 +
1873 +A. returns EROFS.
1874 +   since dirA exists on a readonly branch which cannot be renamed.
1875 +B. returns EXDEV.
1876 +   it is possible to copy-up dirA (only the dir itself), but the child
1877 +   entries ("file" in this case) should not be. it must be a bad
1878 +   approach to copy-up recursively.
1879 +C. returns a success.
1880 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1881 +   is a violation of aufs' policy.
1882 +D. construct an extra information which indicates that /ro/dirA should
1883 +   be handled as the name of dirB.
1884 +   overlayfs has a similar feature called REDIRECT.
1885 +
1886 +Until now, aufs implements the case B only which returns EXDEV, and
1887 +expects the userspace application behaves like mv(1) which tries
1888 +issueing rename(2) recursively.
1889 +
1890 +A new aufs feature called DIRREN is introduced which implements the case
1891 +D. There are several "extra information" added.
1892 +
1893 +1. detailed info per renamed directory
1894 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1895 +2. the inode-number list of directories on a branch
1896 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1897 +
1898 +The filename of "detailed info per directory" represents the lower
1899 +branch, and its format is
1900 +- a type of the branch id
1901 +  one of these.
1902 +  + uuid (not implemented yet)
1903 +  + fsid
1904 +  + dev
1905 +- the inode-number of the branch root dir
1906 +
1907 +And it contains these info in a single regular file.
1908 +- magic number
1909 +- branch's inode-number of the logically renamed dir
1910 +- the name of the before-renamed dir
1911 +
1912 +The "detailed info per directory" file is created in aufs rename(2), and
1913 +loaded in any lookup.
1914 +The info is considered in lookup for the matching case only. Here
1915 +"matching" means that the root of branch (in the info filename) is same
1916 +to the current looking-up branch. After looking-up the before-renamed
1917 +name, the inode-number is compared. And the matched dentry is used.
1918 +
1919 +The "inode-number list of directories" is a regular file which contains
1920 +simply the inode-numbers on the branch. The file is created or updated
1921 +in removing the branch, and loaded in adding the branch. Its lifetime is
1922 +equal to the branch.
1923 +The list is referred in lookup, and when the current target inode is
1924 +found in the list, the aufs tries loading the "detailed info per
1925 +directory" and get the changed and valid name of the dir.
1926 +
1927 +Theoretically these "extra informaiton" may be able to be put into XATTR
1928 +in the dir inode. But aufs doesn't choose this way because
1929 +1. XATTR may not be supported by the branch (or its configuration)
1930 +2. XATTR may have its size limit.
1931 +3. XATTR may be less easy to convert than a regular file, when the
1932 +   format of the info is changed in the future.
1933 +At the same time, I agree that the regular file approach is much slower
1934 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1935 +better approach.
1936 +
1937 +This DIRREN feature is enabled by aufs configuration, and is activated
1938 +by a new mount option.
1939 +
1940 +For the more complicated case, there is a work with UDBA option, which
1941 +is to dected the direct access to the branches (by-passing aufs) and to
1942 +maintain the cashes in aufs. Since a single cached aufs dentry may
1943 +contains two names, before- and after-rename, the name comparision in
1944 +UDBA handler may not work correctly. In this case, the behaviour will be
1945 +equivalen to udba=reval case.
1946 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1947 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1948 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2022-11-05 23:02:18.959222617 +0100
1949 @@ -0,0 +1,118 @@
1950 +
1951 +# Copyright (C) 2011-2022 Junjiro R. Okajima
1952 +#
1953 +# This program is free software; you can redistribute it and/or modify
1954 +# it under the terms of the GNU General Public License as published by
1955 +# the Free Software Foundation; either version 2 of the License, or
1956 +# (at your option) any later version.
1957 +#
1958 +# This program is distributed in the hope that it will be useful,
1959 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1960 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1961 +# GNU General Public License for more details.
1962 +#
1963 +# You should have received a copy of the GNU General Public License
1964 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1965 +
1966 +File-based Hierarchical Storage Management (FHSM)
1967 +----------------------------------------------------------------------
1968 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1969 +storage world. Aufs provides this feature as file-based with multiple
1970 +writable branches, based upon the principle of "Colder, the Lower".
1971 +Here the word "colder" means that the less used files, and "lower" means
1972 +that the position in the order of the stacked branches vertically.
1973 +These multiple writable branches are prioritized, ie. the topmost one
1974 +should be the fastest drive and be used heavily.
1975 +
1976 +o Characters in aufs FHSM story
1977 +- aufs itself and a new branch attribute.
1978 +- a new ioctl interface to move-down and to establish a connection with
1979 +  the daemon ("move-down" is a converse of "copy-up").
1980 +- userspace tool and daemon.
1981 +
1982 +The userspace daemon establishes a connection with aufs and waits for
1983 +the notification. The notified information is very similar to struct
1984 +statfs containing the number of consumed blocks and inodes.
1985 +When the consumed blocks/inodes of a branch exceeds the user-specified
1986 +upper watermark, the daemon activates its move-down process until the
1987 +consumed blocks/inodes reaches the user-specified lower watermark.
1988 +
1989 +The actual move-down is done by aufs based upon the request from
1990 +user-space since we need to maintain the inode number and the internal
1991 +pointer arrays in aufs.
1992 +
1993 +Currently aufs FHSM handles the regular files only. Additionally they
1994 +must not be hard-linked nor pseudo-linked.
1995 +
1996 +
1997 +o Cowork of aufs and the user-space daemon
1998 +  During the userspace daemon established the connection, aufs sends a
1999 +  small notification to it whenever aufs writes something into the
2000 +  writable branch. But it may cost high since aufs issues statfs(2)
2001 +  internally. So user can specify a new option to cache the
2002 +  info. Actually the notification is controlled by these factors.
2003 +  + the specified cache time.
2004 +  + classified as "force" by aufs internally.
2005 +  Until the specified time expires, aufs doesn't send the info
2006 +  except the forced cases. When aufs decide forcing, the info is always
2007 +  notified to userspace.
2008 +  For example, the number of free inodes is generally large enough and
2009 +  the shortage of it happens rarely. So aufs doesn't force the
2010 +  notification when creating a new file, directory and others. This is
2011 +  the typical case which aufs doesn't force.
2012 +  When aufs writes the actual filedata and the files consumes any of new
2013 +  blocks, the aufs forces notifying.
2014 +
2015 +
2016 +o Interfaces in aufs
2017 +- New branch attribute.
2018 +  + fhsm
2019 +    Specifies that the branch is managed by FHSM feature. In other word,
2020 +    participant in the FHSM.
2021 +    When nofhsm is set to the branch, it will not be the source/target
2022 +    branch of the move-down operation. This attribute is set
2023 +    independently from coo and moo attributes, and if you want full
2024 +    FHSM, you should specify them as well.
2025 +- New mount option.
2026 +  + fhsm_sec
2027 +    Specifies a second to suppress many less important info to be
2028 +    notified.
2029 +- New ioctl.
2030 +  + AUFS_CTL_FHSM_FD
2031 +    create a new file descriptor which userspace can read the notification
2032 +    (a subset of struct statfs) from aufs.
2033 +- Module parameter 'brs'
2034 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2035 +  be set.
2036 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2037 +  When there are two or more branches with fhsm attributes,
2038 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2039 +  terminates it. As a result of remounting and branch-manipulation, the
2040 +  number of branches with fhsm attribute can be one. In this case,
2041 +  /sbin/mount.aufs will terminate the user-space daemon.
2042 +
2043 +
2044 +Finally the operation is done as these steps in kernel-space.
2045 +- make sure that,
2046 +  + no one else is using the file.
2047 +  + the file is not hard-linked.
2048 +  + the file is not pseudo-linked.
2049 +  + the file is a regular file.
2050 +  + the parent dir is not opaqued.
2051 +- find the target writable branch.
2052 +- make sure the file is not whiteout-ed by the upper (than the target)
2053 +  branch.
2054 +- make the parent dir on the target branch.
2055 +- mutex lock the inode on the branch.
2056 +- unlink the whiteout on the target branch (if exists).
2057 +- lookup and create the whiteout-ed temporary name on the target branch.
2058 +- copy the file as the whiteout-ed temporary name on the target branch.
2059 +- rename the whiteout-ed temporary name to the original name.
2060 +- unlink the file on the source branch.
2061 +- maintain the internal pointer array and the external inode number
2062 +  table (XINO).
2063 +- maintain the timestamps and other attributes of the parent dir and the
2064 +  file.
2065 +
2066 +And of course, in every step, an error may happen. So the operation
2067 +should restore the original file state after an error happens.
2068 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2069 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2070 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2022-11-05 23:02:18.959222617 +0100
2071 @@ -0,0 +1,72 @@
2072 +
2073 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2074 +#
2075 +# This program is free software; you can redistribute it and/or modify
2076 +# it under the terms of the GNU General Public License as published by
2077 +# the Free Software Foundation; either version 2 of the License, or
2078 +# (at your option) any later version.
2079 +#
2080 +# This program is distributed in the hope that it will be useful,
2081 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2082 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2083 +# GNU General Public License for more details.
2084 +#
2085 +# You should have received a copy of the GNU General Public License
2086 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2087 +
2088 +mmap(2) -- File Memory Mapping
2089 +----------------------------------------------------------------------
2090 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2091 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2092 +->mmap().
2093 +This approach is simple and good, but there is one problem.
2094 +Under /proc, several entries show the mmapped files by its path (with
2095 +device and inode number), and the printed path will be the path on the
2096 +branch fs's instead of virtual aufs's.
2097 +This is not a problem in most cases, but some utilities lsof(1) (and its
2098 +user) may expect the path on aufs.
2099 +
2100 +To address this issue, aufs adds a new member called vm_prfile in struct
2101 +vm_area_struct (and struct vm_region). The original vm_file points to
2102 +the file on the branch fs in order to handle everything correctly as
2103 +usual. The new vm_prfile points to a virtual file in aufs, and the
2104 +show-functions in procfs refers to vm_prfile if it is set.
2105 +Also we need to maintain several other places where touching vm_file
2106 +such like
2107 +- fork()/clone() copies vma and the reference count of vm_file is
2108 +  incremented.
2109 +- merging vma maintains the ref count too.
2110 +
2111 +This is not a good approach. It just fakes the printed path. But it
2112 +leaves all behaviour around f_mapping unchanged. This is surely an
2113 +advantage.
2114 +Actually aufs had adopted another complicated approach which calls
2115 +generic_file_mmap() and handles struct vm_operations_struct. In this
2116 +approach, aufs met a hard problem and I could not solve it without
2117 +switching the approach.
2118 +
2119 +There may be one more another approach which is
2120 +- bind-mount the branch-root onto the aufs-root internally
2121 +- grab the new vfsmount (ie. struct mount)
2122 +- lazy-umount the branch-root internally
2123 +- in open(2) the aufs-file, open the branch-file with the hidden
2124 +  vfsmount (instead of the original branch's vfsmount)
2125 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2126 +  but it may be possible from userspace by the mount helper.
2127 +
2128 +Adding the internal hidden vfsmount and using it in opening a file, the
2129 +file path under /proc will be printed correctly. This approach looks
2130 +smarter, but is not possible I am afraid.
2131 +- aufs-root may be bind-mount later. when it happens, another hidden
2132 +  vfsmount will be required.
2133 +- it is hard to get the chance to bind-mount and lazy-umount
2134 +  + in kernel-space, FS can have vfsmount in open(2) via
2135 +    file->f_path, and aufs can know its vfsmount. But several locks are
2136 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2137 +    here, then it may cause a deadlock.
2138 +  + in user-space, bind-mount doesn't invoke the mount helper.
2139 +- since /proc shows dev and ino, aufs has to give vma these info. it
2140 +  means a new member vm_prinode will be necessary. this is essentially
2141 +  equivalent to vm_prfile described above.
2142 +
2143 +I have to give up this "looks-smater" approach.
2144 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2145 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2146 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2022-11-05 23:02:18.959222617 +0100
2147 @@ -0,0 +1,94 @@
2148 +
2149 +# Copyright (C) 2014-2022 Junjiro R. Okajima
2150 +#
2151 +# This program is free software; you can redistribute it and/or modify
2152 +# it under the terms of the GNU General Public License as published by
2153 +# the Free Software Foundation; either version 2 of the License, or
2154 +# (at your option) any later version.
2155 +#
2156 +# This program is distributed in the hope that it will be useful,
2157 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2158 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2159 +# GNU General Public License for more details.
2160 +#
2161 +# You should have received a copy of the GNU General Public License
2162 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2163 +
2164 +Listing XATTR/EA and getting the value
2165 +----------------------------------------------------------------------
2166 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2167 +shows the values from the topmost existing file. This behaviour is good
2168 +for the non-dir entries since the bahaviour exactly matches the shown
2169 +information. But for the directories, aufs considers all the same named
2170 +entries on the lower branches. Which means, if one of the lower entry
2171 +rejects readdir call, then aufs returns an error even if the topmost
2172 +entry allows it. This behaviour is necessary to respect the branch fs's
2173 +security, but can make users confused since the user-visible standard
2174 +attributes don't match the behaviour.
2175 +To address this issue, aufs has a mount option called dirperm1 which
2176 +checks the permission for the topmost entry only, and ignores the lower
2177 +entry's permission.
2178 +
2179 +A similar issue can happen around XATTR.
2180 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2181 +always set. Otherwise these very unpleasant situation would happen.
2182 +- listxattr(2) may return the duplicated entries.
2183 +- users may not be able to remove or reset the XATTR forever,
2184 +
2185 +
2186 +XATTR/EA support in the internal (copy,move)-(up,down)
2187 +----------------------------------------------------------------------
2188 +Generally the extended attributes of inode are categorized as these.
2189 +- "security" for LSM and capability.
2190 +- "system" for posix ACL, 'acl' mount option is required for the branch
2191 +  fs generally.
2192 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2193 +- "user" for userspace, 'user_xattr' mount option is required for the
2194 +  branch fs generally.
2195 +
2196 +Moreover there are some other categories. Aufs handles these rather
2197 +unpopular categories as the ordinary ones, ie. there is no special
2198 +condition nor exception.
2199 +
2200 +In copy-up, the support for XATTR on the dst branch may differ from the
2201 +src branch. In this case, the copy-up operation will get an error and
2202 +the original user operation which triggered the copy-up will fail. It
2203 +can happen that even all copy-up will fail.
2204 +When both of src and dst branches support XATTR and if an error occurs
2205 +during copying XATTR, then the copy-up should fail obviously. That is a
2206 +good reason and aufs should return an error to userspace. But when only
2207 +the src branch support that XATTR, aufs should not return an error.
2208 +For example, the src branch supports ACL but the dst branch doesn't
2209 +because the dst branch may natively un-support it or temporary
2210 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2211 +may NOT return an error even if the XATTR is not supported. It is
2212 +totally up to the branch fs.
2213 +
2214 +Anyway when the aufs internal copy-up gets an error from the dst branch
2215 +fs, then aufs tries removing the just copied entry and returns the error
2216 +to the userspace. The worst case of this situation will be all copy-up
2217 +will fail.
2218 +
2219 +For the copy-up operation, there two basic approaches.
2220 +- copy the specified XATTR only (by category above), and return the
2221 +  error unconditionally if it happens.
2222 +- copy all XATTR, and ignore the error on the specified category only.
2223 +
2224 +In order to support XATTR and to implement the correct behaviour, aufs
2225 +chooses the latter approach and introduces some new branch attributes,
2226 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2227 +They correspond to the XATTR namespaces (see above). Additionally, to be
2228 +convenient, "icex" is also provided which means all "icex*" attributes
2229 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2230 +
2231 +The meaning of these attributes is to ignore the error from setting
2232 +XATTR on that branch.
2233 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2234 +error from the dst branch according to the specified attributes.
2235 +
2236 +Some XATTR may have its default value. The default value may come from
2237 +the parent dir or the environment. If the default value is set at the
2238 +file creating-time, it will be overwritten by copy-up.
2239 +Some contradiction may happen I am afraid.
2240 +Do we need another attribute to stop copying XATTR? I am unsure. For
2241 +now, aufs implements the branch attributes to ignore the error.
2242 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2243 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2244 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2022-11-05 23:02:18.959222617 +0100
2245 @@ -0,0 +1,58 @@
2246 +
2247 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2248 +#
2249 +# This program is free software; you can redistribute it and/or modify
2250 +# it under the terms of the GNU General Public License as published by
2251 +# the Free Software Foundation; either version 2 of the License, or
2252 +# (at your option) any later version.
2253 +#
2254 +# This program is distributed in the hope that it will be useful,
2255 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2256 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2257 +# GNU General Public License for more details.
2258 +#
2259 +# You should have received a copy of the GNU General Public License
2260 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2261 +
2262 +Export Aufs via NFS
2263 +----------------------------------------------------------------------
2264 +Here is an approach.
2265 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2266 +  generation.
2267 +- iget_locked(): initialize aufs inode generation for a new inode, and
2268 +  store it in xigen file.
2269 +- destroy_inode(): increment aufs inode generation and store it in xigen
2270 +  file. it is necessary even if it is not unlinked, because any data of
2271 +  inode may be changed by UDBA.
2272 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2273 +  build file handle by
2274 +  + branch id (4 bytes)
2275 +  + superblock generation (4 bytes)
2276 +  + inode number (4 or 8 bytes)
2277 +  + parent dir inode number (4 or 8 bytes)
2278 +  + inode generation (4 bytes))
2279 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2280 +    bytes)
2281 +  + file handle for a branch (by exportfs_encode_fh())
2282 +- fh_to_dentry():
2283 +  + find the index of a branch from its id in handle, and check it is
2284 +    still exist in aufs.
2285 +  + 1st level: get the inode number from handle and search it in cache.
2286 +  + 2nd level: if not found in cache, get the parent inode number from
2287 +    the handle and search it in cache. and then open the found parent
2288 +    dir, find the matching inode number by vfs_readdir() and get its
2289 +    name, and call lookup_one_len() for the target dentry.
2290 +  + 3rd level: if the parent dir is not cached, call
2291 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2292 +    build a pathname of it, convert it a pathname in aufs, call
2293 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2294 +    the 2nd level.
2295 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2296 +    for every branch, but not itself. to get this, (currently) aufs
2297 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2298 +    idea, but I didn't get other approach.
2299 +  + test the generation of the gotten inode.
2300 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2301 +  convert it into ESTALE for NFSD.
2302 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2303 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2304 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2305 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2306 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2022-11-05 23:02:18.959222617 +0100
2307 @@ -0,0 +1,52 @@
2308 +
2309 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2310 +#
2311 +# This program is free software; you can redistribute it and/or modify
2312 +# it under the terms of the GNU General Public License as published by
2313 +# the Free Software Foundation; either version 2 of the License, or
2314 +# (at your option) any later version.
2315 +#
2316 +# This program is distributed in the hope that it will be useful,
2317 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2318 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2319 +# GNU General Public License for more details.
2320 +#
2321 +# You should have received a copy of the GNU General Public License
2322 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2323 +
2324 +Show Whiteout Mode (shwh)
2325 +----------------------------------------------------------------------
2326 +Generally aufs hides the name of whiteouts. But in some cases, to show
2327 +them is very useful for users. For instance, creating a new middle layer
2328 +(branch) by merging existing layers.
2329 +
2330 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2331 +When you have three branches,
2332 +- Bottom: 'system', squashfs (underlying base system), read-only
2333 +- Middle: 'mods', squashfs, read-only
2334 +- Top: 'overlay', ram (tmpfs), read-write
2335 +
2336 +The top layer is loaded at boot time and saved at shutdown, to preserve
2337 +the changes made to the system during the session.
2338 +When larger changes have been made, or smaller changes have accumulated,
2339 +the size of the saved top layer data grows. At this point, it would be
2340 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2341 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2342 +restoring save and load speed.
2343 +
2344 +This merging is simplified by the use of another aufs mount, of just the
2345 +two overlay branches using the 'shwh' option.
2346 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2347 +       aufs /livesys/merge_union
2348 +
2349 +A merged view of these two branches is then available at
2350 +/livesys/merge_union, and the new feature is that the whiteouts are
2351 +visible!
2352 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2353 +writing to all branches. Also the default mode for all branches is 'ro'.
2354 +It is now possible to save the combined contents of the two overlay
2355 +branches to a new squashfs, e.g.:
2356 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2357 +
2358 +This new squashfs archive can be stored on the boot device and the
2359 +initramfs will use it to replace the old one at the next boot.
2360 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2361 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2362 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2022-11-05 23:02:18.959222617 +0100
2363 @@ -0,0 +1,47 @@
2364 +
2365 +# Copyright (C) 2010-2022 Junjiro R. Okajima
2366 +#
2367 +# This program is free software; you can redistribute it and/or modify
2368 +# it under the terms of the GNU General Public License as published by
2369 +# the Free Software Foundation; either version 2 of the License, or
2370 +# (at your option) any later version.
2371 +#
2372 +# This program is distributed in the hope that it will be useful,
2373 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2374 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2375 +# GNU General Public License for more details.
2376 +#
2377 +# You should have received a copy of the GNU General Public License
2378 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2379 +
2380 +Dynamically customizable FS operations
2381 +----------------------------------------------------------------------
2382 +Generally FS operations (struct inode_operations, struct
2383 +address_space_operations, struct file_operations, etc.) are defined as
2384 +"static const", but it never means that FS have only one set of
2385 +operation. Some FS have multiple sets of them. For instance, ext2 has
2386 +three sets, one for XIP, for NOBH, and for normal.
2387 +Since aufs overrides and redirects these operations, sometimes aufs has
2388 +to change its behaviour according to the branch FS type. More importantly
2389 +VFS acts differently if a function (member in the struct) is set or
2390 +not. It means aufs should have several sets of operations and select one
2391 +among them according to the branch FS definition.
2392 +
2393 +In order to solve this problem and not to affect the behaviour of VFS,
2394 +aufs defines these operations dynamically. For instance, aufs defines
2395 +dummy direct_IO function for struct address_space_operations, but it may
2396 +not be set to the address_space_operations actually. When the branch FS
2397 +doesn't have it, aufs doesn't set it to its address_space_operations
2398 +while the function definition itself is still alive. So the behaviour
2399 +itself will not change, and it will return an error when direct_IO is
2400 +not set.
2401 +
2402 +The lifetime of these dynamically generated operation object is
2403 +maintained by aufs branch object. When the branch is removed from aufs,
2404 +the reference counter of the object is decremented. When it reaches
2405 +zero, the dynamically generated operation object will be freed.
2406 +
2407 +This approach is designed to support AIO (io_submit), Direct I/O and
2408 +XIP (DAX) mainly.
2409 +Currently this approach is applied to address_space_operations for
2410 +regular files only.
2411 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2412 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2413 +++ linux/Documentation/filesystems/aufs/README 2023-09-02 12:00:06.376642958 +0200
2414 @@ -0,0 +1,409 @@
2415 +
2416 +Aufs6 -- advanced multi layered unification filesystem version 6.x
2417 +http://aufs.sf.net
2418 +Junjiro R. Okajima
2419 +
2420 +
2421 +0. Introduction
2422 +----------------------------------------
2423 +In the early days, aufs was entirely re-designed and re-implemented
2424 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2425 +improvements and implementations, it became totally different from
2426 +Unionfs while keeping the basic features.
2427 +Later, Unionfs Version 2.x series began taking some of the same
2428 +approaches to aufs1's.
2429 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2430 +University and his team.
2431 +
2432 +Aufs6 supports linux-v6.0 and later, try aufs6.0 branch in
2433 +aufs-linux.git or aufs-standalone.git.
2434 +If you want older kernel version support,
2435 +- for linux-v5.x series, try aufs-linux.git or aufs-standalone.git
2436 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2437 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2438 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2439 +  or aufs1 from CVS on SourceForge.
2440 +
2441 +Note: the name of aufs5-linux.git and aufs5-standalone.git on github
2442 +      were changed. Now they are aufs-linux.git and
2443 +      aufs-standalone.git and they contain aufs5 and later branches.
2444 +
2445 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2446 +      According to Christoph Hellwig, linux rejects all union-type
2447 +      filesystems but UnionMount.
2448 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2449 +
2450 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2451 +    UnionMount, and he pointed out an issue around a directory mutex
2452 +    lock and aufs addressed it. But it is still unsure whether aufs will
2453 +    be merged (or any other union solution).
2454 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2455 +
2456 +
2457 +1. Features
2458 +----------------------------------------
2459 +- unite several directories into a single virtual filesystem. The member
2460 +  directory is called as a branch.
2461 +- you can specify the permission flags to the branch, which are 'readonly',
2462 +  'readwrite' and 'whiteout-able.'
2463 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2464 +  readonly branch are modifiable logically.
2465 +- dynamic branch manipulation, add, del.
2466 +- etc...
2467 +
2468 +Also there are many enhancements in aufs, such as:
2469 +- test only the highest one for the directory permission (dirperm1)
2470 +- copyup on open (coo=)
2471 +- 'move' policy for copy-up between two writable branches, after
2472 +  checking free space.
2473 +- xattr, acl
2474 +- readdir(3) in userspace.
2475 +- keep inode number by external inode number table
2476 +- keep the timestamps of file/dir in internal copyup operation
2477 +- seekable directory, supporting NFS readdir.
2478 +- whiteout is hardlinked in order to reduce the consumption of inodes
2479 +  on branch
2480 +- do not copyup, nor create a whiteout when it is unnecessary
2481 +- revert a single systemcall when an error occurs in aufs
2482 +- remount interface instead of ioctl
2483 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2484 +- loopback mounted filesystem as a branch
2485 +- kernel thread for removing the dir who has a plenty of whiteouts
2486 +- support copyup sparse file (a file which has a 'hole' in it)
2487 +- default permission flags for branches
2488 +- selectable permission flags for ro branch, whether whiteout can
2489 +  exist or not
2490 +- export via NFS.
2491 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2492 +- support multiple writable branches, some policies to select one
2493 +  among multiple writable branches.
2494 +- a new semantics for link(2) and rename(2) to support multiple
2495 +  writable branches.
2496 +- no glibc changes are required.
2497 +- pseudo hardlink (hardlink over branches)
2498 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2499 +  including NFS or remote filesystem branch.
2500 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2501 +- and more...
2502 +
2503 +Currently these features are dropped temporary from aufs6.
2504 +See design/08plan.txt in detail.
2505 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2506 +  (robr)
2507 +- statistics of aufs thread (/sys/fs/aufs/stat)
2508 +
2509 +Features or just an idea in the future (see also design/*.txt),
2510 +- reorder the branch index without del/re-add.
2511 +- permanent xino files for NFSD
2512 +- an option for refreshing the opened files after add/del branches
2513 +- light version, without branch manipulation. (unnecessary?)
2514 +- copyup in userspace
2515 +- inotify in userspace
2516 +- readv/writev
2517 +
2518 +
2519 +2. Download
2520 +----------------------------------------
2521 +There are three GIT trees for aufs6, aufs-linux.git,
2522 +aufs-standalone.git, and aufs-util.git.
2523 +While the aufs-util is always necessary, you need either of aufs-linux
2524 +or aufs-standalone.
2525 +
2526 +The aufs-linux tree includes the whole linux mainline GIT tree,
2527 +git://git.kernel.org/.../torvalds/linux.git.
2528 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2529 +build aufs6 as an external kernel module.
2530 +Several extra patches are not included in this tree. Only
2531 +aufs-standalone tree contains them. They are described in the later
2532 +section "Configuration and Compilation."
2533 +
2534 +On the other hand, the aufs-standalone tree has only aufs source files
2535 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2536 +But you need to apply all aufs patches manually.
2537 +
2538 +You will find GIT branches whose name is in form of "aufs6.x" where "x"
2539 +represents the linux kernel version, "linux-6.x". For instance,
2540 +"aufs6.0" is for linux-6.0. For latest "linux-6.x-rcN", use
2541 +"aufs6.x-rcN" branch.
2542 +
2543 +o aufs-linux tree
2544 +$ git clone --reference /your/linux/git/tree \
2545 +       git://github.com/sfjro/aufs-linux.git aufs-linux.git
2546 +- if you don't have linux GIT tree, then remove "--reference ..."
2547 +$ cd aufs-linux.git
2548 +$ git checkout origin/aufs6.0
2549 +
2550 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2551 +leave the patch-work to GIT.
2552 +$ cd /your/linux/git/tree
2553 +$ git remote add aufs git://github.com/sfjro/aufs-linux.git
2554 +$ git fetch aufs
2555 +$ git checkout -b my6.0 v6.0
2556 +$ (add your local change...)
2557 +$ git pull aufs aufs6.0
2558 +- now you have v6.0 + your_changes + aufs6.0 in you my6.0 branch.
2559 +- you may need to solve some conflicts between your_changes and
2560 +  aufs6.0. in this case, git-rerere is recommended so that you can
2561 +  solve the similar conflicts automatically when you upgrade to 6.1 or
2562 +  later in the future.
2563 +
2564 +o aufs-standalone tree
2565 +$ git clone git://github.com/sfjro/aufs-standalone.git aufs-standalone.git
2566 +$ cd aufs-standalone.git
2567 +$ git checkout origin/aufs6.0
2568 +
2569 +o aufs-util tree
2570 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2571 +- note that the public aufs-util.git is on SourceForge instead of
2572 +  GitHUB.
2573 +$ cd aufs-util.git
2574 +$ git checkout origin/aufs6.0
2575 +
2576 +Note: The 6.x-rcN branch is to be used with `rc' kernel versions ONLY.
2577 +The minor version number, 'x' in '6.x', of aufs may not always
2578 +follow the minor version number of the kernel.
2579 +Because changes in the kernel that cause the use of a new
2580 +minor version number do not always require changes to aufs-util.
2581 +
2582 +Since aufs-util has its own minor version number, you may not be
2583 +able to find a GIT branch in aufs-util for your kernel's
2584 +exact minor version number.
2585 +In this case, you should git-checkout the branch for the
2586 +nearest lower number.
2587 +
2588 +For (an unreleased) example:
2589 +If you are using "linux-6.10" and the "aufs6.10" branch
2590 +does not exist in aufs-util repository, then "aufs6.9", "aufs6.8"
2591 +or something numerically smaller is the branch for your kernel.
2592 +
2593 +Also you can view all branches by
2594 +       $ git branch -a
2595 +
2596 +
2597 +3. Configuration and Compilation
2598 +----------------------------------------
2599 +Make sure you have git-checkout'ed the correct branch.
2600 +
2601 +For aufs-linux tree,
2602 +- enable CONFIG_AUFS_FS.
2603 +- set other aufs configurations if necessary.
2604 +- for aufs5.13 and later
2605 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2606 +  also a caller of VFS functions for branch filesystems, subclassing of
2607 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2608 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2609 +  need to customize some LOCKDEP numbers. Here are what I use on my
2610 +  test environment.
2611 +       CONFIG_LOCKDEP_BITS=21
2612 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2613 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2614 +  Also you will need to expand some constant values in LOCKDEP. Refer
2615 +  to lockdep-debug.patch in aufs-standalone.git.
2616 +
2617 +For aufs-standalone tree,
2618 +There are several ways to build.
2619 +
2620 +1.
2621 +- apply ./aufs6-kbuild.patch to your kernel source files.
2622 +- apply ./aufs6-base.patch too.
2623 +- apply ./aufs6-mmap.patch too.
2624 +- apply ./aufs6-standalone.patch too, if you have a plan to set
2625 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs-standalone.patch.
2626 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2627 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2628 +- enable CONFIG_AUFS_FS, you can select either
2629 +  =m or =y.
2630 +- and build your kernel as usual.
2631 +- install the built kernel.
2632 +- install the header files too by "make headers_install" to the
2633 +  directory where you specify. By default, it is $PWD/usr.
2634 +  "make help" shows a brief note for headers_install.
2635 +- and reboot your system.
2636 +
2637 +2.
2638 +- module only (CONFIG_AUFS_FS=m).
2639 +- apply ./aufs6-base.patch to your kernel source files.
2640 +- apply ./aufs6-mmap.patch too.
2641 +- apply ./aufs6-standalone.patch too.
2642 +- build your kernel, don't forget "make headers_install", and reboot.
2643 +- edit ./config.mk and set other aufs configurations if necessary.
2644 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2645 +  every aufs configurations.
2646 +- build the module by simple "make".
2647 +- you can specify ${KDIR} make variable which points to your kernel
2648 +  source tree.
2649 +- install the files
2650 +  + run "make install" to install the aufs module, or copy the built
2651 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2652 +  + run "make install_headers" (instead of headers_install) to install
2653 +    the modified aufs header file (you can specify DESTDIR which is
2654 +    available in aufs standalone version's Makefile only), or copy
2655 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2656 +    you like manually. By default, the target directory is $PWD/usr.
2657 +- no need to apply aufs6-kbuild.patch, nor copying source files to your
2658 +  kernel source tree.
2659 +
2660 +Note: The header file aufs_type.h is necessary to build aufs-util
2661 +      as well as "make headers_install" in the kernel source tree.
2662 +      headers_install is subject to be forgotten, but it is essentially
2663 +      necessary, not only for building aufs-util.
2664 +      You may not meet problems without headers_install in some older
2665 +      version though.
2666 +
2667 +And then,
2668 +- read README in aufs-util, build and install it
2669 +- note that your distribution may contain an obsoleted version of
2670 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2671 +  utilities, make sure that your compiler refers the correct aufs header
2672 +  file which is built by "make headers_install."
2673 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2674 +  then run "make install_ulib" too. And refer to the aufs manual in
2675 +  detail.
2676 +
2677 +There several other patches in aufs-standalone.git. They are all
2678 +optional. When you meet some problems, they will help you.
2679 +- aufs6-loopback.patch
2680 +  Supports a nested loopback mount in a branch-fs. This patch is
2681 +  unnecessary until aufs produces a message like "you may want to try
2682 +  another patch for loopback file".
2683 +- vfs-ino.patch
2684 +  Modifies a system global kernel internal function get_next_ino() in
2685 +  order to stop assigning 0 for an inode-number. Not directly related to
2686 +  aufs, but recommended generally.
2687 +- tmpfs-idr.patch
2688 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2689 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2690 +  duplication of inode number, which is important for backup tools and
2691 +  other utilities. When you find aufs XINO files for tmpfs branch
2692 +  growing too much, try this patch.
2693 +- lockdep-debug.patch
2694 +  Similar to some kernel configurations for LOCKDEP (see the top of
2695 +  this section), you will need expand some constants in LOCKDEP for
2696 +  aufs if you enable CONFIG_LOCKDEP.
2697 +
2698 +
2699 +4. Usage
2700 +----------------------------------------
2701 +At first, make sure aufs-util are installed, and please read the aufs
2702 +manual, aufs.5 in aufs-util.git tree.
2703 +$ man -l aufs.5
2704 +
2705 +And then,
2706 +$ mkdir /tmp/rw /tmp/aufs
2707 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2708 +
2709 +Here is another example. The result is equivalent.
2710 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2711 +  Or
2712 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2713 +# mount -o remount,append:${HOME} /tmp/aufs
2714 +
2715 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2716 +you modify a file under /tmp/aufs, the one on your home directory is
2717 +not affected, instead the same named file will be newly created under
2718 +/tmp/rw. And all of your modification to a file will be applied to
2719 +the one under /tmp/rw. This is called the file based Copy on Write
2720 +(COW) method.
2721 +Aufs mount options are described in aufs.5.
2722 +If you run chroot or something and make your aufs as a root directory,
2723 +then you need to customize the shutdown script. See the aufs manual in
2724 +detail.
2725 +
2726 +Additionally, there are some sample usages of aufs which are a
2727 +diskless system with network booting, and LiveCD over NFS.
2728 +See sample dir in CVS tree on SourceForge.
2729 +
2730 +
2731 +5. Contact
2732 +----------------------------------------
2733 +When you have any problems or strange behaviour in aufs, please let me
2734 +know with:
2735 +- /proc/mounts (instead of the output of mount(8))
2736 +- /sys/module/aufs/*
2737 +- /sys/fs/aufs/* (if you have them)
2738 +- /debug/aufs/* (if you have them)
2739 +- linux kernel version
2740 +  if your kernel is not plain, for example modified by distributor,
2741 +  the url where i can download its source is necessary too.
2742 +- aufs version which was printed at loading the module or booting the
2743 +  system, instead of the date you downloaded.
2744 +- configuration (define/undefine CONFIG_AUFS_xxx)
2745 +- kernel configuration or /proc/config.gz (if you have it)
2746 +- LSM (linux security module, if you are using)
2747 +- behaviour which you think to be incorrect
2748 +- actual operation, reproducible one is better
2749 +- mailto: aufs-users at lists.sourceforge.net
2750 +
2751 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2752 +and Feature Requests) on SourceForge. Please join and write to
2753 +aufs-users ML.
2754 +
2755 +
2756 +6. Acknowledgements
2757 +----------------------------------------
2758 +Thanks to everyone who have tried and are using aufs, whoever
2759 +have reported a bug or any feedback.
2760 +
2761 +Especially donators:
2762 +Tomas Matejicek(slax.org) made a donation (much more than once).
2763 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2764 +       scripts) is making "doubling" donations.
2765 +       Unfortunately I cannot list all of the donators, but I really
2766 +       appreciate.
2767 +       It ends Aug 2010, but the ordinary donation URL is still available.
2768 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2769 +Dai Itasaka made a donation (2007/8).
2770 +Chuck Smith made a donation (2008/4, 10 and 12).
2771 +Henk Schoneveld made a donation (2008/9).
2772 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2773 +Francois Dupoux made a donation (2008/11).
2774 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2775 +       aufs2 GIT tree (2009/2).
2776 +William Grant made a donation (2009/3).
2777 +Patrick Lane made a donation (2009/4).
2778 +The Mail Archive (mail-archive.com) made donations (2009/5).
2779 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2780 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2781 +Pavel Pronskiy made a donation (2011/2).
2782 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2783 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2784 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2785 +11).
2786 +Sam Liddicott made a donation (2011/9).
2787 +Era Scarecrow made a donation (2013/4).
2788 +Bor Ratajc made a donation (2013/4).
2789 +Alessandro Gorreta made a donation (2013/4).
2790 +POIRETTE Marc made a donation (2013/4).
2791 +Alessandro Gorreta made a donation (2013/4).
2792 +lauri kasvandik made a donation (2013/5).
2793 +"pemasu from Finland" made a donation (2013/7).
2794 +The Parted Magic Project made a donation (2013/9 and 11).
2795 +Pavel Barta made a donation (2013/10).
2796 +Nikolay Pertsev made a donation (2014/5).
2797 +James B made a donation (2014/7, 2015/7, and 2021/12).
2798 +Stefano Di Biase made a donation (2014/8).
2799 +Daniel Epellei made a donation (2015/1).
2800 +OmegaPhil made a donation (2016/1, 2018/4).
2801 +Tomasz Szewczyk made a donation (2016/4).
2802 +James Burry made a donation (2016/12).
2803 +Carsten Rose made a donation (2018/9).
2804 +Porteus Kiosk made a donation (2018/10).
2805 +huronOS team: Enya Quetzalli made donations (2022/5, 2023/5 and 8).
2806 +Vasily Mikhaylichenko made a donation (2023/5).
2807 +
2808 +Thank you very much.
2809 +Donations are always, including future donations, very important and
2810 +helpful for me to keep on developing aufs.
2811 +
2812 +
2813 +7.
2814 +----------------------------------------
2815 +If you are an experienced user, no explanation is needed. Aufs is
2816 +just a linux filesystem.
2817 +
2818 +
2819 +Enjoy!
2820 +
2821 +# Local variables: ;
2822 +# mode: text;
2823 +# End: ;
2824 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2825 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2826 +++ linux/fs/aufs/aufs.h        2022-11-05 23:02:18.959222617 +0100
2827 @@ -0,0 +1,62 @@
2828 +/* SPDX-License-Identifier: GPL-2.0 */
2829 +/*
2830 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2831 + *
2832 + * This program is free software; you can redistribute it and/or modify
2833 + * it under the terms of the GNU General Public License as published by
2834 + * the Free Software Foundation; either version 2 of the License, or
2835 + * (at your option) any later version.
2836 + *
2837 + * This program is distributed in the hope that it will be useful,
2838 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2839 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2840 + * GNU General Public License for more details.
2841 + *
2842 + * You should have received a copy of the GNU General Public License
2843 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2844 + */
2845 +
2846 +/*
2847 + * all header files
2848 + */
2849 +
2850 +#ifndef __AUFS_H__
2851 +#define __AUFS_H__
2852 +
2853 +#ifdef __KERNEL__
2854 +
2855 +#define AuStub(type, name, body, ...) \
2856 +       static inline type name(__VA_ARGS__) { body; }
2857 +
2858 +#define AuStubVoid(name, ...) \
2859 +       AuStub(void, name, , __VA_ARGS__)
2860 +#define AuStubInt0(name, ...) \
2861 +       AuStub(int, name, return 0, __VA_ARGS__)
2862 +
2863 +#include "debug.h"
2864 +
2865 +#include "branch.h"
2866 +#include "cpup.h"
2867 +#include "dcsub.h"
2868 +#include "dbgaufs.h"
2869 +#include "dentry.h"
2870 +#include "dir.h"
2871 +#include "dirren.h"
2872 +#include "dynop.h"
2873 +#include "file.h"
2874 +#include "fstype.h"
2875 +#include "hbl.h"
2876 +#include "inode.h"
2877 +#include "lcnt.h"
2878 +#include "loop.h"
2879 +#include "module.h"
2880 +#include "opts.h"
2881 +#include "rwsem.h"
2882 +#include "super.h"
2883 +#include "sysaufs.h"
2884 +#include "vfsub.h"
2885 +#include "whout.h"
2886 +#include "wkq.h"
2887 +
2888 +#endif /* __KERNEL__ */
2889 +#endif /* __AUFS_H__ */
2890 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2891 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2892 +++ linux/fs/aufs/branch.c      2022-11-05 23:02:18.959222617 +0100
2893 @@ -0,0 +1,1427 @@
2894 +// SPDX-License-Identifier: GPL-2.0
2895 +/*
2896 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2897 + *
2898 + * This program is free software; you can redistribute it and/or modify
2899 + * it under the terms of the GNU General Public License as published by
2900 + * the Free Software Foundation; either version 2 of the License, or
2901 + * (at your option) any later version.
2902 + *
2903 + * This program is distributed in the hope that it will be useful,
2904 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2905 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2906 + * GNU General Public License for more details.
2907 + *
2908 + * You should have received a copy of the GNU General Public License
2909 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2910 + */
2911 +
2912 +/*
2913 + * branch management
2914 + */
2915 +
2916 +#include <linux/compat.h>
2917 +#include <linux/statfs.h>
2918 +#include "aufs.h"
2919 +
2920 +/*
2921 + * free a single branch
2922 + */
2923 +static void au_br_do_free(struct au_branch *br)
2924 +{
2925 +       int i;
2926 +       struct au_wbr *wbr;
2927 +       struct au_dykey **key;
2928 +
2929 +       au_hnotify_fin_br(br);
2930 +       /* always, regardless the mount option */
2931 +       au_dr_hino_free(&br->br_dirren);
2932 +       au_xino_put(br);
2933 +
2934 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2935 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2936 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2937 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2938 +
2939 +       wbr = br->br_wbr;
2940 +       if (wbr) {
2941 +               for (i = 0; i < AuBrWh_Last; i++)
2942 +                       dput(wbr->wbr_wh[i]);
2943 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2944 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2945 +       }
2946 +
2947 +       if (br->br_fhsm) {
2948 +               au_br_fhsm_fin(br->br_fhsm);
2949 +               au_kfree_try_rcu(br->br_fhsm);
2950 +       }
2951 +
2952 +       key = br->br_dykey;
2953 +       for (i = 0; i < AuBrDynOp; i++, key++)
2954 +               if (*key)
2955 +                       au_dy_put(*key);
2956 +               else
2957 +                       break;
2958 +
2959 +       /* recursive lock, s_umount of branch's */
2960 +       /* synchronize_rcu(); */ /* why? */
2961 +       lockdep_off();
2962 +       path_put(&br->br_path);
2963 +       lockdep_on();
2964 +       au_kfree_rcu(wbr);
2965 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2966 +       au_lcnt_wait_for_fin(&br->br_count);
2967 +       /* I don't know why, but percpu_refcount requires this */
2968 +       /* synchronize_rcu(); */
2969 +       au_kfree_rcu(br);
2970 +}
2971 +
2972 +/*
2973 + * frees all branches
2974 + */
2975 +void au_br_free(struct au_sbinfo *sbinfo)
2976 +{
2977 +       aufs_bindex_t bmax;
2978 +       struct au_branch **br;
2979 +
2980 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2981 +
2982 +       bmax = sbinfo->si_bbot + 1;
2983 +       br = sbinfo->si_branch;
2984 +       while (bmax--)
2985 +               au_br_do_free(*br++);
2986 +}
2987 +
2988 +/*
2989 + * find the index of a branch which is specified by @br_id.
2990 + */
2991 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2992 +{
2993 +       aufs_bindex_t bindex, bbot;
2994 +
2995 +       bbot = au_sbbot(sb);
2996 +       for (bindex = 0; bindex <= bbot; bindex++)
2997 +               if (au_sbr_id(sb, bindex) == br_id)
2998 +                       return bindex;
2999 +       return -1;
3000 +}
3001 +
3002 +/* ---------------------------------------------------------------------- */
3003 +
3004 +/*
3005 + * add a branch
3006 + */
3007 +
3008 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3009 +                       struct dentry *h_root)
3010 +{
3011 +       if (unlikely(h_adding == h_root
3012 +                    || au_test_loopback_overlap(sb, h_adding)))
3013 +               return 1;
3014 +       if (h_adding->d_sb != h_root->d_sb)
3015 +               return 0;
3016 +       return au_test_subdir(h_adding, h_root)
3017 +               || au_test_subdir(h_root, h_adding);
3018 +}
3019 +
3020 +/*
3021 + * returns a newly allocated branch. @new_nbranch is a number of branches
3022 + * after adding a branch.
3023 + */
3024 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3025 +                                    int perm)
3026 +{
3027 +       struct au_branch *add_branch;
3028 +       struct dentry *root;
3029 +       struct inode *inode;
3030 +       int err;
3031 +
3032 +       err = -ENOMEM;
3033 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3034 +       if (unlikely(!add_branch))
3035 +               goto out;
3036 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3037 +       if (unlikely(!add_branch->br_xino))
3038 +               goto out_br;
3039 +       err = au_hnotify_init_br(add_branch, perm);
3040 +       if (unlikely(err))
3041 +               goto out_xino;
3042 +
3043 +       if (au_br_writable(perm)) {
3044 +               /* may be freed separately at changing the branch permission */
3045 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3046 +                                            GFP_NOFS);
3047 +               if (unlikely(!add_branch->br_wbr))
3048 +                       goto out_hnotify;
3049 +       }
3050 +
3051 +       if (au_br_fhsm(perm)) {
3052 +               err = au_fhsm_br_alloc(add_branch);
3053 +               if (unlikely(err))
3054 +                       goto out_wbr;
3055 +       }
3056 +
3057 +       root = sb->s_root;
3058 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3059 +       if (!err)
3060 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3061 +       if (!err) {
3062 +               inode = d_inode(root);
3063 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3064 +                                       /*may_shrink*/0);
3065 +       }
3066 +       if (!err)
3067 +               return add_branch; /* success */
3068 +
3069 +out_wbr:
3070 +       au_kfree_rcu(add_branch->br_wbr);
3071 +out_hnotify:
3072 +       au_hnotify_fin_br(add_branch);
3073 +out_xino:
3074 +       au_xino_put(add_branch);
3075 +out_br:
3076 +       au_kfree_rcu(add_branch);
3077 +out:
3078 +       return ERR_PTR(err);
3079 +}
3080 +
3081 +/*
3082 + * test if the branch permission is legal or not.
3083 + */
3084 +static int test_br(struct inode *inode, int brperm, char *path)
3085 +{
3086 +       int err;
3087 +
3088 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3089 +       if (!err)
3090 +               goto out;
3091 +
3092 +       err = -EINVAL;
3093 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3094 +
3095 +out:
3096 +       return err;
3097 +}
3098 +
3099 +/*
3100 + * returns:
3101 + * 0: success, the caller will add it
3102 + * plus: success, it is already unified, the caller should ignore it
3103 + * minus: error
3104 + */
3105 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3106 +{
3107 +       int err;
3108 +       aufs_bindex_t bbot, bindex;
3109 +       struct dentry *root, *h_dentry;
3110 +       struct inode *inode, *h_inode;
3111 +
3112 +       root = sb->s_root;
3113 +       bbot = au_sbbot(sb);
3114 +       if (unlikely(bbot >= 0
3115 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3116 +               err = 1;
3117 +               if (!remount) {
3118 +                       err = -EINVAL;
3119 +                       pr_err("%s duplicated\n", add->pathname);
3120 +               }
3121 +               goto out;
3122 +       }
3123 +
3124 +       err = -ENOSPC; /* -E2BIG; */
3125 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3126 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3127 +               pr_err("number of branches exceeded %s\n", add->pathname);
3128 +               goto out;
3129 +       }
3130 +
3131 +       err = -EDOM;
3132 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3133 +               pr_err("bad index %d\n", add->bindex);
3134 +               goto out;
3135 +       }
3136 +
3137 +       inode = d_inode(add->path.dentry);
3138 +       err = -ENOENT;
3139 +       if (unlikely(!inode->i_nlink)) {
3140 +               pr_err("no existence %s\n", add->pathname);
3141 +               goto out;
3142 +       }
3143 +
3144 +       err = -EINVAL;
3145 +       if (unlikely(inode->i_sb == sb)) {
3146 +               pr_err("%s must be outside\n", add->pathname);
3147 +               goto out;
3148 +       }
3149 +
3150 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3151 +               pr_err("unsupported filesystem, %s (%s)\n",
3152 +                      add->pathname, au_sbtype(inode->i_sb));
3153 +               goto out;
3154 +       }
3155 +
3156 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3157 +               pr_err("already stacked, %s (%s)\n",
3158 +                      add->pathname, au_sbtype(inode->i_sb));
3159 +               goto out;
3160 +       }
3161 +
3162 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3163 +       if (unlikely(err))
3164 +               goto out;
3165 +
3166 +       if (bbot < 0)
3167 +               return 0; /* success */
3168 +
3169 +       err = -EINVAL;
3170 +       for (bindex = 0; bindex <= bbot; bindex++)
3171 +               if (unlikely(test_overlap(sb, add->path.dentry,
3172 +                                         au_h_dptr(root, bindex)))) {
3173 +                       pr_err("%s is overlapped\n", add->pathname);
3174 +                       goto out;
3175 +               }
3176 +
3177 +       err = 0;
3178 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3179 +               h_dentry = au_h_dptr(root, 0);
3180 +               h_inode = d_inode(h_dentry);
3181 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3182 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3183 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3184 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3185 +                               add->pathname,
3186 +                               i_uid_read(inode), i_gid_read(inode),
3187 +                               (inode->i_mode & S_IALLUGO),
3188 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3189 +                               (h_inode->i_mode & S_IALLUGO));
3190 +       }
3191 +
3192 +out:
3193 +       return err;
3194 +}
3195 +
3196 +/*
3197 + * initialize or clean the whiteouts for an adding branch
3198 + */
3199 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3200 +                        int new_perm)
3201 +{
3202 +       int err, old_perm;
3203 +       aufs_bindex_t bindex;
3204 +       struct inode *h_inode;
3205 +       struct au_wbr *wbr;
3206 +       struct au_hinode *hdir;
3207 +       struct dentry *h_dentry;
3208 +
3209 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3210 +       if (unlikely(err))
3211 +               goto out;
3212 +
3213 +       wbr = br->br_wbr;
3214 +       old_perm = br->br_perm;
3215 +       br->br_perm = new_perm;
3216 +       hdir = NULL;
3217 +       h_inode = NULL;
3218 +       bindex = au_br_index(sb, br->br_id);
3219 +       if (0 <= bindex) {
3220 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3221 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3222 +       } else {
3223 +               h_dentry = au_br_dentry(br);
3224 +               h_inode = d_inode(h_dentry);
3225 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3226 +       }
3227 +       if (!wbr)
3228 +               err = au_wh_init(br, sb);
3229 +       else {
3230 +               wbr_wh_write_lock(wbr);
3231 +               err = au_wh_init(br, sb);
3232 +               wbr_wh_write_unlock(wbr);
3233 +       }
3234 +       if (hdir)
3235 +               au_hn_inode_unlock(hdir);
3236 +       else
3237 +               inode_unlock(h_inode);
3238 +       vfsub_mnt_drop_write(au_br_mnt(br));
3239 +       br->br_perm = old_perm;
3240 +
3241 +       if (!err && wbr && !au_br_writable(new_perm)) {
3242 +               au_kfree_rcu(wbr);
3243 +               br->br_wbr = NULL;
3244 +       }
3245 +
3246 +out:
3247 +       return err;
3248 +}
3249 +
3250 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3251 +                      int perm)
3252 +{
3253 +       int err;
3254 +       struct kstatfs kst;
3255 +       struct au_wbr *wbr;
3256 +
3257 +       wbr = br->br_wbr;
3258 +       au_rw_init(&wbr->wbr_wh_rwsem);
3259 +       atomic_set(&wbr->wbr_wh_running, 0);
3260 +
3261 +       /*
3262 +        * a limit for rmdir/rename a dir
3263 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3264 +        */
3265 +       err = vfs_statfs(&br->br_path, &kst);
3266 +       if (unlikely(err))
3267 +               goto out;
3268 +       err = -EINVAL;
3269 +       if (kst.f_namelen >= NAME_MAX)
3270 +               err = au_br_init_wh(sb, br, perm);
3271 +       else
3272 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3273 +                      au_br_dentry(br),
3274 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3275 +
3276 +out:
3277 +       return err;
3278 +}
3279 +
3280 +/* initialize a new branch */
3281 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3282 +                     struct au_opt_add *add)
3283 +{
3284 +       int err;
3285 +       struct au_branch *brbase;
3286 +       struct file *xf;
3287 +       struct inode *h_inode;
3288 +
3289 +       err = 0;
3290 +       br->br_perm = add->perm;
3291 +       br->br_path = add->path; /* set first, path_get() later */
3292 +       spin_lock_init(&br->br_dykey_lock);
3293 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3294 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3295 +       br->br_id = au_new_br_id(sb);
3296 +       AuDebugOn(br->br_id < 0);
3297 +
3298 +       /* always, regardless the given option */
3299 +       err = au_dr_br_init(sb, br, &add->path);
3300 +       if (unlikely(err))
3301 +               goto out_err;
3302 +
3303 +       if (au_br_writable(add->perm)) {
3304 +               err = au_wbr_init(br, sb, add->perm);
3305 +               if (unlikely(err))
3306 +                       goto out_err;
3307 +       }
3308 +
3309 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3310 +               brbase = au_sbr(sb, 0);
3311 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3312 +               AuDebugOn(!xf);
3313 +               h_inode = d_inode(add->path.dentry);
3314 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3315 +               if (unlikely(err)) {
3316 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3317 +                       goto out_err;
3318 +               }
3319 +       }
3320 +
3321 +       sysaufs_br_init(br);
3322 +       path_get(&br->br_path);
3323 +       goto out; /* success */
3324 +
3325 +out_err:
3326 +       memset(&br->br_path, 0, sizeof(br->br_path));
3327 +out:
3328 +       return err;
3329 +}
3330 +
3331 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3332 +                            struct au_branch *br, aufs_bindex_t bbot,
3333 +                            aufs_bindex_t amount)
3334 +{
3335 +       struct au_branch **brp;
3336 +
3337 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3338 +
3339 +       brp = sbinfo->si_branch + bindex;
3340 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3341 +       *brp = br;
3342 +       sbinfo->si_bbot++;
3343 +       if (unlikely(bbot < 0))
3344 +               sbinfo->si_bbot = 0;
3345 +}
3346 +
3347 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3348 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3349 +{
3350 +       struct au_hdentry *hdp;
3351 +
3352 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3353 +
3354 +       hdp = au_hdentry(dinfo, bindex);
3355 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3356 +       au_h_dentry_init(hdp);
3357 +       dinfo->di_bbot++;
3358 +       if (unlikely(bbot < 0))
3359 +               dinfo->di_btop = 0;
3360 +}
3361 +
3362 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3363 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3364 +{
3365 +       struct au_hinode *hip;
3366 +
3367 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3368 +
3369 +       hip = au_hinode(iinfo, bindex);
3370 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3371 +       au_hinode_init(hip);
3372 +       iinfo->ii_bbot++;
3373 +       if (unlikely(bbot < 0))
3374 +               iinfo->ii_btop = 0;
3375 +}
3376 +
3377 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3378 +                        aufs_bindex_t bindex)
3379 +{
3380 +       struct dentry *root, *h_dentry;
3381 +       struct inode *root_inode, *h_inode;
3382 +       aufs_bindex_t bbot, amount;
3383 +
3384 +       root = sb->s_root;
3385 +       root_inode = d_inode(root);
3386 +       bbot = au_sbbot(sb);
3387 +       amount = bbot + 1 - bindex;
3388 +       h_dentry = au_br_dentry(br);
3389 +       au_sbilist_lock();
3390 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3391 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3392 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3393 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3394 +       h_inode = d_inode(h_dentry);
3395 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3396 +       au_sbilist_unlock();
3397 +}
3398 +
3399 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3400 +{
3401 +       int err;
3402 +       aufs_bindex_t bbot, add_bindex;
3403 +       struct dentry *root, *h_dentry;
3404 +       struct inode *root_inode;
3405 +       struct au_branch *add_branch;
3406 +
3407 +       root = sb->s_root;
3408 +       root_inode = d_inode(root);
3409 +       IMustLock(root_inode);
3410 +       IiMustWriteLock(root_inode);
3411 +       err = test_add(sb, add, remount);
3412 +       if (unlikely(err < 0))
3413 +               goto out;
3414 +       if (err) {
3415 +               err = 0;
3416 +               goto out; /* success */
3417 +       }
3418 +
3419 +       bbot = au_sbbot(sb);
3420 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3421 +       err = PTR_ERR(add_branch);
3422 +       if (IS_ERR(add_branch))
3423 +               goto out;
3424 +
3425 +       err = au_br_init(add_branch, sb, add);
3426 +       if (unlikely(err)) {
3427 +               au_br_do_free(add_branch);
3428 +               goto out;
3429 +       }
3430 +
3431 +       add_bindex = add->bindex;
3432 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3433 +       au_br_do_add(sb, add_branch, add_bindex);
3434 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3435 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3436 +
3437 +       h_dentry = add->path.dentry;
3438 +       if (!add_bindex) {
3439 +               au_cpup_attr_all(root_inode, /*force*/1);
3440 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3441 +       } else
3442 +               au_add_nlink(root_inode, d_inode(h_dentry));
3443 +
3444 +out:
3445 +       return err;
3446 +}
3447 +
3448 +/* ---------------------------------------------------------------------- */
3449 +
3450 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3451 +                                      unsigned long long max __maybe_unused,
3452 +                                      void *arg)
3453 +{
3454 +       unsigned long long n;
3455 +       struct file **p, *f;
3456 +       struct hlist_bl_head *files;
3457 +       struct hlist_bl_node *pos;
3458 +       struct au_finfo *finfo;
3459 +
3460 +       n = 0;
3461 +       p = a;
3462 +       files = &au_sbi(sb)->si_files;
3463 +       hlist_bl_lock(files);
3464 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3465 +               f = finfo->fi_file;
3466 +               if (file_count(f)
3467 +                   && !special_file(file_inode(f)->i_mode)) {
3468 +                       get_file(f);
3469 +                       *p++ = f;
3470 +                       n++;
3471 +                       AuDebugOn(n > max);
3472 +               }
3473 +       }
3474 +       hlist_bl_unlock(files);
3475 +
3476 +       return n;
3477 +}
3478 +
3479 +static struct file **au_farray_alloc(struct super_block *sb,
3480 +                                    unsigned long long *max)
3481 +{
3482 +       struct au_sbinfo *sbi;
3483 +
3484 +       sbi = au_sbi(sb);
3485 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3486 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3487 +}
3488 +
3489 +static void au_farray_free(struct file **a, unsigned long long max)
3490 +{
3491 +       unsigned long long ull;
3492 +
3493 +       for (ull = 0; ull < max; ull++)
3494 +               if (a[ull])
3495 +                       fput(a[ull]);
3496 +       kvfree(a);
3497 +}
3498 +
3499 +/* ---------------------------------------------------------------------- */
3500 +
3501 +/*
3502 + * delete a branch
3503 + */
3504 +
3505 +/* to show the line number, do not make it inlined function */
3506 +#define AuVerbose(do_info, fmt, ...) do { \
3507 +       if (do_info) \
3508 +               pr_info(fmt, ##__VA_ARGS__); \
3509 +} while (0)
3510 +
3511 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3512 +                        aufs_bindex_t bbot)
3513 +{
3514 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3515 +}
3516 +
3517 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3518 +                        aufs_bindex_t bbot)
3519 +{
3520 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3521 +}
3522 +
3523 +/*
3524 + * test if the branch is deletable or not.
3525 + */
3526 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3527 +                           unsigned int sigen, const unsigned int verbose)
3528 +{
3529 +       int err, i, j, ndentry;
3530 +       aufs_bindex_t btop, bbot;
3531 +       struct au_dcsub_pages dpages;
3532 +       struct au_dpage *dpage;
3533 +       struct dentry *d;
3534 +
3535 +       err = au_dpages_init(&dpages, GFP_NOFS);
3536 +       if (unlikely(err))
3537 +               goto out;
3538 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3539 +       if (unlikely(err))
3540 +               goto out_dpages;
3541 +
3542 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3543 +               dpage = dpages.dpages + i;
3544 +               ndentry = dpage->ndentry;
3545 +               for (j = 0; !err && j < ndentry; j++) {
3546 +                       d = dpage->dentries[j];
3547 +                       AuDebugOn(au_dcount(d) <= 0);
3548 +                       if (!au_digen_test(d, sigen)) {
3549 +                               di_read_lock_child(d, AuLock_IR);
3550 +                               if (unlikely(au_dbrange_test(d))) {
3551 +                                       di_read_unlock(d, AuLock_IR);
3552 +                                       continue;
3553 +                               }
3554 +                       } else {
3555 +                               di_write_lock_child(d);
3556 +                               if (unlikely(au_dbrange_test(d))) {
3557 +                                       di_write_unlock(d);
3558 +                                       continue;
3559 +                               }
3560 +                               err = au_reval_dpath(d, sigen);
3561 +                               if (!err)
3562 +                                       di_downgrade_lock(d, AuLock_IR);
3563 +                               else {
3564 +                                       di_write_unlock(d);
3565 +                                       break;
3566 +                               }
3567 +                       }
3568 +
3569 +                       /* AuDbgDentry(d); */
3570 +                       btop = au_dbtop(d);
3571 +                       bbot = au_dbbot(d);
3572 +                       if (btop <= bindex
3573 +                           && bindex <= bbot
3574 +                           && au_h_dptr(d, bindex)
3575 +                           && au_test_dbusy(d, btop, bbot)) {
3576 +                               err = -EBUSY;
3577 +                               AuVerbose(verbose, "busy %pd\n", d);
3578 +                               AuDbgDentry(d);
3579 +                       }
3580 +                       di_read_unlock(d, AuLock_IR);
3581 +               }
3582 +       }
3583 +
3584 +out_dpages:
3585 +       au_dpages_free(&dpages);
3586 +out:
3587 +       return err;
3588 +}
3589 +
3590 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3591 +                          unsigned int sigen, const unsigned int verbose)
3592 +{
3593 +       int err;
3594 +       unsigned long long max, ull;
3595 +       struct inode *i, **array;
3596 +       aufs_bindex_t btop, bbot;
3597 +
3598 +       array = au_iarray_alloc(sb, &max);
3599 +       err = PTR_ERR(array);
3600 +       if (IS_ERR(array))
3601 +               goto out;
3602 +
3603 +       err = 0;
3604 +       AuDbg("b%d\n", bindex);
3605 +       for (ull = 0; !err && ull < max; ull++) {
3606 +               i = array[ull];
3607 +               if (unlikely(!i))
3608 +                       break;
3609 +               if (i->i_ino == AUFS_ROOT_INO)
3610 +                       continue;
3611 +
3612 +               /* AuDbgInode(i); */
3613 +               if (au_iigen(i, NULL) == sigen)
3614 +                       ii_read_lock_child(i);
3615 +               else {
3616 +                       ii_write_lock_child(i);
3617 +                       err = au_refresh_hinode_self(i);
3618 +                       au_iigen_dec(i);
3619 +                       if (!err)
3620 +                               ii_downgrade_lock(i);
3621 +                       else {
3622 +                               ii_write_unlock(i);
3623 +                               break;
3624 +                       }
3625 +               }
3626 +
3627 +               btop = au_ibtop(i);
3628 +               bbot = au_ibbot(i);
3629 +               if (btop <= bindex
3630 +                   && bindex <= bbot
3631 +                   && au_h_iptr(i, bindex)
3632 +                   && au_test_ibusy(i, btop, bbot)) {
3633 +                       err = -EBUSY;
3634 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3635 +                       AuDbgInode(i);
3636 +               }
3637 +               ii_read_unlock(i);
3638 +       }
3639 +       au_iarray_free(array, max);
3640 +
3641 +out:
3642 +       return err;
3643 +}
3644 +
3645 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3646 +                             const unsigned int verbose)
3647 +{
3648 +       int err;
3649 +       unsigned int sigen;
3650 +
3651 +       sigen = au_sigen(root->d_sb);
3652 +       DiMustNoWaiters(root);
3653 +       IiMustNoWaiters(d_inode(root));
3654 +       di_write_unlock(root);
3655 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3656 +       if (!err)
3657 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3658 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3659 +
3660 +       return err;
3661 +}
3662 +
3663 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3664 +                        struct file **to_free, int *idx)
3665 +{
3666 +       int err;
3667 +       unsigned char matched, root;
3668 +       aufs_bindex_t bindex, bbot;
3669 +       struct au_fidir *fidir;
3670 +       struct au_hfile *hfile;
3671 +
3672 +       err = 0;
3673 +       root = IS_ROOT(file->f_path.dentry);
3674 +       if (root) {
3675 +               get_file(file);
3676 +               to_free[*idx] = file;
3677 +               (*idx)++;
3678 +               goto out;
3679 +       }
3680 +
3681 +       matched = 0;
3682 +       fidir = au_fi(file)->fi_hdir;
3683 +       AuDebugOn(!fidir);
3684 +       bbot = au_fbbot_dir(file);
3685 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3686 +               hfile = fidir->fd_hfile + bindex;
3687 +               if (!hfile->hf_file)
3688 +                       continue;
3689 +
3690 +               if (hfile->hf_br->br_id == br_id) {
3691 +                       matched = 1;
3692 +                       break;
3693 +               }
3694 +       }
3695 +       if (matched)
3696 +               err = -EBUSY;
3697 +
3698 +out:
3699 +       return err;
3700 +}
3701 +
3702 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3703 +                         struct file **to_free, int opened)
3704 +{
3705 +       int err, idx;
3706 +       unsigned long long ull, max;
3707 +       aufs_bindex_t btop;
3708 +       struct file *file, **array;
3709 +       struct dentry *root;
3710 +       struct au_hfile *hfile;
3711 +
3712 +       array = au_farray_alloc(sb, &max);
3713 +       err = PTR_ERR(array);
3714 +       if (IS_ERR(array))
3715 +               goto out;
3716 +
3717 +       err = 0;
3718 +       idx = 0;
3719 +       root = sb->s_root;
3720 +       di_write_unlock(root);
3721 +       for (ull = 0; ull < max; ull++) {
3722 +               file = array[ull];
3723 +               if (unlikely(!file))
3724 +                       break;
3725 +
3726 +               /* AuDbg("%pD\n", file); */
3727 +               fi_read_lock(file);
3728 +               btop = au_fbtop(file);
3729 +               if (!d_is_dir(file->f_path.dentry)) {
3730 +                       hfile = &au_fi(file)->fi_htop;
3731 +                       if (hfile->hf_br->br_id == br_id)
3732 +                               err = -EBUSY;
3733 +               } else
3734 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3735 +               fi_read_unlock(file);
3736 +               if (unlikely(err))
3737 +                       break;
3738 +       }
3739 +       di_write_lock_child(root);
3740 +       au_farray_free(array, max);
3741 +       AuDebugOn(idx > opened);
3742 +
3743 +out:
3744 +       return err;
3745 +}
3746 +
3747 +static void br_del_file(struct file **to_free, unsigned long long opened,
3748 +                       aufs_bindex_t br_id)
3749 +{
3750 +       unsigned long long ull;
3751 +       aufs_bindex_t bindex, btop, bbot, bfound;
3752 +       struct file *file;
3753 +       struct au_fidir *fidir;
3754 +       struct au_hfile *hfile;
3755 +
3756 +       for (ull = 0; ull < opened; ull++) {
3757 +               file = to_free[ull];
3758 +               if (unlikely(!file))
3759 +                       break;
3760 +
3761 +               /* AuDbg("%pD\n", file); */
3762 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3763 +               bfound = -1;
3764 +               fidir = au_fi(file)->fi_hdir;
3765 +               AuDebugOn(!fidir);
3766 +               fi_write_lock(file);
3767 +               btop = au_fbtop(file);
3768 +               bbot = au_fbbot_dir(file);
3769 +               for (bindex = btop; bindex <= bbot; bindex++) {
3770 +                       hfile = fidir->fd_hfile + bindex;
3771 +                       if (!hfile->hf_file)
3772 +                               continue;
3773 +
3774 +                       if (hfile->hf_br->br_id == br_id) {
3775 +                               bfound = bindex;
3776 +                               break;
3777 +                       }
3778 +               }
3779 +               AuDebugOn(bfound < 0);
3780 +               au_set_h_fptr(file, bfound, NULL);
3781 +               if (bfound == btop) {
3782 +                       for (btop++; btop <= bbot; btop++)
3783 +                               if (au_hf_dir(file, btop)) {
3784 +                                       au_set_fbtop(file, btop);
3785 +                                       break;
3786 +                               }
3787 +               }
3788 +               fi_write_unlock(file);
3789 +       }
3790 +}
3791 +
3792 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3793 +                            const aufs_bindex_t bindex,
3794 +                            const aufs_bindex_t bbot)
3795 +{
3796 +       struct au_branch **brp, **p;
3797 +
3798 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3799 +
3800 +       brp = sbinfo->si_branch + bindex;
3801 +       if (bindex < bbot)
3802 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3803 +       sbinfo->si_branch[0 + bbot] = NULL;
3804 +       sbinfo->si_bbot--;
3805 +
3806 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3807 +                       /*may_shrink*/1);
3808 +       if (p)
3809 +               sbinfo->si_branch = p;
3810 +       /* harmless error */
3811 +}
3812 +
3813 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3814 +                            const aufs_bindex_t bbot)
3815 +{
3816 +       struct au_hdentry *hdp, *p;
3817 +
3818 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3819 +
3820 +       hdp = au_hdentry(dinfo, bindex);
3821 +       if (bindex < bbot)
3822 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3823 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3824 +       dinfo->di_bbot--;
3825 +
3826 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3827 +                       /*may_shrink*/1);
3828 +       if (p)
3829 +               dinfo->di_hdentry = p;
3830 +       /* harmless error */
3831 +}
3832 +
3833 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3834 +                            const aufs_bindex_t bbot)
3835 +{
3836 +       struct au_hinode *hip, *p;
3837 +
3838 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3839 +
3840 +       hip = au_hinode(iinfo, bindex);
3841 +       if (bindex < bbot)
3842 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3843 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3844 +       iinfo->ii_bbot--;
3845 +
3846 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3847 +                       /*may_shrink*/1);
3848 +       if (p)
3849 +               iinfo->ii_hinode = p;
3850 +       /* harmless error */
3851 +}
3852 +
3853 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3854 +                        struct au_branch *br)
3855 +{
3856 +       aufs_bindex_t bbot;
3857 +       struct au_sbinfo *sbinfo;
3858 +       struct dentry *root, *h_root;
3859 +       struct inode *inode, *h_inode;
3860 +       struct au_hinode *hinode;
3861 +
3862 +       SiMustWriteLock(sb);
3863 +
3864 +       root = sb->s_root;
3865 +       inode = d_inode(root);
3866 +       sbinfo = au_sbi(sb);
3867 +       bbot = sbinfo->si_bbot;
3868 +
3869 +       h_root = au_h_dptr(root, bindex);
3870 +       hinode = au_hi(inode, bindex);
3871 +       h_inode = au_igrab(hinode->hi_inode);
3872 +       au_hiput(hinode);
3873 +
3874 +       au_sbilist_lock();
3875 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3876 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3877 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3878 +       au_sbilist_unlock();
3879 +
3880 +       /* ignore an error */
3881 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3882 +
3883 +       dput(h_root);
3884 +       iput(h_inode);
3885 +       au_br_do_free(br);
3886 +}
3887 +
3888 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3889 +                                  unsigned long long max, void *arg)
3890 +{
3891 +       return max;
3892 +}
3893 +
3894 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3895 +{
3896 +       int err, rerr, i;
3897 +       unsigned long long opened;
3898 +       unsigned int mnt_flags;
3899 +       aufs_bindex_t bindex, bbot, br_id;
3900 +       unsigned char do_wh, verbose;
3901 +       struct au_branch *br;
3902 +       struct au_wbr *wbr;
3903 +       struct dentry *root;
3904 +       struct file **to_free;
3905 +
3906 +       err = 0;
3907 +       opened = 0;
3908 +       to_free = NULL;
3909 +       root = sb->s_root;
3910 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3911 +       if (bindex < 0) {
3912 +               if (remount)
3913 +                       goto out; /* success */
3914 +               err = -ENOENT;
3915 +               pr_err("%s no such branch\n", del->pathname);
3916 +               goto out;
3917 +       }
3918 +       AuDbg("bindex b%d\n", bindex);
3919 +
3920 +       err = -EBUSY;
3921 +       mnt_flags = au_mntflags(sb);
3922 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3923 +       bbot = au_sbbot(sb);
3924 +       if (unlikely(!bbot)) {
3925 +               AuVerbose(verbose, "no more branches left\n");
3926 +               goto out;
3927 +       }
3928 +
3929 +       br = au_sbr(sb, bindex);
3930 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3931 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3932 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3933 +               goto out;
3934 +       }
3935 +
3936 +       br_id = br->br_id;
3937 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3938 +       if (unlikely(opened)) {
3939 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3940 +               err = PTR_ERR(to_free);
3941 +               if (IS_ERR(to_free))
3942 +                       goto out;
3943 +
3944 +               err = test_file_busy(sb, br_id, to_free, opened);
3945 +               if (unlikely(err)) {
3946 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3947 +                       goto out;
3948 +               }
3949 +       }
3950 +
3951 +       wbr = br->br_wbr;
3952 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3953 +       if (do_wh) {
3954 +               /* instead of WbrWhMustWriteLock(wbr) */
3955 +               SiMustWriteLock(sb);
3956 +               for (i = 0; i < AuBrWh_Last; i++) {
3957 +                       dput(wbr->wbr_wh[i]);
3958 +                       wbr->wbr_wh[i] = NULL;
3959 +               }
3960 +       }
3961 +
3962 +       err = test_children_busy(root, bindex, verbose);
3963 +       if (unlikely(err)) {
3964 +               if (do_wh)
3965 +                       goto out_wh;
3966 +               goto out;
3967 +       }
3968 +
3969 +       err = 0;
3970 +       if (to_free) {
3971 +               /*
3972 +                * now we confirmed the branch is deletable.
3973 +                * let's free the remaining opened dirs on the branch.
3974 +                */
3975 +               di_write_unlock(root);
3976 +               br_del_file(to_free, opened, br_id);
3977 +               di_write_lock_child(root);
3978 +       }
3979 +
3980 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3981 +       dbgaufs_xino_del(br);           /* remove one */
3982 +       au_br_do_del(sb, bindex, br);
3983 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3984 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
3985 +
3986 +       if (!bindex) {
3987 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3988 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3989 +       } else
3990 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3991 +       if (au_opt_test(mnt_flags, PLINK))
3992 +               au_plink_half_refresh(sb, br_id);
3993 +
3994 +       goto out; /* success */
3995 +
3996 +out_wh:
3997 +       /* revert */
3998 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3999 +       if (rerr)
4000 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4001 +                       del->pathname, rerr);
4002 +out:
4003 +       if (to_free)
4004 +               au_farray_free(to_free, opened);
4005 +       return err;
4006 +}
4007 +
4008 +/* ---------------------------------------------------------------------- */
4009 +
4010 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4011 +{
4012 +       int err;
4013 +       aufs_bindex_t btop, bbot;
4014 +       struct aufs_ibusy ibusy;
4015 +       struct inode *inode, *h_inode;
4016 +
4017 +       err = -EPERM;
4018 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4019 +               goto out;
4020 +
4021 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4022 +       if (!err)
4023 +               /* VERIFY_WRITE */
4024 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4025 +       if (unlikely(err)) {
4026 +               err = -EFAULT;
4027 +               AuTraceErr(err);
4028 +               goto out;
4029 +       }
4030 +
4031 +       err = -EINVAL;
4032 +       si_read_lock(sb, AuLock_FLUSH);
4033 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4034 +               goto out_unlock;
4035 +
4036 +       err = 0;
4037 +       ibusy.h_ino = 0; /* invalid */
4038 +       inode = ilookup(sb, ibusy.ino);
4039 +       if (!inode
4040 +           || inode->i_ino == AUFS_ROOT_INO
4041 +           || au_is_bad_inode(inode))
4042 +               goto out_unlock;
4043 +
4044 +       ii_read_lock_child(inode);
4045 +       btop = au_ibtop(inode);
4046 +       bbot = au_ibbot(inode);
4047 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4048 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4049 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4050 +                       ibusy.h_ino = h_inode->i_ino;
4051 +       }
4052 +       ii_read_unlock(inode);
4053 +       iput(inode);
4054 +
4055 +out_unlock:
4056 +       si_read_unlock(sb);
4057 +       if (!err) {
4058 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4059 +               if (unlikely(err)) {
4060 +                       err = -EFAULT;
4061 +                       AuTraceErr(err);
4062 +               }
4063 +       }
4064 +out:
4065 +       return err;
4066 +}
4067 +
4068 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4069 +{
4070 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4071 +}
4072 +
4073 +#ifdef CONFIG_COMPAT
4074 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4075 +{
4076 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4077 +}
4078 +#endif
4079 +
4080 +/* ---------------------------------------------------------------------- */
4081 +
4082 +/*
4083 + * change a branch permission
4084 + */
4085 +
4086 +static void au_warn_ima(void)
4087 +{
4088 +#ifdef CONFIG_IMA
4089 +       /* since it doesn't support mark_files_ro() */
4090 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4091 +#endif
4092 +}
4093 +
4094 +static int do_need_sigen_inc(int a, int b)
4095 +{
4096 +       return au_br_whable(a) && !au_br_whable(b);
4097 +}
4098 +
4099 +static int need_sigen_inc(int old, int new)
4100 +{
4101 +       return do_need_sigen_inc(old, new)
4102 +               || do_need_sigen_inc(new, old);
4103 +}
4104 +
4105 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4106 +{
4107 +       int err, do_warn;
4108 +       unsigned int mnt_flags;
4109 +       unsigned long long ull, max;
4110 +       aufs_bindex_t br_id;
4111 +       unsigned char verbose, writer;
4112 +       struct file *file, *hf, **array;
4113 +       struct au_hfile *hfile;
4114 +       struct inode *h_inode;
4115 +
4116 +       mnt_flags = au_mntflags(sb);
4117 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4118 +
4119 +       array = au_farray_alloc(sb, &max);
4120 +       err = PTR_ERR(array);
4121 +       if (IS_ERR(array))
4122 +               goto out;
4123 +
4124 +       do_warn = 0;
4125 +       br_id = au_sbr_id(sb, bindex);
4126 +       for (ull = 0; ull < max; ull++) {
4127 +               file = array[ull];
4128 +               if (unlikely(!file))
4129 +                       break;
4130 +
4131 +               /* AuDbg("%pD\n", file); */
4132 +               fi_read_lock(file);
4133 +               if (unlikely(au_test_mmapped(file))) {
4134 +                       err = -EBUSY;
4135 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4136 +                       AuDbgFile(file);
4137 +                       FiMustNoWaiters(file);
4138 +                       fi_read_unlock(file);
4139 +                       goto out_array;
4140 +               }
4141 +
4142 +               hfile = &au_fi(file)->fi_htop;
4143 +               hf = hfile->hf_file;
4144 +               if (!d_is_reg(file->f_path.dentry)
4145 +                   || !(file->f_mode & FMODE_WRITE)
4146 +                   || hfile->hf_br->br_id != br_id
4147 +                   || !(hf->f_mode & FMODE_WRITE))
4148 +                       array[ull] = NULL;
4149 +               else {
4150 +                       do_warn = 1;
4151 +                       get_file(file);
4152 +               }
4153 +
4154 +               FiMustNoWaiters(file);
4155 +               fi_read_unlock(file);
4156 +               fput(file);
4157 +       }
4158 +
4159 +       err = 0;
4160 +       if (do_warn)
4161 +               au_warn_ima();
4162 +
4163 +       for (ull = 0; ull < max; ull++) {
4164 +               file = array[ull];
4165 +               if (!file)
4166 +                       continue;
4167 +
4168 +               /* todo: already flushed? */
4169 +               /*
4170 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4171 +                * approach which resets f_mode and calls mnt_drop_write() and
4172 +                * file_release_write() for each file, because the branch
4173 +                * attribute in aufs world is totally different from the native
4174 +                * fs rw/ro mode.
4175 +                */
4176 +               /* fi_read_lock(file); */
4177 +               hfile = &au_fi(file)->fi_htop;
4178 +               hf = hfile->hf_file;
4179 +               /* fi_read_unlock(file); */
4180 +               spin_lock(&hf->f_lock);
4181 +               writer = !!(hf->f_mode & FMODE_WRITER);
4182 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4183 +               spin_unlock(&hf->f_lock);
4184 +               if (writer) {
4185 +                       h_inode = file_inode(hf);
4186 +                       if (hf->f_mode & FMODE_READ)
4187 +                               i_readcount_inc(h_inode);
4188 +                       put_write_access(h_inode);
4189 +                       __mnt_drop_write(hf->f_path.mnt);
4190 +               }
4191 +       }
4192 +
4193 +out_array:
4194 +       au_farray_free(array, max);
4195 +out:
4196 +       AuTraceErr(err);
4197 +       return err;
4198 +}
4199 +
4200 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4201 +             int *do_refresh)
4202 +{
4203 +       int err, rerr;
4204 +       aufs_bindex_t bindex;
4205 +       struct dentry *root;
4206 +       struct au_branch *br;
4207 +       struct au_br_fhsm *bf;
4208 +
4209 +       root = sb->s_root;
4210 +       bindex = au_find_dbindex(root, mod->h_root);
4211 +       if (bindex < 0) {
4212 +               if (remount)
4213 +                       return 0; /* success */
4214 +               err = -ENOENT;
4215 +               pr_err("%s no such branch\n", mod->path);
4216 +               goto out;
4217 +       }
4218 +       AuDbg("bindex b%d\n", bindex);
4219 +
4220 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4221 +       if (unlikely(err))
4222 +               goto out;
4223 +
4224 +       br = au_sbr(sb, bindex);
4225 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4226 +       if (br->br_perm == mod->perm)
4227 +               return 0; /* success */
4228 +
4229 +       /* pre-allocate for non-fhsm --> fhsm */
4230 +       bf = NULL;
4231 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4232 +               err = au_fhsm_br_alloc(br);
4233 +               if (unlikely(err))
4234 +                       goto out;
4235 +               bf = br->br_fhsm;
4236 +               br->br_fhsm = NULL;
4237 +       }
4238 +
4239 +       if (au_br_writable(br->br_perm)) {
4240 +               /* remove whiteout base */
4241 +               err = au_br_init_wh(sb, br, mod->perm);
4242 +               if (unlikely(err))
4243 +                       goto out_bf;
4244 +
4245 +               if (!au_br_writable(mod->perm)) {
4246 +                       /* rw --> ro, file might be mmapped */
4247 +                       DiMustNoWaiters(root);
4248 +                       IiMustNoWaiters(d_inode(root));
4249 +                       di_write_unlock(root);
4250 +                       err = au_br_mod_files_ro(sb, bindex);
4251 +                       /* aufs_write_lock() calls ..._child() */
4252 +                       di_write_lock_child(root);
4253 +
4254 +                       if (unlikely(err)) {
4255 +                               rerr = -ENOMEM;
4256 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4257 +                                                    GFP_NOFS);
4258 +                               if (br->br_wbr)
4259 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4260 +                               if (unlikely(rerr)) {
4261 +                                       AuIOErr("nested error %d (%d)\n",
4262 +                                               rerr, err);
4263 +                                       br->br_perm = mod->perm;
4264 +                               }
4265 +                       }
4266 +               }
4267 +       } else if (au_br_writable(mod->perm)) {
4268 +               /* ro --> rw */
4269 +               err = -ENOMEM;
4270 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4271 +               if (br->br_wbr) {
4272 +                       err = au_wbr_init(br, sb, mod->perm);
4273 +                       if (unlikely(err)) {
4274 +                               au_kfree_rcu(br->br_wbr);
4275 +                               br->br_wbr = NULL;
4276 +                       }
4277 +               }
4278 +       }
4279 +       if (unlikely(err))
4280 +               goto out_bf;
4281 +
4282 +       if (au_br_fhsm(br->br_perm)) {
4283 +               if (!au_br_fhsm(mod->perm)) {
4284 +                       /* fhsm --> non-fhsm */
4285 +                       au_br_fhsm_fin(br->br_fhsm);
4286 +                       au_kfree_rcu(br->br_fhsm);
4287 +                       br->br_fhsm = NULL;
4288 +               }
4289 +       } else if (au_br_fhsm(mod->perm))
4290 +               /* non-fhsm --> fhsm */
4291 +               br->br_fhsm = bf;
4292 +
4293 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4294 +       br->br_perm = mod->perm;
4295 +       goto out; /* success */
4296 +
4297 +out_bf:
4298 +       au_kfree_try_rcu(bf);
4299 +out:
4300 +       AuTraceErr(err);
4301 +       return err;
4302 +}
4303 +
4304 +/* ---------------------------------------------------------------------- */
4305 +
4306 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4307 +{
4308 +       int err;
4309 +       struct kstatfs kstfs;
4310 +
4311 +       err = vfs_statfs(&br->br_path, &kstfs);
4312 +       if (!err) {
4313 +               stfs->f_blocks = kstfs.f_blocks;
4314 +               stfs->f_bavail = kstfs.f_bavail;
4315 +               stfs->f_files = kstfs.f_files;
4316 +               stfs->f_ffree = kstfs.f_ffree;
4317 +       }
4318 +
4319 +       return err;
4320 +}
4321 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4322 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4323 +++ linux/fs/aufs/branch.h      2023-09-03 02:21:58.856637674 +0200
4324 @@ -0,0 +1,375 @@
4325 +/* SPDX-License-Identifier: GPL-2.0 */
4326 +/*
4327 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4328 + *
4329 + * This program is free software; you can redistribute it and/or modify
4330 + * it under the terms of the GNU General Public License as published by
4331 + * the Free Software Foundation; either version 2 of the License, or
4332 + * (at your option) any later version.
4333 + *
4334 + * This program is distributed in the hope that it will be useful,
4335 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4336 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4337 + * GNU General Public License for more details.
4338 + *
4339 + * You should have received a copy of the GNU General Public License
4340 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4341 + */
4342 +
4343 +/*
4344 + * branch filesystems and xino for them
4345 + */
4346 +
4347 +#ifndef __AUFS_BRANCH_H__
4348 +#define __AUFS_BRANCH_H__
4349 +
4350 +#ifdef __KERNEL__
4351 +
4352 +#include <linux/mount.h>
4353 +#include "dirren.h"
4354 +#include "dynop.h"
4355 +#include "lcnt.h"
4356 +#include "rwsem.h"
4357 +#include "super.h"
4358 +
4359 +/* ---------------------------------------------------------------------- */
4360 +
4361 +/* a xino file */
4362 +struct au_xino {
4363 +       struct file             **xi_file;
4364 +       unsigned int            xi_nfile;
4365 +
4366 +       struct {
4367 +               spinlock_t              spin;
4368 +               ino_t                   *array;
4369 +               int                     total;
4370 +               /* reserved for future use */
4371 +               /* unsigned long        *bitmap; */
4372 +               wait_queue_head_t       wqh;
4373 +       } xi_nondir;
4374 +
4375 +       struct mutex            xi_mtx; /* protects xi_file array */
4376 +       struct hlist_bl_head    xi_writing;
4377 +
4378 +       atomic_t                xi_truncating;
4379 +
4380 +       struct kref             xi_kref;
4381 +};
4382 +
4383 +/* File-based Hierarchical Storage Management */
4384 +struct au_br_fhsm {
4385 +#ifdef CONFIG_AUFS_FHSM
4386 +       struct mutex            bf_lock;
4387 +       unsigned long           bf_jiffy;
4388 +       struct aufs_stfs        bf_stfs;
4389 +       int                     bf_readable;
4390 +#endif
4391 +};
4392 +
4393 +/* members for writable branch only */
4394 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4395 +struct au_wbr {
4396 +       struct au_rwsem         wbr_wh_rwsem;
4397 +       struct dentry           *wbr_wh[AuBrWh_Last];
4398 +       atomic_t                wbr_wh_running;
4399 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4400 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4401 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4402 +
4403 +       /* mfs mode */
4404 +       unsigned long long      wbr_bytes;
4405 +};
4406 +
4407 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4408 +#define AuBrDynOp (AuDyLast * 4)
4409 +
4410 +#ifdef CONFIG_AUFS_HFSNOTIFY
4411 +/* support for asynchronous destruction */
4412 +struct au_br_hfsnotify {
4413 +       struct fsnotify_group   *hfsn_group;
4414 +};
4415 +#endif
4416 +
4417 +/* sysfs entries */
4418 +struct au_brsysfs {
4419 +       char                    name[16];
4420 +       struct attribute        attr;
4421 +};
4422 +
4423 +enum {
4424 +       AuBrSysfs_BR,
4425 +       AuBrSysfs_BRID,
4426 +       AuBrSysfs_Last
4427 +};
4428 +
4429 +/* protected by superblock rwsem */
4430 +struct au_branch {
4431 +       struct au_xino          *br_xino;
4432 +
4433 +       aufs_bindex_t           br_id;
4434 +
4435 +       int                     br_perm;
4436 +       struct path             br_path;
4437 +       spinlock_t              br_dykey_lock;
4438 +       struct au_dykey         *br_dykey[AuBrDynOp];
4439 +       au_lcnt_t               br_nfiles;      /* opened files */
4440 +       au_lcnt_t               br_count;       /* in-use for other */
4441 +
4442 +       struct au_wbr           *br_wbr;
4443 +       struct au_br_fhsm       *br_fhsm;
4444 +
4445 +#ifdef CONFIG_AUFS_HFSNOTIFY
4446 +       struct au_br_hfsnotify  *br_hfsn;
4447 +#endif
4448 +
4449 +#ifdef CONFIG_SYSFS
4450 +       /* entries under sysfs per mount-point */
4451 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4452 +#endif
4453 +
4454 +#ifdef CONFIG_DEBUG_FS
4455 +       struct dentry            *br_dbgaufs; /* xino */
4456 +#endif
4457 +
4458 +       struct au_dr_br         br_dirren;
4459 +};
4460 +
4461 +/* ---------------------------------------------------------------------- */
4462 +
4463 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4464 +{
4465 +       return br->br_path.mnt;
4466 +}
4467 +
4468 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4469 +{
4470 +       return br->br_path.dentry;
4471 +}
4472 +
4473 +static inline struct mnt_idmap *au_br_idmap(struct au_branch *br)
4474 +{
4475 +       return mnt_idmap(br->br_path.mnt);
4476 +}
4477 +
4478 +static inline struct super_block *au_br_sb(struct au_branch *br)
4479 +{
4480 +       return au_br_mnt(br)->mnt_sb;
4481 +}
4482 +
4483 +static inline int au_br_rdonly(struct au_branch *br)
4484 +{
4485 +       return (sb_rdonly(au_br_sb(br))
4486 +               || !au_br_writable(br->br_perm))
4487 +               ? -EROFS : 0;
4488 +}
4489 +
4490 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4491 +{
4492 +#ifdef CONFIG_AUFS_HNOTIFY
4493 +       return !(brperm & AuBrPerm_RR);
4494 +#else
4495 +       return 0;
4496 +#endif
4497 +}
4498 +
4499 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4500 +{
4501 +       int err, exec_flag;
4502 +
4503 +       err = 0;
4504 +       exec_flag = oflag & __FMODE_EXEC;
4505 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4506 +               err = -EACCES;
4507 +
4508 +       return err;
4509 +}
4510 +
4511 +static inline void au_xino_get(struct au_branch *br)
4512 +{
4513 +       struct au_xino *xi;
4514 +
4515 +       xi = br->br_xino;
4516 +       if (xi)
4517 +               kref_get(&xi->xi_kref);
4518 +}
4519 +
4520 +static inline int au_xino_count(struct au_branch *br)
4521 +{
4522 +       int v;
4523 +       struct au_xino *xi;
4524 +
4525 +       v = 0;
4526 +       xi = br->br_xino;
4527 +       if (xi)
4528 +               v = kref_read(&xi->xi_kref);
4529 +
4530 +       return v;
4531 +}
4532 +
4533 +/* ---------------------------------------------------------------------- */
4534 +
4535 +/* branch.c */
4536 +struct au_sbinfo;
4537 +void au_br_free(struct au_sbinfo *sinfo);
4538 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4539 +struct au_opt_add;
4540 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4541 +struct au_opt_del;
4542 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4543 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4544 +#ifdef CONFIG_COMPAT
4545 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4546 +#endif
4547 +struct au_opt_mod;
4548 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4549 +             int *do_refresh);
4550 +struct aufs_stfs;
4551 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4552 +
4553 +/* xino.c */
4554 +static const loff_t au_loff_max = LLONG_MAX;
4555 +
4556 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4557 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4558 +                           int wbrtop);
4559 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4560 +                            struct file *copy_src);
4561 +struct au_xi_new {
4562 +       struct au_xino *xi;     /* switch between xino and xigen */
4563 +       int idx;
4564 +       struct path *base;
4565 +       struct file *copy_src;
4566 +};
4567 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4568 +
4569 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4570 +                ino_t *ino);
4571 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4572 +                 ino_t ino);
4573 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4574 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4575 +
4576 +int au_xib_trunc(struct super_block *sb);
4577 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4578 +
4579 +struct au_xino *au_xino_alloc(unsigned int nfile);
4580 +int au_xino_put(struct au_branch *br);
4581 +struct file *au_xino_file1(struct au_xino *xi);
4582 +
4583 +struct au_opt_xino;
4584 +void au_xino_clr(struct super_block *sb);
4585 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4586 +struct file *au_xino_def(struct super_block *sb);
4587 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4588 +                   struct path *base);
4589 +
4590 +ino_t au_xino_new_ino(struct super_block *sb);
4591 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4592 +
4593 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4594 +                      ino_t h_ino, int idx);
4595 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4596 +                     int *idx);
4597 +
4598 +int au_xino_path(struct seq_file *seq, struct file *file);
4599 +
4600 +/* ---------------------------------------------------------------------- */
4601 +
4602 +/* @idx is signed to accept -1 meaning the first file */
4603 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4604 +{
4605 +       struct file *file;
4606 +
4607 +       file = NULL;
4608 +       if (!xi)
4609 +               goto out;
4610 +
4611 +       if (idx >= 0) {
4612 +               if (idx < xi->xi_nfile)
4613 +                       file = xi->xi_file[idx];
4614 +       } else
4615 +               file = au_xino_file1(xi);
4616 +
4617 +out:
4618 +       return file;
4619 +}
4620 +
4621 +/* ---------------------------------------------------------------------- */
4622 +
4623 +/* Superblock to branch */
4624 +static inline
4625 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4626 +{
4627 +       return au_sbr(sb, bindex)->br_id;
4628 +}
4629 +
4630 +static inline
4631 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4632 +{
4633 +       return au_br_mnt(au_sbr(sb, bindex));
4634 +}
4635 +
4636 +static inline
4637 +struct mnt_idmap *au_sbr_idmap(struct super_block *sb, aufs_bindex_t bindex)
4638 +{
4639 +       return au_br_idmap(au_sbr(sb, bindex));
4640 +}
4641 +
4642 +static inline
4643 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4644 +{
4645 +       return au_br_sb(au_sbr(sb, bindex));
4646 +}
4647 +
4648 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4649 +{
4650 +       return au_sbr(sb, bindex)->br_perm;
4651 +}
4652 +
4653 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4654 +{
4655 +       return au_br_whable(au_sbr_perm(sb, bindex));
4656 +}
4657 +
4658 +/* ---------------------------------------------------------------------- */
4659 +
4660 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4661 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4662 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4663 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4664 +/*
4665 +#define wbr_wh_read_trylock_nested(wbr) \
4666 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4667 +#define wbr_wh_write_trylock_nested(wbr) \
4668 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4669 +*/
4670 +
4671 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4672 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4673 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4674 +
4675 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4676 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4677 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4678 +
4679 +/* ---------------------------------------------------------------------- */
4680 +
4681 +#ifdef CONFIG_AUFS_FHSM
4682 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4683 +{
4684 +       mutex_init(&brfhsm->bf_lock);
4685 +       brfhsm->bf_jiffy = 0;
4686 +       brfhsm->bf_readable = 0;
4687 +}
4688 +
4689 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4690 +{
4691 +       mutex_destroy(&brfhsm->bf_lock);
4692 +}
4693 +#else
4694 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4695 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4696 +#endif
4697 +
4698 +#endif /* __KERNEL__ */
4699 +#endif /* __AUFS_BRANCH_H__ */
4700 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4701 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4702 +++ linux/fs/aufs/conf.mk       2022-11-05 23:02:18.959222617 +0100
4703 @@ -0,0 +1,40 @@
4704 +# SPDX-License-Identifier: GPL-2.0
4705 +
4706 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4707 +
4708 +define AuConf
4709 +ifdef ${1}
4710 +AuConfStr += ${1}=${${1}}
4711 +endif
4712 +endef
4713 +
4714 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4715 +       SBILIST \
4716 +       HNOTIFY HFSNOTIFY \
4717 +       EXPORT INO_T_64 \
4718 +       XATTR \
4719 +       FHSM \
4720 +       RDU \
4721 +       DIRREN \
4722 +       SHWH \
4723 +       BR_RAMFS \
4724 +       BR_FUSE POLL \
4725 +       BR_HFSPLUS \
4726 +       BDEV_LOOP \
4727 +       DEBUG MAGIC_SYSRQ
4728 +$(foreach i, ${AuConfAll}, \
4729 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4730 +
4731 +AuConfName = ${obj}/conf.str
4732 +${AuConfName}.tmp: FORCE
4733 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4734 +${AuConfName}: ${AuConfName}.tmp
4735 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4736 +       echo '  GEN    ' $@; \
4737 +       cp -p $< $@; \
4738 +       }
4739 +FORCE:
4740 +clean-files += ${AuConfName} ${AuConfName}.tmp
4741 +${obj}/sysfs.o: ${AuConfName}
4742 +
4743 +-include ${srctree}/${src}/conf_priv.mk
4744 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4745 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4746 +++ linux/fs/aufs/cpup.c        2023-09-03 02:21:58.859971007 +0200
4747 @@ -0,0 +1,1459 @@
4748 +// SPDX-License-Identifier: GPL-2.0
4749 +/*
4750 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4751 + *
4752 + * This program is free software; you can redistribute it and/or modify
4753 + * it under the terms of the GNU General Public License as published by
4754 + * the Free Software Foundation; either version 2 of the License, or
4755 + * (at your option) any later version.
4756 + *
4757 + * This program is distributed in the hope that it will be useful,
4758 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4759 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4760 + * GNU General Public License for more details.
4761 + *
4762 + * You should have received a copy of the GNU General Public License
4763 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4764 + */
4765 +
4766 +/*
4767 + * copy-up functions, see wbr_policy.c for copy-down
4768 + */
4769 +
4770 +#include <linux/fs_stack.h>
4771 +#include <linux/mm.h>
4772 +#include <linux/task_work.h>
4773 +#include "aufs.h"
4774 +
4775 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4776 +{
4777 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4778 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4779 +
4780 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4781 +
4782 +       dst->i_flags |= iflags & ~mask;
4783 +       if (au_test_fs_notime(dst->i_sb))
4784 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4785 +}
4786 +
4787 +void au_cpup_attr_timesizes(struct inode *inode)
4788 +{
4789 +       struct inode *h_inode;
4790 +
4791 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4792 +       fsstack_copy_attr_times(inode, h_inode);
4793 +       fsstack_copy_inode_size(inode, h_inode);
4794 +}
4795 +
4796 +void au_cpup_attr_nlink(struct inode *inode, int force)
4797 +{
4798 +       struct inode *h_inode;
4799 +       struct super_block *sb;
4800 +       aufs_bindex_t bindex, bbot;
4801 +
4802 +       sb = inode->i_sb;
4803 +       bindex = au_ibtop(inode);
4804 +       h_inode = au_h_iptr(inode, bindex);
4805 +       if (!force
4806 +           && !S_ISDIR(h_inode->i_mode)
4807 +           && au_opt_test(au_mntflags(sb), PLINK)
4808 +           && au_plink_test(inode))
4809 +               return;
4810 +
4811 +       /*
4812 +        * 0 can happen in revalidating.
4813 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4814 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4815 +        * case.
4816 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4817 +        *       the incorrect link count.
4818 +        */
4819 +       set_nlink(inode, h_inode->i_nlink);
4820 +
4821 +       /*
4822 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4823 +        * it may includes whplink directory.
4824 +        */
4825 +       if (S_ISDIR(h_inode->i_mode)) {
4826 +               bbot = au_ibbot(inode);
4827 +               for (bindex++; bindex <= bbot; bindex++) {
4828 +                       h_inode = au_h_iptr(inode, bindex);
4829 +                       if (h_inode)
4830 +                               au_add_nlink(inode, h_inode);
4831 +               }
4832 +       }
4833 +}
4834 +
4835 +void au_cpup_attr_changeable(struct inode *inode)
4836 +{
4837 +       struct inode *h_inode;
4838 +
4839 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4840 +       inode->i_mode = h_inode->i_mode;
4841 +       inode->i_uid = h_inode->i_uid;
4842 +       inode->i_gid = h_inode->i_gid;
4843 +       au_cpup_attr_timesizes(inode);
4844 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4845 +}
4846 +
4847 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4848 +{
4849 +       struct au_iinfo *iinfo = au_ii(inode);
4850 +
4851 +       IiMustWriteLock(inode);
4852 +
4853 +       iinfo->ii_higen = h_inode->i_generation;
4854 +       iinfo->ii_hsb1 = h_inode->i_sb;
4855 +}
4856 +
4857 +void au_cpup_attr_all(struct inode *inode, int force)
4858 +{
4859 +       struct inode *h_inode;
4860 +
4861 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4862 +       au_cpup_attr_changeable(inode);
4863 +       if (inode->i_nlink > 0)
4864 +               au_cpup_attr_nlink(inode, force);
4865 +       inode->i_rdev = h_inode->i_rdev;
4866 +       inode->i_blkbits = h_inode->i_blkbits;
4867 +       au_cpup_igen(inode, h_inode);
4868 +}
4869 +
4870 +/* ---------------------------------------------------------------------- */
4871 +
4872 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4873 +
4874 +/* keep the timestamps of the parent dir when cpup */
4875 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4876 +                   struct path *h_path)
4877 +{
4878 +       struct inode *h_inode;
4879 +
4880 +       dt->dt_dentry = dentry;
4881 +       dt->dt_h_path = *h_path;
4882 +       h_inode = d_inode(h_path->dentry);
4883 +       dt->dt_atime = h_inode->i_atime;
4884 +       dt->dt_mtime = h_inode->i_mtime;
4885 +       /* smp_mb(); */
4886 +}
4887 +
4888 +void au_dtime_revert(struct au_dtime *dt)
4889 +{
4890 +       struct iattr attr;
4891 +       int err;
4892 +
4893 +       attr.ia_atime = dt->dt_atime;
4894 +       attr.ia_mtime = dt->dt_mtime;
4895 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4896 +               | ATTR_ATIME | ATTR_ATIME_SET;
4897 +
4898 +       /* no delegation since this is a directory */
4899 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4900 +       if (unlikely(err))
4901 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4902 +}
4903 +
4904 +/* ---------------------------------------------------------------------- */
4905 +
4906 +/* internal use only */
4907 +struct au_cpup_reg_attr {
4908 +       int             valid;
4909 +       struct kstat    st;
4910 +       unsigned int    iflags; /* inode->i_flags */
4911 +};
4912 +
4913 +static noinline_for_stack
4914 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4915 +              struct au_cpup_reg_attr *h_src_attr)
4916 +{
4917 +       int err, sbits, icex;
4918 +       unsigned int mnt_flags;
4919 +       unsigned char verbose;
4920 +       struct iattr ia;
4921 +       struct path h_path;
4922 +       struct inode *h_isrc, *h_idst;
4923 +       struct kstat *h_st;
4924 +       struct au_branch *br;
4925 +
4926 +       br = au_sbr(dst->d_sb, bindex);
4927 +       h_path.mnt = au_br_mnt(br);
4928 +       h_path.dentry = au_h_dptr(dst, bindex);
4929 +       h_idst = d_inode(h_path.dentry);
4930 +       h_isrc = d_inode(h_src->dentry);
4931 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4932 +               | ATTR_ATIME | ATTR_MTIME
4933 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4934 +       if (h_src_attr && h_src_attr->valid) {
4935 +               h_st = &h_src_attr->st;
4936 +               ia.ia_uid = h_st->uid;
4937 +               ia.ia_gid = h_st->gid;
4938 +               ia.ia_atime = h_st->atime;
4939 +               ia.ia_mtime = h_st->mtime;
4940 +               if (h_idst->i_mode != h_st->mode
4941 +                   && !S_ISLNK(h_idst->i_mode)) {
4942 +                       ia.ia_valid |= ATTR_MODE;
4943 +                       ia.ia_mode = h_st->mode;
4944 +               }
4945 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4946 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4947 +       } else {
4948 +               ia.ia_uid = h_isrc->i_uid;
4949 +               ia.ia_gid = h_isrc->i_gid;
4950 +               ia.ia_atime = h_isrc->i_atime;
4951 +               ia.ia_mtime = h_isrc->i_mtime;
4952 +               if (h_idst->i_mode != h_isrc->i_mode
4953 +                   && !S_ISLNK(h_idst->i_mode)) {
4954 +                       ia.ia_valid |= ATTR_MODE;
4955 +                       ia.ia_mode = h_isrc->i_mode;
4956 +               }
4957 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4958 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4959 +       }
4960 +       /* no delegation since it is just created */
4961 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4962 +
4963 +       /* is this nfs only? */
4964 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4965 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4966 +               ia.ia_mode = h_isrc->i_mode;
4967 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4968 +       }
4969 +
4970 +       icex = br->br_perm & AuBrAttr_ICEX;
4971 +       if (!err) {
4972 +               mnt_flags = au_mntflags(dst->d_sb);
4973 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4974 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4975 +       }
4976 +
4977 +       return err;
4978 +}
4979 +
4980 +/* ---------------------------------------------------------------------- */
4981 +
4982 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4983 +                          char *buf, unsigned long blksize)
4984 +{
4985 +       int err;
4986 +       size_t sz, rbytes, wbytes;
4987 +       unsigned char all_zero;
4988 +       char *p, *zp;
4989 +       struct inode *h_inode;
4990 +       /* reduce stack usage */
4991 +       struct iattr *ia;
4992 +
4993 +       zp = page_address(ZERO_PAGE(0));
4994 +       if (unlikely(!zp))
4995 +               return -ENOMEM; /* possible? */
4996 +
4997 +       err = 0;
4998 +       all_zero = 0;
4999 +       while (len) {
5000 +               AuDbg("len %lld\n", len);
5001 +               sz = blksize;
5002 +               if (len < blksize)
5003 +                       sz = len;
5004 +
5005 +               rbytes = 0;
5006 +               /* todo: signal_pending? */
5007 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5008 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5009 +                       err = rbytes;
5010 +               }
5011 +               if (unlikely(err < 0))
5012 +                       break;
5013 +
5014 +               all_zero = 0;
5015 +               if (len >= rbytes && rbytes == blksize)
5016 +                       all_zero = !memcmp(buf, zp, rbytes);
5017 +               if (!all_zero) {
5018 +                       wbytes = rbytes;
5019 +                       p = buf;
5020 +                       while (wbytes) {
5021 +                               size_t b;
5022 +
5023 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5024 +                               err = b;
5025 +                               /* todo: signal_pending? */
5026 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5027 +                                       continue;
5028 +                               if (unlikely(err < 0))
5029 +                                       break;
5030 +                               wbytes -= b;
5031 +                               p += b;
5032 +                       }
5033 +                       if (unlikely(err < 0))
5034 +                               break;
5035 +               } else {
5036 +                       loff_t res;
5037 +
5038 +                       AuLabel(hole);
5039 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5040 +                       err = res;
5041 +                       if (unlikely(res < 0))
5042 +                               break;
5043 +               }
5044 +               len -= rbytes;
5045 +               err = 0;
5046 +       }
5047 +
5048 +       /* the last block may be a hole */
5049 +       if (!err && all_zero) {
5050 +               AuLabel(last hole);
5051 +
5052 +               err = 1;
5053 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5054 +                       /* nfs requires this step to make last hole */
5055 +                       /* is this only nfs? */
5056 +                       do {
5057 +                               /* todo: signal_pending? */
5058 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5059 +                       } while (err == -EAGAIN || err == -EINTR);
5060 +                       if (err == 1)
5061 +                               dst->f_pos--;
5062 +               }
5063 +
5064 +               if (err == 1) {
5065 +                       ia = (void *)buf;
5066 +                       ia->ia_size = dst->f_pos;
5067 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5068 +                       ia->ia_file = dst;
5069 +                       h_inode = file_inode(dst);
5070 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5071 +                       /* no delegation since it is just created */
5072 +                       err = vfsub_notify_change(&dst->f_path, ia,
5073 +                                                 /*delegated*/NULL);
5074 +                       inode_unlock(h_inode);
5075 +               }
5076 +       }
5077 +
5078 +       return err;
5079 +}
5080 +
5081 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5082 +{
5083 +       int err;
5084 +       unsigned long blksize;
5085 +       unsigned char do_kfree;
5086 +       char *buf;
5087 +       struct super_block *h_sb;
5088 +
5089 +       err = -ENOMEM;
5090 +       h_sb = file_inode(dst)->i_sb;
5091 +       blksize = h_sb->s_blocksize;
5092 +       if (!blksize || PAGE_SIZE < blksize)
5093 +               blksize = PAGE_SIZE;
5094 +       AuDbg("blksize %lu\n", blksize);
5095 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5096 +       if (do_kfree)
5097 +               buf = kmalloc(blksize, GFP_NOFS);
5098 +       else
5099 +               buf = (void *)__get_free_page(GFP_NOFS);
5100 +       if (unlikely(!buf))
5101 +               goto out;
5102 +
5103 +       if (len > (1 << 22))
5104 +               AuDbg("copying a large file %lld\n", (long long)len);
5105 +
5106 +       src->f_pos = 0;
5107 +       dst->f_pos = 0;
5108 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5109 +       if (do_kfree) {
5110 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5111 +               au_kfree_do_rcu(buf);
5112 +       } else
5113 +               free_page((unsigned long)buf);
5114 +
5115 +out:
5116 +       return err;
5117 +}
5118 +
5119 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5120 +{
5121 +       int err;
5122 +       struct super_block *h_src_sb;
5123 +       struct inode *h_src_inode;
5124 +
5125 +       h_src_inode = file_inode(src);
5126 +       h_src_sb = h_src_inode->i_sb;
5127 +
5128 +       /* XFS acquires inode_lock */
5129 +       if (!au_test_xfs(h_src_sb))
5130 +               err = au_copy_file(dst, src, len);
5131 +       else {
5132 +               inode_unlock_shared(h_src_inode);
5133 +               err = au_copy_file(dst, src, len);
5134 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5135 +       }
5136 +
5137 +       return err;
5138 +}
5139 +
5140 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5141 +{
5142 +       int err;
5143 +       loff_t lo;
5144 +       struct super_block *h_src_sb;
5145 +       struct inode *h_src_inode;
5146 +
5147 +       h_src_inode = file_inode(src);
5148 +       h_src_sb = h_src_inode->i_sb;
5149 +       if (h_src_sb != file_inode(dst)->i_sb
5150 +           || !dst->f_op->remap_file_range) {
5151 +               err = au_do_copy(dst, src, len);
5152 +               goto out;
5153 +       }
5154 +
5155 +       if (!au_test_nfs(h_src_sb)) {
5156 +               inode_unlock_shared(h_src_inode);
5157 +               lo = vfsub_clone_file_range(src, dst, len);
5158 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5159 +       } else
5160 +               lo = vfsub_clone_file_range(src, dst, len);
5161 +       if (lo == len) {
5162 +               err = 0;
5163 +               goto out; /* success */
5164 +       } else if (lo >= 0)
5165 +               /* todo: possible? */
5166 +               /* paritially succeeded */
5167 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5168 +       else if (lo != -EOPNOTSUPP) {
5169 +               /* older XFS has a condition in cloning */
5170 +               err = lo;
5171 +               goto out;
5172 +       }
5173 +
5174 +       /* the backend fs on NFS may not support cloning */
5175 +       err = au_do_copy(dst, src, len);
5176 +
5177 +out:
5178 +       AuTraceErr(err);
5179 +       return err;
5180 +}
5181 +
5182 +/*
5183 + * to support a sparse file which is opened with O_APPEND,
5184 + * we need to close the file.
5185 + */
5186 +static int au_cp_regular(struct au_cp_generic *cpg)
5187 +{
5188 +       int err, i;
5189 +       enum { SRC, DST };
5190 +       struct {
5191 +               aufs_bindex_t bindex;
5192 +               unsigned int flags;
5193 +               struct dentry *dentry;
5194 +               int force_wr;
5195 +               struct file *file;
5196 +       } *f, file[] = {
5197 +               {
5198 +                       .bindex = cpg->bsrc,
5199 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5200 +               },
5201 +               {
5202 +                       .bindex = cpg->bdst,
5203 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5204 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5205 +               }
5206 +       };
5207 +       struct au_branch *br;
5208 +       struct super_block *sb, *h_src_sb;
5209 +       struct inode *h_src_inode;
5210 +       struct task_struct *tsk = current;
5211 +
5212 +       /* bsrc branch can be ro/rw. */
5213 +       sb = cpg->dentry->d_sb;
5214 +       f = file;
5215 +       for (i = 0; i < 2; i++, f++) {
5216 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5217 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5218 +                                   /*file*/NULL, f->force_wr);
5219 +               if (IS_ERR(f->file)) {
5220 +                       err = PTR_ERR(f->file);
5221 +                       if (i == SRC)
5222 +                               goto out;
5223 +                       else
5224 +                               goto out_src;
5225 +               }
5226 +       }
5227 +
5228 +       /* try stopping to update while we copyup */
5229 +       h_src_inode = d_inode(file[SRC].dentry);
5230 +       h_src_sb = h_src_inode->i_sb;
5231 +       if (!au_test_nfs(h_src_sb))
5232 +               IMustLock(h_src_inode);
5233 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5234 +
5235 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5236 +       if (tsk->flags & PF_KTHREAD)
5237 +               __fput_sync(file[DST].file);
5238 +       else {
5239 +               /* it happened actually */
5240 +               fput(file[DST].file);
5241 +               /*
5242 +                * too bad.
5243 +                * we have to call both since we don't know which place the file
5244 +                * was added to.
5245 +                */
5246 +               task_work_run();
5247 +               flush_delayed_fput();
5248 +       }
5249 +       br = au_sbr(sb, file[DST].bindex);
5250 +       au_lcnt_dec(&br->br_nfiles);
5251 +
5252 +out_src:
5253 +       fput(file[SRC].file);
5254 +       br = au_sbr(sb, file[SRC].bindex);
5255 +       au_lcnt_dec(&br->br_nfiles);
5256 +out:
5257 +       return err;
5258 +}
5259 +
5260 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5261 +                             struct au_cpup_reg_attr *h_src_attr)
5262 +{
5263 +       int err, rerr;
5264 +       loff_t l;
5265 +       struct path h_path;
5266 +       struct inode *h_src_inode, *h_dst_inode;
5267 +
5268 +       err = 0;
5269 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5270 +       l = i_size_read(h_src_inode);
5271 +       if (cpg->len == -1 || l < cpg->len)
5272 +               cpg->len = l;
5273 +       if (cpg->len) {
5274 +               /* try stopping to update while we are referencing */
5275 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5276 +               au_pin_hdir_unlock(cpg->pin);
5277 +
5278 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5279 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5280 +               h_src_attr->iflags = h_src_inode->i_flags;
5281 +               if (!au_test_nfs(h_src_inode->i_sb))
5282 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5283 +               else {
5284 +                       inode_unlock_shared(h_src_inode);
5285 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5286 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5287 +               }
5288 +               if (unlikely(err)) {
5289 +                       inode_unlock_shared(h_src_inode);
5290 +                       goto out;
5291 +               }
5292 +               h_src_attr->valid = 1;
5293 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5294 +                       err = au_cp_regular(cpg);
5295 +                       inode_unlock_shared(h_src_inode);
5296 +               } else {
5297 +                       inode_unlock_shared(h_src_inode);
5298 +                       err = au_cp_regular(cpg);
5299 +               }
5300 +               rerr = au_pin_hdir_relock(cpg->pin);
5301 +               if (!err && rerr)
5302 +                       err = rerr;
5303 +       }
5304 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5305 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5306 +               h_dst_inode = d_inode(h_path.dentry);
5307 +               spin_lock(&h_dst_inode->i_lock);
5308 +               h_dst_inode->i_state |= I_LINKABLE;
5309 +               spin_unlock(&h_dst_inode->i_lock);
5310 +       }
5311 +
5312 +out:
5313 +       return err;
5314 +}
5315 +
5316 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5317 +                             struct inode *h_dir)
5318 +{
5319 +       int err;
5320 +       DEFINE_DELAYED_CALL(done);
5321 +       const char *sym;
5322 +
5323 +       sym = vfs_get_link(h_src, &done);
5324 +       err = PTR_ERR(sym);
5325 +       if (IS_ERR(sym))
5326 +               goto out;
5327 +
5328 +       err = vfsub_symlink(h_dir, h_path, sym);
5329 +
5330 +out:
5331 +       do_delayed_call(&done);
5332 +       return err;
5333 +}
5334 +
5335 +/*
5336 + * regardless 'acl' option, reset all ACL.
5337 + * All ACL will be copied up later from the original entry on the lower branch.
5338 + */
5339 +static int au_reset_acl(struct path *h_path, umode_t mode)
5340 +{
5341 +       int err;
5342 +       struct dentry *h_dentry;
5343 +       /* struct inode *h_inode; */
5344 +       struct mnt_idmap *h_idmap;
5345 +
5346 +       h_idmap = mnt_idmap(h_path->mnt);
5347 +       h_dentry = h_path->dentry;
5348 +       /* h_inode = d_inode(h_dentry); */
5349 +       /* forget_all_cached_acls(h_inode)); */
5350 +       err = vfsub_remove_acl(h_idmap, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5351 +       AuTraceErr(err);
5352 +       if (err == -EOPNOTSUPP)
5353 +               err = 0;
5354 +       if (!err)
5355 +               err = vfsub_acl_chmod(h_idmap, h_dentry, mode);
5356 +
5357 +       AuTraceErr(err);
5358 +       return err;
5359 +}
5360 +
5361 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5362 +                         struct inode *h_dir, struct path *h_path)
5363 +{
5364 +       int err;
5365 +       struct inode *dir, *inode;
5366 +       struct mnt_idmap *h_idmap;
5367 +
5368 +       h_idmap = mnt_idmap(h_path->mnt);
5369 +       err = vfsub_remove_acl(h_idmap, h_path->dentry,
5370 +                              XATTR_NAME_POSIX_ACL_DEFAULT);
5371 +       AuTraceErr(err);
5372 +       if (err == -EOPNOTSUPP)
5373 +               err = 0;
5374 +       if (unlikely(err))
5375 +               goto out;
5376 +
5377 +       /*
5378 +        * strange behaviour from the users view,
5379 +        * particularly setattr case
5380 +        */
5381 +       dir = d_inode(dst_parent);
5382 +       if (au_ibtop(dir) == cpg->bdst)
5383 +               au_cpup_attr_nlink(dir, /*force*/1);
5384 +       inode = d_inode(cpg->dentry);
5385 +       au_cpup_attr_nlink(inode, /*force*/1);
5386 +
5387 +out:
5388 +       return err;
5389 +}
5390 +
5391 +static noinline_for_stack
5392 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5393 +              struct au_cpup_reg_attr *h_src_attr)
5394 +{
5395 +       int err;
5396 +       umode_t mode;
5397 +       unsigned int mnt_flags;
5398 +       unsigned char isdir, isreg, force;
5399 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5400 +       struct au_dtime dt;
5401 +       struct path h_path;
5402 +       struct dentry *h_src, *h_dst, *h_parent;
5403 +       struct inode *h_inode, *h_dir;
5404 +       struct super_block *sb;
5405 +
5406 +       /* bsrc branch can be ro/rw. */
5407 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5408 +       h_inode = d_inode(h_src);
5409 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5410 +
5411 +       /* try stopping to be referenced while we are creating */
5412 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5413 +       if (au_ftest_cpup(cpg->flags, RENAME))
5414 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5415 +                                 AUFS_WH_PFX_LEN));
5416 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5417 +       h_dir = d_inode(h_parent);
5418 +       IMustLock(h_dir);
5419 +       AuDebugOn(h_parent != h_dst->d_parent);
5420 +
5421 +       sb = cpg->dentry->d_sb;
5422 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5423 +       if (do_dt) {
5424 +               h_path.dentry = h_parent;
5425 +               au_dtime_store(&dt, dst_parent, &h_path);
5426 +       }
5427 +       h_path.dentry = h_dst;
5428 +
5429 +       isreg = 0;
5430 +       isdir = 0;
5431 +       mode = h_inode->i_mode;
5432 +       switch (mode & S_IFMT) {
5433 +       case S_IFREG:
5434 +               isreg = 1;
5435 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5436 +               if (!err)
5437 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5438 +               break;
5439 +       case S_IFDIR:
5440 +               isdir = 1;
5441 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5442 +               if (!err)
5443 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5444 +               break;
5445 +       case S_IFLNK:
5446 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5447 +               break;
5448 +       case S_IFCHR:
5449 +       case S_IFBLK:
5450 +               AuDebugOn(!capable(CAP_MKNOD));
5451 +               fallthrough;
5452 +       case S_IFIFO:
5453 +       case S_IFSOCK:
5454 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5455 +               break;
5456 +       default:
5457 +               AuIOErr("Unknown inode type 0%o\n", mode);
5458 +               err = -EIO;
5459 +       }
5460 +       if (!err)
5461 +               err = au_reset_acl(&h_path, mode);
5462 +
5463 +       mnt_flags = au_mntflags(sb);
5464 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5465 +           && !isdir
5466 +           && au_opt_test(mnt_flags, XINO)
5467 +           && (h_inode->i_nlink == 1
5468 +               || (h_inode->i_state & I_LINKABLE))
5469 +           /* todo: unnecessary? */
5470 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5471 +           && cpg->bdst < cpg->bsrc
5472 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5473 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5474 +               /* ignore this error */
5475 +
5476 +       if (!err) {
5477 +               force = 0;
5478 +               if (isreg) {
5479 +                       force = !!cpg->len;
5480 +                       if (cpg->len == -1)
5481 +                               force = !!i_size_read(h_inode);
5482 +               }
5483 +               au_fhsm_wrote(sb, cpg->bdst, force);
5484 +       }
5485 +
5486 +       if (do_dt)
5487 +               au_dtime_revert(&dt);
5488 +       return err;
5489 +}
5490 +
5491 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5492 +{
5493 +       int err;
5494 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5495 +       struct path h_ppath;
5496 +       struct inode *h_dir;
5497 +       aufs_bindex_t bdst;
5498 +
5499 +       dentry = cpg->dentry;
5500 +       bdst = cpg->bdst;
5501 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5502 +       h_dentry = au_h_dptr(dentry, bdst);
5503 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5504 +               dget(h_dentry);
5505 +               au_set_h_dptr(dentry, bdst, NULL);
5506 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5507 +               if (!err)
5508 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5509 +               au_set_h_dptr(dentry, bdst, h_dentry);
5510 +       } else {
5511 +               err = 0;
5512 +               parent = dget_parent(dentry);
5513 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5514 +               dput(parent);
5515 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5516 +               if (IS_ERR(h_path->dentry))
5517 +                       err = PTR_ERR(h_path->dentry);
5518 +       }
5519 +       if (unlikely(err))
5520 +               goto out;
5521 +
5522 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5523 +       h_dir = d_inode(h_parent);
5524 +       IMustLock(h_dir);
5525 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5526 +       /* no delegation since it is just created */
5527 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5528 +                          /*flags*/0);
5529 +       dput(h_path->dentry);
5530 +
5531 +out:
5532 +       return err;
5533 +}
5534 +
5535 +/*
5536 + * copyup the @dentry from @bsrc to @bdst.
5537 + * the caller must set the both of lower dentries.
5538 + * @len is for truncating when it is -1 copyup the entire file.
5539 + * in link/rename cases, @dst_parent may be different from the real one.
5540 + * basic->bsrc can be larger than basic->bdst.
5541 + * aufs doesn't touch the credential so
5542 + * security_inode_copy_up{,_xattr}() are unnecessary.
5543 + */
5544 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5545 +{
5546 +       int err, rerr;
5547 +       aufs_bindex_t old_ibtop;
5548 +       unsigned char isdir, plink;
5549 +       struct dentry *h_src, *h_dst, *h_parent;
5550 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5551 +       struct super_block *sb;
5552 +       struct au_branch *br;
5553 +       struct path h_src_path;
5554 +       /* to reduce stack size */
5555 +       struct {
5556 +               struct au_dtime dt;
5557 +               struct path h_path;
5558 +               struct au_cpup_reg_attr h_src_attr;
5559 +       } *a;
5560 +
5561 +       err = -ENOMEM;
5562 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5563 +       if (unlikely(!a))
5564 +               goto out;
5565 +       a->h_src_attr.valid = 0;
5566 +
5567 +       sb = cpg->dentry->d_sb;
5568 +       br = au_sbr(sb, cpg->bdst);
5569 +       a->h_path.mnt = au_br_mnt(br);
5570 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5571 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5572 +       h_dir = d_inode(h_parent);
5573 +       IMustLock(h_dir);
5574 +
5575 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5576 +       inode = d_inode(cpg->dentry);
5577 +
5578 +       if (!dst_parent)
5579 +               dst_parent = dget_parent(cpg->dentry);
5580 +       else
5581 +               dget(dst_parent);
5582 +
5583 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5584 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5585 +       if (dst_inode) {
5586 +               if (unlikely(!plink)) {
5587 +                       err = -EIO;
5588 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5589 +                               "but plink is disabled\n",
5590 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5591 +                       goto out_parent;
5592 +               }
5593 +
5594 +               if (dst_inode->i_nlink) {
5595 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5596 +
5597 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5598 +                       err = PTR_ERR(h_src);
5599 +                       if (IS_ERR(h_src))
5600 +                               goto out_parent;
5601 +                       if (unlikely(d_is_negative(h_src))) {
5602 +                               err = -EIO;
5603 +                               AuIOErr("i%lu exists on b%d "
5604 +                                       "but not pseudo-linked\n",
5605 +                                       inode->i_ino, cpg->bdst);
5606 +                               dput(h_src);
5607 +                               goto out_parent;
5608 +                       }
5609 +
5610 +                       if (do_dt) {
5611 +                               a->h_path.dentry = h_parent;
5612 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5613 +                       }
5614 +
5615 +                       a->h_path.dentry = h_dst;
5616 +                       delegated = NULL;
5617 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5618 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5619 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5620 +                       if (do_dt)
5621 +                               au_dtime_revert(&a->dt);
5622 +                       if (unlikely(err == -EWOULDBLOCK)) {
5623 +                               pr_warn("cannot retry for NFSv4 delegation"
5624 +                                       " for an internal link\n");
5625 +                               iput(delegated);
5626 +                       }
5627 +                       dput(h_src);
5628 +                       goto out_parent;
5629 +               } else
5630 +                       /* todo: cpup_wh_file? */
5631 +                       /* udba work */
5632 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5633 +       }
5634 +
5635 +       isdir = S_ISDIR(inode->i_mode);
5636 +       old_ibtop = au_ibtop(inode);
5637 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5638 +       if (unlikely(err))
5639 +               goto out_rev;
5640 +       dst_inode = d_inode(h_dst);
5641 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5642 +       /* todo: necessary? */
5643 +       /* au_pin_hdir_unlock(cpg->pin); */
5644 +
5645 +       h_src_path.dentry = h_src;
5646 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5647 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5648 +       if (unlikely(err)) {
5649 +               /* todo: necessary? */
5650 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5651 +               inode_unlock(dst_inode);
5652 +               goto out_rev;
5653 +       }
5654 +
5655 +       if (cpg->bdst < old_ibtop) {
5656 +               if (S_ISREG(inode->i_mode)) {
5657 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5658 +                       if (unlikely(err)) {
5659 +                               /* ignore an error */
5660 +                               /* au_pin_hdir_relock(cpg->pin); */
5661 +                               inode_unlock(dst_inode);
5662 +                               goto out_rev;
5663 +                       }
5664 +               }
5665 +               au_set_ibtop(inode, cpg->bdst);
5666 +       } else
5667 +               au_set_ibbot(inode, cpg->bdst);
5668 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5669 +                     au_hi_flags(inode, isdir));
5670 +
5671 +       /* todo: necessary? */
5672 +       /* err = au_pin_hdir_relock(cpg->pin); */
5673 +       inode_unlock(dst_inode);
5674 +       if (unlikely(err))
5675 +               goto out_rev;
5676 +
5677 +       src_inode = d_inode(h_src);
5678 +       if (!isdir
5679 +           && (src_inode->i_nlink > 1
5680 +               || src_inode->i_state & I_LINKABLE)
5681 +           && plink)
5682 +               au_plink_append(inode, cpg->bdst, h_dst);
5683 +
5684 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5685 +               a->h_path.dentry = h_dst;
5686 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5687 +       }
5688 +       if (!err)
5689 +               goto out_parent; /* success */
5690 +
5691 +       /* revert */
5692 +out_rev:
5693 +       a->h_path.dentry = h_parent;
5694 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5695 +       a->h_path.dentry = h_dst;
5696 +       rerr = 0;
5697 +       if (d_is_positive(h_dst)) {
5698 +               if (!isdir) {
5699 +                       /* no delegation since it is just created */
5700 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5701 +                                           /*delegated*/NULL, /*force*/0);
5702 +               } else
5703 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5704 +       }
5705 +       au_dtime_revert(&a->dt);
5706 +       if (rerr) {
5707 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5708 +               err = -EIO;
5709 +       }
5710 +out_parent:
5711 +       dput(dst_parent);
5712 +       au_kfree_rcu(a);
5713 +out:
5714 +       return err;
5715 +}
5716 +
5717 +#if 0 /* reserved */
5718 +struct au_cpup_single_args {
5719 +       int *errp;
5720 +       struct au_cp_generic *cpg;
5721 +       struct dentry *dst_parent;
5722 +};
5723 +
5724 +static void au_call_cpup_single(void *args)
5725 +{
5726 +       struct au_cpup_single_args *a = args;
5727 +
5728 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5729 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5730 +       au_pin_hdir_release(a->cpg->pin);
5731 +}
5732 +#endif
5733 +
5734 +/*
5735 + * prevent SIGXFSZ in copy-up.
5736 + * testing CAP_MKNOD is for generic fs,
5737 + * but CAP_FSETID is for xfs only, currently.
5738 + */
5739 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5740 +{
5741 +       int do_sio;
5742 +       struct super_block *sb;
5743 +       struct inode *h_dir;
5744 +
5745 +       do_sio = 0;
5746 +       sb = au_pinned_parent(pin)->d_sb;
5747 +       if (!au_wkq_test()
5748 +           && (!au_sbi(sb)->si_plink_maint_pid
5749 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5750 +               switch (mode & S_IFMT) {
5751 +               case S_IFREG:
5752 +                       /* no condition about RLIMIT_FSIZE and the file size */
5753 +                       do_sio = 1;
5754 +                       break;
5755 +               case S_IFCHR:
5756 +               case S_IFBLK:
5757 +                       do_sio = !capable(CAP_MKNOD);
5758 +                       break;
5759 +               }
5760 +               if (!do_sio)
5761 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5762 +                                 && !capable(CAP_FSETID));
5763 +               /* this workaround may be removed in the future */
5764 +               if (!do_sio) {
5765 +                       h_dir = au_pinned_h_dir(pin);
5766 +                       do_sio = h_dir->i_mode & S_ISVTX;
5767 +               }
5768 +       }
5769 +
5770 +       return do_sio;
5771 +}
5772 +
5773 +#if 0 /* reserved */
5774 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5775 +{
5776 +       int err, wkq_err;
5777 +       struct dentry *h_dentry;
5778 +
5779 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5780 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5781 +               err = au_cpup_single(cpg, dst_parent);
5782 +       else {
5783 +               struct au_cpup_single_args args = {
5784 +                       .errp           = &err,
5785 +                       .cpg            = cpg,
5786 +                       .dst_parent     = dst_parent
5787 +               };
5788 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5789 +               if (unlikely(wkq_err))
5790 +                       err = wkq_err;
5791 +       }
5792 +
5793 +       return err;
5794 +}
5795 +#endif
5796 +
5797 +/*
5798 + * copyup the @dentry from the first active lower branch to @bdst,
5799 + * using au_cpup_single().
5800 + */
5801 +static int au_cpup_simple(struct au_cp_generic *cpg)
5802 +{
5803 +       int err;
5804 +       unsigned int flags_orig;
5805 +       struct dentry *dentry;
5806 +
5807 +       AuDebugOn(cpg->bsrc < 0);
5808 +
5809 +       dentry = cpg->dentry;
5810 +       DiMustWriteLock(dentry);
5811 +
5812 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5813 +       if (!err) {
5814 +               flags_orig = cpg->flags;
5815 +               au_fset_cpup(cpg->flags, RENAME);
5816 +               err = au_cpup_single(cpg, NULL);
5817 +               cpg->flags = flags_orig;
5818 +               if (!err)
5819 +                       return 0; /* success */
5820 +
5821 +               /* revert */
5822 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5823 +               au_set_dbtop(dentry, cpg->bsrc);
5824 +       }
5825 +
5826 +       return err;
5827 +}
5828 +
5829 +struct au_cpup_simple_args {
5830 +       int *errp;
5831 +       struct au_cp_generic *cpg;
5832 +};
5833 +
5834 +static void au_call_cpup_simple(void *args)
5835 +{
5836 +       struct au_cpup_simple_args *a = args;
5837 +
5838 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5839 +       *a->errp = au_cpup_simple(a->cpg);
5840 +       au_pin_hdir_release(a->cpg->pin);
5841 +}
5842 +
5843 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5844 +{
5845 +       int err, wkq_err;
5846 +       struct dentry *dentry, *parent;
5847 +       struct file *h_file;
5848 +       struct inode *h_dir;
5849 +       struct mnt_idmap *h_idmap;
5850 +
5851 +       dentry = cpg->dentry;
5852 +       h_file = NULL;
5853 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5854 +               AuDebugOn(cpg->bsrc < 0);
5855 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5856 +               err = PTR_ERR(h_file);
5857 +               if (IS_ERR(h_file))
5858 +                       goto out;
5859 +       }
5860 +
5861 +       parent = dget_parent(dentry);
5862 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5863 +       h_idmap = au_sbr_idmap(dentry->d_sb, cpg->bdst);
5864 +       if (!au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC | MAY_WRITE)
5865 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5866 +               err = au_cpup_simple(cpg);
5867 +       else {
5868 +               struct au_cpup_simple_args args = {
5869 +                       .errp           = &err,
5870 +                       .cpg            = cpg
5871 +               };
5872 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5873 +               if (unlikely(wkq_err))
5874 +                       err = wkq_err;
5875 +       }
5876 +
5877 +       dput(parent);
5878 +       if (h_file)
5879 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5880 +
5881 +out:
5882 +       return err;
5883 +}
5884 +
5885 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5886 +{
5887 +       aufs_bindex_t bsrc, bbot;
5888 +       struct dentry *dentry, *h_dentry;
5889 +
5890 +       if (cpg->bsrc < 0) {
5891 +               dentry = cpg->dentry;
5892 +               bbot = au_dbbot(dentry);
5893 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5894 +                       h_dentry = au_h_dptr(dentry, bsrc);
5895 +                       if (h_dentry) {
5896 +                               AuDebugOn(d_is_negative(h_dentry));
5897 +                               break;
5898 +                       }
5899 +               }
5900 +               AuDebugOn(bsrc > bbot);
5901 +               cpg->bsrc = bsrc;
5902 +       }
5903 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5904 +       return au_do_sio_cpup_simple(cpg);
5905 +}
5906 +
5907 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5908 +{
5909 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5910 +       return au_do_sio_cpup_simple(cpg);
5911 +}
5912 +
5913 +/* ---------------------------------------------------------------------- */
5914 +
5915 +/*
5916 + * copyup the deleted file for writing.
5917 + */
5918 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5919 +                        struct file *file)
5920 +{
5921 +       int err;
5922 +       unsigned int flags_orig;
5923 +       aufs_bindex_t bsrc_orig;
5924 +       struct au_dinfo *dinfo;
5925 +       struct {
5926 +               struct au_hdentry *hd;
5927 +               struct dentry *h_dentry;
5928 +       } hdst, hsrc;
5929 +
5930 +       dinfo = au_di(cpg->dentry);
5931 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5932 +
5933 +       bsrc_orig = cpg->bsrc;
5934 +       cpg->bsrc = dinfo->di_btop;
5935 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5936 +       hdst.h_dentry = hdst.hd->hd_dentry;
5937 +       hdst.hd->hd_dentry = wh_dentry;
5938 +       dinfo->di_btop = cpg->bdst;
5939 +
5940 +       hsrc.h_dentry = NULL;
5941 +       if (file) {
5942 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5943 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5944 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5945 +       }
5946 +       flags_orig = cpg->flags;
5947 +       cpg->flags = !AuCpup_DTIME;
5948 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5949 +       cpg->flags = flags_orig;
5950 +       if (file) {
5951 +               if (!err)
5952 +                       err = au_reopen_nondir(file);
5953 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5954 +       }
5955 +       hdst.hd->hd_dentry = hdst.h_dentry;
5956 +       dinfo->di_btop = cpg->bsrc;
5957 +       cpg->bsrc = bsrc_orig;
5958 +
5959 +       return err;
5960 +}
5961 +
5962 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5963 +{
5964 +       int err;
5965 +       aufs_bindex_t bdst;
5966 +       struct au_dtime dt;
5967 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5968 +       struct au_branch *br;
5969 +       struct path h_path;
5970 +
5971 +       dentry = cpg->dentry;
5972 +       bdst = cpg->bdst;
5973 +       br = au_sbr(dentry->d_sb, bdst);
5974 +       parent = dget_parent(dentry);
5975 +       h_parent = au_h_dptr(parent, bdst);
5976 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5977 +       err = PTR_ERR(wh_dentry);
5978 +       if (IS_ERR(wh_dentry))
5979 +               goto out;
5980 +
5981 +       h_path.dentry = h_parent;
5982 +       h_path.mnt = au_br_mnt(br);
5983 +       au_dtime_store(&dt, parent, &h_path);
5984 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5985 +       if (unlikely(err))
5986 +               goto out_wh;
5987 +
5988 +       dget(wh_dentry);
5989 +       h_path.dentry = wh_dentry;
5990 +       if (!d_is_dir(wh_dentry)) {
5991 +               /* no delegation since it is just created */
5992 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5993 +                                  /*delegated*/NULL, /*force*/0);
5994 +       } else
5995 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5996 +       if (unlikely(err)) {
5997 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5998 +                       wh_dentry, err);
5999 +               err = -EIO;
6000 +       }
6001 +       au_dtime_revert(&dt);
6002 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6003 +
6004 +out_wh:
6005 +       dput(wh_dentry);
6006 +out:
6007 +       dput(parent);
6008 +       return err;
6009 +}
6010 +
6011 +struct au_cpup_wh_args {
6012 +       int *errp;
6013 +       struct au_cp_generic *cpg;
6014 +       struct file *file;
6015 +};
6016 +
6017 +static void au_call_cpup_wh(void *args)
6018 +{
6019 +       struct au_cpup_wh_args *a = args;
6020 +
6021 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6022 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6023 +       au_pin_hdir_release(a->cpg->pin);
6024 +}
6025 +
6026 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6027 +{
6028 +       int err, wkq_err;
6029 +       aufs_bindex_t bdst;
6030 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6031 +       struct inode *dir, *h_dir, *h_tmpdir;
6032 +       struct au_wbr *wbr;
6033 +       struct au_pin wh_pin, *pin_orig;
6034 +       struct mnt_idmap *h_idmap;
6035 +
6036 +       dentry = cpg->dentry;
6037 +       bdst = cpg->bdst;
6038 +       parent = dget_parent(dentry);
6039 +       dir = d_inode(parent);
6040 +       h_orph = NULL;
6041 +       h_parent = NULL;
6042 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6043 +       h_tmpdir = h_dir;
6044 +       pin_orig = NULL;
6045 +       if (!h_dir->i_nlink) {
6046 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6047 +               h_orph = wbr->wbr_orph;
6048 +
6049 +               h_parent = dget(au_h_dptr(parent, bdst));
6050 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6051 +               h_tmpdir = d_inode(h_orph);
6052 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6053 +
6054 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6055 +               /* todo: au_h_open_pre()? */
6056 +
6057 +               pin_orig = cpg->pin;
6058 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6059 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6060 +               cpg->pin = &wh_pin;
6061 +       }
6062 +
6063 +       h_idmap = au_sbr_idmap(dentry->d_sb, bdst);
6064 +       if (!au_test_h_perm_sio(h_idmap, h_tmpdir, MAY_EXEC | MAY_WRITE)
6065 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6066 +               err = au_cpup_wh(cpg, file);
6067 +       else {
6068 +               struct au_cpup_wh_args args = {
6069 +                       .errp   = &err,
6070 +                       .cpg    = cpg,
6071 +                       .file   = file
6072 +               };
6073 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6074 +               if (unlikely(wkq_err))
6075 +                       err = wkq_err;
6076 +       }
6077 +
6078 +       if (h_orph) {
6079 +               inode_unlock(h_tmpdir);
6080 +               /* todo: au_h_open_post()? */
6081 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6082 +               au_set_h_dptr(parent, bdst, h_parent);
6083 +               AuDebugOn(!pin_orig);
6084 +               cpg->pin = pin_orig;
6085 +       }
6086 +       iput(h_dir);
6087 +       dput(parent);
6088 +
6089 +       return err;
6090 +}
6091 +
6092 +/* ---------------------------------------------------------------------- */
6093 +
6094 +/*
6095 + * generic routine for both of copy-up and copy-down.
6096 + */
6097 +/* cf. revalidate function in file.c */
6098 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6099 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6100 +                        struct au_pin *pin,
6101 +                        struct dentry *h_parent, void *arg),
6102 +              void *arg)
6103 +{
6104 +       int err;
6105 +       struct au_pin pin;
6106 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6107 +
6108 +       err = 0;
6109 +       parent = dget_parent(dentry);
6110 +       if (IS_ROOT(parent))
6111 +               goto out;
6112 +
6113 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6114 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6115 +
6116 +       /* do not use au_dpage */
6117 +       real_parent = parent;
6118 +       while (1) {
6119 +               dput(parent);
6120 +               parent = dget_parent(dentry);
6121 +               h_parent = au_h_dptr(parent, bdst);
6122 +               if (h_parent)
6123 +                       goto out; /* success */
6124 +
6125 +               /* find top dir which is necessary to cpup */
6126 +               do {
6127 +                       d = parent;
6128 +                       dput(parent);
6129 +                       parent = dget_parent(d);
6130 +                       di_read_lock_parent3(parent, !AuLock_IR);
6131 +                       h_parent = au_h_dptr(parent, bdst);
6132 +                       di_read_unlock(parent, !AuLock_IR);
6133 +               } while (!h_parent);
6134 +
6135 +               if (d != real_parent)
6136 +                       di_write_lock_child3(d);
6137 +
6138 +               /* somebody else might create while we were sleeping */
6139 +               h_dentry = au_h_dptr(d, bdst);
6140 +               if (!h_dentry || d_is_negative(h_dentry)) {
6141 +                       if (h_dentry)
6142 +                               au_update_dbtop(d);
6143 +
6144 +                       au_pin_set_dentry(&pin, d);
6145 +                       err = au_do_pin(&pin);
6146 +                       if (!err) {
6147 +                               err = cp(d, bdst, &pin, h_parent, arg);
6148 +                               au_unpin(&pin);
6149 +                       }
6150 +               }
6151 +
6152 +               if (d != real_parent)
6153 +                       di_write_unlock(d);
6154 +               if (unlikely(err))
6155 +                       break;
6156 +       }
6157 +
6158 +out:
6159 +       dput(parent);
6160 +       return err;
6161 +}
6162 +
6163 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6164 +                      struct au_pin *pin,
6165 +                      struct dentry *h_parent __maybe_unused,
6166 +                      void *arg __maybe_unused)
6167 +{
6168 +       struct au_cp_generic cpg = {
6169 +               .dentry = dentry,
6170 +               .bdst   = bdst,
6171 +               .bsrc   = -1,
6172 +               .len    = 0,
6173 +               .pin    = pin,
6174 +               .flags  = AuCpup_DTIME
6175 +       };
6176 +       return au_sio_cpup_simple(&cpg);
6177 +}
6178 +
6179 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6180 +{
6181 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6182 +}
6183 +
6184 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6185 +{
6186 +       int err;
6187 +       struct dentry *parent;
6188 +       struct inode *dir;
6189 +
6190 +       parent = dget_parent(dentry);
6191 +       dir = d_inode(parent);
6192 +       err = 0;
6193 +       if (au_h_iptr(dir, bdst))
6194 +               goto out;
6195 +
6196 +       di_read_unlock(parent, AuLock_IR);
6197 +       di_write_lock_parent(parent);
6198 +       /* someone else might change our inode while we were sleeping */
6199 +       if (!au_h_iptr(dir, bdst))
6200 +               err = au_cpup_dirs(dentry, bdst);
6201 +       di_downgrade_lock(parent, AuLock_IR);
6202 +
6203 +out:
6204 +       dput(parent);
6205 +       return err;
6206 +}
6207 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6208 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6209 +++ linux/fs/aufs/cpup.h        2022-11-05 23:02:18.962555950 +0100
6210 @@ -0,0 +1,100 @@
6211 +/* SPDX-License-Identifier: GPL-2.0 */
6212 +/*
6213 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6214 + *
6215 + * This program is free software; you can redistribute it and/or modify
6216 + * it under the terms of the GNU General Public License as published by
6217 + * the Free Software Foundation; either version 2 of the License, or
6218 + * (at your option) any later version.
6219 + *
6220 + * This program is distributed in the hope that it will be useful,
6221 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6222 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6223 + * GNU General Public License for more details.
6224 + *
6225 + * You should have received a copy of the GNU General Public License
6226 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6227 + */
6228 +
6229 +/*
6230 + * copy-up/down functions
6231 + */
6232 +
6233 +#ifndef __AUFS_CPUP_H__
6234 +#define __AUFS_CPUP_H__
6235 +
6236 +#ifdef __KERNEL__
6237 +
6238 +#include <linux/path.h>
6239 +
6240 +struct inode;
6241 +struct file;
6242 +struct au_pin;
6243 +
6244 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6245 +void au_cpup_attr_timesizes(struct inode *inode);
6246 +void au_cpup_attr_nlink(struct inode *inode, int force);
6247 +void au_cpup_attr_changeable(struct inode *inode);
6248 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6249 +void au_cpup_attr_all(struct inode *inode, int force);
6250 +
6251 +/* ---------------------------------------------------------------------- */
6252 +
6253 +struct au_cp_generic {
6254 +       struct dentry   *dentry;
6255 +       aufs_bindex_t   bdst, bsrc;
6256 +       loff_t          len;
6257 +       struct au_pin   *pin;
6258 +       unsigned int    flags;
6259 +};
6260 +
6261 +/* cpup flags */
6262 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6263 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6264 +                                                  for link(2) */
6265 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6266 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6267 +                                                  cpup */
6268 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6269 +                                                  existing entry */
6270 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6271 +                                                  the branch is marked as RO */
6272 +
6273 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6274 +#undef AuCpup_HOPEN
6275 +#define AuCpup_HOPEN           0
6276 +#endif
6277 +
6278 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6279 +#define au_fset_cpup(flags, name) \
6280 +       do { (flags) |= AuCpup_##name; } while (0)
6281 +#define au_fclr_cpup(flags, name) \
6282 +       do { (flags) &= ~AuCpup_##name; } while (0)
6283 +
6284 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6285 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6286 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6287 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6288 +
6289 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6290 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6291 +                        struct au_pin *pin,
6292 +                        struct dentry *h_parent, void *arg),
6293 +              void *arg);
6294 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6295 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6296 +
6297 +/* ---------------------------------------------------------------------- */
6298 +
6299 +/* keep timestamps when copyup */
6300 +struct au_dtime {
6301 +       struct dentry *dt_dentry;
6302 +       struct path dt_h_path;
6303 +       struct timespec64 dt_atime, dt_mtime;
6304 +};
6305 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6306 +                   struct path *h_path);
6307 +void au_dtime_revert(struct au_dtime *dt);
6308 +
6309 +#endif /* __KERNEL__ */
6310 +#endif /* __AUFS_CPUP_H__ */
6311 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6312 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6313 +++ linux/fs/aufs/dbgaufs.c     2022-11-05 23:02:18.962555950 +0100
6314 @@ -0,0 +1,526 @@
6315 +// SPDX-License-Identifier: GPL-2.0
6316 +/*
6317 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6318 + *
6319 + * This program is free software; you can redistribute it and/or modify
6320 + * it under the terms of the GNU General Public License as published by
6321 + * the Free Software Foundation; either version 2 of the License, or
6322 + * (at your option) any later version.
6323 + *
6324 + * This program is distributed in the hope that it will be useful,
6325 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6326 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6327 + * GNU General Public License for more details.
6328 + *
6329 + * You should have received a copy of the GNU General Public License
6330 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6331 + */
6332 +
6333 +/*
6334 + * debugfs interface
6335 + */
6336 +
6337 +#include <linux/debugfs.h>
6338 +#include "aufs.h"
6339 +
6340 +#ifndef CONFIG_SYSFS
6341 +#error DEBUG_FS depends upon SYSFS
6342 +#endif
6343 +
6344 +static struct dentry *dbgaufs;
6345 +static const mode_t dbgaufs_mode = 0444;
6346 +
6347 +/* 20 is max digits length of ulong 64 */
6348 +struct dbgaufs_arg {
6349 +       int n;
6350 +       char a[20 * 4];
6351 +};
6352 +
6353 +/*
6354 + * common function for all XINO files
6355 + */
6356 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6357 +                             struct file *file)
6358 +{
6359 +       void *p;
6360 +
6361 +       p = file->private_data;
6362 +       if (p) {
6363 +               /* this is struct dbgaufs_arg */
6364 +               AuDebugOn(!au_kfree_sz_test(p));
6365 +               au_kfree_do_rcu(p);
6366 +       }
6367 +       return 0;
6368 +}
6369 +
6370 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6371 +                          int cnt)
6372 +{
6373 +       int err;
6374 +       struct kstat st;
6375 +       struct dbgaufs_arg *p;
6376 +
6377 +       err = -ENOMEM;
6378 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6379 +       if (unlikely(!p))
6380 +               goto out;
6381 +
6382 +       err = 0;
6383 +       p->n = 0;
6384 +       file->private_data = p;
6385 +       if (!xf)
6386 +               goto out;
6387 +
6388 +       err = vfsub_getattr(&xf->f_path, &st);
6389 +       if (!err) {
6390 +               if (do_fcnt)
6391 +                       p->n = snprintf
6392 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6393 +                                cnt, st.blocks, st.blksize,
6394 +                                (long long)st.size);
6395 +               else
6396 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6397 +                                       st.blocks, st.blksize,
6398 +                                       (long long)st.size);
6399 +               AuDebugOn(p->n >= sizeof(p->a));
6400 +       } else {
6401 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6402 +               err = 0;
6403 +       }
6404 +
6405 +out:
6406 +       return err;
6407 +}
6408 +
6409 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6410 +                              size_t count, loff_t *ppos)
6411 +{
6412 +       struct dbgaufs_arg *p;
6413 +
6414 +       p = file->private_data;
6415 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6416 +}
6417 +
6418 +/* ---------------------------------------------------------------------- */
6419 +
6420 +struct dbgaufs_plink_arg {
6421 +       int n;
6422 +       char a[];
6423 +};
6424 +
6425 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6426 +                                struct file *file)
6427 +{
6428 +       free_page((unsigned long)file->private_data);
6429 +       return 0;
6430 +}
6431 +
6432 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6433 +{
6434 +       int err, i, limit;
6435 +       unsigned long n, sum;
6436 +       struct dbgaufs_plink_arg *p;
6437 +       struct au_sbinfo *sbinfo;
6438 +       struct super_block *sb;
6439 +       struct hlist_bl_head *hbl;
6440 +
6441 +       err = -ENOMEM;
6442 +       p = (void *)get_zeroed_page(GFP_NOFS);
6443 +       if (unlikely(!p))
6444 +               goto out;
6445 +
6446 +       err = -EFBIG;
6447 +       sbinfo = inode->i_private;
6448 +       sb = sbinfo->si_sb;
6449 +       si_noflush_read_lock(sb);
6450 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6451 +               limit = PAGE_SIZE - sizeof(p->n);
6452 +
6453 +               /* the number of buckets */
6454 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6455 +               p->n += n;
6456 +               limit -= n;
6457 +
6458 +               sum = 0;
6459 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6460 +                    i++, hbl++) {
6461 +                       n = au_hbl_count(hbl);
6462 +                       sum += n;
6463 +
6464 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6465 +                       p->n += n;
6466 +                       limit -= n;
6467 +                       if (unlikely(limit <= 0))
6468 +                               goto out_free;
6469 +               }
6470 +               p->a[p->n - 1] = '\n';
6471 +
6472 +               /* the sum of plinks */
6473 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6474 +               p->n += n;
6475 +               limit -= n;
6476 +               if (unlikely(limit <= 0))
6477 +                       goto out_free;
6478 +       } else {
6479 +#define str "1\n0\n0\n"
6480 +               p->n = sizeof(str) - 1;
6481 +               strcpy(p->a, str);
6482 +#undef str
6483 +       }
6484 +       si_read_unlock(sb);
6485 +
6486 +       err = 0;
6487 +       file->private_data = p;
6488 +       goto out; /* success */
6489 +
6490 +out_free:
6491 +       free_page((unsigned long)p);
6492 +out:
6493 +       return err;
6494 +}
6495 +
6496 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6497 +                                 size_t count, loff_t *ppos)
6498 +{
6499 +       struct dbgaufs_plink_arg *p;
6500 +
6501 +       p = file->private_data;
6502 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6503 +}
6504 +
6505 +static const struct file_operations dbgaufs_plink_fop = {
6506 +       .owner          = THIS_MODULE,
6507 +       .open           = dbgaufs_plink_open,
6508 +       .release        = dbgaufs_plink_release,
6509 +       .read           = dbgaufs_plink_read
6510 +};
6511 +
6512 +/* ---------------------------------------------------------------------- */
6513 +
6514 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6515 +{
6516 +       int err;
6517 +       struct au_sbinfo *sbinfo;
6518 +       struct super_block *sb;
6519 +
6520 +       sbinfo = inode->i_private;
6521 +       sb = sbinfo->si_sb;
6522 +       si_noflush_read_lock(sb);
6523 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6524 +       si_read_unlock(sb);
6525 +       return err;
6526 +}
6527 +
6528 +static const struct file_operations dbgaufs_xib_fop = {
6529 +       .owner          = THIS_MODULE,
6530 +       .open           = dbgaufs_xib_open,
6531 +       .release        = dbgaufs_xi_release,
6532 +       .read           = dbgaufs_xi_read
6533 +};
6534 +
6535 +/* ---------------------------------------------------------------------- */
6536 +
6537 +#define DbgaufsXi_PREFIX "xi"
6538 +
6539 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6540 +{
6541 +       int err, idx;
6542 +       long l;
6543 +       aufs_bindex_t bindex;
6544 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6545 +       struct au_sbinfo *sbinfo;
6546 +       struct super_block *sb;
6547 +       struct au_xino *xi;
6548 +       struct file *xf;
6549 +       struct qstr *name;
6550 +       struct au_branch *br;
6551 +
6552 +       err = -ENOENT;
6553 +       name = &file->f_path.dentry->d_name;
6554 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6555 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6556 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6557 +               goto out;
6558 +
6559 +       AuDebugOn(name->len >= sizeof(a));
6560 +       memcpy(a, name->name, name->len);
6561 +       a[name->len] = '\0';
6562 +       p = strchr(a, '-');
6563 +       if (p)
6564 +               *p = '\0';
6565 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6566 +       if (unlikely(err))
6567 +               goto out;
6568 +       bindex = l;
6569 +       idx = 0;
6570 +       if (p) {
6571 +               err = kstrtol(p + 1, 10, &l);
6572 +               if (unlikely(err))
6573 +                       goto out;
6574 +               idx = l;
6575 +       }
6576 +
6577 +       err = -ENOENT;
6578 +       sbinfo = inode->i_private;
6579 +       sb = sbinfo->si_sb;
6580 +       si_noflush_read_lock(sb);
6581 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6582 +               goto out_si;
6583 +       br = au_sbr(sb, bindex);
6584 +       xi = br->br_xino;
6585 +       if (unlikely(idx >= xi->xi_nfile))
6586 +               goto out_si;
6587 +       xf = au_xino_file(xi, idx);
6588 +       if (xf)
6589 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6590 +                                     au_xino_count(br));
6591 +
6592 +out_si:
6593 +       si_read_unlock(sb);
6594 +out:
6595 +       AuTraceErr(err);
6596 +       return err;
6597 +}
6598 +
6599 +static const struct file_operations dbgaufs_xino_fop = {
6600 +       .owner          = THIS_MODULE,
6601 +       .open           = dbgaufs_xino_open,
6602 +       .release        = dbgaufs_xi_release,
6603 +       .read           = dbgaufs_xi_read
6604 +};
6605 +
6606 +void dbgaufs_xino_del(struct au_branch *br)
6607 +{
6608 +       struct dentry *dbgaufs;
6609 +
6610 +       dbgaufs = br->br_dbgaufs;
6611 +       if (!dbgaufs)
6612 +               return;
6613 +
6614 +       br->br_dbgaufs = NULL;
6615 +       /* debugfs acquires the parent i_mutex */
6616 +       lockdep_off();
6617 +       debugfs_remove(dbgaufs);
6618 +       lockdep_on();
6619 +}
6620 +
6621 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6622 +{
6623 +       aufs_bindex_t bbot;
6624 +       struct au_branch *br;
6625 +
6626 +       if (!au_sbi(sb)->si_dbgaufs)
6627 +               return;
6628 +
6629 +       bbot = au_sbbot(sb);
6630 +       for (; bindex <= bbot; bindex++) {
6631 +               br = au_sbr(sb, bindex);
6632 +               dbgaufs_xino_del(br);
6633 +       }
6634 +}
6635 +
6636 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6637 +                             unsigned int idx, struct dentry *parent,
6638 +                             struct au_sbinfo *sbinfo)
6639 +{
6640 +       struct au_branch *br;
6641 +       struct dentry *d;
6642 +       /* "xi" bindex(5) "-" idx(2) NULL */
6643 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6644 +
6645 +       if (!idx)
6646 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6647 +       else
6648 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6649 +                        bindex, idx);
6650 +       br = au_sbr(sb, bindex);
6651 +       if (br->br_dbgaufs) {
6652 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6653 +
6654 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6655 +                       /* debugfs acquires the parent i_mutex */
6656 +                       lockdep_off();
6657 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6658 +                                          name);
6659 +                       lockdep_on();
6660 +                       if (unlikely(!d))
6661 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6662 +                                       parent, name);
6663 +               }
6664 +       } else {
6665 +               lockdep_off();
6666 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6667 +                                                    sbinfo, &dbgaufs_xino_fop);
6668 +               lockdep_on();
6669 +               if (unlikely(!br->br_dbgaufs))
6670 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6671 +                               parent, name);
6672 +       }
6673 +}
6674 +
6675 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6676 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6677 +{
6678 +       struct au_branch *br;
6679 +       struct au_xino *xi;
6680 +       unsigned int u;
6681 +
6682 +       br = au_sbr(sb, bindex);
6683 +       xi = br->br_xino;
6684 +       for (u = 0; u < xi->xi_nfile; u++)
6685 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6686 +}
6687 +
6688 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6689 +{
6690 +       struct au_sbinfo *sbinfo;
6691 +       struct dentry *parent;
6692 +       aufs_bindex_t bbot;
6693 +
6694 +       if (!au_opt_test(au_mntflags(sb), XINO))
6695 +               return;
6696 +
6697 +       sbinfo = au_sbi(sb);
6698 +       parent = sbinfo->si_dbgaufs;
6699 +       if (!parent)
6700 +               return;
6701 +
6702 +       bbot = au_sbbot(sb);
6703 +       if (topdown)
6704 +               for (; bindex <= bbot; bindex++)
6705 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6706 +       else
6707 +               for (; bbot >= bindex; bbot--)
6708 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6709 +}
6710 +
6711 +/* ---------------------------------------------------------------------- */
6712 +
6713 +#ifdef CONFIG_AUFS_EXPORT
6714 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6715 +{
6716 +       int err;
6717 +       struct au_sbinfo *sbinfo;
6718 +       struct super_block *sb;
6719 +
6720 +       sbinfo = inode->i_private;
6721 +       sb = sbinfo->si_sb;
6722 +       si_noflush_read_lock(sb);
6723 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6724 +       si_read_unlock(sb);
6725 +       return err;
6726 +}
6727 +
6728 +static const struct file_operations dbgaufs_xigen_fop = {
6729 +       .owner          = THIS_MODULE,
6730 +       .open           = dbgaufs_xigen_open,
6731 +       .release        = dbgaufs_xi_release,
6732 +       .read           = dbgaufs_xi_read
6733 +};
6734 +
6735 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6736 +{
6737 +       int err;
6738 +
6739 +       /*
6740 +        * This function is a dynamic '__init' function actually,
6741 +        * so the tiny check for si_rwsem is unnecessary.
6742 +        */
6743 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6744 +
6745 +       err = -EIO;
6746 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6747 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6748 +                &dbgaufs_xigen_fop);
6749 +       if (sbinfo->si_dbgaufs_xigen)
6750 +               err = 0;
6751 +
6752 +       return err;
6753 +}
6754 +#else
6755 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6756 +{
6757 +       return 0;
6758 +}
6759 +#endif /* CONFIG_AUFS_EXPORT */
6760 +
6761 +/* ---------------------------------------------------------------------- */
6762 +
6763 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6764 +{
6765 +       /*
6766 +        * This function is a dynamic '__fin' function actually,
6767 +        * so the tiny check for si_rwsem is unnecessary.
6768 +        */
6769 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6770 +
6771 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6772 +       sbinfo->si_dbgaufs = NULL;
6773 +}
6774 +
6775 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6776 +{
6777 +       int err;
6778 +       char name[SysaufsSiNameLen];
6779 +
6780 +       /*
6781 +        * This function is a dynamic '__init' function actually,
6782 +        * so the tiny check for si_rwsem is unnecessary.
6783 +        */
6784 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6785 +
6786 +       err = -ENOENT;
6787 +       if (!dbgaufs) {
6788 +               AuErr1("/debug/aufs is uninitialized\n");
6789 +               goto out;
6790 +       }
6791 +
6792 +       err = -EIO;
6793 +       sysaufs_name(sbinfo, name);
6794 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6795 +       if (unlikely(!sbinfo->si_dbgaufs))
6796 +               goto out;
6797 +
6798 +       /* regardless plink/noplink option */
6799 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6800 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6801 +                &dbgaufs_plink_fop);
6802 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6803 +               goto out_dir;
6804 +
6805 +       /* regardless xino/noxino option */
6806 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6807 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6808 +                &dbgaufs_xib_fop);
6809 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6810 +               goto out_dir;
6811 +
6812 +       err = dbgaufs_xigen_init(sbinfo);
6813 +       if (!err)
6814 +               goto out; /* success */
6815 +
6816 +out_dir:
6817 +       dbgaufs_si_fin(sbinfo);
6818 +out:
6819 +       if (unlikely(err))
6820 +               pr_err("debugfs/aufs failed\n");
6821 +       return err;
6822 +}
6823 +
6824 +/* ---------------------------------------------------------------------- */
6825 +
6826 +void dbgaufs_fin(void)
6827 +{
6828 +       debugfs_remove(dbgaufs);
6829 +}
6830 +
6831 +int __init dbgaufs_init(void)
6832 +{
6833 +       int err;
6834 +
6835 +       err = -EIO;
6836 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6837 +       if (dbgaufs)
6838 +               err = 0;
6839 +       return err;
6840 +}
6841 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6842 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6843 +++ linux/fs/aufs/dbgaufs.h     2022-11-05 23:02:18.962555950 +0100
6844 @@ -0,0 +1,53 @@
6845 +/* SPDX-License-Identifier: GPL-2.0 */
6846 +/*
6847 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6848 + *
6849 + * This program is free software; you can redistribute it and/or modify
6850 + * it under the terms of the GNU General Public License as published by
6851 + * the Free Software Foundation; either version 2 of the License, or
6852 + * (at your option) any later version.
6853 + *
6854 + * This program is distributed in the hope that it will be useful,
6855 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6856 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6857 + * GNU General Public License for more details.
6858 + *
6859 + * You should have received a copy of the GNU General Public License
6860 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6861 + */
6862 +
6863 +/*
6864 + * debugfs interface
6865 + */
6866 +
6867 +#ifndef __DBGAUFS_H__
6868 +#define __DBGAUFS_H__
6869 +
6870 +#ifdef __KERNEL__
6871 +
6872 +struct super_block;
6873 +struct au_sbinfo;
6874 +struct au_branch;
6875 +
6876 +#ifdef CONFIG_DEBUG_FS
6877 +/* dbgaufs.c */
6878 +void dbgaufs_xino_del(struct au_branch *br);
6879 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6880 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6881 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6882 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6883 +void dbgaufs_fin(void);
6884 +int __init dbgaufs_init(void);
6885 +#else
6886 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6887 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6888 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6889 +          int topdown)
6890 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6891 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6892 +AuStubVoid(dbgaufs_fin, void)
6893 +AuStubInt0(__init dbgaufs_init, void)
6894 +#endif /* CONFIG_DEBUG_FS */
6895 +
6896 +#endif /* __KERNEL__ */
6897 +#endif /* __DBGAUFS_H__ */
6898 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6899 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6900 +++ linux/fs/aufs/dcsub.c       2022-11-05 23:02:18.962555950 +0100
6901 @@ -0,0 +1,225 @@
6902 +// SPDX-License-Identifier: GPL-2.0
6903 +/*
6904 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6905 + *
6906 + * This program is free software; you can redistribute it and/or modify
6907 + * it under the terms of the GNU General Public License as published by
6908 + * the Free Software Foundation; either version 2 of the License, or
6909 + * (at your option) any later version.
6910 + *
6911 + * This program is distributed in the hope that it will be useful,
6912 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6913 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6914 + * GNU General Public License for more details.
6915 + *
6916 + * You should have received a copy of the GNU General Public License
6917 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6918 + */
6919 +
6920 +/*
6921 + * sub-routines for dentry cache
6922 + */
6923 +
6924 +#include "aufs.h"
6925 +
6926 +static void au_dpage_free(struct au_dpage *dpage)
6927 +{
6928 +       int i;
6929 +       struct dentry **p;
6930 +
6931 +       p = dpage->dentries;
6932 +       for (i = 0; i < dpage->ndentry; i++)
6933 +               dput(*p++);
6934 +       free_page((unsigned long)dpage->dentries);
6935 +}
6936 +
6937 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6938 +{
6939 +       int err;
6940 +       void *p;
6941 +
6942 +       err = -ENOMEM;
6943 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6944 +       if (unlikely(!dpages->dpages))
6945 +               goto out;
6946 +
6947 +       p = (void *)__get_free_page(gfp);
6948 +       if (unlikely(!p))
6949 +               goto out_dpages;
6950 +
6951 +       dpages->dpages[0].ndentry = 0;
6952 +       dpages->dpages[0].dentries = p;
6953 +       dpages->ndpage = 1;
6954 +       return 0; /* success */
6955 +
6956 +out_dpages:
6957 +       au_kfree_try_rcu(dpages->dpages);
6958 +out:
6959 +       return err;
6960 +}
6961 +
6962 +void au_dpages_free(struct au_dcsub_pages *dpages)
6963 +{
6964 +       int i;
6965 +       struct au_dpage *p;
6966 +
6967 +       p = dpages->dpages;
6968 +       for (i = 0; i < dpages->ndpage; i++)
6969 +               au_dpage_free(p++);
6970 +       au_kfree_try_rcu(dpages->dpages);
6971 +}
6972 +
6973 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6974 +                           struct dentry *dentry, gfp_t gfp)
6975 +{
6976 +       int err, sz;
6977 +       struct au_dpage *dpage;
6978 +       void *p;
6979 +
6980 +       dpage = dpages->dpages + dpages->ndpage - 1;
6981 +       sz = PAGE_SIZE / sizeof(dentry);
6982 +       if (unlikely(dpage->ndentry >= sz)) {
6983 +               AuLabel(new dpage);
6984 +               err = -ENOMEM;
6985 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6986 +               p = au_kzrealloc(dpages->dpages, sz,
6987 +                                sz + sizeof(*dpages->dpages), gfp,
6988 +                                /*may_shrink*/0);
6989 +               if (unlikely(!p))
6990 +                       goto out;
6991 +
6992 +               dpages->dpages = p;
6993 +               dpage = dpages->dpages + dpages->ndpage;
6994 +               p = (void *)__get_free_page(gfp);
6995 +               if (unlikely(!p))
6996 +                       goto out;
6997 +
6998 +               dpage->ndentry = 0;
6999 +               dpage->dentries = p;
7000 +               dpages->ndpage++;
7001 +       }
7002 +
7003 +       AuDebugOn(au_dcount(dentry) <= 0);
7004 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7005 +       return 0; /* success */
7006 +
7007 +out:
7008 +       return err;
7009 +}
7010 +
7011 +/* todo: BAD approach */
7012 +/* copied from linux/fs/dcache.c */
7013 +enum d_walk_ret {
7014 +       D_WALK_CONTINUE,
7015 +       D_WALK_QUIT,
7016 +       D_WALK_NORETRY,
7017 +       D_WALK_SKIP,
7018 +};
7019 +
7020 +extern void d_walk(struct dentry *parent, void *data,
7021 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7022 +
7023 +struct ac_dpages_arg {
7024 +       int err;
7025 +       struct au_dcsub_pages *dpages;
7026 +       struct super_block *sb;
7027 +       au_dpages_test test;
7028 +       void *arg;
7029 +};
7030 +
7031 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7032 +{
7033 +       enum d_walk_ret ret;
7034 +       struct ac_dpages_arg *arg = _arg;
7035 +
7036 +       ret = D_WALK_CONTINUE;
7037 +       if (dentry->d_sb == arg->sb
7038 +           && !IS_ROOT(dentry)
7039 +           && au_dcount(dentry) > 0
7040 +           && au_di(dentry)
7041 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7042 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7043 +               if (unlikely(arg->err))
7044 +                       ret = D_WALK_QUIT;
7045 +       }
7046 +
7047 +       return ret;
7048 +}
7049 +
7050 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7051 +                  au_dpages_test test, void *arg)
7052 +{
7053 +       struct ac_dpages_arg args = {
7054 +               .err    = 0,
7055 +               .dpages = dpages,
7056 +               .sb     = root->d_sb,
7057 +               .test   = test,
7058 +               .arg    = arg
7059 +       };
7060 +
7061 +       d_walk(root, &args, au_call_dpages_append);
7062 +
7063 +       return args.err;
7064 +}
7065 +
7066 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7067 +                      int do_include, au_dpages_test test, void *arg)
7068 +{
7069 +       int err;
7070 +
7071 +       err = 0;
7072 +       write_seqlock(&rename_lock);
7073 +       spin_lock(&dentry->d_lock);
7074 +       if (do_include
7075 +           && au_dcount(dentry) > 0
7076 +           && (!test || test(dentry, arg)))
7077 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7078 +       spin_unlock(&dentry->d_lock);
7079 +       if (unlikely(err))
7080 +               goto out;
7081 +
7082 +       /*
7083 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7084 +        * mount
7085 +        */
7086 +       while (!IS_ROOT(dentry)) {
7087 +               dentry = dentry->d_parent; /* rename_lock is locked */
7088 +               spin_lock(&dentry->d_lock);
7089 +               if (au_dcount(dentry) > 0
7090 +                   && (!test || test(dentry, arg)))
7091 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7092 +               spin_unlock(&dentry->d_lock);
7093 +               if (unlikely(err))
7094 +                       break;
7095 +       }
7096 +
7097 +out:
7098 +       write_sequnlock(&rename_lock);
7099 +       return err;
7100 +}
7101 +
7102 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7103 +{
7104 +       return au_di(dentry) && dentry->d_sb == arg;
7105 +}
7106 +
7107 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7108 +                           struct dentry *dentry, int do_include)
7109 +{
7110 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7111 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7112 +}
7113 +
7114 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7115 +{
7116 +       struct path path[2] = {
7117 +               {
7118 +                       .dentry = d1
7119 +               },
7120 +               {
7121 +                       .dentry = d2
7122 +               }
7123 +       };
7124 +
7125 +       return path_is_under(path + 0, path + 1);
7126 +}
7127 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7128 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7129 +++ linux/fs/aufs/dcsub.h       2022-11-05 23:02:18.962555950 +0100
7130 @@ -0,0 +1,137 @@
7131 +/* SPDX-License-Identifier: GPL-2.0 */
7132 +/*
7133 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7134 + *
7135 + * This program is free software; you can redistribute it and/or modify
7136 + * it under the terms of the GNU General Public License as published by
7137 + * the Free Software Foundation; either version 2 of the License, or
7138 + * (at your option) any later version.
7139 + *
7140 + * This program is distributed in the hope that it will be useful,
7141 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7142 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7143 + * GNU General Public License for more details.
7144 + *
7145 + * You should have received a copy of the GNU General Public License
7146 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7147 + */
7148 +
7149 +/*
7150 + * sub-routines for dentry cache
7151 + */
7152 +
7153 +#ifndef __AUFS_DCSUB_H__
7154 +#define __AUFS_DCSUB_H__
7155 +
7156 +#ifdef __KERNEL__
7157 +
7158 +#include <linux/dcache.h>
7159 +#include <linux/fs.h>
7160 +
7161 +struct au_dpage {
7162 +       int ndentry;
7163 +       struct dentry **dentries;
7164 +};
7165 +
7166 +struct au_dcsub_pages {
7167 +       int ndpage;
7168 +       struct au_dpage *dpages;
7169 +};
7170 +
7171 +/* ---------------------------------------------------------------------- */
7172 +
7173 +/* dcsub.c */
7174 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7175 +void au_dpages_free(struct au_dcsub_pages *dpages);
7176 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7177 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7178 +                  au_dpages_test test, void *arg);
7179 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7180 +                      int do_include, au_dpages_test test, void *arg);
7181 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7182 +                           struct dentry *dentry, int do_include);
7183 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7184 +
7185 +/* ---------------------------------------------------------------------- */
7186 +
7187 +/*
7188 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7189 + * include/linux/dcache.h. Try them (in the future).
7190 + */
7191 +
7192 +static inline int au_d_hashed_positive(struct dentry *d)
7193 +{
7194 +       int err;
7195 +       struct inode *inode = d_inode(d);
7196 +
7197 +       err = 0;
7198 +       if (unlikely(d_unhashed(d)
7199 +                    || d_is_negative(d)
7200 +                    || !inode->i_nlink))
7201 +               err = -ENOENT;
7202 +       return err;
7203 +}
7204 +
7205 +static inline int au_d_linkable(struct dentry *d)
7206 +{
7207 +       int err;
7208 +       struct inode *inode = d_inode(d);
7209 +
7210 +       err = au_d_hashed_positive(d);
7211 +       if (err
7212 +           && d_is_positive(d)
7213 +           && (inode->i_state & I_LINKABLE))
7214 +               err = 0;
7215 +       return err;
7216 +}
7217 +
7218 +static inline int au_d_alive(struct dentry *d)
7219 +{
7220 +       int err;
7221 +       struct inode *inode;
7222 +
7223 +       err = 0;
7224 +       if (!IS_ROOT(d))
7225 +               err = au_d_hashed_positive(d);
7226 +       else {
7227 +               inode = d_inode(d);
7228 +               if (unlikely(d_unlinked(d)
7229 +                            || d_is_negative(d)
7230 +                            || !inode->i_nlink))
7231 +                       err = -ENOENT;
7232 +       }
7233 +       return err;
7234 +}
7235 +
7236 +static inline int au_alive_dir(struct dentry *d)
7237 +{
7238 +       int err;
7239 +
7240 +       err = au_d_alive(d);
7241 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7242 +               err = -ENOENT;
7243 +       return err;
7244 +}
7245 +
7246 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7247 +{
7248 +       return a->len == b->len
7249 +               && !memcmp(a->name, b->name, a->len);
7250 +}
7251 +
7252 +/*
7253 + * by the commit
7254 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7255 + *                     taking d_lock
7256 + * the type of d_lockref.count became int, but the inlined function d_count()
7257 + * still returns unsigned int.
7258 + * I don't know why. Maybe it is for every d_count() users?
7259 + * Anyway au_dcount() lives on.
7260 + */
7261 +static inline int au_dcount(struct dentry *d)
7262 +{
7263 +       return (int)d_count(d);
7264 +}
7265 +
7266 +#endif /* __KERNEL__ */
7267 +#endif /* __AUFS_DCSUB_H__ */
7268 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7269 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7270 +++ linux/fs/aufs/debug.c       2023-09-03 02:21:58.859971007 +0200
7271 @@ -0,0 +1,446 @@
7272 +// SPDX-License-Identifier: GPL-2.0
7273 +/*
7274 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7275 + *
7276 + * This program is free software; you can redistribute it and/or modify
7277 + * it under the terms of the GNU General Public License as published by
7278 + * the Free Software Foundation; either version 2 of the License, or
7279 + * (at your option) any later version.
7280 + *
7281 + * This program is distributed in the hope that it will be useful,
7282 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7283 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7284 + * GNU General Public License for more details.
7285 + *
7286 + * You should have received a copy of the GNU General Public License
7287 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7288 + */
7289 +
7290 +/*
7291 + * debug print functions
7292 + */
7293 +
7294 +#include <linux/iversion.h>
7295 +#include "aufs.h"
7296 +
7297 +/* Returns 0, or -errno.  arg is in kp->arg. */
7298 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7299 +{
7300 +       int err, n;
7301 +
7302 +       err = kstrtoint(val, 0, &n);
7303 +       if (!err) {
7304 +               if (n > 0)
7305 +                       au_debug_on();
7306 +               else
7307 +                       au_debug_off();
7308 +       }
7309 +       return err;
7310 +}
7311 +
7312 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7313 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7314 +{
7315 +       atomic_t *a;
7316 +
7317 +       a = kp->arg;
7318 +       return sprintf(buffer, "%d", atomic_read(a));
7319 +}
7320 +
7321 +static const struct kernel_param_ops param_ops_atomic_t = {
7322 +       .set = param_atomic_t_set,
7323 +       .get = param_atomic_t_get
7324 +       /* void (*free)(void *arg) */
7325 +};
7326 +
7327 +atomic_t aufs_debug = ATOMIC_INIT(0);
7328 +MODULE_PARM_DESC(debug, "debug print");
7329 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7330 +
7331 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7332 +char *au_plevel = KERN_DEBUG;
7333 +#define dpri(fmt, ...) do {                                    \
7334 +       if ((au_plevel                                          \
7335 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7336 +           || au_debug_test())                                 \
7337 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7338 +} while (0)
7339 +
7340 +/* ---------------------------------------------------------------------- */
7341 +
7342 +void au_dpri_whlist(struct au_nhash *whlist)
7343 +{
7344 +       unsigned long ul, n;
7345 +       struct hlist_head *head;
7346 +       struct au_vdir_wh *pos;
7347 +
7348 +       n = whlist->nh_num;
7349 +       head = whlist->nh_head;
7350 +       for (ul = 0; ul < n; ul++) {
7351 +               hlist_for_each_entry(pos, head, wh_hash)
7352 +                       dpri("b%d, %.*s, %d\n",
7353 +                            pos->wh_bindex,
7354 +                            pos->wh_str.len, pos->wh_str.name,
7355 +                            pos->wh_str.len);
7356 +               head++;
7357 +       }
7358 +}
7359 +
7360 +void au_dpri_vdir(struct au_vdir *vdir)
7361 +{
7362 +       unsigned long ul;
7363 +       union au_vdir_deblk_p p;
7364 +       unsigned char *o;
7365 +
7366 +       if (!vdir || IS_ERR(vdir)) {
7367 +               dpri("err %ld\n", PTR_ERR(vdir));
7368 +               return;
7369 +       }
7370 +
7371 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7372 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7373 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7374 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7375 +               p.deblk = vdir->vd_deblk[ul];
7376 +               o = p.deblk;
7377 +               dpri("[%lu]: %p\n", ul, o);
7378 +       }
7379 +}
7380 +
7381 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7382 +                       struct dentry *wh)
7383 +{
7384 +       char *n = NULL;
7385 +       int l = 0;
7386 +
7387 +       if (!inode || IS_ERR(inode)) {
7388 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7389 +               return -1;
7390 +       }
7391 +
7392 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7393 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7394 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7395 +       if (wh) {
7396 +               n = (void *)wh->d_name.name;
7397 +               l = wh->d_name.len;
7398 +       }
7399 +
7400 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7401 +            " acl %p, def_acl %p,"
7402 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7403 +            bindex, inode,
7404 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7405 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7406 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7407 +            inode->i_acl, inode->i_default_acl,
7408 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7409 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7410 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7411 +            inode->i_generation,
7412 +            l ? ", wh " : "", l, n);
7413 +       return 0;
7414 +}
7415 +
7416 +void au_dpri_inode(struct inode *inode)
7417 +{
7418 +       struct au_iinfo *iinfo;
7419 +       struct au_hinode *hi;
7420 +       aufs_bindex_t bindex;
7421 +       int err, hn;
7422 +
7423 +       err = do_pri_inode(-1, inode, -1, NULL);
7424 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7425 +               return;
7426 +
7427 +       iinfo = au_ii(inode);
7428 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7429 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7430 +       if (iinfo->ii_btop < 0)
7431 +               return;
7432 +       hn = 0;
7433 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7434 +               hi = au_hinode(iinfo, bindex);
7435 +               hn = !!au_hn(hi);
7436 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7437 +       }
7438 +}
7439 +
7440 +void au_dpri_dalias(struct inode *inode)
7441 +{
7442 +       struct dentry *d;
7443 +
7444 +       spin_lock(&inode->i_lock);
7445 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7446 +               au_dpri_dentry(d);
7447 +       spin_unlock(&inode->i_lock);
7448 +}
7449 +
7450 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7451 +{
7452 +       struct dentry *wh = NULL;
7453 +       int hn;
7454 +       struct inode *inode;
7455 +       struct au_iinfo *iinfo;
7456 +       struct au_hinode *hi;
7457 +
7458 +       if (!dentry || IS_ERR(dentry)) {
7459 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7460 +               return -1;
7461 +       }
7462 +       /* do not call dget_parent() here */
7463 +       /* note: access d_xxx without d_lock */
7464 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7465 +            bindex, dentry, dentry,
7466 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7467 +            au_dcount(dentry), dentry->d_flags,
7468 +            d_unhashed(dentry) ? "un" : "");
7469 +       hn = -1;
7470 +       inode = NULL;
7471 +       if (d_is_positive(dentry))
7472 +               inode = d_inode(dentry);
7473 +       if (inode
7474 +           && au_test_aufs(dentry->d_sb)
7475 +           && bindex >= 0
7476 +           && !au_is_bad_inode(inode)) {
7477 +               iinfo = au_ii(inode);
7478 +               hi = au_hinode(iinfo, bindex);
7479 +               hn = !!au_hn(hi);
7480 +               wh = hi->hi_whdentry;
7481 +       }
7482 +       do_pri_inode(bindex, inode, hn, wh);
7483 +       return 0;
7484 +}
7485 +
7486 +void au_dpri_dentry(struct dentry *dentry)
7487 +{
7488 +       struct au_dinfo *dinfo;
7489 +       aufs_bindex_t bindex;
7490 +       int err;
7491 +
7492 +       err = do_pri_dentry(-1, dentry);
7493 +       if (err || !au_test_aufs(dentry->d_sb))
7494 +               return;
7495 +
7496 +       dinfo = au_di(dentry);
7497 +       if (!dinfo)
7498 +               return;
7499 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7500 +            dinfo->di_btop, dinfo->di_bbot,
7501 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7502 +            dinfo->di_tmpfile);
7503 +       if (dinfo->di_btop < 0)
7504 +               return;
7505 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7506 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7507 +}
7508 +
7509 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7510 +{
7511 +       char a[32];
7512 +
7513 +       if (!file || IS_ERR(file)) {
7514 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7515 +               return -1;
7516 +       }
7517 +       a[0] = 0;
7518 +       if (bindex < 0
7519 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7520 +           && au_test_aufs(file->f_path.dentry->d_sb)
7521 +           && au_fi(file))
7522 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7523 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7524 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7525 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7526 +            file->f_version, file->f_pos, a);
7527 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7528 +               do_pri_dentry(bindex, file->f_path.dentry);
7529 +       return 0;
7530 +}
7531 +
7532 +void au_dpri_file(struct file *file)
7533 +{
7534 +       struct au_finfo *finfo;
7535 +       struct au_fidir *fidir;
7536 +       struct au_hfile *hfile;
7537 +       aufs_bindex_t bindex;
7538 +       int err;
7539 +
7540 +       err = do_pri_file(-1, file);
7541 +       if (err
7542 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7543 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7544 +               return;
7545 +
7546 +       finfo = au_fi(file);
7547 +       if (!finfo)
7548 +               return;
7549 +       if (finfo->fi_btop < 0)
7550 +               return;
7551 +       fidir = finfo->fi_hdir;
7552 +       if (!fidir)
7553 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7554 +       else
7555 +               for (bindex = finfo->fi_btop;
7556 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7557 +                    bindex++) {
7558 +                       hfile = fidir->fd_hfile + bindex;
7559 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7560 +               }
7561 +}
7562 +
7563 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7564 +{
7565 +       struct vfsmount *mnt;
7566 +       struct super_block *sb;
7567 +
7568 +       if (!br || IS_ERR(br))
7569 +               goto out;
7570 +       mnt = au_br_mnt(br);
7571 +       if (!mnt || IS_ERR(mnt))
7572 +               goto out;
7573 +       sb = mnt->mnt_sb;
7574 +       if (!sb || IS_ERR(sb))
7575 +               goto out;
7576 +
7577 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7578 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7579 +            "xino %d\n",
7580 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7581 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7582 +            sb->s_flags, sb->s_count,
7583 +            atomic_read(&sb->s_active),
7584 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7585 +       return 0;
7586 +
7587 +out:
7588 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7589 +       return -1;
7590 +}
7591 +
7592 +void au_dpri_sb(struct super_block *sb)
7593 +{
7594 +       struct au_sbinfo *sbinfo;
7595 +       aufs_bindex_t bindex;
7596 +       int err;
7597 +       /* to reduce stack size */
7598 +       struct {
7599 +               struct vfsmount mnt;
7600 +               struct au_branch fake;
7601 +       } *a;
7602 +
7603 +       /* this function can be called from magic sysrq */
7604 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7605 +       if (unlikely(!a)) {
7606 +               dpri("no memory\n");
7607 +               return;
7608 +       }
7609 +
7610 +       a->mnt.mnt_sb = sb;
7611 +       a->fake.br_path.mnt = &a->mnt;
7612 +       err = do_pri_br(-1, &a->fake);
7613 +       au_kfree_rcu(a);
7614 +       dpri("dev 0x%x\n", sb->s_dev);
7615 +       if (err || !au_test_aufs(sb))
7616 +               return;
7617 +
7618 +       sbinfo = au_sbi(sb);
7619 +       if (!sbinfo)
7620 +               return;
7621 +       dpri("nw %d, gen %u, kobj %d\n",
7622 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7623 +            kref_read(&sbinfo->si_kobj.kref));
7624 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7625 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7626 +}
7627 +
7628 +/* ---------------------------------------------------------------------- */
7629 +
7630 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7631 +{
7632 +       struct inode *h_inode, *inode = d_inode(dentry);
7633 +       struct dentry *h_dentry;
7634 +       aufs_bindex_t bindex, bbot, bi;
7635 +
7636 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7637 +               return;
7638 +
7639 +       bbot = au_dbbot(dentry);
7640 +       bi = au_ibbot(inode);
7641 +       if (bi < bbot)
7642 +               bbot = bi;
7643 +       bindex = au_dbtop(dentry);
7644 +       bi = au_ibtop(inode);
7645 +       if (bi > bindex)
7646 +               bindex = bi;
7647 +
7648 +       for (; bindex <= bbot; bindex++) {
7649 +               h_dentry = au_h_dptr(dentry, bindex);
7650 +               if (!h_dentry)
7651 +                       continue;
7652 +               h_inode = au_h_iptr(inode, bindex);
7653 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7654 +                       au_debug_on();
7655 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7656 +                       AuDbgDentry(dentry);
7657 +                       AuDbgInode(inode);
7658 +                       au_debug_off();
7659 +                       if (au_test_fuse(h_inode->i_sb))
7660 +                               WARN_ON_ONCE(1);
7661 +                       else
7662 +                               BUG();
7663 +               }
7664 +       }
7665 +}
7666 +
7667 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7668 +{
7669 +       int err, i, j;
7670 +       struct au_dcsub_pages dpages;
7671 +       struct au_dpage *dpage;
7672 +       struct dentry **dentries;
7673 +
7674 +       err = au_dpages_init(&dpages, GFP_NOFS);
7675 +       AuDebugOn(err);
7676 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7677 +       AuDebugOn(err);
7678 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7679 +               dpage = dpages.dpages + i;
7680 +               dentries = dpage->dentries;
7681 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7682 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7683 +       }
7684 +       au_dpages_free(&dpages);
7685 +}
7686 +
7687 +void au_dbg_verify_kthread(void)
7688 +{
7689 +       if (au_wkq_test()) {
7690 +               au_dbg_blocked();
7691 +               /*
7692 +                * It may be recursive, but udba=notify between two aufs mounts,
7693 +                * where a single ro branch is shared, is not a problem.
7694 +                */
7695 +               /* WARN_ON(1); */
7696 +       }
7697 +}
7698 +
7699 +/* ---------------------------------------------------------------------- */
7700 +
7701 +int __init au_debug_init(void)
7702 +{
7703 +       aufs_bindex_t bindex;
7704 +       struct au_vdir_destr destr;
7705 +
7706 +       bindex = -1;
7707 +       AuDebugOn(bindex >= 0);
7708 +
7709 +       destr.len = -1;
7710 +       AuDebugOn(destr.len < NAME_MAX);
7711 +
7712 +#ifdef CONFIG_4KSTACKS
7713 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7714 +#endif
7715 +
7716 +       return 0;
7717 +}
7718 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7719 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7720 +++ linux/fs/aufs/debug.h       2022-11-05 23:02:18.962555950 +0100
7721 @@ -0,0 +1,226 @@
7722 +/* SPDX-License-Identifier: GPL-2.0 */
7723 +/*
7724 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7725 + *
7726 + * This program is free software; you can redistribute it and/or modify
7727 + * it under the terms of the GNU General Public License as published by
7728 + * the Free Software Foundation; either version 2 of the License, or
7729 + * (at your option) any later version.
7730 + *
7731 + * This program is distributed in the hope that it will be useful,
7732 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7733 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7734 + * GNU General Public License for more details.
7735 + *
7736 + * You should have received a copy of the GNU General Public License
7737 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7738 + */
7739 +
7740 +/*
7741 + * debug print functions
7742 + */
7743 +
7744 +#ifndef __AUFS_DEBUG_H__
7745 +#define __AUFS_DEBUG_H__
7746 +
7747 +#ifdef __KERNEL__
7748 +
7749 +#include <linux/atomic.h>
7750 +#include <linux/module.h>
7751 +#include <linux/kallsyms.h>
7752 +#include <linux/sysrq.h>
7753 +
7754 +#ifdef CONFIG_AUFS_DEBUG
7755 +#define AuDebugOn(a)           BUG_ON(a)
7756 +
7757 +/* module parameter */
7758 +extern atomic_t aufs_debug;
7759 +static inline void au_debug_on(void)
7760 +{
7761 +       atomic_inc(&aufs_debug);
7762 +}
7763 +static inline void au_debug_off(void)
7764 +{
7765 +       atomic_dec_if_positive(&aufs_debug);
7766 +}
7767 +
7768 +static inline int au_debug_test(void)
7769 +{
7770 +       return atomic_read(&aufs_debug) > 0;
7771 +}
7772 +#else
7773 +#define AuDebugOn(a)           do {} while (0)
7774 +AuStubVoid(au_debug_on, void)
7775 +AuStubVoid(au_debug_off, void)
7776 +AuStubInt0(au_debug_test, void)
7777 +#endif /* CONFIG_AUFS_DEBUG */
7778 +
7779 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7780 +
7781 +/* ---------------------------------------------------------------------- */
7782 +
7783 +/* debug print */
7784 +
7785 +#define AuDbg(fmt, ...) do { \
7786 +       if (au_debug_test()) \
7787 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7788 +} while (0)
7789 +#define AuLabel(l)             AuDbg(#l "\n")
7790 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7791 +#define AuWarn1(fmt, ...) do { \
7792 +       static unsigned char _c; \
7793 +       if (!_c++) \
7794 +               pr_warn(fmt, ##__VA_ARGS__); \
7795 +} while (0)
7796 +
7797 +#define AuErr1(fmt, ...) do { \
7798 +       static unsigned char _c; \
7799 +       if (!_c++) \
7800 +               pr_err(fmt, ##__VA_ARGS__); \
7801 +} while (0)
7802 +
7803 +#define AuIOErr1(fmt, ...) do { \
7804 +       static unsigned char _c; \
7805 +       if (!_c++) \
7806 +               AuIOErr(fmt, ##__VA_ARGS__); \
7807 +} while (0)
7808 +
7809 +#define AuUnsupportMsg "This operation is not supported." \
7810 +                       " Please report this application to aufs-users ML."
7811 +#define AuUnsupport(fmt, ...) do { \
7812 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7813 +       dump_stack(); \
7814 +} while (0)
7815 +
7816 +#define AuTraceErr(e) do { \
7817 +       if (unlikely((e) < 0)) \
7818 +               AuDbg("err %d\n", (int)(e)); \
7819 +} while (0)
7820 +
7821 +#define AuTraceErrPtr(p) do { \
7822 +       if (IS_ERR(p)) \
7823 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7824 +} while (0)
7825 +
7826 +/* dirty macros for debug print, use with "%.*s" and caution */
7827 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7828 +
7829 +/* ---------------------------------------------------------------------- */
7830 +
7831 +struct dentry;
7832 +#ifdef CONFIG_AUFS_DEBUG
7833 +extern struct mutex au_dbg_mtx;
7834 +extern char *au_plevel;
7835 +struct au_nhash;
7836 +void au_dpri_whlist(struct au_nhash *whlist);
7837 +struct au_vdir;
7838 +void au_dpri_vdir(struct au_vdir *vdir);
7839 +struct inode;
7840 +void au_dpri_inode(struct inode *inode);
7841 +void au_dpri_dalias(struct inode *inode);
7842 +void au_dpri_dentry(struct dentry *dentry);
7843 +struct file;
7844 +void au_dpri_file(struct file *filp);
7845 +struct super_block;
7846 +void au_dpri_sb(struct super_block *sb);
7847 +
7848 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7849 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7850 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7851 +void au_dbg_verify_kthread(void);
7852 +
7853 +int __init au_debug_init(void);
7854 +
7855 +#define AuDbgWhlist(w) do { \
7856 +       mutex_lock(&au_dbg_mtx); \
7857 +       AuDbg(#w "\n"); \
7858 +       au_dpri_whlist(w); \
7859 +       mutex_unlock(&au_dbg_mtx); \
7860 +} while (0)
7861 +
7862 +#define AuDbgVdir(v) do { \
7863 +       mutex_lock(&au_dbg_mtx); \
7864 +       AuDbg(#v "\n"); \
7865 +       au_dpri_vdir(v); \
7866 +       mutex_unlock(&au_dbg_mtx); \
7867 +} while (0)
7868 +
7869 +#define AuDbgInode(i) do { \
7870 +       mutex_lock(&au_dbg_mtx); \
7871 +       AuDbg(#i "\n"); \
7872 +       au_dpri_inode(i); \
7873 +       mutex_unlock(&au_dbg_mtx); \
7874 +} while (0)
7875 +
7876 +#define AuDbgDAlias(i) do { \
7877 +       mutex_lock(&au_dbg_mtx); \
7878 +       AuDbg(#i "\n"); \
7879 +       au_dpri_dalias(i); \
7880 +       mutex_unlock(&au_dbg_mtx); \
7881 +} while (0)
7882 +
7883 +#define AuDbgDentry(d) do { \
7884 +       mutex_lock(&au_dbg_mtx); \
7885 +       AuDbg(#d "\n"); \
7886 +       au_dpri_dentry(d); \
7887 +       mutex_unlock(&au_dbg_mtx); \
7888 +} while (0)
7889 +
7890 +#define AuDbgFile(f) do { \
7891 +       mutex_lock(&au_dbg_mtx); \
7892 +       AuDbg(#f "\n"); \
7893 +       au_dpri_file(f); \
7894 +       mutex_unlock(&au_dbg_mtx); \
7895 +} while (0)
7896 +
7897 +#define AuDbgSb(sb) do { \
7898 +       mutex_lock(&au_dbg_mtx); \
7899 +       AuDbg(#sb "\n"); \
7900 +       au_dpri_sb(sb); \
7901 +       mutex_unlock(&au_dbg_mtx); \
7902 +} while (0)
7903 +
7904 +#define AuDbgSym(addr) do {                            \
7905 +       char sym[KSYM_SYMBOL_LEN];                      \
7906 +       sprint_symbol(sym, (unsigned long)addr);        \
7907 +       AuDbg("%s\n", sym);                             \
7908 +} while (0)
7909 +#else
7910 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7911 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7912 +AuStubVoid(au_dbg_verify_kthread, void)
7913 +AuStubInt0(__init au_debug_init, void)
7914 +
7915 +#define AuDbgWhlist(w)         do {} while (0)
7916 +#define AuDbgVdir(v)           do {} while (0)
7917 +#define AuDbgInode(i)          do {} while (0)
7918 +#define AuDbgDAlias(i)         do {} while (0)
7919 +#define AuDbgDentry(d)         do {} while (0)
7920 +#define AuDbgFile(f)           do {} while (0)
7921 +#define AuDbgSb(sb)            do {} while (0)
7922 +#define AuDbgSym(addr)         do {} while (0)
7923 +#endif /* CONFIG_AUFS_DEBUG */
7924 +
7925 +/* ---------------------------------------------------------------------- */
7926 +
7927 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7928 +int __init au_sysrq_init(void);
7929 +void au_sysrq_fin(void);
7930 +
7931 +#ifdef CONFIG_HW_CONSOLE
7932 +#define au_dbg_blocked() do { \
7933 +       WARN_ON(1); \
7934 +       handle_sysrq('w'); \
7935 +} while (0)
7936 +#else
7937 +AuStubVoid(au_dbg_blocked, void)
7938 +#endif
7939 +
7940 +#else
7941 +AuStubInt0(__init au_sysrq_init, void)
7942 +AuStubVoid(au_sysrq_fin, void)
7943 +AuStubVoid(au_dbg_blocked, void)
7944 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7945 +
7946 +#endif /* __KERNEL__ */
7947 +#endif /* __AUFS_DEBUG_H__ */
7948 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7949 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7950 +++ linux/fs/aufs/dentry.c      2023-09-03 02:21:58.859971007 +0200
7951 @@ -0,0 +1,1168 @@
7952 +// SPDX-License-Identifier: GPL-2.0
7953 +/*
7954 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7955 + *
7956 + * This program is free software; you can redistribute it and/or modify
7957 + * it under the terms of the GNU General Public License as published by
7958 + * the Free Software Foundation; either version 2 of the License, or
7959 + * (at your option) any later version.
7960 + *
7961 + * This program is distributed in the hope that it will be useful,
7962 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7963 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7964 + * GNU General Public License for more details.
7965 + *
7966 + * You should have received a copy of the GNU General Public License
7967 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7968 + */
7969 +
7970 +/*
7971 + * lookup and dentry operations
7972 + */
7973 +
7974 +#include <linux/iversion.h>
7975 +#include "aufs.h"
7976 +
7977 +/*
7978 + * returns positive/negative dentry, NULL or an error.
7979 + * NULL means whiteout-ed or not-found.
7980 + */
7981 +static struct dentry*
7982 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7983 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7984 +{
7985 +       struct dentry *h_dentry;
7986 +       struct inode *h_inode;
7987 +       struct au_branch *br;
7988 +       struct mnt_idmap *h_idmap;
7989 +       struct path h_path;
7990 +       int wh_found, opq;
7991 +       unsigned char wh_able;
7992 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7993 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7994 +                                                         IGNORE_PERM);
7995 +
7996 +       wh_found = 0;
7997 +       br = au_sbr(dentry->d_sb, bindex);
7998 +       h_path.dentry = h_parent;
7999 +       h_path.mnt = au_br_mnt(br);
8000 +       h_idmap = au_br_idmap(br);
8001 +       wh_able = !!au_br_whable(br->br_perm);
8002 +       if (wh_able)
8003 +               wh_found = au_wh_test(h_idmap, &h_path, &args->whname,
8004 +                                     ignore_perm);
8005 +       h_dentry = ERR_PTR(wh_found);
8006 +       if (!wh_found)
8007 +               goto real_lookup;
8008 +       if (unlikely(wh_found < 0))
8009 +               goto out;
8010 +
8011 +       /* We found a whiteout */
8012 +       /* au_set_dbbot(dentry, bindex); */
8013 +       au_set_dbwh(dentry, bindex);
8014 +       if (!allow_neg)
8015 +               return NULL; /* success */
8016 +
8017 +real_lookup:
8018 +       if (!ignore_perm)
8019 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8020 +       else
8021 +               h_dentry = au_sio_lkup_one(h_idmap, args->name, &h_path);
8022 +       if (IS_ERR(h_dentry)) {
8023 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8024 +                   && !allow_neg)
8025 +                       h_dentry = NULL;
8026 +               goto out;
8027 +       }
8028 +
8029 +       h_inode = d_inode(h_dentry);
8030 +       if (d_is_negative(h_dentry)) {
8031 +               if (!allow_neg)
8032 +                       goto out_neg;
8033 +       } else if (wh_found
8034 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8035 +               goto out_neg;
8036 +       else if (au_ftest_lkup(args->flags, DIRREN)
8037 +                /* && h_inode */
8038 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8039 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8040 +                     (unsigned long long)h_inode->i_ino);
8041 +               goto out_neg;
8042 +       }
8043 +
8044 +       if (au_dbbot(dentry) <= bindex)
8045 +               au_set_dbbot(dentry, bindex);
8046 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8047 +               au_set_dbtop(dentry, bindex);
8048 +       au_set_h_dptr(dentry, bindex, h_dentry);
8049 +
8050 +       if (!d_is_dir(h_dentry)
8051 +           || !wh_able
8052 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8053 +               goto out; /* success */
8054 +
8055 +       h_path.dentry = h_dentry;
8056 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8057 +       opq = au_diropq_test(h_idmap, &h_path);
8058 +       inode_unlock_shared(h_inode);
8059 +       if (opq > 0)
8060 +               au_set_dbdiropq(dentry, bindex);
8061 +       else if (unlikely(opq < 0)) {
8062 +               au_set_h_dptr(dentry, bindex, NULL);
8063 +               h_dentry = ERR_PTR(opq);
8064 +       }
8065 +       goto out;
8066 +
8067 +out_neg:
8068 +       dput(h_dentry);
8069 +       h_dentry = NULL;
8070 +out:
8071 +       return h_dentry;
8072 +}
8073 +
8074 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8075 +{
8076 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8077 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8078 +               return -EPERM;
8079 +       return 0;
8080 +}
8081 +
8082 +/*
8083 + * returns the number of lower positive dentries,
8084 + * otherwise an error.
8085 + * can be called at unlinking with @type is zero.
8086 + */
8087 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8088 +                  unsigned int flags)
8089 +{
8090 +       int npositive, err;
8091 +       aufs_bindex_t bindex, btail, bdiropq;
8092 +       unsigned char isdir, dirperm1, dirren;
8093 +       struct au_do_lookup_args args = {
8094 +               .flags          = flags,
8095 +               .name           = &dentry->d_name
8096 +       };
8097 +       struct dentry *parent;
8098 +       struct super_block *sb;
8099 +
8100 +       sb = dentry->d_sb;
8101 +       err = au_test_shwh(sb, args.name);
8102 +       if (unlikely(err))
8103 +               goto out;
8104 +
8105 +       err = au_wh_name_alloc(&args.whname, args.name);
8106 +       if (unlikely(err))
8107 +               goto out;
8108 +
8109 +       isdir = !!d_is_dir(dentry);
8110 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8111 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8112 +       if (dirren)
8113 +               au_fset_lkup(args.flags, DIRREN);
8114 +
8115 +       npositive = 0;
8116 +       parent = dget_parent(dentry);
8117 +       btail = au_dbtaildir(parent);
8118 +       for (bindex = btop; bindex <= btail; bindex++) {
8119 +               struct dentry *h_parent, *h_dentry;
8120 +               struct inode *h_inode, *h_dir;
8121 +               struct au_branch *br;
8122 +
8123 +               h_dentry = au_h_dptr(dentry, bindex);
8124 +               if (h_dentry) {
8125 +                       if (d_is_positive(h_dentry))
8126 +                               npositive++;
8127 +                       break;
8128 +               }
8129 +               h_parent = au_h_dptr(parent, bindex);
8130 +               if (!h_parent || !d_is_dir(h_parent))
8131 +                       continue;
8132 +
8133 +               if (dirren) {
8134 +                       /* if the inum matches, then use the prepared name */
8135 +                       err = au_dr_lkup_name(&args, bindex);
8136 +                       if (unlikely(err))
8137 +                               goto out_parent;
8138 +               }
8139 +
8140 +               h_dir = d_inode(h_parent);
8141 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8142 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8143 +               inode_unlock_shared(h_dir);
8144 +               err = PTR_ERR(h_dentry);
8145 +               if (IS_ERR(h_dentry))
8146 +                       goto out_parent;
8147 +               if (h_dentry)
8148 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8149 +               if (dirperm1)
8150 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8151 +
8152 +               if (au_dbwh(dentry) == bindex)
8153 +                       break;
8154 +               if (!h_dentry)
8155 +                       continue;
8156 +               if (d_is_negative(h_dentry))
8157 +                       continue;
8158 +               h_inode = d_inode(h_dentry);
8159 +               npositive++;
8160 +               if (!args.type)
8161 +                       args.type = h_inode->i_mode & S_IFMT;
8162 +               if (args.type != S_IFDIR)
8163 +                       break;
8164 +               else if (isdir) {
8165 +                       /* the type of lower may be different */
8166 +                       bdiropq = au_dbdiropq(dentry);
8167 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8168 +                               break;
8169 +               }
8170 +               br = au_sbr(sb, bindex);
8171 +               if (dirren
8172 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8173 +                                          /*add_ent*/NULL)) {
8174 +                       /* prepare next name to lookup */
8175 +                       err = au_dr_lkup(&args, dentry, bindex);
8176 +                       if (unlikely(err))
8177 +                               goto out_parent;
8178 +               }
8179 +       }
8180 +
8181 +       if (npositive) {
8182 +               AuLabel(positive);
8183 +               au_update_dbtop(dentry);
8184 +       }
8185 +       err = npositive;
8186 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8187 +                    && au_dbtop(dentry) < 0)) {
8188 +               err = -EIO;
8189 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8190 +                       dentry, err);
8191 +       }
8192 +
8193 +out_parent:
8194 +       dput(parent);
8195 +       au_kfree_try_rcu(args.whname.name);
8196 +       if (dirren)
8197 +               au_dr_lkup_fin(&args);
8198 +out:
8199 +       return err;
8200 +}
8201 +
8202 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
8203 +                              struct path *ppath)
8204 +{
8205 +       struct dentry *dentry;
8206 +       int wkq_err;
8207 +
8208 +       if (!au_test_h_perm_sio(idmap, d_inode(ppath->dentry), MAY_EXEC))
8209 +               dentry = vfsub_lkup_one(name, ppath);
8210 +       else {
8211 +               struct vfsub_lkup_one_args args = {
8212 +                       .errp   = &dentry,
8213 +                       .name   = name,
8214 +                       .ppath  = ppath
8215 +               };
8216 +
8217 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8218 +               if (unlikely(wkq_err))
8219 +                       dentry = ERR_PTR(wkq_err);
8220 +       }
8221 +
8222 +       return dentry;
8223 +}
8224 +
8225 +/*
8226 + * lookup @dentry on @bindex which should be negative.
8227 + */
8228 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8229 +{
8230 +       int err;
8231 +       struct dentry *parent, *h_dentry;
8232 +       struct au_branch *br;
8233 +       struct mnt_idmap *h_idmap;
8234 +       struct path h_ppath;
8235 +
8236 +       parent = dget_parent(dentry);
8237 +       br = au_sbr(dentry->d_sb, bindex);
8238 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8239 +       h_ppath.mnt = au_br_mnt(br);
8240 +       h_idmap = au_br_idmap(br);
8241 +       if (wh)
8242 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8243 +       else
8244 +               h_dentry = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
8245 +       err = PTR_ERR(h_dentry);
8246 +       if (IS_ERR(h_dentry))
8247 +               goto out;
8248 +       if (unlikely(d_is_positive(h_dentry))) {
8249 +               err = -EIO;
8250 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8251 +               dput(h_dentry);
8252 +               goto out;
8253 +       }
8254 +
8255 +       err = 0;
8256 +       if (bindex < au_dbtop(dentry))
8257 +               au_set_dbtop(dentry, bindex);
8258 +       if (au_dbbot(dentry) < bindex)
8259 +               au_set_dbbot(dentry, bindex);
8260 +       au_set_h_dptr(dentry, bindex, h_dentry);
8261 +
8262 +out:
8263 +       dput(parent);
8264 +       return err;
8265 +}
8266 +
8267 +/* ---------------------------------------------------------------------- */
8268 +
8269 +/* subset of struct inode */
8270 +struct au_iattr {
8271 +       unsigned long           i_ino;
8272 +       /* unsigned int         i_nlink; */
8273 +       kuid_t                  i_uid;
8274 +       kgid_t                  i_gid;
8275 +       u64                     i_version;
8276 +/*
8277 +       loff_t                  i_size;
8278 +       blkcnt_t                i_blocks;
8279 +*/
8280 +       umode_t                 i_mode;
8281 +};
8282 +
8283 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8284 +{
8285 +       ia->i_ino = h_inode->i_ino;
8286 +       /* ia->i_nlink = h_inode->i_nlink; */
8287 +       ia->i_uid = h_inode->i_uid;
8288 +       ia->i_gid = h_inode->i_gid;
8289 +       ia->i_version = inode_query_iversion(h_inode);
8290 +/*
8291 +       ia->i_size = h_inode->i_size;
8292 +       ia->i_blocks = h_inode->i_blocks;
8293 +*/
8294 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8295 +}
8296 +
8297 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8298 +{
8299 +       return ia->i_ino != h_inode->i_ino
8300 +               /* || ia->i_nlink != h_inode->i_nlink */
8301 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8302 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8303 +               || !inode_eq_iversion(h_inode, ia->i_version)
8304 +/*
8305 +               || ia->i_size != h_inode->i_size
8306 +               || ia->i_blocks != h_inode->i_blocks
8307 +*/
8308 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8309 +}
8310 +
8311 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8312 +                             struct au_branch *br)
8313 +{
8314 +       int err;
8315 +       struct au_iattr ia;
8316 +       struct inode *h_inode;
8317 +       struct dentry *h_d;
8318 +       struct super_block *h_sb;
8319 +       struct path h_ppath;
8320 +
8321 +       err = 0;
8322 +       memset(&ia, -1, sizeof(ia));
8323 +       h_sb = h_dentry->d_sb;
8324 +       h_inode = NULL;
8325 +       if (d_is_positive(h_dentry)) {
8326 +               h_inode = d_inode(h_dentry);
8327 +               au_iattr_save(&ia, h_inode);
8328 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8329 +               /* nfs d_revalidate may return 0 for negative dentry */
8330 +               /* fuse d_revalidate always return 0 for negative dentry */
8331 +               goto out;
8332 +
8333 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8334 +       h_ppath.dentry = h_parent;
8335 +       h_ppath.mnt = au_br_mnt(br);
8336 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8337 +       err = PTR_ERR(h_d);
8338 +       if (IS_ERR(h_d))
8339 +               goto out;
8340 +
8341 +       err = 0;
8342 +       if (unlikely(h_d != h_dentry
8343 +                    || d_inode(h_d) != h_inode
8344 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8345 +               err = au_busy_or_stale();
8346 +       dput(h_d);
8347 +
8348 +out:
8349 +       AuTraceErr(err);
8350 +       return err;
8351 +}
8352 +
8353 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8354 +               struct dentry *h_parent, struct au_branch *br)
8355 +{
8356 +       int err;
8357 +
8358 +       err = 0;
8359 +       if (udba == AuOpt_UDBA_REVAL
8360 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8361 +               IMustLock(h_dir);
8362 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8363 +       } else if (udba != AuOpt_UDBA_NONE)
8364 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8365 +
8366 +       return err;
8367 +}
8368 +
8369 +/* ---------------------------------------------------------------------- */
8370 +
8371 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8372 +{
8373 +       int err;
8374 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8375 +       struct au_hdentry tmp, *p, *q;
8376 +       struct au_dinfo *dinfo;
8377 +       struct super_block *sb;
8378 +
8379 +       DiMustWriteLock(dentry);
8380 +
8381 +       sb = dentry->d_sb;
8382 +       dinfo = au_di(dentry);
8383 +       bbot = dinfo->di_bbot;
8384 +       bwh = dinfo->di_bwh;
8385 +       bdiropq = dinfo->di_bdiropq;
8386 +       bindex = dinfo->di_btop;
8387 +       p = au_hdentry(dinfo, bindex);
8388 +       for (; bindex <= bbot; bindex++, p++) {
8389 +               if (!p->hd_dentry)
8390 +                       continue;
8391 +
8392 +               new_bindex = au_br_index(sb, p->hd_id);
8393 +               if (new_bindex == bindex)
8394 +                       continue;
8395 +
8396 +               if (dinfo->di_bwh == bindex)
8397 +                       bwh = new_bindex;
8398 +               if (dinfo->di_bdiropq == bindex)
8399 +                       bdiropq = new_bindex;
8400 +               if (new_bindex < 0) {
8401 +                       au_hdput(p);
8402 +                       p->hd_dentry = NULL;
8403 +                       continue;
8404 +               }
8405 +
8406 +               /* swap two lower dentries, and loop again */
8407 +               q = au_hdentry(dinfo, new_bindex);
8408 +               tmp = *q;
8409 +               *q = *p;
8410 +               *p = tmp;
8411 +               if (tmp.hd_dentry) {
8412 +                       bindex--;
8413 +                       p--;
8414 +               }
8415 +       }
8416 +
8417 +       dinfo->di_bwh = -1;
8418 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8419 +               dinfo->di_bwh = bwh;
8420 +
8421 +       dinfo->di_bdiropq = -1;
8422 +       if (bdiropq >= 0
8423 +           && bdiropq <= au_sbbot(sb)
8424 +           && au_sbr_whable(sb, bdiropq))
8425 +               dinfo->di_bdiropq = bdiropq;
8426 +
8427 +       err = -EIO;
8428 +       dinfo->di_btop = -1;
8429 +       dinfo->di_bbot = -1;
8430 +       bbot = au_dbbot(parent);
8431 +       bindex = 0;
8432 +       p = au_hdentry(dinfo, bindex);
8433 +       for (; bindex <= bbot; bindex++, p++)
8434 +               if (p->hd_dentry) {
8435 +                       dinfo->di_btop = bindex;
8436 +                       break;
8437 +               }
8438 +
8439 +       if (dinfo->di_btop >= 0) {
8440 +               bindex = bbot;
8441 +               p = au_hdentry(dinfo, bindex);
8442 +               for (; bindex >= 0; bindex--, p--)
8443 +                       if (p->hd_dentry) {
8444 +                               dinfo->di_bbot = bindex;
8445 +                               err = 0;
8446 +                               break;
8447 +                       }
8448 +       }
8449 +
8450 +       return err;
8451 +}
8452 +
8453 +static void au_do_hide(struct dentry *dentry)
8454 +{
8455 +       struct inode *inode;
8456 +
8457 +       if (d_really_is_positive(dentry)) {
8458 +               inode = d_inode(dentry);
8459 +               if (!d_is_dir(dentry)) {
8460 +                       if (inode->i_nlink && !d_unhashed(dentry))
8461 +                               drop_nlink(inode);
8462 +               } else {
8463 +                       clear_nlink(inode);
8464 +                       /* stop next lookup */
8465 +                       inode->i_flags |= S_DEAD;
8466 +               }
8467 +               smp_mb(); /* necessary? */
8468 +       }
8469 +       d_drop(dentry);
8470 +}
8471 +
8472 +static int au_hide_children(struct dentry *parent)
8473 +{
8474 +       int err, i, j, ndentry;
8475 +       struct au_dcsub_pages dpages;
8476 +       struct au_dpage *dpage;
8477 +       struct dentry *dentry;
8478 +
8479 +       err = au_dpages_init(&dpages, GFP_NOFS);
8480 +       if (unlikely(err))
8481 +               goto out;
8482 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8483 +       if (unlikely(err))
8484 +               goto out_dpages;
8485 +
8486 +       /* in reverse order */
8487 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8488 +               dpage = dpages.dpages + i;
8489 +               ndentry = dpage->ndentry;
8490 +               for (j = ndentry - 1; j >= 0; j--) {
8491 +                       dentry = dpage->dentries[j];
8492 +                       if (dentry != parent)
8493 +                               au_do_hide(dentry);
8494 +               }
8495 +       }
8496 +
8497 +out_dpages:
8498 +       au_dpages_free(&dpages);
8499 +out:
8500 +       return err;
8501 +}
8502 +
8503 +static void au_hide(struct dentry *dentry)
8504 +{
8505 +       int err;
8506 +
8507 +       AuDbgDentry(dentry);
8508 +       if (d_is_dir(dentry)) {
8509 +               /* shrink_dcache_parent(dentry); */
8510 +               err = au_hide_children(dentry);
8511 +               if (unlikely(err))
8512 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8513 +                               dentry, err);
8514 +       }
8515 +       au_do_hide(dentry);
8516 +}
8517 +
8518 +/*
8519 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8520 + *
8521 + * a dirty branch is added
8522 + * - on the top of layers
8523 + * - in the middle of layers
8524 + * - to the bottom of layers
8525 + *
8526 + * on the added branch there exists
8527 + * - a whiteout
8528 + * - a diropq
8529 + * - a same named entry
8530 + *   + exist
8531 + *     * negative --> positive
8532 + *     * positive --> positive
8533 + *      - type is unchanged
8534 + *      - type is changed
8535 + *   + doesn't exist
8536 + *     * negative --> negative
8537 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8538 + * - none
8539 + */
8540 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8541 +                              struct au_dinfo *tmp)
8542 +{
8543 +       int err;
8544 +       aufs_bindex_t bindex, bbot;
8545 +       struct {
8546 +               struct dentry *dentry;
8547 +               struct inode *inode;
8548 +               mode_t mode;
8549 +       } orig_h, tmp_h = {
8550 +               .dentry = NULL
8551 +       };
8552 +       struct au_hdentry *hd;
8553 +       struct inode *inode, *h_inode;
8554 +       struct dentry *h_dentry;
8555 +
8556 +       err = 0;
8557 +       AuDebugOn(dinfo->di_btop < 0);
8558 +       orig_h.mode = 0;
8559 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8560 +       orig_h.inode = NULL;
8561 +       if (d_is_positive(orig_h.dentry)) {
8562 +               orig_h.inode = d_inode(orig_h.dentry);
8563 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8564 +       }
8565 +       if (tmp->di_btop >= 0) {
8566 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8567 +               if (d_is_positive(tmp_h.dentry)) {
8568 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8569 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8570 +               }
8571 +       }
8572 +
8573 +       inode = NULL;
8574 +       if (d_really_is_positive(dentry))
8575 +               inode = d_inode(dentry);
8576 +       if (!orig_h.inode) {
8577 +               AuDbg("negative originally\n");
8578 +               if (inode) {
8579 +                       au_hide(dentry);
8580 +                       goto out;
8581 +               }
8582 +               AuDebugOn(inode);
8583 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8584 +               AuDebugOn(dinfo->di_bdiropq != -1);
8585 +
8586 +               if (!tmp_h.inode) {
8587 +                       AuDbg("negative --> negative\n");
8588 +                       /* should have only one negative lower */
8589 +                       if (tmp->di_btop >= 0
8590 +                           && tmp->di_btop < dinfo->di_btop) {
8591 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8592 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8593 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8594 +                               au_di_cp(dinfo, tmp);
8595 +                               hd = au_hdentry(tmp, tmp->di_btop);
8596 +                               au_set_h_dptr(dentry, tmp->di_btop,
8597 +                                             dget(hd->hd_dentry));
8598 +                       }
8599 +                       au_dbg_verify_dinode(dentry);
8600 +               } else {
8601 +                       AuDbg("negative --> positive\n");
8602 +                       /*
8603 +                        * similar to the behaviour of creating with bypassing
8604 +                        * aufs.
8605 +                        * unhash it in order to force an error in the
8606 +                        * succeeding create operation.
8607 +                        * we should not set S_DEAD here.
8608 +                        */
8609 +                       d_drop(dentry);
8610 +                       /* au_di_swap(tmp, dinfo); */
8611 +                       au_dbg_verify_dinode(dentry);
8612 +               }
8613 +       } else {
8614 +               AuDbg("positive originally\n");
8615 +               /* inode may be NULL */
8616 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8617 +               if (!tmp_h.inode) {
8618 +                       AuDbg("positive --> negative\n");
8619 +                       /* or bypassing aufs */
8620 +                       au_hide(dentry);
8621 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8622 +                               dinfo->di_bwh = tmp->di_bwh;
8623 +                       if (inode)
8624 +                               err = au_refresh_hinode_self(inode);
8625 +                       au_dbg_verify_dinode(dentry);
8626 +               } else if (orig_h.mode == tmp_h.mode) {
8627 +                       AuDbg("positive --> positive, same type\n");
8628 +                       if (!S_ISDIR(orig_h.mode)
8629 +                           && dinfo->di_btop > tmp->di_btop) {
8630 +                               /*
8631 +                                * similar to the behaviour of removing and
8632 +                                * creating.
8633 +                                */
8634 +                               au_hide(dentry);
8635 +                               if (inode)
8636 +                                       err = au_refresh_hinode_self(inode);
8637 +                               au_dbg_verify_dinode(dentry);
8638 +                       } else {
8639 +                               /* fill empty slots */
8640 +                               if (dinfo->di_btop > tmp->di_btop)
8641 +                                       dinfo->di_btop = tmp->di_btop;
8642 +                               if (dinfo->di_bbot < tmp->di_bbot)
8643 +                                       dinfo->di_bbot = tmp->di_bbot;
8644 +                               dinfo->di_bwh = tmp->di_bwh;
8645 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8646 +                               bbot = dinfo->di_bbot;
8647 +                               bindex = tmp->di_btop;
8648 +                               hd = au_hdentry(tmp, bindex);
8649 +                               for (; bindex <= bbot; bindex++, hd++) {
8650 +                                       if (au_h_dptr(dentry, bindex))
8651 +                                               continue;
8652 +                                       h_dentry = hd->hd_dentry;
8653 +                                       if (!h_dentry)
8654 +                                               continue;
8655 +                                       AuDebugOn(d_is_negative(h_dentry));
8656 +                                       h_inode = d_inode(h_dentry);
8657 +                                       AuDebugOn(orig_h.mode
8658 +                                                 != (h_inode->i_mode
8659 +                                                     & S_IFMT));
8660 +                                       au_set_h_dptr(dentry, bindex,
8661 +                                                     dget(h_dentry));
8662 +                               }
8663 +                               if (inode)
8664 +                                       err = au_refresh_hinode(inode, dentry);
8665 +                               au_dbg_verify_dinode(dentry);
8666 +                       }
8667 +               } else {
8668 +                       AuDbg("positive --> positive, different type\n");
8669 +                       /* similar to the behaviour of removing and creating */
8670 +                       au_hide(dentry);
8671 +                       if (inode)
8672 +                               err = au_refresh_hinode_self(inode);
8673 +                       au_dbg_verify_dinode(dentry);
8674 +               }
8675 +       }
8676 +
8677 +out:
8678 +       return err;
8679 +}
8680 +
8681 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8682 +{
8683 +       const struct dentry_operations *dop
8684 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8685 +       static const unsigned int mask
8686 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8687 +
8688 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8689 +
8690 +       if (dentry->d_op == dop)
8691 +               return;
8692 +
8693 +       AuDbg("%pd\n", dentry);
8694 +       spin_lock(&dentry->d_lock);
8695 +       if (dop == &aufs_dop)
8696 +               dentry->d_flags |= mask;
8697 +       else
8698 +               dentry->d_flags &= ~mask;
8699 +       dentry->d_op = dop;
8700 +       spin_unlock(&dentry->d_lock);
8701 +}
8702 +
8703 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8704 +{
8705 +       int err, ebrange, nbr;
8706 +       unsigned int sigen;
8707 +       struct au_dinfo *dinfo, *tmp;
8708 +       struct super_block *sb;
8709 +       struct inode *inode;
8710 +
8711 +       DiMustWriteLock(dentry);
8712 +       AuDebugOn(IS_ROOT(dentry));
8713 +       AuDebugOn(d_really_is_negative(parent));
8714 +
8715 +       sb = dentry->d_sb;
8716 +       sigen = au_sigen(sb);
8717 +       err = au_digen_test(parent, sigen);
8718 +       if (unlikely(err))
8719 +               goto out;
8720 +
8721 +       nbr = au_sbbot(sb) + 1;
8722 +       dinfo = au_di(dentry);
8723 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8724 +       if (unlikely(err))
8725 +               goto out;
8726 +       ebrange = au_dbrange_test(dentry);
8727 +       if (!ebrange)
8728 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8729 +
8730 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8731 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8732 +               if (d_really_is_positive(dentry)) {
8733 +                       inode = d_inode(dentry);
8734 +                       err = au_refresh_hinode_self(inode);
8735 +               }
8736 +               au_dbg_verify_dinode(dentry);
8737 +               if (!err)
8738 +                       goto out_dgen; /* success */
8739 +               goto out;
8740 +       }
8741 +
8742 +       /* temporary dinfo */
8743 +       AuDbgDentry(dentry);
8744 +       err = -ENOMEM;
8745 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8746 +       if (unlikely(!tmp))
8747 +               goto out;
8748 +       au_di_swap(tmp, dinfo);
8749 +       /* returns the number of positive dentries */
8750 +       /*
8751 +        * if current working dir is removed, it returns an error.
8752 +        * but the dentry is legal.
8753 +        */
8754 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8755 +       AuDbgDentry(dentry);
8756 +       au_di_swap(tmp, dinfo);
8757 +       if (err == -ENOENT)
8758 +               err = 0;
8759 +       if (err >= 0) {
8760 +               /* compare/refresh by dinfo */
8761 +               AuDbgDentry(dentry);
8762 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8763 +               au_dbg_verify_dinode(dentry);
8764 +               AuTraceErr(err);
8765 +       }
8766 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8767 +       au_rw_write_unlock(&tmp->di_rwsem);
8768 +       au_di_free(tmp);
8769 +       if (unlikely(err))
8770 +               goto out;
8771 +
8772 +out_dgen:
8773 +       au_update_digen(dentry);
8774 +out:
8775 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8776 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8777 +               AuDbgDentry(dentry);
8778 +       }
8779 +       AuTraceErr(err);
8780 +       return err;
8781 +}
8782 +
8783 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8784 +                          struct dentry *dentry, aufs_bindex_t bindex)
8785 +{
8786 +       int err, valid;
8787 +
8788 +       err = 0;
8789 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8790 +               goto out;
8791 +
8792 +       AuDbg("b%d\n", bindex);
8793 +       /*
8794 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8795 +        * due to whiteout and branch permission.
8796 +        */
8797 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8798 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8799 +       /* it may return tri-state */
8800 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8801 +
8802 +       if (unlikely(valid < 0))
8803 +               err = valid;
8804 +       else if (!valid)
8805 +               err = -EINVAL;
8806 +
8807 +out:
8808 +       AuTraceErr(err);
8809 +       return err;
8810 +}
8811 +
8812 +/* todo: remove this */
8813 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8814 +                         unsigned int flags, int do_udba, int dirren)
8815 +{
8816 +       int err;
8817 +       umode_t mode, h_mode;
8818 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8819 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8820 +       struct inode *h_inode, *h_cached_inode;
8821 +       struct dentry *h_dentry;
8822 +       struct qstr *name, *h_name;
8823 +
8824 +       err = 0;
8825 +       plus = 0;
8826 +       mode = 0;
8827 +       ibs = -1;
8828 +       ibe = -1;
8829 +       unhashed = !!d_unhashed(dentry);
8830 +       is_root = !!IS_ROOT(dentry);
8831 +       name = &dentry->d_name;
8832 +       tmpfile = au_di(dentry)->di_tmpfile;
8833 +
8834 +       /*
8835 +        * Theoretically, REVAL test should be unnecessary in case of
8836 +        * {FS,I}NOTIFY.
8837 +        * But {fs,i}notify doesn't fire some necessary events,
8838 +        *      IN_ATTRIB for atime/nlink/pageio
8839 +        * Let's do REVAL test too.
8840 +        */
8841 +       if (do_udba && inode) {
8842 +               mode = (inode->i_mode & S_IFMT);
8843 +               plus = (inode->i_nlink > 0);
8844 +               ibs = au_ibtop(inode);
8845 +               ibe = au_ibbot(inode);
8846 +       }
8847 +
8848 +       btop = au_dbtop(dentry);
8849 +       btail = btop;
8850 +       if (inode && S_ISDIR(inode->i_mode))
8851 +               btail = au_dbtaildir(dentry);
8852 +       for (bindex = btop; bindex <= btail; bindex++) {
8853 +               h_dentry = au_h_dptr(dentry, bindex);
8854 +               if (!h_dentry)
8855 +                       continue;
8856 +
8857 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8858 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8859 +               spin_lock(&h_dentry->d_lock);
8860 +               h_name = &h_dentry->d_name;
8861 +               if (unlikely(do_udba
8862 +                            && !is_root
8863 +                            && ((!h_nfs
8864 +                                 && (unhashed != !!d_unhashed(h_dentry)
8865 +                                     || (!tmpfile && !dirren
8866 +                                         && !au_qstreq(name, h_name))
8867 +                                         ))
8868 +                                || (h_nfs
8869 +                                    && !(flags & LOOKUP_OPEN)
8870 +                                    && (h_dentry->d_flags
8871 +                                        & DCACHE_NFSFS_RENAMED)))
8872 +                           )) {
8873 +                       int h_unhashed;
8874 +
8875 +                       h_unhashed = d_unhashed(h_dentry);
8876 +                       spin_unlock(&h_dentry->d_lock);
8877 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8878 +                             unhashed, h_unhashed, dentry, h_dentry);
8879 +                       goto err;
8880 +               }
8881 +               spin_unlock(&h_dentry->d_lock);
8882 +
8883 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8884 +               if (unlikely(err))
8885 +                       /* do not goto err, to keep the errno */
8886 +                       break;
8887 +
8888 +               /* todo: plink too? */
8889 +               if (!do_udba)
8890 +                       continue;
8891 +
8892 +               /* UDBA tests */
8893 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8894 +                       goto err;
8895 +
8896 +               h_inode = NULL;
8897 +               if (d_is_positive(h_dentry))
8898 +                       h_inode = d_inode(h_dentry);
8899 +               h_plus = plus;
8900 +               h_mode = mode;
8901 +               h_cached_inode = h_inode;
8902 +               if (h_inode) {
8903 +                       h_mode = (h_inode->i_mode & S_IFMT);
8904 +                       h_plus = (h_inode->i_nlink > 0);
8905 +               }
8906 +               if (inode && ibs <= bindex && bindex <= ibe)
8907 +                       h_cached_inode = au_h_iptr(inode, bindex);
8908 +
8909 +               if (!h_nfs) {
8910 +                       if (unlikely(plus != h_plus && !tmpfile))
8911 +                               goto err;
8912 +               } else {
8913 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8914 +                                    && !is_root
8915 +                                    && !IS_ROOT(h_dentry)
8916 +                                    && unhashed != d_unhashed(h_dentry)))
8917 +                               goto err;
8918 +               }
8919 +               if (unlikely(mode != h_mode
8920 +                            || h_cached_inode != h_inode))
8921 +                       goto err;
8922 +               continue;
8923 +
8924 +err:
8925 +               err = -EINVAL;
8926 +               break;
8927 +       }
8928 +
8929 +       AuTraceErr(err);
8930 +       return err;
8931 +}
8932 +
8933 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8934 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8935 +{
8936 +       int err;
8937 +       struct dentry *parent;
8938 +
8939 +       if (!au_digen_test(dentry, sigen))
8940 +               return 0;
8941 +
8942 +       parent = dget_parent(dentry);
8943 +       di_read_lock_parent(parent, AuLock_IR);
8944 +       AuDebugOn(au_digen_test(parent, sigen));
8945 +       au_dbg_verify_gen(parent, sigen);
8946 +       err = au_refresh_dentry(dentry, parent);
8947 +       di_read_unlock(parent, AuLock_IR);
8948 +       dput(parent);
8949 +       AuTraceErr(err);
8950 +       return err;
8951 +}
8952 +
8953 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8954 +{
8955 +       int err;
8956 +       struct dentry *d, *parent;
8957 +
8958 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8959 +               return simple_reval_dpath(dentry, sigen);
8960 +
8961 +       /* slow loop, keep it simple and stupid */
8962 +       /* cf: au_cpup_dirs() */
8963 +       err = 0;
8964 +       parent = NULL;
8965 +       while (au_digen_test(dentry, sigen)) {
8966 +               d = dentry;
8967 +               while (1) {
8968 +                       dput(parent);
8969 +                       parent = dget_parent(d);
8970 +                       if (!au_digen_test(parent, sigen))
8971 +                               break;
8972 +                       d = parent;
8973 +               }
8974 +
8975 +               if (d != dentry)
8976 +                       di_write_lock_child2(d);
8977 +
8978 +               /* someone might update our dentry while we were sleeping */
8979 +               if (au_digen_test(d, sigen)) {
8980 +                       /*
8981 +                        * todo: consolidate with simple_reval_dpath(),
8982 +                        * do_refresh() and au_reval_for_attr().
8983 +                        */
8984 +                       di_read_lock_parent(parent, AuLock_IR);
8985 +                       err = au_refresh_dentry(d, parent);
8986 +                       di_read_unlock(parent, AuLock_IR);
8987 +               }
8988 +
8989 +               if (d != dentry)
8990 +                       di_write_unlock(d);
8991 +               dput(parent);
8992 +               if (unlikely(err))
8993 +                       break;
8994 +       }
8995 +
8996 +       return err;
8997 +}
8998 +
8999 +/*
9000 + * if valid returns 1, otherwise 0.
9001 + */
9002 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9003 +{
9004 +       int valid, err;
9005 +       unsigned int sigen;
9006 +       unsigned char do_udba, dirren;
9007 +       struct super_block *sb;
9008 +       struct inode *inode;
9009 +
9010 +       /* todo: support rcu-walk? */
9011 +       if (flags & LOOKUP_RCU)
9012 +               return -ECHILD;
9013 +
9014 +       valid = 0;
9015 +       if (unlikely(!au_di(dentry)))
9016 +               goto out;
9017 +
9018 +       valid = 1;
9019 +       sb = dentry->d_sb;
9020 +       /*
9021 +        * todo: very ugly
9022 +        * i_mutex of parent dir may be held,
9023 +        * but we should not return 'invalid' due to busy.
9024 +        */
9025 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9026 +       if (unlikely(err)) {
9027 +               valid = err;
9028 +               AuTraceErr(err);
9029 +               goto out;
9030 +       }
9031 +       inode = NULL;
9032 +       if (d_really_is_positive(dentry))
9033 +               inode = d_inode(dentry);
9034 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9035 +               err = -EINVAL;
9036 +               AuTraceErr(err);
9037 +               goto out_dgrade;
9038 +       }
9039 +       if (unlikely(au_dbrange_test(dentry))) {
9040 +               err = -EINVAL;
9041 +               AuTraceErr(err);
9042 +               goto out_dgrade;
9043 +       }
9044 +
9045 +       sigen = au_sigen(sb);
9046 +       if (au_digen_test(dentry, sigen)) {
9047 +               AuDebugOn(IS_ROOT(dentry));
9048 +               err = au_reval_dpath(dentry, sigen);
9049 +               if (unlikely(err)) {
9050 +                       AuTraceErr(err);
9051 +                       goto out_dgrade;
9052 +               }
9053 +       }
9054 +       di_downgrade_lock(dentry, AuLock_IR);
9055 +
9056 +       err = -EINVAL;
9057 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9058 +           && inode
9059 +           && !(inode->i_state && I_LINKABLE)
9060 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9061 +               AuTraceErr(err);
9062 +               goto out_inval;
9063 +       }
9064 +
9065 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9066 +       if (do_udba && inode) {
9067 +               aufs_bindex_t btop = au_ibtop(inode);
9068 +               struct inode *h_inode;
9069 +
9070 +               if (btop >= 0) {
9071 +                       h_inode = au_h_iptr(inode, btop);
9072 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9073 +                               AuTraceErr(err);
9074 +                               goto out_inval;
9075 +                       }
9076 +               }
9077 +       }
9078 +
9079 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9080 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9081 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9082 +               err = -EIO;
9083 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9084 +                     dentry, err);
9085 +       }
9086 +       goto out_inval;
9087 +
9088 +out_dgrade:
9089 +       di_downgrade_lock(dentry, AuLock_IR);
9090 +out_inval:
9091 +       aufs_read_unlock(dentry, AuLock_IR);
9092 +       AuTraceErr(err);
9093 +       valid = !err;
9094 +out:
9095 +       if (!valid) {
9096 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9097 +               d_drop(dentry);
9098 +       }
9099 +       return valid;
9100 +}
9101 +
9102 +static void aufs_d_release(struct dentry *dentry)
9103 +{
9104 +       if (au_di(dentry)) {
9105 +               au_di_fin(dentry);
9106 +               au_hn_di_reinit(dentry);
9107 +       }
9108 +}
9109 +
9110 +const struct dentry_operations aufs_dop = {
9111 +       .d_revalidate           = aufs_d_revalidate,
9112 +       .d_weak_revalidate      = aufs_d_revalidate,
9113 +       .d_release              = aufs_d_release
9114 +};
9115 +
9116 +/* aufs_dop without d_revalidate */
9117 +const struct dentry_operations aufs_dop_noreval = {
9118 +       .d_release              = aufs_d_release
9119 +};
9120 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9121 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9122 +++ linux/fs/aufs/dentry.h      2023-09-03 02:21:58.859971007 +0200
9123 @@ -0,0 +1,270 @@
9124 +/* SPDX-License-Identifier: GPL-2.0 */
9125 +/*
9126 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9127 + *
9128 + * This program is free software; you can redistribute it and/or modify
9129 + * it under the terms of the GNU General Public License as published by
9130 + * the Free Software Foundation; either version 2 of the License, or
9131 + * (at your option) any later version.
9132 + *
9133 + * This program is distributed in the hope that it will be useful,
9134 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9135 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9136 + * GNU General Public License for more details.
9137 + *
9138 + * You should have received a copy of the GNU General Public License
9139 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9140 + */
9141 +
9142 +/*
9143 + * lookup and dentry operations
9144 + */
9145 +
9146 +#ifndef __AUFS_DENTRY_H__
9147 +#define __AUFS_DENTRY_H__
9148 +
9149 +#ifdef __KERNEL__
9150 +
9151 +#include <linux/dcache.h>
9152 +#include "dirren.h"
9153 +#include "rwsem.h"
9154 +
9155 +struct au_hdentry {
9156 +       struct dentry           *hd_dentry;
9157 +       aufs_bindex_t           hd_id;
9158 +};
9159 +
9160 +struct au_dinfo {
9161 +       atomic_t                di_generation;
9162 +
9163 +       struct au_rwsem         di_rwsem;
9164 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9165 +       unsigned char           di_tmpfile; /* to allow the different name */
9166 +       struct au_hdentry       *di_hdentry;
9167 +       struct file             *di_htmpfile;
9168 +       struct rcu_head         rcu;
9169 +} ____cacheline_aligned_in_smp;
9170 +
9171 +/* ---------------------------------------------------------------------- */
9172 +
9173 +/* flags for au_lkup_dentry() */
9174 +#define AuLkup_ALLOW_NEG       1
9175 +#define AuLkup_IGNORE_PERM     (1 << 1)
9176 +#define AuLkup_DIRREN          (1 << 2)
9177 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9178 +#define au_fset_lkup(flags, name) \
9179 +       do { (flags) |= AuLkup_##name; } while (0)
9180 +#define au_fclr_lkup(flags, name) \
9181 +       do { (flags) &= ~AuLkup_##name; } while (0)
9182 +
9183 +#ifndef CONFIG_AUFS_DIRREN
9184 +#undef AuLkup_DIRREN
9185 +#define AuLkup_DIRREN 0
9186 +#endif
9187 +
9188 +struct au_do_lookup_args {
9189 +       unsigned int            flags;
9190 +       mode_t                  type;
9191 +       struct qstr             whname, *name;
9192 +       struct au_dr_lookup     dirren;
9193 +};
9194 +
9195 +/* ---------------------------------------------------------------------- */
9196 +
9197 +/* dentry.c */
9198 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9199 +struct au_branch;
9200 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
9201 +                              struct path *ppath);
9202 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9203 +               struct dentry *h_parent, struct au_branch *br);
9204 +
9205 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9206 +                  unsigned int flags);
9207 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9208 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9209 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9210 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9211 +
9212 +/* dinfo.c */
9213 +void au_di_init_once(void *_di);
9214 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9215 +void au_di_free(struct au_dinfo *dinfo);
9216 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9217 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9218 +int au_di_init(struct dentry *dentry);
9219 +void au_di_fin(struct dentry *dentry);
9220 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9221 +
9222 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9223 +void di_read_unlock(struct dentry *d, int flags);
9224 +void di_downgrade_lock(struct dentry *d, int flags);
9225 +void di_write_lock(struct dentry *d, unsigned int lsc);
9226 +void di_write_unlock(struct dentry *d);
9227 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9228 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9229 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9230 +
9231 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9232 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9233 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9234 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9235 +
9236 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9237 +                  struct dentry *h_dentry);
9238 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9239 +int au_dbrange_test(struct dentry *dentry);
9240 +void au_update_digen(struct dentry *dentry);
9241 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9242 +void au_update_dbtop(struct dentry *dentry);
9243 +void au_update_dbbot(struct dentry *dentry);
9244 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9245 +
9246 +/* ---------------------------------------------------------------------- */
9247 +
9248 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9249 +{
9250 +       return dentry->d_fsdata;
9251 +}
9252 +
9253 +/* ---------------------------------------------------------------------- */
9254 +
9255 +/* lock subclass for dinfo */
9256 +enum {
9257 +       AuLsc_DI_CHILD,         /* child first */
9258 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9259 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9260 +       AuLsc_DI_PARENT,
9261 +       AuLsc_DI_PARENT2,
9262 +       AuLsc_DI_PARENT3,
9263 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9264 +};
9265 +
9266 +/*
9267 + * di_read_lock_child, di_write_lock_child,
9268 + * di_read_lock_child2, di_write_lock_child2,
9269 + * di_read_lock_child3, di_write_lock_child3,
9270 + * di_read_lock_parent, di_write_lock_parent,
9271 + * di_read_lock_parent2, di_write_lock_parent2,
9272 + * di_read_lock_parent3, di_write_lock_parent3,
9273 + */
9274 +#define AuReadLockFunc(name, lsc) \
9275 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9276 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9277 +
9278 +#define AuWriteLockFunc(name, lsc) \
9279 +static inline void di_write_lock_##name(struct dentry *d) \
9280 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9281 +
9282 +#define AuRWLockFuncs(name, lsc) \
9283 +       AuReadLockFunc(name, lsc) \
9284 +       AuWriteLockFunc(name, lsc)
9285 +
9286 +AuRWLockFuncs(child, CHILD);
9287 +AuRWLockFuncs(child2, CHILD2);
9288 +AuRWLockFuncs(child3, CHILD3);
9289 +AuRWLockFuncs(parent, PARENT);
9290 +AuRWLockFuncs(parent2, PARENT2);
9291 +AuRWLockFuncs(parent3, PARENT3);
9292 +
9293 +#undef AuReadLockFunc
9294 +#undef AuWriteLockFunc
9295 +#undef AuRWLockFuncs
9296 +
9297 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9298 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9299 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9300 +
9301 +/* ---------------------------------------------------------------------- */
9302 +
9303 +/* todo: memory barrier? */
9304 +static inline unsigned int au_digen(struct dentry *d)
9305 +{
9306 +       return atomic_read(&au_di(d)->di_generation);
9307 +}
9308 +
9309 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9310 +{
9311 +       hdentry->hd_dentry = NULL;
9312 +}
9313 +
9314 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9315 +                                           aufs_bindex_t bindex)
9316 +{
9317 +       return di->di_hdentry + bindex;
9318 +}
9319 +
9320 +static inline void au_hdput(struct au_hdentry *hd)
9321 +{
9322 +       if (hd)
9323 +               dput(hd->hd_dentry);
9324 +}
9325 +
9326 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9327 +{
9328 +       DiMustAnyLock(dentry);
9329 +       return au_di(dentry)->di_btop;
9330 +}
9331 +
9332 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9333 +{
9334 +       DiMustAnyLock(dentry);
9335 +       return au_di(dentry)->di_bbot;
9336 +}
9337 +
9338 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9339 +{
9340 +       DiMustAnyLock(dentry);
9341 +       return au_di(dentry)->di_bwh;
9342 +}
9343 +
9344 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9345 +{
9346 +       DiMustAnyLock(dentry);
9347 +       return au_di(dentry)->di_bdiropq;
9348 +}
9349 +
9350 +/* todo: hard/soft set? */
9351 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9352 +{
9353 +       DiMustWriteLock(dentry);
9354 +       au_di(dentry)->di_btop = bindex;
9355 +}
9356 +
9357 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9358 +{
9359 +       DiMustWriteLock(dentry);
9360 +       au_di(dentry)->di_bbot = bindex;
9361 +}
9362 +
9363 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9364 +{
9365 +       DiMustWriteLock(dentry);
9366 +       /* dbwh can be outside of btop - bbot range */
9367 +       au_di(dentry)->di_bwh = bindex;
9368 +}
9369 +
9370 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9371 +{
9372 +       DiMustWriteLock(dentry);
9373 +       au_di(dentry)->di_bdiropq = bindex;
9374 +}
9375 +
9376 +/* ---------------------------------------------------------------------- */
9377 +
9378 +#ifdef CONFIG_AUFS_HNOTIFY
9379 +static inline void au_digen_dec(struct dentry *d)
9380 +{
9381 +       atomic_dec(&au_di(d)->di_generation);
9382 +}
9383 +
9384 +static inline void au_hn_di_reinit(struct dentry *dentry)
9385 +{
9386 +       dentry->d_fsdata = NULL;
9387 +}
9388 +#else
9389 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9390 +#endif /* CONFIG_AUFS_HNOTIFY */
9391 +
9392 +#endif /* __KERNEL__ */
9393 +#endif /* __AUFS_DENTRY_H__ */
9394 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9395 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9396 +++ linux/fs/aufs/dinfo.c       2022-12-17 09:21:34.796521861 +0100
9397 @@ -0,0 +1,555 @@
9398 +// SPDX-License-Identifier: GPL-2.0
9399 +/*
9400 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9401 + *
9402 + * This program is free software; you can redistribute it and/or modify
9403 + * it under the terms of the GNU General Public License as published by
9404 + * the Free Software Foundation; either version 2 of the License, or
9405 + * (at your option) any later version.
9406 + *
9407 + * This program is distributed in the hope that it will be useful,
9408 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9409 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9410 + * GNU General Public License for more details.
9411 + *
9412 + * You should have received a copy of the GNU General Public License
9413 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9414 + */
9415 +
9416 +/*
9417 + * dentry private data
9418 + */
9419 +
9420 +#include "aufs.h"
9421 +
9422 +void au_di_init_once(void *_dinfo)
9423 +{
9424 +       struct au_dinfo *dinfo = _dinfo;
9425 +
9426 +       au_rw_init(&dinfo->di_rwsem);
9427 +}
9428 +
9429 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9430 +{
9431 +       struct au_dinfo *dinfo;
9432 +       int nbr, i;
9433 +
9434 +       dinfo = au_cache_alloc_dinfo();
9435 +       if (unlikely(!dinfo))
9436 +               goto out;
9437 +
9438 +       nbr = au_sbbot(sb) + 1;
9439 +       if (nbr <= 0)
9440 +               nbr = 1;
9441 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9442 +       if (dinfo->di_hdentry) {
9443 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9444 +               dinfo->di_btop = -1;
9445 +               dinfo->di_bbot = -1;
9446 +               dinfo->di_bwh = -1;
9447 +               dinfo->di_bdiropq = -1;
9448 +               dinfo->di_tmpfile = 0;
9449 +               for (i = 0; i < nbr; i++)
9450 +                       dinfo->di_hdentry[i].hd_id = -1;
9451 +               dinfo->di_htmpfile = NULL;
9452 +               goto out;
9453 +       }
9454 +
9455 +       au_cache_free_dinfo(dinfo);
9456 +       dinfo = NULL;
9457 +
9458 +out:
9459 +       return dinfo;
9460 +}
9461 +
9462 +void au_di_free(struct au_dinfo *dinfo)
9463 +{
9464 +       struct au_hdentry *p;
9465 +       aufs_bindex_t bbot, bindex;
9466 +
9467 +       /* dentry may not be revalidated */
9468 +       bindex = dinfo->di_btop;
9469 +       if (bindex >= 0) {
9470 +               bbot = dinfo->di_bbot;
9471 +               p = au_hdentry(dinfo, bindex);
9472 +               while (bindex++ <= bbot)
9473 +                       au_hdput(p++);
9474 +       }
9475 +       au_kfree_try_rcu(dinfo->di_hdentry);
9476 +       au_cache_free_dinfo(dinfo);
9477 +}
9478 +
9479 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9480 +{
9481 +       struct au_hdentry *p;
9482 +       aufs_bindex_t bi;
9483 +
9484 +       AuRwMustWriteLock(&a->di_rwsem);
9485 +       AuRwMustWriteLock(&b->di_rwsem);
9486 +
9487 +#define DiSwap(v, name)                                \
9488 +       do {                                    \
9489 +               v = a->di_##name;               \
9490 +               a->di_##name = b->di_##name;    \
9491 +               b->di_##name = v;               \
9492 +       } while (0)
9493 +
9494 +       DiSwap(p, hdentry);
9495 +       DiSwap(bi, btop);
9496 +       DiSwap(bi, bbot);
9497 +       DiSwap(bi, bwh);
9498 +       DiSwap(bi, bdiropq);
9499 +       /* smp_mb(); */
9500 +
9501 +#undef DiSwap
9502 +}
9503 +
9504 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9505 +{
9506 +       AuRwMustWriteLock(&dst->di_rwsem);
9507 +       AuRwMustWriteLock(&src->di_rwsem);
9508 +
9509 +       dst->di_btop = src->di_btop;
9510 +       dst->di_bbot = src->di_bbot;
9511 +       dst->di_bwh = src->di_bwh;
9512 +       dst->di_bdiropq = src->di_bdiropq;
9513 +       /* smp_mb(); */
9514 +}
9515 +
9516 +int au_di_init(struct dentry *dentry)
9517 +{
9518 +       int err;
9519 +       struct super_block *sb;
9520 +       struct au_dinfo *dinfo;
9521 +
9522 +       err = 0;
9523 +       sb = dentry->d_sb;
9524 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9525 +       if (dinfo) {
9526 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9527 +               /* smp_mb(); */ /* atomic_set */
9528 +               dentry->d_fsdata = dinfo;
9529 +       } else
9530 +               err = -ENOMEM;
9531 +
9532 +       return err;
9533 +}
9534 +
9535 +void au_di_fin(struct dentry *dentry)
9536 +{
9537 +       struct au_dinfo *dinfo;
9538 +
9539 +       dinfo = au_di(dentry);
9540 +       AuRwDestroy(&dinfo->di_rwsem);
9541 +       au_di_free(dinfo);
9542 +}
9543 +
9544 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9545 +{
9546 +       int err, sz;
9547 +       struct au_hdentry *hdp;
9548 +
9549 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9550 +
9551 +       err = -ENOMEM;
9552 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9553 +       if (!sz)
9554 +               sz = sizeof(*hdp);
9555 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9556 +                          may_shrink);
9557 +       if (hdp) {
9558 +               dinfo->di_hdentry = hdp;
9559 +               err = 0;
9560 +       }
9561 +
9562 +       return err;
9563 +}
9564 +
9565 +/* ---------------------------------------------------------------------- */
9566 +
9567 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9568 +{
9569 +       switch (lsc) {
9570 +       case AuLsc_DI_CHILD:
9571 +               ii_write_lock_child(inode);
9572 +               break;
9573 +       case AuLsc_DI_CHILD2:
9574 +               ii_write_lock_child2(inode);
9575 +               break;
9576 +       case AuLsc_DI_CHILD3:
9577 +               ii_write_lock_child3(inode);
9578 +               break;
9579 +       case AuLsc_DI_PARENT:
9580 +               ii_write_lock_parent(inode);
9581 +               break;
9582 +       case AuLsc_DI_PARENT2:
9583 +               ii_write_lock_parent2(inode);
9584 +               break;
9585 +       case AuLsc_DI_PARENT3:
9586 +               ii_write_lock_parent3(inode);
9587 +               break;
9588 +       default:
9589 +               BUG();
9590 +       }
9591 +}
9592 +
9593 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9594 +{
9595 +       switch (lsc) {
9596 +       case AuLsc_DI_CHILD:
9597 +               ii_read_lock_child(inode);
9598 +               break;
9599 +       case AuLsc_DI_CHILD2:
9600 +               ii_read_lock_child2(inode);
9601 +               break;
9602 +       case AuLsc_DI_CHILD3:
9603 +               ii_read_lock_child3(inode);
9604 +               break;
9605 +       case AuLsc_DI_PARENT:
9606 +               ii_read_lock_parent(inode);
9607 +               break;
9608 +       case AuLsc_DI_PARENT2:
9609 +               ii_read_lock_parent2(inode);
9610 +               break;
9611 +       case AuLsc_DI_PARENT3:
9612 +               ii_read_lock_parent3(inode);
9613 +               break;
9614 +       default:
9615 +               BUG();
9616 +       }
9617 +}
9618 +
9619 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9620 +{
9621 +       struct inode *inode;
9622 +
9623 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9624 +       if (d_really_is_positive(d)) {
9625 +               inode = d_inode(d);
9626 +               if (au_ftest_lock(flags, IW))
9627 +                       do_ii_write_lock(inode, lsc);
9628 +               else if (au_ftest_lock(flags, IR))
9629 +                       do_ii_read_lock(inode, lsc);
9630 +       }
9631 +}
9632 +
9633 +void di_read_unlock(struct dentry *d, int flags)
9634 +{
9635 +       struct inode *inode;
9636 +
9637 +       if (d_really_is_positive(d)) {
9638 +               inode = d_inode(d);
9639 +               if (au_ftest_lock(flags, IW)) {
9640 +                       au_dbg_verify_dinode(d);
9641 +                       ii_write_unlock(inode);
9642 +               } else if (au_ftest_lock(flags, IR)) {
9643 +                       au_dbg_verify_dinode(d);
9644 +                       ii_read_unlock(inode);
9645 +               }
9646 +       }
9647 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9648 +}
9649 +
9650 +void di_downgrade_lock(struct dentry *d, int flags)
9651 +{
9652 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9653 +               ii_downgrade_lock(d_inode(d));
9654 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9655 +}
9656 +
9657 +void di_write_lock(struct dentry *d, unsigned int lsc)
9658 +{
9659 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9660 +       if (d_really_is_positive(d))
9661 +               do_ii_write_lock(d_inode(d), lsc);
9662 +}
9663 +
9664 +void di_write_unlock(struct dentry *d)
9665 +{
9666 +       au_dbg_verify_dinode(d);
9667 +       if (d_really_is_positive(d))
9668 +               ii_write_unlock(d_inode(d));
9669 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9670 +}
9671 +
9672 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9673 +{
9674 +       AuDebugOn(d1 == d2
9675 +                 || d_inode(d1) == d_inode(d2)
9676 +                 || d1->d_sb != d2->d_sb);
9677 +
9678 +       if ((isdir && au_test_subdir(d1, d2))
9679 +           || d1 < d2) {
9680 +               di_write_lock_child(d1);
9681 +               di_write_lock_child2(d2);
9682 +       } else {
9683 +               di_write_lock_child(d2);
9684 +               di_write_lock_child2(d1);
9685 +       }
9686 +}
9687 +
9688 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9689 +{
9690 +       AuDebugOn(d1 == d2
9691 +                 || d_inode(d1) == d_inode(d2)
9692 +                 || d1->d_sb != d2->d_sb);
9693 +
9694 +       if ((isdir && au_test_subdir(d1, d2))
9695 +           || d1 < d2) {
9696 +               di_write_lock_parent(d1);
9697 +               di_write_lock_parent2(d2);
9698 +       } else {
9699 +               di_write_lock_parent(d2);
9700 +               di_write_lock_parent2(d1);
9701 +       }
9702 +}
9703 +
9704 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9705 +{
9706 +       di_write_unlock(d1);
9707 +       if (d_inode(d1) == d_inode(d2))
9708 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9709 +       else
9710 +               di_write_unlock(d2);
9711 +}
9712 +
9713 +/* ---------------------------------------------------------------------- */
9714 +
9715 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9716 +{
9717 +       struct dentry *d;
9718 +
9719 +       DiMustAnyLock(dentry);
9720 +
9721 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9722 +               return NULL;
9723 +       AuDebugOn(bindex < 0);
9724 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9725 +       AuDebugOn(d && au_dcount(d) <= 0);
9726 +       return d;
9727 +}
9728 +
9729 +/*
9730 + * extended version of au_h_dptr().
9731 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9732 + * error.
9733 + */
9734 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9735 +{
9736 +       struct dentry *h_dentry;
9737 +       struct inode *inode, *h_inode;
9738 +
9739 +       AuDebugOn(d_really_is_negative(dentry));
9740 +
9741 +       h_dentry = NULL;
9742 +       if (au_dbtop(dentry) <= bindex
9743 +           && bindex <= au_dbbot(dentry))
9744 +               h_dentry = au_h_dptr(dentry, bindex);
9745 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9746 +               dget(h_dentry);
9747 +               goto out; /* success */
9748 +       }
9749 +
9750 +       inode = d_inode(dentry);
9751 +       AuDebugOn(bindex < au_ibtop(inode));
9752 +       AuDebugOn(au_ibbot(inode) < bindex);
9753 +       h_inode = au_h_iptr(inode, bindex);
9754 +       h_dentry = d_find_alias(h_inode);
9755 +       if (h_dentry) {
9756 +               if (!IS_ERR(h_dentry)) {
9757 +                       if (!au_d_linkable(h_dentry))
9758 +                               goto out; /* success */
9759 +                       dput(h_dentry);
9760 +               } else
9761 +                       goto out;
9762 +       }
9763 +
9764 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9765 +               h_dentry = au_plink_lkup(inode, bindex);
9766 +               AuDebugOn(!h_dentry);
9767 +               if (!IS_ERR(h_dentry)) {
9768 +                       if (!au_d_hashed_positive(h_dentry))
9769 +                               goto out; /* success */
9770 +                       dput(h_dentry);
9771 +                       h_dentry = NULL;
9772 +               }
9773 +       }
9774 +
9775 +out:
9776 +       AuDbgDentry(h_dentry);
9777 +       return h_dentry;
9778 +}
9779 +
9780 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9781 +{
9782 +       aufs_bindex_t bbot, bwh;
9783 +
9784 +       bbot = au_dbbot(dentry);
9785 +       if (0 <= bbot) {
9786 +               bwh = au_dbwh(dentry);
9787 +               if (!bwh)
9788 +                       return bwh;
9789 +               if (0 < bwh && bwh < bbot)
9790 +                       return bwh - 1;
9791 +       }
9792 +       return bbot;
9793 +}
9794 +
9795 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9796 +{
9797 +       aufs_bindex_t bbot, bopq;
9798 +
9799 +       bbot = au_dbtail(dentry);
9800 +       if (0 <= bbot) {
9801 +               bopq = au_dbdiropq(dentry);
9802 +               if (0 <= bopq && bopq < bbot)
9803 +                       bbot = bopq;
9804 +       }
9805 +       return bbot;
9806 +}
9807 +
9808 +/* ---------------------------------------------------------------------- */
9809 +
9810 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9811 +                  struct dentry *h_dentry)
9812 +{
9813 +       struct au_dinfo *dinfo;
9814 +       struct au_hdentry *hd;
9815 +       struct au_branch *br;
9816 +
9817 +       DiMustWriteLock(dentry);
9818 +
9819 +       dinfo = au_di(dentry);
9820 +       hd = au_hdentry(dinfo, bindex);
9821 +       au_hdput(hd);
9822 +       hd->hd_dentry = h_dentry;
9823 +       if (h_dentry) {
9824 +               br = au_sbr(dentry->d_sb, bindex);
9825 +               hd->hd_id = br->br_id;
9826 +       }
9827 +}
9828 +
9829 +int au_dbrange_test(struct dentry *dentry)
9830 +{
9831 +       int err;
9832 +       aufs_bindex_t btop, bbot;
9833 +
9834 +       err = 0;
9835 +       btop = au_dbtop(dentry);
9836 +       bbot = au_dbbot(dentry);
9837 +       if (btop >= 0)
9838 +               AuDebugOn(bbot < 0 && btop > bbot);
9839 +       else {
9840 +               err = -EIO;
9841 +               AuDebugOn(bbot >= 0);
9842 +       }
9843 +
9844 +       return err;
9845 +}
9846 +
9847 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9848 +{
9849 +       int err;
9850 +
9851 +       err = 0;
9852 +       if (unlikely(au_digen(dentry) != sigen
9853 +                    || au_iigen_test(d_inode(dentry), sigen)))
9854 +               err = -EIO;
9855 +
9856 +       return err;
9857 +}
9858 +
9859 +void au_update_digen(struct dentry *dentry)
9860 +{
9861 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9862 +       /* smp_mb(); */ /* atomic_set */
9863 +}
9864 +
9865 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9866 +{
9867 +       struct au_dinfo *dinfo;
9868 +       struct dentry *h_d;
9869 +       struct au_hdentry *hdp;
9870 +       aufs_bindex_t bindex, bbot;
9871 +
9872 +       DiMustWriteLock(dentry);
9873 +
9874 +       dinfo = au_di(dentry);
9875 +       if (!dinfo || dinfo->di_btop < 0)
9876 +               return;
9877 +
9878 +       if (do_put_zero) {
9879 +               bbot = dinfo->di_bbot;
9880 +               bindex = dinfo->di_btop;
9881 +               hdp = au_hdentry(dinfo, bindex);
9882 +               for (; bindex <= bbot; bindex++, hdp++) {
9883 +                       h_d = hdp->hd_dentry;
9884 +                       if (h_d && d_is_negative(h_d))
9885 +                               au_set_h_dptr(dentry, bindex, NULL);
9886 +               }
9887 +       }
9888 +
9889 +       dinfo->di_btop = 0;
9890 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9891 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9892 +               if (hdp->hd_dentry)
9893 +                       break;
9894 +       if (dinfo->di_btop > dinfo->di_bbot) {
9895 +               dinfo->di_btop = -1;
9896 +               dinfo->di_bbot = -1;
9897 +               return;
9898 +       }
9899 +
9900 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9901 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9902 +               if (hdp->hd_dentry)
9903 +                       break;
9904 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9905 +}
9906 +
9907 +void au_update_dbtop(struct dentry *dentry)
9908 +{
9909 +       aufs_bindex_t bindex, bbot;
9910 +       struct dentry *h_dentry;
9911 +
9912 +       bbot = au_dbbot(dentry);
9913 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9914 +               h_dentry = au_h_dptr(dentry, bindex);
9915 +               if (!h_dentry)
9916 +                       continue;
9917 +               if (d_is_positive(h_dentry)) {
9918 +                       au_set_dbtop(dentry, bindex);
9919 +                       return;
9920 +               }
9921 +               au_set_h_dptr(dentry, bindex, NULL);
9922 +       }
9923 +}
9924 +
9925 +void au_update_dbbot(struct dentry *dentry)
9926 +{
9927 +       aufs_bindex_t bindex, btop;
9928 +       struct dentry *h_dentry;
9929 +
9930 +       btop = au_dbtop(dentry);
9931 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9932 +               h_dentry = au_h_dptr(dentry, bindex);
9933 +               if (!h_dentry)
9934 +                       continue;
9935 +               if (d_is_positive(h_dentry)) {
9936 +                       au_set_dbbot(dentry, bindex);
9937 +                       return;
9938 +               }
9939 +               au_set_h_dptr(dentry, bindex, NULL);
9940 +       }
9941 +}
9942 +
9943 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9944 +{
9945 +       aufs_bindex_t bindex, bbot;
9946 +
9947 +       bbot = au_dbbot(dentry);
9948 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9949 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9950 +                       return bindex;
9951 +       return -1;
9952 +}
9953 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9954 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9955 +++ linux/fs/aufs/dir.c 2023-09-03 02:21:58.859971007 +0200
9956 @@ -0,0 +1,765 @@
9957 +// SPDX-License-Identifier: GPL-2.0
9958 +/*
9959 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9960 + *
9961 + * This program is free software; you can redistribute it and/or modify
9962 + * it under the terms of the GNU General Public License as published by
9963 + * the Free Software Foundation; either version 2 of the License, or
9964 + * (at your option) any later version.
9965 + *
9966 + * This program is distributed in the hope that it will be useful,
9967 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9968 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9969 + * GNU General Public License for more details.
9970 + *
9971 + * You should have received a copy of the GNU General Public License
9972 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9973 + */
9974 +
9975 +/*
9976 + * directory operations
9977 + */
9978 +
9979 +#include <linux/fs_stack.h>
9980 +#include <linux/iversion.h>
9981 +#include "aufs.h"
9982 +
9983 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9984 +{
9985 +       unsigned int nlink;
9986 +
9987 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9988 +
9989 +       nlink = dir->i_nlink;
9990 +       nlink += h_dir->i_nlink - 2;
9991 +       if (h_dir->i_nlink < 2)
9992 +               nlink += 2;
9993 +       smp_mb(); /* for i_nlink */
9994 +       /* 0 can happen in revaliding */
9995 +       set_nlink(dir, nlink);
9996 +}
9997 +
9998 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9999 +{
10000 +       unsigned int nlink;
10001 +
10002 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10003 +
10004 +       nlink = dir->i_nlink;
10005 +       nlink -= h_dir->i_nlink - 2;
10006 +       if (h_dir->i_nlink < 2)
10007 +               nlink -= 2;
10008 +       smp_mb(); /* for i_nlink */
10009 +       /* nlink == 0 means the branch-fs is broken */
10010 +       set_nlink(dir, nlink);
10011 +}
10012 +
10013 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10014 +{
10015 +       loff_t sz;
10016 +       aufs_bindex_t bindex, bbot;
10017 +       struct file *h_file;
10018 +       struct dentry *h_dentry;
10019 +
10020 +       sz = 0;
10021 +       if (file) {
10022 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10023 +
10024 +               bbot = au_fbbot_dir(file);
10025 +               for (bindex = au_fbtop(file);
10026 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10027 +                    bindex++) {
10028 +                       h_file = au_hf_dir(file, bindex);
10029 +                       if (h_file && file_inode(h_file))
10030 +                               sz += vfsub_f_size_read(h_file);
10031 +               }
10032 +       } else {
10033 +               AuDebugOn(!dentry);
10034 +               AuDebugOn(!d_is_dir(dentry));
10035 +
10036 +               bbot = au_dbtaildir(dentry);
10037 +               for (bindex = au_dbtop(dentry);
10038 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10039 +                    bindex++) {
10040 +                       h_dentry = au_h_dptr(dentry, bindex);
10041 +                       if (h_dentry && d_is_positive(h_dentry))
10042 +                               sz += i_size_read(d_inode(h_dentry));
10043 +               }
10044 +       }
10045 +       if (sz < KMALLOC_MAX_SIZE)
10046 +               sz = roundup_pow_of_two(sz);
10047 +       if (sz > KMALLOC_MAX_SIZE)
10048 +               sz = KMALLOC_MAX_SIZE;
10049 +       else if (sz < NAME_MAX) {
10050 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10051 +               sz = AUFS_RDBLK_DEF;
10052 +       }
10053 +       return sz;
10054 +}
10055 +
10056 +struct au_dir_ts_arg {
10057 +       struct dentry *dentry;
10058 +       aufs_bindex_t brid;
10059 +};
10060 +
10061 +static void au_do_dir_ts(void *arg)
10062 +{
10063 +       struct au_dir_ts_arg *a = arg;
10064 +       struct au_dtime dt;
10065 +       struct path h_path;
10066 +       struct inode *dir, *h_dir;
10067 +       struct super_block *sb;
10068 +       struct au_branch *br;
10069 +       struct au_hinode *hdir;
10070 +       int err;
10071 +       aufs_bindex_t btop, bindex;
10072 +
10073 +       sb = a->dentry->d_sb;
10074 +       if (d_really_is_negative(a->dentry))
10075 +               goto out;
10076 +       /* no dir->i_mutex lock */
10077 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10078 +
10079 +       dir = d_inode(a->dentry);
10080 +       btop = au_ibtop(dir);
10081 +       bindex = au_br_index(sb, a->brid);
10082 +       if (bindex < btop)
10083 +               goto out_unlock;
10084 +
10085 +       br = au_sbr(sb, bindex);
10086 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10087 +       if (!h_path.dentry)
10088 +               goto out_unlock;
10089 +       h_path.mnt = au_br_mnt(br);
10090 +       au_dtime_store(&dt, a->dentry, &h_path);
10091 +
10092 +       br = au_sbr(sb, btop);
10093 +       if (!au_br_writable(br->br_perm))
10094 +               goto out_unlock;
10095 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10096 +       h_path.mnt = au_br_mnt(br);
10097 +       err = vfsub_mnt_want_write(h_path.mnt);
10098 +       if (err)
10099 +               goto out_unlock;
10100 +       hdir = au_hi(dir, btop);
10101 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10102 +       h_dir = au_h_iptr(dir, btop);
10103 +       if (h_dir->i_nlink
10104 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10105 +               dt.dt_h_path = h_path;
10106 +               au_dtime_revert(&dt);
10107 +       }
10108 +       au_hn_inode_unlock(hdir);
10109 +       vfsub_mnt_drop_write(h_path.mnt);
10110 +       au_cpup_attr_timesizes(dir);
10111 +
10112 +out_unlock:
10113 +       aufs_read_unlock(a->dentry, AuLock_DW);
10114 +out:
10115 +       dput(a->dentry);
10116 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10117 +       au_kfree_try_rcu(arg);
10118 +}
10119 +
10120 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10121 +{
10122 +       int perm, wkq_err;
10123 +       aufs_bindex_t btop;
10124 +       struct au_dir_ts_arg *arg;
10125 +       struct dentry *dentry;
10126 +       struct super_block *sb;
10127 +
10128 +       IMustLock(dir);
10129 +
10130 +       dentry = d_find_any_alias(dir);
10131 +       AuDebugOn(!dentry);
10132 +       sb = dentry->d_sb;
10133 +       btop = au_ibtop(dir);
10134 +       if (btop == bindex) {
10135 +               au_cpup_attr_timesizes(dir);
10136 +               goto out;
10137 +       }
10138 +
10139 +       perm = au_sbr_perm(sb, btop);
10140 +       if (!au_br_writable(perm))
10141 +               goto out;
10142 +
10143 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10144 +       if (!arg)
10145 +               goto out;
10146 +
10147 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10148 +       arg->brid = au_sbr_id(sb, bindex);
10149 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10150 +       if (unlikely(wkq_err)) {
10151 +               pr_err("wkq %d\n", wkq_err);
10152 +               dput(dentry);
10153 +               au_kfree_try_rcu(arg);
10154 +       }
10155 +
10156 +out:
10157 +       dput(dentry);
10158 +}
10159 +
10160 +/* ---------------------------------------------------------------------- */
10161 +
10162 +static int reopen_dir(struct file *file)
10163 +{
10164 +       int err;
10165 +       unsigned int flags;
10166 +       aufs_bindex_t bindex, btail, btop;
10167 +       struct dentry *dentry, *h_dentry;
10168 +       struct file *h_file;
10169 +
10170 +       /* open all lower dirs */
10171 +       dentry = file->f_path.dentry;
10172 +       btop = au_dbtop(dentry);
10173 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10174 +               au_set_h_fptr(file, bindex, NULL);
10175 +       au_set_fbtop(file, btop);
10176 +
10177 +       btail = au_dbtaildir(dentry);
10178 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10179 +               au_set_h_fptr(file, bindex, NULL);
10180 +       au_set_fbbot_dir(file, btail);
10181 +
10182 +       flags = vfsub_file_flags(file);
10183 +       for (bindex = btop; bindex <= btail; bindex++) {
10184 +               h_dentry = au_h_dptr(dentry, bindex);
10185 +               if (!h_dentry)
10186 +                       continue;
10187 +               h_file = au_hf_dir(file, bindex);
10188 +               if (h_file)
10189 +                       continue;
10190 +
10191 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10192 +               err = PTR_ERR(h_file);
10193 +               if (IS_ERR(h_file))
10194 +                       goto out; /* close all? */
10195 +               au_set_h_fptr(file, bindex, h_file);
10196 +       }
10197 +       au_update_figen(file);
10198 +       /* todo: necessary? */
10199 +       /* file->f_ra = h_file->f_ra; */
10200 +       err = 0;
10201 +
10202 +out:
10203 +       return err;
10204 +}
10205 +
10206 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10207 +{
10208 +       int err;
10209 +       aufs_bindex_t bindex, btail;
10210 +       struct dentry *dentry, *h_dentry;
10211 +       struct vfsmount *mnt;
10212 +
10213 +       FiMustWriteLock(file);
10214 +       AuDebugOn(h_file);
10215 +
10216 +       err = 0;
10217 +       mnt = file->f_path.mnt;
10218 +       dentry = file->f_path.dentry;
10219 +       file->f_version = inode_query_iversion(d_inode(dentry));
10220 +       bindex = au_dbtop(dentry);
10221 +       au_set_fbtop(file, bindex);
10222 +       btail = au_dbtaildir(dentry);
10223 +       au_set_fbbot_dir(file, btail);
10224 +       for (; !err && bindex <= btail; bindex++) {
10225 +               h_dentry = au_h_dptr(dentry, bindex);
10226 +               if (!h_dentry)
10227 +                       continue;
10228 +
10229 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10230 +               if (unlikely(err))
10231 +                       break;
10232 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10233 +               if (IS_ERR(h_file)) {
10234 +                       err = PTR_ERR(h_file);
10235 +                       break;
10236 +               }
10237 +               au_set_h_fptr(file, bindex, h_file);
10238 +       }
10239 +       au_update_figen(file);
10240 +       /* todo: necessary? */
10241 +       /* file->f_ra = h_file->f_ra; */
10242 +       if (!err)
10243 +               return 0; /* success */
10244 +
10245 +       /* close all */
10246 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10247 +               au_set_h_fptr(file, bindex, NULL);
10248 +       au_set_fbtop(file, -1);
10249 +       au_set_fbbot_dir(file, -1);
10250 +
10251 +       return err;
10252 +}
10253 +
10254 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10255 +                        struct file *file)
10256 +{
10257 +       int err;
10258 +       struct super_block *sb;
10259 +       struct au_fidir *fidir;
10260 +
10261 +       err = -ENOMEM;
10262 +       sb = file->f_path.dentry->d_sb;
10263 +       si_read_lock(sb, AuLock_FLUSH);
10264 +       fidir = au_fidir_alloc(sb);
10265 +       if (fidir) {
10266 +               struct au_do_open_args args = {
10267 +                       .open   = do_open_dir,
10268 +                       .fidir  = fidir
10269 +               };
10270 +               err = au_do_open(file, &args);
10271 +               if (unlikely(err))
10272 +                       au_kfree_rcu(fidir);
10273 +       }
10274 +       si_read_unlock(sb);
10275 +       return err;
10276 +}
10277 +
10278 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10279 +                           struct file *file)
10280 +{
10281 +       struct au_vdir *vdir_cache;
10282 +       struct au_finfo *finfo;
10283 +       struct au_fidir *fidir;
10284 +       struct au_hfile *hf;
10285 +       aufs_bindex_t bindex, bbot;
10286 +
10287 +       finfo = au_fi(file);
10288 +       fidir = finfo->fi_hdir;
10289 +       if (fidir) {
10290 +               au_hbl_del(&finfo->fi_hlist,
10291 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10292 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10293 +               if (vdir_cache)
10294 +                       au_vdir_free(vdir_cache);
10295 +
10296 +               bindex = finfo->fi_btop;
10297 +               if (bindex >= 0) {
10298 +                       hf = fidir->fd_hfile + bindex;
10299 +                       /*
10300 +                        * calls fput() instead of filp_close(),
10301 +                        * since no dnotify or lock for the lower file.
10302 +                        */
10303 +                       bbot = fidir->fd_bbot;
10304 +                       for (; bindex <= bbot; bindex++, hf++)
10305 +                               if (hf->hf_file)
10306 +                                       au_hfput(hf, /*execed*/0);
10307 +               }
10308 +               au_kfree_rcu(fidir);
10309 +               finfo->fi_hdir = NULL;
10310 +       }
10311 +       au_finfo_fin(file);
10312 +       return 0;
10313 +}
10314 +
10315 +/* ---------------------------------------------------------------------- */
10316 +
10317 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10318 +{
10319 +       int err;
10320 +       aufs_bindex_t bindex, bbot;
10321 +       struct file *h_file;
10322 +
10323 +       err = 0;
10324 +       bbot = au_fbbot_dir(file);
10325 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10326 +               h_file = au_hf_dir(file, bindex);
10327 +               if (h_file)
10328 +                       err = vfsub_flush(h_file, id);
10329 +       }
10330 +       return err;
10331 +}
10332 +
10333 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10334 +{
10335 +       return au_do_flush(file, id, au_do_flush_dir);
10336 +}
10337 +
10338 +/* ---------------------------------------------------------------------- */
10339 +
10340 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10341 +{
10342 +       int err;
10343 +       aufs_bindex_t bbot, bindex;
10344 +       struct inode *inode;
10345 +       struct super_block *sb;
10346 +
10347 +       err = 0;
10348 +       sb = dentry->d_sb;
10349 +       inode = d_inode(dentry);
10350 +       IMustLock(inode);
10351 +       bbot = au_dbbot(dentry);
10352 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10353 +               struct path h_path;
10354 +
10355 +               if (au_test_ro(sb, bindex, inode))
10356 +                       continue;
10357 +               h_path.dentry = au_h_dptr(dentry, bindex);
10358 +               if (!h_path.dentry)
10359 +                       continue;
10360 +
10361 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10362 +               err = vfsub_fsync(NULL, &h_path, datasync);
10363 +       }
10364 +
10365 +       return err;
10366 +}
10367 +
10368 +static int au_do_fsync_dir(struct file *file, int datasync)
10369 +{
10370 +       int err;
10371 +       aufs_bindex_t bbot, bindex;
10372 +       struct file *h_file;
10373 +       struct super_block *sb;
10374 +       struct inode *inode;
10375 +
10376 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10377 +       if (unlikely(err))
10378 +               goto out;
10379 +
10380 +       inode = file_inode(file);
10381 +       sb = inode->i_sb;
10382 +       bbot = au_fbbot_dir(file);
10383 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10384 +               h_file = au_hf_dir(file, bindex);
10385 +               if (!h_file || au_test_ro(sb, bindex, inode))
10386 +                       continue;
10387 +
10388 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10389 +       }
10390 +
10391 +out:
10392 +       return err;
10393 +}
10394 +
10395 +/*
10396 + * @file may be NULL
10397 + */
10398 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10399 +                         int datasync)
10400 +{
10401 +       int err;
10402 +       struct dentry *dentry;
10403 +       struct inode *inode;
10404 +       struct super_block *sb;
10405 +
10406 +       err = 0;
10407 +       dentry = file->f_path.dentry;
10408 +       inode = d_inode(dentry);
10409 +       inode_lock(inode);
10410 +       sb = dentry->d_sb;
10411 +       si_noflush_read_lock(sb);
10412 +       if (file)
10413 +               err = au_do_fsync_dir(file, datasync);
10414 +       else {
10415 +               di_write_lock_child(dentry);
10416 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10417 +       }
10418 +       au_cpup_attr_timesizes(inode);
10419 +       di_write_unlock(dentry);
10420 +       if (file)
10421 +               fi_write_unlock(file);
10422 +
10423 +       si_read_unlock(sb);
10424 +       inode_unlock(inode);
10425 +       return err;
10426 +}
10427 +
10428 +/* ---------------------------------------------------------------------- */
10429 +
10430 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10431 +{
10432 +       int err;
10433 +       struct dentry *dentry;
10434 +       struct inode *inode, *h_inode;
10435 +       struct super_block *sb;
10436 +
10437 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10438 +
10439 +       dentry = file->f_path.dentry;
10440 +       inode = d_inode(dentry);
10441 +       IMustLock(inode);
10442 +
10443 +       sb = dentry->d_sb;
10444 +       si_read_lock(sb, AuLock_FLUSH);
10445 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10446 +       if (unlikely(err))
10447 +               goto out;
10448 +       err = au_alive_dir(dentry);
10449 +       if (!err)
10450 +               err = au_vdir_init(file);
10451 +       di_downgrade_lock(dentry, AuLock_IR);
10452 +       if (unlikely(err))
10453 +               goto out_unlock;
10454 +
10455 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10456 +       if (!au_test_nfsd()) {
10457 +               err = au_vdir_fill_de(file, ctx);
10458 +               fsstack_copy_attr_atime(inode, h_inode);
10459 +       } else {
10460 +               /*
10461 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10462 +                * encode_fh() and others.
10463 +                */
10464 +               atomic_inc(&h_inode->i_count);
10465 +               di_read_unlock(dentry, AuLock_IR);
10466 +               si_read_unlock(sb);
10467 +               err = au_vdir_fill_de(file, ctx);
10468 +               fsstack_copy_attr_atime(inode, h_inode);
10469 +               fi_write_unlock(file);
10470 +               iput(h_inode);
10471 +
10472 +               AuTraceErr(err);
10473 +               return err;
10474 +       }
10475 +
10476 +out_unlock:
10477 +       di_read_unlock(dentry, AuLock_IR);
10478 +       fi_write_unlock(file);
10479 +out:
10480 +       si_read_unlock(sb);
10481 +       return err;
10482 +}
10483 +
10484 +/* ---------------------------------------------------------------------- */
10485 +
10486 +#define AuTestEmpty_WHONLY     1
10487 +#define AuTestEmpty_CALLED     (1 << 1)
10488 +#define AuTestEmpty_SHWH       (1 << 2)
10489 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10490 +#define au_fset_testempty(flags, name) \
10491 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10492 +#define au_fclr_testempty(flags, name) \
10493 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10494 +
10495 +#ifndef CONFIG_AUFS_SHWH
10496 +#undef AuTestEmpty_SHWH
10497 +#define AuTestEmpty_SHWH       0
10498 +#endif
10499 +
10500 +struct test_empty_arg {
10501 +       struct dir_context ctx;
10502 +       struct au_nhash *whlist;
10503 +       unsigned int flags;
10504 +       int err;
10505 +       aufs_bindex_t bindex;
10506 +};
10507 +
10508 +static bool test_empty_cb(struct dir_context *ctx, const char *__name,
10509 +                         int namelen, loff_t offset __maybe_unused, u64 ino,
10510 +                         unsigned int d_type)
10511 +{
10512 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10513 +                                                 ctx);
10514 +       char *name = (void *)__name;
10515 +
10516 +       arg->err = 0;
10517 +       au_fset_testempty(arg->flags, CALLED);
10518 +       /* smp_mb(); */
10519 +       if (name[0] == '.'
10520 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10521 +               goto out; /* success */
10522 +
10523 +       if (namelen <= AUFS_WH_PFX_LEN
10524 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10525 +               if (au_ftest_testempty(arg->flags, WHONLY)
10526 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10527 +                       arg->err = -ENOTEMPTY;
10528 +               goto out;
10529 +       }
10530 +
10531 +       name += AUFS_WH_PFX_LEN;
10532 +       namelen -= AUFS_WH_PFX_LEN;
10533 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10534 +               arg->err = au_nhash_append_wh
10535 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10536 +                        au_ftest_testempty(arg->flags, SHWH));
10537 +
10538 +out:
10539 +       /* smp_mb(); */
10540 +       AuTraceErr(arg->err);
10541 +       return !arg->err;
10542 +}
10543 +
10544 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10545 +{
10546 +       int err;
10547 +       struct file *h_file;
10548 +       struct au_branch *br;
10549 +
10550 +       h_file = au_h_open(dentry, arg->bindex,
10551 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10552 +                          /*file*/NULL, /*force_wr*/0);
10553 +       err = PTR_ERR(h_file);
10554 +       if (IS_ERR(h_file))
10555 +               goto out;
10556 +
10557 +       err = 0;
10558 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10559 +           && !file_inode(h_file)->i_nlink)
10560 +               goto out_put;
10561 +
10562 +       do {
10563 +               arg->err = 0;
10564 +               au_fclr_testempty(arg->flags, CALLED);
10565 +               /* smp_mb(); */
10566 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10567 +               if (err >= 0)
10568 +                       err = arg->err;
10569 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10570 +
10571 +out_put:
10572 +       fput(h_file);
10573 +       br = au_sbr(dentry->d_sb, arg->bindex);
10574 +       au_lcnt_dec(&br->br_nfiles);
10575 +out:
10576 +       return err;
10577 +}
10578 +
10579 +struct do_test_empty_args {
10580 +       int *errp;
10581 +       struct dentry *dentry;
10582 +       struct test_empty_arg *arg;
10583 +};
10584 +
10585 +static void call_do_test_empty(void *args)
10586 +{
10587 +       struct do_test_empty_args *a = args;
10588 +       *a->errp = do_test_empty(a->dentry, a->arg);
10589 +}
10590 +
10591 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10592 +{
10593 +       int err, wkq_err;
10594 +       struct dentry *h_dentry;
10595 +       struct inode *h_inode;
10596 +       struct mnt_idmap *h_idmap;
10597 +
10598 +       h_idmap = au_sbr_idmap(dentry->d_sb, arg->bindex);
10599 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10600 +       h_inode = d_inode(h_dentry);
10601 +       /* todo: i_mode changes anytime? */
10602 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10603 +       err = au_test_h_perm_sio(h_idmap, h_inode, MAY_EXEC | MAY_READ);
10604 +       inode_unlock_shared(h_inode);
10605 +       if (!err)
10606 +               err = do_test_empty(dentry, arg);
10607 +       else {
10608 +               struct do_test_empty_args args = {
10609 +                       .errp   = &err,
10610 +                       .dentry = dentry,
10611 +                       .arg    = arg
10612 +               };
10613 +               unsigned int flags = arg->flags;
10614 +
10615 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10616 +               if (unlikely(wkq_err))
10617 +                       err = wkq_err;
10618 +               arg->flags = flags;
10619 +       }
10620 +
10621 +       return err;
10622 +}
10623 +
10624 +int au_test_empty_lower(struct dentry *dentry)
10625 +{
10626 +       int err;
10627 +       unsigned int rdhash;
10628 +       aufs_bindex_t bindex, btop, btail;
10629 +       struct au_nhash whlist;
10630 +       struct test_empty_arg arg = {
10631 +               .ctx = {
10632 +                       .actor = test_empty_cb
10633 +               }
10634 +       };
10635 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10636 +
10637 +       SiMustAnyLock(dentry->d_sb);
10638 +
10639 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10640 +       if (!rdhash)
10641 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10642 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10643 +       if (unlikely(err))
10644 +               goto out;
10645 +
10646 +       arg.flags = 0;
10647 +       arg.whlist = &whlist;
10648 +       btop = au_dbtop(dentry);
10649 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10650 +               au_fset_testempty(arg.flags, SHWH);
10651 +       test_empty = do_test_empty;
10652 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10653 +               test_empty = sio_test_empty;
10654 +       arg.bindex = btop;
10655 +       err = test_empty(dentry, &arg);
10656 +       if (unlikely(err))
10657 +               goto out_whlist;
10658 +
10659 +       au_fset_testempty(arg.flags, WHONLY);
10660 +       btail = au_dbtaildir(dentry);
10661 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10662 +               struct dentry *h_dentry;
10663 +
10664 +               h_dentry = au_h_dptr(dentry, bindex);
10665 +               if (h_dentry && d_is_positive(h_dentry)) {
10666 +                       arg.bindex = bindex;
10667 +                       err = test_empty(dentry, &arg);
10668 +               }
10669 +       }
10670 +
10671 +out_whlist:
10672 +       au_nhash_wh_free(&whlist);
10673 +out:
10674 +       return err;
10675 +}
10676 +
10677 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10678 +{
10679 +       int err;
10680 +       struct test_empty_arg arg = {
10681 +               .ctx = {
10682 +                       .actor = test_empty_cb
10683 +               }
10684 +       };
10685 +       aufs_bindex_t bindex, btail;
10686 +
10687 +       err = 0;
10688 +       arg.whlist = whlist;
10689 +       arg.flags = AuTestEmpty_WHONLY;
10690 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10691 +               au_fset_testempty(arg.flags, SHWH);
10692 +       btail = au_dbtaildir(dentry);
10693 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10694 +               struct dentry *h_dentry;
10695 +
10696 +               h_dentry = au_h_dptr(dentry, bindex);
10697 +               if (h_dentry && d_is_positive(h_dentry)) {
10698 +                       arg.bindex = bindex;
10699 +                       err = sio_test_empty(dentry, &arg);
10700 +               }
10701 +       }
10702 +
10703 +       return err;
10704 +}
10705 +
10706 +/* ---------------------------------------------------------------------- */
10707 +
10708 +const struct file_operations aufs_dir_fop = {
10709 +       .owner          = THIS_MODULE,
10710 +       .llseek         = default_llseek,
10711 +       .read           = generic_read_dir,
10712 +       .iterate_shared = aufs_iterate_shared,
10713 +       .unlocked_ioctl = aufs_ioctl_dir,
10714 +#ifdef CONFIG_COMPAT
10715 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10716 +#endif
10717 +       .open           = aufs_open_dir,
10718 +       .release        = aufs_release_dir,
10719 +       .flush          = aufs_flush_dir,
10720 +       .fsync          = aufs_fsync_dir
10721 +};
10722 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10723 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10724 +++ linux/fs/aufs/dir.h 2022-11-05 23:02:18.962555950 +0100
10725 @@ -0,0 +1,134 @@
10726 +/* SPDX-License-Identifier: GPL-2.0 */
10727 +/*
10728 + * Copyright (C) 2005-2022 Junjiro R. Okajima
10729 + *
10730 + * This program is free software; you can redistribute it and/or modify
10731 + * it under the terms of the GNU General Public License as published by
10732 + * the Free Software Foundation; either version 2 of the License, or
10733 + * (at your option) any later version.
10734 + *
10735 + * This program is distributed in the hope that it will be useful,
10736 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10737 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10738 + * GNU General Public License for more details.
10739 + *
10740 + * You should have received a copy of the GNU General Public License
10741 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10742 + */
10743 +
10744 +/*
10745 + * directory operations
10746 + */
10747 +
10748 +#ifndef __AUFS_DIR_H__
10749 +#define __AUFS_DIR_H__
10750 +
10751 +#ifdef __KERNEL__
10752 +
10753 +#include <linux/fs.h>
10754 +
10755 +/* ---------------------------------------------------------------------- */
10756 +
10757 +/* need to be faster and smaller */
10758 +
10759 +struct au_nhash {
10760 +       unsigned int            nh_num;
10761 +       struct hlist_head       *nh_head;
10762 +};
10763 +
10764 +struct au_vdir_destr {
10765 +       unsigned char   len;
10766 +       unsigned char   name[];
10767 +} __packed;
10768 +
10769 +struct au_vdir_dehstr {
10770 +       struct hlist_node       hash;
10771 +       struct au_vdir_destr    *str;
10772 +       struct rcu_head         rcu;
10773 +} ____cacheline_aligned_in_smp;
10774 +
10775 +struct au_vdir_de {
10776 +       ino_t                   de_ino;
10777 +       unsigned char           de_type;
10778 +       /* caution: packed */
10779 +       struct au_vdir_destr    de_str;
10780 +} __packed;
10781 +
10782 +struct au_vdir_wh {
10783 +       struct hlist_node       wh_hash;
10784 +#ifdef CONFIG_AUFS_SHWH
10785 +       ino_t                   wh_ino;
10786 +       aufs_bindex_t           wh_bindex;
10787 +       unsigned char           wh_type;
10788 +#else
10789 +       aufs_bindex_t           wh_bindex;
10790 +#endif
10791 +       /* caution: packed */
10792 +       struct au_vdir_destr    wh_str;
10793 +} __packed;
10794 +
10795 +union au_vdir_deblk_p {
10796 +       unsigned char           *deblk;
10797 +       struct au_vdir_de       *de;
10798 +};
10799 +
10800 +struct au_vdir {
10801 +       unsigned char   **vd_deblk;
10802 +       unsigned long   vd_nblk;
10803 +       struct {
10804 +               unsigned long           ul;
10805 +               union au_vdir_deblk_p   p;
10806 +       } vd_last;
10807 +
10808 +       u64             vd_version;
10809 +       unsigned int    vd_deblk_sz;
10810 +       unsigned long   vd_jiffy;
10811 +       struct rcu_head rcu;
10812 +} ____cacheline_aligned_in_smp;
10813 +
10814 +/* ---------------------------------------------------------------------- */
10815 +
10816 +/* dir.c */
10817 +extern const struct file_operations aufs_dir_fop;
10818 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10819 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10820 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10821 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10822 +int au_test_empty_lower(struct dentry *dentry);
10823 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10824 +
10825 +/* vdir.c */
10826 +unsigned int au_rdhash_est(loff_t sz);
10827 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10828 +void au_nhash_wh_free(struct au_nhash *whlist);
10829 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10830 +                           int limit);
10831 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10832 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10833 +                      unsigned int d_type, aufs_bindex_t bindex,
10834 +                      unsigned char shwh);
10835 +void au_vdir_free(struct au_vdir *vdir);
10836 +int au_vdir_init(struct file *file);
10837 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10838 +
10839 +/* ioctl.c */
10840 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10841 +
10842 +#ifdef CONFIG_AUFS_RDU
10843 +/* rdu.c */
10844 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10845 +#ifdef CONFIG_COMPAT
10846 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10847 +                        unsigned long arg);
10848 +#endif
10849 +#else
10850 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10851 +       unsigned int cmd, unsigned long arg)
10852 +#ifdef CONFIG_COMPAT
10853 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10854 +       unsigned int cmd, unsigned long arg)
10855 +#endif
10856 +#endif
10857 +
10858 +#endif /* __KERNEL__ */
10859 +#endif /* __AUFS_DIR_H__ */
10860 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10861 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10862 +++ linux/fs/aufs/dirren.c      2022-11-05 23:02:18.962555950 +0100
10863 @@ -0,0 +1,1315 @@
10864 +// SPDX-License-Identifier: GPL-2.0
10865 +/*
10866 + * Copyright (C) 2017-2022 Junjiro R. Okajima
10867 + *
10868 + * This program is free software; you can redistribute it and/or modify
10869 + * it under the terms of the GNU General Public License as published by
10870 + * the Free Software Foundation; either version 2 of the License, or
10871 + * (at your option) any later version.
10872 + *
10873 + * This program is distributed in the hope that it will be useful,
10874 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10875 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10876 + * GNU General Public License for more details.
10877 + *
10878 + * You should have received a copy of the GNU General Public License
10879 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10880 + */
10881 +
10882 +/*
10883 + * special handling in renaming a directory
10884 + * in order to support looking-up the before-renamed name on the lower readonly
10885 + * branches
10886 + */
10887 +
10888 +#include <linux/byteorder/generic.h>
10889 +#include "aufs.h"
10890 +
10891 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10892 +{
10893 +       int idx;
10894 +
10895 +       idx = au_dr_ihash(ent->dr_h_ino);
10896 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10897 +}
10898 +
10899 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10900 +{
10901 +       int ret, i;
10902 +       struct hlist_bl_head *hbl;
10903 +
10904 +       ret = 1;
10905 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10906 +               hbl = dr->dr_h_ino + i;
10907 +               hlist_bl_lock(hbl);
10908 +               ret &= hlist_bl_empty(hbl);
10909 +               hlist_bl_unlock(hbl);
10910 +       }
10911 +
10912 +       return ret;
10913 +}
10914 +
10915 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10916 +{
10917 +       struct au_dr_hino *found, *ent;
10918 +       struct hlist_bl_head *hbl;
10919 +       struct hlist_bl_node *pos;
10920 +       int idx;
10921 +
10922 +       found = NULL;
10923 +       idx = au_dr_ihash(ino);
10924 +       hbl = dr->dr_h_ino + idx;
10925 +       hlist_bl_lock(hbl);
10926 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10927 +               if (ent->dr_h_ino == ino) {
10928 +                       found = ent;
10929 +                       break;
10930 +               }
10931 +       hlist_bl_unlock(hbl);
10932 +
10933 +       return found;
10934 +}
10935 +
10936 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10937 +                       struct au_dr_hino *add_ent)
10938 +{
10939 +       int found, idx;
10940 +       struct hlist_bl_head *hbl;
10941 +       struct hlist_bl_node *pos;
10942 +       struct au_dr_hino *ent;
10943 +
10944 +       found = 0;
10945 +       idx = au_dr_ihash(ino);
10946 +       hbl = dr->dr_h_ino + idx;
10947 +#if 0 /* debug print */
10948 +       {
10949 +               struct hlist_bl_node *tmp;
10950 +
10951 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10952 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10953 +       }
10954 +#endif
10955 +       hlist_bl_lock(hbl);
10956 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10957 +               if (ent->dr_h_ino == ino) {
10958 +                       found = 1;
10959 +                       break;
10960 +               }
10961 +       if (!found && add_ent)
10962 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10963 +       hlist_bl_unlock(hbl);
10964 +
10965 +       if (!found && add_ent)
10966 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10967 +
10968 +       return found;
10969 +}
10970 +
10971 +void au_dr_hino_free(struct au_dr_br *dr)
10972 +{
10973 +       int i;
10974 +       struct hlist_bl_head *hbl;
10975 +       struct hlist_bl_node *pos, *tmp;
10976 +       struct au_dr_hino *ent;
10977 +
10978 +       /* SiMustWriteLock(sb); */
10979 +
10980 +       for (i = 0; i < AuDirren_NHASH; i++) {
10981 +               hbl = dr->dr_h_ino + i;
10982 +               /* no spinlock since sbinfo must be write-locked */
10983 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10984 +                       au_kfree_rcu(ent);
10985 +               INIT_HLIST_BL_HEAD(hbl);
10986 +       }
10987 +}
10988 +
10989 +/* returns the number of inodes or an error */
10990 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10991 +                           struct file *hinofile)
10992 +{
10993 +       int err, i;
10994 +       ssize_t ssz;
10995 +       loff_t pos, oldsize;
10996 +       __be64 u64;
10997 +       struct inode *hinoinode;
10998 +       struct hlist_bl_head *hbl;
10999 +       struct hlist_bl_node *n1, *n2;
11000 +       struct au_dr_hino *ent;
11001 +
11002 +       SiMustWriteLock(sb);
11003 +       AuDebugOn(!au_br_writable(br->br_perm));
11004 +
11005 +       hinoinode = file_inode(hinofile);
11006 +       oldsize = i_size_read(hinoinode);
11007 +
11008 +       err = 0;
11009 +       pos = 0;
11010 +       hbl = br->br_dirren.dr_h_ino;
11011 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11012 +               /* no bit-lock since sbinfo must be write-locked */
11013 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11014 +                       AuDbg("hi%llu, %pD2\n",
11015 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11016 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11017 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11018 +                       if (ssz == sizeof(u64))
11019 +                               continue;
11020 +
11021 +                       /* write error */
11022 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11023 +                       err = -ENOSPC;
11024 +                       if (ssz < 0)
11025 +                               err = ssz;
11026 +                       break;
11027 +               }
11028 +       }
11029 +       /* regardless the error */
11030 +       if (pos < oldsize) {
11031 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11032 +               AuTraceErr(err);
11033 +       }
11034 +
11035 +       AuTraceErr(err);
11036 +       return err;
11037 +}
11038 +
11039 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11040 +{
11041 +       int err, hidx;
11042 +       ssize_t ssz;
11043 +       size_t sz, n;
11044 +       loff_t pos;
11045 +       uint64_t u64;
11046 +       struct au_dr_hino *ent;
11047 +       struct inode *hinoinode;
11048 +       struct hlist_bl_head *hbl;
11049 +
11050 +       err = 0;
11051 +       pos = 0;
11052 +       hbl = dr->dr_h_ino;
11053 +       hinoinode = file_inode(hinofile);
11054 +       sz = i_size_read(hinoinode);
11055 +       AuDebugOn(sz % sizeof(u64));
11056 +       n = sz / sizeof(u64);
11057 +       while (n--) {
11058 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11059 +               if (unlikely(ssz != sizeof(u64))) {
11060 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11061 +                       err = -EINVAL;
11062 +                       if (ssz < 0)
11063 +                               err = ssz;
11064 +                       goto out_free;
11065 +               }
11066 +
11067 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11068 +               if (!ent) {
11069 +                       err = -ENOMEM;
11070 +                       AuTraceErr(err);
11071 +                       goto out_free;
11072 +               }
11073 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11074 +               AuDbg("hi%llu, %pD2\n",
11075 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11076 +               hidx = au_dr_ihash(ent->dr_h_ino);
11077 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11078 +       }
11079 +       goto out; /* success */
11080 +
11081 +out_free:
11082 +       au_dr_hino_free(dr);
11083 +out:
11084 +       AuTraceErr(err);
11085 +       return err;
11086 +}
11087 +
11088 +/*
11089 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11090 + * @path is a switch to distinguish load and store.
11091 + */
11092 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11093 +                     struct au_branch *br, const struct path *path)
11094 +{
11095 +       int err, flags;
11096 +       unsigned char load, suspend;
11097 +       struct file *hinofile;
11098 +       struct au_hinode *hdir;
11099 +       struct inode *dir, *delegated;
11100 +       struct path hinopath;
11101 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11102 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11103 +
11104 +       AuDebugOn(bindex < 0 && !br);
11105 +       AuDebugOn(bindex >= 0 && br);
11106 +
11107 +       err = -EINVAL;
11108 +       suspend = !br;
11109 +       if (suspend)
11110 +               br = au_sbr(sb, bindex);
11111 +       load = !!path;
11112 +       if (!load) {
11113 +               path = &br->br_path;
11114 +               AuDebugOn(!au_br_writable(br->br_perm));
11115 +               if (unlikely(!au_br_writable(br->br_perm)))
11116 +                       goto out;
11117 +       }
11118 +
11119 +       hdir = NULL;
11120 +       if (suspend) {
11121 +               dir = d_inode(sb->s_root);
11122 +               hdir = au_hinode(au_ii(dir), bindex);
11123 +               dir = hdir->hi_inode;
11124 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11125 +       } else {
11126 +               dir = d_inode(path->dentry);
11127 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11128 +       }
11129 +       hinopath.mnt = path->mnt;
11130 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11131 +       err = PTR_ERR(hinopath.dentry);
11132 +       if (IS_ERR(hinopath.dentry))
11133 +               goto out_unlock;
11134 +
11135 +       err = 0;
11136 +       flags = O_RDONLY;
11137 +       if (load) {
11138 +               if (d_is_negative(hinopath.dentry))
11139 +                       goto out_dput; /* success */
11140 +       } else {
11141 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11142 +                       if (d_is_positive(hinopath.dentry)) {
11143 +                               delegated = NULL;
11144 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11145 +                                                  /*force*/0);
11146 +                               AuTraceErr(err);
11147 +                               if (unlikely(err))
11148 +                                       pr_err("ignored err %d, %pd2\n",
11149 +                                              err, hinopath.dentry);
11150 +                               if (unlikely(err == -EWOULDBLOCK))
11151 +                                       iput(delegated);
11152 +                               err = 0;
11153 +                       }
11154 +                       goto out_dput;
11155 +               } else if (!d_is_positive(hinopath.dentry)) {
11156 +                       err = vfsub_create(dir, &hinopath, 0600,
11157 +                                          /*want_excl*/false);
11158 +                       AuTraceErr(err);
11159 +                       if (unlikely(err))
11160 +                               goto out_dput;
11161 +               }
11162 +               flags = O_WRONLY;
11163 +       }
11164 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11165 +       if (suspend)
11166 +               au_hn_inode_unlock(hdir);
11167 +       else
11168 +               inode_unlock(dir);
11169 +       dput(hinopath.dentry);
11170 +       AuTraceErrPtr(hinofile);
11171 +       if (IS_ERR(hinofile)) {
11172 +               err = PTR_ERR(hinofile);
11173 +               goto out;
11174 +       }
11175 +
11176 +       if (load)
11177 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11178 +       else
11179 +               err = au_dr_hino_store(sb, br, hinofile);
11180 +       fput(hinofile);
11181 +       goto out;
11182 +
11183 +out_dput:
11184 +       dput(hinopath.dentry);
11185 +out_unlock:
11186 +       if (suspend)
11187 +               au_hn_inode_unlock(hdir);
11188 +       else
11189 +               inode_unlock(dir);
11190 +out:
11191 +       AuTraceErr(err);
11192 +       return err;
11193 +}
11194 +
11195 +/* ---------------------------------------------------------------------- */
11196 +
11197 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11198 +{
11199 +       int err;
11200 +       struct kstatfs kstfs;
11201 +       dev_t dev;
11202 +       struct dentry *dentry;
11203 +       struct super_block *sb;
11204 +
11205 +       err = vfs_statfs((void *)path, &kstfs);
11206 +       AuTraceErr(err);
11207 +       if (unlikely(err))
11208 +               goto out;
11209 +
11210 +       /* todo: support for UUID */
11211 +
11212 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11213 +               brid->type = AuBrid_FSID;
11214 +               brid->fsid = kstfs.f_fsid;
11215 +       } else {
11216 +               dentry = path->dentry;
11217 +               sb = dentry->d_sb;
11218 +               dev = sb->s_dev;
11219 +               if (dev) {
11220 +                       brid->type = AuBrid_DEV;
11221 +                       brid->dev = dev;
11222 +               }
11223 +       }
11224 +
11225 +out:
11226 +       return err;
11227 +}
11228 +
11229 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11230 +                 const struct path *path)
11231 +{
11232 +       int err, i;
11233 +       struct au_dr_br *dr;
11234 +       struct hlist_bl_head *hbl;
11235 +
11236 +       dr = &br->br_dirren;
11237 +       hbl = dr->dr_h_ino;
11238 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11239 +               INIT_HLIST_BL_HEAD(hbl);
11240 +
11241 +       err = au_dr_brid_init(&dr->dr_brid, path);
11242 +       if (unlikely(err))
11243 +               goto out;
11244 +
11245 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11246 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11247 +
11248 +out:
11249 +       AuTraceErr(err);
11250 +       return err;
11251 +}
11252 +
11253 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11254 +{
11255 +       int err;
11256 +
11257 +       err = 0;
11258 +       if (au_br_writable(br->br_perm))
11259 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11260 +       if (!err)
11261 +               au_dr_hino_free(&br->br_dirren);
11262 +
11263 +       return err;
11264 +}
11265 +
11266 +/* ---------------------------------------------------------------------- */
11267 +
11268 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11269 +                      char *buf, size_t sz)
11270 +{
11271 +       int err;
11272 +       unsigned int major, minor;
11273 +       char *p;
11274 +
11275 +       p = buf;
11276 +       err = snprintf(p, sz, "%d_", brid->type);
11277 +       AuDebugOn(err > sz);
11278 +       p += err;
11279 +       sz -= err;
11280 +       switch (brid->type) {
11281 +       case AuBrid_Unset:
11282 +               return -EINVAL;
11283 +       case AuBrid_UUID:
11284 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11285 +               break;
11286 +       case AuBrid_FSID:
11287 +               err = snprintf(p, sz, "%08x-%08x",
11288 +                              brid->fsid.val[0], brid->fsid.val[1]);
11289 +               break;
11290 +       case AuBrid_DEV:
11291 +               major = MAJOR(brid->dev);
11292 +               minor = MINOR(brid->dev);
11293 +               if (major <= 0xff && minor <= 0xff)
11294 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11295 +               else
11296 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11297 +               break;
11298 +       }
11299 +       AuDebugOn(err > sz);
11300 +       p += err;
11301 +       sz -= err;
11302 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11303 +       AuDebugOn(err > sz);
11304 +       p += err;
11305 +       sz -= err;
11306 +
11307 +       return p - buf;
11308 +}
11309 +
11310 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11311 +{
11312 +       int rlen;
11313 +       struct dentry *br_dentry;
11314 +       struct inode *br_inode;
11315 +
11316 +       br_dentry = au_br_dentry(br);
11317 +       br_inode = d_inode(br_dentry);
11318 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11319 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11320 +       AuDebugOn(rlen > len);
11321 +
11322 +       return rlen;
11323 +}
11324 +
11325 +/* ---------------------------------------------------------------------- */
11326 +
11327 +/*
11328 + * from the given @h_dentry, construct drinfo at @*fdata.
11329 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11330 + * @allocated.
11331 + */
11332 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11333 +                              struct dentry *h_dentry,
11334 +                              unsigned char *allocated)
11335 +{
11336 +       int err, v;
11337 +       struct au_drinfo_fdata *f, *p;
11338 +       struct au_drinfo *drinfo;
11339 +       struct inode *h_inode;
11340 +       struct qstr *qname;
11341 +
11342 +       err = 0;
11343 +       f = *fdata;
11344 +       h_inode = d_inode(h_dentry);
11345 +       qname = &h_dentry->d_name;
11346 +       drinfo = &f->drinfo;
11347 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11348 +       drinfo->oldnamelen = qname->len;
11349 +       if (*allocated < sizeof(*f) + qname->len) {
11350 +               v = roundup_pow_of_two(*allocated + qname->len);
11351 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11352 +               if (unlikely(!p)) {
11353 +                       err = -ENOMEM;
11354 +                       AuTraceErr(err);
11355 +                       goto out;
11356 +               }
11357 +               f = p;
11358 +               *fdata = f;
11359 +               *allocated = v;
11360 +               drinfo = &f->drinfo;
11361 +       }
11362 +       memcpy(drinfo->oldname, qname->name, qname->len);
11363 +       AuDbg("i%llu, %.*s\n",
11364 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11365 +             drinfo->oldname);
11366 +
11367 +out:
11368 +       AuTraceErr(err);
11369 +       return err;
11370 +}
11371 +
11372 +/* callers have to free the return value */
11373 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11374 +{
11375 +       struct au_drinfo *ret, *drinfo;
11376 +       struct au_drinfo_fdata fdata;
11377 +       int len;
11378 +       loff_t pos;
11379 +       ssize_t ssz;
11380 +
11381 +       ret = ERR_PTR(-EIO);
11382 +       pos = 0;
11383 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11384 +       if (unlikely(ssz != sizeof(fdata))) {
11385 +               AuIOErr("ssz %zd, %u, %pD2\n",
11386 +                       ssz, (unsigned int)sizeof(fdata), file);
11387 +               goto out;
11388 +       }
11389 +
11390 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11391 +       switch (fdata.magic) {
11392 +       case AUFS_DRINFO_MAGIC_V1:
11393 +               break;
11394 +       default:
11395 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11396 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11397 +               goto out;
11398 +       }
11399 +
11400 +       drinfo = &fdata.drinfo;
11401 +       len = drinfo->oldnamelen;
11402 +       if (!len) {
11403 +               AuIOErr("broken drinfo %pD2\n", file);
11404 +               goto out;
11405 +       }
11406 +
11407 +       ret = NULL;
11408 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11409 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11410 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11411 +                     (unsigned long long)drinfo->ino,
11412 +                     (unsigned long long)h_ino, file);
11413 +               goto out; /* success */
11414 +       }
11415 +
11416 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11417 +       if (unlikely(!ret)) {
11418 +               ret = ERR_PTR(-ENOMEM);
11419 +               AuTraceErrPtr(ret);
11420 +               goto out;
11421 +       }
11422 +
11423 +       *ret = *drinfo;
11424 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11425 +       if (unlikely(ssz != len)) {
11426 +               au_kfree_rcu(ret);
11427 +               ret = ERR_PTR(-EIO);
11428 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11429 +               goto out;
11430 +       }
11431 +
11432 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11433 +
11434 +out:
11435 +       return ret;
11436 +}
11437 +
11438 +/* ---------------------------------------------------------------------- */
11439 +
11440 +/* in order to be revertible */
11441 +struct au_drinfo_rev_elm {
11442 +       int                     created;
11443 +       struct dentry           *info_dentry;
11444 +       struct au_drinfo        *info_last;
11445 +};
11446 +
11447 +struct au_drinfo_rev {
11448 +       unsigned char                   already;
11449 +       aufs_bindex_t                   nelm;
11450 +       struct au_drinfo_rev_elm        elm[];
11451 +};
11452 +
11453 +/* todo: isn't it too large? */
11454 +struct au_drinfo_store {
11455 +       struct path h_ppath;
11456 +       struct dentry *h_dentry;
11457 +       struct au_drinfo_fdata *fdata;
11458 +       char *infoname;                 /* inside of whname, just after PFX */
11459 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11460 +       aufs_bindex_t btgt, btail;
11461 +       unsigned char no_sio,
11462 +               allocated,              /* current size of *fdata */
11463 +               infonamelen,            /* room size for p */
11464 +               whnamelen,              /* length of the generated name */
11465 +               renameback;             /* renamed back */
11466 +};
11467 +
11468 +/* on rename(2) error, the caller should revert it using @elm */
11469 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11470 +                             struct au_drinfo_rev_elm *elm)
11471 +{
11472 +       int err, len;
11473 +       ssize_t ssz;
11474 +       loff_t pos;
11475 +       struct path infopath = {
11476 +               .mnt = w->h_ppath.mnt
11477 +       };
11478 +       struct inode *h_dir, *h_inode, *delegated;
11479 +       struct file *infofile;
11480 +       struct qstr *qname;
11481 +
11482 +       AuDebugOn(elm
11483 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11484 +
11485 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11486 +                                              w->whnamelen);
11487 +       AuTraceErrPtr(infopath.dentry);
11488 +       if (IS_ERR(infopath.dentry)) {
11489 +               err = PTR_ERR(infopath.dentry);
11490 +               goto out;
11491 +       }
11492 +
11493 +       err = 0;
11494 +       h_dir = d_inode(w->h_ppath.dentry);
11495 +       if (elm && d_is_negative(infopath.dentry)) {
11496 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11497 +               AuTraceErr(err);
11498 +               if (unlikely(err))
11499 +                       goto out_dput;
11500 +               elm->created = 1;
11501 +               elm->info_dentry = dget(infopath.dentry);
11502 +       }
11503 +
11504 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11505 +       AuTraceErrPtr(infofile);
11506 +       if (IS_ERR(infofile)) {
11507 +               err = PTR_ERR(infofile);
11508 +               goto out_dput;
11509 +       }
11510 +
11511 +       h_inode = d_inode(infopath.dentry);
11512 +       if (elm && i_size_read(h_inode)) {
11513 +               h_inode = d_inode(w->h_dentry);
11514 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11515 +               AuTraceErrPtr(elm->info_last);
11516 +               if (IS_ERR(elm->info_last)) {
11517 +                       err = PTR_ERR(elm->info_last);
11518 +                       elm->info_last = NULL;
11519 +                       AuDebugOn(elm->info_dentry);
11520 +                       goto out_fput;
11521 +               }
11522 +       }
11523 +
11524 +       if (elm && w->renameback) {
11525 +               delegated = NULL;
11526 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11527 +               AuTraceErr(err);
11528 +               if (unlikely(err == -EWOULDBLOCK))
11529 +                       iput(delegated);
11530 +               goto out_fput;
11531 +       }
11532 +
11533 +       pos = 0;
11534 +       qname = &w->h_dentry->d_name;
11535 +       len = sizeof(*w->fdata) + qname->len;
11536 +       if (!elm)
11537 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11538 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11539 +       if (ssz == len) {
11540 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11541 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11542 +               goto out_fput; /* success */
11543 +       } else {
11544 +               err = -EIO;
11545 +               if (ssz < 0)
11546 +                       err = ssz;
11547 +               /* the caller should revert it using @elm */
11548 +       }
11549 +
11550 +out_fput:
11551 +       fput(infofile);
11552 +out_dput:
11553 +       dput(infopath.dentry);
11554 +out:
11555 +       AuTraceErr(err);
11556 +       return err;
11557 +}
11558 +
11559 +struct au_call_drinfo_do_store_args {
11560 +       int *errp;
11561 +       struct au_drinfo_store *w;
11562 +       struct au_drinfo_rev_elm *elm;
11563 +};
11564 +
11565 +static void au_call_drinfo_do_store(void *args)
11566 +{
11567 +       struct au_call_drinfo_do_store_args *a = args;
11568 +
11569 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11570 +}
11571 +
11572 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11573 +                              struct au_drinfo_rev_elm *elm)
11574 +{
11575 +       int err, wkq_err;
11576 +
11577 +       if (w->no_sio)
11578 +               err = au_drinfo_do_store(w, elm);
11579 +       else {
11580 +               struct au_call_drinfo_do_store_args a = {
11581 +                       .errp   = &err,
11582 +                       .w      = w,
11583 +                       .elm    = elm
11584 +               };
11585 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11586 +               if (unlikely(wkq_err))
11587 +                       err = wkq_err;
11588 +       }
11589 +       AuTraceErr(err);
11590 +
11591 +       return err;
11592 +}
11593 +
11594 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11595 +                                    aufs_bindex_t btgt)
11596 +{
11597 +       int err;
11598 +
11599 +       memset(w, 0, sizeof(*w));
11600 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11601 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11602 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11603 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11604 +       w->btgt = btgt;
11605 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11606 +
11607 +       err = -ENOMEM;
11608 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11609 +       if (unlikely(!w->fdata)) {
11610 +               AuTraceErr(err);
11611 +               goto out;
11612 +       }
11613 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11614 +       err = 0;
11615 +
11616 +out:
11617 +       return err;
11618 +}
11619 +
11620 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11621 +{
11622 +       au_kfree_rcu(w->fdata);
11623 +}
11624 +
11625 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11626 +                               struct au_drinfo_store *w)
11627 +{
11628 +       struct au_drinfo_rev_elm *elm;
11629 +       struct inode *h_dir, *delegated;
11630 +       int err, nelm;
11631 +       struct path infopath = {
11632 +               .mnt = w->h_ppath.mnt
11633 +       };
11634 +
11635 +       h_dir = d_inode(w->h_ppath.dentry);
11636 +       IMustLock(h_dir);
11637 +
11638 +       err = 0;
11639 +       elm = rev->elm;
11640 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11641 +               AuDebugOn(elm->created && elm->info_last);
11642 +               if (elm->created) {
11643 +                       AuDbg("here\n");
11644 +                       delegated = NULL;
11645 +                       infopath.dentry = elm->info_dentry;
11646 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11647 +                                          !w->no_sio);
11648 +                       AuTraceErr(err);
11649 +                       if (unlikely(err == -EWOULDBLOCK))
11650 +                               iput(delegated);
11651 +                       dput(elm->info_dentry);
11652 +               } else if (elm->info_last) {
11653 +                       AuDbg("here\n");
11654 +                       w->fdata->drinfo = *elm->info_last;
11655 +                       memcpy(w->fdata->drinfo.oldname,
11656 +                              elm->info_last->oldname,
11657 +                              elm->info_last->oldnamelen);
11658 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11659 +                       au_kfree_rcu(elm->info_last);
11660 +               }
11661 +               if (unlikely(err))
11662 +                       AuIOErr("%d, %s\n", err, w->whname);
11663 +               /* go on even if err */
11664 +       }
11665 +}
11666 +
11667 +/* caller has to call au_dr_rename_fin() later */
11668 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11669 +                          struct qstr *dst_name, void *_rev)
11670 +{
11671 +       int err, sz, nelm;
11672 +       aufs_bindex_t bindex, btail;
11673 +       struct au_drinfo_store work;
11674 +       struct au_drinfo_rev *rev, **p;
11675 +       struct au_drinfo_rev_elm *elm;
11676 +       struct super_block *sb;
11677 +       struct au_branch *br;
11678 +       struct au_hinode *hdir;
11679 +
11680 +       err = au_drinfo_store_work_init(&work, btgt);
11681 +       AuTraceErr(err);
11682 +       if (unlikely(err))
11683 +               goto out;
11684 +
11685 +       err = -ENOMEM;
11686 +       btail = au_dbtaildir(dentry);
11687 +       nelm = btail - btgt;
11688 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11689 +       rev = kcalloc(1, sz, GFP_NOFS);
11690 +       if (unlikely(!rev)) {
11691 +               AuTraceErr(err);
11692 +               goto out_args;
11693 +       }
11694 +       rev->nelm = nelm;
11695 +       elm = rev->elm;
11696 +       p = _rev;
11697 +       *p = rev;
11698 +
11699 +       err = 0;
11700 +       sb = dentry->d_sb;
11701 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11702 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11703 +       hdir = au_hi(d_inode(dentry), btgt);
11704 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11705 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11706 +               work.h_dentry = au_h_dptr(dentry, bindex);
11707 +               if (!work.h_dentry)
11708 +                       continue;
11709 +
11710 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11711 +                                         &work.allocated);
11712 +               AuTraceErr(err);
11713 +               if (unlikely(err))
11714 +                       break;
11715 +
11716 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11717 +               br = au_sbr(sb, bindex);
11718 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11719 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11720 +                                                work.infonamelen);
11721 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11722 +                     work.whnamelen, work.whname,
11723 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11724 +                     work.fdata->drinfo.oldnamelen,
11725 +                     work.fdata->drinfo.oldname);
11726 +
11727 +               err = au_drinfo_store_sio(&work, elm);
11728 +               AuTraceErr(err);
11729 +               if (unlikely(err))
11730 +                       break;
11731 +       }
11732 +       if (unlikely(err)) {
11733 +               /* revert all drinfo */
11734 +               au_drinfo_store_rev(rev, &work);
11735 +               au_kfree_try_rcu(rev);
11736 +               *p = NULL;
11737 +       }
11738 +       au_hn_inode_unlock(hdir);
11739 +
11740 +out_args:
11741 +       au_drinfo_store_work_fin(&work);
11742 +out:
11743 +       return err;
11744 +}
11745 +
11746 +/* ---------------------------------------------------------------------- */
11747 +
11748 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11749 +                struct qstr *dst_name, void *_rev)
11750 +{
11751 +       int err, already;
11752 +       ino_t ino;
11753 +       struct super_block *sb;
11754 +       struct au_branch *br;
11755 +       struct au_dr_br *dr;
11756 +       struct dentry *h_dentry;
11757 +       struct inode *h_inode;
11758 +       struct au_dr_hino *ent;
11759 +       struct au_drinfo_rev *rev, **p;
11760 +
11761 +       AuDbg("bindex %d\n", bindex);
11762 +
11763 +       err = -ENOMEM;
11764 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11765 +       if (unlikely(!ent))
11766 +               goto out;
11767 +
11768 +       sb = src->d_sb;
11769 +       br = au_sbr(sb, bindex);
11770 +       dr = &br->br_dirren;
11771 +       h_dentry = au_h_dptr(src, bindex);
11772 +       h_inode = d_inode(h_dentry);
11773 +       ino = h_inode->i_ino;
11774 +       ent->dr_h_ino = ino;
11775 +       already = au_dr_hino_test_add(dr, ino, ent);
11776 +       AuDbg("b%d, hi%llu, already %d\n",
11777 +             bindex, (unsigned long long)ino, already);
11778 +
11779 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11780 +       AuTraceErr(err);
11781 +       if (!err) {
11782 +               p = _rev;
11783 +               rev = *p;
11784 +               rev->already = already;
11785 +               goto out; /* success */
11786 +       }
11787 +
11788 +       /* revert */
11789 +       if (!already)
11790 +               au_dr_hino_del(dr, ent);
11791 +       au_kfree_rcu(ent);
11792 +
11793 +out:
11794 +       AuTraceErr(err);
11795 +       return err;
11796 +}
11797 +
11798 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11799 +{
11800 +       struct au_drinfo_rev *rev;
11801 +       struct au_drinfo_rev_elm *elm;
11802 +       int nelm;
11803 +
11804 +       rev = _rev;
11805 +       elm = rev->elm;
11806 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11807 +               dput(elm->info_dentry);
11808 +               au_kfree_rcu(elm->info_last);
11809 +       }
11810 +       au_kfree_try_rcu(rev);
11811 +}
11812 +
11813 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11814 +{
11815 +       int err;
11816 +       struct au_drinfo_store work;
11817 +       struct au_drinfo_rev *rev = _rev;
11818 +       struct super_block *sb;
11819 +       struct au_branch *br;
11820 +       struct inode *h_inode;
11821 +       struct au_dr_br *dr;
11822 +       struct au_dr_hino *ent;
11823 +
11824 +       err = au_drinfo_store_work_init(&work, btgt);
11825 +       if (unlikely(err))
11826 +               goto out;
11827 +
11828 +       sb = src->d_sb;
11829 +       br = au_sbr(sb, btgt);
11830 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11831 +       work.h_ppath.mnt = au_br_mnt(br);
11832 +       au_drinfo_store_rev(rev, &work);
11833 +       au_drinfo_store_work_fin(&work);
11834 +       if (rev->already)
11835 +               goto out;
11836 +
11837 +       dr = &br->br_dirren;
11838 +       h_inode = d_inode(work.h_ppath.dentry);
11839 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11840 +       BUG_ON(!ent);
11841 +       au_dr_hino_del(dr, ent);
11842 +       au_kfree_rcu(ent);
11843 +
11844 +out:
11845 +       au_kfree_try_rcu(rev);
11846 +       if (unlikely(err))
11847 +               pr_err("failed to remove dirren info\n");
11848 +}
11849 +
11850 +/* ---------------------------------------------------------------------- */
11851 +
11852 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11853 +                                          char *whname, int whnamelen,
11854 +                                          struct dentry **info_dentry)
11855 +{
11856 +       struct au_drinfo *drinfo;
11857 +       struct file *f;
11858 +       struct inode *h_dir;
11859 +       struct path infopath;
11860 +       int unlocked;
11861 +
11862 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11863 +
11864 +       *info_dentry = NULL;
11865 +       drinfo = NULL;
11866 +       unlocked = 0;
11867 +       h_dir = d_inode(h_ppath->dentry);
11868 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11869 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11870 +       if (IS_ERR(infopath.dentry)) {
11871 +               drinfo = (void *)infopath.dentry;
11872 +               goto out;
11873 +       }
11874 +
11875 +       if (d_is_negative(infopath.dentry))
11876 +               goto out_dput; /* success */
11877 +
11878 +       infopath.mnt = h_ppath->mnt;
11879 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11880 +       inode_unlock_shared(h_dir);
11881 +       unlocked = 1;
11882 +       if (IS_ERR(f)) {
11883 +               drinfo = (void *)f;
11884 +               goto out_dput;
11885 +       }
11886 +
11887 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11888 +       if (IS_ERR_OR_NULL(drinfo))
11889 +               goto out_fput;
11890 +
11891 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11892 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11893 +
11894 +out_fput:
11895 +       fput(f);
11896 +out_dput:
11897 +       dput(infopath.dentry);
11898 +out:
11899 +       if (!unlocked)
11900 +               inode_unlock_shared(h_dir);
11901 +       AuTraceErrPtr(drinfo);
11902 +       return drinfo;
11903 +}
11904 +
11905 +struct au_drinfo_do_load_args {
11906 +       struct au_drinfo **drinfop;
11907 +       struct path *h_ppath;
11908 +       char *whname;
11909 +       int whnamelen;
11910 +       struct dentry **info_dentry;
11911 +};
11912 +
11913 +static void au_call_drinfo_do_load(void *args)
11914 +{
11915 +       struct au_drinfo_do_load_args *a = args;
11916 +
11917 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11918 +                                       a->info_dentry);
11919 +}
11920 +
11921 +struct au_drinfo_load {
11922 +       struct path h_ppath;
11923 +       struct qstr *qname;
11924 +       unsigned char no_sio;
11925 +
11926 +       aufs_bindex_t ninfo;
11927 +       struct au_drinfo **drinfo;
11928 +};
11929 +
11930 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11931 +                         struct au_branch *br)
11932 +{
11933 +       int err, wkq_err, whnamelen, e;
11934 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11935 +               = AUFS_WH_DR_INFO_PFX;
11936 +       struct au_drinfo *drinfo;
11937 +       struct qstr oldname;
11938 +       struct inode *h_dir, *delegated;
11939 +       struct dentry *info_dentry;
11940 +       struct path infopath;
11941 +
11942 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11943 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11944 +                                   sizeof(whname) - whnamelen);
11945 +       if (w->no_sio)
11946 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11947 +                                          &info_dentry);
11948 +       else {
11949 +               struct au_drinfo_do_load_args args = {
11950 +                       .drinfop        = &drinfo,
11951 +                       .h_ppath        = &w->h_ppath,
11952 +                       .whname         = whname,
11953 +                       .whnamelen      = whnamelen,
11954 +                       .info_dentry    = &info_dentry
11955 +               };
11956 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11957 +               if (unlikely(wkq_err))
11958 +                       drinfo = ERR_PTR(wkq_err);
11959 +       }
11960 +       err = PTR_ERR(drinfo);
11961 +       if (IS_ERR_OR_NULL(drinfo))
11962 +               goto out;
11963 +
11964 +       err = 0;
11965 +       oldname.len = drinfo->oldnamelen;
11966 +       oldname.name = drinfo->oldname;
11967 +       if (au_qstreq(w->qname, &oldname)) {
11968 +               /* the name is renamed back */
11969 +               au_kfree_rcu(drinfo);
11970 +               drinfo = NULL;
11971 +
11972 +               infopath.dentry = info_dentry;
11973 +               infopath.mnt = w->h_ppath.mnt;
11974 +               h_dir = d_inode(w->h_ppath.dentry);
11975 +               delegated = NULL;
11976 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11977 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11978 +               inode_unlock(h_dir);
11979 +               if (unlikely(e))
11980 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11981 +               if (unlikely(e == -EWOULDBLOCK))
11982 +                       iput(delegated);
11983 +       }
11984 +       au_kfree_rcu(w->drinfo[bindex]);
11985 +       w->drinfo[bindex] = drinfo;
11986 +       dput(info_dentry);
11987 +
11988 +out:
11989 +       AuTraceErr(err);
11990 +       return err;
11991 +}
11992 +
11993 +/* ---------------------------------------------------------------------- */
11994 +
11995 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
11996 +{
11997 +       struct au_drinfo **p = drinfo;
11998 +
11999 +       while (n-- > 0)
12000 +               au_kfree_rcu(*drinfo++);
12001 +       au_kfree_try_rcu(p);
12002 +}
12003 +
12004 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12005 +              aufs_bindex_t btgt)
12006 +{
12007 +       int err, ninfo;
12008 +       struct au_drinfo_load w;
12009 +       aufs_bindex_t bindex, bbot;
12010 +       struct au_branch *br;
12011 +       struct inode *h_dir;
12012 +       struct au_dr_hino *ent;
12013 +       struct super_block *sb;
12014 +
12015 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12016 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12017 +             AuLNPair(&lkup->whname), btgt);
12018 +
12019 +       sb = dentry->d_sb;
12020 +       bbot = au_sbbot(sb);
12021 +       w.ninfo = bbot + 1;
12022 +       if (!lkup->dirren.drinfo) {
12023 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12024 +                                             sizeof(*lkup->dirren.drinfo),
12025 +                                             GFP_NOFS);
12026 +               if (unlikely(!lkup->dirren.drinfo)) {
12027 +                       err = -ENOMEM;
12028 +                       goto out;
12029 +               }
12030 +               lkup->dirren.ninfo = w.ninfo;
12031 +       }
12032 +       w.drinfo = lkup->dirren.drinfo;
12033 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12034 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12035 +       AuDebugOn(!w.h_ppath.dentry);
12036 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12037 +       w.qname = &dentry->d_name;
12038 +
12039 +       ninfo = 0;
12040 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12041 +               br = au_sbr(sb, bindex);
12042 +               err = au_drinfo_load(&w, bindex, br);
12043 +               if (unlikely(err))
12044 +                       goto out_free;
12045 +               if (w.drinfo[bindex])
12046 +                       ninfo++;
12047 +       }
12048 +       if (!ninfo) {
12049 +               br = au_sbr(sb, btgt);
12050 +               h_dir = d_inode(w.h_ppath.dentry);
12051 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12052 +               AuDebugOn(!ent);
12053 +               au_dr_hino_del(&br->br_dirren, ent);
12054 +               au_kfree_rcu(ent);
12055 +       }
12056 +       goto out; /* success */
12057 +
12058 +out_free:
12059 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12060 +       lkup->dirren.ninfo = 0;
12061 +       lkup->dirren.drinfo = NULL;
12062 +out:
12063 +       AuTraceErr(err);
12064 +       return err;
12065 +}
12066 +
12067 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12068 +{
12069 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12070 +}
12071 +
12072 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12073 +{
12074 +       int err;
12075 +       struct au_drinfo *drinfo;
12076 +
12077 +       err = 0;
12078 +       if (!lkup->dirren.drinfo)
12079 +               goto out;
12080 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12081 +       drinfo = lkup->dirren.drinfo[btgt];
12082 +       if (!drinfo)
12083 +               goto out;
12084 +
12085 +       au_kfree_try_rcu(lkup->whname.name);
12086 +       lkup->whname.name = NULL;
12087 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12088 +       lkup->dirren.dr_name.name = drinfo->oldname;
12089 +       lkup->name = &lkup->dirren.dr_name;
12090 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12091 +       if (!err)
12092 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12093 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12094 +                     btgt);
12095 +
12096 +out:
12097 +       AuTraceErr(err);
12098 +       return err;
12099 +}
12100 +
12101 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12102 +                    ino_t h_ino)
12103 +{
12104 +       int match;
12105 +       struct au_drinfo *drinfo;
12106 +
12107 +       match = 1;
12108 +       if (!lkup->dirren.drinfo)
12109 +               goto out;
12110 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12111 +       drinfo = lkup->dirren.drinfo[bindex];
12112 +       if (!drinfo)
12113 +               goto out;
12114 +
12115 +       match = (drinfo->ino == h_ino);
12116 +       AuDbg("match %d\n", match);
12117 +
12118 +out:
12119 +       return match;
12120 +}
12121 +
12122 +/* ---------------------------------------------------------------------- */
12123 +
12124 +int au_dr_opt_set(struct super_block *sb)
12125 +{
12126 +       int err;
12127 +       aufs_bindex_t bindex, bbot;
12128 +       struct au_branch *br;
12129 +
12130 +       err = 0;
12131 +       bbot = au_sbbot(sb);
12132 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12133 +               br = au_sbr(sb, bindex);
12134 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12135 +       }
12136 +
12137 +       return err;
12138 +}
12139 +
12140 +int au_dr_opt_flush(struct super_block *sb)
12141 +{
12142 +       int err;
12143 +       aufs_bindex_t bindex, bbot;
12144 +       struct au_branch *br;
12145 +
12146 +       err = 0;
12147 +       bbot = au_sbbot(sb);
12148 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12149 +               br = au_sbr(sb, bindex);
12150 +               if (au_br_writable(br->br_perm))
12151 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12152 +       }
12153 +
12154 +       return err;
12155 +}
12156 +
12157 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12158 +{
12159 +       int err;
12160 +       aufs_bindex_t bindex, bbot;
12161 +       struct au_branch *br;
12162 +
12163 +       err = 0;
12164 +       if (!no_flush) {
12165 +               err = au_dr_opt_flush(sb);
12166 +               if (unlikely(err))
12167 +                       goto out;
12168 +       }
12169 +
12170 +       bbot = au_sbbot(sb);
12171 +       for (bindex = 0; bindex <= bbot; bindex++) {
12172 +               br = au_sbr(sb, bindex);
12173 +               au_dr_hino_free(&br->br_dirren);
12174 +       }
12175 +
12176 +out:
12177 +       return err;
12178 +}
12179 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12180 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12181 +++ linux/fs/aufs/dirren.h      2022-11-05 23:02:18.962555950 +0100
12182 @@ -0,0 +1,140 @@
12183 +/* SPDX-License-Identifier: GPL-2.0 */
12184 +/*
12185 + * Copyright (C) 2017-2022 Junjiro R. Okajima
12186 + *
12187 + * This program is free software; you can redistribute it and/or modify
12188 + * it under the terms of the GNU General Public License as published by
12189 + * the Free Software Foundation; either version 2 of the License, or
12190 + * (at your option) any later version.
12191 + *
12192 + * This program is distributed in the hope that it will be useful,
12193 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12194 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12195 + * GNU General Public License for more details.
12196 + *
12197 + * You should have received a copy of the GNU General Public License
12198 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12199 + */
12200 +
12201 +/*
12202 + * renamed dir info
12203 + */
12204 +
12205 +#ifndef __AUFS_DIRREN_H__
12206 +#define __AUFS_DIRREN_H__
12207 +
12208 +#ifdef __KERNEL__
12209 +
12210 +#include <linux/dcache.h>
12211 +#include <linux/statfs.h>
12212 +#include <linux/uuid.h>
12213 +#include "hbl.h"
12214 +
12215 +#define AuDirren_NHASH 100
12216 +
12217 +#ifdef CONFIG_AUFS_DIRREN
12218 +enum au_brid_type {
12219 +       AuBrid_Unset,
12220 +       AuBrid_UUID,
12221 +       AuBrid_FSID,
12222 +       AuBrid_DEV
12223 +};
12224 +
12225 +struct au_dr_brid {
12226 +       enum au_brid_type       type;
12227 +       union {
12228 +               uuid_t  uuid;   /* unimplemented yet */
12229 +               fsid_t  fsid;
12230 +               dev_t   dev;
12231 +       };
12232 +};
12233 +
12234 +/* 20 is the max digits length of ulong 64 */
12235 +/* brid-type "_" uuid "_" inum */
12236 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12237 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12238 +
12239 +struct au_dr_hino {
12240 +       struct hlist_bl_node    dr_hnode;
12241 +       ino_t                   dr_h_ino;
12242 +};
12243 +
12244 +struct au_dr_br {
12245 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12246 +       struct au_dr_brid       dr_brid;
12247 +};
12248 +
12249 +struct au_dr_lookup {
12250 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12251 +       struct qstr             dr_name; /* subset of dr_info */
12252 +       aufs_bindex_t           ninfo;
12253 +       struct au_drinfo        **drinfo;
12254 +};
12255 +#else
12256 +struct au_dr_hino;
12257 +/* empty */
12258 +struct au_dr_br { };
12259 +struct au_dr_lookup { };
12260 +#endif
12261 +
12262 +/* ---------------------------------------------------------------------- */
12263 +
12264 +struct au_branch;
12265 +struct au_do_lookup_args;
12266 +struct au_hinode;
12267 +#ifdef CONFIG_AUFS_DIRREN
12268 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12269 +                       struct au_dr_hino *add_ent);
12270 +void au_dr_hino_free(struct au_dr_br *dr);
12271 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12272 +                 const struct path *path);
12273 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12274 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12275 +                struct qstr *dst_name, void *_rev);
12276 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12277 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12278 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12279 +              aufs_bindex_t bindex);
12280 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12281 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12282 +                    ino_t h_ino);
12283 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12284 +int au_dr_opt_set(struct super_block *sb);
12285 +int au_dr_opt_flush(struct super_block *sb);
12286 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12287 +#else
12288 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12289 +          struct au_dr_hino *add_ent);
12290 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12291 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12292 +          const struct path *path);
12293 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12294 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12295 +          struct qstr *dst_name, void *_rev);
12296 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12297 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12298 +          void *rev);
12299 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12300 +          aufs_bindex_t bindex);
12301 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12302 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12303 +          aufs_bindex_t bindex, ino_t h_ino);
12304 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12305 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12306 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12307 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12308 +#endif
12309 +
12310 +/* ---------------------------------------------------------------------- */
12311 +
12312 +#ifdef CONFIG_AUFS_DIRREN
12313 +static inline int au_dr_ihash(ino_t h_ino)
12314 +{
12315 +       return h_ino % AuDirren_NHASH;
12316 +}
12317 +#else
12318 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12319 +#endif
12320 +
12321 +#endif /* __KERNEL__ */
12322 +#endif /* __AUFS_DIRREN_H__ */
12323 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12324 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12325 +++ linux/fs/aufs/dynop.c       2022-11-05 23:02:18.962555950 +0100
12326 @@ -0,0 +1,366 @@
12327 +// SPDX-License-Identifier: GPL-2.0
12328 +/*
12329 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12330 + *
12331 + * This program is free software; you can redistribute it and/or modify
12332 + * it under the terms of the GNU General Public License as published by
12333 + * the Free Software Foundation; either version 2 of the License, or
12334 + * (at your option) any later version.
12335 + *
12336 + * This program is distributed in the hope that it will be useful,
12337 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12338 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12339 + * GNU General Public License for more details.
12340 + *
12341 + * You should have received a copy of the GNU General Public License
12342 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12343 + */
12344 +
12345 +/*
12346 + * dynamically customizable operations for regular files
12347 + */
12348 +
12349 +#include "aufs.h"
12350 +
12351 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12352 +
12353 +/*
12354 + * How large will these lists be?
12355 + * Usually just a few elements, 20-30 at most for each, I guess.
12356 + */
12357 +static struct hlist_bl_head dynop[AuDyLast];
12358 +
12359 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12360 +                                    const void *h_op)
12361 +{
12362 +       struct au_dykey *key, *tmp;
12363 +       struct hlist_bl_node *pos;
12364 +
12365 +       key = NULL;
12366 +       hlist_bl_lock(hbl);
12367 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12368 +               if (tmp->dk_op.dy_hop == h_op) {
12369 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12370 +                               key = tmp;
12371 +                       break;
12372 +               }
12373 +       hlist_bl_unlock(hbl);
12374 +
12375 +       return key;
12376 +}
12377 +
12378 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12379 +{
12380 +       struct au_dykey **k, *found;
12381 +       const void *h_op = key->dk_op.dy_hop;
12382 +       int i;
12383 +
12384 +       found = NULL;
12385 +       k = br->br_dykey;
12386 +       for (i = 0; i < AuBrDynOp; i++)
12387 +               if (k[i]) {
12388 +                       if (k[i]->dk_op.dy_hop == h_op) {
12389 +                               found = k[i];
12390 +                               break;
12391 +                       }
12392 +               } else
12393 +                       break;
12394 +       if (!found) {
12395 +               spin_lock(&br->br_dykey_lock);
12396 +               for (; i < AuBrDynOp; i++)
12397 +                       if (k[i]) {
12398 +                               if (k[i]->dk_op.dy_hop == h_op) {
12399 +                                       found = k[i];
12400 +                                       break;
12401 +                               }
12402 +                       } else {
12403 +                               k[i] = key;
12404 +                               break;
12405 +                       }
12406 +               spin_unlock(&br->br_dykey_lock);
12407 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12408 +       }
12409 +
12410 +       return found;
12411 +}
12412 +
12413 +/* kref_get() if @key is already added */
12414 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12415 +{
12416 +       struct au_dykey *tmp, *found;
12417 +       struct hlist_bl_node *pos;
12418 +       const void *h_op = key->dk_op.dy_hop;
12419 +
12420 +       found = NULL;
12421 +       hlist_bl_lock(hbl);
12422 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12423 +               if (tmp->dk_op.dy_hop == h_op) {
12424 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12425 +                               found = tmp;
12426 +                       break;
12427 +               }
12428 +       if (!found)
12429 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12430 +       hlist_bl_unlock(hbl);
12431 +
12432 +       if (!found)
12433 +               DyPrSym(key);
12434 +       return found;
12435 +}
12436 +
12437 +static void dy_free_rcu(struct rcu_head *rcu)
12438 +{
12439 +       struct au_dykey *key;
12440 +
12441 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12442 +       DyPrSym(key);
12443 +       kfree(key);
12444 +}
12445 +
12446 +static void dy_free(struct kref *kref)
12447 +{
12448 +       struct au_dykey *key;
12449 +       struct hlist_bl_head *hbl;
12450 +
12451 +       key = container_of(kref, struct au_dykey, dk_kref);
12452 +       hbl = dynop + key->dk_op.dy_type;
12453 +       au_hbl_del(&key->dk_hnode, hbl);
12454 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12455 +}
12456 +
12457 +void au_dy_put(struct au_dykey *key)
12458 +{
12459 +       kref_put(&key->dk_kref, dy_free);
12460 +}
12461 +
12462 +/* ---------------------------------------------------------------------- */
12463 +
12464 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12465 +
12466 +#ifdef CONFIG_AUFS_DEBUG
12467 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12468 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12469 +#else
12470 +#define DyDbgDeclare(cnt)      do {} while (0)
12471 +#define DyDbgInc(cnt)          do {} while (0)
12472 +#endif
12473 +
12474 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12475 +       DyDbgInc(cnt);                                                  \
12476 +       if (h_op->func) {                                               \
12477 +               if (src.func)                                           \
12478 +                       dst.func = src.func;                            \
12479 +               else                                                    \
12480 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12481 +       }                                                               \
12482 +} while (0)
12483 +
12484 +#define DySetForce(func, dst, src) do {                \
12485 +       AuDebugOn(!src.func);                   \
12486 +       DyDbgInc(cnt);                          \
12487 +       dst.func = src.func;                    \
12488 +} while (0)
12489 +
12490 +#define DySetAop(func) \
12491 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12492 +#define DySetAopForce(func) \
12493 +       DySetForce(func, dyaop->da_op, aufs_aop)
12494 +
12495 +static void dy_aop(struct au_dykey *key, const void *h_op,
12496 +                  struct super_block *h_sb __maybe_unused)
12497 +{
12498 +       struct au_dyaop *dyaop = (void *)key;
12499 +       const struct address_space_operations *h_aop = h_op;
12500 +       DyDbgDeclare(cnt);
12501 +
12502 +       AuDbg("%s\n", au_sbtype(h_sb));
12503 +
12504 +       DySetAop(writepage);
12505 +       DySetAopForce(read_folio);      /* force */
12506 +       DySetAop(writepages);
12507 +       DySetAop(dirty_folio);
12508 +       DySetAop(invalidate_folio);
12509 +       DySetAop(readahead);
12510 +       DySetAop(write_begin);
12511 +       DySetAop(write_end);
12512 +       DySetAop(bmap);
12513 +       DySetAop(release_folio);
12514 +       DySetAop(free_folio);
12515 +       /* this one will be changed according to an aufs mount option */
12516 +       DySetAop(direct_IO);
12517 +       DySetAop(migrate_folio);
12518 +       DySetAop(launder_folio);
12519 +       DySetAop(is_partially_uptodate);
12520 +       DySetAop(is_dirty_writeback);
12521 +       DySetAop(error_remove_page);
12522 +       DySetAop(swap_activate);
12523 +       DySetAop(swap_deactivate);
12524 +       DySetAop(swap_rw);
12525 +
12526 +       DyDbgSize(cnt, *h_aop);
12527 +}
12528 +
12529 +/* ---------------------------------------------------------------------- */
12530 +
12531 +static void dy_bug(struct kref *kref)
12532 +{
12533 +       BUG();
12534 +}
12535 +
12536 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12537 +{
12538 +       struct au_dykey *key, *old;
12539 +       struct hlist_bl_head *hbl;
12540 +       struct op {
12541 +               unsigned int sz;
12542 +               void (*set)(struct au_dykey *key, const void *h_op,
12543 +                           struct super_block *h_sb __maybe_unused);
12544 +       };
12545 +       static const struct op a[] = {
12546 +               [AuDy_AOP] = {
12547 +                       .sz     = sizeof(struct au_dyaop),
12548 +                       .set    = dy_aop
12549 +               }
12550 +       };
12551 +       const struct op *p;
12552 +
12553 +       hbl = dynop + op->dy_type;
12554 +       key = dy_gfind_get(hbl, op->dy_hop);
12555 +       if (key)
12556 +               goto out_add; /* success */
12557 +
12558 +       p = a + op->dy_type;
12559 +       key = kzalloc(p->sz, GFP_NOFS);
12560 +       if (unlikely(!key)) {
12561 +               key = ERR_PTR(-ENOMEM);
12562 +               goto out;
12563 +       }
12564 +
12565 +       key->dk_op.dy_hop = op->dy_hop;
12566 +       kref_init(&key->dk_kref);
12567 +       p->set(key, op->dy_hop, au_br_sb(br));
12568 +       old = dy_gadd(hbl, key);
12569 +       if (old) {
12570 +               au_kfree_rcu(key);
12571 +               key = old;
12572 +       }
12573 +
12574 +out_add:
12575 +       old = dy_bradd(br, key);
12576 +       if (old)
12577 +               /* its ref-count should never be zero here */
12578 +               kref_put(&key->dk_kref, dy_bug);
12579 +out:
12580 +       return key;
12581 +}
12582 +
12583 +/* ---------------------------------------------------------------------- */
12584 +/*
12585 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12586 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12587 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12588 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12589 + * See the aufs manual in detail.
12590 + */
12591 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12592 +{
12593 +       if (!do_dx)
12594 +               dyaop->da_op.direct_IO = NULL;
12595 +       else
12596 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12597 +}
12598 +
12599 +static struct au_dyaop *dy_aget(struct au_branch *br,
12600 +                               const struct address_space_operations *h_aop,
12601 +                               int do_dx)
12602 +{
12603 +       struct au_dyaop *dyaop;
12604 +       struct au_dynop op;
12605 +
12606 +       op.dy_type = AuDy_AOP;
12607 +       op.dy_haop = h_aop;
12608 +       dyaop = (void *)dy_get(&op, br);
12609 +       if (IS_ERR(dyaop))
12610 +               goto out;
12611 +       dy_adx(dyaop, do_dx);
12612 +
12613 +out:
12614 +       return dyaop;
12615 +}
12616 +
12617 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12618 +               struct inode *h_inode)
12619 +{
12620 +       int err, do_dx;
12621 +       struct super_block *sb;
12622 +       struct au_branch *br;
12623 +       struct au_dyaop *dyaop;
12624 +
12625 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12626 +       IiMustWriteLock(inode);
12627 +
12628 +       sb = inode->i_sb;
12629 +       br = au_sbr(sb, bindex);
12630 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12631 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12632 +       err = PTR_ERR(dyaop);
12633 +       if (IS_ERR(dyaop))
12634 +               /* unnecessary to call dy_fput() */
12635 +               goto out;
12636 +
12637 +       err = 0;
12638 +       inode->i_mapping->a_ops = &dyaop->da_op;
12639 +
12640 +out:
12641 +       return err;
12642 +}
12643 +
12644 +/*
12645 + * Is it safe to replace a_ops during the inode/file is in operation?
12646 + * Yes, I hope so.
12647 + */
12648 +int au_dy_irefresh(struct inode *inode)
12649 +{
12650 +       int err;
12651 +       aufs_bindex_t btop;
12652 +       struct inode *h_inode;
12653 +
12654 +       err = 0;
12655 +       if (S_ISREG(inode->i_mode)) {
12656 +               btop = au_ibtop(inode);
12657 +               h_inode = au_h_iptr(inode, btop);
12658 +               err = au_dy_iaop(inode, btop, h_inode);
12659 +       }
12660 +       return err;
12661 +}
12662 +
12663 +void au_dy_arefresh(int do_dx)
12664 +{
12665 +       struct hlist_bl_head *hbl;
12666 +       struct hlist_bl_node *pos;
12667 +       struct au_dykey *key;
12668 +
12669 +       hbl = dynop + AuDy_AOP;
12670 +       hlist_bl_lock(hbl);
12671 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12672 +               dy_adx((void *)key, do_dx);
12673 +       hlist_bl_unlock(hbl);
12674 +}
12675 +
12676 +/* ---------------------------------------------------------------------- */
12677 +
12678 +void __init au_dy_init(void)
12679 +{
12680 +       int i;
12681 +
12682 +       for (i = 0; i < AuDyLast; i++)
12683 +               INIT_HLIST_BL_HEAD(dynop + i);
12684 +}
12685 +
12686 +void au_dy_fin(void)
12687 +{
12688 +       int i;
12689 +
12690 +       for (i = 0; i < AuDyLast; i++)
12691 +               WARN_ON(!hlist_bl_empty(dynop + i));
12692 +}
12693 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12694 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12695 +++ linux/fs/aufs/dynop.h       2022-11-05 23:02:18.962555950 +0100
12696 @@ -0,0 +1,77 @@
12697 +/* SPDX-License-Identifier: GPL-2.0 */
12698 +/*
12699 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12700 + *
12701 + * This program is free software; you can redistribute it and/or modify
12702 + * it under the terms of the GNU General Public License as published by
12703 + * the Free Software Foundation; either version 2 of the License, or
12704 + * (at your option) any later version.
12705 + *
12706 + * This program is distributed in the hope that it will be useful,
12707 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12708 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12709 + * GNU General Public License for more details.
12710 + *
12711 + * You should have received a copy of the GNU General Public License
12712 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12713 + */
12714 +
12715 +/*
12716 + * dynamically customizable operations (for regular files only)
12717 + */
12718 +
12719 +#ifndef __AUFS_DYNOP_H__
12720 +#define __AUFS_DYNOP_H__
12721 +
12722 +#ifdef __KERNEL__
12723 +
12724 +#include <linux/fs.h>
12725 +#include <linux/kref.h>
12726 +
12727 +enum {AuDy_AOP, AuDyLast};
12728 +
12729 +struct au_dynop {
12730 +       int                                             dy_type;
12731 +       union {
12732 +               const void                              *dy_hop;
12733 +               const struct address_space_operations   *dy_haop;
12734 +       };
12735 +};
12736 +
12737 +struct au_dykey {
12738 +       union {
12739 +               struct hlist_bl_node    dk_hnode;
12740 +               struct rcu_head         dk_rcu;
12741 +       };
12742 +       struct au_dynop         dk_op;
12743 +
12744 +       /*
12745 +        * during I am in the branch local array, kref is gotten. when the
12746 +        * branch is removed, kref is put.
12747 +        */
12748 +       struct kref             dk_kref;
12749 +};
12750 +
12751 +/* stop unioning since their sizes are very different from each other */
12752 +struct au_dyaop {
12753 +       struct au_dykey                 da_key;
12754 +       struct address_space_operations da_op; /* not const */
12755 +};
12756 +/* make sure that 'struct au_dykey *' can be any type */
12757 +static_assert(!offsetof(struct au_dyaop, da_key));
12758 +
12759 +/* ---------------------------------------------------------------------- */
12760 +
12761 +/* dynop.c */
12762 +struct au_branch;
12763 +void au_dy_put(struct au_dykey *key);
12764 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12765 +               struct inode *h_inode);
12766 +int au_dy_irefresh(struct inode *inode);
12767 +void au_dy_arefresh(int do_dio);
12768 +
12769 +void __init au_dy_init(void);
12770 +void au_dy_fin(void);
12771 +
12772 +#endif /* __KERNEL__ */
12773 +#endif /* __AUFS_DYNOP_H__ */
12774 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12775 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12776 +++ linux/fs/aufs/export.c      2022-12-17 09:21:34.796521861 +0100
12777 @@ -0,0 +1,830 @@
12778 +// SPDX-License-Identifier: GPL-2.0
12779 +/*
12780 + * Copyright (C) 2005-2022 Junjiro R. Okajima
12781 + *
12782 + * This program is free software; you can redistribute it and/or modify
12783 + * it under the terms of the GNU General Public License as published by
12784 + * the Free Software Foundation; either version 2 of the License, or
12785 + * (at your option) any later version.
12786 + *
12787 + * This program is distributed in the hope that it will be useful,
12788 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12789 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12790 + * GNU General Public License for more details.
12791 + *
12792 + * You should have received a copy of the GNU General Public License
12793 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12794 + */
12795 +
12796 +/*
12797 + * export via nfs
12798 + */
12799 +
12800 +#include <linux/exportfs.h>
12801 +#include <linux/fs_struct.h>
12802 +#include <linux/nsproxy.h>
12803 +#include <linux/random.h>
12804 +#include <linux/writeback.h>
12805 +#include "aufs.h"
12806 +
12807 +union conv {
12808 +#ifdef CONFIG_AUFS_INO_T_64
12809 +       __u32 a[2];
12810 +#else
12811 +       __u32 a[1];
12812 +#endif
12813 +       ino_t ino;
12814 +};
12815 +
12816 +static ino_t decode_ino(__u32 *a)
12817 +{
12818 +       union conv u;
12819 +
12820 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12821 +       u.a[0] = a[0];
12822 +#ifdef CONFIG_AUFS_INO_T_64
12823 +       u.a[1] = a[1];
12824 +#endif
12825 +       return u.ino;
12826 +}
12827 +
12828 +static void encode_ino(__u32 *a, ino_t ino)
12829 +{
12830 +       union conv u;
12831 +
12832 +       u.ino = ino;
12833 +       a[0] = u.a[0];
12834 +#ifdef CONFIG_AUFS_INO_T_64
12835 +       a[1] = u.a[1];
12836 +#endif
12837 +}
12838 +
12839 +/* NFS file handle */
12840 +enum {
12841 +       Fh_br_id,
12842 +       Fh_sigen,
12843 +#ifdef CONFIG_AUFS_INO_T_64
12844 +       /* support 64bit inode number */
12845 +       Fh_ino1,
12846 +       Fh_ino2,
12847 +       Fh_dir_ino1,
12848 +       Fh_dir_ino2,
12849 +#else
12850 +       Fh_ino1,
12851 +       Fh_dir_ino1,
12852 +#endif
12853 +       Fh_igen,
12854 +       Fh_h_type,
12855 +       Fh_tail,
12856 +
12857 +       Fh_ino = Fh_ino1,
12858 +       Fh_dir_ino = Fh_dir_ino1
12859 +};
12860 +
12861 +static int au_test_anon(struct dentry *dentry)
12862 +{
12863 +       /* note: read d_flags without d_lock */
12864 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12865 +}
12866 +
12867 +int au_test_nfsd(void)
12868 +{
12869 +       int ret;
12870 +       struct task_struct *tsk = current;
12871 +       char comm[sizeof(tsk->comm)];
12872 +
12873 +       ret = 0;
12874 +       if (tsk->flags & PF_KTHREAD) {
12875 +               get_task_comm(comm, tsk);
12876 +               ret = !strcmp(comm, "nfsd");
12877 +       }
12878 +
12879 +       return ret;
12880 +}
12881 +
12882 +/* ---------------------------------------------------------------------- */
12883 +/* inode generation external table */
12884 +
12885 +void au_xigen_inc(struct inode *inode)
12886 +{
12887 +       loff_t pos;
12888 +       ssize_t sz;
12889 +       __u32 igen;
12890 +       struct super_block *sb;
12891 +       struct au_sbinfo *sbinfo;
12892 +
12893 +       sb = inode->i_sb;
12894 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12895 +
12896 +       sbinfo = au_sbi(sb);
12897 +       pos = inode->i_ino;
12898 +       pos *= sizeof(igen);
12899 +       igen = inode->i_generation + 1;
12900 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12901 +       if (sz == sizeof(igen))
12902 +               return; /* success */
12903 +
12904 +       if (unlikely(sz >= 0))
12905 +               AuIOErr("xigen error (%zd)\n", sz);
12906 +}
12907 +
12908 +int au_xigen_new(struct inode *inode)
12909 +{
12910 +       int err;
12911 +       loff_t pos;
12912 +       ssize_t sz;
12913 +       struct super_block *sb;
12914 +       struct au_sbinfo *sbinfo;
12915 +       struct file *file;
12916 +
12917 +       err = 0;
12918 +       /* todo: dirty, at mount time */
12919 +       if (inode->i_ino == AUFS_ROOT_INO)
12920 +               goto out;
12921 +       sb = inode->i_sb;
12922 +       SiMustAnyLock(sb);
12923 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12924 +               goto out;
12925 +
12926 +       err = -EFBIG;
12927 +       pos = inode->i_ino;
12928 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12929 +               AuIOErr1("too large i%lld\n", pos);
12930 +               goto out;
12931 +       }
12932 +       pos *= sizeof(inode->i_generation);
12933 +
12934 +       err = 0;
12935 +       sbinfo = au_sbi(sb);
12936 +       file = sbinfo->si_xigen;
12937 +       BUG_ON(!file);
12938 +
12939 +       if (vfsub_f_size_read(file)
12940 +           < pos + sizeof(inode->i_generation)) {
12941 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12942 +               sz = xino_fwrite(file, &inode->i_generation,
12943 +                                sizeof(inode->i_generation), &pos);
12944 +       } else
12945 +               sz = xino_fread(file, &inode->i_generation,
12946 +                               sizeof(inode->i_generation), &pos);
12947 +       if (sz == sizeof(inode->i_generation))
12948 +               goto out; /* success */
12949 +
12950 +       err = sz;
12951 +       if (unlikely(sz >= 0)) {
12952 +               err = -EIO;
12953 +               AuIOErr("xigen error (%zd)\n", sz);
12954 +       }
12955 +
12956 +out:
12957 +       return err;
12958 +}
12959 +
12960 +int au_xigen_set(struct super_block *sb, struct path *path)
12961 +{
12962 +       int err;
12963 +       struct au_sbinfo *sbinfo;
12964 +       struct file *file;
12965 +
12966 +       SiMustWriteLock(sb);
12967 +
12968 +       sbinfo = au_sbi(sb);
12969 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12970 +       err = PTR_ERR(file);
12971 +       if (IS_ERR(file))
12972 +               goto out;
12973 +       err = 0;
12974 +       if (sbinfo->si_xigen)
12975 +               fput(sbinfo->si_xigen);
12976 +       sbinfo->si_xigen = file;
12977 +
12978 +out:
12979 +       AuTraceErr(err);
12980 +       return err;
12981 +}
12982 +
12983 +void au_xigen_clr(struct super_block *sb)
12984 +{
12985 +       struct au_sbinfo *sbinfo;
12986 +
12987 +       SiMustWriteLock(sb);
12988 +
12989 +       sbinfo = au_sbi(sb);
12990 +       if (sbinfo->si_xigen) {
12991 +               fput(sbinfo->si_xigen);
12992 +               sbinfo->si_xigen = NULL;
12993 +       }
12994 +}
12995 +
12996 +/* ---------------------------------------------------------------------- */
12997 +
12998 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
12999 +                                   ino_t dir_ino)
13000 +{
13001 +       struct dentry *dentry, *d;
13002 +       struct inode *inode;
13003 +       unsigned int sigen;
13004 +
13005 +       dentry = NULL;
13006 +       inode = ilookup(sb, ino);
13007 +       if (!inode)
13008 +               goto out;
13009 +
13010 +       dentry = ERR_PTR(-ESTALE);
13011 +       sigen = au_sigen(sb);
13012 +       if (unlikely(au_is_bad_inode(inode)
13013 +                    || IS_DEADDIR(inode)
13014 +                    || sigen != au_iigen(inode, NULL)))
13015 +               goto out_iput;
13016 +
13017 +       dentry = NULL;
13018 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13019 +               dentry = d_find_alias(inode);
13020 +       else {
13021 +               spin_lock(&inode->i_lock);
13022 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13023 +                       spin_lock(&d->d_lock);
13024 +                       if (!au_test_anon(d)
13025 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13026 +                               dentry = dget_dlock(d);
13027 +                               spin_unlock(&d->d_lock);
13028 +                               break;
13029 +                       }
13030 +                       spin_unlock(&d->d_lock);
13031 +               }
13032 +               spin_unlock(&inode->i_lock);
13033 +       }
13034 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13035 +               /* need to refresh */
13036 +               dput(dentry);
13037 +               dentry = NULL;
13038 +       }
13039 +
13040 +out_iput:
13041 +       iput(inode);
13042 +out:
13043 +       AuTraceErrPtr(dentry);
13044 +       return dentry;
13045 +}
13046 +
13047 +/* ---------------------------------------------------------------------- */
13048 +
13049 +/* todo: dirty? */
13050 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13051 +
13052 +struct au_compare_mnt_args {
13053 +       /* input */
13054 +       struct super_block *sb;
13055 +
13056 +       /* output */
13057 +       struct vfsmount *mnt;
13058 +};
13059 +
13060 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13061 +{
13062 +       struct au_compare_mnt_args *a = arg;
13063 +
13064 +       if (mnt->mnt_sb != a->sb)
13065 +               return 0;
13066 +       a->mnt = mntget(mnt);
13067 +       return 1;
13068 +}
13069 +
13070 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13071 +{
13072 +       int err;
13073 +       struct path root;
13074 +       struct au_compare_mnt_args args = {
13075 +               .sb = sb
13076 +       };
13077 +
13078 +       get_fs_root(current->fs, &root);
13079 +       rcu_read_lock();
13080 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13081 +       rcu_read_unlock();
13082 +       path_put(&root);
13083 +       AuDebugOn(!err);
13084 +       AuDebugOn(!args.mnt);
13085 +       return args.mnt;
13086 +}
13087 +
13088 +struct au_nfsd_si_lock {
13089 +       unsigned int sigen;
13090 +       aufs_bindex_t bindex, br_id;
13091 +       unsigned char force_lock;
13092 +};
13093 +
13094 +static int si_nfsd_read_lock(struct super_block *sb,
13095 +                            struct au_nfsd_si_lock *nsi_lock)
13096 +{
13097 +       int err;
13098 +       aufs_bindex_t bindex;
13099 +
13100 +       si_read_lock(sb, AuLock_FLUSH);
13101 +
13102 +       /* branch id may be wrapped around */
13103 +       err = 0;
13104 +       bindex = au_br_index(sb, nsi_lock->br_id);
13105 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13106 +               goto out; /* success */
13107 +
13108 +       err = -ESTALE;
13109 +       bindex = -1;
13110 +       if (!nsi_lock->force_lock)
13111 +               si_read_unlock(sb);
13112 +
13113 +out:
13114 +       nsi_lock->bindex = bindex;
13115 +       return err;
13116 +}
13117 +
13118 +struct find_name_by_ino {
13119 +       struct dir_context ctx;
13120 +       int called, found;
13121 +       ino_t ino;
13122 +       char *name;
13123 +       int namelen;
13124 +};
13125 +
13126 +static bool
13127 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13128 +                loff_t offset, u64 ino, unsigned int d_type)
13129 +{
13130 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13131 +                                                 ctx);
13132 +
13133 +       a->called++;
13134 +       if (a->ino != ino)
13135 +               return true;
13136 +
13137 +       memcpy(a->name, name, namelen);
13138 +       a->namelen = namelen;
13139 +       a->found = 1;
13140 +       return false;
13141 +}
13142 +
13143 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13144 +                                    struct au_nfsd_si_lock *nsi_lock)
13145 +{
13146 +       struct dentry *dentry, *parent;
13147 +       struct file *file;
13148 +       struct inode *dir;
13149 +       struct find_name_by_ino arg = {
13150 +               .ctx = {
13151 +                       .actor = find_name_by_ino
13152 +               }
13153 +       };
13154 +       int err;
13155 +
13156 +       parent = path->dentry;
13157 +       if (nsi_lock)
13158 +               si_read_unlock(parent->d_sb);
13159 +       file = vfsub_dentry_open(path, au_dir_roflags);
13160 +       dentry = (void *)file;
13161 +       if (IS_ERR(file))
13162 +               goto out;
13163 +
13164 +       dentry = ERR_PTR(-ENOMEM);
13165 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13166 +       if (unlikely(!arg.name))
13167 +               goto out_file;
13168 +       arg.ino = ino;
13169 +       arg.found = 0;
13170 +       do {
13171 +               arg.called = 0;
13172 +               /* smp_mb(); */
13173 +               err = vfsub_iterate_dir(file, &arg.ctx);
13174 +       } while (!err && !arg.found && arg.called);
13175 +       dentry = ERR_PTR(err);
13176 +       if (unlikely(err))
13177 +               goto out_name;
13178 +       /* instead of ENOENT */
13179 +       dentry = ERR_PTR(-ESTALE);
13180 +       if (!arg.found)
13181 +               goto out_name;
13182 +
13183 +       /* do not call vfsub_lkup_one() */
13184 +       dir = d_inode(parent);
13185 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13186 +       AuTraceErrPtr(dentry);
13187 +       if (IS_ERR(dentry))
13188 +               goto out_name;
13189 +       AuDebugOn(au_test_anon(dentry));
13190 +       if (unlikely(d_really_is_negative(dentry))) {
13191 +               dput(dentry);
13192 +               dentry = ERR_PTR(-ENOENT);
13193 +       }
13194 +
13195 +out_name:
13196 +       free_page((unsigned long)arg.name);
13197 +out_file:
13198 +       fput(file);
13199 +out:
13200 +       if (unlikely(nsi_lock
13201 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13202 +               if (!IS_ERR(dentry)) {
13203 +                       dput(dentry);
13204 +                       dentry = ERR_PTR(-ESTALE);
13205 +               }
13206 +       AuTraceErrPtr(dentry);
13207 +       return dentry;
13208 +}
13209 +
13210 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13211 +                                       ino_t dir_ino,
13212 +                                       struct au_nfsd_si_lock *nsi_lock)
13213 +{
13214 +       struct dentry *dentry;
13215 +       struct path path;
13216 +
13217 +       if (dir_ino != AUFS_ROOT_INO) {
13218 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13219 +               dentry = path.dentry;
13220 +               if (!path.dentry || IS_ERR(path.dentry))
13221 +                       goto out;
13222 +               AuDebugOn(au_test_anon(path.dentry));
13223 +       } else
13224 +               path.dentry = dget(sb->s_root);
13225 +
13226 +       path.mnt = au_mnt_get(sb);
13227 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13228 +       path_put(&path);
13229 +
13230 +out:
13231 +       AuTraceErrPtr(dentry);
13232 +       return dentry;
13233 +}
13234 +
13235 +/* ---------------------------------------------------------------------- */
13236 +
13237 +static int h_acceptable(void *expv, struct dentry *dentry)
13238 +{
13239 +       return 1;
13240 +}
13241 +
13242 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13243 +                          char *buf, int len, struct super_block *sb)
13244 +{
13245 +       char *p;
13246 +       int n;
13247 +       struct path path;
13248 +
13249 +       p = d_path(h_rootpath, buf, len);
13250 +       if (IS_ERR(p))
13251 +               goto out;
13252 +       n = strlen(p);
13253 +
13254 +       path.mnt = h_rootpath->mnt;
13255 +       path.dentry = h_parent;
13256 +       p = d_path(&path, buf, len);
13257 +       if (IS_ERR(p))
13258 +               goto out;
13259 +       if (n != 1)
13260 +               p += n;
13261 +
13262 +       path.mnt = au_mnt_get(sb);
13263 +       path.dentry = sb->s_root;
13264 +       p = d_path(&path, buf, len - strlen(p));
13265 +       mntput(path.mnt);
13266 +       if (IS_ERR(p))
13267 +               goto out;
13268 +       if (n != 1)
13269 +               p[strlen(p)] = '/';
13270 +
13271 +out:
13272 +       AuTraceErrPtr(p);
13273 +       return p;
13274 +}
13275 +
13276 +static
13277 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13278 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13279 +{
13280 +       struct dentry *dentry, *h_parent, *root;
13281 +       struct super_block *h_sb;
13282 +       char *pathname, *p;
13283 +       struct vfsmount *h_mnt;
13284 +       struct au_branch *br;
13285 +       int err;
13286 +       struct path path;
13287 +
13288 +       br = au_sbr(sb, nsi_lock->bindex);
13289 +       h_mnt = au_br_mnt(br);
13290 +       h_sb = h_mnt->mnt_sb;
13291 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13292 +       lockdep_off();
13293 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13294 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13295 +                                     h_acceptable, /*context*/NULL);
13296 +       lockdep_on();
13297 +       dentry = h_parent;
13298 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13299 +               AuWarn1("%s decode_fh failed, %ld\n",
13300 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13301 +               goto out;
13302 +       }
13303 +       dentry = NULL;
13304 +       if (unlikely(au_test_anon(h_parent))) {
13305 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13306 +                       au_sbtype(h_sb));
13307 +               goto out_h_parent;
13308 +       }
13309 +
13310 +       dentry = ERR_PTR(-ENOMEM);
13311 +       pathname = (void *)__get_free_page(GFP_NOFS);
13312 +       if (unlikely(!pathname))
13313 +               goto out_h_parent;
13314 +
13315 +       root = sb->s_root;
13316 +       path.mnt = h_mnt;
13317 +       di_read_lock_parent(root, !AuLock_IR);
13318 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13319 +       di_read_unlock(root, !AuLock_IR);
13320 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13321 +       dentry = (void *)p;
13322 +       if (IS_ERR(p))
13323 +               goto out_pathname;
13324 +
13325 +       si_read_unlock(sb);
13326 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13327 +       dentry = ERR_PTR(err);
13328 +       if (unlikely(err))
13329 +               goto out_relock;
13330 +
13331 +       dentry = ERR_PTR(-ENOENT);
13332 +       AuDebugOn(au_test_anon(path.dentry));
13333 +       if (unlikely(d_really_is_negative(path.dentry)))
13334 +               goto out_path;
13335 +
13336 +       if (ino != d_inode(path.dentry)->i_ino)
13337 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13338 +       else
13339 +               dentry = dget(path.dentry);
13340 +
13341 +out_path:
13342 +       path_put(&path);
13343 +out_relock:
13344 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13345 +               if (!IS_ERR(dentry)) {
13346 +                       dput(dentry);
13347 +                       dentry = ERR_PTR(-ESTALE);
13348 +               }
13349 +out_pathname:
13350 +       free_page((unsigned long)pathname);
13351 +out_h_parent:
13352 +       dput(h_parent);
13353 +out:
13354 +       AuTraceErrPtr(dentry);
13355 +       return dentry;
13356 +}
13357 +
13358 +/* ---------------------------------------------------------------------- */
13359 +
13360 +static struct dentry *
13361 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13362 +                 int fh_type)
13363 +{
13364 +       struct dentry *dentry;
13365 +       __u32 *fh = fid->raw;
13366 +       struct au_branch *br;
13367 +       ino_t ino, dir_ino;
13368 +       struct au_nfsd_si_lock nsi_lock = {
13369 +               .force_lock     = 0
13370 +       };
13371 +
13372 +       dentry = ERR_PTR(-ESTALE);
13373 +       /* it should never happen, but the file handle is unreliable */
13374 +       if (unlikely(fh_len < Fh_tail))
13375 +               goto out;
13376 +       nsi_lock.sigen = fh[Fh_sigen];
13377 +       nsi_lock.br_id = fh[Fh_br_id];
13378 +
13379 +       /* branch id may be wrapped around */
13380 +       br = NULL;
13381 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13382 +               goto out;
13383 +       nsi_lock.force_lock = 1;
13384 +
13385 +       /* is this inode still cached? */
13386 +       ino = decode_ino(fh + Fh_ino);
13387 +       /* it should never happen */
13388 +       if (unlikely(ino == AUFS_ROOT_INO))
13389 +               goto out_unlock;
13390 +
13391 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13392 +       dentry = decode_by_ino(sb, ino, dir_ino);
13393 +       if (IS_ERR(dentry))
13394 +               goto out_unlock;
13395 +       if (dentry)
13396 +               goto accept;
13397 +
13398 +       /* is the parent dir cached? */
13399 +       br = au_sbr(sb, nsi_lock.bindex);
13400 +       au_lcnt_inc(&br->br_nfiles);
13401 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13402 +       if (IS_ERR(dentry))
13403 +               goto out_unlock;
13404 +       if (dentry)
13405 +               goto accept;
13406 +
13407 +       /* lookup path */
13408 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13409 +       if (IS_ERR(dentry))
13410 +               goto out_unlock;
13411 +       if (unlikely(!dentry))
13412 +               /* todo?: make it ESTALE */
13413 +               goto out_unlock;
13414 +
13415 +accept:
13416 +       if (!au_digen_test(dentry, au_sigen(sb))
13417 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13418 +               goto out_unlock; /* success */
13419 +
13420 +       dput(dentry);
13421 +       dentry = ERR_PTR(-ESTALE);
13422 +out_unlock:
13423 +       if (br)
13424 +               au_lcnt_dec(&br->br_nfiles);
13425 +       si_read_unlock(sb);
13426 +out:
13427 +       AuTraceErrPtr(dentry);
13428 +       return dentry;
13429 +}
13430 +
13431 +#if 0 /* reserved for future use */
13432 +/* support subtreecheck option */
13433 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13434 +                                       int fh_len, int fh_type)
13435 +{
13436 +       struct dentry *parent;
13437 +       __u32 *fh = fid->raw;
13438 +       ino_t dir_ino;
13439 +
13440 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13441 +       parent = decode_by_ino(sb, dir_ino, 0);
13442 +       if (IS_ERR(parent))
13443 +               goto out;
13444 +       if (!parent)
13445 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13446 +                                       dir_ino, fh, fh_len);
13447 +
13448 +out:
13449 +       AuTraceErrPtr(parent);
13450 +       return parent;
13451 +}
13452 +#endif
13453 +
13454 +/* ---------------------------------------------------------------------- */
13455 +
13456 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13457 +                         struct inode *dir)
13458 +{
13459 +       int err;
13460 +       aufs_bindex_t bindex;
13461 +       struct super_block *sb, *h_sb;
13462 +       struct dentry *dentry, *parent, *h_parent;
13463 +       struct inode *h_dir;
13464 +       struct au_branch *br;
13465 +
13466 +       err = -ENOSPC;
13467 +       if (unlikely(*max_len <= Fh_tail)) {
13468 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13469 +               goto out;
13470 +       }
13471 +
13472 +       err = FILEID_ROOT;
13473 +       if (inode->i_ino == AUFS_ROOT_INO) {
13474 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13475 +               goto out;
13476 +       }
13477 +
13478 +       h_parent = NULL;
13479 +       sb = inode->i_sb;
13480 +       err = si_read_lock(sb, AuLock_FLUSH);
13481 +       if (unlikely(err))
13482 +               goto out;
13483 +
13484 +#ifdef CONFIG_AUFS_DEBUG
13485 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13486 +               AuWarn1("NFS-exporting requires xino\n");
13487 +#endif
13488 +       err = -EIO;
13489 +       parent = NULL;
13490 +       ii_read_lock_child(inode);
13491 +       bindex = au_ibtop(inode);
13492 +       if (!dir) {
13493 +               dentry = d_find_any_alias(inode);
13494 +               if (unlikely(!dentry))
13495 +                       goto out_unlock;
13496 +               AuDebugOn(au_test_anon(dentry));
13497 +               parent = dget_parent(dentry);
13498 +               dput(dentry);
13499 +               if (unlikely(!parent))
13500 +                       goto out_unlock;
13501 +               if (d_really_is_positive(parent))
13502 +                       dir = d_inode(parent);
13503 +       }
13504 +
13505 +       ii_read_lock_parent(dir);
13506 +       h_dir = au_h_iptr(dir, bindex);
13507 +       ii_read_unlock(dir);
13508 +       if (unlikely(!h_dir))
13509 +               goto out_parent;
13510 +       h_parent = d_find_any_alias(h_dir);
13511 +       if (unlikely(!h_parent))
13512 +               goto out_hparent;
13513 +
13514 +       err = -EPERM;
13515 +       br = au_sbr(sb, bindex);
13516 +       h_sb = au_br_sb(br);
13517 +       if (unlikely(!h_sb->s_export_op)) {
13518 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13519 +               goto out_hparent;
13520 +       }
13521 +
13522 +       fh[Fh_br_id] = br->br_id;
13523 +       fh[Fh_sigen] = au_sigen(sb);
13524 +       encode_ino(fh + Fh_ino, inode->i_ino);
13525 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13526 +       fh[Fh_igen] = inode->i_generation;
13527 +
13528 +       *max_len -= Fh_tail;
13529 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13530 +                                          max_len,
13531 +                                          /*connectable or subtreecheck*/0);
13532 +       err = fh[Fh_h_type];
13533 +       *max_len += Fh_tail;
13534 +       /* todo: macros? */
13535 +       if (err != FILEID_INVALID)
13536 +               err = 99;
13537 +       else
13538 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13539 +
13540 +out_hparent:
13541 +       dput(h_parent);
13542 +out_parent:
13543 +       dput(parent);
13544 +out_unlock:
13545 +       ii_read_unlock(inode);
13546 +       si_read_unlock(sb);
13547 +out:
13548 +       if (unlikely(err < 0))
13549 +               err = FILEID_INVALID;
13550 +       return err;
13551 +}
13552 +
13553 +/* ---------------------------------------------------------------------- */
13554 +
13555 +static int aufs_commit_metadata(struct inode *inode)
13556 +{
13557 +       int err;
13558 +       aufs_bindex_t bindex;
13559 +       struct super_block *sb;
13560 +       struct inode *h_inode;
13561 +       int (*f)(struct inode *inode);
13562 +
13563 +       sb = inode->i_sb;
13564 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13565 +       ii_write_lock_child(inode);
13566 +       bindex = au_ibtop(inode);
13567 +       AuDebugOn(bindex < 0);
13568 +       h_inode = au_h_iptr(inode, bindex);
13569 +
13570 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13571 +       if (f)
13572 +               err = f(h_inode);
13573 +       else
13574 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13575 +
13576 +       au_cpup_attr_timesizes(inode);
13577 +       ii_write_unlock(inode);
13578 +       si_read_unlock(sb);
13579 +       return err;
13580 +}
13581 +
13582 +/* ---------------------------------------------------------------------- */
13583 +
13584 +static struct export_operations aufs_export_op = {
13585 +       .fh_to_dentry           = aufs_fh_to_dentry,
13586 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13587 +       .encode_fh              = aufs_encode_fh,
13588 +       .commit_metadata        = aufs_commit_metadata
13589 +};
13590 +
13591 +void au_export_init(struct super_block *sb)
13592 +{
13593 +       struct au_sbinfo *sbinfo;
13594 +       __u32 u;
13595 +
13596 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13597 +                        && IS_MODULE(CONFIG_EXPORTFS),
13598 +                        AUFS_NAME ": unsupported configuration "
13599 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13600 +
13601 +       sb->s_export_op = &aufs_export_op;
13602 +       sbinfo = au_sbi(sb);
13603 +       sbinfo->si_xigen = NULL;
13604 +       get_random_bytes(&u, sizeof(u));
13605 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13606 +       atomic_set(&sbinfo->si_xigen_next, u);
13607 +}
13608 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13609 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13610 +++ linux/fs/aufs/fhsm.c        2022-11-05 23:02:18.962555950 +0100
13611 @@ -0,0 +1,426 @@
13612 +// SPDX-License-Identifier: GPL-2.0
13613 +/*
13614 + * Copyright (C) 2011-2022 Junjiro R. Okajima
13615 + *
13616 + * This program is free software; you can redistribute it and/or modify
13617 + * it under the terms of the GNU General Public License as published by
13618 + * the Free Software Foundation; either version 2 of the License, or
13619 + * (at your option) any later version.
13620 + *
13621 + * This program is distributed in the hope that it will be useful,
13622 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13623 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13624 + * GNU General Public License for more details.
13625 + *
13626 + * You should have received a copy of the GNU General Public License
13627 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13628 + */
13629 +
13630 +/*
13631 + * File-based Hierarchy Storage Management
13632 + */
13633 +
13634 +#include <linux/anon_inodes.h>
13635 +#include <linux/poll.h>
13636 +#include <linux/seq_file.h>
13637 +#include <linux/statfs.h>
13638 +#include "aufs.h"
13639 +
13640 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13641 +{
13642 +       struct au_sbinfo *sbinfo;
13643 +       struct au_fhsm *fhsm;
13644 +
13645 +       SiMustAnyLock(sb);
13646 +
13647 +       sbinfo = au_sbi(sb);
13648 +       fhsm = &sbinfo->si_fhsm;
13649 +       AuDebugOn(!fhsm);
13650 +       return fhsm->fhsm_bottom;
13651 +}
13652 +
13653 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13654 +{
13655 +       struct au_sbinfo *sbinfo;
13656 +       struct au_fhsm *fhsm;
13657 +
13658 +       SiMustWriteLock(sb);
13659 +
13660 +       sbinfo = au_sbi(sb);
13661 +       fhsm = &sbinfo->si_fhsm;
13662 +       AuDebugOn(!fhsm);
13663 +       fhsm->fhsm_bottom = bindex;
13664 +}
13665 +
13666 +/* ---------------------------------------------------------------------- */
13667 +
13668 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13669 +{
13670 +       struct au_br_fhsm *bf;
13671 +
13672 +       bf = br->br_fhsm;
13673 +       MtxMustLock(&bf->bf_lock);
13674 +
13675 +       return !bf->bf_readable
13676 +               || time_after(jiffies,
13677 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13678 +}
13679 +
13680 +/* ---------------------------------------------------------------------- */
13681 +
13682 +static void au_fhsm_notify(struct super_block *sb, int val)
13683 +{
13684 +       struct au_sbinfo *sbinfo;
13685 +       struct au_fhsm *fhsm;
13686 +
13687 +       SiMustAnyLock(sb);
13688 +
13689 +       sbinfo = au_sbi(sb);
13690 +       fhsm = &sbinfo->si_fhsm;
13691 +       if (au_fhsm_pid(fhsm)
13692 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13693 +               atomic_set(&fhsm->fhsm_readable, val);
13694 +               if (val)
13695 +                       wake_up(&fhsm->fhsm_wqh);
13696 +       }
13697 +}
13698 +
13699 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13700 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13701 +{
13702 +       int err;
13703 +       struct au_branch *br;
13704 +       struct au_br_fhsm *bf;
13705 +
13706 +       br = au_sbr(sb, bindex);
13707 +       AuDebugOn(au_br_rdonly(br));
13708 +       bf = br->br_fhsm;
13709 +       AuDebugOn(!bf);
13710 +
13711 +       if (do_lock)
13712 +               mutex_lock(&bf->bf_lock);
13713 +       else
13714 +               MtxMustLock(&bf->bf_lock);
13715 +
13716 +       /* sb->s_root for NFS is unreliable */
13717 +       err = au_br_stfs(br, &bf->bf_stfs);
13718 +       if (unlikely(err)) {
13719 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13720 +               goto out;
13721 +       }
13722 +
13723 +       bf->bf_jiffy = jiffies;
13724 +       bf->bf_readable = 1;
13725 +       if (do_notify)
13726 +               au_fhsm_notify(sb, /*val*/1);
13727 +       if (rstfs)
13728 +               *rstfs = bf->bf_stfs;
13729 +
13730 +out:
13731 +       if (do_lock)
13732 +               mutex_unlock(&bf->bf_lock);
13733 +       au_fhsm_notify(sb, /*val*/1);
13734 +
13735 +       return err;
13736 +}
13737 +
13738 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13739 +{
13740 +       int err;
13741 +       struct au_sbinfo *sbinfo;
13742 +       struct au_fhsm *fhsm;
13743 +       struct au_branch *br;
13744 +       struct au_br_fhsm *bf;
13745 +
13746 +       AuDbg("b%d, force %d\n", bindex, force);
13747 +       SiMustAnyLock(sb);
13748 +
13749 +       sbinfo = au_sbi(sb);
13750 +       fhsm = &sbinfo->si_fhsm;
13751 +       if (!au_ftest_si(sbinfo, FHSM)
13752 +           || fhsm->fhsm_bottom == bindex)
13753 +               return;
13754 +
13755 +       br = au_sbr(sb, bindex);
13756 +       bf = br->br_fhsm;
13757 +       AuDebugOn(!bf);
13758 +       mutex_lock(&bf->bf_lock);
13759 +       if (force
13760 +           || au_fhsm_pid(fhsm)
13761 +           || au_fhsm_test_jiffy(sbinfo, br))
13762 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13763 +                                 /*do_notify*/1);
13764 +       mutex_unlock(&bf->bf_lock);
13765 +}
13766 +
13767 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13768 +{
13769 +       aufs_bindex_t bindex, bbot;
13770 +       struct au_branch *br;
13771 +
13772 +       /* exclude the bottom */
13773 +       bbot = au_fhsm_bottom(sb);
13774 +       for (bindex = 0; bindex < bbot; bindex++) {
13775 +               br = au_sbr(sb, bindex);
13776 +               if (au_br_fhsm(br->br_perm))
13777 +                       au_fhsm_wrote(sb, bindex, force);
13778 +       }
13779 +}
13780 +
13781 +/* ---------------------------------------------------------------------- */
13782 +
13783 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13784 +{
13785 +       __poll_t mask;
13786 +       struct au_sbinfo *sbinfo;
13787 +       struct au_fhsm *fhsm;
13788 +
13789 +       mask = 0;
13790 +       sbinfo = file->private_data;
13791 +       fhsm = &sbinfo->si_fhsm;
13792 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13793 +       if (atomic_read(&fhsm->fhsm_readable))
13794 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13795 +
13796 +       if (!mask)
13797 +               AuDbg("mask 0x%x\n", mask);
13798 +       return mask;
13799 +}
13800 +
13801 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13802 +                             struct aufs_stfs *stfs, __s16 brid)
13803 +{
13804 +       int err;
13805 +
13806 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13807 +       if (!err)
13808 +               err = __put_user(brid, &stbr->brid);
13809 +       if (unlikely(err))
13810 +               err = -EFAULT;
13811 +
13812 +       return err;
13813 +}
13814 +
13815 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13816 +                              struct aufs_stbr __user *stbr, size_t count)
13817 +{
13818 +       ssize_t err;
13819 +       int nstbr;
13820 +       aufs_bindex_t bindex, bbot;
13821 +       struct au_branch *br;
13822 +       struct au_br_fhsm *bf;
13823 +
13824 +       /* except the bottom branch */
13825 +       err = 0;
13826 +       nstbr = 0;
13827 +       bbot = au_fhsm_bottom(sb);
13828 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13829 +               br = au_sbr(sb, bindex);
13830 +               if (!au_br_fhsm(br->br_perm))
13831 +                       continue;
13832 +
13833 +               bf = br->br_fhsm;
13834 +               mutex_lock(&bf->bf_lock);
13835 +               if (bf->bf_readable) {
13836 +                       err = -EFAULT;
13837 +                       if (count >= sizeof(*stbr))
13838 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13839 +                                                         br->br_id);
13840 +                       if (!err) {
13841 +                               bf->bf_readable = 0;
13842 +                               count -= sizeof(*stbr);
13843 +                               nstbr++;
13844 +                       }
13845 +               }
13846 +               mutex_unlock(&bf->bf_lock);
13847 +       }
13848 +       if (!err)
13849 +               err = sizeof(*stbr) * nstbr;
13850 +
13851 +       return err;
13852 +}
13853 +
13854 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13855 +                          loff_t *pos)
13856 +{
13857 +       ssize_t err;
13858 +       int readable;
13859 +       aufs_bindex_t nfhsm, bindex, bbot;
13860 +       struct au_sbinfo *sbinfo;
13861 +       struct au_fhsm *fhsm;
13862 +       struct au_branch *br;
13863 +       struct super_block *sb;
13864 +
13865 +       err = 0;
13866 +       sbinfo = file->private_data;
13867 +       fhsm = &sbinfo->si_fhsm;
13868 +need_data:
13869 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13870 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13871 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13872 +                       err = -EAGAIN;
13873 +               else
13874 +                       err = wait_event_interruptible_locked_irq
13875 +                               (fhsm->fhsm_wqh,
13876 +                                atomic_read(&fhsm->fhsm_readable));
13877 +       }
13878 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13879 +       if (unlikely(err))
13880 +               goto out;
13881 +
13882 +       /* sb may already be dead */
13883 +       au_rw_read_lock(&sbinfo->si_rwsem);
13884 +       readable = atomic_read(&fhsm->fhsm_readable);
13885 +       if (readable > 0) {
13886 +               sb = sbinfo->si_sb;
13887 +               AuDebugOn(!sb);
13888 +               /* exclude the bottom branch */
13889 +               nfhsm = 0;
13890 +               bbot = au_fhsm_bottom(sb);
13891 +               for (bindex = 0; bindex < bbot; bindex++) {
13892 +                       br = au_sbr(sb, bindex);
13893 +                       if (au_br_fhsm(br->br_perm))
13894 +                               nfhsm++;
13895 +               }
13896 +               err = -EMSGSIZE;
13897 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13898 +                       atomic_set(&fhsm->fhsm_readable, 0);
13899 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13900 +                                            count);
13901 +               }
13902 +       }
13903 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13904 +       if (!readable)
13905 +               goto need_data;
13906 +
13907 +out:
13908 +       return err;
13909 +}
13910 +
13911 +static int au_fhsm_release(struct inode *inode, struct file *file)
13912 +{
13913 +       struct au_sbinfo *sbinfo;
13914 +       struct au_fhsm *fhsm;
13915 +
13916 +       /* sb may already be dead */
13917 +       sbinfo = file->private_data;
13918 +       fhsm = &sbinfo->si_fhsm;
13919 +       spin_lock(&fhsm->fhsm_spin);
13920 +       fhsm->fhsm_pid = 0;
13921 +       spin_unlock(&fhsm->fhsm_spin);
13922 +       kobject_put(&sbinfo->si_kobj);
13923 +
13924 +       return 0;
13925 +}
13926 +
13927 +static const struct file_operations au_fhsm_fops = {
13928 +       .owner          = THIS_MODULE,
13929 +       .llseek         = noop_llseek,
13930 +       .read           = au_fhsm_read,
13931 +       .poll           = au_fhsm_poll,
13932 +       .release        = au_fhsm_release
13933 +};
13934 +
13935 +int au_fhsm_fd(struct super_block *sb, int oflags)
13936 +{
13937 +       int err, fd;
13938 +       struct au_sbinfo *sbinfo;
13939 +       struct au_fhsm *fhsm;
13940 +
13941 +       err = -EPERM;
13942 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13943 +               goto out;
13944 +
13945 +       err = -EINVAL;
13946 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13947 +               goto out;
13948 +
13949 +       err = 0;
13950 +       sbinfo = au_sbi(sb);
13951 +       fhsm = &sbinfo->si_fhsm;
13952 +       spin_lock(&fhsm->fhsm_spin);
13953 +       if (!fhsm->fhsm_pid)
13954 +               fhsm->fhsm_pid = current->pid;
13955 +       else
13956 +               err = -EBUSY;
13957 +       spin_unlock(&fhsm->fhsm_spin);
13958 +       if (unlikely(err))
13959 +               goto out;
13960 +
13961 +       oflags |= O_RDONLY;
13962 +       /* oflags |= FMODE_NONOTIFY; */
13963 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13964 +       err = fd;
13965 +       if (unlikely(fd < 0))
13966 +               goto out_pid;
13967 +
13968 +       /* succeed regardless 'fhsm' status */
13969 +       kobject_get(&sbinfo->si_kobj);
13970 +       si_noflush_read_lock(sb);
13971 +       if (au_ftest_si(sbinfo, FHSM))
13972 +               au_fhsm_wrote_all(sb, /*force*/0);
13973 +       si_read_unlock(sb);
13974 +       goto out; /* success */
13975 +
13976 +out_pid:
13977 +       spin_lock(&fhsm->fhsm_spin);
13978 +       fhsm->fhsm_pid = 0;
13979 +       spin_unlock(&fhsm->fhsm_spin);
13980 +out:
13981 +       AuTraceErr(err);
13982 +       return err;
13983 +}
13984 +
13985 +/* ---------------------------------------------------------------------- */
13986 +
13987 +int au_fhsm_br_alloc(struct au_branch *br)
13988 +{
13989 +       int err;
13990 +
13991 +       err = 0;
13992 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
13993 +       if (br->br_fhsm)
13994 +               au_br_fhsm_init(br->br_fhsm);
13995 +       else
13996 +               err = -ENOMEM;
13997 +
13998 +       return err;
13999 +}
14000 +
14001 +/* ---------------------------------------------------------------------- */
14002 +
14003 +void au_fhsm_fin(struct super_block *sb)
14004 +{
14005 +       au_fhsm_notify(sb, /*val*/-1);
14006 +}
14007 +
14008 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14009 +{
14010 +       struct au_fhsm *fhsm;
14011 +
14012 +       fhsm = &sbinfo->si_fhsm;
14013 +       spin_lock_init(&fhsm->fhsm_spin);
14014 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14015 +       atomic_set(&fhsm->fhsm_readable, 0);
14016 +       fhsm->fhsm_expire
14017 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14018 +       fhsm->fhsm_bottom = -1;
14019 +}
14020 +
14021 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14022 +{
14023 +       sbinfo->si_fhsm.fhsm_expire
14024 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14025 +}
14026 +
14027 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14028 +{
14029 +       unsigned int u;
14030 +
14031 +       if (!au_ftest_si(sbinfo, FHSM))
14032 +               return;
14033 +
14034 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14035 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14036 +               seq_printf(seq, ",fhsm_sec=%u", u);
14037 +}
14038 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14039 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14040 +++ linux/fs/aufs/file.c        2023-09-04 13:39:55.763295607 +0200
14041 @@ -0,0 +1,865 @@
14042 +// SPDX-License-Identifier: GPL-2.0
14043 +/*
14044 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14045 + *
14046 + * This program is free software; you can redistribute it and/or modify
14047 + * it under the terms of the GNU General Public License as published by
14048 + * the Free Software Foundation; either version 2 of the License, or
14049 + * (at your option) any later version.
14050 + *
14051 + * This program is distributed in the hope that it will be useful,
14052 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14053 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14054 + * GNU General Public License for more details.
14055 + *
14056 + * You should have received a copy of the GNU General Public License
14057 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14058 + */
14059 +
14060 +/*
14061 + * handling file/dir, and address_space operation
14062 + */
14063 +
14064 +#ifdef CONFIG_AUFS_DEBUG
14065 +#include <linux/migrate.h>
14066 +#endif
14067 +#include <linux/pagemap.h>
14068 +#include "aufs.h"
14069 +
14070 +/* drop flags for writing */
14071 +unsigned int au_file_roflags(unsigned int flags)
14072 +{
14073 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14074 +       flags |= O_RDONLY | O_NOATIME;
14075 +       return flags;
14076 +}
14077 +
14078 +/* common functions to regular file and dir */
14079 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14080 +                      struct file *file, int force_wr)
14081 +{
14082 +       struct file *h_file;
14083 +       struct dentry *h_dentry;
14084 +       struct inode *h_inode;
14085 +       struct super_block *sb;
14086 +       struct au_branch *br;
14087 +       struct path h_path;
14088 +       int err;
14089 +
14090 +       /* a race condition can happen between open and unlink/rmdir */
14091 +       h_file = ERR_PTR(-ENOENT);
14092 +       h_dentry = au_h_dptr(dentry, bindex);
14093 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14094 +               goto out;
14095 +       h_inode = d_inode(h_dentry);
14096 +       spin_lock(&h_dentry->d_lock);
14097 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14098 +               /* || !d_inode(dentry)->i_nlink */
14099 +               ;
14100 +       spin_unlock(&h_dentry->d_lock);
14101 +       if (unlikely(err))
14102 +               goto out;
14103 +
14104 +       sb = dentry->d_sb;
14105 +       br = au_sbr(sb, bindex);
14106 +       err = au_br_test_oflag(flags, br);
14107 +       h_file = ERR_PTR(err);
14108 +       if (unlikely(err))
14109 +               goto out;
14110 +
14111 +       /* drop flags for writing */
14112 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14113 +               if (force_wr && !(flags & O_WRONLY))
14114 +                       force_wr = 0;
14115 +               flags = au_file_roflags(flags);
14116 +               if (force_wr) {
14117 +                       h_file = ERR_PTR(-EROFS);
14118 +                       flags = au_file_roflags(flags);
14119 +                       if (unlikely(vfsub_native_ro(h_inode)
14120 +                                    || IS_APPEND(h_inode)))
14121 +                               goto out;
14122 +                       flags &= ~O_ACCMODE;
14123 +                       flags |= O_WRONLY;
14124 +               }
14125 +       }
14126 +       flags &= ~O_CREAT;
14127 +       au_lcnt_inc(&br->br_nfiles);
14128 +       h_path.dentry = h_dentry;
14129 +       h_path.mnt = au_br_mnt(br);
14130 +       /*
14131 +        * vfs::backing_file_open() looks promising since it can get rid of
14132 +        * mm::vm_prfile approach from my mind.
14133 +        * but I keep current approach for a while.
14134 +        */
14135 +       h_file = vfsub_dentry_open(&h_path, flags);
14136 +       if (IS_ERR(h_file))
14137 +               goto out_br;
14138 +
14139 +       if (flags & __FMODE_EXEC) {
14140 +               err = deny_write_access(h_file);
14141 +               if (unlikely(err)) {
14142 +                       fput(h_file);
14143 +                       h_file = ERR_PTR(err);
14144 +                       goto out_br;
14145 +               }
14146 +       }
14147 +       fsnotify_open(h_file);
14148 +       goto out; /* success */
14149 +
14150 +out_br:
14151 +       au_lcnt_dec(&br->br_nfiles);
14152 +out:
14153 +       return h_file;
14154 +}
14155 +
14156 +static int au_cmoo(struct dentry *dentry)
14157 +{
14158 +       int err, cmoo, matched;
14159 +       unsigned int udba;
14160 +       struct path h_path;
14161 +       struct au_pin pin;
14162 +       struct au_cp_generic cpg = {
14163 +               .dentry = dentry,
14164 +               .bdst   = -1,
14165 +               .bsrc   = -1,
14166 +               .len    = -1,
14167 +               .pin    = &pin,
14168 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14169 +       };
14170 +       struct inode *delegated;
14171 +       struct super_block *sb;
14172 +       struct au_sbinfo *sbinfo;
14173 +       struct au_fhsm *fhsm;
14174 +       pid_t pid;
14175 +       struct au_branch *br;
14176 +       struct dentry *parent;
14177 +       struct au_hinode *hdir;
14178 +
14179 +       DiMustWriteLock(dentry);
14180 +       IiMustWriteLock(d_inode(dentry));
14181 +
14182 +       err = 0;
14183 +       if (IS_ROOT(dentry))
14184 +               goto out;
14185 +       cpg.bsrc = au_dbtop(dentry);
14186 +       if (!cpg.bsrc)
14187 +               goto out;
14188 +
14189 +       sb = dentry->d_sb;
14190 +       sbinfo = au_sbi(sb);
14191 +       fhsm = &sbinfo->si_fhsm;
14192 +       pid = au_fhsm_pid(fhsm);
14193 +       rcu_read_lock();
14194 +       matched = (pid
14195 +                  && (current->pid == pid
14196 +                      || rcu_dereference(current->real_parent)->pid == pid));
14197 +       rcu_read_unlock();
14198 +       if (matched)
14199 +               goto out;
14200 +
14201 +       br = au_sbr(sb, cpg.bsrc);
14202 +       cmoo = au_br_cmoo(br->br_perm);
14203 +       if (!cmoo)
14204 +               goto out;
14205 +       if (!d_is_reg(dentry))
14206 +               cmoo &= AuBrAttr_COO_ALL;
14207 +       if (!cmoo)
14208 +               goto out;
14209 +
14210 +       parent = dget_parent(dentry);
14211 +       di_write_lock_parent(parent);
14212 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14213 +       cpg.bdst = err;
14214 +       if (unlikely(err < 0)) {
14215 +               err = 0;        /* there is no upper writable branch */
14216 +               goto out_dgrade;
14217 +       }
14218 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14219 +
14220 +       /* do not respect the coo attrib for the target branch */
14221 +       err = au_cpup_dirs(dentry, cpg.bdst);
14222 +       if (unlikely(err))
14223 +               goto out_dgrade;
14224 +
14225 +       di_downgrade_lock(parent, AuLock_IR);
14226 +       udba = au_opt_udba(sb);
14227 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14228 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14229 +       if (unlikely(err))
14230 +               goto out_parent;
14231 +
14232 +       err = au_sio_cpup_simple(&cpg);
14233 +       au_unpin(&pin);
14234 +       if (unlikely(err))
14235 +               goto out_parent;
14236 +       if (!(cmoo & AuBrWAttr_MOO))
14237 +               goto out_parent; /* success */
14238 +
14239 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14240 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14241 +       if (unlikely(err))
14242 +               goto out_parent;
14243 +
14244 +       h_path.mnt = au_br_mnt(br);
14245 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14246 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14247 +       delegated = NULL;
14248 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14249 +       au_unpin(&pin);
14250 +       /* todo: keep h_dentry or not? */
14251 +       if (unlikely(err == -EWOULDBLOCK)) {
14252 +               pr_warn("cannot retry for NFSv4 delegation"
14253 +                       " for an internal unlink\n");
14254 +               iput(delegated);
14255 +       }
14256 +       if (unlikely(err)) {
14257 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14258 +                      dentry, err);
14259 +               err = 0;
14260 +       }
14261 +       goto out_parent; /* success */
14262 +
14263 +out_dgrade:
14264 +       di_downgrade_lock(parent, AuLock_IR);
14265 +out_parent:
14266 +       di_read_unlock(parent, AuLock_IR);
14267 +       dput(parent);
14268 +out:
14269 +       AuTraceErr(err);
14270 +       return err;
14271 +}
14272 +
14273 +int au_do_open(struct file *file, struct au_do_open_args *args)
14274 +{
14275 +       int err, aopen = args->aopen;
14276 +       struct dentry *dentry;
14277 +       struct au_finfo *finfo;
14278 +
14279 +       if (!aopen)
14280 +               err = au_finfo_init(file, args->fidir);
14281 +       else {
14282 +               lockdep_off();
14283 +               err = au_finfo_init(file, args->fidir);
14284 +               lockdep_on();
14285 +       }
14286 +       if (unlikely(err))
14287 +               goto out;
14288 +
14289 +       dentry = file->f_path.dentry;
14290 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14291 +       di_write_lock_child(dentry);
14292 +       err = au_cmoo(dentry);
14293 +       if (!err) {
14294 +               if (!aopen) {
14295 +                       err = args->open(file, vfsub_file_flags(file),
14296 +                                        au_di(dentry)->di_htmpfile);
14297 +                       di_write_unlock(dentry);
14298 +               } else {
14299 +                       di_downgrade_lock(dentry, AuLock_IR);
14300 +                       lockdep_off();
14301 +                       err = args->open(file, vfsub_file_flags(file),
14302 +                                        args->h_file);
14303 +                       lockdep_on();
14304 +                       di_read_unlock(dentry, AuLock_IR);
14305 +               }
14306 +       }
14307 +
14308 +       finfo = au_fi(file);
14309 +       if (!err) {
14310 +               finfo->fi_file = file;
14311 +               au_hbl_add(&finfo->fi_hlist,
14312 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14313 +       }
14314 +       if (!aopen)
14315 +               fi_write_unlock(file);
14316 +       else {
14317 +               lockdep_off();
14318 +               fi_write_unlock(file);
14319 +               lockdep_on();
14320 +       }
14321 +       if (unlikely(err)) {
14322 +               finfo->fi_hdir = NULL;
14323 +               au_finfo_fin(file);
14324 +       }
14325 +
14326 +out:
14327 +       AuTraceErr(err);
14328 +       return err;
14329 +}
14330 +
14331 +int au_reopen_nondir(struct file *file)
14332 +{
14333 +       int err;
14334 +       aufs_bindex_t btop;
14335 +       struct dentry *dentry;
14336 +       struct au_branch *br;
14337 +       struct file *h_file, *h_file_tmp;
14338 +
14339 +       dentry = file->f_path.dentry;
14340 +       btop = au_dbtop(dentry);
14341 +       br = au_sbr(dentry->d_sb, btop);
14342 +       h_file_tmp = NULL;
14343 +       if (au_fbtop(file) == btop) {
14344 +               h_file = au_hf_top(file);
14345 +               if (file->f_mode == h_file->f_mode)
14346 +                       return 0; /* success */
14347 +               h_file_tmp = h_file;
14348 +               get_file(h_file_tmp);
14349 +               au_lcnt_inc(&br->br_nfiles);
14350 +               au_set_h_fptr(file, btop, NULL);
14351 +       }
14352 +       AuDebugOn(au_fi(file)->fi_hdir);
14353 +       /*
14354 +        * it can happen
14355 +        * file exists on both of rw and ro
14356 +        * open --> dbtop and fbtop are both 0
14357 +        * prepend a branch as rw, "rw" become ro
14358 +        * remove rw/file
14359 +        * delete the top branch, "rw" becomes rw again
14360 +        *      --> dbtop is 1, fbtop is still 0
14361 +        * write --> fbtop is 0 but dbtop is 1
14362 +        */
14363 +       /* AuDebugOn(au_fbtop(file) < btop); */
14364 +
14365 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14366 +                          file, /*force_wr*/0);
14367 +       err = PTR_ERR(h_file);
14368 +       if (IS_ERR(h_file)) {
14369 +               if (h_file_tmp) {
14370 +                       /* revert */
14371 +                       au_set_h_fptr(file, btop, h_file_tmp);
14372 +                       h_file_tmp = NULL;
14373 +               }
14374 +               goto out; /* todo: close all? */
14375 +       }
14376 +
14377 +       err = 0;
14378 +       au_set_fbtop(file, btop);
14379 +       au_set_h_fptr(file, btop, h_file);
14380 +       au_update_figen(file);
14381 +       /* todo: necessary? */
14382 +       /* file->f_ra = h_file->f_ra; */
14383 +
14384 +out:
14385 +       if (h_file_tmp) {
14386 +               fput(h_file_tmp);
14387 +               au_lcnt_dec(&br->br_nfiles);
14388 +       }
14389 +       return err;
14390 +}
14391 +
14392 +/* ---------------------------------------------------------------------- */
14393 +
14394 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14395 +                       struct dentry *hi_wh)
14396 +{
14397 +       int err;
14398 +       aufs_bindex_t btop;
14399 +       struct au_dinfo *dinfo;
14400 +       struct dentry *h_dentry;
14401 +       struct au_hdentry *hdp;
14402 +
14403 +       dinfo = au_di(file->f_path.dentry);
14404 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14405 +
14406 +       btop = dinfo->di_btop;
14407 +       dinfo->di_btop = btgt;
14408 +       hdp = au_hdentry(dinfo, btgt);
14409 +       h_dentry = hdp->hd_dentry;
14410 +       hdp->hd_dentry = hi_wh;
14411 +       err = au_reopen_nondir(file);
14412 +       hdp->hd_dentry = h_dentry;
14413 +       dinfo->di_btop = btop;
14414 +
14415 +       return err;
14416 +}
14417 +
14418 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14419 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14420 +{
14421 +       int err;
14422 +       struct inode *inode, *h_inode;
14423 +       struct dentry *h_dentry, *hi_wh;
14424 +       struct au_cp_generic cpg = {
14425 +               .dentry = file->f_path.dentry,
14426 +               .bdst   = bcpup,
14427 +               .bsrc   = -1,
14428 +               .len    = len,
14429 +               .pin    = pin
14430 +       };
14431 +
14432 +       au_update_dbtop(cpg.dentry);
14433 +       inode = d_inode(cpg.dentry);
14434 +       h_inode = NULL;
14435 +       if (au_dbtop(cpg.dentry) <= bcpup
14436 +           && au_dbbot(cpg.dentry) >= bcpup) {
14437 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14438 +               if (h_dentry && d_is_positive(h_dentry))
14439 +                       h_inode = d_inode(h_dentry);
14440 +       }
14441 +       hi_wh = au_hi_wh(inode, bcpup);
14442 +       if (!hi_wh && !h_inode)
14443 +               err = au_sio_cpup_wh(&cpg, file);
14444 +       else
14445 +               /* already copied-up after unlink */
14446 +               err = au_reopen_wh(file, bcpup, hi_wh);
14447 +
14448 +       if (!err
14449 +           && (inode->i_nlink > 1
14450 +               || (inode->i_state & I_LINKABLE))
14451 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14452 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14453 +
14454 +       return err;
14455 +}
14456 +
14457 +/*
14458 + * prepare the @file for writing.
14459 + */
14460 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14461 +{
14462 +       int err;
14463 +       aufs_bindex_t dbtop;
14464 +       struct dentry *parent;
14465 +       struct inode *inode;
14466 +       struct super_block *sb;
14467 +       struct file *h_file;
14468 +       struct au_cp_generic cpg = {
14469 +               .dentry = file->f_path.dentry,
14470 +               .bdst   = -1,
14471 +               .bsrc   = -1,
14472 +               .len    = len,
14473 +               .pin    = pin,
14474 +               .flags  = AuCpup_DTIME
14475 +       };
14476 +
14477 +       sb = cpg.dentry->d_sb;
14478 +       inode = d_inode(cpg.dentry);
14479 +       cpg.bsrc = au_fbtop(file);
14480 +       err = au_test_ro(sb, cpg.bsrc, inode);
14481 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14482 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14483 +                            /*flags*/0);
14484 +               goto out;
14485 +       }
14486 +
14487 +       /* need to cpup or reopen */
14488 +       parent = dget_parent(cpg.dentry);
14489 +       di_write_lock_parent(parent);
14490 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14491 +       cpg.bdst = err;
14492 +       if (unlikely(err < 0))
14493 +               goto out_dgrade;
14494 +       err = 0;
14495 +
14496 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14497 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14498 +               if (unlikely(err))
14499 +                       goto out_dgrade;
14500 +       }
14501 +
14502 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14503 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14504 +       if (unlikely(err))
14505 +               goto out_dgrade;
14506 +
14507 +       dbtop = au_dbtop(cpg.dentry);
14508 +       if (dbtop <= cpg.bdst)
14509 +               cpg.bsrc = cpg.bdst;
14510 +
14511 +       if (dbtop <= cpg.bdst           /* just reopen */
14512 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14513 +               ) {
14514 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14515 +               if (IS_ERR(h_file))
14516 +                       err = PTR_ERR(h_file);
14517 +               else {
14518 +                       di_downgrade_lock(parent, AuLock_IR);
14519 +                       if (dbtop > cpg.bdst)
14520 +                               err = au_sio_cpup_simple(&cpg);
14521 +                       if (!err)
14522 +                               err = au_reopen_nondir(file);
14523 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14524 +               }
14525 +       } else {                        /* copyup as wh and reopen */
14526 +               /*
14527 +                * since writable hfsplus branch is not supported,
14528 +                * h_open_pre/post() are unnecessary.
14529 +                */
14530 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14531 +               di_downgrade_lock(parent, AuLock_IR);
14532 +       }
14533 +
14534 +       if (!err) {
14535 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14536 +               goto out_dput; /* success */
14537 +       }
14538 +       au_unpin(pin);
14539 +       goto out_unlock;
14540 +
14541 +out_dgrade:
14542 +       di_downgrade_lock(parent, AuLock_IR);
14543 +out_unlock:
14544 +       di_read_unlock(parent, AuLock_IR);
14545 +out_dput:
14546 +       dput(parent);
14547 +out:
14548 +       return err;
14549 +}
14550 +
14551 +/* ---------------------------------------------------------------------- */
14552 +
14553 +int au_do_flush(struct file *file, fl_owner_t id,
14554 +               int (*flush)(struct file *file, fl_owner_t id))
14555 +{
14556 +       int err;
14557 +       struct super_block *sb;
14558 +       struct inode *inode;
14559 +
14560 +       inode = file_inode(file);
14561 +       sb = inode->i_sb;
14562 +       si_noflush_read_lock(sb);
14563 +       fi_read_lock(file);
14564 +       ii_read_lock_child(inode);
14565 +
14566 +       err = flush(file, id);
14567 +       au_cpup_attr_timesizes(inode);
14568 +
14569 +       ii_read_unlock(inode);
14570 +       fi_read_unlock(file);
14571 +       si_read_unlock(sb);
14572 +       return err;
14573 +}
14574 +
14575 +/* ---------------------------------------------------------------------- */
14576 +
14577 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14578 +{
14579 +       int err;
14580 +       struct au_pin pin;
14581 +       struct au_finfo *finfo;
14582 +       struct dentry *parent, *hi_wh;
14583 +       struct inode *inode;
14584 +       struct super_block *sb;
14585 +       struct au_cp_generic cpg = {
14586 +               .dentry = file->f_path.dentry,
14587 +               .bdst   = -1,
14588 +               .bsrc   = -1,
14589 +               .len    = -1,
14590 +               .pin    = &pin,
14591 +               .flags  = AuCpup_DTIME
14592 +       };
14593 +
14594 +       FiMustWriteLock(file);
14595 +
14596 +       err = 0;
14597 +       finfo = au_fi(file);
14598 +       sb = cpg.dentry->d_sb;
14599 +       inode = d_inode(cpg.dentry);
14600 +       cpg.bdst = au_ibtop(inode);
14601 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14602 +               goto out;
14603 +
14604 +       parent = dget_parent(cpg.dentry);
14605 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14606 +               di_read_lock_parent(parent, !AuLock_IR);
14607 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14608 +               cpg.bdst = err;
14609 +               di_read_unlock(parent, !AuLock_IR);
14610 +               if (unlikely(err < 0))
14611 +                       goto out_parent;
14612 +               err = 0;
14613 +       }
14614 +
14615 +       di_read_lock_parent(parent, AuLock_IR);
14616 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14617 +       if (!S_ISDIR(inode->i_mode)
14618 +           && au_opt_test(au_mntflags(sb), PLINK)
14619 +           && au_plink_test(inode)
14620 +           && !d_unhashed(cpg.dentry)
14621 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14622 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14623 +               if (unlikely(err))
14624 +                       goto out_unlock;
14625 +
14626 +               /* always superio. */
14627 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14628 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14629 +               if (!err) {
14630 +                       err = au_sio_cpup_simple(&cpg);
14631 +                       au_unpin(&pin);
14632 +               }
14633 +       } else if (hi_wh) {
14634 +               /* already copied-up after unlink */
14635 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14636 +               *need_reopen = 0;
14637 +       }
14638 +
14639 +out_unlock:
14640 +       di_read_unlock(parent, AuLock_IR);
14641 +out_parent:
14642 +       dput(parent);
14643 +out:
14644 +       return err;
14645 +}
14646 +
14647 +static void au_do_refresh_dir(struct file *file)
14648 +{
14649 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14650 +       struct au_hfile *p, tmp, *q;
14651 +       struct au_finfo *finfo;
14652 +       struct super_block *sb;
14653 +       struct au_fidir *fidir;
14654 +
14655 +       FiMustWriteLock(file);
14656 +
14657 +       sb = file->f_path.dentry->d_sb;
14658 +       finfo = au_fi(file);
14659 +       fidir = finfo->fi_hdir;
14660 +       AuDebugOn(!fidir);
14661 +       p = fidir->fd_hfile + finfo->fi_btop;
14662 +       brid = p->hf_br->br_id;
14663 +       bbot = fidir->fd_bbot;
14664 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14665 +               if (!p->hf_file)
14666 +                       continue;
14667 +
14668 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14669 +               if (new_bindex == bindex)
14670 +                       continue;
14671 +               if (new_bindex < 0) {
14672 +                       au_set_h_fptr(file, bindex, NULL);
14673 +                       continue;
14674 +               }
14675 +
14676 +               /* swap two lower inode, and loop again */
14677 +               q = fidir->fd_hfile + new_bindex;
14678 +               tmp = *q;
14679 +               *q = *p;
14680 +               *p = tmp;
14681 +               if (tmp.hf_file) {
14682 +                       bindex--;
14683 +                       p--;
14684 +               }
14685 +       }
14686 +
14687 +       p = fidir->fd_hfile;
14688 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14689 +               bbot = au_sbbot(sb);
14690 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14691 +                    finfo->fi_btop++, p++)
14692 +                       if (p->hf_file) {
14693 +                               if (file_inode(p->hf_file))
14694 +                                       break;
14695 +                               au_hfput(p, /*execed*/0);
14696 +                       }
14697 +       } else {
14698 +               bbot = au_br_index(sb, brid);
14699 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14700 +                    finfo->fi_btop++, p++)
14701 +                       if (p->hf_file)
14702 +                               au_hfput(p, /*execed*/0);
14703 +               bbot = au_sbbot(sb);
14704 +       }
14705 +
14706 +       p = fidir->fd_hfile + bbot;
14707 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14708 +            fidir->fd_bbot--, p--)
14709 +               if (p->hf_file) {
14710 +                       if (file_inode(p->hf_file))
14711 +                               break;
14712 +                       au_hfput(p, /*execed*/0);
14713 +               }
14714 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14715 +}
14716 +
14717 +/*
14718 + * after branch manipulating, refresh the file.
14719 + */
14720 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14721 +{
14722 +       int err, need_reopen, nbr;
14723 +       aufs_bindex_t bbot, bindex;
14724 +       struct dentry *dentry;
14725 +       struct super_block *sb;
14726 +       struct au_finfo *finfo;
14727 +       struct au_hfile *hfile;
14728 +
14729 +       dentry = file->f_path.dentry;
14730 +       sb = dentry->d_sb;
14731 +       nbr = au_sbbot(sb) + 1;
14732 +       finfo = au_fi(file);
14733 +       if (!finfo->fi_hdir) {
14734 +               hfile = &finfo->fi_htop;
14735 +               AuDebugOn(!hfile->hf_file);
14736 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14737 +               AuDebugOn(bindex < 0);
14738 +               if (bindex != finfo->fi_btop)
14739 +                       au_set_fbtop(file, bindex);
14740 +       } else {
14741 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14742 +               if (unlikely(err))
14743 +                       goto out;
14744 +               au_do_refresh_dir(file);
14745 +       }
14746 +
14747 +       err = 0;
14748 +       need_reopen = 1;
14749 +       if (!au_test_mmapped(file))
14750 +               err = au_file_refresh_by_inode(file, &need_reopen);
14751 +       if (finfo->fi_hdir)
14752 +               /* harmless if err */
14753 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14754 +       if (!err && need_reopen && !d_unlinked(dentry))
14755 +               err = reopen(file);
14756 +       if (!err) {
14757 +               au_update_figen(file);
14758 +               goto out; /* success */
14759 +       }
14760 +
14761 +       /* error, close all lower files */
14762 +       if (finfo->fi_hdir) {
14763 +               bbot = au_fbbot_dir(file);
14764 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14765 +                       au_set_h_fptr(file, bindex, NULL);
14766 +       }
14767 +
14768 +out:
14769 +       return err;
14770 +}
14771 +
14772 +/* common function to regular file and dir */
14773 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14774 +                         int wlock, unsigned int fi_lsc)
14775 +{
14776 +       int err;
14777 +       unsigned int sigen, figen;
14778 +       aufs_bindex_t btop;
14779 +       unsigned char pseudo_link;
14780 +       struct dentry *dentry;
14781 +       struct inode *inode;
14782 +
14783 +       err = 0;
14784 +       dentry = file->f_path.dentry;
14785 +       inode = d_inode(dentry);
14786 +       sigen = au_sigen(dentry->d_sb);
14787 +       fi_write_lock_nested(file, fi_lsc);
14788 +       figen = au_figen(file);
14789 +       if (!fi_lsc)
14790 +               di_write_lock_child(dentry);
14791 +       else
14792 +               di_write_lock_child2(dentry);
14793 +       btop = au_dbtop(dentry);
14794 +       pseudo_link = (btop != au_ibtop(inode));
14795 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14796 +               if (!wlock) {
14797 +                       di_downgrade_lock(dentry, AuLock_IR);
14798 +                       fi_downgrade_lock(file);
14799 +               }
14800 +               goto out; /* success */
14801 +       }
14802 +
14803 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14804 +       if (au_digen_test(dentry, sigen)) {
14805 +               err = au_reval_dpath(dentry, sigen);
14806 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14807 +       }
14808 +
14809 +       if (!err)
14810 +               err = refresh_file(file, reopen);
14811 +       if (!err) {
14812 +               if (!wlock) {
14813 +                       di_downgrade_lock(dentry, AuLock_IR);
14814 +                       fi_downgrade_lock(file);
14815 +               }
14816 +       } else {
14817 +               di_write_unlock(dentry);
14818 +               fi_write_unlock(file);
14819 +       }
14820 +
14821 +out:
14822 +       return err;
14823 +}
14824 +
14825 +/* ---------------------------------------------------------------------- */
14826 +
14827 +/* cf. aufs_nopage() */
14828 +/* for madvise(2) */
14829 +static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
14830 +{
14831 +       folio_unlock(folio);
14832 +       return 0;
14833 +}
14834 +
14835 +/* it will never be called, but necessary to support O_DIRECT */
14836 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14837 +{ BUG(); return 0; }
14838 +
14839 +/* they will never be called. */
14840 +#ifdef CONFIG_AUFS_DEBUG
14841 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14842 +                           loff_t pos, unsigned len,
14843 +                           struct page **pagep, void **fsdata)
14844 +{ AuUnsupport(); return 0; }
14845 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14846 +                         loff_t pos, unsigned len, unsigned copied,
14847 +                         struct page *page, void *fsdata)
14848 +{ AuUnsupport(); return 0; }
14849 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14850 +{ AuUnsupport(); return 0; }
14851 +
14852 +static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
14853 +{ AuUnsupport(); return true; }
14854 +static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
14855 +{ AuUnsupport(); }
14856 +static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
14857 +{ AuUnsupport(); return true; }
14858 +#if 0 /* called by memory compaction regardless file */
14859 +static int aufs_migrate_folio(struct address_space *mapping, struct folio *dst,
14860 +                             struct folio *src, enum migrate_mode mode)
14861 +{ AuUnsupport(); return 0; }
14862 +#endif
14863 +static int aufs_launder_folio(struct folio *folio)
14864 +{ AuUnsupport(); return 0; }
14865 +static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
14866 +                                     size_t count)
14867 +{ AuUnsupport(); return true; }
14868 +static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
14869 +                                   bool *writeback)
14870 +{ AuUnsupport(); }
14871 +static int aufs_error_remove_page(struct address_space *mapping,
14872 +                                 struct page *page)
14873 +{ AuUnsupport(); return 0; }
14874 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14875 +                             sector_t *span)
14876 +{ AuUnsupport(); return 0; }
14877 +static void aufs_swap_deactivate(struct file *file)
14878 +{ AuUnsupport(); }
14879 +static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
14880 +{ AuUnsupport(); return 0; }
14881 +#endif /* CONFIG_AUFS_DEBUG */
14882 +
14883 +const struct address_space_operations aufs_aop = {
14884 +       .read_folio             = aufs_read_folio,
14885 +       .direct_IO              = aufs_direct_IO,
14886 +#ifdef CONFIG_AUFS_DEBUG
14887 +       .writepage              = aufs_writepage,
14888 +       /* no writepages, because of writepage */
14889 +       .dirty_folio            = aufs_dirty_folio,
14890 +       /* no readpages, because of readpage */
14891 +       .write_begin            = aufs_write_begin,
14892 +       .write_end              = aufs_write_end,
14893 +       /* no bmap, no block device */
14894 +       .invalidate_folio       = aufs_invalidate_folio,
14895 +       .release_folio          = aufs_release_folio,
14896 +       /* is fallback_migrate_page ok? */
14897 +       /* .migrate_folio       = aufs_migrate_folio, */
14898 +       .launder_folio          = aufs_launder_folio,
14899 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14900 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14901 +       .error_remove_page      = aufs_error_remove_page,
14902 +       .swap_activate          = aufs_swap_activate,
14903 +       .swap_deactivate        = aufs_swap_deactivate,
14904 +       .swap_rw                = aufs_swap_rw
14905 +#endif /* CONFIG_AUFS_DEBUG */
14906 +};
14907 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14908 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14909 +++ linux/fs/aufs/file.h        2022-11-05 23:02:18.965889284 +0100
14910 @@ -0,0 +1,342 @@
14911 +/* SPDX-License-Identifier: GPL-2.0 */
14912 +/*
14913 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14914 + *
14915 + * This program is free software; you can redistribute it and/or modify
14916 + * it under the terms of the GNU General Public License as published by
14917 + * the Free Software Foundation; either version 2 of the License, or
14918 + * (at your option) any later version.
14919 + *
14920 + * This program is distributed in the hope that it will be useful,
14921 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14922 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14923 + * GNU General Public License for more details.
14924 + *
14925 + * You should have received a copy of the GNU General Public License
14926 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14927 + */
14928 +
14929 +/*
14930 + * file operations
14931 + */
14932 +
14933 +#ifndef __AUFS_FILE_H__
14934 +#define __AUFS_FILE_H__
14935 +
14936 +#ifdef __KERNEL__
14937 +
14938 +#include <linux/file.h>
14939 +#include <linux/fs.h>
14940 +#include <linux/mm_types.h>
14941 +#include <linux/poll.h>
14942 +#include "rwsem.h"
14943 +
14944 +struct au_branch;
14945 +struct au_hfile {
14946 +       struct file             *hf_file;
14947 +       struct au_branch        *hf_br;
14948 +};
14949 +
14950 +struct au_vdir;
14951 +struct au_fidir {
14952 +       aufs_bindex_t           fd_bbot;
14953 +       aufs_bindex_t           fd_nent;
14954 +       struct au_vdir          *fd_vdir_cache;
14955 +       struct au_hfile         fd_hfile[];
14956 +};
14957 +
14958 +static inline int au_fidir_sz(int nent)
14959 +{
14960 +       AuDebugOn(nent < 0);
14961 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14962 +}
14963 +
14964 +struct au_finfo {
14965 +       atomic_t                fi_generation;
14966 +
14967 +       struct au_rwsem         fi_rwsem;
14968 +       aufs_bindex_t           fi_btop;
14969 +
14970 +       /* do not union them */
14971 +       struct {                                /* for non-dir */
14972 +               struct au_hfile                 fi_htop;
14973 +               atomic_t                        fi_mmapped;
14974 +       };
14975 +       struct au_fidir         *fi_hdir;       /* for dir only */
14976 +
14977 +       struct hlist_bl_node    fi_hlist;
14978 +       struct file             *fi_file;       /* very ugly */
14979 +       struct rcu_head         rcu;
14980 +} ____cacheline_aligned_in_smp;
14981 +
14982 +/* ---------------------------------------------------------------------- */
14983 +
14984 +/* file.c */
14985 +extern const struct address_space_operations aufs_aop;
14986 +unsigned int au_file_roflags(unsigned int flags);
14987 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14988 +                      struct file *file, int force_wr);
14989 +struct au_do_open_args {
14990 +       int             aopen;
14991 +       int             (*open)(struct file *file, int flags,
14992 +                               struct file *h_file);
14993 +       struct au_fidir *fidir;
14994 +       struct file     *h_file;
14995 +};
14996 +int au_do_open(struct file *file, struct au_do_open_args *args);
14997 +int au_reopen_nondir(struct file *file);
14998 +struct au_pin;
14999 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15000 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15001 +                         int wlock, unsigned int fi_lsc);
15002 +int au_do_flush(struct file *file, fl_owner_t id,
15003 +               int (*flush)(struct file *file, fl_owner_t id));
15004 +
15005 +/* poll.c */
15006 +#ifdef CONFIG_AUFS_POLL
15007 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15008 +#endif
15009 +
15010 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15011 +/* hfsplus.c */
15012 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15013 +                          int force_wr);
15014 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15015 +                   struct file *h_file);
15016 +#else
15017 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15018 +       aufs_bindex_t bindex, int force_wr)
15019 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15020 +          struct file *h_file);
15021 +#endif
15022 +
15023 +/* f_op.c */
15024 +extern const struct file_operations aufs_file_fop;
15025 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15026 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15027 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15028 +
15029 +/* finfo.c */
15030 +void au_hfput(struct au_hfile *hf, int execed);
15031 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15032 +                  struct file *h_file);
15033 +
15034 +void au_update_figen(struct file *file);
15035 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15036 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15037 +
15038 +void au_fi_init_once(void *_fi);
15039 +void au_finfo_fin(struct file *file);
15040 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15041 +
15042 +/* ioctl.c */
15043 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15044 +#ifdef CONFIG_COMPAT
15045 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15046 +                          unsigned long arg);
15047 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15048 +                             unsigned long arg);
15049 +#endif
15050 +
15051 +/* ---------------------------------------------------------------------- */
15052 +
15053 +static inline struct au_finfo *au_fi(struct file *file)
15054 +{
15055 +       return file->private_data;
15056 +}
15057 +
15058 +/* ---------------------------------------------------------------------- */
15059 +
15060 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15061 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15062 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15063 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15064 +/*
15065 +#define fi_read_trylock_nested(f) \
15066 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15067 +#define fi_write_trylock_nested(f) \
15068 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15069 +*/
15070 +
15071 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15072 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15073 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15074 +
15075 +/* lock subclass for finfo */
15076 +enum {
15077 +       AuLsc_FI_1,
15078 +       AuLsc_FI_2
15079 +};
15080 +
15081 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15082 +{
15083 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15084 +}
15085 +
15086 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15087 +{
15088 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15089 +}
15090 +
15091 +/*
15092 + * fi_read_lock_1, fi_write_lock_1,
15093 + * fi_read_lock_2, fi_write_lock_2
15094 + */
15095 +#define AuReadLockFunc(name) \
15096 +static inline void fi_read_lock_##name(struct file *f) \
15097 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15098 +
15099 +#define AuWriteLockFunc(name) \
15100 +static inline void fi_write_lock_##name(struct file *f) \
15101 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15102 +
15103 +#define AuRWLockFuncs(name) \
15104 +       AuReadLockFunc(name) \
15105 +       AuWriteLockFunc(name)
15106 +
15107 +AuRWLockFuncs(1);
15108 +AuRWLockFuncs(2);
15109 +
15110 +#undef AuReadLockFunc
15111 +#undef AuWriteLockFunc
15112 +#undef AuRWLockFuncs
15113 +
15114 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15115 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15116 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15117 +
15118 +/* ---------------------------------------------------------------------- */
15119 +
15120 +/* todo: hard/soft set? */
15121 +static inline aufs_bindex_t au_fbtop(struct file *file)
15122 +{
15123 +       FiMustAnyLock(file);
15124 +       return au_fi(file)->fi_btop;
15125 +}
15126 +
15127 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15128 +{
15129 +       FiMustAnyLock(file);
15130 +       AuDebugOn(!au_fi(file)->fi_hdir);
15131 +       return au_fi(file)->fi_hdir->fd_bbot;
15132 +}
15133 +
15134 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15135 +{
15136 +       FiMustAnyLock(file);
15137 +       AuDebugOn(!au_fi(file)->fi_hdir);
15138 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15139 +}
15140 +
15141 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15142 +{
15143 +       FiMustWriteLock(file);
15144 +       au_fi(file)->fi_btop = bindex;
15145 +}
15146 +
15147 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15148 +{
15149 +       FiMustWriteLock(file);
15150 +       AuDebugOn(!au_fi(file)->fi_hdir);
15151 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15152 +}
15153 +
15154 +static inline void au_set_fvdir_cache(struct file *file,
15155 +                                     struct au_vdir *vdir_cache)
15156 +{
15157 +       FiMustWriteLock(file);
15158 +       AuDebugOn(!au_fi(file)->fi_hdir);
15159 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15160 +}
15161 +
15162 +static inline struct file *au_hf_top(struct file *file)
15163 +{
15164 +       FiMustAnyLock(file);
15165 +       AuDebugOn(au_fi(file)->fi_hdir);
15166 +       return au_fi(file)->fi_htop.hf_file;
15167 +}
15168 +
15169 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15170 +{
15171 +       FiMustAnyLock(file);
15172 +       AuDebugOn(!au_fi(file)->fi_hdir);
15173 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15174 +}
15175 +
15176 +/* todo: memory barrier? */
15177 +static inline unsigned int au_figen(struct file *f)
15178 +{
15179 +       return atomic_read(&au_fi(f)->fi_generation);
15180 +}
15181 +
15182 +static inline void au_set_mmapped(struct file *f)
15183 +{
15184 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15185 +               return;
15186 +       pr_warn("fi_mmapped wrapped around\n");
15187 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15188 +               ;
15189 +}
15190 +
15191 +static inline void au_unset_mmapped(struct file *f)
15192 +{
15193 +       atomic_dec(&au_fi(f)->fi_mmapped);
15194 +}
15195 +
15196 +static inline int au_test_mmapped(struct file *f)
15197 +{
15198 +       return atomic_read(&au_fi(f)->fi_mmapped);
15199 +}
15200 +
15201 +/* customize vma->vm_file */
15202 +
15203 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15204 +                                      struct file *file)
15205 +{
15206 +       struct file *f;
15207 +
15208 +       f = vma->vm_file;
15209 +       get_file(file);
15210 +       vma->vm_file = file;
15211 +       fput(f);
15212 +}
15213 +
15214 +#ifdef CONFIG_MMU
15215 +#define AuDbgVmRegion(file, vma) do {} while (0)
15216 +
15217 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15218 +                                   struct file *file)
15219 +{
15220 +       au_do_vm_file_reset(vma, file);
15221 +}
15222 +#else
15223 +#define AuDbgVmRegion(file, vma) \
15224 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15225 +
15226 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15227 +                                   struct file *file)
15228 +{
15229 +       struct file *f;
15230 +
15231 +       au_do_vm_file_reset(vma, file);
15232 +       f = vma->vm_region->vm_file;
15233 +       get_file(file);
15234 +       vma->vm_region->vm_file = file;
15235 +       fput(f);
15236 +}
15237 +#endif /* CONFIG_MMU */
15238 +
15239 +/* handle vma->vm_prfile */
15240 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15241 +                                   struct file *file)
15242 +{
15243 +       get_file(file);
15244 +       vma->vm_prfile = file;
15245 +#ifndef CONFIG_MMU
15246 +       get_file(file);
15247 +       vma->vm_region->vm_prfile = file;
15248 +#endif
15249 +}
15250 +
15251 +#endif /* __KERNEL__ */
15252 +#endif /* __AUFS_FILE_H__ */
15253 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15254 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15255 +++ linux/fs/aufs/finfo.c       2022-11-05 23:02:18.965889284 +0100
15256 @@ -0,0 +1,149 @@
15257 +// SPDX-License-Identifier: GPL-2.0
15258 +/*
15259 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15260 + *
15261 + * This program is free software; you can redistribute it and/or modify
15262 + * it under the terms of the GNU General Public License as published by
15263 + * the Free Software Foundation; either version 2 of the License, or
15264 + * (at your option) any later version.
15265 + *
15266 + * This program is distributed in the hope that it will be useful,
15267 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15268 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15269 + * GNU General Public License for more details.
15270 + *
15271 + * You should have received a copy of the GNU General Public License
15272 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15273 + */
15274 +
15275 +/*
15276 + * file private data
15277 + */
15278 +
15279 +#include "aufs.h"
15280 +
15281 +void au_hfput(struct au_hfile *hf, int execed)
15282 +{
15283 +       if (execed)
15284 +               allow_write_access(hf->hf_file);
15285 +       fput(hf->hf_file);
15286 +       hf->hf_file = NULL;
15287 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15288 +       hf->hf_br = NULL;
15289 +}
15290 +
15291 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15292 +{
15293 +       struct au_finfo *finfo = au_fi(file);
15294 +       struct au_hfile *hf;
15295 +       struct au_fidir *fidir;
15296 +
15297 +       fidir = finfo->fi_hdir;
15298 +       if (!fidir) {
15299 +               AuDebugOn(finfo->fi_btop != bindex);
15300 +               hf = &finfo->fi_htop;
15301 +       } else
15302 +               hf = fidir->fd_hfile + bindex;
15303 +
15304 +       if (hf && hf->hf_file)
15305 +               au_hfput(hf, vfsub_file_execed(file));
15306 +       if (val) {
15307 +               FiMustWriteLock(file);
15308 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15309 +               hf->hf_file = val;
15310 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15311 +       }
15312 +}
15313 +
15314 +void au_update_figen(struct file *file)
15315 +{
15316 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15317 +       /* smp_mb(); */ /* atomic_set */
15318 +}
15319 +
15320 +/* ---------------------------------------------------------------------- */
15321 +
15322 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15323 +{
15324 +       struct au_fidir *fidir;
15325 +       int nbr;
15326 +
15327 +       nbr = au_sbbot(sb) + 1;
15328 +       if (nbr < 2)
15329 +               nbr = 2; /* initial allocate for 2 branches */
15330 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15331 +       if (fidir) {
15332 +               fidir->fd_bbot = -1;
15333 +               fidir->fd_nent = nbr;
15334 +       }
15335 +
15336 +       return fidir;
15337 +}
15338 +
15339 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15340 +{
15341 +       int err;
15342 +       struct au_fidir *fidir, *p;
15343 +
15344 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15345 +       fidir = finfo->fi_hdir;
15346 +       AuDebugOn(!fidir);
15347 +
15348 +       err = -ENOMEM;
15349 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15350 +                        GFP_NOFS, may_shrink);
15351 +       if (p) {
15352 +               p->fd_nent = nbr;
15353 +               finfo->fi_hdir = p;
15354 +               err = 0;
15355 +       }
15356 +
15357 +       return err;
15358 +}
15359 +
15360 +/* ---------------------------------------------------------------------- */
15361 +
15362 +void au_finfo_fin(struct file *file)
15363 +{
15364 +       struct au_finfo *finfo;
15365 +
15366 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15367 +
15368 +       finfo = au_fi(file);
15369 +       AuDebugOn(finfo->fi_hdir);
15370 +       AuRwDestroy(&finfo->fi_rwsem);
15371 +       au_cache_free_finfo(finfo);
15372 +}
15373 +
15374 +void au_fi_init_once(void *_finfo)
15375 +{
15376 +       struct au_finfo *finfo = _finfo;
15377 +
15378 +       au_rw_init(&finfo->fi_rwsem);
15379 +}
15380 +
15381 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15382 +{
15383 +       int err;
15384 +       struct au_finfo *finfo;
15385 +       struct dentry *dentry;
15386 +
15387 +       err = -ENOMEM;
15388 +       dentry = file->f_path.dentry;
15389 +       finfo = au_cache_alloc_finfo();
15390 +       if (unlikely(!finfo))
15391 +               goto out;
15392 +
15393 +       err = 0;
15394 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15395 +       au_rw_write_lock(&finfo->fi_rwsem);
15396 +       finfo->fi_btop = -1;
15397 +       finfo->fi_hdir = fidir;
15398 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15399 +       /* smp_mb(); */ /* atomic_set */
15400 +
15401 +       file->private_data = finfo;
15402 +
15403 +out:
15404 +       return err;
15405 +}
15406 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15407 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15408 +++ linux/fs/aufs/f_op.c        2023-09-04 13:39:55.763295607 +0200
15409 @@ -0,0 +1,769 @@
15410 +// SPDX-License-Identifier: GPL-2.0
15411 +/*
15412 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15413 + *
15414 + * This program is free software; you can redistribute it and/or modify
15415 + * it under the terms of the GNU General Public License as published by
15416 + * the Free Software Foundation; either version 2 of the License, or
15417 + * (at your option) any later version.
15418 + *
15419 + * This program is distributed in the hope that it will be useful,
15420 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15421 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15422 + * GNU General Public License for more details.
15423 + *
15424 + * You should have received a copy of the GNU General Public License
15425 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15426 + */
15427 +
15428 +/*
15429 + * file and vm operations
15430 + */
15431 +
15432 +#include <linux/aio.h>
15433 +#include <linux/fs_stack.h>
15434 +#include <linux/mman.h>
15435 +#include <linux/security.h>
15436 +#include "aufs.h"
15437 +
15438 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15439 +{
15440 +       int err;
15441 +       aufs_bindex_t bindex;
15442 +       struct dentry *dentry, *h_dentry;
15443 +       struct au_finfo *finfo;
15444 +       struct inode *h_inode;
15445 +
15446 +       FiMustWriteLock(file);
15447 +
15448 +       err = 0;
15449 +       dentry = file->f_path.dentry;
15450 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15451 +       finfo = au_fi(file);
15452 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15453 +       atomic_set(&finfo->fi_mmapped, 0);
15454 +       bindex = au_dbtop(dentry);
15455 +       if (!h_file) {
15456 +               h_dentry = au_h_dptr(dentry, bindex);
15457 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15458 +               if (unlikely(err))
15459 +                       goto out;
15460 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15461 +               if (IS_ERR(h_file)) {
15462 +                       err = PTR_ERR(h_file);
15463 +                       goto out;
15464 +               }
15465 +       } else {
15466 +               h_dentry = h_file->f_path.dentry;
15467 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15468 +               if (unlikely(err))
15469 +                       goto out;
15470 +               /* br ref is already inc-ed */
15471 +       }
15472 +
15473 +       if (flags & __O_TMPFILE) {
15474 +               AuDebugOn(!h_file);
15475 +               AuDebugOn(h_file != au_di(dentry)->di_htmpfile);
15476 +               au_di(dentry)->di_htmpfile = NULL;
15477 +
15478 +               if (!(flags & O_EXCL)) {
15479 +                       h_inode = file_inode(h_file);
15480 +                       spin_lock(&h_inode->i_lock);
15481 +                       h_inode->i_state |= I_LINKABLE;
15482 +                       spin_unlock(&h_inode->i_lock);
15483 +               }
15484 +       }
15485 +       au_set_fbtop(file, bindex);
15486 +       au_set_h_fptr(file, bindex, h_file);
15487 +       au_update_figen(file);
15488 +       /* todo: necessary? */
15489 +       /* file->f_ra = h_file->f_ra; */
15490 +
15491 +out:
15492 +       return err;
15493 +}
15494 +
15495 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15496 +                           struct file *file)
15497 +{
15498 +       int err;
15499 +       struct super_block *sb;
15500 +       struct au_do_open_args args = {
15501 +               .open   = au_do_open_nondir
15502 +       };
15503 +
15504 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15505 +             file, vfsub_file_flags(file), file->f_mode);
15506 +
15507 +       sb = file->f_path.dentry->d_sb;
15508 +       si_read_lock(sb, AuLock_FLUSH);
15509 +       err = au_do_open(file, &args);
15510 +       si_read_unlock(sb);
15511 +       return err;
15512 +}
15513 +
15514 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15515 +{
15516 +       struct au_finfo *finfo;
15517 +       aufs_bindex_t bindex;
15518 +
15519 +       finfo = au_fi(file);
15520 +       au_hbl_del(&finfo->fi_hlist,
15521 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15522 +       bindex = finfo->fi_btop;
15523 +       if (bindex >= 0)
15524 +               au_set_h_fptr(file, bindex, NULL);
15525 +
15526 +       au_finfo_fin(file);
15527 +       return 0;
15528 +}
15529 +
15530 +/* ---------------------------------------------------------------------- */
15531 +
15532 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15533 +{
15534 +       int err;
15535 +       struct file *h_file;
15536 +
15537 +       err = 0;
15538 +       h_file = au_hf_top(file);
15539 +       if (h_file)
15540 +               err = vfsub_flush(h_file, id);
15541 +       return err;
15542 +}
15543 +
15544 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15545 +{
15546 +       return au_do_flush(file, id, au_do_flush_nondir);
15547 +}
15548 +
15549 +/* ---------------------------------------------------------------------- */
15550 +/*
15551 + * read and write functions acquire [fdi]_rwsem once, but release before
15552 + * mmap_sem. This is because to stop a race condition between mmap(2).
15553 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15554 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15555 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15556 + */
15557 +
15558 +/* Callers should call au_read_post() or fput() in the end */
15559 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15560 +{
15561 +       struct file *h_file;
15562 +       int err;
15563 +
15564 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15565 +       if (!err) {
15566 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15567 +               h_file = au_hf_top(file);
15568 +               get_file(h_file);
15569 +               if (!keep_fi)
15570 +                       fi_read_unlock(file);
15571 +       } else
15572 +               h_file = ERR_PTR(err);
15573 +
15574 +       return h_file;
15575 +}
15576 +
15577 +static void au_read_post(struct inode *inode, struct file *h_file)
15578 +{
15579 +       /* update without lock, I don't think it a problem */
15580 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15581 +       fput(h_file);
15582 +}
15583 +
15584 +struct au_write_pre {
15585 +       /* input */
15586 +       unsigned int lsc;
15587 +
15588 +       /* output */
15589 +       blkcnt_t blks;
15590 +       aufs_bindex_t btop;
15591 +};
15592 +
15593 +/*
15594 + * return with iinfo is write-locked
15595 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15596 + * end
15597 + */
15598 +static struct file *au_write_pre(struct file *file, int do_ready,
15599 +                                struct au_write_pre *wpre)
15600 +{
15601 +       struct file *h_file;
15602 +       struct dentry *dentry;
15603 +       int err;
15604 +       unsigned int lsc;
15605 +       struct au_pin pin;
15606 +
15607 +       lsc = 0;
15608 +       if (wpre)
15609 +               lsc = wpre->lsc;
15610 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15611 +       h_file = ERR_PTR(err);
15612 +       if (unlikely(err))
15613 +               goto out;
15614 +
15615 +       dentry = file->f_path.dentry;
15616 +       if (do_ready) {
15617 +               err = au_ready_to_write(file, -1, &pin);
15618 +               if (unlikely(err)) {
15619 +                       h_file = ERR_PTR(err);
15620 +                       di_write_unlock(dentry);
15621 +                       goto out_fi;
15622 +               }
15623 +       }
15624 +
15625 +       di_downgrade_lock(dentry, /*flags*/0);
15626 +       if (wpre)
15627 +               wpre->btop = au_fbtop(file);
15628 +       h_file = au_hf_top(file);
15629 +       get_file(h_file);
15630 +       if (wpre)
15631 +               wpre->blks = file_inode(h_file)->i_blocks;
15632 +       if (do_ready)
15633 +               au_unpin(&pin);
15634 +       di_read_unlock(dentry, /*flags*/0);
15635 +
15636 +out_fi:
15637 +       fi_write_unlock(file);
15638 +out:
15639 +       return h_file;
15640 +}
15641 +
15642 +static void au_write_post(struct inode *inode, struct file *h_file,
15643 +                         struct au_write_pre *wpre, ssize_t written)
15644 +{
15645 +       struct inode *h_inode;
15646 +
15647 +       au_cpup_attr_timesizes(inode);
15648 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15649 +       h_inode = file_inode(h_file);
15650 +       inode->i_mode = h_inode->i_mode;
15651 +       ii_write_unlock(inode);
15652 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15653 +       if (written > 0)
15654 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15655 +                             /*force*/h_inode->i_blocks > wpre->blks);
15656 +       fput(h_file);
15657 +}
15658 +
15659 +/*
15660 + * todo: very ugly
15661 + * it locks both of i_mutex and si_rwsem for read in safe.
15662 + * if the plink maintenance mode continues forever (that is the problem),
15663 + * may loop forever.
15664 + */
15665 +static void au_mtx_and_read_lock(struct inode *inode)
15666 +{
15667 +       int err;
15668 +       struct super_block *sb = inode->i_sb;
15669 +
15670 +       while (1) {
15671 +               inode_lock(inode);
15672 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15673 +               if (!err)
15674 +                       break;
15675 +               inode_unlock(inode);
15676 +               si_read_lock(sb, AuLock_NOPLMW);
15677 +               si_read_unlock(sb);
15678 +       }
15679 +}
15680 +
15681 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15682 +                         struct iov_iter *iov_iter)
15683 +{
15684 +       ssize_t err;
15685 +       struct file *file;
15686 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15687 +
15688 +       err = security_file_permission(h_file, rw);
15689 +       if (unlikely(err))
15690 +               goto out;
15691 +
15692 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15693 +       iter = NULL;
15694 +       if (rw == MAY_READ)
15695 +               iter = h_file->f_op->read_iter;
15696 +       else if (rw == MAY_WRITE)
15697 +               iter = h_file->f_op->write_iter;
15698 +
15699 +       file = kio->ki_filp;
15700 +       kio->ki_filp = h_file;
15701 +       if (iter) {
15702 +               lockdep_off();
15703 +               err = iter(kio, iov_iter);
15704 +               lockdep_on();
15705 +       } else
15706 +               /* currently there is no such fs */
15707 +               WARN_ON_ONCE(1);
15708 +       kio->ki_filp = file;
15709 +
15710 +out:
15711 +       return err;
15712 +}
15713 +
15714 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15715 +{
15716 +       ssize_t err;
15717 +       struct file *file, *h_file;
15718 +       struct inode *inode;
15719 +       struct super_block *sb;
15720 +
15721 +       file = kio->ki_filp;
15722 +       inode = file_inode(file);
15723 +       sb = inode->i_sb;
15724 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15725 +
15726 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15727 +       err = PTR_ERR(h_file);
15728 +       if (IS_ERR(h_file))
15729 +               goto out;
15730 +
15731 +       if (au_test_loopback_kthread()) {
15732 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15733 +               if (file->f_mapping != h_file->f_mapping) {
15734 +                       file->f_mapping = h_file->f_mapping;
15735 +                       smp_mb(); /* unnecessary? */
15736 +               }
15737 +       }
15738 +       fi_read_unlock(file);
15739 +
15740 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15741 +       /* todo: necessary? */
15742 +       /* file->f_ra = h_file->f_ra; */
15743 +       au_read_post(inode, h_file);
15744 +
15745 +out:
15746 +       si_read_unlock(sb);
15747 +       return err;
15748 +}
15749 +
15750 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15751 +{
15752 +       ssize_t err;
15753 +       struct au_write_pre wpre;
15754 +       struct inode *inode;
15755 +       struct file *file, *h_file;
15756 +
15757 +       file = kio->ki_filp;
15758 +       inode = file_inode(file);
15759 +       au_mtx_and_read_lock(inode);
15760 +
15761 +       wpre.lsc = 0;
15762 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15763 +       err = PTR_ERR(h_file);
15764 +       if (IS_ERR(h_file))
15765 +               goto out;
15766 +
15767 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15768 +       au_write_post(inode, h_file, &wpre, err);
15769 +
15770 +out:
15771 +       si_read_unlock(inode->i_sb);
15772 +       inode_unlock(inode);
15773 +       return err;
15774 +}
15775 +
15776 +/*
15777 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15778 + * don't have their own .splice_{read,write} implimentations, and they use
15779 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15780 + * simple converters to f_op->iter_read() and ->iter_write().
15781 + * But we keep our own implementations because some non-mainlined FSes may have
15782 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15783 + * away an opportunity to co-work with aufs from them.
15784 + */
15785 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15786 +                               struct pipe_inode_info *pipe, size_t len,
15787 +                               unsigned int flags)
15788 +{
15789 +       ssize_t err;
15790 +       struct file *h_file;
15791 +       struct inode *inode;
15792 +       struct super_block *sb;
15793 +
15794 +       inode = file_inode(file);
15795 +       sb = inode->i_sb;
15796 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15797 +
15798 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15799 +       err = PTR_ERR(h_file);
15800 +       if (IS_ERR(h_file))
15801 +               goto out;
15802 +
15803 +       err = vfsub_splice_read(h_file, ppos, pipe, len, flags);
15804 +       /* todo: necessary? */
15805 +       /* file->f_ra = h_file->f_ra; */
15806 +       au_read_post(inode, h_file);
15807 +
15808 +out:
15809 +       si_read_unlock(sb);
15810 +       return err;
15811 +}
15812 +
15813 +static ssize_t
15814 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15815 +                 size_t len, unsigned int flags)
15816 +{
15817 +       ssize_t err;
15818 +       struct au_write_pre wpre;
15819 +       struct inode *inode;
15820 +       struct file *h_file;
15821 +
15822 +       inode = file_inode(file);
15823 +       au_mtx_and_read_lock(inode);
15824 +
15825 +       wpre.lsc = 0;
15826 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15827 +       err = PTR_ERR(h_file);
15828 +       if (IS_ERR(h_file))
15829 +               goto out;
15830 +
15831 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15832 +       au_write_post(inode, h_file, &wpre, err);
15833 +
15834 +out:
15835 +       si_read_unlock(inode->i_sb);
15836 +       inode_unlock(inode);
15837 +       return err;
15838 +}
15839 +
15840 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15841 +                          loff_t len)
15842 +{
15843 +       long err;
15844 +       struct au_write_pre wpre;
15845 +       struct inode *inode;
15846 +       struct file *h_file;
15847 +
15848 +       inode = file_inode(file);
15849 +       au_mtx_and_read_lock(inode);
15850 +
15851 +       wpre.lsc = 0;
15852 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15853 +       err = PTR_ERR(h_file);
15854 +       if (IS_ERR(h_file))
15855 +               goto out;
15856 +
15857 +       lockdep_off();
15858 +       err = vfs_fallocate(h_file, mode, offset, len);
15859 +       lockdep_on();
15860 +       /*
15861 +        * we don't need to call file_modifed() here since au_write_post()
15862 +        * is equivalent and copies-up all timestamps and permission bits.
15863 +        */
15864 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15865 +
15866 +out:
15867 +       si_read_unlock(inode->i_sb);
15868 +       inode_unlock(inode);
15869 +       return err;
15870 +}
15871 +
15872 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15873 +                                   struct file *dst, loff_t dst_pos,
15874 +                                   size_t len, unsigned int flags)
15875 +{
15876 +       ssize_t err;
15877 +       struct au_write_pre wpre;
15878 +       enum { SRC, DST };
15879 +       struct {
15880 +               struct inode *inode;
15881 +               struct file *h_file;
15882 +               struct super_block *h_sb;
15883 +       } a[2];
15884 +#define a_src  a[SRC]
15885 +#define a_dst  a[DST]
15886 +
15887 +       err = -EINVAL;
15888 +       a_src.inode = file_inode(src);
15889 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15890 +               goto out;
15891 +       a_dst.inode = file_inode(dst);
15892 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15893 +               goto out;
15894 +
15895 +       au_mtx_and_read_lock(a_dst.inode);
15896 +       /*
15897 +        * in order to match the order in di_write_lock2_{child,parent}(),
15898 +        * use f_path.dentry for this comparison.
15899 +        */
15900 +       if (src->f_path.dentry < dst->f_path.dentry) {
15901 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15902 +               err = PTR_ERR(a_src.h_file);
15903 +               if (IS_ERR(a_src.h_file))
15904 +                       goto out_si;
15905 +
15906 +               wpre.lsc = AuLsc_FI_2;
15907 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15908 +               err = PTR_ERR(a_dst.h_file);
15909 +               if (IS_ERR(a_dst.h_file)) {
15910 +                       au_read_post(a_src.inode, a_src.h_file);
15911 +                       goto out_si;
15912 +               }
15913 +       } else {
15914 +               wpre.lsc = AuLsc_FI_1;
15915 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15916 +               err = PTR_ERR(a_dst.h_file);
15917 +               if (IS_ERR(a_dst.h_file))
15918 +                       goto out_si;
15919 +
15920 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15921 +               err = PTR_ERR(a_src.h_file);
15922 +               if (IS_ERR(a_src.h_file)) {
15923 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15924 +                                     /*written*/0);
15925 +                       goto out_si;
15926 +               }
15927 +       }
15928 +
15929 +       err = -EXDEV;
15930 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15931 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15932 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15933 +               AuDbgFile(src);
15934 +               AuDbgFile(dst);
15935 +               goto out_file;
15936 +       }
15937 +
15938 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15939 +                                   dst_pos, len, flags);
15940 +
15941 +out_file:
15942 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15943 +       fi_read_unlock(src);
15944 +       au_read_post(a_src.inode, a_src.h_file);
15945 +out_si:
15946 +       si_read_unlock(a_dst.inode->i_sb);
15947 +       inode_unlock(a_dst.inode);
15948 +out:
15949 +       return err;
15950 +#undef a_src
15951 +#undef a_dst
15952 +}
15953 +
15954 +/* ---------------------------------------------------------------------- */
15955 +
15956 +/*
15957 + * The locking order around current->mmap_sem.
15958 + * - in most and regular cases
15959 + *   file I/O syscall -- aufs_read() or something
15960 + *     -- si_rwsem for read -- mmap_sem
15961 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15962 + * - in mmap case
15963 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15964 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15965 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15966 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15967 + * It means that when aufs acquires si_rwsem for write, the process should never
15968 + * acquire mmap_sem.
15969 + *
15970 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15971 + * problem either since any directory is not able to be mmap-ed.
15972 + * The similar scenario is applied to aufs_readlink() too.
15973 + */
15974 +
15975 +#if 0 /* stop calling security_file_mmap() */
15976 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15977 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15978 +
15979 +static unsigned long au_arch_prot_conv(unsigned long flags)
15980 +{
15981 +       /* currently ppc64 only */
15982 +#ifdef CONFIG_PPC64
15983 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15984 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15985 +       return AuConv_VM_PROT(flags, SAO);
15986 +#else
15987 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15988 +       return 0;
15989 +#endif
15990 +}
15991 +
15992 +static unsigned long au_prot_conv(unsigned long flags)
15993 +{
15994 +       return AuConv_VM_PROT(flags, READ)
15995 +               | AuConv_VM_PROT(flags, WRITE)
15996 +               | AuConv_VM_PROT(flags, EXEC)
15997 +               | au_arch_prot_conv(flags);
15998 +}
15999 +
16000 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16001 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16002 +
16003 +static unsigned long au_flag_conv(unsigned long flags)
16004 +{
16005 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16006 +               | AuConv_VM_MAP(flags, DENYWRITE)
16007 +               | AuConv_VM_MAP(flags, LOCKED);
16008 +}
16009 +#endif
16010 +
16011 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16012 +{
16013 +       int err;
16014 +       const unsigned char wlock
16015 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16016 +       struct super_block *sb;
16017 +       struct file *h_file;
16018 +       struct inode *inode;
16019 +
16020 +       AuDbgVmRegion(file, vma);
16021 +
16022 +       inode = file_inode(file);
16023 +       sb = inode->i_sb;
16024 +       lockdep_off();
16025 +       si_read_lock(sb, AuLock_NOPLMW);
16026 +
16027 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16028 +       lockdep_on();
16029 +       err = PTR_ERR(h_file);
16030 +       if (IS_ERR(h_file))
16031 +               goto out;
16032 +
16033 +       err = 0;
16034 +       au_set_mmapped(file);
16035 +       au_vm_file_reset(vma, h_file);
16036 +       /*
16037 +        * we cannot call security_mmap_file() here since it may acquire
16038 +        * mmap_sem or i_mutex.
16039 +        *
16040 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16041 +        *                       au_flag_conv(vma->vm_flags));
16042 +        */
16043 +       if (!err)
16044 +               err = call_mmap(h_file, vma);
16045 +       if (!err) {
16046 +               au_vm_prfile_set(vma, file);
16047 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16048 +               goto out_fput; /* success */
16049 +       }
16050 +       au_unset_mmapped(file);
16051 +       au_vm_file_reset(vma, file);
16052 +
16053 +out_fput:
16054 +       lockdep_off();
16055 +       ii_write_unlock(inode);
16056 +       lockdep_on();
16057 +       fput(h_file);
16058 +out:
16059 +       lockdep_off();
16060 +       si_read_unlock(sb);
16061 +       lockdep_on();
16062 +       AuTraceErr(err);
16063 +       return err;
16064 +}
16065 +
16066 +/* ---------------------------------------------------------------------- */
16067 +
16068 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16069 +                            int datasync)
16070 +{
16071 +       int err;
16072 +       struct au_write_pre wpre;
16073 +       struct inode *inode;
16074 +       struct file *h_file;
16075 +
16076 +       err = 0; /* -EBADF; */ /* posix? */
16077 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16078 +               goto out;
16079 +
16080 +       inode = file_inode(file);
16081 +       au_mtx_and_read_lock(inode);
16082 +
16083 +       wpre.lsc = 0;
16084 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16085 +       err = PTR_ERR(h_file);
16086 +       if (IS_ERR(h_file))
16087 +               goto out_unlock;
16088 +
16089 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16090 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16091 +
16092 +out_unlock:
16093 +       si_read_unlock(inode->i_sb);
16094 +       inode_unlock(inode);
16095 +out:
16096 +       return err;
16097 +}
16098 +
16099 +static int aufs_fasync(int fd, struct file *file, int flag)
16100 +{
16101 +       int err;
16102 +       struct file *h_file;
16103 +       struct super_block *sb;
16104 +
16105 +       sb = file->f_path.dentry->d_sb;
16106 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16107 +
16108 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16109 +       err = PTR_ERR(h_file);
16110 +       if (IS_ERR(h_file))
16111 +               goto out;
16112 +
16113 +       if (h_file->f_op->fasync)
16114 +               err = h_file->f_op->fasync(fd, h_file, flag);
16115 +       fput(h_file); /* instead of au_read_post() */
16116 +
16117 +out:
16118 +       si_read_unlock(sb);
16119 +       return err;
16120 +}
16121 +
16122 +static int aufs_setfl(struct file *file, unsigned long arg)
16123 +{
16124 +       int err;
16125 +       struct file *h_file;
16126 +       struct super_block *sb;
16127 +
16128 +       sb = file->f_path.dentry->d_sb;
16129 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16130 +
16131 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16132 +       err = PTR_ERR(h_file);
16133 +       if (IS_ERR(h_file))
16134 +               goto out;
16135 +
16136 +       /* stop calling h_file->fasync */
16137 +       arg |= vfsub_file_flags(file) & FASYNC;
16138 +       err = setfl(/*unused fd*/-1, h_file, arg);
16139 +       fput(h_file); /* instead of au_read_post() */
16140 +
16141 +out:
16142 +       si_read_unlock(sb);
16143 +       return err;
16144 +}
16145 +
16146 +/* ---------------------------------------------------------------------- */
16147 +
16148 +const struct file_operations aufs_file_fop = {
16149 +       .owner          = THIS_MODULE,
16150 +
16151 +       .llseek         = default_llseek,
16152 +
16153 +       .read_iter      = aufs_read_iter,
16154 +       .write_iter     = aufs_write_iter,
16155 +
16156 +#ifdef CONFIG_AUFS_POLL
16157 +       .poll           = aufs_poll,
16158 +#endif
16159 +       .unlocked_ioctl = aufs_ioctl_nondir,
16160 +#ifdef CONFIG_COMPAT
16161 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16162 +#endif
16163 +       .mmap           = aufs_mmap,
16164 +       .open           = aufs_open_nondir,
16165 +       .flush          = aufs_flush_nondir,
16166 +       .release        = aufs_release_nondir,
16167 +       .fsync          = aufs_fsync_nondir,
16168 +       .fasync         = aufs_fasync,
16169 +       .setfl          = aufs_setfl,
16170 +       .splice_write   = aufs_splice_write,
16171 +       .splice_read    = aufs_splice_read,
16172 +#if 0 /* reserved for future use */
16173 +       .aio_splice_write = aufs_aio_splice_write,
16174 +       .aio_splice_read  = aufs_aio_splice_read,
16175 +#endif
16176 +       .fallocate      = aufs_fallocate,
16177 +       .copy_file_range = aufs_copy_file_range
16178 +};
16179 diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16180 --- /usr/share/empty/fs/aufs/fsctx.c    1970-01-01 01:00:00.000000000 +0100
16181 +++ linux/fs/aufs/fsctx.c       2022-11-05 23:02:18.965889284 +0100
16182 @@ -0,0 +1,1242 @@
16183 +// SPDX-License-Identifier: GPL-2.0
16184 +/*
16185 + * Copyright (C) 2022 Junjiro R. Okajima
16186 + *
16187 + * This program is free software; you can redistribute it and/or modify
16188 + * it under the terms of the GNU General Public License as published by
16189 + * the Free Software Foundation; either version 2 of the License, or
16190 + * (at your option) any later version.
16191 + *
16192 + * This program is distributed in the hope that it will be useful,
16193 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16194 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16195 + * GNU General Public License for more details.
16196 + *
16197 + * You should have received a copy of the GNU General Public License
16198 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16199 + */
16200 +
16201 +/*
16202 + * fs context, aka new mount api
16203 + */
16204 +
16205 +#include <linux/fs_context.h>
16206 +#include "aufs.h"
16207 +
16208 +struct au_fsctx_opts {
16209 +       aufs_bindex_t bindex;
16210 +       unsigned char skipped;
16211 +       struct au_opt *opt, *opt_tail;
16212 +       struct super_block *sb;
16213 +       struct au_sbinfo *sbinfo;
16214 +       struct au_opts opts;
16215 +};
16216 +
16217 +/* stop extra interpretation of errno in mount(8), and strange error messages */
16218 +static int cvt_err(int err)
16219 +{
16220 +       AuTraceErr(err);
16221 +
16222 +       switch (err) {
16223 +       case -ENOENT:
16224 +       case -ENOTDIR:
16225 +       case -EEXIST:
16226 +       case -EIO:
16227 +               err = -EINVAL;
16228 +       }
16229 +       return err;
16230 +}
16231 +
16232 +static int au_fsctx_reconfigure(struct fs_context *fc)
16233 +{
16234 +       int err, do_dx;
16235 +       unsigned int mntflags;
16236 +       struct dentry *root;
16237 +       struct super_block *sb;
16238 +       struct inode *inode;
16239 +       struct au_fsctx_opts *a = fc->fs_private;
16240 +
16241 +       AuDbg("fc %p\n", fc);
16242 +
16243 +       root = fc->root;
16244 +       sb = root->d_sb;
16245 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16246 +       if (!err) {
16247 +               di_write_lock_child(root);
16248 +               err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16249 +               aufs_write_unlock(root);
16250 +       }
16251 +
16252 +       inode = d_inode(root);
16253 +       inode_lock(inode);
16254 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16255 +       if (unlikely(err))
16256 +               goto out;
16257 +       di_write_lock_child(root);
16258 +
16259 +       /* au_opts_remount() may return an error */
16260 +       err = au_opts_remount(sb, &a->opts);
16261 +
16262 +       if (au_ftest_opts(a->opts.flags, REFRESH))
16263 +               au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16264 +                                                    REFRESH_IDOP));
16265 +
16266 +       if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16267 +               mntflags = au_mntflags(sb);
16268 +               do_dx = !!au_opt_test(mntflags, DIO);
16269 +               au_dy_arefresh(do_dx);
16270 +       }
16271 +
16272 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16273 +       aufs_write_unlock(root);
16274 +
16275 +out:
16276 +       inode_unlock(inode);
16277 +       err = cvt_err(err);
16278 +       AuTraceErr(err);
16279 +
16280 +       return err;
16281 +}
16282 +
16283 +/* ---------------------------------------------------------------------- */
16284 +
16285 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16286 +{
16287 +       int err;
16288 +       struct au_fsctx_opts *a = fc->fs_private;
16289 +       struct au_sbinfo *sbinfo = a->sbinfo;
16290 +       struct dentry *root;
16291 +       struct inode *inode;
16292 +
16293 +       sbinfo->si_sb = sb;
16294 +       sb->s_fs_info = sbinfo;
16295 +       kobject_get(&sbinfo->si_kobj);
16296 +
16297 +       __si_write_lock(sb);
16298 +       si_pid_set(sb);
16299 +       au_sbilist_add(sb);
16300 +
16301 +       /* all timestamps always follow the ones on the branch */
16302 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16303 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16304 +       sb->s_op = &aufs_sop;
16305 +       sb->s_d_op = &aufs_dop;
16306 +       sb->s_magic = AUFS_SUPER_MAGIC;
16307 +       sb->s_maxbytes = 0;
16308 +       sb->s_stack_depth = 1;
16309 +       au_export_init(sb);
16310 +       au_xattr_init(sb);
16311 +
16312 +       err = au_alloc_root(sb);
16313 +       if (unlikely(err)) {
16314 +               si_write_unlock(sb);
16315 +               goto out;
16316 +       }
16317 +       root = sb->s_root;
16318 +       inode = d_inode(root);
16319 +       ii_write_lock_parent(inode);
16320 +       aufs_write_unlock(root);
16321 +
16322 +       /* lock vfs_inode first, then aufs. */
16323 +       inode_lock(inode);
16324 +       aufs_write_lock(root);
16325 +       err = au_opts_mount(sb, &a->opts);
16326 +       AuTraceErr(err);
16327 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16328 +               sb->s_d_op = &aufs_dop_noreval;
16329 +               /* infofc(fc, "%ps", sb->s_d_op); */
16330 +               pr_info("%ps\n", sb->s_d_op);
16331 +               au_refresh_dop(root, /*force_reval*/0);
16332 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
16333 +               au_refresh_iop(inode, /*force_getattr*/0);
16334 +       }
16335 +       aufs_write_unlock(root);
16336 +       inode_unlock(inode);
16337 +       if (!err)
16338 +               goto out; /* success */
16339 +
16340 +       dput(root);
16341 +       sb->s_root = NULL;
16342 +
16343 +out:
16344 +       if (unlikely(err))
16345 +               kobject_put(&sbinfo->si_kobj);
16346 +       AuTraceErr(err);
16347 +       err = cvt_err(err);
16348 +       AuTraceErr(err);
16349 +       return err;
16350 +}
16351 +
16352 +static int au_fsctx_get_tree(struct fs_context *fc)
16353 +{
16354 +       int err;
16355 +
16356 +       AuDbg("fc %p\n", fc);
16357 +       err = get_tree_nodev(fc, au_fsctx_fill_super);
16358 +
16359 +       AuTraceErr(err);
16360 +       return err;
16361 +}
16362 +
16363 +/* ---------------------------------------------------------------------- */
16364 +
16365 +static void au_fsctx_dump(struct au_opts *opts)
16366 +{
16367 +#ifdef CONFIG_AUFS_DEBUG
16368 +       /* reduce stack space */
16369 +       union {
16370 +               struct au_opt_add *add;
16371 +               struct au_opt_del *del;
16372 +               struct au_opt_mod *mod;
16373 +               struct au_opt_xino *xino;
16374 +               struct au_opt_xino_itrunc *xino_itrunc;
16375 +               struct au_opt_wbr_create *create;
16376 +       } u;
16377 +       struct au_opt *opt;
16378 +
16379 +       opt = opts->opt;
16380 +       while (opt->type != Opt_tail) {
16381 +               switch (opt->type) {
16382 +               case Opt_add:
16383 +                       u.add = &opt->add;
16384 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16385 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16386 +                                 u.add->path.dentry);
16387 +                       break;
16388 +               case Opt_del:
16389 +                       fallthrough;
16390 +               case Opt_idel:
16391 +                       u.del = &opt->del;
16392 +                       AuDbg("del {%s, %p}\n",
16393 +                             u.del->pathname, u.del->h_path.dentry);
16394 +                       break;
16395 +               case Opt_mod:
16396 +                       fallthrough;
16397 +               case Opt_imod:
16398 +                       u.mod = &opt->mod;
16399 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16400 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16401 +                       break;
16402 +               case Opt_append:
16403 +                       u.add = &opt->add;
16404 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16405 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16406 +                                 u.add->path.dentry);
16407 +                       break;
16408 +               case Opt_prepend:
16409 +                       u.add = &opt->add;
16410 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16411 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16412 +                                 u.add->path.dentry);
16413 +                       break;
16414 +
16415 +               case Opt_dirwh:
16416 +                       AuDbg("dirwh %d\n", opt->dirwh);
16417 +                       break;
16418 +               case Opt_rdcache:
16419 +                       AuDbg("rdcache %d\n", opt->rdcache);
16420 +                       break;
16421 +               case Opt_rdblk:
16422 +                       AuDbg("rdblk %d\n", opt->rdblk);
16423 +                       break;
16424 +               case Opt_rdhash:
16425 +                       AuDbg("rdhash %u\n", opt->rdhash);
16426 +                       break;
16427 +
16428 +               case Opt_xino:
16429 +                       u.xino = &opt->xino;
16430 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16431 +                       break;
16432 +
16433 +#define au_fsctx_TF(name)                                        \
16434 +                       case Opt_##name:                          \
16435 +                               if (opt->tf)                      \
16436 +                                       AuLabel(name);            \
16437 +                               else                              \
16438 +                                       AuLabel(no##name);        \
16439 +                               break;
16440 +
16441 +               /* simple true/false flag */
16442 +               au_fsctx_TF(trunc_xino);
16443 +               au_fsctx_TF(trunc_xib);
16444 +               au_fsctx_TF(dirperm1);
16445 +               au_fsctx_TF(plink);
16446 +               au_fsctx_TF(shwh);
16447 +               au_fsctx_TF(dio);
16448 +               au_fsctx_TF(warn_perm);
16449 +               au_fsctx_TF(verbose);
16450 +               au_fsctx_TF(sum);
16451 +               au_fsctx_TF(dirren);
16452 +               au_fsctx_TF(acl);
16453 +#undef au_fsctx_TF
16454 +
16455 +               case Opt_trunc_xino_path:
16456 +                       fallthrough;
16457 +               case Opt_itrunc_xino:
16458 +                       u.xino_itrunc = &opt->xino_itrunc;
16459 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16460 +                       break;
16461 +               case Opt_noxino:
16462 +                       AuLabel(noxino);
16463 +                       break;
16464 +
16465 +               case Opt_list_plink:
16466 +                       AuLabel(list_plink);
16467 +                       break;
16468 +               case Opt_udba:
16469 +                       AuDbg("udba %d, %s\n",
16470 +                                 opt->udba, au_optstr_udba(opt->udba));
16471 +                       break;
16472 +               case Opt_diropq_a:
16473 +                       AuLabel(diropq_a);
16474 +                       break;
16475 +               case Opt_diropq_w:
16476 +                       AuLabel(diropq_w);
16477 +                       break;
16478 +               case Opt_wsum:
16479 +                       AuLabel(wsum);
16480 +                       break;
16481 +               case Opt_wbr_create:
16482 +                       u.create = &opt->wbr_create;
16483 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
16484 +                                 au_optstr_wbr_create(u.create->wbr_create));
16485 +                       switch (u.create->wbr_create) {
16486 +                       case AuWbrCreate_MFSV:
16487 +                               fallthrough;
16488 +                       case AuWbrCreate_PMFSV:
16489 +                               AuDbg("%d sec\n", u.create->mfs_second);
16490 +                               break;
16491 +                       case AuWbrCreate_MFSRR:
16492 +                               fallthrough;
16493 +                       case AuWbrCreate_TDMFS:
16494 +                               AuDbg("%llu watermark\n",
16495 +                                         u.create->mfsrr_watermark);
16496 +                               break;
16497 +                       case AuWbrCreate_MFSRRV:
16498 +                               fallthrough;
16499 +                       case AuWbrCreate_TDMFSV:
16500 +                               fallthrough;
16501 +                       case AuWbrCreate_PMFSRRV:
16502 +                               AuDbg("%llu watermark, %d sec\n",
16503 +                                         u.create->mfsrr_watermark,
16504 +                                         u.create->mfs_second);
16505 +                               break;
16506 +                       }
16507 +                       break;
16508 +               case Opt_wbr_copyup:
16509 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16510 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
16511 +                       break;
16512 +               case Opt_fhsm_sec:
16513 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16514 +                       break;
16515 +
16516 +               default:
16517 +                       AuDbg("type %d\n", opt->type);
16518 +                       BUG();
16519 +               }
16520 +               opt++;
16521 +       }
16522 +#endif
16523 +}
16524 +
16525 +/* ---------------------------------------------------------------------- */
16526 +
16527 +/*
16528 + * For conditionally compiled mount options.
16529 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16530 + */
16531 +#define au_ignore_flag(name, action)           \
16532 +       fsparam_flag(name, action),             \
16533 +       fsparam_flag("no" name, Opt_ignore_silent)
16534 +
16535 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16536 +       fsparam_string("br", Opt_br),
16537 +
16538 +       /* "add=%d:%s" or "ins=%d:%s" */
16539 +       fsparam_string("add", Opt_add),
16540 +       fsparam_string("ins", Opt_add),
16541 +       fsparam_path("append", Opt_append),
16542 +       fsparam_path("prepend", Opt_prepend),
16543 +
16544 +       fsparam_path("del", Opt_del),
16545 +       /* fsparam_s32("idel", Opt_idel), */
16546 +       fsparam_path("mod", Opt_mod),
16547 +       /* fsparam_string("imod", Opt_imod), */
16548 +
16549 +       fsparam_s32("dirwh", Opt_dirwh),
16550 +
16551 +       fsparam_path("xino", Opt_xino),
16552 +       fsparam_flag("noxino", Opt_noxino),
16553 +       fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16554 +       /* "trunc_xino_v=%d:%d" */
16555 +       /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16556 +       fsparam_path("trunc_xino", Opt_trunc_xino_path),
16557 +       fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16558 +       /* fsparam_path("zxino", Opt_zxino), */
16559 +       fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16560 +
16561 +#ifdef CONFIG_PROC_FS
16562 +       fsparam_flag_no("plink", Opt_plink),
16563 +#else
16564 +       au_ignore_flag("plink", Opt_ignore),
16565 +#endif
16566 +
16567 +#ifdef CONFIG_AUFS_DEBUG
16568 +       fsparam_flag("list_plink", Opt_list_plink),
16569 +#endif
16570 +
16571 +       fsparam_string("udba", Opt_udba),
16572 +
16573 +       fsparam_flag_no("dio", Opt_dio),
16574 +
16575 +#ifdef CONFIG_AUFS_DIRREN
16576 +       fsparam_flag_no("dirren", Opt_dirren),
16577 +#else
16578 +       au_ignore_flag("dirren", Opt_ignore),
16579 +#endif
16580 +
16581 +#ifdef CONFIG_AUFS_FHSM
16582 +       fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16583 +#else
16584 +       fsparam_s32("fhsm_sec", Opt_ignore),
16585 +#endif
16586 +
16587 +       /* always | a | whiteouted | w */
16588 +       fsparam_string("diropq", Opt_diropq),
16589 +
16590 +       fsparam_flag_no("warn_perm", Opt_warn_perm),
16591 +
16592 +#ifdef CONFIG_AUFS_SHWH
16593 +       fsparam_flag_no("shwh", Opt_shwh),
16594 +#else
16595 +       au_ignore_flag("shwh", Opt_err),
16596 +#endif
16597 +
16598 +       fsparam_flag_no("dirperm1", Opt_dirperm1),
16599 +
16600 +       fsparam_flag_no("verbose", Opt_verbose),
16601 +       fsparam_flag("v", Opt_verbose),
16602 +       fsparam_flag("quiet", Opt_noverbose),
16603 +       fsparam_flag("q", Opt_noverbose),
16604 +       /* user-space may handle this */
16605 +       fsparam_flag("silent", Opt_noverbose),
16606 +
16607 +       fsparam_flag_no("sum", Opt_sum),
16608 +       fsparam_flag("wsum", Opt_wsum),
16609 +
16610 +       fsparam_s32("rdcache", Opt_rdcache),
16611 +       /* "def" or s32 */
16612 +       fsparam_string("rdblk", Opt_rdblk),
16613 +       /* "def" or s32 */
16614 +       fsparam_string("rdhash", Opt_rdhash),
16615 +
16616 +       fsparam_string("create", Opt_wbr_create),
16617 +       fsparam_string("create_policy", Opt_wbr_create),
16618 +       fsparam_string("cpup", Opt_wbr_copyup),
16619 +       fsparam_string("copyup", Opt_wbr_copyup),
16620 +       fsparam_string("copyup_policy", Opt_wbr_copyup),
16621 +
16622 +       /* generic VFS flag */
16623 +#ifdef CONFIG_FS_POSIX_ACL
16624 +       fsparam_flag_no("acl", Opt_acl),
16625 +#else
16626 +       au_ignore_flag("acl", Opt_ignore),
16627 +#endif
16628 +
16629 +       /* internal use for the scripts */
16630 +       fsparam_string("si", Opt_ignore_silent),
16631 +
16632 +       /* obsoleted, keep them temporary */
16633 +       fsparam_flag("nodlgt", Opt_ignore_silent),
16634 +       fsparam_flag("clean_plink", Opt_ignore),
16635 +       fsparam_string("dirs", Opt_br),
16636 +       fsparam_u32("debug", Opt_ignore),
16637 +       /* "whiteout" or "all" */
16638 +       fsparam_string("delete", Opt_ignore),
16639 +       fsparam_string("imap", Opt_ignore),
16640 +
16641 +       /* temporary workaround, due to old mount(8)? */
16642 +       fsparam_flag("relatime", Opt_ignore_silent),
16643 +
16644 +       {}
16645 +};
16646 +
16647 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16648 +                                char *brspec, size_t speclen,
16649 +                                aufs_bindex_t bindex)
16650 +{
16651 +       int err;
16652 +       char *p;
16653 +
16654 +       AuDbg("brspec %s\n", brspec);
16655 +
16656 +       err = -ENOMEM;
16657 +       if (!speclen)
16658 +               speclen = strlen(brspec);
16659 +       /* will be freed by au_fsctx_free() */
16660 +       p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16661 +       if (unlikely(!p)) {
16662 +               errorfc(fc, "failed in %s", brspec);
16663 +               goto out;
16664 +       }
16665 +       err = au_opt_add(opt, p, fc->sb_flags, bindex);
16666 +
16667 +out:
16668 +       AuTraceErr(err);
16669 +       return err;
16670 +}
16671 +
16672 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16673 +{
16674 +       int err;
16675 +       char *p;
16676 +       struct au_fsctx_opts *a = fc->fs_private;
16677 +       struct au_opt *opt = a->opt;
16678 +       aufs_bindex_t bindex = a->bindex;
16679 +
16680 +       AuDbg("brspec %s\n", brspec);
16681 +
16682 +       err = -EINVAL;
16683 +       while ((p = strsep(&brspec, ":")) && *p) {
16684 +               err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16685 +               AuTraceErr(err);
16686 +               if (unlikely(err))
16687 +                       break;
16688 +               bindex++;
16689 +               opt++;
16690 +               if (unlikely(opt > a->opt_tail)) {
16691 +                       err = -E2BIG;
16692 +                       bindex--;
16693 +                       opt--;
16694 +                       break;
16695 +               }
16696 +               opt->type = Opt_tail;
16697 +               a->skipped = 1;
16698 +       }
16699 +       a->bindex = bindex;
16700 +       a->opt = opt;
16701 +
16702 +       AuTraceErr(err);
16703 +       return err;
16704 +}
16705 +
16706 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16707 +{
16708 +       int err, n;
16709 +       char *p;
16710 +       struct au_fsctx_opts *a = fc->fs_private;
16711 +       struct au_opt *opt = a->opt;
16712 +
16713 +       err = -EINVAL;
16714 +       p = strchr(addspec, ':');
16715 +       if (unlikely(!p)) {
16716 +               errorfc(fc, "bad arg in %s", addspec);
16717 +               goto out;
16718 +       }
16719 +       *p++ = '\0';
16720 +       err = kstrtoint(addspec, 0, &n);
16721 +       if (unlikely(err)) {
16722 +               errorfc(fc, "bad integer in %s", addspec);
16723 +               goto out;
16724 +       }
16725 +       AuDbg("n %d\n", n);
16726 +       err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16727 +
16728 +out:
16729 +       AuTraceErr(err);
16730 +       return err;
16731 +}
16732 +
16733 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16734 +                             struct fs_parameter *param)
16735 +{
16736 +       int err;
16737 +
16738 +       err = -ENOMEM;
16739 +       /* will be freed by au_fsctx_free() */
16740 +       del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16741 +       if (unlikely(!del->pathname))
16742 +               goto out;
16743 +       AuDbg("del %s\n", del->pathname);
16744 +       err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16745 +       if (unlikely(err))
16746 +               errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16747 +
16748 +out:
16749 +       AuTraceErr(err);
16750 +       return err;
16751 +}
16752 +
16753 +#if 0 /* reserved for future use */
16754 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16755 +                              aufs_bindex_t bindex)
16756 +{
16757 +       int err;
16758 +       struct super_block *sb;
16759 +       struct dentry *root;
16760 +       struct au_fsctx_opts *a = fc->fs_private;
16761 +
16762 +       sb = a->sb;
16763 +       AuDebugOn(!sb);
16764 +
16765 +       err = -EINVAL;
16766 +       root = sb->s_root;
16767 +       aufs_read_lock(root, AuLock_FLUSH);
16768 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
16769 +               errorfc(fc, "out of bounds, %d", bindex);
16770 +               goto out;
16771 +       }
16772 +
16773 +       err = 0;
16774 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
16775 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16776 +
16777 +out:
16778 +       aufs_read_unlock(root, !AuLock_IR);
16779 +       AuTraceErr(err);
16780 +       return err;
16781 +}
16782 +#endif
16783 +
16784 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16785 +                             struct fs_parameter *param)
16786 +{
16787 +       int err;
16788 +       struct path path;
16789 +       char *p;
16790 +
16791 +       err = -ENOMEM;
16792 +       /* will be freed by au_fsctx_free() */
16793 +       mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16794 +       if (unlikely(!mod->path))
16795 +               goto out;
16796 +
16797 +       err = -EINVAL;
16798 +       p = strchr(mod->path, '=');
16799 +       if (unlikely(!p)) {
16800 +               errorfc(fc, "no permission %s", mod->path);
16801 +               goto out;
16802 +       }
16803 +
16804 +       *p++ = 0;
16805 +       err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16806 +       if (unlikely(err)) {
16807 +               errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16808 +               goto out;
16809 +       }
16810 +
16811 +       mod->perm = au_br_perm_val(p);
16812 +       AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16813 +       mod->h_root = dget(path.dentry);
16814 +       path_put(&path);
16815 +
16816 +out:
16817 +       AuTraceErr(err);
16818 +       return err;
16819 +}
16820 +
16821 +#if 0 /* reserved for future use */
16822 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16823 +                              char *ibrspec)
16824 +{
16825 +       int err, n;
16826 +       char *p;
16827 +       struct super_block *sb;
16828 +       struct dentry *root;
16829 +       struct au_fsctx_opts *a = fc->fs_private;
16830 +
16831 +       sb = a->sb;
16832 +       AuDebugOn(!sb);
16833 +
16834 +       err = -EINVAL;
16835 +       p = strchr(ibrspec, ':');
16836 +       if (unlikely(!p)) {
16837 +               errorfc(fc, "no index, %s", ibrspec);
16838 +               goto out;
16839 +       }
16840 +       *p++ = '\0';
16841 +       err = kstrtoint(ibrspec, 0, &n);
16842 +       if (unlikely(err)) {
16843 +               errorfc(fc, "bad integer in %s", ibrspec);
16844 +               goto out;
16845 +       }
16846 +       AuDbg("n %d\n", n);
16847 +
16848 +       root = sb->s_root;
16849 +       aufs_read_lock(root, AuLock_FLUSH);
16850 +       if (n < 0 || au_sbbot(sb) < n) {
16851 +               errorfc(fc, "out of bounds, %d", bindex);
16852 +               goto out_root;
16853 +       }
16854 +
16855 +       err = 0;
16856 +       mod->perm = au_br_perm_val(p);
16857 +       AuDbg("mod path %s, perm 0x%x, %s\n",
16858 +             mod->path, mod->perm, p);
16859 +       mod->h_root = dget(au_h_dptr(root, bindex));
16860 +
16861 +out_root:
16862 +       aufs_read_unlock(root, !AuLock_IR);
16863 +out:
16864 +       AuTraceErr(err);
16865 +       return err;
16866 +}
16867 +#endif
16868 +
16869 +static int au_fsctx_parse_xino(struct fs_context *fc,
16870 +                              struct au_opt_xino *xino,
16871 +                              struct fs_parameter *param)
16872 +{
16873 +       int err;
16874 +       struct au_fsctx_opts *a = fc->fs_private;
16875 +
16876 +       err = -ENOMEM;
16877 +       /* will be freed by au_opts_free() */
16878 +       xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16879 +       if (unlikely(!xino->path))
16880 +               goto out;
16881 +       AuDbg("path %s\n", xino->path);
16882 +
16883 +       xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16884 +                                   /*wbrtop*/0);
16885 +       err = PTR_ERR(xino->file);
16886 +       if (IS_ERR(xino->file)) {
16887 +               xino->file = NULL;
16888 +               goto out;
16889 +       }
16890 +
16891 +       err = 0;
16892 +       if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16893 +               err = -EINVAL;
16894 +               errorfc(fc, "%s must be outside", xino->path);
16895 +       }
16896 +
16897 +out:
16898 +       AuTraceErr(err);
16899 +       return err;
16900 +}
16901 +
16902 +static
16903 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16904 +                                   struct au_opt_xino_itrunc *xino_itrunc,
16905 +                                   char *pathname)
16906 +{
16907 +       int err;
16908 +       aufs_bindex_t bbot, bindex;
16909 +       struct path path;
16910 +       struct dentry *root;
16911 +       struct au_fsctx_opts *a = fc->fs_private;
16912 +
16913 +       AuDebugOn(!a->sb);
16914 +
16915 +       err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16916 +       if (unlikely(err)) {
16917 +               errorfc(fc, "lookup failed %s (%d)", pathname, err);
16918 +               goto out;
16919 +       }
16920 +
16921 +       xino_itrunc->bindex = -1;
16922 +       root = a->sb->s_root;
16923 +       aufs_read_lock(root, AuLock_FLUSH);
16924 +       bbot = au_sbbot(a->sb);
16925 +       for (bindex = 0; bindex <= bbot; bindex++) {
16926 +               if (au_h_dptr(root, bindex) == path.dentry) {
16927 +                       xino_itrunc->bindex = bindex;
16928 +                       break;
16929 +               }
16930 +       }
16931 +       aufs_read_unlock(root, !AuLock_IR);
16932 +       path_put(&path);
16933 +
16934 +       if (unlikely(xino_itrunc->bindex < 0)) {
16935 +               err = -EINVAL;
16936 +               errorfc(fc, "no such branch %s", pathname);
16937 +       }
16938 +
16939 +out:
16940 +       AuTraceErr(err);
16941 +       return err;
16942 +}
16943 +
16944 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16945 +                                     struct au_opt_xino_itrunc *xino_itrunc,
16946 +                                     unsigned int bindex)
16947 +{
16948 +       int err;
16949 +       aufs_bindex_t bbot;
16950 +       struct super_block *sb;
16951 +       struct au_fsctx_opts *a = fc->fs_private;
16952 +
16953 +       sb = a->sb;
16954 +       AuDebugOn(!sb);
16955 +
16956 +       err = 0;
16957 +       si_noflush_read_lock(sb);
16958 +       bbot = au_sbbot(sb);
16959 +       si_read_unlock(sb);
16960 +       if (bindex <= bbot)
16961 +               xino_itrunc->bindex = bindex;
16962 +       else {
16963 +               err = -EINVAL;
16964 +               errorfc(fc, "out of bounds, %u", bindex);
16965 +       }
16966 +
16967 +       AuTraceErr(err);
16968 +       return err;
16969 +}
16970 +
16971 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16972 +{
16973 +       int err, token;
16974 +       struct fs_parse_result result;
16975 +       struct au_fsctx_opts *a = fc->fs_private;
16976 +       struct au_opt *opt = a->opt;
16977 +
16978 +       AuDbg("fc %p, param {key %s, string %s}\n",
16979 +             fc, param->key, param->string);
16980 +       err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
16981 +       if (unlikely(err < 0))
16982 +               goto out;
16983 +       token = err;
16984 +       AuDbg("token %d, res{negated %d, uint64 %llu}\n",
16985 +             token, result.negated, result.uint_64);
16986 +
16987 +       err = -EINVAL;
16988 +       a->skipped = 0;
16989 +       switch (token) {
16990 +       case Opt_br:
16991 +               err = au_fsctx_parse_br(fc, param->string);
16992 +               break;
16993 +       case Opt_add:
16994 +               err = au_fsctx_parse_add(fc, param->string);
16995 +               break;
16996 +       case Opt_append:
16997 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
16998 +                                           /*dummy bindex*/1);
16999 +               break;
17000 +       case Opt_prepend:
17001 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17002 +                                           /*bindex*/0);
17003 +               break;
17004 +
17005 +       case Opt_del:
17006 +               err = au_fsctx_parse_del(fc, &opt->del, param);
17007 +               break;
17008 +#if 0 /* reserved for future use */
17009 +       case Opt_idel:
17010 +               if (!a->sb) {
17011 +                       err = 0;
17012 +                       a->skipped = 1;
17013 +                       break;
17014 +               }
17015 +               del->pathname = "(indexed)";
17016 +               err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17017 +               break;
17018 +#endif
17019 +
17020 +       case Opt_mod:
17021 +               err = au_fsctx_parse_mod(fc, &opt->mod, param);
17022 +               break;
17023 +#ifdef IMOD /* reserved for future use */
17024 +       case Opt_imod:
17025 +               if (!a->sb) {
17026 +                       err = 0;
17027 +                       a->skipped = 1;
17028 +                       break;
17029 +               }
17030 +               u.mod->path = "(indexed)";
17031 +               err = au_opts_parse_imod(fc, &opt->mod, param->string);
17032 +               break;
17033 +#endif
17034 +
17035 +       case Opt_xino:
17036 +               err = au_fsctx_parse_xino(fc, &opt->xino, param);
17037 +               break;
17038 +       case Opt_trunc_xino_path:
17039 +               if (!a->sb) {
17040 +                       errorfc(fc, "no such branch %s", param->string);
17041 +                       break;
17042 +               }
17043 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17044 +                                                     param->string);
17045 +               break;
17046 +#if 0
17047 +       case Opt_trunc_xino_v:
17048 +               if (!a->sb) {
17049 +                       err = 0;
17050 +                       a->skipped = 1;
17051 +                       break;
17052 +               }
17053 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17054 +                                                     param->string);
17055 +               break;
17056 +#endif
17057 +       case Opt_itrunc_xino:
17058 +               if (!a->sb) {
17059 +                       errorfc(fc, "out of bounds %s", param->string);
17060 +                       break;
17061 +               }
17062 +               err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17063 +                                                result.int_32);
17064 +               break;
17065 +
17066 +       case Opt_dirwh:
17067 +               err = 0;
17068 +               opt->dirwh = result.int_32;
17069 +               break;
17070 +
17071 +       case Opt_rdcache:
17072 +               if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17073 +                       errorfc(fc, "rdcache must be smaller than %d",
17074 +                               AUFS_RDCACHE_MAX);
17075 +                       break;
17076 +               }
17077 +               err = 0;
17078 +               opt->rdcache = result.int_32;
17079 +               break;
17080 +
17081 +       case Opt_rdblk:
17082 +               err = 0;
17083 +               opt->rdblk = AUFS_RDBLK_DEF;
17084 +               if (!strcmp(param->string, "def"))
17085 +                       break;
17086 +
17087 +               err = kstrtoint(param->string, 0, &result.int_32);
17088 +               if (unlikely(err)) {
17089 +                       errorfc(fc, "bad value in %s", param->key);
17090 +                       break;
17091 +               }
17092 +               err = -EINVAL;
17093 +               if (unlikely(result.int_32 < 0
17094 +                            || result.int_32 > KMALLOC_MAX_SIZE)) {
17095 +                       errorfc(fc, "bad value in %s", param->key);
17096 +                       break;
17097 +               }
17098 +               if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17099 +                       errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17100 +                       break;
17101 +               }
17102 +               err = 0;
17103 +               opt->rdblk = result.int_32;
17104 +               break;
17105 +
17106 +       case Opt_rdhash:
17107 +               err = 0;
17108 +               opt->rdhash = AUFS_RDHASH_DEF;
17109 +               if (!strcmp(param->string, "def"))
17110 +                       break;
17111 +
17112 +               err = kstrtoint(param->string, 0, &result.int_32);
17113 +               if (unlikely(err)) {
17114 +                       errorfc(fc, "bad value in %s", param->key);
17115 +                       break;
17116 +               }
17117 +               /* how about zero? */
17118 +               if (result.int_32 < 0
17119 +                   || result.int_32 * sizeof(struct hlist_head)
17120 +                   > KMALLOC_MAX_SIZE) {
17121 +                       err = -EINVAL;
17122 +                       errorfc(fc, "bad integer in %s", param->key);
17123 +                       break;
17124 +               }
17125 +               opt->rdhash = result.int_32;
17126 +               break;
17127 +
17128 +       case Opt_diropq:
17129 +               /*
17130 +                * As other options, fs/aufs/opts.c can handle these strings by
17131 +                * match_token().  But "diropq=" is deprecated now and will
17132 +                * never have other value.  So simple strcmp() is enough here.
17133 +                */
17134 +               if (!strcmp(param->string, "a") ||
17135 +                   !strcmp(param->string, "always")) {
17136 +                       err = 0;
17137 +                       opt->type = Opt_diropq_a;
17138 +               } else if (!strcmp(param->string, "w") ||
17139 +                          !strcmp(param->string, "whiteouted")) {
17140 +                       err = 0;
17141 +                       opt->type = Opt_diropq_w;
17142 +               } else
17143 +                       errorfc(fc, "unknown value %s", param->string);
17144 +               break;
17145 +
17146 +       case Opt_udba:
17147 +               opt->udba = au_udba_val(param->string);
17148 +               if (opt->udba >= 0)
17149 +                       err = 0;
17150 +               else
17151 +                       errorf(fc, "wrong value, %s", param->string);
17152 +               break;
17153 +
17154 +       case Opt_wbr_create:
17155 +               opt->wbr_create.wbr_create
17156 +                       = au_wbr_create_val(param->string, &opt->wbr_create);
17157 +               if (opt->wbr_create.wbr_create >= 0)
17158 +                       err = 0;
17159 +               else
17160 +                       errorf(fc, "wrong value, %s", param->key);
17161 +               break;
17162 +
17163 +       case Opt_wbr_copyup:
17164 +               opt->wbr_copyup = au_wbr_copyup_val(param->string);
17165 +               if (opt->wbr_copyup >= 0)
17166 +                       err = 0;
17167 +               else
17168 +                       errorfc(fc, "wrong value, %s", param->key);
17169 +               break;
17170 +
17171 +       case Opt_fhsm_sec:
17172 +               if (unlikely(result.int_32 < 0)) {
17173 +                       errorfc(fc, "bad integer in %s\n", param->key);
17174 +                       break;
17175 +               }
17176 +               err = 0;
17177 +               if (sysaufs_brs)
17178 +                       opt->fhsm_second = result.int_32;
17179 +               else
17180 +                       warnfc(fc, "ignored %s %s", param->key, param->string);
17181 +               break;
17182 +
17183 +       /* simple true/false flag */
17184 +#define au_fsctx_TF(name)                              \
17185 +               case Opt_##name:                        \
17186 +                       err = 0;                        \
17187 +                       opt->tf = !result.negated;      \
17188 +                       break
17189 +       au_fsctx_TF(trunc_xino);
17190 +       au_fsctx_TF(trunc_xib);
17191 +       au_fsctx_TF(dirperm1);
17192 +       au_fsctx_TF(plink);
17193 +       au_fsctx_TF(shwh);
17194 +       au_fsctx_TF(dio);
17195 +       au_fsctx_TF(warn_perm);
17196 +       au_fsctx_TF(verbose);
17197 +       au_fsctx_TF(sum);
17198 +       au_fsctx_TF(dirren);
17199 +       au_fsctx_TF(acl);
17200 +#undef au_fsctx_TF
17201 +
17202 +       case Opt_noverbose:
17203 +               err = 0;
17204 +               opt->type = Opt_verbose;
17205 +               opt->tf = false;
17206 +               break;
17207 +
17208 +       case Opt_noxino:
17209 +               fallthrough;
17210 +       case Opt_list_plink:
17211 +               fallthrough;
17212 +       case Opt_wsum:
17213 +               err = 0;
17214 +               break;
17215 +
17216 +       case Opt_ignore:
17217 +               warnfc(fc, "ignored %s", param->key);
17218 +               fallthrough;
17219 +       case Opt_ignore_silent:
17220 +               a->skipped = 1;
17221 +               err = 0;
17222 +               break;
17223 +       default:
17224 +               a->skipped = 1;
17225 +               err = -ENOPARAM;
17226 +               break;
17227 +       }
17228 +       if (unlikely(err))
17229 +               goto out;
17230 +       if (a->skipped)
17231 +               goto out;
17232 +
17233 +       switch (token) {
17234 +       case Opt_br:
17235 +               fallthrough;
17236 +       case Opt_noverbose:
17237 +               fallthrough;
17238 +       case Opt_diropq:
17239 +               break;
17240 +       default:
17241 +               opt->type = token;
17242 +               break;
17243 +       }
17244 +       opt++;
17245 +       if (unlikely(opt > a->opt_tail)) {
17246 +               err = -E2BIG;
17247 +               opt--;
17248 +       }
17249 +       opt->type = Opt_tail;
17250 +       a->opt = opt;
17251 +
17252 +out:
17253 +       return err;
17254 +}
17255 +
17256 +/*
17257 + * these options accept both 'name=val' and 'name:val' form.
17258 + * some accept optional '=' in its value.
17259 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17260 + */
17261 +static inline unsigned int is_colonopt(char *str)
17262 +{
17263 +#define do_test(name)                                  \
17264 +       if (!strncmp(str, name ":", sizeof(name)))      \
17265 +               return sizeof(name) - 1
17266 +       do_test("br");
17267 +       do_test("add");
17268 +       do_test("ins");
17269 +       do_test("append");
17270 +       do_test("prepend");
17271 +       do_test("del");
17272 +       /* do_test("idel"); */
17273 +       do_test("mod");
17274 +       /* do_test("imod"); */
17275 +#undef do_test
17276 +
17277 +       return 0;
17278 +}
17279 +
17280 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17281 +{
17282 +       int err;
17283 +       unsigned int u;
17284 +       char *str;
17285 +       struct au_fsctx_opts *a = fc->fs_private;
17286 +
17287 +       str = data;
17288 +       AuDbg("str %s\n", str);
17289 +       while (str) {
17290 +               u = is_colonopt(str);
17291 +               if (u)
17292 +                       str[u] = '=';
17293 +               str = strchr(str, ',');
17294 +               if (!str)
17295 +                       break;
17296 +               str++;
17297 +       }
17298 +       str = data;
17299 +       AuDbg("str %s\n", str);
17300 +
17301 +       err = generic_parse_monolithic(fc, str);
17302 +       AuTraceErr(err);
17303 +       au_fsctx_dump(&a->opts);
17304 +
17305 +       return err;
17306 +}
17307 +
17308 +/* ---------------------------------------------------------------------- */
17309 +
17310 +static void au_fsctx_opts_free(struct au_opts *opts)
17311 +{
17312 +       struct au_opt *opt;
17313 +
17314 +       opt = opts->opt;
17315 +       while (opt->type != Opt_tail) {
17316 +               switch (opt->type) {
17317 +               case Opt_add:
17318 +                       fallthrough;
17319 +               case Opt_append:
17320 +                       fallthrough;
17321 +               case Opt_prepend:
17322 +                       kfree(opt->add.pathname);
17323 +                       path_put(&opt->add.path);
17324 +                       break;
17325 +               case Opt_del:
17326 +                       kfree(opt->del.pathname);
17327 +                       fallthrough;
17328 +               case Opt_idel:
17329 +                       path_put(&opt->del.h_path);
17330 +                       break;
17331 +               case Opt_mod:
17332 +                       kfree(opt->mod.path);
17333 +                       fallthrough;
17334 +               case Opt_imod:
17335 +                       dput(opt->mod.h_root);
17336 +                       break;
17337 +               case Opt_xino:
17338 +                       kfree(opt->xino.path);
17339 +                       fput(opt->xino.file);
17340 +                       break;
17341 +               }
17342 +               opt++;
17343 +       }
17344 +}
17345 +
17346 +static void au_fsctx_free(struct fs_context *fc)
17347 +{
17348 +       struct au_fsctx_opts *a = fc->fs_private;
17349 +
17350 +       /* fs_type=%p, root=%pD */
17351 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17352 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17353 +
17354 +       kobject_put(&a->sbinfo->si_kobj);
17355 +       au_fsctx_opts_free(&a->opts);
17356 +       free_page((unsigned long)a->opts.opt);
17357 +       au_kfree_rcu(a);
17358 +}
17359 +
17360 +static const struct fs_context_operations au_fsctx_ops = {
17361 +       .free                   = au_fsctx_free,
17362 +       .parse_param            = au_fsctx_parse_param,
17363 +       .parse_monolithic       = au_fsctx_parse_monolithic,
17364 +       .get_tree               = au_fsctx_get_tree,
17365 +       .reconfigure            = au_fsctx_reconfigure
17366 +       /*
17367 +        * nfs4 requires ->dup()? No.
17368 +        * I don't know what is this ->dup() for.
17369 +        */
17370 +};
17371 +
17372 +int aufs_fsctx_init(struct fs_context *fc)
17373 +{
17374 +       int err;
17375 +       struct au_fsctx_opts *a;
17376 +
17377 +       /* fs_type=%p, root=%pD */
17378 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17379 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17380 +
17381 +       /* they will be freed by au_fsctx_free() */
17382 +       err = -ENOMEM;
17383 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17384 +       if (unlikely(!a))
17385 +               goto out;
17386 +       a->bindex = 0;
17387 +       a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17388 +       if (unlikely(!a->opts.opt))
17389 +               goto out_a;
17390 +       a->opt = a->opts.opt;
17391 +       a->opt->type = Opt_tail;
17392 +       a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17393 +       a->opt_tail = a->opt + a->opts.max_opt - 1;
17394 +       a->opts.sb_flags = fc->sb_flags;
17395 +
17396 +       a->sb = NULL;
17397 +       if (fc->root) {
17398 +               AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17399 +               a->opts.flags = AuOpts_REMOUNT;
17400 +               a->sb = fc->root->d_sb;
17401 +               a->sbinfo = au_sbi(a->sb);
17402 +               kobject_get(&a->sbinfo->si_kobj);
17403 +       } else {
17404 +               a->sbinfo = au_si_alloc(a->sb);
17405 +               AuDebugOn(!a->sbinfo);
17406 +               err = PTR_ERR(a->sbinfo);
17407 +               if (IS_ERR(a->sbinfo))
17408 +                       goto out_opt;
17409 +               au_rw_write_unlock(&a->sbinfo->si_rwsem);
17410 +       }
17411 +
17412 +       err = 0;
17413 +       fc->fs_private = a;
17414 +       fc->ops = &au_fsctx_ops;
17415 +       goto out; /* success */
17416 +
17417 +out_opt:
17418 +       free_page((unsigned long)a->opts.opt);
17419 +out_a:
17420 +       au_kfree_rcu(a);
17421 +out:
17422 +       AuTraceErr(err);
17423 +       return err;
17424 +}
17425 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
17426 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
17427 +++ linux/fs/aufs/fstype.h      2022-11-05 23:02:18.965889284 +0100
17428 @@ -0,0 +1,401 @@
17429 +/* SPDX-License-Identifier: GPL-2.0 */
17430 +/*
17431 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17432 + *
17433 + * This program is free software; you can redistribute it and/or modify
17434 + * it under the terms of the GNU General Public License as published by
17435 + * the Free Software Foundation; either version 2 of the License, or
17436 + * (at your option) any later version.
17437 + *
17438 + * This program is distributed in the hope that it will be useful,
17439 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17440 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17441 + * GNU General Public License for more details.
17442 + *
17443 + * You should have received a copy of the GNU General Public License
17444 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17445 + */
17446 +
17447 +/*
17448 + * judging filesystem type
17449 + */
17450 +
17451 +#ifndef __AUFS_FSTYPE_H__
17452 +#define __AUFS_FSTYPE_H__
17453 +
17454 +#ifdef __KERNEL__
17455 +
17456 +#include <linux/fs.h>
17457 +#include <linux/magic.h>
17458 +#include <linux/nfs_fs.h>
17459 +#include <linux/romfs_fs.h>
17460 +
17461 +static inline int au_test_aufs(struct super_block *sb)
17462 +{
17463 +       return sb->s_magic == AUFS_SUPER_MAGIC;
17464 +}
17465 +
17466 +static inline const char *au_sbtype(struct super_block *sb)
17467 +{
17468 +       return sb->s_type->name;
17469 +}
17470 +
17471 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17472 +{
17473 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17474 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
17475 +#else
17476 +       return 0;
17477 +#endif
17478 +}
17479 +
17480 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17481 +{
17482 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17483 +       return sb->s_magic == ROMFS_MAGIC;
17484 +#else
17485 +       return 0;
17486 +#endif
17487 +}
17488 +
17489 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17490 +{
17491 +#if IS_ENABLED(CONFIG_CRAMFS)
17492 +       return sb->s_magic == CRAMFS_MAGIC;
17493 +#endif
17494 +       return 0;
17495 +}
17496 +
17497 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17498 +{
17499 +#if IS_ENABLED(CONFIG_NFS_FS)
17500 +       return sb->s_magic == NFS_SUPER_MAGIC;
17501 +#else
17502 +       return 0;
17503 +#endif
17504 +}
17505 +
17506 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17507 +{
17508 +#if IS_ENABLED(CONFIG_FUSE_FS)
17509 +       return sb->s_magic == FUSE_SUPER_MAGIC;
17510 +#else
17511 +       return 0;
17512 +#endif
17513 +}
17514 +
17515 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17516 +{
17517 +#if IS_ENABLED(CONFIG_XFS_FS)
17518 +       return sb->s_magic == XFS_SB_MAGIC;
17519 +#else
17520 +       return 0;
17521 +#endif
17522 +}
17523 +
17524 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17525 +{
17526 +#ifdef CONFIG_TMPFS
17527 +       return sb->s_magic == TMPFS_MAGIC;
17528 +#else
17529 +       return 0;
17530 +#endif
17531 +}
17532 +
17533 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17534 +{
17535 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17536 +       return !strcmp(au_sbtype(sb), "ecryptfs");
17537 +#else
17538 +       return 0;
17539 +#endif
17540 +}
17541 +
17542 +static inline int au_test_ramfs(struct super_block *sb)
17543 +{
17544 +       return sb->s_magic == RAMFS_MAGIC;
17545 +}
17546 +
17547 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17548 +{
17549 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17550 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
17551 +#else
17552 +       return 0;
17553 +#endif
17554 +}
17555 +
17556 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17557 +{
17558 +#ifdef CONFIG_PROC_FS
17559 +       return sb->s_magic == PROC_SUPER_MAGIC;
17560 +#else
17561 +       return 0;
17562 +#endif
17563 +}
17564 +
17565 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17566 +{
17567 +#ifdef CONFIG_SYSFS
17568 +       return sb->s_magic == SYSFS_MAGIC;
17569 +#else
17570 +       return 0;
17571 +#endif
17572 +}
17573 +
17574 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17575 +{
17576 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17577 +       return sb->s_magic == CONFIGFS_MAGIC;
17578 +#else
17579 +       return 0;
17580 +#endif
17581 +}
17582 +
17583 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17584 +{
17585 +#if IS_ENABLED(CONFIG_MINIX_FS)
17586 +       return sb->s_magic == MINIX3_SUPER_MAGIC
17587 +               || sb->s_magic == MINIX2_SUPER_MAGIC
17588 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
17589 +               || sb->s_magic == MINIX_SUPER_MAGIC
17590 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
17591 +#else
17592 +       return 0;
17593 +#endif
17594 +}
17595 +
17596 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17597 +{
17598 +#if IS_ENABLED(CONFIG_FAT_FS)
17599 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
17600 +#else
17601 +       return 0;
17602 +#endif
17603 +}
17604 +
17605 +static inline int au_test_msdos(struct super_block *sb)
17606 +{
17607 +       return au_test_fat(sb);
17608 +}
17609 +
17610 +static inline int au_test_vfat(struct super_block *sb)
17611 +{
17612 +       return au_test_fat(sb);
17613 +}
17614 +
17615 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17616 +{
17617 +#ifdef CONFIG_SECURITYFS
17618 +       return sb->s_magic == SECURITYFS_MAGIC;
17619 +#else
17620 +       return 0;
17621 +#endif
17622 +}
17623 +
17624 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17625 +{
17626 +#if IS_ENABLED(CONFIG_SQUASHFS)
17627 +       return sb->s_magic == SQUASHFS_MAGIC;
17628 +#else
17629 +       return 0;
17630 +#endif
17631 +}
17632 +
17633 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17634 +{
17635 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17636 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
17637 +#else
17638 +       return 0;
17639 +#endif
17640 +}
17641 +
17642 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17643 +{
17644 +#if IS_ENABLED(CONFIG_XENFS)
17645 +       return sb->s_magic == XENFS_SUPER_MAGIC;
17646 +#else
17647 +       return 0;
17648 +#endif
17649 +}
17650 +
17651 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17652 +{
17653 +#ifdef CONFIG_DEBUG_FS
17654 +       return sb->s_magic == DEBUGFS_MAGIC;
17655 +#else
17656 +       return 0;
17657 +#endif
17658 +}
17659 +
17660 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17661 +{
17662 +#if IS_ENABLED(CONFIG_NILFS)
17663 +       return sb->s_magic == NILFS_SUPER_MAGIC;
17664 +#else
17665 +       return 0;
17666 +#endif
17667 +}
17668 +
17669 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17670 +{
17671 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17672 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17673 +#else
17674 +       return 0;
17675 +#endif
17676 +}
17677 +
17678 +/* ---------------------------------------------------------------------- */
17679 +/*
17680 + * they can't be an aufs branch.
17681 + */
17682 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17683 +{
17684 +       return
17685 +#ifndef CONFIG_AUFS_BR_RAMFS
17686 +               au_test_ramfs(sb) ||
17687 +#endif
17688 +               au_test_procfs(sb)
17689 +               || au_test_sysfs(sb)
17690 +               || au_test_configfs(sb)
17691 +               || au_test_debugfs(sb)
17692 +               || au_test_securityfs(sb)
17693 +               || au_test_xenfs(sb)
17694 +               || au_test_ecryptfs(sb)
17695 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
17696 +               || au_test_aufs(sb); /* will be supported in next version */
17697 +}
17698 +
17699 +static inline int au_test_fs_remote(struct super_block *sb)
17700 +{
17701 +       return !au_test_tmpfs(sb)
17702 +#ifdef CONFIG_AUFS_BR_RAMFS
17703 +               && !au_test_ramfs(sb)
17704 +#endif
17705 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17706 +}
17707 +
17708 +/* ---------------------------------------------------------------------- */
17709 +
17710 +/*
17711 + * Note: these functions (below) are created after reading ->getattr() in all
17712 + * filesystems under linux/fs. it means we have to do so in every update...
17713 + */
17714 +
17715 +/*
17716 + * some filesystems require getattr to refresh the inode attributes before
17717 + * referencing.
17718 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17719 + * and leave the work for d_revalidate()
17720 + */
17721 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17722 +{
17723 +       return au_test_nfs(sb)
17724 +               || au_test_fuse(sb)
17725 +               /* || au_test_btrfs(sb) */      /* untested */
17726 +               ;
17727 +}
17728 +
17729 +/*
17730 + * filesystems which don't maintain i_size or i_blocks.
17731 + */
17732 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17733 +{
17734 +       return au_test_xfs(sb)
17735 +               || au_test_btrfs(sb)
17736 +               || au_test_ubifs(sb)
17737 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
17738 +               /* || au_test_minix(sb) */      /* untested */
17739 +               ;
17740 +}
17741 +
17742 +/*
17743 + * filesystems which don't store the correct value in some of their inode
17744 + * attributes.
17745 + */
17746 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17747 +{
17748 +       return au_test_fs_bad_iattr_size(sb)
17749 +               || au_test_fat(sb)
17750 +               || au_test_msdos(sb)
17751 +               || au_test_vfat(sb);
17752 +}
17753 +
17754 +/* they don't check i_nlink in link(2) */
17755 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17756 +{
17757 +       return au_test_tmpfs(sb)
17758 +#ifdef CONFIG_AUFS_BR_RAMFS
17759 +               || au_test_ramfs(sb)
17760 +#endif
17761 +               || au_test_ubifs(sb)
17762 +               || au_test_hfsplus(sb);
17763 +}
17764 +
17765 +/*
17766 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17767 + */
17768 +static inline int au_test_fs_notime(struct super_block *sb)
17769 +{
17770 +       return au_test_nfs(sb)
17771 +               || au_test_fuse(sb)
17772 +               || au_test_ubifs(sb)
17773 +               ;
17774 +}
17775 +
17776 +/* temporary support for i#1 in cramfs */
17777 +static inline int au_test_fs_unique_ino(struct inode *inode)
17778 +{
17779 +       if (au_test_cramfs(inode->i_sb))
17780 +               return inode->i_ino != 1;
17781 +       return 1;
17782 +}
17783 +
17784 +/* ---------------------------------------------------------------------- */
17785 +
17786 +/*
17787 + * the filesystem where the xino files placed must support i/o after unlink and
17788 + * maintain i_size and i_blocks.
17789 + */
17790 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17791 +{
17792 +       return au_test_fs_remote(sb)
17793 +               || au_test_fs_bad_iattr_size(sb)
17794 +               /* don't want unnecessary work for xino */
17795 +               || au_test_aufs(sb)
17796 +               || au_test_ecryptfs(sb)
17797 +               || au_test_nilfs(sb);
17798 +}
17799 +
17800 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17801 +{
17802 +       return au_test_tmpfs(sb)
17803 +               || au_test_ramfs(sb);
17804 +}
17805 +
17806 +/*
17807 + * test if the @sb is real-readonly.
17808 + */
17809 +static inline int au_test_fs_rr(struct super_block *sb)
17810 +{
17811 +       return au_test_squashfs(sb)
17812 +               || au_test_iso9660(sb)
17813 +               || au_test_cramfs(sb)
17814 +               || au_test_romfs(sb);
17815 +}
17816 +
17817 +/*
17818 + * test if the @inode is nfs with 'noacl' option
17819 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17820 + */
17821 +static inline int au_test_nfs_noacl(struct inode *inode)
17822 +{
17823 +       return au_test_nfs(inode->i_sb)
17824 +               /* && IS_POSIXACL(inode) */
17825 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
17826 +}
17827 +
17828 +#endif /* __KERNEL__ */
17829 +#endif /* __AUFS_FSTYPE_H__ */
17830 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
17831 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
17832 +++ linux/fs/aufs/hbl.h 2022-11-05 23:02:18.965889284 +0100
17833 @@ -0,0 +1,65 @@
17834 +/* SPDX-License-Identifier: GPL-2.0 */
17835 +/*
17836 + * Copyright (C) 2017-2022 Junjiro R. Okajima
17837 + *
17838 + * This program is free software; you can redistribute it and/or modify
17839 + * it under the terms of the GNU General Public License as published by
17840 + * the Free Software Foundation; either version 2 of the License, or
17841 + * (at your option) any later version.
17842 + *
17843 + * This program is distributed in the hope that it will be useful,
17844 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17845 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17846 + * GNU General Public License for more details.
17847 + *
17848 + * You should have received a copy of the GNU General Public License
17849 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17850 + */
17851 +
17852 +/*
17853 + * helpers for hlist_bl.h
17854 + */
17855 +
17856 +#ifndef __AUFS_HBL_H__
17857 +#define __AUFS_HBL_H__
17858 +
17859 +#ifdef __KERNEL__
17860 +
17861 +#include <linux/list_bl.h>
17862 +
17863 +static inline void au_hbl_add(struct hlist_bl_node *node,
17864 +                             struct hlist_bl_head *hbl)
17865 +{
17866 +       hlist_bl_lock(hbl);
17867 +       hlist_bl_add_head(node, hbl);
17868 +       hlist_bl_unlock(hbl);
17869 +}
17870 +
17871 +static inline void au_hbl_del(struct hlist_bl_node *node,
17872 +                             struct hlist_bl_head *hbl)
17873 +{
17874 +       hlist_bl_lock(hbl);
17875 +       hlist_bl_del(node);
17876 +       hlist_bl_unlock(hbl);
17877 +}
17878 +
17879 +#define au_hbl_for_each(pos, head)                                     \
17880 +       for (pos = hlist_bl_first(head);                                \
17881 +            pos;                                                       \
17882 +            pos = pos->next)
17883 +
17884 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17885 +{
17886 +       unsigned long cnt;
17887 +       struct hlist_bl_node *pos;
17888 +
17889 +       cnt = 0;
17890 +       hlist_bl_lock(hbl);
17891 +       au_hbl_for_each(pos, hbl)
17892 +               cnt++;
17893 +       hlist_bl_unlock(hbl);
17894 +       return cnt;
17895 +}
17896 +
17897 +#endif /* __KERNEL__ */
17898 +#endif /* __AUFS_HBL_H__ */
17899 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
17900 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
17901 +++ linux/fs/aufs/hfsnotify.c   2022-11-05 23:02:18.965889284 +0100
17902 @@ -0,0 +1,290 @@
17903 +// SPDX-License-Identifier: GPL-2.0
17904 +/*
17905 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17906 + *
17907 + * This program is free software; you can redistribute it and/or modify
17908 + * it under the terms of the GNU General Public License as published by
17909 + * the Free Software Foundation; either version 2 of the License, or
17910 + * (at your option) any later version.
17911 + *
17912 + * This program is distributed in the hope that it will be useful,
17913 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17914 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17915 + * GNU General Public License for more details.
17916 + *
17917 + * You should have received a copy of the GNU General Public License
17918 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17919 + */
17920 +
17921 +/*
17922 + * fsnotify for the lower directories
17923 + */
17924 +
17925 +#include "aufs.h"
17926 +
17927 +/* FS_IN_IGNORED is unnecessary */
17928 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17929 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
17930 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17931 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17932 +
17933 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17934 +{
17935 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17936 +                                            hn_mark);
17937 +       /* AuDbg("here\n"); */
17938 +       au_cache_free_hnotify(hn);
17939 +       smp_mb__before_atomic(); /* for atomic64_dec */
17940 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
17941 +               wake_up(&au_hfsn_wq);
17942 +}
17943 +
17944 +static int au_hfsn_alloc(struct au_hinode *hinode)
17945 +{
17946 +       int err;
17947 +       struct au_hnotify *hn;
17948 +       struct super_block *sb;
17949 +       struct au_branch *br;
17950 +       struct fsnotify_mark *mark;
17951 +       aufs_bindex_t bindex;
17952 +
17953 +       hn = hinode->hi_notify;
17954 +       sb = hn->hn_aufs_inode->i_sb;
17955 +       bindex = au_br_index(sb, hinode->hi_id);
17956 +       br = au_sbr(sb, bindex);
17957 +       AuDebugOn(!br->br_hfsn);
17958 +
17959 +       mark = &hn->hn_mark;
17960 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17961 +       mark->mask = AuHfsnMask;
17962 +       /*
17963 +        * by udba rename or rmdir, aufs assign a new inode to the known
17964 +        * h_inode, so specify 1 to allow dups.
17965 +        */
17966 +       lockdep_off();
17967 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
17968 +       lockdep_on();
17969 +
17970 +       return err;
17971 +}
17972 +
17973 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
17974 +{
17975 +       struct fsnotify_mark *mark;
17976 +       unsigned long long ull;
17977 +       struct fsnotify_group *group;
17978 +
17979 +       ull = atomic64_inc_return(&au_hfsn_ifree);
17980 +       BUG_ON(!ull);
17981 +
17982 +       mark = &hn->hn_mark;
17983 +       spin_lock(&mark->lock);
17984 +       group = mark->group;
17985 +       fsnotify_get_group(group);
17986 +       spin_unlock(&mark->lock);
17987 +       lockdep_off();
17988 +       fsnotify_destroy_mark(mark, group);
17989 +       fsnotify_put_mark(mark);
17990 +       fsnotify_put_group(group);
17991 +       lockdep_on();
17992 +
17993 +       /* free hn by myself */
17994 +       return 0;
17995 +}
17996 +
17997 +/* ---------------------------------------------------------------------- */
17998 +
17999 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
18000 +{
18001 +       struct fsnotify_mark *mark;
18002 +
18003 +       mark = &hinode->hi_notify->hn_mark;
18004 +       spin_lock(&mark->lock);
18005 +       if (do_set) {
18006 +               AuDebugOn(mark->mask & AuHfsnMask);
18007 +               mark->mask |= AuHfsnMask;
18008 +       } else {
18009 +               AuDebugOn(!(mark->mask & AuHfsnMask));
18010 +               mark->mask &= ~AuHfsnMask;
18011 +       }
18012 +       spin_unlock(&mark->lock);
18013 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
18014 +}
18015 +
18016 +/* ---------------------------------------------------------------------- */
18017 +
18018 +/* #define AuDbgHnotify */
18019 +#ifdef AuDbgHnotify
18020 +static char *au_hfsn_name(u32 mask)
18021 +{
18022 +#ifdef CONFIG_AUFS_DEBUG
18023 +#define test_ret(flag)                         \
18024 +       do {                                    \
18025 +               if (mask & flag)                \
18026 +                       return #flag;           \
18027 +       } while (0)
18028 +       test_ret(FS_ACCESS);
18029 +       test_ret(FS_MODIFY);
18030 +       test_ret(FS_ATTRIB);
18031 +       test_ret(FS_CLOSE_WRITE);
18032 +       test_ret(FS_CLOSE_NOWRITE);
18033 +       test_ret(FS_OPEN);
18034 +       test_ret(FS_MOVED_FROM);
18035 +       test_ret(FS_MOVED_TO);
18036 +       test_ret(FS_CREATE);
18037 +       test_ret(FS_DELETE);
18038 +       test_ret(FS_DELETE_SELF);
18039 +       test_ret(FS_MOVE_SELF);
18040 +       test_ret(FS_UNMOUNT);
18041 +       test_ret(FS_Q_OVERFLOW);
18042 +       test_ret(FS_IN_IGNORED);
18043 +       test_ret(FS_ISDIR);
18044 +       test_ret(FS_IN_ONESHOT);
18045 +       test_ret(FS_EVENT_ON_CHILD);
18046 +       return "";
18047 +#undef test_ret
18048 +#else
18049 +       return "??";
18050 +#endif
18051 +}
18052 +#endif
18053 +
18054 +/* ---------------------------------------------------------------------- */
18055 +
18056 +static void au_hfsn_free_group(struct fsnotify_group *group)
18057 +{
18058 +       struct au_br_hfsnotify *hfsn = group->private;
18059 +
18060 +       /* AuDbg("here\n"); */
18061 +       au_kfree_try_rcu(hfsn);
18062 +}
18063 +
18064 +static int au_hfsn_handle_event(struct fsnotify_group *group,
18065 +                               u32 mask, const void *data, int data_type,
18066 +                               struct inode *dir,
18067 +                               const struct qstr *file_name, u32 cookie,
18068 +                               struct fsnotify_iter_info *iter_info)
18069 +{
18070 +       int err;
18071 +       struct au_hnotify *hnotify;
18072 +       struct inode *h_dir, *h_inode;
18073 +       struct fsnotify_mark *inode_mark;
18074 +
18075 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18076 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
18077 +
18078 +       err = 0;
18079 +       /* if FS_UNMOUNT happens, there must be another bug */
18080 +       AuDebugOn(mask & FS_UNMOUNT);
18081 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
18082 +               goto out;
18083 +
18084 +       h_dir = dir;
18085 +       h_inode = NULL;
18086 +#ifdef AuDbgHnotify
18087 +       au_debug_on();
18088 +       if (1 || file_name.len != sizeof(AUFS_XINO_FNAME) - 1
18089 +           || strncmp(file_name.name, AUFS_XINO_FNAME, file_name.len)) {
18090 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18091 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
18092 +                     AuLNPair(file_name), h_inode ? h_inode->i_ino : 0);
18093 +               /* WARN_ON(1); */
18094 +       }
18095 +       au_debug_off();
18096 +#endif
18097 +
18098 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
18099 +       AuDebugOn(!inode_mark);
18100 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
18101 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
18102 +
18103 +out:
18104 +       return err;
18105 +}
18106 +
18107 +static struct fsnotify_ops au_hfsn_ops = {
18108 +       .handle_event           = au_hfsn_handle_event,
18109 +       .free_group_priv        = au_hfsn_free_group,
18110 +       .free_mark              = au_hfsn_free_mark
18111 +};
18112 +
18113 +/* ---------------------------------------------------------------------- */
18114 +
18115 +static void au_hfsn_fin_br(struct au_branch *br)
18116 +{
18117 +       struct au_br_hfsnotify *hfsn;
18118 +
18119 +       hfsn = br->br_hfsn;
18120 +       if (hfsn) {
18121 +               lockdep_off();
18122 +               fsnotify_put_group(hfsn->hfsn_group);
18123 +               lockdep_on();
18124 +       }
18125 +}
18126 +
18127 +static int au_hfsn_init_br(struct au_branch *br, int perm)
18128 +{
18129 +       int err;
18130 +       struct fsnotify_group *group;
18131 +       struct au_br_hfsnotify *hfsn;
18132 +
18133 +       err = 0;
18134 +       br->br_hfsn = NULL;
18135 +       if (!au_br_hnotifyable(perm))
18136 +               goto out;
18137 +
18138 +       err = -ENOMEM;
18139 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18140 +       if (unlikely(!hfsn))
18141 +               goto out;
18142 +
18143 +       err = 0;
18144 +       group = fsnotify_alloc_group(&au_hfsn_ops,
18145 +                                    /*flags - not for userspace*/0);
18146 +       if (IS_ERR(group)) {
18147 +               err = PTR_ERR(group);
18148 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
18149 +               goto out_hfsn;
18150 +       }
18151 +
18152 +       group->private = hfsn;
18153 +       hfsn->hfsn_group = group;
18154 +       br->br_hfsn = hfsn;
18155 +       goto out; /* success */
18156 +
18157 +out_hfsn:
18158 +       au_kfree_try_rcu(hfsn);
18159 +out:
18160 +       return err;
18161 +}
18162 +
18163 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18164 +{
18165 +       int err;
18166 +
18167 +       err = 0;
18168 +       if (!br->br_hfsn)
18169 +               err = au_hfsn_init_br(br, perm);
18170 +
18171 +       return err;
18172 +}
18173 +
18174 +/* ---------------------------------------------------------------------- */
18175 +
18176 +static void au_hfsn_fin(void)
18177 +{
18178 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18179 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18180 +}
18181 +
18182 +const struct au_hnotify_op au_hnotify_op = {
18183 +       .ctl            = au_hfsn_ctl,
18184 +       .alloc          = au_hfsn_alloc,
18185 +       .free           = au_hfsn_free,
18186 +
18187 +       .fin            = au_hfsn_fin,
18188 +
18189 +       .reset_br       = au_hfsn_reset_br,
18190 +       .fin_br         = au_hfsn_fin_br,
18191 +       .init_br        = au_hfsn_init_br
18192 +};
18193 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
18194 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
18195 +++ linux/fs/aufs/hfsplus.c     2022-11-05 23:02:18.965889284 +0100
18196 @@ -0,0 +1,60 @@
18197 +// SPDX-License-Identifier: GPL-2.0
18198 +/*
18199 + * Copyright (C) 2010-2022 Junjiro R. Okajima
18200 + *
18201 + * This program is free software; you can redistribute it and/or modify
18202 + * it under the terms of the GNU General Public License as published by
18203 + * the Free Software Foundation; either version 2 of the License, or
18204 + * (at your option) any later version.
18205 + *
18206 + * This program is distributed in the hope that it will be useful,
18207 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18208 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18209 + * GNU General Public License for more details.
18210 + *
18211 + * You should have received a copy of the GNU General Public License
18212 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18213 + */
18214 +
18215 +/*
18216 + * special support for filesystems which acquires an inode mutex
18217 + * at final closing a file, eg, hfsplus.
18218 + *
18219 + * This trick is very simple and stupid, just to open the file before really
18220 + * necessary open to tell hfsplus that this is not the final closing.
18221 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
18222 + * and au_h_open_post() after releasing it.
18223 + */
18224 +
18225 +#include "aufs.h"
18226 +
18227 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18228 +                          int force_wr)
18229 +{
18230 +       struct file *h_file;
18231 +       struct dentry *h_dentry;
18232 +
18233 +       h_dentry = au_h_dptr(dentry, bindex);
18234 +       AuDebugOn(!h_dentry);
18235 +       AuDebugOn(d_is_negative(h_dentry));
18236 +
18237 +       h_file = NULL;
18238 +       if (au_test_hfsplus(h_dentry->d_sb)
18239 +           && d_is_reg(h_dentry))
18240 +               h_file = au_h_open(dentry, bindex,
18241 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
18242 +                                  /*file*/NULL, force_wr);
18243 +       return h_file;
18244 +}
18245 +
18246 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18247 +                   struct file *h_file)
18248 +{
18249 +       struct au_branch *br;
18250 +
18251 +       if (h_file) {
18252 +               fput(h_file);
18253 +               br = au_sbr(dentry->d_sb, bindex);
18254 +               au_lcnt_dec(&br->br_nfiles);
18255 +       }
18256 +}
18257 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
18258 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
18259 +++ linux/fs/aufs/hnotify.c     2022-11-05 23:02:18.965889284 +0100
18260 @@ -0,0 +1,715 @@
18261 +// SPDX-License-Identifier: GPL-2.0
18262 +/*
18263 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18264 + *
18265 + * This program is free software; you can redistribute it and/or modify
18266 + * it under the terms of the GNU General Public License as published by
18267 + * the Free Software Foundation; either version 2 of the License, or
18268 + * (at your option) any later version.
18269 + *
18270 + * This program is distributed in the hope that it will be useful,
18271 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18272 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18273 + * GNU General Public License for more details.
18274 + *
18275 + * You should have received a copy of the GNU General Public License
18276 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18277 + */
18278 +
18279 +/*
18280 + * abstraction to notify the direct changes on lower directories
18281 + */
18282 +
18283 +/* #include <linux/iversion.h> */
18284 +#include "aufs.h"
18285 +
18286 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18287 +{
18288 +       int err;
18289 +       struct au_hnotify *hn;
18290 +
18291 +       err = -ENOMEM;
18292 +       hn = au_cache_alloc_hnotify();
18293 +       if (hn) {
18294 +               hn->hn_aufs_inode = inode;
18295 +               hinode->hi_notify = hn;
18296 +               err = au_hnotify_op.alloc(hinode);
18297 +               AuTraceErr(err);
18298 +               if (unlikely(err)) {
18299 +                       hinode->hi_notify = NULL;
18300 +                       au_cache_free_hnotify(hn);
18301 +                       /*
18302 +                        * The upper dir was removed by udba, but the same named
18303 +                        * dir left. In this case, aufs assigns a new inode
18304 +                        * number and set the monitor again.
18305 +                        * For the lower dir, the old monitor is still left.
18306 +                        */
18307 +                       if (err == -EEXIST)
18308 +                               err = 0;
18309 +               }
18310 +       }
18311 +
18312 +       AuTraceErr(err);
18313 +       return err;
18314 +}
18315 +
18316 +void au_hn_free(struct au_hinode *hinode)
18317 +{
18318 +       struct au_hnotify *hn;
18319 +
18320 +       hn = hinode->hi_notify;
18321 +       if (hn) {
18322 +               hinode->hi_notify = NULL;
18323 +               if (au_hnotify_op.free(hinode, hn))
18324 +                       au_cache_free_hnotify(hn);
18325 +       }
18326 +}
18327 +
18328 +/* ---------------------------------------------------------------------- */
18329 +
18330 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18331 +{
18332 +       if (hinode->hi_notify)
18333 +               au_hnotify_op.ctl(hinode, do_set);
18334 +}
18335 +
18336 +void au_hn_reset(struct inode *inode, unsigned int flags)
18337 +{
18338 +       aufs_bindex_t bindex, bbot;
18339 +       struct inode *hi;
18340 +       struct dentry *iwhdentry;
18341 +
18342 +       bbot = au_ibbot(inode);
18343 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18344 +               hi = au_h_iptr(inode, bindex);
18345 +               if (!hi)
18346 +                       continue;
18347 +
18348 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18349 +               iwhdentry = au_hi_wh(inode, bindex);
18350 +               if (iwhdentry)
18351 +                       dget(iwhdentry);
18352 +               au_igrab(hi);
18353 +               au_set_h_iptr(inode, bindex, NULL, 0);
18354 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
18355 +                             flags & ~AuHi_XINO);
18356 +               iput(hi);
18357 +               dput(iwhdentry);
18358 +               /* inode_unlock(hi); */
18359 +       }
18360 +}
18361 +
18362 +/* ---------------------------------------------------------------------- */
18363 +
18364 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18365 +{
18366 +       int err;
18367 +       aufs_bindex_t bindex, bbot, bfound, btop;
18368 +       struct inode *h_i;
18369 +
18370 +       err = 0;
18371 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18372 +               pr_warn("branch root dir was changed\n");
18373 +               goto out;
18374 +       }
18375 +
18376 +       bfound = -1;
18377 +       bbot = au_ibbot(inode);
18378 +       btop = au_ibtop(inode);
18379 +#if 0 /* reserved for future use */
18380 +       if (bindex == bbot) {
18381 +               /* keep this ino in rename case */
18382 +               goto out;
18383 +       }
18384 +#endif
18385 +       for (bindex = btop; bindex <= bbot; bindex++)
18386 +               if (au_h_iptr(inode, bindex) == h_inode) {
18387 +                       bfound = bindex;
18388 +                       break;
18389 +               }
18390 +       if (bfound < 0)
18391 +               goto out;
18392 +
18393 +       for (bindex = btop; bindex <= bbot; bindex++) {
18394 +               h_i = au_h_iptr(inode, bindex);
18395 +               if (!h_i)
18396 +                       continue;
18397 +
18398 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18399 +               /* ignore this error */
18400 +               /* bad action? */
18401 +       }
18402 +
18403 +       /* children inode number will be broken */
18404 +
18405 +out:
18406 +       AuTraceErr(err);
18407 +       return err;
18408 +}
18409 +
18410 +static int hn_gen_tree(struct dentry *dentry)
18411 +{
18412 +       int err, i, j, ndentry;
18413 +       struct au_dcsub_pages dpages;
18414 +       struct au_dpage *dpage;
18415 +       struct dentry **dentries;
18416 +
18417 +       err = au_dpages_init(&dpages, GFP_NOFS);
18418 +       if (unlikely(err))
18419 +               goto out;
18420 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18421 +       if (unlikely(err))
18422 +               goto out_dpages;
18423 +
18424 +       for (i = 0; i < dpages.ndpage; i++) {
18425 +               dpage = dpages.dpages + i;
18426 +               dentries = dpage->dentries;
18427 +               ndentry = dpage->ndentry;
18428 +               for (j = 0; j < ndentry; j++) {
18429 +                       struct dentry *d;
18430 +
18431 +                       d = dentries[j];
18432 +                       if (IS_ROOT(d))
18433 +                               continue;
18434 +
18435 +                       au_digen_dec(d);
18436 +                       if (d_really_is_positive(d))
18437 +                               /* todo: reset children xino?
18438 +                                  cached children only? */
18439 +                               au_iigen_dec(d_inode(d));
18440 +               }
18441 +       }
18442 +
18443 +out_dpages:
18444 +       au_dpages_free(&dpages);
18445 +out:
18446 +       return err;
18447 +}
18448 +
18449 +/*
18450 + * return 0 if processed.
18451 + */
18452 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18453 +                          const unsigned int isdir)
18454 +{
18455 +       int err;
18456 +       struct dentry *d;
18457 +       struct qstr *dname;
18458 +
18459 +       err = 1;
18460 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18461 +               pr_warn("branch root dir was changed\n");
18462 +               err = 0;
18463 +               goto out;
18464 +       }
18465 +
18466 +       if (!isdir) {
18467 +               AuDebugOn(!name);
18468 +               au_iigen_dec(inode);
18469 +               spin_lock(&inode->i_lock);
18470 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18471 +                       spin_lock(&d->d_lock);
18472 +                       dname = &d->d_name;
18473 +                       if (dname->len != nlen
18474 +                           && memcmp(dname->name, name, nlen)) {
18475 +                               spin_unlock(&d->d_lock);
18476 +                               continue;
18477 +                       }
18478 +                       err = 0;
18479 +                       au_digen_dec(d);
18480 +                       spin_unlock(&d->d_lock);
18481 +                       break;
18482 +               }
18483 +               spin_unlock(&inode->i_lock);
18484 +       } else {
18485 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18486 +               d = d_find_any_alias(inode);
18487 +               if (!d) {
18488 +                       au_iigen_dec(inode);
18489 +                       goto out;
18490 +               }
18491 +
18492 +               spin_lock(&d->d_lock);
18493 +               dname = &d->d_name;
18494 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18495 +                       spin_unlock(&d->d_lock);
18496 +                       err = hn_gen_tree(d);
18497 +                       spin_lock(&d->d_lock);
18498 +               }
18499 +               spin_unlock(&d->d_lock);
18500 +               dput(d);
18501 +       }
18502 +
18503 +out:
18504 +       AuTraceErr(err);
18505 +       return err;
18506 +}
18507 +
18508 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18509 +{
18510 +       int err;
18511 +
18512 +       if (IS_ROOT(dentry)) {
18513 +               pr_warn("branch root dir was changed\n");
18514 +               return 0;
18515 +       }
18516 +
18517 +       err = 0;
18518 +       if (!isdir) {
18519 +               au_digen_dec(dentry);
18520 +               if (d_really_is_positive(dentry))
18521 +                       au_iigen_dec(d_inode(dentry));
18522 +       } else {
18523 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18524 +               if (d_really_is_positive(dentry))
18525 +                       err = hn_gen_tree(dentry);
18526 +       }
18527 +
18528 +       AuTraceErr(err);
18529 +       return err;
18530 +}
18531 +
18532 +/* ---------------------------------------------------------------------- */
18533 +
18534 +/* hnotify job flags */
18535 +#define AuHnJob_XINO0          1
18536 +#define AuHnJob_GEN            (1 << 1)
18537 +#define AuHnJob_DIRENT         (1 << 2)
18538 +#define AuHnJob_ISDIR          (1 << 3)
18539 +#define AuHnJob_TRYXINO0       (1 << 4)
18540 +#define AuHnJob_MNTPNT         (1 << 5)
18541 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
18542 +#define au_fset_hnjob(flags, name) \
18543 +       do { (flags) |= AuHnJob_##name; } while (0)
18544 +#define au_fclr_hnjob(flags, name) \
18545 +       do { (flags) &= ~AuHnJob_##name; } while (0)
18546 +
18547 +enum {
18548 +       AuHn_CHILD,
18549 +       AuHn_PARENT,
18550 +       AuHnLast
18551 +};
18552 +
18553 +struct au_hnotify_args {
18554 +       struct inode *h_dir, *dir, *h_child_inode;
18555 +       u32 mask;
18556 +       unsigned int flags[AuHnLast];
18557 +       unsigned int h_child_nlen;
18558 +       char h_child_name[];
18559 +};
18560 +
18561 +struct hn_job_args {
18562 +       unsigned int flags;
18563 +       struct inode *inode, *h_inode, *dir, *h_dir;
18564 +       struct dentry *dentry;
18565 +       char *h_name;
18566 +       int h_nlen;
18567 +};
18568 +
18569 +static int hn_job(struct hn_job_args *a)
18570 +{
18571 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18572 +       int e;
18573 +
18574 +       /* reset xino */
18575 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18576 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
18577 +
18578 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
18579 +           && a->inode
18580 +           && a->h_inode) {
18581 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18582 +               if (!a->h_inode->i_nlink
18583 +                   && !(a->h_inode->i_state & I_LINKABLE))
18584 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
18585 +               inode_unlock_shared(a->h_inode);
18586 +       }
18587 +
18588 +       /* make the generation obsolete */
18589 +       if (au_ftest_hnjob(a->flags, GEN)) {
18590 +               e = -1;
18591 +               if (a->inode)
18592 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18593 +                                             isdir);
18594 +               if (e && a->dentry)
18595 +                       hn_gen_by_name(a->dentry, isdir);
18596 +               /* ignore this error */
18597 +       }
18598 +
18599 +       /* make dir entries obsolete */
18600 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18601 +               struct au_vdir *vdir;
18602 +
18603 +               vdir = au_ivdir(a->inode);
18604 +               if (vdir)
18605 +                       vdir->vd_jiffy = 0;
18606 +               /* IMustLock(a->inode); */
18607 +               /* inode_inc_iversion(a->inode); */
18608 +       }
18609 +
18610 +       /* can do nothing but warn */
18611 +       if (au_ftest_hnjob(a->flags, MNTPNT)
18612 +           && a->dentry
18613 +           && d_mountpoint(a->dentry))
18614 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18615 +
18616 +       return 0;
18617 +}
18618 +
18619 +/* ---------------------------------------------------------------------- */
18620 +
18621 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18622 +                                          struct inode *dir)
18623 +{
18624 +       struct dentry *dentry, *d, *parent;
18625 +       struct qstr *dname;
18626 +
18627 +       parent = d_find_any_alias(dir);
18628 +       if (!parent)
18629 +               return NULL;
18630 +
18631 +       dentry = NULL;
18632 +       spin_lock(&parent->d_lock);
18633 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
18634 +               /* AuDbg("%pd\n", d); */
18635 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18636 +               dname = &d->d_name;
18637 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
18638 +                       goto cont_unlock;
18639 +               if (au_di(d))
18640 +                       au_digen_dec(d);
18641 +               else
18642 +                       goto cont_unlock;
18643 +               if (au_dcount(d) > 0) {
18644 +                       dentry = dget_dlock(d);
18645 +                       spin_unlock(&d->d_lock);
18646 +                       break;
18647 +               }
18648 +
18649 +cont_unlock:
18650 +               spin_unlock(&d->d_lock);
18651 +       }
18652 +       spin_unlock(&parent->d_lock);
18653 +       dput(parent);
18654 +
18655 +       if (dentry)
18656 +               di_write_lock_child(dentry);
18657 +
18658 +       return dentry;
18659 +}
18660 +
18661 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18662 +                                        aufs_bindex_t bindex, ino_t h_ino)
18663 +{
18664 +       struct inode *inode;
18665 +       ino_t ino;
18666 +       int err;
18667 +
18668 +       inode = NULL;
18669 +       err = au_xino_read(sb, bindex, h_ino, &ino);
18670 +       if (!err && ino)
18671 +               inode = ilookup(sb, ino);
18672 +       if (!inode)
18673 +               goto out;
18674 +
18675 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18676 +               pr_warn("wrong root branch\n");
18677 +               iput(inode);
18678 +               inode = NULL;
18679 +               goto out;
18680 +       }
18681 +
18682 +       ii_write_lock_child(inode);
18683 +
18684 +out:
18685 +       return inode;
18686 +}
18687 +
18688 +static void au_hn_bh(void *_args)
18689 +{
18690 +       struct au_hnotify_args *a = _args;
18691 +       struct super_block *sb;
18692 +       aufs_bindex_t bindex, bbot, bfound;
18693 +       unsigned char xino, try_iput;
18694 +       int err;
18695 +       struct inode *inode;
18696 +       ino_t h_ino;
18697 +       struct hn_job_args args;
18698 +       struct dentry *dentry;
18699 +       struct au_sbinfo *sbinfo;
18700 +
18701 +       AuDebugOn(!_args);
18702 +       AuDebugOn(!a->h_dir);
18703 +       AuDebugOn(!a->dir);
18704 +       AuDebugOn(!a->mask);
18705 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18706 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
18707 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
18708 +
18709 +       inode = NULL;
18710 +       dentry = NULL;
18711 +       /*
18712 +        * do not lock a->dir->i_mutex here
18713 +        * because of d_revalidate() may cause a deadlock.
18714 +        */
18715 +       sb = a->dir->i_sb;
18716 +       AuDebugOn(!sb);
18717 +       sbinfo = au_sbi(sb);
18718 +       AuDebugOn(!sbinfo);
18719 +       si_write_lock(sb, AuLock_NOPLMW);
18720 +
18721 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18722 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18723 +               case FS_MOVED_FROM:
18724 +               case FS_MOVED_TO:
18725 +                       AuWarn1("DIRREN with UDBA may not work correctly "
18726 +                               "for the direct rename(2)\n");
18727 +               }
18728 +
18729 +       ii_read_lock_parent(a->dir);
18730 +       bfound = -1;
18731 +       bbot = au_ibbot(a->dir);
18732 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18733 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18734 +                       bfound = bindex;
18735 +                       break;
18736 +               }
18737 +       ii_read_unlock(a->dir);
18738 +       if (unlikely(bfound < 0))
18739 +               goto out;
18740 +
18741 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
18742 +       h_ino = 0;
18743 +       if (a->h_child_inode)
18744 +               h_ino = a->h_child_inode->i_ino;
18745 +
18746 +       if (a->h_child_nlen
18747 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18748 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18749 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18750 +                                             a->dir);
18751 +       try_iput = 0;
18752 +       if (dentry && d_really_is_positive(dentry))
18753 +               inode = d_inode(dentry);
18754 +       if (xino && !inode && h_ino
18755 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18756 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18757 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18758 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18759 +               try_iput = 1;
18760 +       }
18761 +
18762 +       args.flags = a->flags[AuHn_CHILD];
18763 +       args.dentry = dentry;
18764 +       args.inode = inode;
18765 +       args.h_inode = a->h_child_inode;
18766 +       args.dir = a->dir;
18767 +       args.h_dir = a->h_dir;
18768 +       args.h_name = a->h_child_name;
18769 +       args.h_nlen = a->h_child_nlen;
18770 +       err = hn_job(&args);
18771 +       if (dentry) {
18772 +               if (au_di(dentry))
18773 +                       di_write_unlock(dentry);
18774 +               dput(dentry);
18775 +       }
18776 +       if (inode && try_iput) {
18777 +               ii_write_unlock(inode);
18778 +               iput(inode);
18779 +       }
18780 +
18781 +       ii_write_lock_parent(a->dir);
18782 +       args.flags = a->flags[AuHn_PARENT];
18783 +       args.dentry = NULL;
18784 +       args.inode = a->dir;
18785 +       args.h_inode = a->h_dir;
18786 +       args.dir = NULL;
18787 +       args.h_dir = NULL;
18788 +       args.h_name = NULL;
18789 +       args.h_nlen = 0;
18790 +       err = hn_job(&args);
18791 +       ii_write_unlock(a->dir);
18792 +
18793 +out:
18794 +       iput(a->h_child_inode);
18795 +       iput(a->h_dir);
18796 +       iput(a->dir);
18797 +       si_write_unlock(sb);
18798 +       au_nwt_done(&sbinfo->si_nowait);
18799 +       au_kfree_rcu(a);
18800 +}
18801 +
18802 +/* ---------------------------------------------------------------------- */
18803 +
18804 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18805 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
18806 +{
18807 +       int err, len;
18808 +       unsigned int flags[AuHnLast], f;
18809 +       unsigned char isdir, isroot, wh;
18810 +       struct inode *dir;
18811 +       struct au_hnotify_args *args;
18812 +       char *p, *h_child_name;
18813 +
18814 +       err = 0;
18815 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18816 +       dir = igrab(hnotify->hn_aufs_inode);
18817 +       if (!dir)
18818 +               goto out;
18819 +
18820 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
18821 +       wh = 0;
18822 +       h_child_name = (void *)h_child_qstr->name;
18823 +       len = h_child_qstr->len;
18824 +       if (h_child_name) {
18825 +               if (len > AUFS_WH_PFX_LEN
18826 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18827 +                       h_child_name += AUFS_WH_PFX_LEN;
18828 +                       len -= AUFS_WH_PFX_LEN;
18829 +                       wh = 1;
18830 +               }
18831 +       }
18832 +
18833 +       isdir = 0;
18834 +       if (h_child_inode)
18835 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
18836 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
18837 +       flags[AuHn_CHILD] = 0;
18838 +       if (isdir)
18839 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
18840 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18841 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
18842 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18843 +       case FS_MOVED_FROM:
18844 +       case FS_MOVED_TO:
18845 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18846 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18847 +               fallthrough;
18848 +       case FS_CREATE:
18849 +               AuDebugOn(!h_child_name);
18850 +               break;
18851 +
18852 +       case FS_DELETE:
18853 +               /*
18854 +                * aufs never be able to get this child inode.
18855 +                * revalidation should be in d_revalidate()
18856 +                * by checking i_nlink, i_generation or d_unhashed().
18857 +                */
18858 +               AuDebugOn(!h_child_name);
18859 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18860 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18861 +               break;
18862 +
18863 +       default:
18864 +               AuDebugOn(1);
18865 +       }
18866 +
18867 +       if (wh)
18868 +               h_child_inode = NULL;
18869 +
18870 +       err = -ENOMEM;
18871 +       /* iput() and kfree() will be called in au_hnotify() */
18872 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18873 +       if (unlikely(!args)) {
18874 +               AuErr1("no memory\n");
18875 +               iput(dir);
18876 +               goto out;
18877 +       }
18878 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18879 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18880 +       args->mask = mask;
18881 +       args->dir = dir;
18882 +       args->h_dir = igrab(h_dir);
18883 +       if (h_child_inode)
18884 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
18885 +       args->h_child_inode = h_child_inode;
18886 +       args->h_child_nlen = len;
18887 +       if (len) {
18888 +               p = (void *)args;
18889 +               p += sizeof(*args);
18890 +               memcpy(p, h_child_name, len);
18891 +               p[len] = 0;
18892 +       }
18893 +
18894 +       /* NFS fires the event for silly-renamed one from kworker */
18895 +       f = 0;
18896 +       if (!dir->i_nlink
18897 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18898 +               f = AuWkq_NEST;
18899 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18900 +       if (unlikely(err)) {
18901 +               pr_err("wkq %d\n", err);
18902 +               iput(args->h_child_inode);
18903 +               iput(args->h_dir);
18904 +               iput(args->dir);
18905 +               au_kfree_rcu(args);
18906 +       }
18907 +
18908 +out:
18909 +       return err;
18910 +}
18911 +
18912 +/* ---------------------------------------------------------------------- */
18913 +
18914 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18915 +{
18916 +       int err;
18917 +
18918 +       AuDebugOn(!(udba & AuOptMask_UDBA));
18919 +
18920 +       err = 0;
18921 +       if (au_hnotify_op.reset_br)
18922 +               err = au_hnotify_op.reset_br(udba, br, perm);
18923 +
18924 +       return err;
18925 +}
18926 +
18927 +int au_hnotify_init_br(struct au_branch *br, int perm)
18928 +{
18929 +       int err;
18930 +
18931 +       err = 0;
18932 +       if (au_hnotify_op.init_br)
18933 +               err = au_hnotify_op.init_br(br, perm);
18934 +
18935 +       return err;
18936 +}
18937 +
18938 +void au_hnotify_fin_br(struct au_branch *br)
18939 +{
18940 +       if (au_hnotify_op.fin_br)
18941 +               au_hnotify_op.fin_br(br);
18942 +}
18943 +
18944 +static void au_hn_destroy_cache(void)
18945 +{
18946 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18947 +       au_cache[AuCache_HNOTIFY] = NULL;
18948 +}
18949 +
18950 +int __init au_hnotify_init(void)
18951 +{
18952 +       int err;
18953 +
18954 +       err = -ENOMEM;
18955 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18956 +       if (au_cache[AuCache_HNOTIFY]) {
18957 +               err = 0;
18958 +               if (au_hnotify_op.init)
18959 +                       err = au_hnotify_op.init();
18960 +               if (unlikely(err))
18961 +                       au_hn_destroy_cache();
18962 +       }
18963 +       AuTraceErr(err);
18964 +       return err;
18965 +}
18966 +
18967 +void au_hnotify_fin(void)
18968 +{
18969 +       if (au_hnotify_op.fin)
18970 +               au_hnotify_op.fin();
18971 +
18972 +       /* cf. au_cache_fin() */
18973 +       if (au_cache[AuCache_HNOTIFY])
18974 +               au_hn_destroy_cache();
18975 +}
18976 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
18977 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
18978 +++ linux/fs/aufs/iinfo.c       2022-11-05 23:02:18.965889284 +0100
18979 @@ -0,0 +1,286 @@
18980 +// SPDX-License-Identifier: GPL-2.0
18981 +/*
18982 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18983 + *
18984 + * This program is free software; you can redistribute it and/or modify
18985 + * it under the terms of the GNU General Public License as published by
18986 + * the Free Software Foundation; either version 2 of the License, or
18987 + * (at your option) any later version.
18988 + *
18989 + * This program is distributed in the hope that it will be useful,
18990 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18991 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18992 + * GNU General Public License for more details.
18993 + *
18994 + * You should have received a copy of the GNU General Public License
18995 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18996 + */
18997 +
18998 +/*
18999 + * inode private data
19000 + */
19001 +
19002 +#include "aufs.h"
19003 +
19004 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
19005 +{
19006 +       struct inode *h_inode;
19007 +       struct au_hinode *hinode;
19008 +
19009 +       IiMustAnyLock(inode);
19010 +
19011 +       hinode = au_hinode(au_ii(inode), bindex);
19012 +       h_inode = hinode->hi_inode;
19013 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19014 +       return h_inode;
19015 +}
19016 +
19017 +/* todo: hard/soft set? */
19018 +void au_hiput(struct au_hinode *hinode)
19019 +{
19020 +       au_hn_free(hinode);
19021 +       dput(hinode->hi_whdentry);
19022 +       iput(hinode->hi_inode);
19023 +}
19024 +
19025 +unsigned int au_hi_flags(struct inode *inode, int isdir)
19026 +{
19027 +       unsigned int flags;
19028 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
19029 +
19030 +       flags = 0;
19031 +       if (au_opt_test(mnt_flags, XINO))
19032 +               au_fset_hi(flags, XINO);
19033 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19034 +               au_fset_hi(flags, HNOTIFY);
19035 +       return flags;
19036 +}
19037 +
19038 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19039 +                  struct inode *h_inode, unsigned int flags)
19040 +{
19041 +       struct au_hinode *hinode;
19042 +       struct inode *hi;
19043 +       struct au_iinfo *iinfo = au_ii(inode);
19044 +
19045 +       IiMustWriteLock(inode);
19046 +
19047 +       hinode = au_hinode(iinfo, bindex);
19048 +       hi = hinode->hi_inode;
19049 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19050 +
19051 +       if (hi)
19052 +               au_hiput(hinode);
19053 +       hinode->hi_inode = h_inode;
19054 +       if (h_inode) {
19055 +               int err;
19056 +               struct super_block *sb = inode->i_sb;
19057 +               struct au_branch *br;
19058 +
19059 +               AuDebugOn(inode->i_mode
19060 +                         && (h_inode->i_mode & S_IFMT)
19061 +                         != (inode->i_mode & S_IFMT));
19062 +               if (bindex == iinfo->ii_btop)
19063 +                       au_cpup_igen(inode, h_inode);
19064 +               br = au_sbr(sb, bindex);
19065 +               hinode->hi_id = br->br_id;
19066 +               if (au_ftest_hi(flags, XINO)) {
19067 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
19068 +                                           inode->i_ino);
19069 +                       if (unlikely(err))
19070 +                               AuIOErr1("failed au_xino_write() %d\n", err);
19071 +               }
19072 +
19073 +               if (au_ftest_hi(flags, HNOTIFY)
19074 +                   && au_br_hnotifyable(br->br_perm)) {
19075 +                       err = au_hn_alloc(hinode, inode);
19076 +                       if (unlikely(err))
19077 +                               AuIOErr1("au_hn_alloc() %d\n", err);
19078 +               }
19079 +       }
19080 +}
19081 +
19082 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19083 +                 struct dentry *h_wh)
19084 +{
19085 +       struct au_hinode *hinode;
19086 +
19087 +       IiMustWriteLock(inode);
19088 +
19089 +       hinode = au_hinode(au_ii(inode), bindex);
19090 +       AuDebugOn(hinode->hi_whdentry);
19091 +       hinode->hi_whdentry = h_wh;
19092 +}
19093 +
19094 +void au_update_iigen(struct inode *inode, int half)
19095 +{
19096 +       struct au_iinfo *iinfo;
19097 +       struct au_iigen *iigen;
19098 +       unsigned int sigen;
19099 +
19100 +       sigen = au_sigen(inode->i_sb);
19101 +       iinfo = au_ii(inode);
19102 +       iigen = &iinfo->ii_generation;
19103 +       spin_lock(&iigen->ig_spin);
19104 +       iigen->ig_generation = sigen;
19105 +       if (half)
19106 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19107 +       else
19108 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
19109 +       spin_unlock(&iigen->ig_spin);
19110 +}
19111 +
19112 +/* it may be called at remount time, too */
19113 +void au_update_ibrange(struct inode *inode, int do_put_zero)
19114 +{
19115 +       struct au_iinfo *iinfo;
19116 +       aufs_bindex_t bindex, bbot;
19117 +
19118 +       AuDebugOn(au_is_bad_inode(inode));
19119 +       IiMustWriteLock(inode);
19120 +
19121 +       iinfo = au_ii(inode);
19122 +       if (do_put_zero && iinfo->ii_btop >= 0) {
19123 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19124 +                    bindex++) {
19125 +                       struct inode *h_i;
19126 +
19127 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
19128 +                       if (h_i
19129 +                           && !h_i->i_nlink
19130 +                           && !(h_i->i_state & I_LINKABLE))
19131 +                               au_set_h_iptr(inode, bindex, NULL, 0);
19132 +               }
19133 +       }
19134 +
19135 +       iinfo->ii_btop = -1;
19136 +       iinfo->ii_bbot = -1;
19137 +       bbot = au_sbbot(inode->i_sb);
19138 +       for (bindex = 0; bindex <= bbot; bindex++)
19139 +               if (au_hinode(iinfo, bindex)->hi_inode) {
19140 +                       iinfo->ii_btop = bindex;
19141 +                       break;
19142 +               }
19143 +       if (iinfo->ii_btop >= 0)
19144 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19145 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
19146 +                               iinfo->ii_bbot = bindex;
19147 +                               break;
19148 +                       }
19149 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
19150 +}
19151 +
19152 +/* ---------------------------------------------------------------------- */
19153 +
19154 +void au_icntnr_init_once(void *_c)
19155 +{
19156 +       struct au_icntnr *c = _c;
19157 +       struct au_iinfo *iinfo = &c->iinfo;
19158 +
19159 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
19160 +       au_rw_init(&iinfo->ii_rwsem);
19161 +       inode_init_once(&c->vfs_inode);
19162 +}
19163 +
19164 +void au_hinode_init(struct au_hinode *hinode)
19165 +{
19166 +       hinode->hi_inode = NULL;
19167 +       hinode->hi_id = -1;
19168 +       au_hn_init(hinode);
19169 +       hinode->hi_whdentry = NULL;
19170 +}
19171 +
19172 +int au_iinfo_init(struct inode *inode)
19173 +{
19174 +       struct au_iinfo *iinfo;
19175 +       struct super_block *sb;
19176 +       struct au_hinode *hi;
19177 +       int nbr, i;
19178 +
19179 +       sb = inode->i_sb;
19180 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19181 +       nbr = au_sbbot(sb) + 1;
19182 +       if (unlikely(nbr <= 0))
19183 +               nbr = 1;
19184 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19185 +       if (hi) {
19186 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
19187 +
19188 +               iinfo->ii_hinode = hi;
19189 +               for (i = 0; i < nbr; i++, hi++)
19190 +                       au_hinode_init(hi);
19191 +
19192 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
19193 +               iinfo->ii_btop = -1;
19194 +               iinfo->ii_bbot = -1;
19195 +               iinfo->ii_vdir = NULL;
19196 +               return 0;
19197 +       }
19198 +       return -ENOMEM;
19199 +}
19200 +
19201 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
19202 +{
19203 +       int err, i;
19204 +       struct au_hinode *hip;
19205 +
19206 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
19207 +
19208 +       err = -ENOMEM;
19209 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19210 +                         may_shrink);
19211 +       if (hip) {
19212 +               iinfo->ii_hinode = hip;
19213 +               i = iinfo->ii_bbot + 1;
19214 +               hip += i;
19215 +               for (; i < nbr; i++, hip++)
19216 +                       au_hinode_init(hip);
19217 +               err = 0;
19218 +       }
19219 +
19220 +       return err;
19221 +}
19222 +
19223 +void au_iinfo_fin(struct inode *inode)
19224 +{
19225 +       struct au_iinfo *iinfo;
19226 +       struct au_hinode *hi;
19227 +       struct super_block *sb;
19228 +       aufs_bindex_t bindex, bbot;
19229 +       const unsigned char unlinked = !inode->i_nlink;
19230 +
19231 +       AuDebugOn(au_is_bad_inode(inode));
19232 +
19233 +       sb = inode->i_sb;
19234 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
19235 +       if (si_pid_test(sb))
19236 +               au_xino_delete_inode(inode, unlinked);
19237 +       else {
19238 +               /*
19239 +                * it is safe to hide the dependency between sbinfo and
19240 +                * sb->s_umount.
19241 +                */
19242 +               lockdep_off();
19243 +               si_noflush_read_lock(sb);
19244 +               au_xino_delete_inode(inode, unlinked);
19245 +               si_read_unlock(sb);
19246 +               lockdep_on();
19247 +       }
19248 +
19249 +       iinfo = au_ii(inode);
19250 +       if (iinfo->ii_vdir)
19251 +               au_vdir_free(iinfo->ii_vdir);
19252 +
19253 +       bindex = iinfo->ii_btop;
19254 +       if (bindex >= 0) {
19255 +               hi = au_hinode(iinfo, bindex);
19256 +               bbot = iinfo->ii_bbot;
19257 +               while (bindex++ <= bbot) {
19258 +                       if (hi->hi_inode)
19259 +                               au_hiput(hi);
19260 +                       hi++;
19261 +               }
19262 +       }
19263 +       au_kfree_rcu(iinfo->ii_hinode);
19264 +       AuRwDestroy(&iinfo->ii_rwsem);
19265 +}
19266 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
19267 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
19268 +++ linux/fs/aufs/inode.c       2023-09-03 02:21:58.863304341 +0200
19269 @@ -0,0 +1,531 @@
19270 +// SPDX-License-Identifier: GPL-2.0
19271 +/*
19272 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19273 + *
19274 + * This program is free software; you can redistribute it and/or modify
19275 + * it under the terms of the GNU General Public License as published by
19276 + * the Free Software Foundation; either version 2 of the License, or
19277 + * (at your option) any later version.
19278 + *
19279 + * This program is distributed in the hope that it will be useful,
19280 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19281 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19282 + * GNU General Public License for more details.
19283 + *
19284 + * You should have received a copy of the GNU General Public License
19285 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19286 + */
19287 +
19288 +/*
19289 + * inode functions
19290 + */
19291 +
19292 +#include <linux/iversion.h>
19293 +#include "aufs.h"
19294 +
19295 +struct inode *au_igrab(struct inode *inode)
19296 +{
19297 +       if (inode) {
19298 +               AuDebugOn(!atomic_read(&inode->i_count));
19299 +               ihold(inode);
19300 +       }
19301 +       return inode;
19302 +}
19303 +
19304 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19305 +{
19306 +       au_cpup_attr_all(inode, /*force*/0);
19307 +       au_update_iigen(inode, /*half*/1);
19308 +       if (do_version)
19309 +               inode_inc_iversion(inode);
19310 +}
19311 +
19312 +static int au_ii_refresh(struct inode *inode, int *update)
19313 +{
19314 +       int err, e, nbr;
19315 +       umode_t type;
19316 +       aufs_bindex_t bindex, new_bindex;
19317 +       struct super_block *sb;
19318 +       struct au_iinfo *iinfo;
19319 +       struct au_hinode *p, *q, tmp;
19320 +
19321 +       AuDebugOn(au_is_bad_inode(inode));
19322 +       IiMustWriteLock(inode);
19323 +
19324 +       *update = 0;
19325 +       sb = inode->i_sb;
19326 +       nbr = au_sbbot(sb) + 1;
19327 +       type = inode->i_mode & S_IFMT;
19328 +       iinfo = au_ii(inode);
19329 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
19330 +       if (unlikely(err))
19331 +               goto out;
19332 +
19333 +       AuDebugOn(iinfo->ii_btop < 0);
19334 +       p = au_hinode(iinfo, iinfo->ii_btop);
19335 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19336 +            bindex++, p++) {
19337 +               if (!p->hi_inode)
19338 +                       continue;
19339 +
19340 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
19341 +               new_bindex = au_br_index(sb, p->hi_id);
19342 +               if (new_bindex == bindex)
19343 +                       continue;
19344 +
19345 +               if (new_bindex < 0) {
19346 +                       *update = 1;
19347 +                       au_hiput(p);
19348 +                       p->hi_inode = NULL;
19349 +                       continue;
19350 +               }
19351 +
19352 +               if (new_bindex < iinfo->ii_btop)
19353 +                       iinfo->ii_btop = new_bindex;
19354 +               if (iinfo->ii_bbot < new_bindex)
19355 +                       iinfo->ii_bbot = new_bindex;
19356 +               /* swap two lower inode, and loop again */
19357 +               q = au_hinode(iinfo, new_bindex);
19358 +               tmp = *q;
19359 +               *q = *p;
19360 +               *p = tmp;
19361 +               if (tmp.hi_inode) {
19362 +                       bindex--;
19363 +                       p--;
19364 +               }
19365 +       }
19366 +       au_update_ibrange(inode, /*do_put_zero*/0);
19367 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
19368 +       e = au_dy_irefresh(inode);
19369 +       if (unlikely(e && !err))
19370 +               err = e;
19371 +
19372 +out:
19373 +       AuTraceErr(err);
19374 +       return err;
19375 +}
19376 +
19377 +void au_refresh_iop(struct inode *inode, int force_getattr)
19378 +{
19379 +       int type;
19380 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19381 +       const struct inode_operations *iop
19382 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
19383 +
19384 +       if (inode->i_op == iop)
19385 +               return;
19386 +
19387 +       switch (inode->i_mode & S_IFMT) {
19388 +       case S_IFDIR:
19389 +               type = AuIop_DIR;
19390 +               break;
19391 +       case S_IFLNK:
19392 +               type = AuIop_SYMLINK;
19393 +               break;
19394 +       default:
19395 +               type = AuIop_OTHER;
19396 +               break;
19397 +       }
19398 +
19399 +       inode->i_op = iop + type;
19400 +       /* unnecessary smp_wmb() */
19401 +}
19402 +
19403 +int au_refresh_hinode_self(struct inode *inode)
19404 +{
19405 +       int err, update;
19406 +
19407 +       err = au_ii_refresh(inode, &update);
19408 +       if (!err)
19409 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19410 +
19411 +       AuTraceErr(err);
19412 +       return err;
19413 +}
19414 +
19415 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19416 +{
19417 +       int err, e, update;
19418 +       unsigned int flags;
19419 +       umode_t mode;
19420 +       aufs_bindex_t bindex, bbot;
19421 +       unsigned char isdir;
19422 +       struct au_hinode *p;
19423 +       struct au_iinfo *iinfo;
19424 +
19425 +       err = au_ii_refresh(inode, &update);
19426 +       if (unlikely(err))
19427 +               goto out;
19428 +
19429 +       update = 0;
19430 +       iinfo = au_ii(inode);
19431 +       p = au_hinode(iinfo, iinfo->ii_btop);
19432 +       mode = (inode->i_mode & S_IFMT);
19433 +       isdir = S_ISDIR(mode);
19434 +       flags = au_hi_flags(inode, isdir);
19435 +       bbot = au_dbbot(dentry);
19436 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
19437 +               struct inode *h_i, *h_inode;
19438 +               struct dentry *h_d;
19439 +
19440 +               h_d = au_h_dptr(dentry, bindex);
19441 +               if (!h_d || d_is_negative(h_d))
19442 +                       continue;
19443 +
19444 +               h_inode = d_inode(h_d);
19445 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
19446 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
19447 +                       h_i = au_h_iptr(inode, bindex);
19448 +                       if (h_i) {
19449 +                               if (h_i == h_inode)
19450 +                                       continue;
19451 +                               err = -EIO;
19452 +                               break;
19453 +                       }
19454 +               }
19455 +               if (bindex < iinfo->ii_btop)
19456 +                       iinfo->ii_btop = bindex;
19457 +               if (iinfo->ii_bbot < bindex)
19458 +                       iinfo->ii_bbot = bindex;
19459 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
19460 +               update = 1;
19461 +       }
19462 +       au_update_ibrange(inode, /*do_put_zero*/0);
19463 +       e = au_dy_irefresh(inode);
19464 +       if (unlikely(e && !err))
19465 +               err = e;
19466 +       if (!err)
19467 +               au_refresh_hinode_attr(inode, update && isdir);
19468 +
19469 +out:
19470 +       AuTraceErr(err);
19471 +       return err;
19472 +}
19473 +
19474 +static int set_inode(struct inode *inode, struct dentry *dentry)
19475 +{
19476 +       int err;
19477 +       unsigned int flags;
19478 +       umode_t mode;
19479 +       aufs_bindex_t bindex, btop, btail;
19480 +       unsigned char isdir;
19481 +       struct dentry *h_dentry;
19482 +       struct inode *h_inode;
19483 +       struct au_iinfo *iinfo;
19484 +       const struct inode_operations *iop;
19485 +
19486 +       IiMustWriteLock(inode);
19487 +
19488 +       err = 0;
19489 +       isdir = 0;
19490 +       iop = au_sbi(inode->i_sb)->si_iop_array;
19491 +       btop = au_dbtop(dentry);
19492 +       h_dentry = au_h_dptr(dentry, btop);
19493 +       h_inode = d_inode(h_dentry);
19494 +       mode = h_inode->i_mode;
19495 +       switch (mode & S_IFMT) {
19496 +       case S_IFREG:
19497 +               btail = au_dbtail(dentry);
19498 +               inode->i_op = iop + AuIop_OTHER;
19499 +               inode->i_fop = &aufs_file_fop;
19500 +               err = au_dy_iaop(inode, btop, h_inode);
19501 +               if (unlikely(err))
19502 +                       goto out;
19503 +               break;
19504 +       case S_IFDIR:
19505 +               isdir = 1;
19506 +               btail = au_dbtaildir(dentry);
19507 +               inode->i_op = iop + AuIop_DIR;
19508 +               inode->i_fop = &aufs_dir_fop;
19509 +               break;
19510 +       case S_IFLNK:
19511 +               btail = au_dbtail(dentry);
19512 +               inode->i_op = iop + AuIop_SYMLINK;
19513 +               break;
19514 +       case S_IFBLK:
19515 +       case S_IFCHR:
19516 +       case S_IFIFO:
19517 +       case S_IFSOCK:
19518 +               btail = au_dbtail(dentry);
19519 +               inode->i_op = iop + AuIop_OTHER;
19520 +               init_special_inode(inode, mode, h_inode->i_rdev);
19521 +               break;
19522 +       default:
19523 +               AuIOErr("Unknown file type 0%o\n", mode);
19524 +               err = -EIO;
19525 +               goto out;
19526 +       }
19527 +
19528 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
19529 +       flags = au_hi_flags(inode, isdir);
19530 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19531 +           && au_ftest_hi(flags, HNOTIFY)
19532 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
19533 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19534 +               au_fclr_hi(flags, HNOTIFY);
19535 +       iinfo = au_ii(inode);
19536 +       iinfo->ii_btop = btop;
19537 +       iinfo->ii_bbot = btail;
19538 +       for (bindex = btop; bindex <= btail; bindex++) {
19539 +               h_dentry = au_h_dptr(dentry, bindex);
19540 +               if (h_dentry)
19541 +                       au_set_h_iptr(inode, bindex,
19542 +                                     au_igrab(d_inode(h_dentry)), flags);
19543 +       }
19544 +       au_cpup_attr_all(inode, /*force*/1);
19545 +       /*
19546 +        * to force calling aufs_get_inode_acl() every time,
19547 +        * do not call cache_no_acl() for aufs inode.
19548 +        */
19549 +
19550 +out:
19551 +       return err;
19552 +}
19553 +
19554 +/*
19555 + * successful returns with iinfo write_locked
19556 + * minus: errno
19557 + * zero: success, matched
19558 + * plus: no error, but unmatched
19559 + */
19560 +static int reval_inode(struct inode *inode, struct dentry *dentry)
19561 +{
19562 +       int err;
19563 +       unsigned int gen, igflags;
19564 +       aufs_bindex_t bindex, bbot;
19565 +       struct inode *h_inode, *h_dinode;
19566 +       struct dentry *h_dentry;
19567 +
19568 +       /*
19569 +        * before this function, if aufs got any iinfo lock, it must be only
19570 +        * one, the parent dir.
19571 +        * it can happen by UDBA and the obsoleted inode number.
19572 +        */
19573 +       err = -EIO;
19574 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
19575 +               goto out;
19576 +
19577 +       err = 1;
19578 +       ii_write_lock_new_child(inode);
19579 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
19580 +       h_dinode = d_inode(h_dentry);
19581 +       bbot = au_ibbot(inode);
19582 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
19583 +               h_inode = au_h_iptr(inode, bindex);
19584 +               if (!h_inode || h_inode != h_dinode)
19585 +                       continue;
19586 +
19587 +               err = 0;
19588 +               gen = au_iigen(inode, &igflags);
19589 +               if (gen == au_digen(dentry)
19590 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
19591 +                       break;
19592 +
19593 +               /* fully refresh inode using dentry */
19594 +               err = au_refresh_hinode(inode, dentry);
19595 +               if (!err)
19596 +                       au_update_iigen(inode, /*half*/0);
19597 +               break;
19598 +       }
19599 +
19600 +       if (unlikely(err))
19601 +               ii_write_unlock(inode);
19602 +out:
19603 +       return err;
19604 +}
19605 +
19606 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19607 +          unsigned int d_type, ino_t *ino)
19608 +{
19609 +       int err, idx;
19610 +       const int isnondir = d_type != DT_DIR;
19611 +
19612 +       /* prevent hardlinked inode number from race condition */
19613 +       if (isnondir) {
19614 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19615 +               if (unlikely(err))
19616 +                       goto out;
19617 +       }
19618 +
19619 +       err = au_xino_read(sb, bindex, h_ino, ino);
19620 +       if (unlikely(err))
19621 +               goto out_xinondir;
19622 +
19623 +       if (!*ino) {
19624 +               err = -EIO;
19625 +               *ino = au_xino_new_ino(sb);
19626 +               if (unlikely(!*ino))
19627 +                       goto out_xinondir;
19628 +               err = au_xino_write(sb, bindex, h_ino, *ino);
19629 +               if (unlikely(err))
19630 +                       goto out_xinondir;
19631 +       }
19632 +
19633 +out_xinondir:
19634 +       if (isnondir && idx >= 0)
19635 +               au_xinondir_leave(sb, bindex, h_ino, idx);
19636 +out:
19637 +       return err;
19638 +}
19639 +
19640 +/* successful returns with iinfo write_locked */
19641 +/* todo: return with unlocked? */
19642 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
19643 +{
19644 +       struct inode *inode, *h_inode;
19645 +       struct dentry *h_dentry;
19646 +       struct super_block *sb;
19647 +       ino_t h_ino, ino;
19648 +       int err, idx, hlinked;
19649 +       aufs_bindex_t btop;
19650 +
19651 +       sb = dentry->d_sb;
19652 +       btop = au_dbtop(dentry);
19653 +       h_dentry = au_h_dptr(dentry, btop);
19654 +       h_inode = d_inode(h_dentry);
19655 +       h_ino = h_inode->i_ino;
19656 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
19657 +
19658 +new_ino:
19659 +       /*
19660 +        * stop 'race'-ing between hardlinks under different
19661 +        * parents.
19662 +        */
19663 +       if (hlinked) {
19664 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
19665 +               inode = ERR_PTR(err);
19666 +               if (unlikely(err))
19667 +                       goto out;
19668 +       }
19669 +
19670 +       err = au_xino_read(sb, btop, h_ino, &ino);
19671 +       inode = ERR_PTR(err);
19672 +       if (unlikely(err))
19673 +               goto out_xinondir;
19674 +
19675 +       if (!ino) {
19676 +               ino = au_xino_new_ino(sb);
19677 +               if (unlikely(!ino)) {
19678 +                       inode = ERR_PTR(-EIO);
19679 +                       goto out_xinondir;
19680 +               }
19681 +       }
19682 +
19683 +       AuDbg("i%lu\n", (unsigned long)ino);
19684 +       inode = au_iget_locked(sb, ino);
19685 +       err = PTR_ERR(inode);
19686 +       if (IS_ERR(inode))
19687 +               goto out_xinondir;
19688 +
19689 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19690 +       if (inode->i_state & I_NEW) {
19691 +               ii_write_lock_new_child(inode);
19692 +               err = set_inode(inode, dentry);
19693 +               if (!err) {
19694 +                       unlock_new_inode(inode);
19695 +                       goto out_xinondir; /* success */
19696 +               }
19697 +
19698 +               /*
19699 +                * iget_failed() calls iput(), but we need to call
19700 +                * ii_write_unlock() after iget_failed(). so dirty hack for
19701 +                * i_count.
19702 +                */
19703 +               atomic_inc(&inode->i_count);
19704 +               iget_failed(inode);
19705 +               ii_write_unlock(inode);
19706 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
19707 +               /* ignore this error */
19708 +               goto out_iput;
19709 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
19710 +               /*
19711 +                * horrible race condition between lookup, readdir and copyup
19712 +                * (or something).
19713 +                */
19714 +               if (hlinked && idx >= 0)
19715 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19716 +               err = reval_inode(inode, dentry);
19717 +               if (unlikely(err < 0)) {
19718 +                       hlinked = 0;
19719 +                       goto out_iput;
19720 +               }
19721 +               if (!err)
19722 +                       goto out; /* success */
19723 +               else if (hlinked && idx >= 0) {
19724 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
19725 +                       if (unlikely(err)) {
19726 +                               iput(inode);
19727 +                               inode = ERR_PTR(err);
19728 +                               goto out;
19729 +                       }
19730 +               }
19731 +       }
19732 +
19733 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
19734 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
19735 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
19736 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
19737 +                       (unsigned long)h_ino, (unsigned long)ino);
19738 +       ino = 0;
19739 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
19740 +       if (!err) {
19741 +               iput(inode);
19742 +               if (hlinked && idx >= 0)
19743 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19744 +               goto new_ino;
19745 +       }
19746 +
19747 +out_iput:
19748 +       iput(inode);
19749 +       inode = ERR_PTR(err);
19750 +out_xinondir:
19751 +       if (hlinked && idx >= 0)
19752 +               au_xinondir_leave(sb, btop, h_ino, idx);
19753 +out:
19754 +       return inode;
19755 +}
19756 +
19757 +/* ---------------------------------------------------------------------- */
19758 +
19759 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19760 +              struct inode *inode)
19761 +{
19762 +       int err;
19763 +       struct inode *hi;
19764 +
19765 +       err = au_br_rdonly(au_sbr(sb, bindex));
19766 +
19767 +       /* pseudo-link after flushed may happen out of bounds */
19768 +       if (!err
19769 +           && inode
19770 +           && au_ibtop(inode) <= bindex
19771 +           && bindex <= au_ibbot(inode)) {
19772 +               /*
19773 +                * permission check is unnecessary since vfsub routine
19774 +                * will be called later
19775 +                */
19776 +               hi = au_h_iptr(inode, bindex);
19777 +               if (hi)
19778 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
19779 +       }
19780 +
19781 +       return err;
19782 +}
19783 +
19784 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19785 +                  int mask)
19786 +{
19787 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
19788 +               return 0;
19789 +       return inode_permission(h_idmap, h_inode, mask);
19790 +}
19791 +
19792 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19793 +                      int mask)
19794 +{
19795 +       if (au_test_nfs(h_inode->i_sb)
19796 +           && (mask & MAY_WRITE)
19797 +           && S_ISDIR(h_inode->i_mode))
19798 +               mask |= MAY_READ; /* force permission check */
19799 +       return au_test_h_perm(h_idmap, h_inode, mask);
19800 +}
19801 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
19802 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
19803 +++ linux/fs/aufs/inode.h       2023-09-03 02:21:58.863304341 +0200
19804 @@ -0,0 +1,707 @@
19805 +/* SPDX-License-Identifier: GPL-2.0 */
19806 +/*
19807 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19808 + *
19809 + * This program is free software; you can redistribute it and/or modify
19810 + * it under the terms of the GNU General Public License as published by
19811 + * the Free Software Foundation; either version 2 of the License, or
19812 + * (at your option) any later version.
19813 + *
19814 + * This program is distributed in the hope that it will be useful,
19815 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19816 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19817 + * GNU General Public License for more details.
19818 + *
19819 + * You should have received a copy of the GNU General Public License
19820 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19821 + */
19822 +
19823 +/*
19824 + * inode operations
19825 + */
19826 +
19827 +#ifndef __AUFS_INODE_H__
19828 +#define __AUFS_INODE_H__
19829 +
19830 +#ifdef __KERNEL__
19831 +
19832 +#include <linux/fsnotify.h>
19833 +#include "rwsem.h"
19834 +
19835 +struct vfsmount;
19836 +
19837 +struct au_hnotify {
19838 +#ifdef CONFIG_AUFS_HNOTIFY
19839 +#ifdef CONFIG_AUFS_HFSNOTIFY
19840 +       /* never use fsnotify_add_vfsmount_mark() */
19841 +       struct fsnotify_mark            hn_mark;
19842 +#endif
19843 +       struct inode            *hn_aufs_inode; /* no get/put */
19844 +       struct rcu_head         rcu;
19845 +#endif
19846 +} ____cacheline_aligned_in_smp;
19847 +
19848 +struct au_hinode {
19849 +       struct inode            *hi_inode;
19850 +       aufs_bindex_t           hi_id;
19851 +#ifdef CONFIG_AUFS_HNOTIFY
19852 +       struct au_hnotify       *hi_notify;
19853 +#endif
19854 +
19855 +       /* reference to the copied-up whiteout with get/put */
19856 +       struct dentry           *hi_whdentry;
19857 +};
19858 +
19859 +/* ig_flags */
19860 +#define AuIG_HALF_REFRESHED            1
19861 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
19862 +#define au_ig_fset(flags, name) \
19863 +       do { (flags) |= AuIG_##name; } while (0)
19864 +#define au_ig_fclr(flags, name) \
19865 +       do { (flags) &= ~AuIG_##name; } while (0)
19866 +
19867 +struct au_iigen {
19868 +       spinlock_t      ig_spin;
19869 +       __u32           ig_generation, ig_flags;
19870 +};
19871 +
19872 +struct au_vdir;
19873 +struct au_iinfo {
19874 +       struct au_iigen         ii_generation;
19875 +       struct super_block      *ii_hsb1;       /* no get/put */
19876 +
19877 +       struct au_rwsem         ii_rwsem;
19878 +       aufs_bindex_t           ii_btop, ii_bbot;
19879 +       __u32                   ii_higen;
19880 +       struct au_hinode        *ii_hinode;
19881 +       struct au_vdir          *ii_vdir;
19882 +};
19883 +
19884 +struct au_icntnr {
19885 +       struct au_iinfo         iinfo;
19886 +       struct inode            vfs_inode;
19887 +       struct hlist_bl_node    plink;
19888 +       struct rcu_head         rcu;
19889 +} ____cacheline_aligned_in_smp;
19890 +
19891 +/* au_pin flags */
19892 +#define AuPin_DI_LOCKED                1
19893 +#define AuPin_MNT_WRITE                (1 << 1)
19894 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
19895 +#define au_fset_pin(flags, name) \
19896 +       do { (flags) |= AuPin_##name; } while (0)
19897 +#define au_fclr_pin(flags, name) \
19898 +       do { (flags) &= ~AuPin_##name; } while (0)
19899 +
19900 +struct au_pin {
19901 +       /* input */
19902 +       struct dentry *dentry;
19903 +       unsigned int udba;
19904 +       unsigned char lsc_di, lsc_hi, flags;
19905 +       aufs_bindex_t bindex;
19906 +
19907 +       /* output */
19908 +       struct dentry *parent;
19909 +       struct au_hinode *hdir;
19910 +       struct vfsmount *h_mnt;
19911 +
19912 +       /* temporary unlock/relock for copyup */
19913 +       struct dentry *h_dentry, *h_parent;
19914 +       struct au_branch *br;
19915 +       struct task_struct *task;
19916 +};
19917 +
19918 +void au_pin_hdir_unlock(struct au_pin *p);
19919 +int au_pin_hdir_lock(struct au_pin *p);
19920 +int au_pin_hdir_relock(struct au_pin *p);
19921 +void au_pin_hdir_acquire_nest(struct au_pin *p);
19922 +void au_pin_hdir_release(struct au_pin *p);
19923 +
19924 +/* ---------------------------------------------------------------------- */
19925 +
19926 +static inline struct au_iinfo *au_ii(struct inode *inode)
19927 +{
19928 +       BUG_ON(is_bad_inode(inode));
19929 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19930 +}
19931 +
19932 +/* ---------------------------------------------------------------------- */
19933 +
19934 +/* inode.c */
19935 +struct inode *au_igrab(struct inode *inode);
19936 +void au_refresh_iop(struct inode *inode, int force_getattr);
19937 +int au_refresh_hinode_self(struct inode *inode);
19938 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19939 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19940 +          unsigned int d_type, ino_t *ino);
19941 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
19942 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19943 +              struct inode *inode);
19944 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19945 +                  int mask);
19946 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19947 +                      int mask);
19948 +
19949 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19950 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
19951 +{
19952 +#ifdef CONFIG_AUFS_SHWH
19953 +       return au_ino(sb, bindex, h_ino, d_type, ino);
19954 +#else
19955 +       return 0;
19956 +#endif
19957 +}
19958 +
19959 +/* i_op.c */
19960 +enum {
19961 +       AuIop_SYMLINK,
19962 +       AuIop_DIR,
19963 +       AuIop_OTHER,
19964 +       AuIop_Last
19965 +};
19966 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
19967 +       aufs_iop_nogetattr[AuIop_Last];
19968 +
19969 +/* au_wr_dir flags */
19970 +#define AuWrDir_ADD_ENTRY      1
19971 +#define AuWrDir_ISDIR          (1 << 1)
19972 +#define AuWrDir_TMPFILE                (1 << 2)
19973 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
19974 +#define au_fset_wrdir(flags, name) \
19975 +       do { (flags) |= AuWrDir_##name; } while (0)
19976 +#define au_fclr_wrdir(flags, name) \
19977 +       do { (flags) &= ~AuWrDir_##name; } while (0)
19978 +
19979 +struct au_wr_dir_args {
19980 +       aufs_bindex_t force_btgt;
19981 +       unsigned char flags;
19982 +};
19983 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19984 +             struct au_wr_dir_args *args);
19985 +
19986 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
19987 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
19988 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19989 +                unsigned int udba, unsigned char flags);
19990 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19991 +          unsigned int udba, unsigned char flags) __must_check;
19992 +int au_do_pin(struct au_pin *pin) __must_check;
19993 +void au_unpin(struct au_pin *pin);
19994 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
19995 +
19996 +#define AuIcpup_DID_CPUP       1
19997 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
19998 +#define au_fset_icpup(flags, name) \
19999 +       do { (flags) |= AuIcpup_##name; } while (0)
20000 +#define au_fclr_icpup(flags, name) \
20001 +       do { (flags) &= ~AuIcpup_##name; } while (0)
20002 +
20003 +struct au_icpup_args {
20004 +       unsigned char flags;
20005 +       unsigned char pin_flags;
20006 +       aufs_bindex_t btgt;
20007 +       unsigned int udba;
20008 +       struct au_pin pin;
20009 +       struct path h_path;
20010 +       struct inode *h_inode;
20011 +};
20012 +
20013 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20014 +                    struct au_icpup_args *a);
20015 +
20016 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20017 +                     struct path *h_path, int locked);
20018 +
20019 +/* i_op_add.c */
20020 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20021 +              struct dentry *h_parent, int isdir);
20022 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
20023 +              struct dentry *dentry, umode_t mode, dev_t dev);
20024 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
20025 +                struct dentry *dentry, const char *symname);
20026 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
20027 +               struct dentry *dentry, umode_t mode, bool want_excl);
20028 +struct vfsub_aopen_args;
20029 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20030 +                      struct vfsub_aopen_args *args);
20031 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
20032 +                struct file *file, umode_t mode);
20033 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20034 +             struct dentry *dentry);
20035 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
20036 +              struct dentry *dentry, umode_t mode);
20037 +
20038 +/* i_op_del.c */
20039 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20040 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20041 +              struct dentry *h_parent, int isdir);
20042 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
20043 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
20044 +
20045 +/* i_op_ren.c */
20046 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
20047 +int aufs_rename(struct mnt_idmap *idmap,
20048 +               struct inode *_src_dir, struct dentry *_src_dentry,
20049 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
20050 +               unsigned int _flags);
20051 +
20052 +/* iinfo.c */
20053 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20054 +void au_hiput(struct au_hinode *hinode);
20055 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20056 +                 struct dentry *h_wh);
20057 +unsigned int au_hi_flags(struct inode *inode, int isdir);
20058 +
20059 +/* hinode flags */
20060 +#define AuHi_XINO      1
20061 +#define AuHi_HNOTIFY   (1 << 1)
20062 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
20063 +#define au_fset_hi(flags, name) \
20064 +       do { (flags) |= AuHi_##name; } while (0)
20065 +#define au_fclr_hi(flags, name) \
20066 +       do { (flags) &= ~AuHi_##name; } while (0)
20067 +
20068 +#ifndef CONFIG_AUFS_HNOTIFY
20069 +#undef AuHi_HNOTIFY
20070 +#define AuHi_HNOTIFY   0
20071 +#endif
20072 +
20073 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20074 +                  struct inode *h_inode, unsigned int flags);
20075 +
20076 +void au_update_iigen(struct inode *inode, int half);
20077 +void au_update_ibrange(struct inode *inode, int do_put_zero);
20078 +
20079 +void au_icntnr_init_once(void *_c);
20080 +void au_hinode_init(struct au_hinode *hinode);
20081 +int au_iinfo_init(struct inode *inode);
20082 +void au_iinfo_fin(struct inode *inode);
20083 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
20084 +
20085 +#ifdef CONFIG_PROC_FS
20086 +/* plink.c */
20087 +int au_plink_maint(struct super_block *sb, int flags);
20088 +struct au_sbinfo;
20089 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20090 +int au_plink_maint_enter(struct super_block *sb);
20091 +#ifdef CONFIG_AUFS_DEBUG
20092 +void au_plink_list(struct super_block *sb);
20093 +#else
20094 +AuStubVoid(au_plink_list, struct super_block *sb)
20095 +#endif
20096 +int au_plink_test(struct inode *inode);
20097 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20098 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20099 +                    struct dentry *h_dentry);
20100 +void au_plink_put(struct super_block *sb, int verbose);
20101 +void au_plink_clean(struct super_block *sb, int verbose);
20102 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
20103 +#else
20104 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20105 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20106 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20107 +AuStubVoid(au_plink_list, struct super_block *sb);
20108 +AuStubInt0(au_plink_test, struct inode *inode);
20109 +AuStub(struct dentry *, au_plink_lkup, return NULL,
20110 +       struct inode *inode, aufs_bindex_t bindex);
20111 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20112 +          struct dentry *h_dentry);
20113 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20114 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20115 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20116 +#endif /* CONFIG_PROC_FS */
20117 +
20118 +#ifdef CONFIG_AUFS_XATTR
20119 +/* xattr.c */
20120 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
20121 +                 unsigned int verbose);
20122 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
20123 +void au_xattr_init(struct super_block *sb);
20124 +#else
20125 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
20126 +          int ignore_flags, unsigned int verbose);
20127 +AuStubVoid(au_xattr_init, struct super_block *sb);
20128 +#endif
20129 +
20130 +#ifdef CONFIG_FS_POSIX_ACL
20131 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu);
20132 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
20133 +                              struct dentry *dentry, int type);
20134 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
20135 +                struct posix_acl *acl, int type);
20136 +#endif
20137 +
20138 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20139 +enum {
20140 +       AU_XATTR_SET,
20141 +       AU_ACL_SET
20142 +};
20143 +
20144 +struct au_sxattr {
20145 +       int type;
20146 +       union {
20147 +               struct {
20148 +                       const char      *name;
20149 +                       const void      *value;
20150 +                       size_t          size;
20151 +                       int             flags;
20152 +               } set;
20153 +               struct {
20154 +                       struct posix_acl *acl;
20155 +                       int             type;
20156 +               } acl_set;
20157 +       } u;
20158 +};
20159 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20160 +                 struct au_sxattr *arg);
20161 +#endif
20162 +
20163 +/* ---------------------------------------------------------------------- */
20164 +
20165 +/* lock subclass for iinfo */
20166 +enum {
20167 +       AuLsc_II_CHILD,         /* child first */
20168 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
20169 +       AuLsc_II_CHILD3,        /* copyup dirs */
20170 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
20171 +       AuLsc_II_PARENT2,
20172 +       AuLsc_II_PARENT3,       /* copyup dirs */
20173 +       AuLsc_II_NEW_CHILD
20174 +};
20175 +
20176 +/*
20177 + * ii_read_lock_child, ii_write_lock_child,
20178 + * ii_read_lock_child2, ii_write_lock_child2,
20179 + * ii_read_lock_child3, ii_write_lock_child3,
20180 + * ii_read_lock_parent, ii_write_lock_parent,
20181 + * ii_read_lock_parent2, ii_write_lock_parent2,
20182 + * ii_read_lock_parent3, ii_write_lock_parent3,
20183 + * ii_read_lock_new_child, ii_write_lock_new_child,
20184 + */
20185 +#define AuReadLockFunc(name, lsc) \
20186 +static inline void ii_read_lock_##name(struct inode *i) \
20187 +{ \
20188 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20189 +}
20190 +
20191 +#define AuWriteLockFunc(name, lsc) \
20192 +static inline void ii_write_lock_##name(struct inode *i) \
20193 +{ \
20194 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20195 +}
20196 +
20197 +#define AuRWLockFuncs(name, lsc) \
20198 +       AuReadLockFunc(name, lsc) \
20199 +       AuWriteLockFunc(name, lsc)
20200 +
20201 +AuRWLockFuncs(child, CHILD);
20202 +AuRWLockFuncs(child2, CHILD2);
20203 +AuRWLockFuncs(child3, CHILD3);
20204 +AuRWLockFuncs(parent, PARENT);
20205 +AuRWLockFuncs(parent2, PARENT2);
20206 +AuRWLockFuncs(parent3, PARENT3);
20207 +AuRWLockFuncs(new_child, NEW_CHILD);
20208 +
20209 +#undef AuReadLockFunc
20210 +#undef AuWriteLockFunc
20211 +#undef AuRWLockFuncs
20212 +
20213 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20214 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20215 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
20216 +
20217 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20218 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20219 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
20220 +
20221 +/* ---------------------------------------------------------------------- */
20222 +
20223 +static inline void au_icntnr_init(struct au_icntnr *c)
20224 +{
20225 +#ifdef CONFIG_AUFS_DEBUG
20226 +       c->vfs_inode.i_mode = 0;
20227 +#endif
20228 +}
20229 +
20230 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
20231 +{
20232 +       unsigned int gen;
20233 +       struct au_iinfo *iinfo;
20234 +       struct au_iigen *iigen;
20235 +
20236 +       iinfo = au_ii(inode);
20237 +       iigen = &iinfo->ii_generation;
20238 +       spin_lock(&iigen->ig_spin);
20239 +       if (igflags)
20240 +               *igflags = iigen->ig_flags;
20241 +       gen = iigen->ig_generation;
20242 +       spin_unlock(&iigen->ig_spin);
20243 +
20244 +       return gen;
20245 +}
20246 +
20247 +/* tiny test for inode number */
20248 +/* tmpfs generation is too rough */
20249 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20250 +{
20251 +       struct au_iinfo *iinfo;
20252 +
20253 +       iinfo = au_ii(inode);
20254 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
20255 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
20256 +                && iinfo->ii_higen == h_inode->i_generation);
20257 +}
20258 +
20259 +static inline void au_iigen_dec(struct inode *inode)
20260 +{
20261 +       struct au_iinfo *iinfo;
20262 +       struct au_iigen *iigen;
20263 +
20264 +       iinfo = au_ii(inode);
20265 +       iigen = &iinfo->ii_generation;
20266 +       spin_lock(&iigen->ig_spin);
20267 +       iigen->ig_generation--;
20268 +       spin_unlock(&iigen->ig_spin);
20269 +}
20270 +
20271 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20272 +{
20273 +       int err;
20274 +
20275 +       err = 0;
20276 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
20277 +               err = -EIO;
20278 +
20279 +       return err;
20280 +}
20281 +
20282 +/* ---------------------------------------------------------------------- */
20283 +
20284 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20285 +                                         aufs_bindex_t bindex)
20286 +{
20287 +       return iinfo->ii_hinode + bindex;
20288 +}
20289 +
20290 +static inline int au_is_bad_inode(struct inode *inode)
20291 +{
20292 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20293 +}
20294 +
20295 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20296 +                                       aufs_bindex_t bindex)
20297 +{
20298 +       IiMustAnyLock(inode);
20299 +       return au_hinode(au_ii(inode), bindex)->hi_id;
20300 +}
20301 +
20302 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
20303 +{
20304 +       IiMustAnyLock(inode);
20305 +       return au_ii(inode)->ii_btop;
20306 +}
20307 +
20308 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
20309 +{
20310 +       IiMustAnyLock(inode);
20311 +       return au_ii(inode)->ii_bbot;
20312 +}
20313 +
20314 +static inline struct au_vdir *au_ivdir(struct inode *inode)
20315 +{
20316 +       IiMustAnyLock(inode);
20317 +       return au_ii(inode)->ii_vdir;
20318 +}
20319 +
20320 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20321 +{
20322 +       IiMustAnyLock(inode);
20323 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
20324 +}
20325 +
20326 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
20327 +{
20328 +       IiMustWriteLock(inode);
20329 +       au_ii(inode)->ii_btop = bindex;
20330 +}
20331 +
20332 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
20333 +{
20334 +       IiMustWriteLock(inode);
20335 +       au_ii(inode)->ii_bbot = bindex;
20336 +}
20337 +
20338 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20339 +{
20340 +       IiMustWriteLock(inode);
20341 +       au_ii(inode)->ii_vdir = vdir;
20342 +}
20343 +
20344 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
20345 +{
20346 +       IiMustAnyLock(inode);
20347 +       return au_hinode(au_ii(inode), bindex);
20348 +}
20349 +
20350 +/* ---------------------------------------------------------------------- */
20351 +
20352 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20353 +{
20354 +       if (pin)
20355 +               return pin->parent;
20356 +       return NULL;
20357 +}
20358 +
20359 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
20360 +{
20361 +       if (pin && pin->hdir)
20362 +               return pin->hdir->hi_inode;
20363 +       return NULL;
20364 +}
20365 +
20366 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20367 +{
20368 +       if (pin)
20369 +               return pin->hdir;
20370 +       return NULL;
20371 +}
20372 +
20373 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
20374 +{
20375 +       if (pin)
20376 +               pin->dentry = dentry;
20377 +}
20378 +
20379 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20380 +                                          unsigned char lflag)
20381 +{
20382 +       if (pin) {
20383 +               if (lflag)
20384 +                       au_fset_pin(pin->flags, DI_LOCKED);
20385 +               else
20386 +                       au_fclr_pin(pin->flags, DI_LOCKED);
20387 +       }
20388 +}
20389 +
20390 +#if 0 /* reserved */
20391 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20392 +{
20393 +       if (pin) {
20394 +               dput(pin->parent);
20395 +               pin->parent = dget(parent);
20396 +       }
20397 +}
20398 +#endif
20399 +
20400 +/* ---------------------------------------------------------------------- */
20401 +
20402 +struct au_branch;
20403 +#ifdef CONFIG_AUFS_HNOTIFY
20404 +struct au_hnotify_op {
20405 +       void (*ctl)(struct au_hinode *hinode, int do_set);
20406 +       int (*alloc)(struct au_hinode *hinode);
20407 +
20408 +       /*
20409 +        * if it returns true, the caller should free hinode->hi_notify,
20410 +        * otherwise ->free() frees it.
20411 +        */
20412 +       int (*free)(struct au_hinode *hinode,
20413 +                   struct au_hnotify *hn) __must_check;
20414 +
20415 +       void (*fin)(void);
20416 +       int (*init)(void);
20417 +
20418 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20419 +       void (*fin_br)(struct au_branch *br);
20420 +       int (*init_br)(struct au_branch *br, int perm);
20421 +};
20422 +
20423 +/* hnotify.c */
20424 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
20425 +void au_hn_free(struct au_hinode *hinode);
20426 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
20427 +void au_hn_reset(struct inode *inode, unsigned int flags);
20428 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
20429 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
20430 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20431 +int au_hnotify_init_br(struct au_branch *br, int perm);
20432 +void au_hnotify_fin_br(struct au_branch *br);
20433 +int __init au_hnotify_init(void);
20434 +void au_hnotify_fin(void);
20435 +
20436 +/* hfsnotify.c */
20437 +extern const struct au_hnotify_op au_hnotify_op;
20438 +
20439 +static inline
20440 +void au_hn_init(struct au_hinode *hinode)
20441 +{
20442 +       hinode->hi_notify = NULL;
20443 +}
20444 +
20445 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20446 +{
20447 +       return hinode->hi_notify;
20448 +}
20449 +
20450 +#else
20451 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20452 +       struct au_hinode *hinode __maybe_unused,
20453 +       struct inode *inode __maybe_unused)
20454 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
20455 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20456 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20457 +          int do_set __maybe_unused)
20458 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20459 +          unsigned int flags __maybe_unused)
20460 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20461 +          struct au_branch *br __maybe_unused,
20462 +          int perm __maybe_unused)
20463 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20464 +          int perm __maybe_unused)
20465 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
20466 +AuStubInt0(__init au_hnotify_init, void)
20467 +AuStubVoid(au_hnotify_fin, void)
20468 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20469 +#endif /* CONFIG_AUFS_HNOTIFY */
20470 +
20471 +static inline void au_hn_suspend(struct au_hinode *hdir)
20472 +{
20473 +       au_hn_ctl(hdir, /*do_set*/0);
20474 +}
20475 +
20476 +static inline void au_hn_resume(struct au_hinode *hdir)
20477 +{
20478 +       au_hn_ctl(hdir, /*do_set*/1);
20479 +}
20480 +
20481 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
20482 +{
20483 +       inode_lock(hdir->hi_inode);
20484 +       au_hn_suspend(hdir);
20485 +}
20486 +
20487 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
20488 +                                         unsigned int sc __maybe_unused)
20489 +{
20490 +       inode_lock_nested(hdir->hi_inode, sc);
20491 +       au_hn_suspend(hdir);
20492 +}
20493 +
20494 +#if 0 /* unused */
20495 +#include "vfsub.h"
20496 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20497 +                                                 unsigned int sc)
20498 +{
20499 +       inode_lock_shared_nested(hdir->hi_inode, sc);
20500 +       au_hn_suspend(hdir);
20501 +}
20502 +#endif
20503 +
20504 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
20505 +{
20506 +       au_hn_resume(hdir);
20507 +       inode_unlock(hdir->hi_inode);
20508 +}
20509 +
20510 +#endif /* __KERNEL__ */
20511 +#endif /* __AUFS_INODE_H__ */
20512 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
20513 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
20514 +++ linux/fs/aufs/ioctl.c       2022-11-05 23:02:18.965889284 +0100
20515 @@ -0,0 +1,220 @@
20516 +// SPDX-License-Identifier: GPL-2.0
20517 +/*
20518 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20519 + *
20520 + * This program is free software; you can redistribute it and/or modify
20521 + * it under the terms of the GNU General Public License as published by
20522 + * the Free Software Foundation; either version 2 of the License, or
20523 + * (at your option) any later version.
20524 + *
20525 + * This program is distributed in the hope that it will be useful,
20526 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20527 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20528 + * GNU General Public License for more details.
20529 + *
20530 + * You should have received a copy of the GNU General Public License
20531 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20532 + */
20533 +
20534 +/*
20535 + * ioctl
20536 + * plink-management and readdir in userspace.
20537 + * assist the pathconf(3) wrapper library.
20538 + * move-down
20539 + * File-based Hierarchical Storage Management.
20540 + */
20541 +
20542 +#include <linux/compat.h>
20543 +#include <linux/file.h>
20544 +#include "aufs.h"
20545 +
20546 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
20547 +{
20548 +       int err, fd;
20549 +       aufs_bindex_t wbi, bindex, bbot;
20550 +       struct file *h_file;
20551 +       struct super_block *sb;
20552 +       struct dentry *root;
20553 +       struct au_branch *br;
20554 +       struct aufs_wbr_fd wbrfd = {
20555 +               .oflags = au_dir_roflags,
20556 +               .brid   = -1
20557 +       };
20558 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20559 +               | O_NOATIME | O_CLOEXEC;
20560 +
20561 +       AuDebugOn(wbrfd.oflags & ~valid);
20562 +
20563 +       if (arg) {
20564 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20565 +               if (unlikely(err)) {
20566 +                       err = -EFAULT;
20567 +                       goto out;
20568 +               }
20569 +
20570 +               err = -EINVAL;
20571 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20572 +               wbrfd.oflags |= au_dir_roflags;
20573 +               AuDbg("0%o\n", wbrfd.oflags);
20574 +               if (unlikely(wbrfd.oflags & ~valid))
20575 +                       goto out;
20576 +       }
20577 +
20578 +       fd = get_unused_fd_flags(0);
20579 +       err = fd;
20580 +       if (unlikely(fd < 0))
20581 +               goto out;
20582 +
20583 +       h_file = ERR_PTR(-EINVAL);
20584 +       wbi = 0;
20585 +       br = NULL;
20586 +       sb = path->dentry->d_sb;
20587 +       root = sb->s_root;
20588 +       aufs_read_lock(root, AuLock_IR);
20589 +       bbot = au_sbbot(sb);
20590 +       if (wbrfd.brid >= 0) {
20591 +               wbi = au_br_index(sb, wbrfd.brid);
20592 +               if (unlikely(wbi < 0 || wbi > bbot))
20593 +                       goto out_unlock;
20594 +       }
20595 +
20596 +       h_file = ERR_PTR(-ENOENT);
20597 +       br = au_sbr(sb, wbi);
20598 +       if (!au_br_writable(br->br_perm)) {
20599 +               if (arg)
20600 +                       goto out_unlock;
20601 +
20602 +               bindex = wbi + 1;
20603 +               wbi = -1;
20604 +               for (; bindex <= bbot; bindex++) {
20605 +                       br = au_sbr(sb, bindex);
20606 +                       if (au_br_writable(br->br_perm)) {
20607 +                               wbi = bindex;
20608 +                               br = au_sbr(sb, wbi);
20609 +                               break;
20610 +                       }
20611 +               }
20612 +       }
20613 +       AuDbg("wbi %d\n", wbi);
20614 +       if (wbi >= 0)
20615 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20616 +                                  /*force_wr*/0);
20617 +
20618 +out_unlock:
20619 +       aufs_read_unlock(root, AuLock_IR);
20620 +       err = PTR_ERR(h_file);
20621 +       if (IS_ERR(h_file))
20622 +               goto out_fd;
20623 +
20624 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
20625 +       fd_install(fd, h_file);
20626 +       err = fd;
20627 +       goto out; /* success */
20628 +
20629 +out_fd:
20630 +       put_unused_fd(fd);
20631 +out:
20632 +       AuTraceErr(err);
20633 +       return err;
20634 +}
20635 +
20636 +/* ---------------------------------------------------------------------- */
20637 +
20638 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20639 +{
20640 +       long err;
20641 +       struct dentry *dentry;
20642 +
20643 +       switch (cmd) {
20644 +       case AUFS_CTL_RDU:
20645 +       case AUFS_CTL_RDU_INO:
20646 +               err = au_rdu_ioctl(file, cmd, arg);
20647 +               break;
20648 +
20649 +       case AUFS_CTL_WBR_FD:
20650 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20651 +               break;
20652 +
20653 +       case AUFS_CTL_IBUSY:
20654 +               err = au_ibusy_ioctl(file, arg);
20655 +               break;
20656 +
20657 +       case AUFS_CTL_BRINFO:
20658 +               err = au_brinfo_ioctl(file, arg);
20659 +               break;
20660 +
20661 +       case AUFS_CTL_FHSM_FD:
20662 +               dentry = file->f_path.dentry;
20663 +               if (IS_ROOT(dentry))
20664 +                       err = au_fhsm_fd(dentry->d_sb, arg);
20665 +               else
20666 +                       err = -ENOTTY;
20667 +               break;
20668 +
20669 +       default:
20670 +               /* do not call the lower */
20671 +               AuDbg("0x%x\n", cmd);
20672 +               err = -ENOTTY;
20673 +       }
20674 +
20675 +       AuTraceErr(err);
20676 +       return err;
20677 +}
20678 +
20679 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20680 +{
20681 +       long err;
20682 +
20683 +       switch (cmd) {
20684 +       case AUFS_CTL_MVDOWN:
20685 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
20686 +               break;
20687 +
20688 +       case AUFS_CTL_WBR_FD:
20689 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20690 +               break;
20691 +
20692 +       default:
20693 +               /* do not call the lower */
20694 +               AuDbg("0x%x\n", cmd);
20695 +               err = -ENOTTY;
20696 +       }
20697 +
20698 +       AuTraceErr(err);
20699 +       return err;
20700 +}
20701 +
20702 +#ifdef CONFIG_COMPAT
20703 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20704 +                          unsigned long arg)
20705 +{
20706 +       long err;
20707 +
20708 +       switch (cmd) {
20709 +       case AUFS_CTL_RDU:
20710 +       case AUFS_CTL_RDU_INO:
20711 +               err = au_rdu_compat_ioctl(file, cmd, arg);
20712 +               break;
20713 +
20714 +       case AUFS_CTL_IBUSY:
20715 +               err = au_ibusy_compat_ioctl(file, arg);
20716 +               break;
20717 +
20718 +       case AUFS_CTL_BRINFO:
20719 +               err = au_brinfo_compat_ioctl(file, arg);
20720 +               break;
20721 +
20722 +       default:
20723 +               err = aufs_ioctl_dir(file, cmd, arg);
20724 +       }
20725 +
20726 +       AuTraceErr(err);
20727 +       return err;
20728 +}
20729 +
20730 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20731 +                             unsigned long arg)
20732 +{
20733 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20734 +}
20735 +#endif
20736 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
20737 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
20738 +++ linux/fs/aufs/i_op_add.c    2023-09-04 13:39:55.763295607 +0200
20739 @@ -0,0 +1,972 @@
20740 +// SPDX-License-Identifier: GPL-2.0
20741 +/*
20742 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20743 + *
20744 + * This program is free software; you can redistribute it and/or modify
20745 + * it under the terms of the GNU General Public License as published by
20746 + * the Free Software Foundation; either version 2 of the License, or
20747 + * (at your option) any later version.
20748 + *
20749 + * This program is distributed in the hope that it will be useful,
20750 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20751 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20752 + * GNU General Public License for more details.
20753 + *
20754 + * You should have received a copy of the GNU General Public License
20755 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20756 + */
20757 +
20758 +/*
20759 + * inode operations (add entry)
20760 + */
20761 +
20762 +#include <linux/iversion.h>
20763 +#include "aufs.h"
20764 +
20765 +/*
20766 + * final procedure of adding a new entry, except link(2).
20767 + * remove whiteout, instantiate, copyup the parent dir's times and size
20768 + * and update version.
20769 + * if it failed, re-create the removed whiteout.
20770 + */
20771 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20772 +                 struct dentry *wh_dentry, struct dentry *dentry)
20773 +{
20774 +       int err, rerr;
20775 +       aufs_bindex_t bwh;
20776 +       struct path h_path;
20777 +       struct super_block *sb;
20778 +       struct inode *inode, *h_dir;
20779 +       struct dentry *wh;
20780 +
20781 +       bwh = -1;
20782 +       sb = dir->i_sb;
20783 +       if (wh_dentry) {
20784 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20785 +               IMustLock(h_dir);
20786 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20787 +               bwh = au_dbwh(dentry);
20788 +               h_path.dentry = wh_dentry;
20789 +               h_path.mnt = au_sbr_mnt(sb, bindex);
20790 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20791 +                                         dentry);
20792 +               if (unlikely(err))
20793 +                       goto out;
20794 +       }
20795 +
20796 +       inode = au_new_inode(dentry, /*must_new*/1);
20797 +       if (!IS_ERR(inode)) {
20798 +               d_instantiate(dentry, inode);
20799 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
20800 +               IMustLock(dir);
20801 +               au_dir_ts(dir, bindex);
20802 +               inode_inc_iversion(dir);
20803 +               au_fhsm_wrote(sb, bindex, /*force*/0);
20804 +               return 0; /* success */
20805 +       }
20806 +
20807 +       err = PTR_ERR(inode);
20808 +       if (!wh_dentry)
20809 +               goto out;
20810 +
20811 +       /* revert */
20812 +       /* dir inode is locked */
20813 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20814 +       rerr = PTR_ERR(wh);
20815 +       if (IS_ERR(wh)) {
20816 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20817 +                       dentry, err, rerr);
20818 +               err = -EIO;
20819 +       } else
20820 +               dput(wh);
20821 +
20822 +out:
20823 +       return err;
20824 +}
20825 +
20826 +static int au_d_may_add(struct dentry *dentry)
20827 +{
20828 +       int err;
20829 +
20830 +       err = 0;
20831 +       if (unlikely(d_unhashed(dentry)))
20832 +               err = -ENOENT;
20833 +       if (unlikely(d_really_is_positive(dentry)))
20834 +               err = -EEXIST;
20835 +       return err;
20836 +}
20837 +
20838 +/*
20839 + * simple tests for the adding inode operations.
20840 + * following the checks in vfs, plus the parent-child relationship.
20841 + */
20842 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20843 +              struct dentry *h_parent, int isdir)
20844 +{
20845 +       int err;
20846 +       umode_t h_mode;
20847 +       struct dentry *h_dentry;
20848 +       struct inode *h_inode;
20849 +
20850 +       err = -ENAMETOOLONG;
20851 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20852 +               goto out;
20853 +
20854 +       h_dentry = au_h_dptr(dentry, bindex);
20855 +       if (d_really_is_negative(dentry)) {
20856 +               err = -EEXIST;
20857 +               if (unlikely(d_is_positive(h_dentry)))
20858 +                       goto out;
20859 +       } else {
20860 +               /* rename(2) case */
20861 +               err = -EIO;
20862 +               if (unlikely(d_is_negative(h_dentry)))
20863 +                       goto out;
20864 +               h_inode = d_inode(h_dentry);
20865 +               if (unlikely(!h_inode->i_nlink))
20866 +                       goto out;
20867 +
20868 +               h_mode = h_inode->i_mode;
20869 +               if (!isdir) {
20870 +                       err = -EISDIR;
20871 +                       if (unlikely(S_ISDIR(h_mode)))
20872 +                               goto out;
20873 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20874 +                       err = -ENOTDIR;
20875 +                       goto out;
20876 +               }
20877 +       }
20878 +
20879 +       err = 0;
20880 +       /* expected parent dir is locked */
20881 +       if (unlikely(h_parent != h_dentry->d_parent))
20882 +               err = -EIO;
20883 +
20884 +out:
20885 +       AuTraceErr(err);
20886 +       return err;
20887 +}
20888 +
20889 +/*
20890 + * initial procedure of adding a new entry.
20891 + * prepare writable branch and the parent dir, lock it,
20892 + * and lookup whiteout for the new entry.
20893 + */
20894 +static struct dentry*
20895 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20896 +                 struct dentry *src_dentry, struct au_pin *pin,
20897 +                 struct au_wr_dir_args *wr_dir_args)
20898 +{
20899 +       struct dentry *wh_dentry, *h_parent;
20900 +       struct super_block *sb;
20901 +       struct au_branch *br;
20902 +       int err;
20903 +       unsigned int udba;
20904 +       aufs_bindex_t bcpup;
20905 +
20906 +       AuDbg("%pd\n", dentry);
20907 +
20908 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20909 +       bcpup = err;
20910 +       wh_dentry = ERR_PTR(err);
20911 +       if (unlikely(err < 0))
20912 +               goto out;
20913 +
20914 +       sb = dentry->d_sb;
20915 +       udba = au_opt_udba(sb);
20916 +       err = au_pin(pin, dentry, bcpup, udba,
20917 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20918 +       wh_dentry = ERR_PTR(err);
20919 +       if (unlikely(err))
20920 +               goto out;
20921 +
20922 +       h_parent = au_pinned_h_parent(pin);
20923 +       if (udba != AuOpt_UDBA_NONE
20924 +           && au_dbtop(dentry) == bcpup)
20925 +               err = au_may_add(dentry, bcpup, h_parent,
20926 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20927 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20928 +               err = -ENAMETOOLONG;
20929 +       wh_dentry = ERR_PTR(err);
20930 +       if (unlikely(err))
20931 +               goto out_unpin;
20932 +
20933 +       br = au_sbr(sb, bcpup);
20934 +       if (dt) {
20935 +               struct path tmp = {
20936 +                       .dentry = h_parent,
20937 +                       .mnt    = au_br_mnt(br)
20938 +               };
20939 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20940 +       }
20941 +
20942 +       wh_dentry = NULL;
20943 +       if (bcpup != au_dbwh(dentry))
20944 +               goto out; /* success */
20945 +
20946 +       /*
20947 +        * ENAMETOOLONG here means that if we allowed create such name, then it
20948 +        * would not be able to removed in the future. So we don't allow such
20949 +        * name here and we don't handle ENAMETOOLONG differently here.
20950 +        */
20951 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20952 +
20953 +out_unpin:
20954 +       if (IS_ERR(wh_dentry))
20955 +               au_unpin(pin);
20956 +out:
20957 +       return wh_dentry;
20958 +}
20959 +
20960 +/* ---------------------------------------------------------------------- */
20961 +
20962 +enum { Mknod, Symlink, Creat };
20963 +struct simple_arg {
20964 +       int type;
20965 +       union {
20966 +               struct {
20967 +                       umode_t                 mode;
20968 +                       bool                    want_excl;
20969 +                       bool                    try_aopen;
20970 +                       struct vfsub_aopen_args *aopen;
20971 +               } c;
20972 +               struct {
20973 +                       const char *symname;
20974 +               } s;
20975 +               struct {
20976 +                       umode_t mode;
20977 +                       dev_t dev;
20978 +               } m;
20979 +       } u;
20980 +};
20981 +
20982 +static int add_simple(struct inode *dir, struct dentry *dentry,
20983 +                     struct simple_arg *arg)
20984 +{
20985 +       int err, rerr;
20986 +       aufs_bindex_t btop;
20987 +       unsigned char created;
20988 +       const unsigned char try_aopen
20989 +               = (arg->type == Creat && arg->u.c.try_aopen);
20990 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
20991 +       struct dentry *wh_dentry, *parent;
20992 +       struct inode *h_dir;
20993 +       struct super_block *sb;
20994 +       struct au_branch *br;
20995 +       /* to reduce stack size */
20996 +       struct {
20997 +               struct au_dtime dt;
20998 +               struct au_pin pin;
20999 +               struct path h_path;
21000 +               struct au_wr_dir_args wr_dir_args;
21001 +       } *a;
21002 +
21003 +       AuDbg("%pd\n", dentry);
21004 +       IMustLock(dir);
21005 +
21006 +       err = -ENOMEM;
21007 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21008 +       if (unlikely(!a))
21009 +               goto out;
21010 +       a->wr_dir_args.force_btgt = -1;
21011 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21012 +
21013 +       parent = dentry->d_parent; /* dir inode is locked */
21014 +       if (!try_aopen) {
21015 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21016 +               if (unlikely(err))
21017 +                       goto out_free;
21018 +       }
21019 +       err = au_d_may_add(dentry);
21020 +       if (unlikely(err))
21021 +               goto out_unlock;
21022 +       if (!try_aopen)
21023 +               di_write_lock_parent(parent);
21024 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21025 +                                     &a->pin, &a->wr_dir_args);
21026 +       err = PTR_ERR(wh_dentry);
21027 +       if (IS_ERR(wh_dentry))
21028 +               goto out_parent;
21029 +
21030 +       btop = au_dbtop(dentry);
21031 +       sb = dentry->d_sb;
21032 +       br = au_sbr(sb, btop);
21033 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21034 +       a->h_path.mnt = au_br_mnt(br);
21035 +       h_dir = au_pinned_h_dir(&a->pin);
21036 +       switch (arg->type) {
21037 +       case Creat:
21038 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
21039 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21040 +                                          arg->u.c.want_excl);
21041 +                       created = !err;
21042 +                       if (!err && try_aopen)
21043 +                               aopen->file->f_mode |= FMODE_CREATED;
21044 +               } else {
21045 +                       aopen->br = br;
21046 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21047 +                       AuDbg("err %d\n", err);
21048 +                       AuDbgFile(aopen->file);
21049 +                       created = err >= 0
21050 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
21051 +               }
21052 +               break;
21053 +       case Symlink:
21054 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
21055 +               created = !err;
21056 +               break;
21057 +       case Mknod:
21058 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21059 +                                 arg->u.m.dev);
21060 +               created = !err;
21061 +               break;
21062 +       default:
21063 +               BUG();
21064 +       }
21065 +       if (unlikely(err < 0))
21066 +               goto out_unpin;
21067 +
21068 +       err = epilog(dir, btop, wh_dentry, dentry);
21069 +       if (!err)
21070 +               goto out_unpin; /* success */
21071 +
21072 +       /* revert */
21073 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
21074 +               /* no delegation since it is just created */
21075 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21076 +                                   /*force*/0);
21077 +               if (rerr) {
21078 +                       AuIOErr("%pd revert failure(%d, %d)\n",
21079 +                               dentry, err, rerr);
21080 +                       err = -EIO;
21081 +               }
21082 +               au_dtime_revert(&a->dt);
21083 +       }
21084 +       if (try_aopen && h_dir->i_op->atomic_open
21085 +           && (aopen->file->f_mode & FMODE_OPENED))
21086 +               /* aopen->file is still opened */
21087 +               au_lcnt_dec(&aopen->br->br_nfiles);
21088 +
21089 +out_unpin:
21090 +       au_unpin(&a->pin);
21091 +       dput(wh_dentry);
21092 +out_parent:
21093 +       if (!try_aopen)
21094 +               di_write_unlock(parent);
21095 +out_unlock:
21096 +       if (unlikely(err)) {
21097 +               au_update_dbtop(dentry);
21098 +               d_drop(dentry);
21099 +       }
21100 +       if (!try_aopen)
21101 +               aufs_read_unlock(dentry, AuLock_DW);
21102 +out_free:
21103 +       au_kfree_rcu(a);
21104 +out:
21105 +       return err;
21106 +}
21107 +
21108 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
21109 +              struct dentry *dentry, umode_t mode, dev_t dev)
21110 +{
21111 +       struct simple_arg arg = {
21112 +               .type = Mknod,
21113 +               .u.m = {
21114 +                       .mode   = mode,
21115 +                       .dev    = dev
21116 +               }
21117 +       };
21118 +       return add_simple(dir, dentry, &arg);
21119 +}
21120 +
21121 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
21122 +                struct dentry *dentry, const char *symname)
21123 +{
21124 +       struct simple_arg arg = {
21125 +               .type = Symlink,
21126 +               .u.s.symname = symname
21127 +       };
21128 +       return add_simple(dir, dentry, &arg);
21129 +}
21130 +
21131 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
21132 +               struct dentry *dentry, umode_t mode, bool want_excl)
21133 +{
21134 +       struct simple_arg arg = {
21135 +               .type = Creat,
21136 +               .u.c = {
21137 +                       .mode           = mode,
21138 +                       .want_excl      = want_excl
21139 +               }
21140 +       };
21141 +       return add_simple(dir, dentry, &arg);
21142 +}
21143 +
21144 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21145 +                      struct vfsub_aopen_args *aopen_args)
21146 +{
21147 +       struct simple_arg arg = {
21148 +               .type = Creat,
21149 +               .u.c = {
21150 +                       .mode           = aopen_args->create_mode,
21151 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
21152 +                       .try_aopen      = true,
21153 +                       .aopen          = aopen_args
21154 +               }
21155 +       };
21156 +       return add_simple(dir, dentry, &arg);
21157 +}
21158 +
21159 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
21160 +                struct file *file, umode_t mode)
21161 +{
21162 +       int err;
21163 +       aufs_bindex_t bindex;
21164 +       struct path h_ppath;
21165 +       struct super_block *sb;
21166 +       struct au_branch *br;
21167 +       struct dentry *dentry, *parent, *h_parent, *h_dentry;
21168 +       struct inode *h_dir, *inode;
21169 +       struct vfsmount *h_mnt;
21170 +       struct mnt_idmap *h_idmap;
21171 +       struct file *h_file;
21172 +       struct au_wr_dir_args wr_dir_args = {
21173 +               .force_btgt     = -1,
21174 +               .flags          = AuWrDir_TMPFILE
21175 +       };
21176 +
21177 +       /* copy-up may happen */
21178 +       inode_lock(dir);
21179 +
21180 +       h_file = NULL;
21181 +       sb = dir->i_sb;
21182 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21183 +       if (unlikely(err))
21184 +               goto out;
21185 +
21186 +       dentry = file->f_path.dentry;
21187 +       err = au_di_init(dentry);
21188 +       if (unlikely(err))
21189 +               goto out_si;
21190 +
21191 +       err = -EBUSY;
21192 +       parent = d_find_any_alias(dir);
21193 +       AuDebugOn(!parent);
21194 +       di_write_lock_parent(parent);
21195 +       if (unlikely(d_inode(parent) != dir))
21196 +               goto out_parent;
21197 +
21198 +       err = au_digen_test(parent, au_sigen(sb));
21199 +       if (unlikely(err))
21200 +               goto out_parent;
21201 +
21202 +       bindex = au_dbtop(parent);
21203 +       au_set_dbtop(dentry, bindex);
21204 +       au_set_dbbot(dentry, bindex);
21205 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21206 +       bindex = err;
21207 +       if (unlikely(err < 0))
21208 +               goto out_parent;
21209 +
21210 +       err = -EOPNOTSUPP;
21211 +       h_dir = au_h_iptr(dir, bindex);
21212 +       if (unlikely(!h_dir->i_op->tmpfile))
21213 +               goto out_parent;
21214 +
21215 +       br = au_sbr(sb, bindex);
21216 +       h_mnt = au_br_mnt(br);
21217 +       err = vfsub_mnt_want_write(h_mnt);
21218 +       if (unlikely(err))
21219 +               goto out_parent;
21220 +
21221 +       h_idmap = mnt_idmap(h_mnt);
21222 +       h_parent = au_h_dptr(parent, bindex);
21223 +       h_ppath.mnt = h_mnt;
21224 +       h_ppath.dentry = h_parent;
21225 +       h_file = kernel_tmpfile_open(h_idmap, &h_ppath, mode, /*open_flag*/0,
21226 +                                    current_cred());
21227 +       if (IS_ERR(h_file)) {
21228 +               err = PTR_ERR(h_file);
21229 +               h_file = NULL;
21230 +               goto out_mnt;
21231 +       }
21232 +
21233 +       h_dentry = h_file->f_path.dentry;
21234 +       au_set_dbtop(dentry, bindex);
21235 +       au_set_dbbot(dentry, bindex);
21236 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
21237 +       inode = au_new_inode(dentry, /*must_new*/1);
21238 +       if (IS_ERR(inode)) {
21239 +               err = PTR_ERR(inode);
21240 +               au_set_h_dptr(dentry, bindex, NULL);
21241 +               au_set_dbtop(dentry, -1);
21242 +               au_set_dbbot(dentry, -1);
21243 +               goto out_h_file;
21244 +       }
21245 +
21246 +       if (!inode->i_nlink)
21247 +               set_nlink(inode, 1);
21248 +       d_tmpfile(file, inode);
21249 +       au_di(dentry)->di_tmpfile = 1;
21250 +       get_file(h_file);
21251 +       au_di(dentry)->di_htmpfile = h_file;
21252 +
21253 +       /* update without i_mutex */
21254 +       if (au_ibtop(dir) == au_dbtop(dentry))
21255 +               au_cpup_attr_timesizes(dir);
21256 +
21257 +out_h_file:
21258 +       fput(h_file);
21259 +out_mnt:
21260 +       vfsub_mnt_drop_write(h_mnt);
21261 +out_parent:
21262 +       di_write_unlock(parent);
21263 +       dput(parent);
21264 +       di_write_unlock(dentry);
21265 +       if (!err)
21266 +               goto out_si;
21267 +       if (h_file)
21268 +               fput(h_file);
21269 +       au_di(dentry)->di_htmpfile = NULL;
21270 +       au_di_fin(dentry);
21271 +       dentry->d_fsdata = NULL;
21272 +out_si:
21273 +       si_read_unlock(sb);
21274 +       if (!err && h_file) {
21275 +               /* finally... */
21276 +               err = finish_open_simple(file, err);
21277 +               if (!err)
21278 +                       au_lcnt_inc(&br->br_nfiles);
21279 +               else {
21280 +                       fput(h_file);
21281 +                       au_di(dentry)->di_htmpfile = NULL;
21282 +                       au_di_fin(dentry);
21283 +                       dentry->d_fsdata = NULL;
21284 +               }
21285 +       }
21286 +out:
21287 +       inode_unlock(dir);
21288 +       AuTraceErr(err);
21289 +       return err;
21290 +}
21291 +
21292 +/* ---------------------------------------------------------------------- */
21293 +
21294 +struct au_link_args {
21295 +       aufs_bindex_t bdst, bsrc;
21296 +       struct au_pin pin;
21297 +       struct path h_path;
21298 +       struct dentry *src_parent, *parent;
21299 +};
21300 +
21301 +static int au_cpup_before_link(struct dentry *src_dentry,
21302 +                              struct au_link_args *a)
21303 +{
21304 +       int err;
21305 +       struct dentry *h_src_dentry;
21306 +       struct au_cp_generic cpg = {
21307 +               .dentry = src_dentry,
21308 +               .bdst   = a->bdst,
21309 +               .bsrc   = a->bsrc,
21310 +               .len    = -1,
21311 +               .pin    = &a->pin,
21312 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21313 +       };
21314 +
21315 +       di_read_lock_parent(a->src_parent, AuLock_IR);
21316 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21317 +       if (unlikely(err))
21318 +               goto out;
21319 +
21320 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
21321 +       err = au_pin(&a->pin, src_dentry, a->bdst,
21322 +                    au_opt_udba(src_dentry->d_sb),
21323 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21324 +       if (unlikely(err))
21325 +               goto out;
21326 +
21327 +       err = au_sio_cpup_simple(&cpg);
21328 +       au_unpin(&a->pin);
21329 +
21330 +out:
21331 +       di_read_unlock(a->src_parent, AuLock_IR);
21332 +       return err;
21333 +}
21334 +
21335 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21336 +                          struct au_link_args *a)
21337 +{
21338 +       int err;
21339 +       unsigned char plink;
21340 +       aufs_bindex_t bbot;
21341 +       struct dentry *h_src_dentry;
21342 +       struct inode *h_inode, *inode, *delegated;
21343 +       struct super_block *sb;
21344 +       struct file *h_file;
21345 +
21346 +       plink = 0;
21347 +       h_inode = NULL;
21348 +       sb = src_dentry->d_sb;
21349 +       inode = d_inode(src_dentry);
21350 +       if (au_ibtop(inode) <= a->bdst)
21351 +               h_inode = au_h_iptr(inode, a->bdst);
21352 +       if (!h_inode || !h_inode->i_nlink) {
21353 +               /* copyup src_dentry as the name of dentry. */
21354 +               bbot = au_dbbot(dentry);
21355 +               if (bbot < a->bsrc)
21356 +                       au_set_dbbot(dentry, a->bsrc);
21357 +               au_set_h_dptr(dentry, a->bsrc,
21358 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
21359 +               dget(a->h_path.dentry);
21360 +               au_set_h_dptr(dentry, a->bdst, NULL);
21361 +               AuDbg("temporary d_inode...\n");
21362 +               spin_lock(&dentry->d_lock);
21363 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
21364 +               spin_unlock(&dentry->d_lock);
21365 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
21366 +               if (IS_ERR(h_file))
21367 +                       err = PTR_ERR(h_file);
21368 +               else {
21369 +                       struct au_cp_generic cpg = {
21370 +                               .dentry = dentry,
21371 +                               .bdst   = a->bdst,
21372 +                               .bsrc   = -1,
21373 +                               .len    = -1,
21374 +                               .pin    = &a->pin,
21375 +                               .flags  = AuCpup_KEEPLINO
21376 +                       };
21377 +                       err = au_sio_cpup_simple(&cpg);
21378 +                       au_h_open_post(dentry, a->bsrc, h_file);
21379 +                       if (!err) {
21380 +                               dput(a->h_path.dentry);
21381 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21382 +                       } else
21383 +                               au_set_h_dptr(dentry, a->bdst,
21384 +                                             a->h_path.dentry);
21385 +               }
21386 +               spin_lock(&dentry->d_lock);
21387 +               dentry->d_inode = NULL; /* restore */
21388 +               spin_unlock(&dentry->d_lock);
21389 +               AuDbg("temporary d_inode...done\n");
21390 +               au_set_h_dptr(dentry, a->bsrc, NULL);
21391 +               au_set_dbbot(dentry, bbot);
21392 +       } else {
21393 +               /* the inode of src_dentry already exists on a.bdst branch */
21394 +               h_src_dentry = d_find_alias(h_inode);
21395 +               if (!h_src_dentry && au_plink_test(inode)) {
21396 +                       plink = 1;
21397 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
21398 +                       err = PTR_ERR(h_src_dentry);
21399 +                       if (IS_ERR(h_src_dentry))
21400 +                               goto out;
21401 +
21402 +                       if (unlikely(d_is_negative(h_src_dentry))) {
21403 +                               dput(h_src_dentry);
21404 +                               h_src_dentry = NULL;
21405 +                       }
21406 +
21407 +               }
21408 +               if (h_src_dentry) {
21409 +                       delegated = NULL;
21410 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21411 +                                        &a->h_path, &delegated);
21412 +                       if (unlikely(err == -EWOULDBLOCK)) {
21413 +                               pr_warn("cannot retry for NFSv4 delegation"
21414 +                                       " for an internal link\n");
21415 +                               iput(delegated);
21416 +                       }
21417 +                       dput(h_src_dentry);
21418 +               } else {
21419 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
21420 +                               h_inode->i_ino, a->bdst);
21421 +                       err = -EIO;
21422 +               }
21423 +       }
21424 +
21425 +       if (!err && !plink)
21426 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
21427 +
21428 +out:
21429 +       AuTraceErr(err);
21430 +       return err;
21431 +}
21432 +
21433 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
21434 +             struct dentry *dentry)
21435 +{
21436 +       int err, rerr;
21437 +       struct au_dtime dt;
21438 +       struct au_link_args *a;
21439 +       struct dentry *wh_dentry, *h_src_dentry;
21440 +       struct inode *inode, *delegated;
21441 +       struct super_block *sb;
21442 +       struct au_wr_dir_args wr_dir_args = {
21443 +               /* .force_btgt  = -1, */
21444 +               .flags          = AuWrDir_ADD_ENTRY
21445 +       };
21446 +
21447 +       IMustLock(dir);
21448 +       inode = d_inode(src_dentry);
21449 +       IMustLock(inode);
21450 +
21451 +       err = -ENOMEM;
21452 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21453 +       if (unlikely(!a))
21454 +               goto out;
21455 +
21456 +       a->parent = dentry->d_parent; /* dir inode is locked */
21457 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
21458 +                                       AuLock_NOPLM | AuLock_GEN);
21459 +       if (unlikely(err))
21460 +               goto out_kfree;
21461 +       err = au_d_linkable(src_dentry);
21462 +       if (unlikely(err))
21463 +               goto out_unlock;
21464 +       err = au_d_may_add(dentry);
21465 +       if (unlikely(err))
21466 +               goto out_unlock;
21467 +
21468 +       a->src_parent = dget_parent(src_dentry);
21469 +       wr_dir_args.force_btgt = au_ibtop(inode);
21470 +
21471 +       di_write_lock_parent(a->parent);
21472 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21473 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21474 +                                     &wr_dir_args);
21475 +       err = PTR_ERR(wh_dentry);
21476 +       if (IS_ERR(wh_dentry))
21477 +               goto out_parent;
21478 +
21479 +       err = 0;
21480 +       sb = dentry->d_sb;
21481 +       a->bdst = au_dbtop(dentry);
21482 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21483 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21484 +       a->bsrc = au_ibtop(inode);
21485 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21486 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21487 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21488 +       if (!h_src_dentry) {
21489 +               a->bsrc = au_dbtop(src_dentry);
21490 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21491 +               AuDebugOn(!h_src_dentry);
21492 +       } else if (IS_ERR(h_src_dentry)) {
21493 +               err = PTR_ERR(h_src_dentry);
21494 +               goto out_parent;
21495 +       }
21496 +
21497 +       /*
21498 +        * aufs doesn't touch the credential so
21499 +        * security_dentry_create_files_as() is unnecessary.
21500 +        */
21501 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21502 +               if (a->bdst < a->bsrc
21503 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21504 +                       err = au_cpup_or_link(src_dentry, dentry, a);
21505 +               else {
21506 +                       delegated = NULL;
21507 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21508 +                                        &a->h_path, &delegated);
21509 +                       if (unlikely(err == -EWOULDBLOCK)) {
21510 +                               pr_warn("cannot retry for NFSv4 delegation"
21511 +                                       " for an internal link\n");
21512 +                               iput(delegated);
21513 +                       }
21514 +               }
21515 +               dput(h_src_dentry);
21516 +       } else {
21517 +               /*
21518 +                * copyup src_dentry to the branch we process,
21519 +                * and then link(2) to it.
21520 +                */
21521 +               dput(h_src_dentry);
21522 +               if (a->bdst < a->bsrc
21523 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21524 +                       au_unpin(&a->pin);
21525 +                       di_write_unlock(a->parent);
21526 +                       err = au_cpup_before_link(src_dentry, a);
21527 +                       di_write_lock_parent(a->parent);
21528 +                       if (!err)
21529 +                               err = au_pin(&a->pin, dentry, a->bdst,
21530 +                                            au_opt_udba(sb),
21531 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21532 +                       if (unlikely(err))
21533 +                               goto out_wh;
21534 +               }
21535 +               if (!err) {
21536 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21537 +                       err = -ENOENT;
21538 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
21539 +                               delegated = NULL;
21540 +                               err = vfsub_link(h_src_dentry,
21541 +                                                au_pinned_h_dir(&a->pin),
21542 +                                                &a->h_path, &delegated);
21543 +                               if (unlikely(err == -EWOULDBLOCK)) {
21544 +                                       pr_warn("cannot retry"
21545 +                                               " for NFSv4 delegation"
21546 +                                               " for an internal link\n");
21547 +                                       iput(delegated);
21548 +                               }
21549 +                       }
21550 +               }
21551 +       }
21552 +       if (unlikely(err))
21553 +               goto out_unpin;
21554 +
21555 +       if (wh_dentry) {
21556 +               a->h_path.dentry = wh_dentry;
21557 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21558 +                                         dentry);
21559 +               if (unlikely(err))
21560 +                       goto out_revert;
21561 +       }
21562 +
21563 +       au_dir_ts(dir, a->bdst);
21564 +       inode_inc_iversion(dir);
21565 +       inc_nlink(inode);
21566 +       inode->i_ctime = dir->i_ctime;
21567 +       d_instantiate(dentry, au_igrab(inode));
21568 +       if (d_unhashed(a->h_path.dentry))
21569 +               /* some filesystem calls d_drop() */
21570 +               d_drop(dentry);
21571 +       /* some filesystems consume an inode even hardlink */
21572 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
21573 +       goto out_unpin; /* success */
21574 +
21575 +out_revert:
21576 +       /* no delegation since it is just created */
21577 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21578 +                           /*delegated*/NULL, /*force*/0);
21579 +       if (unlikely(rerr)) {
21580 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21581 +               err = -EIO;
21582 +       }
21583 +       au_dtime_revert(&dt);
21584 +out_unpin:
21585 +       au_unpin(&a->pin);
21586 +out_wh:
21587 +       dput(wh_dentry);
21588 +out_parent:
21589 +       di_write_unlock(a->parent);
21590 +       dput(a->src_parent);
21591 +out_unlock:
21592 +       if (unlikely(err)) {
21593 +               au_update_dbtop(dentry);
21594 +               d_drop(dentry);
21595 +       }
21596 +       aufs_read_and_write_unlock2(dentry, src_dentry);
21597 +out_kfree:
21598 +       au_kfree_rcu(a);
21599 +out:
21600 +       AuTraceErr(err);
21601 +       return err;
21602 +}
21603 +
21604 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
21605 +              struct dentry *dentry, umode_t mode)
21606 +{
21607 +       int err, rerr;
21608 +       aufs_bindex_t bindex;
21609 +       unsigned char diropq;
21610 +       struct path h_path;
21611 +       struct dentry *wh_dentry, *parent, *opq_dentry;
21612 +       struct inode *h_inode;
21613 +       struct super_block *sb;
21614 +       struct {
21615 +               struct au_pin pin;
21616 +               struct au_dtime dt;
21617 +       } *a; /* reduce the stack usage */
21618 +       struct au_wr_dir_args wr_dir_args = {
21619 +               .force_btgt     = -1,
21620 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21621 +       };
21622 +
21623 +       IMustLock(dir);
21624 +
21625 +       err = -ENOMEM;
21626 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21627 +       if (unlikely(!a))
21628 +               goto out;
21629 +
21630 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21631 +       if (unlikely(err))
21632 +               goto out_free;
21633 +       err = au_d_may_add(dentry);
21634 +       if (unlikely(err))
21635 +               goto out_unlock;
21636 +
21637 +       parent = dentry->d_parent; /* dir inode is locked */
21638 +       di_write_lock_parent(parent);
21639 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21640 +                                     &a->pin, &wr_dir_args);
21641 +       err = PTR_ERR(wh_dentry);
21642 +       if (IS_ERR(wh_dentry))
21643 +               goto out_parent;
21644 +
21645 +       sb = dentry->d_sb;
21646 +       bindex = au_dbtop(dentry);
21647 +       h_path.dentry = au_h_dptr(dentry, bindex);
21648 +       h_path.mnt = au_sbr_mnt(sb, bindex);
21649 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21650 +       if (unlikely(err))
21651 +               goto out_unpin;
21652 +
21653 +       /* make the dir opaque */
21654 +       diropq = 0;
21655 +       h_inode = d_inode(h_path.dentry);
21656 +       if (wh_dentry
21657 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21658 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21659 +               opq_dentry = au_diropq_create(dentry, bindex);
21660 +               inode_unlock(h_inode);
21661 +               err = PTR_ERR(opq_dentry);
21662 +               if (IS_ERR(opq_dentry))
21663 +                       goto out_dir;
21664 +               dput(opq_dentry);
21665 +               diropq = 1;
21666 +       }
21667 +
21668 +       err = epilog(dir, bindex, wh_dentry, dentry);
21669 +       if (!err) {
21670 +               inc_nlink(dir);
21671 +               goto out_unpin; /* success */
21672 +       }
21673 +
21674 +       /* revert */
21675 +       if (diropq) {
21676 +               AuLabel(revert opq);
21677 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21678 +               rerr = au_diropq_remove(dentry, bindex);
21679 +               inode_unlock(h_inode);
21680 +               if (rerr) {
21681 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21682 +                               dentry, err, rerr);
21683 +                       err = -EIO;
21684 +               }
21685 +       }
21686 +
21687 +out_dir:
21688 +       AuLabel(revert dir);
21689 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21690 +       if (rerr) {
21691 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
21692 +                       dentry, err, rerr);
21693 +               err = -EIO;
21694 +       }
21695 +       au_dtime_revert(&a->dt);
21696 +out_unpin:
21697 +       au_unpin(&a->pin);
21698 +       dput(wh_dentry);
21699 +out_parent:
21700 +       di_write_unlock(parent);
21701 +out_unlock:
21702 +       if (unlikely(err)) {
21703 +               au_update_dbtop(dentry);
21704 +               d_drop(dentry);
21705 +       }
21706 +       aufs_read_unlock(dentry, AuLock_DW);
21707 +out_free:
21708 +       au_kfree_rcu(a);
21709 +out:
21710 +       return err;
21711 +}
21712 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
21713 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
21714 +++ linux/fs/aufs/i_op.c        2023-09-03 02:21:58.859971007 +0200
21715 @@ -0,0 +1,1517 @@
21716 +// SPDX-License-Identifier: GPL-2.0
21717 +/*
21718 + * Copyright (C) 2005-2022 Junjiro R. Okajima
21719 + *
21720 + * This program is free software; you can redistribute it and/or modify
21721 + * it under the terms of the GNU General Public License as published by
21722 + * the Free Software Foundation; either version 2 of the License, or
21723 + * (at your option) any later version.
21724 + *
21725 + * This program is distributed in the hope that it will be useful,
21726 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21727 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21728 + * GNU General Public License for more details.
21729 + *
21730 + * You should have received a copy of the GNU General Public License
21731 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21732 + */
21733 +
21734 +/*
21735 + * inode operations (except add/del/rename)
21736 + */
21737 +
21738 +#include <linux/device_cgroup.h>
21739 +#include <linux/filelock.h>
21740 +#include <linux/fs_stack.h>
21741 +#include <linux/iversion.h>
21742 +#include <linux/security.h>
21743 +#include "aufs.h"
21744 +
21745 +static int h_permission(struct inode *h_inode, int mask,
21746 +                       struct path *h_path, int brperm)
21747 +{
21748 +       int err;
21749 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21750 +       struct mnt_idmap *h_idmap;
21751 +
21752 +       err = -EPERM;
21753 +       if (write_mask && IS_IMMUTABLE(h_inode))
21754 +               goto out;
21755 +
21756 +       err = -EACCES;
21757 +       if (((mask & MAY_EXEC)
21758 +            && S_ISREG(h_inode->i_mode)
21759 +            && (path_noexec(h_path)
21760 +                || !(h_inode->i_mode & 0111))))
21761 +               goto out;
21762 +
21763 +       /*
21764 +        * - skip the lower fs test in the case of write to ro branch.
21765 +        * - nfs dir permission write check is optimized, but a policy for
21766 +        *   link/rename requires a real check.
21767 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
21768 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
21769 +        */
21770 +       h_idmap = mnt_idmap(h_path->mnt);
21771 +       if ((write_mask && !au_br_writable(brperm))
21772 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21773 +               && write_mask && !(mask & MAY_READ))
21774 +           || !h_inode->i_op->permission) {
21775 +               /* AuLabel(generic_permission); */
21776 +               /* AuDbg("get_inode_acl %ps\n",
21777 +                  h_inode->i_op->get_inode_acl); */
21778 +               err = generic_permission(h_idmap, h_inode, mask);
21779 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
21780 +                       err = h_inode->i_op->permission(h_idmap, h_inode,
21781 +                                                       mask);
21782 +               AuTraceErr(err);
21783 +       } else {
21784 +               /* AuLabel(h_inode->permission); */
21785 +               err = h_inode->i_op->permission(h_idmap, h_inode, mask);
21786 +               AuTraceErr(err);
21787 +       }
21788 +
21789 +       if (!err)
21790 +               err = devcgroup_inode_permission(h_inode, mask);
21791 +       if (!err)
21792 +               err = security_inode_permission(h_inode, mask);
21793 +
21794 +out:
21795 +       return err;
21796 +}
21797 +
21798 +static int aufs_permission(struct mnt_idmap *idmap, struct inode *inode,
21799 +                          int mask)
21800 +{
21801 +       int err;
21802 +       aufs_bindex_t bindex, bbot;
21803 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21804 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21805 +       struct inode *h_inode;
21806 +       struct super_block *sb;
21807 +       struct au_branch *br;
21808 +
21809 +       /* todo: support rcu-walk? */
21810 +       if (mask & MAY_NOT_BLOCK)
21811 +               return -ECHILD;
21812 +
21813 +       sb = inode->i_sb;
21814 +       si_read_lock(sb, AuLock_FLUSH);
21815 +       ii_read_lock_child(inode);
21816 +#if 0 /* reserved for future use */
21817 +       /*
21818 +        * This test may be rather 'too much' since the test is essentially done
21819 +        * in the aufs_lookup().  Theoretically it is possible that the inode
21820 +        * generation doesn't match to the superblock's here.  But it isn't a
21821 +        * big deal I suppose.
21822 +        */
21823 +       err = au_iigen_test(inode, au_sigen(sb));
21824 +       if (unlikely(err))
21825 +               goto out;
21826 +#endif
21827 +
21828 +       if (!isdir
21829 +           || write_mask
21830 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
21831 +               err = au_busy_or_stale();
21832 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
21833 +               if (unlikely(!h_inode
21834 +                            || (h_inode->i_mode & S_IFMT)
21835 +                            != (inode->i_mode & S_IFMT)))
21836 +                       goto out;
21837 +
21838 +               err = 0;
21839 +               bindex = au_ibtop(inode);
21840 +               br = au_sbr(sb, bindex);
21841 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
21842 +               if (write_mask
21843 +                   && !err
21844 +                   && !special_file(h_inode->i_mode)) {
21845 +                       /* test whether the upper writable branch exists */
21846 +                       err = -EROFS;
21847 +                       for (; bindex >= 0; bindex--)
21848 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
21849 +                                       err = 0;
21850 +                                       break;
21851 +                               }
21852 +               }
21853 +               goto out;
21854 +       }
21855 +
21856 +       /* non-write to dir */
21857 +       err = 0;
21858 +       bbot = au_ibbot(inode);
21859 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
21860 +               h_inode = au_h_iptr(inode, bindex);
21861 +               if (h_inode) {
21862 +                       err = au_busy_or_stale();
21863 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
21864 +                               break;
21865 +
21866 +                       br = au_sbr(sb, bindex);
21867 +                       err = h_permission(h_inode, mask, &br->br_path,
21868 +                                          br->br_perm);
21869 +               }
21870 +       }
21871 +
21872 +out:
21873 +       ii_read_unlock(inode);
21874 +       si_read_unlock(sb);
21875 +       return err;
21876 +}
21877 +
21878 +/* ---------------------------------------------------------------------- */
21879 +
21880 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
21881 +                                 unsigned int flags)
21882 +{
21883 +       struct dentry *ret, *parent;
21884 +       struct inode *inode;
21885 +       struct super_block *sb;
21886 +       int err, npositive;
21887 +
21888 +       IMustLock(dir);
21889 +
21890 +       /* todo: support rcu-walk? */
21891 +       ret = ERR_PTR(-ECHILD);
21892 +       if (flags & LOOKUP_RCU)
21893 +               goto out;
21894 +
21895 +       ret = ERR_PTR(-ENAMETOOLONG);
21896 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21897 +               goto out;
21898 +
21899 +       sb = dir->i_sb;
21900 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21901 +       ret = ERR_PTR(err);
21902 +       if (unlikely(err))
21903 +               goto out;
21904 +
21905 +       err = au_di_init(dentry);
21906 +       ret = ERR_PTR(err);
21907 +       if (unlikely(err))
21908 +               goto out_si;
21909 +
21910 +       inode = NULL;
21911 +       npositive = 0; /* suppress a warning */
21912 +       parent = dentry->d_parent; /* dir inode is locked */
21913 +       di_read_lock_parent(parent, AuLock_IR);
21914 +       err = au_alive_dir(parent);
21915 +       if (!err)
21916 +               err = au_digen_test(parent, au_sigen(sb));
21917 +       if (!err) {
21918 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21919 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21920 +                                          AuLkup_ALLOW_NEG);
21921 +               err = npositive;
21922 +       }
21923 +       di_read_unlock(parent, AuLock_IR);
21924 +       ret = ERR_PTR(err);
21925 +       if (unlikely(err < 0))
21926 +               goto out_unlock;
21927 +
21928 +       if (npositive) {
21929 +               inode = au_new_inode(dentry, /*must_new*/0);
21930 +               if (IS_ERR(inode)) {
21931 +                       ret = (void *)inode;
21932 +                       inode = NULL;
21933 +                       goto out_unlock;
21934 +               }
21935 +       }
21936 +
21937 +       if (inode)
21938 +               atomic_inc(&inode->i_count);
21939 +       ret = d_splice_alias(inode, dentry);
21940 +#if 0 /* reserved for future use */
21941 +       if (unlikely(d_need_lookup(dentry))) {
21942 +               spin_lock(&dentry->d_lock);
21943 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21944 +               spin_unlock(&dentry->d_lock);
21945 +       } else
21946 +#endif
21947 +       if (inode) {
21948 +               if (!IS_ERR(ret)) {
21949 +                       iput(inode);
21950 +                       if (ret && ret != dentry)
21951 +                               ii_write_unlock(inode);
21952 +               } else {
21953 +                       ii_write_unlock(inode);
21954 +                       iput(inode);
21955 +                       inode = NULL;
21956 +               }
21957 +       }
21958 +
21959 +out_unlock:
21960 +       di_write_unlock(dentry);
21961 +out_si:
21962 +       si_read_unlock(sb);
21963 +out:
21964 +       return ret;
21965 +}
21966 +
21967 +/* ---------------------------------------------------------------------- */
21968 +
21969 +/*
21970 + * very dirty and complicated aufs ->atomic_open().
21971 + * aufs_atomic_open()
21972 + * + au_aopen_or_create()
21973 + *   + add_simple()
21974 + *     + vfsub_atomic_open()
21975 + *       + branch fs ->atomic_open()
21976 + *        may call the actual 'open' for h_file
21977 + *       + inc br_nfiles only if opened
21978 + * + au_aopen_no_open() or au_aopen_do_open()
21979 + *
21980 + * au_aopen_do_open()
21981 + * + finish_open()
21982 + *   + au_do_aopen()
21983 + *     + au_do_open() the body of all 'open'
21984 + *       + au_do_open_nondir()
21985 + *        set the passed h_file
21986 + *
21987 + * au_aopen_no_open()
21988 + * + finish_no_open()
21989 + */
21990 +
21991 +struct aopen_node {
21992 +       struct hlist_bl_node hblist;
21993 +       struct file *file, *h_file;
21994 +};
21995 +
21996 +static int au_do_aopen(struct inode *inode, struct file *file)
21997 +{
21998 +       struct hlist_bl_head *aopen;
21999 +       struct hlist_bl_node *pos;
22000 +       struct aopen_node *node;
22001 +       struct au_do_open_args args = {
22002 +               .aopen  = 1,
22003 +               .open   = au_do_open_nondir
22004 +       };
22005 +
22006 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
22007 +       hlist_bl_lock(aopen);
22008 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
22009 +               if (node->file == file) {
22010 +                       args.h_file = node->h_file;
22011 +                       break;
22012 +               }
22013 +       hlist_bl_unlock(aopen);
22014 +       /* AuDebugOn(!args.h_file); */
22015 +
22016 +       return au_do_open(file, &args);
22017 +}
22018 +
22019 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
22020 +                           struct aopen_node *aopen_node)
22021 +{
22022 +       int err;
22023 +       struct hlist_bl_head *aopen;
22024 +
22025 +       AuLabel(here);
22026 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
22027 +       au_hbl_add(&aopen_node->hblist, aopen);
22028 +       err = finish_open(file, dentry, au_do_aopen);
22029 +       au_hbl_del(&aopen_node->hblist, aopen);
22030 +       /* AuDbgFile(file); */
22031 +       AuDbg("%pd%s%s\n", dentry,
22032 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
22033 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
22034 +
22035 +       AuTraceErr(err);
22036 +       return err;
22037 +}
22038 +
22039 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
22040 +{
22041 +       int err;
22042 +
22043 +       AuLabel(here);
22044 +       dget(dentry);
22045 +       err = finish_no_open(file, dentry);
22046 +
22047 +       AuTraceErr(err);
22048 +       return err;
22049 +}
22050 +
22051 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22052 +                           struct file *file, unsigned int open_flag,
22053 +                           umode_t create_mode)
22054 +{
22055 +       int err, did_open;
22056 +       unsigned int lkup_flags;
22057 +       aufs_bindex_t bindex;
22058 +       struct super_block *sb;
22059 +       struct dentry *parent, *d;
22060 +       struct vfsub_aopen_args args = {
22061 +               .open_flag      = open_flag,
22062 +               .create_mode    = create_mode
22063 +       };
22064 +       struct aopen_node aopen_node = {
22065 +               .file   = file
22066 +       };
22067 +
22068 +       IMustLock(dir);
22069 +       AuDbg("open_flag 0%o\n", open_flag);
22070 +       AuDbgDentry(dentry);
22071 +
22072 +       err = 0;
22073 +       if (!au_di(dentry)) {
22074 +               lkup_flags = LOOKUP_OPEN;
22075 +               if (open_flag & O_CREAT)
22076 +                       lkup_flags |= LOOKUP_CREATE;
22077 +               d = aufs_lookup(dir, dentry, lkup_flags);
22078 +               if (IS_ERR(d)) {
22079 +                       err = PTR_ERR(d);
22080 +                       AuTraceErr(err);
22081 +                       goto out;
22082 +               } else if (d) {
22083 +                       /*
22084 +                        * obsoleted dentry found.
22085 +                        * another error will be returned later.
22086 +                        */
22087 +                       d_drop(d);
22088 +                       AuDbgDentry(d);
22089 +                       dput(d);
22090 +               }
22091 +               AuDbgDentry(dentry);
22092 +       }
22093 +
22094 +       if (d_is_positive(dentry)
22095 +           || d_unhashed(dentry)
22096 +           || d_unlinked(dentry)
22097 +           || !(open_flag & O_CREAT)) {
22098 +               err = au_aopen_no_open(file, dentry);
22099 +               goto out; /* success */
22100 +       }
22101 +
22102 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22103 +       if (unlikely(err))
22104 +               goto out;
22105 +
22106 +       sb = dentry->d_sb;
22107 +       parent = dentry->d_parent;      /* dir is locked */
22108 +       di_write_lock_parent(parent);
22109 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
22110 +       if (unlikely(err < 0))
22111 +               goto out_parent;
22112 +
22113 +       AuDbgDentry(dentry);
22114 +       if (d_is_positive(dentry)) {
22115 +               err = au_aopen_no_open(file, dentry);
22116 +               goto out_parent; /* success */
22117 +       }
22118 +
22119 +       args.file = alloc_empty_file(file->f_flags, current_cred());
22120 +       err = PTR_ERR(args.file);
22121 +       if (IS_ERR(args.file))
22122 +               goto out_parent;
22123 +
22124 +       bindex = au_dbtop(dentry);
22125 +       err = au_aopen_or_create(dir, dentry, &args);
22126 +       AuTraceErr(err);
22127 +       AuDbgFile(args.file);
22128 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22129 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
22130 +       if (!did_open) {
22131 +               fput(args.file);
22132 +               args.file = NULL;
22133 +       }
22134 +       di_write_unlock(parent);
22135 +       di_write_unlock(dentry);
22136 +       if (unlikely(err < 0)) {
22137 +               if (args.file)
22138 +                       fput(args.file);
22139 +               goto out_sb;
22140 +       }
22141 +
22142 +       if (!did_open)
22143 +               err = au_aopen_no_open(file, dentry);
22144 +       else {
22145 +               aopen_node.h_file = args.file;
22146 +               err = au_aopen_do_open(file, dentry, &aopen_node);
22147 +       }
22148 +       if (unlikely(err < 0)) {
22149 +               if (args.file)
22150 +                       fput(args.file);
22151 +               if (did_open)
22152 +                       au_lcnt_dec(&args.br->br_nfiles);
22153 +       }
22154 +       goto out_sb; /* success */
22155 +
22156 +out_parent:
22157 +       di_write_unlock(parent);
22158 +       di_write_unlock(dentry);
22159 +out_sb:
22160 +       si_read_unlock(sb);
22161 +out:
22162 +       AuTraceErr(err);
22163 +       AuDbgFile(file);
22164 +       return err;
22165 +}
22166 +
22167 +
22168 +/* ---------------------------------------------------------------------- */
22169 +
22170 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22171 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
22172 +                         aufs_bindex_t btop)
22173 +{
22174 +       int err;
22175 +       struct dentry *h_parent;
22176 +       struct inode *h_dir;
22177 +
22178 +       if (add_entry)
22179 +               IMustLock(d_inode(parent));
22180 +       else
22181 +               di_write_lock_parent(parent);
22182 +
22183 +       err = 0;
22184 +       if (!au_h_dptr(parent, bcpup)) {
22185 +               if (btop > bcpup)
22186 +                       err = au_cpup_dirs(dentry, bcpup);
22187 +               else if (btop < bcpup)
22188 +                       err = au_cpdown_dirs(dentry, bcpup);
22189 +               else
22190 +                       BUG();
22191 +       }
22192 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
22193 +               h_parent = au_h_dptr(parent, bcpup);
22194 +               h_dir = d_inode(h_parent);
22195 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
22196 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
22197 +               /* todo: no unlock here */
22198 +               inode_unlock_shared(h_dir);
22199 +
22200 +               AuDbg("bcpup %d\n", bcpup);
22201 +               if (!err) {
22202 +                       if (d_really_is_negative(dentry))
22203 +                               au_set_h_dptr(dentry, btop, NULL);
22204 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
22205 +               }
22206 +       }
22207 +
22208 +       if (!add_entry)
22209 +               di_write_unlock(parent);
22210 +       if (!err)
22211 +               err = bcpup; /* success */
22212 +
22213 +       AuTraceErr(err);
22214 +       return err;
22215 +}
22216 +
22217 +/*
22218 + * decide the branch and the parent dir where we will create a new entry.
22219 + * returns new bindex or an error.
22220 + * copyup the parent dir if needed.
22221 + */
22222 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22223 +             struct au_wr_dir_args *args)
22224 +{
22225 +       int err;
22226 +       unsigned int flags;
22227 +       aufs_bindex_t bcpup, btop, src_btop;
22228 +       const unsigned char add_entry
22229 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
22230 +               | au_ftest_wrdir(args->flags, TMPFILE);
22231 +       struct super_block *sb;
22232 +       struct dentry *parent;
22233 +       struct au_sbinfo *sbinfo;
22234 +
22235 +       sb = dentry->d_sb;
22236 +       sbinfo = au_sbi(sb);
22237 +       parent = dget_parent(dentry);
22238 +       btop = au_dbtop(dentry);
22239 +       bcpup = btop;
22240 +       if (args->force_btgt < 0) {
22241 +               if (src_dentry) {
22242 +                       src_btop = au_dbtop(src_dentry);
22243 +                       if (src_btop < btop)
22244 +                               bcpup = src_btop;
22245 +               } else if (add_entry) {
22246 +                       flags = 0;
22247 +                       if (au_ftest_wrdir(args->flags, ISDIR))
22248 +                               au_fset_wbr(flags, DIR);
22249 +                       err = AuWbrCreate(sbinfo, dentry, flags);
22250 +                       bcpup = err;
22251 +               }
22252 +
22253 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
22254 +                       if (add_entry)
22255 +                               err = AuWbrCopyup(sbinfo, dentry);
22256 +                       else {
22257 +                               if (!IS_ROOT(dentry)) {
22258 +                                       di_read_lock_parent(parent, !AuLock_IR);
22259 +                                       err = AuWbrCopyup(sbinfo, dentry);
22260 +                                       di_read_unlock(parent, !AuLock_IR);
22261 +                               } else
22262 +                                       err = AuWbrCopyup(sbinfo, dentry);
22263 +                       }
22264 +                       bcpup = err;
22265 +                       if (unlikely(err < 0))
22266 +                               goto out;
22267 +               }
22268 +       } else {
22269 +               bcpup = args->force_btgt;
22270 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
22271 +       }
22272 +
22273 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
22274 +       err = bcpup;
22275 +       if (bcpup == btop)
22276 +               goto out; /* success */
22277 +
22278 +       /* copyup the new parent into the branch we process */
22279 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
22280 +       if (err >= 0) {
22281 +               if (d_really_is_negative(dentry)) {
22282 +                       au_set_h_dptr(dentry, btop, NULL);
22283 +                       au_set_dbtop(dentry, bcpup);
22284 +                       au_set_dbbot(dentry, bcpup);
22285 +               }
22286 +               AuDebugOn(add_entry
22287 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
22288 +                         && !au_h_dptr(dentry, bcpup));
22289 +       }
22290 +
22291 +out:
22292 +       dput(parent);
22293 +       return err;
22294 +}
22295 +
22296 +/* ---------------------------------------------------------------------- */
22297 +
22298 +void au_pin_hdir_unlock(struct au_pin *p)
22299 +{
22300 +       if (p->hdir)
22301 +               au_hn_inode_unlock(p->hdir);
22302 +}
22303 +
22304 +int au_pin_hdir_lock(struct au_pin *p)
22305 +{
22306 +       int err;
22307 +
22308 +       err = 0;
22309 +       if (!p->hdir)
22310 +               goto out;
22311 +
22312 +       /* even if an error happens later, keep this lock */
22313 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
22314 +
22315 +       err = -EBUSY;
22316 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
22317 +               goto out;
22318 +
22319 +       err = 0;
22320 +       if (p->h_dentry)
22321 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22322 +                                 p->h_parent, p->br);
22323 +
22324 +out:
22325 +       return err;
22326 +}
22327 +
22328 +int au_pin_hdir_relock(struct au_pin *p)
22329 +{
22330 +       int err, i;
22331 +       struct inode *h_i;
22332 +       struct dentry *h_d[] = {
22333 +               p->h_dentry,
22334 +               p->h_parent
22335 +       };
22336 +
22337 +       err = au_pin_hdir_lock(p);
22338 +       if (unlikely(err))
22339 +               goto out;
22340 +
22341 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22342 +               if (!h_d[i])
22343 +                       continue;
22344 +               if (d_is_positive(h_d[i])) {
22345 +                       h_i = d_inode(h_d[i]);
22346 +                       err = !h_i->i_nlink;
22347 +               }
22348 +       }
22349 +
22350 +out:
22351 +       return err;
22352 +}
22353 +
22354 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
22355 +{
22356 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
22357 +}
22358 +
22359 +void au_pin_hdir_acquire_nest(struct au_pin *p)
22360 +{
22361 +       if (p->hdir) {
22362 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
22363 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
22364 +               au_pin_hdir_set_owner(p, current);
22365 +       }
22366 +}
22367 +
22368 +void au_pin_hdir_release(struct au_pin *p)
22369 +{
22370 +       if (p->hdir) {
22371 +               au_pin_hdir_set_owner(p, p->task);
22372 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
22373 +       }
22374 +}
22375 +
22376 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
22377 +{
22378 +       if (pin && pin->parent)
22379 +               return au_h_dptr(pin->parent, pin->bindex);
22380 +       return NULL;
22381 +}
22382 +
22383 +void au_unpin(struct au_pin *p)
22384 +{
22385 +       if (p->hdir)
22386 +               au_pin_hdir_unlock(p);
22387 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
22388 +               vfsub_mnt_drop_write(p->h_mnt);
22389 +       if (!p->hdir)
22390 +               return;
22391 +
22392 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22393 +               di_read_unlock(p->parent, AuLock_IR);
22394 +       iput(p->hdir->hi_inode);
22395 +       dput(p->parent);
22396 +       p->parent = NULL;
22397 +       p->hdir = NULL;
22398 +       p->h_mnt = NULL;
22399 +       /* do not clear p->task */
22400 +}
22401 +
22402 +int au_do_pin(struct au_pin *p)
22403 +{
22404 +       int err;
22405 +       struct super_block *sb;
22406 +       struct inode *h_dir;
22407 +
22408 +       err = 0;
22409 +       sb = p->dentry->d_sb;
22410 +       p->br = au_sbr(sb, p->bindex);
22411 +       if (IS_ROOT(p->dentry)) {
22412 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
22413 +                       p->h_mnt = au_br_mnt(p->br);
22414 +                       err = vfsub_mnt_want_write(p->h_mnt);
22415 +                       if (unlikely(err)) {
22416 +                               au_fclr_pin(p->flags, MNT_WRITE);
22417 +                               goto out_err;
22418 +                       }
22419 +               }
22420 +               goto out;
22421 +       }
22422 +
22423 +       p->h_dentry = NULL;
22424 +       if (p->bindex <= au_dbbot(p->dentry))
22425 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
22426 +
22427 +       p->parent = dget_parent(p->dentry);
22428 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22429 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
22430 +
22431 +       h_dir = NULL;
22432 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
22433 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
22434 +       if (p->hdir)
22435 +               h_dir = p->hdir->hi_inode;
22436 +
22437 +       /*
22438 +        * udba case, or
22439 +        * if DI_LOCKED is not set, then p->parent may be different
22440 +        * and h_parent can be NULL.
22441 +        */
22442 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
22443 +               err = -EBUSY;
22444 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
22445 +                       di_read_unlock(p->parent, AuLock_IR);
22446 +               dput(p->parent);
22447 +               p->parent = NULL;
22448 +               goto out_err;
22449 +       }
22450 +
22451 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
22452 +               p->h_mnt = au_br_mnt(p->br);
22453 +               err = vfsub_mnt_want_write(p->h_mnt);
22454 +               if (unlikely(err)) {
22455 +                       au_fclr_pin(p->flags, MNT_WRITE);
22456 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
22457 +                               di_read_unlock(p->parent, AuLock_IR);
22458 +                       dput(p->parent);
22459 +                       p->parent = NULL;
22460 +                       goto out_err;
22461 +               }
22462 +       }
22463 +
22464 +       au_igrab(h_dir);
22465 +       err = au_pin_hdir_lock(p);
22466 +       if (!err)
22467 +               goto out; /* success */
22468 +
22469 +       au_unpin(p);
22470 +
22471 +out_err:
22472 +       pr_err("err %d\n", err);
22473 +       err = au_busy_or_stale();
22474 +out:
22475 +       return err;
22476 +}
22477 +
22478 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
22479 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22480 +                unsigned int udba, unsigned char flags)
22481 +{
22482 +       p->dentry = dentry;
22483 +       p->udba = udba;
22484 +       p->lsc_di = lsc_di;
22485 +       p->lsc_hi = lsc_hi;
22486 +       p->flags = flags;
22487 +       p->bindex = bindex;
22488 +
22489 +       p->parent = NULL;
22490 +       p->hdir = NULL;
22491 +       p->h_mnt = NULL;
22492 +
22493 +       p->h_dentry = NULL;
22494 +       p->h_parent = NULL;
22495 +       p->br = NULL;
22496 +       p->task = current;
22497 +}
22498 +
22499 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22500 +          unsigned int udba, unsigned char flags)
22501 +{
22502 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22503 +                   udba, flags);
22504 +       return au_do_pin(pin);
22505 +}
22506 +
22507 +/* ---------------------------------------------------------------------- */
22508 +
22509 +/*
22510 + * ->setattr() and ->getattr() are called in various cases.
22511 + * chmod, stat: dentry is revalidated.
22512 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22513 + *               unhashed.
22514 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
22515 + */
22516 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
22517 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
22518 +{
22519 +       int err;
22520 +       struct dentry *parent;
22521 +
22522 +       err = 0;
22523 +       if (au_digen_test(dentry, sigen)) {
22524 +               parent = dget_parent(dentry);
22525 +               di_read_lock_parent(parent, AuLock_IR);
22526 +               err = au_refresh_dentry(dentry, parent);
22527 +               di_read_unlock(parent, AuLock_IR);
22528 +               dput(parent);
22529 +       }
22530 +
22531 +       AuTraceErr(err);
22532 +       return err;
22533 +}
22534 +
22535 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22536 +                    struct au_icpup_args *a)
22537 +{
22538 +       int err;
22539 +       loff_t sz;
22540 +       aufs_bindex_t btop, ibtop;
22541 +       struct dentry *hi_wh, *parent;
22542 +       struct inode *inode;
22543 +       struct au_wr_dir_args wr_dir_args = {
22544 +               .force_btgt     = -1,
22545 +               .flags          = 0
22546 +       };
22547 +
22548 +       if (d_is_dir(dentry))
22549 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
22550 +       /* plink or hi_wh() case */
22551 +       btop = au_dbtop(dentry);
22552 +       inode = d_inode(dentry);
22553 +       ibtop = au_ibtop(inode);
22554 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22555 +               wr_dir_args.force_btgt = ibtop;
22556 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22557 +       if (unlikely(err < 0))
22558 +               goto out;
22559 +       a->btgt = err;
22560 +       if (err != btop)
22561 +               au_fset_icpup(a->flags, DID_CPUP);
22562 +
22563 +       err = 0;
22564 +       a->pin_flags = AuPin_MNT_WRITE;
22565 +       parent = NULL;
22566 +       if (!IS_ROOT(dentry)) {
22567 +               au_fset_pin(a->pin_flags, DI_LOCKED);
22568 +               parent = dget_parent(dentry);
22569 +               di_write_lock_parent(parent);
22570 +       }
22571 +
22572 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22573 +       if (unlikely(err))
22574 +               goto out_parent;
22575 +
22576 +       sz = -1;
22577 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22578 +       a->h_inode = d_inode(a->h_path.dentry);
22579 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
22580 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
22581 +               if (ia->ia_size < i_size_read(a->h_inode))
22582 +                       sz = ia->ia_size;
22583 +               inode_unlock_shared(a->h_inode);
22584 +       }
22585 +
22586 +       hi_wh = NULL;
22587 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
22588 +               hi_wh = au_hi_wh(inode, a->btgt);
22589 +               if (!hi_wh) {
22590 +                       struct au_cp_generic cpg = {
22591 +                               .dentry = dentry,
22592 +                               .bdst   = a->btgt,
22593 +                               .bsrc   = -1,
22594 +                               .len    = sz,
22595 +                               .pin    = &a->pin
22596 +                       };
22597 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
22598 +                       if (unlikely(err))
22599 +                               goto out_unlock;
22600 +                       hi_wh = au_hi_wh(inode, a->btgt);
22601 +                       /* todo: revalidate hi_wh? */
22602 +               }
22603 +       }
22604 +
22605 +       if (parent) {
22606 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22607 +               di_downgrade_lock(parent, AuLock_IR);
22608 +               dput(parent);
22609 +               parent = NULL;
22610 +       }
22611 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
22612 +               goto out; /* success */
22613 +
22614 +       if (!d_unhashed(dentry)) {
22615 +               struct au_cp_generic cpg = {
22616 +                       .dentry = dentry,
22617 +                       .bdst   = a->btgt,
22618 +                       .bsrc   = btop,
22619 +                       .len    = sz,
22620 +                       .pin    = &a->pin,
22621 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
22622 +               };
22623 +               err = au_sio_cpup_simple(&cpg);
22624 +               if (!err)
22625 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22626 +       } else if (!hi_wh)
22627 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22628 +       else
22629 +               a->h_path.dentry = hi_wh; /* do not dget here */
22630 +
22631 +out_unlock:
22632 +       a->h_inode = d_inode(a->h_path.dentry);
22633 +       if (!err)
22634 +               goto out; /* success */
22635 +       au_unpin(&a->pin);
22636 +out_parent:
22637 +       if (parent) {
22638 +               di_write_unlock(parent);
22639 +               dput(parent);
22640 +       }
22641 +out:
22642 +       if (!err)
22643 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22644 +       return err;
22645 +}
22646 +
22647 +static int aufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
22648 +                       struct iattr *ia)
22649 +{
22650 +       int err;
22651 +       struct inode *inode, *delegated;
22652 +       struct super_block *sb;
22653 +       struct file *file;
22654 +       struct au_icpup_args *a;
22655 +       struct mnt_idmap *h_idmap;
22656 +
22657 +       inode = d_inode(dentry);
22658 +       IMustLock(inode);
22659 +
22660 +       err = setattr_prepare(idmap, dentry, ia);
22661 +       if (unlikely(err))
22662 +               goto out;
22663 +
22664 +       err = -ENOMEM;
22665 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22666 +       if (unlikely(!a))
22667 +               goto out;
22668 +
22669 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22670 +               ia->ia_valid &= ~ATTR_MODE;
22671 +
22672 +       file = NULL;
22673 +       sb = dentry->d_sb;
22674 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22675 +       if (unlikely(err))
22676 +               goto out_kfree;
22677 +
22678 +       if (ia->ia_valid & ATTR_FILE) {
22679 +               /* currently ftruncate(2) only */
22680 +               AuDebugOn(!d_is_reg(dentry));
22681 +               file = ia->ia_file;
22682 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22683 +                                           /*fi_lsc*/0);
22684 +               if (unlikely(err))
22685 +                       goto out_si;
22686 +               ia->ia_file = au_hf_top(file);
22687 +               a->udba = AuOpt_UDBA_NONE;
22688 +       } else {
22689 +               /* fchmod() doesn't pass ia_file */
22690 +               a->udba = au_opt_udba(sb);
22691 +               di_write_lock_child(dentry);
22692 +               /* no d_unlinked(), to set UDBA_NONE for root */
22693 +               if (d_unhashed(dentry))
22694 +                       a->udba = AuOpt_UDBA_NONE;
22695 +               if (a->udba != AuOpt_UDBA_NONE) {
22696 +                       AuDebugOn(IS_ROOT(dentry));
22697 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
22698 +                       if (unlikely(err))
22699 +                               goto out_dentry;
22700 +               }
22701 +       }
22702 +
22703 +       err = au_pin_and_icpup(dentry, ia, a);
22704 +       if (unlikely(err < 0))
22705 +               goto out_dentry;
22706 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
22707 +               ia->ia_file = NULL;
22708 +               ia->ia_valid &= ~ATTR_FILE;
22709 +       }
22710 +
22711 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22712 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22713 +           == (ATTR_MODE | ATTR_CTIME)) {
22714 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
22715 +               if (unlikely(err))
22716 +                       goto out_unlock;
22717 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22718 +                  && (ia->ia_valid & ATTR_CTIME)) {
22719 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
22720 +               if (unlikely(err))
22721 +                       goto out_unlock;
22722 +       }
22723 +
22724 +       if (ia->ia_valid & ATTR_SIZE) {
22725 +               struct file *f;
22726 +
22727 +               if (ia->ia_size < i_size_read(inode))
22728 +                       /* unmap only */
22729 +                       truncate_setsize(inode, ia->ia_size);
22730 +
22731 +               f = NULL;
22732 +               if (ia->ia_valid & ATTR_FILE)
22733 +                       f = ia->ia_file;
22734 +               inode_unlock(a->h_inode);
22735 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
22736 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22737 +       } else {
22738 +               delegated = NULL;
22739 +               while (1) {
22740 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
22741 +                       if (delegated) {
22742 +                               err = break_deleg_wait(&delegated);
22743 +                               if (!err)
22744 +                                       continue;
22745 +                       }
22746 +                       break;
22747 +               }
22748 +       }
22749 +       /*
22750 +        * regardless aufs 'acl' option setting.
22751 +        * why don't all acl-aware fs call this func from their ->setattr()?
22752 +        */
22753 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
22754 +               h_idmap = mnt_idmap(a->h_path.mnt);
22755 +               err = vfsub_acl_chmod(h_idmap, a->h_path.dentry, ia->ia_mode);
22756 +       }
22757 +       if (!err)
22758 +               au_cpup_attr_changeable(inode);
22759 +
22760 +out_unlock:
22761 +       inode_unlock(a->h_inode);
22762 +       au_unpin(&a->pin);
22763 +       if (unlikely(err))
22764 +               au_update_dbtop(dentry);
22765 +out_dentry:
22766 +       di_write_unlock(dentry);
22767 +       if (file) {
22768 +               fi_write_unlock(file);
22769 +               ia->ia_file = file;
22770 +               ia->ia_valid |= ATTR_FILE;
22771 +       }
22772 +out_si:
22773 +       si_read_unlock(sb);
22774 +out_kfree:
22775 +       au_kfree_rcu(a);
22776 +out:
22777 +       AuTraceErr(err);
22778 +       return err;
22779 +}
22780 +
22781 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22782 +static int au_h_path_to_set_attr(struct dentry *dentry,
22783 +                                struct au_icpup_args *a, struct path *h_path)
22784 +{
22785 +       int err;
22786 +       struct super_block *sb;
22787 +
22788 +       sb = dentry->d_sb;
22789 +       a->udba = au_opt_udba(sb);
22790 +       /* no d_unlinked(), to set UDBA_NONE for root */
22791 +       if (d_unhashed(dentry))
22792 +               a->udba = AuOpt_UDBA_NONE;
22793 +       if (a->udba != AuOpt_UDBA_NONE) {
22794 +               AuDebugOn(IS_ROOT(dentry));
22795 +               err = au_reval_for_attr(dentry, au_sigen(sb));
22796 +               if (unlikely(err))
22797 +                       goto out;
22798 +       }
22799 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22800 +       if (unlikely(err < 0))
22801 +               goto out;
22802 +
22803 +       h_path->dentry = a->h_path.dentry;
22804 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
22805 +
22806 +out:
22807 +       return err;
22808 +}
22809 +
22810 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22811 +                 struct au_sxattr *arg)
22812 +{
22813 +       int err;
22814 +       struct path h_path;
22815 +       struct super_block *sb;
22816 +       struct au_icpup_args *a;
22817 +       struct inode *h_inode;
22818 +       struct mnt_idmap *h_idmap;
22819 +
22820 +       IMustLock(inode);
22821 +
22822 +       err = -ENOMEM;
22823 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22824 +       if (unlikely(!a))
22825 +               goto out;
22826 +
22827 +       sb = dentry->d_sb;
22828 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22829 +       if (unlikely(err))
22830 +               goto out_kfree;
22831 +
22832 +       h_path.dentry = NULL;   /* silence gcc */
22833 +       di_write_lock_child(dentry);
22834 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
22835 +       if (unlikely(err))
22836 +               goto out_di;
22837 +       h_idmap = mnt_idmap(h_path.mnt);
22838 +
22839 +       inode_unlock(a->h_inode);
22840 +       switch (arg->type) {
22841 +       case AU_XATTR_SET:
22842 +               AuDebugOn(d_is_negative(h_path.dentry));
22843 +               err = vfsub_setxattr(h_idmap, h_path.dentry,
22844 +                                    arg->u.set.name, arg->u.set.value,
22845 +                                    arg->u.set.size, arg->u.set.flags);
22846 +               break;
22847 +       case AU_ACL_SET:
22848 +               err = -EOPNOTSUPP;
22849 +               h_inode = d_inode(h_path.dentry);
22850 +               if (h_inode->i_op->set_acl) {
22851 +                       /* this will call posix_acl_update_mode */
22852 +                       err = h_inode->i_op->set_acl(h_idmap, h_path.dentry,
22853 +                                                    arg->u.acl_set.acl,
22854 +                                                    arg->u.acl_set.type);
22855 +               }
22856 +               break;
22857 +       }
22858 +       if (!err)
22859 +               au_cpup_attr_timesizes(inode);
22860 +
22861 +       au_unpin(&a->pin);
22862 +       if (unlikely(err))
22863 +               au_update_dbtop(dentry);
22864 +
22865 +out_di:
22866 +       di_write_unlock(dentry);
22867 +       si_read_unlock(sb);
22868 +out_kfree:
22869 +       au_kfree_rcu(a);
22870 +out:
22871 +       AuTraceErr(err);
22872 +       return err;
22873 +}
22874 +#endif
22875 +
22876 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22877 +                            unsigned int nlink)
22878 +{
22879 +       unsigned int n;
22880 +
22881 +       inode->i_mode = st->mode;
22882 +       /* don't i_[ug]id_write() here */
22883 +       inode->i_uid = st->uid;
22884 +       inode->i_gid = st->gid;
22885 +       inode->i_atime = st->atime;
22886 +       inode->i_mtime = st->mtime;
22887 +       inode->i_ctime = st->ctime;
22888 +
22889 +       au_cpup_attr_nlink(inode, /*force*/0);
22890 +       if (S_ISDIR(inode->i_mode)) {
22891 +               n = inode->i_nlink;
22892 +               n -= nlink;
22893 +               n += st->nlink;
22894 +               smp_mb(); /* for i_nlink */
22895 +               /* 0 can happen */
22896 +               set_nlink(inode, n);
22897 +       }
22898 +
22899 +       spin_lock(&inode->i_lock);
22900 +       inode->i_blocks = st->blocks;
22901 +       i_size_write(inode, st->size);
22902 +       spin_unlock(&inode->i_lock);
22903 +}
22904 +
22905 +/*
22906 + * common routine for aufs_getattr() and au_getxattr().
22907 + * returns zero or negative (an error).
22908 + * @dentry will be read-locked in success.
22909 + */
22910 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22911 +                     struct path *h_path, int locked)
22912 +{
22913 +       int err;
22914 +       unsigned int mnt_flags, sigen;
22915 +       unsigned char udba_none;
22916 +       aufs_bindex_t bindex;
22917 +       struct super_block *sb, *h_sb;
22918 +
22919 +       h_path->mnt = NULL;
22920 +       h_path->dentry = NULL;
22921 +
22922 +       err = 0;
22923 +       sb = dentry->d_sb;
22924 +       mnt_flags = au_mntflags(sb);
22925 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
22926 +
22927 +       if (unlikely(locked))
22928 +               goto body; /* skip locking dinfo */
22929 +
22930 +       /* support fstat(2) */
22931 +       if (!d_unlinked(dentry) && !udba_none) {
22932 +               sigen = au_sigen(sb);
22933 +               err = au_digen_test(dentry, sigen);
22934 +               if (!err) {
22935 +                       di_read_lock_child(dentry, AuLock_IR);
22936 +                       err = au_dbrange_test(dentry);
22937 +                       if (unlikely(err)) {
22938 +                               di_read_unlock(dentry, AuLock_IR);
22939 +                               goto out;
22940 +                       }
22941 +               } else {
22942 +                       AuDebugOn(IS_ROOT(dentry));
22943 +                       di_write_lock_child(dentry);
22944 +                       err = au_dbrange_test(dentry);
22945 +                       if (!err)
22946 +                               err = au_reval_for_attr(dentry, sigen);
22947 +                       if (!err)
22948 +                               di_downgrade_lock(dentry, AuLock_IR);
22949 +                       else {
22950 +                               di_write_unlock(dentry);
22951 +                               goto out;
22952 +                       }
22953 +               }
22954 +       } else
22955 +               di_read_lock_child(dentry, AuLock_IR);
22956 +
22957 +body:
22958 +       if (!inode) {
22959 +               inode = d_inode(dentry);
22960 +               if (unlikely(!inode))
22961 +                       goto out;
22962 +       }
22963 +       bindex = au_ibtop(inode);
22964 +       h_path->mnt = au_sbr_mnt(sb, bindex);
22965 +       h_sb = h_path->mnt->mnt_sb;
22966 +       if (!force
22967 +           && !au_test_fs_bad_iattr(h_sb)
22968 +           && udba_none)
22969 +               goto out; /* success */
22970 +
22971 +       if (au_dbtop(dentry) == bindex)
22972 +               h_path->dentry = au_h_dptr(dentry, bindex);
22973 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
22974 +               h_path->dentry = au_plink_lkup(inode, bindex);
22975 +               if (IS_ERR(h_path->dentry))
22976 +                       /* pretending success */
22977 +                       h_path->dentry = NULL;
22978 +               else
22979 +                       dput(h_path->dentry);
22980 +       }
22981 +
22982 +out:
22983 +       return err;
22984 +}
22985 +
22986 +static int aufs_getattr(struct mnt_idmap *idmap, const struct path *path,
22987 +                       struct kstat *st, u32 request, unsigned int query)
22988 +{
22989 +       int err;
22990 +       unsigned char positive;
22991 +       struct path h_path;
22992 +       struct dentry *dentry;
22993 +       struct inode *inode;
22994 +       struct super_block *sb;
22995 +
22996 +       dentry = path->dentry;
22997 +       inode = d_inode(dentry);
22998 +       sb = dentry->d_sb;
22999 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23000 +       if (unlikely(err))
23001 +               goto out;
23002 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
23003 +                               /*locked*/0);
23004 +       if (unlikely(err))
23005 +               goto out_si;
23006 +       if (unlikely(!h_path.dentry))
23007 +               /* illegally overlapped or something */
23008 +               goto out_fill; /* pretending success */
23009 +
23010 +       positive = d_is_positive(h_path.dentry);
23011 +       if (positive)
23012 +               /* no vfsub version */
23013 +               err = vfs_getattr(&h_path, st, request, query);
23014 +       if (!err) {
23015 +               if (positive)
23016 +                       au_refresh_iattr(inode, st,
23017 +                                        d_inode(h_path.dentry)->i_nlink);
23018 +               goto out_fill; /* success */
23019 +       }
23020 +       AuTraceErr(err);
23021 +       goto out_di;
23022 +
23023 +out_fill:
23024 +       generic_fillattr(idmap, inode, st);
23025 +out_di:
23026 +       di_read_unlock(dentry, AuLock_IR);
23027 +out_si:
23028 +       si_read_unlock(sb);
23029 +out:
23030 +       AuTraceErr(err);
23031 +       return err;
23032 +}
23033 +
23034 +/* ---------------------------------------------------------------------- */
23035 +
23036 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
23037 +                                struct delayed_call *done)
23038 +{
23039 +       const char *ret;
23040 +       struct dentry *h_dentry;
23041 +       struct inode *h_inode;
23042 +       int err;
23043 +       aufs_bindex_t bindex;
23044 +
23045 +       ret = NULL; /* suppress a warning */
23046 +       err = -ECHILD;
23047 +       if (!dentry)
23048 +               goto out;
23049 +
23050 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23051 +       if (unlikely(err))
23052 +               goto out;
23053 +
23054 +       err = au_d_hashed_positive(dentry);
23055 +       if (unlikely(err))
23056 +               goto out_unlock;
23057 +
23058 +       err = -EINVAL;
23059 +       inode = d_inode(dentry);
23060 +       bindex = au_ibtop(inode);
23061 +       h_inode = au_h_iptr(inode, bindex);
23062 +       if (unlikely(!h_inode->i_op->get_link))
23063 +               goto out_unlock;
23064 +
23065 +       err = -EBUSY;
23066 +       h_dentry = NULL;
23067 +       if (au_dbtop(dentry) <= bindex) {
23068 +               h_dentry = au_h_dptr(dentry, bindex);
23069 +               if (h_dentry)
23070 +                       dget(h_dentry);
23071 +       }
23072 +       if (!h_dentry) {
23073 +               h_dentry = d_find_any_alias(h_inode);
23074 +               if (IS_ERR(h_dentry)) {
23075 +                       err = PTR_ERR(h_dentry);
23076 +                       goto out_unlock;
23077 +               }
23078 +       }
23079 +       if (unlikely(!h_dentry))
23080 +               goto out_unlock;
23081 +
23082 +       err = 0;
23083 +       AuDbg("%ps\n", h_inode->i_op->get_link);
23084 +       AuDbgDentry(h_dentry);
23085 +       ret = vfs_get_link(h_dentry, done);
23086 +       dput(h_dentry);
23087 +       if (IS_ERR(ret))
23088 +               err = PTR_ERR(ret);
23089 +
23090 +out_unlock:
23091 +       aufs_read_unlock(dentry, AuLock_IR);
23092 +out:
23093 +       if (unlikely(err))
23094 +               ret = ERR_PTR(err);
23095 +       AuTraceErrPtr(ret);
23096 +       return ret;
23097 +}
23098 +
23099 +/* ---------------------------------------------------------------------- */
23100 +
23101 +static int au_is_special(struct inode *inode)
23102 +{
23103 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23104 +}
23105 +
23106 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
23107 +                           int flags)
23108 +{
23109 +       int err;
23110 +       aufs_bindex_t bindex;
23111 +       struct super_block *sb;
23112 +       struct inode *h_inode;
23113 +       struct vfsmount *h_mnt;
23114 +
23115 +       sb = inode->i_sb;
23116 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23117 +                 "unexpected s_flags 0x%lx", sb->s_flags);
23118 +
23119 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
23120 +       lockdep_off();
23121 +       si_read_lock(sb, AuLock_FLUSH);
23122 +       ii_write_lock_child(inode);
23123 +
23124 +       err = 0;
23125 +       bindex = au_ibtop(inode);
23126 +       h_inode = au_h_iptr(inode, bindex);
23127 +       if (!au_test_ro(sb, bindex, inode)) {
23128 +               h_mnt = au_sbr_mnt(sb, bindex);
23129 +               err = vfsub_mnt_want_write(h_mnt);
23130 +               if (!err) {
23131 +                       err = vfsub_update_time(h_inode, ts, flags);
23132 +                       vfsub_mnt_drop_write(h_mnt);
23133 +               }
23134 +       } else if (au_is_special(h_inode)) {
23135 +               /*
23136 +                * Never copy-up here.
23137 +                * These special files may already be opened and used for
23138 +                * communicating. If we copied it up, then the communication
23139 +                * would be corrupted.
23140 +                */
23141 +               AuWarn1("timestamps for i%lu are ignored "
23142 +                       "since it is on readonly branch (hi%lu).\n",
23143 +                       inode->i_ino, h_inode->i_ino);
23144 +       } else if (flags & ~S_ATIME) {
23145 +               err = -EIO;
23146 +               AuIOErr1("unexpected flags 0x%x\n", flags);
23147 +               AuDebugOn(1);
23148 +       }
23149 +
23150 +       if (!err)
23151 +               au_cpup_attr_timesizes(inode);
23152 +       ii_write_unlock(inode);
23153 +       si_read_unlock(sb);
23154 +       lockdep_on();
23155 +
23156 +       if (!err && (flags & S_VERSION))
23157 +               inode_inc_iversion(inode);
23158 +
23159 +       return err;
23160 +}
23161 +
23162 +/* ---------------------------------------------------------------------- */
23163 +
23164 +/* no getattr version will be set by module.c:aufs_init() */
23165 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23166 +       aufs_iop[] = {
23167 +       [AuIop_SYMLINK] = {
23168 +               .permission     = aufs_permission,
23169 +#ifdef CONFIG_FS_POSIX_ACL
23170 +               .get_inode_acl  = aufs_get_inode_acl,
23171 +               .get_acl        = aufs_get_acl,
23172 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
23173 +#endif
23174 +
23175 +               .setattr        = aufs_setattr,
23176 +               .getattr        = aufs_getattr,
23177 +
23178 +#ifdef CONFIG_AUFS_XATTR
23179 +               .listxattr      = aufs_listxattr,
23180 +#endif
23181 +
23182 +               .get_link       = aufs_get_link
23183 +
23184 +               /* .update_time = aufs_update_time */
23185 +       },
23186 +       [AuIop_DIR] = {
23187 +               .create         = aufs_create,
23188 +               .lookup         = aufs_lookup,
23189 +               .link           = aufs_link,
23190 +               .unlink         = aufs_unlink,
23191 +               .symlink        = aufs_symlink,
23192 +               .mkdir          = aufs_mkdir,
23193 +               .rmdir          = aufs_rmdir,
23194 +               .mknod          = aufs_mknod,
23195 +               .rename         = aufs_rename,
23196 +
23197 +               .permission     = aufs_permission,
23198 +#ifdef CONFIG_FS_POSIX_ACL
23199 +               .get_inode_acl  = aufs_get_inode_acl,
23200 +               .get_acl        = aufs_get_acl,
23201 +               .set_acl        = aufs_set_acl,
23202 +#endif
23203 +
23204 +               .setattr        = aufs_setattr,
23205 +               .getattr        = aufs_getattr,
23206 +
23207 +#ifdef CONFIG_AUFS_XATTR
23208 +               .listxattr      = aufs_listxattr,
23209 +#endif
23210 +
23211 +               .update_time    = aufs_update_time,
23212 +               .atomic_open    = aufs_atomic_open,
23213 +               .tmpfile        = aufs_tmpfile
23214 +       },
23215 +       [AuIop_OTHER] = {
23216 +               .permission     = aufs_permission,
23217 +#ifdef CONFIG_FS_POSIX_ACL
23218 +               .get_inode_acl  = aufs_get_inode_acl,
23219 +               .get_acl        = aufs_get_acl,
23220 +               .set_acl        = aufs_set_acl,
23221 +#endif
23222 +
23223 +               .setattr        = aufs_setattr,
23224 +               .getattr        = aufs_getattr,
23225 +
23226 +#ifdef CONFIG_AUFS_XATTR
23227 +               .listxattr      = aufs_listxattr,
23228 +#endif
23229 +
23230 +               .update_time    = aufs_update_time
23231 +       }
23232 +};
23233 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
23234 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
23235 +++ linux/fs/aufs/i_op_del.c    2023-09-03 02:21:58.859971007 +0200
23236 @@ -0,0 +1,522 @@
23237 +// SPDX-License-Identifier: GPL-2.0
23238 +/*
23239 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23240 + *
23241 + * This program is free software; you can redistribute it and/or modify
23242 + * it under the terms of the GNU General Public License as published by
23243 + * the Free Software Foundation; either version 2 of the License, or
23244 + * (at your option) any later version.
23245 + *
23246 + * This program is distributed in the hope that it will be useful,
23247 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23248 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23249 + * GNU General Public License for more details.
23250 + *
23251 + * You should have received a copy of the GNU General Public License
23252 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23253 + */
23254 +
23255 +/*
23256 + * inode operations (del entry)
23257 + */
23258 +
23259 +#include <linux/iversion.h>
23260 +#include "aufs.h"
23261 +
23262 +/*
23263 + * decide if a new whiteout for @dentry is necessary or not.
23264 + * when it is necessary, prepare the parent dir for the upper branch whose
23265 + * branch index is @bcpup for creation. the actual creation of the whiteout will
23266 + * be done by caller.
23267 + * return value:
23268 + * 0: wh is unnecessary
23269 + * plus: wh is necessary
23270 + * minus: error
23271 + */
23272 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
23273 +{
23274 +       int need_wh, err;
23275 +       aufs_bindex_t btop;
23276 +       struct super_block *sb;
23277 +
23278 +       sb = dentry->d_sb;
23279 +       btop = au_dbtop(dentry);
23280 +       if (*bcpup < 0) {
23281 +               *bcpup = btop;
23282 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
23283 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
23284 +                       *bcpup = err;
23285 +                       if (unlikely(err < 0))
23286 +                               goto out;
23287 +               }
23288 +       } else
23289 +               AuDebugOn(btop < *bcpup
23290 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
23291 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
23292 +
23293 +       if (*bcpup != btop) {
23294 +               err = au_cpup_dirs(dentry, *bcpup);
23295 +               if (unlikely(err))
23296 +                       goto out;
23297 +               need_wh = 1;
23298 +       } else {
23299 +               struct au_dinfo *dinfo, *tmp;
23300 +
23301 +               need_wh = -ENOMEM;
23302 +               dinfo = au_di(dentry);
23303 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23304 +               if (tmp) {
23305 +                       au_di_cp(tmp, dinfo);
23306 +                       au_di_swap(tmp, dinfo);
23307 +                       /* returns the number of positive dentries */
23308 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
23309 +                                                /* AuLkup_IGNORE_PERM */ 0);
23310 +                       au_di_swap(tmp, dinfo);
23311 +                       au_rw_write_unlock(&tmp->di_rwsem);
23312 +                       au_di_free(tmp);
23313 +               }
23314 +       }
23315 +       AuDbg("need_wh %d\n", need_wh);
23316 +       err = need_wh;
23317 +
23318 +out:
23319 +       return err;
23320 +}
23321 +
23322 +/*
23323 + * simple tests for the del-entry operations.
23324 + * following the checks in vfs, plus the parent-child relationship.
23325 + */
23326 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23327 +              struct dentry *h_parent, int isdir)
23328 +{
23329 +       int err;
23330 +       umode_t h_mode;
23331 +       struct dentry *h_dentry, *h_latest;
23332 +       struct inode *h_inode;
23333 +       struct path h_ppath;
23334 +       struct super_block *sb;
23335 +       struct au_branch *br;
23336 +       struct mnt_idmap *h_idmap;
23337 +
23338 +       h_dentry = au_h_dptr(dentry, bindex);
23339 +       if (d_really_is_positive(dentry)) {
23340 +               err = -ENOENT;
23341 +               if (unlikely(d_is_negative(h_dentry)))
23342 +                       goto out;
23343 +               h_inode = d_inode(h_dentry);
23344 +               if (unlikely(!h_inode->i_nlink))
23345 +                       goto out;
23346 +
23347 +               h_mode = h_inode->i_mode;
23348 +               if (!isdir) {
23349 +                       err = -EISDIR;
23350 +                       if (unlikely(S_ISDIR(h_mode)))
23351 +                               goto out;
23352 +               } else if (unlikely(!S_ISDIR(h_mode))) {
23353 +                       err = -ENOTDIR;
23354 +                       goto out;
23355 +               }
23356 +       } else {
23357 +               /* rename(2) case */
23358 +               err = -EIO;
23359 +               if (unlikely(d_is_positive(h_dentry)))
23360 +                       goto out;
23361 +       }
23362 +
23363 +       err = -ENOENT;
23364 +       /* expected parent dir is locked */
23365 +       if (unlikely(h_parent != h_dentry->d_parent))
23366 +               goto out;
23367 +       err = 0;
23368 +
23369 +       /*
23370 +        * rmdir a dir may break the consistency on some filesystem.
23371 +        * let's try heavy test.
23372 +        */
23373 +       err = -EACCES;
23374 +       sb = dentry->d_sb;
23375 +       br = au_sbr(sb, bindex);
23376 +       h_idmap = au_br_idmap(br);
23377 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
23378 +                    && au_test_h_perm(h_idmap, d_inode(h_parent),
23379 +                                      MAY_EXEC | MAY_WRITE)))
23380 +               goto out;
23381 +
23382 +       h_ppath.dentry = h_parent;
23383 +       h_ppath.mnt = au_br_mnt(br);
23384 +       h_latest = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
23385 +       err = -EIO;
23386 +       if (IS_ERR(h_latest))
23387 +               goto out;
23388 +       if (h_latest == h_dentry)
23389 +               err = 0;
23390 +       dput(h_latest);
23391 +
23392 +out:
23393 +       return err;
23394 +}
23395 +
23396 +/*
23397 + * decide the branch where we operate for @dentry. the branch index will be set
23398 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
23399 + * dir for reverting.
23400 + * when a new whiteout is necessary, create it.
23401 + */
23402 +static struct dentry*
23403 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23404 +                   struct au_dtime *dt, struct au_pin *pin)
23405 +{
23406 +       struct dentry *wh_dentry;
23407 +       struct super_block *sb;
23408 +       struct path h_path;
23409 +       int err, need_wh;
23410 +       unsigned int udba;
23411 +       aufs_bindex_t bcpup;
23412 +
23413 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23414 +       wh_dentry = ERR_PTR(need_wh);
23415 +       if (unlikely(need_wh < 0))
23416 +               goto out;
23417 +
23418 +       sb = dentry->d_sb;
23419 +       udba = au_opt_udba(sb);
23420 +       bcpup = *rbcpup;
23421 +       err = au_pin(pin, dentry, bcpup, udba,
23422 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23423 +       wh_dentry = ERR_PTR(err);
23424 +       if (unlikely(err))
23425 +               goto out;
23426 +
23427 +       h_path.dentry = au_pinned_h_parent(pin);
23428 +       if (udba != AuOpt_UDBA_NONE
23429 +           && au_dbtop(dentry) == bcpup) {
23430 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23431 +               wh_dentry = ERR_PTR(err);
23432 +               if (unlikely(err))
23433 +                       goto out_unpin;
23434 +       }
23435 +
23436 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
23437 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23438 +       wh_dentry = NULL;
23439 +       if (!need_wh)
23440 +               goto out; /* success, no need to create whiteout */
23441 +
23442 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23443 +       if (IS_ERR(wh_dentry))
23444 +               goto out_unpin;
23445 +
23446 +       /* returns with the parent is locked and wh_dentry is dget-ed */
23447 +       goto out; /* success */
23448 +
23449 +out_unpin:
23450 +       au_unpin(pin);
23451 +out:
23452 +       return wh_dentry;
23453 +}
23454 +
23455 +/*
23456 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
23457 + * in order to be revertible and save time for removing many child whiteouts
23458 + * under the dir.
23459 + * returns 1 when there are too many child whiteout and caller should remove
23460 + * them asynchronously. returns 0 when the number of children is enough small to
23461 + * remove now or the branch fs is a remote fs.
23462 + * otherwise return an error.
23463 + */
23464 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23465 +                          struct au_nhash *whlist, struct inode *dir)
23466 +{
23467 +       int rmdir_later, err, dirwh;
23468 +       struct dentry *h_dentry;
23469 +       struct super_block *sb;
23470 +       struct inode *inode;
23471 +
23472 +       sb = dentry->d_sb;
23473 +       SiMustAnyLock(sb);
23474 +       h_dentry = au_h_dptr(dentry, bindex);
23475 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23476 +       if (unlikely(err))
23477 +               goto out;
23478 +
23479 +       /* stop monitoring */
23480 +       inode = d_inode(dentry);
23481 +       au_hn_free(au_hi(inode, bindex));
23482 +
23483 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
23484 +               dirwh = au_sbi(sb)->si_dirwh;
23485 +               rmdir_later = (dirwh <= 1);
23486 +               if (!rmdir_later)
23487 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23488 +                                                             dirwh);
23489 +               if (rmdir_later)
23490 +                       return rmdir_later;
23491 +       }
23492 +
23493 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23494 +       if (unlikely(err)) {
23495 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23496 +                       h_dentry, bindex, err);
23497 +               err = 0;
23498 +       }
23499 +
23500 +out:
23501 +       AuTraceErr(err);
23502 +       return err;
23503 +}
23504 +
23505 +/*
23506 + * final procedure for deleting a entry.
23507 + * maintain dentry and iattr.
23508 + */
23509 +static void epilog(struct inode *dir, struct dentry *dentry,
23510 +                  aufs_bindex_t bindex)
23511 +{
23512 +       struct inode *inode;
23513 +
23514 +       inode = d_inode(dentry);
23515 +       d_drop(dentry);
23516 +       inode->i_ctime = dir->i_ctime;
23517 +
23518 +       au_dir_ts(dir, bindex);
23519 +       inode_inc_iversion(dir);
23520 +}
23521 +
23522 +/*
23523 + * when an error happened, remove the created whiteout and revert everything.
23524 + */
23525 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23526 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
23527 +                    struct dentry *dentry, struct au_dtime *dt)
23528 +{
23529 +       int rerr;
23530 +       struct path h_path = {
23531 +               .dentry = wh_dentry,
23532 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
23533 +       };
23534 +
23535 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
23536 +       if (!rerr) {
23537 +               au_set_dbwh(dentry, bwh);
23538 +               au_dtime_revert(dt);
23539 +               return 0;
23540 +       }
23541 +
23542 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
23543 +       return -EIO;
23544 +}
23545 +
23546 +/* ---------------------------------------------------------------------- */
23547 +
23548 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
23549 +{
23550 +       int err;
23551 +       aufs_bindex_t bwh, bindex, btop;
23552 +       struct inode *inode, *h_dir, *delegated;
23553 +       struct dentry *parent, *wh_dentry;
23554 +       /* to reduce stack size */
23555 +       struct {
23556 +               struct au_dtime dt;
23557 +               struct au_pin pin;
23558 +               struct path h_path;
23559 +       } *a;
23560 +
23561 +       IMustLock(dir);
23562 +
23563 +       err = -ENOMEM;
23564 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23565 +       if (unlikely(!a))
23566 +               goto out;
23567 +
23568 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23569 +       if (unlikely(err))
23570 +               goto out_free;
23571 +       err = au_d_hashed_positive(dentry);
23572 +       if (unlikely(err))
23573 +               goto out_unlock;
23574 +       inode = d_inode(dentry);
23575 +       IMustLock(inode);
23576 +       err = -EISDIR;
23577 +       if (unlikely(d_is_dir(dentry)))
23578 +               goto out_unlock; /* possible? */
23579 +
23580 +       btop = au_dbtop(dentry);
23581 +       bwh = au_dbwh(dentry);
23582 +       bindex = -1;
23583 +       parent = dentry->d_parent; /* dir inode is locked */
23584 +       di_write_lock_parent(parent);
23585 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23586 +                                       &a->pin);
23587 +       err = PTR_ERR(wh_dentry);
23588 +       if (IS_ERR(wh_dentry))
23589 +               goto out_parent;
23590 +
23591 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23592 +       a->h_path.dentry = au_h_dptr(dentry, btop);
23593 +       dget(a->h_path.dentry);
23594 +       if (bindex == btop) {
23595 +               h_dir = au_pinned_h_dir(&a->pin);
23596 +               delegated = NULL;
23597 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23598 +               if (unlikely(err == -EWOULDBLOCK)) {
23599 +                       pr_warn("cannot retry for NFSv4 delegation"
23600 +                               " for an internal unlink\n");
23601 +                       iput(delegated);
23602 +               }
23603 +       } else {
23604 +               /* dir inode is locked */
23605 +               h_dir = d_inode(wh_dentry->d_parent);
23606 +               IMustLock(h_dir);
23607 +               err = 0;
23608 +       }
23609 +
23610 +       if (!err) {
23611 +               vfsub_drop_nlink(inode);
23612 +               epilog(dir, dentry, bindex);
23613 +
23614 +               /* update target timestamps */
23615 +               if (bindex == btop) {
23616 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23617 +                       /*ignore*/
23618 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23619 +               } else
23620 +                       /* todo: this timestamp may be reverted later */
23621 +                       inode->i_ctime = h_dir->i_ctime;
23622 +               goto out_unpin; /* success */
23623 +       }
23624 +
23625 +       /* revert */
23626 +       if (wh_dentry) {
23627 +               int rerr;
23628 +
23629 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23630 +                                &a->dt);
23631 +               if (rerr)
23632 +                       err = rerr;
23633 +       }
23634 +
23635 +out_unpin:
23636 +       au_unpin(&a->pin);
23637 +       dput(wh_dentry);
23638 +       dput(a->h_path.dentry);
23639 +out_parent:
23640 +       di_write_unlock(parent);
23641 +out_unlock:
23642 +       aufs_read_unlock(dentry, AuLock_DW);
23643 +out_free:
23644 +       au_kfree_rcu(a);
23645 +out:
23646 +       return err;
23647 +}
23648 +
23649 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
23650 +{
23651 +       int err, rmdir_later;
23652 +       aufs_bindex_t bwh, bindex, btop;
23653 +       struct inode *inode;
23654 +       struct dentry *parent, *wh_dentry, *h_dentry;
23655 +       struct au_whtmp_rmdir *args;
23656 +       /* to reduce stack size */
23657 +       struct {
23658 +               struct au_dtime dt;
23659 +               struct au_pin pin;
23660 +       } *a;
23661 +
23662 +       IMustLock(dir);
23663 +
23664 +       err = -ENOMEM;
23665 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23666 +       if (unlikely(!a))
23667 +               goto out;
23668 +
23669 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23670 +       if (unlikely(err))
23671 +               goto out_free;
23672 +       err = au_alive_dir(dentry);
23673 +       if (unlikely(err))
23674 +               goto out_unlock;
23675 +       inode = d_inode(dentry);
23676 +       IMustLock(inode);
23677 +       err = -ENOTDIR;
23678 +       if (unlikely(!d_is_dir(dentry)))
23679 +               goto out_unlock; /* possible? */
23680 +
23681 +       err = -ENOMEM;
23682 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23683 +       if (unlikely(!args))
23684 +               goto out_unlock;
23685 +
23686 +       parent = dentry->d_parent; /* dir inode is locked */
23687 +       di_write_lock_parent(parent);
23688 +       err = au_test_empty(dentry, &args->whlist);
23689 +       if (unlikely(err))
23690 +               goto out_parent;
23691 +
23692 +       btop = au_dbtop(dentry);
23693 +       bwh = au_dbwh(dentry);
23694 +       bindex = -1;
23695 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23696 +                                       &a->pin);
23697 +       err = PTR_ERR(wh_dentry);
23698 +       if (IS_ERR(wh_dentry))
23699 +               goto out_parent;
23700 +
23701 +       h_dentry = au_h_dptr(dentry, btop);
23702 +       dget(h_dentry);
23703 +       rmdir_later = 0;
23704 +       if (bindex == btop) {
23705 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
23706 +               if (err > 0) {
23707 +                       rmdir_later = err;
23708 +                       err = 0;
23709 +               }
23710 +       } else {
23711 +               /* stop monitoring */
23712 +               au_hn_free(au_hi(inode, btop));
23713 +
23714 +               /* dir inode is locked */
23715 +               IMustLock(d_inode(wh_dentry->d_parent));
23716 +               err = 0;
23717 +       }
23718 +
23719 +       if (!err) {
23720 +               vfsub_dead_dir(inode);
23721 +               au_set_dbdiropq(dentry, -1);
23722 +               epilog(dir, dentry, bindex);
23723 +
23724 +               if (rmdir_later) {
23725 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
23726 +                       args = NULL;
23727 +               }
23728 +
23729 +               goto out_unpin; /* success */
23730 +       }
23731 +
23732 +       /* revert */
23733 +       AuLabel(revert);
23734 +       if (wh_dentry) {
23735 +               int rerr;
23736 +
23737 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23738 +                                &a->dt);
23739 +               if (rerr)
23740 +                       err = rerr;
23741 +       }
23742 +
23743 +out_unpin:
23744 +       au_unpin(&a->pin);
23745 +       dput(wh_dentry);
23746 +       dput(h_dentry);
23747 +out_parent:
23748 +       di_write_unlock(parent);
23749 +       if (args)
23750 +               au_whtmp_rmdir_free(args);
23751 +out_unlock:
23752 +       aufs_read_unlock(dentry, AuLock_DW);
23753 +out_free:
23754 +       au_kfree_rcu(a);
23755 +out:
23756 +       AuTraceErr(err);
23757 +       return err;
23758 +}
23759 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
23760 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
23761 +++ linux/fs/aufs/i_op_ren.c    2023-09-03 02:21:58.863304341 +0200
23762 @@ -0,0 +1,1257 @@
23763 +// SPDX-License-Identifier: GPL-2.0
23764 +/*
23765 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23766 + *
23767 + * This program is free software; you can redistribute it and/or modify
23768 + * it under the terms of the GNU General Public License as published by
23769 + * the Free Software Foundation; either version 2 of the License, or
23770 + * (at your option) any later version.
23771 + *
23772 + * This program is distributed in the hope that it will be useful,
23773 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23774 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23775 + * GNU General Public License for more details.
23776 + *
23777 + * You should have received a copy of the GNU General Public License
23778 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23779 + */
23780 +
23781 +/*
23782 + * inode operation (rename entry)
23783 + * todo: this is crazy monster
23784 + */
23785 +
23786 +#include <linux/iversion.h>
23787 +#include "aufs.h"
23788 +
23789 +enum { AuSRC, AuDST, AuSrcDst };
23790 +enum { AuPARENT, AuCHILD, AuParentChild };
23791 +
23792 +#define AuRen_ISDIR_SRC                1
23793 +#define AuRen_ISDIR_DST                (1 << 1)
23794 +#define AuRen_ISSAMEDIR                (1 << 2)
23795 +#define AuRen_WHSRC            (1 << 3)
23796 +#define AuRen_WHDST            (1 << 4)
23797 +#define AuRen_MNT_WRITE                (1 << 5)
23798 +#define AuRen_DT_DSTDIR                (1 << 6)
23799 +#define AuRen_DIROPQ_SRC       (1 << 7)
23800 +#define AuRen_DIROPQ_DST       (1 << 8)
23801 +#define AuRen_DIRREN           (1 << 9)
23802 +#define AuRen_DROPPED_SRC      (1 << 10)
23803 +#define AuRen_DROPPED_DST      (1 << 11)
23804 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
23805 +#define au_fset_ren(flags, name) \
23806 +       do { (flags) |= AuRen_##name; } while (0)
23807 +#define au_fclr_ren(flags, name) \
23808 +       do { (flags) &= ~AuRen_##name; } while (0)
23809 +
23810 +#ifndef CONFIG_AUFS_DIRREN
23811 +#undef AuRen_DIRREN
23812 +#define AuRen_DIRREN           0
23813 +#endif
23814 +
23815 +struct au_ren_args {
23816 +       struct {
23817 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
23818 +                       *wh_dentry;
23819 +               struct inode *dir, *inode;
23820 +               struct au_hinode *hdir, *hinode;
23821 +               struct au_dtime dt[AuParentChild];
23822 +               aufs_bindex_t btop, bdiropq;
23823 +       } sd[AuSrcDst];
23824 +
23825 +#define src_dentry     sd[AuSRC].dentry
23826 +#define src_dir                sd[AuSRC].dir
23827 +#define src_inode      sd[AuSRC].inode
23828 +#define src_h_dentry   sd[AuSRC].h_dentry
23829 +#define src_parent     sd[AuSRC].parent
23830 +#define src_h_parent   sd[AuSRC].h_parent
23831 +#define src_wh_dentry  sd[AuSRC].wh_dentry
23832 +#define src_hdir       sd[AuSRC].hdir
23833 +#define src_hinode     sd[AuSRC].hinode
23834 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
23835 +#define src_dt         sd[AuSRC].dt
23836 +#define src_btop       sd[AuSRC].btop
23837 +#define src_bdiropq    sd[AuSRC].bdiropq
23838 +
23839 +#define dst_dentry     sd[AuDST].dentry
23840 +#define dst_dir                sd[AuDST].dir
23841 +#define dst_inode      sd[AuDST].inode
23842 +#define dst_h_dentry   sd[AuDST].h_dentry
23843 +#define dst_parent     sd[AuDST].parent
23844 +#define dst_h_parent   sd[AuDST].h_parent
23845 +#define dst_wh_dentry  sd[AuDST].wh_dentry
23846 +#define dst_hdir       sd[AuDST].hdir
23847 +#define dst_hinode     sd[AuDST].hinode
23848 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
23849 +#define dst_dt         sd[AuDST].dt
23850 +#define dst_btop       sd[AuDST].btop
23851 +#define dst_bdiropq    sd[AuDST].bdiropq
23852 +
23853 +       struct dentry *h_trap;
23854 +       struct au_branch *br;
23855 +       struct path h_path;
23856 +       struct au_nhash whlist;
23857 +       aufs_bindex_t btgt, src_bwh;
23858 +
23859 +       struct {
23860 +               unsigned short auren_flags;
23861 +               unsigned char flags;    /* syscall parameter */
23862 +               unsigned char exchange;
23863 +       } __packed;
23864 +
23865 +       struct au_whtmp_rmdir *thargs;
23866 +       struct dentry *h_dst;
23867 +       struct au_hinode *h_root;
23868 +};
23869 +
23870 +/* ---------------------------------------------------------------------- */
23871 +
23872 +/*
23873 + * functions for reverting.
23874 + * when an error happened in a single rename systemcall, we should revert
23875 + * everything as if nothing happened.
23876 + * we don't need to revert the copied-up/down the parent dir since they are
23877 + * harmless.
23878 + */
23879 +
23880 +#define RevertFailure(fmt, ...) do { \
23881 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23882 +               ##__VA_ARGS__, err, rerr); \
23883 +       err = -EIO; \
23884 +} while (0)
23885 +
23886 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
23887 +{
23888 +       int rerr;
23889 +       struct dentry *d;
23890 +#define src_or_dst(member) a->sd[idx].member
23891 +
23892 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
23893 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23894 +       rerr = au_diropq_remove(d, a->btgt);
23895 +       au_hn_inode_unlock(src_or_dst(hinode));
23896 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
23897 +       if (rerr)
23898 +               RevertFailure("remove diropq %pd", d);
23899 +
23900 +#undef src_or_dst_
23901 +}
23902 +
23903 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23904 +{
23905 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23906 +               au_ren_do_rev_diropq(err, a, AuSRC);
23907 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23908 +               au_ren_do_rev_diropq(err, a, AuDST);
23909 +}
23910 +
23911 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
23912 +{
23913 +       int rerr;
23914 +       struct inode *delegated;
23915 +       struct path h_ppath = {
23916 +               .dentry = a->src_h_parent,
23917 +               .mnt    = a->h_path.mnt
23918 +       };
23919 +
23920 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
23921 +       rerr = PTR_ERR(a->h_path.dentry);
23922 +       if (IS_ERR(a->h_path.dentry)) {
23923 +               RevertFailure("lkup one %pd", a->src_dentry);
23924 +               return;
23925 +       }
23926 +
23927 +       delegated = NULL;
23928 +       rerr = vfsub_rename(a->dst_h_dir,
23929 +                           au_h_dptr(a->src_dentry, a->btgt),
23930 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
23931 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23932 +               pr_warn("cannot retry for NFSv4 delegation"
23933 +                       " for an internal rename\n");
23934 +               iput(delegated);
23935 +       }
23936 +       d_drop(a->h_path.dentry);
23937 +       dput(a->h_path.dentry);
23938 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23939 +       if (rerr)
23940 +               RevertFailure("rename %pd", a->src_dentry);
23941 +}
23942 +
23943 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
23944 +{
23945 +       int rerr;
23946 +       struct inode *delegated;
23947 +       struct path h_ppath = {
23948 +               .dentry = a->dst_h_parent,
23949 +               .mnt    = a->h_path.mnt
23950 +       };
23951 +
23952 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
23953 +       rerr = PTR_ERR(a->h_path.dentry);
23954 +       if (IS_ERR(a->h_path.dentry)) {
23955 +               RevertFailure("lkup one %pd", a->dst_dentry);
23956 +               return;
23957 +       }
23958 +       if (d_is_positive(a->h_path.dentry)) {
23959 +               d_drop(a->h_path.dentry);
23960 +               dput(a->h_path.dentry);
23961 +               return;
23962 +       }
23963 +
23964 +       delegated = NULL;
23965 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
23966 +                           &delegated, a->flags);
23967 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23968 +               pr_warn("cannot retry for NFSv4 delegation"
23969 +                       " for an internal rename\n");
23970 +               iput(delegated);
23971 +       }
23972 +       d_drop(a->h_path.dentry);
23973 +       dput(a->h_path.dentry);
23974 +       if (!rerr)
23975 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
23976 +       else
23977 +               RevertFailure("rename %pd", a->h_dst);
23978 +}
23979 +
23980 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
23981 +{
23982 +       int rerr;
23983 +
23984 +       a->h_path.dentry = a->src_wh_dentry;
23985 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
23986 +       au_set_dbwh(a->src_dentry, a->src_bwh);
23987 +       if (rerr)
23988 +               RevertFailure("unlink %pd", a->src_wh_dentry);
23989 +}
23990 +#undef RevertFailure
23991 +
23992 +/* ---------------------------------------------------------------------- */
23993 +
23994 +/*
23995 + * when we have to copyup the renaming entry, do it with the rename-target name
23996 + * in order to minimize the cost (the later actual rename is unnecessary).
23997 + * otherwise rename it on the target branch.
23998 + */
23999 +static int au_ren_or_cpup(struct au_ren_args *a)
24000 +{
24001 +       int err;
24002 +       struct dentry *d;
24003 +       struct inode *delegated;
24004 +
24005 +       d = a->src_dentry;
24006 +       if (au_dbtop(d) == a->btgt) {
24007 +               a->h_path.dentry = a->dst_h_dentry;
24008 +               AuDebugOn(au_dbtop(d) != a->btgt);
24009 +               delegated = NULL;
24010 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
24011 +                                  a->dst_h_dir, &a->h_path, &delegated,
24012 +                                  a->flags);
24013 +               if (unlikely(err == -EWOULDBLOCK)) {
24014 +                       pr_warn("cannot retry for NFSv4 delegation"
24015 +                               " for an internal rename\n");
24016 +                       iput(delegated);
24017 +               }
24018 +       } else
24019 +               BUG();
24020 +
24021 +       if (!err && a->h_dst)
24022 +               /* it will be set to dinfo later */
24023 +               dget(a->h_dst);
24024 +
24025 +       return err;
24026 +}
24027 +
24028 +/* cf. aufs_rmdir() */
24029 +static int au_ren_del_whtmp(struct au_ren_args *a)
24030 +{
24031 +       int err;
24032 +       struct inode *dir;
24033 +
24034 +       dir = a->dst_dir;
24035 +       SiMustAnyLock(dir->i_sb);
24036 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
24037 +                                    au_sbi(dir->i_sb)->si_dirwh)
24038 +           || au_test_fs_remote(a->h_dst->d_sb)) {
24039 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
24040 +               if (unlikely(err))
24041 +                       pr_warn("failed removing whtmp dir %pd (%d), "
24042 +                               "ignored.\n", a->h_dst, err);
24043 +       } else {
24044 +               au_nhash_wh_free(&a->thargs->whlist);
24045 +               a->thargs->whlist = a->whlist;
24046 +               a->whlist.nh_num = 0;
24047 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24048 +               dput(a->h_dst);
24049 +               a->thargs = NULL;
24050 +       }
24051 +
24052 +       return 0;
24053 +}
24054 +
24055 +/* make it 'opaque' dir. */
24056 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
24057 +{
24058 +       int err;
24059 +       struct dentry *d, *diropq;
24060 +#define src_or_dst(member) a->sd[idx].member
24061 +
24062 +       err = 0;
24063 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
24064 +       src_or_dst(bdiropq) = au_dbdiropq(d);
24065 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24066 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24067 +       diropq = au_diropq_create(d, a->btgt);
24068 +       au_hn_inode_unlock(src_or_dst(hinode));
24069 +       if (IS_ERR(diropq))
24070 +               err = PTR_ERR(diropq);
24071 +       else
24072 +               dput(diropq);
24073 +
24074 +#undef src_or_dst_
24075 +       return err;
24076 +}
24077 +
24078 +static int au_ren_diropq(struct au_ren_args *a)
24079 +{
24080 +       int err;
24081 +       unsigned char always;
24082 +       struct dentry *d;
24083 +
24084 +       err = 0;
24085 +       d = a->dst_dentry; /* already renamed on the branch */
24086 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24087 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24088 +           && !au_ftest_ren(a->auren_flags, DIRREN)
24089 +           && a->btgt != au_dbdiropq(a->src_dentry)
24090 +           && (a->dst_wh_dentry
24091 +               || a->btgt <= au_dbdiropq(d)
24092 +               /* hide the lower to keep xino */
24093 +               /* the lowers may not be a dir, but we hide them anyway */
24094 +               || a->btgt < au_dbbot(d)
24095 +               || always)) {
24096 +               AuDbg("here\n");
24097 +               err = au_ren_do_diropq(a, AuSRC);
24098 +               if (unlikely(err))
24099 +                       goto out;
24100 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
24101 +       }
24102 +       if (!a->exchange)
24103 +               goto out; /* success */
24104 +
24105 +       d = a->src_dentry; /* already renamed on the branch */
24106 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24107 +           && a->btgt != au_dbdiropq(a->dst_dentry)
24108 +           && (a->btgt < au_dbdiropq(d)
24109 +               || a->btgt < au_dbbot(d)
24110 +               || always)) {
24111 +               AuDbgDentry(a->src_dentry);
24112 +               AuDbgDentry(a->dst_dentry);
24113 +               err = au_ren_do_diropq(a, AuDST);
24114 +               if (unlikely(err))
24115 +                       goto out_rev_src;
24116 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
24117 +       }
24118 +       goto out; /* success */
24119 +
24120 +out_rev_src:
24121 +       AuDbg("err %d, reverting src\n", err);
24122 +       au_ren_rev_diropq(err, a);
24123 +out:
24124 +       return err;
24125 +}
24126 +
24127 +static int do_rename(struct au_ren_args *a)
24128 +{
24129 +       int err;
24130 +       struct dentry *d, *h_d;
24131 +
24132 +       if (!a->exchange) {
24133 +               /* prepare workqueue args for asynchronous rmdir */
24134 +               h_d = a->dst_h_dentry;
24135 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24136 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
24137 +                   && d_is_positive(h_d)) {
24138 +                       err = -ENOMEM;
24139 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24140 +                                                        GFP_NOFS);
24141 +                       if (unlikely(!a->thargs))
24142 +                               goto out;
24143 +                       a->h_dst = dget(h_d);
24144 +               }
24145 +
24146 +               /* create whiteout for src_dentry */
24147 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
24148 +                       a->src_bwh = au_dbwh(a->src_dentry);
24149 +                       AuDebugOn(a->src_bwh >= 0);
24150 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24151 +                                                       a->src_h_parent);
24152 +                       err = PTR_ERR(a->src_wh_dentry);
24153 +                       if (IS_ERR(a->src_wh_dentry))
24154 +                               goto out_thargs;
24155 +               }
24156 +
24157 +               /* lookup whiteout for dentry */
24158 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
24159 +                       h_d = au_wh_lkup(a->dst_h_parent,
24160 +                                        &a->dst_dentry->d_name, a->br);
24161 +                       err = PTR_ERR(h_d);
24162 +                       if (IS_ERR(h_d))
24163 +                               goto out_whsrc;
24164 +                       if (d_is_negative(h_d))
24165 +                               dput(h_d);
24166 +                       else
24167 +                               a->dst_wh_dentry = h_d;
24168 +               }
24169 +
24170 +               /* rename dentry to tmpwh */
24171 +               if (a->thargs) {
24172 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
24173 +                       if (unlikely(err))
24174 +                               goto out_whdst;
24175 +
24176 +                       d = a->dst_dentry;
24177 +                       au_set_h_dptr(d, a->btgt, NULL);
24178 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
24179 +                       if (unlikely(err))
24180 +                               goto out_whtmp;
24181 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
24182 +               }
24183 +       }
24184 +
24185 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
24186 +#if 0 /* debugging */
24187 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24188 +              && d_is_positive(a->dst_h_dentry)
24189 +              && a->src_btop != a->btgt);
24190 +#endif
24191 +
24192 +       /* rename by vfs_rename or cpup */
24193 +       err = au_ren_or_cpup(a);
24194 +       if (unlikely(err))
24195 +               /* leave the copied-up one */
24196 +               goto out_whtmp;
24197 +
24198 +       /* make dir opaque */
24199 +       err = au_ren_diropq(a);
24200 +       if (unlikely(err))
24201 +               goto out_rename;
24202 +
24203 +       /* update target timestamps */
24204 +       if (a->exchange) {
24205 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24206 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24207 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24208 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24209 +       }
24210 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
24211 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24212 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24213 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24214 +
24215 +       if (!a->exchange) {
24216 +               /* remove whiteout for dentry */
24217 +               if (a->dst_wh_dentry) {
24218 +                       a->h_path.dentry = a->dst_wh_dentry;
24219 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24220 +                                                 a->dst_dentry);
24221 +                       if (unlikely(err))
24222 +                               goto out_diropq;
24223 +               }
24224 +
24225 +               /* remove whtmp */
24226 +               if (a->thargs)
24227 +                       au_ren_del_whtmp(a); /* ignore this error */
24228 +
24229 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24230 +       }
24231 +       err = 0;
24232 +       goto out_success;
24233 +
24234 +out_diropq:
24235 +       au_ren_rev_diropq(err, a);
24236 +out_rename:
24237 +       au_ren_rev_rename(err, a);
24238 +       dput(a->h_dst);
24239 +out_whtmp:
24240 +       if (a->thargs)
24241 +               au_ren_rev_whtmp(err, a);
24242 +out_whdst:
24243 +       dput(a->dst_wh_dentry);
24244 +       a->dst_wh_dentry = NULL;
24245 +out_whsrc:
24246 +       if (a->src_wh_dentry)
24247 +               au_ren_rev_whsrc(err, a);
24248 +out_success:
24249 +       dput(a->src_wh_dentry);
24250 +       dput(a->dst_wh_dentry);
24251 +out_thargs:
24252 +       if (a->thargs) {
24253 +               dput(a->h_dst);
24254 +               au_whtmp_rmdir_free(a->thargs);
24255 +               a->thargs = NULL;
24256 +       }
24257 +out:
24258 +       return err;
24259 +}
24260 +
24261 +/* ---------------------------------------------------------------------- */
24262 +
24263 +/*
24264 + * test if @dentry dir can be rename destination or not.
24265 + * success means, it is a logically empty dir.
24266 + */
24267 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
24268 +{
24269 +       return au_test_empty(dentry, whlist);
24270 +}
24271 +
24272 +/*
24273 + * test if @a->src_dentry dir can be rename source or not.
24274 + * if it can, return 0.
24275 + * success means,
24276 + * - it is a logically empty dir.
24277 + * - or, it exists on writable branch and has no children including whiteouts
24278 + *   on the lower branch unless DIRREN is on.
24279 + */
24280 +static int may_rename_srcdir(struct au_ren_args *a)
24281 +{
24282 +       int err;
24283 +       unsigned int rdhash;
24284 +       aufs_bindex_t btop, btgt;
24285 +       struct dentry *dentry;
24286 +       struct super_block *sb;
24287 +       struct au_sbinfo *sbinfo;
24288 +
24289 +       dentry = a->src_dentry;
24290 +       sb = dentry->d_sb;
24291 +       sbinfo = au_sbi(sb);
24292 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24293 +               au_fset_ren(a->auren_flags, DIRREN);
24294 +
24295 +       btgt = a->btgt;
24296 +       btop = au_dbtop(dentry);
24297 +       if (btop != btgt) {
24298 +               struct au_nhash whlist;
24299 +
24300 +               SiMustAnyLock(sb);
24301 +               rdhash = sbinfo->si_rdhash;
24302 +               if (!rdhash)
24303 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24304 +                                                          dentry));
24305 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24306 +               if (unlikely(err))
24307 +                       goto out;
24308 +               err = au_test_empty(dentry, &whlist);
24309 +               au_nhash_wh_free(&whlist);
24310 +               goto out;
24311 +       }
24312 +
24313 +       if (btop == au_dbtaildir(dentry))
24314 +               return 0; /* success */
24315 +
24316 +       err = au_test_empty_lower(dentry);
24317 +
24318 +out:
24319 +       if (err == -ENOTEMPTY) {
24320 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
24321 +                       err = 0;
24322 +               } else {
24323 +                       AuWarn1("renaming dir who has child(ren) on multiple "
24324 +                               "branches, is not supported\n");
24325 +                       err = -EXDEV;
24326 +               }
24327 +       }
24328 +       return err;
24329 +}
24330 +
24331 +/* side effect: sets whlist and h_dentry */
24332 +static int au_ren_may_dir(struct au_ren_args *a)
24333 +{
24334 +       int err;
24335 +       unsigned int rdhash;
24336 +       struct dentry *d;
24337 +
24338 +       d = a->dst_dentry;
24339 +       SiMustAnyLock(d->d_sb);
24340 +
24341 +       err = 0;
24342 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
24343 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
24344 +               if (!rdhash)
24345 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24346 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24347 +               if (unlikely(err))
24348 +                       goto out;
24349 +
24350 +               if (!a->exchange) {
24351 +                       au_set_dbtop(d, a->dst_btop);
24352 +                       err = may_rename_dstdir(d, &a->whlist);
24353 +                       au_set_dbtop(d, a->btgt);
24354 +               } else
24355 +                       err = may_rename_srcdir(a);
24356 +       }
24357 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
24358 +       if (unlikely(err))
24359 +               goto out;
24360 +
24361 +       d = a->src_dentry;
24362 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
24363 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24364 +               err = may_rename_srcdir(a);
24365 +               if (unlikely(err)) {
24366 +                       au_nhash_wh_free(&a->whlist);
24367 +                       a->whlist.nh_num = 0;
24368 +               }
24369 +       }
24370 +out:
24371 +       return err;
24372 +}
24373 +
24374 +/* ---------------------------------------------------------------------- */
24375 +
24376 +/*
24377 + * simple tests for rename.
24378 + * following the checks in vfs, plus the parent-child relationship.
24379 + */
24380 +static int au_may_ren(struct au_ren_args *a)
24381 +{
24382 +       int err, isdir;
24383 +       struct inode *h_inode;
24384 +
24385 +       if (a->src_btop == a->btgt) {
24386 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
24387 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
24388 +               if (unlikely(err))
24389 +                       goto out;
24390 +               err = -EINVAL;
24391 +               if (unlikely(a->src_h_dentry == a->h_trap))
24392 +                       goto out;
24393 +       }
24394 +
24395 +       err = 0;
24396 +       if (a->dst_btop != a->btgt)
24397 +               goto out;
24398 +
24399 +       err = -ENOTEMPTY;
24400 +       if (unlikely(a->dst_h_dentry == a->h_trap))
24401 +               goto out;
24402 +
24403 +       err = -EIO;
24404 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
24405 +       if (d_really_is_negative(a->dst_dentry)) {
24406 +               if (d_is_negative(a->dst_h_dentry))
24407 +                       err = au_may_add(a->dst_dentry, a->btgt,
24408 +                                        a->dst_h_parent, isdir);
24409 +       } else {
24410 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
24411 +                       goto out;
24412 +               h_inode = d_inode(a->dst_h_dentry);
24413 +               if (h_inode->i_nlink)
24414 +                       err = au_may_del(a->dst_dentry, a->btgt,
24415 +                                        a->dst_h_parent, isdir);
24416 +       }
24417 +
24418 +out:
24419 +       if (unlikely(err == -ENOENT || err == -EEXIST))
24420 +               err = -EIO;
24421 +       AuTraceErr(err);
24422 +       return err;
24423 +}
24424 +
24425 +/* ---------------------------------------------------------------------- */
24426 +
24427 +/*
24428 + * locking order
24429 + * (VFS)
24430 + * - src_dir and dir by lock_rename()
24431 + * - inode if exists
24432 + * (aufs)
24433 + * - lock all
24434 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24435 + *     + si_read_lock
24436 + *     + di_write_lock2_child()
24437 + *       + di_write_lock_child()
24438 + *        + ii_write_lock_child()
24439 + *       + di_write_lock_child2()
24440 + *        + ii_write_lock_child2()
24441 + *     + src_parent and parent
24442 + *       + di_write_lock_parent()
24443 + *        + ii_write_lock_parent()
24444 + *       + di_write_lock_parent2()
24445 + *        + ii_write_lock_parent2()
24446 + *   + lower src_dir and dir by vfsub_lock_rename()
24447 + *   + verify the every relationships between child and parent. if any
24448 + *     of them failed, unlock all and return -EBUSY.
24449 + */
24450 +static void au_ren_unlock(struct au_ren_args *a)
24451 +{
24452 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24453 +                           a->dst_h_parent, a->dst_hdir);
24454 +       if (au_ftest_ren(a->auren_flags, DIRREN)
24455 +           && a->h_root)
24456 +               au_hn_inode_unlock(a->h_root);
24457 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
24458 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
24459 +}
24460 +
24461 +static int au_ren_lock(struct au_ren_args *a)
24462 +{
24463 +       int err;
24464 +       unsigned int udba;
24465 +
24466 +       err = 0;
24467 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24468 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
24469 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24470 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
24471 +
24472 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
24473 +       if (unlikely(err))
24474 +               goto out;
24475 +       au_fset_ren(a->auren_flags, MNT_WRITE);
24476 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24477 +               struct dentry *root;
24478 +               struct inode *dir;
24479 +
24480 +               /*
24481 +                * sbinfo is already locked, so this ii_read_lock is
24482 +                * unnecessary. but our debugging feature checks it.
24483 +                */
24484 +               root = a->src_inode->i_sb->s_root;
24485 +               if (root != a->src_parent && root != a->dst_parent) {
24486 +                       dir = d_inode(root);
24487 +                       ii_read_lock_parent3(dir);
24488 +                       a->h_root = au_hi(dir, a->btgt);
24489 +                       ii_read_unlock(dir);
24490 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24491 +               }
24492 +       }
24493 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24494 +                                     a->dst_h_parent, a->dst_hdir);
24495 +       udba = au_opt_udba(a->src_dentry->d_sb);
24496 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24497 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
24498 +               err = au_busy_or_stale();
24499 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
24500 +               err = au_h_verify(a->src_h_dentry, udba,
24501 +                                 d_inode(a->src_h_parent), a->src_h_parent,
24502 +                                 a->br);
24503 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
24504 +               err = au_h_verify(a->dst_h_dentry, udba,
24505 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
24506 +                                 a->br);
24507 +       if (!err)
24508 +               goto out; /* success */
24509 +
24510 +       err = au_busy_or_stale();
24511 +       au_ren_unlock(a);
24512 +
24513 +out:
24514 +       return err;
24515 +}
24516 +
24517 +/* ---------------------------------------------------------------------- */
24518 +
24519 +static void au_ren_refresh_dir(struct au_ren_args *a)
24520 +{
24521 +       struct inode *dir;
24522 +
24523 +       dir = a->dst_dir;
24524 +       inode_inc_iversion(dir);
24525 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24526 +               /* is this updating defined in POSIX? */
24527 +               au_cpup_attr_timesizes(a->src_inode);
24528 +               au_cpup_attr_nlink(dir, /*force*/1);
24529 +       }
24530 +       au_dir_ts(dir, a->btgt);
24531 +
24532 +       if (a->exchange) {
24533 +               dir = a->src_dir;
24534 +               inode_inc_iversion(dir);
24535 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24536 +                       /* is this updating defined in POSIX? */
24537 +                       au_cpup_attr_timesizes(a->dst_inode);
24538 +                       au_cpup_attr_nlink(dir, /*force*/1);
24539 +               }
24540 +               au_dir_ts(dir, a->btgt);
24541 +       }
24542 +
24543 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24544 +               return;
24545 +
24546 +       dir = a->src_dir;
24547 +       inode_inc_iversion(dir);
24548 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
24549 +               au_cpup_attr_nlink(dir, /*force*/1);
24550 +       au_dir_ts(dir, a->btgt);
24551 +}
24552 +
24553 +static void au_ren_refresh(struct au_ren_args *a)
24554 +{
24555 +       aufs_bindex_t bbot, bindex;
24556 +       struct dentry *d, *h_d;
24557 +       struct inode *i, *h_i;
24558 +       struct super_block *sb;
24559 +
24560 +       d = a->dst_dentry;
24561 +       d_drop(d);
24562 +       if (a->h_dst)
24563 +               /* already dget-ed by au_ren_or_cpup() */
24564 +               au_set_h_dptr(d, a->btgt, a->h_dst);
24565 +
24566 +       i = a->dst_inode;
24567 +       if (i) {
24568 +               if (!a->exchange) {
24569 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24570 +                               vfsub_drop_nlink(i);
24571 +                       else {
24572 +                               vfsub_dead_dir(i);
24573 +                               au_cpup_attr_timesizes(i);
24574 +                       }
24575 +                       au_update_dbrange(d, /*do_put_zero*/1);
24576 +               } else
24577 +                       au_cpup_attr_nlink(i, /*force*/1);
24578 +       } else {
24579 +               bbot = a->btgt;
24580 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
24581 +                       au_set_h_dptr(d, bindex, NULL);
24582 +               bbot = au_dbbot(d);
24583 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
24584 +                       au_set_h_dptr(d, bindex, NULL);
24585 +               au_update_dbrange(d, /*do_put_zero*/0);
24586 +       }
24587 +
24588 +       if (a->exchange
24589 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
24590 +               d_drop(a->src_dentry);
24591 +               if (au_ftest_ren(a->auren_flags, DIRREN))
24592 +                       au_set_dbwh(a->src_dentry, -1);
24593 +               return;
24594 +       }
24595 +
24596 +       d = a->src_dentry;
24597 +       au_set_dbwh(d, -1);
24598 +       bbot = au_dbbot(d);
24599 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24600 +               h_d = au_h_dptr(d, bindex);
24601 +               if (h_d)
24602 +                       au_set_h_dptr(d, bindex, NULL);
24603 +       }
24604 +       au_set_dbbot(d, a->btgt);
24605 +
24606 +       sb = d->d_sb;
24607 +       i = a->src_inode;
24608 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24609 +               return; /* success */
24610 +
24611 +       bbot = au_ibbot(i);
24612 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24613 +               h_i = au_h_iptr(i, bindex);
24614 +               if (h_i) {
24615 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24616 +                       /* ignore this error */
24617 +                       au_set_h_iptr(i, bindex, NULL, 0);
24618 +               }
24619 +       }
24620 +       au_set_ibbot(i, a->btgt);
24621 +}
24622 +
24623 +/* ---------------------------------------------------------------------- */
24624 +
24625 +/* mainly for link(2) and rename(2) */
24626 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
24627 +{
24628 +       aufs_bindex_t bdiropq, bwh;
24629 +       struct dentry *parent;
24630 +       struct au_branch *br;
24631 +
24632 +       parent = dentry->d_parent;
24633 +       IMustLock(d_inode(parent)); /* dir is locked */
24634 +
24635 +       bdiropq = au_dbdiropq(parent);
24636 +       bwh = au_dbwh(dentry);
24637 +       br = au_sbr(dentry->d_sb, btgt);
24638 +       if (au_br_rdonly(br)
24639 +           || (0 <= bdiropq && bdiropq < btgt)
24640 +           || (0 <= bwh && bwh < btgt))
24641 +               btgt = -1;
24642 +
24643 +       AuDbg("btgt %d\n", btgt);
24644 +       return btgt;
24645 +}
24646 +
24647 +/* sets src_btop, dst_btop and btgt */
24648 +static int au_ren_wbr(struct au_ren_args *a)
24649 +{
24650 +       int err;
24651 +       struct au_wr_dir_args wr_dir_args = {
24652 +               /* .force_btgt  = -1, */
24653 +               .flags          = AuWrDir_ADD_ENTRY
24654 +       };
24655 +
24656 +       a->src_btop = au_dbtop(a->src_dentry);
24657 +       a->dst_btop = au_dbtop(a->dst_dentry);
24658 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24659 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
24660 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
24661 +       wr_dir_args.force_btgt = a->src_btop;
24662 +       if (a->dst_inode && a->dst_btop < a->src_btop)
24663 +               wr_dir_args.force_btgt = a->dst_btop;
24664 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24665 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24666 +       a->btgt = err;
24667 +       if (a->exchange)
24668 +               au_update_dbtop(a->dst_dentry);
24669 +
24670 +       return err;
24671 +}
24672 +
24673 +static void au_ren_dt(struct au_ren_args *a)
24674 +{
24675 +       a->h_path.dentry = a->src_h_parent;
24676 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
24677 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
24678 +               a->h_path.dentry = a->dst_h_parent;
24679 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24680 +       }
24681 +
24682 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
24683 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24684 +           && !a->exchange)
24685 +               return;
24686 +
24687 +       a->h_path.dentry = a->src_h_dentry;
24688 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
24689 +       if (d_is_positive(a->dst_h_dentry)) {
24690 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
24691 +               a->h_path.dentry = a->dst_h_dentry;
24692 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24693 +       }
24694 +}
24695 +
24696 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
24697 +{
24698 +       struct dentry *h_d;
24699 +       struct inode *h_inode;
24700 +
24701 +       au_dtime_revert(a->src_dt + AuPARENT);
24702 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
24703 +               au_dtime_revert(a->dst_dt + AuPARENT);
24704 +
24705 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
24706 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
24707 +               h_inode = d_inode(h_d);
24708 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
24709 +               au_dtime_revert(a->src_dt + AuCHILD);
24710 +               inode_unlock(h_inode);
24711 +
24712 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
24713 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
24714 +                       h_inode = d_inode(h_d);
24715 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
24716 +                       au_dtime_revert(a->dst_dt + AuCHILD);
24717 +                       inode_unlock(h_inode);
24718 +               }
24719 +       }
24720 +}
24721 +
24722 +/* ---------------------------------------------------------------------- */
24723 +
24724 +int aufs_rename(struct mnt_idmap *idmap,
24725 +               struct inode *_src_dir, struct dentry *_src_dentry,
24726 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
24727 +               unsigned int _flags)
24728 +{
24729 +       int err, lock_flags;
24730 +       void *rev;
24731 +       /* reduce stack space */
24732 +       struct au_ren_args *a;
24733 +       struct au_pin pin;
24734 +
24735 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
24736 +       IMustLock(_src_dir);
24737 +       IMustLock(_dst_dir);
24738 +
24739 +       err = -EINVAL;
24740 +       if (unlikely(_flags & RENAME_WHITEOUT))
24741 +               goto out;
24742 +
24743 +       err = -ENOMEM;
24744 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24745 +       a = kzalloc(sizeof(*a), GFP_NOFS);
24746 +       if (unlikely(!a))
24747 +               goto out;
24748 +
24749 +       a->flags = _flags;
24750 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24751 +                    && RENAME_EXCHANGE > U8_MAX);
24752 +       a->exchange = _flags & RENAME_EXCHANGE;
24753 +       a->src_dir = _src_dir;
24754 +       a->src_dentry = _src_dentry;
24755 +       a->src_inode = NULL;
24756 +       if (d_really_is_positive(a->src_dentry))
24757 +               a->src_inode = d_inode(a->src_dentry);
24758 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24759 +       a->dst_dir = _dst_dir;
24760 +       a->dst_dentry = _dst_dentry;
24761 +       a->dst_inode = NULL;
24762 +       if (d_really_is_positive(a->dst_dentry))
24763 +               a->dst_inode = d_inode(a->dst_dentry);
24764 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24765 +       if (a->dst_inode) {
24766 +               /*
24767 +                * if EXCHANGE && src is non-dir && dst is dir,
24768 +                * dst is not locked.
24769 +                */
24770 +               /* IMustLock(a->dst_inode); */
24771 +               au_igrab(a->dst_inode);
24772 +       }
24773 +
24774 +       err = -ENOTDIR;
24775 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
24776 +       if (d_is_dir(a->src_dentry)) {
24777 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
24778 +               if (unlikely(!a->exchange
24779 +                            && d_really_is_positive(a->dst_dentry)
24780 +                            && !d_is_dir(a->dst_dentry)))
24781 +                       goto out_free;
24782 +               lock_flags |= AuLock_DIRS;
24783 +       }
24784 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24785 +               au_fset_ren(a->auren_flags, ISDIR_DST);
24786 +               if (unlikely(!a->exchange
24787 +                            && d_really_is_positive(a->src_dentry)
24788 +                            && !d_is_dir(a->src_dentry)))
24789 +                       goto out_free;
24790 +               lock_flags |= AuLock_DIRS;
24791 +       }
24792 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24793 +                                       lock_flags);
24794 +       if (unlikely(err))
24795 +               goto out_free;
24796 +
24797 +       err = au_d_hashed_positive(a->src_dentry);
24798 +       if (unlikely(err))
24799 +               goto out_unlock;
24800 +       err = -ENOENT;
24801 +       if (a->dst_inode) {
24802 +               /*
24803 +                * If it is a dir, VFS unhash it before this
24804 +                * function. It means we cannot rely upon d_unhashed().
24805 +                */
24806 +               if (unlikely(!a->dst_inode->i_nlink))
24807 +                       goto out_unlock;
24808 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24809 +                       err = au_d_hashed_positive(a->dst_dentry);
24810 +                       if (unlikely(err && !a->exchange))
24811 +                               goto out_unlock;
24812 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24813 +                       goto out_unlock;
24814 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
24815 +               goto out_unlock;
24816 +
24817 +       /*
24818 +        * is it possible?
24819 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
24820 +        * there may exist a problem somewhere else.
24821 +        */
24822 +       err = -EINVAL;
24823 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
24824 +               goto out_unlock;
24825 +
24826 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
24827 +       di_write_lock_parent(a->dst_parent);
24828 +
24829 +       /* which branch we process */
24830 +       err = au_ren_wbr(a);
24831 +       if (unlikely(err < 0))
24832 +               goto out_parent;
24833 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
24834 +       a->h_path.mnt = au_br_mnt(a->br);
24835 +
24836 +       /* are they available to be renamed */
24837 +       err = au_ren_may_dir(a);
24838 +       if (unlikely(err))
24839 +               goto out_children;
24840 +
24841 +       /* prepare the writable parent dir on the same branch */
24842 +       if (a->dst_btop == a->btgt) {
24843 +               au_fset_ren(a->auren_flags, WHDST);
24844 +       } else {
24845 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
24846 +               if (unlikely(err))
24847 +                       goto out_children;
24848 +       }
24849 +
24850 +       err = 0;
24851 +       if (!a->exchange) {
24852 +               if (a->src_dir != a->dst_dir) {
24853 +                       /*
24854 +                        * this temporary unlock is safe,
24855 +                        * because both dir->i_mutex are locked.
24856 +                        */
24857 +                       di_write_unlock(a->dst_parent);
24858 +                       di_write_lock_parent(a->src_parent);
24859 +                       err = au_wr_dir_need_wh(a->src_dentry,
24860 +                                               au_ftest_ren(a->auren_flags,
24861 +                                                            ISDIR_SRC),
24862 +                                               &a->btgt);
24863 +                       di_write_unlock(a->src_parent);
24864 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
24865 +                                             /*isdir*/1);
24866 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
24867 +               } else
24868 +                       err = au_wr_dir_need_wh(a->src_dentry,
24869 +                                               au_ftest_ren(a->auren_flags,
24870 +                                                            ISDIR_SRC),
24871 +                                               &a->btgt);
24872 +       }
24873 +       if (unlikely(err < 0))
24874 +               goto out_children;
24875 +       if (err)
24876 +               au_fset_ren(a->auren_flags, WHSRC);
24877 +
24878 +       /* cpup src */
24879 +       if (a->src_btop != a->btgt) {
24880 +               err = au_pin(&pin, a->src_dentry, a->btgt,
24881 +                            au_opt_udba(a->src_dentry->d_sb),
24882 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24883 +               if (!err) {
24884 +                       struct au_cp_generic cpg = {
24885 +                               .dentry = a->src_dentry,
24886 +                               .bdst   = a->btgt,
24887 +                               .bsrc   = a->src_btop,
24888 +                               .len    = -1,
24889 +                               .pin    = &pin,
24890 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24891 +                       };
24892 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
24893 +                       err = au_sio_cpup_simple(&cpg);
24894 +                       au_unpin(&pin);
24895 +               }
24896 +               if (unlikely(err))
24897 +                       goto out_children;
24898 +               a->src_btop = a->btgt;
24899 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
24900 +               if (!a->exchange)
24901 +                       au_fset_ren(a->auren_flags, WHSRC);
24902 +       }
24903 +
24904 +       /* cpup dst */
24905 +       if (a->exchange && a->dst_inode
24906 +           && a->dst_btop != a->btgt) {
24907 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
24908 +                            au_opt_udba(a->dst_dentry->d_sb),
24909 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24910 +               if (!err) {
24911 +                       struct au_cp_generic cpg = {
24912 +                               .dentry = a->dst_dentry,
24913 +                               .bdst   = a->btgt,
24914 +                               .bsrc   = a->dst_btop,
24915 +                               .len    = -1,
24916 +                               .pin    = &pin,
24917 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24918 +                       };
24919 +                       err = au_sio_cpup_simple(&cpg);
24920 +                       au_unpin(&pin);
24921 +               }
24922 +               if (unlikely(err))
24923 +                       goto out_children;
24924 +               a->dst_btop = a->btgt;
24925 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
24926 +       }
24927 +
24928 +       /* lock them all */
24929 +       err = au_ren_lock(a);
24930 +       if (unlikely(err))
24931 +               /* leave the copied-up one */
24932 +               goto out_children;
24933 +
24934 +       if (!a->exchange) {
24935 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24936 +                       err = au_may_ren(a);
24937 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24938 +                       err = -ENAMETOOLONG;
24939 +               if (unlikely(err))
24940 +                       goto out_hdir;
24941 +       }
24942 +
24943 +       /* store timestamps to be revertible */
24944 +       au_ren_dt(a);
24945 +
24946 +       /* store dirren info */
24947 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24948 +               err = au_dr_rename(a->src_dentry, a->btgt,
24949 +                                  &a->dst_dentry->d_name, &rev);
24950 +               AuTraceErr(err);
24951 +               if (unlikely(err))
24952 +                       goto out_dt;
24953 +       }
24954 +
24955 +       /* here we go */
24956 +       err = do_rename(a);
24957 +       if (unlikely(err))
24958 +               goto out_dirren;
24959 +
24960 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24961 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
24962 +
24963 +       /* update dir attributes */
24964 +       au_ren_refresh_dir(a);
24965 +
24966 +       /* dput/iput all lower dentries */
24967 +       au_ren_refresh(a);
24968 +
24969 +       goto out_hdir; /* success */
24970 +
24971 +out_dirren:
24972 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24973 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
24974 +out_dt:
24975 +       au_ren_rev_dt(err, a);
24976 +out_hdir:
24977 +       au_ren_unlock(a);
24978 +out_children:
24979 +       au_nhash_wh_free(&a->whlist);
24980 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
24981 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
24982 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
24983 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
24984 +       }
24985 +out_parent:
24986 +       if (!err) {
24987 +               if (d_unhashed(a->src_dentry))
24988 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
24989 +               if (d_unhashed(a->dst_dentry))
24990 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
24991 +               if (!a->exchange)
24992 +                       d_move(a->src_dentry, a->dst_dentry);
24993 +               else {
24994 +                       d_exchange(a->src_dentry, a->dst_dentry);
24995 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
24996 +                               d_drop(a->dst_dentry);
24997 +               }
24998 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
24999 +                       d_drop(a->src_dentry);
25000 +       } else {
25001 +               au_update_dbtop(a->dst_dentry);
25002 +               if (!a->dst_inode)
25003 +                       d_drop(a->dst_dentry);
25004 +       }
25005 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
25006 +               di_write_unlock(a->dst_parent);
25007 +       else
25008 +               di_write_unlock2(a->src_parent, a->dst_parent);
25009 +out_unlock:
25010 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
25011 +out_free:
25012 +       iput(a->dst_inode);
25013 +       if (a->thargs)
25014 +               au_whtmp_rmdir_free(a->thargs);
25015 +       au_kfree_rcu(a);
25016 +out:
25017 +       AuTraceErr(err);
25018 +       return err;
25019 +}
25020 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
25021 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
25022 +++ linux/fs/aufs/Kconfig       2022-11-05 23:02:18.959222617 +0100
25023 @@ -0,0 +1,199 @@
25024 +# SPDX-License-Identifier: GPL-2.0
25025 +config AUFS_FS
25026 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
25027 +       help
25028 +       Aufs is a stackable unification filesystem such as Unionfs,
25029 +       which unifies several directories and provides a merged single
25030 +       directory.
25031 +       In the early days, aufs was entirely re-designed and
25032 +       re-implemented Unionfs Version 1.x series. Introducing many
25033 +       original ideas, approaches and improvements, it becomes totally
25034 +       different from Unionfs while keeping the basic features.
25035 +
25036 +if AUFS_FS
25037 +choice
25038 +       prompt "Maximum number of branches"
25039 +       default AUFS_BRANCH_MAX_127
25040 +       help
25041 +       Specifies the maximum number of branches (or member directories)
25042 +       in a single aufs. The larger value consumes more system
25043 +       resources and has a minor impact to performance.
25044 +config AUFS_BRANCH_MAX_127
25045 +       bool "127"
25046 +       help
25047 +       Specifies the maximum number of branches (or member directories)
25048 +       in a single aufs. The larger value consumes more system
25049 +       resources and has a minor impact to performance.
25050 +config AUFS_BRANCH_MAX_511
25051 +       bool "511"
25052 +       help
25053 +       Specifies the maximum number of branches (or member directories)
25054 +       in a single aufs. The larger value consumes more system
25055 +       resources and has a minor impact to performance.
25056 +config AUFS_BRANCH_MAX_1023
25057 +       bool "1023"
25058 +       help
25059 +       Specifies the maximum number of branches (or member directories)
25060 +       in a single aufs. The larger value consumes more system
25061 +       resources and has a minor impact to performance.
25062 +config AUFS_BRANCH_MAX_32767
25063 +       bool "32767"
25064 +       help
25065 +       Specifies the maximum number of branches (or member directories)
25066 +       in a single aufs. The larger value consumes more system
25067 +       resources and has a minor impact to performance.
25068 +endchoice
25069 +
25070 +config AUFS_SBILIST
25071 +       bool
25072 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
25073 +       default y
25074 +       help
25075 +       Automatic configuration for internal use.
25076 +       When aufs supports Magic SysRq or /proc, enabled automatically.
25077 +
25078 +config AUFS_HNOTIFY
25079 +       bool "Detect direct branch access (bypassing aufs)"
25080 +       help
25081 +       If you want to modify files on branches directly, eg. bypassing aufs,
25082 +       and want aufs to detect the changes of them fully, then enable this
25083 +       option and use 'udba=notify' mount option.
25084 +       Currently there is only one available configuration, "fsnotify".
25085 +       It will have a negative impact to the performance.
25086 +       See detail in aufs.5.
25087 +
25088 +choice
25089 +       prompt "method" if AUFS_HNOTIFY
25090 +       default AUFS_HFSNOTIFY
25091 +config AUFS_HFSNOTIFY
25092 +       bool "fsnotify"
25093 +       select FSNOTIFY
25094 +endchoice
25095 +
25096 +config AUFS_EXPORT
25097 +       bool "NFS-exportable aufs"
25098 +       depends on EXPORTFS
25099 +       help
25100 +       If you want to export your mounted aufs via NFS, then enable this
25101 +       option. There are several requirements for this configuration.
25102 +       See detail in aufs.5.
25103 +
25104 +config AUFS_INO_T_64
25105 +       bool
25106 +       depends on AUFS_EXPORT
25107 +       depends on 64BIT && !(ALPHA || S390)
25108 +       default y
25109 +       help
25110 +       Automatic configuration for internal use.
25111 +       /* typedef unsigned long/int __kernel_ino_t */
25112 +       /* alpha and s390x are int */
25113 +
25114 +config AUFS_XATTR
25115 +       bool "support for XATTR/EA (including Security Labels)"
25116 +       help
25117 +       If your branch fs supports XATTR/EA and you want to make them
25118 +       available in aufs too, then enable this opsion and specify the
25119 +       branch attributes for EA.
25120 +       See detail in aufs.5.
25121 +
25122 +config AUFS_FHSM
25123 +       bool "File-based Hierarchical Storage Management"
25124 +       help
25125 +       Hierarchical Storage Management (or HSM) is a well-known feature
25126 +       in the storage world. Aufs provides this feature as file-based.
25127 +       with multiple branches.
25128 +       These multiple branches are prioritized, ie. the topmost one
25129 +       should be the fastest drive and be used heavily.
25130 +
25131 +config AUFS_RDU
25132 +       bool "Readdir in userspace"
25133 +       help
25134 +       Aufs has two methods to provide a merged view for a directory,
25135 +       by a user-space library and by kernel-space natively. The latter
25136 +       is always enabled but sometimes large and slow.
25137 +       If you enable this option, install the library in aufs2-util
25138 +       package, and set some environment variables for your readdir(3),
25139 +       then the work will be handled in user-space which generally
25140 +       shows better performance in most cases.
25141 +       See detail in aufs.5.
25142 +
25143 +config AUFS_DIRREN
25144 +       bool "Workaround for rename(2)-ing a directory"
25145 +       help
25146 +       By default, aufs returns EXDEV error in renameing a dir who has
25147 +       his child on the lower branch, since it is a bad idea to issue
25148 +       rename(2) internally for every lower branch. But user may not
25149 +       accept this behaviour. So here is a workaround to allow such
25150 +       rename(2) and store some extra information on the writable
25151 +       branch. Obviously this costs high (and I don't like it).
25152 +       To use this feature, you need to enable this configuration AND
25153 +       to specify the mount option `dirren.'
25154 +       See details in aufs.5 and the design documents.
25155 +
25156 +config AUFS_SHWH
25157 +       bool "Show whiteouts"
25158 +       help
25159 +       If you want to make the whiteouts in aufs visible, then enable
25160 +       this option and specify 'shwh' mount option. Although it may
25161 +       sounds like philosophy or something, but in technically it
25162 +       simply shows the name of whiteout with keeping its behaviour.
25163 +
25164 +config AUFS_BR_RAMFS
25165 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
25166 +       help
25167 +       If you want to use ramfs as an aufs branch fs, then enable this
25168 +       option. Generally tmpfs is recommended.
25169 +       Aufs prohibited them to be a branch fs by default, because
25170 +       initramfs becomes unusable after switch_root or something
25171 +       generally. If you sets initramfs as an aufs branch and boot your
25172 +       system by switch_root, you will meet a problem easily since the
25173 +       files in initramfs may be inaccessible.
25174 +       Unless you are going to use ramfs as an aufs branch fs without
25175 +       switch_root or something, leave it N.
25176 +
25177 +config AUFS_BR_FUSE
25178 +       bool "Fuse fs as an aufs branch"
25179 +       depends on FUSE_FS
25180 +       select AUFS_POLL
25181 +       help
25182 +       If you want to use fuse-based userspace filesystem as an aufs
25183 +       branch fs, then enable this option.
25184 +       It implements the internal poll(2) operation which is
25185 +       implemented by fuse only (curretnly).
25186 +
25187 +config AUFS_POLL
25188 +       bool
25189 +       help
25190 +       Automatic configuration for internal use.
25191 +
25192 +config AUFS_BR_HFSPLUS
25193 +       bool "Hfsplus as an aufs branch"
25194 +       depends on HFSPLUS_FS
25195 +       default y
25196 +       help
25197 +       If you want to use hfsplus fs as an aufs branch fs, then enable
25198 +       this option. This option introduces a small overhead at
25199 +       copying-up a file on hfsplus.
25200 +
25201 +config AUFS_BDEV_LOOP
25202 +       bool
25203 +       depends on BLK_DEV_LOOP
25204 +       default y
25205 +       help
25206 +       Automatic configuration for internal use.
25207 +       Convert =[ym] into =y.
25208 +
25209 +config AUFS_DEBUG
25210 +       bool "Debug aufs"
25211 +       help
25212 +       Enable this to compile aufs internal debug code.
25213 +       It will have a negative impact to the performance.
25214 +
25215 +config AUFS_MAGIC_SYSRQ
25216 +       bool
25217 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
25218 +       default y
25219 +       help
25220 +       Automatic configuration for internal use.
25221 +       When aufs supports Magic SysRq, enabled automatically.
25222 +endif
25223 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
25224 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
25225 +++ linux/fs/aufs/lcnt.h        2022-11-05 23:02:18.965889284 +0100
25226 @@ -0,0 +1,186 @@
25227 +/* SPDX-License-Identifier: GPL-2.0 */
25228 +/*
25229 + * Copyright (C) 2018-2022 Junjiro R. Okajima
25230 + *
25231 + * This program is free software; you can redistribute it and/or modify
25232 + * it under the terms of the GNU General Public License as published by
25233 + * the Free Software Foundation; either version 2 of the License, or
25234 + * (at your option) any later version.
25235 + *
25236 + * This program is distributed in the hope that it will be useful,
25237 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25238 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25239 + * GNU General Public License for more details.
25240 + *
25241 + * You should have received a copy of the GNU General Public License
25242 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25243 + */
25244 +
25245 +/*
25246 + * simple long counter wrapper
25247 + */
25248 +
25249 +#ifndef __AUFS_LCNT_H__
25250 +#define __AUFS_LCNT_H__
25251 +
25252 +#ifdef __KERNEL__
25253 +
25254 +#include "debug.h"
25255 +
25256 +#define AuLCntATOMIC   1
25257 +#define AuLCntPCPUCNT  2
25258 +/*
25259 + * why does percpu_refcount require extra synchronize_rcu()s in
25260 + * au_br_do_free()
25261 + */
25262 +#define AuLCntPCPUREF  3
25263 +
25264 +/* #define AuLCntChosen        AuLCntATOMIC */
25265 +#define AuLCntChosen   AuLCntPCPUCNT
25266 +/* #define AuLCntChosen        AuLCntPCPUREF */
25267 +
25268 +#if AuLCntChosen == AuLCntATOMIC
25269 +#include <linux/atomic.h>
25270 +
25271 +typedef atomic_long_t au_lcnt_t;
25272 +
25273 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25274 +{
25275 +       atomic_long_set(cnt, 0);
25276 +       return 0;
25277 +}
25278 +
25279 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25280 +{
25281 +       /* empty */
25282 +}
25283 +
25284 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25285 +                              int do_sync __maybe_unused)
25286 +{
25287 +       /* empty */
25288 +}
25289 +
25290 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25291 +{
25292 +       atomic_long_inc(cnt);
25293 +}
25294 +
25295 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25296 +{
25297 +       atomic_long_dec(cnt);
25298 +}
25299 +
25300 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25301 +{
25302 +       return atomic_long_read(cnt);
25303 +}
25304 +#endif
25305 +
25306 +#if AuLCntChosen == AuLCntPCPUCNT
25307 +#include <linux/percpu_counter.h>
25308 +
25309 +typedef struct percpu_counter au_lcnt_t;
25310 +
25311 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25312 +{
25313 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
25314 +}
25315 +
25316 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25317 +{
25318 +       /* empty */
25319 +}
25320 +
25321 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25322 +{
25323 +       percpu_counter_destroy(cnt);
25324 +}
25325 +
25326 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25327 +{
25328 +       percpu_counter_inc(cnt);
25329 +}
25330 +
25331 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25332 +{
25333 +       percpu_counter_dec(cnt);
25334 +}
25335 +
25336 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25337 +{
25338 +       s64 n;
25339 +
25340 +       n = percpu_counter_sum(cnt);
25341 +       BUG_ON(n < 0);
25342 +       if (LONG_MAX != LLONG_MAX
25343 +           && n > LONG_MAX)
25344 +               AuWarn1("%s\n", "wrap-around");
25345 +
25346 +       return n;
25347 +}
25348 +#endif
25349 +
25350 +#if AuLCntChosen == AuLCntPCPUREF
25351 +#include <linux/percpu-refcount.h>
25352 +
25353 +typedef struct percpu_ref au_lcnt_t;
25354 +
25355 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25356 +{
25357 +       if (!release)
25358 +               release = percpu_ref_exit;
25359 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25360 +}
25361 +
25362 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25363 +{
25364 +       synchronize_rcu();
25365 +}
25366 +
25367 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25368 +{
25369 +       percpu_ref_kill(cnt);
25370 +       if (do_sync)
25371 +               au_lcnt_wait_for_fin(cnt);
25372 +}
25373 +
25374 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25375 +{
25376 +       percpu_ref_get(cnt);
25377 +}
25378 +
25379 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25380 +{
25381 +       percpu_ref_put(cnt);
25382 +}
25383 +
25384 +/*
25385 + * avoid calling this func as possible.
25386 + */
25387 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25388 +{
25389 +       long l;
25390 +
25391 +       percpu_ref_switch_to_atomic_sync(cnt);
25392 +       l = atomic_long_read(&cnt->count);
25393 +       if (do_rev)
25394 +               percpu_ref_switch_to_percpu(cnt);
25395 +
25396 +       /* percpu_ref is initialized by 1 instead of 0 */
25397 +       return l - 1;
25398 +}
25399 +#endif
25400 +
25401 +#ifdef CONFIG_AUFS_DEBUG
25402 +#define AuLCntZero(val) do {                   \
25403 +       long l = val;                           \
25404 +       if (l)                                  \
25405 +               AuDbg("%s = %ld\n", #val, l);   \
25406 +} while (0)
25407 +#else
25408 +#define AuLCntZero(val)                do {} while (0)
25409 +#endif
25410 +
25411 +#endif /* __KERNEL__ */
25412 +#endif /* __AUFS_LCNT_H__ */
25413 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
25414 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
25415 +++ linux/fs/aufs/loop.c        2022-11-05 23:02:18.965889284 +0100
25416 @@ -0,0 +1,148 @@
25417 +// SPDX-License-Identifier: GPL-2.0
25418 +/*
25419 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25420 + *
25421 + * This program is free software; you can redistribute it and/or modify
25422 + * it under the terms of the GNU General Public License as published by
25423 + * the Free Software Foundation; either version 2 of the License, or
25424 + * (at your option) any later version.
25425 + *
25426 + * This program is distributed in the hope that it will be useful,
25427 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25428 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25429 + * GNU General Public License for more details.
25430 + *
25431 + * You should have received a copy of the GNU General Public License
25432 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25433 + */
25434 +
25435 +/*
25436 + * support for loopback block device as a branch
25437 + */
25438 +
25439 +#include "aufs.h"
25440 +
25441 +/* added into drivers/block/loop.c */
25442 +static struct file *(*backing_file_func)(struct super_block *sb);
25443 +
25444 +/*
25445 + * test if two lower dentries have overlapping branches.
25446 + */
25447 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25448 +{
25449 +       struct super_block *h_sb;
25450 +       struct file *backing_file;
25451 +
25452 +       if (unlikely(!backing_file_func)) {
25453 +               /* don't load "loop" module here */
25454 +               backing_file_func = symbol_get(loop_backing_file);
25455 +               if (unlikely(!backing_file_func))
25456 +                       /* "loop" module is not loaded */
25457 +                       return 0;
25458 +       }
25459 +
25460 +       h_sb = h_adding->d_sb;
25461 +       backing_file = backing_file_func(h_sb);
25462 +       if (!backing_file)
25463 +               return 0;
25464 +
25465 +       h_adding = backing_file->f_path.dentry;
25466 +       /*
25467 +        * h_adding can be local NFS.
25468 +        * in this case aufs cannot detect the loop.
25469 +        */
25470 +       if (unlikely(h_adding->d_sb == sb))
25471 +               return 1;
25472 +       return !!au_test_subdir(h_adding, sb->s_root);
25473 +}
25474 +
25475 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25476 +int au_test_loopback_kthread(void)
25477 +{
25478 +       int ret;
25479 +       struct task_struct *tsk = current;
25480 +       char c, comm[sizeof(tsk->comm)];
25481 +
25482 +       ret = 0;
25483 +       if (tsk->flags & PF_KTHREAD) {
25484 +               get_task_comm(comm, tsk);
25485 +               c = comm[4];
25486 +               ret = ('0' <= c && c <= '9'
25487 +                      && !strncmp(comm, "loop", 4));
25488 +       }
25489 +
25490 +       return ret;
25491 +}
25492 +
25493 +/* ---------------------------------------------------------------------- */
25494 +
25495 +#define au_warn_loopback_step  16
25496 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25497 +static unsigned long *au_warn_loopback_array;
25498 +
25499 +void au_warn_loopback(struct super_block *h_sb)
25500 +{
25501 +       int i, new_nelem;
25502 +       unsigned long *a, magic;
25503 +       static DEFINE_SPINLOCK(spin);
25504 +
25505 +       magic = h_sb->s_magic;
25506 +       spin_lock(&spin);
25507 +       a = au_warn_loopback_array;
25508 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25509 +               if (a[i] == magic) {
25510 +                       spin_unlock(&spin);
25511 +                       return;
25512 +               }
25513 +
25514 +       /* h_sb is new to us, print it */
25515 +       if (i < au_warn_loopback_nelem) {
25516 +               a[i] = magic;
25517 +               goto pr;
25518 +       }
25519 +
25520 +       /* expand the array */
25521 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25522 +       a = au_kzrealloc(au_warn_loopback_array,
25523 +                        au_warn_loopback_nelem * sizeof(unsigned long),
25524 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25525 +                        /*may_shrink*/0);
25526 +       if (a) {
25527 +               au_warn_loopback_nelem = new_nelem;
25528 +               au_warn_loopback_array = a;
25529 +               a[i] = magic;
25530 +               goto pr;
25531 +       }
25532 +
25533 +       spin_unlock(&spin);
25534 +       AuWarn1("realloc failed, ignored\n");
25535 +       return;
25536 +
25537 +pr:
25538 +       spin_unlock(&spin);
25539 +       pr_warn("you may want to try another patch for loopback file "
25540 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25541 +}
25542 +
25543 +int au_loopback_init(void)
25544 +{
25545 +       int err;
25546 +       struct super_block *sb __maybe_unused;
25547 +
25548 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25549 +
25550 +       err = 0;
25551 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25552 +                                        sizeof(unsigned long), GFP_NOFS);
25553 +       if (unlikely(!au_warn_loopback_array))
25554 +               err = -ENOMEM;
25555 +
25556 +       return err;
25557 +}
25558 +
25559 +void au_loopback_fin(void)
25560 +{
25561 +       if (backing_file_func)
25562 +               symbol_put(loop_backing_file);
25563 +       au_kfree_try_rcu(au_warn_loopback_array);
25564 +}
25565 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
25566 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
25567 +++ linux/fs/aufs/loop.h        2022-11-05 23:02:18.965889284 +0100
25568 @@ -0,0 +1,55 @@
25569 +/* SPDX-License-Identifier: GPL-2.0 */
25570 +/*
25571 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25572 + *
25573 + * This program is free software; you can redistribute it and/or modify
25574 + * it under the terms of the GNU General Public License as published by
25575 + * the Free Software Foundation; either version 2 of the License, or
25576 + * (at your option) any later version.
25577 + *
25578 + * This program is distributed in the hope that it will be useful,
25579 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25580 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25581 + * GNU General Public License for more details.
25582 + *
25583 + * You should have received a copy of the GNU General Public License
25584 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25585 + */
25586 +
25587 +/*
25588 + * support for loopback mount as a branch
25589 + */
25590 +
25591 +#ifndef __AUFS_LOOP_H__
25592 +#define __AUFS_LOOP_H__
25593 +
25594 +#ifdef __KERNEL__
25595 +
25596 +struct dentry;
25597 +struct super_block;
25598 +
25599 +#ifdef CONFIG_AUFS_BDEV_LOOP
25600 +/* drivers/block/loop.c */
25601 +struct file *loop_backing_file(struct super_block *sb);
25602 +
25603 +/* loop.c */
25604 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25605 +int au_test_loopback_kthread(void);
25606 +void au_warn_loopback(struct super_block *h_sb);
25607 +
25608 +int au_loopback_init(void);
25609 +void au_loopback_fin(void);
25610 +#else
25611 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25612 +
25613 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25614 +          struct dentry *h_adding)
25615 +AuStubInt0(au_test_loopback_kthread, void)
25616 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25617 +
25618 +AuStubInt0(au_loopback_init, void)
25619 +AuStubVoid(au_loopback_fin, void)
25620 +#endif /* BLK_DEV_LOOP */
25621 +
25622 +#endif /* __KERNEL__ */
25623 +#endif /* __AUFS_LOOP_H__ */
25624 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
25625 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
25626 +++ linux/fs/aufs/magic.mk      2022-11-05 23:02:18.965889284 +0100
25627 @@ -0,0 +1,31 @@
25628 +# SPDX-License-Identifier: GPL-2.0
25629 +
25630 +# defined in ${srctree}/fs/fuse/inode.c
25631 +# tristate
25632 +ifdef CONFIG_FUSE_FS
25633 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25634 +endif
25635 +
25636 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25637 +# tristate
25638 +ifdef CONFIG_XFS_FS
25639 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25640 +endif
25641 +
25642 +# defined in ${srctree}/fs/configfs/mount.c
25643 +# tristate
25644 +ifdef CONFIG_CONFIGFS_FS
25645 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25646 +endif
25647 +
25648 +# defined in ${srctree}/fs/ubifs/ubifs.h
25649 +# tristate
25650 +ifdef CONFIG_UBIFS_FS
25651 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25652 +endif
25653 +
25654 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25655 +# tristate
25656 +ifdef CONFIG_HFSPLUS_FS
25657 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25658 +endif
25659 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
25660 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
25661 +++ linux/fs/aufs/Makefile      2022-11-05 23:02:18.959222617 +0100
25662 @@ -0,0 +1,46 @@
25663 +# SPDX-License-Identifier: GPL-2.0
25664 +
25665 +include ${src}/magic.mk
25666 +ifeq (${CONFIG_AUFS_FS},m)
25667 +include ${src}/conf.mk
25668 +endif
25669 +-include ${src}/priv_def.mk
25670 +
25671 +# cf. include/linux/kernel.h
25672 +# enable pr_debug
25673 +ccflags-y += -DDEBUG
25674 +# sparse requires the full pathname
25675 +ifdef M
25676 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
25677 +else
25678 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
25679 +endif
25680 +
25681 +obj-$(CONFIG_AUFS_FS) += aufs.o
25682 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
25683 +       wkq.o vfsub.o dcsub.o \
25684 +       cpup.o whout.o wbr_policy.o \
25685 +       dinfo.o dentry.o \
25686 +       dynop.o \
25687 +       finfo.o file.o f_op.o \
25688 +       dir.o vdir.o \
25689 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
25690 +       mvdown.o ioctl.o
25691 +
25692 +# all are boolean
25693 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
25694 +aufs-$(CONFIG_SYSFS) += sysfs.o
25695 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25696 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25697 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25698 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
25699 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
25700 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25701 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
25702 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
25703 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
25704 +aufs-$(CONFIG_AUFS_POLL) += poll.o
25705 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
25706 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25707 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25708 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
25709 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
25710 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
25711 +++ linux/fs/aufs/module.c      2022-11-05 23:02:18.965889284 +0100
25712 @@ -0,0 +1,273 @@
25713 +// SPDX-License-Identifier: GPL-2.0
25714 +/*
25715 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25716 + *
25717 + * This program is free software; you can redistribute it and/or modify
25718 + * it under the terms of the GNU General Public License as published by
25719 + * the Free Software Foundation; either version 2 of the License, or
25720 + * (at your option) any later version.
25721 + *
25722 + * This program is distributed in the hope that it will be useful,
25723 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25724 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25725 + * GNU General Public License for more details.
25726 + *
25727 + * You should have received a copy of the GNU General Public License
25728 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25729 + */
25730 +
25731 +/*
25732 + * module global variables and operations
25733 + */
25734 +
25735 +#include <linux/module.h>
25736 +#include <linux/seq_file.h>
25737 +#include "aufs.h"
25738 +
25739 +/* shrinkable realloc */
25740 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25741 +{
25742 +       size_t sz;
25743 +       int diff;
25744 +
25745 +       sz = 0;
25746 +       diff = -1;
25747 +       if (p) {
25748 +#if 0 /* unused */
25749 +               if (!new_sz) {
25750 +                       au_kfree_rcu(p);
25751 +                       p = NULL;
25752 +                       goto out;
25753 +               }
25754 +#else
25755 +               AuDebugOn(!new_sz);
25756 +#endif
25757 +               sz = ksize(p);
25758 +               diff = au_kmidx_sub(sz, new_sz);
25759 +       }
25760 +       if (sz && !diff)
25761 +               goto out;
25762 +
25763 +       if (sz < new_sz)
25764 +               /* expand or SLOB */
25765 +               p = krealloc(p, new_sz, gfp);
25766 +       else if (new_sz < sz && may_shrink) {
25767 +               /* shrink */
25768 +               void *q;
25769 +
25770 +               q = kmalloc(new_sz, gfp);
25771 +               if (q) {
25772 +                       if (p) {
25773 +                               memcpy(q, p, new_sz);
25774 +                               au_kfree_try_rcu(p);
25775 +                       }
25776 +                       p = q;
25777 +               } else
25778 +                       p = NULL;
25779 +       }
25780 +
25781 +out:
25782 +       return p;
25783 +}
25784 +
25785 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25786 +                  int may_shrink)
25787 +{
25788 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
25789 +       if (p && new_sz > nused)
25790 +               memset(p + nused, 0, new_sz - nused);
25791 +       return p;
25792 +}
25793 +
25794 +/* ---------------------------------------------------------------------- */
25795 +/*
25796 + * aufs caches
25797 + */
25798 +struct kmem_cache *au_cache[AuCache_Last];
25799 +
25800 +static void au_cache_fin(void)
25801 +{
25802 +       int i;
25803 +
25804 +       /*
25805 +        * Make sure all delayed rcu free inodes are flushed before we
25806 +        * destroy cache.
25807 +        */
25808 +       rcu_barrier();
25809 +
25810 +       /* excluding AuCache_HNOTIFY */
25811 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25812 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
25813 +               kmem_cache_destroy(au_cache[i]);
25814 +               au_cache[i] = NULL;
25815 +       }
25816 +}
25817 +
25818 +static int __init au_cache_init(void)
25819 +{
25820 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25821 +       if (au_cache[AuCache_DINFO])
25822 +               /* SLAB_DESTROY_BY_RCU */
25823 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25824 +                                                      au_icntnr_init_once);
25825 +       if (au_cache[AuCache_ICNTNR])
25826 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25827 +                                                     au_fi_init_once);
25828 +       if (au_cache[AuCache_FINFO])
25829 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
25830 +       if (au_cache[AuCache_VDIR])
25831 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25832 +       if (au_cache[AuCache_DEHSTR])
25833 +               return 0;
25834 +
25835 +       au_cache_fin();
25836 +       return -ENOMEM;
25837 +}
25838 +
25839 +/* ---------------------------------------------------------------------- */
25840 +
25841 +int au_dir_roflags;
25842 +
25843 +#ifdef CONFIG_AUFS_SBILIST
25844 +/*
25845 + * iterate_supers_type() doesn't protect us from
25846 + * remounting (branch management)
25847 + */
25848 +struct hlist_bl_head au_sbilist;
25849 +#endif
25850 +
25851 +/*
25852 + * functions for module interface.
25853 + */
25854 +MODULE_LICENSE("GPL");
25855 +/* MODULE_LICENSE("GPL v2"); */
25856 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25857 +MODULE_DESCRIPTION(AUFS_NAME
25858 +       " -- Advanced multi layered unification filesystem");
25859 +MODULE_VERSION(AUFS_VERSION);
25860 +MODULE_ALIAS_FS(AUFS_NAME);
25861 +
25862 +/* this module parameter has no meaning when SYSFS is disabled */
25863 +int sysaufs_brs = 1;
25864 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25865 +module_param_named(brs, sysaufs_brs, int, 0444);
25866 +
25867 +/* this module parameter has no meaning when USER_NS is disabled */
25868 +bool au_userns;
25869 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25870 +module_param_named(allow_userns, au_userns, bool, 0444);
25871 +
25872 +/* ---------------------------------------------------------------------- */
25873 +
25874 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25875 +
25876 +int au_seq_path(struct seq_file *seq, struct path *path)
25877 +{
25878 +       int err;
25879 +
25880 +       err = seq_path(seq, path, au_esc_chars);
25881 +       if (err >= 0)
25882 +               err = 0;
25883 +       else
25884 +               err = -ENOMEM;
25885 +
25886 +       return err;
25887 +}
25888 +
25889 +/* ---------------------------------------------------------------------- */
25890 +
25891 +static int __init aufs_init(void)
25892 +{
25893 +       int err, i;
25894 +       char *p;
25895 +
25896 +       p = au_esc_chars;
25897 +       for (i = 1; i <= ' '; i++)
25898 +               *p++ = i;
25899 +       *p++ = '\\';
25900 +       *p++ = '\x7f';
25901 +       *p = 0;
25902 +
25903 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25904 +
25905 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25906 +       for (i = 0; i < AuIop_Last; i++)
25907 +               aufs_iop_nogetattr[i].getattr = NULL;
25908 +
25909 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
25910 +
25911 +       au_sbilist_init();
25912 +       sysaufs_brs_init();
25913 +       au_debug_init();
25914 +       au_dy_init();
25915 +       err = sysaufs_init();
25916 +       if (unlikely(err))
25917 +               goto out;
25918 +       err = dbgaufs_init();
25919 +       if (unlikely(err))
25920 +               goto out_sysaufs;
25921 +       err = au_procfs_init();
25922 +       if (unlikely(err))
25923 +               goto out_dbgaufs;
25924 +       err = au_wkq_init();
25925 +       if (unlikely(err))
25926 +               goto out_procfs;
25927 +       err = au_loopback_init();
25928 +       if (unlikely(err))
25929 +               goto out_wkq;
25930 +       err = au_hnotify_init();
25931 +       if (unlikely(err))
25932 +               goto out_loopback;
25933 +       err = au_sysrq_init();
25934 +       if (unlikely(err))
25935 +               goto out_hin;
25936 +       err = au_cache_init();
25937 +       if (unlikely(err))
25938 +               goto out_sysrq;
25939 +
25940 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25941 +       err = register_filesystem(&aufs_fs_type);
25942 +       if (unlikely(err))
25943 +               goto out_cache;
25944 +
25945 +       /* since we define pr_fmt, call printk directly */
25946 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25947 +       goto out; /* success */
25948 +
25949 +out_cache:
25950 +       au_cache_fin();
25951 +out_sysrq:
25952 +       au_sysrq_fin();
25953 +out_hin:
25954 +       au_hnotify_fin();
25955 +out_loopback:
25956 +       au_loopback_fin();
25957 +out_wkq:
25958 +       au_wkq_fin();
25959 +out_procfs:
25960 +       au_procfs_fin();
25961 +out_dbgaufs:
25962 +       dbgaufs_fin();
25963 +out_sysaufs:
25964 +       sysaufs_fin();
25965 +       au_dy_fin();
25966 +out:
25967 +       return err;
25968 +}
25969 +
25970 +static void __exit aufs_exit(void)
25971 +{
25972 +       unregister_filesystem(&aufs_fs_type);
25973 +       au_cache_fin();
25974 +       au_sysrq_fin();
25975 +       au_hnotify_fin();
25976 +       au_loopback_fin();
25977 +       au_wkq_fin();
25978 +       au_procfs_fin();
25979 +       dbgaufs_fin();
25980 +       sysaufs_fin();
25981 +       au_dy_fin();
25982 +}
25983 +
25984 +module_init(aufs_init);
25985 +module_exit(aufs_exit);
25986 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
25987 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
25988 +++ linux/fs/aufs/module.h      2022-11-05 23:02:18.969222617 +0100
25989 @@ -0,0 +1,180 @@
25990 +/* SPDX-License-Identifier: GPL-2.0 */
25991 +/*
25992 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25993 + *
25994 + * This program is free software; you can redistribute it and/or modify
25995 + * it under the terms of the GNU General Public License as published by
25996 + * the Free Software Foundation; either version 2 of the License, or
25997 + * (at your option) any later version.
25998 + *
25999 + * This program is distributed in the hope that it will be useful,
26000 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26001 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26002 + * GNU General Public License for more details.
26003 + *
26004 + * You should have received a copy of the GNU General Public License
26005 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26006 + */
26007 +
26008 +/*
26009 + * module initialization and module-global
26010 + */
26011 +
26012 +#ifndef __AUFS_MODULE_H__
26013 +#define __AUFS_MODULE_H__
26014 +
26015 +#ifdef __KERNEL__
26016 +
26017 +#include <linux/slab.h>
26018 +#include "debug.h"
26019 +#include "dentry.h"
26020 +#include "dir.h"
26021 +#include "file.h"
26022 +#include "inode.h"
26023 +
26024 +struct path;
26025 +struct seq_file;
26026 +
26027 +/* module parameters */
26028 +extern int sysaufs_brs;
26029 +extern bool au_userns;
26030 +
26031 +/* ---------------------------------------------------------------------- */
26032 +
26033 +extern int au_dir_roflags;
26034 +
26035 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
26036 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
26037 +                  int may_shrink);
26038 +
26039 +/*
26040 + * Comparing the size of the object with sizeof(struct rcu_head)
26041 + * case 1: object is always larger
26042 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
26043 + * case 2: object is always smaller
26044 + *     --> au_kfree_small()
26045 + * case 3: object can be any size
26046 + *     --> au_kfree_try_rcu()
26047 + */
26048 +
26049 +static inline void au_kfree_do_rcu(const void *p)
26050 +{
26051 +       struct {
26052 +               struct rcu_head rcu;
26053 +       } *a = (void *)p;
26054 +
26055 +       kfree_rcu(a, rcu);
26056 +}
26057 +
26058 +#define au_kfree_rcu(_p) do {                                          \
26059 +               typeof(_p) p = (_p);                                    \
26060 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
26061 +               if (p)                                                  \
26062 +                       au_kfree_do_rcu(p);                             \
26063 +       } while (0)
26064 +
26065 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
26066 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
26067 +
26068 +static inline void au_kfree_try_rcu(const void *p)
26069 +{
26070 +       if (!p)
26071 +               return;
26072 +       if (au_kfree_sz_test(p))
26073 +               au_kfree_do_rcu(p);
26074 +       else
26075 +               kfree(p);
26076 +}
26077 +
26078 +static inline void au_kfree_small(const void *p)
26079 +{
26080 +       if (!p)
26081 +               return;
26082 +       AuDebugOn(au_kfree_sz_test(p));
26083 +       kfree(p);
26084 +}
26085 +
26086 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26087 +{
26088 +#ifndef CONFIG_SLOB
26089 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
26090 +#else
26091 +       return -1; /* SLOB is untested */
26092 +#endif
26093 +}
26094 +
26095 +int au_seq_path(struct seq_file *seq, struct path *path);
26096 +
26097 +#ifdef CONFIG_PROC_FS
26098 +/* procfs.c */
26099 +int __init au_procfs_init(void);
26100 +void au_procfs_fin(void);
26101 +#else
26102 +AuStubInt0(au_procfs_init, void);
26103 +AuStubVoid(au_procfs_fin, void);
26104 +#endif
26105 +
26106 +/* ---------------------------------------------------------------------- */
26107 +
26108 +/* kmem cache */
26109 +enum {
26110 +       AuCache_DINFO,
26111 +       AuCache_ICNTNR,
26112 +       AuCache_FINFO,
26113 +       AuCache_VDIR,
26114 +       AuCache_DEHSTR,
26115 +       AuCache_HNOTIFY, /* must be last */
26116 +       AuCache_Last
26117 +};
26118 +
26119 +extern struct kmem_cache *au_cache[AuCache_Last];
26120 +
26121 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26122 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
26123 +#define AuCacheCtor(type, ctor)        \
26124 +       kmem_cache_create(#type, sizeof(struct type), \
26125 +                         __alignof__(struct type), AuCacheFlags, ctor)
26126 +
26127 +#define AuCacheFuncAlloc(name, index)                                  \
26128 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
26129 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
26130 +
26131 +#define AuCacheFuncs(name, index)                                      \
26132 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26133 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
26134 +                                                                       \
26135 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26136 +       { void *p = rcu;                                                \
26137 +               p -= offsetof(struct au_##name, rcu);                   \
26138 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
26139 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26140 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26141 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
26142 +                                                                       \
26143 +       static inline void au_cache_free_##name(struct au_##name *p)    \
26144 +       { /* au_cache_free_##name##_norcu(p); */                        \
26145 +               au_cache_free_##name##_rcu(p); }
26146 +
26147 +AuCacheFuncs(dinfo, DINFO);
26148 +AuCacheFuncAlloc(dinfo, DINFO);
26149 +
26150 +AuCacheFuncs(icntnr, ICNTNR);
26151 +static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
26152 +{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
26153 +
26154 +AuCacheFuncs(finfo, FINFO);
26155 +AuCacheFuncAlloc(finfo, FINFO);
26156 +
26157 +AuCacheFuncs(vdir, VDIR);
26158 +AuCacheFuncAlloc(vdir, VDIR);
26159 +
26160 +AuCacheFuncs(vdir_dehstr, DEHSTR);
26161 +AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
26162 +
26163 +#ifdef CONFIG_AUFS_HNOTIFY
26164 +AuCacheFuncs(hnotify, HNOTIFY);
26165 +AuCacheFuncAlloc(hnotify, HNOTIFY);
26166 +#endif
26167 +
26168 +#endif /* __KERNEL__ */
26169 +#endif /* __AUFS_MODULE_H__ */
26170 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
26171 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
26172 +++ linux/fs/aufs/mvdown.c      2022-11-05 23:02:18.969222617 +0100
26173 @@ -0,0 +1,706 @@
26174 +// SPDX-License-Identifier: GPL-2.0
26175 +/*
26176 + * Copyright (C) 2011-2022 Junjiro R. Okajima
26177 + *
26178 + * This program is free software; you can redistribute it and/or modify
26179 + * it under the terms of the GNU General Public License as published by
26180 + * the Free Software Foundation; either version 2 of the License, or
26181 + * (at your option) any later version.
26182 + *
26183 + * This program is distributed in the hope that it will be useful,
26184 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26185 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26186 + * GNU General Public License for more details.
26187 + *
26188 + * You should have received a copy of the GNU General Public License
26189 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26190 + */
26191 +
26192 +/*
26193 + * move-down, opposite of copy-up
26194 + */
26195 +
26196 +#include "aufs.h"
26197 +
26198 +struct au_mvd_args {
26199 +       struct {
26200 +               struct super_block *h_sb;
26201 +               struct dentry *h_parent;
26202 +               struct au_hinode *hdir;
26203 +               struct inode *h_dir, *h_inode;
26204 +               struct au_pin pin;
26205 +       } info[AUFS_MVDOWN_NARRAY];
26206 +
26207 +       struct aufs_mvdown mvdown;
26208 +       struct dentry *dentry, *parent;
26209 +       struct inode *inode, *dir;
26210 +       struct super_block *sb;
26211 +       aufs_bindex_t bopq, bwh, bfound;
26212 +       unsigned char rename_lock;
26213 +};
26214 +
26215 +#define mvd_errno              mvdown.au_errno
26216 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26217 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26218 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26219 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
26220 +
26221 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
26222 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
26223 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
26224 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
26225 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
26226 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
26227 +
26228 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
26229 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
26230 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
26231 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
26232 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
26233 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
26234 +
26235 +#define AU_MVD_PR(flag, ...) do {                      \
26236 +               if (flag)                               \
26237 +                       pr_err(__VA_ARGS__);            \
26238 +       } while (0)
26239 +
26240 +static int find_lower_writable(struct au_mvd_args *a)
26241 +{
26242 +       struct super_block *sb;
26243 +       aufs_bindex_t bindex, bbot;
26244 +       struct au_branch *br;
26245 +
26246 +       sb = a->sb;
26247 +       bindex = a->mvd_bsrc;
26248 +       bbot = au_sbbot(sb);
26249 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
26250 +               for (bindex++; bindex <= bbot; bindex++) {
26251 +                       br = au_sbr(sb, bindex);
26252 +                       if (au_br_fhsm(br->br_perm)
26253 +                           && !sb_rdonly(au_br_sb(br)))
26254 +                               return bindex;
26255 +               }
26256 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
26257 +               for (bindex++; bindex <= bbot; bindex++) {
26258 +                       br = au_sbr(sb, bindex);
26259 +                       if (!au_br_rdonly(br))
26260 +                               return bindex;
26261 +               }
26262 +       else
26263 +               for (bindex++; bindex <= bbot; bindex++) {
26264 +                       br = au_sbr(sb, bindex);
26265 +                       if (!sb_rdonly(au_br_sb(br))) {
26266 +                               if (au_br_rdonly(br))
26267 +                                       a->mvdown.flags
26268 +                                               |= AUFS_MVDOWN_ROLOWER_R;
26269 +                               return bindex;
26270 +                       }
26271 +               }
26272 +
26273 +       return -1;
26274 +}
26275 +
26276 +/* make the parent dir on bdst */
26277 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
26278 +{
26279 +       int err;
26280 +
26281 +       err = 0;
26282 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26283 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26284 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26285 +       a->mvd_h_dst_parent = NULL;
26286 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
26287 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26288 +       if (!a->mvd_h_dst_parent) {
26289 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26290 +               if (unlikely(err)) {
26291 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
26292 +                       goto out;
26293 +               }
26294 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26295 +       }
26296 +
26297 +out:
26298 +       AuTraceErr(err);
26299 +       return err;
26300 +}
26301 +
26302 +/* lock them all */
26303 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
26304 +{
26305 +       int err;
26306 +       struct dentry *h_trap;
26307 +
26308 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26309 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
26310 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26311 +                    au_opt_udba(a->sb),
26312 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26313 +       AuTraceErr(err);
26314 +       if (unlikely(err)) {
26315 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
26316 +               goto out;
26317 +       }
26318 +
26319 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26320 +               a->rename_lock = 0;
26321 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26322 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26323 +                           au_opt_udba(a->sb),
26324 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26325 +               err = au_do_pin(&a->mvd_pin_src);
26326 +               AuTraceErr(err);
26327 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26328 +               if (unlikely(err)) {
26329 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
26330 +                       goto out_dst;
26331 +               }
26332 +               goto out; /* success */
26333 +       }
26334 +
26335 +       a->rename_lock = 1;
26336 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
26337 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26338 +                    au_opt_udba(a->sb),
26339 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26340 +       AuTraceErr(err);
26341 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26342 +       if (unlikely(err)) {
26343 +               AU_MVD_PR(dmsg, "pin_src failed\n");
26344 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26345 +               goto out_dst;
26346 +       }
26347 +       au_pin_hdir_unlock(&a->mvd_pin_src);
26348 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26349 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
26350 +       if (h_trap) {
26351 +               err = (h_trap != a->mvd_h_src_parent);
26352 +               if (err)
26353 +                       err = (h_trap != a->mvd_h_dst_parent);
26354 +       }
26355 +       BUG_ON(err); /* it should never happen */
26356 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26357 +               err = -EBUSY;
26358 +               AuTraceErr(err);
26359 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26360 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26361 +               au_pin_hdir_lock(&a->mvd_pin_src);
26362 +               au_unpin(&a->mvd_pin_src);
26363 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26364 +               goto out_dst;
26365 +       }
26366 +       goto out; /* success */
26367 +
26368 +out_dst:
26369 +       au_unpin(&a->mvd_pin_dst);
26370 +out:
26371 +       AuTraceErr(err);
26372 +       return err;
26373 +}
26374 +
26375 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
26376 +{
26377 +       if (!a->rename_lock)
26378 +               au_unpin(&a->mvd_pin_src);
26379 +       else {
26380 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26381 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26382 +               au_pin_hdir_lock(&a->mvd_pin_src);
26383 +               au_unpin(&a->mvd_pin_src);
26384 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26385 +       }
26386 +       au_unpin(&a->mvd_pin_dst);
26387 +}
26388 +
26389 +/* copy-down the file */
26390 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
26391 +{
26392 +       int err;
26393 +       struct au_cp_generic cpg = {
26394 +               .dentry = a->dentry,
26395 +               .bdst   = a->mvd_bdst,
26396 +               .bsrc   = a->mvd_bsrc,
26397 +               .len    = -1,
26398 +               .pin    = &a->mvd_pin_dst,
26399 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
26400 +       };
26401 +
26402 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
26403 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26404 +               au_fset_cpup(cpg.flags, OVERWRITE);
26405 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26406 +               au_fset_cpup(cpg.flags, RWDST);
26407 +       err = au_sio_cpdown_simple(&cpg);
26408 +       if (unlikely(err))
26409 +               AU_MVD_PR(dmsg, "cpdown failed\n");
26410 +
26411 +       AuTraceErr(err);
26412 +       return err;
26413 +}
26414 +
26415 +/*
26416 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
26417 + * were sleeping
26418 + */
26419 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
26420 +{
26421 +       int err;
26422 +       struct path h_path;
26423 +       struct au_branch *br;
26424 +       struct inode *delegated;
26425 +
26426 +       br = au_sbr(a->sb, a->mvd_bdst);
26427 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26428 +       err = PTR_ERR(h_path.dentry);
26429 +       if (IS_ERR(h_path.dentry)) {
26430 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
26431 +               goto out;
26432 +       }
26433 +
26434 +       err = 0;
26435 +       if (d_is_positive(h_path.dentry)) {
26436 +               h_path.mnt = au_br_mnt(br);
26437 +               delegated = NULL;
26438 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
26439 +                                  &delegated, /*force*/0);
26440 +               if (unlikely(err == -EWOULDBLOCK)) {
26441 +                       pr_warn("cannot retry for NFSv4 delegation"
26442 +                               " for an internal unlink\n");
26443 +                       iput(delegated);
26444 +               }
26445 +               if (unlikely(err))
26446 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
26447 +       }
26448 +       dput(h_path.dentry);
26449 +
26450 +out:
26451 +       AuTraceErr(err);
26452 +       return err;
26453 +}
26454 +
26455 +/*
26456 + * unlink the topmost h_dentry
26457 + */
26458 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
26459 +{
26460 +       int err;
26461 +       struct path h_path;
26462 +       struct inode *delegated;
26463 +
26464 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26465 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26466 +       delegated = NULL;
26467 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26468 +       if (unlikely(err == -EWOULDBLOCK)) {
26469 +               pr_warn("cannot retry for NFSv4 delegation"
26470 +                       " for an internal unlink\n");
26471 +               iput(delegated);
26472 +       }
26473 +       if (unlikely(err))
26474 +               AU_MVD_PR(dmsg, "unlink failed\n");
26475 +
26476 +       AuTraceErr(err);
26477 +       return err;
26478 +}
26479 +
26480 +/* Since mvdown succeeded, we ignore an error of this function */
26481 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26482 +{
26483 +       int err;
26484 +       struct au_branch *br;
26485 +
26486 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26487 +       br = au_sbr(a->sb, a->mvd_bsrc);
26488 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26489 +       if (!err) {
26490 +               br = au_sbr(a->sb, a->mvd_bdst);
26491 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26492 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26493 +       }
26494 +       if (!err)
26495 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26496 +       else
26497 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26498 +}
26499 +
26500 +/*
26501 + * copy-down the file and unlink the bsrc file.
26502 + * - unlink the bdst whout if exist
26503 + * - copy-down the file (with whtmp name and rename)
26504 + * - unlink the bsrc file
26505 + */
26506 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26507 +{
26508 +       int err;
26509 +
26510 +       err = au_do_mkdir(dmsg, a);
26511 +       if (!err)
26512 +               err = au_do_lock(dmsg, a);
26513 +       if (unlikely(err))
26514 +               goto out;
26515 +
26516 +       /*
26517 +        * do not revert the activities we made on bdst since they should be
26518 +        * harmless in aufs.
26519 +        */
26520 +
26521 +       err = au_do_cpdown(dmsg, a);
26522 +       if (!err)
26523 +               err = au_do_unlink_wh(dmsg, a);
26524 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26525 +               err = au_do_unlink(dmsg, a);
26526 +       if (unlikely(err))
26527 +               goto out_unlock;
26528 +
26529 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
26530 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26531 +       if (find_lower_writable(a) < 0)
26532 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26533 +
26534 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26535 +               au_do_stfs(dmsg, a);
26536 +
26537 +       /* maintain internal array */
26538 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26539 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26540 +               au_set_dbtop(a->dentry, a->mvd_bdst);
26541 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26542 +               au_set_ibtop(a->inode, a->mvd_bdst);
26543 +       } else {
26544 +               /* hide the lower */
26545 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26546 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
26547 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26548 +               au_set_ibbot(a->inode, a->mvd_bsrc);
26549 +       }
26550 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
26551 +               au_set_dbbot(a->dentry, a->mvd_bdst);
26552 +       if (au_ibbot(a->inode) < a->mvd_bdst)
26553 +               au_set_ibbot(a->inode, a->mvd_bdst);
26554 +
26555 +out_unlock:
26556 +       au_do_unlock(dmsg, a);
26557 +out:
26558 +       AuTraceErr(err);
26559 +       return err;
26560 +}
26561 +
26562 +/* ---------------------------------------------------------------------- */
26563 +
26564 +/* make sure the file is idle */
26565 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26566 +{
26567 +       int err, plinked;
26568 +
26569 +       err = 0;
26570 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26571 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
26572 +           && au_dcount(a->dentry) == 1
26573 +           && atomic_read(&a->inode->i_count) == 1
26574 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
26575 +           && (!plinked || !au_plink_test(a->inode))
26576 +           && a->inode->i_nlink == 1)
26577 +               goto out;
26578 +
26579 +       err = -EBUSY;
26580 +       AU_MVD_PR(dmsg,
26581 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26582 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26583 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
26584 +                 a->mvd_h_src_inode->i_nlink,
26585 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
26586 +
26587 +out:
26588 +       AuTraceErr(err);
26589 +       return err;
26590 +}
26591 +
26592 +/* make sure the parent dir is fine */
26593 +static int au_mvd_args_parent(const unsigned char dmsg,
26594 +                             struct au_mvd_args *a)
26595 +{
26596 +       int err;
26597 +       aufs_bindex_t bindex;
26598 +
26599 +       err = 0;
26600 +       if (unlikely(au_alive_dir(a->parent))) {
26601 +               err = -ENOENT;
26602 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
26603 +               goto out;
26604 +       }
26605 +
26606 +       a->bopq = au_dbdiropq(a->parent);
26607 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26608 +       AuDbg("b%d\n", bindex);
26609 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26610 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26611 +               err = -EINVAL;
26612 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
26613 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26614 +                         a->bopq, a->mvd_bdst);
26615 +       }
26616 +
26617 +out:
26618 +       AuTraceErr(err);
26619 +       return err;
26620 +}
26621 +
26622 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26623 +                                   struct au_mvd_args *a)
26624 +{
26625 +       int err;
26626 +       struct au_dinfo *dinfo, *tmp;
26627 +
26628 +       /* lookup the next lower positive entry */
26629 +       err = -ENOMEM;
26630 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26631 +       if (unlikely(!tmp))
26632 +               goto out;
26633 +
26634 +       a->bfound = -1;
26635 +       a->bwh = -1;
26636 +       dinfo = au_di(a->dentry);
26637 +       au_di_cp(tmp, dinfo);
26638 +       au_di_swap(tmp, dinfo);
26639 +
26640 +       /* returns the number of positive dentries */
26641 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26642 +                            /* AuLkup_IGNORE_PERM */ 0);
26643 +       if (!err)
26644 +               a->bwh = au_dbwh(a->dentry);
26645 +       else if (err > 0)
26646 +               a->bfound = au_dbtop(a->dentry);
26647 +
26648 +       au_di_swap(tmp, dinfo);
26649 +       au_rw_write_unlock(&tmp->di_rwsem);
26650 +       au_di_free(tmp);
26651 +       if (unlikely(err < 0))
26652 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
26653 +
26654 +       /*
26655 +        * here, we have these cases.
26656 +        * bfound == -1
26657 +        *      no positive dentry under bsrc. there are more sub-cases.
26658 +        *      bwh < 0
26659 +        *              there no whiteout, we can safely move-down.
26660 +        *      bwh <= bsrc
26661 +        *              impossible
26662 +        *      bsrc < bwh && bwh < bdst
26663 +        *              there is a whiteout on RO branch. cannot proceed.
26664 +        *      bwh == bdst
26665 +        *              there is a whiteout on the RW target branch. it should
26666 +        *              be removed.
26667 +        *      bdst < bwh
26668 +        *              there is a whiteout somewhere unrelated branch.
26669 +        * -1 < bfound && bfound <= bsrc
26670 +        *      impossible.
26671 +        * bfound < bdst
26672 +        *      found, but it is on RO branch between bsrc and bdst. cannot
26673 +        *      proceed.
26674 +        * bfound == bdst
26675 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26676 +        *      error.
26677 +        * bdst < bfound
26678 +        *      found, after we create the file on bdst, it will be hidden.
26679 +        */
26680 +
26681 +       AuDebugOn(a->bfound == -1
26682 +                 && a->bwh != -1
26683 +                 && a->bwh <= a->mvd_bsrc);
26684 +       AuDebugOn(-1 < a->bfound
26685 +                 && a->bfound <= a->mvd_bsrc);
26686 +
26687 +       err = -EINVAL;
26688 +       if (a->bfound == -1
26689 +           && a->mvd_bsrc < a->bwh
26690 +           && a->bwh != -1
26691 +           && a->bwh < a->mvd_bdst) {
26692 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26693 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26694 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26695 +               goto out;
26696 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26697 +               a->mvd_errno = EAU_MVDOWN_UPPER;
26698 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26699 +                         a->mvd_bdst, a->bfound);
26700 +               goto out;
26701 +       }
26702 +
26703 +       err = 0; /* success */
26704 +
26705 +out:
26706 +       AuTraceErr(err);
26707 +       return err;
26708 +}
26709 +
26710 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26711 +{
26712 +       int err;
26713 +
26714 +       err = 0;
26715 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26716 +           && a->bfound == a->mvd_bdst)
26717 +               err = -EEXIST;
26718 +       AuTraceErr(err);
26719 +       return err;
26720 +}
26721 +
26722 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26723 +{
26724 +       int err;
26725 +       struct au_branch *br;
26726 +
26727 +       err = -EISDIR;
26728 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
26729 +               goto out;
26730 +
26731 +       err = -EINVAL;
26732 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26733 +               a->mvd_bsrc = au_ibtop(a->inode);
26734 +       else {
26735 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26736 +               if (unlikely(a->mvd_bsrc < 0
26737 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
26738 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
26739 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
26740 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
26741 +                                || au_ibbot(a->inode) < a->mvd_bsrc
26742 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26743 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
26744 +                       AU_MVD_PR(dmsg, "no upper\n");
26745 +                       goto out;
26746 +               }
26747 +       }
26748 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26749 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
26750 +               AU_MVD_PR(dmsg, "on the bottom\n");
26751 +               goto out;
26752 +       }
26753 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26754 +       br = au_sbr(a->sb, a->mvd_bsrc);
26755 +       err = au_br_rdonly(br);
26756 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26757 +               if (unlikely(err))
26758 +                       goto out;
26759 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26760 +                    || IS_APPEND(a->mvd_h_src_inode))) {
26761 +               if (err)
26762 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26763 +               /* go on */
26764 +       } else
26765 +               goto out;
26766 +
26767 +       err = -EINVAL;
26768 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26769 +               a->mvd_bdst = find_lower_writable(a);
26770 +               if (unlikely(a->mvd_bdst < 0)) {
26771 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
26772 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
26773 +                       goto out;
26774 +               }
26775 +       } else {
26776 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26777 +               if (unlikely(a->mvd_bdst < 0
26778 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
26779 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26780 +                       AU_MVD_PR(dmsg, "no lower brid\n");
26781 +                       goto out;
26782 +               }
26783 +       }
26784 +
26785 +       err = au_mvd_args_busy(dmsg, a);
26786 +       if (!err)
26787 +               err = au_mvd_args_parent(dmsg, a);
26788 +       if (!err)
26789 +               err = au_mvd_args_intermediate(dmsg, a);
26790 +       if (!err)
26791 +               err = au_mvd_args_exist(dmsg, a);
26792 +       if (!err)
26793 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26794 +
26795 +out:
26796 +       AuTraceErr(err);
26797 +       return err;
26798 +}
26799 +
26800 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26801 +{
26802 +       int err, e;
26803 +       unsigned char dmsg;
26804 +       struct au_mvd_args *args;
26805 +       struct inode *inode;
26806 +
26807 +       inode = d_inode(dentry);
26808 +       err = -EPERM;
26809 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26810 +               goto out;
26811 +
26812 +       err = -ENOMEM;
26813 +       args = kmalloc(sizeof(*args), GFP_NOFS);
26814 +       if (unlikely(!args))
26815 +               goto out;
26816 +
26817 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26818 +       if (!err)
26819 +               /* VERIFY_WRITE */
26820 +               err = !access_ok(uarg, sizeof(*uarg));
26821 +       if (unlikely(err)) {
26822 +               err = -EFAULT;
26823 +               AuTraceErr(err);
26824 +               goto out_free;
26825 +       }
26826 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
26827 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26828 +       args->mvdown.au_errno = 0;
26829 +       args->dentry = dentry;
26830 +       args->inode = inode;
26831 +       args->sb = dentry->d_sb;
26832 +
26833 +       err = -ENOENT;
26834 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26835 +       args->parent = dget_parent(dentry);
26836 +       args->dir = d_inode(args->parent);
26837 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
26838 +       dput(args->parent);
26839 +       if (unlikely(args->parent != dentry->d_parent)) {
26840 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
26841 +               goto out_dir;
26842 +       }
26843 +
26844 +       inode_lock_nested(inode, I_MUTEX_CHILD);
26845 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26846 +       if (unlikely(err))
26847 +               goto out_inode;
26848 +
26849 +       di_write_lock_parent(args->parent);
26850 +       err = au_mvd_args(dmsg, args);
26851 +       if (unlikely(err))
26852 +               goto out_parent;
26853 +
26854 +       err = au_do_mvdown(dmsg, args);
26855 +       if (unlikely(err))
26856 +               goto out_parent;
26857 +
26858 +       au_cpup_attr_timesizes(args->dir);
26859 +       au_cpup_attr_timesizes(inode);
26860 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26861 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26862 +       /* au_digen_dec(dentry); */
26863 +
26864 +out_parent:
26865 +       di_write_unlock(args->parent);
26866 +       aufs_read_unlock(dentry, AuLock_DW);
26867 +out_inode:
26868 +       inode_unlock(inode);
26869 +out_dir:
26870 +       inode_unlock(args->dir);
26871 +out_free:
26872 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26873 +       if (unlikely(e))
26874 +               err = -EFAULT;
26875 +       au_kfree_rcu(args);
26876 +out:
26877 +       AuTraceErr(err);
26878 +       return err;
26879 +}
26880 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
26881 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
26882 +++ linux/fs/aufs/opts.c        2022-11-05 23:02:18.969222617 +0100
26883 @@ -0,0 +1,1032 @@
26884 +// SPDX-License-Identifier: GPL-2.0
26885 +/*
26886 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26887 + *
26888 + * This program is free software; you can redistribute it and/or modify
26889 + * it under the terms of the GNU General Public License as published by
26890 + * the Free Software Foundation; either version 2 of the License, or
26891 + * (at your option) any later version.
26892 + *
26893 + * This program is distributed in the hope that it will be useful,
26894 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26895 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26896 + * GNU General Public License for more details.
26897 + *
26898 + * You should have received a copy of the GNU General Public License
26899 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26900 + */
26901 +
26902 +/*
26903 + * mount options/flags
26904 + */
26905 +
26906 +#include <linux/types.h> /* a distribution requires */
26907 +#include <linux/parser.h>
26908 +#include "aufs.h"
26909 +
26910 +/* ---------------------------------------------------------------------- */
26911 +
26912 +static const char *au_parser_pattern(int val, match_table_t tbl)
26913 +{
26914 +       struct match_token *p;
26915 +
26916 +       p = tbl;
26917 +       while (p->pattern) {
26918 +               if (p->token == val)
26919 +                       return p->pattern;
26920 +               p++;
26921 +       }
26922 +       BUG();
26923 +       return "??";
26924 +}
26925 +
26926 +static const char *au_optstr(int *val, match_table_t tbl)
26927 +{
26928 +       struct match_token *p;
26929 +       int v;
26930 +
26931 +       v = *val;
26932 +       if (!v)
26933 +               goto out;
26934 +       p = tbl;
26935 +       while (p->pattern) {
26936 +               if (p->token
26937 +                   && (v & p->token) == p->token) {
26938 +                       *val &= ~p->token;
26939 +                       return p->pattern;
26940 +               }
26941 +               p++;
26942 +       }
26943 +
26944 +out:
26945 +       return NULL;
26946 +}
26947 +
26948 +/* ---------------------------------------------------------------------- */
26949 +
26950 +static match_table_t brperm = {
26951 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
26952 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
26953 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
26954 +       {0, NULL}
26955 +};
26956 +
26957 +static match_table_t brattr = {
26958 +       /* general */
26959 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26960 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
26961 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
26962 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
26963 +#ifdef CONFIG_AUFS_FHSM
26964 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
26965 +#endif
26966 +#ifdef CONFIG_AUFS_XATTR
26967 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26968 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26969 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26970 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26971 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26972 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
26973 +#endif
26974 +
26975 +       /* ro/rr branch */
26976 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
26977 +
26978 +       /* rw branch */
26979 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
26980 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
26981 +
26982 +       {0, NULL}
26983 +};
26984 +
26985 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
26986 +{
26987 +       int attr, v;
26988 +       char *p;
26989 +
26990 +       attr = 0;
26991 +       do {
26992 +               p = strchr(str, '+');
26993 +               if (p)
26994 +                       *p = 0;
26995 +               v = match_token(str, table, args);
26996 +               if (v) {
26997 +                       if (v & AuBrAttr_CMOO_Mask)
26998 +                               attr &= ~AuBrAttr_CMOO_Mask;
26999 +                       attr |= v;
27000 +               } else {
27001 +                       if (p)
27002 +                               *p = '+';
27003 +                       pr_warn("ignored branch attribute %s\n", str);
27004 +                       break;
27005 +               }
27006 +               if (p)
27007 +                       str = p + 1;
27008 +       } while (p);
27009 +
27010 +       return attr;
27011 +}
27012 +
27013 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
27014 +{
27015 +       int sz;
27016 +       const char *p;
27017 +       char *q;
27018 +
27019 +       q = str->a;
27020 +       *q = 0;
27021 +       p = au_optstr(&perm, brattr);
27022 +       if (p) {
27023 +               sz = strlen(p);
27024 +               memcpy(q, p, sz + 1);
27025 +               q += sz;
27026 +       } else
27027 +               goto out;
27028 +
27029 +       do {
27030 +               p = au_optstr(&perm, brattr);
27031 +               if (p) {
27032 +                       *q++ = '+';
27033 +                       sz = strlen(p);
27034 +                       memcpy(q, p, sz + 1);
27035 +                       q += sz;
27036 +               }
27037 +       } while (p);
27038 +
27039 +out:
27040 +       return q - str->a;
27041 +}
27042 +
27043 +int au_br_perm_val(char *perm)
27044 +{
27045 +       int val, bad, sz;
27046 +       char *p;
27047 +       substring_t args[MAX_OPT_ARGS];
27048 +       au_br_perm_str_t attr;
27049 +
27050 +       p = strchr(perm, '+');
27051 +       if (p)
27052 +               *p = 0;
27053 +       val = match_token(perm, brperm, args);
27054 +       if (!val) {
27055 +               if (p)
27056 +                       *p = '+';
27057 +               pr_warn("ignored branch permission %s\n", perm);
27058 +               val = AuBrPerm_RO;
27059 +               goto out;
27060 +       }
27061 +       if (!p)
27062 +               goto out;
27063 +
27064 +       val |= br_attr_val(p + 1, brattr, args);
27065 +
27066 +       bad = 0;
27067 +       switch (val & AuBrPerm_Mask) {
27068 +       case AuBrPerm_RO:
27069 +       case AuBrPerm_RR:
27070 +               bad = val & AuBrWAttr_Mask;
27071 +               val &= ~AuBrWAttr_Mask;
27072 +               break;
27073 +       case AuBrPerm_RW:
27074 +               bad = val & AuBrRAttr_Mask;
27075 +               val &= ~AuBrRAttr_Mask;
27076 +               break;
27077 +       }
27078 +
27079 +       /*
27080 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27081 +        * does not treat it as an error, just warning.
27082 +        * this is a tiny guard for the user operation.
27083 +        */
27084 +       if (val & AuBrAttr_UNPIN) {
27085 +               bad |= AuBrAttr_UNPIN;
27086 +               val &= ~AuBrAttr_UNPIN;
27087 +       }
27088 +
27089 +       if (unlikely(bad)) {
27090 +               sz = au_do_optstr_br_attr(&attr, bad);
27091 +               AuDebugOn(!sz);
27092 +               pr_warn("ignored branch attribute %s\n", attr.a);
27093 +       }
27094 +
27095 +out:
27096 +       return val;
27097 +}
27098 +
27099 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
27100 +{
27101 +       au_br_perm_str_t attr;
27102 +       const char *p;
27103 +       char *q;
27104 +       int sz;
27105 +
27106 +       q = str->a;
27107 +       p = au_optstr(&perm, brperm);
27108 +       AuDebugOn(!p || !*p);
27109 +       sz = strlen(p);
27110 +       memcpy(q, p, sz + 1);
27111 +       q += sz;
27112 +
27113 +       sz = au_do_optstr_br_attr(&attr, perm);
27114 +       if (sz) {
27115 +               *q++ = '+';
27116 +               memcpy(q, attr.a, sz + 1);
27117 +       }
27118 +
27119 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
27120 +}
27121 +
27122 +/* ---------------------------------------------------------------------- */
27123 +
27124 +static match_table_t udbalevel = {
27125 +       {AuOpt_UDBA_REVAL, "reval"},
27126 +       {AuOpt_UDBA_NONE, "none"},
27127 +#ifdef CONFIG_AUFS_HNOTIFY
27128 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27129 +#ifdef CONFIG_AUFS_HFSNOTIFY
27130 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
27131 +#endif
27132 +#endif
27133 +       {-1, NULL}
27134 +};
27135 +
27136 +int au_udba_val(char *str)
27137 +{
27138 +       substring_t args[MAX_OPT_ARGS];
27139 +
27140 +       return match_token(str, udbalevel, args);
27141 +}
27142 +
27143 +const char *au_optstr_udba(int udba)
27144 +{
27145 +       return au_parser_pattern(udba, udbalevel);
27146 +}
27147 +
27148 +/* ---------------------------------------------------------------------- */
27149 +
27150 +static match_table_t au_wbr_create_policy = {
27151 +       {AuWbrCreate_TDP, "tdp"},
27152 +       {AuWbrCreate_TDP, "top-down-parent"},
27153 +       {AuWbrCreate_RR, "rr"},
27154 +       {AuWbrCreate_RR, "round-robin"},
27155 +       {AuWbrCreate_MFS, "mfs"},
27156 +       {AuWbrCreate_MFS, "most-free-space"},
27157 +       {AuWbrCreate_MFSV, "mfs:%d"},
27158 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
27159 +
27160 +       /* top-down regardless the parent, and then mfs */
27161 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
27162 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27163 +
27164 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
27165 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27166 +       {AuWbrCreate_PMFS, "pmfs"},
27167 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
27168 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27169 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
27170 +
27171 +       {-1, NULL}
27172 +};
27173 +
27174 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27175 +                           struct au_opt_wbr_create *create)
27176 +{
27177 +       int err;
27178 +       unsigned long long ull;
27179 +
27180 +       err = 0;
27181 +       if (!match_u64(arg, &ull))
27182 +               create->mfsrr_watermark = ull;
27183 +       else {
27184 +               pr_err("bad integer in %s\n", str);
27185 +               err = -EINVAL;
27186 +       }
27187 +
27188 +       return err;
27189 +}
27190 +
27191 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
27192 +                         struct au_opt_wbr_create *create)
27193 +{
27194 +       int n, err;
27195 +
27196 +       err = 0;
27197 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
27198 +               create->mfs_second = n;
27199 +       else {
27200 +               pr_err("bad integer in %s\n", str);
27201 +               err = -EINVAL;
27202 +       }
27203 +
27204 +       return err;
27205 +}
27206 +
27207 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
27208 +{
27209 +       int err, e;
27210 +       substring_t args[MAX_OPT_ARGS];
27211 +
27212 +       err = match_token(str, au_wbr_create_policy, args);
27213 +       create->wbr_create = err;
27214 +       switch (err) {
27215 +       case AuWbrCreate_MFSRRV:
27216 +       case AuWbrCreate_TDMFSV:
27217 +       case AuWbrCreate_PMFSRRV:
27218 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27219 +               if (!e)
27220 +                       e = au_wbr_mfs_sec(&args[1], str, create);
27221 +               if (unlikely(e))
27222 +                       err = e;
27223 +               break;
27224 +       case AuWbrCreate_MFSRR:
27225 +       case AuWbrCreate_TDMFS:
27226 +       case AuWbrCreate_PMFSRR:
27227 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27228 +               if (unlikely(e)) {
27229 +                       err = e;
27230 +                       break;
27231 +               }
27232 +               fallthrough;
27233 +       case AuWbrCreate_MFS:
27234 +       case AuWbrCreate_PMFS:
27235 +               create->mfs_second = AUFS_MFS_DEF_SEC;
27236 +               break;
27237 +       case AuWbrCreate_MFSV:
27238 +       case AuWbrCreate_PMFSV:
27239 +               e = au_wbr_mfs_sec(&args[0], str, create);
27240 +               if (unlikely(e))
27241 +                       err = e;
27242 +               break;
27243 +       }
27244 +
27245 +       return err;
27246 +}
27247 +
27248 +const char *au_optstr_wbr_create(int wbr_create)
27249 +{
27250 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
27251 +}
27252 +
27253 +static match_table_t au_wbr_copyup_policy = {
27254 +       {AuWbrCopyup_TDP, "tdp"},
27255 +       {AuWbrCopyup_TDP, "top-down-parent"},
27256 +       {AuWbrCopyup_BUP, "bup"},
27257 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
27258 +       {AuWbrCopyup_BU, "bu"},
27259 +       {AuWbrCopyup_BU, "bottom-up"},
27260 +       {-1, NULL}
27261 +};
27262 +
27263 +int au_wbr_copyup_val(char *str)
27264 +{
27265 +       substring_t args[MAX_OPT_ARGS];
27266 +
27267 +       return match_token(str, au_wbr_copyup_policy, args);
27268 +}
27269 +
27270 +const char *au_optstr_wbr_copyup(int wbr_copyup)
27271 +{
27272 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
27273 +}
27274 +
27275 +/* ---------------------------------------------------------------------- */
27276 +
27277 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27278 +              aufs_bindex_t bindex)
27279 +{
27280 +       int err;
27281 +       struct au_opt_add *add = &opt->add;
27282 +       char *p;
27283 +
27284 +       add->bindex = bindex;
27285 +       add->perm = AuBrPerm_RO;
27286 +       add->pathname = opt_str;
27287 +       p = strchr(opt_str, '=');
27288 +       if (p) {
27289 +               *p++ = 0;
27290 +               if (*p)
27291 +                       add->perm = au_br_perm_val(p);
27292 +       }
27293 +
27294 +       err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
27295 +       if (!err) {
27296 +               if (!p) {
27297 +                       add->perm = AuBrPerm_RO;
27298 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
27299 +                               add->perm = AuBrPerm_RR;
27300 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
27301 +                               add->perm = AuBrPerm_RW;
27302 +               }
27303 +               opt->type = Opt_add;
27304 +               goto out;
27305 +       }
27306 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
27307 +       err = -EINVAL;
27308 +
27309 +out:
27310 +       return err;
27311 +}
27312 +
27313 +static int au_opt_wbr_create(struct super_block *sb,
27314 +                            struct au_opt_wbr_create *create)
27315 +{
27316 +       int err;
27317 +       struct au_sbinfo *sbinfo;
27318 +
27319 +       SiMustWriteLock(sb);
27320 +
27321 +       err = 1; /* handled */
27322 +       sbinfo = au_sbi(sb);
27323 +       if (sbinfo->si_wbr_create_ops->fin) {
27324 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27325 +               if (!err)
27326 +                       err = 1;
27327 +       }
27328 +
27329 +       sbinfo->si_wbr_create = create->wbr_create;
27330 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27331 +       switch (create->wbr_create) {
27332 +       case AuWbrCreate_MFSRRV:
27333 +       case AuWbrCreate_MFSRR:
27334 +       case AuWbrCreate_TDMFS:
27335 +       case AuWbrCreate_TDMFSV:
27336 +       case AuWbrCreate_PMFSRR:
27337 +       case AuWbrCreate_PMFSRRV:
27338 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27339 +               fallthrough;
27340 +       case AuWbrCreate_MFS:
27341 +       case AuWbrCreate_MFSV:
27342 +       case AuWbrCreate_PMFS:
27343 +       case AuWbrCreate_PMFSV:
27344 +               sbinfo->si_wbr_mfs.mfs_expire
27345 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27346 +               break;
27347 +       }
27348 +
27349 +       if (sbinfo->si_wbr_create_ops->init)
27350 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27351 +
27352 +       return err;
27353 +}
27354 +
27355 +/*
27356 + * returns,
27357 + * plus: processed without an error
27358 + * zero: unprocessed
27359 + */
27360 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27361 +                        struct au_opts *opts)
27362 +{
27363 +       int err;
27364 +       struct au_sbinfo *sbinfo;
27365 +
27366 +       SiMustWriteLock(sb);
27367 +
27368 +       err = 1; /* handled */
27369 +       sbinfo = au_sbi(sb);
27370 +       switch (opt->type) {
27371 +       case Opt_udba:
27372 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27373 +               sbinfo->si_mntflags |= opt->udba;
27374 +               opts->given_udba |= opt->udba;
27375 +               break;
27376 +
27377 +       case Opt_plink:
27378 +               if (opt->tf)
27379 +                       au_opt_set(sbinfo->si_mntflags, PLINK);
27380 +               else {
27381 +                       if (au_opt_test(sbinfo->si_mntflags, PLINK))
27382 +                               au_plink_put(sb, /*verbose*/1);
27383 +                       au_opt_clr(sbinfo->si_mntflags, PLINK);
27384 +               }
27385 +               break;
27386 +       case Opt_list_plink:
27387 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27388 +                       au_plink_list(sb);
27389 +               break;
27390 +
27391 +       case Opt_dio:
27392 +               if (opt->tf) {
27393 +                       au_opt_set(sbinfo->si_mntflags, DIO);
27394 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27395 +               } else {
27396 +                       au_opt_clr(sbinfo->si_mntflags, DIO);
27397 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27398 +               }
27399 +               break;
27400 +
27401 +       case Opt_fhsm_sec:
27402 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27403 +               break;
27404 +
27405 +       case Opt_diropq_a:
27406 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27407 +               break;
27408 +       case Opt_diropq_w:
27409 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27410 +               break;
27411 +
27412 +       case Opt_warn_perm:
27413 +               if (opt->tf)
27414 +                       au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27415 +               else
27416 +                       au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27417 +               break;
27418 +
27419 +       case Opt_verbose:
27420 +               if (opt->tf)
27421 +                       au_opt_set(sbinfo->si_mntflags, VERBOSE);
27422 +               else
27423 +                       au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27424 +               break;
27425 +
27426 +       case Opt_sum:
27427 +               if (opt->tf)
27428 +                       au_opt_set(sbinfo->si_mntflags, SUM);
27429 +               else {
27430 +                       au_opt_clr(sbinfo->si_mntflags, SUM);
27431 +                       au_opt_clr(sbinfo->si_mntflags, SUM_W);
27432 +               }
27433 +               break;
27434 +       case Opt_wsum:
27435 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27436 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27437 +               break;
27438 +
27439 +       case Opt_wbr_create:
27440 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27441 +               break;
27442 +       case Opt_wbr_copyup:
27443 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27444 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27445 +               break;
27446 +
27447 +       case Opt_dirwh:
27448 +               sbinfo->si_dirwh = opt->dirwh;
27449 +               break;
27450 +
27451 +       case Opt_rdcache:
27452 +               sbinfo->si_rdcache
27453 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27454 +               break;
27455 +       case Opt_rdblk:
27456 +               sbinfo->si_rdblk = opt->rdblk;
27457 +               break;
27458 +       case Opt_rdhash:
27459 +               sbinfo->si_rdhash = opt->rdhash;
27460 +               break;
27461 +
27462 +       case Opt_shwh:
27463 +               if (opt->tf)
27464 +                       au_opt_set(sbinfo->si_mntflags, SHWH);
27465 +               else
27466 +                       au_opt_clr(sbinfo->si_mntflags, SHWH);
27467 +               break;
27468 +
27469 +       case Opt_dirperm1:
27470 +               if (opt->tf)
27471 +                       au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27472 +               else
27473 +                       au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27474 +               break;
27475 +
27476 +       case Opt_trunc_xino:
27477 +               if (opt->tf)
27478 +                       au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27479 +               else
27480 +                       au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27481 +               break;
27482 +
27483 +       case Opt_trunc_xino_path:
27484 +       case Opt_itrunc_xino:
27485 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27486 +                                   /*idx_begin*/0);
27487 +               if (!err)
27488 +                       err = 1;
27489 +               break;
27490 +
27491 +       case Opt_trunc_xib:
27492 +               if (opt->tf)
27493 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27494 +               else
27495 +                       au_fclr_opts(opts->flags, TRUNC_XIB);
27496 +               break;
27497 +
27498 +       case Opt_dirren:
27499 +               err = 1;
27500 +               if (opt->tf) {
27501 +                       if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27502 +                               err = au_dr_opt_set(sb);
27503 +                               if (!err)
27504 +                                       err = 1;
27505 +                       }
27506 +                       if (err == 1)
27507 +                               au_opt_set(sbinfo->si_mntflags, DIRREN);
27508 +               } else {
27509 +                       if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27510 +                               err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27511 +                                                                     DR_FLUSHED));
27512 +                               if (!err)
27513 +                                       err = 1;
27514 +                       }
27515 +                       if (err == 1)
27516 +                               au_opt_clr(sbinfo->si_mntflags, DIRREN);
27517 +               }
27518 +               break;
27519 +
27520 +       case Opt_acl:
27521 +               if (opt->tf)
27522 +                       sb->s_flags |= SB_POSIXACL;
27523 +               else
27524 +                       sb->s_flags &= ~SB_POSIXACL;
27525 +               break;
27526 +
27527 +       default:
27528 +               err = 0;
27529 +               break;
27530 +       }
27531 +
27532 +       return err;
27533 +}
27534 +
27535 +/*
27536 + * returns tri-state.
27537 + * plus: processed without an error
27538 + * zero: unprocessed
27539 + * minus: error
27540 + */
27541 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27542 +                    struct au_opts *opts)
27543 +{
27544 +       int err, do_refresh;
27545 +
27546 +       err = 0;
27547 +       switch (opt->type) {
27548 +       case Opt_append:
27549 +               opt->add.bindex = au_sbbot(sb) + 1;
27550 +               if (opt->add.bindex < 0)
27551 +                       opt->add.bindex = 0;
27552 +               goto add;
27553 +               /* Always goto add, not fallthrough */
27554 +       case Opt_prepend:
27555 +               opt->add.bindex = 0;
27556 +               fallthrough;
27557 +       add: /* indented label */
27558 +       case Opt_add:
27559 +               err = au_br_add(sb, &opt->add,
27560 +                               au_ftest_opts(opts->flags, REMOUNT));
27561 +               if (!err) {
27562 +                       err = 1;
27563 +                       au_fset_opts(opts->flags, REFRESH);
27564 +               }
27565 +               break;
27566 +
27567 +       case Opt_del:
27568 +       case Opt_idel:
27569 +               err = au_br_del(sb, &opt->del,
27570 +                               au_ftest_opts(opts->flags, REMOUNT));
27571 +               if (!err) {
27572 +                       err = 1;
27573 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27574 +                       au_fset_opts(opts->flags, REFRESH);
27575 +               }
27576 +               break;
27577 +
27578 +       case Opt_mod:
27579 +       case Opt_imod:
27580 +               err = au_br_mod(sb, &opt->mod,
27581 +                               au_ftest_opts(opts->flags, REMOUNT),
27582 +                               &do_refresh);
27583 +               if (!err) {
27584 +                       err = 1;
27585 +                       if (do_refresh)
27586 +                               au_fset_opts(opts->flags, REFRESH);
27587 +               }
27588 +               break;
27589 +       }
27590 +       return err;
27591 +}
27592 +
27593 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27594 +                      struct au_opt_xino **opt_xino,
27595 +                      struct au_opts *opts)
27596 +{
27597 +       int err;
27598 +
27599 +       err = 0;
27600 +       switch (opt->type) {
27601 +       case Opt_xino:
27602 +               err = au_xino_set(sb, &opt->xino,
27603 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27604 +               if (!err)
27605 +                       *opt_xino = &opt->xino;
27606 +               break;
27607 +       case Opt_noxino:
27608 +               au_xino_clr(sb);
27609 +               *opt_xino = (void *)-1;
27610 +               break;
27611 +       }
27612 +
27613 +       return err;
27614 +}
27615 +
27616 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27617 +                  unsigned int pending)
27618 +{
27619 +       int err, fhsm;
27620 +       aufs_bindex_t bindex, bbot;
27621 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27622 +       struct au_branch *br;
27623 +       struct au_wbr *wbr;
27624 +       struct dentry *root, *dentry;
27625 +       struct inode *dir, *h_dir;
27626 +       struct au_sbinfo *sbinfo;
27627 +       struct au_hinode *hdir;
27628 +
27629 +       SiMustAnyLock(sb);
27630 +
27631 +       sbinfo = au_sbi(sb);
27632 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27633 +
27634 +       if (!(sb_flags & SB_RDONLY)) {
27635 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27636 +                       pr_warn("first branch should be rw\n");
27637 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27638 +                       pr_warn_once("shwh should be used with ro\n");
27639 +       }
27640 +
27641 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27642 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27643 +               pr_warn_once("udba=*notify requires xino\n");
27644 +
27645 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27646 +               pr_warn_once("dirperm1 breaks the protection"
27647 +                            " by the permission bits on the lower branch\n");
27648 +
27649 +       err = 0;
27650 +       fhsm = 0;
27651 +       root = sb->s_root;
27652 +       dir = d_inode(root);
27653 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27654 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27655 +                                     UDBA_NONE);
27656 +       bbot = au_sbbot(sb);
27657 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27658 +               skip = 0;
27659 +               h_dir = au_h_iptr(dir, bindex);
27660 +               br = au_sbr(sb, bindex);
27661 +
27662 +               if ((br->br_perm & AuBrAttr_ICEX)
27663 +                   && !h_dir->i_op->listxattr)
27664 +                       br->br_perm &= ~AuBrAttr_ICEX;
27665 +#if 0 /* untested */
27666 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27667 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27668 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27669 +#endif
27670 +
27671 +               do_free = 0;
27672 +               wbr = br->br_wbr;
27673 +               if (wbr)
27674 +                       wbr_wh_read_lock(wbr);
27675 +
27676 +               if (!au_br_writable(br->br_perm)) {
27677 +                       do_free = !!wbr;
27678 +                       skip = (!wbr
27679 +                               || (!wbr->wbr_whbase
27680 +                                   && !wbr->wbr_plink
27681 +                                   && !wbr->wbr_orph));
27682 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27683 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27684 +                       skip = (!wbr || !wbr->wbr_whbase);
27685 +                       if (skip && wbr) {
27686 +                               if (do_plink)
27687 +                                       skip = !!wbr->wbr_plink;
27688 +                               else
27689 +                                       skip = !wbr->wbr_plink;
27690 +                       }
27691 +               } else {
27692 +                       /* skip = (br->br_whbase && br->br_ohph); */
27693 +                       skip = (wbr && wbr->wbr_whbase);
27694 +                       if (skip) {
27695 +                               if (do_plink)
27696 +                                       skip = !!wbr->wbr_plink;
27697 +                               else
27698 +                                       skip = !wbr->wbr_plink;
27699 +                       }
27700 +               }
27701 +               if (wbr)
27702 +                       wbr_wh_read_unlock(wbr);
27703 +
27704 +               if (can_no_dreval) {
27705 +                       dentry = br->br_path.dentry;
27706 +                       spin_lock(&dentry->d_lock);
27707 +                       if (dentry->d_flags &
27708 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27709 +                               can_no_dreval = 0;
27710 +                       spin_unlock(&dentry->d_lock);
27711 +               }
27712 +
27713 +               if (au_br_fhsm(br->br_perm)) {
27714 +                       fhsm++;
27715 +                       AuDebugOn(!br->br_fhsm);
27716 +               }
27717 +
27718 +               if (skip)
27719 +                       continue;
27720 +
27721 +               hdir = au_hi(dir, bindex);
27722 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27723 +               if (wbr)
27724 +                       wbr_wh_write_lock(wbr);
27725 +               err = au_wh_init(br, sb);
27726 +               if (wbr)
27727 +                       wbr_wh_write_unlock(wbr);
27728 +               au_hn_inode_unlock(hdir);
27729 +
27730 +               if (!err && do_free) {
27731 +                       au_kfree_rcu(wbr);
27732 +                       br->br_wbr = NULL;
27733 +               }
27734 +       }
27735 +
27736 +       if (can_no_dreval)
27737 +               au_fset_si(sbinfo, NO_DREVAL);
27738 +       else
27739 +               au_fclr_si(sbinfo, NO_DREVAL);
27740 +
27741 +       if (fhsm >= 2) {
27742 +               au_fset_si(sbinfo, FHSM);
27743 +               for (bindex = bbot; bindex >= 0; bindex--) {
27744 +                       br = au_sbr(sb, bindex);
27745 +                       if (au_br_fhsm(br->br_perm)) {
27746 +                               au_fhsm_set_bottom(sb, bindex);
27747 +                               break;
27748 +                       }
27749 +               }
27750 +       } else {
27751 +               au_fclr_si(sbinfo, FHSM);
27752 +               au_fhsm_set_bottom(sb, -1);
27753 +       }
27754 +
27755 +       return err;
27756 +}
27757 +
27758 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27759 +{
27760 +       int err;
27761 +       unsigned int tmp;
27762 +       aufs_bindex_t bindex, bbot;
27763 +       struct au_opt *opt;
27764 +       struct au_opt_xino *opt_xino, xino;
27765 +       struct au_sbinfo *sbinfo;
27766 +       struct au_branch *br;
27767 +       struct inode *dir;
27768 +
27769 +       SiMustWriteLock(sb);
27770 +
27771 +       err = 0;
27772 +       opt_xino = NULL;
27773 +       opt = opts->opt;
27774 +       while (err >= 0 && opt->type != Opt_tail)
27775 +               err = au_opt_simple(sb, opt++, opts);
27776 +       if (err > 0)
27777 +               err = 0;
27778 +       else if (unlikely(err < 0))
27779 +               goto out;
27780 +
27781 +       /* disable xino and udba temporary */
27782 +       sbinfo = au_sbi(sb);
27783 +       tmp = sbinfo->si_mntflags;
27784 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27785 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27786 +
27787 +       opt = opts->opt;
27788 +       while (err >= 0 && opt->type != Opt_tail)
27789 +               err = au_opt_br(sb, opt++, opts);
27790 +       if (err > 0)
27791 +               err = 0;
27792 +       else if (unlikely(err < 0))
27793 +               goto out;
27794 +
27795 +       bbot = au_sbbot(sb);
27796 +       if (unlikely(bbot < 0)) {
27797 +               err = -EINVAL;
27798 +               pr_err("no branches\n");
27799 +               goto out;
27800 +       }
27801 +
27802 +       if (au_opt_test(tmp, XINO))
27803 +               au_opt_set(sbinfo->si_mntflags, XINO);
27804 +       opt = opts->opt;
27805 +       while (!err && opt->type != Opt_tail)
27806 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27807 +       if (unlikely(err))
27808 +               goto out;
27809 +
27810 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27811 +       if (unlikely(err))
27812 +               goto out;
27813 +
27814 +       /* restore xino */
27815 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27816 +               xino.file = au_xino_def(sb);
27817 +               err = PTR_ERR(xino.file);
27818 +               if (IS_ERR(xino.file))
27819 +                       goto out;
27820 +
27821 +               err = au_xino_set(sb, &xino, /*remount*/0);
27822 +               fput(xino.file);
27823 +               if (unlikely(err))
27824 +                       goto out;
27825 +       }
27826 +
27827 +       /* restore udba */
27828 +       tmp &= AuOptMask_UDBA;
27829 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27830 +       sbinfo->si_mntflags |= tmp;
27831 +       bbot = au_sbbot(sb);
27832 +       for (bindex = 0; bindex <= bbot; bindex++) {
27833 +               br = au_sbr(sb, bindex);
27834 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27835 +               if (unlikely(err))
27836 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27837 +                               bindex, err);
27838 +               /* go on even if err */
27839 +       }
27840 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27841 +               dir = d_inode(sb->s_root);
27842 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27843 +       }
27844 +
27845 +out:
27846 +       return err;
27847 +}
27848 +
27849 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27850 +{
27851 +       int err, rerr;
27852 +       unsigned char no_dreval;
27853 +       struct inode *dir;
27854 +       struct au_opt_xino *opt_xino;
27855 +       struct au_opt *opt;
27856 +       struct au_sbinfo *sbinfo;
27857 +
27858 +       SiMustWriteLock(sb);
27859 +
27860 +       err = au_dr_opt_flush(sb);
27861 +       if (unlikely(err))
27862 +               goto out;
27863 +       au_fset_opts(opts->flags, DR_FLUSHED);
27864 +
27865 +       dir = d_inode(sb->s_root);
27866 +       sbinfo = au_sbi(sb);
27867 +       opt_xino = NULL;
27868 +       opt = opts->opt;
27869 +       while (err >= 0 && opt->type != Opt_tail) {
27870 +               err = au_opt_simple(sb, opt, opts);
27871 +               if (!err)
27872 +                       err = au_opt_br(sb, opt, opts);
27873 +               if (!err)
27874 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27875 +               opt++;
27876 +       }
27877 +       if (err > 0)
27878 +               err = 0;
27879 +       AuTraceErr(err);
27880 +       /* go on even err */
27881 +
27882 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27883 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27884 +       if (unlikely(rerr && !err))
27885 +               err = rerr;
27886 +
27887 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27888 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27889 +
27890 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27891 +               rerr = au_xib_trunc(sb);
27892 +               if (unlikely(rerr && !err))
27893 +                       err = rerr;
27894 +       }
27895 +
27896 +       /* will be handled by the caller */
27897 +       if (!au_ftest_opts(opts->flags, REFRESH)
27898 +           && (opts->given_udba
27899 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27900 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27901 +                   ))
27902 +               au_fset_opts(opts->flags, REFRESH);
27903 +
27904 +       AuDbg("status 0x%x\n", opts->flags);
27905 +
27906 +out:
27907 +       return err;
27908 +}
27909 +
27910 +/* ---------------------------------------------------------------------- */
27911 +
27912 +unsigned int au_opt_udba(struct super_block *sb)
27913 +{
27914 +       return au_mntflags(sb) & AuOptMask_UDBA;
27915 +}
27916 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27917 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27918 +++ linux/fs/aufs/opts.h        2022-11-05 23:02:18.969222617 +0100
27919 @@ -0,0 +1,263 @@
27920 +/* SPDX-License-Identifier: GPL-2.0 */
27921 +/*
27922 + * Copyright (C) 2005-2022 Junjiro R. Okajima
27923 + *
27924 + * This program is free software; you can redistribute it and/or modify
27925 + * it under the terms of the GNU General Public License as published by
27926 + * the Free Software Foundation; either version 2 of the License, or
27927 + * (at your option) any later version.
27928 + *
27929 + * This program is distributed in the hope that it will be useful,
27930 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27931 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27932 + * GNU General Public License for more details.
27933 + *
27934 + * You should have received a copy of the GNU General Public License
27935 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27936 + */
27937 +
27938 +/*
27939 + * mount options/flags
27940 + */
27941 +
27942 +#ifndef __AUFS_OPTS_H__
27943 +#define __AUFS_OPTS_H__
27944 +
27945 +#ifdef __KERNEL__
27946 +
27947 +#include <linux/fs_parser.h>
27948 +#include <linux/namei.h>
27949 +#include <linux/path.h>
27950 +
27951 +enum {
27952 +       Opt_br,
27953 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27954 +       Opt_idel, Opt_imod,
27955 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27956 +       Opt_xino, Opt_noxino,
27957 +       Opt_trunc_xino, Opt_trunc_xino_v,
27958 +       Opt_trunc_xino_path, Opt_itrunc_xino,
27959 +       Opt_trunc_xib,
27960 +       Opt_shwh,
27961 +       Opt_plink, Opt_list_plink,
27962 +       Opt_udba,
27963 +       Opt_dio,
27964 +       Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27965 +       Opt_warn_perm,
27966 +       Opt_wbr_copyup, Opt_wbr_create,
27967 +       Opt_fhsm_sec,
27968 +       Opt_verbose, Opt_noverbose,
27969 +       Opt_sum, Opt_wsum,
27970 +       Opt_dirperm1,
27971 +       Opt_dirren,
27972 +       Opt_acl,
27973 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
27974 +};
27975 +
27976 +/* ---------------------------------------------------------------------- */
27977 +
27978 +/* mount flags */
27979 +#define AuOpt_XINO             1               /* external inode number bitmap
27980 +                                                  and translation table */
27981 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27982 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27983 +#define AuOpt_UDBA_REVAL       (1 << 3)
27984 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27985 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27986 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27987 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27988 +                                                  bits */
27989 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27990 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27991 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27992 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27993 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27994 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27995 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27996 +
27997 +#ifndef CONFIG_AUFS_HNOTIFY
27998 +#undef AuOpt_UDBA_HNOTIFY
27999 +#define AuOpt_UDBA_HNOTIFY     0
28000 +#endif
28001 +#ifndef CONFIG_AUFS_DIRREN
28002 +#undef AuOpt_DIRREN
28003 +#define AuOpt_DIRREN           0
28004 +#endif
28005 +#ifndef CONFIG_AUFS_SHWH
28006 +#undef AuOpt_SHWH
28007 +#define AuOpt_SHWH             0
28008 +#endif
28009 +
28010 +#define AuOpt_Def      (AuOpt_XINO \
28011 +                        | AuOpt_UDBA_REVAL \
28012 +                        | AuOpt_PLINK \
28013 +                        /* | AuOpt_DIRPERM1 */ \
28014 +                        | AuOpt_WARN_PERM)
28015 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
28016 +                        | AuOpt_UDBA_REVAL \
28017 +                        | AuOpt_UDBA_HNOTIFY)
28018 +
28019 +#define AuOpt_LkupDirFlags     (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
28020 +
28021 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
28022 +#define au_opt_set(flags, name) do { \
28023 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
28024 +       ((flags) |= AuOpt_##name); \
28025 +} while (0)
28026 +#define au_opt_set_udba(flags, name) do { \
28027 +       (flags) &= ~AuOptMask_UDBA; \
28028 +       ((flags) |= AuOpt_##name); \
28029 +} while (0)
28030 +#define au_opt_clr(flags, name) do { \
28031 +       ((flags) &= ~AuOpt_##name); \
28032 +} while (0)
28033 +
28034 +static inline unsigned int au_opts_plink(unsigned int mntflags)
28035 +{
28036 +#ifdef CONFIG_PROC_FS
28037 +       return mntflags;
28038 +#else
28039 +       return mntflags & ~AuOpt_PLINK;
28040 +#endif
28041 +}
28042 +
28043 +/* ---------------------------------------------------------------------- */
28044 +
28045 +/* policies to select one among multiple writable branches */
28046 +enum {
28047 +       AuWbrCreate_TDP,        /* top down parent */
28048 +       AuWbrCreate_RR,         /* round robin */
28049 +       AuWbrCreate_MFS,        /* most free space */
28050 +       AuWbrCreate_MFSV,       /* mfs with seconds */
28051 +       AuWbrCreate_MFSRR,      /* mfs then rr */
28052 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
28053 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
28054 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
28055 +       AuWbrCreate_PMFS,       /* parent and mfs */
28056 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
28057 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
28058 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
28059 +
28060 +       AuWbrCreate_Def = AuWbrCreate_TDP
28061 +};
28062 +
28063 +enum {
28064 +       AuWbrCopyup_TDP,        /* top down parent */
28065 +       AuWbrCopyup_BUP,        /* bottom up parent */
28066 +       AuWbrCopyup_BU,         /* bottom up */
28067 +
28068 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
28069 +};
28070 +
28071 +/* ---------------------------------------------------------------------- */
28072 +
28073 +struct file;
28074 +
28075 +struct au_opt_add {
28076 +       aufs_bindex_t   bindex;
28077 +       char            *pathname;
28078 +       int             perm;
28079 +       struct path     path;
28080 +};
28081 +
28082 +struct au_opt_del {
28083 +       char            *pathname;
28084 +       struct path     h_path;
28085 +};
28086 +
28087 +struct au_opt_mod {
28088 +       char            *path;
28089 +       int             perm;
28090 +       struct dentry   *h_root;
28091 +};
28092 +
28093 +struct au_opt_xino {
28094 +       char            *path;
28095 +       struct file     *file;
28096 +};
28097 +
28098 +struct au_opt_xino_itrunc {
28099 +       aufs_bindex_t   bindex;
28100 +};
28101 +
28102 +struct au_opt_wbr_create {
28103 +       int                     wbr_create;
28104 +       int                     mfs_second;
28105 +       unsigned long long      mfsrr_watermark;
28106 +};
28107 +
28108 +struct au_opt {
28109 +       int type;
28110 +       union {
28111 +               struct au_opt_xino      xino;
28112 +               struct au_opt_xino_itrunc xino_itrunc;
28113 +               struct au_opt_add       add;
28114 +               struct au_opt_del       del;
28115 +               struct au_opt_mod       mod;
28116 +               int                     dirwh;
28117 +               int                     rdcache;
28118 +               unsigned int            rdblk;
28119 +               unsigned int            rdhash;
28120 +               int                     udba;
28121 +               struct au_opt_wbr_create wbr_create;
28122 +               int                     wbr_copyup;
28123 +               unsigned int            fhsm_second;
28124 +               bool                    tf; /* generic flag, true or false */
28125 +       };
28126 +};
28127 +
28128 +/* opts flags */
28129 +#define AuOpts_REMOUNT         1
28130 +#define AuOpts_REFRESH         (1 << 1)
28131 +#define AuOpts_TRUNC_XIB       (1 << 2)
28132 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
28133 +#define AuOpts_REFRESH_IDOP    (1 << 4)
28134 +#define AuOpts_DR_FLUSHED      (1 << 5)
28135 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
28136 +#define au_fset_opts(flags, name) \
28137 +       do { (flags) |= AuOpts_##name; } while (0)
28138 +#define au_fclr_opts(flags, name) \
28139 +       do { (flags) &= ~AuOpts_##name; } while (0)
28140 +
28141 +#ifndef CONFIG_AUFS_DIRREN
28142 +#undef AuOpts_DR_FLUSHED
28143 +#define AuOpts_DR_FLUSHED      0
28144 +#endif
28145 +
28146 +struct au_opts {
28147 +       struct au_opt   *opt;
28148 +       int             max_opt;
28149 +
28150 +       unsigned int    given_udba;
28151 +       unsigned int    flags;
28152 +       unsigned long   sb_flags;
28153 +};
28154 +
28155 +/* ---------------------------------------------------------------------- */
28156 +
28157 +/* opts.c */
28158 +int au_br_perm_val(char *perm);
28159 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
28160 +int au_udba_val(char *str);
28161 +const char *au_optstr_udba(int udba);
28162 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
28163 +const char *au_optstr_wbr_create(int wbr_create);
28164 +int au_wbr_copyup_val(char *str);
28165 +const char *au_optstr_wbr_copyup(int wbr_copyup);
28166 +
28167 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28168 +              aufs_bindex_t bindex);
28169 +struct super_block;
28170 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28171 +                  unsigned int pending);
28172 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28173 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28174 +
28175 +unsigned int au_opt_udba(struct super_block *sb);
28176 +
28177 +/* fsctx.c */
28178 +int aufs_fsctx_init(struct fs_context *fc);
28179 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28180 +
28181 +#endif /* __KERNEL__ */
28182 +#endif /* __AUFS_OPTS_H__ */
28183 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
28184 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
28185 +++ linux/fs/aufs/plink.c       2022-11-05 23:02:18.969222617 +0100
28186 @@ -0,0 +1,516 @@
28187 +// SPDX-License-Identifier: GPL-2.0
28188 +/*
28189 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28190 + *
28191 + * This program is free software; you can redistribute it and/or modify
28192 + * it under the terms of the GNU General Public License as published by
28193 + * the Free Software Foundation; either version 2 of the License, or
28194 + * (at your option) any later version.
28195 + *
28196 + * This program is distributed in the hope that it will be useful,
28197 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28198 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28199 + * GNU General Public License for more details.
28200 + *
28201 + * You should have received a copy of the GNU General Public License
28202 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28203 + */
28204 +
28205 +/*
28206 + * pseudo-link
28207 + */
28208 +
28209 +#include "aufs.h"
28210 +
28211 +/*
28212 + * the pseudo-link maintenance mode.
28213 + * during a user process maintains the pseudo-links,
28214 + * prohibit adding a new plink and branch manipulation.
28215 + *
28216 + * Flags
28217 + * NOPLM:
28218 + *     For entry functions which will handle plink, and i_mutex is already held
28219 + *     in VFS.
28220 + *     They cannot wait and should return an error at once.
28221 + *     Callers has to check the error.
28222 + * NOPLMW:
28223 + *     For entry functions which will handle plink, but i_mutex is not held
28224 + *     in VFS.
28225 + *     They can wait the plink maintenance mode to finish.
28226 + *
28227 + * They behave like F_SETLK and F_SETLKW.
28228 + * If the caller never handle plink, then both flags are unnecessary.
28229 + */
28230 +
28231 +int au_plink_maint(struct super_block *sb, int flags)
28232 +{
28233 +       int err;
28234 +       pid_t pid, ppid;
28235 +       struct task_struct *parent, *prev;
28236 +       struct au_sbinfo *sbi;
28237 +
28238 +       SiMustAnyLock(sb);
28239 +
28240 +       err = 0;
28241 +       if (!au_opt_test(au_mntflags(sb), PLINK))
28242 +               goto out;
28243 +
28244 +       sbi = au_sbi(sb);
28245 +       pid = sbi->si_plink_maint_pid;
28246 +       if (!pid || pid == current->pid)
28247 +               goto out;
28248 +
28249 +       /* todo: it highly depends upon /sbin/mount.aufs */
28250 +       prev = NULL;
28251 +       parent = current;
28252 +       ppid = 0;
28253 +       rcu_read_lock();
28254 +       while (1) {
28255 +               parent = rcu_dereference(parent->real_parent);
28256 +               if (parent == prev)
28257 +                       break;
28258 +               ppid = task_pid_vnr(parent);
28259 +               if (pid == ppid) {
28260 +                       rcu_read_unlock();
28261 +                       goto out;
28262 +               }
28263 +               prev = parent;
28264 +       }
28265 +       rcu_read_unlock();
28266 +
28267 +       if (au_ftest_lock(flags, NOPLMW)) {
28268 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
28269 +               /* AuDebugOn(!lockdep_depth(current)); */
28270 +               while (sbi->si_plink_maint_pid) {
28271 +                       si_read_unlock(sb);
28272 +                       /* gave up wake_up_bit() */
28273 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28274 +
28275 +                       if (au_ftest_lock(flags, FLUSH))
28276 +                               au_nwt_flush(&sbi->si_nowait);
28277 +                       si_noflush_read_lock(sb);
28278 +               }
28279 +       } else if (au_ftest_lock(flags, NOPLM)) {
28280 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
28281 +               err = -EAGAIN;
28282 +       }
28283 +
28284 +out:
28285 +       return err;
28286 +}
28287 +
28288 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28289 +{
28290 +       spin_lock(&sbinfo->si_plink_maint_lock);
28291 +       sbinfo->si_plink_maint_pid = 0;
28292 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28293 +       wake_up_all(&sbinfo->si_plink_wq);
28294 +}
28295 +
28296 +int au_plink_maint_enter(struct super_block *sb)
28297 +{
28298 +       int err;
28299 +       struct au_sbinfo *sbinfo;
28300 +
28301 +       err = 0;
28302 +       sbinfo = au_sbi(sb);
28303 +       /* make sure i am the only one in this fs */
28304 +       si_write_lock(sb, AuLock_FLUSH);
28305 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28306 +               spin_lock(&sbinfo->si_plink_maint_lock);
28307 +               if (!sbinfo->si_plink_maint_pid)
28308 +                       sbinfo->si_plink_maint_pid = current->pid;
28309 +               else
28310 +                       err = -EBUSY;
28311 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28312 +       }
28313 +       si_write_unlock(sb);
28314 +
28315 +       return err;
28316 +}
28317 +
28318 +/* ---------------------------------------------------------------------- */
28319 +
28320 +#ifdef CONFIG_AUFS_DEBUG
28321 +void au_plink_list(struct super_block *sb)
28322 +{
28323 +       int i;
28324 +       struct au_sbinfo *sbinfo;
28325 +       struct hlist_bl_head *hbl;
28326 +       struct hlist_bl_node *pos;
28327 +       struct au_icntnr *icntnr;
28328 +
28329 +       SiMustAnyLock(sb);
28330 +
28331 +       sbinfo = au_sbi(sb);
28332 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28333 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28334 +
28335 +       for (i = 0; i < AuPlink_NHASH; i++) {
28336 +               hbl = sbinfo->si_plink + i;
28337 +               hlist_bl_lock(hbl);
28338 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28339 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28340 +               hlist_bl_unlock(hbl);
28341 +       }
28342 +}
28343 +#endif
28344 +
28345 +/* is the inode pseudo-linked? */
28346 +int au_plink_test(struct inode *inode)
28347 +{
28348 +       int found, i;
28349 +       struct au_sbinfo *sbinfo;
28350 +       struct hlist_bl_head *hbl;
28351 +       struct hlist_bl_node *pos;
28352 +       struct au_icntnr *icntnr;
28353 +
28354 +       sbinfo = au_sbi(inode->i_sb);
28355 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28356 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28357 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28358 +
28359 +       found = 0;
28360 +       i = au_plink_hash(inode->i_ino);
28361 +       hbl =  sbinfo->si_plink + i;
28362 +       hlist_bl_lock(hbl);
28363 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28364 +               if (&icntnr->vfs_inode == inode) {
28365 +                       found = 1;
28366 +                       break;
28367 +               }
28368 +       hlist_bl_unlock(hbl);
28369 +       return found;
28370 +}
28371 +
28372 +/* ---------------------------------------------------------------------- */
28373 +
28374 +/*
28375 + * generate a name for plink.
28376 + * the file will be stored under AUFS_WH_PLINKDIR.
28377 + */
28378 +/* 20 is max digits length of ulong 64 */
28379 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28380 +
28381 +static int plink_name(char *name, int len, struct inode *inode,
28382 +                     aufs_bindex_t bindex)
28383 +{
28384 +       int rlen;
28385 +       struct inode *h_inode;
28386 +
28387 +       h_inode = au_h_iptr(inode, bindex);
28388 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28389 +       return rlen;
28390 +}
28391 +
28392 +struct au_do_plink_lkup_args {
28393 +       struct dentry **errp;
28394 +       struct qstr *tgtname;
28395 +       struct path *h_ppath;
28396 +};
28397 +
28398 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28399 +                                      struct path *h_ppath)
28400 +{
28401 +       struct dentry *h_dentry;
28402 +       struct inode *h_inode;
28403 +
28404 +       h_inode = d_inode(h_ppath->dentry);
28405 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28406 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
28407 +       inode_unlock_shared(h_inode);
28408 +
28409 +       return h_dentry;
28410 +}
28411 +
28412 +static void au_call_do_plink_lkup(void *args)
28413 +{
28414 +       struct au_do_plink_lkup_args *a = args;
28415 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
28416 +}
28417 +
28418 +/* lookup the plink-ed @inode under the branch at @bindex */
28419 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28420 +{
28421 +       struct dentry *h_dentry;
28422 +       struct au_branch *br;
28423 +       struct path h_ppath;
28424 +       int wkq_err;
28425 +       char a[PLINK_NAME_LEN];
28426 +       struct qstr tgtname = QSTR_INIT(a, 0);
28427 +
28428 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28429 +
28430 +       br = au_sbr(inode->i_sb, bindex);
28431 +       h_ppath.dentry = br->br_wbr->wbr_plink;
28432 +       h_ppath.mnt = au_br_mnt(br);
28433 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28434 +
28435 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28436 +               struct au_do_plink_lkup_args args = {
28437 +                       .errp           = &h_dentry,
28438 +                       .tgtname        = &tgtname,
28439 +                       .h_ppath        = &h_ppath
28440 +               };
28441 +
28442 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28443 +               if (unlikely(wkq_err))
28444 +                       h_dentry = ERR_PTR(wkq_err);
28445 +       } else
28446 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28447 +
28448 +       return h_dentry;
28449 +}
28450 +
28451 +/* create a pseudo-link */
28452 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28453 +                     struct dentry *h_dentry)
28454 +{
28455 +       int err;
28456 +       struct path h_path;
28457 +       struct inode *h_dir, *delegated;
28458 +
28459 +       h_dir = d_inode(h_ppath->dentry);
28460 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28461 +       h_path.mnt = h_ppath->mnt;
28462 +again:
28463 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28464 +       err = PTR_ERR(h_path.dentry);
28465 +       if (IS_ERR(h_path.dentry))
28466 +               goto out;
28467 +
28468 +       err = 0;
28469 +       /* wh.plink dir is not monitored */
28470 +       /* todo: is it really safe? */
28471 +       if (d_is_positive(h_path.dentry)
28472 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28473 +               delegated = NULL;
28474 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28475 +               if (unlikely(err == -EWOULDBLOCK)) {
28476 +                       pr_warn("cannot retry for NFSv4 delegation"
28477 +                               " for an internal unlink\n");
28478 +                       iput(delegated);
28479 +               }
28480 +               dput(h_path.dentry);
28481 +               h_path.dentry = NULL;
28482 +               if (!err)
28483 +                       goto again;
28484 +       }
28485 +       if (!err && d_is_negative(h_path.dentry)) {
28486 +               delegated = NULL;
28487 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28488 +               if (unlikely(err == -EWOULDBLOCK)) {
28489 +                       pr_warn("cannot retry for NFSv4 delegation"
28490 +                               " for an internal link\n");
28491 +                       iput(delegated);
28492 +               }
28493 +       }
28494 +       dput(h_path.dentry);
28495 +
28496 +out:
28497 +       inode_unlock(h_dir);
28498 +       return err;
28499 +}
28500 +
28501 +struct do_whplink_args {
28502 +       int *errp;
28503 +       struct qstr *tgt;
28504 +       struct path *h_ppath;
28505 +       struct dentry *h_dentry;
28506 +};
28507 +
28508 +static void call_do_whplink(void *args)
28509 +{
28510 +       struct do_whplink_args *a = args;
28511 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28512 +}
28513 +
28514 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28515 +                  aufs_bindex_t bindex)
28516 +{
28517 +       int err, wkq_err;
28518 +       struct au_branch *br;
28519 +       struct au_wbr *wbr;
28520 +       struct path h_ppath;
28521 +       char a[PLINK_NAME_LEN];
28522 +       struct qstr tgtname = QSTR_INIT(a, 0);
28523 +
28524 +       br = au_sbr(inode->i_sb, bindex);
28525 +       wbr = br->br_wbr;
28526 +       h_ppath.dentry = wbr->wbr_plink;
28527 +       h_ppath.mnt = au_br_mnt(br);
28528 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28529 +
28530 +       /* always superio. */
28531 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28532 +               struct do_whplink_args args = {
28533 +                       .errp           = &err,
28534 +                       .tgt            = &tgtname,
28535 +                       .h_ppath        = &h_ppath,
28536 +                       .h_dentry       = h_dentry
28537 +               };
28538 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28539 +               if (unlikely(wkq_err))
28540 +                       err = wkq_err;
28541 +       } else
28542 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28543 +
28544 +       return err;
28545 +}
28546 +
28547 +/*
28548 + * create a new pseudo-link for @h_dentry on @bindex.
28549 + * the linked inode is held in aufs @inode.
28550 + */
28551 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28552 +                    struct dentry *h_dentry)
28553 +{
28554 +       struct super_block *sb;
28555 +       struct au_sbinfo *sbinfo;
28556 +       struct hlist_bl_head *hbl;
28557 +       struct hlist_bl_node *pos;
28558 +       struct au_icntnr *icntnr;
28559 +       int found, err, cnt, i;
28560 +
28561 +       sb = inode->i_sb;
28562 +       sbinfo = au_sbi(sb);
28563 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28564 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28565 +
28566 +       found = au_plink_test(inode);
28567 +       if (found)
28568 +               return;
28569 +
28570 +       i = au_plink_hash(inode->i_ino);
28571 +       hbl = sbinfo->si_plink + i;
28572 +       au_igrab(inode);
28573 +
28574 +       hlist_bl_lock(hbl);
28575 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28576 +               if (&icntnr->vfs_inode == inode) {
28577 +                       found = 1;
28578 +                       break;
28579 +               }
28580 +       }
28581 +       if (!found) {
28582 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28583 +               hlist_bl_add_head(&icntnr->plink, hbl);
28584 +       }
28585 +       hlist_bl_unlock(hbl);
28586 +       if (!found) {
28587 +               cnt = au_hbl_count(hbl);
28588 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28589 +               if (cnt > AUFS_PLINK_WARN)
28590 +                       AuWarn1(msg ", %d\n", cnt);
28591 +#undef msg
28592 +               err = whplink(h_dentry, inode, bindex);
28593 +               if (unlikely(err)) {
28594 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28595 +                       au_hbl_del(&icntnr->plink, hbl);
28596 +                       iput(&icntnr->vfs_inode);
28597 +               }
28598 +       } else
28599 +               iput(&icntnr->vfs_inode);
28600 +}
28601 +
28602 +/* free all plinks */
28603 +void au_plink_put(struct super_block *sb, int verbose)
28604 +{
28605 +       int i, warned;
28606 +       struct au_sbinfo *sbinfo;
28607 +       struct hlist_bl_head *hbl;
28608 +       struct hlist_bl_node *pos, *tmp;
28609 +       struct au_icntnr *icntnr;
28610 +
28611 +       SiMustWriteLock(sb);
28612 +
28613 +       sbinfo = au_sbi(sb);
28614 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28615 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28616 +
28617 +       /* no spin_lock since sbinfo is write-locked */
28618 +       warned = 0;
28619 +       for (i = 0; i < AuPlink_NHASH; i++) {
28620 +               hbl = sbinfo->si_plink + i;
28621 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28622 +                       pr_warn("pseudo-link is not flushed");
28623 +                       warned = 1;
28624 +               }
28625 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28626 +                       iput(&icntnr->vfs_inode);
28627 +               INIT_HLIST_BL_HEAD(hbl);
28628 +       }
28629 +}
28630 +
28631 +void au_plink_clean(struct super_block *sb, int verbose)
28632 +{
28633 +       struct dentry *root;
28634 +
28635 +       root = sb->s_root;
28636 +       aufs_write_lock(root);
28637 +       if (au_opt_test(au_mntflags(sb), PLINK))
28638 +               au_plink_put(sb, verbose);
28639 +       aufs_write_unlock(root);
28640 +}
28641 +
28642 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28643 +{
28644 +       int do_put;
28645 +       aufs_bindex_t btop, bbot, bindex;
28646 +
28647 +       do_put = 0;
28648 +       btop = au_ibtop(inode);
28649 +       bbot = au_ibbot(inode);
28650 +       if (btop >= 0) {
28651 +               for (bindex = btop; bindex <= bbot; bindex++) {
28652 +                       if (!au_h_iptr(inode, bindex)
28653 +                           || au_ii_br_id(inode, bindex) != br_id)
28654 +                               continue;
28655 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28656 +                       do_put = 1;
28657 +                       break;
28658 +               }
28659 +               if (do_put)
28660 +                       for (bindex = btop; bindex <= bbot; bindex++)
28661 +                               if (au_h_iptr(inode, bindex)) {
28662 +                                       do_put = 0;
28663 +                                       break;
28664 +                               }
28665 +       } else
28666 +               do_put = 1;
28667 +
28668 +       return do_put;
28669 +}
28670 +
28671 +/* free the plinks on a branch specified by @br_id */
28672 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28673 +{
28674 +       struct au_sbinfo *sbinfo;
28675 +       struct hlist_bl_head *hbl;
28676 +       struct hlist_bl_node *pos, *tmp;
28677 +       struct au_icntnr *icntnr;
28678 +       struct inode *inode;
28679 +       int i, do_put;
28680 +
28681 +       SiMustWriteLock(sb);
28682 +
28683 +       sbinfo = au_sbi(sb);
28684 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28685 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28686 +
28687 +       /* no bit_lock since sbinfo is write-locked */
28688 +       for (i = 0; i < AuPlink_NHASH; i++) {
28689 +               hbl = sbinfo->si_plink + i;
28690 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28691 +                       inode = au_igrab(&icntnr->vfs_inode);
28692 +                       ii_write_lock_child(inode);
28693 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28694 +                       if (do_put) {
28695 +                               hlist_bl_del(&icntnr->plink);
28696 +                               iput(inode);
28697 +                       }
28698 +                       ii_write_unlock(inode);
28699 +                       iput(inode);
28700 +               }
28701 +       }
28702 +}
28703 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28704 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28705 +++ linux/fs/aufs/poll.c        2022-11-05 23:02:18.969222617 +0100
28706 @@ -0,0 +1,51 @@
28707 +// SPDX-License-Identifier: GPL-2.0
28708 +/*
28709 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28710 + *
28711 + * This program is free software; you can redistribute it and/or modify
28712 + * it under the terms of the GNU General Public License as published by
28713 + * the Free Software Foundation; either version 2 of the License, or
28714 + * (at your option) any later version.
28715 + *
28716 + * This program is distributed in the hope that it will be useful,
28717 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28718 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28719 + * GNU General Public License for more details.
28720 + *
28721 + * You should have received a copy of the GNU General Public License
28722 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28723 + */
28724 +
28725 +/*
28726 + * poll operation
28727 + * There is only one filesystem which implements ->poll operation, currently.
28728 + */
28729 +
28730 +#include "aufs.h"
28731 +
28732 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28733 +{
28734 +       __poll_t mask;
28735 +       struct file *h_file;
28736 +       struct super_block *sb;
28737 +
28738 +       /* We should pretend an error happened. */
28739 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28740 +       sb = file->f_path.dentry->d_sb;
28741 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28742 +
28743 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28744 +       if (IS_ERR(h_file)) {
28745 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28746 +               goto out;
28747 +       }
28748 +
28749 +       mask = vfs_poll(h_file, pt);
28750 +       fput(h_file); /* instead of au_read_post() */
28751 +
28752 +out:
28753 +       si_read_unlock(sb);
28754 +       if (mask & EPOLLERR)
28755 +               AuDbg("mask 0x%x\n", mask);
28756 +       return mask;
28757 +}
28758 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28759 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28760 +++ linux/fs/aufs/posix_acl.c   2023-09-03 02:21:58.863304341 +0200
28761 @@ -0,0 +1,108 @@
28762 +// SPDX-License-Identifier: GPL-2.0
28763 +/*
28764 + * Copyright (C) 2014-2022 Junjiro R. Okajima
28765 + *
28766 + * This program is free software; you can redistribute it and/or modify
28767 + * it under the terms of the GNU General Public License as published by
28768 + * the Free Software Foundation; either version 2 of the License, or
28769 + * (at your option) any later version.
28770 + *
28771 + * This program is distributed in the hope that it will be useful,
28772 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28773 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28774 + * GNU General Public License for more details.
28775 + *
28776 + * You should have received a copy of the GNU General Public License
28777 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28778 + */
28779 +
28780 +/*
28781 + * posix acl operations
28782 + */
28783 +
28784 +#include <linux/fs.h>
28785 +#include "aufs.h"
28786 +
28787 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu)
28788 +{
28789 +       struct posix_acl *acl;
28790 +       int err;
28791 +       aufs_bindex_t bindex;
28792 +       struct inode *h_inode;
28793 +       struct super_block *sb;
28794 +
28795 +       acl = ERR_PTR(-ECHILD);
28796 +       if (rcu)
28797 +               goto out;
28798 +
28799 +       acl = NULL;
28800 +       sb = inode->i_sb;
28801 +       si_read_lock(sb, AuLock_FLUSH);
28802 +       ii_read_lock_child(inode);
28803 +       if (!(sb->s_flags & SB_POSIXACL))
28804 +               goto unlock;
28805 +
28806 +       bindex = au_ibtop(inode);
28807 +       h_inode = au_h_iptr(inode, bindex);
28808 +       if (unlikely(!h_inode
28809 +                    || ((h_inode->i_mode & S_IFMT)
28810 +                        != (inode->i_mode & S_IFMT)))) {
28811 +               err = au_busy_or_stale();
28812 +               acl = ERR_PTR(err);
28813 +               goto unlock;
28814 +       }
28815 +
28816 +       /* always topmost only */
28817 +       acl = get_inode_acl(h_inode, type);
28818 +       if (IS_ERR(acl))
28819 +               forget_cached_acl(inode, type);
28820 +       else
28821 +               set_cached_acl(inode, type, acl);
28822 +
28823 +unlock:
28824 +       ii_read_unlock(inode);
28825 +       si_read_unlock(sb);
28826 +
28827 +out:
28828 +       AuTraceErrPtr(acl);
28829 +       return acl;
28830 +}
28831 +
28832 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
28833 +                              struct dentry *dentry, int type)
28834 +{
28835 +       struct posix_acl *acl;
28836 +       struct inode *inode;
28837 +
28838 +       inode = d_inode(dentry);
28839 +       acl = aufs_get_inode_acl(inode, type, /*rcu*/false);
28840 +
28841 +       return acl;
28842 +}
28843 +
28844 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
28845 +                struct posix_acl *acl, int type)
28846 +{
28847 +       int err;
28848 +       ssize_t ssz;
28849 +       struct inode *inode;
28850 +       struct au_sxattr arg = {
28851 +               .type = AU_ACL_SET,
28852 +               .u.acl_set = {
28853 +                       .acl    = acl,
28854 +                       .type   = type
28855 +               },
28856 +       };
28857 +
28858 +       inode = d_inode(dentry);
28859 +       IMustLock(inode);
28860 +
28861 +       ssz = au_sxattr(dentry, inode, &arg);
28862 +       /* forget even it if succeeds since the branch might set differently */
28863 +       forget_cached_acl(inode, type);
28864 +       err = ssz;
28865 +       if (ssz >= 0)
28866 +               err = 0;
28867 +
28868 +       return err;
28869 +}
28870 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28871 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28872 +++ linux/fs/aufs/procfs.c      2022-11-05 23:02:18.969222617 +0100
28873 @@ -0,0 +1,170 @@
28874 +// SPDX-License-Identifier: GPL-2.0
28875 +/*
28876 + * Copyright (C) 2010-2022 Junjiro R. Okajima
28877 + *
28878 + * This program is free software; you can redistribute it and/or modify
28879 + * it under the terms of the GNU General Public License as published by
28880 + * the Free Software Foundation; either version 2 of the License, or
28881 + * (at your option) any later version.
28882 + *
28883 + * This program is distributed in the hope that it will be useful,
28884 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28885 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28886 + * GNU General Public License for more details.
28887 + *
28888 + * You should have received a copy of the GNU General Public License
28889 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28890 + */
28891 +
28892 +/*
28893 + * procfs interfaces
28894 + */
28895 +
28896 +#include <linux/proc_fs.h>
28897 +#include "aufs.h"
28898 +
28899 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28900 +{
28901 +       struct au_sbinfo *sbinfo;
28902 +
28903 +       sbinfo = file->private_data;
28904 +       if (sbinfo) {
28905 +               au_plink_maint_leave(sbinfo);
28906 +               kobject_put(&sbinfo->si_kobj);
28907 +       }
28908 +
28909 +       return 0;
28910 +}
28911 +
28912 +static void au_procfs_plm_write_clean(struct file *file)
28913 +{
28914 +       struct au_sbinfo *sbinfo;
28915 +
28916 +       sbinfo = file->private_data;
28917 +       if (sbinfo)
28918 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28919 +}
28920 +
28921 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28922 +{
28923 +       int err;
28924 +       struct super_block *sb;
28925 +       struct au_sbinfo *sbinfo;
28926 +       struct hlist_bl_node *pos;
28927 +
28928 +       err = -EBUSY;
28929 +       if (unlikely(file->private_data))
28930 +               goto out;
28931 +
28932 +       sb = NULL;
28933 +       /* don't use au_sbilist_lock() here */
28934 +       hlist_bl_lock(&au_sbilist);
28935 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28936 +               if (id == sysaufs_si_id(sbinfo)) {
28937 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28938 +                               sb = sbinfo->si_sb;
28939 +                       break;
28940 +               }
28941 +       hlist_bl_unlock(&au_sbilist);
28942 +
28943 +       err = -EINVAL;
28944 +       if (unlikely(!sb))
28945 +               goto out;
28946 +
28947 +       err = au_plink_maint_enter(sb);
28948 +       if (!err)
28949 +               /* keep kobject_get() */
28950 +               file->private_data = sbinfo;
28951 +       else
28952 +               kobject_put(&sbinfo->si_kobj);
28953 +out:
28954 +       return err;
28955 +}
28956 +
28957 +/*
28958 + * Accept a valid "si=xxxx" only.
28959 + * Once it is accepted successfully, accept "clean" too.
28960 + */
28961 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28962 +                                  size_t count, loff_t *ppos)
28963 +{
28964 +       ssize_t err;
28965 +       unsigned long id;
28966 +       /* last newline is allowed */
28967 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28968 +
28969 +       err = -EACCES;
28970 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28971 +               goto out;
28972 +
28973 +       err = -EINVAL;
28974 +       if (unlikely(count > sizeof(buf)))
28975 +               goto out;
28976 +
28977 +       err = copy_from_user(buf, ubuf, count);
28978 +       if (unlikely(err)) {
28979 +               err = -EFAULT;
28980 +               goto out;
28981 +       }
28982 +       buf[count] = 0;
28983 +
28984 +       err = -EINVAL;
28985 +       if (!strcmp("clean", buf)) {
28986 +               au_procfs_plm_write_clean(file);
28987 +               goto out_success;
28988 +       } else if (unlikely(strncmp("si=", buf, 3)))
28989 +               goto out;
28990 +
28991 +       err = kstrtoul(buf + 3, 16, &id);
28992 +       if (unlikely(err))
28993 +               goto out;
28994 +
28995 +       err = au_procfs_plm_write_si(file, id);
28996 +       if (unlikely(err))
28997 +               goto out;
28998 +
28999 +out_success:
29000 +       err = count; /* success */
29001 +out:
29002 +       return err;
29003 +}
29004 +
29005 +static const struct proc_ops au_procfs_plm_op = {
29006 +       .proc_write     = au_procfs_plm_write,
29007 +       .proc_release   = au_procfs_plm_release
29008 +};
29009 +
29010 +/* ---------------------------------------------------------------------- */
29011 +
29012 +static struct proc_dir_entry *au_procfs_dir;
29013 +
29014 +void au_procfs_fin(void)
29015 +{
29016 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
29017 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29018 +}
29019 +
29020 +int __init au_procfs_init(void)
29021 +{
29022 +       int err;
29023 +       struct proc_dir_entry *entry;
29024 +
29025 +       err = -ENOMEM;
29026 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
29027 +       if (unlikely(!au_procfs_dir))
29028 +               goto out;
29029 +
29030 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
29031 +                           au_procfs_dir, &au_procfs_plm_op);
29032 +       if (unlikely(!entry))
29033 +               goto out_dir;
29034 +
29035 +       err = 0;
29036 +       goto out; /* success */
29037 +
29038 +
29039 +out_dir:
29040 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29041 +out:
29042 +       return err;
29043 +}
29044 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
29045 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
29046 +++ linux/fs/aufs/rdu.c 2023-09-04 13:39:55.763295607 +0200
29047 @@ -0,0 +1,384 @@
29048 +// SPDX-License-Identifier: GPL-2.0
29049 +/*
29050 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29051 + *
29052 + * This program is free software; you can redistribute it and/or modify
29053 + * it under the terms of the GNU General Public License as published by
29054 + * the Free Software Foundation; either version 2 of the License, or
29055 + * (at your option) any later version.
29056 + *
29057 + * This program is distributed in the hope that it will be useful,
29058 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29059 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29060 + * GNU General Public License for more details.
29061 + *
29062 + * You should have received a copy of the GNU General Public License
29063 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29064 + */
29065 +
29066 +/*
29067 + * readdir in userspace.
29068 + */
29069 +
29070 +#include <linux/compat.h>
29071 +#include <linux/fs_stack.h>
29072 +#include <linux/security.h>
29073 +#include "aufs.h"
29074 +
29075 +/* bits for struct aufs_rdu.flags */
29076 +#define        AuRdu_CALLED    1
29077 +#define        AuRdu_CONT      (1 << 1)
29078 +#define        AuRdu_FULL      (1 << 2)
29079 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
29080 +#define au_fset_rdu(flags, name) \
29081 +       do { (flags) |= AuRdu_##name; } while (0)
29082 +#define au_fclr_rdu(flags, name) \
29083 +       do { (flags) &= ~AuRdu_##name; } while (0)
29084 +
29085 +struct au_rdu_arg {
29086 +       struct dir_context              ctx;
29087 +       struct aufs_rdu                 *rdu;
29088 +       union au_rdu_ent_ul             ent;
29089 +       unsigned long                   end;
29090 +
29091 +       struct super_block              *sb;
29092 +       int                             err;
29093 +};
29094 +
29095 +static bool au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29096 +                       loff_t offset, u64 h_ino, unsigned int d_type)
29097 +{
29098 +       int err, len;
29099 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
29100 +       struct aufs_rdu *rdu = arg->rdu;
29101 +       struct au_rdu_ent ent;
29102 +
29103 +       err = 0;
29104 +       arg->err = 0;
29105 +       au_fset_rdu(rdu->cookie.flags, CALLED);
29106 +       len = au_rdu_len(nlen);
29107 +       if (arg->ent.ul + len  < arg->end) {
29108 +               ent.ino = h_ino;
29109 +               ent.bindex = rdu->cookie.bindex;
29110 +               ent.type = d_type;
29111 +               ent.nlen = nlen;
29112 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
29113 +                       ent.type = DT_UNKNOWN;
29114 +
29115 +               /* unnecessary to support mmap_sem since this is a dir */
29116 +               err = -EFAULT;
29117 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29118 +                       goto out;
29119 +               if (copy_to_user(arg->ent.e->name, name, nlen))
29120 +                       goto out;
29121 +               /* the terminating NULL */
29122 +               if (__put_user(0, arg->ent.e->name + nlen))
29123 +                       goto out;
29124 +               err = 0;
29125 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29126 +               arg->ent.ul += len;
29127 +               rdu->rent++;
29128 +       } else {
29129 +               err = -EFAULT;
29130 +               au_fset_rdu(rdu->cookie.flags, FULL);
29131 +               rdu->full = 1;
29132 +               rdu->tail = arg->ent;
29133 +       }
29134 +
29135 +out:
29136 +       /* AuTraceErr(err); */
29137 +       return !err;
29138 +}
29139 +
29140 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29141 +{
29142 +       int err;
29143 +       loff_t offset;
29144 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29145 +
29146 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
29147 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29148 +       err = offset;
29149 +       if (unlikely(offset != cookie->h_pos))
29150 +               goto out;
29151 +
29152 +       err = 0;
29153 +       do {
29154 +               arg->err = 0;
29155 +               au_fclr_rdu(cookie->flags, CALLED);
29156 +               /* smp_mb(); */
29157 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
29158 +               if (err >= 0)
29159 +                       err = arg->err;
29160 +       } while (!err
29161 +                && au_ftest_rdu(cookie->flags, CALLED)
29162 +                && !au_ftest_rdu(cookie->flags, FULL));
29163 +       cookie->h_pos = h_file->f_pos;
29164 +
29165 +out:
29166 +       AuTraceErr(err);
29167 +       return err;
29168 +}
29169 +
29170 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29171 +{
29172 +       int err;
29173 +       aufs_bindex_t bbot;
29174 +       struct au_rdu_arg arg = {
29175 +               .ctx = {
29176 +                       .actor = au_rdu_fill
29177 +               }
29178 +       };
29179 +       struct dentry *dentry;
29180 +       struct inode *inode;
29181 +       struct file *h_file;
29182 +       struct au_rdu_cookie *cookie = &rdu->cookie;
29183 +
29184 +       /* VERIFY_WRITE */
29185 +       err = !access_ok(rdu->ent.e, rdu->sz);
29186 +       if (unlikely(err)) {
29187 +               err = -EFAULT;
29188 +               AuTraceErr(err);
29189 +               goto out;
29190 +       }
29191 +       rdu->rent = 0;
29192 +       rdu->tail = rdu->ent;
29193 +       rdu->full = 0;
29194 +       arg.rdu = rdu;
29195 +       arg.ent = rdu->ent;
29196 +       arg.end = arg.ent.ul;
29197 +       arg.end += rdu->sz;
29198 +
29199 +       err = -ENOTDIR;
29200 +       if (unlikely(!file->f_op->iterate_shared))
29201 +               goto out;
29202 +
29203 +       err = security_file_permission(file, MAY_READ);
29204 +       AuTraceErr(err);
29205 +       if (unlikely(err))
29206 +               goto out;
29207 +
29208 +       dentry = file->f_path.dentry;
29209 +       inode = d_inode(dentry);
29210 +       inode_lock_shared(inode);
29211 +
29212 +       arg.sb = inode->i_sb;
29213 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29214 +       if (unlikely(err))
29215 +               goto out_mtx;
29216 +       err = au_alive_dir(dentry);
29217 +       if (unlikely(err))
29218 +               goto out_si;
29219 +       /* todo: reval? */
29220 +       fi_read_lock(file);
29221 +
29222 +       err = -EAGAIN;
29223 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29224 +                    && cookie->generation != au_figen(file)))
29225 +               goto out_unlock;
29226 +
29227 +       err = 0;
29228 +       if (!rdu->blk) {
29229 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
29230 +               if (!rdu->blk)
29231 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
29232 +       }
29233 +       bbot = au_fbtop(file);
29234 +       if (cookie->bindex < bbot)
29235 +               cookie->bindex = bbot;
29236 +       bbot = au_fbbot_dir(file);
29237 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29238 +       for (; !err && cookie->bindex <= bbot;
29239 +            cookie->bindex++, cookie->h_pos = 0) {
29240 +               h_file = au_hf_dir(file, cookie->bindex);
29241 +               if (!h_file)
29242 +                       continue;
29243 +
29244 +               au_fclr_rdu(cookie->flags, FULL);
29245 +               err = au_rdu_do(h_file, &arg);
29246 +               AuTraceErr(err);
29247 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29248 +                       break;
29249 +       }
29250 +       AuDbg("rent %llu\n", rdu->rent);
29251 +
29252 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29253 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29254 +               au_fset_rdu(cookie->flags, CONT);
29255 +               cookie->generation = au_figen(file);
29256 +       }
29257 +
29258 +       ii_read_lock_child(inode);
29259 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
29260 +       ii_read_unlock(inode);
29261 +
29262 +out_unlock:
29263 +       fi_read_unlock(file);
29264 +out_si:
29265 +       si_read_unlock(arg.sb);
29266 +out_mtx:
29267 +       inode_unlock_shared(inode);
29268 +out:
29269 +       AuTraceErr(err);
29270 +       return err;
29271 +}
29272 +
29273 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29274 +{
29275 +       int err;
29276 +       ino_t ino;
29277 +       unsigned long long nent;
29278 +       union au_rdu_ent_ul *u;
29279 +       struct au_rdu_ent ent;
29280 +       struct super_block *sb;
29281 +
29282 +       err = 0;
29283 +       nent = rdu->nent;
29284 +       u = &rdu->ent;
29285 +       sb = file->f_path.dentry->d_sb;
29286 +       si_read_lock(sb, AuLock_FLUSH);
29287 +       while (nent-- > 0) {
29288 +               /* unnecessary to support mmap_sem since this is a dir */
29289 +               err = copy_from_user(&ent, u->e, sizeof(ent));
29290 +               if (!err)
29291 +                       /* VERIFY_WRITE */
29292 +                       err = !access_ok(&u->e->ino, sizeof(ino));
29293 +               if (unlikely(err)) {
29294 +                       err = -EFAULT;
29295 +                       AuTraceErr(err);
29296 +                       break;
29297 +               }
29298 +
29299 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29300 +               if (!ent.wh)
29301 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29302 +               else
29303 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29304 +                                       &ino);
29305 +               if (unlikely(err)) {
29306 +                       AuTraceErr(err);
29307 +                       break;
29308 +               }
29309 +
29310 +               err = __put_user(ino, &u->e->ino);
29311 +               if (unlikely(err)) {
29312 +                       err = -EFAULT;
29313 +                       AuTraceErr(err);
29314 +                       break;
29315 +               }
29316 +               u->ul += au_rdu_len(ent.nlen);
29317 +       }
29318 +       si_read_unlock(sb);
29319 +
29320 +       return err;
29321 +}
29322 +
29323 +/* ---------------------------------------------------------------------- */
29324 +
29325 +static int au_rdu_verify(struct aufs_rdu *rdu)
29326 +{
29327 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29328 +             "%llu, b%d, 0x%x, g%u}\n",
29329 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29330 +             rdu->blk,
29331 +             rdu->rent, rdu->shwh, rdu->full,
29332 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29333 +             rdu->cookie.generation);
29334 +
29335 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29336 +               return 0;
29337 +
29338 +       AuDbg("%u:%u\n",
29339 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29340 +       return -EINVAL;
29341 +}
29342 +
29343 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29344 +{
29345 +       long err, e;
29346 +       struct aufs_rdu rdu;
29347 +       void __user *p = (void __user *)arg;
29348 +
29349 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29350 +       if (unlikely(err)) {
29351 +               err = -EFAULT;
29352 +               AuTraceErr(err);
29353 +               goto out;
29354 +       }
29355 +       err = au_rdu_verify(&rdu);
29356 +       if (unlikely(err))
29357 +               goto out;
29358 +
29359 +       switch (cmd) {
29360 +       case AUFS_CTL_RDU:
29361 +               err = au_rdu(file, &rdu);
29362 +               if (unlikely(err))
29363 +                       break;
29364 +
29365 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29366 +               if (unlikely(e)) {
29367 +                       err = -EFAULT;
29368 +                       AuTraceErr(err);
29369 +               }
29370 +               break;
29371 +       case AUFS_CTL_RDU_INO:
29372 +               err = au_rdu_ino(file, &rdu);
29373 +               break;
29374 +
29375 +       default:
29376 +               /* err = -ENOTTY; */
29377 +               err = -EINVAL;
29378 +       }
29379 +
29380 +out:
29381 +       AuTraceErr(err);
29382 +       return err;
29383 +}
29384 +
29385 +#ifdef CONFIG_COMPAT
29386 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29387 +{
29388 +       long err, e;
29389 +       struct aufs_rdu rdu;
29390 +       void __user *p = compat_ptr(arg);
29391 +
29392 +       /* todo: get_user()? */
29393 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29394 +       if (unlikely(err)) {
29395 +               err = -EFAULT;
29396 +               AuTraceErr(err);
29397 +               goto out;
29398 +       }
29399 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29400 +       err = au_rdu_verify(&rdu);
29401 +       if (unlikely(err))
29402 +               goto out;
29403 +
29404 +       switch (cmd) {
29405 +       case AUFS_CTL_RDU:
29406 +               err = au_rdu(file, &rdu);
29407 +               if (unlikely(err))
29408 +                       break;
29409 +
29410 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29411 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29412 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29413 +               if (unlikely(e)) {
29414 +                       err = -EFAULT;
29415 +                       AuTraceErr(err);
29416 +               }
29417 +               break;
29418 +       case AUFS_CTL_RDU_INO:
29419 +               err = au_rdu_ino(file, &rdu);
29420 +               break;
29421 +
29422 +       default:
29423 +               /* err = -ENOTTY; */
29424 +               err = -EINVAL;
29425 +       }
29426 +
29427 +out:
29428 +       AuTraceErr(err);
29429 +       return err;
29430 +}
29431 +#endif
29432 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29433 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29434 +++ linux/fs/aufs/rwsem.h       2022-11-05 23:02:18.969222617 +0100
29435 @@ -0,0 +1,85 @@
29436 +/* SPDX-License-Identifier: GPL-2.0 */
29437 +/*
29438 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29439 + *
29440 + * This program is free software; you can redistribute it and/or modify
29441 + * it under the terms of the GNU General Public License as published by
29442 + * the Free Software Foundation; either version 2 of the License, or
29443 + * (at your option) any later version.
29444 + *
29445 + * This program is distributed in the hope that it will be useful,
29446 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29447 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29448 + * GNU General Public License for more details.
29449 + *
29450 + * You should have received a copy of the GNU General Public License
29451 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29452 + */
29453 +
29454 +/*
29455 + * simple read-write semaphore wrappers
29456 + */
29457 +
29458 +#ifndef __AUFS_RWSEM_H__
29459 +#define __AUFS_RWSEM_H__
29460 +
29461 +#ifdef __KERNEL__
29462 +
29463 +#include "debug.h"
29464 +
29465 +/* in the future, the name 'au_rwsem' will be totally gone */
29466 +#define au_rwsem       rw_semaphore
29467 +
29468 +/* to debug easier, do not make them inlined functions */
29469 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29470 +
29471 +#ifdef CONFIG_LOCKDEP
29472 +/* rwsem_is_locked() is unusable */
29473 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29474 +                                         && !lockdep_recursing(current) \
29475 +                                         && debug_locks                \
29476 +                                         && !lockdep_is_held_type(rw, 1))
29477 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29478 +                                         && !lockdep_recursing(current) \
29479 +                                         && debug_locks                \
29480 +                                         && !lockdep_is_held_type(rw, 0))
29481 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29482 +                                         && !lockdep_recursing(current) \
29483 +                                         && debug_locks                \
29484 +                                         && !lockdep_is_held(rw))
29485 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29486 +                                         && !lockdep_recursing(current) \
29487 +                                         && debug_locks                \
29488 +                                         && lockdep_is_held(rw))
29489 +#else
29490 +#define AuRwMustReadLock(rw)   do {} while (0)
29491 +#define AuRwMustWriteLock(rw)  do {} while (0)
29492 +#define AuRwMustAnyLock(rw)    do {} while (0)
29493 +#define AuRwDestroy(rw)                do {} while (0)
29494 +#endif
29495 +
29496 +#define au_rw_init(rw) init_rwsem(rw)
29497 +
29498 +#define au_rw_init_wlock(rw) do {              \
29499 +               au_rw_init(rw);                 \
29500 +               down_write(rw);                 \
29501 +       } while (0)
29502 +
29503 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29504 +               au_rw_init(rw);                 \
29505 +               down_write_nested(rw, lsc);     \
29506 +       } while (0)
29507 +
29508 +#define au_rw_read_lock(rw)            down_read(rw)
29509 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29510 +#define au_rw_read_unlock(rw)          up_read(rw)
29511 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29512 +#define au_rw_write_lock(rw)           down_write(rw)
29513 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29514 +#define au_rw_write_unlock(rw)         up_write(rw)
29515 +/* why is not _nested version defined? */
29516 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29517 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29518 +
29519 +#endif /* __KERNEL__ */
29520 +#endif /* __AUFS_RWSEM_H__ */
29521 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29522 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29523 +++ linux/fs/aufs/sbinfo.c      2022-11-05 23:02:18.969222617 +0100
29524 @@ -0,0 +1,316 @@
29525 +// SPDX-License-Identifier: GPL-2.0
29526 +/*
29527 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29528 + *
29529 + * This program is free software; you can redistribute it and/or modify
29530 + * it under the terms of the GNU General Public License as published by
29531 + * the Free Software Foundation; either version 2 of the License, or
29532 + * (at your option) any later version.
29533 + *
29534 + * This program is distributed in the hope that it will be useful,
29535 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29536 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29537 + * GNU General Public License for more details.
29538 + *
29539 + * You should have received a copy of the GNU General Public License
29540 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29541 + */
29542 +
29543 +/*
29544 + * superblock private data
29545 + */
29546 +
29547 +#include <linux/iversion.h>
29548 +#include "aufs.h"
29549 +
29550 +/*
29551 + * they are necessary regardless sysfs is disabled.
29552 + */
29553 +void au_si_free(struct kobject *kobj)
29554 +{
29555 +       int i;
29556 +       struct au_sbinfo *sbinfo;
29557 +       char *locked __maybe_unused; /* debug only */
29558 +
29559 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29560 +       for (i = 0; i < AuPlink_NHASH; i++)
29561 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29562 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29563 +
29564 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29565 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29566 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29567 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29568 +
29569 +       dbgaufs_si_fin(sbinfo);
29570 +       au_rw_write_lock(&sbinfo->si_rwsem);
29571 +       au_br_free(sbinfo);
29572 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29573 +
29574 +       au_kfree_try_rcu(sbinfo->si_branch);
29575 +       mutex_destroy(&sbinfo->si_xib_mtx);
29576 +       AuRwDestroy(&sbinfo->si_rwsem);
29577 +
29578 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29579 +       /* si_nfiles is waited too */
29580 +       au_kfree_rcu(sbinfo);
29581 +}
29582 +
29583 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29584 +{
29585 +       struct au_sbinfo *sbinfo;
29586 +       int err, i;
29587 +
29588 +       err = -ENOMEM;
29589 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29590 +       if (unlikely(!sbinfo))
29591 +               goto out;
29592 +
29593 +       /* will be reallocated separately */
29594 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29595 +       if (unlikely(!sbinfo->si_branch))
29596 +               goto out_sbinfo;
29597 +
29598 +       err = sysaufs_si_init(sbinfo);
29599 +       if (!err) {
29600 +               dbgaufs_si_null(sbinfo);
29601 +               err = dbgaufs_si_init(sbinfo);
29602 +               if (unlikely(err))
29603 +                       kobject_put(&sbinfo->si_kobj);
29604 +       }
29605 +       if (unlikely(err))
29606 +               goto out_br;
29607 +
29608 +       au_nwt_init(&sbinfo->si_nowait);
29609 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29610 +
29611 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29612 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29613 +
29614 +       sbinfo->si_bbot = -1;
29615 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29616 +
29617 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29618 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29619 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29620 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29621 +
29622 +       au_fhsm_init(sbinfo);
29623 +
29624 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29625 +
29626 +       sbinfo->si_xino_jiffy = jiffies;
29627 +       sbinfo->si_xino_expire
29628 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29629 +       mutex_init(&sbinfo->si_xib_mtx);
29630 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29631 +
29632 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29633 +
29634 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29635 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29636 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29637 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29638 +
29639 +       for (i = 0; i < AuPlink_NHASH; i++)
29640 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29641 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29642 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29643 +
29644 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29645 +
29646 +       /* with getattr by default */
29647 +       sbinfo->si_iop_array = aufs_iop;
29648 +
29649 +       /* leave other members for sysaufs and si_mnt. */
29650 +       sbinfo->si_sb = sb;
29651 +       if (sb) {
29652 +               sb->s_fs_info = sbinfo;
29653 +               si_pid_set(sb);
29654 +       }
29655 +       return sbinfo; /* success */
29656 +
29657 +out_br:
29658 +       au_kfree_try_rcu(sbinfo->si_branch);
29659 +out_sbinfo:
29660 +       au_kfree_rcu(sbinfo);
29661 +out:
29662 +       return ERR_PTR(err);
29663 +}
29664 +
29665 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29666 +{
29667 +       int err, sz;
29668 +       struct au_branch **brp;
29669 +
29670 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29671 +
29672 +       err = -ENOMEM;
29673 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29674 +       if (unlikely(!sz))
29675 +               sz = sizeof(*brp);
29676 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29677 +                          may_shrink);
29678 +       if (brp) {
29679 +               sbinfo->si_branch = brp;
29680 +               err = 0;
29681 +       }
29682 +
29683 +       return err;
29684 +}
29685 +
29686 +/* ---------------------------------------------------------------------- */
29687 +
29688 +unsigned int au_sigen_inc(struct super_block *sb)
29689 +{
29690 +       unsigned int gen;
29691 +       struct inode *inode;
29692 +
29693 +       SiMustWriteLock(sb);
29694 +
29695 +       gen = ++au_sbi(sb)->si_generation;
29696 +       au_update_digen(sb->s_root);
29697 +       inode = d_inode(sb->s_root);
29698 +       au_update_iigen(inode, /*half*/0);
29699 +       inode_inc_iversion(inode);
29700 +       return gen;
29701 +}
29702 +
29703 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29704 +{
29705 +       aufs_bindex_t br_id;
29706 +       int i;
29707 +       struct au_sbinfo *sbinfo;
29708 +
29709 +       SiMustWriteLock(sb);
29710 +
29711 +       sbinfo = au_sbi(sb);
29712 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29713 +               br_id = ++sbinfo->si_last_br_id;
29714 +               AuDebugOn(br_id < 0);
29715 +               if (br_id && au_br_index(sb, br_id) < 0)
29716 +                       return br_id;
29717 +       }
29718 +
29719 +       return -1;
29720 +}
29721 +
29722 +/* ---------------------------------------------------------------------- */
29723 +
29724 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29725 +int si_read_lock(struct super_block *sb, int flags)
29726 +{
29727 +       int err;
29728 +
29729 +       err = 0;
29730 +       if (au_ftest_lock(flags, FLUSH))
29731 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29732 +
29733 +       si_noflush_read_lock(sb);
29734 +       err = au_plink_maint(sb, flags);
29735 +       if (unlikely(err))
29736 +               si_read_unlock(sb);
29737 +
29738 +       return err;
29739 +}
29740 +
29741 +int si_write_lock(struct super_block *sb, int flags)
29742 +{
29743 +       int err;
29744 +
29745 +       if (au_ftest_lock(flags, FLUSH))
29746 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29747 +
29748 +       si_noflush_write_lock(sb);
29749 +       err = au_plink_maint(sb, flags);
29750 +       if (unlikely(err))
29751 +               si_write_unlock(sb);
29752 +
29753 +       return err;
29754 +}
29755 +
29756 +/* dentry and super_block lock. call at entry point */
29757 +int aufs_read_lock(struct dentry *dentry, int flags)
29758 +{
29759 +       int err;
29760 +       struct super_block *sb;
29761 +
29762 +       sb = dentry->d_sb;
29763 +       err = si_read_lock(sb, flags);
29764 +       if (unlikely(err))
29765 +               goto out;
29766 +
29767 +       if (au_ftest_lock(flags, DW))
29768 +               di_write_lock_child(dentry);
29769 +       else
29770 +               di_read_lock_child(dentry, flags);
29771 +
29772 +       if (au_ftest_lock(flags, GEN)) {
29773 +               err = au_digen_test(dentry, au_sigen(sb));
29774 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29775 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29776 +               else if (!err)
29777 +                       err = au_dbrange_test(dentry);
29778 +               if (unlikely(err))
29779 +                       aufs_read_unlock(dentry, flags);
29780 +       }
29781 +
29782 +out:
29783 +       return err;
29784 +}
29785 +
29786 +void aufs_read_unlock(struct dentry *dentry, int flags)
29787 +{
29788 +       if (au_ftest_lock(flags, DW))
29789 +               di_write_unlock(dentry);
29790 +       else
29791 +               di_read_unlock(dentry, flags);
29792 +       si_read_unlock(dentry->d_sb);
29793 +}
29794 +
29795 +void aufs_write_lock(struct dentry *dentry)
29796 +{
29797 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29798 +       di_write_lock_child(dentry);
29799 +}
29800 +
29801 +void aufs_write_unlock(struct dentry *dentry)
29802 +{
29803 +       di_write_unlock(dentry);
29804 +       si_write_unlock(dentry->d_sb);
29805 +}
29806 +
29807 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29808 +{
29809 +       int err;
29810 +       unsigned int sigen;
29811 +       struct super_block *sb;
29812 +
29813 +       sb = d1->d_sb;
29814 +       err = si_read_lock(sb, flags);
29815 +       if (unlikely(err))
29816 +               goto out;
29817 +
29818 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29819 +
29820 +       if (au_ftest_lock(flags, GEN)) {
29821 +               sigen = au_sigen(sb);
29822 +               err = au_digen_test(d1, sigen);
29823 +               AuDebugOn(!err && au_dbrange_test(d1));
29824 +               if (!err) {
29825 +                       err = au_digen_test(d2, sigen);
29826 +                       AuDebugOn(!err && au_dbrange_test(d2));
29827 +               }
29828 +               if (unlikely(err))
29829 +                       aufs_read_and_write_unlock2(d1, d2);
29830 +       }
29831 +
29832 +out:
29833 +       return err;
29834 +}
29835 +
29836 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29837 +{
29838 +       di_write_unlock2(d1, d2);
29839 +       si_read_unlock(d1->d_sb);
29840 +}
29841 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29842 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29843 +++ linux/fs/aufs/super.c       2022-11-05 23:02:18.969222617 +0100
29844 @@ -0,0 +1,871 @@
29845 +// SPDX-License-Identifier: GPL-2.0
29846 +/*
29847 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29848 + *
29849 + * This program is free software; you can redistribute it and/or modify
29850 + * it under the terms of the GNU General Public License as published by
29851 + * the Free Software Foundation; either version 2 of the License, or
29852 + * (at your option) any later version.
29853 + *
29854 + * This program is distributed in the hope that it will be useful,
29855 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29856 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29857 + * GNU General Public License for more details.
29858 + *
29859 + * You should have received a copy of the GNU General Public License
29860 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29861 + */
29862 +
29863 +/*
29864 + * mount and super_block operations
29865 + */
29866 +
29867 +#include <linux/iversion.h>
29868 +#include <linux/mm.h>
29869 +#include <linux/seq_file.h>
29870 +#include <linux/statfs.h>
29871 +#include <linux/vmalloc.h>
29872 +#include "aufs.h"
29873 +
29874 +/*
29875 + * super_operations
29876 + */
29877 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29878 +{
29879 +       struct au_icntnr *c;
29880 +
29881 +       c = au_cache_alloc_icntnr(sb);
29882 +       if (c) {
29883 +               au_icntnr_init(c);
29884 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29885 +               c->iinfo.ii_hinode = NULL;
29886 +               return &c->vfs_inode;
29887 +       }
29888 +       return NULL;
29889 +}
29890 +
29891 +static void aufs_destroy_inode(struct inode *inode)
29892 +{
29893 +       if (!au_is_bad_inode(inode))
29894 +               au_iinfo_fin(inode);
29895 +}
29896 +
29897 +static void aufs_free_inode(struct inode *inode)
29898 +{
29899 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29900 +}
29901 +
29902 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29903 +{
29904 +       struct inode *inode;
29905 +       int err;
29906 +
29907 +       inode = iget_locked(sb, ino);
29908 +       if (unlikely(!inode)) {
29909 +               inode = ERR_PTR(-ENOMEM);
29910 +               goto out;
29911 +       }
29912 +       if (!(inode->i_state & I_NEW))
29913 +               goto out;
29914 +
29915 +       err = au_xigen_new(inode);
29916 +       if (!err)
29917 +               err = au_iinfo_init(inode);
29918 +       if (!err)
29919 +               inode_inc_iversion(inode);
29920 +       else {
29921 +               iget_failed(inode);
29922 +               inode = ERR_PTR(err);
29923 +       }
29924 +
29925 +out:
29926 +       /* never return NULL */
29927 +       AuDebugOn(!inode);
29928 +       AuTraceErrPtr(inode);
29929 +       return inode;
29930 +}
29931 +
29932 +/* lock free root dinfo */
29933 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29934 +{
29935 +       int err;
29936 +       aufs_bindex_t bindex, bbot;
29937 +       struct path path;
29938 +       struct au_hdentry *hdp;
29939 +       struct au_branch *br;
29940 +       au_br_perm_str_t perm;
29941 +
29942 +       err = 0;
29943 +       bbot = au_sbbot(sb);
29944 +       bindex = 0;
29945 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29946 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29947 +               br = au_sbr(sb, bindex);
29948 +               path.mnt = au_br_mnt(br);
29949 +               path.dentry = hdp->hd_dentry;
29950 +               err = au_seq_path(seq, &path);
29951 +               if (!err) {
29952 +                       au_optstr_br_perm(&perm, br->br_perm);
29953 +                       seq_printf(seq, "=%s", perm.a);
29954 +                       if (bindex != bbot)
29955 +                               seq_putc(seq, ':');
29956 +               }
29957 +       }
29958 +       if (unlikely(err || seq_has_overflowed(seq)))
29959 +               err = -E2BIG;
29960 +
29961 +       return err;
29962 +}
29963 +
29964 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29965 +                      const char *append)
29966 +{
29967 +       char *p;
29968 +
29969 +       p = fmt;
29970 +       while (*pat != ':')
29971 +               *p++ = *pat++;
29972 +       *p++ = *pat++;
29973 +       strcpy(p, append);
29974 +       AuDebugOn(strlen(fmt) >= len);
29975 +}
29976 +
29977 +static void au_show_wbr_create(struct seq_file *m, int v,
29978 +                              struct au_sbinfo *sbinfo)
29979 +{
29980 +       const char *pat;
29981 +       char fmt[32];
29982 +       struct au_wbr_mfs *mfs;
29983 +
29984 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29985 +
29986 +       seq_puts(m, ",create=");
29987 +       pat = au_optstr_wbr_create(v);
29988 +       mfs = &sbinfo->si_wbr_mfs;
29989 +       switch (v) {
29990 +       case AuWbrCreate_TDP:
29991 +       case AuWbrCreate_RR:
29992 +       case AuWbrCreate_MFS:
29993 +       case AuWbrCreate_PMFS:
29994 +               seq_puts(m, pat);
29995 +               break;
29996 +       case AuWbrCreate_MFSRR:
29997 +       case AuWbrCreate_TDMFS:
29998 +       case AuWbrCreate_PMFSRR:
29999 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
30000 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
30001 +               break;
30002 +       case AuWbrCreate_MFSV:
30003 +       case AuWbrCreate_PMFSV:
30004 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
30005 +               seq_printf(m, fmt,
30006 +                          jiffies_to_msecs(mfs->mfs_expire)
30007 +                          / MSEC_PER_SEC);
30008 +               break;
30009 +       case AuWbrCreate_MFSRRV:
30010 +       case AuWbrCreate_TDMFSV:
30011 +       case AuWbrCreate_PMFSRRV:
30012 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
30013 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
30014 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
30015 +               break;
30016 +       default:
30017 +               BUG();
30018 +       }
30019 +}
30020 +
30021 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
30022 +{
30023 +#ifdef CONFIG_SYSFS
30024 +       return 0;
30025 +#else
30026 +       int err;
30027 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
30028 +       aufs_bindex_t bindex, brid;
30029 +       struct qstr *name;
30030 +       struct file *f;
30031 +       struct dentry *d, *h_root;
30032 +       struct au_branch *br;
30033 +
30034 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30035 +
30036 +       err = 0;
30037 +       f = au_sbi(sb)->si_xib;
30038 +       if (!f)
30039 +               goto out;
30040 +
30041 +       /* stop printing the default xino path on the first writable branch */
30042 +       h_root = NULL;
30043 +       bindex = au_xi_root(sb, f->f_path.dentry);
30044 +       if (bindex >= 0) {
30045 +               br = au_sbr_sb(sb, bindex);
30046 +               h_root = au_br_dentry(br);
30047 +       }
30048 +
30049 +       d = f->f_path.dentry;
30050 +       name = &d->d_name;
30051 +       /* safe ->d_parent because the file is unlinked */
30052 +       if (d->d_parent == h_root
30053 +           && name->len == len
30054 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
30055 +               goto out;
30056 +
30057 +       seq_puts(seq, ",xino=");
30058 +       err = au_xino_path(seq, f);
30059 +
30060 +out:
30061 +       return err;
30062 +#endif
30063 +}
30064 +
30065 +/* seq_file will re-call me in case of too long string */
30066 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
30067 +{
30068 +       int err;
30069 +       unsigned int mnt_flags, v;
30070 +       struct super_block *sb;
30071 +       struct au_sbinfo *sbinfo;
30072 +
30073 +#define AuBool(name, str) do { \
30074 +       v = au_opt_test(mnt_flags, name); \
30075 +       if (v != au_opt_test(AuOpt_Def, name)) \
30076 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
30077 +} while (0)
30078 +
30079 +#define AuStr(name, str) do { \
30080 +       v = mnt_flags & AuOptMask_##name; \
30081 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
30082 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30083 +} while (0)
30084 +
30085 +#define AuUInt(name, str, val) do { \
30086 +       if (val != AUFS_##name##_DEF) \
30087 +               seq_printf(m, "," #str "=%u", val); \
30088 +} while (0)
30089 +
30090 +       sb = dentry->d_sb;
30091 +       if (sb->s_flags & SB_POSIXACL)
30092 +               seq_puts(m, ",acl");
30093 +#if 0 /* reserved for future use */
30094 +       if (sb->s_flags & SB_I_VERSION)
30095 +               seq_puts(m, ",i_version");
30096 +#endif
30097 +
30098 +       /* lock free root dinfo */
30099 +       si_noflush_read_lock(sb);
30100 +       sbinfo = au_sbi(sb);
30101 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30102 +
30103 +       mnt_flags = au_mntflags(sb);
30104 +       if (au_opt_test(mnt_flags, XINO)) {
30105 +               err = au_show_xino(m, sb);
30106 +               if (unlikely(err))
30107 +                       goto out;
30108 +       } else
30109 +               seq_puts(m, ",noxino");
30110 +
30111 +       AuBool(TRUNC_XINO, trunc_xino);
30112 +       AuStr(UDBA, udba);
30113 +       AuBool(SHWH, shwh);
30114 +       AuBool(PLINK, plink);
30115 +       AuBool(DIO, dio);
30116 +       AuBool(DIRPERM1, dirperm1);
30117 +
30118 +       v = sbinfo->si_wbr_create;
30119 +       if (v != AuWbrCreate_Def)
30120 +               au_show_wbr_create(m, v, sbinfo);
30121 +
30122 +       v = sbinfo->si_wbr_copyup;
30123 +       if (v != AuWbrCopyup_Def)
30124 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30125 +
30126 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30127 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30128 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30129 +
30130 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30131 +
30132 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30133 +       AuUInt(RDCACHE, rdcache, v);
30134 +
30135 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30136 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30137 +
30138 +       au_fhsm_show(m, sbinfo);
30139 +
30140 +       AuBool(DIRREN, dirren);
30141 +       AuBool(SUM, sum);
30142 +       /* AuBool(SUM_W, wsum); */
30143 +       AuBool(WARN_PERM, warn_perm);
30144 +       AuBool(VERBOSE, verbose);
30145 +
30146 +out:
30147 +       /* be sure to print "br:" last */
30148 +       if (!sysaufs_brs) {
30149 +               seq_puts(m, ",br:");
30150 +               au_show_brs(m, sb);
30151 +       }
30152 +       si_read_unlock(sb);
30153 +       return 0;
30154 +
30155 +#undef AuBool
30156 +#undef AuStr
30157 +#undef AuUInt
30158 +}
30159 +
30160 +/* ---------------------------------------------------------------------- */
30161 +
30162 +/* sum mode which returns the summation for statfs(2) */
30163 +
30164 +static u64 au_add_till_max(u64 a, u64 b)
30165 +{
30166 +       u64 old;
30167 +
30168 +       old = a;
30169 +       a += b;
30170 +       if (old <= a)
30171 +               return a;
30172 +       return ULLONG_MAX;
30173 +}
30174 +
30175 +static u64 au_mul_till_max(u64 a, long mul)
30176 +{
30177 +       u64 old;
30178 +
30179 +       old = a;
30180 +       a *= mul;
30181 +       if (old <= a)
30182 +               return a;
30183 +       return ULLONG_MAX;
30184 +}
30185 +
30186 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30187 +{
30188 +       int err;
30189 +       long bsize, factor;
30190 +       u64 blocks, bfree, bavail, files, ffree;
30191 +       aufs_bindex_t bbot, bindex, i;
30192 +       unsigned char shared;
30193 +       struct path h_path;
30194 +       struct super_block *h_sb;
30195 +
30196 +       err = 0;
30197 +       bsize = LONG_MAX;
30198 +       files = 0;
30199 +       ffree = 0;
30200 +       blocks = 0;
30201 +       bfree = 0;
30202 +       bavail = 0;
30203 +       bbot = au_sbbot(sb);
30204 +       for (bindex = 0; bindex <= bbot; bindex++) {
30205 +               h_path.mnt = au_sbr_mnt(sb, bindex);
30206 +               h_sb = h_path.mnt->mnt_sb;
30207 +               shared = 0;
30208 +               for (i = 0; !shared && i < bindex; i++)
30209 +                       shared = (au_sbr_sb(sb, i) == h_sb);
30210 +               if (shared)
30211 +                       continue;
30212 +
30213 +               /* sb->s_root for NFS is unreliable */
30214 +               h_path.dentry = h_path.mnt->mnt_root;
30215 +               err = vfs_statfs(&h_path, buf);
30216 +               if (unlikely(err))
30217 +                       goto out;
30218 +
30219 +               if (bsize > buf->f_bsize) {
30220 +                       /*
30221 +                        * we will reduce bsize, so we have to expand blocks
30222 +                        * etc. to match them again
30223 +                        */
30224 +                       factor = (bsize / buf->f_bsize);
30225 +                       blocks = au_mul_till_max(blocks, factor);
30226 +                       bfree = au_mul_till_max(bfree, factor);
30227 +                       bavail = au_mul_till_max(bavail, factor);
30228 +                       bsize = buf->f_bsize;
30229 +               }
30230 +
30231 +               factor = (buf->f_bsize / bsize);
30232 +               blocks = au_add_till_max(blocks,
30233 +                               au_mul_till_max(buf->f_blocks, factor));
30234 +               bfree = au_add_till_max(bfree,
30235 +                               au_mul_till_max(buf->f_bfree, factor));
30236 +               bavail = au_add_till_max(bavail,
30237 +                               au_mul_till_max(buf->f_bavail, factor));
30238 +               files = au_add_till_max(files, buf->f_files);
30239 +               ffree = au_add_till_max(ffree, buf->f_ffree);
30240 +       }
30241 +
30242 +       buf->f_bsize = bsize;
30243 +       buf->f_blocks = blocks;
30244 +       buf->f_bfree = bfree;
30245 +       buf->f_bavail = bavail;
30246 +       buf->f_files = files;
30247 +       buf->f_ffree = ffree;
30248 +       buf->f_frsize = 0;
30249 +
30250 +out:
30251 +       return err;
30252 +}
30253 +
30254 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30255 +{
30256 +       int err;
30257 +       struct path h_path;
30258 +       struct super_block *sb;
30259 +
30260 +       /* lock free root dinfo */
30261 +       sb = dentry->d_sb;
30262 +       si_noflush_read_lock(sb);
30263 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
30264 +               /* sb->s_root for NFS is unreliable */
30265 +               h_path.mnt = au_sbr_mnt(sb, 0);
30266 +               h_path.dentry = h_path.mnt->mnt_root;
30267 +               err = vfs_statfs(&h_path, buf);
30268 +       } else
30269 +               err = au_statfs_sum(sb, buf);
30270 +       si_read_unlock(sb);
30271 +
30272 +       if (!err) {
30273 +               buf->f_type = AUFS_SUPER_MAGIC;
30274 +               buf->f_namelen = AUFS_MAX_NAMELEN;
30275 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30276 +       }
30277 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30278 +
30279 +       return err;
30280 +}
30281 +
30282 +/* ---------------------------------------------------------------------- */
30283 +
30284 +static int aufs_sync_fs(struct super_block *sb, int wait)
30285 +{
30286 +       int err, e;
30287 +       aufs_bindex_t bbot, bindex;
30288 +       struct au_branch *br;
30289 +       struct super_block *h_sb;
30290 +
30291 +       err = 0;
30292 +       si_noflush_read_lock(sb);
30293 +       bbot = au_sbbot(sb);
30294 +       for (bindex = 0; bindex <= bbot; bindex++) {
30295 +               br = au_sbr(sb, bindex);
30296 +               if (!au_br_writable(br->br_perm))
30297 +                       continue;
30298 +
30299 +               h_sb = au_sbr_sb(sb, bindex);
30300 +               e = vfsub_sync_filesystem(h_sb);
30301 +               if (unlikely(e && !err))
30302 +                       err = e;
30303 +               /* go on even if an error happens */
30304 +       }
30305 +       si_read_unlock(sb);
30306 +
30307 +       return err;
30308 +}
30309 +
30310 +/* ---------------------------------------------------------------------- */
30311 +
30312 +/* final actions when unmounting a file system */
30313 +static void aufs_put_super(struct super_block *sb)
30314 +{
30315 +       struct au_sbinfo *sbinfo;
30316 +
30317 +       sbinfo = au_sbi(sb);
30318 +       if (sbinfo)
30319 +               kobject_put(&sbinfo->si_kobj);
30320 +}
30321 +
30322 +/* ---------------------------------------------------------------------- */
30323 +
30324 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30325 +                    struct super_block *sb, void *arg)
30326 +{
30327 +       void *array;
30328 +       unsigned long long n, sz;
30329 +
30330 +       array = NULL;
30331 +       n = 0;
30332 +       if (!*hint)
30333 +               goto out;
30334 +
30335 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30336 +               array = ERR_PTR(-EMFILE);
30337 +               pr_err("hint %llu\n", *hint);
30338 +               goto out;
30339 +       }
30340 +
30341 +       sz = sizeof(array) * *hint;
30342 +       array = kzalloc(sz, GFP_NOFS);
30343 +       if (unlikely(!array))
30344 +               array = vzalloc(sz);
30345 +       if (unlikely(!array)) {
30346 +               array = ERR_PTR(-ENOMEM);
30347 +               goto out;
30348 +       }
30349 +
30350 +       n = cb(sb, array, *hint, arg);
30351 +       AuDebugOn(n > *hint);
30352 +
30353 +out:
30354 +       *hint = n;
30355 +       return array;
30356 +}
30357 +
30358 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30359 +                                      unsigned long long max __maybe_unused,
30360 +                                      void *arg)
30361 +{
30362 +       unsigned long long n;
30363 +       struct inode **p, *inode;
30364 +       struct list_head *head;
30365 +
30366 +       n = 0;
30367 +       p = a;
30368 +       head = arg;
30369 +       spin_lock(&sb->s_inode_list_lock);
30370 +       list_for_each_entry(inode, head, i_sb_list) {
30371 +               if (!au_is_bad_inode(inode)
30372 +                   && au_ii(inode)->ii_btop >= 0) {
30373 +                       spin_lock(&inode->i_lock);
30374 +                       if (atomic_read(&inode->i_count)) {
30375 +                               au_igrab(inode);
30376 +                               *p++ = inode;
30377 +                               n++;
30378 +                               AuDebugOn(n > max);
30379 +                       }
30380 +                       spin_unlock(&inode->i_lock);
30381 +               }
30382 +       }
30383 +       spin_unlock(&sb->s_inode_list_lock);
30384 +
30385 +       return n;
30386 +}
30387 +
30388 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30389 +{
30390 +       struct au_sbinfo *sbi;
30391 +
30392 +       sbi = au_sbi(sb);
30393 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30394 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30395 +}
30396 +
30397 +void au_iarray_free(struct inode **a, unsigned long long max)
30398 +{
30399 +       unsigned long long ull;
30400 +
30401 +       for (ull = 0; ull < max; ull++)
30402 +               iput(a[ull]);
30403 +       kvfree(a);
30404 +}
30405 +
30406 +/* ---------------------------------------------------------------------- */
30407 +
30408 +/*
30409 + * refresh dentry and inode at remount time.
30410 + */
30411 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30412 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30413 +                     struct dentry *parent)
30414 +{
30415 +       int err;
30416 +
30417 +       di_write_lock_child(dentry);
30418 +       di_read_lock_parent(parent, AuLock_IR);
30419 +       err = au_refresh_dentry(dentry, parent);
30420 +       if (!err && dir_flags)
30421 +               au_hn_reset(d_inode(dentry), dir_flags);
30422 +       di_read_unlock(parent, AuLock_IR);
30423 +       di_write_unlock(dentry);
30424 +
30425 +       return err;
30426 +}
30427 +
30428 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30429 +                          struct au_sbinfo *sbinfo,
30430 +                          const unsigned int dir_flags, unsigned int do_idop)
30431 +{
30432 +       int err;
30433 +       struct dentry *parent;
30434 +
30435 +       err = 0;
30436 +       parent = dget_parent(dentry);
30437 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30438 +               if (d_really_is_positive(dentry)) {
30439 +                       if (!d_is_dir(dentry))
30440 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30441 +                                                parent);
30442 +                       else {
30443 +                               err = au_do_refresh(dentry, dir_flags, parent);
30444 +                               if (unlikely(err))
30445 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30446 +                       }
30447 +               } else
30448 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30449 +               AuDbgDentry(dentry);
30450 +       }
30451 +       dput(parent);
30452 +
30453 +       if (!err) {
30454 +               if (do_idop)
30455 +                       au_refresh_dop(dentry, /*force_reval*/0);
30456 +       } else
30457 +               au_refresh_dop(dentry, /*force_reval*/1);
30458 +
30459 +       AuTraceErr(err);
30460 +       return err;
30461 +}
30462 +
30463 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30464 +{
30465 +       int err, i, j, ndentry, e;
30466 +       unsigned int sigen;
30467 +       struct au_dcsub_pages dpages;
30468 +       struct au_dpage *dpage;
30469 +       struct dentry **dentries, *d;
30470 +       struct au_sbinfo *sbinfo;
30471 +       struct dentry *root = sb->s_root;
30472 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30473 +
30474 +       if (do_idop)
30475 +               au_refresh_dop(root, /*force_reval*/0);
30476 +
30477 +       err = au_dpages_init(&dpages, GFP_NOFS);
30478 +       if (unlikely(err))
30479 +               goto out;
30480 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30481 +       if (unlikely(err))
30482 +               goto out_dpages;
30483 +
30484 +       sigen = au_sigen(sb);
30485 +       sbinfo = au_sbi(sb);
30486 +       for (i = 0; i < dpages.ndpage; i++) {
30487 +               dpage = dpages.dpages + i;
30488 +               dentries = dpage->dentries;
30489 +               ndentry = dpage->ndentry;
30490 +               for (j = 0; j < ndentry; j++) {
30491 +                       d = dentries[j];
30492 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30493 +                                           do_idop);
30494 +                       if (unlikely(e && !err))
30495 +                               err = e;
30496 +                       /* go on even err */
30497 +               }
30498 +       }
30499 +
30500 +out_dpages:
30501 +       au_dpages_free(&dpages);
30502 +out:
30503 +       return err;
30504 +}
30505 +
30506 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30507 +{
30508 +       int err, e;
30509 +       unsigned int sigen;
30510 +       unsigned long long max, ull;
30511 +       struct inode *inode, **array;
30512 +
30513 +       array = au_iarray_alloc(sb, &max);
30514 +       err = PTR_ERR(array);
30515 +       if (IS_ERR(array))
30516 +               goto out;
30517 +
30518 +       err = 0;
30519 +       sigen = au_sigen(sb);
30520 +       for (ull = 0; ull < max; ull++) {
30521 +               inode = array[ull];
30522 +               if (unlikely(!inode))
30523 +                       break;
30524 +
30525 +               e = 0;
30526 +               ii_write_lock_child(inode);
30527 +               if (au_iigen(inode, NULL) != sigen) {
30528 +                       e = au_refresh_hinode_self(inode);
30529 +                       if (unlikely(e)) {
30530 +                               au_refresh_iop(inode, /*force_getattr*/1);
30531 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30532 +                               if (!err)
30533 +                                       err = e;
30534 +                               /* go on even if err */
30535 +                       }
30536 +               }
30537 +               if (!e && do_idop)
30538 +                       au_refresh_iop(inode, /*force_getattr*/0);
30539 +               ii_write_unlock(inode);
30540 +       }
30541 +
30542 +       au_iarray_free(array, max);
30543 +
30544 +out:
30545 +       return err;
30546 +}
30547 +
30548 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30549 +{
30550 +       int err, e;
30551 +       unsigned int udba;
30552 +       aufs_bindex_t bindex, bbot;
30553 +       struct dentry *root;
30554 +       struct inode *inode;
30555 +       struct au_branch *br;
30556 +       struct au_sbinfo *sbi;
30557 +
30558 +       au_sigen_inc(sb);
30559 +       sbi = au_sbi(sb);
30560 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30561 +
30562 +       root = sb->s_root;
30563 +       DiMustNoWaiters(root);
30564 +       inode = d_inode(root);
30565 +       IiMustNoWaiters(inode);
30566 +
30567 +       udba = au_opt_udba(sb);
30568 +       bbot = au_sbbot(sb);
30569 +       for (bindex = 0; bindex <= bbot; bindex++) {
30570 +               br = au_sbr(sb, bindex);
30571 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30572 +               if (unlikely(err))
30573 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30574 +                               bindex, err);
30575 +               /* go on even if err */
30576 +       }
30577 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30578 +
30579 +       if (do_idop) {
30580 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30581 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30582 +                       sb->s_d_op = &aufs_dop_noreval;
30583 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30584 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30585 +               } else {
30586 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30587 +                       sb->s_d_op = &aufs_dop;
30588 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30589 +                       sbi->si_iop_array = aufs_iop;
30590 +               }
30591 +               pr_info("reset to %ps and %ps\n",
30592 +                       sb->s_d_op, sbi->si_iop_array);
30593 +       }
30594 +
30595 +       di_write_unlock(root);
30596 +       err = au_refresh_d(sb, do_idop);
30597 +       e = au_refresh_i(sb, do_idop);
30598 +       if (unlikely(e && !err))
30599 +               err = e;
30600 +       /* aufs_write_lock() calls ..._child() */
30601 +       di_write_lock_child(root);
30602 +
30603 +       au_cpup_attr_all(inode, /*force*/1);
30604 +
30605 +       if (unlikely(err))
30606 +               AuIOErr("refresh failed, ignored, %d\n", err);
30607 +}
30608 +
30609 +const struct super_operations aufs_sop = {
30610 +       .alloc_inode    = aufs_alloc_inode,
30611 +       .destroy_inode  = aufs_destroy_inode,
30612 +       .free_inode     = aufs_free_inode,
30613 +       /* always deleting, no clearing */
30614 +       .drop_inode     = generic_delete_inode,
30615 +       .show_options   = aufs_show_options,
30616 +       .statfs         = aufs_statfs,
30617 +       .put_super      = aufs_put_super,
30618 +       .sync_fs        = aufs_sync_fs
30619 +};
30620 +
30621 +/* ---------------------------------------------------------------------- */
30622 +
30623 +int au_alloc_root(struct super_block *sb)
30624 +{
30625 +       int err;
30626 +       struct inode *inode;
30627 +       struct dentry *root;
30628 +
30629 +       err = -ENOMEM;
30630 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30631 +       err = PTR_ERR(inode);
30632 +       if (IS_ERR(inode))
30633 +               goto out;
30634 +
30635 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30636 +       inode->i_fop = &aufs_dir_fop;
30637 +       inode->i_mode = S_IFDIR;
30638 +       set_nlink(inode, 2);
30639 +       unlock_new_inode(inode);
30640 +
30641 +       root = d_make_root(inode);
30642 +       if (unlikely(!root))
30643 +               goto out;
30644 +       err = PTR_ERR(root);
30645 +       if (IS_ERR(root))
30646 +               goto out;
30647 +
30648 +       err = au_di_init(root);
30649 +       if (!err) {
30650 +               sb->s_root = root;
30651 +               return 0; /* success */
30652 +       }
30653 +       dput(root);
30654 +
30655 +out:
30656 +       return err;
30657 +}
30658 +
30659 +/* ---------------------------------------------------------------------- */
30660 +
30661 +static void aufs_kill_sb(struct super_block *sb)
30662 +{
30663 +       struct au_sbinfo *sbinfo;
30664 +       struct dentry *root;
30665 +
30666 +       sbinfo = au_sbi(sb);
30667 +       if (!sbinfo)
30668 +               goto out;
30669 +
30670 +       au_sbilist_del(sb);
30671 +
30672 +       root = sb->s_root;
30673 +       if (root)
30674 +               aufs_write_lock(root);
30675 +       else
30676 +               __si_write_lock(sb);
30677 +
30678 +       au_fhsm_fin(sb);
30679 +       if (sbinfo->si_wbr_create_ops->fin)
30680 +               sbinfo->si_wbr_create_ops->fin(sb);
30681 +       if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30682 +               au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30683 +               au_remount_refresh(sb, /*do_idop*/0);
30684 +       }
30685 +       if (au_opt_test(sbinfo->si_mntflags, PLINK))
30686 +               au_plink_put(sb, /*verbose*/1);
30687 +       au_xino_clr(sb);
30688 +       if (root)
30689 +               au_dr_opt_flush(sb);
30690 +
30691 +       if (root)
30692 +               aufs_write_unlock(root);
30693 +       else
30694 +               __si_write_unlock(sb);
30695 +
30696 +       sbinfo->si_sb = NULL;
30697 +       au_nwt_flush(&sbinfo->si_nowait);
30698 +
30699 +out:
30700 +       kill_anon_super(sb);
30701 +}
30702 +
30703 +struct file_system_type aufs_fs_type = {
30704 +       .name           = AUFS_FSTYPE,
30705 +       /* a race between rename and others */
30706 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30707 +                               /* untested */
30708 +                               /*| FS_ALLOW_IDMAP*/
30709 +                               ,
30710 +       .init_fs_context = aufs_fsctx_init,
30711 +       .parameters     = aufs_fsctx_paramspec,
30712 +       .kill_sb        = aufs_kill_sb,
30713 +       /* no need to __module_get() and module_put(). */
30714 +       .owner          = THIS_MODULE,
30715 +};
30716 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30717 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30718 +++ linux/fs/aufs/super.h       2022-11-05 23:02:18.969222617 +0100
30719 @@ -0,0 +1,592 @@
30720 +/* SPDX-License-Identifier: GPL-2.0 */
30721 +/*
30722 + * Copyright (C) 2005-2022 Junjiro R. Okajima
30723 + *
30724 + * This program is free software; you can redistribute it and/or modify
30725 + * it under the terms of the GNU General Public License as published by
30726 + * the Free Software Foundation; either version 2 of the License, or
30727 + * (at your option) any later version.
30728 + *
30729 + * This program is distributed in the hope that it will be useful,
30730 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30731 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30732 + * GNU General Public License for more details.
30733 + *
30734 + * You should have received a copy of the GNU General Public License
30735 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30736 + */
30737 +
30738 +/*
30739 + * super_block operations
30740 + */
30741 +
30742 +#ifndef __AUFS_SUPER_H__
30743 +#define __AUFS_SUPER_H__
30744 +
30745 +#ifdef __KERNEL__
30746 +
30747 +#include <linux/fs.h>
30748 +#include <linux/kobject.h>
30749 +#include "hbl.h"
30750 +#include "lcnt.h"
30751 +#include "rwsem.h"
30752 +#include "wkq.h"
30753 +
30754 +/* policies to select one among multiple writable branches */
30755 +struct au_wbr_copyup_operations {
30756 +       int (*copyup)(struct dentry *dentry);
30757 +};
30758 +
30759 +#define AuWbr_DIR      1               /* target is a dir */
30760 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30761 +
30762 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30763 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30764 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30765 +
30766 +struct au_wbr_create_operations {
30767 +       int (*create)(struct dentry *dentry, unsigned int flags);
30768 +       int (*init)(struct super_block *sb);
30769 +       int (*fin)(struct super_block *sb);
30770 +};
30771 +
30772 +struct au_wbr_mfs {
30773 +       struct mutex    mfs_lock; /* protect this structure */
30774 +       unsigned long   mfs_jiffy;
30775 +       unsigned long   mfs_expire;
30776 +       aufs_bindex_t   mfs_bindex;
30777 +
30778 +       unsigned long long      mfsrr_bytes;
30779 +       unsigned long long      mfsrr_watermark;
30780 +};
30781 +
30782 +#define AuPlink_NHASH 100
30783 +static inline int au_plink_hash(ino_t ino)
30784 +{
30785 +       return ino % AuPlink_NHASH;
30786 +}
30787 +
30788 +/* File-based Hierarchical Storage Management */
30789 +struct au_fhsm {
30790 +#ifdef CONFIG_AUFS_FHSM
30791 +       /* allow only one process who can receive the notification */
30792 +       spinlock_t              fhsm_spin;
30793 +       pid_t                   fhsm_pid;
30794 +       wait_queue_head_t       fhsm_wqh;
30795 +       atomic_t                fhsm_readable;
30796 +
30797 +       /* these are protected by si_rwsem */
30798 +       unsigned long           fhsm_expire;
30799 +       aufs_bindex_t           fhsm_bottom;
30800 +#endif
30801 +};
30802 +
30803 +struct au_branch;
30804 +struct au_sbinfo {
30805 +       /* nowait tasks in the system-wide workqueue */
30806 +       struct au_nowait_tasks  si_nowait;
30807 +
30808 +       /*
30809 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30810 +        * rwsem for au_sbinfo is necessary.
30811 +        */
30812 +       struct au_rwsem         si_rwsem;
30813 +
30814 +       /*
30815 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30816 +        * remount.
30817 +        */
30818 +       au_lcnt_t               si_ninodes, si_nfiles;
30819 +
30820 +       /* branch management */
30821 +       unsigned int            si_generation;
30822 +
30823 +       /* see AuSi_ flags */
30824 +       unsigned char           au_si_status;
30825 +
30826 +       aufs_bindex_t           si_bbot;
30827 +
30828 +       /* dirty trick to keep br_id plus */
30829 +       unsigned int            si_last_br_id :
30830 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30831 +       struct au_branch        **si_branch;
30832 +
30833 +       /* policy to select a writable branch */
30834 +       unsigned char           si_wbr_copyup;
30835 +       unsigned char           si_wbr_create;
30836 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30837 +       struct au_wbr_create_operations *si_wbr_create_ops;
30838 +
30839 +       /* round robin */
30840 +       atomic_t                si_wbr_rr_next;
30841 +
30842 +       /* most free space */
30843 +       struct au_wbr_mfs       si_wbr_mfs;
30844 +
30845 +       /* File-based Hierarchical Storage Management */
30846 +       struct au_fhsm          si_fhsm;
30847 +
30848 +       /* mount flags */
30849 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30850 +       unsigned int            si_mntflags;
30851 +
30852 +       /* external inode number (bitmap and translation table) */
30853 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30854 +
30855 +       struct file             *si_xib;
30856 +       struct mutex            si_xib_mtx; /* protect xib members */
30857 +       unsigned long           *si_xib_buf;
30858 +       unsigned long           si_xib_last_pindex;
30859 +       int                     si_xib_next_bit;
30860 +
30861 +       unsigned long           si_xino_jiffy;
30862 +       unsigned long           si_xino_expire;
30863 +       /* reserved for future use */
30864 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30865 +
30866 +#ifdef CONFIG_AUFS_EXPORT
30867 +       /* i_generation */
30868 +       /* todo: make xigen file an array to support many inode numbers */
30869 +       struct file             *si_xigen;
30870 +       atomic_t                si_xigen_next;
30871 +#endif
30872 +
30873 +       /* dirty trick to support atomic_open */
30874 +       struct hlist_bl_head    si_aopen;
30875 +
30876 +       /* vdir parameters */
30877 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30878 +       unsigned int            si_rdblk;       /* deblk size */
30879 +       unsigned int            si_rdhash;      /* hash size */
30880 +
30881 +       /*
30882 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30883 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30884 +        * future fsck.aufs or kernel thread will remove them later.
30885 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30886 +        */
30887 +       unsigned int            si_dirwh;
30888 +
30889 +       /* pseudo_link list */
30890 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30891 +       wait_queue_head_t       si_plink_wq;
30892 +       spinlock_t              si_plink_maint_lock;
30893 +       pid_t                   si_plink_maint_pid;
30894 +
30895 +       /* file list */
30896 +       struct hlist_bl_head    si_files;
30897 +
30898 +       /* with/without getattr, brother of sb->s_d_op */
30899 +       const struct inode_operations *si_iop_array;
30900 +
30901 +       /*
30902 +        * sysfs and lifetime management.
30903 +        * this is not a small structure and it may be a waste of memory in case
30904 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30905 +        * but using sysfs is majority.
30906 +        */
30907 +       struct kobject          si_kobj;
30908 +#ifdef CONFIG_DEBUG_FS
30909 +       struct dentry            *si_dbgaufs;
30910 +       struct dentry            *si_dbgaufs_plink;
30911 +       struct dentry            *si_dbgaufs_xib;
30912 +#ifdef CONFIG_AUFS_EXPORT
30913 +       struct dentry            *si_dbgaufs_xigen;
30914 +#endif
30915 +#endif
30916 +
30917 +#ifdef CONFIG_AUFS_SBILIST
30918 +       struct hlist_bl_node    si_list;
30919 +#endif
30920 +
30921 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30922 +       struct super_block      *si_sb;
30923 +};
30924 +
30925 +/* sbinfo status flags */
30926 +/*
30927 + * set true when refresh_dirs() failed at remount time.
30928 + * then try refreshing dirs at access time again.
30929 + * if it is false, refreshing dirs at access time is unnecessary
30930 + */
30931 +#define AuSi_FAILED_REFRESH_DIR        1
30932 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30933 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30934 +
30935 +#ifndef CONFIG_AUFS_FHSM
30936 +#undef AuSi_FHSM
30937 +#define AuSi_FHSM              0
30938 +#endif
30939 +
30940 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30941 +                                          unsigned int flag)
30942 +{
30943 +       AuRwMustAnyLock(&sbi->si_rwsem);
30944 +       return sbi->au_si_status & flag;
30945 +}
30946 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30947 +#define au_fset_si(sbinfo, name) do { \
30948 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30949 +       (sbinfo)->au_si_status |= AuSi_##name; \
30950 +} while (0)
30951 +#define au_fclr_si(sbinfo, name) do { \
30952 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30953 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30954 +} while (0)
30955 +
30956 +/* ---------------------------------------------------------------------- */
30957 +
30958 +/* policy to select one among writable branches */
30959 +#define AuWbrCopyup(sbinfo, ...) \
30960 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30961 +#define AuWbrCreate(sbinfo, ...) \
30962 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30963 +
30964 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30965 +#define AuLock_DW              1               /* write-lock dentry */
30966 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30967 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30968 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30969 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30970 +                                               /* except RENAME_EXCHANGE */
30971 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30972 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30973 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30974 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30975 +#define au_fset_lock(flags, name) \
30976 +       do { (flags) |= AuLock_##name; } while (0)
30977 +#define au_fclr_lock(flags, name) \
30978 +       do { (flags) &= ~AuLock_##name; } while (0)
30979 +
30980 +/* ---------------------------------------------------------------------- */
30981 +
30982 +/* super.c */
30983 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30984 +
30985 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30986 +                                          unsigned long long max, void *arg);
30987 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30988 +                    struct super_block *sb, void *arg);
30989 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30990 +void au_iarray_free(struct inode **a, unsigned long long max);
30991 +
30992 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
30993 +extern const struct super_operations aufs_sop;
30994 +int au_alloc_root(struct super_block *sb);
30995 +extern struct file_system_type aufs_fs_type;
30996 +
30997 +/* sbinfo.c */
30998 +void au_si_free(struct kobject *kobj);
30999 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
31000 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
31001 +
31002 +unsigned int au_sigen_inc(struct super_block *sb);
31003 +aufs_bindex_t au_new_br_id(struct super_block *sb);
31004 +
31005 +int si_read_lock(struct super_block *sb, int flags);
31006 +int si_write_lock(struct super_block *sb, int flags);
31007 +int aufs_read_lock(struct dentry *dentry, int flags);
31008 +void aufs_read_unlock(struct dentry *dentry, int flags);
31009 +void aufs_write_lock(struct dentry *dentry);
31010 +void aufs_write_unlock(struct dentry *dentry);
31011 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
31012 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
31013 +
31014 +/* wbr_policy.c */
31015 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
31016 +extern struct au_wbr_create_operations au_wbr_create_ops[];
31017 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
31018 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
31019 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
31020 +
31021 +/* mvdown.c */
31022 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
31023 +
31024 +#ifdef CONFIG_AUFS_FHSM
31025 +/* fhsm.c */
31026 +
31027 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
31028 +{
31029 +       pid_t pid;
31030 +
31031 +       spin_lock(&fhsm->fhsm_spin);
31032 +       pid = fhsm->fhsm_pid;
31033 +       spin_unlock(&fhsm->fhsm_spin);
31034 +
31035 +       return pid;
31036 +}
31037 +
31038 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
31039 +void au_fhsm_wrote_all(struct super_block *sb, int force);
31040 +int au_fhsm_fd(struct super_block *sb, int oflags);
31041 +int au_fhsm_br_alloc(struct au_branch *br);
31042 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
31043 +void au_fhsm_fin(struct super_block *sb);
31044 +void au_fhsm_init(struct au_sbinfo *sbinfo);
31045 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
31046 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
31047 +#else
31048 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
31049 +          int force)
31050 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
31051 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
31052 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
31053 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
31054 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
31055 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
31056 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
31057 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
31058 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31059 +#endif
31060 +
31061 +/* ---------------------------------------------------------------------- */
31062 +
31063 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31064 +{
31065 +       return sb->s_fs_info;
31066 +}
31067 +
31068 +/* ---------------------------------------------------------------------- */
31069 +
31070 +#ifdef CONFIG_AUFS_EXPORT
31071 +int au_test_nfsd(void);
31072 +void au_export_init(struct super_block *sb);
31073 +void au_xigen_inc(struct inode *inode);
31074 +int au_xigen_new(struct inode *inode);
31075 +int au_xigen_set(struct super_block *sb, struct path *path);
31076 +void au_xigen_clr(struct super_block *sb);
31077 +
31078 +static inline int au_busy_or_stale(void)
31079 +{
31080 +       if (!au_test_nfsd())
31081 +               return -EBUSY;
31082 +       return -ESTALE;
31083 +}
31084 +#else
31085 +AuStubInt0(au_test_nfsd, void)
31086 +AuStubVoid(au_export_init, struct super_block *sb)
31087 +AuStubVoid(au_xigen_inc, struct inode *inode)
31088 +AuStubInt0(au_xigen_new, struct inode *inode)
31089 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
31090 +AuStubVoid(au_xigen_clr, struct super_block *sb)
31091 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
31092 +#endif /* CONFIG_AUFS_EXPORT */
31093 +
31094 +/* ---------------------------------------------------------------------- */
31095 +
31096 +#ifdef CONFIG_AUFS_SBILIST
31097 +/* module.c */
31098 +extern struct hlist_bl_head au_sbilist;
31099 +
31100 +static inline void au_sbilist_init(void)
31101 +{
31102 +       INIT_HLIST_BL_HEAD(&au_sbilist);
31103 +}
31104 +
31105 +static inline void au_sbilist_add(struct super_block *sb)
31106 +{
31107 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
31108 +}
31109 +
31110 +static inline void au_sbilist_del(struct super_block *sb)
31111 +{
31112 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
31113 +}
31114 +
31115 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31116 +static inline void au_sbilist_lock(void)
31117 +{
31118 +       hlist_bl_lock(&au_sbilist);
31119 +}
31120 +
31121 +static inline void au_sbilist_unlock(void)
31122 +{
31123 +       hlist_bl_unlock(&au_sbilist);
31124 +}
31125 +#define AuGFP_SBILIST  GFP_ATOMIC
31126 +#else
31127 +AuStubVoid(au_sbilist_lock, void)
31128 +AuStubVoid(au_sbilist_unlock, void)
31129 +#define AuGFP_SBILIST  GFP_NOFS
31130 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
31131 +#else
31132 +AuStubVoid(au_sbilist_init, void)
31133 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31134 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31135 +AuStubVoid(au_sbilist_lock, void)
31136 +AuStubVoid(au_sbilist_unlock, void)
31137 +#define AuGFP_SBILIST  GFP_NOFS
31138 +#endif
31139 +
31140 +/* ---------------------------------------------------------------------- */
31141 +
31142 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31143 +{
31144 +       /*
31145 +        * This function is a dynamic '__init' function actually,
31146 +        * so the tiny check for si_rwsem is unnecessary.
31147 +        */
31148 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31149 +#ifdef CONFIG_DEBUG_FS
31150 +       sbinfo->si_dbgaufs = NULL;
31151 +       sbinfo->si_dbgaufs_plink = NULL;
31152 +       sbinfo->si_dbgaufs_xib = NULL;
31153 +#ifdef CONFIG_AUFS_EXPORT
31154 +       sbinfo->si_dbgaufs_xigen = NULL;
31155 +#endif
31156 +#endif
31157 +}
31158 +
31159 +/* ---------------------------------------------------------------------- */
31160 +
31161 +/* current->atomic_flags */
31162 +/* this value should never corrupt the ones defined in linux/sched.h */
31163 +#define PFA_AUFS       0x10
31164 +
31165 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31166 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31167 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31168 +
31169 +static inline int si_pid_test(struct super_block *sb)
31170 +{
31171 +       return !!task_test_aufs(current);
31172 +}
31173 +
31174 +static inline void si_pid_clr(struct super_block *sb)
31175 +{
31176 +       AuDebugOn(!task_test_aufs(current));
31177 +       task_clear_aufs(current);
31178 +}
31179 +
31180 +static inline void si_pid_set(struct super_block *sb)
31181 +{
31182 +       AuDebugOn(task_test_aufs(current));
31183 +       task_set_aufs(current);
31184 +}
31185 +
31186 +/* ---------------------------------------------------------------------- */
31187 +
31188 +/* lock superblock. mainly for entry point functions */
31189 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31190 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31191 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31192 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31193 +/*
31194 +#define __si_read_trylock_nested(sb) \
31195 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31196 +#define __si_write_trylock_nested(sb) \
31197 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31198 +*/
31199 +
31200 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31201 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31202 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31203 +
31204 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31205 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31206 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31207 +
31208 +static inline void si_noflush_read_lock(struct super_block *sb)
31209 +{
31210 +       __si_read_lock(sb);
31211 +       si_pid_set(sb);
31212 +}
31213 +
31214 +static inline int si_noflush_read_trylock(struct super_block *sb)
31215 +{
31216 +       int locked;
31217 +
31218 +       locked = __si_read_trylock(sb);
31219 +       if (locked)
31220 +               si_pid_set(sb);
31221 +       return locked;
31222 +}
31223 +
31224 +static inline void si_noflush_write_lock(struct super_block *sb)
31225 +{
31226 +       __si_write_lock(sb);
31227 +       si_pid_set(sb);
31228 +}
31229 +
31230 +static inline int si_noflush_write_trylock(struct super_block *sb)
31231 +{
31232 +       int locked;
31233 +
31234 +       locked = __si_write_trylock(sb);
31235 +       if (locked)
31236 +               si_pid_set(sb);
31237 +       return locked;
31238 +}
31239 +
31240 +#if 0 /* reserved */
31241 +static inline int si_read_trylock(struct super_block *sb, int flags)
31242 +{
31243 +       if (au_ftest_lock(flags, FLUSH))
31244 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31245 +       return si_noflush_read_trylock(sb);
31246 +}
31247 +#endif
31248 +
31249 +static inline void si_read_unlock(struct super_block *sb)
31250 +{
31251 +       si_pid_clr(sb);
31252 +       __si_read_unlock(sb);
31253 +}
31254 +
31255 +#if 0 /* reserved */
31256 +static inline int si_write_trylock(struct super_block *sb, int flags)
31257 +{
31258 +       if (au_ftest_lock(flags, FLUSH))
31259 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31260 +       return si_noflush_write_trylock(sb);
31261 +}
31262 +#endif
31263 +
31264 +static inline void si_write_unlock(struct super_block *sb)
31265 +{
31266 +       si_pid_clr(sb);
31267 +       __si_write_unlock(sb);
31268 +}
31269 +
31270 +#if 0 /* reserved */
31271 +static inline void si_downgrade_lock(struct super_block *sb)
31272 +{
31273 +       __si_downgrade_lock(sb);
31274 +}
31275 +#endif
31276 +
31277 +/* ---------------------------------------------------------------------- */
31278 +
31279 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31280 +{
31281 +       SiMustAnyLock(sb);
31282 +       return au_sbi(sb)->si_bbot;
31283 +}
31284 +
31285 +static inline unsigned int au_mntflags(struct super_block *sb)
31286 +{
31287 +       SiMustAnyLock(sb);
31288 +       return au_sbi(sb)->si_mntflags;
31289 +}
31290 +
31291 +static inline unsigned int au_sigen(struct super_block *sb)
31292 +{
31293 +       SiMustAnyLock(sb);
31294 +       return au_sbi(sb)->si_generation;
31295 +}
31296 +
31297 +static inline struct au_branch *au_sbr(struct super_block *sb,
31298 +                                      aufs_bindex_t bindex)
31299 +{
31300 +       SiMustAnyLock(sb);
31301 +       return au_sbi(sb)->si_branch[0 + bindex];
31302 +}
31303 +
31304 +static inline loff_t au_xi_maxent(struct super_block *sb)
31305 +{
31306 +       SiMustAnyLock(sb);
31307 +       return au_sbi(sb)->si_ximaxent;
31308 +}
31309 +
31310 +#endif /* __KERNEL__ */
31311 +#endif /* __AUFS_SUPER_H__ */
31312 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31313 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31314 +++ linux/fs/aufs/sysaufs.c     2022-11-05 23:02:18.969222617 +0100
31315 @@ -0,0 +1,94 @@
31316 +// SPDX-License-Identifier: GPL-2.0
31317 +/*
31318 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31319 + *
31320 + * This program is free software; you can redistribute it and/or modify
31321 + * it under the terms of the GNU General Public License as published by
31322 + * the Free Software Foundation; either version 2 of the License, or
31323 + * (at your option) any later version.
31324 + *
31325 + * This program is distributed in the hope that it will be useful,
31326 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31327 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31328 + * GNU General Public License for more details.
31329 + *
31330 + * You should have received a copy of the GNU General Public License
31331 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31332 + */
31333 +
31334 +/*
31335 + * sysfs interface and lifetime management
31336 + * they are necessary regardless sysfs is disabled.
31337 + */
31338 +
31339 +#include <linux/random.h>
31340 +#include "aufs.h"
31341 +
31342 +unsigned long sysaufs_si_mask;
31343 +struct kset *sysaufs_kset;
31344 +
31345 +#define AuSiAttr(_name) { \
31346 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31347 +       .show   = sysaufs_si_##_name,                           \
31348 +}
31349 +
31350 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31351 +struct attribute *sysaufs_si_attrs[] = {
31352 +       &sysaufs_si_attr_xi_path.attr,
31353 +       NULL,
31354 +};
31355 +ATTRIBUTE_GROUPS(sysaufs_si);
31356 +
31357 +static const struct sysfs_ops au_sbi_ops = {
31358 +       .show   = sysaufs_si_show
31359 +};
31360 +
31361 +static struct kobj_type au_sbi_ktype = {
31362 +       .release        = au_si_free,
31363 +       .sysfs_ops      = &au_sbi_ops,
31364 +       .default_groups = sysaufs_si_groups
31365 +};
31366 +
31367 +/* ---------------------------------------------------------------------- */
31368 +
31369 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31370 +{
31371 +       int err;
31372 +
31373 +       sbinfo->si_kobj.kset = sysaufs_kset;
31374 +       /* cf. sysaufs_name() */
31375 +       err = kobject_init_and_add
31376 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31377 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31378 +
31379 +       return err;
31380 +}
31381 +
31382 +void sysaufs_fin(void)
31383 +{
31384 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31385 +       kset_unregister(sysaufs_kset);
31386 +}
31387 +
31388 +int __init sysaufs_init(void)
31389 +{
31390 +       int err;
31391 +
31392 +       do {
31393 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31394 +       } while (!sysaufs_si_mask);
31395 +
31396 +       err = -EINVAL;
31397 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31398 +       if (unlikely(!sysaufs_kset))
31399 +               goto out;
31400 +       err = PTR_ERR(sysaufs_kset);
31401 +       if (IS_ERR(sysaufs_kset))
31402 +               goto out;
31403 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31404 +       if (unlikely(err))
31405 +               kset_unregister(sysaufs_kset);
31406 +
31407 +out:
31408 +       return err;
31409 +}
31410 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31411 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31412 +++ linux/fs/aufs/sysaufs.h     2022-11-05 23:02:18.969222617 +0100
31413 @@ -0,0 +1,102 @@
31414 +/* SPDX-License-Identifier: GPL-2.0 */
31415 +/*
31416 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31417 + *
31418 + * This program is free software; you can redistribute it and/or modify
31419 + * it under the terms of the GNU General Public License as published by
31420 + * the Free Software Foundation; either version 2 of the License, or
31421 + * (at your option) any later version.
31422 + *
31423 + * This program is distributed in the hope that it will be useful,
31424 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31425 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31426 + * GNU General Public License for more details.
31427 + *
31428 + * You should have received a copy of the GNU General Public License
31429 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31430 + */
31431 +
31432 +/*
31433 + * sysfs interface and mount lifetime management
31434 + */
31435 +
31436 +#ifndef __SYSAUFS_H__
31437 +#define __SYSAUFS_H__
31438 +
31439 +#ifdef __KERNEL__
31440 +
31441 +#include <linux/sysfs.h>
31442 +#include "module.h"
31443 +
31444 +struct super_block;
31445 +struct au_sbinfo;
31446 +
31447 +struct sysaufs_si_attr {
31448 +       struct attribute attr;
31449 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31450 +};
31451 +
31452 +/* ---------------------------------------------------------------------- */
31453 +
31454 +/* sysaufs.c */
31455 +extern unsigned long sysaufs_si_mask;
31456 +extern struct kset *sysaufs_kset;
31457 +extern struct attribute *sysaufs_si_attrs[];
31458 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31459 +int __init sysaufs_init(void);
31460 +void sysaufs_fin(void);
31461 +
31462 +/* ---------------------------------------------------------------------- */
31463 +
31464 +/* some people doesn't like to show a pointer in kernel */
31465 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31466 +{
31467 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31468 +}
31469 +
31470 +#define SysaufsSiNamePrefix    "si_"
31471 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31472 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31473 +{
31474 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31475 +                sysaufs_si_id(sbinfo));
31476 +}
31477 +
31478 +struct au_branch;
31479 +#ifdef CONFIG_SYSFS
31480 +/* sysfs.c */
31481 +extern struct attribute_group *sysaufs_attr_group;
31482 +
31483 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31484 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31485 +                        char *buf);
31486 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31487 +#ifdef CONFIG_COMPAT
31488 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31489 +#endif
31490 +
31491 +void sysaufs_br_init(struct au_branch *br);
31492 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31493 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31494 +
31495 +#define sysaufs_brs_init()     do {} while (0)
31496 +
31497 +#else
31498 +#define sysaufs_attr_group     NULL
31499 +
31500 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31501 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31502 +       struct attribute *attr, char *buf)
31503 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31504 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31505 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31506 +
31507 +static inline void sysaufs_brs_init(void)
31508 +{
31509 +       sysaufs_brs = 0;
31510 +}
31511 +
31512 +#endif /* CONFIG_SYSFS */
31513 +
31514 +#endif /* __KERNEL__ */
31515 +#endif /* __SYSAUFS_H__ */
31516 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31517 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31518 +++ linux/fs/aufs/sysfs.c       2022-11-05 23:02:18.969222617 +0100
31519 @@ -0,0 +1,374 @@
31520 +// SPDX-License-Identifier: GPL-2.0
31521 +/*
31522 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31523 + *
31524 + * This program is free software; you can redistribute it and/or modify
31525 + * it under the terms of the GNU General Public License as published by
31526 + * the Free Software Foundation; either version 2 of the License, or
31527 + * (at your option) any later version.
31528 + *
31529 + * This program is distributed in the hope that it will be useful,
31530 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31531 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31532 + * GNU General Public License for more details.
31533 + *
31534 + * You should have received a copy of the GNU General Public License
31535 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31536 + */
31537 +
31538 +/*
31539 + * sysfs interface
31540 + */
31541 +
31542 +#include <linux/compat.h>
31543 +#include <linux/seq_file.h>
31544 +#include "aufs.h"
31545 +
31546 +#ifdef CONFIG_AUFS_FS_MODULE
31547 +/* this entry violates the "one line per file" policy of sysfs */
31548 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31549 +                          char *buf)
31550 +{
31551 +       ssize_t err;
31552 +       static char *conf =
31553 +/* this file is generated at compiling */
31554 +#include "conf.str"
31555 +               ;
31556 +
31557 +       err = snprintf(buf, PAGE_SIZE, conf);
31558 +       if (unlikely(err >= PAGE_SIZE))
31559 +               err = -EFBIG;
31560 +       return err;
31561 +}
31562 +
31563 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31564 +#endif
31565 +
31566 +static struct attribute *au_attr[] = {
31567 +#ifdef CONFIG_AUFS_FS_MODULE
31568 +       &au_config_attr.attr,
31569 +#endif
31570 +       NULL,   /* need to NULL terminate the list of attributes */
31571 +};
31572 +
31573 +static struct attribute_group sysaufs_attr_group_body = {
31574 +       .attrs = au_attr
31575 +};
31576 +
31577 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31578 +
31579 +/* ---------------------------------------------------------------------- */
31580 +
31581 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31582 +{
31583 +       int err;
31584 +
31585 +       SiMustAnyLock(sb);
31586 +
31587 +       err = 0;
31588 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31589 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31590 +               seq_putc(seq, '\n');
31591 +       }
31592 +       return err;
31593 +}
31594 +
31595 +/*
31596 + * the lifetime of branch is independent from the entry under sysfs.
31597 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31598 + * unlinked.
31599 + */
31600 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31601 +                        aufs_bindex_t bindex, int idx)
31602 +{
31603 +       int err;
31604 +       struct path path;
31605 +       struct dentry *root;
31606 +       struct au_branch *br;
31607 +       au_br_perm_str_t perm;
31608 +
31609 +       AuDbg("b%d\n", bindex);
31610 +
31611 +       err = 0;
31612 +       root = sb->s_root;
31613 +       di_read_lock_parent(root, !AuLock_IR);
31614 +       br = au_sbr(sb, bindex);
31615 +
31616 +       switch (idx) {
31617 +       case AuBrSysfs_BR:
31618 +               path.mnt = au_br_mnt(br);
31619 +               path.dentry = au_h_dptr(root, bindex);
31620 +               err = au_seq_path(seq, &path);
31621 +               if (!err) {
31622 +                       au_optstr_br_perm(&perm, br->br_perm);
31623 +                       seq_printf(seq, "=%s\n", perm.a);
31624 +               }
31625 +               break;
31626 +       case AuBrSysfs_BRID:
31627 +               seq_printf(seq, "%d\n", br->br_id);
31628 +               break;
31629 +       }
31630 +       di_read_unlock(root, !AuLock_IR);
31631 +       if (unlikely(err || seq_has_overflowed(seq)))
31632 +               err = -E2BIG;
31633 +
31634 +       return err;
31635 +}
31636 +
31637 +/* ---------------------------------------------------------------------- */
31638 +
31639 +static struct seq_file *au_seq(char *p, ssize_t len)
31640 +{
31641 +       struct seq_file *seq;
31642 +
31643 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31644 +       if (seq) {
31645 +               /* mutex_init(&seq.lock); */
31646 +               seq->buf = p;
31647 +               seq->size = len;
31648 +               return seq; /* success */
31649 +       }
31650 +
31651 +       seq = ERR_PTR(-ENOMEM);
31652 +       return seq;
31653 +}
31654 +
31655 +#define SysaufsBr_PREFIX       "br"
31656 +#define SysaufsBrid_PREFIX     "brid"
31657 +
31658 +/* todo: file size may exceed PAGE_SIZE */
31659 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31660 +                       char *buf)
31661 +{
31662 +       ssize_t err;
31663 +       int idx;
31664 +       long l;
31665 +       aufs_bindex_t bbot;
31666 +       struct au_sbinfo *sbinfo;
31667 +       struct super_block *sb;
31668 +       struct seq_file *seq;
31669 +       char *name;
31670 +       struct attribute **cattr;
31671 +
31672 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31673 +       sb = sbinfo->si_sb;
31674 +
31675 +       /*
31676 +        * prevent a race condition between sysfs and aufs.
31677 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31678 +        * prohibits maintaining the sysfs entries.
31679 +        * hew we acquire read lock after sysfs_get_active_two().
31680 +        * on the other hand, the remount process may maintain the sysfs/aufs
31681 +        * entries after acquiring write lock.
31682 +        * it can cause a deadlock.
31683 +        * simply we gave up processing read here.
31684 +        */
31685 +       err = -EBUSY;
31686 +       if (unlikely(!si_noflush_read_trylock(sb)))
31687 +               goto out;
31688 +
31689 +       seq = au_seq(buf, PAGE_SIZE);
31690 +       err = PTR_ERR(seq);
31691 +       if (IS_ERR(seq))
31692 +               goto out_unlock;
31693 +
31694 +       name = (void *)attr->name;
31695 +       cattr = sysaufs_si_attrs;
31696 +       while (*cattr) {
31697 +               if (!strcmp(name, (*cattr)->name)) {
31698 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31699 +                               ->show(seq, sb);
31700 +                       goto out_seq;
31701 +               }
31702 +               cattr++;
31703 +       }
31704 +
31705 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31706 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31707 +               idx = AuBrSysfs_BRID;
31708 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31709 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31710 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31711 +               idx = AuBrSysfs_BR;
31712 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31713 +       } else
31714 +                 BUG();
31715 +
31716 +       err = kstrtol(name, 10, &l);
31717 +       if (!err) {
31718 +               bbot = au_sbbot(sb);
31719 +               if (l <= bbot)
31720 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31721 +               else
31722 +                       err = -ENOENT;
31723 +       }
31724 +
31725 +out_seq:
31726 +       if (!err) {
31727 +               err = seq->count;
31728 +               /* sysfs limit */
31729 +               if (unlikely(err == PAGE_SIZE))
31730 +                       err = -EFBIG;
31731 +       }
31732 +       au_kfree_rcu(seq);
31733 +out_unlock:
31734 +       si_read_unlock(sb);
31735 +out:
31736 +       return err;
31737 +}
31738 +
31739 +/* ---------------------------------------------------------------------- */
31740 +
31741 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31742 +{
31743 +       int err;
31744 +       int16_t brid;
31745 +       aufs_bindex_t bindex, bbot;
31746 +       size_t sz;
31747 +       char *buf;
31748 +       struct seq_file *seq;
31749 +       struct au_branch *br;
31750 +
31751 +       si_read_lock(sb, AuLock_FLUSH);
31752 +       bbot = au_sbbot(sb);
31753 +       err = bbot + 1;
31754 +       if (!arg)
31755 +               goto out;
31756 +
31757 +       err = -ENOMEM;
31758 +       buf = (void *)__get_free_page(GFP_NOFS);
31759 +       if (unlikely(!buf))
31760 +               goto out;
31761 +
31762 +       seq = au_seq(buf, PAGE_SIZE);
31763 +       err = PTR_ERR(seq);
31764 +       if (IS_ERR(seq))
31765 +               goto out_buf;
31766 +
31767 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31768 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31769 +               /* VERIFY_WRITE */
31770 +               err = !access_ok(arg, sizeof(*arg));
31771 +               if (unlikely(err))
31772 +                       break;
31773 +
31774 +               br = au_sbr(sb, bindex);
31775 +               brid = br->br_id;
31776 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31777 +               err = __put_user(brid, &arg->id);
31778 +               if (unlikely(err))
31779 +                       break;
31780 +
31781 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31782 +               err = __put_user(br->br_perm, &arg->perm);
31783 +               if (unlikely(err))
31784 +                       break;
31785 +
31786 +               err = au_seq_path(seq, &br->br_path);
31787 +               if (unlikely(err))
31788 +                       break;
31789 +               seq_putc(seq, '\0');
31790 +               if (!seq_has_overflowed(seq)) {
31791 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31792 +                       seq->count = 0;
31793 +                       if (unlikely(err))
31794 +                               break;
31795 +               } else {
31796 +                       err = -E2BIG;
31797 +                       goto out_seq;
31798 +               }
31799 +       }
31800 +       if (unlikely(err))
31801 +               err = -EFAULT;
31802 +
31803 +out_seq:
31804 +       au_kfree_rcu(seq);
31805 +out_buf:
31806 +       free_page((unsigned long)buf);
31807 +out:
31808 +       si_read_unlock(sb);
31809 +       return err;
31810 +}
31811 +
31812 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31813 +{
31814 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31815 +}
31816 +
31817 +#ifdef CONFIG_COMPAT
31818 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31819 +{
31820 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31821 +}
31822 +#endif
31823 +
31824 +/* ---------------------------------------------------------------------- */
31825 +
31826 +void sysaufs_br_init(struct au_branch *br)
31827 +{
31828 +       int i;
31829 +       struct au_brsysfs *br_sysfs;
31830 +       struct attribute *attr;
31831 +
31832 +       br_sysfs = br->br_sysfs;
31833 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31834 +               attr = &br_sysfs->attr;
31835 +               sysfs_attr_init(attr);
31836 +               attr->name = br_sysfs->name;
31837 +               attr->mode = 0444;
31838 +               br_sysfs++;
31839 +       }
31840 +}
31841 +
31842 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31843 +{
31844 +       struct au_branch *br;
31845 +       struct kobject *kobj;
31846 +       struct au_brsysfs *br_sysfs;
31847 +       int i;
31848 +       aufs_bindex_t bbot;
31849 +
31850 +       if (!sysaufs_brs)
31851 +               return;
31852 +
31853 +       kobj = &au_sbi(sb)->si_kobj;
31854 +       bbot = au_sbbot(sb);
31855 +       for (; bindex <= bbot; bindex++) {
31856 +               br = au_sbr(sb, bindex);
31857 +               br_sysfs = br->br_sysfs;
31858 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31859 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31860 +                       br_sysfs++;
31861 +               }
31862 +       }
31863 +}
31864 +
31865 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31866 +{
31867 +       int err, i;
31868 +       aufs_bindex_t bbot;
31869 +       struct kobject *kobj;
31870 +       struct au_branch *br;
31871 +       struct au_brsysfs *br_sysfs;
31872 +
31873 +       if (!sysaufs_brs)
31874 +               return;
31875 +
31876 +       kobj = &au_sbi(sb)->si_kobj;
31877 +       bbot = au_sbbot(sb);
31878 +       for (; bindex <= bbot; bindex++) {
31879 +               br = au_sbr(sb, bindex);
31880 +               br_sysfs = br->br_sysfs;
31881 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31882 +                        SysaufsBr_PREFIX "%d", bindex);
31883 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31884 +                        SysaufsBrid_PREFIX "%d", bindex);
31885 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31886 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31887 +                       if (unlikely(err))
31888 +                               pr_warn("failed %s under sysfs(%d)\n",
31889 +                                       br_sysfs->name, err);
31890 +                       br_sysfs++;
31891 +               }
31892 +       }
31893 +}
31894 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31895 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31896 +++ linux/fs/aufs/sysrq.c       2022-11-05 23:02:18.969222617 +0100
31897 @@ -0,0 +1,149 @@
31898 +// SPDX-License-Identifier: GPL-2.0
31899 +/*
31900 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31901 + *
31902 + * This program is free software; you can redistribute it and/or modify
31903 + * it under the terms of the GNU General Public License as published by
31904 + * the Free Software Foundation; either version 2 of the License, or
31905 + * (at your option) any later version.
31906 + *
31907 + * This program is distributed in the hope that it will be useful,
31908 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31909 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31910 + * GNU General Public License for more details.
31911 + *
31912 + * You should have received a copy of the GNU General Public License
31913 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31914 + */
31915 +
31916 +/*
31917 + * magic sysrq handler
31918 + */
31919 +
31920 +/* #include <linux/sysrq.h> */
31921 +#include <linux/writeback.h>
31922 +#include "aufs.h"
31923 +
31924 +/* ---------------------------------------------------------------------- */
31925 +
31926 +static void sysrq_sb(struct super_block *sb)
31927 +{
31928 +       char *plevel;
31929 +       struct au_sbinfo *sbinfo;
31930 +       struct file *file;
31931 +       struct hlist_bl_head *files;
31932 +       struct hlist_bl_node *pos;
31933 +       struct au_finfo *finfo;
31934 +       struct inode *i;
31935 +
31936 +       plevel = au_plevel;
31937 +       au_plevel = KERN_WARNING;
31938 +
31939 +       /* since we define pr_fmt, call printk directly */
31940 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31941 +
31942 +       sbinfo = au_sbi(sb);
31943 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31944 +       pr("superblock\n");
31945 +       au_dpri_sb(sb);
31946 +
31947 +#if 0 /* reserved */
31948 +       do {
31949 +               int err, i, j, ndentry;
31950 +               struct au_dcsub_pages dpages;
31951 +               struct au_dpage *dpage;
31952 +
31953 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31954 +               if (unlikely(err))
31955 +                       break;
31956 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31957 +               if (!err)
31958 +                       for (i = 0; i < dpages.ndpage; i++) {
31959 +                               dpage = dpages.dpages + i;
31960 +                               ndentry = dpage->ndentry;
31961 +                               for (j = 0; j < ndentry; j++)
31962 +                                       au_dpri_dentry(dpage->dentries[j]);
31963 +                       }
31964 +               au_dpages_free(&dpages);
31965 +       } while (0);
31966 +#endif
31967 +
31968 +       pr("isolated inode\n");
31969 +       spin_lock(&sb->s_inode_list_lock);
31970 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31971 +               spin_lock(&i->i_lock);
31972 +               if (hlist_empty(&i->i_dentry))
31973 +                       au_dpri_inode(i);
31974 +               spin_unlock(&i->i_lock);
31975 +       }
31976 +       spin_unlock(&sb->s_inode_list_lock);
31977 +
31978 +       pr("files\n");
31979 +       files = &au_sbi(sb)->si_files;
31980 +       hlist_bl_lock(files);
31981 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31982 +               umode_t mode;
31983 +
31984 +               file = finfo->fi_file;
31985 +               mode = file_inode(file)->i_mode;
31986 +               if (!special_file(mode))
31987 +                       au_dpri_file(file);
31988 +       }
31989 +       hlist_bl_unlock(files);
31990 +       pr("done\n");
31991 +
31992 +#undef pr
31993 +       au_plevel = plevel;
31994 +}
31995 +
31996 +/* ---------------------------------------------------------------------- */
31997 +
31998 +/* module parameter */
31999 +static char *aufs_sysrq_key = "a";
32000 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
32001 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
32002 +
32003 +static void au_sysrq(int key __maybe_unused)
32004 +{
32005 +       struct au_sbinfo *sbinfo;
32006 +       struct hlist_bl_node *pos;
32007 +
32008 +       lockdep_off();
32009 +       au_sbilist_lock();
32010 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
32011 +               sysrq_sb(sbinfo->si_sb);
32012 +       au_sbilist_unlock();
32013 +       lockdep_on();
32014 +}
32015 +
32016 +static struct sysrq_key_op au_sysrq_op = {
32017 +       .handler        = au_sysrq,
32018 +       .help_msg       = "Aufs",
32019 +       .action_msg     = "Aufs",
32020 +       .enable_mask    = SYSRQ_ENABLE_DUMP
32021 +};
32022 +
32023 +/* ---------------------------------------------------------------------- */
32024 +
32025 +int __init au_sysrq_init(void)
32026 +{
32027 +       int err;
32028 +       char key;
32029 +
32030 +       err = -1;
32031 +       key = *aufs_sysrq_key;
32032 +       if ('a' <= key && key <= 'z')
32033 +               err = register_sysrq_key(key, &au_sysrq_op);
32034 +       if (unlikely(err))
32035 +               pr_err("err %d, sysrq=%c\n", err, key);
32036 +       return err;
32037 +}
32038 +
32039 +void au_sysrq_fin(void)
32040 +{
32041 +       int err;
32042 +
32043 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
32044 +       if (unlikely(err))
32045 +               pr_err("err %d (ignored)\n", err);
32046 +}
32047 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
32048 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
32049 +++ linux/fs/aufs/vdir.c        2022-12-17 09:21:34.799855195 +0100
32050 @@ -0,0 +1,896 @@
32051 +// SPDX-License-Identifier: GPL-2.0
32052 +/*
32053 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32054 + *
32055 + * This program is free software; you can redistribute it and/or modify
32056 + * it under the terms of the GNU General Public License as published by
32057 + * the Free Software Foundation; either version 2 of the License, or
32058 + * (at your option) any later version.
32059 + *
32060 + * This program is distributed in the hope that it will be useful,
32061 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32062 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32063 + * GNU General Public License for more details.
32064 + *
32065 + * You should have received a copy of the GNU General Public License
32066 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32067 + */
32068 +
32069 +/*
32070 + * virtual or vertical directory
32071 + */
32072 +
32073 +#include <linux/iversion.h>
32074 +#include "aufs.h"
32075 +
32076 +static unsigned int calc_size(int nlen)
32077 +{
32078 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
32079 +}
32080 +
32081 +static int set_deblk_end(union au_vdir_deblk_p *p,
32082 +                        union au_vdir_deblk_p *deblk_end)
32083 +{
32084 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32085 +               p->de->de_str.len = 0;
32086 +               /* smp_mb(); */
32087 +               return 0;
32088 +       }
32089 +       return -1; /* error */
32090 +}
32091 +
32092 +/* returns true or false */
32093 +static int is_deblk_end(union au_vdir_deblk_p *p,
32094 +                       union au_vdir_deblk_p *deblk_end)
32095 +{
32096 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
32097 +               return !p->de->de_str.len;
32098 +       return 1;
32099 +}
32100 +
32101 +static unsigned char *last_deblk(struct au_vdir *vdir)
32102 +{
32103 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
32104 +}
32105 +
32106 +/* ---------------------------------------------------------------------- */
32107 +
32108 +/* estimate the appropriate size for name hash table */
32109 +unsigned int au_rdhash_est(loff_t sz)
32110 +{
32111 +       unsigned int n;
32112 +
32113 +       n = UINT_MAX;
32114 +       sz >>= 10;
32115 +       if (sz < n)
32116 +               n = sz;
32117 +       if (sz < AUFS_RDHASH_DEF)
32118 +               n = AUFS_RDHASH_DEF;
32119 +       /* pr_info("n %u\n", n); */
32120 +       return n;
32121 +}
32122 +
32123 +/*
32124 + * the allocated memory has to be freed by
32125 + * au_nhash_wh_free() or au_nhash_de_free().
32126 + */
32127 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32128 +{
32129 +       struct hlist_head *head;
32130 +       unsigned int u;
32131 +       size_t sz;
32132 +
32133 +       sz = sizeof(*nhash->nh_head) * num_hash;
32134 +       head = kmalloc(sz, gfp);
32135 +       if (head) {
32136 +               nhash->nh_num = num_hash;
32137 +               nhash->nh_head = head;
32138 +               for (u = 0; u < num_hash; u++)
32139 +                       INIT_HLIST_HEAD(head++);
32140 +               return 0; /* success */
32141 +       }
32142 +
32143 +       return -ENOMEM;
32144 +}
32145 +
32146 +static void nhash_count(struct hlist_head *head)
32147 +{
32148 +#if 0 /* debugging */
32149 +       unsigned long n;
32150 +       struct hlist_node *pos;
32151 +
32152 +       n = 0;
32153 +       hlist_for_each(pos, head)
32154 +               n++;
32155 +       pr_info("%lu\n", n);
32156 +#endif
32157 +}
32158 +
32159 +static void au_nhash_wh_do_free(struct hlist_head *head)
32160 +{
32161 +       struct au_vdir_wh *pos;
32162 +       struct hlist_node *node;
32163 +
32164 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32165 +               au_kfree_rcu(pos);
32166 +}
32167 +
32168 +static void au_nhash_de_do_free(struct hlist_head *head)
32169 +{
32170 +       struct au_vdir_dehstr *pos;
32171 +       struct hlist_node *node;
32172 +
32173 +       hlist_for_each_entry_safe(pos, node, head, hash)
32174 +               au_cache_free_vdir_dehstr(pos);
32175 +}
32176 +
32177 +static void au_nhash_do_free(struct au_nhash *nhash,
32178 +                            void (*free)(struct hlist_head *head))
32179 +{
32180 +       unsigned int n;
32181 +       struct hlist_head *head;
32182 +
32183 +       n = nhash->nh_num;
32184 +       if (!n)
32185 +               return;
32186 +
32187 +       head = nhash->nh_head;
32188 +       while (n-- > 0) {
32189 +               nhash_count(head);
32190 +               free(head++);
32191 +       }
32192 +       au_kfree_try_rcu(nhash->nh_head);
32193 +}
32194 +
32195 +void au_nhash_wh_free(struct au_nhash *whlist)
32196 +{
32197 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32198 +}
32199 +
32200 +static void au_nhash_de_free(struct au_nhash *delist)
32201 +{
32202 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32203 +}
32204 +
32205 +/* ---------------------------------------------------------------------- */
32206 +
32207 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32208 +                           int limit)
32209 +{
32210 +       int num;
32211 +       unsigned int u, n;
32212 +       struct hlist_head *head;
32213 +       struct au_vdir_wh *pos;
32214 +
32215 +       num = 0;
32216 +       n = whlist->nh_num;
32217 +       head = whlist->nh_head;
32218 +       for (u = 0; u < n; u++, head++)
32219 +               hlist_for_each_entry(pos, head, wh_hash)
32220 +                       if (pos->wh_bindex == btgt && ++num > limit)
32221 +                               return 1;
32222 +       return 0;
32223 +}
32224 +
32225 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32226 +                                      unsigned char *name,
32227 +                                      unsigned int len)
32228 +{
32229 +       unsigned int v;
32230 +       /* const unsigned int magic_bit = 12; */
32231 +
32232 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32233 +
32234 +       v = 0;
32235 +       if (len > 8)
32236 +               len = 8;
32237 +       while (len--)
32238 +               v += *name++;
32239 +       /* v = hash_long(v, magic_bit); */
32240 +       v %= nhash->nh_num;
32241 +       return nhash->nh_head + v;
32242 +}
32243 +
32244 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32245 +                             int nlen)
32246 +{
32247 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32248 +}
32249 +
32250 +/* returns found or not */
32251 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32252 +{
32253 +       struct hlist_head *head;
32254 +       struct au_vdir_wh *pos;
32255 +       struct au_vdir_destr *str;
32256 +
32257 +       head = au_name_hash(whlist, name, nlen);
32258 +       hlist_for_each_entry(pos, head, wh_hash) {
32259 +               str = &pos->wh_str;
32260 +               AuDbg("%.*s\n", str->len, str->name);
32261 +               if (au_nhash_test_name(str, name, nlen))
32262 +                       return 1;
32263 +       }
32264 +       return 0;
32265 +}
32266 +
32267 +/* returns found(true) or not */
32268 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32269 +{
32270 +       struct hlist_head *head;
32271 +       struct au_vdir_dehstr *pos;
32272 +       struct au_vdir_destr *str;
32273 +
32274 +       head = au_name_hash(delist, name, nlen);
32275 +       hlist_for_each_entry(pos, head, hash) {
32276 +               str = pos->str;
32277 +               AuDbg("%.*s\n", str->len, str->name);
32278 +               if (au_nhash_test_name(str, name, nlen))
32279 +                       return 1;
32280 +       }
32281 +       return 0;
32282 +}
32283 +
32284 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32285 +                           unsigned char d_type)
32286 +{
32287 +#ifdef CONFIG_AUFS_SHWH
32288 +       wh->wh_ino = ino;
32289 +       wh->wh_type = d_type;
32290 +#endif
32291 +}
32292 +
32293 +/* ---------------------------------------------------------------------- */
32294 +
32295 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32296 +                      unsigned int d_type, aufs_bindex_t bindex,
32297 +                      unsigned char shwh)
32298 +{
32299 +       int err;
32300 +       struct au_vdir_destr *str;
32301 +       struct au_vdir_wh *wh;
32302 +
32303 +       AuDbg("%.*s\n", nlen, name);
32304 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32305 +
32306 +       err = -ENOMEM;
32307 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32308 +       if (unlikely(!wh))
32309 +               goto out;
32310 +
32311 +       err = 0;
32312 +       wh->wh_bindex = bindex;
32313 +       if (shwh)
32314 +               au_shwh_init_wh(wh, ino, d_type);
32315 +       str = &wh->wh_str;
32316 +       str->len = nlen;
32317 +       memcpy(str->name, name, nlen);
32318 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32319 +       /* smp_mb(); */
32320 +
32321 +out:
32322 +       return err;
32323 +}
32324 +
32325 +static int append_deblk(struct au_vdir *vdir)
32326 +{
32327 +       int err;
32328 +       unsigned long ul;
32329 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32330 +       union au_vdir_deblk_p p, deblk_end;
32331 +       unsigned char **o;
32332 +
32333 +       err = -ENOMEM;
32334 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32335 +                       GFP_NOFS, /*may_shrink*/0);
32336 +       if (unlikely(!o))
32337 +               goto out;
32338 +
32339 +       vdir->vd_deblk = o;
32340 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32341 +       if (p.deblk) {
32342 +               ul = vdir->vd_nblk++;
32343 +               vdir->vd_deblk[ul] = p.deblk;
32344 +               vdir->vd_last.ul = ul;
32345 +               vdir->vd_last.p.deblk = p.deblk;
32346 +               deblk_end.deblk = p.deblk + deblk_sz;
32347 +               err = set_deblk_end(&p, &deblk_end);
32348 +       }
32349 +
32350 +out:
32351 +       return err;
32352 +}
32353 +
32354 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32355 +                    unsigned int d_type, struct au_nhash *delist)
32356 +{
32357 +       int err;
32358 +       unsigned int sz;
32359 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32360 +       union au_vdir_deblk_p p, *room, deblk_end;
32361 +       struct au_vdir_dehstr *dehstr;
32362 +
32363 +       p.deblk = last_deblk(vdir);
32364 +       deblk_end.deblk = p.deblk + deblk_sz;
32365 +       room = &vdir->vd_last.p;
32366 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32367 +                 || !is_deblk_end(room, &deblk_end));
32368 +
32369 +       sz = calc_size(nlen);
32370 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32371 +               err = append_deblk(vdir);
32372 +               if (unlikely(err))
32373 +                       goto out;
32374 +
32375 +               p.deblk = last_deblk(vdir);
32376 +               deblk_end.deblk = p.deblk + deblk_sz;
32377 +               /* smp_mb(); */
32378 +               AuDebugOn(room->deblk != p.deblk);
32379 +       }
32380 +
32381 +       err = -ENOMEM;
32382 +       dehstr = au_cache_alloc_vdir_dehstr();
32383 +       if (unlikely(!dehstr))
32384 +               goto out;
32385 +
32386 +       dehstr->str = &room->de->de_str;
32387 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32388 +       room->de->de_ino = ino;
32389 +       room->de->de_type = d_type;
32390 +       room->de->de_str.len = nlen;
32391 +       memcpy(room->de->de_str.name, name, nlen);
32392 +
32393 +       err = 0;
32394 +       room->deblk += sz;
32395 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32396 +               err = append_deblk(vdir);
32397 +       /* smp_mb(); */
32398 +
32399 +out:
32400 +       return err;
32401 +}
32402 +
32403 +/* ---------------------------------------------------------------------- */
32404 +
32405 +void au_vdir_free(struct au_vdir *vdir)
32406 +{
32407 +       unsigned char **deblk;
32408 +
32409 +       deblk = vdir->vd_deblk;
32410 +       while (vdir->vd_nblk--)
32411 +               au_kfree_try_rcu(*deblk++);
32412 +       au_kfree_try_rcu(vdir->vd_deblk);
32413 +       au_cache_free_vdir(vdir);
32414 +}
32415 +
32416 +static struct au_vdir *alloc_vdir(struct file *file)
32417 +{
32418 +       struct au_vdir *vdir;
32419 +       struct super_block *sb;
32420 +       int err;
32421 +
32422 +       sb = file->f_path.dentry->d_sb;
32423 +       SiMustAnyLock(sb);
32424 +
32425 +       err = -ENOMEM;
32426 +       vdir = au_cache_alloc_vdir();
32427 +       if (unlikely(!vdir))
32428 +               goto out;
32429 +
32430 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32431 +       if (unlikely(!vdir->vd_deblk))
32432 +               goto out_free;
32433 +
32434 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32435 +       if (!vdir->vd_deblk_sz) {
32436 +               /* estimate the appropriate size for deblk */
32437 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32438 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32439 +       }
32440 +       vdir->vd_nblk = 0;
32441 +       vdir->vd_version = 0;
32442 +       vdir->vd_jiffy = 0;
32443 +       err = append_deblk(vdir);
32444 +       if (!err)
32445 +               return vdir; /* success */
32446 +
32447 +       au_kfree_try_rcu(vdir->vd_deblk);
32448 +
32449 +out_free:
32450 +       au_cache_free_vdir(vdir);
32451 +out:
32452 +       vdir = ERR_PTR(err);
32453 +       return vdir;
32454 +}
32455 +
32456 +static int reinit_vdir(struct au_vdir *vdir)
32457 +{
32458 +       int err;
32459 +       union au_vdir_deblk_p p, deblk_end;
32460 +
32461 +       while (vdir->vd_nblk > 1) {
32462 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32463 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32464 +               vdir->vd_nblk--;
32465 +       }
32466 +       p.deblk = vdir->vd_deblk[0];
32467 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32468 +       err = set_deblk_end(&p, &deblk_end);
32469 +       /* keep vd_dblk_sz */
32470 +       vdir->vd_last.ul = 0;
32471 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32472 +       vdir->vd_version = 0;
32473 +       vdir->vd_jiffy = 0;
32474 +       /* smp_mb(); */
32475 +       return err;
32476 +}
32477 +
32478 +/* ---------------------------------------------------------------------- */
32479 +
32480 +#define AuFillVdir_CALLED      1
32481 +#define AuFillVdir_WHABLE      (1 << 1)
32482 +#define AuFillVdir_SHWH                (1 << 2)
32483 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32484 +#define au_fset_fillvdir(flags, name) \
32485 +       do { (flags) |= AuFillVdir_##name; } while (0)
32486 +#define au_fclr_fillvdir(flags, name) \
32487 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32488 +
32489 +#ifndef CONFIG_AUFS_SHWH
32490 +#undef AuFillVdir_SHWH
32491 +#define AuFillVdir_SHWH                0
32492 +#endif
32493 +
32494 +struct fillvdir_arg {
32495 +       struct dir_context      ctx;
32496 +       struct file             *file;
32497 +       struct au_vdir          *vdir;
32498 +       struct au_nhash         delist;
32499 +       struct au_nhash         whlist;
32500 +       aufs_bindex_t           bindex;
32501 +       unsigned int            flags;
32502 +       int                     err;
32503 +};
32504 +
32505 +static bool fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32506 +                   loff_t offset __maybe_unused, u64 h_ino,
32507 +                   unsigned int d_type)
32508 +{
32509 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32510 +       char *name = (void *)__name;
32511 +       struct super_block *sb;
32512 +       ino_t ino;
32513 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32514 +
32515 +       arg->err = 0;
32516 +       sb = arg->file->f_path.dentry->d_sb;
32517 +       au_fset_fillvdir(arg->flags, CALLED);
32518 +       /* smp_mb(); */
32519 +       if (nlen <= AUFS_WH_PFX_LEN
32520 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32521 +               if (test_known(&arg->delist, name, nlen)
32522 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32523 +                       goto out; /* already exists or whiteouted */
32524 +
32525 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32526 +               if (!arg->err) {
32527 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32528 +                               d_type = DT_UNKNOWN;
32529 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32530 +                                            d_type, &arg->delist);
32531 +               }
32532 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32533 +               name += AUFS_WH_PFX_LEN;
32534 +               nlen -= AUFS_WH_PFX_LEN;
32535 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32536 +                       goto out; /* already whiteouted */
32537 +
32538 +               ino = 0; /* just to suppress a warning */
32539 +               if (shwh)
32540 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32541 +                                            &ino);
32542 +               if (!arg->err) {
32543 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32544 +                               d_type = DT_UNKNOWN;
32545 +                       arg->err = au_nhash_append_wh
32546 +                               (&arg->whlist, name, nlen, ino, d_type,
32547 +                                arg->bindex, shwh);
32548 +               }
32549 +       }
32550 +
32551 +out:
32552 +       if (!arg->err)
32553 +               arg->vdir->vd_jiffy = jiffies;
32554 +       /* smp_mb(); */
32555 +       AuTraceErr(arg->err);
32556 +       return !arg->err;
32557 +}
32558 +
32559 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32560 +                         struct au_nhash *whlist, struct au_nhash *delist)
32561 +{
32562 +#ifdef CONFIG_AUFS_SHWH
32563 +       int err;
32564 +       unsigned int nh, u;
32565 +       struct hlist_head *head;
32566 +       struct au_vdir_wh *pos;
32567 +       struct hlist_node *n;
32568 +       char *p, *o;
32569 +       struct au_vdir_destr *destr;
32570 +
32571 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32572 +
32573 +       err = -ENOMEM;
32574 +       o = p = (void *)__get_free_page(GFP_NOFS);
32575 +       if (unlikely(!p))
32576 +               goto out;
32577 +
32578 +       err = 0;
32579 +       nh = whlist->nh_num;
32580 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32581 +       p += AUFS_WH_PFX_LEN;
32582 +       for (u = 0; u < nh; u++) {
32583 +               head = whlist->nh_head + u;
32584 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32585 +                       destr = &pos->wh_str;
32586 +                       memcpy(p, destr->name, destr->len);
32587 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32588 +                                       pos->wh_ino, pos->wh_type, delist);
32589 +                       if (unlikely(err))
32590 +                               break;
32591 +               }
32592 +       }
32593 +
32594 +       free_page((unsigned long)o);
32595 +
32596 +out:
32597 +       AuTraceErr(err);
32598 +       return err;
32599 +#else
32600 +       return 0;
32601 +#endif
32602 +}
32603 +
32604 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32605 +{
32606 +       int err;
32607 +       unsigned int rdhash;
32608 +       loff_t offset;
32609 +       aufs_bindex_t bbot, bindex, btop;
32610 +       unsigned char shwh;
32611 +       struct file *hf, *file;
32612 +       struct super_block *sb;
32613 +
32614 +       file = arg->file;
32615 +       sb = file->f_path.dentry->d_sb;
32616 +       SiMustAnyLock(sb);
32617 +
32618 +       rdhash = au_sbi(sb)->si_rdhash;
32619 +       if (!rdhash)
32620 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32621 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32622 +       if (unlikely(err))
32623 +               goto out;
32624 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32625 +       if (unlikely(err))
32626 +               goto out_delist;
32627 +
32628 +       err = 0;
32629 +       arg->flags = 0;
32630 +       shwh = 0;
32631 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32632 +               shwh = 1;
32633 +               au_fset_fillvdir(arg->flags, SHWH);
32634 +       }
32635 +       btop = au_fbtop(file);
32636 +       bbot = au_fbbot_dir(file);
32637 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32638 +               hf = au_hf_dir(file, bindex);
32639 +               if (!hf)
32640 +                       continue;
32641 +
32642 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32643 +               err = offset;
32644 +               if (unlikely(offset))
32645 +                       break;
32646 +
32647 +               arg->bindex = bindex;
32648 +               au_fclr_fillvdir(arg->flags, WHABLE);
32649 +               if (shwh
32650 +                   || (bindex != bbot
32651 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32652 +                       au_fset_fillvdir(arg->flags, WHABLE);
32653 +               do {
32654 +                       arg->err = 0;
32655 +                       au_fclr_fillvdir(arg->flags, CALLED);
32656 +                       /* smp_mb(); */
32657 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32658 +                       if (err >= 0)
32659 +                               err = arg->err;
32660 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32661 +
32662 +               /*
32663 +                * dir_relax() may be good for concurrency, but aufs should not
32664 +                * use it since it will cause a lockdep problem.
32665 +                */
32666 +       }
32667 +
32668 +       if (!err && shwh)
32669 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32670 +
32671 +       au_nhash_wh_free(&arg->whlist);
32672 +
32673 +out_delist:
32674 +       au_nhash_de_free(&arg->delist);
32675 +out:
32676 +       return err;
32677 +}
32678 +
32679 +static int read_vdir(struct file *file, int may_read)
32680 +{
32681 +       int err;
32682 +       unsigned long expire;
32683 +       unsigned char do_read;
32684 +       struct fillvdir_arg arg = {
32685 +               .ctx = {
32686 +                       .actor = fillvdir
32687 +               }
32688 +       };
32689 +       struct inode *inode;
32690 +       struct au_vdir *vdir, *allocated;
32691 +
32692 +       err = 0;
32693 +       inode = file_inode(file);
32694 +       IMustLock(inode);
32695 +       IiMustWriteLock(inode);
32696 +       SiMustAnyLock(inode->i_sb);
32697 +
32698 +       allocated = NULL;
32699 +       do_read = 0;
32700 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32701 +       vdir = au_ivdir(inode);
32702 +       if (!vdir) {
32703 +               do_read = 1;
32704 +               vdir = alloc_vdir(file);
32705 +               err = PTR_ERR(vdir);
32706 +               if (IS_ERR(vdir))
32707 +                       goto out;
32708 +               err = 0;
32709 +               allocated = vdir;
32710 +       } else if (may_read
32711 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32712 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32713 +               do_read = 1;
32714 +               err = reinit_vdir(vdir);
32715 +               if (unlikely(err))
32716 +                       goto out;
32717 +       }
32718 +
32719 +       if (!do_read)
32720 +               return 0; /* success */
32721 +
32722 +       arg.file = file;
32723 +       arg.vdir = vdir;
32724 +       err = au_do_read_vdir(&arg);
32725 +       if (!err) {
32726 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32727 +               vdir->vd_version = inode_query_iversion(inode);
32728 +               vdir->vd_last.ul = 0;
32729 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32730 +               if (allocated)
32731 +                       au_set_ivdir(inode, allocated);
32732 +       } else if (allocated)
32733 +               au_vdir_free(allocated);
32734 +
32735 +out:
32736 +       return err;
32737 +}
32738 +
32739 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32740 +{
32741 +       int err, rerr;
32742 +       unsigned long ul, n;
32743 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32744 +
32745 +       AuDebugOn(tgt->vd_nblk != 1);
32746 +
32747 +       err = -ENOMEM;
32748 +       if (tgt->vd_nblk < src->vd_nblk) {
32749 +               unsigned char **p;
32750 +
32751 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32752 +                               GFP_NOFS, /*may_shrink*/0);
32753 +               if (unlikely(!p))
32754 +                       goto out;
32755 +               tgt->vd_deblk = p;
32756 +       }
32757 +
32758 +       if (tgt->vd_deblk_sz != deblk_sz) {
32759 +               unsigned char *p;
32760 +
32761 +               tgt->vd_deblk_sz = deblk_sz;
32762 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32763 +                               /*may_shrink*/1);
32764 +               if (unlikely(!p))
32765 +                       goto out;
32766 +               tgt->vd_deblk[0] = p;
32767 +       }
32768 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32769 +       tgt->vd_version = src->vd_version;
32770 +       tgt->vd_jiffy = src->vd_jiffy;
32771 +
32772 +       n = src->vd_nblk;
32773 +       for (ul = 1; ul < n; ul++) {
32774 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32775 +                                           GFP_NOFS);
32776 +               if (unlikely(!tgt->vd_deblk[ul]))
32777 +                       goto out;
32778 +               tgt->vd_nblk++;
32779 +       }
32780 +       tgt->vd_nblk = n;
32781 +       tgt->vd_last.ul = tgt->vd_last.ul;
32782 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32783 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32784 +               - src->vd_deblk[src->vd_last.ul];
32785 +       /* smp_mb(); */
32786 +       return 0; /* success */
32787 +
32788 +out:
32789 +       rerr = reinit_vdir(tgt);
32790 +       BUG_ON(rerr);
32791 +       return err;
32792 +}
32793 +
32794 +int au_vdir_init(struct file *file)
32795 +{
32796 +       int err;
32797 +       struct inode *inode;
32798 +       struct au_vdir *vdir_cache, *allocated;
32799 +
32800 +       /* test file->f_pos here instead of ctx->pos */
32801 +       err = read_vdir(file, !file->f_pos);
32802 +       if (unlikely(err))
32803 +               goto out;
32804 +
32805 +       allocated = NULL;
32806 +       vdir_cache = au_fvdir_cache(file);
32807 +       if (!vdir_cache) {
32808 +               vdir_cache = alloc_vdir(file);
32809 +               err = PTR_ERR(vdir_cache);
32810 +               if (IS_ERR(vdir_cache))
32811 +                       goto out;
32812 +               allocated = vdir_cache;
32813 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32814 +               /* test file->f_pos here instead of ctx->pos */
32815 +               err = reinit_vdir(vdir_cache);
32816 +               if (unlikely(err))
32817 +                       goto out;
32818 +       } else
32819 +               return 0; /* success */
32820 +
32821 +       inode = file_inode(file);
32822 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32823 +       if (!err) {
32824 +               file->f_version = inode_query_iversion(inode);
32825 +               if (allocated)
32826 +                       au_set_fvdir_cache(file, allocated);
32827 +       } else if (allocated)
32828 +               au_vdir_free(allocated);
32829 +
32830 +out:
32831 +       return err;
32832 +}
32833 +
32834 +static loff_t calc_offset(struct au_vdir *vdir)
32835 +{
32836 +       loff_t offset;
32837 +       union au_vdir_deblk_p p;
32838 +
32839 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32840 +       offset = vdir->vd_last.p.deblk - p.deblk;
32841 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32842 +       return offset;
32843 +}
32844 +
32845 +/* returns true or false */
32846 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32847 +{
32848 +       int valid;
32849 +       unsigned int deblk_sz;
32850 +       unsigned long ul, n;
32851 +       loff_t offset;
32852 +       union au_vdir_deblk_p p, deblk_end;
32853 +       struct au_vdir *vdir_cache;
32854 +
32855 +       valid = 1;
32856 +       vdir_cache = au_fvdir_cache(file);
32857 +       offset = calc_offset(vdir_cache);
32858 +       AuDbg("offset %lld\n", offset);
32859 +       if (ctx->pos == offset)
32860 +               goto out;
32861 +
32862 +       vdir_cache->vd_last.ul = 0;
32863 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32864 +       if (!ctx->pos)
32865 +               goto out;
32866 +
32867 +       valid = 0;
32868 +       deblk_sz = vdir_cache->vd_deblk_sz;
32869 +       ul = div64_u64(ctx->pos, deblk_sz);
32870 +       AuDbg("ul %lu\n", ul);
32871 +       if (ul >= vdir_cache->vd_nblk)
32872 +               goto out;
32873 +
32874 +       n = vdir_cache->vd_nblk;
32875 +       for (; ul < n; ul++) {
32876 +               p.deblk = vdir_cache->vd_deblk[ul];
32877 +               deblk_end.deblk = p.deblk + deblk_sz;
32878 +               offset = ul;
32879 +               offset *= deblk_sz;
32880 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32881 +                       unsigned int l;
32882 +
32883 +                       l = calc_size(p.de->de_str.len);
32884 +                       offset += l;
32885 +                       p.deblk += l;
32886 +               }
32887 +               if (!is_deblk_end(&p, &deblk_end)) {
32888 +                       valid = 1;
32889 +                       vdir_cache->vd_last.ul = ul;
32890 +                       vdir_cache->vd_last.p = p;
32891 +                       break;
32892 +               }
32893 +       }
32894 +
32895 +out:
32896 +       /* smp_mb(); */
32897 +       if (!valid)
32898 +               AuDbg("valid %d\n", !valid);
32899 +       return valid;
32900 +}
32901 +
32902 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32903 +{
32904 +       unsigned int l, deblk_sz;
32905 +       union au_vdir_deblk_p deblk_end;
32906 +       struct au_vdir *vdir_cache;
32907 +       struct au_vdir_de *de;
32908 +
32909 +       if (!seek_vdir(file, ctx))
32910 +               return 0;
32911 +
32912 +       vdir_cache = au_fvdir_cache(file);
32913 +       deblk_sz = vdir_cache->vd_deblk_sz;
32914 +       while (1) {
32915 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32916 +               deblk_end.deblk += deblk_sz;
32917 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32918 +                       de = vdir_cache->vd_last.p.de;
32919 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32920 +                             de->de_str.len, de->de_str.name, ctx->pos,
32921 +                             (unsigned long)de->de_ino, de->de_type);
32922 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32923 +                                              de->de_str.len, de->de_ino,
32924 +                                              de->de_type))) {
32925 +                               /* todo: ignore the error caused by udba? */
32926 +                               /* return err; */
32927 +                               return 0;
32928 +                       }
32929 +
32930 +                       l = calc_size(de->de_str.len);
32931 +                       vdir_cache->vd_last.p.deblk += l;
32932 +                       ctx->pos += l;
32933 +               }
32934 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32935 +                       vdir_cache->vd_last.ul++;
32936 +                       vdir_cache->vd_last.p.deblk
32937 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32938 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32939 +                       continue;
32940 +               }
32941 +               break;
32942 +       }
32943 +
32944 +       /* smp_mb(); */
32945 +       return 0;
32946 +}
32947 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32948 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32949 +++ linux/fs/aufs/vfsub.c       2023-09-04 13:39:55.763295607 +0200
32950 @@ -0,0 +1,918 @@
32951 +// SPDX-License-Identifier: GPL-2.0
32952 +/*
32953 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32954 + *
32955 + * This program is free software; you can redistribute it and/or modify
32956 + * it under the terms of the GNU General Public License as published by
32957 + * the Free Software Foundation; either version 2 of the License, or
32958 + * (at your option) any later version.
32959 + *
32960 + * This program is distributed in the hope that it will be useful,
32961 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32962 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32963 + * GNU General Public License for more details.
32964 + *
32965 + * You should have received a copy of the GNU General Public License
32966 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32967 + */
32968 +
32969 +/*
32970 + * sub-routines for VFS
32971 + */
32972 +
32973 +#include <linux/mnt_namespace.h>
32974 +#include <linux/nsproxy.h>
32975 +#include <linux/security.h>
32976 +#include <linux/splice.h>
32977 +#include "aufs.h"
32978 +
32979 +#ifdef CONFIG_AUFS_BR_FUSE
32980 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32981 +{
32982 +       if (!au_test_fuse(h_sb) || !au_userns)
32983 +               return 0;
32984 +
32985 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32986 +}
32987 +#endif
32988 +
32989 +int vfsub_sync_filesystem(struct super_block *h_sb)
32990 +{
32991 +       int err;
32992 +
32993 +       lockdep_off();
32994 +       down_read(&h_sb->s_umount);
32995 +       err = sync_filesystem(h_sb);
32996 +       up_read(&h_sb->s_umount);
32997 +       lockdep_on();
32998 +
32999 +       return err;
33000 +}
33001 +
33002 +/* ---------------------------------------------------------------------- */
33003 +
33004 +int vfsub_update_h_iattr(struct path *h_path, int *did)
33005 +{
33006 +       int err;
33007 +       struct kstat st;
33008 +       struct super_block *h_sb;
33009 +
33010 +       /*
33011 +        * Always needs h_path->mnt for LSM or FUSE branch.
33012 +        */
33013 +       AuDebugOn(!h_path->mnt);
33014 +
33015 +       /* for remote fs, leave work for its getattr or d_revalidate */
33016 +       /* for bad i_attr fs, handle them in aufs_getattr() */
33017 +       /* still some fs may acquire i_mutex. we need to skip them */
33018 +       err = 0;
33019 +       if (!did)
33020 +               did = &err;
33021 +       h_sb = h_path->dentry->d_sb;
33022 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
33023 +       if (*did)
33024 +               err = vfsub_getattr(h_path, &st);
33025 +
33026 +       return err;
33027 +}
33028 +
33029 +/* ---------------------------------------------------------------------- */
33030 +
33031 +struct file *vfsub_dentry_open(struct path *path, int flags)
33032 +{
33033 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
33034 +                          current_cred());
33035 +}
33036 +
33037 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
33038 +{
33039 +       struct file *file;
33040 +
33041 +       lockdep_off();
33042 +       file = filp_open(path,
33043 +                        oflags /* | __FMODE_NONOTIFY */,
33044 +                        mode);
33045 +       lockdep_on();
33046 +       if (IS_ERR(file))
33047 +               goto out;
33048 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33049 +
33050 +out:
33051 +       return file;
33052 +}
33053 +
33054 +/*
33055 + * Ideally this function should call VFS:do_last() in order to keep all its
33056 + * checkings. But it is very hard for aufs to regenerate several VFS internal
33057 + * structure such as nameidata. This is a second (or third) best approach.
33058 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
33059 + */
33060 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33061 +                     struct vfsub_aopen_args *args)
33062 +{
33063 +       int err;
33064 +       struct au_branch *br = args->br;
33065 +       struct file *file = args->file;
33066 +       /* copied from linux/fs/namei.c:atomic_open() */
33067 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33068 +
33069 +       IMustLock(dir);
33070 +       AuDebugOn(!dir->i_op->atomic_open);
33071 +
33072 +       err = au_br_test_oflag(args->open_flag, br);
33073 +       if (unlikely(err))
33074 +               goto out;
33075 +
33076 +       au_lcnt_inc(&br->br_nfiles);
33077 +       file->f_path.dentry = DENTRY_NOT_SET;
33078 +       file->f_path.mnt = au_br_mnt(br);
33079 +       AuDbg("%ps\n", dir->i_op->atomic_open);
33080 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
33081 +                                    args->create_mode);
33082 +       if (unlikely(err < 0)) {
33083 +               au_lcnt_dec(&br->br_nfiles);
33084 +               goto out;
33085 +       }
33086 +
33087 +       /* temporary workaround for nfsv4 branch */
33088 +       if (au_test_nfs(dir->i_sb))
33089 +               nfs_mark_for_revalidate(dir);
33090 +
33091 +       if (file->f_mode & FMODE_CREATED)
33092 +               fsnotify_create(dir, dentry);
33093 +       if (!(file->f_mode & FMODE_OPENED)) {
33094 +               au_lcnt_dec(&br->br_nfiles);
33095 +               goto out;
33096 +       }
33097 +
33098 +       /* todo: call VFS:may_open() here */
33099 +       /* todo: ima_file_check() too? */
33100 +       if (!err && (args->open_flag & __FMODE_EXEC))
33101 +               err = deny_write_access(file);
33102 +       if (!err)
33103 +               fsnotify_open(file);
33104 +       else
33105 +               au_lcnt_dec(&br->br_nfiles);
33106 +       /* note that the file is created and still opened */
33107 +
33108 +out:
33109 +       return err;
33110 +}
33111 +
33112 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33113 +{
33114 +       int err;
33115 +
33116 +       err = kern_path(name, flags, path);
33117 +       if (!err && d_is_positive(path->dentry))
33118 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33119 +       return err;
33120 +}
33121 +
33122 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33123 +                                            struct path *ppath, int len)
33124 +{
33125 +       struct path path;
33126 +
33127 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
33128 +       if (IS_ERR(path.dentry))
33129 +               goto out;
33130 +       if (d_is_positive(path.dentry)) {
33131 +               path.mnt = ppath->mnt;
33132 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33133 +       }
33134 +
33135 +out:
33136 +       AuTraceErrPtr(path.dentry);
33137 +       return path.dentry;
33138 +}
33139 +
33140 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33141 +                                   int len)
33142 +{
33143 +       struct path path;
33144 +
33145 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33146 +       IMustLock(d_inode(ppath->dentry));
33147 +
33148 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
33149 +       if (IS_ERR(path.dentry))
33150 +               goto out;
33151 +       if (d_is_positive(path.dentry)) {
33152 +               path.mnt = ppath->mnt;
33153 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33154 +       }
33155 +
33156 +out:
33157 +       AuTraceErrPtr(path.dentry);
33158 +       return path.dentry;
33159 +}
33160 +
33161 +void vfsub_call_lkup_one(void *args)
33162 +{
33163 +       struct vfsub_lkup_one_args *a = args;
33164 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
33165 +}
33166 +
33167 +/* ---------------------------------------------------------------------- */
33168 +
33169 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33170 +                                struct dentry *d2, struct au_hinode *hdir2)
33171 +{
33172 +       struct dentry *d;
33173 +
33174 +       lockdep_off();
33175 +       d = lock_rename(d1, d2);
33176 +       lockdep_on();
33177 +       au_hn_suspend(hdir1);
33178 +       if (hdir1 != hdir2)
33179 +               au_hn_suspend(hdir2);
33180 +
33181 +       return d;
33182 +}
33183 +
33184 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33185 +                        struct dentry *d2, struct au_hinode *hdir2)
33186 +{
33187 +       au_hn_resume(hdir1);
33188 +       if (hdir1 != hdir2)
33189 +               au_hn_resume(hdir2);
33190 +       lockdep_off();
33191 +       unlock_rename(d1, d2);
33192 +       lockdep_on();
33193 +}
33194 +
33195 +/* ---------------------------------------------------------------------- */
33196 +
33197 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33198 +{
33199 +       int err;
33200 +       struct dentry *d;
33201 +       struct mnt_idmap *idmap;
33202 +
33203 +       IMustLock(dir);
33204 +
33205 +       d = path->dentry;
33206 +       path->dentry = d->d_parent;
33207 +       err = security_path_mknod(path, d, mode, 0);
33208 +       path->dentry = d;
33209 +       if (unlikely(err))
33210 +               goto out;
33211 +       idmap = mnt_idmap(path->mnt);
33212 +
33213 +       lockdep_off();
33214 +       err = vfs_create(idmap, dir, path->dentry, mode, want_excl);
33215 +       lockdep_on();
33216 +       if (!err) {
33217 +               struct path tmp = *path;
33218 +               int did;
33219 +
33220 +               vfsub_update_h_iattr(&tmp, &did);
33221 +               if (did) {
33222 +                       tmp.dentry = path->dentry->d_parent;
33223 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33224 +               }
33225 +               /*ignore*/
33226 +       }
33227 +
33228 +out:
33229 +       return err;
33230 +}
33231 +
33232 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33233 +{
33234 +       int err;
33235 +       struct dentry *d;
33236 +       struct mnt_idmap *idmap;
33237 +
33238 +       IMustLock(dir);
33239 +
33240 +       d = path->dentry;
33241 +       path->dentry = d->d_parent;
33242 +       err = security_path_symlink(path, d, symname);
33243 +       path->dentry = d;
33244 +       if (unlikely(err))
33245 +               goto out;
33246 +       idmap = mnt_idmap(path->mnt);
33247 +
33248 +       lockdep_off();
33249 +       err = vfs_symlink(idmap, dir, path->dentry, symname);
33250 +       lockdep_on();
33251 +       if (!err) {
33252 +               struct path tmp = *path;
33253 +               int did;
33254 +
33255 +               vfsub_update_h_iattr(&tmp, &did);
33256 +               if (did) {
33257 +                       tmp.dentry = path->dentry->d_parent;
33258 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33259 +               }
33260 +               /*ignore*/
33261 +       }
33262 +
33263 +out:
33264 +       return err;
33265 +}
33266 +
33267 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33268 +{
33269 +       int err;
33270 +       struct dentry *d;
33271 +       struct mnt_idmap *idmap;
33272 +
33273 +       IMustLock(dir);
33274 +
33275 +       d = path->dentry;
33276 +       path->dentry = d->d_parent;
33277 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33278 +       path->dentry = d;
33279 +       if (unlikely(err))
33280 +               goto out;
33281 +       idmap = mnt_idmap(path->mnt);
33282 +
33283 +       lockdep_off();
33284 +       err = vfs_mknod(idmap, dir, path->dentry, mode, dev);
33285 +       lockdep_on();
33286 +       if (!err) {
33287 +               struct path tmp = *path;
33288 +               int did;
33289 +
33290 +               vfsub_update_h_iattr(&tmp, &did);
33291 +               if (did) {
33292 +                       tmp.dentry = path->dentry->d_parent;
33293 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33294 +               }
33295 +               /*ignore*/
33296 +       }
33297 +
33298 +out:
33299 +       return err;
33300 +}
33301 +
33302 +static int au_test_nlink(struct inode *inode)
33303 +{
33304 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33305 +
33306 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33307 +           || inode->i_nlink < link_max)
33308 +               return 0;
33309 +       return -EMLINK;
33310 +}
33311 +
33312 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33313 +              struct inode **delegated_inode)
33314 +{
33315 +       int err;
33316 +       struct dentry *d;
33317 +       struct mnt_idmap *idmap;
33318 +
33319 +       IMustLock(dir);
33320 +
33321 +       err = au_test_nlink(d_inode(src_dentry));
33322 +       if (unlikely(err))
33323 +               return err;
33324 +
33325 +       /* we don't call may_linkat() */
33326 +       d = path->dentry;
33327 +       path->dentry = d->d_parent;
33328 +       err = security_path_link(src_dentry, path, d);
33329 +       path->dentry = d;
33330 +       if (unlikely(err))
33331 +               goto out;
33332 +       idmap = mnt_idmap(path->mnt);
33333 +
33334 +       lockdep_off();
33335 +       err = vfs_link(src_dentry, idmap, dir, path->dentry, delegated_inode);
33336 +       lockdep_on();
33337 +       if (!err) {
33338 +               struct path tmp = *path;
33339 +               int did;
33340 +
33341 +               /* fuse has different memory inode for the same inumber */
33342 +               vfsub_update_h_iattr(&tmp, &did);
33343 +               if (did) {
33344 +                       tmp.dentry = path->dentry->d_parent;
33345 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33346 +                       tmp.dentry = src_dentry;
33347 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33348 +               }
33349 +               /*ignore*/
33350 +       }
33351 +
33352 +out:
33353 +       return err;
33354 +}
33355 +
33356 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33357 +                struct inode *dir, struct path *path,
33358 +                struct inode **delegated_inode, unsigned int flags)
33359 +{
33360 +       int err;
33361 +       struct renamedata rd;
33362 +       struct path tmp = {
33363 +               .mnt    = path->mnt
33364 +       };
33365 +       struct dentry *d;
33366 +
33367 +       IMustLock(dir);
33368 +       IMustLock(src_dir);
33369 +
33370 +       d = path->dentry;
33371 +       path->dentry = d->d_parent;
33372 +       tmp.dentry = src_dentry->d_parent;
33373 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33374 +       path->dentry = d;
33375 +       if (unlikely(err))
33376 +               goto out;
33377 +
33378 +       rd.old_mnt_idmap = mnt_idmap(path->mnt);
33379 +       rd.old_dir = src_dir;
33380 +       rd.old_dentry = src_dentry;
33381 +       rd.new_mnt_idmap = rd.old_mnt_idmap;
33382 +       rd.new_dir = dir;
33383 +       rd.new_dentry = path->dentry;
33384 +       rd.delegated_inode = delegated_inode;
33385 +       rd.flags = flags;
33386 +       lockdep_off();
33387 +       err = vfs_rename(&rd);
33388 +       lockdep_on();
33389 +       if (!err) {
33390 +               int did;
33391 +
33392 +               tmp.dentry = d->d_parent;
33393 +               vfsub_update_h_iattr(&tmp, &did);
33394 +               if (did) {
33395 +                       tmp.dentry = src_dentry;
33396 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33397 +                       tmp.dentry = src_dentry->d_parent;
33398 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33399 +               }
33400 +               /*ignore*/
33401 +       }
33402 +
33403 +out:
33404 +       return err;
33405 +}
33406 +
33407 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33408 +{
33409 +       int err;
33410 +       struct dentry *d;
33411 +       struct mnt_idmap *idmap;
33412 +
33413 +       IMustLock(dir);
33414 +
33415 +       d = path->dentry;
33416 +       path->dentry = d->d_parent;
33417 +       err = security_path_mkdir(path, d, mode);
33418 +       path->dentry = d;
33419 +       if (unlikely(err))
33420 +               goto out;
33421 +       idmap = mnt_idmap(path->mnt);
33422 +
33423 +       lockdep_off();
33424 +       err = vfs_mkdir(idmap, dir, path->dentry, mode);
33425 +       lockdep_on();
33426 +       if (!err) {
33427 +               struct path tmp = *path;
33428 +               int did;
33429 +
33430 +               vfsub_update_h_iattr(&tmp, &did);
33431 +               if (did) {
33432 +                       tmp.dentry = path->dentry->d_parent;
33433 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33434 +               }
33435 +               /*ignore*/
33436 +       }
33437 +
33438 +out:
33439 +       return err;
33440 +}
33441 +
33442 +int vfsub_rmdir(struct inode *dir, struct path *path)
33443 +{
33444 +       int err;
33445 +       struct dentry *d;
33446 +       struct mnt_idmap *idmap;
33447 +
33448 +       IMustLock(dir);
33449 +
33450 +       d = path->dentry;
33451 +       path->dentry = d->d_parent;
33452 +       err = security_path_rmdir(path, d);
33453 +       path->dentry = d;
33454 +       if (unlikely(err))
33455 +               goto out;
33456 +       idmap = mnt_idmap(path->mnt);
33457 +
33458 +       lockdep_off();
33459 +       err = vfs_rmdir(idmap, dir, path->dentry);
33460 +       lockdep_on();
33461 +       if (!err) {
33462 +               struct path tmp = {
33463 +                       .dentry = path->dentry->d_parent,
33464 +                       .mnt    = path->mnt
33465 +               };
33466 +
33467 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33468 +       }
33469 +
33470 +out:
33471 +       return err;
33472 +}
33473 +
33474 +/* ---------------------------------------------------------------------- */
33475 +
33476 +/* todo: support mmap_sem? */
33477 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33478 +                    loff_t *ppos)
33479 +{
33480 +       ssize_t err;
33481 +
33482 +       lockdep_off();
33483 +       err = vfs_read(file, ubuf, count, ppos);
33484 +       lockdep_on();
33485 +       if (err >= 0)
33486 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33487 +       return err;
33488 +}
33489 +
33490 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33491 +                    loff_t *ppos)
33492 +{
33493 +       ssize_t err;
33494 +
33495 +       lockdep_off();
33496 +       err = kernel_read(file, kbuf, count, ppos);
33497 +       lockdep_on();
33498 +       AuTraceErr(err);
33499 +       if (err >= 0)
33500 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33501 +       return err;
33502 +}
33503 +
33504 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33505 +                     loff_t *ppos)
33506 +{
33507 +       ssize_t err;
33508 +
33509 +       lockdep_off();
33510 +       err = vfs_write(file, ubuf, count, ppos);
33511 +       lockdep_on();
33512 +       if (err >= 0)
33513 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33514 +       return err;
33515 +}
33516 +
33517 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33518 +{
33519 +       ssize_t err;
33520 +
33521 +       lockdep_off();
33522 +       err = kernel_write(file, kbuf, count, ppos);
33523 +       lockdep_on();
33524 +       if (err >= 0)
33525 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33526 +       return err;
33527 +}
33528 +
33529 +int vfsub_flush(struct file *file, fl_owner_t id)
33530 +{
33531 +       int err;
33532 +
33533 +       err = 0;
33534 +       if (file->f_op->flush) {
33535 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33536 +                       err = file->f_op->flush(file, id);
33537 +               else {
33538 +                       lockdep_off();
33539 +                       err = file->f_op->flush(file, id);
33540 +                       lockdep_on();
33541 +               }
33542 +               if (!err)
33543 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33544 +               /*ignore*/
33545 +       }
33546 +       return err;
33547 +}
33548 +
33549 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33550 +{
33551 +       int err;
33552 +
33553 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33554 +
33555 +       lockdep_off();
33556 +       err = iterate_dir(file, ctx);
33557 +       lockdep_on();
33558 +       if (err >= 0)
33559 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33560 +
33561 +       return err;
33562 +}
33563 +
33564 +long vfsub_splice_read(struct file *in, loff_t *ppos,
33565 +                      struct pipe_inode_info *pipe, size_t len,
33566 +                      unsigned int flags)
33567 +{
33568 +       long err;
33569 +
33570 +       lockdep_off();
33571 +       err = vfs_splice_read(in, ppos, pipe, len, flags);
33572 +       lockdep_on();
33573 +       file_accessed(in);
33574 +       if (err >= 0)
33575 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33576 +       return err;
33577 +}
33578 +
33579 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33580 +                      loff_t *ppos, size_t len, unsigned int flags)
33581 +{
33582 +       long err;
33583 +
33584 +       lockdep_off();
33585 +       err = do_splice_from(pipe, out, ppos, len, flags);
33586 +       lockdep_on();
33587 +       if (err >= 0)
33588 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33589 +       return err;
33590 +}
33591 +
33592 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33593 +{
33594 +       int err;
33595 +
33596 +       /* file can be NULL */
33597 +       lockdep_off();
33598 +       err = vfs_fsync(file, datasync);
33599 +       lockdep_on();
33600 +       if (!err) {
33601 +               if (!path) {
33602 +                       AuDebugOn(!file);
33603 +                       path = &file->f_path;
33604 +               }
33605 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33606 +       }
33607 +       return err;
33608 +}
33609 +
33610 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33611 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33612 +               struct file *h_file)
33613 +{
33614 +       int err;
33615 +       struct inode *h_inode;
33616 +       struct super_block *h_sb;
33617 +       struct mnt_idmap *h_idmap;
33618 +
33619 +       if (!h_file) {
33620 +               err = vfsub_truncate(h_path, length);
33621 +               goto out;
33622 +       }
33623 +
33624 +       h_inode = d_inode(h_path->dentry);
33625 +       h_sb = h_inode->i_sb;
33626 +       lockdep_off();
33627 +       sb_start_write(h_sb);
33628 +       lockdep_on();
33629 +       err = security_file_truncate(h_file);
33630 +       if (!err) {
33631 +               h_idmap = mnt_idmap(h_path->mnt);
33632 +               lockdep_off();
33633 +               err = do_truncate(h_idmap, h_path->dentry, length, attr,
33634 +                                 h_file);
33635 +               lockdep_on();
33636 +       }
33637 +       lockdep_off();
33638 +       sb_end_write(h_sb);
33639 +       lockdep_on();
33640 +
33641 +out:
33642 +       return err;
33643 +}
33644 +
33645 +/* ---------------------------------------------------------------------- */
33646 +
33647 +struct au_vfsub_mkdir_args {
33648 +       int *errp;
33649 +       struct inode *dir;
33650 +       struct path *path;
33651 +       int mode;
33652 +};
33653 +
33654 +static void au_call_vfsub_mkdir(void *args)
33655 +{
33656 +       struct au_vfsub_mkdir_args *a = args;
33657 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33658 +}
33659 +
33660 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33661 +{
33662 +       int err, do_sio, wkq_err;
33663 +       struct mnt_idmap *idmap;
33664 +
33665 +       idmap = mnt_idmap(path->mnt);
33666 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33667 +       if (!do_sio) {
33668 +               lockdep_off();
33669 +               err = vfsub_mkdir(dir, path, mode);
33670 +               lockdep_on();
33671 +       } else {
33672 +               struct au_vfsub_mkdir_args args = {
33673 +                       .errp   = &err,
33674 +                       .dir    = dir,
33675 +                       .path   = path,
33676 +                       .mode   = mode
33677 +               };
33678 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33679 +               if (unlikely(wkq_err))
33680 +                       err = wkq_err;
33681 +       }
33682 +
33683 +       return err;
33684 +}
33685 +
33686 +struct au_vfsub_rmdir_args {
33687 +       int *errp;
33688 +       struct inode *dir;
33689 +       struct path *path;
33690 +};
33691 +
33692 +static void au_call_vfsub_rmdir(void *args)
33693 +{
33694 +       struct au_vfsub_rmdir_args *a = args;
33695 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33696 +}
33697 +
33698 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33699 +{
33700 +       int err, do_sio, wkq_err;
33701 +       struct mnt_idmap *idmap;
33702 +
33703 +       idmap = mnt_idmap(path->mnt);
33704 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33705 +       if (!do_sio) {
33706 +               lockdep_off();
33707 +               err = vfsub_rmdir(dir, path);
33708 +               lockdep_on();
33709 +       } else {
33710 +               struct au_vfsub_rmdir_args args = {
33711 +                       .errp   = &err,
33712 +                       .dir    = dir,
33713 +                       .path   = path
33714 +               };
33715 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33716 +               if (unlikely(wkq_err))
33717 +                       err = wkq_err;
33718 +       }
33719 +
33720 +       return err;
33721 +}
33722 +
33723 +/* ---------------------------------------------------------------------- */
33724 +
33725 +struct notify_change_args {
33726 +       int *errp;
33727 +       struct path *path;
33728 +       struct iattr *ia;
33729 +       struct inode **delegated_inode;
33730 +};
33731 +
33732 +static void call_notify_change(void *args)
33733 +{
33734 +       struct notify_change_args *a = args;
33735 +       struct inode *h_inode;
33736 +       struct mnt_idmap *idmap;
33737 +
33738 +       h_inode = d_inode(a->path->dentry);
33739 +       IMustLock(h_inode);
33740 +
33741 +       *a->errp = -EPERM;
33742 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33743 +               idmap = mnt_idmap(a->path->mnt);
33744 +               lockdep_off();
33745 +               *a->errp = notify_change(idmap, a->path->dentry, a->ia,
33746 +                                        a->delegated_inode);
33747 +               lockdep_on();
33748 +               if (!*a->errp)
33749 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33750 +       }
33751 +       AuTraceErr(*a->errp);
33752 +}
33753 +
33754 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33755 +                       struct inode **delegated_inode)
33756 +{
33757 +       int err;
33758 +       struct notify_change_args args = {
33759 +               .errp                   = &err,
33760 +               .path                   = path,
33761 +               .ia                     = ia,
33762 +               .delegated_inode        = delegated_inode
33763 +       };
33764 +
33765 +       call_notify_change(&args);
33766 +
33767 +       return err;
33768 +}
33769 +
33770 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33771 +                           struct inode **delegated_inode)
33772 +{
33773 +       int err, wkq_err;
33774 +       struct notify_change_args args = {
33775 +               .errp                   = &err,
33776 +               .path                   = path,
33777 +               .ia                     = ia,
33778 +               .delegated_inode        = delegated_inode
33779 +       };
33780 +
33781 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33782 +       if (unlikely(wkq_err))
33783 +               err = wkq_err;
33784 +
33785 +       return err;
33786 +}
33787 +
33788 +/* ---------------------------------------------------------------------- */
33789 +
33790 +struct unlink_args {
33791 +       int *errp;
33792 +       struct inode *dir;
33793 +       struct path *path;
33794 +       struct inode **delegated_inode;
33795 +};
33796 +
33797 +static void call_unlink(void *args)
33798 +{
33799 +       struct unlink_args *a = args;
33800 +       struct dentry *d = a->path->dentry;
33801 +       struct inode *h_inode;
33802 +       struct mnt_idmap *idmap;
33803 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33804 +                                     && au_dcount(d) == 1);
33805 +
33806 +       IMustLock(a->dir);
33807 +
33808 +       a->path->dentry = d->d_parent;
33809 +       *a->errp = security_path_unlink(a->path, d);
33810 +       a->path->dentry = d;
33811 +       if (unlikely(*a->errp))
33812 +               return;
33813 +
33814 +       if (!stop_sillyrename)
33815 +               dget(d);
33816 +       h_inode = NULL;
33817 +       if (d_is_positive(d)) {
33818 +               h_inode = d_inode(d);
33819 +               ihold(h_inode);
33820 +       }
33821 +
33822 +       idmap = mnt_idmap(a->path->mnt);
33823 +       lockdep_off();
33824 +       *a->errp = vfs_unlink(idmap, a->dir, d, a->delegated_inode);
33825 +       lockdep_on();
33826 +       if (!*a->errp) {
33827 +               struct path tmp = {
33828 +                       .dentry = d->d_parent,
33829 +                       .mnt    = a->path->mnt
33830 +               };
33831 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33832 +       }
33833 +
33834 +       if (!stop_sillyrename)
33835 +               dput(d);
33836 +       if (h_inode)
33837 +               iput(h_inode);
33838 +
33839 +       AuTraceErr(*a->errp);
33840 +}
33841 +
33842 +/*
33843 + * @dir: must be locked.
33844 + * @dentry: target dentry.
33845 + */
33846 +int vfsub_unlink(struct inode *dir, struct path *path,
33847 +                struct inode **delegated_inode, int force)
33848 +{
33849 +       int err;
33850 +       struct unlink_args args = {
33851 +               .errp                   = &err,
33852 +               .dir                    = dir,
33853 +               .path                   = path,
33854 +               .delegated_inode        = delegated_inode
33855 +       };
33856 +
33857 +       if (!force)
33858 +               call_unlink(&args);
33859 +       else {
33860 +               int wkq_err;
33861 +
33862 +               wkq_err = au_wkq_wait(call_unlink, &args);
33863 +               if (unlikely(wkq_err))
33864 +                       err = wkq_err;
33865 +       }
33866 +
33867 +       return err;
33868 +}
33869 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33870 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33871 +++ linux/fs/aufs/vfsub.h       2023-09-04 13:39:55.763295607 +0200
33872 @@ -0,0 +1,390 @@
33873 +/* SPDX-License-Identifier: GPL-2.0 */
33874 +/*
33875 + * Copyright (C) 2005-2022 Junjiro R. Okajima
33876 + *
33877 + * This program is free software; you can redistribute it and/or modify
33878 + * it under the terms of the GNU General Public License as published by
33879 + * the Free Software Foundation; either version 2 of the License, or
33880 + * (at your option) any later version.
33881 + *
33882 + * This program is distributed in the hope that it will be useful,
33883 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33884 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33885 + * GNU General Public License for more details.
33886 + *
33887 + * You should have received a copy of the GNU General Public License
33888 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33889 + */
33890 +
33891 +/*
33892 + * sub-routines for VFS
33893 + */
33894 +
33895 +#ifndef __AUFS_VFSUB_H__
33896 +#define __AUFS_VFSUB_H__
33897 +
33898 +#ifdef __KERNEL__
33899 +
33900 +#include <linux/fs.h>
33901 +#include <linux/mount.h>
33902 +#include <linux/posix_acl.h>
33903 +#include <linux/xattr.h>
33904 +#include "debug.h"
33905 +
33906 +/* copied from linux/fs/internal.h */
33907 +/* todo: BAD approach!! */
33908 +extern void __mnt_drop_write(struct vfsmount *);
33909 +extern struct file *alloc_empty_file(int, const struct cred *);
33910 +
33911 +/* ---------------------------------------------------------------------- */
33912 +
33913 +/* lock subclass for lower inode */
33914 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33915 +/* reduce? gave up. */
33916 +enum {
33917 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33918 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33919 +       AuLsc_I_PARENT2,        /* copyup dirs */
33920 +       AuLsc_I_PARENT3,        /* copyup wh */
33921 +       AuLsc_I_CHILD,
33922 +       AuLsc_I_CHILD2,
33923 +       AuLsc_I_End
33924 +};
33925 +
33926 +/* to debug easier, do not make them inlined functions */
33927 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33928 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33929 +
33930 +/* ---------------------------------------------------------------------- */
33931 +
33932 +static inline void vfsub_drop_nlink(struct inode *inode)
33933 +{
33934 +       AuDebugOn(!inode->i_nlink);
33935 +       drop_nlink(inode);
33936 +}
33937 +
33938 +static inline void vfsub_dead_dir(struct inode *inode)
33939 +{
33940 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33941 +       inode->i_flags |= S_DEAD;
33942 +       clear_nlink(inode);
33943 +}
33944 +
33945 +static inline int vfsub_native_ro(struct inode *inode)
33946 +{
33947 +       return sb_rdonly(inode->i_sb)
33948 +               || IS_RDONLY(inode)
33949 +               /* || IS_APPEND(inode) */
33950 +               || IS_IMMUTABLE(inode);
33951 +}
33952 +
33953 +#ifdef CONFIG_AUFS_BR_FUSE
33954 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33955 +#else
33956 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33957 +#endif
33958 +
33959 +int vfsub_sync_filesystem(struct super_block *h_sb);
33960 +
33961 +/* ---------------------------------------------------------------------- */
33962 +
33963 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33964 +struct file *vfsub_dentry_open(struct path *path, int flags);
33965 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33966 +struct au_branch;
33967 +struct vfsub_aopen_args {
33968 +       struct file             *file;
33969 +       unsigned int            open_flag;
33970 +       umode_t                 create_mode;
33971 +       struct au_branch        *br;
33972 +};
33973 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33974 +                     struct vfsub_aopen_args *args);
33975 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33976 +
33977 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33978 +                                            struct path *ppath, int len);
33979 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33980 +                                   int len);
33981 +
33982 +struct vfsub_lkup_one_args {
33983 +       struct dentry **errp;
33984 +       struct qstr *name;
33985 +       struct path *ppath;
33986 +};
33987 +
33988 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33989 +                                           struct path *ppath)
33990 +{
33991 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
33992 +}
33993 +
33994 +void vfsub_call_lkup_one(void *args);
33995 +
33996 +/* ---------------------------------------------------------------------- */
33997 +
33998 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33999 +{
34000 +       int err;
34001 +
34002 +       lockdep_off();
34003 +       err = mnt_want_write(mnt);
34004 +       lockdep_on();
34005 +       return err;
34006 +}
34007 +
34008 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
34009 +{
34010 +       lockdep_off();
34011 +       mnt_drop_write(mnt);
34012 +       lockdep_on();
34013 +}
34014 +
34015 +#if 0 /* reserved */
34016 +static inline void vfsub_mnt_drop_write_file(struct file *file)
34017 +{
34018 +       lockdep_off();
34019 +       mnt_drop_write_file(file);
34020 +       lockdep_on();
34021 +}
34022 +#endif
34023 +
34024 +/* ---------------------------------------------------------------------- */
34025 +
34026 +struct au_hinode;
34027 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
34028 +                                struct dentry *d2, struct au_hinode *hdir2);
34029 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
34030 +                        struct dentry *d2, struct au_hinode *hdir2);
34031 +
34032 +int vfsub_create(struct inode *dir, struct path *path, int mode,
34033 +                bool want_excl);
34034 +int vfsub_symlink(struct inode *dir, struct path *path,
34035 +                 const char *symname);
34036 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
34037 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
34038 +              struct path *path, struct inode **delegated_inode);
34039 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
34040 +                struct inode *hdir, struct path *path,
34041 +                struct inode **delegated_inode, unsigned int flags);
34042 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
34043 +int vfsub_rmdir(struct inode *dir, struct path *path);
34044 +
34045 +/* ---------------------------------------------------------------------- */
34046 +
34047 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
34048 +                    loff_t *ppos);
34049 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
34050 +                       loff_t *ppos);
34051 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
34052 +                     loff_t *ppos);
34053 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
34054 +                     loff_t *ppos);
34055 +int vfsub_flush(struct file *file, fl_owner_t id);
34056 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
34057 +
34058 +static inline loff_t vfsub_f_size_read(struct file *file)
34059 +{
34060 +       return i_size_read(file_inode(file));
34061 +}
34062 +
34063 +static inline unsigned int vfsub_file_flags(struct file *file)
34064 +{
34065 +       unsigned int flags;
34066 +
34067 +       spin_lock(&file->f_lock);
34068 +       flags = file->f_flags;
34069 +       spin_unlock(&file->f_lock);
34070 +
34071 +       return flags;
34072 +}
34073 +
34074 +static inline int vfsub_file_execed(struct file *file)
34075 +{
34076 +       /* todo: direct access f_flags */
34077 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34078 +}
34079 +
34080 +#if 0 /* reserved */
34081 +static inline void vfsub_file_accessed(struct file *h_file)
34082 +{
34083 +       file_accessed(h_file);
34084 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34085 +}
34086 +#endif
34087 +
34088 +#if 0 /* reserved */
34089 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34090 +                                    struct dentry *h_dentry)
34091 +{
34092 +       struct path h_path = {
34093 +               .dentry = h_dentry,
34094 +               .mnt    = h_mnt
34095 +       };
34096 +       touch_atime(&h_path);
34097 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34098 +}
34099 +#endif
34100 +
34101 +static inline int vfsub_update_time(struct inode *h_inode,
34102 +                                   struct timespec64 *ts, int flags)
34103 +{
34104 +       return inode_update_time(h_inode, ts, flags);
34105 +       /* no vfsub_update_h_iattr() since we don't have struct path */
34106 +}
34107 +
34108 +#ifdef CONFIG_FS_POSIX_ACL
34109 +static inline int vfsub_acl_chmod(struct mnt_idmap *h_idmap,
34110 +                                 struct dentry *h_dentry, umode_t h_mode)
34111 +{
34112 +       int err;
34113 +
34114 +       err = posix_acl_chmod(h_idmap, h_dentry, h_mode);
34115 +       if (err == -EOPNOTSUPP)
34116 +               err = 0;
34117 +       return err;
34118 +}
34119 +#else
34120 +AuStubInt0(vfsub_acl_chmod, struct mnt_idmap *h_idmap,
34121 +          struct dentry *h_dentry, umode_t h_mode);
34122 +#endif
34123 +
34124 +long vfsub_splice_read(struct file *in, loff_t *ppos,
34125 +                      struct pipe_inode_info *pipe, size_t len,
34126 +                      unsigned int flags);
34127 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34128 +                      loff_t *ppos, size_t len, unsigned int flags);
34129 +
34130 +static inline long vfsub_truncate(struct path *path, loff_t length)
34131 +{
34132 +       long err;
34133 +
34134 +       lockdep_off();
34135 +       err = vfs_truncate(path, length);
34136 +       lockdep_on();
34137 +       return err;
34138 +}
34139 +
34140 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34141 +               struct file *h_file);
34142 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34143 +
34144 +/*
34145 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34146 + * ioctl.
34147 + */
34148 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34149 +                                           loff_t len)
34150 +{
34151 +       loff_t err;
34152 +
34153 +       lockdep_off();
34154 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34155 +       lockdep_on();
34156 +
34157 +       return err;
34158 +}
34159 +
34160 +/* copy_file_range(2) is a systemcall */
34161 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34162 +                                           struct file *dst, loff_t dst_pos,
34163 +                                           size_t len, unsigned int flags)
34164 +{
34165 +       ssize_t ssz;
34166 +
34167 +       lockdep_off();
34168 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34169 +       lockdep_on();
34170 +
34171 +       return ssz;
34172 +}
34173 +
34174 +/* ---------------------------------------------------------------------- */
34175 +
34176 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34177 +{
34178 +       loff_t err;
34179 +
34180 +       lockdep_off();
34181 +       err = vfs_llseek(file, offset, origin);
34182 +       lockdep_on();
34183 +       return err;
34184 +}
34185 +
34186 +/* ---------------------------------------------------------------------- */
34187 +
34188 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34189 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34190 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34191 +                           struct inode **delegated_inode);
34192 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34193 +                       struct inode **delegated_inode);
34194 +int vfsub_unlink(struct inode *dir, struct path *path,
34195 +                struct inode **delegated_inode, int force);
34196 +
34197 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34198 +{
34199 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34200 +}
34201 +
34202 +/* ---------------------------------------------------------------------- */
34203 +
34204 +static inline int vfsub_setxattr(struct mnt_idmap *idmap,
34205 +                                struct dentry *dentry, const char *name,
34206 +                                const void *value, size_t size, int flags)
34207 +{
34208 +       int err;
34209 +
34210 +       lockdep_off();
34211 +       err = vfs_setxattr(idmap, dentry, name, value, size, flags);
34212 +       lockdep_on();
34213 +
34214 +       return err;
34215 +}
34216 +
34217 +static inline int vfsub_removexattr(struct mnt_idmap *idmap,
34218 +                                   struct dentry *dentry, const char *name)
34219 +{
34220 +       int err;
34221 +
34222 +       lockdep_off();
34223 +       err = vfs_removexattr(idmap, dentry, name);
34224 +       lockdep_on();
34225 +
34226 +       return err;
34227 +}
34228 +
34229 +#ifdef CONFIG_FS_POSIX_ACL
34230 +static inline int vfsub_set_acl(struct mnt_idmap *idmap,
34231 +                               struct dentry *dentry, const char *name,
34232 +                               struct posix_acl *acl)
34233 +{
34234 +       int err;
34235 +
34236 +       lockdep_off();
34237 +       err = vfs_set_acl(idmap, dentry, name, acl);
34238 +       lockdep_on();
34239 +
34240 +       return err;
34241 +}
34242 +
34243 +static inline int vfsub_remove_acl(struct mnt_idmap *idmap,
34244 +                                  struct dentry *dentry, const char *name)
34245 +{
34246 +       int err;
34247 +
34248 +       lockdep_off();
34249 +       err = vfs_remove_acl(idmap, dentry, name);
34250 +       lockdep_on();
34251 +
34252 +       return err;
34253 +}
34254 +#else
34255 +AuStubInt0(vfsub_set_acl, struct mnt_idmap *idmap, struct dentry *dentry,
34256 +          const char *name, struct posix_acl *acl);
34257 +AuStubInt0(vfsub_remove_acl, struct mnt_idmap *idmap,
34258 +          struct dentry *dentry, const char *name);
34259 +#endif
34260 +
34261 +#endif /* __KERNEL__ */
34262 +#endif /* __AUFS_VFSUB_H__ */
34263 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34264 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34265 +++ linux/fs/aufs/wbr_policy.c  2022-11-05 23:02:18.969222617 +0100
34266 @@ -0,0 +1,830 @@
34267 +// SPDX-License-Identifier: GPL-2.0
34268 +/*
34269 + * Copyright (C) 2005-2022 Junjiro R. Okajima
34270 + *
34271 + * This program is free software; you can redistribute it and/or modify
34272 + * it under the terms of the GNU General Public License as published by
34273 + * the Free Software Foundation; either version 2 of the License, or
34274 + * (at your option) any later version.
34275 + *
34276 + * This program is distributed in the hope that it will be useful,
34277 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34278 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34279 + * GNU General Public License for more details.
34280 + *
34281 + * You should have received a copy of the GNU General Public License
34282 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34283 + */
34284 +
34285 +/*
34286 + * policies for selecting one among multiple writable branches
34287 + */
34288 +
34289 +#include <linux/statfs.h>
34290 +#include "aufs.h"
34291 +
34292 +/* subset of cpup_attr() */
34293 +static noinline_for_stack
34294 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34295 +{
34296 +       int err, sbits;
34297 +       struct iattr ia;
34298 +       struct inode *h_isrc;
34299 +
34300 +       h_isrc = d_inode(h_src);
34301 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34302 +       ia.ia_mode = h_isrc->i_mode;
34303 +       ia.ia_uid = h_isrc->i_uid;
34304 +       ia.ia_gid = h_isrc->i_gid;
34305 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34306 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34307 +       /* no delegation since it is just created */
34308 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34309 +
34310 +       /* is this nfs only? */
34311 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34312 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34313 +               ia.ia_mode = h_isrc->i_mode;
34314 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34315 +       }
34316 +
34317 +       return err;
34318 +}
34319 +
34320 +#define AuCpdown_PARENT_OPQ    1
34321 +#define AuCpdown_WHED          (1 << 1)
34322 +#define AuCpdown_MADE_DIR      (1 << 2)
34323 +#define AuCpdown_DIROPQ                (1 << 3)
34324 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34325 +#define au_fset_cpdown(flags, name) \
34326 +       do { (flags) |= AuCpdown_##name; } while (0)
34327 +#define au_fclr_cpdown(flags, name) \
34328 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34329 +
34330 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34331 +                            unsigned int *flags)
34332 +{
34333 +       int err;
34334 +       struct dentry *opq_dentry;
34335 +
34336 +       opq_dentry = au_diropq_create(dentry, bdst);
34337 +       err = PTR_ERR(opq_dentry);
34338 +       if (IS_ERR(opq_dentry))
34339 +               goto out;
34340 +       dput(opq_dentry);
34341 +       au_fset_cpdown(*flags, DIROPQ);
34342 +
34343 +out:
34344 +       return err;
34345 +}
34346 +
34347 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34348 +                           struct inode *dir, aufs_bindex_t bdst)
34349 +{
34350 +       int err;
34351 +       struct path h_path;
34352 +       struct au_branch *br;
34353 +
34354 +       br = au_sbr(dentry->d_sb, bdst);
34355 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34356 +       err = PTR_ERR(h_path.dentry);
34357 +       if (IS_ERR(h_path.dentry))
34358 +               goto out;
34359 +
34360 +       err = 0;
34361 +       if (d_is_positive(h_path.dentry)) {
34362 +               h_path.mnt = au_br_mnt(br);
34363 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34364 +                                         dentry);
34365 +       }
34366 +       dput(h_path.dentry);
34367 +
34368 +out:
34369 +       return err;
34370 +}
34371 +
34372 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34373 +                        struct au_pin *pin,
34374 +                        struct dentry *h_parent, void *arg)
34375 +{
34376 +       int err, rerr;
34377 +       aufs_bindex_t bopq, btop;
34378 +       struct path h_path;
34379 +       struct dentry *parent;
34380 +       struct inode *h_dir, *h_inode, *inode, *dir;
34381 +       unsigned int *flags = arg;
34382 +
34383 +       btop = au_dbtop(dentry);
34384 +       /* dentry is di-locked */
34385 +       parent = dget_parent(dentry);
34386 +       dir = d_inode(parent);
34387 +       h_dir = d_inode(h_parent);
34388 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34389 +       IMustLock(h_dir);
34390 +
34391 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34392 +       if (unlikely(err < 0))
34393 +               goto out;
34394 +       h_path.dentry = au_h_dptr(dentry, bdst);
34395 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34396 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34397 +       if (unlikely(err))
34398 +               goto out_put;
34399 +       au_fset_cpdown(*flags, MADE_DIR);
34400 +
34401 +       bopq = au_dbdiropq(dentry);
34402 +       au_fclr_cpdown(*flags, WHED);
34403 +       au_fclr_cpdown(*flags, DIROPQ);
34404 +       if (au_dbwh(dentry) == bdst)
34405 +               au_fset_cpdown(*flags, WHED);
34406 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34407 +               au_fset_cpdown(*flags, PARENT_OPQ);
34408 +       h_inode = d_inode(h_path.dentry);
34409 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34410 +       if (au_ftest_cpdown(*flags, WHED)) {
34411 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34412 +               if (unlikely(err)) {
34413 +                       inode_unlock(h_inode);
34414 +                       goto out_dir;
34415 +               }
34416 +       }
34417 +
34418 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34419 +       inode_unlock(h_inode);
34420 +       if (unlikely(err))
34421 +               goto out_opq;
34422 +
34423 +       if (au_ftest_cpdown(*flags, WHED)) {
34424 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34425 +               if (unlikely(err))
34426 +                       goto out_opq;
34427 +       }
34428 +
34429 +       inode = d_inode(dentry);
34430 +       if (au_ibbot(inode) < bdst)
34431 +               au_set_ibbot(inode, bdst);
34432 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34433 +                     au_hi_flags(inode, /*isdir*/1));
34434 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34435 +       goto out; /* success */
34436 +
34437 +       /* revert */
34438 +out_opq:
34439 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34440 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34441 +               rerr = au_diropq_remove(dentry, bdst);
34442 +               inode_unlock(h_inode);
34443 +               if (unlikely(rerr)) {
34444 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34445 +                               dentry, bdst, rerr);
34446 +                       err = -EIO;
34447 +                       goto out;
34448 +               }
34449 +       }
34450 +out_dir:
34451 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34452 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34453 +               if (unlikely(rerr)) {
34454 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34455 +                               dentry, bdst, rerr);
34456 +                       err = -EIO;
34457 +               }
34458 +       }
34459 +out_put:
34460 +       au_set_h_dptr(dentry, bdst, NULL);
34461 +       if (au_dbbot(dentry) == bdst)
34462 +               au_update_dbbot(dentry);
34463 +out:
34464 +       dput(parent);
34465 +       return err;
34466 +}
34467 +
34468 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34469 +{
34470 +       int err;
34471 +       unsigned int flags;
34472 +
34473 +       flags = 0;
34474 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34475 +
34476 +       return err;
34477 +}
34478 +
34479 +/* ---------------------------------------------------------------------- */
34480 +
34481 +/* policies for create */
34482 +
34483 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34484 +{
34485 +       int err, i, j, ndentry;
34486 +       aufs_bindex_t bopq;
34487 +       struct au_dcsub_pages dpages;
34488 +       struct au_dpage *dpage;
34489 +       struct dentry **dentries, *parent, *d;
34490 +
34491 +       err = au_dpages_init(&dpages, GFP_NOFS);
34492 +       if (unlikely(err))
34493 +               goto out;
34494 +       parent = dget_parent(dentry);
34495 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34496 +       if (unlikely(err))
34497 +               goto out_free;
34498 +
34499 +       err = bindex;
34500 +       for (i = 0; i < dpages.ndpage; i++) {
34501 +               dpage = dpages.dpages + i;
34502 +               dentries = dpage->dentries;
34503 +               ndentry = dpage->ndentry;
34504 +               for (j = 0; j < ndentry; j++) {
34505 +                       d = dentries[j];
34506 +                       di_read_lock_parent2(d, !AuLock_IR);
34507 +                       bopq = au_dbdiropq(d);
34508 +                       di_read_unlock(d, !AuLock_IR);
34509 +                       if (bopq >= 0 && bopq < err)
34510 +                               err = bopq;
34511 +               }
34512 +       }
34513 +
34514 +out_free:
34515 +       dput(parent);
34516 +       au_dpages_free(&dpages);
34517 +out:
34518 +       return err;
34519 +}
34520 +
34521 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34522 +{
34523 +       for (; bindex >= 0; bindex--)
34524 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34525 +                       return bindex;
34526 +       return -EROFS;
34527 +}
34528 +
34529 +/* top down parent */
34530 +static int au_wbr_create_tdp(struct dentry *dentry,
34531 +                            unsigned int flags __maybe_unused)
34532 +{
34533 +       int err;
34534 +       aufs_bindex_t btop, bindex;
34535 +       struct super_block *sb;
34536 +       struct dentry *parent, *h_parent;
34537 +
34538 +       sb = dentry->d_sb;
34539 +       btop = au_dbtop(dentry);
34540 +       err = btop;
34541 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34542 +               goto out;
34543 +
34544 +       err = -EROFS;
34545 +       parent = dget_parent(dentry);
34546 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34547 +               h_parent = au_h_dptr(parent, bindex);
34548 +               if (!h_parent || d_is_negative(h_parent))
34549 +                       continue;
34550 +
34551 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34552 +                       err = bindex;
34553 +                       break;
34554 +               }
34555 +       }
34556 +       dput(parent);
34557 +
34558 +       /* bottom up here */
34559 +       if (unlikely(err < 0)) {
34560 +               err = au_wbr_bu(sb, btop - 1);
34561 +               if (err >= 0)
34562 +                       err = au_wbr_nonopq(dentry, err);
34563 +       }
34564 +
34565 +out:
34566 +       AuDbg("b%d\n", err);
34567 +       return err;
34568 +}
34569 +
34570 +/* ---------------------------------------------------------------------- */
34571 +
34572 +/* an exception for the policy other than tdp */
34573 +static int au_wbr_create_exp(struct dentry *dentry)
34574 +{
34575 +       int err;
34576 +       aufs_bindex_t bwh, bdiropq;
34577 +       struct dentry *parent;
34578 +
34579 +       err = -1;
34580 +       bwh = au_dbwh(dentry);
34581 +       parent = dget_parent(dentry);
34582 +       bdiropq = au_dbdiropq(parent);
34583 +       if (bwh >= 0) {
34584 +               if (bdiropq >= 0)
34585 +                       err = min(bdiropq, bwh);
34586 +               else
34587 +                       err = bwh;
34588 +               AuDbg("%d\n", err);
34589 +       } else if (bdiropq >= 0) {
34590 +               err = bdiropq;
34591 +               AuDbg("%d\n", err);
34592 +       }
34593 +       dput(parent);
34594 +
34595 +       if (err >= 0)
34596 +               err = au_wbr_nonopq(dentry, err);
34597 +
34598 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34599 +               err = -1;
34600 +
34601 +       AuDbg("%d\n", err);
34602 +       return err;
34603 +}
34604 +
34605 +/* ---------------------------------------------------------------------- */
34606 +
34607 +/* round robin */
34608 +static int au_wbr_create_init_rr(struct super_block *sb)
34609 +{
34610 +       int err;
34611 +
34612 +       err = au_wbr_bu(sb, au_sbbot(sb));
34613 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34614 +       /* smp_mb(); */
34615 +
34616 +       AuDbg("b%d\n", err);
34617 +       return err;
34618 +}
34619 +
34620 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34621 +{
34622 +       int err, nbr;
34623 +       unsigned int u;
34624 +       aufs_bindex_t bindex, bbot;
34625 +       struct super_block *sb;
34626 +       atomic_t *next;
34627 +
34628 +       err = au_wbr_create_exp(dentry);
34629 +       if (err >= 0)
34630 +               goto out;
34631 +
34632 +       sb = dentry->d_sb;
34633 +       next = &au_sbi(sb)->si_wbr_rr_next;
34634 +       bbot = au_sbbot(sb);
34635 +       nbr = bbot + 1;
34636 +       for (bindex = 0; bindex <= bbot; bindex++) {
34637 +               if (!au_ftest_wbr(flags, DIR)) {
34638 +                       err = atomic_dec_return(next) + 1;
34639 +                       /* modulo for 0 is meaningless */
34640 +                       if (unlikely(!err))
34641 +                               err = atomic_dec_return(next) + 1;
34642 +               } else
34643 +                       err = atomic_read(next);
34644 +               AuDbg("%d\n", err);
34645 +               u = err;
34646 +               err = u % nbr;
34647 +               AuDbg("%d\n", err);
34648 +               if (!au_br_rdonly(au_sbr(sb, err)))
34649 +                       break;
34650 +               err = -EROFS;
34651 +       }
34652 +
34653 +       if (err >= 0)
34654 +               err = au_wbr_nonopq(dentry, err);
34655 +
34656 +out:
34657 +       AuDbg("%d\n", err);
34658 +       return err;
34659 +}
34660 +
34661 +/* ---------------------------------------------------------------------- */
34662 +
34663 +/* most free space */
34664 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34665 +{
34666 +       struct super_block *sb;
34667 +       struct au_branch *br;
34668 +       struct au_wbr_mfs *mfs;
34669 +       struct dentry *h_parent;
34670 +       aufs_bindex_t bindex, bbot;
34671 +       int err;
34672 +       unsigned long long b, bavail;
34673 +       struct path h_path;
34674 +       /* reduce the stack usage */
34675 +       struct kstatfs *st;
34676 +
34677 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34678 +       if (unlikely(!st)) {
34679 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34680 +               return;
34681 +       }
34682 +
34683 +       bavail = 0;
34684 +       sb = dentry->d_sb;
34685 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34686 +       MtxMustLock(&mfs->mfs_lock);
34687 +       mfs->mfs_bindex = -EROFS;
34688 +       mfs->mfsrr_bytes = 0;
34689 +       if (!parent) {
34690 +               bindex = 0;
34691 +               bbot = au_sbbot(sb);
34692 +       } else {
34693 +               bindex = au_dbtop(parent);
34694 +               bbot = au_dbtaildir(parent);
34695 +       }
34696 +
34697 +       for (; bindex <= bbot; bindex++) {
34698 +               if (parent) {
34699 +                       h_parent = au_h_dptr(parent, bindex);
34700 +                       if (!h_parent || d_is_negative(h_parent))
34701 +                               continue;
34702 +               }
34703 +               br = au_sbr(sb, bindex);
34704 +               if (au_br_rdonly(br))
34705 +                       continue;
34706 +
34707 +               /* sb->s_root for NFS is unreliable */
34708 +               h_path.mnt = au_br_mnt(br);
34709 +               h_path.dentry = h_path.mnt->mnt_root;
34710 +               err = vfs_statfs(&h_path, st);
34711 +               if (unlikely(err)) {
34712 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34713 +                       continue;
34714 +               }
34715 +
34716 +               /* when the available size is equal, select the lower one */
34717 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34718 +                            || sizeof(b) < sizeof(st->f_bsize));
34719 +               b = st->f_bavail * st->f_bsize;
34720 +               br->br_wbr->wbr_bytes = b;
34721 +               if (b >= bavail) {
34722 +                       bavail = b;
34723 +                       mfs->mfs_bindex = bindex;
34724 +                       mfs->mfs_jiffy = jiffies;
34725 +               }
34726 +       }
34727 +
34728 +       mfs->mfsrr_bytes = bavail;
34729 +       AuDbg("b%d\n", mfs->mfs_bindex);
34730 +       au_kfree_rcu(st);
34731 +}
34732 +
34733 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34734 +{
34735 +       int err;
34736 +       struct dentry *parent;
34737 +       struct super_block *sb;
34738 +       struct au_wbr_mfs *mfs;
34739 +
34740 +       err = au_wbr_create_exp(dentry);
34741 +       if (err >= 0)
34742 +               goto out;
34743 +
34744 +       sb = dentry->d_sb;
34745 +       parent = NULL;
34746 +       if (au_ftest_wbr(flags, PARENT))
34747 +               parent = dget_parent(dentry);
34748 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34749 +       mutex_lock(&mfs->mfs_lock);
34750 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34751 +           || mfs->mfs_bindex < 0
34752 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34753 +               au_mfs(dentry, parent);
34754 +       mutex_unlock(&mfs->mfs_lock);
34755 +       err = mfs->mfs_bindex;
34756 +       dput(parent);
34757 +
34758 +       if (err >= 0)
34759 +               err = au_wbr_nonopq(dentry, err);
34760 +
34761 +out:
34762 +       AuDbg("b%d\n", err);
34763 +       return err;
34764 +}
34765 +
34766 +static int au_wbr_create_init_mfs(struct super_block *sb)
34767 +{
34768 +       struct au_wbr_mfs *mfs;
34769 +
34770 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34771 +       mutex_init(&mfs->mfs_lock);
34772 +       mfs->mfs_jiffy = 0;
34773 +       mfs->mfs_bindex = -EROFS;
34774 +
34775 +       return 0;
34776 +}
34777 +
34778 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34779 +{
34780 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34781 +       return 0;
34782 +}
34783 +
34784 +/* ---------------------------------------------------------------------- */
34785 +
34786 +/* top down regardless parent, and then mfs */
34787 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34788 +                              unsigned int flags __maybe_unused)
34789 +{
34790 +       int err;
34791 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34792 +       unsigned long long watermark;
34793 +       struct super_block *sb;
34794 +       struct au_wbr_mfs *mfs;
34795 +       struct au_branch *br;
34796 +       struct dentry *parent;
34797 +
34798 +       sb = dentry->d_sb;
34799 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34800 +       mutex_lock(&mfs->mfs_lock);
34801 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34802 +           || mfs->mfs_bindex < 0)
34803 +               au_mfs(dentry, /*parent*/NULL);
34804 +       watermark = mfs->mfsrr_watermark;
34805 +       bmfs = mfs->mfs_bindex;
34806 +       mutex_unlock(&mfs->mfs_lock);
34807 +
34808 +       /* another style of au_wbr_create_exp() */
34809 +       bwh = au_dbwh(dentry);
34810 +       parent = dget_parent(dentry);
34811 +       btail = au_dbtaildir(parent);
34812 +       if (bwh >= 0 && bwh < btail)
34813 +               btail = bwh;
34814 +
34815 +       err = au_wbr_nonopq(dentry, btail);
34816 +       if (unlikely(err < 0))
34817 +               goto out;
34818 +       btail = err;
34819 +       bfound = -1;
34820 +       for (bindex = 0; bindex <= btail; bindex++) {
34821 +               br = au_sbr(sb, bindex);
34822 +               if (au_br_rdonly(br))
34823 +                       continue;
34824 +               if (br->br_wbr->wbr_bytes > watermark) {
34825 +                       bfound = bindex;
34826 +                       break;
34827 +               }
34828 +       }
34829 +       err = bfound;
34830 +       if (err < 0)
34831 +               err = bmfs;
34832 +
34833 +out:
34834 +       dput(parent);
34835 +       AuDbg("b%d\n", err);
34836 +       return err;
34837 +}
34838 +
34839 +/* ---------------------------------------------------------------------- */
34840 +
34841 +/* most free space and then round robin */
34842 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34843 +{
34844 +       int err;
34845 +       struct au_wbr_mfs *mfs;
34846 +
34847 +       err = au_wbr_create_mfs(dentry, flags);
34848 +       if (err >= 0) {
34849 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34850 +               mutex_lock(&mfs->mfs_lock);
34851 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34852 +                       err = au_wbr_create_rr(dentry, flags);
34853 +               mutex_unlock(&mfs->mfs_lock);
34854 +       }
34855 +
34856 +       AuDbg("b%d\n", err);
34857 +       return err;
34858 +}
34859 +
34860 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34861 +{
34862 +       int err;
34863 +
34864 +       au_wbr_create_init_mfs(sb); /* ignore */
34865 +       err = au_wbr_create_init_rr(sb);
34866 +
34867 +       return err;
34868 +}
34869 +
34870 +/* ---------------------------------------------------------------------- */
34871 +
34872 +/* top down parent and most free space */
34873 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34874 +{
34875 +       int err, e2;
34876 +       unsigned long long b;
34877 +       aufs_bindex_t bindex, btop, bbot;
34878 +       struct super_block *sb;
34879 +       struct dentry *parent, *h_parent;
34880 +       struct au_branch *br;
34881 +
34882 +       err = au_wbr_create_tdp(dentry, flags);
34883 +       if (unlikely(err < 0))
34884 +               goto out;
34885 +       parent = dget_parent(dentry);
34886 +       btop = au_dbtop(parent);
34887 +       bbot = au_dbtaildir(parent);
34888 +       if (btop == bbot)
34889 +               goto out_parent; /* success */
34890 +
34891 +       e2 = au_wbr_create_mfs(dentry, flags);
34892 +       if (e2 < 0)
34893 +               goto out_parent; /* success */
34894 +
34895 +       /* when the available size is equal, select upper one */
34896 +       sb = dentry->d_sb;
34897 +       br = au_sbr(sb, err);
34898 +       b = br->br_wbr->wbr_bytes;
34899 +       AuDbg("b%d, %llu\n", err, b);
34900 +
34901 +       for (bindex = btop; bindex <= bbot; bindex++) {
34902 +               h_parent = au_h_dptr(parent, bindex);
34903 +               if (!h_parent || d_is_negative(h_parent))
34904 +                       continue;
34905 +
34906 +               br = au_sbr(sb, bindex);
34907 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34908 +                       b = br->br_wbr->wbr_bytes;
34909 +                       err = bindex;
34910 +                       AuDbg("b%d, %llu\n", err, b);
34911 +               }
34912 +       }
34913 +
34914 +       if (err >= 0)
34915 +               err = au_wbr_nonopq(dentry, err);
34916 +
34917 +out_parent:
34918 +       dput(parent);
34919 +out:
34920 +       AuDbg("b%d\n", err);
34921 +       return err;
34922 +}
34923 +
34924 +/* ---------------------------------------------------------------------- */
34925 +
34926 +/*
34927 + * - top down parent
34928 + * - most free space with parent
34929 + * - most free space round-robin regardless parent
34930 + */
34931 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34932 +{
34933 +       int err;
34934 +       unsigned long long watermark;
34935 +       struct super_block *sb;
34936 +       struct au_branch *br;
34937 +       struct au_wbr_mfs *mfs;
34938 +
34939 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34940 +       if (unlikely(err < 0))
34941 +               goto out;
34942 +
34943 +       sb = dentry->d_sb;
34944 +       br = au_sbr(sb, err);
34945 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34946 +       mutex_lock(&mfs->mfs_lock);
34947 +       watermark = mfs->mfsrr_watermark;
34948 +       mutex_unlock(&mfs->mfs_lock);
34949 +       if (br->br_wbr->wbr_bytes < watermark)
34950 +               /* regardless the parent dir */
34951 +               err = au_wbr_create_mfsrr(dentry, flags);
34952 +
34953 +out:
34954 +       AuDbg("b%d\n", err);
34955 +       return err;
34956 +}
34957 +
34958 +/* ---------------------------------------------------------------------- */
34959 +
34960 +/* policies for copyup */
34961 +
34962 +/* top down parent */
34963 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34964 +{
34965 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34966 +}
34967 +
34968 +/* bottom up parent */
34969 +static int au_wbr_copyup_bup(struct dentry *dentry)
34970 +{
34971 +       int err;
34972 +       aufs_bindex_t bindex, btop;
34973 +       struct dentry *parent, *h_parent;
34974 +       struct super_block *sb;
34975 +
34976 +       err = -EROFS;
34977 +       sb = dentry->d_sb;
34978 +       parent = dget_parent(dentry);
34979 +       btop = au_dbtop(parent);
34980 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34981 +               h_parent = au_h_dptr(parent, bindex);
34982 +               if (!h_parent || d_is_negative(h_parent))
34983 +                       continue;
34984 +
34985 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34986 +                       err = bindex;
34987 +                       break;
34988 +               }
34989 +       }
34990 +       dput(parent);
34991 +
34992 +       /* bottom up here */
34993 +       if (unlikely(err < 0))
34994 +               err = au_wbr_bu(sb, btop - 1);
34995 +
34996 +       AuDbg("b%d\n", err);
34997 +       return err;
34998 +}
34999 +
35000 +/* bottom up */
35001 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
35002 +{
35003 +       int err;
35004 +
35005 +       err = au_wbr_bu(dentry->d_sb, btop);
35006 +       AuDbg("b%d\n", err);
35007 +       if (err > btop)
35008 +               err = au_wbr_nonopq(dentry, err);
35009 +
35010 +       AuDbg("b%d\n", err);
35011 +       return err;
35012 +}
35013 +
35014 +static int au_wbr_copyup_bu(struct dentry *dentry)
35015 +{
35016 +       int err;
35017 +       aufs_bindex_t btop;
35018 +
35019 +       btop = au_dbtop(dentry);
35020 +       err = au_wbr_do_copyup_bu(dentry, btop);
35021 +       return err;
35022 +}
35023 +
35024 +/* ---------------------------------------------------------------------- */
35025 +
35026 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
35027 +       [AuWbrCopyup_TDP] = {
35028 +               .copyup = au_wbr_copyup_tdp
35029 +       },
35030 +       [AuWbrCopyup_BUP] = {
35031 +               .copyup = au_wbr_copyup_bup
35032 +       },
35033 +       [AuWbrCopyup_BU] = {
35034 +               .copyup = au_wbr_copyup_bu
35035 +       }
35036 +};
35037 +
35038 +struct au_wbr_create_operations au_wbr_create_ops[] = {
35039 +       [AuWbrCreate_TDP] = {
35040 +               .create = au_wbr_create_tdp
35041 +       },
35042 +       [AuWbrCreate_RR] = {
35043 +               .create = au_wbr_create_rr,
35044 +               .init   = au_wbr_create_init_rr
35045 +       },
35046 +       [AuWbrCreate_MFS] = {
35047 +               .create = au_wbr_create_mfs,
35048 +               .init   = au_wbr_create_init_mfs,
35049 +               .fin    = au_wbr_create_fin_mfs
35050 +       },
35051 +       [AuWbrCreate_MFSV] = {
35052 +               .create = au_wbr_create_mfs,
35053 +               .init   = au_wbr_create_init_mfs,
35054 +               .fin    = au_wbr_create_fin_mfs
35055 +       },
35056 +       [AuWbrCreate_MFSRR] = {
35057 +               .create = au_wbr_create_mfsrr,
35058 +               .init   = au_wbr_create_init_mfsrr,
35059 +               .fin    = au_wbr_create_fin_mfs
35060 +       },
35061 +       [AuWbrCreate_MFSRRV] = {
35062 +               .create = au_wbr_create_mfsrr,
35063 +               .init   = au_wbr_create_init_mfsrr,
35064 +               .fin    = au_wbr_create_fin_mfs
35065 +       },
35066 +       [AuWbrCreate_TDMFS] = {
35067 +               .create = au_wbr_create_tdmfs,
35068 +               .init   = au_wbr_create_init_mfs,
35069 +               .fin    = au_wbr_create_fin_mfs
35070 +       },
35071 +       [AuWbrCreate_TDMFSV] = {
35072 +               .create = au_wbr_create_tdmfs,
35073 +               .init   = au_wbr_create_init_mfs,
35074 +               .fin    = au_wbr_create_fin_mfs
35075 +       },
35076 +       [AuWbrCreate_PMFS] = {
35077 +               .create = au_wbr_create_pmfs,
35078 +               .init   = au_wbr_create_init_mfs,
35079 +               .fin    = au_wbr_create_fin_mfs
35080 +       },
35081 +       [AuWbrCreate_PMFSV] = {
35082 +               .create = au_wbr_create_pmfs,
35083 +               .init   = au_wbr_create_init_mfs,
35084 +               .fin    = au_wbr_create_fin_mfs
35085 +       },
35086 +       [AuWbrCreate_PMFSRR] = {
35087 +               .create = au_wbr_create_pmfsrr,
35088 +               .init   = au_wbr_create_init_mfsrr,
35089 +               .fin    = au_wbr_create_fin_mfs
35090 +       },
35091 +       [AuWbrCreate_PMFSRRV] = {
35092 +               .create = au_wbr_create_pmfsrr,
35093 +               .init   = au_wbr_create_init_mfsrr,
35094 +               .fin    = au_wbr_create_fin_mfs
35095 +       }
35096 +};
35097 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
35098 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
35099 +++ linux/fs/aufs/whout.c       2023-09-03 02:21:58.863304341 +0200
35100 @@ -0,0 +1,1072 @@
35101 +// SPDX-License-Identifier: GPL-2.0
35102 +/*
35103 + * Copyright (C) 2005-2022 Junjiro R. Okajima
35104 + *
35105 + * This program is free software; you can redistribute it and/or modify
35106 + * it under the terms of the GNU General Public License as published by
35107 + * the Free Software Foundation; either version 2 of the License, or
35108 + * (at your option) any later version.
35109 + *
35110 + * This program is distributed in the hope that it will be useful,
35111 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35112 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35113 + * GNU General Public License for more details.
35114 + *
35115 + * You should have received a copy of the GNU General Public License
35116 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35117 + */
35118 +
35119 +/*
35120 + * whiteout for logical deletion and opaque directory
35121 + */
35122 +
35123 +#include "aufs.h"
35124 +
35125 +#define WH_MASK                        0444
35126 +
35127 +/*
35128 + * If a directory contains this file, then it is opaque.  We start with the
35129 + * .wh. flag so that it is blocked by lookup.
35130 + */
35131 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35132 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
35133 +
35134 +/*
35135 + * generate whiteout name, which is NOT terminated by NULL.
35136 + * @name: original d_name.name
35137 + * @len: original d_name.len
35138 + * @wh: whiteout qstr
35139 + * returns zero when succeeds, otherwise error.
35140 + * succeeded value as wh->name should be freed by kfree().
35141 + */
35142 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35143 +{
35144 +       char *p;
35145 +
35146 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35147 +               return -ENAMETOOLONG;
35148 +
35149 +       wh->len = name->len + AUFS_WH_PFX_LEN;
35150 +       p = kmalloc(wh->len, GFP_NOFS);
35151 +       wh->name = p;
35152 +       if (p) {
35153 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35154 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35155 +               /* smp_mb(); */
35156 +               return 0;
35157 +       }
35158 +       return -ENOMEM;
35159 +}
35160 +
35161 +/* ---------------------------------------------------------------------- */
35162 +
35163 +/*
35164 + * test if the @wh_name exists under @h_ppath.
35165 + * @try_sio specifies the necessary of super-io.
35166 + */
35167 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
35168 +              struct qstr *wh_name, int try_sio)
35169 +{
35170 +       int err;
35171 +       struct dentry *wh_dentry;
35172 +
35173 +       if (!try_sio)
35174 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
35175 +       else
35176 +               wh_dentry = au_sio_lkup_one(h_idmap, wh_name, h_ppath);
35177 +       err = PTR_ERR(wh_dentry);
35178 +       if (IS_ERR(wh_dentry)) {
35179 +               if (err == -ENAMETOOLONG)
35180 +                       err = 0;
35181 +               goto out;
35182 +       }
35183 +
35184 +       err = 0;
35185 +       if (d_is_negative(wh_dentry))
35186 +               goto out_wh; /* success */
35187 +
35188 +       err = 1;
35189 +       if (d_is_reg(wh_dentry))
35190 +               goto out_wh; /* success */
35191 +
35192 +       err = -EIO;
35193 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35194 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35195 +
35196 +out_wh:
35197 +       dput(wh_dentry);
35198 +out:
35199 +       return err;
35200 +}
35201 +
35202 +/*
35203 + * test if the @h_path->dentry sets opaque or not.
35204 + */
35205 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path)
35206 +{
35207 +       int err;
35208 +       struct inode *h_dir;
35209 +
35210 +       h_dir = d_inode(h_path->dentry);
35211 +       err = au_wh_test(h_idmap, h_path, &diropq_name,
35212 +                        au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC));
35213 +       return err;
35214 +}
35215 +
35216 +/*
35217 + * returns a negative dentry whose name is unique and temporary.
35218 + */
35219 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35220 +                            struct qstr *prefix)
35221 +{
35222 +       struct dentry *dentry;
35223 +       int i;
35224 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35225 +               *name, *p;
35226 +       /* strict atomic_t is unnecessary here */
35227 +       static unsigned short cnt;
35228 +       struct qstr qs;
35229 +       struct path h_ppath;
35230 +       struct mnt_idmap *h_idmap;
35231 +
35232 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35233 +
35234 +       name = defname;
35235 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35236 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35237 +               dentry = ERR_PTR(-ENAMETOOLONG);
35238 +               if (unlikely(qs.len > NAME_MAX))
35239 +                       goto out;
35240 +               dentry = ERR_PTR(-ENOMEM);
35241 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35242 +               if (unlikely(!name))
35243 +                       goto out;
35244 +       }
35245 +
35246 +       /* doubly whiteout-ed */
35247 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35248 +       p = name + AUFS_WH_PFX_LEN * 2;
35249 +       memcpy(p, prefix->name, prefix->len);
35250 +       p += prefix->len;
35251 +       *p++ = '.';
35252 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35253 +
35254 +       h_ppath.dentry = h_parent;
35255 +       h_ppath.mnt = au_br_mnt(br);
35256 +       h_idmap = au_br_idmap(br);
35257 +       qs.name = name;
35258 +       for (i = 0; i < 3; i++) {
35259 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35260 +               dentry = au_sio_lkup_one(h_idmap, &qs, &h_ppath);
35261 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35262 +                       goto out_name;
35263 +               dput(dentry);
35264 +       }
35265 +       /* pr_warn("could not get random name\n"); */
35266 +       dentry = ERR_PTR(-EEXIST);
35267 +       AuDbg("%.*s\n", AuLNPair(&qs));
35268 +       BUG();
35269 +
35270 +out_name:
35271 +       if (name != defname)
35272 +               au_kfree_try_rcu(name);
35273 +out:
35274 +       AuTraceErrPtr(dentry);
35275 +       return dentry;
35276 +}
35277 +
35278 +/*
35279 + * rename the @h_dentry on @br to the whiteouted temporary name.
35280 + */
35281 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35282 +{
35283 +       int err;
35284 +       struct path h_path = {
35285 +               .mnt = au_br_mnt(br)
35286 +       };
35287 +       struct inode *h_dir, *delegated;
35288 +       struct dentry *h_parent;
35289 +
35290 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35291 +       h_dir = d_inode(h_parent);
35292 +       IMustLock(h_dir);
35293 +
35294 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35295 +       err = PTR_ERR(h_path.dentry);
35296 +       if (IS_ERR(h_path.dentry))
35297 +               goto out;
35298 +
35299 +       /* under the same dir, no need to lock_rename() */
35300 +       delegated = NULL;
35301 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35302 +                          /*flags*/0);
35303 +       AuTraceErr(err);
35304 +       if (unlikely(err == -EWOULDBLOCK)) {
35305 +               pr_warn("cannot retry for NFSv4 delegation"
35306 +                       " for an internal rename\n");
35307 +               iput(delegated);
35308 +       }
35309 +       dput(h_path.dentry);
35310 +
35311 +out:
35312 +       AuTraceErr(err);
35313 +       return err;
35314 +}
35315 +
35316 +/* ---------------------------------------------------------------------- */
35317 +/*
35318 + * functions for removing a whiteout
35319 + */
35320 +
35321 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35322 +{
35323 +       int err, force;
35324 +       struct inode *delegated;
35325 +
35326 +       /*
35327 +        * forces superio when the dir has a sticky bit.
35328 +        * this may be a violation of unix fs semantics.
35329 +        */
35330 +       force = (h_dir->i_mode & S_ISVTX)
35331 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35332 +       delegated = NULL;
35333 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35334 +       if (unlikely(err == -EWOULDBLOCK)) {
35335 +               pr_warn("cannot retry for NFSv4 delegation"
35336 +                       " for an internal unlink\n");
35337 +               iput(delegated);
35338 +       }
35339 +       return err;
35340 +}
35341 +
35342 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35343 +                       struct dentry *dentry)
35344 +{
35345 +       int err;
35346 +
35347 +       err = do_unlink_wh(h_dir, h_path);
35348 +       if (!err && dentry)
35349 +               au_set_dbwh(dentry, -1);
35350 +
35351 +       return err;
35352 +}
35353 +
35354 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35355 +{
35356 +       int err;
35357 +       struct path h_path;
35358 +
35359 +       err = 0;
35360 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35361 +       if (IS_ERR(h_path.dentry))
35362 +               err = PTR_ERR(h_path.dentry);
35363 +       else {
35364 +               if (d_is_reg(h_path.dentry)) {
35365 +                       h_path.mnt = h_ppath->mnt;
35366 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35367 +               }
35368 +               dput(h_path.dentry);
35369 +       }
35370 +
35371 +       return err;
35372 +}
35373 +
35374 +/* ---------------------------------------------------------------------- */
35375 +/*
35376 + * initialize/clean whiteout for a branch
35377 + */
35378 +
35379 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35380 +                       const int isdir)
35381 +{
35382 +       int err;
35383 +       struct inode *delegated;
35384 +
35385 +       if (d_is_negative(whpath->dentry))
35386 +               return;
35387 +
35388 +       if (isdir)
35389 +               err = vfsub_rmdir(h_dir, whpath);
35390 +       else {
35391 +               delegated = NULL;
35392 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35393 +               if (unlikely(err == -EWOULDBLOCK)) {
35394 +                       pr_warn("cannot retry for NFSv4 delegation"
35395 +                               " for an internal unlink\n");
35396 +                       iput(delegated);
35397 +               }
35398 +       }
35399 +       if (unlikely(err))
35400 +               pr_warn("failed removing %pd (%d), ignored.\n",
35401 +                       whpath->dentry, err);
35402 +}
35403 +
35404 +static int test_linkable(struct dentry *h_root)
35405 +{
35406 +       struct inode *h_dir = d_inode(h_root);
35407 +
35408 +       if (h_dir->i_op->link)
35409 +               return 0;
35410 +
35411 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35412 +              h_root, au_sbtype(h_root->d_sb));
35413 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35414 +}
35415 +
35416 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35417 +static int au_whdir(struct inode *h_dir, struct path *path)
35418 +{
35419 +       int err;
35420 +
35421 +       err = -EEXIST;
35422 +       if (d_is_negative(path->dentry)) {
35423 +               int mode = 0700;
35424 +
35425 +               if (au_test_nfs(path->dentry->d_sb))
35426 +                       mode |= 0111;
35427 +               err = vfsub_mkdir(h_dir, path, mode);
35428 +       } else if (d_is_dir(path->dentry))
35429 +               err = 0;
35430 +       else
35431 +               pr_err("unknown %pd exists\n", path->dentry);
35432 +
35433 +       return err;
35434 +}
35435 +
35436 +struct au_wh_base {
35437 +       const struct qstr *name;
35438 +       struct dentry *dentry;
35439 +};
35440 +
35441 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35442 +                         struct path *h_path)
35443 +{
35444 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35445 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35446 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35447 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35448 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35449 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35450 +}
35451 +
35452 +/*
35453 + * returns tri-state,
35454 + * minus: error, caller should print the message
35455 + * zero: success
35456 + * plus: error, caller should NOT print the message
35457 + */
35458 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35459 +                               int do_plink, struct au_wh_base base[],
35460 +                               struct path *h_path)
35461 +{
35462 +       int err;
35463 +       struct inode *h_dir;
35464 +
35465 +       h_dir = d_inode(h_root);
35466 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35467 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35468 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35469 +       if (do_plink) {
35470 +               err = test_linkable(h_root);
35471 +               if (unlikely(err)) {
35472 +                       err = 1;
35473 +                       goto out;
35474 +               }
35475 +
35476 +               err = au_whdir(h_dir, h_path);
35477 +               if (unlikely(err))
35478 +                       goto out;
35479 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35480 +       } else
35481 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35482 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35483 +       err = au_whdir(h_dir, h_path);
35484 +       if (unlikely(err))
35485 +               goto out;
35486 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35487 +
35488 +out:
35489 +       return err;
35490 +}
35491 +
35492 +/*
35493 + * for the moment, aufs supports the branch filesystem which does not support
35494 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35495 + * copyup failed. finally, such filesystem will not be used as the writable
35496 + * branch.
35497 + *
35498 + * returns tri-state, see above.
35499 + */
35500 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35501 +                        int do_plink, struct au_wh_base base[],
35502 +                        struct path *h_path)
35503 +{
35504 +       int err;
35505 +       struct inode *h_dir;
35506 +
35507 +       WbrWhMustWriteLock(wbr);
35508 +
35509 +       err = test_linkable(h_root);
35510 +       if (unlikely(err)) {
35511 +               err = 1;
35512 +               goto out;
35513 +       }
35514 +
35515 +       /*
35516 +        * todo: should this create be done in /sbin/mount.aufs helper?
35517 +        */
35518 +       err = -EEXIST;
35519 +       h_dir = d_inode(h_root);
35520 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35521 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35522 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35523 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35524 +               err = 0;
35525 +       else
35526 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35527 +       if (unlikely(err))
35528 +               goto out;
35529 +
35530 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35531 +       if (do_plink) {
35532 +               err = au_whdir(h_dir, h_path);
35533 +               if (unlikely(err))
35534 +                       goto out;
35535 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35536 +       } else
35537 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35538 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35539 +
35540 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35541 +       err = au_whdir(h_dir, h_path);
35542 +       if (unlikely(err))
35543 +               goto out;
35544 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35545 +
35546 +out:
35547 +       return err;
35548 +}
35549 +
35550 +/*
35551 + * initialize the whiteout base file/dir for @br.
35552 + */
35553 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35554 +{
35555 +       int err, i;
35556 +       const unsigned char do_plink
35557 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35558 +       struct inode *h_dir;
35559 +       struct path path = br->br_path;
35560 +       struct dentry *h_root = path.dentry;
35561 +       struct au_wbr *wbr = br->br_wbr;
35562 +       static const struct qstr base_name[] = {
35563 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35564 +                                         sizeof(AUFS_BASE_NAME) - 1),
35565 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35566 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35567 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35568 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35569 +       };
35570 +       struct au_wh_base base[] = {
35571 +               [AuBrWh_BASE] = {
35572 +                       .name   = base_name + AuBrWh_BASE,
35573 +                       .dentry = NULL
35574 +               },
35575 +               [AuBrWh_PLINK] = {
35576 +                       .name   = base_name + AuBrWh_PLINK,
35577 +                       .dentry = NULL
35578 +               },
35579 +               [AuBrWh_ORPH] = {
35580 +                       .name   = base_name + AuBrWh_ORPH,
35581 +                       .dentry = NULL
35582 +               }
35583 +       };
35584 +
35585 +       if (wbr)
35586 +               WbrWhMustWriteLock(wbr);
35587 +
35588 +       for (i = 0; i < AuBrWh_Last; i++) {
35589 +               /* doubly whiteouted */
35590 +               struct dentry *d;
35591 +
35592 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35593 +               err = PTR_ERR(d);
35594 +               if (IS_ERR(d))
35595 +                       goto out;
35596 +
35597 +               base[i].dentry = d;
35598 +               AuDebugOn(wbr
35599 +                         && wbr->wbr_wh[i]
35600 +                         && wbr->wbr_wh[i] != base[i].dentry);
35601 +       }
35602 +
35603 +       if (wbr)
35604 +               for (i = 0; i < AuBrWh_Last; i++) {
35605 +                       dput(wbr->wbr_wh[i]);
35606 +                       wbr->wbr_wh[i] = NULL;
35607 +               }
35608 +
35609 +       err = 0;
35610 +       if (!au_br_writable(br->br_perm)) {
35611 +               h_dir = d_inode(h_root);
35612 +               au_wh_init_ro(h_dir, base, &path);
35613 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35614 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35615 +               if (err > 0)
35616 +                       goto out;
35617 +               else if (err)
35618 +                       goto out_err;
35619 +       } else {
35620 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35621 +               if (err > 0)
35622 +                       goto out;
35623 +               else if (err)
35624 +                       goto out_err;
35625 +       }
35626 +       goto out; /* success */
35627 +
35628 +out_err:
35629 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35630 +              err, h_root, au_sbtype(h_root->d_sb));
35631 +out:
35632 +       for (i = 0; i < AuBrWh_Last; i++)
35633 +               dput(base[i].dentry);
35634 +       return err;
35635 +}
35636 +
35637 +/* ---------------------------------------------------------------------- */
35638 +/*
35639 + * whiteouts are all hard-linked usually.
35640 + * when its link count reaches a ceiling, we create a new whiteout base
35641 + * asynchronously.
35642 + */
35643 +
35644 +struct reinit_br_wh {
35645 +       struct super_block *sb;
35646 +       struct au_branch *br;
35647 +};
35648 +
35649 +static void reinit_br_wh(void *arg)
35650 +{
35651 +       int err;
35652 +       aufs_bindex_t bindex;
35653 +       struct path h_path;
35654 +       struct reinit_br_wh *a = arg;
35655 +       struct au_wbr *wbr;
35656 +       struct inode *dir, *delegated;
35657 +       struct dentry *h_root;
35658 +       struct au_hinode *hdir;
35659 +
35660 +       err = 0;
35661 +       wbr = a->br->br_wbr;
35662 +       /* big aufs lock */
35663 +       si_noflush_write_lock(a->sb);
35664 +       if (!au_br_writable(a->br->br_perm))
35665 +               goto out;
35666 +       bindex = au_br_index(a->sb, a->br->br_id);
35667 +       if (unlikely(bindex < 0))
35668 +               goto out;
35669 +
35670 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35671 +       dir = d_inode(a->sb->s_root);
35672 +       hdir = au_hi(dir, bindex);
35673 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35674 +       AuDebugOn(h_root != au_br_dentry(a->br));
35675 +
35676 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35677 +       wbr_wh_write_lock(wbr);
35678 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35679 +                         h_root, a->br);
35680 +       if (!err) {
35681 +               h_path.dentry = wbr->wbr_whbase;
35682 +               h_path.mnt = au_br_mnt(a->br);
35683 +               delegated = NULL;
35684 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35685 +                                  /*force*/0);
35686 +               if (unlikely(err == -EWOULDBLOCK)) {
35687 +                       pr_warn("cannot retry for NFSv4 delegation"
35688 +                               " for an internal unlink\n");
35689 +                       iput(delegated);
35690 +               }
35691 +       } else {
35692 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35693 +               err = 0;
35694 +       }
35695 +       dput(wbr->wbr_whbase);
35696 +       wbr->wbr_whbase = NULL;
35697 +       if (!err)
35698 +               err = au_wh_init(a->br, a->sb);
35699 +       wbr_wh_write_unlock(wbr);
35700 +       au_hn_inode_unlock(hdir);
35701 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35702 +       if (!err)
35703 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35704 +
35705 +out:
35706 +       if (wbr)
35707 +               atomic_dec(&wbr->wbr_wh_running);
35708 +       au_lcnt_dec(&a->br->br_count);
35709 +       si_write_unlock(a->sb);
35710 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35711 +       au_kfree_rcu(a);
35712 +       if (unlikely(err))
35713 +               AuIOErr("err %d\n", err);
35714 +}
35715 +
35716 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35717 +{
35718 +       int do_dec, wkq_err;
35719 +       struct reinit_br_wh *arg;
35720 +
35721 +       do_dec = 1;
35722 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35723 +               goto out;
35724 +
35725 +       /* ignore ENOMEM */
35726 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35727 +       if (arg) {
35728 +               /*
35729 +                * dec(wh_running), kfree(arg) and dec(br_count)
35730 +                * in reinit function
35731 +                */
35732 +               arg->sb = sb;
35733 +               arg->br = br;
35734 +               au_lcnt_inc(&br->br_count);
35735 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35736 +               if (unlikely(wkq_err)) {
35737 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35738 +                       au_lcnt_dec(&br->br_count);
35739 +                       au_kfree_rcu(arg);
35740 +               }
35741 +               do_dec = 0;
35742 +       }
35743 +
35744 +out:
35745 +       if (do_dec)
35746 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35747 +}
35748 +
35749 +/* ---------------------------------------------------------------------- */
35750 +
35751 +/*
35752 + * create the whiteout @wh.
35753 + */
35754 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35755 +                            struct dentry *wh)
35756 +{
35757 +       int err;
35758 +       struct path h_path = {
35759 +               .dentry = wh
35760 +       };
35761 +       struct au_branch *br;
35762 +       struct au_wbr *wbr;
35763 +       struct dentry *h_parent;
35764 +       struct inode *h_dir, *delegated;
35765 +
35766 +       h_parent = wh->d_parent; /* dir inode is locked */
35767 +       h_dir = d_inode(h_parent);
35768 +       IMustLock(h_dir);
35769 +
35770 +       br = au_sbr(sb, bindex);
35771 +       h_path.mnt = au_br_mnt(br);
35772 +       wbr = br->br_wbr;
35773 +       wbr_wh_read_lock(wbr);
35774 +       if (wbr->wbr_whbase) {
35775 +               delegated = NULL;
35776 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35777 +               if (unlikely(err == -EWOULDBLOCK)) {
35778 +                       pr_warn("cannot retry for NFSv4 delegation"
35779 +                               " for an internal link\n");
35780 +                       iput(delegated);
35781 +               }
35782 +               if (!err || err != -EMLINK)
35783 +                       goto out;
35784 +
35785 +               /* link count full. re-initialize br_whbase. */
35786 +               kick_reinit_br_wh(sb, br);
35787 +       }
35788 +
35789 +       /* return this error in this context */
35790 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35791 +       if (!err)
35792 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35793 +
35794 +out:
35795 +       wbr_wh_read_unlock(wbr);
35796 +       return err;
35797 +}
35798 +
35799 +/* ---------------------------------------------------------------------- */
35800 +
35801 +/*
35802 + * create or remove the diropq.
35803 + */
35804 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35805 +                               unsigned int flags)
35806 +{
35807 +       struct dentry *opq_dentry;
35808 +       struct super_block *sb;
35809 +       struct au_branch *br;
35810 +       struct path h_path;
35811 +       int err;
35812 +
35813 +       sb = dentry->d_sb;
35814 +       br = au_sbr(sb, bindex);
35815 +       h_path.dentry = au_h_dptr(dentry, bindex);
35816 +       h_path.mnt = au_br_mnt(br);
35817 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35818 +       if (IS_ERR(opq_dentry))
35819 +               goto out;
35820 +
35821 +       if (au_ftest_diropq(flags, CREATE)) {
35822 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35823 +               if (!err) {
35824 +                       au_set_dbdiropq(dentry, bindex);
35825 +                       goto out; /* success */
35826 +               }
35827 +       } else {
35828 +               h_path.dentry = opq_dentry;
35829 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35830 +               if (!err)
35831 +                       au_set_dbdiropq(dentry, -1);
35832 +       }
35833 +       dput(opq_dentry);
35834 +       opq_dentry = ERR_PTR(err);
35835 +
35836 +out:
35837 +       return opq_dentry;
35838 +}
35839 +
35840 +struct do_diropq_args {
35841 +       struct dentry **errp;
35842 +       struct dentry *dentry;
35843 +       aufs_bindex_t bindex;
35844 +       unsigned int flags;
35845 +};
35846 +
35847 +static void call_do_diropq(void *args)
35848 +{
35849 +       struct do_diropq_args *a = args;
35850 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35851 +}
35852 +
35853 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35854 +                            unsigned int flags)
35855 +{
35856 +       struct dentry *diropq, *h_dentry;
35857 +       struct mnt_idmap *h_idmap;
35858 +
35859 +       h_idmap = au_sbr_idmap(dentry->d_sb, bindex);
35860 +       h_dentry = au_h_dptr(dentry, bindex);
35861 +       if (!au_test_h_perm_sio(h_idmap, d_inode(h_dentry),
35862 +                               MAY_EXEC | MAY_WRITE))
35863 +               diropq = do_diropq(dentry, bindex, flags);
35864 +       else {
35865 +               int wkq_err;
35866 +               struct do_diropq_args args = {
35867 +                       .errp           = &diropq,
35868 +                       .dentry         = dentry,
35869 +                       .bindex         = bindex,
35870 +                       .flags          = flags
35871 +               };
35872 +
35873 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35874 +               if (unlikely(wkq_err))
35875 +                       diropq = ERR_PTR(wkq_err);
35876 +       }
35877 +
35878 +       return diropq;
35879 +}
35880 +
35881 +/* ---------------------------------------------------------------------- */
35882 +
35883 +/*
35884 + * lookup whiteout dentry.
35885 + * @h_parent: lower parent dentry which must exist and be locked
35886 + * @base_name: name of dentry which will be whiteouted
35887 + * returns dentry for whiteout.
35888 + */
35889 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35890 +                         struct au_branch *br)
35891 +{
35892 +       int err;
35893 +       struct qstr wh_name;
35894 +       struct dentry *wh_dentry;
35895 +       struct path h_path;
35896 +
35897 +       err = au_wh_name_alloc(&wh_name, base_name);
35898 +       wh_dentry = ERR_PTR(err);
35899 +       if (!err) {
35900 +               h_path.dentry = h_parent;
35901 +               h_path.mnt = au_br_mnt(br);
35902 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35903 +               au_kfree_try_rcu(wh_name.name);
35904 +       }
35905 +       return wh_dentry;
35906 +}
35907 +
35908 +/*
35909 + * link/create a whiteout for @dentry on @bindex.
35910 + */
35911 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35912 +                           struct dentry *h_parent)
35913 +{
35914 +       struct dentry *wh_dentry;
35915 +       struct super_block *sb;
35916 +       int err;
35917 +
35918 +       sb = dentry->d_sb;
35919 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35920 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35921 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35922 +               if (!err) {
35923 +                       au_set_dbwh(dentry, bindex);
35924 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35925 +               } else {
35926 +                       dput(wh_dentry);
35927 +                       wh_dentry = ERR_PTR(err);
35928 +               }
35929 +       }
35930 +
35931 +       return wh_dentry;
35932 +}
35933 +
35934 +/* ---------------------------------------------------------------------- */
35935 +
35936 +/* Delete all whiteouts in this directory on branch bindex. */
35937 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35938 +                          aufs_bindex_t bindex)
35939 +{
35940 +       int err;
35941 +       unsigned long ul, n;
35942 +       struct qstr wh_name;
35943 +       char *p;
35944 +       struct hlist_head *head;
35945 +       struct au_vdir_wh *pos;
35946 +       struct au_vdir_destr *str;
35947 +
35948 +       err = -ENOMEM;
35949 +       p = (void *)__get_free_page(GFP_NOFS);
35950 +       wh_name.name = p;
35951 +       if (unlikely(!wh_name.name))
35952 +               goto out;
35953 +
35954 +       err = 0;
35955 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35956 +       p += AUFS_WH_PFX_LEN;
35957 +       n = whlist->nh_num;
35958 +       head = whlist->nh_head;
35959 +       for (ul = 0; !err && ul < n; ul++, head++) {
35960 +               hlist_for_each_entry(pos, head, wh_hash) {
35961 +                       if (pos->wh_bindex != bindex)
35962 +                               continue;
35963 +
35964 +                       str = &pos->wh_str;
35965 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35966 +                               memcpy(p, str->name, str->len);
35967 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35968 +                               err = unlink_wh_name(h_path, &wh_name);
35969 +                               if (!err)
35970 +                                       continue;
35971 +                               break;
35972 +                       }
35973 +                       AuIOErr("whiteout name too long %.*s\n",
35974 +                               str->len, str->name);
35975 +                       err = -EIO;
35976 +                       break;
35977 +               }
35978 +       }
35979 +       free_page((unsigned long)wh_name.name);
35980 +
35981 +out:
35982 +       return err;
35983 +}
35984 +
35985 +struct del_wh_children_args {
35986 +       int *errp;
35987 +       struct path *h_path;
35988 +       struct au_nhash *whlist;
35989 +       aufs_bindex_t bindex;
35990 +};
35991 +
35992 +static void call_del_wh_children(void *args)
35993 +{
35994 +       struct del_wh_children_args *a = args;
35995 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
35996 +}
35997 +
35998 +/* ---------------------------------------------------------------------- */
35999 +
36000 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
36001 +{
36002 +       struct au_whtmp_rmdir *whtmp;
36003 +       int err;
36004 +       unsigned int rdhash;
36005 +
36006 +       SiMustAnyLock(sb);
36007 +
36008 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
36009 +       if (unlikely(!whtmp)) {
36010 +               whtmp = ERR_PTR(-ENOMEM);
36011 +               goto out;
36012 +       }
36013 +
36014 +       /* no estimation for dir size */
36015 +       rdhash = au_sbi(sb)->si_rdhash;
36016 +       if (!rdhash)
36017 +               rdhash = AUFS_RDHASH_DEF;
36018 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
36019 +       if (unlikely(err)) {
36020 +               au_kfree_rcu(whtmp);
36021 +               whtmp = ERR_PTR(err);
36022 +       }
36023 +
36024 +out:
36025 +       return whtmp;
36026 +}
36027 +
36028 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
36029 +{
36030 +       if (whtmp->br)
36031 +               au_lcnt_dec(&whtmp->br->br_count);
36032 +       dput(whtmp->wh_dentry);
36033 +       iput(whtmp->dir);
36034 +       au_nhash_wh_free(&whtmp->whlist);
36035 +       au_kfree_rcu(whtmp);
36036 +}
36037 +
36038 +/*
36039 + * rmdir the whiteouted temporary named dir @h_dentry.
36040 + * @whlist: whiteouted children.
36041 + */
36042 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36043 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
36044 +{
36045 +       int err;
36046 +       unsigned int h_nlink;
36047 +       struct path wh_path;
36048 +       struct inode *wh_inode, *h_dir;
36049 +       struct au_branch *br;
36050 +       struct mnt_idmap *h_idmap;
36051 +
36052 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
36053 +       IMustLock(h_dir);
36054 +
36055 +       br = au_sbr(dir->i_sb, bindex);
36056 +       wh_path.dentry = wh_dentry;
36057 +       wh_path.mnt = au_br_mnt(br);
36058 +       h_idmap = au_br_idmap(br);
36059 +       wh_inode = d_inode(wh_dentry);
36060 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
36061 +
36062 +       /*
36063 +        * someone else might change some whiteouts while we were sleeping.
36064 +        * it means this whlist may have an obsoleted entry.
36065 +        */
36066 +       if (!au_test_h_perm_sio(h_idmap, wh_inode, MAY_EXEC | MAY_WRITE))
36067 +               err = del_wh_children(&wh_path, whlist, bindex);
36068 +       else {
36069 +               int wkq_err;
36070 +               struct del_wh_children_args args = {
36071 +                       .errp           = &err,
36072 +                       .h_path         = &wh_path,
36073 +                       .whlist         = whlist,
36074 +                       .bindex         = bindex
36075 +               };
36076 +
36077 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
36078 +               if (unlikely(wkq_err))
36079 +                       err = wkq_err;
36080 +       }
36081 +       inode_unlock(wh_inode);
36082 +
36083 +       if (!err) {
36084 +               h_nlink = h_dir->i_nlink;
36085 +               err = vfsub_rmdir(h_dir, &wh_path);
36086 +               /* some fs doesn't change the parent nlink in some cases */
36087 +               h_nlink -= h_dir->i_nlink;
36088 +       }
36089 +
36090 +       if (!err) {
36091 +               if (au_ibtop(dir) == bindex) {
36092 +                       /* todo: dir->i_mutex is necessary */
36093 +                       au_cpup_attr_timesizes(dir);
36094 +                       if (h_nlink)
36095 +                               vfsub_drop_nlink(dir);
36096 +               }
36097 +               return 0; /* success */
36098 +       }
36099 +
36100 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
36101 +       return err;
36102 +}
36103 +
36104 +static void call_rmdir_whtmp(void *args)
36105 +{
36106 +       int err;
36107 +       aufs_bindex_t bindex;
36108 +       struct au_whtmp_rmdir *a = args;
36109 +       struct super_block *sb;
36110 +       struct dentry *h_parent;
36111 +       struct inode *h_dir;
36112 +       struct au_hinode *hdir;
36113 +
36114 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
36115 +       /* inode_lock(a->dir); */
36116 +       err = -EROFS;
36117 +       sb = a->dir->i_sb;
36118 +       si_read_lock(sb, !AuLock_FLUSH);
36119 +       if (!au_br_writable(a->br->br_perm))
36120 +               goto out;
36121 +       bindex = au_br_index(sb, a->br->br_id);
36122 +       if (unlikely(bindex < 0))
36123 +               goto out;
36124 +
36125 +       err = -EIO;
36126 +       ii_write_lock_parent(a->dir);
36127 +       h_parent = dget_parent(a->wh_dentry);
36128 +       h_dir = d_inode(h_parent);
36129 +       hdir = au_hi(a->dir, bindex);
36130 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
36131 +       if (unlikely(err))
36132 +               goto out_mnt;
36133 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
36134 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36135 +                         a->br);
36136 +       if (!err)
36137 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
36138 +       au_hn_inode_unlock(hdir);
36139 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
36140 +
36141 +out_mnt:
36142 +       dput(h_parent);
36143 +       ii_write_unlock(a->dir);
36144 +out:
36145 +       /* inode_unlock(a->dir); */
36146 +       au_whtmp_rmdir_free(a);
36147 +       si_read_unlock(sb);
36148 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36149 +       if (unlikely(err))
36150 +               AuIOErr("err %d\n", err);
36151 +}
36152 +
36153 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36154 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36155 +{
36156 +       int wkq_err;
36157 +       struct super_block *sb;
36158 +
36159 +       IMustLock(dir);
36160 +
36161 +       /* all post-process will be done in do_rmdir_whtmp(). */
36162 +       sb = dir->i_sb;
36163 +       args->dir = au_igrab(dir);
36164 +       args->br = au_sbr(sb, bindex);
36165 +       au_lcnt_inc(&args->br->br_count);
36166 +       args->wh_dentry = dget(wh_dentry);
36167 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
36168 +       if (unlikely(wkq_err)) {
36169 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
36170 +               au_whtmp_rmdir_free(args);
36171 +       }
36172 +}
36173 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
36174 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
36175 +++ linux/fs/aufs/whout.h       2023-09-03 02:21:58.863304341 +0200
36176 @@ -0,0 +1,87 @@
36177 +/* SPDX-License-Identifier: GPL-2.0 */
36178 +/*
36179 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36180 + *
36181 + * This program is free software; you can redistribute it and/or modify
36182 + * it under the terms of the GNU General Public License as published by
36183 + * the Free Software Foundation; either version 2 of the License, or
36184 + * (at your option) any later version.
36185 + *
36186 + * This program is distributed in the hope that it will be useful,
36187 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36188 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36189 + * GNU General Public License for more details.
36190 + *
36191 + * You should have received a copy of the GNU General Public License
36192 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36193 + */
36194 +
36195 +/*
36196 + * whiteout for logical deletion and opaque directory
36197 + */
36198 +
36199 +#ifndef __AUFS_WHOUT_H__
36200 +#define __AUFS_WHOUT_H__
36201 +
36202 +#ifdef __KERNEL__
36203 +
36204 +#include "dir.h"
36205 +
36206 +/* whout.c */
36207 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36208 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
36209 +              struct qstr *wh_name, int try_sio);
36210 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path);
36211 +struct au_branch;
36212 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36213 +                            struct qstr *prefix);
36214 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36215 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36216 +                       struct dentry *dentry);
36217 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36218 +
36219 +/* diropq flags */
36220 +#define AuDiropq_CREATE        1
36221 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36222 +#define au_fset_diropq(flags, name) \
36223 +       do { (flags) |= AuDiropq_##name; } while (0)
36224 +#define au_fclr_diropq(flags, name) \
36225 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36226 +
36227 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36228 +                            unsigned int flags);
36229 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36230 +                         struct au_branch *br);
36231 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36232 +                           struct dentry *h_parent);
36233 +
36234 +/* real rmdir for the whiteout-ed dir */
36235 +struct au_whtmp_rmdir {
36236 +       struct inode *dir;
36237 +       struct au_branch *br;
36238 +       struct dentry *wh_dentry;
36239 +       struct au_nhash whlist;
36240 +};
36241 +
36242 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36243 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36244 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36245 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36246 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36247 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36248 +
36249 +/* ---------------------------------------------------------------------- */
36250 +
36251 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36252 +                                             aufs_bindex_t bindex)
36253 +{
36254 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36255 +}
36256 +
36257 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36258 +{
36259 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36260 +}
36261 +
36262 +#endif /* __KERNEL__ */
36263 +#endif /* __AUFS_WHOUT_H__ */
36264 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36265 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36266 +++ linux/fs/aufs/wkq.c 2022-11-05 23:02:18.972555950 +0100
36267 @@ -0,0 +1,372 @@
36268 +// SPDX-License-Identifier: GPL-2.0
36269 +/*
36270 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36271 + *
36272 + * This program is free software; you can redistribute it and/or modify
36273 + * it under the terms of the GNU General Public License as published by
36274 + * the Free Software Foundation; either version 2 of the License, or
36275 + * (at your option) any later version.
36276 + *
36277 + * This program is distributed in the hope that it will be useful,
36278 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36279 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36280 + * GNU General Public License for more details.
36281 + *
36282 + * You should have received a copy of the GNU General Public License
36283 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36284 + */
36285 +
36286 +/*
36287 + * workqueue for asynchronous/super-io operations
36288 + * todo: try new credential scheme
36289 + */
36290 +
36291 +#include <linux/module.h>
36292 +#include "aufs.h"
36293 +
36294 +/* internal workqueue named AUFS_WKQ_NAME */
36295 +
36296 +static struct workqueue_struct *au_wkq;
36297 +
36298 +struct au_wkinfo {
36299 +       struct work_struct wk;
36300 +       struct kobject *kobj;
36301 +
36302 +       unsigned int flags; /* see wkq.h */
36303 +
36304 +       au_wkq_func_t func;
36305 +       void *args;
36306 +
36307 +#ifdef CONFIG_LOCKDEP
36308 +       int dont_check;
36309 +       struct held_lock **hlock;
36310 +#endif
36311 +
36312 +       struct completion *comp;
36313 +};
36314 +
36315 +/* ---------------------------------------------------------------------- */
36316 +/*
36317 + * Aufs passes some operations to the workqueue such as the internal copyup.
36318 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36319 + * job run by workqueue depends upon the locks acquired in the other task.
36320 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36321 + * information too. And the job in the workqueue restores the info in order to
36322 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36323 + * correctly and expectedly.
36324 + */
36325 +
36326 +#ifndef CONFIG_LOCKDEP
36327 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36328 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36329 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36330 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36331 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36332 +#else
36333 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36334 +{
36335 +       wkinfo->hlock = NULL;
36336 +       wkinfo->dont_check = 0;
36337 +}
36338 +
36339 +/*
36340 + * 1: matched
36341 + * 0: unmatched
36342 + */
36343 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36344 +{
36345 +       static DEFINE_SPINLOCK(spin);
36346 +       static struct {
36347 +               char *name;
36348 +               struct lock_class_key *key;
36349 +       } a[] = {
36350 +               { .name = "&sbinfo->si_rwsem" },
36351 +               { .name = "&finfo->fi_rwsem" },
36352 +               { .name = "&dinfo->di_rwsem" },
36353 +               { .name = "&iinfo->ii_rwsem" }
36354 +       };
36355 +       static int set;
36356 +       int i;
36357 +
36358 +       /* lockless read from 'set.' see below */
36359 +       if (set == ARRAY_SIZE(a)) {
36360 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36361 +                       if (a[i].key == key)
36362 +                               goto match;
36363 +               goto unmatch;
36364 +       }
36365 +
36366 +       spin_lock(&spin);
36367 +       if (set)
36368 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36369 +                       if (a[i].key == key) {
36370 +                               spin_unlock(&spin);
36371 +                               goto match;
36372 +                       }
36373 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36374 +               if (a[i].key) {
36375 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36376 +                               spin_unlock(&spin);
36377 +                               goto match;
36378 +                       } else
36379 +                               continue;
36380 +               }
36381 +               if (strstr(a[i].name, name)) {
36382 +                       /*
36383 +                        * the order of these three lines is important for the
36384 +                        * lockless read above.
36385 +                        */
36386 +                       a[i].key = key;
36387 +                       spin_unlock(&spin);
36388 +                       set++;
36389 +                       /* AuDbg("%d, %s\n", set, name); */
36390 +                       goto match;
36391 +               }
36392 +       }
36393 +       spin_unlock(&spin);
36394 +       goto unmatch;
36395 +
36396 +match:
36397 +       return 1;
36398 +unmatch:
36399 +       return 0;
36400 +}
36401 +
36402 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36403 +{
36404 +       int err, n;
36405 +       struct task_struct *curr;
36406 +       struct held_lock **hl, *held_locks, *p;
36407 +
36408 +       err = 0;
36409 +       curr = current;
36410 +       wkinfo->dont_check = lockdep_recursing(curr);
36411 +       if (wkinfo->dont_check)
36412 +               goto out;
36413 +       n = curr->lockdep_depth;
36414 +       if (!n)
36415 +               goto out;
36416 +
36417 +       err = -ENOMEM;
36418 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36419 +       if (unlikely(!wkinfo->hlock))
36420 +               goto out;
36421 +
36422 +       err = 0;
36423 +#if 0 /* left for debugging */
36424 +       if (0 && au_debug_test())
36425 +               lockdep_print_held_locks(curr);
36426 +#endif
36427 +       held_locks = curr->held_locks;
36428 +       hl = wkinfo->hlock;
36429 +       while (n--) {
36430 +               p = held_locks++;
36431 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36432 +                       *hl++ = p;
36433 +       }
36434 +       *hl = NULL;
36435 +
36436 +out:
36437 +       return err;
36438 +}
36439 +
36440 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36441 +{
36442 +       au_kfree_try_rcu(wkinfo->hlock);
36443 +}
36444 +
36445 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36446 +{
36447 +       struct held_lock *p, **hl = wkinfo->hlock;
36448 +       int subclass;
36449 +
36450 +       if (wkinfo->dont_check)
36451 +               lockdep_off();
36452 +       if (!hl)
36453 +               return;
36454 +       while ((p = *hl++)) { /* assignment */
36455 +               subclass = lockdep_hlock_class(p)->subclass;
36456 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36457 +               if (p->read)
36458 +                       rwsem_acquire_read(p->instance, subclass, 0,
36459 +                                          /*p->acquire_ip*/_RET_IP_);
36460 +               else
36461 +                       rwsem_acquire(p->instance, subclass, 0,
36462 +                                     /*p->acquire_ip*/_RET_IP_);
36463 +       }
36464 +}
36465 +
36466 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36467 +{
36468 +       struct held_lock *p, **hl = wkinfo->hlock;
36469 +
36470 +       if (wkinfo->dont_check)
36471 +               lockdep_on();
36472 +       if (!hl)
36473 +               return;
36474 +       while ((p = *hl++)) /* assignment */
36475 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36476 +}
36477 +#endif
36478 +
36479 +static void wkq_func(struct work_struct *wk)
36480 +{
36481 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36482 +
36483 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36484 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36485 +
36486 +       au_wkq_lockdep_pre(wkinfo);
36487 +       wkinfo->func(wkinfo->args);
36488 +       au_wkq_lockdep_post(wkinfo);
36489 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36490 +               complete(wkinfo->comp);
36491 +       else {
36492 +               kobject_put(wkinfo->kobj);
36493 +               module_put(THIS_MODULE); /* todo: ?? */
36494 +               au_kfree_rcu(wkinfo);
36495 +       }
36496 +}
36497 +
36498 +/*
36499 + * Since struct completion is large, try allocating it dynamically.
36500 + */
36501 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36502 +
36503 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36504 +{
36505 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36506 +       if (*comp) {
36507 +               init_completion(*comp);
36508 +               wkinfo->comp = *comp;
36509 +               return 0;
36510 +       }
36511 +       return -ENOMEM;
36512 +}
36513 +
36514 +static void au_wkq_comp_free(struct completion *comp)
36515 +{
36516 +       au_kfree_rcu(comp);
36517 +}
36518 +
36519 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36520 +{
36521 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36522 +               if (au_wkq_test()) {
36523 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36524 +                               " due to a dead dir by UDBA,"
36525 +                               " or async xino write?\n");
36526 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36527 +               }
36528 +       } else
36529 +               au_dbg_verify_kthread();
36530 +
36531 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36532 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36533 +               queue_work(au_wkq, &wkinfo->wk);
36534 +       } else {
36535 +               INIT_WORK(&wkinfo->wk, wkq_func);
36536 +               schedule_work(&wkinfo->wk);
36537 +       }
36538 +}
36539 +
36540 +/*
36541 + * Be careful. It is easy to make deadlock happen.
36542 + * processA: lock, wkq and wait
36543 + * processB: wkq and wait, lock in wkq
36544 + * --> deadlock
36545 + */
36546 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36547 +{
36548 +       int err;
36549 +       AuWkqCompDeclare(comp);
36550 +       struct au_wkinfo wkinfo = {
36551 +               .flags  = flags,
36552 +               .func   = func,
36553 +               .args   = args
36554 +       };
36555 +
36556 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36557 +       if (unlikely(err))
36558 +               goto out;
36559 +       err = au_wkq_lockdep_alloc(&wkinfo);
36560 +       if (unlikely(err))
36561 +               goto out_comp;
36562 +       if (!err) {
36563 +               au_wkq_run(&wkinfo);
36564 +               /* no timeout, no interrupt */
36565 +               wait_for_completion(wkinfo.comp);
36566 +       }
36567 +       au_wkq_lockdep_free(&wkinfo);
36568 +
36569 +out_comp:
36570 +       au_wkq_comp_free(comp);
36571 +out:
36572 +       destroy_work_on_stack(&wkinfo.wk);
36573 +       return err;
36574 +}
36575 +
36576 +/*
36577 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36578 + * problem in a concurrent umounting.
36579 + */
36580 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36581 +                 unsigned int flags)
36582 +{
36583 +       int err;
36584 +       struct au_wkinfo *wkinfo;
36585 +
36586 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36587 +
36588 +       /*
36589 +        * wkq_func() must free this wkinfo.
36590 +        * it highly depends upon the implementation of workqueue.
36591 +        */
36592 +       err = 0;
36593 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36594 +       if (wkinfo) {
36595 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36596 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36597 +               wkinfo->func = func;
36598 +               wkinfo->args = args;
36599 +               wkinfo->comp = NULL;
36600 +               au_wkq_lockdep_init(wkinfo);
36601 +               kobject_get(wkinfo->kobj);
36602 +               __module_get(THIS_MODULE); /* todo: ?? */
36603 +
36604 +               au_wkq_run(wkinfo);
36605 +       } else {
36606 +               err = -ENOMEM;
36607 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36608 +       }
36609 +
36610 +       return err;
36611 +}
36612 +
36613 +/* ---------------------------------------------------------------------- */
36614 +
36615 +void au_nwt_init(struct au_nowait_tasks *nwt)
36616 +{
36617 +       atomic_set(&nwt->nw_len, 0);
36618 +       /* smp_mb(); */ /* atomic_set */
36619 +       init_waitqueue_head(&nwt->nw_wq);
36620 +}
36621 +
36622 +void au_wkq_fin(void)
36623 +{
36624 +       destroy_workqueue(au_wkq);
36625 +}
36626 +
36627 +int __init au_wkq_init(void)
36628 +{
36629 +       int err;
36630 +
36631 +       err = 0;
36632 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36633 +       if (IS_ERR(au_wkq))
36634 +               err = PTR_ERR(au_wkq);
36635 +       else if (!au_wkq)
36636 +               err = -ENOMEM;
36637 +
36638 +       return err;
36639 +}
36640 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36641 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36642 +++ linux/fs/aufs/wkq.h 2022-11-05 23:02:18.972555950 +0100
36643 @@ -0,0 +1,89 @@
36644 +/* SPDX-License-Identifier: GPL-2.0 */
36645 +/*
36646 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36647 + *
36648 + * This program is free software; you can redistribute it and/or modify
36649 + * it under the terms of the GNU General Public License as published by
36650 + * the Free Software Foundation; either version 2 of the License, or
36651 + * (at your option) any later version.
36652 + *
36653 + * This program is distributed in the hope that it will be useful,
36654 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36655 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36656 + * GNU General Public License for more details.
36657 + *
36658 + * You should have received a copy of the GNU General Public License
36659 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36660 + */
36661 +
36662 +/*
36663 + * workqueue for asynchronous/super-io operations
36664 + * todo: try new credentials management scheme
36665 + */
36666 +
36667 +#ifndef __AUFS_WKQ_H__
36668 +#define __AUFS_WKQ_H__
36669 +
36670 +#ifdef __KERNEL__
36671 +
36672 +#include <linux/wait.h>
36673 +
36674 +struct super_block;
36675 +
36676 +/* ---------------------------------------------------------------------- */
36677 +
36678 +/*
36679 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36680 + */
36681 +struct au_nowait_tasks {
36682 +       atomic_t                nw_len;
36683 +       wait_queue_head_t       nw_wq;
36684 +};
36685 +
36686 +/* ---------------------------------------------------------------------- */
36687 +
36688 +typedef void (*au_wkq_func_t)(void *args);
36689 +
36690 +/* wkq flags */
36691 +#define AuWkq_WAIT     1
36692 +#define AuWkq_NEST     (1 << 1)
36693 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36694 +#define au_fset_wkq(flags, name) \
36695 +       do { (flags) |= AuWkq_##name; } while (0)
36696 +#define au_fclr_wkq(flags, name) \
36697 +       do { (flags) &= ~AuWkq_##name; } while (0)
36698 +
36699 +/* wkq.c */
36700 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36701 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36702 +                 unsigned int flags);
36703 +void au_nwt_init(struct au_nowait_tasks *nwt);
36704 +int __init au_wkq_init(void);
36705 +void au_wkq_fin(void);
36706 +
36707 +/* ---------------------------------------------------------------------- */
36708 +
36709 +static inline int au_wkq_test(void)
36710 +{
36711 +       return current->flags & PF_WQ_WORKER;
36712 +}
36713 +
36714 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36715 +{
36716 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36717 +}
36718 +
36719 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36720 +{
36721 +       if (atomic_dec_and_test(&nwt->nw_len))
36722 +               wake_up_all(&nwt->nw_wq);
36723 +}
36724 +
36725 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36726 +{
36727 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36728 +       return 0;
36729 +}
36730 +
36731 +#endif /* __KERNEL__ */
36732 +#endif /* __AUFS_WKQ_H__ */
36733 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36734 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36735 +++ linux/fs/aufs/xattr.c       2023-09-03 02:21:58.863304341 +0200
36736 @@ -0,0 +1,360 @@
36737 +// SPDX-License-Identifier: GPL-2.0
36738 +/*
36739 + * Copyright (C) 2014-2022 Junjiro R. Okajima
36740 + *
36741 + * This program is free software; you can redistribute it and/or modify
36742 + * it under the terms of the GNU General Public License as published by
36743 + * the Free Software Foundation; either version 2 of the License, or
36744 + * (at your option) any later version.
36745 + *
36746 + * This program is distributed in the hope that it will be useful,
36747 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36748 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36749 + * GNU General Public License for more details.
36750 + *
36751 + * You should have received a copy of the GNU General Public License
36752 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36753 + */
36754 +
36755 +/*
36756 + * handling xattr functions
36757 + */
36758 +
36759 +#include <linux/fs.h>
36760 +#include <linux/xattr.h>
36761 +#include "aufs.h"
36762 +
36763 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36764 +{
36765 +       if (!ignore_flags)
36766 +               goto out;
36767 +       switch (err) {
36768 +       case -ENOMEM:
36769 +       case -EDQUOT:
36770 +               goto out;
36771 +       }
36772 +
36773 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36774 +               err = 0;
36775 +               goto out;
36776 +       }
36777 +
36778 +#define cmp(brattr, prefix) do {                                       \
36779 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36780 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36781 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36782 +                               err = 0;                                \
36783 +                       goto out;                                       \
36784 +               }                                                       \
36785 +       } while (0)
36786 +
36787 +       cmp(SEC, SECURITY);
36788 +       cmp(SYS, SYSTEM);
36789 +       cmp(TR, TRUSTED);
36790 +       cmp(USR, USER);
36791 +#undef cmp
36792 +
36793 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36794 +               err = 0;
36795 +
36796 +out:
36797 +       return err;
36798 +}
36799 +
36800 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36801 +
36802 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36803 +                           char *name, char **buf, unsigned int ignore_flags,
36804 +                           unsigned int verbose)
36805 +{
36806 +       int err, is_acl;
36807 +       ssize_t ssz;
36808 +       struct inode *h_idst;
36809 +       struct dentry *h_dst_dentry, *h_src_dentry;
36810 +       struct mnt_idmap *h_dst_idmap, *h_src_idmap;
36811 +       struct posix_acl *acl;
36812 +
36813 +       is_acl = !!is_posix_acl_xattr(name);
36814 +       h_src_idmap = mnt_idmap(h_src->mnt);
36815 +       h_src_dentry = h_src->dentry;
36816 +       if (is_acl) {
36817 +               acl = vfs_get_acl(h_src_idmap, h_src_dentry, name);
36818 +               AuDebugOn(!acl);
36819 +               if (unlikely(IS_ERR(acl))) {
36820 +                       err = PTR_ERR(acl);
36821 +                       if (err == -ENODATA)
36822 +                               err = 0;
36823 +                       else if (err == -EOPNOTSUPP
36824 +                                && au_test_nfs_noacl(d_inode(h_src_dentry)))
36825 +                               err = 0;
36826 +                       else if (verbose || au_debug_test())
36827 +                               pr_err("%s, err %d\n", name, err);
36828 +                       goto out;
36829 +               }
36830 +       } else {
36831 +               ssz = vfs_getxattr_alloc(h_src_idmap, h_src_dentry, name, buf,
36832 +                                        0, GFP_NOFS);
36833 +               if (unlikely(ssz <= 0)) {
36834 +                       err = ssz;
36835 +                       if (err == -ENODATA)
36836 +                               err = 0;
36837 +                       else if (err == -EOPNOTSUPP
36838 +                                && (ignore_flags & au_xattr_out_of_list))
36839 +                                err = 0;
36840 +                       else if (err && (verbose || au_debug_test()))
36841 +                               pr_err("%s, err %d\n", name, err);
36842 +                       goto out;
36843 +               }
36844 +       }
36845 +
36846 +       /* unlock it temporary */
36847 +       h_dst_idmap = mnt_idmap(h_dst->mnt);
36848 +       h_dst_dentry = h_dst->dentry;
36849 +       h_idst = d_inode(h_dst_dentry);
36850 +       inode_unlock(h_idst);
36851 +       if (is_acl) {
36852 +               err = vfsub_set_acl(h_dst_idmap, h_dst_dentry, name, acl);
36853 +               posix_acl_release(acl);
36854 +       } else
36855 +               err = vfsub_setxattr(h_dst_idmap, h_dst_dentry, name, *buf,
36856 +                                    ssz, /*flags*/0);
36857 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36858 +       if (unlikely(err)) {
36859 +               if (verbose || au_debug_test())
36860 +                       pr_err("%s, err %d\n", name, err);
36861 +               err = au_xattr_ignore(err, name, ignore_flags);
36862 +       }
36863 +
36864 +out:
36865 +       return err;
36866 +}
36867 +
36868 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36869 +                 unsigned int verbose)
36870 +{
36871 +       int err, unlocked;
36872 +       ssize_t ssz;
36873 +       struct dentry *h_dst_dentry, *h_src_dentry;
36874 +       struct inode *h_isrc, *h_idst;
36875 +       char *value, *p, *o, *e;
36876 +
36877 +       /* try stopping to update the source inode while we are referencing */
36878 +       /* there should not be the parent-child relationship between them */
36879 +       h_dst_dentry = h_dst->dentry;
36880 +       h_idst = d_inode(h_dst_dentry);
36881 +       h_src_dentry = h_src->dentry;
36882 +       h_isrc = d_inode(h_src_dentry);
36883 +       inode_unlock(h_idst);
36884 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36885 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36886 +       unlocked = 0;
36887 +
36888 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36889 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36890 +       err = ssz;
36891 +       if (unlikely(err < 0)) {
36892 +               AuTraceErr(err);
36893 +               if (err == -ENODATA
36894 +                   || err == -EOPNOTSUPP)
36895 +                       err = 0;        /* ignore */
36896 +               goto out;
36897 +       }
36898 +
36899 +       err = 0;
36900 +       p = NULL;
36901 +       o = NULL;
36902 +       if (ssz) {
36903 +               err = -ENOMEM;
36904 +               p = kmalloc(ssz, GFP_NOFS);
36905 +               o = p;
36906 +               if (unlikely(!p))
36907 +                       goto out;
36908 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36909 +       }
36910 +       inode_unlock_shared(h_isrc);
36911 +       unlocked = 1;
36912 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36913 +       if (unlikely(err < 0))
36914 +               goto out_free;
36915 +
36916 +       err = 0;
36917 +       e = p + ssz;
36918 +       value = NULL;
36919 +       ignore_flags |= au_xattr_out_of_list;
36920 +       while (!err && p < e) {
36921 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36922 +                                      verbose);
36923 +               p += strlen(p) + 1;
36924 +       }
36925 +       au_kfree_try_rcu(value);
36926 +
36927 +out_free:
36928 +       au_kfree_try_rcu(o);
36929 +out:
36930 +       if (!unlocked)
36931 +               inode_unlock_shared(h_isrc);
36932 +       AuTraceErr(err);
36933 +       return err;
36934 +}
36935 +
36936 +/* ---------------------------------------------------------------------- */
36937 +
36938 +static int au_smack_reentering(struct super_block *sb)
36939 +{
36940 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36941 +       /*
36942 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36943 +        * i_op->getxattr(). ouch.
36944 +        */
36945 +       return si_pid_test(sb);
36946 +#else
36947 +       return 0;
36948 +#endif
36949 +}
36950 +
36951 +enum {
36952 +       AU_XATTR_LIST,
36953 +       AU_XATTR_GET
36954 +};
36955 +
36956 +struct au_lgxattr {
36957 +       int type;
36958 +       union {
36959 +               struct {
36960 +                       char    *list;
36961 +                       size_t  size;
36962 +               } list;
36963 +               struct {
36964 +                       const char      *name;
36965 +                       void            *value;
36966 +                       size_t          size;
36967 +               } get;
36968 +       } u;
36969 +};
36970 +
36971 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36972 +                         struct au_lgxattr *arg)
36973 +{
36974 +       ssize_t err;
36975 +       int reenter;
36976 +       struct path h_path;
36977 +       struct super_block *sb;
36978 +
36979 +       sb = dentry->d_sb;
36980 +       reenter = au_smack_reentering(sb);
36981 +       if (!reenter) {
36982 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36983 +               if (unlikely(err))
36984 +                       goto out;
36985 +       }
36986 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36987 +       if (unlikely(err))
36988 +               goto out_si;
36989 +       if (unlikely(!h_path.dentry))
36990 +               /* illegally overlapped or something */
36991 +               goto out_di; /* pretending success */
36992 +
36993 +       /* always topmost entry only */
36994 +       switch (arg->type) {
36995 +       case AU_XATTR_LIST:
36996 +               err = vfs_listxattr(h_path.dentry,
36997 +                                   arg->u.list.list, arg->u.list.size);
36998 +               break;
36999 +       case AU_XATTR_GET:
37000 +               AuDebugOn(d_is_negative(h_path.dentry));
37001 +               err = vfs_getxattr(mnt_idmap(h_path.mnt), h_path.dentry,
37002 +                                  arg->u.get.name, arg->u.get.value,
37003 +                                  arg->u.get.size);
37004 +               break;
37005 +       }
37006 +
37007 +out_di:
37008 +       if (!reenter)
37009 +               di_read_unlock(dentry, AuLock_IR);
37010 +out_si:
37011 +       if (!reenter)
37012 +               si_read_unlock(sb);
37013 +out:
37014 +       AuTraceErr(err);
37015 +       return err;
37016 +}
37017 +
37018 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
37019 +{
37020 +       struct au_lgxattr arg = {
37021 +               .type = AU_XATTR_LIST,
37022 +               .u.list = {
37023 +                       .list   = list,
37024 +                       .size   = size
37025 +               },
37026 +       };
37027 +
37028 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
37029 +}
37030 +
37031 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
37032 +                          const char *name, void *value, size_t size)
37033 +{
37034 +       struct au_lgxattr arg = {
37035 +               .type = AU_XATTR_GET,
37036 +               .u.get = {
37037 +                       .name   = name,
37038 +                       .value  = value,
37039 +                       .size   = size
37040 +               },
37041 +       };
37042 +
37043 +       return au_lgxattr(dentry, inode, &arg);
37044 +}
37045 +
37046 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
37047 +                      const char *name, const void *value, size_t size,
37048 +                      int flags)
37049 +{
37050 +       struct au_sxattr arg = {
37051 +               .type = AU_XATTR_SET,
37052 +               .u.set = {
37053 +                       .name   = name,
37054 +                       .value  = value,
37055 +                       .size   = size,
37056 +                       .flags  = flags
37057 +               },
37058 +       };
37059 +
37060 +       return au_sxattr(dentry, inode, &arg);
37061 +}
37062 +
37063 +/* ---------------------------------------------------------------------- */
37064 +
37065 +static int au_xattr_get(const struct xattr_handler *handler,
37066 +                       struct dentry *dentry, struct inode *inode,
37067 +                       const char *name, void *buffer, size_t size)
37068 +{
37069 +       return au_getxattr(dentry, inode, name, buffer, size);
37070 +}
37071 +
37072 +static int au_xattr_set(const struct xattr_handler *handler,
37073 +                       struct mnt_idmap *idmap,
37074 +                       struct dentry *dentry, struct inode *inode,
37075 +                       const char *name, const void *value, size_t size,
37076 +                       int flags)
37077 +{
37078 +       return au_setxattr(dentry, inode, name, value, size, flags);
37079 +}
37080 +
37081 +static const struct xattr_handler au_xattr_handler = {
37082 +       .name   = "",
37083 +       .prefix = "",
37084 +       .get    = au_xattr_get,
37085 +       .set    = au_xattr_set
37086 +};
37087 +
37088 +static const struct xattr_handler *au_xattr_handlers[] = {
37089 +       &au_xattr_handler, /* must be last */
37090 +       NULL
37091 +};
37092 +
37093 +void au_xattr_init(struct super_block *sb)
37094 +{
37095 +       sb->s_xattr = au_xattr_handlers;
37096 +}
37097 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
37098 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
37099 +++ linux/fs/aufs/xino.c        2023-09-03 02:21:58.863304341 +0200
37100 @@ -0,0 +1,1926 @@
37101 +// SPDX-License-Identifier: GPL-2.0
37102 +/*
37103 + * Copyright (C) 2005-2022 Junjiro R. Okajima
37104 + *
37105 + * This program is free software; you can redistribute it and/or modify
37106 + * it under the terms of the GNU General Public License as published by
37107 + * the Free Software Foundation; either version 2 of the License, or
37108 + * (at your option) any later version.
37109 + *
37110 + * This program is distributed in the hope that it will be useful,
37111 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37112 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37113 + * GNU General Public License for more details.
37114 + *
37115 + * You should have received a copy of the GNU General Public License
37116 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37117 + */
37118 +
37119 +/*
37120 + * external inode number translation table and bitmap
37121 + *
37122 + * things to consider
37123 + * - the lifetime
37124 + *   + au_xino object
37125 + *   + XINO files (xino, xib, xigen)
37126 + *   + dynamic debugfs entries (xiN)
37127 + *   + static debugfs entries (xib, xigen)
37128 + *   + static sysfs entry (xi_path)
37129 + * - several entry points to handle them.
37130 + *   + mount(2) without xino option (default)
37131 + *   + mount(2) with xino option
37132 + *   + mount(2) with noxino option
37133 + *   + umount(2)
37134 + *   + remount with add/del branches
37135 + *   + remount with xino/noxino options
37136 + */
37137 +
37138 +#include <linux/seq_file.h>
37139 +#include <linux/statfs.h>
37140 +#include "aufs.h"
37141 +
37142 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37143 +                                    aufs_bindex_t bbot,
37144 +                                    struct super_block *h_sb)
37145 +{
37146 +       /* todo: try binary-search if the branches are many */
37147 +       for (; btop <= bbot; btop++)
37148 +               if (h_sb == au_sbr_sb(sb, btop))
37149 +                       return btop;
37150 +       return -1;
37151 +}
37152 +
37153 +/*
37154 + * find another branch who is on the same filesystem of the specified
37155 + * branch{@btgt}. search until @bbot.
37156 + */
37157 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37158 +                                 aufs_bindex_t bbot)
37159 +{
37160 +       aufs_bindex_t bindex;
37161 +       struct super_block *tgt_sb;
37162 +
37163 +       tgt_sb = au_sbr_sb(sb, btgt);
37164 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37165 +       if (bindex < 0)
37166 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
37167 +
37168 +       return bindex;
37169 +}
37170 +
37171 +/* ---------------------------------------------------------------------- */
37172 +
37173 +/*
37174 + * stop unnecessary notify events at creating xino files
37175 + */
37176 +
37177 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37178 +{
37179 +       aufs_bindex_t bfound, bindex, bbot;
37180 +       struct dentry *parent;
37181 +       struct au_branch *br;
37182 +
37183 +       bfound = -1;
37184 +       parent = dentry->d_parent; /* safe d_parent access */
37185 +       bbot = au_sbbot(sb);
37186 +       for (bindex = 0; bindex <= bbot; bindex++) {
37187 +               br = au_sbr(sb, bindex);
37188 +               if (au_br_dentry(br) == parent) {
37189 +                       bfound = bindex;
37190 +                       break;
37191 +               }
37192 +       }
37193 +
37194 +       AuDbg("bfound b%d\n", bfound);
37195 +       return bfound;
37196 +}
37197 +
37198 +struct au_xino_lock_dir {
37199 +       struct au_hinode *hdir;
37200 +       struct dentry *parent;
37201 +       struct inode *dir;
37202 +};
37203 +
37204 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37205 +                                         unsigned int lsc)
37206 +{
37207 +       struct dentry *parent;
37208 +       struct inode *dir;
37209 +
37210 +       parent = dget_parent(dentry);
37211 +       dir = d_inode(parent);
37212 +       inode_lock_nested(dir, lsc);
37213 +#if 0 /* it should not happen */
37214 +       spin_lock(&dentry->d_lock);
37215 +       if (unlikely(dentry->d_parent != parent)) {
37216 +               spin_unlock(&dentry->d_lock);
37217 +               inode_unlock(dir);
37218 +               dput(parent);
37219 +               parent = NULL;
37220 +               goto out;
37221 +       }
37222 +       spin_unlock(&dentry->d_lock);
37223 +
37224 +out:
37225 +#endif
37226 +       return parent;
37227 +}
37228 +
37229 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37230 +                            struct au_xino_lock_dir *ldir)
37231 +{
37232 +       aufs_bindex_t bindex;
37233 +
37234 +       ldir->hdir = NULL;
37235 +       bindex = au_xi_root(sb, xipath->dentry);
37236 +       if (bindex >= 0) {
37237 +               /* rw branch root */
37238 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37239 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37240 +       } else {
37241 +               /* other */
37242 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37243 +                                                  AuLsc_I_PARENT);
37244 +               ldir->dir = d_inode(ldir->parent);
37245 +       }
37246 +}
37247 +
37248 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37249 +{
37250 +       if (ldir->hdir)
37251 +               au_hn_inode_unlock(ldir->hdir);
37252 +       else {
37253 +               inode_unlock(ldir->dir);
37254 +               dput(ldir->parent);
37255 +       }
37256 +}
37257 +
37258 +/* ---------------------------------------------------------------------- */
37259 +
37260 +/*
37261 + * create and set a new xino file
37262 + */
37263 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37264 +                           int wbrtop)
37265 +{
37266 +       struct file *file;
37267 +       struct dentry *h_parent, *d;
37268 +       struct inode *h_dir, *inode;
37269 +       int err;
37270 +       static DEFINE_MUTEX(mtx);
37271 +
37272 +       /*
37273 +        * at mount-time, and the xino file is the default path,
37274 +        * hnotify is disabled so we have no notify events to ignore.
37275 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37276 +        */
37277 +       if (!wbrtop)
37278 +               mutex_lock(&mtx);
37279 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37280 +                              /* | __FMODE_NONOTIFY */,
37281 +                              0666);
37282 +       if (IS_ERR(file)) {
37283 +               if (!wbrtop)
37284 +                       mutex_unlock(&mtx);
37285 +               if (!silent)
37286 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37287 +               return file;
37288 +       }
37289 +
37290 +       /* keep file count */
37291 +       err = 0;
37292 +       d = file->f_path.dentry;
37293 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37294 +       if (!wbrtop)
37295 +               mutex_unlock(&mtx);
37296 +       /* mnt_want_write() is unnecessary here */
37297 +       h_dir = d_inode(h_parent);
37298 +       inode = file_inode(file);
37299 +       /* no delegation since it is just created */
37300 +       if (inode->i_nlink)
37301 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37302 +                                  /*force*/0);
37303 +       inode_unlock(h_dir);
37304 +       dput(h_parent);
37305 +       if (unlikely(err)) {
37306 +               if (!silent)
37307 +                       pr_err("unlink %s(%d)\n", fpath, err);
37308 +               goto out;
37309 +       }
37310 +
37311 +       err = -EINVAL;
37312 +       if (unlikely(sb && sb == d->d_sb)) {
37313 +               if (!silent)
37314 +                       pr_err("%s must be outside\n", fpath);
37315 +               goto out;
37316 +       }
37317 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37318 +               if (!silent)
37319 +                       pr_err("xino doesn't support %s(%s)\n",
37320 +                              fpath, au_sbtype(d->d_sb));
37321 +               goto out;
37322 +       }
37323 +       return file; /* success */
37324 +
37325 +out:
37326 +       fput(file);
37327 +       file = ERR_PTR(err);
37328 +       return file;
37329 +}
37330 +
37331 +/*
37332 + * create a new xinofile at the same place/path as @base.
37333 + */
37334 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37335 +                            struct file *copy_src)
37336 +{
37337 +       struct file *file;
37338 +       struct dentry *dentry;
37339 +       struct inode *dir, *delegated;
37340 +       struct qstr *name;
37341 +       struct path ppath, path;
37342 +       int err, do_unlock;
37343 +       struct au_xino_lock_dir ldir;
37344 +
37345 +       do_unlock = 1;
37346 +       au_xino_lock_dir(sb, base, &ldir);
37347 +       dentry = base->dentry;
37348 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37349 +       ppath.mnt = base->mnt;
37350 +       dir = d_inode(ppath.dentry);
37351 +       IMustLock(dir);
37352 +
37353 +       name = &dentry->d_name;
37354 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37355 +       if (IS_ERR(path.dentry)) {
37356 +               file = (void *)path.dentry;
37357 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37358 +               goto out;
37359 +       }
37360 +
37361 +       /* no need to mnt_want_write() since we call dentry_open() later */
37362 +       err = vfs_create(mnt_idmap(base->mnt), dir, path.dentry, 0666, NULL);
37363 +       if (unlikely(err)) {
37364 +               file = ERR_PTR(err);
37365 +               pr_err("%pd create err %d\n", dentry, err);
37366 +               goto out_dput;
37367 +       }
37368 +
37369 +       path.mnt = base->mnt;
37370 +       file = vfsub_dentry_open(&path,
37371 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37372 +                                /* | __FMODE_NONOTIFY */);
37373 +       if (IS_ERR(file)) {
37374 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37375 +               goto out_dput;
37376 +       }
37377 +
37378 +       delegated = NULL;
37379 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37380 +       au_xino_unlock_dir(&ldir);
37381 +       do_unlock = 0;
37382 +       if (unlikely(err == -EWOULDBLOCK)) {
37383 +               pr_warn("cannot retry for NFSv4 delegation"
37384 +                       " for an internal unlink\n");
37385 +               iput(delegated);
37386 +       }
37387 +       if (unlikely(err)) {
37388 +               pr_err("%pd unlink err %d\n", dentry, err);
37389 +               goto out_fput;
37390 +       }
37391 +
37392 +       if (copy_src) {
37393 +               /* no one can touch copy_src xino */
37394 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37395 +               if (unlikely(err)) {
37396 +                       pr_err("%pd copy err %d\n", dentry, err);
37397 +                       goto out_fput;
37398 +               }
37399 +       }
37400 +       goto out_dput; /* success */
37401 +
37402 +out_fput:
37403 +       fput(file);
37404 +       file = ERR_PTR(err);
37405 +out_dput:
37406 +       dput(path.dentry);
37407 +out:
37408 +       if (do_unlock)
37409 +               au_xino_unlock_dir(&ldir);
37410 +       return file;
37411 +}
37412 +
37413 +struct file *au_xino_file1(struct au_xino *xi)
37414 +{
37415 +       struct file *file;
37416 +       unsigned int u, nfile;
37417 +
37418 +       file = NULL;
37419 +       nfile = xi->xi_nfile;
37420 +       for (u = 0; u < nfile; u++) {
37421 +               file = xi->xi_file[u];
37422 +               if (file)
37423 +                       break;
37424 +       }
37425 +
37426 +       return file;
37427 +}
37428 +
37429 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37430 +{
37431 +       int err;
37432 +       struct file *f;
37433 +       void *p;
37434 +
37435 +       if (file)
37436 +               get_file(file);
37437 +
37438 +       err = 0;
37439 +       f = NULL;
37440 +       if (idx < xi->xi_nfile) {
37441 +               f = xi->xi_file[idx];
37442 +               if (f)
37443 +                       fput(f);
37444 +       } else {
37445 +               p = au_kzrealloc(xi->xi_file,
37446 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37447 +                                sizeof(*xi->xi_file) * (idx + 1),
37448 +                                GFP_NOFS, /*may_shrink*/0);
37449 +               if (p) {
37450 +                       MtxMustLock(&xi->xi_mtx);
37451 +                       xi->xi_file = p;
37452 +                       xi->xi_nfile = idx + 1;
37453 +               } else {
37454 +                       err = -ENOMEM;
37455 +                       if (file)
37456 +                               fput(file);
37457 +                       goto out;
37458 +               }
37459 +       }
37460 +       xi->xi_file[idx] = file;
37461 +
37462 +out:
37463 +       return err;
37464 +}
37465 +
37466 +/*
37467 + * if @xinew->xi is not set, then create new xigen file.
37468 + */
37469 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37470 +{
37471 +       struct file *file;
37472 +       int err;
37473 +
37474 +       SiMustAnyLock(sb);
37475 +
37476 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37477 +       if (IS_ERR(file)) {
37478 +               err = PTR_ERR(file);
37479 +               pr_err("%s[%d], err %d\n",
37480 +                      xinew->xi ? "xino" : "xigen",
37481 +                      xinew->idx, err);
37482 +               goto out;
37483 +       }
37484 +
37485 +       if (xinew->xi)
37486 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37487 +       else {
37488 +               BUG();
37489 +               /* todo: make xigen file an array */
37490 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37491 +       }
37492 +       fput(file);
37493 +       if (unlikely(err))
37494 +               file = ERR_PTR(err);
37495 +
37496 +out:
37497 +       return file;
37498 +}
37499 +
37500 +/* ---------------------------------------------------------------------- */
37501 +
37502 +/*
37503 + * truncate xino files
37504 + */
37505 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37506 +                           int idx, struct kstatfs *st)
37507 +{
37508 +       int err;
37509 +       blkcnt_t blocks;
37510 +       struct file *file, *new_xino;
37511 +       struct au_xi_new xinew = {
37512 +               .idx = idx
37513 +       };
37514 +
37515 +       err = 0;
37516 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37517 +       file = au_xino_file(xinew.xi, idx);
37518 +       if (!file)
37519 +               goto out;
37520 +
37521 +       xinew.base = &file->f_path;
37522 +       err = vfs_statfs(xinew.base, st);
37523 +       if (unlikely(err)) {
37524 +               AuErr1("statfs err %d, ignored\n", err);
37525 +               err = 0;
37526 +               goto out;
37527 +       }
37528 +
37529 +       blocks = file_inode(file)->i_blocks;
37530 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37531 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37532 +
37533 +       xinew.copy_src = file;
37534 +       new_xino = au_xi_new(sb, &xinew);
37535 +       if (IS_ERR(new_xino)) {
37536 +               err = PTR_ERR(new_xino);
37537 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37538 +               goto out;
37539 +       }
37540 +
37541 +       err = vfs_statfs(&new_xino->f_path, st);
37542 +       if (!err)
37543 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37544 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37545 +                       st->f_bfree, st->f_blocks);
37546 +       else {
37547 +               AuErr1("statfs err %d, ignored\n", err);
37548 +               err = 0;
37549 +       }
37550 +
37551 +out:
37552 +       return err;
37553 +}
37554 +
37555 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37556 +{
37557 +       int err, i;
37558 +       unsigned long jiffy;
37559 +       aufs_bindex_t bbot;
37560 +       struct kstatfs *st;
37561 +       struct au_branch *br;
37562 +       struct au_xino *xi;
37563 +
37564 +       err = -ENOMEM;
37565 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37566 +       if (unlikely(!st))
37567 +               goto out;
37568 +
37569 +       err = -EINVAL;
37570 +       bbot = au_sbbot(sb);
37571 +       if (unlikely(bindex < 0 || bbot < bindex))
37572 +               goto out_st;
37573 +
37574 +       err = 0;
37575 +       jiffy = jiffies;
37576 +       br = au_sbr(sb, bindex);
37577 +       xi = br->br_xino;
37578 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37579 +               err = au_xino_do_trunc(sb, bindex, i, st);
37580 +       if (!err)
37581 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37582 +
37583 +out_st:
37584 +       au_kfree_rcu(st);
37585 +out:
37586 +       return err;
37587 +}
37588 +
37589 +struct xino_do_trunc_args {
37590 +       struct super_block *sb;
37591 +       struct au_branch *br;
37592 +       int idx;
37593 +};
37594 +
37595 +static void xino_do_trunc(void *_args)
37596 +{
37597 +       struct xino_do_trunc_args *args = _args;
37598 +       struct super_block *sb;
37599 +       struct au_branch *br;
37600 +       struct inode *dir;
37601 +       int err, idx;
37602 +       aufs_bindex_t bindex;
37603 +
37604 +       err = 0;
37605 +       sb = args->sb;
37606 +       dir = d_inode(sb->s_root);
37607 +       br = args->br;
37608 +       idx = args->idx;
37609 +
37610 +       si_noflush_write_lock(sb);
37611 +       ii_read_lock_parent(dir);
37612 +       bindex = au_br_index(sb, br->br_id);
37613 +       err = au_xino_trunc(sb, bindex, idx);
37614 +       ii_read_unlock(dir);
37615 +       if (unlikely(err))
37616 +               pr_warn("err b%d, (%d)\n", bindex, err);
37617 +       atomic_dec(&br->br_xino->xi_truncating);
37618 +       au_lcnt_dec(&br->br_count);
37619 +       si_write_unlock(sb);
37620 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37621 +       au_kfree_rcu(args);
37622 +}
37623 +
37624 +/*
37625 + * returns the index in the xi_file array whose corresponding file is necessary
37626 + * to truncate, or -1 which means no need to truncate.
37627 + */
37628 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37629 +{
37630 +       int err;
37631 +       unsigned int u;
37632 +       struct kstatfs st;
37633 +       struct au_sbinfo *sbinfo;
37634 +       struct au_xino *xi;
37635 +       struct file *file;
37636 +
37637 +       /* todo: si_xino_expire and the ratio should be customizable */
37638 +       sbinfo = au_sbi(sb);
37639 +       if (time_before(jiffies,
37640 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37641 +               return -1;
37642 +
37643 +       /* truncation border */
37644 +       xi = br->br_xino;
37645 +       for (u = 0; u < xi->xi_nfile; u++) {
37646 +               file = au_xino_file(xi, u);
37647 +               if (!file)
37648 +                       continue;
37649 +
37650 +               err = vfs_statfs(&file->f_path, &st);
37651 +               if (unlikely(err)) {
37652 +                       AuErr1("statfs err %d, ignored\n", err);
37653 +                       return -1;
37654 +               }
37655 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37656 +                   >= AUFS_XINO_DEF_TRUNC)
37657 +                       return u;
37658 +       }
37659 +
37660 +       return -1;
37661 +}
37662 +
37663 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37664 +{
37665 +       int idx;
37666 +       struct xino_do_trunc_args *args;
37667 +       int wkq_err;
37668 +
37669 +       idx = xino_trunc_test(sb, br);
37670 +       if (idx < 0)
37671 +               return;
37672 +
37673 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37674 +               goto out;
37675 +
37676 +       /* lock and kfree() will be called in trunc_xino() */
37677 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37678 +       if (unlikely(!args)) {
37679 +               AuErr1("no memory\n");
37680 +               goto out;
37681 +       }
37682 +
37683 +       au_lcnt_inc(&br->br_count);
37684 +       args->sb = sb;
37685 +       args->br = br;
37686 +       args->idx = idx;
37687 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37688 +       if (!wkq_err)
37689 +               return; /* success */
37690 +
37691 +       pr_err("wkq %d\n", wkq_err);
37692 +       au_lcnt_dec(&br->br_count);
37693 +       au_kfree_rcu(args);
37694 +
37695 +out:
37696 +       atomic_dec(&br->br_xino->xi_truncating);
37697 +}
37698 +
37699 +/* ---------------------------------------------------------------------- */
37700 +
37701 +struct au_xi_calc {
37702 +       int idx;
37703 +       loff_t pos;
37704 +};
37705 +
37706 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37707 +                      struct au_xi_calc *calc)
37708 +{
37709 +       loff_t maxent;
37710 +
37711 +       maxent = au_xi_maxent(sb);
37712 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37713 +       calc->pos *= sizeof(ino_t);
37714 +}
37715 +
37716 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37717 +                               struct au_xi_calc *calc)
37718 +{
37719 +       int err;
37720 +       struct file *file;
37721 +       struct au_xino *xi = br->br_xino;
37722 +       struct au_xi_new xinew = {
37723 +               .xi = xi
37724 +       };
37725 +
37726 +       SiMustAnyLock(sb);
37727 +
37728 +       err = 0;
37729 +       if (!xi)
37730 +               goto out;
37731 +
37732 +       mutex_lock(&xi->xi_mtx);
37733 +       file = au_xino_file(xi, calc->idx);
37734 +       if (file)
37735 +               goto out_mtx;
37736 +
37737 +       file = au_xino_file(xi, /*idx*/-1);
37738 +       AuDebugOn(!file);
37739 +       xinew.idx = calc->idx;
37740 +       xinew.base = &file->f_path;
37741 +       /* xinew.copy_src = NULL; */
37742 +       file = au_xi_new(sb, &xinew);
37743 +       if (IS_ERR(file))
37744 +               err = PTR_ERR(file);
37745 +
37746 +out_mtx:
37747 +       mutex_unlock(&xi->xi_mtx);
37748 +out:
37749 +       return err;
37750 +}
37751 +
37752 +struct au_xino_do_new_async_args {
37753 +       struct super_block *sb;
37754 +       struct au_branch *br;
37755 +       struct au_xi_calc calc;
37756 +       ino_t ino;
37757 +};
37758 +
37759 +struct au_xi_writing {
37760 +       struct hlist_bl_node node;
37761 +       ino_t h_ino, ino;
37762 +};
37763 +
37764 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37765 +                           ino_t ino);
37766 +
37767 +static void au_xino_call_do_new_async(void *args)
37768 +{
37769 +       struct au_xino_do_new_async_args *a = args;
37770 +       struct au_branch *br;
37771 +       struct super_block *sb;
37772 +       struct au_sbinfo *sbi;
37773 +       struct inode *root;
37774 +       struct file *file;
37775 +       struct au_xi_writing *del, *p;
37776 +       struct hlist_bl_head *hbl;
37777 +       struct hlist_bl_node *pos;
37778 +       int err;
37779 +
37780 +       br = a->br;
37781 +       sb = a->sb;
37782 +       sbi = au_sbi(sb);
37783 +       si_noflush_read_lock(sb);
37784 +       root = d_inode(sb->s_root);
37785 +       ii_read_lock_child(root);
37786 +       err = au_xino_do_new_async(sb, br, &a->calc);
37787 +       if (unlikely(err)) {
37788 +               AuIOErr("err %d\n", err);
37789 +               goto out;
37790 +       }
37791 +
37792 +       file = au_xino_file(br->br_xino, a->calc.idx);
37793 +       AuDebugOn(!file);
37794 +       err = au_xino_do_write(file, &a->calc, a->ino);
37795 +       if (unlikely(err)) {
37796 +               AuIOErr("err %d\n", err);
37797 +               goto out;
37798 +       }
37799 +
37800 +       del = NULL;
37801 +       hbl = &br->br_xino->xi_writing;
37802 +       hlist_bl_lock(hbl);
37803 +       au_hbl_for_each(pos, hbl) {
37804 +               p = container_of(pos, struct au_xi_writing, node);
37805 +               if (p->ino == a->ino) {
37806 +                       del = p;
37807 +                       hlist_bl_del(&p->node);
37808 +                       break;
37809 +               }
37810 +       }
37811 +       hlist_bl_unlock(hbl);
37812 +       au_kfree_rcu(del);
37813 +
37814 +out:
37815 +       au_lcnt_dec(&br->br_count);
37816 +       ii_read_unlock(root);
37817 +       si_read_unlock(sb);
37818 +       au_nwt_done(&sbi->si_nowait);
37819 +       au_kfree_rcu(a);
37820 +}
37821 +
37822 +/*
37823 + * create a new xino file asynchronously
37824 + */
37825 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37826 +                            struct au_xi_calc *calc, ino_t ino)
37827 +{
37828 +       int err;
37829 +       struct au_xino_do_new_async_args *arg;
37830 +
37831 +       err = -ENOMEM;
37832 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37833 +       if (unlikely(!arg))
37834 +               goto out;
37835 +
37836 +       arg->sb = sb;
37837 +       arg->br = br;
37838 +       arg->calc = *calc;
37839 +       arg->ino = ino;
37840 +       au_lcnt_inc(&br->br_count);
37841 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37842 +       if (unlikely(err)) {
37843 +               pr_err("wkq %d\n", err);
37844 +               au_lcnt_dec(&br->br_count);
37845 +               au_kfree_rcu(arg);
37846 +       }
37847 +
37848 +out:
37849 +       return err;
37850 +}
37851 +
37852 +/*
37853 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37854 + * at the position of @h_ino.
37855 + */
37856 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37857 +                ino_t *ino)
37858 +{
37859 +       int err;
37860 +       ssize_t sz;
37861 +       struct au_xi_calc calc;
37862 +       struct au_sbinfo *sbinfo;
37863 +       struct file *file;
37864 +       struct au_xino *xi;
37865 +       struct hlist_bl_head *hbl;
37866 +       struct hlist_bl_node *pos;
37867 +       struct au_xi_writing *p;
37868 +
37869 +       *ino = 0;
37870 +       if (!au_opt_test(au_mntflags(sb), XINO))
37871 +               return 0; /* no xino */
37872 +
37873 +       err = 0;
37874 +       au_xi_calc(sb, h_ino, &calc);
37875 +       xi = au_sbr(sb, bindex)->br_xino;
37876 +       file = au_xino_file(xi, calc.idx);
37877 +       if (!file) {
37878 +               hbl = &xi->xi_writing;
37879 +               hlist_bl_lock(hbl);
37880 +               au_hbl_for_each(pos, hbl) {
37881 +                       p = container_of(pos, struct au_xi_writing, node);
37882 +                       if (p->h_ino == h_ino) {
37883 +                               AuDbg("hi%llu, i%llu, found\n",
37884 +                                     (u64)p->h_ino, (u64)p->ino);
37885 +                               *ino = p->ino;
37886 +                               break;
37887 +                       }
37888 +               }
37889 +               hlist_bl_unlock(hbl);
37890 +               return 0;
37891 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37892 +               return 0; /* no xino */
37893 +
37894 +       sbinfo = au_sbi(sb);
37895 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37896 +       if (sz == sizeof(*ino))
37897 +               return 0; /* success */
37898 +
37899 +       err = sz;
37900 +       if (unlikely(sz >= 0)) {
37901 +               err = -EIO;
37902 +               AuIOErr("xino read error (%zd)\n", sz);
37903 +       }
37904 +       return err;
37905 +}
37906 +
37907 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37908 +                           ino_t ino)
37909 +{
37910 +       ssize_t sz;
37911 +
37912 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37913 +       if (sz == sizeof(ino))
37914 +               return 0; /* success */
37915 +
37916 +       AuIOErr("write failed (%zd)\n", sz);
37917 +       return -EIO;
37918 +}
37919 +
37920 +/*
37921 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37922 + * at the position of @h_ino.
37923 + * even if @ino is zero, it is written to the xinofile and means no entry.
37924 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37925 + * try truncating it.
37926 + */
37927 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37928 +                 ino_t ino)
37929 +{
37930 +       int err;
37931 +       unsigned int mnt_flags;
37932 +       struct au_xi_calc calc;
37933 +       struct file *file;
37934 +       struct au_branch *br;
37935 +       struct au_xino *xi;
37936 +       struct au_xi_writing *p;
37937 +
37938 +       SiMustAnyLock(sb);
37939 +
37940 +       mnt_flags = au_mntflags(sb);
37941 +       if (!au_opt_test(mnt_flags, XINO))
37942 +               return 0;
37943 +
37944 +       au_xi_calc(sb, h_ino, &calc);
37945 +       br = au_sbr(sb, bindex);
37946 +       xi = br->br_xino;
37947 +       file = au_xino_file(xi, calc.idx);
37948 +       if (!file) {
37949 +               /* store the inum pair into the list */
37950 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37951 +               p->h_ino = h_ino;
37952 +               p->ino = ino;
37953 +               au_hbl_add(&p->node, &xi->xi_writing);
37954 +
37955 +               /* create and write a new xino file asynchronously */
37956 +               err = au_xino_new_async(sb, br, &calc, ino);
37957 +               if (!err)
37958 +                       return 0; /* success */
37959 +               goto out;
37960 +       }
37961 +
37962 +       err = au_xino_do_write(file, &calc, ino);
37963 +       if (!err) {
37964 +               br = au_sbr(sb, bindex);
37965 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37966 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37967 +                       xino_try_trunc(sb, br);
37968 +               return 0; /* success */
37969 +       }
37970 +
37971 +out:
37972 +       AuIOErr("write failed (%d)\n", err);
37973 +       return -EIO;
37974 +}
37975 +
37976 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37977 +                             loff_t *pos);
37978 +
37979 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37980 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37981 +{
37982 +       ssize_t err;
37983 +       int i;
37984 +       const int prevent_endless = 10;
37985 +
37986 +       i = 0;
37987 +       do {
37988 +               err = vfsub_read_k(file, kbuf, size, pos);
37989 +               if (err == -EINTR
37990 +                   && !au_wkq_test()
37991 +                   && fatal_signal_pending(current)) {
37992 +                       err = xino_fread_wkq(file, kbuf, size, pos);
37993 +                       BUG_ON(err == -EINTR);
37994 +               }
37995 +       } while (i++ < prevent_endless
37996 +                && (err == -EAGAIN || err == -EINTR));
37997 +
37998 +#if 0 /* reserved for future use */
37999 +       if (err > 0)
38000 +               fsnotify_access(file->f_path.dentry);
38001 +#endif
38002 +
38003 +       return err;
38004 +}
38005 +
38006 +struct xino_fread_args {
38007 +       ssize_t *errp;
38008 +       struct file *file;
38009 +       void *buf;
38010 +       size_t size;
38011 +       loff_t *pos;
38012 +};
38013 +
38014 +static void call_xino_fread(void *args)
38015 +{
38016 +       struct xino_fread_args *a = args;
38017 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
38018 +}
38019 +
38020 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38021 +                             loff_t *pos)
38022 +{
38023 +       ssize_t err;
38024 +       int wkq_err;
38025 +       struct xino_fread_args args = {
38026 +               .errp   = &err,
38027 +               .file   = file,
38028 +               .buf    = buf,
38029 +               .size   = size,
38030 +               .pos    = pos
38031 +       };
38032 +
38033 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
38034 +       if (unlikely(wkq_err))
38035 +               err = wkq_err;
38036 +
38037 +       return err;
38038 +}
38039 +
38040 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38041 +                              loff_t *pos);
38042 +
38043 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
38044 +                             loff_t *pos)
38045 +{
38046 +       ssize_t err;
38047 +       int i;
38048 +       const int prevent_endless = 10;
38049 +
38050 +       i = 0;
38051 +       do {
38052 +               err = vfsub_write_k(file, kbuf, size, pos);
38053 +               if (err == -EINTR
38054 +                   && !au_wkq_test()
38055 +                   && fatal_signal_pending(current)) {
38056 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
38057 +                       BUG_ON(err == -EINTR);
38058 +               }
38059 +       } while (i++ < prevent_endless
38060 +                && (err == -EAGAIN || err == -EINTR));
38061 +
38062 +#if 0 /* reserved for future use */
38063 +       if (err > 0)
38064 +               fsnotify_modify(file->f_path.dentry);
38065 +#endif
38066 +
38067 +       return err;
38068 +}
38069 +
38070 +struct do_xino_fwrite_args {
38071 +       ssize_t *errp;
38072 +       struct file *file;
38073 +       void *buf;
38074 +       size_t size;
38075 +       loff_t *pos;
38076 +};
38077 +
38078 +static void call_do_xino_fwrite(void *args)
38079 +{
38080 +       struct do_xino_fwrite_args *a = args;
38081 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
38082 +}
38083 +
38084 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38085 +                              loff_t *pos)
38086 +{
38087 +       ssize_t err;
38088 +       int wkq_err;
38089 +       struct do_xino_fwrite_args args = {
38090 +               .errp   = &err,
38091 +               .file   = file,
38092 +               .buf    = buf,
38093 +               .size   = size,
38094 +               .pos    = pos
38095 +       };
38096 +
38097 +       /*
38098 +        * it breaks RLIMIT_FSIZE and normal user's limit,
38099 +        * users should care about quota and real 'filesystem full.'
38100 +        */
38101 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38102 +       if (unlikely(wkq_err))
38103 +               err = wkq_err;
38104 +
38105 +       return err;
38106 +}
38107 +
38108 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
38109 +{
38110 +       ssize_t err;
38111 +
38112 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38113 +               lockdep_off();
38114 +               err = do_xino_fwrite(file, buf, size, pos);
38115 +               lockdep_on();
38116 +       } else {
38117 +               lockdep_off();
38118 +               err = xino_fwrite_wkq(file, buf, size, pos);
38119 +               lockdep_on();
38120 +       }
38121 +
38122 +       return err;
38123 +}
38124 +
38125 +/* ---------------------------------------------------------------------- */
38126 +
38127 +/*
38128 + * inode number bitmap
38129 + */
38130 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38131 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
38132 +{
38133 +       ino_t ino;
38134 +
38135 +       AuDebugOn(bit < 0 || page_bits <= bit);
38136 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38137 +       return ino;
38138 +}
38139 +
38140 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38141 +{
38142 +       AuDebugOn(ino < AUFS_FIRST_INO);
38143 +       ino -= AUFS_FIRST_INO;
38144 +       *pindex = ino / page_bits;
38145 +       *bit = ino % page_bits;
38146 +}
38147 +
38148 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
38149 +{
38150 +       int err;
38151 +       loff_t pos;
38152 +       ssize_t sz;
38153 +       struct au_sbinfo *sbinfo;
38154 +       struct file *xib;
38155 +       unsigned long *p;
38156 +
38157 +       sbinfo = au_sbi(sb);
38158 +       MtxMustLock(&sbinfo->si_xib_mtx);
38159 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38160 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38161 +
38162 +       if (pindex == sbinfo->si_xib_last_pindex)
38163 +               return 0;
38164 +
38165 +       xib = sbinfo->si_xib;
38166 +       p = sbinfo->si_xib_buf;
38167 +       pos = sbinfo->si_xib_last_pindex;
38168 +       pos *= PAGE_SIZE;
38169 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38170 +       if (unlikely(sz != PAGE_SIZE))
38171 +               goto out;
38172 +
38173 +       pos = pindex;
38174 +       pos *= PAGE_SIZE;
38175 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38176 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
38177 +       else {
38178 +               memset(p, 0, PAGE_SIZE);
38179 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38180 +       }
38181 +       if (sz == PAGE_SIZE) {
38182 +               sbinfo->si_xib_last_pindex = pindex;
38183 +               return 0; /* success */
38184 +       }
38185 +
38186 +out:
38187 +       AuIOErr1("write failed (%zd)\n", sz);
38188 +       err = sz;
38189 +       if (sz >= 0)
38190 +               err = -EIO;
38191 +       return err;
38192 +}
38193 +
38194 +static void au_xib_clear_bit(struct inode *inode)
38195 +{
38196 +       int err, bit;
38197 +       unsigned long pindex;
38198 +       struct super_block *sb;
38199 +       struct au_sbinfo *sbinfo;
38200 +
38201 +       AuDebugOn(inode->i_nlink);
38202 +
38203 +       sb = inode->i_sb;
38204 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38205 +       AuDebugOn(page_bits <= bit);
38206 +       sbinfo = au_sbi(sb);
38207 +       mutex_lock(&sbinfo->si_xib_mtx);
38208 +       err = xib_pindex(sb, pindex);
38209 +       if (!err) {
38210 +               clear_bit(bit, sbinfo->si_xib_buf);
38211 +               sbinfo->si_xib_next_bit = bit;
38212 +       }
38213 +       mutex_unlock(&sbinfo->si_xib_mtx);
38214 +}
38215 +
38216 +/* ---------------------------------------------------------------------- */
38217 +
38218 +/*
38219 + * truncate a xino bitmap file
38220 + */
38221 +
38222 +/* todo: slow */
38223 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38224 +{
38225 +       int err, bit;
38226 +       ssize_t sz;
38227 +       unsigned long pindex;
38228 +       loff_t pos, pend;
38229 +       struct au_sbinfo *sbinfo;
38230 +       ino_t *ino;
38231 +       unsigned long *p;
38232 +
38233 +       err = 0;
38234 +       sbinfo = au_sbi(sb);
38235 +       MtxMustLock(&sbinfo->si_xib_mtx);
38236 +       p = sbinfo->si_xib_buf;
38237 +       pend = vfsub_f_size_read(file);
38238 +       pos = 0;
38239 +       while (pos < pend) {
38240 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
38241 +               err = sz;
38242 +               if (unlikely(sz <= 0))
38243 +                       goto out;
38244 +
38245 +               err = 0;
38246 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38247 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38248 +                               continue;
38249 +
38250 +                       xib_calc_bit(*ino, &pindex, &bit);
38251 +                       AuDebugOn(page_bits <= bit);
38252 +                       err = xib_pindex(sb, pindex);
38253 +                       if (!err)
38254 +                               set_bit(bit, p);
38255 +                       else
38256 +                               goto out;
38257 +               }
38258 +       }
38259 +
38260 +out:
38261 +       return err;
38262 +}
38263 +
38264 +static int xib_restore(struct super_block *sb)
38265 +{
38266 +       int err, i;
38267 +       unsigned int nfile;
38268 +       aufs_bindex_t bindex, bbot;
38269 +       void *page;
38270 +       struct au_branch *br;
38271 +       struct au_xino *xi;
38272 +       struct file *file;
38273 +
38274 +       err = -ENOMEM;
38275 +       page = (void *)__get_free_page(GFP_NOFS);
38276 +       if (unlikely(!page))
38277 +               goto out;
38278 +
38279 +       err = 0;
38280 +       bbot = au_sbbot(sb);
38281 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38282 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38283 +                       br = au_sbr(sb, bindex);
38284 +                       xi = br->br_xino;
38285 +                       nfile = xi->xi_nfile;
38286 +                       for (i = 0; i < nfile; i++) {
38287 +                               file = au_xino_file(xi, i);
38288 +                               if (file)
38289 +                                       err = do_xib_restore(sb, file, page);
38290 +                       }
38291 +               } else
38292 +                       AuDbg("skip shared b%d\n", bindex);
38293 +       free_page((unsigned long)page);
38294 +
38295 +out:
38296 +       return err;
38297 +}
38298 +
38299 +int au_xib_trunc(struct super_block *sb)
38300 +{
38301 +       int err;
38302 +       ssize_t sz;
38303 +       loff_t pos;
38304 +       struct au_sbinfo *sbinfo;
38305 +       unsigned long *p;
38306 +       struct file *file;
38307 +
38308 +       SiMustWriteLock(sb);
38309 +
38310 +       err = 0;
38311 +       sbinfo = au_sbi(sb);
38312 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38313 +               goto out;
38314 +
38315 +       file = sbinfo->si_xib;
38316 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38317 +               goto out;
38318 +
38319 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38320 +       err = PTR_ERR(file);
38321 +       if (IS_ERR(file))
38322 +               goto out;
38323 +       fput(sbinfo->si_xib);
38324 +       sbinfo->si_xib = file;
38325 +
38326 +       p = sbinfo->si_xib_buf;
38327 +       memset(p, 0, PAGE_SIZE);
38328 +       pos = 0;
38329 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38330 +       if (unlikely(sz != PAGE_SIZE)) {
38331 +               err = sz;
38332 +               AuIOErr("err %d\n", err);
38333 +               if (sz >= 0)
38334 +                       err = -EIO;
38335 +               goto out;
38336 +       }
38337 +
38338 +       mutex_lock(&sbinfo->si_xib_mtx);
38339 +       /* mnt_want_write() is unnecessary here */
38340 +       err = xib_restore(sb);
38341 +       mutex_unlock(&sbinfo->si_xib_mtx);
38342 +
38343 +out:
38344 +       return err;
38345 +}
38346 +
38347 +/* ---------------------------------------------------------------------- */
38348 +
38349 +struct au_xino *au_xino_alloc(unsigned int nfile)
38350 +{
38351 +       struct au_xino *xi;
38352 +
38353 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38354 +       if (unlikely(!xi))
38355 +               goto out;
38356 +       xi->xi_nfile = nfile;
38357 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38358 +       if (unlikely(!xi->xi_file))
38359 +               goto out_free;
38360 +
38361 +       xi->xi_nondir.total = 8; /* initial size */
38362 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38363 +                                     GFP_NOFS);
38364 +       if (unlikely(!xi->xi_nondir.array))
38365 +               goto out_file;
38366 +
38367 +       spin_lock_init(&xi->xi_nondir.spin);
38368 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38369 +       mutex_init(&xi->xi_mtx);
38370 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38371 +       atomic_set(&xi->xi_truncating, 0);
38372 +       kref_init(&xi->xi_kref);
38373 +       goto out; /* success */
38374 +
38375 +out_file:
38376 +       au_kfree_try_rcu(xi->xi_file);
38377 +out_free:
38378 +       au_kfree_rcu(xi);
38379 +       xi = NULL;
38380 +out:
38381 +       return xi;
38382 +}
38383 +
38384 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38385 +{
38386 +       int err;
38387 +       struct au_xino *xi;
38388 +
38389 +       err = 0;
38390 +       xi = au_xino_alloc(idx + 1);
38391 +       if (unlikely(!xi)) {
38392 +               err = -ENOMEM;
38393 +               goto out;
38394 +       }
38395 +
38396 +       if (file)
38397 +               get_file(file);
38398 +       xi->xi_file[idx] = file;
38399 +       AuDebugOn(br->br_xino);
38400 +       br->br_xino = xi;
38401 +
38402 +out:
38403 +       return err;
38404 +}
38405 +
38406 +static void au_xino_release(struct kref *kref)
38407 +{
38408 +       struct au_xino *xi;
38409 +       int i;
38410 +       unsigned long ul;
38411 +       struct hlist_bl_head *hbl;
38412 +       struct hlist_bl_node *pos, *n;
38413 +       struct au_xi_writing *p;
38414 +
38415 +       xi = container_of(kref, struct au_xino, xi_kref);
38416 +       for (i = 0; i < xi->xi_nfile; i++)
38417 +               if (xi->xi_file[i])
38418 +                       fput(xi->xi_file[i]);
38419 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38420 +               AuDebugOn(xi->xi_nondir.array[i]);
38421 +       mutex_destroy(&xi->xi_mtx);
38422 +       hbl = &xi->xi_writing;
38423 +       ul = au_hbl_count(hbl);
38424 +       if (unlikely(ul)) {
38425 +               pr_warn("xi_writing %lu\n", ul);
38426 +               hlist_bl_lock(hbl);
38427 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38428 +                       hlist_bl_del(&p->node);
38429 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38430 +                       kfree(p);
38431 +               }
38432 +               hlist_bl_unlock(hbl);
38433 +       }
38434 +       au_kfree_try_rcu(xi->xi_file);
38435 +       au_kfree_try_rcu(xi->xi_nondir.array);
38436 +       au_kfree_rcu(xi);
38437 +}
38438 +
38439 +int au_xino_put(struct au_branch *br)
38440 +{
38441 +       int ret;
38442 +       struct au_xino *xi;
38443 +
38444 +       ret = 0;
38445 +       xi = br->br_xino;
38446 +       if (xi) {
38447 +               br->br_xino = NULL;
38448 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38449 +       }
38450 +
38451 +       return ret;
38452 +}
38453 +
38454 +/* ---------------------------------------------------------------------- */
38455 +
38456 +/*
38457 + * xino mount option handlers
38458 + */
38459 +
38460 +/* xino bitmap */
38461 +static void xino_clear_xib(struct super_block *sb)
38462 +{
38463 +       struct au_sbinfo *sbinfo;
38464 +
38465 +       SiMustWriteLock(sb);
38466 +
38467 +       sbinfo = au_sbi(sb);
38468 +       if (sbinfo->si_xib)
38469 +               fput(sbinfo->si_xib);
38470 +       sbinfo->si_xib = NULL;
38471 +       if (sbinfo->si_xib_buf)
38472 +               free_page((unsigned long)sbinfo->si_xib_buf);
38473 +       sbinfo->si_xib_buf = NULL;
38474 +}
38475 +
38476 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38477 +{
38478 +       int err;
38479 +       loff_t pos;
38480 +       struct au_sbinfo *sbinfo;
38481 +       struct file *file;
38482 +       struct super_block *xi_sb;
38483 +
38484 +       SiMustWriteLock(sb);
38485 +
38486 +       sbinfo = au_sbi(sb);
38487 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38488 +       err = PTR_ERR(file);
38489 +       if (IS_ERR(file))
38490 +               goto out;
38491 +       if (sbinfo->si_xib)
38492 +               fput(sbinfo->si_xib);
38493 +       sbinfo->si_xib = file;
38494 +       xi_sb = file_inode(file)->i_sb;
38495 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38496 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38497 +               err = -EIO;
38498 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38499 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38500 +               goto out_unset;
38501 +       }
38502 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38503 +
38504 +       err = -ENOMEM;
38505 +       if (!sbinfo->si_xib_buf)
38506 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38507 +       if (unlikely(!sbinfo->si_xib_buf))
38508 +               goto out_unset;
38509 +
38510 +       sbinfo->si_xib_last_pindex = 0;
38511 +       sbinfo->si_xib_next_bit = 0;
38512 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38513 +               pos = 0;
38514 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38515 +               if (unlikely(err != PAGE_SIZE))
38516 +                       goto out_free;
38517 +       }
38518 +       err = 0;
38519 +       goto out; /* success */
38520 +
38521 +out_free:
38522 +       if (sbinfo->si_xib_buf)
38523 +               free_page((unsigned long)sbinfo->si_xib_buf);
38524 +       sbinfo->si_xib_buf = NULL;
38525 +       if (err >= 0)
38526 +               err = -EIO;
38527 +out_unset:
38528 +       fput(sbinfo->si_xib);
38529 +       sbinfo->si_xib = NULL;
38530 +out:
38531 +       AuTraceErr(err);
38532 +       return err;
38533 +}
38534 +
38535 +/* xino for each branch */
38536 +static void xino_clear_br(struct super_block *sb)
38537 +{
38538 +       aufs_bindex_t bindex, bbot;
38539 +       struct au_branch *br;
38540 +
38541 +       bbot = au_sbbot(sb);
38542 +       for (bindex = 0; bindex <= bbot; bindex++) {
38543 +               br = au_sbr(sb, bindex);
38544 +               AuDebugOn(!br);
38545 +               au_xino_put(br);
38546 +       }
38547 +}
38548 +
38549 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38550 +                                 aufs_bindex_t bshared)
38551 +{
38552 +       struct au_branch *brshared;
38553 +
38554 +       brshared = au_sbr(sb, bshared);
38555 +       AuDebugOn(!brshared->br_xino);
38556 +       AuDebugOn(!brshared->br_xino->xi_file);
38557 +       if (br->br_xino != brshared->br_xino) {
38558 +               au_xino_get(brshared);
38559 +               au_xino_put(br);
38560 +               br->br_xino = brshared->br_xino;
38561 +       }
38562 +}
38563 +
38564 +struct au_xino_do_set_br {
38565 +       struct au_branch *br;
38566 +       ino_t h_ino;
38567 +       aufs_bindex_t bshared;
38568 +};
38569 +
38570 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38571 +                            struct au_xino_do_set_br *args)
38572 +{
38573 +       int err;
38574 +       struct au_xi_calc calc;
38575 +       struct file *file;
38576 +       struct au_branch *br;
38577 +       struct au_xi_new xinew = {
38578 +               .base = path
38579 +       };
38580 +
38581 +       br = args->br;
38582 +       xinew.xi = br->br_xino;
38583 +       au_xi_calc(sb, args->h_ino, &calc);
38584 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38585 +       if (args->bshared >= 0)
38586 +               /* shared xino */
38587 +               au_xino_set_br_shared(sb, br, args->bshared);
38588 +       else if (!xinew.xi) {
38589 +               /* new xino */
38590 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38591 +               if (unlikely(err))
38592 +                       goto out;
38593 +       }
38594 +
38595 +       /* force re-creating */
38596 +       xinew.xi = br->br_xino;
38597 +       xinew.idx = calc.idx;
38598 +       mutex_lock(&xinew.xi->xi_mtx);
38599 +       file = au_xi_new(sb, &xinew);
38600 +       mutex_unlock(&xinew.xi->xi_mtx);
38601 +       err = PTR_ERR(file);
38602 +       if (IS_ERR(file))
38603 +               goto out;
38604 +       AuDebugOn(!file);
38605 +
38606 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38607 +       if (unlikely(err))
38608 +               au_xino_put(br);
38609 +
38610 +out:
38611 +       AuTraceErr(err);
38612 +       return err;
38613 +}
38614 +
38615 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38616 +{
38617 +       int err;
38618 +       aufs_bindex_t bindex, bbot;
38619 +       struct au_xino_do_set_br args;
38620 +       struct inode *inode;
38621 +
38622 +       SiMustWriteLock(sb);
38623 +
38624 +       bbot = au_sbbot(sb);
38625 +       inode = d_inode(sb->s_root);
38626 +       for (bindex = 0; bindex <= bbot; bindex++) {
38627 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38628 +               args.br = au_sbr(sb, bindex);
38629 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38630 +               err = au_xino_do_set_br(sb, path, &args);
38631 +               if (unlikely(err))
38632 +                       break;
38633 +       }
38634 +
38635 +       AuTraceErr(err);
38636 +       return err;
38637 +}
38638 +
38639 +void au_xino_clr(struct super_block *sb)
38640 +{
38641 +       struct au_sbinfo *sbinfo;
38642 +
38643 +       au_xigen_clr(sb);
38644 +       xino_clear_xib(sb);
38645 +       xino_clear_br(sb);
38646 +       dbgaufs_brs_del(sb, 0);
38647 +       sbinfo = au_sbi(sb);
38648 +       /* lvalue, do not call au_mntflags() */
38649 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38650 +}
38651 +
38652 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38653 +{
38654 +       int err, skip;
38655 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38656 +       struct qstr *dname, *cur_name;
38657 +       struct file *cur_xino;
38658 +       struct au_sbinfo *sbinfo;
38659 +       struct path *path, *cur_path;
38660 +
38661 +       SiMustWriteLock(sb);
38662 +
38663 +       err = 0;
38664 +       sbinfo = au_sbi(sb);
38665 +       path = &xiopt->file->f_path;
38666 +       dentry = path->dentry;
38667 +       parent = dget_parent(dentry);
38668 +       if (remount) {
38669 +               skip = 0;
38670 +               cur_xino = sbinfo->si_xib;
38671 +               if (cur_xino) {
38672 +                       cur_path = &cur_xino->f_path;
38673 +                       cur_dentry = cur_path->dentry;
38674 +                       cur_parent = dget_parent(cur_dentry);
38675 +                       cur_name = &cur_dentry->d_name;
38676 +                       dname = &dentry->d_name;
38677 +                       skip = (cur_parent == parent
38678 +                               && au_qstreq(dname, cur_name));
38679 +                       dput(cur_parent);
38680 +               }
38681 +               if (skip)
38682 +                       goto out;
38683 +       }
38684 +
38685 +       au_opt_set(sbinfo->si_mntflags, XINO);
38686 +       err = au_xino_set_xib(sb, path);
38687 +       /* si_x{read,write} are set */
38688 +       if (!err)
38689 +               err = au_xigen_set(sb, path);
38690 +       if (!err)
38691 +               err = au_xino_set_br(sb, path);
38692 +       if (!err) {
38693 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38694 +               goto out; /* success */
38695 +       }
38696 +
38697 +       /* reset all */
38698 +       AuIOErr("failed setting xino(%d).\n", err);
38699 +       au_xino_clr(sb);
38700 +
38701 +out:
38702 +       dput(parent);
38703 +       return err;
38704 +}
38705 +
38706 +/*
38707 + * create a xinofile at the default place/path.
38708 + */
38709 +struct file *au_xino_def(struct super_block *sb)
38710 +{
38711 +       struct file *file;
38712 +       char *page, *p;
38713 +       struct au_branch *br;
38714 +       struct super_block *h_sb;
38715 +       struct path path;
38716 +       aufs_bindex_t bbot, bindex, bwr;
38717 +
38718 +       br = NULL;
38719 +       bbot = au_sbbot(sb);
38720 +       bwr = -1;
38721 +       for (bindex = 0; bindex <= bbot; bindex++) {
38722 +               br = au_sbr(sb, bindex);
38723 +               if (au_br_writable(br->br_perm)
38724 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38725 +                       bwr = bindex;
38726 +                       break;
38727 +               }
38728 +       }
38729 +
38730 +       if (bwr >= 0) {
38731 +               file = ERR_PTR(-ENOMEM);
38732 +               page = (void *)__get_free_page(GFP_NOFS);
38733 +               if (unlikely(!page))
38734 +                       goto out;
38735 +               path.mnt = au_br_mnt(br);
38736 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38737 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38738 +               file = (void *)p;
38739 +               if (!IS_ERR(p)) {
38740 +                       strcat(p, "/" AUFS_XINO_FNAME);
38741 +                       AuDbg("%s\n", p);
38742 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38743 +               }
38744 +               free_page((unsigned long)page);
38745 +       } else {
38746 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38747 +                                     /*wbrtop*/0);
38748 +               if (IS_ERR(file))
38749 +                       goto out;
38750 +               h_sb = file->f_path.dentry->d_sb;
38751 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38752 +                       pr_err("xino doesn't support %s(%s)\n",
38753 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38754 +                       fput(file);
38755 +                       file = ERR_PTR(-EINVAL);
38756 +               }
38757 +       }
38758 +
38759 +out:
38760 +       return file;
38761 +}
38762 +
38763 +/* ---------------------------------------------------------------------- */
38764 +
38765 +/*
38766 + * initialize the xinofile for the specified branch @br
38767 + * at the place/path where @base_file indicates.
38768 + * test whether another branch is on the same filesystem or not,
38769 + * if found then share the xinofile with another branch.
38770 + */
38771 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38772 +                   struct path *base)
38773 +{
38774 +       int err;
38775 +       struct au_xino_do_set_br args = {
38776 +               .h_ino  = h_ino,
38777 +               .br     = br
38778 +       };
38779 +
38780 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38781 +                                      au_br_sb(br));
38782 +       err = au_xino_do_set_br(sb, base, &args);
38783 +       if (unlikely(err))
38784 +               au_xino_put(br);
38785 +
38786 +       return err;
38787 +}
38788 +
38789 +/* ---------------------------------------------------------------------- */
38790 +
38791 +/*
38792 + * get an unused inode number from bitmap
38793 + */
38794 +ino_t au_xino_new_ino(struct super_block *sb)
38795 +{
38796 +       ino_t ino;
38797 +       unsigned long *p, pindex, ul, pend;
38798 +       struct au_sbinfo *sbinfo;
38799 +       struct file *file;
38800 +       int free_bit, err;
38801 +
38802 +       if (!au_opt_test(au_mntflags(sb), XINO))
38803 +               return iunique(sb, AUFS_FIRST_INO);
38804 +
38805 +       sbinfo = au_sbi(sb);
38806 +       mutex_lock(&sbinfo->si_xib_mtx);
38807 +       p = sbinfo->si_xib_buf;
38808 +       free_bit = sbinfo->si_xib_next_bit;
38809 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38810 +               goto out; /* success */
38811 +       free_bit = find_first_zero_bit(p, page_bits);
38812 +       if (free_bit < page_bits)
38813 +               goto out; /* success */
38814 +
38815 +       pindex = sbinfo->si_xib_last_pindex;
38816 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38817 +               err = xib_pindex(sb, ul);
38818 +               if (unlikely(err))
38819 +                       goto out_err;
38820 +               free_bit = find_first_zero_bit(p, page_bits);
38821 +               if (free_bit < page_bits)
38822 +                       goto out; /* success */
38823 +       }
38824 +
38825 +       file = sbinfo->si_xib;
38826 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38827 +       for (ul = pindex + 1; ul <= pend; ul++) {
38828 +               err = xib_pindex(sb, ul);
38829 +               if (unlikely(err))
38830 +                       goto out_err;
38831 +               free_bit = find_first_zero_bit(p, page_bits);
38832 +               if (free_bit < page_bits)
38833 +                       goto out; /* success */
38834 +       }
38835 +       BUG();
38836 +
38837 +out:
38838 +       set_bit(free_bit, p);
38839 +       sbinfo->si_xib_next_bit = free_bit + 1;
38840 +       pindex = sbinfo->si_xib_last_pindex;
38841 +       mutex_unlock(&sbinfo->si_xib_mtx);
38842 +       ino = xib_calc_ino(pindex, free_bit);
38843 +       AuDbg("i%lu\n", (unsigned long)ino);
38844 +       return ino;
38845 +out_err:
38846 +       mutex_unlock(&sbinfo->si_xib_mtx);
38847 +       AuDbg("i0\n");
38848 +       return 0;
38849 +}
38850 +
38851 +/* for s_op->delete_inode() */
38852 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38853 +{
38854 +       int err;
38855 +       unsigned int mnt_flags;
38856 +       aufs_bindex_t bindex, bbot, bi;
38857 +       unsigned char try_trunc;
38858 +       struct au_iinfo *iinfo;
38859 +       struct super_block *sb;
38860 +       struct au_hinode *hi;
38861 +       struct inode *h_inode;
38862 +       struct au_branch *br;
38863 +       struct au_xi_calc calc;
38864 +       struct file *file;
38865 +
38866 +       AuDebugOn(au_is_bad_inode(inode));
38867 +
38868 +       sb = inode->i_sb;
38869 +       mnt_flags = au_mntflags(sb);
38870 +       if (!au_opt_test(mnt_flags, XINO)
38871 +           || inode->i_ino == AUFS_ROOT_INO)
38872 +               return;
38873 +
38874 +       if (unlinked) {
38875 +               au_xigen_inc(inode);
38876 +               au_xib_clear_bit(inode);
38877 +       }
38878 +
38879 +       iinfo = au_ii(inode);
38880 +       bindex = iinfo->ii_btop;
38881 +       if (bindex < 0)
38882 +               return;
38883 +
38884 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38885 +       hi = au_hinode(iinfo, bindex);
38886 +       bbot = iinfo->ii_bbot;
38887 +       for (; bindex <= bbot; bindex++, hi++) {
38888 +               h_inode = hi->hi_inode;
38889 +               if (!h_inode
38890 +                   || (!unlinked && h_inode->i_nlink))
38891 +                       continue;
38892 +
38893 +               /* inode may not be revalidated */
38894 +               bi = au_br_index(sb, hi->hi_id);
38895 +               if (bi < 0)
38896 +                       continue;
38897 +
38898 +               br = au_sbr(sb, bi);
38899 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38900 +               file = au_xino_file(br->br_xino, calc.idx);
38901 +               if (IS_ERR_OR_NULL(file))
38902 +                       continue;
38903 +
38904 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38905 +               if (!err && try_trunc
38906 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38907 +                       xino_try_trunc(sb, br);
38908 +       }
38909 +}
38910 +
38911 +/* ---------------------------------------------------------------------- */
38912 +
38913 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38914 +{
38915 +       int found, total, i;
38916 +
38917 +       found = -1;
38918 +       total = xi->xi_nondir.total;
38919 +       for (i = 0; i < total; i++) {
38920 +               if (xi->xi_nondir.array[i] != h_ino)
38921 +                       continue;
38922 +               found = i;
38923 +               break;
38924 +       }
38925 +
38926 +       return found;
38927 +}
38928 +
38929 +static int au_xinondir_expand(struct au_xino *xi)
38930 +{
38931 +       int err, sz;
38932 +       ino_t *p;
38933 +
38934 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38935 +
38936 +       err = -ENOMEM;
38937 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38938 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38939 +               goto out;
38940 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38941 +                        /*may_shrink*/0);
38942 +       if (p) {
38943 +               xi->xi_nondir.array = p;
38944 +               xi->xi_nondir.total <<= 1;
38945 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38946 +               err = 0;
38947 +       }
38948 +
38949 +out:
38950 +       return err;
38951 +}
38952 +
38953 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38954 +                      ino_t h_ino, int idx)
38955 +{
38956 +       struct au_xino *xi;
38957 +
38958 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38959 +       xi = au_sbr(sb, bindex)->br_xino;
38960 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38961 +
38962 +       spin_lock(&xi->xi_nondir.spin);
38963 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38964 +       xi->xi_nondir.array[idx] = 0;
38965 +       spin_unlock(&xi->xi_nondir.spin);
38966 +       wake_up_all(&xi->xi_nondir.wqh);
38967 +}
38968 +
38969 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38970 +                     int *idx)
38971 +{
38972 +       int err, found, empty;
38973 +       struct au_xino *xi;
38974 +
38975 +       err = 0;
38976 +       *idx = -1;
38977 +       if (!au_opt_test(au_mntflags(sb), XINO))
38978 +               goto out; /* no xino */
38979 +
38980 +       xi = au_sbr(sb, bindex)->br_xino;
38981 +
38982 +again:
38983 +       spin_lock(&xi->xi_nondir.spin);
38984 +       found = au_xinondir_find(xi, h_ino);
38985 +       if (found == -1) {
38986 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38987 +               if (empty == -1) {
38988 +                       empty = xi->xi_nondir.total;
38989 +                       err = au_xinondir_expand(xi);
38990 +                       if (unlikely(err))
38991 +                               goto out_unlock;
38992 +               }
38993 +               xi->xi_nondir.array[empty] = h_ino;
38994 +               *idx = empty;
38995 +       } else {
38996 +               spin_unlock(&xi->xi_nondir.spin);
38997 +               wait_event(xi->xi_nondir.wqh,
38998 +                          xi->xi_nondir.array[found] != h_ino);
38999 +               goto again;
39000 +       }
39001 +
39002 +out_unlock:
39003 +       spin_unlock(&xi->xi_nondir.spin);
39004 +out:
39005 +       return err;
39006 +}
39007 +
39008 +/* ---------------------------------------------------------------------- */
39009 +
39010 +int au_xino_path(struct seq_file *seq, struct file *file)
39011 +{
39012 +       int err;
39013 +
39014 +       err = au_seq_path(seq, &file->f_path);
39015 +       if (unlikely(err))
39016 +               goto out;
39017 +
39018 +#define Deleted "\\040(deleted)"
39019 +       seq->count -= sizeof(Deleted) - 1;
39020 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
39021 +                        sizeof(Deleted) - 1));
39022 +#undef Deleted
39023 +
39024 +out:
39025 +       return err;
39026 +}
39027 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
39028 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
39029 +++ linux/include/uapi/linux/aufs_type.h        2023-09-04 13:40:14.109962274 +0200
39030 @@ -0,0 +1,452 @@
39031 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
39032 +/*
39033 + * Copyright (C) 2005-2022 Junjiro R. Okajima
39034 + *
39035 + * This program is free software; you can redistribute it and/or modify
39036 + * it under the terms of the GNU General Public License as published by
39037 + * the Free Software Foundation; either version 2 of the License, or
39038 + * (at your option) any later version.
39039 + *
39040 + * This program is distributed in the hope that it will be useful,
39041 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
39042 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39043 + * GNU General Public License for more details.
39044 + *
39045 + * You should have received a copy of the GNU General Public License
39046 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
39047 + */
39048 +
39049 +#ifndef __AUFS_TYPE_H__
39050 +#define __AUFS_TYPE_H__
39051 +
39052 +#define AUFS_NAME      "aufs"
39053 +
39054 +#ifdef __KERNEL__
39055 +/*
39056 + * define it before including all other headers.
39057 + * sched.h may use pr_* macros before defining "current", so define the
39058 + * no-current version first, and re-define later.
39059 + */
39060 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39061 +#include <linux/sched.h>
39062 +#undef pr_fmt
39063 +#define pr_fmt(fmt) \
39064 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39065 +               (int)sizeof(current->comm), current->comm, current->pid
39066 +#include <linux/limits.h>
39067 +#else
39068 +#include <stdint.h>
39069 +#include <sys/types.h>
39070 +#include <limits.h>
39071 +#endif /* __KERNEL__ */
39072 +
39073 +#define AUFS_VERSION   "6.5-20230904"
39074 +
39075 +/* todo? move this to linux-2.6.19/include/magic.h */
39076 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39077 +
39078 +/* ---------------------------------------------------------------------- */
39079 +
39080 +#ifdef __KERNEL__
39081 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
39082 +typedef int8_t aufs_bindex_t;
39083 +#define AUFS_BRANCH_MAX 127
39084 +#else
39085 +typedef int16_t aufs_bindex_t;
39086 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
39087 +#define AUFS_BRANCH_MAX 511
39088 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39089 +#define AUFS_BRANCH_MAX 1023
39090 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39091 +#define AUFS_BRANCH_MAX 32767
39092 +#endif
39093 +#endif
39094 +
39095 +#ifndef AUFS_BRANCH_MAX
39096 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39097 +#endif
39098 +#endif /* __KERNEL__ */
39099 +
39100 +/* ---------------------------------------------------------------------- */
39101 +
39102 +#define AUFS_FSTYPE            AUFS_NAME
39103 +
39104 +#define AUFS_ROOT_INO          2
39105 +#define AUFS_FIRST_INO         11
39106 +
39107 +#define AUFS_WH_PFX            ".wh."
39108 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
39109 +#define AUFS_WH_TMP_LEN                4
39110 +/* a limit for rmdir/rename a dir and copyup */
39111 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
39112 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
39113 +                               - 1                     /* dot */\
39114 +                               - AUFS_WH_TMP_LEN)      /* hex */
39115 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
39116 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
39117 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
39118 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
39119 +#define AUFS_DIRWH_DEF         3
39120 +#define AUFS_RDCACHE_DEF       10 /* seconds */
39121 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
39122 +#define AUFS_RDBLK_DEF         512 /* bytes */
39123 +#define AUFS_RDHASH_DEF                32
39124 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
39125 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
39126 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
39127 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
39128 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
39129 +
39130 +/* pseudo-link maintenace under /proc */
39131 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
39132 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
39133 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39134 +
39135 +/* dirren, renamed dir */
39136 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
39137 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
39138 +/* whiteouted doubly */
39139 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
39140 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39141 +
39142 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39143 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39144 +
39145 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39146 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39147 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39148 +
39149 +/* doubly whiteouted */
39150 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39151 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39152 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39153 +
39154 +/* branch permissions and attributes */
39155 +#define AUFS_BRPERM_RW         "rw"
39156 +#define AUFS_BRPERM_RO         "ro"
39157 +#define AUFS_BRPERM_RR         "rr"
39158 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39159 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39160 +#define AUFS_BRATTR_FHSM       "fhsm"
39161 +#define AUFS_BRATTR_UNPIN      "unpin"
39162 +#define AUFS_BRATTR_ICEX       "icex"
39163 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39164 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39165 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39166 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39167 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39168 +#define AUFS_BRRATTR_WH                "wh"
39169 +#define AUFS_BRWATTR_NLWH      "nolwh"
39170 +#define AUFS_BRWATTR_MOO       "moo"
39171 +
39172 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39173 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39174 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39175 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39176 +
39177 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39178 +#define AuBrAttr_COO_ALL       (1 << 4)
39179 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39180 +
39181 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39182 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39183 +                                                  branch. meaningless since
39184 +                                                  linux-3.18-rc1 */
39185 +
39186 +/* ignore error in copying XATTR */
39187 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39188 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39189 +#define AuBrAttr_ICEX_TR       (1 << 9)
39190 +#define AuBrAttr_ICEX_USR      (1 << 10)
39191 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39192 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39193 +                                | AuBrAttr_ICEX_SYS    \
39194 +                                | AuBrAttr_ICEX_TR     \
39195 +                                | AuBrAttr_ICEX_USR    \
39196 +                                | AuBrAttr_ICEX_OTH)
39197 +
39198 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39199 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39200 +
39201 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39202 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39203 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39204 +
39205 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39206 +
39207 +/* #warning test userspace */
39208 +#ifdef __KERNEL__
39209 +#ifndef CONFIG_AUFS_FHSM
39210 +#undef AuBrAttr_FHSM
39211 +#define AuBrAttr_FHSM          0
39212 +#endif
39213 +#ifndef CONFIG_AUFS_XATTR
39214 +#undef AuBrAttr_ICEX
39215 +#define AuBrAttr_ICEX          0
39216 +#undef AuBrAttr_ICEX_SEC
39217 +#define AuBrAttr_ICEX_SEC      0
39218 +#undef AuBrAttr_ICEX_SYS
39219 +#define AuBrAttr_ICEX_SYS      0
39220 +#undef AuBrAttr_ICEX_TR
39221 +#define AuBrAttr_ICEX_TR       0
39222 +#undef AuBrAttr_ICEX_USR
39223 +#define AuBrAttr_ICEX_USR      0
39224 +#undef AuBrAttr_ICEX_OTH
39225 +#define AuBrAttr_ICEX_OTH      0
39226 +#endif
39227 +#endif
39228 +
39229 +/* the longest combination */
39230 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39231 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39232 +                              "+" AUFS_BRATTR_COO_REG          \
39233 +                              "+" AUFS_BRATTR_FHSM             \
39234 +                              "+" AUFS_BRATTR_UNPIN            \
39235 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39236 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39237 +                              "+" AUFS_BRATTR_ICEX_USR         \
39238 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39239 +                              "+" AUFS_BRWATTR_NLWH)
39240 +
39241 +typedef struct {
39242 +       char a[AuBrPermStrSz];
39243 +} au_br_perm_str_t;
39244 +
39245 +static inline int au_br_writable(int brperm)
39246 +{
39247 +       return brperm & AuBrPerm_RW;
39248 +}
39249 +
39250 +static inline int au_br_whable(int brperm)
39251 +{
39252 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39253 +}
39254 +
39255 +static inline int au_br_wh_linkable(int brperm)
39256 +{
39257 +       return !(brperm & AuBrWAttr_NoLinkWH);
39258 +}
39259 +
39260 +static inline int au_br_cmoo(int brperm)
39261 +{
39262 +       return brperm & AuBrAttr_CMOO_Mask;
39263 +}
39264 +
39265 +static inline int au_br_fhsm(int brperm)
39266 +{
39267 +       return brperm & AuBrAttr_FHSM;
39268 +}
39269 +
39270 +/* ---------------------------------------------------------------------- */
39271 +
39272 +/* ioctl */
39273 +enum {
39274 +       /* readdir in userspace */
39275 +       AuCtl_RDU,
39276 +       AuCtl_RDU_INO,
39277 +
39278 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39279 +       AuCtl_IBUSY,    /* busy inode */
39280 +       AuCtl_MVDOWN,   /* move-down */
39281 +       AuCtl_BR,       /* info about branches */
39282 +       AuCtl_FHSM_FD   /* connection for fhsm */
39283 +};
39284 +
39285 +/* borrowed from linux/include/linux/kernel.h */
39286 +#ifndef ALIGN
39287 +#ifdef _GNU_SOURCE
39288 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39289 +#else
39290 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39291 +#endif
39292 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39293 +#endif
39294 +
39295 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39296 +#ifndef __aligned
39297 +#define __aligned(x)                   __attribute__((aligned(x)))
39298 +#endif
39299 +
39300 +#ifdef __KERNEL__
39301 +#ifndef __packed
39302 +#define __packed                       __attribute__((packed))
39303 +#endif
39304 +#endif
39305 +
39306 +struct au_rdu_cookie {
39307 +       uint64_t        h_pos;
39308 +       int16_t         bindex;
39309 +       uint8_t         flags;
39310 +       uint8_t         pad;
39311 +       uint32_t        generation;
39312 +} __aligned(8);
39313 +
39314 +struct au_rdu_ent {
39315 +       uint64_t        ino;
39316 +       int16_t         bindex;
39317 +       uint8_t         type;
39318 +       uint8_t         nlen;
39319 +       uint8_t         wh;
39320 +       char            name[];
39321 +} __aligned(8);
39322 +
39323 +static inline int au_rdu_len(int nlen)
39324 +{
39325 +       /* include the terminating NULL */
39326 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39327 +                    sizeof(uint64_t));
39328 +}
39329 +
39330 +union au_rdu_ent_ul {
39331 +       struct au_rdu_ent __user        *e;
39332 +       uint64_t                        ul;
39333 +};
39334 +
39335 +enum {
39336 +       AufsCtlRduV_SZ,
39337 +       AufsCtlRduV_End
39338 +};
39339 +
39340 +struct aufs_rdu {
39341 +       /* input */
39342 +       union {
39343 +               uint64_t        sz;     /* AuCtl_RDU */
39344 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39345 +       };
39346 +       union au_rdu_ent_ul     ent;
39347 +       uint16_t                verify[AufsCtlRduV_End];
39348 +
39349 +       /* input/output */
39350 +       uint32_t                blk;
39351 +
39352 +       /* output */
39353 +       union au_rdu_ent_ul     tail;
39354 +       /* number of entries which were added in a single call */
39355 +       uint64_t                rent;
39356 +       uint8_t                 full;
39357 +       uint8_t                 shwh;
39358 +
39359 +       struct au_rdu_cookie    cookie;
39360 +} __aligned(8);
39361 +
39362 +/* ---------------------------------------------------------------------- */
39363 +
39364 +/* dirren. the branch is identified by the filename who contains this */
39365 +struct au_drinfo {
39366 +       uint64_t ino;
39367 +       union {
39368 +               uint8_t oldnamelen;
39369 +               uint64_t _padding;
39370 +       };
39371 +       uint8_t oldname[];
39372 +} __aligned(8);
39373 +
39374 +struct au_drinfo_fdata {
39375 +       uint32_t magic;
39376 +       struct au_drinfo drinfo;
39377 +} __aligned(8);
39378 +
39379 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39380 +/* future */
39381 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39382 +
39383 +/* ---------------------------------------------------------------------- */
39384 +
39385 +struct aufs_wbr_fd {
39386 +       uint32_t        oflags;
39387 +       int16_t         brid;
39388 +} __aligned(8);
39389 +
39390 +/* ---------------------------------------------------------------------- */
39391 +
39392 +struct aufs_ibusy {
39393 +       uint64_t        ino, h_ino;
39394 +       int16_t         bindex;
39395 +} __aligned(8);
39396 +
39397 +/* ---------------------------------------------------------------------- */
39398 +
39399 +/* error code for move-down */
39400 +/* the actual message strings are implemented in aufs-util.git */
39401 +enum {
39402 +       EAU_MVDOWN_OPAQUE = 1,
39403 +       EAU_MVDOWN_WHITEOUT,
39404 +       EAU_MVDOWN_UPPER,
39405 +       EAU_MVDOWN_BOTTOM,
39406 +       EAU_MVDOWN_NOUPPER,
39407 +       EAU_MVDOWN_NOLOWERBR,
39408 +       EAU_Last
39409 +};
39410 +
39411 +/* flags for move-down */
39412 +#define AUFS_MVDOWN_DMSG       1
39413 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39414 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39415 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39416 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39417 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39418 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39419 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39420 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39421 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39422 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39423 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39424 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39425 +
39426 +/* index for move-down */
39427 +enum {
39428 +       AUFS_MVDOWN_UPPER,
39429 +       AUFS_MVDOWN_LOWER,
39430 +       AUFS_MVDOWN_NARRAY
39431 +};
39432 +
39433 +/*
39434 + * additional info of move-down
39435 + * number of free blocks and inodes.
39436 + * subset of struct kstatfs, but smaller and always 64bit.
39437 + */
39438 +struct aufs_stfs {
39439 +       uint64_t        f_blocks;
39440 +       uint64_t        f_bavail;
39441 +       uint64_t        f_files;
39442 +       uint64_t        f_ffree;
39443 +};
39444 +
39445 +struct aufs_stbr {
39446 +       int16_t                 brid;   /* optional input */
39447 +       int16_t                 bindex; /* output */
39448 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39449 +} __aligned(8);
39450 +
39451 +struct aufs_mvdown {
39452 +       uint32_t                flags;                  /* input/output */
39453 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39454 +       int8_t                  au_errno;               /* output */
39455 +} __aligned(8);
39456 +
39457 +/* ---------------------------------------------------------------------- */
39458 +
39459 +union aufs_brinfo {
39460 +       /* PATH_MAX may differ between kernel-space and user-space */
39461 +       char    _spacer[4096];
39462 +       struct {
39463 +               int16_t id;
39464 +               int     perm;
39465 +               char    path[];
39466 +       };
39467 +} __aligned(8);
39468 +
39469 +/* ---------------------------------------------------------------------- */
39470 +
39471 +#define AuCtlType              'A'
39472 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39473 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39474 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39475 +                                    struct aufs_wbr_fd)
39476 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39477 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39478 +                                     struct aufs_mvdown)
39479 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39480 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39481 +
39482 +#endif /* __AUFS_TYPE_H__ */
39483 SPDX-License-Identifier: GPL-2.0
39484 aufs6.5 loopback patch
39485
39486 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39487 index cbefd23beb44..53b9f81161d4 100644
39488 --- a/drivers/block/loop.c
39489 +++ b/drivers/block/loop.c
39490 @@ -54,7 +54,7 @@ struct loop_device {
39491         int             lo_flags;
39492         char            lo_file_name[LO_NAME_SIZE];
39493  
39494 -       struct file *   lo_backing_file;
39495 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39496         struct block_device *lo_device;
39497  
39498         gfp_t           old_gfp_mask;
39499 @@ -510,6 +510,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39500                                 lo->use_dio);
39501  }
39502  
39503 +static struct file *loop_real_file(struct file *file)
39504 +{
39505 +       struct file *f = NULL;
39506 +
39507 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39508 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39509 +       return f;
39510 +}
39511 +
39512  static void loop_reread_partitions(struct loop_device *lo)
39513  {
39514         int rc;
39515 @@ -567,6 +576,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39516  {
39517         struct file *file = fget(arg);
39518         struct file *old_file;
39519 +       struct file *f, *virt_file = NULL, *old_virt_file;
39520         int error;
39521         bool partscan;
39522         bool is_loop;
39523 @@ -590,11 +600,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39524         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39525                 goto out_err;
39526  
39527 +       f = loop_real_file(file);
39528 +       if (f) {
39529 +               virt_file = file;
39530 +               file = f;
39531 +               get_file(file);
39532 +       }
39533 +
39534         error = loop_validate_file(file, bdev);
39535         if (error)
39536                 goto out_err;
39537  
39538         old_file = lo->lo_backing_file;
39539 +       old_virt_file = lo->lo_backing_virt_file;
39540  
39541         error = -EINVAL;
39542  
39543 @@ -607,6 +625,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39544         blk_mq_freeze_queue(lo->lo_queue);
39545         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39546         lo->lo_backing_file = file;
39547 +       lo->lo_backing_virt_file = virt_file;
39548         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39549         mapping_set_gfp_mask(file->f_mapping,
39550                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39551 @@ -629,6 +648,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39552          * dependency.
39553          */
39554         fput(old_file);
39555 +       if (old_virt_file)
39556 +               fput(old_virt_file);
39557         if (partscan)
39558                 loop_reread_partitions(lo);
39559  
39560 @@ -642,6 +663,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39561         loop_global_unlock(lo, is_loop);
39562  out_putf:
39563         fput(file);
39564 +       if (virt_file)
39565 +               fput(virt_file);
39566         goto done;
39567  }
39568  
39569 @@ -1013,6 +1036,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39570                           const struct loop_config *config)
39571  {
39572         struct file *file = fget(config->fd);
39573 +       struct file *f, *virt_file = NULL;
39574         struct inode *inode;
39575         struct address_space *mapping;
39576         int error;
39577 @@ -1028,6 +1052,13 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39578         /* This is safe, since we have a reference from open(). */
39579         __module_get(THIS_MODULE);
39580  
39581 +       f = loop_real_file(file);
39582 +       if (f) {
39583 +               virt_file = file;
39584 +               file = f;
39585 +               get_file(file);
39586 +       }
39587 +
39588         /*
39589          * If we don't hold exclusive handle for the device, upgrade to it
39590          * here to avoid changing device under exclusive owner.
39591 @@ -1091,6 +1122,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39592         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39593         lo->lo_device = bdev;
39594         lo->lo_backing_file = file;
39595 +       lo->lo_backing_virt_file = virt_file;
39596         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39597         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39598  
39599 @@ -1146,6 +1178,8 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39600                 bd_abort_claiming(bdev, loop_configure);
39601  out_putf:
39602         fput(file);
39603 +       if (virt_file)
39604 +               fput(virt_file);
39605         /* This is safe: open() is still holding a reference. */
39606         module_put(THIS_MODULE);
39607         return error;
39608 @@ -1154,6 +1188,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39609  static void __loop_clr_fd(struct loop_device *lo, bool release)
39610  {
39611         struct file *filp;
39612 +       struct file *virt_filp = lo->lo_backing_virt_file;
39613         gfp_t gfp = lo->old_gfp_mask;
39614  
39615         if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
39616 @@ -1170,6 +1205,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39617         spin_lock_irq(&lo->lo_lock);
39618         filp = lo->lo_backing_file;
39619         lo->lo_backing_file = NULL;
39620 +       lo->lo_backing_virt_file = NULL;
39621         spin_unlock_irq(&lo->lo_lock);
39622  
39623         lo->lo_device = NULL;
39624 @@ -1232,6 +1268,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39625          * fput can take open_mutex which is usually taken before lo_mutex.
39626          */
39627         fput(filp);
39628 +       if (virt_filp)
39629 +               fput(virt_filp);
39630  }
39631  
39632  static int loop_clr_fd(struct loop_device *lo)
39633 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39634 index 6b9cfc26b56b..e8878a13a0c8 100644
39635 --- a/fs/aufs/f_op.c
39636 +++ b/fs/aufs/f_op.c
39637 @@ -309,7 +309,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39638         if (IS_ERR(h_file))
39639                 goto out;
39640  
39641 -       if (au_test_loopback_kthread()) {
39642 +       if (0 && au_test_loopback_kthread()) {
39643                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39644                 if (file->f_mapping != h_file->f_mapping) {
39645                         file->f_mapping = h_file->f_mapping;
39646 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39647 index 58043e31e5f3..e2bfae6f9d59 100644
39648 --- a/fs/aufs/loop.c
39649 +++ b/fs/aufs/loop.c
39650 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39651                 symbol_put(loop_backing_file);
39652         au_kfree_try_rcu(au_warn_loopback_array);
39653  }
39654 +
39655 +/* ---------------------------------------------------------------------- */
39656 +
39657 +/* support the loopback block device insude aufs */
39658 +
39659 +struct file *aufs_real_loop(struct file *file)
39660 +{
39661 +       struct file *f;
39662 +
39663 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39664 +       fi_read_lock(file);
39665 +       f = au_hf_top(file);
39666 +       fi_read_unlock(file);
39667 +       AuDebugOn(!f);
39668 +       return f;
39669 +}
39670 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39671 index 03d4908a6c03..34d356e181d5 100644
39672 --- a/fs/aufs/loop.h
39673 +++ b/fs/aufs/loop.h
39674 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39675  
39676  int au_loopback_init(void);
39677  void au_loopback_fin(void);
39678 +
39679 +struct file *aufs_real_loop(struct file *file);
39680  #else
39681  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39682  
39683 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39684  
39685  AuStubInt0(au_loopback_init, void)
39686  AuStubVoid(au_loopback_fin, void)
39687 +
39688 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39689  #endif /* BLK_DEV_LOOP */
39690  
39691  #endif /* __KERNEL__ */
39692 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39693 index 81922d4faf54..c8a62c267d72 100644
39694 --- a/fs/aufs/super.c
39695 +++ b/fs/aufs/super.c
39696 @@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39697         .show_options   = aufs_show_options,
39698         .statfs         = aufs_statfs,
39699         .put_super      = aufs_put_super,
39700 -       .sync_fs        = aufs_sync_fs
39701 +       .sync_fs        = aufs_sync_fs,
39702 +#ifdef CONFIG_AUFS_BDEV_LOOP
39703 +       .real_loop      = aufs_real_loop
39704 +#endif
39705  };
39706  
39707  /* ---------------------------------------------------------------------- */
39708 diff --git a/include/linux/fs.h b/include/linux/fs.h
39709 index 5a2db7b8eca5..fc74b8e852c3 100644
39710 --- a/include/linux/fs.h
39711 +++ b/include/linux/fs.h
39712 @@ -1944,6 +1944,11 @@ struct super_operations {
39713         long (*free_cached_objects)(struct super_block *,
39714                                     struct shrink_control *);
39715         void (*shutdown)(struct super_block *sb);
39716 +
39717 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39718 +       /* and aufs */
39719 +       struct file *(*real_loop)(struct file *);
39720 +#endif
39721  };
39722  
39723  /*
This page took 3.06203 seconds and 3 git commands to generate.